Skip to content
30 linesCodeBlameRaw
1/**
2 * Making sure a workspace has its built-in @g1t
3 * (docs.g1t.sh/guides/agents/, "g1t, the orchestrator"). Kept apart from
4 * the service so it is tested with a stand-in database.
5 */
6import { newId } from "../../../packages/contracts/src/ids.ts";
7import { builtinDefinition } from "./orchestrator.ts";
8import { insertAgent, versionStatement } from "./store.ts";
9
10/** Workspaces whose @g1t this isolate has seen made, so it is not written again. */
11const ensured = new Set<string>();
12
13/**
14 * Makes the workspace's built-in @g1t if it does not exist yet: an
15 * ordinary agent row, marked builtin, at version 1, with its first
16 * version recorded. Safe to call on every request: the insert is ignored
17 * when the workspace has one (one builtin per workspace, by index), and
18 * once seen, an isolate does not write again.
19 */
20export async function ensureBuiltin(db: D1Database, workspaceId: string, memo: Set<string> = ensured): Promise<void> {
21 if (!workspaceId || memo.has(workspaceId)) return;
22 const definition = builtinDefinition();
23 const id = newId("agt");
24 const now = new Date().toISOString();
25 // Ignored when the workspace has one already (one builtin per workspace, by index).
26 const made = await insertAgent(db, id, workspaceId, definition, { version: 1, builtin: 1, created_by: "g1t", created_at: now, updated_at: now }, true).run();
27 if (made.meta.changes) await versionStatement(db, id, 1, definition, "g1t", now).run();
28 if (memo.size > 10_000) memo.clear();
29 memo.add(workspaceId);
30}