@chalky/chalky 0.1.4 → 0.1.5

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.
Files changed (104) hide show
  1. package/README.md +10 -5
  2. package/{app → agent}/SYSTEM.md +15 -11
  3. package/agent/extensions/app/README.md +68 -0
  4. package/{app/extensions/ui → agent/extensions/app}/agents.js +22 -16
  5. package/agent/extensions/app/guide.md +5 -0
  6. package/agent/extensions/app/index.ts +66 -0
  7. package/{app/extensions/ui/skills/agent-apps → agent/extensions/app/skills/app-agents}/SKILL.md +17 -18
  8. package/{app/extensions/boards → agent/extensions/board}/browser.js +25 -23
  9. package/{app/extensions/boards → agent/extensions/board}/guide.md +1 -1
  10. package/{app/extensions/boards → agent/extensions/board}/index.ts +32 -30
  11. package/agent/extensions/card/README.md +88 -0
  12. package/agent/extensions/card/blocks/Button.js +34 -0
  13. package/{app/extensions/ui/components → agent/extensions/card/blocks}/Column.js +2 -2
  14. package/agent/extensions/card/blocks/Markdown.js +37 -0
  15. package/{app/extensions/ui/components/Card.js → agent/extensions/card/blocks/Panel.js} +3 -3
  16. package/{app/extensions/ui/components → agent/extensions/card/blocks}/Row.js +2 -2
  17. package/{app/extensions/ui/components → agent/extensions/card/blocks}/TextField.js +3 -3
  18. package/{app/extensions/ui → agent/extensions/card}/browser.js +155 -129
  19. package/agent/extensions/card/guide.md +13 -0
  20. package/agent/extensions/card/index.ts +370 -0
  21. package/{app/extensions/commands → agent/extensions/command}/browser.js +3 -3
  22. package/agent/extensions/command/guide.md +2 -0
  23. package/{app/extensions/commands → agent/extensions/command}/index.ts +14 -16
  24. package/agent/extensions/doodle/README.md +57 -0
  25. package/agent/extensions/doodle/browser.js +1013 -0
  26. package/agent/extensions/doodle/guide.md +22 -0
  27. package/agent/extensions/doodle/index.ts +274 -0
  28. package/agent/extensions/file/browser.js +143 -0
  29. package/agent/extensions/file/guide.md +5 -0
  30. package/{app/extensions/files → agent/extensions/file}/index.ts +34 -16
  31. package/{app/extensions/frames → agent/extensions/frame}/README.md +13 -13
  32. package/{app/extensions/frames → agent/extensions/frame}/browser.js +20 -18
  33. package/agent/extensions/frame/guide.md +3 -0
  34. package/{app/extensions/frames → agent/extensions/frame}/index.ts +27 -31
  35. package/{app/extensions/ui_history → agent/extensions/history}/browser.js +18 -18
  36. package/{app/extensions/ui_history → agent/extensions/history}/index.ts +32 -33
  37. package/{app → agent}/extensions/mcp/browser.js +1 -1
  38. package/agent/extensions/mcp/guide.md +3 -0
  39. package/{app → agent}/extensions/mcp/index.ts +2 -4
  40. package/{app/extensions/login → agent/extensions/model}/browser.js +67 -67
  41. package/{app → agent}/extensions/model/index.ts +8 -4
  42. package/{app/extensions/login/index.ts → agent/extensions/model/login.ts} +47 -46
  43. package/agent/extensions/note/README.md +35 -0
  44. package/agent/extensions/note/browser.js +356 -0
  45. package/agent/extensions/note/guide.md +8 -0
  46. package/agent/extensions/note/index.ts +192 -0
  47. package/{app → agent}/extensions/page/index.ts +2 -2
  48. package/{app → agent}/extensions/page/page.html +55 -49
  49. package/agent/extensions/prompt/index.ts +38 -0
  50. package/{app → agent}/extensions/reload/index.ts +3 -3
  51. package/{app → agent}/extensions/screen/browser.js +4 -4
  52. package/{app → agent}/extensions/screen/index.ts +11 -11
  53. package/{app/extensions/steps → agent/extensions/step}/index.ts +1 -1
  54. package/{app/extensions/swarm → agent/extensions/team}/README.md +19 -19
  55. package/{app/extensions/swarm → agent/extensions/team}/guide.md +4 -4
  56. package/{app/extensions/swarm → agent/extensions/team}/index.ts +41 -66
  57. package/{app/extensions/versions → agent/extensions/version}/browser.js +10 -10
  58. package/{app/extensions/versions → agent/extensions/version}/index.ts +39 -54
  59. package/{app → agent}/extensions/viewer/README.md +4 -4
  60. package/{app → agent}/extensions/viewer/browser.js +8 -8
  61. package/agent/extensions/viewer/guide.md +3 -0
  62. package/{app → agent}/extensions/viewer/index.ts +22 -17
  63. package/{app → agent}/extensions/voice/browser.js +4 -3
  64. package/{app → agent}/extensions/voice/index.ts +8 -6
  65. package/{app/extensions/ui → agent/extensions/voice}/voice.js +5 -5
  66. package/agent/settings.json +8 -0
  67. package/{app/extensions/ui/skills/ui-check → agent/skills/browser-check}/SKILL.md +4 -4
  68. package/{app/extensions/ui/skills/ui-check → agent/skills/browser-check}/scripts/check.mjs +1 -1
  69. package/{app/extensions/ui/skills/ui-design → agent/skills/design}/SKILL.md +14 -14
  70. package/agent/skills/extension-map/SKILL.md +56 -0
  71. package/{app → agent}/skills/write-skill/SKILL.md +1 -1
  72. package/bin/chalky.mjs +2 -2
  73. package/package.json +8 -8
  74. package/server.ts +151 -58
  75. package/app/extensions/appdata/README.md +0 -10
  76. package/app/extensions/appdata/guide.md +0 -4
  77. package/app/extensions/appdata/index.ts +0 -47
  78. package/app/extensions/commands/guide.md +0 -3
  79. package/app/extensions/doodle/README.md +0 -15
  80. package/app/extensions/doodle/browser.js +0 -398
  81. package/app/extensions/doodle/guide.md +0 -6
  82. package/app/extensions/doodle/index.ts +0 -54
  83. package/app/extensions/files/browser.js +0 -96
  84. package/app/extensions/files/guide.md +0 -7
  85. package/app/extensions/frames/guide.md +0 -7
  86. package/app/extensions/mcp/guide.md +0 -5
  87. package/app/extensions/point/README.md +0 -22
  88. package/app/extensions/point/browser.js +0 -247
  89. package/app/extensions/point/guide.md +0 -6
  90. package/app/extensions/point/index.ts +0 -112
  91. package/app/extensions/prompt/index.ts +0 -19
  92. package/app/extensions/ui/README.md +0 -69
  93. package/app/extensions/ui/components/App.js +0 -28
  94. package/app/extensions/ui/components/Button.js +0 -28
  95. package/app/extensions/ui/components/Markdown.js +0 -37
  96. package/app/extensions/ui/guide.md +0 -7
  97. package/app/extensions/ui/index.ts +0 -291
  98. package/app/extensions/viewer/guide.md +0 -5
  99. package/app/mcp.json +0 -3
  100. package/app/settings.json +0 -3
  101. package/app/skills/plugin-map/SKILL.md +0 -49
  102. package/app/ui/apps/.keep +0 -0
  103. /package/{app/extensions/ui/components → agent/extensions/card/blocks}/Table.js +0 -0
  104. /package/{app/extensions/ui/components → agent/extensions/card/blocks}/Text.js +0 -0
