Skip to content

Commit bc24eda

Browse files
authored
fix(OpenAI): Preserve function call namespaces (#788)
* Support streaming compaction output * Preserve function call namespaces
1 parent b13f46f commit bc24eda

3 files changed

Lines changed: 67 additions & 1 deletion

File tree

src/Responses/Responses/Output/OutputFunctionToolCall.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use OpenAI\Testing\Responses\Concerns\Fakeable;
1010

1111
/**
12-
* @phpstan-type OutputFunctionToolCallType array{arguments: string, call_id: string, name: string, type: 'function_call', id: string, status: 'in_progress'|'completed'|'incomplete'}
12+
* @phpstan-type OutputFunctionToolCallType array{arguments: string, call_id: string, name: string, namespace?: ?string, type: 'function_call', id: string, status: 'in_progress'|'completed'|'incomplete'}
1313
*
1414
* @implements ResponseContract<OutputFunctionToolCallType>
1515
*/
@@ -33,6 +33,7 @@ private function __construct(
3333
public readonly string $type,
3434
public readonly string $id,
3535
public readonly string $status,
36+
public readonly ?string $namespace,
3637
) {}
3738

3839
/**
@@ -47,6 +48,7 @@ public static function from(array $attributes): self
4748
type: $attributes['type'],
4849
id: $attributes['id'],
4950
status: $attributes['status'],
51+
namespace: $attributes['namespace'] ?? null,
5052
);
5153
}
5254

@@ -59,6 +61,7 @@ public function toArray(): array
5961
'arguments' => $this->arguments,
6062
'call_id' => $this->callId,
6163
'name' => $this->name,
64+
'namespace' => $this->namespace,
6265
'type' => $this->type,
6366
'id' => $this->id,
6467
'status' => $this->status,

tests/Fixtures/Responses.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,22 @@ function outputCustomToolCall(): array
726726
];
727727
}
728728

729+
/**
730+
* @return array<string, mixed>
731+
*/
732+
function outputFunctionToolCall(): array
733+
{
734+
return [
735+
'arguments' => '{"customer_id":"CUST-12345"}',
736+
'call_id' => 'call_abc123',
737+
'name' => 'list_open_orders',
738+
'namespace' => 'crm',
739+
'type' => 'function_call',
740+
'id' => 'fc_abc123',
741+
'status' => 'completed',
742+
];
743+
}
744+
729745
/**
730746
* @return array<string, mixed>
731747
*/
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
use OpenAI\Responses\Responses\Output\OutputFunctionToolCall;
4+
5+
test('from', function () {
6+
$response = OutputFunctionToolCall::from(outputFunctionToolCall());
7+
8+
expect($response)
9+
->toBeInstanceOf(OutputFunctionToolCall::class)
10+
->arguments->toBe('{"customer_id":"CUST-12345"}')
11+
->callId->toBe('call_abc123')
12+
->name->toBe('list_open_orders')
13+
->namespace->toBe('crm')
14+
->type->toBe('function_call')
15+
->id->toBe('fc_abc123')
16+
->status->toBe('completed');
17+
});
18+
19+
test('from without namespace', function () {
20+
$attributes = outputFunctionToolCall();
21+
22+
unset($attributes['namespace']);
23+
24+
set_error_handler(static fn (int $errno, string $errstr): bool => throw new ErrorException($errstr), E_WARNING);
25+
26+
try {
27+
$response = OutputFunctionToolCall::from($attributes);
28+
} finally {
29+
restore_error_handler();
30+
}
31+
32+
expect($response->namespace)->toBeNull();
33+
});
34+
35+
test('as array accessible', function () {
36+
$response = OutputFunctionToolCall::from(outputFunctionToolCall());
37+
38+
expect($response['namespace'])->toBe('crm');
39+
});
40+
41+
test('to array', function () {
42+
$response = OutputFunctionToolCall::from(outputFunctionToolCall());
43+
44+
expect($response->toArray())
45+
->toBeArray()
46+
->toBe(outputFunctionToolCall());
47+
});

0 commit comments

Comments
 (0)