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
23 changes: 22 additions & 1 deletion src/Responses/Responses/CreateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
* @phpstan-type ToolChoiceType 'none'|'auto'|'required'|FunctionToolChoiceType|HostedToolChoiceType
* @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: InstructionsType, max_output_tokens: int|null, model: string, output: OutputType, output_text: string|null, parallel_tool_calls: bool, previous_response_id: string|null, prompt: ReferencePromptObjectType|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}
* @phpstan-type CreateResponseType array{id: string, background?: bool|null, object: 'response', created_at: int, status: 'completed'|'failed'|'in_progress'|'incomplete', error: ErrorType|null, incomplete_details: IncompleteDetailsType|null, instructions: InstructionsType, max_output_tokens: int|null, max_tool_calls?: int|null, model: string, output: OutputType, output_text: string|null, parallel_tool_calls: bool, previous_response_id: string|null, prompt: ReferencePromptObjectType|null, prompt_cache_key?: string|null, reasoning: ReasoningType|null, safety_identifier?: string|null, service_tier?: string|null, store: bool, temperature: float|null, text: ResponseFormatType, tool_choice: ToolChoiceType, tools: ToolsType, top_logprobs?: int|null, top_p: float|null, truncation: 'auto'|'disabled'|null, usage: UsageType|null, user: string|null, verbosity: string|null, metadata: array<string, string>|null}
*
* @implements ResponseContract<CreateResponseType>
*/
Expand All @@ -89,29 +89,36 @@ final class CreateResponse implements ResponseContract, ResponseHasMetaInformati
*/
private function __construct(
public readonly string $id,
public readonly ?bool $background,
public readonly string $object,
public readonly int $createdAt,
public readonly string $status,
public readonly ?CreateResponseError $error,
public readonly ?CreateResponseIncompleteDetails $incompleteDetails,
public readonly array|string|null $instructions,
public readonly ?int $maxToolCalls,
public readonly ?int $maxOutputTokens,
public readonly string $model,
public readonly array $output,
public readonly ?string $outputText,
public readonly bool $parallelToolCalls,
public readonly ?string $previousResponseId,
public readonly ?ReferencePromptObject $prompt,
public readonly ?string $promptCacheKey,
public readonly ?string $safetyIdentifier,
public readonly ?string $serviceTier,
public readonly ?CreateResponseReasoning $reasoning,
public readonly bool $store,
public readonly ?float $temperature,
public readonly CreateResponseFormat $text,
public readonly string|FunctionToolChoice|HostedToolChoice $toolChoice,
public readonly array $tools,
public readonly ?int $topLogProbs,
public readonly ?float $topP,
public readonly ?string $truncation,
public readonly ?CreateResponseUsage $usage,
public readonly ?string $user,
public readonly ?string $verbosity,
public readonly array $metadata,
private readonly MetaInformation $meta,
) {}
Expand Down Expand Up @@ -172,6 +179,7 @@ public static function from(array $attributes, MetaInformation $meta): self

