pi-mono/apps/p2/plugins/history/worker.ts
Mario Zechner 20d86c3657 feat(p2): Chord v2 cleanup pass
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.
2026-11-05 18:22:13 +00:00

21 lines
925 B
TypeScript

// The history tools (tools.ts), contributed to every conversation of the session's worker; searches go to the server's
// index through the search plugin's worker feed, which scopes them to this worker's session.
import { contribute, defineFacet, expectVariant } from "@p2/app/v1";
import { Extensions, workerOf } from "@p2/app/v1/worker";
import { SearchFeed } from "../search/contract.ts";
import { createHistoryExtension } from "./tools.ts";
export default defineFacet((host) => {
contribute(host, "p2.history-tools", (self) => {
const worker = workerOf(self);
const extension = createHistoryExtension(
(query, context) =>
worker().root(
async (ctx) => expectVariant(await ctx.get(SearchFeed, {}), "worker").history(query, ctx),
context,
),
(conversation) => `${worker().init.values.session.id}#${conversation}`,
);
self.contribute(Extensions, { [extension.name]: extension });
});
});