openai-responses.test.ts 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. import { describe, expect } from "bun:test"
  2. import { ConfigProvider, Effect, Layer, Stream } from "effect"
  3. import { Headers, HttpClientRequest } from "effect/unstable/http"
  4. import { LLM, LLMError, Message, Model, ToolCallPart, Usage } from "../../src"
  5. import { Auth, LLMClient, RequestExecutor, WebSocketExecutor } from "../../src/route"
  6. import * as Azure from "../../src/providers/azure"
  7. import * as OpenAI from "../../src/providers/openai"
  8. import * as OpenAIResponses from "../../src/protocols/openai-responses"
  9. import * as ProviderShared from "../../src/protocols/shared"
  10. import { continuationRequest, nativeOpenAIResponsesContinuation } from "../continuation-scenarios"
  11. import { it } from "../lib/effect"
  12. import { dynamicResponse, fixedResponse } from "../lib/http"
  13. import { sseEvents } from "../lib/sse"
  14. const model = OpenAIResponses.route
  15. .with({ endpoint: { baseURL: "https://api.openai.test/v1/" }, auth: Auth.bearer("test") })
  16. .model({ id: "gpt-4.1-mini" })
  17. const request = LLM.request({
  18. id: "req_1",
  19. model,
  20. system: "You are concise.",
  21. prompt: "Say hello.",
  22. generation: { maxTokens: 20, temperature: 0 },
  23. })
  24. const configEnv = (env: Record<string, string>) => Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env })))
  25. type OpenAIToolOutput = Extract<
  26. OpenAIResponses.OpenAIResponsesBody["input"][number],
  27. { readonly type: "function_call_output" }
  28. >
  29. const expectToolOutput = (body: OpenAIResponses.OpenAIResponsesBody): OpenAIToolOutput => {
  30. const output = body.input.find(
  31. (item): item is OpenAIToolOutput => "type" in item && item.type === "function_call_output",
  32. )
  33. expect(output).toBeDefined()
  34. return output!
  35. }
  36. describe("OpenAI Responses route", () => {
  37. it.effect("prepares OpenAI Responses target", () =>
  38. Effect.gen(function* () {
  39. const prepared = yield* LLMClient.prepare(request)
  40. expect(prepared.body).toEqual({
  41. model: "gpt-4.1-mini",
  42. input: [
  43. { role: "system", content: "You are concise." },
  44. { role: "user", content: [{ type: "input_text", text: "Say hello." }] },
  45. ],
  46. store: false,
  47. stream: true,
  48. max_output_tokens: 20,
  49. temperature: 0,
  50. })
  51. }),
  52. )
  53. it.effect("lowers semantic service tier options", () =>
  54. Effect.gen(function* () {
  55. const input = LLM.updateRequest(request, { providerOptions: { openai: { serviceTier: "priority" } } })
  56. expect(input.providerOptions).toEqual({ openai: { serviceTier: "priority" } })
  57. const prepared = yield* LLMClient.prepare(input)
  58. expect(prepared.body).toMatchObject({ service_tier: "priority" })
  59. expect(prepared.body).not.toHaveProperty("serviceTier")
  60. }),
  61. )
  62. it.effect("passes through custom OpenAI reasoning effort strings", () =>
  63. Effect.gen(function* () {
  64. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  65. LLM.updateRequest(request, { providerOptions: { openai: { reasoningEffort: "experimental" } } }),
  66. )
  67. expect(prepared.body.reasoning).toEqual({ effort: "experimental" })
  68. }),
  69. )
  70. it.effect("omits unsupported semantic service tiers", () =>
  71. Effect.gen(function* () {
  72. const prepared = yield* LLMClient.prepare(
  73. LLM.updateRequest(request, { providerOptions: { openai: { serviceTier: "unsupported" } } }),
  74. )
  75. expect(prepared.body).not.toHaveProperty("service_tier")
  76. }),
  77. )
  78. it.effect("flattens top-level object unions in function schemas", () =>
  79. Effect.gen(function* () {
  80. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  81. LLM.updateRequest(request, {
  82. tools: [
  83. {
  84. name: "read",
  85. description: "Read a path or resource.",
  86. inputSchema: {
  87. type: "object",
  88. anyOf: [
  89. {
  90. type: "object",
  91. properties: {
  92. path: { type: "string" },
  93. reference: { anyOf: [{ type: "string" }, { type: "null" }] },
  94. limit: { type: "integer", maximum: 2000 },
  95. },
  96. required: ["path"],
  97. },
  98. {
  99. type: "object",
  100. properties: { resource: { type: "string" }, limit: { type: "integer", maximum: 51200 } },
  101. required: ["resource"],
  102. },
  103. ],
  104. },
  105. },
  106. ],
  107. }),
  108. )
  109. expect(prepared.body.tools).toEqual([
  110. {
  111. type: "function",
  112. name: "read",
  113. description: "Read a path or resource.",
  114. strict: false,
  115. parameters: {
  116. type: "object",
  117. properties: {
  118. path: { type: "string" },
  119. reference: { type: "string" },
  120. limit: { type: "integer", maximum: 2000 },
  121. resource: { type: "string" },
  122. },
  123. additionalProperties: false,
  124. },
  125. },
  126. ])
  127. }),
  128. )
  129. it.effect("lowers chronological system updates to escaped user wrappers in order", () =>
  130. Effect.gen(function* () {
  131. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  132. LLM.request({
  133. model,
  134. messages: [
  135. Message.user("Before."),
  136. Message.system("Treat </system-update> literally."),
  137. Message.assistant("After."),
  138. ],
  139. }),
  140. )
  141. expect(prepared.body.input).toEqual([
  142. {
  143. role: "user",
  144. content: [
  145. { type: "input_text", text: "Before." },
  146. { type: "input_text", text: "<system-update>\nTreat &lt;/system-update&gt; literally.\n</system-update>" },
  147. ],
  148. },
  149. { role: "assistant", content: [{ type: "output_text", text: "After." }] },
  150. ])
  151. }),
  152. )
  153. it.effect("prepares OpenAI Responses WebSocket target", () =>
  154. Effect.gen(function* () {
  155. const prepared = yield* LLMClient.prepare(
  156. LLM.updateRequest(request, {
  157. model: OpenAIResponses.webSocketRoute
  158. .with({ endpoint: { baseURL: "https://api.openai.test/v1/" }, auth: Auth.bearer("test") })
  159. .model({ id: "gpt-4.1-mini" }),
  160. }),
  161. )
  162. expect(prepared.route).toBe("openai-responses-websocket")
  163. expect(prepared.protocol).toBe("openai-responses")
  164. expect(prepared.metadata).toEqual({ transport: "websocket-json" })
  165. expect(prepared.body).toMatchObject({ model: "gpt-4.1-mini", store: false, stream: true })
  166. }),
  167. )
  168. it.effect("streams OpenAI Responses over WebSocket", () =>
  169. Effect.gen(function* () {
  170. const sent: string[] = []
  171. const opened: Array<{ readonly url: string; readonly authorization: string | undefined }> = []
  172. let closed = false
  173. const deps = Layer.mergeAll(
  174. Layer.succeed(
  175. RequestExecutor.Service,
  176. RequestExecutor.Service.of({
  177. execute: () => Effect.die("unexpected HTTP request"),
  178. }),
  179. ),
  180. Layer.succeed(
  181. WebSocketExecutor.Service,
  182. WebSocketExecutor.Service.of({
  183. open: (input) =>
  184. Effect.succeed({
  185. sendText: (message) =>
  186. Effect.sync(() => {
  187. opened.push({ url: input.url, authorization: input.headers.authorization })
  188. sent.push(message)
  189. }),
  190. messages: Stream.fromArray([
  191. ProviderShared.encodeJson({ type: "response.output_text.delta", item_id: "msg_1", delta: "Hi" }),
  192. ProviderShared.encodeJson({ type: "response.completed", response: { id: "resp_ws" } }),
  193. ]),
  194. close: Effect.sync(() => {
  195. closed = true
  196. }),
  197. }),
  198. }),
  199. ),
  200. )
  201. const response = yield* LLMClient.generate(
  202. LLM.request({
  203. model: OpenAI.configure({ baseURL: "https://api.openai.test/v1/", apiKey: "test" }).responsesWebSocket(
  204. "gpt-4.1-mini",
  205. ),
  206. prompt: "Say hello.",
  207. }),
  208. ).pipe(Effect.provide(LLMClient.layer.pipe(Layer.provide(deps))))
  209. expect(response.text).toBe("Hi")
  210. expect(opened).toEqual([{ url: "wss://api.openai.test/v1/responses", authorization: "Bearer test" }])
  211. expect(closed).toBe(true)
  212. expect(sent).toHaveLength(1)
  213. expect(JSON.parse(sent[0])).toEqual({
  214. type: "response.create",
  215. model: "gpt-4.1-mini",
  216. input: [{ role: "user", content: [{ type: "input_text", text: "Say hello." }] }],
  217. store: false,
  218. })
  219. }),
  220. )
  221. it.effect("fails immediately when WebSocket is already closed", () =>
  222. Effect.gen(function* () {
  223. const error = yield* WebSocketExecutor.fromWebSocket(
  224. // oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion -- fromWebSocket reads readyState before touching WebSocket methods on this branch.
  225. { readyState: globalThis.WebSocket.CLOSED } as globalThis.WebSocket,
  226. { url: "wss://api.openai.test/v1/responses", headers: Headers.empty },
  227. ).pipe(Effect.flip)
  228. expect(error.message).toContain("closed before opening")
  229. }),
  230. )
  231. it.effect("adds native query params to the Responses URL", () =>
  232. Effect.gen(function* () {
  233. yield* LLMClient.generate(
  234. LLM.updateRequest(request, {
  235. model: Model.update(model, { route: model.route.with({ endpoint: { query: { "api-version": "v1" } } }) }),
  236. }),
  237. ).pipe(
  238. Effect.provide(
  239. dynamicResponse((input) =>
  240. Effect.gen(function* () {
  241. const web = yield* HttpClientRequest.toWeb(input.request).pipe(Effect.orDie)
  242. expect(web.url).toBe("https://api.openai.test/v1/responses?api-version=v1")
  243. return input.respond(sseEvents({ type: "response.completed", response: {} }), {
  244. headers: { "content-type": "text/event-stream" },
  245. })
  246. }),
  247. ),
  248. ),
  249. )
  250. }),
  251. )
  252. it.effect("uses Azure api-key header for static OpenAI Responses keys", () =>
  253. Effect.gen(function* () {
  254. yield* LLMClient.generate(
  255. LLM.updateRequest(request, {
  256. model: Azure.configure({
  257. baseURL: "https://opencode-test.openai.azure.com/openai/v1/",
  258. apiKey: "azure-key",
  259. headers: { authorization: "Bearer stale" },
  260. }).responses("gpt-4.1-mini"),
  261. }),
  262. ).pipe(
  263. Effect.provide(
  264. dynamicResponse((input) =>
  265. Effect.gen(function* () {
  266. const web = yield* HttpClientRequest.toWeb(input.request).pipe(Effect.orDie)
  267. expect(web.url).toBe("https://opencode-test.openai.azure.com/openai/v1/responses?api-version=v1")
  268. expect(web.headers.get("api-key")).toBe("azure-key")
  269. expect(web.headers.get("authorization")).toBeNull()
  270. return input.respond(sseEvents({ type: "response.completed", response: {} }), {
  271. headers: { "content-type": "text/event-stream" },
  272. })
  273. }),
  274. ),
  275. ),
  276. )
  277. }),
  278. )
  279. it.effect("loads OpenAI default auth from Effect Config", () =>
  280. LLMClient.generate(
  281. LLM.updateRequest(request, {
  282. model: OpenAI.configure({ baseURL: "https://api.openai.test/v1/" }).responses("gpt-4.1-mini"),
  283. }),
  284. ).pipe(
  285. configEnv({ OPENAI_API_KEY: "env-key" }),
  286. Effect.provide(
  287. dynamicResponse((input) =>
  288. Effect.gen(function* () {
  289. const web = yield* HttpClientRequest.toWeb(input.request).pipe(Effect.orDie)
  290. expect(web.headers.get("authorization")).toBe("Bearer env-key")
  291. return input.respond(sseEvents({ type: "response.completed", response: {} }), {
  292. headers: { "content-type": "text/event-stream" },
  293. })
  294. }),
  295. ),
  296. ),
  297. ),
  298. )
  299. it.effect("lets explicit auth override OpenAI default API key auth", () =>
  300. LLMClient.generate(
  301. LLM.updateRequest(request, {
  302. model: OpenAI.configure({
  303. baseURL: "https://api.openai.test/v1/",
  304. auth: Auth.bearer("oauth-token"),
  305. }).responses("gpt-4.1-mini"),
  306. }),
  307. ).pipe(
  308. Effect.provide(
  309. dynamicResponse((input) =>
  310. Effect.gen(function* () {
  311. const web = yield* HttpClientRequest.toWeb(input.request).pipe(Effect.orDie)
  312. expect(web.headers.get("authorization")).toBe("Bearer oauth-token")
  313. return input.respond(sseEvents({ type: "response.completed", response: {} }), {
  314. headers: { "content-type": "text/event-stream" },
  315. })
  316. }),
  317. ),
  318. ),
  319. ),
  320. )
  321. it.effect("prepares function call and function output input items", () =>
  322. Effect.gen(function* () {
  323. const prepared = yield* LLMClient.prepare(
  324. LLM.request({
  325. id: "req_tool_result",
  326. model,
  327. messages: [
  328. Message.user("What is the weather?"),
  329. Message.assistant([ToolCallPart.make({ id: "call_1", name: "lookup", input: { query: "weather" } })]),
  330. Message.tool({ id: "call_1", name: "lookup", result: { forecast: "sunny" } }),
  331. ],
  332. }),
  333. )
  334. expect(prepared.body).toEqual({
  335. model: "gpt-4.1-mini",
  336. input: [
  337. { role: "user", content: [{ type: "input_text", text: "What is the weather?" }] },
  338. { type: "function_call", call_id: "call_1", name: "lookup", arguments: '{"query":"weather"}' },
  339. { type: "function_call_output", call_id: "call_1", output: '{"forecast":"sunny"}' },
  340. ],
  341. store: false,
  342. stream: true,
  343. max_output_tokens: undefined,
  344. temperature: undefined,
  345. tool_choice: undefined,
  346. tools: undefined,
  347. top_p: undefined,
  348. })
  349. }),
  350. )
  351. it.effect("preserves structured tool errors for the model", () =>
  352. Effect.gen(function* () {
  353. const error = {
  354. error: { type: "unknown", message: "Tool execution interrupted" },
  355. content: [],
  356. structured: {},
  357. }
  358. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  359. LLM.request({
  360. model,
  361. messages: [
  362. Message.assistant([ToolCallPart.make({ id: "call_1", name: "bash", input: { command: "sleep 10" } })]),
  363. Message.tool({
  364. id: "call_1",
  365. name: "bash",
  366. resultType: "error",
  367. result: error,
  368. }),
  369. ],
  370. }),
  371. )
  372. expect(expectToolOutput(prepared.body).output).toBe(ProviderShared.encodeJson(error))
  373. }),
  374. )
  375. it.effect("keeps primitive tool errors as plain text", () =>
  376. Effect.gen(function* () {
  377. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  378. LLM.request({
  379. model,
  380. messages: [
  381. Message.assistant([ToolCallPart.make({ id: "call_1", name: "bash", input: {} })]),
  382. Message.tool({ id: "call_1", name: "bash", resultType: "error", result: 503 }),
  383. ],
  384. }),
  385. )
  386. expect(expectToolOutput(prepared.body).output).toBe("503")
  387. }),
  388. )
  389. it.effect("keeps non-JSON tool errors as plain text", () =>
  390. Effect.gen(function* () {
  391. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  392. LLM.request({
  393. model,
  394. messages: [
  395. Message.assistant([ToolCallPart.make({ id: "call_1", name: "bash", input: {} })]),
  396. Message.tool({ id: "call_1", name: "bash", resultType: "error", result: new Error("boom") }),
  397. ],
  398. }),
  399. )
  400. expect(expectToolOutput(prepared.body).output).toBe("Error: boom")
  401. }),
  402. )
  403. // Regression: screenshot/read tool results must stay structured so base64
  404. // image data is not JSON-stringified into `function_call_output.output`.
  405. it.effect("lowers image tool-result content as structured input_image items", () =>
  406. Effect.gen(function* () {
  407. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  408. LLM.request({
  409. id: "req_tool_result_image",
  410. model,
  411. messages: [
  412. Message.user("Show me the screenshot."),
  413. Message.assistant([ToolCallPart.make({ id: "call_1", name: "read", input: { filePath: "shot.png" } })]),
  414. Message.tool({
  415. id: "call_1",
  416. name: "read",
  417. resultType: "content",
  418. result: [
  419. { type: "text", text: "Image read successfully" },
  420. { type: "file", uri: "data:image/png;base64,AAECAw==", mime: "image/png" },
  421. ],
  422. }),
  423. ],
  424. }),
  425. )
  426. expect(expectToolOutput(prepared.body).output).toEqual([
  427. { type: "input_text", text: "Image read successfully" },
  428. { type: "input_image", image_url: "data:image/png;base64,AAECAw==" },
  429. ])
  430. }),
  431. )
  432. it.effect("lowers single-image tool-result content as structured input_image array", () =>
  433. Effect.gen(function* () {
  434. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  435. LLM.request({
  436. id: "req_tool_result_image_only",
  437. model,
  438. messages: [
  439. Message.assistant([ToolCallPart.make({ id: "call_1", name: "screenshot", input: {} })]),
  440. Message.tool({
  441. id: "call_1",
  442. name: "screenshot",
  443. resultType: "content",
  444. result: [{ type: "file", uri: "data:image/png;base64,AAECAw==", mime: "image/png" }],
  445. }),
  446. ],
  447. }),
  448. )
  449. expect(expectToolOutput(prepared.body).output).toEqual([
  450. { type: "input_image", image_url: "data:image/png;base64,AAECAw==" },
  451. ])
  452. }),
  453. )
  454. it.effect("rejects non-image media in tool-result content with a clear error", () =>
  455. Effect.gen(function* () {
  456. const error = yield* LLMClient.prepare(
  457. LLM.request({
  458. id: "req_tool_result_unsupported_media",
  459. model,
  460. messages: [
  461. Message.assistant([ToolCallPart.make({ id: "call_1", name: "fetch", input: {} })]),
  462. Message.tool({
  463. id: "call_1",
  464. name: "fetch",
  465. resultType: "content",
  466. result: [{ type: "file", uri: "data:audio/mpeg;base64,AAECAw==", mime: "audio/mpeg" }],
  467. }),
  468. ],
  469. }),
  470. ).pipe(Effect.flip)
  471. expect(error.message).toContain("OpenAI Responses")
  472. expect(error.message).toContain("audio/mpeg")
  473. }),
  474. )
  475. it.effect("prepares the composed native continuation request", () =>
  476. Effect.gen(function* () {
  477. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  478. continuationRequest({
  479. id: "req_native_continuation_openai",
  480. model,
  481. features: nativeOpenAIResponsesContinuation,
  482. }),
  483. )
  484. expect(prepared.body).toMatchObject({
  485. input: [
  486. { role: "system", content: "You are concise. Continue from the provided history." },
  487. {
  488. role: "user",
  489. content: [
  490. { type: "input_text", text: "What is shown here?" },
  491. { type: "input_image", image_url: "data:image/png;base64,AAECAw==" },
  492. ],
  493. },
  494. {
  495. type: "reasoning",
  496. encrypted_content: "encrypted-continuation-state",
  497. summary: [{ type: "summary_text", text: "I inspected the previous turn." }],
  498. },
  499. { role: "assistant", content: [{ type: "output_text", text: "It shows a small test image." }] },
  500. { role: "user", content: [{ type: "input_text", text: "Check the weather in Paris before continuing." }] },
  501. { type: "function_call", call_id: "call_weather_1", name: "get_weather", arguments: '{"city":"Paris"}' },
  502. { type: "function_call_output", call_id: "call_weather_1", output: '{"temperature":22}' },
  503. { role: "assistant", content: [{ type: "output_text", text: "Paris is 22 degrees." }] },
  504. {
  505. role: "user",
  506. content: [{ type: "input_text", text: "Continue from this conversation in one short sentence." }],
  507. },
  508. ],
  509. include: ["reasoning.encrypted_content"],
  510. store: false,
  511. })
  512. expect(prepared.body.tools).toEqual([expect.objectContaining({ type: "function", name: "get_weather" })])
  513. }),
  514. )
  515. it.effect("maps OpenAI provider options to Responses options", () =>
  516. Effect.gen(function* () {
  517. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  518. LLM.request({
  519. model: OpenAI.configure({ baseURL: "https://api.openai.test/v1/", apiKey: "test" }).model("gpt-5.2"),
  520. prompt: "think",
  521. providerOptions: {
  522. openai: {
  523. promptCacheKey: "session_123",
  524. reasoningEffort: "high",
  525. reasoningSummary: "auto",
  526. include: ["reasoning.encrypted_content"],
  527. },
  528. },
  529. }),
  530. )
  531. expect(prepared.body.store).toBe(false)
  532. expect(prepared.body.prompt_cache_key).toBe("session_123")
  533. expect(prepared.body.include).toEqual(["reasoning.encrypted_content"])
  534. expect(prepared.body.reasoning).toEqual({ effort: "high", summary: "auto" })
  535. expect(prepared.body.text).toEqual({ verbosity: "low" })
  536. }),
  537. )
  538. it.effect("accepts the full ResponseIncludable union", () =>
  539. Effect.gen(function* () {
  540. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  541. LLM.request({
  542. model,
  543. prompt: "hi",
  544. providerOptions: {
  545. openai: {
  546. include: ["reasoning.encrypted_content", "code_interpreter_call.outputs", "web_search_call.results"],
  547. },
  548. },
  549. }),
  550. )
  551. expect(prepared.body.include).toEqual([
  552. "reasoning.encrypted_content",
  553. "code_interpreter_call.outputs",
  554. "web_search_call.results",
  555. ])
  556. }),
  557. )
  558. it.effect("filters unknown includable values out of the include array", () =>
  559. Effect.gen(function* () {
  560. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  561. LLM.request({
  562. model,
  563. prompt: "hi",
  564. // The user passed one invalid entry alongside a valid one. Keep the
  565. // valid one so the request still succeeds rather than failing on a
  566. // typo from upstream config.
  567. providerOptions: { openai: { include: ["reasoning.encrypted_content", "bogus.thing"] } },
  568. }),
  569. )
  570. expect(prepared.body.include).toEqual(["reasoning.encrypted_content"])
  571. }),
  572. )
  573. it.effect("treats an explicit empty include as no include at all", () =>
  574. Effect.gen(function* () {
  575. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  576. LLM.request({ model, prompt: "hi", providerOptions: { openai: { include: [] } } }),
  577. )
  578. expect(prepared.body.include).toBeUndefined()
  579. }),
  580. )
  581. it.effect("treats an all-invalid include as no include at all", () =>
  582. Effect.gen(function* () {
  583. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  584. LLM.request({ model, prompt: "hi", providerOptions: { openai: { include: ["bogus.thing"] } } }),
  585. )
  586. expect(prepared.body.include).toBeUndefined()
  587. }),
  588. )
  589. it.effect("omits include when no include is set", () =>
  590. Effect.gen(function* () {
  591. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  592. LLM.request({ model, prompt: "hi", providerOptions: { openai: { store: false } } }),
  593. )
  594. expect(prepared.body.include).toBeUndefined()
  595. }),
  596. )
  597. it.effect("requests encrypted reasoning by default for GPT-5 reasoning models", () =>
  598. Effect.gen(function* () {
  599. // The native OpenAI facade configures GPT-5 stateless (store: false) with
  600. // reasoningSummary: "auto" by default. Without `include`, a follow-up
  601. // turn cannot replay reasoning state, so the facade also opts into
  602. // `reasoning.encrypted_content` automatically.
  603. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  604. LLM.request({
  605. model: OpenAI.configure({ baseURL: "https://api.openai.test/v1/", apiKey: "test" }).responses("gpt-5.2"),
  606. prompt: "hi",
  607. }),
  608. )
  609. expect(prepared.body.store).toBe(false)
  610. expect(prepared.body.include).toEqual(["reasoning.encrypted_content"])
  611. expect(prepared.body.reasoning).toEqual({ effort: "medium", summary: "auto" })
  612. }),
  613. )
  614. it.effect("lets callers opt out of the GPT-5 default include", () =>
  615. Effect.gen(function* () {
  616. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  617. LLM.request({
  618. model: OpenAI.configure({ baseURL: "https://api.openai.test/v1/", apiKey: "test" }).responses("gpt-5.2"),
  619. prompt: "hi",
  620. providerOptions: { openai: { include: [] } },
  621. }),
  622. )
  623. expect(prepared.body.include).toBeUndefined()
  624. }),
  625. )
  626. it.effect("request OpenAI provider options override route defaults", () =>
  627. Effect.gen(function* () {
  628. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  629. LLM.request({
  630. model: OpenAI.configure({
  631. baseURL: "https://api.openai.test/v1/",
  632. apiKey: "test",
  633. providerOptions: { openai: { promptCacheKey: "model_cache" } },
  634. }).model("gpt-4.1-mini"),
  635. prompt: "no cache",
  636. providerOptions: { openai: { promptCacheKey: "request_cache" } },
  637. }),
  638. )
  639. expect(prepared.body.prompt_cache_key).toBe("request_cache")
  640. }),
  641. )
  642. it.effect("parses text and usage stream fixtures", () =>
  643. Effect.gen(function* () {
  644. const body = sseEvents(
  645. { type: "response.output_text.delta", item_id: "msg_1", delta: "Hello" },
  646. { type: "response.output_text.delta", item_id: "msg_1", delta: "!" },
  647. {
  648. type: "response.completed",
  649. response: {
  650. id: "resp_1",
  651. service_tier: "default",
  652. usage: {
  653. input_tokens: 5,
  654. output_tokens: 2,
  655. total_tokens: 7,
  656. input_tokens_details: { cached_tokens: 1 },
  657. output_tokens_details: { reasoning_tokens: 0 },
  658. },
  659. },
  660. },
  661. )
  662. const response = yield* LLMClient.generate(request).pipe(Effect.provide(fixedResponse(body)))
  663. const usage = new Usage({
  664. inputTokens: 5,
  665. outputTokens: 2,
  666. nonCachedInputTokens: 4,
  667. cacheReadInputTokens: 1,
  668. reasoningTokens: 0,
  669. totalTokens: 7,
  670. providerMetadata: {
  671. openai: {
  672. input_tokens: 5,
  673. output_tokens: 2,
  674. total_tokens: 7,
  675. input_tokens_details: { cached_tokens: 1 },
  676. output_tokens_details: { reasoning_tokens: 0 },
  677. },
  678. },
  679. })
  680. expect(response.text).toBe("Hello!")
  681. expect(response.events).toEqual([
  682. { type: "step-start", index: 0 },
  683. { type: "text-start", id: "msg_1" },
  684. { type: "text-delta", id: "msg_1", text: "Hello" },
  685. { type: "text-delta", id: "msg_1", text: "!" },
  686. { type: "text-end", id: "msg_1" },
  687. {
  688. type: "step-finish",
  689. index: 0,
  690. reason: "stop",
  691. providerMetadata: { openai: { responseId: "resp_1", serviceTier: "default" } },
  692. usage,
  693. },
  694. {
  695. type: "finish",
  696. reason: "stop",
  697. providerMetadata: { openai: { responseId: "resp_1", serviceTier: "default" } },
  698. usage,
  699. },
  700. ])
  701. }),
  702. )
  703. // OpenAI's documented stream orders output text within one message item; no
  704. // provider-valid same-kind overlap is evidenced, so done boundaries close it.
  705. it.effect("closes sequential output messages before starting the next", () =>
  706. Effect.gen(function* () {
  707. const response = yield* LLMClient.generate(request).pipe(
  708. Effect.provide(
  709. fixedResponse(
  710. sseEvents(
  711. { type: "response.output_text.delta", item_id: "msg_1", delta: "First" },
  712. { type: "response.output_text.done", item_id: "msg_1" },
  713. { type: "response.output_text.delta", item_id: "msg_2", delta: "Second" },
  714. { type: "response.output_item.done", item: { type: "message", id: "msg_2" } },
  715. { type: "response.completed", response: { id: "resp_1" } },
  716. ),
  717. ),
  718. ),
  719. )
  720. expect(response.events.filter((event) => event.type.startsWith("text-"))).toEqual([
  721. { type: "text-start", id: "msg_1" },
  722. { type: "text-delta", id: "msg_1", text: "First" },
  723. { type: "text-end", id: "msg_1" },
  724. { type: "text-start", id: "msg_2" },
  725. { type: "text-delta", id: "msg_2", text: "Second" },
  726. { type: "text-end", id: "msg_2" },
  727. ])
  728. }),
  729. )
  730. it.effect("parses reasoning summary stream fixtures", () =>
  731. Effect.gen(function* () {
  732. const body = sseEvents(
  733. { type: "response.reasoning_summary_text.delta", item_id: "rs_1", delta: "thinking" },
  734. { type: "response.output_text.delta", item_id: "msg_1", delta: "Hello" },
  735. { type: "response.reasoning_summary_text.done", item_id: "rs_1" },
  736. { type: "response.completed", response: { id: "resp_1" } },
  737. )
  738. const response = yield* LLMClient.generate(request).pipe(Effect.provide(fixedResponse(body)))
  739. expect(response.reasoning).toBe("thinking")
  740. expect(response.text).toBe("Hello")
  741. expect(response.events).toMatchObject([
  742. { type: "step-start", index: 0 },
  743. { type: "reasoning-start", id: "rs_1" },
  744. { type: "reasoning-delta", id: "rs_1", text: "thinking" },
  745. { type: "text-start", id: "msg_1" },
  746. { type: "text-delta", id: "msg_1", text: "Hello" },
  747. { type: "reasoning-end", id: "rs_1" },
  748. { type: "text-end", id: "msg_1" },
  749. { type: "step-finish", index: 0, reason: "stop" },
  750. { type: "finish", reason: "stop" },
  751. ])
  752. expect(response.events.filter((event) => event.type === "finish")).toHaveLength(1)
  753. expect(response.message.content).toEqual([
  754. { type: "reasoning", text: "thinking" },
  755. { type: "text", text: "Hello" },
  756. ])
  757. }),
  758. )
  759. it.effect("preserves encrypted reasoning metadata for continuation", () =>
  760. Effect.gen(function* () {
  761. const response = yield* LLMClient.generate(request).pipe(
  762. Effect.provide(
  763. fixedResponse(
  764. sseEvents(
  765. { type: "response.reasoning_summary_text.delta", item_id: "rs_1", delta: "thinking" },
  766. {
  767. type: "response.output_item.done",
  768. item: {
  769. type: "reasoning",
  770. id: "rs_1",
  771. encrypted_content: "encrypted-state",
  772. summary: [{ type: "summary_text", text: "thinking" }],
  773. },
  774. },
  775. { type: "response.completed", response: { id: "resp_1" } },
  776. ),
  777. ),
  778. ),
  779. )
  780. expect(response.events).toContainEqual(
  781. expect.objectContaining({
  782. type: "reasoning-end",
  783. id: "rs_1",
  784. providerMetadata: { openai: { itemId: "rs_1", reasoningEncryptedContent: "encrypted-state" } },
  785. }),
  786. )
  787. }),
  788. )
  789. it.effect("streams each reasoning summary part as a separate block", () =>
  790. Effect.gen(function* () {
  791. const response = yield* LLMClient.generate(
  792. LLM.updateRequest(request, { providerOptions: { openai: { store: false } } }),
  793. ).pipe(
  794. Effect.provide(
  795. fixedResponse(
  796. sseEvents(
  797. {
  798. type: "response.output_item.added",
  799. item: { type: "reasoning", id: "rs_1", encrypted_content: null },
  800. },
  801. { type: "response.reasoning_summary_part.added", item_id: "rs_1", summary_index: 0 },
  802. { type: "response.reasoning_summary_text.delta", item_id: "rs_1", summary_index: 0, delta: "First" },
  803. { type: "response.reasoning_summary_part.done", item_id: "rs_1", summary_index: 0 },
  804. { type: "response.reasoning_summary_part.added", item_id: "rs_1", summary_index: 1 },
  805. { type: "response.reasoning_summary_text.delta", item_id: "rs_1", summary_index: 1, delta: "Second" },
  806. { type: "response.reasoning_summary_part.done", item_id: "rs_1", summary_index: 1 },
  807. {
  808. type: "response.output_item.done",
  809. item: { type: "reasoning", id: "rs_1", encrypted_content: "encrypted-state" },
  810. },
  811. { type: "response.completed", response: { id: "resp_1" } },
  812. ),
  813. ),
  814. ),
  815. )
  816. expect(response.reasoning).toBe("FirstSecond")
  817. expect(response.events).toMatchObject([
  818. { type: "step-start", index: 0 },
  819. {
  820. type: "reasoning-start",
  821. id: "rs_1:0",
  822. providerMetadata: { openai: { itemId: "rs_1", reasoningEncryptedContent: null } },
  823. },
  824. { type: "reasoning-delta", id: "rs_1:0", text: "First" },
  825. { type: "reasoning-end", id: "rs_1:0", providerMetadata: { openai: { itemId: "rs_1" } } },
  826. {
  827. type: "reasoning-start",
  828. id: "rs_1:1",
  829. providerMetadata: { openai: { itemId: "rs_1", reasoningEncryptedContent: null } },
  830. },
  831. { type: "reasoning-delta", id: "rs_1:1", text: "Second" },
  832. {
  833. type: "reasoning-end",
  834. id: "rs_1:1",
  835. providerMetadata: { openai: { itemId: "rs_1", reasoningEncryptedContent: "encrypted-state" } },
  836. },
  837. { type: "step-finish", index: 0, reason: "stop" },
  838. { type: "finish", reason: "stop" },
  839. ])
  840. }),
  841. )
  842. it.effect("closes reasoning summary parts when storage is not disabled", () =>
  843. Effect.gen(function* () {
  844. const response = yield* LLMClient.generate(
  845. LLM.updateRequest(request, { providerOptions: { openai: { store: true } } }),
  846. ).pipe(
  847. Effect.provide(
  848. fixedResponse(
  849. sseEvents(
  850. {
  851. type: "response.output_item.added",
  852. item: { type: "reasoning", id: "rs_1", encrypted_content: null },
  853. },
  854. { type: "response.reasoning_summary_part.added", item_id: "rs_1", summary_index: 0 },
  855. { type: "response.reasoning_summary_text.delta", item_id: "rs_1", summary_index: 0, delta: "First" },
  856. { type: "response.reasoning_summary_part.done", item_id: "rs_1", summary_index: 0 },
  857. { type: "response.reasoning_summary_part.added", item_id: "rs_1", summary_index: 1 },
  858. { type: "response.reasoning_summary_text.delta", item_id: "rs_1", summary_index: 1, delta: "Second" },
  859. { type: "response.reasoning_summary_part.done", item_id: "rs_1", summary_index: 1 },
  860. {
  861. type: "response.output_item.done",
  862. item: { type: "reasoning", id: "rs_1", encrypted_content: null },
  863. },
  864. { type: "response.completed", response: { id: "resp_1" } },
  865. ),
  866. ),
  867. ),
  868. )
  869. expect(response.events.filter((event) => event.type === "reasoning-end")).toEqual([
  870. { type: "reasoning-end", id: "rs_1:0", providerMetadata: { openai: { itemId: "rs_1" } } },
  871. { type: "reasoning-end", id: "rs_1:1", providerMetadata: { openai: { itemId: "rs_1" } } },
  872. ])
  873. }),
  874. )
  875. it.effect("continues a stateless reasoning conversation", () =>
  876. Effect.gen(function* () {
  877. const response = yield* LLMClient.generate(
  878. LLM.request({
  879. id: "req_reasoning_continue",
  880. model,
  881. messages: [
  882. Message.user("What changed?"),
  883. Message.assistant([
  884. {
  885. type: "reasoning",
  886. text: "Checked the previous diff.",
  887. providerMetadata: {
  888. openai: {
  889. itemId: "rs_1",
  890. reasoningEncryptedContent: "encrypted-state",
  891. },
  892. },
  893. },
  894. { type: "text", text: "The parser changed." },
  895. ]),
  896. Message.user("Summarize it."),
  897. ],
  898. providerOptions: { openai: { store: false } },
  899. }),
  900. ).pipe(
  901. Effect.provide(
  902. dynamicResponse((input) =>
  903. Effect.gen(function* () {
  904. const web = yield* HttpClientRequest.toWeb(input.request).pipe(Effect.orDie)
  905. const body = yield* Effect.promise(() => web.json())
  906. expect(body).toMatchObject({
  907. input: [
  908. { role: "user", content: [{ type: "input_text", text: "What changed?" }] },
  909. {
  910. type: "reasoning",
  911. encrypted_content: "encrypted-state",
  912. summary: [{ type: "summary_text", text: "Checked the previous diff." }],
  913. },
  914. { role: "assistant", content: [{ type: "output_text", text: "The parser changed." }] },
  915. { role: "user", content: [{ type: "input_text", text: "Summarize it." }] },
  916. ],
  917. })
  918. expect(body.input[1]).not.toHaveProperty("id")
  919. return input.respond(
  920. sseEvents(
  921. { type: "response.output_text.delta", item_id: "msg_1", delta: "Parser now round-trips reasoning." },
  922. { type: "response.completed", response: { id: "resp_1" } },
  923. ),
  924. { headers: { "content-type": "text/event-stream" } },
  925. )
  926. }),
  927. ),
  928. ),
  929. )
  930. expect(response.text).toBe("Parser now round-trips reasoning.")
  931. }),
  932. )
  933. it.effect("preserves assistant content order around reasoning items", () =>
  934. Effect.gen(function* () {
  935. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  936. LLM.request({
  937. id: "req_reasoning_order",
  938. model,
  939. messages: [
  940. Message.assistant([
  941. { type: "text", text: "Before." },
  942. {
  943. type: "reasoning",
  944. text: "Checked order.",
  945. providerMetadata: {
  946. openai: {
  947. itemId: "rs_1",
  948. reasoningEncryptedContent: "encrypted-state",
  949. },
  950. },
  951. },
  952. { type: "text", text: "After." },
  953. ]),
  954. ],
  955. providerOptions: { openai: { store: false } },
  956. }),
  957. )
  958. expect(prepared.body.input).toEqual([
  959. { role: "assistant", content: [{ type: "output_text", text: "Before." }] },
  960. {
  961. type: "reasoning",
  962. encrypted_content: "encrypted-state",
  963. summary: [{ type: "summary_text", text: "Checked order." }],
  964. },
  965. { role: "assistant", content: [{ type: "output_text", text: "After." }] },
  966. ])
  967. }),
  968. )
  969. it.effect("references stored reasoning items by id", () =>
  970. Effect.gen(function* () {
  971. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  972. LLM.request({
  973. model,
  974. messages: [
  975. Message.assistant([
  976. {
  977. type: "reasoning",
  978. text: "Checked the previous diff.",
  979. providerMetadata: { openai: { itemId: "rs_1" } },
  980. },
  981. ]),
  982. ],
  983. providerOptions: { openai: { store: true } },
  984. }),
  985. )
  986. expect(prepared.body.input).toEqual([{ type: "item_reference", id: "rs_1" }])
  987. }),
  988. )
  989. it.effect("references stored provider-executed hosted tool results by id", () =>
  990. Effect.gen(function* () {
  991. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  992. LLM.request({
  993. model,
  994. messages: [
  995. Message.assistant([
  996. ToolCallPart.make({
  997. id: "ws_1",
  998. name: "web_search",
  999. input: { query: "effect 4" },
  1000. providerExecuted: true,
  1001. providerMetadata: { openai: { itemId: "ws_1" } },
  1002. }),
  1003. {
  1004. type: "tool-result",
  1005. id: "ws_1",
  1006. name: "web_search",
  1007. result: { type: "json", value: { type: "web_search_call", id: "ws_1", status: "completed" } },
  1008. providerExecuted: true,
  1009. providerMetadata: { openai: { itemId: "ws_1" } },
  1010. },
  1011. ]),
  1012. Message.user("Continue."),
  1013. ],
  1014. providerOptions: { openai: { store: true } },
  1015. }),
  1016. )
  1017. expect(prepared.body.input).toEqual([
  1018. { type: "item_reference", id: "ws_1" },
  1019. { role: "user", content: [{ type: "input_text", text: "Continue." }] },
  1020. ])
  1021. }),
  1022. )
  1023. it.effect("joins streamed summary blocks into one continuation reasoning item", () =>
  1024. Effect.gen(function* () {
  1025. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  1026. LLM.request({
  1027. id: "req_multi_summary_continuation",
  1028. model,
  1029. messages: [
  1030. Message.assistant([
  1031. {
  1032. type: "reasoning",
  1033. text: "First",
  1034. providerMetadata: { openai: { itemId: "rs_1" } },
  1035. },
  1036. {
  1037. type: "reasoning",
  1038. text: "Second",
  1039. providerMetadata: { openai: { itemId: "rs_1", reasoningEncryptedContent: "encrypted-state" } },
  1040. },
  1041. ]),
  1042. ],
  1043. providerOptions: { openai: { store: false } },
  1044. }),
  1045. )
  1046. expect(prepared.body.input).toEqual([
  1047. {
  1048. type: "reasoning",
  1049. encrypted_content: "encrypted-state",
  1050. summary: [
  1051. { type: "summary_text", text: "First" },
  1052. { type: "summary_text", text: "Second" },
  1053. ],
  1054. },
  1055. ])
  1056. }),
  1057. )
  1058. it.effect("skips non-persisted reasoning ids without encrypted state", () =>
  1059. Effect.gen(function* () {
  1060. const prepared = yield* LLMClient.prepare(
  1061. LLM.request({
  1062. id: "req_reasoning_without_encrypted_state",
  1063. model,
  1064. messages: [
  1065. Message.user("What changed?"),
  1066. Message.assistant([
  1067. {
  1068. type: "reasoning",
  1069. text: "Checked the previous diff.",
  1070. providerMetadata: {
  1071. openai: {
  1072. itemId: "rs_1",
  1073. reasoningEncryptedContent: null,
  1074. },
  1075. },
  1076. },
  1077. { type: "text", text: "The parser changed." },
  1078. ]),
  1079. Message.user("Summarize it."),
  1080. ],
  1081. providerOptions: { openai: { store: false } },
  1082. }),
  1083. )
  1084. expect(prepared.body).toMatchObject({
  1085. input: [
  1086. { role: "user", content: [{ type: "input_text", text: "What changed?" }] },
  1087. { role: "assistant", content: [{ type: "output_text", text: "The parser changed." }] },
  1088. { role: "user", content: [{ type: "input_text", text: "Summarize it." }] },
  1089. ],
  1090. store: false,
  1091. })
  1092. }),
  1093. )
  1094. it.effect("assembles streamed function call input", () =>
  1095. Effect.gen(function* () {
  1096. const body = sseEvents(
  1097. {
  1098. type: "response.output_item.added",
  1099. item: { type: "function_call", id: "item_1", call_id: "call_1", name: "lookup", arguments: "" },
  1100. },
  1101. { type: "response.function_call_arguments.delta", item_id: "item_1", delta: '{"query"' },
  1102. { type: "response.function_call_arguments.delta", item_id: "item_1", delta: ':"weather"}' },
  1103. {
  1104. type: "response.output_item.done",
  1105. item: {
  1106. type: "function_call",
  1107. id: "item_1",
  1108. call_id: "call_1",
  1109. name: "lookup",
  1110. arguments: '{"query":"weather"}',
  1111. },
  1112. },
  1113. { type: "response.completed", response: { usage: { input_tokens: 5, output_tokens: 1 } } },
  1114. )
  1115. const response = yield* LLMClient.generate(
  1116. LLM.updateRequest(request, {
  1117. tools: [{ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } }],
  1118. }),
  1119. ).pipe(Effect.provide(fixedResponse(body)))
  1120. const usage = new Usage({
  1121. inputTokens: 5,
  1122. outputTokens: 1,
  1123. nonCachedInputTokens: 5,
  1124. cacheReadInputTokens: undefined,
  1125. reasoningTokens: undefined,
  1126. totalTokens: 6,
  1127. providerMetadata: { openai: { input_tokens: 5, output_tokens: 1 } },
  1128. })
  1129. expect(response.events).toEqual([
  1130. { type: "step-start", index: 0 },
  1131. {
  1132. type: "tool-input-start",
  1133. id: "call_1",
  1134. name: "lookup",
  1135. providerMetadata: { openai: { itemId: "item_1" } },
  1136. },
  1137. {
  1138. type: "tool-input-delta",
  1139. id: "call_1",
  1140. name: "lookup",
  1141. text: '{"query"',
  1142. },
  1143. {
  1144. type: "tool-input-delta",
  1145. id: "call_1",
  1146. name: "lookup",
  1147. text: ':"weather"}',
  1148. },
  1149. {
  1150. type: "tool-input-end",
  1151. id: "call_1",
  1152. name: "lookup",
  1153. providerMetadata: { openai: { itemId: "item_1" } },
  1154. },
  1155. {
  1156. type: "tool-call",
  1157. id: "call_1",
  1158. name: "lookup",
  1159. input: { query: "weather" },
  1160. providerExecuted: undefined,
  1161. providerMetadata: { openai: { itemId: "item_1" } },
  1162. },
  1163. { type: "step-finish", index: 0, reason: "tool-calls", usage, providerMetadata: undefined },
  1164. {
  1165. type: "finish",
  1166. reason: "tool-calls",
  1167. providerMetadata: undefined,
  1168. usage,
  1169. },
  1170. ])
  1171. }),
  1172. )
  1173. it.effect("decodes web_search_call as provider-executed tool-call + tool-result", () =>
  1174. Effect.gen(function* () {
  1175. const item = {
  1176. type: "web_search_call",
  1177. id: "ws_1",
  1178. status: "completed",
  1179. action: { type: "search", query: "effect 4" },
  1180. }
  1181. const body = sseEvents(
  1182. { type: "response.output_item.added", item },
  1183. { type: "response.output_item.done", item },
  1184. { type: "response.completed", response: { usage: { input_tokens: 5, output_tokens: 1 } } },
  1185. )
  1186. const response = yield* LLMClient.generate(request).pipe(Effect.provide(fixedResponse(body)))
  1187. const callsAndResults = response.events.filter(
  1188. (event) => event.type === "tool-call" || event.type === "tool-result",
  1189. )
  1190. expect(callsAndResults).toEqual([
  1191. {
  1192. type: "tool-call",
  1193. id: "ws_1",
  1194. name: "web_search",
  1195. input: { type: "search", query: "effect 4" },
  1196. providerExecuted: true,
  1197. providerMetadata: { openai: { itemId: "ws_1" } },
  1198. },
  1199. {
  1200. type: "tool-result",
  1201. id: "ws_1",
  1202. name: "web_search",
  1203. result: { type: "json", value: item },
  1204. providerExecuted: true,
  1205. providerMetadata: { openai: { itemId: "ws_1" } },
  1206. },
  1207. ])
  1208. }),
  1209. )
  1210. it.effect("decodes code_interpreter_call as provider-executed events with code input", () =>
  1211. Effect.gen(function* () {
  1212. const item = {
  1213. type: "code_interpreter_call",
  1214. id: "ci_1",
  1215. status: "completed",
  1216. code: "print(1+1)",
  1217. container_id: "cnt_xyz",
  1218. outputs: [{ type: "logs", logs: "2\n" }],
  1219. }
  1220. const body = sseEvents(
  1221. { type: "response.output_item.done", item },
  1222. { type: "response.completed", response: { usage: { input_tokens: 5, output_tokens: 1 } } },
  1223. )
  1224. const response = yield* LLMClient.generate(request).pipe(Effect.provide(fixedResponse(body)))
  1225. const toolCall = response.events.find((event) => event.type === "tool-call")
  1226. expect(toolCall).toEqual({
  1227. type: "tool-call",
  1228. id: "ci_1",
  1229. name: "code_interpreter",
  1230. input: { code: "print(1+1)", container_id: "cnt_xyz" },
  1231. providerExecuted: true,
  1232. providerMetadata: { openai: { itemId: "ci_1" } },
  1233. })
  1234. const toolResult = response.events.find((event) => event.type === "tool-result")
  1235. expect(toolResult).toEqual({
  1236. type: "tool-result",
  1237. id: "ci_1",
  1238. name: "code_interpreter",
  1239. result: { type: "json", value: item },
  1240. providerExecuted: true,
  1241. providerMetadata: { openai: { itemId: "ci_1" } },
  1242. })
  1243. }),
  1244. )
  1245. it.effect("lowers user image content", () =>
  1246. Effect.gen(function* () {
  1247. const prepared = yield* LLMClient.prepare<OpenAIResponses.OpenAIResponsesBody>(
  1248. LLM.request({
  1249. id: "req_media",
  1250. model,
  1251. messages: [Message.user({ type: "media", mediaType: "image/png", data: "AAECAw==" })],
  1252. }),
  1253. )
  1254. expect(prepared.body.input).toEqual([
  1255. {
  1256. role: "user",
  1257. content: [{ type: "input_image", image_url: "data:image/png;base64,AAECAw==" }],
  1258. },
  1259. ])
  1260. }),
  1261. )
  1262. it.effect("rejects unsupported user media content", () =>
  1263. Effect.gen(function* () {
  1264. const error = yield* LLMClient.prepare(
  1265. LLM.request({
  1266. id: "req_media",
  1267. model,
  1268. messages: [Message.user({ type: "media", mediaType: "application/pdf", data: "AAECAw==" })],
  1269. }),
  1270. ).pipe(Effect.flip)
  1271. expect(error.message).toContain("OpenAI Responses does not support media type application/pdf")
  1272. }),
  1273. )
  1274. it.effect("emits provider-error events for mid-stream provider errors", () =>
  1275. Effect.gen(function* () {
  1276. const response = yield* LLMClient.generate(request).pipe(
  1277. Effect.provide(fixedResponse(sseEvents({ type: "error", code: "rate_limit_exceeded", message: "Slow down" }))),
  1278. )
  1279. // Prefix the code so consumers see the failure mode, not just the
  1280. // sometimes-generic provider message. The bare message alone meant
  1281. // production errors like rate limits were indistinguishable from
  1282. // unrelated stream failures.
  1283. expect(response.events).toEqual([{ type: "provider-error", message: "rate_limit_exceeded: Slow down" }])
  1284. }),
  1285. )
  1286. it.effect("falls back to error code when no message is present", () =>
  1287. Effect.gen(function* () {
  1288. const response = yield* LLMClient.generate(request).pipe(
  1289. Effect.provide(fixedResponse(sseEvents({ type: "error", code: "internal_error" }))),
  1290. )
  1291. expect(response.events).toEqual([{ type: "provider-error", message: "internal_error" }])
  1292. }),
  1293. )
  1294. it.effect("falls back to error code when message is empty", () =>
  1295. Effect.gen(function* () {
  1296. const response = yield* LLMClient.generate(request).pipe(
  1297. Effect.provide(fixedResponse(sseEvents({ type: "error", code: "internal_error", message: "" }))),
  1298. )
  1299. expect(response.events).toEqual([{ type: "provider-error", message: "internal_error" }])
  1300. }),
  1301. )
  1302. // Regression: `response.failed` carries the failure details under
  1303. // `response.error`, not at the top level. The previous handler only
  1304. // checked top-level `message`/`code` and so always emitted the bare
  1305. // "OpenAI Responses response failed" string, hiding the real cause.
  1306. it.effect("surfaces response.failed details from response.error", () =>
  1307. Effect.gen(function* () {
  1308. const response = yield* LLMClient.generate(request).pipe(
  1309. Effect.provide(
  1310. fixedResponse(
  1311. sseEvents({
  1312. type: "response.failed",
  1313. response: {
  1314. id: "resp_failed_1",
  1315. error: { code: "server_error", message: "Upstream model unavailable" },
  1316. },
  1317. }),
  1318. ),
  1319. ),
  1320. )
  1321. expect(response.events).toEqual([{ type: "provider-error", message: "server_error: Upstream model unavailable" }])
  1322. }),
  1323. )
  1324. it.effect("surfaces response.failed code when no nested message is present", () =>
  1325. Effect.gen(function* () {
  1326. const response = yield* LLMClient.generate(request).pipe(
  1327. Effect.provide(
  1328. fixedResponse(
  1329. sseEvents({
  1330. type: "response.failed",
  1331. response: { id: "resp_failed_2", error: { code: "invalid_prompt" } },
  1332. }),
  1333. ),
  1334. ),
  1335. )
  1336. expect(response.events).toEqual([{ type: "provider-error", message: "invalid_prompt" }])
  1337. }),
  1338. )
  1339. it.effect("surfaces error event details even when they arrive nested under response.error", () =>
  1340. Effect.gen(function* () {
  1341. // Some OpenAI-compatible proxies and older SDK versions wrap the
  1342. // top-level error fields into a nested `response.error` payload
  1343. // when they bubble up an HTTP error as an SSE `error` event. Honour
  1344. // both shapes so the user still sees the underlying cause instead
  1345. // of the catch-all string.
  1346. const response = yield* LLMClient.generate(request).pipe(
  1347. Effect.provide(
  1348. fixedResponse(
  1349. sseEvents({
  1350. type: "error",
  1351. response: { error: { code: "context_length_exceeded", message: "prompt too long" } },
  1352. }),
  1353. ),
  1354. ),
  1355. )
  1356. expect(response.events).toEqual([
  1357. {
  1358. type: "provider-error",
  1359. message: "context_length_exceeded: prompt too long",
  1360. classification: "context-overflow",
  1361. },
  1362. ])
  1363. }),
  1364. )
  1365. it.effect("surfaces error event details nested under a top-level error field", () =>
  1366. Effect.gen(function* () {
  1367. const response = yield* LLMClient.generate(request).pipe(
  1368. Effect.provide(
  1369. fixedResponse(
  1370. sseEvents({
  1371. type: "error",
  1372. error: { code: "context_length_exceeded", message: "prompt too long" },
  1373. }),
  1374. ),
  1375. ),
  1376. )
  1377. expect(response.events).toEqual([
  1378. {
  1379. type: "provider-error",
  1380. message: "context_length_exceeded: prompt too long",
  1381. classification: "context-overflow",
  1382. },
  1383. ])
  1384. }),
  1385. )
  1386. it.effect("falls back to a stable default when both error and response are absent", () =>
  1387. Effect.gen(function* () {
  1388. const response = yield* LLMClient.generate(request).pipe(
  1389. Effect.provide(fixedResponse(sseEvents({ type: "error" }))),
  1390. )
  1391. expect(response.events).toEqual([{ type: "provider-error", message: "OpenAI Responses stream error" }])
  1392. }),
  1393. )
  1394. it.effect("falls back to a stable default when response.failed has no error payload", () =>
  1395. Effect.gen(function* () {
  1396. const response = yield* LLMClient.generate(request).pipe(
  1397. Effect.provide(fixedResponse(sseEvents({ type: "response.failed", response: { id: "resp_failed_3" } }))),
  1398. )
  1399. expect(response.events).toEqual([{ type: "provider-error", message: "OpenAI Responses response failed" }])
  1400. }),
  1401. )
  1402. it.effect("fails HTTP provider errors before stream parsing", () =>
  1403. Effect.gen(function* () {
  1404. const error = yield* LLMClient.generate(request).pipe(
  1405. Effect.provide(
  1406. fixedResponse('{"error":{"type":"invalid_request_error","message":"Bad request"}}', {
  1407. status: 400,
  1408. headers: { "content-type": "application/json" },
  1409. }),
  1410. ),
  1411. Effect.flip,
  1412. )
  1413. expect(error).toBeInstanceOf(LLMError)
  1414. expect(error.reason).toMatchObject({ _tag: "InvalidRequest" })
  1415. expect(error.message).toContain("HTTP 400")
  1416. }),
  1417. )
  1418. })