|
| 1 | +import { describe, expect } from "bun:test" |
| 2 | +import { Effect } from "effect" |
| 3 | +import { LLM, LLMEvent, Message } from "../../src/index.js" |
| 4 | +import { OpenAI } from "../../src/providers.js" |
| 5 | +import { configure } from "../../src/providers/openai-compatible-responses.js" |
| 6 | +import { compileRequest, LLMClient } from "../../src/route/client.js" |
| 7 | +import { it } from "../lib/effect.js" |
| 8 | +import { fixedResponse } from "../lib/http.js" |
| 9 | +import { sseEvents } from "../lib/sse.js" |
| 10 | + |
| 11 | +for (const model of [ |
| 12 | + OpenAI.configure({ apiKey: "test-key" }).responses("example-model"), |
| 13 | + configure({ apiKey: "test-key", baseURL: "https://responses.example.test/v1" }).model("example-model"), |
| 14 | +]) { |
| 15 | + describe(`${model.route.protocol} message replay`, () => { |
| 16 | + const key = model.route.providerMetadataKey ?? "openresponses" |
| 17 | + |
| 18 | + it.effect("marks assistant text completed regardless of stored status", () => |
| 19 | + Effect.gen(function* () { |
| 20 | + const prepared = yield* compileRequest( |
| 21 | + LLM.request({ |
| 22 | + model, |
| 23 | + messages: [ |
| 24 | + ...[undefined, "in_progress", "incomplete", "completed"].map((status, index) => |
| 25 | + Message.make({ |
| 26 | + role: "assistant", |
| 27 | + providerMetadata: { [key]: { status } }, |
| 28 | + content: [ |
| 29 | + { |
| 30 | + type: "text", |
| 31 | + text: `Saved ${index}`, |
| 32 | + providerMetadata: { [key]: { itemId: `msg_${index}`, phase: "commentary", status } }, |
| 33 | + }, |
| 34 | + { |
| 35 | + type: "text", |
| 36 | + text: `Final ${index}`, |
| 37 | + providerMetadata: { [key]: { itemId: `msg_final_${index}`, phase: "final_answer", status } }, |
| 38 | + }, |
| 39 | + ], |
| 40 | + }), |
| 41 | + ), |
| 42 | + Message.make({ |
| 43 | + role: "user", |
| 44 | + content: [{ type: "text", text: "Continue" }], |
| 45 | + providerMetadata: { [key]: { status: "incomplete" } }, |
| 46 | + }), |
| 47 | + ], |
| 48 | + }), |
| 49 | + ) |
| 50 | + expect(prepared.body.input).toEqual([ |
| 51 | + ...[0, 1, 2, 3].flatMap((index) => [ |
| 52 | + { |
| 53 | + type: "message", |
| 54 | + role: "assistant", |
| 55 | + id: `msg_${index}`, |
| 56 | + phase: "commentary", |
| 57 | + status: "completed", |
| 58 | + content: [{ type: "output_text", text: `Saved ${index}` }], |
| 59 | + }, |
| 60 | + { |
| 61 | + type: "message", |
| 62 | + role: "assistant", |
| 63 | + id: `msg_final_${index}`, |
| 64 | + phase: "final_answer", |
| 65 | + status: "completed", |
| 66 | + content: [{ type: "output_text", text: `Final ${index}` }], |
| 67 | + }, |
| 68 | + ]), |
| 69 | + { role: "user", status: "incomplete", content: [{ type: "input_text", text: "Continue" }] }, |
| 70 | + ]) |
| 71 | + }), |
| 72 | + ) |
| 73 | + |
| 74 | + it.effect("replays truncated text as completed while retaining the response finish reason", () => |
| 75 | + Effect.gen(function* () { |
| 76 | + const response = yield* LLMClient.generate(LLM.request({ model, prompt: "Respond" })).pipe( |
| 77 | + Effect.provide( |
| 78 | + fixedResponse( |
| 79 | + sseEvents( |
| 80 | + { |
| 81 | + type: "response.output_item.added", |
| 82 | + item: { type: "message", id: "msg_partial", status: "in_progress" }, |
| 83 | + }, |
| 84 | + { type: "response.output_text.delta", item_id: "msg_partial", delta: "The next step is" }, |
| 85 | + { |
| 86 | + type: "response.output_item.done", |
| 87 | + item: { |
| 88 | + type: "message", |
| 89 | + id: "msg_partial", |
| 90 | + status: "incomplete", |
| 91 | + content: [{ type: "output_text", text: "The next step is" }], |
| 92 | + }, |
| 93 | + }, |
| 94 | + { |
| 95 | + type: "response.incomplete", |
| 96 | + response: { status: "incomplete", incomplete_details: { reason: "max_output_tokens" } }, |
| 97 | + }, |
| 98 | + ), |
| 99 | + ), |
| 100 | + ), |
| 101 | + ) |
| 102 | + expect(response.finishReason.normalized).toBe("length") |
| 103 | + expect(response.events.filter(LLMEvent.is.textEnd)).toHaveLength(1) |
| 104 | + const prepared = yield* compileRequest( |
| 105 | + LLM.request({ model, messages: [response.message, Message.user("Continue")] }), |
| 106 | + ) |
| 107 | + expect(prepared.body.input).toEqual([ |
| 108 | + { |
| 109 | + type: "message", |
| 110 | + role: "assistant", |
| 111 | + id: "msg_partial", |
| 112 | + status: "completed", |
| 113 | + content: [{ type: "output_text", text: "The next step is" }], |
| 114 | + }, |
| 115 | + { role: "user", content: [{ type: "input_text", text: "Continue" }] }, |
| 116 | + ]) |
| 117 | + }), |
| 118 | + ) |
| 119 | + }) |
| 120 | +} |
0 commit comments