mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-11-05 23:37:25 +00:00
p2 rewritten by the Chord v2 authoring rules (decision "App authoring rules confirmed by the p2 audit"), in four parts merged together: - core: session lifecycle (relocate, clone, fork, archive) and search rebuild are persisted jobs on a new server store; Sessions v6 with client-chosen ids and visible operations; Retry answers with the worker's real outcome; workers push branch, subagent and status state to the server; roots take the method's ctx. - services: models, envs, ports, login, mcp, logs, sharing and voice follow external state (self.follow, followNative) instead of copy loops; methods answer after their state commits; presence from host.references. - chat: chat mode, artifacts and prompt resources follow Durable and native state; shell commands are Chord jobs (shellId is the requestId); status pushed before send and stop answer; race-free draft claim. - ui: optimistic updates on web (useOptimistic) and terminal (TuiScope.optimistic, extended in Chord with a reducer and a pending flag), pending states for long work, visible rollback, load waterfalls removed; merged with the one composer and immediate first send (ccff9876e) and the folder + button (5a31a6d5f). docs/optimistic-ui.md describes the pattern.
61 lines
2.3 KiB
TypeScript
61 lines
2.3 KiB
TypeScript
import { num, obj } from "@earendil-works/pi-schema";
|
|
import { authorIdOf, canSend, defineFacet, defineState, forbidden, isGuest, isOwner } from "@p2/app/v1";
|
|
import { DraftStore, Drafts, DraftsByKeySchema, draftKey } from "./contract.ts";
|
|
|
|
/** Restored drafts by `<conversation>:<author id>`, and the next draft's ID (in memory, for the worker's lifetime). */
|
|
const Store = defineState("p2.drafts", {
|
|
schema: obj({ drafts: DraftsByKeySchema, next: num() }),
|
|
version: 1,
|
|
initial: () => ({ drafts: {}, next: 1 }),
|
|
});
|
|
|
|
export default defineFacet((host) => {
|
|
host.serve(DraftStore, (_params, self) => {
|
|
const store = self.state(Store);
|
|
return {
|
|
drafts: self.derive([store], ([value]) => value.drafts),
|
|
add: async ({ conversation, items }, ctx) => {
|
|
if (!items.length) return null;
|
|
await store.change((draft) => {
|
|
const first = draft.next;
|
|
draft.next += items.length;
|
|
items.forEach(({ author, text }, index) => {
|
|
const key = draftKey(conversation, author.id);
|
|
draft.drafts[key] = [...(draft.drafts[key] ?? []), { id: first + index, text }];
|
|
});
|
|
}, ctx);
|
|
return null;
|
|
},
|
|
claim: async ({ conversation, authorId, ids }, ctx) => {
|
|
const key = draftKey(conversation, authorId);
|
|
const wanted = new Set(ids);
|
|
// Read and removed in one change: two tabs claiming at once get each draft once.
|
|
const texts = await store.change((draft) => {
|
|
const items = draft.drafts[key] ?? [];
|
|
const claimed = items.filter((item) => wanted.has(item.id)).map((item) => item.text);
|
|
if (!claimed.length) return [];
|
|
const rest = items.filter((item) => !wanted.has(item.id));
|
|
if (rest.length) draft.drafts[key] = rest;
|
|
else delete draft.drafts[key];
|
|
return claimed;
|
|
}, ctx);
|
|
return { texts };
|
|
},
|
|
};
|
|
});
|
|
|
|
host.serve(Drafts, ({ session, conversation }, principal, self) => {
|
|
if (!canSend(principal, session)) throw forbidden("participants only");
|
|
if (!isOwner(principal) && !isGuest(principal)) throw forbidden("people only");
|
|
const authorId = authorIdOf(principal);
|
|
const store = self.get(DraftStore, {});
|
|
const key = draftKey(conversation, authorId);
|
|
return {
|
|
variant: "author",
|
|
members: {
|
|
drafts: self.derive([store.drafts], ([drafts]) => drafts[key] ?? []),
|
|
claim: ({ ids }, ctx) => store.claim({ conversation, authorId, ids }, ctx),
|
|
},
|
|
};
|
|
});
|
|
});
|