chad-code 1.3.1 → 1.3.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/README.md +55 -6
- package/package.json +36 -98
- package/AGENTS.md +0 -27
- package/Dockerfile +0 -18
- package/README.npm.md +0 -64
- package/bunfig.toml +0 -7
- package/eslint.config.js +0 -29
- package/parsers-config.ts +0 -253
- package/script/build.ts +0 -167
- package/script/publish-registries.ts +0 -187
- package/script/publish.ts +0 -93
- package/script/schema.ts +0 -47
- package/src/acp/README.md +0 -164
- package/src/acp/agent.ts +0 -1086
- package/src/acp/session.ts +0 -101
- package/src/acp/types.ts +0 -22
- package/src/agent/agent.ts +0 -253
- package/src/agent/generate.txt +0 -75
- package/src/agent/prompt/compaction.txt +0 -12
- package/src/agent/prompt/explore.txt +0 -18
- package/src/agent/prompt/summary.txt +0 -11
- package/src/agent/prompt/title.txt +0 -36
- package/src/auth/index.ts +0 -70
- package/src/bun/index.ts +0 -130
- package/src/bus/bus-event.ts +0 -43
- package/src/bus/global.ts +0 -10
- package/src/bus/index.ts +0 -105
- package/src/cli/bootstrap.ts +0 -17
- package/src/cli/cmd/acp.ts +0 -69
- package/src/cli/cmd/agent.ts +0 -257
- package/src/cli/cmd/auth.ts +0 -132
- package/src/cli/cmd/cmd.ts +0 -7
- package/src/cli/cmd/debug/agent.ts +0 -28
- package/src/cli/cmd/debug/config.ts +0 -15
- package/src/cli/cmd/debug/file.ts +0 -91
- package/src/cli/cmd/debug/index.ts +0 -45
- package/src/cli/cmd/debug/lsp.ts +0 -48
- package/src/cli/cmd/debug/ripgrep.ts +0 -83
- 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 -48
- package/src/cli/cmd/export.ts +0 -88
- package/src/cli/cmd/generate.ts +0 -38
- package/src/cli/cmd/github.ts +0 -32
- package/src/cli/cmd/import.ts +0 -98
- package/src/cli/cmd/mcp.ts +0 -670
- package/src/cli/cmd/models.ts +0 -42
- package/src/cli/cmd/pr.ts +0 -112
- package/src/cli/cmd/run.ts +0 -374
- package/src/cli/cmd/serve.ts +0 -16
- package/src/cli/cmd/session.ts +0 -135
- package/src/cli/cmd/stats.ts +0 -402
- package/src/cli/cmd/tui/app.tsx +0 -705
- package/src/cli/cmd/tui/attach.ts +0 -32
- package/src/cli/cmd/tui/component/border.tsx +0 -21
- package/src/cli/cmd/tui/component/dialog-agent.tsx +0 -31
- package/src/cli/cmd/tui/component/dialog-command.tsx +0 -124
- package/src/cli/cmd/tui/component/dialog-mcp.tsx +0 -86
- package/src/cli/cmd/tui/component/dialog-model.tsx +0 -232
- package/src/cli/cmd/tui/component/dialog-provider.tsx +0 -228
- package/src/cli/cmd/tui/component/dialog-session-list.tsx +0 -115
- package/src/cli/cmd/tui/component/dialog-session-rename.tsx +0 -31
- package/src/cli/cmd/tui/component/dialog-stash.tsx +0 -86
- package/src/cli/cmd/tui/component/dialog-status.tsx +0 -162
- package/src/cli/cmd/tui/component/dialog-tag.tsx +0 -44
- package/src/cli/cmd/tui/component/dialog-theme-list.tsx +0 -50
- package/src/cli/cmd/tui/component/did-you-know.tsx +0 -85
- package/src/cli/cmd/tui/component/logo.tsx +0 -43
- package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +0 -654
- package/src/cli/cmd/tui/component/prompt/history.tsx +0 -108
- package/src/cli/cmd/tui/component/prompt/index.tsx +0 -1078
- package/src/cli/cmd/tui/component/prompt/stash.tsx +0 -101
- package/src/cli/cmd/tui/component/textarea-keybindings.ts +0 -73
- package/src/cli/cmd/tui/component/tips.ts +0 -92
- package/src/cli/cmd/tui/component/todo-item.tsx +0 -32
- package/src/cli/cmd/tui/context/args.tsx +0 -14
- package/src/cli/cmd/tui/context/directory.ts +0 -13
- package/src/cli/cmd/tui/context/exit.tsx +0 -23
- package/src/cli/cmd/tui/context/helper.tsx +0 -25
- package/src/cli/cmd/tui/context/keybind.tsx +0 -101
- package/src/cli/cmd/tui/context/kv.tsx +0 -49
- package/src/cli/cmd/tui/context/local.tsx +0 -392
- package/src/cli/cmd/tui/context/prompt.tsx +0 -18
- package/src/cli/cmd/tui/context/route.tsx +0 -46
- package/src/cli/cmd/tui/context/sdk.tsx +0 -75
- package/src/cli/cmd/tui/context/sync.tsx +0 -384
- package/src/cli/cmd/tui/context/theme/aura.json +0 -69
- package/src/cli/cmd/tui/context/theme/ayu.json +0 -80
- package/src/cli/cmd/tui/context/theme/catppuccin-frappe.json +0 -233
- package/src/cli/cmd/tui/context/theme/catppuccin-macchiato.json +0 -233
- package/src/cli/cmd/tui/context/theme/catppuccin.json +0 -112
- package/src/cli/cmd/tui/context/theme/chad.json +0 -245
- package/src/cli/cmd/tui/context/theme/cobalt2.json +0 -228
- package/src/cli/cmd/tui/context/theme/cursor.json +0 -249
- package/src/cli/cmd/tui/context/theme/dracula.json +0 -219
- package/src/cli/cmd/tui/context/theme/everforest.json +0 -241
- package/src/cli/cmd/tui/context/theme/flexoki.json +0 -237
- package/src/cli/cmd/tui/context/theme/github.json +0 -233
- package/src/cli/cmd/tui/context/theme/gruvbox.json +0 -95
- package/src/cli/cmd/tui/context/theme/kanagawa.json +0 -77
- package/src/cli/cmd/tui/context/theme/lucent-orng.json +0 -227
- package/src/cli/cmd/tui/context/theme/material.json +0 -235
- package/src/cli/cmd/tui/context/theme/matrix.json +0 -77
- package/src/cli/cmd/tui/context/theme/mercury.json +0 -252
- package/src/cli/cmd/tui/context/theme/monokai.json +0 -221
- package/src/cli/cmd/tui/context/theme/nightowl.json +0 -221
- package/src/cli/cmd/tui/context/theme/nord.json +0 -223
- package/src/cli/cmd/tui/context/theme/one-dark.json +0 -84
- package/src/cli/cmd/tui/context/theme/orng.json +0 -245
- package/src/cli/cmd/tui/context/theme/osaka-jade.json +0 -93
- package/src/cli/cmd/tui/context/theme/palenight.json +0 -222
- package/src/cli/cmd/tui/context/theme/rosepine.json +0 -234
- package/src/cli/cmd/tui/context/theme/solarized.json +0 -223
- package/src/cli/cmd/tui/context/theme/synthwave84.json +0 -226
- package/src/cli/cmd/tui/context/theme/tokyonight.json +0 -243
- package/src/cli/cmd/tui/context/theme/vercel.json +0 -245
- package/src/cli/cmd/tui/context/theme/vesper.json +0 -218
- package/src/cli/cmd/tui/context/theme/zenburn.json +0 -223
- package/src/cli/cmd/tui/context/theme.tsx +0 -1137
- package/src/cli/cmd/tui/event.ts +0 -46
- package/src/cli/cmd/tui/routes/home.tsx +0 -138
- package/src/cli/cmd/tui/routes/session/dialog-fork-from-timeline.tsx +0 -64
- package/src/cli/cmd/tui/routes/session/dialog-message.tsx +0 -109
- package/src/cli/cmd/tui/routes/session/dialog-subagent.tsx +0 -26
- package/src/cli/cmd/tui/routes/session/dialog-timeline.tsx +0 -47
- package/src/cli/cmd/tui/routes/session/footer.tsx +0 -88
- package/src/cli/cmd/tui/routes/session/header.tsx +0 -125
- package/src/cli/cmd/tui/routes/session/index.tsx +0 -1814
- package/src/cli/cmd/tui/routes/session/permission.tsx +0 -416
- package/src/cli/cmd/tui/routes/session/sidebar.tsx +0 -318
- package/src/cli/cmd/tui/spawn.ts +0 -48
- package/src/cli/cmd/tui/thread.ts +0 -111
- package/src/cli/cmd/tui/ui/dialog-alert.tsx +0 -57
- package/src/cli/cmd/tui/ui/dialog-confirm.tsx +0 -83
- package/src/cli/cmd/tui/ui/dialog-export-options.tsx +0 -204
- package/src/cli/cmd/tui/ui/dialog-help.tsx +0 -38
- package/src/cli/cmd/tui/ui/dialog-prompt.tsx +0 -77
- package/src/cli/cmd/tui/ui/dialog-select.tsx +0 -345
- package/src/cli/cmd/tui/ui/dialog.tsx +0 -171
- package/src/cli/cmd/tui/ui/link.tsx +0 -28
- package/src/cli/cmd/tui/ui/spinner.ts +0 -368
- package/src/cli/cmd/tui/ui/toast.tsx +0 -100
- package/src/cli/cmd/tui/util/clipboard.ts +0 -127
- package/src/cli/cmd/tui/util/editor.ts +0 -32
- package/src/cli/cmd/tui/util/signal.ts +0 -7
- package/src/cli/cmd/tui/util/terminal.ts +0 -114
- package/src/cli/cmd/tui/util/transcript.ts +0 -98
- package/src/cli/cmd/tui/worker.ts +0 -68
- package/src/cli/cmd/uninstall.ts +0 -344
- package/src/cli/cmd/upgrade.ts +0 -67
- package/src/cli/cmd/web.ts +0 -73
- package/src/cli/error.ts +0 -56
- package/src/cli/network.ts +0 -53
- package/src/cli/ui.ts +0 -87
- package/src/cli/upgrade.ts +0 -25
- package/src/command/index.ts +0 -131
- package/src/command/template/initialize.txt +0 -10
- package/src/command/template/review.txt +0 -97
- package/src/config/config.ts +0 -1124
- package/src/config/markdown.ts +0 -41
- package/src/env/index.ts +0 -26
- package/src/file/ignore.ts +0 -83
- package/src/file/index.ts +0 -411
- package/src/file/ripgrep.ts +0 -402
- package/src/file/time.ts +0 -64
- package/src/file/watcher.ts +0 -117
- package/src/flag/flag.ts +0 -52
- package/src/format/formatter.ts +0 -359
- package/src/format/index.ts +0 -137
- package/src/global/index.ts +0 -55
- package/src/id/id.ts +0 -73
- package/src/ide/index.ts +0 -77
- package/src/index.ts +0 -159
- package/src/installation/index.ts +0 -198
- package/src/lsp/client.ts +0 -252
- package/src/lsp/index.ts +0 -485
- package/src/lsp/language.ts +0 -119
- package/src/lsp/server.ts +0 -2023
- package/src/mcp/auth.ts +0 -135
- package/src/mcp/index.ts +0 -874
- package/src/mcp/oauth-callback.ts +0 -200
- package/src/mcp/oauth-provider.ts +0 -154
- package/src/patch/index.ts +0 -622
- package/src/permission/arity.ts +0 -163
- package/src/permission/index.ts +0 -210
- package/src/permission/next.ts +0 -268
- package/src/plugin/index.ts +0 -106
- package/src/project/bootstrap.ts +0 -31
- package/src/project/instance.ts +0 -78
- package/src/project/project.ts +0 -263
- package/src/project/state.ts +0 -65
- package/src/project/vcs.ts +0 -76
- package/src/provider/auth.ts +0 -143
- package/src/provider/models-macro.ts +0 -4
- package/src/provider/models.ts +0 -77
- package/src/provider/provider.ts +0 -516
- package/src/provider/transform.ts +0 -114
- package/src/pty/index.ts +0 -212
- package/src/server/error.ts +0 -36
- package/src/server/mdns.ts +0 -57
- package/src/server/project.ts +0 -79
- package/src/server/server.ts +0 -2866
- package/src/server/tui.ts +0 -71
- package/src/session/compaction.ts +0 -225
- package/src/session/index.ts +0 -469
- package/src/session/llm.ts +0 -213
- package/src/session/message-v2.ts +0 -742
- package/src/session/message.ts +0 -189
- package/src/session/processor.ts +0 -402
- package/src/session/prompt/anthropic-20250930.txt +0 -166
- package/src/session/prompt/anthropic.txt +0 -105
- package/src/session/prompt/anthropic_spoof.txt +0 -1
- package/src/session/prompt/beast.txt +0 -147
- package/src/session/prompt/build-switch.txt +0 -5
- package/src/session/prompt/codex.txt +0 -318
- package/src/session/prompt/copilot-gpt-5.txt +0 -143
- package/src/session/prompt/gemini.txt +0 -155
- package/src/session/prompt/max-steps.txt +0 -16
- package/src/session/prompt/plan-reminder-anthropic.txt +0 -67
- package/src/session/prompt/plan.txt +0 -26
- package/src/session/prompt/qwen.txt +0 -109
- package/src/session/prompt.ts +0 -1621
- package/src/session/retry.ts +0 -90
- package/src/session/revert.ts +0 -108
- package/src/session/status.ts +0 -76
- package/src/session/summary.ts +0 -194
- package/src/session/system.ts +0 -108
- package/src/session/todo.ts +0 -37
- package/src/share/share-next.ts +0 -194
- package/src/share/share.ts +0 -23
- package/src/shell/shell.ts +0 -67
- package/src/skill/index.ts +0 -1
- package/src/skill/skill.ts +0 -124
- package/src/snapshot/index.ts +0 -197
- package/src/storage/storage.ts +0 -226
- package/src/tool/bash.ts +0 -262
- package/src/tool/bash.txt +0 -116
- package/src/tool/batch.ts +0 -175
- package/src/tool/batch.txt +0 -24
- package/src/tool/codesearch.ts +0 -132
- package/src/tool/codesearch.txt +0 -12
- package/src/tool/edit.ts +0 -655
- package/src/tool/edit.txt +0 -10
- package/src/tool/glob.ts +0 -75
- package/src/tool/glob.txt +0 -6
- package/src/tool/grep.ts +0 -132
- package/src/tool/grep.txt +0 -8
- package/src/tool/invalid.ts +0 -17
- package/src/tool/ls.ts +0 -119
- package/src/tool/ls.txt +0 -1
- package/src/tool/lsp.ts +0 -94
- package/src/tool/lsp.txt +0 -19
- package/src/tool/multiedit.ts +0 -46
- package/src/tool/multiedit.txt +0 -41
- package/src/tool/patch.ts +0 -210
- package/src/tool/patch.txt +0 -1
- package/src/tool/read.ts +0 -191
- package/src/tool/read.txt +0 -12
- package/src/tool/registry.ts +0 -137
- package/src/tool/skill.ts +0 -77
- package/src/tool/task.ts +0 -167
- package/src/tool/task.txt +0 -60
- package/src/tool/todo.ts +0 -53
- package/src/tool/todoread.txt +0 -14
- package/src/tool/todowrite.txt +0 -167
- package/src/tool/tool.ts +0 -73
- package/src/tool/webfetch.ts +0 -182
- package/src/tool/webfetch.txt +0 -13
- package/src/tool/websearch.ts +0 -144
- package/src/tool/websearch.txt +0 -11
- package/src/tool/write.ts +0 -84
- package/src/tool/write.txt +0 -8
- package/src/util/archive.ts +0 -16
- package/src/util/color.ts +0 -19
- package/src/util/context.ts +0 -25
- package/src/util/defer.ts +0 -12
- package/src/util/eventloop.ts +0 -20
- package/src/util/filesystem.ts +0 -83
- package/src/util/fn.ts +0 -11
- package/src/util/iife.ts +0 -3
- package/src/util/keybind.ts +0 -102
- package/src/util/lazy.ts +0 -18
- package/src/util/locale.ts +0 -81
- package/src/util/lock.ts +0 -98
- package/src/util/log.ts +0 -180
- package/src/util/queue.ts +0 -32
- package/src/util/rpc.ts +0 -42
- package/src/util/scrap.ts +0 -10
- package/src/util/signal.ts +0 -12
- package/src/util/timeout.ts +0 -14
- package/src/util/token.ts +0 -7
- package/src/util/wildcard.ts +0 -54
- package/src/worktree/index.ts +0 -217
- package/sst-env.d.ts +0 -9
- package/test/agent/agent.test.ts +0 -448
- package/test/bun.test.ts +0 -53
- package/test/cli/github-action.test.ts +0 -129
- package/test/cli/github-remote.test.ts +0 -80
- package/test/cli/tui/transcript.test.ts +0 -297
- package/test/config/agent-color.test.ts +0 -66
- package/test/config/config.test.ts +0 -870
- package/test/config/markdown.test.ts +0 -89
- package/test/file/ignore.test.ts +0 -10
- package/test/file/path-traversal.test.ts +0 -115
- package/test/fixture/fixture.ts +0 -45
- package/test/fixture/lsp/fake-lsp-server.js +0 -77
- package/test/ide/ide.test.ts +0 -82
- package/test/keybind.test.ts +0 -421
- package/test/lsp/client.test.ts +0 -95
- package/test/mcp/headers.test.ts +0 -153
- package/test/patch/patch.test.ts +0 -348
- package/test/permission/arity.test.ts +0 -33
- package/test/permission/next.test.ts +0 -652
- package/test/preload.ts +0 -63
- package/test/project/project.test.ts +0 -120
- package/test/provider/amazon-bedrock.test.ts +0 -236
- package/test/provider/provider.test.ts +0 -2127
- package/test/provider/transform.test.ts +0 -980
- package/test/server/session-select.test.ts +0 -78
- package/test/session/compaction.test.ts +0 -251
- package/test/session/message-v2.test.ts +0 -570
- package/test/session/retry.test.ts +0 -131
- package/test/session/revert-compact.test.ts +0 -285
- package/test/session/session.test.ts +0 -71
- package/test/skill/skill.test.ts +0 -185
- package/test/snapshot/snapshot.test.ts +0 -939
- package/test/tool/__snapshots__/tool.test.ts.snap +0 -9
- package/test/tool/bash.test.ts +0 -232
- package/test/tool/grep.test.ts +0 -109
- package/test/tool/patch.test.ts +0 -261
- package/test/tool/read.test.ts +0 -167
- package/test/util/iife.test.ts +0 -36
- package/test/util/lazy.test.ts +0 -50
- package/test/util/timeout.test.ts +0 -21
- package/test/util/wildcard.test.ts +0 -55
- package/tsconfig.json +0 -16
- /package/{script/postinstall.mjs → postinstall.mjs} +0 -0
package/src/acp/agent.ts
DELETED
|
@@ -1,1086 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
RequestError,
|
|
3
|
-
type Agent as ACPAgent,
|
|
4
|
-
type AgentSideConnection,
|
|
5
|
-
type AuthenticateRequest,
|
|
6
|
-
type AuthMethod,
|
|
7
|
-
type CancelNotification,
|
|
8
|
-
type InitializeRequest,
|
|
9
|
-
type InitializeResponse,
|
|
10
|
-
type LoadSessionRequest,
|
|
11
|
-
type NewSessionRequest,
|
|
12
|
-
type PermissionOption,
|
|
13
|
-
type PlanEntry,
|
|
14
|
-
type PromptRequest,
|
|
15
|
-
type SetSessionModelRequest,
|
|
16
|
-
type SetSessionModeRequest,
|
|
17
|
-
type SetSessionModeResponse,
|
|
18
|
-
type ToolCallContent,
|
|
19
|
-
type ToolKind,
|
|
20
|
-
} from "@agentclientprotocol/sdk"
|
|
21
|
-
import { Log } from "../util/log"
|
|
22
|
-
import { ACPSessionManager } from "./session"
|
|
23
|
-
import type { ACPConfig, ACPSessionState } from "./types"
|
|
24
|
-
import { Provider } from "../provider/provider"
|
|
25
|
-
import { Agent as AgentModule } from "../agent/agent"
|
|
26
|
-
import { Installation } from "@/installation"
|
|
27
|
-
import { MessageV2 } from "@/session/message-v2"
|
|
28
|
-
import { Config } from "@/config/config"
|
|
29
|
-
import { Todo } from "@/session/todo"
|
|
30
|
-
import { z } from "zod"
|
|
31
|
-
import { LoadAPIKeyError } from "ai"
|
|
32
|
-
import type { OpencodeClient, SessionMessageResponse } from "@opencode-ai/sdk/v2"
|
|
33
|
-
|
|
34
|
-
export namespace ACP {
|
|
35
|
-
const log = Log.create({ service: "acp-agent" })
|
|
36
|
-
|
|
37
|
-
export async function init({ sdk: _sdk }: { sdk: OpencodeClient }) {
|
|
38
|
-
return {
|
|
39
|
-
create: (connection: AgentSideConnection, fullConfig: ACPConfig) => {
|
|
40
|
-
return new Agent(connection, fullConfig)
|
|
41
|
-
},
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export class Agent implements ACPAgent {
|
|
46
|
-
private connection: AgentSideConnection
|
|
47
|
-
private config: ACPConfig
|
|
48
|
-
private sdk: OpencodeClient
|
|
49
|
-
private sessionManager
|
|
50
|
-
|
|
51
|
-
constructor(connection: AgentSideConnection, config: ACPConfig) {
|
|
52
|
-
this.connection = connection
|
|
53
|
-
this.config = config
|
|
54
|
-
this.sdk = config.sdk
|
|
55
|
-
this.sessionManager = new ACPSessionManager(this.sdk)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
private setupEventSubscriptions(session: ACPSessionState) {
|
|
59
|
-
const sessionId = session.id
|
|
60
|
-
const directory = session.cwd
|
|
61
|
-
|
|
62
|
-
const options: PermissionOption[] = [
|
|
63
|
-
{ optionId: "once", kind: "allow_once", name: "Allow once" },
|
|
64
|
-
{ optionId: "always", kind: "allow_always", name: "Always allow" },
|
|
65
|
-
{ optionId: "reject", kind: "reject_once", name: "Reject" },
|
|
66
|
-
]
|
|
67
|
-
this.config.sdk.event.subscribe({ directory }).then(async (events) => {
|
|
68
|
-
for await (const event of events.stream) {
|
|
69
|
-
switch (event.type) {
|
|
70
|
-
case "permission.asked":
|
|
71
|
-
try {
|
|
72
|
-
const permission = event.properties
|
|
73
|
-
const res = await this.connection
|
|
74
|
-
.requestPermission({
|
|
75
|
-
sessionId,
|
|
76
|
-
toolCall: {
|
|
77
|
-
toolCallId: permission.tool?.callID ?? permission.id,
|
|
78
|
-
status: "pending",
|
|
79
|
-
title: permission.permission,
|
|
80
|
-
rawInput: permission.metadata,
|
|
81
|
-
kind: toToolKind(permission.permission),
|
|
82
|
-
locations: toLocations(permission.permission, permission.metadata),
|
|
83
|
-
},
|
|
84
|
-
options,
|
|
85
|
-
})
|
|
86
|
-
.catch(async (error) => {
|
|
87
|
-
log.error("failed to request permission from ACP", {
|
|
88
|
-
error,
|
|
89
|
-
permissionID: permission.id,
|
|
90
|
-
sessionID: permission.sessionID,
|
|
91
|
-
})
|
|
92
|
-
await this.config.sdk.permission.reply({
|
|
93
|
-
requestID: permission.id,
|
|
94
|
-
reply: "reject",
|
|
95
|
-
directory,
|
|
96
|
-
})
|
|
97
|
-
return
|
|
98
|
-
})
|
|
99
|
-
if (!res) return
|
|
100
|
-
if (res.outcome.outcome !== "selected") {
|
|
101
|
-
await this.config.sdk.permission.reply({
|
|
102
|
-
requestID: permission.id,
|
|
103
|
-
reply: "reject",
|
|
104
|
-
directory,
|
|
105
|
-
})
|
|
106
|
-
return
|
|
107
|
-
}
|
|
108
|
-
await this.config.sdk.permission.reply({
|
|
109
|
-
requestID: permission.id,
|
|
110
|
-
reply: res.outcome.optionId as "once" | "always" | "reject",
|
|
111
|
-
directory,
|
|
112
|
-
})
|
|
113
|
-
} catch (err) {
|
|
114
|
-
log.error("unexpected error when handling permission", { error: err })
|
|
115
|
-
} finally {
|
|
116
|
-
break
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
case "message.part.updated":
|
|
120
|
-
log.info("message part updated", { event: event.properties })
|
|
121
|
-
try {
|
|
122
|
-
const props = event.properties
|
|
123
|
-
const { part } = props
|
|
124
|
-
|
|
125
|
-
const message = await this.config.sdk.session
|
|
126
|
-
.message(
|
|
127
|
-
{
|
|
128
|
-
sessionID: part.sessionID,
|
|
129
|
-
messageID: part.messageID,
|
|
130
|
-
directory,
|
|
131
|
-
},
|
|
132
|
-
{ throwOnError: true },
|
|
133
|
-
)
|
|
134
|
-
.then((x) => x.data)
|
|
135
|
-
.catch((err) => {
|
|
136
|
-
log.error("unexpected error when fetching message", { error: err })
|
|
137
|
-
return undefined
|
|
138
|
-
})
|
|
139
|
-
|
|
140
|
-
if (!message || message.info.role !== "assistant") return
|
|
141
|
-
|
|
142
|
-
if (part.type === "tool") {
|
|
143
|
-
switch (part.state.status) {
|
|
144
|
-
case "pending":
|
|
145
|
-
await this.connection
|
|
146
|
-
.sessionUpdate({
|
|
147
|
-
sessionId,
|
|
148
|
-
update: {
|
|
149
|
-
sessionUpdate: "tool_call",
|
|
150
|
-
toolCallId: part.callID,
|
|
151
|
-
title: part.tool,
|
|
152
|
-
kind: toToolKind(part.tool),
|
|
153
|
-
status: "pending",
|
|
154
|
-
locations: [],
|
|
155
|
-
rawInput: {},
|
|
156
|
-
},
|
|
157
|
-
})
|
|
158
|
-
.catch((err) => {
|
|
159
|
-
log.error("failed to send tool pending to ACP", { error: err })
|
|
160
|
-
})
|
|
161
|
-
break
|
|
162
|
-
case "running":
|
|
163
|
-
await this.connection
|
|
164
|
-
.sessionUpdate({
|
|
165
|
-
sessionId,
|
|
166
|
-
update: {
|
|
167
|
-
sessionUpdate: "tool_call_update",
|
|
168
|
-
toolCallId: part.callID,
|
|
169
|
-
status: "in_progress",
|
|
170
|
-
locations: toLocations(part.tool, part.state.input),
|
|
171
|
-
rawInput: part.state.input,
|
|
172
|
-
},
|
|
173
|
-
})
|
|
174
|
-
.catch((err) => {
|
|
175
|
-
log.error("failed to send tool in_progress to ACP", { error: err })
|
|
176
|
-
})
|
|
177
|
-
break
|
|
178
|
-
case "completed":
|
|
179
|
-
const kind = toToolKind(part.tool)
|
|
180
|
-
const content: ToolCallContent[] = [
|
|
181
|
-
{
|
|
182
|
-
type: "content",
|
|
183
|
-
content: {
|
|
184
|
-
type: "text",
|
|
185
|
-
text: part.state.output,
|
|
186
|
-
},
|
|
187
|
-
},
|
|
188
|
-
]
|
|
189
|
-
|
|
190
|
-
if (kind === "edit") {
|
|
191
|
-
const input = part.state.input
|
|
192
|
-
const filePath = typeof input["filePath"] === "string" ? input["filePath"] : ""
|
|
193
|
-
const oldText = typeof input["oldString"] === "string" ? input["oldString"] : ""
|
|
194
|
-
const newText =
|
|
195
|
-
typeof input["newString"] === "string"
|
|
196
|
-
? input["newString"]
|
|
197
|
-
: typeof input["content"] === "string"
|
|
198
|
-
? input["content"]
|
|
199
|
-
: ""
|
|
200
|
-
content.push({
|
|
201
|
-
type: "diff",
|
|
202
|
-
path: filePath,
|
|
203
|
-
oldText,
|
|
204
|
-
newText,
|
|
205
|
-
})
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
if (part.tool === "todowrite") {
|
|
209
|
-
const parsedTodos = z.array(Todo.Info).safeParse(JSON.parse(part.state.output))
|
|
210
|
-
if (parsedTodos.success) {
|
|
211
|
-
await this.connection
|
|
212
|
-
.sessionUpdate({
|
|
213
|
-
sessionId,
|
|
214
|
-
update: {
|
|
215
|
-
sessionUpdate: "plan",
|
|
216
|
-
entries: parsedTodos.data.map((todo) => {
|
|
217
|
-
const status: PlanEntry["status"] =
|
|
218
|
-
todo.status === "cancelled" ? "completed" : (todo.status as PlanEntry["status"])
|
|
219
|
-
return {
|
|
220
|
-
priority: "medium",
|
|
221
|
-
status,
|
|
222
|
-
content: todo.content,
|
|
223
|
-
}
|
|
224
|
-
}),
|
|
225
|
-
},
|
|
226
|
-
})
|
|
227
|
-
.catch((err) => {
|
|
228
|
-
log.error("failed to send session update for todo", { error: err })
|
|
229
|
-
})
|
|
230
|
-
} else {
|
|
231
|
-
log.error("failed to parse todo output", { error: parsedTodos.error })
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
await this.connection
|
|
236
|
-
.sessionUpdate({
|
|
237
|
-
sessionId,
|
|
238
|
-
update: {
|
|
239
|
-
sessionUpdate: "tool_call_update",
|
|
240
|
-
toolCallId: part.callID,
|
|
241
|
-
status: "completed",
|
|
242
|
-
kind,
|
|
243
|
-
content,
|
|
244
|
-
title: part.state.title,
|
|
245
|
-
rawOutput: {
|
|
246
|
-
output: part.state.output,
|
|
247
|
-
metadata: part.state.metadata,
|
|
248
|
-
},
|
|
249
|
-
},
|
|
250
|
-
})
|
|
251
|
-
.catch((err) => {
|
|
252
|
-
log.error("failed to send tool completed to ACP", { error: err })
|
|
253
|
-
})
|
|
254
|
-
break
|
|
255
|
-
case "error":
|
|
256
|
-
await this.connection
|
|
257
|
-
.sessionUpdate({
|
|
258
|
-
sessionId,
|
|
259
|
-
update: {
|
|
260
|
-
sessionUpdate: "tool_call_update",
|
|
261
|
-
toolCallId: part.callID,
|
|
262
|
-
status: "failed",
|
|
263
|
-
content: [
|
|
264
|
-
{
|
|
265
|
-
type: "content",
|
|
266
|
-
content: {
|
|
267
|
-
type: "text",
|
|
268
|
-
text: part.state.error,
|
|
269
|
-
},
|
|
270
|
-
},
|
|
271
|
-
],
|
|
272
|
-
rawOutput: {
|
|
273
|
-
error: part.state.error,
|
|
274
|
-
},
|
|
275
|
-
},
|
|
276
|
-
})
|
|
277
|
-
.catch((err) => {
|
|
278
|
-
log.error("failed to send tool error to ACP", { error: err })
|
|
279
|
-
})
|
|
280
|
-
break
|
|
281
|
-
}
|
|
282
|
-
} else if (part.type === "text") {
|
|
283
|
-
const delta = props.delta
|
|
284
|
-
if (delta && part.synthetic !== true) {
|
|
285
|
-
await this.connection
|
|
286
|
-
.sessionUpdate({
|
|
287
|
-
sessionId,
|
|
288
|
-
update: {
|
|
289
|
-
sessionUpdate: "agent_message_chunk",
|
|
290
|
-
content: {
|
|
291
|
-
type: "text",
|
|
292
|
-
text: delta,
|
|
293
|
-
},
|
|
294
|
-
},
|
|
295
|
-
})
|
|
296
|
-
.catch((err) => {
|
|
297
|
-
log.error("failed to send text to ACP", { error: err })
|
|
298
|
-
})
|
|
299
|
-
}
|
|
300
|
-
} else if (part.type === "reasoning") {
|
|
301
|
-
const delta = props.delta
|
|
302
|
-
if (delta) {
|
|
303
|
-
await this.connection
|
|
304
|
-
.sessionUpdate({
|
|
305
|
-
sessionId,
|
|
306
|
-
update: {
|
|
307
|
-
sessionUpdate: "agent_thought_chunk",
|
|
308
|
-
content: {
|
|
309
|
-
type: "text",
|
|
310
|
-
text: delta,
|
|
311
|
-
},
|
|
312
|
-
},
|
|
313
|
-
})
|
|
314
|
-
.catch((err) => {
|
|
315
|
-
log.error("failed to send reasoning to ACP", { error: err })
|
|
316
|
-
})
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
} finally {
|
|
320
|
-
break
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
})
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
async initialize(params: InitializeRequest): Promise<InitializeResponse> {
|
|
328
|
-
log.info("initialize", { protocolVersion: params.protocolVersion })
|
|
329
|
-
|
|
330
|
-
const authMethod: AuthMethod = {
|
|
331
|
-
description: "Run `opencode auth login` in the terminal",
|
|
332
|
-
name: "Login with opencode",
|
|
333
|
-
id: "opencode-login",
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
// If client supports terminal-auth capability, use that instead.
|
|
337
|
-
if (params.clientCapabilities?._meta?.["terminal-auth"] === true) {
|
|
338
|
-
authMethod._meta = {
|
|
339
|
-
"terminal-auth": {
|
|
340
|
-
command: "opencode",
|
|
341
|
-
args: ["auth", "login"],
|
|
342
|
-
label: "OpenCode Login",
|
|
343
|
-
},
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
return {
|
|
348
|
-
protocolVersion: 1,
|
|
349
|
-
agentCapabilities: {
|
|
350
|
-
loadSession: true,
|
|
351
|
-
mcpCapabilities: {
|
|
352
|
-
http: true,
|
|
353
|
-
sse: true,
|
|
354
|
-
},
|
|
355
|
-
promptCapabilities: {
|
|
356
|
-
embeddedContext: true,
|
|
357
|
-
image: true,
|
|
358
|
-
},
|
|
359
|
-
},
|
|
360
|
-
authMethods: [authMethod],
|
|
361
|
-
agentInfo: {
|
|
362
|
-
name: "OpenCode",
|
|
363
|
-
version: Installation.VERSION,
|
|
364
|
-
},
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
async authenticate(_params: AuthenticateRequest) {
|
|
369
|
-
throw new Error("Authentication not implemented")
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
async newSession(params: NewSessionRequest) {
|
|
373
|
-
const directory = params.cwd
|
|
374
|
-
try {
|
|
375
|
-
const model = await defaultModel(this.config, directory)
|
|
376
|
-
|
|
377
|
-
// Store ACP session state
|
|
378
|
-
const state = await this.sessionManager.create(params.cwd, params.mcpServers, model)
|
|
379
|
-
const sessionId = state.id
|
|
380
|
-
|
|
381
|
-
log.info("creating_session", { sessionId, mcpServers: params.mcpServers.length })
|
|
382
|
-
|
|
383
|
-
const load = await this.loadSessionMode({
|
|
384
|
-
cwd: directory,
|
|
385
|
-
mcpServers: params.mcpServers,
|
|
386
|
-
sessionId,
|
|
387
|
-
})
|
|
388
|
-
|
|
389
|
-
this.setupEventSubscriptions(state)
|
|
390
|
-
|
|
391
|
-
return {
|
|
392
|
-
sessionId,
|
|
393
|
-
models: load.models,
|
|
394
|
-
modes: load.modes,
|
|
395
|
-
_meta: {},
|
|
396
|
-
}
|
|
397
|
-
} catch (e) {
|
|
398
|
-
const error = MessageV2.fromError(e, {
|
|
399
|
-
providerID: this.config.defaultModel?.providerID ?? "unknown",
|
|
400
|
-
})
|
|
401
|
-
if (LoadAPIKeyError.isInstance(error)) {
|
|
402
|
-
throw RequestError.authRequired()
|
|
403
|
-
}
|
|
404
|
-
throw e
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
async loadSession(params: LoadSessionRequest) {
|
|
409
|
-
const directory = params.cwd
|
|
410
|
-
const sessionId = params.sessionId
|
|
411
|
-
|
|
412
|
-
try {
|
|
413
|
-
const model = await defaultModel(this.config, directory)
|
|
414
|
-
|
|
415
|
-
// Store ACP session state
|
|
416
|
-
const state = await this.sessionManager.load(sessionId, params.cwd, params.mcpServers, model)
|
|
417
|
-
|
|
418
|
-
log.info("load_session", { sessionId, mcpServers: params.mcpServers.length })
|
|
419
|
-
|
|
420
|
-
const mode = await this.loadSessionMode({
|
|
421
|
-
cwd: directory,
|
|
422
|
-
mcpServers: params.mcpServers,
|
|
423
|
-
sessionId,
|
|
424
|
-
})
|
|
425
|
-
|
|
426
|
-
this.setupEventSubscriptions(state)
|
|
427
|
-
|
|
428
|
-
// Replay session history
|
|
429
|
-
const messages = await this.sdk.session
|
|
430
|
-
.messages(
|
|
431
|
-
{
|
|
432
|
-
sessionID: sessionId,
|
|
433
|
-
directory,
|
|
434
|
-
},
|
|
435
|
-
{ throwOnError: true },
|
|
436
|
-
)
|
|
437
|
-
.then((x) => x.data)
|
|
438
|
-
.catch((err) => {
|
|
439
|
-
log.error("unexpected error when fetching message", { error: err })
|
|
440
|
-
return undefined
|
|
441
|
-
})
|
|
442
|
-
|
|
443
|
-
for (const msg of messages ?? []) {
|
|
444
|
-
log.debug("replay message", msg)
|
|
445
|
-
await this.processMessage(msg)
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
return mode
|
|
449
|
-
} catch (e) {
|
|
450
|
-
const error = MessageV2.fromError(e, {
|
|
451
|
-
providerID: this.config.defaultModel?.providerID ?? "unknown",
|
|
452
|
-
})
|
|
453
|
-
if (LoadAPIKeyError.isInstance(error)) {
|
|
454
|
-
throw RequestError.authRequired()
|
|
455
|
-
}
|
|
456
|
-
throw e
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
private async processMessage(message: SessionMessageResponse) {
|
|
461
|
-
log.debug("process message", message)
|
|
462
|
-
if (message.info.role !== "assistant" && message.info.role !== "user") return
|
|
463
|
-
const sessionId = message.info.sessionID
|
|
464
|
-
|
|
465
|
-
for (const part of message.parts) {
|
|
466
|
-
if (part.type === "tool") {
|
|
467
|
-
switch (part.state.status) {
|
|
468
|
-
case "pending":
|
|
469
|
-
await this.connection
|
|
470
|
-
.sessionUpdate({
|
|
471
|
-
sessionId,
|
|
472
|
-
update: {
|
|
473
|
-
sessionUpdate: "tool_call",
|
|
474
|
-
toolCallId: part.callID,
|
|
475
|
-
title: part.tool,
|
|
476
|
-
kind: toToolKind(part.tool),
|
|
477
|
-
status: "pending",
|
|
478
|
-
locations: [],
|
|
479
|
-
rawInput: {},
|
|
480
|
-
},
|
|
481
|
-
})
|
|
482
|
-
.catch((err) => {
|
|
483
|
-
log.error("failed to send tool pending to ACP", { error: err })
|
|
484
|
-
})
|
|
485
|
-
break
|
|
486
|
-
case "running":
|
|
487
|
-
await this.connection
|
|
488
|
-
.sessionUpdate({
|
|
489
|
-
sessionId,
|
|
490
|
-
update: {
|
|
491
|
-
sessionUpdate: "tool_call_update",
|
|
492
|
-
toolCallId: part.callID,
|
|
493
|
-
status: "in_progress",
|
|
494
|
-
locations: toLocations(part.tool, part.state.input),
|
|
495
|
-
rawInput: part.state.input,
|
|
496
|
-
},
|
|
497
|
-
})
|
|
498
|
-
.catch((err) => {
|
|
499
|
-
log.error("failed to send tool in_progress to ACP", { error: err })
|
|
500
|
-
})
|
|
501
|
-
break
|
|
502
|
-
case "completed":
|
|
503
|
-
const kind = toToolKind(part.tool)
|
|
504
|
-
const content: ToolCallContent[] = [
|
|
505
|
-
{
|
|
506
|
-
type: "content",
|
|
507
|
-
content: {
|
|
508
|
-
type: "text",
|
|
509
|
-
text: part.state.output,
|
|
510
|
-
},
|
|
511
|
-
},
|
|
512
|
-
]
|
|
513
|
-
|
|
514
|
-
if (kind === "edit") {
|
|
515
|
-
const input = part.state.input
|
|
516
|
-
const filePath = typeof input["filePath"] === "string" ? input["filePath"] : ""
|
|
517
|
-
const oldText = typeof input["oldString"] === "string" ? input["oldString"] : ""
|
|
518
|
-
const newText =
|
|
519
|
-
typeof input["newString"] === "string"
|
|
520
|
-
? input["newString"]
|
|
521
|
-
: typeof input["content"] === "string"
|
|
522
|
-
? input["content"]
|
|
523
|
-
: ""
|
|
524
|
-
content.push({
|
|
525
|
-
type: "diff",
|
|
526
|
-
path: filePath,
|
|
527
|
-
oldText,
|
|
528
|
-
newText,
|
|
529
|
-
})
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
if (part.tool === "todowrite") {
|
|
533
|
-
const parsedTodos = z.array(Todo.Info).safeParse(JSON.parse(part.state.output))
|
|
534
|
-
if (parsedTodos.success) {
|
|
535
|
-
await this.connection
|
|
536
|
-
.sessionUpdate({
|
|
537
|
-
sessionId,
|
|
538
|
-
update: {
|
|
539
|
-
sessionUpdate: "plan",
|
|
540
|
-
entries: parsedTodos.data.map((todo) => {
|
|
541
|
-
const status: PlanEntry["status"] =
|
|
542
|
-
todo.status === "cancelled" ? "completed" : (todo.status as PlanEntry["status"])
|
|
543
|
-
return {
|
|
544
|
-
priority: "medium",
|
|
545
|
-
status,
|
|
546
|
-
content: todo.content,
|
|
547
|
-
}
|
|
548
|
-
}),
|
|
549
|
-
},
|
|
550
|
-
})
|
|
551
|
-
.catch((err) => {
|
|
552
|
-
log.error("failed to send session update for todo", { error: err })
|
|
553
|
-
})
|
|
554
|
-
} else {
|
|
555
|
-
log.error("failed to parse todo output", { error: parsedTodos.error })
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
await this.connection
|
|
560
|
-
.sessionUpdate({
|
|
561
|
-
sessionId,
|
|
562
|
-
update: {
|
|
563
|
-
sessionUpdate: "tool_call_update",
|
|
564
|
-
toolCallId: part.callID,
|
|
565
|
-
status: "completed",
|
|
566
|
-
kind,
|
|
567
|
-
content,
|
|
568
|
-
title: part.state.title,
|
|
569
|
-
rawOutput: {
|
|
570
|
-
output: part.state.output,
|
|
571
|
-
metadata: part.state.metadata,
|
|
572
|
-
},
|
|
573
|
-
},
|
|
574
|
-
})
|
|
575
|
-
.catch((err) => {
|
|
576
|
-
log.error("failed to send tool completed to ACP", { error: err })
|
|
577
|
-
})
|
|
578
|
-
break
|
|
579
|
-
case "error":
|
|
580
|
-
await this.connection
|
|
581
|
-
.sessionUpdate({
|
|
582
|
-
sessionId,
|
|
583
|
-
update: {
|
|
584
|
-
sessionUpdate: "tool_call_update",
|
|
585
|
-
toolCallId: part.callID,
|
|
586
|
-
status: "failed",
|
|
587
|
-
content: [
|
|
588
|
-
{
|
|
589
|
-
type: "content",
|
|
590
|
-
content: {
|
|
591
|
-
type: "text",
|
|
592
|
-
text: part.state.error,
|
|
593
|
-
},
|
|
594
|
-
},
|
|
595
|
-
],
|
|
596
|
-
rawOutput: {
|
|
597
|
-
error: part.state.error,
|
|
598
|
-
},
|
|
599
|
-
},
|
|
600
|
-
})
|
|
601
|
-
.catch((err) => {
|
|
602
|
-
log.error("failed to send tool error to ACP", { error: err })
|
|
603
|
-
})
|
|
604
|
-
break
|
|
605
|
-
}
|
|
606
|
-
} else if (part.type === "text") {
|
|
607
|
-
if (part.text) {
|
|
608
|
-
await this.connection
|
|
609
|
-
.sessionUpdate({
|
|
610
|
-
sessionId,
|
|
611
|
-
update: {
|
|
612
|
-
sessionUpdate: message.info.role === "user" ? "user_message_chunk" : "agent_message_chunk",
|
|
613
|
-
content: {
|
|
614
|
-
type: "text",
|
|
615
|
-
text: part.text,
|
|
616
|
-
},
|
|
617
|
-
},
|
|
618
|
-
})
|
|
619
|
-
.catch((err) => {
|
|
620
|
-
log.error("failed to send text to ACP", { error: err })
|
|
621
|
-
})
|
|
622
|
-
}
|
|
623
|
-
} else if (part.type === "reasoning") {
|
|
624
|
-
if (part.text) {
|
|
625
|
-
await this.connection
|
|
626
|
-
.sessionUpdate({
|
|
627
|
-
sessionId,
|
|
628
|
-
update: {
|
|
629
|
-
sessionUpdate: "agent_thought_chunk",
|
|
630
|
-
content: {
|
|
631
|
-
type: "text",
|
|
632
|
-
text: part.text,
|
|
633
|
-
},
|
|
634
|
-
},
|
|
635
|
-
})
|
|
636
|
-
.catch((err) => {
|
|
637
|
-
log.error("failed to send reasoning to ACP", { error: err })
|
|
638
|
-
})
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
private async loadSessionMode(params: LoadSessionRequest) {
|
|
645
|
-
const directory = params.cwd
|
|
646
|
-
const model = await defaultModel(this.config, directory)
|
|
647
|
-
const sessionId = params.sessionId
|
|
648
|
-
|
|
649
|
-
const providers = await this.sdk.config.providers({ directory }).then((x) => x.data!.providers)
|
|
650
|
-
const entries = providers.sort((a, b) => {
|
|
651
|
-
const nameA = a.name.toLowerCase()
|
|
652
|
-
const nameB = b.name.toLowerCase()
|
|
653
|
-
if (nameA < nameB) return -1
|
|
654
|
-
if (nameA > nameB) return 1
|
|
655
|
-
return 0
|
|
656
|
-
})
|
|
657
|
-
const availableModels = entries.flatMap((provider) => {
|
|
658
|
-
const models = Provider.sort(Object.values(provider.models))
|
|
659
|
-
return models.map((model) => ({
|
|
660
|
-
modelId: `${provider.id}/${model.id}`,
|
|
661
|
-
name: `${provider.name}/${model.name}`,
|
|
662
|
-
}))
|
|
663
|
-
})
|
|
664
|
-
|
|
665
|
-
const agents = await this.config.sdk.app
|
|
666
|
-
.agents(
|
|
667
|
-
{
|
|
668
|
-
directory,
|
|
669
|
-
},
|
|
670
|
-
{ throwOnError: true },
|
|
671
|
-
)
|
|
672
|
-
.then((resp) => resp.data!)
|
|
673
|
-
|
|
674
|
-
const commands = await this.config.sdk.command
|
|
675
|
-
.list(
|
|
676
|
-
{
|
|
677
|
-
directory,
|
|
678
|
-
},
|
|
679
|
-
{ throwOnError: true },
|
|
680
|
-
)
|
|
681
|
-
.then((resp) => resp.data!)
|
|
682
|
-
|
|
683
|
-
const availableCommands = commands.map((command) => ({
|
|
684
|
-
name: command.name,
|
|
685
|
-
description: command.description ?? "",
|
|
686
|
-
}))
|
|
687
|
-
const names = new Set(availableCommands.map((c) => c.name))
|
|
688
|
-
if (!names.has("compact"))
|
|
689
|
-
availableCommands.push({
|
|
690
|
-
name: "compact",
|
|
691
|
-
description: "compact the session",
|
|
692
|
-
})
|
|
693
|
-
|
|
694
|
-
const availableModes = agents
|
|
695
|
-
.filter((agent) => agent.mode !== "subagent" && !agent.hidden)
|
|
696
|
-
.map((agent) => ({
|
|
697
|
-
id: agent.name,
|
|
698
|
-
name: agent.name,
|
|
699
|
-
description: agent.description,
|
|
700
|
-
}))
|
|
701
|
-
|
|
702
|
-
const defaultAgentName = await AgentModule.defaultAgent()
|
|
703
|
-
const currentModeId = availableModes.find((m) => m.name === defaultAgentName)?.id ?? availableModes[0].id
|
|
704
|
-
|
|
705
|
-
const mcpServers: Record<string, Config.Mcp> = {}
|
|
706
|
-
for (const server of params.mcpServers) {
|
|
707
|
-
if ("type" in server) {
|
|
708
|
-
mcpServers[server.name] = {
|
|
709
|
-
url: server.url,
|
|
710
|
-
headers: server.headers.reduce<Record<string, string>>((acc, { name, value }) => {
|
|
711
|
-
acc[name] = value
|
|
712
|
-
return acc
|
|
713
|
-
}, {}),
|
|
714
|
-
type: "remote",
|
|
715
|
-
}
|
|
716
|
-
} else {
|
|
717
|
-
mcpServers[server.name] = {
|
|
718
|
-
type: "local",
|
|
719
|
-
command: [server.command, ...server.args],
|
|
720
|
-
environment: server.env.reduce<Record<string, string>>((acc, { name, value }) => {
|
|
721
|
-
acc[name] = value
|
|
722
|
-
return acc
|
|
723
|
-
}, {}),
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
await Promise.all(
|
|
729
|
-
Object.entries(mcpServers).map(async ([key, mcp]) => {
|
|
730
|
-
await this.sdk.mcp
|
|
731
|
-
.add(
|
|
732
|
-
{
|
|
733
|
-
directory,
|
|
734
|
-
name: key,
|
|
735
|
-
config: mcp,
|
|
736
|
-
},
|
|
737
|
-
{ throwOnError: true },
|
|
738
|
-
)
|
|
739
|
-
.catch((error) => {
|
|
740
|
-
log.error("failed to add mcp server", { name: key, error })
|
|
741
|
-
})
|
|
742
|
-
}),
|
|
743
|
-
)
|
|
744
|
-
|
|
745
|
-
setTimeout(() => {
|
|
746
|
-
this.connection.sessionUpdate({
|
|
747
|
-
sessionId,
|
|
748
|
-
update: {
|
|
749
|
-
sessionUpdate: "available_commands_update",
|
|
750
|
-
availableCommands,
|
|
751
|
-
},
|
|
752
|
-
})
|
|
753
|
-
}, 0)
|
|
754
|
-
|
|
755
|
-
return {
|
|
756
|
-
sessionId,
|
|
757
|
-
models: {
|
|
758
|
-
currentModelId: `${model.providerID}/${model.modelID}`,
|
|
759
|
-
availableModels,
|
|
760
|
-
},
|
|
761
|
-
modes: {
|
|
762
|
-
availableModes,
|
|
763
|
-
currentModeId,
|
|
764
|
-
},
|
|
765
|
-
_meta: {},
|
|
766
|
-
}
|
|
767
|
-
}
|
|
768
|
-
|
|
769
|
-
async setSessionModel(params: SetSessionModelRequest) {
|
|
770
|
-
const session = this.sessionManager.get(params.sessionId)
|
|
771
|
-
|
|
772
|
-
const model = Provider.parseModel(params.modelId)
|
|
773
|
-
|
|
774
|
-
this.sessionManager.setModel(session.id, {
|
|
775
|
-
providerID: model.providerID,
|
|
776
|
-
modelID: model.modelID,
|
|
777
|
-
})
|
|
778
|
-
|
|
779
|
-
return {
|
|
780
|
-
_meta: {},
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
|
|
784
|
-
async setSessionMode(params: SetSessionModeRequest): Promise<SetSessionModeResponse | void> {
|
|
785
|
-
this.sessionManager.get(params.sessionId)
|
|
786
|
-
await this.config.sdk.app
|
|
787
|
-
.agents({}, { throwOnError: true })
|
|
788
|
-
.then((x) => x.data)
|
|
789
|
-
.then((agent) => {
|
|
790
|
-
if (!agent) throw new Error(`Agent not found: ${params.modeId}`)
|
|
791
|
-
})
|
|
792
|
-
this.sessionManager.setMode(params.sessionId, params.modeId)
|
|
793
|
-
}
|
|
794
|
-
|
|
795
|
-
async prompt(params: PromptRequest) {
|
|
796
|
-
const sessionID = params.sessionId
|
|
797
|
-
const session = this.sessionManager.get(sessionID)
|
|
798
|
-
const directory = session.cwd
|
|
799
|
-
|
|
800
|
-
const current = session.model
|
|
801
|
-
const model = current ?? (await defaultModel(this.config, directory))
|
|
802
|
-
if (!current) {
|
|
803
|
-
this.sessionManager.setModel(session.id, model)
|
|
804
|
-
}
|
|
805
|
-
const agent = session.modeId ?? (await AgentModule.defaultAgent())
|
|
806
|
-
|
|
807
|
-
const parts: Array<
|
|
808
|
-
{ type: "text"; text: string } | { type: "file"; url: string; filename: string; mime: string }
|
|
809
|
-
> = []
|
|
810
|
-
for (const part of params.prompt) {
|
|
811
|
-
switch (part.type) {
|
|
812
|
-
case "text":
|
|
813
|
-
parts.push({
|
|
814
|
-
type: "text" as const,
|
|
815
|
-
text: part.text,
|
|
816
|
-
})
|
|
817
|
-
break
|
|
818
|
-
case "image":
|
|
819
|
-
if (part.data) {
|
|
820
|
-
parts.push({
|
|
821
|
-
type: "file",
|
|
822
|
-
url: `data:${part.mimeType};base64,${part.data}`,
|
|
823
|
-
filename: "image",
|
|
824
|
-
mime: part.mimeType,
|
|
825
|
-
})
|
|
826
|
-
} else if (part.uri && part.uri.startsWith("http:")) {
|
|
827
|
-
parts.push({
|
|
828
|
-
type: "file",
|
|
829
|
-
url: part.uri,
|
|
830
|
-
filename: "image",
|
|
831
|
-
mime: part.mimeType,
|
|
832
|
-
})
|
|
833
|
-
}
|
|
834
|
-
break
|
|
835
|
-
|
|
836
|
-
case "resource_link":
|
|
837
|
-
const parsed = parseUri(part.uri)
|
|
838
|
-
parts.push(parsed)
|
|
839
|
-
|
|
840
|
-
break
|
|
841
|
-
|
|
842
|
-
case "resource":
|
|
843
|
-
const resource = part.resource
|
|
844
|
-
if ("text" in resource) {
|
|
845
|
-
parts.push({
|
|
846
|
-
type: "text",
|
|
847
|
-
text: resource.text,
|
|
848
|
-
})
|
|
849
|
-
}
|
|
850
|
-
break
|
|
851
|
-
|
|
852
|
-
default:
|
|
853
|
-
break
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
|
|
857
|
-
log.info("parts", { parts })
|
|
858
|
-
|
|
859
|
-
const cmd = (() => {
|
|
860
|
-
const text = parts
|
|
861
|
-
.filter((p): p is { type: "text"; text: string } => p.type === "text")
|
|
862
|
-
.map((p) => p.text)
|
|
863
|
-
.join("")
|
|
864
|
-
.trim()
|
|
865
|
-
|
|
866
|
-
if (!text.startsWith("/")) return
|
|
867
|
-
|
|
868
|
-
const [name, ...rest] = text.slice(1).split(/\s+/)
|
|
869
|
-
return { name, args: rest.join(" ").trim() }
|
|
870
|
-
})()
|
|
871
|
-
|
|
872
|
-
const done = {
|
|
873
|
-
stopReason: "end_turn" as const,
|
|
874
|
-
_meta: {},
|
|
875
|
-
}
|
|
876
|
-
|
|
877
|
-
if (!cmd) {
|
|
878
|
-
await this.sdk.session.prompt({
|
|
879
|
-
sessionID,
|
|
880
|
-
model: {
|
|
881
|
-
providerID: model.providerID,
|
|
882
|
-
modelID: model.modelID,
|
|
883
|
-
},
|
|
884
|
-
parts,
|
|
885
|
-
agent,
|
|
886
|
-
directory,
|
|
887
|
-
})
|
|
888
|
-
return done
|
|
889
|
-
}
|
|
890
|
-
|
|
891
|
-
const command = await this.config.sdk.command
|
|
892
|
-
.list({ directory }, { throwOnError: true })
|
|
893
|
-
.then((x) => x.data!.find((c) => c.name === cmd.name))
|
|
894
|
-
if (command) {
|
|
895
|
-
await this.sdk.session.command({
|
|
896
|
-
sessionID,
|
|
897
|
-
command: command.name,
|
|
898
|
-
arguments: cmd.args,
|
|
899
|
-
model: model.providerID + "/" + model.modelID,
|
|
900
|
-
agent,
|
|
901
|
-
directory,
|
|
902
|
-
})
|
|
903
|
-
return done
|
|
904
|
-
}
|
|
905
|
-
|
|
906
|
-
switch (cmd.name) {
|
|
907
|
-
case "compact":
|
|
908
|
-
await this.config.sdk.session.summarize(
|
|
909
|
-
{
|
|
910
|
-
sessionID,
|
|
911
|
-
directory,
|
|
912
|
-
providerID: model.providerID,
|
|
913
|
-
modelID: model.modelID,
|
|
914
|
-
},
|
|
915
|
-
{ throwOnError: true },
|
|
916
|
-
)
|
|
917
|
-
break
|
|
918
|
-
}
|
|
919
|
-
|
|
920
|
-
return done
|
|
921
|
-
}
|
|
922
|
-
|
|
923
|
-
async cancel(params: CancelNotification) {
|
|
924
|
-
const session = this.sessionManager.get(params.sessionId)
|
|
925
|
-
await this.config.sdk.session.abort(
|
|
926
|
-
{
|
|
927
|
-
sessionID: params.sessionId,
|
|
928
|
-
directory: session.cwd,
|
|
929
|
-
},
|
|
930
|
-
{ throwOnError: true },
|
|
931
|
-
)
|
|
932
|
-
}
|
|
933
|
-
}
|
|
934
|
-
|
|
935
|
-
function toToolKind(toolName: string): ToolKind {
|
|
936
|
-
const tool = toolName.toLocaleLowerCase()
|
|
937
|
-
switch (tool) {
|
|
938
|
-
case "bash":
|
|
939
|
-
return "execute"
|
|
940
|
-
case "webfetch":
|
|
941
|
-
return "fetch"
|
|
942
|
-
|
|
943
|
-
case "edit":
|
|
944
|
-
case "patch":
|
|
945
|
-
case "write":
|
|
946
|
-
return "edit"
|
|
947
|
-
|
|
948
|
-
case "grep":
|
|
949
|
-
case "glob":
|
|
950
|
-
case "context7_resolve_library_id":
|
|
951
|
-
case "context7_get_library_docs":
|
|
952
|
-
return "search"
|
|
953
|
-
|
|
954
|
-
case "list":
|
|
955
|
-
case "read":
|
|
956
|
-
return "read"
|
|
957
|
-
|
|
958
|
-
default:
|
|
959
|
-
return "other"
|
|
960
|
-
}
|
|
961
|
-
}
|
|
962
|
-
|
|
963
|
-
function toLocations(toolName: string, input: Record<string, any>): { path: string }[] {
|
|
964
|
-
const tool = toolName.toLocaleLowerCase()
|
|
965
|
-
switch (tool) {
|
|
966
|
-
case "read":
|
|
967
|
-
case "edit":
|
|
968
|
-
case "write":
|
|
969
|
-
return input["filePath"] ? [{ path: input["filePath"] }] : []
|
|
970
|
-
case "glob":
|
|
971
|
-
case "grep":
|
|
972
|
-
return input["path"] ? [{ path: input["path"] }] : []
|
|
973
|
-
case "bash":
|
|
974
|
-
return []
|
|
975
|
-
case "list":
|
|
976
|
-
return input["path"] ? [{ path: input["path"] }] : []
|
|
977
|
-
default:
|
|
978
|
-
return []
|
|
979
|
-
}
|
|
980
|
-
}
|
|
981
|
-
|
|
982
|
-
async function defaultModel(config: ACPConfig, cwd?: string) {
|
|
983
|
-
const sdk = config.sdk
|
|
984
|
-
const configured = config.defaultModel
|
|
985
|
-
if (configured) return configured
|
|
986
|
-
|
|
987
|
-
const directory = cwd ?? process.cwd()
|
|
988
|
-
|
|
989
|
-
const specified = await sdk.config
|
|
990
|
-
.get({ directory }, { throwOnError: true })
|
|
991
|
-
.then((resp) => {
|
|
992
|
-
const cfg = resp.data
|
|
993
|
-
if (!cfg || !cfg.model) return undefined
|
|
994
|
-
const parsed = Provider.parseModel(cfg.model)
|
|
995
|
-
return {
|
|
996
|
-
providerID: parsed.providerID,
|
|
997
|
-
modelID: parsed.modelID,
|
|
998
|
-
}
|
|
999
|
-
})
|
|
1000
|
-
.catch((error) => {
|
|
1001
|
-
log.error("failed to load user config for default model", { error })
|
|
1002
|
-
return undefined
|
|
1003
|
-
})
|
|
1004
|
-
|
|
1005
|
-
const providers = await sdk.config
|
|
1006
|
-
.providers({ directory }, { throwOnError: true })
|
|
1007
|
-
.then((x) => x.data?.providers ?? [])
|
|
1008
|
-
.catch((error) => {
|
|
1009
|
-
log.error("failed to list providers for default model", { error })
|
|
1010
|
-
return []
|
|
1011
|
-
})
|
|
1012
|
-
|
|
1013
|
-
if (specified && providers.length) {
|
|
1014
|
-
const provider = providers.find((p) => p.id === specified.providerID)
|
|
1015
|
-
if (provider && provider.models[specified.modelID]) return specified
|
|
1016
|
-
}
|
|
1017
|
-
|
|
1018
|
-
if (specified && !providers.length) return specified
|
|
1019
|
-
|
|
1020
|
-
const corethinkProvider = providers.find((p) => p.id === "corethink")
|
|
1021
|
-
if (corethinkProvider) {
|
|
1022
|
-
if (corethinkProvider.models["corethink"]) {
|
|
1023
|
-
return { providerID: "corethink", modelID: "corethink" }
|
|
1024
|
-
}
|
|
1025
|
-
const [best] = Provider.sort(Object.values(corethinkProvider.models))
|
|
1026
|
-
if (best) {
|
|
1027
|
-
return {
|
|
1028
|
-
providerID: best.providerID,
|
|
1029
|
-
modelID: best.id,
|
|
1030
|
-
}
|
|
1031
|
-
}
|
|
1032
|
-
}
|
|
1033
|
-
|
|
1034
|
-
const models = providers.flatMap((p) => Object.values(p.models))
|
|
1035
|
-
const [best] = Provider.sort(models)
|
|
1036
|
-
if (best) {
|
|
1037
|
-
return {
|
|
1038
|
-
providerID: best.providerID,
|
|
1039
|
-
modelID: best.id,
|
|
1040
|
-
}
|
|
1041
|
-
}
|
|
1042
|
-
|
|
1043
|
-
if (specified) return specified
|
|
1044
|
-
|
|
1045
|
-
return { providerID: "corethink", modelID: "corethink" }
|
|
1046
|
-
}
|
|
1047
|
-
|
|
1048
|
-
function parseUri(
|
|
1049
|
-
uri: string,
|
|
1050
|
-
): { type: "file"; url: string; filename: string; mime: string } | { type: "text"; text: string } {
|
|
1051
|
-
try {
|
|
1052
|
-
if (uri.startsWith("file://")) {
|
|
1053
|
-
const path = uri.slice(7)
|
|
1054
|
-
const name = path.split("/").pop() || path
|
|
1055
|
-
return {
|
|
1056
|
-
type: "file",
|
|
1057
|
-
url: uri,
|
|
1058
|
-
filename: name,
|
|
1059
|
-
mime: "text/plain",
|
|
1060
|
-
}
|
|
1061
|
-
}
|
|
1062
|
-
if (uri.startsWith("zed://")) {
|
|
1063
|
-
const url = new URL(uri)
|
|
1064
|
-
const path = url.searchParams.get("path")
|
|
1065
|
-
if (path) {
|
|
1066
|
-
const name = path.split("/").pop() || path
|
|
1067
|
-
return {
|
|
1068
|
-
type: "file",
|
|
1069
|
-
url: `file://${path}`,
|
|
1070
|
-
filename: name,
|
|
1071
|
-
mime: "text/plain",
|
|
1072
|
-
}
|
|
1073
|
-
}
|
|
1074
|
-
}
|
|
1075
|
-
return {
|
|
1076
|
-
type: "text",
|
|
1077
|
-
text: uri,
|
|
1078
|
-
}
|
|
1079
|
-
} catch {
|
|
1080
|
-
return {
|
|
1081
|
-
type: "text",
|
|
1082
|
-
text: uri,
|
|
1083
|
-
}
|
|
1084
|
-
}
|
|
1085
|
-
}
|
|
1086
|
-
}
|