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
22 changes: 14 additions & 8 deletions src/Responses/Responses/CreateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use OpenAI\Responses\Concerns\ArrayAccessible;
use OpenAI\Responses\Concerns\HasMetaInformation;
use OpenAI\Responses\Meta\MetaInformation;
use OpenAI\Responses\Responses\Output\OutputCodeInterpreterToolCall;
use OpenAI\Responses\Responses\Output\OutputComputerToolCall;
use OpenAI\Responses\Responses\Output\OutputFileSearchToolCall;
use OpenAI\Responses\Responses\Output\OutputFunctionToolCall;
Expand All @@ -20,6 +21,7 @@
use OpenAI\Responses\Responses\Output\OutputMessageContentOutputText;
use OpenAI\Responses\Responses\Output\OutputReasoning;
use OpenAI\Responses\Responses\Output\OutputWebSearchToolCall;
use OpenAI\Responses\Responses\Tool\CodeInterpreterTool;
use OpenAI\Responses\Responses\Tool\ComputerUseTool;
use OpenAI\Responses\Responses\Tool\FileSearchTool;
use OpenAI\Responses\Responses\Tool\FunctionTool;
Expand All @@ -42,12 +44,14 @@
* @phpstan-import-type OutputMcpApprovalRequestType from OutputMcpApprovalRequest
* @phpstan-import-type OutputMcpCallType from OutputMcpCall
* @phpstan-import-type OutputImageGenerationToolCallType from OutputImageGenerationToolCall
* @phpstan-import-type OutputCodeInterpreterToolCallType from OutputCodeInterpreterToolCall
* @phpstan-import-type ComputerUseToolType from ComputerUseTool
* @phpstan-import-type FileSearchToolType from FileSearchTool
* @phpstan-import-type ImageGenerationToolType from ImageGenerationTool
* @phpstan-import-type RemoteMcpToolType from RemoteMcpTool
* @phpstan-import-type FunctionToolType from FunctionTool
* @phpstan-import-type WebSearchToolType from WebSearchTool
* @phpstan-import-type CodeInterpreterToolType from CodeInterpreterTool
* @phpstan-import-type ErrorType from CreateResponseError
* @phpstan-import-type IncompleteDetailsType from CreateResponseIncompleteDetails
* @phpstan-import-type UsageType from CreateResponseUsage
Expand All @@ -56,8 +60,8 @@
* @phpstan-import-type ReasoningType from CreateResponseReasoning
*
* @phpstan-type ToolChoiceType 'none'|'auto'|'required'|FunctionToolChoiceType|HostedToolChoiceType
* @phpstan-type ToolsType array<int, ComputerUseToolType|FileSearchToolType|FunctionToolType|WebSearchToolType|ImageGenerationToolType|RemoteMcpToolType|ImageGenerationToolType>
* @phpstan-type OutputType array<int, OutputComputerToolCallType|OutputFileSearchToolCallType|OutputFunctionToolCallType|OutputMessageType|OutputReasoningType|OutputWebSearchToolCallType|OutputMcpListToolsType|OutputMcpApprovalRequestType|OutputMcpCallType|OutputImageGenerationToolCallType>
* @phpstan-type ToolsType array<int, ComputerUseToolType|FileSearchToolType|FunctionToolType|WebSearchToolType|ImageGenerationToolType|RemoteMcpToolType|CodeInterpreterToolType>
* @phpstan-type OutputType array<int, OutputComputerToolCallType|OutputFileSearchToolCallType|OutputFunctionToolCallType|OutputMessageType|OutputReasoningType|OutputWebSearchToolCallType|OutputMcpListToolsType|OutputMcpApprovalRequestType|OutputMcpCallType|OutputImageGenerationToolCallType|OutputCodeInterpreterToolCallType>
* @phpstan-type CreateResponseType array{id: string, object: 'response', created_at: int, status: 'completed'|'failed'|'in_progress'|'incomplete', error: ErrorType|null, incomplete_details: IncompleteDetailsType|null, instructions: string|null, max_output_tokens: int|null, model: string, output: OutputType, output_text: string|null, parallel_tool_calls: bool, previous_response_id: string|null, reasoning: ReasoningType|null, store: bool, temperature: float|null, text: ResponseFormatType, tool_choice: ToolChoiceType, tools: ToolsType, top_p: float|null, truncation: 'auto'|'disabled'|null, usage: UsageType|null, user: string|null, metadata: array<string, string>|null}
*
* @implements ResponseContract<CreateResponseType>
Expand All @@ -75,8 +79,8 @@ final class CreateResponse implements ResponseContract, ResponseHasMetaInformati
/**
* @param 'response' $object
* @param 'completed'|'failed'|'in_progress'|'incomplete' $status
* @param array<int, OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall> $output
* @param array<int, ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool|RemoteMcpTool|ImageGenerationTool> $tools
* @param array<int, OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall> $output
* @param array<int, ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool|RemoteMcpTool|ImageGenerationTool|CodeInterpreterTool> $tools
* @param 'auto'|'disabled'|null $truncation
* @param array<string, string> $metadata
*/
Expand Down Expand Up @@ -114,7 +118,7 @@ private function __construct(
public static function from(array $attributes, MetaInformation $meta): self
{
$output = array_map(
fn (array $output): OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall => match ($output['type']) {
fn (array $output): OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall => match ($output['type']) {
'message' => OutputMessage::from($output),
'file_search_call' => OutputFileSearchToolCall::from($output),
'function_call' => OutputFunctionToolCall::from($output),
Expand All @@ -125,6 +129,7 @@ public static function from(array $attributes, MetaInformation $meta): self
'mcp_approval_request' => OutputMcpApprovalRequest::from($output),
'mcp_call' => OutputMcpCall::from($output),
'image_generation_call' => OutputImageGenerationToolCall::from($output),
'code_interpreter_call' => OutputCodeInterpreterToolCall::from($output),
},
$attributes['output'],
);
Expand All @@ -137,13 +142,14 @@ public static function from(array $attributes, MetaInformation $meta): self
: $attributes['tool_choice'];

$tools = array_map(
fn (array $tool): ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool|RemoteMcpTool => match ($tool['type']) {
fn (array $tool): ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool|RemoteMcpTool|CodeInterpreterTool => match ($tool['type']) {
'file_search' => FileSearchTool::from($tool),
'web_search_preview', 'web_search_preview_2025_03_11' => WebSearchTool::from($tool),
'function' => FunctionTool::from($tool),
'computer_use_preview' => ComputerUseTool::from($tool),
'image_generation' => ImageGenerationTool::from($tool),
'mcp' => RemoteMcpTool::from($tool),
'code_interpreter' => CodeInterpreterTool::from($tool),
},
$attributes['tools'],
);
Expand Down Expand Up @@ -216,7 +222,7 @@ public function toArray(): array
'metadata' => $this->metadata ?? [],
'model' => $this->model,
'output' => array_map(
fn (OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall $output): array => $output->toArray(),
fn (OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall $output): array => $output->toArray(),
$this->output
),
'parallel_tool_calls' => $this->parallelToolCalls,
Expand All @@ -229,7 +235,7 @@ public function toArray(): array
? $this->toolChoice
: $this->toolChoice->toArray(),
'tools' => array_map(
fn (ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool|RemoteMcpTool $tool): array => $tool->toArray(),
fn (ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool|RemoteMcpTool|CodeInterpreterTool $tool): array => $tool->toArray(),
$this->tools
),
'top_p' => $this->topP,
Expand Down
11 changes: 10 additions & 1 deletion src/Responses/Responses/CreateStreamedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use OpenAI\Contracts\ResponseContract;
use OpenAI\Exceptions\UnknownEventException;
use OpenAI\Responses\Concerns\ArrayAccessible;
use OpenAI\Responses\Responses\Streaming\CodeInterpreterCall;
use OpenAI\Responses\Responses\Streaming\CodeInterpreterCodeDelta;
use OpenAI\Responses\Responses\Streaming\CodeInterpreterCodeDone;
use OpenAI\Responses\Responses\Streaming\ContentPart;
use OpenAI\Responses\Responses\Streaming\Error;
use OpenAI\Responses\Responses\Streaming\FileSearchCall;
Expand Down Expand Up @@ -47,7 +50,7 @@ final class CreateStreamedResponse implements ResponseContract

private function __construct(
public readonly string $event,
public readonly CreateResponse|OutputItem|ContentPart|OutputTextDelta|OutputTextAnnotationAdded|OutputTextDone|RefusalDelta|RefusalDone|FunctionCallArgumentsDelta|FunctionCallArgumentsDone|FileSearchCall|WebSearchCall|ReasoningSummaryPart|ReasoningSummaryTextDelta|ReasoningSummaryTextDone|McpListTools|McpListToolsInProgress|McpCall|McpCallArgumentsDelta|McpCallArgumentsDone|ImageGenerationPart|ImageGenerationPartialImage|Error $response,
public readonly CreateResponse|OutputItem|ContentPart|OutputTextDelta|OutputTextAnnotationAdded|OutputTextDone|RefusalDelta|RefusalDone|FunctionCallArgumentsDelta|FunctionCallArgumentsDone|FileSearchCall|WebSearchCall|CodeInterpreterCall|CodeInterpreterCodeDelta|CodeInterpreterCodeDone|ReasoningSummaryPart|ReasoningSummaryTextDelta|ReasoningSummaryTextDone|McpListTools|McpListToolsInProgress|McpCall|McpCallArgumentsDelta|McpCallArgumentsDone|ImageGenerationPart|ImageGenerationPartialImage|Error $response,
) {}

/**
Expand Down Expand Up @@ -82,6 +85,12 @@ public static function from(array $attributes): self
'response.web_search_call.in_progress',
'response.web_search_call.searching',
'response.web_search_call.completed' => WebSearchCall::from($attributes, $meta), // @phpstan-ignore-line
'response.code_interpreter_call.in_progress',
'response.code_interpreter_call.running',
'response.code_interpreter_call.interpreting',
'response.code_interpreter_call.completed' => CodeInterpreterCall::from($attributes, $meta), // @phpstan-ignore-line
'response.code_interpreter_call_code.delta' => CodeInterpreterCodeDelta::from($attributes, $meta), // @phpstan-ignore-line
'response.code_interpreter_call_code.done' => CodeInterpreterCodeDone::from($attributes, $meta), // @phpstan-ignore-line
'response.reasoning_summary_part.added',
'response.reasoning_summary_part.done' => ReasoningSummaryPart::from($attributes, $meta), // @phpstan-ignore-line
'response.reasoning_summary_text.delta' => ReasoningSummaryTextDelta::from($attributes, $meta), // @phpstan-ignore-line
Expand Down
51 changes: 51 additions & 0 deletions src/Responses/Responses/Output/CodeInterpreter/CodeFileObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace OpenAI\Responses\Responses\Output\CodeInterpreter;

use OpenAI\Contracts\ResponseContract;
use OpenAI\Responses\Concerns\ArrayAccessible;
use OpenAI\Testing\Responses\Concerns\Fakeable;

/**
* @phpstan-type CodeFileObjectType array{file_id: string, mime_type: string}
*
* @implements ResponseContract<CodeFileObjectType>
*/
final class CodeFileObject implements ResponseContract
{
/**
* @use ArrayAccessible<CodeFileObjectType>
*/
use ArrayAccessible;

use Fakeable;

private function __construct(
public readonly string $fileId,
public readonly string $mimeType,
) {}

/**
* @param CodeFileObjectType $attributes
*/
public static function from(array $attributes): self
{
return new self(
fileId: $attributes['file_id'],
mimeType: $attributes['mime_type'],
);
}

/**
* {@inheritDoc}
*/
public function toArray(): array
{
return [
'file_id' => $this->fileId,
'mime_type' => $this->mimeType,
];
}
}
65 changes: 65 additions & 0 deletions src/Responses/Responses/Output/CodeInterpreter/CodeFileOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace OpenAI\Responses\Responses\Output\CodeInterpreter;

use OpenAI\Contracts\ResponseContract;
use OpenAI\Responses\Concerns\ArrayAccessible;
use OpenAI\Testing\Responses\Concerns\Fakeable;

/**
* @phpstan-import-type CodeFileObjectType from CodeFileObject
*
* @phpstan-type CodeFileOutputType array{files: array<int, CodeFileObjectType>, type: 'files'}
*
* @implements ResponseContract<CodeFileOutputType>
*/
final class CodeFileOutput implements ResponseContract
{
/**
* @use ArrayAccessible<CodeFileOutputType>
*/
use ArrayAccessible;

use Fakeable;

/**
* @param array<int, CodeFileObject> $files
* @param 'files' $type
*/
private function __construct(
public readonly array $files,
public readonly string $type,
) {}

/**
* @param CodeFileOutputType $attributes
*/
public static function from(array $attributes): self
{
$files = array_map(
static fn (array $file): CodeFileObject => CodeFileObject::from($file),
$attributes['files']
);

return new self(
files: $files,
type: $attributes['type'],
);
}

/**
* {@inheritDoc}
*/
public function toArray(): array
{
return [
'type' => $this->type,
'files' => array_map(
static fn (CodeFileObject $file): array => $file->toArray(),
$this->files
),
];
}
}
54 changes: 54 additions & 0 deletions src/Responses/Responses/Output/CodeInterpreter/CodeTextOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace OpenAI\Responses\Responses\Output\CodeInterpreter;

use OpenAI\Contracts\ResponseContract;
use OpenAI\Responses\Concerns\ArrayAccessible;
use OpenAI\Testing\Responses\Concerns\Fakeable;

/**
* @phpstan-type CodeTextOutputType array{logs: string, type: 'logs'}
*
* @implements ResponseContract<CodeTextOutputType>
*/
final class CodeTextOutput implements ResponseContract
{
/**
* @use ArrayAccessible<CodeTextOutputType>
*/
use ArrayAccessible;

use Fakeable;

/**
* @param 'logs' $type
*/
private function __construct(
public readonly string $logs,
public readonly string $type,
) {}

/**
* @param CodeTextOutputType $attributes
*/
public static function from(array $attributes): self
{
return new self(
logs: $attributes['logs'],
type: $attributes['type'],
);
}

/**
* {@inheritDoc}
*/
public function toArray(): array
{
return [
'type' => $this->type,
'logs' => $this->logs,
];
}
}
87 changes: 87 additions & 0 deletions src/Responses/Responses/Output/OutputCodeInterpreterToolCall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace OpenAI\Responses\Responses\Output;

use OpenAI\Contracts\ResponseContract;
use OpenAI\Responses\Concerns\ArrayAccessible;
use OpenAI\Responses\Responses\Output\CodeInterpreter\CodeFileOutput;
use OpenAI\Responses\Responses\Output\CodeInterpreter\CodeTextOutput;
use OpenAI\Testing\Responses\Concerns\Fakeable;

/**
* @phpstan-import-type CodeFileOutputType from CodeFileOutput
* @phpstan-import-type CodeTextOutputType from CodeTextOutput
*
* @phpstan-type OutputType array<int, CodeFileOutputType|CodeTextOutputType>|null
* @phpstan-type OutputCodeInterpreterToolCallType array{code: string, id: string, outputs: OutputType, status: string, type: 'code_interpreter_call', container_id: string}
*
* @implements ResponseContract<OutputCodeInterpreterToolCallType>
*/
final class OutputCodeInterpreterToolCall implements ResponseContract
{
/**
* @use ArrayAccessible<OutputCodeInterpreterToolCallType>
*/
use ArrayAccessible;

use Fakeable;

/**
* @param array<int, CodeFileOutput|CodeTextOutput>|null $outputs
* @param 'code_interpreter_call' $type
*/
private function __construct(
public readonly string $code,
public readonly string $id,
public readonly ?array $outputs,
public readonly string $status,
public readonly string $type,
public readonly string $containerId,
) {}

/**
* @param OutputCodeInterpreterToolCallType $attributes
*/
public static function from(array $attributes): self
{
$outputs = null;

if (is_array($attributes['outputs'])) {
$outputs = array_map(
static fn (array $output): CodeFileOutput|CodeTextOutput => match ($output['type']) {
'files' => CodeFileOutput::from($output),
'logs' => CodeTextOutput::from($output),
},
$attributes['outputs']
);
}

return new self(
code: $attributes['code'],
id: $attributes['id'],
outputs: $outputs,
status: $attributes['status'],
type: $attributes['type'],
containerId: $attributes['container_id'],
);
}

/**
* {@inheritDoc}
*/
public function toArray(): array
{
return [
'code' => $this->code,
'id' => $this->id,
'outputs' => $this->outputs
? array_map(static fn (CodeFileOutput|CodeTextOutput $output): array => $output->toArray(), $this->outputs)
: null,
'status' => $this->status,
'type' => $this->type,
'container_id' => $this->containerId,
];
}
}
Loading