@zergai/ztc 0.26.2-dev.202611210149 → 0.26.4-dev.202611231953
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 +590 -28
- package/bin/zmux-darwin-arm64 +0 -0
- package/bin/zmux-linux-x64 +0 -0
- package/bin/ztc-audio-darwin-arm64 +0 -0
- package/bin/ztc-capture-darwin-arm64 +0 -0
- package/bin/ztc-tui-darwin-arm64 +0 -0
- package/bin/ztc-tui-linux-x64 +0 -0
- package/bin/zterm-darwin-arm64 +2 -2
- package/dist/App.js +18 -1
- package/dist/agent/agent.d.ts +25 -1
- package/dist/agent/agent.js +26 -1
- package/dist/agent/attached_images.d.ts +58 -0
- package/dist/agent/attached_images.js +145 -0
- package/dist/agent/backends/anthropic.d.ts +14 -0
- package/dist/agent/backends/anthropic.js +49 -2
- package/dist/agent/backends/codex_app_server.d.ts +3 -1
- package/dist/agent/backends/codex_app_server.js +9 -6
- package/dist/agent/backends/codex_auth.js +3 -0
- package/dist/agent/backends/gemini.js +3 -0
- package/dist/agent/backends/host_effort_policy.js +14 -2
- package/dist/agent/backends/http_error.d.ts +17 -0
- package/dist/agent/backends/http_error.js +50 -1
- package/dist/agent/backends/index.js +2 -1
- package/dist/agent/backends/model_redirect.d.ts +47 -0
- package/dist/agent/backends/model_redirect.js +97 -0
- package/dist/agent/backends/openai_compatible.js +5 -0
- package/dist/agent/backends/openai_responses.d.ts +9 -1
- package/dist/agent/backends/openai_responses.js +21 -12
- package/dist/agent/backends/prompt_cache.d.ts +8 -0
- package/dist/agent/backends/prompt_cache.js +10 -0
- package/dist/agent/backends/provider_ids.d.ts +1 -1
- package/dist/agent/backends/provider_ids.js +1 -1
- package/dist/agent/backends/types.d.ts +8 -0
- package/dist/agent/backends/zerg-api.js +5 -1
- package/dist/agent/backends/zrouter_stream.d.ts +75 -9
- package/dist/agent/backends/zrouter_stream.js +250 -49
- package/dist/agent/commands/config.js +51 -4
- package/dist/agent/commands/connect.js +20 -0
- package/dist/agent/commands/credential_status.js +5 -3
- package/dist/agent/commands/debug.js +3 -0
- package/dist/agent/commands/diff.js +9 -5
- package/dist/agent/commands/emulation.js +7 -1
- package/dist/agent/commands/inbox.js +4 -3
- package/dist/agent/commands/mcp.js +3 -0
- package/dist/agent/commands/models.js +20 -3
- package/dist/agent/commands/permissions.js +16 -2
- package/dist/agent/commands/provider_selection.js +13 -6
- package/dist/agent/commands/settings.js +15 -4
- package/dist/agent/commands/status.js +23 -1
- package/dist/agent/commands/types.d.ts +34 -3
- package/dist/agent/commands/update.d.ts +2 -1
- package/dist/agent/commands/update.js +8 -3
- package/dist/agent/commands/zerg.d.ts +6 -0
- package/dist/agent/commands/zerg.js +152 -28
- package/dist/agent/commands/ztc_pane.js +8 -5
- package/dist/agent/factory.js +22 -4
- package/dist/agent/request_encoder.js +11 -2
- package/dist/agent/runtime/command_jobs.js +16 -1
- package/dist/agent/runtime/command_panes.js +4 -1
- package/dist/agent/runtime/execution_context.d.ts +8 -1
- package/dist/agent/runtime/execution_context.js +8 -2
- package/dist/agent/runtime/policy.d.ts +66 -5
- package/dist/agent/runtime/policy.js +161 -33
- package/dist/agent/runtime/remote_broker_transport.d.ts +7 -0
- package/dist/agent/runtime/remote_broker_transport.js +33 -3
- package/dist/agent/runtime/remote_resource_errors.d.ts +7 -0
- package/dist/agent/runtime/remote_resource_errors.js +10 -0
- package/dist/agent/runtime/remote_resources.d.ts +1 -1
- package/dist/agent/runtime/remote_resources.js +7 -3
- package/dist/agent/runtime/source_verification.js +15 -12
- package/dist/agent/runtime/tool_runtime.js +165 -58
- package/dist/agent/runtime/verifier_capture.d.ts +1 -1
- package/dist/agent/runtime/verifier_capture.js +6 -0
- package/dist/agent/runtime/work_base.js +46 -14
- package/dist/agent/tools/adaptation.js +29 -12
- package/dist/agent/tools/apply_patch.js +62 -5
- package/dist/agent/tools/background.js +2 -0
- package/dist/agent/tools/benchmark_cpu.js +2 -0
- package/dist/agent/tools/bus_message.d.ts +4 -2
- package/dist/agent/tools/bus_message.js +21 -3
- package/dist/agent/tools/delegate.js +2 -0
- package/dist/agent/tools/file.d.ts +12 -1
- package/dist/agent/tools/file.js +92 -11
- package/dist/agent/tools/lsp.d.ts +12 -0
- package/dist/agent/tools/lsp.js +61 -5
- package/dist/agent/tools/pane.js +20 -45
- package/dist/agent/tools/project_context.js +5 -2
- package/dist/agent/tools/routine.js +20 -4
- package/dist/agent/tools/screenshot.d.ts +8 -0
- package/dist/agent/tools/screenshot.js +39 -25
- package/dist/agent/tools/search.js +31 -3
- package/dist/agent/tools/shell.js +7 -1
- package/dist/agent/tools/supervision.js +6 -0
- package/dist/agent/tools/types.d.ts +18 -0
- package/dist/agent/tools/zerg.js +15 -19
- package/dist/agent/tools/ztc_command.d.ts +15 -4
- package/dist/agent/tools/ztc_command.js +48 -34
- package/dist/app/agent_runner.js +8 -6
- package/dist/app/conversation_compaction.d.ts +6 -3
- package/dist/app/conversation_compaction.js +6 -3
- package/dist/app/tool_callbacks.js +1 -0
- package/dist/audio/sfx.js +6 -0
- package/dist/batch/worktree.js +22 -10
- package/dist/board/client.js +2 -1
- package/dist/cli_main.d.ts +1 -0
- package/dist/cli_main.js +141 -45
- package/dist/cli_ratatui.d.ts +49 -4
- package/dist/cli_ratatui.js +355 -79
- package/dist/conductor_branch.js +4 -1
- package/dist/config/host_credentials.d.ts +157 -0
- package/dist/config/host_credentials.js +589 -0
- package/dist/config/model_pin.d.ts +111 -0
- package/dist/config/model_pin.js +182 -0
- package/dist/config/own_state_files.d.ts +35 -0
- package/dist/config/own_state_files.js +161 -0
- package/dist/config/permission_pin.d.ts +148 -0
- package/dist/config/permission_pin.js +203 -0
- package/dist/config/permissions.d.ts +9 -2
- package/dist/config/permissions.js +14 -3
- package/dist/config/platform_login.d.ts +72 -0
- package/dist/config/platform_login.js +111 -0
- package/dist/config/profile_limits.d.ts +34 -0
- package/dist/config/profile_limits.js +66 -0
- package/dist/config/provider_api_key_env.js +3 -2
- package/dist/config/provider_models.d.ts +6 -1
- package/dist/config/provider_models.js +38 -11
- package/dist/config/repo_config.d.ts +22 -0
- package/dist/config/repo_config.js +77 -3
- package/dist/config/router_runtime_grant.d.ts +53 -0
- package/dist/config/router_runtime_grant.js +131 -0
- package/dist/config/settings_merge.d.ts +22 -0
- package/dist/config/settings_merge.js +75 -0
- package/dist/config/types.d.ts +8 -0
- package/dist/config.d.ts +334 -4
- package/dist/config.js +1024 -41
- package/dist/context/project_intelligence.d.ts +11 -2
- package/dist/context/project_intelligence.js +38 -15
- package/dist/desktop/daily/desktop_wiring.js +8 -6
- package/dist/desktop/daily/native_pane_launcher.js +2 -1
- package/dist/desktop/daily/native_tool.d.ts +1 -1
- package/dist/desktop/daily/native_tool.js +4 -3
- package/dist/desktop/daily/process_identity.js +10 -2
- package/dist/desktop/daily/source_baseline.js +4 -3
- package/dist/diagnostics/native_terminal_benchmark.js +3 -2
- package/dist/diagnostics/session_export.js +27 -14
- package/dist/embedded/anempa_runtime.js +3 -3
- package/dist/embedded/gameboy_runtime.js +5 -4
- package/dist/embedded/zergpod_runtime.js +5 -4
- package/dist/emulation/catalog.js +6 -1
- package/dist/emulation/tool_adapters.js +13 -3
- package/dist/environment/store.js +5 -4
- package/dist/extension_discovery.d.ts +12 -2
- package/dist/extension_discovery.js +52 -7
- package/dist/headless.d.ts +8 -0
- package/dist/headless.js +52 -13
- package/dist/headless_cli.js +16 -1
- package/dist/headless_workflow_trigger.js +1 -1
- package/dist/hooks/dispatcher.d.ts +8 -0
- package/dist/hooks/dispatcher.js +22 -2
- package/dist/hooks/lifecycle.d.ts +6 -0
- package/dist/hooks/lifecycle.js +1 -0
- package/dist/hooks/program_files.d.ts +10 -0
- package/dist/hooks/program_files.js +175 -0
- package/dist/hooks/runner.d.ts +11 -1
- package/dist/hooks/runner.js +112 -11
- package/dist/hooks/types.d.ts +2 -0
- package/dist/host_credentials_startup.d.ts +6 -0
- package/dist/host_credentials_startup.js +17 -0
- package/dist/instance_registry.d.ts +6 -2
- package/dist/instance_registry.js +9 -15
- package/dist/intake/format.d.ts +6 -0
- package/dist/intake/format.js +9 -0
- package/dist/intake/notifications.js +2 -1
- package/dist/lsp/manager.d.ts +1 -1
- package/dist/lsp/manager.js +4 -5
- package/dist/mcp/client.d.ts +2 -0
- package/dist/mcp/client.js +116 -18
- package/dist/mcp/registry.d.ts +12 -0
- package/dist/mcp/registry.js +15 -1
- package/dist/modes/handlers.js +14 -5
- package/dist/panes/agent_cli.d.ts +23 -1
- package/dist/panes/agent_cli.js +96 -1
- package/dist/panes/agent_panes.d.ts +27 -4
- package/dist/panes/agent_panes.js +73 -11
- package/dist/panes/editor_program.d.ts +8 -0
- package/dist/panes/editor_program.js +25 -0
- package/dist/panes/motsi.d.ts +11 -1
- package/dist/panes/motsi.js +14 -1
- package/dist/panes/pane_registry.d.ts +28 -1
- package/dist/panes/pane_registry.js +25 -0
- package/dist/panes/worker_route.js +5 -3
- package/dist/panes/ztc_launch.d.ts +8 -0
- package/dist/panes/ztc_launch.js +26 -1
- package/dist/profile_baseline.js +2 -0
- package/dist/router_auth.d.ts +8 -3
- package/dist/router_auth.js +34 -2
- package/dist/runtime_info.js +56 -38
- package/dist/session.js +12 -2
- package/dist/shannon/runtime.js +13 -1
- package/dist/simulation/domains/loader.d.ts +2 -1
- package/dist/simulation/domains/loader.js +19 -2
- package/dist/simulation/living/context.d.ts +8 -8
- package/dist/simulation/living/types.d.ts +2 -2
- package/dist/simulation/memory/embedding.d.ts +2 -1
- package/dist/simulation/memory/embedding.js +25 -6
- package/dist/terminal_attach.js +2 -1
- package/dist/types.d.ts +21 -0
- package/dist/ui/ratatui/headless_pane_manager.d.ts +12 -6
- package/dist/ui/ratatui/headless_pane_manager.js +65 -21
- package/dist/ui/ratatui/ipc_path.d.ts +41 -2
- package/dist/ui/ratatui/ipc_path.js +164 -5
- package/dist/ui/ratatui/pane_credential_broker.d.ts +17 -4
- package/dist/ui/ratatui/pane_credential_broker.js +23 -5
- package/dist/ui/ratatui/pane_relay.d.ts +74 -0
- package/dist/ui/ratatui/pane_relay.js +188 -13
- package/dist/ui/ratatui/process.d.ts +15 -1
- package/dist/ui/ratatui/process.js +129 -163
- package/dist/ui/ratatui/protocol.d.ts +11 -1
- package/dist/ui/ratatui/protocol.js +6 -0
- package/dist/ui/ratatui/ztc_launch_trust.d.ts +87 -0
- package/dist/ui/ratatui/ztc_launch_trust.js +249 -0
- package/dist/utils/admitted_open.d.ts +40 -0
- package/dist/utils/admitted_open.js +111 -0
- package/dist/utils/clipboard.js +2 -1
- package/dist/utils/computer_native.js +12 -56
- package/dist/utils/dictation.js +15 -12
- package/dist/utils/dictation_native.js +17 -60
- package/dist/utils/git_probe_options.d.ts +28 -0
- package/dist/utils/git_probe_options.js +30 -0
- package/dist/utils/git_program_files.d.ts +145 -0
- package/dist/utils/git_program_files.js +1282 -0
- package/dist/utils/git_repository_programs.d.ts +13 -0
- package/dist/utils/git_repository_programs.js +38 -0
- package/dist/utils/host_shell.js +2 -1
- package/dist/utils/idle_compaction.js +8 -1
- package/dist/utils/image_fit.d.ts +4 -1
- package/dist/utils/image_fit.js +6 -2
- package/dist/utils/image_preview.js +3 -2
- package/dist/utils/mirror_media.d.ts +22 -4
- package/dist/utils/mirror_media.js +87 -86
- package/dist/utils/ml_native.js +11 -56
- package/dist/utils/native_install.d.ts +55 -0
- package/dist/utils/native_install.js +107 -0
- package/dist/utils/open_url.js +2 -0
- package/dist/utils/process_end.d.ts +16 -0
- package/dist/utils/process_end.js +73 -0
- package/dist/utils/router_catalog.js +9 -2
- package/dist/utils/sandbox.d.ts +64 -1
- package/dist/utils/sandbox.js +397 -89
- package/dist/utils/shell.js +31 -12
- package/dist/utils/subprocess_env.d.ts +218 -2
- package/dist/utils/subprocess_env.js +429 -11
- package/dist/utils/system_ps.d.ts +5 -0
- package/dist/utils/system_ps.js +30 -0
- package/dist/utils/tts.js +5 -0
- package/dist/utils/tts_native.js +13 -60
- package/dist/utils/zlicky_overlay.js +17 -50
- package/dist/utils/ztc_own_state.d.ts +58 -0
- package/dist/utils/ztc_own_state.js +236 -0
- package/dist/web/mirror_authenticator.d.ts +67 -6
- package/dist/web/mirror_authenticator.js +131 -22
- package/dist/web/mirror_stream.d.ts +54 -10
- package/dist/web/mirror_stream.js +379 -92
- package/dist/web/observer_query_redaction.js +53 -13
- package/dist/web/observer_secret_redaction.d.ts +59 -0
- package/dist/web/observer_secret_redaction.js +714 -0
- package/dist/web/pane_credentials.d.ts +20 -2
- package/dist/web/pane_credentials.js +58 -5
- package/dist/worker/executor.js +3 -1
- package/dist/workspace_collision.js +19 -15
- package/dist/zerg/local_zerg_launches.d.ts +27 -0
- package/dist/zerg/local_zerg_launches.js +198 -0
- package/dist/zlm-embed.mjs +6257 -2791
- package/npm-shrinkwrap.json +1 -1
- package/package.json +42 -15
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which launches a pane relay's owner starts as ZTC panes, which are given its Router runtime grant
|
|
3
|
+
* (hostRouterGrantForZtcChild): those it decides are ZTC's own, never because a child of the relay says so.
|
|
4
|
+
*
|
|
5
|
+
* Every program in a pane the main ZTC runs can reach the relay's socket (relay_policy.ts), and a pane's type is only
|
|
6
|
+
* what its sender wrote. A create_pane or replace_pane that arrives through the relay as a ZTC pane (pane_type ztc or
|
|
7
|
+
* colony_ztc) therefore starts as a plain process pane, with a user subprocess's environment, unless it carries a launch
|
|
8
|
+
* nonce: one the relay issued, to that connection, a moment before, for that exact command and those exact arguments,
|
|
9
|
+
* and only for a launch that runs ZTC from the install this process runs from (ownZtcLaunch). A nonce is good once, and
|
|
10
|
+
* what it starts is the install's own program as this process found it, never the paths the client named, which it
|
|
11
|
+
* can point elsewhere once the nonce is issued, nor a motsi the client named.
|
|
12
|
+
* A ZTC pane's own pane tools ask for one as they open a ZTC pane through the relay (PaneRelayClient), as do the
|
|
13
|
+
* workspace tools of a Claude Code or Codex pane; ZTC's own launches that reach the renderer through its own transport
|
|
14
|
+
* (its agent's create_ztc_pane, the person's splits and the colony's cells) never pass through the relay.
|
|
15
|
+
*/
|
|
16
|
+
import { randomBytes } from 'node:crypto';
|
|
17
|
+
import { existsSync, realpathSync } from 'node:fs';
|
|
18
|
+
import { dirname, isAbsolute, join, resolve } from 'node:path';
|
|
19
|
+
import { PERMISSION_PIN_ENV } from '../../config/permission_pin.js';
|
|
20
|
+
import { ROUTER_RUNTIME_GRANT_ENV } from '../../config/router_runtime_grant.js';
|
|
21
|
+
import { loadDesktopRuntime } from '../../desktop/runtime.js';
|
|
22
|
+
import { ZTC_EXTENSION_DISCOVERY_ENV } from '../../extension_discovery.js';
|
|
23
|
+
import { MOTSI_PANE_ENV } from '../../panes/motsi.js';
|
|
24
|
+
import { hostRouterGrantBlank, hostRouterGrantDiscovery } from '../../utils/subprocess_env.js';
|
|
25
|
+
import { PANE_CREDENTIAL_ENV, WORKSPACE_MONITOR_CREDENTIAL_REQUEST, ZTC_PANE_CREDENTIAL_REQUEST } from '../../web/pane_credentials.js';
|
|
26
|
+
/** A ZTC entry point by its name, as a ZTC pane's command line names it. */
|
|
27
|
+
export const CHILD_ZTC_SCRIPT_RE = /(^|[\\/])cli_(main|ratatui)\.(js|ts)$/;
|
|
28
|
+
const PROCESS_START_CWD = process.cwd();
|
|
29
|
+
/**
|
|
30
|
+
* The entry point a ZTC pane this process opens runs: the host's ZTC_CHILD_ENTRYPOINT (a colony cell's worktree ZTC),
|
|
31
|
+
* else this process's own, else a checkout's in the working directory, for a process not started from one.
|
|
32
|
+
*/
|
|
33
|
+
export function resolveChildZtcEntrypoint() {
|
|
34
|
+
const override = process.env.ZTC_CHILD_ENTRYPOINT?.trim();
|
|
35
|
+
if (override) {
|
|
36
|
+
const resolved = resolve(override);
|
|
37
|
+
if (existsSync(resolved)) {
|
|
38
|
+
return resolved;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const currentEntrypoint = process.argv[1] || '';
|
|
42
|
+
if (currentEntrypoint && CHILD_ZTC_SCRIPT_RE.test(currentEntrypoint)) {
|
|
43
|
+
if (isAbsolute(currentEntrypoint)) {
|
|
44
|
+
return currentEntrypoint;
|
|
45
|
+
}
|
|
46
|
+
const roots = [
|
|
47
|
+
PROCESS_START_CWD,
|
|
48
|
+
process.env.INIT_CWD,
|
|
49
|
+
process.env.PWD,
|
|
50
|
+
process.cwd(),
|
|
51
|
+
].filter((value) => !!value);
|
|
52
|
+
for (const root of roots) {
|
|
53
|
+
const candidate = resolve(root, currentEntrypoint);
|
|
54
|
+
if (existsSync(candidate)) {
|
|
55
|
+
return candidate;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const candidates = [
|
|
60
|
+
resolve(process.cwd(), 'src', 'cli_main.ts'),
|
|
61
|
+
resolve(process.cwd(), 'dist', 'cli_main.js'),
|
|
62
|
+
];
|
|
63
|
+
for (const candidate of candidates) {
|
|
64
|
+
if (existsSync(candidate)) {
|
|
65
|
+
return candidate;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
let desktopProgram;
|
|
71
|
+
/** The desktop app's renderer in its CLI mode, when this process runs from the app's bundle (desktop/runtime.ts). */
|
|
72
|
+
function desktopZtcProgram() {
|
|
73
|
+
const manifest = process.env.ZTC_DESKTOP_RUNTIME_MANIFEST;
|
|
74
|
+
if (desktopProgram !== undefined && desktopProgram.manifest === manifest)
|
|
75
|
+
return desktopProgram.program;
|
|
76
|
+
let program = null;
|
|
77
|
+
try {
|
|
78
|
+
const runtime = manifest ? loadDesktopRuntime() : null;
|
|
79
|
+
program = runtime ? { command: runtime.rendererPath, lead: ['--ztc-cli'] } : null;
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
program = null;
|
|
83
|
+
}
|
|
84
|
+
desktopProgram = { manifest, program };
|
|
85
|
+
return program;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The ZTC this process launches its own ZTC panes with (panes/ztc_launch.ts buildZtcPaneLaunchCommand): the entry point
|
|
89
|
+
* its panes run (resolveChildZtcEntrypoint) and the one it runs itself, each under the Node it runs on, or a checkout's
|
|
90
|
+
* sources under the tsx beside them (npx's where there is none), and the desktop app's renderer in its CLI mode when it
|
|
91
|
+
* runs from the app.
|
|
92
|
+
*/
|
|
93
|
+
export function ownZtcPrograms() {
|
|
94
|
+
const programs = [];
|
|
95
|
+
const own = process.argv[1] ? resolve(process.argv[1]) : null;
|
|
96
|
+
for (const entry of new Set([resolveChildZtcEntrypoint(), own].filter((value) => !!value))) {
|
|
97
|
+
if (entry.endsWith('.ts')) {
|
|
98
|
+
const tsx = join(dirname(dirname(entry)), 'node_modules', '.bin', process.platform === 'win32' ? 'tsx.cmd' : 'tsx');
|
|
99
|
+
programs.push(existsSync(tsx) ? { command: tsx, lead: [entry] } : { command: 'npx', lead: ['tsx', entry] });
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
programs.push({ command: process.execPath, lead: [entry] });
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const desktop = desktopZtcProgram();
|
|
106
|
+
if (desktop)
|
|
107
|
+
programs.push(desktop);
|
|
108
|
+
return programs;
|
|
109
|
+
}
|
|
110
|
+
/** Whether A and B name the same file: the same path once links are resolved, or the same bare program name. */
|
|
111
|
+
function sameProgram(a, b) {
|
|
112
|
+
if (a === b)
|
|
113
|
+
return true;
|
|
114
|
+
if (!isAbsolute(a) || !isAbsolute(b))
|
|
115
|
+
return false;
|
|
116
|
+
try {
|
|
117
|
+
return realpathSync(a) === realpathSync(b);
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** Whether COMMAND with ARGS is MotSi's `motsi run --title=… -- <program> …` (panes/motsi.ts throughMotsi). */
|
|
124
|
+
function isMotsiRun(command, args) {
|
|
125
|
+
return /(^|[\\/])motsi$/.test(command) && args[0] === 'run' && args[1]?.startsWith('--title=') === true && args[2] === '--'
|
|
126
|
+
&& typeof args[3] === 'string';
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The launch of ZTC from this process's install that COMMAND with ARGS asks for, or null when it asks for none: one of
|
|
130
|
+
* PROGRAMS (ownZtcPrograms) with its leading arguments, each named by the same path once links are resolved, directly
|
|
131
|
+
* or as MotSi runs it (`motsi run --title=… -- <program>`, panes/motsi.ts). What it returns names the program as this
|
|
132
|
+
* process found it, never the paths the client named, which the client could point at a program of its own once its
|
|
133
|
+
* nonce was issued; and it names no motsi, since the client's could be any program: whether a relayed ZTC pane runs
|
|
134
|
+
* inside MotSi is for the relay's owner to decide, with its own motsi (PaneRelayServer).
|
|
135
|
+
*/
|
|
136
|
+
export function ownZtcLaunch(command, args, programs = ownZtcPrograms()) {
|
|
137
|
+
const throughMotsi = isMotsiRun(command, args);
|
|
138
|
+
const program = throughMotsi ? args[3] : command;
|
|
139
|
+
const rest = throughMotsi ? args.slice(4) : args;
|
|
140
|
+
const own = programs.find(candidate => sameProgram(program, candidate.command)
|
|
141
|
+
&& candidate.lead.every((lead, index) => typeof rest[index] === 'string' && sameProgram(rest[index], lead)));
|
|
142
|
+
if (!own)
|
|
143
|
+
return null;
|
|
144
|
+
return { command: own.command, args: [...own.lead, ...rest.slice(own.lead.length)],
|
|
145
|
+
...(throughMotsi ? { motsiTitle: args[1].slice('--title='.length) } : {}) };
|
|
146
|
+
}
|
|
147
|
+
/** How long a launch nonce waits for the create_pane or replace_pane it was issued for. */
|
|
148
|
+
const LAUNCH_NONCE_MS = 30_000;
|
|
149
|
+
/**
|
|
150
|
+
* The launch nonces a pane relay issued: each for one connection (OWNER), one command and its arguments, good once and
|
|
151
|
+
* for LAUNCH_NONCE_MS, with the LAUNCH the relay starts on it. A nonce presented is spent whatever it is presented with.
|
|
152
|
+
*/
|
|
153
|
+
export class ZtcLaunchNonces {
|
|
154
|
+
now;
|
|
155
|
+
issued = new Map();
|
|
156
|
+
constructor(now = Date.now) {
|
|
157
|
+
this.now = now;
|
|
158
|
+
}
|
|
159
|
+
/** A nonce for OWNER to start COMMAND with ARGS as a ZTC pane, which starts LAUNCH. */
|
|
160
|
+
issue(owner, command, args, launch) {
|
|
161
|
+
this.sweep();
|
|
162
|
+
const nonce = randomBytes(24).toString('base64url');
|
|
163
|
+
this.issued.set(nonce, { owner, command, args: [...args], launch, expires: this.now() + LAUNCH_NONCE_MS });
|
|
164
|
+
return nonce;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* The launch NONCE was issued for, when it was issued to OWNER for COMMAND with exactly ARGS and has not expired; null
|
|
168
|
+
* otherwise. It is spent either way.
|
|
169
|
+
*/
|
|
170
|
+
redeem(nonce, owner, command, args) {
|
|
171
|
+
const issued = this.issued.get(nonce);
|
|
172
|
+
this.issued.delete(nonce);
|
|
173
|
+
if (!issued || issued.owner !== owner || issued.expires < this.now() || issued.command !== command)
|
|
174
|
+
return null;
|
|
175
|
+
const given = args ?? [];
|
|
176
|
+
return given.length === issued.args.length && given.every((arg, index) => arg === issued.args[index]) ? issued.launch : null;
|
|
177
|
+
}
|
|
178
|
+
/** Forget every nonce OWNER holds (its connection closed). */
|
|
179
|
+
forget(owner) {
|
|
180
|
+
for (const [nonce, issued] of this.issued)
|
|
181
|
+
if (issued.owner === owner)
|
|
182
|
+
this.issued.delete(nonce);
|
|
183
|
+
}
|
|
184
|
+
sweep() {
|
|
185
|
+
const now = this.now();
|
|
186
|
+
for (const [nonce, issued] of this.issued)
|
|
187
|
+
if (issued.expires < now)
|
|
188
|
+
this.issued.delete(nonce);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* What a ZTC pane a child opens through the relay keeps of the environment its command sends: the names ZTC's own pane
|
|
193
|
+
* tools send a ZTC pane (agent/tools/pane.ts childZtcPaneEnv and agentZtcPaneEnv), and only the values they send, and a
|
|
194
|
+
* request for the pane's own mirror credential, which the pane transport answers (pane_credential_broker.ts). Nothing
|
|
195
|
+
* else of it reaches the pane: not a program to preload (NODE_OPTIONS), not a PATH or another grant or Router of the
|
|
196
|
+
* child's choosing; the host's pin of the permission settings only as 1, which pins. The pane joins this relay
|
|
197
|
+
* (RELAY_PATH) when its command asks to join one. One the relay starts
|
|
198
|
+
* inside MotSi (THROUGH_MOTSI) has MotSi's terminal id blank (panes/motsi.ts MOTSI_PANE_ENV), as ZTC's own do. What this
|
|
199
|
+
* process gives one of its own ZTC panes of its grant goes in whatever the client sent: a blank for a grant that takes
|
|
200
|
+
* no effect, and explicit discovery with one that does (hostRouterGrantBlank, hostRouterGrantDiscovery), so a pane a
|
|
201
|
+
* renderer starts with this process's environment imports no plugin from where an agent writes.
|
|
202
|
+
*/
|
|
203
|
+
export function relayedZtcPaneEnv(env, relayPath, { throughMotsi = false } = {}) {
|
|
204
|
+
const kept = {};
|
|
205
|
+
for (const [name, value] of Object.entries(env ?? {})) {
|
|
206
|
+
if (typeof value !== 'string')
|
|
207
|
+
continue;
|
|
208
|
+
switch (name) {
|
|
209
|
+
case 'ZTC_PARENT_PID':
|
|
210
|
+
case 'ZTC_MIRROR_PORT':
|
|
211
|
+
if (/^\d{1,10}$/.test(value))
|
|
212
|
+
kept[name] = value;
|
|
213
|
+
break;
|
|
214
|
+
case 'ZTC_NAVIGATOR_GROUP':
|
|
215
|
+
kept[name] = value;
|
|
216
|
+
break;
|
|
217
|
+
// panes/initial_input.ts PANE_TASK_AS_MESSAGE_ENV: the pane's task is an agent's, a message.
|
|
218
|
+
case 'ZTC_PANE_TASK_AS_MESSAGE':
|
|
219
|
+
if (value === '1')
|
|
220
|
+
kept[name] = value;
|
|
221
|
+
break;
|
|
222
|
+
// A blank only: a grant that takes no effect where the pane is opened.
|
|
223
|
+
case ROUTER_RUNTIME_GRANT_ENV:
|
|
224
|
+
if (value === '')
|
|
225
|
+
kept[name] = value;
|
|
226
|
+
break;
|
|
227
|
+
case ZTC_EXTENSION_DISCOVERY_ENV:
|
|
228
|
+
if (value === 'explicit')
|
|
229
|
+
kept[name] = value;
|
|
230
|
+
break;
|
|
231
|
+
// The host's pin of the permission settings, which a pinned pane passes on so the new one pins itself: 1 only,
|
|
232
|
+
// which can only pin (config/permission_pin.ts).
|
|
233
|
+
case PERMISSION_PIN_ENV:
|
|
234
|
+
if (value === '1')
|
|
235
|
+
kept[name] = value;
|
|
236
|
+
break;
|
|
237
|
+
case PANE_CREDENTIAL_ENV:
|
|
238
|
+
if (value === ZTC_PANE_CREDENTIAL_REQUEST || value === WORKSPACE_MONITOR_CREDENTIAL_REQUEST)
|
|
239
|
+
kept[name] = value;
|
|
240
|
+
break;
|
|
241
|
+
case 'ZTC_PANE_RELAY':
|
|
242
|
+
kept[name] = relayPath;
|
|
243
|
+
break;
|
|
244
|
+
default:
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return { ...kept, ...hostRouterGrantBlank(), ...hostRouterGrantDiscovery(), ...(throughMotsi ? MOTSI_PANE_ENV : {}) };
|
|
249
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type Stats } from 'node:fs';
|
|
2
|
+
/** The policy's admission of a path, as the tool runtime gives a tool (ToolContext.admitReads, admitWrites). */
|
|
3
|
+
export interface OpenAdmission {
|
|
4
|
+
/** Throws, naming the rule, when the policy refuses to read PATH (absolute) now. */
|
|
5
|
+
read(path: string): void;
|
|
6
|
+
/** Throws, naming the rule, when the policy refuses to write PATH (absolute) now. */
|
|
7
|
+
write(path: string): void;
|
|
8
|
+
/**
|
|
9
|
+
* Rejects, naming the rule, when the policy refuses the file opened for PATH for the file it is, whatever it is named:
|
|
10
|
+
* one it refuses under another name, which a hard link gives a second one (utils/ztc_own_state.ts). FILE is what
|
|
11
|
+
* fstat says of the open descriptor; nothing has been read from it yet.
|
|
12
|
+
*/
|
|
13
|
+
identity(path: string, file: Stats): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The bytes of the file PATH names, read only from the file the policy admitted: PATH's real path is admitted (for
|
|
17
|
+
* reading, or with ADMIT as a write tool admits what it reads of its own target), opened without following a link and
|
|
18
|
+
* without blocking, and must be a regular file, the one that real path names after the open, which PATH must still
|
|
19
|
+
* resolve to, and one the policy admits for what it is (OpenAdmission.identity). A file larger than MAXBYTES is
|
|
20
|
+
* refused before it is read.
|
|
21
|
+
*/
|
|
22
|
+
export declare function readAdmittedFile(path: string, admission: OpenAdmission, options?: {
|
|
23
|
+
maxBytes?: number;
|
|
24
|
+
tooLarge?: (size: number) => Error;
|
|
25
|
+
admit?: 'read' | 'write';
|
|
26
|
+
}): Promise<{
|
|
27
|
+
data: Buffer;
|
|
28
|
+
size: number;
|
|
29
|
+
real: string;
|
|
30
|
+
}>;
|
|
31
|
+
/** PATH with its folder as it really is now, and its last part as named (which may itself be a link). */
|
|
32
|
+
export declare function realFolderPath(path: string): Promise<string>;
|
|
33
|
+
/**
|
|
34
|
+
* Write DATA to PATH atomically, as the file the policy admitted: in the folder PATH's parent really is, under PATH's
|
|
35
|
+
* name, admitted for writing there. The temporary file is made exclusively in that folder without following a link,
|
|
36
|
+
* must still be in it when it is renamed into place (a rename through a folder swapped since finds no such file), and
|
|
37
|
+
* the file in place afterwards must be it. The mode of the file it replaces is kept, as writeFileAtomic keeps it.
|
|
38
|
+
* Returns the path written.
|
|
39
|
+
*/
|
|
40
|
+
export declare function writeAdmittedFileAtomic(path: string, data: string | Uint8Array, admission: OpenAdmission): Promise<string>;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Opening exactly the file a permission policy admitted. The tool runtime admits a path before a file tool runs, but a
|
|
3
|
+
* link in that path can lead somewhere else by the time the tool opens it: a workspace link flipped to a file outside
|
|
4
|
+
* the allowed roots between the check and the open had read_file return that file and write_file replace it. Under a
|
|
5
|
+
* profile that limits the agent the file tools therefore open through these helpers, which check after the open, as
|
|
6
|
+
* ztc-web-client's /file-content does: the real path is admitted, opened without following a link in its last part,
|
|
7
|
+
* the descriptor must be that path's file (dev, ino), and the path must still resolve there afterwards. Node has no
|
|
8
|
+
* openat or O_RESOLVE_BENEATH, so a folder swapped exactly around the open and back before both checks is narrowed, not
|
|
9
|
+
* closed: whatever was opened is then refused, never returned or written.
|
|
10
|
+
*/
|
|
11
|
+
import { randomBytes } from 'node:crypto';
|
|
12
|
+
import { constants } from 'node:fs';
|
|
13
|
+
import { lstat, open, realpath, rename, stat, unlink } from 'node:fs/promises';
|
|
14
|
+
import { basename, dirname, join } from 'node:path';
|
|
15
|
+
const NOFOLLOW = constants.O_NOFOLLOW ?? 0;
|
|
16
|
+
const NONBLOCK = constants.O_NONBLOCK ?? 0;
|
|
17
|
+
function changed(path, action) {
|
|
18
|
+
return new Error(`${path} changed while ZTC opened it (a link in it now leads elsewhere); nothing was ${action}`);
|
|
19
|
+
}
|
|
20
|
+
function sameFile(a, b) {
|
|
21
|
+
return a !== null && b !== null && a.dev === b.dev && a.ino === b.ino;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The bytes of the file PATH names, read only from the file the policy admitted: PATH's real path is admitted (for
|
|
25
|
+
* reading, or with ADMIT as a write tool admits what it reads of its own target), opened without following a link and
|
|
26
|
+
* without blocking, and must be a regular file, the one that real path names after the open, which PATH must still
|
|
27
|
+
* resolve to, and one the policy admits for what it is (OpenAdmission.identity). A file larger than MAXBYTES is
|
|
28
|
+
* refused before it is read.
|
|
29
|
+
*/
|
|
30
|
+
export async function readAdmittedFile(path, admission, options = {}) {
|
|
31
|
+
const real = await realpath(path);
|
|
32
|
+
if (options.admit === 'write')
|
|
33
|
+
admission.write(real);
|
|
34
|
+
else
|
|
35
|
+
admission.read(real);
|
|
36
|
+
let handle;
|
|
37
|
+
try {
|
|
38
|
+
handle = await open(real, constants.O_RDONLY | NOFOLLOW | NONBLOCK);
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
if (error.code === 'ELOOP')
|
|
42
|
+
throw changed(path, 'read');
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
const opened = await handle.stat();
|
|
47
|
+
const [again, there] = await Promise.all([realpath(path).catch(() => null), stat(real).catch(() => null)]);
|
|
48
|
+
if (again !== real || !sameFile(opened, there))
|
|
49
|
+
throw changed(path, 'read');
|
|
50
|
+
if (!opened.isFile())
|
|
51
|
+
throw new Error(`${path} is not a regular file, so ZTC does not read it here`);
|
|
52
|
+
await admission.identity(path, opened);
|
|
53
|
+
if (options.maxBytes !== undefined && opened.size > options.maxBytes) {
|
|
54
|
+
throw options.tooLarge?.(opened.size) ?? new Error(`${path} is larger than ${options.maxBytes} bytes`);
|
|
55
|
+
}
|
|
56
|
+
return { data: await handle.readFile(), size: opened.size, real };
|
|
57
|
+
}
|
|
58
|
+
finally {
|
|
59
|
+
await handle.close();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** PATH with its folder as it really is now, and its last part as named (which may itself be a link). */
|
|
63
|
+
export async function realFolderPath(path) {
|
|
64
|
+
return join(await realpath(dirname(path)), basename(path));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Write DATA to PATH atomically, as the file the policy admitted: in the folder PATH's parent really is, under PATH's
|
|
68
|
+
* name, admitted for writing there. The temporary file is made exclusively in that folder without following a link,
|
|
69
|
+
* must still be in it when it is renamed into place (a rename through a folder swapped since finds no such file), and
|
|
70
|
+
* the file in place afterwards must be it. The mode of the file it replaces is kept, as writeFileAtomic keeps it.
|
|
71
|
+
* Returns the path written.
|
|
72
|
+
*/
|
|
73
|
+
export async function writeAdmittedFileAtomic(path, data, admission) {
|
|
74
|
+
const folder = await realpath(dirname(path));
|
|
75
|
+
const target = join(folder, basename(path));
|
|
76
|
+
admission.write(target);
|
|
77
|
+
const existing = await stat(target).catch(() => null);
|
|
78
|
+
const temporary = join(folder, `${basename(path)}.tmp.${process.pid}.${randomBytes(4).toString('hex')}`);
|
|
79
|
+
// As writeFileAtomic makes its temporary file: the umask's mode, then the replaced file's.
|
|
80
|
+
const handle = await open(temporary, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | NOFOLLOW, 0o666);
|
|
81
|
+
let created;
|
|
82
|
+
try {
|
|
83
|
+
await handle.writeFile(data);
|
|
84
|
+
if (existing)
|
|
85
|
+
await handle.chmod(existing.mode & 0o777);
|
|
86
|
+
created = await handle.stat();
|
|
87
|
+
}
|
|
88
|
+
finally {
|
|
89
|
+
await handle.close();
|
|
90
|
+
}
|
|
91
|
+
const removeOurs = async () => {
|
|
92
|
+
// Only the file this call made, wherever its name now leads.
|
|
93
|
+
if (sameFile(await lstat(temporary).catch(() => null), created))
|
|
94
|
+
await unlink(temporary).catch(() => undefined);
|
|
95
|
+
};
|
|
96
|
+
try {
|
|
97
|
+
const [again, placed] = await Promise.all([realpath(folder).catch(() => null), lstat(temporary).catch(() => null)]);
|
|
98
|
+
if (again !== folder || !sameFile(placed, created))
|
|
99
|
+
throw changed(path, 'written');
|
|
100
|
+
await rename(temporary, target);
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
await removeOurs();
|
|
104
|
+
if (error.code === 'ENOENT')
|
|
105
|
+
throw changed(path, 'written');
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
if (!sameFile(await lstat(target).catch(() => null), created))
|
|
109
|
+
throw changed(path, 'written');
|
|
110
|
+
return target;
|
|
111
|
+
}
|
package/dist/utils/clipboard.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { spawn } from 'child_process';
|
|
2
|
+
import { buildUserSubprocessEnv } from './subprocess_env.js';
|
|
2
3
|
function runPipe(command, args, input) {
|
|
3
4
|
return new Promise((resolve, reject) => {
|
|
4
|
-
const child = spawn(command, args, { stdio: ['pipe', 'ignore', 'pipe'] });
|
|
5
|
+
const child = spawn(command, args, { stdio: ['pipe', 'ignore', 'pipe'], env: buildUserSubprocessEnv() });
|
|
5
6
|
let err = '';
|
|
6
7
|
child.stderr.on('data', chunk => {
|
|
7
8
|
err += chunk.toString();
|
|
@@ -9,63 +9,17 @@
|
|
|
9
9
|
* Follows the same pattern as dictation_native.ts.
|
|
10
10
|
*/
|
|
11
11
|
import { spawn } from 'child_process';
|
|
12
|
-
import { existsSync } from 'fs';
|
|
13
12
|
import { createInterface } from 'readline';
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import { fileURLToPath } from 'url';
|
|
17
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
18
|
-
const __dirname = dirname(__filename);
|
|
13
|
+
import { findNativeProgram, nativeProgramBuildCommand } from './native_install.js';
|
|
14
|
+
import { buildNativeHelperEnv } from './subprocess_env.js';
|
|
19
15
|
// --- Binary discovery ---
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
case 'darwin':
|
|
26
|
-
os = 'darwin';
|
|
27
|
-
break;
|
|
28
|
-
case 'linux':
|
|
29
|
-
os = 'linux';
|
|
30
|
-
break;
|
|
31
|
-
case 'win32':
|
|
32
|
-
os = 'win';
|
|
33
|
-
break;
|
|
34
|
-
default:
|
|
35
|
-
os = p;
|
|
36
|
-
}
|
|
37
|
-
let cpu;
|
|
38
|
-
switch (a) {
|
|
39
|
-
case 'x64':
|
|
40
|
-
cpu = 'x64';
|
|
41
|
-
break;
|
|
42
|
-
case 'arm64':
|
|
43
|
-
cpu = 'arm64';
|
|
44
|
-
break;
|
|
45
|
-
default:
|
|
46
|
-
cpu = a;
|
|
47
|
-
}
|
|
48
|
-
const ext = p === 'win32' ? '.exe' : '';
|
|
49
|
-
return `ztc-computer-${os}-${cpu}${ext}`;
|
|
50
|
-
}
|
|
16
|
+
/**
|
|
17
|
+
* The ztc-computer binary, from ZTC's own install only (native_install.ts findNativeProgram): a source checkout's cargo
|
|
18
|
+
* release build under the package first, as it came first before, then the package's bin/, then the checkout's debug
|
|
19
|
+
* build. Never the working directory or ~/.ztc/bin.
|
|
20
|
+
*/
|
|
51
21
|
function getBinaryPath() {
|
|
52
|
-
|
|
53
|
-
const possiblePaths = [
|
|
54
|
-
// Development: Rust build output
|
|
55
|
-
join(process.cwd(), 'packages', 'ztc-computer', 'target', 'release', 'ztc-computer'),
|
|
56
|
-
// Installed via npm (relative to this file in dist/utils/)
|
|
57
|
-
join(__dirname, '..', '..', 'bin', binaryName),
|
|
58
|
-
// Development: bin directory
|
|
59
|
-
join(process.cwd(), 'bin', binaryName),
|
|
60
|
-
// Global install location
|
|
61
|
-
join(homedir(), '.ztc', 'bin', binaryName),
|
|
62
|
-
];
|
|
63
|
-
for (const p of possiblePaths) {
|
|
64
|
-
if (existsSync(p)) {
|
|
65
|
-
return p;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
return null;
|
|
22
|
+
return findNativeProgram('ztc-computer');
|
|
69
23
|
}
|
|
70
24
|
/**
|
|
71
25
|
* Check if the native computer binary is available.
|
|
@@ -115,7 +69,7 @@ export class ComputerNative {
|
|
|
115
69
|
if (!binaryPath) {
|
|
116
70
|
this.emit({
|
|
117
71
|
type: 'error',
|
|
118
|
-
message:
|
|
72
|
+
message: `ztc-computer binary not found. Build with: ${nativeProgramBuildCommand('ztc-computer')}`,
|
|
119
73
|
});
|
|
120
74
|
return false;
|
|
121
75
|
}
|
|
@@ -126,6 +80,8 @@ export class ComputerNative {
|
|
|
126
80
|
try {
|
|
127
81
|
this.process = spawn(binaryPath, args, {
|
|
128
82
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
83
|
+
// The person's environment, none of ZTC's or its route's credentials: the processes it starts get the same.
|
|
84
|
+
env: buildNativeHelperEnv(),
|
|
129
85
|
});
|
|
130
86
|
// Parse JSON lines from stdout
|
|
131
87
|
this.readline = createInterface({
|
|
@@ -460,7 +416,7 @@ export function getComputerNativeStatus() {
|
|
|
460
416
|
}
|
|
461
417
|
else {
|
|
462
418
|
parts.push('Native computer: not available');
|
|
463
|
-
parts.push(
|
|
419
|
+
parts.push(`Build with: ${nativeProgramBuildCommand('ztc-computer')}`);
|
|
464
420
|
}
|
|
465
421
|
return parts.join('\n');
|
|
466
422
|
}
|
package/dist/utils/dictation.js
CHANGED
|
@@ -17,6 +17,8 @@ import { accessSync, constants, existsSync, mkdirSync, rmSync, statSync, writeFi
|
|
|
17
17
|
import { delimiter, join } from 'path';
|
|
18
18
|
import { tmpdir, homedir, platform } from 'os';
|
|
19
19
|
import { configStore } from '../config.js';
|
|
20
|
+
import { hostEnvironmentValue } from '../config/host_credentials.js';
|
|
21
|
+
import { buildUserSubprocessEnv } from './subprocess_env.js';
|
|
20
22
|
import { isNativeDictationAvailable } from './dictation_native.js';
|
|
21
23
|
/** What the first recording a session sends to OpenAI's Whisper API says (see DictationResult.notice). */
|
|
22
24
|
const OPENAI_SESSION_NOTICE = 'Dictation sends your recordings to OpenAI\'s Whisper API (api.openai.com) on OPENAI_API_KEY, as '
|
|
@@ -197,7 +199,7 @@ function macOSDictation(askAgain = false) {
|
|
|
197
199
|
if (onDeviceCheck?.swift !== swift || retry) {
|
|
198
200
|
const script = writeSpeechScript('on-device-check');
|
|
199
201
|
try {
|
|
200
|
-
const result = spawnSync(swift, [script, ON_DEVICE_CHECK], { encoding: 'utf-8', timeout: ON_DEVICE_CHECK_TIMEOUT_MS });
|
|
202
|
+
const result = spawnSync(swift, [script, ON_DEVICE_CHECK], { encoding: 'utf-8', timeout: ON_DEVICE_CHECK_TIMEOUT_MS, env: buildUserSubprocessEnv() });
|
|
201
203
|
const answer = result.status === 0 ? result.stdout.trim() : '';
|
|
202
204
|
onDeviceCheck = answer === 'on-device' || answer === 'server-only'
|
|
203
205
|
? { swift, answer, error: '', at: Date.now(), timedOut: false }
|
|
@@ -231,7 +233,7 @@ function getDictationChoice() {
|
|
|
231
233
|
export function getBestProvider() {
|
|
232
234
|
const choice = getDictationChoice();
|
|
233
235
|
if (choice === 'openai')
|
|
234
|
-
return
|
|
236
|
+
return hostEnvironmentValue('OPENAI_API_KEY') ? 'openai' : null;
|
|
235
237
|
const local = localWhisper() ? 'local' : null;
|
|
236
238
|
if (choice === 'macos')
|
|
237
239
|
return macOSDictation() === 'on-device' ? 'macos' : local;
|
|
@@ -301,7 +303,7 @@ export function dictationChoiceNotice(choice) {
|
|
|
301
303
|
return 'Dictation now sends each recording to OpenAI\'s Whisper API (api.openai.com) to transcribe it, and '
|
|
302
304
|
+ 'OPENAI_API_KEY pays. Run /dictation local to keep your audio on this computer.'
|
|
303
305
|
+ (isNativeDictationAvailable() ? ' While native dictation (ztc-audio) is installed, Ctrl+R uses it instead, on this computer.' : '')
|
|
304
|
-
+ (
|
|
306
|
+
+ (hostEnvironmentValue('OPENAI_API_KEY') ? '' : ' OPENAI_API_KEY isn\'t set yet: export it before you dictate.');
|
|
305
307
|
}
|
|
306
308
|
// macOS dictation is used only on-device: where this Mac can't, the notice says local whisper alone, and why.
|
|
307
309
|
// Choosing macOS dictation asks swift again if it failed before, however long ago: the person asked.
|
|
@@ -368,7 +370,7 @@ export function startRecording() {
|
|
|
368
370
|
'-c', '1', // Mono
|
|
369
371
|
'-b', '16', // 16-bit
|
|
370
372
|
RECORDING_FILE
|
|
371
|
-
], { stdio: 'ignore' });
|
|
373
|
+
], { stdio: 'ignore', env: buildUserSubprocessEnv() });
|
|
372
374
|
}
|
|
373
375
|
else if (tool === 'ffmpeg') {
|
|
374
376
|
if (os === 'darwin') {
|
|
@@ -376,21 +378,21 @@ export function startRecording() {
|
|
|
376
378
|
'-y', '-f', 'avfoundation', '-i', ':0',
|
|
377
379
|
'-ar', '16000', '-ac', '1', '-sample_fmt', 's16',
|
|
378
380
|
RECORDING_FILE
|
|
379
|
-
], { stdio: 'ignore' });
|
|
381
|
+
], { stdio: 'ignore', env: buildUserSubprocessEnv() });
|
|
380
382
|
}
|
|
381
383
|
else {
|
|
382
384
|
proc = spawn('ffmpeg', [
|
|
383
385
|
'-y', '-f', 'alsa', '-i', 'default',
|
|
384
386
|
'-ar', '16000', '-ac', '1', '-sample_fmt', 's16',
|
|
385
387
|
RECORDING_FILE
|
|
386
|
-
], { stdio: 'ignore' });
|
|
388
|
+
], { stdio: 'ignore', env: buildUserSubprocessEnv() });
|
|
387
389
|
}
|
|
388
390
|
}
|
|
389
391
|
else {
|
|
390
392
|
proc = spawn('arecord', [
|
|
391
393
|
'-f', 'S16_LE', '-r', '16000', '-c', '1',
|
|
392
394
|
RECORDING_FILE
|
|
393
|
-
], { stdio: 'ignore' });
|
|
395
|
+
], { stdio: 'ignore', env: buildUserSubprocessEnv() });
|
|
394
396
|
}
|
|
395
397
|
activeRecording = {
|
|
396
398
|
process: proc,
|
|
@@ -483,7 +485,7 @@ async function transcribe(audioPath, provider) {
|
|
|
483
485
|
* (see getBestProvider): the recording goes to OpenAI on OPENAI_API_KEY.
|
|
484
486
|
*/
|
|
485
487
|
async function transcribeOpenAI(audioPath) {
|
|
486
|
-
const apiKey =
|
|
488
|
+
const apiKey = hostEnvironmentValue('OPENAI_API_KEY');
|
|
487
489
|
if (!apiKey) {
|
|
488
490
|
throw new Error('OPENAI_API_KEY not set');
|
|
489
491
|
}
|
|
@@ -516,7 +518,7 @@ async function transcribeLocal(audioPath) {
|
|
|
516
518
|
'--model', 'base',
|
|
517
519
|
'--output_format', 'txt',
|
|
518
520
|
'--output_dir', RECORDING_DIR
|
|
519
|
-
], { encoding: 'utf-8', timeout: 60000 });
|
|
521
|
+
], { encoding: 'utf-8', timeout: 60000, env: buildUserSubprocessEnv() });
|
|
520
522
|
if (result.status === 0) {
|
|
521
523
|
const outputFile = audioPath.replace(/\.wav$/, '.txt');
|
|
522
524
|
if (existsSync(outputFile)) {
|
|
@@ -535,7 +537,7 @@ async function transcribeLocal(audioPath) {
|
|
|
535
537
|
const result = spawnSync('whisper-cpp', [
|
|
536
538
|
'-m', modelPath,
|
|
537
539
|
'-f', audioPath
|
|
538
|
-
], { encoding: 'utf-8', timeout: 60000 });
|
|
540
|
+
], { encoding: 'utf-8', timeout: 60000, env: buildUserSubprocessEnv() });
|
|
539
541
|
if (result.status === 0) {
|
|
540
542
|
return result.stdout.trim();
|
|
541
543
|
}
|
|
@@ -555,7 +557,8 @@ async function transcribeMacOS(audioPath) {
|
|
|
555
557
|
try {
|
|
556
558
|
const result = spawnSync(swift, [script, audioPath], {
|
|
557
559
|
encoding: 'utf-8',
|
|
558
|
-
timeout: 35000
|
|
560
|
+
timeout: 35000,
|
|
561
|
+
env: buildUserSubprocessEnv(),
|
|
559
562
|
});
|
|
560
563
|
if (result.status !== 0) {
|
|
561
564
|
throw new Error(result.stderr?.trim() || 'macOS transcription failed');
|
|
@@ -569,7 +572,7 @@ async function transcribeMacOS(audioPath) {
|
|
|
569
572
|
/** What transcribes a recording, as /dictation shows it: on this computer, or OpenAI's Whisper API as chosen. */
|
|
570
573
|
function describeTranscription(provider, choice) {
|
|
571
574
|
if (choice === 'openai') {
|
|
572
|
-
return
|
|
575
|
+
return hostEnvironmentValue('OPENAI_API_KEY')
|
|
573
576
|
? 'OpenAI Whisper API, as you chose with /dictation openai: each recording goes to OpenAI, and OPENAI_API_KEY pays '
|
|
574
577
|
+ '(/dictation local keeps your audio on this computer)'
|
|
575
578
|
: 'OpenAI Whisper API, as you chose with /dictation openai, but OPENAI_API_KEY isn\'t set '
|