@phnx-labs/agents-cli 1.22.126 → 1.22.128
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +66 -0
- package/README.md +6 -15
- package/dist/commands/browser.js +4 -19
- package/dist/commands/computer.d.ts +2 -6
- package/dist/commands/computer.js +12 -18
- package/dist/commands/logs.js +2 -6
- package/dist/commands/sessions.d.ts +0 -11
- package/dist/commands/sessions.js +31 -364
- package/dist/index.js +12 -12
- package/dist/lib/accounting/usage-ingest.js +2 -1
- package/dist/lib/browser-client.d.ts +0 -21
- package/dist/lib/browser-client.js +1 -44
- package/dist/lib/computer/record.d.ts +1 -0
- package/dist/lib/computer/record.js +2 -16
- package/dist/lib/daemon/runner.js +3 -2
- package/dist/lib/device-config.d.ts +1 -1
- package/dist/lib/device-config.js +7 -7
- package/dist/lib/feed/tool-activity.d.ts +22 -10
- package/dist/lib/feed/tool-activity.js +161 -32
- package/dist/lib/feed/tools.d.ts +55 -2
- package/dist/lib/feed/tools.js +22 -28
- package/dist/lib/feed/watch.js +4 -3
- package/dist/lib/session/cloud.d.ts +0 -1
- package/dist/lib/session/cloud.js +0 -5
- package/dist/lib/session/db.d.ts +0 -64
- package/dist/lib/session/db.js +0 -116
- package/dist/lib/session/remote/remote-list.d.ts +2 -36
- package/dist/lib/session/remote/remote-list.js +3 -320
- package/dist/lib/session/tool-index.d.ts +0 -62
- package/dist/lib/session/tool-index.js +1 -537
- package/dist/lib/sessions-client.d.ts +1 -0
- package/dist/lib/sessions-client.js +17 -1
- package/dist/lib/standalone-tools.js +2 -2
- package/dist/lib/stdout.d.ts +1 -0
- package/dist/lib/stdout.js +5 -0
- package/package.json +1 -1
- package/dist/commands/browser-sessions-picker.d.ts +0 -9
- package/dist/commands/browser-sessions-picker.js +0 -136
- package/dist/commands/computer-sessions-picker.d.ts +0 -11
- package/dist/commands/computer-sessions-picker.js +0 -90
- package/dist/commands/sessions-tail.d.ts +0 -13
- package/dist/commands/sessions-tail.js +0 -222
- package/dist/lib/browser/native-history.test-fixture.d.ts +0 -12
- package/dist/lib/browser/native-history.test-fixture.js +0 -43
- package/dist/lib/browser/paths.d.ts +0 -9
- package/dist/lib/browser/paths.js +0 -31
- package/dist/lib/browser/record.d.ts +0 -4
- package/dist/lib/browser/record.js +0 -19
- package/dist/lib/browser/sessions-list.d.ts +0 -79
- package/dist/lib/browser/sessions-list.js +0 -415
- package/dist/lib/computer/sessions-list.d.ts +0 -78
- package/dist/lib/computer/sessions-list.js +0 -372
|
@@ -4,7 +4,6 @@ import * as path from 'node:path';
|
|
|
4
4
|
import { findInPath } from './agent-spec/agents.js';
|
|
5
5
|
const INSTALL_HINT = 'npm i -g @phnx-labs/browser-cli';
|
|
6
6
|
export const BROWSER_CONTEXT_FD = 3;
|
|
7
|
-
export const BROWSER_EVENTS_FD = 4;
|
|
8
7
|
export class BrowserClientError extends Error {
|
|
9
8
|
code;
|
|
10
9
|
constructor(code, message) {
|
|
@@ -58,38 +57,17 @@ export function invocation(bin) {
|
|
|
58
57
|
return { command: process.execPath, prefix: [bin] };
|
|
59
58
|
return { command: bin, prefix: [] };
|
|
60
59
|
}
|
|
61
|
-
export function parseEventLines(buffer) {
|
|
62
|
-
const events = [];
|
|
63
|
-
const parts = buffer.split('\n');
|
|
64
|
-
const rest = parts.pop() ?? '';
|
|
65
|
-
for (const line of parts) {
|
|
66
|
-
const trimmed = line.trim();
|
|
67
|
-
if (!trimmed)
|
|
68
|
-
continue;
|
|
69
|
-
try {
|
|
70
|
-
const parsed = JSON.parse(trimmed);
|
|
71
|
-
if (parsed && typeof parsed === 'object' && typeof parsed.command === 'string') {
|
|
72
|
-
events.push(parsed);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
catch {
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return { events, rest };
|
|
79
|
-
}
|
|
80
60
|
export async function runBrowser(opts) {
|
|
81
61
|
const bin = resolveBrowserBin();
|
|
82
62
|
const { command, prefix } = invocation(bin);
|
|
83
63
|
const child = spawn(command, [...prefix, ...opts.argv], {
|
|
84
|
-
stdio: ['inherit', opts.capture ? 'pipe' : 'inherit', 'inherit', 'pipe'
|
|
64
|
+
stdio: ['inherit', opts.capture ? 'pipe' : 'inherit', 'inherit', 'pipe'],
|
|
85
65
|
env: {
|
|
86
66
|
...process.env,
|
|
87
67
|
BROWSER_CONTEXT_FD: String(BROWSER_CONTEXT_FD),
|
|
88
|
-
BROWSER_EVENTS_FD: String(BROWSER_EVENTS_FD),
|
|
89
68
|
},
|
|
90
69
|
});
|
|
91
70
|
const contextPipe = child.stdio[BROWSER_CONTEXT_FD];
|
|
92
|
-
const eventsPipe = child.stdio[BROWSER_EVENTS_FD];
|
|
93
71
|
if (contextPipe) {
|
|
94
72
|
contextPipe.on('error', () => { });
|
|
95
73
|
contextPipe.end(JSON.stringify(opts.context) + '\n');
|
|
@@ -101,26 +79,6 @@ export async function runBrowser(opts) {
|
|
|
101
79
|
stdout += chunk;
|
|
102
80
|
});
|
|
103
81
|
}
|
|
104
|
-
let pending = '';
|
|
105
|
-
const drained = new Promise((resolve) => {
|
|
106
|
-
if (!eventsPipe)
|
|
107
|
-
return resolve();
|
|
108
|
-
eventsPipe.setEncoding('utf-8');
|
|
109
|
-
eventsPipe.on('data', (chunk) => {
|
|
110
|
-
const { events, rest } = parseEventLines(pending + chunk);
|
|
111
|
-
pending = rest;
|
|
112
|
-
for (const event of events)
|
|
113
|
-
opts.onEvent?.(event);
|
|
114
|
-
});
|
|
115
|
-
eventsPipe.on('error', () => resolve());
|
|
116
|
-
eventsPipe.on('end', () => {
|
|
117
|
-
const { events } = parseEventLines(pending.endsWith('\n') ? pending : pending + '\n');
|
|
118
|
-
pending = '';
|
|
119
|
-
for (const event of events)
|
|
120
|
-
opts.onEvent?.(event);
|
|
121
|
-
resolve();
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
82
|
const exitCode = await new Promise((resolve, reject) => {
|
|
125
83
|
child.on('error', (err) => {
|
|
126
84
|
reject(new BrowserClientError('BROWSER_SPAWN_FAILED', `Could not run \`${bin}\`: ${err.message}`));
|
|
@@ -131,7 +89,6 @@ export async function runBrowser(opts) {
|
|
|
131
89
|
resolve(code);
|
|
132
90
|
});
|
|
133
91
|
});
|
|
134
|
-
await drained;
|
|
135
92
|
return { exitCode, stdout };
|
|
136
93
|
}
|
|
137
94
|
function osSignalNumber(signal) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ComputerActionEvent } from '../computer-client.js';
|
|
2
|
+
export declare const TASK_PREVIEW_MAX_CHARS = 200;
|
|
2
3
|
export declare const COMPUTER_INVOCATION_ID: `${string}-${string}-${string}-${string}-${string}`;
|
|
3
4
|
export declare function recordComputerAction(event: ComputerActionEvent, opts?: {
|
|
4
5
|
device?: string;
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
2
|
import { emit as emitEvent } from '../feed/events.js';
|
|
3
|
-
import { recordComputerSession } from '../session/db.js';
|
|
4
|
-
import { resolveActor } from '../actor.js';
|
|
5
3
|
import { truncate } from '../feed/events.js';
|
|
6
|
-
|
|
4
|
+
export const TASK_PREVIEW_MAX_CHARS = 200;
|
|
7
5
|
export const COMPUTER_INVOCATION_ID = randomUUID();
|
|
8
6
|
export function recordComputerAction(event, opts = {}) {
|
|
9
|
-
const { event: _kind, command, invocationId, pid: _enginePid, host, sessionId, launchId, actor, ...rest } = event;
|
|
7
|
+
const { event: _kind, command, invocationId, pid: _enginePid, host, sessionId, launchId, actor: _actor, ...rest } = event;
|
|
10
8
|
const runId = invocationId || COMPUTER_INVOCATION_ID;
|
|
11
9
|
const drivenHost = host ?? opts.device;
|
|
12
10
|
const extra = typeof rest.task === 'string'
|
|
@@ -24,16 +22,4 @@ export function recordComputerAction(event, opts = {}) {
|
|
|
24
22
|
}
|
|
25
23
|
catch {
|
|
26
24
|
}
|
|
27
|
-
try {
|
|
28
|
-
recordComputerSession({
|
|
29
|
-
invocationId: runId,
|
|
30
|
-
sessionId: sessionId ?? process.env.AGENT_SESSION_ID ?? process.env.AGENTS_SESSION_ID,
|
|
31
|
-
launchId: launchId ?? process.env.AGENT_LAUNCH_ID,
|
|
32
|
-
actor: actor ?? resolveActor().id,
|
|
33
|
-
actionCount: 1,
|
|
34
|
-
taskPreview: typeof extra.task === 'string' ? extra.task : undefined,
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
catch {
|
|
38
|
-
}
|
|
39
25
|
}
|
|
@@ -596,12 +596,13 @@ export function claudeVersionIsAuthenticated(version) {
|
|
|
596
596
|
const binary = getBinaryPath('claude', version);
|
|
597
597
|
if (!binary)
|
|
598
598
|
return false;
|
|
599
|
-
const
|
|
599
|
+
const env = buildRoutineSpawnEnv(process.env, 'claude', version);
|
|
600
|
+
env.HOME = process.env.AGENTS_REAL_HOME || os.homedir();
|
|
600
601
|
try {
|
|
601
602
|
const raw = execFileSync(binary, ['auth', 'status', '--json'], {
|
|
602
603
|
encoding: 'utf8',
|
|
603
604
|
timeout: 5_000,
|
|
604
|
-
env
|
|
605
|
+
env,
|
|
605
606
|
stdio: ['ignore', 'pipe', 'ignore'],
|
|
606
607
|
});
|
|
607
608
|
return JSON.parse(raw).loggedIn === true;
|
|
@@ -29,7 +29,7 @@ interface ConfigTarget {
|
|
|
29
29
|
}
|
|
30
30
|
declare const DEVICE_ROLES: readonly ["worker", "personal", "desktop"];
|
|
31
31
|
declare const AUTO_POOL_MODES: readonly ["workers", "all"];
|
|
32
|
-
export declare const MENUBAR_TABS: readonly ["home", "goals", "projects", "sessions", "inbox"];
|
|
32
|
+
export declare const MENUBAR_TABS: readonly ["home", "goals", "secrets", "projects", "sessions", "inbox"];
|
|
33
33
|
export declare const MENUBAR_HOME_GOALS: readonly ["company", "week", "myWeek", "myDay"];
|
|
34
34
|
export declare function resolveMenubarTabOrder(stored: readonly string[]): string[];
|
|
35
35
|
export declare const CONFIG_KEYS: readonly ConfigKeySpec[];
|
|
@@ -14,8 +14,8 @@ const SSH_AUTH_METHODS = ['key', 'password'];
|
|
|
14
14
|
const DEVICE_ROLES = ['worker', 'personal', 'desktop'];
|
|
15
15
|
const AUTO_POOL_MODES = ['workers', 'all'];
|
|
16
16
|
const DEVICE_FORM_FACTORS = ['laptop', 'desktop', 'server', 'unknown'];
|
|
17
|
-
export const MENUBAR_TABS = ['home', 'goals', 'projects', 'sessions', 'inbox'];
|
|
18
|
-
const MENUBAR_TABS_ADDED_LATER = ['goals'];
|
|
17
|
+
export const MENUBAR_TABS = ['home', 'goals', 'secrets', 'projects', 'sessions', 'inbox'];
|
|
18
|
+
const MENUBAR_TABS_ADDED_LATER = ['goals', 'secrets'];
|
|
19
19
|
export const MENUBAR_HOME_GOALS = ['company', 'week', 'myWeek', 'myDay'];
|
|
20
20
|
export function resolveMenubarTabOrder(stored) {
|
|
21
21
|
const known = MENUBAR_TABS;
|
|
@@ -550,8 +550,8 @@ export const CONFIG_KEYS = [
|
|
|
550
550
|
type: 'string-list',
|
|
551
551
|
defaultValue: [...MENUBAR_TABS],
|
|
552
552
|
description: `AGI Menu: the order of the tab bar — every one of ${MENUBAR_TABS.join(', ')} exactly once. ` +
|
|
553
|
-
'Settings is always last and is not listed. An order saved before Goals existed may omit
|
|
554
|
-
'
|
|
553
|
+
'Settings is always last and is not listed. An order saved before Goals or Secrets existed may omit them; ' +
|
|
554
|
+
'they read back appended. Hiding a tab is menubar.menu.hiddenTabs, not this key.',
|
|
555
555
|
validate: (v) => {
|
|
556
556
|
const tabs = v;
|
|
557
557
|
const required = MENUBAR_TABS.filter((t) => !MENUBAR_TABS_ADDED_LATER.includes(t));
|
|
@@ -561,7 +561,7 @@ export const CONFIG_KEYS = [
|
|
|
561
561
|
return ok
|
|
562
562
|
? null
|
|
563
563
|
: `menubar.menu.tabOrder must list each of ${MENUBAR_TABS.join(', ')} exactly once ` +
|
|
564
|
-
`(${MENUBAR_TABS_ADDED_LATER.join(', ')} may be left out and
|
|
564
|
+
`(${MENUBAR_TABS_ADDED_LATER.join(', ')} may be left out and are appended).`;
|
|
565
565
|
},
|
|
566
566
|
normalize: (v) => (Array.isArray(v) ? resolveMenubarTabOrder(v.map(String)) : v),
|
|
567
567
|
},
|
|
@@ -570,9 +570,9 @@ export const CONFIG_KEYS = [
|
|
|
570
570
|
yamlKey: 'menubarMenuHiddenTabs',
|
|
571
571
|
scope: 'user',
|
|
572
572
|
type: 'string-list',
|
|
573
|
-
defaultValue: [],
|
|
573
|
+
defaultValue: ['secrets'],
|
|
574
574
|
description: `AGI Menu: tabs hidden from the tab bar — any of ${MENUBAR_TABS.join(', ')}, but not all of them. ` +
|
|
575
|
-
'Settings cannot be hidden. Hiding Inbox loses nothing: its requests still show on Home.',
|
|
575
|
+
'Settings cannot be hidden. Hiding Inbox loses nothing: its requests still show on Home. Secrets is hidden unless this list is set without it.',
|
|
576
576
|
validate: (v) => {
|
|
577
577
|
const tabs = v;
|
|
578
578
|
const bad = tabs.find((t) => !MENUBAR_TABS.includes(t));
|
|
@@ -1,15 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import type
|
|
4
|
-
import { type ToolRow } from './tools.js';
|
|
1
|
+
import type { SessionMeta } from '@phnx-labs/sessions-cli/reader';
|
|
2
|
+
import type { BrowserSessionRow, ComputerRunRow, LiveBrowserTask } from './tools.js';
|
|
3
|
+
import { type ToolKind, type ToolRow } from './tools.js';
|
|
5
4
|
export declare const TOOL_SWEEP_MS = 5000;
|
|
5
|
+
type StandaloneTool = 'browser' | 'computer';
|
|
6
|
+
export declare class ToolNotInstalledError extends Error {
|
|
7
|
+
}
|
|
8
|
+
/** Runs a standalone tool's `--json` listing; any failure rejects so the snapshot is reported incomplete. */
|
|
9
|
+
export declare function readToolJson(tool: StandaloneTool, argv: string[]): Promise<unknown[]>;
|
|
10
|
+
export declare function readStandaloneBrowserRows(): Promise<BrowserSessionRow[]>;
|
|
11
|
+
export declare function readStandaloneComputerRows(): Promise<ComputerRunRow[]>;
|
|
12
|
+
/** The tools record a session or launch id; only agents-cli's index can say which agent session that was. */
|
|
13
|
+
export declare function linkToolSessions<T extends {
|
|
14
|
+
sessionId?: string;
|
|
15
|
+
launchId?: string;
|
|
16
|
+
linkedSession?: SessionMeta;
|
|
17
|
+
}>(rows: T[]): T[];
|
|
6
18
|
export interface ToolDiff {
|
|
7
19
|
upserts: ToolRow[];
|
|
8
20
|
removes: string[];
|
|
9
21
|
}
|
|
10
22
|
interface ToolSources {
|
|
11
|
-
browserRows?: () => BrowserSessionRow[]
|
|
12
|
-
computerRows?: () => ComputerRunRow[]
|
|
23
|
+
browserRows?: () => BrowserSessionRow[] | Promise<BrowserSessionRow[]>;
|
|
24
|
+
computerRows?: () => ComputerRunRow[] | Promise<ComputerRunRow[]>;
|
|
13
25
|
bindings?: () => Array<{
|
|
14
26
|
name: string;
|
|
15
27
|
device?: string;
|
|
@@ -23,12 +35,13 @@ interface ToolSources {
|
|
|
23
35
|
}
|
|
24
36
|
export interface ToolSnapshot {
|
|
25
37
|
rows: ToolRow[];
|
|
26
|
-
|
|
38
|
+
incomplete: ToolKind[];
|
|
39
|
+
retry: boolean;
|
|
27
40
|
}
|
|
28
|
-
export declare function collectToolRows(scope: string, sources?: ToolSources): ToolSnapshot
|
|
41
|
+
export declare function collectToolRows(scope: string, sources?: ToolSources): Promise<ToolSnapshot>;
|
|
29
42
|
export declare class ToolRowSet {
|
|
30
43
|
private readonly rows;
|
|
31
|
-
diff(next: ToolRow[]): ToolDiff;
|
|
44
|
+
diff(next: ToolRow[], unreadKinds?: readonly ToolKind[]): ToolDiff;
|
|
32
45
|
reset(rows: ToolRow[]): void;
|
|
33
46
|
}
|
|
34
47
|
interface ToolWatchOptions {
|
|
@@ -40,7 +53,6 @@ interface ToolWatchOptions {
|
|
|
40
53
|
sources?: ToolSources;
|
|
41
54
|
initial?: ToolRow[];
|
|
42
55
|
}
|
|
43
|
-
export declare function toolWatchRoots(): string[];
|
|
44
56
|
export declare function readLiveBrowserTasks(root?: string): LiveBrowserTask[];
|
|
45
57
|
export declare function watchToolActivity(options: ToolWatchOptions): {
|
|
46
58
|
armed: () => boolean;
|
|
@@ -1,31 +1,149 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
1
2
|
import * as fs from 'node:fs';
|
|
2
3
|
import * as path from 'node:path';
|
|
3
|
-
import { getBrowserRuntimeDir,
|
|
4
|
+
import { getBrowserRuntimeDir, getCacheDir } from '../state.js';
|
|
4
5
|
import { getEventsDir } from './events.js';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
6
|
+
import { readRecentActivity } from './activity.js';
|
|
7
|
+
import { invocation as browserInvocation, resolveBrowserBin } from '../browser-client.js';
|
|
8
|
+
import { invocation as computerInvocation, resolveComputerBin } from '../computer-client.js';
|
|
9
|
+
import { getSessionById } from '../session/db.js';
|
|
10
|
+
import { loadHookSessionIndex } from '../session/hook-sessions.js';
|
|
11
|
+
import { listPidSessionEntries } from '../session/pid-registry.js';
|
|
7
12
|
import { boundBrowserRow, projectBrowserToolRow, projectComputerToolRow, sortToolRows } from './tools.js';
|
|
8
13
|
export const TOOL_SWEEP_MS = 5_000;
|
|
9
14
|
const TOOL_COMPUTER_LIMIT = 500;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
15
|
+
const TOOL_JSON_MAX_BYTES = 64 * 1024 * 1024;
|
|
16
|
+
const TOOL_JSON_TIMEOUT_MS = 30_000;
|
|
17
|
+
export class ToolNotInstalledError extends Error {
|
|
18
|
+
}
|
|
19
|
+
function resolveTool(tool) {
|
|
20
|
+
let bin;
|
|
21
|
+
try {
|
|
22
|
+
bin = tool === 'browser' ? resolveBrowserBin() : resolveComputerBin();
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
throw new ToolNotInstalledError(`${error.message}\nRun \`agents setup tools\` to install the pinned \`${tool}\` CLI.`);
|
|
26
|
+
}
|
|
27
|
+
return tool === 'browser' ? browserInvocation(bin) : computerInvocation(bin);
|
|
28
|
+
}
|
|
29
|
+
/** Runs a standalone tool's `--json` listing; any failure rejects so the snapshot is reported incomplete. */
|
|
30
|
+
export async function readToolJson(tool, argv) {
|
|
31
|
+
const { command, prefix } = resolveTool(tool);
|
|
32
|
+
const label = `\`${tool} ${argv.join(' ')}\``;
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
const child = spawn(command, [...prefix, ...argv], { stdio: ['ignore', 'pipe', 'pipe'] });
|
|
35
|
+
const stdout = [];
|
|
36
|
+
let stdoutBytes = 0;
|
|
37
|
+
let stderr = '';
|
|
38
|
+
let failure;
|
|
39
|
+
const fail = (error) => { failure ??= error; child.kill('SIGKILL'); };
|
|
40
|
+
const timer = setTimeout(() => fail(new Error(`${label} did not finish within ${TOOL_JSON_TIMEOUT_MS / 1000}s`)), TOOL_JSON_TIMEOUT_MS);
|
|
41
|
+
child.stdout.on('data', (chunk) => {
|
|
42
|
+
stdoutBytes += chunk.length;
|
|
43
|
+
if (stdoutBytes > TOOL_JSON_MAX_BYTES)
|
|
44
|
+
fail(new Error(`${label} printed more than ${TOOL_JSON_MAX_BYTES} bytes`));
|
|
45
|
+
else
|
|
46
|
+
stdout.push(chunk);
|
|
47
|
+
});
|
|
48
|
+
child.stderr.on('data', (chunk) => { if (stderr.length < 4096)
|
|
49
|
+
stderr += chunk.toString('utf8'); });
|
|
50
|
+
child.on('error', (error) => fail(new Error(`${label} failed: ${error.message}`)));
|
|
51
|
+
child.on('close', (code, signal) => {
|
|
52
|
+
clearTimeout(timer);
|
|
53
|
+
if (failure)
|
|
54
|
+
return reject(failure);
|
|
55
|
+
if (code !== 0)
|
|
56
|
+
return reject(new Error(`${label} exited ${code ?? signal}: ${stderr.trim().slice(0, 500)}`));
|
|
57
|
+
let value;
|
|
58
|
+
try {
|
|
59
|
+
value = JSON.parse(Buffer.concat(stdout).toString('utf8'));
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
return reject(new Error(`${label} printed unparseable JSON: ${error.message}`));
|
|
63
|
+
}
|
|
64
|
+
if (!Array.isArray(value))
|
|
65
|
+
return reject(new Error(`${label} printed ${typeof value}, expected an array of rows`));
|
|
66
|
+
resolve(value);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
function isRecord(value) {
|
|
71
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
72
|
+
}
|
|
73
|
+
export async function readStandaloneBrowserRows() {
|
|
74
|
+
return (await readToolJson('browser', ['sessions', '--tasks', '--json', '--no-interactive'])).map((row, index) => {
|
|
75
|
+
if (!isRecord(row) || typeof row.profile !== 'string' || !Array.isArray(row.artifacts)
|
|
76
|
+
|| !isRecord(row.counts) || typeof row.latestMtimeMs !== 'number') {
|
|
77
|
+
throw new Error(`\`browser sessions --json\` row ${index} lacks profile, artifacts, counts or latestMtimeMs`);
|
|
78
|
+
}
|
|
79
|
+
return row;
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
export async function readStandaloneComputerRows() {
|
|
83
|
+
return (await readToolJson('computer', ['sessions', '--json', '--no-interactive', '--limit', String(TOOL_COMPUTER_LIMIT)])).map((row, index) => {
|
|
84
|
+
if (!isRecord(row) || typeof row.machine !== 'string' || !Array.isArray(row.actions)
|
|
85
|
+
|| !isRecord(row.counts) || typeof row.startMs !== 'number' || typeof row.endMs !== 'number') {
|
|
86
|
+
throw new Error(`\`computer sessions --json\` row ${index} lacks machine, actions, counts or startMs/endMs`);
|
|
87
|
+
}
|
|
88
|
+
return row;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
function buildLaunchSessionIndex() {
|
|
92
|
+
const byLaunchId = new Map();
|
|
93
|
+
for (const event of readRecentActivity({ maxBytesPerSession: 64 * 1024 })) {
|
|
94
|
+
if (event.launchId && event.sessionId)
|
|
95
|
+
byLaunchId.set(event.launchId, event.sessionId);
|
|
96
|
+
}
|
|
97
|
+
for (const [launchId, record] of loadHookSessionIndex().byLaunchId) {
|
|
98
|
+
if (record.session_id)
|
|
99
|
+
byLaunchId.set(launchId, record.session_id);
|
|
100
|
+
}
|
|
101
|
+
for (const entry of listPidSessionEntries()) {
|
|
102
|
+
if (entry.launchId && entry.sessionId)
|
|
103
|
+
byLaunchId.set(entry.launchId, entry.sessionId);
|
|
104
|
+
}
|
|
105
|
+
return byLaunchId;
|
|
106
|
+
}
|
|
107
|
+
/** The tools record a session or launch id; only agents-cli's index can say which agent session that was. */
|
|
108
|
+
export function linkToolSessions(rows) {
|
|
109
|
+
let byLaunchId;
|
|
110
|
+
return rows.map((row) => {
|
|
111
|
+
let linked = row.sessionId ? getSessionById(row.sessionId) : null;
|
|
112
|
+
if (!linked && row.launchId) {
|
|
113
|
+
byLaunchId ??= buildLaunchSessionIndex();
|
|
114
|
+
const sessionId = byLaunchId.get(row.launchId);
|
|
115
|
+
linked = sessionId ? getSessionById(sessionId) : null;
|
|
116
|
+
}
|
|
117
|
+
return linked ? { ...row, linkedSession: linked } : row;
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
export async function collectToolRows(scope, sources = {}) {
|
|
121
|
+
const incomplete = new Set();
|
|
122
|
+
let retry = false;
|
|
123
|
+
const read = async (kind, source, empty) => {
|
|
13
124
|
try {
|
|
14
|
-
return source();
|
|
125
|
+
return await source();
|
|
15
126
|
}
|
|
16
|
-
catch {
|
|
17
|
-
|
|
127
|
+
catch (error) {
|
|
128
|
+
incomplete.add(kind);
|
|
129
|
+
if (!(error instanceof ToolNotInstalledError))
|
|
130
|
+
retry = true;
|
|
18
131
|
return empty;
|
|
19
132
|
}
|
|
20
133
|
};
|
|
134
|
+
const [bindingList, liveTaskList, browserRows, computerRows] = await Promise.all([
|
|
135
|
+
read('browser', sources.bindings ?? (() => []), []),
|
|
136
|
+
read('browser', sources.liveTasks ?? (() => readLiveBrowserTasks()), []),
|
|
137
|
+
read('browser', sources.browserRows ?? (async () => linkToolSessions(await readStandaloneBrowserRows())), []),
|
|
138
|
+
read('computer', sources.computerRows ?? (async () => linkToolSessions(await readStandaloneComputerRows())), []),
|
|
139
|
+
]);
|
|
21
140
|
const bindings = new Map();
|
|
22
|
-
for (const binding of
|
|
141
|
+
for (const binding of bindingList)
|
|
23
142
|
bindings.set(binding.name, binding);
|
|
24
143
|
const liveTasks = new Map();
|
|
25
|
-
for (const task of
|
|
144
|
+
for (const task of liveTaskList)
|
|
26
145
|
liveTasks.set(task.task, task);
|
|
27
146
|
const rows = [];
|
|
28
|
-
const browserRows = read(sources.browserRows ?? (() => readBrowserSessionRows()), []);
|
|
29
147
|
const captured = new Set();
|
|
30
148
|
for (const row of browserRows) {
|
|
31
149
|
if (row.task)
|
|
@@ -39,27 +157,27 @@ export function collectToolRows(scope, sources = {}) {
|
|
|
39
157
|
const live = liveTasks.get(task);
|
|
40
158
|
rows.push(projectBrowserToolRow(scope, boundBrowserRow(task, live ?? binding ?? {}), binding, live));
|
|
41
159
|
}
|
|
42
|
-
const computerRows = read(sources.computerRows ?? (() => buildComputerSessionRows({ limit: TOOL_COMPUTER_LIMIT, observer: scope })), []);
|
|
43
160
|
for (const row of computerRows)
|
|
44
161
|
rows.push(projectComputerToolRow(scope, row));
|
|
45
|
-
return { rows: sortToolRows(rows),
|
|
162
|
+
return { rows: sortToolRows(rows.filter((row) => !incomplete.has(row.kind))), incomplete: [...incomplete], retry };
|
|
46
163
|
}
|
|
47
164
|
export class ToolRowSet {
|
|
48
165
|
rows = new Map();
|
|
49
|
-
|
|
166
|
+
// A kind whose read failed keeps the rows it last published: a failed read is not "every task closed".
|
|
167
|
+
diff(next, unreadKinds = []) {
|
|
50
168
|
const upserts = [];
|
|
51
169
|
const seen = new Set();
|
|
52
170
|
for (const row of next) {
|
|
53
171
|
seen.add(row.rowKey);
|
|
54
172
|
const serialized = JSON.stringify(row);
|
|
55
|
-
if (this.rows.get(row.rowKey) === serialized)
|
|
173
|
+
if (this.rows.get(row.rowKey)?.serialized === serialized)
|
|
56
174
|
continue;
|
|
57
|
-
this.rows.set(row.rowKey, serialized);
|
|
175
|
+
this.rows.set(row.rowKey, { kind: row.kind, serialized });
|
|
58
176
|
upserts.push(row);
|
|
59
177
|
}
|
|
60
178
|
const removes = [];
|
|
61
|
-
for (const key of [...this.rows
|
|
62
|
-
if (seen.has(key))
|
|
179
|
+
for (const [key, { kind }] of [...this.rows]) {
|
|
180
|
+
if (seen.has(key) || unreadKinds.includes(kind))
|
|
63
181
|
continue;
|
|
64
182
|
this.rows.delete(key);
|
|
65
183
|
removes.push(key);
|
|
@@ -69,11 +187,13 @@ export class ToolRowSet {
|
|
|
69
187
|
reset(rows) {
|
|
70
188
|
this.rows.clear();
|
|
71
189
|
for (const row of rows)
|
|
72
|
-
this.rows.set(row.rowKey, JSON.stringify(row));
|
|
190
|
+
this.rows.set(row.rowKey, { kind: row.kind, serialized: JSON.stringify(row) });
|
|
73
191
|
}
|
|
74
192
|
}
|
|
75
|
-
|
|
76
|
-
|
|
193
|
+
// Only roots the tools append to while they act: their history databases are
|
|
194
|
+
// written by the very listings this watcher runs, so watching those would loop.
|
|
195
|
+
function toolWatchRoots() {
|
|
196
|
+
return [getBrowserRuntimeDir(), getEventsDir(), path.join(getCacheDir(), 'computer', 'actions')];
|
|
77
197
|
}
|
|
78
198
|
function readLiveTasksFor(profileDir) {
|
|
79
199
|
const file = path.join(profileDir, 'tasks.json');
|
|
@@ -171,24 +291,33 @@ export function watchToolActivity(options) {
|
|
|
171
291
|
for (const root of roots)
|
|
172
292
|
armRoot(root);
|
|
173
293
|
const armed = () => [...watchers.values()].every((watcher) => watcher !== undefined);
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
294
|
+
let projecting = false;
|
|
295
|
+
const reproject = async () => {
|
|
296
|
+
projecting = true;
|
|
297
|
+
try {
|
|
298
|
+
const snapshot = await collectToolRows(options.scope, options.sources);
|
|
299
|
+
if (stopped)
|
|
300
|
+
return;
|
|
301
|
+
if (snapshot.retry)
|
|
302
|
+
dirty = true;
|
|
303
|
+
const diff = set.diff(snapshot.rows, snapshot.incomplete);
|
|
304
|
+
if (diff.upserts.length > 0 || diff.removes.length > 0)
|
|
305
|
+
options.onDiff(diff);
|
|
306
|
+
}
|
|
307
|
+
finally {
|
|
308
|
+
projecting = false;
|
|
179
309
|
}
|
|
180
|
-
const diff = set.diff(snapshot.rows);
|
|
181
|
-
if (diff.upserts.length > 0 || diff.removes.length > 0)
|
|
182
|
-
options.onDiff(diff);
|
|
183
310
|
};
|
|
184
311
|
const timer = setInterval(() => {
|
|
185
312
|
for (const root of roots)
|
|
186
313
|
armRoot(root);
|
|
187
|
-
if (armed() && !dirty)
|
|
314
|
+
if (projecting || (armed() && !dirty))
|
|
188
315
|
return;
|
|
189
316
|
dirty = false;
|
|
190
|
-
reproject();
|
|
317
|
+
void reproject();
|
|
191
318
|
}, options.sweepMs ?? TOOL_SWEEP_MS);
|
|
319
|
+
if (!options.initial)
|
|
320
|
+
void reproject();
|
|
192
321
|
const stop = () => {
|
|
193
322
|
stopped = true;
|
|
194
323
|
clearInterval(timer);
|
package/dist/lib/feed/tools.d.ts
CHANGED
|
@@ -1,5 +1,58 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type { SessionMeta } from '@phnx-labs/sessions-cli/reader';
|
|
2
|
+
export type ArtifactKind = 'screenshot' | 'pdf' | 'recording' | 'download';
|
|
3
|
+
export interface BrowserArtifact {
|
|
4
|
+
kind: ArtifactKind;
|
|
5
|
+
task?: string;
|
|
6
|
+
name: string;
|
|
7
|
+
path: string;
|
|
8
|
+
bytes: number;
|
|
9
|
+
mtimeMs: number;
|
|
10
|
+
}
|
|
11
|
+
export interface BrowserSessionRow {
|
|
12
|
+
kind: 'task' | 'downloads';
|
|
13
|
+
profile: string;
|
|
14
|
+
task?: string;
|
|
15
|
+
launchId?: string;
|
|
16
|
+
sessionId?: string;
|
|
17
|
+
linkedSession?: SessionMeta;
|
|
18
|
+
artifacts: BrowserArtifact[];
|
|
19
|
+
counts: Record<ArtifactKind, number>;
|
|
20
|
+
latestMtimeMs: number;
|
|
21
|
+
startedAt?: number;
|
|
22
|
+
machine?: string;
|
|
23
|
+
captureDir?: string;
|
|
24
|
+
capturesRemote?: string;
|
|
25
|
+
}
|
|
26
|
+
export interface ComputerAction {
|
|
27
|
+
verb: string;
|
|
28
|
+
tsMs: number;
|
|
29
|
+
host?: string;
|
|
30
|
+
bundle?: string;
|
|
31
|
+
capture?: {
|
|
32
|
+
kind: 'screenshot';
|
|
33
|
+
path: string;
|
|
34
|
+
name: string;
|
|
35
|
+
bytes?: number;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export interface ComputerRunRow {
|
|
39
|
+
pid?: number;
|
|
40
|
+
invocationId?: string;
|
|
41
|
+
task?: string;
|
|
42
|
+
machine: string;
|
|
43
|
+
remoteHost?: string;
|
|
44
|
+
bundle?: string;
|
|
45
|
+
sessionId?: string;
|
|
46
|
+
launchId?: string;
|
|
47
|
+
linkedSession?: SessionMeta;
|
|
48
|
+
actions: ComputerAction[];
|
|
49
|
+
counts: Record<string, number>;
|
|
50
|
+
recoveredActionCount?: number;
|
|
51
|
+
captures?: ComputerAction[];
|
|
52
|
+
captureCount?: number;
|
|
53
|
+
startMs: number;
|
|
54
|
+
endMs: number;
|
|
55
|
+
}
|
|
3
56
|
export declare const TOOL_CAPTURE_LIMIT = 20;
|
|
4
57
|
export declare const TOOL_ACTION_LIMIT = 50;
|
|
5
58
|
export type ToolKind = 'browser' | 'computer';
|
package/dist/lib/feed/tools.js
CHANGED
|
@@ -3,15 +3,16 @@
|
|
|
3
3
|
* projected into one row contract, alongside the agent and attention rows.
|
|
4
4
|
*
|
|
5
5
|
* WHY A PROJECTION AND NOT A NEW STORE. Both sources already exist and already
|
|
6
|
-
* have an owner: `
|
|
7
|
-
* task-first rows
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* tool
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
6
|
+
* have an owner: the standalone `browser sessions --tasks --json` groups a
|
|
7
|
+
* profile's captures into task-first rows, and the standalone
|
|
8
|
+
* `computer sessions --json` groups its action ledger into run rows. A menu-bar
|
|
9
|
+
* or Fleet view that wanted the same information had only one way to get it:
|
|
10
|
+
* shell out to those two commands, per tool, per device, on a timer. That is the
|
|
11
|
+
* per-tool polling this module removes — the rows ride the ONE stream
|
|
12
|
+
* `agents feed watch --json` already holds open, so a consumer switching its
|
|
13
|
+
* All/Agents/Browser/Computer filter spawns zero commands. {@link BrowserSessionRow}
|
|
14
|
+
* and {@link ComputerRunRow} are those commands' JSON rows, plus the agent session
|
|
15
|
+
* agents-cli resolved for them from its own index.
|
|
15
16
|
*
|
|
16
17
|
* WHAT IS DELIBERATELY DIFFERENT BETWEEN THE TWO KINDS. A browser task is a
|
|
17
18
|
* LIVE resource: it is bound in the task index while it exists, it can be
|
|
@@ -70,12 +71,12 @@ function ownerOf(sessionId, linkedSession, scope) {
|
|
|
70
71
|
};
|
|
71
72
|
}
|
|
72
73
|
function knownMachine(machine) {
|
|
73
|
-
return machine
|
|
74
|
+
return machine !== 'local' ? machine : undefined;
|
|
74
75
|
}
|
|
75
76
|
export function boundBrowserRow(task, binding) {
|
|
76
77
|
return {
|
|
77
78
|
kind: 'task', task, profile: binding.profile ?? '',
|
|
78
|
-
|
|
79
|
+
artifacts: [],
|
|
79
80
|
counts: { screenshot: 0, pdf: 0, recording: 0, download: 0 },
|
|
80
81
|
latestMtimeMs: 0,
|
|
81
82
|
};
|
|
@@ -125,22 +126,15 @@ export function projectBrowserToolRow(scope, row, binding, live) {
|
|
|
125
126
|
export function projectComputerToolRow(scope, row) {
|
|
126
127
|
const host = normalizeHost(scope);
|
|
127
128
|
const owner = ownerOf(row.sessionId, row.linkedSession, host);
|
|
128
|
-
const agent = row.
|
|
129
|
-
const
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
captures.push({
|
|
138
|
-
kind: action.capture.kind, name: action.capture.name, path: action.capture.path,
|
|
139
|
-
host,
|
|
140
|
-
...(action.capture.bytes !== undefined ? { bytes: action.capture.bytes } : {}),
|
|
141
|
-
atMs: action.tsMs,
|
|
142
|
-
});
|
|
143
|
-
}
|
|
129
|
+
const agent = row.linkedSession?.agent;
|
|
130
|
+
const captured = (row.captures ?? row.actions).flatMap((action) => action.capture ? [{ ...action.capture, atMs: action.tsMs }] : []);
|
|
131
|
+
const captures = captured.slice(0, TOOL_CAPTURE_LIMIT).map((capture) => ({
|
|
132
|
+
kind: capture.kind, name: capture.name, path: capture.path, host,
|
|
133
|
+
...(capture.bytes !== undefined ? { bytes: capture.bytes } : {}),
|
|
134
|
+
atMs: capture.atMs,
|
|
135
|
+
}));
|
|
136
|
+
const captureTotal = row.captureCount ?? captured.length;
|
|
137
|
+
const captureCounts = captureTotal > 0 ? { screenshot: captureTotal } : {};
|
|
144
138
|
const actions = row.actions.slice(0, TOOL_ACTION_LIMIT).map((action) => ({
|
|
145
139
|
verb: action.verb, atMs: action.tsMs,
|
|
146
140
|
...(action.host ? { host: action.host } : {}),
|
|
@@ -157,7 +151,7 @@ export function projectComputerToolRow(scope, row) {
|
|
|
157
151
|
...(row.launchId ? { launchId: row.launchId } : {}),
|
|
158
152
|
...(agent ? { agent } : {}),
|
|
159
153
|
...(owner ? { owner } : {}),
|
|
160
|
-
linkStatus: row.
|
|
154
|
+
linkStatus: row.linkedSession ? 'linked' : (row.sessionId || row.launchId) ? 'unresolved' : 'unlinked',
|
|
161
155
|
startedAtMs: row.startMs,
|
|
162
156
|
updatedAtMs: row.endMs,
|
|
163
157
|
captures,
|