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