isorite 0.1.0 → 0.2.0-trial.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/dist/core.js +125 -2
- package/dist/main.js +2277 -363
- package/package.json +2 -1
- package/plugin/server.ts +9 -8
- package/plugin/tour.ts +5 -8
- package/web/_app/immutable/assets/0.s5xqevrq.css +1 -0
- package/web/_app/immutable/assets/4.PVZR2_N5.css +1 -0
- package/web/_app/immutable/assets/5.DCxNIgyW.css +1 -0
- package/web/_app/immutable/assets/9.CFA9_nYZ.css +1 -0
- package/web/_app/immutable/assets/FolderField._A4_sc1Z.css +1 -0
- package/web/_app/immutable/assets/NoteEditor.ChlpmyHO.css +1 -0
- package/web/_app/immutable/assets/NoteList.F579Zvj_.css +1 -0
- package/web/_app/immutable/assets/{Timeline.UMF1LjSO.css → Timeline.KTc3LKuK.css} +1 -1
- package/web/_app/immutable/assets/Tutorial.DOfsM49T.css +1 -0
- package/web/_app/immutable/chunks/B0NIb88Q.js +1 -0
- package/web/_app/immutable/chunks/{B-si2ell.js → BRnMDye8.js} +1 -1
- package/web/_app/immutable/chunks/C38XJeNJ.js +1 -0
- package/web/_app/immutable/chunks/C8UdwlAg.js +3 -0
- package/web/_app/immutable/chunks/CEQ-cFxt.js +667 -0
- package/web/_app/immutable/chunks/CRzLMJPS.js +1 -0
- package/web/_app/immutable/chunks/CWITxcMm.js +13 -0
- package/web/_app/immutable/chunks/Ch0511Wu.js +1 -0
- package/web/_app/immutable/chunks/D56iKolu.js +1 -0
- package/web/_app/immutable/chunks/DAVQx5al.js +1 -0
- package/web/_app/immutable/chunks/DP2vIlnJ.js +20 -0
- package/web/_app/immutable/chunks/DTwIGCa8.js +1 -0
- package/web/_app/immutable/chunks/DhKewaLD.js +1 -0
- package/web/_app/immutable/chunks/Dm229u8R.js +1 -0
- package/web/_app/immutable/chunks/GgEyipRh.js +1 -0
- package/web/_app/immutable/entry/app.C_YVtBm3.js +2 -0
- package/web/_app/immutable/entry/start.36VrMTu2.js +1 -0
- package/web/_app/immutable/nodes/0.CgaPlR93.js +14 -0
- package/web/_app/immutable/nodes/1.CeByL9Re.js +1 -0
- package/web/_app/immutable/nodes/2.DUNH537j.js +1 -0
- package/web/_app/immutable/nodes/3.CaAED8T_.js +1 -0
- package/web/_app/immutable/nodes/4.BBhoqQg0.js +2 -0
- package/web/_app/immutable/nodes/5.CkVNYg2h.js +5 -0
- package/web/_app/immutable/nodes/6.CYRn28m8.js +1 -0
- package/web/_app/immutable/nodes/7.BYBNXM1A.js +1 -0
- package/web/_app/immutable/nodes/8.BdGwzJFT.js +1 -0
- package/web/_app/immutable/nodes/9.NF5w8YC2.js +486 -0
- package/web/_app/immutable/workers/worker-rKDNrEKQ.js +1 -0
- package/web/_app/version.json +1 -1
- package/web/icon-180.png +0 -0
- package/web/icon-192.png +0 -0
- package/web/icon-512.png +0 -0
- package/web/icon-maskable-512.png +0 -0
- package/web/index.html +35 -12
- package/web/manifest.webmanifest +14 -0
- package/web/_app/immutable/assets/0.CbWSsYpm.css +0 -1
- package/web/_app/immutable/assets/3.Bgs8n1LK.css +0 -1
- package/web/_app/immutable/assets/4.CCFEPTFN.css +0 -1
- package/web/_app/immutable/assets/5.BCvLF9Xz.css +0 -1
- package/web/_app/immutable/assets/6.kPo5mcJx.css +0 -1
- package/web/_app/immutable/assets/NoteEditor.Bp-YDqYj.css +0 -1
- package/web/_app/immutable/chunks/BD1Dpm2e.js +0 -3
- package/web/_app/immutable/chunks/C0fpmehI.js +0 -1
- package/web/_app/immutable/chunks/CWYACT2V.js +0 -1
- package/web/_app/immutable/chunks/CdQZ-ArP.js +0 -1
- package/web/_app/immutable/chunks/DN3dk6rD.js +0 -147
- package/web/_app/immutable/chunks/DtAOoM-T.js +0 -1
- package/web/_app/immutable/chunks/DykrN7z5.js +0 -535
- package/web/_app/immutable/chunks/cmGruEKS.js +0 -1
- package/web/_app/immutable/chunks/gRYnlonl.js +0 -1
- package/web/_app/immutable/entry/app.DgJiXIYs.js +0 -2
- package/web/_app/immutable/entry/start.l9vV7NaJ.js +0 -1
- package/web/_app/immutable/nodes/0.DWPeBju2.js +0 -1
- package/web/_app/immutable/nodes/1.CJVDS8FT.js +0 -1
- package/web/_app/immutable/nodes/2.o8ItfT-a.js +0 -1
- package/web/_app/immutable/nodes/3.uhHyOPGK.js +0 -13
- package/web/_app/immutable/nodes/4.BYKkAZTp.js +0 -2
- package/web/_app/immutable/nodes/5.BzEQO7Xv.js +0 -4
- package/web/_app/immutable/nodes/6.GxiAUa6J.js +0 -1
- package/web/_app/immutable/nodes/7.d2Ere8Jb.js +0 -1
- package/web/_app/immutable/nodes/8.CvPHZL8j.js +0 -1
- package/web/_app/immutable/workers/worker-d9GxGKde.js +0 -1
package/dist/main.js
CHANGED
|
@@ -12,10 +12,12 @@ import { Worker } from "node:worker_threads";
|
|
|
12
12
|
import { Hono } from "hono";
|
|
13
13
|
import { streamSSE } from "hono/streaming";
|
|
14
14
|
import { z } from "zod";
|
|
15
|
+
import { OpenCode } from "@opencode/client";
|
|
15
16
|
import { appendFile, mkdir, readFile, readdir, rename, rm, writeFile } from "node:fs/promises";
|
|
16
17
|
import { execFileSync, spawn } from "node:child_process";
|
|
17
18
|
import { createInterface } from "node:readline";
|
|
18
|
-
import { createServer } from "node:
|
|
19
|
+
import { createServer } from "node:net";
|
|
20
|
+
import { createServer as createServer$1 } from "node:http";
|
|
19
21
|
//#region packages/core/src/text.ts
|
|
20
22
|
const WS = "\\t\\n\\x0b\\x0c\\r\\x1c-\\x1f \\x85\\xa0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000";
|
|
21
23
|
const LEAD = new RegExp(`^[${WS}]+`, "u");
|
|
@@ -1407,7 +1409,8 @@ function moments(entries) {
|
|
|
1407
1409
|
const what = e.why === "start" ? e.who === "external" ? "found" : "wrote" : e.why === "edit" ? "edited" : e.why === "restore" ? "restored" : e.why === "keep" ? "kept" : e.why === "move" ? "moved" : "changed elsewhere";
|
|
1408
1410
|
out.push({
|
|
1409
1411
|
...base,
|
|
1410
|
-
what
|
|
1412
|
+
what,
|
|
1413
|
+
...e.asked ? { asked: e.asked } : {}
|
|
1411
1414
|
});
|
|
1412
1415
|
}
|
|
1413
1416
|
}
|
|
@@ -1574,6 +1577,11 @@ function understanding(events, now = Date.now()) {
|
|
|
1574
1577
|
s.last = when;
|
|
1575
1578
|
s.evidence.push(e.id);
|
|
1576
1579
|
}
|
|
1580
|
+
} else if (e.event === "understood" && typeof e.note === "string") {
|
|
1581
|
+
const s = at(e.note), by = e.by;
|
|
1582
|
+
s.level = clamp(s.level + (typeof by === "number" && Number.isFinite(by) ? Math.max(-1, Math.min(1, by)) : 0));
|
|
1583
|
+
s.last = when;
|
|
1584
|
+
s.evidence.push(e.id);
|
|
1577
1585
|
} else if (e.event === "moved" && typeof e.from === "string" && typeof e.to === "string") {
|
|
1578
1586
|
const s = state.get(e.from);
|
|
1579
1587
|
if (s) {
|
|
@@ -2055,7 +2063,7 @@ async function rounds(o) {
|
|
|
2055
2063
|
callId: c.id,
|
|
2056
2064
|
content: text
|
|
2057
2065
|
});
|
|
2058
|
-
o.onStep(step);
|
|
2066
|
+
if (step) o.onStep(step);
|
|
2059
2067
|
}
|
|
2060
2068
|
}
|
|
2061
2069
|
}
|
|
@@ -2063,7 +2071,7 @@ async function rounds(o) {
|
|
|
2063
2071
|
//#region packages/engine/src/iso.ts
|
|
2064
2072
|
/** The point in the note where new text is asked for. Not part of the note. */
|
|
2065
2073
|
const HERE = "⟦HERE⟧";
|
|
2066
|
-
const WHO = `You are Iso, the companion in isorite, an app for understanding a subject by
|
|
2074
|
+
const WHO$1 = `You are Iso, the companion in isorite, an app for understanding a subject by
|
|
2067
2075
|
writing it down. The person is writing a note in their own knowledge base: a
|
|
2068
2076
|
folder of Markdown notes with links between them. They have asked you for
|
|
2069
2077
|
something at one place in the note they have open.`;
|
|
@@ -2145,6 +2153,8 @@ const FORMAT = `# How notes work here
|
|
|
2145
2153
|
equation as a paragraph of its own, with $$ alone on the line before it
|
|
2146
2154
|
and $$ alone on the line after.
|
|
2147
2155
|
- Ordinary Markdown otherwise. No HTML.`;
|
|
2156
|
+
/** How notes are written, for any request that writes or quotes them. */
|
|
2157
|
+
const FORMAT_RULES = FORMAT;
|
|
2148
2158
|
const LOOKUP = (rounds, meaning) => `# Looking things up
|
|
2149
2159
|
|
|
2150
2160
|
Before you reply you may look things up in their knowledge base with the
|
|
@@ -2245,14 +2255,23 @@ function windowed(lines, keep, budget = NOTE_BUDGET) {
|
|
|
2245
2255
|
flush();
|
|
2246
2256
|
return out.join("\n");
|
|
2247
2257
|
}
|
|
2258
|
+
/** The notes a note links to, as they are sent with a request in it. */
|
|
2259
|
+
function linkedSection(linked, intro = "The notes this note links to") {
|
|
2260
|
+
return linked.length ? `# ${intro}\n\n${linked.map((l) => `## ${l.title} (/${l.id}.md)\n\n${l.text}`).join("\n\n")}` : "";
|
|
2261
|
+
}
|
|
2262
|
+
const NO_LOOKUP = `# Looking things up
|
|
2263
|
+
|
|
2264
|
+
You have no tools this time: they chose what you are sent. Answer from it, and
|
|
2265
|
+
say plainly what you would need to look up to say more.`;
|
|
2248
2266
|
/** The messages for new text at a point, and what they hold, part by part. */
|
|
2249
2267
|
function insertMessages(a) {
|
|
2250
2268
|
const system = [
|
|
2251
|
-
WHO,
|
|
2269
|
+
WHO$1,
|
|
2252
2270
|
INSERT$1,
|
|
2253
2271
|
FORMAT,
|
|
2254
|
-
|
|
2272
|
+
a.lookup === false ? NO_LOOKUP : looked$1(a)
|
|
2255
2273
|
].join("\n\n");
|
|
2274
|
+
const linked = linkedSection(a.linked ?? []);
|
|
2256
2275
|
const note = `# Their note: ${a.title} (/${a.id}.md)\n\n${a.marked}` + (a.unkept.length ? `\n\n# Text you offered in this note that they have not kept yet\n\nIt is not part of the note. Do not write it again.\n\n${a.unkept.join("\n\n---\n\n")}` : "");
|
|
2257
2276
|
const earlier = a.conversation ? `# Earlier in this session\n\n${a.conversation}` : "";
|
|
2258
2277
|
const request = `# What they ask now\n\n${a.asked}`;
|
|
@@ -2267,47 +2286,54 @@ function insertMessages(a) {
|
|
|
2267
2286
|
role: "user",
|
|
2268
2287
|
content: [
|
|
2269
2288
|
earlier,
|
|
2289
|
+
linked,
|
|
2270
2290
|
note,
|
|
2271
2291
|
request
|
|
2272
2292
|
].filter(Boolean).join("\n\n")
|
|
2273
2293
|
}],
|
|
2274
|
-
parts:
|
|
2275
|
-
{
|
|
2276
|
-
name: "instructions and tools",
|
|
2277
|
-
detail: "",
|
|
2278
|
-
tokens: estimate(system) + 500
|
|
2279
|
-
},
|
|
2280
|
-
{
|
|
2281
|
-
name: "study record",
|
|
2282
|
-
detail: "nothing yet",
|
|
2283
|
-
tokens: 0
|
|
2284
|
-
},
|
|
2285
|
-
{
|
|
2286
|
-
name: "your prompts and Iso's answers",
|
|
2287
|
-
detail: a.moments ? `${a.moments} ${a.moments === 1 ? "moment" : "moments"}` : "",
|
|
2288
|
-
tokens: estimate(earlier)
|
|
2289
|
-
},
|
|
2290
|
-
{
|
|
2291
|
-
name: "this note",
|
|
2292
|
-
detail: a.title,
|
|
2293
|
-
tokens: estimate(note) + estimate(request)
|
|
2294
|
-
},
|
|
2295
|
-
{
|
|
2296
|
-
name: "linked notes",
|
|
2297
|
-
detail: a.linked?.notes ? `${a.linked.notes} read` : "",
|
|
2298
|
-
tokens: a.linked?.tokens ?? 0
|
|
2299
|
-
}
|
|
2300
|
-
]
|
|
2294
|
+
parts: inPlaceParts(a, system, earlier, linked, note, request)
|
|
2301
2295
|
};
|
|
2302
2296
|
}
|
|
2297
|
+
/** The look-up instructions for a request in place, saying what is sent below it. */
|
|
2298
|
+
const looked$1 = (a) => LOOKUP(ROUNDS[a.tier], a.meaning).replace("below there is only the note.", a.linked?.length ? "below there is the note, and\nthe notes it links to that they chose to send." : "below there is only the note.");
|
|
2299
|
+
function inPlaceParts(a, system, earlier, linked, note, request) {
|
|
2300
|
+
return [
|
|
2301
|
+
{
|
|
2302
|
+
name: "instructions and tools",
|
|
2303
|
+
detail: a.lookup === false ? "no tools" : "",
|
|
2304
|
+
tokens: estimate(system) + (a.lookup === false ? 0 : 500)
|
|
2305
|
+
},
|
|
2306
|
+
{
|
|
2307
|
+
name: "study record",
|
|
2308
|
+
detail: "nothing yet",
|
|
2309
|
+
tokens: 0
|
|
2310
|
+
},
|
|
2311
|
+
{
|
|
2312
|
+
name: "your prompts and Iso's answers",
|
|
2313
|
+
detail: a.moments ? `${a.moments} ${a.moments === 1 ? "moment" : "moments"}` : "",
|
|
2314
|
+
tokens: estimate(earlier)
|
|
2315
|
+
},
|
|
2316
|
+
{
|
|
2317
|
+
name: "this note",
|
|
2318
|
+
detail: a.title,
|
|
2319
|
+
tokens: estimate(note) + estimate(request)
|
|
2320
|
+
},
|
|
2321
|
+
{
|
|
2322
|
+
name: "linked notes",
|
|
2323
|
+
detail: a.linked?.length ? `${a.linked.length} sent` : "",
|
|
2324
|
+
tokens: estimate(linked)
|
|
2325
|
+
}
|
|
2326
|
+
];
|
|
2327
|
+
}
|
|
2303
2328
|
/** The messages for a rewrite of chosen paragraphs. */
|
|
2304
2329
|
function rewriteMessages(a) {
|
|
2305
2330
|
const system = [
|
|
2306
|
-
WHO,
|
|
2331
|
+
WHO$1,
|
|
2307
2332
|
REWRITE(a.count),
|
|
2308
2333
|
FORMAT,
|
|
2309
|
-
|
|
2334
|
+
a.lookup === false ? NO_LOOKUP : looked$1(a)
|
|
2310
2335
|
].join("\n\n");
|
|
2336
|
+
const linked = linkedSection(a.linked ?? []);
|
|
2311
2337
|
const note = `# Their note: ${a.title} (/${a.id}.md)\n\n${a.marked}`;
|
|
2312
2338
|
const earlier = a.conversation ? `# Earlier in this session\n\n${a.conversation}` : "";
|
|
2313
2339
|
const request = `# What they ask now, of the marked ${a.count === 1 ? "passage" : "passages"}\n\n${a.asked}`;
|
|
@@ -2322,37 +2348,12 @@ function rewriteMessages(a) {
|
|
|
2322
2348
|
role: "user",
|
|
2323
2349
|
content: [
|
|
2324
2350
|
earlier,
|
|
2351
|
+
linked,
|
|
2325
2352
|
note,
|
|
2326
2353
|
request
|
|
2327
2354
|
].filter(Boolean).join("\n\n")
|
|
2328
2355
|
}],
|
|
2329
|
-
parts:
|
|
2330
|
-
{
|
|
2331
|
-
name: "instructions and tools",
|
|
2332
|
-
detail: "",
|
|
2333
|
-
tokens: estimate(system) + 500
|
|
2334
|
-
},
|
|
2335
|
-
{
|
|
2336
|
-
name: "study record",
|
|
2337
|
-
detail: "nothing yet",
|
|
2338
|
-
tokens: 0
|
|
2339
|
-
},
|
|
2340
|
-
{
|
|
2341
|
-
name: "your prompts and Iso's answers",
|
|
2342
|
-
detail: a.moments ? `${a.moments} ${a.moments === 1 ? "moment" : "moments"}` : "",
|
|
2343
|
-
tokens: estimate(earlier)
|
|
2344
|
-
},
|
|
2345
|
-
{
|
|
2346
|
-
name: "this note",
|
|
2347
|
-
detail: a.title,
|
|
2348
|
-
tokens: estimate(note) + estimate(request)
|
|
2349
|
-
},
|
|
2350
|
-
{
|
|
2351
|
-
name: "linked notes",
|
|
2352
|
-
detail: a.linked?.notes ? `${a.linked.notes} read` : "",
|
|
2353
|
-
tokens: a.linked?.tokens ?? 0
|
|
2354
|
-
}
|
|
2355
|
-
]
|
|
2356
|
+
parts: inPlaceParts(a, system, earlier, linked, note, request)
|
|
2356
2357
|
};
|
|
2357
2358
|
}
|
|
2358
2359
|
/** A rewrite's reply, taken apart: the new text for each of the `count` passages (null where it left one as it
|
|
@@ -2396,7 +2397,7 @@ const LINKED_BUDGET = 8e3;
|
|
|
2396
2397
|
/** The messages for a note written for Iso. */
|
|
2397
2398
|
function noteMessages(a) {
|
|
2398
2399
|
const system = [
|
|
2399
|
-
WHO.replace("They have asked you for\nsomething at one place in the note they have open.", "This time they have written to you at length."),
|
|
2400
|
+
WHO$1.replace("They have asked you for\nsomething at one place in the note they have open.", "This time they have written to you at length."),
|
|
2400
2401
|
NOTE,
|
|
2401
2402
|
FORMAT,
|
|
2402
2403
|
LOOKUP(ROUNDS[a.tier], a.meaning).replace("below there is only the note.", "below there is what they wrote, and the notes they linked.")
|
|
@@ -2654,8 +2655,8 @@ function exerciseReply(reply) {
|
|
|
2654
2655
|
}
|
|
2655
2656
|
//#endregion
|
|
2656
2657
|
//#region packages/engine/src/lookup.ts
|
|
2657
|
-
const str = { type: "string" };
|
|
2658
|
-
const fn = (name, description, properties, required) => ({
|
|
2658
|
+
const str$1 = { type: "string" };
|
|
2659
|
+
const fn$1 = (name, description, properties, required) => ({
|
|
2659
2660
|
type: "function",
|
|
2660
2661
|
function: {
|
|
2661
2662
|
name,
|
|
@@ -2669,10 +2670,10 @@ const fn = (name, description, properties, required) => ({
|
|
|
2669
2670
|
}
|
|
2670
2671
|
});
|
|
2671
2672
|
const TOOLS = [
|
|
2672
|
-
fn("search_notes", "Keyword search over the knowledge base. Returns note ids, titles, types, one-sentence descriptions and a one-line snippet. Search by the words a note would use.", {
|
|
2673
|
-
query: str,
|
|
2673
|
+
fn$1("search_notes", "Keyword search over the knowledge base. Returns note ids, titles, types, one-sentence descriptions and a one-line snippet. Search by the words a note would use.", {
|
|
2674
|
+
query: str$1,
|
|
2674
2675
|
under: {
|
|
2675
|
-
...str,
|
|
2676
|
+
...str$1,
|
|
2676
2677
|
description: "Only notes in this folder."
|
|
2677
2678
|
},
|
|
2678
2679
|
limit: {
|
|
@@ -2680,25 +2681,25 @@ const TOOLS = [
|
|
|
2680
2681
|
description: "How many, 8 unless said, 15 at most."
|
|
2681
2682
|
}
|
|
2682
2683
|
}, ["query"]),
|
|
2683
|
-
fn("outline_note", "A note's description, its headings, the notes it links to and the notes that link to it, without its body. Use it to choose a section to read.", { id: {
|
|
2684
|
-
...str,
|
|
2684
|
+
fn$1("outline_note", "A note's description, its headings, the notes it links to and the notes that link to it, without its body. Use it to choose a section to read.", { id: {
|
|
2685
|
+
...str$1,
|
|
2685
2686
|
description: "The note's id or path: basics/entropy or /basics/entropy.md."
|
|
2686
2687
|
} }, ["id"]),
|
|
2687
|
-
fn("read_note", "Read one section of a note, by its heading, or the whole note when no section is named. Prefer a section.", {
|
|
2688
|
-
id: str,
|
|
2688
|
+
fn$1("read_note", "Read one section of a note, by its heading, or the whole note when no section is named. Prefer a section.", {
|
|
2689
|
+
id: str$1,
|
|
2689
2690
|
section: {
|
|
2690
|
-
...str,
|
|
2691
|
+
...str$1,
|
|
2691
2692
|
description: "A heading's text, as outline_note gives it."
|
|
2692
2693
|
}
|
|
2693
2694
|
}, ["id"])
|
|
2694
2695
|
];
|
|
2695
|
-
const FIND_SIMILAR = fn("find_similar", "Find notes by meaning: notes that say something like the text you give, even in quite other words. Slower and vaguer than search_notes: use it after search_notes and the links of the notes in hand have not found what you need, or when you cannot guess the words a note would use. Give a question or a sentence, not keywords. Returns each note's nearest section, with how alike it is (0 to 1).", {
|
|
2696
|
+
const FIND_SIMILAR = fn$1("find_similar", "Find notes by meaning: notes that say something like the text you give, even in quite other words. Slower and vaguer than search_notes: use it after search_notes and the links of the notes in hand have not found what you need, or when you cannot guess the words a note would use. Give a question or a sentence, not keywords. Returns each note's nearest section, with how alike it is (0 to 1).", {
|
|
2696
2697
|
text: {
|
|
2697
|
-
...str,
|
|
2698
|
+
...str$1,
|
|
2698
2699
|
description: "A question, or a sentence saying what you are looking for."
|
|
2699
2700
|
},
|
|
2700
2701
|
under: {
|
|
2701
|
-
...str,
|
|
2702
|
+
...str$1,
|
|
2702
2703
|
description: "Only notes in this folder."
|
|
2703
2704
|
},
|
|
2704
2705
|
limit: {
|
|
@@ -2867,84 +2868,337 @@ var Lookup = class {
|
|
|
2867
2868
|
}
|
|
2868
2869
|
};
|
|
2869
2870
|
//#endregion
|
|
2870
|
-
//#region packages/engine/src/
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
/** The
|
|
2878
|
-
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
|
|
2882
|
-
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
if (
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
}
|
|
2871
|
+
//#region packages/engine/src/understand.ts
|
|
2872
|
+
const UNDERSTANDING_PREFIX = "@understanding/";
|
|
2873
|
+
const UNDERSTANDING_DIR = "understanding";
|
|
2874
|
+
const isUnderstandingId = (id) => id.startsWith(UNDERSTANDING_PREFIX);
|
|
2875
|
+
const understandingId = (note) => UNDERSTANDING_PREFIX + note;
|
|
2876
|
+
const READING = "## Iso's reading";
|
|
2877
|
+
const PAD = "## Scratchpad";
|
|
2878
|
+
/** The two parts of an Understanding note's file. A file in another shape is read as all scratchpad. */
|
|
2879
|
+
function parseUnderstanding(text) {
|
|
2880
|
+
if (!text) return {
|
|
2881
|
+
reading: "",
|
|
2882
|
+
scratchpad: ""
|
|
2883
|
+
};
|
|
2884
|
+
const body = text.replace(/^---\n[\s\S]*?\n---\n/, "");
|
|
2885
|
+
const r = body.indexOf(READING), p = body.indexOf(PAD);
|
|
2886
|
+
if (r < 0 && p < 0) return {
|
|
2887
|
+
reading: "",
|
|
2888
|
+
scratchpad: body.trim()
|
|
2889
|
+
};
|
|
2890
|
+
return {
|
|
2891
|
+
reading: r < 0 ? "" : body.slice(r + 16, p > r ? p : void 0).trim(),
|
|
2892
|
+
scratchpad: p < 0 ? "" : body.slice(p + 13, r > p ? r : void 0).trim()
|
|
2893
|
+
};
|
|
2893
2894
|
}
|
|
2894
|
-
/**
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
|
|
2895
|
+
/** An Understanding note's file, from its parts. */
|
|
2896
|
+
function understandingText(note, title, parts) {
|
|
2897
|
+
return `---\ntype: Understanding\ntitle: ${JSON.stringify(`Understanding · ${title}`)}\nof: ${note}\n---\n\n${READING}\n\n${parts.reading.trim()}\n\n${PAD}\n\n${parts.scratchpad.trim()}\n`;
|
|
2898
|
+
}
|
|
2899
|
+
//#endregion
|
|
2900
|
+
//#region packages/engine/src/turn.ts
|
|
2901
|
+
/** What Iso may do. Build needs a project and a shell, and is the terminal's (M11). */
|
|
2902
|
+
const MODES = [
|
|
2903
|
+
"ask",
|
|
2904
|
+
"write",
|
|
2905
|
+
"build",
|
|
2906
|
+
"understand"
|
|
2907
|
+
];
|
|
2908
|
+
const CONVERSATIONS_DIR = "conversations";
|
|
2909
|
+
const conversationPath = (id) => `${CONVERSATIONS_DIR}/${id}.jsonl`;
|
|
2910
|
+
/** A conversation's turns from its file, oldest first. A line that cannot be read is passed over. */
|
|
2911
|
+
function readTurns(text) {
|
|
2912
|
+
const turns = /* @__PURE__ */ new Map();
|
|
2913
|
+
for (const raw of text.split("\n")) {
|
|
2914
|
+
if (!raw.trim()) continue;
|
|
2915
|
+
let line;
|
|
2916
|
+
try {
|
|
2917
|
+
line = JSON.parse(raw);
|
|
2918
|
+
} catch {
|
|
2919
|
+
continue;
|
|
2911
2920
|
}
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
if (last && s.from <= last.to) last.to = Math.max(last.to, s.to);
|
|
2921
|
-
else merged.push({ ...s });
|
|
2922
|
-
}
|
|
2923
|
-
const moved = (i, counts) => i + hunks.filter((h) => counts(h.at)).reduce((d, h) => d + h.ins.length - h.del.length, 0);
|
|
2924
|
-
const start = (i) => moved(i, (at) => at < i), end = (i) => moved(i, (at) => at <= i);
|
|
2925
|
-
const offset = (i) => a.slice(0, i).reduce((n, line) => n + line.length + 1, 0);
|
|
2926
|
-
return merged.map(({ from, to }) => {
|
|
2927
|
-
const lines = b.slice(start(from), end(to));
|
|
2928
|
-
let text = a.slice(from, to).join("\n"), at = offset(from);
|
|
2929
|
-
if (!lines.length) {
|
|
2930
|
-
if (to < a.length) text += "\n";
|
|
2931
|
-
else if (from > 0) {
|
|
2932
|
-
text = "\n" + text;
|
|
2933
|
-
at -= 1;
|
|
2934
|
-
}
|
|
2921
|
+
if (line.kind === "turn") {
|
|
2922
|
+
const { kind: _, ...t } = line;
|
|
2923
|
+
turns.set(t.id, {
|
|
2924
|
+
...t,
|
|
2925
|
+
items: [],
|
|
2926
|
+
state: "running"
|
|
2927
|
+
});
|
|
2928
|
+
continue;
|
|
2935
2929
|
}
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
|
|
2940
|
-
|
|
2941
|
-
|
|
2942
|
-
|
|
2943
|
-
|
|
2930
|
+
const t = turns.get(line.turn);
|
|
2931
|
+
if (!t) continue;
|
|
2932
|
+
if (line.kind === "item") t.items.push(line.item);
|
|
2933
|
+
else if (line.kind === "answer") {
|
|
2934
|
+
for (const i of t.items) if (i.kind === "permission" && i.id === line.permission) i.answer = line.decision;
|
|
2935
|
+
} else {
|
|
2936
|
+
t.state = line.state;
|
|
2937
|
+
if (line.message) t.message = line.message;
|
|
2938
|
+
if (line.cost) t.cost = line.cost;
|
|
2939
|
+
}
|
|
2940
|
+
}
|
|
2941
|
+
return [...turns.values()].map((t) => t.state === "running" ? {
|
|
2942
|
+
...t,
|
|
2943
|
+
state: "stopped"
|
|
2944
|
+
} : t).sort((a, b) => a.id < b.id ? -1 : 1);
|
|
2945
|
+
}
|
|
2946
|
+
const WHO = `You are Iso, the companion in isorite, an app for understanding a subject by
|
|
2947
|
+
writing it down. The person keeps a knowledge base: a folder of Markdown notes
|
|
2948
|
+
with links between them. They are talking with you in isorite's Iso pane,
|
|
2949
|
+
beside their notes and the Atlas, the map of their base. What you look up and
|
|
2950
|
+
what you change is shown to them there as you do it.`;
|
|
2951
|
+
const MODE_RULES = {
|
|
2952
|
+
ask: `# Ask mode: what you may do
|
|
2953
|
+
|
|
2954
|
+
You read their notes and answer. You cannot change anything in this mode: no
|
|
2955
|
+
note, no folder.
|
|
2956
|
+
|
|
2957
|
+
- Answer what they ask, in plain prose. Short: a few sentences to a few
|
|
2958
|
+
paragraphs, unless more is asked for. Markdown and maths are shown as they
|
|
2959
|
+
are in their notes.
|
|
2960
|
+
- Rest on their notes where the base says something: look first, and link to
|
|
2961
|
+
the notes you used, so they can open them. Where the base does not hold what
|
|
2962
|
+
is asked, answer from what you know of the subject, exactly, and say that
|
|
2963
|
+
their notes do not cover it.
|
|
2964
|
+
- When one of their notes would help them and they may not know it, point it
|
|
2965
|
+
out with point_to_note. Use it sparingly: one or two notes.
|
|
2966
|
+
- Never invent a note, a link or a quotation.`,
|
|
2967
|
+
write: `# Write mode: what you may do
|
|
2968
|
+
|
|
2969
|
+
You may change their knowledge base: edit notes, make new ones, move them and
|
|
2970
|
+
delete them. What you change goes into their notes at once. They see every
|
|
2971
|
+
change marked as yours, and can undo it.
|
|
2972
|
+
|
|
2973
|
+
- Do what they ask, and only that. Change the least that does it: edit the
|
|
2974
|
+
passage that needs it with edit_note, not the whole note. Keep their words,
|
|
2975
|
+
links, maths and voice wherever the request does not call for a change.
|
|
2976
|
+
- Look before you write: read the part of the note you change, and the notes
|
|
2977
|
+
it rests on, so what you write agrees with the base and links to it.
|
|
2978
|
+
- A new note (write_note) is written as one of theirs: frontmatter with type
|
|
2979
|
+
and title, their notation, their level, plain words, linked to the notes it
|
|
2980
|
+
rests on.
|
|
2981
|
+
- Moving or deleting a note is asked of them before it happens. Do it only
|
|
2982
|
+
when they asked for it.
|
|
2983
|
+
- When you are done, say in a sentence or two what you changed and why. Do not
|
|
2984
|
+
repeat the text you wrote: they see it in the note.
|
|
2985
|
+
- Never invent a note, a link or a quotation.`,
|
|
2986
|
+
understand: `# Understand mode: what you may do
|
|
2987
|
+
|
|
2988
|
+
You help them understand: what is in their notes, what changed in them, or
|
|
2989
|
+
something they want to understand more deeply. You do not change their notes.
|
|
2990
|
+
You keep a record of their understanding, note by note, in their study base.
|
|
2991
|
+
|
|
2992
|
+
- Start from where they are. Below, for each note in hand, is your reading of
|
|
2993
|
+
their understanding of it so far and what changed it. Use it: do not explain
|
|
2994
|
+
what they have shown they know; go to what they have not yet shown.
|
|
2995
|
+
- Ask before you explain, where a question would show you where they stand.
|
|
2996
|
+
One question at a time. Short turns.
|
|
2997
|
+
- When they show understanding, or a gap, record it with
|
|
2998
|
+
record_understanding: how far this turn is evidence that their
|
|
2999
|
+
understanding of the note differs from what you had (-1, -0.5, 0, 0.5 or 1)
|
|
3000
|
+
and why, in one plain sentence to them. At most once a note a turn.
|
|
3001
|
+
- At the end of a conversation about a note, or when your reading of them has
|
|
3002
|
+
changed, rewrite your reading with write_understanding: a few plain
|
|
3003
|
+
sentences on what they can do with the note and what they have not yet
|
|
3004
|
+
shown. It is theirs to read.
|
|
3005
|
+
- Never invent a note, a link or a quotation.`
|
|
3006
|
+
};
|
|
3007
|
+
const LOOKING = (rounds, meaning, lookup) => lookup ? `# Looking things up
|
|
3008
|
+
|
|
3009
|
+
- Look up only what the request needs. search_notes finds notes by their
|
|
3010
|
+
words; outline_note shows a note's headings and links; read_note reads one
|
|
3011
|
+
section. Prefer a section to a whole note. Follow links one step at a time.${meaning ? `
|
|
3012
|
+
- find_similar finds notes by meaning, when their words have not found them.` : ""}
|
|
3013
|
+
- You may call several tools at once, over ${rounds} rounds at most; then reply.
|
|
3014
|
+
- Say nothing between tool calls. Only the reply is shown.` : `# Looking things up
|
|
3015
|
+
|
|
3016
|
+
They chose what you are sent and asked you not to look further: you have no
|
|
3017
|
+
tools for searching or reading. Answer from what is below, and say plainly what
|
|
3018
|
+
you would need to look at to say more.`;
|
|
3019
|
+
const str = { type: "string" };
|
|
3020
|
+
const fn = (name, description, properties, required) => ({
|
|
3021
|
+
type: "function",
|
|
3022
|
+
function: {
|
|
3023
|
+
name,
|
|
3024
|
+
description,
|
|
3025
|
+
parameters: {
|
|
3026
|
+
type: "object",
|
|
3027
|
+
properties,
|
|
3028
|
+
required,
|
|
3029
|
+
additionalProperties: false
|
|
3030
|
+
}
|
|
3031
|
+
}
|
|
3032
|
+
});
|
|
3033
|
+
const POINT_TO_NOTE = fn("point_to_note", "Point the person to one of their notes: it is marked on their Atlas, with why. For a note that would help them that they may not know of.", {
|
|
3034
|
+
id: {
|
|
3035
|
+
...str,
|
|
3036
|
+
description: "The note's id or path."
|
|
3037
|
+
},
|
|
3038
|
+
why: {
|
|
3039
|
+
...str,
|
|
3040
|
+
description: "Why, in a short sentence to them."
|
|
3041
|
+
}
|
|
3042
|
+
}, ["id", "why"]);
|
|
3043
|
+
const EDIT_NOTE$1 = fn("edit_note", "Replace one passage of a note with new text. `old` must be exactly as it is in the note, and must occur in it once: read it first. Give enough of it to be unique. An empty `new` takes the passage out.", {
|
|
3044
|
+
id: {
|
|
3045
|
+
...str,
|
|
3046
|
+
description: "The note's id or path."
|
|
3047
|
+
},
|
|
3048
|
+
old: {
|
|
3049
|
+
...str,
|
|
3050
|
+
description: "The passage as it is now, exactly."
|
|
3051
|
+
},
|
|
3052
|
+
new: {
|
|
3053
|
+
...str,
|
|
3054
|
+
description: "What it becomes."
|
|
3055
|
+
}
|
|
3056
|
+
}, [
|
|
3057
|
+
"id",
|
|
3058
|
+
"old",
|
|
3059
|
+
"new"
|
|
3060
|
+
]);
|
|
3061
|
+
const WRITE_NOTE$1 = fn("write_note", "Make a new note, or write a note's whole text anew. For a new note give its whole file: frontmatter (type, title) then the body. Prefer edit_note for a change to a note that exists.", {
|
|
3062
|
+
id: {
|
|
3063
|
+
...str,
|
|
3064
|
+
description: "The note's id: folder/name, lower case with hyphens. A new folder is made for it."
|
|
3065
|
+
},
|
|
3066
|
+
text: {
|
|
3067
|
+
...str,
|
|
3068
|
+
description: "The note's whole file."
|
|
3069
|
+
}
|
|
3070
|
+
}, ["id", "text"]);
|
|
3071
|
+
const MOVE_NOTE = fn("move_note", "Move or rename a note. Links to it are changed to match. They are asked first.", {
|
|
3072
|
+
id: str,
|
|
3073
|
+
to: {
|
|
3074
|
+
...str,
|
|
3075
|
+
description: "Its new id: folder/name."
|
|
3076
|
+
}
|
|
3077
|
+
}, ["id", "to"]);
|
|
3078
|
+
const DELETE_NOTE = fn("delete_note", "Delete a note. They are asked first. Its history stays.", { id: str }, ["id"]);
|
|
3079
|
+
const RECORD_UNDERSTANDING = fn("record_understanding", "Record evidence of their understanding of one note from this turn: how far it differs from what you had, and why.", {
|
|
3080
|
+
id: {
|
|
3081
|
+
...str,
|
|
3082
|
+
description: "The note's id."
|
|
3083
|
+
},
|
|
3084
|
+
by: {
|
|
3085
|
+
type: "number",
|
|
3086
|
+
enum: [
|
|
3087
|
+
-1,
|
|
3088
|
+
-.5,
|
|
3089
|
+
0,
|
|
3090
|
+
.5,
|
|
3091
|
+
1
|
|
3092
|
+
]
|
|
3093
|
+
},
|
|
3094
|
+
why: {
|
|
3095
|
+
...str,
|
|
3096
|
+
description: "One plain sentence, to them."
|
|
3097
|
+
}
|
|
3098
|
+
}, [
|
|
3099
|
+
"id",
|
|
3100
|
+
"by",
|
|
3101
|
+
"why"
|
|
3102
|
+
]);
|
|
3103
|
+
const WRITE_UNDERSTANDING = fn("write_understanding", "Rewrite your reading of their understanding of one note: a few plain sentences on what they can do with it and what they have not yet shown. Replaces the reading you had.", {
|
|
3104
|
+
id: {
|
|
3105
|
+
...str,
|
|
3106
|
+
description: "The note's id."
|
|
3107
|
+
},
|
|
3108
|
+
reading: str
|
|
3109
|
+
}, ["id", "reading"]);
|
|
3110
|
+
/** The tools each mode adds to looking things up. */
|
|
3111
|
+
const MODE_TOOLS = {
|
|
3112
|
+
ask: [POINT_TO_NOTE],
|
|
3113
|
+
write: [
|
|
3114
|
+
EDIT_NOTE$1,
|
|
3115
|
+
WRITE_NOTE$1,
|
|
3116
|
+
MOVE_NOTE,
|
|
3117
|
+
DELETE_NOTE,
|
|
3118
|
+
POINT_TO_NOTE
|
|
3119
|
+
],
|
|
3120
|
+
understand: [
|
|
3121
|
+
RECORD_UNDERSTANDING,
|
|
3122
|
+
WRITE_UNDERSTANDING,
|
|
3123
|
+
POINT_TO_NOTE
|
|
3124
|
+
]
|
|
3125
|
+
};
|
|
3126
|
+
function sentText(s) {
|
|
3127
|
+
if (s.kind === "note") return `## ${s.title} (/${s.id}.md)\n\n${windowed(s.text.split("\n"), [0])}`;
|
|
3128
|
+
if (s.kind === "passage") return `## A passage they chose in ${s.title} (/${s.id}.md)\n\n${s.text.split("\n").map((l) => `> ${l}`).join("\n")}`;
|
|
3129
|
+
if (s.kind === "folder") return `## The folder ${s.id || "at the top of the base"}: its notes\n\n${s.notes.map((n) => `- ${n.title} (/${n.id}.md)${n.description ? `: ${n.description}` : ""}`).join("\n") || "(empty)"}`;
|
|
3130
|
+
return `## Their understanding of ${s.title} (/${s.id}.md): ${s.squares} of 6 squares\n\nYour reading so far: ${s.reading || "none yet."}\n\nWhat changed it, newest first:\n${s.events.map((e) => `- ${e}`).join("\n") || "- nothing yet"}`;
|
|
3131
|
+
}
|
|
3132
|
+
/** An earlier turn as it is sent: in the shape `conversation()` shortens. */
|
|
3133
|
+
function turnExchange(t) {
|
|
3134
|
+
const text = t.items.filter((i) => i.kind === "text").map((i) => i.text).join("\n\n").trim();
|
|
3135
|
+
const changed = t.items.flatMap((i) => i.kind === "change" ? [`${i.why === "create" ? "made" : i.why === "delete" ? "deleted" : i.why === "move" ? "moved" : "changed"} ${i.title} (/${i.note}.md)`] : []);
|
|
3136
|
+
const gave = t.given.filter((g) => g.kind !== "understanding").map((g) => g.kind === "passage" ? `a passage of /${g.id}.md` : `/${g.id}${g.kind === "note" ? ".md" : "/"}`);
|
|
3137
|
+
return {
|
|
3138
|
+
id: t.id,
|
|
3139
|
+
note: "",
|
|
3140
|
+
text: `In ${t.mode} mode they asked${gave.length ? `, giving you ${gave.join(", ")}` : ""}: ${t.asked}\n\nYou wrote:\n\n${text || "(nothing)"}\n\n${changed.length ? `You ${changed.join("; ")}.` : t.state === "done" ? "" : `The turn was ${t.state}.`}`.trim()
|
|
3141
|
+
};
|
|
2944
3142
|
}
|
|
2945
|
-
/** The
|
|
2946
|
-
function
|
|
2947
|
-
|
|
3143
|
+
/** The messages for a turn, and what they hold, part by part. */
|
|
3144
|
+
function turnMessages(a) {
|
|
3145
|
+
const system = [
|
|
3146
|
+
WHO,
|
|
3147
|
+
MODE_RULES[a.mode],
|
|
3148
|
+
FORMAT_RULES,
|
|
3149
|
+
LOOKING(ROUNDS[a.tier], a.meaning, a.lookup)
|
|
3150
|
+
].join("\n\n");
|
|
3151
|
+
const talk = conversation(a.earlier.map(turnExchange));
|
|
3152
|
+
const earlier = talk ? `# Earlier in this conversation\n\n${talk}` : "";
|
|
3153
|
+
const understood = a.sent.filter((s) => s.kind === "understanding");
|
|
3154
|
+
const given = a.sent.filter((s) => s.kind !== "understanding");
|
|
3155
|
+
const gave = given.length ? `# What they gave you\n\n${given.map(sentText).join("\n\n")}` : "";
|
|
3156
|
+
const known = understood.length ? `# Their understanding, as you have recorded it\n\n${understood.map(sentText).join("\n\n")}` : "";
|
|
3157
|
+
const request = `# What they ask now\n\n${a.asked}`;
|
|
3158
|
+
return {
|
|
3159
|
+
messages: [{
|
|
3160
|
+
role: "system",
|
|
3161
|
+
content: [{
|
|
3162
|
+
text: system,
|
|
3163
|
+
cache: true
|
|
3164
|
+
}]
|
|
3165
|
+
}, {
|
|
3166
|
+
role: "user",
|
|
3167
|
+
content: [
|
|
3168
|
+
earlier,
|
|
3169
|
+
known,
|
|
3170
|
+
gave,
|
|
3171
|
+
request
|
|
3172
|
+
].filter(Boolean).join("\n\n")
|
|
3173
|
+
}],
|
|
3174
|
+
parts: [
|
|
3175
|
+
{
|
|
3176
|
+
name: "instructions and tools",
|
|
3177
|
+
detail: `${a.mode} mode${a.lookup ? "" : ", no look-ups"}`,
|
|
3178
|
+
tokens: estimate(system) + (a.lookup ? 500 : 0) + MODE_TOOLS[a.mode].length * 120
|
|
3179
|
+
},
|
|
3180
|
+
{
|
|
3181
|
+
name: "earlier in this conversation",
|
|
3182
|
+
detail: a.earlier.length ? `${a.earlier.length} ${a.earlier.length === 1 ? "turn" : "turns"}` : "",
|
|
3183
|
+
tokens: estimate(earlier)
|
|
3184
|
+
},
|
|
3185
|
+
{
|
|
3186
|
+
name: "what you gave",
|
|
3187
|
+
detail: given.length ? `${given.length} ${given.length === 1 ? "part" : "parts"}` : "nothing",
|
|
3188
|
+
tokens: estimate(gave)
|
|
3189
|
+
},
|
|
3190
|
+
...a.mode === "understand" ? [{
|
|
3191
|
+
name: "your understanding",
|
|
3192
|
+
detail: `${understood.length} ${understood.length === 1 ? "note" : "notes"}`,
|
|
3193
|
+
tokens: estimate(known)
|
|
3194
|
+
}] : [],
|
|
3195
|
+
{
|
|
3196
|
+
name: "your message",
|
|
3197
|
+
detail: "",
|
|
3198
|
+
tokens: estimate(request)
|
|
3199
|
+
}
|
|
3200
|
+
]
|
|
3201
|
+
};
|
|
2948
3202
|
}
|
|
2949
3203
|
//#endregion
|
|
2950
3204
|
//#region packages/engine/src/meaning.ts
|
|
@@ -3140,9 +3394,9 @@ const DRAFT_PREFIX = "@draft/";
|
|
|
3140
3394
|
const DRAFT_DIR = "drafts";
|
|
3141
3395
|
const isDraftId = (id) => id.startsWith(DRAFT_PREFIX);
|
|
3142
3396
|
/** A note's id without the prefix that says which folder of the study base it is in. */
|
|
3143
|
-
const bare = (id) => isStudyId(id) ? id.slice(7) : isDraftId(id) ? id.slice(7) : id;
|
|
3397
|
+
const bare = (id) => isStudyId(id) ? id.slice(7) : isDraftId(id) ? id.slice(7) : isUnderstandingId(id) ? id.slice(15) : id;
|
|
3144
3398
|
/** In the knowledge base itself: neither an exercise nor a draft. */
|
|
3145
|
-
const inBase = (id) => !isStudyId(id) && !isDraftId(id);
|
|
3399
|
+
const inBase = (id) => !isStudyId(id) && !isDraftId(id) && !isUnderstandingId(id);
|
|
3146
3400
|
const BAD_PART = /^\.|[\0\\:*?"<>|]|^\s|\s$/;
|
|
3147
3401
|
/** A note's id, checked before it becomes a path: no "..", nothing hidden, no absolute path. */
|
|
3148
3402
|
function checkNoteId(id) {
|
|
@@ -3150,6 +3404,8 @@ function checkNoteId(id) {
|
|
|
3150
3404
|
if (!id || id.length > 400 || parts.some((p) => !p || BAD_PART.test(p))) throw new EngineError(`not a note's id: ${JSON.stringify(id)}`);
|
|
3151
3405
|
return id;
|
|
3152
3406
|
}
|
|
3407
|
+
/** A conversation's or turn's id: a ULID. */
|
|
3408
|
+
const TURN_ID = /^[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
3153
3409
|
const escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
3154
3410
|
async function walk(files, root) {
|
|
3155
3411
|
const out = [];
|
|
@@ -3195,12 +3451,12 @@ const NOTE_ARG = {
|
|
|
3195
3451
|
type: "string",
|
|
3196
3452
|
description: "The note's id or path in the knowledge base: physics/softening or /physics/softening.md."
|
|
3197
3453
|
};
|
|
3198
|
-
const
|
|
3199
|
-
const
|
|
3454
|
+
const MARKED = "The change is written at once, under your name: the person sees your words marked in the note, and can undo it from the browser. Use this, never edit or write, for any note of the knowledge base.";
|
|
3455
|
+
const EDIT_NOTE = {
|
|
3200
3456
|
type: "function",
|
|
3201
3457
|
function: {
|
|
3202
|
-
name: "
|
|
3203
|
-
description: `
|
|
3458
|
+
name: "edit_note",
|
|
3459
|
+
description: `Change a note of the knowledge base: oldString replaced by newString, as the edit tool does. ${MARKED}`,
|
|
3204
3460
|
parameters: {
|
|
3205
3461
|
type: "object",
|
|
3206
3462
|
properties: {
|
|
@@ -3224,11 +3480,11 @@ const PROPOSE_EDIT = {
|
|
|
3224
3480
|
}
|
|
3225
3481
|
}
|
|
3226
3482
|
};
|
|
3227
|
-
const
|
|
3483
|
+
const WRITE_NOTE = {
|
|
3228
3484
|
type: "function",
|
|
3229
3485
|
function: {
|
|
3230
|
-
name: "
|
|
3231
|
-
description: `
|
|
3486
|
+
name: "write_note",
|
|
3487
|
+
description: `Write a whole note: a new note of the knowledge base, at folder/name (lower case, hyphens), with its frontmatter (type, title), or one written anew. ${MARKED}`,
|
|
3232
3488
|
parameters: {
|
|
3233
3489
|
type: "object",
|
|
3234
3490
|
properties: {
|
|
@@ -3341,6 +3597,14 @@ var Engine = class Engine {
|
|
|
3341
3597
|
step: read.get(c.id)?.step ?? 0,
|
|
3342
3598
|
tokens: estimate(c.body.trim())
|
|
3343
3599
|
};
|
|
3600
|
+
for (const [id, u] of read) {
|
|
3601
|
+
if (!notes[id] || u.last === null || !study) continue;
|
|
3602
|
+
const changed = (await this.journal(id)).findLast((e) => e.kind === "change" && e.who !== "you" && e.why !== "move");
|
|
3603
|
+
if (changed && Date.parse(changed.at) > u.last) notes[id] = {
|
|
3604
|
+
...notes[id],
|
|
3605
|
+
changedSince: changed.at
|
|
3606
|
+
};
|
|
3607
|
+
}
|
|
3344
3608
|
for (const c of exercises?.concepts.values() ?? []) notes[studyId(c.id)] = {
|
|
3345
3609
|
title: c.title,
|
|
3346
3610
|
step: 0,
|
|
@@ -3371,23 +3635,23 @@ var Engine = class Engine {
|
|
|
3371
3635
|
TOOLS[0],
|
|
3372
3636
|
...this.similar ? [FIND_SIMILAR] : [],
|
|
3373
3637
|
...TOOLS.slice(1),
|
|
3374
|
-
|
|
3375
|
-
|
|
3638
|
+
EDIT_NOTE,
|
|
3639
|
+
WRITE_NOTE
|
|
3376
3640
|
];
|
|
3377
3641
|
}
|
|
3378
3642
|
async agentLook({ agent, session, tool, args, asked = "" }) {
|
|
3379
3643
|
if (tool === BRIEF.function.name) return { text: this.brief() };
|
|
3380
|
-
if (tool ===
|
|
3644
|
+
if (tool === EDIT_NOTE.function.name || tool === WRITE_NOTE.function.name) {
|
|
3381
3645
|
const str = (v, name) => {
|
|
3382
3646
|
if (typeof v !== "string") throw new EngineError(`${tool} needs ${name}, as text`, 400);
|
|
3383
3647
|
return v;
|
|
3384
3648
|
};
|
|
3385
|
-
const edit = tool ===
|
|
3649
|
+
const edit = tool === WRITE_NOTE.function.name ? { content: str(args.content, "content") } : {
|
|
3386
3650
|
oldString: str(args.oldString, "oldString"),
|
|
3387
3651
|
newString: str(args.newString, "newString"),
|
|
3388
3652
|
replaceAll: args.replaceAll === true
|
|
3389
3653
|
};
|
|
3390
|
-
return this.
|
|
3654
|
+
return this.agentWrite({
|
|
3391
3655
|
agent,
|
|
3392
3656
|
session,
|
|
3393
3657
|
note: str(args.note, "note"),
|
|
@@ -3419,44 +3683,43 @@ var Engine = class Engine {
|
|
|
3419
3683
|
});
|
|
3420
3684
|
}
|
|
3421
3685
|
}
|
|
3422
|
-
async
|
|
3423
|
-
|
|
3424
|
-
const
|
|
3425
|
-
const
|
|
3686
|
+
async agentWrite({ agent, session, note, asked, edit }) {
|
|
3687
|
+
const who = `agent:${agent}`;
|
|
3688
|
+
const raw = note.replace(/^\/+/, "").replace(/\.md$/i, "");
|
|
3689
|
+
const existing = noteId(this.bundle, raw) ?? await this.onDisk(raw);
|
|
3690
|
+
if (!existing) {
|
|
3691
|
+
if (!("content" in edit)) throw new EngineError(`there is no note ${raw}: to make one, write the whole of it`, 404);
|
|
3692
|
+
const id = this.newNoteId(raw);
|
|
3693
|
+
if (!id) throw new EngineError(`${raw} is not a note's id in this base: give folder/name, lower case with hyphens`, 400);
|
|
3694
|
+
let text = edit.content;
|
|
3695
|
+
if (!/^---\n/.test(text)) text = `---\ntype: Concept\ntitle: ${JSON.stringify(/^#\s+(.+)$/m.exec(text)?.[1]?.trim() ?? id.split("/").at(-1).replace(/-/g, " "))}\n---\n\n${text.replace(/^#\s+.+\n+/, "")}`;
|
|
3696
|
+
await this.one(() => this.put(id, text, null, "edit", { asked }, who));
|
|
3697
|
+
return { text: `Made ${this.titleOf(id, text)} (/${id}.md). The person sees it as yours, and can undo it.` };
|
|
3698
|
+
}
|
|
3699
|
+
const id = existing, now = await this.readNote(id);
|
|
3700
|
+
let text;
|
|
3701
|
+
if ("content" in edit) text = edit.content;
|
|
3702
|
+
else {
|
|
3703
|
+
const at = now.text.indexOf(edit.oldString);
|
|
3704
|
+
if (!edit.oldString || at < 0) throw new EngineError(`that passage is not in ${this.titleOf(id, now.text)} as you wrote it: read the note again and give the passage exactly`, 409);
|
|
3705
|
+
if (edit.replaceAll) text = now.text.split(edit.oldString).join(edit.newString);
|
|
3706
|
+
else {
|
|
3707
|
+
if (now.text.indexOf(edit.oldString, at + 1) >= 0) throw new EngineError(`that passage occurs more than once in ${this.titleOf(id, now.text)}: give more of it, or replaceAll`, 409);
|
|
3708
|
+
text = now.text.slice(0, at) + edit.newString + now.text.slice(at + edit.oldString.length);
|
|
3709
|
+
}
|
|
3710
|
+
}
|
|
3711
|
+
return { text: (await this.one(() => this.put(id, text, now.version, "edit", { asked }, who))).changed ? `Changed ${this.titleOf(id, text)}. The person sees your words marked, and can undo it.` : `That leaves ${this.titleOf(id, now.text)} as it is.` };
|
|
3712
|
+
}
|
|
3713
|
+
/** A note's id when its file is there, though the index may not have it yet; else null. */
|
|
3714
|
+
async onDisk(raw) {
|
|
3715
|
+
let id;
|
|
3716
|
+
try {
|
|
3717
|
+
id = checkNoteId(raw);
|
|
3718
|
+
} catch {
|
|
3719
|
+
return null;
|
|
3720
|
+
}
|
|
3426
3721
|
const { files, path } = this.where(id);
|
|
3427
|
-
|
|
3428
|
-
if (!("content" in edit)) throw new EngineError(`there is no note ${id}: to propose a new note, write the whole of it`, 404);
|
|
3429
|
-
let meta = null, body = edit.content;
|
|
3430
|
-
try {
|
|
3431
|
-
[meta, body] = splitFrontmatter(edit.content);
|
|
3432
|
-
} catch {}
|
|
3433
|
-
const title = typeof meta?.title === "string" && meta.title ? meta.title : id.split("/").at(-1);
|
|
3434
|
-
const draft = DRAFT_PREFIX + newId(this.opts.clock.now()).toLowerCase();
|
|
3435
|
-
await this.createNote(draft, `---\ntype: ${typeof meta?.type === "string" ? meta.type : "Concept"}\ntitle: ${JSON.stringify(title)}\n---\n`);
|
|
3436
|
-
await this.one(async () => this.enter(draft, await this.journal(draft), {
|
|
3437
|
-
...await this.stamp(who),
|
|
3438
|
-
kind: "proposal",
|
|
3439
|
-
form: "insert",
|
|
3440
|
-
after: null,
|
|
3441
|
-
before: null,
|
|
3442
|
-
text: body.replace(/^\n+/, ""),
|
|
3443
|
-
asked: asked || `a new note at ${id}`,
|
|
3444
|
-
read
|
|
3445
|
-
}));
|
|
3446
|
-
return { text: `The new note "${title}" waits for the person to review in the browser, as a note for review; they choose its folder when they keep it (you meant ${id.split("/").slice(0, -1).join("/") || "the top"}). Nothing was written to the knowledge base. Do not write it again.` };
|
|
3447
|
-
}
|
|
3448
|
-
const current = (await this.readNote(id)).text;
|
|
3449
|
-
const regions = regionsOf(current, edit);
|
|
3450
|
-
if (!regions.length) return { text: `That leaves ${this.titleOf(id, current)} as it is: nothing was proposed.` };
|
|
3451
|
-
await this.one(async () => this.enter(id, await this.journal(id), {
|
|
3452
|
-
...await this.stamp(who),
|
|
3453
|
-
kind: "proposal",
|
|
3454
|
-
form: "rewrite",
|
|
3455
|
-
regions,
|
|
3456
|
-
asked: asked || "an edit",
|
|
3457
|
-
read
|
|
3458
|
-
}));
|
|
3459
|
-
return { text: `Your edit to ${this.titleOf(id, current)} (${regions.length} ${regions.length === 1 ? "passage" : "passages"}) waits for the person to keep it, in the browser or the terminal's note view. The file is unchanged until they do. Do not write it again; go on as if it may not be kept.` };
|
|
3722
|
+
return await files.read(path) === null ? null : id;
|
|
3460
3723
|
}
|
|
3461
3724
|
async agentSend({ note, passage, said }) {
|
|
3462
3725
|
const id = checkNoteId(note), concept = this.bundle.concepts.get(id);
|
|
@@ -3521,7 +3784,8 @@ var Engine = class Engine {
|
|
|
3521
3784
|
createNote(id, text) {
|
|
3522
3785
|
return this.one(() => this.put(id, text, null));
|
|
3523
3786
|
}
|
|
3524
|
-
|
|
3787
|
+
/** `who` is you unless Iso moves it, in Write mode. */
|
|
3788
|
+
async moveNote(id, to, base, who = "you") {
|
|
3525
3789
|
to = to.trim().replace(/^\/+|\/+$/g, "").replace(/\.md$/, "");
|
|
3526
3790
|
if (!inBase(id) || !inBase(to)) throw new EngineError("only a note of the base is moved, and only within it");
|
|
3527
3791
|
checkNoteId(to);
|
|
@@ -3549,7 +3813,7 @@ var Engine = class Engine {
|
|
|
3549
3813
|
}
|
|
3550
3814
|
const hash = hashText(now);
|
|
3551
3815
|
await this.enter(to, entries, {
|
|
3552
|
-
...await this.stamp(
|
|
3816
|
+
...await this.stamp(who),
|
|
3553
3817
|
kind: "change",
|
|
3554
3818
|
why: "move",
|
|
3555
3819
|
before: hash,
|
|
@@ -3574,13 +3838,13 @@ var Engine = class Engine {
|
|
|
3574
3838
|
type: "note-changed",
|
|
3575
3839
|
id,
|
|
3576
3840
|
version: null,
|
|
3577
|
-
who
|
|
3841
|
+
who
|
|
3578
3842
|
});
|
|
3579
3843
|
this.publish({
|
|
3580
3844
|
type: "note-changed",
|
|
3581
3845
|
id: to,
|
|
3582
3846
|
version: hashText(now),
|
|
3583
|
-
who
|
|
3847
|
+
who
|
|
3584
3848
|
});
|
|
3585
3849
|
return now;
|
|
3586
3850
|
});
|
|
@@ -3588,10 +3852,7 @@ var Engine = class Engine {
|
|
|
3588
3852
|
for (const c of linking) {
|
|
3589
3853
|
const source = await this.readNote(c);
|
|
3590
3854
|
const changed = source.text.replace(old, `](/${to}.md`);
|
|
3591
|
-
if (changed !== source.text) await this.
|
|
3592
|
-
base: source.version,
|
|
3593
|
-
text: changed
|
|
3594
|
-
});
|
|
3855
|
+
if (changed !== source.text) await this.one(() => this.put(c, changed, source.version, "edit", {}, who));
|
|
3595
3856
|
}
|
|
3596
3857
|
if (study) for (const ex of Bundle.fromFiles(await walk(study, STUDY_DIR)).concepts.values()) {
|
|
3597
3858
|
if (!testsOf(ex.meta).includes(id)) continue;
|
|
@@ -3609,7 +3870,7 @@ var Engine = class Engine {
|
|
|
3609
3870
|
changed: true
|
|
3610
3871
|
};
|
|
3611
3872
|
}
|
|
3612
|
-
async deleteNote(id, base) {
|
|
3873
|
+
async deleteNote(id, base, who = "you") {
|
|
3613
3874
|
if (!inBase(id)) throw new EngineError("only a note of the base is deleted here");
|
|
3614
3875
|
const notes = this.opts.notes, path = `${checkNoteId(id)}.md`;
|
|
3615
3876
|
await this.one(async () => {
|
|
@@ -3631,7 +3892,7 @@ var Engine = class Engine {
|
|
|
3631
3892
|
type: "note-changed",
|
|
3632
3893
|
id,
|
|
3633
3894
|
version: null,
|
|
3634
|
-
who
|
|
3895
|
+
who
|
|
3635
3896
|
});
|
|
3636
3897
|
}
|
|
3637
3898
|
/** Writes happen one at a time, in the order asked. */
|
|
@@ -3646,7 +3907,7 @@ var Engine = class Engine {
|
|
|
3646
3907
|
if (!this.opts.study) throw new EngineError("there is no study base open", 404);
|
|
3647
3908
|
return {
|
|
3648
3909
|
files: this.opts.study,
|
|
3649
|
-
path: `${isDraftId(id) ? DRAFT_DIR : STUDY_DIR}/${checkNoteId(bare(id))}.md`
|
|
3910
|
+
path: `${isDraftId(id) ? DRAFT_DIR : isUnderstandingId(id) ? UNDERSTANDING_DIR : STUDY_DIR}/${checkNoteId(bare(id))}.md`
|
|
3650
3911
|
};
|
|
3651
3912
|
}
|
|
3652
3913
|
return {
|
|
@@ -3666,7 +3927,7 @@ var Engine = class Engine {
|
|
|
3666
3927
|
restore(id, entry, base) {
|
|
3667
3928
|
return this.one(async () => this.put(id, await this.version(id, entry), base, "restore", { to: entry }));
|
|
3668
3929
|
}
|
|
3669
|
-
async put(id, text, base, why = "edit", extra = {}) {
|
|
3930
|
+
async put(id, text, base, why = "edit", extra = {}, who = "you") {
|
|
3670
3931
|
const { files, path } = this.where(id);
|
|
3671
3932
|
if (text.length > 2e6) throw new EngineError("note too large", 413);
|
|
3672
3933
|
const now = await files.read(path);
|
|
@@ -3686,7 +3947,8 @@ var Engine = class Engine {
|
|
|
3686
3947
|
changed: false
|
|
3687
3948
|
};
|
|
3688
3949
|
await files.write(path, text);
|
|
3689
|
-
|
|
3950
|
+
const entry = change(entries, text, await this.stamp(who), why, extra);
|
|
3951
|
+
await this.enter(id, entries, entry);
|
|
3690
3952
|
if (inBase(id)) {
|
|
3691
3953
|
this.files.set(path, {
|
|
3692
3954
|
path,
|
|
@@ -3700,13 +3962,14 @@ var Engine = class Engine {
|
|
|
3700
3962
|
type: "note-changed",
|
|
3701
3963
|
id,
|
|
3702
3964
|
version,
|
|
3703
|
-
who
|
|
3965
|
+
who
|
|
3704
3966
|
});
|
|
3705
3967
|
return {
|
|
3706
3968
|
id,
|
|
3707
3969
|
text,
|
|
3708
3970
|
version,
|
|
3709
|
-
changed: true
|
|
3971
|
+
changed: true,
|
|
3972
|
+
...entry ? { entry: entry.id } : {}
|
|
3710
3973
|
};
|
|
3711
3974
|
}
|
|
3712
3975
|
get provider() {
|
|
@@ -3808,7 +4071,7 @@ var Engine = class Engine {
|
|
|
3808
4071
|
if (!key && /\/\/(?:[^/]*\.)?openrouter\.ai\b/.test(this.provider.url)) throw new EngineError("There is no provider key yet. Set one in settings, behind the logo.", 409);
|
|
3809
4072
|
const { messages, olds, text: sent } = await this.compose(req, asked);
|
|
3810
4073
|
const maxTokens = req.form === "insert" ? REPLY_TOKENS : req.form === "note" ? NOTE_TOKENS : req.form === "mark" ? MARK_TOKENS : req.form === "exercise" ? EXERCISE_TOKENS : Math.max(REPLY_TOKENS, Math.ceil(olds.join("").length / 3) + 1e3);
|
|
3811
|
-
const most = req.form === "mark" || req.form === "exercise" ? 0 : void 0;
|
|
4074
|
+
const most = req.form === "mark" || req.form === "exercise" || (req.form === "insert" || req.form === "rewrite") && req.choice?.lookup === false ? 0 : void 0;
|
|
3812
4075
|
const request = newId(this.opts.clock.now()), abort = new AbortController();
|
|
3813
4076
|
this.running.set(request, abort);
|
|
3814
4077
|
this.asking.set(req.note, request);
|
|
@@ -4042,20 +4305,7 @@ var Engine = class Engine {
|
|
|
4042
4305
|
body = splitFrontmatter(text)[1];
|
|
4043
4306
|
} catch {}
|
|
4044
4307
|
if (!body.trim()) throw new EngineError("write what you want from Iso first");
|
|
4045
|
-
const linked =
|
|
4046
|
-
let room = LINKED_BUDGET;
|
|
4047
|
-
for (const m of body.matchAll(/\]\(\/([^)\s#]+)\.md(?:#[^)\s]*)?(?:\s+"[^"]*")?\)/g)) {
|
|
4048
|
-
const c = this.bundle.concepts.get(decodeURIComponent(m[1]));
|
|
4049
|
-
if (!c || linked.some((l) => l.id === c.id)) continue;
|
|
4050
|
-
const piece = c.body.trim().slice(0, Math.max(0, room) * 4);
|
|
4051
|
-
if (piece.length < Math.min(c.body.trim().length, 400)) break;
|
|
4052
|
-
linked.push({
|
|
4053
|
-
id: c.id,
|
|
4054
|
-
title: c.title,
|
|
4055
|
-
text: piece + (piece.length < c.body.trim().length ? "\n[…cut: read the rest with read_note]" : "")
|
|
4056
|
-
});
|
|
4057
|
-
room -= estimate(piece);
|
|
4058
|
-
}
|
|
4308
|
+
const linked = this.linkedSent(body);
|
|
4059
4309
|
const earlier = await this.exchangesNow();
|
|
4060
4310
|
return {
|
|
4061
4311
|
...noteMessages({
|
|
@@ -4105,8 +4355,12 @@ var Engine = class Engine {
|
|
|
4105
4355
|
}
|
|
4106
4356
|
const title = this.titleOf(req.note, text);
|
|
4107
4357
|
const entries = await this.journal(req.note);
|
|
4108
|
-
const
|
|
4109
|
-
const
|
|
4358
|
+
const off = new Set(req.choice?.off ?? []);
|
|
4359
|
+
const earlier = (await this.exchangesNow()).filter((x) => !off.has(`x:${x.id}`));
|
|
4360
|
+
let body = text;
|
|
4361
|
+
try {
|
|
4362
|
+
body = splitFrontmatter(text)[1];
|
|
4363
|
+
} catch {}
|
|
4110
4364
|
const given = {
|
|
4111
4365
|
tier: req.tier,
|
|
4112
4366
|
meaning: Boolean(this.similar),
|
|
@@ -4117,10 +4371,8 @@ var Engine = class Engine {
|
|
|
4117
4371
|
unkept: pending(entries).map((p) => p.form === "insert" ? p.text : p.regions.map((r) => r.new).join("\n\n")),
|
|
4118
4372
|
conversation: conversation(earlier),
|
|
4119
4373
|
moments: earlier.length,
|
|
4120
|
-
linked:
|
|
4121
|
-
|
|
4122
|
-
tokens: [...read].reduce((n, id) => n + estimate(this.bundle.concepts.get(id)?.body ?? ""), 0)
|
|
4123
|
-
}
|
|
4374
|
+
linked: this.linkedSent(body, off, req.note),
|
|
4375
|
+
lookup: req.choice?.lookup !== false
|
|
4124
4376
|
};
|
|
4125
4377
|
return {
|
|
4126
4378
|
...req.form === "insert" ? insertMessages(given) : rewriteMessages({
|
|
@@ -4131,6 +4383,37 @@ var Engine = class Engine {
|
|
|
4131
4383
|
olds
|
|
4132
4384
|
};
|
|
4133
4385
|
}
|
|
4386
|
+
/** The notes a note's text links to, each once, in order: the note itself left out. */
|
|
4387
|
+
linksOf(body, self = "") {
|
|
4388
|
+
const out = [];
|
|
4389
|
+
for (const m of body.matchAll(/\]\(\/([^)\s#]+)\.md(?:#[^)\s]*)?(?:\s+"[^"]*")?\)/g)) {
|
|
4390
|
+
const c = this.bundle.concepts.get(decodeURIComponent(m[1]));
|
|
4391
|
+
if (c && c.id !== self && !out.some((l) => l.id === c.id)) out.push({
|
|
4392
|
+
id: c.id,
|
|
4393
|
+
title: c.title,
|
|
4394
|
+
body: c.body.trim()
|
|
4395
|
+
});
|
|
4396
|
+
}
|
|
4397
|
+
return out;
|
|
4398
|
+
}
|
|
4399
|
+
/** The linked notes sent with a request, those switched off left out, as far as there is room; the rest Iso
|
|
4400
|
+
* reads with its tools. */
|
|
4401
|
+
linkedSent(body, off = /* @__PURE__ */ new Set(), self = "") {
|
|
4402
|
+
const linked = [];
|
|
4403
|
+
let room = LINKED_BUDGET;
|
|
4404
|
+
for (const c of this.linksOf(body, self)) {
|
|
4405
|
+
if (off.has(`n:${c.id}`)) continue;
|
|
4406
|
+
const piece = c.body.slice(0, Math.max(0, room) * 4);
|
|
4407
|
+
if (piece.length < Math.min(c.body.length, 400)) break;
|
|
4408
|
+
linked.push({
|
|
4409
|
+
id: c.id,
|
|
4410
|
+
title: c.title,
|
|
4411
|
+
text: piece + (piece.length < c.body.length ? "\n[…cut: read the rest with read_note]" : "")
|
|
4412
|
+
});
|
|
4413
|
+
room -= estimate(piece);
|
|
4414
|
+
}
|
|
4415
|
+
return linked;
|
|
4416
|
+
}
|
|
4134
4417
|
titleOf(id, text) {
|
|
4135
4418
|
try {
|
|
4136
4419
|
const t = splitFrontmatter(text)[0]?.title;
|
|
@@ -4208,22 +4491,43 @@ var Engine = class Engine {
|
|
|
4208
4491
|
if (!this.opts.study) return;
|
|
4209
4492
|
this.cleared = await this.one(() => this.record({ event: "context-cleared" }));
|
|
4210
4493
|
}
|
|
4211
|
-
async context(note) {
|
|
4212
|
-
let parts = null;
|
|
4494
|
+
async context(note, choice) {
|
|
4495
|
+
let parts = null, items = [];
|
|
4213
4496
|
if (note && isDraftId(note)) parts = await this.exerciseDraft(note).catch(() => null) ? await this.readNote(note).then(async (s) => (await this.exerciseCompose(note, s.text, await this.exerciseAsked(note))).parts, () => null) : await this.compose({
|
|
4214
4497
|
note,
|
|
4215
4498
|
form: "note",
|
|
4216
4499
|
tier: "mid"
|
|
4217
4500
|
}, "").then((c) => c.parts, () => null);
|
|
4218
4501
|
else if (note && isStudyId(note)) parts = await this.readNote(note).then(async (s) => (await this.markCompose(note, s.text, null)).parts, () => null);
|
|
4219
|
-
else if (note)
|
|
4220
|
-
|
|
4221
|
-
|
|
4222
|
-
|
|
4223
|
-
|
|
4224
|
-
|
|
4225
|
-
|
|
4226
|
-
|
|
4502
|
+
else if (note) {
|
|
4503
|
+
({parts} = await this.compose({
|
|
4504
|
+
note,
|
|
4505
|
+
form: "insert",
|
|
4506
|
+
after: null,
|
|
4507
|
+
before: null,
|
|
4508
|
+
asked: "",
|
|
4509
|
+
tier: "mid",
|
|
4510
|
+
...choice ? { choice } : {}
|
|
4511
|
+
}, ""));
|
|
4512
|
+
const off = new Set(choice?.off ?? []);
|
|
4513
|
+
let body = (await this.readNote(note)).text;
|
|
4514
|
+
try {
|
|
4515
|
+
body = splitFrontmatter(body)[1];
|
|
4516
|
+
} catch {}
|
|
4517
|
+
items = [...(await this.exchangesNow()).map((x) => ({
|
|
4518
|
+
id: `x:${x.id}`,
|
|
4519
|
+
kind: "exchange",
|
|
4520
|
+
title: x.text.split("\n")[0].replace(/^In (.+?) \(\/[^)]*\) they asked[^:]*: /, "$1: "),
|
|
4521
|
+
tokens: estimate(x.text),
|
|
4522
|
+
on: !off.has(`x:${x.id}`)
|
|
4523
|
+
})), ...this.linksOf(body, note).map((c) => ({
|
|
4524
|
+
id: `n:${c.id}`,
|
|
4525
|
+
kind: "linked",
|
|
4526
|
+
title: c.title,
|
|
4527
|
+
tokens: estimate(c.body),
|
|
4528
|
+
on: !off.has(`n:${c.id}`)
|
|
4529
|
+
}))];
|
|
4530
|
+
}
|
|
4227
4531
|
if (!parts) {
|
|
4228
4532
|
const earlier = await this.exchangesNow();
|
|
4229
4533
|
({parts} = insertMessages({
|
|
@@ -4243,6 +4547,18 @@ var Engine = class Engine {
|
|
|
4243
4547
|
tokens: 0
|
|
4244
4548
|
} : p);
|
|
4245
4549
|
}
|
|
4550
|
+
return {
|
|
4551
|
+
tokens: parts.reduce((n, p) => n + p.tokens, 0),
|
|
4552
|
+
limit: this.provider.context ?? 2e5,
|
|
4553
|
+
parts,
|
|
4554
|
+
session: await this.sessionCost(),
|
|
4555
|
+
items,
|
|
4556
|
+
lookup: choice?.lookup !== false
|
|
4557
|
+
};
|
|
4558
|
+
}
|
|
4559
|
+
/** What the session has cost: every reply offered, kept or not, every turn in Iso's pane, and every reply that came
|
|
4560
|
+
* to nothing. */
|
|
4561
|
+
async sessionCost() {
|
|
4246
4562
|
const session = await this.sessionNow();
|
|
4247
4563
|
let usd = 0, requests = 0;
|
|
4248
4564
|
for (const entries of (await this.sessionEntries()).values()) for (const e of entries) if ((e.kind === "proposal" && !e.copyOf || e.kind === "marks") && e.cost) {
|
|
@@ -4250,20 +4566,683 @@ var Engine = class Engine {
|
|
|
4250
4566
|
requests++;
|
|
4251
4567
|
}
|
|
4252
4568
|
const text = this.opts.study ? await this.opts.study.read(recordPath(this.opts.device)) : null;
|
|
4253
|
-
for (const e of readRecord(text ?? "")) if (e.event === "iso-unused" && e.session === session) {
|
|
4569
|
+
for (const e of readRecord(text ?? "")) if ((e.event === "iso-unused" || e.event === "iso-turn") && e.session === session) {
|
|
4254
4570
|
usd += typeof e.usd === "number" ? e.usd : 0;
|
|
4255
4571
|
requests++;
|
|
4256
4572
|
}
|
|
4257
4573
|
return {
|
|
4258
|
-
|
|
4259
|
-
|
|
4260
|
-
parts,
|
|
4261
|
-
session: {
|
|
4262
|
-
usd,
|
|
4263
|
-
requests
|
|
4264
|
-
}
|
|
4574
|
+
usd,
|
|
4575
|
+
requests
|
|
4265
4576
|
};
|
|
4266
4577
|
}
|
|
4578
|
+
/** The turn running in each conversation: one at a time. */
|
|
4579
|
+
conversing = /* @__PURE__ */ new Map();
|
|
4580
|
+
/** What Iso asked before doing something, waiting for your answer, by the permission's id. */
|
|
4581
|
+
waitingAnswer = /* @__PURE__ */ new Map();
|
|
4582
|
+
async conversation(id) {
|
|
4583
|
+
const study = this.opts.study;
|
|
4584
|
+
if (!study) return null;
|
|
4585
|
+
if (!id) {
|
|
4586
|
+
id = (await study.list(CONVERSATIONS_DIR).catch(() => [])).filter((f) => !f.dir && f.name.endsWith(".jsonl")).map((f) => f.name.slice(0, -6)).sort().at(-1);
|
|
4587
|
+
if (!id) return null;
|
|
4588
|
+
}
|
|
4589
|
+
if (!TURN_ID.test(id)) throw new EngineError("not a conversation's id");
|
|
4590
|
+
const text = await study.read(conversationPath(id));
|
|
4591
|
+
return text === null ? null : {
|
|
4592
|
+
id,
|
|
4593
|
+
turns: readTurns(text).map((t) => this.running.has(t.id) ? {
|
|
4594
|
+
...t,
|
|
4595
|
+
state: "running"
|
|
4596
|
+
} : t)
|
|
4597
|
+
};
|
|
4598
|
+
}
|
|
4599
|
+
/** What was given, checked: notes and passages of the base, and its folders. */
|
|
4600
|
+
given(raw) {
|
|
4601
|
+
if (!Array.isArray(raw) || raw.length > 40) throw new EngineError("give Iso between none and forty notes, folders and passages");
|
|
4602
|
+
return raw.map((g) => {
|
|
4603
|
+
const id = typeof g?.id === "string" ? g.id.replace(/^\/+|\/+$/g, "") : "";
|
|
4604
|
+
if (g?.kind === "folder") {
|
|
4605
|
+
if (id) checkNoteId(id);
|
|
4606
|
+
return {
|
|
4607
|
+
kind: "folder",
|
|
4608
|
+
id
|
|
4609
|
+
};
|
|
4610
|
+
}
|
|
4611
|
+
if (!inBase(checkNoteId(id))) throw new EngineError("only notes of the base are given to Iso");
|
|
4612
|
+
if (g?.kind === "note") return {
|
|
4613
|
+
kind: "note",
|
|
4614
|
+
id
|
|
4615
|
+
};
|
|
4616
|
+
if (g?.kind === "understanding") return {
|
|
4617
|
+
kind: "understanding",
|
|
4618
|
+
id
|
|
4619
|
+
};
|
|
4620
|
+
if (g?.kind === "passage" && typeof g.text === "string" && g.text.trim() && g.text.length <= 2e4) return {
|
|
4621
|
+
kind: "passage",
|
|
4622
|
+
id,
|
|
4623
|
+
text: g.text
|
|
4624
|
+
};
|
|
4625
|
+
throw new EngineError("a part given to Iso is a note, a folder or a passage");
|
|
4626
|
+
});
|
|
4627
|
+
}
|
|
4628
|
+
/** What was given, read now, as it is sent. A note that is gone is left out. */
|
|
4629
|
+
async sentOf(given) {
|
|
4630
|
+
const out = [];
|
|
4631
|
+
for (const g of given) {
|
|
4632
|
+
if (g.kind === "folder") {
|
|
4633
|
+
const under = g.id ? `${g.id}/` : "";
|
|
4634
|
+
out.push({
|
|
4635
|
+
kind: "folder",
|
|
4636
|
+
id: g.id,
|
|
4637
|
+
notes: [...this.bundle.concepts.values()].filter((c) => c.id.startsWith(under) && inBase(c.id)).map((c) => ({
|
|
4638
|
+
id: c.id,
|
|
4639
|
+
title: c.title,
|
|
4640
|
+
description: c.description
|
|
4641
|
+
}))
|
|
4642
|
+
});
|
|
4643
|
+
continue;
|
|
4644
|
+
}
|
|
4645
|
+
const c = this.bundle.concepts.get(g.id);
|
|
4646
|
+
if (!c) continue;
|
|
4647
|
+
if (g.kind === "understanding") {
|
|
4648
|
+
const u = await this.understanding(g.id);
|
|
4649
|
+
const said = (e) => `${e.at.slice(0, 10)}: ${e.by > 0 ? "+" : ""}${e.by}${e.event === "wind-back" ? " (they wound you back)" : e.event === "attempt" ? " (an exercise)" : ""}${e.why ? `, ${e.why}` : ""}`;
|
|
4650
|
+
out.push({
|
|
4651
|
+
kind: "understanding",
|
|
4652
|
+
id: g.id,
|
|
4653
|
+
title: c.title,
|
|
4654
|
+
reading: u.reading,
|
|
4655
|
+
events: u.evidence.slice(-8).reverse().map(said),
|
|
4656
|
+
squares: u.squares
|
|
4657
|
+
});
|
|
4658
|
+
continue;
|
|
4659
|
+
}
|
|
4660
|
+
if (g.kind === "passage") out.push({
|
|
4661
|
+
kind: "passage",
|
|
4662
|
+
id: g.id,
|
|
4663
|
+
title: c.title,
|
|
4664
|
+
text: g.text
|
|
4665
|
+
});
|
|
4666
|
+
else out.push({
|
|
4667
|
+
kind: "note",
|
|
4668
|
+
id: g.id,
|
|
4669
|
+
title: c.title,
|
|
4670
|
+
text: (await this.readNote(g.id)).text
|
|
4671
|
+
});
|
|
4672
|
+
}
|
|
4673
|
+
return out;
|
|
4674
|
+
}
|
|
4675
|
+
async turnContext(req) {
|
|
4676
|
+
const mode = req.mode === "build" ? "ask" : req.mode;
|
|
4677
|
+
const all = req.conversation ? (await this.conversation(req.conversation))?.turns ?? [] : [];
|
|
4678
|
+
const off = new Set(req.off ?? []), earlier = all.filter((t) => !off.has(t.id));
|
|
4679
|
+
const sent = await this.sentOf(this.given(req.given ?? []));
|
|
4680
|
+
const { parts } = turnMessages({
|
|
4681
|
+
mode,
|
|
4682
|
+
tier: req.tier,
|
|
4683
|
+
meaning: Boolean(this.similar),
|
|
4684
|
+
lookup: req.lookup !== false,
|
|
4685
|
+
earlier,
|
|
4686
|
+
sent,
|
|
4687
|
+
asked: ""
|
|
4688
|
+
});
|
|
4689
|
+
const items = all.map((t) => ({
|
|
4690
|
+
id: t.id,
|
|
4691
|
+
kind: "exchange",
|
|
4692
|
+
title: t.asked,
|
|
4693
|
+
tokens: estimate(turnExchange(t).text),
|
|
4694
|
+
on: !off.has(t.id)
|
|
4695
|
+
}));
|
|
4696
|
+
return {
|
|
4697
|
+
tokens: parts.reduce((n, p) => n + p.tokens, 0),
|
|
4698
|
+
limit: this.provider.context ?? 2e5,
|
|
4699
|
+
parts,
|
|
4700
|
+
session: await this.sessionCost(),
|
|
4701
|
+
items,
|
|
4702
|
+
lookup: req.lookup !== false,
|
|
4703
|
+
given: sent.map((g) => ({
|
|
4704
|
+
kind: g.kind,
|
|
4705
|
+
id: g.id,
|
|
4706
|
+
...g.kind === "passage" ? { text: g.text } : {},
|
|
4707
|
+
tokens: estimate(sentText(g))
|
|
4708
|
+
}))
|
|
4709
|
+
};
|
|
4710
|
+
}
|
|
4711
|
+
async converse(req) {
|
|
4712
|
+
const http = this.opts.http, study = this.opts.study;
|
|
4713
|
+
if (!http) throw new EngineError("Iso cannot be asked here", 409);
|
|
4714
|
+
if (!study) throw new EngineError("Iso's conversations are kept in a study base, and none is open (isorite study init)", 409);
|
|
4715
|
+
if (!MODES.includes(req.mode)) throw new EngineError("the mode is ask, write, build or understand");
|
|
4716
|
+
if (req.mode === "build") throw new EngineError("Build mode is the terminal's: start isorite term", 409);
|
|
4717
|
+
if (!TIERS.includes(req.tier)) throw new EngineError("the tier is low, mid or max");
|
|
4718
|
+
const asked = String(req.asked ?? "").trim();
|
|
4719
|
+
if (!asked) throw new EngineError("say what you want from Iso");
|
|
4720
|
+
if (asked.length > 4e3) throw new EngineError("that is too long to ask at once", 413);
|
|
4721
|
+
const given = this.given(req.given ?? []);
|
|
4722
|
+
if (req.conversation !== void 0 && !TURN_ID.test(req.conversation)) throw new EngineError("not a conversation's id");
|
|
4723
|
+
const conversation = req.conversation ?? newId(this.opts.clock.now());
|
|
4724
|
+
if (this.conversing.has(conversation)) throw new EngineError("Iso is still answering in this conversation. Wait for it, or stop it.", 409);
|
|
4725
|
+
this.conversing.set(conversation, "");
|
|
4726
|
+
try {
|
|
4727
|
+
return {
|
|
4728
|
+
conversation,
|
|
4729
|
+
turn: await this.beginTurn(conversation, {
|
|
4730
|
+
...req,
|
|
4731
|
+
mode: req.mode,
|
|
4732
|
+
asked,
|
|
4733
|
+
given
|
|
4734
|
+
}, http)
|
|
4735
|
+
};
|
|
4736
|
+
} catch (err) {
|
|
4737
|
+
this.conversing.delete(conversation);
|
|
4738
|
+
throw err;
|
|
4739
|
+
}
|
|
4740
|
+
}
|
|
4741
|
+
async beginTurn(conversation, req, http) {
|
|
4742
|
+
const study = this.opts.study;
|
|
4743
|
+
const key = await this.opts.secrets?.get("provider.key") ?? null;
|
|
4744
|
+
if (!key && /\/\/(?:[^/]*\.)?openrouter\.ai\b/.test(this.provider.url)) throw new EngineError("There is no provider key yet. Set one in settings, behind the logo.", 409);
|
|
4745
|
+
const off = new Set(req.off ?? []), earlier = ((await this.conversation(conversation))?.turns ?? []).filter((t) => !off.has(t.id));
|
|
4746
|
+
const lookupOn = req.lookup !== false, mode = req.mode;
|
|
4747
|
+
const { messages } = turnMessages({
|
|
4748
|
+
mode,
|
|
4749
|
+
tier: req.tier,
|
|
4750
|
+
meaning: Boolean(this.similar),
|
|
4751
|
+
lookup: lookupOn,
|
|
4752
|
+
earlier,
|
|
4753
|
+
sent: await this.sentOf(req.given),
|
|
4754
|
+
asked: req.asked
|
|
4755
|
+
});
|
|
4756
|
+
const s = await this.stamp("you"), turn = s.id, abort = new AbortController(), path = conversationPath(conversation);
|
|
4757
|
+
this.last = {
|
|
4758
|
+
at: s.at,
|
|
4759
|
+
session: s.session
|
|
4760
|
+
};
|
|
4761
|
+
const line = (l) => this.one(() => study.append(path, JSON.stringify(l) + "\n"));
|
|
4762
|
+
await line({
|
|
4763
|
+
kind: "turn",
|
|
4764
|
+
id: turn,
|
|
4765
|
+
conversation,
|
|
4766
|
+
at: s.at,
|
|
4767
|
+
session: s.session,
|
|
4768
|
+
mode,
|
|
4769
|
+
asked: req.asked,
|
|
4770
|
+
given: req.given,
|
|
4771
|
+
tier: req.tier
|
|
4772
|
+
});
|
|
4773
|
+
this.running.set(turn, abort);
|
|
4774
|
+
this.conversing.set(conversation, turn);
|
|
4775
|
+
this.publish({
|
|
4776
|
+
type: "iso-start",
|
|
4777
|
+
request: turn,
|
|
4778
|
+
note: "",
|
|
4779
|
+
asked: req.asked,
|
|
4780
|
+
tier: req.tier,
|
|
4781
|
+
place: {
|
|
4782
|
+
form: "turn",
|
|
4783
|
+
conversation,
|
|
4784
|
+
mode,
|
|
4785
|
+
given: req.given
|
|
4786
|
+
}
|
|
4787
|
+
});
|
|
4788
|
+
const item = async (i) => {
|
|
4789
|
+
await line({
|
|
4790
|
+
kind: "item",
|
|
4791
|
+
turn,
|
|
4792
|
+
item: i
|
|
4793
|
+
});
|
|
4794
|
+
this.publish({
|
|
4795
|
+
type: "iso-item",
|
|
4796
|
+
request: turn,
|
|
4797
|
+
conversation,
|
|
4798
|
+
item: i
|
|
4799
|
+
});
|
|
4800
|
+
};
|
|
4801
|
+
const who = `iso:${this.provider.name}/${this.provider.tiers[req.tier]}`;
|
|
4802
|
+
const lookup = new Lookup(this.bundle, req.given.find((g) => g.kind !== "folder")?.id ?? "", this.similar);
|
|
4803
|
+
const looks = new Set(lookup.defs().map((d) => d.function.name));
|
|
4804
|
+
const tools = {
|
|
4805
|
+
defs: () => [...lookupOn ? lookup.defs() : [], ...MODE_TOOLS[mode]],
|
|
4806
|
+
call: async (name, raw) => {
|
|
4807
|
+
if (looks.has(name)) return lookupOn ? lookup.call(name, raw) : { text: "Looking things up is off: they chose what you are sent." };
|
|
4808
|
+
let args = {};
|
|
4809
|
+
try {
|
|
4810
|
+
const parsed = raw.trim() ? JSON.parse(raw) : {};
|
|
4811
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) args = parsed;
|
|
4812
|
+
} catch {}
|
|
4813
|
+
if (!MODE_TOOLS[mode].some((d) => d.function.name === name)) return { text: `There is no tool ${name} in ${mode} mode.` };
|
|
4814
|
+
return { text: await this.turnTool(name, args, {
|
|
4815
|
+
turn,
|
|
4816
|
+
conversation,
|
|
4817
|
+
mode,
|
|
4818
|
+
item,
|
|
4819
|
+
signal: abort.signal,
|
|
4820
|
+
who,
|
|
4821
|
+
asked: req.asked
|
|
4822
|
+
}) };
|
|
4823
|
+
}
|
|
4824
|
+
};
|
|
4825
|
+
(async () => {
|
|
4826
|
+
try {
|
|
4827
|
+
const got = await rounds({
|
|
4828
|
+
http,
|
|
4829
|
+
provider: this.provider,
|
|
4830
|
+
key,
|
|
4831
|
+
tier: req.tier,
|
|
4832
|
+
messages,
|
|
4833
|
+
lookup: tools,
|
|
4834
|
+
maxTokens: REPLY_TOKENS * 2,
|
|
4835
|
+
signal: abort.signal,
|
|
4836
|
+
most: ROUNDS[req.tier],
|
|
4837
|
+
onText: (delta, again) => this.publish({
|
|
4838
|
+
type: "iso-text",
|
|
4839
|
+
request: turn,
|
|
4840
|
+
note: "",
|
|
4841
|
+
delta,
|
|
4842
|
+
again
|
|
4843
|
+
}),
|
|
4844
|
+
onStep: (step) => {
|
|
4845
|
+
item({
|
|
4846
|
+
kind: "look",
|
|
4847
|
+
step
|
|
4848
|
+
});
|
|
4849
|
+
this.publish({
|
|
4850
|
+
type: "iso-step",
|
|
4851
|
+
request: turn,
|
|
4852
|
+
note: "",
|
|
4853
|
+
step
|
|
4854
|
+
});
|
|
4855
|
+
}
|
|
4856
|
+
});
|
|
4857
|
+
if (abort.signal.aborted) throw Object.assign(/* @__PURE__ */ new Error("stopped"), { name: "AbortError" });
|
|
4858
|
+
const cost = {
|
|
4859
|
+
model: got.usage.model,
|
|
4860
|
+
tier: req.tier,
|
|
4861
|
+
tokensIn: got.usage.tokensIn,
|
|
4862
|
+
tokensOut: got.usage.tokensOut,
|
|
4863
|
+
tokensCached: got.usage.tokensCached,
|
|
4864
|
+
usd: got.usage.usd
|
|
4865
|
+
};
|
|
4866
|
+
const said = got.text.trim() + (got.cut ? "\n\n[Cut off at its length limit.]" : "");
|
|
4867
|
+
if (said) await item({
|
|
4868
|
+
kind: "text",
|
|
4869
|
+
text: said
|
|
4870
|
+
});
|
|
4871
|
+
await line({
|
|
4872
|
+
kind: "end",
|
|
4873
|
+
turn,
|
|
4874
|
+
state: "done",
|
|
4875
|
+
cost
|
|
4876
|
+
});
|
|
4877
|
+
if (cost.usd > 0) await this.one(async () => this.record({
|
|
4878
|
+
event: "iso-turn",
|
|
4879
|
+
conversation,
|
|
4880
|
+
turn,
|
|
4881
|
+
session: s.session,
|
|
4882
|
+
model: cost.model,
|
|
4883
|
+
usd: cost.usd
|
|
4884
|
+
}));
|
|
4885
|
+
this.publish({
|
|
4886
|
+
type: "iso-done",
|
|
4887
|
+
request: turn,
|
|
4888
|
+
note: "",
|
|
4889
|
+
proposal: "",
|
|
4890
|
+
cost
|
|
4891
|
+
});
|
|
4892
|
+
} catch (err) {
|
|
4893
|
+
const stopped = abort.signal.aborted || err.name === "AbortError";
|
|
4894
|
+
const message = err.message || "Iso could not answer";
|
|
4895
|
+
await line(stopped ? {
|
|
4896
|
+
kind: "end",
|
|
4897
|
+
turn,
|
|
4898
|
+
state: "stopped"
|
|
4899
|
+
} : {
|
|
4900
|
+
kind: "end",
|
|
4901
|
+
turn,
|
|
4902
|
+
state: "failed",
|
|
4903
|
+
message
|
|
4904
|
+
}).catch(() => void 0);
|
|
4905
|
+
if (stopped) this.publish({
|
|
4906
|
+
type: "iso-stopped",
|
|
4907
|
+
request: turn,
|
|
4908
|
+
note: ""
|
|
4909
|
+
});
|
|
4910
|
+
else this.publish({
|
|
4911
|
+
type: "iso-failed",
|
|
4912
|
+
request: turn,
|
|
4913
|
+
note: "",
|
|
4914
|
+
message
|
|
4915
|
+
});
|
|
4916
|
+
} finally {
|
|
4917
|
+
this.running.delete(turn);
|
|
4918
|
+
this.conversing.delete(conversation);
|
|
4919
|
+
for (const [id, w] of this.waitingAnswer) if (w.turn === turn) {
|
|
4920
|
+
this.waitingAnswer.delete(id);
|
|
4921
|
+
w.settle("reject");
|
|
4922
|
+
}
|
|
4923
|
+
}
|
|
4924
|
+
})();
|
|
4925
|
+
return turn;
|
|
4926
|
+
}
|
|
4927
|
+
/** What you allowed for the rest of a conversation ("always"): by conversation, the tools. */
|
|
4928
|
+
allowed = /* @__PURE__ */ new Map();
|
|
4929
|
+
/** Ask you before doing something; wait for your answer. "always" holds for the rest of the conversation. */
|
|
4930
|
+
async permit(t, tool, note, what) {
|
|
4931
|
+
if (this.allowed.get(t.conversation)?.has(tool)) return "always";
|
|
4932
|
+
const id = newId(this.opts.clock.now());
|
|
4933
|
+
const answer = new Promise((settle) => this.waitingAnswer.set(id, {
|
|
4934
|
+
turn: t.turn,
|
|
4935
|
+
conversation: t.conversation,
|
|
4936
|
+
settle
|
|
4937
|
+
}));
|
|
4938
|
+
await t.item({
|
|
4939
|
+
kind: "permission",
|
|
4940
|
+
id,
|
|
4941
|
+
tool,
|
|
4942
|
+
note,
|
|
4943
|
+
what
|
|
4944
|
+
});
|
|
4945
|
+
const stop = new Promise((settle) => t.signal.addEventListener("abort", () => settle("reject"), { once: true }));
|
|
4946
|
+
const d = await Promise.race([answer, stop]);
|
|
4947
|
+
this.waitingAnswer.delete(id);
|
|
4948
|
+
if (d === "always") this.allowed.set(t.conversation, /* @__PURE__ */ new Set([...this.allowed.get(t.conversation) ?? [], tool]));
|
|
4949
|
+
return d;
|
|
4950
|
+
}
|
|
4951
|
+
/** A note's id as Iso writes one for a new note: folder/name, without a leading slash or `.md`. */
|
|
4952
|
+
newNoteId(raw) {
|
|
4953
|
+
const id = String(raw ?? "").trim().replace(/^\/+|\/+$/g, "").replace(/\.md$/i, "");
|
|
4954
|
+
try {
|
|
4955
|
+
checkNoteId(id);
|
|
4956
|
+
} catch {
|
|
4957
|
+
return null;
|
|
4958
|
+
}
|
|
4959
|
+
return inBase(id) ? id : null;
|
|
4960
|
+
}
|
|
4961
|
+
/** One of a mode's own tools, in a turn: what is said back to Iso. */
|
|
4962
|
+
async turnTool(name, args, t) {
|
|
4963
|
+
const title = (id) => this.bundle.concepts.get(id)?.title ?? id;
|
|
4964
|
+
if (name === "point_to_note") {
|
|
4965
|
+
const id = noteId(this.bundle, args.id);
|
|
4966
|
+
if (!id) return `There is no note ${String(args.id ?? "")}.`;
|
|
4967
|
+
await t.item({
|
|
4968
|
+
kind: "point",
|
|
4969
|
+
note: id,
|
|
4970
|
+
why: String(args.why ?? "").trim().slice(0, 300)
|
|
4971
|
+
});
|
|
4972
|
+
return `${title(id)} is marked on their Atlas.`;
|
|
4973
|
+
}
|
|
4974
|
+
const counted = (before, after) => {
|
|
4975
|
+
const hunks = diffLines(before.split("\n"), after.split("\n"));
|
|
4976
|
+
return {
|
|
4977
|
+
added: hunks.reduce((n, h) => n + h.ins.length, 0),
|
|
4978
|
+
removed: hunks.reduce((n, h) => n + h.del.length, 0)
|
|
4979
|
+
};
|
|
4980
|
+
};
|
|
4981
|
+
if (name === "edit_note") {
|
|
4982
|
+
const id = noteId(this.bundle, args.id);
|
|
4983
|
+
if (!id) return `There is no note ${String(args.id ?? "")}. Search for it by its words.`;
|
|
4984
|
+
const old = String(args.old ?? ""), next = String(args.new ?? "");
|
|
4985
|
+
if (!old) return "Give the passage as it is now, in old.";
|
|
4986
|
+
const now = await this.readNote(id);
|
|
4987
|
+
const at = now.text.indexOf(old), again = at < 0 ? -1 : now.text.indexOf(old, at + 1);
|
|
4988
|
+
if (at < 0) return `That passage is not in ${title(id)} as you wrote it. Read the note again and give the passage exactly.`;
|
|
4989
|
+
if (again >= 0) return `That passage occurs more than once in ${title(id)}. Give more of it, so it is found once.`;
|
|
4990
|
+
const text = now.text.slice(0, at) + next + now.text.slice(at + old.length);
|
|
4991
|
+
const saved = await this.one(() => this.put(id, text, now.version, "edit", { asked: t.asked }, t.who));
|
|
4992
|
+
if (saved.entry) await t.item({
|
|
4993
|
+
kind: "change",
|
|
4994
|
+
note: id,
|
|
4995
|
+
entry: saved.entry,
|
|
4996
|
+
why: "edit",
|
|
4997
|
+
title: title(id),
|
|
4998
|
+
...counted(now.text, text)
|
|
4999
|
+
});
|
|
5000
|
+
return `Changed ${title(id)}.`;
|
|
5001
|
+
}
|
|
5002
|
+
if (name === "write_note") {
|
|
5003
|
+
const id = this.newNoteId(args.id);
|
|
5004
|
+
if (!id) return `${String(args.id ?? "")} is not a note's id in their base: give folder/name, lower case with hyphens.`;
|
|
5005
|
+
let text = String(args.text ?? "");
|
|
5006
|
+
if (!text.trim()) return "Give the note's whole text.";
|
|
5007
|
+
if (!/^---\n/.test(text)) text = `---\ntype: Concept\ntitle: ${JSON.stringify(/^#\s+(.+)$/m.exec(text)?.[1]?.trim() ?? id.split("/").at(-1).replace(/-/g, " "))}\n---\n\n${text.replace(/^#\s+.+\n+/, "")}`;
|
|
5008
|
+
const was = this.bundle.concepts.has(id) ? await this.readNote(id) : null;
|
|
5009
|
+
const saved = await this.one(() => this.put(id, text, was?.version ?? null, "edit", { asked: t.asked }, t.who));
|
|
5010
|
+
if (saved.entry) await t.item({
|
|
5011
|
+
kind: "change",
|
|
5012
|
+
note: id,
|
|
5013
|
+
entry: saved.entry,
|
|
5014
|
+
why: was ? "edit" : "create",
|
|
5015
|
+
title: title(id),
|
|
5016
|
+
...counted(was?.text ?? "", text)
|
|
5017
|
+
});
|
|
5018
|
+
return was ? `Wrote ${title(id)} anew.` : `Made ${title(id)} (/${id}.md).`;
|
|
5019
|
+
}
|
|
5020
|
+
if (name === "move_note") {
|
|
5021
|
+
const id = noteId(this.bundle, args.id), to = this.newNoteId(args.to);
|
|
5022
|
+
if (!id) return `There is no note ${String(args.id ?? "")}.`;
|
|
5023
|
+
if (!to) return `${String(args.to ?? "")} is not a note's id in their base.`;
|
|
5024
|
+
if (await this.permit(t, "move_note", id, `move ${title(id)} to ${to}`) === "reject") return "They did not allow it. It was not moved.";
|
|
5025
|
+
const now = await this.readNote(id), before = title(id);
|
|
5026
|
+
try {
|
|
5027
|
+
await this.moveNote(id, to, now.version, t.who);
|
|
5028
|
+
} catch (e) {
|
|
5029
|
+
return `It could not be moved: ${e.message}`;
|
|
5030
|
+
}
|
|
5031
|
+
const entry = (await this.journal(to)).at(-1)?.id ?? "";
|
|
5032
|
+
await t.item({
|
|
5033
|
+
kind: "change",
|
|
5034
|
+
note: to,
|
|
5035
|
+
entry,
|
|
5036
|
+
why: "move",
|
|
5037
|
+
to,
|
|
5038
|
+
title: before,
|
|
5039
|
+
added: 0,
|
|
5040
|
+
removed: 0
|
|
5041
|
+
});
|
|
5042
|
+
return `Moved ${before} to /${to}.md.`;
|
|
5043
|
+
}
|
|
5044
|
+
if (name === "delete_note") {
|
|
5045
|
+
const id = noteId(this.bundle, args.id);
|
|
5046
|
+
if (!id) return `There is no note ${String(args.id ?? "")}.`;
|
|
5047
|
+
if (await this.permit(t, "delete_note", id, `delete ${title(id)}`) === "reject") return "They did not allow it. It was not deleted.";
|
|
5048
|
+
const now = await this.readNote(id), before = title(id);
|
|
5049
|
+
try {
|
|
5050
|
+
await this.deleteNote(id, now.version, t.who);
|
|
5051
|
+
} catch (e) {
|
|
5052
|
+
return `It could not be deleted: ${e.message}`;
|
|
5053
|
+
}
|
|
5054
|
+
await t.item({
|
|
5055
|
+
kind: "change",
|
|
5056
|
+
note: id,
|
|
5057
|
+
entry: (await this.journal(id)).at(-1)?.id ?? "",
|
|
5058
|
+
why: "delete",
|
|
5059
|
+
title: before,
|
|
5060
|
+
added: 0,
|
|
5061
|
+
removed: now.text.split("\n").length
|
|
5062
|
+
});
|
|
5063
|
+
return `Deleted ${before}. Its history stays.`;
|
|
5064
|
+
}
|
|
5065
|
+
if (name === "record_understanding") {
|
|
5066
|
+
const id = noteId(this.bundle, args.id);
|
|
5067
|
+
if (!id) return `There is no note ${String(args.id ?? "")}.`;
|
|
5068
|
+
const by = Number(args.by);
|
|
5069
|
+
if (![
|
|
5070
|
+
-1,
|
|
5071
|
+
-.5,
|
|
5072
|
+
0,
|
|
5073
|
+
.5,
|
|
5074
|
+
1
|
|
5075
|
+
].includes(by)) return "by is one of -1, -0.5, 0, 0.5, 1.";
|
|
5076
|
+
const why = String(args.why ?? "").trim().slice(0, 300);
|
|
5077
|
+
if (this.recorded.get(t.turn)?.has(id)) return `You recorded ${title(id)} once this turn already.`;
|
|
5078
|
+
this.recorded.set(t.turn, /* @__PURE__ */ new Set([...this.recorded.get(t.turn) ?? [], id]));
|
|
5079
|
+
const event = await this.one(() => this.record({
|
|
5080
|
+
event: "understood",
|
|
5081
|
+
note: id,
|
|
5082
|
+
by,
|
|
5083
|
+
why,
|
|
5084
|
+
conversation: t.conversation,
|
|
5085
|
+
turn: t.turn
|
|
5086
|
+
}));
|
|
5087
|
+
await t.item({
|
|
5088
|
+
kind: "understood",
|
|
5089
|
+
note: id,
|
|
5090
|
+
event,
|
|
5091
|
+
by,
|
|
5092
|
+
why
|
|
5093
|
+
});
|
|
5094
|
+
return `Recorded for ${title(id)}.`;
|
|
5095
|
+
}
|
|
5096
|
+
if (name === "write_understanding") {
|
|
5097
|
+
const id = noteId(this.bundle, args.id);
|
|
5098
|
+
if (!id) return `There is no note ${String(args.id ?? "")}.`;
|
|
5099
|
+
const reading = String(args.reading ?? "").trim();
|
|
5100
|
+
if (!reading) return "Give your reading.";
|
|
5101
|
+
const uid = understandingId(id), was = await this.readNote(uid).catch(() => null);
|
|
5102
|
+
const text = understandingText(id, title(id), {
|
|
5103
|
+
...parseUnderstanding(was?.text ?? null),
|
|
5104
|
+
reading
|
|
5105
|
+
});
|
|
5106
|
+
const saved = await this.one(() => this.put(uid, text, was?.version ?? null, "edit", {}, t.who));
|
|
5107
|
+
if (saved.entry) await t.item({
|
|
5108
|
+
kind: "change",
|
|
5109
|
+
note: uid,
|
|
5110
|
+
entry: saved.entry,
|
|
5111
|
+
why: was ? "edit" : "create",
|
|
5112
|
+
title: `Understanding · ${title(id)}`,
|
|
5113
|
+
added: 0,
|
|
5114
|
+
removed: 0
|
|
5115
|
+
});
|
|
5116
|
+
return `Your reading of ${title(id)} is written.`;
|
|
5117
|
+
}
|
|
5118
|
+
return `${name} cannot be used yet.`;
|
|
5119
|
+
}
|
|
5120
|
+
/** The notes each turn recorded understanding of: once a note a turn. */
|
|
5121
|
+
recorded = /* @__PURE__ */ new Map();
|
|
5122
|
+
/** A note's Understanding note (M10): Iso's reading, your scratchpad, and what the record says of it. */
|
|
5123
|
+
async understanding(note) {
|
|
5124
|
+
if (!inBase(checkNoteId(note))) throw new EngineError("only a note of the base has an Understanding note");
|
|
5125
|
+
const uid = understandingId(note);
|
|
5126
|
+
const source = this.opts.study ? await this.readNote(uid).catch(() => null) : null;
|
|
5127
|
+
const read = (await this.study()).notes[note];
|
|
5128
|
+
return {
|
|
5129
|
+
note,
|
|
5130
|
+
...parseUnderstanding(source?.text ?? null),
|
|
5131
|
+
version: source?.version ?? null,
|
|
5132
|
+
level: read?.level ?? 0,
|
|
5133
|
+
squares: read?.squares ?? 0,
|
|
5134
|
+
step: read?.step ?? 0,
|
|
5135
|
+
due: read?.due ?? false,
|
|
5136
|
+
evidence: read?.evidence ?? []
|
|
5137
|
+
};
|
|
5138
|
+
}
|
|
5139
|
+
/** Your scratchpad in a note's Understanding note: yours alone; Iso does not write it. `base` as for saveNote, or
|
|
5140
|
+
* null when there is no Understanding note yet. */
|
|
5141
|
+
async saveScratchpad(note, scratchpad, base) {
|
|
5142
|
+
if (!this.opts.study) throw new EngineError("there is no study base open", 409);
|
|
5143
|
+
if (!inBase(checkNoteId(note))) throw new EngineError("only a note of the base has an Understanding note");
|
|
5144
|
+
const uid = understandingId(note), was = await this.readNote(uid).catch(() => null);
|
|
5145
|
+
const title = this.bundle.concepts.get(note)?.title ?? note;
|
|
5146
|
+
await this.one(() => this.put(uid, understandingText(note, title, {
|
|
5147
|
+
...parseUnderstanding(was?.text ?? null),
|
|
5148
|
+
scratchpad
|
|
5149
|
+
}), base));
|
|
5150
|
+
return this.understanding(note);
|
|
5151
|
+
}
|
|
5152
|
+
async undoTurn(conversation, turn) {
|
|
5153
|
+
const t = (await this.conversation(conversation))?.turns.find((x) => x.id === turn);
|
|
5154
|
+
if (!t) throw new EngineError("there is no such turn", 404);
|
|
5155
|
+
if (t.state === "running") throw new EngineError("wait for Iso to finish, or stop it, first", 409);
|
|
5156
|
+
if (t.items.some((i) => i.kind === "undone")) throw new EngineError("that turn was undone already", 409);
|
|
5157
|
+
const changes = t.items.filter((i) => i.kind === "change");
|
|
5158
|
+
const notes = [], left = [];
|
|
5159
|
+
const done = /* @__PURE__ */ new Set();
|
|
5160
|
+
for (const ch of [...changes].reverse()) {
|
|
5161
|
+
const key = ch.why === "move" ? ch.to : ch.note;
|
|
5162
|
+
if (done.has(key)) continue;
|
|
5163
|
+
done.add(key);
|
|
5164
|
+
try {
|
|
5165
|
+
if (ch.why === "delete") {
|
|
5166
|
+
const was = replay(await this.journal(ch.note))?.text;
|
|
5167
|
+
if (was === void 0 || this.bundle.concepts.has(ch.note)) {
|
|
5168
|
+
left.push(ch.note);
|
|
5169
|
+
continue;
|
|
5170
|
+
}
|
|
5171
|
+
await this.createNote(ch.note, was);
|
|
5172
|
+
} else if (ch.why === "move") {
|
|
5173
|
+
const from = (await this.journal(ch.to)).find((e) => e.id === ch.entry);
|
|
5174
|
+
const back = from && "from" in from ? from.from : void 0;
|
|
5175
|
+
if (!back || !this.bundle.concepts.has(ch.to)) {
|
|
5176
|
+
left.push(ch.to);
|
|
5177
|
+
continue;
|
|
5178
|
+
}
|
|
5179
|
+
await this.moveNote(ch.to, back, (await this.readNote(ch.to)).version);
|
|
5180
|
+
} else {
|
|
5181
|
+
const entries = await this.journal(ch.note);
|
|
5182
|
+
const mine = changes.filter((x) => x.note === ch.note && x.why !== "move").map((x) => x.entry);
|
|
5183
|
+
const first = entries.findIndex((e) => mine.includes(e.id)), last = entries.findLastIndex((e) => e.kind === "change");
|
|
5184
|
+
if (first < 0 || !mine.includes(entries[last]?.id ?? "")) {
|
|
5185
|
+
left.push(ch.note);
|
|
5186
|
+
continue;
|
|
5187
|
+
}
|
|
5188
|
+
const now = await this.readNote(ch.note);
|
|
5189
|
+
if (hashText(now.text) !== hashText(replay(entries).text)) {
|
|
5190
|
+
left.push(ch.note);
|
|
5191
|
+
continue;
|
|
5192
|
+
}
|
|
5193
|
+
if (first === 0) await this.deleteNote(ch.note, now.version);
|
|
5194
|
+
else await this.restore(ch.note, entries.slice(0, first).findLast((e) => e.kind === "change").id, now.version);
|
|
5195
|
+
}
|
|
5196
|
+
notes.push(key);
|
|
5197
|
+
} catch {
|
|
5198
|
+
left.push(key);
|
|
5199
|
+
}
|
|
5200
|
+
}
|
|
5201
|
+
const item = {
|
|
5202
|
+
kind: "undone",
|
|
5203
|
+
notes,
|
|
5204
|
+
left
|
|
5205
|
+
};
|
|
5206
|
+
await this.one(() => this.opts.study.append(conversationPath(conversation), JSON.stringify({
|
|
5207
|
+
kind: "item",
|
|
5208
|
+
turn,
|
|
5209
|
+
item
|
|
5210
|
+
}) + "\n"));
|
|
5211
|
+
this.publish({
|
|
5212
|
+
type: "iso-item",
|
|
5213
|
+
request: turn,
|
|
5214
|
+
conversation,
|
|
5215
|
+
item
|
|
5216
|
+
});
|
|
5217
|
+
return {
|
|
5218
|
+
notes,
|
|
5219
|
+
left
|
|
5220
|
+
};
|
|
5221
|
+
}
|
|
5222
|
+
async answer(turn, permission, decision) {
|
|
5223
|
+
if (![
|
|
5224
|
+
"once",
|
|
5225
|
+
"always",
|
|
5226
|
+
"reject"
|
|
5227
|
+
].includes(decision)) throw new EngineError("the answer is once, always or reject");
|
|
5228
|
+
const w = this.waitingAnswer.get(permission);
|
|
5229
|
+
if (!w || w.turn !== turn) throw new EngineError("Iso is not waiting for that answer", 409);
|
|
5230
|
+
this.waitingAnswer.delete(permission);
|
|
5231
|
+
await this.one(() => this.opts.study.append(conversationPath(w.conversation), JSON.stringify({
|
|
5232
|
+
kind: "answer",
|
|
5233
|
+
turn,
|
|
5234
|
+
permission,
|
|
5235
|
+
decision
|
|
5236
|
+
}) + "\n"));
|
|
5237
|
+
this.publish({
|
|
5238
|
+
type: "iso-answer",
|
|
5239
|
+
request: turn,
|
|
5240
|
+
conversation: w.conversation,
|
|
5241
|
+
permission,
|
|
5242
|
+
decision
|
|
5243
|
+
});
|
|
5244
|
+
w.settle(decision);
|
|
5245
|
+
}
|
|
4267
5246
|
async drafts() {
|
|
4268
5247
|
const study = this.opts.study;
|
|
4269
5248
|
if (!study) return [];
|
|
@@ -4538,6 +5517,14 @@ var Engine = class Engine {
|
|
|
4538
5517
|
event: "wind-back",
|
|
4539
5518
|
by: -1
|
|
4540
5519
|
}];
|
|
5520
|
+
if (e.event === "understood") return [{
|
|
5521
|
+
id,
|
|
5522
|
+
at: String(e.at),
|
|
5523
|
+
event: "understood",
|
|
5524
|
+
by: typeof e.by === "number" ? e.by : 0,
|
|
5525
|
+
...typeof e.why === "string" && e.why ? { why: e.why } : {},
|
|
5526
|
+
...typeof e.conversation === "string" ? { conversation: e.conversation } : {}
|
|
5527
|
+
}];
|
|
4541
5528
|
const move = e.moves?.[note];
|
|
4542
5529
|
return [{
|
|
4543
5530
|
id,
|
|
@@ -4952,6 +5939,341 @@ function nodeEmbedder(dir) {
|
|
|
4952
5939
|
};
|
|
4953
5940
|
}
|
|
4954
5941
|
//#endregion
|
|
5942
|
+
//#region packages/server/src/terminal.ts
|
|
5943
|
+
const OUTPUT = 4e3;
|
|
5944
|
+
const LOOKUPS = /* @__PURE__ */ new Set([
|
|
5945
|
+
"search_notes",
|
|
5946
|
+
"find_similar",
|
|
5947
|
+
"outline_note",
|
|
5948
|
+
"read_note",
|
|
5949
|
+
"brief_base"
|
|
5950
|
+
]);
|
|
5951
|
+
/** isorite's tools that change a note: their line opens it, where the agent's words are marked. */
|
|
5952
|
+
const CHANGES = /* @__PURE__ */ new Set(["edit_note", "write_note"]);
|
|
5953
|
+
/** What a tool call acted on, in a line. */
|
|
5954
|
+
function titleOf(name, input = {}) {
|
|
5955
|
+
const s = (k) => typeof input[k] === "string" ? input[k] : "";
|
|
5956
|
+
if (name === "shell" || name === "bash") return s("command");
|
|
5957
|
+
if (LOOKUPS.has(name)) return s("query") || s("text") || s("id") + (s("section") ? ` § ${s("section")}` : "");
|
|
5958
|
+
if (CHANGES.has(name)) return s("note");
|
|
5959
|
+
return s("path") || s("filePath") || s("pattern") || s("url") || s("description") || "";
|
|
5960
|
+
}
|
|
5961
|
+
/** An edit's lines as a diff: what it took out, then what it put in. A write is all put in. */
|
|
5962
|
+
function diffOf(name, input) {
|
|
5963
|
+
const lines = (v) => typeof v === "string" ? v.replace(/\n$/, "").split("\n") : [];
|
|
5964
|
+
if ((name === "edit" || name === "edit_note") && typeof input.oldString === "string") return [...lines(input.oldString).map((l) => "-" + l), ...lines(input.newString).map((l) => "+" + l)].slice(0, 200);
|
|
5965
|
+
if ((name === "write" || name === "write_note") && typeof input.content === "string") return lines(input.content).map((l) => "+" + l).slice(0, 200);
|
|
5966
|
+
}
|
|
5967
|
+
/** The session's messages (oldest first) as the pane's turns. Text still arriving (`live`, by assistant message) is
|
|
5968
|
+
* added where the listed message does not have it yet; `ours` are the prompts asked from a page. */
|
|
5969
|
+
function toTurns(messages, { live = /* @__PURE__ */ new Map(), ours = /* @__PURE__ */ new Set(), busy = false } = {}) {
|
|
5970
|
+
const turns = [];
|
|
5971
|
+
for (const m of messages) {
|
|
5972
|
+
if (m.type === "user" && "text" in m) {
|
|
5973
|
+
turns.push({
|
|
5974
|
+
id: m.id,
|
|
5975
|
+
asked: m.text,
|
|
5976
|
+
from: ours.has(m.id) || ours.has(m.text) ? "here" : "terminal",
|
|
5977
|
+
items: [],
|
|
5978
|
+
state: "running"
|
|
5979
|
+
});
|
|
5980
|
+
continue;
|
|
5981
|
+
}
|
|
5982
|
+
const turn = turns.at(-1);
|
|
5983
|
+
if (!turn) continue;
|
|
5984
|
+
if (m.type === "idle" && "outcome" in m) {
|
|
5985
|
+
turn.state = m.outcome === "succeeded" ? "done" : /interrupt|cancel|abort/.test(m.outcome) ? "stopped" : "failed";
|
|
5986
|
+
continue;
|
|
5987
|
+
}
|
|
5988
|
+
if (m.type !== "assistant" || !("content" in m)) continue;
|
|
5989
|
+
let texts = 0;
|
|
5990
|
+
for (const c of m.content) if (c.type === "text" && "text" in c) {
|
|
5991
|
+
texts++;
|
|
5992
|
+
if (c.text) turn.items.push({
|
|
5993
|
+
kind: "text",
|
|
5994
|
+
text: c.text
|
|
5995
|
+
});
|
|
5996
|
+
} else if (c.type === "tool" && "state" in c) {
|
|
5997
|
+
const input = c.state.input ?? {};
|
|
5998
|
+
const output = (c.state.content ?? []).map((x) => x.text ?? "").join("").slice(0, OUTPUT);
|
|
5999
|
+
const status = c.state.status === "completed" ? "done" : /error|fail/.test(c.state.status) ? "failed" : "running";
|
|
6000
|
+
const item = {
|
|
6001
|
+
kind: "tool",
|
|
6002
|
+
id: c.id,
|
|
6003
|
+
name: c.name,
|
|
6004
|
+
title: titleOf(c.name, input),
|
|
6005
|
+
status,
|
|
6006
|
+
output
|
|
6007
|
+
};
|
|
6008
|
+
const named = LOOKUPS.has(c.name) ? input.id : CHANGES.has(c.name) ? input.note : void 0;
|
|
6009
|
+
if (typeof named === "string" && named) item.note = named.replace(/^\//, "").replace(/\.md$/, "");
|
|
6010
|
+
const diff = diffOf(c.name, input);
|
|
6011
|
+
if (diff) item.diff = diff;
|
|
6012
|
+
turn.items.push(item);
|
|
6013
|
+
}
|
|
6014
|
+
const streaming = live.get(m.id);
|
|
6015
|
+
if (streaming && !texts) turn.items.push({
|
|
6016
|
+
kind: "text",
|
|
6017
|
+
text: streaming
|
|
6018
|
+
});
|
|
6019
|
+
}
|
|
6020
|
+
const listed = new Set(messages.map((m) => m.id));
|
|
6021
|
+
const last = turns.at(-1);
|
|
6022
|
+
if (last && last.state === "running") {
|
|
6023
|
+
for (const [id, text] of live) if (!listed.has(id) && text) last.items.push({
|
|
6024
|
+
kind: "text",
|
|
6025
|
+
text
|
|
6026
|
+
});
|
|
6027
|
+
}
|
|
6028
|
+
if (!busy) {
|
|
6029
|
+
for (const t of turns) if (t.state === "running") t.state = "done";
|
|
6030
|
+
}
|
|
6031
|
+
return turns;
|
|
6032
|
+
}
|
|
6033
|
+
/** What is asked from a page, with what was given in the Iso pane's context: notes and folders by name, for the
|
|
6034
|
+
* agent to read with isorite's tools, and passages as they are. */
|
|
6035
|
+
function given(asked, parts, title) {
|
|
6036
|
+
const notes = parts.filter((p) => p.kind === "note" || p.kind === "understanding").map((p) => `- ${title(p.id)} (${p.id})`);
|
|
6037
|
+
const folders = parts.filter((p) => p.kind === "folder").map((p) => `- the folder ${p.id}`);
|
|
6038
|
+
const passages = parts.flatMap((p) => p.kind === "passage" ? [`From ${title(p.id)} (${p.id}):\n${p.text.split("\n").map((l) => "> " + l).join("\n")}`] : []);
|
|
6039
|
+
const out = [asked.trim()];
|
|
6040
|
+
if (notes.length || folders.length) out.push(`From my knowledge base (read them with read_note, or outline_note first):\n${[...notes, ...folders].join("\n")}`);
|
|
6041
|
+
if (passages.length) out.push(passages.join("\n\n"));
|
|
6042
|
+
return out.join("\n\n");
|
|
6043
|
+
}
|
|
6044
|
+
const EMPTY = {
|
|
6045
|
+
connected: false,
|
|
6046
|
+
session: null,
|
|
6047
|
+
title: "",
|
|
6048
|
+
busy: false,
|
|
6049
|
+
turns: [],
|
|
6050
|
+
asking: [],
|
|
6051
|
+
cost: 0,
|
|
6052
|
+
tokens: {
|
|
6053
|
+
input: 0,
|
|
6054
|
+
output: 0
|
|
6055
|
+
},
|
|
6056
|
+
model: ""
|
|
6057
|
+
};
|
|
6058
|
+
/** Events after which the session is read again; text arrives as deltas in between. */
|
|
6059
|
+
const REREAD = /^session\.(step\.ended|tool\.|inbox\.delivered|execution\.|created|renamed)|^permission\./;
|
|
6060
|
+
/** The terminal's session, followed. One terminal at a time: a second `isorite term` on the same server takes over. */
|
|
6061
|
+
var Terminal = class {
|
|
6062
|
+
#view = EMPTY;
|
|
6063
|
+
#client = null;
|
|
6064
|
+
#stop = null;
|
|
6065
|
+
#listeners = /* @__PURE__ */ new Set();
|
|
6066
|
+
#live = /* @__PURE__ */ new Map();
|
|
6067
|
+
#ours = /* @__PURE__ */ new Set();
|
|
6068
|
+
#asking = /* @__PURE__ */ new Map();
|
|
6069
|
+
#timer = null;
|
|
6070
|
+
#directory = "";
|
|
6071
|
+
view() {
|
|
6072
|
+
return this.#view;
|
|
6073
|
+
}
|
|
6074
|
+
listen(fn) {
|
|
6075
|
+
this.#listeners.add(fn);
|
|
6076
|
+
return () => this.#listeners.delete(fn);
|
|
6077
|
+
}
|
|
6078
|
+
#tell() {
|
|
6079
|
+
for (const fn of this.#listeners) fn(this.#view);
|
|
6080
|
+
}
|
|
6081
|
+
async attach({ url, password, directory }) {
|
|
6082
|
+
this.detach();
|
|
6083
|
+
const client = OpenCode.make({
|
|
6084
|
+
baseUrl: url,
|
|
6085
|
+
headers: { authorization: "Basic " + Buffer.from(`opencode:${password}`).toString("base64") }
|
|
6086
|
+
});
|
|
6087
|
+
await client.server.info();
|
|
6088
|
+
this.#client = client;
|
|
6089
|
+
this.#directory = directory;
|
|
6090
|
+
this.#view = {
|
|
6091
|
+
...EMPTY,
|
|
6092
|
+
connected: true
|
|
6093
|
+
};
|
|
6094
|
+
const stop = this.#stop = new AbortController();
|
|
6095
|
+
this.#follow(client, stop.signal);
|
|
6096
|
+
await this.#choose();
|
|
6097
|
+
await this.#read();
|
|
6098
|
+
}
|
|
6099
|
+
detach() {
|
|
6100
|
+
this.#stop?.abort();
|
|
6101
|
+
this.#stop = null;
|
|
6102
|
+
this.#client = null;
|
|
6103
|
+
this.#live.clear();
|
|
6104
|
+
this.#asking.clear();
|
|
6105
|
+
if (this.#timer) clearTimeout(this.#timer);
|
|
6106
|
+
if (this.#view.connected) {
|
|
6107
|
+
this.#view = EMPTY;
|
|
6108
|
+
this.#tell();
|
|
6109
|
+
}
|
|
6110
|
+
}
|
|
6111
|
+
/** Ask in the session the terminal shows, from a page. While it works, the prompt steers the turn under way. */
|
|
6112
|
+
async prompt(text) {
|
|
6113
|
+
const client = this.#need();
|
|
6114
|
+
if (!this.#view.session) await this.#choose(true);
|
|
6115
|
+
this.#ours.add(text);
|
|
6116
|
+
const sent = await client.session.prompt({
|
|
6117
|
+
sessionID: this.#view.session,
|
|
6118
|
+
text
|
|
6119
|
+
});
|
|
6120
|
+
if (sent?.id) this.#ours.add(sent.id);
|
|
6121
|
+
this.#soon();
|
|
6122
|
+
}
|
|
6123
|
+
async interrupt() {
|
|
6124
|
+
const client = this.#need();
|
|
6125
|
+
if (this.#view.session) await client.session.interrupt({ sessionID: this.#view.session });
|
|
6126
|
+
this.#soon();
|
|
6127
|
+
}
|
|
6128
|
+
/** Answer what the agent asked; only what this session is asking can be answered. */
|
|
6129
|
+
async reply(id, decision) {
|
|
6130
|
+
const client = this.#need();
|
|
6131
|
+
if (!this.#asking.has(id) || !this.#view.session) throw new Error("the terminal is not asking that");
|
|
6132
|
+
await client.permission.reply({
|
|
6133
|
+
sessionID: this.#view.session,
|
|
6134
|
+
requestID: id,
|
|
6135
|
+
decision
|
|
6136
|
+
});
|
|
6137
|
+
this.#asking.delete(id);
|
|
6138
|
+
this.#soon();
|
|
6139
|
+
}
|
|
6140
|
+
#need() {
|
|
6141
|
+
if (!this.#client) throw new Error("no terminal is attached: start one with isorite term");
|
|
6142
|
+
return this.#client;
|
|
6143
|
+
}
|
|
6144
|
+
/** The session to follow: the one the terminal last showed, else the latest. With `make`, one is made when none is. */
|
|
6145
|
+
async #choose(make = false) {
|
|
6146
|
+
const client = this.#client;
|
|
6147
|
+
if (!client) return;
|
|
6148
|
+
const sessions = [...(await client.session.list().catch(() => null))?.data ?? []].sort((a, b) => (b.time?.updated ?? 0) - (a.time?.updated ?? 0));
|
|
6149
|
+
if (sessions[0]) {
|
|
6150
|
+
this.#show(sessions[0].id);
|
|
6151
|
+
return;
|
|
6152
|
+
}
|
|
6153
|
+
if (make) {
|
|
6154
|
+
const made = await client.session.create({ location: { directory: this.#directory } });
|
|
6155
|
+
this.#show(made.id);
|
|
6156
|
+
}
|
|
6157
|
+
}
|
|
6158
|
+
#show(session) {
|
|
6159
|
+
if (this.#view.session === session) return;
|
|
6160
|
+
this.#live.clear();
|
|
6161
|
+
this.#asking.clear();
|
|
6162
|
+
this.#view = {
|
|
6163
|
+
...this.#view,
|
|
6164
|
+
session,
|
|
6165
|
+
turns: [],
|
|
6166
|
+
asking: []
|
|
6167
|
+
};
|
|
6168
|
+
this.#pending();
|
|
6169
|
+
}
|
|
6170
|
+
async #pending() {
|
|
6171
|
+
const client = this.#client, session = this.#view.session;
|
|
6172
|
+
if (!client || !session) return;
|
|
6173
|
+
const asked = await client.permission.list({ sessionID: session }).catch(() => []);
|
|
6174
|
+
for (const p of asked ?? []) this.#asking.set(p.id, {
|
|
6175
|
+
id: p.id,
|
|
6176
|
+
action: p.action,
|
|
6177
|
+
resources: p.resources ?? [],
|
|
6178
|
+
...p.source?.id ? { tool: p.source.id } : {}
|
|
6179
|
+
});
|
|
6180
|
+
this.#soon();
|
|
6181
|
+
}
|
|
6182
|
+
async #follow(client, signal) {
|
|
6183
|
+
try {
|
|
6184
|
+
for await (const raw of client.event.subscribe()) {
|
|
6185
|
+
if (signal.aborted) return;
|
|
6186
|
+
const e = raw;
|
|
6187
|
+
const d = e.data ?? {};
|
|
6188
|
+
const session = typeof d.sessionID === "string" ? d.sessionID : null;
|
|
6189
|
+
if ((e.type === "session.viewed" || e.type === "session.created") && session) this.#show(session);
|
|
6190
|
+
if (session && session !== this.#view.session) continue;
|
|
6191
|
+
if (e.type === "session.execution.started") this.#view = {
|
|
6192
|
+
...this.#view,
|
|
6193
|
+
busy: true
|
|
6194
|
+
};
|
|
6195
|
+
if (/^session\.execution\.(succeeded|failed|interrupted|cancelled|stopped)/.test(e.type)) {
|
|
6196
|
+
this.#view = {
|
|
6197
|
+
...this.#view,
|
|
6198
|
+
busy: false
|
|
6199
|
+
};
|
|
6200
|
+
this.#live.clear();
|
|
6201
|
+
}
|
|
6202
|
+
if (e.type === "session.text.delta" && typeof d.assistantMessageID === "string") {
|
|
6203
|
+
this.#live.set(d.assistantMessageID, (this.#live.get(d.assistantMessageID) ?? "") + String(d.delta ?? ""));
|
|
6204
|
+
this.#view = {
|
|
6205
|
+
...this.#view,
|
|
6206
|
+
turns: this.#turns(this.#messages)
|
|
6207
|
+
};
|
|
6208
|
+
this.#tell();
|
|
6209
|
+
continue;
|
|
6210
|
+
}
|
|
6211
|
+
if (e.type === "permission.asked" && typeof d.id === "string") {
|
|
6212
|
+
const source = d.source;
|
|
6213
|
+
this.#asking.set(d.id, {
|
|
6214
|
+
id: d.id,
|
|
6215
|
+
action: String(d.action ?? ""),
|
|
6216
|
+
resources: d.resources ?? [],
|
|
6217
|
+
...source?.id ? { tool: source.id } : {}
|
|
6218
|
+
});
|
|
6219
|
+
}
|
|
6220
|
+
if (e.type === "permission.replied" && typeof d.requestID === "string") this.#asking.delete(d.requestID);
|
|
6221
|
+
if (e.type === "session.usage.updated") this.#view = {
|
|
6222
|
+
...this.#view,
|
|
6223
|
+
cost: Number(d.cost ?? 0),
|
|
6224
|
+
tokens: {
|
|
6225
|
+
input: Number(d.tokens?.input ?? 0),
|
|
6226
|
+
output: Number(d.tokens?.output ?? 0)
|
|
6227
|
+
}
|
|
6228
|
+
};
|
|
6229
|
+
if (REREAD.test(e.type) || e.type === "session.usage.updated") this.#soon();
|
|
6230
|
+
}
|
|
6231
|
+
} catch {
|
|
6232
|
+
if (!signal.aborted && this.#client === client) this.detach();
|
|
6233
|
+
}
|
|
6234
|
+
}
|
|
6235
|
+
#messages = [];
|
|
6236
|
+
#turns(messages) {
|
|
6237
|
+
return toTurns(messages, {
|
|
6238
|
+
live: this.#live,
|
|
6239
|
+
ours: this.#ours,
|
|
6240
|
+
busy: this.#view.busy
|
|
6241
|
+
});
|
|
6242
|
+
}
|
|
6243
|
+
/** Read the session again shortly, once for a burst of events. */
|
|
6244
|
+
#soon() {
|
|
6245
|
+
if (this.#timer) return;
|
|
6246
|
+
this.#timer = setTimeout(() => {
|
|
6247
|
+
this.#timer = null;
|
|
6248
|
+
this.#read();
|
|
6249
|
+
}, 80);
|
|
6250
|
+
}
|
|
6251
|
+
async #read() {
|
|
6252
|
+
const client = this.#client, session = this.#view.session;
|
|
6253
|
+
if (!client) return;
|
|
6254
|
+
if (!session) {
|
|
6255
|
+
this.#tell();
|
|
6256
|
+
return;
|
|
6257
|
+
}
|
|
6258
|
+
const [listed, info] = await Promise.all([client.message.list({
|
|
6259
|
+
sessionID: session,
|
|
6260
|
+
limit: 200,
|
|
6261
|
+
order: "desc"
|
|
6262
|
+
}).catch(() => null), client.session.get({ sessionID: session }).catch(() => null)]);
|
|
6263
|
+
if (this.#client !== client || this.#view.session !== session) return;
|
|
6264
|
+
this.#messages = [...listed?.data ?? []].reverse();
|
|
6265
|
+
const model = [...this.#messages].reverse().find((m) => m.type === "assistant" && "content" in m)?.model?.id ?? info?.model?.id ?? this.#view.model;
|
|
6266
|
+
this.#view = {
|
|
6267
|
+
...this.#view,
|
|
6268
|
+
title: info?.title ?? this.#view.title,
|
|
6269
|
+
model,
|
|
6270
|
+
turns: this.#turns(this.#messages),
|
|
6271
|
+
asking: [...this.#asking.values()]
|
|
6272
|
+
};
|
|
6273
|
+
this.#tell();
|
|
6274
|
+
}
|
|
6275
|
+
};
|
|
6276
|
+
//#endregion
|
|
4955
6277
|
//#region packages/server/src/http.ts
|
|
4956
6278
|
const LOOPBACK = /* @__PURE__ */ new Set([
|
|
4957
6279
|
"localhost",
|
|
@@ -4964,6 +6286,7 @@ const TYPES = {
|
|
|
4964
6286
|
".js": "text/javascript; charset=utf-8",
|
|
4965
6287
|
".css": "text/css; charset=utf-8",
|
|
4966
6288
|
".json": "application/json",
|
|
6289
|
+
".webmanifest": "application/manifest+json",
|
|
4967
6290
|
".svg": "image/svg+xml",
|
|
4968
6291
|
".png": "image/png",
|
|
4969
6292
|
".woff2": "font/woff2",
|
|
@@ -4986,6 +6309,11 @@ const Tier = z.enum([
|
|
|
4986
6309
|
"mid",
|
|
4987
6310
|
"max"
|
|
4988
6311
|
]);
|
|
6312
|
+
/** What you chose of Iso's context (I3): parts switched off by id, and whether it may look up more. */
|
|
6313
|
+
const ChoiceSchema = z.object({
|
|
6314
|
+
off: z.array(z.string().max(400)).max(500).optional(),
|
|
6315
|
+
lookup: z.boolean().optional()
|
|
6316
|
+
}).strict();
|
|
4989
6317
|
const Ask = z.discriminatedUnion("form", [
|
|
4990
6318
|
z.object({
|
|
4991
6319
|
note: z.string(),
|
|
@@ -4993,7 +6321,8 @@ const Ask = z.discriminatedUnion("form", [
|
|
|
4993
6321
|
after: Anchor.nullable(),
|
|
4994
6322
|
before: Anchor.nullable(),
|
|
4995
6323
|
asked: z.string(),
|
|
4996
|
-
tier: Tier
|
|
6324
|
+
tier: Tier,
|
|
6325
|
+
choice: ChoiceSchema.optional()
|
|
4997
6326
|
}).strict(),
|
|
4998
6327
|
z.object({
|
|
4999
6328
|
note: z.string(),
|
|
@@ -5002,7 +6331,8 @@ const Ask = z.discriminatedUnion("form", [
|
|
|
5002
6331
|
after: Anchor.nullable(),
|
|
5003
6332
|
before: Anchor.nullable(),
|
|
5004
6333
|
asked: z.string(),
|
|
5005
|
-
tier: Tier
|
|
6334
|
+
tier: Tier,
|
|
6335
|
+
choice: ChoiceSchema.optional()
|
|
5006
6336
|
}).strict(),
|
|
5007
6337
|
z.object({
|
|
5008
6338
|
note: z.string(),
|
|
@@ -5021,6 +6351,41 @@ const Ask = z.discriminatedUnion("form", [
|
|
|
5021
6351
|
tier: Tier
|
|
5022
6352
|
}).strict()
|
|
5023
6353
|
]);
|
|
6354
|
+
const Given = z.discriminatedUnion("kind", [
|
|
6355
|
+
z.object({
|
|
6356
|
+
kind: z.literal("note"),
|
|
6357
|
+
id: z.string().max(400)
|
|
6358
|
+
}).strict(),
|
|
6359
|
+
z.object({
|
|
6360
|
+
kind: z.literal("folder"),
|
|
6361
|
+
id: z.string().max(400)
|
|
6362
|
+
}).strict(),
|
|
6363
|
+
z.object({
|
|
6364
|
+
kind: z.literal("passage"),
|
|
6365
|
+
id: z.string().max(400),
|
|
6366
|
+
text: z.string().max(2e4)
|
|
6367
|
+
}).strict(),
|
|
6368
|
+
z.object({
|
|
6369
|
+
kind: z.literal("understanding"),
|
|
6370
|
+
id: z.string().max(400)
|
|
6371
|
+
}).strict()
|
|
6372
|
+
]);
|
|
6373
|
+
const Mode = z.enum([
|
|
6374
|
+
"ask",
|
|
6375
|
+
"write",
|
|
6376
|
+
"build",
|
|
6377
|
+
"understand"
|
|
6378
|
+
]);
|
|
6379
|
+
const TurnAsk = z.object({
|
|
6380
|
+
conversation: z.string().max(64).optional(),
|
|
6381
|
+
mode: Mode,
|
|
6382
|
+
asked: z.string().max(8e3),
|
|
6383
|
+
given: z.array(Given).max(40),
|
|
6384
|
+
tier: Tier,
|
|
6385
|
+
lookup: z.boolean().optional(),
|
|
6386
|
+
off: z.array(z.string().max(64)).max(500).optional()
|
|
6387
|
+
}).strict();
|
|
6388
|
+
const TurnShape = TurnAsk.omit({ asked: true });
|
|
5024
6389
|
const ExerciseAsk = z.object({
|
|
5025
6390
|
folder: z.string().max(400),
|
|
5026
6391
|
tests: z.array(z.string().max(400)).min(1).max(20),
|
|
@@ -5048,7 +6413,7 @@ const AgentEdit = z.union([z.object({
|
|
|
5048
6413
|
newString: z.string(),
|
|
5049
6414
|
replaceAll: z.boolean().optional()
|
|
5050
6415
|
}).strict(), z.object({ content: z.string() }).strict()]);
|
|
5051
|
-
const
|
|
6416
|
+
const AgentChange = z.object({
|
|
5052
6417
|
agent: AgentName,
|
|
5053
6418
|
session: z.string().min(1).max(200),
|
|
5054
6419
|
note: z.string().max(400),
|
|
@@ -5060,7 +6425,7 @@ const AgentStatus = z.object({
|
|
|
5060
6425
|
session: z.string().min(1).max(200),
|
|
5061
6426
|
busy: z.boolean()
|
|
5062
6427
|
}).strict();
|
|
5063
|
-
function createApp({ api, token, loopback, web, allowHosts = [], notes }) {
|
|
6428
|
+
function createApp({ api, token, loopback, web, allowHosts = [], notes, terminal }) {
|
|
5064
6429
|
const app = new Hono();
|
|
5065
6430
|
const allowed = new Set([...allowHosts].map((h) => h.toLowerCase()));
|
|
5066
6431
|
const reply = (c, status, value) => c.body(JSON.stringify(value), status, {
|
|
@@ -5144,6 +6509,36 @@ function createApp({ api, token, loopback, web, allowHosts = [], notes }) {
|
|
|
5144
6509
|
return sent instanceof Response ? sent : answer(c, async () => await act(c, sent) ?? {});
|
|
5145
6510
|
});
|
|
5146
6511
|
post("/api/iso/ask", Ask, (_c, b) => api.ask(b));
|
|
6512
|
+
post("/api/iso/converse", TurnAsk, (_c, b) => api.converse(b));
|
|
6513
|
+
app.get("/api/understanding", (c) => answer(c, () => api.understanding(id(c))));
|
|
6514
|
+
post("/api/understanding/scratchpad", z.object({
|
|
6515
|
+
scratchpad: z.string().max(2e5),
|
|
6516
|
+
base: z.string().max(64).nullable()
|
|
6517
|
+
}).strict(), (c, b) => api.saveScratchpad(id(c), b.scratchpad, b.base));
|
|
6518
|
+
post("/api/iso/undo", z.object({
|
|
6519
|
+
conversation: z.string().max(64),
|
|
6520
|
+
turn: z.string().max(64)
|
|
6521
|
+
}).strict(), (_c, b) => api.undoTurn(b.conversation, b.turn));
|
|
6522
|
+
post("/api/iso/answer", z.object({
|
|
6523
|
+
turn: z.string().max(64),
|
|
6524
|
+
permission: z.string().max(64),
|
|
6525
|
+
decision: z.enum([
|
|
6526
|
+
"once",
|
|
6527
|
+
"always",
|
|
6528
|
+
"reject"
|
|
6529
|
+
])
|
|
6530
|
+
}).strict(), (_c, b) => api.answer(b.turn, b.permission, b.decision));
|
|
6531
|
+
app.get("/api/conversation", (c) => answer(c, async () => await api.conversation(c.req.query("id") || void 0) ?? {
|
|
6532
|
+
id: "",
|
|
6533
|
+
turns: []
|
|
6534
|
+
}));
|
|
6535
|
+
app.get("/api/turn/context", (c) => answer(c, () => {
|
|
6536
|
+
let raw = {};
|
|
6537
|
+
try {
|
|
6538
|
+
raw = JSON.parse(c.req.query("turn") ?? "{}");
|
|
6539
|
+
} catch {}
|
|
6540
|
+
return api.turnContext(TurnShape.parse(raw));
|
|
6541
|
+
}));
|
|
5147
6542
|
post("/api/iso/stop", z.object({ request: z.string().max(64) }).strict(), (_c, b) => api.stop(b.request));
|
|
5148
6543
|
post("/api/note/restore", z.object({
|
|
5149
6544
|
entry: z.string().max(64),
|
|
@@ -5168,7 +6563,7 @@ function createApp({ api, token, loopback, web, allowHosts = [], notes }) {
|
|
|
5168
6563
|
post("/api/exercise/draft", ExerciseAsk, (_c, b) => api.createExerciseDraft(b.folder, b.tests, b.after ?? null));
|
|
5169
6564
|
post("/api/exercise/keep", z.object({ proposal: z.string().max(64) }).strict(), (c, b) => api.keepExercise(id(c), b.proposal));
|
|
5170
6565
|
post("/api/exercise/wind-back", z.object({}).strict(), (c) => api.windBack(id(c)));
|
|
5171
|
-
app.get("/api/context", (c) => answer(c, () => api.context(c.req.query("id") || void 0)));
|
|
6566
|
+
app.get("/api/context", (c) => answer(c, () => api.context(c.req.query("id") || void 0, choiceOf(c.req.query("choice")))));
|
|
5172
6567
|
post("/api/context/clear", z.object({}).strict(), () => api.clearConversation());
|
|
5173
6568
|
app.get("/api/settings", (c) => answer(c, () => api.settings()));
|
|
5174
6569
|
post("/api/settings/key", Key, (_c, b) => api.setKey(b.key), "put");
|
|
@@ -5180,7 +6575,7 @@ function createApp({ api, token, loopback, web, allowHosts = [], notes }) {
|
|
|
5180
6575
|
app.get("/api/agent/tools", (c) => answer(c, () => api.agentTools()));
|
|
5181
6576
|
post("/api/agent/look", AgentLook, (_c, b) => api.agentLook(b));
|
|
5182
6577
|
app.get("/api/agent/base", (c) => answer(c, async () => ({ notes: notes ?? null })));
|
|
5183
|
-
post("/api/agent/
|
|
6578
|
+
post("/api/agent/write", AgentChange, (_c, b) => api.agentWrite(b));
|
|
5184
6579
|
post("/api/agent/send", z.object({
|
|
5185
6580
|
note: z.string().max(400),
|
|
5186
6581
|
passage: z.string().max(1e4).optional(),
|
|
@@ -5191,6 +6586,53 @@ function createApp({ api, token, loopback, web, allowHosts = [], notes }) {
|
|
|
5191
6586
|
agent: AgentName
|
|
5192
6587
|
}).strict(), (_c, b) => api.agentReceived(b.id, b.agent));
|
|
5193
6588
|
post("/api/agent/status", AgentStatus, (_c, b) => api.agentStatus(b.agent, b.session, b.busy));
|
|
6589
|
+
if (terminal) {
|
|
6590
|
+
const Local = z.string().max(200).refine((u) => {
|
|
6591
|
+
try {
|
|
6592
|
+
const h = new URL(u).hostname;
|
|
6593
|
+
return h === "127.0.0.1" || h === "localhost" || h === "[::1]";
|
|
6594
|
+
} catch {
|
|
6595
|
+
return false;
|
|
6596
|
+
}
|
|
6597
|
+
}, "OpenCode's server must be on this machine");
|
|
6598
|
+
post("/api/terminal/attach", z.object({
|
|
6599
|
+
url: Local,
|
|
6600
|
+
password: z.string().min(1).max(200),
|
|
6601
|
+
directory: z.string().max(4e3)
|
|
6602
|
+
}).strict(), async (_c, b) => {
|
|
6603
|
+
await terminal.attach(b).catch((e) => {
|
|
6604
|
+
throw new EngineError(`OpenCode's server did not answer: ${e.message}`, 502);
|
|
6605
|
+
});
|
|
6606
|
+
});
|
|
6607
|
+
post("/api/terminal/detach", z.object({}).strict(), async () => {
|
|
6608
|
+
terminal.detach();
|
|
6609
|
+
});
|
|
6610
|
+
app.get("/api/terminal", (c) => answer(c, async () => terminal.view()));
|
|
6611
|
+
const failed = (e) => {
|
|
6612
|
+
throw new EngineError(e.message, 409);
|
|
6613
|
+
};
|
|
6614
|
+
post("/api/terminal/prompt", z.object({
|
|
6615
|
+
asked: z.string().min(1).max(8e3),
|
|
6616
|
+
given: z.array(Given).max(40)
|
|
6617
|
+
}).strict(), async (_c, b) => {
|
|
6618
|
+
const titles = new Map((await api.notes()).notes.map((n) => [n.id, n.title]));
|
|
6619
|
+
const text = given(b.asked, b.given, (id) => titles.get(id) ?? id);
|
|
6620
|
+
await terminal.prompt(text).catch(failed);
|
|
6621
|
+
});
|
|
6622
|
+
post("/api/terminal/stop", z.object({}).strict(), async () => {
|
|
6623
|
+
await terminal.interrupt().catch(failed);
|
|
6624
|
+
});
|
|
6625
|
+
post("/api/terminal/permission", z.object({
|
|
6626
|
+
id: z.string().max(200),
|
|
6627
|
+
decision: z.enum([
|
|
6628
|
+
"once",
|
|
6629
|
+
"always",
|
|
6630
|
+
"reject"
|
|
6631
|
+
])
|
|
6632
|
+
}).strict(), async (_c, b) => {
|
|
6633
|
+
await terminal.reply(b.id, b.decision).catch(failed);
|
|
6634
|
+
});
|
|
6635
|
+
}
|
|
5194
6636
|
const streams = /* @__PURE__ */ new Map();
|
|
5195
6637
|
const watchers = () => {
|
|
5196
6638
|
const browsers = [...streams.values()].filter(Boolean).length;
|
|
@@ -5209,6 +6651,16 @@ function createApp({ api, token, loopback, web, allowHosts = [], notes }) {
|
|
|
5209
6651
|
data: JSON.stringify(event)
|
|
5210
6652
|
});
|
|
5211
6653
|
});
|
|
6654
|
+
const send = (view) => {
|
|
6655
|
+
stream.writeSSE({
|
|
6656
|
+
event: "terminal",
|
|
6657
|
+
data: JSON.stringify({
|
|
6658
|
+
type: "terminal",
|
|
6659
|
+
terminal: view
|
|
6660
|
+
})
|
|
6661
|
+
}).catch(() => void 0);
|
|
6662
|
+
};
|
|
6663
|
+
const unfollow = terminal?.listen(send);
|
|
5212
6664
|
const beat = setInterval(() => {
|
|
5213
6665
|
stream.write(": still here\n\n");
|
|
5214
6666
|
}, 25e3);
|
|
@@ -5216,6 +6668,7 @@ function createApp({ api, token, loopback, web, allowHosts = [], notes }) {
|
|
|
5216
6668
|
event: "open",
|
|
5217
6669
|
data: "{}"
|
|
5218
6670
|
});
|
|
6671
|
+
if (terminal?.view().connected) send(terminal.view());
|
|
5219
6672
|
streams.set(stream, c.req.query("agent") === void 0);
|
|
5220
6673
|
watchers();
|
|
5221
6674
|
await new Promise((done) => stream.onAbort(done));
|
|
@@ -5223,6 +6676,7 @@ function createApp({ api, token, loopback, web, allowHosts = [], notes }) {
|
|
|
5223
6676
|
watchers();
|
|
5224
6677
|
clearInterval(beat);
|
|
5225
6678
|
stop();
|
|
6679
|
+
unfollow?.();
|
|
5226
6680
|
}));
|
|
5227
6681
|
app.all("/api/*", (c) => refuse(c, 404, "not found"));
|
|
5228
6682
|
app.on(["GET", "HEAD"], "*", (c) => !hostOk(c) ? c.text(NOT_HERE, 403) : web ? staticFile(c, web) : c.text("Not found", 404));
|
|
@@ -5247,6 +6701,19 @@ function staticFile(c, web) {
|
|
|
5247
6701
|
"X-Content-Type-Options": "nosniff"
|
|
5248
6702
|
});
|
|
5249
6703
|
}
|
|
6704
|
+
/** The choice of what Iso is sent, as the context route takes it: JSON, and only its known fields kept. */
|
|
6705
|
+
function choiceOf(raw) {
|
|
6706
|
+
if (!raw) return void 0;
|
|
6707
|
+
try {
|
|
6708
|
+
const c = JSON.parse(raw);
|
|
6709
|
+
return {
|
|
6710
|
+
...Array.isArray(c.off) ? { off: c.off.filter((x) => typeof x === "string").slice(0, 500) } : {},
|
|
6711
|
+
...typeof c.lookup === "boolean" ? { lookup: c.lookup } : {}
|
|
6712
|
+
};
|
|
6713
|
+
} catch {
|
|
6714
|
+
return;
|
|
6715
|
+
}
|
|
6716
|
+
}
|
|
5250
6717
|
//#endregion
|
|
5251
6718
|
//#region packages/server/src/nodefiles.ts
|
|
5252
6719
|
const gone = (e) => ["ENOENT", "ENOTDIR"].includes(e.code ?? "");
|
|
@@ -5420,13 +6887,15 @@ async function serve$1(path, { host = "127.0.0.1", port = 4767, allowHosts = []
|
|
|
5420
6887
|
const opened = locate(path);
|
|
5421
6888
|
const api = await openEngine(opened);
|
|
5422
6889
|
const web = existsSync(join(WEB_DIR, "index.html")) ? WEB_DIR : null;
|
|
6890
|
+
const terminal = new Terminal();
|
|
5423
6891
|
const app = createApp({
|
|
5424
6892
|
api,
|
|
5425
6893
|
token: randomBytes(24).toString("base64url"),
|
|
5426
6894
|
loopback: LOOPBACK.has(host),
|
|
5427
6895
|
web,
|
|
5428
6896
|
allowHosts,
|
|
5429
|
-
notes: opened.notes
|
|
6897
|
+
notes: opened.notes,
|
|
6898
|
+
terminal
|
|
5430
6899
|
});
|
|
5431
6900
|
const server = serve({
|
|
5432
6901
|
fetch: app.fetch,
|
|
@@ -5462,100 +6931,333 @@ function scriptedHttp(answer, pace = 0) {
|
|
|
5462
6931
|
stop(Object.assign(/* @__PURE__ */ new Error("aborted"), { name: "AbortError" }));
|
|
5463
6932
|
return;
|
|
5464
6933
|
}
|
|
5465
|
-
const t = setTimeout(done, ms);
|
|
5466
|
-
signal?.addEventListener("abort", () => {
|
|
5467
|
-
clearTimeout(t);
|
|
5468
|
-
stop(Object.assign(/* @__PURE__ */ new Error("aborted"), { name: "AbortError" }));
|
|
5469
|
-
});
|
|
5470
|
-
});
|
|
5471
|
-
const body = new ReadableStream({ async start(c) {
|
|
5472
|
-
const enc = new TextEncoder(), send = (o) => c.enqueue(enc.encode(data(o)));
|
|
5473
|
-
try {
|
|
5474
|
-
const text = reply.text ?? "";
|
|
5475
|
-
for (const piece of pace ? text.match(/[\s\S]{1,14}/g) ?? [] : text ? [text] : []) {
|
|
5476
|
-
send({
|
|
5477
|
-
model: request.model,
|
|
5478
|
-
choices: [{ delta: { content: piece } }]
|
|
5479
|
-
});
|
|
5480
|
-
if (pace) await wait(pace);
|
|
6934
|
+
const t = setTimeout(done, ms);
|
|
6935
|
+
signal?.addEventListener("abort", () => {
|
|
6936
|
+
clearTimeout(t);
|
|
6937
|
+
stop(Object.assign(/* @__PURE__ */ new Error("aborted"), { name: "AbortError" }));
|
|
6938
|
+
});
|
|
6939
|
+
});
|
|
6940
|
+
const body = new ReadableStream({ async start(c) {
|
|
6941
|
+
const enc = new TextEncoder(), send = (o) => c.enqueue(enc.encode(data(o)));
|
|
6942
|
+
try {
|
|
6943
|
+
const text = reply.text ?? "";
|
|
6944
|
+
for (const piece of pace ? text.match(/[\s\S]{1,14}/g) ?? [] : text ? [text] : []) {
|
|
6945
|
+
send({
|
|
6946
|
+
model: request.model,
|
|
6947
|
+
choices: [{ delta: { content: piece } }]
|
|
6948
|
+
});
|
|
6949
|
+
if (pace) await wait(pace);
|
|
6950
|
+
}
|
|
6951
|
+
for (const [index, call] of (reply.calls ?? []).entries()) send({ choices: [{ delta: { tool_calls: [{
|
|
6952
|
+
index,
|
|
6953
|
+
id: `call_${index}_${Math.random().toString(36).slice(2, 8)}`,
|
|
6954
|
+
function: {
|
|
6955
|
+
name: call.name,
|
|
6956
|
+
arguments: JSON.stringify(call.arguments)
|
|
6957
|
+
}
|
|
6958
|
+
}] } }] });
|
|
6959
|
+
if (pace && reply.calls) await wait(pace * 20);
|
|
6960
|
+
const u = reply.usage ?? {
|
|
6961
|
+
in: 3e3,
|
|
6962
|
+
out: 200,
|
|
6963
|
+
usd: .008
|
|
6964
|
+
};
|
|
6965
|
+
send({ choices: [{
|
|
6966
|
+
delta: {},
|
|
6967
|
+
finish_reason: reply.calls ? "tool_calls" : "stop"
|
|
6968
|
+
}] });
|
|
6969
|
+
send({
|
|
6970
|
+
choices: [],
|
|
6971
|
+
usage: {
|
|
6972
|
+
prompt_tokens: u.in,
|
|
6973
|
+
completion_tokens: u.out,
|
|
6974
|
+
cost: u.usd,
|
|
6975
|
+
prompt_tokens_details: { cached_tokens: u.cached ?? 0 }
|
|
6976
|
+
}
|
|
6977
|
+
});
|
|
6978
|
+
c.enqueue(enc.encode("data: [DONE]\n\n"));
|
|
6979
|
+
c.close();
|
|
6980
|
+
} catch (e) {
|
|
6981
|
+
c.error(e);
|
|
6982
|
+
}
|
|
6983
|
+
} });
|
|
6984
|
+
return new Response(body, {
|
|
6985
|
+
status: 200,
|
|
6986
|
+
headers: { "content-type": "text/event-stream" }
|
|
6987
|
+
});
|
|
6988
|
+
});
|
|
6989
|
+
return { fetch };
|
|
6990
|
+
}
|
|
6991
|
+
//#endregion
|
|
6992
|
+
//#region packages/example/src/tutorial.ts
|
|
6993
|
+
const UNSCRIPTED$1 = "In this tutorial I answer only what it walks you through. With a provider key in your own isorite, I would answer this too.";
|
|
6994
|
+
const text = (r) => r.messages.filter((m) => m.role === "user").map((m) => String(m.content)).join("\n\n");
|
|
6995
|
+
const looked = (r) => r.messages.some((m) => m.role === "tool");
|
|
6996
|
+
const asked = (r) => (text(r).split(/# What they ask(?: now)?[^\n]*\n\n/).at(-1) ?? "").trim().toLowerCase();
|
|
6997
|
+
const INSERT = `To insert a body, start at the root and walk down: at each node, go into the one of its four squares that holds the body's position.
|
|
6998
|
+
|
|
6999
|
+
- **An empty leaf** takes the body, and is done.
|
|
7000
|
+
- **A leaf with a body already** is split: both bodies go down into its new children, which may split again if they land in the same square.
|
|
7001
|
+
- **An inner node** passes the body down to the child whose square holds it.
|
|
7002
|
+
|
|
7003
|
+
On the way down, every node passed adds the body to its total mass $M$ and to $M\\mathbf{R}$, so that each keeps its mass and centre of mass with no second pass. A node keeps its square (centre and side $s$), its mass, its centre of mass, and its four children or its one body.`;
|
|
7004
|
+
const THETA = `With $\\theta = 0.5$ the force error is about a percent for most bodies, and several percent for a few near dense nodes; it falls quickly as $\\theta$ shrinks. The error of a node used whole is of order $(s/d)^2$, the [centre of mass](/algorithms/centre-of-mass.md "uses")'s quadrupole, and $s/d < \\theta$ bounds it.
|
|
7005
|
+
|
|
7006
|
+
The cost: a body opens nodes only near it. Around it, each level of the tree adds a ring of about the same number of nodes used whole, and there are $\\log_4 N$ levels, so each body visits $O(\\log N)$ nodes, and all of them $O(N \\log N)$.`;
|
|
7007
|
+
const SPEED = `About ten times fewer interactions. Direct summation computes $N(N-1)/2 \\approx 1.25 \\times 10^7$ pairs at $N = 5000$. Barnes–Hut at $\\theta = 0.5$ uses some 200 nodes for each body, so about $10^6$ interactions in all.
|
|
7008
|
+
|
|
7009
|
+
Each of them costs more: the tree walk branches, and in plain Python it is slow enough to lose the whole factor. Measure it, as your note on [cost and accuracy](/algorithms/cost-and-accuracy.md) says, before counting on it.`;
|
|
7010
|
+
const OCTREE = `An octree is the same tree with eight children instead of four: each cube is split in half along each of the three axes.
|
|
7011
|
+
|
|
7012
|
+
What changes:
|
|
7013
|
+
- **Insertion** picks one of eight children, by the sign of the body's position against the cube's centre on each axis: three comparisons, one bit each.
|
|
7014
|
+
- **The opening rule** is the same, $s/d < \\theta$, with $s$ the cube's side.
|
|
7015
|
+
- **The cost** is still $O(N \\log N)$; the log is now base 8, but each opened node has twice as many children, so the work per body is about the same.
|
|
7016
|
+
|
|
7017
|
+
Nothing in the [main loop](/simulation/the-main-loop.md) changes: only \`accelerations\`.`;
|
|
7018
|
+
const OCTREE_NOTE = `---
|
|
7019
|
+
type: Concept
|
|
7020
|
+
title: The octree
|
|
7021
|
+
description: The quadtree in 3D: each cube split into eight.
|
|
7022
|
+
---
|
|
7023
|
+
|
|
7024
|
+
The [quadtree](/algorithms/the-quadtree.md "requires") in three dimensions, for [Barnes–Hut](/algorithms/barnes-hut.md "uses") on the 3D simulation.
|
|
7025
|
+
|
|
7026
|
+
${OCTREE}
|
|
7027
|
+
`;
|
|
7028
|
+
const CLOSE = `When two bodies pass within a small fraction of their usual distance, their acceleration grows as $1/r^2$, and a step of fixed length overshoots: the energy jumps.
|
|
7029
|
+
|
|
7030
|
+
Three ways out:
|
|
7031
|
+
- **Soften the force.** With $\\varepsilon$ a little larger than the closest passes you care about, the force stays finite. Right for a galaxy, whose bodies stand for many stars.
|
|
7032
|
+
- **A shorter step**, for everyone. Simple, and leapfrog stays symplectic, but every body pays for the one close pair.
|
|
7033
|
+
- **Steps that differ by body** (block timesteps). Efficient, but only a careful scheme keeps the energy from drifting.
|
|
7034
|
+
|
|
7035
|
+
For two galaxies colliding, the first is the usual answer.`;
|
|
7036
|
+
/** The answer to one request to the tutorial's Iso. */
|
|
7037
|
+
/** In a turn in Iso's pane (M10): which mode, from what it was told. */
|
|
7038
|
+
const modeOf = (r) => {
|
|
7039
|
+
const system = JSON.stringify(r.messages.find((m) => m.role === "system")?.content ?? "");
|
|
7040
|
+
return system.includes("# Write mode") ? "write" : system.includes("# Understand mode") ? "understand" : system.includes("# Ask mode") ? "ask" : null;
|
|
7041
|
+
};
|
|
7042
|
+
const tools = (r) => r.messages.filter((m) => m.role === "tool").length;
|
|
7043
|
+
const TODO = "TODO: how big an error does θ = 0.5 give, and why does the cost grow only as N log N?";
|
|
7044
|
+
/** Iso's pane (M10): Ask, Write and Understand mode, on the questions the tutorial walks through. */
|
|
7045
|
+
function turnAnswer(r, mode) {
|
|
7046
|
+
const q = asked(r), n = tools(r);
|
|
7047
|
+
if (mode === "ask" && q.includes("inserted")) {
|
|
7048
|
+
if (n === 0) return {
|
|
7049
|
+
calls: [{
|
|
7050
|
+
name: "search_notes",
|
|
7051
|
+
arguments: { query: "quadtree" }
|
|
7052
|
+
}, {
|
|
7053
|
+
name: "read_note",
|
|
7054
|
+
arguments: { id: "algorithms/centre-of-mass" }
|
|
7055
|
+
}],
|
|
7056
|
+
usage: {
|
|
7057
|
+
in: 5100,
|
|
7058
|
+
out: 50,
|
|
7059
|
+
usd: .0059
|
|
7060
|
+
}
|
|
7061
|
+
};
|
|
7062
|
+
if (n === 2) return {
|
|
7063
|
+
calls: [{
|
|
7064
|
+
name: "point_to_note",
|
|
7065
|
+
arguments: {
|
|
7066
|
+
id: "algorithms/centre-of-mass",
|
|
7067
|
+
why: "It is what each node keeps as bodies pass down."
|
|
7068
|
+
}
|
|
7069
|
+
}],
|
|
7070
|
+
usage: {
|
|
7071
|
+
in: 7800,
|
|
7072
|
+
out: 40,
|
|
7073
|
+
usd: .0061
|
|
7074
|
+
}
|
|
7075
|
+
};
|
|
7076
|
+
return {
|
|
7077
|
+
text: INSERT,
|
|
7078
|
+
usage: {
|
|
7079
|
+
in: 8400,
|
|
7080
|
+
out: 310,
|
|
7081
|
+
cached: 4800,
|
|
7082
|
+
usd: .0163
|
|
7083
|
+
}
|
|
7084
|
+
};
|
|
7085
|
+
}
|
|
7086
|
+
if (mode === "ask" && q.includes("faster")) {
|
|
7087
|
+
if (n === 0) return {
|
|
7088
|
+
calls: [{
|
|
7089
|
+
name: "read_note",
|
|
7090
|
+
arguments: { id: "algorithms/direct-summation" }
|
|
7091
|
+
}, {
|
|
7092
|
+
name: "read_note",
|
|
7093
|
+
arguments: { id: "algorithms/barnes-hut" }
|
|
7094
|
+
}],
|
|
7095
|
+
usage: {
|
|
7096
|
+
in: 5300,
|
|
7097
|
+
out: 50,
|
|
7098
|
+
usd: .006
|
|
7099
|
+
}
|
|
7100
|
+
};
|
|
7101
|
+
return {
|
|
7102
|
+
text: SPEED,
|
|
7103
|
+
usage: {
|
|
7104
|
+
in: 8800,
|
|
7105
|
+
out: 260,
|
|
7106
|
+
cached: 5e3,
|
|
7107
|
+
usd: .0158
|
|
7108
|
+
}
|
|
7109
|
+
};
|
|
7110
|
+
}
|
|
7111
|
+
if (mode === "write" && q.includes("octree")) {
|
|
7112
|
+
if (n === 0) return {
|
|
7113
|
+
calls: [{
|
|
7114
|
+
name: "read_note",
|
|
7115
|
+
arguments: { id: "algorithms/the-quadtree" }
|
|
7116
|
+
}, {
|
|
7117
|
+
name: "read_note",
|
|
7118
|
+
arguments: { id: "algorithms/barnes-hut" }
|
|
7119
|
+
}],
|
|
7120
|
+
usage: {
|
|
7121
|
+
in: 5200,
|
|
7122
|
+
out: 60,
|
|
7123
|
+
usd: .0058
|
|
7124
|
+
}
|
|
7125
|
+
};
|
|
7126
|
+
if (n === 2) return {
|
|
7127
|
+
calls: [{
|
|
7128
|
+
name: "write_note",
|
|
7129
|
+
arguments: {
|
|
7130
|
+
id: "algorithms/the-octree",
|
|
7131
|
+
text: OCTREE_NOTE
|
|
7132
|
+
}
|
|
7133
|
+
}],
|
|
7134
|
+
usage: {
|
|
7135
|
+
in: 9900,
|
|
7136
|
+
out: 380,
|
|
7137
|
+
cached: 4400,
|
|
7138
|
+
usd: .0192
|
|
7139
|
+
}
|
|
7140
|
+
};
|
|
7141
|
+
return {
|
|
7142
|
+
text: "I wrote The octree in algorithms, from the quadtree and Barnes–Hut, and linked it to both.",
|
|
7143
|
+
usage: {
|
|
7144
|
+
in: 10400,
|
|
7145
|
+
out: 40,
|
|
7146
|
+
cached: 9800,
|
|
7147
|
+
usd: .0043
|
|
7148
|
+
}
|
|
7149
|
+
};
|
|
7150
|
+
}
|
|
7151
|
+
if (mode === "write" && q.includes("todo")) {
|
|
7152
|
+
if (n === 0) return {
|
|
7153
|
+
calls: [{
|
|
7154
|
+
name: "read_note",
|
|
7155
|
+
arguments: { id: "algorithms/barnes-hut" }
|
|
7156
|
+
}, {
|
|
7157
|
+
name: "read_note",
|
|
7158
|
+
arguments: { id: "algorithms/the-quadtree" }
|
|
7159
|
+
}],
|
|
7160
|
+
usage: {
|
|
7161
|
+
in: 5600,
|
|
7162
|
+
out: 60,
|
|
7163
|
+
usd: .0061
|
|
7164
|
+
}
|
|
7165
|
+
};
|
|
7166
|
+
if (n === 2) return {
|
|
7167
|
+
calls: [{
|
|
7168
|
+
name: "edit_note",
|
|
7169
|
+
arguments: {
|
|
7170
|
+
id: "algorithms/barnes-hut",
|
|
7171
|
+
old: TODO,
|
|
7172
|
+
new: THETA
|
|
7173
|
+
}
|
|
7174
|
+
}],
|
|
7175
|
+
usage: {
|
|
7176
|
+
in: 8900,
|
|
7177
|
+
out: 290,
|
|
7178
|
+
cached: 5100,
|
|
7179
|
+
usd: .0154
|
|
7180
|
+
}
|
|
7181
|
+
};
|
|
7182
|
+
return {
|
|
7183
|
+
text: "I answered both questions in your TODO, from the quadtree's depth and the centre of mass, and took the TODO away.",
|
|
7184
|
+
usage: {
|
|
7185
|
+
in: 9400,
|
|
7186
|
+
out: 40,
|
|
7187
|
+
cached: 8800,
|
|
7188
|
+
usd: .0041
|
|
7189
|
+
}
|
|
7190
|
+
};
|
|
7191
|
+
}
|
|
7192
|
+
if (mode === "understand" && /backwards|retrace|reverse/.test(q)) {
|
|
7193
|
+
if (n === 0) return {
|
|
7194
|
+
calls: [{
|
|
7195
|
+
name: "record_understanding",
|
|
7196
|
+
arguments: {
|
|
7197
|
+
id: "integrators/leapfrog",
|
|
7198
|
+
by: .5,
|
|
7199
|
+
why: "You saw that running it backwards retraces it, and that this is why its error cannot pile up."
|
|
5481
7200
|
}
|
|
5482
|
-
|
|
5483
|
-
|
|
5484
|
-
|
|
5485
|
-
|
|
5486
|
-
|
|
5487
|
-
|
|
5488
|
-
|
|
5489
|
-
|
|
5490
|
-
|
|
5491
|
-
|
|
5492
|
-
|
|
5493
|
-
out: 200,
|
|
5494
|
-
usd: .008
|
|
5495
|
-
};
|
|
5496
|
-
send({ choices: [{
|
|
5497
|
-
delta: {},
|
|
5498
|
-
finish_reason: reply.calls ? "tool_calls" : "stop"
|
|
5499
|
-
}] });
|
|
5500
|
-
send({
|
|
5501
|
-
choices: [],
|
|
5502
|
-
usage: {
|
|
5503
|
-
prompt_tokens: u.in,
|
|
5504
|
-
completion_tokens: u.out,
|
|
5505
|
-
cost: u.usd,
|
|
5506
|
-
prompt_tokens_details: { cached_tokens: u.cached ?? 0 }
|
|
5507
|
-
}
|
|
5508
|
-
});
|
|
5509
|
-
c.enqueue(enc.encode("data: [DONE]\n\n"));
|
|
5510
|
-
c.close();
|
|
5511
|
-
} catch (e) {
|
|
5512
|
-
c.error(e);
|
|
7201
|
+
}, {
|
|
7202
|
+
name: "write_understanding",
|
|
7203
|
+
arguments: {
|
|
7204
|
+
id: "integrators/leapfrog",
|
|
7205
|
+
reading: "You can say why leapfrog's energy error stays bounded: it is time-reversible, so it cannot drift one way. Not yet shown: what being symplectic adds to that."
|
|
7206
|
+
}
|
|
7207
|
+
}],
|
|
7208
|
+
usage: {
|
|
7209
|
+
in: 6200,
|
|
7210
|
+
out: 120,
|
|
7211
|
+
usd: .0068
|
|
5513
7212
|
}
|
|
5514
|
-
}
|
|
5515
|
-
return
|
|
5516
|
-
|
|
5517
|
-
|
|
5518
|
-
|
|
5519
|
-
|
|
5520
|
-
|
|
7213
|
+
};
|
|
7214
|
+
return {
|
|
7215
|
+
text: "Yes: run backwards it retraces its steps, so an error it made going forward is undone coming back, and it cannot keep growing one way. That is reversibility. Being [symplectic](/integrators/symplectic-integrators.md) is the stronger reason, and the next thing to look at.",
|
|
7216
|
+
usage: {
|
|
7217
|
+
in: 7100,
|
|
7218
|
+
out: 90,
|
|
7219
|
+
cached: 5600,
|
|
7220
|
+
usd: .0049
|
|
7221
|
+
}
|
|
7222
|
+
};
|
|
7223
|
+
}
|
|
7224
|
+
if (mode === "understand" && /leapfrog|bounded|drift/.test(q)) {
|
|
7225
|
+
if (n === 0) return {
|
|
7226
|
+
calls: [{
|
|
7227
|
+
name: "read_note",
|
|
7228
|
+
arguments: {
|
|
7229
|
+
id: "integrators/leapfrog",
|
|
7230
|
+
section: "Why it does not drift"
|
|
7231
|
+
}
|
|
7232
|
+
}],
|
|
7233
|
+
usage: {
|
|
7234
|
+
in: 5200,
|
|
7235
|
+
out: 40,
|
|
7236
|
+
usd: .0056
|
|
7237
|
+
}
|
|
7238
|
+
};
|
|
7239
|
+
return {
|
|
7240
|
+
text: "Before I say why: what happens if you run leapfrog with $-\\Delta t$, from where it ended?",
|
|
7241
|
+
usage: {
|
|
7242
|
+
in: 6900,
|
|
7243
|
+
out: 30,
|
|
7244
|
+
cached: 4800,
|
|
7245
|
+
usd: .0044
|
|
7246
|
+
}
|
|
7247
|
+
};
|
|
7248
|
+
}
|
|
7249
|
+
return null;
|
|
5521
7250
|
}
|
|
5522
|
-
//#endregion
|
|
5523
|
-
//#region packages/example/src/tutorial.ts
|
|
5524
|
-
const UNSCRIPTED$1 = "In this tutorial I answer only what it walks you through. With a provider key in your own isorite, I would answer this too.";
|
|
5525
|
-
const text = (r) => r.messages.filter((m) => m.role === "user").map((m) => String(m.content)).join("\n\n");
|
|
5526
|
-
const looked = (r) => r.messages.some((m) => m.role === "tool");
|
|
5527
|
-
const asked = (r) => (text(r).split(/# What they ask(?: now)?[^\n]*\n\n/).at(-1) ?? "").trim().toLowerCase();
|
|
5528
|
-
const INSERT = `To insert a body, start at the root and walk down: at each node, go into the one of its four squares that holds the body's position.
|
|
5529
|
-
|
|
5530
|
-
- **An empty leaf** takes the body, and is done.
|
|
5531
|
-
- **A leaf with a body already** is split: both bodies go down into its new children, which may split again if they land in the same square.
|
|
5532
|
-
- **An inner node** passes the body down to the child whose square holds it.
|
|
5533
|
-
|
|
5534
|
-
On the way down, every node passed adds the body to its total mass $M$ and to $M\\mathbf{R}$, so that each keeps its mass and centre of mass with no second pass. A node keeps its square (centre and side $s$), its mass, its centre of mass, and its four children or its one body.`;
|
|
5535
|
-
const THETA = `With $\\theta = 0.5$ the force error is about a percent for most bodies, and several percent for a few near dense nodes; it falls quickly as $\\theta$ shrinks. The error of a node used whole is of order $(s/d)^2$, the [centre of mass](/algorithms/centre-of-mass.md "uses")'s quadrupole, and $s/d < \\theta$ bounds it.
|
|
5536
|
-
|
|
5537
|
-
The cost: a body opens nodes only near it. Around it, each level of the tree adds a ring of about the same number of nodes used whole, and there are $\\log_4 N$ levels, so each body visits $O(\\log N)$ nodes, and all of them $O(N \\log N)$.`;
|
|
5538
|
-
const SPEED = `About ten times fewer interactions. Direct summation computes $N(N-1)/2 \\approx 1.25 \\times 10^7$ pairs at $N = 5000$. Barnes–Hut at $\\theta = 0.5$ uses some 200 nodes for each body, so about $10^6$ interactions in all.
|
|
5539
|
-
|
|
5540
|
-
Each of them costs more: the tree walk branches, and in plain Python it is slow enough to lose the whole factor. Measure it, as your note on [cost and accuracy](/algorithms/cost-and-accuracy.md) says, before counting on it.`;
|
|
5541
|
-
const OCTREE = `An octree is the same tree with eight children instead of four: each cube is split in half along each of the three axes.
|
|
5542
|
-
|
|
5543
|
-
What changes:
|
|
5544
|
-
- **Insertion** picks one of eight children, by the sign of the body's position against the cube's centre on each axis: three comparisons, one bit each.
|
|
5545
|
-
- **The opening rule** is the same, $s/d < \\theta$, with $s$ the cube's side.
|
|
5546
|
-
- **The cost** is still $O(N \\log N)$; the log is now base 8, but each opened node has twice as many children, so the work per body is about the same.
|
|
5547
|
-
|
|
5548
|
-
Nothing in the [main loop](/simulation/the-main-loop.md) changes: only \`accelerations\`.`;
|
|
5549
|
-
const CLOSE = `When two bodies pass within a small fraction of their usual distance, their acceleration grows as $1/r^2$, and a step of fixed length overshoots: the energy jumps.
|
|
5550
|
-
|
|
5551
|
-
Three ways out:
|
|
5552
|
-
- **Soften the force.** With $\\varepsilon$ a little larger than the closest passes you care about, the force stays finite. Right for a galaxy, whose bodies stand for many stars.
|
|
5553
|
-
- **A shorter step**, for everyone. Simple, and leapfrog stays symplectic, but every body pays for the one close pair.
|
|
5554
|
-
- **Steps that differ by body** (block timesteps). Efficient, but only a careful scheme keeps the energy from drifting.
|
|
5555
|
-
|
|
5556
|
-
For two galaxies colliding, the first is the usual answer.`;
|
|
5557
|
-
/** The answer to one request to the tutorial's Iso. */
|
|
5558
7251
|
function tutorialAnswer(r) {
|
|
7252
|
+
const mode = modeOf(r);
|
|
7253
|
+
if (mode) return turnAnswer(r, mode) ?? {
|
|
7254
|
+
text: "In this tutorial I answer only what it walks you through. With a provider key in your own isorite, I would answer this too.",
|
|
7255
|
+
usage: {
|
|
7256
|
+
in: 4e3,
|
|
7257
|
+
out: 40,
|
|
7258
|
+
usd: .0049
|
|
7259
|
+
}
|
|
7260
|
+
};
|
|
5559
7261
|
const all = text(r);
|
|
5560
7262
|
if (all.includes("⟦answer⟧")) {
|
|
5561
7263
|
const answer = /⟦answer⟧([\s\S]*?)⟦\/answer⟧/.exec(all)?.[1] ?? "";
|
|
@@ -5721,6 +7423,70 @@ function tutorialAnswer(r) {
|
|
|
5721
7423
|
}
|
|
5722
7424
|
};
|
|
5723
7425
|
}
|
|
7426
|
+
if (q.includes("octree") && q.includes("barnes")) {
|
|
7427
|
+
if (!looked(r)) return {
|
|
7428
|
+
calls: [
|
|
7429
|
+
{
|
|
7430
|
+
name: "read_note",
|
|
7431
|
+
arguments: { id: "algorithms/the-quadtree" }
|
|
7432
|
+
},
|
|
7433
|
+
{
|
|
7434
|
+
name: "read_note",
|
|
7435
|
+
arguments: { id: "algorithms/barnes-hut" }
|
|
7436
|
+
},
|
|
7437
|
+
{
|
|
7438
|
+
name: "search_notes",
|
|
7439
|
+
arguments: { query: "octree" }
|
|
7440
|
+
}
|
|
7441
|
+
],
|
|
7442
|
+
usage: {
|
|
7443
|
+
in: 5200,
|
|
7444
|
+
out: 60,
|
|
7445
|
+
usd: .0058
|
|
7446
|
+
}
|
|
7447
|
+
};
|
|
7448
|
+
return {
|
|
7449
|
+
text: OCTREE,
|
|
7450
|
+
usage: {
|
|
7451
|
+
in: 9900,
|
|
7452
|
+
out: 340,
|
|
7453
|
+
cached: 4400,
|
|
7454
|
+
usd: .0188
|
|
7455
|
+
}
|
|
7456
|
+
};
|
|
7457
|
+
}
|
|
7458
|
+
if (/pass(es)? close|close (pass|encounter)/.test(q)) {
|
|
7459
|
+
if (!looked(r)) return {
|
|
7460
|
+
calls: [
|
|
7461
|
+
{
|
|
7462
|
+
name: "read_note",
|
|
7463
|
+
arguments: { id: "physics/softening" }
|
|
7464
|
+
},
|
|
7465
|
+
{
|
|
7466
|
+
name: "read_note",
|
|
7467
|
+
arguments: { id: "integrators/choosing-the-timestep" }
|
|
7468
|
+
},
|
|
7469
|
+
{
|
|
7470
|
+
name: "find_similar",
|
|
7471
|
+
arguments: { text: "an energy jump when two bodies pass close" }
|
|
7472
|
+
}
|
|
7473
|
+
],
|
|
7474
|
+
usage: {
|
|
7475
|
+
in: 4900,
|
|
7476
|
+
out: 60,
|
|
7477
|
+
usd: .0056
|
|
7478
|
+
}
|
|
7479
|
+
};
|
|
7480
|
+
return {
|
|
7481
|
+
text: CLOSE,
|
|
7482
|
+
usage: {
|
|
7483
|
+
in: 9600,
|
|
7484
|
+
out: 360,
|
|
7485
|
+
cached: 4100,
|
|
7486
|
+
usd: .0191
|
|
7487
|
+
}
|
|
7488
|
+
};
|
|
7489
|
+
}
|
|
5724
7490
|
if (/\b3d\b|three dimensions/.test(q)) return {
|
|
5725
7491
|
text: "In 3D the same tree has eight children a node: an octree.",
|
|
5726
7492
|
usage: {
|
|
@@ -6632,7 +8398,7 @@ function writeHome(dir, { gateway = null, plugin = pluginDir } = {}) {
|
|
|
6632
8398
|
mkdirSync(join(dir, "themes"), { recursive: true });
|
|
6633
8399
|
mkdirSync(join(dir, "plugins"), { recursive: true });
|
|
6634
8400
|
const json = (value) => JSON.stringify(value, null, 2) + "\n";
|
|
6635
|
-
writeFileSync(join(dir, "opencode.json"), json(openCodeConfig(gateway)));
|
|
8401
|
+
writeFileSync(join(dir, "opencode.json"), json(withLocal(openCodeConfig(gateway), join(dir, "local.json"))));
|
|
6636
8402
|
writeFileSync(join(dir, "cli.json"), json({
|
|
6637
8403
|
$schema: "https://opencode.ai/cli.json",
|
|
6638
8404
|
theme: {
|
|
@@ -6651,6 +8417,28 @@ function writeHome(dir, { gateway = null, plugin = pluginDir } = {}) {
|
|
|
6651
8417
|
}
|
|
6652
8418
|
return dir;
|
|
6653
8419
|
}
|
|
8420
|
+
/** Your local.json over what isorite writes: its providers added to isorite's, everything else of yours taking the
|
|
8421
|
+
* place of isorite's. A local.json that is not JSON is said, and left out. */
|
|
8422
|
+
function withLocal(config, local) {
|
|
8423
|
+
if (!existsSync(local)) return config;
|
|
8424
|
+
let mine;
|
|
8425
|
+
try {
|
|
8426
|
+
mine = JSON.parse(readFileSync(local, "utf8"));
|
|
8427
|
+
} catch (e) {
|
|
8428
|
+
console.error(`isorite: ${local} is not JSON, so it is left out: ${e.message}`);
|
|
8429
|
+
return config;
|
|
8430
|
+
}
|
|
8431
|
+
const ours = config;
|
|
8432
|
+
const providers = {
|
|
8433
|
+
...ours.providers,
|
|
8434
|
+
...mine.providers
|
|
8435
|
+
};
|
|
8436
|
+
return {
|
|
8437
|
+
...ours,
|
|
8438
|
+
...mine,
|
|
8439
|
+
...Object.keys(providers).length ? { providers } : {}
|
|
8440
|
+
};
|
|
8441
|
+
}
|
|
6654
8442
|
function linksTo(path, target) {
|
|
6655
8443
|
try {
|
|
6656
8444
|
return lstatSync(path).isSymbolicLink() && readlinkSync(path) === target;
|
|
@@ -6703,7 +8491,7 @@ function lastAsked(request) {
|
|
|
6703
8491
|
}
|
|
6704
8492
|
async function startScripted(script) {
|
|
6705
8493
|
const requests = [];
|
|
6706
|
-
const server = createServer((req, res) => {
|
|
8494
|
+
const server = createServer$1((req, res) => {
|
|
6707
8495
|
let body = "";
|
|
6708
8496
|
req.on("data", (chunk) => body += chunk);
|
|
6709
8497
|
req.on("end", () => {
|
|
@@ -6891,7 +8679,7 @@ function tutorialScript(notes) {
|
|
|
6891
8679
|
newString: NOW
|
|
6892
8680
|
}
|
|
6893
8681
|
}
|
|
6894
|
-
][done] ?? { text: "barnes_hut.py builds the quadtree and walks it with θ = 0.5 `[1]`. Your note on the goal now says where the code is
|
|
8682
|
+
][done] ?? { text: "barnes_hut.py builds the quadtree and walks it with θ = 0.5 `[1]`. Your note on the goal now says where the code is, marked as mine `[2]`.\n\n```isorite\n[1] algorithms/barnes-hut\n[2] simulation/two-galaxies-colliding\n```" };
|
|
6895
8683
|
return { text: UNSCRIPTED };
|
|
6896
8684
|
};
|
|
6897
8685
|
}
|
|
@@ -6969,23 +8757,147 @@ async function ensureServer(path, { port = 4767, env = process.env, wait = 12e4
|
|
|
6969
8757
|
}
|
|
6970
8758
|
};
|
|
6971
8759
|
}
|
|
6972
|
-
|
|
6973
|
-
|
|
6974
|
-
|
|
8760
|
+
/** A port nobody is listening on, on this machine. */
|
|
8761
|
+
async function freePort() {
|
|
8762
|
+
return new Promise((done, fail) => {
|
|
8763
|
+
const probe = createServer().once("error", fail).listen(0, "127.0.0.1", () => {
|
|
8764
|
+
const { port } = probe.address();
|
|
8765
|
+
probe.close(() => done(port));
|
|
8766
|
+
});
|
|
8767
|
+
});
|
|
8768
|
+
}
|
|
8769
|
+
/** A POST to the isorite server, made as its page makes one: from its origin, with the token it hands out. */
|
|
8770
|
+
async function tell(url, path, body) {
|
|
8771
|
+
const { token } = await (await fetch(`${url}/api/session`)).json();
|
|
8772
|
+
const response = await fetch(url + path, {
|
|
8773
|
+
method: "POST",
|
|
8774
|
+
headers: {
|
|
8775
|
+
"Content-Type": "application/json",
|
|
8776
|
+
"x-isorite-token": token,
|
|
8777
|
+
Origin: url
|
|
8778
|
+
},
|
|
8779
|
+
body: JSON.stringify(body)
|
|
8780
|
+
});
|
|
8781
|
+
if (!response.ok) throw new Error((await response.json().catch(() => ({}))).error ?? `${response.status} from ${path}`);
|
|
8782
|
+
}
|
|
8783
|
+
/** OpenCode's own server, on this machine only, with a password of its own; its output kept in a log. The terminal
|
|
8784
|
+
* attaches to it, and so does the isorite server, which shows its session in the Iso pane (M11). */
|
|
8785
|
+
async function startOpenCode(command, { cwd, env, wait = 6e4 }) {
|
|
8786
|
+
const port = await freePort(), password = randomBytes(24).toString("base64url"), url = `http://127.0.0.1:${port}`;
|
|
8787
|
+
const log = join(tmpdir(), `isorite-opencode-${port}.log`);
|
|
8788
|
+
const out = openSync(log, "w");
|
|
8789
|
+
const server = spawn(command, [
|
|
8790
|
+
"serve",
|
|
8791
|
+
"--port",
|
|
8792
|
+
String(port),
|
|
8793
|
+
"--hostname",
|
|
8794
|
+
"127.0.0.1"
|
|
8795
|
+
], {
|
|
8796
|
+
cwd,
|
|
8797
|
+
stdio: [
|
|
8798
|
+
"ignore",
|
|
8799
|
+
out,
|
|
8800
|
+
out
|
|
8801
|
+
],
|
|
8802
|
+
env: {
|
|
8803
|
+
...env,
|
|
8804
|
+
OPENCODE_PASSWORD: password
|
|
8805
|
+
}
|
|
8806
|
+
});
|
|
8807
|
+
const auth = { authorization: "Basic " + Buffer.from(`opencode:${password}`).toString("base64") };
|
|
8808
|
+
const started = Date.now();
|
|
8809
|
+
for (;;) {
|
|
8810
|
+
if (server.exitCode !== null || Date.now() - started > wait) {
|
|
8811
|
+
server.kill();
|
|
8812
|
+
throw new StudyError(`OpenCode's server did not start; its output is in ${log}`);
|
|
8813
|
+
}
|
|
8814
|
+
try {
|
|
8815
|
+
if ((await fetch(url, {
|
|
8816
|
+
headers: auth,
|
|
8817
|
+
signal: AbortSignal.timeout(1e3)
|
|
8818
|
+
})).status < 500) break;
|
|
8819
|
+
} catch {}
|
|
8820
|
+
await new Promise((r) => setTimeout(r, 250));
|
|
8821
|
+
}
|
|
8822
|
+
return {
|
|
8823
|
+
url,
|
|
8824
|
+
password,
|
|
8825
|
+
stop: () => {
|
|
8826
|
+
server.kill();
|
|
8827
|
+
}
|
|
8828
|
+
};
|
|
8829
|
+
}
|
|
8830
|
+
/** The models the terminal uses: ISORITE_GATEWAY_URL and _MODELS when set; else Iso's own provider, as
|
|
8831
|
+
* ~/.config/isorite/config.toml has it (OpenRouter unless `[provider] url` names another), its models the tiers'
|
|
8832
|
+
* (mid, the default, then max and low), with the key it holds handed to OpenCode in the environment only. Null without a key: then
|
|
8833
|
+
* OpenCode's own models, or whatever your local.json names. */
|
|
8834
|
+
async function modelsFor(env) {
|
|
8835
|
+
const named = gatewayFrom(env);
|
|
8836
|
+
if (named) return {
|
|
8837
|
+
gateway: named,
|
|
8838
|
+
env
|
|
8839
|
+
};
|
|
8840
|
+
const path = configPath(env);
|
|
8841
|
+
const key = await new ConfigSecrets(path).get("provider.key");
|
|
8842
|
+
if (!key) return {
|
|
8843
|
+
gateway: null,
|
|
8844
|
+
env
|
|
8845
|
+
};
|
|
8846
|
+
const provider = providerConfig(path);
|
|
8847
|
+
const models = [.../* @__PURE__ */ new Set([
|
|
8848
|
+
provider.tiers.mid,
|
|
8849
|
+
provider.tiers.max,
|
|
8850
|
+
provider.tiers.low
|
|
8851
|
+
])];
|
|
8852
|
+
return {
|
|
8853
|
+
gateway: {
|
|
8854
|
+
url: provider.url,
|
|
8855
|
+
models
|
|
8856
|
+
},
|
|
8857
|
+
env: {
|
|
8858
|
+
...env,
|
|
8859
|
+
[GATEWAY_KEY]: key
|
|
8860
|
+
}
|
|
8861
|
+
};
|
|
8862
|
+
}
|
|
8863
|
+
async function term(path, { port = 4767, cwd = process.cwd(), env: given = process.env, wait = 12e4, onServer } = {}) {
|
|
8864
|
+
const opencode = findOpenCode(given);
|
|
8865
|
+
const { gateway, env } = await modelsFor(given);
|
|
6975
8866
|
const server = await ensureServer(path, {
|
|
6976
8867
|
port,
|
|
6977
|
-
env,
|
|
8868
|
+
env: given,
|
|
6978
8869
|
wait
|
|
6979
8870
|
});
|
|
6980
8871
|
onServer?.(server.url);
|
|
6981
8872
|
const home = writeHome(defaultHome(env), { gateway });
|
|
6982
|
-
const
|
|
8873
|
+
const shared = {
|
|
8874
|
+
...env,
|
|
8875
|
+
OPENCODE_CONFIG_DIR: home,
|
|
8876
|
+
ISORITE_URL: server.url
|
|
8877
|
+
};
|
|
8878
|
+
let backend;
|
|
8879
|
+
try {
|
|
8880
|
+
backend = await startOpenCode(opencode.command, {
|
|
8881
|
+
cwd,
|
|
8882
|
+
env: shared
|
|
8883
|
+
});
|
|
8884
|
+
} catch (e) {
|
|
8885
|
+
server.release();
|
|
8886
|
+
throw e;
|
|
8887
|
+
}
|
|
8888
|
+
await tell(server.url, "/api/terminal/attach", {
|
|
8889
|
+
url: backend.url,
|
|
8890
|
+
password: backend.password,
|
|
8891
|
+
directory: cwd
|
|
8892
|
+
}).catch((e) => console.error(`isorite: the Iso pane will not show the terminal: ${e.message}`));
|
|
8893
|
+
const child = spawn(opencode.command, ["--server", backend.url], {
|
|
6983
8894
|
cwd,
|
|
6984
8895
|
stdio: "inherit",
|
|
6985
8896
|
env: {
|
|
6986
|
-
...
|
|
8897
|
+
...given,
|
|
6987
8898
|
OPENCODE_CONFIG_DIR: home,
|
|
6988
|
-
ISORITE_URL: server.url
|
|
8899
|
+
ISORITE_URL: server.url,
|
|
8900
|
+
OPENCODE_PASSWORD: backend.password
|
|
6989
8901
|
}
|
|
6990
8902
|
});
|
|
6991
8903
|
const forward = (signal) => () => child.kill(signal);
|
|
@@ -6999,6 +8911,8 @@ async function term(path, { port = 4767, cwd = process.cwd(), env = process.env,
|
|
|
6999
8911
|
child.once("exit", (exit) => done(exit ?? 1));
|
|
7000
8912
|
});
|
|
7001
8913
|
for (const [signal, handler] of handlers) process.off(signal, handler);
|
|
8914
|
+
await tell(server.url, "/api/terminal/detach", {}).catch(() => void 0);
|
|
8915
|
+
backend.stop();
|
|
7002
8916
|
server.release();
|
|
7003
8917
|
return code;
|
|
7004
8918
|
}
|
|
@@ -7176,7 +9090,7 @@ async function mcp(path, { port = 4767, agent = "mcp", quiet = QUIET_MS, input =
|
|
|
7176
9090
|
}
|
|
7177
9091
|
//#endregion
|
|
7178
9092
|
//#region packages/server/src/main.ts
|
|
7179
|
-
const VERSION = "0.
|
|
9093
|
+
const VERSION = "0.2.0-trial.1";
|
|
7180
9094
|
const USAGE = `isorite: an agentic harness for understanding
|
|
7181
9095
|
|
|
7182
9096
|
isorite check <notes> check a folder of notes: frontmatter, links, cycles
|