rird 1.0.200
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/AGENTS.md +27 -0
- package/Dockerfile +18 -0
- package/README.md +15 -0
- package/bin/opencode +336 -0
- package/bin/pty-wrapper.js +285 -0
- package/bunfig.toml +4 -0
- package/facebook_ads_library.png +0 -0
- package/nul`nif +0 -0
- package/package.json +111 -0
- package/parsers-config.ts +239 -0
- package/rird-1.0.199.tgz +0 -0
- package/script/build-windows.ts +54 -0
- package/script/build.ts +167 -0
- package/script/postinstall.mjs +544 -0
- package/script/publish-registries.ts +187 -0
- package/script/publish.ts +72 -0
- package/script/schema.ts +47 -0
- package/src/acp/README.md +164 -0
- package/src/acp/agent.ts +1063 -0
- package/src/acp/session.ts +101 -0
- package/src/acp/types.ts +22 -0
- package/src/agent/agent.ts +367 -0
- package/src/agent/generate.txt +75 -0
- package/src/agent/prompt/compaction.txt +12 -0
- package/src/agent/prompt/explore.txt +18 -0
- package/src/agent/prompt/summary.txt +10 -0
- package/src/agent/prompt/title.txt +36 -0
- package/src/auth/index.ts +70 -0
- package/src/bun/index.ts +114 -0
- package/src/bus/bus-event.ts +43 -0
- package/src/bus/global.ts +10 -0
- package/src/bus/index.ts +105 -0
- package/src/cli/bootstrap.ts +17 -0
- package/src/cli/cmd/acp.ts +88 -0
- package/src/cli/cmd/agent.ts +256 -0
- package/src/cli/cmd/auth.ts +391 -0
- package/src/cli/cmd/cmd.ts +7 -0
- package/src/cli/cmd/debug/config.ts +15 -0
- package/src/cli/cmd/debug/file.ts +91 -0
- package/src/cli/cmd/debug/index.ts +43 -0
- package/src/cli/cmd/debug/lsp.ts +48 -0
- package/src/cli/cmd/debug/ripgrep.ts +83 -0
- package/src/cli/cmd/debug/scrap.ts +15 -0
- package/src/cli/cmd/debug/skill.ts +15 -0
- package/src/cli/cmd/debug/snapshot.ts +48 -0
- package/src/cli/cmd/export.ts +88 -0
- package/src/cli/cmd/generate.ts +38 -0
- package/src/cli/cmd/github.ts +1400 -0
- package/src/cli/cmd/import.ts +98 -0
- package/src/cli/cmd/mcp.ts +654 -0
- package/src/cli/cmd/models.ts +77 -0
- package/src/cli/cmd/pr.ts +112 -0
- package/src/cli/cmd/run.ts +368 -0
- package/src/cli/cmd/serve.ts +31 -0
- package/src/cli/cmd/session.ts +106 -0
- package/src/cli/cmd/stats.ts +298 -0
- package/src/cli/cmd/tui/app.tsx +696 -0
- package/src/cli/cmd/tui/attach.ts +30 -0
- package/src/cli/cmd/tui/component/border.tsx +21 -0
- package/src/cli/cmd/tui/component/dialog-agent.tsx +31 -0
- package/src/cli/cmd/tui/component/dialog-command.tsx +124 -0
- package/src/cli/cmd/tui/component/dialog-mcp.tsx +86 -0
- package/src/cli/cmd/tui/component/dialog-model.tsx +245 -0
- package/src/cli/cmd/tui/component/dialog-provider.tsx +224 -0
- package/src/cli/cmd/tui/component/dialog-session-list.tsx +102 -0
- package/src/cli/cmd/tui/component/dialog-session-rename.tsx +31 -0
- package/src/cli/cmd/tui/component/dialog-stash.tsx +86 -0
- package/src/cli/cmd/tui/component/dialog-status.tsx +162 -0
- package/src/cli/cmd/tui/component/dialog-tag.tsx +44 -0
- package/src/cli/cmd/tui/component/dialog-theme-list.tsx +50 -0
- package/src/cli/cmd/tui/component/did-you-know.tsx +85 -0
- package/src/cli/cmd/tui/component/logo.tsx +35 -0
- package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +574 -0
- package/src/cli/cmd/tui/component/prompt/history.tsx +108 -0
- package/src/cli/cmd/tui/component/prompt/index.tsx +1090 -0
- package/src/cli/cmd/tui/component/prompt/stash.tsx +101 -0
- package/src/cli/cmd/tui/component/tips.ts +27 -0
- package/src/cli/cmd/tui/component/todo-item.tsx +32 -0
- package/src/cli/cmd/tui/context/args.tsx +14 -0
- package/src/cli/cmd/tui/context/directory.ts +13 -0
- package/src/cli/cmd/tui/context/exit.tsx +23 -0
- package/src/cli/cmd/tui/context/helper.tsx +25 -0
- package/src/cli/cmd/tui/context/keybind.tsx +101 -0
- package/src/cli/cmd/tui/context/kv.tsx +49 -0
- package/src/cli/cmd/tui/context/local.tsx +354 -0
- package/src/cli/cmd/tui/context/prompt.tsx +18 -0
- package/src/cli/cmd/tui/context/route.tsx +46 -0
- package/src/cli/cmd/tui/context/sdk.tsx +74 -0
- package/src/cli/cmd/tui/context/sync.tsx +372 -0
- package/src/cli/cmd/tui/context/theme/aura.json +69 -0
- package/src/cli/cmd/tui/context/theme/ayu.json +80 -0
- package/src/cli/cmd/tui/context/theme/catppuccin-frappe.json +233 -0
- package/src/cli/cmd/tui/context/theme/catppuccin-macchiato.json +233 -0
- package/src/cli/cmd/tui/context/theme/catppuccin.json +112 -0
- package/src/cli/cmd/tui/context/theme/cobalt2.json +228 -0
- package/src/cli/cmd/tui/context/theme/cursor.json +249 -0
- package/src/cli/cmd/tui/context/theme/dracula.json +219 -0
- package/src/cli/cmd/tui/context/theme/everforest.json +241 -0
- package/src/cli/cmd/tui/context/theme/flexoki.json +237 -0
- package/src/cli/cmd/tui/context/theme/github.json +233 -0
- package/src/cli/cmd/tui/context/theme/gruvbox.json +95 -0
- package/src/cli/cmd/tui/context/theme/kanagawa.json +77 -0
- package/src/cli/cmd/tui/context/theme/lucent-orng.json +227 -0
- package/src/cli/cmd/tui/context/theme/material.json +235 -0
- package/src/cli/cmd/tui/context/theme/matrix.json +77 -0
- package/src/cli/cmd/tui/context/theme/mercury.json +252 -0
- package/src/cli/cmd/tui/context/theme/monokai.json +221 -0
- package/src/cli/cmd/tui/context/theme/nightowl.json +221 -0
- package/src/cli/cmd/tui/context/theme/nord.json +223 -0
- package/src/cli/cmd/tui/context/theme/one-dark.json +84 -0
- package/src/cli/cmd/tui/context/theme/orng.json +245 -0
- package/src/cli/cmd/tui/context/theme/palenight.json +222 -0
- package/src/cli/cmd/tui/context/theme/rird.json +245 -0
- package/src/cli/cmd/tui/context/theme/rosepine.json +234 -0
- package/src/cli/cmd/tui/context/theme/solarized.json +223 -0
- package/src/cli/cmd/tui/context/theme/synthwave84.json +226 -0
- package/src/cli/cmd/tui/context/theme/tokyonight.json +243 -0
- package/src/cli/cmd/tui/context/theme/vercel.json +245 -0
- package/src/cli/cmd/tui/context/theme/vesper.json +218 -0
- package/src/cli/cmd/tui/context/theme/zenburn.json +223 -0
- package/src/cli/cmd/tui/context/theme.tsx +1109 -0
- package/src/cli/cmd/tui/event.ts +40 -0
- package/src/cli/cmd/tui/routes/home.tsx +138 -0
- package/src/cli/cmd/tui/routes/session/dialog-fork-from-timeline.tsx +64 -0
- package/src/cli/cmd/tui/routes/session/dialog-message.tsx +109 -0
- package/src/cli/cmd/tui/routes/session/dialog-subagent.tsx +26 -0
- package/src/cli/cmd/tui/routes/session/dialog-timeline.tsx +47 -0
- package/src/cli/cmd/tui/routes/session/footer.tsx +88 -0
- package/src/cli/cmd/tui/routes/session/header.tsx +125 -0
- package/src/cli/cmd/tui/routes/session/index.tsx +1864 -0
- package/src/cli/cmd/tui/routes/session/sidebar.tsx +318 -0
- package/src/cli/cmd/tui/spawn.ts +60 -0
- package/src/cli/cmd/tui/thread.ts +142 -0
- package/src/cli/cmd/tui/ui/dialog-alert.tsx +57 -0
- package/src/cli/cmd/tui/ui/dialog-confirm.tsx +83 -0
- package/src/cli/cmd/tui/ui/dialog-help.tsx +38 -0
- package/src/cli/cmd/tui/ui/dialog-prompt.tsx +77 -0
- package/src/cli/cmd/tui/ui/dialog-select.tsx +332 -0
- package/src/cli/cmd/tui/ui/dialog.tsx +170 -0
- package/src/cli/cmd/tui/ui/spinner.ts +368 -0
- package/src/cli/cmd/tui/ui/toast.tsx +100 -0
- package/src/cli/cmd/tui/util/clipboard.ts +127 -0
- package/src/cli/cmd/tui/util/editor.ts +32 -0
- package/src/cli/cmd/tui/util/terminal.ts +114 -0
- package/src/cli/cmd/tui/worker.ts +63 -0
- package/src/cli/cmd/uninstall.ts +344 -0
- package/src/cli/cmd/upgrade.ts +100 -0
- package/src/cli/cmd/web.ts +84 -0
- package/src/cli/error.ts +56 -0
- package/src/cli/ui.ts +84 -0
- package/src/cli/upgrade.ts +25 -0
- package/src/command/index.ts +80 -0
- package/src/command/template/initialize.txt +10 -0
- package/src/command/template/review.txt +97 -0
- package/src/config/config.ts +995 -0
- package/src/config/markdown.ts +41 -0
- package/src/env/index.ts +26 -0
- package/src/file/ignore.ts +83 -0
- package/src/file/index.ts +328 -0
- package/src/file/ripgrep.ts +393 -0
- package/src/file/time.ts +64 -0
- package/src/file/watcher.ts +103 -0
- package/src/flag/flag.ts +46 -0
- package/src/format/formatter.ts +315 -0
- package/src/format/index.ts +137 -0
- package/src/global/index.ts +52 -0
- package/src/id/id.ts +73 -0
- package/src/ide/index.ts +76 -0
- package/src/index.ts +240 -0
- package/src/installation/index.ts +239 -0
- package/src/lsp/client.ts +229 -0
- package/src/lsp/index.ts +485 -0
- package/src/lsp/language.ts +116 -0
- package/src/lsp/server.ts +1895 -0
- package/src/mcp/auth.ts +135 -0
- package/src/mcp/index.ts +690 -0
- package/src/mcp/oauth-callback.ts +200 -0
- package/src/mcp/oauth-provider.ts +154 -0
- package/src/patch/index.ts +622 -0
- package/src/permission/index.ts +199 -0
- package/src/plugin/index.ts +91 -0
- package/src/project/bootstrap.ts +31 -0
- package/src/project/instance.ts +78 -0
- package/src/project/project.ts +221 -0
- package/src/project/state.ts +65 -0
- package/src/project/vcs.ts +76 -0
- package/src/provider/auth.ts +143 -0
- package/src/provider/models-macro.ts +11 -0
- package/src/provider/models.ts +106 -0
- package/src/provider/provider.ts +1071 -0
- package/src/provider/sdk/openai-compatible/src/README.md +5 -0
- package/src/provider/sdk/openai-compatible/src/index.ts +2 -0
- package/src/provider/sdk/openai-compatible/src/openai-compatible-provider.ts +100 -0
- package/src/provider/sdk/openai-compatible/src/responses/convert-to-openai-responses-input.ts +303 -0
- package/src/provider/sdk/openai-compatible/src/responses/map-openai-responses-finish-reason.ts +22 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-config.ts +18 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-error.ts +22 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-api-types.ts +207 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-language-model.ts +1713 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-prepare-tools.ts +177 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-settings.ts +1 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/code-interpreter.ts +88 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/file-search.ts +128 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/image-generation.ts +115 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/local-shell.ts +65 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/web-search-preview.ts +104 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/web-search.ts +103 -0
- package/src/provider/transform.ts +455 -0
- package/src/pty/index.ts +231 -0
- package/src/security/guardrails.test.ts +341 -0
- package/src/security/guardrails.ts +558 -0
- package/src/security/index.ts +19 -0
- package/src/server/error.ts +36 -0
- package/src/server/project.ts +79 -0
- package/src/server/server.ts +2642 -0
- package/src/server/tui.ts +71 -0
- package/src/session/compaction.ts +223 -0
- package/src/session/index.ts +461 -0
- package/src/session/llm.ts +201 -0
- package/src/session/message-v2.ts +690 -0
- package/src/session/message.ts +189 -0
- package/src/session/processor.ts +409 -0
- package/src/session/prompt/act-switch.txt +5 -0
- package/src/session/prompt/anthropic-20250930.txt +166 -0
- package/src/session/prompt/anthropic.txt +85 -0
- package/src/session/prompt/anthropic_spoof.txt +1 -0
- package/src/session/prompt/beast.txt +103 -0
- package/src/session/prompt/codex.txt +304 -0
- package/src/session/prompt/copilot-gpt-5.txt +138 -0
- package/src/session/prompt/gemini.txt +85 -0
- package/src/session/prompt/max-steps.txt +16 -0
- package/src/session/prompt/plan-reminder-anthropic.txt +35 -0
- package/src/session/prompt/plan.txt +24 -0
- package/src/session/prompt/polaris.txt +84 -0
- package/src/session/prompt/qwen.txt +106 -0
- package/src/session/prompt.ts +1509 -0
- package/src/session/retry.ts +86 -0
- package/src/session/revert.ts +108 -0
- package/src/session/sensitive-filter.test.ts +327 -0
- package/src/session/sensitive-filter.ts +466 -0
- package/src/session/status.ts +76 -0
- package/src/session/summary.ts +194 -0
- package/src/session/system.ts +120 -0
- package/src/session/todo.ts +37 -0
- package/src/share/share-next.ts +194 -0
- package/src/share/share.ts +87 -0
- package/src/shell/shell.ts +67 -0
- package/src/skill/index.ts +1 -0
- package/src/skill/skill.ts +83 -0
- package/src/snapshot/index.ts +197 -0
- package/src/storage/storage.ts +226 -0
- package/src/tests/agent.test.ts +308 -0
- package/src/tests/build-guards.test.ts +267 -0
- package/src/tests/config.test.ts +664 -0
- package/src/tests/tool-registry.test.ts +589 -0
- package/src/tool/bash.ts +317 -0
- package/src/tool/bash.txt +158 -0
- package/src/tool/batch.ts +175 -0
- package/src/tool/batch.txt +24 -0
- package/src/tool/codesearch.ts +168 -0
- package/src/tool/codesearch.txt +12 -0
- package/src/tool/edit.ts +675 -0
- package/src/tool/edit.txt +10 -0
- package/src/tool/glob.ts +65 -0
- package/src/tool/glob.txt +6 -0
- package/src/tool/grep.ts +121 -0
- package/src/tool/grep.txt +8 -0
- package/src/tool/invalid.ts +17 -0
- package/src/tool/ls.ts +110 -0
- package/src/tool/ls.txt +1 -0
- package/src/tool/lsp-diagnostics.ts +26 -0
- package/src/tool/lsp-diagnostics.txt +1 -0
- package/src/tool/lsp-hover.ts +31 -0
- package/src/tool/lsp-hover.txt +1 -0
- package/src/tool/lsp.ts +87 -0
- package/src/tool/lsp.txt +19 -0
- package/src/tool/multiedit.ts +46 -0
- package/src/tool/multiedit.txt +41 -0
- package/src/tool/patch.ts +233 -0
- package/src/tool/patch.txt +1 -0
- package/src/tool/read.ts +219 -0
- package/src/tool/read.txt +12 -0
- package/src/tool/registry.ts +162 -0
- package/src/tool/skill.ts +100 -0
- package/src/tool/task.ts +136 -0
- package/src/tool/task.txt +51 -0
- package/src/tool/todo.ts +39 -0
- package/src/tool/todoread.txt +14 -0
- package/src/tool/todowrite.txt +167 -0
- package/src/tool/tool.ts +71 -0
- package/src/tool/webfetch.ts +198 -0
- package/src/tool/webfetch.txt +13 -0
- package/src/tool/websearch.ts +180 -0
- package/src/tool/websearch.txt +11 -0
- package/src/tool/write.ts +110 -0
- package/src/tool/write.txt +8 -0
- package/src/util/archive.ts +16 -0
- package/src/util/color.ts +19 -0
- package/src/util/context.ts +25 -0
- package/src/util/defer.ts +12 -0
- package/src/util/eventloop.ts +20 -0
- package/src/util/filesystem.ts +83 -0
- package/src/util/fn.ts +11 -0
- package/src/util/iife.ts +3 -0
- package/src/util/keybind.ts +102 -0
- package/src/util/lazy.ts +11 -0
- package/src/util/license.ts +325 -0
- package/src/util/locale.ts +81 -0
- package/src/util/lock.ts +98 -0
- package/src/util/log.ts +180 -0
- package/src/util/queue.ts +32 -0
- package/src/util/rpc.ts +42 -0
- package/src/util/scrap.ts +10 -0
- package/src/util/signal.ts +12 -0
- package/src/util/timeout.ts +14 -0
- package/src/util/token.ts +7 -0
- package/src/util/wildcard.ts +54 -0
- package/sst-env.d.ts +9 -0
- package/test/agent/agent.test.ts +146 -0
- package/test/bun.test.ts +53 -0
- package/test/cli/github-remote.test.ts +80 -0
- package/test/config/agent-color.test.ts +66 -0
- package/test/config/config.test.ts +535 -0
- package/test/config/markdown.test.ts +89 -0
- package/test/file/ignore.test.ts +10 -0
- package/test/fixture/fixture.ts +36 -0
- package/test/fixture/lsp/fake-lsp-server.js +77 -0
- package/test/ide/ide.test.ts +82 -0
- package/test/keybind.test.ts +421 -0
- package/test/lsp/client.test.ts +95 -0
- package/test/mcp/headers.test.ts +153 -0
- package/test/patch/patch.test.ts +348 -0
- package/test/preload.ts +57 -0
- package/test/project/project.test.ts +72 -0
- package/test/provider/provider.test.ts +1809 -0
- package/test/provider/transform.test.ts +411 -0
- package/test/session/retry.test.ts +111 -0
- package/test/session/session.test.ts +71 -0
- package/test/skill/skill.test.ts +131 -0
- package/test/snapshot/snapshot.test.ts +939 -0
- package/test/tool/__snapshots__/tool.test.ts.snap +9 -0
- package/test/tool/bash.test.ts +434 -0
- package/test/tool/grep.test.ts +108 -0
- package/test/tool/patch.test.ts +259 -0
- package/test/tool/read.test.ts +42 -0
- package/test/util/iife.test.ts +36 -0
- package/test/util/lazy.test.ts +50 -0
- package/test/util/timeout.test.ts +21 -0
- package/test/util/wildcard.test.ts +55 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,690 @@
|
|
|
1
|
+
import { BusEvent } from "@/bus/bus-event"
|
|
2
|
+
import { Bus } from "@/bus"
|
|
3
|
+
import z from "zod"
|
|
4
|
+
import { NamedError } from "@opencode-ai/util/error"
|
|
5
|
+
import { Message } from "./message"
|
|
6
|
+
import { APICallError, convertToModelMessages, LoadAPIKeyError, type ModelMessage, type UIMessage } from "ai"
|
|
7
|
+
import { Identifier } from "../id/id"
|
|
8
|
+
import { LSP } from "../lsp"
|
|
9
|
+
import { Snapshot } from "@/snapshot"
|
|
10
|
+
import { fn } from "@/util/fn"
|
|
11
|
+
import { Storage } from "@/storage/storage"
|
|
12
|
+
import { ProviderTransform } from "@/provider/transform"
|
|
13
|
+
import { STATUS_CODES } from "http"
|
|
14
|
+
import { iife } from "@/util/iife"
|
|
15
|
+
import { type SystemError } from "bun"
|
|
16
|
+
import { SensitiveFilter } from "./sensitive-filter"
|
|
17
|
+
|
|
18
|
+
export namespace MessageV2 {
|
|
19
|
+
export const OutputLengthError = NamedError.create("MessageOutputLengthError", z.object({}))
|
|
20
|
+
export const AbortedError = NamedError.create("MessageAbortedError", z.object({ message: z.string() }))
|
|
21
|
+
export const AuthError = NamedError.create(
|
|
22
|
+
"ProviderAuthError",
|
|
23
|
+
z.object({
|
|
24
|
+
providerID: z.string(),
|
|
25
|
+
message: z.string(),
|
|
26
|
+
}),
|
|
27
|
+
)
|
|
28
|
+
export const APIError = NamedError.create(
|
|
29
|
+
"APIError",
|
|
30
|
+
z.object({
|
|
31
|
+
message: z.string(),
|
|
32
|
+
statusCode: z.number().optional(),
|
|
33
|
+
isRetryable: z.boolean(),
|
|
34
|
+
responseHeaders: z.record(z.string(), z.string()).optional(),
|
|
35
|
+
responseBody: z.string().optional(),
|
|
36
|
+
metadata: z.record(z.string(), z.string()).optional(),
|
|
37
|
+
}),
|
|
38
|
+
)
|
|
39
|
+
export type APIError = z.infer<typeof APIError.Schema>
|
|
40
|
+
|
|
41
|
+
const PartBase = z.object({
|
|
42
|
+
id: z.string(),
|
|
43
|
+
sessionID: z.string(),
|
|
44
|
+
messageID: z.string(),
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
export const SnapshotPart = PartBase.extend({
|
|
48
|
+
type: z.literal("snapshot"),
|
|
49
|
+
snapshot: z.string(),
|
|
50
|
+
}).meta({
|
|
51
|
+
ref: "SnapshotPart",
|
|
52
|
+
})
|
|
53
|
+
export type SnapshotPart = z.infer<typeof SnapshotPart>
|
|
54
|
+
|
|
55
|
+
export const PatchPart = PartBase.extend({
|
|
56
|
+
type: z.literal("patch"),
|
|
57
|
+
hash: z.string(),
|
|
58
|
+
files: z.string().array(),
|
|
59
|
+
}).meta({
|
|
60
|
+
ref: "PatchPart",
|
|
61
|
+
})
|
|
62
|
+
export type PatchPart = z.infer<typeof PatchPart>
|
|
63
|
+
|
|
64
|
+
export const TextPart = PartBase.extend({
|
|
65
|
+
type: z.literal("text"),
|
|
66
|
+
text: z.string(),
|
|
67
|
+
synthetic: z.boolean().optional(),
|
|
68
|
+
ignored: z.boolean().optional(),
|
|
69
|
+
time: z
|
|
70
|
+
.object({
|
|
71
|
+
start: z.number(),
|
|
72
|
+
end: z.number().optional(),
|
|
73
|
+
})
|
|
74
|
+
.optional(),
|
|
75
|
+
metadata: z.record(z.string(), z.any()).optional(),
|
|
76
|
+
}).meta({
|
|
77
|
+
ref: "TextPart",
|
|
78
|
+
})
|
|
79
|
+
export type TextPart = z.infer<typeof TextPart>
|
|
80
|
+
|
|
81
|
+
export const ReasoningPart = PartBase.extend({
|
|
82
|
+
type: z.literal("reasoning"),
|
|
83
|
+
text: z.string(),
|
|
84
|
+
metadata: z.record(z.string(), z.any()).optional(),
|
|
85
|
+
time: z.object({
|
|
86
|
+
start: z.number(),
|
|
87
|
+
end: z.number().optional(),
|
|
88
|
+
}),
|
|
89
|
+
}).meta({
|
|
90
|
+
ref: "ReasoningPart",
|
|
91
|
+
})
|
|
92
|
+
export type ReasoningPart = z.infer<typeof ReasoningPart>
|
|
93
|
+
|
|
94
|
+
const FilePartSourceBase = z.object({
|
|
95
|
+
text: z
|
|
96
|
+
.object({
|
|
97
|
+
value: z.string(),
|
|
98
|
+
start: z.number().int(),
|
|
99
|
+
end: z.number().int(),
|
|
100
|
+
})
|
|
101
|
+
.meta({
|
|
102
|
+
ref: "FilePartSourceText",
|
|
103
|
+
}),
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
export const FileSource = FilePartSourceBase.extend({
|
|
107
|
+
type: z.literal("file"),
|
|
108
|
+
path: z.string(),
|
|
109
|
+
}).meta({
|
|
110
|
+
ref: "FileSource",
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
export const SymbolSource = FilePartSourceBase.extend({
|
|
114
|
+
type: z.literal("symbol"),
|
|
115
|
+
path: z.string(),
|
|
116
|
+
range: LSP.Range,
|
|
117
|
+
name: z.string(),
|
|
118
|
+
kind: z.number().int(),
|
|
119
|
+
}).meta({
|
|
120
|
+
ref: "SymbolSource",
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
export const FilePartSource = z.discriminatedUnion("type", [FileSource, SymbolSource]).meta({
|
|
124
|
+
ref: "FilePartSource",
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
export const FilePart = PartBase.extend({
|
|
128
|
+
type: z.literal("file"),
|
|
129
|
+
mime: z.string(),
|
|
130
|
+
filename: z.string().optional(),
|
|
131
|
+
url: z.string(),
|
|
132
|
+
source: FilePartSource.optional(),
|
|
133
|
+
}).meta({
|
|
134
|
+
ref: "FilePart",
|
|
135
|
+
})
|
|
136
|
+
export type FilePart = z.infer<typeof FilePart>
|
|
137
|
+
|
|
138
|
+
export const AgentPart = PartBase.extend({
|
|
139
|
+
type: z.literal("agent"),
|
|
140
|
+
name: z.string(),
|
|
141
|
+
source: z
|
|
142
|
+
.object({
|
|
143
|
+
value: z.string(),
|
|
144
|
+
start: z.number().int(),
|
|
145
|
+
end: z.number().int(),
|
|
146
|
+
})
|
|
147
|
+
.optional(),
|
|
148
|
+
}).meta({
|
|
149
|
+
ref: "AgentPart",
|
|
150
|
+
})
|
|
151
|
+
export type AgentPart = z.infer<typeof AgentPart>
|
|
152
|
+
|
|
153
|
+
export const CompactionPart = PartBase.extend({
|
|
154
|
+
type: z.literal("compaction"),
|
|
155
|
+
auto: z.boolean(),
|
|
156
|
+
}).meta({
|
|
157
|
+
ref: "CompactionPart",
|
|
158
|
+
})
|
|
159
|
+
export type CompactionPart = z.infer<typeof CompactionPart>
|
|
160
|
+
|
|
161
|
+
export const SubtaskPart = PartBase.extend({
|
|
162
|
+
type: z.literal("subtask"),
|
|
163
|
+
prompt: z.string(),
|
|
164
|
+
description: z.string(),
|
|
165
|
+
agent: z.string(),
|
|
166
|
+
command: z.string().optional(),
|
|
167
|
+
})
|
|
168
|
+
export type SubtaskPart = z.infer<typeof SubtaskPart>
|
|
169
|
+
|
|
170
|
+
export const RetryPart = PartBase.extend({
|
|
171
|
+
type: z.literal("retry"),
|
|
172
|
+
attempt: z.number(),
|
|
173
|
+
error: APIError.Schema,
|
|
174
|
+
time: z.object({
|
|
175
|
+
created: z.number(),
|
|
176
|
+
}),
|
|
177
|
+
}).meta({
|
|
178
|
+
ref: "RetryPart",
|
|
179
|
+
})
|
|
180
|
+
export type RetryPart = z.infer<typeof RetryPart>
|
|
181
|
+
|
|
182
|
+
export const StepStartPart = PartBase.extend({
|
|
183
|
+
type: z.literal("step-start"),
|
|
184
|
+
snapshot: z.string().optional(),
|
|
185
|
+
}).meta({
|
|
186
|
+
ref: "StepStartPart",
|
|
187
|
+
})
|
|
188
|
+
export type StepStartPart = z.infer<typeof StepStartPart>
|
|
189
|
+
|
|
190
|
+
export const StepFinishPart = PartBase.extend({
|
|
191
|
+
type: z.literal("step-finish"),
|
|
192
|
+
reason: z.string(),
|
|
193
|
+
snapshot: z.string().optional(),
|
|
194
|
+
cost: z.number(),
|
|
195
|
+
tokens: z.object({
|
|
196
|
+
input: z.number(),
|
|
197
|
+
output: z.number(),
|
|
198
|
+
reasoning: z.number(),
|
|
199
|
+
cache: z.object({
|
|
200
|
+
read: z.number(),
|
|
201
|
+
write: z.number(),
|
|
202
|
+
}),
|
|
203
|
+
}),
|
|
204
|
+
}).meta({
|
|
205
|
+
ref: "StepFinishPart",
|
|
206
|
+
})
|
|
207
|
+
export type StepFinishPart = z.infer<typeof StepFinishPart>
|
|
208
|
+
|
|
209
|
+
export const ToolStatePending = z
|
|
210
|
+
.object({
|
|
211
|
+
status: z.literal("pending"),
|
|
212
|
+
input: z.record(z.string(), z.any()),
|
|
213
|
+
raw: z.string(),
|
|
214
|
+
})
|
|
215
|
+
.meta({
|
|
216
|
+
ref: "ToolStatePending",
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
export type ToolStatePending = z.infer<typeof ToolStatePending>
|
|
220
|
+
|
|
221
|
+
export const ToolStateRunning = z
|
|
222
|
+
.object({
|
|
223
|
+
status: z.literal("running"),
|
|
224
|
+
input: z.record(z.string(), z.any()),
|
|
225
|
+
title: z.string().optional(),
|
|
226
|
+
metadata: z.record(z.string(), z.any()).optional(),
|
|
227
|
+
time: z.object({
|
|
228
|
+
start: z.number(),
|
|
229
|
+
}),
|
|
230
|
+
})
|
|
231
|
+
.meta({
|
|
232
|
+
ref: "ToolStateRunning",
|
|
233
|
+
})
|
|
234
|
+
export type ToolStateRunning = z.infer<typeof ToolStateRunning>
|
|
235
|
+
|
|
236
|
+
export const ToolStateCompleted = z
|
|
237
|
+
.object({
|
|
238
|
+
status: z.literal("completed"),
|
|
239
|
+
input: z.record(z.string(), z.any()),
|
|
240
|
+
output: z.string(),
|
|
241
|
+
title: z.string(),
|
|
242
|
+
metadata: z.record(z.string(), z.any()),
|
|
243
|
+
time: z.object({
|
|
244
|
+
start: z.number(),
|
|
245
|
+
end: z.number(),
|
|
246
|
+
compacted: z.number().optional(),
|
|
247
|
+
}),
|
|
248
|
+
attachments: FilePart.array().optional(),
|
|
249
|
+
})
|
|
250
|
+
.meta({
|
|
251
|
+
ref: "ToolStateCompleted",
|
|
252
|
+
})
|
|
253
|
+
export type ToolStateCompleted = z.infer<typeof ToolStateCompleted>
|
|
254
|
+
|
|
255
|
+
export const ToolStateError = z
|
|
256
|
+
.object({
|
|
257
|
+
status: z.literal("error"),
|
|
258
|
+
input: z.record(z.string(), z.any()),
|
|
259
|
+
error: z.string(),
|
|
260
|
+
metadata: z.record(z.string(), z.any()).optional(),
|
|
261
|
+
time: z.object({
|
|
262
|
+
start: z.number(),
|
|
263
|
+
end: z.number(),
|
|
264
|
+
}),
|
|
265
|
+
})
|
|
266
|
+
.meta({
|
|
267
|
+
ref: "ToolStateError",
|
|
268
|
+
})
|
|
269
|
+
export type ToolStateError = z.infer<typeof ToolStateError>
|
|
270
|
+
|
|
271
|
+
export const ToolState = z
|
|
272
|
+
.discriminatedUnion("status", [ToolStatePending, ToolStateRunning, ToolStateCompleted, ToolStateError])
|
|
273
|
+
.meta({
|
|
274
|
+
ref: "ToolState",
|
|
275
|
+
})
|
|
276
|
+
|
|
277
|
+
export const ToolPart = PartBase.extend({
|
|
278
|
+
type: z.literal("tool"),
|
|
279
|
+
callID: z.string(),
|
|
280
|
+
tool: z.string(),
|
|
281
|
+
state: ToolState,
|
|
282
|
+
metadata: z.record(z.string(), z.any()).optional(),
|
|
283
|
+
}).meta({
|
|
284
|
+
ref: "ToolPart",
|
|
285
|
+
})
|
|
286
|
+
export type ToolPart = z.infer<typeof ToolPart>
|
|
287
|
+
|
|
288
|
+
const Base = z.object({
|
|
289
|
+
id: z.string(),
|
|
290
|
+
sessionID: z.string(),
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
export const User = Base.extend({
|
|
294
|
+
role: z.literal("user"),
|
|
295
|
+
time: z.object({
|
|
296
|
+
created: z.number(),
|
|
297
|
+
}),
|
|
298
|
+
summary: z
|
|
299
|
+
.object({
|
|
300
|
+
title: z.string().optional(),
|
|
301
|
+
body: z.string().optional(),
|
|
302
|
+
diffs: Snapshot.FileDiff.array(),
|
|
303
|
+
})
|
|
304
|
+
.optional(),
|
|
305
|
+
agent: z.string(),
|
|
306
|
+
model: z.object({
|
|
307
|
+
providerID: z.string(),
|
|
308
|
+
modelID: z.string(),
|
|
309
|
+
}),
|
|
310
|
+
system: z.string().optional(),
|
|
311
|
+
tools: z.record(z.string(), z.boolean()).optional(),
|
|
312
|
+
}).meta({
|
|
313
|
+
ref: "UserMessage",
|
|
314
|
+
})
|
|
315
|
+
export type User = z.infer<typeof User>
|
|
316
|
+
|
|
317
|
+
export const Part = z
|
|
318
|
+
.discriminatedUnion("type", [
|
|
319
|
+
TextPart,
|
|
320
|
+
SubtaskPart,
|
|
321
|
+
ReasoningPart,
|
|
322
|
+
FilePart,
|
|
323
|
+
ToolPart,
|
|
324
|
+
StepStartPart,
|
|
325
|
+
StepFinishPart,
|
|
326
|
+
SnapshotPart,
|
|
327
|
+
PatchPart,
|
|
328
|
+
AgentPart,
|
|
329
|
+
RetryPart,
|
|
330
|
+
CompactionPart,
|
|
331
|
+
])
|
|
332
|
+
.meta({
|
|
333
|
+
ref: "Part",
|
|
334
|
+
})
|
|
335
|
+
export type Part = z.infer<typeof Part>
|
|
336
|
+
|
|
337
|
+
export const Assistant = Base.extend({
|
|
338
|
+
role: z.literal("assistant"),
|
|
339
|
+
time: z.object({
|
|
340
|
+
created: z.number(),
|
|
341
|
+
completed: z.number().optional(),
|
|
342
|
+
}),
|
|
343
|
+
error: z
|
|
344
|
+
.discriminatedUnion("name", [
|
|
345
|
+
AuthError.Schema,
|
|
346
|
+
NamedError.Unknown.Schema,
|
|
347
|
+
OutputLengthError.Schema,
|
|
348
|
+
AbortedError.Schema,
|
|
349
|
+
APIError.Schema,
|
|
350
|
+
])
|
|
351
|
+
.optional(),
|
|
352
|
+
parentID: z.string(),
|
|
353
|
+
modelID: z.string(),
|
|
354
|
+
providerID: z.string(),
|
|
355
|
+
/**
|
|
356
|
+
* @deprecated
|
|
357
|
+
*/
|
|
358
|
+
mode: z.string(),
|
|
359
|
+
agent: z.string(),
|
|
360
|
+
path: z.object({
|
|
361
|
+
cwd: z.string(),
|
|
362
|
+
root: z.string(),
|
|
363
|
+
}),
|
|
364
|
+
summary: z.boolean().optional(),
|
|
365
|
+
cost: z.number(),
|
|
366
|
+
tokens: z.object({
|
|
367
|
+
input: z.number(),
|
|
368
|
+
output: z.number(),
|
|
369
|
+
reasoning: z.number(),
|
|
370
|
+
cache: z.object({
|
|
371
|
+
read: z.number(),
|
|
372
|
+
write: z.number(),
|
|
373
|
+
}),
|
|
374
|
+
}),
|
|
375
|
+
finish: z.string().optional(),
|
|
376
|
+
}).meta({
|
|
377
|
+
ref: "AssistantMessage",
|
|
378
|
+
})
|
|
379
|
+
export type Assistant = z.infer<typeof Assistant>
|
|
380
|
+
|
|
381
|
+
export const Info = z.discriminatedUnion("role", [User, Assistant]).meta({
|
|
382
|
+
ref: "Message",
|
|
383
|
+
})
|
|
384
|
+
export type Info = z.infer<typeof Info>
|
|
385
|
+
|
|
386
|
+
export const Event = {
|
|
387
|
+
Updated: BusEvent.define(
|
|
388
|
+
"message.updated",
|
|
389
|
+
z.object({
|
|
390
|
+
info: Info,
|
|
391
|
+
}),
|
|
392
|
+
),
|
|
393
|
+
Removed: BusEvent.define(
|
|
394
|
+
"message.removed",
|
|
395
|
+
z.object({
|
|
396
|
+
sessionID: z.string(),
|
|
397
|
+
messageID: z.string(),
|
|
398
|
+
}),
|
|
399
|
+
),
|
|
400
|
+
PartUpdated: BusEvent.define(
|
|
401
|
+
"message.part.updated",
|
|
402
|
+
z.object({
|
|
403
|
+
part: Part,
|
|
404
|
+
delta: z.string().optional(),
|
|
405
|
+
}),
|
|
406
|
+
),
|
|
407
|
+
PartRemoved: BusEvent.define(
|
|
408
|
+
"message.part.removed",
|
|
409
|
+
z.object({
|
|
410
|
+
sessionID: z.string(),
|
|
411
|
+
messageID: z.string(),
|
|
412
|
+
partID: z.string(),
|
|
413
|
+
}),
|
|
414
|
+
),
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export const WithParts = z.object({
|
|
418
|
+
info: Info,
|
|
419
|
+
parts: z.array(Part),
|
|
420
|
+
})
|
|
421
|
+
export type WithParts = z.infer<typeof WithParts>
|
|
422
|
+
|
|
423
|
+
export interface ToModelMessageOptions {
|
|
424
|
+
/** Session ID for sensitive data filtering */
|
|
425
|
+
sessionID?: string
|
|
426
|
+
/** Whether to filter sensitive data (default: true) */
|
|
427
|
+
filterSensitive?: boolean
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
export function toModelMessage(input: WithParts[], options?: ToModelMessageOptions): ModelMessage[] {
|
|
431
|
+
const result: UIMessage[] = []
|
|
432
|
+
const sessionID = options?.sessionID ?? input[0]?.info.sessionID
|
|
433
|
+
const shouldFilter = options?.filterSensitive !== false && sessionID
|
|
434
|
+
|
|
435
|
+
// Helper to filter text if filtering is enabled
|
|
436
|
+
const filterText = (text: string): string => {
|
|
437
|
+
if (!shouldFilter) return text
|
|
438
|
+
return SensitiveFilter.filter(text, { sessionID }).text
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
for (const msg of input) {
|
|
442
|
+
if (msg.parts.length === 0) continue
|
|
443
|
+
|
|
444
|
+
if (msg.info.role === "user") {
|
|
445
|
+
const userMessage: UIMessage = {
|
|
446
|
+
id: msg.info.id,
|
|
447
|
+
role: "user",
|
|
448
|
+
parts: [],
|
|
449
|
+
}
|
|
450
|
+
result.push(userMessage)
|
|
451
|
+
for (const part of msg.parts) {
|
|
452
|
+
if (part.type === "text" && !part.ignored)
|
|
453
|
+
userMessage.parts.push({
|
|
454
|
+
type: "text",
|
|
455
|
+
text: filterText(part.text),
|
|
456
|
+
})
|
|
457
|
+
// text/plain and directory files are converted into text parts, ignore them
|
|
458
|
+
if (part.type === "file" && part.mime !== "text/plain" && part.mime !== "application/x-directory")
|
|
459
|
+
userMessage.parts.push({
|
|
460
|
+
type: "file",
|
|
461
|
+
url: part.url,
|
|
462
|
+
mediaType: part.mime,
|
|
463
|
+
filename: part.filename,
|
|
464
|
+
})
|
|
465
|
+
|
|
466
|
+
if (part.type === "compaction") {
|
|
467
|
+
userMessage.parts.push({
|
|
468
|
+
type: "text",
|
|
469
|
+
text: "What did we do so far?",
|
|
470
|
+
})
|
|
471
|
+
}
|
|
472
|
+
if (part.type === "subtask") {
|
|
473
|
+
userMessage.parts.push({
|
|
474
|
+
type: "text",
|
|
475
|
+
text: "The following tool was executed by the user",
|
|
476
|
+
})
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
if (msg.info.role === "assistant") {
|
|
482
|
+
if (
|
|
483
|
+
msg.info.error &&
|
|
484
|
+
!(
|
|
485
|
+
MessageV2.AbortedError.isInstance(msg.info.error) &&
|
|
486
|
+
msg.parts.some((part) => part.type !== "step-start" && part.type !== "reasoning")
|
|
487
|
+
)
|
|
488
|
+
) {
|
|
489
|
+
continue
|
|
490
|
+
}
|
|
491
|
+
const assistantMessage: UIMessage = {
|
|
492
|
+
id: msg.info.id,
|
|
493
|
+
role: "assistant",
|
|
494
|
+
parts: [],
|
|
495
|
+
}
|
|
496
|
+
result.push(assistantMessage)
|
|
497
|
+
for (const part of msg.parts) {
|
|
498
|
+
if (part.type === "text")
|
|
499
|
+
assistantMessage.parts.push({
|
|
500
|
+
type: "text",
|
|
501
|
+
text: filterText(part.text),
|
|
502
|
+
providerMetadata: part.metadata,
|
|
503
|
+
})
|
|
504
|
+
if (part.type === "step-start")
|
|
505
|
+
assistantMessage.parts.push({
|
|
506
|
+
type: "step-start",
|
|
507
|
+
})
|
|
508
|
+
if (part.type === "tool") {
|
|
509
|
+
if (part.state.status === "completed") {
|
|
510
|
+
if (part.state.attachments?.length) {
|
|
511
|
+
result.push({
|
|
512
|
+
id: Identifier.ascending("message"),
|
|
513
|
+
role: "user",
|
|
514
|
+
parts: [
|
|
515
|
+
{
|
|
516
|
+
type: "text",
|
|
517
|
+
text: `Tool ${part.tool} returned an attachment:`,
|
|
518
|
+
},
|
|
519
|
+
...part.state.attachments.map((attachment) => ({
|
|
520
|
+
type: "file" as const,
|
|
521
|
+
url: attachment.url,
|
|
522
|
+
mediaType: attachment.mime,
|
|
523
|
+
filename: attachment.filename,
|
|
524
|
+
})),
|
|
525
|
+
],
|
|
526
|
+
})
|
|
527
|
+
}
|
|
528
|
+
assistantMessage.parts.push({
|
|
529
|
+
type: ("tool-" + part.tool) as `tool-${string}`,
|
|
530
|
+
state: "output-available",
|
|
531
|
+
toolCallId: part.callID,
|
|
532
|
+
input: part.state.input,
|
|
533
|
+
output: part.state.time.compacted ? "[Old tool result content cleared]" : filterText(part.state.output),
|
|
534
|
+
callProviderMetadata: part.metadata,
|
|
535
|
+
})
|
|
536
|
+
}
|
|
537
|
+
if (part.state.status === "error")
|
|
538
|
+
assistantMessage.parts.push({
|
|
539
|
+
type: ("tool-" + part.tool) as `tool-${string}`,
|
|
540
|
+
state: "output-error",
|
|
541
|
+
toolCallId: part.callID,
|
|
542
|
+
input: part.state.input,
|
|
543
|
+
errorText: filterText(part.state.error),
|
|
544
|
+
callProviderMetadata: part.metadata,
|
|
545
|
+
})
|
|
546
|
+
}
|
|
547
|
+
if (part.type === "reasoning") {
|
|
548
|
+
assistantMessage.parts.push({
|
|
549
|
+
type: "reasoning",
|
|
550
|
+
text: filterText(part.text),
|
|
551
|
+
providerMetadata: part.metadata,
|
|
552
|
+
})
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
return convertToModelMessages(result.filter((msg) => msg.parts.length > 0))
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
export const stream = fn(Identifier.schema("session"), async function* (sessionID) {
|
|
562
|
+
const list = await Array.fromAsync(await Storage.list(["message", sessionID]))
|
|
563
|
+
for (let i = list.length - 1; i >= 0; i--) {
|
|
564
|
+
yield await get({
|
|
565
|
+
sessionID,
|
|
566
|
+
messageID: list[i][2],
|
|
567
|
+
})
|
|
568
|
+
}
|
|
569
|
+
})
|
|
570
|
+
|
|
571
|
+
export const parts = fn(Identifier.schema("message"), async (messageID) => {
|
|
572
|
+
const result = [] as MessageV2.Part[]
|
|
573
|
+
for (const item of await Storage.list(["part", messageID])) {
|
|
574
|
+
const read = await Storage.read<MessageV2.Part>(item)
|
|
575
|
+
result.push(read)
|
|
576
|
+
}
|
|
577
|
+
result.sort((a, b) => (a.id > b.id ? 1 : -1))
|
|
578
|
+
return result
|
|
579
|
+
})
|
|
580
|
+
|
|
581
|
+
export const get = fn(
|
|
582
|
+
z.object({
|
|
583
|
+
sessionID: Identifier.schema("session"),
|
|
584
|
+
messageID: Identifier.schema("message"),
|
|
585
|
+
}),
|
|
586
|
+
async (input) => {
|
|
587
|
+
return {
|
|
588
|
+
info: await Storage.read<MessageV2.Info>(["message", input.sessionID, input.messageID]),
|
|
589
|
+
parts: await parts(input.messageID),
|
|
590
|
+
}
|
|
591
|
+
},
|
|
592
|
+
)
|
|
593
|
+
|
|
594
|
+
export async function filterCompacted(stream: AsyncIterable<MessageV2.WithParts>) {
|
|
595
|
+
const result = [] as MessageV2.WithParts[]
|
|
596
|
+
const completed = new Set<string>()
|
|
597
|
+
for await (const msg of stream) {
|
|
598
|
+
result.push(msg)
|
|
599
|
+
if (
|
|
600
|
+
msg.info.role === "user" &&
|
|
601
|
+
completed.has(msg.info.id) &&
|
|
602
|
+
msg.parts.some((part) => part.type === "compaction")
|
|
603
|
+
)
|
|
604
|
+
break
|
|
605
|
+
if (msg.info.role === "assistant" && msg.info.summary && msg.info.finish) completed.add(msg.info.parentID)
|
|
606
|
+
}
|
|
607
|
+
result.reverse()
|
|
608
|
+
return result
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
export function fromError(e: unknown, ctx: { providerID: string }) {
|
|
612
|
+
switch (true) {
|
|
613
|
+
case e instanceof DOMException && e.name === "AbortError":
|
|
614
|
+
return new MessageV2.AbortedError(
|
|
615
|
+
{ message: e.message },
|
|
616
|
+
{
|
|
617
|
+
cause: e,
|
|
618
|
+
},
|
|
619
|
+
).toObject()
|
|
620
|
+
case MessageV2.OutputLengthError.isInstance(e):
|
|
621
|
+
return e
|
|
622
|
+
case LoadAPIKeyError.isInstance(e):
|
|
623
|
+
return new MessageV2.AuthError(
|
|
624
|
+
{
|
|
625
|
+
providerID: ctx.providerID,
|
|
626
|
+
message: e.message,
|
|
627
|
+
},
|
|
628
|
+
{ cause: e },
|
|
629
|
+
).toObject()
|
|
630
|
+
case (e as SystemError)?.code === "ECONNRESET":
|
|
631
|
+
return new MessageV2.APIError(
|
|
632
|
+
{
|
|
633
|
+
message: "Connection reset by server",
|
|
634
|
+
isRetryable: true,
|
|
635
|
+
metadata: {
|
|
636
|
+
code: (e as SystemError).code ?? "",
|
|
637
|
+
syscall: (e as SystemError).syscall ?? "",
|
|
638
|
+
message: (e as SystemError).message ?? "",
|
|
639
|
+
},
|
|
640
|
+
},
|
|
641
|
+
{ cause: e },
|
|
642
|
+
).toObject()
|
|
643
|
+
case APICallError.isInstance(e):
|
|
644
|
+
const message = iife(() => {
|
|
645
|
+
let msg = e.message
|
|
646
|
+
if (msg === "") {
|
|
647
|
+
if (e.responseBody) return e.responseBody
|
|
648
|
+
if (e.statusCode) {
|
|
649
|
+
const err = STATUS_CODES[e.statusCode]
|
|
650
|
+
if (err) return err
|
|
651
|
+
}
|
|
652
|
+
return "Unknown error"
|
|
653
|
+
}
|
|
654
|
+
const transformed = ProviderTransform.error(ctx.providerID, e)
|
|
655
|
+
if (transformed !== msg) {
|
|
656
|
+
return transformed
|
|
657
|
+
}
|
|
658
|
+
if (!e.responseBody || (e.statusCode && msg !== STATUS_CODES[e.statusCode])) {
|
|
659
|
+
return msg
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
try {
|
|
663
|
+
const body = JSON.parse(e.responseBody)
|
|
664
|
+
// try to extract common error message fields
|
|
665
|
+
const errMsg = body.message || body.error || body.error?.message
|
|
666
|
+
if (errMsg && typeof errMsg === "string") {
|
|
667
|
+
return `${msg}: ${errMsg}`
|
|
668
|
+
}
|
|
669
|
+
} catch {}
|
|
670
|
+
|
|
671
|
+
return `${msg}: ${e.responseBody}`
|
|
672
|
+
}).trim()
|
|
673
|
+
|
|
674
|
+
return new MessageV2.APIError(
|
|
675
|
+
{
|
|
676
|
+
message,
|
|
677
|
+
statusCode: e.statusCode,
|
|
678
|
+
isRetryable: e.isRetryable,
|
|
679
|
+
responseHeaders: e.responseHeaders,
|
|
680
|
+
responseBody: e.responseBody,
|
|
681
|
+
},
|
|
682
|
+
{ cause: e },
|
|
683
|
+
).toObject()
|
|
684
|
+
case e instanceof Error:
|
|
685
|
+
return new NamedError.Unknown({ message: e.toString() }, { cause: e }).toObject()
|
|
686
|
+
default:
|
|
687
|
+
return new NamedError.Unknown({ message: JSON.stringify(e) }, { cause: e })
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
}
|