package/README.md CHANGED
@@ -34,13 +34,18 @@ Then open http://127.0.0.1:3000.
34
34
 
35
35
  `~/.chalky` (or `CHALKY_HOME`) holds:
36
36
 
37
- - `app/`: the agents' code, copied from the package on the first run. The agents edit it, and each change is
38
- versioned. A newer version of the package doesn't overwrite it.
39
- - `state/`: conversations, version history, what the apps keep.
40
- - `workspace/`: the agents' working folder.
37
+ - `agent/`: what the agents are: their prompt, settings and extensions (the page among them), copied from the
38
+ package on the first run. The agents edit it. A newer version of the package doesn't overwrite it.
39
+ - `built/`: what the agents made for you: the cards on your boards, and the apps they wrote.
40
+ - `data/`: what is kept while running: conversations, what you typed into cards and apps, notes, undo history.
41
+ - `workspace/`: the agents' working folder; `workspace/drops/` holds your files.
42
+
43
+ `agent/` and `built/` share one version history: each agent turn that changes them is saved, and a rollback puts
44
+ both back. A home from before this layout (`app/`, `state/`) is moved into it on the first start; the old code
45
+ stays in `app-before-reorg/`.
41
46
 
42
47
  ## MCP servers
43
48
 
44
- Tools from MCP servers go in `~/.chalky/app/mcp.json`, in pi's format (stdio or streamable HTTP; see pi's
49
+ Tools from MCP servers go in `~/.chalky/agent/mcp.json`, in pi's format (stdio or streamable HTTP; see pi's
45
50
  `docs/mcp.md`). Type `/mcp` on the page to choose which servers and tools the agents may use. Keep secrets in
46
51
  environment variables and refer to them as `${NAME}` in `headers` or `env`.
@@ -1,15 +1,19 @@
1
- You are an agent in Chalky. You build what the user sees: the page is a workspace where what you build (UI
2
- surfaces and apps) fills the screen, and the conversation is a side panel. The user says what they want; you put a
1
+ You are an agent in Chalky. You build what the user sees: the page is a workspace where what you build (cards on
2
+ boards, each showing blocks or an app) fills the screen, and the conversation is a side panel. The user says what they want; you put a
3
3
  working first version on screen; then they point at things on it and leave notes, and you change exactly that.
4
4
  - Build, don't describe. A first version they can point at beats questions you could answer by building.
5
5
  - Your final reply is the only text they read in full: one or two sentences on what changed.
6
6
  - The page around your work is the user's: change it only when asked. Build inside the main space.
7
7
 
8
8
  What follows this: the skills you can load, your working folder (<cwd>: bash runs there), one short section for
