Skip to content
214 linesCodeBlameRaw
1/**
2 * Agents as stored in D1, and their spend. Shared by the RPC methods and
3 * the desk.
4 */
5import type { SubagentDef, WorkspaceAgent } from "@g1t/contracts";
6
7import { agentStatus, budgetBlock, dayKey, monthKey } from "./budget.ts";
8import { type Definition, DEFAULT_AUTONOMY, DEFAULT_BUDGET, DEFAULT_ROUTING, PRESETS, readJson } from "./definition.ts";
9
10export type Row = {
11 id: string;
12 workspace_id: string;
13 handle: string;
14 display_name: string;
15 avatar: string | null;
16 role: string;
17 instructions: string;
18 personality_preset: string;
19 personality: string;
20 routing: string;
21 budget: string;
22 autonomy: string;
23 capacity: number;
24 template: string | null;
25 avatar_seed: string | null;
26 title: string | null;
27 team: string | null;
28 department: string | null;
29 responsibilities: string | null;
30 subagents: string | null;
31 faces: string | null;
32 reading?: string | null;
33 skills_off?: string | null;
34 version: number;
35 /** 1 for the workspace's built-in @g1t. */
36 builtin: number;
37 busy_until: string | null;
38 created_by: string;
39 created_at: string;
40 updated_at: string;
41 archived_at: string | null;
42 /** Joined from agent_spend: this month and today. */
43 spent_month?: number | null;
44 spent_day?: number | null;
45};
46
47/**
48 * Agents with this month's and today's spend: `where` filters `agents`
49 * (aliased `a`); its parameters follow the two periods.
50 */
51export function selectAgents(where: string): string {
52 return `SELECT a.*, m.micros AS spent_month, d.micros AS spent_day FROM agents a
53 LEFT JOIN agent_spend m ON m.agent_id = a.id AND m.period = ?1
54 LEFT JOIN agent_spend d ON d.agent_id = a.id AND d.period = ?2
55 WHERE ${where}`;
56}
57
58export function periods(now: Date): [string, string] {
59 return [monthKey(now), dayKey(now)];
60}
61
62export function definitionOf(row: Row): Definition {
63 return {
64 handle: row.handle,
65 display_name: row.display_name,
66 role: row.role,
67 instructions: row.instructions,
68 personality_preset: PRESETS.includes(row.personality_preset as Definition["personality_preset"])
69 ? (row.personality_preset as Definition["personality_preset"])
70 : "crisp",
71 personality: row.personality,
72 routing: readJson(row.routing, DEFAULT_ROUTING),
73 budget: readJson(row.budget, DEFAULT_BUDGET),
74 autonomy: readJson(row.autonomy, DEFAULT_AUTONOMY),
75 capacity: row.capacity,
76 template: row.template,
77 avatar_seed: row.avatar_seed || row.handle,
78 title: row.title ?? "",
79 team: row.team ?? null,
80 department: row.department ?? "",
81 responsibilities: readList<string>(row.responsibilities),
82 subagents: readList<SubagentDef>(row.subagents),
83 faces: "internal",
84 reading: readList<string>(row.reading ?? null),
85 skills_off: readList<string>(row.skills_off ?? null),
86 };
87}
88
89export function toAgent(row: Row, now: Date): WorkspaceAgent {
90 const definition = definitionOf(row);
91 const spent = { month: row.spent_month ?? 0, day: row.spent_day ?? 0 };
92 return {
93 id: row.id,
94 workspace_id: row.workspace_id,
95 avatar: row.avatar,
96 ...definition,
97 builtin: !!row.builtin,
98 version: row.version,
99 status: row.archived_at ? "paused" : agentStatus({ busyUntil: row.busy_until, now, blocked: budgetBlock(definition.budget, spent, now) !== null }),
100 spent_month_micros: spent.month,
101 created_by: row.created_by,
102 created_at: row.created_at,
103 updated_at: row.updated_at,
104 archived_at: row.archived_at,
105 };
106}
107
108/**
109 * Adds a charge to the paying agent's month and day: a reply's, or a
110 * session step's (`task`), counted as one of each.
111 */
112export function spendStatements(db: D1Database, agentId: string, micros: number, now: Date, task: "reply" | "session" = "reply"): D1PreparedStatement[] {
113 const [replies, sessions] = task === "reply" ? [1, 0] : [0, 1];
114 return periods(now).map((period) =>
115 db
116 .prepare(
117 `INSERT INTO agent_spend (agent_id, period, micros, replies, sessions) VALUES (?1, ?2, ?3, ?4, ?5)
118 ON CONFLICT (agent_id, period) DO UPDATE SET micros = micros + ?3, replies = replies + ?4, sessions = sessions + ?5`,
119 )
120 .bind(agentId, period, Math.max(0, Math.ceil(micros)), replies, sessions),
121 );
122}
123
124/** A stored JSON list, read defensively: anything else is empty. */
125function readList<T>(raw: string | null): T[] {
126 if (!raw) return [];
127 try {
128 const parsed = JSON.parse(raw) as unknown;
129 return Array.isArray(parsed) ? (parsed as T[]) : [];
130 } catch {
131 return [];
132 }
133}
134
135/**
136 * The agents table's definition columns, in the order `definitionColumns`
137 * gives their values. Statements are built from this one list, so a new
138 * field is added in one place.
139 */
140export const DEFINITION_COLUMNS = [
141 "handle",
142 "display_name",
143 "role",
144 "instructions",
145 "personality_preset",
146 "personality",
147 "routing",
148 "budget",
149 "autonomy",
150 "capacity",
151 "template",
152 "avatar_seed",
153 "title",
154 "team",
155 "department",
156 "responsibilities",
157 "subagents",
158 "faces",
159 "reading",
160 "skills_off",
161] as const;
162
163/** A definition's values, in `DEFINITION_COLUMNS` order. */
164export function definitionColumns(d: Definition): (string | number | null)[] {
165 return [
166 d.handle,
167 d.display_name,
168 d.role,
169 d.instructions,
170 d.personality_preset,
171 d.personality,
172 JSON.stringify(d.routing),
173 JSON.stringify(d.budget),
174 JSON.stringify(d.autonomy),
175 d.capacity,
176 d.template,
177 d.avatar_seed,
178 d.title,
179 d.team,
180 d.department,
181 JSON.stringify(d.responsibilities),
182 JSON.stringify(d.subagents),
183 d.faces,
184 JSON.stringify(d.reading ?? []),
185 JSON.stringify(d.skills_off ?? []),
186 ];
187}
188
189/**
190 * A new agent row: `id`, `workspace_id`, the definition, then `extra`
191 * columns (version, builtin, who made it, when), all bound in order.
192 */
193export function insertAgent(db: D1Database, id: string, workspaceId: string, d: Definition, extra: Record<string, string | number>, orIgnore = false): D1PreparedStatement {
194 const names = ["id", "workspace_id", ...DEFINITION_COLUMNS, ...Object.keys(extra)];
195 const values = [id, workspaceId, ...definitionColumns(d), ...Object.values(extra)];
196 return db
197 .prepare(`INSERT ${orIgnore ? "OR IGNORE " : ""}INTO agents (${names.join(", ")}) VALUES (${names.map(() => "?").join(", ")})`)
198 .bind(...values);
199}
200
201/** Sets a row's definition, from the version read only, with `extra` columns. */
202export function updateAgent(db: D1Database, id: string, readVersion: number, d: Definition, extra: Record<string, string | number>): D1PreparedStatement {
203 const names = [...DEFINITION_COLUMNS, ...Object.keys(extra)];
204 return db
205 .prepare(`UPDATE agents SET ${names.map((name) => `${name} = ?`).join(", ")} WHERE id = ? AND version = ?`)
206 .bind(...definitionColumns(d), ...Object.values(extra), id, readVersion);
207}
208
209/** A version of a definition, as saved. */
210export function versionStatement(db: D1Database, agentId: string, version: number, d: Definition, by: string, at: string): D1PreparedStatement {
211 return db
212 .prepare("INSERT INTO agent_versions (agent_id, version, definition, changed_by, created_at) VALUES (?, ?, ?, ?, ?)")
213 .bind(agentId, version, JSON.stringify(d), by, at);
214}