Skip to content

Commit f48d9f2

Browse files
committed
improve shape of turn context
1 parent af4f852 commit f48d9f2

File tree

5 files changed

+55
-50
lines changed

5 files changed

+55
-50
lines changed

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export type {
2424
StopCondition,
2525
StopWhen,
2626
Tool,
27-
ToolCallInfo,
2827
ToolPreliminaryResultEvent,
2928
ToolStreamEvent,
3029
ToolWithExecute,

src/lib/model-result.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,6 @@ export class ModelResult {
157157
// Build initial turn context (turn 0 for initial request)
158158
const initialContext: TurnContext = {
159159
numberOfTurns: 0,
160-
input: [],
161-
model: undefined,
162-
models: undefined,
163160
};
164161

165162
// Resolve any async functions first
@@ -248,8 +245,6 @@ export class ModelResult {
248245

249246
let currentResponse = initialResponse;
250247
let currentRound = 0;
251-
let currentInput: models.OpenResponsesInput =
252-
(this.options.request as models.OpenResponsesRequest).input || [];
253248

254249
while (true) {
255250
const currentToolCalls = extractToolCallsFromResponse(currentResponse);
@@ -274,17 +269,9 @@ export class ModelResult {
274269
response: currentResponse,
275270
});
276271

277-
// Build turn context for this round
278-
const resolvedRequest = this.options.request as models.OpenResponsesRequest;
272+
// Build turn context for this round (for async parameter resolution only)
279273
const turnContext: TurnContext = {
280274
numberOfTurns: currentRound + 1, // 1-indexed
281-
input: currentInput,
282-
...(resolvedRequest.model && {
283-
model: resolvedRequest.model,
284-
}),
285-
...(resolvedRequest.models && {
286-
models: resolvedRequest.models,
287-
}),
288275
};
289276

290277
// Resolve async functions for this turn
@@ -353,9 +340,6 @@ export class ModelResult {
353340
...toolResults,
354341
];
355342

356-
// Update current input for next iteration
357-
currentInput = newInput;
358-
359343
// Make new request with tool results
360344
const newRequest: models.OpenResponsesRequest = {
361345
...(this.options.request as models.OpenResponsesRequest),

src/lib/tool-orchestrator.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,31 @@ export async function executeToolLoop(
104104
return null;
105105
}
106106

107-
// Build turn context
107+
// Find the raw tool call from the response output
108+
const rawToolCall = currentResponse.output.find(
109+
(item): item is models.ResponsesOutputItemFunctionCall =>
110+
'type' in item && item.type === 'function_call' && item.callId === toolCall.id
111+
);
112+
113+
if (!rawToolCall) {
114+
throw new Error(`Could not find raw tool call for ${toolCall.id}`);
115+
}
116+
117+
// Convert to OpenResponsesFunctionToolCall format
118+
const openResponsesToolCall: models.OpenResponsesFunctionToolCall = {
119+
type: 'function_call' as const,
120+
callId: rawToolCall.callId,
121+
name: rawToolCall.name,
122+
arguments: rawToolCall.arguments,
123+
id: rawToolCall.callId,
124+
status: rawToolCall.status,
125+
};
126+
127+
// Build turn context with full information
108128
const turnContext = buildTurnContext({
109129
numberOfTurns: currentRound,
110-
messageHistory: conversationInput,
111-
model: currentRequest.model,
112-
models: currentRequest.models,
130+
toolCall: openResponsesToolCall,
131+
turnRequest: currentRequest,
113132
});
114133

115134
// Execute the tool

src/lib/tool-types.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ export enum ToolType {
1111
}
1212

1313
/**
14-
* Turn context passed to tool execute functions
14+
* Turn context passed to tool execute functions and async parameter resolution
1515
* Contains information about the current conversation state
1616
*/
1717
export interface TurnContext {
18-
toolCall: models.OpenResponsesFunctionToolCall;
19-
/** Number of tool execution turns so far (1-indexed: first turn = 1) */
18+
/** The specific tool call being executed (only available during tool execution) */
19+
toolCall?: models.OpenResponsesFunctionToolCall;
20+
/** Number of tool execution turns so far (1-indexed: first turn = 1, 0 = initial request) */
2021
numberOfTurns: number;
21-
turnRequest: models.OpenResponsesRequest;
22+
/** The full request being sent to the API (only available during tool execution) */
23+
turnRequest?: models.OpenResponsesRequest;
2224
}
2325

2426
/**
@@ -56,16 +58,6 @@ export type NextTurnParamsFunctions<TInput> = {
5658
) => NextTurnParamsContext[K] | Promise<NextTurnParamsContext[K]>;
5759
};
5860

59-
/**
60-
* Information about a tool call needed for nextTurnParams execution
61-
*/
62-
export interface ToolCallInfo {
63-
id: string;
64-
name: string;
65-
arguments: unknown;
66-
tool: Tool;
67-
}
68-
6961
/**
7062
* Base tool function interface with inputSchema
7163
*/

src/lib/turn-context.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,49 @@ import type { TurnContext } from './tool-types.js';
55
* Options for building a turn context
66
*/
77
export interface BuildTurnContextOptions {
8-
/** Number of turns so far (1-indexed) */
8+
/** Number of turns so far (1-indexed for tool execution, 0 for initial request) */
99
numberOfTurns: number;
10-
/** Current message history */
11-
messageHistory: models.OpenResponsesInput;
12-
/** Current model (if set) */
13-
model?: string | undefined;
14-
/** Current models array (if set) */
15-
models?: string[] | undefined;
10+
/** The specific tool call being executed (optional for initial/async resolution contexts) */
11+
toolCall?: models.OpenResponsesFunctionToolCall;
12+
/** The full request being sent to the API (optional for initial/async resolution contexts) */
13+
turnRequest?: models.OpenResponsesRequest;
1614
}
1715

1816
/**
19-
* Build a turn context for tool execution
17+
* Build a turn context for tool execution or async parameter resolution
2018
*
2119
* @param options - Options for building the context
2220
* @returns A TurnContext object
2321
*
2422
* @example
2523
* ```typescript
24+
* // For tool execution with full context
2625
* const context = buildTurnContext({
2726
* numberOfTurns: 1,
28-
* messageHistory: input,
29-
* model: 'anthropic/claude-3-sonnet',
27+
* toolCall: rawToolCall,
28+
* turnRequest: currentRequest,
29+
* });
30+
*
31+
* // For async parameter resolution (partial context)
32+
* const context = buildTurnContext({
33+
* numberOfTurns: 0,
3034
* });
3135
* ```
3236
*/
3337
export function buildTurnContext(options: BuildTurnContextOptions): TurnContext {
34-
return {
38+
const context: TurnContext = {
3539
numberOfTurns: options.numberOfTurns,
36-
input: options.messageHistory,
37-
model: options.model,
38-
models: options.models,
3940
};
41+
42+
if (options.toolCall !== undefined) {
43+
context.toolCall = options.toolCall;
44+
}
45+
46+
if (options.turnRequest !== undefined) {
47+
context.turnRequest = options.turnRequest;
48+
}
49+
50+
return context;
4051
}
4152

4253
/**

0 commit comments

Comments
 (0)