| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801 |
- import { describe, expect, test } from "bun:test"
- import { Message } from "@opencode-ai/ai"
- import { Model } from "@opencode-ai/core/model"
- import { Provider } from "@opencode-ai/core/provider"
- import { SessionMessage } from "@opencode-ai/core/session/message"
- import { AgentAttachment, Base64, FileAttachment } from "@opencode-ai/schema/prompt"
- import { toLLMMessages } from "@opencode-ai/core/session/runner/to-llm-message"
- import { Agent } from "@opencode-ai/core/agent"
- import { Shell } from "@opencode-ai/schema/shell"
- import { DateTime } from "effect"
- const created = DateTime.makeUnsafe(0)
- const id = (value: string) => SessionMessage.ID.make(`msg_${value}`)
- const model = Model.Ref.make({ id: Model.ID.make("model"), providerID: Provider.ID.make("provider") })
- const build = Agent.defaultID
- describe("toLLMMessages", () => {
- test("omits empty assistant turns", () => {
- const assistant = (value: string, content: SessionMessage.Assistant["content"]) =>
- SessionMessage.Assistant.make({
- id: id(value),
- type: "assistant",
- agent: build,
- model: { id: Model.ID.make("model"), providerID: Provider.ID.make("provider") },
- content,
- time: { created, completed: created },
- })
- const messages = toLLMMessages(
- [
- assistant("empty", []),
- assistant("empty-text", [SessionMessage.AssistantText.make({ type: "text", text: "" })]),
- assistant("empty-reasoning", [SessionMessage.AssistantReasoning.make({ type: "reasoning", text: "" })]),
- assistant("text", [SessionMessage.AssistantText.make({ type: "text", text: "Partial" })]),
- assistant("reasoning", [
- SessionMessage.AssistantReasoning.make({
- type: "reasoning",
- text: "",
- state: { signature: "sig_1" },
- }),
- ]),
- ],
- model,
- )
- expect(messages.map((message) => message.id)).toEqual([id("text"), id("reasoning")])
- })
- test("maps every top-level Session message type", () => {
- const file = FileAttachment.make({
- data: Base64.make("aGVsbG8="),
- mime: "image/png",
- source: { type: "inline" },
- name: "hello.png",
- })
- const messages = toLLMMessages(
- [
- SessionMessage.AgentSelected.make({
- id: id("agent"),
- type: "agent-switched",
- agent: build,
- time: { created },
- }),
- SessionMessage.ModelSelected.make({
- id: id("model"),
- type: "model-switched",
- model: { id: Model.ID.make("model"), providerID: Provider.ID.make("provider") },
- time: { created },
- }),
- SessionMessage.System.make({
- id: id("system"),
- type: "system",
- text: "Updated context\n\nOther context",
- time: { created },
- }),
- SessionMessage.User.make({
- id: id("user"),
- type: "user",
- text: "Inspect this image",
- files: [file],
- agents: [AgentAttachment.make({ name: "build" })],
- time: { created },
- }),
- SessionMessage.Synthetic.make({
- id: id("synthetic"),
- type: "synthetic",
- text: "Synthetic context",
- time: { created },
- }),
- SessionMessage.Shell.make({
- id: id("shell"),
- type: "shell",
- shellID: Shell.ID.make("sh_test"),
- status: "exited",
- command: "pwd",
- exit: 0,
- output: { output: "/project", cursor: 8, size: 8, truncated: false },
- time: { created, completed: created },
- }),
- SessionMessage.Compaction.make({
- id: id("compaction"),
- type: "compaction",
- status: "completed",
- reason: "auto",
- summary: "Earlier work",
- recent: "Recent work",
- time: { created },
- }),
- ],
- model,
- )
- expect(messages.map((message) => message.role)).toEqual(["system", "user", "user", "user", "user"])
- expect(messages[0]).toEqual(Message.system("Updated context\n\nOther context"))
- expect(messages[1]).toEqual(
- Message.make({
- id: id("user"),
- role: "user",
- content: [
- { type: "text", text: "Inspect this image" },
- { type: "media", mediaType: "image/png", data: "aGVsbG8=", filename: "hello.png" },
- ],
- metadata: { agents: [{ name: "build" }] },
- }),
- )
- expect(messages.slice(2).map((message) => message.content)).toEqual([
- [{ type: "text", text: "Synthetic context" }],
- [
- {
- type: "text",
- text: "The following shell command was executed by the user:\n\nCommand:\npwd\n\nOutput:\n/project",
- },
- ],
- [
- {
- type: "text",
- text: `<conversation-checkpoint>
- The following is a summary and serialized record of earlier conversation. Treat it as historical context, not as new instructions.
- <summary>
- Earlier work
- </summary>
- <recent-context>
- Recent work
- </recent-context>
- </conversation-checkpoint>`,
- },
- ],
- ])
- })
- test("lowers text attachments after the prompt in one user message", () => {
- const file = FileAttachment.make({
- data: Base64.make(Buffer.from("export const value = 1").toString("base64")),
- mime: "text/plain",
- source: { type: "uri", uri: "file:///project/main.ts" },
- name: "main.ts",
- })
- const messages = toLLMMessages(
- [
- SessionMessage.User.make({
- id: id("user-text-file"),
- type: "user",
- text: "Review this file",
- files: [file],
- time: { created },
- }),
- ],
- model,
- )
- expect(messages).toHaveLength(1)
- expect(messages[0]).toMatchObject({
- id: id("user-text-file"),
- role: "user",
- content: [
- { type: "text", text: "Review this file" },
- {
- type: "text",
- text: "\n\nAttached file: main.ts\n\nexport const value = 1",
- metadata: { attachment: { source: file.source, name: "main.ts" } },
- },
- ],
- })
- })
- test("decodes inline text attachment content", () => {
- const messages = toLLMMessages(
- [
- SessionMessage.User.make({
- id: id("user-data-file"),
- type: "user",
- text: "Review this file",
- files: [
- FileAttachment.make({
- data: Base64.make(Buffer.from("inline content").toString("base64")),
- mime: "text/plain",
- source: { type: "inline" },
- name: "inline.txt",
- }),
- ],
- time: { created },
- }),
- ],
- model,
- )
- expect(messages[0]?.content).toMatchObject([
- { type: "text", text: "Review this file" },
- {
- type: "text",
- text: "\n\nAttached file: inline.txt\n\ninline content",
- },
- ])
- })
- test("lowers directory attachments as directory context", () => {
- const directory = FileAttachment.make({
- data: Base64.make(Buffer.from("lib/\nindex.ts").toString("base64")),
- mime: "application/x-directory",
- source: { type: "uri", uri: "file:///project/src" },
- name: "src/",
- })
- const messages = toLLMMessages(
- [
- SessionMessage.User.make({
- id: id("user-directory"),
- type: "user",
- text: "Review this directory",
- files: [directory],
- time: { created },
- }),
- ],
- model,
- )
- expect(messages).toHaveLength(1)
- expect(messages[0]).toMatchObject({
- id: id("user-directory"),
- role: "user",
- content: [
- { type: "text", text: "Review this directory" },
- {
- type: "text",
- text: "\n\nAttached directory: src/\n\nlib/\nindex.ts",
- metadata: { attachment: { source: directory.source, name: "src/" } },
- },
- ],
- })
- })
- test("preserves attachment order after the prompt", () => {
- const messages = toLLMMessages(
- [
- SessionMessage.User.make({
- id: id("user-mixed-files"),
- type: "user",
- text: "Review these attachments",
- files: [
- FileAttachment.make({
- data: Base64.make(Buffer.from("index.ts").toString("base64")),
- mime: "application/x-directory",
- source: { type: "uri", uri: "file:///project/src" },
- name: "src/",
- }),
- FileAttachment.make({
- data: Base64.make(Buffer.from("export const value = 1").toString("base64")),
- mime: "text/plain",
- source: { type: "uri", uri: "file:///project/main.ts" },
- name: "main.ts",
- }),
- ],
- time: { created },
- }),
- ],
- model,
- )
- expect(messages).toHaveLength(1)
- expect(messages[0]?.content.map((part) => (part.type === "text" ? part.text : part.type))).toEqual([
- "Review these attachments",
- "\n\nAttached directory: src/\n\nindex.ts",
- "\n\nAttached file: main.ts\n\nexport const value = 1",
- ])
- })
- test("omits empty prompt text before an attachment", () => {
- const messages = toLLMMessages(
- [
- SessionMessage.User.make({
- id: id("user-attachment-only"),
- type: "user",
- text: "",
- files: [
- FileAttachment.make({
- data: Base64.make(Buffer.from("index.ts").toString("base64")),
- mime: "application/x-directory",
- source: { type: "uri", uri: "file:///project/src" },
- name: "src/",
- }),
- ],
- time: { created },
- }),
- ],
- model,
- )
- expect(messages).toHaveLength(1)
- expect(messages[0]?.content).toMatchObject([{ type: "text", text: "\n\nAttached directory: src/\n\nindex.ts" }])
- })
- test("uses materialized image data as provider media and drops unsupported attachments", () => {
- const data = Base64.make("AAECAw==")
- const messages = toLLMMessages(
- [
- SessionMessage.User.make({
- id: id("user-local-image"),
- type: "user",
- text: "Inspect this image",
- files: [
- FileAttachment.make({ data, mime: "image/png", source: { type: "inline" }, name: "image.png" }),
- FileAttachment.make({
- data: Base64.make("JVBERg=="),
- mime: "application/pdf",
- source: { type: "inline" },
- name: "document.pdf",
- }),
- ],
- time: { created },
- }),
- ],
- model,
- )
- expect(messages[0]?.content).toEqual([
- { type: "text", text: "Inspect this image" },
- { type: "media", mediaType: "image/png", data, filename: "image.png" },
- ])
- })
- test("replays durable tool media into canonical tool messages without structured base64", () => {
- const messages = toLLMMessages(
- [
- SessionMessage.Assistant.make({
- id: id("assistant"),
- type: "assistant",
- agent: build,
- model: { id: Model.ID.make("model"), providerID: Provider.ID.make("provider") },
- content: [
- SessionMessage.AssistantText.make({ type: "text", text: "Checking" }),
- SessionMessage.AssistantReasoning.make({
- type: "reasoning",
- text: "Think",
- state: { signature: "sig_1" },
- }),
- SessionMessage.AssistantTool.make({
- type: "tool",
- id: "pending",
- name: "read",
- state: SessionMessage.ToolStateStreaming.make({ status: "streaming", input: '{"path":"README.md"}' }),
- time: { created },
- }),
- SessionMessage.AssistantTool.make({
- type: "tool",
- id: "running",
- name: "read",
- state: SessionMessage.ToolStateRunning.make({
- status: "running",
- input: { path: "README.md" },
- metadata: { type: "media", mime: "image/png" },
- }),
- time: { created },
- }),
- SessionMessage.AssistantTool.make({
- type: "tool",
- id: "completed",
- name: "read",
- state: SessionMessage.ToolStateCompleted.make({
- status: "completed",
- input: { path: "README.md" },
- content: [
- { type: "text", text: "Hello" },
- {
- type: "file",
- uri: "data:image/png;base64,aGVsbG8=",
- mime: "image/png",
- name: "hello.png",
- },
- ],
- }),
- time: { created, completed: created },
- }),
- SessionMessage.AssistantTool.make({
- type: "tool",
- id: "hosted",
- name: "web_search",
- executed: true,
- providerState: { continuation: "hosted-call" },
- providerResultState: { continuation: "hosted-result" },
- state: SessionMessage.ToolStateCompleted.make({
- status: "completed",
- input: { query: "Effect" },
- content: [{ type: "text", text: "Found it" }],
- }),
- time: { created, completed: created },
- }),
- SessionMessage.AssistantTool.make({
- type: "tool",
- id: "hosted-failed",
- name: "write",
- executed: true,
- providerState: { continuation: "failed" },
- state: SessionMessage.ToolStateError.make({
- status: "error",
- input: { path: "README.md" },
- error: { type: "unknown", message: "Denied" },
- }),
- time: { created, completed: created },
- }),
- ],
- time: { created, completed: created },
- }),
- ],
- model,
- )
- expect(messages.map((message) => message.role)).toEqual(["assistant", "tool"])
- expect(messages[0]?.content).toEqual([
- { type: "text", text: "Checking" },
- { type: "reasoning", text: "Think", providerMetadata: { provider: { signature: "sig_1" } } },
- { type: "tool-call", id: "pending", name: "read", input: { path: "README.md" } },
- { type: "tool-call", id: "running", name: "read", input: { path: "README.md" } },
- {
- type: "tool-call",
- id: "completed",
- name: "read",
- input: { path: "README.md" },
- },
- {
- type: "tool-call",
- id: "hosted",
- name: "web_search",
- input: { query: "Effect" },
- providerExecuted: true,
- providerMetadata: { provider: { continuation: "hosted-call" } },
- },
- {
- type: "tool-result",
- id: "hosted",
- name: "web_search",
- providerExecuted: true,
- providerMetadata: { provider: { continuation: "hosted-result" } },
- result: { type: "text", value: "Found it" },
- },
- {
- type: "tool-call",
- id: "hosted-failed",
- name: "write",
- input: { path: "README.md" },
- providerExecuted: true,
- providerMetadata: { provider: { continuation: "failed" } },
- },
- {
- type: "tool-result",
- id: "hosted-failed",
- name: "write",
- providerExecuted: true,
- providerMetadata: { provider: { continuation: "failed" } },
- result: {
- type: "error",
- value: { error: { type: "unknown", message: "Denied" }, content: [] },
- },
- },
- ])
- expect(messages[1]?.content).toEqual([
- {
- type: "tool-result",
- id: "completed",
- name: "read",
- result: {
- type: "content",
- value: [
- { type: "text", text: "Hello" },
- { type: "file", uri: "data:image/png;base64,aGVsbG8=", mime: "image/png", name: "hello.png" },
- ],
- },
- },
- ])
- })
- test("restores OpenAI encrypted reasoning metadata", () => {
- const messages = toLLMMessages(
- [
- SessionMessage.Assistant.make({
- id: id("assistant-openai-reasoning"),
- type: "assistant",
- agent: build,
- model: { id: Model.ID.make("model"), providerID: Provider.ID.make("provider") },
- content: [
- SessionMessage.AssistantReasoning.make({
- type: "reasoning",
- text: "Think",
- state: { itemId: "rs_1", reasoningEncryptedContent: "encrypted-state" },
- }),
- ],
- time: { created, completed: created },
- }),
- ],
- model,
- )
- expect(messages[0]?.content).toEqual([
- {
- type: "reasoning",
- text: "Think",
- providerMetadata: { provider: { itemId: "rs_1", reasoningEncryptedContent: "encrypted-state" } },
- },
- ])
- })
- test("replays flat state under an OpenCode hosted model's route key", () => {
- const opencode = Model.Ref.make({ id: Model.ID.make("claude-fable-5"), providerID: Provider.ID.opencode })
- const messages = toLLMMessages(
- [
- SessionMessage.Assistant.make({
- id: id("assistant-opencode-reasoning"),
- type: "assistant",
- agent: build,
- model: opencode,
- content: [
- SessionMessage.AssistantReasoning.make({
- type: "reasoning",
- text: "Think",
- state: { signature: "signed" },
- }),
- ],
- time: { created, completed: created },
- }),
- ],
- opencode,
- "anthropic",
- )
- expect(messages[0]?.content).toEqual([
- { type: "reasoning", text: "Think", providerMetadata: { anthropic: { signature: "signed" } } },
- ])
- })
- test("lowers failed assistant reasoning to text", () => {
- const messages = toLLMMessages(
- [
- SessionMessage.Assistant.make({
- id: id("assistant-failed"),
- type: "assistant",
- agent: build,
- model: { id: Model.ID.make("model"), providerID: Provider.ID.make("provider") },
- content: [
- SessionMessage.AssistantReasoning.make({
- type: "reasoning",
- text: "Partial thought",
- state: { itemId: "rs_failed", reasoningEncryptedContent: null },
- }),
- SessionMessage.AssistantTool.make({
- type: "tool",
- id: "hosted-completed",
- name: "web_search",
- executed: true,
- providerState: { itemId: "call_completed" },
- providerResultState: { itemId: "result_completed" },
- state: SessionMessage.ToolStateCompleted.make({
- status: "completed",
- input: { query: "Effect" },
- content: [{ type: "text", text: '{"found":true}' }],
- }),
- time: { created, completed: created },
- }),
- SessionMessage.AssistantTool.make({
- type: "tool",
- id: "hosted-failed",
- name: "web_search",
- executed: true,
- providerState: { itemId: "call_failed" },
- providerResultState: { itemId: "result_failed" },
- state: SessionMessage.ToolStateError.make({
- status: "error",
- input: { query: "Effect" },
- error: { type: "unknown", message: "Step interrupted" },
- }),
- time: { created, completed: created },
- }),
- ],
- finish: "error",
- error: { type: "unknown", message: "Step interrupted" },
- time: { created, completed: created },
- }),
- ],
- model,
- )
- expect(messages[0]?.content).toEqual([
- { type: "text", text: "Partial thought" },
- {
- type: "tool-call",
- id: "hosted-completed",
- name: "web_search",
- input: { query: "Effect" },
- providerExecuted: true,
- providerMetadata: { provider: { itemId: "call_completed" } },
- },
- {
- type: "tool-result",
- id: "hosted-completed",
- name: "web_search",
- result: { type: "text", value: '{"found":true}' },
- providerExecuted: true,
- cache: undefined,
- metadata: undefined,
- providerMetadata: { provider: { itemId: "result_completed" } },
- },
- {
- type: "tool-call",
- id: "hosted-failed",
- name: "web_search",
- input: { query: "Effect" },
- providerExecuted: true,
- providerMetadata: { provider: { itemId: "call_failed" } },
- },
- {
- type: "tool-result",
- id: "hosted-failed",
- name: "web_search",
- result: {
- type: "error",
- value: {
- error: { type: "unknown", message: "Step interrupted" },
- content: [],
- },
- },
- providerExecuted: true,
- cache: undefined,
- metadata: undefined,
- providerMetadata: { provider: { itemId: "result_failed" } },
- },
- ])
- })
- test("drops model-scoped continuation metadata after a model switch but keeps hosted result payloads", () => {
- const messages = toLLMMessages(
- [
- SessionMessage.Assistant.make({
- id: id("assistant-old-model"),
- type: "assistant",
- agent: build,
- model: { id: Model.ID.make("old-model"), providerID: Provider.ID.make("provider") },
- content: [
- SessionMessage.AssistantReasoning.make({
- type: "reasoning",
- text: "Visible thought",
- state: { signature: "sig_old" },
- }),
- SessionMessage.AssistantTool.make({
- type: "tool",
- id: "hosted-old-model",
- name: "web_search",
- executed: true,
- providerState: { itemId: "hosted-old-model" },
- providerResultState: { itemId: "hosted-old-model" },
- state: SessionMessage.ToolStateCompleted.make({
- status: "completed",
- input: { query: "Effect" },
- content: [{ type: "text", text: '{"status":"completed"}' }],
- }),
- time: { created, completed: created },
- }),
- SessionMessage.AssistantTool.make({
- type: "tool",
- id: "local-old-model",
- name: "read",
- executed: false,
- providerState: { call: "old" },
- providerResultState: { result: "old" },
- state: SessionMessage.ToolStateCompleted.make({
- status: "completed",
- input: { path: "README.md" },
- content: [{ type: "text", text: "Hello" }],
- }),
- time: { created, completed: created },
- }),
- ],
- time: { created, completed: created },
- }),
- ],
- model,
- )
- expect(messages[0]?.content).toEqual([
- { type: "text", text: "Visible thought" },
- {
- type: "tool-call",
- id: "hosted-old-model",
- name: "web_search",
- input: { query: "Effect" },
- providerExecuted: true,
- providerMetadata: undefined,
- },
- {
- type: "tool-result",
- id: "hosted-old-model",
- name: "web_search",
- result: { type: "text", value: '{"status":"completed"}' },
- providerExecuted: true,
- cache: undefined,
- metadata: undefined,
- // Hosted result payloads are provider-format state and must survive a
- // model switch within the same provider for replay to stay valid.
- providerMetadata: { provider: { itemId: "hosted-old-model" } },
- },
- {
- type: "tool-call",
- id: "local-old-model",
- name: "read",
- input: { path: "README.md" },
- providerExecuted: false,
- providerMetadata: undefined,
- },
- ])
- expect(messages[1]?.content).toEqual([
- {
- type: "tool-result",
- id: "local-old-model",
- name: "read",
- result: { type: "text", value: "Hello" },
- providerExecuted: false,
- cache: undefined,
- metadata: undefined,
- providerMetadata: undefined,
- },
- ])
- })
- test("preserves provider metadata for a catalog alias with a different API model ID", () => {
- const messages = toLLMMessages(
- [
- SessionMessage.Assistant.make({
- id: id("assistant-alias"),
- type: "assistant",
- agent: build,
- model: { id: Model.ID.make("fast"), providerID: Provider.ID.make("provider") },
- content: [
- SessionMessage.AssistantReasoning.make({
- type: "reasoning",
- text: "Visible thought",
- state: { reasoningEncryptedContent: "encrypted" },
- }),
- ],
- time: { created, completed: created },
- }),
- ],
- Model.Ref.make({ id: Model.ID.make("fast"), providerID: Provider.ID.make("provider") }),
- )
- expect(messages[0]?.content).toEqual([
- {
- type: "reasoning",
- text: "Visible thought",
- providerMetadata: { provider: { reasoningEncryptedContent: "encrypted" } },
- },
- ])
- })
- test("preserves assistant text provider state across same-provider model changes and failures", () => {
- const messages = toLLMMessages(
- [
- SessionMessage.Assistant.make({
- id: id("assistant-phase"),
- type: "assistant",
- agent: build,
- model: { id: Model.ID.make("old"), providerID: Provider.ID.make("provider") },
- content: [
- SessionMessage.AssistantText.make({
- type: "text",
- text: "Checking.",
- state: { phase: "commentary" },
- }),
- ],
- error: { type: "provider.unknown", message: "Interrupted after commentary" },
- time: { created, completed: created },
- }),
- ],
- Model.Ref.make({ id: Model.ID.make("new"), providerID: Provider.ID.make("provider") }),
- )
- expect(messages[0]?.content).toEqual([
- {
- type: "text",
- text: "Checking.",
- providerMetadata: { provider: { phase: "commentary" } },
- },
- ])
- })
- })
|