@blueshed/joined 0.0.0-stage → 0.1.20
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/.claude/skills/joined/SKILL.md +17 -0
- package/.claude/skills/joined/reference.md +768 -0
- package/LICENSE +19 -0
- package/README.md +115 -2
- package/docs/design.md +148 -0
- package/docs/introduction.md +193 -0
- package/docs/learning.md +76 -0
- package/docs/stories.md +82 -0
- package/examples/app.css +27 -0
- package/examples/cafe/books.html +38 -0
- package/examples/cafe/books.json +13 -0
- package/examples/cafe/books.ts +64 -0
- package/examples/cafe/cafe.css +512 -0
- package/examples/cafe/front.html +115 -0
- package/examples/cafe/front.ts +7 -0
- package/examples/cafe/generated/books.elements.ts +52 -0
- package/examples/cafe/generated/books.queries.ts +11 -0
- package/examples/cafe/generated/books.rules.ts +42 -0
- package/examples/cafe/generated/kitchen.elements.ts +87 -0
- package/examples/cafe/generated/kitchen.queries.ts +47 -0
- package/examples/cafe/generated/kitchen.rules.ts +98 -0
- package/examples/cafe/generated/menu.elements.ts +61 -0
- package/examples/cafe/generated/menu.queries.ts +28 -0
- package/examples/cafe/generated/menu.rules.ts +65 -0
- package/examples/cafe/generated/order.elements.ts +87 -0
- package/examples/cafe/generated/order.queries.ts +47 -0
- package/examples/cafe/generated/order.rules.ts +95 -0
- package/examples/cafe/generated/staff.elements.ts +55 -0
- package/examples/cafe/generated/staff.queries.ts +27 -0
- package/examples/cafe/generated/staff.rules.ts +59 -0
- package/examples/cafe/generated/supplies.elements.ts +62 -0
- package/examples/cafe/generated/supplies.queries.ts +43 -0
- package/examples/cafe/generated/supplies.rules.ts +84 -0
- package/examples/cafe/generated/till.elements.ts +93 -0
- package/examples/cafe/generated/till.queries.ts +57 -0
- package/examples/cafe/generated/till.rules.ts +113 -0
- package/examples/cafe/index.html +70 -0
- package/examples/cafe/kitchen.css +107 -0
- package/examples/cafe/kitchen.html +39 -0
- package/examples/cafe/kitchen.ts +125 -0
- package/examples/cafe/menu.html +55 -0
- package/examples/cafe/menu.json +41 -0
- package/examples/cafe/menu.ts +84 -0
- package/examples/cafe/migrations/001-cafe.ts +11 -0
- package/examples/cafe/order.html +37 -0
- package/examples/cafe/order.ts +45 -0
- package/examples/cafe/rail-drag.ts +30 -0
- package/examples/cafe/routes.ts +32 -0
- package/examples/cafe/staff.json +20 -0
- package/examples/cafe/stories.jsonl +43 -0
- package/examples/cafe/supplies.html +77 -0
- package/examples/cafe/supplies.json +33 -0
- package/examples/cafe/supplies.ts +170 -0
- package/examples/cafe/till.json +95 -0
- package/examples/cafe/till.ts +122 -0
- package/examples/cafe/trust.ts +20 -0
- package/examples/cafe/views.ts +171 -0
- package/examples/docs.ts +28 -0
- package/examples/film.jpg +0 -0
- package/examples/hello/generated/elements.ts +45 -0
- package/examples/hello/generated/queries.ts +24 -0
- package/examples/hello/generated/rules.ts +45 -0
- package/examples/hello/hello.css +27 -0
- package/examples/hello/hello.json +1 -0
- package/examples/hello/index.html +32 -0
- package/examples/hello/migrations/001-hello.ts +6 -0
- package/examples/hello/page.ts +49 -0
- package/examples/hello/routes.ts +6 -0
- package/examples/hello/stories.jsonl +3 -0
- package/examples/index.html +146 -0
- package/examples/page.ts +11 -0
- package/examples/people/adding.ts +20 -0
- package/examples/people/generated/elements.ts +68 -0
- package/examples/people/generated/queries.ts +28 -0
- package/examples/people/generated/rules.ts +85 -0
- package/examples/people/links.ts +124 -0
- package/examples/people/migrations/001-people.ts +6 -0
- package/examples/people/migrations/002-add-people-member-and-guard-people.ts +7 -0
- package/examples/people/migrations/003-add-people-rank-and-guard-people.ts +7 -0
- package/examples/people/order.ts +61 -0
- package/examples/people/people.css +65 -0
- package/examples/people/people.json +26 -0
- package/examples/people/routes.ts +12 -0
- package/examples/people/stories.jsonl +20 -0
- package/examples/people/trust.ts +14 -0
- package/examples/people/view.ts +115 -0
- package/examples/routes.ts +11 -0
- package/examples/trip/answer-rows.ts +70 -0
- package/examples/trip/answers.ts +11 -0
- package/examples/trip/bar.ts +89 -0
- package/examples/trip/calendar-map.css +709 -0
- package/examples/trip/calendar-map.ts +249 -0
- package/examples/trip/drags.ts +143 -0
- package/examples/trip/drawer.ts +250 -0
- package/examples/trip/event-form.ts +199 -0
- package/examples/trip/generated/elements.ts +144 -0
- package/examples/trip/generated/queries.ts +46 -0
- package/examples/trip/generated/rules.ts +239 -0
- package/examples/trip/holding.ts +61 -0
- package/examples/trip/ics.ts +117 -0
- package/examples/trip/index.html +82 -0
- package/examples/trip/line-stops.ts +92 -0
- package/examples/trip/line-words.ts +79 -0
- package/examples/trip/line.ts +121 -0
- package/examples/trip/map.ts +234 -0
- package/examples/trip/mercator.ts +28 -0
- package/examples/trip/migrations/001-trip.ts +7 -0
- package/examples/trip/migrations/002-add-events-zone-and-guard-events.ts +7 -0
- package/examples/trip/migrations/003-guard-events-and-create-events_asked-and-more.ts +9 -0
- package/examples/trip/migrations/004-create-stays.ts +6 -0
- package/examples/trip/migrations/005-guard-events-and-create-events_arrives-and-more.ts +8 -0
- package/examples/trip/month.ts +87 -0
- package/examples/trip/other-weeks.ts +67 -0
- package/examples/trip/page.ts +121 -0
- package/examples/trip/peek.ts +71 -0
- package/examples/trip/people-chips.ts +74 -0
- package/examples/trip/place-search.ts +51 -0
- package/examples/trip/places.ts +34 -0
- package/examples/trip/routes.ts +27 -0
- package/examples/trip/shell.ts +39 -0
- package/examples/trip/stays.css +51 -0
- package/examples/trip/stays.ts +68 -0
- package/examples/trip/stories.jsonl +72 -0
- package/examples/trip/time.ts +46 -0
- package/examples/trip/told.ts +48 -0
- package/examples/trip/trip.json +69 -0
- package/examples/trip/trust.ts +51 -0
- package/examples/trip/week.ts +237 -0
- package/examples/trip/words.ts +4 -0
- package/examples/trip/writes.ts +91 -0
- package/examples/trip/zones.ts +16 -0
- package/examples/trips/card.ts +149 -0
- package/examples/trips/generated/elements.ts +60 -0
- package/examples/trips/generated/queries.ts +28 -0
- package/examples/trips/generated/rules.ts +62 -0
- package/examples/trips/index.html +47 -0
- package/examples/trips/map.ts +52 -0
- package/examples/trips/migrations/001-trips.ts +8 -0
- package/examples/trips/new-trip.ts +46 -0
- package/examples/trips/page.ts +9 -0
- package/examples/trips/routes.ts +7 -0
- package/examples/trips/stories.jsonl +12 -0
- package/examples/trips/trips.css +183 -0
- package/examples/trips/trips.json +7 -0
- package/examples/weeks/derive.ts +23 -0
- package/examples/weeks/generated/elements.ts +52 -0
- package/examples/weeks/generated/rules.ts +33 -0
- package/examples/weeks/routes.ts +4 -0
- package/examples/weeks/weeks.json +1 -0
- package/examples/welcome.css +299 -0
- package/package.json +55 -4
- package/src/attachment.ts +70 -0
- package/src/cli.ts +52 -0
- package/src/client.ts +98 -0
- package/src/compile.ts +2034 -0
- package/src/css.d.ts +5 -0
- package/src/definitions.ts +268 -0
- package/src/demo.css +29 -0
- package/src/demo.ts +42 -0
- package/src/draft.ts +69 -0
- package/src/engine.ts +288 -0
- package/src/file-value.ts +13 -0
- package/src/files.ts +187 -0
- package/src/follow.ts +56 -0
- package/src/hues.ts +3 -0
- package/src/icon-names.ts +77 -0
- package/src/icon.png +0 -0
- package/src/icons.ts +32 -0
- package/src/keep.ts +33 -0
- package/src/link.ts +31 -0
- package/src/mcp.ts +252 -0
- package/src/members/demo.ts +53 -0
- package/src/members/generated/elements.ts +68 -0
- package/src/members/generated/queries.ts +29 -0
- package/src/members/generated/rules.ts +65 -0
- package/src/members/group.ts +294 -0
- package/src/members/keys.ts +221 -0
- package/src/members/members.json +9 -0
- package/src/members/migrations/001-members.ts +6 -0
- package/src/members/migrations/002-guard-members.ts +11 -0
- package/src/members/migrations/003-add-members-act-and-guard-members.ts +7 -0
- package/src/members/panel.css +208 -0
- package/src/members/panel.ts +398 -0
- package/src/members/routes.ts +4 -0
- package/src/members/stories.jsonl +27 -0
- package/src/members/trust.ts +116 -0
- package/src/migrate.ts +597 -0
- package/src/migrations/001-joined.ts +13 -0
- package/src/migrations/002-group-keys.ts +16 -0
- package/src/migrations/003-change-inverse.ts +10 -0
- package/src/migrations/004-change-followed.ts +6 -0
- package/src/ops.ts +61 -0
- package/src/page.css +79 -0
- package/src/page.ts +413 -0
- package/src/parts.css +471 -0
- package/src/parts.ts +152 -0
- package/src/server.ts +862 -0
- package/src/settings.ts +94 -0
- package/src/starter.ts +89 -0
- package/src/store.ts +815 -0
- package/src/stories.ts +387 -0
- package/src/toast.ts +52 -0
- package/src/together.ts +167 -0
- package/src/tokens.css +85 -0
- package/src/tokens.json +457 -0
- package/src/upkeep.ts +86 -0
- package/src/visitors.ts +43 -0
- package/src/write.ts +567 -0
- package/starter/CLAUDE.md +37 -0
- package/starter/gitignore +3 -0
- package/starter/tests/hello-page.test.ts +14 -0
- package/starter/tests/hello.test.ts +8 -0
- package/starter/tsconfig.json +17 -0
package/src/keep.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// A display's list kept by key, as the applier (src/page.ts) keeps a document's items in the light DOM: a
|
|
2
|
+
// display's own rows, in its shadow. A display imports this; a page needn't.
|
|
3
|
+
|
|
4
|
+
/** A row as a list wants it: its key, what it shows (said as a string, to see whether it has changed), and how it's made. */
|
|
5
|
+
export type Row = { key: string; shows: string; make: () => HTMLElement };
|
|
6
|
+
|
|
7
|
+
/** Whether something is being typed in: a field, a text area, a choice or what's editable. A button or a tick is
|
|
8
|
+
* done once it's pressed, though it keeps the focus a hand's press gives it. */
|
|
9
|
+
const typing = (el: Element | null) =>
|
|
10
|
+
el instanceof HTMLTextAreaElement ||
|
|
11
|
+
el instanceof HTMLSelectElement ||
|
|
12
|
+
(el instanceof HTMLElement && el.isContentEditable) ||
|
|
13
|
+
(el instanceof HTMLInputElement &&
|
|
14
|
+
!["button", "submit", "reset", "checkbox", "radio", "range", "color", "file", "image"].includes(el.type));
|
|
15
|
+
|
|
16
|
+
/** A display's list kept by key: a row is made again only when what it shows has changed (and never while
|
|
17
|
+
* something in it is being typed in: a button just pressed doesn't hold it), one gone is taken away, and the rest
|
|
18
|
+
* are moved into their order, never made again. */
|
|
19
|
+
export function keep(list: HTMLElement, kept: Map<string, { shows: string; el: HTMLElement }>, rows: Row[]) {
|
|
20
|
+
const wanted = new Set(rows.map((r) => r.key));
|
|
21
|
+
const active = (list.getRootNode() as ShadowRoot | Document).activeElement;
|
|
22
|
+
for (const [key, k] of kept) if (!wanted.has(key)) (k.el.remove(), kept.delete(key));
|
|
23
|
+
rows.forEach((row, i) => {
|
|
24
|
+
let k = kept.get(row.key);
|
|
25
|
+
const busy = typing(active) && k?.el.contains(active);
|
|
26
|
+
if (!k || (k.shows !== row.shows && !busy)) {
|
|
27
|
+
const el = row.make();
|
|
28
|
+
k?.el.replaceWith(el);
|
|
29
|
+
kept.set(row.key, (k = { shows: row.shows, el }));
|
|
30
|
+
}
|
|
31
|
+
if (list.children[i] !== k.el) list.insertBefore(k.el, list.children[i] ?? null);
|
|
32
|
+
});
|
|
33
|
+
}
|
package/src/link.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// bun run link <name>: a member's link, printed, for the terminal (Bun reads ROOT_TOKEN from .env).
|
|
2
|
+
// It reads the members and the keys as the running server keeps them, and changes nothing: the
|
|
3
|
+
// database is opened read-only.
|
|
4
|
+
|
|
5
|
+
import { DB } from "./compile.ts";
|
|
6
|
+
import { sqlite } from "./engine.ts";
|
|
7
|
+
import { openKeys } from "./members/keys.ts";
|
|
8
|
+
import { bdflOf, type Member } from "./members/trust.ts";
|
|
9
|
+
|
|
10
|
+
/** Where the links point: JOINED_URL, or this machine on the port joined serves (PORT, as `joined start` reads it). */
|
|
11
|
+
export const baseOf = (env: Record<string, string | undefined> = Bun.env) =>
|
|
12
|
+
env.JOINED_URL ?? `http://localhost:${env.PORT || 3380}`;
|
|
13
|
+
|
|
14
|
+
/** A member's link, by name: the BDFL's is ROOT_TOKEN's, anyone else's is from the keys. */
|
|
15
|
+
export async function linkFor(
|
|
16
|
+
name: string | undefined,
|
|
17
|
+
{ root = Bun.env.ROOT_TOKEN, base = baseOf(), db = DB }: { root?: string; base?: string; db?: string },
|
|
18
|
+
) {
|
|
19
|
+
if (!name || !root) throw new Error("bun run link <name>, with ROOT_TOKEN set (in .env)");
|
|
20
|
+
const store = sqlite(db, { readonly: true });
|
|
21
|
+
// a column left empty is a field not given, as the store reads it: the BDFL has no inviter
|
|
22
|
+
const all = (store.query("SELECT id, name, inviter FROM members").all() as Record<string, unknown>[]).map(
|
|
23
|
+
(r) => Object.fromEntries(Object.entries(r).filter(([, v]) => v !== null)) as Member,
|
|
24
|
+
);
|
|
25
|
+
const m = all.find((x) => x.name.toLowerCase() === name.toLowerCase());
|
|
26
|
+
const token = !m ? undefined : m.id === bdflOf(all)?.id ? root : (await openKeys(store, root)).peek(m.id);
|
|
27
|
+
store.close();
|
|
28
|
+
if (!m) throw new Error(`nobody in the group is called ${name}`);
|
|
29
|
+
if (!token) throw new Error(`${m.name} has no link yet: start joined once, and it makes one`);
|
|
30
|
+
return `${m.name}'s link: ${base}/auth/${token}`;
|
|
31
|
+
}
|
package/src/mcp.ts
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
// The MCP door: Claude in a chat (claude.ai, on a phone too) working with joined's documents as a custom connector,
|
|
2
|
+
// by an agent's link (made in the group's Agents tab): POST /mcp/:token, MCP's Streamable HTTP, answered in plain JSON.
|
|
3
|
+
// Only an agent's token opens it: a member's own, a removed agent's or an unknown one is refused. Where nobody's a
|
|
4
|
+
// member (every story anonymous), it's anyone's, as a stranger's page is: POST /mcp, with no link. The agent writes as a
|
|
5
|
+
// page does: each write its member's, via the agent, under the same rules (the app's trust.ts, the grants, the
|
|
6
|
+
// group's), whole or not at all, and told to every page at once. Its tools are written from each document's compiled
|
|
7
|
+
// rules, never the stories: read_<doc>, the document as its member sees it, and write_<doc>, one write, its ops typed
|
|
8
|
+
// as the grants allow; a kind's (trip:1) take the instance's key. A window isn't a tool's: a document is read whole,
|
|
9
|
+
// as its member sees it. Each app may give the agent its own words (its routes.ts's `instructions`), said as it opens.
|
|
10
|
+
|
|
11
|
+
import { FORMS, SAID } from "./compile.ts";
|
|
12
|
+
import type { Who } from "./members/group.ts";
|
|
13
|
+
import type { Op, Type } from "./ops.ts";
|
|
14
|
+
import type { Doc } from "./store.ts";
|
|
15
|
+
import type { Rules } from "./write.ts";
|
|
16
|
+
|
|
17
|
+
/** A write's answer: done, with the version it made and the ids of what it added, in order; or why it was refused. */
|
|
18
|
+
export type Written = { done: true; version: number; ids: number[] } | { why: string };
|
|
19
|
+
/** What the door is given by the server: the documents' rules, and a way to know a token, to hold, read and write. */
|
|
20
|
+
type Door = {
|
|
21
|
+
rules: Rules[];
|
|
22
|
+
whoIs(token: string): Promise<Who | null>;
|
|
23
|
+
/** Where nobody's a member: anyone's, with no link, as a stranger's page is. */
|
|
24
|
+
anyone?: boolean;
|
|
25
|
+
/** Why a member may not hold a document (or it isn't there), or null. */
|
|
26
|
+
holds(doc: string, member: number): string | null;
|
|
27
|
+
read(doc: string, member: number): Doc;
|
|
28
|
+
/** A write by an agent's member; or, at a door that's anyone's, nobody's (null), as a stranger's page writes. */
|
|
29
|
+
write(doc: string, ops: Op[], why: string | undefined, who: Who | null): Promise<Written>;
|
|
30
|
+
instructions: string;
|
|
31
|
+
version: string;
|
|
32
|
+
};
|
|
33
|
+
type Schema = Record<string, unknown>;
|
|
34
|
+
type Collection = Rules["collections"][string] & { needs?: readonly string[] };
|
|
35
|
+
/** A tool's answer, as MCP says it: its words, and whether it's a refusal. */
|
|
36
|
+
type Result = { content: { type: "text"; text: string }[]; isError?: true };
|
|
37
|
+
|
|
38
|
+
/** The versions of MCP it speaks: the one a client asks, if it's one of these, else the newest. */
|
|
39
|
+
const VERSIONS = ["2025-06-18", "2025-03-26", "2024-11-05"];
|
|
40
|
+
const ID = "[1-9]\\d*";
|
|
41
|
+
|
|
42
|
+
const answered = (value: unknown): Result => ({ content: [{ type: "text", text: JSON.stringify(value) }] });
|
|
43
|
+
const refused = (why: string): Result => ({ content: [{ type: "text", text: why }], isError: true });
|
|
44
|
+
|
|
45
|
+
/** A field's value, as its type says it: its JSON type, and a string's form and words, as the database holds it to. */
|
|
46
|
+
function formOf(c: Collection, field: string): Schema {
|
|
47
|
+
const t = c.fields[field] as Type;
|
|
48
|
+
if (t === "ids") return { type: "array", items: { type: "integer" }, description: `ids of ${c.links[field]}` };
|
|
49
|
+
const json = t === "boolean" ? "boolean" : t === "integer" ? "integer" : t === "decimal" ? "number" : "string";
|
|
50
|
+
const form = FORMS[t as keyof typeof FORMS];
|
|
51
|
+
return { type: json, description: SAID[t], ...(form ? { pattern: form.source } : {}) };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** An item as an add gives it: each field but what the server fills, an object's fields in their object, and those an
|
|
55
|
+
* add needs. */
|
|
56
|
+
function itemOf(c: Collection): Schema {
|
|
57
|
+
const properties: Record<string, Schema> = {},
|
|
58
|
+
filled = Object.keys(c.fills ?? {});
|
|
59
|
+
for (const f of Object.keys(c.fields).filter((f) => !filled.includes(f))) {
|
|
60
|
+
const [top, sub] = f.split(".") as [string, string?];
|
|
61
|
+
const into = sub
|
|
62
|
+
? ((properties[top] ??= { type: "object", properties: {} }).properties as Record<string, Schema>)
|
|
63
|
+
: properties;
|
|
64
|
+
into[sub ?? top] = formOf(c, f);
|
|
65
|
+
}
|
|
66
|
+
return { type: "object", properties, required: (c.needs ?? []).filter((f) => !filled.includes(f)) };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Each op a write may hold, read off a document's grants: the op on its path's form (`replace /events/{id}/title`), the
|
|
70
|
+
* pattern its path matches, and the value it takes; an add by a first step (`add posts.status:>waiting`), what it
|
|
71
|
+
* gives (`status waiting`). */
|
|
72
|
+
function opsOf(r: Rules): { op: string; path: string; pattern: string; value?: Schema; gives?: string }[] {
|
|
73
|
+
return Object.keys(r.grants ?? {}).map((grant) => {
|
|
74
|
+
const sp = grant.indexOf(" "),
|
|
75
|
+
op = grant.slice(0, sp),
|
|
76
|
+
of = grant.slice(sp + 1);
|
|
77
|
+
const dot = of.indexOf("."),
|
|
78
|
+
[coll, rest] = dot < 0 ? [of, ""] : [of.slice(0, dot), of.slice(dot + 1)];
|
|
79
|
+
const c = r.collections[coll] as Collection | undefined;
|
|
80
|
+
// a document's own field, at its top (/greeting)
|
|
81
|
+
if (!c) {
|
|
82
|
+
const own = Object.values(r.collections).find((c) => c.own) as Collection;
|
|
83
|
+
return { op, path: `/${of}`, pattern: `^/${of}$`, value: formOf(own, of) };
|
|
84
|
+
}
|
|
85
|
+
if (!rest)
|
|
86
|
+
return op === "add"
|
|
87
|
+
? { op, path: `/${coll}/-`, pattern: `^/${coll}/-$`, value: itemOf(c) }
|
|
88
|
+
: { op, path: `/${coll}/{id}`, pattern: `^/${coll}/${ID}$` };
|
|
89
|
+
// a link's id, added or taken
|
|
90
|
+
if (rest.endsWith("[]")) {
|
|
91
|
+
const f = rest.slice(0, -2);
|
|
92
|
+
return op === "add"
|
|
93
|
+
? {
|
|
94
|
+
op,
|
|
95
|
+
path: `/${coll}/{id}/${f}/-`,
|
|
96
|
+
pattern: `^/${coll}/${ID}/${f}/-$`,
|
|
97
|
+
value: { type: "integer", description: `an id of ${c.links[f]}` },
|
|
98
|
+
}
|
|
99
|
+
: { op, path: `/${coll}/{id}/${f}/{its id}`, pattern: `^/${coll}/${ID}/${f}/${ID}$` };
|
|
100
|
+
}
|
|
101
|
+
// a field (an object's, terms.days, at /terms/days), or a step of its move (status:placed>being made)
|
|
102
|
+
const [field, step] = rest.split(":") as [string, string?],
|
|
103
|
+
at = field.replaceAll(".", "/"),
|
|
104
|
+
[from, to] = step?.split(">") ?? [];
|
|
105
|
+
// an add by its field's first step: an item, giving that step
|
|
106
|
+
if (op === "add") {
|
|
107
|
+
const item = itemOf(c);
|
|
108
|
+
const value = { ...item, properties: { ...(item.properties as object), [field]: { const: to } } };
|
|
109
|
+
return { op, path: `/${coll}/-`, pattern: `^/${coll}/-$`, value, gives: `${field} ${to}` };
|
|
110
|
+
}
|
|
111
|
+
const value =
|
|
112
|
+
op === "remove" ? undefined : step ? { const: to, description: `only from ${from}` } : formOf(c, field);
|
|
113
|
+
return { op, path: `/${coll}/{id}/${at}`, pattern: `^/${coll}/${ID}/${at}$`, ...(value ? { value } : {}) };
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** What a document holds, said for an agent: each collection and its fields' types. */
|
|
118
|
+
const described = (r: Rules) =>
|
|
119
|
+
Object.entries(r.collections)
|
|
120
|
+
.map(
|
|
121
|
+
([name, c]) =>
|
|
122
|
+
`${c.own ? "its own fields" : name}: ${Object.entries(c.fields)
|
|
123
|
+
.map(([f, t]) => `${f} (${t})`)
|
|
124
|
+
.join(", ")}`,
|
|
125
|
+
)
|
|
126
|
+
.join("; ");
|
|
127
|
+
|
|
128
|
+
/** Who comes to a door that's anyone's: nobody, as a stranger's page is. */
|
|
129
|
+
const ANYONE: Who = { member: 0, token: "" };
|
|
130
|
+
|
|
131
|
+
/** The door: POST answers MCP's messages, by the agent its token names; a GET is refused, as no stream is kept. */
|
|
132
|
+
export function door(d: Door) {
|
|
133
|
+
const rpc = (id: unknown, result: unknown) => ({ jsonrpc: "2.0", id, result });
|
|
134
|
+
const fail = (id: unknown, code: number, message: string) => ({ jsonrpc: "2.0", id, error: { code, message } });
|
|
135
|
+
|
|
136
|
+
/** The tools for a member's agent: each document's read and, where pages write it, its write; a kind's take its key. */
|
|
137
|
+
function toolsFor(who: Who) {
|
|
138
|
+
const tools: {
|
|
139
|
+
name: string;
|
|
140
|
+
description: string;
|
|
141
|
+
inputSchema: Schema;
|
|
142
|
+
run(args: any): Promise<Result> | Result;
|
|
143
|
+
}[] = [];
|
|
144
|
+
for (const r of d.rules) {
|
|
145
|
+
// the document a call names: a kind's instance by its key (trip:1), or the one
|
|
146
|
+
const name = (args: { key?: unknown }) => (r.kind ? `${r.doc}:${args.key}` : r.doc);
|
|
147
|
+
if (!r.kind && d.holds(r.doc, who.member)) continue;
|
|
148
|
+
const key = r.kind
|
|
149
|
+
? { key: { type: "integer", description: `which ${r.doc}: the id of one of ${r.kind.doc}'s ${r.kind.root}` } }
|
|
150
|
+
: {};
|
|
151
|
+
tools.push({
|
|
152
|
+
name: `read_${r.doc}`,
|
|
153
|
+
description: `Read ${r.doc}, as you see it, with its version. It holds ${described(r)}.`,
|
|
154
|
+
inputSchema: { type: "object", properties: key, required: Object.keys(key) },
|
|
155
|
+
run: (args) => {
|
|
156
|
+
const no = d.holds(name(args), who.member);
|
|
157
|
+
return no ? refused(no) : answered(d.read(name(args), who.member));
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
const ops = opsOf(r);
|
|
161
|
+
// an op's path as it's said: an add by a first step, what it gives
|
|
162
|
+
const said = (o: (typeof ops)[number]) => `${o.path}${o.gives ? ` giving ${o.gives}` : ""}`;
|
|
163
|
+
if (r.type !== "shared" || !ops.length) continue;
|
|
164
|
+
tools.push({
|
|
165
|
+
name: `write_${r.doc}`,
|
|
166
|
+
description: `One write to ${r.doc}, whole or not at all, told to every page at once: its ops, each one of ${ops.map((o) => `${o.op} ${said(o)}`).join(", ")}. {id} is an item's id, as read_${r.doc} shows it. Done answers with its version and the ids of what it added; refused, with why.`,
|
|
167
|
+
inputSchema: {
|
|
168
|
+
type: "object",
|
|
169
|
+
properties: {
|
|
170
|
+
...key,
|
|
171
|
+
ops: {
|
|
172
|
+
type: "array",
|
|
173
|
+
minItems: 1,
|
|
174
|
+
items: {
|
|
175
|
+
oneOf: ops.map((o) => ({
|
|
176
|
+
type: "object",
|
|
177
|
+
properties: {
|
|
178
|
+
op: { const: o.op },
|
|
179
|
+
path: { type: "string", pattern: o.pattern, description: said(o) },
|
|
180
|
+
...(o.value ? { value: o.value } : {}),
|
|
181
|
+
},
|
|
182
|
+
required: ["op", "path", ...(o.value ? ["value"] : [])],
|
|
183
|
+
})),
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
why: { type: "string", description: "why, in a few words, where it helps those who read it" },
|
|
187
|
+
},
|
|
188
|
+
required: [...Object.keys(key), "ops"],
|
|
189
|
+
},
|
|
190
|
+
run: async (args) => {
|
|
191
|
+
if (!Array.isArray(args.ops) || !args.ops.length)
|
|
192
|
+
return refused("ops: one write's ops, a list of at least one");
|
|
193
|
+
const no = d.holds(name(args), who.member);
|
|
194
|
+
if (no) return refused(no);
|
|
195
|
+
const w = await d.write(
|
|
196
|
+
name(args),
|
|
197
|
+
args.ops,
|
|
198
|
+
typeof args.why === "string" ? args.why.trim() || undefined : undefined,
|
|
199
|
+
d.anyone ? null : who,
|
|
200
|
+
);
|
|
201
|
+
return "why" in w ? refused(w.why) : answered(w);
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
return tools;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** One message: answered, or a notification (no id), which isn't. */
|
|
209
|
+
async function one(m: { id?: unknown; method?: string; params?: any }, who: Who) {
|
|
210
|
+
if (m.id === undefined) return null;
|
|
211
|
+
if (m.method === "initialize")
|
|
212
|
+
return rpc(m.id, {
|
|
213
|
+
protocolVersion: VERSIONS.includes(m.params?.protocolVersion) ? m.params.protocolVersion : VERSIONS[0],
|
|
214
|
+
capabilities: { tools: {} },
|
|
215
|
+
serverInfo: { name: "joined", version: d.version },
|
|
216
|
+
...(d.instructions ? { instructions: d.instructions } : {}),
|
|
217
|
+
});
|
|
218
|
+
if (m.method === "ping") return rpc(m.id, {});
|
|
219
|
+
if (m.method === "tools/list") return rpc(m.id, { tools: toolsFor(who).map(({ run: _, ...t }) => t) });
|
|
220
|
+
if (m.method === "tools/call") {
|
|
221
|
+
const tool = toolsFor(who).find((t) => t.name === m.params?.name);
|
|
222
|
+
return tool
|
|
223
|
+
? rpc(m.id, await tool.run(m.params.arguments ?? {}))
|
|
224
|
+
: fail(m.id, -32602, `no tool called ${m.params?.name}`);
|
|
225
|
+
}
|
|
226
|
+
return fail(m.id, -32601, `no method called ${m.method}`);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return {
|
|
230
|
+
GET: () => new Response("this door is POST alone", { status: 405, headers: { allow: "POST" } }),
|
|
231
|
+
async POST(req: Request & { params: { token?: string } }) {
|
|
232
|
+
// an agent's link alone (never a member's own, a removed agent's or one unknown); where nobody's a member, anyone
|
|
233
|
+
const who = d.anyone ? ANYONE : await d.whoIs(req.params.token!);
|
|
234
|
+
if (!d.anyone && !who?.agent)
|
|
235
|
+
return Response.json(
|
|
236
|
+
fail(null, -32001, "an agent's link opens this door: make one in the group's Agents tab"),
|
|
237
|
+
{
|
|
238
|
+
status: 401,
|
|
239
|
+
},
|
|
240
|
+
);
|
|
241
|
+
const m = await req.json().catch(() => undefined);
|
|
242
|
+
if (!m || typeof m !== "object")
|
|
243
|
+
return Response.json(fail(null, -32700, "that isn't a JSON-RPC message"), { status: 400 });
|
|
244
|
+
// never thrown: Bun's line for a request that throws names its URL, and the URL holds the agent's token
|
|
245
|
+
const answer = await one(m, who!).catch((e) => {
|
|
246
|
+
console.error("the MCP door failed:", e);
|
|
247
|
+
return fail(m.id ?? null, -32603, "That wasn't done: the server had a problem with it.");
|
|
248
|
+
});
|
|
249
|
+
return answer ? Response.json(answer) : new Response(null, { status: 202 });
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// A demo on this machine, never live (start refuses one in production): anyone here may be any member, without their
|
|
2
|
+
// link, from the root page's line, so a demo of Nora's or Nick's pages always has a way back to the BDFL. It's asked
|
|
3
|
+
// of this machine alone: from its loopback, by its own name (a name that only resolves to it, a page elsewhere, is
|
|
4
|
+
// refused), and never by a page on another site (a picture's address there would sign this browser in).
|
|
5
|
+
|
|
6
|
+
import type { Member } from "./trust.ts";
|
|
7
|
+
|
|
8
|
+
const LOOPBACK = ["127.0.0.1", "::1", "::ffff:127.0.0.1"];
|
|
9
|
+
const HOSTS = ["localhost", "127.0.0.1", "[::1]"];
|
|
10
|
+
|
|
11
|
+
/** Whether a request is this machine's own: from its loopback (its address, `ip`), by its own name, from no other
|
|
12
|
+
* site. */
|
|
13
|
+
export const fromHere = (req: Request, ip?: string) =>
|
|
14
|
+
LOOPBACK.includes(ip ?? "") &&
|
|
15
|
+
HOSTS.includes(new URL(req.url).hostname) &&
|
|
16
|
+
req.headers.get("sec-fetch-site") !== "cross-site";
|
|
17
|
+
|
|
18
|
+
/** The demo's routes: who there is to be, the BDFL first, and who this browser is (`me`, none signed out); and
|
|
19
|
+
* being one, this browser signed in as them by the server (`signIn`), their link never shown. Refused to anyone not
|
|
20
|
+
* `here`. */
|
|
21
|
+
export function demoRoutes({
|
|
22
|
+
here,
|
|
23
|
+
members,
|
|
24
|
+
bdfl,
|
|
25
|
+
me,
|
|
26
|
+
signIn,
|
|
27
|
+
}: {
|
|
28
|
+
here: (req: Request) => boolean;
|
|
29
|
+
members: () => Member[];
|
|
30
|
+
bdfl: () => number;
|
|
31
|
+
me: (req: Request) => Promise<number | null>;
|
|
32
|
+
signIn: (req: Request, member: number) => Promise<Response>;
|
|
33
|
+
}) {
|
|
34
|
+
const refused = () => new Response("a demo answers this machine alone", { status: 403 });
|
|
35
|
+
return {
|
|
36
|
+
"/as": async (req: Request) =>
|
|
37
|
+
here(req)
|
|
38
|
+
? Response.json({
|
|
39
|
+
me: await me(req),
|
|
40
|
+
people: [...members()]
|
|
41
|
+
.sort((a, b) => Number(b.id === bdfl()) - Number(a.id === bdfl()))
|
|
42
|
+
.map((m) => ({ id: m.id, name: m.name, bdfl: m.id === bdfl() })),
|
|
43
|
+
})
|
|
44
|
+
: refused(),
|
|
45
|
+
"/as/:member": async (req: Bun.BunRequest<"/as/:member">) => {
|
|
46
|
+
const member = Number(req.params.member);
|
|
47
|
+
if (!here(req)) return refused();
|
|
48
|
+
return members().some((m) => m.id === member)
|
|
49
|
+
? signIn(req, member)
|
|
50
|
+
: new Response("no such member", { status: 404 });
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Written by src/compile.ts from src/members/stories.jsonl: change the stories, not this.
|
|
2
|
+
|
|
3
|
+
// Each collection is an element, defined here: on a page, it holds the document, and its items
|
|
4
|
+
// are put inside it. Each item is an element, defined by the page: as it is, or by a view that
|
|
5
|
+
// extends it. A field is an attribute (a list of ids as IDREFS: "1 3"), and an item's key the
|
|
6
|
+
// document's id for it. How an item looks is the app's: its view, or a display holding the
|
|
7
|
+
// collection draws it. Ops go up as an "ops" event, for the page to send to members, with why they
|
|
8
|
+
// were made if it's said; only the ops the stories allow can be sent. Each returns its write's answer.
|
|
9
|
+
|
|
10
|
+
import { hold, letGo, type Answer } from "joined/page";
|
|
11
|
+
|
|
12
|
+
// the document an element is of: its collection's doc attribute (an instance of a kind, shelf:1), or members
|
|
13
|
+
const docOf = (el: Element) => el.closest("[doc]")?.getAttribute("doc") ?? "members";
|
|
14
|
+
// an element's ops go up as an "ops" event; what it returns is the write's answer, done or refused
|
|
15
|
+
const send = (from: Element, ops: object[], why?: string) => new Promise<Answer>((answer) => from.dispatchEvent(new CustomEvent("ops", { bubbles: true, composed: true, detail: { doc: docOf(from), ops, why, answer } })));
|
|
16
|
+
|
|
17
|
+
/** A member's fields, as the stories give them. */
|
|
18
|
+
export type JMemberData = { "name": string; "inviter": number; "act"?: string; "readonly"?: number };
|
|
19
|
+
/** An add of a member, as it's sent: its fields as the stories write them. */
|
|
20
|
+
export type JMemberValue = { "name": string; "inviter": number; "act"?: string; "readonly"?: number };
|
|
21
|
+
|
|
22
|
+
/** <j-member key>: name (string), inviter (integer), act (string), readonly (integer), each an attribute. */
|
|
23
|
+
export class JMember extends HTMLElement {
|
|
24
|
+
static observedAttributes = ["name","inviter","act","readonly","may"];
|
|
25
|
+
#data?: JMemberData;
|
|
26
|
+
/** Its id in the document. */
|
|
27
|
+
get key() { return Number(this.getAttribute("key")); }
|
|
28
|
+
get path() { return `/members/${this.key}`; }
|
|
29
|
+
/** Its fields, read from its attributes once and again when one changes: frozen, since they're shared. */
|
|
30
|
+
get data(): JMemberData {
|
|
31
|
+
return (this.#data ??= Object.freeze({ "name": this.getAttribute("name") ?? "", "inviter": Number(this.getAttribute("inviter")), "act": this.getAttribute("act") ?? undefined, "readonly": this.hasAttribute("readonly") ? Number(this.getAttribute("readonly")) : undefined }) as JMemberData);
|
|
32
|
+
}
|
|
33
|
+
/** How a view shows it, drawn again whenever an attribute changes (what its person may do, its may, too). */
|
|
34
|
+
draw?(): void;
|
|
35
|
+
attributeChangedCallback() { this.#data = undefined; this.draw?.(); }
|
|
36
|
+
/** Replace some of its fields (name, readonly): one write for all of them. */
|
|
37
|
+
replace(fields: Partial<Pick<JMemberData, "name" | "readonly">>, why?: string) {
|
|
38
|
+
return send(this, Object.entries(fields).filter(([, value]) => value !== undefined).map(([field, value]) => ({ op: "replace", path: `${this.path}/${field.replace(".", "/")}`, value })), why);
|
|
39
|
+
}
|
|
40
|
+
/** Remove some of its fields (act, readonly), so that it has none. */
|
|
41
|
+
removeField(...fields: ("act" | "readonly")[]) { return send(this, fields.map((field) => ({ op: "remove", path: `${this.path}/${field}` }))); }
|
|
42
|
+
/** Remove it from the document. */
|
|
43
|
+
removeItem(why?: string) { return send(this, [{ op: "remove", path: this.path }], why); }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** <j-members>: the document's members, each a <j-member> inside it. */
|
|
47
|
+
export class JMembers extends HTMLElement {
|
|
48
|
+
connectedCallback() { hold(this, docOf(this), "members", "j-member", {"name":"string","inviter":"integer","act":"string","readonly":"integer"}); }
|
|
49
|
+
disconnectedCallback() { letGo(this); }
|
|
50
|
+
/** Its members, as the document has them. */
|
|
51
|
+
get items() { return [...this.children].filter((el): el is JMember => el.localName === "j-member"); }
|
|
52
|
+
/** Add members: one write for all of them. */
|
|
53
|
+
add(...values: JMemberValue[]) { return send(this, values.map((value) => ({ op: "add", path: "/members/-", value }))); }
|
|
54
|
+
}
|
|
55
|
+
customElements.define("j-members", JMembers);
|
|
56
|
+
|
|
57
|
+
/** An op on members the stories allow, in any of its collections. */
|
|
58
|
+
export type MembersOp =
|
|
59
|
+
| { op: "add"; path: "/members/-"; value: JMemberValue }
|
|
60
|
+
| { op: "replace"; path: `/members/${number}/name`; value: string }
|
|
61
|
+
| { op: "replace"; path: `/members/${number}/readonly`; value: number }
|
|
62
|
+
| { op: "remove"; path: `/members/${number}/act` }
|
|
63
|
+
| { op: "remove"; path: `/members/${number}/readonly` }
|
|
64
|
+
| { op: "remove"; path: `/members/${number}` };
|
|
65
|
+
|
|
66
|
+
/** One write to members across its collections (a move with its legs): sent from an element of it, checked
|
|
67
|
+
* whole by its rules, and made whole or not at all. */
|
|
68
|
+
export function write(from: Element, ops: MembersOp[], why?: string) { return send(from, ops, why); }
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Written by src/compile.ts from src/members/stories.jsonl: change the stories, not this.
|
|
2
|
+
|
|
3
|
+
/** Each collection's item, as one row of its table (`t`) is it: what the query gathers, and what the store keeps of a
|
|
4
|
+
* row a write changes, before and after, to tell each document holding it. */
|
|
5
|
+
export const items: Record<string, string> = {
|
|
6
|
+
"members": "json_patch('{}', json_object('id', t.id,\n 'name', t.\"name\",\n 'inviter', t.\"inviter\",\n 'act', t.\"act\",\n 'readonly', t.\"readonly\"))"
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/** members, whole, as one statement over its tables (SQLite): its parameters $doc. */
|
|
10
|
+
export const query = `SELECT json_object('version', COALESCE((SELECT version FROM documents WHERE name = $doc), 0),
|
|
11
|
+
'members', json((SELECT json_group_array(json_patch('{}', json_object('id', t.id,
|
|
12
|
+
'name', t."name",
|
|
13
|
+
'inviter', t."inviter",
|
|
14
|
+
'act', t."act",
|
|
15
|
+
'readonly', t."readonly")) ORDER BY t.id)
|
|
16
|
+
FROM "members" t))) AS doc`;
|
|
17
|
+
|
|
18
|
+
/** Its rules over values (definitions' takes, gives and totals), as triggers the store makes in its database: run in
|
|
19
|
+
* each write's transaction, and told as any change is. */
|
|
20
|
+
export const triggers: string[] = [];
|
|
21
|
+
|
|
22
|
+
/** Its items, its query and its triggers, as Postgres says them. */
|
|
23
|
+
export const postgres = {
|
|
24
|
+
"items": {
|
|
25
|
+
"members": "json_strip_nulls(json_build_object('id', t.id,\n 'name', t.\"name\",\n 'inviter', t.\"inviter\",\n 'act', t.\"act\",\n 'readonly', t.\"readonly\"))"
|
|
26
|
+
},
|
|
27
|
+
"query": "SELECT json_build_object('version', COALESCE((SELECT version FROM documents WHERE name = $doc), 0),\n 'members', (SELECT COALESCE(json_agg(json_strip_nulls(json_build_object('id', t.id,\n 'name', t.\"name\",\n 'inviter', t.\"inviter\",\n 'act', t.\"act\",\n 'readonly', t.\"readonly\")) ORDER BY t.id), '[]')\n FROM \"members\" t)) AS doc",
|
|
28
|
+
"triggers": []
|
|
29
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Written by src/compile.ts from src/members/stories.jsonl: change the stories, not this.
|
|
2
|
+
|
|
3
|
+
export type Op = { op: "add" | "replace" | "remove"; path: string; value?: any };
|
|
4
|
+
|
|
5
|
+
/** The document, and each collection: its element, the element its items are, their fields, those an add needs, and
|
|
6
|
+
* what each link is to. */
|
|
7
|
+
export const doc = "members";
|
|
8
|
+
export const collections = {
|
|
9
|
+
"members": {
|
|
10
|
+
"place": "j-members",
|
|
11
|
+
"tag": "j-member",
|
|
12
|
+
"fields": {
|
|
13
|
+
"name": "string",
|
|
14
|
+
"inviter": "integer",
|
|
15
|
+
"act": "string",
|
|
16
|
+
"readonly": "integer"
|
|
17
|
+
},
|
|
18
|
+
"needs": [
|
|
19
|
+
"name",
|
|
20
|
+
"inviter"
|
|
21
|
+
],
|
|
22
|
+
"links": {}
|
|
23
|
+
}
|
|
24
|
+
} as const;
|
|
25
|
+
/** Whether every story is anonymous, none told as a member: where every app's is, the project has no members. */
|
|
26
|
+
export const anonymous = false;
|
|
27
|
+
/** What kind of document members is, by the flows it uses: static, changed only with a release (a page reads it, and
|
|
28
|
+
* never subscribes); read-only, live, but no page writes it (derived); or shared, its members' to write. */
|
|
29
|
+
export const type = "shared";
|
|
30
|
+
/** The parts of members a page may hold alone, by its query's parameters (its definitions' window): null, none. */
|
|
31
|
+
export const window = null;
|
|
32
|
+
/** The document as one statement over its tables (generated/queries.ts). */
|
|
33
|
+
export { query, postgres, items, triggers } from "./queries.ts";
|
|
34
|
+
|
|
35
|
+
/** members is one document. */
|
|
36
|
+
export const kind = null;
|
|
37
|
+
|
|
38
|
+
/** Each grant, read off the stories: the roles whose stories do it (a member: anyone). An op no story
|
|
39
|
+
* does isn't one. */
|
|
40
|
+
export const grants: Record<string, string[]> = {
|
|
41
|
+
"add members": [
|
|
42
|
+
"member"
|
|
43
|
+
],
|
|
44
|
+
"remove members": [
|
|
45
|
+
"member"
|
|
46
|
+
],
|
|
47
|
+
"remove members.act": [
|
|
48
|
+
"member"
|
|
49
|
+
],
|
|
50
|
+
"remove members.readonly": [
|
|
51
|
+
"member"
|
|
52
|
+
],
|
|
53
|
+
"replace members.name": [
|
|
54
|
+
"member"
|
|
55
|
+
],
|
|
56
|
+
"replace members.readonly": [
|
|
57
|
+
"member"
|
|
58
|
+
]
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** Who someone is to members: a member, as it names no roles. */
|
|
62
|
+
export const rolesOf = (_m: number, _read: (doc: string) => any, _doc: string, _at?: any): string[] => [];
|
|
63
|
+
|
|
64
|
+
/** What someone sees: all of members, as it has no owns. */
|
|
65
|
+
export const sees = null;
|