sumo-agents 0.1.0 → 0.3.0
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/AGENTS.md +3 -2
- package/README.md +97 -611
- package/bin/sumo.mjs +3 -1
- package/guides/delegation.md +8 -7
- package/package.json +3 -2
- package/prompts/agent.md +3 -3
- package/prompts/dream.md +1 -0
- package/prompts/scribe.md +1 -0
- package/src/apply.mjs +79 -19
- package/src/card.mjs +11 -6
- package/src/chat.mjs +215 -40
- package/src/cli.mjs +120 -31
- package/src/cost.mjs +128 -0
- package/src/db.mjs +40 -1
- package/src/export.mjs +1 -1
- package/src/hooks.mjs +8 -1
- package/src/jobs.mjs +606 -59
- package/src/jobtools.mjs +10 -7
- package/src/loop.mjs +358 -53
- package/src/mcp.mjs +44 -14
- package/src/memory.mjs +39 -4
- package/src/model.mjs +6 -5
- package/src/page.mjs +3 -1
- package/src/paths.mjs +9 -2
- package/src/prime.mjs +14 -5
- package/src/render.mjs +1 -0
- package/src/route.mjs +87 -9
- package/src/sandbox.mjs +173 -0
- package/src/scribe.mjs +7 -5
- package/src/sessions.mjs +9 -6
- package/src/setup.mjs +72 -7
- package/src/sqlite.mjs +66 -0
- package/src/text.mjs +8 -3
- package/src/tools.mjs +562 -31
- package/src/tty.mjs +5 -4
- package/src/ui.mjs +74 -18
- package/src/verify.mjs +51 -8
- package/src/worktree.mjs +128 -0
package/src/scribe.mjs
CHANGED
|
@@ -8,7 +8,7 @@ import { MODELS, offLine, usableModels } from './catalog.mjs';
|
|
|
8
8
|
import { getMeta, openDb, setMeta, tx } from './db.mjs';
|
|
9
9
|
import { localServerStatus } from './local-server.mjs';
|
|
10
10
|
import { callModel, failure, logRun } from './model.mjs';
|
|
11
|
-
import {
|
|
11
|
+
import { paths, REPO_ROOT, SELF } from './paths.mjs';
|
|
12
12
|
import { aliasesOf, listProjects } from './projects.mjs';
|
|
13
13
|
import { line } from './render.mjs';
|
|
14
14
|
import { currentProject, markScribed, pendingTurns } from './sessions.mjs';
|
|
@@ -64,7 +64,7 @@ export function spawnDetached(args) {
|
|
|
64
64
|
appendFileSync(process.env.SUMO_AGENTS_SPAWN_LOG, `${args.join(' ')}\n`);
|
|
65
65
|
return;
|
|
66
66
|
}
|
|
67
|
-
const child = spawn(
|
|
67
|
+
const child = spawn(SELF[0], [...SELF.slice(1), ...args], {
|
|
68
68
|
detached: true,
|
|
69
69
|
stdio: 'ignore',
|
|
70
70
|
env: process.env,
|
|
@@ -155,18 +155,20 @@ export function opsSchema(db, ops) {
|
|
|
155
155
|
body: { type: 'string', description: 'one plain sentence, written as a standing fact or rule' },
|
|
156
156
|
turn: { type: ['integer', 'null'], description: 'the number from the [tN] tag, or null when this is an inferred pattern' },
|
|
157
157
|
quote: { type: ['string', 'null'], description: 'an exact run of words from that user turn, or null when this is an inferred pattern' },
|
|
158
|
+
// Required like the rest, and may be empty: an optional field is a combination more for the grammar to compile.
|
|
159
|
+
keys: { type: 'array', items: { type: 'string' }, description: 'up to 6 short lowercase search terms it is about that are not words of body, or []' },
|
|
158
160
|
};
|
|
159
161
|
const branch = (op, properties) => {
|
|
160
162
|
const all = { op: { type: 'string', enum: [op] }, ...properties };
|
|
161
163
|
return { type: 'object', properties: all, required: Object.keys(all), additionalProperties: false };
|
|
162
164
|
};
|
|
163
165
|
const branches = {
|
|
164
|
-
add: branch('add', { type: field.type, scope: field.scope, topic: field.topic, body: field.body, turn: field.turn, quote: field.quote }),
|
|
166
|
+
add: branch('add', { type: field.type, scope: field.scope, topic: field.topic, body: field.body, turn: field.turn, quote: field.quote, keys: field.keys }),
|
|
165
167
|
supersede: branch('supersede', {
|
|
166
168
|
old: { type: 'integer', description: 'the id of the existing memory this replaces' },
|
|
167
|
-
type: field.type, scope: field.scope, topic: field.topic, body: field.body, turn: field.turn, quote: field.quote,
|
|
169
|
+
type: field.type, scope: field.scope, topic: field.topic, body: field.body, turn: field.turn, quote: field.quote, keys: field.keys,
|
|
168
170
|
}),
|
|
169
|
-
gotcha: branch('gotcha', { scope: field.scope, body: field.body }),
|
|
171
|
+
gotcha: branch('gotcha', { scope: field.scope, body: field.body, keys: field.keys }),
|
|
170
172
|
checkpoint: slugs.length > 0
|
|
171
173
|
? branch('checkpoint', { project: { type: 'string', enum: slugs }, done: { type: 'string' }, next: { type: ['string', 'null'] } })
|
|
172
174
|
: null,
|
package/src/sessions.mjs
CHANGED
|
@@ -14,10 +14,12 @@ const TURN_MAX_CHARS = 4000;
|
|
|
14
14
|
* Where a session starts costing quality. The numbers are absolute tokens, not
|
|
15
15
|
* a share of the window: what degrades an answer is how much context is in
|
|
16
16
|
* play, and a million-token window degrades at the same sizes a small one does.
|
|
17
|
+
* It is said early because every request pays for all of it again: a chat averages
|
|
18
|
+
* some 65k tokens a request, and it is most of what is spent.
|
|
17
19
|
*/
|
|
18
20
|
const BANDS = [
|
|
19
21
|
{ band: 'act', from: 150_000 },
|
|
20
|
-
{ band: 'watch', from:
|
|
22
|
+
{ band: 'watch', from: 60_000 },
|
|
21
23
|
];
|
|
22
24
|
|
|
23
25
|
/** Hooks can arrive in any order and a session can outlive a restart, so every write starts by making sure the row exists. */
|
|
@@ -155,12 +157,13 @@ const inK = (tokens) => `${Math.round(tokens / 1000)}k`;
|
|
|
155
157
|
|
|
156
158
|
/**
|
|
157
159
|
* What the session is told when it crosses a band. One line, ending with the
|
|
158
|
-
* hint, so a `/compact` can be pasted exactly as it stands.
|
|
160
|
+
* hint, so a `/compact` can be pasted exactly as it stands. No figure: a count
|
|
161
|
+
* of its own tokens is what makes a model cut its work short; the screen shows it.
|
|
159
162
|
*/
|
|
160
|
-
export function contextNudge(band,
|
|
163
|
+
export function contextNudge(band, focus) {
|
|
161
164
|
const hint = `hint: focus on ${focus}; keep decisions and open job ids in a note; drop tool output and file contents.`;
|
|
162
|
-
if (band === 'act') return `context:
|
|
163
|
-
return `context:
|
|
165
|
+
if (band === 'act') return `context: very large — start fresh now: note where you are (sumo job note; the end of the turn records "Left off"), then tell the user to type /new. ${hint}`;
|
|
166
|
+
return `context: large — quality drops from here. Finish the piece in hand, then tell the user to type /new: the memory block brings the thread back. ${hint}`;
|
|
164
167
|
}
|
|
165
168
|
|
|
166
169
|
/**
|
|
@@ -172,7 +175,7 @@ export function taskEndedNudge(db) {
|
|
|
172
175
|
const path = db.prepare(`SELECT transcript_path FROM sessions WHERE id NOT LIKE 'job:%' ORDER BY COALESCE(last_turn_at, started_at) DESC LIMIT 1`).get()?.transcript_path ?? null;
|
|
173
176
|
const use = contextUse(path);
|
|
174
177
|
if (!use || !contextBand(use.tokens)) return null;
|
|
175
|
-
return
|
|
178
|
+
return 'this session has grown large and the task just ended — a good moment for /new; the memory block brings the thread back.';
|
|
176
179
|
}
|
|
177
180
|
|
|
178
181
|
/**
|
package/src/setup.mjs
CHANGED
|
@@ -8,12 +8,12 @@ import { basename, delimiter, isAbsolute, join } from 'node:path';
|
|
|
8
8
|
import { createInterface } from 'node:readline/promises';
|
|
9
9
|
import { pipeline } from 'node:stream/promises';
|
|
10
10
|
import { Readable, Transform } from 'node:stream';
|
|
11
|
-
import { resolveAnthropicCredential } from './auth.mjs';
|
|
12
|
-
import { assertOn, discoverModels, discoverySummary, knownModel, MODELS, modelsChecked, modelStates } from './catalog.mjs';
|
|
11
|
+
import { anthropicClient, authenticatedRequest, resolveAnthropicCredential } from './auth.mjs';
|
|
12
|
+
import { assertOn, describeApiError, discoverModels, discoverySummary, knownModel, MODELS, modelsChecked, modelStates } from './catalog.mjs';
|
|
13
13
|
import { getMeta, openDb, SCHEMA_VERSION, schemaVersion, setMeta } from './db.mjs';
|
|
14
14
|
import { stopLocalServer } from './local-server.mjs';
|
|
15
15
|
import { UsageError } from './memory.mjs';
|
|
16
|
-
import { ENTRY, paths, REPO_ROOT } from './paths.mjs';
|
|
16
|
+
import { COMPILED, ENTRY, paths, REPO_ROOT } from './paths.mjs';
|
|
17
17
|
|
|
18
18
|
const NODE_REQUIREMENT = '22.19+ or 24.6+';
|
|
19
19
|
// route.mjs's EFFORTS, spelled out: importing it would close a cycle (route → model → setup). A test keeps them equal.
|
|
@@ -25,10 +25,17 @@ export const CONFIG_DEFAULTS = {
|
|
|
25
25
|
'scribe.model': 'local',
|
|
26
26
|
'dream.model': 'local',
|
|
27
27
|
'chat.model': 'opus',
|
|
28
|
-
'chat.effort': '
|
|
28
|
+
'chat.effort': 'medium',
|
|
29
29
|
'chat.notify': 'on',
|
|
30
|
+
'chat.budget': '5',
|
|
30
31
|
'guides.auto': 'on',
|
|
31
32
|
'jobs.advisor': 'off',
|
|
33
|
+
'jobs.budget': '10',
|
|
34
|
+
'jobs.escalate': 'on',
|
|
35
|
+
'jobs.review': 'risky',
|
|
36
|
+
'shell.sandbox': 'on',
|
|
37
|
+
'jobs.minutes': '60',
|
|
38
|
+
'shell.network': 'on',
|
|
32
39
|
'projects.home': '~',
|
|
33
40
|
'scratch.days': '14',
|
|
34
41
|
'model.source': 'https://huggingface.co',
|
|
@@ -44,7 +51,8 @@ export const CONFIG_DEFAULTS = {
|
|
|
44
51
|
function launcherScript() {
|
|
45
52
|
// Single quotes keep a `$`, backtick or `"` in either path literal; a `'` is closed, escaped and reopened.
|
|
46
53
|
const quote = (s) => `'${s.replaceAll("'", `'\\''`)}'`;
|
|
47
|
-
|
|
54
|
+
const self = COMPILED ? quote(ENTRY) : `${quote(process.execPath)} --disable-warning=ExperimentalWarning ${quote(ENTRY)}`;
|
|
55
|
+
return `#!/bin/sh\nexport NODE_USE_SYSTEM_CA=1\nexec ${self} "$@"\n`;
|
|
48
56
|
}
|
|
49
57
|
|
|
50
58
|
/** A directory already on PATH, inside the home folder, that a command can be linked into without sudo. */
|
|
@@ -183,6 +191,10 @@ function cappedAt(limit) {
|
|
|
183
191
|
/** A model source is a web address; anything else is refused before it is stored, since every later setup would fail on it. */
|
|
184
192
|
function sourceOrRefuse(value) {
|
|
185
193
|
if (!/^https?:\/\/[^\s/]/i.test(value)) throw new UsageError(`"${redactUrl(value)}" is not a web address — a model source looks like https://<host>[/<path>]`);
|
|
194
|
+
// The router model is run on every job: fetched in clear text it is anyone's on the way to replace. Loopback, a mirror on this machine, is the exception.
|
|
195
|
+
if (/^http:/i.test(value) && !/^http:\/\/(?:[^@/]*@)?(?:127\.0\.0\.1|localhost|\[::1\])(?::\d+)?(?:\/|$)/i.test(value)) {
|
|
196
|
+
throw new UsageError(`"${redactUrl(value)}" is plain http: the model would cross the network in clear text. Use https:// (http:// is taken only for 127.0.0.1 and localhost)`);
|
|
197
|
+
}
|
|
186
198
|
return value;
|
|
187
199
|
}
|
|
188
200
|
|
|
@@ -298,7 +310,7 @@ function commandOnPath(name) {
|
|
|
298
310
|
}
|
|
299
311
|
|
|
300
312
|
/** Each check is [ok, label, how to fix]. `warn` checks never fail the run. */
|
|
301
|
-
export function doctor() {
|
|
313
|
+
export async function doctor() {
|
|
302
314
|
const p = paths();
|
|
303
315
|
const checks = [];
|
|
304
316
|
const check = (ok, label, fix, warn = false) => checks.push({ ok, label, fix, warn });
|
|
@@ -310,7 +322,9 @@ export function doctor() {
|
|
|
310
322
|
|
|
311
323
|
const [major, minor] = process.versions.node.split('.').map(Number);
|
|
312
324
|
const nodeOk = (major === 22 && minor >= 19) || (major === 24 && minor >= 6) || major > 24;
|
|
313
|
-
|
|
325
|
+
// The binary Bun builds carries its own runtime, whatever Node version that says it is.
|
|
326
|
+
if (process.versions.bun) check(true, `bun ${process.versions.bun} (a built binary: no Node needed)`, '');
|
|
327
|
+
else check(nodeOk, `node ${process.versions.node}`, `needs Node ${NODE_REQUIREMENT}`);
|
|
314
328
|
|
|
315
329
|
let db = null;
|
|
316
330
|
// Read here, inside the one guarded open: a database that will not open is a failed check, not the end of the report.
|
|
@@ -369,6 +383,9 @@ export function doctor() {
|
|
|
369
383
|
'ANTHROPIC_API_KEY is set or CLAUDE_CODE_OAUTH_TOKEN is set (chat, jobs, and the passes\' fallback)',
|
|
370
384
|
'export ANTHROPIC_API_KEY or CLAUDE_CODE_OAUTH_TOKEN in the shell that starts sessions and hooks',
|
|
371
385
|
);
|
|
386
|
+
// What every request of each role pays for before its first word: the first number of the ledger, and never a failed check.
|
|
387
|
+
const chatModel = stored.configured.find(([key]) => key === 'chat.model')?.[1];
|
|
388
|
+
checks.push(await promptCheck(MODELS.includes(chatModel) ? chatModel : CONFIG_DEFAULTS['chat.model']));
|
|
372
389
|
|
|
373
390
|
// Not a warning: every job is routed by it, and without an answer no job can be created.
|
|
374
391
|
const standIn = process.env.SUMO_AGENTS_MODEL_CMD;
|
|
@@ -385,6 +402,37 @@ export function doctor() {
|
|
|
385
402
|
return checks;
|
|
386
403
|
}
|
|
387
404
|
|
|
405
|
+
/** How long doctor waits for a count: it is one request, and a doctor that hangs tells nobody anything. */
|
|
406
|
+
const COUNT_TIMEOUT_MS = 15_000;
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* The fixed prompt of each role — its tools and its system prompt, with a one-word turn after them, as the endpoint takes
|
|
410
|
+
* no empty conversation — counted by the API's free count_tokens, every role on the chat's model so the numbers compare.
|
|
411
|
+
* The MCP servers' tools are left out: connecting them is not doctor's to do, and they go deferred, outside the prefix.
|
|
412
|
+
* Without a credential, or on any error, it is a warning that says why nothing was counted.
|
|
413
|
+
*/
|
|
414
|
+
async function promptCheck(model) {
|
|
415
|
+
const skipped = (why) => ({ ok: false, warn: true, label: 'prompt: not counted', fix: why });
|
|
416
|
+
const credential = resolveAnthropicCredential();
|
|
417
|
+
if (!credential) return skipped('no Anthropic credential to count with');
|
|
418
|
+
try {
|
|
419
|
+
// Loaded here: the loop and the chat import this module, and only doctor needs them from it.
|
|
420
|
+
const { jobParams, paramsFor } = await import('./loop.mjs');
|
|
421
|
+
const { CHAT_TOOLS, chatSystem } = await import('./chat.mjs');
|
|
422
|
+
const requests = [
|
|
423
|
+
['chat', paramsFor({ model, tools: CHAT_TOOLS, system: chatSystem(), text: 'hi' })],
|
|
424
|
+
...['worker', 'scout', 'reviewer'].map((agent) => [agent, jobParams({ job: { agent, model }, text: 'hi' })]),
|
|
425
|
+
];
|
|
426
|
+
const client = anthropicClient({ timeout: COUNT_TIMEOUT_MS, maxRetries: 0 }, credential);
|
|
427
|
+
// Sent as each role sends it, less what only bounds a reply.
|
|
428
|
+
const counts = await Promise.all(requests.map(([, { max_tokens: _bound, ...params }]) => client.beta.messages.countTokens(authenticatedRequest(params, credential))));
|
|
429
|
+
const said = requests.map(([role], i) => `${role} ${counts[i].input_tokens.toLocaleString('en-US')}`).join(' · ');
|
|
430
|
+
return { ok: true, warn: false, label: `prompt: ${said} tokens on ${model}, without MCP tools`, fix: '' };
|
|
431
|
+
} catch (cause) {
|
|
432
|
+
return skipped(describeApiError(cause));
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
388
436
|
/** A checkout says its commit; an installed package says its version. */
|
|
389
437
|
function codeVersion() {
|
|
390
438
|
try {
|
|
@@ -418,6 +466,10 @@ export function config(key, value) {
|
|
|
418
466
|
if (value !== undefined) {
|
|
419
467
|
// A budget that is not a number would be no budget at all: every session's block would carry every preference.
|
|
420
468
|
if (key === 'prime.budget') positiveInteger(value, 'prime.budget needs a positive number');
|
|
469
|
+
// A cap that is not a number of dollars would be read as the default without a word; 0 is a choice, no cap at all.
|
|
470
|
+
if (key === 'jobs.budget' && !/^\d+(\.\d+)?$/.test(value)) throw new UsageError('jobs.budget needs a number of dollars, 0 or more: what one job may spend before it is stopped, 0 for no cap');
|
|
471
|
+
// The same for a limit in time: whole minutes, and 0 for none.
|
|
472
|
+
if (key === 'jobs.minutes' && !/^\d+$/.test(value)) throw new UsageError('jobs.minutes needs a whole number of minutes, 0 or more: how long one job may run before it is stopped, 0 for no limit');
|
|
421
473
|
// Zero or a word would let go of every chore at the next session start, or of none ever.
|
|
422
474
|
if (key === 'scratch.days') positiveInteger(value, 'scratch.days needs a positive number: the days a chore nothing is written in is kept');
|
|
423
475
|
// Where a new project is made: a place that is not there, or that moves with the shell's directory, is no answer to "where".
|
|
@@ -428,8 +480,16 @@ export function config(key, value) {
|
|
|
428
480
|
// Sent with every chat turn: a word the API does not take would fail each one, not this command.
|
|
429
481
|
if (key === 'chat.effort' && !CHAT_EFFORTS.includes(value)) throw new UsageError(`no such effort "${value}" — one of: ${CHAT_EFFORTS.join(', ')}`);
|
|
430
482
|
if (key === 'chat.notify' && value !== 'on' && value !== 'off') throw new UsageError('chat.notify is on or off: the bell and the notification when a long turn ends behind another window');
|
|
483
|
+
// A word would be no budget at all, and the line it holds back would never be said.
|
|
484
|
+
if (key === 'chat.budget' && !/^\d+(\.\d+)?$/.test(value)) throw new UsageError('chat.budget is a sum in US dollars, like 5 or 2.50 (0 for never): what a chat session costs before the screen says so, once');
|
|
431
485
|
if (key === 'guides.auto' && value !== 'on' && value !== 'off') throw new UsageError('guides.auto is on or off: a guide riding in with the message that asks for its kind of work, and a turn held for an edit nothing checked');
|
|
432
486
|
if (key === 'jobs.advisor' && value !== 'on' && value !== 'off') throw new UsageError('jobs.advisor is on or off: a job asking the next model up for advice, from inside its own request');
|
|
487
|
+
if (key === 'jobs.escalate' && value !== 'on' && value !== 'off') throw new UsageError('jobs.escalate is on or off: a worker\'s first attempt that fails retried once, one step up, without being asked');
|
|
488
|
+
if (key === 'jobs.review' && !['risky', 'all', 'off'].includes(value)) {
|
|
489
|
+
throw new UsageError('jobs.review is risky, all or off: which workers that finish DONE, verified, are reviewed without being asked — the ones the router called risky, every one, or none');
|
|
490
|
+
}
|
|
491
|
+
if (key === 'shell.sandbox' && value !== 'on' && value !== 'off') throw new UsageError('shell.sandbox is on or off: the model\'s shell writing only in the project, the temporary folders and the tool caches, on macOS');
|
|
492
|
+
if (key === 'shell.network' && value !== 'on' && value !== 'off') throw new UsageError('shell.network is on or off: the sandboxed shell reaching past this machine — off, it reaches only loopback and local sockets');
|
|
433
493
|
// Stored for every later setup: one that is not a URL would fail each of them.
|
|
434
494
|
if (key === 'model.source') sourceOrRefuse(value);
|
|
435
495
|
// The file name decides where the bytes land and what the server is asked for: a name, never a path.
|
|
@@ -446,6 +506,11 @@ export function config(key, value) {
|
|
|
446
506
|
assertOn(db, value);
|
|
447
507
|
}
|
|
448
508
|
setMeta(db, `config.${key}`, value);
|
|
509
|
+
// Read once a process, so a shell that edits the database cannot switch them off under itself: a chat already open keeps
|
|
510
|
+
// what it read, for the jobs it starts too.
|
|
511
|
+
if (key === 'shell.sandbox' || key === 'shell.network') {
|
|
512
|
+
return [shown(key), 'takes effect in chats and `sumo job run`s started from now on — a chat already open keeps its setting, for the jobs it starts too, until it is started again'];
|
|
513
|
+
}
|
|
449
514
|
}
|
|
450
515
|
return [shown(key)];
|
|
451
516
|
} finally {
|
package/src/sqlite.mjs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const PREFIXES = [':', '$', '@'];
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The class every sumo database is opened with. Under Node it is node:sqlite's DatabaseSync itself. The binary
|
|
5
|
+
* `npm run build:bin` makes runs on Bun, which has no node:sqlite: there it is bun:sqlite's Database made to answer
|
|
6
|
+
* the calls sumo makes the way DatabaseSync does. Node never loads bun:sqlite, nor Bun node:sqlite.
|
|
7
|
+
*/
|
|
8
|
+
export const DatabaseSync = process.versions.bun
|
|
9
|
+
? bunDatabaseSync((await import('bun:sqlite')).Database)
|
|
10
|
+
: (await import('node:sqlite')).DatabaseSync;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The values for one statement, as bun:sqlite takes them. node:sqlite binds a named value given bare ({ id }) to
|
|
14
|
+
* :id, $id or @id, and bun:sqlite only one spelled with its prefix; node:sqlite leaves a placeholder it was given no
|
|
15
|
+
* value for NULL, and bun:sqlite refuses the statement.
|
|
16
|
+
*/
|
|
17
|
+
export function bunArgs(args, count) {
|
|
18
|
+
const [first] = args;
|
|
19
|
+
if (args.length === 1 && first !== null && typeof first === 'object' && !ArrayBuffer.isView(first)) {
|
|
20
|
+
const named = {};
|
|
21
|
+
for (const [key, value] of Object.entries(first)) {
|
|
22
|
+
for (const name of PREFIXES.includes(key[0]) ? [key] : PREFIXES.map((p) => `${p}${key}`)) named[name] = value;
|
|
23
|
+
}
|
|
24
|
+
return [named];
|
|
25
|
+
}
|
|
26
|
+
return args.length < count ? [...args, ...Array(count - args.length).fill(null)] : args;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** DatabaseSync's surface over bun:sqlite's Database, for what sumo uses: prepare().get/all/run/iterate, exec, close. */
|
|
30
|
+
export function bunDatabaseSync(Database) {
|
|
31
|
+
const statement = (db, stmt) => {
|
|
32
|
+
const bind = (args) => bunArgs(args, stmt.paramsCount);
|
|
33
|
+
return {
|
|
34
|
+
// No row is undefined from node:sqlite, null from bun:sqlite.
|
|
35
|
+
get: (...args) => stmt.get(...bind(args)) ?? undefined,
|
|
36
|
+
all: (...args) => stmt.all(...bind(args)),
|
|
37
|
+
run: (...args) => {
|
|
38
|
+
const { lastInsertRowid } = stmt.run(...bind(args));
|
|
39
|
+
// bun:sqlite counts the rows its triggers wrote too (an index kept in step); node:sqlite, as SQLite's changes(), the statement's own.
|
|
40
|
+
return { changes: db.query('SELECT changes() AS n').get().n, lastInsertRowid };
|
|
41
|
+
},
|
|
42
|
+
iterate: (...args) => stmt.iterate(...bind(args)),
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
return class DatabaseSync {
|
|
46
|
+
#db;
|
|
47
|
+
|
|
48
|
+
constructor(path, { readOnly = false } = {}) {
|
|
49
|
+
this.#db = new Database(path, readOnly ? { readonly: true } : { readwrite: true, create: true });
|
|
50
|
+
// node:sqlite turns foreign keys on for every connection it opens; SQLite's own default is off.
|
|
51
|
+
this.#db.exec('PRAGMA foreign_keys = ON');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
prepare(sql) {
|
|
55
|
+
return statement(this.#db, this.#db.prepare(sql));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
exec(sql) {
|
|
59
|
+
this.#db.exec(sql);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
close() {
|
|
63
|
+
this.#db.close();
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
package/src/text.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const STOPWORDS = new Set(
|
|
1
|
+
export const STOPWORDS = new Set(
|
|
2
2
|
`a an and are as at be but by do does for from has have how i if in into is it its me my no not of
|
|
3
3
|
on or our so that the their them then there these this to us was we what when where which who why
|
|
4
4
|
will with you your`.split(/\s+/),
|
|
@@ -78,15 +78,20 @@ export function clip(text, max) {
|
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
/** Words that mark a sentence as a standing instruction, which is worth filing right away rather than in the next batch. */
|
|
81
|
-
const DURABLE = /\b(always|never|from now on|going forward|remember|don'?t ever|i (prefer|like|want|hate|use)|we (use|moved|switched|decided)|make sure|by default|every time|stop (doing|using))\b/i;
|
|
81
|
+
const DURABLE = /\b(always|never|from now on|going forward|remember|don'?t ever|i (prefer|like|want(?! to\b)|hate|use)|we (use|moved|switched|decided)|make sure|by default|every time|stop (doing|using))\b/i;
|
|
82
82
|
|
|
83
83
|
export const looksDurable = (text) => DURABLE.test(text);
|
|
84
84
|
|
|
85
85
|
/**
|
|
86
86
|
* A clause that asks for work or an answer — describe this, fix that, delegate, a question — rather than stating
|
|
87
87
|
* something. Only verbs that are almost never a standing rule: "write tests first" or "keep answers short" are rules.
|
|
88
|
+
* The words people open an order with (ok, lets, so) do not hide it, and "I want to …" is the next piece of work.
|
|
88
89
|
*/
|
|
89
|
-
const
|
|
90
|
+
const OPENERS = /(?:(?:please|pls|now|then|also|and|so+|ok|okay|yes|yeah|yep|na+h|just|lets|let's|let\s+us)[\s,]+)*/.source;
|
|
91
|
+
const VERBS = /describe|explain|delegate|fix|find|investigate|look\s+(?:at|into|for)|show\s+me|tell\s+me|give\s+me|summari[sz]e|debug|continue|research|install|spawn|implement|proceed|resume|redeploy|restart|rerun|retry|go\s+ahead|carry\s+on|try\s+again|(?:can|could|would|will)\s+you|i\s+(?:want|need|would\s+like|'d\s+like)\s+to/.source;
|
|
92
|
+
/** What marks an order as one for now, whatever its verb: "not … yet" holds something for the moment, "let me know" asks for an answer. */
|
|
93
|
+
const FOR_NOW = /\b(?:not|n't|dont|don't)\b[^.!?\n]*\byet\b|\b(?:lmk|let\s+me\s+know)\b/.source;
|
|
94
|
+
const REQUEST = new RegExp(`(?:^|[.,:;!?)]\\s+|\\n\\s*)${OPENERS}(?:${VERBS})\\b|\\?\\s*$|${FOR_NOW}`, 'im');
|
|
90
95
|
|
|
91
96
|
/** Words that ask for work or an answer and state nothing that lasts: nothing in them is a memory. */
|
|
92
97
|
export const asksForWork = (text) => REQUEST.test(String(text ?? '')) && !looksDurable(String(text ?? ''));
|