9
- each part of you that has something to say (each comes from an extension), and <paths>: where your folders
10
- ({code}, {work}, {data}), the host and pi's docs are. In these texts and the README.md files, {code}/..., {work}/...
11
- and {data}/... are paths in those folders. A section that names a README.md is the short version of its
12
- extension: read that file when the section says, once (it stays in your conversation).
9
+ each part of you that has something to say (each comes from an extension), and <paths>: your four folders, the
10
+ host and pi's docs. In README.md files and skills, {agent}/..., {built}/..., {data}/... and {workspace}/... are paths
11
+ in those folders. A section that names a README.md is the short version of its extension: read that file when the
12
+ section says, once (it stays in your conversation). Some tools are loaded only when needed: tool_search finds and
13
+ loads them (card_blocks, viewer_read, screen_see, team_reset, MCP servers' tools). Tools you already have need no
14
+ search.
15
+ Two kinds of change, two places: what the user asks you to make (a card, an app) goes in {built}; a change to how
16
+ Chalky itself works is a change to you, in {agent}.
13
17
 
14
18
  Your tools:
15
19
  - read to look at files and pictures (not cat or sed); bash for ls, grep, find and running things.
@@ -20,11 +24,11 @@ Your tools:
20
24
  - Name files by their full path when you mention them.
21
25
 
22
26
  Changing yourself: when the work needs it, you can change any part of you, the page included. You are pi
23
- extensions, one folder each: {code}/extensions/<name>/. In it, index.ts is `export default function (pi:
24
- ExtensionAPI) { ... }`, which registers tools (pi.registerTool), prompt sections and event handlers (pi.on); and as
25
- the extension needs them, guide.md (its short prompt section), README.md (the whole of it), browser.js (its part
26
- of the page) and skills/. The page itself is the page extension; this text is {code}/SYSTEM.md; pi's settings (e.g. compaction) are
27
- {code}/settings.json (pi's docs/settings.md). Extensions reach
27
+ extensions, one folder each: {agent}/extensions/<name>/. In it, index.ts is `export default function (pi:
28
+ ExtensionAPI) { ... }`, which registers tools (pi.registerTool) and event handlers (pi.on); and as the extension
29
+ needs them, guide.md (its prompt section, found by itself), README.md (the whole of it), browser.js (its part of
30
+ the page, found by itself) and skills/. The page itself is the page extension; this text is {agent}/SYSTEM.md;
31
+ pi's settings (e.g. compaction) are {agent}/settings.json (pi's docs/settings.md). Extensions reach
28
32
  shared state and the web server through globalThis.chalky: the host, the fixed core under them (don't edit it).
29
33
  Read pi's extension docs (<paths>) before writing or changing an extension. After editing, call the reload tool:
30
34
  your extensions load again when your turn ends, and your conversation is kept. A changed page or browser.js shows
@@ -0,0 +1,68 @@
1
+ # Making an app (the app extension)
2
+ Everything a usual app needs is here: for one, you don't need the card README or the design and browser-check skills.
3
+
4
+ ## Write it, show it
5
+ - Write {built}/apps/<name>/index.html (other files next to it), then card_show(card=<id>, app=<name>, about="...").
6
+ It is served at {page}/built/apps/<name>/. card_show checks it on the board and reports errors and whether it
7
+ fits its card: fix those and show it again. height=<px>: its height when nothing else sizes it (default 400).
8
+ - about: one or two sentences on what it is for and any decision a later change should respect (its back side).
9
+ - Name what the user may point at: ids or data-name on elements ("habit-Read", not "row3"). A canvas: give the
10
+ page's window a partAt(x, y) returning {"name", ...} for what is at that spot.
11
+ - The user moves and resizes the card: fill the space (%, flex, grid, an SVG with a viewBox), not fixed px.
12
+ - A pinned card (the user held its label): card_show on it fails. Make a new card (made_from=[it]) or ask.
13
+ - Changing it later: edit the part (the edit tool), not a rewrite. App files show when your turn ends.
14
+
15
+ ## Keep its data
16
+ `import agents from "/app/agents.js"` (a module script). `await agents.load()` gives the saved data ({} at first);
17
+ `agents.save(data)` replaces it after every change (on the server, {data}/app/<name>.json: kept across reloads, the
18
+ same in every browser, readable by you with cat). Other cards showing the same app hear each save:
19
+ `agents.on("app_data", ({app, data}) => { if (app === agents.app) ... })` (the field is `data`). Never localStorage
20
+ (one browser only, invisible to you). To change it yourself: curl -X PUT --data @new.json {page}/app/data/<name>.
21
+
22
+ ## An app that asks an AI (a plan from a goal, a summary, an answer)
23
+ A real agent does it, never a canned sample. While building, spawn one for the card: team_spawn(role, name="planner",
24
+ app=<card id>), its role ending "Answer only with JSON like {...}. Don't use tools." In the app:
25
+ ```
26
+ const asked = Date.now() / 1000; agents.send("planner", goal);
27
+ agents.on("message", (ev) => { // events replay on connect: take only the answer to this question
28
+ if (ev.agent !== "planner" || ev.role !== "assistant" || ev.tool_calls?.length || ev.ts < asked) return;
29
+ data.plan = JSON.parse(ev.text.slice(ev.text.indexOf("{"), ev.text.lastIndexOf("}") + 1)); agents.save(data); draw();
30
+ });
31
+ ```
32
+ Show that it is thinking while it waits; agents.send is limited per minute. Agents with personalities that talk to
33
+ each other (a debate, a simulation): load the app-agents skill.
34
+
35
+ ## Apps that work together
36
+ Each card is its own page: a click in one never reaches another. One app saves what was picked
37
+ (`agents.save({...data, picked: "Goblet squat"})`); the other hears every app's saves:
38
+ `agents.on("app_data", ({app, data}) => { if (app === "fitflow") show(data.picked) })`, and can read the first one's
39
+ data at start with `fetch("/app/data/fitflow").then((r) => r.json())`.
40
+
41
+ ## Look
42
+ The page is light, glassy and quiet; match it unless the user asks for another look:
43
+ - Transparent background; the system font (`font: inherit` or system-ui); text #1d1d1f, muted rgba(60,60,67,.55),
44
+ accent #0071e3, danger #e5484d, lines rgba(0,0,0,.06), glass rgba(255,255,255,.7), radius 16px.
45
+ - Show just the thing: no frame or panel around it unless it needs one. Group with flex/grid and gap.
46
+ - Open in a working state (its real data, or a marked example), never an empty shell; `tabular-nums` for digits.
47
+ - Buttons say what happens ("Add habit"); short plain words; status colours always with a word or icon.
48
+ - Avoid the generic AI look: cream and terracotta, purple gradients, emoji as markers, the same shadowed card on
49
+ everything. For a chart, a diagram, a game's art or a restyle, load the design skill.
50
+
51
+ ## Check it works
52
+ card_show covers errors and fit. Before telling the user what it does, try its main action and a reload with
53
+ {agent}/skills/browser-check/scripts/check.mjs:
54
+ ```
55
+ node <that check.mjs> {page}/built/apps/<name>/index.html "eval:(()=>{const i=document.querySelector('#new'); i.value='Test-1';
56
+ i.dispatchEvent(new Event('input',{bubbles:true})); i.form.requestSubmit(); return 'ok'})()" wait:800 \
57
+ "eval:document.body.innerText.includes('Test-1')"
58
+ ```
59
+ Set values with eval (`type:` adds to what a field already holds). Run it again to see what a reload keeps.
60
+ Your test writes the user's real data: note it first (cat {data}/app/<name>.json) and put it back afterwards
61
+ (PUT, above). Say only what you saw working.
62
+
63
+ ## More
64
+ - Calculations (totals, streaks, dates) belong in the page's code, never worked out by you and typed in.
65
+ - Voice input: import /voice/voice.js (read its header), not the browser's speech API.
66
+ - Libraries: put an ES-module build in {agent}/extensions/card/lib/<pkg>/ and import /card/lib/<pkg>/<file>.js.
67
+ - agents.action(name, context) wakes the agent that owns the card (for what truly needs you); the rest of the
68
+ bridge: the header of {agent}/extensions/app/agents.js.
@@ -1,18 +1,19 @@
1
- // Bridge from an app ({code}/ui/apps/<name>/, shown by the App component) to the agents. An app imports it:
2
- // import agents from "/ui/agents.js";
1
+ // Bridge from an app ({built}/apps/<name>/, shown on a card) to the agents. An app imports it:
2
+ // import agents from "/app/agents.js";
3
3
  // agents.send(to, text) a message to an agent, from "app"; its answer comes as a "message" event
4
4
  // (role "assistant", no tool_calls). Each one is a model call: limited per minute.
5
- // agents.action(name, context) an action for the agent that owns this app's surface, like a Button
5
+ // agents.action(name, context) an action for the agent that owns this app's card, like a Button
6
6
  // agents.on(type, fn) events as the page gets them, past ones first (replayed on connect), then live:
7
- // "message" {agent, role, content, tool_calls, ts}: every message of every agent,
8
- // incl. its ask_agent / tell calls (who talks to whom); "status" {agents}: who is busy
7
+ // "message" {agent, role, content, tool_calls, ts, to, text}: every message of every agent;
8
+ // to and text: its "[to: <name>]" tag ("all" without one) and the words after it;
9
+ // incl. its team_ask / team_tell calls (who talks to whom); "status" {agents}: who is busy
9
10
  // agents.list() {name: {role, app, state, doing, queued}}, from the latest "status" event
10
- // agents.reset() empty the memories of this app's agents (spawned with app=<this surface>),
11
+ // agents.reset() empty the memories of this app's agents (spawned with app=<this card>),
11
12
  // roles kept: a fresh round. Refused (an "error" event) while one is busy.
12
- // agents.load() this app's saved data ({} at first), from the appdata plugin (on the server)
13
+ // agents.load() this app's saved data ({} at first), kept by the app extension (on the server)
13
14
  // agents.save(data) replace it; every page and app frame then gets "app_data" {app, data}, so
14
- // the same app in other surfaces updates (check ev.app === agents.app)
15
- // agents.app this app's name: its folder in {code}/ui/apps
15
+ // the same app on other cards updates (check ev.app === agents.app)
16
+ // agents.app this app's name: its folder in {built}/apps
16
17
  // It opens its own WebSocket to /ws, the page's protocol (the page extension).
17
18
  const ws = new WebSocket((location.protocol === "https:" ? "wss://" : "ws://") + location.host + "/ws");
18
19
  const open = new Promise((resolve) => ws.addEventListener("open", resolve));
@@ -22,23 +23,28 @@ let status = {};
22
23
  ws.onmessage = (e) => {
23
24
  const ev = JSON.parse(e.data);
24
25
  if (ev.type === "status") status = ev.agents;
26
+ if (ev.type === "message" && typeof ev.content === "string") { // an app agent's "[to: <name>] ..." answer, read once here
27
+ const tag = /^\s*\[to:\s*([^\]]+)\]\s*/i.exec(ev.content);
28
+ ev.to = tag ? tag[1].trim() : "all";
29
+ ev.text = tag ? ev.content.slice(tag[0].length) : ev.content;
30
+ }
25
31
  for (const fn of listeners[ev.type] || []) fn(ev);
26
32
  };
27
33
 
28
- // In the versions plugin's screenshot (/?shot) the app is only looked at: send nothing, no model calls.
34
+ // In the version extension's screenshot (/?shot) the app is only looked at: send nothing, no model calls.
29
35
  const shot = new URLSearchParams(window.top.location.search).has("shot");
30
36
  const post = async (obj) => { if (shot) return; await open; ws.send(JSON.stringify(obj)); };
31
- const app = location.pathname.split("/")[3]; // /ui/apps/<name>/...
32
- const store = `/ui-api/app-data/${app}`;
33
- // the surface this app is shown in (the App component's frame sits inside it on the page)
34
- const surface = () => window.frameElement?.closest("[data-surface]")?.dataset.surface;
37
+ const app = location.pathname.split("/")[3]; // /built/apps/<name>/...
38
+ const store = `/app/data/${app}`;
39
+ // the card this app is shown on (its frame sits inside the card on the page)
40
+ const card = () => window.frameElement?.closest("[data-card]")?.dataset.card;
35
41
 
36
42
  export default {
37
43
  send: (to, text) => post({ type: "app_message", to, text }),
38
- action: (name, context = {}) => post({ type: "ui_action", surface: surface(), component: "app", name, context }),
44
+ action: (name, context = {}) => post({ type: "card_action", card: card(), block: "app", name, context }),
39
45
  on: (type, fn) => (listeners[type] ??= []).push(fn),
40
46
  list: () => status,
41
- reset: () => post({ type: "app_reset", surface: surface() }),
47
+ reset: () => post({ type: "app_reset", card: card() }),
42
48
  app,
43
49
  load: async () => (await fetch(store, { cache: "no-store" })).json(),
44
50
  save: async (data) => { if (!shot) await fetch(store, { method: "PUT", body: JSON.stringify(data) }); },
@@ -0,0 +1,5 @@
1
+ # Apps
2
+ An app is a page you write in {built}/apps/<name>/ and show on a card (card_show with app). It keeps what the user
3
+ makes in it (notes, lists, picks) with /app/agents.js (agents.load, agents.save), on the server and shared by every
4
+ card showing it; never localStorage (one browser only, and you can't see it).
5
+ Before writing or changing an app, read README.md: it has all an app needs (showing it, its data, its look, checking it).
@@ -0,0 +1,66 @@
1
+ // Apps: pages the agents write in built/apps/<name>/ (the host serves them at /built/apps/<name>/), shown on a card
2
+ // (the card extension draws the frame). This extension gives apps what a plain page can't have:
3
+ // - their data, kept on the server: one JSON value per app in data/app/<name>.json, so it outlives the page, is
4
+ // the same in every browser, and the agents can read it. Whole value replaced each time: the last save wins
5
+ // (keep one app's data in one value). The same app on several cards shares it: every save is an "app_data" event;
6
+ // - agents.js, their bridge to the agents (load, save, send, action, reset, on: read its header);
7
+ // - the app-agents skill (apps with agents inside, e.g. a simulation's members).
8
+ // GET /app/data/<name> the app's data ({} if none yet) GET /app/agents.js the bridge
9
+ // PUT /app/data/<name> replace it (JSON, at most MAX_BYTES); event "app_data" {app, data}
10
+ // page messages app_message {to, text}: to an agent, from "app" (at most MESSAGES_PER_MINUTE, all apps together)
11
+ // app_reset {card}: empty the memories of the agents spawned for the app on that card (the team's reset)
12
+ import fs from "node:fs";
13
+ import path from "node:path";
14
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
15
+
16
+ const host = (globalThis as any).chalky;
17
+ const DIR = path.join(host.data, "app");
18
+ const MAX_BYTES = 1024 * 1024;
19
+ const MESSAGES_PER_MINUTE = 30;
20
+ const sent: number[] = host.shared.get("app-sent") ?? []; // when apps sent messages (on the host: outlives reloads)
21
+ host.shared.set("app-sent", sent);
22
+ const json = (res: any, v: unknown, status = 200) => res.writeHead(status, { "content-type": "application/json" }).end(JSON.stringify(v));
23
+
24
+ export default function (pi: ExtensionAPI) {
25
+ pi.on("resources_discover", () => ({ skillPaths: [path.join(import.meta.dirname, "skills")] }));
26
+
27
+ host.routes.set("/app/agents.js", (_req: any, res: any) => host.file(res, path.join(import.meta.dirname, "agents.js")));
28
+ host.routes.set("/app/data/", (req: any, res: any) => {
29
+ const name = decodeURIComponent(new URL(req.url, "http://x").pathname.slice("/app/data/".length));
30
+ if (!/^[\w-]+$/.test(name)) return json(res, { error: "app names are letters, digits, _ and -" }, 400);
31
+ const p = path.join(DIR, name + ".json");
32
+ if (req.method === "GET") {
33
+ if (!fs.existsSync(p)) return json(res, {});
34
+ return res.writeHead(200, { "content-type": "application/json", "cache-control": "no-store" }).end(fs.readFileSync(p));
35
+ }
36
+ if (req.method !== "PUT") return res.writeHead(405).end();
37
+ const chunks: Buffer[] = [];
38
+ let size = 0;
39
+ req.on("data", (c: Buffer) => { size += c.length; if (size <= MAX_BYTES) chunks.push(c); });
40
+ req.on("end", () => {
41
+ if (size > MAX_BYTES) return json(res, { error: `over ${MAX_BYTES / 1024} KB` }, 400);
42
+ let data;
43
+ try { data = JSON.parse(Buffer.concat(chunks).toString("utf8")); } catch (e: any) { return json(res, { error: `not JSON: ${e.message}` }, 400); }
44
+ fs.mkdirSync(DIR, { recursive: true });
45
+ fs.writeFileSync(p + ".tmp", JSON.stringify(data));
46
+ fs.renameSync(p + ".tmp", p);
47
+ host.broadcast("app_data", { app: name, data });
48
+ json(res, { saved: true });
49
+ });
50
+ });
51
+
52
+ host.pageHandlers.set("app", async (msg: any) => {
53
+ const team = host.api.team, error = (text: string) => host.broadcast("error", { agent: "app", text });
54
+ if (msg.type === "app_reset" && team) {
55
+ const names = [...host.agents.values()].filter((x: any) => x.app === msg.card).map((x: any) => x.name);
56
+ const problem = await team.reset(names);
57
+ if (problem) error(`reset of ${msg.card}: ${problem}`);
58
+ } else if (msg.type === "app_message" && team) {
59
+ const now = Date.now();
60
+ sent.splice(0, sent.length, ...sent.filter((t) => now - t < 60000));
61
+ if (!host.agents.has(msg.to)) error(`app message to unknown agent '${msg.to}'`);
62
+ else if (sent.length >= MESSAGES_PER_MINUTE) error(`app message to ${msg.to} dropped: more than ${MESSAGES_PER_MINUTE} a minute`);
63
+ else { sent.push(now); team.send("app", msg.to, msg.text ?? ""); }
64
+ }
65
+ });
66
+ }
@@ -1,6 +1,6 @@
1
1
  ---
2
- name: agent-apps
3
- description: Build an app whose members are real AI agents (a simulated board meeting, a debate, a negotiation, a panel of experts, game characters), run and shown by the app itself, in turns or all at once. Use when the user wants agents inside what you build, as opposed to agents helping you build it.
2
+ name: app-agents
3
+ description: Build an app whose members are real AI agents (a simulated board meeting, a debate, a negotiation, a panel of experts, game characters), run and shown by the app itself, in turns or all at once. Use when the user wants agents inside what you build, as opposed to agents helping you build it. (One agent that just answers the app's questions: the app README's recipe is enough.)
4
4
  ---
5
5
  # Apps with real agents inside
6
6
 
@@ -8,8 +8,8 @@ description: Build an app whose members are real AI agents (a simulated board me
8
8
  | | Worker agents | App agents |
9
9
  |-------------------|---------------------------------|----------------------------------------------|
10
10
  | What for | help you build or fix the app | are part of the app (a board member, a debater) |
11
- | Spawn | `spawn_agent(role)` | `spawn_agent(role, name, app="<surface>")` |
12
- | Who talks to them | you (`tell`, `ask_agent`) | the app, through `/ui/agents.js` |
11
+ | Spawn | `team_spawn(role)` | `team_spawn(role, name, app="<card>")` |
12
+ | Who talks to them | you (`team_tell`, `team_ask`) | the app, through `/app/agents.js` |
13
13
  | Messages panel | shown | hidden: the app shows their talk |
14
14
  | When they run | while you build | each time the user runs the app |
15
15
 
@@ -19,7 +19,7 @@ is what keeps app agents out of the user's messages panel and lets the app reset
19
19
  ## Why the app runs the activity, not you
20
20
  Your context is large (system prompt plus everything you did while building): each of your model calls
21
21
  costs all of it, often 50k tokens or more. If you relay a meeting yourself (ask the chair who's next, ask
22
- that member, `ui_update` the transcript), every line spoken costs two or three of your calls. The app
22
+ that member, `card_update` the transcript), every line spoken costs two or three of your calls. The app
23
23
  can talk to its agents directly, and their contexts are small. So you build, fix, and answer the user;
24
24
  at run time you make no calls at all.
25
25
 
@@ -28,17 +28,17 @@ at run time you make no calls at all.
28
28
  stance and personality. End each role with: "You are a character in <app>. Answer in plain text,
29
29
  briefly, starting with [to: <name>] or [to: all]. Don't use tools." App agents have the same tools
30
30
  you have; this keeps them in character, fast, and gives the app a "to" to draw.
31
- 2. **Build the UI as an App**: `ui/apps/<name>/index.html` in your code folder (the one your prompt names, with
32
- `extensions/` in it), and its scripts, shown with
33
- `ui_render("<surface>", [{"id": "root", "component": "App", "src": "<name>/index.html"}])`.
34
- An App can import `/ui/agents.js`; the bridge finds the app's surface from the frame it runs in.
35
- 3. **Spawn the members once**, while building: `spawn_agent(role, name=..., app="<surface>")`, the same
36
- string as the surface id. In the app, check `agents.list()`: if a member is missing, show that
31
+ 2. **Build it as an app**: `{built}/apps/<name>/index.html` and its scripts, shown on a card with
32
+ `card_show(card="<card>", app="<name>")`.
33
+ An app can import `/app/agents.js`; the bridge finds the app's card from the frame it runs in.
34
+ 3. **Spawn the members once**, while building: `team_spawn(role, name=..., app="<card>")`, the same
35
+ string as the card id. In the app, check `agents.list()`: if a member is missing, show that
37
36
  instead of failing.
38
- 4. **Wire the app with `/ui/agents.js`** (read its header):
37
+ 4. **Wire the app with `/app/agents.js`** (read its header: {agent}/extensions/app/agents.js):
39
38
  - `send(to, text)`: ask a member. Its answer arrives as a `"message"` event: `agent` is that member,
40
39
  `role` is `"assistant"`, and there are no `tool_calls` (its final answer).
41
- - `on("message", fn)`: draw each answer as it arrives (speaker, the `[to: …]` target, the text).
40
+ - `on("message", fn)`: draw each answer as it arrives: `ev.agent` (the speaker), `ev.to` (its target,
41
+ "all" when it gave none) and `ev.text` (the answer without its `[to: …]` tag; show this, not `ev.content`).
42
42
  - `reset()`: a fresh round, members' memories empty and roles kept. Call it before each run if the
43
43
  user wants rounds to start fresh.
44
44
  - `action(name, context)`: only for what needs you, e.g. "add a member" (you spawn it).
@@ -48,11 +48,11 @@ at run time you make no calls at all.
48
48
  - All at once: send to several members together; answers come back in any order, so draw each one
49
49
  as it arrives.
50
50
  Members only know what you send them: include the transcript so far (or its last lines) each time.
51
- 6. **Check it** with the ui-check skill: one short run, budget 2 or 3 messages (each is a real model call).
51
+ 6. **Check it** with the browser-check skill: one short run, budget 2 or 3 messages (each is a real model call).
52
52
 
53
53
  ## The ask helper (copy into the app)
54
54
  ```js
55
- import agents from "/ui/agents.js";
55
+ import agents from "/app/agents.js";
56
56
  const waiting = {}; // member -> resolve() for its next final answer
57
57
  agents.on("message", (ev) => {
58
58
  if (ev.role === "assistant" && !ev.tool_calls?.length && waiting[ev.agent]) {
@@ -76,7 +76,7 @@ answer from an earlier run must not resolve a new question.
76
76
  with play, pause and a scrubber.
77
77
  - After a reload, past events replay: rebuild earlier runs from them (only your members' messages).
78
78
  A member's `"system"` message means a fresh memory, i.e. a new round began.
79
- - Don't keep the transcript in the surface's data with `ui_update`: that is you relaying again.
79
+ - Don't keep the transcript in the card's data with `card_update`: that is you relaying again.
80
80
 
81
81
  ## Limits
82
82
  - App messages: at most 30 a minute (each is a model call); more are dropped with an `"error"` event.
@@ -86,6 +86,5 @@ answer from an earlier run must not resolve a new question.
86
86
 
87
87
  ## Pitfalls
88
88
  - Disable Start while a run is going: a double click starts two runs.
89
- - An answer without a `[to: …]` tag: treat it as to all.
90
89
  - If the app shows nothing, the panel won't help (app agents are hidden there): check the app with
91
- ui-check, e.g. `eval:` the length of its transcript, and look for `"error"` events.
90
+ browser-check, e.g. `eval:` the length of its transcript, and look for `"error"` events.
@@ -1,16 +1,16 @@
1
- // Browser half of boards: the main space as numbered boards, and the shelf to move between them (above the input).
1
+ // The page side of the board extension: the main space as numbered boards, and the shelf to move between them (above the input).
2
2
  // Each board is a container in #top. The one you're on is display: contents (its things sit in the main space as
3
3
  // before); the others are laid out but hidden, so apps in them keep running and the shelf can draw them.
4
- // Any element with data-board="n" that lands in #top (a surface, a frame) is moved into board n's container, and
4
+ // Any element with data-board="n" that lands in #top (a card, a frame) is moved into board n's container, and
5
5
  // moved again when its data-board changes (a MutationObserver): no extension needs this one. One on the body (a
6
6
  // note's pin, fixed to the window) shows only while its board is shown.
7
7
  // Other scripts read the board shown from body[data-board] (new things go there), and get a DOM event "board"
8
8
  // {from, to} just before it changes (doodle: the drawing so far becomes a step; frames: count this board's).
9
9
  // The shelf: a dot per board (yours is bigger). Hover it and the dots open into thumbnails, with + for a new board;
10
10
  // click one to go there. It stays clear of the chat panel: more thumbnails than fit scroll between ‹ and › (grey at
11
- // the ends; a new board scrolls to the end). Drag a surface by its label onto a thumbnail to move it to that board, where it was ({type: "ui_board"}).
11
+ // the ends; a new board scrolls to the end). Drag a card by its label onto a thumbnail to move it to that board, where it was ({type: "card_board"}).
12
12
  // page messages board_view {board}: the board this page shows; board_add
13
- // GET /boards {boards, current} event "boards" {boards, added?, by?}
13
+ // GET /board {boards, current} event "board" {boards, added?, by?}
14
14
  const CSS = `
15
15
  #top > .board:not(.on) { position: absolute; top: 0; left: 0; right: 0; height: 0; overflow: hidden; padding: inherit;
16
16
  box-sizing: border-box; visibility: hidden; pointer-events: none; }
@@ -41,7 +41,7 @@ const CSS = `
41
41
  .boards-shelf .mini { display: none; position: relative; width: 120px; overflow: hidden; border-radius: 6px;
42
42
  background: rgba(255, 255, 255, 0.6); border: 1px solid var(--line); }
43
43
  .boards-shelf .mini i, .boards-peek .mini i { position: absolute; overflow: hidden; border-radius: 3px; background: rgba(0, 0, 0, 0.08); }
44
- /* a surface: a card in its own main colour (light grey when it has none) */
44
+ /* a card: a tile in its own main colour (light grey when it has none) */
45
45
  .boards-shelf .mini i.s, .boards-peek .mini i.s { --accent-of: rgb(200, 203, 210);
46
46
  background: color-mix(in srgb, var(--accent-of) 30%, #fff);
47
47
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--accent-of) 70%, transparent), 0 1px 3px rgba(0, 0, 0, 0.08); }
@@ -93,6 +93,7 @@ export default async function (page) {
93
93
 
94
94
  // the shelf: ‹ [the thumbnails, scrolling] › + (+ outside the scrolling part, so it is always there)
95
95
  const shelf = Object.assign(document.createElement("div"), { className: "boards-shelf" });
96
+ shelf.dataset.name = "boards";
96
97
  const button = (className, textContent, title, onclick) => Object.assign(document.createElement("button"), { className, textContent, title, onclick });
97
98
  const view = Object.assign(document.createElement("div"), { className: "view" });
98
99
  const track = Object.assign(document.createElement("div"), { className: "track" });
@@ -100,6 +101,7 @@ export default async function (page) {
100
101
  const prev = button("arrow", "‹", "Earlier boards", () => step(-1));
101
102
  const next = button("arrow", "›", "Later boards", () => step(1));
102
103
  const add = button("add", "+", "New board", () => { adding = true; page.send({ type: "board_add" }); });
104
+ add.dataset.name = "new-board";
103
105
  view.append(track);
104
106
  shelf.append(prev, view, next, add);
105
107
  document.body.append(shelf);
@@ -202,8 +204,8 @@ export default async function (page) {
202
204
  function thumbnails() {
203
205
  for (const slot of shelf.querySelectorAll(".slot")) drawBoard(slot.querySelector(".mini"), Number(slot.dataset.n), 120, 68);
204
206
  }
205
- // board n's things where they are in the main space, scaled down into mini (w x h): a surface is a small card in
206
- // its main colour (accentOf), no words. Zoomed to fit what's on the board (not the whole window), so the cards fill it.
207
+ // board n's things where they are in the main space, scaled down into mini (w x h): a card is a small tile in
208
+ // its main colour (accentOf), no words. Zoomed to fit what's on the board (not the whole window), so the tiles fill it.
207
209
  function drawBoard(mini, n, W, H) {
208
210
  const PAD = 12, els = [...container(n).children];
209
211
  const rects = els.map((el) => el.getBoundingClientRect());
@@ -212,10 +214,10 @@ export default async function (page) {
212
214
  mini.style.height = `${H}px`;
213
215
  mini.replaceChildren(...els.map((el, n) => {
214
216
  const r = rects[n], i = document.createElement("i");
215
- if (el.dataset.surface) {
216
- const accent = accentOf(el.querySelector(".surface-body") ?? el);
217
+ if (el.dataset.card) {
218
+ const accent = accentOf(el.querySelector(".card-body") ?? el);
217
219
  i.className = "s";
218
- i.title = el.dataset.surface;
220
+ i.title = el.dataset.card;
219
221
  if (accent) i.style.setProperty("--accent-of", accent);
220
222
  if (W > 200) { i.classList.add("detail"); sketch(el, i, k); } // the big preview: what's inside it
221
223
  }
@@ -238,20 +240,20 @@ export default async function (page) {
238
240
  peek.style.left = `${Math.min(innerWidth - half - 8, Math.max(half + 8, s.left + s.width / 2))}px`;
239
241
  peek.style.bottom = `${innerHeight - shelf.getBoundingClientRect().top + 8}px`;
240
242
  }
241
- // a surface's insides, scaled by k into its card (the big preview): each line of text a bar in its colour, coloured
243
+ // a card's insides, scaled by k into its tile (the big preview): each line of text a bar in its colour, coloured
242
244
  // areas (buttons, filled boxes) in theirs, pictures and canvases (a game) as small images. Apps' pages too (same
243
- // server). No words. At most 400 pieces a surface.
244
- function sketch(surface, card, k) {
245
- const base = surface.getBoundingClientRect();
245
+ // server). No words. At most 400 pieces a card.
246
+ function sketch(card, tile, k) {
247
+ const base = card.getBoundingClientRect();
246
248
  const put = (tag, r, dx, dy, style) => {
247
249
  if (r.width * k < 0.5 || r.height * k < 0.5) return null;
248
- const e = card.appendChild(document.createElement(tag));
250
+ const e = tile.appendChild(document.createElement(tag));
249
251
  Object.assign(e.style, { position: "absolute", left: `${(r.left + dx - base.left) * k}px`, top: `${(r.top + dy - base.top) * k}px`,
250
252
  width: `${r.width * k}px`, height: `${r.height * k}px` }, style);
251
253
  return e;
252
254
  };
253
- const roots = [[surface.querySelector(".surface-body") ?? surface, 0, 0]];
254
- for (const f of surface.querySelectorAll("iframe")) {
255
+ const roots = [[card.querySelector(".card-body") ?? card, 0, 0]];
256
+ for (const f of card.querySelectorAll("iframe")) {
255
257
  try { const r = f.getBoundingClientRect(); if (f.contentDocument?.body) roots.push([f.contentDocument.body, r.left + f.clientLeft, r.top + f.clientTop]); } catch {}
256
258
  }
257
259
  let n = 0;
@@ -313,12 +315,12 @@ export default async function (page) {
313
315
  else { peek.hidden = true; fit(); }
314
316
  }
315
317
 
316
- // drag a surface (by its label: ui/browser.js moves it) onto a thumbnail: it goes to that board, at the place it had
318
+ // drag a card (by its label: card/browser.js moves it) onto a thumbnail: it goes to that board, at the place it had
317
319
  document.addEventListener("pointerdown", (e) => {
318
- const box = e.target.closest?.("[data-surface] > .surface-label")?.parentElement;
320
+ const box = e.target.closest?.("[data-card] > .card-label")?.parentElement;
319
321
  if (!box || e.target.closest("button")) return;
320
322
  const px = (v) => (v ? parseFloat(v) : undefined);
321
- drag = { id: box.dataset.surface, board: Number(box.dataset.board) || 1,
323
+ drag = { id: box.dataset.card, board: Number(box.dataset.board) || 1,
322
324
  place: box.hasAttribute("data-placed") ? { x: box.offsetLeft, y: box.offsetTop, width: px(box.style.width), height: px(box.style.height) } : null };
323
325
  });
324
326
  document.addEventListener("pointermove", (e) => {
@@ -335,13 +337,13 @@ export default async function (page) {
335
337
  document.addEventListener("pointerup", () => {
336
338
  if (!drag) return;
337
339
  const to = over && Number(over.dataset.n);
338
- if (to && to !== drag.board) page.send({ type: "ui_board", surface: drag.id, board: to, place: drag.place });
340
+ if (to && to !== drag.board) page.send({ type: "card_board", card: drag.id, board: to, place: drag.place });
339
341
  over?.classList.remove("drop");
340
342
  drag = over = null;
341
343
  if (!shelf.matches(":hover")) open(false);
342
344
  });
343
345
 
344
- page.on("boards", (ev) => {
346
+ page.on("board", (ev) => {
345
347
  boards = ev.boards;
346
348
  if (ev.removed) { // its things went with it (their own events); its container goes now
347
349
  containers[ev.removed]?.remove();
@@ -352,6 +354,6 @@ export default async function (page) {
352
354
  render();
353
355
  });
354
356
 
355
- try { ({ boards, current } = await (await fetch("/boards")).json()); } catch {}
357
+ try { ({ boards, current } = await (await fetch("/board")).json()); } catch {}
356
358
  show(current, false); // nothing to tell: the server said it (and the page's WebSocket may not be open yet)
357
359
  }
@@ -1,4 +1,4 @@
1
1
  # Boards
2
2
  The user's work is on numbered boards; the page shows one at a time. A plain message doesn't say which board the
3
3
  user means: when it names one, use that; when it is about something already built, use that thing's board; else
4
- call boards() (it says which board the user is on) and, if it is still unclear, ask the user.
4
+ leave board out (card_show puts a new card on the board the user is on). board_list() lists them all.