Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 79 additions & 25 deletions src/Concerns/CallsTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,111 @@

namespace Prism\Prism\Concerns;

use Generator;
use Illuminate\Support\ItemNotFoundException;
use Illuminate\Support\MultipleItemsFoundException;
use Prism\Prism\Exceptions\PrismException;
use Prism\Prism\Streaming\EventID;
use Prism\Prism\Streaming\Events\ArtifactEvent;
use Prism\Prism\Streaming\Events\ToolResultEvent;
use Prism\Prism\Tool;
use Prism\Prism\ValueObjects\ToolCall;
use Prism\Prism\ValueObjects\ToolOutput;
use Prism\Prism\ValueObjects\ToolResult;
use Throwable;

trait CallsTools
{
/**
* Execute tools and return results (for non-streaming handlers).
*
* @param Tool[] $tools
* @param ToolCall[] $toolCalls
* @return ToolResult[]
*/
protected function callTools(array $tools, array $toolCalls): array
{
return array_map(
function (ToolCall $toolCall) use ($tools): ToolResult {
$toolResults = [];

// Consume generator to execute all tools and collect results
foreach ($this->callToolsAndYieldEvents($tools, $toolCalls, EventID::generate(), $toolResults) as $event) {
// Events are discarded for non-streaming handlers
}

return $toolResults;
}

/**
* Generate tool execution events and collect results (for streaming handlers).
*
* @param Tool[] $tools
* @param ToolCall[] $toolCalls
* @param ToolResult[] $toolResults Results are collected into this array by reference
* @return Generator<ToolResultEvent|ArtifactEvent>
*/
protected function callToolsAndYieldEvents(array $tools, array $toolCalls, string $messageId, array &$toolResults): Generator
{
foreach ($toolCalls as $toolCall) {
try {
$tool = $this->resolveTool($toolCall->name, $tools);
$output = call_user_func_array(
$tool->handle(...),
$toolCall->arguments()
);

try {
$output = call_user_func_array(
$tool->handle(...),
$toolCall->arguments()
);
if (is_string($output)) {
$output = new ToolOutput(result: $output);
}

$toolResult = new ToolResult(
toolCallId: $toolCall->id,
toolName: $toolCall->name,
args: $toolCall->arguments(),
result: $output->result,
toolCallResultId: $toolCall->resultId,
artifacts: $output->artifacts,
);

if (is_string($output)) {
$output = new ToolOutput(result: $output);
}
$toolResults[] = $toolResult;

return new ToolResult(
yield new ToolResultEvent(
id: EventID::generate(),
timestamp: time(),
toolResult: $toolResult,
messageId: $messageId,
success: true
);

foreach ($toolResult->artifacts as $artifact) {
yield new ArtifactEvent(
id: EventID::generate(),
timestamp: time(),
artifact: $artifact,
toolCallId: $toolCall->id,
toolName: $toolCall->name,
args: $toolCall->arguments(),
result: $output->result,
toolCallResultId: $toolCall->resultId,
artifacts: $output->artifacts,
messageId: $messageId,
);
} catch (Throwable $e) {
if ($e instanceof PrismException) {
throw $e;
}

throw PrismException::toolCallFailed($toolCall, $e);
}
} catch (PrismException $e) {
$toolResult = new ToolResult(
toolCallId: $toolCall->id,
toolName: $toolCall->name,
args: $toolCall->arguments(),
result: $e->getMessage(),
toolCallResultId: $toolCall->resultId,
);

},
$toolCalls
);
$toolResults[] = $toolResult;

yield new ToolResultEvent(
id: EventID::generate(),
timestamp: time(),
toolResult: $toolResult,
messageId: $messageId,
success: false,
error: $e->getMessage()
);
}
}
}

/**
Expand Down
60 changes: 1 addition & 59 deletions src/Providers/Anthropic/Handlers/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Prism\Prism\Providers\Anthropic\Maps\CitationsMapper;
use Prism\Prism\Providers\Anthropic\ValueObjects\AnthropicStreamState;
use Prism\Prism\Streaming\EventID;
use Prism\Prism\Streaming\Events\ArtifactEvent;
use Prism\Prism\Streaming\Events\CitationEvent;
use Prism\Prism\Streaming\Events\ErrorEvent;
use Prism\Prism\Streaming\Events\ProviderToolEvent;
Expand All @@ -31,15 +30,12 @@
use Prism\Prism\Streaming\Events\ThinkingStartEvent;
use Prism\Prism\Streaming\Events\ToolCallDeltaEvent;
use Prism\Prism\Streaming\Events\ToolCallEvent;
use Prism\Prism\Streaming\Events\ToolResultEvent;
use Prism\Prism\Text\Request;
use Prism\Prism\ValueObjects\Citation;
use Prism\Prism\ValueObjects\MessagePartWithCitations;
use Prism\Prism\ValueObjects\Messages\AssistantMessage;
use Prism\Prism\ValueObjects\Messages\ToolResultMessage;
use Prism\Prism\ValueObjects\ToolCall;
use Prism\Prism\ValueObjects\ToolOutput;
use Prism\Prism\ValueObjects\ToolResult;
use Prism\Prism\ValueObjects\Usage;
use Psr\Http\Message\StreamInterface;
use Throwable;
Expand Down Expand Up @@ -495,61 +491,7 @@ protected function handleToolCalls(Request $request, int $depth): Generator

// Execute tools and emit results
$toolResults = [];
foreach ($toolCalls as $toolCall) {
try {
$tool = $this->resolveTool($toolCall->name, $request->tools());
$output = call_user_func_array($tool->handle(...), $toolCall->arguments());

if (is_string($output)) {
$output = new ToolOutput(result: $output);
}

$toolResult = new ToolResult(
toolCallId: $toolCall->id,
toolName: $toolCall->name,
args: $toolCall->arguments(),
result: $output->result,
artifacts: $output->artifacts,
);

$toolResults[] = $toolResult;

yield new ToolResultEvent(
id: EventID::generate(),
timestamp: time(),
toolResult: $toolResult,
messageId: $this->state->messageId(),
success: true
);

foreach ($toolResult->artifacts as $artifact) {
yield new ArtifactEvent(
id: EventID::generate(),
timestamp: time(),
artifact: $artifact,
toolCallId: $toolCall->id,
toolName: $toolCall->name,
messageId: $this->state->messageId(),
);
}
} catch (Throwable $e) {
$errorResultObj = new ToolResult(
toolCallId: $toolCall->id,
toolName: $toolCall->name,
args: $toolCall->arguments(),
result: []
);

yield new ToolResultEvent(
id: EventID::generate(),
timestamp: time(),
toolResult: $errorResultObj,
messageId: $this->state->messageId(),
success: false,
error: $e->getMessage()
);
}
}
yield from $this->callToolsAndYieldEvents($request->tools(), $toolCalls, $this->state->messageId(), $toolResults);

// Add messages to request for next turn
if ($toolResults !== []) {
Expand Down
25 changes: 2 additions & 23 deletions src/Providers/DeepSeek/Handlers/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Prism\Prism\Providers\DeepSeek\Maps\ToolChoiceMap;
use Prism\Prism\Providers\DeepSeek\Maps\ToolMap;
use Prism\Prism\Streaming\EventID;
use Prism\Prism\Streaming\Events\ArtifactEvent;
use Prism\Prism\Streaming\Events\StepFinishEvent;
use Prism\Prism\Streaming\Events\StepStartEvent;
use Prism\Prism\Streaming\Events\StreamEndEvent;
Expand All @@ -33,7 +32,6 @@
use Prism\Prism\Streaming\Events\ThinkingEvent;
use Prism\Prism\Streaming\Events\ThinkingStartEvent;
use Prism\Prism\Streaming\Events\ToolCallEvent;
use Prism\Prism\Streaming\Events\ToolResultEvent;
use Prism\Prism\Streaming\StreamState;
use Prism\Prism\Text\Request;
use Prism\Prism\ValueObjects\Messages\AssistantMessage;
Expand Down Expand Up @@ -373,27 +371,8 @@ protected function handleToolCalls(Request $request, string $text, array $toolCa
);
}

$toolResults = $this->callTools($request->tools(), $mappedToolCalls);

foreach ($toolResults as $result) {
yield new ToolResultEvent(
id: EventID::generate(),
timestamp: time(),
toolResult: $result,
messageId: $this->state->messageId()
);

foreach ($result->artifacts as $artifact) {
yield new ArtifactEvent(
id: EventID::generate(),
timestamp: time(),
artifact: $artifact,
toolCallId: $result->toolCallId,
toolName: $result->toolName,
messageId: $this->state->messageId(),
);
}
}
$toolResults = [];
yield from $this->callToolsAndYieldEvents($request->tools(), $mappedToolCalls, $this->state->messageId(), $toolResults);

$request->addMessage(new AssistantMessage($text, $mappedToolCalls));
$request->addMessage(new ToolResultMessage($toolResults));
Expand Down
62 changes: 1 addition & 61 deletions src/Providers/Gemini/Handlers/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Prism\Prism\Providers\Gemini\Maps\ToolChoiceMap;
use Prism\Prism\Providers\Gemini\Maps\ToolMap;
use Prism\Prism\Streaming\EventID;
use Prism\Prism\Streaming\Events\ArtifactEvent;
use Prism\Prism\Streaming\Events\StepFinishEvent;
use Prism\Prism\Streaming\Events\StepStartEvent;
use Prism\Prism\Streaming\Events\StreamEndEvent;
Expand All @@ -30,14 +29,11 @@
use Prism\Prism\Streaming\Events\ThinkingEvent;
use Prism\Prism\Streaming\Events\ThinkingStartEvent;
use Prism\Prism\Streaming\Events\ToolCallEvent;
use Prism\Prism\Streaming\Events\ToolResultEvent;
use Prism\Prism\Streaming\StreamState;
use Prism\Prism\Text\Request;
use Prism\Prism\ValueObjects\Messages\AssistantMessage;
use Prism\Prism\ValueObjects\Messages\ToolResultMessage;
use Prism\Prism\ValueObjects\ToolCall;
use Prism\Prism\ValueObjects\ToolOutput;
use Prism\Prism\ValueObjects\ToolResult;
use Prism\Prism\ValueObjects\Usage;
use Psr\Http\Message\StreamInterface;
use Throwable;
Expand Down Expand Up @@ -313,63 +309,7 @@ protected function handleToolCalls(

// Execute tools and emit results
$toolResults = [];
foreach ($mappedToolCalls as $toolCall) {
try {
$tool = $this->resolveTool($toolCall->name, $request->tools());
$output = call_user_func_array($tool->handle(...), $toolCall->arguments());

if (is_string($output)) {
$output = new ToolOutput(result: $output);
}

$toolResult = new ToolResult(
toolCallId: $toolCall->id,
toolName: $toolCall->name,
args: $toolCall->arguments(),
result: is_array($output->result) ? $output->result : ['result' => $output->result],
artifacts: $output->artifacts,
);

$toolResults[] = $toolResult;

yield new ToolResultEvent(
id: EventID::generate(),
timestamp: time(),
toolResult: $toolResult,
messageId: $this->state->messageId(),
success: true
);

foreach ($toolResult->artifacts as $artifact) {
yield new ArtifactEvent(
id: EventID::generate(),
timestamp: time(),
artifact: $artifact,
toolCallId: $toolCall->id,
toolName: $toolCall->name,
messageId: $this->state->messageId(),
);
}
} catch (Throwable $e) {
$errorResult = new ToolResult(
toolCallId: $toolCall->id,
toolName: $toolCall->name,
args: $toolCall->arguments(),
result: []
);

$toolResults[] = $errorResult;

yield new ToolResultEvent(
id: EventID::generate(),
timestamp: time(),
toolResult: $errorResult,
messageId: $this->state->messageId(),
success: false,
error: $e->getMessage()
);
}
}
yield from $this->callToolsAndYieldEvents($request->tools(), $mappedToolCalls, $this->state->messageId(), $toolResults);

if ($toolResults !== []) {
$request->addMessage(new AssistantMessage($this->state->currentText(), $mappedToolCalls));
Expand Down
Loading