@phnx-labs/agents-cli 1.23.39 → 1.23.41
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 +20 -0
- package/README.md +7 -20
- package/dist/cli/command-registry.js +0 -2
- package/dist/commands/config.js +0 -4
- package/dist/commands/daemon.js +5 -5
- package/dist/commands/events.js +1 -1
- package/dist/commands/lead.js +4 -3
- package/dist/commands/setup.d.ts +1 -1
- package/dist/commands/setup.js +1 -9
- package/dist/commands/ssh.js +4 -5
- package/dist/lib/accounting/usage.d.ts +1 -1
- package/dist/lib/answer-router.d.ts +1 -1
- package/dist/lib/answer-router.js +16 -6
- package/dist/lib/channels/providers/session.js +13 -0
- package/dist/lib/channels/registry.d.ts +1 -1
- package/dist/lib/config-keys.d.ts +1 -1
- package/dist/lib/config-keys.js +1 -4
- package/dist/lib/daemon/daemon.js +1 -6
- package/dist/lib/daemon-services.d.ts +1 -1
- package/dist/lib/daemon-services.js +0 -5
- package/dist/lib/device-config.js +2 -11
- package/dist/lib/feed/answer.js +25 -1
- package/dist/lib/feed/attention.d.ts +8 -0
- package/dist/lib/feed/attention.js +39 -0
- package/dist/lib/feed/tab-delivery.d.ts +27 -0
- package/dist/lib/feed/tab-delivery.js +102 -0
- package/dist/lib/feed/watch.d.ts +3 -0
- package/dist/lib/feed/watch.js +63 -13
- package/dist/lib/hosts/passthrough.js +0 -1
- package/dist/lib/installations/migrate.d.ts +0 -1
- package/dist/lib/installations/migrate.js +0 -17
- package/dist/lib/lead/drafts.d.ts +11 -0
- package/dist/lib/lead/drafts.js +34 -0
- package/dist/lib/lead/host.d.ts +5 -0
- package/dist/lib/lead/host.js +52 -1
- package/dist/lib/lead/paths.js +1 -1
- package/dist/lib/lead/tools.d.ts +1 -0
- package/dist/lib/lead/tools.js +22 -0
- package/dist/lib/menubar/snapshot.d.ts +0 -6
- package/dist/lib/menubar/snapshot.js +0 -18
- package/dist/lib/recordings/settle.d.ts +5 -2
- package/dist/lib/recordings/settle.js +8 -6
- package/dist/lib/session/active.d.ts +2 -1
- package/dist/lib/session/active.js +10 -6
- package/dist/lib/session/db.js +24 -6
- package/dist/lib/session/hook-sessions.d.ts +10 -0
- package/dist/lib/session/hook-sessions.js +22 -10
- package/dist/lib/session/inject-target.d.ts +1 -0
- package/dist/lib/session/inject-target.js +2 -1
- package/dist/lib/session/pid-registry.d.ts +1 -0
- package/dist/lib/session/remote/watch.d.ts +1 -0
- package/dist/lib/startup/command-registry.js +2 -1
- package/dist/lib/state.js +0 -1
- package/dist/lib/terminal/index.d.ts +1 -1
- package/dist/lib/terminal/index.js +1 -1
- package/dist/lib/terminal/inject.d.ts +0 -15
- package/dist/lib/terminal/inject.js +2 -16
- package/dist/lib/terminal/resolve.d.ts +2 -1
- package/dist/lib/terminal/resolve.js +13 -18
- package/dist/lib/types.d.ts +0 -3
- package/package.json +1 -1
- package/dist/commands/setup-watchdog.d.ts +0 -3
- package/dist/commands/setup-watchdog.js +0 -49
- package/dist/commands/watchdog.d.ts +0 -4
- package/dist/commands/watchdog.js +0 -361
- package/dist/lib/daemon/watchdog-service.d.ts +0 -10
- package/dist/lib/daemon/watchdog-service.js +0 -27
- package/dist/lib/session/presence.d.ts +0 -47
- package/dist/lib/session/presence.js +0 -100
- package/dist/lib/watchdog/history.d.ts +0 -20
- package/dist/lib/watchdog/history.js +0 -43
- package/dist/lib/watchdog/log.d.ts +0 -33
- package/dist/lib/watchdog/log.js +0 -117
- package/dist/lib/watchdog/read.d.ts +0 -8
- package/dist/lib/watchdog/read.js +0 -93
- package/dist/lib/watchdog/rotate.d.ts +0 -55
- package/dist/lib/watchdog/rotate.js +0 -220
- package/dist/lib/watchdog/runner.d.ts +0 -100
- package/dist/lib/watchdog/runner.js +0 -727
- package/dist/lib/watchdog/service.d.ts +0 -15
- package/dist/lib/watchdog/service.js +0 -36
- package/dist/lib/watchdog/watchdog-agent.d.ts +0 -8
- package/dist/lib/watchdog/watchdog-agent.js +0 -32
- package/dist/lib/watchdog/watchdog.d.ts +0 -43
- package/dist/lib/watchdog/watchdog.js +0 -116
- package/dist/lib/watchdog/watchdogTail.d.ts +0 -6
- package/dist/lib/watchdog/watchdogTail.js +0 -116
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { type ActiveSession } from '../session/active.js';
|
|
2
|
-
import { type WatchdogThresholds, type WatchdogTickResult } from './runner.js';
|
|
3
|
-
interface WatchdogPassOptions {
|
|
4
|
-
nudge: boolean;
|
|
5
|
-
nudgeText?: string;
|
|
6
|
-
smartAgent?: string;
|
|
7
|
-
thresholds?: WatchdogThresholds;
|
|
8
|
-
allowGhosttyFocus?: boolean;
|
|
9
|
-
sessions?: ActiveSession[];
|
|
10
|
-
stateDir?: string;
|
|
11
|
-
mailboxGc?: boolean;
|
|
12
|
-
}
|
|
13
|
-
export declare function loadWatchdogSessions(): Promise<ActiveSession[]>;
|
|
14
|
-
export declare function runWatchdogPass(opts: WatchdogPassOptions): Promise<WatchdogTickResult>;
|
|
15
|
-
export {};
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import { gcMailbox } from '../mailbox-gc.js';
|
|
3
|
-
import { mailboxIdForActiveSession } from '../mailbox-target.js';
|
|
4
|
-
import { getActiveSessions } from '../session/active.js';
|
|
5
|
-
import { loadLocalActiveSessions } from '../session/session-cache.js';
|
|
6
|
-
import { getRuntimeStateDir } from '../state.js';
|
|
7
|
-
import { runWatchdogTick } from './runner.js';
|
|
8
|
-
export async function loadWatchdogSessions() {
|
|
9
|
-
const loaded = await loadLocalActiveSessions({
|
|
10
|
-
gather: () => getActiveSessions({ localOnly: true }),
|
|
11
|
-
});
|
|
12
|
-
return loaded.sessions;
|
|
13
|
-
}
|
|
14
|
-
function runWatchdogMailboxGc(sessions) {
|
|
15
|
-
const activeBoxIds = new Set(sessions.map(mailboxIdForActiveSession).filter((id) => Boolean(id)));
|
|
16
|
-
try {
|
|
17
|
-
gcMailbox(activeBoxIds);
|
|
18
|
-
}
|
|
19
|
-
catch {
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
export async function runWatchdogPass(opts) {
|
|
23
|
-
const sessions = opts.sessions ?? await loadWatchdogSessions();
|
|
24
|
-
const result = await runWatchdogTick({
|
|
25
|
-
nudge: opts.nudge,
|
|
26
|
-
nudgeText: opts.nudgeText,
|
|
27
|
-
smartAgent: opts.smartAgent,
|
|
28
|
-
thresholds: opts.thresholds,
|
|
29
|
-
allowGhosttyFocus: opts.allowGhosttyFocus,
|
|
30
|
-
stateDir: opts.stateDir ?? path.join(getRuntimeStateDir(), 'watchdog'),
|
|
31
|
-
sessions,
|
|
32
|
-
});
|
|
33
|
-
if (opts.mailboxGc !== false)
|
|
34
|
-
runWatchdogMailboxGc(sessions);
|
|
35
|
-
return result;
|
|
36
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { type WatchdogCandidate, type Decision } from './watchdog.js';
|
|
2
|
-
export type WatchdogAgentDecider = (candidates: WatchdogCandidate[]) => Promise<Map<string, Decision>>;
|
|
3
|
-
type WatchdogAgentRunner = (runTarget: string, prompt: string) => Promise<string>;
|
|
4
|
-
export declare function makeWatchdogAgentDecider(agent: string, opts?: {
|
|
5
|
-
workflowCwd?: string;
|
|
6
|
-
run?: WatchdogAgentRunner;
|
|
7
|
-
}): WatchdogAgentDecider;
|
|
8
|
-
export {};
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { renderWatchdogPrompt, parseWatchdogResponse } from './watchdog.js';
|
|
2
|
-
async function defaultAgentRunner(runTarget, prompt) {
|
|
3
|
-
const [{ execFile }, { promisify }] = await Promise.all([import('child_process'), import('util')]);
|
|
4
|
-
const execFileAsync = promisify(execFile);
|
|
5
|
-
const { stdout } = await execFileAsync('agents', ['run', runTarget, '--mode', 'plan', prompt], {
|
|
6
|
-
encoding: 'utf8',
|
|
7
|
-
maxBuffer: 4 * 1024 * 1024,
|
|
8
|
-
timeout: 120_000,
|
|
9
|
-
});
|
|
10
|
-
return stdout;
|
|
11
|
-
}
|
|
12
|
-
export function makeWatchdogAgentDecider(agent, opts = {}) {
|
|
13
|
-
return async (candidates) => {
|
|
14
|
-
const result = new Map();
|
|
15
|
-
if (candidates.length === 0)
|
|
16
|
-
return result;
|
|
17
|
-
try {
|
|
18
|
-
const { resolveWorkflowRef } = await import('../workflows.js');
|
|
19
|
-
const cwd = opts.workflowCwd || process.cwd();
|
|
20
|
-
const workflowPath = resolveWorkflowRef('watchdog', cwd);
|
|
21
|
-
const runTarget = workflowPath ? 'watchdog' : agent;
|
|
22
|
-
const prompt = renderWatchdogPrompt(candidates);
|
|
23
|
-
const run = opts.run ?? defaultAgentRunner;
|
|
24
|
-
const stdout = await run(runTarget, prompt);
|
|
25
|
-
for (const d of parseWatchdogResponse(stdout))
|
|
26
|
-
result.set(d.terminalId, d);
|
|
27
|
-
}
|
|
28
|
-
catch {
|
|
29
|
-
}
|
|
30
|
-
return result;
|
|
31
|
-
};
|
|
32
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
export interface WatchdogCandidate {
|
|
2
|
-
terminalId: string;
|
|
3
|
-
agentType: 'claude' | 'codex';
|
|
4
|
-
tailLines: string[];
|
|
5
|
-
stalledForMs: number;
|
|
6
|
-
task?: string;
|
|
7
|
-
cwd?: string;
|
|
8
|
-
}
|
|
9
|
-
export interface Decision {
|
|
10
|
-
terminalId: string;
|
|
11
|
-
action: 'nudge' | 'skip';
|
|
12
|
-
text: string;
|
|
13
|
-
reason: string;
|
|
14
|
-
needsHuman?: boolean;
|
|
15
|
-
}
|
|
16
|
-
export type StallStatus = {
|
|
17
|
-
kind: 'active';
|
|
18
|
-
} | {
|
|
19
|
-
kind: 'dormant';
|
|
20
|
-
} | {
|
|
21
|
-
kind: 'opted_out';
|
|
22
|
-
} | {
|
|
23
|
-
kind: 'rate_limited';
|
|
24
|
-
cooldownRemainingMs: number;
|
|
25
|
-
} | {
|
|
26
|
-
kind: 'stalled';
|
|
27
|
-
stalledForMs: number;
|
|
28
|
-
};
|
|
29
|
-
interface ClassifyInput {
|
|
30
|
-
lastActivityMs: number;
|
|
31
|
-
nowMs: number;
|
|
32
|
-
lastNudgeMs: number | null;
|
|
33
|
-
optedOut: boolean;
|
|
34
|
-
stallMs: number;
|
|
35
|
-
cooldownMs: number;
|
|
36
|
-
dormantMs: number;
|
|
37
|
-
}
|
|
38
|
-
export declare function classifyTerminal(input: ClassifyInput): StallStatus;
|
|
39
|
-
export declare const WATCHDOG_SYSTEM_PROMPT = "You are the watchdog for AI coding agents running in terminals. You are given the idle\nsessions on this machine \u2014 each with its originating TASK, how long it has been idle, and\nthe tail of its transcript. Your one job is to tell, for each one, whether it is\nIDLE-BUT-UNFINISHED (it was given a task, went quiet, and has NOT finished or handed it\noff) or IDLE-AND-DONE (it finished, or it genuinely needs the human). Idle-but-unfinished\nis the dangerous state \u2014 the work is most likely to be silently abandoned \u2014 so those get a\nNUDGE that drives them to finish. Everything else is a SKIP.\n\nRead each transcript before judging \u2014 an agent that already reached a decision needs \"do\nit,\" not \"decide.\" Judge from the task + tail, not from keywords.\n\nNUDGE when the agent went idle and could keep going on its own:\n- It asked permission for an obvious or already-authorized next step\n (\"should I proceed?\", \"want me to continue?\", \"shall I run the tests?\").\n- It asked a question it could answer itself from the available context or a\n reasonable default, or by using a tool it already has.\n- It announced an action (\"I'll run X\", \"let me write Y\") but no tool call followed.\n- It already decided what to do, then stalled without doing it.\n- It paused with the task incomplete and no real blocker.\n\nThe nudge text MUST carry context, not shove:\n- Restate the goal and reference the conclusion the agent ALREADY reached.\n- Give ONE concrete next step \u2014 the specific action, the sensible default, or a TOOL it\n forgot it has (e.g. \"computer\" to drive the Mac, \"browser\" for the web,\n \"agents ssh <mac> \\\"computer \u2026\\\"\" to drive a Mac from another box).\n- Split the ask: drive the reversible, goal-advancing part now; flag only a genuinely\n disruptive sub-step for the human.\n- Tell it to use best judgment and finish end-to-end WITHOUT asking again.\n- Imperative, 1-2 sentences, no emojis, under 240 characters.\n\nSKIP in two distinct cases \u2014 mark which with \"needsHuman\":\n- needsHuman=true \u2014 the agent is genuinely blocked on a human (these belong in the user's\n feed): credentials, auth, login, 2FA, or biometric; an irreversible or outward-facing\n action needing sign-off (force-push, delete prod data, publish/release, spend money,\n send an external message) UNLESS the House Rules below authorize it; a real product or\n intent decision with genuine ambiguity (not a trivial default); or you cannot tell what\n the agent is doing.\n- needsHuman=false \u2014 the task is actually complete (idle-and-done). Leave it alone; do NOT\n poke a finished session.\n\nRespond with ONLY a JSON array (no prose, no code fence). Include \"needsHuman\" on every\nskip:\n[{\"terminalId\":\"<id>\",\"action\":\"nudge\"|\"skip\",\"text\":\"<message or empty>\",\"reason\":\"<brief>\",\"needsHuman\":true|false}]";
|
|
40
|
-
export declare function composePromptWithPlaybook(basePrompt: string, playbook: string): string;
|
|
41
|
-
export declare function renderWatchdogPrompt(candidates: WatchdogCandidate[], playbook?: string): string;
|
|
42
|
-
export declare function parseWatchdogResponse(stdout: string): Decision[];
|
|
43
|
-
export {};
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
export function classifyTerminal(input) {
|
|
2
|
-
if (input.optedOut)
|
|
3
|
-
return { kind: 'opted_out' };
|
|
4
|
-
const age = input.nowMs - input.lastActivityMs;
|
|
5
|
-
if (age < input.stallMs)
|
|
6
|
-
return { kind: 'active' };
|
|
7
|
-
if (age > input.dormantMs)
|
|
8
|
-
return { kind: 'dormant' };
|
|
9
|
-
if (input.lastNudgeMs !== null) {
|
|
10
|
-
const sinceNudge = input.nowMs - input.lastNudgeMs;
|
|
11
|
-
if (sinceNudge < input.cooldownMs) {
|
|
12
|
-
return { kind: 'rate_limited', cooldownRemainingMs: input.cooldownMs - sinceNudge };
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
return { kind: 'stalled', stalledForMs: age };
|
|
16
|
-
}
|
|
17
|
-
export const WATCHDOG_SYSTEM_PROMPT = `You are the watchdog for AI coding agents running in terminals. You are given the idle
|
|
18
|
-
sessions on this machine — each with its originating TASK, how long it has been idle, and
|
|
19
|
-
the tail of its transcript. Your one job is to tell, for each one, whether it is
|
|
20
|
-
IDLE-BUT-UNFINISHED (it was given a task, went quiet, and has NOT finished or handed it
|
|
21
|
-
off) or IDLE-AND-DONE (it finished, or it genuinely needs the human). Idle-but-unfinished
|
|
22
|
-
is the dangerous state — the work is most likely to be silently abandoned — so those get a
|
|
23
|
-
NUDGE that drives them to finish. Everything else is a SKIP.
|
|
24
|
-
|
|
25
|
-
Read each transcript before judging — an agent that already reached a decision needs "do
|
|
26
|
-
it," not "decide." Judge from the task + tail, not from keywords.
|
|
27
|
-
|
|
28
|
-
NUDGE when the agent went idle and could keep going on its own:
|
|
29
|
-
- It asked permission for an obvious or already-authorized next step
|
|
30
|
-
("should I proceed?", "want me to continue?", "shall I run the tests?").
|
|
31
|
-
- It asked a question it could answer itself from the available context or a
|
|
32
|
-
reasonable default, or by using a tool it already has.
|
|
33
|
-
- It announced an action ("I'll run X", "let me write Y") but no tool call followed.
|
|
34
|
-
- It already decided what to do, then stalled without doing it.
|
|
35
|
-
- It paused with the task incomplete and no real blocker.
|
|
36
|
-
|
|
37
|
-
The nudge text MUST carry context, not shove:
|
|
38
|
-
- Restate the goal and reference the conclusion the agent ALREADY reached.
|
|
39
|
-
- Give ONE concrete next step — the specific action, the sensible default, or a TOOL it
|
|
40
|
-
forgot it has (e.g. "computer" to drive the Mac, "browser" for the web,
|
|
41
|
-
"agents ssh <mac> \\"computer …\\"" to drive a Mac from another box).
|
|
42
|
-
- Split the ask: drive the reversible, goal-advancing part now; flag only a genuinely
|
|
43
|
-
disruptive sub-step for the human.
|
|
44
|
-
- Tell it to use best judgment and finish end-to-end WITHOUT asking again.
|
|
45
|
-
- Imperative, 1-2 sentences, no emojis, under 240 characters.
|
|
46
|
-
|
|
47
|
-
SKIP in two distinct cases — mark which with "needsHuman":
|
|
48
|
-
- needsHuman=true — the agent is genuinely blocked on a human (these belong in the user's
|
|
49
|
-
feed): credentials, auth, login, 2FA, or biometric; an irreversible or outward-facing
|
|
50
|
-
action needing sign-off (force-push, delete prod data, publish/release, spend money,
|
|
51
|
-
send an external message) UNLESS the House Rules below authorize it; a real product or
|
|
52
|
-
intent decision with genuine ambiguity (not a trivial default); or you cannot tell what
|
|
53
|
-
the agent is doing.
|
|
54
|
-
- needsHuman=false — the task is actually complete (idle-and-done). Leave it alone; do NOT
|
|
55
|
-
poke a finished session.
|
|
56
|
-
|
|
57
|
-
Respond with ONLY a JSON array (no prose, no code fence). Include "needsHuman" on every
|
|
58
|
-
skip:
|
|
59
|
-
[{"terminalId":"<id>","action":"nudge"|"skip","text":"<message or empty>","reason":"<brief>","needsHuman":true|false}]`;
|
|
60
|
-
export function composePromptWithPlaybook(basePrompt, playbook) {
|
|
61
|
-
const trimmed = playbook.trim();
|
|
62
|
-
if (!trimmed)
|
|
63
|
-
return basePrompt;
|
|
64
|
-
return `${basePrompt}\n\n## House Rules (user playbook)\n\n${trimmed}`;
|
|
65
|
-
}
|
|
66
|
-
export function renderWatchdogPrompt(candidates, playbook = '') {
|
|
67
|
-
const systemPrompt = composePromptWithPlaybook(WATCHDOG_SYSTEM_PROMPT, playbook);
|
|
68
|
-
const parts = [systemPrompt, '', 'IDLE SESSIONS:', ''];
|
|
69
|
-
for (const c of candidates) {
|
|
70
|
-
const seconds = Math.round(c.stalledForMs / 1000);
|
|
71
|
-
parts.push(`--- terminal ${c.terminalId} (${c.agentType}, idle ${seconds}s) ---`);
|
|
72
|
-
if (c.task)
|
|
73
|
-
parts.push(`task: ${c.task}`);
|
|
74
|
-
if (c.cwd)
|
|
75
|
-
parts.push(`cwd: ${c.cwd}`);
|
|
76
|
-
parts.push('last JSONL lines:');
|
|
77
|
-
for (const line of c.tailLines) {
|
|
78
|
-
parts.push(line);
|
|
79
|
-
}
|
|
80
|
-
parts.push('');
|
|
81
|
-
}
|
|
82
|
-
return parts.join('\n');
|
|
83
|
-
}
|
|
84
|
-
export function parseWatchdogResponse(stdout) {
|
|
85
|
-
if (!stdout || !stdout.trim())
|
|
86
|
-
return [];
|
|
87
|
-
const arrayMatch = stdout.match(/\[[\s\S]*\]/);
|
|
88
|
-
if (!arrayMatch)
|
|
89
|
-
return [];
|
|
90
|
-
let parsed;
|
|
91
|
-
try {
|
|
92
|
-
parsed = JSON.parse(arrayMatch[0]);
|
|
93
|
-
}
|
|
94
|
-
catch {
|
|
95
|
-
return [];
|
|
96
|
-
}
|
|
97
|
-
if (!Array.isArray(parsed))
|
|
98
|
-
return [];
|
|
99
|
-
const decisions = [];
|
|
100
|
-
for (const d of parsed) {
|
|
101
|
-
if (!d || typeof d !== 'object')
|
|
102
|
-
continue;
|
|
103
|
-
const obj = d;
|
|
104
|
-
const terminalId = typeof obj.terminalId === 'string' ? obj.terminalId : '';
|
|
105
|
-
const action = obj.action === 'nudge' || obj.action === 'skip' ? obj.action : null;
|
|
106
|
-
const text = typeof obj.text === 'string' ? obj.text : '';
|
|
107
|
-
const reason = typeof obj.reason === 'string' ? obj.reason : '';
|
|
108
|
-
if (!terminalId || !action)
|
|
109
|
-
continue;
|
|
110
|
-
const needsHuman = action === 'skip' && obj.needsHuman === true ? true
|
|
111
|
-
: action === 'skip' && obj.needsHuman === false ? false
|
|
112
|
-
: undefined;
|
|
113
|
-
decisions.push({ terminalId, action, text, reason, needsHuman });
|
|
114
|
-
}
|
|
115
|
-
return decisions;
|
|
116
|
-
}
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
const MAX_MESSAGE_CHARS = 600;
|
|
2
|
-
export function summarizeWatchdogTail(tailLines, agentType) {
|
|
3
|
-
let lastUser;
|
|
4
|
-
let lastAssistant;
|
|
5
|
-
for (let i = tailLines.length - 1; i >= 0; i--) {
|
|
6
|
-
if (lastUser && lastAssistant)
|
|
7
|
-
break;
|
|
8
|
-
let raw;
|
|
9
|
-
try {
|
|
10
|
-
raw = JSON.parse(tailLines[i]);
|
|
11
|
-
}
|
|
12
|
-
catch {
|
|
13
|
-
continue;
|
|
14
|
-
}
|
|
15
|
-
if (!raw || typeof raw !== 'object')
|
|
16
|
-
continue;
|
|
17
|
-
if (agentType === 'claude') {
|
|
18
|
-
const claim = readClaude(raw);
|
|
19
|
-
if (claim?.role === 'user' && !lastUser)
|
|
20
|
-
lastUser = claim.text;
|
|
21
|
-
else if (claim?.role === 'assistant' && !lastAssistant)
|
|
22
|
-
lastAssistant = claim.text;
|
|
23
|
-
}
|
|
24
|
-
else if (agentType === 'codex') {
|
|
25
|
-
const claim = readCodex(raw);
|
|
26
|
-
if (claim?.role === 'user' && !lastUser)
|
|
27
|
-
lastUser = claim.text;
|
|
28
|
-
else if (claim?.role === 'assistant' && !lastAssistant)
|
|
29
|
-
lastAssistant = claim.text;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return {
|
|
33
|
-
lastUserMessage: clip(lastUser),
|
|
34
|
-
lastAssistantMessage: clip(lastAssistant),
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
function readClaude(raw) {
|
|
38
|
-
const t = raw.type;
|
|
39
|
-
if (t !== 'user' && t !== 'assistant')
|
|
40
|
-
return null;
|
|
41
|
-
const msg = raw.message;
|
|
42
|
-
if (!msg)
|
|
43
|
-
return null;
|
|
44
|
-
const text = pickContentText(msg.content);
|
|
45
|
-
if (!text)
|
|
46
|
-
return null;
|
|
47
|
-
return { role: t, text };
|
|
48
|
-
}
|
|
49
|
-
function readCodex(raw) {
|
|
50
|
-
if (raw.type !== 'response_item')
|
|
51
|
-
return null;
|
|
52
|
-
const payload = raw.payload;
|
|
53
|
-
if (!payload || payload.type !== 'message')
|
|
54
|
-
return null;
|
|
55
|
-
const role = payload.role;
|
|
56
|
-
if (role !== 'user' && role !== 'assistant')
|
|
57
|
-
return null;
|
|
58
|
-
const text = pickContentText(payload.content);
|
|
59
|
-
if (!text)
|
|
60
|
-
return null;
|
|
61
|
-
return { role, text };
|
|
62
|
-
}
|
|
63
|
-
function pickContentText(content) {
|
|
64
|
-
if (typeof content === 'string')
|
|
65
|
-
return isSyntheticTagged(content) ? null : content;
|
|
66
|
-
if (!Array.isArray(content))
|
|
67
|
-
return null;
|
|
68
|
-
const parts = [];
|
|
69
|
-
for (const part of content) {
|
|
70
|
-
if (!part || typeof part !== 'object')
|
|
71
|
-
continue;
|
|
72
|
-
const p = part;
|
|
73
|
-
if ((p.type === 'text' || p.type === 'input_text' || p.type === 'output_text') && typeof p.text === 'string') {
|
|
74
|
-
if (isSyntheticTagged(p.text))
|
|
75
|
-
continue;
|
|
76
|
-
parts.push(p.text);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return parts.length ? parts.join('\n') : null;
|
|
80
|
-
}
|
|
81
|
-
const SYNTHETIC_TAG_PREFIXES = [
|
|
82
|
-
'<local-command-caveat',
|
|
83
|
-
'<local-command-stdout',
|
|
84
|
-
'<local-command-stderr',
|
|
85
|
-
'<command-name',
|
|
86
|
-
'<command-message',
|
|
87
|
-
'<command-args',
|
|
88
|
-
'<bash-input',
|
|
89
|
-
'<bash-stdout',
|
|
90
|
-
'<bash-stderr',
|
|
91
|
-
'<system-reminder',
|
|
92
|
-
'<user-prompt-submit-hook',
|
|
93
|
-
'<task-notification',
|
|
94
|
-
'<persisted-output',
|
|
95
|
-
];
|
|
96
|
-
function isSyntheticTagged(text) {
|
|
97
|
-
const trimmed = text.trim();
|
|
98
|
-
if (!trimmed)
|
|
99
|
-
return true;
|
|
100
|
-
const lower = trimmed.toLowerCase();
|
|
101
|
-
for (const prefix of SYNTHETIC_TAG_PREFIXES) {
|
|
102
|
-
if (lower.startsWith(prefix))
|
|
103
|
-
return true;
|
|
104
|
-
}
|
|
105
|
-
return false;
|
|
106
|
-
}
|
|
107
|
-
function clip(text) {
|
|
108
|
-
if (!text)
|
|
109
|
-
return undefined;
|
|
110
|
-
const cleaned = text.replace(/\s+/g, ' ').trim();
|
|
111
|
-
if (!cleaned)
|
|
112
|
-
return undefined;
|
|
113
|
-
return cleaned.length > MAX_MESSAGE_CHARS
|
|
114
|
-
? cleaned.slice(0, MAX_MESSAGE_CHARS - 1) + '…'
|
|
115
|
-
: cleaned;
|
|
116
|
-
}
|