indusagi-coding-agent 0.1.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/CHANGELOG.md +2249 -0
- package/README.md +546 -0
- package/dist/cli/args.js +282 -0
- package/dist/cli/config-selector.js +30 -0
- package/dist/cli/file-processor.js +78 -0
- package/dist/cli/list-models.js +91 -0
- package/dist/cli/session-picker.js +31 -0
- package/dist/cli.js +10 -0
- package/dist/config.js +158 -0
- package/dist/core/agent-session.js +2097 -0
- package/dist/core/auth-storage.js +278 -0
- package/dist/core/bash-executor.js +211 -0
- package/dist/core/compaction/branch-summarization.js +241 -0
- package/dist/core/compaction/compaction.js +606 -0
- package/dist/core/compaction/index.js +6 -0
- package/dist/core/compaction/utils.js +137 -0
- package/dist/core/diagnostics.js +1 -0
- package/dist/core/event-bus.js +24 -0
- package/dist/core/exec.js +70 -0
- package/dist/core/export-html/ansi-to-html.js +248 -0
- package/dist/core/export-html/index.js +221 -0
- package/dist/core/export-html/template.css +905 -0
- package/dist/core/export-html/template.html +54 -0
- package/dist/core/export-html/template.js +1549 -0
- package/dist/core/export-html/tool-renderer.js +56 -0
- package/dist/core/export-html/vendor/highlight.min.js +1213 -0
- package/dist/core/export-html/vendor/marked.min.js +6 -0
- package/dist/core/extensions/index.js +8 -0
- package/dist/core/extensions/loader.js +395 -0
- package/dist/core/extensions/runner.js +499 -0
- package/dist/core/extensions/types.js +31 -0
- package/dist/core/extensions/wrapper.js +101 -0
- package/dist/core/footer-data-provider.js +133 -0
- package/dist/core/index.js +8 -0
- package/dist/core/keybindings.js +140 -0
- package/dist/core/messages.js +122 -0
- package/dist/core/model-registry.js +454 -0
- package/dist/core/model-resolver.js +309 -0
- package/dist/core/package-manager.js +1142 -0
- package/dist/core/prompt-templates.js +250 -0
- package/dist/core/resource-loader.js +569 -0
- package/dist/core/sdk.js +225 -0
- package/dist/core/session-manager.js +1078 -0
- package/dist/core/settings-manager.js +430 -0
- package/dist/core/skills.js +339 -0
- package/dist/core/system-prompt.js +136 -0
- package/dist/core/timings.js +24 -0
- package/dist/core/tools/bash.js +226 -0
- package/dist/core/tools/edit-diff.js +242 -0
- package/dist/core/tools/edit.js +145 -0
- package/dist/core/tools/find.js +205 -0
- package/dist/core/tools/grep.js +238 -0
- package/dist/core/tools/index.js +60 -0
- package/dist/core/tools/ls.js +117 -0
- package/dist/core/tools/path-utils.js +52 -0
- package/dist/core/tools/read.js +165 -0
- package/dist/core/tools/truncate.js +204 -0
- package/dist/core/tools/write.js +77 -0
- package/dist/index.js +41 -0
- package/dist/main.js +565 -0
- package/dist/migrations.js +260 -0
- package/dist/modes/index.js +7 -0
- package/dist/modes/interactive/components/armin.js +328 -0
- package/dist/modes/interactive/components/assistant-message.js +86 -0
- package/dist/modes/interactive/components/bash-execution.js +155 -0
- package/dist/modes/interactive/components/bordered-loader.js +47 -0
- package/dist/modes/interactive/components/branch-summary-message.js +41 -0
- package/dist/modes/interactive/components/compaction-summary-message.js +42 -0
- package/dist/modes/interactive/components/config-selector.js +458 -0
- package/dist/modes/interactive/components/countdown-timer.js +27 -0
- package/dist/modes/interactive/components/custom-editor.js +61 -0
- package/dist/modes/interactive/components/custom-message.js +80 -0
- package/dist/modes/interactive/components/diff.js +132 -0
- package/dist/modes/interactive/components/dynamic-border.js +19 -0
- package/dist/modes/interactive/components/extension-editor.js +96 -0
- package/dist/modes/interactive/components/extension-input.js +54 -0
- package/dist/modes/interactive/components/extension-selector.js +70 -0
- package/dist/modes/interactive/components/footer.js +213 -0
- package/dist/modes/interactive/components/index.js +31 -0
- package/dist/modes/interactive/components/keybinding-hints.js +60 -0
- package/dist/modes/interactive/components/login-dialog.js +138 -0
- package/dist/modes/interactive/components/model-selector.js +253 -0
- package/dist/modes/interactive/components/oauth-selector.js +91 -0
- package/dist/modes/interactive/components/scoped-models-selector.js +262 -0
- package/dist/modes/interactive/components/session-selector-search.js +145 -0
- package/dist/modes/interactive/components/session-selector.js +698 -0
- package/dist/modes/interactive/components/settings-selector.js +250 -0
- package/dist/modes/interactive/components/show-images-selector.js +33 -0
- package/dist/modes/interactive/components/skill-invocation-message.js +44 -0
- package/dist/modes/interactive/components/theme-selector.js +43 -0
- package/dist/modes/interactive/components/thinking-selector.js +45 -0
- package/dist/modes/interactive/components/tool-execution.js +608 -0
- package/dist/modes/interactive/components/tree-selector.js +892 -0
- package/dist/modes/interactive/components/user-message-selector.js +109 -0
- package/dist/modes/interactive/components/user-message.js +15 -0
- package/dist/modes/interactive/components/visual-truncate.js +32 -0
- package/dist/modes/interactive/interactive-mode.js +3576 -0
- package/dist/modes/interactive/theme/dark.json +85 -0
- package/dist/modes/interactive/theme/light.json +84 -0
- package/dist/modes/interactive/theme/theme-schema.json +335 -0
- package/dist/modes/interactive/theme/theme.js +938 -0
- package/dist/modes/print-mode.js +96 -0
- package/dist/modes/rpc/rpc-client.js +390 -0
- package/dist/modes/rpc/rpc-mode.js +448 -0
- package/dist/modes/rpc/rpc-types.js +7 -0
- package/dist/utils/changelog.js +86 -0
- package/dist/utils/clipboard-image.js +116 -0
- package/dist/utils/clipboard.js +58 -0
- package/dist/utils/frontmatter.js +25 -0
- package/dist/utils/git.js +5 -0
- package/dist/utils/image-convert.js +34 -0
- package/dist/utils/image-resize.js +180 -0
- package/dist/utils/mime.js +25 -0
- package/dist/utils/photon.js +120 -0
- package/dist/utils/shell.js +164 -0
- package/dist/utils/sleep.js +16 -0
- package/dist/utils/tools-manager.js +186 -0
- package/docs/compaction.md +390 -0
- package/docs/custom-provider.md +538 -0
- package/docs/development.md +69 -0
- package/docs/extensions.md +1733 -0
- package/docs/images/doom-extension.png +0 -0
- package/docs/images/interactive-mode.png +0 -0
- package/docs/images/tree-view.png +0 -0
- package/docs/json.md +79 -0
- package/docs/keybindings.md +162 -0
- package/docs/models.md +193 -0
- package/docs/packages.md +163 -0
- package/docs/prompt-templates.md +67 -0
- package/docs/providers.md +147 -0
- package/docs/rpc.md +1048 -0
- package/docs/sdk.md +957 -0
- package/docs/session.md +412 -0
- package/docs/settings.md +216 -0
- package/docs/shell-aliases.md +13 -0
- package/docs/skills.md +226 -0
- package/docs/terminal-setup.md +65 -0
- package/docs/themes.md +295 -0
- package/docs/tree.md +219 -0
- package/docs/tui.md +887 -0
- package/docs/windows.md +17 -0
- package/examples/README.md +25 -0
- package/examples/extensions/README.md +192 -0
- package/examples/extensions/antigravity-image-gen.ts +414 -0
- package/examples/extensions/auto-commit-on-exit.ts +49 -0
- package/examples/extensions/bookmark.ts +50 -0
- package/examples/extensions/claude-rules.ts +86 -0
- package/examples/extensions/confirm-destructive.ts +59 -0
- package/examples/extensions/custom-compaction.ts +115 -0
- package/examples/extensions/custom-footer.ts +65 -0
- package/examples/extensions/custom-header.ts +73 -0
- package/examples/extensions/custom-provider-anthropic/index.ts +605 -0
- package/examples/extensions/custom-provider-anthropic/package-lock.json +24 -0
- package/examples/extensions/custom-provider-anthropic/package.json +19 -0
- package/examples/extensions/custom-provider-gitlab-duo/index.ts +350 -0
- package/examples/extensions/custom-provider-gitlab-duo/package.json +16 -0
- package/examples/extensions/custom-provider-gitlab-duo/test.ts +83 -0
- package/examples/extensions/dirty-repo-guard.ts +56 -0
- package/examples/extensions/doom-overlay/README.md +46 -0
- package/examples/extensions/doom-overlay/doom/build/doom.js +21 -0
- package/examples/extensions/doom-overlay/doom/build/doom.wasm +0 -0
- package/examples/extensions/doom-overlay/doom/build.sh +152 -0
- package/examples/extensions/doom-overlay/doom/doomgeneric_pi.c +72 -0
- package/examples/extensions/doom-overlay/doom-component.ts +133 -0
- package/examples/extensions/doom-overlay/doom-engine.ts +173 -0
- package/examples/extensions/doom-overlay/doom-keys.ts +105 -0
- package/examples/extensions/doom-overlay/index.ts +74 -0
- package/examples/extensions/doom-overlay/wad-finder.ts +51 -0
- package/examples/extensions/event-bus.ts +43 -0
- package/examples/extensions/file-trigger.ts +41 -0
- package/examples/extensions/git-checkpoint.ts +53 -0
- package/examples/extensions/handoff.ts +151 -0
- package/examples/extensions/hello.ts +25 -0
- package/examples/extensions/inline-bash.ts +94 -0
- package/examples/extensions/input-transform.ts +43 -0
- package/examples/extensions/interactive-shell.ts +196 -0
- package/examples/extensions/mac-system-theme.ts +47 -0
- package/examples/extensions/message-renderer.ts +60 -0
- package/examples/extensions/modal-editor.ts +86 -0
- package/examples/extensions/model-status.ts +31 -0
- package/examples/extensions/notify.ts +25 -0
- package/examples/extensions/overlay-qa-tests.ts +882 -0
- package/examples/extensions/overlay-test.ts +151 -0
- package/examples/extensions/permission-gate.ts +34 -0
- package/examples/extensions/pirate.ts +47 -0
- package/examples/extensions/plan-mode/README.md +65 -0
- package/examples/extensions/plan-mode/index.ts +341 -0
- package/examples/extensions/plan-mode/utils.ts +168 -0
- package/examples/extensions/preset.ts +399 -0
- package/examples/extensions/protected-paths.ts +30 -0
- package/examples/extensions/qna.ts +120 -0
- package/examples/extensions/question.ts +265 -0
- package/examples/extensions/questionnaire.ts +428 -0
- package/examples/extensions/rainbow-editor.ts +88 -0
- package/examples/extensions/sandbox/index.ts +318 -0
- package/examples/extensions/sandbox/package-lock.json +92 -0
- package/examples/extensions/sandbox/package.json +19 -0
- package/examples/extensions/send-user-message.ts +97 -0
- package/examples/extensions/session-name.ts +27 -0
- package/examples/extensions/shutdown-command.ts +63 -0
- package/examples/extensions/snake.ts +344 -0
- package/examples/extensions/space-invaders.ts +561 -0
- package/examples/extensions/ssh.ts +220 -0
- package/examples/extensions/status-line.ts +40 -0
- package/examples/extensions/subagent/README.md +172 -0
- package/examples/extensions/subagent/agents/planner.md +37 -0
- package/examples/extensions/subagent/agents/reviewer.md +35 -0
- package/examples/extensions/subagent/agents/scout.md +50 -0
- package/examples/extensions/subagent/agents/worker.md +24 -0
- package/examples/extensions/subagent/agents.ts +127 -0
- package/examples/extensions/subagent/index.ts +964 -0
- package/examples/extensions/subagent/prompts/implement-and-review.md +10 -0
- package/examples/extensions/subagent/prompts/implement.md +10 -0
- package/examples/extensions/subagent/prompts/scout-and-plan.md +9 -0
- package/examples/extensions/summarize.ts +196 -0
- package/examples/extensions/timed-confirm.ts +70 -0
- package/examples/extensions/todo.ts +300 -0
- package/examples/extensions/tool-override.ts +144 -0
- package/examples/extensions/tools.ts +147 -0
- package/examples/extensions/trigger-compact.ts +40 -0
- package/examples/extensions/truncated-tool.ts +193 -0
- package/examples/extensions/widget-placement.ts +17 -0
- package/examples/extensions/with-deps/index.ts +36 -0
- package/examples/extensions/with-deps/package-lock.json +31 -0
- package/examples/extensions/with-deps/package.json +22 -0
- package/examples/sdk/01-minimal.ts +22 -0
- package/examples/sdk/02-custom-model.ts +50 -0
- package/examples/sdk/03-custom-prompt.ts +55 -0
- package/examples/sdk/04-skills.ts +46 -0
- package/examples/sdk/05-tools.ts +56 -0
- package/examples/sdk/06-extensions.ts +88 -0
- package/examples/sdk/07-context-files.ts +40 -0
- package/examples/sdk/08-prompt-templates.ts +47 -0
- package/examples/sdk/09-api-keys-and-oauth.ts +48 -0
- package/examples/sdk/10-settings.ts +38 -0
- package/examples/sdk/11-sessions.ts +48 -0
- package/examples/sdk/12-full-control.ts +82 -0
- package/examples/sdk/13-codex-oauth.ts +37 -0
- package/examples/sdk/README.md +144 -0
- package/package.json +85 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Print mode (single-shot): Send prompts, output result, exit.
|
|
3
|
+
*
|
|
4
|
+
* Used for:
|
|
5
|
+
* - `indusagi -p "prompt"` - text output
|
|
6
|
+
* - `indusagi --mode json "prompt"` - JSON event stream
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Run in print (single-shot) mode.
|
|
10
|
+
* Sends prompts to the agent and outputs the result.
|
|
11
|
+
*/
|
|
12
|
+
export async function runPrintMode(session, options) {
|
|
13
|
+
const { mode, messages = [], initialMessage, initialImages } = options;
|
|
14
|
+
if (mode === "json") {
|
|
15
|
+
const header = session.sessionManager.getHeader();
|
|
16
|
+
if (header) {
|
|
17
|
+
console.log(JSON.stringify(header));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
// Set up extensions for print mode (no UI)
|
|
21
|
+
const extensionRunner = session.extensionRunner;
|
|
22
|
+
if (extensionRunner) {
|
|
23
|
+
await session.bindExtensions({
|
|
24
|
+
commandContextActions: {
|
|
25
|
+
waitForIdle: () => session.agent.waitForIdle(),
|
|
26
|
+
newSession: async (options) => {
|
|
27
|
+
const success = await session.newSession({ parentSession: options?.parentSession });
|
|
28
|
+
if (success && options?.setup) {
|
|
29
|
+
await options.setup(session.sessionManager);
|
|
30
|
+
}
|
|
31
|
+
return { cancelled: !success };
|
|
32
|
+
},
|
|
33
|
+
fork: async (entryId) => {
|
|
34
|
+
const result = await session.fork(entryId);
|
|
35
|
+
return { cancelled: result.cancelled };
|
|
36
|
+
},
|
|
37
|
+
navigateTree: async (targetId, options) => {
|
|
38
|
+
const result = await session.navigateTree(targetId, {
|
|
39
|
+
summarize: options?.summarize,
|
|
40
|
+
customInstructions: options?.customInstructions,
|
|
41
|
+
replaceInstructions: options?.replaceInstructions,
|
|
42
|
+
label: options?.label,
|
|
43
|
+
});
|
|
44
|
+
return { cancelled: result.cancelled };
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
onError: (err) => {
|
|
48
|
+
console.error(`Extension error (${err.extensionPath}): ${err.error}`);
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
// Always subscribe to enable session persistence via _handleAgentEvent
|
|
53
|
+
session.subscribe((event) => {
|
|
54
|
+
// In JSON mode, output all events
|
|
55
|
+
if (mode === "json") {
|
|
56
|
+
console.log(JSON.stringify(event));
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
// Send initial message with attachments
|
|
60
|
+
if (initialMessage) {
|
|
61
|
+
await session.prompt(initialMessage, { images: initialImages });
|
|
62
|
+
}
|
|
63
|
+
// Send remaining messages
|
|
64
|
+
for (const message of messages) {
|
|
65
|
+
await session.prompt(message);
|
|
66
|
+
}
|
|
67
|
+
// In text mode, output final response
|
|
68
|
+
if (mode === "text") {
|
|
69
|
+
const state = session.state;
|
|
70
|
+
const lastMessage = state.messages[state.messages.length - 1];
|
|
71
|
+
if (lastMessage?.role === "assistant") {
|
|
72
|
+
const assistantMsg = lastMessage;
|
|
73
|
+
// Check for error/aborted
|
|
74
|
+
if (assistantMsg.stopReason === "error" || assistantMsg.stopReason === "aborted") {
|
|
75
|
+
console.error(assistantMsg.errorMessage || `Request ${assistantMsg.stopReason}`);
|
|
76
|
+
process.exit(1);
|
|
77
|
+
}
|
|
78
|
+
// Output text content
|
|
79
|
+
for (const content of assistantMsg.content) {
|
|
80
|
+
if (content.type === "text") {
|
|
81
|
+
console.log(content.text);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// Ensure stdout is fully flushed before returning
|
|
87
|
+
// This prevents race conditions where the process exits before all output is written
|
|
88
|
+
await new Promise((resolve, reject) => {
|
|
89
|
+
process.stdout.write("", (err) => {
|
|
90
|
+
if (err)
|
|
91
|
+
reject(err);
|
|
92
|
+
else
|
|
93
|
+
resolve();
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
}
|
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RPC Client for programmatic access to the coding agent.
|
|
3
|
+
*
|
|
4
|
+
* Spawns the agent in RPC mode and provides a typed API for all operations.
|
|
5
|
+
*/
|
|
6
|
+
import { spawn } from "node:child_process";
|
|
7
|
+
import * as readline from "node:readline";
|
|
8
|
+
// ============================================================================
|
|
9
|
+
// RPC Client
|
|
10
|
+
// ============================================================================
|
|
11
|
+
export class RpcClient {
|
|
12
|
+
constructor(options = {}) {
|
|
13
|
+
this.options = options;
|
|
14
|
+
this.process = null;
|
|
15
|
+
this.rl = null;
|
|
16
|
+
this.eventListeners = [];
|
|
17
|
+
this.pendingRequests = new Map();
|
|
18
|
+
this.requestId = 0;
|
|
19
|
+
this.stderr = "";
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Start the RPC agent process.
|
|
23
|
+
*/
|
|
24
|
+
async start() {
|
|
25
|
+
if (this.process) {
|
|
26
|
+
throw new Error("Client already started");
|
|
27
|
+
}
|
|
28
|
+
const cliPath = this.options.cliPath ?? "dist/cli.js";
|
|
29
|
+
const args = ["--mode", "rpc"];
|
|
30
|
+
if (this.options.provider) {
|
|
31
|
+
args.push("--provider", this.options.provider);
|
|
32
|
+
}
|
|
33
|
+
if (this.options.model) {
|
|
34
|
+
args.push("--model", this.options.model);
|
|
35
|
+
}
|
|
36
|
+
if (this.options.args) {
|
|
37
|
+
args.push(...this.options.args);
|
|
38
|
+
}
|
|
39
|
+
this.process = spawn("node", [cliPath, ...args], {
|
|
40
|
+
cwd: this.options.cwd,
|
|
41
|
+
env: { ...process.env, ...this.options.env },
|
|
42
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
43
|
+
});
|
|
44
|
+
// Collect stderr for debugging
|
|
45
|
+
this.process.stderr?.on("data", (data) => {
|
|
46
|
+
this.stderr += data.toString();
|
|
47
|
+
});
|
|
48
|
+
// Set up line reader for stdout
|
|
49
|
+
this.rl = readline.createInterface({
|
|
50
|
+
input: this.process.stdout,
|
|
51
|
+
terminal: false,
|
|
52
|
+
});
|
|
53
|
+
this.rl.on("line", (line) => {
|
|
54
|
+
this.handleLine(line);
|
|
55
|
+
});
|
|
56
|
+
// Wait a moment for process to initialize
|
|
57
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
58
|
+
if (this.process.exitCode !== null) {
|
|
59
|
+
throw new Error(`Agent process exited immediately with code ${this.process.exitCode}. Stderr: ${this.stderr}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Stop the RPC agent process.
|
|
64
|
+
*/
|
|
65
|
+
async stop() {
|
|
66
|
+
if (!this.process)
|
|
67
|
+
return;
|
|
68
|
+
this.rl?.close();
|
|
69
|
+
this.process.kill("SIGTERM");
|
|
70
|
+
// Wait for process to exit
|
|
71
|
+
await new Promise((resolve) => {
|
|
72
|
+
const timeout = setTimeout(() => {
|
|
73
|
+
this.process?.kill("SIGKILL");
|
|
74
|
+
resolve();
|
|
75
|
+
}, 1000);
|
|
76
|
+
this.process?.on("exit", () => {
|
|
77
|
+
clearTimeout(timeout);
|
|
78
|
+
resolve();
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
this.process = null;
|
|
82
|
+
this.rl = null;
|
|
83
|
+
this.pendingRequests.clear();
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Subscribe to agent events.
|
|
87
|
+
*/
|
|
88
|
+
onEvent(listener) {
|
|
89
|
+
this.eventListeners.push(listener);
|
|
90
|
+
return () => {
|
|
91
|
+
const index = this.eventListeners.indexOf(listener);
|
|
92
|
+
if (index !== -1) {
|
|
93
|
+
this.eventListeners.splice(index, 1);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Get collected stderr output (useful for debugging).
|
|
99
|
+
*/
|
|
100
|
+
getStderr() {
|
|
101
|
+
return this.stderr;
|
|
102
|
+
}
|
|
103
|
+
// =========================================================================
|
|
104
|
+
// Command Methods
|
|
105
|
+
// =========================================================================
|
|
106
|
+
/**
|
|
107
|
+
* Send a prompt to the agent.
|
|
108
|
+
* Returns immediately after sending; use onEvent() to receive streaming events.
|
|
109
|
+
* Use waitForIdle() to wait for completion.
|
|
110
|
+
*/
|
|
111
|
+
async prompt(message, images) {
|
|
112
|
+
await this.send({ type: "prompt", message, images });
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Queue a steering message to interrupt the agent mid-run.
|
|
116
|
+
*/
|
|
117
|
+
async steer(message) {
|
|
118
|
+
await this.send({ type: "steer", message });
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Queue a follow-up message to be processed after the agent finishes.
|
|
122
|
+
*/
|
|
123
|
+
async followUp(message) {
|
|
124
|
+
await this.send({ type: "follow_up", message });
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Abort current operation.
|
|
128
|
+
*/
|
|
129
|
+
async abort() {
|
|
130
|
+
await this.send({ type: "abort" });
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Start a new session, optionally with parent tracking.
|
|
134
|
+
* @param parentSession - Optional parent session path for lineage tracking
|
|
135
|
+
* @returns Object with `cancelled: true` if an extension cancelled the new session
|
|
136
|
+
*/
|
|
137
|
+
async newSession(parentSession) {
|
|
138
|
+
const response = await this.send({ type: "new_session", parentSession });
|
|
139
|
+
return this.getData(response);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Get current session state.
|
|
143
|
+
*/
|
|
144
|
+
async getState() {
|
|
145
|
+
const response = await this.send({ type: "get_state" });
|
|
146
|
+
return this.getData(response);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Set model by provider and ID.
|
|
150
|
+
*/
|
|
151
|
+
async setModel(provider, modelId) {
|
|
152
|
+
const response = await this.send({ type: "set_model", provider, modelId });
|
|
153
|
+
return this.getData(response);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Cycle to next model.
|
|
157
|
+
*/
|
|
158
|
+
async cycleModel() {
|
|
159
|
+
const response = await this.send({ type: "cycle_model" });
|
|
160
|
+
return this.getData(response);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Get list of available models.
|
|
164
|
+
*/
|
|
165
|
+
async getAvailableModels() {
|
|
166
|
+
const response = await this.send({ type: "get_available_models" });
|
|
167
|
+
return this.getData(response).models;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Set thinking level.
|
|
171
|
+
*/
|
|
172
|
+
async setThinkingLevel(level) {
|
|
173
|
+
await this.send({ type: "set_thinking_level", level });
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Cycle thinking level.
|
|
177
|
+
*/
|
|
178
|
+
async cycleThinkingLevel() {
|
|
179
|
+
const response = await this.send({ type: "cycle_thinking_level" });
|
|
180
|
+
return this.getData(response);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Set steering mode.
|
|
184
|
+
*/
|
|
185
|
+
async setSteeringMode(mode) {
|
|
186
|
+
await this.send({ type: "set_steering_mode", mode });
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Set follow-up mode.
|
|
190
|
+
*/
|
|
191
|
+
async setFollowUpMode(mode) {
|
|
192
|
+
await this.send({ type: "set_follow_up_mode", mode });
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Compact session context.
|
|
196
|
+
*/
|
|
197
|
+
async compact(customInstructions) {
|
|
198
|
+
const response = await this.send({ type: "compact", customInstructions });
|
|
199
|
+
return this.getData(response);
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Set auto-compaction enabled/disabled.
|
|
203
|
+
*/
|
|
204
|
+
async setAutoCompaction(enabled) {
|
|
205
|
+
await this.send({ type: "set_auto_compaction", enabled });
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Set auto-retry enabled/disabled.
|
|
209
|
+
*/
|
|
210
|
+
async setAutoRetry(enabled) {
|
|
211
|
+
await this.send({ type: "set_auto_retry", enabled });
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Abort in-progress retry.
|
|
215
|
+
*/
|
|
216
|
+
async abortRetry() {
|
|
217
|
+
await this.send({ type: "abort_retry" });
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Execute a bash command.
|
|
221
|
+
*/
|
|
222
|
+
async bash(command) {
|
|
223
|
+
const response = await this.send({ type: "bash", command });
|
|
224
|
+
return this.getData(response);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Abort running bash command.
|
|
228
|
+
*/
|
|
229
|
+
async abortBash() {
|
|
230
|
+
await this.send({ type: "abort_bash" });
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Get session statistics.
|
|
234
|
+
*/
|
|
235
|
+
async getSessionStats() {
|
|
236
|
+
const response = await this.send({ type: "get_session_stats" });
|
|
237
|
+
return this.getData(response);
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Export session to HTML.
|
|
241
|
+
*/
|
|
242
|
+
async exportHtml(outputPath) {
|
|
243
|
+
const response = await this.send({ type: "export_html", outputPath });
|
|
244
|
+
return this.getData(response);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Switch to a different session file.
|
|
248
|
+
* @returns Object with `cancelled: true` if an extension cancelled the switch
|
|
249
|
+
*/
|
|
250
|
+
async switchSession(sessionPath) {
|
|
251
|
+
const response = await this.send({ type: "switch_session", sessionPath });
|
|
252
|
+
return this.getData(response);
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Fork from a specific message.
|
|
256
|
+
* @returns Object with `text` (the message text) and `cancelled` (if extension cancelled)
|
|
257
|
+
*/
|
|
258
|
+
async fork(entryId) {
|
|
259
|
+
const response = await this.send({ type: "fork", entryId });
|
|
260
|
+
return this.getData(response);
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Get messages available for forking.
|
|
264
|
+
*/
|
|
265
|
+
async getForkMessages() {
|
|
266
|
+
const response = await this.send({ type: "get_fork_messages" });
|
|
267
|
+
return this.getData(response).messages;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Get text of last assistant message.
|
|
271
|
+
*/
|
|
272
|
+
async getLastAssistantText() {
|
|
273
|
+
const response = await this.send({ type: "get_last_assistant_text" });
|
|
274
|
+
return this.getData(response).text;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Get all messages in the session.
|
|
278
|
+
*/
|
|
279
|
+
async getMessages() {
|
|
280
|
+
const response = await this.send({ type: "get_messages" });
|
|
281
|
+
return this.getData(response).messages;
|
|
282
|
+
}
|
|
283
|
+
// =========================================================================
|
|
284
|
+
// Helpers
|
|
285
|
+
// =========================================================================
|
|
286
|
+
/**
|
|
287
|
+
* Wait for agent to become idle (no streaming).
|
|
288
|
+
* Resolves when agent_end event is received.
|
|
289
|
+
*/
|
|
290
|
+
waitForIdle(timeout = 60000) {
|
|
291
|
+
return new Promise((resolve, reject) => {
|
|
292
|
+
const timer = setTimeout(() => {
|
|
293
|
+
unsubscribe();
|
|
294
|
+
reject(new Error(`Timeout waiting for agent to become idle. Stderr: ${this.stderr}`));
|
|
295
|
+
}, timeout);
|
|
296
|
+
const unsubscribe = this.onEvent((event) => {
|
|
297
|
+
if (event.type === "agent_end") {
|
|
298
|
+
clearTimeout(timer);
|
|
299
|
+
unsubscribe();
|
|
300
|
+
resolve();
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Collect events until agent becomes idle.
|
|
307
|
+
*/
|
|
308
|
+
collectEvents(timeout = 60000) {
|
|
309
|
+
return new Promise((resolve, reject) => {
|
|
310
|
+
const events = [];
|
|
311
|
+
const timer = setTimeout(() => {
|
|
312
|
+
unsubscribe();
|
|
313
|
+
reject(new Error(`Timeout collecting events. Stderr: ${this.stderr}`));
|
|
314
|
+
}, timeout);
|
|
315
|
+
const unsubscribe = this.onEvent((event) => {
|
|
316
|
+
events.push(event);
|
|
317
|
+
if (event.type === "agent_end") {
|
|
318
|
+
clearTimeout(timer);
|
|
319
|
+
unsubscribe();
|
|
320
|
+
resolve(events);
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Send prompt and wait for completion, returning all events.
|
|
327
|
+
*/
|
|
328
|
+
async promptAndWait(message, images, timeout = 60000) {
|
|
329
|
+
const eventsPromise = this.collectEvents(timeout);
|
|
330
|
+
await this.prompt(message, images);
|
|
331
|
+
return eventsPromise;
|
|
332
|
+
}
|
|
333
|
+
// =========================================================================
|
|
334
|
+
// Internal
|
|
335
|
+
// =========================================================================
|
|
336
|
+
handleLine(line) {
|
|
337
|
+
try {
|
|
338
|
+
const data = JSON.parse(line);
|
|
339
|
+
// Check if it's a response to a pending request
|
|
340
|
+
if (data.type === "response" && data.id && this.pendingRequests.has(data.id)) {
|
|
341
|
+
const pending = this.pendingRequests.get(data.id);
|
|
342
|
+
this.pendingRequests.delete(data.id);
|
|
343
|
+
pending.resolve(data);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
// Otherwise it's an event
|
|
347
|
+
for (const listener of this.eventListeners) {
|
|
348
|
+
listener(data);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
catch {
|
|
352
|
+
// Ignore non-JSON lines
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
async send(command) {
|
|
356
|
+
if (!this.process?.stdin) {
|
|
357
|
+
throw new Error("Client not started");
|
|
358
|
+
}
|
|
359
|
+
const id = `req_${++this.requestId}`;
|
|
360
|
+
const fullCommand = { ...command, id };
|
|
361
|
+
return new Promise((resolve, reject) => {
|
|
362
|
+
this.pendingRequests.set(id, { resolve, reject });
|
|
363
|
+
const timeout = setTimeout(() => {
|
|
364
|
+
this.pendingRequests.delete(id);
|
|
365
|
+
reject(new Error(`Timeout waiting for response to ${command.type}. Stderr: ${this.stderr}`));
|
|
366
|
+
}, 30000);
|
|
367
|
+
this.pendingRequests.set(id, {
|
|
368
|
+
resolve: (response) => {
|
|
369
|
+
clearTimeout(timeout);
|
|
370
|
+
resolve(response);
|
|
371
|
+
},
|
|
372
|
+
reject: (error) => {
|
|
373
|
+
clearTimeout(timeout);
|
|
374
|
+
reject(error);
|
|
375
|
+
},
|
|
376
|
+
});
|
|
377
|
+
this.process.stdin.write(`${JSON.stringify(fullCommand)}\n`);
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
getData(response) {
|
|
381
|
+
if (!response.success) {
|
|
382
|
+
const errorResponse = response;
|
|
383
|
+
throw new Error(errorResponse.error);
|
|
384
|
+
}
|
|
385
|
+
// Type assertion: we trust response.data matches T based on the command sent.
|
|
386
|
+
// This is safe because each public method specifies the correct T for its command.
|
|
387
|
+
const successResponse = response;
|
|
388
|
+
return successResponse.data;
|
|
389
|
+
}
|
|
390
|
+
}
|