refactor: unwrap session/ tier-2 namespaces + self-reexport (#22973)
This commit is contained in:
@@ -17,23 +17,22 @@ import { Effect, Layer, Context } from "effect"
|
|||||||
import { InstanceState } from "@/effect"
|
import { InstanceState } from "@/effect"
|
||||||
import { isOverflow as overflow } from "./overflow"
|
import { isOverflow as overflow } from "./overflow"
|
||||||
|
|
||||||
export namespace SessionCompaction {
|
const log = Log.create({ service: "session.compaction" })
|
||||||
const log = Log.create({ service: "session.compaction" })
|
|
||||||
|
|
||||||
export const Event = {
|
export const Event = {
|
||||||
Compacted: BusEvent.define(
|
Compacted: BusEvent.define(
|
||||||
"session.compacted",
|
"session.compacted",
|
||||||
z.object({
|
z.object({
|
||||||
sessionID: SessionID.zod,
|
sessionID: SessionID.zod,
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
export const PRUNE_MINIMUM = 20_000
|
export const PRUNE_MINIMUM = 20_000
|
||||||
export const PRUNE_PROTECT = 40_000
|
export const PRUNE_PROTECT = 40_000
|
||||||
const PRUNE_PROTECTED_TOOLS = ["skill"]
|
const PRUNE_PROTECTED_TOOLS = ["skill"]
|
||||||
|
|
||||||
export interface Interface {
|
export interface Interface {
|
||||||
readonly isOverflow: (input: {
|
readonly isOverflow: (input: {
|
||||||
tokens: MessageV2.Assistant["tokens"]
|
tokens: MessageV2.Assistant["tokens"]
|
||||||
model: Provider.Model
|
model: Provider.Model
|
||||||
@@ -53,11 +52,11 @@ export namespace SessionCompaction {
|
|||||||
auto: boolean
|
auto: boolean
|
||||||
overflow?: boolean
|
overflow?: boolean
|
||||||
}) => Effect.Effect<void>
|
}) => Effect.Effect<void>
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionCompaction") {}
|
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionCompaction") {}
|
||||||
|
|
||||||
export const layer: Layer.Layer<
|
export const layer: Layer.Layer<
|
||||||
Service,
|
Service,
|
||||||
never,
|
never,
|
||||||
| Bus.Service
|
| Bus.Service
|
||||||
@@ -67,7 +66,7 @@ export namespace SessionCompaction {
|
|||||||
| Plugin.Service
|
| Plugin.Service
|
||||||
| SessionProcessor.Service
|
| SessionProcessor.Service
|
||||||
| Provider.Service
|
| Provider.Service
|
||||||
> = Layer.effect(
|
> = Layer.effect(
|
||||||
Service,
|
Service,
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
const bus = yield* Bus.Service
|
const bus = yield* Bus.Service
|
||||||
@@ -396,9 +395,9 @@ When constructing the summary, try to stick to this template:
|
|||||||
create,
|
create,
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
export const defaultLayer = Layer.suspend(() =>
|
export const defaultLayer = Layer.suspend(() =>
|
||||||
layer.pipe(
|
layer.pipe(
|
||||||
Layer.provide(Provider.defaultLayer),
|
Layer.provide(Provider.defaultLayer),
|
||||||
Layer.provide(Session.defaultLayer),
|
Layer.provide(Session.defaultLayer),
|
||||||
@@ -408,5 +407,6 @@ When constructing the summary, try to stick to this template:
|
|||||||
Layer.provide(Bus.layer),
|
Layer.provide(Bus.layer),
|
||||||
Layer.provide(Config.defaultLayer),
|
Layer.provide(Config.defaultLayer),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
export * as SessionCompaction from "./compaction"
|
||||||
|
|||||||
@@ -50,8 +50,7 @@ function extract(messages: MessageV2.WithParts[]) {
|
|||||||
return paths
|
return paths
|
||||||
}
|
}
|
||||||
|
|
||||||
export namespace Instruction {
|
export interface Interface {
|
||||||
export interface Interface {
|
|
||||||
readonly clear: (messageID: MessageID) => Effect.Effect<void>
|
readonly clear: (messageID: MessageID) => Effect.Effect<void>
|
||||||
readonly systemPaths: () => Effect.Effect<Set<string>, AppFileSystem.Error>
|
readonly systemPaths: () => Effect.Effect<Set<string>, AppFileSystem.Error>
|
||||||
readonly system: () => Effect.Effect<string[], AppFileSystem.Error>
|
readonly system: () => Effect.Effect<string[], AppFileSystem.Error>
|
||||||
@@ -61,11 +60,11 @@ export namespace Instruction {
|
|||||||
filepath: string,
|
filepath: string,
|
||||||
messageID: MessageID,
|
messageID: MessageID,
|
||||||
) => Effect.Effect<{ filepath: string; content: string }[], AppFileSystem.Error>
|
) => Effect.Effect<{ filepath: string; content: string }[], AppFileSystem.Error>
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Service extends Context.Service<Service, Interface>()("@opencode/Instruction") {}
|
export class Service extends Context.Service<Service, Interface>()("@opencode/Instruction") {}
|
||||||
|
|
||||||
export const layer: Layer.Layer<Service, never, AppFileSystem.Service | Config.Service | HttpClient.HttpClient> =
|
export const layer: Layer.Layer<Service, never, AppFileSystem.Service | Config.Service | HttpClient.HttpClient> =
|
||||||
Layer.effect(
|
Layer.effect(
|
||||||
Service,
|
Service,
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
@@ -231,13 +230,14 @@ export namespace Instruction {
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
export const defaultLayer = layer.pipe(
|
export const defaultLayer = layer.pipe(
|
||||||
Layer.provide(Config.defaultLayer),
|
Layer.provide(Config.defaultLayer),
|
||||||
Layer.provide(AppFileSystem.defaultLayer),
|
Layer.provide(AppFileSystem.defaultLayer),
|
||||||
Layer.provide(FetchHttpClient.layer),
|
Layer.provide(FetchHttpClient.layer),
|
||||||
)
|
)
|
||||||
|
|
||||||
export function loaded(messages: MessageV2.WithParts[]) {
|
export function loaded(messages: MessageV2.WithParts[]) {
|
||||||
return extract(messages)
|
return extract(messages)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export * as Instruction from "./instruction"
|
||||||
|
|||||||
@@ -25,12 +25,11 @@ import { EffectBridge } from "@/effect"
|
|||||||
import * as Option from "effect/Option"
|
import * as Option from "effect/Option"
|
||||||
import * as OtelTracer from "@effect/opentelemetry/Tracer"
|
import * as OtelTracer from "@effect/opentelemetry/Tracer"
|
||||||
|
|
||||||
export namespace LLM {
|
const log = Log.create({ service: "llm" })
|
||||||
const log = Log.create({ service: "llm" })
|
export const OUTPUT_TOKEN_MAX = ProviderTransform.OUTPUT_TOKEN_MAX
|
||||||
export const OUTPUT_TOKEN_MAX = ProviderTransform.OUTPUT_TOKEN_MAX
|
type Result = Awaited<ReturnType<typeof streamText>>
|
||||||
type Result = Awaited<ReturnType<typeof streamText>>
|
|
||||||
|
|
||||||
export type StreamInput = {
|
export type StreamInput = {
|
||||||
user: MessageV2.User
|
user: MessageV2.User
|
||||||
sessionID: string
|
sessionID: string
|
||||||
parentSessionID?: string
|
parentSessionID?: string
|
||||||
@@ -43,25 +42,25 @@ export namespace LLM {
|
|||||||
tools: Record<string, Tool>
|
tools: Record<string, Tool>
|
||||||
retries?: number
|
retries?: number
|
||||||
toolChoice?: "auto" | "required" | "none"
|
toolChoice?: "auto" | "required" | "none"
|
||||||
}
|
}
|
||||||
|
|
||||||
export type StreamRequest = StreamInput & {
|
export type StreamRequest = StreamInput & {
|
||||||
abort: AbortSignal
|
abort: AbortSignal
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Event = Result["fullStream"] extends AsyncIterable<infer T> ? T : never
|
export type Event = Result["fullStream"] extends AsyncIterable<infer T> ? T : never
|
||||||
|
|
||||||
export interface Interface {
|
export interface Interface {
|
||||||
readonly stream: (input: StreamInput) => Stream.Stream<Event, unknown>
|
readonly stream: (input: StreamInput) => Stream.Stream<Event, unknown>
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Service extends Context.Service<Service, Interface>()("@opencode/LLM") {}
|
export class Service extends Context.Service<Service, Interface>()("@opencode/LLM") {}
|
||||||
|
|
||||||
const live: Layer.Layer<
|
const live: Layer.Layer<
|
||||||
Service,
|
Service,
|
||||||
never,
|
never,
|
||||||
Auth.Service | Config.Service | Provider.Service | Plugin.Service | Permission.Service
|
Auth.Service | Config.Service | Provider.Service | Plugin.Service | Permission.Service
|
||||||
> = Layer.effect(
|
> = Layer.effect(
|
||||||
Service,
|
Service,
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
const auth = yield* Auth.Service
|
const auth = yield* Auth.Service
|
||||||
@@ -418,30 +417,30 @@ export namespace LLM {
|
|||||||
|
|
||||||
return Service.of({ stream })
|
return Service.of({ stream })
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
export const layer = live.pipe(Layer.provide(Permission.defaultLayer))
|
export const layer = live.pipe(Layer.provide(Permission.defaultLayer))
|
||||||
|
|
||||||
export const defaultLayer = Layer.suspend(() =>
|
export const defaultLayer = Layer.suspend(() =>
|
||||||
layer.pipe(
|
layer.pipe(
|
||||||
Layer.provide(Auth.defaultLayer),
|
Layer.provide(Auth.defaultLayer),
|
||||||
Layer.provide(Config.defaultLayer),
|
Layer.provide(Config.defaultLayer),
|
||||||
Layer.provide(Provider.defaultLayer),
|
Layer.provide(Provider.defaultLayer),
|
||||||
Layer.provide(Plugin.defaultLayer),
|
Layer.provide(Plugin.defaultLayer),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
function resolveTools(input: Pick<StreamInput, "tools" | "agent" | "permission" | "user">) {
|
function resolveTools(input: Pick<StreamInput, "tools" | "agent" | "permission" | "user">) {
|
||||||
const disabled = Permission.disabled(
|
const disabled = Permission.disabled(
|
||||||
Object.keys(input.tools),
|
Object.keys(input.tools),
|
||||||
Permission.merge(input.agent.permission, input.permission ?? []),
|
Permission.merge(input.agent.permission, input.permission ?? []),
|
||||||
)
|
)
|
||||||
return Record.filter(input.tools, (_, k) => input.user.tools?.[k] !== false && !disabled.has(k))
|
return Record.filter(input.tools, (_, k) => input.user.tools?.[k] !== false && !disabled.has(k))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if messages contain any tool-call content
|
// Check if messages contain any tool-call content
|
||||||
// Used to determine if a dummy tool should be added for LiteLLM proxy compatibility
|
// Used to determine if a dummy tool should be added for LiteLLM proxy compatibility
|
||||||
export function hasToolCalls(messages: ModelMessage[]): boolean {
|
export function hasToolCalls(messages: ModelMessage[]): boolean {
|
||||||
for (const msg of messages) {
|
for (const msg of messages) {
|
||||||
if (!Array.isArray(msg.content)) continue
|
if (!Array.isArray(msg.content)) continue
|
||||||
for (const part of msg.content) {
|
for (const part of msg.content) {
|
||||||
@@ -449,5 +448,6 @@ export namespace LLM {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export * as LLM from "./llm"
|
||||||
|
|||||||
@@ -24,30 +24,29 @@ interface FetchDecompressionError extends Error {
|
|||||||
path: string
|
path: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export namespace MessageV2 {
|
export const SYNTHETIC_ATTACHMENT_PROMPT = "Attached image(s) from tool result:"
|
||||||
export const SYNTHETIC_ATTACHMENT_PROMPT = "Attached image(s) from tool result:"
|
|
||||||
|
|
||||||
export function isMedia(mime: string) {
|
export function isMedia(mime: string) {
|
||||||
return mime.startsWith("image/") || mime === "application/pdf"
|
return mime.startsWith("image/") || mime === "application/pdf"
|
||||||
}
|
}
|
||||||
|
|
||||||
export const OutputLengthError = NamedError.create("MessageOutputLengthError", z.object({}))
|
export const OutputLengthError = NamedError.create("MessageOutputLengthError", z.object({}))
|
||||||
export const AbortedError = NamedError.create("MessageAbortedError", z.object({ message: z.string() }))
|
export const AbortedError = NamedError.create("MessageAbortedError", z.object({ message: z.string() }))
|
||||||
export const StructuredOutputError = NamedError.create(
|
export const StructuredOutputError = NamedError.create(
|
||||||
"StructuredOutputError",
|
"StructuredOutputError",
|
||||||
z.object({
|
z.object({
|
||||||
message: z.string(),
|
message: z.string(),
|
||||||
retries: z.number(),
|
retries: z.number(),
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
export const AuthError = NamedError.create(
|
export const AuthError = NamedError.create(
|
||||||
"ProviderAuthError",
|
"ProviderAuthError",
|
||||||
z.object({
|
z.object({
|
||||||
providerID: z.string(),
|
providerID: z.string(),
|
||||||
message: z.string(),
|
message: z.string(),
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
export const APIError = NamedError.create(
|
export const APIError = NamedError.create(
|
||||||
"APIError",
|
"APIError",
|
||||||
z.object({
|
z.object({
|
||||||
message: z.string(),
|
message: z.string(),
|
||||||
@@ -57,14 +56,14 @@ export namespace MessageV2 {
|
|||||||
responseBody: z.string().optional(),
|
responseBody: z.string().optional(),
|
||||||
metadata: z.record(z.string(), z.string()).optional(),
|
metadata: z.record(z.string(), z.string()).optional(),
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
export type APIError = z.infer<typeof APIError.Schema>
|
export type APIError = z.infer<typeof APIError.Schema>
|
||||||
export const ContextOverflowError = NamedError.create(
|
export const ContextOverflowError = NamedError.create(
|
||||||
"ContextOverflowError",
|
"ContextOverflowError",
|
||||||
z.object({ message: z.string(), responseBody: z.string().optional() }),
|
z.object({ message: z.string(), responseBody: z.string().optional() }),
|
||||||
)
|
)
|
||||||
|
|
||||||
export const OutputFormatText = z
|
export const OutputFormatText = z
|
||||||
.object({
|
.object({
|
||||||
type: z.literal("text"),
|
type: z.literal("text"),
|
||||||
})
|
})
|
||||||
@@ -72,7 +71,7 @@ export namespace MessageV2 {
|
|||||||
ref: "OutputFormatText",
|
ref: "OutputFormatText",
|
||||||
})
|
})
|
||||||
|
|
||||||
export const OutputFormatJsonSchema = z
|
export const OutputFormatJsonSchema = z
|
||||||
.object({
|
.object({
|
||||||
type: z.literal("json_schema"),
|
type: z.literal("json_schema"),
|
||||||
schema: z.record(z.string(), z.any()).meta({ ref: "JSONSchema" }),
|
schema: z.record(z.string(), z.any()).meta({ ref: "JSONSchema" }),
|
||||||
@@ -82,35 +81,35 @@ export namespace MessageV2 {
|
|||||||
ref: "OutputFormatJsonSchema",
|
ref: "OutputFormatJsonSchema",
|
||||||
})
|
})
|
||||||
|
|
||||||
export const Format = z.discriminatedUnion("type", [OutputFormatText, OutputFormatJsonSchema]).meta({
|
export const Format = z.discriminatedUnion("type", [OutputFormatText, OutputFormatJsonSchema]).meta({
|
||||||
ref: "OutputFormat",
|
ref: "OutputFormat",
|
||||||
})
|
})
|
||||||
export type OutputFormat = z.infer<typeof Format>
|
export type OutputFormat = z.infer<typeof Format>
|
||||||
|
|
||||||
const PartBase = z.object({
|
const PartBase = z.object({
|
||||||
id: PartID.zod,
|
id: PartID.zod,
|
||||||
sessionID: SessionID.zod,
|
sessionID: SessionID.zod,
|
||||||
messageID: MessageID.zod,
|
messageID: MessageID.zod,
|
||||||
})
|
})
|
||||||
|
|
||||||
export const SnapshotPart = PartBase.extend({
|
export const SnapshotPart = PartBase.extend({
|
||||||
type: z.literal("snapshot"),
|
type: z.literal("snapshot"),
|
||||||
snapshot: z.string(),
|
snapshot: z.string(),
|
||||||
}).meta({
|
}).meta({
|
||||||
ref: "SnapshotPart",
|
ref: "SnapshotPart",
|
||||||
})
|
})
|
||||||
export type SnapshotPart = z.infer<typeof SnapshotPart>
|
export type SnapshotPart = z.infer<typeof SnapshotPart>
|
||||||
|
|
||||||
export const PatchPart = PartBase.extend({
|
export const PatchPart = PartBase.extend({
|
||||||
type: z.literal("patch"),
|
type: z.literal("patch"),
|
||||||
hash: z.string(),
|
hash: z.string(),
|
||||||
files: z.string().array(),
|
files: z.string().array(),
|
||||||
}).meta({
|
}).meta({
|
||||||
ref: "PatchPart",
|
ref: "PatchPart",
|
||||||
})
|
})
|
||||||
export type PatchPart = z.infer<typeof PatchPart>
|
export type PatchPart = z.infer<typeof PatchPart>
|
||||||
|
|
||||||
export const TextPart = PartBase.extend({
|
export const TextPart = PartBase.extend({
|
||||||
type: z.literal("text"),
|
type: z.literal("text"),
|
||||||
text: z.string(),
|
text: z.string(),
|
||||||
synthetic: z.boolean().optional(),
|
synthetic: z.boolean().optional(),
|
||||||
@@ -122,12 +121,12 @@ export namespace MessageV2 {
|
|||||||
})
|
})
|
||||||
.optional(),
|
.optional(),
|
||||||
metadata: z.record(z.string(), z.any()).optional(),
|
metadata: z.record(z.string(), z.any()).optional(),
|
||||||
}).meta({
|
}).meta({
|
||||||
ref: "TextPart",
|
ref: "TextPart",
|
||||||
})
|
})
|
||||||
export type TextPart = z.infer<typeof TextPart>
|
export type TextPart = z.infer<typeof TextPart>
|
||||||
|
|
||||||
export const ReasoningPart = PartBase.extend({
|
export const ReasoningPart = PartBase.extend({
|
||||||
type: z.literal("reasoning"),
|
type: z.literal("reasoning"),
|
||||||
text: z.string(),
|
text: z.string(),
|
||||||
metadata: z.record(z.string(), z.any()).optional(),
|
metadata: z.record(z.string(), z.any()).optional(),
|
||||||
@@ -135,12 +134,12 @@ export namespace MessageV2 {
|
|||||||
start: z.number(),
|
start: z.number(),
|
||||||
end: z.number().optional(),
|
end: z.number().optional(),
|
||||||
}),
|
}),
|
||||||
}).meta({
|
}).meta({
|
||||||
ref: "ReasoningPart",
|
ref: "ReasoningPart",
|
||||||
})
|
})
|
||||||
export type ReasoningPart = z.infer<typeof ReasoningPart>
|
export type ReasoningPart = z.infer<typeof ReasoningPart>
|
||||||
|
|
||||||
const FilePartSourceBase = z.object({
|
const FilePartSourceBase = z.object({
|
||||||
text: z
|
text: z
|
||||||
.object({
|
.object({
|
||||||
value: z.string(),
|
value: z.string(),
|
||||||
@@ -150,49 +149,49 @@ export namespace MessageV2 {
|
|||||||
.meta({
|
.meta({
|
||||||
ref: "FilePartSourceText",
|
ref: "FilePartSourceText",
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
export const FileSource = FilePartSourceBase.extend({
|
export const FileSource = FilePartSourceBase.extend({
|
||||||
type: z.literal("file"),
|
type: z.literal("file"),
|
||||||
path: z.string(),
|
path: z.string(),
|
||||||
}).meta({
|
}).meta({
|
||||||
ref: "FileSource",
|
ref: "FileSource",
|
||||||
})
|
})
|
||||||
|
|
||||||
export const SymbolSource = FilePartSourceBase.extend({
|
export const SymbolSource = FilePartSourceBase.extend({
|
||||||
type: z.literal("symbol"),
|
type: z.literal("symbol"),
|
||||||
path: z.string(),
|
path: z.string(),
|
||||||
range: LSP.Range,
|
range: LSP.Range,
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
kind: z.number().int(),
|
kind: z.number().int(),
|
||||||
}).meta({
|
}).meta({
|
||||||
ref: "SymbolSource",
|
ref: "SymbolSource",
|
||||||
})
|
})
|
||||||
|
|
||||||
export const ResourceSource = FilePartSourceBase.extend({
|
export const ResourceSource = FilePartSourceBase.extend({
|
||||||
type: z.literal("resource"),
|
type: z.literal("resource"),
|
||||||
clientName: z.string(),
|
clientName: z.string(),
|
||||||
uri: z.string(),
|
uri: z.string(),
|
||||||
}).meta({
|
}).meta({
|
||||||
ref: "ResourceSource",
|
ref: "ResourceSource",
|
||||||
})
|
})
|
||||||
|
|
||||||
export const FilePartSource = z.discriminatedUnion("type", [FileSource, SymbolSource, ResourceSource]).meta({
|
export const FilePartSource = z.discriminatedUnion("type", [FileSource, SymbolSource, ResourceSource]).meta({
|
||||||
ref: "FilePartSource",
|
ref: "FilePartSource",
|
||||||
})
|
})
|
||||||
|
|
||||||
export const FilePart = PartBase.extend({
|
export const FilePart = PartBase.extend({
|
||||||
type: z.literal("file"),
|
type: z.literal("file"),
|
||||||
mime: z.string(),
|
mime: z.string(),
|
||||||
filename: z.string().optional(),
|
filename: z.string().optional(),
|
||||||
url: z.string(),
|
url: z.string(),
|
||||||
source: FilePartSource.optional(),
|
source: FilePartSource.optional(),
|
||||||
}).meta({
|
}).meta({
|
||||||
ref: "FilePart",
|
ref: "FilePart",
|
||||||
})
|
})
|
||||||
export type FilePart = z.infer<typeof FilePart>
|
export type FilePart = z.infer<typeof FilePart>
|
||||||
|
|
||||||
export const AgentPart = PartBase.extend({
|
export const AgentPart = PartBase.extend({
|
||||||
type: z.literal("agent"),
|
type: z.literal("agent"),
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
source: z
|
source: z
|
||||||
@@ -202,21 +201,21 @@ export namespace MessageV2 {
|
|||||||
end: z.number().int(),
|
end: z.number().int(),
|
||||||
})
|
})
|
||||||
.optional(),
|
.optional(),
|
||||||
}).meta({
|
}).meta({
|
||||||
ref: "AgentPart",
|
ref: "AgentPart",
|
||||||
})
|
})
|
||||||
export type AgentPart = z.infer<typeof AgentPart>
|
export type AgentPart = z.infer<typeof AgentPart>
|
||||||
|
|
||||||
export const CompactionPart = PartBase.extend({
|
export const CompactionPart = PartBase.extend({
|
||||||
type: z.literal("compaction"),
|
type: z.literal("compaction"),
|
||||||
auto: z.boolean(),
|
auto: z.boolean(),
|
||||||
overflow: z.boolean().optional(),
|
overflow: z.boolean().optional(),
|
||||||
}).meta({
|
}).meta({
|
||||||
ref: "CompactionPart",
|
ref: "CompactionPart",
|
||||||
})
|
})
|
||||||
export type CompactionPart = z.infer<typeof CompactionPart>
|
export type CompactionPart = z.infer<typeof CompactionPart>
|
||||||
|
|
||||||
export const SubtaskPart = PartBase.extend({
|
export const SubtaskPart = PartBase.extend({
|
||||||
type: z.literal("subtask"),
|
type: z.literal("subtask"),
|
||||||
prompt: z.string(),
|
prompt: z.string(),
|
||||||
description: z.string(),
|
description: z.string(),
|
||||||
@@ -228,32 +227,32 @@ export namespace MessageV2 {
|
|||||||
})
|
})
|
||||||
.optional(),
|
.optional(),
|
||||||
command: z.string().optional(),
|
command: z.string().optional(),
|
||||||
}).meta({
|
}).meta({
|
||||||
ref: "SubtaskPart",
|
ref: "SubtaskPart",
|
||||||
})
|
})
|
||||||
export type SubtaskPart = z.infer<typeof SubtaskPart>
|
export type SubtaskPart = z.infer<typeof SubtaskPart>
|
||||||
|
|
||||||
export const RetryPart = PartBase.extend({
|
export const RetryPart = PartBase.extend({
|
||||||
type: z.literal("retry"),
|
type: z.literal("retry"),
|
||||||
attempt: z.number(),
|
attempt: z.number(),
|
||||||
error: APIError.Schema,
|
error: APIError.Schema,
|
||||||
time: z.object({
|
time: z.object({
|
||||||
created: z.number(),
|
created: z.number(),
|
||||||
}),
|
}),
|
||||||
}).meta({
|
}).meta({
|
||||||
ref: "RetryPart",
|
ref: "RetryPart",
|
||||||
})
|
})
|
||||||
export type RetryPart = z.infer<typeof RetryPart>
|
export type RetryPart = z.infer<typeof RetryPart>
|
||||||
|
|
||||||
export const StepStartPart = PartBase.extend({
|
export const StepStartPart = PartBase.extend({
|
||||||
type: z.literal("step-start"),
|
type: z.literal("step-start"),
|
||||||
snapshot: z.string().optional(),
|
snapshot: z.string().optional(),
|
||||||
}).meta({
|
}).meta({
|
||||||
ref: "StepStartPart",
|
ref: "StepStartPart",
|
||||||
})
|
})
|
||||||
export type StepStartPart = z.infer<typeof StepStartPart>
|
export type StepStartPart = z.infer<typeof StepStartPart>
|
||||||
|
|
||||||
export const StepFinishPart = PartBase.extend({
|
export const StepFinishPart = PartBase.extend({
|
||||||
type: z.literal("step-finish"),
|
type: z.literal("step-finish"),
|
||||||
reason: z.string(),
|
reason: z.string(),
|
||||||
snapshot: z.string().optional(),
|
snapshot: z.string().optional(),
|
||||||
@@ -268,12 +267,12 @@ export namespace MessageV2 {
|
|||||||
write: z.number(),
|
write: z.number(),
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
}).meta({
|
}).meta({
|
||||||
ref: "StepFinishPart",
|
ref: "StepFinishPart",
|
||||||
})
|
})
|
||||||
export type StepFinishPart = z.infer<typeof StepFinishPart>
|
export type StepFinishPart = z.infer<typeof StepFinishPart>
|
||||||
|
|
||||||
export const ToolStatePending = z
|
export const ToolStatePending = z
|
||||||
.object({
|
.object({
|
||||||
status: z.literal("pending"),
|
status: z.literal("pending"),
|
||||||
input: z.record(z.string(), z.any()),
|
input: z.record(z.string(), z.any()),
|
||||||
@@ -283,9 +282,9 @@ export namespace MessageV2 {
|
|||||||
ref: "ToolStatePending",
|
ref: "ToolStatePending",
|
||||||
})
|
})
|
||||||
|
|
||||||
export type ToolStatePending = z.infer<typeof ToolStatePending>
|
export type ToolStatePending = z.infer<typeof ToolStatePending>
|
||||||
|
|
||||||
export const ToolStateRunning = z
|
export const ToolStateRunning = z
|
||||||
.object({
|
.object({
|
||||||
status: z.literal("running"),
|
status: z.literal("running"),
|
||||||
input: z.record(z.string(), z.any()),
|
input: z.record(z.string(), z.any()),
|
||||||
@@ -298,9 +297,9 @@ export namespace MessageV2 {
|
|||||||
.meta({
|
.meta({
|
||||||
ref: "ToolStateRunning",
|
ref: "ToolStateRunning",
|
||||||
})
|
})
|
||||||
export type ToolStateRunning = z.infer<typeof ToolStateRunning>
|
export type ToolStateRunning = z.infer<typeof ToolStateRunning>
|
||||||
|
|
||||||
export const ToolStateCompleted = z
|
export const ToolStateCompleted = z
|
||||||
.object({
|
.object({
|
||||||
status: z.literal("completed"),
|
status: z.literal("completed"),
|
||||||
input: z.record(z.string(), z.any()),
|
input: z.record(z.string(), z.any()),
|
||||||
@@ -317,9 +316,9 @@ export namespace MessageV2 {
|
|||||||
.meta({
|
.meta({
|
||||||
ref: "ToolStateCompleted",
|
ref: "ToolStateCompleted",
|
||||||
})
|
})
|
||||||
export type ToolStateCompleted = z.infer<typeof ToolStateCompleted>
|
export type ToolStateCompleted = z.infer<typeof ToolStateCompleted>
|
||||||
|
|
||||||
export const ToolStateError = z
|
export const ToolStateError = z
|
||||||
.object({
|
.object({
|
||||||
status: z.literal("error"),
|
status: z.literal("error"),
|
||||||
input: z.record(z.string(), z.any()),
|
input: z.record(z.string(), z.any()),
|
||||||
@@ -333,31 +332,31 @@ export namespace MessageV2 {
|
|||||||
.meta({
|
.meta({
|
||||||
ref: "ToolStateError",
|
ref: "ToolStateError",
|
||||||
})
|
})
|
||||||
export type ToolStateError = z.infer<typeof ToolStateError>
|
export type ToolStateError = z.infer<typeof ToolStateError>
|
||||||
|
|
||||||
export const ToolState = z
|
export const ToolState = z
|
||||||
.discriminatedUnion("status", [ToolStatePending, ToolStateRunning, ToolStateCompleted, ToolStateError])
|
.discriminatedUnion("status", [ToolStatePending, ToolStateRunning, ToolStateCompleted, ToolStateError])
|
||||||
.meta({
|
.meta({
|
||||||
ref: "ToolState",
|
ref: "ToolState",
|
||||||
})
|
})
|
||||||
|
|
||||||
export const ToolPart = PartBase.extend({
|
export const ToolPart = PartBase.extend({
|
||||||
type: z.literal("tool"),
|
type: z.literal("tool"),
|
||||||
callID: z.string(),
|
callID: z.string(),
|
||||||
tool: z.string(),
|
tool: z.string(),
|
||||||
state: ToolState,
|
state: ToolState,
|
||||||
metadata: z.record(z.string(), z.any()).optional(),
|
metadata: z.record(z.string(), z.any()).optional(),
|
||||||
}).meta({
|
}).meta({
|
||||||
ref: "ToolPart",
|
ref: "ToolPart",
|
||||||
})
|
})
|
||||||
export type ToolPart = z.infer<typeof ToolPart>
|
export type ToolPart = z.infer<typeof ToolPart>
|
||||||
|
|
||||||
const Base = z.object({
|
const Base = z.object({
|
||||||
id: MessageID.zod,
|
id: MessageID.zod,
|
||||||
sessionID: SessionID.zod,
|
sessionID: SessionID.zod,
|
||||||
})
|
})
|
||||||
|
|
||||||
export const User = Base.extend({
|
export const User = Base.extend({
|
||||||
role: z.literal("user"),
|
role: z.literal("user"),
|
||||||
time: z.object({
|
time: z.object({
|
||||||
created: z.number(),
|
created: z.number(),
|
||||||
@@ -378,12 +377,12 @@ export namespace MessageV2 {
|
|||||||
}),
|
}),
|
||||||
system: z.string().optional(),
|
system: z.string().optional(),
|
||||||
tools: z.record(z.string(), z.boolean()).optional(),
|
tools: z.record(z.string(), z.boolean()).optional(),
|
||||||
}).meta({
|
}).meta({
|
||||||
ref: "UserMessage",
|
ref: "UserMessage",
|
||||||
})
|
})
|
||||||
export type User = z.infer<typeof User>
|
export type User = z.infer<typeof User>
|
||||||
|
|
||||||
export const Part = z
|
export const Part = z
|
||||||
.discriminatedUnion("type", [
|
.discriminatedUnion("type", [
|
||||||
TextPart,
|
TextPart,
|
||||||
SubtaskPart,
|
SubtaskPart,
|
||||||
@@ -401,9 +400,9 @@ export namespace MessageV2 {
|
|||||||
.meta({
|
.meta({
|
||||||
ref: "Part",
|
ref: "Part",
|
||||||
})
|
})
|
||||||
export type Part = z.infer<typeof Part>
|
export type Part = z.infer<typeof Part>
|
||||||
|
|
||||||
export const Assistant = Base.extend({
|
export const Assistant = Base.extend({
|
||||||
role: z.literal("assistant"),
|
role: z.literal("assistant"),
|
||||||
time: z.object({
|
time: z.object({
|
||||||
created: z.number(),
|
created: z.number(),
|
||||||
@@ -447,17 +446,17 @@ export namespace MessageV2 {
|
|||||||
structured: z.any().optional(),
|
structured: z.any().optional(),
|
||||||
variant: z.string().optional(),
|
variant: z.string().optional(),
|
||||||
finish: z.string().optional(),
|
finish: z.string().optional(),
|
||||||
}).meta({
|
}).meta({
|
||||||
ref: "AssistantMessage",
|
ref: "AssistantMessage",
|
||||||
})
|
})
|
||||||
export type Assistant = z.infer<typeof Assistant>
|
export type Assistant = z.infer<typeof Assistant>
|
||||||
|
|
||||||
export const Info = z.discriminatedUnion("role", [User, Assistant]).meta({
|
export const Info = z.discriminatedUnion("role", [User, Assistant]).meta({
|
||||||
ref: "Message",
|
ref: "Message",
|
||||||
})
|
})
|
||||||
export type Info = z.infer<typeof Info>
|
export type Info = z.infer<typeof Info>
|
||||||
|
|
||||||
export const Event = {
|
export const Event = {
|
||||||
Updated: SyncEvent.define({
|
Updated: SyncEvent.define({
|
||||||
type: "message.updated",
|
type: "message.updated",
|
||||||
version: 1,
|
version: 1,
|
||||||
@@ -506,53 +505,53 @@ export namespace MessageV2 {
|
|||||||
partID: PartID.zod,
|
partID: PartID.zod,
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
export const WithParts = z.object({
|
export const WithParts = z.object({
|
||||||
info: Info,
|
info: Info,
|
||||||
parts: z.array(Part),
|
parts: z.array(Part),
|
||||||
})
|
})
|
||||||
export type WithParts = z.infer<typeof WithParts>
|
export type WithParts = z.infer<typeof WithParts>
|
||||||
|
|
||||||
const Cursor = z.object({
|
const Cursor = z.object({
|
||||||
id: MessageID.zod,
|
id: MessageID.zod,
|
||||||
time: z.number(),
|
time: z.number(),
|
||||||
})
|
})
|
||||||
type Cursor = z.infer<typeof Cursor>
|
type Cursor = z.infer<typeof Cursor>
|
||||||
|
|
||||||
export const cursor = {
|
export const cursor = {
|
||||||
encode(input: Cursor) {
|
encode(input: Cursor) {
|
||||||
return Buffer.from(JSON.stringify(input)).toString("base64url")
|
return Buffer.from(JSON.stringify(input)).toString("base64url")
|
||||||
},
|
},
|
||||||
decode(input: string) {
|
decode(input: string) {
|
||||||
return Cursor.parse(JSON.parse(Buffer.from(input, "base64url").toString("utf8")))
|
return Cursor.parse(JSON.parse(Buffer.from(input, "base64url").toString("utf8")))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
const info = (row: typeof MessageTable.$inferSelect) =>
|
const info = (row: typeof MessageTable.$inferSelect) =>
|
||||||
({
|
({
|
||||||
...row.data,
|
...row.data,
|
||||||
id: row.id,
|
id: row.id,
|
||||||
sessionID: row.session_id,
|
sessionID: row.session_id,
|
||||||
}) as MessageV2.Info
|
}) as Info
|
||||||
|
|
||||||
const part = (row: typeof PartTable.$inferSelect) =>
|
const part = (row: typeof PartTable.$inferSelect) =>
|
||||||
({
|
({
|
||||||
...row.data,
|
...row.data,
|
||||||
id: row.id,
|
id: row.id,
|
||||||
sessionID: row.session_id,
|
sessionID: row.session_id,
|
||||||
messageID: row.message_id,
|
messageID: row.message_id,
|
||||||
}) as MessageV2.Part
|
}) as Part
|
||||||
|
|
||||||
const older = (row: Cursor) =>
|
const older = (row: Cursor) =>
|
||||||
or(
|
or(
|
||||||
lt(MessageTable.time_created, row.time),
|
lt(MessageTable.time_created, row.time),
|
||||||
and(eq(MessageTable.time_created, row.time), lt(MessageTable.id, row.id)),
|
and(eq(MessageTable.time_created, row.time), lt(MessageTable.id, row.id)),
|
||||||
)
|
)
|
||||||
|
|
||||||
function hydrate(rows: (typeof MessageTable.$inferSelect)[]) {
|
function hydrate(rows: (typeof MessageTable.$inferSelect)[]) {
|
||||||
const ids = rows.map((row) => row.id)
|
const ids = rows.map((row) => row.id)
|
||||||
const partByMessage = new Map<string, MessageV2.Part[]>()
|
const partByMessage = new Map<string, Part[]>()
|
||||||
if (ids.length > 0) {
|
if (ids.length > 0) {
|
||||||
const partRows = Database.use((db) =>
|
const partRows = Database.use((db) =>
|
||||||
db
|
db
|
||||||
@@ -574,19 +573,19 @@ export namespace MessageV2 {
|
|||||||
info: info(row),
|
info: info(row),
|
||||||
parts: partByMessage.get(row.id) ?? [],
|
parts: partByMessage.get(row.id) ?? [],
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
function providerMeta(metadata: Record<string, any> | undefined) {
|
function providerMeta(metadata: Record<string, any> | undefined) {
|
||||||
if (!metadata) return undefined
|
if (!metadata) return undefined
|
||||||
const { providerExecuted: _, ...rest } = metadata
|
const { providerExecuted: _, ...rest } = metadata
|
||||||
return Object.keys(rest).length > 0 ? rest : undefined
|
return Object.keys(rest).length > 0 ? rest : undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
export const toModelMessagesEffect = Effect.fnUntraced(function* (
|
export const toModelMessagesEffect = Effect.fnUntraced(function* (
|
||||||
input: WithParts[],
|
input: WithParts[],
|
||||||
model: Provider.Model,
|
model: Provider.Model,
|
||||||
options?: { stripMedia?: boolean },
|
options?: { stripMedia?: boolean },
|
||||||
) {
|
) {
|
||||||
const result: UIMessage[] = []
|
const result: UIMessage[] = []
|
||||||
const toolNames = new Set<string>()
|
const toolNames = new Set<string>()
|
||||||
// Track media from tool results that need to be injected as user messages
|
// Track media from tool results that need to be injected as user messages
|
||||||
@@ -699,7 +698,7 @@ export namespace MessageV2 {
|
|||||||
if (
|
if (
|
||||||
msg.info.error &&
|
msg.info.error &&
|
||||||
!(
|
!(
|
||||||
MessageV2.AbortedError.isInstance(msg.info.error) &&
|
AbortedError.isInstance(msg.info.error) &&
|
||||||
msg.parts.some((part) => part.type !== "step-start" && part.type !== "reasoning")
|
msg.parts.some((part) => part.type !== "step-start" && part.type !== "reasoning")
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
@@ -835,17 +834,17 @@ export namespace MessageV2 {
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
export function toModelMessages(
|
export function toModelMessages(
|
||||||
input: WithParts[],
|
input: WithParts[],
|
||||||
model: Provider.Model,
|
model: Provider.Model,
|
||||||
options?: { stripMedia?: boolean },
|
options?: { stripMedia?: boolean },
|
||||||
): Promise<ModelMessage[]> {
|
): Promise<ModelMessage[]> {
|
||||||
return Effect.runPromise(toModelMessagesEffect(input, model, options).pipe(Effect.provide(EffectLogger.layer)))
|
return Effect.runPromise(toModelMessagesEffect(input, model, options).pipe(Effect.provide(EffectLogger.layer)))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function page(input: { sessionID: SessionID; limit: number; before?: string }) {
|
export function page(input: { sessionID: SessionID; limit: number; before?: string }) {
|
||||||
const before = input.before ? cursor.decode(input.before) : undefined
|
const before = input.before ? cursor.decode(input.before) : undefined
|
||||||
const where = before
|
const where = before
|
||||||
? and(eq(MessageTable.session_id, input.sessionID), older(before))
|
? and(eq(MessageTable.session_id, input.sessionID), older(before))
|
||||||
@@ -865,7 +864,7 @@ export namespace MessageV2 {
|
|||||||
)
|
)
|
||||||
if (!row) throw new NotFoundError({ message: `Session not found: ${input.sessionID}` })
|
if (!row) throw new NotFoundError({ message: `Session not found: ${input.sessionID}` })
|
||||||
return {
|
return {
|
||||||
items: [] as MessageV2.WithParts[],
|
items: [] as WithParts[],
|
||||||
more: false,
|
more: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -880,9 +879,9 @@ export namespace MessageV2 {
|
|||||||
more,
|
more,
|
||||||
cursor: more && tail ? cursor.encode({ id: tail.id, time: tail.time_created }) : undefined,
|
cursor: more && tail ? cursor.encode({ id: tail.id, time: tail.time_created }) : undefined,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function* stream(sessionID: SessionID) {
|
export function* stream(sessionID: SessionID) {
|
||||||
const size = 50
|
const size = 50
|
||||||
let before: string | undefined
|
let before: string | undefined
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -894,9 +893,9 @@ export namespace MessageV2 {
|
|||||||
if (!next.more || !next.cursor) break
|
if (!next.more || !next.cursor) break
|
||||||
before = next.cursor
|
before = next.cursor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parts(message_id: MessageID) {
|
export function parts(message_id: MessageID) {
|
||||||
const rows = Database.use((db) =>
|
const rows = Database.use((db) =>
|
||||||
db.select().from(PartTable).where(eq(PartTable.message_id, message_id)).orderBy(PartTable.id).all(),
|
db.select().from(PartTable).where(eq(PartTable.message_id, message_id)).orderBy(PartTable.id).all(),
|
||||||
)
|
)
|
||||||
@@ -907,11 +906,11 @@ export namespace MessageV2 {
|
|||||||
id: row.id,
|
id: row.id,
|
||||||
sessionID: row.session_id,
|
sessionID: row.session_id,
|
||||||
messageID: row.message_id,
|
messageID: row.message_id,
|
||||||
}) as MessageV2.Part,
|
}) as Part,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function get(input: { sessionID: SessionID; messageID: MessageID }): WithParts {
|
export function get(input: { sessionID: SessionID; messageID: MessageID }): WithParts {
|
||||||
const row = Database.use((db) =>
|
const row = Database.use((db) =>
|
||||||
db
|
db
|
||||||
.select()
|
.select()
|
||||||
@@ -924,10 +923,10 @@ export namespace MessageV2 {
|
|||||||
info: info(row),
|
info: info(row),
|
||||||
parts: parts(input.messageID),
|
parts: parts(input.messageID),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function filterCompacted(msgs: Iterable<MessageV2.WithParts>) {
|
export function filterCompacted(msgs: Iterable<WithParts>) {
|
||||||
const result = [] as MessageV2.WithParts[]
|
const result = [] as WithParts[]
|
||||||
const completed = new Set<string>()
|
const completed = new Set<string>()
|
||||||
for (const msg of msgs) {
|
for (const msg of msgs) {
|
||||||
result.push(msg)
|
result.push(msg)
|
||||||
@@ -942,28 +941,28 @@ export namespace MessageV2 {
|
|||||||
}
|
}
|
||||||
result.reverse()
|
result.reverse()
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
export const filterCompactedEffect = Effect.fnUntraced(function* (sessionID: SessionID) {
|
export const filterCompactedEffect = Effect.fnUntraced(function* (sessionID: SessionID) {
|
||||||
return filterCompacted(stream(sessionID))
|
return filterCompacted(stream(sessionID))
|
||||||
})
|
})
|
||||||
|
|
||||||
export function fromError(
|
export function fromError(
|
||||||
e: unknown,
|
e: unknown,
|
||||||
ctx: { providerID: ProviderID; aborted?: boolean },
|
ctx: { providerID: ProviderID; aborted?: boolean },
|
||||||
): NonNullable<Assistant["error"]> {
|
): NonNullable<Assistant["error"]> {
|
||||||
switch (true) {
|
switch (true) {
|
||||||
case e instanceof DOMException && e.name === "AbortError":
|
case e instanceof DOMException && e.name === "AbortError":
|
||||||
return new MessageV2.AbortedError(
|
return new AbortedError(
|
||||||
{ message: e.message },
|
{ message: e.message },
|
||||||
{
|
{
|
||||||
cause: e,
|
cause: e,
|
||||||
},
|
},
|
||||||
).toObject()
|
).toObject()
|
||||||
case MessageV2.OutputLengthError.isInstance(e):
|
case OutputLengthError.isInstance(e):
|
||||||
return e
|
return e
|
||||||
case LoadAPIKeyError.isInstance(e):
|
case LoadAPIKeyError.isInstance(e):
|
||||||
return new MessageV2.AuthError(
|
return new AuthError(
|
||||||
{
|
{
|
||||||
providerID: ctx.providerID,
|
providerID: ctx.providerID,
|
||||||
message: e.message,
|
message: e.message,
|
||||||
@@ -971,7 +970,7 @@ export namespace MessageV2 {
|
|||||||
{ cause: e },
|
{ cause: e },
|
||||||
).toObject()
|
).toObject()
|
||||||
case (e as SystemError)?.code === "ECONNRESET":
|
case (e as SystemError)?.code === "ECONNRESET":
|
||||||
return new MessageV2.APIError(
|
return new APIError(
|
||||||
{
|
{
|
||||||
message: "Connection reset by server",
|
message: "Connection reset by server",
|
||||||
isRetryable: true,
|
isRetryable: true,
|
||||||
@@ -985,9 +984,9 @@ export namespace MessageV2 {
|
|||||||
).toObject()
|
).toObject()
|
||||||
case e instanceof Error && (e as FetchDecompressionError).code === "ZlibError":
|
case e instanceof Error && (e as FetchDecompressionError).code === "ZlibError":
|
||||||
if (ctx.aborted) {
|
if (ctx.aborted) {
|
||||||
return new MessageV2.AbortedError({ message: e.message }, { cause: e }).toObject()
|
return new AbortedError({ message: e.message }, { cause: e }).toObject()
|
||||||
}
|
}
|
||||||
return new MessageV2.APIError(
|
return new APIError(
|
||||||
{
|
{
|
||||||
message: "Response decompression failed",
|
message: "Response decompression failed",
|
||||||
isRetryable: true,
|
isRetryable: true,
|
||||||
@@ -1004,7 +1003,7 @@ export namespace MessageV2 {
|
|||||||
error: e,
|
error: e,
|
||||||
})
|
})
|
||||||
if (parsed.type === "context_overflow") {
|
if (parsed.type === "context_overflow") {
|
||||||
return new MessageV2.ContextOverflowError(
|
return new ContextOverflowError(
|
||||||
{
|
{
|
||||||
message: parsed.message,
|
message: parsed.message,
|
||||||
responseBody: parsed.responseBody,
|
responseBody: parsed.responseBody,
|
||||||
@@ -1013,7 +1012,7 @@ export namespace MessageV2 {
|
|||||||
).toObject()
|
).toObject()
|
||||||
}
|
}
|
||||||
|
|
||||||
return new MessageV2.APIError(
|
return new APIError(
|
||||||
{
|
{
|
||||||
message: parsed.message,
|
message: parsed.message,
|
||||||
statusCode: parsed.statusCode,
|
statusCode: parsed.statusCode,
|
||||||
@@ -1031,7 +1030,7 @@ export namespace MessageV2 {
|
|||||||
const parsed = ProviderError.parseStreamError(e)
|
const parsed = ProviderError.parseStreamError(e)
|
||||||
if (parsed) {
|
if (parsed) {
|
||||||
if (parsed.type === "context_overflow") {
|
if (parsed.type === "context_overflow") {
|
||||||
return new MessageV2.ContextOverflowError(
|
return new ContextOverflowError(
|
||||||
{
|
{
|
||||||
message: parsed.message,
|
message: parsed.message,
|
||||||
responseBody: parsed.responseBody,
|
responseBody: parsed.responseBody,
|
||||||
@@ -1039,7 +1038,7 @@ export namespace MessageV2 {
|
|||||||
{ cause: e },
|
{ cause: e },
|
||||||
).toObject()
|
).toObject()
|
||||||
}
|
}
|
||||||
return new MessageV2.APIError(
|
return new APIError(
|
||||||
{
|
{
|
||||||
message: parsed.message,
|
message: parsed.message,
|
||||||
isRetryable: parsed.isRetryable,
|
isRetryable: parsed.isRetryable,
|
||||||
@@ -1053,5 +1052,6 @@ export namespace MessageV2 {
|
|||||||
} catch {}
|
} catch {}
|
||||||
return new NamedError.Unknown({ message: JSON.stringify(e) }, { cause: e }).toObject()
|
return new NamedError.Unknown({ message: JSON.stringify(e) }, { cause: e }).toObject()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export * as MessageV2 from "./message-v2"
|
||||||
|
|||||||
@@ -3,17 +3,16 @@ import { SessionID } from "./schema"
|
|||||||
import { ModelID, ProviderID } from "../provider/schema"
|
import { ModelID, ProviderID } from "../provider/schema"
|
||||||
import { NamedError } from "@opencode-ai/shared/util/error"
|
import { NamedError } from "@opencode-ai/shared/util/error"
|
||||||
|
|
||||||
export namespace Message {
|
export const OutputLengthError = NamedError.create("MessageOutputLengthError", z.object({}))
|
||||||
export const OutputLengthError = NamedError.create("MessageOutputLengthError", z.object({}))
|
export const AuthError = NamedError.create(
|
||||||
export const AuthError = NamedError.create(
|
|
||||||
"ProviderAuthError",
|
"ProviderAuthError",
|
||||||
z.object({
|
z.object({
|
||||||
providerID: z.string(),
|
providerID: z.string(),
|
||||||
message: z.string(),
|
message: z.string(),
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
export const ToolCall = z
|
export const ToolCall = z
|
||||||
.object({
|
.object({
|
||||||
state: z.literal("call"),
|
state: z.literal("call"),
|
||||||
step: z.number().optional(),
|
step: z.number().optional(),
|
||||||
@@ -24,9 +23,9 @@ export namespace Message {
|
|||||||
.meta({
|
.meta({
|
||||||
ref: "ToolCall",
|
ref: "ToolCall",
|
||||||
})
|
})
|
||||||
export type ToolCall = z.infer<typeof ToolCall>
|
export type ToolCall = z.infer<typeof ToolCall>
|
||||||
|
|
||||||
export const ToolPartialCall = z
|
export const ToolPartialCall = z
|
||||||
.object({
|
.object({
|
||||||
state: z.literal("partial-call"),
|
state: z.literal("partial-call"),
|
||||||
step: z.number().optional(),
|
step: z.number().optional(),
|
||||||
@@ -37,9 +36,9 @@ export namespace Message {
|
|||||||
.meta({
|
.meta({
|
||||||
ref: "ToolPartialCall",
|
ref: "ToolPartialCall",
|
||||||
})
|
})
|
||||||
export type ToolPartialCall = z.infer<typeof ToolPartialCall>
|
export type ToolPartialCall = z.infer<typeof ToolPartialCall>
|
||||||
|
|
||||||
export const ToolResult = z
|
export const ToolResult = z
|
||||||
.object({
|
.object({
|
||||||
state: z.literal("result"),
|
state: z.literal("result"),
|
||||||
step: z.number().optional(),
|
step: z.number().optional(),
|
||||||
@@ -51,14 +50,14 @@ export namespace Message {
|
|||||||
.meta({
|
.meta({
|
||||||
ref: "ToolResult",
|
ref: "ToolResult",
|
||||||
})
|
})
|
||||||
export type ToolResult = z.infer<typeof ToolResult>
|
export type ToolResult = z.infer<typeof ToolResult>
|
||||||
|
|
||||||
export const ToolInvocation = z.discriminatedUnion("state", [ToolCall, ToolPartialCall, ToolResult]).meta({
|
export const ToolInvocation = z.discriminatedUnion("state", [ToolCall, ToolPartialCall, ToolResult]).meta({
|
||||||
ref: "ToolInvocation",
|
ref: "ToolInvocation",
|
||||||
})
|
})
|
||||||
export type ToolInvocation = z.infer<typeof ToolInvocation>
|
export type ToolInvocation = z.infer<typeof ToolInvocation>
|
||||||
|
|
||||||
export const TextPart = z
|
export const TextPart = z
|
||||||
.object({
|
.object({
|
||||||
type: z.literal("text"),
|
type: z.literal("text"),
|
||||||
text: z.string(),
|
text: z.string(),
|
||||||
@@ -66,9 +65,9 @@ export namespace Message {
|
|||||||
.meta({
|
.meta({
|
||||||
ref: "TextPart",
|
ref: "TextPart",
|
||||||
})
|
})
|
||||||
export type TextPart = z.infer<typeof TextPart>
|
export type TextPart = z.infer<typeof TextPart>
|
||||||
|
|
||||||
export const ReasoningPart = z
|
export const ReasoningPart = z
|
||||||
.object({
|
.object({
|
||||||
type: z.literal("reasoning"),
|
type: z.literal("reasoning"),
|
||||||
text: z.string(),
|
text: z.string(),
|
||||||
@@ -77,9 +76,9 @@ export namespace Message {
|
|||||||
.meta({
|
.meta({
|
||||||
ref: "ReasoningPart",
|
ref: "ReasoningPart",
|
||||||
})
|
})
|
||||||
export type ReasoningPart = z.infer<typeof ReasoningPart>
|
export type ReasoningPart = z.infer<typeof ReasoningPart>
|
||||||
|
|
||||||
export const ToolInvocationPart = z
|
export const ToolInvocationPart = z
|
||||||
.object({
|
.object({
|
||||||
type: z.literal("tool-invocation"),
|
type: z.literal("tool-invocation"),
|
||||||
toolInvocation: ToolInvocation,
|
toolInvocation: ToolInvocation,
|
||||||
@@ -87,9 +86,9 @@ export namespace Message {
|
|||||||
.meta({
|
.meta({
|
||||||
ref: "ToolInvocationPart",
|
ref: "ToolInvocationPart",
|
||||||
})
|
})
|
||||||
export type ToolInvocationPart = z.infer<typeof ToolInvocationPart>
|
export type ToolInvocationPart = z.infer<typeof ToolInvocationPart>
|
||||||
|
|
||||||
export const SourceUrlPart = z
|
export const SourceUrlPart = z
|
||||||
.object({
|
.object({
|
||||||
type: z.literal("source-url"),
|
type: z.literal("source-url"),
|
||||||
sourceId: z.string(),
|
sourceId: z.string(),
|
||||||
@@ -100,9 +99,9 @@ export namespace Message {
|
|||||||
.meta({
|
.meta({
|
||||||
ref: "SourceUrlPart",
|
ref: "SourceUrlPart",
|
||||||
})
|
})
|
||||||
export type SourceUrlPart = z.infer<typeof SourceUrlPart>
|
export type SourceUrlPart = z.infer<typeof SourceUrlPart>
|
||||||
|
|
||||||
export const FilePart = z
|
export const FilePart = z
|
||||||
.object({
|
.object({
|
||||||
type: z.literal("file"),
|
type: z.literal("file"),
|
||||||
mediaType: z.string(),
|
mediaType: z.string(),
|
||||||
@@ -112,25 +111,25 @@ export namespace Message {
|
|||||||
.meta({
|
.meta({
|
||||||
ref: "FilePart",
|
ref: "FilePart",
|
||||||
})
|
})
|
||||||
export type FilePart = z.infer<typeof FilePart>
|
export type FilePart = z.infer<typeof FilePart>
|
||||||
|
|
||||||
export const StepStartPart = z
|
export const StepStartPart = z
|
||||||
.object({
|
.object({
|
||||||
type: z.literal("step-start"),
|
type: z.literal("step-start"),
|
||||||
})
|
})
|
||||||
.meta({
|
.meta({
|
||||||
ref: "StepStartPart",
|
ref: "StepStartPart",
|
||||||
})
|
})
|
||||||
export type StepStartPart = z.infer<typeof StepStartPart>
|
export type StepStartPart = z.infer<typeof StepStartPart>
|
||||||
|
|
||||||
export const MessagePart = z
|
export const MessagePart = z
|
||||||
.discriminatedUnion("type", [TextPart, ReasoningPart, ToolInvocationPart, SourceUrlPart, FilePart, StepStartPart])
|
.discriminatedUnion("type", [TextPart, ReasoningPart, ToolInvocationPart, SourceUrlPart, FilePart, StepStartPart])
|
||||||
.meta({
|
.meta({
|
||||||
ref: "MessagePart",
|
ref: "MessagePart",
|
||||||
})
|
})
|
||||||
export type MessagePart = z.infer<typeof MessagePart>
|
export type MessagePart = z.infer<typeof MessagePart>
|
||||||
|
|
||||||
export const Info = z
|
export const Info = z
|
||||||
.object({
|
.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
role: z.enum(["user", "assistant"]),
|
role: z.enum(["user", "assistant"]),
|
||||||
@@ -187,5 +186,6 @@ export namespace Message {
|
|||||||
.meta({
|
.meta({
|
||||||
ref: "Message",
|
ref: "Message",
|
||||||
})
|
})
|
||||||
export type Info = z.infer<typeof Info>
|
export type Info = z.infer<typeof Info>
|
||||||
}
|
|
||||||
|
export * as Message from "./message"
|
||||||
|
|||||||
@@ -21,15 +21,14 @@ import { errorMessage } from "@/util/error"
|
|||||||
import { Log } from "@/util"
|
import { Log } from "@/util"
|
||||||
import { isRecord } from "@/util/record"
|
import { isRecord } from "@/util/record"
|
||||||
|
|
||||||
export namespace SessionProcessor {
|
const DOOM_LOOP_THRESHOLD = 3
|
||||||
const DOOM_LOOP_THRESHOLD = 3
|
const log = Log.create({ service: "session.processor" })
|
||||||
const log = Log.create({ service: "session.processor" })
|
|
||||||
|
|
||||||
export type Result = "compact" | "stop" | "continue"
|
export type Result = "compact" | "stop" | "continue"
|
||||||
|
|
||||||
export type Event = LLM.Event
|
export type Event = LLM.Event
|
||||||
|
|
||||||
export interface Handle {
|
export interface Handle {
|
||||||
readonly message: MessageV2.Assistant
|
readonly message: MessageV2.Assistant
|
||||||
readonly updateToolCall: (
|
readonly updateToolCall: (
|
||||||
toolCallID: string,
|
toolCallID: string,
|
||||||
@@ -45,26 +44,26 @@ export namespace SessionProcessor {
|
|||||||
},
|
},
|
||||||
) => Effect.Effect<void>
|
) => Effect.Effect<void>
|
||||||
readonly process: (streamInput: LLM.StreamInput) => Effect.Effect<Result>
|
readonly process: (streamInput: LLM.StreamInput) => Effect.Effect<Result>
|
||||||
}
|
}
|
||||||
|
|
||||||
type Input = {
|
type Input = {
|
||||||
assistantMessage: MessageV2.Assistant
|
assistantMessage: MessageV2.Assistant
|
||||||
sessionID: SessionID
|
sessionID: SessionID
|
||||||
model: Provider.Model
|
model: Provider.Model
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Interface {
|
export interface Interface {
|
||||||
readonly create: (input: Input) => Effect.Effect<Handle>
|
readonly create: (input: Input) => Effect.Effect<Handle>
|
||||||
}
|
}
|
||||||
|
|
||||||
type ToolCall = {
|
type ToolCall = {
|
||||||
partID: MessageV2.ToolPart["id"]
|
partID: MessageV2.ToolPart["id"]
|
||||||
messageID: MessageV2.ToolPart["messageID"]
|
messageID: MessageV2.ToolPart["messageID"]
|
||||||
sessionID: MessageV2.ToolPart["sessionID"]
|
sessionID: MessageV2.ToolPart["sessionID"]
|
||||||
done: Deferred.Deferred<void>
|
done: Deferred.Deferred<void>
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ProcessorContext extends Input {
|
interface ProcessorContext extends Input {
|
||||||
toolcalls: Record<string, ToolCall>
|
toolcalls: Record<string, ToolCall>
|
||||||
shouldBreak: boolean
|
shouldBreak: boolean
|
||||||
snapshot: string | undefined
|
snapshot: string | undefined
|
||||||
@@ -72,13 +71,13 @@ export namespace SessionProcessor {
|
|||||||
needsCompaction: boolean
|
needsCompaction: boolean
|
||||||
currentText: MessageV2.TextPart | undefined
|
currentText: MessageV2.TextPart | undefined
|
||||||
reasoningMap: Record<string, MessageV2.ReasoningPart>
|
reasoningMap: Record<string, MessageV2.ReasoningPart>
|
||||||
}
|
}
|
||||||
|
|
||||||
type StreamEvent = Event
|
type StreamEvent = Event
|
||||||
|
|
||||||
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionProcessor") {}
|
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionProcessor") {}
|
||||||
|
|
||||||
export const layer: Layer.Layer<
|
export const layer: Layer.Layer<
|
||||||
Service,
|
Service,
|
||||||
never,
|
never,
|
||||||
| Session.Service
|
| Session.Service
|
||||||
@@ -91,7 +90,7 @@ export namespace SessionProcessor {
|
|||||||
| Plugin.Service
|
| Plugin.Service
|
||||||
| SessionSummary.Service
|
| SessionSummary.Service
|
||||||
| SessionStatus.Service
|
| SessionStatus.Service
|
||||||
> = Layer.effect(
|
> = Layer.effect(
|
||||||
Service,
|
Service,
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
const session = yield* Session.Service
|
const session = yield* Session.Service
|
||||||
@@ -600,9 +599,9 @@ export namespace SessionProcessor {
|
|||||||
|
|
||||||
return Service.of({ create })
|
return Service.of({ create })
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
export const defaultLayer = Layer.suspend(() =>
|
export const defaultLayer = Layer.suspend(() =>
|
||||||
layer.pipe(
|
layer.pipe(
|
||||||
Layer.provide(Session.defaultLayer),
|
Layer.provide(Session.defaultLayer),
|
||||||
Layer.provide(Snapshot.defaultLayer),
|
Layer.provide(Snapshot.defaultLayer),
|
||||||
@@ -615,5 +614,6 @@ export namespace SessionProcessor {
|
|||||||
Layer.provide(Bus.layer),
|
Layer.provide(Bus.layer),
|
||||||
Layer.provide(Config.defaultLayer),
|
Layer.provide(Config.defaultLayer),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
export * as SessionProcessor from "./processor"
|
||||||
|
|||||||
@@ -64,22 +64,21 @@ IMPORTANT:
|
|||||||
|
|
||||||
const STRUCTURED_OUTPUT_SYSTEM_PROMPT = `IMPORTANT: The user has requested structured output. You MUST use the StructuredOutput tool to provide your final response. Do NOT respond with plain text - you MUST call the StructuredOutput tool with your answer formatted according to the schema.`
|
const STRUCTURED_OUTPUT_SYSTEM_PROMPT = `IMPORTANT: The user has requested structured output. You MUST use the StructuredOutput tool to provide your final response. Do NOT respond with plain text - you MUST call the StructuredOutput tool with your answer formatted according to the schema.`
|
||||||
|
|
||||||
export namespace SessionPrompt {
|
const log = Log.create({ service: "session.prompt" })
|
||||||
const log = Log.create({ service: "session.prompt" })
|
const elog = EffectLogger.create({ service: "session.prompt" })
|
||||||
const elog = EffectLogger.create({ service: "session.prompt" })
|
|
||||||
|
|
||||||
export interface Interface {
|
export interface Interface {
|
||||||
readonly cancel: (sessionID: SessionID) => Effect.Effect<void>
|
readonly cancel: (sessionID: SessionID) => Effect.Effect<void>
|
||||||
readonly prompt: (input: PromptInput) => Effect.Effect<MessageV2.WithParts>
|
readonly prompt: (input: PromptInput) => Effect.Effect<MessageV2.WithParts>
|
||||||
readonly loop: (input: z.infer<typeof LoopInput>) => Effect.Effect<MessageV2.WithParts>
|
readonly loop: (input: z.infer<typeof LoopInput>) => Effect.Effect<MessageV2.WithParts>
|
||||||
readonly shell: (input: ShellInput) => Effect.Effect<MessageV2.WithParts>
|
readonly shell: (input: ShellInput) => Effect.Effect<MessageV2.WithParts>
|
||||||
readonly command: (input: CommandInput) => Effect.Effect<MessageV2.WithParts>
|
readonly command: (input: CommandInput) => Effect.Effect<MessageV2.WithParts>
|
||||||
readonly resolvePromptParts: (template: string) => Effect.Effect<PromptInput["parts"]>
|
readonly resolvePromptParts: (template: string) => Effect.Effect<PromptInput["parts"]>
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionPrompt") {}
|
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionPrompt") {}
|
||||||
|
|
||||||
export const layer = Layer.effect(
|
export const layer = Layer.effect(
|
||||||
Service,
|
Service,
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
const bus = yield* Bus.Service
|
const bus = yield* Bus.Service
|
||||||
@@ -1677,9 +1676,9 @@ NOTE: At any point in time through this workflow you should feel free to ask the
|
|||||||
resolvePromptParts,
|
resolvePromptParts,
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
export const defaultLayer = Layer.suspend(() =>
|
export const defaultLayer = Layer.suspend(() =>
|
||||||
layer.pipe(
|
layer.pipe(
|
||||||
Layer.provide(SessionRunState.defaultLayer),
|
Layer.provide(SessionRunState.defaultLayer),
|
||||||
Layer.provide(SessionStatus.defaultLayer),
|
Layer.provide(SessionStatus.defaultLayer),
|
||||||
@@ -1709,8 +1708,8 @@ NOTE: At any point in time through this workflow you should feel free to ask the
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
export const PromptInput = z.object({
|
export const PromptInput = z.object({
|
||||||
sessionID: SessionID.zod,
|
sessionID: SessionID.zod,
|
||||||
messageID: MessageID.zod.optional(),
|
messageID: MessageID.zod.optional(),
|
||||||
model: z
|
model: z
|
||||||
@@ -1774,14 +1773,14 @@ NOTE: At any point in time through this workflow you should feel free to ask the
|
|||||||
}),
|
}),
|
||||||
]),
|
]),
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
export type PromptInput = z.infer<typeof PromptInput>
|
export type PromptInput = z.infer<typeof PromptInput>
|
||||||
|
|
||||||
export const LoopInput = z.object({
|
export const LoopInput = z.object({
|
||||||
sessionID: SessionID.zod,
|
sessionID: SessionID.zod,
|
||||||
})
|
})
|
||||||
|
|
||||||
export const ShellInput = z.object({
|
export const ShellInput = z.object({
|
||||||
sessionID: SessionID.zod,
|
sessionID: SessionID.zod,
|
||||||
messageID: MessageID.zod.optional(),
|
messageID: MessageID.zod.optional(),
|
||||||
agent: z.string(),
|
agent: z.string(),
|
||||||
@@ -1792,10 +1791,10 @@ NOTE: At any point in time through this workflow you should feel free to ask the
|
|||||||
})
|
})
|
||||||
.optional(),
|
.optional(),
|
||||||
command: z.string(),
|
command: z.string(),
|
||||||
})
|
})
|
||||||
export type ShellInput = z.infer<typeof ShellInput>
|
export type ShellInput = z.infer<typeof ShellInput>
|
||||||
|
|
||||||
export const CommandInput = z.object({
|
export const CommandInput = z.object({
|
||||||
messageID: MessageID.zod.optional(),
|
messageID: MessageID.zod.optional(),
|
||||||
sessionID: SessionID.zod,
|
sessionID: SessionID.zod,
|
||||||
agent: z.string().optional(),
|
agent: z.string().optional(),
|
||||||
@@ -1815,14 +1814,14 @@ NOTE: At any point in time through this workflow you should feel free to ask the
|
|||||||
]),
|
]),
|
||||||
)
|
)
|
||||||
.optional(),
|
.optional(),
|
||||||
})
|
})
|
||||||
export type CommandInput = z.infer<typeof CommandInput>
|
export type CommandInput = z.infer<typeof CommandInput>
|
||||||
|
|
||||||
/** @internal Exported for testing */
|
/** @internal Exported for testing */
|
||||||
export function createStructuredOutputTool(input: {
|
export function createStructuredOutputTool(input: {
|
||||||
schema: Record<string, any>
|
schema: Record<string, any>
|
||||||
onSuccess: (output: unknown) => void
|
onSuccess: (output: unknown) => void
|
||||||
}): AITool {
|
}): AITool {
|
||||||
// Remove $schema property if present (not needed for tool input)
|
// Remove $schema property if present (not needed for tool input)
|
||||||
const { $schema: _, ...toolSchema } = input.schema
|
const { $schema: _, ...toolSchema } = input.schema
|
||||||
|
|
||||||
@@ -1845,10 +1844,11 @@ NOTE: At any point in time through this workflow you should feel free to ask the
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
|
||||||
const bashRegex = /!`([^`]+)`/g
|
|
||||||
// Match [Image N] as single token, quoted strings, or non-space sequences
|
|
||||||
const argsRegex = /(?:\[Image\s+\d+\]|"[^"]*"|'[^']*'|[^\s"']+)/gi
|
|
||||||
const placeholderRegex = /\$(\d+)/g
|
|
||||||
const quoteTrimRegex = /^["']|["']$/g
|
|
||||||
}
|
}
|
||||||
|
const bashRegex = /!`([^`]+)`/g
|
||||||
|
// Match [Image N] as single token, quoted strings, or non-space sequences
|
||||||
|
const argsRegex = /(?:\[Image\s+\d+\]|"[^"]*"|'[^']*'|[^\s"']+)/gi
|
||||||
|
const placeholderRegex = /\$(\d+)/g
|
||||||
|
const quoteTrimRegex = /^["']|["']$/g
|
||||||
|
|
||||||
|
export * as SessionPrompt from "./prompt"
|
||||||
|
|||||||
@@ -3,23 +3,22 @@ import { Cause, Clock, Duration, Effect, Schedule } from "effect"
|
|||||||
import { MessageV2 } from "./message-v2"
|
import { MessageV2 } from "./message-v2"
|
||||||
import { iife } from "@/util/iife"
|
import { iife } from "@/util/iife"
|
||||||
|
|
||||||
export namespace SessionRetry {
|
export type Err = ReturnType<NamedError["toObject"]>
|
||||||
export type Err = ReturnType<NamedError["toObject"]>
|
|
||||||
|
|
||||||
// This exported message is shared with the TUI upsell detector. Matching on a
|
// This exported message is shared with the TUI upsell detector. Matching on a
|
||||||
// literal error string kind of sucks, but it is the simplest for now.
|
// literal error string kind of sucks, but it is the simplest for now.
|
||||||
export const GO_UPSELL_MESSAGE = "Free usage exceeded, subscribe to Go https://opencode.ai/go"
|
export const GO_UPSELL_MESSAGE = "Free usage exceeded, subscribe to Go https://opencode.ai/go"
|
||||||
|
|
||||||
export const RETRY_INITIAL_DELAY = 2000
|
export const RETRY_INITIAL_DELAY = 2000
|
||||||
export const RETRY_BACKOFF_FACTOR = 2
|
export const RETRY_BACKOFF_FACTOR = 2
|
||||||
export const RETRY_MAX_DELAY_NO_HEADERS = 30_000 // 30 seconds
|
export const RETRY_MAX_DELAY_NO_HEADERS = 30_000 // 30 seconds
|
||||||
export const RETRY_MAX_DELAY = 2_147_483_647 // max 32-bit signed integer for setTimeout
|
export const RETRY_MAX_DELAY = 2_147_483_647 // max 32-bit signed integer for setTimeout
|
||||||
|
|
||||||
function cap(ms: number) {
|
function cap(ms: number) {
|
||||||
return Math.min(ms, RETRY_MAX_DELAY)
|
return Math.min(ms, RETRY_MAX_DELAY)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function delay(attempt: number, error?: MessageV2.APIError) {
|
export function delay(attempt: number, error?: MessageV2.APIError) {
|
||||||
if (error) {
|
if (error) {
|
||||||
const headers = error.data.responseHeaders
|
const headers = error.data.responseHeaders
|
||||||
if (headers) {
|
if (headers) {
|
||||||
@@ -50,9 +49,9 @@ export namespace SessionRetry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return cap(Math.min(RETRY_INITIAL_DELAY * Math.pow(RETRY_BACKOFF_FACTOR, attempt - 1), RETRY_MAX_DELAY_NO_HEADERS))
|
return cap(Math.min(RETRY_INITIAL_DELAY * Math.pow(RETRY_BACKOFF_FACTOR, attempt - 1), RETRY_MAX_DELAY_NO_HEADERS))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function retryable(error: Err) {
|
export function retryable(error: Err) {
|
||||||
// context overflow errors should not be retried
|
// context overflow errors should not be retried
|
||||||
if (MessageV2.ContextOverflowError.isInstance(error)) return undefined
|
if (MessageV2.ContextOverflowError.isInstance(error)) return undefined
|
||||||
if (MessageV2.APIError.isInstance(error)) {
|
if (MessageV2.APIError.isInstance(error)) {
|
||||||
@@ -102,12 +101,12 @@ export namespace SessionRetry {
|
|||||||
return "Rate Limited"
|
return "Rate Limited"
|
||||||
}
|
}
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
export function policy(opts: {
|
export function policy(opts: {
|
||||||
parse: (error: unknown) => Err
|
parse: (error: unknown) => Err
|
||||||
set: (input: { attempt: number; message: string; next: number }) => Effect.Effect<void>
|
set: (input: { attempt: number; message: string; next: number }) => Effect.Effect<void>
|
||||||
}) {
|
}) {
|
||||||
return Schedule.fromStepWithMetadata(
|
return Schedule.fromStepWithMetadata(
|
||||||
Effect.succeed((meta: Schedule.InputMetadata<unknown>) => {
|
Effect.succeed((meta: Schedule.InputMetadata<unknown>) => {
|
||||||
const error = opts.parse(meta.input)
|
const error = opts.parse(meta.input)
|
||||||
@@ -121,5 +120,6 @@ export namespace SessionRetry {
|
|||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export * as SessionRetry from "./retry"
|
||||||
|
|||||||
@@ -11,25 +11,24 @@ import { SessionID, MessageID, PartID } from "./schema"
|
|||||||
import { SessionRunState } from "./run-state"
|
import { SessionRunState } from "./run-state"
|
||||||
import { SessionSummary } from "./summary"
|
import { SessionSummary } from "./summary"
|
||||||
|
|
||||||
export namespace SessionRevert {
|
const log = Log.create({ service: "session.revert" })
|
||||||
const log = Log.create({ service: "session.revert" })
|
|
||||||
|
|
||||||
export const RevertInput = z.object({
|
export const RevertInput = z.object({
|
||||||
sessionID: SessionID.zod,
|
sessionID: SessionID.zod,
|
||||||
messageID: MessageID.zod,
|
messageID: MessageID.zod,
|
||||||
partID: PartID.zod.optional(),
|
partID: PartID.zod.optional(),
|
||||||
})
|
})
|
||||||
export type RevertInput = z.infer<typeof RevertInput>
|
export type RevertInput = z.infer<typeof RevertInput>
|
||||||
|
|
||||||
export interface Interface {
|
export interface Interface {
|
||||||
readonly revert: (input: RevertInput) => Effect.Effect<Session.Info>
|
readonly revert: (input: RevertInput) => Effect.Effect<Session.Info>
|
||||||
readonly unrevert: (input: { sessionID: SessionID }) => Effect.Effect<Session.Info>
|
readonly unrevert: (input: { sessionID: SessionID }) => Effect.Effect<Session.Info>
|
||||||
readonly cleanup: (session: Session.Info) => Effect.Effect<void>
|
readonly cleanup: (session: Session.Info) => Effect.Effect<void>
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionRevert") {}
|
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionRevert") {}
|
||||||
|
|
||||||
export const layer = Layer.effect(
|
export const layer = Layer.effect(
|
||||||
Service,
|
Service,
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
const sessions = yield* Session.Service
|
const sessions = yield* Session.Service
|
||||||
@@ -146,9 +145,9 @@ export namespace SessionRevert {
|
|||||||
|
|
||||||
return Service.of({ revert, unrevert, cleanup })
|
return Service.of({ revert, unrevert, cleanup })
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
export const defaultLayer = Layer.suspend(() =>
|
export const defaultLayer = Layer.suspend(() =>
|
||||||
layer.pipe(
|
layer.pipe(
|
||||||
Layer.provide(SessionRunState.defaultLayer),
|
Layer.provide(SessionRunState.defaultLayer),
|
||||||
Layer.provide(Session.defaultLayer),
|
Layer.provide(Session.defaultLayer),
|
||||||
@@ -157,5 +156,6 @@ export namespace SessionRevert {
|
|||||||
Layer.provide(Bus.layer),
|
Layer.provide(Bus.layer),
|
||||||
Layer.provide(SessionSummary.defaultLayer),
|
Layer.provide(SessionSummary.defaultLayer),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
export * as SessionRevert from "./revert"
|
||||||
|
|||||||
@@ -6,8 +6,7 @@ import { MessageV2 } from "./message-v2"
|
|||||||
import { SessionID } from "./schema"
|
import { SessionID } from "./schema"
|
||||||
import { SessionStatus } from "./status"
|
import { SessionStatus } from "./status"
|
||||||
|
|
||||||
export namespace SessionRunState {
|
export interface Interface {
|
||||||
export interface Interface {
|
|
||||||
readonly assertNotBusy: (sessionID: SessionID) => Effect.Effect<void>
|
readonly assertNotBusy: (sessionID: SessionID) => Effect.Effect<void>
|
||||||
readonly cancel: (sessionID: SessionID) => Effect.Effect<void>
|
readonly cancel: (sessionID: SessionID) => Effect.Effect<void>
|
||||||
readonly ensureRunning: (
|
readonly ensureRunning: (
|
||||||
@@ -20,11 +19,11 @@ export namespace SessionRunState {
|
|||||||
onInterrupt: Effect.Effect<MessageV2.WithParts>,
|
onInterrupt: Effect.Effect<MessageV2.WithParts>,
|
||||||
work: Effect.Effect<MessageV2.WithParts>,
|
work: Effect.Effect<MessageV2.WithParts>,
|
||||||
) => Effect.Effect<MessageV2.WithParts>
|
) => Effect.Effect<MessageV2.WithParts>
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionRunState") {}
|
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionRunState") {}
|
||||||
|
|
||||||
export const layer = Layer.effect(
|
export const layer = Layer.effect(
|
||||||
Service,
|
Service,
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
const status = yield* SessionStatus.Service
|
const status = yield* SessionStatus.Service
|
||||||
@@ -102,7 +101,8 @@ export namespace SessionRunState {
|
|||||||
|
|
||||||
return Service.of({ assertNotBusy, cancel, ensureRunning, startShell })
|
return Service.of({ assertNotBusy, cancel, ensureRunning, startShell })
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
export const defaultLayer = layer.pipe(Layer.provide(SessionStatus.defaultLayer))
|
export const defaultLayer = layer.pipe(Layer.provide(SessionStatus.defaultLayer))
|
||||||
}
|
|
||||||
|
export * as SessionRunState from "./run-state"
|
||||||
|
|||||||
@@ -5,8 +5,7 @@ import { SessionID } from "./schema"
|
|||||||
import { Effect, Layer, Context } from "effect"
|
import { Effect, Layer, Context } from "effect"
|
||||||
import z from "zod"
|
import z from "zod"
|
||||||
|
|
||||||
export namespace SessionStatus {
|
export const Info = z
|
||||||
export const Info = z
|
|
||||||
.union([
|
.union([
|
||||||
z.object({
|
z.object({
|
||||||
type: z.literal("idle"),
|
type: z.literal("idle"),
|
||||||
@@ -24,9 +23,9 @@ export namespace SessionStatus {
|
|||||||
.meta({
|
.meta({
|
||||||
ref: "SessionStatus",
|
ref: "SessionStatus",
|
||||||
})
|
})
|
||||||
export type Info = z.infer<typeof Info>
|
export type Info = z.infer<typeof Info>
|
||||||
|
|
||||||
export const Event = {
|
export const Event = {
|
||||||
Status: BusEvent.define(
|
Status: BusEvent.define(
|
||||||
"session.status",
|
"session.status",
|
||||||
z.object({
|
z.object({
|
||||||
@@ -41,17 +40,17 @@ export namespace SessionStatus {
|
|||||||
sessionID: SessionID.zod,
|
sessionID: SessionID.zod,
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Interface {
|
export interface Interface {
|
||||||
readonly get: (sessionID: SessionID) => Effect.Effect<Info>
|
readonly get: (sessionID: SessionID) => Effect.Effect<Info>
|
||||||
readonly list: () => Effect.Effect<Map<SessionID, Info>>
|
readonly list: () => Effect.Effect<Map<SessionID, Info>>
|
||||||
readonly set: (sessionID: SessionID, status: Info) => Effect.Effect<void>
|
readonly set: (sessionID: SessionID, status: Info) => Effect.Effect<void>
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionStatus") {}
|
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionStatus") {}
|
||||||
|
|
||||||
export const layer = Layer.effect(
|
export const layer = Layer.effect(
|
||||||
Service,
|
Service,
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
const bus = yield* Bus.Service
|
const bus = yield* Bus.Service
|
||||||
@@ -82,7 +81,8 @@ export namespace SessionStatus {
|
|||||||
|
|
||||||
return Service.of({ get, list, set })
|
return Service.of({ get, list, set })
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
export const defaultLayer = layer.pipe(Layer.provide(Bus.layer))
|
export const defaultLayer = layer.pipe(Layer.provide(Bus.layer))
|
||||||
}
|
|
||||||
|
export * as SessionStatus from "./status"
|
||||||
|
|||||||
@@ -7,8 +7,7 @@ import * as Session from "./session"
|
|||||||
import { MessageV2 } from "./message-v2"
|
import { MessageV2 } from "./message-v2"
|
||||||
import { SessionID, MessageID } from "./schema"
|
import { SessionID, MessageID } from "./schema"
|
||||||
|
|
||||||
export namespace SessionSummary {
|
function unquoteGitPath(input: string) {
|
||||||
function unquoteGitPath(input: string) {
|
|
||||||
if (!input.startsWith('"')) return input
|
if (!input.startsWith('"')) return input
|
||||||
if (!input.endsWith('"')) return input
|
if (!input.endsWith('"')) return input
|
||||||
const body = input.slice(1, -1)
|
const body = input.slice(1, -1)
|
||||||
@@ -62,17 +61,17 @@ export namespace SessionSummary {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return Buffer.from(bytes).toString()
|
return Buffer.from(bytes).toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Interface {
|
export interface Interface {
|
||||||
readonly summarize: (input: { sessionID: SessionID; messageID: MessageID }) => Effect.Effect<void>
|
readonly summarize: (input: { sessionID: SessionID; messageID: MessageID }) => Effect.Effect<void>
|
||||||
readonly diff: (input: { sessionID: SessionID; messageID?: MessageID }) => Effect.Effect<Snapshot.FileDiff[]>
|
readonly diff: (input: { sessionID: SessionID; messageID?: MessageID }) => Effect.Effect<Snapshot.FileDiff[]>
|
||||||
readonly computeDiff: (input: { messages: MessageV2.WithParts[] }) => Effect.Effect<Snapshot.FileDiff[]>
|
readonly computeDiff: (input: { messages: MessageV2.WithParts[] }) => Effect.Effect<Snapshot.FileDiff[]>
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionSummary") {}
|
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionSummary") {}
|
||||||
|
|
||||||
export const layer = Layer.effect(
|
export const layer = Layer.effect(
|
||||||
Service,
|
Service,
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
const sessions = yield* Session.Service
|
const sessions = yield* Session.Service
|
||||||
@@ -147,19 +146,20 @@ export namespace SessionSummary {
|
|||||||
|
|
||||||
return Service.of({ summarize, diff, computeDiff })
|
return Service.of({ summarize, diff, computeDiff })
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
export const defaultLayer = Layer.suspend(() =>
|
export const defaultLayer = Layer.suspend(() =>
|
||||||
layer.pipe(
|
layer.pipe(
|
||||||
Layer.provide(Session.defaultLayer),
|
Layer.provide(Session.defaultLayer),
|
||||||
Layer.provide(Snapshot.defaultLayer),
|
Layer.provide(Snapshot.defaultLayer),
|
||||||
Layer.provide(Storage.defaultLayer),
|
Layer.provide(Storage.defaultLayer),
|
||||||
Layer.provide(Bus.layer),
|
Layer.provide(Bus.layer),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
export const DiffInput = z.object({
|
export const DiffInput = z.object({
|
||||||
sessionID: SessionID.zod,
|
sessionID: SessionID.zod,
|
||||||
messageID: MessageID.zod.optional(),
|
messageID: MessageID.zod.optional(),
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
export * as SessionSummary from "./summary"
|
||||||
|
|||||||
@@ -16,8 +16,7 @@ import type { Agent } from "@/agent/agent"
|
|||||||
import { Permission } from "@/permission"
|
import { Permission } from "@/permission"
|
||||||
import { Skill } from "@/skill"
|
import { Skill } from "@/skill"
|
||||||
|
|
||||||
export namespace SystemPrompt {
|
export function provider(model: Provider.Model) {
|
||||||
export function provider(model: Provider.Model) {
|
|
||||||
if (model.api.id.includes("gpt-4") || model.api.id.includes("o1") || model.api.id.includes("o3"))
|
if (model.api.id.includes("gpt-4") || model.api.id.includes("o1") || model.api.id.includes("o3"))
|
||||||
return [PROMPT_BEAST]
|
return [PROMPT_BEAST]
|
||||||
if (model.api.id.includes("gpt")) {
|
if (model.api.id.includes("gpt")) {
|
||||||
@@ -31,16 +30,16 @@ export namespace SystemPrompt {
|
|||||||
if (model.api.id.toLowerCase().includes("trinity")) return [PROMPT_TRINITY]
|
if (model.api.id.toLowerCase().includes("trinity")) return [PROMPT_TRINITY]
|
||||||
if (model.api.id.toLowerCase().includes("kimi")) return [PROMPT_KIMI]
|
if (model.api.id.toLowerCase().includes("kimi")) return [PROMPT_KIMI]
|
||||||
return [PROMPT_DEFAULT]
|
return [PROMPT_DEFAULT]
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Interface {
|
export interface Interface {
|
||||||
readonly environment: (model: Provider.Model) => string[]
|
readonly environment: (model: Provider.Model) => string[]
|
||||||
readonly skills: (agent: Agent.Info) => Effect.Effect<string | undefined>
|
readonly skills: (agent: Agent.Info) => Effect.Effect<string | undefined>
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Service extends Context.Service<Service, Interface>()("@opencode/SystemPrompt") {}
|
export class Service extends Context.Service<Service, Interface>()("@opencode/SystemPrompt") {}
|
||||||
|
|
||||||
export const layer = Layer.effect(
|
export const layer = Layer.effect(
|
||||||
Service,
|
Service,
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
const skill = yield* Skill.Service
|
const skill = yield* Skill.Service
|
||||||
@@ -78,7 +77,8 @@ export namespace SystemPrompt {
|
|||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
export const defaultLayer = layer.pipe(Layer.provide(Skill.defaultLayer))
|
export const defaultLayer = layer.pipe(Layer.provide(Skill.defaultLayer))
|
||||||
}
|
|
||||||
|
export * as SystemPrompt from "./system"
|
||||||
|
|||||||
@@ -6,17 +6,16 @@ import z from "zod"
|
|||||||
import { Database, eq, asc } from "../storage"
|
import { Database, eq, asc } from "../storage"
|
||||||
import { TodoTable } from "./session.sql"
|
import { TodoTable } from "./session.sql"
|
||||||
|
|
||||||
export namespace Todo {
|
export const Info = z
|
||||||
export const Info = z
|
|
||||||
.object({
|
.object({
|
||||||
content: z.string().describe("Brief description of the task"),
|
content: z.string().describe("Brief description of the task"),
|
||||||
status: z.string().describe("Current status of the task: pending, in_progress, completed, cancelled"),
|
status: z.string().describe("Current status of the task: pending, in_progress, completed, cancelled"),
|
||||||
priority: z.string().describe("Priority level of the task: high, medium, low"),
|
priority: z.string().describe("Priority level of the task: high, medium, low"),
|
||||||
})
|
})
|
||||||
.meta({ ref: "Todo" })
|
.meta({ ref: "Todo" })
|
||||||
export type Info = z.infer<typeof Info>
|
export type Info = z.infer<typeof Info>
|
||||||
|
|
||||||
export const Event = {
|
export const Event = {
|
||||||
Updated: BusEvent.define(
|
Updated: BusEvent.define(
|
||||||
"todo.updated",
|
"todo.updated",
|
||||||
z.object({
|
z.object({
|
||||||
@@ -24,16 +23,16 @@ export namespace Todo {
|
|||||||
todos: z.array(Info),
|
todos: z.array(Info),
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Interface {
|
export interface Interface {
|
||||||
readonly update: (input: { sessionID: SessionID; todos: Info[] }) => Effect.Effect<void>
|
readonly update: (input: { sessionID: SessionID; todos: Info[] }) => Effect.Effect<void>
|
||||||
readonly get: (sessionID: SessionID) => Effect.Effect<Info[]>
|
readonly get: (sessionID: SessionID) => Effect.Effect<Info[]>
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionTodo") {}
|
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionTodo") {}
|
||||||
|
|
||||||
export const layer = Layer.effect(
|
export const layer = Layer.effect(
|
||||||
Service,
|
Service,
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
const bus = yield* Bus.Service
|
const bus = yield* Bus.Service
|
||||||
@@ -79,7 +78,8 @@ export namespace Todo {
|
|||||||
|
|
||||||
return Service.of({ update, get })
|
return Service.of({ update, get })
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
export const defaultLayer = layer.pipe(Layer.provide(Bus.layer))
|
export const defaultLayer = layer.pipe(Layer.provide(Bus.layer))
|
||||||
}
|
|
||||||
|
export * as Todo from "./todo"
|
||||||
|
|||||||
Reference in New Issue
Block a user