agent-office 0.0.1 → 0.0.2
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/dist/cli.js +84 -3
- package/dist/commands/serve.js +7 -1
- package/dist/commands/worker.d.ts +11 -0
- package/dist/commands/worker.js +47 -0
- package/dist/db/index.d.ts +19 -0
- package/dist/db/migrate.js +36 -0
- package/dist/manage/app.js +42 -43
- package/dist/manage/components/CronList.d.ts +9 -0
- package/dist/manage/components/CronList.js +310 -0
- package/dist/manage/components/ItemSelector.d.ts +7 -0
- package/dist/manage/components/ItemSelector.js +20 -0
- package/dist/manage/components/MenuSelect.d.ts +13 -0
- package/dist/manage/components/MenuSelect.js +22 -0
- package/dist/manage/components/MyMail.d.ts +2 -1
- package/dist/manage/components/MyMail.js +107 -34
- package/dist/manage/components/ReadMail.js +3 -3
- package/dist/manage/components/SendMessage.d.ts +2 -1
- package/dist/manage/components/SendMessage.js +9 -6
- package/dist/manage/components/SessionList.js +392 -31
- package/dist/manage/hooks/useApi.d.ts +43 -1
- package/dist/manage/hooks/useApi.js +34 -2
- package/dist/server/cron.d.ts +24 -0
- package/dist/server/cron.js +121 -0
- package/dist/server/index.d.ts +2 -1
- package/dist/server/index.js +2 -2
- package/dist/server/routes.d.ts +2 -1
- package/dist/server/routes.js +719 -38
- package/package.json +2 -1
package/dist/server/routes.js
CHANGED
|
@@ -1,13 +1,42 @@
|
|
|
1
1
|
import { Router } from "express";
|
|
2
|
-
|
|
2
|
+
import { Cron as CronerInstance } from "croner";
|
|
3
|
+
const MAIL_INJECTION_BLURB = [
|
|
4
|
+
``,
|
|
5
|
+
`---`,
|
|
6
|
+
`You have a new message. Please review the injected message above and respond accordingly.`,
|
|
7
|
+
`Respond using markdown. Your markdown front-matter can contain a property "choices" which`,
|
|
8
|
+
`is an array of choices for the message sender to choose from. These choices are optional`,
|
|
9
|
+
`and shouldn't alter your authentic personality in your responses.`,
|
|
10
|
+
`IMPORTANT: in order for the sender to see your response, you must send them a message back`,
|
|
11
|
+
`using the \`agent-office worker send-message\` tool. Also, don't go on for too long if things seem repetitive.`,
|
|
12
|
+
].join("\n");
|
|
13
|
+
export function createRouter(sql, opencode, serverUrl, scheduler) {
|
|
3
14
|
const router = Router();
|
|
4
15
|
router.get("/health", (_req, res) => {
|
|
5
16
|
res.json({ ok: true });
|
|
6
17
|
});
|
|
18
|
+
router.get("/modes", async (_req, res) => {
|
|
19
|
+
try {
|
|
20
|
+
const config = await opencode.config.get();
|
|
21
|
+
const agent = config.agent ?? {};
|
|
22
|
+
const modes = Object.entries(agent)
|
|
23
|
+
.filter(([, val]) => val != null)
|
|
24
|
+
.map(([name, val]) => ({
|
|
25
|
+
name,
|
|
26
|
+
description: val.description ?? "",
|
|
27
|
+
model: val.model ?? "",
|
|
28
|
+
}));
|
|
29
|
+
res.json(modes);
|
|
30
|
+
}
|
|
31
|
+
catch (err) {
|
|
32
|
+
console.error("GET /modes error:", err);
|
|
33
|
+
res.json([]);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
7
36
|
router.get("/sessions", async (_req, res) => {
|
|
8
37
|
try {
|
|
9
38
|
const rows = await sql `
|
|
10
|
-
SELECT id, name, session_id, agent_code, created_at
|
|
39
|
+
SELECT id, name, session_id, agent_code, mode, created_at
|
|
11
40
|
FROM sessions
|
|
12
41
|
ORDER BY created_at DESC
|
|
13
42
|
`;
|
|
@@ -19,12 +48,13 @@ export function createRouter(sql, opencode, serverUrl) {
|
|
|
19
48
|
}
|
|
20
49
|
});
|
|
21
50
|
router.post("/sessions", async (req, res) => {
|
|
22
|
-
const { name } = req.body;
|
|
51
|
+
const { name, mode } = req.body;
|
|
23
52
|
if (!name || typeof name !== "string" || !name.trim()) {
|
|
24
53
|
res.status(400).json({ error: "name is required" });
|
|
25
54
|
return;
|
|
26
55
|
}
|
|
27
56
|
const trimmedName = name.trim();
|
|
57
|
+
const trimmedMode = typeof mode === "string" && mode.trim() ? mode.trim() : null;
|
|
28
58
|
const existing = await sql `
|
|
29
59
|
SELECT id FROM sessions WHERE name = ${trimmedName}
|
|
30
60
|
`;
|
|
@@ -45,9 +75,9 @@ export function createRouter(sql, opencode, serverUrl) {
|
|
|
45
75
|
let row;
|
|
46
76
|
try {
|
|
47
77
|
const [inserted] = await sql `
|
|
48
|
-
INSERT INTO sessions (name, session_id)
|
|
49
|
-
VALUES (${trimmedName}, ${opencodeSessionId})
|
|
50
|
-
RETURNING id, name, session_id, agent_code, created_at
|
|
78
|
+
INSERT INTO sessions (name, session_id, mode)
|
|
79
|
+
VALUES (${trimmedName}, ${opencodeSessionId}, ${trimmedMode})
|
|
80
|
+
RETURNING id, name, session_id, agent_code, mode, created_at
|
|
51
81
|
`;
|
|
52
82
|
row = inserted;
|
|
53
83
|
}
|
|
@@ -65,11 +95,13 @@ export function createRouter(sql, opencode, serverUrl) {
|
|
|
65
95
|
const defaultEntry = Object.entries(providers.default)[0];
|
|
66
96
|
if (defaultEntry) {
|
|
67
97
|
const clockInToken = `${row.agent_code}@${serverUrl}`;
|
|
68
|
-
const
|
|
98
|
+
const modeNote = trimmedMode ? `\n Mode: ${trimmedMode}` : "";
|
|
99
|
+
const firstMessage = `You have been enrolled in the agent office.${modeNote}\n\nTo clock in and receive your full briefing, run:\n\n agent-office worker clock-in ${clockInToken}`;
|
|
69
100
|
await opencode.session.chat(opencodeSessionId, {
|
|
70
101
|
modelID: defaultEntry[0],
|
|
71
102
|
providerID: defaultEntry[1],
|
|
72
103
|
parts: [{ type: "text", text: firstMessage }],
|
|
104
|
+
...(trimmedMode ? { mode: trimmedMode } : {}),
|
|
73
105
|
});
|
|
74
106
|
}
|
|
75
107
|
}
|
|
@@ -321,23 +353,21 @@ export function createRouter(sql, opencode, serverUrl) {
|
|
|
321
353
|
`;
|
|
322
354
|
if (sessionMap.has(recipient)) {
|
|
323
355
|
const sessionId = sessionMap.get(recipient);
|
|
324
|
-
|
|
325
|
-
|
|
356
|
+
const msgId = msgRow.id;
|
|
357
|
+
injected = true;
|
|
358
|
+
opencode.app.providers().then((providers) => {
|
|
326
359
|
const defaultEntry = Object.entries(providers.default)[0];
|
|
327
|
-
if (defaultEntry)
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
catch (err) {
|
|
360
|
+
if (!defaultEntry)
|
|
361
|
+
return;
|
|
362
|
+
const injectText = `[Message from "${trimmedFrom}"]: ${trimmedBody}${MAIL_INJECTION_BLURB}`;
|
|
363
|
+
return opencode.session.chat(sessionId, {
|
|
364
|
+
modelID: defaultEntry[0],
|
|
365
|
+
providerID: defaultEntry[1],
|
|
366
|
+
parts: [{ type: "text", text: injectText }],
|
|
367
|
+
}).then(() => sql `UPDATE messages SET injected = TRUE WHERE id = ${msgId}`);
|
|
368
|
+
}).catch((err) => {
|
|
339
369
|
console.warn(`Warning: could not inject message into session ${recipient}:`, err);
|
|
340
|
-
}
|
|
370
|
+
});
|
|
341
371
|
}
|
|
342
372
|
results.push({ to: recipient, messageId: msgRow.id, injected });
|
|
343
373
|
}
|
|
@@ -365,6 +395,253 @@ export function createRouter(sql, opencode, serverUrl) {
|
|
|
365
395
|
res.status(500).json({ error: "Internal server error" });
|
|
366
396
|
}
|
|
367
397
|
});
|
|
398
|
+
// ── Cron Jobs Endpoints ────────────────────────────────────────────────────
|
|
399
|
+
router.get("/crons", async (req, res) => {
|
|
400
|
+
try {
|
|
401
|
+
const { session_name } = req.query;
|
|
402
|
+
let rows;
|
|
403
|
+
if (session_name) {
|
|
404
|
+
rows = await sql `
|
|
405
|
+
SELECT id, name, session_name, schedule, timezone, message, enabled, created_at, last_run
|
|
406
|
+
FROM cron_jobs
|
|
407
|
+
WHERE session_name = ${session_name}
|
|
408
|
+
ORDER BY name
|
|
409
|
+
`;
|
|
410
|
+
}
|
|
411
|
+
else {
|
|
412
|
+
rows = await sql `
|
|
413
|
+
SELECT id, name, session_name, schedule, timezone, message, enabled, created_at, last_run
|
|
414
|
+
FROM cron_jobs
|
|
415
|
+
ORDER BY name
|
|
416
|
+
`;
|
|
417
|
+
}
|
|
418
|
+
const jobs = rows.map((job) => {
|
|
419
|
+
let nextRun = null;
|
|
420
|
+
if (job.enabled) {
|
|
421
|
+
try {
|
|
422
|
+
const options = {};
|
|
423
|
+
if (job.timezone)
|
|
424
|
+
options.timezone = job.timezone;
|
|
425
|
+
const cronJob = new CronerInstance(job.schedule, options);
|
|
426
|
+
const next = cronJob.nextRun();
|
|
427
|
+
nextRun = next ? next.toISOString() : null;
|
|
428
|
+
}
|
|
429
|
+
catch { }
|
|
430
|
+
}
|
|
431
|
+
return { ...job, next_run: nextRun, last_run: job.last_run ? job.last_run.toISOString() : null, created_at: job.created_at.toISOString() };
|
|
432
|
+
});
|
|
433
|
+
res.json(jobs);
|
|
434
|
+
}
|
|
435
|
+
catch (err) {
|
|
436
|
+
console.error("GET /crons error:", err);
|
|
437
|
+
res.status(500).json({ error: "Internal server error" });
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
router.post("/crons", async (req, res) => {
|
|
441
|
+
const { name, session_name, schedule, message, timezone } = req.body;
|
|
442
|
+
if (!name || typeof name !== "string" || !name.trim()) {
|
|
443
|
+
res.status(400).json({ error: "name is required" });
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
if (!session_name || typeof session_name !== "string") {
|
|
447
|
+
res.status(400).json({ error: "session_name is required" });
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
if (!schedule || typeof schedule !== "string" || !schedule.trim()) {
|
|
451
|
+
res.status(400).json({ error: "schedule is required" });
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
if (!message || typeof message !== "string" || !message.trim()) {
|
|
455
|
+
res.status(400).json({ error: "message is required" });
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
const trimmedName = name.trim();
|
|
459
|
+
const trimmedSchedule = schedule.trim();
|
|
460
|
+
const trimmedMessage = message.trim();
|
|
461
|
+
try {
|
|
462
|
+
new CronerInstance(trimmedSchedule);
|
|
463
|
+
}
|
|
464
|
+
catch {
|
|
465
|
+
res.status(400).json({ error: "Invalid cron schedule expression" });
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
if (timezone) {
|
|
469
|
+
try {
|
|
470
|
+
new CronerInstance("0 0 * * *", { timezone });
|
|
471
|
+
}
|
|
472
|
+
catch {
|
|
473
|
+
res.status(400).json({ error: "Invalid timezone" });
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
const [existing] = await sql `
|
|
478
|
+
SELECT id FROM cron_jobs WHERE name = ${trimmedName} AND session_name = ${session_name}
|
|
479
|
+
`;
|
|
480
|
+
if (existing) {
|
|
481
|
+
res.status(409).json({ error: `Cron job "${trimmedName}" already exists for this session` });
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
try {
|
|
485
|
+
const [inserted] = await sql `
|
|
486
|
+
INSERT INTO cron_jobs (name, session_name, schedule, timezone, message)
|
|
487
|
+
VALUES (${trimmedName}, ${session_name}, ${trimmedSchedule}, ${timezone ?? null}, ${trimmedMessage})
|
|
488
|
+
RETURNING id, name, session_name, schedule, timezone, message, enabled, created_at, last_run
|
|
489
|
+
`;
|
|
490
|
+
const cronJobrow = inserted;
|
|
491
|
+
scheduler.addCronJob(cronJobrow);
|
|
492
|
+
const nextRun = cronJobrow.enabled ? (() => {
|
|
493
|
+
try {
|
|
494
|
+
const options = {};
|
|
495
|
+
if (cronJobrow.timezone)
|
|
496
|
+
options.timezone = cronJobrow.timezone;
|
|
497
|
+
const cronJob = new CronerInstance(cronJobrow.schedule, options);
|
|
498
|
+
const next = cronJob.nextRun();
|
|
499
|
+
return next ? next.toISOString() : null;
|
|
500
|
+
}
|
|
501
|
+
catch {
|
|
502
|
+
return null;
|
|
503
|
+
}
|
|
504
|
+
})() : null;
|
|
505
|
+
res.status(201).json({
|
|
506
|
+
...cronJobrow,
|
|
507
|
+
next_run: nextRun,
|
|
508
|
+
last_run: cronJobrow?.last_run?.toISOString() ?? null,
|
|
509
|
+
created_at: cronJobrow.created_at.toISOString(),
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
catch (err) {
|
|
513
|
+
console.error("POST /crons error:", err);
|
|
514
|
+
res.status(500).json({ error: "Internal server error" });
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
router.delete("/crons/:id", async (req, res) => {
|
|
518
|
+
const id = parseInt(req.params.id, 10);
|
|
519
|
+
if (isNaN(id)) {
|
|
520
|
+
res.status(400).json({ error: "Invalid cron job id" });
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
try {
|
|
524
|
+
const [job] = await sql `SELECT id, name FROM cron_jobs WHERE id = ${id}`;
|
|
525
|
+
if (!job) {
|
|
526
|
+
res.status(404).json({ error: "Cron job not found" });
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
scheduler.removeCronJob(id);
|
|
530
|
+
await sql `DELETE FROM cron_jobs WHERE id = ${id}`;
|
|
531
|
+
res.json({ deleted: true, id, name: job.name });
|
|
532
|
+
}
|
|
533
|
+
catch (err) {
|
|
534
|
+
console.error("DELETE /crons/:id error:", err);
|
|
535
|
+
res.status(500).json({ error: "Internal server error" });
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
router.post("/crons/:id/enable", async (req, res) => {
|
|
539
|
+
const id = parseInt(req.params.id, 10);
|
|
540
|
+
if (isNaN(id)) {
|
|
541
|
+
res.status(400).json({ error: "Invalid cron job id" });
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
try {
|
|
545
|
+
const [existing] = await sql `
|
|
546
|
+
SELECT id, name FROM cron_jobs WHERE id = ${id}
|
|
547
|
+
`;
|
|
548
|
+
if (!existing) {
|
|
549
|
+
res.status(404).json({ error: "Cron job not found" });
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
await sql `UPDATE cron_jobs SET enabled = TRUE WHERE id = ${id}`;
|
|
553
|
+
const [updated] = await sql `
|
|
554
|
+
SELECT id, name, session_name, schedule, timezone, message, enabled, created_at, last_run
|
|
555
|
+
FROM cron_jobs WHERE id = ${id}
|
|
556
|
+
`;
|
|
557
|
+
if (updated) {
|
|
558
|
+
scheduler.enableCronJob(updated);
|
|
559
|
+
const nextRun = (() => {
|
|
560
|
+
try {
|
|
561
|
+
const options = {};
|
|
562
|
+
if (updated.timezone)
|
|
563
|
+
options.timezone = updated.timezone;
|
|
564
|
+
const cronJob = new CronerInstance(updated.schedule, options);
|
|
565
|
+
const next = cronJob.nextRun();
|
|
566
|
+
return next ? next.toISOString() : null;
|
|
567
|
+
}
|
|
568
|
+
catch {
|
|
569
|
+
return null;
|
|
570
|
+
}
|
|
571
|
+
})();
|
|
572
|
+
res.json({
|
|
573
|
+
...updated,
|
|
574
|
+
next_run: nextRun,
|
|
575
|
+
last_run: updated.last_run?.toISOString() ?? null,
|
|
576
|
+
created_at: updated.created_at.toISOString(),
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
else {
|
|
580
|
+
res.status(404).json({ error: "Cron job not found" });
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
catch (err) {
|
|
584
|
+
console.error("POST /crons/:id/enable error:", err);
|
|
585
|
+
res.status(500).json({ error: "Internal server error" });
|
|
586
|
+
}
|
|
587
|
+
});
|
|
588
|
+
router.post("/crons/:id/disable", async (req, res) => {
|
|
589
|
+
const id = parseInt(req.params.id, 10);
|
|
590
|
+
if (isNaN(id)) {
|
|
591
|
+
res.status(400).json({ error: "Invalid cron job id" });
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
try {
|
|
595
|
+
const [job] = await sql `
|
|
596
|
+
SELECT id, name FROM cron_jobs WHERE id = ${id}
|
|
597
|
+
`;
|
|
598
|
+
if (!job) {
|
|
599
|
+
res.status(404).json({ error: "Cron job not found" });
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
await sql `UPDATE cron_jobs SET enabled = FALSE WHERE id = ${id}`;
|
|
603
|
+
scheduler.disableCronJob(id);
|
|
604
|
+
const [updated] = await sql `
|
|
605
|
+
SELECT id, name, session_name, schedule, timezone, message, enabled, created_at, last_run
|
|
606
|
+
FROM cron_jobs WHERE id = ${id}
|
|
607
|
+
`;
|
|
608
|
+
res.json({
|
|
609
|
+
...updated,
|
|
610
|
+
next_run: null,
|
|
611
|
+
last_run: updated?.last_run?.toISOString() ?? null,
|
|
612
|
+
created_at: updated?.created_at.toISOString() ?? null,
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
catch (err) {
|
|
616
|
+
console.error("POST /crons/:id/disable error:", err);
|
|
617
|
+
res.status(500).json({ error: "Internal server error" });
|
|
618
|
+
}
|
|
619
|
+
});
|
|
620
|
+
router.get("/crons/:id/history", async (req, res) => {
|
|
621
|
+
const id = parseInt(req.params.id, 10);
|
|
622
|
+
const limit = Math.min(parseInt(req.query.limit ?? "10", 10), 100);
|
|
623
|
+
if (isNaN(id)) {
|
|
624
|
+
res.status(400).json({ error: "Invalid cron job id" });
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
try {
|
|
628
|
+
const rows = await sql `
|
|
629
|
+
SELECT id, cron_job_id, executed_at, success, error_message
|
|
630
|
+
FROM cron_history
|
|
631
|
+
WHERE cron_job_id = ${id}
|
|
632
|
+
ORDER BY executed_at DESC
|
|
633
|
+
LIMIT ${limit}
|
|
634
|
+
`;
|
|
635
|
+
res.json(rows.map((r) => ({
|
|
636
|
+
...r,
|
|
637
|
+
executed_at: r.executed_at.toISOString(),
|
|
638
|
+
})));
|
|
639
|
+
}
|
|
640
|
+
catch (err) {
|
|
641
|
+
console.error("GET /crons/:id/history error:", err);
|
|
642
|
+
res.status(500).json({ error: "Internal server error" });
|
|
643
|
+
}
|
|
644
|
+
});
|
|
368
645
|
return router;
|
|
369
646
|
}
|
|
370
647
|
export function createWorkerRouter(sql, opencode, serverUrl) {
|
|
@@ -385,11 +662,97 @@ export function createWorkerRouter(sql, opencode, serverUrl) {
|
|
|
385
662
|
return;
|
|
386
663
|
}
|
|
387
664
|
const session = rows[0];
|
|
665
|
+
const token = `${session.agent_code}@<server-url>`;
|
|
666
|
+
const name = session.name;
|
|
667
|
+
const humanConfig = await sql `SELECT value FROM config WHERE key = 'human_name'`;
|
|
668
|
+
const humanName = humanConfig[0]?.value ?? "your human manager";
|
|
669
|
+
const humanDescConfig = await sql `SELECT value FROM config WHERE key = 'human_description'`;
|
|
670
|
+
const humanDescription = humanDescConfig[0]?.value ?? "";
|
|
671
|
+
const message = [
|
|
672
|
+
`╔══════════════════════════════════════════════════════╗`,
|
|
673
|
+
`║ WELCOME TO THE AGENT OFFICE ║`,
|
|
674
|
+
`╚══════════════════════════════════════════════════════╝`,
|
|
675
|
+
``,
|
|
676
|
+
`You are now clocked in.`,
|
|
677
|
+
` Name: ${name}`,
|
|
678
|
+
...(session.mode ? [` Mode: ${session.mode}`] : []),
|
|
679
|
+
` Human manager: ${humanName} — the human who created your`,
|
|
680
|
+
` session, assigns your work, and is your`,
|
|
681
|
+
` primary point of contact for questions,`,
|
|
682
|
+
` updates, and decisions.`,
|
|
683
|
+
...(humanDescription ? [
|
|
684
|
+
` "${humanDescription}"`,
|
|
685
|
+
] : []),
|
|
686
|
+
``,
|
|
687
|
+
`The agent-office CLI is your PRIMARY means of communicating`,
|
|
688
|
+
`with your human manager (${humanName}) and your coworkers.`,
|
|
689
|
+
`Use it to send and receive messages, and to discover who`,
|
|
690
|
+
`else is working.`,
|
|
691
|
+
``,
|
|
692
|
+
`════════════════════════════════════════════════════════`,
|
|
693
|
+
` AVAILABLE COMMANDS`,
|
|
694
|
+
`════════════════════════════════════════════════════════`,
|
|
695
|
+
``,
|
|
696
|
+
` List your coworkers`,
|
|
697
|
+
` agent-office worker list-coworkers \\`,
|
|
698
|
+
` ${token}`,
|
|
699
|
+
``,
|
|
700
|
+
` Send a message to your manager or a coworker`,
|
|
701
|
+
` agent-office worker send-message \\`,
|
|
702
|
+
` --name <recipient-name> \\`,
|
|
703
|
+
` --body "Your message here" \\`,
|
|
704
|
+
` ${token}`,
|
|
705
|
+
``,
|
|
706
|
+
` Send a message to multiple recipients at once`,
|
|
707
|
+
` agent-office worker send-message \\`,
|
|
708
|
+
` --name alice --name bob \\`,
|
|
709
|
+
` --body "Your message here" \\`,
|
|
710
|
+
` ${token}`,
|
|
711
|
+
``,
|
|
712
|
+
` Manage scheduled tasks (optional)`,
|
|
713
|
+
` agent-office worker cron \\`,
|
|
714
|
+
` ${token}`,
|
|
715
|
+
``,
|
|
716
|
+
`════════════════════════════════════════════════════════`,
|
|
717
|
+
` ⚠ IMPORTANT: YOUR SESSIONS ARE PRIVATE`,
|
|
718
|
+
`════════════════════════════════════════════════════════`,
|
|
719
|
+
``,
|
|
720
|
+
` Nobody — not ${humanName}, not your coworkers — can see`,
|
|
721
|
+
` anything you think, reason, or write inside this session.`,
|
|
722
|
+
` Your work is completely private until you explicitly send`,
|
|
723
|
+
` a message using the send-message command.`,
|
|
724
|
+
``,
|
|
725
|
+
` This means:`,
|
|
726
|
+
` • You must send a message to report progress or ask`,
|
|
727
|
+
` for help. Silence looks like inaction.`,
|
|
728
|
+
` • Finishing a task without messaging ${humanName}`,
|
|
729
|
+
` means they will never know it is done.`,
|
|
730
|
+
` • If you are blocked or uncertain, send a message —`,
|
|
731
|
+
` nobody will know otherwise.`,
|
|
732
|
+
``,
|
|
733
|
+
`════════════════════════════════════════════════════════`,
|
|
734
|
+
` TIPS`,
|
|
735
|
+
`════════════════════════════════════════════════════════`,
|
|
736
|
+
``,
|
|
737
|
+
` - Run list-coworkers to discover who is available and`,
|
|
738
|
+
` what their names are before sending messages.`,
|
|
739
|
+
` - Messages you send are delivered directly into the`,
|
|
740
|
+
` recipient's active session — they will see them`,
|
|
741
|
+
` immediately.`,
|
|
742
|
+
` - Your human manager is ${humanName}. They can send you`,
|
|
743
|
+
` messages at any time and those will appear here in`,
|
|
744
|
+
` your session just like this one. You can reach them`,
|
|
745
|
+
` by sending a message to --name ${humanName}.`,
|
|
746
|
+
` - Optional: Set up recurring scheduled tasks with cron`,
|
|
747
|
+
` jobs. Run 'agent-office worker cron list ${token}' to`,
|
|
748
|
+
` get started.`,
|
|
749
|
+
``,
|
|
750
|
+
].join("\n");
|
|
388
751
|
res.json({
|
|
389
752
|
ok: true,
|
|
390
753
|
name: session.name,
|
|
391
754
|
session_id: session.session_id,
|
|
392
|
-
message
|
|
755
|
+
message,
|
|
393
756
|
});
|
|
394
757
|
});
|
|
395
758
|
router.get("/worker/list-coworkers", async (req, res) => {
|
|
@@ -474,27 +837,345 @@ export function createWorkerRouter(sql, opencode, serverUrl) {
|
|
|
474
837
|
`;
|
|
475
838
|
if (sessionMap.has(recipient)) {
|
|
476
839
|
const recipientSessionId = sessionMap.get(recipient);
|
|
477
|
-
|
|
478
|
-
|
|
840
|
+
const msgId = msgRow.id;
|
|
841
|
+
injected = true;
|
|
842
|
+
opencode.app.providers().then((providers) => {
|
|
479
843
|
const defaultEntry = Object.entries(providers.default)[0];
|
|
480
|
-
if (defaultEntry)
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
catch (err) {
|
|
844
|
+
if (!defaultEntry)
|
|
845
|
+
return;
|
|
846
|
+
const injectText = `[Message from "${session.name}"]: ${trimmedBody}${MAIL_INJECTION_BLURB}`;
|
|
847
|
+
return opencode.session.chat(recipientSessionId, {
|
|
848
|
+
modelID: defaultEntry[0],
|
|
849
|
+
providerID: defaultEntry[1],
|
|
850
|
+
parts: [{ type: "text", text: injectText }],
|
|
851
|
+
}).then(() => sql `UPDATE messages SET injected = TRUE WHERE id = ${msgId}`);
|
|
852
|
+
}).catch((err) => {
|
|
492
853
|
console.warn(`Warning: could not inject message into session ${recipient}:`, err);
|
|
493
|
-
}
|
|
854
|
+
});
|
|
494
855
|
}
|
|
495
856
|
results.push({ to: recipient, messageId: msgRow.id, injected });
|
|
496
857
|
}
|
|
497
858
|
res.status(201).json({ ok: true, results });
|
|
498
859
|
});
|
|
860
|
+
// ── Worker Cron Endpoints (authenticated via agent_code in query) ───────────
|
|
861
|
+
router.get("/worker/crons", async (req, res) => {
|
|
862
|
+
const { code } = req.query;
|
|
863
|
+
if (!code || typeof code !== "string") {
|
|
864
|
+
res.status(400).json({ error: "code query parameter is required" });
|
|
865
|
+
return;
|
|
866
|
+
}
|
|
867
|
+
const session = await sql `
|
|
868
|
+
SELECT name FROM sessions WHERE agent_code = ${code}
|
|
869
|
+
`;
|
|
870
|
+
if (session.length === 0) {
|
|
871
|
+
res.status(401).json({ error: "Invalid agent code" });
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
const sessionName = session[0].name;
|
|
875
|
+
try {
|
|
876
|
+
const rows = await sql `
|
|
877
|
+
SELECT id, name, session_name, schedule, timezone, message, enabled, created_at, last_run
|
|
878
|
+
FROM cron_jobs
|
|
879
|
+
WHERE session_name = ${sessionName}
|
|
880
|
+
ORDER BY name
|
|
881
|
+
`;
|
|
882
|
+
const jobs = rows.map((job) => {
|
|
883
|
+
let nextRun = null;
|
|
884
|
+
if (job.enabled) {
|
|
885
|
+
try {
|
|
886
|
+
const options = {};
|
|
887
|
+
if (job.timezone)
|
|
888
|
+
options.timezone = job.timezone;
|
|
889
|
+
const cronJob = new CronerInstance(job.schedule, options);
|
|
890
|
+
const next = cronJob.nextRun();
|
|
891
|
+
nextRun = next ? next.toISOString() : null;
|
|
892
|
+
}
|
|
893
|
+
catch { }
|
|
894
|
+
}
|
|
895
|
+
return {
|
|
896
|
+
...job,
|
|
897
|
+
next_run: nextRun,
|
|
898
|
+
last_run: job.last_run ? job.last_run.toISOString() : null,
|
|
899
|
+
created_at: job.created_at.toISOString(),
|
|
900
|
+
};
|
|
901
|
+
});
|
|
902
|
+
res.json(jobs);
|
|
903
|
+
}
|
|
904
|
+
catch (err) {
|
|
905
|
+
console.error("GET /worker/crons error:", err);
|
|
906
|
+
res.status(500).json({ error: "Internal server error" });
|
|
907
|
+
}
|
|
908
|
+
});
|
|
909
|
+
router.post("/worker/crons", async (req, res) => {
|
|
910
|
+
const { code } = req.query;
|
|
911
|
+
const { name, schedule, message, timezone } = req.body;
|
|
912
|
+
if (!code || typeof code !== "string") {
|
|
913
|
+
res.status(400).json({ error: "code query parameter is required" });
|
|
914
|
+
return;
|
|
915
|
+
}
|
|
916
|
+
const session = await sql `
|
|
917
|
+
SELECT name FROM sessions WHERE agent_code = ${code}
|
|
918
|
+
`;
|
|
919
|
+
if (session.length === 0) {
|
|
920
|
+
res.status(401).json({ error: "Invalid agent code" });
|
|
921
|
+
return;
|
|
922
|
+
}
|
|
923
|
+
if (!name || typeof name !== "string" || !name.trim()) {
|
|
924
|
+
res.status(400).json({ error: "name is required" });
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
if (!schedule || typeof schedule !== "string" || !schedule.trim()) {
|
|
928
|
+
res.status(400).json({ error: "schedule is required" });
|
|
929
|
+
return;
|
|
930
|
+
}
|
|
931
|
+
if (!message || typeof message !== "string" || !message.trim()) {
|
|
932
|
+
res.status(400).json({ error: "message is required" });
|
|
933
|
+
return;
|
|
934
|
+
}
|
|
935
|
+
const trimmedName = name.trim();
|
|
936
|
+
const trimmedSchedule = schedule.trim();
|
|
937
|
+
const trimmedMessage = message.trim();
|
|
938
|
+
const sessionName = session[0].name;
|
|
939
|
+
try {
|
|
940
|
+
new CronerInstance(trimmedSchedule);
|
|
941
|
+
}
|
|
942
|
+
catch {
|
|
943
|
+
res.status(400).json({ error: "Invalid cron schedule expression" });
|
|
944
|
+
return;
|
|
945
|
+
}
|
|
946
|
+
if (timezone) {
|
|
947
|
+
try {
|
|
948
|
+
new CronerInstance("0 0 * * *", { timezone });
|
|
949
|
+
}
|
|
950
|
+
catch {
|
|
951
|
+
res.status(400).json({ error: "Invalid timezone" });
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
const [existing] = await sql `
|
|
956
|
+
SELECT id FROM cron_jobs WHERE name = ${trimmedName} AND session_name = ${sessionName}
|
|
957
|
+
`;
|
|
958
|
+
if (existing) {
|
|
959
|
+
res.status(409).json({ error: `Cron job "${trimmedName}" already exists` });
|
|
960
|
+
return;
|
|
961
|
+
}
|
|
962
|
+
try {
|
|
963
|
+
const [inserted] = await sql `
|
|
964
|
+
INSERT INTO cron_jobs (name, session_name, schedule, timezone, message)
|
|
965
|
+
VALUES (${trimmedName}, ${sessionName}, ${trimmedSchedule}, ${timezone ?? null}, ${trimmedMessage})
|
|
966
|
+
RETURNING id, name, session_name, schedule, timezone, message, enabled, created_at, last_run
|
|
967
|
+
`;
|
|
968
|
+
const cronJobrow = inserted;
|
|
969
|
+
const nextRun = cronJobrow.enabled ? (() => {
|
|
970
|
+
try {
|
|
971
|
+
const options = {};
|
|
972
|
+
if (cronJobrow.timezone)
|
|
973
|
+
options.timezone = cronJobrow.timezone;
|
|
974
|
+
const cronJob = new CronerInstance(cronJobrow.schedule, options);
|
|
975
|
+
const next = cronJob.nextRun();
|
|
976
|
+
return next ? next.toISOString() : null;
|
|
977
|
+
}
|
|
978
|
+
catch {
|
|
979
|
+
return null;
|
|
980
|
+
}
|
|
981
|
+
})() : null;
|
|
982
|
+
res.status(201).json({
|
|
983
|
+
...cronJobrow,
|
|
984
|
+
next_run: nextRun,
|
|
985
|
+
last_run: cronJobrow?.last_run?.toISOString() ?? null,
|
|
986
|
+
created_at: cronJobrow.created_at.toISOString(),
|
|
987
|
+
});
|
|
988
|
+
}
|
|
989
|
+
catch (err) {
|
|
990
|
+
console.error("POST /worker/crons error:", err);
|
|
991
|
+
res.status(500).json({ error: "Internal server error" });
|
|
992
|
+
}
|
|
993
|
+
});
|
|
994
|
+
router.delete("/worker/crons/:id", async (req, res) => {
|
|
995
|
+
const { code } = req.query;
|
|
996
|
+
const id = parseInt(req.params.id, 10);
|
|
997
|
+
if (!code || typeof code !== "string") {
|
|
998
|
+
res.status(400).json({ error: "code query parameter is required" });
|
|
999
|
+
return;
|
|
1000
|
+
}
|
|
1001
|
+
if (isNaN(id)) {
|
|
1002
|
+
res.status(400).json({ error: "Invalid cron job id" });
|
|
1003
|
+
return;
|
|
1004
|
+
}
|
|
1005
|
+
const session = await sql `
|
|
1006
|
+
SELECT name FROM sessions WHERE agent_code = ${code}
|
|
1007
|
+
`;
|
|
1008
|
+
if (session.length === 0) {
|
|
1009
|
+
res.status(401).json({ error: "Invalid agent code" });
|
|
1010
|
+
return;
|
|
1011
|
+
}
|
|
1012
|
+
const sessionName = session[0].name;
|
|
1013
|
+
try {
|
|
1014
|
+
const [job] = await sql `
|
|
1015
|
+
SELECT id, name FROM cron_jobs WHERE id = ${id} AND session_name = ${sessionName}
|
|
1016
|
+
`;
|
|
1017
|
+
if (!job) {
|
|
1018
|
+
res.status(404).json({ error: "Cron job not found or not owned by you" });
|
|
1019
|
+
return;
|
|
1020
|
+
}
|
|
1021
|
+
await sql `DELETE FROM cron_jobs WHERE id = ${id}`;
|
|
1022
|
+
res.json({ deleted: true, id, name: job.name });
|
|
1023
|
+
}
|
|
1024
|
+
catch (err) {
|
|
1025
|
+
console.error("DELETE /worker/crons/:id error:", err);
|
|
1026
|
+
res.status(500).json({ error: "Internal server error" });
|
|
1027
|
+
}
|
|
1028
|
+
});
|
|
1029
|
+
router.post("/worker/crons/:id/enable", async (req, res) => {
|
|
1030
|
+
const { code } = req.query;
|
|
1031
|
+
const id = parseInt(req.params.id, 10);
|
|
1032
|
+
if (!code || typeof code !== "string") {
|
|
1033
|
+
res.status(400).json({ error: "code query parameter is required" });
|
|
1034
|
+
return;
|
|
1035
|
+
}
|
|
1036
|
+
if (isNaN(id)) {
|
|
1037
|
+
res.status(400).json({ error: "Invalid cron job id" });
|
|
1038
|
+
return;
|
|
1039
|
+
}
|
|
1040
|
+
const session = await sql `
|
|
1041
|
+
SELECT name FROM sessions WHERE agent_code = ${code}
|
|
1042
|
+
`;
|
|
1043
|
+
if (session.length === 0) {
|
|
1044
|
+
res.status(401).json({ error: "Invalid agent code" });
|
|
1045
|
+
return;
|
|
1046
|
+
}
|
|
1047
|
+
const sessionName = session[0].name;
|
|
1048
|
+
try {
|
|
1049
|
+
const [existing] = await sql `
|
|
1050
|
+
SELECT id FROM cron_jobs WHERE id = ${id} AND session_name = ${sessionName}
|
|
1051
|
+
`;
|
|
1052
|
+
if (!existing) {
|
|
1053
|
+
res.status(404).json({ error: "Cron job not found or not owned by you" });
|
|
1054
|
+
return;
|
|
1055
|
+
}
|
|
1056
|
+
await sql `UPDATE cron_jobs SET enabled = TRUE WHERE id = ${id}`;
|
|
1057
|
+
const [updated] = await sql `
|
|
1058
|
+
SELECT id, name, session_name, schedule, timezone, message, enabled, created_at, last_run
|
|
1059
|
+
FROM cron_jobs WHERE id = ${id}
|
|
1060
|
+
`;
|
|
1061
|
+
if (updated) {
|
|
1062
|
+
const nextRun = (() => {
|
|
1063
|
+
try {
|
|
1064
|
+
const options = {};
|
|
1065
|
+
if (updated.timezone)
|
|
1066
|
+
options.timezone = updated.timezone;
|
|
1067
|
+
const cronJob = new CronerInstance(updated.schedule, options);
|
|
1068
|
+
const next = cronJob.nextRun();
|
|
1069
|
+
return next ? next.toISOString() : null;
|
|
1070
|
+
}
|
|
1071
|
+
catch {
|
|
1072
|
+
return null;
|
|
1073
|
+
}
|
|
1074
|
+
})();
|
|
1075
|
+
res.json({
|
|
1076
|
+
...updated,
|
|
1077
|
+
next_run: nextRun,
|
|
1078
|
+
last_run: updated.last_run?.toISOString() ?? null,
|
|
1079
|
+
created_at: updated.created_at.toISOString(),
|
|
1080
|
+
});
|
|
1081
|
+
}
|
|
1082
|
+
else {
|
|
1083
|
+
res.status(404).json({ error: "Cron job not found" });
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
catch (err) {
|
|
1087
|
+
console.error("POST /worker/crons/:id/enable error:", err);
|
|
1088
|
+
res.status(500).json({ error: "Internal server error" });
|
|
1089
|
+
}
|
|
1090
|
+
});
|
|
1091
|
+
router.post("/worker/crons/:id/disable", async (req, res) => {
|
|
1092
|
+
const { code } = req.query;
|
|
1093
|
+
const id = parseInt(req.params.id, 10);
|
|
1094
|
+
if (!code || typeof code !== "string") {
|
|
1095
|
+
res.status(400).json({ error: "code query parameter is required" });
|
|
1096
|
+
return;
|
|
1097
|
+
}
|
|
1098
|
+
if (isNaN(id)) {
|
|
1099
|
+
res.status(400).json({ error: "Invalid cron job id" });
|
|
1100
|
+
return;
|
|
1101
|
+
}
|
|
1102
|
+
const session = await sql `
|
|
1103
|
+
SELECT name FROM sessions WHERE agent_code = ${code}
|
|
1104
|
+
`;
|
|
1105
|
+
if (session.length === 0) {
|
|
1106
|
+
res.status(401).json({ error: "Invalid agent code" });
|
|
1107
|
+
return;
|
|
1108
|
+
}
|
|
1109
|
+
const sessionName = session[0].name;
|
|
1110
|
+
try {
|
|
1111
|
+
const [job] = await sql `
|
|
1112
|
+
SELECT id FROM cron_jobs WHERE id = ${id} AND session_name = ${sessionName}
|
|
1113
|
+
`;
|
|
1114
|
+
if (!job) {
|
|
1115
|
+
res.status(404).json({ error: "Cron job not found or not owned by you" });
|
|
1116
|
+
return;
|
|
1117
|
+
}
|
|
1118
|
+
await sql `UPDATE cron_jobs SET enabled = FALSE WHERE id = ${id}`;
|
|
1119
|
+
const [updated] = await sql `
|
|
1120
|
+
SELECT id, name, session_name, schedule, timezone, message, enabled, created_at, last_run
|
|
1121
|
+
FROM cron_jobs WHERE id = ${id}
|
|
1122
|
+
`;
|
|
1123
|
+
res.json({
|
|
1124
|
+
...updated,
|
|
1125
|
+
next_run: null,
|
|
1126
|
+
last_run: updated?.last_run?.toISOString() ?? null,
|
|
1127
|
+
created_at: updated?.created_at.toISOString() ?? null,
|
|
1128
|
+
});
|
|
1129
|
+
}
|
|
1130
|
+
catch (err) {
|
|
1131
|
+
console.error("POST /worker/crons/:id/disable error:", err);
|
|
1132
|
+
res.status(500).json({ error: "Internal server error" });
|
|
1133
|
+
}
|
|
1134
|
+
});
|
|
1135
|
+
router.get("/worker/crons/:id/history", async (req, res) => {
|
|
1136
|
+
const { code } = req.query;
|
|
1137
|
+
const id = parseInt(req.params.id, 10);
|
|
1138
|
+
const limit = Math.min(parseInt(req.query.limit ?? "10", 10), 100);
|
|
1139
|
+
if (!code || typeof code !== "string") {
|
|
1140
|
+
res.status(400).json({ error: "code query parameter is required" });
|
|
1141
|
+
return;
|
|
1142
|
+
}
|
|
1143
|
+
if (isNaN(id)) {
|
|
1144
|
+
res.status(400).json({ error: "Invalid cron job id" });
|
|
1145
|
+
return;
|
|
1146
|
+
}
|
|
1147
|
+
const session = await sql `
|
|
1148
|
+
SELECT name FROM sessions WHERE agent_code = ${code}
|
|
1149
|
+
`;
|
|
1150
|
+
if (session.length === 0) {
|
|
1151
|
+
res.status(401).json({ error: "Invalid agent code" });
|
|
1152
|
+
return;
|
|
1153
|
+
}
|
|
1154
|
+
const sessionName = session[0].name;
|
|
1155
|
+
try {
|
|
1156
|
+
const [job] = await sql `
|
|
1157
|
+
SELECT id FROM cron_jobs WHERE id = ${id} AND session_name = ${sessionName}
|
|
1158
|
+
`;
|
|
1159
|
+
if (!job) {
|
|
1160
|
+
res.status(404).json({ error: "Cron job not found or not owned by you" });
|
|
1161
|
+
return;
|
|
1162
|
+
}
|
|
1163
|
+
const rows = await sql `
|
|
1164
|
+
SELECT id, cron_job_id, executed_at, success, error_message
|
|
1165
|
+
FROM cron_history
|
|
1166
|
+
WHERE cron_job_id = ${id}
|
|
1167
|
+
ORDER BY executed_at DESC
|
|
1168
|
+
LIMIT ${limit}
|
|
1169
|
+
`;
|
|
1170
|
+
res.json(rows.map((r) => ({
|
|
1171
|
+
...r,
|
|
1172
|
+
executed_at: r.executed_at.toISOString(),
|
|
1173
|
+
})));
|
|
1174
|
+
}
|
|
1175
|
+
catch (err) {
|
|
1176
|
+
console.error("GET /worker/crons/:id/history error:", err);
|
|
1177
|
+
res.status(500).json({ error: "Internal server error" });
|
|
1178
|
+
}
|
|
1179
|
+
});
|
|
499
1180
|
return router;
|
|
500
1181
|
}
|