cloudaxe-opencode/packages/opencode/src/session/message.ts

188 lines
4.9 KiB
TypeScript
Raw Normal View History

2025-05-31 18:41:00 +00:00
import z from "zod"
2025-06-09 18:01:11 +00:00
import { Provider } from "../provider/provider"
import { NamedError } from "../util/error"
2025-05-29 14:21:59 +00:00
export namespace Message {
export const OutputLengthError = NamedError.create("MessageOutputLengthError", z.object({}))
2025-06-25 15:02:09 +00:00
2025-05-29 14:21:59 +00: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>>(),
})
.openapi({
ref: "ToolCall",
2025-05-31 18:41:00 +00:00
})
export type ToolCall = z.infer<typeof ToolCall>
2025-05-29 14:21:59 +00: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>>(),
})
.openapi({
ref: "ToolPartialCall",
2025-05-31 18:41:00 +00:00
})
export type ToolPartialCall = z.infer<typeof ToolPartialCall>
2025-05-29 14:21:59 +00: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(),
})
.openapi({
ref: "ToolResult",
2025-05-31 18:41:00 +00:00
})
export type ToolResult = z.infer<typeof ToolResult>
2025-05-29 14:21:59 +00:00
export const ToolInvocation = z.discriminatedUnion("state", [ToolCall, ToolPartialCall, ToolResult]).openapi({
ref: "ToolInvocation",
})
2025-05-31 18:41:00 +00:00
export type ToolInvocation = z.infer<typeof ToolInvocation>
2025-05-29 14:21:59 +00:00
export const TextPart = z
.object({
type: z.literal("text"),
text: z.string(),
})
.openapi({
ref: "TextPart",
2025-05-31 18:41:00 +00:00
})
export type TextPart = z.infer<typeof TextPart>
2025-05-29 14:21:59 +00:00
export const ReasoningPart = z
.object({
type: z.literal("reasoning"),
text: z.string(),
providerMetadata: z.record(z.any()).optional(),
})
.openapi({
ref: "ReasoningPart",
2025-05-31 18:41:00 +00:00
})
export type ReasoningPart = z.infer<typeof ReasoningPart>
2025-05-29 14:21:59 +00:00
export const ToolInvocationPart = z
.object({
type: z.literal("tool-invocation"),
toolInvocation: ToolInvocation,
})
.openapi({
ref: "ToolInvocationPart",
2025-05-31 18:41:00 +00:00
})
export type ToolInvocationPart = z.infer<typeof ToolInvocationPart>
2025-05-29 14:21:59 +00: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.any()).optional(),
})
.openapi({
ref: "SourceUrlPart",
2025-05-31 18:41:00 +00:00
})
export type SourceUrlPart = z.infer<typeof SourceUrlPart>
2025-05-29 14:21:59 +00:00
export const FilePart = z
.object({
type: z.literal("file"),
mediaType: z.string(),
filename: z.string().optional(),
url: z.string(),
})
.openapi({
ref: "FilePart",
2025-05-31 18:41:00 +00:00
})
export type FilePart = z.infer<typeof FilePart>
2025-05-29 14:21:59 +00:00
export const StepStartPart = z
.object({
type: z.literal("step-start"),
})
.openapi({
ref: "StepStartPart",
2025-05-31 18:41:00 +00:00
})
export type StepStartPart = z.infer<typeof StepStartPart>
2025-05-29 14:21:59 +00:00
export const MessagePart = z
.discriminatedUnion("type", [TextPart, ReasoningPart, ToolInvocationPart, SourceUrlPart, FilePart, StepStartPart])
2025-05-29 14:21:59 +00:00
.openapi({
ref: "MessagePart",
2025-05-31 18:41:00 +00:00
})
export type MessagePart = z.infer<typeof MessagePart>
2025-05-29 14:21:59 +00:00
export const Info = z
.object({
id: z.string(),
2025-06-12 21:04:34 +00: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
.discriminatedUnion("name", [
Provider.AuthError.Schema,
NamedError.Unknown.Schema,
2025-06-25 15:02:09 +00:00
OutputLengthError.Schema,
])
.optional(),
sessionID: z.string(),
tool: z.record(
z.string(),
z
.object({
title: z.string(),
2025-07-02 17:00:46 +00:00
snapshot: z.string().optional(),
time: z.object({
start: z.number(),
end: z.number(),
}),
})
.catchall(z.any()),
),
assistant: z
2025-06-11 16:44:17 +00:00
.object({
system: z.string().array(),
modelID: z.string(),
providerID: z.string(),
path: z.object({
cwd: z.string(),
root: z.string(),
2025-06-11 16:44:17 +00: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-16 16:43:22 +00:00
}),
})
.optional(),
2025-07-02 17:00:46 +00:00
snapshot: z.string().optional(),
})
.openapi({ ref: "MessageMetadata" }),
2025-05-29 14:21:59 +00:00
})
.openapi({
ref: "Message",
2025-05-31 18:41:00 +00:00
})
export type Info = z.infer<typeof Info>
2025-05-29 14:21:59 +00:00
}