kaizen-agent 1.33.1 → 1.34.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/cli/browser.test.ts +1 -0
- package/cli/browser.ts +12 -8
- package/cli/sources.test.ts +82 -3
- package/cli/sources.ts +153 -20
- package/cli/state.test.ts +31 -0
- package/cli/state.ts +10 -0
- package/cli/web.ts +50 -22
- package/package.json +1 -1
- package/skills/kaizen/gather.md +4 -1
- package/skills/kaizen/spec.md +5 -1
- package/web/board/api.ts +5 -2
- package/web/board/icebox.ts +26 -26
- package/web/board/live.ts +3 -1
- package/web/board/projects.ts +4 -1
- package/web/board/store.ts +2 -0
- package/web/board.html +10 -2
- package/web/board.js +18 -18
- package/web/notebook.js +1 -1
- package/web/term.js +1 -1
package/cli/browser.test.ts
CHANGED
|
@@ -31,6 +31,7 @@ test("an Instagram caption splits into author, date and the post's own words; no
|
|
|
31
31
|
expect(authorOf("https://x.com/ana/status/1")).toBe("@ana");
|
|
32
32
|
expect(authorOf("https://www.instagram.com/p/AAA")).toBe("");
|
|
33
33
|
expect(noteOf({ text: "t", author: "@ana", link: "https://x.com/ana/status/1", date: "" })).toBe("@ana · https://x.com/ana/status/1");
|
|
34
|
+
expect(noteOf({ text: "t", author: "@ana", link: "https://x.com/ana/status/1", date: "", images: ["https://pbs.example/a.jpg"] })).toBe("@ana · https://x.com/ana/status/1 img https://pbs.example/a.jpg");
|
|
34
35
|
});
|
|
35
36
|
|
|
36
37
|
test("no browser, or a link that is not social, is a note and not a crash; no profile is made", async () => {
|
package/cli/browser.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { join } from "node:path";
|
|
|
9
9
|
import { home } from "./state.ts";
|
|
10
10
|
import { socialOf, PLATFORMS } from "./sources.ts";
|
|
11
11
|
|
|
12
|
-
export type Post = { text: string; author: string; link: string; date: string };
|
|
12
|
+
export type Post = { text: string; author: string; link: string; date: string; images?: string[] };
|
|
13
13
|
export type Read = { ok: true; posts: Post[] } | { ok: false; note: string };
|
|
14
14
|
|
|
15
15
|
export const profileDir = () => join(home, ".kaizen", "browser");
|
|
@@ -131,7 +131,10 @@ const COLLECT = (re: RegExp) => `(() => {
|
|
|
131
131
|
if (time) names.add(time.innerText.trim());
|
|
132
132
|
const lines = (box.innerText || "").split("\\n").map((l) => l.trim()).filter(Boolean);
|
|
133
133
|
const text = own ? own.innerText : lines.filter((l) => !names.has(l) && !junk.test(l) && !when.test(l)).join("\\n");
|
|
134
|
-
|
|
134
|
+
// A post's own pictures, not avatars or icons: 100px across or more, https only.
|
|
135
|
+
const images = [...box.querySelectorAll("img")].filter((i) => Math.max(i.naturalWidth, i.width) >= 100)
|
|
136
|
+
.map((i) => i.currentSrc || i.src).filter((u) => u.startsWith("https://")).slice(0, 4);
|
|
137
|
+
posts.set(k, { link: location.origin + k, text, images, date: time ? (time.getAttribute("datetime") || time.innerText) : (lines.find((l) => when.test(l)) || "") });
|
|
135
138
|
}
|
|
136
139
|
return [...posts.values()];
|
|
137
140
|
})()`;
|
|
@@ -140,7 +143,7 @@ const COLLECT = (re: RegExp) => `(() => {
|
|
|
140
143
|
const CAPTION = `(() => {
|
|
141
144
|
const m = (p) => document.querySelector('meta[property="' + p + '"]')?.getAttribute("content") || "";
|
|
142
145
|
const time = document.querySelector("time");
|
|
143
|
-
return { text: m("og:description") || m("og:title"), date: time ? (time.getAttribute("datetime") || time.innerText) : "" };
|
|
146
|
+
return { text: m("og:description") || m("og:title"), image: m("og:image"), date: time ? (time.getAttribute("datetime") || time.innerText) : "" };
|
|
144
147
|
})()`;
|
|
145
148
|
|
|
146
149
|
// A sign-in page. OFFERS also counts a page that offers to sign in, which tells only
|
|
@@ -158,7 +161,8 @@ export function caption(raw: string): { text: string; author: string; date: stri
|
|
|
158
161
|
}
|
|
159
162
|
|
|
160
163
|
// A post's notes, as the icebox shows them: `@author · link · date`.
|
|
161
|
-
|
|
164
|
+
// Its images follow as `img <url>` tokens, which ingest takes out of the notes.
|
|
165
|
+
export const noteOf = (p: Post) => [p.author, p.link, p.date].filter(Boolean).join(" · ") + (p.images ?? []).map((u) => ` img ${u}`).join("");
|
|
162
166
|
|
|
163
167
|
export const authorOf = (link: string) => { try { const p = new URL(link).pathname.split("/").filter(Boolean); return p[0]!.startsWith("@") ? p[0]! : p.length > 2 ? "@" + p[0] : ""; } catch { return ""; } };
|
|
164
168
|
|
|
@@ -217,7 +221,7 @@ export async function readPage(page: Page, url: string, platform: string, max =
|
|
|
217
221
|
await page.go(url);
|
|
218
222
|
if (!onPlatform(await page.js<string>("location.href")) || (await page.js<boolean>(WALL))) return { ok: false, note: signInNote(platform) };
|
|
219
223
|
if (await page.js<boolean>(CHECK)) return { ok: false, note: checkNote(platform) };
|
|
220
|
-
const seen = new Map<string, { link: string; text: string; date: string; author?: string }>();
|
|
224
|
+
const seen = new Map<string, { link: string; text: string; date: string; author?: string; images?: string[] }>();
|
|
221
225
|
for (let i = 0, still = 0; i < 12 && seen.size < max && still < 3; i++) {
|
|
222
226
|
const before = seen.size;
|
|
223
227
|
for (const p of await page.js<any[]>(COLLECT(re)) ?? []) if (!seen.has(p.link) && onPlatform(p.link)) seen.set(p.link, p);
|
|
@@ -233,13 +237,13 @@ export async function readPage(page: Page, url: string, platform: string, max =
|
|
|
233
237
|
if (!onPlatform(p.link)) continue;
|
|
234
238
|
await page.go(p.link);
|
|
235
239
|
if (!onPlatform(await page.js<string>("location.href"))) continue;
|
|
236
|
-
const c = await page.js<{ text: string; date: string }>(CAPTION);
|
|
240
|
+
const c = await page.js<{ text: string; date: string; image: string }>(CAPTION);
|
|
237
241
|
const cap = caption(c?.text ?? "");
|
|
238
|
-
read.push({ link: p.link, text: cap.text || p.text, date: c?.date || cap.date || p.date, author: cap.author });
|
|
242
|
+
read.push({ link: p.link, text: cap.text || p.text, date: c?.date || cap.date || p.date, author: cap.author, images: c?.image?.startsWith("https://") ? [c.image] : p.images });
|
|
239
243
|
}
|
|
240
244
|
found = read;
|
|
241
245
|
}
|
|
242
|
-
const posts = found.map((p) => ({ link: p.link, author: p.author || authorOf(p.link), date: p.date, text: p.text.trim() })).filter((p) => p.text);
|
|
246
|
+
const posts = found.map((p) => ({ link: p.link, author: p.author || authorOf(p.link), date: p.date, text: p.text.trim(), ...(p.images?.length ? { images: p.images } : {}) })).filter((p) => p.text);
|
|
243
247
|
if (!posts.length && (await page.js<boolean>(CHECK))) return { ok: false, note: checkNote(platform) };
|
|
244
248
|
if (!posts.length && (await page.js<boolean>(OFFERS))) return { ok: false, note: signInNote(platform) };
|
|
245
249
|
return { ok: true, posts };
|
package/cli/sources.test.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { tmpdir } from "node:os";
|
|
|
7
7
|
import { readInbox, writeInbox, replaceIdea, parseItem } from "./state.ts";
|
|
8
8
|
import {
|
|
9
9
|
readSources, addSource, removeSource, ingest, checkUrl, resolvesPublic, exportUrl, fetchText, split, sift,
|
|
10
|
-
gatherSource, gatherAll, due, SIGN_IN, UNREADABLE, CAP, moveSource, gatherDirs, writeSources,
|
|
10
|
+
gatherSource, freshImages, gatherAll, due, takeImages, sheetImages, docImages, imagesUrl, SIGN_IN, UNREADABLE, CAP, moveSource, gatherDirs, writeSources,
|
|
11
11
|
readIcebox, writeIcebox, promote, dismiss, markReplied, setCategory, socialUrl, socialOf, AGENT_ONLY, PLATFORMS,
|
|
12
12
|
} from "./sources.ts";
|
|
13
13
|
|
|
@@ -230,7 +230,7 @@ test("promote: one or several items become one open idea with where they came fr
|
|
|
230
230
|
expect(readIcebox(state).map((s) => [s.status, s.idea])).toEqual([
|
|
231
231
|
["promoted", idea.text], ["promoted", idea.text], ["new", undefined],
|
|
232
232
|
]);
|
|
233
|
-
expect(promote(state, [a!.id])).toBe("already promoted
|
|
233
|
+
expect(promote(state, [a!.id])).toBe("already promoted");
|
|
234
234
|
expect(promote(state, [c!.id], " ")).toBeNull(); // blank text: the item's own
|
|
235
235
|
expect(readInbox(state).at(-1)!.text).toBe("Tambah personel");
|
|
236
236
|
expect(dismiss(state, ["nope0000"])).toBe("no such item");
|
|
@@ -634,10 +634,14 @@ test("the board refuses to add a source without a project, and moves one between
|
|
|
634
634
|
expect(await ice({ op: "promote", dir: a, ids: ["nope0000"] })).toMatchObject({ status: 404 });
|
|
635
635
|
expect(await ice({ op: "explode", dir: a, ids: [x!.id] })).toMatchObject({ status: 400 });
|
|
636
636
|
expect(await ice({ op: "promote", dir: a, ids: [x!.id] })).toMatchObject({ status: 200 });
|
|
637
|
-
expect(await ice({ op: "promote", dir: a, ids: [x!.id] })).toMatchObject({ status: 409, error: "already promoted
|
|
637
|
+
expect(await ice({ op: "promote", dir: a, ids: [x!.id] })).toMatchObject({ status: 409, error: "already promoted" });
|
|
638
638
|
expect(await ice({ op: "dismiss", dir: a, ids: [y!.id] })).toMatchObject({ status: 200 });
|
|
639
639
|
expect(readInbox(a).map((l) => l.text)).toEqual(["Dark mode"]);
|
|
640
640
|
expect(readIcebox(a).map((s) => s.status)).toEqual(["promoted", "dismissed"]);
|
|
641
|
+
// A dismissed item can be restored, then dismissed or promoted again.
|
|
642
|
+
expect(await ice({ op: "restore", dir: a, ids: [y!.id] })).toMatchObject({ status: 200 });
|
|
643
|
+
expect(readIcebox(a).map((s) => s.status)).toEqual(["promoted", "new"]);
|
|
644
|
+
expect(await ice({ op: "dismiss", dir: a, ids: [y!.id] })).toMatchObject({ status: 200 });
|
|
641
645
|
const listedIce = ((await (await fetch(at + "/sources")).json()) as any).icebox;
|
|
642
646
|
expect(listedIce).toEqual([]);
|
|
643
647
|
} finally { proc.kill(); await proc.exited; }
|
|
@@ -710,3 +714,78 @@ test("a GitHub issues link is read through the API: open issues, no pull request
|
|
|
710
714
|
// JSON only when asked: a plain source answering JSON is still refused.
|
|
711
715
|
expect(await fetchText(api, json, pub)).toEqual({ ok: false, note: UNREADABLE });
|
|
712
716
|
});
|
|
717
|
+
|
|
718
|
+
test("images ride beside a request, never in its key", () => {
|
|
719
|
+
addSource(state, SHEET);
|
|
720
|
+
expect(ingest(state, SHEET, [{ text: "Broken chart", notes: "@ana · https://x.com/ana/status/1 img https://pbs.example/a.jpg ", images: ["https://cdn.example/c.png", "http://plain/b.jpg"] }])).toEqual({ added: 1, held: 0 });
|
|
721
|
+
const [it] = readIcebox(state);
|
|
722
|
+
expect(it!.notes).toBe("@ana · https://x.com/ana/status/1");
|
|
723
|
+
expect(it!.images).toEqual(["https://cdn.example/c.png", "https://pbs.example/a.jpg"]);
|
|
724
|
+
// The same post read again with fresh CDN links is not new.
|
|
725
|
+
expect(ingest(state, SHEET, [{ text: "Broken chart", notes: "@ana · https://x.com/ana/status/1 img https://pbs.example/z.jpg" }])).toEqual({ added: 0, held: 0 });
|
|
726
|
+
expect(takeImages("a · b").images).toEqual([]);
|
|
727
|
+
expect(takeImages("img broken on page")).toEqual({ notes: "img broken on page", images: [] });
|
|
728
|
+
});
|
|
729
|
+
|
|
730
|
+
test("sheet and doc images land on their row and line", async () => {
|
|
731
|
+
const sheetHtml = `<table><tr><th></th><th>A</th></tr><tr><th id="0R0"><div class="row-header-wrapper">1</div></th><td>Request</td></tr>
|
|
732
|
+
<tr><th id="0R1"><div>2</div></th><td>Fix login</td><td><div><img src="https://lh3.googleusercontent.com/x?a=1&b=2"></div></td></tr>
|
|
733
|
+
<tr><th id="0R2"><div>3</div></th><td>Dark mode</td></tr></table>`;
|
|
734
|
+
expect(sheetImages(sheetHtml)).toEqual(new Map([[2, ["https://lh3.googleusercontent.com/x?a=1&b=2"]]]));
|
|
735
|
+
expect(sift("sheet", "Request\nFix login\nDark mode\n", sheetImages(sheetHtml) as any).items).toEqual([
|
|
736
|
+
{ text: "Fix login", images: ["https://lh3.googleusercontent.com/x?a=1&b=2"] }, { text: "Dark mode" }]);
|
|
737
|
+
const docHtml = `<html><body><p><span>Export is slow</span></p><p><img src="https://lh7.example/p.png"></p><li>Add & remove tags</li></body></html>`;
|
|
738
|
+
expect(sift("doc", "Export is slow\n\n* Add & remove tags\n", docImages(docHtml) as any).items).toEqual([
|
|
739
|
+
{ text: "Export is slow", images: ["https://lh7.example/p.png"] }, { text: "* Add & remove tags" }]);
|
|
740
|
+
expect(imagesUrl(SHEET)).toBe("https://docs.google.com/spreadsheets/d/abc123/htmlview/sheet?headers=true&gid=7");
|
|
741
|
+
expect(imagesUrl("https://docs.google.com/document/d/D0c_1/edit")).toBe("https://docs.google.com/document/d/D0c_1/export?format=html");
|
|
742
|
+
expect(imagesUrl("https://notes.example.com/raw/1")).toBeNull();
|
|
743
|
+
// A gather whose image page fails still gathers.
|
|
744
|
+
addSource(state, SHEET);
|
|
745
|
+
expect(await gatherSource(state, SHEET, text("Request\nFix login\n"), pub)).toMatchObject({ ok: true, added: 1 });
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
test("a waiting item takes fresh image links when gathered again", () => {
|
|
749
|
+
addSource(state, SHEET);
|
|
750
|
+
ingest(state, SHEET, [{ text: "Old request" }]);
|
|
751
|
+
ingest(state, SHEET, [{ text: "Old request", images: ["https://cdn.example/new.jpg"] }]);
|
|
752
|
+
expect(readIcebox(state)[0]!.images).toEqual(["https://cdn.example/new.jpg"]);
|
|
753
|
+
// No images read this time: the ones it has stay.
|
|
754
|
+
ingest(state, SHEET, [{ text: "Old request" }]);
|
|
755
|
+
expect(readIcebox(state)[0]!.images).toEqual(["https://cdn.example/new.jpg"]);
|
|
756
|
+
expect(sheetImages(`<tr><th id="0R4" class="row-headers-background"></th><td><img src="https://x.example/i.png"></td></tr>`)).toEqual(new Map([[5, ["https://x.example/i.png"]]]));
|
|
757
|
+
});
|
|
758
|
+
|
|
759
|
+
test("sheet images placed over the grid land on their anchored row", () => {
|
|
760
|
+
// The shape of a real htmlview/sheet page: rows in the table, floating images after it.
|
|
761
|
+
const html = `<table><tbody><tr><th id="0R0"><div>1</div></th><td>Request</td></tr><tr><th id="0R1"><div>2</div></th><td>Fix login</td></tr></tbody></table>`
|
|
762
|
+
+ `<div id='embed_11' class='waffle-embedded-object-overlay'><img src='https://docs.google.com/sheets-images-rt/A=s2048' style='display: block;'></div>`
|
|
763
|
+
+ `<div id='embed_22' class='waffle-embedded-object-overlay'><img src='https://docs.google.com/sheets-images-rt/B=s2048'></div>`
|
|
764
|
+
+ `<script>posObj('0', 'embed_11', 1, 7, 14, 9);posObj('0', 'embed_22', 1, 7, 7, 316);</script>`;
|
|
765
|
+
expect(sheetImages(html)).toEqual(new Map([[2, ["https://docs.google.com/sheets-images-rt/A=s2048", "https://docs.google.com/sheets-images-rt/B=s2048"]]]));
|
|
766
|
+
});
|
|
767
|
+
|
|
768
|
+
test("an item's images are read again from its source, since Google's links expire", async () => {
|
|
769
|
+
addSource(state, SHEET);
|
|
770
|
+
ingest(state, SHEET, [{ text: "Fix login", images: ["https://docs.google.com/sheets-images-rt/OLD=s2048"] }]);
|
|
771
|
+
const id = readIcebox(state)[0]!.id;
|
|
772
|
+
const html = `<table><tr><th id="0R0"><div>1</div></th><td>Request</td></tr><tr><th id="0R1"><div>2</div></th><td>Fix login</td><td><img src="https://docs.google.com/sheets-images-rt/NEW=s2048"></td></tr></table>`;
|
|
773
|
+
const page: any = async (u: string) => u.includes("htmlview")
|
|
774
|
+
? new Response(html, { headers: { "content-type": "text/html" } })
|
|
775
|
+
: new Response("Request\nFix login\n", { headers: { "content-type": "text/plain" } });
|
|
776
|
+
expect(await freshImages(state, id, page, pub)).toEqual(["https://docs.google.com/sheets-images-rt/NEW=s2048"]);
|
|
777
|
+
// The source cannot be read: the kept links come back.
|
|
778
|
+
expect(await freshImages(state, id, (async () => new Response("", { status: 500 })) as any, pub)).toEqual(["https://docs.google.com/sheets-images-rt/OLD=s2048"]);
|
|
779
|
+
expect(await freshImages(state, "nope", page, pub)).toEqual([]);
|
|
780
|
+
});
|
|
781
|
+
|
|
782
|
+
test("a fresh image link on a host the item never used is not taken", async () => {
|
|
783
|
+
addSource(state, SHEET);
|
|
784
|
+
ingest(state, SHEET, [{ text: "Fix login", images: ["https://docs.google.com/sheets-images-rt/OLD=s2048"] }]);
|
|
785
|
+
const id = readIcebox(state)[0]!.id;
|
|
786
|
+
const html = `<table><tr><th id="0R1"><div>2</div></th><td>Fix login</td><td><img src="https://evil.example/x.png"></td></tr></table>`;
|
|
787
|
+
const page: any = async (u: string) => u.includes("htmlview")
|
|
788
|
+
? new Response(html, { headers: { "content-type": "text/html" } })
|
|
789
|
+
: new Response("Request\nFix login\n", { headers: { "content-type": "text/plain" } });
|
|
790
|
+
expect(await freshImages(state, id, page, pub)).toEqual(["https://docs.google.com/sheets-images-rt/OLD=s2048"]);
|
|
791
|
+
});
|
package/cli/sources.ts
CHANGED
|
@@ -134,7 +134,7 @@ function clean(raw: string, extra = ""): { text: string; lines: string[] } | nul
|
|
|
134
134
|
// The icebox: everything gathered, held per project until a person or an agent
|
|
135
135
|
// promotes it to an idea or dismisses it. Promoted and dismissed items stay, so the
|
|
136
136
|
// same request never comes back.
|
|
137
|
-
export type Signal = { id: string; text: string; notes?: string; source: string; category?: string; at: string; status: "new" | "promoted" | "dismissed"; idea?: string; replied?: string };
|
|
137
|
+
export type Signal = { id: string; text: string; notes?: string; source: string; category?: string; at: string; status: "new" | "promoted" | "dismissed"; idea?: string; replied?: string; images?: string[] };
|
|
138
138
|
|
|
139
139
|
const iceFile = (state: string) => join(state, "icebox.json");
|
|
140
140
|
|
|
@@ -168,9 +168,30 @@ function ideaKey(l: { text: string; notes?: string }): string | null {
|
|
|
168
168
|
return normalise(l.text);
|
|
169
169
|
}
|
|
170
170
|
|
|
171
|
+
// Image links a source showed beside a request: https only, at most four. Kept apart
|
|
172
|
+
// from the text and notes and out of the row's key, since Google and social CDN
|
|
173
|
+
// addresses change between reads and would make every row new again.
|
|
174
|
+
export const pics = (urls: string[] = []) => [...new Set(urls.filter((u) => /^https:\/\/\S+$/.test(u) && u.length <= 2000))].slice(0, 4);
|
|
175
|
+
|
|
176
|
+
// `img <url>` tokens in an agent's notes (the --stdin path) are images, not notes.
|
|
177
|
+
export function takeImages(notes = ""): { notes: string; images: string[] } {
|
|
178
|
+
const images: string[] = [];
|
|
179
|
+
notes = notes.replace(/(^|\s)img\s+(https:\/\/\S+)/g, (_, sp, u) => { images.push(u); return sp; }).replace(/[ \t]*·[ \t]*$/gm, "").trim();
|
|
180
|
+
return { notes, images: pics(images) };
|
|
181
|
+
}
|
|
182
|
+
|
|
171
183
|
// The single place outside text enters kaizen, and it enters the icebox, never Ideas.
|
|
172
184
|
// Synchronous on purpose: nothing may run between reading the files and writing them.
|
|
173
|
-
|
|
185
|
+
// A gathered item as the icebox keeps it, keyed the way dedup sees it.
|
|
186
|
+
function shape(it: Item) {
|
|
187
|
+
const got = takeImages(it.notes);
|
|
188
|
+
const c = clean(it.text, got.notes);
|
|
189
|
+
if (!c) return null;
|
|
190
|
+
const notes = c.lines.join("\n").slice(0, 2000);
|
|
191
|
+
return { c, notes, images: pics([...(it.images ?? []), ...got.images]), key: rowKey(c.text, notes) };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function ingest(state: string, url: string, items: Item[]): { added: number; held: number } {
|
|
174
195
|
const sources = readSources(state);
|
|
175
196
|
const src = sources.find((s) => s.url === url);
|
|
176
197
|
if (!src) throw new Error("not a listed source");
|
|
@@ -178,19 +199,25 @@ export function ingest(state: string, url: string, items: { text: string; notes?
|
|
|
178
199
|
const known = new Set([...src.seen, ...readInbox(state).map(ideaKey).filter((k) => k !== null), ...ice.map((s) => rowKey(s.text, s.notes))]);
|
|
179
200
|
const fresh: Signal[] = [];
|
|
180
201
|
const seen: string[] = [];
|
|
181
|
-
let held = 0;
|
|
202
|
+
let held = 0, refreshed = false;
|
|
182
203
|
for (const it of items) {
|
|
183
|
-
const
|
|
184
|
-
if (!
|
|
185
|
-
const notes =
|
|
186
|
-
|
|
187
|
-
|
|
204
|
+
const s = shape(it);
|
|
205
|
+
if (!s) continue;
|
|
206
|
+
const { c, notes, images, key } = s;
|
|
207
|
+
if (known.has(key)) {
|
|
208
|
+
seen.push(key);
|
|
209
|
+
// Image links expire (social CDNs) or arrive late (items gathered before images
|
|
210
|
+
// were read): an item still waiting takes the ones read now.
|
|
211
|
+
const old = images.length && ice.find((s) => s.status === "new" && s.source === url && rowKey(s.text, s.notes) === key);
|
|
212
|
+
if (old && String(old.images) !== String(images)) { old.images = images; refreshed = true; }
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
188
215
|
if (fresh.length >= CAP) { held++; continue; } // not marked seen: the next gather takes it
|
|
189
216
|
known.add(key);
|
|
190
217
|
seen.push(key);
|
|
191
|
-
fresh.push({ id: idOf(key), text: c.text, ...(notes ? { notes } : {}), source: url, ...(src.category ? { category: src.category } : {}), at: new Date().toISOString(), status: "new" });
|
|
218
|
+
fresh.push({ id: idOf(key), text: c.text, ...(notes ? { notes } : {}), source: url, ...(src.category ? { category: src.category } : {}), at: new Date().toISOString(), status: "new", ...(images.length ? { images } : {}) });
|
|
192
219
|
}
|
|
193
|
-
if (fresh.length) writeIcebox(state, [...ice, ...fresh]);
|
|
220
|
+
if (fresh.length || refreshed) writeIcebox(state, [...ice, ...fresh]);
|
|
194
221
|
// ponytail: seen only grows; trim to the newest few thousand if a source ever gets that long.
|
|
195
222
|
src.seen = [...new Set([...src.seen, ...seen])];
|
|
196
223
|
writeSources(state, sources);
|
|
@@ -207,7 +234,7 @@ export function promote(state: string, ids: string[], text?: string, details?: s
|
|
|
207
234
|
const picked = ids.map((id) => ice.find((s) => s.id === id));
|
|
208
235
|
if (!ids.length) return "no items named";
|
|
209
236
|
if (picked.some((s) => !s)) return "no such item";
|
|
210
|
-
if (picked.some((s) => s!.status
|
|
237
|
+
if (picked.some((s) => s!.status === "promoted")) return "already promoted";
|
|
211
238
|
const own = !!text?.trim();
|
|
212
239
|
const c = clean(own ? text! : picked[0]!.text);
|
|
213
240
|
if (!c) return "the idea text is too short";
|
|
@@ -227,6 +254,17 @@ export function promote(state: string, ids: string[], text?: string, details?: s
|
|
|
227
254
|
return null;
|
|
228
255
|
}
|
|
229
256
|
|
|
257
|
+
// A dismissed item back to waiting.
|
|
258
|
+
export function restore(state: string, ids: string[]): string | null {
|
|
259
|
+
const ice = readIcebox(state);
|
|
260
|
+
const picked = ids.map((id) => ice.find((s) => s.id === id));
|
|
261
|
+
if (!ids.length) return "no items named";
|
|
262
|
+
if (picked.some((s) => !s)) return "no such item";
|
|
263
|
+
for (const s of picked) if (s!.status === "dismissed") s!.status = "new";
|
|
264
|
+
writeIcebox(state, ice);
|
|
265
|
+
return null;
|
|
266
|
+
}
|
|
267
|
+
|
|
230
268
|
export function dismiss(state: string, ids: string[]): string | null {
|
|
231
269
|
const ice = readIcebox(state);
|
|
232
270
|
const picked = ids.map((id) => ice.find((s) => s.id === id));
|
|
@@ -308,6 +346,59 @@ export function exportUrl(raw: string): { url: string; kind: Kind } {
|
|
|
308
346
|
return { url: raw, kind: "text" };
|
|
309
347
|
}
|
|
310
348
|
|
|
349
|
+
// Where a Google sheet or doc shows its images, which its text export drops. A
|
|
350
|
+
// published sheet is left out.
|
|
351
|
+
// ponytail: /d/e/ published sheets get no images; add pubhtml when one asks.
|
|
352
|
+
export function imagesUrl(raw: string): string | null {
|
|
353
|
+
const u = new URL(raw);
|
|
354
|
+
if (u.hostname !== "docs.google.com") return null;
|
|
355
|
+
const t = exportUrl(raw);
|
|
356
|
+
if (t.kind === "sheet" && t.url.includes("/export?")) return t.url.replace("/export?format=tsv&", "/htmlview/sheet?headers=true&");
|
|
357
|
+
if (t.kind === "doc") return t.url.replace("format=txt", "format=html");
|
|
358
|
+
return null;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const srcs = (html: string) => [...html.matchAll(/<img\b[^>]*?\ssrc=(?:"([^"]+)"|'([^']+)')/gi)].map((m) => (m[1] ?? m[2])!.replace(/&/g, "&"));
|
|
362
|
+
const plain = (html: string) => html.replace(/<[^>]+>/g, " ").replace(/ /g, " ").replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/'/g, "'");
|
|
363
|
+
const lineKey = (t: string) => normalise(t.replace(/^\s*[-*•●○■]\s*/, ""));
|
|
364
|
+
|
|
365
|
+
// A sheet's htmlview: the images in each row, by the row number in its header cell.
|
|
366
|
+
export function sheetImages(html: string): Map<number, string[]> {
|
|
367
|
+
const out = new Map<number, string[]>();
|
|
368
|
+
for (const tr of html.split(/<\/table>/i)[0]!.split(/<tr\b/i).slice(1)) {
|
|
369
|
+
// The header cell shows the row number; its id (`0R4`) counts from zero.
|
|
370
|
+
const shown = /<th\b[^>]*>(?:\s*<[^>]+>)*\s*(\d+)\s*</i.exec(tr)?.[1];
|
|
371
|
+
const id = /<th\b[^>]*\bid="\d+R(\d+)"/i.exec(tr)?.[1];
|
|
372
|
+
const row = shown ? Number(shown) : id ? Number(id) + 1 : 0;
|
|
373
|
+
const got = row ? srcs(tr) : [];
|
|
374
|
+
if (got.length) out.set(row, got);
|
|
375
|
+
}
|
|
376
|
+
// Images placed over the grid sit after the table, each in its own embed box, and
|
|
377
|
+
// the page anchors each box to a row with `posObj(sheet, 'embed_<id>', row, col, …)`,
|
|
378
|
+
// the row counted from zero.
|
|
379
|
+
const anchor = new Map([...html.matchAll(/posObj\(\s*'[^']*'\s*,\s*'(embed_\d+)'\s*,\s*(\d+)/g)].map((m) => [m[1]!, Number(m[2]) + 1]));
|
|
380
|
+
for (const box of html.split(/(?=<div\b[^>]*\bid=['"]embed_\d+['"])/i).slice(1)) {
|
|
381
|
+
const row = anchor.get(/id=['"](embed_\d+)['"]/.exec(box)![1]!);
|
|
382
|
+
const got = row ? srcs(box.split(/<div\b[^>]*\bid=['"](?!embed_)/i)[0]!) : [];
|
|
383
|
+
if (got.length) out.set(row!, [...(out.get(row!) ?? []), ...got]);
|
|
384
|
+
}
|
|
385
|
+
return out;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// A doc's html export: each paragraph's images, keyed by its text. An image on a line
|
|
389
|
+
// of its own belongs to the paragraph above it.
|
|
390
|
+
export function docImages(html: string): Map<string, string[]> {
|
|
391
|
+
const out = new Map<string, string[]>();
|
|
392
|
+
let last = "";
|
|
393
|
+
for (const part of html.split(/<\/(?:p|h\d|li)>/i)) {
|
|
394
|
+
const text = lineKey(plain(part.replace(/[\s\S]*<body[^>]*>/i, "")));
|
|
395
|
+
if (text) last = text;
|
|
396
|
+
const got = srcs(part);
|
|
397
|
+
if (got.length && last) out.set(last, [...(out.get(last) ?? []), ...got]);
|
|
398
|
+
}
|
|
399
|
+
return out;
|
|
400
|
+
}
|
|
401
|
+
|
|
311
402
|
export const SIGN_IN = 'Needs sign-in. Share it as "anyone with the link can view", or run /kaizen gather in your agent.';
|
|
312
403
|
export const UNREADABLE = "Kaizen cannot read this page on its own. Run /kaizen gather in your agent.";
|
|
313
404
|
const MAX_BYTES = 1_000_000;
|
|
@@ -316,7 +407,7 @@ type Fetch = (url: string, init: RequestInit) => Promise<Response>;
|
|
|
316
407
|
|
|
317
408
|
// No cookie, no authorization, no redirect followed unchecked: every hop goes
|
|
318
409
|
// through the same guard as the first address.
|
|
319
|
-
export async function fetchText(url: string, doFetch: Fetch = fetch, lookup: Lookup = dnsLookup, json = false): Promise<{ ok: true; body: string } | { ok: false; note: string }> {
|
|
410
|
+
export async function fetchText(url: string, doFetch: Fetch = fetch, lookup: Lookup = dnsLookup, json: boolean | "html" = false): Promise<{ ok: true; body: string } | { ok: false; note: string }> {
|
|
320
411
|
const signal = AbortSignal.timeout(15000);
|
|
321
412
|
for (let hop = 0; hop <= 5; hop++) {
|
|
322
413
|
const why = checkUrl(url);
|
|
@@ -326,7 +417,7 @@ export async function fetchText(url: string, doFetch: Fetch = fetch, lookup: Loo
|
|
|
326
417
|
const pub = await resolvesPublic(host, lookup);
|
|
327
418
|
if (pub === null) return { ok: false, note: hop ? "The link redirected to an address that could not be found." : "The link's address could not be found. Check it for a typo." };
|
|
328
419
|
if (!pub) return { ok: false, note: "Refused: not a public address." };
|
|
329
|
-
const res = await doFetch(url, { redirect: "manual", credentials: "omit", signal, headers: { accept: json ? "application/json" : "text/plain, text/*" } });
|
|
420
|
+
const res = await doFetch(url, { redirect: "manual", credentials: "omit", signal, headers: { accept: json === "html" ? "text/html" : json ? "application/json" : "text/plain, text/*" } });
|
|
330
421
|
if (res.status >= 300 && res.status < 400) {
|
|
331
422
|
const to = res.headers.get("location");
|
|
332
423
|
if (!to) return { ok: false, note: `Redirect with nowhere to go (HTTP ${res.status}).` };
|
|
@@ -336,7 +427,7 @@ export async function fetchText(url: string, doFetch: Fetch = fetch, lookup: Loo
|
|
|
336
427
|
if (res.status === 401 || res.status === 403) return { ok: false, note: SIGN_IN };
|
|
337
428
|
if (!res.ok) return { ok: false, note: `The link answered HTTP ${res.status}.` };
|
|
338
429
|
const type = (res.headers.get("content-type") ?? "").toLowerCase();
|
|
339
|
-
if (json ? !type.startsWith("application/json") : !type.startsWith("text/") || type.startsWith("text/html")) return { ok: false, note: UNREADABLE };
|
|
430
|
+
if (json === "html" ? !type.startsWith("text/html") : json ? !type.startsWith("application/json") : !type.startsWith("text/") || type.startsWith("text/html")) return { ok: false, note: UNREADABLE };
|
|
340
431
|
// Read no further than the cap; a cut-off last line is dropped, not gathered half.
|
|
341
432
|
const chunks: Uint8Array[] = [];
|
|
342
433
|
let size = 0;
|
|
@@ -360,19 +451,22 @@ const wordy = (c: string) => c.length >= 3 && !datum(c);
|
|
|
360
451
|
const STATUS = /^status$/i;
|
|
361
452
|
const DONE = /^(done|finish|finished)$/i;
|
|
362
453
|
|
|
363
|
-
type Item = { text: string; notes?: string };
|
|
454
|
+
type Item = { text: string; notes?: string; images?: string[] };
|
|
364
455
|
|
|
365
456
|
export const split = (kind: Kind, body: string): Item[] => sift(kind, body).items;
|
|
366
457
|
|
|
367
458
|
// The requests, and how many rows were left out for their status. A row left out is
|
|
368
459
|
// never handed to ingest, so it is not marked seen: reopened, it is gathered then.
|
|
369
|
-
|
|
460
|
+
// `pics`, when given, are the images found beside the rows: by sheet row number for a
|
|
461
|
+
// sheet, by line text for a doc.
|
|
462
|
+
export function sift(kind: Kind, body: string, pics?: Map<number | string, string[]>): { items: Item[]; done: number } {
|
|
463
|
+
const with_ = (it: Item, at: number | string): Item => { const p = pics?.get(at); return p?.length ? { ...it, images: p } : it; };
|
|
370
464
|
// Pull requests come back from the issues API too; they are not requests. The notes
|
|
371
465
|
// are the number and link only, so an edited body does not make the issue new.
|
|
372
466
|
if (kind === "github") return { items: (JSON.parse(body) as any[]).filter((i) => !i.pull_request && typeof i.title === "string")
|
|
373
467
|
.map((i) => ({ text: i.title, notes: `#${i.number} · ${i.html_url}` })), done: 0 };
|
|
374
468
|
const rows = body.split(/\r?\n/);
|
|
375
|
-
if (kind !== "sheet") return { items: rows.filter((r) => r.trim()).map((text) => ({ text })), done: 0 };
|
|
469
|
+
if (kind !== "sheet") return { items: rows.filter((r) => r.trim()).map((text) => with_({ text }, lineKey(text))), done: 0 };
|
|
376
470
|
const cells = rows.map((r) => r.split("\t").map((c) => c.trim()));
|
|
377
471
|
const filled = cells.filter((row) => row.some(Boolean));
|
|
378
472
|
// Sheets often open with blank rows or a title: the header is looked for in the
|
|
@@ -404,7 +498,8 @@ export function sift(kind: Kind, body: string): { items: Item[]; done: number }
|
|
|
404
498
|
if (!text || (known < 0 && !wordy(text))) return [];
|
|
405
499
|
if (status >= 0 && DONE.test(row[status] ?? "")) { done++; return []; }
|
|
406
500
|
const rest = row.filter((c, i) => c && i !== col).join(" · ");
|
|
407
|
-
|
|
501
|
+
// The export keeps every row from the top, so a row's index is its number less one.
|
|
502
|
+
return [with_({ text, ...(rest ? { notes: rest } : {}) }, cells.indexOf(row) + 1)];
|
|
408
503
|
});
|
|
409
504
|
return { items, done };
|
|
410
505
|
}
|
|
@@ -429,6 +524,39 @@ export function record(state: string, url: string, r: { added: number; held: num
|
|
|
429
524
|
return { url, ...last };
|
|
430
525
|
}
|
|
431
526
|
|
|
527
|
+
// An item's images read again from its source now. Google signs sheet and doc image
|
|
528
|
+
// links with a token that expires, so a link kept since the gather answers 403; this
|
|
529
|
+
// reads the page again and returns the same row's links. Falls back to the kept ones.
|
|
530
|
+
// A source's page is read once a minute however many items and images ask, and a
|
|
531
|
+
// fresh link is only taken on a host the kept links already used.
|
|
532
|
+
const pages = new Map<string, { at: number; p: Promise<Map<string, string[]> | null> }>();
|
|
533
|
+
async function readPage(source: string, doFetch?: Fetch, lookup?: Lookup) {
|
|
534
|
+
const target = exportUrl(source), at = imagesUrl(source)!;
|
|
535
|
+
const [got, page] = await Promise.all([fetchText(target.url, doFetch, lookup), fetchText(at, doFetch, lookup, "html")]);
|
|
536
|
+
if (!got.ok || !page.ok) return null;
|
|
537
|
+
const found = target.kind === "sheet" ? sheetImages(page.body) : docImages(page.body);
|
|
538
|
+
const byKey = new Map<string, string[]>();
|
|
539
|
+
for (const x of sift(target.kind, got.body, found as Map<number | string, string[]>).items) {
|
|
540
|
+
const s = shape(x);
|
|
541
|
+
if (s?.images.length) byKey.set(s.key, s.images);
|
|
542
|
+
}
|
|
543
|
+
return byKey;
|
|
544
|
+
}
|
|
545
|
+
export async function freshImages(state: string, id: string, doFetch?: Fetch, lookup?: Lookup): Promise<string[]> {
|
|
546
|
+
const it = readIcebox(state).find((s) => s.id === id);
|
|
547
|
+
if (!it) return [];
|
|
548
|
+
const kept = it.images ?? [];
|
|
549
|
+
if (!imagesUrl(it.source)) return kept;
|
|
550
|
+
const now = Date.now();
|
|
551
|
+
for (const [k, v] of pages) if (now - v.at >= 60_000) pages.delete(k);
|
|
552
|
+
const hit = doFetch ? undefined : pages.get(it.source); // a test's own fetch is never cached
|
|
553
|
+
const p = hit?.p ?? readPage(it.source, doFetch, lookup).catch(() => null);
|
|
554
|
+
if (!hit && !doFetch) pages.set(it.source, { at: now, p });
|
|
555
|
+
const hosts = new Set(kept.map((u) => new URL(u).hostname));
|
|
556
|
+
const fresh = ((await p)?.get(rowKey(it.text, it.notes)) ?? []).filter((u) => hosts.has(new URL(u).hostname));
|
|
557
|
+
return fresh.length ? fresh : kept;
|
|
558
|
+
}
|
|
559
|
+
|
|
432
560
|
// Never throws. A failure is recorded on the source and marks nothing seen.
|
|
433
561
|
export async function gatherSource(state: string, url: string, doFetch?: Fetch, lookup?: Lookup): Promise<Result> {
|
|
434
562
|
if (socialOf(url)) {
|
|
@@ -438,9 +566,14 @@ export async function gatherSource(state: string, url: string, doFetch?: Fetch,
|
|
|
438
566
|
}
|
|
439
567
|
try {
|
|
440
568
|
const target = exportUrl(url);
|
|
441
|
-
|
|
569
|
+
// Images are extra, read alongside the text: a page that cannot be read for them
|
|
570
|
+
// leaves the gather as it was, and costs no time beyond the text's own.
|
|
571
|
+
const at = imagesUrl(url);
|
|
572
|
+
const [got, page] = await Promise.all([fetchText(target.url, doFetch, lookup, target.kind === "github"),
|
|
573
|
+
at ? fetchText(at, doFetch, lookup, "html").catch(() => null) : null]);
|
|
442
574
|
if (got.ok) {
|
|
443
|
-
const
|
|
575
|
+
const found = page?.ok ? (target.kind === "sheet" ? sheetImages(page.body) : docImages(page.body)) : undefined;
|
|
576
|
+
const { items, done } = sift(target.kind, got.body, found as Map<number | string, string[]> | undefined);
|
|
444
577
|
return record(state, url, ingest(state, url, items), target.kind === "github" ? undefined : got.body.split("\n").filter((r) => r.trim()).length, done);
|
|
445
578
|
}
|
|
446
579
|
const last = { at: new Date().toISOString(), ok: false, added: 0, note: got.note };
|
package/cli/state.test.ts
CHANGED
|
@@ -687,6 +687,16 @@ test("ownerOf: an idea's session belongs to the first run created after it, not
|
|
|
687
687
|
expect(ownerOf(st, { ...rec, run: "2026-10-01-x" })).toBe("2026-10-01-x");
|
|
688
688
|
});
|
|
689
689
|
|
|
690
|
+
test("ownerOf: a run that recorded the launch's session id owns it, however its request is worded", () => {
|
|
691
|
+
const p = sessionProject(), st = join(p, ".kaizen");
|
|
692
|
+
newRun(p, "2026-10-02-reworded", "/kaizen something else entirely");
|
|
693
|
+
const f = join(st, "runs", "2026-10-02-reworded", "state.json");
|
|
694
|
+
writeFileSync(f, JSON.stringify({ ...JSON.parse(readFileSync(f, "utf8")), session: "abc" }));
|
|
695
|
+
const rec = { key: "k", run: null, text: "open it on my phone", cmd: "claude", sessionId: "abc", started: Date.now() };
|
|
696
|
+
expect(ownerOf(st, rec)).toBe("2026-10-02-reworded");
|
|
697
|
+
expect(ownerOf(st, { ...rec, sessionId: "other" })).toBe(null);
|
|
698
|
+
});
|
|
699
|
+
|
|
690
700
|
test("findTerminal records the agent's PID: a POSIX wrapper that execs it, a PowerShell prefix on Windows", () => {
|
|
691
701
|
const keys = ["PATH", "HERDR_ENV", "TMUX", "TERMINAL", "SystemRoot", "windir", "ComSpec"];
|
|
692
702
|
const saved = keys.map((k) => process.env[k]);
|
|
@@ -998,3 +1008,24 @@ test("the board refuses an upgrade from another origin", async () => {
|
|
|
998
1008
|
expect(r.status).toBe(403);
|
|
999
1009
|
} finally { proc.kill(); await proc.exited; rmSync(homeDir, { recursive: true, force: true }); }
|
|
1000
1010
|
});
|
|
1011
|
+
|
|
1012
|
+
// KAIZEN_WEB_HOSTS (tailscale serve): the listed host gets in, any other is still refused.
|
|
1013
|
+
test("the board answers only the hosts listed in KAIZEN_WEB_HOSTS", async () => {
|
|
1014
|
+
const homeDir = mkdtempSync(join(tmpdir(), "kaizen-hosts-"));
|
|
1015
|
+
for (const d of [join(homeDir, ".kaizen"), join(homeDir, ".claude")]) mkdirSync(d, { recursive: true });
|
|
1016
|
+
const port = 20000 + Math.floor(Math.random() * 20000), repo = join(import.meta.dir, "..");
|
|
1017
|
+
const proc = Bun.spawn([process.execPath, join(repo, "cli", "install.ts"), "web", "--port", String(port), "--no-open", "--foreground"], {
|
|
1018
|
+
cwd: homeDir, env: { ...process.env, HOME: homeDir, USERPROFILE: homeDir, KAIZEN_HOME: repo, KAIZEN_WEB_HOSTS: " PC.tail1234.ts.net:443 , " }, stdout: "ignore", stderr: "ignore",
|
|
1019
|
+
});
|
|
1020
|
+
try {
|
|
1021
|
+
const at = `http://127.0.0.1:${port}`;
|
|
1022
|
+
for (let i = 0; ; i++) { try { await fetch(at + "/state"); break; } catch { if (i > 100) throw new Error("board did not start"); await Bun.sleep(50); } }
|
|
1023
|
+
expect((await fetch(at + "/state", { headers: { host: "pc.tail1234.ts.net" } })).status).toBe(200);
|
|
1024
|
+
expect((await fetch(at + "/state", { headers: { host: "evil.example" } })).status).toBe(403);
|
|
1025
|
+
expect((await fetch(at + "/state", { headers: { host: "other.tail1234.ts.net" } })).status).toBe(403);
|
|
1026
|
+
expect((await fetch(at + "/upgrade", { method: "POST", headers: { origin: "https://evil.example" } })).status).toBe(403);
|
|
1027
|
+
const m = await fetch(at + "/manifest.webmanifest");
|
|
1028
|
+
expect(m.status).toBe(200);
|
|
1029
|
+
expect((await m.json()).display).toBe("standalone");
|
|
1030
|
+
} finally { proc.kill(); await proc.exited; rmSync(homeDir, { recursive: true, force: true }); }
|
|
1031
|
+
});
|
package/cli/state.ts
CHANGED
|
@@ -1121,6 +1121,16 @@ export function readSessions(st: string): Session[] {
|
|
|
1121
1121
|
// request holds the idea's text: one run per record, never every run that mentions it.
|
|
1122
1122
|
export function ownerOf(st: string, s: Session): string | null {
|
|
1123
1123
|
if (s.run) return s.run;
|
|
1124
|
+
// The launch's own conversation id, recorded by the run at intake: holds however the
|
|
1125
|
+
// request was worded. Text matching below is for tools that expose no id.
|
|
1126
|
+
if (s.sessionId) {
|
|
1127
|
+
try {
|
|
1128
|
+
const mine = readdirSync(join(st, "runs")).filter((id) => {
|
|
1129
|
+
try { return JSON.parse(readText(join(st, "runs", id, "state.json"))).session === s.sessionId; } catch { return false; }
|
|
1130
|
+
}).sort();
|
|
1131
|
+
if (mine.length) return mine[0]!;
|
|
1132
|
+
} catch { /* no runs dir */ }
|
|
1133
|
+
}
|
|
1124
1134
|
const t = normalise(s.text);
|
|
1125
1135
|
let best: string | null = null, at = Infinity;
|
|
1126
1136
|
try {
|