schema.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { describe, expect, test } from "bun:test"
  2. import { Schema } from "effect"
  3. import * as OpenAIChat from "../src/protocols/openai-chat"
  4. import * as OpenAIResponses from "../src/protocols/openai-responses"
  5. import { ContentPart, LLMEvent, LLMRequest, Model, ModelID, ProviderID, Usage } from "../src/schema"
  6. import { ProviderShared } from "../src/protocols/shared"
  7. const model = new Model({
  8. id: ModelID.make("fake-model"),
  9. provider: ProviderID.make("fake-provider"),
  10. route: OpenAIChat.route,
  11. })
  12. const decodeLLMRequest = Schema.decodeUnknownSync(LLMRequest as unknown as Schema.Decoder<LLMRequest>)
  13. const decodeLLMEvent = Schema.decodeUnknownSync(LLMEvent as unknown as Schema.Decoder<LLMEvent>)
  14. describe("llm schema", () => {
  15. test("decodes a minimal request", () => {
  16. const input: unknown = {
  17. id: "req_1",
  18. model,
  19. system: [{ type: "text", text: "You are terse." }],
  20. messages: [{ role: "user", content: [{ type: "text", text: "hi" }] }],
  21. tools: [],
  22. generation: {},
  23. }
  24. const decoded = decodeLLMRequest(input)
  25. expect(decoded.id).toBe("req_1")
  26. expect(decoded.messages[0]?.content[0]?.type).toBe("text")
  27. })
  28. test("accepts custom route ids", () => {
  29. const decoded = decodeLLMRequest({
  30. model: Model.update(model, { route: OpenAIResponses.route }),
  31. system: [],
  32. messages: [],
  33. tools: [],
  34. generation: {},
  35. })
  36. expect(decoded.model.route.id).toBe("openai-responses")
  37. })
  38. test("rejects invalid event type", () => {
  39. expect(() => decodeLLMEvent({ type: "bogus" })).toThrow()
  40. })
  41. test("finish constructors accept usage input", () => {
  42. expect(
  43. LLMEvent.stepFinish({ index: 0, reason: { normalized: "stop" }, usage: { inputTokens: 1 } }).usage,
  44. ).toBeInstanceOf(Usage)
  45. expect(LLMEvent.finish({ reason: { normalized: "stop" }, usage: { outputTokens: 2 } }).usage).toBeInstanceOf(
  46. Usage,
  47. )
  48. })
  49. test("content part tagged union exposes guards", () => {
  50. expect(ContentPart.guards.text({ type: "text", text: "hi" })).toBe(true)
  51. expect(ContentPart.guards.media({ type: "text", text: "hi" })).toBe(false)
  52. })
  53. })
  54. describe("LLM.Usage", () => {
  55. test("subtractTokens clamps non-sensical breakdowns to zero", () => {
  56. // Defense against a provider reporting cached_tokens > prompt_tokens or
  57. // reasoning_tokens > completion_tokens — the negative would otherwise
  58. // round-trip through the pipeline and crash strict downstream schemas.
  59. expect(ProviderShared.subtractTokens(5, 3)).toBe(2)
  60. expect(ProviderShared.subtractTokens(5, 10)).toBe(0)
  61. expect(ProviderShared.subtractTokens(5, undefined)).toBe(5)
  62. expect(ProviderShared.subtractTokens(undefined, 3)).toBeUndefined()
  63. expect(ProviderShared.subtractTokens(undefined, undefined)).toBeUndefined()
  64. })
  65. test("sumTokens returns undefined only when every input is undefined", () => {
  66. expect(ProviderShared.sumTokens(1, 2, 3)).toBe(6)
  67. expect(ProviderShared.sumTokens(1, undefined, 3)).toBe(4)
  68. expect(ProviderShared.sumTokens(undefined, undefined, undefined)).toBeUndefined()
  69. expect(ProviderShared.sumTokens()).toBeUndefined()
  70. })
  71. test("visibleOutputTokens clamps reasoning > output to zero", () => {
  72. expect(new Usage({ outputTokens: 10, reasoningTokens: 4 }).visibleOutputTokens).toBe(6)
  73. expect(new Usage({ outputTokens: 10 }).visibleOutputTokens).toBe(10)
  74. expect(new Usage({ outputTokens: 4, reasoningTokens: 10 }).visibleOutputTokens).toBe(0)
  75. expect(new Usage({}).visibleOutputTokens).toBe(0)
  76. })
  77. })