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.
64 lines
2.7 KiB
TypeScript
64 lines
2.7 KiB
TypeScript
import type { ConversationId } from "@earendil-works/pi-durable";
|
|
import { canView, contribute, defineFacet, deriveTyped, forbidden } from "@p2/app/v1";
|
|
import { agentConversation, Extensions, harnessOf, workerOf } from "@p2/app/v1/worker";
|
|
import { ArtifactContent, ArtifactList, MAX_ARTIFACT_PREVIEW } from "./contract.ts";
|
|
import { Artifacts } from "./store.ts";
|
|
import { ArtifactDoc, createArtifactExtension } from "./tool.ts";
|
|
|
|
export default defineFacet((host) => {
|
|
contribute(host, "p2.artifact-tools", (self) => {
|
|
const worker = workerOf(self);
|
|
// The tool's declaration; each call saves for the worker's session.
|
|
const template = createArtifactExtension("", "");
|
|
self.contribute(Extensions, {
|
|
[template.name]: {
|
|
...template,
|
|
tools: template.tools!.map((tool) => ({
|
|
...tool,
|
|
execute: (...args: Parameters<typeof tool.execute>) =>
|
|
createArtifactExtension(worker().init.dataDir, worker().init.values.session.id).tools![0]!.execute(
|
|
...args,
|
|
),
|
|
})),
|
|
},
|
|
});
|
|
});
|
|
|
|
host.serve(ArtifactContent, ({ session }, principal, self) => {
|
|
if (!canView(principal, session)) throw forbidden("viewers only");
|
|
const worker = workerOf(self);
|
|
return {
|
|
variant: "reader",
|
|
members: {
|
|
read: async ({ name, version }) => {
|
|
const { info, content } = await new Artifacts(worker().init.dataDir).read(session, name, version);
|
|
const truncated = content.length > MAX_ARTIFACT_PREVIEW;
|
|
// Never split a surrogate pair at the cut.
|
|
let end = MAX_ARTIFACT_PREVIEW;
|
|
if (truncated && /[\uD800-\uDBFF]/.test(content[end - 1] ?? "")) end--;
|
|
return { info, content: truncated ? content.slice(0, end) : content, truncated };
|
|
},
|
|
},
|
|
};
|
|
});
|
|
|
|
host.serve(ArtifactList, ({ session, conversation }, principal, self) => {
|
|
if (!canView(principal, session)) throw forbidden("viewers only");
|
|
const harness = harnessOf(self);
|
|
const id = conversation as ConversationId;
|
|
// The viewed agent's list (the main agent's on conversation 1), followed.
|
|
const listed = self.follow(async (observer, ctx) => {
|
|
const durable = harness().durable;
|
|
if (!(await durable.conversation(id, ctx))) throw new Error(`Unknown conversation ${conversation}`);
|
|
const holder = await agentConversation(durable, id, ctx);
|
|
const observed = await durable.observeDoc(ArtifactDoc, holder, observer, ctx);
|
|
if (observed !== undefined) return observed;
|
|
// No artifact saved yet: the document is absent, and an absent address cannot be followed. Create it empty.
|
|
await durable.commit(async (tx) => {
|
|
await tx.doc(ArtifactDoc, holder);
|
|
}, ctx);
|
|
return durable.observeDoc(ArtifactDoc, holder, observer, ctx);
|
|
});
|
|
return { variant: "reader", members: { artifacts: deriveTyped(self, [listed], ([value]) => value.artifacts) } };
|
|
});
|
|
});
|