agentcue 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +122 -2
- package/dist/cli.js +770 -0
- package/dist/client.js +489 -0
- package/dist/demo.html +110 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +661 -0
- package/dist/server/dispatcher.d.ts +40 -0
- package/dist/server/handler.d.ts +20 -0
- package/dist/server/server.d.ts +20 -0
- package/dist/shared/prompt.d.ts +2 -0
- package/dist/shared/types.d.ts +69 -0
- package/dist/vite.d.ts +60 -0
- package/dist/vite.js +636 -0
- package/logo.svg +18 -0
- package/package.json +48 -4
package/dist/vite.js
ADDED
|
@@ -0,0 +1,636 @@
|
|
|
1
|
+
// src/vite.ts
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
|
|
4
|
+
// src/server/dispatcher.ts
|
|
5
|
+
import { spawn } from "node:child_process";
|
|
6
|
+
import { randomUUID } from "node:crypto";
|
|
7
|
+
import { accessSync, constants } from "node:fs";
|
|
8
|
+
import { delimiter, join } from "node:path";
|
|
9
|
+
|
|
10
|
+
// src/shared/prompt.ts
|
|
11
|
+
function formatStylesInline(styles) {
|
|
12
|
+
const entries = Object.entries(styles).filter(([, val]) => val && val !== "none" && val !== "normal" && val !== "0px");
|
|
13
|
+
if (entries.length === 0)
|
|
14
|
+
return "";
|
|
15
|
+
return entries.map(([k, v]) => `${k}: ${v}`).join("; ");
|
|
16
|
+
}
|
|
17
|
+
function formatPinSection(pin) {
|
|
18
|
+
const { context, note, index } = pin;
|
|
19
|
+
const lines = [];
|
|
20
|
+
lines.push(`### Pin #${index}: ${note.trim()}`);
|
|
21
|
+
if (context.sourceHint) {
|
|
22
|
+
lines.push(`- **Source hint**: \`${context.sourceHint}\``);
|
|
23
|
+
}
|
|
24
|
+
if (context.componentStack) {
|
|
25
|
+
lines.push(`- **Component tree**: \`${context.componentStack}\``);
|
|
26
|
+
}
|
|
27
|
+
lines.push(`- **Selector**: \`${context.selector}\``);
|
|
28
|
+
lines.push(`- **DOM path**: \`${context.breadcrumb}\``);
|
|
29
|
+
const { width, height, x, y } = context.boundingBox;
|
|
30
|
+
lines.push(`- **Box**: ${Math.round(width)}×${Math.round(height)}px at (${Math.round(x)}, ${Math.round(y)})`);
|
|
31
|
+
if (context.textExcerpt) {
|
|
32
|
+
lines.push(`- **Visible text**: ${JSON.stringify(context.textExcerpt)}`);
|
|
33
|
+
}
|
|
34
|
+
const styleSummary = formatStylesInline(context.computedStyles);
|
|
35
|
+
if (styleSummary) {
|
|
36
|
+
lines.push(`- **Computed styles**: \`${styleSummary}\``);
|
|
37
|
+
}
|
|
38
|
+
lines.push("- **DOM snippet**:");
|
|
39
|
+
lines.push("```html");
|
|
40
|
+
lines.push(context.outerHTML);
|
|
41
|
+
lines.push("```");
|
|
42
|
+
return lines.join(`
|
|
43
|
+
`);
|
|
44
|
+
}
|
|
45
|
+
function formatCuePrompt(cue) {
|
|
46
|
+
const urlPath = (() => {
|
|
47
|
+
try {
|
|
48
|
+
const parsed = new URL(cue.pageUrl);
|
|
49
|
+
return `${parsed.pathname}${parsed.search}${parsed.hash}` || "/";
|
|
50
|
+
} catch {
|
|
51
|
+
return cue.pageUrl || "/";
|
|
52
|
+
}
|
|
53
|
+
})();
|
|
54
|
+
const header = [
|
|
55
|
+
"Apply the following visual UI changes directly to the source files in this workspace.",
|
|
56
|
+
"Locate the exact source files using the selectors, visible text, DOM structure, and source hints below, then make minimal, clean edits.",
|
|
57
|
+
"",
|
|
58
|
+
`- **Page**: \`${urlPath}\`${cue.pageTitle ? ` (${cue.pageTitle})` : ""}`,
|
|
59
|
+
`- **Total Pins**: ${cue.pins.length}`
|
|
60
|
+
].join(`
|
|
61
|
+
`);
|
|
62
|
+
const pinBlocks = cue.pins.map((pin, idx) => formatPinSection({ ...pin, index: idx + 1 }));
|
|
63
|
+
return [header, ...pinBlocks].join(`
|
|
64
|
+
|
|
65
|
+
`) + `
|
|
66
|
+
`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// src/shared/types.ts
|
|
70
|
+
var SUPPORTED_AGENTS = [
|
|
71
|
+
"agy",
|
|
72
|
+
"claude",
|
|
73
|
+
"cursor-agent",
|
|
74
|
+
"codex",
|
|
75
|
+
"pi"
|
|
76
|
+
];
|
|
77
|
+
|
|
78
|
+
// src/server/dispatcher.ts
|
|
79
|
+
function buildAgentCommand(agent, prompt, continueSession) {
|
|
80
|
+
switch (agent) {
|
|
81
|
+
case "agy":
|
|
82
|
+
return {
|
|
83
|
+
bin: "agy",
|
|
84
|
+
args: [
|
|
85
|
+
"--dangerously-skip-permissions",
|
|
86
|
+
...continueSession ? ["--continue"] : [],
|
|
87
|
+
"-p",
|
|
88
|
+
prompt
|
|
89
|
+
]
|
|
90
|
+
};
|
|
91
|
+
case "claude":
|
|
92
|
+
return {
|
|
93
|
+
bin: "claude",
|
|
94
|
+
args: [
|
|
95
|
+
"--dangerously-skip-permissions",
|
|
96
|
+
...continueSession ? ["--continue"] : [],
|
|
97
|
+
"-p",
|
|
98
|
+
prompt
|
|
99
|
+
]
|
|
100
|
+
};
|
|
101
|
+
case "cursor-agent":
|
|
102
|
+
return {
|
|
103
|
+
bin: "cursor-agent",
|
|
104
|
+
args: [
|
|
105
|
+
"--yolo",
|
|
106
|
+
...continueSession ? ["--continue"] : [],
|
|
107
|
+
"-p",
|
|
108
|
+
prompt
|
|
109
|
+
]
|
|
110
|
+
};
|
|
111
|
+
case "codex":
|
|
112
|
+
if (continueSession) {
|
|
113
|
+
return {
|
|
114
|
+
bin: "codex",
|
|
115
|
+
args: [
|
|
116
|
+
"exec",
|
|
117
|
+
"resume",
|
|
118
|
+
"--dangerously-bypass-approvals-and-sandbox",
|
|
119
|
+
"--skip-git-repo-check",
|
|
120
|
+
"--last",
|
|
121
|
+
prompt
|
|
122
|
+
]
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
bin: "codex",
|
|
127
|
+
args: [
|
|
128
|
+
"exec",
|
|
129
|
+
"--dangerously-bypass-approvals-and-sandbox",
|
|
130
|
+
"--skip-git-repo-check",
|
|
131
|
+
prompt
|
|
132
|
+
]
|
|
133
|
+
};
|
|
134
|
+
case "pi":
|
|
135
|
+
return {
|
|
136
|
+
bin: "pi",
|
|
137
|
+
args: [
|
|
138
|
+
"--approve",
|
|
139
|
+
...continueSession ? ["--continue"] : [],
|
|
140
|
+
"-p",
|
|
141
|
+
"--",
|
|
142
|
+
prompt
|
|
143
|
+
]
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
function isBinaryInPath(bin, pathEnv = process.env.PATH || "") {
|
|
148
|
+
const dirs = pathEnv.split(delimiter).filter(Boolean);
|
|
149
|
+
for (const dir of dirs) {
|
|
150
|
+
const fullPath = join(dir, bin);
|
|
151
|
+
try {
|
|
152
|
+
accessSync(fullPath, constants.X_OK);
|
|
153
|
+
return true;
|
|
154
|
+
} catch {}
|
|
155
|
+
}
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
function detectAvailableAgents(pathEnv = process.env.PATH || "") {
|
|
159
|
+
return SUPPORTED_AGENTS.filter((agent) => isBinaryInPath(agent, pathEnv));
|
|
160
|
+
}
|
|
161
|
+
function createSpawnExecutor(options) {
|
|
162
|
+
const quiet = options?.quiet ?? false;
|
|
163
|
+
return (invocation) => new Promise((resolve) => {
|
|
164
|
+
const { bin, args } = invocation.command;
|
|
165
|
+
if (!quiet) {
|
|
166
|
+
console.log(`
|
|
167
|
+
\x1B[36m▶ [agentcue]\x1B[0m Running \x1B[1m${invocation.agent}\x1B[0m${invocation.continueSession ? " (continue)" : ""} · cue ${invocation.cueId.slice(0, 8)}`);
|
|
168
|
+
}
|
|
169
|
+
const child = spawn(bin, args, {
|
|
170
|
+
cwd: invocation.cwd,
|
|
171
|
+
shell: false,
|
|
172
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
173
|
+
env: process.env
|
|
174
|
+
});
|
|
175
|
+
let stderrTail = "";
|
|
176
|
+
child.stdout?.on("data", (chunk) => {
|
|
177
|
+
if (!quiet)
|
|
178
|
+
process.stdout.write(chunk);
|
|
179
|
+
});
|
|
180
|
+
child.stderr?.on("data", (chunk) => {
|
|
181
|
+
const text = chunk.toString();
|
|
182
|
+
stderrTail = (stderrTail + text).slice(-600);
|
|
183
|
+
if (!quiet)
|
|
184
|
+
process.stderr.write(chunk);
|
|
185
|
+
});
|
|
186
|
+
child.on("error", (err) => {
|
|
187
|
+
resolve({
|
|
188
|
+
ok: false,
|
|
189
|
+
exitCode: null,
|
|
190
|
+
error: `Failed to start ${bin}: ${err.message}`
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
child.on("close", (code) => {
|
|
194
|
+
if (code === 0) {
|
|
195
|
+
if (!quiet) {
|
|
196
|
+
console.log(`\x1B[32m✓ [agentcue]\x1B[0m ${invocation.agent} finished cue ${invocation.cueId.slice(0, 8)}
|
|
197
|
+
`);
|
|
198
|
+
}
|
|
199
|
+
resolve({ ok: true, exitCode: 0 });
|
|
200
|
+
} else {
|
|
201
|
+
const reason = stderrTail.trim().split(`
|
|
202
|
+
`).slice(-3).join(" ") || `${bin} exited with code ${code}`;
|
|
203
|
+
if (!quiet) {
|
|
204
|
+
console.error(`\x1B[31m✗ [agentcue]\x1B[0m ${invocation.agent} failed: ${reason}
|
|
205
|
+
`);
|
|
206
|
+
}
|
|
207
|
+
resolve({
|
|
208
|
+
ok: false,
|
|
209
|
+
exitCode: code,
|
|
210
|
+
error: reason
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
function createDispatcher(options) {
|
|
217
|
+
const agents = options.agents ?? detectAvailableAgents();
|
|
218
|
+
const preferredDefault = options.defaultAgent && agents.includes(options.defaultAgent) ? options.defaultAgent : agents[0] ?? options.defaultAgent ?? null;
|
|
219
|
+
const executor = options.executor ?? createSpawnExecutor();
|
|
220
|
+
const listeners = new Set;
|
|
221
|
+
const queue = [];
|
|
222
|
+
let state = {
|
|
223
|
+
cwd: options.cwd,
|
|
224
|
+
agents,
|
|
225
|
+
defaultAgent: preferredDefault,
|
|
226
|
+
status: "idle",
|
|
227
|
+
activeCue: null,
|
|
228
|
+
queuedCount: 0,
|
|
229
|
+
lastResult: null
|
|
230
|
+
};
|
|
231
|
+
let processing = false;
|
|
232
|
+
function emit(event) {
|
|
233
|
+
for (const listener of listeners) {
|
|
234
|
+
listener(event);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function emitState() {
|
|
238
|
+
emit({ type: "state", state: getState() });
|
|
239
|
+
}
|
|
240
|
+
function getState() {
|
|
241
|
+
return {
|
|
242
|
+
...state,
|
|
243
|
+
agents: [...state.agents],
|
|
244
|
+
activeCue: state.activeCue ? { ...state.activeCue } : null,
|
|
245
|
+
lastResult: state.lastResult ? { ...state.lastResult } : null
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
async function drainQueue() {
|
|
249
|
+
if (processing)
|
|
250
|
+
return;
|
|
251
|
+
processing = true;
|
|
252
|
+
while (queue.length > 0) {
|
|
253
|
+
const next = queue.shift();
|
|
254
|
+
const startedAt = Date.now();
|
|
255
|
+
state = {
|
|
256
|
+
...state,
|
|
257
|
+
status: "running",
|
|
258
|
+
activeCue: {
|
|
259
|
+
cueId: next.cueId,
|
|
260
|
+
agent: next.payload.agent,
|
|
261
|
+
pinCount: next.payload.pins.length,
|
|
262
|
+
startedAt
|
|
263
|
+
},
|
|
264
|
+
queuedCount: queue.length
|
|
265
|
+
};
|
|
266
|
+
emitState();
|
|
267
|
+
const command = buildAgentCommand(next.payload.agent, next.prompt, next.payload.continueSession);
|
|
268
|
+
let result;
|
|
269
|
+
try {
|
|
270
|
+
result = await executor({
|
|
271
|
+
cueId: next.cueId,
|
|
272
|
+
agent: next.payload.agent,
|
|
273
|
+
continueSession: next.payload.continueSession,
|
|
274
|
+
prompt: next.prompt,
|
|
275
|
+
cwd: options.cwd,
|
|
276
|
+
command
|
|
277
|
+
});
|
|
278
|
+
} catch (err) {
|
|
279
|
+
result = {
|
|
280
|
+
ok: false,
|
|
281
|
+
exitCode: null,
|
|
282
|
+
error: err instanceof Error ? err.message : String(err)
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
const finishedAt = Date.now();
|
|
286
|
+
const durationMs = finishedAt - startedAt;
|
|
287
|
+
state = {
|
|
288
|
+
...state,
|
|
289
|
+
status: queue.length > 0 ? "running" : result.ok ? "done" : "error",
|
|
290
|
+
activeCue: null,
|
|
291
|
+
queuedCount: queue.length,
|
|
292
|
+
lastResult: {
|
|
293
|
+
cueId: next.cueId,
|
|
294
|
+
agent: next.payload.agent,
|
|
295
|
+
ok: result.ok,
|
|
296
|
+
durationMs,
|
|
297
|
+
error: result.error,
|
|
298
|
+
finishedAt
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
if (result.ok) {
|
|
302
|
+
emit({
|
|
303
|
+
type: "cue:done",
|
|
304
|
+
cueId: next.cueId,
|
|
305
|
+
agent: next.payload.agent,
|
|
306
|
+
durationMs
|
|
307
|
+
});
|
|
308
|
+
} else {
|
|
309
|
+
emit({
|
|
310
|
+
type: "cue:error",
|
|
311
|
+
cueId: next.cueId,
|
|
312
|
+
agent: next.payload.agent,
|
|
313
|
+
error: result.error || "Agent command failed"
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
emitState();
|
|
317
|
+
}
|
|
318
|
+
processing = false;
|
|
319
|
+
}
|
|
320
|
+
function enqueue(payload) {
|
|
321
|
+
const cueId = randomUUID();
|
|
322
|
+
const prompt = formatCuePrompt(payload);
|
|
323
|
+
queue.push({ cueId, payload, prompt });
|
|
324
|
+
const queuePosition = queue.length + (state.activeCue ? 1 : 0);
|
|
325
|
+
state = {
|
|
326
|
+
...state,
|
|
327
|
+
queuedCount: queue.length
|
|
328
|
+
};
|
|
329
|
+
emitState();
|
|
330
|
+
drainQueue();
|
|
331
|
+
return { cueId, queuePosition };
|
|
332
|
+
}
|
|
333
|
+
function subscribe(listener) {
|
|
334
|
+
listeners.add(listener);
|
|
335
|
+
return () => {
|
|
336
|
+
listeners.delete(listener);
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
return {
|
|
340
|
+
enqueue,
|
|
341
|
+
getState,
|
|
342
|
+
subscribe
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// src/server/handler.ts
|
|
347
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
348
|
+
import { dirname, join as join2 } from "node:path";
|
|
349
|
+
import { fileURLToPath } from "node:url";
|
|
350
|
+
var CORS_HEADERS = {
|
|
351
|
+
"Access-Control-Allow-Origin": "*",
|
|
352
|
+
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
|
|
353
|
+
"Access-Control-Allow-Headers": "Content-Type",
|
|
354
|
+
"Access-Control-Allow-Private-Network": "true"
|
|
355
|
+
};
|
|
356
|
+
function sendJson(res, status, body) {
|
|
357
|
+
res.writeHead(status, {
|
|
358
|
+
...CORS_HEADERS,
|
|
359
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
360
|
+
"Cache-Control": "no-store"
|
|
361
|
+
});
|
|
362
|
+
res.end(JSON.stringify(body));
|
|
363
|
+
}
|
|
364
|
+
function readJsonBody(req, maxBytes = 512000) {
|
|
365
|
+
return new Promise((resolveBody, reject) => {
|
|
366
|
+
const chunks = [];
|
|
367
|
+
let size = 0;
|
|
368
|
+
req.on("data", (chunk) => {
|
|
369
|
+
size += chunk.length;
|
|
370
|
+
if (size > maxBytes) {
|
|
371
|
+
reject(new Error("Payload too large"));
|
|
372
|
+
req.destroy();
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
chunks.push(chunk);
|
|
376
|
+
});
|
|
377
|
+
req.on("end", () => {
|
|
378
|
+
try {
|
|
379
|
+
const raw = Buffer.concat(chunks).toString("utf-8");
|
|
380
|
+
resolveBody(raw ? JSON.parse(raw) : {});
|
|
381
|
+
} catch {
|
|
382
|
+
reject(new Error("Invalid JSON payload"));
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
req.on("error", reject);
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
function validateCuePayload(raw, allowedAgents) {
|
|
389
|
+
if (!raw || typeof raw !== "object") {
|
|
390
|
+
throw new Error("Cue body must be a JSON object");
|
|
391
|
+
}
|
|
392
|
+
const obj = raw;
|
|
393
|
+
const agent = obj.agent;
|
|
394
|
+
if (!SUPPORTED_AGENTS.includes(agent)) {
|
|
395
|
+
throw new Error(`Unsupported agent "${String(obj.agent)}"`);
|
|
396
|
+
}
|
|
397
|
+
if (allowedAgents.length > 0 && !allowedAgents.includes(agent)) {
|
|
398
|
+
throw new Error(`Agent "${agent}" is not installed on PATH`);
|
|
399
|
+
}
|
|
400
|
+
if (!Array.isArray(obj.pins) || obj.pins.length === 0) {
|
|
401
|
+
throw new Error("Cue must contain at least one Pin");
|
|
402
|
+
}
|
|
403
|
+
for (const pin of obj.pins) {
|
|
404
|
+
if (!pin || typeof pin !== "object")
|
|
405
|
+
throw new Error("Invalid Pin entry");
|
|
406
|
+
const p = pin;
|
|
407
|
+
if (typeof p.note !== "string" || !p.note.trim()) {
|
|
408
|
+
throw new Error("Every Pin requires a non-empty note");
|
|
409
|
+
}
|
|
410
|
+
if (!p.context || typeof p.context !== "object") {
|
|
411
|
+
throw new Error("Every Pin requires DOM context");
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
return {
|
|
415
|
+
agent,
|
|
416
|
+
continueSession: Boolean(obj.continueSession),
|
|
417
|
+
pageUrl: typeof obj.pageUrl === "string" ? obj.pageUrl : "/",
|
|
418
|
+
pageTitle: typeof obj.pageTitle === "string" ? obj.pageTitle : "",
|
|
419
|
+
pins: obj.pins
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
function resolveClientBundlePath() {
|
|
423
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
424
|
+
const candidates = [
|
|
425
|
+
join2(here, "client.js"),
|
|
426
|
+
join2(here, "..", "dist", "client.js"),
|
|
427
|
+
join2(here, "..", "..", "dist", "client.js")
|
|
428
|
+
];
|
|
429
|
+
return candidates.find((p) => existsSync(p)) || candidates[0];
|
|
430
|
+
}
|
|
431
|
+
function createBridgeHandler(options) {
|
|
432
|
+
const prefix = (options.basePath || "").replace(/\/+$/, "");
|
|
433
|
+
const bundlePath = options.clientBundlePath ?? resolveClientBundlePath();
|
|
434
|
+
const activeCleanups = new Set;
|
|
435
|
+
async function handleCuePost(req, res) {
|
|
436
|
+
try {
|
|
437
|
+
const rawBody = await readJsonBody(req);
|
|
438
|
+
const state = options.dispatcher.getState();
|
|
439
|
+
const payload = validateCuePayload(rawBody, state.agents);
|
|
440
|
+
const queued = options.dispatcher.enqueue(payload);
|
|
441
|
+
sendJson(res, 202, {
|
|
442
|
+
ok: true,
|
|
443
|
+
cueId: queued.cueId,
|
|
444
|
+
queuePosition: queued.queuePosition
|
|
445
|
+
});
|
|
446
|
+
} catch (err) {
|
|
447
|
+
sendJson(res, 400, {
|
|
448
|
+
ok: false,
|
|
449
|
+
error: err instanceof Error ? err.message : "Bad request"
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
function middleware(req, res, next) {
|
|
454
|
+
const method = req.method || "GET";
|
|
455
|
+
const urlObj = new URL(req.url || "/", `http://${req.headers.host || "localhost"}`);
|
|
456
|
+
const pathname = urlObj.pathname;
|
|
457
|
+
let subPath = pathname;
|
|
458
|
+
if (prefix) {
|
|
459
|
+
if (pathname !== prefix && !pathname.startsWith(`${prefix}/`)) {
|
|
460
|
+
next?.();
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
subPath = pathname.slice(prefix.length) || "/";
|
|
464
|
+
}
|
|
465
|
+
if (method === "OPTIONS") {
|
|
466
|
+
res.writeHead(204, CORS_HEADERS);
|
|
467
|
+
res.end();
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
if (method === "GET" && subPath === "/client.js") {
|
|
471
|
+
const origin = typeof options.clientOrigin === "function" ? options.clientOrigin() : options.clientOrigin;
|
|
472
|
+
const scriptBody = options.clientScript ?? (existsSync(bundlePath) ? readFileSync(bundlePath, "utf-8") : "console.error('[agentcue] dist/client.js not built yet.');");
|
|
473
|
+
const injected = `window.__AGENTCUE_ORIGIN__=${JSON.stringify(origin)};
|
|
474
|
+
${scriptBody}`;
|
|
475
|
+
res.writeHead(200, {
|
|
476
|
+
...CORS_HEADERS,
|
|
477
|
+
"Content-Type": "application/javascript; charset=utf-8",
|
|
478
|
+
"Cache-Control": "no-store"
|
|
479
|
+
});
|
|
480
|
+
res.end(injected);
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
if (method === "GET" && subPath === "/api/state") {
|
|
484
|
+
sendJson(res, 200, options.dispatcher.getState());
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
if (method === "GET" && subPath === "/api/events") {
|
|
488
|
+
res.writeHead(200, {
|
|
489
|
+
...CORS_HEADERS,
|
|
490
|
+
"Content-Type": "text/event-stream; charset=utf-8",
|
|
491
|
+
"Cache-Control": "no-cache, no-transform",
|
|
492
|
+
"X-Accel-Buffering": "no",
|
|
493
|
+
...req.httpVersionMajor >= 2 ? {} : { Connection: "keep-alive" }
|
|
494
|
+
});
|
|
495
|
+
res.flushHeaders?.();
|
|
496
|
+
const writeEvent = (evt) => {
|
|
497
|
+
if (!res.writableEnded) {
|
|
498
|
+
res.write(`data: ${JSON.stringify(evt)}
|
|
499
|
+
|
|
500
|
+
`);
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
writeEvent({ type: "state", state: options.dispatcher.getState() });
|
|
504
|
+
const unsubscribe = options.dispatcher.subscribe(writeEvent);
|
|
505
|
+
const heartbeat = setInterval(() => {
|
|
506
|
+
if (!res.writableEnded) {
|
|
507
|
+
res.write(`: ping
|
|
508
|
+
|
|
509
|
+
`);
|
|
510
|
+
}
|
|
511
|
+
}, 15000);
|
|
512
|
+
heartbeat.unref?.();
|
|
513
|
+
let closed = false;
|
|
514
|
+
const cleanup = () => {
|
|
515
|
+
if (closed)
|
|
516
|
+
return;
|
|
517
|
+
closed = true;
|
|
518
|
+
clearInterval(heartbeat);
|
|
519
|
+
unsubscribe();
|
|
520
|
+
activeCleanups.delete(forceClose);
|
|
521
|
+
};
|
|
522
|
+
const forceClose = () => {
|
|
523
|
+
cleanup();
|
|
524
|
+
if (!res.writableEnded) {
|
|
525
|
+
res.end();
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
activeCleanups.add(forceClose);
|
|
529
|
+
req.on("close", cleanup);
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
if (method === "POST" && subPath === "/api/cue") {
|
|
533
|
+
handleCuePost(req, res);
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
if (prefix) {
|
|
537
|
+
sendJson(res, 404, { error: "Not found" });
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
540
|
+
if (next) {
|
|
541
|
+
next();
|
|
542
|
+
} else {
|
|
543
|
+
sendJson(res, 404, { error: "Not found" });
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
function dispose() {
|
|
547
|
+
for (const closeStream of activeCleanups) {
|
|
548
|
+
closeStream();
|
|
549
|
+
}
|
|
550
|
+
activeCleanups.clear();
|
|
551
|
+
}
|
|
552
|
+
return { middleware, dispose };
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
// src/vite.ts
|
|
556
|
+
function normalizeMountPath(viteBase, rawBasePath) {
|
|
557
|
+
const cleanMount = `/${rawBasePath.replace(/^\/+|\/+$/g, "")}`;
|
|
558
|
+
const trimmedViteBase = viteBase.trim();
|
|
559
|
+
if (!trimmedViteBase || trimmedViteBase === "/" || trimmedViteBase.startsWith(".")) {
|
|
560
|
+
return cleanMount;
|
|
561
|
+
}
|
|
562
|
+
const cleanViteBase = `/${trimmedViteBase.replace(/^\/+|\/+$/g, "")}`;
|
|
563
|
+
if (cleanMount === cleanViteBase || cleanMount.startsWith(`${cleanViteBase}/`)) {
|
|
564
|
+
return cleanMount;
|
|
565
|
+
}
|
|
566
|
+
return `${cleanViteBase}${cleanMount}`;
|
|
567
|
+
}
|
|
568
|
+
function agentcue(options = {}) {
|
|
569
|
+
const enabled = options.enabled ?? true;
|
|
570
|
+
const rawBasePath = options.basePath ?? "/__agentcue";
|
|
571
|
+
let resolvedRoot = process.cwd();
|
|
572
|
+
let mountPath = normalizeMountPath("/", rawBasePath);
|
|
573
|
+
return {
|
|
574
|
+
name: "vite-plugin-agentcue",
|
|
575
|
+
apply: "serve",
|
|
576
|
+
configResolved(config) {
|
|
577
|
+
resolvedRoot = config.root;
|
|
578
|
+
mountPath = normalizeMountPath(config.base || "/", rawBasePath);
|
|
579
|
+
},
|
|
580
|
+
configureServer(server) {
|
|
581
|
+
if (!enabled)
|
|
582
|
+
return;
|
|
583
|
+
const root = server.config?.root || resolvedRoot;
|
|
584
|
+
const base = server.config?.base || "/";
|
|
585
|
+
mountPath = normalizeMountPath(base, rawBasePath);
|
|
586
|
+
const cwd = options.cwd ? resolve(root, options.cwd) : resolve(root);
|
|
587
|
+
const dispatcher = createDispatcher({
|
|
588
|
+
cwd,
|
|
589
|
+
agents: options.agents,
|
|
590
|
+
defaultAgent: options.defaultAgent,
|
|
591
|
+
executor: options.executor
|
|
592
|
+
});
|
|
593
|
+
const bridge = createBridgeHandler({
|
|
594
|
+
dispatcher,
|
|
595
|
+
basePath: mountPath,
|
|
596
|
+
clientOrigin: mountPath,
|
|
597
|
+
clientScript: options.clientScript,
|
|
598
|
+
clientBundlePath: resolveClientBundlePath()
|
|
599
|
+
});
|
|
600
|
+
server.middlewares.use((req, res, next) => {
|
|
601
|
+
bridge.middleware(req, res, next);
|
|
602
|
+
});
|
|
603
|
+
server.httpServer?.once("close", () => {
|
|
604
|
+
bridge.dispose();
|
|
605
|
+
});
|
|
606
|
+
if (!options.quiet && typeof server.printUrls === "function") {
|
|
607
|
+
const originalPrintUrls = server.printUrls.bind(server);
|
|
608
|
+
server.printUrls = () => {
|
|
609
|
+
originalPrintUrls();
|
|
610
|
+
const state = dispatcher.getState();
|
|
611
|
+
const agentList = SUPPORTED_AGENTS.map((a) => state.agents.includes(a) ? `\x1B[32m✓ ${a}\x1B[0m` : `\x1B[90m✗ ${a}\x1B[0m`).join(" ");
|
|
612
|
+
server.config.logger.info(` \x1B[36m➜\x1B[0m \x1B[1magentcue\x1B[0m: press \x1B[1mCmd/Ctrl + Shift + F\x1B[0m in browser · ${agentList}`);
|
|
613
|
+
};
|
|
614
|
+
}
|
|
615
|
+
},
|
|
616
|
+
transformIndexHtml() {
|
|
617
|
+
if (!enabled)
|
|
618
|
+
return [];
|
|
619
|
+
return [
|
|
620
|
+
{
|
|
621
|
+
tag: "script",
|
|
622
|
+
attrs: {
|
|
623
|
+
src: `${mountPath}/client.js`,
|
|
624
|
+
defer: true
|
|
625
|
+
},
|
|
626
|
+
injectTo: "body"
|
|
627
|
+
}
|
|
628
|
+
];
|
|
629
|
+
}
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
var vite_default = agentcue;
|
|
633
|
+
export {
|
|
634
|
+
agentcue,
|
|
635
|
+
vite_default as default
|
|
636
|
+
};
|
package/logo.svg
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" fill="none">
|
|
2
|
+
<rect width="32" height="32" rx="8" fill="#111113" />
|
|
3
|
+
<rect x="0.5" y="0.5" width="31" height="31" rx="7.5" stroke="#ffffff" stroke-opacity="0.12" />
|
|
4
|
+
<g transform="translate(4 4)">
|
|
5
|
+
<path
|
|
6
|
+
d="M19 9.5V7a3 3 0 0 0-3-3H7a3 3 0 0 0-3 3v9a3 3 0 0 0 3 3h2.5"
|
|
7
|
+
stroke="#3b82f6"
|
|
8
|
+
stroke-width="2.25"
|
|
9
|
+
stroke-linecap="round"
|
|
10
|
+
stroke-linejoin="round"
|
|
11
|
+
/>
|
|
12
|
+
<circle cx="9.5" cy="9.5" r="1.75" fill="#3b82f6" />
|
|
13
|
+
<path
|
|
14
|
+
d="M15.5 10C16.15 14.05 16.95 14.85 21 15.5C16.95 16.15 16.15 16.95 15.5 21C14.85 16.95 14.05 16.15 10 15.5C14.05 14.85 14.85 14.05 15.5 10Z"
|
|
15
|
+
fill="#f8fafc"
|
|
16
|
+
/>
|
|
17
|
+
</g>
|
|
18
|
+
</svg>
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agentcue",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Pick DOM nodes in the browser and push structured UI edit Cues straight to your local Agent CLI (agy, claude, cursor-agent, codex, pi).",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://agentcue.yuler.dev",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/yuler/agentcue.git",
|
|
10
|
+
"directory": "packages/agentcue"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"bin": {
|
|
14
|
+
"agentcue": "./dist/cli.js"
|
|
15
|
+
},
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./vite": {
|
|
24
|
+
"types": "./dist/vite.d.ts",
|
|
25
|
+
"default": "./dist/vite.js"
|
|
26
|
+
},
|
|
27
|
+
"./client": "./dist/client.js"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE",
|
|
33
|
+
"logo.svg"
|
|
34
|
+
],
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"vite": ">=4.0.0"
|
|
37
|
+
},
|
|
38
|
+
"peerDependenciesMeta": {
|
|
39
|
+
"vite": {
|
|
40
|
+
"optional": true
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/node": "^22.0.0",
|
|
45
|
+
"typescript": "^5.8.0"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=22"
|
|
49
|
+
}
|
|
50
|
+
}
|