Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/worker/project/draft-state.ts?version=277&format=json
87 linesFirst import 100%Last change First import in version 0, 1 version changed it

You're reading version 277. The newest is 318, and no version since then changed this file. Go to the newestCompare with 318

1// A named draft's state in one read. Reads go through DraftStateHost; today
2// the host is Project.
3
4import type { AtomId, Graph } from "../../src/engine/index.ts";
5import { ProjectError } from "./project-error.ts";
6import type { AgentState, Collision } from "./project-types.ts";
7import { last, SHARED, type Space, sharedBaseOf } from "./spaces.ts";
8
9/** What a named draft's state read needs from a project. */
10export interface DraftStateHost {
11 needInit(): void;
12 spaceNamed(name: string): Space;
13 shared(): Space;
14 readonly graph: Graph;
15 readonly agents: ReadonlyMap<string, AgentState>;
16 draft(agent: AgentState): AtomId[];
17 checkClash(changes: readonly AtomId[]): void;
18 landCollisions(space: Space, changes: AtomId[], pick: boolean): { collisions: Collision[] };
21/**
22 * A named draft's state in one read (#41, the Drafts page, D3 and D16): the shared version it builds on and
23 * how far behind shared that is, its versions, when its first edit was, who works in it, and what landing it
24 * now would collide with (the same check land makes, so nothing is written).
25 */
26export function draftState(
27 host: DraftStateHost,
28 name: string,
29): {
30 draft: string;
31 base: number;
32 shared: number;
33 behind: number;
34 versions: { version: number; agent: string; message: string; time: number; base: number }[];
35 firstEdit: number | null;
36 agents: { agent: string; unpublished: number }[];
37 collisions: Collision[];
38 clash?: string;
39} {
40 host.needInit();
41 if (typeof name !== "string" || name === "") throw new ProjectError("bad-request", "draft needs ?name=<draft>");
42 const space = host.spaceNamed(name);
43 if (space.name === SHARED) throw new ProjectError("bad-request", "shared isn't a named draft");
44 const shared = host.shared();
45 const base = sharedBaseOf(space);
46 const changes = [...space.own]
47 .filter((id) => !shared.own.has(id) && !shared.droppedIn.has(id))
48 .toSorted((p, q) => host.graph.atom(p).seq - host.graph.atom(q).seq);
49 const agents = [...host.agents.values()]
50 .filter((a) => a.space === space.name)
51 .map((a) => ({ agent: a.name, unpublished: host.draft(a).length }))
52 .toSorted((a, b) => (a.agent < b.agent ? -1 : 1));
53 // The first edit: its earliest change, published or not.
54 let firstEdit: number | null = null;
55 const unpublished = [...host.agents.values()].filter((a) => a.space === space.name).flatMap((a) => host.draft(a));
56 for (const id of [...changes, ...unpublished]) {
57 const t = host.graph.atom(id).time;
58 if (firstEdit === null || t < firstEdit) firstEdit = t;
59 }
60 let clash: string | undefined;
61 try {
62 host.checkClash(changes);
63 } catch (e) {
64 if (!(e instanceof ProjectError)) throw e;
65 clash = e.message;
66 }
67 const collisions = changes.length > 0 ? host.landCollisions(space, changes, false).collisions : [];
68 return {
69 draft: space.name,
70 base,
71 shared: last(shared),
72 behind: last(shared) - base,
73 versions: space.versions
74 .filter((p) => p.version > 0)
75 .map((p) => ({
76 version: p.version,
77 agent: p.agent,
78 message: p.message,
79 time: p.time,
80 base: p.sharedBase ?? 0,
81 })),
82 firstEdit,
83 agents,
84 collisions,
85 ...(clash !== undefined ? { clash } : {}),
86 };