| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801 |
- import { BusEvent } from "@/bus/bus-event"
- import z from "zod"
- import { NamedError } from "@opencode-ai/util/error"
- import { APICallError, convertToModelMessages, LoadAPIKeyError, type ModelMessage, type UIMessage } from "ai"
- import { Identifier } from "../id/id"
- import { LSP } from "../lsp"
- import { Snapshot } from "@/snapshot"
- import { fn } from "@/util/fn"
- import { Storage } from "@/storage/storage"
- import { ProviderTransform } from "@/provider/transform"
- import { STATUS_CODES } from "http"
- import { iife } from "@/util/iife"
- import { type SystemError } from "bun"
- import type { Provider } from "@/provider/provider"
- export namespace MessageV2 {
- export const OutputLengthError = NamedError.create("MessageOutputLengthError", z.object({}))
- export const AbortedError = NamedError.create("MessageAbortedError", z.object({ message: z.string() }))
- export const AuthError = NamedError.create(
- "ProviderAuthError",
- z.object({
- providerID: z.string(),
- message: z.string(),
- }),
- )
- export const APIError = NamedError.create(
- "APIError",
- z.object({
- message: z.string(),
- statusCode: z.number().optional(),
- isRetryable: z.boolean(),
- responseHeaders: z.record(z.string(), z.string()).optional(),
- responseBody: z.string().optional(),
- metadata: z.record(z.string(), z.string()).optional(),
- }),
- )
- export type APIError = z.infer<typeof APIError.Schema>
- const PartBase = z.object({
- id: z.string(),
- sessionID: z.string(),
- messageID: z.string(),
- })
- export const SnapshotPart = PartBase.extend({
- type: z.literal("snapshot"),
- snapshot: z.string(),
- }).meta({
- ref: "SnapshotPart",
- })
- export type SnapshotPart = z.infer<typeof SnapshotPart>
- export const PatchPart = PartBase.extend({
- type: z.literal("patch"),
- hash: z.string(),
- files: z.string().array(),
- }).meta({
- ref: "PatchPart",
- })
- export type PatchPart = z.infer<typeof PatchPart>
- export const TextPart = PartBase.extend({
- type: z.literal("text"),
- text: z.string(),
- synthetic: z.boolean().optional(),
- ignored: z.boolean().optional(),
- time: z
- .object({
- start: z.number(),
- end: z.number().optional(),
- })
- .optional(),
- metadata: z.record(z.string(), z.any()).optional(),
- }).meta({
- ref: "TextPart",
- })
- export type TextPart = z.infer<typeof TextPart>
- export const ReasoningPart = PartBase.extend({
- type: z.literal("reasoning"),
- text: z.string(),
- metadata: z.record(z.string(), z.any()).optional(),
- time: z.object({
- start: z.number(),
- end: z.number().optional(),
- }),
- }).meta({
- ref: "ReasoningPart",
- })
- export type ReasoningPart = z.infer<typeof ReasoningPart>
- const FilePartSourceBase = z.object({
- text: z
- .object({
- value: z.string(),
- start: z.number().int(),
- end: z.number().int(),
- })
- .meta({
- ref: "FilePartSourceText",
- }),
- })
- export const FileSource = FilePartSourceBase.extend({
- type: z.literal("file"),
- path: z.string(),
- }).meta({
- ref: "FileSource",
- })
- export const SymbolSource = FilePartSourceBase.extend({
- type: z.literal("symbol"),
- path: z.string(),
- range: LSP.Range,
- name: z.string(),
- kind: z.number().int(),
- }).meta({
- ref: "SymbolSource",
- })
- export const ResourceSource = FilePartSourceBase.extend({
- type: z.literal("resource"),
- clientName: z.string(),
- uri: z.string(),
- }).meta({
- ref: "ResourceSource",
- })
- export const FilePartSource = z.discriminatedUnion("type", [FileSource, SymbolSource, ResourceSource]).meta({
- ref: "FilePartSource",
- })
- export const FilePart = PartBase.extend({
- type: z.literal("file"),
- mime: z.string(),
- filename: z.string().optional(),
- url: z.string(),
- source: FilePartSource.optional(),
- }).meta({
- ref: "FilePart",
- })
- export type FilePart = z.infer<typeof FilePart>
- export const AgentPart = PartBase.extend({
- type: z.literal("agent"),
- name: z.string(),
- source: z
- .object({
- value: z.string(),
- start: z.number().int(),
- end: z.number().int(),
- })
- .optional(),
- }).meta({
- ref: "AgentPart",
- })
- export type AgentPart = z.infer<typeof AgentPart>
- export const CompactionPart = PartBase.extend({
- type: z.literal("compaction"),
- auto: z.boolean(),
- }).meta({
- ref: "CompactionPart",
- })
- export type CompactionPart = z.infer<typeof CompactionPart>
- export const SubtaskPart = PartBase.extend({
- type: z.literal("subtask"),
- prompt: z.string(),
- description: z.string(),
- agent: z.string(),
- model: z
- .object({
- providerID: z.string(),
- modelID: z.string(),
- })
- .optional(),
- command: z.string().optional(),
- }).meta({
- ref: "SubtaskPart",
- })
- export type SubtaskPart = z.infer<typeof SubtaskPart>
- export const RetryPart = PartBase.extend({
- type: z.literal("retry"),
- attempt: z.number(),
- error: APIError.Schema,
- time: z.object({
- created: z.number(),
- }),
- }).meta({
- ref: "RetryPart",
- })
- export type RetryPart = z.infer<typeof RetryPart>
- export const StepStartPart = PartBase.extend({
- type: z.literal("step-start"),
- snapshot: z.string().optional(),
- }).meta({
- ref: "StepStartPart",
- })
- export type StepStartPart = z.infer<typeof StepStartPart>
- export const StepFinishPart = PartBase.extend({
- type: z.literal("step-finish"),
- reason: z.string(),
- snapshot: z.string().optional(),
- cost: z.number(),
- tokens: z.object({
- input: z.number(),
- output: z.number(),
- reasoning: z.number(),
- cache: z.object({
- read: z.number(),
- write: z.number(),
- }),
- }),
- }).meta({
- ref: "StepFinishPart",
- })
- export type StepFinishPart = z.infer<typeof StepFinishPart>
- export const ToolStatePending = z
- .object({
- status: z.literal("pending"),
- input: z.record(z.string(), z.any()),
- raw: z.string(),
- })
- .meta({
- ref: "ToolStatePending",
- })
- export type ToolStatePending = z.infer<typeof ToolStatePending>
- export const ToolStateRunning = z
- .object({
- status: z.literal("running"),
- input: z.record(z.string(), z.any()),
- title: z.string().optional(),
- metadata: z.record(z.string(), z.any()).optional(),
- time: z.object({
- start: z.number(),
- }),
- })
- .meta({
- ref: "ToolStateRunning",
- })
- export type ToolStateRunning = z.infer<typeof ToolStateRunning>
- export const ToolStateCompleted = z
- .object({
- status: z.literal("completed"),
- input: z.record(z.string(), z.any()),
- output: z.string(),
- title: z.string(),
- metadata: z.record(z.string(), z.any()),
- time: z.object({
- start: z.number(),
- end: z.number(),
- compacted: z.number().optional(),
- }),
- attachments: FilePart.array().optional(),
- })
- .meta({
- ref: "ToolStateCompleted",
- })
- export type ToolStateCompleted = z.infer<typeof ToolStateCompleted>
- export const ToolStateError = z
- .object({
- status: z.literal("error"),
- input: z.record(z.string(), z.any()),
- error: z.string(),
- metadata: z.record(z.string(), z.any()).optional(),
- time: z.object({
- start: z.number(),
- end: z.number(),
- }),
- })
- .meta({
- ref: "ToolStateError",
- })
- export type ToolStateError = z.infer<typeof ToolStateError>
- export const ToolState = z
- .discriminatedUnion("status", [ToolStatePending, ToolStateRunning, ToolStateCompleted, ToolStateError])
- .meta({
- ref: "ToolState",
- })
- export const ToolPart = PartBase.extend({
- type: z.literal("tool"),
- callID: z.string(),
- tool: z.string(),
- state: ToolState,
- metadata: z.record(z.string(), z.any()).optional(),
- }).meta({
- ref: "ToolPart",
- })
- export type ToolPart = z.infer<typeof ToolPart>
- const Base = z.object({
- id: z.string(),
- sessionID: z.string(),
- })
- export const User = Base.extend({
- role: z.literal("user"),
- time: z.object({
- created: z.number(),
- }),
- summary: z
- .object({
- title: z.string().optional(),
- body: z.string().optional(),
- diffs: Snapshot.FileDiff.array(),
- })
- .optional(),
- agent: z.string(),
- model: z.object({
- providerID: z.string(),
- modelID: z.string(),
- }),
- system: z.string().optional(),
- tools: z.record(z.string(), z.boolean()).optional(),
- variant: z.string().optional(),
- }).meta({
- ref: "UserMessage",
- })
- export type User = z.infer<typeof User>
- export const Part = z
- .discriminatedUnion("type", [
- TextPart,
- SubtaskPart,
- ReasoningPart,
- FilePart,
- ToolPart,
- StepStartPart,
- StepFinishPart,
- SnapshotPart,
- PatchPart,
- AgentPart,
- RetryPart,
- CompactionPart,
- ])
- .meta({
- ref: "Part",
- })
- export type Part = z.infer<typeof Part>
- export const Assistant = Base.extend({
- role: z.literal("assistant"),
- time: z.object({
- created: z.number(),
- completed: z.number().optional(),
- }),
- error: z
- .discriminatedUnion("name", [
- AuthError.Schema,
- NamedError.Unknown.Schema,
- OutputLengthError.Schema,
- AbortedError.Schema,
- APIError.Schema,
- ])
- .optional(),
- parentID: z.string(),
- modelID: z.string(),
- providerID: z.string(),
- /**
- * @deprecated
- */
- mode: z.string(),
- agent: z.string(),
- path: z.object({
- cwd: z.string(),
- root: z.string(),
- }),
- summary: z.boolean().optional(),
- cost: z.number(),
- tokens: z.object({
- input: z.number(),
- output: z.number(),
- reasoning: z.number(),
- cache: z.object({
- read: z.number(),
- write: z.number(),
- }),
- }),
- finish: z.string().optional(),
- }).meta({
- ref: "AssistantMessage",
- })
- export type Assistant = z.infer<typeof Assistant>
- export const Info = z.discriminatedUnion("role", [User, Assistant]).meta({
- ref: "Message",
- })
- export type Info = z.infer<typeof Info>
- export const Event = {
- Updated: BusEvent.define(
- "message.updated",
- z.object({
- info: Info,
- }),
- ),
- Removed: BusEvent.define(
- "message.removed",
- z.object({
- sessionID: z.string(),
- messageID: z.string(),
- }),
- ),
- PartUpdated: BusEvent.define(
- "message.part.updated",
- z.object({
- part: Part,
- delta: z.string().optional(),
- }),
- ),
- PartRemoved: BusEvent.define(
- "message.part.removed",
- z.object({
- sessionID: z.string(),
- messageID: z.string(),
- partID: z.string(),
- }),
- ),
- }
- export const WithParts = z.object({
- info: Info,
- parts: z.array(Part),
- })
- export type WithParts = z.infer<typeof WithParts>
- export function toModelMessages(input: WithParts[], model: Provider.Model): ModelMessage[] {
- const result: UIMessage[] = []
- const toolNames = new Set<string>()
- // Track media from tool results that need to be injected as user messages
- // for providers that don't support media in tool results.
- //
- // OpenAI-compatible APIs only support string content in tool results, so we need
- // to extract media and inject as user messages. Other SDKs (anthropic, google,
- // bedrock) handle type: "content" with media parts natively.
- //
- // Only apply this workaround if the model actually supports image input -
- // otherwise there's no point extracting images.
- const supportsMediaInToolResults = (() => {
- if (model.api.npm === "@ai-sdk/anthropic") return true
- if (model.api.npm === "@ai-sdk/openai") return true
- if (model.api.npm === "@ai-sdk/amazon-bedrock") return true
- if (model.api.npm === "@ai-sdk/google-vertex/anthropic") return true
- if (model.api.npm === "@ai-sdk/google") {
- const id = model.api.id.toLowerCase()
- return id.includes("gemini-3") && !id.includes("gemini-2")
- }
- return false
- })()
- const toModelOutput = (output: unknown) => {
- if (typeof output === "string") {
- return { type: "text", value: output }
- }
- if (typeof output === "object") {
- const outputObject = output as {
- text: string
- attachments?: Array<{ mime: string; url: string }>
- }
- const attachments = (outputObject.attachments ?? []).filter((attachment) => {
- return attachment.url.startsWith("data:") && attachment.url.includes(",")
- })
- return {
- type: "content",
- value: [
- { type: "text", text: outputObject.text },
- ...attachments.map((attachment) => ({
- type: "media",
- mediaType: attachment.mime,
- data: iife(() => {
- const commaIndex = attachment.url.indexOf(",")
- return commaIndex === -1 ? attachment.url : attachment.url.slice(commaIndex + 1)
- }),
- })),
- ],
- }
- }
- return { type: "json", value: output as never }
- }
- for (const msg of input) {
- if (msg.parts.length === 0) continue
- if (msg.info.role === "user") {
- const userMessage: UIMessage = {
- id: msg.info.id,
- role: "user",
- parts: [],
- }
- result.push(userMessage)
- for (const part of msg.parts) {
- if (part.type === "text" && !part.ignored)
- userMessage.parts.push({
- type: "text",
- text: part.text,
- })
- // text/plain and directory files are converted into text parts, ignore them
- if (part.type === "file" && part.mime !== "text/plain" && part.mime !== "application/x-directory")
- userMessage.parts.push({
- type: "file",
- url: part.url,
- mediaType: part.mime,
- filename: part.filename,
- })
- if (part.type === "compaction") {
- userMessage.parts.push({
- type: "text",
- text: "What did we do so far?",
- })
- }
- if (part.type === "subtask") {
- userMessage.parts.push({
- type: "text",
- text: "The following tool was executed by the user",
- })
- }
- }
- }
- if (msg.info.role === "assistant") {
- const differentModel = `${model.providerID}/${model.id}` !== `${msg.info.providerID}/${msg.info.modelID}`
- const media: Array<{ mime: string; url: string }> = []
- if (
- msg.info.error &&
- !(
- MessageV2.AbortedError.isInstance(msg.info.error) &&
- msg.parts.some((part) => part.type !== "step-start" && part.type !== "reasoning")
- )
- ) {
- continue
- }
- const assistantMessage: UIMessage = {
- id: msg.info.id,
- role: "assistant",
- parts: [],
- }
- for (const part of msg.parts) {
- if (part.type === "text")
- assistantMessage.parts.push({
- type: "text",
- text: part.text,
- ...(differentModel ? {} : { providerMetadata: part.metadata }),
- })
- if (part.type === "step-start")
- assistantMessage.parts.push({
- type: "step-start",
- })
- if (part.type === "tool") {
- toolNames.add(part.tool)
- if (part.state.status === "completed") {
- const outputText = part.state.time.compacted ? "[Old tool result content cleared]" : part.state.output
- const attachments = part.state.time.compacted ? [] : (part.state.attachments ?? [])
- // For providers that don't support media in tool results, extract media files
- // (images, PDFs) to be sent as a separate user message
- const isMediaAttachment = (a: { mime: string }) =>
- a.mime.startsWith("image/") || a.mime === "application/pdf"
- const mediaAttachments = attachments.filter(isMediaAttachment)
- const nonMediaAttachments = attachments.filter((a) => !isMediaAttachment(a))
- if (!supportsMediaInToolResults && mediaAttachments.length > 0) {
- media.push(...mediaAttachments)
- }
- const finalAttachments = supportsMediaInToolResults ? attachments : nonMediaAttachments
- const output =
- finalAttachments.length > 0
- ? {
- text: outputText,
- attachments: finalAttachments,
- }
- : outputText
- assistantMessage.parts.push({
- type: ("tool-" + part.tool) as `tool-${string}`,
- state: "output-available",
- toolCallId: part.callID,
- input: part.state.input,
- output,
- ...(differentModel ? {} : { callProviderMetadata: part.metadata }),
- })
- }
- if (part.state.status === "error")
- assistantMessage.parts.push({
- type: ("tool-" + part.tool) as `tool-${string}`,
- state: "output-error",
- toolCallId: part.callID,
- input: part.state.input,
- errorText: part.state.error,
- ...(differentModel ? {} : { callProviderMetadata: part.metadata }),
- })
- // Handle pending/running tool calls to prevent dangling tool_use blocks
- // Anthropic/Claude APIs require every tool_use to have a corresponding tool_result
- if (part.state.status === "pending" || part.state.status === "running")
- assistantMessage.parts.push({
- type: ("tool-" + part.tool) as `tool-${string}`,
- state: "output-error",
- toolCallId: part.callID,
- input: part.state.input,
- errorText: "[Tool execution was interrupted]",
- ...(differentModel ? {} : { callProviderMetadata: part.metadata }),
- })
- }
- if (part.type === "reasoning") {
- assistantMessage.parts.push({
- type: "reasoning",
- text: part.text,
- ...(differentModel ? {} : { providerMetadata: part.metadata }),
- })
- }
- }
- if (assistantMessage.parts.length > 0) {
- result.push(assistantMessage)
- // Inject pending media as a user message for providers that don't support
- // media (images, PDFs) in tool results
- if (media.length > 0) {
- result.push({
- id: Identifier.ascending("message"),
- role: "user",
- parts: [
- {
- type: "text" as const,
- text: "Attached image(s) from tool result:",
- },
- ...media.map((attachment) => ({
- type: "file" as const,
- url: attachment.url,
- mediaType: attachment.mime,
- })),
- ],
- })
- }
- }
- }
- }
- const tools = Object.fromEntries(Array.from(toolNames).map((toolName) => [toolName, { toModelOutput }]))
- return convertToModelMessages(
- result.filter((msg) => msg.parts.some((part) => part.type !== "step-start")),
- {
- //@ts-expect-error (convertToModelMessages expects a ToolSet but only actually needs tools[name]?.toModelOutput)
- tools,
- },
- )
- }
- export const stream = fn(Identifier.schema("session"), async function* (sessionID) {
- const list = await Array.fromAsync(await Storage.list(["message", sessionID]))
- for (let i = list.length - 1; i >= 0; i--) {
- yield await get({
- sessionID,
- messageID: list[i][2],
- })
- }
- })
- export const parts = fn(Identifier.schema("message"), async (messageID) => {
- const result = [] as MessageV2.Part[]
- for (const item of await Storage.list(["part", messageID])) {
- const read = await Storage.read<MessageV2.Part>(item)
- result.push(read)
- }
- result.sort((a, b) => (a.id > b.id ? 1 : -1))
- return result
- })
- export const get = fn(
- z.object({
- sessionID: Identifier.schema("session"),
- messageID: Identifier.schema("message"),
- }),
- async (input): Promise<WithParts> => {
- return {
- info: await Storage.read<MessageV2.Info>(["message", input.sessionID, input.messageID]),
- parts: await parts(input.messageID),
- }
- },
- )
- export async function filterCompacted(stream: AsyncIterable<MessageV2.WithParts>) {
- const result = [] as MessageV2.WithParts[]
- const completed = new Set<string>()
- for await (const msg of stream) {
- result.push(msg)
- if (
- msg.info.role === "user" &&
- completed.has(msg.info.id) &&
- msg.parts.some((part) => part.type === "compaction")
- )
- break
- if (msg.info.role === "assistant" && msg.info.summary && msg.info.finish) completed.add(msg.info.parentID)
- }
- result.reverse()
- return result
- }
- const isOpenAiErrorRetryable = (e: APICallError) => {
- const status = e.statusCode
- if (!status) return e.isRetryable
- // openai sometimes returns 404 for models that are actually available
- return status === 404 || e.isRetryable
- }
- export function fromError(e: unknown, ctx: { providerID: string }) {
- switch (true) {
- case e instanceof DOMException && e.name === "AbortError":
- return new MessageV2.AbortedError(
- { message: e.message },
- {
- cause: e,
- },
- ).toObject()
- case MessageV2.OutputLengthError.isInstance(e):
- return e
- case LoadAPIKeyError.isInstance(e):
- return new MessageV2.AuthError(
- {
- providerID: ctx.providerID,
- message: e.message,
- },
- { cause: e },
- ).toObject()
- case (e as SystemError)?.code === "ECONNRESET":
- return new MessageV2.APIError(
- {
- message: "Connection reset by server",
- isRetryable: true,
- metadata: {
- code: (e as SystemError).code ?? "",
- syscall: (e as SystemError).syscall ?? "",
- message: (e as SystemError).message ?? "",
- },
- },
- { cause: e },
- ).toObject()
- case APICallError.isInstance(e):
- const message = iife(() => {
- let msg = e.message
- if (msg === "") {
- if (e.responseBody) return e.responseBody
- if (e.statusCode) {
- const err = STATUS_CODES[e.statusCode]
- if (err) return err
- }
- return "Unknown error"
- }
- const transformed = ProviderTransform.error(ctx.providerID, e)
- if (transformed !== msg) {
- return transformed
- }
- if (!e.responseBody || (e.statusCode && msg !== STATUS_CODES[e.statusCode])) {
- return msg
- }
- try {
- const body = JSON.parse(e.responseBody)
- // try to extract common error message fields
- const errMsg = body.message || body.error || body.error?.message
- if (errMsg && typeof errMsg === "string") {
- return `${msg}: ${errMsg}`
- }
- } catch {}
- return `${msg}: ${e.responseBody}`
- }).trim()
- const metadata = e.url ? { url: e.url } : undefined
- return new MessageV2.APIError(
- {
- message,
- statusCode: e.statusCode,
- isRetryable: ctx.providerID.startsWith("openai") ? isOpenAiErrorRetryable(e) : e.isRetryable,
- responseHeaders: e.responseHeaders,
- responseBody: e.responseBody,
- metadata,
- },
- { cause: e },
- ).toObject()
- case e instanceof Error:
- return new NamedError.Unknown({ message: e.toString() }, { cause: e }).toObject()
- default:
- return new NamedError.Unknown({ message: JSON.stringify(e) }, { cause: e })
- }
- }
- }
|