@causyne/agent 1.20.0 → 1.20.5
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 +0 -18
- package/package.json +1 -1
- package/src/acp/client.js +2 -1
- package/src/acp/client.ts +2 -1
- package/src/acp/server.js +5 -6
- package/src/acp/server.ts +4 -6
- package/src/agentLoop.d.ts +1 -0
- package/src/agentLoop.js +505 -241
- package/src/agentLoop.ts +527 -258
- package/src/agentPolicy.js +9 -0
- package/src/agentPolicy.ts +9 -0
- package/src/approvalPolicy.js +55 -0
- package/src/approvalPolicy.ts +66 -0
- package/src/browser/browserRuntime.js +217 -14
- package/src/browser/browserRuntime.ts +255 -14
- package/src/browser/causyneBrowser.js +1215 -68
- package/src/browser/causyneBrowser.ts +1340 -72
- package/src/cliMode.js +52 -29
- package/src/commands.js +141 -17
- package/src/config.js +22 -12
- package/src/continuum/ui/continuumPicker.js +18 -1
- package/src/continuum/ui/continuumPicker.ts +19 -1
- package/src/dynamicCatalogue.js +87 -20
- package/src/dynamicCatalogue.ts +85 -15
- package/src/launchIntent.js +40 -0
- package/src/launchIntent.ts +49 -0
- package/src/memoryManager.js +26 -2
- package/src/modelCatalogue.js +2 -0
- package/src/planCache.js +320 -11
- package/src/planCache.ts +402 -13
- package/src/providers/acp.js +67 -1
- package/src/providers/acp.ts +65 -2
- package/src/providers/antigravity.d.ts +5 -1
- package/src/providers/antigravity.js +643 -32
- package/src/providers/antigravity.ts +671 -33
- package/src/providers/base.d.ts +9 -0
- package/src/providers/factory.js +2 -0
- package/src/providers/pi.js +157 -0
- package/src/providers/pi.ts +144 -0
- package/src/provisioning/capabilityProvisioner.js +17 -0
- package/src/provisioning/capabilityTools.js +1 -1
- package/src/sandbox.js +30 -39
- package/src/sessionJournal.js +53 -10
- package/src/sessionJournal.ts +64 -10
- package/src/taskShape.js +9 -9
- package/src/taskShape.ts +9 -9
- package/src/timeMachine.js +124 -0
- package/src/timeMachine.ts +141 -0
- package/src/toolPacks.js +4 -10
- package/src/tools/archive.js +196 -59
- package/src/tools/archive.ts +220 -0
- package/src/tools/browserAutomator.js +78 -9
- package/src/tools/browserAutomator.ts +90 -9
- package/src/tools/desktopOpen.js +114 -31
- package/src/tools/desktopOpen.ts +144 -0
- package/src/tools/gitExec.js +5 -3
- package/src/tools/nativeEngine.js +4 -3
- package/src/tools/nativeEngine.ts +5 -3
- package/src/tools/openSourceToolProvisioner.js +5 -4
- package/src/tools/openSourceToolProvisioner.ts +5 -4
- package/src/tools/registry.js +3 -0
- package/src/tools/screenshot.js +55 -71
- package/src/tools/screenshot.ts +70 -0
- package/src/tools/sysRun.js +284 -117
- package/src/tools/sysRun.ts +347 -0
- package/src/tools/toolSelfRepair.js +109 -2
- package/src/tools/toolSelfRepair.ts +100 -2
- package/src/tools/updatePlan.js +49 -1
- package/src/tools/updatePlan.ts +43 -1
- package/src/tools/viewImage.js +93 -78
- package/src/tools/viewImage.ts +116 -0
- package/src/tools/webFetch.js +20 -6
- package/src/tools/webFetch.ts +26 -6
- package/src/tui/block.d.ts +1 -0
- package/src/tui/chatRenderer.d.ts +21 -0
- package/src/tui/chatRenderer.js +34 -47
- package/src/tui/clipboardSync.js +28 -1
- package/src/tui/clipboardSync.ts +26 -1
- package/src/tui/codeProgress.js +299 -0
- package/src/tui/codeProgress.ts +304 -0
- package/src/tui/codeStreamRenderer.js +116 -0
- package/src/tui/codeStreamRenderer.ts +124 -0
- package/src/tui/commandPresentation.js +11 -0
- package/src/tui/commandPresentation.ts +18 -0
- package/src/tui/completion.js +45 -13
- package/src/tui/composerControls.js +66 -0
- package/src/tui/composerControls.ts +78 -0
- package/src/tui/diff.d.ts +2 -0
- package/src/tui/home.js +4 -2
- package/src/tui/homeTransition.js +21 -0
- package/src/tui/homeTransition.ts +27 -0
- package/src/tui/homeView.js +62 -0
- package/src/tui/homeView.ts +91 -0
- package/src/tui/layout.js +46 -0
- package/src/tui/layout.ts +52 -0
- package/src/tui/messageActionPlacement.js +16 -0
- package/src/tui/messageActionPlacement.ts +25 -0
- package/src/tui/messageInteraction.js +221 -0
- package/src/tui/messageInteraction.ts +275 -0
- package/src/tui/modelPicker.js +4 -24
- package/src/tui/modelPicker.ts +4 -20
- package/src/tui/mouseHover.js +6 -0
- package/src/tui/mouseHover.ts +5 -0
- package/src/tui/prompt.js +152 -76
- package/src/tui/resizeRecovery.js +15 -0
- package/src/tui/resizeRecovery.ts +27 -0
- package/src/tui/scrollInteraction.js +97 -0
- package/src/tui/scrollInteraction.ts +189 -0
- package/src/tui/sessionLayout.js +12 -0
- package/src/tui/sessionLayout.ts +12 -0
- package/src/tui/sessionPicker.js +111 -16
- package/src/tui/sessionPicker.ts +121 -16
- package/src/tui/sessionSidebar.js +78 -0
- package/src/tui/sessionSidebar.ts +95 -0
- package/src/tui/setup.js +1 -1
- package/src/tui/setup.ts +1 -1
- package/src/tui/stageSurface.js +120 -58
- package/src/tui/text.d.ts +5 -0
- package/src/tui/textLayout.js +23 -0
- package/src/tui/textLayout.ts +28 -0
- package/src/tui/viewport.js +204 -279
- package/src/tui/viewport.ts +326 -0
- package/src/types/contracts.ts +54 -0
- package/src/utils/paths.js +5 -4
- package/src/utils/paths.ts +5 -5
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Product policy supplied to every Causyne Agent session, regardless of provider. */
|
|
2
|
+
export const AGENT_USER_POLICY = `# User Authority, Scope, and Evidence
|
|
3
|
+
1. The current user's objective, selected workspace, stated limits, and order of operations define the task within applicable safety constraints. Treat that intent as your operating center. Do not silently substitute a different goal or invent a prohibition against ordinary authorized work.
|
|
4
|
+
2. Act only within the requested scope. Preserve unrelated files, data, services, and behavior. A broad objective permits suitable methods, not unrelated changes.
|
|
5
|
+
3. When the user delegates implementation choices, investigate and act independently. Resolve routine choices yourself and continue until the goal is achieved or a real blocker is evidenced. Delegation does not erase the user's boundaries.
|
|
6
|
+
4. Never invent sources, tool results, capabilities, tests, fixes, deployments, or success. Report only what evidence supports, and distinguish observation from inference and uncertainty.
|
|
7
|
+
5. Investigate difficult work and legitimate alternatives before concluding that it cannot be done. Respect real technical and safety limits; stop unproductive retries and explain the verified blocker.
|
|
8
|
+
6. In Mode C, perform ordinary authorized work without repeat permission prompts. Ask before deleting an entire file or directory unless the current session holds an explicit file-deletion grant. Editing or replacing lines inside a file is not whole-file deletion. Workspace checks and hard safety blocks still apply.
|
|
9
|
+
7. Ask for a user decision only when a genuinely unresolved, durable choice matters. Never re-ask for a choice the user already made.`;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Product policy supplied to every Causyne Agent session, regardless of provider. */
|
|
2
|
+
export const AGENT_USER_POLICY = `# User Authority, Scope, and Evidence
|
|
3
|
+
1. The current user's objective, selected workspace, stated limits, and order of operations define the task within applicable safety constraints. Treat that intent as your operating center. Do not silently substitute a different goal or invent a prohibition against ordinary authorized work.
|
|
4
|
+
2. Act only within the requested scope. Preserve unrelated files, data, services, and behavior. A broad objective permits suitable methods, not unrelated changes.
|
|
5
|
+
3. When the user delegates implementation choices, investigate and act independently. Resolve routine choices yourself and continue until the goal is achieved or a real blocker is evidenced. Delegation does not erase the user's boundaries.
|
|
6
|
+
4. Never invent sources, tool results, capabilities, tests, fixes, deployments, or success. Report only what evidence supports, and distinguish observation from inference and uncertainty.
|
|
7
|
+
5. Investigate difficult work and legitimate alternatives before concluding that it cannot be done. Respect real technical and safety limits; stop unproductive retries and explain the verified blocker.
|
|
8
|
+
6. In Mode C, perform ordinary authorized work without repeat permission prompts. Ask before deleting an entire file or directory unless the current session holds an explicit file-deletion grant. Editing or replacing lines inside a file is not whole-file deletion. Workspace checks and hard safety blocks still apply.
|
|
9
|
+
7. Ask for a user decision only when a genuinely unresolved, durable choice matters. Never re-ask for a choice the user already made.`;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
const toolAutonomy = new AsyncLocalStorage();
|
|
3
|
+
/** Session-scoped context for legacy tools whose execute signature has no context parameter. */
|
|
4
|
+
export function withToolAutonomyMode(mode, run) {
|
|
5
|
+
return toolAutonomy.run(mode, run);
|
|
6
|
+
}
|
|
7
|
+
export function currentToolAutonomyMode() {
|
|
8
|
+
return toolAutonomy.getStore();
|
|
9
|
+
}
|
|
10
|
+
/** Read the live session mode from the tool context; never infer it from a tool argument. */
|
|
11
|
+
export function isAutonomousToolContext(context) {
|
|
12
|
+
if (!context || typeof context !== 'object')
|
|
13
|
+
return false;
|
|
14
|
+
const parentLoop = context.parentLoop;
|
|
15
|
+
if (!parentLoop || typeof parentLoop !== 'object')
|
|
16
|
+
return false;
|
|
17
|
+
const config = parentLoop.config;
|
|
18
|
+
if (!config || typeof config !== 'object')
|
|
19
|
+
return false;
|
|
20
|
+
const mode = config.autonomyMode;
|
|
21
|
+
return mode === 'C' || mode === undefined;
|
|
22
|
+
}
|
|
23
|
+
const SHELL_TOOLS = new Set(['sysRun', 'sysRunBg', 'terminal_persistent_shell']);
|
|
24
|
+
const WHOLE_FILE_DELETE = /(?:^|[;&|\n(]\s*)(?:(?:sudo|command|env)\s+)*(?:rm|unlink|rmdir|del|erase|Remove-Item)\b|\bfind\b[^\n;]*\s-delete\b|\bgit\s+(?:rm|clean\s+-[a-z]*f)\b/i;
|
|
25
|
+
const DELETE_API_CALL = /\b(?:os\.(?:remove|unlink)|shutil\.rmtree|fs\.(?:rm|unlink)(?:Sync)?|\.unlink|\.rmdir)\s*\(/i;
|
|
26
|
+
const NESTED_SHELL_DELETE = /\b(?:bash|sh|zsh|dash)\s+-c\s+['"]\s*(?:rm|unlink|rmdir)\b|\b(?:powershell|pwsh)\s+(?:-Command|-c)\s+['"]?\s*Remove-Item\b/i;
|
|
27
|
+
/** A deletion is a whole filesystem entry, not removal of lines in an edit. */
|
|
28
|
+
export function isWholeFileDeletion(tool, args) {
|
|
29
|
+
if (tool === 'fs_delete')
|
|
30
|
+
return true;
|
|
31
|
+
const command = tool === 'gitOp'
|
|
32
|
+
? `git ${String(args.command ?? args.action ?? '')}`
|
|
33
|
+
: SHELL_TOOLS.has(tool)
|
|
34
|
+
? String(args.command ?? args.input ?? args.cmd ?? '')
|
|
35
|
+
: '';
|
|
36
|
+
if (/^\s*git\s+rm\s+--cached\b/i.test(command))
|
|
37
|
+
return false;
|
|
38
|
+
return command.length > 0
|
|
39
|
+
&& (WHOLE_FILE_DELETE.test(command) || DELETE_API_CALL.test(command) || NESTED_SHELL_DELETE.test(command));
|
|
40
|
+
}
|
|
41
|
+
/** Prompt policy only. Sandbox denial, workspace checks, and observer veto run separately. */
|
|
42
|
+
export function requiresToolApproval(input) {
|
|
43
|
+
if (input.mode === 'C') {
|
|
44
|
+
if (!isWholeFileDeletion(input.tool, input.args))
|
|
45
|
+
return false;
|
|
46
|
+
// A general shell or git grant must not authorize future deletions.
|
|
47
|
+
return input.tool !== 'fs_delete' || !input.standingApproval;
|
|
48
|
+
}
|
|
49
|
+
if (input.standingApproval)
|
|
50
|
+
return false;
|
|
51
|
+
return input.risk === 'extreme'
|
|
52
|
+
|| input.heavyDownload
|
|
53
|
+
|| input.risk === 'dangerous'
|
|
54
|
+
|| input.mode === 'A';
|
|
55
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
|
|
3
|
+
export type AutonomyMode = 'A' | 'B' | 'C' | string;
|
|
4
|
+
|
|
5
|
+
const toolAutonomy = new AsyncLocalStorage<AutonomyMode>();
|
|
6
|
+
|
|
7
|
+
/** Session-scoped context for legacy tools whose execute signature has no context parameter. */
|
|
8
|
+
export function withToolAutonomyMode<T>(mode: AutonomyMode, run: () => Promise<T>): Promise<T> {
|
|
9
|
+
return toolAutonomy.run(mode, run);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function currentToolAutonomyMode(): AutonomyMode | undefined {
|
|
13
|
+
return toolAutonomy.getStore();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface ToolApprovalInput {
|
|
17
|
+
mode: AutonomyMode;
|
|
18
|
+
tool: string;
|
|
19
|
+
args: Record<string, unknown>;
|
|
20
|
+
risk: string;
|
|
21
|
+
heavyDownload: boolean;
|
|
22
|
+
standingApproval: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Read the live session mode from the tool context; never infer it from a tool argument. */
|
|
26
|
+
export function isAutonomousToolContext(context: unknown): boolean {
|
|
27
|
+
if (!context || typeof context !== 'object') return false;
|
|
28
|
+
const parentLoop = (context as { parentLoop?: unknown }).parentLoop;
|
|
29
|
+
if (!parentLoop || typeof parentLoop !== 'object') return false;
|
|
30
|
+
const config = (parentLoop as { config?: unknown }).config;
|
|
31
|
+
if (!config || typeof config !== 'object') return false;
|
|
32
|
+
const mode = (config as { autonomyMode?: unknown }).autonomyMode;
|
|
33
|
+
return mode === 'C' || mode === undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const SHELL_TOOLS = new Set(['sysRun', 'sysRunBg', 'terminal_persistent_shell']);
|
|
37
|
+
const WHOLE_FILE_DELETE = /(?:^|[;&|\n(]\s*)(?:(?:sudo|command|env)\s+)*(?:rm|unlink|rmdir|del|erase|Remove-Item)\b|\bfind\b[^\n;]*\s-delete\b|\bgit\s+(?:rm|clean\s+-[a-z]*f)\b/i;
|
|
38
|
+
const DELETE_API_CALL = /\b(?:os\.(?:remove|unlink)|shutil\.rmtree|fs\.(?:rm|unlink)(?:Sync)?|\.unlink|\.rmdir)\s*\(/i;
|
|
39
|
+
const NESTED_SHELL_DELETE = /\b(?:bash|sh|zsh|dash)\s+-c\s+['"]\s*(?:rm|unlink|rmdir)\b|\b(?:powershell|pwsh)\s+(?:-Command|-c)\s+['"]?\s*Remove-Item\b/i;
|
|
40
|
+
|
|
41
|
+
/** A deletion is a whole filesystem entry, not removal of lines in an edit. */
|
|
42
|
+
export function isWholeFileDeletion(tool: string, args: Record<string, unknown>): boolean {
|
|
43
|
+
if (tool === 'fs_delete') return true;
|
|
44
|
+
const command = tool === 'gitOp'
|
|
45
|
+
? `git ${String(args.command ?? args.action ?? '')}`
|
|
46
|
+
: SHELL_TOOLS.has(tool)
|
|
47
|
+
? String(args.command ?? args.input ?? args.cmd ?? '')
|
|
48
|
+
: '';
|
|
49
|
+
if (/^\s*git\s+rm\s+--cached\b/i.test(command)) return false;
|
|
50
|
+
return command.length > 0
|
|
51
|
+
&& (WHOLE_FILE_DELETE.test(command) || DELETE_API_CALL.test(command) || NESTED_SHELL_DELETE.test(command));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Prompt policy only. Sandbox denial, workspace checks, and observer veto run separately. */
|
|
55
|
+
export function requiresToolApproval(input: ToolApprovalInput): boolean {
|
|
56
|
+
if (input.mode === 'C') {
|
|
57
|
+
if (!isWholeFileDeletion(input.tool, input.args)) return false;
|
|
58
|
+
// A general shell or git grant must not authorize future deletions.
|
|
59
|
+
return input.tool !== 'fs_delete' || !input.standingApproval;
|
|
60
|
+
}
|
|
61
|
+
if (input.standingApproval) return false;
|
|
62
|
+
return input.risk === 'extreme'
|
|
63
|
+
|| input.heavyDownload
|
|
64
|
+
|| input.risk === 'dangerous'
|
|
65
|
+
|| input.mode === 'A';
|
|
66
|
+
}
|
|
@@ -1,6 +1,30 @@
|
|
|
1
|
-
import
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import { accessSync, constants, existsSync, mkdirSync, readdirSync, readFileSync, realpathSync, rmSync, statSync, symlinkSync, writeFileSync } from 'node:fs';
|
|
2
3
|
import os from 'node:os';
|
|
3
4
|
import path from 'node:path';
|
|
5
|
+
function resolveCandidateExecutablePath(candidate) {
|
|
6
|
+
if (!existsSync(candidate))
|
|
7
|
+
return candidate;
|
|
8
|
+
try {
|
|
9
|
+
const stat = statSync(candidate);
|
|
10
|
+
if (stat.isDirectory()) {
|
|
11
|
+
const nestedCandidates = [
|
|
12
|
+
path.join(candidate, 'Contents', 'MacOS', 'camoufox'),
|
|
13
|
+
path.join(candidate, 'Camoufox.app', 'Contents', 'MacOS', 'camoufox'),
|
|
14
|
+
path.join(candidate, 'camoufox-bin'),
|
|
15
|
+
path.join(candidate, 'camoufox'),
|
|
16
|
+
path.join(candidate, 'camoufox.exe')
|
|
17
|
+
];
|
|
18
|
+
const found = nestedCandidates.find(existsSync);
|
|
19
|
+
if (found)
|
|
20
|
+
return found;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
// Fall through to assertExecutable for deterministic error reporting.
|
|
25
|
+
}
|
|
26
|
+
return candidate;
|
|
27
|
+
}
|
|
4
28
|
function assertExecutable(candidate) {
|
|
5
29
|
const stat = statSync(candidate);
|
|
6
30
|
if (!stat.isFile() && !stat.isSymbolicLink()) {
|
|
@@ -14,8 +38,12 @@ function candidateExecutables() {
|
|
|
14
38
|
if (process.platform === 'darwin') {
|
|
15
39
|
return [
|
|
16
40
|
path.join(home, 'Library', 'Caches', 'camoufox', 'Camoufox.app', 'Contents', 'MacOS', 'camoufox'),
|
|
41
|
+
path.join(home, 'Library', 'Application Support', 'camoufox', 'Camoufox.app', 'Contents', 'MacOS', 'camoufox'),
|
|
42
|
+
path.join(home, '.cache', 'camoufox', 'Camoufox.app', 'Contents', 'MacOS', 'camoufox'),
|
|
17
43
|
'/Applications/Camoufox.app/Contents/MacOS/camoufox',
|
|
18
|
-
path.join(home, 'Applications', 'Camoufox.app', 'Contents', 'MacOS', 'camoufox')
|
|
44
|
+
path.join(home, 'Applications', 'Camoufox.app', 'Contents', 'MacOS', 'camoufox'),
|
|
45
|
+
'/opt/homebrew/bin/camoufox',
|
|
46
|
+
'/usr/local/bin/camoufox'
|
|
19
47
|
];
|
|
20
48
|
}
|
|
21
49
|
if (process.platform === 'win32') {
|
|
@@ -24,14 +52,19 @@ function candidateExecutables() {
|
|
|
24
52
|
return [
|
|
25
53
|
...(localAppData ? [path.join(localAppData, 'camoufox', 'camoufox.exe')] : []),
|
|
26
54
|
...(localAppData ? [path.join(localAppData, 'Camoufox', 'camoufox.exe')] : []),
|
|
27
|
-
...(programFiles ? [path.join(programFiles, 'Camoufox', 'camoufox.exe')] : [])
|
|
55
|
+
...(programFiles ? [path.join(programFiles, 'Camoufox', 'camoufox.exe')] : []),
|
|
56
|
+
path.join(home, '.cache', 'camoufox', 'camoufox.exe')
|
|
28
57
|
];
|
|
29
58
|
}
|
|
30
59
|
return [
|
|
31
60
|
path.join(home, '.cache', 'camoufox', 'camoufox-bin'),
|
|
32
61
|
'/opt/camoufox/camoufox-bin',
|
|
62
|
+
'/opt/camoufox/camoufox',
|
|
33
63
|
path.join(home, '.local', 'share', 'camoufox', 'camoufox-bin'),
|
|
34
|
-
'
|
|
64
|
+
path.join(home, '.local', 'bin', 'camoufox'),
|
|
65
|
+
'/usr/local/lib/camoufox/camoufox-bin',
|
|
66
|
+
'/usr/local/bin/camoufox',
|
|
67
|
+
'/usr/bin/camoufox'
|
|
35
68
|
];
|
|
36
69
|
}
|
|
37
70
|
function expectedExecutable(installDirectory) {
|
|
@@ -42,6 +75,40 @@ function expectedExecutable(installDirectory) {
|
|
|
42
75
|
return path.join(installDirectory, 'camoufox.exe');
|
|
43
76
|
return path.join(installDirectory, 'camoufox-bin');
|
|
44
77
|
}
|
|
78
|
+
function nixStoreRoot(candidatePath) {
|
|
79
|
+
const match = candidatePath.match(/^\/nix\/store\/[^/]+/);
|
|
80
|
+
return match?.[0] ?? null;
|
|
81
|
+
}
|
|
82
|
+
function collectDirs(root, maxDepth = 4) {
|
|
83
|
+
const dirs = [];
|
|
84
|
+
const queue = [{ dir: root, depth: 0 }];
|
|
85
|
+
const seen = new Set();
|
|
86
|
+
while (queue.length > 0) {
|
|
87
|
+
const current = queue.shift();
|
|
88
|
+
if (!current)
|
|
89
|
+
break;
|
|
90
|
+
const { dir, depth } = current;
|
|
91
|
+
if (seen.has(dir))
|
|
92
|
+
continue;
|
|
93
|
+
seen.add(dir);
|
|
94
|
+
dirs.push(dir);
|
|
95
|
+
if (depth >= maxDepth)
|
|
96
|
+
continue;
|
|
97
|
+
let entries = [];
|
|
98
|
+
try {
|
|
99
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
for (const entry of entries) {
|
|
105
|
+
if (!entry.isDirectory() && !entry.isSymbolicLink())
|
|
106
|
+
continue;
|
|
107
|
+
queue.push({ dir: path.join(dir, entry.name), depth: depth + 1 });
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return dirs;
|
|
111
|
+
}
|
|
45
112
|
function findInstallDirectory(executablePath) {
|
|
46
113
|
let current = path.dirname(executablePath);
|
|
47
114
|
for (let depth = 0; depth < 7; depth += 1) {
|
|
@@ -55,15 +122,138 @@ function findInstallDirectory(executablePath) {
|
|
|
55
122
|
}
|
|
56
123
|
return null;
|
|
57
124
|
}
|
|
58
|
-
function
|
|
125
|
+
function findResourceDirectory(executablePath, resolvedExecutable) {
|
|
126
|
+
const directCandidates = [];
|
|
59
127
|
if (process.platform === 'darwin') {
|
|
60
|
-
|
|
128
|
+
directCandidates.push(path.resolve(path.dirname(resolvedExecutable), '..', 'Resources'));
|
|
129
|
+
directCandidates.push(path.resolve(path.dirname(executablePath), '..', 'Resources'));
|
|
130
|
+
}
|
|
131
|
+
directCandidates.push(path.dirname(resolvedExecutable));
|
|
132
|
+
directCandidates.push(path.dirname(executablePath));
|
|
133
|
+
let ancestor = path.dirname(resolvedExecutable);
|
|
134
|
+
for (let depth = 0; depth < 5; depth += 1) {
|
|
135
|
+
directCandidates.push(ancestor);
|
|
136
|
+
const parent = path.dirname(ancestor);
|
|
137
|
+
if (parent === ancestor)
|
|
138
|
+
break;
|
|
139
|
+
ancestor = parent;
|
|
61
140
|
}
|
|
62
|
-
|
|
141
|
+
const storeRoot = nixStoreRoot(resolvedExecutable) ?? nixStoreRoot(executablePath);
|
|
142
|
+
if (storeRoot) {
|
|
143
|
+
directCandidates.push(storeRoot, path.join(storeRoot, 'lib', 'camoufox'), path.join(storeRoot, 'libexec', 'camoufox'), path.join(storeRoot, 'share', 'camoufox'), path.join(storeRoot, 'opt', 'camoufox'));
|
|
144
|
+
}
|
|
145
|
+
for (const dir of directCandidates) {
|
|
146
|
+
if (existsSync(path.join(dir, 'properties.json')))
|
|
147
|
+
return dir;
|
|
148
|
+
}
|
|
149
|
+
if (storeRoot) {
|
|
150
|
+
for (const dir of collectDirs(storeRoot)) {
|
|
151
|
+
if (existsSync(path.join(dir, 'properties.json')))
|
|
152
|
+
return dir;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
function findVersionFile(executablePath, resourceDirectory) {
|
|
158
|
+
const candidates = [
|
|
159
|
+
path.join(resourceDirectory, 'version.json'),
|
|
160
|
+
path.dirname(executablePath)
|
|
161
|
+
];
|
|
162
|
+
let current = path.dirname(executablePath);
|
|
163
|
+
for (let depth = 0; depth < 7; depth += 1) {
|
|
164
|
+
const versionCandidate = path.join(current, 'version.json');
|
|
165
|
+
if (existsSync(versionCandidate))
|
|
166
|
+
return versionCandidate;
|
|
167
|
+
const parent = path.dirname(current);
|
|
168
|
+
if (parent === current)
|
|
169
|
+
break;
|
|
170
|
+
current = parent;
|
|
171
|
+
}
|
|
172
|
+
for (const candidate of candidates) {
|
|
173
|
+
if (candidate.endsWith('version.json') && existsSync(candidate))
|
|
174
|
+
return candidate;
|
|
175
|
+
}
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
function ensureSymlink(target, linkPath, type = 'file') {
|
|
179
|
+
rmSync(linkPath, { force: true, recursive: true });
|
|
180
|
+
mkdirSync(path.dirname(linkPath), { recursive: true });
|
|
181
|
+
symlinkSync(target, linkPath, process.platform === 'win32' && type === 'dir' ? 'junction' : type);
|
|
182
|
+
}
|
|
183
|
+
function ensureExternalBundleInstallDirectory(resolvedExecutable, resourceDirectory, versionFile) {
|
|
184
|
+
const propertiesFile = path.join(resourceDirectory, 'properties.json');
|
|
185
|
+
if (!existsSync(propertiesFile))
|
|
186
|
+
return null;
|
|
187
|
+
const key = crypto
|
|
188
|
+
.createHash('sha256')
|
|
189
|
+
.update(`${resolvedExecutable}\n${realpathSync(resourceDirectory)}`)
|
|
190
|
+
.digest('hex')
|
|
191
|
+
.slice(0, 16);
|
|
192
|
+
const shimDir = path.join(os.tmpdir(), 'causyne-browser-runtime', key);
|
|
193
|
+
const cacheVersion = path.join(shimDir, 'version.json');
|
|
194
|
+
const cacheProperties = path.join(shimDir, 'properties.json');
|
|
195
|
+
const cacheFontconfig = path.join(shimDir, 'fontconfig');
|
|
196
|
+
const cacheExecutable = expectedExecutable(shimDir);
|
|
197
|
+
mkdirSync(shimDir, { recursive: true });
|
|
198
|
+
if (!existsSync(cacheVersion)) {
|
|
199
|
+
if (versionFile && existsSync(versionFile)) {
|
|
200
|
+
writeFileSync(cacheVersion, readFileSync(versionFile));
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
writeFileSync(cacheVersion, JSON.stringify({ version: 'external', build: 'local' }), 'utf8');
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (!existsSync(cacheProperties)) {
|
|
207
|
+
ensureSymlink(realpathSync(propertiesFile), cacheProperties, 'file');
|
|
208
|
+
}
|
|
209
|
+
const sourceFontconfig = path.join(resourceDirectory, 'fontconfig');
|
|
210
|
+
if (existsSync(sourceFontconfig) && !existsSync(cacheFontconfig)) {
|
|
211
|
+
ensureSymlink(realpathSync(sourceFontconfig), cacheFontconfig, 'dir');
|
|
212
|
+
}
|
|
213
|
+
if (process.platform === 'darwin') {
|
|
214
|
+
const appResourceDir = path.join(shimDir, 'Camoufox.app', 'Contents', 'Resources');
|
|
215
|
+
mkdirSync(appResourceDir, { recursive: true });
|
|
216
|
+
const appProperties = path.join(appResourceDir, 'properties.json');
|
|
217
|
+
if (!existsSync(appProperties)) {
|
|
218
|
+
ensureSymlink(realpathSync(propertiesFile), appProperties, 'file');
|
|
219
|
+
}
|
|
220
|
+
if (existsSync(sourceFontconfig) && !existsSync(path.join(appResourceDir, 'fontconfig'))) {
|
|
221
|
+
ensureSymlink(realpathSync(sourceFontconfig), path.join(appResourceDir, 'fontconfig'), 'dir');
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if (!existsSync(cacheExecutable)) {
|
|
225
|
+
ensureSymlink(resolvedExecutable, cacheExecutable, 'file');
|
|
226
|
+
}
|
|
227
|
+
return shimDir;
|
|
228
|
+
}
|
|
229
|
+
function ensureLaunchShim(resolvedExecutable, resourceDirectory) {
|
|
230
|
+
const key = crypto
|
|
231
|
+
.createHash('sha256')
|
|
232
|
+
.update(`${resolvedExecutable}\n${realpathSync(resourceDirectory)}`)
|
|
233
|
+
.digest('hex')
|
|
234
|
+
.slice(0, 16);
|
|
235
|
+
const shimRoot = path.join(os.tmpdir(), 'causyne-browser-launch-shim', key);
|
|
236
|
+
mkdirSync(shimRoot, { recursive: true });
|
|
237
|
+
const shimExecutable = path.join(shimRoot, process.platform === 'win32' ? 'camoufox.exe' : 'camoufox-bin');
|
|
238
|
+
ensureSymlink(resolvedExecutable, shimExecutable, 'file');
|
|
239
|
+
for (const name of ['properties.json', 'version.json']) {
|
|
240
|
+
const target = path.join(resourceDirectory, name);
|
|
241
|
+
if (existsSync(target)) {
|
|
242
|
+
ensureSymlink(realpathSync(target), path.join(shimRoot, name), 'file');
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
const fontconfig = path.join(resourceDirectory, 'fontconfig');
|
|
246
|
+
if (existsSync(fontconfig)) {
|
|
247
|
+
ensureSymlink(realpathSync(fontconfig), path.join(shimRoot, 'fontconfig'), 'dir');
|
|
248
|
+
}
|
|
249
|
+
return shimExecutable;
|
|
63
250
|
}
|
|
64
251
|
export function resolveBrowserRuntime() {
|
|
65
|
-
const configured = process.env.CAUSYNE_BROWSER_EXECUTABLE?.trim()
|
|
66
|
-
|
|
252
|
+
const configured = process.env.CAUSYNE_BROWSER_EXECUTABLE?.trim()
|
|
253
|
+
|| process.env.CAMOUFOX_EXECUTABLE_PATH?.trim();
|
|
254
|
+
const candidates = configured
|
|
255
|
+
? [resolveCandidateExecutablePath(path.resolve(configured))]
|
|
256
|
+
: candidateExecutables().map(resolveCandidateExecutablePath);
|
|
67
257
|
const executablePath = candidates.find(existsSync);
|
|
68
258
|
if (!executablePath) {
|
|
69
259
|
throw new Error('Causyne Browser runtime is not installed. Causyne does not download browser binaries automatically. ' +
|
|
@@ -71,18 +261,31 @@ export function resolveBrowserRuntime() {
|
|
|
71
261
|
}
|
|
72
262
|
assertExecutable(executablePath);
|
|
73
263
|
const resolvedExecutable = realpathSync(executablePath);
|
|
74
|
-
const
|
|
264
|
+
const resourceDirectory = findResourceDirectory(executablePath, resolvedExecutable);
|
|
265
|
+
if (!resourceDirectory) {
|
|
266
|
+
throw new Error(`Causyne Browser runtime metadata (properties.json) was not found for ${resolvedExecutable}. ` +
|
|
267
|
+
'Keep properties.json beside its platform bundle and point CAUSYNE_BROWSER_EXECUTABLE at that bundle.');
|
|
268
|
+
}
|
|
269
|
+
let installDirectory = findInstallDirectory(resolvedExecutable);
|
|
270
|
+
if (!installDirectory) {
|
|
271
|
+
const versionFile = findVersionFile(resolvedExecutable, resourceDirectory);
|
|
272
|
+
installDirectory = ensureExternalBundleInstallDirectory(resolvedExecutable, resourceDirectory, versionFile);
|
|
273
|
+
}
|
|
75
274
|
if (!installDirectory) {
|
|
76
275
|
throw new Error(`Causyne Browser runtime at ${resolvedExecutable} is not a complete distribution. ` +
|
|
77
|
-
'Keep version.json beside its platform bundle and point CAUSYNE_BROWSER_EXECUTABLE at that bundle.');
|
|
276
|
+
'Keep version.json and properties.json beside its platform bundle and point CAUSYNE_BROWSER_EXECUTABLE at that bundle.');
|
|
277
|
+
}
|
|
278
|
+
let launchExecutable = resolvedExecutable;
|
|
279
|
+
try {
|
|
280
|
+
launchExecutable = ensureLaunchShim(resolvedExecutable, resourceDirectory);
|
|
78
281
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
throw new Error(`Causyne Browser runtime metadata was not found in ${resourceDirectory}.`);
|
|
282
|
+
catch {
|
|
283
|
+
launchExecutable = resolvedExecutable;
|
|
82
284
|
}
|
|
83
285
|
return {
|
|
84
286
|
installDirectory,
|
|
85
287
|
resourceDirectory,
|
|
288
|
+
executablePath: launchExecutable,
|
|
86
289
|
source: configured ? 'environment' : 'system'
|
|
87
290
|
};
|
|
88
291
|
}
|