@blockrun/franklin 3.33.0 → 3.34.1
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 +23 -2
- package/dist/agent/approvals.d.ts +47 -0
- package/dist/agent/approvals.js +66 -0
- package/dist/agent/commands.js +230 -7
- package/dist/agent/input-queue.d.ts +26 -0
- package/dist/agent/input-queue.js +65 -0
- package/dist/agent/loop.js +196 -3
- package/dist/agent/streaming-executor.d.ts +4 -0
- package/dist/agent/streaming-executor.js +52 -0
- package/dist/agent/tool-guard.d.ts +7 -0
- package/dist/agent/tool-guard.js +21 -0
- package/dist/agent/types.d.ts +26 -0
- package/dist/audit/approvals.d.ts +20 -0
- package/dist/audit/approvals.js +21 -0
- package/dist/commands/start.d.ts +10 -1
- package/dist/commands/start.js +53 -1
- package/dist/events/types.d.ts +38 -1
- package/dist/goal/engine.d.ts +39 -0
- package/dist/goal/engine.js +182 -0
- package/dist/goal/prompts.d.ts +16 -0
- package/dist/goal/prompts.js +206 -0
- package/dist/goal/runtime.d.ts +20 -0
- package/dist/goal/runtime.js +110 -0
- package/dist/goal/store.d.ts +22 -0
- package/dist/goal/store.js +79 -0
- package/dist/goal/types.d.ts +63 -0
- package/dist/goal/types.js +26 -0
- package/dist/hooks/loader.d.ts +19 -0
- package/dist/hooks/loader.js +115 -0
- package/dist/hooks/runner.d.ts +31 -0
- package/dist/hooks/runner.js +147 -0
- package/dist/hooks/types.d.ts +64 -0
- package/dist/hooks/types.js +35 -0
- package/dist/index.js +5 -1
- package/dist/memory/capture.d.ts +40 -0
- package/dist/memory/capture.js +95 -0
- package/dist/memory/dream.d.ts +24 -0
- package/dist/memory/dream.js +147 -0
- package/dist/memory/indexer.d.ts +29 -0
- package/dist/memory/indexer.js +144 -0
- package/dist/memory/store.d.ts +50 -0
- package/dist/memory/store.js +163 -0
- package/dist/monitors/registry.d.ts +42 -0
- package/dist/monitors/registry.js +104 -0
- package/dist/panel/html.js +299 -0
- package/dist/panel/server.js +41 -0
- package/dist/scheduler/service.d.ts +24 -0
- package/dist/scheduler/service.js +44 -0
- package/dist/scheduler/store.d.ts +26 -0
- package/dist/scheduler/store.js +116 -0
- package/dist/scheduler/types.d.ts +36 -0
- package/dist/scheduler/types.js +31 -0
- package/dist/serve/agent-host.d.ts +63 -0
- package/dist/serve/agent-host.js +270 -0
- package/dist/serve/server.js +186 -3
- package/dist/session/live-registry.d.ts +32 -0
- package/dist/session/live-registry.js +83 -0
- package/dist/session/search.d.ts +6 -0
- package/dist/session/search.js +3 -3
- package/dist/session/storage.d.ts +6 -0
- package/dist/session/storage.js +6 -0
- package/dist/tools/goal.d.ts +7 -0
- package/dist/tools/goal.js +29 -0
- package/dist/tools/index.js +11 -0
- package/dist/tools/memory.js +14 -4
- package/dist/tools/monitor.d.ts +11 -0
- package/dist/tools/monitor.js +96 -0
- package/dist/tools/scheduler.d.ts +12 -0
- package/dist/tools/scheduler.js +79 -0
- package/dist/tools/spend-tools.d.ts +33 -0
- package/dist/tools/spend-tools.js +114 -0
- package/dist/tools/tool-categories.js +7 -0
- package/dist/tools/trade-plan.d.ts +11 -0
- package/dist/tools/trade-plan.js +137 -0
- package/dist/tools/trading-execute.js +24 -0
- package/dist/trading/trade-plan.d.ts +75 -0
- package/dist/trading/trade-plan.js +290 -0
- package/package.json +2 -2
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Document memory — curated markdown files with identity-keyed scoping.
|
|
3
|
+
*
|
|
4
|
+
* Layout under ~/.blockrun/memory/:
|
|
5
|
+
* MEMORY.md global, cross-project facts
|
|
6
|
+
* <slug>-<hash8>/MEMORY.md workspace-scoped (curated)
|
|
7
|
+
* <slug>-<hash8>/sessions/YYYY-MM-DD-*.md per-session logs (decaying)
|
|
8
|
+
* trading-<chain>-<addr8>/JOURNAL.md trade journal (curated)
|
|
9
|
+
* trading-<chain>-<addr8>/theses/<asset>.md one thesis file per asset
|
|
10
|
+
*
|
|
11
|
+
* Identity keying: a workspace is keyed by its git origin (org/repo form)
|
|
12
|
+
* so clones and worktrees of the same repo share memory; non-repos fall
|
|
13
|
+
* back to the directory path. Trading memory is keyed by WALLET ADDRESS —
|
|
14
|
+
* the journal follows the money, not the directory.
|
|
15
|
+
*
|
|
16
|
+
* This subsystem is additive to the Brain entity graph (src/brain/): the
|
|
17
|
+
* graph answers "what do I know about entity X", documents answer "what
|
|
18
|
+
* was my SOL thesis" / "what did we do last week". MemoryRecall facades
|
|
19
|
+
* both. Kill switch: FRANKLIN_MEMORY=0.
|
|
20
|
+
*/
|
|
21
|
+
import fs from 'node:fs';
|
|
22
|
+
import path from 'node:path';
|
|
23
|
+
import crypto from 'node:crypto';
|
|
24
|
+
import { execFileSync } from 'node:child_process';
|
|
25
|
+
import { BLOCKRUN_DIR } from '../config.js';
|
|
26
|
+
export function memoryEnabled() {
|
|
27
|
+
return process.env.FRANKLIN_MEMORY !== '0';
|
|
28
|
+
}
|
|
29
|
+
export function memoryRoot() {
|
|
30
|
+
return path.join(BLOCKRUN_DIR, 'memory');
|
|
31
|
+
}
|
|
32
|
+
export function globalMemoryPath() {
|
|
33
|
+
return path.join(memoryRoot(), 'MEMORY.md');
|
|
34
|
+
}
|
|
35
|
+
// ─── Identity keying ───────────────────────────────────────────────────────
|
|
36
|
+
function slugify(text) {
|
|
37
|
+
return text.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 32) || 'workspace';
|
|
38
|
+
}
|
|
39
|
+
/** org/repo from the git origin remote, or null outside a repo. */
|
|
40
|
+
export function repoIdentity(workDir) {
|
|
41
|
+
try {
|
|
42
|
+
const url = execFileSync('git', ['-C', workDir, 'remote', 'get-url', 'origin'], {
|
|
43
|
+
encoding: 'utf-8',
|
|
44
|
+
timeout: 2000,
|
|
45
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
46
|
+
}).trim();
|
|
47
|
+
const m = url.match(/[:/]([^/:]+\/[^/]+?)(?:\.git)?$/);
|
|
48
|
+
return m ? m[1] : url || null;
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export function workspaceKey(workDir) {
|
|
55
|
+
const identity = repoIdentity(workDir) ?? workDir;
|
|
56
|
+
return {
|
|
57
|
+
slug: slugify(path.basename(identity)),
|
|
58
|
+
hash8: crypto.createHash('sha256').update(identity).digest('hex').slice(0, 8),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export function workspaceDir(workDir) {
|
|
62
|
+
const { slug, hash8 } = workspaceKey(workDir);
|
|
63
|
+
return path.join(memoryRoot(), `${slug}-${hash8}`);
|
|
64
|
+
}
|
|
65
|
+
export function workspaceMemoryPath(workDir) {
|
|
66
|
+
return path.join(workspaceDir(workDir), 'MEMORY.md');
|
|
67
|
+
}
|
|
68
|
+
export function sessionLogsDir(workDir) {
|
|
69
|
+
return path.join(workspaceDir(workDir), 'sessions');
|
|
70
|
+
}
|
|
71
|
+
export function tradingDir(chain, address) {
|
|
72
|
+
return path.join(memoryRoot(), `trading-${chain}-${address.slice(0, 8).toLowerCase()}`);
|
|
73
|
+
}
|
|
74
|
+
export function tradingJournalPath(chain, address) {
|
|
75
|
+
return path.join(tradingDir(chain, address), 'JOURNAL.md');
|
|
76
|
+
}
|
|
77
|
+
export function thesisPath(chain, address, asset) {
|
|
78
|
+
return path.join(tradingDir(chain, address), 'theses', `${slugify(asset)}.md`);
|
|
79
|
+
}
|
|
80
|
+
// ─── Writes ────────────────────────────────────────────────────────────────
|
|
81
|
+
function appendToFile(filePath, text) {
|
|
82
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
83
|
+
fs.appendFileSync(filePath, text.endsWith('\n') ? text : text + '\n');
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Append a note under a markdown heading, creating file/heading as needed.
|
|
87
|
+
* Used by /remember and the trading capture.
|
|
88
|
+
*/
|
|
89
|
+
export function appendUnderHeading(filePath, heading, note) {
|
|
90
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
91
|
+
const stamp = new Date().toISOString().slice(0, 10);
|
|
92
|
+
const line = `- (${stamp}) ${note.trim()}`;
|
|
93
|
+
let content = '';
|
|
94
|
+
try {
|
|
95
|
+
content = fs.readFileSync(filePath, 'utf-8');
|
|
96
|
+
}
|
|
97
|
+
catch { /* new file */ }
|
|
98
|
+
const headingLine = `## ${heading}`;
|
|
99
|
+
if (!content.includes(headingLine)) {
|
|
100
|
+
appendToFile(filePath, `${content && !content.endsWith('\n') ? '\n' : ''}${headingLine}\n${line}`);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
// Insert directly after the heading's existing block (before the next ##).
|
|
104
|
+
const lines = content.split('\n');
|
|
105
|
+
const idx = lines.findIndex(l => l.trim() === headingLine);
|
|
106
|
+
let insertAt = lines.length;
|
|
107
|
+
for (let i = idx + 1; i < lines.length; i++) {
|
|
108
|
+
if (lines[i].startsWith('## ')) {
|
|
109
|
+
insertAt = i;
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
lines.splice(insertAt, 0, line);
|
|
114
|
+
fs.writeFileSync(filePath, lines.join('\n'));
|
|
115
|
+
}
|
|
116
|
+
/** Dated session log path for today. */
|
|
117
|
+
export function sessionLogPath(workDir, sessionId) {
|
|
118
|
+
const day = new Date().toISOString().slice(0, 10);
|
|
119
|
+
return path.join(sessionLogsDir(workDir), `${day}-${sessionId.slice(0, 8)}.md`);
|
|
120
|
+
}
|
|
121
|
+
export function writeSessionLog(workDir, sessionId, content) {
|
|
122
|
+
const p = sessionLogPath(workDir, sessionId);
|
|
123
|
+
fs.mkdirSync(path.dirname(p), { recursive: true });
|
|
124
|
+
fs.writeFileSync(p, content.endsWith('\n') ? content : content + '\n');
|
|
125
|
+
}
|
|
126
|
+
export function listMemoryFiles(workDir) {
|
|
127
|
+
const refs = [];
|
|
128
|
+
const push = (p, scope) => {
|
|
129
|
+
try {
|
|
130
|
+
const stat = fs.statSync(p);
|
|
131
|
+
if (stat.isFile())
|
|
132
|
+
refs.push({ path: p, scope, mtimeMs: stat.mtimeMs });
|
|
133
|
+
}
|
|
134
|
+
catch { /* missing */ }
|
|
135
|
+
};
|
|
136
|
+
push(globalMemoryPath(), 'global');
|
|
137
|
+
push(workspaceMemoryPath(workDir), 'workspace');
|
|
138
|
+
try {
|
|
139
|
+
for (const f of fs.readdirSync(sessionLogsDir(workDir))) {
|
|
140
|
+
if (f.endsWith('.md'))
|
|
141
|
+
push(path.join(sessionLogsDir(workDir), f), 'session');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
catch { /* no session logs yet */ }
|
|
145
|
+
// Every trading identity the user has — journals + theses.
|
|
146
|
+
try {
|
|
147
|
+
for (const dir of fs.readdirSync(memoryRoot())) {
|
|
148
|
+
if (!dir.startsWith('trading-'))
|
|
149
|
+
continue;
|
|
150
|
+
const base = path.join(memoryRoot(), dir);
|
|
151
|
+
push(path.join(base, 'JOURNAL.md'), 'trading');
|
|
152
|
+
try {
|
|
153
|
+
for (const f of fs.readdirSync(path.join(base, 'theses'))) {
|
|
154
|
+
if (f.endsWith('.md'))
|
|
155
|
+
push(path.join(base, 'theses', f), 'trading');
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
catch { /* no theses */ }
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch { /* no memory root yet */ }
|
|
162
|
+
return refs;
|
|
163
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Monitor registry — session-scoped bookkeeping for line-oriented monitors.
|
|
3
|
+
*
|
|
4
|
+
* A monitor is a detached background command (task subsystem) whose log is
|
|
5
|
+
* swept at turn boundaries; new lines land in the next turn's user content
|
|
6
|
+
* as a bracketed block. The registry owns read offsets so the task-side
|
|
7
|
+
* meta.json single-writer contract (runner only) stays intact.
|
|
8
|
+
*
|
|
9
|
+
* Volume cutoff: a monitor producing more than MAX_LINES_PER_SWEEP lines in
|
|
10
|
+
* one sweep, or MAX_EVENTS_PER_SESSION lines overall, is auto-paused with a
|
|
11
|
+
* note — a flooding monitor must not drown the conversation. Restart it
|
|
12
|
+
* with a tighter filter pattern instead.
|
|
13
|
+
*/
|
|
14
|
+
export declare const MAX_LINES_PER_SWEEP = 50;
|
|
15
|
+
export declare const MAX_EVENTS_PER_SESSION = 200;
|
|
16
|
+
export interface MonitorEntry {
|
|
17
|
+
taskRunId: string;
|
|
18
|
+
label: string;
|
|
19
|
+
pattern?: RegExp;
|
|
20
|
+
byteOffset: number;
|
|
21
|
+
eventsDelivered: number;
|
|
22
|
+
paused: boolean;
|
|
23
|
+
pausedReason?: string;
|
|
24
|
+
}
|
|
25
|
+
export declare function resetMonitors(): void;
|
|
26
|
+
export declare function registerMonitor(entry: {
|
|
27
|
+
taskRunId: string;
|
|
28
|
+
label: string;
|
|
29
|
+
pattern?: RegExp;
|
|
30
|
+
}): void;
|
|
31
|
+
export declare function getMonitor(taskRunId: string): MonitorEntry | undefined;
|
|
32
|
+
export declare function removeMonitor(taskRunId: string): boolean;
|
|
33
|
+
export declare function listMonitors(): MonitorEntry[];
|
|
34
|
+
export interface MonitorSweepResult {
|
|
35
|
+
/** Ready-to-append blocks, one per monitor with fresh lines. */
|
|
36
|
+
blocks: string[];
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Read fresh log lines for every active monitor. Called at turn boundaries
|
|
40
|
+
* by the agent loop; safe to call often (offset-tracked, no re-reads).
|
|
41
|
+
*/
|
|
42
|
+
export declare function sweepMonitors(): MonitorSweepResult;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Monitor registry — session-scoped bookkeeping for line-oriented monitors.
|
|
3
|
+
*
|
|
4
|
+
* A monitor is a detached background command (task subsystem) whose log is
|
|
5
|
+
* swept at turn boundaries; new lines land in the next turn's user content
|
|
6
|
+
* as a bracketed block. The registry owns read offsets so the task-side
|
|
7
|
+
* meta.json single-writer contract (runner only) stays intact.
|
|
8
|
+
*
|
|
9
|
+
* Volume cutoff: a monitor producing more than MAX_LINES_PER_SWEEP lines in
|
|
10
|
+
* one sweep, or MAX_EVENTS_PER_SESSION lines overall, is auto-paused with a
|
|
11
|
+
* note — a flooding monitor must not drown the conversation. Restart it
|
|
12
|
+
* with a tighter filter pattern instead.
|
|
13
|
+
*/
|
|
14
|
+
import fs from 'node:fs';
|
|
15
|
+
import { taskLogPath } from '../tasks/paths.js';
|
|
16
|
+
import { readTaskMeta } from '../tasks/store.js';
|
|
17
|
+
import { isTerminalTaskStatus } from '../tasks/types.js';
|
|
18
|
+
export const MAX_LINES_PER_SWEEP = 50;
|
|
19
|
+
export const MAX_EVENTS_PER_SESSION = 200;
|
|
20
|
+
const monitors = new Map();
|
|
21
|
+
export function resetMonitors() {
|
|
22
|
+
monitors.clear();
|
|
23
|
+
}
|
|
24
|
+
export function registerMonitor(entry) {
|
|
25
|
+
monitors.set(entry.taskRunId, {
|
|
26
|
+
...entry,
|
|
27
|
+
byteOffset: 0,
|
|
28
|
+
eventsDelivered: 0,
|
|
29
|
+
paused: false,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
export function getMonitor(taskRunId) {
|
|
33
|
+
return monitors.get(taskRunId);
|
|
34
|
+
}
|
|
35
|
+
export function removeMonitor(taskRunId) {
|
|
36
|
+
return monitors.delete(taskRunId);
|
|
37
|
+
}
|
|
38
|
+
export function listMonitors() {
|
|
39
|
+
return [...monitors.values()];
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Read fresh log lines for every active monitor. Called at turn boundaries
|
|
43
|
+
* by the agent loop; safe to call often (offset-tracked, no re-reads).
|
|
44
|
+
*/
|
|
45
|
+
export function sweepMonitors() {
|
|
46
|
+
const blocks = [];
|
|
47
|
+
for (const entry of monitors.values()) {
|
|
48
|
+
if (entry.paused)
|
|
49
|
+
continue;
|
|
50
|
+
const meta = readTaskMeta(entry.taskRunId);
|
|
51
|
+
const logPath = taskLogPath(entry.taskRunId);
|
|
52
|
+
let fresh = '';
|
|
53
|
+
try {
|
|
54
|
+
const stat = fs.statSync(logPath);
|
|
55
|
+
if (stat.size > entry.byteOffset) {
|
|
56
|
+
const fd = fs.openSync(logPath, 'r');
|
|
57
|
+
try {
|
|
58
|
+
const len = stat.size - entry.byteOffset;
|
|
59
|
+
const buf = Buffer.alloc(len);
|
|
60
|
+
fs.readSync(fd, buf, 0, len, entry.byteOffset);
|
|
61
|
+
fresh = buf.toString('utf-8');
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
fs.closeSync(fd);
|
|
65
|
+
}
|
|
66
|
+
entry.byteOffset = stat.size;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
/* log not readable yet — nothing fresh */
|
|
71
|
+
}
|
|
72
|
+
let lines = fresh.split('\n').map(l => l.trimEnd()).filter(l => l.length > 0);
|
|
73
|
+
if (entry.pattern) {
|
|
74
|
+
const re = entry.pattern;
|
|
75
|
+
lines = lines.filter(l => re.test(l));
|
|
76
|
+
}
|
|
77
|
+
const ended = meta ? isTerminalTaskStatus(meta.status) : false;
|
|
78
|
+
if (lines.length > MAX_LINES_PER_SWEEP) {
|
|
79
|
+
const dropped = lines.length - MAX_LINES_PER_SWEEP;
|
|
80
|
+
lines = lines.slice(0, MAX_LINES_PER_SWEEP);
|
|
81
|
+
entry.paused = true;
|
|
82
|
+
entry.pausedReason = `flooded (${dropped} lines dropped in one sweep) — restart with a tighter filter pattern`;
|
|
83
|
+
}
|
|
84
|
+
entry.eventsDelivered += lines.length;
|
|
85
|
+
if (!entry.paused && entry.eventsDelivered > MAX_EVENTS_PER_SESSION) {
|
|
86
|
+
entry.paused = true;
|
|
87
|
+
entry.pausedReason = `delivered ${entry.eventsDelivered} events this session — restart with a tighter filter pattern`;
|
|
88
|
+
}
|
|
89
|
+
if (lines.length > 0 || ended || entry.paused) {
|
|
90
|
+
const parts = [];
|
|
91
|
+
if (lines.length > 0)
|
|
92
|
+
parts.push(lines.join('\n'));
|
|
93
|
+
if (entry.paused)
|
|
94
|
+
parts.push(`(monitor auto-paused: ${entry.pausedReason})`);
|
|
95
|
+
if (ended && meta) {
|
|
96
|
+
parts.push(`(monitored command ended: ${meta.status}${meta.exitCode != null ? `, exit ${meta.exitCode}` : ''})`);
|
|
97
|
+
}
|
|
98
|
+
blocks.push(`[monitor: ${entry.label}]\n${parts.join('\n')}`);
|
|
99
|
+
}
|
|
100
|
+
if (ended)
|
|
101
|
+
monitors.delete(entry.taskRunId);
|
|
102
|
+
}
|
|
103
|
+
return { blocks };
|
|
104
|
+
}
|
package/dist/panel/html.js
CHANGED
|
@@ -513,6 +513,11 @@ a:hover { text-decoration:underline; }
|
|
|
513
513
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="7" height="7" rx="1.5"/><rect x="14" y="3" width="7" height="7" rx="1.5"/><rect x="3" y="14" width="7" height="7" rx="1.5"/><rect x="14" y="14" width="7" height="7" rx="1.5"/></svg>
|
|
514
514
|
Overview
|
|
515
515
|
</button>
|
|
516
|
+
<button class="nav-item" data-tab="agents">
|
|
517
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="5" r="2.2"/><circle cx="5" cy="19" r="2.2"/><circle cx="19" cy="19" r="2.2"/><path d="M12 7.2V12"/><path d="M12 12l-5.2 5"/><path d="M12 12l5.2 5"/></svg>
|
|
518
|
+
Agents
|
|
519
|
+
<span class="nav-badge" id="agents-nav-badge" style="display:none"></span>
|
|
520
|
+
</button>
|
|
516
521
|
<button class="nav-item" data-tab="wallet">
|
|
517
522
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12V7H5a2 2 0 0 1 0-4h14v4"/><path d="M3 5v14a2 2 0 0 0 2 2h16v-5"/><path d="M18 12a2 2 0 0 0 0 4h4v-4z"/></svg>
|
|
518
523
|
Wallet
|
|
@@ -627,6 +632,58 @@ a:hover { text-decoration:underline; }
|
|
|
627
632
|
</div>
|
|
628
633
|
</div>
|
|
629
634
|
|
|
635
|
+
<!-- Agents — mission control -->
|
|
636
|
+
<div class="tab" id="tab-agents">
|
|
637
|
+
<div class="content-header">
|
|
638
|
+
<h2>Agents</h2>
|
|
639
|
+
<p>Mission control — dispatch, watch, and approve your agent fleet</p>
|
|
640
|
+
</div>
|
|
641
|
+
|
|
642
|
+
<div id="agents-offline" class="card" style="display:none;border-color:#8a6d3b">
|
|
643
|
+
<h3>Agent host offline</h3>
|
|
644
|
+
<div class="sub">Run <code>franklin serve</code> in a terminal to dispatch and control agents from here. Sessions below are read-only.</div>
|
|
645
|
+
</div>
|
|
646
|
+
|
|
647
|
+
<div class="card" id="agents-dispatch-card" style="margin-bottom:12px">
|
|
648
|
+
<h3>Dispatch a new agent</h3>
|
|
649
|
+
<div style="display:flex;gap:8px;flex-wrap:wrap;align-items:center">
|
|
650
|
+
<input id="agents-dispatch-prompt" type="text" placeholder="e.g. monitor BTC funding rates and alert me on a flip"
|
|
651
|
+
style="flex:1;min-width:260px;background:transparent;border:1px solid var(--border,#333);border-radius:6px;padding:8px 10px;color:inherit" />
|
|
652
|
+
<input id="agents-dispatch-model" type="text" placeholder="model (optional)"
|
|
653
|
+
style="width:160px;background:transparent;border:1px solid var(--border,#333);border-radius:6px;padding:8px 10px;color:inherit" />
|
|
654
|
+
<input id="agents-dispatch-maxspend" type="number" step="0.5" min="0" placeholder="max $"
|
|
655
|
+
style="width:80px;background:transparent;border:1px solid var(--border,#333);border-radius:6px;padding:8px 10px;color:inherit" />
|
|
656
|
+
<button id="agents-dispatch-btn" class="btn">Dispatch</button>
|
|
657
|
+
</div>
|
|
658
|
+
<div class="sub" style="margin-top:6px">Every dispatch starts a NEW agent. Press Enter to batch-dispatch several.</div>
|
|
659
|
+
</div>
|
|
660
|
+
|
|
661
|
+
<div class="grid" style="grid-template-columns: minmax(300px, 5fr) minmax(320px, 7fr); gap:12px; align-items:start">
|
|
662
|
+
<div class="card">
|
|
663
|
+
<h3>Fleet</h3>
|
|
664
|
+
<div id="agents-list" class="sub">Loading…</div>
|
|
665
|
+
</div>
|
|
666
|
+
<div class="card" id="agents-peek" style="display:none">
|
|
667
|
+
<h3 id="agents-peek-title">Agent</h3>
|
|
668
|
+
<div id="agents-peek-state" class="sub"></div>
|
|
669
|
+
<div id="agents-approval" style="display:none;margin:10px 0;padding:10px;border:1px solid #8a6d3b;border-radius:8px">
|
|
670
|
+
<div id="agents-approval-title" style="font-weight:600;margin-bottom:6px"></div>
|
|
671
|
+
<pre id="agents-approval-desc" style="white-space:pre-wrap;font-size:12px;max-height:220px;overflow:auto"></pre>
|
|
672
|
+
<div id="agents-approval-options" style="display:flex;gap:8px;flex-wrap:wrap;margin-top:8px"></div>
|
|
673
|
+
<textarea id="agents-approval-message" placeholder="optional message (e.g. requested changes)" rows="2"
|
|
674
|
+
style="width:100%;margin-top:8px;background:transparent;border:1px solid var(--border,#333);border-radius:6px;padding:6px;color:inherit;display:none"></textarea>
|
|
675
|
+
</div>
|
|
676
|
+
<pre id="agents-transcript" style="white-space:pre-wrap;font-size:12px;max-height:420px;overflow:auto;background:rgba(0,0,0,0.25);border-radius:8px;padding:10px;margin-top:8px"></pre>
|
|
677
|
+
<div style="display:flex;gap:8px;margin-top:10px">
|
|
678
|
+
<input id="agents-reply-input" type="text" placeholder="reply to this agent…"
|
|
679
|
+
style="flex:1;background:transparent;border:1px solid var(--border,#333);border-radius:6px;padding:8px 10px;color:inherit" />
|
|
680
|
+
<button id="agents-reply-btn" class="btn">Send</button>
|
|
681
|
+
<button id="agents-cancel-btn" class="btn" style="border-color:#8a3b3b">Cancel turn</button>
|
|
682
|
+
</div>
|
|
683
|
+
</div>
|
|
684
|
+
</div>
|
|
685
|
+
</div>
|
|
686
|
+
|
|
630
687
|
<!-- Wallet -->
|
|
631
688
|
<div class="tab" id="tab-wallet">
|
|
632
689
|
<div class="content-header">
|
|
@@ -2159,6 +2216,248 @@ setInterval(() => api('wallet').then(w => {
|
|
|
2159
2216
|
document.getElementById('sidebar-balance').textContent = usdBig(w.balance) + ' USDC';
|
|
2160
2217
|
}
|
|
2161
2218
|
}), 30000);
|
|
2219
|
+
|
|
2220
|
+
// ── Agents tab (mission control) ────────────────────────────────────────
|
|
2221
|
+
// Control plane: the serve WebSocket (ws://127.0.0.1:3737/agent), reached
|
|
2222
|
+
// DIRECTLY from this page — the panel's own REST stays read-only. When the
|
|
2223
|
+
// host is offline the tab degrades to read-only registry rows + a banner.
|
|
2224
|
+
(function() {
|
|
2225
|
+
var serveUrl = null, serveAlive = false;
|
|
2226
|
+
var ws = null, wsOpen = null;
|
|
2227
|
+
var reqSeq = 0, pending = {};
|
|
2228
|
+
var hosted = []; // serve-hosted agents (controllable)
|
|
2229
|
+
var registry = []; // /api/agents live-registry rows (incl. CLI, dead)
|
|
2230
|
+
var selected = null, subscribedTo = null;
|
|
2231
|
+
var approvals = {}; // sessionId -> latest pending approval request
|
|
2232
|
+
var pollTimer = null;
|
|
2233
|
+
|
|
2234
|
+
var STATE_ORDER = { 'needs-input': 0, 'working': 1, 'idle': 2, 'inactive': 3, 'completed': 4, 'failed': 5 };
|
|
2235
|
+
var STATE_DOT = { 'needs-input': '#e6b450', 'working': '#58c05e', 'idle': '#8a93a3', 'inactive': '#555c66', 'completed': '#4a708b', 'failed': '#c05858' };
|
|
2236
|
+
|
|
2237
|
+
function ensureWs() {
|
|
2238
|
+
if (ws && ws.readyState === 1) return Promise.resolve(ws);
|
|
2239
|
+
if (wsOpen) return wsOpen;
|
|
2240
|
+
if (!serveUrl) return Promise.reject(new Error('agent host offline'));
|
|
2241
|
+
wsOpen = new Promise(function(resolve, reject) {
|
|
2242
|
+
var sock = new WebSocket(serveUrl);
|
|
2243
|
+
sock.onopen = function() { ws = sock; wsOpen = null; resolve(sock); };
|
|
2244
|
+
sock.onerror = function() { wsOpen = null; reject(new Error('ws error')); };
|
|
2245
|
+
sock.onclose = function() { ws = null; subscribedTo = null; };
|
|
2246
|
+
sock.onmessage = function(evt) {
|
|
2247
|
+
var msg; try { msg = JSON.parse(evt.data); } catch { return; }
|
|
2248
|
+
if (msg.kind === 'response' || msg.kind === 'error') {
|
|
2249
|
+
var r = pending[msg.id];
|
|
2250
|
+
if (r) { delete pending[msg.id]; r(msg.kind === 'error' ? null : (msg.payload || {})); }
|
|
2251
|
+
} else if (msg.kind === 'agent.event' && msg.payload) {
|
|
2252
|
+
onAgentEvent(msg.payload.sessionId, msg.payload.ev);
|
|
2253
|
+
} else if (msg.kind === 'approval.request' && msg.payload) {
|
|
2254
|
+
approvals[msg.payload.sessionId] = msg.payload;
|
|
2255
|
+
if (msg.payload.sessionId === selected) renderApproval();
|
|
2256
|
+
refreshAgents();
|
|
2257
|
+
}
|
|
2258
|
+
};
|
|
2259
|
+
});
|
|
2260
|
+
return wsOpen;
|
|
2261
|
+
}
|
|
2262
|
+
|
|
2263
|
+
function wsCall(kind, payload) {
|
|
2264
|
+
return ensureWs().then(function(sock) {
|
|
2265
|
+
return new Promise(function(resolve) {
|
|
2266
|
+
var id = 'panel-' + (++reqSeq);
|
|
2267
|
+
pending[id] = resolve;
|
|
2268
|
+
sock.send(JSON.stringify({ id: id, kind: kind, payload: payload || {} }));
|
|
2269
|
+
setTimeout(function() { if (pending[id]) { delete pending[id]; resolve(null); } }, 10000);
|
|
2270
|
+
});
|
|
2271
|
+
}).catch(function() { return null; });
|
|
2272
|
+
}
|
|
2273
|
+
|
|
2274
|
+
function transcriptAppend(text) {
|
|
2275
|
+
var pre = document.getElementById('agents-transcript');
|
|
2276
|
+
pre.textContent += text;
|
|
2277
|
+
pre.scrollTop = pre.scrollHeight;
|
|
2278
|
+
}
|
|
2279
|
+
|
|
2280
|
+
function renderStreamEvent(ev) {
|
|
2281
|
+
if (!ev) return;
|
|
2282
|
+
if (ev.kind === 'text_delta') transcriptAppend(ev.text);
|
|
2283
|
+
else if (ev.kind === 'capability_start') transcriptAppend('\\n▸ ' + ev.name + (ev.preview ? ' · ' + ev.preview : '') + '\\n');
|
|
2284
|
+
else if (ev.kind === 'turn_done') transcriptAppend('\\n— turn ' + (ev.reason || 'done') + ' —\\n');
|
|
2285
|
+
}
|
|
2286
|
+
|
|
2287
|
+
function onAgentEvent(sessionId, ev) {
|
|
2288
|
+
if (sessionId !== selected) return;
|
|
2289
|
+
renderStreamEvent(ev);
|
|
2290
|
+
if (ev && ev.kind === 'turn_done') refreshAgents();
|
|
2291
|
+
}
|
|
2292
|
+
|
|
2293
|
+
function mergedAgents() {
|
|
2294
|
+
var bySession = {};
|
|
2295
|
+
registry.forEach(function(r) {
|
|
2296
|
+
var controllable = false;
|
|
2297
|
+
var state = r.state;
|
|
2298
|
+
if (r.host === 'cli' && state !== 'completed' && state !== 'failed') state = 'inactive';
|
|
2299
|
+
bySession[r.sessionId] = { sessionId: r.sessionId, label: r.label, model: r.model, state: state,
|
|
2300
|
+
host: r.host, controllable: controllable, costUsd: r.costUsd, updatedAt: r.updatedAt };
|
|
2301
|
+
});
|
|
2302
|
+
hosted.forEach(function(a) {
|
|
2303
|
+
var state = a.state;
|
|
2304
|
+
if (a.pendingApprovals && a.pendingApprovals.length > 0) {
|
|
2305
|
+
state = 'needs-input';
|
|
2306
|
+
approvals[a.sessionId] = approvals[a.sessionId] || a.pendingApprovals[0];
|
|
2307
|
+
}
|
|
2308
|
+
bySession[a.sessionId] = { sessionId: a.sessionId, label: a.label, model: a.model, state: state,
|
|
2309
|
+
host: 'serve', controllable: true, updatedAt: a.lastEventAt };
|
|
2310
|
+
});
|
|
2311
|
+
var rows = Object.keys(bySession).map(function(k) { return bySession[k]; });
|
|
2312
|
+
rows.sort(function(a, b) {
|
|
2313
|
+
var d = (STATE_ORDER[a.state] ?? 9) - (STATE_ORDER[b.state] ?? 9);
|
|
2314
|
+
return d !== 0 ? d : (b.updatedAt || 0) - (a.updatedAt || 0);
|
|
2315
|
+
});
|
|
2316
|
+
return rows;
|
|
2317
|
+
}
|
|
2318
|
+
|
|
2319
|
+
function refreshAgents() {
|
|
2320
|
+
var rows = mergedAgents();
|
|
2321
|
+
var el = document.getElementById('agents-list');
|
|
2322
|
+
var needsInput = rows.filter(function(r) { return r.state === 'needs-input'; }).length;
|
|
2323
|
+
var badge = document.getElementById('agents-nav-badge');
|
|
2324
|
+
if (needsInput > 0) { badge.style.display = ''; badge.textContent = String(needsInput); }
|
|
2325
|
+
else badge.style.display = 'none';
|
|
2326
|
+
|
|
2327
|
+
if (rows.length === 0) { el.textContent = serveAlive ? 'No agents yet — dispatch one above.' : 'No agents recorded.'; return; }
|
|
2328
|
+
el.innerHTML = '';
|
|
2329
|
+
rows.forEach(function(r) {
|
|
2330
|
+
var row = document.createElement('div');
|
|
2331
|
+
row.style.cssText = 'display:flex;align-items:center;gap:8px;padding:7px 6px;border-radius:6px;cursor:pointer'
|
|
2332
|
+
+ (r.sessionId === selected ? ';background:rgba(255,255,255,0.06)' : '');
|
|
2333
|
+
var dot = document.createElement('span');
|
|
2334
|
+
dot.style.cssText = 'width:9px;height:9px;border-radius:50%;flex:none;background:' + (STATE_DOT[r.state] || '#666');
|
|
2335
|
+
var label = document.createElement('span');
|
|
2336
|
+
label.textContent = r.label || r.sessionId.slice(0, 8);
|
|
2337
|
+
label.style.cssText = 'flex:1;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:13px';
|
|
2338
|
+
var meta = document.createElement('span');
|
|
2339
|
+
meta.textContent = r.state + ' · ' + (r.model || '').split('/').pop() + (r.host === 'cli' ? ' · cli' : '');
|
|
2340
|
+
meta.style.cssText = 'font-size:11px;color:var(--text-dim,#889)';
|
|
2341
|
+
row.appendChild(dot); row.appendChild(label); row.appendChild(meta);
|
|
2342
|
+
row.addEventListener('click', function() { selectAgent(r); });
|
|
2343
|
+
el.appendChild(row);
|
|
2344
|
+
});
|
|
2345
|
+
}
|
|
2346
|
+
|
|
2347
|
+
function renderApproval() {
|
|
2348
|
+
var box = document.getElementById('agents-approval');
|
|
2349
|
+
var req = selected ? approvals[selected] : null;
|
|
2350
|
+
if (!req) { box.style.display = 'none'; return; }
|
|
2351
|
+
box.style.display = '';
|
|
2352
|
+
document.getElementById('agents-approval-title').textContent = req.title || req.kind;
|
|
2353
|
+
document.getElementById('agents-approval-desc').textContent = req.description || '';
|
|
2354
|
+
var opts = document.getElementById('agents-approval-options');
|
|
2355
|
+
var msgBox = document.getElementById('agents-approval-message');
|
|
2356
|
+
msgBox.style.display = '';
|
|
2357
|
+
opts.innerHTML = '';
|
|
2358
|
+
(req.options && req.options.length ? req.options : ['approve', 'deny']).forEach(function(choice, i) {
|
|
2359
|
+
var btn = document.createElement('button');
|
|
2360
|
+
btn.className = 'btn';
|
|
2361
|
+
btn.textContent = (i + 1) + '. ' + choice;
|
|
2362
|
+
btn.addEventListener('click', function() {
|
|
2363
|
+
wsCall('agent.permissionResponse', {
|
|
2364
|
+
sessionId: req.sessionId, requestId: req.requestId,
|
|
2365
|
+
choice: choice, message: msgBox.value.trim() || undefined,
|
|
2366
|
+
}).then(function() {
|
|
2367
|
+
delete approvals[req.sessionId];
|
|
2368
|
+
msgBox.value = '';
|
|
2369
|
+
renderApproval();
|
|
2370
|
+
setTimeout(loadAgents, 400);
|
|
2371
|
+
});
|
|
2372
|
+
});
|
|
2373
|
+
opts.appendChild(btn);
|
|
2374
|
+
});
|
|
2375
|
+
}
|
|
2376
|
+
|
|
2377
|
+
function selectAgent(row) {
|
|
2378
|
+
selected = row.sessionId;
|
|
2379
|
+
document.getElementById('agents-peek').style.display = '';
|
|
2380
|
+
document.getElementById('agents-peek-title').textContent = row.label || row.sessionId.slice(0, 8);
|
|
2381
|
+
document.getElementById('agents-peek-state').textContent =
|
|
2382
|
+
row.state + ' · ' + (row.model || '') + (row.controllable ? '' : ' · read-only (attach in the terminal that owns it)');
|
|
2383
|
+
document.getElementById('agents-transcript').textContent = '';
|
|
2384
|
+
var canDrive = row.controllable && serveAlive;
|
|
2385
|
+
document.getElementById('agents-reply-input').disabled = !canDrive;
|
|
2386
|
+
document.getElementById('agents-reply-btn').disabled = !canDrive;
|
|
2387
|
+
document.getElementById('agents-cancel-btn').disabled = !canDrive;
|
|
2388
|
+
renderApproval();
|
|
2389
|
+
refreshAgents();
|
|
2390
|
+
if (!canDrive) return;
|
|
2391
|
+
if (subscribedTo && subscribedTo !== selected) wsCall('agents.unsubscribe', { sessionId: subscribedTo });
|
|
2392
|
+
var sid = selected;
|
|
2393
|
+
wsCall('agent.output.replay', { sessionId: sid }).then(function(resp) {
|
|
2394
|
+
if (selected !== sid) return;
|
|
2395
|
+
((resp && resp.events) || []).forEach(renderStreamEvent);
|
|
2396
|
+
return wsCall('agents.subscribe', { sessionId: sid }).then(function() { subscribedTo = sid; });
|
|
2397
|
+
});
|
|
2398
|
+
}
|
|
2399
|
+
|
|
2400
|
+
function loadAgents() {
|
|
2401
|
+
return Promise.all([api('serve-info'), api('agents')]).then(function(res) {
|
|
2402
|
+
var info = res[0] || {}; var reg = res[1] || {};
|
|
2403
|
+
serveUrl = info.url || null;
|
|
2404
|
+
serveAlive = !!info.alive;
|
|
2405
|
+
registry = reg.agents || [];
|
|
2406
|
+
document.getElementById('agents-offline').style.display = serveAlive ? 'none' : '';
|
|
2407
|
+
document.getElementById('agents-dispatch-btn').disabled = !serveAlive;
|
|
2408
|
+
if (!serveAlive) { hosted = []; refreshAgents(); return; }
|
|
2409
|
+
return wsCall('agents.list', {}).then(function(resp) {
|
|
2410
|
+
hosted = (resp && resp.agents) || [];
|
|
2411
|
+
refreshAgents();
|
|
2412
|
+
});
|
|
2413
|
+
});
|
|
2414
|
+
}
|
|
2415
|
+
|
|
2416
|
+
function dispatchAgent() {
|
|
2417
|
+
var promptEl = document.getElementById('agents-dispatch-prompt');
|
|
2418
|
+
var prompt = promptEl.value.trim();
|
|
2419
|
+
if (!prompt || !serveAlive) return;
|
|
2420
|
+
var model = document.getElementById('agents-dispatch-model').value.trim() || undefined;
|
|
2421
|
+
var maxSpend = parseFloat(document.getElementById('agents-dispatch-maxspend').value);
|
|
2422
|
+
promptEl.value = '';
|
|
2423
|
+
promptEl.focus(); // keep focus for batch dispatch
|
|
2424
|
+
wsCall('agents.dispatch', {
|
|
2425
|
+
prompt: prompt, model: model,
|
|
2426
|
+
maxSpendUsd: isFinite(maxSpend) && maxSpend > 0 ? maxSpend : undefined,
|
|
2427
|
+
}).then(loadAgents);
|
|
2428
|
+
}
|
|
2429
|
+
|
|
2430
|
+
document.getElementById('agents-dispatch-btn').addEventListener('click', dispatchAgent);
|
|
2431
|
+
document.getElementById('agents-dispatch-prompt').addEventListener('keydown', function(e) {
|
|
2432
|
+
if (e.key === 'Enter') dispatchAgent();
|
|
2433
|
+
});
|
|
2434
|
+
document.getElementById('agents-reply-btn').addEventListener('click', function() {
|
|
2435
|
+
var input = document.getElementById('agents-reply-input');
|
|
2436
|
+
var text = input.value.trim();
|
|
2437
|
+
if (!text || !selected) return;
|
|
2438
|
+
input.value = '';
|
|
2439
|
+
transcriptAppend('\\n❯ ' + text + '\\n');
|
|
2440
|
+
wsCall('agents.reply', { sessionId: selected, text: text }).then(function() { setTimeout(loadAgents, 300); });
|
|
2441
|
+
});
|
|
2442
|
+
document.getElementById('agents-reply-input').addEventListener('keydown', function(e) {
|
|
2443
|
+
if (e.key === 'Enter') document.getElementById('agents-reply-btn').click();
|
|
2444
|
+
});
|
|
2445
|
+
document.getElementById('agents-cancel-btn').addEventListener('click', function() {
|
|
2446
|
+
if (selected) wsCall('agents.cancel', { sessionId: selected }).then(function() { setTimeout(loadAgents, 300); });
|
|
2447
|
+
});
|
|
2448
|
+
|
|
2449
|
+
document.addEventListener('tab:activated', function(e) {
|
|
2450
|
+
if (e.detail.name !== 'agents') return;
|
|
2451
|
+
loadAgents();
|
|
2452
|
+
if (!pollTimer) pollTimer = setInterval(loadAgents, 5000);
|
|
2453
|
+
});
|
|
2454
|
+
document.addEventListener('tab:deactivated', function(e) {
|
|
2455
|
+
if (e.detail.name !== 'agents') return;
|
|
2456
|
+
if (pollTimer) { clearInterval(pollTimer); pollTimer = null; }
|
|
2457
|
+
});
|
|
2458
|
+
// Badge even before first visit.
|
|
2459
|
+
loadAgents();
|
|
2460
|
+
})();
|
|
2162
2461
|
</script>
|
|
2163
2462
|
</body>
|
|
2164
2463
|
</html>`;
|