@ai-ecoverse/slicc-agent 7.15.1 → 7.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -3
- package/dist/approvals/sudo.js +4 -0
- package/dist/approvals/sudo.js.map +2 -2
- package/dist/kernel/agent-users.js +7 -1
- package/dist/kernel/agent-users.js.map +3 -3
- package/dist/main.js +21 -14
- package/dist/main.js.map +2 -2
- package/dist/scoops/cli.js +20 -0
- package/dist/scoops/cli.js.map +2 -2
- package/dist/scoops/index.js +1 -0
- package/dist/scoops/index.js.map +2 -2
- package/dist/spectrum/index.js +1 -1
- package/dist/spectrum/index.js.map +2 -2
- package/dist/spectrum/sprinkle-port.js +5 -2
- package/dist/spectrum/sprinkle-port.js.map +2 -2
- package/dist/sprinkles/bridge.js +89 -0
- package/dist/sprinkles/bridge.js.map +7 -0
- package/dist/sprinkles/cgi-run.js +340 -0
- package/dist/sprinkles/cgi-run.js.map +7 -0
- package/dist/sprinkles/cgi.js +213 -0
- package/dist/sprinkles/cgi.js.map +7 -0
- package/dist/sprinkles/index.js +28 -9
- package/dist/sprinkles/index.js.map +3 -3
- package/dist/sprinkles/sprinkles.js +1 -1
- package/dist/sprinkles/sprinkles.js.map +1 -1
- package/dist/types/approvals/sudo.d.ts +1 -0
- package/dist/types/kernel/client.d.ts +3 -0
- package/dist/types/scoops/cli.d.ts +2 -0
- package/dist/types/scoops/index.d.ts +1 -0
- package/dist/types/spectrum/sprinkle-port.d.ts +1 -1
- package/dist/types/sprinkles/bridge.d.ts +30 -0
- package/dist/types/sprinkles/cgi-run.d.ts +59 -0
- package/dist/types/sprinkles/cgi.d.ts +69 -0
- package/dist/types/sprinkles/index.d.ts +5 -1
- package/dist/types/sprinkles/sprinkles.d.ts +1 -1
- package/package.json +5 -4
- package/packages/vfs-root/skills/sprinkles/SKILL.md +22 -3
package/dist/sprinkles/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
replicatedState
|
|
3
3
|
} from "@earendil-works/chord";
|
|
4
4
|
import { live } from "../agents.js";
|
|
5
|
+
import { cgiBridge, folderOf, NO_OWNER, targetOf } from "./bridge.js";
|
|
5
6
|
import {
|
|
6
7
|
describe,
|
|
7
8
|
homePath,
|
|
@@ -19,9 +20,6 @@ import {
|
|
|
19
20
|
WELCOMED
|
|
20
21
|
} from "./sprinkles.js";
|
|
21
22
|
const BUILTIN = /* @__PURE__ */ new Set([WELCOME, SUGGESTIONS]);
|
|
22
|
-
function targetOf(agentId) {
|
|
23
|
-
return agentId.startsWith("scoop:") ? agentId : `cone:${agentId}`;
|
|
24
|
-
}
|
|
25
23
|
function liveOwner(agents, owner) {
|
|
26
24
|
if (!owner) return void 0;
|
|
27
25
|
const state = agents.state();
|
|
@@ -41,7 +39,7 @@ function entry(name, html, agentId, id = name) {
|
|
|
41
39
|
function placesOf(options, dir) {
|
|
42
40
|
return options.places?.() ?? [{ dir, cone: "" }];
|
|
43
41
|
}
|
|
44
|
-
async function filesIn(options, dir, reserved, owned, using, found = {}) {
|
|
42
|
+
async function filesIn(options, dir, reserved, owned, using, found = {}, folders = /* @__PURE__ */ new Map()) {
|
|
45
43
|
const { env, agents } = options;
|
|
46
44
|
const out = [];
|
|
47
45
|
for (const place of placesOf(options, dir))
|
|
@@ -50,12 +48,14 @@ async function filesIn(options, dir, reserved, owned, using, found = {}) {
|
|
|
50
48
|
if (reserved.has(name) || out.some((item) => item.id === id)) continue;
|
|
51
49
|
const read = await env.readTextFile(path, using);
|
|
52
50
|
if (place.cone && !owned[id]) found[id] = place.cone;
|
|
51
|
+
const folder = folderOf(name, path);
|
|
52
|
+
if (folder) folders.set(id, folder);
|
|
53
53
|
const owner = owned[id] ?? (place.cone || void 0);
|
|
54
54
|
if (read.ok) out.push(entry(name, read.value, ownerOf(agents, owner), id));
|
|
55
55
|
}
|
|
56
56
|
return out;
|
|
57
57
|
}
|
|
58
|
-
function reloader(options, dir, builtins, sprinkles, owners) {
|
|
58
|
+
function reloader(options, dir, builtins, sprinkles, owners, folders) {
|
|
59
59
|
const { env, agents } = options;
|
|
60
60
|
const { welcome, suggestions } = builtins;
|
|
61
61
|
return async (using) => {
|
|
@@ -65,7 +65,10 @@ function reloader(options, dir, builtins, sprinkles, owners) {
|
|
|
65
65
|
const offered = stored?.ok === true && stored.value;
|
|
66
66
|
const reserved = /* @__PURE__ */ new Set([WELCOME, ...offered ? [SUGGESTIONS] : []]);
|
|
67
67
|
const found = {};
|
|
68
|
-
const
|
|
68
|
+
const seen = /* @__PURE__ */ new Map();
|
|
69
|
+
const out = await filesIn(options, dir, reserved, owned, using, found, seen);
|
|
70
|
+
folders.clear();
|
|
71
|
+
for (const [id, folder] of seen) folders.set(id, folder);
|
|
69
72
|
if (Object.keys(found).length)
|
|
70
73
|
await options.harness.commit(async (tx) => {
|
|
71
74
|
const doc = await tx.doc(SprinklesDoc);
|
|
@@ -81,7 +84,7 @@ function reloader(options, dir, builtins, sprinkles, owners) {
|
|
|
81
84
|
sprinkles.replace(using, out);
|
|
82
85
|
};
|
|
83
86
|
}
|
|
84
|
-
|
|
87
|
+
import { NO_OWNER as NO_OWNER2 } from "./bridge.js";
|
|
85
88
|
async function ownedRead(files, asked, home, using) {
|
|
86
89
|
const real = await files.canonicalPath(asked, using);
|
|
87
90
|
const path = real.ok ? homePath(real.value, home, home) : asked;
|
|
@@ -121,7 +124,18 @@ function setupSprinkles() {
|
|
|
121
124
|
const welcome = await builtin(WELCOME);
|
|
122
125
|
const suggestions = await builtin(SUGGESTIONS);
|
|
123
126
|
const owners = async (using) => (await harness.snapshot(SprinklesDoc, using))?.owners ?? {};
|
|
124
|
-
const
|
|
127
|
+
const folders = /* @__PURE__ */ new Map();
|
|
128
|
+
const reload = reloader(options, dir, { welcome, suggestions }, sprinkles, owners, folders);
|
|
129
|
+
const cgi = cgiBridge({
|
|
130
|
+
licks,
|
|
131
|
+
kernelOf: options.kernelOf,
|
|
132
|
+
find: (id) => find(id),
|
|
133
|
+
folder: (id) => folders.get(id),
|
|
134
|
+
async owner(id, using) {
|
|
135
|
+
const owned = (await owners(using))[id];
|
|
136
|
+
return options.places ? liveOwner(agents, owned) : ownerOf(agents, owned);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
125
139
|
const inside = async (path, using) => {
|
|
126
140
|
const asked = homePath(path);
|
|
127
141
|
const real = await env.canonicalPath(asked, using);
|
|
@@ -232,10 +246,14 @@ function setupSprinkles() {
|
|
|
232
246
|
return null;
|
|
233
247
|
}
|
|
234
248
|
};
|
|
249
|
+
const scripts = cgi.methods(id, args, using);
|
|
250
|
+
if (Object.hasOwn(scripts, method))
|
|
251
|
+
return scripts[method]();
|
|
235
252
|
const handler = handlers[method];
|
|
236
253
|
if (!handler) throw new Error(NO_EXEC);
|
|
237
254
|
return handler();
|
|
238
255
|
},
|
|
256
|
+
lick: cgi.lick,
|
|
239
257
|
async claim(id, cone, using) {
|
|
240
258
|
await reload(using);
|
|
241
259
|
if (id === SUGGESTIONS || !find(id) || (await owners(using))[id]) return;
|
|
@@ -250,6 +268,7 @@ function setupSprinkles() {
|
|
|
250
268
|
await reload(using);
|
|
251
269
|
},
|
|
252
270
|
async close(using) {
|
|
271
|
+
cgi.stop();
|
|
253
272
|
offCone();
|
|
254
273
|
clearTimeout(timer);
|
|
255
274
|
await watcher?.stop(using);
|
|
@@ -259,7 +278,7 @@ function setupSprinkles() {
|
|
|
259
278
|
};
|
|
260
279
|
}
|
|
261
280
|
export {
|
|
262
|
-
NO_OWNER,
|
|
281
|
+
NO_OWNER2 as NO_OWNER,
|
|
263
282
|
liveOwner,
|
|
264
283
|
ownedRead,
|
|
265
284
|
ownerOf,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/sprinkles/index.ts"],
|
|
4
|
-
"sourcesContent": ["import {\n type Context,\n type JsonValue,\n type ReplicatedState,\n replicatedState,\n} from '@earendil-works/chord';\nimport type { Harness } from '@earendil-works/pi-durable';\nimport type { ExecutionEnv, FileWatcher } from '@earendil-works/pi-durable/env';\nimport { type Agents, live } from '../agents.ts';\nimport type { LickTarget } from '../licks/lick.ts';\nimport type { Licks } from '../licks/licks.ts';\nimport {\n describe,\n homePath,\n NO_EXEC,\n SPRINKLE_KIND,\n SPRINKLES_DIR,\n type Sprinkle,\n type SprinkleMethod,\n SprinklesDoc,\n SUGGESTIONS,\n SUGGESTIONS_DIR,\n SUGGESTIONS_STORE,\n sharedPaths,\n sprinkleFiles,\n WELCOME,\n WELCOME_PROFILE,\n WELCOMED,\n} from './sprinkles.ts';\n\nexport interface SprinklePayload {\n action: string;\n data: JsonValue | null;\n target: string | null;\n}\n\nexport interface SprinklesRuntime {\n readonly sprinkles: ReplicatedState<Sprinkle[]>;\n send(id: string, payload: SprinklePayload, context: Context): Promise<{ delivered: boolean }>;\n call(id: string, method: string, args: readonly unknown[], context: Context): Promise<JsonValue>;\n claim(id: string, cone: string, context: Context): Promise<void>;\n reload(context: Context): Promise<void>;\n close(context: Context): Promise<void>;\n}\n\nexport interface SprinklesAttach {\n harness: Harness;\n agents: Agents;\n licks: Licks;\n env: ExecutionEnv;\n assets?: (path: string) => Promise<string>;\n reloadMs: number;\n dir?: string;\n intercept?: (id: string, payload: SprinklePayload, context: Context) => Promise<boolean>;\n places?: () => readonly SprinklePlace[];\n envOf?: (agentId: string, context: Context) => Promise<ExecutionEnv | undefined>;\n homeOf?: (agentId: string) => string | undefined;\n shared?: string;\n}\n\nconst BUILTIN = new Set([WELCOME, SUGGESTIONS]);\n\nexport interface SprinklePlace {\n dir: string;\n cone: string;\n}\n\nfunction targetOf(agentId: string): LickTarget {\n return agentId.startsWith('scoop:') ? (agentId as LickTarget) : `cone:${agentId}`;\n}\n\nexport function liveOwner(agents: Agents, owner: string | undefined): string | undefined {\n if (!owner) return undefined;\n const state = agents.state();\n if (state.cones[owner]) return owner;\n const scoop = state.scoops[owner];\n return scoop && live(scoop) ? owner : undefined;\n}\n\nexport function ownerOf(agents: Agents, owner: string | undefined): string {\n const state = agents.state();\n if (owner && state.cones[owner]) return owner;\n const scoop = owner ? state.scoops[owner] : undefined;\n return scoop && live(scoop) ? (owner as string) : agents.activeCone();\n}\n\nfunction entry(name: string, html: string, agentId: string, id = name): Sprinkle {\n return { id, name, ...describe(name, html), agentId, html };\n}\n\nfunction placesOf(options: SprinklesAttach, dir: string): readonly SprinklePlace[] {\n return options.places?.() ?? [{ dir, cone: '' }];\n}\n\nasync function filesIn(\n options: SprinklesAttach,\n dir: string,\n reserved: ReadonlySet<string>,\n owned: Record<string, string>,\n using: Context,\n found: Record<string, string> = {}\n): Promise<Sprinkle[]> {\n const { env, agents } = options;\n const out: Sprinkle[] = [];\n for (const place of placesOf(options, dir))\n for (const { name, path } of await sprinkleFiles(env, place.dir, using)) {\n const id = place.cone ? `${place.cone}/${name}` : name;\n if (reserved.has(name) || out.some((item) => item.id === id)) continue;\n const read = await env.readTextFile(path, using);\n if (place.cone && !owned[id]) found[id] = place.cone;\n const owner = owned[id] ?? (place.cone || undefined);\n if (read.ok) out.push(entry(name, read.value, ownerOf(agents, owner), id));\n }\n return out;\n}\n\nfunction reloader(\n options: SprinklesAttach,\n dir: string,\n builtins: { welcome: string | undefined; suggestions: string | undefined },\n sprinkles: ReplicatedState<Sprinkle[]> & { replace(context: Context, value: Sprinkle[]): void },\n owners: (context: Context) => Promise<Record<string, string>>\n) {\n const { env, agents } = options;\n const { welcome, suggestions } = builtins;\n return async (using: Context) => {\n const owned = await owners(using);\n const store = options.shared ? sharedPaths(options.shared).suggestions : SUGGESTIONS_STORE;\n const stored = suggestions === undefined ? undefined : await env.exists(store, using);\n const offered = stored?.ok === true && stored.value;\n const reserved = new Set([WELCOME, ...(offered ? [SUGGESTIONS] : [])]);\n const found: Record<string, string> = {};\n const out = await filesIn(options, dir, reserved, owned, using, found);\n if (Object.keys(found).length)\n await options.harness.commit(async (tx) => {\n const doc = await tx.doc(SprinklesDoc);\n for (const [id, cone] of Object.entries(found)) doc.owners[id] ??= cone;\n }, using);\n if (offered)\n out.push(entry(SUGGESTIONS, suggestions as string, ownerOf(agents, owned[SUGGESTIONS])));\n if (welcome !== undefined)\n out.push({\n ...entry(WELCOME, welcome, ownerOf(agents, owned[WELCOME] ?? 'cone')),\n inline: true,\n });\n sprinkles.replace(using, out);\n };\n}\n\nexport const NO_OWNER = 'this sprinkle has no owner, so it can only be shown';\n\nexport async function ownedRead(\n files: ExecutionEnv,\n asked: string,\n home: string,\n using: Context\n): Promise<{ files: ExecutionEnv; path: string; check: () => Promise<void> }> {\n const real = await files.canonicalPath(asked, using);\n const path = real.ok ? homePath(real.value, home, home) : asked;\n return {\n files,\n path,\n async check() {\n const again = await files.canonicalPath(path, using);\n if (again.ok && again.value !== path)\n throw new Error(`${asked} changed while it was read; read it again`);\n },\n };\n}\n\nfunction readerFor(\n options: SprinklesAttach,\n owners: (context: Context) => Promise<Record<string, string>>,\n inside: (path: unknown, context: Context) => Promise<string>\n) {\n const { env, agents } = options;\n const none = async () => undefined;\n return async (id: string, path: unknown, using: Context) => {\n if (options.shared && BUILTIN.has(id))\n return { files: env, path: homePath(path, options.shared, options.shared), check: none };\n if (!options.places) return { files: env, path: await inside(path, using), check: none };\n const owner = liveOwner(agents, (await owners(using))[id]);\n if (!owner) throw new Error(NO_OWNER);\n const agentId = owner.startsWith('scoop:') ? owner : `cone:${owner}`;\n const home = options.homeOf?.(agentId);\n const files = await options.envOf?.(agentId, using);\n if (!home || !files) throw new Error(NO_OWNER);\n return ownedRead(files, homePath(path, home, home), home, using);\n };\n}\n\nexport function setupSprinkles() {\n return {\n async attach(options: SprinklesAttach, context: Context): Promise<SprinklesRuntime> {\n const { harness, agents, licks, env } = options;\n const dir = options.dir ?? SPRINKLES_DIR;\n const sprinkles = replicatedState<Sprinkle[]>([]);\n const builtin = (name: string) =>\n options.assets\n ? options.assets(`packages/vfs-root/sprinkles/${name}.shtml`).catch(() => undefined)\n : Promise.resolve(undefined);\n const welcome = await builtin(WELCOME);\n const suggestions = await builtin(SUGGESTIONS);\n const owners = async (using: Context) =>\n (await harness.snapshot(SprinklesDoc, using))?.owners ?? {};\n const reload = reloader(options, dir, { welcome, suggestions }, sprinkles, owners);\n const inside = async (path: unknown, using: Context) => {\n const asked = homePath(path);\n const real = await env.canonicalPath(asked, using);\n return real.ok ? homePath(real.value) : asked;\n };\n const paths = options.shared ? sharedPaths(options.shared) : undefined;\n const reader = readerFor(options, owners, inside);\n const offCone = agents.onCone(() => reload(context).catch(() => undefined));\n const find = (id: string) => sprinkles.value.find((item) => item.id === id);\n const post = async (id: string, agentId: string, using: Context) => {\n const conversation = await agents.conversation(agentId, using);\n if (!conversation) throw new Error(`there is no agent ${agentId}`);\n await conversation.submit(\n { type: 'write', entry: { kind: SPRINKLE_KIND, data: { sprinkle: id, at: Date.now() } } },\n using\n );\n };\n await reload(context);\n const state = await harness.snapshot(SprinklesDoc, context);\n if (welcome !== undefined && !state?.welcomed) {\n await post(WELCOME, 'cone', context);\n await harness.commit(async (tx) => {\n (await tx.doc(SprinklesDoc)).welcomed = true;\n }, context);\n }\n let timer: ReturnType<typeof setTimeout> | undefined;\n let watcher: FileWatcher | undefined;\n let seen = '';\n const watch = async (using: Context) => {\n const dirs = placesOf(options, dir).map((place) => place.dir);\n const key = dirs.join('\\n');\n if (key === seen) return;\n seen = key;\n await watcher?.stop(using);\n const watched = await env.watch(\n [\n ...dirs.map((path) => ({ path, recursive: true })),\n { path: paths?.suggestionsDir ?? SUGGESTIONS_DIR },\n ],\n () => {\n clearTimeout(timer);\n timer = setTimeout(() => void reload(context).catch(() => undefined), options.reloadMs);\n },\n using\n );\n watcher = watched.ok ? watched.value : undefined;\n };\n await watch(context);\n return {\n sprinkles,\n async send(id, payload, using) {\n const sprinkle = find(id);\n if (!sprinkle) return { delivered: false };\n if (await options.intercept?.(id, payload, using)) return { delivered: true };\n if (id === WELCOME && payload.action === 'onboarding-complete') {\n await env.writeFile(\n paths?.welcomed ?? WELCOMED,\n `${new Date().toISOString()}\\n`,\n using\n );\n if (payload.data && typeof payload.data === 'object' && !Array.isArray(payload.data))\n await env.writeFile(\n paths?.profile ?? WELCOME_PROFILE,\n `${JSON.stringify(payload.data, null, 2)}\\n`,\n using\n );\n }\n const owner = (await owners(using))[id];\n if (options.places && !BUILTIN.has(id) && !liveOwner(agents, owner))\n return { delivered: false };\n await licks.deliver(\n {\n channel: 'sprinkle',\n source: `sprinkle:${id}`,\n title: sprinkle.title,\n text: payload.action || '(no action)',\n ...(payload.data === null ? {} : { body: JSON.stringify(payload.data, null, 2) }),\n target: targetOf(ownerOf(agents, owner)),\n },\n using\n );\n return { delivered: true };\n },\n async call(id, method, args, using) {\n if (!find(id)) throw new Error(`there is no sprinkle ${id}`);\n const handlers: Record<SprinkleMethod, () => Promise<JsonValue>> = {\n async readFile() {\n const { files, path, check } = await reader(id, args[0], using);\n const read = await files.readTextFile(path, using);\n if (!read.ok) throw new Error(read.error.message);\n await check();\n return read.value;\n },\n async exists() {\n const { files, path } = await reader(id, args[0], using);\n const found = await files.exists(path, using);\n return found.ok && found.value;\n },\n async getState() {\n return (await harness.snapshot(SprinklesDoc, using))?.state[id] ?? null;\n },\n async setState() {\n await harness.commit(async (tx) => {\n (await tx.doc(SprinklesDoc)).state[id] = (args[0] ?? null) as JsonValue;\n }, using);\n return null;\n },\n };\n const handler = handlers[method as SprinkleMethod];\n if (!handler) throw new Error(NO_EXEC);\n return handler();\n },\n async claim(id, cone, using) {\n await reload(using);\n if (id === SUGGESTIONS || !find(id) || (await owners(using))[id]) return;\n await harness.commit(async (tx) => {\n const doc = await tx.doc(SprinklesDoc);\n doc.owners[id] ??= cone;\n }, using);\n await reload(using);\n },\n async reload(using) {\n await watch(using);\n await reload(using);\n },\n async close(using) {\n offCone();\n clearTimeout(timer);\n await watcher?.stop(using);\n },\n };\n },\n };\n}\n"],
|
|
5
|
-
"mappings": "AAAA;AAAA,EAIE;AAAA,OACK;AAGP,SAAsB,YAAY;
|
|
6
|
-
"names": []
|
|
4
|
+
"sourcesContent": ["import {\n type Context,\n type JsonValue,\n type ReplicatedState,\n replicatedState,\n} from '@earendil-works/chord';\nimport type { Harness } from '@earendil-works/pi-durable';\nimport type { ExecutionEnv, FileWatcher } from '@earendil-works/pi-durable/env';\nimport { type Agents, live } from '../agents.ts';\nimport type { Licks } from '../licks/licks.ts';\nimport { cgiBridge, folderOf, NO_OWNER, type SprinkleLick, targetOf } from './bridge.ts';\nimport type { CgiKernel } from './cgi-run.ts';\nimport {\n describe,\n homePath,\n NO_EXEC,\n SPRINKLE_KIND,\n SPRINKLES_DIR,\n type Sprinkle,\n type SprinkleMethod,\n SprinklesDoc,\n SUGGESTIONS,\n SUGGESTIONS_DIR,\n SUGGESTIONS_STORE,\n sharedPaths,\n sprinkleFiles,\n WELCOME,\n WELCOME_PROFILE,\n WELCOMED,\n} from './sprinkles.ts';\n\nexport interface SprinklePayload {\n action: string;\n data: JsonValue | null;\n target: string | null;\n}\n\nexport interface SprinklesRuntime {\n readonly sprinkles: ReplicatedState<Sprinkle[]>;\n send(id: string, payload: SprinklePayload, context: Context): Promise<{ delivered: boolean }>;\n call(id: string, method: string, args: readonly unknown[], context: Context): Promise<JsonValue>;\n lick(id: string, ask: SprinkleLick, context: Context): Promise<string | undefined>;\n claim(id: string, cone: string, context: Context): Promise<void>;\n reload(context: Context): Promise<void>;\n close(context: Context): Promise<void>;\n}\n\nexport interface SprinklesAttach {\n harness: Harness;\n agents: Agents;\n licks: Licks;\n env: ExecutionEnv;\n assets?: (path: string) => Promise<string>;\n reloadMs: number;\n dir?: string;\n intercept?: (id: string, payload: SprinklePayload, context: Context) => Promise<boolean>;\n places?: () => readonly SprinklePlace[];\n envOf?: (agentId: string, context: Context) => Promise<ExecutionEnv | undefined>;\n homeOf?: (agentId: string) => string | undefined;\n shared?: string;\n kernelOf?: (agentId: string) => Promise<CgiKernel | undefined>;\n}\n\nconst BUILTIN = new Set([WELCOME, SUGGESTIONS]);\n\nexport interface SprinklePlace {\n dir: string;\n cone: string;\n}\n\nexport function liveOwner(agents: Agents, owner: string | undefined): string | undefined {\n if (!owner) return undefined;\n const state = agents.state();\n if (state.cones[owner]) return owner;\n const scoop = state.scoops[owner];\n return scoop && live(scoop) ? owner : undefined;\n}\n\nexport function ownerOf(agents: Agents, owner: string | undefined): string {\n const state = agents.state();\n if (owner && state.cones[owner]) return owner;\n const scoop = owner ? state.scoops[owner] : undefined;\n return scoop && live(scoop) ? (owner as string) : agents.activeCone();\n}\n\nfunction entry(name: string, html: string, agentId: string, id = name): Sprinkle {\n return { id, name, ...describe(name, html), agentId, html };\n}\n\nfunction placesOf(options: SprinklesAttach, dir: string): readonly SprinklePlace[] {\n return options.places?.() ?? [{ dir, cone: '' }];\n}\n\nasync function filesIn(\n options: SprinklesAttach,\n dir: string,\n reserved: ReadonlySet<string>,\n owned: Record<string, string>,\n using: Context,\n found: Record<string, string> = {},\n folders: Map<string, string> = new Map()\n): Promise<Sprinkle[]> {\n const { env, agents } = options;\n const out: Sprinkle[] = [];\n for (const place of placesOf(options, dir))\n for (const { name, path } of await sprinkleFiles(env, place.dir, using)) {\n const id = place.cone ? `${place.cone}/${name}` : name;\n if (reserved.has(name) || out.some((item) => item.id === id)) continue;\n const read = await env.readTextFile(path, using);\n if (place.cone && !owned[id]) found[id] = place.cone;\n const folder = folderOf(name, path);\n if (folder) folders.set(id, folder);\n const owner = owned[id] ?? (place.cone || undefined);\n if (read.ok) out.push(entry(name, read.value, ownerOf(agents, owner), id));\n }\n return out;\n}\n\nfunction reloader(\n options: SprinklesAttach,\n dir: string,\n builtins: { welcome: string | undefined; suggestions: string | undefined },\n sprinkles: ReplicatedState<Sprinkle[]> & { replace(context: Context, value: Sprinkle[]): void },\n owners: (context: Context) => Promise<Record<string, string>>,\n folders: Map<string, string>\n) {\n const { env, agents } = options;\n const { welcome, suggestions } = builtins;\n return async (using: Context) => {\n const owned = await owners(using);\n const store = options.shared ? sharedPaths(options.shared).suggestions : SUGGESTIONS_STORE;\n const stored = suggestions === undefined ? undefined : await env.exists(store, using);\n const offered = stored?.ok === true && stored.value;\n const reserved = new Set([WELCOME, ...(offered ? [SUGGESTIONS] : [])]);\n const found: Record<string, string> = {};\n const seen = new Map<string, string>();\n const out = await filesIn(options, dir, reserved, owned, using, found, seen);\n folders.clear();\n for (const [id, folder] of seen) folders.set(id, folder);\n if (Object.keys(found).length)\n await options.harness.commit(async (tx) => {\n const doc = await tx.doc(SprinklesDoc);\n for (const [id, cone] of Object.entries(found)) doc.owners[id] ??= cone;\n }, using);\n if (offered)\n out.push(entry(SUGGESTIONS, suggestions as string, ownerOf(agents, owned[SUGGESTIONS])));\n if (welcome !== undefined)\n out.push({\n ...entry(WELCOME, welcome, ownerOf(agents, owned[WELCOME] ?? 'cone')),\n inline: true,\n });\n sprinkles.replace(using, out);\n };\n}\n\nexport { NO_OWNER, type SprinkleLick } from './bridge.ts';\n\nexport async function ownedRead(\n files: ExecutionEnv,\n asked: string,\n home: string,\n using: Context\n): Promise<{ files: ExecutionEnv; path: string; check: () => Promise<void> }> {\n const real = await files.canonicalPath(asked, using);\n const path = real.ok ? homePath(real.value, home, home) : asked;\n return {\n files,\n path,\n async check() {\n const again = await files.canonicalPath(path, using);\n if (again.ok && again.value !== path)\n throw new Error(`${asked} changed while it was read; read it again`);\n },\n };\n}\n\nfunction readerFor(\n options: SprinklesAttach,\n owners: (context: Context) => Promise<Record<string, string>>,\n inside: (path: unknown, context: Context) => Promise<string>\n) {\n const { env, agents } = options;\n const none = async () => undefined;\n return async (id: string, path: unknown, using: Context) => {\n if (options.shared && BUILTIN.has(id))\n return { files: env, path: homePath(path, options.shared, options.shared), check: none };\n if (!options.places) return { files: env, path: await inside(path, using), check: none };\n const owner = liveOwner(agents, (await owners(using))[id]);\n if (!owner) throw new Error(NO_OWNER);\n const agentId = owner.startsWith('scoop:') ? owner : `cone:${owner}`;\n const home = options.homeOf?.(agentId);\n const files = await options.envOf?.(agentId, using);\n if (!home || !files) throw new Error(NO_OWNER);\n return ownedRead(files, homePath(path, home, home), home, using);\n };\n}\n\nexport function setupSprinkles() {\n return {\n async attach(options: SprinklesAttach, context: Context): Promise<SprinklesRuntime> {\n const { harness, agents, licks, env } = options;\n const dir = options.dir ?? SPRINKLES_DIR;\n const sprinkles = replicatedState<Sprinkle[]>([]);\n const builtin = (name: string) =>\n options.assets\n ? options.assets(`packages/vfs-root/sprinkles/${name}.shtml`).catch(() => undefined)\n : Promise.resolve(undefined);\n const welcome = await builtin(WELCOME);\n const suggestions = await builtin(SUGGESTIONS);\n const owners = async (using: Context) =>\n (await harness.snapshot(SprinklesDoc, using))?.owners ?? {};\n const folders = new Map<string, string>();\n const reload = reloader(options, dir, { welcome, suggestions }, sprinkles, owners, folders);\n const cgi = cgiBridge({\n licks,\n kernelOf: options.kernelOf,\n find: (id) => find(id),\n folder: (id) => folders.get(id),\n async owner(id, using) {\n const owned = (await owners(using))[id];\n return options.places ? liveOwner(agents, owned) : ownerOf(agents, owned);\n },\n });\n const inside = async (path: unknown, using: Context) => {\n const asked = homePath(path);\n const real = await env.canonicalPath(asked, using);\n return real.ok ? homePath(real.value) : asked;\n };\n const paths = options.shared ? sharedPaths(options.shared) : undefined;\n const reader = readerFor(options, owners, inside);\n const offCone = agents.onCone(() => reload(context).catch(() => undefined));\n const find = (id: string) => sprinkles.value.find((item) => item.id === id);\n const post = async (id: string, agentId: string, using: Context) => {\n const conversation = await agents.conversation(agentId, using);\n if (!conversation) throw new Error(`there is no agent ${agentId}`);\n await conversation.submit(\n { type: 'write', entry: { kind: SPRINKLE_KIND, data: { sprinkle: id, at: Date.now() } } },\n using\n );\n };\n await reload(context);\n const state = await harness.snapshot(SprinklesDoc, context);\n if (welcome !== undefined && !state?.welcomed) {\n await post(WELCOME, 'cone', context);\n await harness.commit(async (tx) => {\n (await tx.doc(SprinklesDoc)).welcomed = true;\n }, context);\n }\n let timer: ReturnType<typeof setTimeout> | undefined;\n let watcher: FileWatcher | undefined;\n let seen = '';\n const watch = async (using: Context) => {\n const dirs = placesOf(options, dir).map((place) => place.dir);\n const key = dirs.join('\\n');\n if (key === seen) return;\n seen = key;\n await watcher?.stop(using);\n const watched = await env.watch(\n [\n ...dirs.map((path) => ({ path, recursive: true })),\n { path: paths?.suggestionsDir ?? SUGGESTIONS_DIR },\n ],\n () => {\n clearTimeout(timer);\n timer = setTimeout(() => void reload(context).catch(() => undefined), options.reloadMs);\n },\n using\n );\n watcher = watched.ok ? watched.value : undefined;\n };\n await watch(context);\n return {\n sprinkles,\n async send(id, payload, using) {\n const sprinkle = find(id);\n if (!sprinkle) return { delivered: false };\n if (await options.intercept?.(id, payload, using)) return { delivered: true };\n if (id === WELCOME && payload.action === 'onboarding-complete') {\n await env.writeFile(\n paths?.welcomed ?? WELCOMED,\n `${new Date().toISOString()}\\n`,\n using\n );\n if (payload.data && typeof payload.data === 'object' && !Array.isArray(payload.data))\n await env.writeFile(\n paths?.profile ?? WELCOME_PROFILE,\n `${JSON.stringify(payload.data, null, 2)}\\n`,\n using\n );\n }\n const owner = (await owners(using))[id];\n if (options.places && !BUILTIN.has(id) && !liveOwner(agents, owner))\n return { delivered: false };\n await licks.deliver(\n {\n channel: 'sprinkle',\n source: `sprinkle:${id}`,\n title: sprinkle.title,\n text: payload.action || '(no action)',\n ...(payload.data === null ? {} : { body: JSON.stringify(payload.data, null, 2) }),\n target: targetOf(ownerOf(agents, owner)),\n },\n using\n );\n return { delivered: true };\n },\n async call(id, method, args, using) {\n if (!find(id)) throw new Error(`there is no sprinkle ${id}`);\n const handlers: Record<SprinkleMethod, () => Promise<JsonValue>> = {\n async readFile() {\n const { files, path, check } = await reader(id, args[0], using);\n const read = await files.readTextFile(path, using);\n if (!read.ok) throw new Error(read.error.message);\n await check();\n return read.value;\n },\n async exists() {\n const { files, path } = await reader(id, args[0], using);\n const found = await files.exists(path, using);\n return found.ok && found.value;\n },\n async getState() {\n return (await harness.snapshot(SprinklesDoc, using))?.state[id] ?? null;\n },\n async setState() {\n await harness.commit(async (tx) => {\n (await tx.doc(SprinklesDoc)).state[id] = (args[0] ?? null) as JsonValue;\n }, using);\n return null;\n },\n };\n const scripts = cgi.methods(id, args, using);\n if (Object.hasOwn(scripts, method))\n return (scripts[method] as () => Promise<JsonValue>)();\n const handler = handlers[method as SprinkleMethod];\n if (!handler) throw new Error(NO_EXEC);\n return handler();\n },\n lick: cgi.lick,\n async claim(id, cone, using) {\n await reload(using);\n if (id === SUGGESTIONS || !find(id) || (await owners(using))[id]) return;\n await harness.commit(async (tx) => {\n const doc = await tx.doc(SprinklesDoc);\n doc.owners[id] ??= cone;\n }, using);\n await reload(using);\n },\n async reload(using) {\n await watch(using);\n await reload(using);\n },\n async close(using) {\n cgi.stop();\n offCone();\n clearTimeout(timer);\n await watcher?.stop(using);\n },\n };\n },\n };\n}\n"],
|
|
5
|
+
"mappings": "AAAA;AAAA,EAIE;AAAA,OACK;AAGP,SAAsB,YAAY;AAElC,SAAS,WAAW,UAAU,UAA6B,gBAAgB;AAE3E;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAkCP,MAAM,UAAU,oBAAI,IAAI,CAAC,SAAS,WAAW,CAAC;AAOvC,SAAS,UAAU,QAAgB,OAA+C;AACvF,MAAI,CAAC,MAAO,QAAO;AACnB,QAAM,QAAQ,OAAO,MAAM;AAC3B,MAAI,MAAM,MAAM,KAAK,EAAG,QAAO;AAC/B,QAAM,QAAQ,MAAM,OAAO,KAAK;AAChC,SAAO,SAAS,KAAK,KAAK,IAAI,QAAQ;AACxC;AAEO,SAAS,QAAQ,QAAgB,OAAmC;AACzE,QAAM,QAAQ,OAAO,MAAM;AAC3B,MAAI,SAAS,MAAM,MAAM,KAAK,EAAG,QAAO;AACxC,QAAM,QAAQ,QAAQ,MAAM,OAAO,KAAK,IAAI;AAC5C,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BAAM,MAAM,MAAM,GAAG,IAAI,YAAY;AACrC,gBAAI,OAAO,EAAE,MAAM;AAAA,UACrB,GAAG,KAAK;AACR,gBAAM,OAAO,KAAK;AAAA,QACpB;AAAA,QACA,MAAM,OAAO,OAAO;AAClB,gBAAM,MAAM,KAAK;AACjB,gBAAM,OAAO,KAAK;AAAA,QACpB;AAAA,QACA,MAAM,MAAM,OAAO;AACjB,cAAI,KAAK;AACT,kBAAQ;AACR,uBAAa,KAAK;AAClB,gBAAM,SAAS,KAAK,KAAK;AAAA,QAC3B;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;",
|
|
6
|
+
"names": ["NO_OWNER"]
|
|
7
7
|
}
|
|
@@ -62,7 +62,7 @@ function sharedPaths(shared) {
|
|
|
62
62
|
profile: `${shared}/.welcome.json`
|
|
63
63
|
};
|
|
64
64
|
}
|
|
65
|
-
const NO_EXEC = "
|
|
65
|
+
const NO_EXEC = "a sprinkle runs commands only through the scripts in its own cgi-bin/ folder, or by sending the agent a message with slicc.lick()";
|
|
66
66
|
export {
|
|
67
67
|
DEFAULT_ICON,
|
|
68
68
|
NAME,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/sprinkles/sprinkles.ts"],
|
|
4
|
-
"sourcesContent": ["import type { Context, JsonValue } from '@earendil-works/chord';\nimport { defineDoc } from '@earendil-works/pi-durable';\nimport type { ExecutionEnv } from '@earendil-works/pi-durable/env';\nimport { boundedDeltas } from '../checkpoints.ts';\nimport { normalize } from '../kernel/paths.ts';\n\nexport { SPRINKLE_KIND, type Sprinkle } from './kind.ts';\n\nexport const SPRINKLES_DIR = '/home/sprinkles';\nexport const WELCOME = 'welcome';\nexport const SUGGESTIONS = 'suggestions';\nexport const SUGGESTIONS_DIR = '/home/.gelatiere';\nexport const SUGGESTIONS_STORE = `${SUGGESTIONS_DIR}/suggestions.json`;\nexport const WELCOMED = '/home/.welcomed';\nexport const WELCOME_PROFILE = '/home/.welcome.json';\nexport const DEFAULT_ICON = 'sparkles';\n\nexport type SprinkleMethod = 'readFile' | 'exists' | 'getState' | 'setState';\n\nexport const SprinklesDoc = defineDoc<{\n owners: Record<string, string>;\n state: Record<string, JsonValue>;\n welcomed: boolean;\n}>({\n kind: 'slicc.sprinkles',\n checkpointWhen: boundedDeltas,\n version: 1,\n scope: 'session',\n initial: () => ({ owners: {}, state: {}, welcomed: false }),\n});\n\nfunction decode(text: string): string {\n return text\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/"/g, '\"')\n .replace(/'/g, \"'\")\n .replace(/&/g, '&');\n}\n\nexport function describe(name: string, html: string): { title: string; icon: string } {\n const title = /<title>([\\s\\S]*?)<\\/title>/i.exec(html)?.[1]?.trim();\n const icon =\n /<link\\b[^>]*\\brel=[\"']icon[\"'][^>]*\\bhref=[\"']([^\"']+)[\"']/i.exec(html)?.[1] ??\n /<link\\b[^>]*\\bhref=[\"']([^\"']+)[\"'][^>]*\\brel=[\"']icon[\"']/i.exec(html)?.[1];\n return { title: title ? decode(title) : name, icon: icon ?? DEFAULT_ICON };\n}\n\nexport const NAME = /^[a-z0-9][a-z0-9._-]{0,63}$/;\n\nexport async function sprinkleFiles(\n env: Pick<ExecutionEnv, 'listDir'>,\n dir: string,\n context: Context\n): Promise<{ name: string; path: string }[]> {\n const listed = await env.listDir(dir, context);\n if (!listed.ok) return [];\n const out = new Map<string, string>();\n for (const entry of [...listed.value].sort((a, b) => a.name.localeCompare(b.name))) {\n if (entry.kind !== 'directory' && entry.name.endsWith('.shtml')) {\n const name = entry.name.slice(0, -'.shtml'.length);\n if (NAME.test(name)) out.set(name, `${dir}/${entry.name}`);\n }\n if (entry.kind === 'directory' && NAME.test(entry.name) && !out.has(entry.name)) {\n const inner = await env.listDir(`${dir}/${entry.name}`, context);\n if (inner.ok && inner.value.some((item) => item.name === `${entry.name}.shtml`))\n out.set(entry.name, `${dir}/${entry.name}/${entry.name}.shtml`);\n }\n }\n return [...out].map(([name, path]) => ({ name, path }));\n}\n\nexport function homePath(path: unknown, shared = '/home', root = '/home'): string {\n if (typeof path !== 'string' || !path) throw new Error('a sprinkle reads files by absolute path');\n const mapped = path.startsWith('/shared/') ? `${shared}/${path.slice('/shared/'.length)}` : path;\n const segments = mapped.split('/');\n if (segments.includes('.') || segments.includes('..'))\n throw new Error(`a sprinkle reads paths without . or .. segments, not ${path}`);\n const resolved = normalize(mapped);\n if (resolved !== root && !resolved.startsWith(`${root}/`))\n throw new Error(`a sprinkle can only read files in ${root}, not ${path}`);\n return resolved;\n}\n\nexport function sharedPaths(shared: string) {\n return {\n suggestionsDir: `${shared}/.gelatiere`,\n suggestions: `${shared}/.gelatiere/suggestions.json`,\n welcomed: `${shared}/.welcomed`,\n profile: `${shared}/.welcome.json`,\n };\n}\n\nexport const NO_EXEC =\n
|
|
4
|
+
"sourcesContent": ["import type { Context, JsonValue } from '@earendil-works/chord';\nimport { defineDoc } from '@earendil-works/pi-durable';\nimport type { ExecutionEnv } from '@earendil-works/pi-durable/env';\nimport { boundedDeltas } from '../checkpoints.ts';\nimport { normalize } from '../kernel/paths.ts';\n\nexport { SPRINKLE_KIND, type Sprinkle } from './kind.ts';\n\nexport const SPRINKLES_DIR = '/home/sprinkles';\nexport const WELCOME = 'welcome';\nexport const SUGGESTIONS = 'suggestions';\nexport const SUGGESTIONS_DIR = '/home/.gelatiere';\nexport const SUGGESTIONS_STORE = `${SUGGESTIONS_DIR}/suggestions.json`;\nexport const WELCOMED = '/home/.welcomed';\nexport const WELCOME_PROFILE = '/home/.welcome.json';\nexport const DEFAULT_ICON = 'sparkles';\n\nexport type SprinkleMethod = 'readFile' | 'exists' | 'getState' | 'setState';\n\nexport const SprinklesDoc = defineDoc<{\n owners: Record<string, string>;\n state: Record<string, JsonValue>;\n welcomed: boolean;\n}>({\n kind: 'slicc.sprinkles',\n checkpointWhen: boundedDeltas,\n version: 1,\n scope: 'session',\n initial: () => ({ owners: {}, state: {}, welcomed: false }),\n});\n\nfunction decode(text: string): string {\n return text\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/"/g, '\"')\n .replace(/'/g, \"'\")\n .replace(/&/g, '&');\n}\n\nexport function describe(name: string, html: string): { title: string; icon: string } {\n const title = /<title>([\\s\\S]*?)<\\/title>/i.exec(html)?.[1]?.trim();\n const icon =\n /<link\\b[^>]*\\brel=[\"']icon[\"'][^>]*\\bhref=[\"']([^\"']+)[\"']/i.exec(html)?.[1] ??\n /<link\\b[^>]*\\bhref=[\"']([^\"']+)[\"'][^>]*\\brel=[\"']icon[\"']/i.exec(html)?.[1];\n return { title: title ? decode(title) : name, icon: icon ?? DEFAULT_ICON };\n}\n\nexport const NAME = /^[a-z0-9][a-z0-9._-]{0,63}$/;\n\nexport async function sprinkleFiles(\n env: Pick<ExecutionEnv, 'listDir'>,\n dir: string,\n context: Context\n): Promise<{ name: string; path: string }[]> {\n const listed = await env.listDir(dir, context);\n if (!listed.ok) return [];\n const out = new Map<string, string>();\n for (const entry of [...listed.value].sort((a, b) => a.name.localeCompare(b.name))) {\n if (entry.kind !== 'directory' && entry.name.endsWith('.shtml')) {\n const name = entry.name.slice(0, -'.shtml'.length);\n if (NAME.test(name)) out.set(name, `${dir}/${entry.name}`);\n }\n if (entry.kind === 'directory' && NAME.test(entry.name) && !out.has(entry.name)) {\n const inner = await env.listDir(`${dir}/${entry.name}`, context);\n if (inner.ok && inner.value.some((item) => item.name === `${entry.name}.shtml`))\n out.set(entry.name, `${dir}/${entry.name}/${entry.name}.shtml`);\n }\n }\n return [...out].map(([name, path]) => ({ name, path }));\n}\n\nexport function homePath(path: unknown, shared = '/home', root = '/home'): string {\n if (typeof path !== 'string' || !path) throw new Error('a sprinkle reads files by absolute path');\n const mapped = path.startsWith('/shared/') ? `${shared}/${path.slice('/shared/'.length)}` : path;\n const segments = mapped.split('/');\n if (segments.includes('.') || segments.includes('..'))\n throw new Error(`a sprinkle reads paths without . or .. segments, not ${path}`);\n const resolved = normalize(mapped);\n if (resolved !== root && !resolved.startsWith(`${root}/`))\n throw new Error(`a sprinkle can only read files in ${root}, not ${path}`);\n return resolved;\n}\n\nexport function sharedPaths(shared: string) {\n return {\n suggestionsDir: `${shared}/.gelatiere`,\n suggestions: `${shared}/.gelatiere/suggestions.json`,\n welcomed: `${shared}/.welcomed`,\n profile: `${shared}/.welcome.json`,\n };\n}\n\nexport const NO_EXEC =\n 'a sprinkle runs commands only through the scripts in its own cgi-bin/ folder, or by sending the agent a message with slicc.lick()';\n"],
|
|
5
5
|
"mappings": "AACA,SAAS,iBAAiB;AAE1B,SAAS,qBAAqB;AAC9B,SAAS,iBAAiB;AAE1B,SAAS,qBAAoC;AAEtC,MAAM,gBAAgB;AACtB,MAAM,UAAU;AAChB,MAAM,cAAc;AACpB,MAAM,kBAAkB;AACxB,MAAM,oBAAoB,GAAG,eAAe;AAC5C,MAAM,WAAW;AACjB,MAAM,kBAAkB;AACxB,MAAM,eAAe;AAIrB,MAAM,eAAe,UAIzB;AAAA,EACD,MAAM;AAAA,EACN,gBAAgB;AAAA,EAChB,SAAS;AAAA,EACT,OAAO;AAAA,EACP,SAAS,OAAO,EAAE,QAAQ,CAAC,GAAG,OAAO,CAAC,GAAG,UAAU,MAAM;AAC3D,CAAC;AAED,SAAS,OAAO,MAAsB;AACpC,SAAO,KACJ,QAAQ,SAAS,GAAG,EACpB,QAAQ,SAAS,GAAG,EACpB,QAAQ,WAAW,GAAG,EACtB,QAAQ,UAAU,GAAG,EACrB,QAAQ,UAAU,GAAG;AAC1B;AAEO,SAAS,SAAS,MAAc,MAA+C;AACpF,QAAM,QAAQ,8BAA8B,KAAK,IAAI,IAAI,CAAC,GAAG,KAAK;AAClE,QAAM,OACJ,8DAA8D,KAAK,IAAI,IAAI,CAAC,KAC5E,8DAA8D,KAAK,IAAI,IAAI,CAAC;AAC9E,SAAO,EAAE,OAAO,QAAQ,OAAO,KAAK,IAAI,MAAM,MAAM,QAAQ,aAAa;AAC3E;AAEO,MAAM,O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M,GAAG;AACjC,MAAI,SAAS,SAAS,GAAG,KAAK,SAAS,SAAS,IAAI;AAClD,UAAM,IAAI,MAAM,wDAAwD,IAAI,EAAE;AAChF,QAAM,WAAW,UAAU,MAAM;AACjC,MAAI,aAAa,QAAQ,CAAC,SAAS,WAAW,GAAG,IAAI,GAAG;AACtD,UAAM,IAAI,MAAM,qCAAqC,IAAI,SAAS,IAAI,EAAE;AAC1E,SAAO;AACT;AAEO,SAAS,YAAY,QAAgB;AAC1C,SAAO;AAAA,IACL,gBAAgB,GAAG,MAAM;AAAA,IACzB,aAAa,GAAG,MAAM;AAAA,IACtB,UAAU,GAAG,MAAM;AAAA,IACnB,SAAS,GAAG,MAAM;AAAA,EACpB;AACF;AAEO,MAAM,UACX;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -5,6 +5,8 @@ export interface KernelStat {
|
|
|
5
5
|
size: number;
|
|
6
6
|
mtime: Date | number;
|
|
7
7
|
ino?: number;
|
|
8
|
+
mode?: number;
|
|
9
|
+
uid?: number;
|
|
8
10
|
}
|
|
9
11
|
export interface KernelFs {
|
|
10
12
|
readFile(path: string): Promise<Uint8Array>;
|
|
@@ -41,6 +43,7 @@ export interface SpawnOptions {
|
|
|
41
43
|
env?: Record<string, string>;
|
|
42
44
|
pgid?: number;
|
|
43
45
|
tag?: string;
|
|
46
|
+
stdin?: Uint8Array;
|
|
44
47
|
onStdout?(bytes: Uint8Array): void;
|
|
45
48
|
onStderr?(bytes: Uint8Array): void;
|
|
46
49
|
}
|
|
@@ -8,6 +8,7 @@ import { type Layout } from '../layout.ts';
|
|
|
8
8
|
import type { Licks } from '../licks/licks.ts';
|
|
9
9
|
import { type WebhookDelivery } from '../licks/sources.ts';
|
|
10
10
|
import { type Roles } from '../roles/roles.ts';
|
|
11
|
+
import type { SprinkleLick } from '../sprinkles/index.ts';
|
|
11
12
|
import type { Request, Runner } from './requests.ts';
|
|
12
13
|
import { type Answer, type Scoops } from './service.ts';
|
|
13
14
|
import { type SyncState } from './sync.ts';
|
|
@@ -35,6 +36,7 @@ export interface CliOptions {
|
|
|
35
36
|
panel?: (argv: readonly string[], caller: PanelCaller, context: Context) => Promise<Answer>;
|
|
36
37
|
licks?: Pick<Licks, 'deliver'>;
|
|
37
38
|
webhook?: (name: string, delivery: WebhookDelivery, context: Context) => Promise<boolean>;
|
|
39
|
+
sprinkle?: (id: string, ask: SprinkleLick, context: Context) => Promise<string | undefined>;
|
|
38
40
|
workflow?: (argv: readonly string[], who: Caller & {
|
|
39
41
|
taints: InheritedTaint[];
|
|
40
42
|
}, request: Request, context: Context) => Promise<Answer>;
|
|
@@ -32,6 +32,7 @@ export interface AttachOptions {
|
|
|
32
32
|
models?: CliOptions['models'];
|
|
33
33
|
panel?: CliOptions['panel'];
|
|
34
34
|
webhook?: CliOptions['webhook'];
|
|
35
|
+
sprinkle?: CliOptions['sprinkle'];
|
|
35
36
|
mcp?: (names: readonly string[]) => ToolRegistration[];
|
|
36
37
|
layout?: Layout;
|
|
37
38
|
spools?: Spools;
|
|
@@ -4,7 +4,7 @@ import { type Report } from './attempt.ts';
|
|
|
4
4
|
import { Emitter } from './emitter.ts';
|
|
5
5
|
export declare class SprinkleAdapter extends Emitter<SprinkleEvents> implements SprinklePort {
|
|
6
6
|
#private;
|
|
7
|
-
constructor(connection: AgentConnection, report?: Report);
|
|
7
|
+
constructor(connection: AgentConnection, report?: Report, tab?: string | null);
|
|
8
8
|
list(): readonly Sprinkle[];
|
|
9
9
|
send(id: string, payload: unknown): void;
|
|
10
10
|
call(id: string, method: string, args: readonly unknown[]): Promise<unknown>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Context, JsonValue } from '@earendil-works/chord';
|
|
2
|
+
import type { InheritedTaint } from '../guests/guard.ts';
|
|
3
|
+
import type { LickTarget } from '../licks/lick.ts';
|
|
4
|
+
import type { Licks } from '../licks/licks.ts';
|
|
5
|
+
import { type CgiRequest } from './cgi.ts';
|
|
6
|
+
import { type CgiKernel } from './cgi-run.ts';
|
|
7
|
+
import type { Sprinkle } from './kind.ts';
|
|
8
|
+
export declare const NO_OWNER = "this sprinkle has no owner, so it can only be shown";
|
|
9
|
+
export interface SprinkleLick {
|
|
10
|
+
target: string;
|
|
11
|
+
source: string | null;
|
|
12
|
+
text: string;
|
|
13
|
+
user: string | null;
|
|
14
|
+
taints: readonly InheritedTaint[];
|
|
15
|
+
}
|
|
16
|
+
export interface CgiBridgeOptions {
|
|
17
|
+
licks: Pick<Licks, 'deliver'>;
|
|
18
|
+
kernelOf?: (agentId: string) => Promise<CgiKernel | undefined>;
|
|
19
|
+
find(id: string): Sprinkle | undefined;
|
|
20
|
+
folder(id: string): string | undefined;
|
|
21
|
+
owner(id: string, context: Context): Promise<string | undefined>;
|
|
22
|
+
}
|
|
23
|
+
export declare function targetOf(agentId: string): LickTarget;
|
|
24
|
+
export declare function folderOf(name: string, path: string): string | undefined;
|
|
25
|
+
export declare function cgiRequest(value: unknown): CgiRequest;
|
|
26
|
+
export declare function cgiBridge(options: CgiBridgeOptions): {
|
|
27
|
+
methods(id: string, args: readonly unknown[], using: Context): Record<string, () => Promise<JsonValue>>;
|
|
28
|
+
lick(id: string, ask: SprinkleLick, using: Context): Promise<string | undefined>;
|
|
29
|
+
stop: () => void;
|
|
30
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { KernelClient, KernelFs, KernelProcess } from '../kernel/client.ts';
|
|
2
|
+
import { CGI_LIMITS, type CgiHeaders, type CgiRequest } from './cgi.ts';
|
|
3
|
+
export interface CgiKernel {
|
|
4
|
+
client: KernelClient;
|
|
5
|
+
user: string;
|
|
6
|
+
}
|
|
7
|
+
export interface CgiSite {
|
|
8
|
+
id: string;
|
|
9
|
+
folder: string;
|
|
10
|
+
owner: string;
|
|
11
|
+
}
|
|
12
|
+
export interface CgiReply {
|
|
13
|
+
status: number;
|
|
14
|
+
statusText: string;
|
|
15
|
+
headers: CgiHeaders;
|
|
16
|
+
body: string;
|
|
17
|
+
done: boolean;
|
|
18
|
+
stream: string | null;
|
|
19
|
+
}
|
|
20
|
+
export interface CgiChunk {
|
|
21
|
+
body: string;
|
|
22
|
+
done: boolean;
|
|
23
|
+
}
|
|
24
|
+
export interface CgiRunner {
|
|
25
|
+
start(site: CgiSite, kernel: CgiKernel, request: CgiRequest): Promise<CgiReply>;
|
|
26
|
+
read(stream: string, sprinkle: string): Promise<CgiChunk>;
|
|
27
|
+
close(stream: string, sprinkle: string): void;
|
|
28
|
+
stop(): void;
|
|
29
|
+
}
|
|
30
|
+
export type CgiLimits = typeof CGI_LIMITS;
|
|
31
|
+
type Found = {
|
|
32
|
+
path: string;
|
|
33
|
+
} | {
|
|
34
|
+
status: 403 | 404;
|
|
35
|
+
reason: string;
|
|
36
|
+
};
|
|
37
|
+
export declare function locate(fs: KernelFs, folder: string, script: string, uid: number): Promise<Found>;
|
|
38
|
+
export declare function uidOf(fs: KernelFs, user: string): Promise<number>;
|
|
39
|
+
export declare function cgiReply(status: number, text: string, more?: CgiHeaders): CgiReply;
|
|
40
|
+
export declare function appendLog(fs: KernelFs, path: string, text: string, max: number): Promise<void>;
|
|
41
|
+
export declare class CgiRun {
|
|
42
|
+
#private;
|
|
43
|
+
readonly sprinkle: string;
|
|
44
|
+
readonly answered: Promise<CgiReply>;
|
|
45
|
+
process: KernelProcess | undefined;
|
|
46
|
+
ended: boolean;
|
|
47
|
+
why: string | undefined;
|
|
48
|
+
constructor(sprinkle: string, limits: CgiLimits, publish: (run: CgiRun) => string, bodiless?: boolean);
|
|
49
|
+
stop(why: string): void;
|
|
50
|
+
fail(message: string): void;
|
|
51
|
+
readonly out: (bytes: Uint8Array) => void;
|
|
52
|
+
readonly err: (bytes: Uint8Array) => void;
|
|
53
|
+
end(code: number | undefined): string;
|
|
54
|
+
drain(): string;
|
|
55
|
+
get pending(): boolean;
|
|
56
|
+
wait(ms: number, later: (ms: number, run: () => void) => () => void): Promise<void>;
|
|
57
|
+
}
|
|
58
|
+
export declare function cgiRunner(limits?: CgiLimits, now?: () => Date): CgiRunner;
|
|
59
|
+
export {};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export declare const CGI_DIR = "cgi-bin";
|
|
2
|
+
export declare const CGI_TAG = "sprinkle:";
|
|
3
|
+
export declare const CGI_LIMITS: {
|
|
4
|
+
input: number;
|
|
5
|
+
output: number;
|
|
6
|
+
stderr: number;
|
|
7
|
+
head: number;
|
|
8
|
+
timeoutMs: number;
|
|
9
|
+
parallel: number;
|
|
10
|
+
pollMs: number;
|
|
11
|
+
keepMs: number;
|
|
12
|
+
log: number;
|
|
13
|
+
};
|
|
14
|
+
export type CgiHeaders = [string, string][];
|
|
15
|
+
export interface CgiRequest {
|
|
16
|
+
method: string;
|
|
17
|
+
path: string;
|
|
18
|
+
query: string;
|
|
19
|
+
headers: CgiHeaders;
|
|
20
|
+
body: Uint8Array | null;
|
|
21
|
+
tab: string | null;
|
|
22
|
+
}
|
|
23
|
+
export interface CgiHead {
|
|
24
|
+
status: number;
|
|
25
|
+
statusText: string;
|
|
26
|
+
headers: CgiHeaders;
|
|
27
|
+
}
|
|
28
|
+
export type CgiParsed = {
|
|
29
|
+
kind: 'head';
|
|
30
|
+
head: CgiHead;
|
|
31
|
+
body: Uint8Array;
|
|
32
|
+
} | {
|
|
33
|
+
kind: 'body';
|
|
34
|
+
} | {
|
|
35
|
+
kind: 'more';
|
|
36
|
+
} | {
|
|
37
|
+
kind: 'bad';
|
|
38
|
+
reason: string;
|
|
39
|
+
};
|
|
40
|
+
export declare const REASONS: Record<number, string>;
|
|
41
|
+
export declare function scriptOf(path: unknown): {
|
|
42
|
+
script: string;
|
|
43
|
+
pathInfo: string;
|
|
44
|
+
} | undefined;
|
|
45
|
+
export declare function methodOf(method: unknown): string | undefined;
|
|
46
|
+
export declare function requestHeaders(headers: CgiHeaders): Record<string, string>;
|
|
47
|
+
export interface CgiContext {
|
|
48
|
+
request: CgiRequest;
|
|
49
|
+
script: string;
|
|
50
|
+
pathInfo: string;
|
|
51
|
+
folder: string;
|
|
52
|
+
sprinkle: string;
|
|
53
|
+
owner: string;
|
|
54
|
+
user: string;
|
|
55
|
+
}
|
|
56
|
+
export declare function cgiEnv(at: CgiContext): Record<string, string>;
|
|
57
|
+
export declare function headOf(lines: readonly string[]): CgiHead | string;
|
|
58
|
+
export declare function parseHead(bytes: Uint8Array, ended: boolean): CgiParsed;
|
|
59
|
+
export declare function shebang(bytes: Uint8Array): string[] | undefined;
|
|
60
|
+
export interface Mode {
|
|
61
|
+
mode?: number;
|
|
62
|
+
uid?: number;
|
|
63
|
+
}
|
|
64
|
+
export declare function openTo(stat: Mode, uid: number): string | undefined;
|
|
65
|
+
export declare function inside(path: string, root: string): boolean;
|
|
66
|
+
export declare function tagOf(id: string): string;
|
|
67
|
+
export declare function sprinklesOf(tags: readonly string[]): string[];
|
|
68
|
+
export declare function toBase64(bytes: Uint8Array): string;
|
|
69
|
+
export declare function fromBase64(text: string): Uint8Array;
|
|
@@ -3,6 +3,8 @@ import type { Harness } from '@earendil-works/pi-durable';
|
|
|
3
3
|
import type { ExecutionEnv } from '@earendil-works/pi-durable/env';
|
|
4
4
|
import { type Agents } from '../agents.ts';
|
|
5
5
|
import type { Licks } from '../licks/licks.ts';
|
|
6
|
+
import { type SprinkleLick } from './bridge.ts';
|
|
7
|
+
import type { CgiKernel } from './cgi-run.ts';
|
|
6
8
|
import { type Sprinkle } from './sprinkles.ts';
|
|
7
9
|
export interface SprinklePayload {
|
|
8
10
|
action: string;
|
|
@@ -15,6 +17,7 @@ export interface SprinklesRuntime {
|
|
|
15
17
|
delivered: boolean;
|
|
16
18
|
}>;
|
|
17
19
|
call(id: string, method: string, args: readonly unknown[], context: Context): Promise<JsonValue>;
|
|
20
|
+
lick(id: string, ask: SprinkleLick, context: Context): Promise<string | undefined>;
|
|
18
21
|
claim(id: string, cone: string, context: Context): Promise<void>;
|
|
19
22
|
reload(context: Context): Promise<void>;
|
|
20
23
|
close(context: Context): Promise<void>;
|
|
@@ -32,6 +35,7 @@ export interface SprinklesAttach {
|
|
|
32
35
|
envOf?: (agentId: string, context: Context) => Promise<ExecutionEnv | undefined>;
|
|
33
36
|
homeOf?: (agentId: string) => string | undefined;
|
|
34
37
|
shared?: string;
|
|
38
|
+
kernelOf?: (agentId: string) => Promise<CgiKernel | undefined>;
|
|
35
39
|
}
|
|
36
40
|
export interface SprinklePlace {
|
|
37
41
|
dir: string;
|
|
@@ -39,7 +43,7 @@ export interface SprinklePlace {
|
|
|
39
43
|
}
|
|
40
44
|
export declare function liveOwner(agents: Agents, owner: string | undefined): string | undefined;
|
|
41
45
|
export declare function ownerOf(agents: Agents, owner: string | undefined): string;
|
|
42
|
-
export
|
|
46
|
+
export { NO_OWNER, type SprinkleLick } from './bridge.ts';
|
|
43
47
|
export declare function ownedRead(files: ExecutionEnv, asked: string, home: string, using: Context): Promise<{
|
|
44
48
|
files: ExecutionEnv;
|
|
45
49
|
path: string;
|
|
@@ -31,4 +31,4 @@ export declare function sharedPaths(shared: string): {
|
|
|
31
31
|
welcomed: string;
|
|
32
32
|
profile: string;
|
|
33
33
|
};
|
|
34
|
-
export declare const NO_EXEC = "
|
|
34
|
+
export declare const NO_EXEC = "a sprinkle runs commands only through the scripts in its own cgi-bin/ folder, or by sending the agent a message with slicc.lick()";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-ecoverse/slicc-agent",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.16.1",
|
|
4
4
|
"description": "SLICC's agent core on pi durable: an agent worker, session storage, remoting and pi extensions",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -72,10 +72,11 @@
|
|
|
72
72
|
"release": "semantic-release"
|
|
73
73
|
},
|
|
74
74
|
"devDependencies": {
|
|
75
|
-
"@ai-ecoverse/slicc-
|
|
75
|
+
"@ai-ecoverse/slicc-jsh": "0.64.0",
|
|
76
|
+
"@ai-ecoverse/slicc-kernel": "1.172.2",
|
|
76
77
|
"@ai-ecoverse/slicc-scrub": "1.1.0",
|
|
77
78
|
"@ai-ecoverse/slicc-shared-web": "2.13.10",
|
|
78
|
-
"@ai-ecoverse/slicc-spectrum": "1.
|
|
79
|
+
"@ai-ecoverse/slicc-spectrum": "1.111.0",
|
|
79
80
|
"@ai-ecoverse/wasi-fnox": "1.43.0-1",
|
|
80
81
|
"@ai-ecoverse/wasm-bash": "5.3.0-23",
|
|
81
82
|
"@ai-ecoverse/wasm-coreutils": "9.12.0-15",
|
|
@@ -105,7 +106,7 @@
|
|
|
105
106
|
"yaml": "2.9.1"
|
|
106
107
|
},
|
|
107
108
|
"peerDependencies": {
|
|
108
|
-
"@ai-ecoverse/slicc-kernel": ">=1.
|
|
109
|
+
"@ai-ecoverse/slicc-kernel": ">=1.172.2 <2",
|
|
109
110
|
"@ai-ecoverse/slicc-spectrum": "^1.103.0"
|
|
110
111
|
},
|
|
111
112
|
"peerDependenciesMeta": {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: sprinkles
|
|
3
|
-
description: Build a sprinkle, a small HTML panel next to the chat (a dashboard, form, report or visualization) that the user opens from the rail, or answer a <lick channel="sprinkle"> from one. Covers the .shtml file, the slicc bridge in the frame, and opening it with panel open sprinkle:<name>.
|
|
3
|
+
description: Build a sprinkle, a small HTML panel next to the chat (a dashboard, form, report or visualization) that the user opens from the rail, or answer a <lick channel="sprinkle"> from one. Covers the .shtml file, the slicc bridge in the frame, its cgi-bin/ scripts, and opening it with panel open sprinkle:<name>.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Sprinkles
|
|
@@ -37,11 +37,30 @@ A sprinkle is an `.shtml` file in `/home/sprinkles/`, as `<name>.shtml` or `<nam
|
|
|
37
37
|
|
|
38
38
|
Inside the frame, `slicc` has:
|
|
39
39
|
|
|
40
|
-
- `slicc.lick({ action, data })` (or `slicc.lick('action')`): sends you a lick on the `sprinkle` channel. Its text is the action, its body the data as JSON.
|
|
40
|
+
- `slicc.lick({ action, data })` (or `slicc.lick('action')`): sends you a lick on the `sprinkle` channel. Its text is the action, its body the data as JSON.
|
|
41
41
|
- `slicc.readFile(path)` and `slicc.exists(path)`: read files under `/home` (paths under `/shared/` mean `/home/`).
|
|
42
42
|
- `slicc.getState()` and `slicc.setState(value)`: keep a small JSON value per sprinkle, across reloads.
|
|
43
43
|
|
|
44
|
-
There's no `exec
|
|
44
|
+
There's no `exec` or `writeFile` in the frame: a sprinkle that needs a command runs one of its own `cgi-bin/` scripts, or sends you a lick, you run it, and you update a file the sprinkle reads. To push new data, write that file; the sprinkle reads it again on its next load or poll.
|
|
45
|
+
|
|
46
|
+
## cgi-bin
|
|
47
|
+
|
|
48
|
+
A sprinkle in a folder can do things without a turn of yours: put scripts in `/home/sprinkles/<name>/cgi-bin/`, and the frame calls them with `fetch('cgi-bin/<script>')` or a `<form action="cgi-bin/<script>" method="post">`. They follow CGI/1.1, so the folder also runs under `python3 -m http.server --cgi`.
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
#!/bin/sh
|
|
52
|
+
# cgi-bin/items: GET answers the store
|
|
53
|
+
printf 'Content-Type: application/json\r\n\r\n'
|
|
54
|
+
cat store.json
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
- The script runs by its `#!` line as the sprinkle's owner, in the sprinkle folder, at most 30 s. It gets the request only as `REQUEST_METHOD`, `QUERY_STRING`, `PATH_INFO` (`cgi-bin/item/42` gives `/42`), `CONTENT_TYPE`, `CONTENT_LENGTH`, `HTTP_*`, `SLICC_SPRINKLE`, `SLICC_OWNER` and `SLICC_TAB`, with the body (at most 64 KB) on stdin. Never build a shell command from them.
|
|
58
|
+
- It prints headers (`Status: 404 Not Found`, `Content-Type:`, `Location:`), a blank line, then the body. Without headers, its output is a `200 text/plain` body; a failing script with no output is a `500` with its stderr.
|
|
59
|
+
- stderr goes to `<name>/error.log`: read it when a script misbehaves.
|
|
60
|
+
- Keep `cgi-bin/` and its scripts writable only by you: anything else, and `..`, dotfiles or links out of the folder, are refused.
|
|
61
|
+
- GET should only read; POST changes things.
|
|
62
|
+
- To tell its owner what happened, a script runs `agent send "$SLICC_OWNER" --lick=sprinkle --source <name> -` with the message on stdin. Only the sprinkle's own scripts may send that lick.
|
|
63
|
+
- A frame whose SLICC can't run `cgi-bin/` gets a failed `fetch`: fall back to `slicc.lick()` then.
|
|
45
64
|
|
|
46
65
|
## Answer its licks
|
|
47
66
|
|