opencode_offline/packages/opencode/src/session/message.ts

190 lines
5.0 KiB
TypeScript
Raw Normal View History

import z from "zod"
2025-11-25 01:56:00 +08:00
import { NamedError } from "@opencode-ai/util/error"
2025-05-29 22:21:59 +08:00
export namespace Message {
export const OutputLengthError = NamedError.create("MessageOutputLengthError", z.object({}))
2025-07-15 12:05:54 +08:00
export const AuthError = NamedError.create(
"ProviderAuthError",
z.object({
providerID: z.string(),
message: z.string(),
}),
)
2025-06-25 23:02:09 +08:00
2025-05-29 22:21:59 +08:00
export const ToolCall = z
.object({
state: z.literal("call"),
step: z.number().optional(),
toolCallId: z.string(),
toolName: z.string(),
args: z.custom<Required<unknown>>(),
})
.meta({
ref: "ToolCall",
2025-06-01 02:41:00 +08:00
})
export type ToolCall = z.infer<typeof ToolCall>
2025-05-29 22:21:59 +08:00
export const ToolPartialCall = z
.object({
state: z.literal("partial-call"),
step: z.number().optional(),
toolCallId: z.string(),
toolName: z.string(),
args: z.custom<Required<unknown>>(),
})
.meta({
ref: "ToolPartialCall",
2025-06-01 02:41:00 +08:00
})
export type ToolPartialCall = z.infer<typeof ToolPartialCall>
2025-05-29 22:21:59 +08:00
export const ToolResult = z
.object({
state: z.literal("result"),
step: z.number().optional(),
toolCallId: z.string(),
toolName: z.string(),
args: z.custom<Required<unknown>>(),
result: z.string(),
})
.meta({
ref: "ToolResult",
2025-06-01 02:41:00 +08:00
})
export type ToolResult = z.infer<typeof ToolResult>
2025-05-29 22:21:59 +08:00
export const ToolInvocation = z.discriminatedUnion("state", [ToolCall, ToolPartialCall, ToolResult]).meta({
ref: "ToolInvocation",
})
2025-06-01 02:41:00 +08:00
export type ToolInvocation = z.infer<typeof ToolInvocation>
2025-05-29 22:21:59 +08:00
export const TextPart = z
.object({
type: z.literal("text"),
text: z.string(),
})
.meta({
ref: "TextPart",
2025-06-01 02:41:00 +08:00
})
export type TextPart = z.infer<typeof TextPart>
2025-05-29 22:21:59 +08:00
export const ReasoningPart = z
.object({
type: z.literal("reasoning"),
text: z.string(),
providerMetadata: z.record(z.string(), z.any()).optional(),
2025-05-29 22:21:59 +08:00
})
.meta({
ref: "ReasoningPart",
2025-06-01 02:41:00 +08:00
})
export type ReasoningPart = z.infer<typeof ReasoningPart>
2025-05-29 22:21:59 +08:00
export const ToolInvocationPart = z
.object({
type: z.literal("tool-invocation"),
toolInvocation: ToolInvocation,
})
.meta({
ref: "ToolInvocationPart",
2025-06-01 02:41:00 +08:00
})
export type ToolInvocationPart = z.infer<typeof ToolInvocationPart>
2025-05-29 22:21:59 +08:00
export const SourceUrlPart = z
.object({
type: z.literal("source-url"),
sourceId: z.string(),
url: z.string(),
title: z.string().optional(),
providerMetadata: z.record(z.string(), z.any()).optional(),
2025-05-29 22:21:59 +08:00
})
.meta({
ref: "SourceUrlPart",
2025-06-01 02:41:00 +08:00
})
export type SourceUrlPart = z.infer<typeof SourceUrlPart>
2025-05-29 22:21:59 +08:00
export const FilePart = z
.object({
type: z.literal("file"),
mediaType: z.string(),
filename: z.string().optional(),
url: z.string(),
})
.meta({
ref: "FilePart",
2025-06-01 02:41:00 +08:00
})
export type FilePart = z.infer<typeof FilePart>
2025-05-29 22:21:59 +08:00
export const StepStartPart = z
.object({
type: z.literal("step-start"),
})
.meta({
ref: "StepStartPart",
2025-06-01 02:41:00 +08:00
})
export type StepStartPart = z.infer<typeof StepStartPart>
2025-05-29 22:21:59 +08:00
export const MessagePart = z
.discriminatedUnion("type", [TextPart, ReasoningPart, ToolInvocationPart, SourceUrlPart, FilePart, StepStartPart])
.meta({
ref: "MessagePart",
2025-06-01 02:41:00 +08:00
})
export type MessagePart = z.infer<typeof MessagePart>
2025-05-29 22:21:59 +08:00
export const Info = z
.object({
id: z.string(),
2025-06-13 05:04:34 +08:00
role: z.enum(["user", "assistant"]),
parts: z.array(MessagePart),
metadata: z
.object({
time: z.object({
created: z.number(),
completed: z.number().optional(),
}),
error: z
2025-07-15 12:05:54 +08:00
.discriminatedUnion("name", [AuthError.Schema, NamedError.Unknown.Schema, OutputLengthError.Schema])
.optional(),
sessionID: z.string(),
tool: z.record(
z.string(),
z
.object({
title: z.string(),
2025-07-03 01:00:46 +08:00
snapshot: z.string().optional(),
time: z.object({
start: z.number(),
end: z.number(),
}),
})
.catchall(z.any()),
),
assistant: z
2025-06-12 00:44:17 +08:00
.object({
system: z.string().array(),
modelID: z.string(),
providerID: z.string(),
path: z.object({
cwd: z.string(),
root: z.string(),
2025-06-12 00:44:17 +08:00
}),
cost: z.number(),
summary: z.boolean().optional(),
tokens: z.object({
input: z.number(),
output: z.number(),
reasoning: z.number(),
cache: z.object({
read: z.number(),
write: z.number(),
}),
2025-06-17 00:43:22 +08:00
}),
})
.optional(),
2025-07-03 01:00:46 +08:00
snapshot: z.string().optional(),
})
.meta({ ref: "MessageMetadata" }),
2025-05-29 22:21:59 +08:00
})
.meta({
ref: "Message",
2025-06-01 02:41:00 +08:00
})
export type Info = z.infer<typeof Info>
2025-05-29 22:21:59 +08:00
}