The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { OpenAiClient, OpenAiLanguageModel } from "@effect/ai-openai-compat" |
| 2 | import { assert, describe, it } from "@effect/vitest" |
| 3 | import { Effect, Layer, Redacted, Ref, Schema, Stream } from "effect" |
| 4 | import { type AiError, LanguageModel, Prompt, Tool, Toolkit } from "effect/ai" |
| 5 | import { HttpClient, type HttpClientError, type HttpClientRequest, HttpClientResponse } from "effect/http" |
| 6 | |
| 7 | describe("OpenAiLanguageModel", () => { |
| 8 | for (const mode of ["complete", "stream"] as const) { |
| 9 | it.effect(`prepares selected tool codecs once per ${mode} request and normalizes repeated calls`, () => |
| 10 | Effect.gen(function*() { |
| 11 | let conversions = 0 |
| 12 | const Env = Schema.Record(Schema.String, Schema.String).check( |
| 13 | Schema.makeFilter<object>(() => true, { |
| 14 | representation: { id: "test/compat/toolCodec", payload: null }, |
| 15 | toJsonSchema: () => { |
| 16 | conversions++ |
| 17 | return { minItems: 1 } |
| 18 | } |
| 19 | }) |
| 20 | ) |
| 21 | const ReadEnv = Tool.make("ReadEnv", { parameters: Schema.Struct({ env: Env }) }) |
| 22 | const Unselected = Tool.make("Unselected", { |
| 23 | parameters: Schema.Struct({ opaque: Schema.declare((input): input is string => typeof input === "string") }) |
| 24 | }) |
| 25 | const toolkit = Toolkit.make(ReadEnv, Unselected) |
| 26 | const toolkitLayer = toolkit.toLayer({ |
| 27 | ReadEnv: () => Effect.void, |
| 28 | Unselected: () => Effect.void |
| 29 | }) |
| 30 | const calls = ["call_1", "call_2"].map((id) => ({ |
| 31 | id, |
| 32 | type: "function" as const, |
| 33 | function: { name: "ReadEnv", arguments: JSON.stringify({ env: [{ 0: "PATH", 1: "/usr/bin" }] }) } |
| 34 | })) |
| 35 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 36 | Layer.provide(Layer.succeed( |
| 37 | HttpClient.HttpClient, |
| 38 | makeHttpClient((request) => |
| 39 | Effect.gen(function*() { |
| 40 | const body = yield* getRequestBody(request) |
| 41 | assert.deepStrictEqual(body.tools.map((tool: any) => tool.function.name), ["ReadEnv"]) |
| 42 | assert.strictEqual(body.tools[0].function.parameters.properties.env.type, "array") |
| 43 | return mode === "complete" |
| 44 | ? jsonResponse( |
| 45 | request, |
| 46 | makeChatCompletion({ |
| 47 | choices: [{ |
| 48 | index: 0, |
| 49 | finish_reason: "tool_calls", |
| 50 | message: { role: "assistant", content: null, tool_calls: calls } |
| 51 | }] |
| 52 | }) |
| 53 | ) |
| 54 | : sseResponse(request, [{ |
| 55 | id: "chatcmpl_env", |
| 56 | object: "chat.completion.chunk", |
| 57 | model: "gpt-4o-mini", |
| 58 | created: 1, |
| 59 | choices: [{ |
| 60 | index: 0, |
| 61 | delta: { tool_calls: calls.map((call, index) => ({ ...call, index })) }, |
| 62 | finish_reason: "tool_calls" |
| 63 | }] |
| 64 | }, "[DONE]"]) |
| 65 | }) |
| 66 | ) |
| 67 | )) |
| 68 | ) |
| 69 | for (let request = 1; request <= 2; request++) { |
| 70 | const options = { |
| 71 | prompt: "read the environment", |
| 72 | toolkit, |
| 73 | toolChoice: { oneOf: ["ReadEnv"] as Array<"ReadEnv"> }, |
| 74 | disableToolCallResolution: true |
| 75 | } |
| 76 | const calls = yield* (mode === "complete" |
| 77 | ? LanguageModel.generateText(options).pipe( |
| 78 | Effect.map((result) => result.toolCalls.map((part) => ({ id: part.id, params: part.params }))) |
| 79 | ) |
| 80 | : LanguageModel.streamText(options).pipe( |
| 81 | Stream.runCollect, |
| 82 | Effect.map((parts) => |
| 83 | parts.filter((part) => part.type === "tool-call").map((part) => ({ id: part.id, params: part.params })) |
| 84 | ) |
| 85 | )).pipe( |
| 86 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 87 | Effect.provide(toolkitLayer), |
| 88 | Effect.provide(layer) |
| 89 | ) |
| 90 | assert.deepStrictEqual( |
| 91 | calls, |
| 92 | [ |
| 93 | { id: "call_1", params: { env: { PATH: "/usr/bin" } } }, |
| 94 | { id: "call_2", params: { env: { PATH: "/usr/bin" } } } |
| 95 | ] |
| 96 | ) |
| 97 | assert.strictEqual(conversions, request) |
| 98 | } |
| 99 | })) |
| 100 | } |
| 101 | |
| 102 | describe("generateText", () => { |
| 103 | it.effect("serializes text-only user messages and decodes text output", () => |
| 104 | Effect.gen(function*() { |
| 105 | let capturedRequest: HttpClientRequest.HttpClientRequest | undefined |
| 106 | |
| 107 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 108 | Layer.provide(Layer.succeed( |
| 109 | HttpClient.HttpClient, |
| 110 | makeHttpClient((request) => { |
| 111 | capturedRequest = request |
| 112 | return Effect.succeed(jsonResponse( |
| 113 | request, |
| 114 | makeChatCompletion({ |
| 115 | choices: [{ |
| 116 | index: 0, |
| 117 | finish_reason: "stop", |
| 118 | message: { |
| 119 | role: "assistant", |
| 120 | content: "Hello, compat!" |
| 121 | } |
| 122 | }] |
| 123 | }) |
| 124 | )) |
| 125 | }) |
| 126 | )) |
| 127 | ) |
| 128 | |
| 129 | const result = yield* LanguageModel.generateText({ |
| 130 | prompt: Prompt.make([ |
| 131 | { role: "user", content: [Prompt.textPart({ text: "hello" })] }, |
| 132 | { role: "user", content: [Prompt.textPart({ text: "" })] }, |
| 133 | { role: "user", content: [Prompt.textPart({ text: " Hello, 世界!\nGoodbye. " })] }, |
| 134 | { role: "user", content: [Prompt.textPart({ text: "first" }), Prompt.textPart({ text: "second" })] }, |
| 135 | { role: "user", content: [] } |
| 136 | ]) |
| 137 | }).pipe( |
| 138 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 139 | Effect.provide(layer) |
| 140 | ) |
| 141 | |
| 142 | assert.strictEqual(result.text, "Hello, compat!") |
| 143 | assert.isDefined(capturedRequest) |
| 144 | if (capturedRequest === undefined) { |
| 145 | return |
| 146 | } |
| 147 | |
| 148 | const requestBody = yield* getRequestBody(capturedRequest) |
| 149 | assert.strictEqual(requestBody.model, "gpt-4o-mini") |
| 150 | assert.deepStrictEqual(requestBody.messages, [ |
| 151 | { role: "user", content: "hello" }, |
| 152 | { role: "user", content: "" }, |
| 153 | { role: "user", content: " Hello, 世界!\nGoodbye. " }, |
| 154 | { role: "user", content: "first\nsecond" }, |
| 155 | { role: "user", content: "" } |
| 156 | ]) |
| 157 | })) |
| 158 | |
| 159 | it.effect("routes invalid tool call params through failureMode: return without failing the effect", () => |
| 160 | Effect.gen(function*() { |
| 161 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 162 | Layer.provide(Layer.succeed( |
| 163 | HttpClient.HttpClient, |
| 164 | makeHttpClient((request) => |
| 165 | Effect.succeed(jsonResponse( |
| 166 | request, |
| 167 | makeChatCompletion({ |
| 168 | choices: [{ |
| 169 | index: 0, |
| 170 | finish_reason: "tool_calls", |
| 171 | message: { |
| 172 | role: "assistant", |
| 173 | content: null, |
| 174 | tool_calls: [{ |
| 175 | id: "call_1", |
| 176 | type: "function", |
| 177 | function: { |
| 178 | name: "ReturnModeTool", |
| 179 | arguments: JSON.stringify({ input: 123 }) |
| 180 | } |
| 181 | }] |
| 182 | } |
| 183 | }] |
| 184 | }) |
| 185 | )) |
| 186 | ) |
| 187 | )) |
| 188 | ) |
| 189 | |
| 190 | const ReturnModeTool = Tool.make("ReturnModeTool", { |
| 191 | description: "A test tool", |
| 192 | failureMode: "return", |
| 193 | parameters: Schema.Struct({ input: Schema.String }), |
| 194 | success: Schema.Struct({ output: Schema.String }), |
| 195 | failure: Schema.Struct({ error: Schema.String }) |
| 196 | }) |
| 197 | |
| 198 | const toolkit = Toolkit.make(ReturnModeTool) |
| 199 | const toolkitLayer = toolkit.toLayer({ |
| 200 | ReturnModeTool: ({ input }) => Effect.succeed({ output: `processed: ${input}` }) |
| 201 | }) |
| 202 | |
| 203 | const result = yield* LanguageModel.generateText({ |
| 204 | prompt: "use the tool", |
| 205 | toolkit |
| 206 | }).pipe( |
| 207 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 208 | Effect.provide(toolkitLayer), |
| 209 | Effect.provide(layer) |
| 210 | ) |
| 211 | |
| 212 | assert.strictEqual(result.toolResults.length, 1) |
| 213 | const toolResult = result.toolResults[0]! |
| 214 | assert.strictEqual(toolResult.isFailure, true) |
| 215 | const failure = toolResult.result as AiError.AiError |
| 216 | assert.strictEqual(failure._tag, "AiError") |
| 217 | assert.strictEqual(failure.reason._tag, "ToolParameterValidationError") |
| 218 | })) |
| 219 | |
| 220 | it.effect("forwards reasoning config to chat completions request", () => |
| 221 | Effect.gen(function*() { |
| 222 | let capturedRequest: HttpClientRequest.HttpClientRequest | undefined |
| 223 | |
| 224 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 225 | Layer.provide(Layer.succeed( |
| 226 | HttpClient.HttpClient, |
| 227 | makeHttpClient((request) => { |
| 228 | capturedRequest = request |
| 229 | return Effect.succeed(jsonResponse( |
| 230 | request, |
| 231 | makeChatCompletion({ |
| 232 | choices: [{ |
| 233 | index: 0, |
| 234 | finish_reason: "stop", |
| 235 | message: { |
| 236 | role: "assistant", |
| 237 | content: "Done" |
| 238 | } |
| 239 | }] |
| 240 | }) |
| 241 | )) |
| 242 | }) |
| 243 | )) |
| 244 | ) |
| 245 | |
| 246 | yield* LanguageModel.generateText({ prompt: "hello" }).pipe( |
| 247 | Effect.provide(OpenAiLanguageModel.model("gpt-5", { |
| 248 | reasoning: { |
| 249 | effort: "medium", |
| 250 | summary: "auto" |
| 251 | } |
| 252 | })), |
| 253 | Effect.provide(layer) |
| 254 | ) |
| 255 | |
| 256 | assert.isDefined(capturedRequest) |
| 257 | if (capturedRequest === undefined) { |
| 258 | return |
| 259 | } |
| 260 | |
| 261 | const requestBody = yield* getRequestBody(capturedRequest) |
| 262 | assert.deepStrictEqual(requestBody.reasoning, { |
| 263 | effort: "medium", |
| 264 | summary: "auto" |
| 265 | }) |
| 266 | })) |
| 267 | |
| 268 | it.effect("forwards custom model config properties to chat completions request", () => |
| 269 | Effect.gen(function*() { |
| 270 | let capturedRequest: HttpClientRequest.HttpClientRequest | undefined |
| 271 | |
| 272 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 273 | Layer.provide(Layer.succeed( |
| 274 | HttpClient.HttpClient, |
| 275 | makeHttpClient((request) => { |
| 276 | capturedRequest = request |
| 277 | return Effect.succeed(jsonResponse( |
| 278 | request, |
| 279 | makeChatCompletion({ |
| 280 | choices: [{ |
| 281 | index: 0, |
| 282 | finish_reason: "stop", |
| 283 | message: { |
| 284 | role: "assistant", |
| 285 | content: "Done" |
| 286 | } |
| 287 | }] |
| 288 | }) |
| 289 | )) |
| 290 | }) |
| 291 | )) |
| 292 | ) |
| 293 | |
| 294 | yield* LanguageModel.generateText({ prompt: "hello" }).pipe( |
| 295 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini", { |
| 296 | vendor_setting: { |
| 297 | mode: "strict" |
| 298 | } |
| 299 | })), |
| 300 | Effect.provide(layer) |
| 301 | ) |
| 302 | |
| 303 | assert.isDefined(capturedRequest) |
| 304 | if (capturedRequest === undefined) { |
| 305 | return |
| 306 | } |
| 307 | |
| 308 | const requestBody = yield* getRequestBody(capturedRequest) |
| 309 | assert.deepStrictEqual(requestBody.vendor_setting, { |
| 310 | mode: "strict" |
| 311 | }) |
| 312 | })) |
| 313 | |
| 314 | it.effect("preserves URL and base64 images in multimodal content order", () => |
| 315 | Effect.gen(function*() { |
| 316 | const base64 = "iVBORw0KGgo=" |
| 317 | let capturedRequest: HttpClientRequest.HttpClientRequest | undefined |
| 318 | |
| 319 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 320 | Layer.provide(Layer.succeed( |
| 321 | HttpClient.HttpClient, |
| 322 | makeHttpClient((request) => { |
| 323 | capturedRequest = request |
| 324 | return Effect.succeed(jsonResponse( |
| 325 | request, |
| 326 | makeChatCompletion({ |
| 327 | choices: [{ |
| 328 | index: 0, |
| 329 | finish_reason: "stop", |
| 330 | message: { |
| 331 | role: "assistant", |
| 332 | content: "done" |
| 333 | } |
| 334 | }] |
| 335 | }) |
| 336 | )) |
| 337 | }) |
| 338 | )) |
| 339 | ) |
| 340 | |
| 341 | yield* LanguageModel.generateText({ |
| 342 | prompt: Prompt.make([{ |
| 343 | role: "user", |
| 344 | content: [ |
| 345 | Prompt.textPart({ text: "first text" }), |
| 346 | Prompt.filePart({ |
| 347 | mediaType: "image/png", |
| 348 | data: new URL("https://example.com/image.png") |
| 349 | }), |
| 350 | Prompt.filePart({ mediaType: "image/png", data: "https://example.com/string-image.png" }), |
| 351 | Prompt.filePart({ mediaType: "image/png", data: base64 }), |
| 352 | Prompt.textPart({ text: "second text" }) |
| 353 | ] |
| 354 | }]) |
| 355 | }).pipe( |
| 356 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 357 | Effect.provide(layer) |
| 358 | ) |
| 359 | |
| 360 | assert.isDefined(capturedRequest) |
| 361 | if (capturedRequest === undefined) { |
| 362 | return |
| 363 | } |
| 364 | |
| 365 | const requestBody = yield* getRequestBody(capturedRequest) |
| 366 | const content = requestBody.messages[0]?.content |
| 367 | assert.isTrue(Array.isArray(content)) |
| 368 | assert.deepStrictEqual(content, [ |
| 369 | { |
| 370 | type: "text", |
| 371 | text: "first text" |
| 372 | }, |
| 373 | { |
| 374 | type: "image_url", |
| 375 | image_url: { |
| 376 | url: "https://example.com/image.png", |
| 377 | detail: "auto" |
| 378 | } |
| 379 | }, |
| 380 | { |
| 381 | type: "image_url", |
| 382 | image_url: { |
| 383 | url: "https://example.com/string-image.png", |
| 384 | detail: "auto" |
| 385 | } |
| 386 | }, |
| 387 | { |
| 388 | type: "image_url", |
| 389 | image_url: { |
| 390 | url: `data:image/png;base64,${base64}`, |
| 391 | detail: "auto" |
| 392 | } |
| 393 | }, |
| 394 | { |
| 395 | type: "text", |
| 396 | text: "second text" |
| 397 | } |
| 398 | ]) |
| 399 | })) |
| 400 | |
| 401 | it.effect("normalizes empty assistant message content to an empty string", () => |
| 402 | Effect.gen(function*() { |
| 403 | let capturedRequest: HttpClientRequest.HttpClientRequest | undefined |
| 404 | |
| 405 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 406 | Layer.provide(Layer.succeed( |
| 407 | HttpClient.HttpClient, |
| 408 | makeHttpClient((request) => { |
| 409 | capturedRequest = request |
| 410 | return Effect.succeed(jsonResponse( |
| 411 | request, |
| 412 | makeChatCompletion({ |
| 413 | choices: [{ |
| 414 | index: 0, |
| 415 | finish_reason: "stop", |
| 416 | message: { |
| 417 | role: "assistant", |
| 418 | content: "done" |
| 419 | } |
| 420 | }] |
| 421 | }) |
| 422 | )) |
| 423 | }) |
| 424 | )) |
| 425 | ) |
| 426 | |
| 427 | yield* LanguageModel.generateText({ |
| 428 | prompt: Prompt.make([ |
| 429 | { role: "user", content: "hello" }, |
| 430 | { |
| 431 | role: "assistant", |
| 432 | content: [Prompt.textPart({ text: "" })] |
| 433 | }, |
| 434 | { role: "user", content: "continue" } |
| 435 | ]) |
| 436 | }).pipe( |
| 437 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 438 | Effect.provide(layer) |
| 439 | ) |
| 440 | |
| 441 | assert.isDefined(capturedRequest) |
| 442 | if (capturedRequest === undefined) { |
| 443 | return |
| 444 | } |
| 445 | |
| 446 | const requestBody = yield* getRequestBody(capturedRequest) |
| 447 | const assistantMessage = requestBody.messages.find((message: any) => message.role === "assistant") |
| 448 | assert.isDefined(assistantMessage) |
| 449 | assert.strictEqual(assistantMessage.content, "") |
| 450 | assert.isUndefined(assistantMessage.tool_calls) |
| 451 | })) |
| 452 | |
| 453 | it.effect("maps function_call output to tool-call part and sends function tool schema", () => |
| 454 | Effect.gen(function*() { |
| 455 | let capturedRequest: HttpClientRequest.HttpClientRequest | undefined |
| 456 | |
| 457 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 458 | Layer.provide(Layer.succeed( |
| 459 | HttpClient.HttpClient, |
| 460 | makeHttpClient((request) => { |
| 461 | capturedRequest = request |
| 462 | return Effect.succeed(jsonResponse( |
| 463 | request, |
| 464 | makeChatCompletion({ |
| 465 | choices: [{ |
| 466 | index: 0, |
| 467 | finish_reason: "tool_calls", |
| 468 | message: { |
| 469 | role: "assistant", |
| 470 | content: null, |
| 471 | tool_calls: [{ |
| 472 | id: "call_1", |
| 473 | type: "function", |
| 474 | function: { |
| 475 | name: "TestTool", |
| 476 | arguments: JSON.stringify({ input: "hello" }) |
| 477 | } |
| 478 | }] |
| 479 | } |
| 480 | }] |
| 481 | }) |
| 482 | )) |
| 483 | }) |
| 484 | )) |
| 485 | ) |
| 486 | |
| 487 | const result = yield* LanguageModel.generateText({ |
| 488 | prompt: "use the tool", |
| 489 | toolkit: TestToolkit, |
| 490 | disableToolCallResolution: true |
| 491 | }).pipe( |
| 492 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 493 | Effect.provide(TestToolkitLayer), |
| 494 | Effect.provide(layer) |
| 495 | ) |
| 496 | |
| 497 | const toolCall = result.content.find((part) => part.type === "tool-call") |
| 498 | assert.isDefined(toolCall) |
| 499 | if (toolCall?.type !== "tool-call") { |
| 500 | return |
| 501 | } |
| 502 | |
| 503 | assert.strictEqual(toolCall.name, "TestTool") |
| 504 | assert.deepStrictEqual(toolCall.params, { input: "hello" }) |
| 505 | |
| 506 | assert.isDefined(capturedRequest) |
| 507 | if (capturedRequest === undefined) { |
| 508 | return |
| 509 | } |
| 510 | |
| 511 | const requestBody = yield* getRequestBody(capturedRequest) |
| 512 | const functionTool = requestBody.tools.find((tool: any) => tool.type === "function") |
| 513 | assert.isDefined(functionTool) |
| 514 | assert.strictEqual(functionTool.function.name, "TestTool") |
| 515 | assert.strictEqual(functionTool.function.strict, true) |
| 516 | })) |
| 517 | |
| 518 | it.effect("decodes tool call params with the OpenAI codec", () => |
| 519 | Effect.gen(function*() { |
| 520 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 521 | Layer.provide(Layer.succeed( |
| 522 | HttpClient.HttpClient, |
| 523 | makeHttpClient((request) => |
| 524 | Effect.succeed(jsonResponse( |
| 525 | request, |
| 526 | makeChatCompletion({ |
| 527 | choices: [{ |
| 528 | index: 0, |
| 529 | finish_reason: "tool_calls", |
| 530 | message: { |
| 531 | role: "assistant", |
| 532 | content: null, |
| 533 | tool_calls: [{ |
| 534 | id: "call_record_1", |
| 535 | type: "function", |
| 536 | function: { |
| 537 | name: "RecordTool", |
| 538 | arguments: JSON.stringify({ env: [{ 0: "PATH", 1: "/usr/bin" }] }) |
| 539 | } |
| 540 | }] |
| 541 | } |
| 542 | }] |
| 543 | }) |
| 544 | )) |
| 545 | ) |
| 546 | )) |
| 547 | ) |
| 548 | |
| 549 | const result = yield* LanguageModel.generateText({ |
| 550 | prompt: "read the environment", |
| 551 | toolkit: RecordToolkit, |
| 552 | disableToolCallResolution: true |
| 553 | }).pipe( |
| 554 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 555 | Effect.provide(RecordToolkitLayer), |
| 556 | Effect.provide(layer) |
| 557 | ) |
| 558 | |
| 559 | const toolCall = result.content.find((part) => part.type === "tool-call") |
| 560 | assert.isDefined(toolCall) |
| 561 | if (toolCall?.type !== "tool-call") { |
| 562 | return |
| 563 | } |
| 564 | assert.deepStrictEqual(toolCall.params, { env: { PATH: "/usr/bin" } }) |
| 565 | })) |
| 566 | |
| 567 | it.effect("preserves raw JSON Schema dynamic tool call params", () => |
| 568 | Effect.gen(function*() { |
| 569 | const params = { query: "effect" } |
| 570 | const client = makeHttpClient((request) => |
| 571 | Effect.succeed(jsonResponse( |
| 572 | request, |
| 573 | makeChatCompletion({ |
| 574 | choices: [{ |
| 575 | index: 0, |
| 576 | finish_reason: "tool_calls", |
| 577 | message: { |
| 578 | role: "assistant", |
| 579 | content: null, |
| 580 | tool_calls: [{ |
| 581 | id: "call_dynamic_1", |
| 582 | type: "function", |
| 583 | function: { name: "DynamicTool", arguments: JSON.stringify(params) } |
| 584 | }] |
| 585 | } |
| 586 | }] |
| 587 | }) |
| 588 | )) |
| 589 | ) |
| 590 | const DynamicTool = Tool.dynamic("DynamicTool", { |
| 591 | parameters: { |
| 592 | type: "object", |
| 593 | properties: { query: { type: "string" } }, |
| 594 | required: ["query"], |
| 595 | additionalProperties: false |
| 596 | } as const |
| 597 | }) |
| 598 | const result = yield* LanguageModel.generateText({ |
| 599 | prompt: "use the dynamic tool", |
| 600 | toolkit: Toolkit.make(DynamicTool), |
| 601 | disableToolCallResolution: true |
| 602 | }).pipe( |
| 603 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 604 | Effect.provide(OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") })), |
| 605 | Effect.provideService(HttpClient.HttpClient, client) |
| 606 | ) |
| 607 | |
| 608 | assert.deepStrictEqual(result.toolCalls[0]?.params, params) |
| 609 | })) |
| 610 | |
| 611 | it.effect("decodes Effect Schema dynamic tool call params with the OpenAI codec", () => |
| 612 | Effect.gen(function*() { |
| 613 | const DynamicTool = Tool.dynamic("DynamicTool", { |
| 614 | parameters: Schema.Struct({ |
| 615 | env: Schema.Record(Schema.String, Schema.String) |
| 616 | }) |
| 617 | }) |
| 618 | const client = makeHttpClient((request) => |
| 619 | Effect.succeed(jsonResponse( |
| 620 | request, |
| 621 | makeChatCompletion({ |
| 622 | choices: [{ |
| 623 | index: 0, |
| 624 | finish_reason: "tool_calls", |
| 625 | message: { |
| 626 | role: "assistant", |
| 627 | content: null, |
| 628 | tool_calls: [{ |
| 629 | id: "call_dynamic_1", |
| 630 | type: "function", |
| 631 | function: { |
| 632 | name: "DynamicTool", |
| 633 | arguments: JSON.stringify({ env: [{ 0: "PATH", 1: "/usr/bin" }] }) |
| 634 | } |
| 635 | }] |
| 636 | } |
| 637 | }] |
| 638 | }) |
| 639 | )) |
| 640 | ) |
| 641 | const result = yield* LanguageModel.generateText({ |
| 642 | prompt: "use the dynamic tool", |
| 643 | toolkit: Toolkit.make(DynamicTool), |
| 644 | disableToolCallResolution: true |
| 645 | }).pipe( |
| 646 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 647 | Effect.provide(OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") })), |
| 648 | Effect.provideService(HttpClient.HttpClient, client) |
| 649 | ) |
| 650 | |
| 651 | assert.deepStrictEqual(result.toolCalls[0]?.params, { env: { PATH: "/usr/bin" } }) |
| 652 | })) |
| 653 | |
| 654 | it.effect("groups parallel tool calls into one assistant message", () => |
| 655 | Effect.gen(function*() { |
| 656 | let capturedRequest: HttpClientRequest.HttpClientRequest | undefined |
| 657 | |
| 658 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 659 | Layer.provide(Layer.succeed( |
| 660 | HttpClient.HttpClient, |
| 661 | makeHttpClient((request) => { |
| 662 | capturedRequest = request |
| 663 | return Effect.succeed(jsonResponse(request, makeChatCompletion())) |
| 664 | }) |
| 665 | )) |
| 666 | ) |
| 667 | |
| 668 | yield* LanguageModel.generateText({ |
| 669 | prompt: Prompt.make([ |
| 670 | { role: "user", content: "use both tools" }, |
| 671 | { |
| 672 | role: "assistant", |
| 673 | content: [ |
| 674 | Prompt.toolCallPart({ |
| 675 | id: "call_1", |
| 676 | name: "TestTool", |
| 677 | params: { input: "first" }, |
| 678 | providerExecuted: false |
| 679 | }), |
| 680 | Prompt.toolCallPart({ |
| 681 | id: "call_2", |
| 682 | name: "TestTool", |
| 683 | params: { input: "second" }, |
| 684 | providerExecuted: false |
| 685 | }) |
| 686 | ] |
| 687 | }, |
| 688 | { |
| 689 | role: "tool", |
| 690 | content: [ |
| 691 | Prompt.toolResultPart({ |
| 692 | id: "call_1", |
| 693 | name: "TestTool", |
| 694 | isFailure: false, |
| 695 | result: { output: "first" }, |
| 696 | providerExecuted: false |
| 697 | }), |
| 698 | Prompt.toolResultPart({ |
| 699 | id: "call_2", |
| 700 | name: "TestTool", |
| 701 | isFailure: false, |
| 702 | result: { output: "second" }, |
| 703 | providerExecuted: false |
| 704 | }) |
| 705 | ] |
| 706 | } |
| 707 | ]), |
| 708 | toolkit: TestToolkit |
| 709 | }).pipe( |
| 710 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 711 | Effect.provide(TestToolkitLayer), |
| 712 | Effect.provide(layer) |
| 713 | ) |
| 714 | |
| 715 | assert.isDefined(capturedRequest) |
| 716 | if (capturedRequest === undefined) { |
| 717 | return |
| 718 | } |
| 719 | |
| 720 | const requestBody = yield* getRequestBody(capturedRequest) |
| 721 | assert.deepStrictEqual(requestBody.messages, [ |
| 722 | { role: "user", content: "use both tools" }, |
| 723 | { |
| 724 | role: "assistant", |
| 725 | content: null, |
| 726 | tool_calls: [ |
| 727 | { |
| 728 | id: "call_1", |
| 729 | type: "function", |
| 730 | function: { name: "TestTool", arguments: JSON.stringify({ input: "first" }) } |
| 731 | }, |
| 732 | { |
| 733 | id: "call_2", |
| 734 | type: "function", |
| 735 | function: { name: "TestTool", arguments: JSON.stringify({ input: "second" }) } |
| 736 | } |
| 737 | ] |
| 738 | }, |
| 739 | { role: "tool", tool_call_id: "call_1", content: JSON.stringify({ output: "first" }) }, |
| 740 | { role: "tool", tool_call_id: "call_2", content: JSON.stringify({ output: "second" }) } |
| 741 | ]) |
| 742 | })) |
| 743 | |
| 744 | const modelOutputCases = [ |
| 745 | { |
| 746 | label: "as text with file placeholders", |
| 747 | modelOutput: [ |
| 748 | Prompt.textPart({ text: "Screenshot" }), |
| 749 | Prompt.filePart({ mediaType: "image/png", fileName: "screenshot.png", data: "iVBORw0KGgo=" }) |
| 750 | ], |
| 751 | content: "Screenshot\n[image/png screenshot.png omitted]" |
| 752 | }, |
| 753 | { label: "as an empty string when it is empty", modelOutput: [], content: "" } |
| 754 | ] |
| 755 | |
| 756 | for (const { content, label, modelOutput } of modelOutputCases) { |
| 757 | it.effect(`sends tool result model output ${label}`, () => |
| 758 | Effect.gen(function*() { |
| 759 | let capturedRequest: HttpClientRequest.HttpClientRequest | undefined |
| 760 | |
| 761 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 762 | Layer.provide(Layer.succeed( |
| 763 | HttpClient.HttpClient, |
| 764 | makeHttpClient((request) => { |
| 765 | capturedRequest = request |
| 766 | return Effect.succeed(jsonResponse(request, makeChatCompletion())) |
| 767 | }) |
| 768 | )) |
| 769 | ) |
| 770 | |
| 771 | yield* LanguageModel.generateText({ |
| 772 | prompt: Prompt.make([ |
| 773 | { |
| 774 | role: "assistant", |
| 775 | content: [Prompt.toolCallPart({ |
| 776 | id: "call_1", |
| 777 | name: "TestTool", |
| 778 | params: { input: "screenshot" }, |
| 779 | providerExecuted: false |
| 780 | })] |
| 781 | }, |
| 782 | { |
| 783 | role: "tool", |
| 784 | content: [Prompt.toolResultPart({ |
| 785 | id: "call_1", |
| 786 | name: "TestTool", |
| 787 | isFailure: false, |
| 788 | result: { output: "APPLICATION_ONLY" }, |
| 789 | modelOutput, |
| 790 | providerExecuted: false |
| 791 | })] |
| 792 | } |
| 793 | ]), |
| 794 | toolkit: TestToolkit, |
| 795 | disableToolCallResolution: true |
| 796 | }).pipe( |
| 797 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 798 | Effect.provide(layer) |
| 799 | ) |
| 800 | |
| 801 | assert.isDefined(capturedRequest) |
| 802 | if (capturedRequest === undefined) { |
| 803 | return |
| 804 | } |
| 805 | |
| 806 | const requestBody = yield* getRequestBody(capturedRequest) |
| 807 | assert.deepStrictEqual(requestBody.messages[1], { |
| 808 | role: "tool", |
| 809 | tool_call_id: "call_1", |
| 810 | content |
| 811 | }) |
| 812 | })) |
| 813 | } |
| 814 | |
| 815 | it.effect("echoes tool call extra_content in follow-up requests", () => |
| 816 | Effect.gen(function*() { |
| 817 | const extraContent = { google: { thought_signature: "signature-1" } } |
| 818 | const requests: Array<HttpClientRequest.HttpClientRequest> = [] |
| 819 | |
| 820 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 821 | Layer.provide(Layer.succeed( |
| 822 | HttpClient.HttpClient, |
| 823 | makeHttpClient((request) => { |
| 824 | requests.push(request) |
| 825 | return Effect.succeed(jsonResponse( |
| 826 | request, |
| 827 | requests.length > 1 ? makeChatCompletion() : makeChatCompletion({ |
| 828 | choices: [{ |
| 829 | index: 0, |
| 830 | finish_reason: "tool_calls", |
| 831 | message: { |
| 832 | role: "assistant", |
| 833 | content: null, |
| 834 | tool_calls: [{ |
| 835 | id: "call_1", |
| 836 | type: "function", |
| 837 | extra_content: extraContent, |
| 838 | function: { name: "TestTool", arguments: JSON.stringify({ input: "hello" }) } |
| 839 | }] |
| 840 | } |
| 841 | }] |
| 842 | }) |
| 843 | )) |
| 844 | }) |
| 845 | )) |
| 846 | ) |
| 847 | |
| 848 | const generate = (prompt: Prompt.Prompt) => |
| 849 | LanguageModel.generateText({ prompt, toolkit: TestToolkit, disableToolCallResolution: true }).pipe( |
| 850 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 851 | Effect.provide(layer) |
| 852 | ) |
| 853 | |
| 854 | const prompt = Prompt.make("use the tool") |
| 855 | const first = yield* generate(prompt) |
| 856 | yield* generate(Prompt.concat(prompt, Prompt.fromResponseParts(first.content))) |
| 857 | |
| 858 | const followUpBody = yield* getRequestBody(requests[1]) |
| 859 | assert.deepStrictEqual(followUpBody.messages[1].tool_calls[0].extra_content, extraContent) |
| 860 | })) |
| 861 | |
| 862 | it.effect("converts dynamic tools to function type", () => |
| 863 | Effect.gen(function*() { |
| 864 | let capturedRequest: HttpClientRequest.HttpClientRequest | undefined |
| 865 | |
| 866 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 867 | Layer.provide(Layer.succeed( |
| 868 | HttpClient.HttpClient, |
| 869 | makeHttpClient((request) => { |
| 870 | capturedRequest = request |
| 871 | return Effect.succeed(jsonResponse( |
| 872 | request, |
| 873 | makeChatCompletion({ |
| 874 | choices: [{ |
| 875 | index: 0, |
| 876 | finish_reason: "stop", |
| 877 | message: { |
| 878 | role: "assistant", |
| 879 | content: "Done" |
| 880 | } |
| 881 | }] |
| 882 | }) |
| 883 | )) |
| 884 | }) |
| 885 | )) |
| 886 | ) |
| 887 | |
| 888 | const inputSchema = { |
| 889 | type: "object", |
| 890 | properties: { |
| 891 | query: { type: "string" }, |
| 892 | limit: { type: "number" } |
| 893 | }, |
| 894 | required: ["query"], |
| 895 | additionalProperties: false |
| 896 | } as const |
| 897 | |
| 898 | const DynamicTool = Tool.dynamic("DynamicTool", { |
| 899 | description: "A dynamic tool", |
| 900 | parameters: inputSchema |
| 901 | }) |
| 902 | |
| 903 | yield* LanguageModel.generateText({ |
| 904 | prompt: "use dynamic tool", |
| 905 | toolkit: Toolkit.make(DynamicTool), |
| 906 | disableToolCallResolution: true |
| 907 | }).pipe( |
| 908 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 909 | Effect.provide(layer) |
| 910 | ) |
| 911 | |
| 912 | assert.isDefined(capturedRequest) |
| 913 | if (capturedRequest === undefined) { |
| 914 | return |
| 915 | } |
| 916 | |
| 917 | const requestBody = yield* getRequestBody(capturedRequest) |
| 918 | const functionTool = requestBody.tools?.find((tool: any) => |
| 919 | tool.type === "function" && tool.function?.name === "DynamicTool" |
| 920 | ) |
| 921 | assert.isDefined(functionTool) |
| 922 | assert.strictEqual(functionTool.function.description, "A dynamic tool") |
| 923 | assert.deepStrictEqual(functionTool.function.parameters, inputSchema) |
| 924 | })) |
| 925 | |
| 926 | it.effect("names the tool when its parameters are not an object schema", () => |
| 927 | Effect.gen(function*() { |
| 928 | const error = yield* LanguageModel.generateText({ |
| 929 | prompt: "Use the tool", |
| 930 | toolkit: Toolkit.make(Tool.make("NoParams", { parameters: Schema.Struct({}) })), |
| 931 | disableToolCallResolution: true |
| 932 | }).pipe( |
| 933 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 934 | Effect.provide(OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") })), |
| 935 | Effect.provideService(HttpClient.HttpClient, makeHttpClient(() => Effect.die("unexpected request"))), |
| 936 | Effect.flip |
| 937 | ) |
| 938 | |
| 939 | assert.strictEqual(error.reason._tag, "UnsupportedSchemaError") |
| 940 | assert.include(error.message, `"NoParams"`) |
| 941 | assert.include(error.message, "Tool.EmptyParams") |
| 942 | })) |
| 943 | |
| 944 | it.effect("maps provider apply_patch function call back to custom provider-defined tool", () => |
| 945 | Effect.gen(function*() { |
| 946 | let capturedRequest: HttpClientRequest.HttpClientRequest | undefined |
| 947 | |
| 948 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 949 | Layer.provide(Layer.succeed( |
| 950 | HttpClient.HttpClient, |
| 951 | makeHttpClient((request) => { |
| 952 | capturedRequest = request |
| 953 | return Effect.succeed(jsonResponse( |
| 954 | request, |
| 955 | makeChatCompletion({ |
| 956 | choices: [{ |
| 957 | index: 0, |
| 958 | finish_reason: "tool_calls", |
| 959 | message: { |
| 960 | role: "assistant", |
| 961 | content: null, |
| 962 | tool_calls: [{ |
| 963 | id: "call_1", |
| 964 | type: "function", |
| 965 | function: { |
| 966 | name: "apply_patch", |
| 967 | arguments: JSON.stringify({ |
| 968 | call_id: "call_1", |
| 969 | operation: { |
| 970 | type: "delete_file", |
| 971 | path: "src/obsolete.ts" |
| 972 | } |
| 973 | }) |
| 974 | } |
| 975 | }] |
| 976 | } |
| 977 | }] |
| 978 | }) |
| 979 | )) |
| 980 | }) |
| 981 | )) |
| 982 | ) |
| 983 | |
| 984 | const toolkit = Toolkit.make(CompatApplyPatchTool({})) |
| 985 | const toolkitLayer = toolkit.toLayer({ |
| 986 | CompatApplyPatch: () => |
| 987 | Effect.succeed({ |
| 988 | status: "completed", |
| 989 | output: "deleted" |
| 990 | }) |
| 991 | }) |
| 992 | |
| 993 | const result = yield* LanguageModel.generateText({ |
| 994 | prompt: "delete src/obsolete.ts", |
| 995 | toolkit, |
| 996 | toolChoice: { tool: "CompatApplyPatch" }, |
| 997 | disableToolCallResolution: true |
| 998 | }).pipe( |
| 999 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1000 | Effect.provide(toolkitLayer), |
| 1001 | Effect.provide(layer) |
| 1002 | ) |
| 1003 | |
| 1004 | const toolCall = result.content.find((part) => part.type === "tool-call") |
| 1005 | assert.isDefined(toolCall) |
| 1006 | if (toolCall?.type !== "tool-call") { |
| 1007 | return |
| 1008 | } |
| 1009 | |
| 1010 | assert.strictEqual(toolCall.name, "CompatApplyPatch") |
| 1011 | assert.deepStrictEqual(toolCall.params, { |
| 1012 | call_id: "call_1", |
| 1013 | operation: { |
| 1014 | type: "delete_file", |
| 1015 | path: "src/obsolete.ts" |
| 1016 | } |
| 1017 | }) |
| 1018 | |
| 1019 | assert.isDefined(capturedRequest) |
| 1020 | if (capturedRequest === undefined) { |
| 1021 | return |
| 1022 | } |
| 1023 | |
| 1024 | const requestBody = yield* getRequestBody(capturedRequest) |
| 1025 | |
| 1026 | const functionTool = requestBody.tools.find((tool: any) => tool.type === "function") |
| 1027 | assert.isDefined(functionTool) |
| 1028 | assert.strictEqual(functionTool.function.name, "apply_patch") |
| 1029 | assert.deepStrictEqual(requestBody.tool_choice, { |
| 1030 | type: "function", |
| 1031 | function: { |
| 1032 | name: "apply_patch" |
| 1033 | } |
| 1034 | }) |
| 1035 | })) |
| 1036 | |
| 1037 | it.effect("decodes usage when token detail fields are absent", () => |
| 1038 | Effect.gen(function*() { |
| 1039 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 1040 | Layer.provide(Layer.succeed( |
| 1041 | HttpClient.HttpClient, |
| 1042 | makeHttpClient((request) => |
| 1043 | Effect.succeed(jsonResponse( |
| 1044 | request, |
| 1045 | makeChatCompletion({ |
| 1046 | choices: [{ |
| 1047 | index: 0, |
| 1048 | finish_reason: "stop", |
| 1049 | message: { |
| 1050 | role: "assistant", |
| 1051 | content: "Hello" |
| 1052 | } |
| 1053 | }], |
| 1054 | usage: { |
| 1055 | prompt_tokens: 4, |
| 1056 | completion_tokens: 5, |
| 1057 | total_tokens: 9, |
| 1058 | provider_future_field: true |
| 1059 | } |
| 1060 | }) |
| 1061 | )) |
| 1062 | ) |
| 1063 | )) |
| 1064 | ) |
| 1065 | |
| 1066 | const result = yield* LanguageModel.generateText({ prompt: "hello" }).pipe( |
| 1067 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1068 | Effect.provide(layer) |
| 1069 | ) |
| 1070 | |
| 1071 | const finish = result.content.find((part) => part.type === "finish") |
| 1072 | assert.isDefined(finish) |
| 1073 | if (finish?.type !== "finish") { |
| 1074 | return |
| 1075 | } |
| 1076 | |
| 1077 | assert.deepStrictEqual(finish.usage.inputTokens, { |
| 1078 | uncached: 4, |
| 1079 | total: 4, |
| 1080 | cacheRead: 0, |
| 1081 | cacheWrite: undefined |
| 1082 | }) |
| 1083 | assert.deepStrictEqual(finish.usage.outputTokens, { |
| 1084 | total: 5, |
| 1085 | text: 5, |
| 1086 | reasoning: 0 |
| 1087 | }) |
| 1088 | })) |
| 1089 | |
| 1090 | it.effect("surfaces reasoning from non-streaming responses", () => |
| 1091 | Effect.gen(function*() { |
| 1092 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 1093 | Layer.provide(Layer.succeed( |
| 1094 | HttpClient.HttpClient, |
| 1095 | makeHttpClient((request) => |
| 1096 | Effect.succeed(jsonResponse( |
| 1097 | request, |
| 1098 | makeChatCompletion({ |
| 1099 | choices: [{ |
| 1100 | index: 0, |
| 1101 | finish_reason: "stop", |
| 1102 | message: { |
| 1103 | role: "assistant", |
| 1104 | reasoning: "I should greet the user.", |
| 1105 | content: "Hello!" |
| 1106 | } |
| 1107 | }] |
| 1108 | }) |
| 1109 | )) |
| 1110 | ) |
| 1111 | )) |
| 1112 | ) |
| 1113 | |
| 1114 | const result = yield* LanguageModel.generateText({ prompt: "hello" }).pipe( |
| 1115 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1116 | Effect.provide(layer) |
| 1117 | ) |
| 1118 | |
| 1119 | const reasoning = result.content.find((part) => part.type === "reasoning") |
| 1120 | assert.isDefined(reasoning) |
| 1121 | if (reasoning?.type !== "reasoning") { |
| 1122 | return |
| 1123 | } |
| 1124 | assert.strictEqual(reasoning.text, "I should greet the user.") |
| 1125 | assert.strictEqual(result.text, "Hello!") |
| 1126 | })) |
| 1127 | |
| 1128 | it.effect("surfaces reasoning_content from non-streaming responses", () => |
| 1129 | Effect.gen(function*() { |
| 1130 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 1131 | Layer.provide(Layer.succeed( |
| 1132 | HttpClient.HttpClient, |
| 1133 | makeHttpClient((request) => |
| 1134 | Effect.succeed(jsonResponse( |
| 1135 | request, |
| 1136 | makeChatCompletion({ |
| 1137 | choices: [{ |
| 1138 | index: 0, |
| 1139 | finish_reason: "stop", |
| 1140 | message: { |
| 1141 | role: "assistant", |
| 1142 | reasoning_content: "I should greet the user.", |
| 1143 | content: "Hello!" |
| 1144 | } |
| 1145 | }] |
| 1146 | }) |
| 1147 | )) |
| 1148 | ) |
| 1149 | )) |
| 1150 | ) |
| 1151 | |
| 1152 | const result = yield* LanguageModel.generateText({ prompt: "hello" }).pipe( |
| 1153 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1154 | Effect.provide(layer) |
| 1155 | ) |
| 1156 | |
| 1157 | const reasoning = result.content.find((part) => part.type === "reasoning") |
| 1158 | assert.isDefined(reasoning) |
| 1159 | if (reasoning?.type !== "reasoning") { |
| 1160 | return |
| 1161 | } |
| 1162 | assert.strictEqual(reasoning.text, "I should greet the user.") |
| 1163 | assert.strictEqual(result.text, "Hello!") |
| 1164 | })) |
| 1165 | |
| 1166 | it.effect("returns invalid and undeclared tool calls when tool call validation is deferred", () => |
| 1167 | Effect.gen(function*() { |
| 1168 | const client = makeHttpClient((request) => |
| 1169 | Effect.succeed(jsonResponse( |
| 1170 | request, |
| 1171 | makeChatCompletion({ |
| 1172 | choices: [{ |
| 1173 | index: 0, |
| 1174 | finish_reason: "tool_calls", |
| 1175 | message: { |
| 1176 | role: "assistant", |
| 1177 | content: null, |
| 1178 | tool_calls: [ |
| 1179 | { id: "call_1", type: "function", function: { name: "TestTool", arguments: "{\"input\":42}" } }, |
| 1180 | { id: "call_2", type: "function", function: { name: "Write", arguments: "{\"path\":\"a\"}" } } |
| 1181 | ] |
| 1182 | } |
| 1183 | }] |
| 1184 | }) |
| 1185 | )) |
| 1186 | ) |
| 1187 | |
| 1188 | const result = yield* LanguageModel.generateText({ |
| 1189 | prompt: "use the tools", |
| 1190 | toolkit: TestToolkit, |
| 1191 | disableToolCallResolution: true, |
| 1192 | toolCallValidation: "deferred" |
| 1193 | }).pipe( |
| 1194 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1195 | Effect.provide(TestToolkitLayer), |
| 1196 | Effect.provide(OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") })), |
| 1197 | Effect.provideService(HttpClient.HttpClient, client) |
| 1198 | ) |
| 1199 | |
| 1200 | assert.deepStrictEqual(result.toolCalls.map(({ name, params }) => ({ name, params })), [ |
| 1201 | { name: "TestTool", params: { input: 42 } }, |
| 1202 | { name: "Write", params: { path: "a" } } |
| 1203 | ]) |
| 1204 | })) |
| 1205 | |
| 1206 | it.effect("replays reasoning alongside assistant text and tool calls", () => |
| 1207 | Effect.gen(function*() { |
| 1208 | let capturedRequest: HttpClientRequest.HttpClientRequest | undefined |
| 1209 | |
| 1210 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 1211 | Layer.provide(Layer.succeed( |
| 1212 | HttpClient.HttpClient, |
| 1213 | makeHttpClient((request) => { |
| 1214 | capturedRequest = request |
| 1215 | return Effect.succeed(jsonResponse(request, makeChatCompletion())) |
| 1216 | }) |
| 1217 | )) |
| 1218 | ) |
| 1219 | |
| 1220 | const prompt = Prompt.make([ |
| 1221 | { role: "user", content: "What is in a.txt?" }, |
| 1222 | { |
| 1223 | role: "assistant", |
| 1224 | content: [ |
| 1225 | { type: "reasoning", text: "The user wants a.txt; I should read it." }, |
| 1226 | { type: "text", text: "Let me read it." }, |
| 1227 | { type: "tool-call", id: "call_1", name: "TestTool", params: { input: "a.txt" } } |
| 1228 | ] |
| 1229 | }, |
| 1230 | { |
| 1231 | role: "tool", |
| 1232 | content: [{ type: "tool-result", id: "call_1", name: "TestTool", isFailure: false, result: "hello" }] |
| 1233 | } |
| 1234 | ]) |
| 1235 | |
| 1236 | yield* LanguageModel.generateText({ prompt, toolkit: TestToolkit, disableToolCallResolution: true }).pipe( |
| 1237 | Effect.provide(OpenAiLanguageModel.model("deepseek-reasoner")), |
| 1238 | Effect.provide(layer) |
| 1239 | ) |
| 1240 | |
| 1241 | assert.isDefined(capturedRequest) |
| 1242 | if (capturedRequest === undefined) { |
| 1243 | return |
| 1244 | } |
| 1245 | |
| 1246 | const requestBody = yield* getRequestBody(capturedRequest) |
| 1247 | assert.deepStrictEqual(requestBody.messages, [ |
| 1248 | { role: "user", content: "What is in a.txt?" }, |
| 1249 | { |
| 1250 | role: "assistant", |
| 1251 | content: "Let me read it.", |
| 1252 | reasoning_content: "The user wants a.txt; I should read it.", |
| 1253 | tool_calls: [{ |
| 1254 | id: "call_1", |
| 1255 | type: "function", |
| 1256 | function: { name: "TestTool", arguments: JSON.stringify({ input: "a.txt" }) } |
| 1257 | }] |
| 1258 | }, |
| 1259 | { role: "tool", tool_call_id: "call_1", content: "hello" } |
| 1260 | ]) |
| 1261 | })) |
| 1262 | |
| 1263 | it.effect("replays decoded Groq reasoning under its original field alongside text and tool calls", () => |
| 1264 | Effect.gen(function*() { |
| 1265 | const requests: Array<HttpClientRequest.HttpClientRequest> = [] |
| 1266 | |
| 1267 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 1268 | Layer.provide(Layer.succeed( |
| 1269 | HttpClient.HttpClient, |
| 1270 | makeHttpClient((request) => { |
| 1271 | requests.push(request) |
| 1272 | return Effect.succeed(jsonResponse( |
| 1273 | request, |
| 1274 | requests.length > 1 ? makeChatCompletion() : makeChatCompletion({ |
| 1275 | choices: [{ |
| 1276 | index: 0, |
| 1277 | finish_reason: "tool_calls", |
| 1278 | message: { |
| 1279 | role: "assistant", |
| 1280 | reasoning: "Need the file", |
| 1281 | content: "Let me read it.", |
| 1282 | tool_calls: [{ |
| 1283 | id: "call_1", |
| 1284 | type: "function", |
| 1285 | function: { name: "TestTool", arguments: JSON.stringify({ input: "a.txt" }) } |
| 1286 | }] |
| 1287 | } |
| 1288 | }] |
| 1289 | }) |
| 1290 | )) |
| 1291 | }) |
| 1292 | )) |
| 1293 | ) |
| 1294 | |
| 1295 | const generate = (prompt: Prompt.Prompt) => |
| 1296 | LanguageModel.generateText({ prompt, toolkit: TestToolkit, disableToolCallResolution: true }).pipe( |
| 1297 | Effect.provide(OpenAiLanguageModel.model("openai/gpt-oss-120b")), |
| 1298 | Effect.provide(layer) |
| 1299 | ) |
| 1300 | |
| 1301 | const prompt = Prompt.make("What is in a.txt?") |
| 1302 | const first = yield* generate(prompt) |
| 1303 | yield* generate(Prompt.concat( |
| 1304 | Prompt.concat(prompt, Prompt.fromResponseParts(first.content)), |
| 1305 | Prompt.make([{ |
| 1306 | role: "tool", |
| 1307 | content: [{ type: "tool-result", id: "call_1", name: "TestTool", isFailure: false, result: "hello" }] |
| 1308 | }]) |
| 1309 | )) |
| 1310 | |
| 1311 | const followUpBody = yield* getRequestBody(requests[1]) |
| 1312 | assert.deepStrictEqual(followUpBody.messages, [ |
| 1313 | { role: "user", content: "What is in a.txt?" }, |
| 1314 | { |
| 1315 | role: "assistant", |
| 1316 | content: "Let me read it.", |
| 1317 | reasoning: "Need the file", |
| 1318 | tool_calls: [{ |
| 1319 | id: "call_1", |
| 1320 | type: "function", |
| 1321 | function: { name: "TestTool", arguments: JSON.stringify({ input: "a.txt" }) } |
| 1322 | }] |
| 1323 | }, |
| 1324 | { role: "tool", tool_call_id: "call_1", content: "hello" } |
| 1325 | ]) |
| 1326 | })) |
| 1327 | }) |
| 1328 | |
| 1329 | describe("generateObject", () => { |
| 1330 | it.effect("uses json_schema format for structured output", () => |
| 1331 | Effect.gen(function*() { |
| 1332 | let capturedRequest: HttpClientRequest.HttpClientRequest | undefined |
| 1333 | |
| 1334 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 1335 | Layer.provide(Layer.succeed( |
| 1336 | HttpClient.HttpClient, |
| 1337 | makeHttpClient((request) => { |
| 1338 | capturedRequest = request |
| 1339 | return Effect.succeed(jsonResponse( |
| 1340 | request, |
| 1341 | makeChatCompletion({ |
| 1342 | choices: [{ |
| 1343 | index: 0, |
| 1344 | finish_reason: "stop", |
| 1345 | message: { |
| 1346 | role: "assistant", |
| 1347 | content: JSON.stringify({ name: "Ada", age: 37 }) |
| 1348 | } |
| 1349 | }] |
| 1350 | }) |
| 1351 | )) |
| 1352 | }) |
| 1353 | )) |
| 1354 | ) |
| 1355 | |
| 1356 | const person = yield* LanguageModel.generateObject({ |
| 1357 | prompt: "Return a person", |
| 1358 | schema: Schema.Struct({ |
| 1359 | name: Schema.String, |
| 1360 | age: Schema.Number |
| 1361 | }) |
| 1362 | }).pipe( |
| 1363 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1364 | Effect.provide(layer) |
| 1365 | ) |
| 1366 | |
| 1367 | assert.strictEqual(person.value.name, "Ada") |
| 1368 | assert.strictEqual(person.value.age, 37) |
| 1369 | |
| 1370 | assert.isDefined(capturedRequest) |
| 1371 | if (capturedRequest === undefined) { |
| 1372 | return |
| 1373 | } |
| 1374 | |
| 1375 | const requestBody = yield* getRequestBody(capturedRequest) |
| 1376 | assert.strictEqual(requestBody.response_format.type, "json_schema") |
| 1377 | assert.strictEqual(requestBody.response_format.json_schema.strict, true) |
| 1378 | })) |
| 1379 | |
| 1380 | it.effect("uses OpenAI codec transformer for optional structured fields", () => |
| 1381 | Effect.gen(function*() { |
| 1382 | let capturedRequest: HttpClientRequest.HttpClientRequest | undefined |
| 1383 | |
| 1384 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 1385 | Layer.provide(Layer.succeed( |
| 1386 | HttpClient.HttpClient, |
| 1387 | makeHttpClient((request) => { |
| 1388 | capturedRequest = request |
| 1389 | return Effect.succeed(jsonResponse( |
| 1390 | request, |
| 1391 | makeChatCompletion({ |
| 1392 | choices: [{ |
| 1393 | index: 0, |
| 1394 | finish_reason: "stop", |
| 1395 | message: { |
| 1396 | role: "assistant", |
| 1397 | content: JSON.stringify({ name: "Ada", nickname: null }) |
| 1398 | } |
| 1399 | }] |
| 1400 | }) |
| 1401 | )) |
| 1402 | }) |
| 1403 | )) |
| 1404 | ) |
| 1405 | |
| 1406 | const person = yield* LanguageModel.generateObject({ |
| 1407 | prompt: "Return a person", |
| 1408 | schema: Schema.Struct({ |
| 1409 | name: Schema.String, |
| 1410 | nickname: Schema.optionalKey(Schema.String) |
| 1411 | }) |
| 1412 | }).pipe( |
| 1413 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1414 | Effect.provide(layer) |
| 1415 | ) |
| 1416 | |
| 1417 | assert.strictEqual(person.value.name, "Ada") |
| 1418 | assert.isUndefined(person.value.nickname) |
| 1419 | |
| 1420 | assert.isDefined(capturedRequest) |
| 1421 | if (capturedRequest === undefined) { |
| 1422 | return |
| 1423 | } |
| 1424 | |
| 1425 | const requestBody = yield* getRequestBody(capturedRequest) |
| 1426 | assert.deepStrictEqual(requestBody.response_format.json_schema.schema.required, ["name", "nickname"]) |
| 1427 | assert.deepStrictEqual(requestBody.response_format.json_schema.schema.properties.nickname, { |
| 1428 | anyOf: [{ type: "string" }, { type: "null" }] |
| 1429 | }) |
| 1430 | })) |
| 1431 | }) |
| 1432 | |
| 1433 | describe("streamText", () => { |
| 1434 | it.effect("replays streamed assistant text and tool calls with store enabled", () => |
| 1435 | Effect.gen(function*() { |
| 1436 | const requests: Array<HttpClientRequest.HttpClientRequest> = [] |
| 1437 | |
| 1438 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 1439 | Layer.provide(Layer.succeed( |
| 1440 | HttpClient.HttpClient, |
| 1441 | makeHttpClient((request) => { |
| 1442 | requests.push(request) |
| 1443 | return Effect.succeed( |
| 1444 | requests.length > 1 |
| 1445 | ? jsonResponse(request, makeChatCompletion()) |
| 1446 | : sseResponse(request, [ |
| 1447 | { |
| 1448 | id: "chatcmpl_store_1", |
| 1449 | object: "chat.completion.chunk", |
| 1450 | model: "gpt-4o-mini", |
| 1451 | created: 1, |
| 1452 | choices: [{ |
| 1453 | index: 0, |
| 1454 | delta: { |
| 1455 | content: "Let me read it.", |
| 1456 | tool_calls: [{ |
| 1457 | index: 0, |
| 1458 | id: "call_1", |
| 1459 | type: "function", |
| 1460 | function: { name: "TestTool", arguments: JSON.stringify({ input: "a.txt" }) } |
| 1461 | }] |
| 1462 | }, |
| 1463 | finish_reason: "tool_calls" |
| 1464 | }] |
| 1465 | }, |
| 1466 | "[DONE]" |
| 1467 | ]) |
| 1468 | ) |
| 1469 | }) |
| 1470 | )) |
| 1471 | ) |
| 1472 | |
| 1473 | const model = OpenAiLanguageModel.model("gpt-4o-mini", { store: true }) |
| 1474 | const prompt = Prompt.make("What is in a.txt?") |
| 1475 | const parts = yield* LanguageModel.streamText({ |
| 1476 | prompt, |
| 1477 | toolkit: TestToolkit, |
| 1478 | disableToolCallResolution: true |
| 1479 | }).pipe( |
| 1480 | Stream.runCollect, |
| 1481 | Effect.provide(model), |
| 1482 | Effect.provide(layer) |
| 1483 | ) |
| 1484 | |
| 1485 | yield* LanguageModel.generateText({ |
| 1486 | prompt: Prompt.concat( |
| 1487 | Prompt.concat(prompt, Prompt.fromResponseParts(parts)), |
| 1488 | Prompt.make([{ |
| 1489 | role: "tool", |
| 1490 | content: [{ type: "tool-result", id: "call_1", name: "TestTool", isFailure: false, result: "hello" }] |
| 1491 | }]) |
| 1492 | ), |
| 1493 | toolkit: TestToolkit, |
| 1494 | disableToolCallResolution: true |
| 1495 | }).pipe( |
| 1496 | Effect.provide(model), |
| 1497 | Effect.provide(layer) |
| 1498 | ) |
| 1499 | |
| 1500 | const followUpBody = yield* getRequestBody(requests[1]) |
| 1501 | assert.deepStrictEqual(followUpBody.messages, [ |
| 1502 | { role: "user", content: "What is in a.txt?" }, |
| 1503 | { |
| 1504 | role: "assistant", |
| 1505 | content: "Let me read it.", |
| 1506 | tool_calls: [{ |
| 1507 | id: "call_1", |
| 1508 | type: "function", |
| 1509 | function: { name: "TestTool", arguments: JSON.stringify({ input: "a.txt" }) } |
| 1510 | }] |
| 1511 | }, |
| 1512 | { role: "tool", tool_call_id: "call_1", content: "hello" } |
| 1513 | ]) |
| 1514 | })) |
| 1515 | |
| 1516 | it.effect("handles chat completion stream chunks", () => |
| 1517 | Effect.gen(function*() { |
| 1518 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 1519 | Layer.provide(Layer.succeed( |
| 1520 | HttpClient.HttpClient, |
| 1521 | makeHttpClient((request) => |
| 1522 | Effect.succeed(sseResponse(request, [ |
| 1523 | { |
| 1524 | id: "chatcmpl_test123", |
| 1525 | object: "chat.completion.chunk", |
| 1526 | model: "gpt-4o-mini", |
| 1527 | created: 1, |
| 1528 | choices: [{ |
| 1529 | index: 0, |
| 1530 | delta: {}, |
| 1531 | finish_reason: "stop" |
| 1532 | }] |
| 1533 | }, |
| 1534 | "[DONE]" |
| 1535 | ])) |
| 1536 | ) |
| 1537 | )) |
| 1538 | ) |
| 1539 | |
| 1540 | const partsChunk = yield* LanguageModel.streamText({ prompt: "test" }).pipe( |
| 1541 | Stream.runCollect, |
| 1542 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1543 | Effect.provide(layer) |
| 1544 | ) |
| 1545 | |
| 1546 | const parts = Array.from(partsChunk) |
| 1547 | |
| 1548 | assert.isTrue(parts.some((part) => part.type === "response-metadata")) |
| 1549 | |
| 1550 | const finish = parts.find((part) => part.type === "finish") |
| 1551 | assert.isDefined(finish) |
| 1552 | if (finish?.type === "finish") { |
| 1553 | assert.strictEqual(finish.reason, "stop") |
| 1554 | } |
| 1555 | })) |
| 1556 | |
| 1557 | it.effect("streams undeclared tool calls when tool call validation is deferred", () => |
| 1558 | Effect.gen(function*() { |
| 1559 | const client = makeHttpClient((request) => |
| 1560 | Effect.succeed(sseResponse(request, [ |
| 1561 | { |
| 1562 | id: "chatcmpl_undeclared", |
| 1563 | object: "chat.completion.chunk", |
| 1564 | model: "gpt-4o-mini", |
| 1565 | created: 1, |
| 1566 | choices: [{ |
| 1567 | index: 0, |
| 1568 | delta: { |
| 1569 | tool_calls: [{ |
| 1570 | index: 0, |
| 1571 | id: "call_1", |
| 1572 | type: "function", |
| 1573 | function: { name: "Write", arguments: JSON.stringify({ path: "a" }) } |
| 1574 | }] |
| 1575 | }, |
| 1576 | finish_reason: "tool_calls" |
| 1577 | }] |
| 1578 | }, |
| 1579 | "[DONE]" |
| 1580 | ])) |
| 1581 | ) |
| 1582 | |
| 1583 | const parts = yield* LanguageModel.streamText({ |
| 1584 | prompt: "use the tools", |
| 1585 | toolkit: TestToolkit, |
| 1586 | disableToolCallResolution: true, |
| 1587 | toolCallValidation: "deferred" |
| 1588 | }).pipe( |
| 1589 | Stream.runCollect, |
| 1590 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1591 | Effect.provide(TestToolkitLayer), |
| 1592 | Effect.provide(OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") })), |
| 1593 | Effect.provideService(HttpClient.HttpClient, client) |
| 1594 | ) |
| 1595 | |
| 1596 | assert.deepStrictEqual( |
| 1597 | parts.filter((part) => part.type === "tool-call").map(({ name, params }) => ({ name, params })), |
| 1598 | [{ name: "Write", params: { path: "a" } }] |
| 1599 | ) |
| 1600 | })) |
| 1601 | |
| 1602 | it.effect("emits text when streamed tool_calls is null", () => |
| 1603 | Effect.gen(function*() { |
| 1604 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 1605 | Layer.provide(Layer.succeed( |
| 1606 | HttpClient.HttpClient, |
| 1607 | makeHttpClient((request) => |
| 1608 | Effect.succeed(sseResponse(request, [ |
| 1609 | { |
| 1610 | id: "chatcmpl_nullable_tool_calls", |
| 1611 | object: "chat.completion.chunk", |
| 1612 | model: "inception/mercury-2", |
| 1613 | created: 1, |
| 1614 | choices: [{ |
| 1615 | index: 0, |
| 1616 | delta: { |
| 1617 | content: "Hello", |
| 1618 | role: "assistant", |
| 1619 | tool_calls: null |
| 1620 | }, |
| 1621 | finish_reason: null |
| 1622 | }] |
| 1623 | }, |
| 1624 | "[DONE]" |
| 1625 | ])) |
| 1626 | ) |
| 1627 | )) |
| 1628 | ) |
| 1629 | |
| 1630 | const partsChunk = yield* LanguageModel.streamText({ prompt: "test" }).pipe( |
| 1631 | Stream.runCollect, |
| 1632 | Effect.provide(OpenAiLanguageModel.model("inception/mercury-2")), |
| 1633 | Effect.provide(layer) |
| 1634 | ) |
| 1635 | |
| 1636 | const text = Array.from(partsChunk) |
| 1637 | .filter((part) => part.type === "text-delta") |
| 1638 | .map((part) => part.delta) |
| 1639 | .join("") |
| 1640 | |
| 1641 | assert.strictEqual(text, "Hello") |
| 1642 | })) |
| 1643 | |
| 1644 | it.effect("preserves streamed text and tool args with nullable delta fields", () => |
| 1645 | Effect.gen(function*() { |
| 1646 | const extraContent = { google: { thought_signature: "signature-1" } } |
| 1647 | const chunk = (delta: Record<string, unknown>) => ({ |
| 1648 | id: "chatcmpl_nullable_delta_fields", |
| 1649 | object: "chat.completion.chunk", |
| 1650 | model: "gpt-4o-mini", |
| 1651 | created: 1, |
| 1652 | choices: [{ index: 0, delta, finish_reason: null }] |
| 1653 | }) |
| 1654 | |
| 1655 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 1656 | Layer.provide(Layer.succeed( |
| 1657 | HttpClient.HttpClient, |
| 1658 | makeHttpClient((request) => |
| 1659 | Effect.succeed(sseResponse(request, [ |
| 1660 | chunk({ content: "Hello", role: null }), |
| 1661 | chunk({ |
| 1662 | tool_calls: [{ |
| 1663 | index: 0, |
| 1664 | id: "call_1", |
| 1665 | type: "function", |
| 1666 | extra_content: extraContent, |
| 1667 | function: { name: "TestTool", arguments: "" } |
| 1668 | }] |
| 1669 | }), |
| 1670 | chunk({ |
| 1671 | tool_calls: [{ |
| 1672 | index: 0, |
| 1673 | id: null, |
| 1674 | extra_content: null, |
| 1675 | function: { name: null, arguments: "{\"in" } |
| 1676 | }] |
| 1677 | }), |
| 1678 | chunk({ |
| 1679 | tool_calls: [{ index: 0, id: null, function: { name: null, arguments: "put\":\"hel" } }] |
| 1680 | }), |
| 1681 | chunk({ |
| 1682 | tool_calls: [{ index: 0, id: null, function: { name: null, arguments: "lo\"}" } }] |
| 1683 | }), |
| 1684 | { |
| 1685 | id: "chatcmpl_nullable_delta_fields", |
| 1686 | object: "chat.completion.chunk", |
| 1687 | model: "gpt-4o-mini", |
| 1688 | created: 1, |
| 1689 | choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] |
| 1690 | }, |
| 1691 | "[DONE]" |
| 1692 | ])) |
| 1693 | ) |
| 1694 | )) |
| 1695 | ) |
| 1696 | |
| 1697 | const partsChunk = yield* LanguageModel.streamText({ |
| 1698 | prompt: "use the tool", |
| 1699 | toolkit: TestToolkit, |
| 1700 | disableToolCallResolution: true |
| 1701 | }).pipe( |
| 1702 | Stream.runCollect, |
| 1703 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1704 | Effect.provide(TestToolkitLayer), |
| 1705 | Effect.provide(layer) |
| 1706 | ) |
| 1707 | |
| 1708 | const parts = globalThis.Array.from(partsChunk) |
| 1709 | const text = parts |
| 1710 | .filter((part) => part.type === "text-delta") |
| 1711 | .map((part) => part.delta) |
| 1712 | .join("") |
| 1713 | const toolCall = parts.find((part) => part.type === "tool-call") |
| 1714 | |
| 1715 | assert.strictEqual(text, "Hello") |
| 1716 | assert.isDefined(toolCall) |
| 1717 | if (toolCall?.type !== "tool-call") { |
| 1718 | return |
| 1719 | } |
| 1720 | assert.strictEqual(toolCall.id, "call_1") |
| 1721 | assert.deepStrictEqual(toolCall.params, { input: "hello" }) |
| 1722 | assert.deepStrictEqual(toolCall.metadata["openai-compat"] as unknown, { itemId: "call_1", extraContent }) |
| 1723 | })) |
| 1724 | |
| 1725 | it.effect("decodes streamed tool call params with the OpenAI codec", () => |
| 1726 | Effect.gen(function*() { |
| 1727 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 1728 | Layer.provide(Layer.succeed( |
| 1729 | HttpClient.HttpClient, |
| 1730 | makeHttpClient((request) => |
| 1731 | Effect.succeed(sseResponse(request, [ |
| 1732 | { |
| 1733 | id: "chatcmpl_record_1", |
| 1734 | object: "chat.completion.chunk", |
| 1735 | model: "gpt-4o-mini", |
| 1736 | created: 1, |
| 1737 | choices: [{ |
| 1738 | index: 0, |
| 1739 | delta: { |
| 1740 | tool_calls: [{ |
| 1741 | index: 0, |
| 1742 | id: "call_record_1", |
| 1743 | type: "function", |
| 1744 | function: { |
| 1745 | name: "RecordTool", |
| 1746 | arguments: JSON.stringify({ env: [{ 0: "PATH", 1: "/usr/bin" }] }) |
| 1747 | } |
| 1748 | }] |
| 1749 | }, |
| 1750 | finish_reason: "tool_calls" |
| 1751 | }] |
| 1752 | }, |
| 1753 | "[DONE]" |
| 1754 | ])) |
| 1755 | ) |
| 1756 | )) |
| 1757 | ) |
| 1758 | |
| 1759 | const partsChunk = yield* LanguageModel.streamText({ |
| 1760 | prompt: "read the environment", |
| 1761 | toolkit: RecordToolkit, |
| 1762 | disableToolCallResolution: true |
| 1763 | }).pipe( |
| 1764 | Stream.runCollect, |
| 1765 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1766 | Effect.provide(RecordToolkitLayer), |
| 1767 | Effect.provide(layer) |
| 1768 | ) |
| 1769 | |
| 1770 | const toolCall = globalThis.Array.from(partsChunk).find((part) => part.type === "tool-call") |
| 1771 | assert.isDefined(toolCall) |
| 1772 | if (toolCall?.type !== "tool-call") { |
| 1773 | return |
| 1774 | } |
| 1775 | assert.deepStrictEqual(toolCall.params, { env: { PATH: "/usr/bin" } }) |
| 1776 | })) |
| 1777 | |
| 1778 | it.effect("maps local shell stream tool calls to local_shell call outputs", () => |
| 1779 | Effect.gen(function*() { |
| 1780 | const capturedRequests = yield* Ref.make<ReadonlyArray<HttpClientRequest.HttpClientRequest>>([]) |
| 1781 | const requestCount = yield* Ref.make(0) |
| 1782 | |
| 1783 | const httpClient = HttpClient.makeWith( |
| 1784 | Effect.fnUntraced(function*(requestEffect) { |
| 1785 | const request = yield* requestEffect |
| 1786 | yield* Ref.update(capturedRequests, (requests) => [...requests, request]) |
| 1787 | const index = yield* Ref.getAndUpdate(requestCount, (value) => value + 1) |
| 1788 | |
| 1789 | if (index === 0) { |
| 1790 | return sseResponse(request, [makeLocalShellChunk(), "[DONE]"]) |
| 1791 | } |
| 1792 | |
| 1793 | return jsonResponse(request, makeChatCompletion()) |
| 1794 | }), |
| 1795 | Effect.succeed as HttpClient.HttpClient.Preprocess<HttpClientError.HttpClientError, never> |
| 1796 | ) |
| 1797 | |
| 1798 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 1799 | Layer.provide(Layer.succeed(HttpClient.HttpClient, httpClient)) |
| 1800 | ) |
| 1801 | |
| 1802 | const toolkit = Toolkit.make(CompatLocalShellTool({})) |
| 1803 | const toolkitLayer = toolkit.toLayer({ |
| 1804 | CompatLocalShell: () => Effect.succeed("done") |
| 1805 | }) |
| 1806 | |
| 1807 | const partsChunk = yield* LanguageModel.streamText({ |
| 1808 | prompt: "Run pwd", |
| 1809 | toolkit, |
| 1810 | disableToolCallResolution: true |
| 1811 | }).pipe( |
| 1812 | Stream.runCollect, |
| 1813 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1814 | Effect.provide(toolkitLayer), |
| 1815 | Effect.provide(layer) |
| 1816 | ) |
| 1817 | |
| 1818 | const toolCall = globalThis.Array.from(partsChunk).find((part) => part.type === "tool-call") |
| 1819 | assert.isDefined(toolCall) |
| 1820 | if (toolCall?.type !== "tool-call") { |
| 1821 | return |
| 1822 | } |
| 1823 | |
| 1824 | assert.strictEqual(toolCall.name, "CompatLocalShell") |
| 1825 | assert.deepStrictEqual(toolCall.params, { action: localShellAction }) |
| 1826 | |
| 1827 | yield* LanguageModel.generateText({ |
| 1828 | prompt: Prompt.make([ |
| 1829 | { role: "user", content: "Run pwd" }, |
| 1830 | { |
| 1831 | role: "assistant", |
| 1832 | content: [Prompt.toolCallPart({ |
| 1833 | id: toolCall.id, |
| 1834 | name: toolCall.name, |
| 1835 | params: { action: localShellAction }, |
| 1836 | providerExecuted: false, |
| 1837 | options: { |
| 1838 | "openai-compat": { |
| 1839 | itemId: "ls_call_1" |
| 1840 | } |
| 1841 | } |
| 1842 | })] |
| 1843 | }, |
| 1844 | { |
| 1845 | role: "tool", |
| 1846 | content: [Prompt.toolResultPart({ |
| 1847 | id: toolCall.id, |
| 1848 | name: toolCall.name, |
| 1849 | isFailure: false, |
| 1850 | result: "done", |
| 1851 | providerExecuted: false |
| 1852 | })] |
| 1853 | } |
| 1854 | ]), |
| 1855 | toolkit |
| 1856 | }).pipe( |
| 1857 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1858 | Effect.provide(toolkitLayer), |
| 1859 | Effect.provide(layer) |
| 1860 | ) |
| 1861 | |
| 1862 | const requests = yield* Ref.get(capturedRequests) |
| 1863 | const followUpRequest = requests[1] |
| 1864 | assert.isDefined(followUpRequest) |
| 1865 | if (followUpRequest === undefined) { |
| 1866 | return |
| 1867 | } |
| 1868 | |
| 1869 | const followUpBody = yield* getRequestBody(followUpRequest) |
| 1870 | |
| 1871 | const localShellCall = followUpBody.messages.find((item: any) => |
| 1872 | item.role === "assistant" && item.tool_calls?.[0]?.function?.name === "local_shell" |
| 1873 | ) |
| 1874 | assert.isDefined(localShellCall) |
| 1875 | assert.strictEqual(localShellCall.content, null) |
| 1876 | assert.strictEqual(localShellCall.tool_calls[0].id, toolCall.id) |
| 1877 | |
| 1878 | const localShellOutput = followUpBody.messages.find((item: any) => item.role === "tool") |
| 1879 | assert.isDefined(localShellOutput) |
| 1880 | assert.strictEqual(localShellOutput.tool_call_id, toolCall.id) |
| 1881 | assert.strictEqual(localShellOutput.content, "done") |
| 1882 | })) |
| 1883 | |
| 1884 | it.effect("maps apply_patch stream tool calls to custom provider-defined tool", () => |
| 1885 | Effect.gen(function*() { |
| 1886 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 1887 | Layer.provide(Layer.succeed( |
| 1888 | HttpClient.HttpClient, |
| 1889 | makeHttpClient((request) => |
| 1890 | Effect.succeed(sseResponse(request, [ |
| 1891 | { |
| 1892 | id: "chatcmpl_apply_patch_1", |
| 1893 | object: "chat.completion.chunk", |
| 1894 | model: "gpt-4o-mini", |
| 1895 | created: 1, |
| 1896 | choices: [{ |
| 1897 | index: 0, |
| 1898 | delta: { |
| 1899 | tool_calls: [{ |
| 1900 | index: 0, |
| 1901 | id: "patch_call_1", |
| 1902 | type: "function", |
| 1903 | function: { |
| 1904 | name: "apply_patch", |
| 1905 | arguments: JSON.stringify({ |
| 1906 | call_id: "patch_call_1", |
| 1907 | operation: { |
| 1908 | type: "delete_file", |
| 1909 | path: "src/legacy.ts" |
| 1910 | } |
| 1911 | }) |
| 1912 | } |
| 1913 | }] |
| 1914 | }, |
| 1915 | finish_reason: "tool_calls" |
| 1916 | }] |
| 1917 | }, |
| 1918 | "[DONE]" |
| 1919 | ])) |
| 1920 | ) |
| 1921 | )) |
| 1922 | ) |
| 1923 | |
| 1924 | const toolkit = Toolkit.make(CompatApplyPatchTool({})) |
| 1925 | const toolkitLayer = toolkit.toLayer({ |
| 1926 | CompatApplyPatch: () => |
| 1927 | Effect.succeed({ |
| 1928 | status: "completed", |
| 1929 | output: "deleted" |
| 1930 | }) |
| 1931 | }) |
| 1932 | |
| 1933 | const partsChunk = yield* LanguageModel.streamText({ |
| 1934 | prompt: "Delete src/legacy.ts", |
| 1935 | toolkit, |
| 1936 | disableToolCallResolution: true |
| 1937 | }).pipe( |
| 1938 | Stream.runCollect, |
| 1939 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 1940 | Effect.provide(toolkitLayer), |
| 1941 | Effect.provide(layer) |
| 1942 | ) |
| 1943 | |
| 1944 | const toolCall = globalThis.Array.from(partsChunk).find((part) => part.type === "tool-call") |
| 1945 | assert.isDefined(toolCall) |
| 1946 | if (toolCall?.type !== "tool-call") { |
| 1947 | return |
| 1948 | } |
| 1949 | |
| 1950 | assert.strictEqual(toolCall.name, "CompatApplyPatch") |
| 1951 | assert.deepStrictEqual(toolCall.params, { |
| 1952 | call_id: "patch_call_1", |
| 1953 | operation: { |
| 1954 | type: "delete_file", |
| 1955 | path: "src/legacy.ts" |
| 1956 | } |
| 1957 | }) |
| 1958 | })) |
| 1959 | |
| 1960 | it.effect("preserves fragmented stream tool call ids and names", () => |
| 1961 | Effect.gen(function*() { |
| 1962 | const expectedParams = { |
| 1963 | call_id: "patch_call_2", |
| 1964 | operation: { |
| 1965 | type: "delete_file", |
| 1966 | path: "src/fragmented.ts" |
| 1967 | } |
| 1968 | } |
| 1969 | const toolArguments = JSON.stringify(expectedParams) |
| 1970 | |
| 1971 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 1972 | Layer.provide(Layer.succeed( |
| 1973 | HttpClient.HttpClient, |
| 1974 | makeHttpClient((request) => |
| 1975 | Effect.succeed(sseResponse(request, [ |
| 1976 | { |
| 1977 | id: "chatcmpl_apply_patch_fragmented_1", |
| 1978 | object: "chat.completion.chunk", |
| 1979 | model: "gpt-4o-mini", |
| 1980 | created: 1, |
| 1981 | choices: [{ |
| 1982 | index: 0, |
| 1983 | delta: { |
| 1984 | tool_calls: [{ |
| 1985 | index: 0, |
| 1986 | id: "patch_call_2", |
| 1987 | type: "function", |
| 1988 | function: { |
| 1989 | name: "apply_patch", |
| 1990 | arguments: toolArguments.slice(0, 24) |
| 1991 | } |
| 1992 | }] |
| 1993 | }, |
| 1994 | finish_reason: null |
| 1995 | }] |
| 1996 | }, |
| 1997 | { |
| 1998 | id: "chatcmpl_apply_patch_fragmented_1", |
| 1999 | object: "chat.completion.chunk", |
| 2000 | model: "gpt-4o-mini", |
| 2001 | created: 1, |
| 2002 | choices: [{ |
| 2003 | index: 0, |
| 2004 | delta: { |
| 2005 | tool_calls: [{ |
| 2006 | index: 0, |
| 2007 | function: { |
| 2008 | arguments: toolArguments.slice(24, 48) |
| 2009 | } |
| 2010 | }] |
| 2011 | }, |
| 2012 | finish_reason: null |
| 2013 | }] |
| 2014 | }, |
| 2015 | { |
| 2016 | id: "chatcmpl_apply_patch_fragmented_1", |
| 2017 | object: "chat.completion.chunk", |
| 2018 | model: "gpt-4o-mini", |
| 2019 | created: 1, |
| 2020 | choices: [{ |
| 2021 | index: 0, |
| 2022 | delta: { |
| 2023 | tool_calls: [{ |
| 2024 | index: 0, |
| 2025 | function: { |
| 2026 | arguments: toolArguments.slice(48) |
| 2027 | } |
| 2028 | }] |
| 2029 | }, |
| 2030 | finish_reason: "tool_calls" |
| 2031 | }] |
| 2032 | }, |
| 2033 | "[DONE]" |
| 2034 | ])) |
| 2035 | ) |
| 2036 | )) |
| 2037 | ) |
| 2038 | |
| 2039 | const toolkit = Toolkit.make(CompatApplyPatchTool({})) |
| 2040 | const toolkitLayer = toolkit.toLayer({ |
| 2041 | CompatApplyPatch: () => |
| 2042 | Effect.succeed({ |
| 2043 | status: "completed", |
| 2044 | output: "deleted" |
| 2045 | }) |
| 2046 | }) |
| 2047 | |
| 2048 | const partsChunk = yield* LanguageModel.streamText({ |
| 2049 | prompt: "Delete src/fragmented.ts", |
| 2050 | toolkit, |
| 2051 | disableToolCallResolution: true |
| 2052 | }).pipe( |
| 2053 | Stream.runCollect, |
| 2054 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 2055 | Effect.provide(toolkitLayer), |
| 2056 | Effect.provide(layer) |
| 2057 | ) |
| 2058 | |
| 2059 | const parts = globalThis.Array.from(partsChunk) |
| 2060 | |
| 2061 | const start = parts.find((part) => part.type === "tool-params-start" && part.id === "patch_call_2") |
| 2062 | assert.isDefined(start) |
| 2063 | if (start?.type !== "tool-params-start") { |
| 2064 | return |
| 2065 | } |
| 2066 | assert.strictEqual(start.name, "CompatApplyPatch") |
| 2067 | |
| 2068 | assert.deepStrictEqual(decodeToolParamsFromStream(parts, "patch_call_2"), expectedParams) |
| 2069 | |
| 2070 | const toolCall = parts.find((part) => part.type === "tool-call") |
| 2071 | assert.isDefined(toolCall) |
| 2072 | if (toolCall?.type !== "tool-call") { |
| 2073 | return |
| 2074 | } |
| 2075 | |
| 2076 | assert.strictEqual(toolCall.id, "patch_call_2") |
| 2077 | assert.strictEqual(toolCall.name, "CompatApplyPatch") |
| 2078 | assert.deepStrictEqual(toolCall.params, expectedParams) |
| 2079 | })) |
| 2080 | |
| 2081 | it.effect("continues after invalid JSON and schema-mismatched events", () => |
| 2082 | Effect.gen(function*() { |
| 2083 | let capturedRequest: HttpClientRequest.HttpClientRequest | undefined |
| 2084 | |
| 2085 | const events = [ |
| 2086 | "{invalid-json", |
| 2087 | { |
| 2088 | id: "chatcmpl_stream_1", |
| 2089 | object: "chat.completion.chunk", |
| 2090 | model: "gpt-4o-mini", |
| 2091 | created: 1, |
| 2092 | choices: [{ |
| 2093 | index: 0, |
| 2094 | delta: { content: "Hello" }, |
| 2095 | finish_reason: null |
| 2096 | }] |
| 2097 | }, |
| 2098 | { |
| 2099 | type: "provider.chat.completion.delta", |
| 2100 | provider_payload: { content: "provider-specific" } |
| 2101 | }, |
| 2102 | { |
| 2103 | id: "chatcmpl_stream_1", |
| 2104 | object: "chat.completion.chunk", |
| 2105 | model: "gpt-4o-mini", |
| 2106 | created: 1, |
| 2107 | choices: [{ |
| 2108 | index: 0, |
| 2109 | delta: {}, |
| 2110 | finish_reason: "stop" |
| 2111 | }], |
| 2112 | usage: { |
| 2113 | prompt_tokens: 10, |
| 2114 | completion_tokens: 7, |
| 2115 | total_tokens: 17, |
| 2116 | prompt_tokens_details: { |
| 2117 | cached_tokens: 3 |
| 2118 | }, |
| 2119 | completion_tokens_details: { |
| 2120 | reasoning_tokens: 2 |
| 2121 | } |
| 2122 | }, |
| 2123 | provider_future_field: { accepted: true } |
| 2124 | }, |
| 2125 | "[DONE]" |
| 2126 | ] |
| 2127 | |
| 2128 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 2129 | Layer.provide(Layer.succeed( |
| 2130 | HttpClient.HttpClient, |
| 2131 | makeHttpClient((request) => { |
| 2132 | capturedRequest = request |
| 2133 | return Effect.succeed(sseResponse(request, events)) |
| 2134 | }) |
| 2135 | )) |
| 2136 | ) |
| 2137 | |
| 2138 | const partsChunk = yield* LanguageModel.streamText({ prompt: "hello" }).pipe( |
| 2139 | Stream.runCollect, |
| 2140 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 2141 | Effect.provide(layer) |
| 2142 | ) |
| 2143 | |
| 2144 | const parts = globalThis.Array.from(partsChunk) |
| 2145 | const metadata = parts.find((part) => part.type === "response-metadata") |
| 2146 | const finish = parts.find((part) => part.type === "finish") |
| 2147 | const deltas = parts.filter((part) => part.type === "text-delta") |
| 2148 | |
| 2149 | assert.isDefined(metadata) |
| 2150 | assert.isDefined(finish) |
| 2151 | assert.strictEqual(deltas.length, 1) |
| 2152 | assert.strictEqual(deltas[0]?.delta, "Hello") |
| 2153 | if (finish?.type === "finish") { |
| 2154 | assert.strictEqual(finish.reason, "stop") |
| 2155 | assert.deepStrictEqual(finish.usage.inputTokens, { |
| 2156 | uncached: 7, |
| 2157 | total: 10, |
| 2158 | cacheRead: 3, |
| 2159 | cacheWrite: undefined |
| 2160 | }) |
| 2161 | assert.deepStrictEqual(finish.usage.outputTokens, { |
| 2162 | total: 7, |
| 2163 | text: 5, |
| 2164 | reasoning: 2 |
| 2165 | }) |
| 2166 | } |
| 2167 | |
| 2168 | assert.isDefined(capturedRequest) |
| 2169 | if (capturedRequest === undefined) { |
| 2170 | return |
| 2171 | } |
| 2172 | |
| 2173 | const requestBody = yield* getRequestBody(capturedRequest) |
| 2174 | assert.strictEqual(requestBody.stream, true) |
| 2175 | assert.isTrue(capturedRequest.url.endsWith("/chat/completions")) |
| 2176 | })) |
| 2177 | |
| 2178 | it.effect("assembles streamed tool args when continuation fragments have function.name: null", () => |
| 2179 | Effect.gen(function*() { |
| 2180 | // Some OpenAI-compatible providers (e.g. Fireworks) only send the tool |
| 2181 | // name on the first fragment and `function.name: null` on every |
| 2182 | // continuation. The argument fragments live on those continuations, so |
| 2183 | // they must not be dropped during chunk validation. |
| 2184 | const chunk = (fnDelta: Record<string, unknown>) => ({ |
| 2185 | id: "chatcmpl_null_name_1", |
| 2186 | object: "chat.completion.chunk", |
| 2187 | model: "gpt-4o-mini", |
| 2188 | created: 1, |
| 2189 | choices: [{ |
| 2190 | index: 0, |
| 2191 | delta: { tool_calls: [{ index: 0, id: "call_1", type: "function", function: fnDelta }] } |
| 2192 | }] |
| 2193 | }) |
| 2194 | |
| 2195 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 2196 | Layer.provide(Layer.succeed( |
| 2197 | HttpClient.HttpClient, |
| 2198 | makeHttpClient((request) => |
| 2199 | Effect.succeed(sseResponse(request, [ |
| 2200 | chunk({ name: "TestTool", arguments: "" }), |
| 2201 | chunk({ name: null, arguments: "{\"in" }), |
| 2202 | chunk({ name: null, arguments: "put\":\"hel" }), |
| 2203 | chunk({ name: null, arguments: "lo\"}" }), |
| 2204 | { |
| 2205 | id: "chatcmpl_null_name_1", |
| 2206 | object: "chat.completion.chunk", |
| 2207 | model: "gpt-4o-mini", |
| 2208 | created: 1, |
| 2209 | choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] |
| 2210 | }, |
| 2211 | "[DONE]" |
| 2212 | ])) |
| 2213 | ) |
| 2214 | )) |
| 2215 | ) |
| 2216 | |
| 2217 | const partsChunk = yield* LanguageModel.streamText({ |
| 2218 | prompt: "use the tool", |
| 2219 | toolkit: TestToolkit, |
| 2220 | disableToolCallResolution: true |
| 2221 | }).pipe( |
| 2222 | Stream.runCollect, |
| 2223 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 2224 | Effect.provide(TestToolkitLayer), |
| 2225 | Effect.provide(layer) |
| 2226 | ) |
| 2227 | |
| 2228 | const parts = globalThis.Array.from(partsChunk) |
| 2229 | const paramsDeltas = parts.filter((part) => part.type === "tool-params-delta") |
| 2230 | assert.isAbove(paramsDeltas.length, 0) |
| 2231 | |
| 2232 | const toolCall = parts.find((part) => part.type === "tool-call") |
| 2233 | assert.isDefined(toolCall) |
| 2234 | if (toolCall?.type !== "tool-call") { |
| 2235 | return |
| 2236 | } |
| 2237 | assert.strictEqual(toolCall.name, "TestTool") |
| 2238 | assert.deepStrictEqual(toolCall.params, { input: "hello" }) |
| 2239 | })) |
| 2240 | |
| 2241 | it.effect("emits reasoning lifecycle parts for delta.reasoning", () => |
| 2242 | Effect.gen(function*() { |
| 2243 | const chunk = (delta: Record<string, unknown>, finishReason: string | null = null) => ({ |
| 2244 | id: "chatcmpl_reasoning_1", |
| 2245 | object: "chat.completion.chunk", |
| 2246 | model: "gpt-4o-mini", |
| 2247 | created: 1, |
| 2248 | choices: [{ index: 0, delta, finish_reason: finishReason }] |
| 2249 | }) |
| 2250 | |
| 2251 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 2252 | Layer.provide(Layer.succeed( |
| 2253 | HttpClient.HttpClient, |
| 2254 | makeHttpClient((request) => |
| 2255 | Effect.succeed(sseResponse(request, [ |
| 2256 | chunk({ content: "", reasoning: "Let me think" }), |
| 2257 | chunk({ content: "", reasoning: " about this." }), |
| 2258 | chunk({ content: "Hello" }), |
| 2259 | chunk({ content: " there" }), |
| 2260 | chunk({ content: "" }, "stop"), |
| 2261 | "[DONE]" |
| 2262 | ])) |
| 2263 | ) |
| 2264 | )) |
| 2265 | ) |
| 2266 | |
| 2267 | const partsChunk = yield* LanguageModel.streamText({ prompt: "test" }).pipe( |
| 2268 | Stream.runCollect, |
| 2269 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 2270 | Effect.provide(layer) |
| 2271 | ) |
| 2272 | |
| 2273 | const parts = globalThis.Array.from(partsChunk) |
| 2274 | assert.deepStrictEqual(parts.map((part) => part.type), [ |
| 2275 | "response-metadata", |
| 2276 | "reasoning-start", |
| 2277 | "reasoning-delta", |
| 2278 | "reasoning-delta", |
| 2279 | "reasoning-end", |
| 2280 | "text-start", |
| 2281 | "text-delta", |
| 2282 | "text-delta", |
| 2283 | "text-end", |
| 2284 | "finish" |
| 2285 | ]) |
| 2286 | |
| 2287 | const reasoningText = parts |
| 2288 | .flatMap((part) => part.type === "reasoning-delta" ? [part.delta] : []) |
| 2289 | .join("") |
| 2290 | assert.strictEqual(reasoningText, "Let me think about this.") |
| 2291 | |
| 2292 | const reasoningIds = parts.flatMap((part) => |
| 2293 | part.type === "reasoning-start" || part.type === "reasoning-delta" || part.type === "reasoning-end" |
| 2294 | ? [part.id] |
| 2295 | : [] |
| 2296 | ) |
| 2297 | assert.strictEqual(new Set(reasoningIds).size, 1) |
| 2298 | |
| 2299 | const textIds = parts.flatMap((part) => part.type === "text-start" ? [part.id] : []) |
| 2300 | assert.notStrictEqual(reasoningIds[0], textIds[0]) |
| 2301 | })) |
| 2302 | |
| 2303 | it.effect("emits reasoning lifecycle parts for delta.reasoning_content", () => |
| 2304 | Effect.gen(function*() { |
| 2305 | const chunk = (delta: Record<string, unknown>, finishReason: string | null = null) => ({ |
| 2306 | id: "chatcmpl_reasoning_2", |
| 2307 | object: "chat.completion.chunk", |
| 2308 | model: "gpt-4o-mini", |
| 2309 | created: 1, |
| 2310 | choices: [{ index: 0, delta, finish_reason: finishReason }] |
| 2311 | }) |
| 2312 | |
| 2313 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 2314 | Layer.provide(Layer.succeed( |
| 2315 | HttpClient.HttpClient, |
| 2316 | makeHttpClient((request) => |
| 2317 | Effect.succeed(sseResponse(request, [ |
| 2318 | chunk({ reasoning_content: "Thinking..." }), |
| 2319 | chunk({ content: "Answer." }, "stop"), |
| 2320 | "[DONE]" |
| 2321 | ])) |
| 2322 | ) |
| 2323 | )) |
| 2324 | ) |
| 2325 | |
| 2326 | const partsChunk = yield* LanguageModel.streamText({ prompt: "test" }).pipe( |
| 2327 | Stream.runCollect, |
| 2328 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 2329 | Effect.provide(layer) |
| 2330 | ) |
| 2331 | |
| 2332 | const parts = globalThis.Array.from(partsChunk) |
| 2333 | assert.deepStrictEqual(parts.map((part) => part.type), [ |
| 2334 | "response-metadata", |
| 2335 | "reasoning-start", |
| 2336 | "reasoning-delta", |
| 2337 | "reasoning-end", |
| 2338 | "text-start", |
| 2339 | "text-delta", |
| 2340 | "text-end", |
| 2341 | "finish" |
| 2342 | ]) |
| 2343 | |
| 2344 | const reasoningText = parts |
| 2345 | .flatMap((part) => part.type === "reasoning-delta" ? [part.delta] : []) |
| 2346 | .join("") |
| 2347 | assert.strictEqual(reasoningText, "Thinking...") |
| 2348 | })) |
| 2349 | |
| 2350 | it.effect("closes an open reasoning part at stream end", () => |
| 2351 | Effect.gen(function*() { |
| 2352 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 2353 | Layer.provide(Layer.succeed( |
| 2354 | HttpClient.HttpClient, |
| 2355 | makeHttpClient((request) => |
| 2356 | Effect.succeed(sseResponse(request, [ |
| 2357 | { |
| 2358 | id: "chatcmpl_reasoning_3", |
| 2359 | object: "chat.completion.chunk", |
| 2360 | model: "gpt-4o-mini", |
| 2361 | created: 1, |
| 2362 | choices: [{ index: 0, delta: { reasoning: "Only thoughts." }, finish_reason: "stop" }] |
| 2363 | }, |
| 2364 | "[DONE]" |
| 2365 | ])) |
| 2366 | ) |
| 2367 | )) |
| 2368 | ) |
| 2369 | |
| 2370 | const partsChunk = yield* LanguageModel.streamText({ prompt: "test" }).pipe( |
| 2371 | Stream.runCollect, |
| 2372 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")), |
| 2373 | Effect.provide(layer) |
| 2374 | ) |
| 2375 | |
| 2376 | const parts = globalThis.Array.from(partsChunk) |
| 2377 | assert.deepStrictEqual(parts.map((part) => part.type), [ |
| 2378 | "response-metadata", |
| 2379 | "reasoning-start", |
| 2380 | "reasoning-delta", |
| 2381 | "reasoning-end", |
| 2382 | "finish" |
| 2383 | ]) |
| 2384 | })) |
| 2385 | }) |
| 2386 | |
| 2387 | describe("config", () => { |
| 2388 | it.effect("does not leak library-only fields into request body", () => |
| 2389 | Effect.gen(function*() { |
| 2390 | let capturedRequest: HttpClientRequest.HttpClientRequest | undefined |
| 2391 | |
| 2392 | const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe( |
| 2393 | Layer.provide(Layer.succeed( |
| 2394 | HttpClient.HttpClient, |
| 2395 | makeHttpClient((request) => { |
| 2396 | capturedRequest = request |
| 2397 | return Effect.succeed(jsonResponse(request, makeChatCompletion())) |
| 2398 | }) |
| 2399 | )) |
| 2400 | ) |
| 2401 | |
| 2402 | yield* LanguageModel.generateText({ prompt: "test" }).pipe( |
| 2403 | Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini", { |
| 2404 | fileIdPrefixes: ["file-"], |
| 2405 | strictJsonSchema: false, |
| 2406 | temperature: 0.5 |
| 2407 | })), |
| 2408 | Effect.provide(layer) |
| 2409 | ) |
| 2410 | |
| 2411 | assert.isDefined(capturedRequest) |
| 2412 | if (capturedRequest === undefined) return |
| 2413 | |
| 2414 | const requestBody = yield* getRequestBody(capturedRequest) |
| 2415 | assert.strictEqual(requestBody.fileIdPrefixes, undefined) |
| 2416 | assert.strictEqual(requestBody.strictJsonSchema, undefined) |
| 2417 | assert.strictEqual(requestBody.temperature, 0.5) |
| 2418 | })) |
| 2419 | }) |
| 2420 | }) |
| 2421 | |
| 2422 | const TestTool = Tool.make("TestTool", { |
| 2423 | description: "A test tool", |
| 2424 | parameters: Schema.Struct({ input: Schema.String }), |
| 2425 | success: Schema.Struct({ output: Schema.String }) |
| 2426 | }) |
| 2427 | |
| 2428 | const TestToolkit = Toolkit.make(TestTool) |
| 2429 | |
| 2430 | const TestToolkitLayer = TestToolkit.toLayer({ |
| 2431 | TestTool: ({ input }) => Effect.succeed({ output: input }) |
| 2432 | }) |
| 2433 | |
| 2434 | const RecordTool = Tool.make("RecordTool", { |
| 2435 | parameters: Schema.Struct({ |
| 2436 | env: Schema.Record(Schema.String, Schema.String) |
| 2437 | }), |
| 2438 | success: Schema.String |
| 2439 | }) |
| 2440 | |
| 2441 | const RecordToolkit = Toolkit.make(RecordTool) |
| 2442 | |
| 2443 | const RecordToolkitLayer = RecordToolkit.toLayer({ |
| 2444 | RecordTool: () => Effect.succeed("done") |
| 2445 | }) |
| 2446 | |
| 2447 | const CompatApplyPatchTool = Tool.providerDefined({ |
| 2448 | id: "compat.apply_patch", |
| 2449 | customName: "CompatApplyPatch", |
| 2450 | providerName: "apply_patch", |
| 2451 | requiresHandler: true, |
| 2452 | parameters: Schema.Struct({ |
| 2453 | call_id: Schema.String, |
| 2454 | operation: Schema.Any |
| 2455 | }), |
| 2456 | success: Schema.Struct({ |
| 2457 | status: Schema.Literals(["completed", "failed"]), |
| 2458 | output: Schema.optionalKey(Schema.NullOr(Schema.String)) |
| 2459 | }) |
| 2460 | }) |
| 2461 | |
| 2462 | const localShellAction = { |
| 2463 | type: "exec", |
| 2464 | command: ["pwd"], |
| 2465 | env: {} |
| 2466 | } |
| 2467 | |
| 2468 | const CompatLocalShellTool = Tool.providerDefined({ |
| 2469 | id: "compat.local_shell", |
| 2470 | customName: "CompatLocalShell", |
| 2471 | providerName: "local_shell", |
| 2472 | requiresHandler: true, |
| 2473 | parameters: Schema.Struct({ |
| 2474 | action: Schema.Any |
| 2475 | }), |
| 2476 | success: Schema.String |
| 2477 | }) |
| 2478 | |
| 2479 | const makeHttpClient = ( |
| 2480 | handler: ( |
| 2481 | request: HttpClientRequest.HttpClientRequest |
| 2482 | ) => Effect.Effect<HttpClientResponse.HttpClientResponse, HttpClientError.HttpClientError> |
| 2483 | ) => |
| 2484 | HttpClient.makeWith( |
| 2485 | Effect.fnUntraced(function*(requestEffect) { |
| 2486 | const request = yield* requestEffect |
| 2487 | return yield* handler(request) |
| 2488 | }), |
| 2489 | Effect.succeed as HttpClient.HttpClient.Preprocess<HttpClientError.HttpClientError, never> |
| 2490 | ) |
| 2491 | |
| 2492 | const makeChatCompletion = (overrides: Record<string, unknown> = {}) => ({ |
| 2493 | id: "chatcmpl_test_1", |
| 2494 | object: "chat.completion", |
| 2495 | model: "gpt-4o-mini", |
| 2496 | created: 1, |
| 2497 | choices: [{ |
| 2498 | index: 0, |
| 2499 | finish_reason: "stop", |
| 2500 | message: { |
| 2501 | role: "assistant", |
| 2502 | content: "" |
| 2503 | } |
| 2504 | }], |
| 2505 | ...overrides |
| 2506 | }) |
| 2507 | |
| 2508 | const makeLocalShellChunk = () => ({ |
| 2509 | id: "chatcmpl_local_shell_1", |
| 2510 | object: "chat.completion.chunk", |
| 2511 | model: "gpt-4o-mini", |
| 2512 | created: 1, |
| 2513 | choices: [{ |
| 2514 | index: 0, |
| 2515 | delta: { |
| 2516 | tool_calls: [{ |
| 2517 | index: 0, |
| 2518 | id: "local_shell_call_1", |
| 2519 | type: "function", |
| 2520 | function: { |
| 2521 | name: "local_shell", |
| 2522 | arguments: JSON.stringify({ action: localShellAction }) |
| 2523 | } |
| 2524 | }] |
| 2525 | }, |
| 2526 | finish_reason: "tool_calls" |
| 2527 | }] |
| 2528 | }) |
| 2529 | |
| 2530 | const jsonResponse = ( |
| 2531 | request: HttpClientRequest.HttpClientRequest, |
| 2532 | body: unknown |
| 2533 | ): HttpClientResponse.HttpClientResponse => |
| 2534 | HttpClientResponse.fromWeb( |
| 2535 | request, |
| 2536 | new Response(JSON.stringify(body), { |
| 2537 | status: 200, |
| 2538 | headers: { |
| 2539 | "content-type": "application/json" |
| 2540 | } |
| 2541 | }) |
| 2542 | ) |
| 2543 | |
| 2544 | const sseResponse = ( |
| 2545 | request: HttpClientRequest.HttpClientRequest, |
| 2546 | events: ReadonlyArray<unknown> |
| 2547 | ): HttpClientResponse.HttpClientResponse => |
| 2548 | HttpClientResponse.fromWeb( |
| 2549 | request, |
| 2550 | new Response(toSseBody(events), { |
| 2551 | status: 200, |
| 2552 | headers: { |
| 2553 | "content-type": "text/event-stream" |
| 2554 | } |
| 2555 | }) |
| 2556 | ) |
| 2557 | |
| 2558 | const getRequestBody = Effect.fnUntraced(function*(request: HttpClientRequest.HttpClientRequest) { |
| 2559 | const body = request.body |
| 2560 | if (body._tag === "Uint8Array") { |
| 2561 | const text = new TextDecoder().decode(body.body) |
| 2562 | return JSON.parse(text) |
| 2563 | } |
| 2564 | return yield* Effect.die(new Error("Expected Uint8Array body")) |
| 2565 | }) |
| 2566 | |
| 2567 | const decodeToolParamsFromStream = ( |
| 2568 | parts: ReadonlyArray<any>, |
| 2569 | toolCallId: string |
| 2570 | ): Record<string, unknown> => { |
| 2571 | const start = parts.find((part) => part.type === "tool-params-start" && part.id === toolCallId) |
| 2572 | const end = parts.find((part) => part.type === "tool-params-end" && part.id === toolCallId) |
| 2573 | assert.isDefined(start) |
| 2574 | assert.isDefined(end) |
| 2575 | |
| 2576 | const deltas = parts |
| 2577 | .filter((part) => part.type === "tool-params-delta" && part.id === toolCallId) |
| 2578 | .map((part) => part.delta) |
| 2579 | .join("") |
| 2580 | |
| 2581 | return JSON.parse(deltas) as Record<string, unknown> |
| 2582 | } |
| 2583 | |
| 2584 | const toSseBody = (events: ReadonlyArray<unknown>): string => |
| 2585 | events.map((event) => { |
| 2586 | if (typeof event === "string") { |
| 2587 | return `data: ${event}\n\n` |
| 2588 | } |
| 2589 | return `data: ${JSON.stringify(event)}\n\n` |
| 2590 | }).join("") |