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
5 changes: 3 additions & 2 deletions packages/agent/src/lib/conversation-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,12 +516,13 @@ export function extractTextFromResponse(response: models.OpenResponsesResult): s
}

/**
* Extract tool calls from a response
* Extract tool calls from a completed response.
* Non-completed responses yield no tool calls.
*/
export function extractToolCallsFromResponse<TTools extends readonly Tool[]>(
response: models.OpenResponsesResult,
): ParsedToolCall<TTools[number]>[] {
if (!response.output) {
if (response.status !== 'completed' || !response.output) {
return [];
}

Expand Down
44 changes: 22 additions & 22 deletions packages/agent/src/lib/stream-transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -806,34 +806,33 @@ export function extractTextFromResponse(response: models.OpenResponsesResult): s
}

/**
* Whether the provider stopped this response at `max_output_tokens`. The
* `function_call` items on such a response are an unfinished batch: the model
* asked for the set together, and the last one carries whatever argument
* prefix fit in the budget. Executing the complete ones would hand the model a
* result set with a silent hole, and spend side effects on a turn that
* truncates the same way on the same budget, so none of them run. The caller
* sees every item and `incomplete_details`, and resumes once the budget is
* raised.
* Whether this response reached the `'completed'` terminal status and its
* `function_call` items are eligible for local tool execution.
*
* Non-completed responses (`incomplete` for `max_output_tokens`,
* `content_filter`, or any other reason; `failed`; `cancelled`; `in_progress`;
* `queued`; or any unknown status) must not trigger local tool execution:
* their output may be truncated, halted by a safety filter, errored,
* cancelled, or still in flight. The caller still receives the response with
* its output items, status, error, and `incompleteDetails` intact.
*/
function isTruncatedAtMaxOutputTokens(response: models.OpenResponsesResult): boolean {
return (
response.status === 'incomplete' && response.incompleteDetails?.reason === 'max_output_tokens'
);
function isResponseCompleted(response: models.OpenResponsesResult): boolean {
return response.status === 'completed';
}

/**
* Extract all tool calls from a completed response
* Returns parsed tool calls with arguments as objects (not JSON strings)
*
* A response truncated at `max_output_tokens` yields no tool calls: see
* `isTruncatedAtMaxOutputTokens`.
* Any response whose status is not `'completed'` yields no tool calls: see
* `isResponseCompleted`.
*/
export function extractToolCallsFromResponse(
response: models.OpenResponsesResult,
): ParsedToolCall<Tool>[] {
const toolCalls: ParsedToolCall<Tool>[] = [];

if (isTruncatedAtMaxOutputTokens(response)) {
if (!isResponseCompleted(response)) {
return toolCalls;
}

Expand Down Expand Up @@ -982,18 +981,19 @@ export async function* buildToolCallStream(
}

/**
* Check if a response contains any tool calls the loop should execute. A
* response truncated at `max_output_tokens` has none, even when its output
* carries a cut-off `function_call` item.
* Check if a response contains any tool calls the loop should execute. Only a
* response with `status === 'completed'` is eligible; any non-completed
* response (`incomplete`, `failed`, `cancelled`, `in_progress`, `queued`, etc.)
* returns `false`, even when its output carries `function_call` items.
*
* Scope: this and `extractToolCallsFromResponse` decide execution on the
* completed response. `buildToolCallStream` (`getToolCallsStream()`) is a
* materialized response. `buildToolCallStream` (`getToolCallsStream()`) is a
* consumer view that yields each call as its `output_item.done` arrives,
* before the terminal event says whether the turn was cut off, so it still
* reports the model's emitted calls, truncated one included.
* before the terminal event reports the final response status, so it still
* reports the model's emitted calls.
*/
export function responseHasToolCalls(response: models.OpenResponsesResult): boolean {
if (isTruncatedAtMaxOutputTokens(response)) {
if (!isResponseCompleted(response)) {
return false;
}
return response.output.some((item) => 'type' in item && item.type === 'function_call');
Expand Down
2 changes: 2 additions & 0 deletions packages/agent/tests/unit/get-usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function textResponse(id = 'resp_text', usage?: models.Usage | null): models.Ope
return {
id,
model: 'test-model-v1',
status: 'completed',
output: [
{
type: 'message',
Expand Down Expand Up @@ -94,6 +95,7 @@ function toolCallResponse(
return {
id,
model: 'test-model-v1',
status: 'completed',
output: [
{
type: 'function_call',
Expand Down
2 changes: 2 additions & 0 deletions packages/agent/tests/unit/hooks-post-model-call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function textResponse(id = 'resp_text', usage?: models.Usage | null): models.Ope
return {
id,
model: 'test-model-v1',
status: 'completed',
output: [
{
type: 'message',
Expand All @@ -76,6 +77,7 @@ function toolCallResponse(id = 'resp_tool'): models.OpenResponsesResult {
return {
id,
model: 'test-model-v1',
status: 'completed',
output: [
{
type: 'function_call',
Expand Down
2 changes: 2 additions & 0 deletions packages/agent/tests/unit/hooks-session-lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ afterEach(() => {
function textResponse(id = 'resp_text'): models.OpenResponsesResult {
return {
id,
status: 'completed',
output: [
{
type: 'message',
Expand All @@ -55,6 +56,7 @@ function textResponse(id = 'resp_text'): models.OpenResponsesResult {
function toolCallResponse(id = 'resp_tool'): models.OpenResponsesResult {
return {
id,
status: 'completed',
output: [
{
type: 'function_call',
Expand Down
2 changes: 2 additions & 0 deletions packages/agent/tests/unit/next-turn-tool-choice-loop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function allowed(names: string[]): models.ResponsesRequest['toolChoice'] {
function toolCallResponse(): models.OpenResponsesResult {
return {
id: 'resp_tool',
status: 'completed',
output: [
{
type: 'function_call',
Expand All @@ -59,6 +60,7 @@ function toolCallResponse(): models.OpenResponsesResult {
function textResponse(): models.OpenResponsesResult {
return {
id: 'resp_text',
status: 'completed',
output: [
{
type: 'message',
Expand Down
Loading
Loading