keycode-ai 0.0.0-stage → 0.1.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/CHANGELOG.md +101 -0
- package/LICENSE +201 -0
- package/README.md +547 -2
- package/dist/app/agent.d.ts +126 -0
- package/dist/app/agent.js +613 -0
- package/dist/app/agent.js.map +1 -0
- package/dist/app/commands.d.ts +15 -0
- package/dist/app/commands.js +281 -0
- package/dist/app/commands.js.map +1 -0
- package/dist/app/compact.d.ts +23 -0
- package/dist/app/compact.js +75 -0
- package/dist/app/compact.js.map +1 -0
- package/dist/app/export.d.ts +6 -0
- package/dist/app/export.js +56 -0
- package/dist/app/export.js.map +1 -0
- package/dist/app/factory.d.ts +106 -0
- package/dist/app/factory.js +687 -0
- package/dist/app/factory.js.map +1 -0
- package/dist/app/onboarding.d.ts +6 -0
- package/dist/app/onboarding.js +33 -0
- package/dist/app/onboarding.js.map +1 -0
- package/dist/app/subagent.d.ts +16 -0
- package/dist/app/subagent.js +14 -0
- package/dist/app/subagent.js.map +1 -0
- package/dist/app/trust.d.ts +6 -0
- package/dist/app/trust.js +74 -0
- package/dist/app/trust.js.map +1 -0
- package/dist/app/trustManifest.d.ts +18 -0
- package/dist/app/trustManifest.js +171 -0
- package/dist/app/trustManifest.js.map +1 -0
- package/dist/artifacts/store.d.ts +43 -0
- package/dist/artifacts/store.js +159 -0
- package/dist/artifacts/store.js.map +1 -0
- package/dist/cli/auth.d.ts +7 -0
- package/dist/cli/auth.js +68 -0
- package/dist/cli/auth.js.map +1 -0
- package/dist/cli/doctor.d.ts +19 -0
- package/dist/cli/doctor.js +205 -0
- package/dist/cli/doctor.js.map +1 -0
- package/dist/cli/extra.d.ts +2 -0
- package/dist/cli/extra.js +990 -0
- package/dist/cli/extra.js.map +1 -0
- package/dist/cli/headless.d.ts +20 -0
- package/dist/cli/headless.js +224 -0
- package/dist/cli/headless.js.map +1 -0
- package/dist/cli/index.d.ts +4 -0
- package/dist/cli/index.js +284 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/contracts/message.d.ts +34 -0
- package/dist/contracts/message.js +10 -0
- package/dist/contracts/message.js.map +1 -0
- package/dist/contracts/permissions.d.ts +16 -0
- package/dist/contracts/permissions.js +9 -0
- package/dist/contracts/permissions.js.map +1 -0
- package/dist/contracts/provider.d.ts +65 -0
- package/dist/contracts/provider.js +2 -0
- package/dist/contracts/provider.js.map +1 -0
- package/dist/contracts/session.d.ts +101 -0
- package/dist/contracts/session.js +11 -0
- package/dist/contracts/session.js.map +1 -0
- package/dist/contracts/tool.d.ts +81 -0
- package/dist/contracts/tool.js +15 -0
- package/dist/contracts/tool.js.map +1 -0
- package/dist/core/budget.d.ts +11 -0
- package/dist/core/budget.js +22 -0
- package/dist/core/budget.js.map +1 -0
- package/dist/core/context.d.ts +27 -0
- package/dist/core/context.js +92 -0
- package/dist/core/context.js.map +1 -0
- package/dist/extensions/commands.d.ts +23 -0
- package/dist/extensions/commands.js +103 -0
- package/dist/extensions/commands.js.map +1 -0
- package/dist/extensions/customAgents.d.ts +19 -0
- package/dist/extensions/customAgents.js +85 -0
- package/dist/extensions/customAgents.js.map +1 -0
- package/dist/extensions/frontmatter.d.ts +6 -0
- package/dist/extensions/frontmatter.js +73 -0
- package/dist/extensions/frontmatter.js.map +1 -0
- package/dist/extensions/hooks.d.ts +63 -0
- package/dist/extensions/hooks.js +145 -0
- package/dist/extensions/hooks.js.map +1 -0
- package/dist/extensions/loops.d.ts +27 -0
- package/dist/extensions/loops.js +119 -0
- package/dist/extensions/loops.js.map +1 -0
- package/dist/extensions/plugins.d.ts +29 -0
- package/dist/extensions/plugins.js +119 -0
- package/dist/extensions/plugins.js.map +1 -0
- package/dist/extensions/skills.d.ts +21 -0
- package/dist/extensions/skills.js +89 -0
- package/dist/extensions/skills.js.map +1 -0
- package/dist/extensions/statusline.d.ts +13 -0
- package/dist/extensions/statusline.js +30 -0
- package/dist/extensions/statusline.js.map +1 -0
- package/dist/extensions/styles.d.ts +14 -0
- package/dist/extensions/styles.js +79 -0
- package/dist/extensions/styles.js.map +1 -0
- package/dist/extensions/teams.d.ts +44 -0
- package/dist/extensions/teams.js +79 -0
- package/dist/extensions/teams.js.map +1 -0
- package/dist/extensions/themes.d.ts +25 -0
- package/dist/extensions/themes.js +109 -0
- package/dist/extensions/themes.js.map +1 -0
- package/dist/extensions/workflows.d.ts +28 -0
- package/dist/extensions/workflows.js +65 -0
- package/dist/extensions/workflows.js.map +1 -0
- package/dist/gateway/index.d.ts +25 -0
- package/dist/gateway/index.js +125 -0
- package/dist/gateway/index.js.map +1 -0
- package/dist/integrations/background.d.ts +35 -0
- package/dist/integrations/background.js +167 -0
- package/dist/integrations/background.js.map +1 -0
- package/dist/integrations/browser.d.ts +36 -0
- package/dist/integrations/browser.js +80 -0
- package/dist/integrations/browser.js.map +1 -0
- package/dist/integrations/github.d.ts +44 -0
- package/dist/integrations/github.js +72 -0
- package/dist/integrations/github.js.map +1 -0
- package/dist/integrations/ide.d.ts +29 -0
- package/dist/integrations/ide.js +69 -0
- package/dist/integrations/ide.js.map +1 -0
- package/dist/integrations/import.d.ts +14 -0
- package/dist/integrations/import.js +76 -0
- package/dist/integrations/import.js.map +1 -0
- package/dist/integrations/playwrightDriver.d.ts +8 -0
- package/dist/integrations/playwrightDriver.js +70 -0
- package/dist/integrations/playwrightDriver.js.map +1 -0
- package/dist/integrations/pr.d.ts +23 -0
- package/dist/integrations/pr.js +81 -0
- package/dist/integrations/pr.js.map +1 -0
- package/dist/integrations/remote.d.ts +107 -0
- package/dist/integrations/remote.js +636 -0
- package/dist/integrations/remote.js.map +1 -0
- package/dist/integrations/runner.d.ts +30 -0
- package/dist/integrations/runner.js +56 -0
- package/dist/integrations/runner.js.map +1 -0
- package/dist/integrations/worktrees.d.ts +17 -0
- package/dist/integrations/worktrees.js +40 -0
- package/dist/integrations/worktrees.js.map +1 -0
- package/dist/integrations/ws.d.ts +9 -0
- package/dist/integrations/ws.js +102 -0
- package/dist/integrations/ws.js.map +1 -0
- package/dist/lsp/client.d.ts +51 -0
- package/dist/lsp/client.js +251 -0
- package/dist/lsp/client.js.map +1 -0
- package/dist/mcp/client.d.ts +56 -0
- package/dist/mcp/client.js +153 -0
- package/dist/mcp/client.js.map +1 -0
- package/dist/mcp/management.d.ts +20 -0
- package/dist/mcp/management.js +97 -0
- package/dist/mcp/management.js.map +1 -0
- package/dist/mcp/tools.d.ts +3 -0
- package/dist/mcp/tools.js +26 -0
- package/dist/mcp/tools.js.map +1 -0
- package/dist/notifications/index.d.ts +25 -0
- package/dist/notifications/index.js +63 -0
- package/dist/notifications/index.js.map +1 -0
- package/dist/permissions/audit.d.ts +25 -0
- package/dist/permissions/audit.js +58 -0
- package/dist/permissions/audit.js.map +1 -0
- package/dist/permissions/broker.d.ts +20 -0
- package/dist/permissions/broker.js +51 -0
- package/dist/permissions/broker.js.map +1 -0
- package/dist/permissions/dangerous.d.ts +7 -0
- package/dist/permissions/dangerous.js +42 -0
- package/dist/permissions/dangerous.js.map +1 -0
- package/dist/permissions/pipeline.d.ts +34 -0
- package/dist/permissions/pipeline.js +217 -0
- package/dist/permissions/pipeline.js.map +1 -0
- package/dist/permissions/prompt.d.ts +2 -0
- package/dist/permissions/prompt.js +33 -0
- package/dist/permissions/prompt.js.map +1 -0
- package/dist/permissions/rules.d.ts +7 -0
- package/dist/permissions/rules.js +176 -0
- package/dist/permissions/rules.js.map +1 -0
- package/dist/platform/index.d.ts +16 -0
- package/dist/platform/index.js +55 -0
- package/dist/platform/index.js.map +1 -0
- package/dist/privacy/index.d.ts +27 -0
- package/dist/privacy/index.js +98 -0
- package/dist/privacy/index.js.map +1 -0
- package/dist/providers/anthropic.d.ts +25 -0
- package/dist/providers/anthropic.js +225 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/budget.d.ts +24 -0
- package/dist/providers/budget.js +47 -0
- package/dist/providers/budget.js.map +1 -0
- package/dist/providers/capabilities.d.ts +9 -0
- package/dist/providers/capabilities.js +39 -0
- package/dist/providers/capabilities.js.map +1 -0
- package/dist/providers/credentials.d.ts +21 -0
- package/dist/providers/credentials.js +86 -0
- package/dist/providers/credentials.js.map +1 -0
- package/dist/providers/fake.d.ts +32 -0
- package/dist/providers/fake.js +95 -0
- package/dist/providers/fake.js.map +1 -0
- package/dist/providers/openai.d.ts +27 -0
- package/dist/providers/openai.js +243 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/presets.d.ts +20 -0
- package/dist/providers/presets.js +166 -0
- package/dist/providers/presets.js.map +1 -0
- package/dist/providers/registry.d.ts +27 -0
- package/dist/providers/registry.js +127 -0
- package/dist/providers/registry.js.map +1 -0
- package/dist/providers/retry.d.ts +22 -0
- package/dist/providers/retry.js +69 -0
- package/dist/providers/retry.js.map +1 -0
- package/dist/providers/sse.d.ts +5 -0
- package/dist/providers/sse.js +74 -0
- package/dist/providers/sse.js.map +1 -0
- package/dist/providers/textProtocol.d.ts +10 -0
- package/dist/providers/textProtocol.js +45 -0
- package/dist/providers/textProtocol.js.map +1 -0
- package/dist/questions/broker.d.ts +16 -0
- package/dist/questions/broker.js +40 -0
- package/dist/questions/broker.js.map +1 -0
- package/dist/sandbox/command.d.ts +20 -0
- package/dist/sandbox/command.js +307 -0
- package/dist/sandbox/command.js.map +1 -0
- package/dist/sandbox/index.d.ts +21 -0
- package/dist/sandbox/index.js +32 -0
- package/dist/sandbox/index.js.map +1 -0
- package/dist/sdk/index.d.ts +14 -0
- package/dist/sdk/index.js +57 -0
- package/dist/sdk/index.js.map +1 -0
- package/dist/sdk/structured.d.ts +7 -0
- package/dist/sdk/structured.js +66 -0
- package/dist/sdk/structured.js.map +1 -0
- package/dist/storage/autoMemory.d.ts +22 -0
- package/dist/storage/autoMemory.js +93 -0
- package/dist/storage/autoMemory.js.map +1 -0
- package/dist/storage/checkpoints.d.ts +15 -0
- package/dist/storage/checkpoints.js +86 -0
- package/dist/storage/checkpoints.js.map +1 -0
- package/dist/storage/memory.d.ts +21 -0
- package/dist/storage/memory.js +137 -0
- package/dist/storage/memory.js.map +1 -0
- package/dist/storage/schema.d.ts +177 -0
- package/dist/storage/schema.js +120 -0
- package/dist/storage/schema.js.map +1 -0
- package/dist/storage/session.d.ts +56 -0
- package/dist/storage/session.js +305 -0
- package/dist/storage/session.js.map +1 -0
- package/dist/storage/settings.d.ts +17 -0
- package/dist/storage/settings.js +146 -0
- package/dist/storage/settings.js.map +1 -0
- package/dist/telemetry/otel.d.ts +21 -0
- package/dist/telemetry/otel.js +71 -0
- package/dist/telemetry/otel.js.map +1 -0
- package/dist/tools/agent.d.ts +13 -0
- package/dist/tools/agent.js +45 -0
- package/dist/tools/agent.js.map +1 -0
- package/dist/tools/artifact.d.ts +31 -0
- package/dist/tools/artifact.js +137 -0
- package/dist/tools/artifact.js.map +1 -0
- package/dist/tools/bash.d.ts +29 -0
- package/dist/tools/bash.js +191 -0
- package/dist/tools/bash.js.map +1 -0
- package/dist/tools/codemode.d.ts +13 -0
- package/dist/tools/codemode.js +77 -0
- package/dist/tools/codemode.js.map +1 -0
- package/dist/tools/edit.d.ts +11 -0
- package/dist/tools/edit.js +128 -0
- package/dist/tools/edit.js.map +1 -0
- package/dist/tools/glob.d.ts +10 -0
- package/dist/tools/glob.js +33 -0
- package/dist/tools/glob.js.map +1 -0
- package/dist/tools/grep.d.ts +20 -0
- package/dist/tools/grep.js +143 -0
- package/dist/tools/grep.js.map +1 -0
- package/dist/tools/index.d.ts +11 -0
- package/dist/tools/index.js +47 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/lsp.d.ts +30 -0
- package/dist/tools/lsp.js +206 -0
- package/dist/tools/lsp.js.map +1 -0
- package/dist/tools/notebook.d.ts +19 -0
- package/dist/tools/notebook.js +83 -0
- package/dist/tools/notebook.js.map +1 -0
- package/dist/tools/powershell.d.ts +10 -0
- package/dist/tools/powershell.js +83 -0
- package/dist/tools/powershell.js.map +1 -0
- package/dist/tools/question.d.ts +15 -0
- package/dist/tools/question.js +50 -0
- package/dist/tools/question.js.map +1 -0
- package/dist/tools/read.d.ts +10 -0
- package/dist/tools/read.js +188 -0
- package/dist/tools/read.js.map +1 -0
- package/dist/tools/shared.d.ts +8 -0
- package/dist/tools/shared.js +35 -0
- package/dist/tools/shared.js.map +1 -0
- package/dist/tools/skill.d.ts +9 -0
- package/dist/tools/skill.js +38 -0
- package/dist/tools/skill.js.map +1 -0
- package/dist/tools/tasks.d.ts +35 -0
- package/dist/tools/tasks.js +139 -0
- package/dist/tools/tasks.js.map +1 -0
- package/dist/tools/todo.d.ts +27 -0
- package/dist/tools/todo.js +40 -0
- package/dist/tools/todo.js.map +1 -0
- package/dist/tools/walk.d.ts +15 -0
- package/dist/tools/walk.js +144 -0
- package/dist/tools/walk.js.map +1 -0
- package/dist/tools/webfetch.d.ts +22 -0
- package/dist/tools/webfetch.js +201 -0
- package/dist/tools/webfetch.js.map +1 -0
- package/dist/tools/websearch.d.ts +33 -0
- package/dist/tools/websearch.js +129 -0
- package/dist/tools/websearch.js.map +1 -0
- package/dist/tools/write.d.ts +8 -0
- package/dist/tools/write.js +51 -0
- package/dist/tools/write.js.map +1 -0
- package/dist/tui/accessibility.d.ts +4 -0
- package/dist/tui/accessibility.js +24 -0
- package/dist/tui/accessibility.js.map +1 -0
- package/dist/tui/app.d.ts +21 -0
- package/dist/tui/app.js +280 -0
- package/dist/tui/app.js.map +1 -0
- package/dist/tui/format.d.ts +22 -0
- package/dist/tui/format.js +91 -0
- package/dist/tui/format.js.map +1 -0
- package/dist/tui/input.d.ts +9 -0
- package/dist/tui/input.js +71 -0
- package/dist/tui/input.js.map +1 -0
- package/dist/tui/permission.d.ts +2 -0
- package/dist/tui/permission.js +2 -0
- package/dist/tui/permission.js.map +1 -0
- package/dist/updates/index.d.ts +33 -0
- package/dist/updates/index.js +60 -0
- package/dist/updates/index.js.map +1 -0
- package/dist/util/clipboard.d.ts +1 -0
- package/dist/util/clipboard.js +30 -0
- package/dist/util/clipboard.js.map +1 -0
- package/dist/util/clock.d.ts +7 -0
- package/dist/util/clock.js +6 -0
- package/dist/util/clock.js.map +1 -0
- package/dist/util/errors.d.ts +19 -0
- package/dist/util/errors.js +64 -0
- package/dist/util/errors.js.map +1 -0
- package/dist/util/fsx.d.ts +4 -0
- package/dist/util/fsx.js +35 -0
- package/dist/util/fsx.js.map +1 -0
- package/dist/util/ids.d.ts +5 -0
- package/dist/util/ids.js +17 -0
- package/dist/util/ids.js.map +1 -0
- package/dist/util/logger.d.ts +13 -0
- package/dist/util/logger.js +23 -0
- package/dist/util/logger.js.map +1 -0
- package/dist/util/paths.d.ts +14 -0
- package/dist/util/paths.js +77 -0
- package/dist/util/paths.js.map +1 -0
- package/dist/util/redact.d.ts +6 -0
- package/dist/util/redact.js +44 -0
- package/dist/util/redact.js.map +1 -0
- package/dist/util/text.d.ts +4 -0
- package/dist/util/text.js +23 -0
- package/dist/util/text.js.map +1 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +2 -0
- package/dist/version.js.map +1 -0
- package/dist/web/public/app.js +917 -0
- package/dist/web/public/favicon.svg +5 -0
- package/dist/web/public/index.html +197 -0
- package/dist/web/public/markdown.js +176 -0
- package/dist/web/public/styles.css +1245 -0
- package/package.json +55 -4
|
@@ -0,0 +1,990 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { extname, join, resolve } from "node:path";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
import { stdout } from "node:process";
|
|
6
|
+
import { createAgentSetup } from "../app/factory.js";
|
|
7
|
+
import { trustWorkspace, untrustWorkspace } from "../app/trust.js";
|
|
8
|
+
import { PRESETS } from "../providers/presets.js";
|
|
9
|
+
import { keycodeHome, projectDir } from "../util/paths.js";
|
|
10
|
+
import { silentLogger } from "../util/logger.js";
|
|
11
|
+
import { systemClock } from "../util/clock.js";
|
|
12
|
+
import { KeycodeError } from "../util/errors.js";
|
|
13
|
+
import { runCommand } from "../tools/bash.js";
|
|
14
|
+
import { VERSION } from "../version.js";
|
|
15
|
+
import { createCodeModeTool } from "../tools/codemode.js";
|
|
16
|
+
import { createBrowserTool } from "../integrations/browser.js";
|
|
17
|
+
import { BackgroundSessions } from "../integrations/background.js";
|
|
18
|
+
import { IdeBridge } from "../integrations/ide.js";
|
|
19
|
+
import { startRemoteControl, defaultWebDir } from "../integrations/remote.js";
|
|
20
|
+
import { PermissionBroker } from "../permissions/broker.js";
|
|
21
|
+
import { QuestionBroker } from "../questions/broker.js";
|
|
22
|
+
import { createWorktree, listWorktrees, removeWorktree } from "../integrations/worktrees.js";
|
|
23
|
+
import { buildPrComment, getPullRequestDiff, reviewDiff, securityReview, } from "../integrations/pr.js";
|
|
24
|
+
import { postIssueComment } from "../integrations/github.js";
|
|
25
|
+
import { importConfigs } from "../integrations/import.js";
|
|
26
|
+
import { checkForUpdate } from "../updates/index.js";
|
|
27
|
+
import { startGateway } from "../gateway/index.js";
|
|
28
|
+
import { ArtifactStore, startPreview } from "../artifacts/store.js";
|
|
29
|
+
import { LspClient } from "../lsp/client.js";
|
|
30
|
+
import { AutoMemoryStore } from "../storage/autoMemory.js";
|
|
31
|
+
import { runWorkflow } from "../extensions/workflows.js";
|
|
32
|
+
import { LoopManager } from "../extensions/loops.js";
|
|
33
|
+
import { Notifier, webhookChannel } from "../notifications/index.js";
|
|
34
|
+
import { OtelExporter } from "../telemetry/otel.js";
|
|
35
|
+
import { addMcpServer, listMcpServers, oauthRefresh, reconnectMcp, removeMcpServer, setMcpEnabled, } from "../mcp/management.js";
|
|
36
|
+
import { installPlugin, listPlugins, pinPlugin, removePlugin, setPluginEnabled, } from "../extensions/plugins.js";
|
|
37
|
+
function opts(cmd) {
|
|
38
|
+
const parent = cmd.parent;
|
|
39
|
+
return { ...(parent ? parent.opts() : {}), ...cmd.opts() };
|
|
40
|
+
}
|
|
41
|
+
function emit(value, json) {
|
|
42
|
+
if (json === true) {
|
|
43
|
+
stdout.write(`${JSON.stringify(value, null, 2)}\n`);
|
|
44
|
+
}
|
|
45
|
+
else if (typeof value === "string") {
|
|
46
|
+
stdout.write(`${value}\n`);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
stdout.write(`${JSON.stringify(value, null, 2)}\n`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function arrayOption(value, label) {
|
|
53
|
+
if (typeof value !== "string" || value.trim().length === 0)
|
|
54
|
+
return undefined;
|
|
55
|
+
const raw = value.trim();
|
|
56
|
+
try {
|
|
57
|
+
const parsed = JSON.parse(raw);
|
|
58
|
+
if (Array.isArray(parsed))
|
|
59
|
+
return parsed.map((entry) => String(entry));
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
// Fall through to the comma-separated form.
|
|
63
|
+
}
|
|
64
|
+
const inner = raw.replace(/^\[/, "").replace(/\]$/, "");
|
|
65
|
+
if (inner.trim().length === 0)
|
|
66
|
+
return [];
|
|
67
|
+
const parts = inner
|
|
68
|
+
.split(",")
|
|
69
|
+
.map((entry) => entry.trim().replace(/^["']|["']$/g, ""))
|
|
70
|
+
.filter((entry) => entry.length > 0);
|
|
71
|
+
if (parts.length > 0)
|
|
72
|
+
return parts;
|
|
73
|
+
throw new KeycodeError("CONFIG_INVALID", `${label} must be a JSON array or a comma-separated list`);
|
|
74
|
+
}
|
|
75
|
+
function scopeOption(value) {
|
|
76
|
+
return value === "project" || value === "local" ? value : "user";
|
|
77
|
+
}
|
|
78
|
+
function toolContext(home, cwd) {
|
|
79
|
+
return {
|
|
80
|
+
sessionId: "cli",
|
|
81
|
+
cwd,
|
|
82
|
+
workspaceRoots: [cwd],
|
|
83
|
+
signal: new AbortController().signal,
|
|
84
|
+
permissions: { check: async () => ({ behavior: "allow", reason: "CLI" }) },
|
|
85
|
+
logger: silentLogger,
|
|
86
|
+
clock: systemClock,
|
|
87
|
+
state: { readFiles: new Set(), todos: [], metadata: new Map() },
|
|
88
|
+
home,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function keepAlive(close) {
|
|
92
|
+
const shutdown = () => {
|
|
93
|
+
void close().finally(() => process.exit(0));
|
|
94
|
+
};
|
|
95
|
+
process.on("SIGINT", shutdown);
|
|
96
|
+
process.on("SIGTERM", shutdown);
|
|
97
|
+
}
|
|
98
|
+
function openBrowser(url) {
|
|
99
|
+
const command = process.platform === "win32" ? "cmd" : process.platform === "darwin" ? "open" : "xdg-open";
|
|
100
|
+
const args = process.platform === "win32" ? ["/c", "start", "", url] : [url];
|
|
101
|
+
try {
|
|
102
|
+
const child = spawn(command, args, { detached: true, stdio: "ignore", windowsHide: true });
|
|
103
|
+
// Browser launch is best effort; the URL is always printed as well.
|
|
104
|
+
child.on("error", () => undefined);
|
|
105
|
+
child.unref();
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
// Ignore a missing launcher and keep the server running.
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
export function registerExtraCommands(program) {
|
|
112
|
+
program
|
|
113
|
+
.command("mcp <action> [name]")
|
|
114
|
+
.description("Manage MCP servers: list, add, remove, enable, disable, reconnect, oauth")
|
|
115
|
+
.option("--command <command>", "stdio command")
|
|
116
|
+
.option("--args <json>", "stdio arguments as JSON")
|
|
117
|
+
.option("--url <url>", "HTTP or SSE URL")
|
|
118
|
+
.option("--scope <scope>", "user, project, or local", "user")
|
|
119
|
+
.option("--json", "emit JSON")
|
|
120
|
+
.action(async (...args) => {
|
|
121
|
+
const cmd = args[args.length - 1];
|
|
122
|
+
const options = opts(cmd);
|
|
123
|
+
const [action, name] = cmd.args;
|
|
124
|
+
const home = keycodeHome();
|
|
125
|
+
const cwd = process.cwd();
|
|
126
|
+
if (action === "list") {
|
|
127
|
+
const servers = listMcpServers(home, cwd);
|
|
128
|
+
emit(options.json === true
|
|
129
|
+
? servers
|
|
130
|
+
: servers.length === 0
|
|
131
|
+
? "No MCP servers."
|
|
132
|
+
: servers
|
|
133
|
+
.map((server) => `${server.name} ${server.scope} ${server.enabled ? "enabled" : "disabled"} ${server.config.type ?? (server.config.url ? "http" : "stdio")}`)
|
|
134
|
+
.join("\n"), options.json);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (!name)
|
|
138
|
+
throw new KeycodeError("CONFIG_INVALID", `mcp ${action} requires a server name`);
|
|
139
|
+
if (action === "add") {
|
|
140
|
+
const type = options.url !== undefined ? "http" : "stdio";
|
|
141
|
+
const argsList = arrayOption(options.args, "--args");
|
|
142
|
+
const config = {
|
|
143
|
+
type,
|
|
144
|
+
...(typeof options.command === "string" ? { command: options.command } : {}),
|
|
145
|
+
...(argsList === undefined ? {} : { args: argsList }),
|
|
146
|
+
...(typeof options.url === "string" ? { url: options.url } : {}),
|
|
147
|
+
};
|
|
148
|
+
addMcpServer(home, cwd, name, config, scopeOption(options.scope));
|
|
149
|
+
emit(`Added MCP server ${name}.`, options.json);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (action === "remove") {
|
|
153
|
+
emit(removeMcpServer(home, cwd, name) ? `Removed ${name}.` : `Not found: ${name}`, options.json);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (action === "enable" || action === "disable") {
|
|
157
|
+
const ok = setMcpEnabled(home, cwd, name, action === "enable");
|
|
158
|
+
emit(ok ? `${action}d ${name}.` : `Not found: ${name}`, options.json);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (action === "reconnect") {
|
|
162
|
+
emit(await reconnectMcp(home, cwd, name), options.json);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (action === "oauth") {
|
|
166
|
+
emit(oauthRefresh(home, cwd, name) ? `Refreshed OAuth for ${name}.` : `Not found: ${name}`, options.json);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
throw new KeycodeError("CONFIG_INVALID", `Unknown mcp action: ${action}`);
|
|
170
|
+
});
|
|
171
|
+
program
|
|
172
|
+
.command("plugins <action> [target]")
|
|
173
|
+
.description("Manage plugins: list, install, enable, disable, remove, pin")
|
|
174
|
+
.option("--version <version>", "pin to a version")
|
|
175
|
+
.option("--json", "emit JSON")
|
|
176
|
+
.action((...args) => {
|
|
177
|
+
const cmd = args[args.length - 1];
|
|
178
|
+
const options = opts(cmd);
|
|
179
|
+
const [action, target] = cmd.args;
|
|
180
|
+
const home = keycodeHome();
|
|
181
|
+
if (action === "list") {
|
|
182
|
+
const plugins = listPlugins(home);
|
|
183
|
+
emit(options.json === true
|
|
184
|
+
? plugins
|
|
185
|
+
: plugins.length === 0
|
|
186
|
+
? "No plugins."
|
|
187
|
+
: plugins
|
|
188
|
+
.map((plugin) => `${plugin.name} ${plugin.pinnedVersion ?? plugin.version} ${plugin.enabled ? "enabled" : "disabled"} ${plugin.trusted ? "trusted" : "untrusted"}`)
|
|
189
|
+
.join("\n"), options.json);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (!target)
|
|
193
|
+
throw new KeycodeError("CONFIG_INVALID", `plugins ${action} requires a target`);
|
|
194
|
+
if (action === "install") {
|
|
195
|
+
emit(installPlugin(home, resolve(process.cwd(), target), { trusted: true }), options.json);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (action === "enable" || action === "disable") {
|
|
199
|
+
const record = setPluginEnabled(home, target, action === "enable");
|
|
200
|
+
emit(record ?? `Not found: ${target}`, options.json);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (action === "remove") {
|
|
204
|
+
emit(removePlugin(home, target) ? `Removed ${target}.` : `Not found: ${target}`, options.json);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (action === "pin") {
|
|
208
|
+
if (typeof options.version !== "string") {
|
|
209
|
+
throw new KeycodeError("CONFIG_INVALID", "plugins pin requires --version");
|
|
210
|
+
}
|
|
211
|
+
emit(pinPlugin(home, target, options.version) ?? `Not found: ${target}`, options.json);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
throw new KeycodeError("CONFIG_INVALID", `Unknown plugins action: ${action}`);
|
|
215
|
+
});
|
|
216
|
+
program
|
|
217
|
+
.command("import")
|
|
218
|
+
.description("Import Codex, Gemini, and Cursor configuration")
|
|
219
|
+
.option("--dry-run", "report without writing")
|
|
220
|
+
.option("--codex <path>", "Codex config path")
|
|
221
|
+
.option("--gemini <path>", "Gemini config path")
|
|
222
|
+
.option("--cursor <path>", "Cursor rules directory")
|
|
223
|
+
.option("--json", "emit JSON")
|
|
224
|
+
.action(async (...args) => {
|
|
225
|
+
const cmd = args[args.length - 1];
|
|
226
|
+
const options = opts(cmd);
|
|
227
|
+
const result = await importConfigs({
|
|
228
|
+
home: keycodeHome(),
|
|
229
|
+
cwd: process.cwd(),
|
|
230
|
+
dryRun: options.dryRun === true,
|
|
231
|
+
...(typeof options.codex === "string" ? { codexPath: options.codex } : {}),
|
|
232
|
+
...(typeof options.gemini === "string" ? { geminiPath: options.gemini } : {}),
|
|
233
|
+
...(typeof options.cursor === "string" ? { cursorPath: options.cursor } : {}),
|
|
234
|
+
});
|
|
235
|
+
emit(result, options.json);
|
|
236
|
+
});
|
|
237
|
+
program
|
|
238
|
+
.command("update")
|
|
239
|
+
.description("Check the release feed for an update")
|
|
240
|
+
.option("--feed <url>", "release feed URL")
|
|
241
|
+
.option("--channel <channel>", "stable or latest", "stable")
|
|
242
|
+
.option("--current <version>", "current version", VERSION)
|
|
243
|
+
.option("--json", "emit JSON")
|
|
244
|
+
.action(async (...args) => {
|
|
245
|
+
const cmd = args[args.length - 1];
|
|
246
|
+
const options = opts(cmd);
|
|
247
|
+
if (typeof options.feed !== "string") {
|
|
248
|
+
throw new KeycodeError("CONFIG_INVALID", "update requires --feed <url>");
|
|
249
|
+
}
|
|
250
|
+
const check = await checkForUpdate({
|
|
251
|
+
feedUrl: options.feed,
|
|
252
|
+
channel: options.channel === "latest" ? "latest" : "stable",
|
|
253
|
+
currentVersion: typeof options.current === "string" ? options.current : VERSION,
|
|
254
|
+
});
|
|
255
|
+
emit(check, options.json);
|
|
256
|
+
});
|
|
257
|
+
program
|
|
258
|
+
.command("worktree <action> [name]")
|
|
259
|
+
.description("Create, list, or remove git worktrees")
|
|
260
|
+
.option("--base-dir <dir>", "worktree base directory")
|
|
261
|
+
.option("--force", "force removal")
|
|
262
|
+
.option("--json", "emit JSON")
|
|
263
|
+
.action((...args) => {
|
|
264
|
+
const cmd = args[args.length - 1];
|
|
265
|
+
const options = opts(cmd);
|
|
266
|
+
const [action, name] = cmd.args;
|
|
267
|
+
const repo = process.cwd();
|
|
268
|
+
if (action === "list") {
|
|
269
|
+
emit(listWorktrees(repo), options.json);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if (!name)
|
|
273
|
+
throw new KeycodeError("CONFIG_INVALID", `worktree ${action} requires a name`);
|
|
274
|
+
if (action === "create") {
|
|
275
|
+
emit(createWorktree(repo, name, {
|
|
276
|
+
...(typeof options.baseDir === "string" ? { baseDir: options.baseDir } : {}),
|
|
277
|
+
}), options.json);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (action === "remove") {
|
|
281
|
+
removeWorktree(repo, resolve(repo, name), { force: options.force === true });
|
|
282
|
+
emit(`Removed ${name}.`, options.json);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
throw new KeycodeError("CONFIG_INVALID", `Unknown worktree action: ${action}`);
|
|
286
|
+
});
|
|
287
|
+
program
|
|
288
|
+
.command("bg <action> [id]")
|
|
289
|
+
.description("Manage background sessions: start, list, logs, stop, rm, respawn")
|
|
290
|
+
.option("--name <name>", "session name")
|
|
291
|
+
.option("--command <command>", "command to run")
|
|
292
|
+
.option("--args <json>", "arguments as JSON")
|
|
293
|
+
.option("--json", "emit JSON")
|
|
294
|
+
.action(async (...args) => {
|
|
295
|
+
const cmd = args[args.length - 1];
|
|
296
|
+
const options = opts(cmd);
|
|
297
|
+
const [action, id] = cmd.args;
|
|
298
|
+
const home = keycodeHome();
|
|
299
|
+
const manager = new BackgroundSessions(join(home, "background"));
|
|
300
|
+
if (action === "start") {
|
|
301
|
+
if (typeof options.command !== "string") {
|
|
302
|
+
throw new KeycodeError("CONFIG_INVALID", "bg start requires --command");
|
|
303
|
+
}
|
|
304
|
+
const argsList = arrayOption(options.args, "--args") ?? [];
|
|
305
|
+
emit(manager.start({
|
|
306
|
+
...(typeof options.name === "string" ? { name: options.name } : {}),
|
|
307
|
+
command: options.command,
|
|
308
|
+
args: argsList,
|
|
309
|
+
cwd: process.cwd(),
|
|
310
|
+
}), options.json);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if (action === "list") {
|
|
314
|
+
emit(manager.list(), options.json);
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
if (!id)
|
|
318
|
+
throw new KeycodeError("CONFIG_INVALID", `bg ${action} requires an id`);
|
|
319
|
+
if (action === "logs") {
|
|
320
|
+
emit(manager.logs(id), options.json);
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
if (action === "stop") {
|
|
324
|
+
emit(manager.stop(id), options.json);
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
if (action === "rm") {
|
|
328
|
+
emit(manager.remove(id) ? `Removed ${id}.` : `Not found: ${id}`, options.json);
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
if (action === "respawn") {
|
|
332
|
+
emit(manager.respawn(id), options.json);
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
throw new KeycodeError("CONFIG_INVALID", `Unknown bg action: ${action}`);
|
|
336
|
+
});
|
|
337
|
+
program
|
|
338
|
+
.command("ide")
|
|
339
|
+
.description("Start the IDE bridge and write its discovery file")
|
|
340
|
+
.option("--token <token>", "pairing token")
|
|
341
|
+
.action(async (...args) => {
|
|
342
|
+
const cmd = args[args.length - 1];
|
|
343
|
+
const options = opts(cmd);
|
|
344
|
+
const bridge = new IdeBridge({
|
|
345
|
+
home: keycodeHome(),
|
|
346
|
+
cwd: process.cwd(),
|
|
347
|
+
...(typeof options.token === "string" ? { token: options.token } : {}),
|
|
348
|
+
});
|
|
349
|
+
const started = await bridge.start();
|
|
350
|
+
stdout.write(`IDE bridge listening on 127.0.0.1:${started.port}\nDiscovery: ${started.discoveryFile}\nToken: ${started.token}\n`);
|
|
351
|
+
keepAlive(() => bridge.stop());
|
|
352
|
+
});
|
|
353
|
+
program
|
|
354
|
+
.command("remote")
|
|
355
|
+
.description("Start Remote Control on loopback with a pairing token")
|
|
356
|
+
.option("--token <token>", "pairing token")
|
|
357
|
+
.option("--open", "open the web UI in the default browser")
|
|
358
|
+
.option("--provider <id>", "provider id")
|
|
359
|
+
.option("--model <id>", "model id")
|
|
360
|
+
.option("--permission-mode <mode>", "permission mode", "manual")
|
|
361
|
+
.action(async (...args) => {
|
|
362
|
+
const cmd = args[args.length - 1];
|
|
363
|
+
const options = opts(cmd);
|
|
364
|
+
const broker = new PermissionBroker();
|
|
365
|
+
const questions = new QuestionBroker();
|
|
366
|
+
let currentProvider = typeof options.provider === "string" ? options.provider : undefined;
|
|
367
|
+
let currentModel = typeof options.model === "string" ? options.model : undefined;
|
|
368
|
+
const buildOptions = (overrides = {}) => ({
|
|
369
|
+
cwd: process.cwd(),
|
|
370
|
+
...(currentProvider === undefined ? {} : { provider: currentProvider }),
|
|
371
|
+
...(currentModel === undefined ? {} : { model: currentModel }),
|
|
372
|
+
permissionMode: options.permissionMode,
|
|
373
|
+
prompter: broker,
|
|
374
|
+
askQuestions: (items) => questions.ask(items),
|
|
375
|
+
...overrides,
|
|
376
|
+
});
|
|
377
|
+
const eventListeners = new Set();
|
|
378
|
+
const dispatch = (event) => {
|
|
379
|
+
for (const listener of eventListeners)
|
|
380
|
+
listener(event);
|
|
381
|
+
};
|
|
382
|
+
let setup = await createAgentSetup(buildOptions());
|
|
383
|
+
currentProvider = setup.providerId;
|
|
384
|
+
currentModel = setup.model;
|
|
385
|
+
let unsubscribeAgent = setup.agent.subscribe(dispatch);
|
|
386
|
+
let controller = new AbortController();
|
|
387
|
+
const describe = (current) => ({
|
|
388
|
+
provider: current.providerId,
|
|
389
|
+
model: current.model,
|
|
390
|
+
sessionId: current.sessionId,
|
|
391
|
+
permissionMode: current.permissionMode,
|
|
392
|
+
});
|
|
393
|
+
const sessionList = () => setup.store.list().slice(0, 50);
|
|
394
|
+
const activate = async (overrides) => {
|
|
395
|
+
broker.cancelAll();
|
|
396
|
+
questions.cancelAll();
|
|
397
|
+
unsubscribeAgent();
|
|
398
|
+
controller.abort();
|
|
399
|
+
setup.lsp?.stop();
|
|
400
|
+
setup.artifacts?.stop();
|
|
401
|
+
await setup.telemetry?.flush();
|
|
402
|
+
await setup.browser?.stop();
|
|
403
|
+
await Promise.all(setup.mcpClients.map((client) => client.close()));
|
|
404
|
+
setup = await createAgentSetup(buildOptions(overrides));
|
|
405
|
+
currentProvider = setup.providerId;
|
|
406
|
+
currentModel = setup.model;
|
|
407
|
+
unsubscribeAgent = setup.agent.subscribe(dispatch);
|
|
408
|
+
controller = new AbortController();
|
|
409
|
+
return describe(setup);
|
|
410
|
+
};
|
|
411
|
+
const providerCatalog = () => {
|
|
412
|
+
const custom = setup.settings.providers ?? {};
|
|
413
|
+
const ids = [
|
|
414
|
+
...new Set([
|
|
415
|
+
...Object.keys(PRESETS),
|
|
416
|
+
...Object.keys(custom),
|
|
417
|
+
...(currentProvider === undefined ? [] : [currentProvider]),
|
|
418
|
+
]),
|
|
419
|
+
];
|
|
420
|
+
return ids.map((id) => {
|
|
421
|
+
const preset = PRESETS[id];
|
|
422
|
+
const config = custom[id];
|
|
423
|
+
return {
|
|
424
|
+
id,
|
|
425
|
+
name: preset?.displayName ?? id,
|
|
426
|
+
models: preset?.models ?? config?.models ?? (id === currentProvider && currentModel ? [currentModel] : []),
|
|
427
|
+
defaultModel: preset?.defaultModel ??
|
|
428
|
+
config?.defaultModel ??
|
|
429
|
+
(id === currentProvider ? (currentModel ?? "") : ""),
|
|
430
|
+
free: preset?.free ?? true,
|
|
431
|
+
local: preset?.local ?? (id === "fake" || id === currentProvider),
|
|
432
|
+
};
|
|
433
|
+
});
|
|
434
|
+
};
|
|
435
|
+
const remotePermission = () => {
|
|
436
|
+
const pending = broker.current;
|
|
437
|
+
if (!pending)
|
|
438
|
+
return null;
|
|
439
|
+
return {
|
|
440
|
+
id: pending.id,
|
|
441
|
+
tool: pending.request.tool,
|
|
442
|
+
reason: pending.decision.reason,
|
|
443
|
+
...(pending.request.preview === undefined ? {} : { preview: pending.request.preview }),
|
|
444
|
+
...(pending.request.diff === undefined ? {} : { diff: pending.request.diff }),
|
|
445
|
+
};
|
|
446
|
+
};
|
|
447
|
+
const server = await startRemoteControl({
|
|
448
|
+
...(typeof options.token === "string" ? { token: options.token } : {}),
|
|
449
|
+
staticDir: defaultWebDir(),
|
|
450
|
+
handler: {
|
|
451
|
+
info: () => describe(setup),
|
|
452
|
+
trust: () => setup.trust,
|
|
453
|
+
setTrust: async (accept) => {
|
|
454
|
+
const sessionId = setup.sessionId;
|
|
455
|
+
if (accept) {
|
|
456
|
+
trustWorkspace(setup.home, setup.store.cwd, setup.trust.fingerprint);
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
untrustWorkspace(setup.home, setup.store.cwd);
|
|
460
|
+
}
|
|
461
|
+
return activate({
|
|
462
|
+
resume: sessionId,
|
|
463
|
+
provider: setup.providerId,
|
|
464
|
+
model: setup.model,
|
|
465
|
+
trust: accept,
|
|
466
|
+
});
|
|
467
|
+
},
|
|
468
|
+
subscribe: (listener) => {
|
|
469
|
+
eventListeners.add(listener);
|
|
470
|
+
return () => {
|
|
471
|
+
eventListeners.delete(listener);
|
|
472
|
+
};
|
|
473
|
+
},
|
|
474
|
+
onPrompt: async (prompt) => {
|
|
475
|
+
const result = await setup.agent.runTurn(prompt, controller.signal);
|
|
476
|
+
return result.text;
|
|
477
|
+
},
|
|
478
|
+
onStop: () => {
|
|
479
|
+
controller.abort();
|
|
480
|
+
broker.cancelAll();
|
|
481
|
+
questions.cancelAll();
|
|
482
|
+
controller = new AbortController();
|
|
483
|
+
},
|
|
484
|
+
sessions: () => sessionList().map((session) => ({
|
|
485
|
+
id: session.id,
|
|
486
|
+
name: session.name,
|
|
487
|
+
messageCount: session.messageCount,
|
|
488
|
+
createdAt: session.createdAt,
|
|
489
|
+
provider: session.provider,
|
|
490
|
+
model: session.model,
|
|
491
|
+
...(session.parentId === undefined || session.parentId === null
|
|
492
|
+
? {}
|
|
493
|
+
: { parentId: session.parentId }),
|
|
494
|
+
})),
|
|
495
|
+
history: () => setup.store.load(setup.sessionId).messages,
|
|
496
|
+
artifacts: () => setup.artifacts?.list() ?? [],
|
|
497
|
+
artifactPreview: async (id, version) => (await setup.artifacts?.preview(id, version)) ?? null,
|
|
498
|
+
resume: async (id) => {
|
|
499
|
+
if (id === null)
|
|
500
|
+
return activate({});
|
|
501
|
+
const meta = setup.store.resolve(id);
|
|
502
|
+
return activate({ resume: meta.id, provider: meta.provider, model: meta.model });
|
|
503
|
+
},
|
|
504
|
+
catalog: () => providerCatalog(),
|
|
505
|
+
setModel: async (provider, model) => {
|
|
506
|
+
const sessionId = setup.sessionId;
|
|
507
|
+
setup.store.setModel(sessionId, provider, model);
|
|
508
|
+
return activate({ resume: sessionId, provider, model });
|
|
509
|
+
},
|
|
510
|
+
renameSession: (id, name) => {
|
|
511
|
+
setup.store.rename(id, name.trim().length === 0 ? id.slice(0, 8) : name.trim());
|
|
512
|
+
return sessionList().map((session) => ({
|
|
513
|
+
id: session.id,
|
|
514
|
+
name: session.name,
|
|
515
|
+
messageCount: session.messageCount,
|
|
516
|
+
createdAt: session.createdAt,
|
|
517
|
+
provider: session.provider,
|
|
518
|
+
model: session.model,
|
|
519
|
+
...(session.parentId === undefined || session.parentId === null
|
|
520
|
+
? {}
|
|
521
|
+
: { parentId: session.parentId }),
|
|
522
|
+
}));
|
|
523
|
+
},
|
|
524
|
+
forkSession: async (id, name) => {
|
|
525
|
+
const header = setup.store.fork(id, name);
|
|
526
|
+
const view = setup.store.load(header.id);
|
|
527
|
+
return activate({
|
|
528
|
+
resume: header.id,
|
|
529
|
+
provider: view.header.provider,
|
|
530
|
+
model: view.header.model,
|
|
531
|
+
});
|
|
532
|
+
},
|
|
533
|
+
deleteSession: (id) => {
|
|
534
|
+
setup.store.delete(id);
|
|
535
|
+
},
|
|
536
|
+
},
|
|
537
|
+
permissions: {
|
|
538
|
+
current: remotePermission,
|
|
539
|
+
subscribe: (listener) => broker.subscribe(() => listener(remotePermission())),
|
|
540
|
+
resolve: (id, choice) => {
|
|
541
|
+
const behaviors = [
|
|
542
|
+
"allow_once",
|
|
543
|
+
"allow_always",
|
|
544
|
+
"allow_session",
|
|
545
|
+
"deny",
|
|
546
|
+
"cancel",
|
|
547
|
+
];
|
|
548
|
+
if (!behaviors.includes(choice))
|
|
549
|
+
return false;
|
|
550
|
+
return broker.resolveById(id, {
|
|
551
|
+
behavior: choice,
|
|
552
|
+
});
|
|
553
|
+
},
|
|
554
|
+
},
|
|
555
|
+
questions: {
|
|
556
|
+
current: () => {
|
|
557
|
+
const pending = questions.current;
|
|
558
|
+
if (!pending)
|
|
559
|
+
return null;
|
|
560
|
+
return { id: pending.id, questions: pending.questions };
|
|
561
|
+
},
|
|
562
|
+
subscribe: (listener) => questions.subscribe(() => listener(questions.current === null
|
|
563
|
+
? null
|
|
564
|
+
: { id: questions.current.id, questions: questions.current.questions })),
|
|
565
|
+
resolve: (id, result) => questions.resolveById(id, result),
|
|
566
|
+
},
|
|
567
|
+
});
|
|
568
|
+
stdout.write(`Remote Control: ${server.url}/#token=${server.token}\n`);
|
|
569
|
+
stdout.write(`Token: ${server.token}\nSession: ${setup.sessionId}\n`);
|
|
570
|
+
if (options.open === true)
|
|
571
|
+
openBrowser(`${server.url}/#token=${server.token}`);
|
|
572
|
+
keepAlive(async () => {
|
|
573
|
+
broker.cancelAll();
|
|
574
|
+
questions.cancelAll();
|
|
575
|
+
unsubscribeAgent();
|
|
576
|
+
setup.lsp?.stop();
|
|
577
|
+
setup.artifacts?.stop();
|
|
578
|
+
await setup.telemetry?.flush();
|
|
579
|
+
await setup.browser?.stop();
|
|
580
|
+
await Promise.all(setup.mcpClients.map((client) => client.close()));
|
|
581
|
+
await server.close();
|
|
582
|
+
});
|
|
583
|
+
});
|
|
584
|
+
program
|
|
585
|
+
.command("gateway")
|
|
586
|
+
.description("Start the local provider gateway")
|
|
587
|
+
.option("--api-key <key>", "provider API key to inject")
|
|
588
|
+
.option("--client-token <token>", "allowed client token (repeatable)", (value, previous) => [...previous, value], [])
|
|
589
|
+
.option("--rate <requests>", "requests per window", "60")
|
|
590
|
+
.option("--window <ms>", "rate-limit window in ms", "60000")
|
|
591
|
+
.option("--allow-private", "allow private upstream targets")
|
|
592
|
+
.action(async (...args) => {
|
|
593
|
+
const cmd = args[args.length - 1];
|
|
594
|
+
const options = opts(cmd);
|
|
595
|
+
if (typeof options.apiKey !== "string") {
|
|
596
|
+
throw new KeycodeError("CONFIG_INVALID", "gateway requires --api-key");
|
|
597
|
+
}
|
|
598
|
+
const clientTokens = Array.isArray(options.clientToken) ? options.clientToken : [];
|
|
599
|
+
if (clientTokens.length === 0) {
|
|
600
|
+
throw new KeycodeError("CONFIG_INVALID", "gateway requires at least one --client-token");
|
|
601
|
+
}
|
|
602
|
+
const server = await startGateway({
|
|
603
|
+
apiKey: options.apiKey,
|
|
604
|
+
clientTokens,
|
|
605
|
+
rateLimit: {
|
|
606
|
+
requests: Number(options.rate ?? 60),
|
|
607
|
+
windowMs: Number(options.window ?? 60_000),
|
|
608
|
+
},
|
|
609
|
+
allowPrivate: options.allowPrivate === true,
|
|
610
|
+
});
|
|
611
|
+
stdout.write(`Gateway listening on ${server.url}\n`);
|
|
612
|
+
keepAlive(() => server.close());
|
|
613
|
+
});
|
|
614
|
+
program
|
|
615
|
+
.command("pr <action> [number]")
|
|
616
|
+
.description("Review a pull request diff")
|
|
617
|
+
.option("--repo <owner/name>", "repository")
|
|
618
|
+
.option("--security", "only high-severity findings")
|
|
619
|
+
.option("--comment", "post the review as a comment")
|
|
620
|
+
.option("--token <token>", "GitHub token for posting")
|
|
621
|
+
.option("--json", "emit JSON")
|
|
622
|
+
.action(async (...args) => {
|
|
623
|
+
const cmd = args[args.length - 1];
|
|
624
|
+
const options = opts(cmd);
|
|
625
|
+
const [action, numberRaw] = cmd.args;
|
|
626
|
+
if (action !== "review")
|
|
627
|
+
throw new KeycodeError("CONFIG_INVALID", `Unknown pr action: ${action}`);
|
|
628
|
+
if (typeof options.repo !== "string") {
|
|
629
|
+
throw new KeycodeError("CONFIG_INVALID", "pr review requires --repo");
|
|
630
|
+
}
|
|
631
|
+
const number = Number(numberRaw);
|
|
632
|
+
const diff = await getPullRequestDiff({ repo: options.repo, number });
|
|
633
|
+
const findings = options.security === true ? securityReview(diff) : reviewDiff(diff);
|
|
634
|
+
const comment = buildPrComment(findings);
|
|
635
|
+
let posted = null;
|
|
636
|
+
if (options.comment === true) {
|
|
637
|
+
if (typeof options.token !== "string") {
|
|
638
|
+
throw new KeycodeError("CONFIG_INVALID", "pr review --comment requires --token");
|
|
639
|
+
}
|
|
640
|
+
posted = await postIssueComment({
|
|
641
|
+
token: options.token,
|
|
642
|
+
repo: options.repo,
|
|
643
|
+
issueNumber: number,
|
|
644
|
+
body: comment,
|
|
645
|
+
});
|
|
646
|
+
}
|
|
647
|
+
emit(options.json === true ? { findings, comment, posted } : comment, options.json);
|
|
648
|
+
});
|
|
649
|
+
program
|
|
650
|
+
.command("artifacts <action> [id]")
|
|
651
|
+
.description("Create, publish, preview, inspect, or annotate local artifacts")
|
|
652
|
+
.option("--name <name>", "artifact name")
|
|
653
|
+
.option("--file <path>", "HTML file")
|
|
654
|
+
.option("--version <n>", "version")
|
|
655
|
+
.option("--text <text>", "comment text")
|
|
656
|
+
.option("--key <key>", "data key")
|
|
657
|
+
.option("--value <value>", "data value")
|
|
658
|
+
.option("--token <token>", "preview token", "preview")
|
|
659
|
+
.option("--json", "emit JSON")
|
|
660
|
+
.action(async (...args) => {
|
|
661
|
+
const cmd = args[args.length - 1];
|
|
662
|
+
const options = opts(cmd);
|
|
663
|
+
const [action, id] = cmd.args;
|
|
664
|
+
const home = keycodeHome();
|
|
665
|
+
const store = new ArtifactStore(join(home, "artifacts"));
|
|
666
|
+
const version = options.version === undefined ? undefined : Number(options.version);
|
|
667
|
+
if (action === "create") {
|
|
668
|
+
if (typeof options.name !== "string" || typeof options.file !== "string") {
|
|
669
|
+
throw new KeycodeError("CONFIG_INVALID", "artifacts create requires --name and --file");
|
|
670
|
+
}
|
|
671
|
+
const artifact = store.create(options.name, readFileSync(resolve(process.cwd(), options.file), "utf8"));
|
|
672
|
+
emit({ id: artifact.id, version: 1 }, options.json);
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
if (action === "list") {
|
|
676
|
+
emit(store.list(), options.json);
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
if (!id)
|
|
680
|
+
throw new KeycodeError("CONFIG_INVALID", `artifacts ${action} requires an id`);
|
|
681
|
+
if (action === "publish") {
|
|
682
|
+
if (typeof options.file !== "string") {
|
|
683
|
+
throw new KeycodeError("CONFIG_INVALID", "artifacts publish requires --file");
|
|
684
|
+
}
|
|
685
|
+
const artifact = store.publish(id, readFileSync(resolve(process.cwd(), options.file), "utf8"));
|
|
686
|
+
emit({ id: artifact.id, version: artifact.versions.length }, options.json);
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
689
|
+
if (action === "inspect") {
|
|
690
|
+
emit(store.inspect(id, version), options.json);
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
if (action === "comment") {
|
|
694
|
+
if (typeof options.text !== "string") {
|
|
695
|
+
throw new KeycodeError("CONFIG_INVALID", "artifacts comment requires --text");
|
|
696
|
+
}
|
|
697
|
+
const targetVersion = version ?? store.get(id)?.version ?? 1;
|
|
698
|
+
emit(store.comment(id, targetVersion, options.text) ? "Comment added." : "Artifact not found.", options.json);
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
if (action === "data") {
|
|
702
|
+
if (typeof options.key !== "string") {
|
|
703
|
+
throw new KeycodeError("CONFIG_INVALID", "artifacts data requires --key");
|
|
704
|
+
}
|
|
705
|
+
const targetVersion = version ?? store.get(id)?.version ?? 1;
|
|
706
|
+
emit(store.setData(id, targetVersion, options.key, options.value ?? "") ? "Data set." : "Artifact not found.", options.json);
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
if (action === "preview") {
|
|
710
|
+
const server = await startPreview(store, {
|
|
711
|
+
token: typeof options.token === "string" ? options.token : "preview",
|
|
712
|
+
});
|
|
713
|
+
const versionPath = version === undefined ? "" : `/${version}`;
|
|
714
|
+
stdout.write(`Preview: ${server.url}/artifacts/${id}${versionPath}?token=${String(options.token ?? "preview")}\n`);
|
|
715
|
+
keepAlive(() => server.close());
|
|
716
|
+
return;
|
|
717
|
+
}
|
|
718
|
+
throw new KeycodeError("CONFIG_INVALID", `Unknown artifacts action: ${action}`);
|
|
719
|
+
});
|
|
720
|
+
program
|
|
721
|
+
.command("codemode [code]")
|
|
722
|
+
.description("Evaluate JavaScript in an opt-in persistent Code Mode session")
|
|
723
|
+
.option("--file <path>", "read code from a file")
|
|
724
|
+
.option("--reset", "reset the session state")
|
|
725
|
+
.option("--json", "emit JSON")
|
|
726
|
+
.action(async (...args) => {
|
|
727
|
+
const cmd = args[args.length - 1];
|
|
728
|
+
const options = opts(cmd);
|
|
729
|
+
const [codeArg] = cmd.args;
|
|
730
|
+
const code = typeof options.file === "string"
|
|
731
|
+
? readFileSync(resolve(process.cwd(), options.file), "utf8")
|
|
732
|
+
: codeArg;
|
|
733
|
+
if (!code)
|
|
734
|
+
throw new KeycodeError("CONFIG_INVALID", "codemode requires code or --file");
|
|
735
|
+
const tool = createCodeModeTool({ enabled: true });
|
|
736
|
+
const result = await tool.execute({ code, ...(options.reset === true ? { reset: true } : {}) }, toolContext(keycodeHome(), process.cwd()));
|
|
737
|
+
const text = result.content
|
|
738
|
+
.map((block) => (block.type === "text" ? block.text : `[${block.type}]`))
|
|
739
|
+
.join("\n");
|
|
740
|
+
emit(options.json === true ? { ok: result.ok, text } : text, options.json);
|
|
741
|
+
if (!result.ok)
|
|
742
|
+
process.exitCode = 1;
|
|
743
|
+
});
|
|
744
|
+
program
|
|
745
|
+
.command("lsp <file>")
|
|
746
|
+
.description("Start a language server and print diagnostics")
|
|
747
|
+
.option("--command <command>", "language server command")
|
|
748
|
+
.option("--args <json>", "language server arguments as JSON")
|
|
749
|
+
.option("--language <id>", "language id", "typescript")
|
|
750
|
+
.option("--hover <line:character>", "request hover at a position")
|
|
751
|
+
.option("--json", "emit JSON")
|
|
752
|
+
.action(async (...args) => {
|
|
753
|
+
const cmd = args[args.length - 1];
|
|
754
|
+
const options = opts(cmd);
|
|
755
|
+
const [file] = cmd.args;
|
|
756
|
+
if (typeof options.command !== "string") {
|
|
757
|
+
throw new KeycodeError("CONFIG_INVALID", "lsp requires --command");
|
|
758
|
+
}
|
|
759
|
+
if (!file)
|
|
760
|
+
throw new KeycodeError("CONFIG_INVALID", "lsp requires a file");
|
|
761
|
+
const path = resolve(process.cwd(), file);
|
|
762
|
+
const uri = pathToFileURL(path).toString();
|
|
763
|
+
const client = new LspClient({
|
|
764
|
+
command: options.command,
|
|
765
|
+
args: arrayOption(options.args, "--args") ?? [],
|
|
766
|
+
cwd: process.cwd(),
|
|
767
|
+
rootUri: pathToFileURL(process.cwd()).toString(),
|
|
768
|
+
timeoutMs: 10_000,
|
|
769
|
+
});
|
|
770
|
+
await client.start();
|
|
771
|
+
await client.initialize();
|
|
772
|
+
client.openDocument(uri, typeof options.language === "string" ? options.language : extname(path).replace(".", ""), readFileSync(path, "utf8"));
|
|
773
|
+
const diagnostics = await client.waitForDiagnostics(uri);
|
|
774
|
+
let hover = null;
|
|
775
|
+
if (typeof options.hover === "string") {
|
|
776
|
+
const [line, character] = options.hover.split(":").map(Number);
|
|
777
|
+
hover = await client.hover(uri, line ?? 0, character ?? 0);
|
|
778
|
+
}
|
|
779
|
+
emit(options.json === true ? { diagnostics, hover } : diagnostics, options.json);
|
|
780
|
+
client.stop();
|
|
781
|
+
});
|
|
782
|
+
program
|
|
783
|
+
.command("memory <action> [text]")
|
|
784
|
+
.description("Manage auto-memory: list, search, add, remove, pause, resume")
|
|
785
|
+
.option("--id <id>", "entry id")
|
|
786
|
+
.option("--query <query>", "search query")
|
|
787
|
+
.option("--json", "emit JSON")
|
|
788
|
+
.action((...args) => {
|
|
789
|
+
const cmd = args[args.length - 1];
|
|
790
|
+
const options = opts(cmd);
|
|
791
|
+
const [action, text] = cmd.args;
|
|
792
|
+
const home = keycodeHome();
|
|
793
|
+
const cwd = process.cwd();
|
|
794
|
+
const store = new AutoMemoryStore(join(projectDir(home, cwd), "memory.json"));
|
|
795
|
+
if (action === "list") {
|
|
796
|
+
emit(store.list(), options.json);
|
|
797
|
+
return;
|
|
798
|
+
}
|
|
799
|
+
if (action === "search") {
|
|
800
|
+
emit(store.recall(typeof options.query === "string" ? options.query : ""), options.json);
|
|
801
|
+
return;
|
|
802
|
+
}
|
|
803
|
+
if (action === "add") {
|
|
804
|
+
if (!text)
|
|
805
|
+
throw new KeycodeError("CONFIG_INVALID", "memory add requires text");
|
|
806
|
+
emit(store.add(text), options.json);
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
if (action === "remove") {
|
|
810
|
+
if (typeof options.id !== "string")
|
|
811
|
+
throw new KeycodeError("CONFIG_INVALID", "memory remove requires --id");
|
|
812
|
+
emit(store.remove(options.id) ? "Removed." : "Not found.", options.json);
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
if (action === "pause") {
|
|
816
|
+
store.pause();
|
|
817
|
+
emit("Auto-memory paused.", options.json);
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
if (action === "resume") {
|
|
821
|
+
store.resume();
|
|
822
|
+
emit("Auto-memory resumed.", options.json);
|
|
823
|
+
return;
|
|
824
|
+
}
|
|
825
|
+
throw new KeycodeError("CONFIG_INVALID", `Unknown memory action: ${action}`);
|
|
826
|
+
});
|
|
827
|
+
program
|
|
828
|
+
.command("workflow <script>")
|
|
829
|
+
.description("Run a workflow script with checkpoints and resume")
|
|
830
|
+
.option("--context <json>", "workflow context as JSON")
|
|
831
|
+
.option("--checkpoint-dir <dir>", "checkpoint directory")
|
|
832
|
+
.option("--budget <usd>", "budget in USD")
|
|
833
|
+
.option("--json", "emit JSON")
|
|
834
|
+
.action(async (...args) => {
|
|
835
|
+
const cmd = args[args.length - 1];
|
|
836
|
+
const options = opts(cmd);
|
|
837
|
+
const [script] = cmd.args;
|
|
838
|
+
if (!script)
|
|
839
|
+
throw new KeycodeError("CONFIG_INVALID", "workflow requires a script path");
|
|
840
|
+
const result = await runWorkflow({
|
|
841
|
+
scriptPath: resolve(process.cwd(), script),
|
|
842
|
+
checkpointDir: typeof options.checkpointDir === "string"
|
|
843
|
+
? options.checkpointDir
|
|
844
|
+
: join(keycodeHome(), "workflows"),
|
|
845
|
+
context: typeof options.context === "string"
|
|
846
|
+
? JSON.parse(options.context)
|
|
847
|
+
: {},
|
|
848
|
+
...(typeof options.budget === "string" ? { budgetUsd: Number(options.budget) } : {}),
|
|
849
|
+
});
|
|
850
|
+
emit(result, options.json);
|
|
851
|
+
if (result.status === "failed")
|
|
852
|
+
process.exitCode = 1;
|
|
853
|
+
});
|
|
854
|
+
program
|
|
855
|
+
.command("loops <action> [id]")
|
|
856
|
+
.description("Manage cron loops: list, add, remove, run")
|
|
857
|
+
.option("--cron <expression>", "cron expression")
|
|
858
|
+
.option("--command <command>", "command to run")
|
|
859
|
+
.option("--timezone <timezone>", "timezone label")
|
|
860
|
+
.option("--json", "emit JSON")
|
|
861
|
+
.action(async (...args) => {
|
|
862
|
+
const cmd = args[args.length - 1];
|
|
863
|
+
const options = opts(cmd);
|
|
864
|
+
const [action, id] = cmd.args;
|
|
865
|
+
const manager = new LoopManager(join(keycodeHome(), "loops.json"));
|
|
866
|
+
if (action === "list") {
|
|
867
|
+
emit(manager.list(), options.json);
|
|
868
|
+
return;
|
|
869
|
+
}
|
|
870
|
+
if (action === "add") {
|
|
871
|
+
if (typeof options.cron !== "string" || typeof options.command !== "string") {
|
|
872
|
+
throw new KeycodeError("CONFIG_INVALID", "loops add requires --cron and --command");
|
|
873
|
+
}
|
|
874
|
+
emit(manager.add({
|
|
875
|
+
cron: options.cron,
|
|
876
|
+
command: options.command,
|
|
877
|
+
...(typeof options.timezone === "string" ? { timezone: options.timezone } : {}),
|
|
878
|
+
}), options.json);
|
|
879
|
+
return;
|
|
880
|
+
}
|
|
881
|
+
if (!id)
|
|
882
|
+
throw new KeycodeError("CONFIG_INVALID", `loops ${action} requires an id`);
|
|
883
|
+
if (action === "remove") {
|
|
884
|
+
emit(manager.remove(id) ? "Removed." : "Not found.", options.json);
|
|
885
|
+
return;
|
|
886
|
+
}
|
|
887
|
+
if (action === "run") {
|
|
888
|
+
const ran = await manager.runDue(new Date(), async (job) => {
|
|
889
|
+
await runCommand({ command: job.command, cwd: process.cwd(), timeoutMs: 60_000 });
|
|
890
|
+
});
|
|
891
|
+
emit(`Ran ${ran} loop(s).`, options.json);
|
|
892
|
+
return;
|
|
893
|
+
}
|
|
894
|
+
throw new KeycodeError("CONFIG_INVALID", `Unknown loops action: ${action}`);
|
|
895
|
+
});
|
|
896
|
+
program
|
|
897
|
+
.command("notify")
|
|
898
|
+
.description("Send a test notification")
|
|
899
|
+
.option("--event <event>", "idle, permission_required, task_complete, or input_needed", "idle")
|
|
900
|
+
.option("--text <text>", "notification text", "KeyCode notification")
|
|
901
|
+
.option("--webhook <url>", "webhook URL")
|
|
902
|
+
.option("--json", "emit JSON")
|
|
903
|
+
.action(async (...args) => {
|
|
904
|
+
const cmd = args[args.length - 1];
|
|
905
|
+
const options = opts(cmd);
|
|
906
|
+
const channels = [
|
|
907
|
+
{
|
|
908
|
+
name: "stdout",
|
|
909
|
+
send: (event, text) => {
|
|
910
|
+
stdout.write(`${event}: ${text}\n`);
|
|
911
|
+
},
|
|
912
|
+
},
|
|
913
|
+
];
|
|
914
|
+
if (typeof options.webhook === "string") {
|
|
915
|
+
channels.push(webhookChannel(options.webhook));
|
|
916
|
+
}
|
|
917
|
+
const notifier = new Notifier({ channels });
|
|
918
|
+
const sent = await notifier.notify(options.event, typeof options.text === "string" ? options.text : "KeyCode notification");
|
|
919
|
+
emit({ sent }, options.json);
|
|
920
|
+
});
|
|
921
|
+
program
|
|
922
|
+
.command("otel")
|
|
923
|
+
.description("Send one OTLP metric to a local collector")
|
|
924
|
+
.option("--endpoint <url>", "OTLP HTTP endpoint")
|
|
925
|
+
.option("--name <name>", "metric name", "keycode.cli")
|
|
926
|
+
.option("--value <value>", "metric value", "1")
|
|
927
|
+
.option("--attributes <json>", "attributes as JSON")
|
|
928
|
+
.option("--json", "emit JSON")
|
|
929
|
+
.action(async (...args) => {
|
|
930
|
+
const cmd = args[args.length - 1];
|
|
931
|
+
const options = opts(cmd);
|
|
932
|
+
if (typeof options.endpoint !== "string") {
|
|
933
|
+
throw new KeycodeError("CONFIG_INVALID", "otel requires --endpoint");
|
|
934
|
+
}
|
|
935
|
+
const attributes = typeof options.attributes === "string"
|
|
936
|
+
? JSON.parse(options.attributes)
|
|
937
|
+
: {};
|
|
938
|
+
const exporter = new OtelExporter({ endpoint: options.endpoint, serviceName: "keycode-cli" });
|
|
939
|
+
exporter.record({
|
|
940
|
+
name: typeof options.name === "string" ? options.name : "keycode.cli",
|
|
941
|
+
value: Number(options.value ?? 1),
|
|
942
|
+
attributes,
|
|
943
|
+
});
|
|
944
|
+
const ok = await exporter.flush();
|
|
945
|
+
emit({ ok }, options.json);
|
|
946
|
+
if (!ok)
|
|
947
|
+
process.exitCode = 1;
|
|
948
|
+
});
|
|
949
|
+
program
|
|
950
|
+
.command("browser <action> [url]")
|
|
951
|
+
.description("Browser automation through a driver module")
|
|
952
|
+
.option("--driver <module>", "driver module exporting createBrowserDriver")
|
|
953
|
+
.option("--allow <domains>", "comma-separated allowed domains", "")
|
|
954
|
+
.option("--selector <selector>", "CSS selector")
|
|
955
|
+
.option("--text <text>", "text to type")
|
|
956
|
+
.option("--json", "emit JSON")
|
|
957
|
+
.action(async (...args) => {
|
|
958
|
+
const cmd = args[args.length - 1];
|
|
959
|
+
const options = opts(cmd);
|
|
960
|
+
const [action, url] = cmd.args;
|
|
961
|
+
if (typeof options.driver !== "string") {
|
|
962
|
+
throw new KeycodeError("CONFIG_INVALID", "Browser automation requires --driver <module> exporting createBrowserDriver.");
|
|
963
|
+
}
|
|
964
|
+
const module = (await import(pathToFileURL(resolve(process.cwd(), options.driver)).href));
|
|
965
|
+
const driver = module.createBrowserDriver !== undefined
|
|
966
|
+
? await module.createBrowserDriver({})
|
|
967
|
+
: module.default;
|
|
968
|
+
if (!driver) {
|
|
969
|
+
throw new KeycodeError("CONFIG_INVALID", "Driver module does not export createBrowserDriver or a default driver");
|
|
970
|
+
}
|
|
971
|
+
const allowedDomains = String(options.allow ?? "")
|
|
972
|
+
.split(",")
|
|
973
|
+
.map((entry) => entry.trim())
|
|
974
|
+
.filter((entry) => entry.length > 0);
|
|
975
|
+
const tool = createBrowserTool({ driver, allowedDomains });
|
|
976
|
+
const result = await tool.execute({
|
|
977
|
+
action: action,
|
|
978
|
+
...(url === undefined ? {} : { url }),
|
|
979
|
+
...(typeof options.selector === "string" ? { selector: options.selector } : {}),
|
|
980
|
+
...(typeof options.text === "string" ? { text: options.text } : {}),
|
|
981
|
+
}, toolContext(keycodeHome(), process.cwd()));
|
|
982
|
+
const text = result.content
|
|
983
|
+
.map((block) => (block.type === "text" ? block.text : `[${block.type}]`))
|
|
984
|
+
.join("\n");
|
|
985
|
+
emit(options.json === true ? { ok: result.ok, text } : text, options.json);
|
|
986
|
+
if (!result.ok)
|
|
987
|
+
process.exitCode = 1;
|
|
988
|
+
});
|
|
989
|
+
}
|
|
990
|
+
//# sourceMappingURL=extra.js.map
|