@carrieros/cli 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +142 -57
- package/dist/commands/attach.js +45 -0
- package/dist/commands/btw.js +78 -0
- package/dist/commands/changes.js +167 -0
- package/dist/commands/chat.js +80 -14
- package/dist/commands/context.js +160 -0
- package/dist/commands/doctor.js +46 -1
- package/dist/commands/files.js +84 -23
- package/dist/commands/help.js +1 -0
- package/dist/commands/history.js +88 -23
- package/dist/commands/login.js +31 -7
- package/dist/commands/memory.js +162 -0
- package/dist/commands/mode-model.js +39 -3
- package/dist/commands/modes.js +4 -1
- package/dist/commands/project.js +106 -0
- package/dist/commands/repo.js +146 -0
- package/dist/commands/repos.js +61 -0
- package/dist/commands/review.js +92 -0
- package/dist/commands/search.js +92 -0
- package/dist/commands/sessions.js +219 -65
- package/dist/commands/settings.js +87 -0
- package/dist/commands/update.js +159 -0
- package/dist/commands/upload.js +204 -31
- package/dist/commands/usage.js +212 -16
- package/dist/ide/editor-context.js +172 -0
- package/dist/ide/ide-session.js +94 -0
- package/dist/ide/tool-host.js +109 -0
- package/dist/main.js +26 -1
- package/dist/services/api.js +140 -6
- package/dist/services/chat-stream.js +145 -0
- package/dist/services/checkpoints.js +116 -0
- package/dist/services/client-tools.js +260 -0
- package/dist/services/command-coordinator.js +200 -0
- package/dist/services/command-policy.js +404 -0
- package/dist/services/config.js +247 -22
- package/dist/services/context.js +265 -20
- package/dist/services/device-auth.js +42 -30
- package/dist/services/diff.js +196 -0
- package/dist/services/env-policy.js +108 -0
- package/dist/services/file-detect.js +104 -0
- package/dist/services/file-resolver.js +199 -0
- package/dist/services/file-upload.js +126 -0
- package/dist/services/git-tools.js +248 -0
- package/dist/services/glob-match.js +164 -0
- package/dist/services/helper-bin.js +64 -0
- package/dist/services/ignore-rules.js +189 -0
- package/dist/services/job-helper.js +307 -0
- package/dist/services/local-files.js +517 -0
- package/dist/services/local-tools.js +432 -0
- package/dist/services/login-flow.js +15 -8
- package/dist/services/meta.js +3 -1
- package/dist/services/mutation-coordinator.js +161 -0
- package/dist/services/mutations.js +493 -0
- package/dist/services/open-url.js +68 -0
- package/dist/services/output.js +5 -2
- package/dist/services/packaged.js +24 -0
- package/dist/services/process-runner.js +406 -0
- package/dist/services/project-context.js +298 -0
- package/dist/services/redaction.js +201 -0
- package/dist/services/secure-store.js +197 -0
- package/dist/services/session-resolver.js +108 -0
- package/dist/services/sia.js +51 -0
- package/dist/services/update.js +286 -0
- package/dist/services/workspace-registry.js +309 -0
- package/dist/services/workspace.js +573 -0
- package/dist/shell/shell-chat.js +1028 -57
- package/dist/shell/shell-commands.js +200 -11
- package/dist/shell/shell-prompt.js +81 -8
- package/dist/shell/shell-renderer.js +7 -19
- package/dist/shell/shell-router.js +19 -3
- package/dist/shell/shell.js +185 -17
- package/dist/types/index.js +3 -1
- package/dist/ui/approval.js +81 -0
- package/dist/ui/banner.js +39 -0
- package/dist/ui/btw-overlay.js +230 -0
- package/dist/ui/change-review.js +87 -0
- package/dist/ui/chat-renderer.js +206 -13
- package/dist/ui/colors.js +4 -0
- package/dist/ui/command-filter.js +26 -5
- package/dist/ui/command-palette.js +17 -4
- package/dist/ui/command-registry.js +84 -9
- package/dist/ui/command-review.js +138 -0
- package/dist/ui/composer-layout.js +41 -0
- package/dist/ui/conversation.js +398 -0
- package/dist/ui/interrupt.js +35 -0
- package/dist/ui/live.js +3 -1
- package/dist/ui/markdown.js +63 -9
- package/dist/ui/message.js +14 -31
- package/dist/ui/screen-prompt.js +181 -0
- package/dist/ui/screen.js +1 -1
- package/dist/ui/select.js +185 -29
- package/dist/ui/sia-qa.js +213 -0
- package/dist/ui/text-prompt.js +74 -0
- package/dist/ui/trust.js +93 -0
- package/dist/ui/ui.js +4 -1
- package/package.json +13 -2
- package/dist/ui/activity.js +0 -77
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
/** Bounds — editor context is a hint, never a payload. */
|
|
3
|
+
const MAX_SELECTION_BYTES = 32 * 1024;
|
|
4
|
+
const MAX_OPEN_FILES = 100;
|
|
5
|
+
const MAX_DIAGNOSTICS = 100;
|
|
6
|
+
const posix = (p) => p.split(path.sep).join("/");
|
|
7
|
+
/** { repository, path } for an absolute file, or null when outside every repo. */
|
|
8
|
+
function locate(absPath, resolve) {
|
|
9
|
+
const repo = resolve(absPath);
|
|
10
|
+
if (!repo)
|
|
11
|
+
return null;
|
|
12
|
+
const rel = posix(path.relative(repo.root, path.resolve(absPath)));
|
|
13
|
+
if (!rel || rel.startsWith(".."))
|
|
14
|
+
return null;
|
|
15
|
+
return { repository: repo.name, path: rel };
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Answer one editor-context tool from a snapshot. Never throws; an unknown tool
|
|
19
|
+
* or an out-of-workspace file comes back as a clean, model-readable result.
|
|
20
|
+
*/
|
|
21
|
+
export function executeIdeContextTool(name, args, state, resolve) {
|
|
22
|
+
switch (name) {
|
|
23
|
+
case "get_active_file": {
|
|
24
|
+
const a = state.activeFile;
|
|
25
|
+
if (!a)
|
|
26
|
+
return ok({ active: false });
|
|
27
|
+
const at = locate(a.absPath, resolve);
|
|
28
|
+
if (!at)
|
|
29
|
+
return ok({ active: false, reason: "The focused file is outside the workspace's repositories." });
|
|
30
|
+
return ok({
|
|
31
|
+
active: true,
|
|
32
|
+
repository: at.repository,
|
|
33
|
+
path: at.path,
|
|
34
|
+
language: a.languageId ?? null,
|
|
35
|
+
cursor: a.cursorLine ? { line: a.cursorLine, column: a.cursorColumn ?? 1 } : null
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
case "get_selection": {
|
|
39
|
+
const a = state.activeFile;
|
|
40
|
+
if (!a?.selection || !a.selection.text)
|
|
41
|
+
return ok({ selected: false });
|
|
42
|
+
const at = locate(a.absPath, resolve);
|
|
43
|
+
if (!at)
|
|
44
|
+
return ok({ selected: false, reason: "The selection is in a file outside the workspace." });
|
|
45
|
+
const text = clampBytes(a.selection.text, MAX_SELECTION_BYTES);
|
|
46
|
+
return ok({
|
|
47
|
+
selected: true,
|
|
48
|
+
repository: at.repository,
|
|
49
|
+
path: at.path,
|
|
50
|
+
language: a.languageId ?? null,
|
|
51
|
+
start_line: a.selection.startLine,
|
|
52
|
+
end_line: a.selection.endLine,
|
|
53
|
+
text: text.text,
|
|
54
|
+
truncated: text.truncated
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
case "get_open_files": {
|
|
58
|
+
const seen = new Set();
|
|
59
|
+
const files = [];
|
|
60
|
+
let outside = 0;
|
|
61
|
+
for (const abs of (state.openFiles ?? []).slice(0, MAX_OPEN_FILES * 2)) {
|
|
62
|
+
const at = locate(abs, resolve);
|
|
63
|
+
if (!at) {
|
|
64
|
+
outside += 1;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const key = `${at.repository}:${at.path}`;
|
|
68
|
+
if (seen.has(key))
|
|
69
|
+
continue;
|
|
70
|
+
seen.add(key);
|
|
71
|
+
if (files.length < MAX_OPEN_FILES)
|
|
72
|
+
files.push(at);
|
|
73
|
+
}
|
|
74
|
+
return ok({ files, outside_workspace: outside, truncated: files.length >= MAX_OPEN_FILES });
|
|
75
|
+
}
|
|
76
|
+
case "get_workspace_folders": {
|
|
77
|
+
const repos = (state.repositories ?? []).map((r) => ({ repository: r.name, active: r.active === true }));
|
|
78
|
+
return ok({ repositories: repos, active: repos.find((r) => r.active)?.repository ?? null });
|
|
79
|
+
}
|
|
80
|
+
case "get_diagnostics": {
|
|
81
|
+
// A named file, or the active one.
|
|
82
|
+
let absPath;
|
|
83
|
+
const wanted = typeof args.path === "string" ? args.path.trim() : "";
|
|
84
|
+
const wantedRepo = typeof args.repository === "string" ? args.repository.trim() : "";
|
|
85
|
+
if (wanted) {
|
|
86
|
+
// Resolve the requested project-relative path within the named/active repo.
|
|
87
|
+
const repoRoot = (state.repositories ?? []).find((r) => !wantedRepo || r.name === wantedRepo)?.root;
|
|
88
|
+
if (repoRoot)
|
|
89
|
+
absPath = path.resolve(repoRoot, wanted);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
absPath = state.activeFile?.absPath;
|
|
93
|
+
}
|
|
94
|
+
if (!absPath)
|
|
95
|
+
return ok({ diagnostics: [], reason: "No file to report diagnostics for." });
|
|
96
|
+
const at = locate(absPath, resolve);
|
|
97
|
+
if (!at)
|
|
98
|
+
return ok({ diagnostics: [], reason: "That file is outside the workspace's repositories." });
|
|
99
|
+
const entry = (state.diagnostics ?? []).find((d) => path.resolve(d.absPath) === path.resolve(absPath));
|
|
100
|
+
const items = (entry?.items ?? []).slice(0, MAX_DIAGNOSTICS).map((d) => ({
|
|
101
|
+
line: d.line,
|
|
102
|
+
severity: d.severity,
|
|
103
|
+
message: clampBytes(d.message, 2000).text,
|
|
104
|
+
source: d.source ?? null
|
|
105
|
+
}));
|
|
106
|
+
return ok({
|
|
107
|
+
repository: at.repository,
|
|
108
|
+
path: at.path,
|
|
109
|
+
diagnostics: items,
|
|
110
|
+
truncated: (entry?.items?.length ?? 0) > items.length
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
default:
|
|
114
|
+
return { ok: false, error: { code: "UNKNOWN_TOOL", message: `No IDE context tool named "${name}".` } };
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/** The IDE editor-context tool names this module answers. */
|
|
118
|
+
export const IDE_CONTEXT_TOOL_NAMES = [
|
|
119
|
+
"get_active_file",
|
|
120
|
+
"get_selection",
|
|
121
|
+
"get_open_files",
|
|
122
|
+
"get_workspace_folders",
|
|
123
|
+
"get_diagnostics"
|
|
124
|
+
];
|
|
125
|
+
export function isIdeContextTool(name) {
|
|
126
|
+
return IDE_CONTEXT_TOOL_NAMES.includes(name);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* A small, bounded hint the host prepends to the user's message so the model
|
|
130
|
+
* knows what "this file"/"this section" refers to WITHOUT having to call
|
|
131
|
+
* get_active_file first. It is not a replacement for those tools — the model may
|
|
132
|
+
* still call them for more (the selected text, diagnostics, other open files).
|
|
133
|
+
*
|
|
134
|
+
* What it carries is deliberately minimal and safe:
|
|
135
|
+
* • the active file's repository and repo-relative path (identity, mapped the
|
|
136
|
+
* same way the tools map it — never an absolute path or a filesystem root);
|
|
137
|
+
* • the selected LINE RANGE only, when a selection exists — never the selected
|
|
138
|
+
* text or any file contents.
|
|
139
|
+
*
|
|
140
|
+
* Returns null when there is no active file inside a workspace repository, so
|
|
141
|
+
* the caller sends the plain message unchanged. The block is clearly labelled as
|
|
142
|
+
* editor-provided, so the model never mistakes it for the user's own words.
|
|
143
|
+
*/
|
|
144
|
+
export function ideContextHint(state, resolve) {
|
|
145
|
+
const a = state.activeFile;
|
|
146
|
+
if (!a)
|
|
147
|
+
return null;
|
|
148
|
+
const at = locate(a.absPath, resolve);
|
|
149
|
+
if (!at)
|
|
150
|
+
return null;
|
|
151
|
+
const lines = [
|
|
152
|
+
"[IDE CONTEXT] (provided by the editor, not written by the user)",
|
|
153
|
+
`Repository: ${at.repository}`,
|
|
154
|
+
`File: ${at.path}`
|
|
155
|
+
];
|
|
156
|
+
const sel = a.selection;
|
|
157
|
+
if (sel && sel.text && Number.isFinite(sel.startLine) && Number.isFinite(sel.endLine)) {
|
|
158
|
+
lines.push(`Selection: lines ${sel.startLine}-${sel.endLine}`);
|
|
159
|
+
}
|
|
160
|
+
return lines.join("\n");
|
|
161
|
+
}
|
|
162
|
+
function ok(result) {
|
|
163
|
+
return { ok: true, result };
|
|
164
|
+
}
|
|
165
|
+
function clampBytes(text, max) {
|
|
166
|
+
if (Buffer.byteLength(text, "utf8") <= max)
|
|
167
|
+
return { text, truncated: false };
|
|
168
|
+
let out = text;
|
|
169
|
+
while (Buffer.byteLength(out, "utf8") > max)
|
|
170
|
+
out = out.slice(0, Math.floor(out.length * 0.9));
|
|
171
|
+
return { text: out + "\n… [truncated]", truncated: true };
|
|
172
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { submitWithRetry } from "../services/client-tools.js";
|
|
2
|
+
import { createMutationCoordinator } from "../services/mutation-coordinator.js";
|
|
3
|
+
import { handleIdeToolCall, ideDeclaredMutationTools, ideDeclaredReadTools } from "./tool-host.js";
|
|
4
|
+
/**
|
|
5
|
+
* Run one IDE turn. Never throws: transport and stream failures come back as
|
|
6
|
+
* `result.error`, so the binding renders them without a second failure path.
|
|
7
|
+
*/
|
|
8
|
+
export async function runIdeTurn(ctx, client, message, ports, externalSignal) {
|
|
9
|
+
const controller = new AbortController();
|
|
10
|
+
const abort = () => controller.abort();
|
|
11
|
+
if (externalSignal) {
|
|
12
|
+
if (externalSignal.aborted)
|
|
13
|
+
abort();
|
|
14
|
+
else
|
|
15
|
+
externalSignal.addEventListener("abort", abort, { once: true });
|
|
16
|
+
}
|
|
17
|
+
const signal = controller.signal;
|
|
18
|
+
// File-changing tools, gated exactly as the CLI gates them — trust plus a
|
|
19
|
+
// present approver (here, a live panel). Reads are gated on trust alone.
|
|
20
|
+
const mutationTools = ideDeclaredMutationTools(ctx, ports.approverPresent);
|
|
21
|
+
const mutations = mutationTools
|
|
22
|
+
? createMutationCoordinator({
|
|
23
|
+
submit: (payload) => submitWithRetry(client, payload),
|
|
24
|
+
ui: {
|
|
25
|
+
review: (plans) => ports.reviewMutations(plans),
|
|
26
|
+
note: (lines) => ports.onMutationNote?.(lines),
|
|
27
|
+
cancelTurn: abort
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
: undefined;
|
|
31
|
+
const sessionId = ctx.config.get("sessionId");
|
|
32
|
+
const compose = ports.composeMessage ?? ((m) => m);
|
|
33
|
+
// What the model has said so far. It narrates as it works; commit each span at
|
|
34
|
+
// a tool boundary so a progress line lands above the activity it describes.
|
|
35
|
+
let streamed = "";
|
|
36
|
+
let shown = 0;
|
|
37
|
+
const say = (upTo = streamed) => {
|
|
38
|
+
const text = upTo.slice(shown);
|
|
39
|
+
shown = upTo.length;
|
|
40
|
+
if (text.trim())
|
|
41
|
+
ports.onAssistantText(text);
|
|
42
|
+
};
|
|
43
|
+
try {
|
|
44
|
+
const result = await client.chatStream({
|
|
45
|
+
mode: ctx.mode,
|
|
46
|
+
message: compose(message),
|
|
47
|
+
session_id: sessionId,
|
|
48
|
+
chat_type: ctx.chatType,
|
|
49
|
+
// Reads + editor context, and — only when an approver is present —
|
|
50
|
+
// the file-changing tools. Commands are not offered on this surface.
|
|
51
|
+
client_tools: ideDeclaredReadTools(ctx),
|
|
52
|
+
client_mutation_tools: mutationTools
|
|
53
|
+
}, {
|
|
54
|
+
onDelta: (delta) => {
|
|
55
|
+
if (signal.aborted)
|
|
56
|
+
return;
|
|
57
|
+
streamed += delta;
|
|
58
|
+
ports.onDelta?.(streamed.slice(shown));
|
|
59
|
+
},
|
|
60
|
+
onClientToolCall: (call) => {
|
|
61
|
+
// Whatever the model said before asking for this tool belongs above it.
|
|
62
|
+
say();
|
|
63
|
+
return handleIdeToolCall(ctx, client, call, {
|
|
64
|
+
editorState: ports.editorState,
|
|
65
|
+
resolveRepoForPath: ports.resolveRepoForPath,
|
|
66
|
+
toolUi: {
|
|
67
|
+
onStart: (label, tool) => ports.onToolStart?.(label, tool),
|
|
68
|
+
onDone: (label, ok, tool, detail) => ports.onToolEnd?.(label, ok, tool, detail),
|
|
69
|
+
mutations
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}, signal);
|
|
74
|
+
// The final message — anything the model did not already narrate.
|
|
75
|
+
say(result.response);
|
|
76
|
+
if (result.session_id && result.session_id !== sessionId) {
|
|
77
|
+
ctx.config.set("sessionId", result.session_id);
|
|
78
|
+
}
|
|
79
|
+
return { response: result.response, sessionId: result.session_id ?? sessionId, applied: mutations?.applied ?? [] };
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
const e = err;
|
|
83
|
+
return {
|
|
84
|
+
response: streamed,
|
|
85
|
+
sessionId,
|
|
86
|
+
applied: mutations?.applied ?? [],
|
|
87
|
+
error: { code: e.code ?? "TURN_FAILED", message: e.message ?? "The turn failed." }
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
if (externalSignal)
|
|
92
|
+
externalSignal.removeEventListener("abort", abort);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { declaredClientTools, handleClientToolCall } from "../services/client-tools.js";
|
|
2
|
+
import { workspaceRoot } from "../services/local-files.js";
|
|
3
|
+
import { MUTATION_TOOL_NAMES } from "../services/mutations.js";
|
|
4
|
+
import { repositoryForPath } from "../services/workspace-registry.js";
|
|
5
|
+
import { isTrusted } from "../ui/trust.js";
|
|
6
|
+
import { executeIdeContextTool, isIdeContextTool } from "./editor-context.js";
|
|
7
|
+
const defaultResolver = (abs) => {
|
|
8
|
+
const repo = repositoryForPath(abs);
|
|
9
|
+
return repo ? { name: repo.name, root: repo.root } : null;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Handle one tool call from an IDE-driven turn. Never throws: every outcome,
|
|
13
|
+
* including a malformed editor-context call, becomes a submitted result so the
|
|
14
|
+
* backend turn can continue.
|
|
15
|
+
*/
|
|
16
|
+
export async function handleIdeToolCall(ctx, client, call, deps) {
|
|
17
|
+
const id = typeof call?.tool_call_id === "string" ? call.tool_call_id : "";
|
|
18
|
+
if (!id)
|
|
19
|
+
return;
|
|
20
|
+
// Editor context is the only thing the IDE answers itself; everything else is
|
|
21
|
+
// the shared engine's job.
|
|
22
|
+
if (isIdeContextTool(call.name)) {
|
|
23
|
+
const started = Date.now();
|
|
24
|
+
const label = ideContextLabel(call);
|
|
25
|
+
deps.toolUi.onStart?.(label, call.name);
|
|
26
|
+
let payload;
|
|
27
|
+
try {
|
|
28
|
+
const res = executeIdeContextTool(call.name, (call.arguments ?? {}), deps.editorState(), deps.resolveRepoForPath ?? defaultResolver);
|
|
29
|
+
payload = res.ok
|
|
30
|
+
? { tool_call_id: id, ok: true, result: (res.result ?? {}) }
|
|
31
|
+
: { tool_call_id: id, ok: false, error: res.error ?? { code: "IDE_TOOL_FAILED", message: "Failed." } };
|
|
32
|
+
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
payload = { tool_call_id: id, ok: false, error: { code: "IDE_TOOL_FAILED", message: err.message ?? "Failed." } };
|
|
35
|
+
}
|
|
36
|
+
payload.duration_ms = Date.now() - started;
|
|
37
|
+
await client.submitToolResult(payload).catch(() => undefined);
|
|
38
|
+
deps.toolUi.onDone?.(label, payload.ok, call.name, payload.ok ? payload.result : payload.error?.message);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
// Read / search / git / resolve / mutation / command — the shared engine,
|
|
42
|
+
// exactly as the CLI runs it. The host's toolUi carries its IDE coordinators.
|
|
43
|
+
await handleClientToolCall(ctx, client, call, deps.toolUi);
|
|
44
|
+
}
|
|
45
|
+
/** A short activity label for an editor-context call, for the chat panel. */
|
|
46
|
+
export function ideContextLabel(call) {
|
|
47
|
+
switch (call.name) {
|
|
48
|
+
case "get_active_file":
|
|
49
|
+
return "Checking the active file";
|
|
50
|
+
case "get_selection":
|
|
51
|
+
return "Reading your selection";
|
|
52
|
+
case "get_open_files":
|
|
53
|
+
return "Checking open files";
|
|
54
|
+
case "get_workspace_folders":
|
|
55
|
+
return "Checking the workspace";
|
|
56
|
+
case "get_diagnostics":
|
|
57
|
+
return "Reading diagnostics";
|
|
58
|
+
default:
|
|
59
|
+
return "Reading editor context";
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The tools an IDE session declares to the backend: the CLI's read/git/resolve
|
|
64
|
+
* set (which the shared engine already gates on trust) plus the IDE
|
|
65
|
+
* editor-context tools. Mutation and command tools are declared separately by
|
|
66
|
+
* the host, exactly as the CLI does, so their approval UIs are wired.
|
|
67
|
+
*/
|
|
68
|
+
export function ideReadTools(cliReadTools) {
|
|
69
|
+
if (!cliReadTools)
|
|
70
|
+
return undefined;
|
|
71
|
+
return [...cliReadTools, "get_active_file", "get_selection", "get_open_files", "get_workspace_folders", "get_diagnostics"];
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The read/context tools an IDE turn offers, gated exactly as the CLI gates
|
|
75
|
+
* reads — folder trust — with the editor-context tools appended. Untrusted
|
|
76
|
+
* folder → no local tools at all, same as the terminal.
|
|
77
|
+
*/
|
|
78
|
+
export function ideDeclaredReadTools(ctx) {
|
|
79
|
+
return ideReadTools(declaredClientTools(ctx));
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The file-changing tools an IDE turn offers.
|
|
83
|
+
*
|
|
84
|
+
* The security rule is unchanged from the CLI's `declaredMutationTools`: folder
|
|
85
|
+
* trust is necessary but never sufficient — a change is only ever possible where
|
|
86
|
+
* a human can approve THAT change. The CLI proves an approver is present with
|
|
87
|
+
* `inScreen()` (the interactive terminal). The IDE cannot use that check —
|
|
88
|
+
* there is no terminal screen — so it substitutes the equivalent fact for its
|
|
89
|
+
* own surface: `approverPresent`, which the host sets true ONLY when a panel is
|
|
90
|
+
* live and will actually show the native diff + approval modal. The trust gate
|
|
91
|
+
* is identical; only the surface-presence proxy differs. If no approver is
|
|
92
|
+
* present, no mutation tools are declared, exactly as a piped CLI run offers
|
|
93
|
+
* none.
|
|
94
|
+
*
|
|
95
|
+
* Commands are deliberately NOT offered on the IDE surface in V1: the CLI's
|
|
96
|
+
* `run_command` depends on Windows Job Object containment wired to the terminal
|
|
97
|
+
* lifecycle and an explicit opt-in, and porting that containment to the IDE is a
|
|
98
|
+
* separate security review. Until then the IDE stays read + change only.
|
|
99
|
+
*/
|
|
100
|
+
export function ideDeclaredMutationTools(ctx, approverPresent) {
|
|
101
|
+
if (!approverPresent)
|
|
102
|
+
return undefined;
|
|
103
|
+
try {
|
|
104
|
+
return isTrusted(ctx.config, workspaceRoot()) ? [...MUTATION_TOOL_NAMES] : undefined;
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
}
|
package/dist/main.js
CHANGED
|
@@ -1,16 +1,28 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { Command, CommanderError } from "commander";
|
|
3
|
+
import { registerAttach } from "./commands/attach.js";
|
|
3
4
|
import { registerAuth } from "./commands/auth.js";
|
|
5
|
+
import { registerBtw } from "./commands/btw.js";
|
|
6
|
+
import { registerUpdate } from "./commands/update.js";
|
|
4
7
|
import { registerChat } from "./commands/chat.js";
|
|
8
|
+
import { registerContext } from "./commands/context.js";
|
|
5
9
|
import { registerDoctor } from "./commands/doctor.js";
|
|
6
10
|
import { registerFiles } from "./commands/files.js";
|
|
7
11
|
import { registerHelp } from "./commands/help.js";
|
|
8
12
|
import { registerHistory } from "./commands/history.js";
|
|
13
|
+
import { registerMemory } from "./commands/memory.js";
|
|
9
14
|
import { registerLogin } from "./commands/login.js";
|
|
10
15
|
import { registerLogout } from "./commands/logout.js";
|
|
11
16
|
import { registerModels } from "./commands/models.js";
|
|
12
17
|
import { registerModes } from "./commands/modes.js";
|
|
13
18
|
import { registerModeModel } from "./commands/mode-model.js";
|
|
19
|
+
import { registerRepo } from "./commands/repo.js";
|
|
20
|
+
import { registerRepos } from "./commands/repos.js";
|
|
21
|
+
import { registerSettings } from "./commands/settings.js";
|
|
22
|
+
import { registerReview } from "./commands/review.js";
|
|
23
|
+
import { attachConfig } from "./services/workspace-registry.js";
|
|
24
|
+
import { registerChanges } from "./commands/changes.js";
|
|
25
|
+
import { registerSearch } from "./commands/search.js";
|
|
14
26
|
import { registerScope } from "./commands/scope.js";
|
|
15
27
|
import { registerSessions } from "./commands/sessions.js";
|
|
16
28
|
import { registerUpload } from "./commands/upload.js";
|
|
@@ -26,6 +38,8 @@ import { runShell } from "./shell/shell.js";
|
|
|
26
38
|
import { loadAccent } from "./ui/theme.js";
|
|
27
39
|
async function main() {
|
|
28
40
|
const ctx = new CommandContext(new ConfigStore());
|
|
41
|
+
// Trust is per repository, so the registry has to be able to read it.
|
|
42
|
+
attachConfig(ctx.config);
|
|
29
43
|
// Restore the saved accent color before anything renders.
|
|
30
44
|
loadAccent(ctx.config.get("theme")?.accent);
|
|
31
45
|
const program = new Command();
|
|
@@ -45,13 +59,24 @@ async function main() {
|
|
|
45
59
|
registerWhoami(program, ctx);
|
|
46
60
|
registerScope(program, ctx);
|
|
47
61
|
registerVersion(program, ctx);
|
|
62
|
+
registerUpdate(program, ctx);
|
|
48
63
|
registerDoctor(program, ctx);
|
|
49
|
-
|
|
64
|
+
registerRepo(program, ctx);
|
|
65
|
+
registerRepos(program, ctx);
|
|
66
|
+
registerSettings(program, ctx);
|
|
67
|
+
registerReview(program, ctx);
|
|
68
|
+
registerSearch(program, ctx);
|
|
69
|
+
registerChanges(program, ctx);
|
|
70
|
+
// Phase B — chat, sessions, history, context
|
|
50
71
|
registerChat(program, ctx);
|
|
51
72
|
registerSessions(program, ctx);
|
|
52
73
|
registerHistory(program, ctx);
|
|
74
|
+
registerContext(program, ctx);
|
|
75
|
+
registerMemory(program, ctx);
|
|
76
|
+
registerBtw(program, ctx);
|
|
53
77
|
// Phase C — files, usage, models, modes, guide
|
|
54
78
|
registerUpload(program, ctx);
|
|
79
|
+
registerAttach(program, ctx);
|
|
55
80
|
registerFiles(program, ctx);
|
|
56
81
|
registerUsage(program, ctx);
|
|
57
82
|
registerModels(program, ctx);
|
package/dist/services/api.js
CHANGED
|
@@ -2,6 +2,24 @@ import fs from "node:fs";
|
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import axios, { isAxiosError } from "axios";
|
|
4
4
|
import FormData from "form-data";
|
|
5
|
+
import { StreamError, consumeChatStream } from "./chat-stream.js";
|
|
6
|
+
/** Endpoints that work without a credential. Mirrors the /v1 router, where
|
|
7
|
+
* /health is mounted ABOVE the API-key gate and everything else below it. */
|
|
8
|
+
const PUBLIC_PATHS = new Set(["/health"]);
|
|
9
|
+
/** Collect a (small) response stream into a string — used for pre-stream errors. */
|
|
10
|
+
function readAll(stream) {
|
|
11
|
+
return new Promise((resolve) => {
|
|
12
|
+
const s = stream;
|
|
13
|
+
if (!s || typeof s.on !== "function")
|
|
14
|
+
return resolve(typeof stream === "string" ? stream : "");
|
|
15
|
+
let out = "";
|
|
16
|
+
s.on("data", (c) => {
|
|
17
|
+
out += typeof c === "string" ? c : c.toString("utf8");
|
|
18
|
+
});
|
|
19
|
+
s.on("end", () => resolve(out));
|
|
20
|
+
s.on("error", () => resolve(out));
|
|
21
|
+
});
|
|
22
|
+
}
|
|
5
23
|
/** A typed, envelope-aware error surfaced by the API client. */
|
|
6
24
|
export class ApiError extends Error {
|
|
7
25
|
code;
|
|
@@ -38,6 +56,14 @@ export class ApiClient {
|
|
|
38
56
|
}
|
|
39
57
|
async request(config, retry = true) {
|
|
40
58
|
const token = this.token();
|
|
59
|
+
// No credential at all — say so here rather than letting the request go out
|
|
60
|
+
// bare and come back as the server's MISSING_API_KEY, which reads like the
|
|
61
|
+
// user forgot to supply a key they were never meant to have. This happens
|
|
62
|
+
// whenever a dead refresh token makes authProvider.refresh clear local auth
|
|
63
|
+
// mid-session, so the shell can be running with the session already gone.
|
|
64
|
+
if (!token && !PUBLIC_PATHS.has(config.url ?? "")) {
|
|
65
|
+
throw new ApiError("NOT_AUTHENTICATED", "You're not signed in.", 401);
|
|
66
|
+
}
|
|
41
67
|
const withAuth = {
|
|
42
68
|
...config,
|
|
43
69
|
headers: {
|
|
@@ -61,10 +87,16 @@ export class ApiClient {
|
|
|
61
87
|
retry &&
|
|
62
88
|
typeof this.auth === "object" &&
|
|
63
89
|
this.auth.refresh) {
|
|
64
|
-
const renewed = await this.auth.refresh();
|
|
90
|
+
const renewed = await this.auth.refresh(token);
|
|
65
91
|
if (renewed)
|
|
66
92
|
return this.request(config, false);
|
|
67
93
|
}
|
|
94
|
+
// The caller aborted (Ctrl+C during a turn). Distinct from a failure —
|
|
95
|
+
// callers render it as an interruption, not an error, and must never
|
|
96
|
+
// retry it.
|
|
97
|
+
if (isAxiosError(err) && (err.code === "ERR_CANCELED" || err.name === "CanceledError")) {
|
|
98
|
+
throw new ApiError("ABORTED", "Interrupted.", 0);
|
|
99
|
+
}
|
|
68
100
|
if (isAxiosError(err)) {
|
|
69
101
|
const status = err.response?.status;
|
|
70
102
|
const body = err.response?.data;
|
|
@@ -72,9 +104,15 @@ export class ApiClient {
|
|
|
72
104
|
if (body?.error?.code || body?.error?.message) {
|
|
73
105
|
throw new ApiError(body.error.code || "REQUEST_FAILED", body.error.message || `Request failed (HTTP ${status}).`, status);
|
|
74
106
|
}
|
|
75
|
-
// Couldn't connect
|
|
76
|
-
|
|
77
|
-
|
|
107
|
+
// Couldn't connect, or the connection died mid-request. ECONNRESET is
|
|
108
|
+
// the one that matters in practice: the API restarting under a long
|
|
109
|
+
// turn drops the socket, and without it here that surfaced as a raw
|
|
110
|
+
// "read ECONNRESET / REQUEST_FAILED" with nothing to act on.
|
|
111
|
+
if (["ECONNREFUSED", "ENOTFOUND", "ECONNABORTED", "ETIMEDOUT", "ECONNRESET", "EPIPE"].includes(err.code ?? "")) {
|
|
112
|
+
const dropped = err.code === "ECONNRESET" || err.code === "EPIPE";
|
|
113
|
+
throw new ApiError("NETWORK_ERROR", dropped
|
|
114
|
+
? "The connection to the API dropped mid-request."
|
|
115
|
+
: `Could not reach the API at ${this.baseUrl}.`, 0);
|
|
78
116
|
}
|
|
79
117
|
// Reached a server, but it didn't speak our envelope (wrong URL, proxy,
|
|
80
118
|
// gateway page, …). Never surface an empty message.
|
|
@@ -97,8 +135,80 @@ export class ApiClient {
|
|
|
97
135
|
return this.request({ method: "GET", url: "/models/chat" });
|
|
98
136
|
}
|
|
99
137
|
// ── Chat ────────────────────────────────────────────────────────────────────
|
|
100
|
-
|
|
101
|
-
|
|
138
|
+
/** `signal` lets the caller abandon the request (Ctrl+C during a turn). */
|
|
139
|
+
chat(body, signal) {
|
|
140
|
+
return this.request({ method: "POST", url: "/chat", data: body, signal });
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* The same turn as `chat()`, over POST /v1/chat/stream. Resolves to the same
|
|
144
|
+
* `ChatResult`, so callers render it identically; along the way it reports
|
|
145
|
+
* answer deltas, progress and local tool requests through `handlers`.
|
|
146
|
+
*
|
|
147
|
+
* Aborting (Ctrl+C) actually stops the turn here: the server sees the socket
|
|
148
|
+
* close and cancels the model call, unlike the blocking endpoint, where the
|
|
149
|
+
* server finished — and billed — a reply nobody was waiting for.
|
|
150
|
+
*/
|
|
151
|
+
async chatStream(body, handlers = {}, signal, retry = true) {
|
|
152
|
+
const token = this.token();
|
|
153
|
+
if (!token)
|
|
154
|
+
throw new ApiError("NOT_AUTHENTICATED", "You're not signed in.", 401);
|
|
155
|
+
let res;
|
|
156
|
+
try {
|
|
157
|
+
res = await this.http.request({
|
|
158
|
+
method: "POST",
|
|
159
|
+
url: "/chat/stream",
|
|
160
|
+
data: body,
|
|
161
|
+
signal,
|
|
162
|
+
responseType: "stream",
|
|
163
|
+
// No client-side timeout: a turn legitimately idles while a local tool
|
|
164
|
+
// runs, and the server's keep-alive pings hold the socket open.
|
|
165
|
+
timeout: 0,
|
|
166
|
+
headers: { Authorization: `Bearer ${token}`, Accept: "text/event-stream" },
|
|
167
|
+
validateStatus: () => true
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
catch (err) {
|
|
171
|
+
if (isAxiosError(err) && (err.code === "ERR_CANCELED" || err.name === "CanceledError")) {
|
|
172
|
+
throw new ApiError("ABORTED", "Interrupted.", 0);
|
|
173
|
+
}
|
|
174
|
+
if (isAxiosError(err) && ["ECONNREFUSED", "ENOTFOUND", "ETIMEDOUT", "ECONNRESET", "EPIPE"].includes(err.code ?? "")) {
|
|
175
|
+
throw new ApiError("NETWORK_ERROR", `Could not reach the API at ${this.baseUrl}.`, 0);
|
|
176
|
+
}
|
|
177
|
+
throw new ApiError("REQUEST_FAILED", err instanceof Error ? err.message : String(err));
|
|
178
|
+
}
|
|
179
|
+
// Anything but 200 is a plain JSON error sent before the stream opened.
|
|
180
|
+
if (res.status !== 200) {
|
|
181
|
+
const text = await readAll(res.data);
|
|
182
|
+
let env;
|
|
183
|
+
try {
|
|
184
|
+
env = JSON.parse(text);
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
env = undefined;
|
|
188
|
+
}
|
|
189
|
+
if (res.status === 401 && retry && typeof this.auth === "object" && this.auth.refresh) {
|
|
190
|
+
const renewed = await this.auth.refresh(token);
|
|
191
|
+
if (renewed)
|
|
192
|
+
return this.chatStream(body, handlers, signal, false);
|
|
193
|
+
}
|
|
194
|
+
throw new ApiError(env?.error?.code ?? `HTTP_${res.status}`, env?.error?.message ?? `Request failed (HTTP ${res.status}).`, res.status);
|
|
195
|
+
}
|
|
196
|
+
try {
|
|
197
|
+
return await consumeChatStream(res.data, handlers, signal);
|
|
198
|
+
}
|
|
199
|
+
catch (err) {
|
|
200
|
+
if (err instanceof StreamError)
|
|
201
|
+
throw new ApiError(err.code, err.message, err.status);
|
|
202
|
+
throw err;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
/** Answer a `client_tool_call` (POST /v1/chat/tool-result). */
|
|
206
|
+
/** Record an undo the user ran with /undo, outside any turn. Metadata only. */
|
|
207
|
+
recordMutationAudit(payload) {
|
|
208
|
+
return this.request({ method: "POST", url: "/chat/mutation-audit", data: payload });
|
|
209
|
+
}
|
|
210
|
+
submitToolResult(payload) {
|
|
211
|
+
return this.request({ method: "POST", url: "/chat/tool-result", data: payload });
|
|
102
212
|
}
|
|
103
213
|
// ── Sessions / conversations ─────────────────────────────────────────────────
|
|
104
214
|
conversations(params) {
|
|
@@ -116,6 +226,30 @@ export class ApiClient {
|
|
|
116
226
|
deleteSession(id) {
|
|
117
227
|
return this.request({ method: "DELETE", url: `/sessions/${id}` });
|
|
118
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* A read-only side question about a session ("/btw"). Persists nothing: no
|
|
231
|
+
* message is stored and the conversation is untouched — which is why it is
|
|
232
|
+
* NOT `chat()`, since /v1/chat always writes both turns.
|
|
233
|
+
*/
|
|
234
|
+
askAboutSession(sessionId, question, signal) {
|
|
235
|
+
return this.request({
|
|
236
|
+
method: "POST",
|
|
237
|
+
url: `/sessions/${sessionId}/ask`,
|
|
238
|
+
data: { question },
|
|
239
|
+
signal
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
// ── Memory (snapshots + restructures) ───────────────────────────────────────
|
|
243
|
+
/** One artifact, by the `artifact_id` carried on a transcript marker. */
|
|
244
|
+
memoryArtifact(id) {
|
|
245
|
+
return this.request({ method: "GET", url: `/memory/${id}` });
|
|
246
|
+
}
|
|
247
|
+
// ── SIA clarification ───────────────────────────────────────────────────────
|
|
248
|
+
/** Questions worth asking about an unsent prompt. Never throws for content
|
|
249
|
+
* reasons — the API answers `{skip:true}` when there's nothing to ask. */
|
|
250
|
+
siaQuestions(text) {
|
|
251
|
+
return this.request({ method: "POST", url: "/sia/questions", data: { text } });
|
|
252
|
+
}
|
|
119
253
|
// ── Usage ────────────────────────────────────────────────────────────────────
|
|
120
254
|
usage() {
|
|
121
255
|
return this.request({ method: "GET", url: "/usage" });
|