@kadaliao/dsh-bots 0.6.1 → 0.7.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 +85 -4
- package/README.zh-CN.md +72 -4
- package/client.js +1319 -37
- package/index.js +878 -18
- package/lib/channels.mjs +32 -0
- package/lib/memory.mjs +508 -0
- package/lib/models.mjs +250 -0
- package/lib/prices.mjs +149 -0
- package/lib/settings.mjs +142 -0
- package/lib/store.mjs +4 -1
- package/lib/topics.mjs +103 -0
- package/lib/usage.mjs +267 -0
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +3 -3
package/index.js
CHANGED
|
@@ -36,6 +36,12 @@ import { createUnfurler } from './lib/unfurl.mjs';
|
|
|
36
36
|
import { isLogoName, withLogo } from './lib/logos.mjs';
|
|
37
37
|
import { CHANNEL_LABELS, channelEntry, channelIssue, createChannels, maskBot, unmaskApps } from './lib/channels.mjs';
|
|
38
38
|
import { describeTiming, dmBody, dmHeader, personaText, textOf, timingFrom, timingOfEntry } from './lib/prompt.mjs';
|
|
39
|
+
import { createSettings } from './lib/settings.mjs';
|
|
40
|
+
import { createPriceCatalog, priceOf } from './lib/prices.mjs';
|
|
41
|
+
import { createLedger, promptTokens, report, todayByBot, usageRow } from './lib/usage.mjs';
|
|
42
|
+
import { createMemory, dreamBotPrompt, dreamTeamPrompt, KINDS as MEMORY_KINDS, looksSecret, parseJsonReply, reflectionPrompt } from './lib/memory.mjs';
|
|
43
|
+
import { createTopics, rotationReason } from './lib/topics.mjs';
|
|
44
|
+
import { API_KINDS, CODEX_ROUTE, PI_SCOPE, ROUTE_PREFIX, codexAuthFile, createCodexSync, detectLogins, discoverModels, endpointView, keyRefFor, profileFor, readCodexAuth, codexCredential } from './lib/models.mjs';
|
|
39
45
|
|
|
40
46
|
/**
|
|
41
47
|
* Import one Harness package: by bare name when this directory resolves it,
|
|
@@ -90,7 +96,19 @@ const AVATAR_ATTEMPTS = 3;
|
|
|
90
96
|
/** Errors from a gateway or the network rather than from the request itself. */
|
|
91
97
|
export const TRANSIENT = /\b(408|409|429|500|502|503|504|520|522|524)\b|connection (lost|reset|closed)|ECONNRESET|ETIMEDOUT|EPIPE|socket hang up|fetch failed|could not reach|did not answer within|overloaded|temporarily/i;
|
|
92
98
|
|
|
93
|
-
export const TOOL_NAMES = ['bot_list', 'bot_create', 'bot_update', 'bot_delete', 'bot_send_message', 'bot_schedule'];
|
|
99
|
+
export const TOOL_NAMES = ['bot_list', 'bot_create', 'bot_update', 'bot_delete', 'bot_send_message', 'bot_schedule', 'memory_save', 'memory_forget', 'memory_search'];
|
|
100
|
+
const MEMORY_SECTION = 'bots:memory';
|
|
101
|
+
const MEMORY_ORDER = 60;
|
|
102
|
+
/** How often idle conversations are checked for a new topic, and the daily consolidation for being due. */
|
|
103
|
+
const SWEEP_MS = 5 * 60_000;
|
|
104
|
+
/** One transcript line a task keeps for its summary, and the whole transcript. */
|
|
105
|
+
const LOG_LINE = 1500;
|
|
106
|
+
const LOG_MAX = 40_000;
|
|
107
|
+
const DAY_MS = 86_400_000;
|
|
108
|
+
/** How long a new topic waits for the last task's memory summary. */
|
|
109
|
+
const REFLECT_WAIT_MS = 20_000;
|
|
110
|
+
/** The local zone's offset east of UTC, in minutes. */
|
|
111
|
+
const tzMinutes = () => -new Date().getTimezoneOffset();
|
|
94
112
|
|
|
95
113
|
function homeDir() {
|
|
96
114
|
return process.env.DSH_HOME && process.env.DSH_HOME !== '' ? process.env.DSH_HOME : join(homedir(), '.dsh');
|
|
@@ -178,6 +196,12 @@ export function apply(ctx, config, deps = {}) {
|
|
|
178
196
|
const cfg = config && typeof config === 'object' ? config : {};
|
|
179
197
|
const dir = join(homeDir(), 'cache', 'bots');
|
|
180
198
|
const store = createStore(dir);
|
|
199
|
+
const settings = createSettings(dir);
|
|
200
|
+
const ledger = createLedger(dir);
|
|
201
|
+
const memory = createMemory(join(dir, 'memory'));
|
|
202
|
+
const topics = createTopics(dir);
|
|
203
|
+
const prices = createPriceCatalog(dir, { enabled: deps.prices !== false, ...(deps.fetch ? { fetch: deps.fetch } : {}) });
|
|
204
|
+
const home = deps.home ?? homedir();
|
|
181
205
|
const workdir = typeof cfg.workdir === 'string' && cfg.workdir.trim() ? cfg.workdir.trim() : join(homeDir(), 'bots', 'workspace');
|
|
182
206
|
const zone = (() => {
|
|
183
207
|
try {
|
|
@@ -311,6 +335,12 @@ export function apply(ctx, config, deps = {}) {
|
|
|
311
335
|
} catch (error) {
|
|
312
336
|
ctx.logger?.warn?.(`bots: persona for @${bot.handle}: ${error?.message ?? error}`);
|
|
313
337
|
}
|
|
338
|
+
try {
|
|
339
|
+
// Memory is read once per task, not per step: a note saved mid-task must not break the prompt cache.
|
|
340
|
+
disposers.push(prompts.section({ name: MEMORY_SECTION, order: MEMORY_ORDER, interpolate: false, text: () => memoryTextFor(agent.session.id) }));
|
|
341
|
+
} catch (error) {
|
|
342
|
+
ctx.logger?.warn?.(`bots: memory for @${bot.handle}: ${error?.message ?? error}`);
|
|
343
|
+
}
|
|
314
344
|
}
|
|
315
345
|
if (bot.tools.deny.length > 0) {
|
|
316
346
|
const tools = service(actx, 'tools');
|
|
@@ -389,6 +419,13 @@ export function apply(ctx, config, deps = {}) {
|
|
|
389
419
|
const found = await controller.resolveAgent(bot.sessionId).catch((error) => ({ error }));
|
|
390
420
|
if (found && !('error' in found)) return bot.sessionId;
|
|
391
421
|
}
|
|
422
|
+
return createSession(bot);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** A fresh home session for the bot (its next topic). Returns the session id. */
|
|
426
|
+
async function createSession(bot) {
|
|
427
|
+
const controller = service(ctx, 'sessionController');
|
|
428
|
+
if (!controller) throw new Error('bot conversations need the Web profile (no session controller here)');
|
|
392
429
|
const sessionId = `session-${randomUUID()}`;
|
|
393
430
|
// Mapped before the Agent exists, so its creation already finds the bot.
|
|
394
431
|
store.patch(bot.id, { sessionId });
|
|
@@ -409,9 +446,10 @@ export function apply(ctx, config, deps = {}) {
|
|
|
409
446
|
} catch {
|
|
410
447
|
// A title is cosmetic.
|
|
411
448
|
}
|
|
412
|
-
|
|
449
|
+
const fresh = store.get(bot.id) ?? bot;
|
|
450
|
+
if (fresh.model) {
|
|
413
451
|
try {
|
|
414
|
-
agent.model?.select?.(
|
|
452
|
+
agent.model?.select?.(selectionOf(fresh.model));
|
|
415
453
|
} catch {
|
|
416
454
|
// The default model stays.
|
|
417
455
|
}
|
|
@@ -435,6 +473,377 @@ export function apply(ctx, config, deps = {}) {
|
|
|
435
473
|
}
|
|
436
474
|
}
|
|
437
475
|
|
|
476
|
+
// --- models ----------------------------------------------------------------------------
|
|
477
|
+
const selectionOf = (m) => ({ provider: m.provider, model: m.model, ...(m.reasoningEffort ? { reasoningEffort: m.reasoningEffort } : {}) });
|
|
478
|
+
/** The model a bot runs on now: its own choice, its conversation's, else the Harness default. */
|
|
479
|
+
function modelOfBot(bot) {
|
|
480
|
+
if (bot?.model) return selectionOf(bot.model);
|
|
481
|
+
const agent = bot?.sessionId ? agents()?.get?.(bot.sessionId) : undefined;
|
|
482
|
+
try {
|
|
483
|
+
const cur = agent?.model?.current?.();
|
|
484
|
+
if (cur?.provider && cur?.model) return { provider: cur.provider, model: cur.model };
|
|
485
|
+
} catch {
|
|
486
|
+
// Not resolvable.
|
|
487
|
+
}
|
|
488
|
+
try {
|
|
489
|
+
const def = service(ctx, 'agentDefaultModel')?.currentSelection?.();
|
|
490
|
+
if (def?.provider && def?.model) return { provider: def.provider, model: def.model };
|
|
491
|
+
} catch {
|
|
492
|
+
// No default either.
|
|
493
|
+
}
|
|
494
|
+
return null;
|
|
495
|
+
}
|
|
496
|
+
/** The model memory work for `bot` runs on: the setting, else the bot's own. */
|
|
497
|
+
const memoryModelOf = (bot) => settings.get().memory.model ?? modelOfBot(bot);
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* One model call outside any conversation (memory summaries): the text of the
|
|
501
|
+
* answer and its token usage. `deps.complete` stands in for tests.
|
|
502
|
+
*/
|
|
503
|
+
async function complete(sel, system, prompt, { purpose = 'bots-memory', sessionId, maxTokens = 2000, timeoutMs = 120_000 } = {}) {
|
|
504
|
+
if (deps.complete) return deps.complete({ ...sel, system, prompt, purpose, maxTokens });
|
|
505
|
+
const llm = service(ctx, 'llm');
|
|
506
|
+
if (!llm?.stream) throw new Error('no model service in this Harness');
|
|
507
|
+
const signal = AbortSignal.timeout(timeoutMs);
|
|
508
|
+
const parts = new Map();
|
|
509
|
+
let usage;
|
|
510
|
+
let failure;
|
|
511
|
+
for await (const c of llm.stream({ provider: sel.provider, model: sel.model, system, messages: [{ role: 'user', content: [{ type: 'text', text: prompt }] }], maxTokens, purpose, signal, ...(sessionId ? { sessionId } : {}) })) {
|
|
512
|
+
if (c?.type === 'text-delta') parts.set(c.index, (parts.get(c.index) ?? '') + c.text);
|
|
513
|
+
else if (c?.type === 'usage') usage = c.usage;
|
|
514
|
+
else if (c?.type === 'finish' && (c.reason?.kind === 'error' || c.reason?.kind === 'aborted')) failure = c.reason.failure?.message ?? c.reason.kind;
|
|
515
|
+
}
|
|
516
|
+
if (failure) throw new Error(String(failure));
|
|
517
|
+
return { text: [...parts.entries()].sort((a, b) => a[0] - b[0]).map(([, t]) => t).join(''), usage };
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// --- tasks and usage ------------------------------------------------------------------------
|
|
521
|
+
/** sessionId → the task the bot is on now. */
|
|
522
|
+
const openTasks = new Map();
|
|
523
|
+
/** schedule id → title, as last listed. */
|
|
524
|
+
const scheduleTitles = new Map();
|
|
525
|
+
/**
|
|
526
|
+
* sessionId → the memory section text. It holds for the whole topic, so the system prompt (and the
|
|
527
|
+
* provider's prompt cache over the conversation) stays the same from task to task; what the bot learns
|
|
528
|
+
* within a topic is in that conversation already. It is read again at the next task after the user
|
|
529
|
+
* edits memory in the panel or the daily consolidation ran, and at every new topic.
|
|
530
|
+
*/
|
|
531
|
+
const memoText = new Map();
|
|
532
|
+
const memoStale = new Set();
|
|
533
|
+
/**
|
|
534
|
+
* Memory changed outside the conversations (a panel edit, the nightly consolidation). An idle bot
|
|
535
|
+
* reads it at its next task; a working one once its task is over. The Harness assembles a step's
|
|
536
|
+
* prompt before it reports the step's message, so this never waits for a task to start.
|
|
537
|
+
*/
|
|
538
|
+
const staleMemory = (only) => {
|
|
539
|
+
for (const id of only ? [only].filter((x) => memoText.has(x)) : [...memoText.keys()]) {
|
|
540
|
+
if (openTasks.has(id) || agents()?.get?.(id)?.status === 'running') memoStale.add(id);
|
|
541
|
+
else memoText.delete(id);
|
|
542
|
+
}
|
|
543
|
+
};
|
|
544
|
+
function memoryTextFor(sessionId) {
|
|
545
|
+
let text = memoText.get(sessionId);
|
|
546
|
+
if (text === undefined) {
|
|
547
|
+
const bot = store.bySession(sessionId);
|
|
548
|
+
try {
|
|
549
|
+
text = bot ? memory.promptFor(bot, { tz: tzMinutes() }) : '';
|
|
550
|
+
} catch {
|
|
551
|
+
text = '';
|
|
552
|
+
}
|
|
553
|
+
memoText.set(sessionId, text);
|
|
554
|
+
}
|
|
555
|
+
return text;
|
|
556
|
+
}
|
|
557
|
+
const firstLine = (s, max = 100) => {
|
|
558
|
+
const line = String(s ?? '').split('\n').map((x) => x.trim()).find(Boolean) ?? '';
|
|
559
|
+
return line.length > max ? `${line.slice(0, max - 1)}…` : line;
|
|
560
|
+
};
|
|
561
|
+
/** What a task is about, from the message that started it. */
|
|
562
|
+
function taskTitle(kind, content, said) {
|
|
563
|
+
if (kind === 'agent-message') return firstLine(Array.isArray(content) && content[1]?.text ? content[1].text : said);
|
|
564
|
+
if (kind === 'schedule') {
|
|
565
|
+
const id = /schedule_id_json:\s*"([^"]+)"/.exec(said)?.[1] ?? /"schedule_id":\s*"([^"]+)"/.exec(said)?.[1];
|
|
566
|
+
if (id && scheduleTitles.has(id)) return scheduleTitles.get(id);
|
|
567
|
+
try {
|
|
568
|
+
const p = /reminder_prompt_json:\s*(".*")\s*$/m.exec(said)?.[1];
|
|
569
|
+
if (p) return firstLine(JSON.parse(p));
|
|
570
|
+
} catch {
|
|
571
|
+
// Fall through.
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
return firstLine(said);
|
|
575
|
+
}
|
|
576
|
+
function startTask(bot, sessionId, kind, event, via) {
|
|
577
|
+
const content = event.data?.content;
|
|
578
|
+
const said = textOf(content);
|
|
579
|
+
const sched = kind === 'schedule' ? (/schedule_id_json:\s*"([^"]+)"/.exec(said)?.[1] ?? /"schedule_id":\s*"([^"]+)"/.exec(said)?.[1] ?? '') : '';
|
|
580
|
+
const from = kind === 'agent-message' ? (store.bySession(event.data?.source?.senderSessionId)?.id ?? 'user') : '';
|
|
581
|
+
const task = {
|
|
582
|
+
id: `task-${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`,
|
|
583
|
+
b: bot.id,
|
|
584
|
+
s: sessionId,
|
|
585
|
+
trigger: kind === 'schedule' ? 'schedule' : kind === 'agent-message' ? 'dm' : via ? 'chat' : 'user',
|
|
586
|
+
...(via ? { via: via.kind } : {}),
|
|
587
|
+
...(from ? { from } : {}),
|
|
588
|
+
...(sched ? { sched } : {}),
|
|
589
|
+
title: taskTitle(kind, content, said) || '…',
|
|
590
|
+
start: Number(event.time) || Date.now(),
|
|
591
|
+
status: 'running',
|
|
592
|
+
log: [],
|
|
593
|
+
size: 0,
|
|
594
|
+
replied: false,
|
|
595
|
+
};
|
|
596
|
+
openTasks.set(sessionId, task);
|
|
597
|
+
topics.current(bot.id, sessionId);
|
|
598
|
+
return task;
|
|
599
|
+
}
|
|
600
|
+
function logLine(task, line) {
|
|
601
|
+
if (!task || !line) return;
|
|
602
|
+
const s = line.length > LOG_LINE ? `${line.slice(0, LOG_LINE)}…` : line;
|
|
603
|
+
task.log.push(s);
|
|
604
|
+
task.size += s.length;
|
|
605
|
+
// Keep the start and the latest; the middle of a long task matters least.
|
|
606
|
+
while (task.size > LOG_MAX && task.log.length > 4) task.size -= task.log.splice(2, 1)[0].length;
|
|
607
|
+
}
|
|
608
|
+
function recordUsage(bot, sessionId, { at, kind, provider, model, usage }) {
|
|
609
|
+
if (!usage) return;
|
|
610
|
+
const task = openTasks.get(sessionId);
|
|
611
|
+
ledger.record(usageRow({ at, botId: bot.id, sessionId, taskId: kind === 'chat' || kind === 'compaction' ? task?.id : undefined, kind, provider, model, usage }));
|
|
612
|
+
if (kind === 'chat') topics.noteCall(bot.id, sessionId, promptTokens(usage));
|
|
613
|
+
}
|
|
614
|
+
/** The task of `sessionId` is over when its Agent is idle again. */
|
|
615
|
+
function settleSoon(sessionId, delay = 1200) {
|
|
616
|
+
const t = setTimeout(() => {
|
|
617
|
+
const task = openTasks.get(sessionId);
|
|
618
|
+
if (!task) return;
|
|
619
|
+
const agent = agents()?.get?.(sessionId);
|
|
620
|
+
if (agent && agent.status === 'running') return;
|
|
621
|
+
finishTask(task);
|
|
622
|
+
}, delay);
|
|
623
|
+
t.unref?.();
|
|
624
|
+
}
|
|
625
|
+
function finishTask(task, status) {
|
|
626
|
+
if (openTasks.get(task.s) !== task) return;
|
|
627
|
+
openTasks.delete(task.s);
|
|
628
|
+
if (memoStale.delete(task.s)) memoText.delete(task.s);
|
|
629
|
+
const bot = store.get(task.b);
|
|
630
|
+
const { log, size, replied, ...row } = task;
|
|
631
|
+
row.end = Date.now();
|
|
632
|
+
row.status = status ?? (task.status === 'running' ? 'done' : task.status);
|
|
633
|
+
ledger.finishTask(row);
|
|
634
|
+
topics.noteTask(task.b, task.s, task.title);
|
|
635
|
+
if (bot) queueReflection(bot, { ...row, log });
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
// --- memory ----------------------------------------------------------------------------------
|
|
639
|
+
const reflecting = new Map();
|
|
640
|
+
/** Summarize a finished task into the journal and the bot's notes, one bot at a time. */
|
|
641
|
+
function queueReflection(bot, task) {
|
|
642
|
+
const prev = reflecting.get(bot.id) ?? Promise.resolve();
|
|
643
|
+
const next = prev
|
|
644
|
+
.then(() => reflect(bot, task))
|
|
645
|
+
.catch((error) => ctx.logger?.warn?.(`bots: memory for @${bot.handle}: ${error?.message ?? error}`));
|
|
646
|
+
reflecting.set(bot.id, next);
|
|
647
|
+
void next.finally(() => {
|
|
648
|
+
if (reflecting.get(bot.id) === next) reflecting.delete(bot.id);
|
|
649
|
+
});
|
|
650
|
+
return next;
|
|
651
|
+
}
|
|
652
|
+
const triggerWords = (task) =>
|
|
653
|
+
task.trigger === 'schedule' ? 'a scheduled task' : task.trigger === 'dm' ? `a message from ${task.from === 'user' ? 'the user' : `@${store.get(task.from)?.handle ?? 'another bot'}`}` : task.trigger === 'chat' ? `a ${task.via ?? 'chat'} message from the user` : 'a message from the user';
|
|
654
|
+
async function reflect(bot, task) {
|
|
655
|
+
// A bot deleted while its summary waited keeps nothing.
|
|
656
|
+
if (!store.get(bot.id)) return;
|
|
657
|
+
const transcript = task.log.join('\n');
|
|
658
|
+
const worth = task.log.some((l) => l.startsWith('[assistant]') || l.startsWith('[tool]'));
|
|
659
|
+
// A short exchange (an acknowledgement, a one-line answer to a teammate) gets a journal line, not a model call.
|
|
660
|
+
const big = transcript.length >= (task.trigger === 'dm' && task.from !== 'user' ? 1200 : 300);
|
|
661
|
+
const sel = settings.get().memory.auto && worth && big ? memoryModelOf(bot) : null;
|
|
662
|
+
let summary = '';
|
|
663
|
+
if (sel) {
|
|
664
|
+
try {
|
|
665
|
+
const p = reflectionPrompt({ bot, notes: memory.notes(bot.id), team: memory.notes('team'), transcript, trigger: triggerWords(task) });
|
|
666
|
+
const r = await complete(sel, p.system, p.user, { purpose: 'bots-memory', sessionId: task.s });
|
|
667
|
+
ledger.record(usageRow({ botId: bot.id, sessionId: task.s, taskId: task.id, kind: 'memory', provider: sel.provider, model: sel.model, usage: r.usage }));
|
|
668
|
+
const parsed = parseJsonReply(r.text) ?? {};
|
|
669
|
+
summary = String(parsed.summary ?? '').trim();
|
|
670
|
+
if (Array.isArray(parsed.notes) && parsed.notes.length) memory.edit(bot.id, parsed.notes, { source: 'auto' });
|
|
671
|
+
if (Array.isArray(parsed.team) && parsed.team.length) memory.edit('team', parsed.team, { source: 'auto' });
|
|
672
|
+
} catch (error) {
|
|
673
|
+
ctx.logger?.warn?.(`bots: summarizing a task of @${bot.handle} failed: ${error?.message ?? error}`);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
if (worth || summary) memory.addJournal({ bot: bot.id, task: task.id, trigger: task.trigger, title: task.title, summary: summary || task.title });
|
|
677
|
+
// The bot started a new topic before this summary was written: that topic reads its memory again.
|
|
678
|
+
const now = store.get(bot.id)?.sessionId;
|
|
679
|
+
if (now && now !== task.s) staleMemory(now);
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
let dreaming = null;
|
|
683
|
+
/** Consolidate every bot's notes and write the team digest (the daily "dream"). */
|
|
684
|
+
function dream({ reason = 'schedule' } = {}) {
|
|
685
|
+
if (dreaming) return dreaming;
|
|
686
|
+
dreaming = (async () => {
|
|
687
|
+
const now = Date.now();
|
|
688
|
+
const tz = tzMinutes();
|
|
689
|
+
const before = memory.dreamState();
|
|
690
|
+
const since = Number(before.lastAt) || now - DAY_MS;
|
|
691
|
+
const bots = store.list();
|
|
692
|
+
const result = { at: now, reason, bots: [], digest: null, errors: [] };
|
|
693
|
+
for (const bot of bots) {
|
|
694
|
+
const journal = memory.journal({ bot: bot.id, since: now - 7 * DAY_MS, limit: 300 });
|
|
695
|
+
const fresh = journal.some((j) => j.at >= since);
|
|
696
|
+
const notes = memory.notes(bot.id);
|
|
697
|
+
if (!fresh && notes.length < 40) continue;
|
|
698
|
+
const sel = memoryModelOf(bot);
|
|
699
|
+
if (!sel) continue;
|
|
700
|
+
try {
|
|
701
|
+
const p = dreamBotPrompt({ bot, notes, journal, tz });
|
|
702
|
+
const r = await complete(sel, p.system, p.user, { purpose: 'bots-dream', maxTokens: 4000, timeoutMs: 180_000 });
|
|
703
|
+
ledger.record(usageRow({ botId: bot.id, kind: 'dream', provider: sel.provider, model: sel.model, usage: r.usage }));
|
|
704
|
+
const parsed = parseJsonReply(r.text);
|
|
705
|
+
if (!parsed) throw new Error('the model did not answer with JSON');
|
|
706
|
+
result.bots.push({ bot: bot.id, ...memory.edit(bot.id, Array.isArray(parsed.notes) ? parsed.notes : [], { source: 'dream' }) });
|
|
707
|
+
} catch (error) {
|
|
708
|
+
result.errors.push({ bot: bot.id, reason: error?.message ?? String(error) });
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
const journal = memory.journal({ since, limit: 2000 });
|
|
712
|
+
if (journal.length > 0) {
|
|
713
|
+
const lead = store.primary() ?? bots[0];
|
|
714
|
+
const sel = lead ? memoryModelOf(lead) : settings.get().memory.model;
|
|
715
|
+
const date = memory.dayOf(journal[journal.length - 1].at, tz);
|
|
716
|
+
if (sel) {
|
|
717
|
+
try {
|
|
718
|
+
const p = dreamTeamPrompt({ bots, team: memory.notes('team'), journal, date, tz });
|
|
719
|
+
const r = await complete(sel, p.system, p.user, { purpose: 'bots-dream', maxTokens: 4000, timeoutMs: 180_000 });
|
|
720
|
+
ledger.record(usageRow({ botId: lead?.id ?? 'team', kind: 'dream', provider: sel.provider, model: sel.model, usage: r.usage }));
|
|
721
|
+
const parsed = parseJsonReply(r.text);
|
|
722
|
+
if (!parsed) throw new Error('the model did not answer with JSON');
|
|
723
|
+
if (String(parsed.digest ?? '').trim()) result.digest = memory.addDigest({ date, text: parsed.digest });
|
|
724
|
+
if (Array.isArray(parsed.team)) result.team = memory.edit('team', parsed.team, { source: 'dream' });
|
|
725
|
+
} catch (error) {
|
|
726
|
+
result.errors.push({ bot: 'team', reason: error?.message ?? String(error) });
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
// A day whose every step failed is tried again from the same point tomorrow.
|
|
731
|
+
const tried = result.bots.length + result.errors.length + (result.digest || result.team ? 1 : 0);
|
|
732
|
+
const advanced = tried === 0 || result.bots.length > 0 || Boolean(result.digest || result.team);
|
|
733
|
+
memory.setDreamState({ lastDate: memory.dayOf(now, tz), lastAt: advanced ? now : (before.lastAt ?? null), result });
|
|
734
|
+
staleMemory();
|
|
735
|
+
return result;
|
|
736
|
+
})().finally(() => {
|
|
737
|
+
dreaming = null;
|
|
738
|
+
});
|
|
739
|
+
return dreaming;
|
|
740
|
+
}
|
|
741
|
+
/** Whether today's consolidation is due: past the set time and not yet run today. */
|
|
742
|
+
function dreamDue(now = Date.now()) {
|
|
743
|
+
const m = settings.get().memory;
|
|
744
|
+
if (!m.dream) return false;
|
|
745
|
+
const tz = tzMinutes();
|
|
746
|
+
if (memory.dreamState().lastDate === memory.dayOf(now, tz)) return false;
|
|
747
|
+
const [hh, mm] = m.dreamTime.split(':').map(Number);
|
|
748
|
+
const local = new Date(now + tz * 60_000);
|
|
749
|
+
return local.getUTCHours() * 60 + local.getUTCMinutes() >= hh * 60 + mm;
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
// --- topics ------------------------------------------------------------------------------------
|
|
753
|
+
const rotating = new Map();
|
|
754
|
+
/**
|
|
755
|
+
* Start `bot`'s next topic: a fresh conversation (the old one is archived, its
|
|
756
|
+
* scheduled tasks move to the new one). Refused while the bot is working.
|
|
757
|
+
*/
|
|
758
|
+
function newTopic(ref, reason = 'manual') {
|
|
759
|
+
const bot = store.get(ref.id) ?? ref;
|
|
760
|
+
if (rotating.has(bot.id)) return rotating.get(bot.id);
|
|
761
|
+
const job = (async () => {
|
|
762
|
+
const prev = bot.sessionId;
|
|
763
|
+
const agent = prev ? agents()?.get?.(prev) : undefined;
|
|
764
|
+
if (agent?.status === 'running') throw new Error(`${bot.name} is still working; start a new topic when it is done`);
|
|
765
|
+
const open = prev ? openTasks.get(prev) : undefined;
|
|
766
|
+
if (open) finishTask(open);
|
|
767
|
+
// The last task's summary belongs in the memory the new topic starts with.
|
|
768
|
+
const pending = reflecting.get(bot.id);
|
|
769
|
+
if (pending) await Promise.race([pending, new Promise((r) => setTimeout(r, REFLECT_WAIT_MS).unref?.())]);
|
|
770
|
+
let next;
|
|
771
|
+
try {
|
|
772
|
+
next = await createSession(store.get(bot.id) ?? bot);
|
|
773
|
+
} catch (error) {
|
|
774
|
+
if (prev) store.patch(bot.id, { sessionId: prev });
|
|
775
|
+
throw error;
|
|
776
|
+
}
|
|
777
|
+
if (prev && prev !== next) {
|
|
778
|
+
// Every scheduled task moves along, or none does: one left behind would run in a conversation no bot owns.
|
|
779
|
+
let entries = [];
|
|
780
|
+
try {
|
|
781
|
+
entries = (await schedule().catalog()).filter((e) => e.target?.kind === 'session' && e.target.sessionId === prev);
|
|
782
|
+
} catch {
|
|
783
|
+
entries = []; // No schedule service: nothing targets it.
|
|
784
|
+
}
|
|
785
|
+
const retarget = async (id, sessionId) => {
|
|
786
|
+
const r = await schedule().edit({ id, action: 'update', patch: { target: { kind: 'session', sessionId } } });
|
|
787
|
+
if (r && typeof r === 'object' && 'code' in r && !('title' in r) && r.code !== 'schedule_not_found') throw new Error(String(r.code));
|
|
788
|
+
};
|
|
789
|
+
const moved = [];
|
|
790
|
+
let failed = null;
|
|
791
|
+
for (const e of entries) {
|
|
792
|
+
try {
|
|
793
|
+
await retarget(e.id, next);
|
|
794
|
+
moved.push(e.id);
|
|
795
|
+
} catch (error) {
|
|
796
|
+
failed = error;
|
|
797
|
+
break;
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
if (failed) {
|
|
801
|
+
for (const id of moved) await retarget(id, prev).catch(() => {});
|
|
802
|
+
store.patch(bot.id, { sessionId: prev });
|
|
803
|
+
uninstall(next);
|
|
804
|
+
const back = agents()?.get?.(prev);
|
|
805
|
+
if (back) reconcile(back);
|
|
806
|
+
await Promise.resolve(service(ctx, 'workspaceRegistry')?.archiveSession?.(next)).catch(() => {});
|
|
807
|
+
throw new Error(`could not move the scheduled tasks of @${bot.handle} to a new topic (${failed?.message ?? failed}); it stays in the current one`);
|
|
808
|
+
}
|
|
809
|
+
uninstall(prev);
|
|
810
|
+
memoText.delete(prev);
|
|
811
|
+
memoStale.delete(prev);
|
|
812
|
+
try {
|
|
813
|
+
await service(ctx, 'workspaceRegistry')?.archiveSession?.(prev);
|
|
814
|
+
} catch (error) {
|
|
815
|
+
ctx.logger?.warn?.(`bots: archiving the last topic of @${bot.handle}: ${error?.message ?? error}`);
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
topics.rotate(bot.id, next, reason);
|
|
819
|
+
store.log({ from: 'system', to: bot.id, kind: 'topic', text: reason });
|
|
820
|
+
return next;
|
|
821
|
+
})().finally(() => rotating.delete(bot.id));
|
|
822
|
+
rotating.set(bot.id, job);
|
|
823
|
+
return job;
|
|
824
|
+
}
|
|
825
|
+
/** Start a new topic first when the conversation has gone quiet for long or grown too big. */
|
|
826
|
+
async function autoTopic(bot, trigger) {
|
|
827
|
+
// A topic starting right now: wait for it, so this message goes to the new conversation.
|
|
828
|
+
if (bot?.id && rotating.has(bot.id)) {
|
|
829
|
+
await rotating.get(bot.id).catch(() => {});
|
|
830
|
+
return;
|
|
831
|
+
}
|
|
832
|
+
if (!bot?.sessionId || openTasks.has(bot.sessionId)) return;
|
|
833
|
+
const agent = agents()?.get?.(bot.sessionId);
|
|
834
|
+
if (agent?.status === 'running') return;
|
|
835
|
+
const current = topics.peek(bot.id);
|
|
836
|
+
if (!current || current.sessionId !== bot.sessionId) return;
|
|
837
|
+
const reason = rotationReason(current, settings.get().topics, { trigger });
|
|
838
|
+
if (!reason) return;
|
|
839
|
+
try {
|
|
840
|
+
await newTopic(bot, reason);
|
|
841
|
+
} catch (error) {
|
|
842
|
+
ctx.logger?.warn?.(`bots: new topic for @${bot.handle}: ${error?.message ?? error}`);
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
const TOPIC_COMMAND = /^\/(new|clear|reset|新话题|新对话)(?=$|\s)\s*/i;
|
|
846
|
+
|
|
438
847
|
// --- delivering messages ---------------------------------------------------------------
|
|
439
848
|
const pairs = new Map();
|
|
440
849
|
function admitPair(a, b) {
|
|
@@ -453,7 +862,9 @@ export function apply(ctx, config, deps = {}) {
|
|
|
453
862
|
if (body === '') throw new Error('text must not be empty');
|
|
454
863
|
if (sender && sender.id === target.id) throw new Error('a bot cannot message itself');
|
|
455
864
|
if (sender && !admitPair(sender.id, target.id)) throw new Error(`@${sender.handle} and @${target.handle} exchanged ${PAIR_LIMIT} messages in the last 10 minutes; stop and report to the user instead of continuing.`);
|
|
456
|
-
|
|
865
|
+
// A teammate's message may answer this bot's own question: only a long quiet starts a new topic here.
|
|
866
|
+
await autoTopic(store.get(target.id) ?? target, sender ? 'dm' : 'user');
|
|
867
|
+
const agent = await agentOf(store.get(target.id) ?? target);
|
|
457
868
|
const message = createUserMessage({
|
|
458
869
|
content: [
|
|
459
870
|
{ type: 'text', text: dmHeader(sender) },
|
|
@@ -467,11 +878,20 @@ export function apply(ctx, config, deps = {}) {
|
|
|
467
878
|
return agent.session.id;
|
|
468
879
|
}
|
|
469
880
|
|
|
470
|
-
/**
|
|
881
|
+
/**
|
|
882
|
+
* The user writes to a bot from the panel: an ordinary user prompt in its conversation.
|
|
883
|
+
* `/new` (or `/clear`, `/新话题`) first starts a new topic; text after it goes there.
|
|
884
|
+
*/
|
|
471
885
|
async function userSend(target, text) {
|
|
472
|
-
|
|
886
|
+
let body = String(text ?? '').trim();
|
|
473
887
|
if (body === '') throw new Error('text must not be empty');
|
|
474
|
-
const
|
|
888
|
+
const cmd = TOPIC_COMMAND.exec(body);
|
|
889
|
+
if (cmd) {
|
|
890
|
+
const sessionId = await newTopic(target, 'manual');
|
|
891
|
+
body = body.slice(cmd[0].length).trim();
|
|
892
|
+
if (!body) return sessionId;
|
|
893
|
+
} else await autoTopic(store.get(target.id) ?? target, 'user');
|
|
894
|
+
const agent = await agentOf(store.get(target.id) ?? target);
|
|
475
895
|
agent.followup(createUserMessage({ content: [{ type: 'text', text: body }], source: { kind: 'user' } }));
|
|
476
896
|
await flush(agent);
|
|
477
897
|
return agent.session.id;
|
|
@@ -484,12 +904,14 @@ export function apply(ctx, config, deps = {}) {
|
|
|
484
904
|
entries: () => templates(),
|
|
485
905
|
// A chat message is a user prompt like one typed in the panel, after a line naming where it came from.
|
|
486
906
|
deliver: async (target, { header, body }) => {
|
|
487
|
-
|
|
907
|
+
await autoTopic(store.get(target.id) ?? target, 'user');
|
|
908
|
+
const agent = await agentOf(store.get(target.id) ?? target);
|
|
488
909
|
agent.followup(createUserMessage({ content: [{ type: 'text', text: header }, { type: 'text', text: body }], source: { kind: 'user' } }));
|
|
489
910
|
await flush(agent);
|
|
490
911
|
return agent.session.id;
|
|
491
912
|
},
|
|
492
913
|
running: (sessionId) => agents()?.get?.(sessionId)?.status === 'running',
|
|
914
|
+
newTopic: (bot) => newTopic(bot, 'manual'),
|
|
493
915
|
...(deps.loadConnector ? { loadConnector: deps.loadConnector } : {}),
|
|
494
916
|
...(deps.typingEveryMs ? { typingEveryMs: deps.typingEveryMs } : {}),
|
|
495
917
|
log: (line) => ctx.logger?.info?.(`bots: ${line}`),
|
|
@@ -507,27 +929,31 @@ export function apply(ctx, config, deps = {}) {
|
|
|
507
929
|
const trigger = new Map();
|
|
508
930
|
/** Sessions whose current turn already sent a bot_send_message. */
|
|
509
931
|
const answered = new Set();
|
|
932
|
+
/** Kinds of user message that start (or join) a task. */
|
|
933
|
+
const TASK_KINDS = new Set(['user', 'schedule', 'agent-message']);
|
|
510
934
|
ctx.on('session/event', (session, event) => {
|
|
935
|
+
let bot;
|
|
936
|
+
let via = null;
|
|
937
|
+
let kind;
|
|
511
938
|
try {
|
|
512
|
-
|
|
939
|
+
bot = store.bySession(session?.id);
|
|
513
940
|
if (!bot || !event) return;
|
|
514
941
|
if (event.type === 'user/message') {
|
|
515
|
-
|
|
942
|
+
kind = event.data?.source?.kind;
|
|
516
943
|
// The Harness tells a session about task edits made elsewhere; that is not a delivery.
|
|
517
944
|
if (kind === 'schedule' && /^\[SCHEDULE CHANGE\]/.test(textOf(event.data.content))) kind = 'schedule-notice';
|
|
518
945
|
trigger.set(session.id, kind);
|
|
519
946
|
answered.delete(session.id);
|
|
520
947
|
const said = textOf(event.data.content);
|
|
521
948
|
// A chat message makes its chat where replies go; any other user message brings them back here.
|
|
522
|
-
|
|
949
|
+
via = channels.noteUserMessage(session.id, bot, kind, said);
|
|
523
950
|
if (kind === 'user') store.log(via ? { from: 'user', to: bot.id, text: via.body, kind: 'user', via: via.kind, who: via.who } : { from: 'user', to: bot.id, text: said, kind: 'user' });
|
|
524
951
|
else if (kind === 'schedule') store.log({ from: 'schedule', to: bot.id, text: said, kind: 'system' });
|
|
525
952
|
} else if (event.type === 'assistant/message') {
|
|
526
953
|
const content = event.data?.message?.content ?? [];
|
|
527
954
|
const calls = content.filter((b) => b?.type === 'tool-call');
|
|
528
955
|
if (calls.some((b) => (b.name ?? b.toolName) === 'bot_send_message')) answered.add(session.id);
|
|
529
|
-
|
|
530
|
-
const text = textOf(content);
|
|
956
|
+
const text = calls.length > 0 ? '' : textOf(content);
|
|
531
957
|
const why = trigger.get(session.id);
|
|
532
958
|
// Shown in the bot's chat: what it says to the user. Hidden: acknowledging a schedule notice,
|
|
533
959
|
// and the closing words of a turn that answered a bot by DM (they reach no one). The
|
|
@@ -544,6 +970,59 @@ export function apply(ctx, config, deps = {}) {
|
|
|
544
970
|
} catch {
|
|
545
971
|
// The timeline must never disturb a conversation.
|
|
546
972
|
}
|
|
973
|
+
// Tasks, usage and the transcript memory is written from; never in the way either.
|
|
974
|
+
try {
|
|
975
|
+
if (!bot || !event) return;
|
|
976
|
+
const sid = session.id;
|
|
977
|
+
if (event.type === 'user/message') {
|
|
978
|
+
if (!TASK_KINDS.has(kind)) return;
|
|
979
|
+
const task = openTasks.get(sid) ?? startTask(bot, sid, kind, event, via);
|
|
980
|
+
const said = textOf(event.data?.content);
|
|
981
|
+
const who = kind === 'schedule' ? 'scheduled' : kind === 'agent-message' ? 'bot message' : via ? `user via ${via.kind}` : 'user';
|
|
982
|
+
logLine(task, `[${who}] ${via ? via.body : said}`);
|
|
983
|
+
} else if (event.type === 'assistant/message') {
|
|
984
|
+
const msg = event.data?.message ?? {};
|
|
985
|
+
recordUsage(bot, sid, { at: event.time, kind: 'chat', provider: msg.source?.provider, model: msg.source?.model, usage: event.data?.usage });
|
|
986
|
+
const task = openTasks.get(sid);
|
|
987
|
+
if (!task) return;
|
|
988
|
+
for (const b of Array.isArray(msg.content) ? msg.content : []) {
|
|
989
|
+
if (b?.type === 'text' && b.text?.trim()) logLine(task, `[assistant] ${b.text.trim()}`);
|
|
990
|
+
else if (b?.type === 'tool-call') {
|
|
991
|
+
let args = '';
|
|
992
|
+
try {
|
|
993
|
+
args = typeof b.argsRaw === 'string' ? b.argsRaw : JSON.stringify(b.args ?? b.arguments ?? b.input ?? {});
|
|
994
|
+
} catch {
|
|
995
|
+
args = '';
|
|
996
|
+
}
|
|
997
|
+
logLine(task, `[tool] ${b.name ?? b.toolName ?? 'tool'} ${args.slice(0, 400)}`);
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
} else if (event.type === 'tool/result') {
|
|
1001
|
+
const task = openTasks.get(sid);
|
|
1002
|
+
const text = textOf(event.data?.message?.content ?? event.data?.content);
|
|
1003
|
+
if (task && text) logLine(task, `[result] ${text.slice(0, 300)}`);
|
|
1004
|
+
} else if (event.type === 'compaction/summary') {
|
|
1005
|
+
const d = event.data ?? {};
|
|
1006
|
+
recordUsage(bot, sid, { at: event.time, kind: 'compaction', provider: d.provider, model: d.model, usage: d.usage });
|
|
1007
|
+
} else if (event.type === 'turn/end') {
|
|
1008
|
+
const task = openTasks.get(sid);
|
|
1009
|
+
const reason = event.data?.reason?.kind;
|
|
1010
|
+
if (task && reason && reason !== 'completed') task.status = reason === 'error' ? 'error' : reason === 'aborted' ? 'stopped' : task.status;
|
|
1011
|
+
settleSoon(sid);
|
|
1012
|
+
}
|
|
1013
|
+
} catch (error) {
|
|
1014
|
+
ctx.logger?.debug?.(`bots: usage: ${error?.message ?? error}`);
|
|
1015
|
+
}
|
|
1016
|
+
});
|
|
1017
|
+
// The surest sign a task is over: the bot's Agent went idle.
|
|
1018
|
+
ctx.on('agent/status', (payload) => {
|
|
1019
|
+
try {
|
|
1020
|
+
const sid = payload?.agent?.session?.id;
|
|
1021
|
+
if (sid && payload.status === 'idle' && openTasks.has(sid)) settleSoon(sid, 300);
|
|
1022
|
+
else if (sid && payload.status === 'idle' && memoStale.delete(sid)) memoText.delete(sid);
|
|
1023
|
+
} catch {
|
|
1024
|
+
// Ignored.
|
|
1025
|
+
}
|
|
547
1026
|
});
|
|
548
1027
|
|
|
549
1028
|
// --- schedules ------------------------------------------------------------------------
|
|
@@ -567,6 +1046,7 @@ export function apply(ctx, config, deps = {}) {
|
|
|
567
1046
|
});
|
|
568
1047
|
async function schedulesOf(bot) {
|
|
569
1048
|
const all = await schedule().catalog();
|
|
1049
|
+
for (const e of all) if (e?.id && e.title) scheduleTitles.set(e.id, e.title);
|
|
570
1050
|
const sessions = new Set(store.list().map((b) => b.sessionId).filter(Boolean));
|
|
571
1051
|
return all
|
|
572
1052
|
.filter((e) => e.target?.kind === 'session' && (bot ? e.target.sessionId === bot.sessionId : sessions.has(e.target.sessionId)))
|
|
@@ -607,6 +1087,8 @@ export function apply(ctx, config, deps = {}) {
|
|
|
607
1087
|
}
|
|
608
1088
|
}
|
|
609
1089
|
store.remove(bot.id);
|
|
1090
|
+
memory.dropBot(bot.id);
|
|
1091
|
+
topics.drop(bot.id);
|
|
610
1092
|
if (bot.avatar.kind === 'image') for (const f of [bot.id, `${bot.id}.type`]) rmSync(join(dir, 'avatars', f), { force: true });
|
|
611
1093
|
}
|
|
612
1094
|
|
|
@@ -619,7 +1101,7 @@ export function apply(ctx, config, deps = {}) {
|
|
|
619
1101
|
const agent = bot.sessionId ? agents()?.get?.(bot.sessionId) : undefined;
|
|
620
1102
|
if (agent && bot.model) {
|
|
621
1103
|
try {
|
|
622
|
-
agent.model?.select?.(
|
|
1104
|
+
agent.model?.select?.(selectionOf(bot.model));
|
|
623
1105
|
} catch {
|
|
624
1106
|
// Kept on its current model.
|
|
625
1107
|
}
|
|
@@ -869,6 +1351,69 @@ export function apply(ctx, config, deps = {}) {
|
|
|
869
1351
|
}
|
|
870
1352
|
},
|
|
871
1353
|
}),
|
|
1354
|
+
defineTool({
|
|
1355
|
+
name: 'memory_save',
|
|
1356
|
+
description:
|
|
1357
|
+
"Save a note to long-term memory, which outlasts this conversation and every new topic. Use it when the user asks you to remember something, or you learn a lasting fact, preference, decision or lesson. scope 'self' is your own memory; 'team' is read by every bot (only for what all of them should know). One self-contained sentence; never secrets.",
|
|
1358
|
+
parameters: {
|
|
1359
|
+
text: { type: 'string', required: true, description: 'The note: one sentence, in the language the user uses.' },
|
|
1360
|
+
scope: { type: 'string', enum: ['self', 'team'], description: "Default 'self'." },
|
|
1361
|
+
kind: { type: 'string', enum: MEMORY_KINDS },
|
|
1362
|
+
replace_id: { type: 'string', description: 'Id of an existing note this one corrects or replaces.' },
|
|
1363
|
+
},
|
|
1364
|
+
output: { schema: anyValue, render: (_a, v) => text(v.text), presentationMeta: (_a, v) => ({ note: v.note, scope: v.scope }) },
|
|
1365
|
+
execute: async (args, exec) => {
|
|
1366
|
+
const me = callerBot(exec);
|
|
1367
|
+
const scope = args.scope === 'team' ? 'team' : 'self';
|
|
1368
|
+
if (scope === 'self' && !me) throw new Error("only a bot has its own memory; use scope 'team' to save a note every bot reads");
|
|
1369
|
+
const key = scope === 'team' ? 'team' : me.id;
|
|
1370
|
+
const ops = args.replace_id ? [{ op: 'update', id: String(args.replace_id), text: args.text, kind: args.kind }] : [{ op: 'add', text: args.text, kind: args.kind }];
|
|
1371
|
+
if (args.replace_id && !memory.notes(key).some((n) => n.id === String(args.replace_id))) throw new Error(`no note ${args.replace_id} in ${scope === 'team' ? 'team' : 'your'} memory`);
|
|
1372
|
+
const r = memory.edit(key, ops, { source: 'tool' });
|
|
1373
|
+
const note = memory.notes(key).find((n) => n.id === r.touched[0]) ?? null;
|
|
1374
|
+
if (!note) {
|
|
1375
|
+
if (args.replace_id) throw new Error(`note ${args.replace_id} is pinned by the user; it stays as it is (ask the user to change it)`);
|
|
1376
|
+
throw new Error(looksSecret(args.text) ? 'not saved: it looks like a secret (keys, passwords and tokens are never stored)' : String(args.text ?? '').trim() ? 'not saved: memory is full of pinned notes; ask the user to unpin some' : 'not saved: the note is empty');
|
|
1377
|
+
}
|
|
1378
|
+
return { ok: true, scope, note, text: `${r.added ? 'Saved' : args.replace_id ? 'Updated' : 'Already remembered'} (${scope === 'team' ? 'team memory' : 'your memory'}${note ? `, id ${note.id}` : ''}).` };
|
|
1379
|
+
},
|
|
1380
|
+
}),
|
|
1381
|
+
defineTool({
|
|
1382
|
+
name: 'memory_forget',
|
|
1383
|
+
description: 'Remove a note from long-term memory that is wrong or no longer true, by its id (shown in brackets in your memory).',
|
|
1384
|
+
parameters: { id: { type: 'string', required: true }, scope: { type: 'string', enum: ['self', 'team'], description: "Default 'self'." } },
|
|
1385
|
+
output: { schema: anyValue, render: (_a, v) => text(v.text) },
|
|
1386
|
+
execute: async (args, exec) => {
|
|
1387
|
+
const me = callerBot(exec);
|
|
1388
|
+
const scope = args.scope === 'team' || String(args.id).startsWith('t-') ? 'team' : 'self';
|
|
1389
|
+
if (scope === 'self' && !me) throw new Error('only a bot has its own memory');
|
|
1390
|
+
const key = scope === 'team' ? 'team' : me.id;
|
|
1391
|
+
const target = memory.notes(key).find((n) => n.id === String(args.id));
|
|
1392
|
+
if (!target) throw new Error(`no note ${args.id} in ${scope === 'team' ? 'team' : 'your'} memory`);
|
|
1393
|
+
if (target.pinned) throw new Error(`note ${args.id} is pinned by the user; ask the user before removing it`);
|
|
1394
|
+
memory.remove(key, String(args.id));
|
|
1395
|
+
return { ok: true, text: `Forgot ${args.id}.` };
|
|
1396
|
+
},
|
|
1397
|
+
}),
|
|
1398
|
+
defineTool({
|
|
1399
|
+
name: 'memory_search',
|
|
1400
|
+
description: "Search long-term memory and the team's work journal (what every bot did, task by task) and daily digests, for words in the query. Use it when the user refers to earlier work that is not in this conversation.",
|
|
1401
|
+
parameters: { query: { type: 'string', required: true, description: 'Words that should all appear.' } },
|
|
1402
|
+
output: { schema: anyValue, render: (_a, v) => text(v.text) },
|
|
1403
|
+
isConcurrencySafe: () => true,
|
|
1404
|
+
execute: async (args, exec) => {
|
|
1405
|
+
const me = callerBot(exec);
|
|
1406
|
+
const r = memory.search(args.query, { bot: me?.id ?? '' });
|
|
1407
|
+
const tz = tzMinutes();
|
|
1408
|
+
const when = (ms) => new Date(ms + tz * 60_000).toISOString().slice(0, 16).replace('T', ' ');
|
|
1409
|
+
const lines = [
|
|
1410
|
+
...r.notes.map((n) => `[${n.id}] (${n.scope === 'team' ? 'team' : 'yours'}) ${n.text}`),
|
|
1411
|
+
...r.journal.map((j) => `${when(j.at)} @${store.get(j.bot)?.handle ?? 'removed'}: ${j.summary || j.title}`),
|
|
1412
|
+
...r.digests.map((d) => `Digest ${d.date}: ${d.text.slice(0, 600)}`),
|
|
1413
|
+
];
|
|
1414
|
+
return { ok: true, text: lines.length ? lines.join('\n') : `Nothing in memory matches "${args.query}".` };
|
|
1415
|
+
},
|
|
1416
|
+
}),
|
|
872
1417
|
];
|
|
873
1418
|
|
|
874
1419
|
ctx.effect(() => {
|
|
@@ -938,9 +1483,30 @@ export function apply(ctx, config, deps = {}) {
|
|
|
938
1483
|
const models = [];
|
|
939
1484
|
try {
|
|
940
1485
|
const llm = service(ctx, 'llm');
|
|
1486
|
+
const logins = await loginRoutes();
|
|
941
1487
|
for (const p of llm?.listProviders?.() ?? []) {
|
|
942
1488
|
try {
|
|
943
|
-
|
|
1489
|
+
const list = await llm.listModels(p.id);
|
|
1490
|
+
const infos = await Promise.all(
|
|
1491
|
+
list.slice(0, 200).map((m) =>
|
|
1492
|
+
Promise.resolve()
|
|
1493
|
+
.then(() => (typeof llm.resolveModelInfo === 'function' ? llm.resolveModelInfo(p.id, m.id, AbortSignal.timeout(3000)) : null))
|
|
1494
|
+
.catch(() => null),
|
|
1495
|
+
),
|
|
1496
|
+
);
|
|
1497
|
+
list.forEach((m, i) => {
|
|
1498
|
+
const efforts = Array.isArray(infos[i]?.reasoning?.efforts) ? infos[i].reasoning.efforts.map((e) => ({ id: String(e.id), name: String(e.name ?? e.id) })) : [];
|
|
1499
|
+
models.push({
|
|
1500
|
+
provider: p.id,
|
|
1501
|
+
model: m.id,
|
|
1502
|
+
label: `${p.name ?? p.id} · ${m.name ?? m.id}`,
|
|
1503
|
+
group: p.name ?? p.id,
|
|
1504
|
+
name: m.name ?? m.id,
|
|
1505
|
+
...(p.id.startsWith(ROUTE_PREFIX) ? { source: 'custom' } : logins.has(p.id) ? { source: 'login' } : {}),
|
|
1506
|
+
...(efforts.length ? { efforts, ...(infos[i].reasoning.defaultEffort ? { defaultEffort: String(infos[i].reasoning.defaultEffort) } : {}) } : {}),
|
|
1507
|
+
...(infos[i]?.context?.contextWindow ? { context: infos[i].context.contextWindow } : {}),
|
|
1508
|
+
});
|
|
1509
|
+
});
|
|
944
1510
|
} catch {
|
|
945
1511
|
// One provider down leaves the rest.
|
|
946
1512
|
}
|
|
@@ -948,6 +1514,13 @@ export function apply(ctx, config, deps = {}) {
|
|
|
948
1514
|
} catch {
|
|
949
1515
|
// No model service.
|
|
950
1516
|
}
|
|
1517
|
+
let defaultModel = null;
|
|
1518
|
+
try {
|
|
1519
|
+
const def = service(ctx, 'agentDefaultModel')?.currentSelection?.();
|
|
1520
|
+
if (def?.provider && def?.model) defaultModel = { provider: def.provider, model: def.model };
|
|
1521
|
+
} catch {
|
|
1522
|
+
defaultModel = null;
|
|
1523
|
+
}
|
|
951
1524
|
const skills = await dshSkills();
|
|
952
1525
|
const ocu = ocuPath();
|
|
953
1526
|
return {
|
|
@@ -958,6 +1531,7 @@ export function apply(ctx, config, deps = {}) {
|
|
|
958
1531
|
imageGen: imageGenAvailable(),
|
|
959
1532
|
tools: toolRows,
|
|
960
1533
|
models,
|
|
1534
|
+
defaultModel,
|
|
961
1535
|
skills: skills.map((s) => ({ name: s.name, description: String(s.description ?? '').slice(0, 300) })),
|
|
962
1536
|
ocu: { installed: ocu !== '', path: ocu || OCU_PATHS[0] },
|
|
963
1537
|
mcp: Boolean(mcpClient),
|
|
@@ -965,11 +1539,230 @@ export function apply(ctx, config, deps = {}) {
|
|
|
965
1539
|
};
|
|
966
1540
|
}
|
|
967
1541
|
|
|
1542
|
+
// --- model sources: custom endpoints and other agents' logins -----------------------------
|
|
1543
|
+
/** The settings namespace of Harness's pi-ai adapter (where provider routes live). */
|
|
1544
|
+
function piNamespace() {
|
|
1545
|
+
try {
|
|
1546
|
+
// Other adapters (DeepSeek's own) configure providers elsewhere; the pi-ai one keeps a `providers` dict.
|
|
1547
|
+
const rows = service(ctx, 'llm')?.listConfigurableProviders?.() ?? [];
|
|
1548
|
+
if (rows.some((p) => p.settingsNs === PI_SCOPE)) return PI_SCOPE;
|
|
1549
|
+
const row = rows.find((p) => p.settingsNs && Array.isArray(p.settingsPath) && p.settingsPath[0] === 'providers');
|
|
1550
|
+
if (row) return row.settingsNs;
|
|
1551
|
+
} catch {
|
|
1552
|
+
// Default below.
|
|
1553
|
+
}
|
|
1554
|
+
return PI_SCOPE;
|
|
1555
|
+
}
|
|
1556
|
+
/** The provider routes configured in Harness now (secrets redacted). */
|
|
1557
|
+
function piProviders() {
|
|
1558
|
+
try {
|
|
1559
|
+
const d = service(ctx, 'settings')?.describe?.({ redactSecrets: true })?.find((x) => x.ns === piNamespace());
|
|
1560
|
+
return d?.value?.providers && typeof d.value.providers === 'object' ? d.value.providers : {};
|
|
1561
|
+
} catch {
|
|
1562
|
+
return {};
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
async function writeProvider(route, value) {
|
|
1566
|
+
const s = service(ctx, 'settings');
|
|
1567
|
+
if (!s?.mutate) throw new Error('this Harness cannot change its model settings from a plugin');
|
|
1568
|
+
await s.mutate(piNamespace(), [value === undefined ? { op: 'unset', path: ['providers', route] } : { op: 'set', path: ['providers', route], value }]);
|
|
1569
|
+
}
|
|
1570
|
+
const creds = () => {
|
|
1571
|
+
const c = service(ctx, 'credentials');
|
|
1572
|
+
if (!c) throw new Error('this Harness has no credential store');
|
|
1573
|
+
return c;
|
|
1574
|
+
};
|
|
1575
|
+
const CODEX_KEY = `${PI_SCOPE}/${CODEX_ROUTE}`;
|
|
1576
|
+
/** Routes signed in with a subscription (a stored login, not a key). */
|
|
1577
|
+
async function loginRoutes() {
|
|
1578
|
+
try {
|
|
1579
|
+
const rows = (await service(ctx, 'credentials')?.listRecords?.()) ?? [];
|
|
1580
|
+
return new Set(rows.filter((r) => String(r.key).startsWith(`${PI_SCOPE}/`) && r.kind === 'grant').map((r) => String(r.key).slice(PI_SCOPE.length + 1)));
|
|
1581
|
+
} catch {
|
|
1582
|
+
return new Set();
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
async function modelSources() {
|
|
1586
|
+
const providers = piProviders();
|
|
1587
|
+
const c = service(ctx, 'credentials');
|
|
1588
|
+
const custom = [];
|
|
1589
|
+
for (const [route, profile] of Object.entries(providers)) {
|
|
1590
|
+
if (!route.startsWith(ROUTE_PREFIX)) continue;
|
|
1591
|
+
let configured = false;
|
|
1592
|
+
try {
|
|
1593
|
+
configured = Boolean((await c?.describe?.(keyRefFor(route)))?.configured);
|
|
1594
|
+
} catch {
|
|
1595
|
+
configured = false;
|
|
1596
|
+
}
|
|
1597
|
+
custom.push(endpointView(route, profile, configured));
|
|
1598
|
+
}
|
|
1599
|
+
const grants = await loginRoutes();
|
|
1600
|
+
const logins = detectLogins(home).map((l) => (l.id === 'codex' ? { ...l, connected: Boolean(providers[CODEX_ROUTE]) && grants.has(CODEX_ROUTE), sync: settings.get().codex.sync } : l));
|
|
1601
|
+
return { custom, logins, apis: API_KINDS, writable: Boolean(service(ctx, 'settings')?.mutate && c) };
|
|
1602
|
+
}
|
|
1603
|
+
async function saveEndpoint(body) {
|
|
1604
|
+
const prevRoute = typeof body.route === 'string' && body.route.startsWith(ROUTE_PREFIX) ? body.route : '';
|
|
1605
|
+
const { route, profile } = profileFor({ ...body, ...(prevRoute ? { route: prevRoute } : {}) });
|
|
1606
|
+
const key = String(body.key ?? '').trim();
|
|
1607
|
+
if (!prevRoute) {
|
|
1608
|
+
const taken = Object.keys(piProviders()).includes(route) || (service(ctx, 'llm')?.listProviders?.() ?? []).some((p) => p.id === route);
|
|
1609
|
+
if (taken) throw new Error(`a provider "${route}" exists already; give this endpoint another name`);
|
|
1610
|
+
if (!key) throw new Error('enter the API key (any text for a local server that needs none)');
|
|
1611
|
+
}
|
|
1612
|
+
if (key) await creds().set(keyRefFor(route), key);
|
|
1613
|
+
await writeProvider(route, profile);
|
|
1614
|
+
return { ok: true, route };
|
|
1615
|
+
}
|
|
1616
|
+
async function deleteEndpoint(route) {
|
|
1617
|
+
if (!String(route).startsWith(ROUTE_PREFIX)) throw new Error('only endpoints added in Bots can be removed here');
|
|
1618
|
+
await writeProvider(route, undefined);
|
|
1619
|
+
try {
|
|
1620
|
+
await creds().unset(keyRefFor(route));
|
|
1621
|
+
} catch {
|
|
1622
|
+
// The key may be gone already.
|
|
1623
|
+
}
|
|
1624
|
+
// Bots on it go back to the Harness default.
|
|
1625
|
+
for (const b of store.list()) if (b.model?.provider === route) store.save({ id: b.id, model: null });
|
|
1626
|
+
return { ok: true };
|
|
1627
|
+
}
|
|
1628
|
+
async function testEndpoint(body) {
|
|
1629
|
+
let key = String(body.key ?? '').trim();
|
|
1630
|
+
let { api, baseURL } = body;
|
|
1631
|
+
if (!key && typeof body.route === 'string' && body.route.startsWith(ROUTE_PREFIX)) {
|
|
1632
|
+
// The saved key only ever goes to the address it was saved for.
|
|
1633
|
+
const stored = piProviders()[body.route];
|
|
1634
|
+
if (stored) ({ api, baseURL } = { api: stored.api ?? api, baseURL: stored.baseURL ?? baseURL });
|
|
1635
|
+
try {
|
|
1636
|
+
key = (await creds().resolve(keyRefFor(body.route)))?.value ?? '';
|
|
1637
|
+
} catch {
|
|
1638
|
+
key = '';
|
|
1639
|
+
}
|
|
1640
|
+
}
|
|
1641
|
+
const models = await discoverModels({ api, baseURL, key, ...(deps.fetch ? { fetchImpl: deps.fetch } : {}) });
|
|
1642
|
+
return { ok: true, models: models.slice(0, 500) };
|
|
1643
|
+
}
|
|
1644
|
+
const codexSync = createCodexSync({
|
|
1645
|
+
file: codexAuthFile(home),
|
|
1646
|
+
readRecord: async () => {
|
|
1647
|
+
const r = await service(ctx, 'credentials')?.readRecord?.(CODEX_KEY);
|
|
1648
|
+
return r?.kind === 'grant' ? r.payload : undefined;
|
|
1649
|
+
},
|
|
1650
|
+
writeRecord: async (cred) => {
|
|
1651
|
+
await creds().modifyRecord(CODEX_KEY, async () => ({ kind: 'grant', payload: cred }));
|
|
1652
|
+
},
|
|
1653
|
+
log: (line) => ctx.logger?.info?.(`bots: ${line}`),
|
|
1654
|
+
});
|
|
1655
|
+
async function connectLogin(id) {
|
|
1656
|
+
if (id !== 'codex') throw new Error('this login cannot be used here');
|
|
1657
|
+
const a = readCodexAuth(codexAuthFile(home));
|
|
1658
|
+
if (!a.ok) throw new Error(a.reason === 'api-key' ? 'the Codex CLI is signed in with an API key, not a ChatGPT login' : 'no Codex CLI login found; run `codex login` first');
|
|
1659
|
+
// Harness signed in to ChatGPT by itself: that login is used as it is, never replaced.
|
|
1660
|
+
const existing = await service(ctx, 'credentials')?.readRecord?.(CODEX_KEY);
|
|
1661
|
+
if (existing && !settings.get().codex.sync) throw new Error('Harness is already signed in to ChatGPT (Settings → Models); its models are in the list already');
|
|
1662
|
+
await creds().modifyRecord(CODEX_KEY, async () => ({ kind: 'grant', payload: codexCredential(a) }));
|
|
1663
|
+
const created = !piProviders()[CODEX_ROUTE];
|
|
1664
|
+
if (created) await writeProvider(CODEX_ROUTE, { displayName: 'ChatGPT (Codex)' });
|
|
1665
|
+
settings.save({ codex: { sync: true, route: created || settings.get().codex.route } });
|
|
1666
|
+
return { ok: true, route: CODEX_ROUTE };
|
|
1667
|
+
}
|
|
1668
|
+
async function disconnectLogin(id) {
|
|
1669
|
+
if (id !== 'codex') throw new Error('unknown login');
|
|
1670
|
+
const { sync, route } = settings.get().codex;
|
|
1671
|
+
if (!sync) return { ok: true };
|
|
1672
|
+
settings.save({ codex: { sync: false, route: false } });
|
|
1673
|
+
try {
|
|
1674
|
+
await creds().deleteRecord(CODEX_KEY);
|
|
1675
|
+
} catch {
|
|
1676
|
+
// Gone already.
|
|
1677
|
+
}
|
|
1678
|
+
// Only what connecting added goes: a route the user had configured before stays.
|
|
1679
|
+
if (route && piProviders()[CODEX_ROUTE]) {
|
|
1680
|
+
await writeProvider(CODEX_ROUTE, undefined);
|
|
1681
|
+
for (const b of store.list()) if (b.model?.provider === CODEX_ROUTE) store.save({ id: b.id, model: null });
|
|
1682
|
+
}
|
|
1683
|
+
return { ok: true };
|
|
1684
|
+
}
|
|
1685
|
+
ctx.on('credentials/record-updated', (key) => {
|
|
1686
|
+
if (String(key) !== CODEX_KEY || !settings.get().codex.sync) return;
|
|
1687
|
+
const t = setTimeout(() => void codexSync.syncOnce().catch((error) => ctx.logger?.warn?.(`bots: codex login sync: ${error?.message ?? error}`)), 500);
|
|
1688
|
+
t.unref?.();
|
|
1689
|
+
});
|
|
1690
|
+
|
|
1691
|
+
// --- upkeep: idle topics, the daily dream, the borrowed login --------------------------------
|
|
1692
|
+
let sweeping = false;
|
|
1693
|
+
async function sweep() {
|
|
1694
|
+
if (sweeping) return;
|
|
1695
|
+
sweeping = true;
|
|
1696
|
+
try {
|
|
1697
|
+
for (const bot of store.list()) await autoTopic(bot, 'sweep');
|
|
1698
|
+
if (dreamDue()) await dream({ reason: 'schedule' }).catch((error) => ctx.logger?.warn?.(`bots: dream: ${error?.message ?? error}`));
|
|
1699
|
+
if (settings.get().codex.sync) await codexSync.syncOnce().catch((error) => ctx.logger?.warn?.(`bots: codex login sync: ${error?.message ?? error}`));
|
|
1700
|
+
} finally {
|
|
1701
|
+
sweeping = false;
|
|
1702
|
+
}
|
|
1703
|
+
}
|
|
1704
|
+
if (deps.sweep !== false) {
|
|
1705
|
+
ctx.effect(() => {
|
|
1706
|
+
const timer = setInterval(() => void sweep(), deps.sweepMs ?? SWEEP_MS);
|
|
1707
|
+
timer.unref?.();
|
|
1708
|
+
const first = setTimeout(() => void sweep(), 30_000);
|
|
1709
|
+
first.unref?.();
|
|
1710
|
+
return () => {
|
|
1711
|
+
clearInterval(timer);
|
|
1712
|
+
clearTimeout(first);
|
|
1713
|
+
};
|
|
1714
|
+
}, 'bots: topics and memory upkeep');
|
|
1715
|
+
}
|
|
1716
|
+
|
|
1717
|
+
// --- usage, memory and settings for the page ---------------------------------------------------
|
|
1718
|
+
function usageReport(url) {
|
|
1719
|
+
const tz = url.searchParams.has('tz') ? Number(url.searchParams.get('tz')) : NaN;
|
|
1720
|
+
const overrides = settings.get().prices;
|
|
1721
|
+
const catalog = prices.peek();
|
|
1722
|
+
const running = [...openTasks.values()].map(({ log, size, replied, ...t }) => t);
|
|
1723
|
+
const r = report(
|
|
1724
|
+
{ calls: ledger.calls(), tasks: ledger.tasks(), running },
|
|
1725
|
+
{ range: url.searchParams.get('range') || '7d', bot: url.searchParams.get('bot') || '', tz: Number.isFinite(tz) ? tz : tzMinutes(), price: (m) => priceOf(m, overrides, catalog), schedules: scheduleTitles },
|
|
1726
|
+
);
|
|
1727
|
+
return { ...r, priceList: prices.status() };
|
|
1728
|
+
}
|
|
1729
|
+
function pricesView() {
|
|
1730
|
+
const overrides = settings.get().prices;
|
|
1731
|
+
const catalog = prices.peek();
|
|
1732
|
+
const seen = new Map();
|
|
1733
|
+
for (const r of ledger.calls()) if (r.m && !seen.has(r.m)) seen.set(r.m, r.p);
|
|
1734
|
+
return {
|
|
1735
|
+
overrides,
|
|
1736
|
+
list: prices.status(),
|
|
1737
|
+
models: [...seen].map(([model, provider]) => ({ model, provider, price: priceOf(model, overrides, catalog) })).sort((a, b) => a.model.localeCompare(b.model)),
|
|
1738
|
+
};
|
|
1739
|
+
}
|
|
1740
|
+
function memoryView(scope) {
|
|
1741
|
+
const tz = tzMinutes();
|
|
1742
|
+
const dreamState = memory.dreamState();
|
|
1743
|
+
if (!scope || scope === 'team') {
|
|
1744
|
+
return { scope: 'team', notes: memory.notes('team'), digests: memory.digests().slice(-30).reverse(), journal: memory.journal({ limit: 150 }).reverse(), dream: dreamState, settings: settings.get().memory, tz };
|
|
1745
|
+
}
|
|
1746
|
+
const bot = store.get(scope);
|
|
1747
|
+
if (!bot) return { ok: false, reason: 'no such bot' };
|
|
1748
|
+
return {
|
|
1749
|
+
scope: bot.id,
|
|
1750
|
+
notes: memory.notes(bot.id),
|
|
1751
|
+
journal: memory.journal({ bot: bot.id, limit: 100 }).reverse(),
|
|
1752
|
+
topic: topics.peek(bot.id),
|
|
1753
|
+
topics: topics.history(bot.id).slice(0, 20),
|
|
1754
|
+
model: memoryModelOf(bot),
|
|
1755
|
+
settings: settings.get().memory,
|
|
1756
|
+
tz,
|
|
1757
|
+
};
|
|
1758
|
+
}
|
|
1759
|
+
|
|
968
1760
|
/** A bot as the page sees it: channel tokens masked. */
|
|
969
1761
|
const publicBot = (bot) => (bot ? maskBot(bot, templates()) : bot);
|
|
970
1762
|
|
|
971
1763
|
function state() {
|
|
972
1764
|
const tpl = templates();
|
|
1765
|
+
const today = todayByBot(ledger.calls(), tzMinutes());
|
|
973
1766
|
const bots = store.list().map((b) => {
|
|
974
1767
|
const appIssues = {};
|
|
975
1768
|
for (const app of b.apps) {
|
|
@@ -992,11 +1785,21 @@ export function apply(ctx, config, deps = {}) {
|
|
|
992
1785
|
const live = readLinkedSkill(s.dir);
|
|
993
1786
|
return live ? { ...s, description: live.description || s.description, content: live.content } : { ...s, missing: true };
|
|
994
1787
|
});
|
|
995
|
-
|
|
1788
|
+
const topic = topics.peek(b.id);
|
|
1789
|
+
return {
|
|
1790
|
+
...publicBot(b),
|
|
1791
|
+
skills,
|
|
1792
|
+
status: botStatus(b),
|
|
1793
|
+
appIssues,
|
|
1794
|
+
channels: channels.view(b),
|
|
1795
|
+
topic: topic && topic.sessionId === b.sessionId ? { startedAt: topic.startedAt, lastActiveAt: topic.lastActiveAt, tasks: topic.tasks, context: topic.lastPrompt } : null,
|
|
1796
|
+
usageToday: today[b.id] ?? 0,
|
|
1797
|
+
memoryNotes: memory.notes(b.id).length,
|
|
1798
|
+
};
|
|
996
1799
|
});
|
|
997
1800
|
const sessionMap = {};
|
|
998
1801
|
for (const b of bots) if (b.sessionId) sessionMap[b.sessionId] = b.id;
|
|
999
|
-
return { bots, primaryId: store.primary()?.id ?? null, sessionMap, messageCount: store.messageCount(), version: store.version(), limits: { skills: MAX_SKILLS, skillsOn: MAX_SKILLS_ON } };
|
|
1802
|
+
return { bots, primaryId: store.primary()?.id ?? null, sessionMap, messageCount: store.messageCount(), version: store.version(), limits: { skills: MAX_SKILLS, skillsOn: MAX_SKILLS_ON }, teamNotes: memory.notes('team').length };
|
|
1000
1803
|
}
|
|
1001
1804
|
|
|
1002
1805
|
let ocuInstalling = false;
|
|
@@ -1328,6 +2131,63 @@ export function apply(ctx, config, deps = {}) {
|
|
|
1328
2131
|
post: async (body) => ({ ok: true, task: entryView(checkEntry(await schedule().edit({ id: String(body.id ?? ''), action: 'update', patch: { active: body.active === true } }))) }),
|
|
1329
2132
|
},
|
|
1330
2133
|
'schedules/run': { post: async (body) => (checkEntry(await schedule().run({ id: String(body.id ?? '') })), { ok: true }) },
|
|
2134
|
+
// Usage, prices, memory, topics, settings, model sources.
|
|
2135
|
+
usage: { get: (url) => usageReport(url) },
|
|
2136
|
+
prices: {
|
|
2137
|
+
get: () => pricesView(),
|
|
2138
|
+
post: (body) => {
|
|
2139
|
+
const model = String(body.model ?? '').trim().toLowerCase();
|
|
2140
|
+
if (!model) return { ok: false, reason: 'which model?' };
|
|
2141
|
+
const next = { ...settings.get().prices };
|
|
2142
|
+
if (body.price === null) delete next[model];
|
|
2143
|
+
else next[model] = body.price;
|
|
2144
|
+
settings.save({ prices: next });
|
|
2145
|
+
return { ok: true, ...pricesView() };
|
|
2146
|
+
},
|
|
2147
|
+
},
|
|
2148
|
+
'prices/refresh': { post: async () => (await prices.refresh(true), { ok: true, ...pricesView() }) },
|
|
2149
|
+
settings: {
|
|
2150
|
+
get: () => ({ settings: settings.get() }),
|
|
2151
|
+
post: (body) => {
|
|
2152
|
+
const patch = body.patch && typeof body.patch === 'object' ? { ...body.patch } : {};
|
|
2153
|
+
delete patch.codex;
|
|
2154
|
+
delete patch.prices;
|
|
2155
|
+
return { ok: true, settings: settings.save(patch) };
|
|
2156
|
+
},
|
|
2157
|
+
},
|
|
2158
|
+
memory: { get: (url) => memoryView(url.searchParams.get('bot') ?? 'team') },
|
|
2159
|
+
'memory/put': {
|
|
2160
|
+
post: (body) => {
|
|
2161
|
+
const scope = body.scope === 'team' ? 'team' : store.get(String(body.scope ?? ''))?.id;
|
|
2162
|
+
if (!scope) return { ok: false, reason: 'no such bot' };
|
|
2163
|
+
const note = memory.put(scope, body.note ?? {});
|
|
2164
|
+
staleMemory();
|
|
2165
|
+
return { ok: true, note };
|
|
2166
|
+
},
|
|
2167
|
+
},
|
|
2168
|
+
'memory/delete': {
|
|
2169
|
+
post: (body) => {
|
|
2170
|
+
const scope = body.scope === 'team' ? 'team' : store.get(String(body.scope ?? ''))?.id;
|
|
2171
|
+
if (!scope) return { ok: false, reason: 'no such bot' };
|
|
2172
|
+
const ok = memory.remove(scope, String(body.id ?? ''));
|
|
2173
|
+
if (ok) staleMemory();
|
|
2174
|
+
return { ok };
|
|
2175
|
+
},
|
|
2176
|
+
},
|
|
2177
|
+
'memory/dream': { post: async () => ({ ok: true, result: await dream({ reason: 'manual' }) }) },
|
|
2178
|
+
'topic/new': {
|
|
2179
|
+
post: async (body) => {
|
|
2180
|
+
const bot = store.get(String(body.id ?? ''));
|
|
2181
|
+
if (!bot) return { ok: false, reason: 'no such bot' };
|
|
2182
|
+
return { ok: true, sessionId: await newTopic(bot, 'manual') };
|
|
2183
|
+
},
|
|
2184
|
+
},
|
|
2185
|
+
'models/sources': { get: () => modelSources() },
|
|
2186
|
+
'models/custom/test': { post: (body) => testEndpoint(body) },
|
|
2187
|
+
'models/custom/save': { post: (body) => saveEndpoint(body) },
|
|
2188
|
+
'models/custom/delete': { post: (body) => deleteEndpoint(String(body.route ?? '')) },
|
|
2189
|
+
'models/login/connect': { post: (body) => connectLogin(String(body.id ?? '')) },
|
|
2190
|
+
'models/login/disconnect': { post: (body) => disconnectLogin(String(body.id ?? '')) },
|
|
1331
2191
|
// Chat channels: a pairing code, forgetting a paired person, options, reconnecting.
|
|
1332
2192
|
'channels/code': { post: (body) => ({ ok: true, ...channels.newCode(String(body.botId ?? ''), String(body.appId ?? '')) }) },
|
|
1333
2193
|
'channels/unpair': {
|
|
@@ -1363,7 +2223,7 @@ export function apply(ctx, config, deps = {}) {
|
|
|
1363
2223
|
if (typeof ctx.inject === 'function') ctx.inject(['webServer'], mountRoutes);
|
|
1364
2224
|
else if (ctx.webServer) mountRoutes({ webServer: ctx.webServer, effect: (fn) => ctx.effect(fn) });
|
|
1365
2225
|
|
|
1366
|
-
return { store, state, deliver, install, reconcile, installed, routes, channels };
|
|
2226
|
+
return { store, state, deliver, install, reconcile, installed, routes, channels, memory, ledger, topics, settings, newTopic, dream, sweep, userSend, openTasks, codexSync, finishTask };
|
|
1367
2227
|
}
|
|
1368
2228
|
|
|
1369
2229
|
function serveAvatar(dir, req, res) {
|