session-model-request.test.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { describe, expect, test } from "bun:test"
  2. import { Message, ToolResultPart } from "@opencode-ai/ai"
  3. import { unsupportedParts } from "@opencode-ai/core/session/model-request"
  4. const capabilities = (input: string[]) => ({ tools: true, input, output: ["text"] })
  5. describe("SessionModelRequest.unsupportedParts", () => {
  6. test("replaces unsupported user media with a visible error", () => {
  7. const messages = unsupportedParts(
  8. [
  9. Message.user([
  10. Message.text("Describe this image"),
  11. { type: "media", mediaType: "image/png", data: "aGVsbG8=", filename: "logo.png" },
  12. ]),
  13. ],
  14. capabilities(["text"]),
  15. )
  16. expect(messages[0]?.content).toEqual([
  17. Message.text("Describe this image"),
  18. Message.text('ERROR: Cannot read "logo.png" (this model does not support image input). Inform the user.'),
  19. ])
  20. })
  21. test("replaces unsupported media nested in tool results", () => {
  22. const messages = unsupportedParts(
  23. [
  24. Message.tool(
  25. ToolResultPart.make({
  26. id: "call_1",
  27. name: "read",
  28. result: {
  29. type: "content",
  30. value: [
  31. { type: "text", text: "Image read successfully" },
  32. { type: "file", uri: "data:image/png;base64,aGVsbG8=", mime: "image/png", name: "logo.png" },
  33. ],
  34. },
  35. }),
  36. ),
  37. ],
  38. capabilities(["text"]),
  39. )
  40. expect(messages[0]?.content[0]).toMatchObject({
  41. type: "tool-result",
  42. result: {
  43. type: "content",
  44. value: [
  45. { type: "text", text: "Image read successfully" },
  46. {
  47. type: "text",
  48. text: 'ERROR: Cannot read "logo.png" (this model does not support image input). Inform the user.',
  49. },
  50. ],
  51. },
  52. })
  53. })
  54. test("preserves supported media", () => {
  55. const message = Message.user({ type: "media", mediaType: "image/png", data: "aGVsbG8=" })
  56. expect(unsupportedParts([message], capabilities(["text", "image"]))[0]?.content).toEqual(message.content)
  57. })
  58. })