onlycode 1.18.0 → 1.19.0
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 +11 -146
- package/src/account/account.ts +0 -463
- package/src/account/repo.ts +0 -173
- package/src/account/schema.ts +0 -99
- package/src/account/url.ts +0 -8
- package/src/acp/agent.ts +0 -95
- package/src/acp/config-option.ts +0 -203
- package/src/acp/content.ts +0 -250
- package/src/acp/directory.ts +0 -210
- package/src/acp/error.ts +0 -90
- package/src/acp/event.ts +0 -342
- package/src/acp/permission.ts +0 -124
- package/src/acp/profile.ts +0 -42
- package/src/acp/service.ts +0 -1048
- package/src/acp/session.ts +0 -231
- package/src/acp/tool.ts +0 -367
- package/src/acp/usage.ts +0 -232
- package/src/agent/agent.ts +0 -459
- package/src/agent/generate.txt +0 -75
- package/src/agent/prompt/compaction.txt +0 -9
- package/src/agent/prompt/explore.txt +0 -18
- package/src/agent/prompt/summary.txt +0 -11
- package/src/agent/prompt/title.txt +0 -44
- package/src/agent/subagent-permissions.ts +0 -27
- package/src/audio.d.ts +0 -14
- package/src/auth/index.ts +0 -99
- package/src/background/job.ts +0 -39
- package/src/bus/global.ts +0 -22
- package/src/cli/bootstrap.ts +0 -11
- package/src/cli/cmd/account.ts +0 -264
- package/src/cli/cmd/acp.ts +0 -73
- package/src/cli/cmd/agent.ts +0 -259
- package/src/cli/cmd/attach.ts +0 -97
- package/src/cli/cmd/cmd.ts +0 -7
- package/src/cli/cmd/db.ts +0 -62
- package/src/cli/cmd/debug/agent.handler.ts +0 -193
- package/src/cli/cmd/debug/agent.ts +0 -27
- package/src/cli/cmd/debug/config.ts +0 -14
- package/src/cli/cmd/debug/file.ts +0 -73
- package/src/cli/cmd/debug/index.ts +0 -87
- package/src/cli/cmd/debug/lsp.ts +0 -50
- package/src/cli/cmd/debug/ripgrep.ts +0 -79
- package/src/cli/cmd/debug/scrap.ts +0 -15
- package/src/cli/cmd/debug/skill.ts +0 -15
- package/src/cli/cmd/debug/snapshot.ts +0 -50
- package/src/cli/cmd/debug/startup.ts +0 -11
- package/src/cli/cmd/debug/v2.ts +0 -44
- package/src/cli/cmd/export.ts +0 -292
- package/src/cli/cmd/generate.ts +0 -54
- package/src/cli/cmd/github.handler.ts +0 -1593
- package/src/cli/cmd/github.shared.ts +0 -30
- package/src/cli/cmd/github.ts +0 -42
- package/src/cli/cmd/import.ts +0 -224
- package/src/cli/cmd/mcp.ts +0 -849
- package/src/cli/cmd/models.ts +0 -66
- package/src/cli/cmd/plug.ts +0 -230
- package/src/cli/cmd/pr.ts +0 -115
- package/src/cli/cmd/prompt-display.ts +0 -1
- package/src/cli/cmd/providers.ts +0 -534
- package/src/cli/cmd/run/demo.ts +0 -1274
- package/src/cli/cmd/run/entry.body.ts +0 -205
- package/src/cli/cmd/run/footer.command.tsx +0 -1064
- package/src/cli/cmd/run/footer.menu.tsx +0 -351
- package/src/cli/cmd/run/footer.permission.tsx +0 -472
- package/src/cli/cmd/run/footer.prompt.tsx +0 -1306
- package/src/cli/cmd/run/footer.question.tsx +0 -573
- package/src/cli/cmd/run/footer.subagent.tsx +0 -173
- package/src/cli/cmd/run/footer.ts +0 -1129
- package/src/cli/cmd/run/footer.view.tsx +0 -943
- package/src/cli/cmd/run/footer.width.ts +0 -27
- package/src/cli/cmd/run/permission.shared.ts +0 -256
- package/src/cli/cmd/run/prompt.editor.ts +0 -157
- package/src/cli/cmd/run/prompt.shared.ts +0 -153
- package/src/cli/cmd/run/question.shared.ts +0 -340
- package/src/cli/cmd/run/runtime.boot.ts +0 -202
- package/src/cli/cmd/run/runtime.lifecycle.ts +0 -406
- package/src/cli/cmd/run/runtime.queue.ts +0 -349
- package/src/cli/cmd/run/runtime.shared.ts +0 -17
- package/src/cli/cmd/run/runtime.stdin.ts +0 -37
- package/src/cli/cmd/run/runtime.ts +0 -814
- package/src/cli/cmd/run/scrollback.shared.ts +0 -92
- package/src/cli/cmd/run/scrollback.surface.ts +0 -431
- package/src/cli/cmd/run/scrollback.writer.tsx +0 -352
- package/src/cli/cmd/run/session-data.ts +0 -1113
- package/src/cli/cmd/run/session-replay.ts +0 -374
- package/src/cli/cmd/run/session.shared.ts +0 -196
- package/src/cli/cmd/run/splash.ts +0 -275
- package/src/cli/cmd/run/stream.transport.ts +0 -1462
- package/src/cli/cmd/run/stream.ts +0 -175
- package/src/cli/cmd/run/subagent-data.ts +0 -876
- package/src/cli/cmd/run/theme.ts +0 -690
- package/src/cli/cmd/run/tool.ts +0 -1489
- package/src/cli/cmd/run/trace.ts +0 -94
- package/src/cli/cmd/run/turn-summary.ts +0 -47
- package/src/cli/cmd/run/types.ts +0 -350
- package/src/cli/cmd/run/variant.shared.ts +0 -215
- package/src/cli/cmd/run.ts +0 -894
- package/src/cli/cmd/serve.ts +0 -24
- package/src/cli/cmd/session.ts +0 -147
- package/src/cli/cmd/stats.ts +0 -393
- package/src/cli/cmd/tui.ts +0 -224
- package/src/cli/cmd/uninstall.ts +0 -353
- package/src/cli/cmd/upgrade.ts +0 -74
- package/src/cli/cmd/web.ts +0 -84
- package/src/cli/effect/prompt.ts +0 -37
- package/src/cli/effect-cmd.ts +0 -96
- package/src/cli/error.ts +0 -130
- package/src/cli/heap.ts +0 -45
- package/src/cli/logo-pixel.ts +0 -35
- package/src/cli/logo.ts +0 -1
- package/src/cli/network.ts +0 -64
- package/src/cli/tui/layer.ts +0 -7
- package/src/cli/tui/validate-session.ts +0 -29
- package/src/cli/tui/worker.ts +0 -71
- package/src/cli/ui.ts +0 -148
- package/src/cli/upgrade.ts +0 -53
- package/src/command/index.ts +0 -184
- package/src/command/template/initialize.txt +0 -66
- package/src/command/template/review.txt +0 -101
- package/src/config/agent.ts +0 -59
- package/src/config/command.ts +0 -39
- package/src/config/config.ts +0 -686
- package/src/config/entry-name.ts +0 -19
- package/src/config/managed.ts +0 -69
- package/src/config/markdown.ts +0 -36
- package/src/config/parse.ts +0 -79
- package/src/config/paths.ts +0 -45
- package/src/config/plugin.ts +0 -79
- package/src/config/tui-cwd.ts +0 -5
- package/src/config/tui-host-attention.ts +0 -21
- package/src/config/tui-migrate.ts +0 -132
- package/src/config/tui.ts +0 -274
- package/src/config/variable.ts +0 -91
- package/src/control-plane/adapters/index.ts +0 -41
- package/src/control-plane/adapters/worktree.ts +0 -96
- package/src/control-plane/dev/README.md +0 -19
- package/src/control-plane/dev/debug-workspace-plugin.ts +0 -73
- package/src/control-plane/types.ts +0 -59
- package/src/control-plane/util.ts +0 -39
- package/src/control-plane/workspace-adapter-runtime.ts +0 -51
- package/src/control-plane/workspace-context.ts +0 -26
- package/src/control-plane/workspace.ts +0 -989
- package/src/effect/app-runtime.ts +0 -132
- package/src/effect/bootstrap-runtime.ts +0 -23
- package/src/effect/bridge.ts +0 -84
- package/src/effect/config-service.ts +0 -67
- package/src/effect/instance-ref.ts +0 -11
- package/src/effect/instance-registry.ts +0 -12
- package/src/effect/instance-state.ts +0 -69
- package/src/effect/promise.ts +0 -17
- package/src/effect/run-service.ts +0 -47
- package/src/effect/runner.ts +0 -217
- package/src/effect/runtime-flags.ts +0 -79
- package/src/env/index.ts +0 -43
- package/src/event-v2-bridge.ts +0 -79
- package/src/format/formatter.ts +0 -404
- package/src/format/index.ts +0 -205
- package/src/git/index.ts +0 -350
- package/src/id/id.ts +0 -80
- package/src/ide/index.ts +0 -61
- package/src/image/image.ts +0 -174
- package/src/index.ts +0 -142
- package/src/installation/index.ts +0 -350
- package/src/lsp/client.ts +0 -650
- package/src/lsp/diagnostic.ts +0 -29
- package/src/lsp/language.ts +0 -121
- package/src/lsp/launch.ts +0 -21
- package/src/lsp/lsp.ts +0 -511
- package/src/lsp/server.ts +0 -1983
- package/src/markdown.d.ts +0 -4
- package/src/mcp/auth.ts +0 -174
- package/src/mcp/catalog.ts +0 -153
- package/src/mcp/index.ts +0 -953
- package/src/mcp/oauth-callback.ts +0 -233
- package/src/mcp/oauth-provider.ts +0 -206
- package/src/node.ts +0 -4
- package/src/patch/index.ts +0 -686
- package/src/permission/arity.ts +0 -163
- package/src/permission/evaluate.ts +0 -1
- package/src/permission/index.ts +0 -230
- package/src/plugin/azure.ts +0 -26
- package/src/plugin/cloudflare.ts +0 -76
- package/src/plugin/digitalocean.ts +0 -383
- package/src/plugin/github-copilot/copilot.ts +0 -414
- package/src/plugin/github-copilot/models.ts +0 -246
- package/src/plugin/index.ts +0 -316
- package/src/plugin/install.ts +0 -439
- package/src/plugin/loader.ts +0 -237
- package/src/plugin/meta.ts +0 -188
- package/src/plugin/openai/README.md +0 -31
- package/src/plugin/openai/codex.ts +0 -641
- package/src/plugin/openai/ws-pool.ts +0 -270
- package/src/plugin/openai/ws.ts +0 -381
- package/src/plugin/pty-environment.ts +0 -24
- package/src/plugin/shared.ts +0 -323
- package/src/plugin/snowflake-cortex.ts +0 -529
- package/src/plugin/tui/internal.ts +0 -10
- package/src/plugin/tui/runtime.ts +0 -1130
- package/src/plugin/xai.ts +0 -716
- package/src/project/bootstrap-service.ts +0 -9
- package/src/project/bootstrap.ts +0 -76
- package/src/project/instance-context.ts +0 -24
- package/src/project/instance-layer.ts +0 -11
- package/src/project/instance-runtime.ts +0 -16
- package/src/project/instance-store.ts +0 -209
- package/src/project/project.ts +0 -519
- package/src/project/vcs.ts +0 -431
- package/src/provider/auth.ts +0 -233
- package/src/provider/error.ts +0 -188
- package/src/provider/model-status.ts +0 -8
- package/src/provider/provider.ts +0 -1975
- package/src/provider/transform.ts +0 -1543
- package/src/question/index.ts +0 -229
- package/src/question/schema.ts +0 -10
- package/src/server/auth.ts +0 -48
- package/src/server/event.ts +0 -13
- package/src/server/global-lifecycle.ts +0 -28
- package/src/server/init-projectors.ts +0 -3
- package/src/server/mdns.ts +0 -47
- package/src/server/projectors.ts +0 -1
- package/src/server/proxy-util.ts +0 -48
- package/src/server/routes/instance/httpapi/AGENTS.md +0 -39
- package/src/server/routes/instance/httpapi/api.ts +0 -78
- package/src/server/routes/instance/httpapi/errors.ts +0 -193
- package/src/server/routes/instance/httpapi/groups/config.ts +0 -65
- package/src/server/routes/instance/httpapi/groups/control-plane.ts +0 -35
- package/src/server/routes/instance/httpapi/groups/control.ts +0 -76
- package/src/server/routes/instance/httpapi/groups/event.ts +0 -29
- package/src/server/routes/instance/httpapi/groups/experimental.ts +0 -275
- package/src/server/routes/instance/httpapi/groups/file.ts +0 -185
- package/src/server/routes/instance/httpapi/groups/global.ts +0 -138
- package/src/server/routes/instance/httpapi/groups/instance.ts +0 -206
- package/src/server/routes/instance/httpapi/groups/mcp.ts +0 -156
- package/src/server/routes/instance/httpapi/groups/metadata.ts +0 -18
- package/src/server/routes/instance/httpapi/groups/permission.ts +0 -61
- package/src/server/routes/instance/httpapi/groups/project-copy.ts +0 -32
- package/src/server/routes/instance/httpapi/groups/project.ts +0 -93
- package/src/server/routes/instance/httpapi/groups/provider.ts +0 -101
- package/src/server/routes/instance/httpapi/groups/pty.ts +0 -172
- package/src/server/routes/instance/httpapi/groups/query.ts +0 -12
- package/src/server/routes/instance/httpapi/groups/question.ts +0 -74
- package/src/server/routes/instance/httpapi/groups/session.ts +0 -462
- package/src/server/routes/instance/httpapi/groups/sync.ts +0 -113
- package/src/server/routes/instance/httpapi/groups/tui.ts +0 -208
- package/src/server/routes/instance/httpapi/groups/workspace.ts +0 -141
- package/src/server/routes/instance/httpapi/handlers/config.ts +0 -34
- package/src/server/routes/instance/httpapi/handlers/control-plane.ts +0 -37
- package/src/server/routes/instance/httpapi/handlers/control.ts +0 -43
- package/src/server/routes/instance/httpapi/handlers/event.ts +0 -99
- package/src/server/routes/instance/httpapi/handlers/experimental.ts +0 -192
- package/src/server/routes/instance/httpapi/handlers/file.ts +0 -139
- package/src/server/routes/instance/httpapi/handlers/global.ts +0 -156
- package/src/server/routes/instance/httpapi/handlers/instance.ts +0 -110
- package/src/server/routes/instance/httpapi/handlers/mcp.ts +0 -111
- package/src/server/routes/instance/httpapi/handlers/permission.ts +0 -41
- package/src/server/routes/instance/httpapi/handlers/project-copy.ts +0 -83
- package/src/server/routes/instance/httpapi/handlers/project.ts +0 -63
- package/src/server/routes/instance/httpapi/handlers/provider.ts +0 -113
- package/src/server/routes/instance/httpapi/handlers/pty.ts +0 -273
- package/src/server/routes/instance/httpapi/handlers/question.ts +0 -54
- package/src/server/routes/instance/httpapi/handlers/session-errors.ts +0 -21
- package/src/server/routes/instance/httpapi/handlers/session.ts +0 -440
- package/src/server/routes/instance/httpapi/handlers/sync.ts +0 -89
- package/src/server/routes/instance/httpapi/handlers/tui.ts +0 -131
- package/src/server/routes/instance/httpapi/handlers/workspace.ts +0 -102
- package/src/server/routes/instance/httpapi/lifecycle.ts +0 -54
- package/src/server/routes/instance/httpapi/middleware/authorization.ts +0 -150
- package/src/server/routes/instance/httpapi/middleware/compression.ts +0 -64
- package/src/server/routes/instance/httpapi/middleware/cors-vary.ts +0 -29
- package/src/server/routes/instance/httpapi/middleware/error.ts +0 -43
- package/src/server/routes/instance/httpapi/middleware/fence.ts +0 -25
- package/src/server/routes/instance/httpapi/middleware/instance-context.ts +0 -43
- package/src/server/routes/instance/httpapi/middleware/proxy.ts +0 -108
- package/src/server/routes/instance/httpapi/middleware/schema-error.ts +0 -41
- package/src/server/routes/instance/httpapi/middleware/workspace-routing.ts +0 -250
- package/src/server/routes/instance/httpapi/public.ts +0 -535
- package/src/server/routes/instance/httpapi/server.ts +0 -298
- package/src/server/routes/instance/httpapi/websocket-tracker.ts +0 -57
- package/src/server/server.ts +0 -225
- package/src/server/shared/fence.ts +0 -60
- package/src/server/shared/pty-ticket.ts +0 -15
- package/src/server/shared/public-ui.ts +0 -12
- package/src/server/shared/tui-control.ts +0 -28
- package/src/server/shared/ui.ts +0 -108
- package/src/server/shared/workspace-routing.ts +0 -38
- package/src/server/tui-event.ts +0 -53
- package/src/session/compaction.ts +0 -620
- package/src/session/instruction.ts +0 -241
- package/src/session/llm/AGENTS.md +0 -90
- package/src/session/llm/ai-sdk.ts +0 -288
- package/src/session/llm/native-request.ts +0 -196
- package/src/session/llm/native-runtime.ts +0 -195
- package/src/session/llm/request.ts +0 -216
- package/src/session/llm.ts +0 -415
- package/src/session/message-error.ts +0 -14
- package/src/session/message-v2.ts +0 -747
- package/src/session/message.ts +0 -148
- package/src/session/overflow.ts +0 -34
- package/src/session/processor.ts +0 -1084
- package/src/session/prompt/anthropic.txt +0 -109
- package/src/session/prompt/beast.txt +0 -151
- package/src/session/prompt/build-switch.txt +0 -9
- package/src/session/prompt/codex.txt +0 -83
- package/src/session/prompt/copilot-gpt-5.txt +0 -147
- package/src/session/prompt/default.txt +0 -99
- package/src/session/prompt/gemini.txt +0 -159
- package/src/session/prompt/gpt.txt +0 -111
- package/src/session/prompt/kimi.txt +0 -99
- package/src/session/prompt/plan-mode.txt +0 -74
- package/src/session/prompt/plan-reminder-anthropic.txt +0 -71
- package/src/session/prompt/plan.txt +0 -30
- package/src/session/prompt/trinity.txt +0 -101
- package/src/session/prompt.ts +0 -1707
- package/src/session/reminders.ts +0 -92
- package/src/session/retry.ts +0 -201
- package/src/session/revert.ts +0 -160
- package/src/session/run-state.ts +0 -156
- package/src/session/schema.ts +0 -26
- package/src/session/session.ts +0 -1119
- package/src/session/status.ts +0 -97
- package/src/session/summary.ts +0 -165
- package/src/session/system.ts +0 -117
- package/src/session/todo.ts +0 -90
- package/src/session/tools.ts +0 -207
- package/src/share/session.ts +0 -61
- package/src/share/share-next.ts +0 -385
- package/src/skill/discovery.ts +0 -109
- package/src/skill/index.ts +0 -366
- package/src/snapshot/index.ts +0 -808
- package/src/sql.d.ts +0 -4
- package/src/storage/schema.ts +0 -5
- package/src/storage/storage.ts +0 -329
- package/src/sync/README.md +0 -179
- package/src/sync/schema.ts +0 -11
- package/src/temporary.ts +0 -31
- package/src/tool/apply_patch.ts +0 -315
- package/src/tool/apply_patch.txt +0 -33
- package/src/tool/apply_patch.zh.txt +0 -33
- package/src/tool/description.ts +0 -100
- package/src/tool/edit.ts +0 -739
- package/src/tool/edit.txt +0 -10
- package/src/tool/edit.zh.txt +0 -10
- package/src/tool/external-directory.ts +0 -49
- package/src/tool/glob.ts +0 -78
- package/src/tool/glob.txt +0 -6
- package/src/tool/glob.zh.txt +0 -6
- package/src/tool/grep.ts +0 -114
- package/src/tool/grep.txt +0 -8
- package/src/tool/grep.zh.txt +0 -8
- package/src/tool/invalid.ts +0 -21
- package/src/tool/json-schema.ts +0 -164
- package/src/tool/lsp.ts +0 -115
- package/src/tool/lsp.txt +0 -24
- package/src/tool/lsp.zh.txt +0 -24
- package/src/tool/mcp-websearch.ts +0 -96
- package/src/tool/plan-enter.txt +0 -14
- package/src/tool/plan-enter.zh.txt +0 -14
- package/src/tool/plan-exit.txt +0 -13
- package/src/tool/plan-exit.zh.txt +0 -13
- package/src/tool/plan.ts +0 -81
- package/src/tool/question.ts +0 -46
- package/src/tool/question.txt +0 -10
- package/src/tool/question.zh.txt +0 -10
- package/src/tool/read.ts +0 -388
- package/src/tool/read.txt +0 -14
- package/src/tool/read.zh.txt +0 -14
- package/src/tool/registry.ts +0 -440
- package/src/tool/schema.ts +0 -14
- package/src/tool/shell/id.ts +0 -19
- package/src/tool/shell/prompt.ts +0 -307
- package/src/tool/shell/shell.txt +0 -21
- package/src/tool/shell.ts +0 -657
- package/src/tool/skill.ts +0 -73
- package/src/tool/skill.txt +0 -5
- package/src/tool/skill.zh.txt +0 -5
- package/src/tool/task.ts +0 -348
- package/src/tool/task.txt +0 -19
- package/src/tool/task.zh.txt +0 -19
- package/src/tool/todo.ts +0 -59
- package/src/tool/todowrite.txt +0 -44
- package/src/tool/todowrite.zh.txt +0 -44
- package/src/tool/tool.ts +0 -183
- package/src/tool/truncate.ts +0 -158
- package/src/tool/truncation-dir.ts +0 -4
- package/src/tool/webfetch.ts +0 -194
- package/src/tool/webfetch.txt +0 -13
- package/src/tool/webfetch.zh.txt +0 -13
- package/src/tool/websearch.ts +0 -145
- package/src/tool/websearch.txt +0 -14
- package/src/tool/websearch.zh.txt +0 -14
- package/src/tool/write.ts +0 -106
- package/src/tool/write.txt +0 -8
- package/src/tool/write.zh.txt +0 -8
- package/src/util/archive.ts +0 -17
- package/src/util/bom.ts +0 -27
- package/src/util/data-url.ts +0 -9
- package/src/util/defer.ts +0 -10
- package/src/util/effect-http-client.ts +0 -11
- package/src/util/error.ts +0 -1
- package/src/util/filesystem.ts +0 -251
- package/src/util/html.ts +0 -8
- package/src/util/iife.ts +0 -3
- package/src/util/lazy.ts +0 -20
- package/src/util/local-context.ts +0 -25
- package/src/util/locale.ts +0 -2
- package/src/util/media.ts +0 -26
- package/src/util/process.ts +0 -177
- package/src/util/proxy-env.ts +0 -72
- package/src/util/queue.ts +0 -32
- package/src/util/record.ts +0 -1
- package/src/util/repository.ts +0 -232
- package/src/util/rpc.ts +0 -66
- package/src/util/signal.ts +0 -12
- package/src/util/timeout.ts +0 -13
- package/src/util/token.ts +0 -1
- package/src/util/wildcard.ts +0 -59
- package/src/worktree/index.ts +0 -654
|
@@ -1,747 +0,0 @@
|
|
|
1
|
-
import { EventV2 } from "@opencode-ai/core/event"
|
|
2
|
-
import { SessionID, MessageID, PartID } from "./schema"
|
|
3
|
-
import { SessionV1 } from "@opencode-ai/core/v1/session"
|
|
4
|
-
import { ProviderV2 } from "@opencode-ai/core/provider"
|
|
5
|
-
import {
|
|
6
|
-
APIError,
|
|
7
|
-
AbortedError,
|
|
8
|
-
Assistant,
|
|
9
|
-
AuthError,
|
|
10
|
-
CompactionPart,
|
|
11
|
-
ContextOverflowError,
|
|
12
|
-
Info,
|
|
13
|
-
OutputLengthError,
|
|
14
|
-
Part,
|
|
15
|
-
StructuredOutputError,
|
|
16
|
-
SubtaskPart,
|
|
17
|
-
User,
|
|
18
|
-
WithParts,
|
|
19
|
-
type ToolPart,
|
|
20
|
-
} from "@opencode-ai/core/v1/session"
|
|
21
|
-
|
|
22
|
-
import { NamedError } from "@opencode-ai/core/util/error"
|
|
23
|
-
import { APICallError, convertToModelMessages, LoadAPIKeyError, type ModelMessage, type UIMessage } from "ai"
|
|
24
|
-
import { Database } from "@opencode-ai/core/database/database"
|
|
25
|
-
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
|
26
|
-
import { NotFoundError } from "@/storage/storage"
|
|
27
|
-
import { and } from "drizzle-orm"
|
|
28
|
-
import { desc } from "drizzle-orm"
|
|
29
|
-
import { eq } from "drizzle-orm"
|
|
30
|
-
import { inArray } from "drizzle-orm"
|
|
31
|
-
import { lt } from "drizzle-orm"
|
|
32
|
-
import { or } from "drizzle-orm"
|
|
33
|
-
import { MessageTable, PartTable, SessionTable } from "@opencode-ai/core/session/sql"
|
|
34
|
-
import { ProviderError } from "@/provider/error"
|
|
35
|
-
import { iife } from "@/util/iife"
|
|
36
|
-
import { errorMessage } from "@/util/error"
|
|
37
|
-
import { isMedia } from "@/util/media"
|
|
38
|
-
import type { SystemError } from "bun"
|
|
39
|
-
import type { Provider } from "@/provider/provider"
|
|
40
|
-
import { Effect, Schema } from "effect"
|
|
41
|
-
|
|
42
|
-
export const node = LayerNode.group([Database.node])
|
|
43
|
-
|
|
44
|
-
/** Error shape thrown by Bun's fetch() when gzip/br decompression fails mid-stream */
|
|
45
|
-
interface FetchDecompressionError extends Error {
|
|
46
|
-
code: "ZlibError"
|
|
47
|
-
errno: number
|
|
48
|
-
path: string
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export const SYNTHETIC_ATTACHMENT_PROMPT = "Attached media from tool result:"
|
|
52
|
-
export { isMedia }
|
|
53
|
-
|
|
54
|
-
function truncateToolOutput(text: string, maxChars?: number) {
|
|
55
|
-
if (!maxChars || text.length <= maxChars) return text
|
|
56
|
-
const omitted = text.length - maxChars
|
|
57
|
-
return `${text.slice(0, maxChars)}\n[Tool output truncated for compaction: omitted ${omitted} chars]`
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export const Event = {
|
|
61
|
-
Updated: SessionV1.Event.MessageUpdated,
|
|
62
|
-
Removed: SessionV1.Event.MessageRemoved,
|
|
63
|
-
PartUpdated: SessionV1.Event.PartUpdated,
|
|
64
|
-
PartDelta: EventV2.define({
|
|
65
|
-
type: "message.part.delta",
|
|
66
|
-
schema: {
|
|
67
|
-
sessionID: SessionID,
|
|
68
|
-
messageID: MessageID,
|
|
69
|
-
partID: PartID,
|
|
70
|
-
field: Schema.String,
|
|
71
|
-
delta: Schema.String,
|
|
72
|
-
},
|
|
73
|
-
}),
|
|
74
|
-
PartRemoved: SessionV1.Event.PartRemoved,
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const Cursor = Schema.Struct({
|
|
78
|
-
id: MessageID,
|
|
79
|
-
time: Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0)),
|
|
80
|
-
})
|
|
81
|
-
type Cursor = typeof Cursor.Type
|
|
82
|
-
|
|
83
|
-
const decodeCursor = Schema.decodeUnknownSync(Cursor)
|
|
84
|
-
|
|
85
|
-
export const cursor = {
|
|
86
|
-
encode(input: Cursor) {
|
|
87
|
-
return Buffer.from(JSON.stringify(input)).toString("base64url")
|
|
88
|
-
},
|
|
89
|
-
decode(input: string) {
|
|
90
|
-
return decodeCursor(JSON.parse(Buffer.from(input, "base64url").toString("utf8")))
|
|
91
|
-
},
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const info = (row: typeof MessageTable.$inferSelect) =>
|
|
95
|
-
({
|
|
96
|
-
...row.data,
|
|
97
|
-
id: row.id,
|
|
98
|
-
sessionID: row.session_id,
|
|
99
|
-
}) as Info
|
|
100
|
-
|
|
101
|
-
const part = (row: typeof PartTable.$inferSelect) =>
|
|
102
|
-
({
|
|
103
|
-
...row.data,
|
|
104
|
-
id: row.id,
|
|
105
|
-
sessionID: row.session_id,
|
|
106
|
-
messageID: row.message_id,
|
|
107
|
-
}) as Part
|
|
108
|
-
|
|
109
|
-
const older = (row: Cursor) =>
|
|
110
|
-
or(lt(MessageTable.time_created, row.time), and(eq(MessageTable.time_created, row.time), lt(MessageTable.id, row.id)))
|
|
111
|
-
|
|
112
|
-
function hydrate(db: Database.Interface["db"], rows: (typeof MessageTable.$inferSelect)[]) {
|
|
113
|
-
const ids = rows.map((row) => row.id)
|
|
114
|
-
const partByMessage = new Map<string, Part[]>()
|
|
115
|
-
return Effect.gen(function* () {
|
|
116
|
-
if (ids.length > 0) {
|
|
117
|
-
const partRows = yield* db
|
|
118
|
-
.select()
|
|
119
|
-
.from(PartTable)
|
|
120
|
-
.where(inArray(PartTable.message_id, ids))
|
|
121
|
-
.orderBy(PartTable.message_id, PartTable.id)
|
|
122
|
-
.all()
|
|
123
|
-
.pipe(Effect.orDie)
|
|
124
|
-
for (const row of partRows) {
|
|
125
|
-
const next = part(row)
|
|
126
|
-
const list = partByMessage.get(row.message_id)
|
|
127
|
-
if (list) list.push(next)
|
|
128
|
-
else partByMessage.set(row.message_id, [next])
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return rows.map((row) => ({
|
|
133
|
-
info: info(row),
|
|
134
|
-
parts: partByMessage.get(row.id) ?? [],
|
|
135
|
-
}))
|
|
136
|
-
})
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function providerMeta(metadata: Record<string, any> | undefined) {
|
|
140
|
-
if (!metadata) return undefined
|
|
141
|
-
const { providerExecuted: _, ...rest } = metadata
|
|
142
|
-
return Object.keys(rest).length > 0 ? rest : undefined
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export const toModelMessagesEffect = Effect.fnUntraced(function* (
|
|
146
|
-
input: WithParts[],
|
|
147
|
-
model: Provider.Model,
|
|
148
|
-
options?: { stripMedia?: boolean; toolOutputMaxChars?: number },
|
|
149
|
-
) {
|
|
150
|
-
const result: UIMessage[] = []
|
|
151
|
-
const toolNames = new Set<string>()
|
|
152
|
-
// Track media from tool results that need to be injected as user messages
|
|
153
|
-
// for providers that don't support that media type in tool results.
|
|
154
|
-
//
|
|
155
|
-
// OpenAI-compatible APIs only support string content in tool results, so we need
|
|
156
|
-
// to extract media and inject as user messages. Some SDKs only support a subset
|
|
157
|
-
// of media in tool results; e.g. Bedrock supports images but not PDFs there.
|
|
158
|
-
//
|
|
159
|
-
// Only apply this workaround if the model actually supports that media input -
|
|
160
|
-
// otherwise unsupportedParts() will turn it into a user-visible error.
|
|
161
|
-
const supportsMediaInToolResult = (attachment: { mime: string }) => {
|
|
162
|
-
if (model.api.npm === "@ai-sdk/anthropic") return true
|
|
163
|
-
if (model.api.npm === "@ai-sdk/openai") return true
|
|
164
|
-
if (model.api.npm === "@ai-sdk/amazon-bedrock/mantle") return true
|
|
165
|
-
if (model.api.npm === "@ai-sdk/amazon-bedrock") return attachment.mime.startsWith("image/")
|
|
166
|
-
if (model.api.npm === "@ai-sdk/xai") return attachment.mime.startsWith("image/")
|
|
167
|
-
if (model.api.npm === "@ai-sdk/google-vertex/anthropic") return true
|
|
168
|
-
if (model.api.npm === "@ai-sdk/google") {
|
|
169
|
-
const id = model.api.id.toLowerCase()
|
|
170
|
-
return id.includes("gemini-3") && !id.includes("gemini-2")
|
|
171
|
-
}
|
|
172
|
-
return false
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
const toModelOutput = (options: { toolCallId: string; input: unknown; output: unknown }) => {
|
|
176
|
-
const output = options.output
|
|
177
|
-
if (typeof output === "string") {
|
|
178
|
-
return { type: "text", value: output }
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
if (typeof output === "object") {
|
|
182
|
-
const outputObject = output as {
|
|
183
|
-
text: string
|
|
184
|
-
attachments?: Array<{ mime: string; url: string }>
|
|
185
|
-
}
|
|
186
|
-
const attachments = (outputObject.attachments ?? []).filter((attachment) => {
|
|
187
|
-
return attachment.url.startsWith("data:") && attachment.url.includes(",")
|
|
188
|
-
})
|
|
189
|
-
|
|
190
|
-
return {
|
|
191
|
-
type: "content",
|
|
192
|
-
value: [
|
|
193
|
-
...(outputObject.text ? [{ type: "text", text: outputObject.text }] : []),
|
|
194
|
-
...attachments.map((attachment) => ({
|
|
195
|
-
type: "media",
|
|
196
|
-
mediaType: attachment.mime,
|
|
197
|
-
data: iife(() => {
|
|
198
|
-
const commaIndex = attachment.url.indexOf(",")
|
|
199
|
-
return commaIndex === -1 ? attachment.url : attachment.url.slice(commaIndex + 1)
|
|
200
|
-
}),
|
|
201
|
-
})),
|
|
202
|
-
],
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return { type: "json", value: output as never }
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
for (const msg of input) {
|
|
210
|
-
if (msg.parts.length === 0) continue
|
|
211
|
-
|
|
212
|
-
if (msg.info.role === "user") {
|
|
213
|
-
const userMessage: UIMessage = {
|
|
214
|
-
id: msg.info.id,
|
|
215
|
-
role: "user",
|
|
216
|
-
parts: [],
|
|
217
|
-
}
|
|
218
|
-
for (const part of msg.parts) {
|
|
219
|
-
// User message parts should never be empty
|
|
220
|
-
if (part.type === "text" && !part.ignored && part.text !== "")
|
|
221
|
-
userMessage.parts.push({
|
|
222
|
-
type: "text",
|
|
223
|
-
text: part.text,
|
|
224
|
-
})
|
|
225
|
-
// text/plain and directory files are converted into text parts, ignore them
|
|
226
|
-
if (part.type === "file" && part.mime !== "text/plain" && part.mime !== "application/x-directory") {
|
|
227
|
-
if (options?.stripMedia && isMedia(part.mime)) {
|
|
228
|
-
userMessage.parts.push({
|
|
229
|
-
type: "text",
|
|
230
|
-
text: `[Attached ${part.mime}: ${part.filename ?? "file"}]`,
|
|
231
|
-
})
|
|
232
|
-
} else {
|
|
233
|
-
userMessage.parts.push({
|
|
234
|
-
type: "file",
|
|
235
|
-
url: part.url,
|
|
236
|
-
mediaType: part.mime,
|
|
237
|
-
filename: part.filename,
|
|
238
|
-
})
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
if (part.type === "compaction") {
|
|
243
|
-
userMessage.parts.push({
|
|
244
|
-
type: "text",
|
|
245
|
-
text: "What did we do so far?",
|
|
246
|
-
})
|
|
247
|
-
}
|
|
248
|
-
if (part.type === "subtask") {
|
|
249
|
-
userMessage.parts.push({
|
|
250
|
-
type: "text",
|
|
251
|
-
text: "The following tool was executed by the user",
|
|
252
|
-
})
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
if (userMessage.parts.length > 0) result.push(userMessage)
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
if (msg.info.role === "assistant") {
|
|
259
|
-
const differentModel = `${model.providerID}/${model.id}` !== `${msg.info.providerID}/${msg.info.modelID}`
|
|
260
|
-
const media: Array<{ mime: string; url: string; filename?: string }> = []
|
|
261
|
-
|
|
262
|
-
if (
|
|
263
|
-
msg.info.error &&
|
|
264
|
-
!(
|
|
265
|
-
AbortedError.isInstance(msg.info.error) &&
|
|
266
|
-
msg.parts.some((part) => part.type !== "step-start" && part.type !== "reasoning")
|
|
267
|
-
)
|
|
268
|
-
) {
|
|
269
|
-
continue
|
|
270
|
-
}
|
|
271
|
-
const assistantMessage: UIMessage = {
|
|
272
|
-
id: msg.info.id,
|
|
273
|
-
role: "assistant",
|
|
274
|
-
parts: [],
|
|
275
|
-
}
|
|
276
|
-
// Anthropic adaptive thinking can persist assistant turns like:
|
|
277
|
-
// step-start, reasoning(signature), text(""), step-start,
|
|
278
|
-
// reasoning(signature). The empty text part is a structural separator,
|
|
279
|
-
// but it does not carry the signature metadata itself. Dropping it shifts
|
|
280
|
-
// signed thinking positions after step-start splitting/provider regrouping;
|
|
281
|
-
// keeping it as "" is filtered by the AI SDK and rejected by Anthropic.
|
|
282
|
-
// It is unclear whether this shape originates in our stream processing,
|
|
283
|
-
// a proxy, or a lower-level library, but preserving a non-empty separator
|
|
284
|
-
// here is the only safe replay point we have.
|
|
285
|
-
// Use a single space so the separator survives replay without changing
|
|
286
|
-
// the neighboring signed reasoning blocks.
|
|
287
|
-
const hasSignedReasoning = msg.parts.some((part) => {
|
|
288
|
-
if (part.type !== "reasoning") return false
|
|
289
|
-
return part.metadata?.anthropic?.signature != null
|
|
290
|
-
})
|
|
291
|
-
for (const part of msg.parts) {
|
|
292
|
-
if (part.type === "text") {
|
|
293
|
-
const text = part.text === "" && hasSignedReasoning ? " " : part.text
|
|
294
|
-
assistantMessage.parts.push({
|
|
295
|
-
type: "text",
|
|
296
|
-
text,
|
|
297
|
-
...(differentModel ? {} : { providerMetadata: part.metadata }),
|
|
298
|
-
})
|
|
299
|
-
}
|
|
300
|
-
if (part.type === "step-start")
|
|
301
|
-
assistantMessage.parts.push({
|
|
302
|
-
type: "step-start",
|
|
303
|
-
})
|
|
304
|
-
if (part.type === "tool") {
|
|
305
|
-
toolNames.add(part.tool)
|
|
306
|
-
if (part.state.status === "completed") {
|
|
307
|
-
const outputText = part.state.time.compacted
|
|
308
|
-
? "[Old tool result content cleared]"
|
|
309
|
-
: truncateToolOutput(part.state.output, options?.toolOutputMaxChars)
|
|
310
|
-
const attachments = part.state.time.compacted || options?.stripMedia ? [] : (part.state.attachments ?? [])
|
|
311
|
-
|
|
312
|
-
// For providers that don't support media in tool results, extract media files
|
|
313
|
-
// (images, PDFs) to be sent as a separate user message
|
|
314
|
-
const mediaAttachments = attachments.filter((a) => isMedia(a.mime))
|
|
315
|
-
const extractedMedia = mediaAttachments.filter((a) => !supportsMediaInToolResult(a))
|
|
316
|
-
if (extractedMedia.length > 0) {
|
|
317
|
-
media.push(...extractedMedia)
|
|
318
|
-
}
|
|
319
|
-
const finalAttachments = attachments.filter((a) => !isMedia(a.mime) || supportsMediaInToolResult(a))
|
|
320
|
-
|
|
321
|
-
const output =
|
|
322
|
-
finalAttachments.length > 0
|
|
323
|
-
? {
|
|
324
|
-
text: outputText,
|
|
325
|
-
attachments: finalAttachments,
|
|
326
|
-
}
|
|
327
|
-
: outputText
|
|
328
|
-
|
|
329
|
-
assistantMessage.parts.push({
|
|
330
|
-
type: ("tool-" + part.tool) as `tool-${string}`,
|
|
331
|
-
state: "output-available",
|
|
332
|
-
toolCallId: part.callID,
|
|
333
|
-
input: part.state.input,
|
|
334
|
-
output,
|
|
335
|
-
...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}),
|
|
336
|
-
...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }),
|
|
337
|
-
})
|
|
338
|
-
}
|
|
339
|
-
if (part.state.status === "error") {
|
|
340
|
-
const output = part.state.metadata?.interrupted === true ? part.state.metadata.output : undefined
|
|
341
|
-
if (typeof output === "string") {
|
|
342
|
-
assistantMessage.parts.push({
|
|
343
|
-
type: ("tool-" + part.tool) as `tool-${string}`,
|
|
344
|
-
state: "output-available",
|
|
345
|
-
toolCallId: part.callID,
|
|
346
|
-
input: part.state.input,
|
|
347
|
-
output,
|
|
348
|
-
...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}),
|
|
349
|
-
...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }),
|
|
350
|
-
})
|
|
351
|
-
} else {
|
|
352
|
-
assistantMessage.parts.push({
|
|
353
|
-
type: ("tool-" + part.tool) as `tool-${string}`,
|
|
354
|
-
state: "output-error",
|
|
355
|
-
toolCallId: part.callID,
|
|
356
|
-
input: part.state.input,
|
|
357
|
-
errorText: part.state.error,
|
|
358
|
-
...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}),
|
|
359
|
-
...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }),
|
|
360
|
-
})
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
// Handle pending/running tool calls to prevent dangling tool_use blocks
|
|
364
|
-
// Anthropic/Claude APIs require every tool_use to have a corresponding tool_result
|
|
365
|
-
if (part.state.status === "pending" || part.state.status === "running")
|
|
366
|
-
assistantMessage.parts.push({
|
|
367
|
-
type: ("tool-" + part.tool) as `tool-${string}`,
|
|
368
|
-
state: "output-error",
|
|
369
|
-
toolCallId: part.callID,
|
|
370
|
-
input: part.state.input,
|
|
371
|
-
errorText: "[Tool execution was interrupted]",
|
|
372
|
-
...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}),
|
|
373
|
-
...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }),
|
|
374
|
-
})
|
|
375
|
-
}
|
|
376
|
-
if (part.type === "reasoning") {
|
|
377
|
-
if (differentModel) {
|
|
378
|
-
if (part.text.trim().length > 0)
|
|
379
|
-
assistantMessage.parts.push({
|
|
380
|
-
type: "text",
|
|
381
|
-
text: part.text,
|
|
382
|
-
})
|
|
383
|
-
continue
|
|
384
|
-
}
|
|
385
|
-
assistantMessage.parts.push({
|
|
386
|
-
type: "reasoning",
|
|
387
|
-
text: part.text,
|
|
388
|
-
providerMetadata: part.metadata,
|
|
389
|
-
})
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
if (assistantMessage.parts.length > 0) {
|
|
393
|
-
result.push(assistantMessage)
|
|
394
|
-
// Inject pending media as a user message for providers that don't support
|
|
395
|
-
// media (images, PDFs) in tool results
|
|
396
|
-
if (media.length > 0) {
|
|
397
|
-
result.push({
|
|
398
|
-
id: MessageID.ascending(),
|
|
399
|
-
role: "user",
|
|
400
|
-
parts: [
|
|
401
|
-
{
|
|
402
|
-
type: "text" as const,
|
|
403
|
-
text: SYNTHETIC_ATTACHMENT_PROMPT,
|
|
404
|
-
},
|
|
405
|
-
...media.map((attachment) => ({
|
|
406
|
-
type: "file" as const,
|
|
407
|
-
url: attachment.url,
|
|
408
|
-
mediaType: attachment.mime,
|
|
409
|
-
filename: attachment.filename,
|
|
410
|
-
})),
|
|
411
|
-
],
|
|
412
|
-
})
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
const tools = Object.fromEntries(Array.from(toolNames).map((toolName) => [toolName, { toModelOutput }]))
|
|
419
|
-
|
|
420
|
-
return yield* Effect.promise(() =>
|
|
421
|
-
convertToModelMessages(
|
|
422
|
-
result.filter((msg) => msg.parts.some((part) => part.type !== "step-start")),
|
|
423
|
-
{
|
|
424
|
-
//@ts-expect-error (convertToModelMessages expects a ToolSet but only actually needs tools[name]?.toModelOutput)
|
|
425
|
-
tools,
|
|
426
|
-
},
|
|
427
|
-
),
|
|
428
|
-
)
|
|
429
|
-
})
|
|
430
|
-
|
|
431
|
-
export function toModelMessages(
|
|
432
|
-
input: WithParts[],
|
|
433
|
-
model: Provider.Model,
|
|
434
|
-
options?: { stripMedia?: boolean; toolOutputMaxChars?: number },
|
|
435
|
-
): Promise<ModelMessage[]> {
|
|
436
|
-
return Effect.runPromise(toModelMessagesEffect(input, model, options))
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
export const page = Effect.fn("MessageV2.page")(function* (input: {
|
|
440
|
-
sessionID: SessionID
|
|
441
|
-
limit: number
|
|
442
|
-
before?: string
|
|
443
|
-
}) {
|
|
444
|
-
const { db } = yield* Database.Service
|
|
445
|
-
const before = input.before ? cursor.decode(input.before) : undefined
|
|
446
|
-
const where = before
|
|
447
|
-
? and(eq(MessageTable.session_id, input.sessionID), older(before))
|
|
448
|
-
: eq(MessageTable.session_id, input.sessionID)
|
|
449
|
-
const rows = yield* db
|
|
450
|
-
.select()
|
|
451
|
-
.from(MessageTable)
|
|
452
|
-
.where(where)
|
|
453
|
-
.orderBy(desc(MessageTable.time_created), desc(MessageTable.id))
|
|
454
|
-
.limit(input.limit + 1)
|
|
455
|
-
.all()
|
|
456
|
-
.pipe(Effect.orDie)
|
|
457
|
-
if (rows.length === 0) {
|
|
458
|
-
const row = yield* db
|
|
459
|
-
.select({ id: SessionTable.id })
|
|
460
|
-
.from(SessionTable)
|
|
461
|
-
.where(eq(SessionTable.id, input.sessionID))
|
|
462
|
-
.get()
|
|
463
|
-
.pipe(Effect.orDie)
|
|
464
|
-
if (!row) return yield* new NotFoundError({ message: `Session not found: ${input.sessionID}` })
|
|
465
|
-
return {
|
|
466
|
-
items: [] as WithParts[],
|
|
467
|
-
more: false,
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
const more = rows.length > input.limit
|
|
472
|
-
const slice = more ? rows.slice(0, input.limit) : rows
|
|
473
|
-
const items = yield* hydrate(db, slice)
|
|
474
|
-
items.reverse()
|
|
475
|
-
const tail = slice.at(-1)
|
|
476
|
-
return {
|
|
477
|
-
items,
|
|
478
|
-
more,
|
|
479
|
-
cursor: more && tail ? cursor.encode({ id: tail.id, time: tail.time_created }) : undefined,
|
|
480
|
-
}
|
|
481
|
-
})
|
|
482
|
-
|
|
483
|
-
export function stream(sessionID: SessionID) {
|
|
484
|
-
const size = 50
|
|
485
|
-
return Effect.gen(function* () {
|
|
486
|
-
const result = [] as WithParts[]
|
|
487
|
-
let before: string | undefined
|
|
488
|
-
while (true) {
|
|
489
|
-
const next = yield* page({ sessionID, limit: size, before }).pipe(
|
|
490
|
-
Effect.catchIf(NotFoundError.isInstance, () =>
|
|
491
|
-
Effect.succeed({ items: [] as WithParts[], more: false, cursor: undefined }),
|
|
492
|
-
),
|
|
493
|
-
)
|
|
494
|
-
if (next.items.length === 0) break
|
|
495
|
-
for (let i = next.items.length - 1; i >= 0; i--) {
|
|
496
|
-
const item = next.items[i]
|
|
497
|
-
if (item) result.push(item)
|
|
498
|
-
}
|
|
499
|
-
if (!next.more || !next.cursor) break
|
|
500
|
-
before = next.cursor
|
|
501
|
-
}
|
|
502
|
-
return result
|
|
503
|
-
})
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
export function parts(messageID: MessageID) {
|
|
507
|
-
return Effect.gen(function* () {
|
|
508
|
-
const { db } = yield* Database.Service
|
|
509
|
-
const rows = yield* db
|
|
510
|
-
.select()
|
|
511
|
-
.from(PartTable)
|
|
512
|
-
.where(eq(PartTable.message_id, messageID))
|
|
513
|
-
.orderBy(PartTable.id)
|
|
514
|
-
.all()
|
|
515
|
-
.pipe(Effect.orDie)
|
|
516
|
-
return rows.map(part)
|
|
517
|
-
})
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
export const get = Effect.fn("MessageV2.get")(function* (input: { sessionID: SessionID; messageID: MessageID }) {
|
|
521
|
-
const { db } = yield* Database.Service
|
|
522
|
-
const row = yield* db
|
|
523
|
-
.select()
|
|
524
|
-
.from(MessageTable)
|
|
525
|
-
.where(and(eq(MessageTable.id, input.messageID), eq(MessageTable.session_id, input.sessionID)))
|
|
526
|
-
.get()
|
|
527
|
-
.pipe(Effect.orDie)
|
|
528
|
-
if (!row) return yield* new NotFoundError({ message: `Message not found: ${input.messageID}` })
|
|
529
|
-
return {
|
|
530
|
-
info: info(row),
|
|
531
|
-
parts: yield* parts(input.messageID),
|
|
532
|
-
}
|
|
533
|
-
})
|
|
534
|
-
|
|
535
|
-
export function filterCompacted(msgs: Iterable<WithParts>) {
|
|
536
|
-
const result = [] as WithParts[]
|
|
537
|
-
const completed = new Set<string>()
|
|
538
|
-
let retain: MessageID | undefined
|
|
539
|
-
for (const msg of msgs) {
|
|
540
|
-
result.push(msg)
|
|
541
|
-
if (retain) {
|
|
542
|
-
if (msg.info.id === retain) break
|
|
543
|
-
continue
|
|
544
|
-
}
|
|
545
|
-
if (msg.info.role === "user" && completed.has(msg.info.id)) {
|
|
546
|
-
const part = msg.parts.find((item): item is CompactionPart => item.type === "compaction")
|
|
547
|
-
if (!part) continue
|
|
548
|
-
if (!part.tail_start_id) break
|
|
549
|
-
retain = part.tail_start_id
|
|
550
|
-
if (msg.info.id === retain) break
|
|
551
|
-
continue
|
|
552
|
-
}
|
|
553
|
-
if (msg.info.role === "user" && completed.has(msg.info.id) && msg.parts.some((part) => part.type === "compaction"))
|
|
554
|
-
break
|
|
555
|
-
if (msg.info.role === "assistant" && msg.info.summary && msg.info.finish && !msg.info.error)
|
|
556
|
-
completed.add(msg.info.parentID)
|
|
557
|
-
}
|
|
558
|
-
result.reverse()
|
|
559
|
-
const compactionIndex = result.findLastIndex(
|
|
560
|
-
(msg) =>
|
|
561
|
-
msg.info.role === "user" &&
|
|
562
|
-
msg.parts.some((item): item is CompactionPart => item.type === "compaction" && item.tail_start_id !== undefined),
|
|
563
|
-
)
|
|
564
|
-
const compaction = result[compactionIndex]
|
|
565
|
-
const part = compaction?.parts.find(
|
|
566
|
-
(item): item is CompactionPart => item.type === "compaction" && item.tail_start_id !== undefined,
|
|
567
|
-
)
|
|
568
|
-
const summaryIndex = compaction
|
|
569
|
-
? result.findIndex(
|
|
570
|
-
(msg, index) =>
|
|
571
|
-
index > compactionIndex &&
|
|
572
|
-
msg.info.role === "assistant" &&
|
|
573
|
-
msg.info.summary &&
|
|
574
|
-
msg.info.parentID === compaction.info.id,
|
|
575
|
-
)
|
|
576
|
-
: -1
|
|
577
|
-
const tailIndex = part?.tail_start_id ? result.findIndex((msg) => msg.info.id === part.tail_start_id) : -1
|
|
578
|
-
if (tailIndex >= 0 && tailIndex < compactionIndex && summaryIndex > compactionIndex) {
|
|
579
|
-
return [
|
|
580
|
-
...result.slice(compactionIndex, summaryIndex + 1),
|
|
581
|
-
...result.slice(tailIndex, compactionIndex),
|
|
582
|
-
...result.slice(summaryIndex + 1),
|
|
583
|
-
]
|
|
584
|
-
}
|
|
585
|
-
return result
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
export const filterCompactedEffect = Effect.fnUntraced(function* (sessionID: SessionID) {
|
|
589
|
-
return filterCompacted(yield* stream(sessionID))
|
|
590
|
-
})
|
|
591
|
-
|
|
592
|
-
// filterCompacted reorders messages for model consumption
|
|
593
|
-
// ([compaction-user, summary, ...retained tail..., continue-user]), so array
|
|
594
|
-
// position is not chronological. Derive each binding by max id (MessageID
|
|
595
|
-
// is monotonic via MessageID.ascending) so a pre-compaction overflowing tail
|
|
596
|
-
// assistant doesn't get mistaken for the most recent turn. tasks are
|
|
597
|
-
// compaction/subtask parts attached to user messages newer than the latest
|
|
598
|
-
// finished assistant — i.e. unprocessed work.
|
|
599
|
-
export function latest(msgs: WithParts[]) {
|
|
600
|
-
let user: User | undefined
|
|
601
|
-
let assistant: Assistant | undefined
|
|
602
|
-
let finished: Assistant | undefined
|
|
603
|
-
for (const msg of msgs) {
|
|
604
|
-
const info = msg.info
|
|
605
|
-
if (info.role === "user" && (!user || info.id > user.id)) user = info
|
|
606
|
-
if (info.role === "assistant" && (!assistant || info.id > assistant.id)) assistant = info
|
|
607
|
-
if (info.role === "assistant" && info.finish && (!finished || info.id > finished.id)) finished = info
|
|
608
|
-
}
|
|
609
|
-
const tasks = msgs.flatMap((m) =>
|
|
610
|
-
finished && m.info.id <= finished.id
|
|
611
|
-
? []
|
|
612
|
-
: m.parts.filter((p): p is CompactionPart | SubtaskPart => p.type === "compaction" || p.type === "subtask"),
|
|
613
|
-
)
|
|
614
|
-
return { user, assistant, finished, tasks }
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
export function fromError(
|
|
618
|
-
e: unknown,
|
|
619
|
-
ctx: { providerID: ProviderV2.ID; aborted?: boolean },
|
|
620
|
-
): NonNullable<Assistant["error"]> {
|
|
621
|
-
switch (true) {
|
|
622
|
-
case e instanceof DOMException && e.name === "AbortError":
|
|
623
|
-
return new AbortedError(
|
|
624
|
-
{ message: e.message },
|
|
625
|
-
{
|
|
626
|
-
cause: e,
|
|
627
|
-
},
|
|
628
|
-
).toObject()
|
|
629
|
-
case OutputLengthError.isInstance(e):
|
|
630
|
-
return e
|
|
631
|
-
case LoadAPIKeyError.isInstance(e):
|
|
632
|
-
return new AuthError(
|
|
633
|
-
{
|
|
634
|
-
providerID: ctx.providerID,
|
|
635
|
-
message: e.message,
|
|
636
|
-
},
|
|
637
|
-
{ cause: e },
|
|
638
|
-
).toObject()
|
|
639
|
-
case (e as SystemError)?.code === "ECONNRESET":
|
|
640
|
-
return new APIError(
|
|
641
|
-
{
|
|
642
|
-
message: "Connection reset by server",
|
|
643
|
-
isRetryable: true,
|
|
644
|
-
metadata: {
|
|
645
|
-
code: (e as SystemError).code ?? "",
|
|
646
|
-
syscall: (e as SystemError).syscall ?? "",
|
|
647
|
-
message: (e as SystemError).message ?? "",
|
|
648
|
-
},
|
|
649
|
-
},
|
|
650
|
-
{ cause: e },
|
|
651
|
-
).toObject()
|
|
652
|
-
case e instanceof Error && (e as FetchDecompressionError).code === "ZlibError":
|
|
653
|
-
if (ctx.aborted) {
|
|
654
|
-
return new AbortedError({ message: e.message }, { cause: e }).toObject()
|
|
655
|
-
}
|
|
656
|
-
return new APIError(
|
|
657
|
-
{
|
|
658
|
-
message: "Response decompression failed",
|
|
659
|
-
isRetryable: true,
|
|
660
|
-
metadata: {
|
|
661
|
-
code: (e as FetchDecompressionError).code,
|
|
662
|
-
message: e.message,
|
|
663
|
-
},
|
|
664
|
-
},
|
|
665
|
-
{ cause: e },
|
|
666
|
-
).toObject()
|
|
667
|
-
case e instanceof ProviderError.HeaderTimeoutError:
|
|
668
|
-
return new APIError(
|
|
669
|
-
{
|
|
670
|
-
message: e.message,
|
|
671
|
-
isRetryable: true,
|
|
672
|
-
metadata: {
|
|
673
|
-
code: e.name,
|
|
674
|
-
timeoutMs: String(e.ms),
|
|
675
|
-
},
|
|
676
|
-
},
|
|
677
|
-
{ cause: e },
|
|
678
|
-
).toObject()
|
|
679
|
-
case e instanceof ProviderError.ResponseStreamError:
|
|
680
|
-
return new APIError(
|
|
681
|
-
{
|
|
682
|
-
message: e.message,
|
|
683
|
-
isRetryable: true,
|
|
684
|
-
metadata: {
|
|
685
|
-
code: e.name,
|
|
686
|
-
},
|
|
687
|
-
},
|
|
688
|
-
{ cause: e },
|
|
689
|
-
).toObject()
|
|
690
|
-
case APICallError.isInstance(e):
|
|
691
|
-
const parsed = ProviderError.parseAPICallError({
|
|
692
|
-
providerID: ctx.providerID,
|
|
693
|
-
error: e,
|
|
694
|
-
})
|
|
695
|
-
if (parsed.type === "context_overflow") {
|
|
696
|
-
return new ContextOverflowError(
|
|
697
|
-
{
|
|
698
|
-
message: parsed.message,
|
|
699
|
-
responseBody: parsed.responseBody,
|
|
700
|
-
},
|
|
701
|
-
{ cause: e },
|
|
702
|
-
).toObject()
|
|
703
|
-
}
|
|
704
|
-
|
|
705
|
-
return new APIError(
|
|
706
|
-
{
|
|
707
|
-
message: parsed.message,
|
|
708
|
-
statusCode: parsed.statusCode,
|
|
709
|
-
isRetryable: parsed.isRetryable,
|
|
710
|
-
responseHeaders: parsed.responseHeaders,
|
|
711
|
-
responseBody: parsed.responseBody,
|
|
712
|
-
metadata: parsed.metadata,
|
|
713
|
-
},
|
|
714
|
-
{ cause: e },
|
|
715
|
-
).toObject()
|
|
716
|
-
case e instanceof Error:
|
|
717
|
-
return new NamedError.Unknown({ message: errorMessage(e) }, { cause: e }).toObject()
|
|
718
|
-
default:
|
|
719
|
-
try {
|
|
720
|
-
const parsed = ProviderError.parseStreamError(e)
|
|
721
|
-
if (parsed) {
|
|
722
|
-
if (parsed.type === "context_overflow") {
|
|
723
|
-
return new ContextOverflowError(
|
|
724
|
-
{
|
|
725
|
-
message: parsed.message,
|
|
726
|
-
responseBody: parsed.responseBody,
|
|
727
|
-
},
|
|
728
|
-
{ cause: e },
|
|
729
|
-
).toObject()
|
|
730
|
-
}
|
|
731
|
-
return new APIError(
|
|
732
|
-
{
|
|
733
|
-
message: parsed.message,
|
|
734
|
-
isRetryable: parsed.isRetryable,
|
|
735
|
-
responseBody: parsed.responseBody,
|
|
736
|
-
},
|
|
737
|
-
{
|
|
738
|
-
cause: e,
|
|
739
|
-
},
|
|
740
|
-
).toObject()
|
|
741
|
-
}
|
|
742
|
-
} catch {}
|
|
743
|
-
return new NamedError.Unknown({ message: JSON.stringify(e) }, { cause: e }).toObject()
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
|
|
747
|
-
export * as MessageV2 from "./message-v2"
|