Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap function calling including error handling in Calls API #12

Merged
merged 9 commits into from
Oct 26, 2024

Conversation

thekid
Copy link
Member

@thekid thekid commented Oct 26, 2024

In essence, this PR simplifies function calling by handling JSON de- and encoding as well as supplying a default error handling mechanism:

Before

try {
  $return= json_encode($functions->invoke(
    $call['function']['name'],
    json_decode($call['function']['arguments'], true)
  );
} catch (Throwable $t) {
  $t->printStackTrace();
  $return= json_encode(['error' => nameof($t), 'message' => $t->getMessage()]);
}

After

// Setup calls
$calls= $functions->calls()->catching(fn($t) => $t->printStackTrace());

// Call the function, including handling JSON de- and encoding and converting
// caught exceptions to a serializable form.
$return= $calls->call($call['function']['name'], $call['function']['arguments']);

@thekid thekid added the enhancement New feature or request label Oct 26, 2024
@thekid thekid merged commit 56c037b into main Oct 26, 2024
12 checks passed
@thekid thekid deleted the feature/calls-api branch October 26, 2024 17:36
@thekid
Copy link
Member Author

thekid commented Oct 26, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant