@phren/cli 0.2.14 → 0.2.16
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 +193 -2
- package/dist/bridge/agent-hooks.js +1045 -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 +22 -0
- package/dist/bridge/code-note.js +42 -0
- package/dist/bridge/code-routes.d.ts +133 -0
- package/dist/bridge/code-routes.js +319 -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 +123 -0
- package/dist/bridge/dispatch-returns.js +279 -0
- package/dist/bridge/dispatch-tree.d.ts +54 -0
- package/dist/bridge/dispatch-tree.js +198 -0
- package/dist/bridge/dispatch.d.ts +207 -0
- package/dist/bridge/dispatch.js +261 -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 +32 -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 +42 -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 +52 -0
- package/dist/bridge/schedule-watch.js +242 -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 +29 -0
- package/dist/bridge/server-pane-routes.js +400 -0
- package/dist/bridge/server-routes.d.ts +106 -0
- package/dist/bridge/server-routes.js +538 -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 +94 -0
- package/dist/bridge/transcript-preview.js +476 -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 +41246 -21385
- package/dist/cli/actions.js +14 -1
- 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.js +3 -6
- 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 +3 -1
- package/dist/content/validate.js +26 -8
- 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 +7 -1
- package/dist/data/access.js +13 -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 +279 -283
- 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 +7 -5
- 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 -0
- package/dist/hooks.js +13 -0
- 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 +2 -2
- package/dist/init/init-walkthrough.d.ts +2 -0
- package/dist/init/init-walkthrough.js +17 -6
- 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 +49 -588
- package/dist/init/shared.js +2 -0
- package/dist/init/store-gitignore.d.ts +1 -1
- package/dist/init/store-gitignore.js +2 -0
- package/dist/init/teardown.d.ts +1 -1
- package/dist/init/teardown.js +2 -2
- package/dist/link/context.js +1 -1
- package/dist/link/doctor.d.ts +22 -1
- package/dist/link/doctor.js +100 -54
- 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 -15
- package/dist/mcp/profile.js +64 -9
- package/dist/memory-ui-graph.runtime.js +279 -283
- package/dist/modules/code-package.d.ts +16 -0
- package/dist/modules/code-package.js +152 -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 +4 -5
- package/dist/project-config.d.ts +19 -1
- package/dist/project-config.js +29 -3
- package/dist/project-topics.js +4 -4
- 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 -1
- package/dist/shared/retrieval.js +100 -11
- 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 +178 -0
- package/dist/tools/config.js +5 -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.js +43 -41
- 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 +34 -6
- package/dist/update.js +1 -1
- 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 +45 -3
- 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
package/dist/bridge/projects.js
CHANGED
|
@@ -1,22 +1,34 @@
|
|
|
1
|
+
import { nonInteractiveGitEnv } from "../utils-helpers.js";
|
|
1
2
|
import { execFile } from "node:child_process";
|
|
2
3
|
import { promisify } from "node:util";
|
|
3
4
|
import { request } from "node:http";
|
|
4
5
|
import { userInfo } from "node:os";
|
|
6
|
+
import { homeDir } from "../home-paths.js";
|
|
7
|
+
import { readFile, realpath, stat } from "node:fs/promises";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { phrenStoreRoot } from "./transcripts.js";
|
|
10
|
+
import { locateProject } from "./locate.js";
|
|
5
11
|
import { BridgeError } from "./protocol.js";
|
|
12
|
+
import { countGit } from "./metrics.js";
|
|
6
13
|
const exec = promisify(execFile);
|
|
7
|
-
export async function
|
|
8
|
-
|
|
9
|
-
|
|
14
|
+
export async function git(cwd, ...args) {
|
|
15
|
+
countGit("projects");
|
|
16
|
+
return (await exec("git", ["-C", cwd, "--no-pager", ...args], {
|
|
17
|
+
timeout: 10_000, maxBuffer: 4_194_304, env: nonInteractiveGitEnv({ ...process.env, GIT_OPTIONAL_LOCKS: "0", GIT_CONFIG_NOSYSTEM: "1" }),
|
|
10
18
|
})).stdout;
|
|
11
|
-
|
|
19
|
+
}
|
|
20
|
+
export async function gitRoot(dir) {
|
|
12
21
|
try {
|
|
13
|
-
|
|
22
|
+
return await realpath((await git(dir, "rev-parse", "--show-toplevel")).trim());
|
|
14
23
|
}
|
|
15
24
|
catch {
|
|
16
|
-
|
|
25
|
+
return undefined;
|
|
17
26
|
}
|
|
18
|
-
|
|
19
|
-
|
|
27
|
+
}
|
|
28
|
+
/** `git status` as the app lists it: one record per file, a staged and an
|
|
29
|
+
* unstaged section where each has a patch. Limited to `pathspecs` when given. */
|
|
30
|
+
async function statusFiles(root, pathspecs = []) {
|
|
31
|
+
const status = (await git(root, "status", "--porcelain=v1", "-z", "--untracked-files=normal", "--", ...pathspecs.map(spec => `:(literal)${spec}`))).split("\0");
|
|
20
32
|
const files = [];
|
|
21
33
|
for (let index = 0; index < status.length && files.length < 500; index++) {
|
|
22
34
|
const record = status[index];
|
|
@@ -29,13 +41,172 @@ export async function repositoryDiff(cwd) {
|
|
|
29
41
|
for (const [kind, staged] of [["staged", true], ["unstaged", false]]) {
|
|
30
42
|
if (code === "??")
|
|
31
43
|
continue;
|
|
32
|
-
const patch = await git("diff", "--no-ext-diff", "--no-textconv", ...(staged ? ["--cached"] : []), "--", file);
|
|
44
|
+
const patch = await git(root, "diff", "--no-ext-diff", "--no-textconv", ...(staged ? ["--cached"] : []), "--", `:(literal)${file}`);
|
|
33
45
|
if (patch)
|
|
34
46
|
sections.push({ id: `${kind}:${file}`, kind, binary: patch.includes("Binary files"), loadState: "loaded", patch });
|
|
35
47
|
}
|
|
36
48
|
files.push({ path: file, status: code, sections });
|
|
37
49
|
}
|
|
38
|
-
return
|
|
50
|
+
return files;
|
|
51
|
+
}
|
|
52
|
+
/** The last commit that touched `pathspec` in the past half hour — what a
|
|
53
|
+
* command changed when a hook (phren's own Stop hook, say) committed it before
|
|
54
|
+
* anyone looked. */
|
|
55
|
+
async function committed(root, pathspec) {
|
|
56
|
+
const log = await git(root, "log", "-1", "--since=30.minutes", "--format=%h%x1f%s%x1f%cr", "-p", "--no-ext-diff", "--no-textconv", "--no-color", "--", `:(literal)${pathspec}`).catch(() => "");
|
|
57
|
+
const newline = log.indexOf("\n");
|
|
58
|
+
if (newline < 0)
|
|
59
|
+
return undefined;
|
|
60
|
+
const [hash, subject, when] = log.slice(0, newline).split("\x1f");
|
|
61
|
+
const patch = log.slice(newline + 1).replace(/^\n+/, "");
|
|
62
|
+
if (!patch)
|
|
63
|
+
return undefined;
|
|
64
|
+
return { id: `committed:${pathspec}`, kind: "committed", binary: patch.includes("Binary files"), loadState: "loaded", patch, note: `${hash} · ${subject.slice(0, 120)} · ${when}` };
|
|
65
|
+
}
|
|
66
|
+
/** A path a command named, made absolute and real — `~/` expanded, relative
|
|
67
|
+
* ones taken from the pane. The caller checks conversation scope after
|
|
68
|
+
* resolution. Missing files resolve through their nearest existing parent so
|
|
69
|
+
* a deleted file still finds its repository. */
|
|
70
|
+
async function resolveTouched(raw, cwd) {
|
|
71
|
+
if (typeof raw !== "string" || !raw || raw.length > 4096 || raw.includes("\0"))
|
|
72
|
+
return undefined;
|
|
73
|
+
const home = homeDir();
|
|
74
|
+
const absolute = raw === "~" || raw.startsWith("~/") ? path.join(home, raw.slice(1)) : path.resolve(cwd, raw);
|
|
75
|
+
let existing = absolute, rest = [];
|
|
76
|
+
while (!(await stat(existing).catch(() => undefined))) {
|
|
77
|
+
const parent = path.dirname(existing);
|
|
78
|
+
if (parent === existing)
|
|
79
|
+
return undefined;
|
|
80
|
+
rest.unshift(path.basename(existing));
|
|
81
|
+
existing = parent;
|
|
82
|
+
}
|
|
83
|
+
const real = path.join(await realpath(existing), ...rest);
|
|
84
|
+
return real;
|
|
85
|
+
}
|
|
86
|
+
/** The pane's working tree, plus anything the command named: files in the
|
|
87
|
+
* same repository that a hook already committed, and files in other
|
|
88
|
+
* repositories — the phren store, a sibling checkout — grouped by root. */
|
|
89
|
+
export async function repositoryDiff(cwd, touched = [], allowedPaths = []) {
|
|
90
|
+
const root = await gitRoot(cwd);
|
|
91
|
+
if (!root)
|
|
92
|
+
throw new BridgeError(409, "This pane is not in a Git repository.");
|
|
93
|
+
const allowed = (await Promise.all([root, phrenStoreRoot(), ...allowedPaths].map(raw => resolveTouched(raw, cwd)))).filter((p) => !!p);
|
|
94
|
+
const requested = [];
|
|
95
|
+
for (const raw of touched.slice(0, 24)) {
|
|
96
|
+
const file = await resolveTouched(raw, cwd);
|
|
97
|
+
if (!file || !allowed.some(base => file === base || file.startsWith(base + path.sep)))
|
|
98
|
+
throw new BridgeError(403, "This path is outside the conversation's recorded changes and commands.");
|
|
99
|
+
requested.push(file);
|
|
100
|
+
}
|
|
101
|
+
const branch = (await git(root, "branch", "--show-current")).trim();
|
|
102
|
+
const files = await statusFiles(root);
|
|
103
|
+
const byRoot = new Map();
|
|
104
|
+
for (const file of requested) {
|
|
105
|
+
const owner = await gitRoot((await stat(file).catch(() => undefined))?.isDirectory() ? file : path.dirname(file));
|
|
106
|
+
if (!owner)
|
|
107
|
+
continue;
|
|
108
|
+
// git speaks forward slashes on every platform.
|
|
109
|
+
const rel = (path.relative(owner, file) || ".").split(path.sep).join("/");
|
|
110
|
+
if (rel.startsWith(":"))
|
|
111
|
+
throw new BridgeError(400, "Invalid diff pathspec.");
|
|
112
|
+
const list = byRoot.get(owner) ?? [];
|
|
113
|
+
if (!list.includes(rel))
|
|
114
|
+
list.push(rel);
|
|
115
|
+
byRoot.set(owner, list);
|
|
116
|
+
}
|
|
117
|
+
const related = [];
|
|
118
|
+
for (const [owner, specs] of byRoot) {
|
|
119
|
+
if (related.length >= 8)
|
|
120
|
+
break;
|
|
121
|
+
const listed = owner === root ? files : await statusFiles(owner, specs);
|
|
122
|
+
const seen = new Set(listed.map(file => file.path));
|
|
123
|
+
for (const spec of specs) {
|
|
124
|
+
// Uncommitted changes under the path are already listed; otherwise show the commit.
|
|
125
|
+
if ([...seen].some(file => file === spec || spec === "." || file.startsWith(spec + "/")))
|
|
126
|
+
continue;
|
|
127
|
+
const section = await committed(owner, spec);
|
|
128
|
+
if (section) {
|
|
129
|
+
listed.push({ path: spec, status: " ", sections: [section] });
|
|
130
|
+
seen.add(spec);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (owner !== root && listed.length)
|
|
134
|
+
related.push({ root: owner, branch: (await git(owner, "branch", "--show-current")).trim(), files: listed });
|
|
135
|
+
}
|
|
136
|
+
return { branch, root, launchPath: cwd, files, ...(related.length ? { related } : {}) };
|
|
137
|
+
}
|
|
138
|
+
/** The HEAD file of the repository holding `cwd` (`.git/HEAD`, or a linked
|
|
139
|
+
* worktree's through its `.git` file), found without spawning git. */
|
|
140
|
+
export async function headFile(cwd) {
|
|
141
|
+
let directory = cwd;
|
|
142
|
+
for (let depth = 0; depth < 64; depth++) {
|
|
143
|
+
const dotGit = path.join(directory, ".git");
|
|
144
|
+
const info = await stat(dotGit).catch(() => undefined);
|
|
145
|
+
if (info?.isDirectory())
|
|
146
|
+
return path.join(dotGit, "HEAD");
|
|
147
|
+
if (info?.isFile()) {
|
|
148
|
+
const match = /^gitdir:\s*(.+)$/m.exec(await readFile(dotGit, "utf8").catch(() => ""));
|
|
149
|
+
return match ? path.join(path.resolve(directory, match[1].trim()), "HEAD") : undefined;
|
|
150
|
+
}
|
|
151
|
+
const parent = path.dirname(directory);
|
|
152
|
+
if (parent === directory)
|
|
153
|
+
return undefined;
|
|
154
|
+
directory = parent;
|
|
155
|
+
}
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
async function headState(cwd) {
|
|
159
|
+
const file = await headFile(cwd);
|
|
160
|
+
if (!file)
|
|
161
|
+
return undefined;
|
|
162
|
+
const text = await readFile(file, "utf8").catch(() => undefined);
|
|
163
|
+
return text === undefined ? undefined : `${file}\n${text}`;
|
|
164
|
+
}
|
|
165
|
+
/** How long a branch is reused outright, and the longest it is kept while
|
|
166
|
+
* the repository's HEAD file reads the same. */
|
|
167
|
+
const BRANCH_FRESH_MS = 10_000, BRANCH_MAX_MS = 300_000;
|
|
168
|
+
/** The pane's current branch for the chat header and the overview cards.
|
|
169
|
+
* Cached per directory: the status stream asks every 1.5s, the overview every
|
|
170
|
+
* 3s, and a branch rarely moves. Past ten seconds the cached branch is kept
|
|
171
|
+
* while HEAD's file and contents are unchanged (a checkout rewrites it), so
|
|
172
|
+
* `git branch` runs again only after a switch or after five minutes. */
|
|
173
|
+
const branches = new Map();
|
|
174
|
+
export async function repositoryBranch(cwd) {
|
|
175
|
+
const cached = branches.get(cwd);
|
|
176
|
+
const now = Date.now();
|
|
177
|
+
if (cached && now - cached.checked < BRANCH_FRESH_MS)
|
|
178
|
+
return cached.value;
|
|
179
|
+
const head = await headState(cwd);
|
|
180
|
+
if (cached && head !== undefined && head === cached.head && now - cached.at < BRANCH_MAX_MS) {
|
|
181
|
+
cached.checked = now;
|
|
182
|
+
return cached.value;
|
|
183
|
+
}
|
|
184
|
+
let value;
|
|
185
|
+
try {
|
|
186
|
+
countGit("branch");
|
|
187
|
+
const { stdout } = await exec("git", ["-C", cwd, "--no-pager", "branch", "--show-current"], {
|
|
188
|
+
timeout: 5_000, maxBuffer: 65_536, env: nonInteractiveGitEnv({ ...process.env, GIT_OPTIONAL_LOCKS: "0", GIT_CONFIG_NOSYSTEM: "1" }),
|
|
189
|
+
});
|
|
190
|
+
value = stdout.trim().slice(0, 200) || undefined;
|
|
191
|
+
}
|
|
192
|
+
catch {
|
|
193
|
+
value = undefined;
|
|
194
|
+
}
|
|
195
|
+
if (branches.size >= 64)
|
|
196
|
+
branches.delete(branches.keys().next().value);
|
|
197
|
+
branches.set(cwd, { at: Date.now(), checked: Date.now(), head, value });
|
|
198
|
+
return value;
|
|
199
|
+
}
|
|
200
|
+
const ENTITIES = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'", nbsp: " " };
|
|
201
|
+
/** Page titles arrive HTML-escaped ("Safety & Quality"); show them as text. */
|
|
202
|
+
function decodeEntities(text) {
|
|
203
|
+
return text.replace(/&(#x[0-9a-f]+|#\d+|[a-z]+);/gi, (match, entity) => {
|
|
204
|
+
if (entity[0] === "#") {
|
|
205
|
+
const code = entity[1]?.toLowerCase() === "x" ? Number.parseInt(entity.slice(2), 16) : Number(entity.slice(1));
|
|
206
|
+
return Number.isFinite(code) && code > 0 && code <= 0x10ffff ? String.fromCodePoint(code) : match;
|
|
207
|
+
}
|
|
208
|
+
return ENTITIES[entity.toLowerCase()] ?? match;
|
|
209
|
+
});
|
|
39
210
|
}
|
|
40
211
|
function probe(port, host) {
|
|
41
212
|
return new Promise(resolve => {
|
|
@@ -48,7 +219,7 @@ function probe(port, host) {
|
|
|
48
219
|
let body = "";
|
|
49
220
|
res.on("data", data => { body += data.toString(); if (body.length > 32_768)
|
|
50
221
|
res.destroy(); });
|
|
51
|
-
const end = () => finish(/<title[^>]*>([^<]{1,300})<\/title>/i.exec(body)?.[1]
|
|
222
|
+
const end = () => finish(decodeEntities(/<title[^>]*>([^<]{1,300})<\/title>/i.exec(body)?.[1] ?? "").trim() || `Web server on port ${port}`);
|
|
52
223
|
res.on("end", end);
|
|
53
224
|
res.on("close", end);
|
|
54
225
|
res.on("error", () => finish(null));
|
|
@@ -58,10 +229,20 @@ function probe(port, host) {
|
|
|
58
229
|
req.end();
|
|
59
230
|
});
|
|
60
231
|
}
|
|
61
|
-
|
|
62
|
-
|
|
232
|
+
const LISTEN_HOST = /^(\*|127\.0\.0\.1|localhost|0\.0\.0\.0|\[::\]|\[::1\]|::|::1)$/;
|
|
233
|
+
/** Linux ephemeral range starts here; ports below it are far more likely to be a
|
|
234
|
+
* dev server than a browser's devtools or IPC listener. */
|
|
235
|
+
const EPHEMERAL_PORT = 32_768;
|
|
236
|
+
function addListener(ports, address, port, processName, pid) {
|
|
237
|
+
if (!LISTEN_HOST.test(address) || !Number.isInteger(port) || port <= 0)
|
|
238
|
+
return;
|
|
239
|
+
const host = address.includes(":") ? "[::1]" : "127.0.0.1";
|
|
240
|
+
ports.set(`${host}:${port}`, { name: "", port, origin: `http://${host}:${port}`, process: processName, pid });
|
|
241
|
+
}
|
|
242
|
+
/** Listening TCP sockets owned by this user, via lsof (always present on macOS). */
|
|
243
|
+
async function listenersFromLsof(ports) {
|
|
244
|
+
const result = await exec(process.platform === "darwin" ? "/usr/sbin/lsof" : "lsof", ["-nP", "-a", "-u", userInfo().username, "-iTCP", "-sTCP:LISTEN", "-Fpcn"], { timeout: 4000, maxBuffer: 1_048_576 });
|
|
63
245
|
let pid, processName;
|
|
64
|
-
const ports = new Map();
|
|
65
246
|
for (const line of result.stdout.split("\n")) {
|
|
66
247
|
if (line.startsWith("p"))
|
|
67
248
|
pid = Number(line.slice(1));
|
|
@@ -69,14 +250,51 @@ export async function webServers() {
|
|
|
69
250
|
processName = line.slice(1);
|
|
70
251
|
if (!line.startsWith("n"))
|
|
71
252
|
continue;
|
|
72
|
-
const
|
|
73
|
-
if (
|
|
253
|
+
const split = line.lastIndexOf(":");
|
|
254
|
+
if (split < 0)
|
|
74
255
|
continue;
|
|
75
|
-
|
|
76
|
-
const host = match[1].includes(":") ? "[::1]" : "127.0.0.1";
|
|
77
|
-
ports.set(`${host}:${port}`, { name: "", port, origin: `http://${host}:${port}`, process: processName, pid });
|
|
256
|
+
addListener(ports, line.slice(1, split), Number(line.slice(split + 1)), processName, pid);
|
|
78
257
|
}
|
|
79
|
-
|
|
258
|
+
}
|
|
259
|
+
/** Listening TCP sockets via iproute2's ss, which every Linux ships even when
|
|
260
|
+
* lsof is absent (Arch/Omarchy, minimal containers). Lines look like
|
|
261
|
+
* `LISTEN 0 512 *:3000 *:* users:(("bun",pid=123,fd=24))`. */
|
|
262
|
+
async function listenersFromSs(ports) {
|
|
263
|
+
const result = await exec("ss", ["-ltnpH"], { timeout: 4000, maxBuffer: 1_048_576 });
|
|
264
|
+
for (const line of result.stdout.split("\n")) {
|
|
265
|
+
const columns = line.trim().split(/\s+/);
|
|
266
|
+
if (columns.length < 4)
|
|
267
|
+
continue;
|
|
268
|
+
const local = columns[3];
|
|
269
|
+
const split = local.lastIndexOf(":");
|
|
270
|
+
if (split < 0)
|
|
271
|
+
continue;
|
|
272
|
+
const owner = /users:\(\("([^"]*)",pid=(\d+)/.exec(line);
|
|
273
|
+
addListener(ports, local.slice(0, split), Number(local.slice(split + 1)), owner?.[1], owner ? Number(owner[2]) : undefined);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
export async function webServers() {
|
|
277
|
+
const ports = new Map();
|
|
278
|
+
// Prefer ss on Linux and lsof elsewhere; try the other if the first is missing.
|
|
279
|
+
const readers = process.platform === "linux" ? [listenersFromSs, listenersFromLsof] : [listenersFromLsof, listenersFromSs];
|
|
280
|
+
let lastError;
|
|
281
|
+
for (const reader of readers) {
|
|
282
|
+
try {
|
|
283
|
+
await reader(ports);
|
|
284
|
+
lastError = undefined;
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
catch (error) {
|
|
288
|
+
lastError = error;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
if (lastError && ports.size === 0)
|
|
292
|
+
throw new BridgeError(503, "No socket listing tool found: install lsof (or iproute2's ss on Linux).");
|
|
293
|
+
// Probe well-known ports first so a browser's dozens of ephemeral listeners
|
|
294
|
+
// cannot push a dev server on :3000 past the cap.
|
|
295
|
+
const candidates = [...ports.values()]
|
|
296
|
+
.sort((a, b) => Number(a.port >= EPHEMERAL_PORT) - Number(b.port >= EPHEMERAL_PORT) || a.port - b.port)
|
|
297
|
+
.slice(0, 64);
|
|
80
298
|
const found = [];
|
|
81
299
|
for (let i = 0; i < candidates.length; i += 8) {
|
|
82
300
|
await Promise.all(candidates.slice(i, i + 8).map(async (server) => {
|
|
@@ -87,3 +305,33 @@ export async function webServers() {
|
|
|
87
305
|
}
|
|
88
306
|
return found.sort((a, b) => a.port - b.port);
|
|
89
307
|
}
|
|
308
|
+
/** Launch only in real local directories under home or a locator candidate. */
|
|
309
|
+
export async function launchDirectory(raw, activity = [], located = []) {
|
|
310
|
+
if (typeof raw !== "string" || raw.length > 4096 || !path.isAbsolute(raw) || /[\x00-\x1f\x7f]/.test(raw))
|
|
311
|
+
throw new BridgeError(400, "Invalid workspace directory.");
|
|
312
|
+
let dir;
|
|
313
|
+
try {
|
|
314
|
+
dir = await realpath(raw);
|
|
315
|
+
if (!(await stat(dir)).isDirectory())
|
|
316
|
+
throw new Error();
|
|
317
|
+
}
|
|
318
|
+
catch {
|
|
319
|
+
throw new BridgeError(400, "Workspace directory does not exist.");
|
|
320
|
+
}
|
|
321
|
+
const home = await realpath(homeDir());
|
|
322
|
+
if (dir === home || dir.startsWith(home + path.sep))
|
|
323
|
+
return dir;
|
|
324
|
+
for (const candidate of located) {
|
|
325
|
+
const real = await realpath(candidate).catch(() => undefined);
|
|
326
|
+
if (real && (dir === real || dir.startsWith(real + path.sep)))
|
|
327
|
+
return dir;
|
|
328
|
+
}
|
|
329
|
+
const names = [...new Set(dir.split(path.sep).filter(name => /^[a-z0-9][a-z0-9-]{0,99}$/.test(name)))];
|
|
330
|
+
for (const name of names) {
|
|
331
|
+
for (const candidate of await locateProject(name, activity)) {
|
|
332
|
+
if (dir === candidate.directory || dir.startsWith(candidate.directory + path.sep))
|
|
333
|
+
return dir;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
throw new BridgeError(403, "Workspace directory must be under home or a located project.");
|
|
337
|
+
}
|
|
@@ -3,12 +3,16 @@ export declare const PROTOCOL = 1;
|
|
|
3
3
|
export declare const MAX_FRAME: number;
|
|
4
4
|
export declare const id: z.ZodString;
|
|
5
5
|
export declare const serverName: z.ZodString;
|
|
6
|
+
export declare const computerName: z.ZodString;
|
|
6
7
|
export declare const provider: z.ZodEnum<{
|
|
7
8
|
claude: "claude";
|
|
8
9
|
codex: "codex";
|
|
9
10
|
copilot: "copilot";
|
|
11
|
+
opencode: "opencode";
|
|
12
|
+
phren: "phren";
|
|
10
13
|
}>;
|
|
11
14
|
export type Provider = z.infer<typeof provider>;
|
|
15
|
+
export declare const sessionId: z.ZodUnion<readonly [z.ZodString, z.ZodString]>;
|
|
12
16
|
export declare const targetSchema: z.ZodObject<{
|
|
13
17
|
server: z.ZodString;
|
|
14
18
|
workspace: z.ZodString;
|
|
@@ -18,18 +22,47 @@ export declare const targetSchema: z.ZodObject<{
|
|
|
18
22
|
claude: "claude";
|
|
19
23
|
codex: "codex";
|
|
20
24
|
copilot: "copilot";
|
|
25
|
+
opencode: "opencode";
|
|
26
|
+
phren: "phren";
|
|
21
27
|
}>;
|
|
22
|
-
session: z.ZodString
|
|
28
|
+
session: z.ZodUnion<readonly [z.ZodString, z.ZodString]>;
|
|
23
29
|
}, z.core.$strip>;
|
|
24
30
|
export type Target = z.infer<typeof targetSchema>;
|
|
31
|
+
export declare const startingTargetSchema: z.ZodObject<{
|
|
32
|
+
server: z.ZodString;
|
|
33
|
+
workspace: z.ZodString;
|
|
34
|
+
tab: z.ZodString;
|
|
35
|
+
pane: z.ZodString;
|
|
36
|
+
source: z.ZodEnum<{
|
|
37
|
+
claude: "claude";
|
|
38
|
+
codex: "codex";
|
|
39
|
+
copilot: "copilot";
|
|
40
|
+
opencode: "opencode";
|
|
41
|
+
phren: "phren";
|
|
42
|
+
}>;
|
|
43
|
+
starting: z.ZodLiteral<true>;
|
|
44
|
+
startingToken: z.ZodString;
|
|
45
|
+
}, z.core.$strip>;
|
|
46
|
+
export type StartingTarget = z.infer<typeof startingTargetSchema>;
|
|
25
47
|
export type Json = Record<string, unknown>;
|
|
48
|
+
/** `value` when it is a plain object, else an empty one. */
|
|
26
49
|
export declare function object(value: unknown): Json;
|
|
27
50
|
export declare function objects(value: unknown): Json[];
|
|
28
51
|
export declare function bridgeRoot(): string;
|
|
29
52
|
export declare function socketPath(): string;
|
|
30
53
|
export declare class BridgeError extends Error {
|
|
31
54
|
status: number;
|
|
32
|
-
|
|
55
|
+
details?: Json | undefined;
|
|
56
|
+
constructor(status: number, message: string, details?: Json | undefined);
|
|
33
57
|
}
|
|
34
58
|
export declare const requestID: () => `${string}-${string}-${string}-${string}-${string}`;
|
|
59
|
+
/**
|
|
60
|
+
* Write a file atomically: serialize to a fresh temp name, then rename it over
|
|
61
|
+
* the target so a reader never observes a partial file. `value` may be
|
|
62
|
+
* pre-serialized text or any JSON-serializable value. The default mode is 0600
|
|
63
|
+
* because every caller writes per-user state.
|
|
64
|
+
*/
|
|
65
|
+
export declare function atomic(file: string, value: unknown, mode?: number): Promise<void>;
|
|
66
|
+
/** {@link atomic}, first creating the file's directory (0700 where it is new). */
|
|
67
|
+
export declare function atomicInPrivateDir(file: string, value: unknown, mode?: number): Promise<void>;
|
|
35
68
|
export declare function targetFromURL(url: URL): Target;
|
package/dist/bridge/protocol.js
CHANGED
|
@@ -1,34 +1,69 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { mkdir, rename, unlink, writeFile } from "node:fs/promises";
|
|
2
3
|
import { homedir } from "node:os";
|
|
3
4
|
import path from "node:path";
|
|
4
5
|
import { z } from "zod";
|
|
6
|
+
import { isRecord } from "../phren-core.js";
|
|
5
7
|
export const PROTOCOL = 1;
|
|
6
8
|
export const MAX_FRAME = 8 * 1024 * 1024;
|
|
7
9
|
export const id = z.string().regex(/^[A-Za-z0-9_%:.-]{1,200}$/);
|
|
8
|
-
export const serverName = z.string().regex(/^[A-Za-z0-9_.-]{
|
|
9
|
-
export const
|
|
10
|
+
export const serverName = z.string().regex(/^(?!\.\.?$)[A-Za-z0-9_][A-Za-z0-9_.-]{0,99}$/);
|
|
11
|
+
export const computerName = serverName.refine(name => name !== "anywhere", "anywhere is reserved for scheduling");
|
|
12
|
+
export const provider = z.enum(["codex", "claude", "copilot", "phren", "opencode"]);
|
|
13
|
+
export const sessionId = z.union([
|
|
14
|
+
z.string().uuid(),
|
|
15
|
+
z.string().regex(/^ses_[0-9A-Za-z]{1,64}$/),
|
|
16
|
+
]);
|
|
10
17
|
export const targetSchema = z.object({
|
|
11
18
|
server: serverName,
|
|
12
19
|
workspace: id,
|
|
13
20
|
tab: id,
|
|
14
21
|
pane: id,
|
|
15
22
|
source: provider,
|
|
16
|
-
session:
|
|
23
|
+
session: sessionId,
|
|
17
24
|
});
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
25
|
+
// Only /v1/prompt accepts a transcript-less target. All transcript, image,
|
|
26
|
+
// approval and diff routes retain the session-bound schema above.
|
|
27
|
+
export const startingTargetSchema = targetSchema.omit({ session: true }).extend({
|
|
28
|
+
starting: z.literal(true), startingToken: z.string().regex(/^[a-f0-9]{64}$/),
|
|
29
|
+
});
|
|
30
|
+
/** `value` when it is a plain object, else an empty one. */
|
|
31
|
+
export function object(value) { return isRecord(value) ? value : {}; }
|
|
21
32
|
export function objects(value) { return Array.isArray(value) ? value.map(object) : []; }
|
|
22
33
|
export function bridgeRoot() { return process.env.PHREN_BRIDGE_HOME || path.join(homedir(), ".local/share/phren/bridge"); }
|
|
23
34
|
export function socketPath() { return path.join(bridgeRoot(), "hook.sock"); }
|
|
24
35
|
export class BridgeError extends Error {
|
|
25
36
|
status;
|
|
26
|
-
|
|
37
|
+
details;
|
|
38
|
+
constructor(status, message, details) {
|
|
27
39
|
super(message);
|
|
28
40
|
this.status = status;
|
|
41
|
+
this.details = details;
|
|
29
42
|
}
|
|
30
43
|
}
|
|
31
44
|
export const requestID = () => randomUUID();
|
|
45
|
+
/**
|
|
46
|
+
* Write a file atomically: serialize to a fresh temp name, then rename it over
|
|
47
|
+
* the target so a reader never observes a partial file. `value` may be
|
|
48
|
+
* pre-serialized text or any JSON-serializable value. The default mode is 0600
|
|
49
|
+
* because every caller writes per-user state.
|
|
50
|
+
*/
|
|
51
|
+
export async function atomic(file, value, mode = 0o600) {
|
|
52
|
+
const temporary = `${file}.${randomUUID()}.tmp`;
|
|
53
|
+
const text = typeof value === "string" ? value : JSON.stringify(value);
|
|
54
|
+
await writeFile(temporary, text, { mode, flag: "wx" });
|
|
55
|
+
try {
|
|
56
|
+
await rename(temporary, file);
|
|
57
|
+
}
|
|
58
|
+
finally {
|
|
59
|
+
await unlink(temporary).catch(() => { });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** {@link atomic}, first creating the file's directory (0700 where it is new). */
|
|
63
|
+
export async function atomicInPrivateDir(file, value, mode) {
|
|
64
|
+
await mkdir(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
65
|
+
await atomic(file, value, mode);
|
|
66
|
+
}
|
|
32
67
|
export function targetFromURL(url) {
|
|
33
68
|
return targetSchema.parse(Object.fromEntries(["server", "workspace", "tab", "pane", "source", "session"].map(k => [k, url.searchParams.get(k)])));
|
|
34
69
|
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
declare const deviceSchema: z.ZodObject<{
|
|
3
|
+
deviceID: z.ZodString;
|
|
4
|
+
hostID: z.ZodString;
|
|
5
|
+
token: z.ZodString;
|
|
6
|
+
environment: z.ZodEnum<{
|
|
7
|
+
development: "development";
|
|
8
|
+
production: "production";
|
|
9
|
+
}>;
|
|
10
|
+
kinds: z.ZodDefault<z.ZodArray<z.ZodEnum<{
|
|
11
|
+
approval: "approval";
|
|
12
|
+
scheduleBlocked: "scheduleBlocked";
|
|
13
|
+
scheduleFailed: "scheduleFailed";
|
|
14
|
+
scheduleFinished: "scheduleFinished";
|
|
15
|
+
scheduleStarted: "scheduleStarted";
|
|
16
|
+
}>>>;
|
|
17
|
+
}, z.core.$strip>;
|
|
18
|
+
export type PushDevice = z.infer<typeof deviceSchema>;
|
|
19
|
+
export interface ApprovalPush {
|
|
20
|
+
binding: string;
|
|
21
|
+
provider: string;
|
|
22
|
+
question: boolean;
|
|
23
|
+
expiresAt: string;
|
|
24
|
+
/** What the request is, when the provider names it (an opencode permission
|
|
25
|
+
* ask): shown in the alert so the phone can answer without opening Phren. */
|
|
26
|
+
title?: string;
|
|
27
|
+
message?: string;
|
|
28
|
+
}
|
|
29
|
+
export interface FanoutBlockedPush {
|
|
30
|
+
job: string;
|
|
31
|
+
label: string;
|
|
32
|
+
provider: string;
|
|
33
|
+
reason: string;
|
|
34
|
+
}
|
|
35
|
+
export type SchedulePushKind = "scheduleStarted" | "scheduleFinished" | "scheduleFailed" | "scheduleBlocked";
|
|
36
|
+
export interface SchedulePush {
|
|
37
|
+
kind: SchedulePushKind;
|
|
38
|
+
scheduleId: string;
|
|
39
|
+
project: string;
|
|
40
|
+
name: string;
|
|
41
|
+
computer: string;
|
|
42
|
+
runId: string;
|
|
43
|
+
status: "running" | "finished" | "needs-you" | "failed" | "blocked";
|
|
44
|
+
reason?: string;
|
|
45
|
+
route?: string;
|
|
46
|
+
}
|
|
47
|
+
export interface SchedulePushResult {
|
|
48
|
+
notified: boolean;
|
|
49
|
+
reason?: string;
|
|
50
|
+
}
|
|
51
|
+
export declare function scheduleCollapseId(kind: SchedulePushKind, runId: string): string;
|
|
52
|
+
export declare function approvalPushPayload(value: ApprovalPush, host?: string): Record<string, unknown>;
|
|
53
|
+
export declare function fanoutBlockedPushPayload(value: FanoutBlockedPush): Record<string, unknown>;
|
|
54
|
+
export declare function schedulePushPayload(value: SchedulePush): Record<string, unknown>;
|
|
55
|
+
/** What `phren bridge doctor` prints when the Hook has no APNs sender. */
|
|
56
|
+
export declare function apnsSetupSteps(configFile?: string): string;
|
|
57
|
+
/** The capability a phone reads: only a Hook with a loaded APNs sender offers push. */
|
|
58
|
+
export declare function approvalPushCapability(status: {
|
|
59
|
+
configured: boolean;
|
|
60
|
+
}): "direct-apns" | undefined;
|
|
61
|
+
export declare function upsertPushDevice(devices: PushDevice[], value: unknown): PushDevice[];
|
|
62
|
+
/** Direct Hook -> APNs delivery. Apple receives only a short-lived opaque
|
|
63
|
+
* binding; conversation identity, action id, command and SSH key stay local. */
|
|
64
|
+
export declare class ApprovalPushService {
|
|
65
|
+
private devices;
|
|
66
|
+
private sender?;
|
|
67
|
+
private readonly devicesFile;
|
|
68
|
+
start(): Promise<void>;
|
|
69
|
+
get available(): boolean;
|
|
70
|
+
get status(): {
|
|
71
|
+
supported: boolean;
|
|
72
|
+
configured: boolean;
|
|
73
|
+
devices: number;
|
|
74
|
+
};
|
|
75
|
+
register(value: unknown): Promise<void>;
|
|
76
|
+
notify(value: ApprovalPush): Promise<boolean>;
|
|
77
|
+
/** A headless worker whose permission the plugin refused. Approval-registered
|
|
78
|
+
* phones already accept agent alerts; the payload carries the reason so the
|
|
79
|
+
* notification is actionable on its own. */
|
|
80
|
+
notifyFanoutBlocked(value: FanoutBlockedPush): Promise<boolean>;
|
|
81
|
+
notifySchedule(value: SchedulePush): Promise<SchedulePushResult>;
|
|
82
|
+
}
|
|
83
|
+
export {};
|