| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- import { describe, expect } from "bun:test"
- import { Effect, Schema, Stream } from "effect"
- import { HttpClientRequest } from "effect/unstable/http"
- import { LLM, LLMError } from "../../src"
- import * as Azure from "../../src/providers/azure"
- import * as OpenAI from "../../src/providers/openai"
- import * as OpenAIChat from "../../src/protocols/openai-chat"
- import { LLMClient } from "../../src/route"
- import { it } from "../lib/effect"
- import { dynamicResponse, fixedResponse, truncatedStream } from "../lib/http"
- import { deltaChunk, usageChunk } from "../lib/openai-chunks"
- import { sseEvents } from "../lib/sse"
- const TargetJson = Schema.fromJsonString(Schema.Unknown)
- const encodeJson = Schema.encodeSync(TargetJson)
- const decodeJson = Schema.decodeUnknownSync(TargetJson)
- const model = OpenAIChat.model({
- id: "gpt-4o-mini",
- baseURL: "https://api.openai.test/v1/",
- headers: { authorization: "Bearer test" },
- })
- const request = LLM.request({
- id: "req_1",
- model,
- system: "You are concise.",
- prompt: "Say hello.",
- generation: { maxTokens: 20, temperature: 0 },
- })
- describe("OpenAI Chat route", () => {
- it.effect("prepares OpenAI Chat payload", () =>
- Effect.gen(function* () {
- // Pass the OpenAIChat payload type so `prepared.body` is statically
- // typed to the route's native shape — the assertions below read field
- // names without `unknown` casts.
- const prepared = yield* LLMClient.prepare<OpenAIChat.OpenAIChatBody>(request)
- const _typed: { readonly model: string; readonly stream: true } = prepared.body
- expect(prepared.body).toEqual({
- model: "gpt-4o-mini",
- messages: [
- { role: "system", content: "You are concise." },
- { role: "user", content: "Say hello." },
- ],
- stream: true,
- stream_options: { include_usage: true },
- max_tokens: 20,
- temperature: 0,
- })
- }),
- )
- it.effect("maps OpenAI provider options to Chat options", () =>
- Effect.gen(function* () {
- const prepared = yield* LLMClient.prepare<OpenAIChat.OpenAIChatBody>(
- LLM.request({
- model: OpenAI.chat("gpt-4o-mini", { baseURL: "https://api.openai.test/v1/" }),
- prompt: "think",
- providerOptions: { openai: { reasoningEffort: "low" } },
- }),
- )
- expect(prepared.body.store).toBe(false)
- expect(prepared.body.reasoning_effort).toBe("low")
- }),
- )
- it.effect("adds native query params to the Chat Completions URL", () =>
- LLMClient.generate(
- LLM.updateRequest(request, { model: OpenAIChat.model({ ...model, queryParams: { "api-version": "v1" } }) }),
- ).pipe(
- Effect.provide(
- dynamicResponse((input) =>
- Effect.gen(function* () {
- const web = yield* HttpClientRequest.toWeb(input.request).pipe(Effect.orDie)
- expect(web.url).toBe("https://api.openai.test/v1/chat/completions?api-version=v1")
- return input.respond(sseEvents(deltaChunk({}, "stop")), {
- headers: { "content-type": "text/event-stream" },
- })
- }),
- ),
- ),
- ),
- )
- it.effect("uses Azure api-key header for static OpenAI Chat keys", () =>
- LLMClient.generate(
- LLM.updateRequest(request, {
- model: Azure.chat("gpt-4o-mini", {
- baseURL: "https://opencode-test.openai.azure.com/openai/v1/",
- apiKey: "azure-key",
- headers: { authorization: "Bearer stale" },
- }),
- }),
- ).pipe(
- Effect.provide(
- dynamicResponse((input) =>
- Effect.gen(function* () {
- const web = yield* HttpClientRequest.toWeb(input.request).pipe(Effect.orDie)
- expect(web.headers.get("api-key")).toBe("azure-key")
- expect(web.headers.get("authorization")).toBeNull()
- return input.respond(sseEvents(deltaChunk({}, "stop")), {
- headers: { "content-type": "text/event-stream" },
- })
- }),
- ),
- ),
- ),
- )
- it.effect("applies serializable HTTP overlays after payload lowering", () =>
- LLMClient.generate(
- LLM.updateRequest(request, {
- model: OpenAIChat.model({ ...model, apiKey: "fresh-key", headers: { authorization: "Bearer stale" } }),
- http: {
- body: { metadata: { source: "test" } },
- headers: { authorization: "Bearer request", "x-custom": "yes" },
- query: { debug: "1" },
- },
- }),
- ).pipe(
- Effect.provide(
- dynamicResponse((input) =>
- Effect.gen(function* () {
- const web = yield* HttpClientRequest.toWeb(input.request).pipe(Effect.orDie)
- expect(web.url).toBe("https://api.openai.test/v1/chat/completions?debug=1")
- expect(web.headers.get("authorization")).toBe("Bearer fresh-key")
- expect(web.headers.get("x-custom")).toBe("yes")
- expect(decodeJson(input.text)).toMatchObject({
- stream: true,
- stream_options: { include_usage: true },
- metadata: { source: "test" },
- })
- return input.respond(sseEvents(deltaChunk({}, "stop")), {
- headers: { "content-type": "text/event-stream" },
- })
- }),
- ),
- ),
- ),
- )
- it.effect("prepares assistant tool-call and tool-result messages", () =>
- Effect.gen(function* () {
- const prepared = yield* LLMClient.prepare(
- LLM.request({
- id: "req_tool_result",
- model,
- messages: [
- LLM.user("What is the weather?"),
- LLM.assistant([LLM.toolCall({ id: "call_1", name: "lookup", input: { query: "weather" } })]),
- LLM.toolMessage({ id: "call_1", name: "lookup", result: { forecast: "sunny" } }),
- ],
- }),
- )
- expect(prepared.body).toEqual({
- model: "gpt-4o-mini",
- messages: [
- { role: "user", content: "What is the weather?" },
- {
- role: "assistant",
- content: null,
- tool_calls: [
- {
- id: "call_1",
- type: "function",
- function: { name: "lookup", arguments: encodeJson({ query: "weather" }) },
- },
- ],
- },
- { role: "tool", tool_call_id: "call_1", content: encodeJson({ forecast: "sunny" }) },
- ],
- stream: true,
- stream_options: { include_usage: true },
- })
- }),
- )
- it.effect("rejects unsupported user media content", () =>
- Effect.gen(function* () {
- const error = yield* LLMClient.prepare(
- LLM.request({
- id: "req_media",
- model,
- messages: [LLM.user({ type: "media", mediaType: "image/png", data: "AAECAw==" })],
- }),
- ).pipe(Effect.flip)
- expect(error.message).toContain("OpenAI Chat user messages only support text content for now")
- }),
- )
- it.effect("rejects unsupported assistant reasoning content", () =>
- Effect.gen(function* () {
- const error = yield* LLMClient.prepare(
- LLM.request({
- id: "req_reasoning",
- model,
- messages: [LLM.assistant({ type: "reasoning", text: "hidden" })],
- }),
- ).pipe(Effect.flip)
- expect(error.message).toContain("OpenAI Chat assistant messages only support text and tool-call content for now")
- }),
- )
- it.effect("parses text and usage stream fixtures", () =>
- Effect.gen(function* () {
- const body = sseEvents(
- deltaChunk({ role: "assistant", content: "Hello" }),
- deltaChunk({ content: "!" }),
- deltaChunk({}, "stop"),
- usageChunk({
- prompt_tokens: 5,
- completion_tokens: 2,
- total_tokens: 7,
- prompt_tokens_details: { cached_tokens: 1 },
- completion_tokens_details: { reasoning_tokens: 0 },
- }),
- )
- const response = yield* LLMClient.generate(request).pipe(Effect.provide(fixedResponse(body)))
- expect(response.text).toBe("Hello!")
- expect(response.events).toEqual([
- { type: "text-delta", text: "Hello" },
- { type: "text-delta", text: "!" },
- {
- type: "request-finish",
- reason: "stop",
- usage: {
- inputTokens: 5,
- outputTokens: 2,
- reasoningTokens: 0,
- cacheReadInputTokens: 1,
- totalTokens: 7,
- native: {
- prompt_tokens: 5,
- completion_tokens: 2,
- total_tokens: 7,
- prompt_tokens_details: { cached_tokens: 1 },
- completion_tokens_details: { reasoning_tokens: 0 },
- },
- },
- },
- ])
- }),
- )
- it.effect("assembles streamed tool call input", () =>
- Effect.gen(function* () {
- const body = sseEvents(
- deltaChunk({
- role: "assistant",
- tool_calls: [{ index: 0, id: "call_1", function: { name: "lookup", arguments: '{"query"' } }],
- }),
- deltaChunk({ tool_calls: [{ index: 0, function: { arguments: ':"weather"}' } }] }),
- deltaChunk({}, "tool_calls"),
- )
- const response = yield* LLMClient.generate(
- LLM.updateRequest(request, {
- tools: [{ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } }],
- }),
- ).pipe(Effect.provide(fixedResponse(body)))
- expect(response.events).toEqual([
- { type: "tool-input-delta", id: "call_1", name: "lookup", text: '{"query"' },
- { type: "tool-input-delta", id: "call_1", name: "lookup", text: ':"weather"}' },
- { type: "tool-call", id: "call_1", name: "lookup", input: { query: "weather" } },
- { type: "request-finish", reason: "tool-calls", usage: undefined },
- ])
- }),
- )
- it.effect("does not finalize streamed tool calls without a finish reason", () =>
- Effect.gen(function* () {
- const body = sseEvents(
- deltaChunk({
- role: "assistant",
- tool_calls: [{ index: 0, id: "call_1", function: { name: "lookup", arguments: '{"query"' } }],
- }),
- deltaChunk({ tool_calls: [{ index: 0, function: { arguments: ':"weather"}' } }] }),
- )
- const response = yield* LLMClient.generate(
- LLM.updateRequest(request, {
- tools: [{ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } }],
- }),
- ).pipe(Effect.provide(fixedResponse(body)))
- expect(response.events).toEqual([
- { type: "tool-input-delta", id: "call_1", name: "lookup", text: '{"query"' },
- { type: "tool-input-delta", id: "call_1", name: "lookup", text: ':"weather"}' },
- ])
- expect(response.toolCalls).toEqual([])
- }),
- )
- it.effect("fails on malformed stream events", () =>
- Effect.gen(function* () {
- const body = sseEvents(deltaChunk({ content: 123 }))
- const error = yield* LLMClient.generate(request).pipe(Effect.provide(fixedResponse(body)), Effect.flip)
- expect(error.message).toContain("Invalid openai/openai-chat stream event")
- }),
- )
- it.effect("surfaces transport errors that occur mid-stream", () =>
- Effect.gen(function* () {
- const layer = truncatedStream([
- `data: ${JSON.stringify(deltaChunk({ role: "assistant", content: "Hello" }))}\n\n`,
- ])
- const error = yield* LLMClient.generate(request).pipe(Effect.provide(layer), Effect.flip)
- expect(error.message).toContain("Failed to read openai/openai-chat stream")
- }),
- )
- it.effect("fails HTTP provider errors before stream parsing", () =>
- Effect.gen(function* () {
- const error = yield* LLMClient.generate(request).pipe(
- Effect.provide(
- fixedResponse('{"error":{"message":"Bad request","type":"invalid_request_error"}}', {
- status: 400,
- headers: { "content-type": "application/json" },
- }),
- ),
- Effect.flip,
- )
- expect(error).toBeInstanceOf(LLMError)
- expect(error.reason).toMatchObject({ _tag: "InvalidRequest" })
- expect(error.message).toContain("HTTP 400")
- }),
- )
- it.effect("short-circuits the upstream stream when the consumer takes a prefix", () =>
- Effect.gen(function* () {
- // The body has more chunks than we'll consume. If `Stream.take(1)` did
- // not interrupt the upstream HTTP body the test would hang waiting for
- // the rest of the stream to drain.
- const body = sseEvents(
- deltaChunk({ role: "assistant", content: "Hello" }),
- deltaChunk({ content: " world" }),
- deltaChunk({}, "stop"),
- )
- const events = Array.from(
- yield* LLMClient.stream(request).pipe(Stream.take(1), Stream.runCollect, Effect.provide(fixedResponse(body))),
- )
- expect(events.map((event) => event.type)).toEqual(["text-delta"])
- }),
- )
- })
|