return new self(
id: $attributes['id'],
background: $attributes['background'] ?? null,
object: $attributes['object'],
createdAt: $attributes['created_at'],
status: $attributes['status'],
Expand All @@ -182,6 +190,7 @@ public static function from(array $attributes, MetaInformation $meta): self
? CreateResponseIncompleteDetails::from($attributes['incomplete_details'])
: null,
instructions: $attributes['instructions'],
maxToolCalls: $attributes['max_tool_calls'] ?? null,
maxOutputTokens: $attributes['max_output_tokens'],
model: $attributes['model'],
output: $output,
Expand All @@ -191,6 +200,9 @@ public static function from(array $attributes, MetaInformation $meta): self
prompt: isset($attributes['prompt'])
? ReferencePromptObject::from($attributes['prompt'])
: null,
promptCacheKey: $attributes['prompt_cache_key'] ?? null,
safetyIdentifier: $attributes['safety_identifier'] ?? null,
serviceTier: $attributes['service_tier'] ?? null,
reasoning: isset($attributes['reasoning'])
? CreateResponseReasoning::from($attributes['reasoning'])
: null,
Expand All @@ -199,12 +211,14 @@ public static function from(array $attributes, MetaInformation $meta): self
text: CreateResponseFormat::from($attributes['text']),
toolChoice: $toolChoice,
tools: $tools,
topLogProbs: $attributes['top_logprobs'] ?? null,
topP: $attributes['top_p'],
truncation: $attributes['truncation'],
usage: isset($attributes['usage'])
? CreateResponseUsage::from($attributes['usage'])
: null,
user: $attributes['user'] ?? null,
verbosity: $attributes['verbosity'] ?? null,
metadata: $attributes['metadata'] ?? [],
meta: $meta,
);
Expand All @@ -219,12 +233,14 @@ public function toArray(): array
// @phpstan-ignore-next-line
return [
'id' => $this->id,
'background' => $this->background,
'object' => $this->object,
'created_at' => $this->createdAt,
'status' => $this->status,
'error' => $this->error?->toArray(),
'incomplete_details' => $this->incompleteDetails?->toArray(),
'instructions' => $this->instructions,
'max_tool_calls' => $this->maxToolCalls,
'max_output_tokens' => $this->maxOutputTokens,
'metadata' => $this->metadata ?? [],
'model' => $this->model,
Expand All @@ -235,6 +251,9 @@ public function toArray(): array
'parallel_tool_calls' => $this->parallelToolCalls,
'previous_response_id' => $this->previousResponseId,
'prompt' => $this->prompt?->toArray(),
'prompt_cache_key' => $this->promptCacheKey,
'safety_identifier' => $this->safetyIdentifier,
'service_tier' => $this->serviceTier,
'reasoning' => $this->reasoning?->toArray(),
'store' => $this->store,
'temperature' => $this->temperature,
Expand All @@ -246,10 +265,12 @@ public function toArray(): array
fn (ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool|RemoteMcpTool|CodeInterpreterTool $tool): array => $tool->toArray(),
$this->tools
),
'top_logprobs' => $this->topLogProbs,
'top_p' => $this->topP,
'truncation' => $this->truncation,
'usage' => $this->usage?->toArray(),
'user' => $this->user,
'verbosity' => $this->verbosity,
'output_text' => $this->outputText,
];
}
Expand Down
41 changes: 39 additions & 2 deletions src/Responses/Responses/RetrieveResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use OpenAI\Responses\Responses\Output\OutputMcpCall;
use OpenAI\Responses\Responses\Output\OutputMcpListTools;
use OpenAI\Responses\Responses\Output\OutputMessage;
use OpenAI\Responses\Responses\Output\OutputMessageContentOutputText;
use OpenAI\Responses\Responses\Output\OutputReasoning;
use OpenAI\Responses\Responses\Output\OutputWebSearchToolCall;
use OpenAI\Responses\Responses\Tool\CodeInterpreterTool;
Expand Down Expand Up @@ -64,7 +65,7 @@
* @phpstan-type ToolChoiceType 'none'|'auto'|'required'|FunctionToolChoiceType|HostedToolChoiceType
* @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 RetrieveResponseType array{id: string, object: 'response', created_at: int, status: 'completed'|'failed'|'in_progress'|'incomplete', error: ErrorType|null, incomplete_details: IncompleteDetailsType|null, instructions: InstructionsType, max_output_tokens: int|null, model: string, output: OutputType, parallel_tool_calls: bool, previous_response_id: string|null, prompt: ReferencePromptObjectType|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}
* @phpstan-type RetrieveResponseType array{id: string, background?: bool|null, object: 'response', created_at: int, status: 'completed'|'failed'|'in_progress'|'incomplete', error: ErrorType|null, incomplete_details: IncompleteDetailsType|null, instructions: InstructionsType, max_output_tokens: int|null, max_tool_calls?: int|null, model: string, output: OutputType, output_text: string|null, parallel_tool_calls: bool, previous_response_id: string|null, prompt: ReferencePromptObjectType|null, prompt_cache_key?: string|null, reasoning: ReasoningType|null, safety_identifier?: string|null, service_tier?: string|null, store: bool, temperature: float|null, text: ResponseFormatType, tool_choice: ToolChoiceType, tools: ToolsType, top_logprobs?: int|null, top_p: float|null, truncation: 'auto'|'disabled'|null, usage: UsageType|null, user: string|null, verbosity: string|null, metadata: array<string, string>|null}
*
* @implements ResponseContract<RetrieveResponseType>
*/
Expand All @@ -89,28 +90,36 @@ final class RetrieveResponse implements ResponseContract, ResponseHasMetaInforma
*/
private function __construct(
public readonly string $id,
public readonly ?bool $background,
public readonly string $object,
public readonly int $createdAt,
public readonly string $status,
public readonly ?CreateResponseError $error,
public readonly ?CreateResponseIncompleteDetails $incompleteDetails,
public readonly array|string|null $instructions,
public readonly ?int $maxToolCalls,
public readonly ?int $maxOutputTokens,
public readonly string $model,
public readonly array $output,
public readonly ?string $outputText,
public readonly bool $parallelToolCalls,
public readonly ?string $previousResponseId,
public readonly ?ReferencePromptObject $prompt,
public readonly ?string $promptCacheKey,
public readonly ?string $safetyIdentifier,
public readonly ?string $serviceTier,
public readonly ?CreateResponseReasoning $reasoning,
public readonly bool $store,
public readonly ?float $temperature,
public readonly CreateResponseFormat $text,
public readonly string|FunctionToolChoice|HostedToolChoice $toolChoice,
public readonly array $tools,
public readonly ?int $topLogProbs,
public readonly ?float $topP,
public readonly ?string $truncation,
public readonly ?CreateResponseUsage $usage,
public readonly ?string $user,
public readonly ?string $verbosity,
public array $metadata,
private readonly MetaInformation $meta,
) {}
Expand Down Expand Up @@ -157,8 +166,21 @@ public static function from(array $attributes, MetaInformation $meta): self
$attributes['tools'],
);

