Skip to content
Open
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
25 changes: 24 additions & 1 deletion packages/llm/src/protocols/openai-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ const OpenAIChatAssistantToolCall = Schema.Struct({
name: Schema.String,
arguments: Schema.String,
}),
extra_content: Schema.optional(
Schema.Struct({ google: Schema.Struct({ thought_signature: Schema.optional(Schema.String) }) }),
),
})
type OpenAIChatAssistantToolCall = Schema.Schema.Type<typeof OpenAIChatAssistantToolCall>

Expand Down Expand Up @@ -139,6 +142,9 @@ const OpenAIChatToolCallDelta = Schema.Struct({
index: Schema.Number,
id: optionalNull(Schema.String),
function: optionalNull(OpenAIChatToolCallDeltaFunction),
extra_content: optionalNull(
Schema.Struct({ google: Schema.Struct({ thought_signature: optionalNull(Schema.String) }) }),
),
})
type OpenAIChatToolCallDelta = Schema.Schema.Type<typeof OpenAIChatToolCallDelta>

Expand Down Expand Up @@ -193,13 +199,23 @@ const lowerToolChoice = (toolChoice: NonNullable<LLMRequest["toolChoice"]>) =>
tool: (name) => ({ type: "function" as const, function: { name } }),
})

const thoughtSignature = (part: ToolCallPart) => {
const google = part.providerMetadata?.google
return ProviderShared.isRecord(google) && typeof google.thoughtSignature === "string"
? google.thoughtSignature
: undefined
}

const lowerToolCall = (part: ToolCallPart): OpenAIChatAssistantToolCall => ({
id: part.id,
type: "function",
function: {
name: part.name,
arguments: ProviderShared.encodeJson(part.input),
},
extra_content: thoughtSignature(part)
? { google: { thought_signature: thoughtSignature(part) } }
: undefined,
})

const lowerMedia = Effect.fn("OpenAIChat.lowerMedia")(function* (part: MediaPart) {
Expand Down Expand Up @@ -431,7 +447,14 @@ const step = (state: ParserState, event: OpenAIChatEvent) =>
ADAPTER,
tools,
tool.index,
{ id: tool.id ?? undefined, name: tool.function?.name ?? undefined, text: tool.function?.arguments ?? "" },
{
id: tool.id ?? undefined,
name: tool.function?.name ?? undefined,
text: tool.function?.arguments ?? "",
providerMetadata: tool.extra_content?.google?.thought_signature
? { google: { thoughtSignature: tool.extra_content.google.thought_signature } }
: undefined,
},
"OpenAI Chat tool call delta is missing id or name",
)
if (ToolStream.isError(result)) return yield* result
Expand Down
9 changes: 7 additions & 2 deletions packages/llm/src/protocols/utils/tool-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ export const appendOrStart = <K extends StreamKey>(
route: string,
tools: State<K>,
key: K,
delta: { readonly id?: string; readonly name?: string; readonly text: string },
delta: {
readonly id?: string
readonly name?: string
readonly text: string
readonly providerMetadata?: ProviderMetadata
},
missingToolMessage: string,
): AppendOutcome<K> | LLMError => {
const current = tools[key]
Expand All @@ -131,7 +136,7 @@ export const appendOrStart = <K extends StreamKey>(
name,
input: `${current?.input ?? ""}${delta.text}`,
providerExecuted: current?.providerExecuted,
providerMetadata: current?.providerMetadata,
providerMetadata: delta.providerMetadata ?? current?.providerMetadata,
}
if (current && delta.text.length === 0 && current.id === id && current.name === name)
return { tools, tool: current, events: [] }
Expand Down
47 changes: 43 additions & 4 deletions packages/llm/test/provider/openai-chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,14 +552,52 @@ describe("OpenAI Chat route", () => {
}),
)

it.effect("round-trips Google OpenAI-compatible tool thought signatures", () =>
Effect.gen(function* () {
const call = ToolCallPart.make({
id: "call_1",
name: "lookup",
input: { query: "weather" },
providerMetadata: { google: { thoughtSignature: "tool_sig" } },
})
const prepared = yield* LLMClient.prepare<OpenAIChat.OpenAIChatBody>(
LLM.request({
model,
messages: [Message.user("Check the weather."), Message.assistant([call])],
tools: [{ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } }],
}),
)

expect(prepared.body.messages[1]).toEqual({
role: "assistant",
content: null,
tool_calls: [
{
id: "call_1",
type: "function",
function: { name: "lookup", arguments: '{"query":"weather"}' },
extra_content: { google: { thought_signature: "tool_sig" } },
},
],
})
}),
)

it.effect("assembles streamed tool call input", () =>
Effect.gen(function* () {
const body = sseEvents(
deltaChunk({
role: "assistant",
tool_calls: [{ index: 0, id: "call_1", function: { name: "lookup", arguments: '{"query"' } }],
tool_calls: [
{
index: 0,
id: "call_1",
function: { name: "lookup", arguments: '{"query"' },
},
],
}),
deltaChunk({ tool_calls: [{ index: 0, function: { arguments: ':"weather"}' } }] }),
deltaChunk({ tool_calls: [{ index: 0, extra_content: { google: { thought_signature: "tool_sig_late" } } }] }),
deltaChunk({}, "tool_calls"),
)
const response = yield* LLMClient.generate(
Expand All @@ -568,19 +606,20 @@ describe("OpenAI Chat route", () => {
}),
).pipe(Effect.provide(fixedResponse(body)))

const signatureMetadata = { google: { thoughtSignature: "tool_sig_late" } }
expect(response.events).toEqual([
{ type: "step-start", index: 0 },
{ type: "tool-input-start", id: "call_1", name: "lookup", providerMetadata: undefined },
{ type: "tool-input-start", id: "call_1", name: "lookup", providerMetadata: signatureMetadata },
{ type: "tool-input-delta", id: "call_1", name: "lookup", text: '{"query"' },
{ type: "tool-input-delta", id: "call_1", name: "lookup", text: ':"weather"}' },
{ type: "tool-input-end", id: "call_1", name: "lookup", providerMetadata: undefined },
{ type: "tool-input-end", id: "call_1", name: "lookup", providerMetadata: signatureMetadata },
{
type: "tool-call",
id: "call_1",
name: "lookup",
input: { query: "weather" },
providerExecuted: undefined,
providerMetadata: undefined,
providerMetadata: signatureMetadata,
},
{ type: "step-finish", index: 0, reason: "tool-calls", usage: undefined, providerMetadata: undefined },
{ type: "finish", reason: "tool-calls", usage: undefined },
Expand Down
Loading