response-assembler.test.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { deepStrictEqual } from "node:assert/strict";
  2. import { describe, it } from "node:test";
  3. import {
  4. assembleNonStreamingAssistantMessage,
  5. assembleStreamingAssistantMessage,
  6. } from "../src/openai-response-assembler.js";
  7. describe("OpenAI response assembler", () => {
  8. it("extracts a non-streaming assistant message", () => {
  9. deepStrictEqual(
  10. assembleNonStreamingAssistantMessage({
  11. choices: [
  12. {
  13. message: {
  14. role: "assistant",
  15. content: "hello",
  16. },
  17. },
  18. ],
  19. }),
  20. {
  21. role: "assistant",
  22. content: "hello",
  23. },
  24. );
  25. });
  26. it("assembles streaming text and tool call deltas into one assistant message", () => {
  27. const chunks = [
  28. 'data: {"choices":[{"delta":{"role":"assistant","content":"hel"}}]}\n\n',
  29. 'data: {"choices":[{"delta":{"content":"lo","tool_calls":[{"index":0,"id":"call_1","type":"function","function":{"name":"Read","arguments":"{\\"path\\""}}]}}]}\n\n',
  30. 'data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":":\\"a.ts\\"}"}}]}}]}\n\n',
  31. "data: [DONE]\n\n",
  32. ];
  33. deepStrictEqual(assembleStreamingAssistantMessage(chunks), {
  34. role: "assistant",
  35. content: "hello",
  36. tool_calls: [
  37. {
  38. id: "call_1",
  39. type: "function",
  40. function: {
  41. name: "Read",
  42. arguments: "{\"path\":\"a.ts\"}",
  43. },
  44. },
  45. ],
  46. });
  47. });
  48. });