@phren/cli 0.2.15 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/agent-instructions.d.ts +12 -0
- package/dist/agent-instructions.js +52 -0
- package/dist/bridge/agent-hook-stores.d.ts +32 -0
- package/dist/bridge/agent-hook-stores.js +67 -0
- package/dist/bridge/agent-hooks.d.ts +195 -2
- package/dist/bridge/agent-hooks.js +1049 -38
- package/dist/bridge/canary.d.ts +27 -0
- package/dist/bridge/canary.js +189 -0
- package/dist/bridge/changes.d.ts +57 -0
- package/dist/bridge/changes.js +403 -0
- package/dist/bridge/client.d.ts +3 -0
- package/dist/bridge/client.js +36 -0
- package/dist/bridge/code-note.d.ts +23 -0
- package/dist/bridge/code-note.js +51 -0
- package/dist/bridge/code-routes.d.ts +151 -0
- package/dist/bridge/code-routes.js +442 -0
- package/dist/bridge/codex-threads.d.ts +27 -0
- package/dist/bridge/codex-threads.js +442 -0
- package/dist/bridge/command.d.ts +7 -0
- package/dist/bridge/command.js +50 -5
- package/dist/bridge/computer-names.d.ts +4 -0
- package/dist/bridge/computer-names.js +16 -0
- package/dist/bridge/computers.d.ts +9 -0
- package/dist/bridge/computers.js +111 -0
- package/dist/bridge/context.d.ts +14 -0
- package/dist/bridge/context.js +149 -0
- package/dist/bridge/dispatch-command.d.ts +3 -0
- package/dist/bridge/dispatch-command.js +93 -0
- package/dist/bridge/dispatch-hosts.d.ts +23 -0
- package/dist/bridge/dispatch-hosts.js +25 -0
- package/dist/bridge/dispatch-returns.d.ts +125 -0
- package/dist/bridge/dispatch-returns.js +283 -0
- package/dist/bridge/dispatch-tree.d.ts +54 -0
- package/dist/bridge/dispatch-tree.js +198 -0
- package/dist/bridge/dispatch.d.ts +210 -0
- package/dist/bridge/dispatch.js +286 -0
- package/dist/bridge/enroll.d.ts +31 -0
- package/dist/bridge/enroll.js +236 -0
- package/dist/bridge/fanout-messages.d.ts +74 -0
- package/dist/bridge/fanout-messages.js +230 -0
- package/dist/bridge/fanouts.d.ts +160 -0
- package/dist/bridge/fanouts.js +702 -0
- package/dist/bridge/file-range.d.ts +16 -0
- package/dist/bridge/file-range.js +90 -0
- package/dist/bridge/files.d.ts +20 -0
- package/dist/bridge/files.js +54 -0
- package/dist/bridge/git-publish.d.ts +23 -0
- package/dist/bridge/git-publish.js +145 -0
- package/dist/bridge/git-worktrees.d.ts +21 -0
- package/dist/bridge/git-worktrees.js +150 -0
- package/dist/bridge/git.d.ts +54 -0
- package/dist/bridge/git.js +552 -0
- package/dist/bridge/grants.d.ts +31 -0
- package/dist/bridge/grants.js +181 -0
- package/dist/bridge/hand-off.d.ts +79 -0
- package/dist/bridge/hand-off.js +151 -0
- package/dist/bridge/health.d.ts +128 -0
- package/dist/bridge/health.js +260 -0
- package/dist/bridge/herdr.d.ts +79 -6
- package/dist/bridge/herdr.js +292 -27
- package/dist/bridge/hook-main.js +2 -0
- package/dist/bridge/install.d.ts +28 -1
- package/dist/bridge/install.js +225 -38
- package/dist/bridge/launch-worktree.d.ts +21 -0
- package/dist/bridge/launch-worktree.js +70 -0
- package/dist/bridge/limits.d.ts +20 -0
- package/dist/bridge/limits.js +65 -0
- package/dist/bridge/locate.d.ts +16 -0
- package/dist/bridge/locate.js +75 -0
- package/dist/bridge/metrics.d.ts +29 -0
- package/dist/bridge/metrics.js +71 -0
- package/dist/bridge/model-switch.d.ts +35 -0
- package/dist/bridge/model-switch.js +250 -0
- package/dist/bridge/models.d.ts +46 -0
- package/dist/bridge/models.js +257 -0
- package/dist/bridge/opencode-approvals.d.ts +6 -0
- package/dist/bridge/opencode-approvals.js +60 -0
- package/dist/bridge/pane-text.d.ts +15 -0
- package/dist/bridge/pane-text.js +23 -0
- package/dist/bridge/peers.d.ts +27 -0
- package/dist/bridge/peers.js +130 -0
- package/dist/bridge/projects.d.ts +12 -1
- package/dist/bridge/projects.js +268 -20
- package/dist/bridge/protocol.d.ts +35 -2
- package/dist/bridge/protocol.js +61 -7
- package/dist/bridge/push.d.ts +83 -0
- package/dist/bridge/push.js +199 -0
- package/dist/bridge/questions.d.ts +38 -0
- package/dist/bridge/questions.js +205 -0
- package/dist/bridge/schedule-format.d.ts +103 -0
- package/dist/bridge/schedule-format.js +266 -0
- package/dist/bridge/schedule-launch.d.ts +11 -0
- package/dist/bridge/schedule-launch.js +169 -0
- package/dist/bridge/schedule-watch.d.ts +54 -0
- package/dist/bridge/schedule-watch.js +255 -0
- package/dist/bridge/schedules.d.ts +48 -0
- package/dist/bridge/schedules.js +205 -0
- package/dist/bridge/server-launch.d.ts +14 -0
- package/dist/bridge/server-launch.js +218 -0
- package/dist/bridge/server-overview.d.ts +45 -0
- package/dist/bridge/server-overview.js +102 -0
- package/dist/bridge/server-pane-routes.d.ts +36 -0
- package/dist/bridge/server-pane-routes.js +479 -0
- package/dist/bridge/server-routes.d.ts +108 -0
- package/dist/bridge/server-routes.js +556 -0
- package/dist/bridge/server-stream.d.ts +46 -0
- package/dist/bridge/server-stream.js +253 -0
- package/dist/bridge/server.d.ts +3 -15
- package/dist/bridge/server.js +162 -264
- package/dist/bridge/session-servers.d.ts +24 -0
- package/dist/bridge/session-servers.js +136 -0
- package/dist/bridge/side-questions.d.ts +74 -0
- package/dist/bridge/side-questions.js +252 -0
- package/dist/bridge/simulators.d.ts +28 -0
- package/dist/bridge/simulators.js +198 -0
- package/dist/bridge/speech-transcribe.d.ts +24 -0
- package/dist/bridge/speech-transcribe.js +124 -0
- package/dist/bridge/speech.d.ts +35 -0
- package/dist/bridge/speech.js +98 -0
- package/dist/bridge/steps.d.ts +13 -0
- package/dist/bridge/steps.js +222 -0
- package/dist/bridge/tab-activity.d.ts +18 -0
- package/dist/bridge/tab-activity.js +99 -0
- package/dist/bridge/terminal-choice.d.ts +64 -0
- package/dist/bridge/terminal-choice.js +281 -0
- package/dist/bridge/transcript-claude.d.ts +22 -0
- package/dist/bridge/transcript-claude.js +353 -0
- package/dist/bridge/transcript-codex.d.ts +30 -0
- package/dist/bridge/transcript-codex.js +282 -0
- package/dist/bridge/transcript-copilot.d.ts +4 -0
- package/dist/bridge/transcript-copilot.js +14 -0
- package/dist/bridge/transcript-opencode.d.ts +5 -0
- package/dist/bridge/transcript-opencode.js +26 -0
- package/dist/bridge/transcript-preview.d.ts +96 -0
- package/dist/bridge/transcript-preview.js +491 -0
- package/dist/bridge/transcripts.d.ts +81 -4
- package/dist/bridge/transcripts.js +332 -67
- package/dist/bridge/transport.d.ts +3 -0
- package/dist/bridge/transport.js +96 -15
- package/dist/bridge/uploads.d.ts +15 -0
- package/dist/bridge/uploads.js +44 -2
- package/dist/bridge/usage.d.ts +124 -0
- package/dist/bridge/usage.js +859 -0
- package/dist/bridge-hook.mjs +46545 -21051
- package/dist/cli/actions.js +14 -1
- package/dist/cli/config-policy.js +25 -0
- package/dist/cli/config-proactivity.d.ts +3 -1
- package/dist/cli/config-proactivity.js +35 -3
- package/dist/cli/config.js +4 -1
- package/dist/cli/extract.d.ts +0 -8
- package/dist/cli/extract.js +4 -7
- package/dist/cli/govern.js +5 -2
- package/dist/cli/hooks-globs.js +1 -1
- package/dist/cli/hooks.d.ts +1 -1
- package/dist/cli/hooks.js +9 -7
- package/dist/cli/namespaces-projects.js +12 -6
- package/dist/cli/namespaces-store.js +37 -6
- package/dist/cli/namespaces-tasks.js +1 -1
- package/dist/cli/schedules.d.ts +1 -0
- package/dist/cli/schedules.js +164 -0
- package/dist/cli/session-background.js +1 -1
- package/dist/cli/session-git.d.ts +8 -5
- package/dist/cli/session-git.js +45 -120
- package/dist/cli/session-start.js +3 -1
- package/dist/cli/session-stop.js +19 -8
- package/dist/cli/team.js +13 -10
- package/dist/cli-handlers.d.ts +1 -1
- package/dist/cli-handlers.js +8 -4
- package/dist/cli-help.d.ts +5 -4
- package/dist/cli-help.js +15 -15
- package/dist/cli-registry.d.ts +4 -1
- package/dist/cli-registry.js +119 -6
- package/dist/content/archive.js +2 -2
- package/dist/content/citation.d.ts +11 -0
- package/dist/content/citation.js +18 -0
- package/dist/content/learning.js +2 -0
- package/dist/content/summarize.d.ts +14 -15
- package/dist/content/summarize.js +60 -5
- package/dist/content/validate.d.ts +17 -4
- package/dist/content/validate.js +200 -122
- package/dist/core/project-registry.d.ts +26 -0
- package/dist/core/project-registry.js +573 -0
- package/dist/core/project.js +2 -3
- package/dist/data/access.d.ts +16 -1
- package/dist/data/access.js +31 -3
- package/dist/data/tasks.d.ts +18 -3
- package/dist/data/tasks.js +8 -7
- package/dist/entrypoint.js +44 -10
- package/dist/fanout/adapters/claude.d.ts +2 -0
- package/dist/fanout/adapters/claude.js +7 -0
- package/dist/fanout/adapters/codex.d.ts +2 -0
- package/dist/fanout/adapters/codex.js +9 -0
- package/dist/fanout/adapters/opencode.d.ts +4 -0
- package/dist/fanout/adapters/opencode.js +9 -0
- package/dist/fanout/adapters/types.d.ts +21 -0
- package/dist/fanout/adapters/types.js +3 -0
- package/dist/fanout/command.d.ts +2 -0
- package/dist/fanout/command.js +78 -0
- package/dist/fanout/launcher.d.ts +27 -0
- package/dist/fanout/launcher.js +156 -0
- package/dist/fanout/opencode-serve.d.ts +24 -0
- package/dist/fanout/opencode-serve.js +191 -0
- package/dist/fanout/picker.d.ts +91 -0
- package/dist/fanout/picker.js +51 -0
- package/dist/fanout/usage.d.ts +5 -0
- package/dist/fanout/usage.js +51 -0
- package/dist/fanout/watchdog.d.ts +10 -0
- package/dist/fanout/watchdog.js +27 -0
- package/dist/filenames.d.ts +9 -0
- package/dist/filenames.js +11 -0
- package/dist/finding/context.js +4 -22
- package/dist/generated/memory-ui-graph.browser.js +525 -419
- package/dist/governance/policy-constants.d.ts +11 -0
- package/dist/governance/policy-constants.js +9 -0
- package/dist/governance/policy.d.ts +5 -8
- package/dist/governance/policy.js +11 -8
- package/dist/graph-core/camera.d.ts +24 -0
- package/dist/graph-core/camera.js +18 -0
- package/dist/graph-core/index.d.ts +2 -0
- package/dist/graph-core/index.js +2 -0
- package/dist/graph-core/labels.d.ts +77 -0
- package/dist/graph-core/labels.js +124 -0
- package/dist/graph-core/model.d.ts +13 -0
- package/dist/graph-core/model.js +33 -0
- package/dist/graph-core/types.d.ts +2 -0
- package/dist/home-paths.d.ts +7 -0
- package/dist/home-paths.js +21 -0
- package/dist/hooks.d.ts +3 -15
- package/dist/hooks.js +14 -1
- package/dist/index.js +24 -2
- package/dist/init/guard-globals.d.ts +1 -1
- package/dist/init/guard-globals.js +4 -4
- package/dist/init/init-configure.js +3 -3
- package/dist/init/init-uninstall.js +15 -5
- package/dist/init/init-walkthrough.js +1 -1
- package/dist/init/init.js +26 -6
- package/dist/init/management-preset.d.ts +1 -1
- package/dist/init/management-preset.js +2 -2
- package/dist/init/preferences.js +1 -1
- package/dist/init/self-wiring.js +1 -1
- package/dist/init/setup.d.ts +3 -26
- package/dist/init/setup.js +68 -593
- package/dist/init/shared.js +2 -0
- package/dist/init/store-gitignore.d.ts +1 -6
- package/dist/init/store-gitignore.js +2 -8
- package/dist/init/teardown.d.ts +1 -1
- package/dist/init/teardown.js +2 -2
- package/dist/link/context-imports.d.ts +15 -0
- package/dist/link/context-imports.js +101 -0
- package/dist/link/context.js +1 -1
- package/dist/link/doctor.d.ts +22 -1
- package/dist/link/doctor.js +128 -58
- package/dist/link/link.js +29 -61
- package/dist/link/refresh.js +2 -2
- package/dist/link/skills.js +26 -5
- package/dist/machine-identity.d.ts +3 -0
- package/dist/machine-identity.js +6 -0
- package/dist/mcp/profile.d.ts +2 -19
- package/dist/mcp/profile.js +64 -22
- package/dist/memory-ui-graph.runtime.js +525 -419
- package/dist/modules/code-package.d.ts +16 -0
- package/dist/modules/code-package.js +160 -0
- package/dist/modules/command.d.ts +2 -0
- package/dist/modules/command.js +67 -0
- package/dist/modules/config.d.ts +4 -0
- package/dist/modules/config.js +70 -0
- package/dist/modules/manifest.d.ts +41 -0
- package/dist/modules/manifest.js +1 -0
- package/dist/modules/provision.d.ts +3 -0
- package/dist/modules/provision.js +84 -0
- package/dist/modules/registry.d.ts +16 -0
- package/dist/modules/registry.js +250 -0
- package/dist/modules/runtime.d.ts +15 -0
- package/dist/modules/runtime.js +59 -0
- package/dist/package-metadata.js +10 -2
- package/dist/phren-dotenv.js +4 -2
- package/dist/phren-paths.d.ts +3 -2
- package/dist/phren-paths.js +4 -5
- package/dist/profile-store.js +43 -8
- package/dist/project-config.d.ts +19 -1
- package/dist/project-config.js +29 -3
- package/dist/project-topics.d.ts +1 -0
- package/dist/project-topics.js +13 -10
- package/dist/provider-adapters.js +1 -4
- package/dist/query-correlation.js +6 -6
- package/dist/session/artifacts.js +5 -5
- package/dist/session/checkpoints.js +5 -0
- package/dist/session/utils.d.ts +2 -2
- package/dist/session/utils.js +6 -6
- package/dist/shared/content.d.ts +1 -1
- package/dist/shared/content.js +1 -1
- package/dist/shared/data-utils.js +1 -1
- package/dist/shared/fragment-graph.d.ts +1 -1
- package/dist/shared/fragment-graph.js +5 -5
- package/dist/shared/index.js +48 -7
- package/dist/shared/retrieval.d.ts +18 -3
- package/dist/shared/retrieval.js +101 -12
- package/dist/shared/search-fallback.js +4 -6
- package/dist/shared/sqljs.d.ts +2 -0
- package/dist/shared/sqljs.js +4 -2
- package/dist/shared.d.ts +0 -1
- package/dist/shared.js +0 -1
- package/dist/shell/input.d.ts +1 -5
- package/dist/shell/input.js +16 -10
- package/dist/shell/palette.d.ts +2 -2
- package/dist/shell/palette.js +3 -7
- package/dist/shell/render.d.ts +3 -1
- package/dist/shell/render.js +4 -4
- package/dist/shell/shell.js +4 -1
- package/dist/shell/types.d.ts +1 -0
- package/dist/shell/types.js +4 -0
- package/dist/shell/view.d.ts +0 -4
- package/dist/shell/view.js +19 -27
- package/dist/skill/registry.js +25 -17
- package/dist/skill/state.d.ts +3 -0
- package/dist/skill/state.js +13 -7
- package/dist/status.js +38 -17
- package/dist/store-registry.d.ts +44 -18
- package/dist/store-registry.js +169 -60
- package/dist/store-weight.d.ts +1 -6
- package/dist/store-weight.js +4 -2
- package/dist/sync/auth-doctor.d.ts +8 -0
- package/dist/sync/auth-doctor.js +27 -0
- package/dist/sync/auth.d.ts +27 -0
- package/dist/sync/auth.js +114 -0
- package/dist/sync/conflict-resolve.d.ts +9 -0
- package/dist/sync/conflict-resolve.js +146 -0
- package/dist/sync/git-state.d.ts +4 -0
- package/dist/sync/git-state.js +49 -0
- package/dist/sync/outcome.d.ts +34 -0
- package/dist/sync/outcome.js +57 -0
- package/dist/sync/pull.d.ts +15 -7
- package/dist/sync/pull.js +84 -35
- package/dist/sync/store-merge.d.ts +29 -0
- package/dist/sync/store-merge.js +137 -0
- package/dist/sync/task-merge.d.ts +22 -0
- package/dist/sync/task-merge.js +158 -0
- package/dist/task/github.js +1 -1
- package/dist/task/lifecycle.d.ts +5 -0
- package/dist/task/lifecycle.js +67 -23
- package/dist/terminal-text.d.ts +5 -0
- package/dist/terminal-text.js +11 -0
- package/dist/test-global-setup.js +7 -0
- package/dist/tool-registry.d.ts +1 -1
- package/dist/tool-registry.js +3 -1
- package/dist/tools/code.d.ts +3 -0
- package/dist/tools/code.js +186 -0
- package/dist/tools/config.js +8 -0
- package/dist/tools/data.js +14 -7
- package/dist/tools/dispatch.d.ts +2 -0
- package/dist/tools/dispatch.js +66 -0
- package/dist/tools/finding.d.ts +11 -0
- package/dist/tools/finding.js +180 -123
- package/dist/tools/ops.js +5 -2
- package/dist/tools/search.js +21 -8
- package/dist/tools/session.js +21 -22
- package/dist/tools/tasks.js +8 -2
- package/dist/tools/types.d.ts +2 -2
- package/dist/tools/types.js +2 -2
- package/dist/ui/assets.js +1 -1
- package/dist/ui/data.js +14 -15
- package/dist/ui/deep-void.d.ts +1 -1
- package/dist/ui/deep-void.js +0 -1
- package/dist/ui/graph-chrome.d.ts +1 -1
- package/dist/ui/graph-chrome.js +51 -0
- package/dist/ui/page.js +5 -5
- package/dist/ui/scripts.js +67 -30
- package/dist/ui/server.js +52 -7
- package/dist/update.js +22 -11
- package/dist/utils-fts.js +1 -0
- package/dist/utils-helpers.d.ts +7 -0
- package/dist/utils-helpers.js +17 -0
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +1 -1
- package/package.json +49 -7
- package/plugins/opencode/phren-transcript.js +340 -0
- package/starter/README.md +4 -4
- package/starter/global/{CLAUDE.md → AGENTS.md} +2 -2
- package/starter/global/skills/conductor/SKILL.md +106 -0
- package/starter/global/skills/fanout/SKILL.md +20 -0
- package/starter/global/skills/phren-discover/SKILL.md +6 -6
- package/starter/global/skills/phren-init/SKILL.md +4 -4
- package/starter/global/skills/phren-sync/SKILL.md +3 -3
- package/starter/templates/frontend/reference/README.md +1 -1
- package/starter/templates/library/reference/README.md +1 -1
- package/starter/templates/monorepo/reference/README.md +1 -1
- package/starter/templates/python-project/reference/README.md +1 -1
- /package/starter/templates/frontend/{CLAUDE.md → AGENTS.md} +0 -0
- /package/starter/templates/library/{CLAUDE.md → AGENTS.md} +0 -0
- /package/starter/templates/monorepo/{CLAUDE.md → AGENTS.md} +0 -0
- /package/starter/templates/python-project/{CLAUDE.md → AGENTS.md} +0 -0
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { gitBranches, gitDiscard, gitLog, gitPulls, gitStage, gitStatus, gitTree, gitUnstage } from "./git.js";
|
|
5
|
+
import { fanoutWorktrees } from "./fanouts.js";
|
|
6
|
+
import { gitWorktrees, resolveWorktree } from "./git-worktrees.js";
|
|
7
|
+
import { gitCommit, gitPullRequest, gitPush } from "./git-publish.js";
|
|
8
|
+
import { findPane, paneAgentName, paneChatState, paneIdentity, rpc, snapshot, startingPane, trustedDirectory, validateStartingTarget, validateTarget } from "./herdr.js";
|
|
9
|
+
import { refuseWorkingSlash } from "./model-switch.js";
|
|
10
|
+
import { sessionWebServers } from "./session-servers.js";
|
|
11
|
+
import { repositoryDiff } from "./projects.js";
|
|
12
|
+
import { BridgeError, MAX_FRAME, object, objects, startingTargetSchema, targetSchema } from "./protocol.js";
|
|
13
|
+
import { childAgent, childAgentTree, conversationNamedPaths, transcriptPath } from "./transcripts.js";
|
|
14
|
+
import { sideQuestionText } from "./side-questions.js";
|
|
15
|
+
import { saveUpload } from "./uploads.js";
|
|
16
|
+
export /** A file from the phone: a plain name and base64 bytes, bounded. */ function uploadBody(data) {
|
|
17
|
+
const name = z.string().regex(/^[A-Za-z0-9_][A-Za-z0-9_ .()-]{0,199}$/).refine(n => !n.includes("..")).parse(data.name);
|
|
18
|
+
const encoded = z.string().max(11_184_812).regex(/^[A-Za-z0-9+/]*={0,2}$/).parse(data.data);
|
|
19
|
+
const bytes = Buffer.from(encoded, "base64");
|
|
20
|
+
if (!bytes.length || bytes.length > MAX_FRAME)
|
|
21
|
+
throw new BridgeError(413, "The file is empty or too large.");
|
|
22
|
+
return { name, bytes };
|
|
23
|
+
}
|
|
24
|
+
/** The repository a git route acts on: the pane's trusted directory, a
|
|
25
|
+
* spawned child's own worktree exactly as /v1/diff resolves it, or one of the
|
|
26
|
+
* pane repository's other worktrees as `/v1/git/worktrees` lists it. */
|
|
27
|
+
export async function gitRepository(pane, target, child, worktree) {
|
|
28
|
+
const id = z.string().regex(/^[a-f0-9]{32}$/).optional().parse(child);
|
|
29
|
+
if (worktree !== undefined && worktree !== null) {
|
|
30
|
+
if (id !== undefined)
|
|
31
|
+
throw new BridgeError(400, "Choose a child agent or a worktree, not both.");
|
|
32
|
+
return resolveWorktree(await trustedDirectory(pane), worktree);
|
|
33
|
+
}
|
|
34
|
+
if (id !== undefined) {
|
|
35
|
+
const relation = childAgent(await childAgentTree(target.source, target.session), id);
|
|
36
|
+
if (!relation)
|
|
37
|
+
throw new BridgeError(404, "That agent is not part of this conversation.");
|
|
38
|
+
return relation.cwd ?? await trustedDirectory(pane);
|
|
39
|
+
}
|
|
40
|
+
return trustedDirectory(pane);
|
|
41
|
+
}
|
|
42
|
+
/** Who might be editing each worktree: this conversation's agents with a
|
|
43
|
+
* checkout of their own (so the phone can open that agent), then every
|
|
44
|
+
* fan-out manifest by its recorded worktree. Best effort; a missing
|
|
45
|
+
* transcript or manifest only leaves a worktree unlabelled. */
|
|
46
|
+
async function worktreeWorkers(target) {
|
|
47
|
+
const workers = [];
|
|
48
|
+
const visit = (nodes) => {
|
|
49
|
+
for (const node of nodes) {
|
|
50
|
+
if (node.remote === undefined && node.cwd)
|
|
51
|
+
workers.push({ cwd: node.cwd, label: node.path, provider: node.provider, child: node.id, state: node.state });
|
|
52
|
+
visit(node.children);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
visit(await childAgentTree(target.source, target.session).catch(() => []));
|
|
56
|
+
for (const job of await fanoutWorktrees().catch(() => []))
|
|
57
|
+
workers.push({ cwd: job.worktree, label: job.label, provider: job.provider, state: job.state });
|
|
58
|
+
workers.push(...herdrWorktreeWorkers(await snapshot(target.server).catch(() => ({}))));
|
|
59
|
+
return workers;
|
|
60
|
+
}
|
|
61
|
+
/** Agents running in this Herdr server's panes, by the folder each works in:
|
|
62
|
+
* an agent launched into a new worktree from the phone is named here. Listed
|
|
63
|
+
* last, so a sub-agent or fan-out manifest for the same folder wins a tie. */
|
|
64
|
+
export function herdrWorktreeWorkers(s) {
|
|
65
|
+
const workers = [];
|
|
66
|
+
for (const pane of objects(s.panes)) {
|
|
67
|
+
const cwd = pane.foreground_cwd || pane.cwd;
|
|
68
|
+
if (typeof pane.agent !== "string" || typeof cwd !== "string" || !path.isAbsolute(cwd))
|
|
69
|
+
continue;
|
|
70
|
+
const name = paneAgentName(s, pane);
|
|
71
|
+
workers.push({ cwd, label: name || pane.agent, provider: pane.agent,
|
|
72
|
+
...(typeof pane.agent_status === "string" ? { state: pane.agent_status } : {}) });
|
|
73
|
+
}
|
|
74
|
+
return workers;
|
|
75
|
+
}
|
|
76
|
+
/** The phone can press these and nothing else; never a typed string.
|
|
77
|
+
* `AltUp` is Codex's "edit/answer the last queued follow-up": it opens the
|
|
78
|
+
* queue, after which the option key (or typed text) is the answer. */
|
|
79
|
+
const ANSWER_KEYS = ["Escape", "Enter", "Up", "Down", "Tab", "AltUp", "y", "n", "p", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
|
|
80
|
+
const HERDR_KEYS = { Escape: "esc", Enter: "enter", Up: "up", Down: "down", Tab: "tab", AltUp: "alt+Up" };
|
|
81
|
+
/** A secret typed into a terminal prompt: printable, bounded, never logged. */
|
|
82
|
+
const secretText = z.string().min(1).max(256).refine(t => !/[\x00-\x1f\x7f]/.test(t));
|
|
83
|
+
/** An idle agent that has not taken typed text within the first wait
|
|
84
|
+
* likely lost the Enter while it redrew (an update notice, a resize): the
|
|
85
|
+
* text sits in its input line. Press Enter once more and wait again. A
|
|
86
|
+
* pane that turned working took it; one still idle is reported unsubmitted,
|
|
87
|
+
* so the phone shows the message as not sent instead of waiting forever. */
|
|
88
|
+
async function resubmitIfIdle(agentHooks, target, text) {
|
|
89
|
+
const status = async () => { try {
|
|
90
|
+
return String(findPane(await snapshot(target.server), target)?.agent_status ?? "unknown");
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
return "unknown";
|
|
94
|
+
} };
|
|
95
|
+
if (await status() === "working")
|
|
96
|
+
return "pending";
|
|
97
|
+
const late = agentHooks.awaitLateDelivery(target, text);
|
|
98
|
+
await rpc(target.server, "agent.send_keys", { target: target.pane, keys: ["enter"] });
|
|
99
|
+
const outcome = await late;
|
|
100
|
+
if (outcome !== "pending")
|
|
101
|
+
return outcome;
|
|
102
|
+
return await status() === "working" ? "pending" : "unsubmitted";
|
|
103
|
+
}
|
|
104
|
+
/** A busy Claude Code queues typed text on Enter, but after a long paste
|
|
105
|
+
* ("paste again to expand") it can swallow that Enter and leave the text in
|
|
106
|
+
* its input line, where it stays after the turn ends. Follow the pane: once
|
|
107
|
+
* it has stayed finished for two looks while the prompt is still unsubmitted,
|
|
108
|
+
* press Enter once. A closed or replaced pane, a delivery the conversation
|
|
109
|
+
* took, or ten minutes end the watch; a pane asking for input is never answered. */
|
|
110
|
+
export function followQueuedDelivery(agentHooks, target, terminal, text, intervalMs = 2_000) {
|
|
111
|
+
const started = Date.now();
|
|
112
|
+
let finishedLooks = 0;
|
|
113
|
+
const look = async () => {
|
|
114
|
+
if (!agentHooks.deliveryPending(target, text) || Date.now() - started > 600_000)
|
|
115
|
+
return;
|
|
116
|
+
let pane;
|
|
117
|
+
try {
|
|
118
|
+
pane = findPane(await snapshot(target.server), target);
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
pane = undefined;
|
|
122
|
+
}
|
|
123
|
+
if (!pane) {
|
|
124
|
+
schedule();
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (pane.terminal_id !== terminal)
|
|
128
|
+
return;
|
|
129
|
+
const status = String(pane.agent_status);
|
|
130
|
+
finishedLooks = status === "idle" || status === "done" ? finishedLooks + 1 : 0;
|
|
131
|
+
if (finishedLooks >= 2) {
|
|
132
|
+
await resubmitIfIdle(agentHooks, target, text).catch(() => undefined);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
schedule();
|
|
136
|
+
};
|
|
137
|
+
const schedule = () => { const timer = setTimeout(() => void look(), intervalMs); timer.unref?.(); };
|
|
138
|
+
schedule();
|
|
139
|
+
}
|
|
140
|
+
/** A fresh Claude Code that took its first prompt starts working. One that
|
|
141
|
+
* stays idle kept the brief in its input line (a long paste can swallow the
|
|
142
|
+
* Enter): press Enter once, only while it is idle, so a dialog is never
|
|
143
|
+
* answered. False when it still has not started. */
|
|
144
|
+
async function submitStartingPrompt(server, target, terminal, looks = 4, intervalMs = 500) {
|
|
145
|
+
const status = async () => {
|
|
146
|
+
try {
|
|
147
|
+
const pane = findPane(await snapshot(server), target);
|
|
148
|
+
return pane && pane.terminal_id === terminal ? String(pane.agent_status) : "gone";
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
return "unknown";
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
const settled = async () => {
|
|
155
|
+
for (let look = 0; look < looks; look++) {
|
|
156
|
+
await sleep(intervalMs);
|
|
157
|
+
const current = await status();
|
|
158
|
+
if (current !== "idle" && current !== "done")
|
|
159
|
+
return current;
|
|
160
|
+
}
|
|
161
|
+
return "idle";
|
|
162
|
+
};
|
|
163
|
+
const first = await settled();
|
|
164
|
+
if (first !== "idle")
|
|
165
|
+
return first !== "gone";
|
|
166
|
+
await rpc(server, "agent.send_keys", { target: target.pane, keys: ["enter"] });
|
|
167
|
+
const second = await settled();
|
|
168
|
+
return second !== "idle" && second !== "gone";
|
|
169
|
+
}
|
|
170
|
+
/** One key per character; Herdr's send_keys takes single characters and named
|
|
171
|
+
* keys only, and a tty password read is corrupted by a bracketed paste. */
|
|
172
|
+
function secretKeys(text) {
|
|
173
|
+
return [...text].map(character => character === " " ? "space" : character);
|
|
174
|
+
}
|
|
175
|
+
/** Type a secret, then submit it. A chunk that may already have reached the
|
|
176
|
+
* terminal is never resent; the phone is told to check it by hand. */
|
|
177
|
+
async function typeSecret(server, pane, text) {
|
|
178
|
+
const keys = secretKeys(text);
|
|
179
|
+
let sent = false;
|
|
180
|
+
for (let index = 0; index < keys.length; index += 32) {
|
|
181
|
+
try {
|
|
182
|
+
await rpc(server, "agent.send_keys", { target: pane, keys: keys.slice(index, index + 32) });
|
|
183
|
+
sent = true;
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
if (sent)
|
|
187
|
+
throw new BridgeError(502, "The password may have been typed only partly; check the terminal.");
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
try {
|
|
192
|
+
await rpc(server, "agent.send_keys", { target: pane, keys: ["enter"] });
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
if (sent)
|
|
196
|
+
throw new BridgeError(502, "The password may have been typed only partly; check the terminal.");
|
|
197
|
+
throw error;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
export async function paneRoute(ctx, url, data, response) {
|
|
201
|
+
const { agentHooks, modelSwitcher, codexQuestions, sideQuestions } = ctx;
|
|
202
|
+
let result;
|
|
203
|
+
if (url.pathname === "/v1/keys" && object(data.target).starting === true) {
|
|
204
|
+
// A folder-trust or login prompt comes before the agent has a
|
|
205
|
+
// conversation; the phone answers it on the starting binding.
|
|
206
|
+
const target = startingTargetSchema.parse(data.target);
|
|
207
|
+
const pane = await startingPane(target);
|
|
208
|
+
const keys = z.array(z.enum(ANSWER_KEYS)).min(1).max(4).parse(data.keys);
|
|
209
|
+
if (!["blocked", "waiting", "unknown"].includes(String(pane.agent_status)))
|
|
210
|
+
throw new BridgeError(409, "This agent is not waiting for an answer.");
|
|
211
|
+
await rpc(target.server, "agent.send_keys", { target: target.pane, keys: keys.map(key => HERDR_KEYS[key] ?? key) });
|
|
212
|
+
result = { ok: true };
|
|
213
|
+
}
|
|
214
|
+
else if (url.pathname === "/v1/secret" && object(data.target).starting === true) {
|
|
215
|
+
// A password the terminal is reading before the agent has a
|
|
216
|
+
// conversation is typed the same way as one after it.
|
|
217
|
+
const target = startingTargetSchema.parse(data.target);
|
|
218
|
+
const pane = await startingPane(target);
|
|
219
|
+
const text = secretText.parse(data.text);
|
|
220
|
+
if (!["blocked", "waiting", "unknown"].includes(String(pane.agent_status)))
|
|
221
|
+
throw new BridgeError(409, "This agent is not waiting for an answer.");
|
|
222
|
+
await typeSecret(target.server, target.pane, text);
|
|
223
|
+
result = { ok: true };
|
|
224
|
+
}
|
|
225
|
+
else if (url.pathname === "/v1/prompt" && object(data.target).starting === true) {
|
|
226
|
+
const target = startingTargetSchema.parse(data.target);
|
|
227
|
+
const pane = await validateStartingTarget(target);
|
|
228
|
+
const text = z.string().min(1).max(32768).refine(t => !/[\x00-\x08\x0b-\x1f\x7f]/.test(t)).parse(data.text);
|
|
229
|
+
refuseWorkingSlash(pane, text);
|
|
230
|
+
await rpc(target.server, "agent.prompt", { target: target.pane, text });
|
|
231
|
+
// A dispatched worker's brief is often long enough for Claude Code to
|
|
232
|
+
// swallow the Enter; a slash command opens a menu an Enter would answer.
|
|
233
|
+
const submitted = target.source !== "claude" || text.trim().startsWith("/")
|
|
234
|
+
|| await submitStartingPrompt(target.server, target, pane.terminal_id);
|
|
235
|
+
if (!submitted)
|
|
236
|
+
result = { ok: true, deliveryUncertain: true, unsubmitted: true };
|
|
237
|
+
else {
|
|
238
|
+
// A first prompt may create its transcript immediately. Recheck the
|
|
239
|
+
// terminal/process binding, not the absence of a session. Never retry.
|
|
240
|
+
let confirmed = false;
|
|
241
|
+
try {
|
|
242
|
+
const current = findPane(await snapshot(target.server), target);
|
|
243
|
+
confirmed = !!current && current.terminal_id === pane.terminal_id && (await paneChatState(target.server, current)).startingToken === target.startingToken;
|
|
244
|
+
}
|
|
245
|
+
catch { /* Already delivered; an uncertain reply must not resend. */ }
|
|
246
|
+
result = { ok: true, ...(!confirmed ? { deliveryUncertain: true } : {}) };
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
const target = targetSchema.parse(data.target);
|
|
251
|
+
// Uploads store bytes without answering or interrupting the agent.
|
|
252
|
+
// They still require fresh identity, just like prompt mutations.
|
|
253
|
+
const sendsInput = ["/v1/prompt", "/v1/keys", "/v1/secret", "/v1/model"].includes(url.pathname);
|
|
254
|
+
if (sendsInput) {
|
|
255
|
+
modelSwitcher.assertAvailable(target);
|
|
256
|
+
sideQuestions.assertAvailable(target);
|
|
257
|
+
}
|
|
258
|
+
// A key press is how a prompt the agent draws in its terminal gets
|
|
259
|
+
// answered, so keys are the one input allowed while the agent is
|
|
260
|
+
// blocked or waiting; the status check below is theirs alone.
|
|
261
|
+
const pane = await validateTarget(target, false, sendsInput || url.pathname === "/v1/upload");
|
|
262
|
+
if (sendsInput) {
|
|
263
|
+
modelSwitcher.assertAvailable(target);
|
|
264
|
+
sideQuestions.assertAvailable(target);
|
|
265
|
+
}
|
|
266
|
+
if (url.pathname === "/v1/prompt") {
|
|
267
|
+
// A waiting agent takes typed text only when nothing structured
|
|
268
|
+
// is pending there: an approval the Hook holds or saw, or a
|
|
269
|
+
// status Herdr cannot read. Otherwise the answer keys are the way.
|
|
270
|
+
const status = String(pane.agent_status);
|
|
271
|
+
if (status === "unknown" || (["blocked", "waiting"].includes(status) && (agentHooks.approval(target) || agentHooks.terminalPrompt(target)))) {
|
|
272
|
+
throw new BridgeError(409, "This agent needs input in the terminal first.");
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (url.pathname === "/v1/prompt") {
|
|
276
|
+
const text = z.string().min(1).max(32768).refine(t => !/[\x00-\x08\x0b-\x1f\x7f]/.test(t)).parse(data.text);
|
|
277
|
+
// Herdr types into the pane; the agent that receives the text
|
|
278
|
+
// confirms or refuses it through its UserPromptSubmit hook, by
|
|
279
|
+
// conversation. That is the binding Herdr's own API lacks.
|
|
280
|
+
// A working agent queues typed text and submits it when its turn
|
|
281
|
+
// ends, which can be minutes away; waiting for that only delays
|
|
282
|
+
// the phone. The record still guards the paste for ten minutes.
|
|
283
|
+
refuseWorkingSlash(pane, text, target.source);
|
|
284
|
+
if (sideQuestionText(target.source, text) !== undefined) {
|
|
285
|
+
// Claude's `/btw` runs beside the turn and never reaches the transcript:
|
|
286
|
+
// the Hook reads its panel and the answer arrives as a side-answer frame.
|
|
287
|
+
return { ok: true, delivered: true, sideQuestion: await sideQuestions.ask(target, pane, text) };
|
|
288
|
+
}
|
|
289
|
+
if (target.source === "codex" && /^\s*\/model\s+\S/i.test(text))
|
|
290
|
+
throw new BridgeError(422, "Use the model picker to switch Codex models.");
|
|
291
|
+
const busy = String(pane.agent_status) === "working";
|
|
292
|
+
const expected = agentHooks.expectDelivery(target, text, busy ? 300 : 1_500);
|
|
293
|
+
await rpc(target.server, "agent.prompt", { target: target.pane, text });
|
|
294
|
+
let outcome = await expected;
|
|
295
|
+
// Only Claude confirms plain prompts through its hook, and a slash
|
|
296
|
+
// command opens a menu that a second Enter would answer.
|
|
297
|
+
if (outcome === "pending" && target.source === "claude" && !text.trim().startsWith("/")) {
|
|
298
|
+
if (!busy)
|
|
299
|
+
outcome = await resubmitIfIdle(agentHooks, target, text);
|
|
300
|
+
else
|
|
301
|
+
followQueuedDelivery(agentHooks, target, pane.terminal_id, text);
|
|
302
|
+
}
|
|
303
|
+
if (outcome === "blocked")
|
|
304
|
+
throw new BridgeError(409, "The conversation in this pane changed; the message was not delivered. Reopen the chat and send it again.");
|
|
305
|
+
// A bare slash command opens the agent's own menu; the phone may
|
|
306
|
+
// walk it with keys for the next half minute. The command rides
|
|
307
|
+
// along so a Codex /permissions walk can find its confirmation.
|
|
308
|
+
if (/^\/[a-z][a-z0-9_-]*$/i.test(text.trim()))
|
|
309
|
+
agentHooks.menuOpened(target, text.trim());
|
|
310
|
+
if (outcome === "delivered") {
|
|
311
|
+
result = { ok: true, delivered: true };
|
|
312
|
+
}
|
|
313
|
+
else if (outcome === "unsubmitted") {
|
|
314
|
+
result = { ok: true, deliveryUncertain: true, unsubmitted: true };
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
// The agent has not submitted it yet (a busy agent queues typed
|
|
318
|
+
// input). Recheck fresh identity and never retry; a late
|
|
319
|
+
// submission to another conversation is still refused above.
|
|
320
|
+
let confirmed = false;
|
|
321
|
+
try {
|
|
322
|
+
const current = findPane(await snapshot(target.server), target);
|
|
323
|
+
confirmed = !!current && current.terminal_id === pane.terminal_id && await paneIdentity(target.server, current, true) === target.session;
|
|
324
|
+
}
|
|
325
|
+
catch { /* No reliable post-delivery identity. */ }
|
|
326
|
+
result = { ok: true, ...(!confirmed ? { deliveryUncertain: true } : {}) };
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
else if (url.pathname === "/v1/side-question/dismiss") {
|
|
330
|
+
result = sideQuestions.dismiss(target, z.string().uuid().parse(data.id));
|
|
331
|
+
}
|
|
332
|
+
else if (url.pathname === "/v1/model") {
|
|
333
|
+
result = await modelSwitcher.switch(target, data);
|
|
334
|
+
}
|
|
335
|
+
else if (url.pathname === "/v1/keys") {
|
|
336
|
+
const keys = z.array(z.enum(ANSWER_KEYS)).min(1).max(4).parse(data.keys);
|
|
337
|
+
const status = String(pane.agent_status), menu = agentHooks.menuOpen(target);
|
|
338
|
+
// A prompt the Hook itself saw go by (a permission request it could
|
|
339
|
+
// not hold) is being answered even when Herdr reads the pane as
|
|
340
|
+
// working or idle; Herdr's status lags the agent's own dialog.
|
|
341
|
+
const holding = menu || !!agentHooks.terminalPrompt(target);
|
|
342
|
+
// Escape interrupts a working agent. Everything else answers a
|
|
343
|
+
// prompt the agent is holding: a menu, a y/n, a trust question.
|
|
344
|
+
if (!holding && (keys.every(key => key === "Escape") ? !["working", "blocked", "waiting", "unknown"].includes(status)
|
|
345
|
+
: !["blocked", "waiting", "unknown"].includes(status)))
|
|
346
|
+
throw new BridgeError(409, keys.every(key => key === "Escape") ? "This agent is no longer working." : "This agent is not waiting for an answer.");
|
|
347
|
+
// A released AskUserQuestion is answered one question at a time:
|
|
348
|
+
// the Hook sends the chosen digit, then Tab to advance or Enter
|
|
349
|
+
// after the last, and clears the prompt when the set is done.
|
|
350
|
+
const question = agentHooks.questionAnswerKeys(target, keys);
|
|
351
|
+
if (question) {
|
|
352
|
+
await rpc(target.server, "agent.send_keys", { target: target.pane, keys: question.map(key => HERDR_KEYS[key] ?? key) });
|
|
353
|
+
result = { ok: true };
|
|
354
|
+
}
|
|
355
|
+
else {
|
|
356
|
+
// Keyless choices move and verify the highlight before Enter;
|
|
357
|
+
// the phone sends the option identifier through the same route.
|
|
358
|
+
const answerKeys = await agentHooks.dialogAnswerKeys(target, keys);
|
|
359
|
+
await rpc(target.server, "agent.send_keys", { target: target.pane, keys: answerKeys.map(key => HERDR_KEYS[key] ?? key) });
|
|
360
|
+
// A remembered prompt is answered by any key but a cursor move; the
|
|
361
|
+
// menu window stays open through Enter because some choices (Codex
|
|
362
|
+
// full access) open a second confirmation the Hook now walks itself.
|
|
363
|
+
if (keys.some(key => key !== "Up" && key !== "Down" && key !== "Tab")) {
|
|
364
|
+
agentHooks.clearTerminalPrompt(target);
|
|
365
|
+
agentHooks.releaseChoice(target);
|
|
366
|
+
}
|
|
367
|
+
if (keys.includes("Escape"))
|
|
368
|
+
agentHooks.menuClosed(target);
|
|
369
|
+
// Enter on Codex's /permissions menu may open "Enable full access?".
|
|
370
|
+
// Watch the pane's lines for it, answer its visible option, and only
|
|
371
|
+
// then close the window; a prompt that never arrives is reported as
|
|
372
|
+
// still waiting with the visible text for the phone's question card.
|
|
373
|
+
if (answerKeys.includes("Enter") && menu && target.source === "codex"
|
|
374
|
+
&& (agentHooks.menuCommand(target) ?? "").toLowerCase() === "/permissions") {
|
|
375
|
+
const walk = await agentHooks.walkMenuConfirmation(target);
|
|
376
|
+
result = { ok: true, ...(walk.menuClosed ? { menuClosed: true } : {}),
|
|
377
|
+
...(walk.waiting ? { waiting: walk.waiting } : {}) };
|
|
378
|
+
}
|
|
379
|
+
else
|
|
380
|
+
result = { ok: true };
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
else if (url.pathname === "/v1/secret") {
|
|
384
|
+
// A password the terminal is reading (sudo, a login) cannot be
|
|
385
|
+
// pasted: bracketed paste corrupts a tty read, so it is typed a
|
|
386
|
+
// character at a time. The Hook never logs, echoes or stores it.
|
|
387
|
+
const text = secretText.parse(data.text);
|
|
388
|
+
if (!["blocked", "waiting", "unknown"].includes(String(pane.agent_status)))
|
|
389
|
+
throw new BridgeError(409, "This agent is not waiting for an answer.");
|
|
390
|
+
await typeSecret(target.server, target.pane, text);
|
|
391
|
+
agentHooks.clearTerminalPrompt(target);
|
|
392
|
+
result = { ok: true };
|
|
393
|
+
}
|
|
394
|
+
else if (url.pathname === "/v1/upload") {
|
|
395
|
+
const { name, bytes } = uploadBody(data);
|
|
396
|
+
result = { ok: true, path: await saveUpload(target.session, name, bytes) };
|
|
397
|
+
}
|
|
398
|
+
else if (url.pathname === "/v1/web-servers/session") {
|
|
399
|
+
// This session's own servers only; the machine-wide list is /v1/web-servers.
|
|
400
|
+
result = await sessionWebServers(target, pane);
|
|
401
|
+
}
|
|
402
|
+
else if (url.pathname === "/v1/diff") {
|
|
403
|
+
const child = z.string().regex(/^[a-f0-9]{32}$/).optional().parse(data.child);
|
|
404
|
+
if (data.worktree !== undefined) {
|
|
405
|
+
// Another worktree of the pane's repository: the whole checkout.
|
|
406
|
+
result = await repositoryDiff(await gitRepository(pane, target, data.child, data.worktree), [], []);
|
|
407
|
+
}
|
|
408
|
+
else if (child !== undefined) {
|
|
409
|
+
// A spawned agent: its own worktree for a fan-out, otherwise the
|
|
410
|
+
// parent's checkout. The whole repository, no phone-named paths.
|
|
411
|
+
const relation = childAgent(await childAgentTree(target.source, target.session), child);
|
|
412
|
+
if (!relation)
|
|
413
|
+
throw new BridgeError(404, "That agent is not part of this conversation.");
|
|
414
|
+
result = await repositoryDiff(relation.cwd ?? await trustedDirectory(pane), [], []);
|
|
415
|
+
}
|
|
416
|
+
else {
|
|
417
|
+
const cwd = await trustedDirectory(pane), paths = z.array(z.string().max(4096)).max(24).optional().parse(data.paths) ?? [];
|
|
418
|
+
const abort = new AbortController();
|
|
419
|
+
response.once("close", () => { if (!response.writableEnded)
|
|
420
|
+
abort.abort(); });
|
|
421
|
+
const allowed = paths.length ? await agentHooks.changes.recordedPaths(`${target.source}:${target.session}`) : [];
|
|
422
|
+
if (paths.length) {
|
|
423
|
+
try {
|
|
424
|
+
allowed.push(...await conversationNamedPaths(await transcriptPath(target.source, target.session), target.source, cwd, abort.signal));
|
|
425
|
+
}
|
|
426
|
+
catch {
|
|
427
|
+
abort.signal.throwIfAborted(); /* Missing transcripts grant no extra paths; recorded scope still works. */
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
result = await repositoryDiff(cwd, paths, allowed);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
else if (url.pathname.startsWith("/v1/git/")) {
|
|
434
|
+
// Git routes read the pane's repository, or a spawned child's own
|
|
435
|
+
// worktree, exactly as /v1/diff resolves it.
|
|
436
|
+
const cwd = await gitRepository(pane, target, data.child, data.worktree);
|
|
437
|
+
if (url.pathname === "/v1/git/status")
|
|
438
|
+
result = await gitStatus(cwd);
|
|
439
|
+
else if (url.pathname === "/v1/git/worktrees")
|
|
440
|
+
result = await gitWorktrees(cwd, await worktreeWorkers(target));
|
|
441
|
+
else if (url.pathname === "/v1/git/log")
|
|
442
|
+
result = await gitLog(cwd, z.coerce.number().int().min(1).max(200).optional().parse(data.limit) ?? 60, z.string().min(1).max(512).optional().parse(data.ref));
|
|
443
|
+
else if (url.pathname === "/v1/git/branches")
|
|
444
|
+
result = await gitBranches(cwd);
|
|
445
|
+
else if (url.pathname === "/v1/git/pulls")
|
|
446
|
+
result = await gitPulls(cwd);
|
|
447
|
+
else if (url.pathname === "/v1/git/tree")
|
|
448
|
+
result = await gitTree(cwd, z.string().max(4096).optional().parse(data.path) ?? "", z.boolean().optional().parse(data.ignored) ?? false);
|
|
449
|
+
else if (url.pathname === "/v1/git/stage")
|
|
450
|
+
result = await gitStage(cwd, data.paths);
|
|
451
|
+
else if (url.pathname === "/v1/git/unstage")
|
|
452
|
+
result = await gitUnstage(cwd, data.paths);
|
|
453
|
+
else if (url.pathname === "/v1/git/discard")
|
|
454
|
+
result = await gitDiscard(cwd, data.paths);
|
|
455
|
+
else if (url.pathname === "/v1/git/commit")
|
|
456
|
+
result = await gitCommit(cwd, data.message);
|
|
457
|
+
else if (url.pathname === "/v1/git/push")
|
|
458
|
+
result = await gitPush(cwd, data.confirmDefault);
|
|
459
|
+
else if (url.pathname === "/v1/git/pr")
|
|
460
|
+
result = await gitPullRequest(cwd, data.draft);
|
|
461
|
+
else
|
|
462
|
+
throw new BridgeError(404, "Unknown Phren Hook route.");
|
|
463
|
+
}
|
|
464
|
+
else if (url.pathname === "/v1/approvals/answer") {
|
|
465
|
+
const actionId = target.source === "opencode"
|
|
466
|
+
? z.string().regex(/^[A-Za-z0-9_]{1,200}$/).parse(data.actionId)
|
|
467
|
+
: z.string().uuid().parse(data.actionId);
|
|
468
|
+
await agentHooks.answer(target, actionId, data.decision, data.updatedInput);
|
|
469
|
+
result = { ok: true };
|
|
470
|
+
}
|
|
471
|
+
else if (url.pathname === "/v1/questions/answer") {
|
|
472
|
+
await codexQuestions.answer(target, data);
|
|
473
|
+
result = { ok: true };
|
|
474
|
+
}
|
|
475
|
+
else
|
|
476
|
+
throw new BridgeError(404, "Unknown Phren Hook route.");
|
|
477
|
+
}
|
|
478
|
+
return result;
|
|
479
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import type { FanoutMessages } from "./fanout-messages.js";
|
|
3
|
+
import type { ModuleSnapshot } from "../modules/runtime.js";
|
|
4
|
+
import type { ActivityJournal } from "./activity.js";
|
|
5
|
+
import type { AgentHooks } from "./agent-hooks.js";
|
|
6
|
+
import type { WorkspaceContextUsage } from "./context.js";
|
|
7
|
+
import { type DispatchService } from "./dispatch.js";
|
|
8
|
+
import { type DispatchReturns } from "./dispatch-returns.js";
|
|
9
|
+
import type { LaunchLimiter } from "./limits.js";
|
|
10
|
+
import { type Json } from "./protocol.js";
|
|
11
|
+
import type { CodexQuestions } from "./questions.js";
|
|
12
|
+
import type { TabActivityStore } from "./tab-activity.js";
|
|
13
|
+
import type { ModelCatalog } from "./models.js";
|
|
14
|
+
import type { ModelSwitcher } from "./model-switch.js";
|
|
15
|
+
import type { SideQuestions } from "./side-questions.js";
|
|
16
|
+
import type { AccountUsageReader } from "./usage.js";
|
|
17
|
+
import type { Scheduler } from "./schedules.js";
|
|
18
|
+
import type { TranscriptStreams } from "./server-stream.js";
|
|
19
|
+
/** The Hook's HTTP API over its Unix socket: module gating, the GET routes,
|
|
20
|
+
* the POST routes that are not bound to one pane, and grant deletion. */
|
|
21
|
+
/** What /v1/health reports about this computer and its Hook. */
|
|
22
|
+
export interface HookInfo {
|
|
23
|
+
product: string;
|
|
24
|
+
protocol: number;
|
|
25
|
+
version: string;
|
|
26
|
+
computer: {
|
|
27
|
+
id: string;
|
|
28
|
+
name: string;
|
|
29
|
+
aliases?: string[];
|
|
30
|
+
};
|
|
31
|
+
capabilities: Record<string, unknown>;
|
|
32
|
+
modules: Record<string, string>;
|
|
33
|
+
store: string;
|
|
34
|
+
profile: string;
|
|
35
|
+
generation: string;
|
|
36
|
+
readonly load: {
|
|
37
|
+
average: number;
|
|
38
|
+
cpus: number;
|
|
39
|
+
};
|
|
40
|
+
readonly gatewayMs: number | undefined;
|
|
41
|
+
}
|
|
42
|
+
export interface RouteContext {
|
|
43
|
+
version: string;
|
|
44
|
+
modules: ModuleSnapshot;
|
|
45
|
+
info: HookInfo;
|
|
46
|
+
computerID: string;
|
|
47
|
+
scheduleStore: string;
|
|
48
|
+
scheduler?: Scheduler;
|
|
49
|
+
dispatches?: DispatchService;
|
|
50
|
+
returns?: DispatchReturns;
|
|
51
|
+
agentHooks: AgentHooks;
|
|
52
|
+
journal: ActivityJournal;
|
|
53
|
+
tabActivity: TabActivityStore;
|
|
54
|
+
contextUsage: WorkspaceContextUsage;
|
|
55
|
+
modelCatalog: ModelCatalog;
|
|
56
|
+
modelSwitcher: ModelSwitcher;
|
|
57
|
+
sideQuestions: SideQuestions;
|
|
58
|
+
accountUsage: AccountUsageReader;
|
|
59
|
+
codexQuestions: CodexQuestions;
|
|
60
|
+
launches: LaunchLimiter;
|
|
61
|
+
locatedDirectories: Set<string>;
|
|
62
|
+
fanoutMessages: FanoutMessages;
|
|
63
|
+
canary: (trigger: "manual" | "daily") => Promise<unknown>;
|
|
64
|
+
streams: TranscriptStreams;
|
|
65
|
+
}
|
|
66
|
+
/** How long the overview waits for per-tab git and transcript reads; the phone gives up at 20 s. */
|
|
67
|
+
export declare const OVERVIEW_ENRICH_BUDGET_MS = 5000;
|
|
68
|
+
export declare const capabilities: {
|
|
69
|
+
transcript: boolean;
|
|
70
|
+
progress: boolean;
|
|
71
|
+
images: boolean;
|
|
72
|
+
prompt: boolean;
|
|
73
|
+
stop: boolean;
|
|
74
|
+
terminal: string;
|
|
75
|
+
shell: string;
|
|
76
|
+
herdr: boolean;
|
|
77
|
+
diff: boolean;
|
|
78
|
+
webServers: boolean;
|
|
79
|
+
webPreview: string;
|
|
80
|
+
activity: boolean;
|
|
81
|
+
approvals: boolean;
|
|
82
|
+
questions: boolean;
|
|
83
|
+
accountUsage: boolean;
|
|
84
|
+
providers: string[];
|
|
85
|
+
files: boolean;
|
|
86
|
+
repositoryFiles: boolean;
|
|
87
|
+
subagents: boolean;
|
|
88
|
+
sideQuestions: boolean;
|
|
89
|
+
dispatch: boolean;
|
|
90
|
+
approvalPush: string;
|
|
91
|
+
simulators: boolean;
|
|
92
|
+
code: boolean;
|
|
93
|
+
overviewStream: boolean;
|
|
94
|
+
speech: boolean;
|
|
95
|
+
transcribe: boolean;
|
|
96
|
+
};
|
|
97
|
+
export declare function capabilitiesForModules(snapshot: ModuleSnapshot): Record<string, unknown>;
|
|
98
|
+
export declare function requireRoute(snapshot: ModuleSnapshot, method: string, route: string): void;
|
|
99
|
+
export declare function selectedServer(url: URL): string;
|
|
100
|
+
/**
|
|
101
|
+
* The overview a phone draws for one Herdr server: every tab with its agent,
|
|
102
|
+
* target, branch, model, running children and current step, from one
|
|
103
|
+
* `session.snapshot`. `GET /v1/workspaces` passes a fresh snapshot; the
|
|
104
|
+
* `/v1/overview` stream passes the shared one it already holds.
|
|
105
|
+
*/
|
|
106
|
+
export type WorkspacesReader = (server: string, s: Json, watchApprovals: boolean) => Promise<Json>;
|
|
107
|
+
export declare function workspacesReader(ctx: Pick<RouteContext, "modules" | "info" | "agentHooks" | "journal" | "tabActivity" | "contextUsage">): WorkspacesReader;
|
|
108
|
+
export declare function createRouteHandler(ctx: RouteContext): (request: IncomingMessage, response: ServerResponse) => Promise<void>;
|