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
278 changes: 122 additions & 156 deletions packages/ai/src/protocols/open-responses.ts

Large diffs are not rendered by default.

23 changes: 16 additions & 7 deletions packages/ai/src/protocols/openai-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ const OpenAIResponsesImageGenerationTool = Schema.Struct({
size: Schema.optional(OpenAIImage.Size),
})

const OpenAIResponsesReasoningItem = Schema.StructWithRest(
Schema.Struct({
type: Schema.tag("reasoning"),
id: Schema.String,
summary: Schema.Array(Schema.Struct({ type: Schema.tag("summary_text"), text: Schema.String })),
content: Schema.optional(Schema.Array(Schema.Struct({ type: Schema.tag("reasoning_text"), text: Schema.String }))),
encrypted_content: optionalNull(Schema.String),
status: Schema.optional(Schema.Literals(["in_progress", "completed", "incomplete"])),
}),
[Schema.Record(Schema.String, Schema.Unknown)],
)

const OpenAIResponsesHostedToolItem = Schema.Union([
Schema.StructWithRest(
Schema.Struct({
Expand Down Expand Up @@ -75,7 +87,9 @@ const OpenAIResponsesToolChoice = Schema.Union([

const OpenAIResponsesCoreFields = {
...OpenResponses.coreFields,
input: Schema.Array(Schema.Union([OpenResponses.InputItem, OpenAIResponsesHostedToolItem])),
input: Schema.Array(
Schema.Union([OpenAIResponsesReasoningItem, OpenResponses.InputItem, OpenAIResponsesHostedToolItem]),
),
tools: optionalArray(OpenAIResponsesTools),
tool_choice: Schema.optional(OpenAIResponsesToolChoice),
}
Expand All @@ -90,6 +104,7 @@ const extension = {
id: ADAPTER,
name: NAME,
lowerHostedToolItem: (item: unknown) => (Schema.is(OpenAIResponsesHostedToolItem)(item) ? item : undefined),
lowerReasoningItem: (item: unknown) => (Schema.is(OpenAIResponsesReasoningItem)(item) ? item : undefined),
} satisfies OpenResponses.Extension

const nativeImageToolInput = (tool: ToolDefinition) => {
Expand Down Expand Up @@ -185,12 +200,6 @@ const HOSTED_TOOLS = {
} as const satisfies ResponsesHostedTools.Definitions

const step = (state: OpenResponses.ParserState, event: OpenResponses.Event) => {
if (event.type === "response.reasoning_text.delta")
return event.item_id !== undefined
? Effect.succeed(
OpenResponses.onReasoningDelta(state, event, OpenResponses.outputItemID(state, event) ?? event.item_id),
)
: ProviderShared.eventError(ADAPTER, `${event.type} is missing item_id`)
if (event.type === "response.output_item.done" && event.item && ResponsesHostedTools.isItem(event.item, HOSTED_TOOLS))
return ResponsesHostedTools.onDone(state, event.item, HOSTED_TOOLS)
return OpenResponses.step(state, event)
Expand Down
2 changes: 2 additions & 0 deletions packages/ai/test/provider/golden.recorded.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as OpenAICompatible from "../../src/providers/openai-compatible.js"
import * as OpenRouter from "../../src/providers/openrouter.js"
import * as XAI from "../../src/providers/xai.js"
import { describeRecordedGoldenScenarios } from "../recorded-golden.js"
import { matchOptionalEmptyReasoningContent } from "../recorded-test.js"

const openAI = OpenAI.configure({
apiKey: process.env.OPENAI_API_KEY ?? "fixture",
Expand Down Expand Up @@ -88,6 +89,7 @@ describeRecordedGoldenScenarios([
model: openAIResponses,
requires: ["OPENAI_API_KEY"],
tags: ["flagship"],
options: { match: matchOptionalEmptyReasoningContent },
scenarios: [
{ id: "text", temperature: false },
{ id: "reasoning", temperature: false },
Expand Down
18 changes: 14 additions & 4 deletions packages/ai/test/provider/open-responses-finals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,23 @@ describe("Open Responses completed item reasoning", () => {
expect(response.reasoning).toBe(fixture.text)
expect(response.events.filter(LLMEvent.is.reasoningEnd)).toHaveLength(1)
expect(response.message.content.find((part) => part.type === "reasoning")?.providerMetadata).toEqual({
"openai-compatible": { itemId: "rs_1", reasoningEncryptedContent: "encrypted" },
"openai-compatible": {
itemId: "rs_1",
reasoningEncryptedContent: "encrypted",
reasoningItem: {
type: "reasoning",
id: "rs_1",
summary: fixture.summary,
content: fixture.content,
encrypted_content: "encrypted",
},
},
})
}),
)
})

it.effect("replaces only the still-open summary without repeating earlier text", () =>
it.effect("replaces all streamed summary text with the completed item", () =>
Effect.gen(function* () {
const response = yield* generate(
{ type: "response.output_item.added", item: { type: "reasoning", id: "rs_1" } },
Expand All @@ -164,8 +174,8 @@ describe("Open Responses completed item reasoning", () => {
},
completed,
)
expect(response.reasoning).toBe("First final")
expect(response.events.filter(LLMEvent.is.reasoningEnd).map((event) => event.text)).toEqual([undefined, "final"])
expect(response.reasoning).toBe("First \n\nfinal")
expect(response.events.filter(LLMEvent.is.reasoningEnd).map((event) => event.text)).toEqual(["First \n\nfinal"])
}),
)
})
Expand Down
44 changes: 20 additions & 24 deletions packages/ai/test/provider/open-responses-lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function expectLifecycle(events: ReadonlyArray<LLMEvent>, completed: boolean) {
}

describe("Open Responses basic-item lifecycles", () => {
it.effect("closes implicit summary boundaries and ignores late events for completed reasoning", () =>
it.effect("keeps summary boundaries in one block and ignores late events for completed reasoning", () =>
Effect.gen(function* () {
const item = { type: "reasoning", id: "rs_1", encrypted_content: "encrypted-state" }
const events = yield* collect(
Expand Down Expand Up @@ -102,27 +102,16 @@ describe("Open Responses basic-item lifecycles", () => {
expect(events.filter((event) => event.type.startsWith("reasoning-"))).toEqual([
{
type: "reasoning-start",
id: "rs_1:0",
providerMetadata: { "openai-compatible": { itemId: "rs_1", reasoningEncryptedContent: null } },
},
{ type: "reasoning-delta", id: "rs_1:0", text: "First" },
{ type: "reasoning-end", id: "rs_1:0", providerMetadata: { "openai-compatible": { itemId: "rs_1" } } },
{
type: "reasoning-start",
id: "rs_1:1",
providerMetadata: { "openai-compatible": { itemId: "rs_1", reasoningEncryptedContent: null } },
},
{ type: "reasoning-delta", id: "rs_1:1", text: "Second" },
{ type: "reasoning-end", id: "rs_1:1", providerMetadata: { "openai-compatible": { itemId: "rs_1" } } },
{
type: "reasoning-start",
id: "rs_1:2",
providerMetadata: { "openai-compatible": { itemId: "rs_1", reasoningEncryptedContent: null } },
id: "rs_1",
providerMetadata: undefined,
},
{ type: "reasoning-delta", id: "rs_1:2", text: "Third" },
{ type: "reasoning-delta", id: "rs_1", text: "First" },
{ type: "reasoning-delta", id: "rs_1", text: "\n\nSecond" },
{ type: "reasoning-delta", id: "rs_1", text: "\n\nThird" },
{
type: "reasoning-end",
id: "rs_1:2",
id: "rs_1",
text: "First\n\nSecond\n\nThird",
providerMetadata: { "openai-compatible": { itemId: "rs_1", reasoningEncryptedContent: "encrypted-state" } },
},
])
Expand Down Expand Up @@ -152,13 +141,19 @@ describe("Open Responses basic-item lifecycles", () => {
{
type: "reasoning-start",
id: "rs_1",
providerMetadata: { "openai-compatible": { itemId: "rs_1", reasoningEncryptedContent: "encrypted-state" } },
providerMetadata: undefined,
},
{
type: "reasoning-end",
id: "rs_1",
text: "Not streamed",
providerMetadata: { "openai-compatible": { itemId: "rs_1", reasoningEncryptedContent: "encrypted-state" } },
providerMetadata: {
"openai-compatible": {
itemId: "rs_1",
reasoningEncryptedContent: "encrypted-state",
reasoningItem: item,
},
},
},
])
}),
Expand Down Expand Up @@ -340,7 +335,7 @@ describe("Open Responses basic-item lifecycles", () => {
providerExecuted: undefined,
providerMetadata: { "openai-compatible": { itemId: "fc_1" } },
},
{ type: "reasoning-end", id: "rs_1:0" },
{ type: "reasoning-end", id: "rs_1" },
])
}),
)
Expand Down Expand Up @@ -377,7 +372,8 @@ describe("Open Responses basic-item lifecycles", () => {
expect(events.filter(LLMEvent.is.reasoningEnd)).toEqual([
{
type: "reasoning-end",
id: ":0",
id: "",
text: "Thinking",
providerMetadata: { "openai-compatible": { itemId: "", reasoningEncryptedContent: "state" } },
},
])
Expand Down Expand Up @@ -452,7 +448,7 @@ describe("Open Responses basic-item lifecycles", () => {
)
expect(events.filter(LLMEvent.is.toolInputEnd)).toEqual([])
expect(events.filter(LLMEvent.is.toolCall)).toEqual([])
expect(events.filter(LLMEvent.is.reasoningEnd)).toEqual([{ type: "reasoning-end", id: "rs_1:0" }])
expect(events.filter(LLMEvent.is.reasoningEnd)).toEqual([{ type: "reasoning-end", id: "rs_1" }])
expect(events.filter(LLMEvent.is.finish)).toEqual([
{
type: "finish",
Expand Down
24 changes: 4 additions & 20 deletions packages/ai/test/provider/openai-compatible-responses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ describe("Open Responses-compatible route", () => {
})

routings.forEach((routing) => {
it.effect(`preserves reasoning summary boundaries without terminal reconciliation with ${routing.name}`, () =>
it.effect(`keeps one reasoning block without terminal reconciliation with ${routing.name}`, () =>
Effect.gen(function* () {
const address = { item_id: routing.item_id, output_index: routing.output_index }
const response = yield* LLMClient.generate(request).pipe(
Expand Down Expand Up @@ -437,26 +437,10 @@ describe("Open Responses-compatible route", () => {
expect(response.message.content).toEqual([
{
type: "reasoning",
text: "First.",
providerMetadata: { "openai-compatible": { itemId: routing.id } },
text: "First.\n\nSecond.",
},
{
type: "reasoning",
text: "Second.",
providerMetadata: {
"openai-compatible": { itemId: routing.id, reasoningEncryptedContent: null },
},
},
])
expect(response.events.filter(LLMEvent.is.reasoningEnd)).toEqual([
{
type: "reasoning-end",
id: `${routing.id}:0`,
text: undefined,
providerMetadata: { "openai-compatible": { itemId: routing.id } },
},
{ type: "reasoning-end", id: `${routing.id}:1` },
])
expect(response.events.filter(LLMEvent.is.reasoningEnd)).toEqual([{ type: "reasoning-end", id: routing.id }])
}),
)
})
Expand Down Expand Up @@ -696,7 +680,7 @@ describe("Open Responses-compatible route", () => {

expect(response.events.find((event) => event.type === "reasoning-end")).toEqual({
type: "reasoning-end",
id: "rs_raw:0",
id: "rs_raw",
})
}),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { describe, expect } from "bun:test"
import { Effect } from "effect"
import { LLM, LLMEvent, Message } from "../../src/index.js"
import { OpenAI } from "../../src/providers.js"
import { recordedTests } from "../recorded-test.js"
import { ProviderShared } from "../../src/protocols/shared.js"
import { matchOptionalEmptyReasoningContent, recordedTests } from "../recorded-test.js"

const openai = OpenAI.configure({
apiKey: process.env.OPENAI_API_KEY ?? "fixture",
Expand All @@ -13,6 +14,9 @@ const recorded = recordedTests({
provider: "openai",
protocol: "openai-responses",
requires: ["OPENAI_API_KEY"],
options: {
match: matchOptionalEmptyReasoningContent,
},
})

describe("OpenAI Responses image generation recorded", () => {
Expand All @@ -38,6 +42,12 @@ describe("OpenAI Responses image generation recorded", () => {
}),
)

const reasoning = response.message.content.find((part) => part.type === "reasoning")
const reasoningItem = reasoning?.providerMetadata?.openai?.reasoningItem
expect(ProviderShared.isRecord(reasoningItem) && Array.isArray(reasoningItem.summary)).toBe(true)
if (!ProviderShared.isRecord(reasoningItem) || !Array.isArray(reasoningItem.summary)) return
expect(reasoningItem.summary).toHaveLength(2)

const result = response.events.find(LLMEvent.is.toolResult)
expect(result).toBeDefined()
expect(result?.providerExecuted).toBe(true)
Expand Down
Loading
Loading