saeeol 1.2.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +14 -14
- package/src/cli/cmd/tui/component/dialog/dialog-agent.tsx +32 -0
- package/src/cli/cmd/tui/component/dialog/dialog-command.tsx +190 -0
- package/src/cli/cmd/tui/component/dialog/dialog-console-org.tsx +103 -0
- package/src/cli/cmd/tui/component/dialog/dialog-go-upsell.tsx +159 -0
- package/src/cli/cmd/tui/component/dialog/dialog-mcp.tsx +86 -0
- package/src/cli/cmd/tui/component/dialog/dialog-model.tsx +238 -0
- package/src/cli/cmd/tui/component/dialog/dialog-provider.tsx +343 -0
- package/src/cli/cmd/tui/component/dialog/dialog-session-delete-failed.tsx +103 -0
- package/src/cli/cmd/tui/component/dialog/dialog-session-list.tsx +301 -0
- package/src/cli/cmd/tui/component/dialog/dialog-session-rename.tsx +35 -0
- package/src/cli/cmd/tui/component/dialog/dialog-skill.tsx +37 -0
- package/src/cli/cmd/tui/component/dialog/dialog-stash.tsx +87 -0
- package/src/cli/cmd/tui/component/dialog/dialog-status.tsx +190 -0
- package/src/cli/cmd/tui/component/dialog/dialog-tag.tsx +44 -0
- package/src/cli/cmd/tui/component/dialog/dialog-theme-list.tsx +50 -0
- package/src/cli/cmd/tui/component/dialog/dialog-variant.tsx +39 -0
- package/src/cli/cmd/tui/component/dialog/dialog-workspace-create.tsx +200 -0
- package/src/cli/cmd/tui/component/dialog/dialog-workspace-unavailable.tsx +81 -0
- package/src/cli/cmd/tui/component/dialog-agent.tsx +1 -32
- package/src/cli/cmd/tui/component/dialog-command.tsx +1 -190
- package/src/cli/cmd/tui/component/dialog-console-org.tsx +1 -103
- package/src/cli/cmd/tui/component/dialog-go-upsell.tsx +1 -159
- package/src/cli/cmd/tui/component/dialog-mcp.tsx +1 -86
- package/src/cli/cmd/tui/component/dialog-model.tsx +1 -238
- package/src/cli/cmd/tui/component/dialog-provider.tsx +1 -343
- package/src/cli/cmd/tui/component/dialog-session-delete-failed.tsx +1 -103
- package/src/cli/cmd/tui/component/dialog-session-list.tsx +1 -301
- package/src/cli/cmd/tui/component/dialog-session-rename.tsx +1 -35
- package/src/cli/cmd/tui/component/dialog-skill.tsx +1 -37
- package/src/cli/cmd/tui/component/dialog-stash.tsx +1 -87
- package/src/cli/cmd/tui/component/dialog-status.tsx +1 -190
- package/src/cli/cmd/tui/component/dialog-tag.tsx +1 -44
- package/src/cli/cmd/tui/component/dialog-theme-list.tsx +1 -50
- package/src/cli/cmd/tui/component/dialog-variant.tsx +1 -39
- package/src/cli/cmd/tui/component/dialog-workspace-create.tsx +1 -200
- package/src/cli/cmd/tui/component/dialog-workspace-unavailable.tsx +1 -81
- package/src/session/compaction-helpers.ts +1 -169
- package/src/session/compaction.ts +1 -712
- package/src/session/core/compaction/compaction-helpers.ts +169 -0
- package/src/session/core/compaction/compaction.ts +712 -0
- package/src/session/core/compaction/overflow.ts +28 -0
- package/src/session/core/instruction.ts +234 -0
- package/src/session/core/llm.ts +504 -0
- package/src/session/core/network.ts +392 -0
- package/src/session/core/processor.ts +731 -0
- package/src/session/core/projectors.ts +139 -0
- package/src/session/core/resolve-tools.ts +241 -0
- package/src/session/core/retry.ts +149 -0
- package/src/session/core/revert.ts +173 -0
- package/src/session/core/run-state.ts +110 -0
- package/src/session/core/schema.ts +35 -0
- package/src/session/core/session-types.ts +160 -0
- package/src/session/core/session.sql.ts +124 -0
- package/src/session/core/session.ts +948 -0
- package/src/session/core/shell-exec.ts +205 -0
- package/src/session/core/status.ts +100 -0
- package/src/session/core/subtask.ts +268 -0
- package/src/session/core/summary.ts +173 -0
- package/src/session/core/system.ts +114 -0
- package/src/session/core/todo.ts +86 -0
- package/src/session/core/user-part.ts +293 -0
- package/src/session/instruction.ts +1 -234
- package/src/session/llm.ts +1 -504
- package/src/session/message/message-errors.ts +83 -0
- package/src/session/message/message-parts.ts +89 -0
- package/src/session/message/message-query.ts +107 -0
- package/src/session/message/message-transform.ts +156 -0
- package/src/session/message/message-types.ts +68 -0
- package/src/session/message/message-v2.ts +73 -0
- package/src/session/message/message.ts +192 -0
- package/src/session/message-errors.ts +1 -83
- package/src/session/message-parts.ts +1 -89
- package/src/session/message-query.ts +1 -107
- package/src/session/message-transform.ts +1 -156
- package/src/session/message-types.ts +1 -68
- package/src/session/message-v2.ts +1 -73
- package/src/session/message.ts +1 -192
- package/src/session/network.ts +1 -392
- package/src/session/overflow.ts +1 -28
- package/src/session/processor.ts +1 -731
- package/src/session/projectors.ts +2 -139
- package/src/session/prompt/prompt-command.ts +93 -0
- package/src/session/prompt/prompt-loop.ts +299 -0
- package/src/session/prompt/prompt-model.ts +44 -0
- package/src/session/prompt/prompt-reminders.ts +120 -0
- package/src/session/prompt/prompt-resolve.ts +42 -0
- package/src/session/prompt/prompt-schemas.ts +128 -0
- package/src/session/prompt/prompt-title.ts +55 -0
- package/src/session/prompt/prompt-types.ts +47 -0
- package/src/session/prompt/prompt-user-msg.ts +80 -0
- package/src/session/prompt/prompt.ts +211 -0
- package/src/session/prompt-command.ts +1 -93
- package/src/session/prompt-loop.ts +1 -299
- package/src/session/prompt-model.ts +1 -44
- package/src/session/prompt-reminders.ts +1 -120
- package/src/session/prompt-resolve.ts +1 -42
- package/src/session/prompt-schemas.ts +1 -128
- package/src/session/prompt-title.ts +1 -55
- package/src/session/prompt-types.ts +1 -47
- package/src/session/prompt-user-msg.ts +1 -80
- package/src/session/prompt.ts +1 -211
- package/src/session/resolve-tools.ts +1 -241
- package/src/session/retry.ts +1 -149
- package/src/session/revert.ts +1 -173
- package/src/session/run-state.ts +1 -110
- package/src/session/schema.ts +1 -35
- package/src/session/session-types.ts +1 -160
- package/src/session/session.sql.ts +1 -124
- package/src/session/session.ts +1 -948
- package/src/session/shell-exec.ts +1 -205
- package/src/session/status.ts +1 -100
- package/src/session/subtask.ts +1 -268
- package/src/session/summary.ts +1 -173
- package/src/session/system.ts +1 -114
- package/src/session/todo.ts +1 -86
- package/src/session/user-part.ts +1 -293
- package/src/tool/apply_patch.ts +1 -334
- package/src/tool/bash.ts +1 -656
- package/src/tool/core/external-directory.ts +55 -0
- package/src/tool/core/invalid.ts +21 -0
- package/src/tool/core/recall.ts +164 -0
- package/src/tool/core/recall.txt +12 -0
- package/src/tool/core/schema.ts +16 -0
- package/src/tool/core/tool.ts +162 -0
- package/src/tool/core/truncate.ts +160 -0
- package/src/tool/core/truncation-dir.ts +4 -0
- package/src/tool/diagnostics.ts +1 -20
- package/src/tool/edit-replacers.ts +1 -288
- package/src/tool/edit-utils.ts +1 -86
- package/src/tool/edit.ts +1 -262
- package/src/tool/external-directory.ts +1 -55
- package/src/tool/file/apply_patch.ts +334 -0
- package/src/tool/file/apply_patch.txt +33 -0
- package/src/tool/file/bash.ts +656 -0
- package/src/tool/file/bash.txt +119 -0
- package/src/tool/file/edit-replacers.ts +288 -0
- package/src/tool/file/edit-utils.ts +86 -0
- package/src/tool/file/edit.ts +262 -0
- package/src/tool/file/edit.txt +10 -0
- package/src/tool/file/read.ts +389 -0
- package/src/tool/file/read.txt +14 -0
- package/src/tool/file/write.ts +114 -0
- package/src/tool/file/write.txt +8 -0
- package/src/tool/glob.ts +1 -115
- package/src/tool/grep.ts +1 -151
- package/src/tool/integration/diagnostics.ts +20 -0
- package/src/tool/integration/lsp.ts +113 -0
- package/src/tool/integration/lsp.txt +24 -0
- package/src/tool/integration/mcp-exa.ts +73 -0
- package/src/tool/integration/package.ts +168 -0
- package/src/tool/integration/registry.ts +375 -0
- package/src/tool/invalid.ts +1 -21
- package/src/tool/lsp.ts +1 -113
- package/src/tool/mcp-exa.ts +1 -73
- package/src/tool/package.ts +1 -168
- package/src/tool/plan.ts +1 -30
- package/src/tool/question.ts +1 -52
- package/src/tool/read.ts +1 -389
- package/src/tool/recall.ts +1 -164
- package/src/tool/registry.ts +1 -375
- package/src/tool/schema.ts +1 -16
- package/src/tool/search/glob.ts +115 -0
- package/src/tool/search/glob.txt +6 -0
- package/src/tool/search/grep.ts +151 -0
- package/src/tool/search/grep.txt +8 -0
- package/src/tool/search/warpgrep.ts +107 -0
- package/src/tool/search/warpgrep.txt +10 -0
- package/src/tool/search/webfetch.ts +202 -0
- package/src/tool/search/webfetch.txt +13 -0
- package/src/tool/search/websearch.ts +71 -0
- package/src/tool/search/websearch.txt +14 -0
- package/src/tool/skill.ts +1 -91
- package/src/tool/task.ts +1 -197
- package/src/tool/todo.ts +1 -62
- package/src/tool/tool.ts +1 -162
- package/src/tool/truncate.ts +1 -160
- package/src/tool/truncation-dir.ts +1 -4
- package/src/tool/warpgrep.ts +1 -107
- package/src/tool/webfetch.ts +1 -202
- package/src/tool/websearch.ts +1 -71
- package/src/tool/workflow/plan-enter.txt +14 -0
- package/src/tool/workflow/plan-exit.txt +13 -0
- package/src/tool/workflow/plan.ts +30 -0
- package/src/tool/workflow/question.ts +52 -0
- package/src/tool/workflow/question.txt +11 -0
- package/src/tool/workflow/skill.ts +91 -0
- package/src/tool/workflow/skill.txt +5 -0
- package/src/tool/workflow/task.ts +197 -0
- package/src/tool/workflow/task.txt +57 -0
- package/src/tool/workflow/todo.ts +62 -0
- package/src/tool/workflow/todowrite.txt +167 -0
- package/src/tool/write.ts +1 -114
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { Schema } from "effect"
|
|
2
|
+
import { SessionID } from "../core/schema"
|
|
3
|
+
import { ModelID, ProviderID } from "../../provider/schema"
|
|
4
|
+
import { zod } from "@/util/effect-zod"
|
|
5
|
+
import { NonNegativeInt, withStatics } from "@/util/schema"
|
|
6
|
+
import { namedSchemaError } from "@/util/named-schema-error"
|
|
7
|
+
|
|
8
|
+
export const OutputLengthError = namedSchemaError("MessageOutputLengthError", {})
|
|
9
|
+
export const AuthError = namedSchemaError("ProviderAuthError", {
|
|
10
|
+
providerID: Schema.String,
|
|
11
|
+
message: Schema.String,
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
const AuthErrorEffect = Schema.Struct({
|
|
15
|
+
name: Schema.Literal("ProviderAuthError"),
|
|
16
|
+
data: Schema.Struct({
|
|
17
|
+
providerID: Schema.String,
|
|
18
|
+
message: Schema.String,
|
|
19
|
+
}),
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
const OutputLengthErrorEffect = Schema.Struct({
|
|
23
|
+
name: Schema.Literal("MessageOutputLengthError"),
|
|
24
|
+
data: Schema.Struct({}),
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const UnknownErrorEffect = Schema.Struct({
|
|
28
|
+
name: Schema.Literal("UnknownError"),
|
|
29
|
+
data: Schema.Struct({
|
|
30
|
+
message: Schema.String,
|
|
31
|
+
}),
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
export const ToolCall = Schema.Struct({
|
|
35
|
+
state: Schema.Literal("call"),
|
|
36
|
+
step: Schema.optional(NonNegativeInt),
|
|
37
|
+
toolCallId: Schema.String,
|
|
38
|
+
toolName: Schema.String,
|
|
39
|
+
args: Schema.Unknown,
|
|
40
|
+
})
|
|
41
|
+
.annotate({ identifier: "ToolCall" })
|
|
42
|
+
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
43
|
+
export type ToolCall = Schema.Schema.Type<typeof ToolCall>
|
|
44
|
+
|
|
45
|
+
export const ToolPartialCall = Schema.Struct({
|
|
46
|
+
state: Schema.Literal("partial-call"),
|
|
47
|
+
step: Schema.optional(NonNegativeInt),
|
|
48
|
+
toolCallId: Schema.String,
|
|
49
|
+
toolName: Schema.String,
|
|
50
|
+
args: Schema.Unknown,
|
|
51
|
+
})
|
|
52
|
+
.annotate({ identifier: "ToolPartialCall" })
|
|
53
|
+
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
54
|
+
export type ToolPartialCall = Schema.Schema.Type<typeof ToolPartialCall>
|
|
55
|
+
|
|
56
|
+
export const ToolResult = Schema.Struct({
|
|
57
|
+
state: Schema.Literal("result"),
|
|
58
|
+
step: Schema.optional(NonNegativeInt),
|
|
59
|
+
toolCallId: Schema.String,
|
|
60
|
+
toolName: Schema.String,
|
|
61
|
+
args: Schema.Unknown,
|
|
62
|
+
result: Schema.String,
|
|
63
|
+
})
|
|
64
|
+
.annotate({ identifier: "ToolResult" })
|
|
65
|
+
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
66
|
+
export type ToolResult = Schema.Schema.Type<typeof ToolResult>
|
|
67
|
+
|
|
68
|
+
export const ToolInvocation = Schema.Union([ToolCall, ToolPartialCall, ToolResult])
|
|
69
|
+
.annotate({ identifier: "ToolInvocation", discriminator: "state" })
|
|
70
|
+
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
71
|
+
export type ToolInvocation = Schema.Schema.Type<typeof ToolInvocation>
|
|
72
|
+
|
|
73
|
+
export const TextPart = Schema.Struct({
|
|
74
|
+
type: Schema.Literal("text"),
|
|
75
|
+
text: Schema.String,
|
|
76
|
+
})
|
|
77
|
+
.annotate({ identifier: "TextPart" })
|
|
78
|
+
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
79
|
+
export type TextPart = Schema.Schema.Type<typeof TextPart>
|
|
80
|
+
|
|
81
|
+
export const ReasoningPart = Schema.Struct({
|
|
82
|
+
type: Schema.Literal("reasoning"),
|
|
83
|
+
text: Schema.String,
|
|
84
|
+
providerMetadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
|
85
|
+
})
|
|
86
|
+
.annotate({ identifier: "ReasoningPart" })
|
|
87
|
+
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
88
|
+
export type ReasoningPart = Schema.Schema.Type<typeof ReasoningPart>
|
|
89
|
+
|
|
90
|
+
export const ToolInvocationPart = Schema.Struct({
|
|
91
|
+
type: Schema.Literal("tool-invocation"),
|
|
92
|
+
toolInvocation: ToolInvocation,
|
|
93
|
+
})
|
|
94
|
+
.annotate({ identifier: "ToolInvocationPart" })
|
|
95
|
+
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
96
|
+
export type ToolInvocationPart = Schema.Schema.Type<typeof ToolInvocationPart>
|
|
97
|
+
|
|
98
|
+
export const SourceUrlPart = Schema.Struct({
|
|
99
|
+
type: Schema.Literal("source-url"),
|
|
100
|
+
sourceId: Schema.String,
|
|
101
|
+
url: Schema.String,
|
|
102
|
+
title: Schema.optional(Schema.String),
|
|
103
|
+
providerMetadata: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
|
|
104
|
+
})
|
|
105
|
+
.annotate({ identifier: "SourceUrlPart" })
|
|
106
|
+
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
107
|
+
export type SourceUrlPart = Schema.Schema.Type<typeof SourceUrlPart>
|
|
108
|
+
|
|
109
|
+
export const FilePart = Schema.Struct({
|
|
110
|
+
type: Schema.Literal("file"),
|
|
111
|
+
mediaType: Schema.String,
|
|
112
|
+
filename: Schema.optional(Schema.String),
|
|
113
|
+
url: Schema.String,
|
|
114
|
+
})
|
|
115
|
+
.annotate({ identifier: "FilePart" })
|
|
116
|
+
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
117
|
+
export type FilePart = Schema.Schema.Type<typeof FilePart>
|
|
118
|
+
|
|
119
|
+
export const StepStartPart = Schema.Struct({
|
|
120
|
+
type: Schema.Literal("step-start"),
|
|
121
|
+
})
|
|
122
|
+
.annotate({ identifier: "StepStartPart" })
|
|
123
|
+
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
124
|
+
export type StepStartPart = Schema.Schema.Type<typeof StepStartPart>
|
|
125
|
+
|
|
126
|
+
export const MessagePart = Schema.Union([
|
|
127
|
+
TextPart,
|
|
128
|
+
ReasoningPart,
|
|
129
|
+
ToolInvocationPart,
|
|
130
|
+
SourceUrlPart,
|
|
131
|
+
FilePart,
|
|
132
|
+
StepStartPart,
|
|
133
|
+
])
|
|
134
|
+
.annotate({ identifier: "MessagePart", discriminator: "type" })
|
|
135
|
+
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
136
|
+
export type MessagePart = Schema.Schema.Type<typeof MessagePart>
|
|
137
|
+
|
|
138
|
+
export const Info = Schema.Struct({
|
|
139
|
+
id: Schema.String,
|
|
140
|
+
role: Schema.Literals(["user", "assistant"]),
|
|
141
|
+
parts: Schema.Array(MessagePart),
|
|
142
|
+
metadata: Schema.Struct({
|
|
143
|
+
time: Schema.Struct({
|
|
144
|
+
created: NonNegativeInt,
|
|
145
|
+
completed: Schema.optional(NonNegativeInt),
|
|
146
|
+
}),
|
|
147
|
+
error: Schema.optional(Schema.Union([AuthErrorEffect, UnknownErrorEffect, OutputLengthErrorEffect])),
|
|
148
|
+
sessionID: SessionID,
|
|
149
|
+
tool: Schema.Record(
|
|
150
|
+
Schema.String,
|
|
151
|
+
Schema.StructWithRest(
|
|
152
|
+
Schema.Struct({
|
|
153
|
+
title: Schema.String,
|
|
154
|
+
snapshot: Schema.optional(Schema.String),
|
|
155
|
+
time: Schema.Struct({
|
|
156
|
+
start: NonNegativeInt,
|
|
157
|
+
end: NonNegativeInt,
|
|
158
|
+
}),
|
|
159
|
+
}),
|
|
160
|
+
[Schema.Record(Schema.String, Schema.Unknown)],
|
|
161
|
+
),
|
|
162
|
+
),
|
|
163
|
+
assistant: Schema.optional(
|
|
164
|
+
Schema.Struct({
|
|
165
|
+
system: Schema.Array(Schema.String),
|
|
166
|
+
modelID: ModelID,
|
|
167
|
+
providerID: ProviderID,
|
|
168
|
+
path: Schema.Struct({
|
|
169
|
+
cwd: Schema.String,
|
|
170
|
+
root: Schema.String,
|
|
171
|
+
}),
|
|
172
|
+
cost: Schema.Finite,
|
|
173
|
+
summary: Schema.optional(Schema.Boolean),
|
|
174
|
+
tokens: Schema.Struct({
|
|
175
|
+
input: NonNegativeInt,
|
|
176
|
+
output: NonNegativeInt,
|
|
177
|
+
reasoning: NonNegativeInt,
|
|
178
|
+
cache: Schema.Struct({
|
|
179
|
+
read: NonNegativeInt,
|
|
180
|
+
write: NonNegativeInt,
|
|
181
|
+
}),
|
|
182
|
+
}),
|
|
183
|
+
}),
|
|
184
|
+
),
|
|
185
|
+
snapshot: Schema.optional(Schema.String),
|
|
186
|
+
}).annotate({ identifier: "MessageMetadata" }),
|
|
187
|
+
})
|
|
188
|
+
.annotate({ identifier: "Message" })
|
|
189
|
+
.pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
190
|
+
export type Info = Schema.Schema.Type<typeof Info>
|
|
191
|
+
|
|
192
|
+
export * as Message from "./message"
|
|
@@ -1,83 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { APICallError, LoadAPIKeyError } from "ai"
|
|
4
|
-
import { NamedError } from "@saeeol/core/util/error"
|
|
5
|
-
import * as ProviderError from "@/provider/error"
|
|
6
|
-
import { SessionNetwork } from "./network"
|
|
7
|
-
import { CodexAuthExpiredError } from "@/saeeol/provider/codex-refresh"
|
|
8
|
-
import { Effect, Schema } from "effect"
|
|
9
|
-
import { zod } from "@/util/effect-zod"
|
|
10
|
-
import { NonNegativeInt } from "@/util/schema"
|
|
11
|
-
import { namedSchemaError } from "@/util/named-schema-error"
|
|
12
|
-
import { errorMessage } from "@/util/error"
|
|
13
|
-
import { ProviderID } from "@/provider/schema"
|
|
14
|
-
import type { Assistant } from "./message-types"
|
|
15
|
-
|
|
16
|
-
interface FetchDecompressionError extends Error { code: "ZlibError"; errno: number; path: string }
|
|
17
|
-
|
|
18
|
-
export const OutputLengthError = namedSchemaError("MessageOutputLengthError", {})
|
|
19
|
-
export const AbortedError = namedSchemaError("MessageAbortedError", { message: Schema.String })
|
|
20
|
-
export const StructuredOutputError = namedSchemaError("StructuredOutputError", { message: Schema.String, retries: NonNegativeInt })
|
|
21
|
-
export const AuthError = namedSchemaError("ProviderAuthError", { providerID: Schema.String, message: Schema.String })
|
|
22
|
-
export const APIError = namedSchemaError("APIError", {
|
|
23
|
-
message: Schema.String, statusCode: Schema.optional(NonNegativeInt), isRetryable: Schema.Boolean,
|
|
24
|
-
responseHeaders: Schema.optional(Schema.Record(Schema.String, Schema.String)), responseBody: Schema.optional(Schema.String),
|
|
25
|
-
metadata: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
|
26
|
-
})
|
|
27
|
-
export type APIError = import("zod").infer<typeof APIError.Schema>
|
|
28
|
-
export const ContextOverflowError = namedSchemaError("ContextOverflowError", { message: Schema.String, responseBody: Schema.optional(Schema.String) })
|
|
29
|
-
|
|
30
|
-
export class OutputFormatText extends Schema.Class<OutputFormatText>("OutputFormatText")({ type: Schema.Literal("text") }) { static readonly zod = zod(this) }
|
|
31
|
-
export class OutputFormatJsonSchema extends Schema.Class<OutputFormatJsonSchema>("OutputFormatJsonSchema")({
|
|
32
|
-
type: Schema.Literal("json_schema"), schema: Schema.Record(Schema.String, Schema.Any).annotate({ identifier: "JSONSchema" }),
|
|
33
|
-
retryCount: NonNegativeInt.pipe(Schema.optional, Schema.withDecodingDefault(Effect.succeed(2))),
|
|
34
|
-
}) { static readonly zod = zod(this) }
|
|
35
|
-
|
|
36
|
-
const _Format = Schema.Union([OutputFormatText, OutputFormatJsonSchema]).annotate({ discriminator: "type", identifier: "OutputFormat" })
|
|
37
|
-
export { _Format }
|
|
38
|
-
export const Format = Object.assign(_Format, { zod: zod(_Format) })
|
|
39
|
-
export type OutputFormat = Schema.Schema.Type<typeof _Format>
|
|
40
|
-
|
|
41
|
-
// Assistant error union (Zod)
|
|
42
|
-
import z from "zod"
|
|
43
|
-
const AssistantErrorZod = z.discriminatedUnion("name", [
|
|
44
|
-
AuthError.Schema, NamedError.Unknown.Schema, OutputLengthError.Schema, AbortedError.Schema,
|
|
45
|
-
StructuredOutputError.Schema, ContextOverflowError.Schema, APIError.Schema,
|
|
46
|
-
])
|
|
47
|
-
export type AssistantError = z.infer<typeof AssistantErrorZod>
|
|
48
|
-
export { AssistantErrorZod }
|
|
49
|
-
|
|
50
|
-
// Assistant error union (Effect Schema)
|
|
51
|
-
export const AssistantErrorSchema = Schema.Union([
|
|
52
|
-
AuthError.EffectSchema,
|
|
53
|
-
Schema.Struct({ name: Schema.Literal("UnknownError"), data: Schema.Struct({ message: Schema.String }) }).annotate({ identifier: "UnknownError" }),
|
|
54
|
-
OutputLengthError.EffectSchema, AbortedError.EffectSchema, StructuredOutputError.EffectSchema, ContextOverflowError.EffectSchema, APIError.EffectSchema,
|
|
55
|
-
]).annotate({ discriminator: "name" })
|
|
56
|
-
|
|
57
|
-
export function fromError(e: unknown, ctx: { providerID: ProviderID; aborted?: boolean }): NonNullable<Assistant["error"]> {
|
|
58
|
-
switch (true) {
|
|
59
|
-
case e instanceof DOMException && e.name === "AbortError": return new AbortedError({ message: e.message }, { cause: e }).toObject()
|
|
60
|
-
case OutputLengthError.isInstance(e): return e
|
|
61
|
-
case LoadAPIKeyError.isInstance(e): return new AuthError({ providerID: ctx.providerID, message: e.message }, { cause: e }).toObject()
|
|
62
|
-
case e instanceof CodexAuthExpiredError: return new AuthError({ providerID: "openai", message: e.message }, { cause: e }).toObject()
|
|
63
|
-
case SessionNetwork.disconnected(e):
|
|
64
|
-
return new APIError({ message: SessionNetwork.message(e), isRetryable: true, metadata: { code: (e as import("bun").SystemError).code ?? "", syscall: (e as import("bun").SystemError).syscall ?? "", message: (e as import("bun").SystemError).message ?? "" } }, { cause: e }).toObject()
|
|
65
|
-
case e instanceof Error && (e as FetchDecompressionError).code === "ZlibError":
|
|
66
|
-
if (ctx.aborted) return new AbortedError({ message: e.message }, { cause: e }).toObject()
|
|
67
|
-
return new APIError({ message: "Response decompression failed", isRetryable: true, metadata: { code: (e as FetchDecompressionError).code, message: e.message } }, { cause: e }).toObject()
|
|
68
|
-
case APICallError.isInstance(e):
|
|
69
|
-
const parsed = ProviderError.parseAPICallError({ providerID: ctx.providerID, error: e })
|
|
70
|
-
if (parsed.type === "context_overflow") return new ContextOverflowError({ message: parsed.message, responseBody: parsed.responseBody }, { cause: e }).toObject()
|
|
71
|
-
return new APIError({ message: parsed.message, statusCode: parsed.statusCode, isRetryable: parsed.isRetryable, responseHeaders: parsed.responseHeaders, responseBody: parsed.responseBody, metadata: parsed.metadata }, { cause: e }).toObject()
|
|
72
|
-
case e instanceof Error: return new NamedError.Unknown({ message: errorMessage(e) }, { cause: e }).toObject()
|
|
73
|
-
default:
|
|
74
|
-
try {
|
|
75
|
-
const parsed = ProviderError.parseStreamError(e)
|
|
76
|
-
if (parsed) {
|
|
77
|
-
if (parsed.type === "context_overflow") return new ContextOverflowError({ message: parsed.message, responseBody: parsed.responseBody }, { cause: e }).toObject()
|
|
78
|
-
return new APIError({ message: parsed.message, isRetryable: parsed.isRetryable, responseBody: parsed.responseBody }, { cause: e }).toObject()
|
|
79
|
-
}
|
|
80
|
-
} catch {}
|
|
81
|
-
return new NamedError.Unknown({ message: JSON.stringify(e) }, { cause: e }).toObject()
|
|
82
|
-
}
|
|
83
|
-
}
|
|
1
|
+
export * from "./message/message-errors"
|
|
@@ -1,89 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { SessionID, MessageID, PartID } from "./schema"
|
|
4
|
-
import { LSP } from "@/lsp/lsp"
|
|
5
|
-
import { Snapshot } from "@/snapshot"
|
|
6
|
-
import { Effect, Schema, Types } from "effect"
|
|
7
|
-
import { zod } from "@/util/effect-zod"
|
|
8
|
-
import { NonNegativeInt, withStatics } from "@/util/schema"
|
|
9
|
-
import { APIError } from "./message-errors"
|
|
10
|
-
import { ModelID, ProviderID } from "@/provider/schema"
|
|
11
|
-
import z from "zod"
|
|
12
|
-
|
|
13
|
-
const partBase = { id: PartID, sessionID: SessionID, messageID: MessageID }
|
|
14
|
-
|
|
15
|
-
export const SnapshotPart = Schema.Struct({ ...partBase, type: Schema.Literal("snapshot"), snapshot: Schema.String }).annotate({ identifier: "SnapshotPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
16
|
-
export type SnapshotPart = Types.DeepMutable<Schema.Schema.Type<typeof SnapshotPart>>
|
|
17
|
-
export const PatchPart = Schema.Struct({ ...partBase, type: Schema.Literal("patch"), hash: Schema.String, files: Schema.Array(Schema.String) }).annotate({ identifier: "PatchPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
18
|
-
export type PatchPart = Types.DeepMutable<Schema.Schema.Type<typeof PatchPart>>
|
|
19
|
-
export const TextPart = Schema.Struct({
|
|
20
|
-
...partBase, type: Schema.Literal("text"), text: Schema.String, synthetic: Schema.optional(Schema.Boolean), ignored: Schema.optional(Schema.Boolean),
|
|
21
|
-
time: Schema.optional(Schema.Struct({ start: NonNegativeInt, end: Schema.optional(NonNegativeInt) })), metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)),
|
|
22
|
-
}).annotate({ identifier: "TextPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
23
|
-
export type TextPart = Types.DeepMutable<Schema.Schema.Type<typeof TextPart>>
|
|
24
|
-
export const ReasoningPart = Schema.Struct({
|
|
25
|
-
...partBase, type: Schema.Literal("reasoning"), text: Schema.String, metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)),
|
|
26
|
-
time: Schema.Struct({ start: NonNegativeInt, end: Schema.optional(NonNegativeInt) }),
|
|
27
|
-
}).annotate({ identifier: "ReasoningPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
28
|
-
export type ReasoningPart = Types.DeepMutable<Schema.Schema.Type<typeof ReasoningPart>>
|
|
29
|
-
|
|
30
|
-
const filePartSourceBase = { text: Schema.Struct({ value: Schema.String, start: NonNegativeInt, end: NonNegativeInt }).annotate({ identifier: "FilePartSourceText" }) }
|
|
31
|
-
export const FileSource = Schema.Struct({ ...filePartSourceBase, type: Schema.Literal("file"), path: Schema.String }).annotate({ identifier: "FileSource" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
32
|
-
export const SymbolSource = Schema.Struct({ ...filePartSourceBase, type: Schema.Literal("symbol"), path: Schema.String, range: LSP.Range, name: Schema.String, kind: NonNegativeInt }).annotate({ identifier: "SymbolSource" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
33
|
-
export const ResourceSource = Schema.Struct({ ...filePartSourceBase, type: Schema.Literal("resource"), clientName: Schema.String, uri: Schema.String }).annotate({ identifier: "ResourceSource" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
34
|
-
|
|
35
|
-
const _FilePartSource = Schema.Union([FileSource, SymbolSource, ResourceSource]).annotate({ discriminator: "type", identifier: "FilePartSource" })
|
|
36
|
-
export const FilePartSource = Object.assign(_FilePartSource, { zod: zod(_FilePartSource) })
|
|
37
|
-
|
|
38
|
-
export const FilePart = Schema.Struct({ ...partBase, type: Schema.Literal("file"), mime: Schema.String, filename: Schema.optional(Schema.String), url: Schema.String, source: Schema.optional(_FilePartSource) }).annotate({ identifier: "FilePart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
39
|
-
export type FilePart = Types.DeepMutable<Schema.Schema.Type<typeof FilePart>>
|
|
40
|
-
export const AgentPart = Schema.Struct({ ...partBase, type: Schema.Literal("agent"), name: Schema.String, source: Schema.optional(Schema.Struct({ value: Schema.String, start: NonNegativeInt, end: NonNegativeInt })) }).annotate({ identifier: "AgentPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
41
|
-
export type AgentPart = Types.DeepMutable<Schema.Schema.Type<typeof AgentPart>>
|
|
42
|
-
export const CompactionPart = Schema.Struct({ ...partBase, type: Schema.Literal("compaction"), auto: Schema.Boolean, overflow: Schema.optional(Schema.Boolean), tail_start_id: Schema.optional(MessageID) }).annotate({ identifier: "CompactionPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
43
|
-
export type CompactionPart = Types.DeepMutable<Schema.Schema.Type<typeof CompactionPart>>
|
|
44
|
-
export const SubtaskPart = Schema.Struct({ ...partBase, type: Schema.Literal("subtask"), prompt: Schema.String, description: Schema.String, agent: Schema.String, model: Schema.optional(Schema.Struct({ providerID: ProviderID, modelID: ModelID })), command: Schema.optional(Schema.String) }).annotate({ identifier: "SubtaskPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
45
|
-
export type SubtaskPart = Types.DeepMutable<Schema.Schema.Type<typeof SubtaskPart>>
|
|
46
|
-
export const RetryPart = Schema.Struct({ ...partBase, type: Schema.Literal("retry"), attempt: NonNegativeInt, error: APIError.EffectSchema, time: Schema.Struct({ created: NonNegativeInt }) }).annotate({ identifier: "RetryPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
47
|
-
export type RetryPart = Omit<Types.DeepMutable<Schema.Schema.Type<typeof RetryPart>>, "error"> & { error: APIError }
|
|
48
|
-
export const StepStartPart = Schema.Struct({ ...partBase, type: Schema.Literal("step-start"), snapshot: Schema.optional(Schema.String) }).annotate({ identifier: "StepStartPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
49
|
-
export type StepStartPart = Types.DeepMutable<Schema.Schema.Type<typeof StepStartPart>>
|
|
50
|
-
export const StepFinishPart = Schema.Struct({
|
|
51
|
-
...partBase, type: Schema.Literal("step-finish"), reason: Schema.String, snapshot: Schema.optional(Schema.String), cost: Schema.Finite,
|
|
52
|
-
tokens: Schema.Struct({ total: Schema.optional(NonNegativeInt), input: NonNegativeInt, output: NonNegativeInt, reasoning: NonNegativeInt, cache: Schema.Struct({ read: NonNegativeInt, write: NonNegativeInt }) }),
|
|
53
|
-
}).annotate({ identifier: "StepFinishPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
54
|
-
export type StepFinishPart = Types.DeepMutable<Schema.Schema.Type<typeof StepFinishPart>>
|
|
55
|
-
|
|
56
|
-
export const ToolStatePending = Schema.Struct({ status: Schema.Literal("pending"), input: Schema.Record(Schema.String, Schema.Any), raw: Schema.String }).annotate({ identifier: "ToolStatePending" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
57
|
-
export type ToolStatePending = Types.DeepMutable<Schema.Schema.Type<typeof ToolStatePending>>
|
|
58
|
-
export const ToolStateRunning = Schema.Struct({ status: Schema.Literal("running"), input: Schema.Record(Schema.String, Schema.Any), title: Schema.optional(Schema.String), metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)), time: Schema.Struct({ start: NonNegativeInt }) }).annotate({ identifier: "ToolStateRunning" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
59
|
-
export type ToolStateRunning = Types.DeepMutable<Schema.Schema.Type<typeof ToolStateRunning>>
|
|
60
|
-
export const ToolStateCompleted = Schema.Struct({
|
|
61
|
-
status: Schema.Literal("completed"), input: Schema.Record(Schema.String, Schema.Any), output: Schema.String, title: Schema.String, metadata: Schema.Record(Schema.String, Schema.Any),
|
|
62
|
-
time: Schema.Struct({ start: NonNegativeInt, end: NonNegativeInt, compacted: Schema.optional(NonNegativeInt) }), attachments: Schema.optional(Schema.Array(FilePart)),
|
|
63
|
-
}).annotate({ identifier: "ToolStateCompleted" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
64
|
-
export type ToolStateCompleted = Types.DeepMutable<Schema.Schema.Type<typeof ToolStateCompleted>>
|
|
65
|
-
export function truncateToolOutput(text: string, maxChars?: number) { if (!maxChars || text.length <= maxChars) return text; return `${text.slice(0, maxChars)}\n[Tool output truncated for compaction: omitted ${text.length - maxChars} chars]` }
|
|
66
|
-
export const ToolStateError = Schema.Struct({ status: Schema.Literal("error"), input: Schema.Record(Schema.String, Schema.Any), error: Schema.String, metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)), time: Schema.Struct({ start: NonNegativeInt, end: NonNegativeInt }) }).annotate({ identifier: "ToolStateError" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
67
|
-
export type ToolStateError = Types.DeepMutable<Schema.Schema.Type<typeof ToolStateError>>
|
|
68
|
-
|
|
69
|
-
const _ToolState = Schema.Union([ToolStatePending, ToolStateRunning, ToolStateCompleted, ToolStateError]).annotate({ discriminator: "status", identifier: "ToolState" })
|
|
70
|
-
export const ToolState = Object.assign(_ToolState, { zod: zod(_ToolState) as unknown as z.ZodType<ToolStatePending | ToolStateRunning | ToolStateCompleted | ToolStateError> })
|
|
71
|
-
export type ToolState = ToolStatePending | ToolStateRunning | ToolStateCompleted | ToolStateError
|
|
72
|
-
export const ToolPart = Schema.Struct({ ...partBase, type: Schema.Literal("tool"), callID: Schema.String, tool: Schema.String, state: _ToolState, metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)) }).annotate({ identifier: "ToolPart" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
73
|
-
export type ToolPart = Omit<Types.DeepMutable<Schema.Schema.Type<typeof ToolPart>>, "state"> & { state: ToolState }
|
|
74
|
-
|
|
75
|
-
// Input variants (without ambient IDs)
|
|
76
|
-
export const TextPartInput = Schema.Struct({ id: Schema.optional(PartID), type: Schema.Literal("text"), text: Schema.String, synthetic: Schema.optional(Schema.Boolean), ignored: Schema.optional(Schema.Boolean), time: Schema.optional(Schema.Struct({ start: NonNegativeInt, end: Schema.optional(NonNegativeInt) })), metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)) }).annotate({ identifier: "TextPartInput" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
77
|
-
export type TextPartInput = Types.DeepMutable<Schema.Schema.Type<typeof TextPartInput>>
|
|
78
|
-
export const FilePartInput = Schema.Struct({ id: Schema.optional(PartID), type: Schema.Literal("file"), mime: Schema.String, filename: Schema.optional(Schema.String), url: Schema.String, source: Schema.optional(_FilePartSource) }).annotate({ identifier: "FilePartInput" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
79
|
-
export type FilePartInput = Types.DeepMutable<Schema.Schema.Type<typeof FilePartInput>>
|
|
80
|
-
export const AgentPartInput = Schema.Struct({ id: Schema.optional(PartID), type: Schema.Literal("agent"), name: Schema.String, source: Schema.optional(Schema.Struct({ value: Schema.String, start: NonNegativeInt, end: NonNegativeInt })) }).annotate({ identifier: "AgentPartInput" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
81
|
-
export type AgentPartInput = Types.DeepMutable<Schema.Schema.Type<typeof AgentPartInput>>
|
|
82
|
-
export const SubtaskPartInput = Schema.Struct({ id: Schema.optional(PartID), type: Schema.Literal("subtask"), prompt: Schema.String, description: Schema.String, agent: Schema.String, model: Schema.optional(Schema.Struct({ providerID: ProviderID, modelID: ModelID })), command: Schema.optional(Schema.String) }).annotate({ identifier: "SubtaskPartInput" }).pipe(withStatics((s) => ({ zod: zod(s) })))
|
|
83
|
-
export type SubtaskPartInput = Types.DeepMutable<Schema.Schema.Type<typeof SubtaskPartInput>>
|
|
84
|
-
|
|
85
|
-
// Part union
|
|
86
|
-
const _Part = Schema.Union([TextPart, SubtaskPart, ReasoningPart, FilePart, ToolPart, StepStartPart, StepFinishPart, SnapshotPart, PatchPart, AgentPart, RetryPart, CompactionPart]).annotate({ discriminator: "type", identifier: "Part" })
|
|
87
|
-
export const Part = Object.assign(_Part, { zod: zod(_Part) as unknown as z.ZodType<TextPart | SubtaskPart | ReasoningPart | FilePart | ToolPart | StepStartPart | StepFinishPart | SnapshotPart | PatchPart | AgentPart | RetryPart | CompactionPart> })
|
|
88
|
-
export type Part = TextPart | SubtaskPart | ReasoningPart | FilePart | ToolPart | StepStartPart | StepFinishPart | SnapshotPart | PatchPart | AgentPart | RetryPart | CompactionPart
|
|
89
|
-
export { _Part }
|
|
1
|
+
export * from "./message/message-parts"
|
|
@@ -1,107 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { and, desc, eq } from "drizzle-orm"
|
|
3
|
-
import { Database } from "@/storage/db"
|
|
4
|
-
import { NotFoundError } from "@/storage/storage"
|
|
5
|
-
import { MessageTable, PartTable, SessionTable } from "./session.sql"
|
|
6
|
-
import { SessionID, MessageID } from "./schema"
|
|
7
|
-
import type { WithParts } from "./message-types"
|
|
8
|
-
import { cursor } from "./message-types"
|
|
9
|
-
import type { Part, CompactionPart } from "./message-parts"
|
|
10
|
-
import { stripPartMetadata, hydrate, older, toInfo } from "./message-transform"
|
|
11
|
-
|
|
12
|
-
export function page(input: { sessionID: SessionID; limit: number; before?: string }) {
|
|
13
|
-
const before = input.before ? cursor.decode(input.before) : undefined
|
|
14
|
-
const where = before
|
|
15
|
-
? and(eq(MessageTable.session_id, input.sessionID), older(before))
|
|
16
|
-
: eq(MessageTable.session_id, input.sessionID)
|
|
17
|
-
const rows = Database.use((db) =>
|
|
18
|
-
db
|
|
19
|
-
.select()
|
|
20
|
-
.from(MessageTable)
|
|
21
|
-
.where(where)
|
|
22
|
-
.orderBy(desc(MessageTable.time_created), desc(MessageTable.id))
|
|
23
|
-
.limit(input.limit + 1)
|
|
24
|
-
.all(),
|
|
25
|
-
)
|
|
26
|
-
if (rows.length === 0) {
|
|
27
|
-
const row = Database.use((db) =>
|
|
28
|
-
db.select({ id: SessionTable.id }).from(SessionTable).where(eq(SessionTable.id, input.sessionID)).get(),
|
|
29
|
-
)
|
|
30
|
-
if (!row) throw new NotFoundError({ message: `Session not found: ${input.sessionID}` })
|
|
31
|
-
return { items: [] as WithParts[], more: false }
|
|
32
|
-
}
|
|
33
|
-
const more = rows.length > input.limit
|
|
34
|
-
const slice = more ? rows.slice(0, input.limit) : rows
|
|
35
|
-
const items = hydrate(slice)
|
|
36
|
-
items.reverse()
|
|
37
|
-
const tail = slice.at(-1)
|
|
38
|
-
return {
|
|
39
|
-
items,
|
|
40
|
-
more,
|
|
41
|
-
cursor: more && tail ? cursor.encode({ id: tail.id, time: tail.time_created }) : undefined,
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function* stream(sessionID: SessionID) {
|
|
46
|
-
const size = 50
|
|
47
|
-
let before: string | undefined
|
|
48
|
-
while (true) {
|
|
49
|
-
const next = page({ sessionID, limit: size, before })
|
|
50
|
-
if (next.items.length === 0) break
|
|
51
|
-
for (let i = next.items.length - 1; i >= 0; i--) yield next.items[i]
|
|
52
|
-
if (!next.more || !next.cursor) break
|
|
53
|
-
before = next.cursor
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function parts(message_id: MessageID) {
|
|
58
|
-
const rows = Database.use((db) =>
|
|
59
|
-
db.select().from(PartTable).where(eq(PartTable.message_id, message_id)).orderBy(PartTable.id).all(),
|
|
60
|
-
)
|
|
61
|
-
return rows.map((row) =>
|
|
62
|
-
stripPartMetadata({ ...row.data, id: row.id, sessionID: row.session_id, messageID: row.message_id } as Part),
|
|
63
|
-
)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function get(input: { sessionID: SessionID; messageID: MessageID }): WithParts {
|
|
67
|
-
const row = Database.use((db) =>
|
|
68
|
-
db
|
|
69
|
-
.select()
|
|
70
|
-
.from(MessageTable)
|
|
71
|
-
.where(and(eq(MessageTable.id, input.messageID), eq(MessageTable.session_id, input.sessionID)))
|
|
72
|
-
.get(),
|
|
73
|
-
)
|
|
74
|
-
if (!row) throw new NotFoundError({ message: `Message not found: ${input.messageID}` })
|
|
75
|
-
return { info: toInfo(row), parts: parts(input.messageID) }
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function filterCompacted(msgs: Iterable<WithParts>) {
|
|
79
|
-
const result = [] as WithParts[]
|
|
80
|
-
const completed = new Set<string>()
|
|
81
|
-
let retain: MessageID | undefined
|
|
82
|
-
for (const msg of msgs) {
|
|
83
|
-
result.push(msg)
|
|
84
|
-
if (retain) {
|
|
85
|
-
if (msg.info.id === retain) break
|
|
86
|
-
continue
|
|
87
|
-
}
|
|
88
|
-
if (msg.info.role === "user" && completed.has(msg.info.id)) {
|
|
89
|
-
const part = msg.parts.find((item): item is CompactionPart => item.type === "compaction")
|
|
90
|
-
if (!part) continue
|
|
91
|
-
if (!part.tail_start_id) break
|
|
92
|
-
retain = part.tail_start_id
|
|
93
|
-
if (msg.info.id === retain) break
|
|
94
|
-
continue
|
|
95
|
-
}
|
|
96
|
-
if (msg.info.role === "user" && completed.has(msg.info.id) && msg.parts.some((part) => part.type === "compaction"))
|
|
97
|
-
break
|
|
98
|
-
if (msg.info.role === "assistant" && msg.info.summary && msg.info.finish && !msg.info.error)
|
|
99
|
-
completed.add(msg.info.parentID)
|
|
100
|
-
}
|
|
101
|
-
result.reverse()
|
|
102
|
-
return result
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export const filterCompactedEffect = Effect.fnUntraced(function* (sessionID: SessionID) {
|
|
106
|
-
return filterCompacted(stream(sessionID))
|
|
107
|
-
})
|
|
1
|
+
export * from "./message/message-query"
|
|
@@ -1,156 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { convertToModelMessages, type UIMessage } from "ai"
|
|
4
|
-
import { Effect } from "effect"
|
|
5
|
-
import { and, desc, eq, inArray, lt, or } from "drizzle-orm"
|
|
6
|
-
import { Database } from "@/storage/db"
|
|
7
|
-
import { MessageTable, PartTable } from "./session.sql"
|
|
8
|
-
import { Snapshot } from "@/snapshot"
|
|
9
|
-
import { iife } from "@/util/iife"
|
|
10
|
-
import { isMedia } from "@/util/media"
|
|
11
|
-
import { MessageID, SessionID } from "./schema"
|
|
12
|
-
import * as EffectLogger from "@saeeol/core/effect/logger"
|
|
13
|
-
import type { Provider } from "@/provider/provider"
|
|
14
|
-
export const SYNTHETIC_ATTACHMENT_PROMPT = "Attached image(s) from tool result:"
|
|
15
|
-
import type { Info, User, WithParts } from "./message-types"
|
|
16
|
-
import type { Part, CompactionPart, ToolState, ToolStateCompleted } from "./message-parts"
|
|
17
|
-
import { truncateToolOutput } from "./message-parts"
|
|
18
|
-
import { AbortedError } from "./message-errors"
|
|
19
|
-
|
|
20
|
-
export { isMedia }
|
|
21
|
-
|
|
22
|
-
function stripPatch(value: unknown) {
|
|
23
|
-
if (typeof value !== "string") return undefined
|
|
24
|
-
if (Buffer.byteLength(value) > Snapshot.MAX_DIFF_SIZE) return undefined
|
|
25
|
-
return value
|
|
26
|
-
}
|
|
27
|
-
function withPatch(value: unknown) { const kept = stripPatch(value); return kept ? { patch: kept } : {} }
|
|
28
|
-
|
|
29
|
-
export function stripPartMetadata(part: Part): Part {
|
|
30
|
-
if (part.type !== "tool") return part
|
|
31
|
-
const { state } = part
|
|
32
|
-
if (state.status !== "completed" && state.status !== "running") return part
|
|
33
|
-
const meta = state.metadata
|
|
34
|
-
if (!meta) return part
|
|
35
|
-
let changed = false
|
|
36
|
-
let next = meta
|
|
37
|
-
if (meta.diff !== undefined) { const { diff, ...rest } = next; next = rest; changed = true }
|
|
38
|
-
if (meta.filediff) { const { before, after, patch, ...rest } = meta.filediff; next = { ...next, filediff: { ...rest, ...withPatch(patch) } }; changed = true }
|
|
39
|
-
if (Array.isArray(meta.files) && meta.files.length > 0) { next = { ...next, files: meta.files.map((f: Record<string, unknown>) => { const { before, after, patch, diff, ...rest } = f; const kept = stripPatch(patch) ?? stripPatch(diff); return { ...rest, ...(kept ? { patch: kept } : {}) } }) }; changed = true }
|
|
40
|
-
if (Array.isArray(meta.results) && meta.results.length > 0) { next = { ...next, results: meta.results.map((r: Record<string, unknown>) => { const { diff, ...rest } = r; if (!r.filediff || typeof r.filediff !== "object") return rest; const fd = r.filediff as Record<string, unknown>; const { before, after, patch, ...file } = fd; return { ...rest, filediff: { ...file, ...withPatch(patch) } } }) }; changed = true }
|
|
41
|
-
if (!changed) return part
|
|
42
|
-
return { ...part, state: { ...state, metadata: next } } as Part
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function stripMessageMetadata(info: Info): Info {
|
|
46
|
-
if (info.role !== "user") return info
|
|
47
|
-
const user = info as User
|
|
48
|
-
if (!user.summary?.diffs?.length) return info
|
|
49
|
-
const oversized = (d: Snapshot.FileDiff) => d.patch && Buffer.byteLength(d.patch) > Snapshot.MAX_DIFF_SIZE
|
|
50
|
-
if (!user.summary.diffs.some(oversized)) return info
|
|
51
|
-
return { ...user, summary: { ...user.summary, diffs: user.summary.diffs.map((d: Snapshot.FileDiff) => (oversized(d) ? { ...d, patch: "" } : d)) } } as Info
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export const toInfo = (row: typeof MessageTable.$inferSelect) => stripMessageMetadata({ ...row.data, id: row.id, sessionID: row.session_id } as Info)
|
|
55
|
-
const toPart = (row: typeof PartTable.$inferSelect) => stripPartMetadata({ ...row.data, id: row.id, sessionID: row.session_id, messageID: row.message_id } as Part)
|
|
56
|
-
|
|
57
|
-
export const older = (row: { id: MessageID; time: number }) =>
|
|
58
|
-
or(lt(MessageTable.time_created, row.time), and(eq(MessageTable.time_created, row.time), lt(MessageTable.id, row.id)))
|
|
59
|
-
|
|
60
|
-
export function hydrate(rows: (typeof MessageTable.$inferSelect)[]) {
|
|
61
|
-
const ids = rows.map((row) => row.id)
|
|
62
|
-
const partByMessage = new Map<string, Part[]>()
|
|
63
|
-
if (ids.length > 0) {
|
|
64
|
-
const partRows = Database.use((db) => db.select().from(PartTable).where(inArray(PartTable.message_id, ids)).orderBy(PartTable.message_id, PartTable.id).all())
|
|
65
|
-
for (const row of partRows) { const next = toPart(row); const list = partByMessage.get(row.message_id); if (list) list.push(next); else partByMessage.set(row.message_id, [next]) }
|
|
66
|
-
}
|
|
67
|
-
return rows.map((row) => ({ info: toInfo(row), parts: partByMessage.get(row.id) ?? [] }))
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function providerMeta(metadata: Record<string, any> | undefined) {
|
|
71
|
-
if (!metadata) return undefined
|
|
72
|
-
const { providerExecuted: _, ...rest } = metadata
|
|
73
|
-
return Object.keys(rest).length > 0 ? rest : undefined
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export const toModelMessagesEffect = Effect.fnUntraced(function* (input: WithParts[], model: Provider.Model, options?: { stripMedia?: boolean; toolOutputMaxChars?: number }) {
|
|
77
|
-
const result: UIMessage[] = []
|
|
78
|
-
const toolNames = new Set<string>()
|
|
79
|
-
const supportsMediaInToolResults = (() => {
|
|
80
|
-
if (model.api.npm === "@ai-sdk/anthropic") return true
|
|
81
|
-
if (model.api.npm === "@ai-sdk/openai") return true
|
|
82
|
-
if (model.api.npm === "@ai-sdk/amazon-bedrock") return true
|
|
83
|
-
if (model.api.npm === "@ai-sdk/google-vertex/anthropic") return true
|
|
84
|
-
if (model.api.npm === "@ai-sdk/google") { const id = model.api.id.toLowerCase(); return id.includes("gemini-3") && !id.includes("gemini-2") }
|
|
85
|
-
return false
|
|
86
|
-
})()
|
|
87
|
-
const toModelOutput = (opts: { toolCallId: string; input: unknown; output: unknown }) => {
|
|
88
|
-
const output = opts.output
|
|
89
|
-
if (typeof output === "string") return { type: "text", value: output }
|
|
90
|
-
if (typeof output === "object") {
|
|
91
|
-
const o = output as { text: string; attachments?: Array<{ mime: string; url: string }> }
|
|
92
|
-
const attachments = (o.attachments ?? []).filter((a) => a.url.startsWith("data:") && a.url.includes(","))
|
|
93
|
-
return { type: "content", value: [...(o.text ? [{ type: "text", text: o.text }] : []), ...attachments.map((a) => ({ type: "media", mediaType: a.mime, data: iife(() => { const ci = a.url.indexOf(","); return ci === -1 ? a.url : a.url.slice(ci + 1) }) }))] }
|
|
94
|
-
}
|
|
95
|
-
return { type: "json", value: output as never }
|
|
96
|
-
}
|
|
97
|
-
for (const msg of input) {
|
|
98
|
-
if (msg.parts.length === 0) continue
|
|
99
|
-
if (msg.info.role === "user") {
|
|
100
|
-
const userMessage: UIMessage = { id: msg.info.id, role: "user", parts: [] }
|
|
101
|
-
result.push(userMessage)
|
|
102
|
-
for (const part of msg.parts) {
|
|
103
|
-
if (part.type === "text" && !part.ignored) userMessage.parts.push({ type: "text", text: part.text })
|
|
104
|
-
if (part.type === "file" && part.mime !== "text/plain" && part.mime !== "application/x-directory") {
|
|
105
|
-
if (options?.stripMedia && isMedia(part.mime)) userMessage.parts.push({ type: "text", text: `[Attached ${part.mime}: ${part.filename ?? "file"}]` })
|
|
106
|
-
else userMessage.parts.push({ type: "file", url: part.url, mediaType: part.mime, filename: part.filename })
|
|
107
|
-
}
|
|
108
|
-
if (part.type === "compaction") userMessage.parts.push({ type: "text", text: "What did we do so far?" })
|
|
109
|
-
if (part.type === "subtask") userMessage.parts.push({ type: "text", text: "The following tool was executed by the user" })
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
if (msg.info.role === "assistant") {
|
|
113
|
-
const differentModel = `${model.providerID}/${model.id}` !== `${msg.info.providerID}/${msg.info.modelID}`
|
|
114
|
-
const media: Array<{ mime: string; url: string }> = []
|
|
115
|
-
if (msg.info.error && !(AbortedError.isInstance(msg.info.error) && msg.parts.some((p) => p.type !== "step-start" && p.type !== "reasoning"))) continue
|
|
116
|
-
const assistantMessage: UIMessage = { id: msg.info.id, role: "assistant", parts: [] }
|
|
117
|
-
for (const part of msg.parts) {
|
|
118
|
-
if (part.type === "text" && !part.ignored) assistantMessage.parts.push({ type: "text", text: part.text, ...(differentModel ? {} : { providerMetadata: part.metadata }) })
|
|
119
|
-
if (part.type === "step-start") assistantMessage.parts.push({ type: "step-start" })
|
|
120
|
-
if (part.type === "tool") {
|
|
121
|
-
toolNames.add(part.tool)
|
|
122
|
-
if (part.state.status === "completed") {
|
|
123
|
-
const outputText = part.state.time.compacted ? "[Old tool result content cleared]" : truncateToolOutput(part.state.output, options?.toolOutputMaxChars)
|
|
124
|
-
const attachments = part.state.time.compacted || options?.stripMedia ? [] : (part.state.attachments ?? [])
|
|
125
|
-
const mediaAttachments = attachments.filter((a) => isMedia(a.mime))
|
|
126
|
-
const nonMediaAttachments = attachments.filter((a) => !isMedia(a.mime))
|
|
127
|
-
if (!supportsMediaInToolResults && mediaAttachments.length > 0) media.push(...mediaAttachments)
|
|
128
|
-
const finalAttachments = supportsMediaInToolResults ? attachments : nonMediaAttachments
|
|
129
|
-
const output = finalAttachments.length > 0 ? { text: outputText, attachments: finalAttachments } : outputText
|
|
130
|
-
assistantMessage.parts.push({ type: ("tool-" + part.tool) as `tool-${string}`, state: "output-available", toolCallId: part.callID, input: part.state.input, output, ...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}), ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }) })
|
|
131
|
-
}
|
|
132
|
-
if (part.state.status === "error") {
|
|
133
|
-
const output = part.state.metadata?.interrupted === true ? part.state.metadata.output : undefined
|
|
134
|
-
if (typeof output === "string") assistantMessage.parts.push({ type: ("tool-" + part.tool) as `tool-${string}`, state: "output-available", toolCallId: part.callID, input: part.state.input, output, ...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}), ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }) })
|
|
135
|
-
else assistantMessage.parts.push({ type: ("tool-" + part.tool) as `tool-${string}`, state: "output-error", toolCallId: part.callID, input: part.state.input, errorText: part.state.error, ...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}), ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }) })
|
|
136
|
-
}
|
|
137
|
-
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]", ...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}), ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }) })
|
|
138
|
-
}
|
|
139
|
-
if (part.type === "reasoning") {
|
|
140
|
-
if (differentModel) { if (part.text.trim().length > 0) assistantMessage.parts.push({ type: "text", text: part.text }); continue }
|
|
141
|
-
assistantMessage.parts.push({ type: "reasoning", text: part.text, providerMetadata: part.metadata })
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
if (assistantMessage.parts.length > 0) {
|
|
145
|
-
result.push(assistantMessage)
|
|
146
|
-
if (media.length > 0) result.push({ id: MessageID.ascending(), role: "user", parts: [{ type: "text" as const, text: SYNTHETIC_ATTACHMENT_PROMPT }, ...media.map((a) => ({ type: "file" as const, url: a.url, mediaType: a.mime }))] })
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
const tools = Object.fromEntries(Array.from(toolNames).map((n) => [n, { toModelOutput }]))
|
|
151
|
-
return yield* Effect.promise(() => convertToModelMessages(result.filter((m) => m.parts.some((p) => p.type !== "step-start")), { tools } as any))
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
export function toModelMessages(input: WithParts[], model: Provider.Model, options?: { stripMedia?: boolean; toolOutputMaxChars?: number }): Promise<import("ai").ModelMessage[]> {
|
|
155
|
-
return Effect.runPromise(toModelMessagesEffect(input, model, options).pipe(Effect.provide(EffectLogger.layer)))
|
|
156
|
-
}
|
|
1
|
+
export * from "./message/message-transform"
|