The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { type Generated, OpenAiClient, OpenAiLanguageModel, OpenAiSchema, OpenAiTool } from "@effect/ai-openai" |
| 2 | import { assert, describe, it } from "@effect/vitest" |
| 3 | import { assertTrue, deepStrictEqual, strictEqual } from "@effect/vitest/utils" |
| 4 | import { Array, Context, Effect, Layer, Redacted, Ref, Schema, SchemaGetter, Stream } from "effect" |
| 5 | import { AiError, LanguageModel, Prompt, Response as AiResponse, Tool, Toolkit } from "effect/ai" |
| 6 | import { HttpClient, type HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/http" |
| 7 | |
| 8 | const webSearchFailures = [ |
| 9 | { tool: OpenAiTool.WebSearch({}), status: "failed" }, |
| 10 | { tool: OpenAiTool.WebSearch({}), status: "incomplete" }, |
| 11 | { tool: OpenAiTool.WebSearchPreview({}), status: "searching" } |
| 12 | ] as const |
| 13 | |
| 14 | const fileSearchOutcomes = [ |
| 15 | { status: "completed", isFailure: false, results: undefined }, |
| 16 | { status: "failed", isFailure: true, results: null }, |
| 17 | { status: "incomplete", isFailure: true, results: [{ file_id: "file_123", text: "Matching text" }] } |
| 18 | ] as const |
| 19 | |
| 20 | describe("OpenAiLanguageModel", () => { |
| 21 | describe("make", () => { |
| 22 | it.effect("sends correct model in request", () => |
| 23 | Effect.gen(function*() { |
| 24 | const result = yield* LanguageModel.generateText({ prompt: "test" }).pipe( |
| 25 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")) |
| 26 | ) |
| 27 | |
| 28 | const metadata = result.content.find((part) => part.type === "response-metadata") |
| 29 | |
| 30 | strictEqual(metadata?.modelId, "gpt-4o-mini") |
| 31 | }).pipe(Effect.provide(makeTestLayer()))) |
| 32 | |
| 33 | it.effect("sends custom model string in request", () => |
| 34 | Effect.gen(function*() { |
| 35 | const result = yield* LanguageModel.generateText({ prompt: "test" }).pipe( |
| 36 | Effect.provide(OpenAiLanguageModel.model("ft:gpt-4o-mini:custom")) |
| 37 | ) |
| 38 | |
| 39 | const metadata = result.content.find((part) => part.type === "response-metadata") |
| 40 | strictEqual(metadata?.modelId, "ft:gpt-4o-mini:custom") |
| 41 | }).pipe(Effect.provide(makeTestLayer({ body: { model: "ft:gpt-4o-mini:custom" as any } })))) |
| 42 | }) |
| 43 | |
| 44 | describe("web search sources", () => { |
| 45 | it.effect.each(["generateText", "streamText"] as const)( |
| 46 | "preserves URL and API sources with %s", |
| 47 | (method) => |
| 48 | Effect.gen(function*() { |
| 49 | const sources = [ |
| 50 | { type: "url", url: "https://example.com/weather" }, |
| 51 | { type: "api", name: "oai-weather" } |
| 52 | ] as const |
| 53 | const action = { type: "search", query: "weather in London", sources } as const |
| 54 | const item = makeWebSearchCall({ action }) |
| 55 | const event = { type: "response.output_item.done", sequence_number: 1, output_index: 0, item } |
| 56 | const body = method === "generateText" |
| 57 | ? JSON.stringify(makeDefaultResponse({ output: [item] })) |
| 58 | : `data: ${JSON.stringify(event)}\n\n` |
| 59 | const client = makeRawResponseClient( |
| 60 | body, |
| 61 | method === "generateText" ? "application/json" : "text/event-stream" |
| 62 | ) |
| 63 | const options = { prompt: "Weather in London", toolkit: Toolkit.make(OpenAiTool.WebSearch({})) } |
| 64 | const parts = yield* Effect.gen(function*() { |
| 65 | if (method === "generateText") { |
| 66 | const response = yield* LanguageModel.generateText(options) |
| 67 | return response.content |
| 68 | } |
| 69 | return yield* LanguageModel.streamText(options).pipe(Stream.runCollect) |
| 70 | }).pipe( |
| 71 | Effect.provide(OpenAiLanguageModel.model("gpt-5.6")), |
| 72 | Effect.provide(OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") })), |
| 73 | Effect.provideService(HttpClient.HttpClient, client) |
| 74 | ) |
| 75 | |
| 76 | const calls = parts.filter((part) => part.type === "tool-call") |
| 77 | deepStrictEqual(calls.map((part) => part.params), [{ action }]) |
| 78 | |
| 79 | const results = parts.filter((part) => part.type === "tool-result") |
| 80 | deepStrictEqual(results.map((part) => part.result), [{ action, status: "completed" }]) |
| 81 | }) |
| 82 | ) |
| 83 | }) |
| 84 | |
| 85 | describe("web search find_in_page", () => { |
| 86 | it.effect.each(["generateText", "streamText"] as const)( |
| 87 | "preserves actions without a URL with %s", |
| 88 | (method) => |
| 89 | Effect.gen(function*() { |
| 90 | const action = { type: "find_in_page", pattern: "og:image" } as const |
| 91 | const item = { type: "web_search_call", id: "ws_123", status: "completed", action } |
| 92 | const event = { type: "response.output_item.done", sequence_number: 1, output_index: 0, item } |
| 93 | const body = method === "generateText" |
| 94 | ? JSON.stringify({ ...makeDefaultResponse(), output: [item] }) |
| 95 | : `data: ${JSON.stringify(event)}\n\n` |
| 96 | const client = makeRawResponseClient( |
| 97 | body, |
| 98 | method === "generateText" ? "application/json" : "text/event-stream" |
| 99 | ) |
| 100 | const options = { prompt: "Search the page", toolkit: Toolkit.make(OpenAiTool.WebSearch({})) } |
| 101 | const parts = yield* Effect.gen(function*() { |
| 102 | if (method === "generateText") return (yield* LanguageModel.generateText(options)).content |
| 103 | return yield* LanguageModel.streamText(options).pipe(Stream.runCollect) |
| 104 | }).pipe( |
| 105 | Effect.provide(OpenAiLanguageModel.model("gpt-5.4")), |
| 106 | Effect.provide(OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") })), |
| 107 | Effect.provideService(HttpClient.HttpClient, client) |
| 108 | ) |
| 109 | |
| 110 | deepStrictEqual(parts.filter((part) => part.type === "tool-call").map((part) => part.params), [{ action }]) |
| 111 | deepStrictEqual<unknown>( |
| 112 | parts.filter((part) => part.type === "tool-result").map((part) => part.result), |
| 113 | [{ action, status: "completed" }] |
| 114 | ) |
| 115 | }) |
| 116 | ) |
| 117 | }) |
| 118 | |
| 119 | describe("web search without an action", () => { |
| 120 | it.effect.each( |
| 121 | [ |
| 122 | { method: "streamText", status: "incomplete", isFailure: true }, |
| 123 | { method: "generateText", status: "incomplete", isFailure: true }, |
| 124 | { method: "generateText", status: "completed", isFailure: false } |
| 125 | ] as const |
| 126 | )("preserves omitted actions for $status with $method", ({ method, status, isFailure }) => |
| 127 | Effect.gen(function*() { |
| 128 | const item: Generated.WebSearchToolCall = { type: "web_search_call", id: "ws_123", status } |
| 129 | const event = { type: "response.output_item.done", sequence_number: 1, output_index: 0, item } |
| 130 | const body = method === "generateText" |
| 131 | ? JSON.stringify(makeDefaultResponse({ output: [item] })) |
| 132 | : `data: ${JSON.stringify(event)}\n\n` |
| 133 | const client = makeRawResponseClient(body, method === "generateText" ? "application/json" : "text/event-stream") |
| 134 | const options = { prompt: "Search the web", toolkit: Toolkit.make(OpenAiTool.WebSearch({})) } |
| 135 | const parts = yield* Effect.gen(function*() { |
| 136 | if (method === "generateText") return (yield* LanguageModel.generateText(options)).content |
| 137 | return yield* LanguageModel.streamText(options).pipe(Stream.runCollect) |
| 138 | }).pipe( |
| 139 | Effect.provide(OpenAiLanguageModel.model("gpt-6-luna")), |
| 140 | Effect.provide(OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") })), |
| 141 | Effect.provideService(HttpClient.HttpClient, client) |
| 142 | ) |
| 143 | |
| 144 | deepStrictEqual( |
| 145 | parts.filter((part) => part.type === "tool-call" || part.type === "tool-result") |
| 146 | .map((part) => ({ |
| 147 | type: part.type, |
| 148 | id: part.id, |
| 149 | providerExecuted: part.providerExecuted, |
| 150 | ...(part.type === "tool-call" |
| 151 | ? { params: part.params } |
| 152 | : { result: part.result, isFailure: part.isFailure }) |
| 153 | })), |
| 154 | [ |
| 155 | { type: "tool-call", id: "ws_123", providerExecuted: true, params: {} }, |
| 156 | { type: "tool-result", id: "ws_123", providerExecuted: true, result: { status }, isFailure } |
| 157 | ] |
| 158 | ) |
| 159 | })) |
| 160 | |
| 161 | it.effect("rejects null and malformed actions when present", () => |
| 162 | Effect.forEach([null, { type: "find_in_page", url: "https://example.com", pattern: null }], (action) => { |
| 163 | const event = { |
| 164 | type: "response.output_item.done", |
| 165 | sequence_number: 1, |
| 166 | output_index: 0, |
| 167 | item: { type: "web_search_call", id: "ws_123", status: "completed", action } |
| 168 | } |
| 169 | const client = makeRawResponseClient(`data: ${JSON.stringify(event)}\n\n`, "text/event-stream") |
| 170 | return LanguageModel.streamText({ prompt: "Search", toolkit: Toolkit.make(OpenAiTool.WebSearch({})) }).pipe( |
| 171 | Stream.runCollect, |
| 172 | Effect.flip, |
| 173 | Effect.tap((error) => { |
| 174 | assert(AiError.isAiError(error)) |
| 175 | strictEqual(error.reason._tag, "InvalidOutputError") |
| 176 | return Effect.void |
| 177 | }), |
| 178 | Effect.provide(OpenAiLanguageModel.model("gpt-6-luna")), |
| 179 | Effect.provide(OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") })), |
| 180 | Effect.provideService(HttpClient.HttpClient, client) |
| 181 | ) |
| 182 | })) |
| 183 | }) |
| 184 | |
| 185 | describe("generateText", () => { |
| 186 | describe("message preparation", () => { |
| 187 | it.effect("forwards prompt cache, safety configuration and text breakpoints", () => |
| 188 | Effect.gen(function*() { |
| 189 | const breakpoint = { mode: "explicit" } as const |
| 190 | yield* LanguageModel.generateText({ |
| 191 | prompt: Prompt.make([ |
| 192 | Prompt.systemMessage({ |
| 193 | content: "Stable instructions", |
| 194 | options: { openai: { promptCacheBreakpoint: breakpoint } } |
| 195 | }), |
| 196 | Prompt.userMessage({ |
| 197 | content: [Prompt.textPart({ |
| 198 | text: "Stable context", |
| 199 | options: { openai: { promptCacheBreakpoint: breakpoint } } |
| 200 | })] |
| 201 | }) |
| 202 | ]) |
| 203 | }).pipe( |
| 204 | Effect.provide(OpenAiLanguageModel.model("gpt-5.6", { |
| 205 | prompt_cache_key: "assistant:v1", |
| 206 | prompt_cache_options: { mode: "explicit", ttl: "30m", comparison_response_id: "resp_baseline" }, |
| 207 | prompt_cache_retention: "24h", |
| 208 | safety_identifier: "user-hash-123" |
| 209 | })) |
| 210 | ) |
| 211 | |
| 212 | const requests = yield* MockHttpClient.requests |
| 213 | const body = yield* getRequestBody(requests[0]) |
| 214 | |
| 215 | strictEqual(body.prompt_cache_key, "assistant:v1") |
| 216 | strictEqual(body.prompt_cache_retention, "24h") |
| 217 | strictEqual(body.safety_identifier, "user-hash-123") |
| 218 | deepStrictEqual(body.prompt_cache_options, { |
| 219 | mode: "explicit", |
| 220 | ttl: "30m", |
| 221 | comparison_response_id: "resp_baseline" |
| 222 | }) |
| 223 | deepStrictEqual(body.input, [{ |
| 224 | role: "developer", |
| 225 | content: [{ |
| 226 | type: "input_text", |
| 227 | text: "Stable instructions", |
| 228 | prompt_cache_breakpoint: breakpoint |
| 229 | }] |
| 230 | }, { |
| 231 | role: "user", |
| 232 | content: [{ |
| 233 | type: "input_text", |
| 234 | text: "Stable context", |
| 235 | prompt_cache_breakpoint: breakpoint |
| 236 | }] |
| 237 | }]) |
| 238 | }).pipe(Effect.provide(makeTestLayer({ body: { model: "gpt-5.6" as any } })))) |
| 239 | |
| 240 | it.effect("forwards image and file breakpoints", () => |
| 241 | Effect.gen(function*() { |
| 242 | const breakpoint = { mode: "explicit" } as const |
| 243 | yield* LanguageModel.generateText({ |
| 244 | prompt: Prompt.make([ |
| 245 | Prompt.userMessage({ |
| 246 | content: [ |
| 247 | Prompt.filePart({ |
| 248 | mediaType: "image/png", |
| 249 | data: new URL("https://example.com/image.png"), |
| 250 | options: { openai: { promptCacheBreakpoint: breakpoint } } |
| 251 | }), |
| 252 | Prompt.filePart({ |
| 253 | mediaType: "application/pdf", |
| 254 | data: new URL("https://example.com/document.pdf"), |
| 255 | options: { openai: { promptCacheBreakpoint: breakpoint } } |
| 256 | }) |
| 257 | ] |
| 258 | }) |
| 259 | ]) |
| 260 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-5.6"))) |
| 261 | |
| 262 | const requests = yield* MockHttpClient.requests |
| 263 | const body = yield* getRequestBody(requests[0]) |
| 264 | |
| 265 | deepStrictEqual(body.input, [{ |
| 266 | role: "user", |
| 267 | content: [{ |
| 268 | type: "input_image", |
| 269 | image_url: "https://example.com/image.png", |
| 270 | detail: "auto", |
| 271 | prompt_cache_breakpoint: breakpoint |
| 272 | }, { |
| 273 | type: "input_file", |
| 274 | file_url: "https://example.com/document.pdf", |
| 275 | prompt_cache_breakpoint: breakpoint |
| 276 | }] |
| 277 | }]) |
| 278 | }).pipe(Effect.provide(makeTestLayer({ body: { model: "gpt-5.6" as any } })))) |
| 279 | |
| 280 | it.effect("forwards implicit prompt cache mode without text breakpoints", () => |
| 281 | Effect.gen(function*() { |
| 282 | yield* LanguageModel.generateText({ prompt: "Stable context" }).pipe( |
| 283 | Effect.provide(OpenAiLanguageModel.model("gpt-5.6", { |
| 284 | prompt_cache_options: { mode: "implicit" } |
| 285 | })) |
| 286 | ) |
| 287 | |
| 288 | const requests = yield* MockHttpClient.requests |
| 289 | const body = yield* getRequestBody(requests[0]) |
| 290 | |
| 291 | deepStrictEqual(body.prompt_cache_options, { mode: "implicit" }) |
| 292 | deepStrictEqual(body.input, [{ |
| 293 | role: "user", |
| 294 | content: [{ type: "input_text", text: "Stable context" }] |
| 295 | }]) |
| 296 | }).pipe(Effect.provide(makeTestLayer({ body: { model: "gpt-5.6" as any } })))) |
| 297 | |
| 298 | describe("system messages", () => { |
| 299 | it.effect("uses system role for standard models", () => |
| 300 | Effect.gen(function*() { |
| 301 | yield* LanguageModel.generateText({ |
| 302 | prompt: Prompt.make([ |
| 303 | { role: "system", content: "You are a helpful assistant" }, |
| 304 | { role: "user", content: "Hello" } |
| 305 | ]) |
| 306 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 307 | |
| 308 | const requests = yield* MockHttpClient.requests |
| 309 | const body = yield* getRequestBody(requests[0]) |
| 310 | |
| 311 | const systemMessage = body.input.find((m: any) => m.role === "system") |
| 312 | assert.isDefined(systemMessage) |
| 313 | deepStrictEqual(systemMessage.content, [{ |
| 314 | type: "input_text", |
| 315 | text: "You are a helpful assistant" |
| 316 | }]) |
| 317 | }).pipe(Effect.provide(makeTestLayer()))) |
| 318 | |
| 319 | it.effect("uses developer role for reasoning models", () => |
| 320 | Effect.gen(function*() { |
| 321 | yield* LanguageModel.generateText({ |
| 322 | prompt: Prompt.make([ |
| 323 | { role: "system", content: "You are a helpful assistant" }, |
| 324 | { role: "user", content: "Hello" } |
| 325 | ]) |
| 326 | }).pipe(Effect.provide(OpenAiLanguageModel.model("o1"))) |
| 327 | |
| 328 | const requests = yield* MockHttpClient.requests |
| 329 | const body = yield* getRequestBody(requests[0]) |
| 330 | |
| 331 | const devMessage = body.input.find((m: any) => m.role === "developer") |
| 332 | assert.isDefined(devMessage) |
| 333 | deepStrictEqual(devMessage.content, [{ |
| 334 | type: "input_text", |
| 335 | text: "You are a helpful assistant" |
| 336 | }]) |
| 337 | }).pipe(Effect.provide(makeTestLayer({ body: { model: "o1" } })))) |
| 338 | |
| 339 | it.effect("uses developer role for gpt-5 models", () => |
| 340 | Effect.gen(function*() { |
| 341 | yield* LanguageModel.generateText({ |
| 342 | prompt: Prompt.make([ |
| 343 | { role: "system", content: "You are a helpful assistant" }, |
| 344 | { role: "user", content: "Hello" } |
| 345 | ]) |
| 346 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-5"))) |
| 347 | |
| 348 | const requests = yield* MockHttpClient.requests |
| 349 | const body = yield* getRequestBody(requests[0]) |
| 350 | |
| 351 | const devMessage = body.input.find((m: any) => m.role === "developer") |
| 352 | assert.isDefined(devMessage) |
| 353 | }).pipe(Effect.provide(makeTestLayer({ body: { model: "gpt-5" } })))) |
| 354 | |
| 355 | it.effect("uses developer role for o3 models", () => |
| 356 | Effect.gen(function*() { |
| 357 | yield* LanguageModel.generateText({ |
| 358 | prompt: Prompt.make([ |
| 359 | { role: "system", content: "You are a helpful assistant" }, |
| 360 | { role: "user", content: "Hello" } |
| 361 | ]) |
| 362 | }).pipe(Effect.provide(OpenAiLanguageModel.model("o3-mini"))) |
| 363 | |
| 364 | const requests = yield* MockHttpClient.requests |
| 365 | const body = yield* getRequestBody(requests[0]) |
| 366 | |
| 367 | const devMessage = body.input.find((m: any) => m.role === "developer") |
| 368 | assert.isDefined(devMessage) |
| 369 | }).pipe(Effect.provide(makeTestLayer({ body: { model: "o3-mini" } })))) |
| 370 | }) |
| 371 | |
| 372 | describe("user messages", () => { |
| 373 | it.effect("converts text parts to input_text", () => |
| 374 | Effect.gen(function*() { |
| 375 | yield* LanguageModel.generateText({ |
| 376 | prompt: "Hello world" |
| 377 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 378 | |
| 379 | const requests = yield* MockHttpClient.requests |
| 380 | const body = yield* getRequestBody(requests[0]) |
| 381 | |
| 382 | const userMessage = body.input.find((m: any) => m.role === "user") |
| 383 | assert.isDefined(userMessage) |
| 384 | deepStrictEqual(userMessage.content, [{ type: "input_text", text: "Hello world" }]) |
| 385 | }).pipe(Effect.provide(makeTestLayer()))) |
| 386 | |
| 387 | it.effect("handles image URLs", () => |
| 388 | Effect.gen(function*() { |
| 389 | yield* LanguageModel.generateText({ |
| 390 | prompt: Prompt.make([{ |
| 391 | role: "user", |
| 392 | content: [ |
| 393 | Prompt.filePart({ |
| 394 | mediaType: "image/png", |
| 395 | data: new URL("https://example.com/image.png") |
| 396 | }) |
| 397 | ] |
| 398 | }]) |
| 399 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 400 | |
| 401 | const requests = yield* MockHttpClient.requests |
| 402 | const body = yield* getRequestBody(requests[0]) |
| 403 | |
| 404 | const userMessage = body.input.find((m: any) => m.role === "user") |
| 405 | deepStrictEqual(userMessage.content, [{ |
| 406 | type: "input_image", |
| 407 | image_url: "https://example.com/image.png", |
| 408 | detail: "auto" |
| 409 | }]) |
| 410 | }).pipe(Effect.provide(makeTestLayer()))) |
| 411 | |
| 412 | it.effect("handles image strings", () => |
| 413 | Effect.gen(function*() { |
| 414 | const base64 = "iVBORw0KGgo=" |
| 415 | const dataUrl = `data:image/png;base64,${base64}` |
| 416 | const upperCaseDataUrl = `DATA:image/png;base64,${base64}` |
| 417 | const url = "https://example.com/image.png" |
| 418 | |
| 419 | yield* LanguageModel.generateText({ |
| 420 | prompt: Prompt.make([Prompt.userMessage({ |
| 421 | content: [base64, dataUrl, upperCaseDataUrl, url].map((data) => |
| 422 | Prompt.filePart({ mediaType: "image/png", data }) |
| 423 | ) |
| 424 | })]) |
| 425 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 426 | |
| 427 | const requests = yield* MockHttpClient.requests |
| 428 | const body = yield* getRequestBody(requests[0]) |
| 429 | |
| 430 | assert.deepStrictEqual(body.input, [{ |
| 431 | role: "user", |
| 432 | content: [ |
| 433 | { type: "input_image", image_url: `data:image/png;base64,${base64}`, detail: "auto" }, |
| 434 | { type: "input_image", image_url: dataUrl, detail: "auto" }, |
| 435 | { type: "input_image", image_url: upperCaseDataUrl, detail: "auto" }, |
| 436 | { type: "input_image", image_url: url, detail: "auto" } |
| 437 | ] |
| 438 | }]) |
| 439 | }).pipe(Effect.provide(makeTestLayer()))) |
| 440 | |
| 441 | it.effect("handles image with custom detail level", () => |
| 442 | Effect.gen(function*() { |
| 443 | yield* LanguageModel.generateText({ |
| 444 | prompt: Prompt.make([{ |
| 445 | role: "user", |
| 446 | content: [ |
| 447 | Prompt.filePart({ |
| 448 | mediaType: "image/png", |
| 449 | data: new URL("https://example.com/image.png"), |
| 450 | options: { openai: { imageDetail: "high" } } |
| 451 | }) |
| 452 | ] |
| 453 | }]) |
| 454 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 455 | |
| 456 | const requests = yield* MockHttpClient.requests |
| 457 | const body = yield* getRequestBody(requests[0]) |
| 458 | |
| 459 | const userMessage = body.input.find((m: any) => m.role === "user") |
| 460 | strictEqual(userMessage.content[0].detail, "high") |
| 461 | }).pipe(Effect.provide(makeTestLayer()))) |
| 462 | |
| 463 | it.effect("handles image file IDs with configured prefixes", () => |
| 464 | Effect.gen(function*() { |
| 465 | yield* LanguageModel.generateText({ |
| 466 | prompt: Prompt.make([{ |
| 467 | role: "user", |
| 468 | content: [ |
| 469 | Prompt.filePart({ |
| 470 | mediaType: "image/png", |
| 471 | data: "file-abc123" |
| 472 | }) |
| 473 | ] |
| 474 | }]) |
| 475 | }).pipe( |
| 476 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini", { |
| 477 | fileIdPrefixes: ["file-"] |
| 478 | })) |
| 479 | ) |
| 480 | |
| 481 | const requests = yield* MockHttpClient.requests |
| 482 | const body = yield* getRequestBody(requests[0]) |
| 483 | |
| 484 | const userMessage = body.input.find((m: any) => m.role === "user") |
| 485 | deepStrictEqual(userMessage.content, [{ |
| 486 | type: "input_image", |
| 487 | file_id: "file-abc123", |
| 488 | detail: "auto" |
| 489 | }]) |
| 490 | }).pipe(Effect.provide(makeTestLayer()))) |
| 491 | |
| 492 | it.effect("handles image base64 data", () => |
| 493 | Effect.gen(function*() { |
| 494 | const imageData = new Uint8Array([137, 80, 78, 71]) // PNG magic bytes |
| 495 | |
| 496 | yield* LanguageModel.generateText({ |
| 497 | prompt: Prompt.make([{ |
| 498 | role: "user", |
| 499 | content: [ |
| 500 | Prompt.filePart({ |
| 501 | mediaType: "image/png", |
| 502 | data: imageData |
| 503 | }) |
| 504 | ] |
| 505 | }]) |
| 506 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 507 | |
| 508 | const requests = yield* MockHttpClient.requests |
| 509 | const body = yield* getRequestBody(requests[0]) |
| 510 | |
| 511 | const userMessage = body.input.find((m: any) => m.role === "user") |
| 512 | assert.isTrue(userMessage.content[0].image_url.startsWith("data:image/png;base64,")) |
| 513 | }).pipe(Effect.provide(makeTestLayer()))) |
| 514 | |
| 515 | it.effect("handles PDF URLs", () => |
| 516 | Effect.gen(function*() { |
| 517 | yield* LanguageModel.generateText({ |
| 518 | prompt: Prompt.make([{ |
| 519 | role: "user", |
| 520 | content: [ |
| 521 | Prompt.filePart({ |
| 522 | mediaType: "application/pdf", |
| 523 | data: new URL("https://example.com/document.pdf") |
| 524 | }) |
| 525 | ] |
| 526 | }]) |
| 527 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 528 | |
| 529 | const requests = yield* MockHttpClient.requests |
| 530 | const body = yield* getRequestBody(requests[0]) |
| 531 | |
| 532 | const userMessage = body.input.find((m: any) => m.role === "user") |
| 533 | deepStrictEqual(userMessage.content, [{ |
| 534 | type: "input_file", |
| 535 | file_url: "https://example.com/document.pdf" |
| 536 | }]) |
| 537 | }).pipe(Effect.provide(makeTestLayer()))) |
| 538 | |
| 539 | it.effect("handles PDF base64 data with filename", () => |
| 540 | Effect.gen(function*() { |
| 541 | const pdfData = new Uint8Array([0x25, 0x50, 0x44, 0x46]) // %PDF |
| 542 | |
| 543 | yield* LanguageModel.generateText({ |
| 544 | prompt: Prompt.make([{ |
| 545 | role: "user", |
| 546 | content: [ |
| 547 | Prompt.filePart({ |
| 548 | mediaType: "application/pdf", |
| 549 | data: pdfData, |
| 550 | fileName: "document.pdf" |
| 551 | }) |
| 552 | ] |
| 553 | }]) |
| 554 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 555 | |
| 556 | const requests = yield* MockHttpClient.requests |
| 557 | const body = yield* getRequestBody(requests[0]) |
| 558 | |
| 559 | const userMessage = body.input.find((m: any) => m.role === "user") |
| 560 | strictEqual(userMessage.content[0].type, "input_file") |
| 561 | strictEqual(userMessage.content[0].filename, "document.pdf") |
| 562 | assert.isTrue(userMessage.content[0].file_data.startsWith("data:application/pdf;base64,")) |
| 563 | }).pipe(Effect.provide(makeTestLayer()))) |
| 564 | |
| 565 | const pdfStringCases = [ |
| 566 | { |
| 567 | label: "base64", |
| 568 | data: "JVBERi0xLjcK", |
| 569 | content: { |
| 570 | type: "input_file", |
| 571 | filename: "part-0.pdf", |
| 572 | file_data: "data:application/pdf;base64,JVBERi0xLjcK" |
| 573 | } |
| 574 | }, |
| 575 | { |
| 576 | label: "data URL", |
| 577 | data: "data:application/pdf;base64,JVBERi0xLjcK", |
| 578 | content: { |
| 579 | type: "input_file", |
| 580 | filename: "part-0.pdf", |
| 581 | file_data: "data:application/pdf;base64,JVBERi0xLjcK" |
| 582 | } |
| 583 | }, |
| 584 | { |
| 585 | label: "HTTPS URL", |
| 586 | data: "https://example.com/document.pdf", |
| 587 | content: { type: "input_file", file_url: "https://example.com/document.pdf" } |
| 588 | } |
| 589 | ] |
| 590 | |
| 591 | for (const { content, data, label } of pdfStringCases) { |
| 592 | it.effect(`handles PDF ${label} strings`, () => |
| 593 | Effect.gen(function*() { |
| 594 | yield* LanguageModel.generateText({ |
| 595 | prompt: Prompt.make([{ |
| 596 | role: "user", |
| 597 | content: [Prompt.filePart({ mediaType: "application/pdf", data })] |
| 598 | }]) |
| 599 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 600 | |
| 601 | const requests = yield* MockHttpClient.requests |
| 602 | const body = yield* getRequestBody(requests[0]) |
| 603 | |
| 604 | const userMessage = body.input.find((m: any) => m.role === "user") |
| 605 | deepStrictEqual(userMessage.content, [content]) |
| 606 | }).pipe(Effect.provide(makeTestLayer()))) |
| 607 | } |
| 608 | }) |
| 609 | |
| 610 | describe("assistant messages", () => { |
| 611 | it.effect("converts text parts to message output", () => |
| 612 | Effect.gen(function*() { |
| 613 | yield* LanguageModel.generateText({ |
| 614 | prompt: Prompt.make([ |
| 615 | { role: "user", content: "Hello" }, |
| 616 | { |
| 617 | role: "assistant", |
| 618 | content: [Prompt.textPart({ text: "Hi there!" })] |
| 619 | }, |
| 620 | { role: "user", content: "How are you?" } |
| 621 | ]) |
| 622 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 623 | |
| 624 | const requests = yield* MockHttpClient.requests |
| 625 | const body = yield* getRequestBody(requests[0]) |
| 626 | |
| 627 | const assistantMessage = body.input.find((m: any) => m.type === "message" && m.role === "assistant") |
| 628 | assert.isDefined(assistantMessage) |
| 629 | strictEqual(assistantMessage.content[0].type, "output_text") |
| 630 | strictEqual(assistantMessage.content[0].text, "Hi there!") |
| 631 | }).pipe(Effect.provide(makeTestLayer()))) |
| 632 | |
| 633 | it.effect("converts reasoning parts", () => |
| 634 | Effect.gen(function*() { |
| 635 | yield* LanguageModel.generateText({ |
| 636 | prompt: Prompt.make([ |
| 637 | { role: "user", content: "Think step by step" }, |
| 638 | { |
| 639 | role: "assistant", |
| 640 | content: [ |
| 641 | Prompt.reasoningPart({ |
| 642 | text: "Let me think...", |
| 643 | options: { openai: { itemId: "reasoning_123" } } |
| 644 | }) |
| 645 | ] |
| 646 | }, |
| 647 | { role: "user", content: "Continue" } |
| 648 | ]) |
| 649 | }).pipe(Effect.provide(OpenAiLanguageModel.model("o1"))) |
| 650 | |
| 651 | const requests = yield* MockHttpClient.requests |
| 652 | const body = yield* getRequestBody(requests[0]) |
| 653 | |
| 654 | const reasoningItem = body.input.find((m: any) => m.type === "reasoning") |
| 655 | assert.isDefined(reasoningItem) |
| 656 | strictEqual(reasoningItem.id, "reasoning_123") |
| 657 | }).pipe(Effect.provide(makeTestLayer({ body: { model: "o1" } })))) |
| 658 | |
| 659 | it.effect("replays encrypted reasoning from response parts", () => |
| 660 | Effect.gen(function*() { |
| 661 | const history = Prompt.fromResponseParts([ |
| 662 | AiResponse.makePart("reasoning-start", { |
| 663 | id: "reasoning_123:0", |
| 664 | metadata: { openai: { itemId: "reasoning_123" } } |
| 665 | }), |
| 666 | AiResponse.makePart("reasoning-delta", { |
| 667 | id: "reasoning_123:0", |
| 668 | delta: "Let me think..." |
| 669 | }), |
| 670 | AiResponse.makePart("reasoning-end", { |
| 671 | id: "reasoning_123:0", |
| 672 | metadata: { |
| 673 | openai: { |
| 674 | itemId: "reasoning_123", |
| 675 | encryptedContent: "encrypted-reasoning" |
| 676 | } |
| 677 | } |
| 678 | }) |
| 679 | ]) |
| 680 | |
| 681 | yield* LanguageModel.generateText({ |
| 682 | prompt: Prompt.concat(history, Prompt.make("Continue")) |
| 683 | }).pipe(Effect.provide(OpenAiLanguageModel.model("o1"))) |
| 684 | |
| 685 | const requests = yield* MockHttpClient.requests |
| 686 | const body = yield* getRequestBody(requests[0]) |
| 687 | const reasoningItem = body.input.find((item: any) => item.type === "reasoning") |
| 688 | |
| 689 | assert.isDefined(reasoningItem) |
| 690 | deepStrictEqual(reasoningItem, { |
| 691 | type: "reasoning", |
| 692 | id: "reasoning_123", |
| 693 | summary: [{ type: "summary_text", text: "Let me think..." }], |
| 694 | encrypted_content: "encrypted-reasoning" |
| 695 | }) |
| 696 | }).pipe(Effect.provide(makeTestLayer({ body: { model: "o1" } })))) |
| 697 | |
| 698 | it.effect("replays encrypted reasoning statelessly for an unknown model", () => |
| 699 | Effect.gen(function*() { |
| 700 | const history = Prompt.fromResponseParts([ |
| 701 | AiResponse.makePart("reasoning-start", { |
| 702 | id: "reasoning_123:0", |
| 703 | metadata: { openai: { itemId: "reasoning_123" } } |
| 704 | }), |
| 705 | AiResponse.makePart("reasoning-delta", { |
| 706 | id: "reasoning_123:0", |
| 707 | delta: "Let me think..." |
| 708 | }), |
| 709 | AiResponse.makePart("reasoning-end", { |
| 710 | id: "reasoning_123:0", |
| 711 | metadata: { |
| 712 | openai: { |
| 713 | itemId: "reasoning_123", |
| 714 | encryptedContent: "encrypted-reasoning" |
| 715 | } |
| 716 | } |
| 717 | }) |
| 718 | ]) |
| 719 | |
| 720 | yield* LanguageModel.generateText({ |
| 721 | prompt: Prompt.concat(history, Prompt.make("Continue")) |
| 722 | }).pipe(Effect.provide(OpenAiLanguageModel.model("custom-model", { store: false }))) |
| 723 | |
| 724 | const requests = yield* MockHttpClient.requests |
| 725 | const body = yield* getRequestBody(requests[0]) |
| 726 | const reasoningItem = body.input.find((item: any) => item.type === "reasoning") |
| 727 | |
| 728 | assert.isDefined(reasoningItem) |
| 729 | deepStrictEqual(reasoningItem, { |
| 730 | type: "reasoning", |
| 731 | id: "reasoning_123", |
| 732 | summary: [{ type: "summary_text", text: "Let me think..." }], |
| 733 | encrypted_content: "encrypted-reasoning" |
| 734 | }) |
| 735 | }).pipe(Effect.provide(makeTestLayer({ body: { model: "custom-model" } })))) |
| 736 | |
| 737 | it.effect("serializes stored assistant history according to item reference config", () => |
| 738 | Effect.gen(function*() { |
| 739 | yield* LanguageModel.generateText({ |
| 740 | prompt: storedHistoryPrompt, |
| 741 | toolkit: TestToolkit, |
| 742 | disableToolCallResolution: true |
| 743 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini", { store: true }))) |
| 744 | |
| 745 | yield* LanguageModel.generateText({ |
| 746 | prompt: storedHistoryPrompt, |
| 747 | toolkit: TestToolkit, |
| 748 | disableToolCallResolution: true |
| 749 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini", { |
| 750 | store: true, |
| 751 | useItemReferences: false |
| 752 | }))) |
| 753 | |
| 754 | const requests = yield* MockHttpClient.requests |
| 755 | const referencedBody = yield* getRequestBody(requests[0]) |
| 756 | const inlineBody = yield* getRequestBody(requests[1]) |
| 757 | |
| 758 | assert.deepStrictEqual(referencedBody.input, [ |
| 759 | { role: "user", content: [{ type: "input_text", text: "Question" }] }, |
| 760 | { type: "item_reference", id: "msg_1" }, |
| 761 | { type: "item_reference", id: "rs_1" }, |
| 762 | { type: "item_reference", id: "fc_1" }, |
| 763 | { type: "function_call_output", call_id: "call_1", output: "{\"output\":\"result\"}" }, |
| 764 | { role: "user", content: [{ type: "input_text", text: "Continue" }] } |
| 765 | ]) |
| 766 | |
| 767 | assert.deepStrictEqual(inlineBody.input, [ |
| 768 | { role: "user", content: [{ type: "input_text", text: "Question" }] }, |
| 769 | { |
| 770 | id: "msg_1", |
| 771 | type: "message", |
| 772 | role: "assistant", |
| 773 | status: "completed", |
| 774 | content: [{ |
| 775 | type: "output_text", |
| 776 | text: "Answer", |
| 777 | annotations: [], |
| 778 | logprobs: [] |
| 779 | }] |
| 780 | }, |
| 781 | { |
| 782 | type: "reasoning", |
| 783 | id: "rs_1", |
| 784 | summary: [{ type: "summary_text", text: "Thinking" }], |
| 785 | encrypted_content: "encrypted-reasoning" |
| 786 | }, |
| 787 | { |
| 788 | type: "function_call", |
| 789 | name: "TestTool", |
| 790 | call_id: "call_1", |
| 791 | arguments: "{\"input\":\"value\"}", |
| 792 | id: "fc_1" |
| 793 | }, |
| 794 | { type: "function_call_output", call_id: "call_1", output: "{\"output\":\"result\"}" }, |
| 795 | { role: "user", content: [{ type: "input_text", text: "Continue" }] } |
| 796 | ]) |
| 797 | strictEqual(inlineBody.useItemReferences, undefined) |
| 798 | }).pipe(Effect.provide(makeTestLayer()))) |
| 799 | |
| 800 | it.effect("replays matched provider-executed history inline", () => |
| 801 | Effect.gen(function*() { |
| 802 | yield* LanguageModel.generateText({ |
| 803 | prompt: providerExecutedHistoryPrompt, |
| 804 | toolkit: Toolkit.make(OpenAiTool.WebSearch({})), |
| 805 | disableToolCallResolution: true |
| 806 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini", { |
| 807 | store: true, |
| 808 | useItemReferences: false |
| 809 | }))) |
| 810 | |
| 811 | const requests = yield* MockHttpClient.requests |
| 812 | const body = yield* getRequestBody(requests[0]) |
| 813 | |
| 814 | assert.deepStrictEqual(body.input, [ |
| 815 | { role: "user", content: [{ type: "input_text", text: "Search" }] }, |
| 816 | { |
| 817 | type: "web_search_call", |
| 818 | id: "ws_1", |
| 819 | status: "completed", |
| 820 | action: { type: "search", query: "Effect TypeScript" } |
| 821 | }, |
| 822 | { role: "user", content: [{ type: "input_text", text: "Continue" }] } |
| 823 | ]) |
| 824 | }).pipe(Effect.provide(makeTestLayer()))) |
| 825 | |
| 826 | it.effect("replays web search history with an omitted action", () => |
| 827 | Effect.gen(function*() { |
| 828 | yield* LanguageModel.generateText({ |
| 829 | prompt: Prompt.make([ |
| 830 | { role: "user", content: "Search" }, |
| 831 | { |
| 832 | role: "assistant", |
| 833 | content: [ |
| 834 | Prompt.toolCallPart({ id: "ws_1", name: "OpenAiWebSearch", params: {}, providerExecuted: true }), |
| 835 | Prompt.toolResultPart({ |
| 836 | id: "ws_1", |
| 837 | name: "OpenAiWebSearch", |
| 838 | providerExecuted: true, |
| 839 | isFailure: true, |
| 840 | result: { status: "incomplete" } |
| 841 | }) |
| 842 | ] |
| 843 | }, |
| 844 | { role: "user", content: "Continue" } |
| 845 | ]), |
| 846 | toolkit: Toolkit.make(OpenAiTool.WebSearch({})), |
| 847 | disableToolCallResolution: true |
| 848 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-6-luna", { |
| 849 | store: true, |
| 850 | useItemReferences: false |
| 851 | }))) |
| 852 | |
| 853 | const body = yield* getRequestBody((yield* MockHttpClient.requests)[0]) |
| 854 | deepStrictEqual(body.input?.[1], { type: "web_search_call", id: "ws_1", status: "incomplete" }) |
| 855 | }).pipe(Effect.provide(makeTestLayer()))) |
| 856 | |
| 857 | it.effect("fails locally when provider-executed history cannot be replayed inline", () => |
| 858 | Effect.gen(function*() { |
| 859 | const cases = [ |
| 860 | providerExecutedCall, |
| 861 | providerExecutedResult |
| 862 | ] as const |
| 863 | yield* Effect.forEach(cases, (part) => |
| 864 | LanguageModel.generateText({ |
| 865 | prompt: Prompt.make([ |
| 866 | { role: "user", content: "Search" }, |
| 867 | { |
| 868 | role: "assistant", |
| 869 | content: [part] |
| 870 | }, |
| 871 | { role: "user", content: "Continue" } |
| 872 | ]), |
| 873 | toolkit: Toolkit.make(OpenAiTool.WebSearch({})), |
| 874 | disableToolCallResolution: true |
| 875 | }).pipe( |
| 876 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini", { |
| 877 | store: true, |
| 878 | useItemReferences: false |
| 879 | })), |
| 880 | Effect.flip, |
| 881 | Effect.tap((error) => |
| 882 | Effect.sync(() => { |
| 883 | strictEqual(error.reason._tag, "InvalidRequestError") |
| 884 | if (error.reason._tag === "InvalidRequestError") { |
| 885 | assert.include(error.reason.description, "OpenAiWebSearch") |
| 886 | assert.include(error.reason.description, "ws_1") |
| 887 | } |
| 888 | }) |
| 889 | ) |
| 890 | )) |
| 891 | |
| 892 | strictEqual((yield* MockHttpClient.requests).length, 0) |
| 893 | }).pipe(Effect.provide(makeTestLayer()))) |
| 894 | |
| 895 | it.effect("converts tool call parts to function_call", () => |
| 896 | Effect.gen(function*() { |
| 897 | yield* LanguageModel.generateText({ |
| 898 | prompt: Prompt.make([ |
| 899 | { role: "user", content: "Use the tool" }, |
| 900 | { |
| 901 | role: "assistant", |
| 902 | content: [ |
| 903 | Prompt.toolCallPart({ |
| 904 | id: "call_abc", |
| 905 | name: "TestTool", |
| 906 | params: { input: "test" }, |
| 907 | providerExecuted: false |
| 908 | }) |
| 909 | ] |
| 910 | }, |
| 911 | { |
| 912 | role: "tool", |
| 913 | content: [ |
| 914 | Prompt.toolResultPart({ |
| 915 | id: "call_abc", |
| 916 | name: "TestTool", |
| 917 | isFailure: false, |
| 918 | result: { output: "result" }, |
| 919 | providerExecuted: false |
| 920 | }) |
| 921 | ] |
| 922 | } |
| 923 | ]), |
| 924 | toolkit: TestToolkit |
| 925 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 926 | |
| 927 | const requests = yield* MockHttpClient.requests |
| 928 | const body = yield* getRequestBody(requests[0]) |
| 929 | |
| 930 | const functionCall = body.input.find((m: any) => m.type === "function_call") |
| 931 | assert.isDefined(functionCall) |
| 932 | strictEqual(functionCall.name, "TestTool") |
| 933 | strictEqual(functionCall.call_id, "call_abc") |
| 934 | }).pipe(Effect.provide([makeTestLayer(), TestToolkitLayer]))) |
| 935 | }) |
| 936 | |
| 937 | describe("tool messages", () => { |
| 938 | it.effect("converts tool results to function_call_output", () => |
| 939 | Effect.gen(function*() { |
| 940 | yield* LanguageModel.generateText({ |
| 941 | prompt: Prompt.make([ |
| 942 | { role: "user", content: "Use the tool" }, |
| 943 | { |
| 944 | role: "assistant", |
| 945 | content: [ |
| 946 | Prompt.toolCallPart({ |
| 947 | id: "call_abc", |
| 948 | name: "TestTool", |
| 949 | params: { input: "test" }, |
| 950 | providerExecuted: false |
| 951 | }) |
| 952 | ] |
| 953 | }, |
| 954 | { |
| 955 | role: "tool", |
| 956 | content: [ |
| 957 | Prompt.toolResultPart({ |
| 958 | id: "call_abc", |
| 959 | name: "TestTool", |
| 960 | isFailure: false, |
| 961 | result: { output: "result" }, |
| 962 | providerExecuted: false |
| 963 | }) |
| 964 | ] |
| 965 | } |
| 966 | ]), |
| 967 | toolkit: TestToolkit |
| 968 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 969 | |
| 970 | const requests = yield* MockHttpClient.requests |
| 971 | const body = yield* getRequestBody(requests[0]) |
| 972 | |
| 973 | const toolOutput = body.input.find((m: any) => m.type === "function_call_output") |
| 974 | assert.isDefined(toolOutput) |
| 975 | strictEqual(toolOutput.call_id, "call_abc") |
| 976 | strictEqual(toolOutput.output, JSON.stringify({ output: "result" })) |
| 977 | }).pipe(Effect.provide([makeTestLayer(), TestToolkitLayer]))) |
| 978 | |
| 979 | it.effect("preserves string tool results", () => |
| 980 | Effect.gen(function*() { |
| 981 | yield* LanguageModel.generateText({ |
| 982 | prompt: Prompt.make([ |
| 983 | { role: "user", content: "Use the tool" }, |
| 984 | { |
| 985 | role: "assistant", |
| 986 | content: [ |
| 987 | Prompt.toolCallPart({ |
| 988 | id: "call_text", |
| 989 | name: "TestTool", |
| 990 | params: { input: "test" }, |
| 991 | providerExecuted: false |
| 992 | }) |
| 993 | ] |
| 994 | }, |
| 995 | { |
| 996 | role: "tool", |
| 997 | content: [ |
| 998 | Prompt.toolResultPart({ |
| 999 | id: "call_text", |
| 1000 | name: "TestTool", |
| 1001 | isFailure: false, |
| 1002 | result: "PLAIN_TEXT_SENTINEL\n", |
| 1003 | providerExecuted: false |
| 1004 | }) |
| 1005 | ] |
| 1006 | } |
| 1007 | ]), |
| 1008 | toolkit: TestToolkit |
| 1009 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1010 | |
| 1011 | const requests = yield* MockHttpClient.requests |
| 1012 | const body = yield* getRequestBody(requests[0]) |
| 1013 | const toolOutput = body.input.find((item: any) => item.type === "function_call_output") |
| 1014 | |
| 1015 | assert.isDefined(toolOutput) |
| 1016 | strictEqual(toolOutput.output, "PLAIN_TEXT_SENTINEL\n") |
| 1017 | }).pipe(Effect.provide([makeTestLayer(), TestToolkitLayer]))) |
| 1018 | |
| 1019 | const png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVR4nGP4DwQACfsD/fteaysAAAAASUVORK5CYII=" |
| 1020 | const modelOutputCases = [ |
| 1021 | { |
| 1022 | label: "as native input items", |
| 1023 | modelOutput: [ |
| 1024 | Prompt.textPart({ text: "Screenshot" }), |
| 1025 | Prompt.filePart({ mediaType: "image/png", data: png }) |
| 1026 | ], |
| 1027 | output: [ |
| 1028 | { type: "input_text", text: "Screenshot" }, |
| 1029 | { type: "input_image", image_url: `data:image/png;base64,${png}` } |
| 1030 | ] |
| 1031 | }, |
| 1032 | { label: "without the result when it is empty", modelOutput: [], output: "" } |
| 1033 | ] |
| 1034 | |
| 1035 | for (const { label, modelOutput, output } of modelOutputCases) { |
| 1036 | it.effect(`sends tool result model output ${label}`, () => |
| 1037 | Effect.gen(function*() { |
| 1038 | yield* LanguageModel.generateText({ |
| 1039 | prompt: Prompt.make([ |
| 1040 | { role: "user", content: "Use the tool" }, |
| 1041 | { |
| 1042 | role: "assistant", |
| 1043 | content: [ |
| 1044 | Prompt.toolCallPart({ |
| 1045 | id: "call_image", |
| 1046 | name: "TestTool", |
| 1047 | params: { input: "test" }, |
| 1048 | providerExecuted: false |
| 1049 | }) |
| 1050 | ] |
| 1051 | }, |
| 1052 | { |
| 1053 | role: "tool", |
| 1054 | content: [ |
| 1055 | Prompt.toolResultPart({ |
| 1056 | id: "call_image", |
| 1057 | name: "TestTool", |
| 1058 | isFailure: false, |
| 1059 | result: { output: png }, |
| 1060 | modelOutput, |
| 1061 | providerExecuted: false |
| 1062 | }) |
| 1063 | ] |
| 1064 | } |
| 1065 | ]), |
| 1066 | toolkit: TestToolkit |
| 1067 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1068 | |
| 1069 | const requests = yield* MockHttpClient.requests |
| 1070 | const body = yield* getRequestBody(requests[0]) |
| 1071 | const toolOutput = body.input.find((item: any) => item.type === "function_call_output") |
| 1072 | |
| 1073 | strictEqual(toolOutput.call_id, "call_image") |
| 1074 | deepStrictEqual( |
| 1075 | Array.isArray(toolOutput.output) |
| 1076 | ? toolOutput.output.map(({ detail: _, ...item }: any) => item) |
| 1077 | : toolOutput.output, |
| 1078 | output |
| 1079 | ) |
| 1080 | }).pipe(Effect.provide([makeTestLayer(), TestToolkitLayer]))) |
| 1081 | } |
| 1082 | |
| 1083 | it.effect("emits only the specialized output for apply_patch results", () => |
| 1084 | Effect.gen(function*() { |
| 1085 | const toolkit = Toolkit.make(OpenAiTool.ApplyPatch({})) |
| 1086 | yield* LanguageModel.generateText({ |
| 1087 | prompt: Prompt.make([ |
| 1088 | { role: "user", content: "Apply a patch" }, |
| 1089 | { |
| 1090 | role: "assistant", |
| 1091 | content: [Prompt.toolCallPart({ |
| 1092 | id: "call_apply_patch", |
| 1093 | name: "OpenAiApplyPatch", |
| 1094 | params: { |
| 1095 | call_id: "call_apply_patch", |
| 1096 | operation: { type: "delete_file", path: "old.ts" } |
| 1097 | }, |
| 1098 | providerExecuted: false |
| 1099 | })] |
| 1100 | }, |
| 1101 | { |
| 1102 | role: "tool", |
| 1103 | content: [Prompt.toolResultPart({ |
| 1104 | id: "call_apply_patch", |
| 1105 | name: "OpenAiApplyPatch", |
| 1106 | isFailure: false, |
| 1107 | result: { status: "completed", output: "deleted" }, |
| 1108 | providerExecuted: false |
| 1109 | })] |
| 1110 | } |
| 1111 | ]), |
| 1112 | toolkit, |
| 1113 | disableToolCallResolution: true |
| 1114 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1115 | |
| 1116 | const requests = yield* MockHttpClient.requests |
| 1117 | const body = yield* getRequestBody(requests[0]) |
| 1118 | const outputs = body.input.filter((item: any) => |
| 1119 | item.call_id === "call_apply_patch" && item.type.endsWith("_output") |
| 1120 | ) |
| 1121 | |
| 1122 | deepStrictEqual(outputs.map((item: any) => item.type), ["apply_patch_call_output"]) |
| 1123 | }).pipe(Effect.provide(makeTestLayer()))) |
| 1124 | |
| 1125 | it.effect("emits only the specialized output for shell results", () => |
| 1126 | Effect.gen(function*() { |
| 1127 | const toolkit = Toolkit.make(OpenAiTool.Shell({})) |
| 1128 | yield* LanguageModel.generateText({ |
| 1129 | prompt: Prompt.make([ |
| 1130 | { role: "user", content: "Run a shell command" }, |
| 1131 | { |
| 1132 | role: "assistant", |
| 1133 | content: [Prompt.toolCallPart({ |
| 1134 | id: "call_shell", |
| 1135 | name: "OpenAiShell", |
| 1136 | params: { |
| 1137 | action: { |
| 1138 | commands: ["echo hello"], |
| 1139 | timeout_ms: null, |
| 1140 | max_output_length: null |
| 1141 | } |
| 1142 | }, |
| 1143 | providerExecuted: false |
| 1144 | })] |
| 1145 | }, |
| 1146 | { |
| 1147 | role: "tool", |
| 1148 | content: [Prompt.toolResultPart({ |
| 1149 | id: "call_shell", |
| 1150 | name: "OpenAiShell", |
| 1151 | isFailure: false, |
| 1152 | result: { |
| 1153 | output: [{ |
| 1154 | stdout: "hello\n", |
| 1155 | stderr: "", |
| 1156 | outcome: { type: "exit", exit_code: 0 } |
| 1157 | }] |
| 1158 | }, |
| 1159 | providerExecuted: false |
| 1160 | })] |
| 1161 | } |
| 1162 | ]), |
| 1163 | toolkit, |
| 1164 | disableToolCallResolution: true |
| 1165 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1166 | |
| 1167 | const requests = yield* MockHttpClient.requests |
| 1168 | const body = yield* getRequestBody(requests[0]) |
| 1169 | const outputs = body.input.filter((item: any) => |
| 1170 | item.call_id === "call_shell" && item.type.endsWith("_output") |
| 1171 | ) |
| 1172 | |
| 1173 | deepStrictEqual(outputs.map((item: any) => item.type), ["shell_call_output"]) |
| 1174 | }).pipe(Effect.provide(makeTestLayer()))) |
| 1175 | |
| 1176 | it.effect("emits only the specialized output for local_shell results", () => |
| 1177 | Effect.gen(function*() { |
| 1178 | const toolkit = Toolkit.make(OpenAiTool.LocalShell({})) |
| 1179 | yield* LanguageModel.generateText({ |
| 1180 | prompt: Prompt.make([ |
| 1181 | { role: "user", content: "Run a local shell command" }, |
| 1182 | { |
| 1183 | role: "assistant", |
| 1184 | content: [Prompt.toolCallPart({ |
| 1185 | id: "call_local_shell", |
| 1186 | name: "OpenAiLocalShell", |
| 1187 | params: { |
| 1188 | action: { |
| 1189 | type: "exec", |
| 1190 | command: ["echo", "hello"], |
| 1191 | env: {} |
| 1192 | } |
| 1193 | }, |
| 1194 | providerExecuted: false |
| 1195 | })] |
| 1196 | }, |
| 1197 | { |
| 1198 | role: "tool", |
| 1199 | content: [Prompt.toolResultPart({ |
| 1200 | id: "call_local_shell", |
| 1201 | name: "OpenAiLocalShell", |
| 1202 | isFailure: false, |
| 1203 | result: { output: "hello\n" }, |
| 1204 | providerExecuted: false |
| 1205 | })] |
| 1206 | } |
| 1207 | ]), |
| 1208 | toolkit, |
| 1209 | disableToolCallResolution: true |
| 1210 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1211 | |
| 1212 | const requests = yield* MockHttpClient.requests |
| 1213 | const body = yield* getRequestBody(requests[0]) |
| 1214 | const outputs = body.input.filter((item: any) => |
| 1215 | item.call_id === "call_local_shell" && item.type.endsWith("_output") |
| 1216 | ) |
| 1217 | |
| 1218 | deepStrictEqual(outputs.map((item: any) => item.type), ["local_shell_call_output"]) |
| 1219 | }).pipe(Effect.provide(makeTestLayer()))) |
| 1220 | }) |
| 1221 | }) |
| 1222 | |
| 1223 | describe("tool preparation", () => { |
| 1224 | it.effect("converts user-defined tools to function type", () => |
| 1225 | Effect.gen(function*() { |
| 1226 | yield* LanguageModel.generateText({ |
| 1227 | prompt: "Use the tool", |
| 1228 | toolkit: TestToolkit |
| 1229 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1230 | |
| 1231 | const requests = yield* MockHttpClient.requests |
| 1232 | const body = yield* getRequestBody(requests[0]) |
| 1233 | |
| 1234 | const tool = body.tools?.find((t: any) => t.type === "function") |
| 1235 | assert.isDefined(tool) |
| 1236 | strictEqual(tool.name, "TestTool") |
| 1237 | strictEqual(tool.description, "A test tool") |
| 1238 | strictEqual(tool.strict, true) |
| 1239 | }).pipe(Effect.provide([makeTestLayer(), TestToolkitLayer]))) |
| 1240 | |
| 1241 | it.effect("empty object on properties for empty parameters", () => |
| 1242 | Effect.gen(function*() { |
| 1243 | const EmptyTool = Tool.make("EmptyParamsTool", { |
| 1244 | description: "Empty params tool", |
| 1245 | parameters: Tool.EmptyParams, |
| 1246 | success: Schema.String |
| 1247 | }) |
| 1248 | const toolkit = Toolkit.make(EmptyTool) |
| 1249 | const toolkitLayer = toolkit.toLayer({ |
| 1250 | EmptyParamsTool: () => Effect.succeed("ok") |
| 1251 | }) |
| 1252 | |
| 1253 | yield* LanguageModel.generateText({ |
| 1254 | prompt: "Use the tool", |
| 1255 | toolkit |
| 1256 | }).pipe(Effect.provide([OpenAiLanguageModel.model("gpt-4o-mini"), toolkitLayer])) |
| 1257 | |
| 1258 | const requests = yield* MockHttpClient.requests |
| 1259 | const body = yield* getRequestBody(requests[0]) |
| 1260 | |
| 1261 | const tool = body.tools?.find((t: any) => t.type === "function" && t.name === "EmptyParamsTool") |
| 1262 | assert.isDefined(tool) |
| 1263 | deepStrictEqual(tool.parameters, { |
| 1264 | type: "object", |
| 1265 | properties: {}, |
| 1266 | additionalProperties: false |
| 1267 | }) |
| 1268 | }).pipe(Effect.provide(makeTestLayer()))) |
| 1269 | |
| 1270 | it.effect("converts dynamic tools to function type", () => |
| 1271 | Effect.gen(function*() { |
| 1272 | const inputSchema = { |
| 1273 | type: "object", |
| 1274 | properties: { |
| 1275 | query: { type: "string" }, |
| 1276 | limit: { type: "number" } |
| 1277 | }, |
| 1278 | required: ["query"], |
| 1279 | additionalProperties: false |
| 1280 | } as const |
| 1281 | |
| 1282 | const DynamicTool = Tool.dynamic("DynamicTool", { |
| 1283 | description: "A dynamic tool", |
| 1284 | parameters: inputSchema |
| 1285 | }) |
| 1286 | |
| 1287 | yield* LanguageModel.generateText({ |
| 1288 | prompt: "Use the dynamic tool", |
| 1289 | toolkit: Toolkit.make(DynamicTool), |
| 1290 | disableToolCallResolution: true |
| 1291 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1292 | |
| 1293 | const requests = yield* MockHttpClient.requests |
| 1294 | const body = yield* getRequestBody(requests[0]) |
| 1295 | |
| 1296 | const tool = body.tools?.find((entry: any) => entry.type === "function" && entry.name === "DynamicTool") |
| 1297 | assert.isDefined(tool) |
| 1298 | strictEqual(tool.description, "A dynamic tool") |
| 1299 | deepStrictEqual(tool.parameters, inputSchema) |
| 1300 | }).pipe(Effect.provide(makeTestLayer()))) |
| 1301 | |
| 1302 | it.effect("names the tool when its parameters are not an object schema", () => |
| 1303 | Effect.gen(function*() { |
| 1304 | const error = yield* LanguageModel.generateText({ |
| 1305 | prompt: "Use the tool", |
| 1306 | toolkit: Toolkit.make(Tool.make("NoParams", { parameters: Schema.Struct({}) })), |
| 1307 | disableToolCallResolution: true |
| 1308 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), Effect.flip) |
| 1309 | |
| 1310 | strictEqual(error.reason._tag, "UnsupportedSchemaError") |
| 1311 | assert.include(error.message, `"NoParams"`) |
| 1312 | assert.include(error.message, "Tool.EmptyParams") |
| 1313 | }).pipe(Effect.provide(makeTestLayer()))) |
| 1314 | |
| 1315 | it.effect("empty object on properties for empty parameters", () => |
| 1316 | Effect.gen(function*() { |
| 1317 | const EmptyTool = Tool.make("EmptyParamsTool", { |
| 1318 | description: "Empty params tool", |
| 1319 | parameters: Tool.EmptyParams, |
| 1320 | success: Schema.String |
| 1321 | }) |
| 1322 | const toolkit = Toolkit.make(EmptyTool) |
| 1323 | const toolkitLayer = toolkit.toLayer({ |
| 1324 | EmptyParamsTool: () => Effect.succeed("ok") |
| 1325 | }) |
| 1326 | |
| 1327 | yield* LanguageModel.generateText({ |
| 1328 | prompt: "Use the tool", |
| 1329 | toolkit |
| 1330 | }).pipe(Effect.provide([OpenAiLanguageModel.model("gpt-4o-mini"), toolkitLayer])) |
| 1331 | |
| 1332 | const requests = yield* MockHttpClient.requests |
| 1333 | const body = yield* getRequestBody(requests[0]) |
| 1334 | |
| 1335 | const tool = body.tools?.find((t: any) => t.type === "function" && t.name === "EmptyParamsTool") |
| 1336 | assert.isDefined(tool) |
| 1337 | deepStrictEqual(tool.parameters, { |
| 1338 | type: "object", |
| 1339 | properties: {}, |
| 1340 | additionalProperties: false |
| 1341 | }) |
| 1342 | }).pipe(Effect.provide(makeTestLayer()))) |
| 1343 | |
| 1344 | it.effect("handles tool choice auto", () => |
| 1345 | Effect.gen(function*() { |
| 1346 | yield* LanguageModel.generateText({ |
| 1347 | prompt: "Use the tool", |
| 1348 | toolkit: TestToolkit, |
| 1349 | toolChoice: "auto" |
| 1350 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1351 | |
| 1352 | const requests = yield* MockHttpClient.requests |
| 1353 | const body = yield* getRequestBody(requests[0]) |
| 1354 | |
| 1355 | strictEqual(body.tool_choice, "auto") |
| 1356 | }).pipe(Effect.provide([makeTestLayer(), TestToolkitLayer]))) |
| 1357 | |
| 1358 | it.effect("handles tool choice none", () => |
| 1359 | Effect.gen(function*() { |
| 1360 | yield* LanguageModel.generateText({ |
| 1361 | prompt: "Use the tool", |
| 1362 | toolkit: TestToolkit, |
| 1363 | toolChoice: "none" |
| 1364 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1365 | |
| 1366 | const requests = yield* MockHttpClient.requests |
| 1367 | const body = yield* getRequestBody(requests[0]) |
| 1368 | |
| 1369 | strictEqual(body.tool_choice, "none") |
| 1370 | }).pipe(Effect.provide([makeTestLayer(), TestToolkitLayer]))) |
| 1371 | |
| 1372 | it.effect("handles tool choice required", () => |
| 1373 | Effect.gen(function*() { |
| 1374 | yield* LanguageModel.generateText({ |
| 1375 | prompt: "Use the tool", |
| 1376 | toolkit: TestToolkit, |
| 1377 | toolChoice: "required" |
| 1378 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1379 | |
| 1380 | const requests = yield* MockHttpClient.requests |
| 1381 | const body = yield* getRequestBody(requests[0]) |
| 1382 | |
| 1383 | strictEqual(body.tool_choice, "required") |
| 1384 | }).pipe(Effect.provide([makeTestLayer(), TestToolkitLayer]))) |
| 1385 | |
| 1386 | it.effect("handles specific tool choice", () => |
| 1387 | Effect.gen(function*() { |
| 1388 | yield* LanguageModel.generateText({ |
| 1389 | prompt: "Use the tool", |
| 1390 | toolkit: TestToolkit, |
| 1391 | toolChoice: { tool: "TestTool" } |
| 1392 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1393 | |
| 1394 | const requests = yield* MockHttpClient.requests |
| 1395 | const body = yield* getRequestBody(requests[0]) |
| 1396 | |
| 1397 | deepStrictEqual(body.tool_choice, { type: "function", name: "TestTool" }) |
| 1398 | }).pipe(Effect.provide([makeTestLayer(), TestToolkitLayer]))) |
| 1399 | |
| 1400 | it.effect("adds code_interpreter tool", () => |
| 1401 | Effect.gen(function*() { |
| 1402 | const toolkit = Toolkit.make(OpenAiTool.CodeInterpreter({ container: { type: "auto" } })) |
| 1403 | |
| 1404 | yield* LanguageModel.generateText({ |
| 1405 | prompt: "Run some code", |
| 1406 | toolkit |
| 1407 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1408 | |
| 1409 | const requests = yield* MockHttpClient.requests |
| 1410 | const body = yield* getRequestBody(requests[0]) |
| 1411 | |
| 1412 | const tool = body.tools?.find((t: any) => t.type === "code_interpreter") |
| 1413 | assert.isDefined(tool) |
| 1414 | }).pipe(Effect.provide(makeTestLayer()))) |
| 1415 | |
| 1416 | it.effect("adds web_search tool", () => |
| 1417 | Effect.gen(function*() { |
| 1418 | const toolkit = Toolkit.make(OpenAiTool.WebSearch({})) |
| 1419 | |
| 1420 | yield* LanguageModel.generateText({ |
| 1421 | prompt: "Search the web", |
| 1422 | toolkit |
| 1423 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1424 | |
| 1425 | const requests = yield* MockHttpClient.requests |
| 1426 | const body = yield* getRequestBody(requests[0]) |
| 1427 | |
| 1428 | const tool = body.tools?.find((t: any) => t.type === "web_search") |
| 1429 | assert.isDefined(tool) |
| 1430 | }).pipe(Effect.provide(makeTestLayer()))) |
| 1431 | |
| 1432 | it.effect("adds file_search tool with vector store IDs", () => |
| 1433 | Effect.gen(function*() { |
| 1434 | const toolkit = Toolkit.make(OpenAiTool.FileSearch({ |
| 1435 | vector_store_ids: ["vs_123"] |
| 1436 | })) |
| 1437 | |
| 1438 | yield* LanguageModel.generateText({ |
| 1439 | prompt: "Search files", |
| 1440 | toolkit |
| 1441 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1442 | |
| 1443 | const requests = yield* MockHttpClient.requests |
| 1444 | const body = yield* getRequestBody(requests[0]) |
| 1445 | |
| 1446 | const tool = body.tools?.find((t: any) => t.type === "file_search") |
| 1447 | assert.isDefined(tool) |
| 1448 | deepStrictEqual(tool.vector_store_ids, ["vs_123"]) |
| 1449 | }).pipe(Effect.provide(makeTestLayer()))) |
| 1450 | }) |
| 1451 | |
| 1452 | describe("response format", () => { |
| 1453 | it.effect("uses text format by default", () => |
| 1454 | Effect.gen(function*() { |
| 1455 | yield* LanguageModel.generateText({ |
| 1456 | prompt: "Hello" |
| 1457 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1458 | |
| 1459 | const requests = yield* MockHttpClient.requests |
| 1460 | const body = yield* getRequestBody(requests[0]) |
| 1461 | |
| 1462 | strictEqual(body.text?.format?.type, "text") |
| 1463 | }).pipe(Effect.provide(makeTestLayer()))) |
| 1464 | |
| 1465 | it.effect("uses json_schema format for structured output", () => |
| 1466 | Effect.gen(function*() { |
| 1467 | yield* LanguageModel.generateObject({ |
| 1468 | prompt: "Give me a person", |
| 1469 | schema: Schema.Struct({ |
| 1470 | name: Schema.String, |
| 1471 | age: Schema.Number |
| 1472 | }) |
| 1473 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1474 | |
| 1475 | const requests = yield* MockHttpClient.requests |
| 1476 | const body = yield* getRequestBody(requests[0]) |
| 1477 | |
| 1478 | strictEqual(body.text?.format?.type, "json_schema") |
| 1479 | strictEqual(body.text?.format?.strict, true) |
| 1480 | }).pipe(Effect.provide(makeTestLayer({ |
| 1481 | body: { |
| 1482 | output: [makeTextOutput(JSON.stringify({ name: "John", age: 30 }))] |
| 1483 | } |
| 1484 | })))) |
| 1485 | }) |
| 1486 | |
| 1487 | describe("response handling", () => { |
| 1488 | it.effect("preserves ultrafast service tier alongside prompt cache diagnostics", () => |
| 1489 | Effect.gen(function*() { |
| 1490 | const diagnostics = { |
| 1491 | type: "cache_miss", |
| 1492 | reason: "tools_changed", |
| 1493 | cache_missed_tokens: 1024, |
| 1494 | comparison_reusable_tokens: 2048 |
| 1495 | } as const |
| 1496 | const body = JSON.stringify({ |
| 1497 | ...makeDefaultResponse({ service_tier: "ultrafast" }), |
| 1498 | prompt_cache_diagnostics: diagnostics |
| 1499 | }) |
| 1500 | const result = yield* LanguageModel.generateText({ prompt: "test" }).pipe( |
| 1501 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1502 | Effect.provide(OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") })), |
| 1503 | Effect.provideService(HttpClient.HttpClient, makeRawResponseClient(body, "application/json")) |
| 1504 | ) |
| 1505 | |
| 1506 | const finish = result.content.find((part) => part.type === "finish") |
| 1507 | assert.isDefined(finish) |
| 1508 | deepStrictEqual(finish.metadata.openai, { |
| 1509 | serviceTier: "ultrafast", |
| 1510 | promptCacheDiagnostics: diagnostics |
| 1511 | }) |
| 1512 | })) |
| 1513 | |
| 1514 | it.effect("extracts text from output_text", () => |
| 1515 | Effect.gen(function*() { |
| 1516 | const result = yield* LanguageModel.generateText({ |
| 1517 | prompt: "Hello" |
| 1518 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1519 | |
| 1520 | strictEqual(result.text, "Hello, world!") |
| 1521 | }).pipe(Effect.provide(makeTestLayer({ |
| 1522 | body: { output: [makeTextOutput("Hello, world!")] } |
| 1523 | })))) |
| 1524 | |
| 1525 | it.effect("extracts multiple text parts", () => |
| 1526 | Effect.gen(function*() { |
| 1527 | const result = yield* LanguageModel.generateText({ |
| 1528 | prompt: "Hello" |
| 1529 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1530 | |
| 1531 | const textParts = result.content.filter((p) => p.type === "text") |
| 1532 | strictEqual(textParts.length, 2) |
| 1533 | }).pipe(Effect.provide(makeTestLayer({ |
| 1534 | body: { |
| 1535 | output: [ |
| 1536 | makeTextOutput("First"), |
| 1537 | makeTextOutput("Second", { id: "msg_456" }) |
| 1538 | ] |
| 1539 | } |
| 1540 | })))) |
| 1541 | |
| 1542 | it.effect("handles refusal content", () => |
| 1543 | Effect.gen(function*() { |
| 1544 | const result = yield* LanguageModel.generateText({ |
| 1545 | prompt: "Do something bad" |
| 1546 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1547 | |
| 1548 | const textPart = result.content.find((p) => p.type === "text") |
| 1549 | strictEqual(textPart?.text, "") |
| 1550 | strictEqual(textPart?.metadata?.openai?.refusal, "I cannot do that") |
| 1551 | }).pipe(Effect.provide(makeTestLayer({ |
| 1552 | body: { |
| 1553 | output: [{ |
| 1554 | type: "message", |
| 1555 | id: "msg_123", |
| 1556 | role: "assistant", |
| 1557 | status: "completed", |
| 1558 | content: [{ type: "refusal", refusal: "I cannot do that" }] |
| 1559 | }] |
| 1560 | } |
| 1561 | })))) |
| 1562 | |
| 1563 | it.effect("parses function call arguments", () => |
| 1564 | Effect.gen(function*() { |
| 1565 | const result = yield* LanguageModel.generateText({ |
| 1566 | prompt: "Use the tool", |
| 1567 | toolkit: TestToolkit |
| 1568 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1569 | |
| 1570 | const toolCall = result.content.find((p) => p.type === "tool-call") |
| 1571 | assert.isDefined(toolCall) |
| 1572 | if (toolCall?.type === "tool-call") { |
| 1573 | strictEqual(toolCall.name, "TestTool") |
| 1574 | deepStrictEqual(toolCall.params, { input: "hello" }) |
| 1575 | } |
| 1576 | }).pipe( |
| 1577 | Effect.provide([ |
| 1578 | makeTestLayer({ |
| 1579 | body: { output: [makeFunctionCall("TestTool", { input: "hello" })] } |
| 1580 | }), |
| 1581 | TestToolkitLayer |
| 1582 | ]) |
| 1583 | )) |
| 1584 | |
| 1585 | it.effect("preserves raw JSON Schema dynamic tool call params", () => |
| 1586 | Effect.gen(function*() { |
| 1587 | const DynamicTool = Tool.dynamic("DynamicTool", { |
| 1588 | parameters: { |
| 1589 | type: "object", |
| 1590 | properties: { query: { type: "string" } }, |
| 1591 | required: ["query"], |
| 1592 | additionalProperties: false |
| 1593 | } as const |
| 1594 | }) |
| 1595 | const params = { query: "effect" } |
| 1596 | const result = yield* LanguageModel.generateText({ |
| 1597 | prompt: "Use the dynamic tool", |
| 1598 | toolkit: Toolkit.make(DynamicTool), |
| 1599 | disableToolCallResolution: true |
| 1600 | }).pipe( |
| 1601 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1602 | Effect.provide(makeTestLayer({ |
| 1603 | body: { output: [makeFunctionCall("DynamicTool", params)] } |
| 1604 | })) |
| 1605 | ) |
| 1606 | |
| 1607 | deepStrictEqual(result.toolCalls[0]?.params, params) |
| 1608 | })) |
| 1609 | |
| 1610 | it.effect("decodes Effect Schema dynamic tool call params with the OpenAI codec", () => |
| 1611 | Effect.gen(function*() { |
| 1612 | const DynamicTool = Tool.dynamic("DynamicTool", { |
| 1613 | parameters: Schema.Struct({ |
| 1614 | env: Schema.Record(Schema.String, Schema.String) |
| 1615 | }) |
| 1616 | }) |
| 1617 | const result = yield* LanguageModel.generateText({ |
| 1618 | prompt: "Use the dynamic tool", |
| 1619 | toolkit: Toolkit.make(DynamicTool), |
| 1620 | disableToolCallResolution: true |
| 1621 | }).pipe( |
| 1622 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1623 | Effect.provide(makeTestLayer({ |
| 1624 | body: { |
| 1625 | output: [makeFunctionCall("DynamicTool", { env: [{ 0: "PATH", 1: "/usr/bin" }] })] |
| 1626 | } |
| 1627 | })) |
| 1628 | ) |
| 1629 | |
| 1630 | deepStrictEqual(result.toolCalls[0]?.params, { env: { PATH: "/usr/bin" } }) |
| 1631 | })) |
| 1632 | |
| 1633 | it.effect("routes invalid tool call params through failureMode: return without failing the effect", () => |
| 1634 | Effect.gen(function*() { |
| 1635 | const toolkit = Toolkit.make(ReturnModeTool) |
| 1636 | const handlers = toolkit.toLayer({ |
| 1637 | ReturnModeTool: ({ input }) => Effect.succeed({ output: `processed: ${input}` }) |
| 1638 | }) |
| 1639 | |
| 1640 | const result = yield* LanguageModel.generateText({ |
| 1641 | prompt: "Use the tool", |
| 1642 | toolkit |
| 1643 | }).pipe( |
| 1644 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1645 | Effect.provide(handlers) |
| 1646 | ) |
| 1647 | |
| 1648 | strictEqual(result.toolResults.length, 1) |
| 1649 | const toolResult = result.toolResults[0]! |
| 1650 | strictEqual(toolResult.isFailure, true) |
| 1651 | const failure = toolResult.result as AiError.AiError |
| 1652 | strictEqual(failure._tag, "AiError") |
| 1653 | strictEqual(failure.reason._tag, "ToolParameterValidationError") |
| 1654 | }).pipe( |
| 1655 | Effect.provide(makeTestLayer({ |
| 1656 | body: { output: [makeFunctionCall("ReturnModeTool", { input: 123 })] } |
| 1657 | })) |
| 1658 | )) |
| 1659 | |
| 1660 | it.effect("converts transformed tool call params to the tool's standard encoded form", () => |
| 1661 | Effect.gen(function*() { |
| 1662 | const received = yield* Ref.make<number | undefined>(undefined) |
| 1663 | const toolkit = Toolkit.make(TransformParamsTool) |
| 1664 | const handlers = toolkit.toLayer({ |
| 1665 | TransformParamsTool: ({ input }) => |
| 1666 | Ref.set(received, input).pipe( |
| 1667 | Effect.as({ output: input * 2 }) |
| 1668 | ) |
| 1669 | }) |
| 1670 | |
| 1671 | const result = yield* LanguageModel.generateText({ |
| 1672 | prompt: "Use the tool", |
| 1673 | toolkit |
| 1674 | }).pipe( |
| 1675 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1676 | Effect.provide(handlers) |
| 1677 | ) |
| 1678 | |
| 1679 | const toolCall = result.toolCalls[0]! |
| 1680 | deepStrictEqual(toolCall.params, { input: "21" }) |
| 1681 | strictEqual(yield* Ref.get(received), 21) |
| 1682 | strictEqual(result.toolResults[0]!.isFailure, false) |
| 1683 | deepStrictEqual(result.toolResults[0]!.result, { output: 42 }) |
| 1684 | }).pipe( |
| 1685 | Effect.provide(makeTestLayer({ |
| 1686 | body: { output: [makeFunctionCall("TransformParamsTool", { input: "21" })] } |
| 1687 | })) |
| 1688 | )) |
| 1689 | |
| 1690 | it.effect("provides parameter encoding services to provider normalization", () => |
| 1691 | Effect.gen(function*() { |
| 1692 | const used = yield* Ref.make(false) |
| 1693 | const toolkit = Toolkit.make(AsymmetricParamsTool) |
| 1694 | const handlers = toolkit.toLayer({ |
| 1695 | AsymmetricParamsTool: ({ input }) => Effect.succeed({ output: `processed: ${input}` }) |
| 1696 | }) |
| 1697 | |
| 1698 | const result = yield* LanguageModel.generateText({ |
| 1699 | prompt: "Use the tool", |
| 1700 | toolkit |
| 1701 | }).pipe( |
| 1702 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1703 | Effect.provide(handlers), |
| 1704 | Effect.provideService(ParamEncodeService, { use: Ref.set(used, true) }) |
| 1705 | ) |
| 1706 | |
| 1707 | strictEqual(yield* Ref.get(used), true) |
| 1708 | strictEqual(result.toolResults[0]!.isFailure, false) |
| 1709 | deepStrictEqual(result.toolResults[0]!.result, { output: "processed: hello" }) |
| 1710 | }).pipe( |
| 1711 | Effect.provide(makeTestLayer({ |
| 1712 | body: { output: [makeFunctionCall("AsymmetricParamsTool", { input: "hello" })] } |
| 1713 | })) |
| 1714 | )) |
| 1715 | |
| 1716 | it.effect("provides parameter encoding services to provider normalization when tool call resolution is disabled", () => |
| 1717 | Effect.gen(function*() { |
| 1718 | const used = yield* Ref.make(false) |
| 1719 | const toolkit = Toolkit.make(AsymmetricParamsTool) |
| 1720 | |
| 1721 | const result = yield* LanguageModel.generateText({ |
| 1722 | prompt: "Use the tool", |
| 1723 | toolkit, |
| 1724 | disableToolCallResolution: true |
| 1725 | }).pipe( |
| 1726 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1727 | Effect.provideService(ParamEncodeService, { use: Ref.set(used, true) }) |
| 1728 | ) |
| 1729 | |
| 1730 | strictEqual(yield* Ref.get(used), true) |
| 1731 | const toolCall = result.toolCalls[0]! |
| 1732 | deepStrictEqual(toolCall.params, { input: "hello" }) |
| 1733 | }).pipe( |
| 1734 | Effect.provide(makeTestLayer({ |
| 1735 | body: { output: [makeFunctionCall("AsymmetricParamsTool", { input: "hello" })] } |
| 1736 | })) |
| 1737 | )) |
| 1738 | |
| 1739 | it.effect("uses canonical OpenAiMcp name for mcp_call", () => |
| 1740 | Effect.gen(function*() { |
| 1741 | const result = yield* LanguageModel.generateText({ |
| 1742 | prompt: "Use MCP", |
| 1743 | toolkit: McpToolkit |
| 1744 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1745 | |
| 1746 | const toolCall = result.content.find((part) => part.type === "tool-call") |
| 1747 | assert.isDefined(toolCall) |
| 1748 | if (toolCall?.type === "tool-call") { |
| 1749 | strictEqual(toolCall.name, "OpenAiMcp") |
| 1750 | deepStrictEqual(toolCall.params, { packageName: "effect" }) |
| 1751 | } |
| 1752 | |
| 1753 | const toolResult = result.content.find((part) => part.type === "tool-result") |
| 1754 | assert.isDefined(toolResult) |
| 1755 | if (toolResult?.type === "tool-result") { |
| 1756 | strictEqual(toolResult.name, "OpenAiMcp") |
| 1757 | assertTrue(!toolResult.isFailure, "expected a successful MCP result") |
| 1758 | strictEqual(toolResult.result.name, "CheckPackage") |
| 1759 | } |
| 1760 | }).pipe(Effect.provide(makeTestLayer({ |
| 1761 | body: { |
| 1762 | output: [makeMcpCall("CheckPackage", { packageName: "effect" })] |
| 1763 | } |
| 1764 | })))) |
| 1765 | |
| 1766 | it.each(["gpt-4.1", "gpt-5.6"] as const)( |
| 1767 | "maps stable web search action to tool call parameters with %s", |
| 1768 | (model) => |
| 1769 | Effect.runPromise( |
| 1770 | Effect.gen(function*() { |
| 1771 | const toolkit = Toolkit.make(OpenAiTool.WebSearch({})) |
| 1772 | const result = yield* LanguageModel.generateText({ |
| 1773 | prompt: "Search the web", |
| 1774 | toolkit |
| 1775 | }).pipe(Effect.provide(OpenAiLanguageModel.model(model))) |
| 1776 | |
| 1777 | const toolCall = result.content.find((part) => part.type === "tool-call") |
| 1778 | assert.isDefined(toolCall) |
| 1779 | assert.deepStrictEqual(toolCall.params, { |
| 1780 | action: { type: "search", query: "Effect TypeScript" } |
| 1781 | }) |
| 1782 | |
| 1783 | const toolResult = result.content.find((part) => part.type === "tool-result") |
| 1784 | assert.isDefined(toolResult) |
| 1785 | assert.deepStrictEqual(toolResult.result, { |
| 1786 | action: { type: "search", query: "Effect TypeScript" }, |
| 1787 | status: "completed" |
| 1788 | }) |
| 1789 | }).pipe(Effect.provide(makeTestLayer({ |
| 1790 | body: { |
| 1791 | model, |
| 1792 | output: [makeWebSearchCall()] |
| 1793 | } |
| 1794 | }))) |
| 1795 | ) |
| 1796 | ) |
| 1797 | |
| 1798 | for (const { tool, status } of webSearchFailures) { |
| 1799 | it.effect( |
| 1800 | `${tool.name} preserves ${status} results`, |
| 1801 | () => |
| 1802 | Effect.gen(function*() { |
| 1803 | const result = yield* LanguageModel.generateText({ |
| 1804 | prompt: "Search the web", |
| 1805 | toolkit: Toolkit.make(tool) |
| 1806 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1807 | const toolResult = result.toolResults[0]! |
| 1808 | strictEqual(toolResult.isFailure, true) |
| 1809 | deepStrictEqual(toolResult.result, { |
| 1810 | action: { type: "search", query: "Effect TypeScript" }, |
| 1811 | status |
| 1812 | }) |
| 1813 | }).pipe(Effect.provide(makeTestLayer({ |
| 1814 | body: { output: [makeWebSearchCall({ status })] } |
| 1815 | }))) |
| 1816 | ) |
| 1817 | } |
| 1818 | |
| 1819 | it.effect.each(fileSearchOutcomes)( |
| 1820 | "OpenAiFileSearch reports $status results", |
| 1821 | ({ status, isFailure, results }) => |
| 1822 | Effect.gen(function*() { |
| 1823 | const result = yield* LanguageModel.generateText({ |
| 1824 | prompt: "Search the files", |
| 1825 | toolkit: Toolkit.make(OpenAiTool.FileSearch({ vector_store_ids: ["vs_123"] })) |
| 1826 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1827 | const toolResult = result.toolResults[0]! |
| 1828 | strictEqual(toolResult.isFailure, isFailure) |
| 1829 | deepStrictEqual(toolResult.result, { |
| 1830 | status, |
| 1831 | queries: ["Effect TypeScript"], |
| 1832 | results: results ?? null |
| 1833 | }) |
| 1834 | }).pipe(Effect.provide(makeTestLayer({ |
| 1835 | body: { output: [makeFileSearchCall({ status, ...(results === undefined ? {} : { results }) })] } |
| 1836 | }))) |
| 1837 | ) |
| 1838 | |
| 1839 | it.effect("uses canonical OpenAiMcp name for mcp_approval_request", () => |
| 1840 | Effect.gen(function*() { |
| 1841 | const result = yield* LanguageModel.generateText({ |
| 1842 | prompt: "Use MCP", |
| 1843 | toolkit: McpToolkit |
| 1844 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1845 | |
| 1846 | const toolCall = result.content.find((part) => part.type === "tool-call") |
| 1847 | assert.isDefined(toolCall) |
| 1848 | if (toolCall?.type === "tool-call") { |
| 1849 | strictEqual(toolCall.name, "OpenAiMcp") |
| 1850 | deepStrictEqual(toolCall.params, { packageName: "effect" }) |
| 1851 | } |
| 1852 | |
| 1853 | const approvalRequest = result.content.find((part) => part.type === "tool-approval-request") |
| 1854 | assert.isDefined(approvalRequest) |
| 1855 | if (toolCall?.type === "tool-call" && approvalRequest?.type === "tool-approval-request") { |
| 1856 | strictEqual(approvalRequest.toolCallId, toolCall.id) |
| 1857 | } |
| 1858 | }).pipe(Effect.provide(makeTestLayer({ |
| 1859 | body: { |
| 1860 | output: [makeMcpApprovalRequest("CheckPackage", { packageName: "effect" })] |
| 1861 | } |
| 1862 | })))) |
| 1863 | |
| 1864 | it.effect("extracts reasoning parts", () => |
| 1865 | Effect.gen(function*() { |
| 1866 | const result = yield* LanguageModel.generateText({ |
| 1867 | prompt: "Think about this" |
| 1868 | }).pipe(Effect.provide(OpenAiLanguageModel.model("o1"))) |
| 1869 | |
| 1870 | const reasoningParts = result.content.filter((p) => p.type === "reasoning") |
| 1871 | strictEqual(reasoningParts.length, 2) |
| 1872 | if (reasoningParts[0]?.type === "reasoning") { |
| 1873 | strictEqual(reasoningParts[0].text, "First thought") |
| 1874 | } |
| 1875 | }).pipe(Effect.provide(makeTestLayer({ |
| 1876 | body: { |
| 1877 | model: "o1", |
| 1878 | output: [makeReasoningOutput(["First thought", "Second thought"])] |
| 1879 | } |
| 1880 | })))) |
| 1881 | |
| 1882 | it.effect("extracts usage information", () => |
| 1883 | Effect.gen(function*() { |
| 1884 | const result = yield* LanguageModel.generateText({ |
| 1885 | prompt: "Hello" |
| 1886 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1887 | |
| 1888 | const finishPart = result.content.find((p) => p.type === "finish") |
| 1889 | assert.isDefined(finishPart) |
| 1890 | if (finishPart?.type === "finish") { |
| 1891 | deepStrictEqual(finishPart.usage.inputTokens, { |
| 1892 | uncached: 7, |
| 1893 | total: 10, |
| 1894 | cacheRead: 3, |
| 1895 | cacheWrite: 4 |
| 1896 | }) |
| 1897 | deepStrictEqual(finishPart.usage.outputTokens, { total: 20, text: 20, reasoning: 0 }) |
| 1898 | } |
| 1899 | }).pipe(Effect.provide(makeTestLayer({ |
| 1900 | body: { |
| 1901 | output: [makeTextOutput("Hello")], |
| 1902 | usage: makeUsage({ |
| 1903 | input_tokens_details: { |
| 1904 | cached_tokens: 3, |
| 1905 | cache_write_tokens: 4 |
| 1906 | } |
| 1907 | }) |
| 1908 | } |
| 1909 | })))) |
| 1910 | |
| 1911 | it.effect("determines finish reason from incomplete_details", () => |
| 1912 | Effect.gen(function*() { |
| 1913 | const result = yield* LanguageModel.generateText({ |
| 1914 | prompt: "Hello" |
| 1915 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1916 | |
| 1917 | const finishPart = result.content.find((p) => p.type === "finish") |
| 1918 | if (finishPart?.type === "finish") { |
| 1919 | strictEqual(finishPart.reason, "content-filter") |
| 1920 | } |
| 1921 | }).pipe(Effect.provide(makeTestLayer({ |
| 1922 | body: { |
| 1923 | output: [makeTextOutput("Hello")], |
| 1924 | incomplete_details: { reason: "content_filter" } |
| 1925 | } |
| 1926 | })))) |
| 1927 | |
| 1928 | it.effect("defaults finish reason to stop", () => |
| 1929 | Effect.gen(function*() { |
| 1930 | const result = yield* LanguageModel.generateText({ |
| 1931 | prompt: "Hello" |
| 1932 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1933 | |
| 1934 | const finishPart = result.content.find((p) => p.type === "finish") |
| 1935 | if (finishPart?.type === "finish") { |
| 1936 | strictEqual(finishPart.reason, "stop") |
| 1937 | } |
| 1938 | }).pipe(Effect.provide(makeTestLayer({ |
| 1939 | body: { output: [makeTextOutput("Hello")] } |
| 1940 | })))) |
| 1941 | |
| 1942 | it.effect("sets finish reason to tool-calls when has tool calls", () => |
| 1943 | Effect.gen(function*() { |
| 1944 | const result = yield* LanguageModel.generateText({ |
| 1945 | prompt: "Use the tool", |
| 1946 | toolkit: TestToolkit |
| 1947 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1948 | |
| 1949 | const finishPart = result.content.find((p) => p.type === "finish") |
| 1950 | if (finishPart?.type === "finish") { |
| 1951 | strictEqual(finishPart.reason, "tool-calls") |
| 1952 | } |
| 1953 | }).pipe( |
| 1954 | Effect.provide([ |
| 1955 | makeTestLayer({ |
| 1956 | body: { output: [makeFunctionCall("TestTool", { input: "test" })] } |
| 1957 | }), |
| 1958 | TestToolkitLayer |
| 1959 | ]) |
| 1960 | )) |
| 1961 | |
| 1962 | it.effect("reports length and interrupts tool calls when max_output_tokens truncates the response", () => |
| 1963 | Effect.gen(function*() { |
| 1964 | const result = yield* LanguageModel.generateText({ |
| 1965 | prompt: "Use the tool", |
| 1966 | toolkit: TestToolkit |
| 1967 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1968 | |
| 1969 | strictEqual(result.finishReason, "length") |
| 1970 | deepStrictEqual(result.toolResults.map((part) => part.result), [{ |
| 1971 | type: "execution-interrupted", |
| 1972 | reason: "Tool call execution was interrupted because the response finished with reason \"length\"" |
| 1973 | }]) |
| 1974 | }).pipe( |
| 1975 | Effect.provide([ |
| 1976 | makeTestLayer({ |
| 1977 | body: { |
| 1978 | status: "incomplete", |
| 1979 | output: [makeFunctionCall("TestTool", { input: "test" })], |
| 1980 | incomplete_details: { reason: "max_output_tokens" } |
| 1981 | } |
| 1982 | }), |
| 1983 | TestToolkitLayer |
| 1984 | ]) |
| 1985 | )) |
| 1986 | |
| 1987 | it.effect("extracts url citations as source parts", () => |
| 1988 | Effect.gen(function*() { |
| 1989 | const result = yield* LanguageModel.generateText({ |
| 1990 | prompt: "Hello" |
| 1991 | }).pipe(Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini"))) |
| 1992 | |
| 1993 | const sourcePart = result.content.find((p) => p.type === "source") |
| 1994 | assert.isDefined(sourcePart) |
| 1995 | if (sourcePart?.type === "source" && sourcePart.sourceType === "url") { |
| 1996 | strictEqual(sourcePart.url.href, "https://example.com/") |
| 1997 | strictEqual(sourcePart.title, "Example") |
| 1998 | } |
| 1999 | }).pipe(Effect.provide(makeTestLayer({ |
| 2000 | body: { |
| 2001 | output: [{ |
| 2002 | type: "message", |
| 2003 | id: "msg_123", |
| 2004 | role: "assistant", |
| 2005 | status: "completed", |
| 2006 | content: [{ |
| 2007 | type: "output_text", |
| 2008 | text: "Check this out", |
| 2009 | annotations: [{ |
| 2010 | type: "url_citation", |
| 2011 | url: "https://example.com", |
| 2012 | title: "Example", |
| 2013 | start_index: 0, |
| 2014 | end_index: 14 |
| 2015 | }], |
| 2016 | logprobs: [] |
| 2017 | }] |
| 2018 | }] |
| 2019 | } |
| 2020 | })))) |
| 2021 | }) |
| 2022 | }) |
| 2023 | |
| 2024 | describe("streamText", () => { |
| 2025 | it.effect.each( |
| 2026 | [ |
| 2027 | { |
| 2028 | status: "completed", |
| 2029 | serviceTier: "fast", |
| 2030 | expectedMetadata: { promptCacheDiagnostics: { type: "cache_hit" } } |
| 2031 | }, |
| 2032 | { |
| 2033 | status: "completed", |
| 2034 | serviceTier: "ultrafast", |
| 2035 | expectedMetadata: { serviceTier: "ultrafast", promptCacheDiagnostics: { type: "cache_hit" } } |
| 2036 | }, |
| 2037 | { |
| 2038 | status: "failed", |
| 2039 | serviceTier: "ultrafast", |
| 2040 | expectedMetadata: { serviceTier: "ultrafast", promptCacheDiagnostics: { type: "cache_hit" } } |
| 2041 | } |
| 2042 | ] as const |
| 2043 | )( |
| 2044 | "preserves supported tiers and prompt cache diagnostics in response.$status with $serviceTier", |
| 2045 | ({ status, serviceTier, expectedMetadata }) => |
| 2046 | Effect.gen(function*() { |
| 2047 | const event = { |
| 2048 | type: `response.${status}`, |
| 2049 | sequence_number: 1, |
| 2050 | response: { |
| 2051 | ...makeDefaultResponse({ |
| 2052 | status, |
| 2053 | error: status === "failed" ? { code: "server_error", message: "generation failed" } : null |
| 2054 | }), |
| 2055 | service_tier: serviceTier, |
| 2056 | prompt_cache_diagnostics: { type: "cache_hit" } |
| 2057 | } |
| 2058 | } |
| 2059 | const parts = yield* LanguageModel.streamText({ prompt: "test" }).pipe( |
| 2060 | Stream.runCollect, |
| 2061 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 2062 | Effect.provide(OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") })), |
| 2063 | Effect.provideService( |
| 2064 | HttpClient.HttpClient, |
| 2065 | makeRawResponseClient( |
| 2066 | `data: ${JSON.stringify(event)}\n\n`, |
| 2067 | "text/event-stream" |
| 2068 | ) |
| 2069 | ) |
| 2070 | ) |
| 2071 | |
| 2072 | const finish = parts.find((part) => part.type === "finish") |
| 2073 | assert.isDefined(finish) |
| 2074 | deepStrictEqual(finish.metadata.openai, expectedMetadata) |
| 2075 | }) |
| 2076 | ) |
| 2077 | |
| 2078 | it.effect("extracts usage information", () => |
| 2079 | Effect.gen(function*() { |
| 2080 | const streamEvents = [ |
| 2081 | { |
| 2082 | type: "response.created", |
| 2083 | sequence_number: 1, |
| 2084 | response: makeDefaultResponse({ status: "in_progress" }) |
| 2085 | }, |
| 2086 | { |
| 2087 | type: "response.completed", |
| 2088 | sequence_number: 2, |
| 2089 | response: makeDefaultResponse({ |
| 2090 | usage: makeUsage({ |
| 2091 | input_tokens_details: { |
| 2092 | cached_tokens: 3, |
| 2093 | cache_write_tokens: 4 |
| 2094 | } |
| 2095 | }) |
| 2096 | }) |
| 2097 | } |
| 2098 | ] as unknown as ReadonlyArray<typeof Generated.ResponseStreamEvent.Type> |
| 2099 | |
| 2100 | const partsChunk = yield* LanguageModel.streamText({ |
| 2101 | prompt: "Hello" |
| 2102 | }).pipe( |
| 2103 | Stream.runCollect, |
| 2104 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 2105 | Effect.provide(makeStreamTestLayer(streamEvents)) |
| 2106 | ) |
| 2107 | |
| 2108 | const finishPart = globalThis.Array.from(partsChunk).find((part) => part.type === "finish") |
| 2109 | assert.isDefined(finishPart) |
| 2110 | if (finishPart?.type === "finish") { |
| 2111 | deepStrictEqual(finishPart.usage.inputTokens, { |
| 2112 | uncached: 7, |
| 2113 | total: 10, |
| 2114 | cacheRead: 3, |
| 2115 | cacheWrite: 4 |
| 2116 | }) |
| 2117 | } |
| 2118 | })) |
| 2119 | |
| 2120 | it.effect("emits valid apply_patch tool params JSON for update_file diffs", () => |
| 2121 | Effect.gen(function*() { |
| 2122 | const diff = "@@ -1 +1 @@\n-old\n+new\n" |
| 2123 | const outputItem = { |
| 2124 | type: "apply_patch_call", |
| 2125 | id: "patch_item_1", |
| 2126 | call_id: "patch_call_1", |
| 2127 | status: "in_progress", |
| 2128 | operation: { |
| 2129 | type: "update_file", |
| 2130 | path: "src/example.ts", |
| 2131 | diff |
| 2132 | } |
| 2133 | } as const |
| 2134 | |
| 2135 | const streamEvents = [ |
| 2136 | { |
| 2137 | type: "response.created", |
| 2138 | sequence_number: 1, |
| 2139 | response: makeDefaultResponse({ |
| 2140 | id: "resp_patch_stream", |
| 2141 | status: "in_progress", |
| 2142 | output: [] |
| 2143 | }) |
| 2144 | }, |
| 2145 | { |
| 2146 | type: "response.output_item.added", |
| 2147 | output_index: 0, |
| 2148 | sequence_number: 2, |
| 2149 | item: outputItem |
| 2150 | }, |
| 2151 | { |
| 2152 | type: "response.apply_patch_call_operation_diff.delta", |
| 2153 | sequence_number: 3, |
| 2154 | output_index: 0, |
| 2155 | item_id: outputItem.id, |
| 2156 | delta: diff |
| 2157 | }, |
| 2158 | { |
| 2159 | type: "response.apply_patch_call_operation_diff.done", |
| 2160 | sequence_number: 4, |
| 2161 | output_index: 0, |
| 2162 | item_id: outputItem.id |
| 2163 | } |
| 2164 | ] as unknown as ReadonlyArray<typeof Generated.ResponseStreamEvent.Type> |
| 2165 | |
| 2166 | const partsChunk = yield* LanguageModel.streamText({ |
| 2167 | prompt: "Update src/example.ts", |
| 2168 | disableToolCallResolution: true |
| 2169 | }).pipe( |
| 2170 | Stream.runCollect, |
| 2171 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 2172 | Effect.provide(makeStreamTestLayer(streamEvents)) |
| 2173 | ) |
| 2174 | |
| 2175 | const parts = globalThis.Array.from(partsChunk) |
| 2176 | const params = decodeToolParamsFromStream(parts, outputItem.call_id) |
| 2177 | |
| 2178 | deepStrictEqual(params, { |
| 2179 | call_id: outputItem.call_id, |
| 2180 | operation: { |
| 2181 | type: "update_file", |
| 2182 | path: "src/example.ts", |
| 2183 | diff |
| 2184 | } |
| 2185 | }) |
| 2186 | })) |
| 2187 | |
| 2188 | it.effect("emits tool call from function_call_arguments.done when output_item.done is missing", () => |
| 2189 | Effect.gen(function*() { |
| 2190 | const streamEvents = [ |
| 2191 | { |
| 2192 | type: "response.created", |
| 2193 | sequence_number: 1, |
| 2194 | response: makeDefaultResponse({ |
| 2195 | id: "resp_function_call_done", |
| 2196 | status: "in_progress", |
| 2197 | output: [] |
| 2198 | }) |
| 2199 | }, |
| 2200 | { |
| 2201 | type: "response.output_item.added", |
| 2202 | sequence_number: 2, |
| 2203 | output_index: 0, |
| 2204 | item: { |
| 2205 | type: "function_call", |
| 2206 | id: "fc_1", |
| 2207 | call_id: "call_1", |
| 2208 | name: "TestTool", |
| 2209 | arguments: "", |
| 2210 | status: "in_progress" |
| 2211 | } |
| 2212 | }, |
| 2213 | { |
| 2214 | type: "response.function_call_arguments.delta", |
| 2215 | sequence_number: 3, |
| 2216 | output_index: 0, |
| 2217 | item_id: "fc_1", |
| 2218 | delta: "{\"input\":\"hel" |
| 2219 | }, |
| 2220 | { |
| 2221 | type: "response.function_call_arguments.done", |
| 2222 | sequence_number: 4, |
| 2223 | output_index: 0, |
| 2224 | item_id: "fc_1", |
| 2225 | name: "TestTool", |
| 2226 | arguments: "{\"input\":\"hello\"}" |
| 2227 | }, |
| 2228 | { |
| 2229 | type: "response.completed", |
| 2230 | sequence_number: 5, |
| 2231 | response: makeDefaultResponse({ |
| 2232 | id: "resp_function_call_done", |
| 2233 | status: "completed", |
| 2234 | output: [] |
| 2235 | }) |
| 2236 | } |
| 2237 | ] as unknown as ReadonlyArray<typeof Generated.ResponseStreamEvent.Type> |
| 2238 | |
| 2239 | const partsChunk = yield* LanguageModel.streamText({ |
| 2240 | prompt: "Use the test tool", |
| 2241 | toolkit: TestToolkit, |
| 2242 | disableToolCallResolution: true |
| 2243 | }).pipe( |
| 2244 | Stream.runCollect, |
| 2245 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 2246 | Effect.provide(makeStreamTestLayer(streamEvents)), |
| 2247 | Effect.provide(TestToolkitLayer) |
| 2248 | ) |
| 2249 | |
| 2250 | const parts = globalThis.Array.from(partsChunk) |
| 2251 | const toolCalls = parts.filter((part) => part.type === "tool-call" && part.id === "call_1") |
| 2252 | strictEqual(toolCalls.length, 1) |
| 2253 | const toolCall = toolCalls[0] |
| 2254 | assert.isDefined(toolCall) |
| 2255 | if (toolCall?.type === "tool-call") { |
| 2256 | strictEqual(toolCall.name, "TestTool") |
| 2257 | deepStrictEqual(toolCall.params, { input: "hello" }) |
| 2258 | } |
| 2259 | |
| 2260 | const toolParamsEnd = parts.find((part) => part.type === "tool-params-end" && part.id === "call_1") |
| 2261 | assert.isDefined(toolParamsEnd) |
| 2262 | })) |
| 2263 | |
| 2264 | it.effect("routes invalid tool call params through failureMode: return without failing the stream", () => |
| 2265 | Effect.gen(function*() { |
| 2266 | const toolkit = Toolkit.make(ReturnModeTool) |
| 2267 | const handlers = toolkit.toLayer({ |
| 2268 | ReturnModeTool: ({ input }) => Effect.succeed({ output: `processed: ${input}` }) |
| 2269 | }) |
| 2270 | |
| 2271 | const streamEvents = [ |
| 2272 | { |
| 2273 | type: "response.created", |
| 2274 | sequence_number: 1, |
| 2275 | response: makeDefaultResponse({ |
| 2276 | id: "resp_invalid_params", |
| 2277 | status: "in_progress", |
| 2278 | output: [] |
| 2279 | }) |
| 2280 | }, |
| 2281 | { |
| 2282 | type: "response.output_item.added", |
| 2283 | sequence_number: 2, |
| 2284 | output_index: 0, |
| 2285 | item: { |
| 2286 | type: "function_call", |
| 2287 | id: "fc_1", |
| 2288 | call_id: "call_1", |
| 2289 | name: "ReturnModeTool", |
| 2290 | arguments: "", |
| 2291 | status: "in_progress" |
| 2292 | } |
| 2293 | }, |
| 2294 | { |
| 2295 | type: "response.function_call_arguments.done", |
| 2296 | sequence_number: 3, |
| 2297 | output_index: 0, |
| 2298 | item_id: "fc_1", |
| 2299 | name: "ReturnModeTool", |
| 2300 | arguments: "{\"input\":123}" |
| 2301 | }, |
| 2302 | { |
| 2303 | type: "response.completed", |
| 2304 | sequence_number: 4, |
| 2305 | response: makeDefaultResponse({ |
| 2306 | id: "resp_invalid_params", |
| 2307 | status: "completed", |
| 2308 | output: [] |
| 2309 | }) |
| 2310 | } |
| 2311 | ] as unknown as ReadonlyArray<typeof Generated.ResponseStreamEvent.Type> |
| 2312 | |
| 2313 | const partsChunk = yield* LanguageModel.streamText({ |
| 2314 | prompt: "Use the test tool", |
| 2315 | toolkit |
| 2316 | }).pipe( |
| 2317 | Stream.runCollect, |
| 2318 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 2319 | Effect.provide(makeStreamTestLayer(streamEvents)), |
| 2320 | Effect.provide(handlers) |
| 2321 | ) |
| 2322 | |
| 2323 | const parts = globalThis.Array.from(partsChunk) |
| 2324 | const toolResults = parts.filter((part) => part.type === "tool-result") |
| 2325 | strictEqual(toolResults.length, 1) |
| 2326 | const toolResult = toolResults[0] |
| 2327 | if (toolResult?.type === "tool-result") { |
| 2328 | strictEqual(toolResult.isFailure, true) |
| 2329 | const result = toolResult.result as AiError.AiError |
| 2330 | strictEqual(result._tag, "AiError") |
| 2331 | strictEqual(result.reason._tag, "ToolParameterValidationError") |
| 2332 | } |
| 2333 | assert.isDefined(parts.find((part) => part.type === "finish")) |
| 2334 | })) |
| 2335 | |
| 2336 | it.effect("streams undeclared tool calls when tool call validation is deferred", () => |
| 2337 | Effect.gen(function*() { |
| 2338 | const streamEvents = [ |
| 2339 | { |
| 2340 | type: "response.created", |
| 2341 | sequence_number: 1, |
| 2342 | response: makeDefaultResponse({ status: "in_progress" }) |
| 2343 | }, |
| 2344 | { |
| 2345 | type: "response.output_item.added", |
| 2346 | sequence_number: 2, |
| 2347 | output_index: 0, |
| 2348 | item: makeFunctionCall("Write", {}, { arguments: "", status: "in_progress" }) |
| 2349 | }, |
| 2350 | { |
| 2351 | type: "response.output_item.done", |
| 2352 | sequence_number: 3, |
| 2353 | output_index: 0, |
| 2354 | item: makeFunctionCall("Write", { path: "a" }) |
| 2355 | }, |
| 2356 | { |
| 2357 | type: "response.completed", |
| 2358 | sequence_number: 4, |
| 2359 | response: makeDefaultResponse() |
| 2360 | } |
| 2361 | ] as unknown as ReadonlyArray<typeof Generated.ResponseStreamEvent.Type> |
| 2362 | |
| 2363 | const parts = yield* LanguageModel.streamText({ |
| 2364 | prompt: "Use the tools", |
| 2365 | toolkit: TestToolkit, |
| 2366 | disableToolCallResolution: true, |
| 2367 | toolCallValidation: "deferred" |
| 2368 | }).pipe( |
| 2369 | Stream.runCollect, |
| 2370 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 2371 | Effect.provide(makeStreamTestLayer(streamEvents)), |
| 2372 | Effect.provide(TestToolkitLayer) |
| 2373 | ) |
| 2374 | |
| 2375 | deepStrictEqual( |
| 2376 | parts.filter((part) => part.type === "tool-call").map(({ name, params }) => ({ name, params })), |
| 2377 | [{ name: "Write", params: { path: "a" } }] |
| 2378 | ) |
| 2379 | })) |
| 2380 | |
| 2381 | it.effect("waits for the stable streamed web search action before emitting the tool call", () => |
| 2382 | Effect.gen(function*() { |
| 2383 | const toolkit = Toolkit.make(OpenAiTool.WebSearch({})) |
| 2384 | const streamEvents = [ |
| 2385 | { |
| 2386 | type: "response.created", |
| 2387 | sequence_number: 1, |
| 2388 | response: makeDefaultResponse({ status: "in_progress" }) |
| 2389 | }, |
| 2390 | { |
| 2391 | type: "response.output_item.added", |
| 2392 | sequence_number: 2, |
| 2393 | output_index: 0, |
| 2394 | item: { |
| 2395 | type: "web_search_call", |
| 2396 | id: "ws_123", |
| 2397 | status: "in_progress" |
| 2398 | } |
| 2399 | }, |
| 2400 | { |
| 2401 | type: "response.output_item.done", |
| 2402 | sequence_number: 3, |
| 2403 | output_index: 0, |
| 2404 | item: makeWebSearchCall() |
| 2405 | } |
| 2406 | ] as unknown as ReadonlyArray<typeof Generated.ResponseStreamEvent.Type> |
| 2407 | |
| 2408 | const parts = yield* LanguageModel.streamText({ |
| 2409 | prompt: "Search the web", |
| 2410 | toolkit, |
| 2411 | disableToolCallResolution: true |
| 2412 | }).pipe( |
| 2413 | Stream.runCollect, |
| 2414 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 2415 | Effect.provide(makeStreamTestLayer(streamEvents)) |
| 2416 | ) |
| 2417 | |
| 2418 | const toolCalls = parts.filter((part) => part.type === "tool-call") |
| 2419 | strictEqual(toolCalls.length, 1) |
| 2420 | const toolCall = toolCalls[0] |
| 2421 | assert.isDefined(toolCall) |
| 2422 | assert.deepStrictEqual(toolCall.params, { |
| 2423 | action: { type: "search", query: "Effect TypeScript" } |
| 2424 | }) |
| 2425 | |
| 2426 | const toolResult = parts.find((part) => part.type === "tool-result") |
| 2427 | assert.isDefined(toolResult) |
| 2428 | assert.deepStrictEqual(toolResult.result, { |
| 2429 | action: { type: "search", query: "Effect TypeScript" }, |
| 2430 | status: "completed" |
| 2431 | }) |
| 2432 | })) |
| 2433 | |
| 2434 | for (const { tool, status } of webSearchFailures) { |
| 2435 | it.effect( |
| 2436 | `${tool.name} preserves ${status} results from output_item.done`, |
| 2437 | () => |
| 2438 | Effect.gen(function*() { |
| 2439 | const parts = yield* LanguageModel.streamText({ |
| 2440 | prompt: "Search the web", |
| 2441 | toolkit: Toolkit.make(tool) |
| 2442 | }).pipe( |
| 2443 | Stream.runCollect, |
| 2444 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 2445 | Effect.provide(makeStreamTestLayer([{ |
| 2446 | type: "response.output_item.done", |
| 2447 | sequence_number: 1, |
| 2448 | output_index: 0, |
| 2449 | item: makeWebSearchCall({ status }) |
| 2450 | }])) |
| 2451 | ) |
| 2452 | |
| 2453 | const toolResult = parts.find((part) => part.type === "tool-result")! |
| 2454 | strictEqual(toolResult.isFailure, true) |
| 2455 | deepStrictEqual(toolResult.result, { |
| 2456 | action: { type: "search", query: "Effect TypeScript" }, |
| 2457 | status |
| 2458 | }) |
| 2459 | }) |
| 2460 | ) |
| 2461 | } |
| 2462 | |
| 2463 | it.effect.each(fileSearchOutcomes)( |
| 2464 | "OpenAiFileSearch reports $status results from output_item.done", |
| 2465 | ({ status, isFailure, results }) => |
| 2466 | Effect.gen(function*() { |
| 2467 | const parts = yield* LanguageModel.streamText({ |
| 2468 | prompt: "Search the files", |
| 2469 | toolkit: Toolkit.make(OpenAiTool.FileSearch({ vector_store_ids: ["vs_123"] })) |
| 2470 | }).pipe( |
| 2471 | Stream.runCollect, |
| 2472 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 2473 | Effect.provide(makeStreamTestLayer([{ |
| 2474 | type: "response.output_item.done", |
| 2475 | sequence_number: 1, |
| 2476 | output_index: 0, |
| 2477 | item: makeFileSearchCall({ status, ...(results === undefined ? {} : { results }) }) |
| 2478 | }])) |
| 2479 | ) |
| 2480 | |
| 2481 | const toolResult = parts.find((part) => part.type === "tool-result")! |
| 2482 | strictEqual(toolResult.isFailure, isFailure) |
| 2483 | deepStrictEqual(toolResult.result, { |
| 2484 | status, |
| 2485 | queries: ["Effect TypeScript"], |
| 2486 | results: results ?? null |
| 2487 | }) |
| 2488 | }) |
| 2489 | ) |
| 2490 | |
| 2491 | it.effect("handles reasoning summary events when reasoning state is missing", () => |
| 2492 | Effect.gen(function*() { |
| 2493 | const streamEvents = [ |
| 2494 | { |
| 2495 | type: "response.created", |
| 2496 | sequence_number: 1, |
| 2497 | response: makeDefaultResponse({ |
| 2498 | id: "resp_reasoning_missing_state", |
| 2499 | status: "in_progress", |
| 2500 | output: [] |
| 2501 | }) |
| 2502 | }, |
| 2503 | { |
| 2504 | type: "response.reasoning_summary_part.added", |
| 2505 | sequence_number: 2, |
| 2506 | output_index: 0, |
| 2507 | item_id: "rs_missing", |
| 2508 | summary_index: 1 |
| 2509 | }, |
| 2510 | { |
| 2511 | type: "response.reasoning_summary_text.delta", |
| 2512 | sequence_number: 3, |
| 2513 | output_index: 0, |
| 2514 | item_id: "rs_missing", |
| 2515 | summary_index: 1, |
| 2516 | delta: "thinking" |
| 2517 | }, |
| 2518 | { |
| 2519 | type: "response.reasoning_summary_part.done", |
| 2520 | sequence_number: 4, |
| 2521 | output_index: 0, |
| 2522 | item_id: "rs_missing", |
| 2523 | summary_index: 1 |
| 2524 | }, |
| 2525 | { |
| 2526 | type: "response.output_item.done", |
| 2527 | sequence_number: 5, |
| 2528 | output_index: 0, |
| 2529 | item: makeReasoningOutput(["thinking"], { id: "rs_missing" }) |
| 2530 | }, |
| 2531 | { |
| 2532 | type: "response.output_item.done", |
| 2533 | sequence_number: 6, |
| 2534 | output_index: 1, |
| 2535 | item: makeReasoningOutput([], { id: "rs_done_only" }) |
| 2536 | }, |
| 2537 | { |
| 2538 | type: "response.completed", |
| 2539 | sequence_number: 7, |
| 2540 | response: makeDefaultResponse({ |
| 2541 | id: "resp_reasoning_missing_state", |
| 2542 | status: "completed", |
| 2543 | output: [] |
| 2544 | }) |
| 2545 | } |
| 2546 | ] as unknown as ReadonlyArray<typeof Generated.ResponseStreamEvent.Type> |
| 2547 | |
| 2548 | const partsChunk = yield* LanguageModel.streamText({ |
| 2549 | prompt: "reason" |
| 2550 | }).pipe( |
| 2551 | Stream.runCollect, |
| 2552 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 2553 | Effect.provide(makeStreamTestLayer(streamEvents)) |
| 2554 | ) |
| 2555 | |
| 2556 | const parts = globalThis.Array.from(partsChunk) |
| 2557 | assert.isDefined(parts.find((part) => part.type === "reasoning-start" && part.id === "rs_missing:1")) |
| 2558 | assert.isDefined(parts.find((part) => part.type === "reasoning-end" && part.id === "rs_missing:1")) |
| 2559 | assert.isDefined(parts.find((part) => part.type === "finish")) |
| 2560 | })) |
| 2561 | |
| 2562 | it.effect("uses canonical OpenAiMcp name for streamed mcp_call", () => |
| 2563 | Effect.gen(function*() { |
| 2564 | const outputItem = makeMcpCall("CheckPackage", { packageName: "effect" }, { id: "mcp_call_1" }) |
| 2565 | const streamEvents = [ |
| 2566 | { |
| 2567 | type: "response.created", |
| 2568 | sequence_number: 1, |
| 2569 | response: makeDefaultResponse({ |
| 2570 | id: "resp_mcp_stream", |
| 2571 | status: "in_progress", |
| 2572 | output: [] |
| 2573 | }) |
| 2574 | }, |
| 2575 | { |
| 2576 | type: "response.output_item.done", |
| 2577 | output_index: 0, |
| 2578 | sequence_number: 2, |
| 2579 | item: outputItem |
| 2580 | } |
| 2581 | ] as unknown as ReadonlyArray<typeof Generated.ResponseStreamEvent.Type> |
| 2582 | |
| 2583 | const partsChunk = yield* LanguageModel.streamText({ |
| 2584 | prompt: "Use MCP", |
| 2585 | toolkit: McpToolkit, |
| 2586 | disableToolCallResolution: true |
| 2587 | }).pipe( |
| 2588 | Stream.runCollect, |
| 2589 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 2590 | Effect.provide(makeStreamTestLayer(streamEvents)) |
| 2591 | ) |
| 2592 | |
| 2593 | const parts = globalThis.Array.from(partsChunk) |
| 2594 | const toolCall = parts.find((part) => part.type === "tool-call") |
| 2595 | assert.isDefined(toolCall) |
| 2596 | if (toolCall?.type === "tool-call") { |
| 2597 | strictEqual(toolCall.name, "OpenAiMcp") |
| 2598 | deepStrictEqual(toolCall.params, { packageName: "effect" }) |
| 2599 | } |
| 2600 | |
| 2601 | const toolResult = parts.find((part) => part.type === "tool-result") |
| 2602 | assert.isDefined(toolResult) |
| 2603 | if (toolResult?.type === "tool-result") { |
| 2604 | strictEqual(toolResult.name, "OpenAiMcp") |
| 2605 | assertTrue(!toolResult.isFailure, "expected a successful MCP result") |
| 2606 | strictEqual(toolResult.result.name, "CheckPackage") |
| 2607 | } |
| 2608 | })) |
| 2609 | |
| 2610 | it.effect("uses canonical OpenAiMcp name for streamed mcp_approval_request", () => |
| 2611 | Effect.gen(function*() { |
| 2612 | const outputItem = makeMcpApprovalRequest("CheckPackage", { packageName: "effect" }, { id: "approval_1" }) |
| 2613 | const streamEvents = [ |
| 2614 | { |
| 2615 | type: "response.created", |
| 2616 | sequence_number: 1, |
| 2617 | response: makeDefaultResponse({ |
| 2618 | id: "resp_mcp_approval_stream", |
| 2619 | status: "in_progress", |
| 2620 | output: [] |
| 2621 | }) |
| 2622 | }, |
| 2623 | { |
| 2624 | type: "response.output_item.done", |
| 2625 | output_index: 0, |
| 2626 | sequence_number: 2, |
| 2627 | item: outputItem |
| 2628 | } |
| 2629 | ] as unknown as ReadonlyArray<typeof Generated.ResponseStreamEvent.Type> |
| 2630 | |
| 2631 | const partsChunk = yield* LanguageModel.streamText({ |
| 2632 | prompt: "Use MCP", |
| 2633 | toolkit: McpToolkit, |
| 2634 | disableToolCallResolution: true |
| 2635 | }).pipe( |
| 2636 | Stream.runCollect, |
| 2637 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 2638 | Effect.provide(makeStreamTestLayer(streamEvents)) |
| 2639 | ) |
| 2640 | |
| 2641 | const parts = globalThis.Array.from(partsChunk) |
| 2642 | const toolCall = parts.find((part) => part.type === "tool-call") |
| 2643 | assert.isDefined(toolCall) |
| 2644 | if (toolCall?.type === "tool-call") { |
| 2645 | strictEqual(toolCall.name, "OpenAiMcp") |
| 2646 | deepStrictEqual(toolCall.params, { packageName: "effect" }) |
| 2647 | } |
| 2648 | |
| 2649 | const approvalRequest = parts.find((part) => part.type === "tool-approval-request") |
| 2650 | assert.isDefined(approvalRequest) |
| 2651 | if (toolCall?.type === "tool-call" && approvalRequest?.type === "tool-approval-request") { |
| 2652 | strictEqual(approvalRequest.toolCallId, toolCall.id) |
| 2653 | } |
| 2654 | })) |
| 2655 | |
| 2656 | it.effect("pre-resolves denied OpenAiMcp approvals without lookup failure", () => |
| 2657 | Effect.gen(function*() { |
| 2658 | const result = yield* LanguageModel.generateText({ |
| 2659 | prompt: Prompt.make([ |
| 2660 | { |
| 2661 | role: "assistant", |
| 2662 | content: [ |
| 2663 | Prompt.toolCallPart({ |
| 2664 | id: "mcp_tool_call_1", |
| 2665 | name: "OpenAiMcp", |
| 2666 | params: { packageName: "effect" }, |
| 2667 | providerExecuted: true |
| 2668 | }), |
| 2669 | Prompt.makePart("tool-approval-request", { |
| 2670 | approvalId: "approval_1", |
| 2671 | toolCallId: "mcp_tool_call_1" |
| 2672 | }) |
| 2673 | ] |
| 2674 | }, |
| 2675 | { |
| 2676 | role: "tool", |
| 2677 | content: [ |
| 2678 | Prompt.toolApprovalResponsePart({ |
| 2679 | approvalId: "approval_1", |
| 2680 | approved: false, |
| 2681 | reason: "Denied" |
| 2682 | }) |
| 2683 | ] |
| 2684 | }, |
| 2685 | { role: "user", content: "Continue" } |
| 2686 | ]), |
| 2687 | toolkit: McpToolkit |
| 2688 | }).pipe( |
| 2689 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 2690 | Effect.provide(makeTestLayer({ |
| 2691 | body: { |
| 2692 | output: [makeTextOutput("Handled denied MCP approval")] |
| 2693 | } |
| 2694 | })) |
| 2695 | ) |
| 2696 | |
| 2697 | strictEqual(result.text, "Handled denied MCP approval") |
| 2698 | })) |
| 2699 | }) |
| 2700 | |
| 2701 | describe("withConfigOverride", () => { |
| 2702 | it.effect("merges config overrides", () => |
| 2703 | Effect.gen(function*() { |
| 2704 | yield* LanguageModel.generateText({ prompt: "test" }).pipe( |
| 2705 | OpenAiLanguageModel.withConfigOverride({ temperature: 0.5 }), |
| 2706 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")) |
| 2707 | ) |
| 2708 | |
| 2709 | const requests = yield* MockHttpClient.requests |
| 2710 | const body = yield* getRequestBody(requests[0]) |
| 2711 | |
| 2712 | strictEqual(body.temperature, 0.5) |
| 2713 | }).pipe(Effect.provide(makeTestLayer()))) |
| 2714 | |
| 2715 | it.effect("override takes precedence", () => |
| 2716 | Effect.gen(function*() { |
| 2717 | yield* LanguageModel.generateText({ prompt: "test" }).pipe( |
| 2718 | OpenAiLanguageModel.withConfigOverride({ temperature: 0.9 }), |
| 2719 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini", { temperature: 0.5 })) |
| 2720 | ) |
| 2721 | |
| 2722 | const requests = yield* MockHttpClient.requests |
| 2723 | const body = yield* getRequestBody(requests[0]) |
| 2724 | |
| 2725 | strictEqual(body.temperature, 0.9) |
| 2726 | }).pipe(Effect.provide(makeTestLayer()))) |
| 2727 | }) |
| 2728 | |
| 2729 | describe("config", () => { |
| 2730 | it.effect("does not leak library-only fields into request body", () => |
| 2731 | Effect.gen(function*() { |
| 2732 | yield* LanguageModel.generateText({ prompt: "test" }).pipe( |
| 2733 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini", { |
| 2734 | fileIdPrefixes: ["file-"], |
| 2735 | strictJsonSchema: false, |
| 2736 | useItemReferences: false, |
| 2737 | reasoningModel: true, |
| 2738 | temperature: 0.5 |
| 2739 | })) |
| 2740 | ) |
| 2741 | |
| 2742 | const requests = yield* MockHttpClient.requests |
| 2743 | const body = yield* getRequestBody(requests[0]) |
| 2744 | |
| 2745 | strictEqual(body.fileIdPrefixes, undefined) |
| 2746 | strictEqual(body.strictJsonSchema, undefined) |
| 2747 | strictEqual(body.useItemReferences, undefined) |
| 2748 | strictEqual(body.reasoningModel, undefined) |
| 2749 | strictEqual(body.temperature, 0.5) |
| 2750 | }).pipe(Effect.provide(makeTestLayer()))) |
| 2751 | }) |
| 2752 | |
| 2753 | describe("reasoning capabilities", () => { |
| 2754 | it.effect("merges explicit include values with generated values", () => |
| 2755 | Effect.gen(function*() { |
| 2756 | yield* LanguageModel.generateText({ prompt: "test" }).pipe( |
| 2757 | Effect.provide(OpenAiLanguageModel.model("custom-model", { |
| 2758 | store: false, |
| 2759 | include: ["message.input_image.image_url", "reasoning.encrypted_content"], |
| 2760 | top_logprobs: 2 |
| 2761 | })) |
| 2762 | ) |
| 2763 | |
| 2764 | const requests = yield* MockHttpClient.requests |
| 2765 | const body = yield* getRequestBody(requests[0]) |
| 2766 | |
| 2767 | deepStrictEqual(body.include, [ |
| 2768 | "message.input_image.image_url", |
| 2769 | "reasoning.encrypted_content", |
| 2770 | "message.output_text.logprobs" |
| 2771 | ]) |
| 2772 | }).pipe(Effect.provide(makeTestLayer()))) |
| 2773 | |
| 2774 | for (const { name, model, config, include, role } of reasoningCapabilityCases) { |
| 2775 | it.effect(name, () => |
| 2776 | Effect.gen(function*() { |
| 2777 | yield* LanguageModel.generateText({ |
| 2778 | prompt: Prompt.make([ |
| 2779 | { role: "system", content: "You are a helpful assistant" }, |
| 2780 | { role: "user", content: "Hello" } |
| 2781 | ]) |
| 2782 | }).pipe(Effect.provide(OpenAiLanguageModel.model(model, config))) |
| 2783 | |
| 2784 | const requests = yield* MockHttpClient.requests |
| 2785 | const body = yield* getRequestBody(requests[0]) |
| 2786 | |
| 2787 | deepStrictEqual(body.include, include) |
| 2788 | strictEqual(body.input[0].role, role) |
| 2789 | }).pipe(Effect.provide(makeTestLayer({ body: { model } })))) |
| 2790 | } |
| 2791 | |
| 2792 | it.effect("prefers the reasoningModel override from withConfigOverride", () => |
| 2793 | Effect.gen(function*() { |
| 2794 | yield* LanguageModel.generateText({ |
| 2795 | prompt: Prompt.make([ |
| 2796 | { role: "system", content: "You are a helpful assistant" }, |
| 2797 | { role: "user", content: "Hello" } |
| 2798 | ]) |
| 2799 | }).pipe( |
| 2800 | OpenAiLanguageModel.withConfigOverride({ reasoningModel: true }), |
| 2801 | Effect.provide(OpenAiLanguageModel.model("gpt-4.1", { store: false, reasoningModel: false })) |
| 2802 | ) |
| 2803 | |
| 2804 | const requests = yield* MockHttpClient.requests |
| 2805 | const body = yield* getRequestBody(requests[0]) |
| 2806 | |
| 2807 | deepStrictEqual(body.include, ["reasoning.encrypted_content"]) |
| 2808 | strictEqual(body.input[0].role, "developer") |
| 2809 | }).pipe(Effect.provide(makeTestLayer({ body: { model: "gpt-4.1" } })))) |
| 2810 | }) |
| 2811 | }) |
| 2812 | |
| 2813 | const reasoningCapabilityCases: ReadonlyArray<{ |
| 2814 | readonly name: string |
| 2815 | readonly model: string |
| 2816 | readonly config: Omit<typeof OpenAiLanguageModel.Config.Service, "model"> |
| 2817 | readonly include: ReadonlyArray<OpenAiSchema.IncludeEnum> | undefined |
| 2818 | readonly role: "system" | "developer" |
| 2819 | }> = [ |
| 2820 | { |
| 2821 | name: "defaults an unknown model to reasoning", |
| 2822 | model: "custom-model", |
| 2823 | config: { store: false }, |
| 2824 | include: ["reasoning.encrypted_content"], |
| 2825 | role: "developer" |
| 2826 | }, |
| 2827 | { |
| 2828 | name: "disables automatic reasoning without dropping explicit includes", |
| 2829 | model: "custom-model", |
| 2830 | config: { store: false, reasoningModel: false, include: ["message.input_image.image_url"] }, |
| 2831 | include: ["message.input_image.image_url"], |
| 2832 | role: "system" |
| 2833 | } |
| 2834 | ] |
| 2835 | |
| 2836 | // ============================================================================= |
| 2837 | // Test Infrastructure |
| 2838 | // ============================================================================= |
| 2839 | |
| 2840 | class MockOpenAiResponse extends Context.Service<MockOpenAiResponse, { |
| 2841 | readonly status: number |
| 2842 | readonly body: Generated.Response |
| 2843 | readonly headers?: Record<string, string> | undefined |
| 2844 | }>()("MockOpenAiResponse") {} |
| 2845 | |
| 2846 | class MockHttpClient extends Context.Service<MockHttpClient, { |
| 2847 | readonly requests: Effect.Effect<ReadonlyArray<HttpClientRequest.HttpClientRequest>> |
| 2848 | }>()("MockHttpClient") { |
| 2849 | static requests = Effect.service(MockHttpClient).pipe( |
| 2850 | Effect.flatMap((client) => client.requests) |
| 2851 | ) |
| 2852 | } |
| 2853 | |
| 2854 | const encodeResponse = Schema.encodeUnknownEffect(OpenAiSchema.Response) |
| 2855 | |
| 2856 | const makeHttpClient = Effect.gen(function*() { |
| 2857 | const capturedRequests = yield* Ref.make<ReadonlyArray<HttpClientRequest.HttpClientRequest>>([]) |
| 2858 | const response = yield* MockOpenAiResponse |
| 2859 | const body = yield* Effect.orDie(encodeResponse(response.body)) |
| 2860 | |
| 2861 | const httpClient = HttpClient.makeWith( |
| 2862 | Effect.fnUntraced(function*(requestEffect) { |
| 2863 | const request = yield* requestEffect |
| 2864 | yield* Ref.update(capturedRequests, Array.append(request)) |
| 2865 | return HttpClientResponse.fromWeb( |
| 2866 | request, |
| 2867 | new Response(JSON.stringify(body), { |
| 2868 | headers: response.headers ?? {}, |
| 2869 | status: response.status |
| 2870 | }) |
| 2871 | ) |
| 2872 | }), |
| 2873 | Effect.succeed as HttpClient.HttpClient.Preprocess<HttpClientError.HttpClientError, never> |
| 2874 | ) |
| 2875 | |
| 2876 | return Context.make(HttpClient.HttpClient, httpClient).pipe( |
| 2877 | Context.add(MockHttpClient, MockHttpClient.of({ requests: Ref.get(capturedRequests) })) |
| 2878 | ) |
| 2879 | }) |
| 2880 | |
| 2881 | const HttpClientLayer = Layer.effectContext(makeHttpClient) |
| 2882 | |
| 2883 | const makeRawResponseClient = (body: string, contentType: string) => |
| 2884 | HttpClient.make((request) => |
| 2885 | Effect.succeed(HttpClientResponse.fromWeb( |
| 2886 | request, |
| 2887 | new Response(body, { headers: { "content-type": contentType } }) |
| 2888 | )) |
| 2889 | ) |
| 2890 | |
| 2891 | const makeStreamTestLayer = (events: ReadonlyArray<typeof Generated.ResponseStreamEvent.Type>) => { |
| 2892 | const response = HttpClientResponse.fromWeb( |
| 2893 | HttpClientRequest.get("https://api.openai.com/v1/responses"), |
| 2894 | new Response("", { |
| 2895 | status: 200, |
| 2896 | headers: { "content-type": "text/event-stream" } |
| 2897 | }) |
| 2898 | ) |
| 2899 | |
| 2900 | return Layer.succeed( |
| 2901 | OpenAiClient.OpenAiClient, |
| 2902 | OpenAiClient.OpenAiClient.of({ |
| 2903 | client: undefined as any, |
| 2904 | createDecisions: () => Effect.die(new Error("unexpected createDecisions call")), |
| 2905 | createResponse: () => Effect.die(new Error("unexpected createResponse call")), |
| 2906 | createResponseStream: () => Effect.succeed([response, Stream.fromIterable(events)]), |
| 2907 | createEmbedding: () => Effect.die(new Error("unexpected createEmbedding call")) |
| 2908 | }) |
| 2909 | ) |
| 2910 | } |
| 2911 | |
| 2912 | const makeDefaultResponse = ( |
| 2913 | overrides: Partial<Generated.Response> = {} |
| 2914 | ): Generated.Response => ({ |
| 2915 | id: "resp_test123", |
| 2916 | object: "response", |
| 2917 | created_at: Math.floor(Date.now() / 1000), |
| 2918 | model: "gpt-4o-mini", |
| 2919 | status: "completed", |
| 2920 | output: [], |
| 2921 | metadata: null, |
| 2922 | temperature: null, |
| 2923 | top_p: null, |
| 2924 | tools: [], |
| 2925 | tool_choice: "auto", |
| 2926 | error: null, |
| 2927 | incomplete_details: null, |
| 2928 | instructions: null, |
| 2929 | parallel_tool_calls: false, |
| 2930 | ...overrides |
| 2931 | }) |
| 2932 | |
| 2933 | const makeTestLayer = (options: { |
| 2934 | readonly body?: Partial<Generated.Response> |
| 2935 | readonly status?: number |
| 2936 | readonly headers?: Record<string, string> |
| 2937 | } = {}) => |
| 2938 | OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 2939 | Layer.provideMerge(HttpClientLayer), |
| 2940 | Layer.provide(Layer.succeed(MockOpenAiResponse, { |
| 2941 | body: makeDefaultResponse(options.body), |
| 2942 | status: options.status ?? 200, |
| 2943 | headers: options.headers ?? {} |
| 2944 | })) |
| 2945 | ) |
| 2946 | |
| 2947 | const getRequestBody = Effect.fnUntraced(function*(request: HttpClientRequest.HttpClientRequest) { |
| 2948 | const body = request.body |
| 2949 | if (body._tag === "Uint8Array") { |
| 2950 | const text = new TextDecoder().decode(body.body) |
| 2951 | return JSON.parse(text) |
| 2952 | } |
| 2953 | return yield* Effect.die(new Error("Expected Uint8Array body")) |
| 2954 | }) |
| 2955 | |
| 2956 | const decodeToolParamsFromStream = ( |
| 2957 | parts: ReadonlyArray<any>, |
| 2958 | toolCallId: string |
| 2959 | ): Record<string, unknown> => { |
| 2960 | const start = parts.find((part) => part.type === "tool-params-start" && part.id === toolCallId) |
| 2961 | const end = parts.find((part) => part.type === "tool-params-end" && part.id === toolCallId) |
| 2962 | assert.isDefined(start) |
| 2963 | assert.isDefined(end) |
| 2964 | |
| 2965 | const deltas = parts |
| 2966 | .filter((part) => part.type === "tool-params-delta" && part.id === toolCallId) |
| 2967 | .map((part) => part.delta) |
| 2968 | .join("") |
| 2969 | |
| 2970 | return JSON.parse(deltas) as Record<string, unknown> |
| 2971 | } |
| 2972 | |
| 2973 | const makeTextOutput = ( |
| 2974 | text: string, |
| 2975 | overrides: Partial<Generated.OutputMessage> = {} |
| 2976 | ): Generated.OutputMessage => ({ |
| 2977 | type: "message", |
| 2978 | id: "msg_123", |
| 2979 | role: "assistant" as const, |
| 2980 | status: "completed", |
| 2981 | content: [{ type: "output_text", text, annotations: [], logprobs: [] }], |
| 2982 | ...overrides |
| 2983 | }) |
| 2984 | |
| 2985 | const makeFunctionCall = ( |
| 2986 | name: string, |
| 2987 | args: Record<string, unknown>, |
| 2988 | overrides: Partial<Generated.FunctionToolCall> = {} |
| 2989 | ): Generated.FunctionToolCall => ({ |
| 2990 | type: "function_call", |
| 2991 | id: "fc_123", |
| 2992 | call_id: "call_123", |
| 2993 | name, |
| 2994 | arguments: JSON.stringify(args), |
| 2995 | status: "completed", |
| 2996 | ...overrides |
| 2997 | }) |
| 2998 | |
| 2999 | const makeWebSearchCall = ( |
| 3000 | overrides: Partial<Generated.WebSearchToolCall> = {} |
| 3001 | ): Generated.WebSearchToolCall => ({ |
| 3002 | type: "web_search_call", |
| 3003 | id: "ws_123", |
| 3004 | status: "completed", |
| 3005 | action: { type: "search", query: "Effect TypeScript" }, |
| 3006 | ...overrides |
| 3007 | }) |
| 3008 | |
| 3009 | const makeFileSearchCall = ( |
| 3010 | overrides: Partial<Generated.FileSearchToolCall> = {} |
| 3011 | ): Generated.FileSearchToolCall => ({ |
| 3012 | type: "file_search_call", |
| 3013 | id: "fs_123", |
| 3014 | status: "completed", |
| 3015 | queries: ["Effect TypeScript"], |
| 3016 | ...overrides |
| 3017 | }) |
| 3018 | |
| 3019 | const makeMcpCall = ( |
| 3020 | name: string, |
| 3021 | args: Record<string, unknown>, |
| 3022 | overrides: Partial<Generated.MCPToolCall> = {} |
| 3023 | ): Generated.MCPToolCall => ({ |
| 3024 | type: "mcp_call", |
| 3025 | id: "mcp_call_123", |
| 3026 | server_label: "npm", |
| 3027 | name, |
| 3028 | arguments: JSON.stringify(args), |
| 3029 | output: "ok", |
| 3030 | status: "completed", |
| 3031 | ...overrides |
| 3032 | }) |
| 3033 | |
| 3034 | const makeMcpApprovalRequest = ( |
| 3035 | name: string, |
| 3036 | args: Record<string, unknown>, |
| 3037 | overrides: Partial<Generated.MCPApprovalRequest> & { readonly approval_request_id?: string } = {} |
| 3038 | ): Generated.MCPApprovalRequest => ({ |
| 3039 | type: "mcp_approval_request", |
| 3040 | id: "approval_123", |
| 3041 | server_label: "npm", |
| 3042 | name, |
| 3043 | arguments: JSON.stringify(args), |
| 3044 | ...overrides |
| 3045 | }) |
| 3046 | |
| 3047 | const makeReasoningOutput = ( |
| 3048 | summaries: Array<string>, |
| 3049 | overrides: Partial<Generated.ReasoningItem> = {} |
| 3050 | ): Generated.ReasoningItem => ({ |
| 3051 | type: "reasoning", |
| 3052 | id: "rs_123", |
| 3053 | summary: summaries.map((text) => ({ type: "summary_text", text })), |
| 3054 | encrypted_content: null, |
| 3055 | ...overrides |
| 3056 | }) |
| 3057 | |
| 3058 | type TestResponseUsage = Omit<Generated.ResponseUsage, "input_tokens_details"> & { |
| 3059 | readonly input_tokens_details: { |
| 3060 | readonly cached_tokens: number |
| 3061 | readonly cache_write_tokens?: number |
| 3062 | } |
| 3063 | } |
| 3064 | |
| 3065 | const makeUsage = ( |
| 3066 | overrides: Partial<TestResponseUsage> = {} |
| 3067 | ): TestResponseUsage => ({ |
| 3068 | input_tokens: 10, |
| 3069 | output_tokens: 20, |
| 3070 | total_tokens: 30, |
| 3071 | input_tokens_details: { cached_tokens: 0 }, |
| 3072 | output_tokens_details: { reasoning_tokens: 0 }, |
| 3073 | ...overrides |
| 3074 | }) |
| 3075 | |
| 3076 | const TestTool = Tool.make("TestTool", { |
| 3077 | description: "A test tool", |
| 3078 | parameters: Schema.Struct({ input: Schema.String }), |
| 3079 | success: Schema.Struct({ output: Schema.String }) |
| 3080 | }) |
| 3081 | |
| 3082 | const ReturnModeTool = Tool.make("ReturnModeTool", { |
| 3083 | description: "A test tool", |
| 3084 | failureMode: "return", |
| 3085 | parameters: Schema.Struct({ input: Schema.String }), |
| 3086 | success: Schema.Struct({ output: Schema.String }), |
| 3087 | failure: Schema.Struct({ error: Schema.String }) |
| 3088 | }) |
| 3089 | |
| 3090 | const TransformParamsTool = Tool.make("TransformParamsTool", { |
| 3091 | description: "A test tool", |
| 3092 | parameters: Schema.Struct({ input: Schema.FiniteFromString }), |
| 3093 | success: Schema.Struct({ output: Schema.Finite }) |
| 3094 | }) |
| 3095 | |
| 3096 | class ParamEncodeService extends Context.Service<ParamEncodeService, { |
| 3097 | readonly use: Effect.Effect<void> |
| 3098 | }>()("ParamEncodeService") {} |
| 3099 | |
| 3100 | const AsymmetricParam = Schema.String.pipe( |
| 3101 | Schema.decodeTo(Schema.String, { |
| 3102 | decode: SchemaGetter.passthrough(), |
| 3103 | encode: SchemaGetter.transformEffect<string, string, ParamEncodeService>((value) => |
| 3104 | Effect.service(ParamEncodeService).pipe( |
| 3105 | Effect.flatMap((service) => service.use), |
| 3106 | Effect.as(value) |
| 3107 | ) |
| 3108 | ) |
| 3109 | }) |
| 3110 | ) |
| 3111 | |
| 3112 | const AsymmetricParamsTool = Tool.make("AsymmetricParamsTool", { |
| 3113 | description: "A test tool", |
| 3114 | parameters: Schema.Struct({ input: AsymmetricParam }), |
| 3115 | success: Schema.Struct({ output: Schema.String }) |
| 3116 | }) |
| 3117 | |
| 3118 | const TestToolkit = Toolkit.make(TestTool) |
| 3119 | |
| 3120 | const storedHistoryPrompt = Prompt.make([ |
| 3121 | { role: "user", content: "Question" }, |
| 3122 | { |
| 3123 | role: "assistant", |
| 3124 | content: [ |
| 3125 | Prompt.textPart({ |
| 3126 | text: "Answer", |
| 3127 | options: { openai: { itemId: "msg_1" } } |
| 3128 | }), |
| 3129 | Prompt.reasoningPart({ |
| 3130 | text: "Thinking", |
| 3131 | options: { |
| 3132 | openai: { |
| 3133 | itemId: "rs_1", |
| 3134 | encryptedContent: "encrypted-reasoning" |
| 3135 | } |
| 3136 | } |
| 3137 | }), |
| 3138 | Prompt.toolCallPart({ |
| 3139 | id: "call_1", |
| 3140 | name: "TestTool", |
| 3141 | params: { input: "value" }, |
| 3142 | providerExecuted: false, |
| 3143 | options: { openai: { itemId: "fc_1" } } |
| 3144 | }) |
| 3145 | ] |
| 3146 | }, |
| 3147 | { |
| 3148 | role: "tool", |
| 3149 | content: [Prompt.toolResultPart({ |
| 3150 | id: "call_1", |
| 3151 | name: "TestTool", |
| 3152 | isFailure: false, |
| 3153 | result: { output: "result" }, |
| 3154 | providerExecuted: false |
| 3155 | })] |
| 3156 | }, |
| 3157 | { role: "user", content: "Continue" } |
| 3158 | ]) |
| 3159 | |
| 3160 | const providerExecutedCall = Prompt.toolCallPart({ |
| 3161 | id: "ws_1", |
| 3162 | name: "OpenAiWebSearch", |
| 3163 | params: { |
| 3164 | action: { type: "search", query: "Effect TypeScript" } |
| 3165 | }, |
| 3166 | providerExecuted: true, |
| 3167 | options: { openai: { itemId: "ws_1", status: "completed" } } |
| 3168 | }) |
| 3169 | |
| 3170 | const providerExecutedResult = Prompt.toolResultPart({ |
| 3171 | id: "ws_1", |
| 3172 | name: "OpenAiWebSearch", |
| 3173 | isFailure: false, |
| 3174 | result: { |
| 3175 | action: { type: "search", query: "Effect TypeScript" }, |
| 3176 | status: "completed" |
| 3177 | }, |
| 3178 | providerExecuted: true, |
| 3179 | options: { openai: { itemId: "ws_1", status: "completed" } } |
| 3180 | }) |
| 3181 | |
| 3182 | const providerExecutedHistoryPrompt = Prompt.make([ |
| 3183 | { role: "user", content: "Search" }, |
| 3184 | { role: "assistant", content: [providerExecutedCall, providerExecutedResult] }, |
| 3185 | { role: "user", content: "Continue" } |
| 3186 | ]) |
| 3187 | |
| 3188 | const McpToolkit = Toolkit.make(OpenAiTool.Mcp({ |
| 3189 | server_label: "npm", |
| 3190 | server_url: "https://example.com/mcp", |
| 3191 | require_approval: "never" |
| 3192 | })) |
| 3193 | |
| 3194 | const TestToolkitLayer = TestToolkit.toLayer({ |
| 3195 | TestTool: ({ input }) => Effect.succeed({ output: `processed: ${input}` }) |
| 3196 | }) |