// Remake the sdk only property output_text.
$texts = [];
foreach ($output as $item) {
if ($item instanceof OutputMessage) {
foreach ($item->content as $content) {
if ($content instanceof OutputMessageContentOutputText) {
$texts[] = $content->text;
}
}
}
}

return new self(
id: $attributes['id'],
background: $attributes['background'] ?? null,
object: $attributes['object'],
createdAt: $attributes['created_at'],
status: $attributes['status'],
Expand All @@ -169,14 +191,19 @@ public static function from(array $attributes, MetaInformation $meta): self
? CreateResponseIncompleteDetails::from($attributes['incomplete_details'])
: null,
instructions: $attributes['instructions'],
maxToolCalls: $attributes['max_tool_calls'] ?? null,
maxOutputTokens: $attributes['max_output_tokens'],
model: $attributes['model'],
output: $output,
outputText: empty($texts) ? null : implode(' ', $texts),
parallelToolCalls: $attributes['parallel_tool_calls'],
previousResponseId: $attributes['previous_response_id'],
prompt: isset($attributes['prompt'])
? ReferencePromptObject::from($attributes['prompt'])
: null,
promptCacheKey: $attributes['prompt_cache_key'] ?? null,
safetyIdentifier: $attributes['safety_identifier'] ?? null,
serviceTier: $attributes['service_tier'] ?? null,
reasoning: isset($attributes['reasoning'])
? CreateResponseReasoning::from($attributes['reasoning'])
: null,
Expand All @@ -185,12 +212,14 @@ public static function from(array $attributes, MetaInformation $meta): self
text: CreateResponseFormat::from($attributes['text']),
toolChoice: $toolChoice,
tools: $tools,
topLogProbs: $attributes['top_logprobs'] ?? null,
topP: $attributes['top_p'],
truncation: $attributes['truncation'],
usage: isset($attributes['usage'])
? CreateResponseUsage::from($attributes['usage'])
: null,
user: $attributes['user'] ?? null,
verbosity: $attributes['verbosity'] ?? null,
metadata: $attributes['metadata'] ?? [],
meta: $meta,
);
Expand All @@ -205,22 +234,28 @@ public function toArray(): array
// @phpstan-ignore-next-line
return [
'id' => $this->id,
'background' => $this->background,
'object' => $this->object,
'created_at' => $this->createdAt,
'status' => $this->status,
'error' => $this->error?->toArray(),
'incomplete_details' => $this->incompleteDetails?->toArray(),
'instructions' => $this->instructions,
'max_tool_calls' => $this->maxToolCalls,
'max_output_tokens' => $this->maxOutputTokens,
'metadata' => $this->metadata ?? [],
'model' => $this->model,
'output' => array_map(
fn (OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpCall|OutputMcpApprovalRequest|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall $output): array => $output->toArray(),
fn (OutputMessage|OutputComputerToolCall|OutputFileSearchToolCall|OutputWebSearchToolCall|OutputFunctionToolCall|OutputReasoning|OutputMcpListTools|OutputMcpApprovalRequest|OutputMcpCall|OutputImageGenerationToolCall|OutputCodeInterpreterToolCall $output): array => $output->toArray(),
$this->output
),
'output_text' => $this->outputText,
'parallel_tool_calls' => $this->parallelToolCalls,
'previous_response_id' => $this->previousResponseId,
'prompt' => $this->prompt?->toArray(),
'prompt_cache_key' => $this->promptCacheKey,
'safety_identifier' => $this->safetyIdentifier,
'service_tier' => $this->serviceTier,
'reasoning' => $this->reasoning?->toArray(),
'store' => $this->store,
'temperature' => $this->temperature,
Expand All @@ -232,10 +267,12 @@ public function toArray(): array
fn (ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool|ImageGenerationTool|RemoteMcpTool|CodeInterpreterTool $tool): array => $tool->toArray(),
$this->tools
),
'top_logprobs' => $this->topLogProbs,
'top_p' => $this->topP,
'truncation' => $this->truncation,
'usage' => $this->usage?->toArray(),
'user' => $this->user,
'verbosity' => $this->verbosity,
];
}
}
Loading