pi-mono/apps/p2/plugins/artifacts/worker.ts
David Brailovsky e885255061 feat(p2): /tree, branches of the main conversation as Durable forks
Each branch is a top-level conversation forked from another at an entry;
the session-scoped p2.main document lists them and names the current one.
Earlier branches are read-only. The main agent's per-agent state
(subagents, artifacts, voice and chat mode, pending move) stays on
conversation 1 for every branch.

- tree plugin: SessionTree (tree, navigate, select); the worker reports
  main and branches to the catalog (Sessions v4, catalog schema 4)
- TUI: /tree and double Escape (pi's default "tree"), a tree selector
  adapted from coding-agent
- web: Edit on user messages, Branch from here on answers, branch rows
  under the session in the sidebar
- main-agent checks use the branch list instead of conversation 1
- search indexes each branch's own entries

Design: apps/p2/docs/tree.md
2026-11-04 13:40:36 +01:00

103 lines
3.6 KiB
TypeScript

import type { ConversationId } from "@earendil-works/pi-durable";
import { arr, obj } from "@earendil-works/pi-schema";
import {
type Context,
canView,
contribute,
defineFacet,
defineTypedState,
deriveTyped,
forbidden,
signalOf,
} from "@p2/app/v1";
import { agentConversation, CommitSignal, Extensions, harnessOf, workerOf } from "@p2/app/v1/worker";
import { ArtifactContent, ArtifactInfoSchema, ArtifactList, MAX_ARTIFACT_PREVIEW } from "./contract.ts";
import { Artifacts } from "./store.ts";
import { ArtifactDoc, createArtifactExtension } from "./tool.ts";
/** The artifacts of one conversation (in memory, from its document). */
const Listed = defineTypedState("p2.artifact-list", {
schema: obj({ artifacts: arr(ArtifactInfoSchema) }),
version: 1,
initial: () => ({ artifacts: [] }),
});
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;
const listed = self.state(Listed);
/** Where the viewed agent's list is (the main agent's on conversation 1); set when the object opens. */
let holder = id;
const show = async (ctx: Context) => {
const doc = await harness().durable.snapshot(ArtifactDoc, holder, ctx);
await listed.change((draft) => {
// A draft's arrays are mutable; the document's are the same JSON.
draft.artifacts = [...(doc?.artifacts ?? [])] as typeof draft.artifacts;
}, ctx);
};
const commits = self.open(
async (ctx) => {
const durable = harness().durable;
if (!(await durable.conversation(id, ctx))) throw new Error(`Unknown conversation ${conversation}`);
holder = await agentConversation(durable, id, ctx);
await show(ctx);
return new CommitSignal(durable, (publication) =>
publication.changes.some(
(change) =>
change.type === "document" &&
change.record.kind === ArtifactDoc.definition.kind &&
change.conversationId === holder,
),
);
},
(signal) => signal.close(),
);
self.onLive(async (ctx) => {
const signal = signalOf(ctx);
while (!signal.aborted) {
const seen = commits.value.version;
await show(ctx);
await commits.value.next(seen, signal).catch(() => {});
}
});
return { variant: "reader", members: { artifacts: deriveTyped(self, [listed], ([value]) => value.artifacts) } };
});
});