Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

374 linesCodeBlameRaw
1/**
2 * @g1t in chat (docs.g1t.sh/guides/ask/): every member has a direct message
3 * with g1t, and a person writing in it starts an answer from the
4 * workspace's own AI provider, through the integrations service's `ask`.
5 * The top bar's Ask is the same conversation.
6 *
7 * What is decided here is pure, so it is tested on its own (g1t.test.ts):
8 * which direct messages are with g1t, which messages start an answer, the
9 * conversation an answer reads, what g1t says when Ask is not set up, and
10 * reading the answer as it streams. The room (src/room.ts) runs answers;
11 * src/index.ts writes the messages.
12 */
13// The modules themselves, not the package's index, so the tests load them.
14import { G1T_KEY, type AskProposalState, type ChatAnswer, type MemberProfile } from "@g1t/contracts/chat";
15import { ASK_MAX_MESSAGES, ASK_MAX_MESSAGE_CHARS, ASK_MAX_TOTAL_CHARS, type AskMessage, type AskPage } from "@g1t/contracts/ai";
16
17/** How @g1t shows in chat: the g1t mark, never a person. */
18export const G1T_PROFILE: MemberProfile = {
19 kind: "system",
20 id: "g1t",
21 name: "g1t",
22 display_name: "g1t",
23 avatar: null,
24 role: "Answers with your workspace's own AI provider",
25 title: null,
26 avatar_seed: null,
27};
28
29/** The most steps (tool calls and the like) one answer keeps. */
30export const MAX_STEPS = 50;
31/** The longest one step may be, as JSON; longer ones keep their type and a note. */
32const MAX_STEP_BYTES = 8_192;
33/** The most answers one direct message with g1t starts in a minute: one person asking. */
34export const ANSWERS_PER_MINUTE = 20;
35
36/** What the `meta` column keeps (migration 0005): on an answer, a question from a page, or a new-conversation mark. */
37export type MessageMeta = { answer?: ChatAnswer; page?: AskPage; reset?: true };
38
39export function parseMeta(text: string | null | undefined): MessageMeta {
40 if (!text) return {};
41 try {
42 const meta = JSON.parse(text) as unknown;
43 return meta && typeof meta === "object" && !Array.isArray(meta) ? (meta as MessageMeta) : {};
44 } catch {
45 return {};
46 }
47}
48
49/** What a proposal's card has done so far, kept on the answer as a `proposal_state` step. */
50export type ProposalState = AskProposalState;
51
52const PROPOSAL_TEXT = ["production_url", "deploy_error", "session_url", "provider_session_url"] as const;
53
54/**
55 * An answer with what its proposal `id`'s card did merged into it: one
56 * `proposal_state` step per proposal, made the first time, merged after.
57 * Only the known fields are kept, texts cut to 500 characters. Null when
58 * the answer has no such proposal.
59 */
60export function withProposalState(answer: ChatAnswer, change: ProposalState): { answer: ChatAnswer; state: ProposalState } | null {
61 const steps = answer.steps ?? [];
62 if (!steps.some((step) => step.type === "proposal" && step.id === change.id)) return null;
63 const at = steps.findIndex((step) => step.type === "proposal_state" && step.id === change.id);
64 const state: ProposalState = { ...(at === -1 ? {} : (steps[at] as unknown as ProposalState)), id: change.id };
65 for (const key of PROPOSAL_TEXT) {
66 const value = change[key];
67 if (value === null) state[key] = null;
68 else if (typeof value === "string") state[key] = value.slice(0, 500);
69 }
70 if (typeof change.repo_url === "string") state.repo_url = change.repo_url.slice(0, 500);
71 if (typeof change.default_branch === "string") state.default_branch = change.default_branch.slice(0, 200);
72 if (Number.isInteger(change.issue) && change.issue! > 0) state.issue = change.issue;
73 if (typeof change.starting_at === "number") state.starting_at = change.starting_at;
74 if (change.started === true) state.started = true;
75 const step = { type: "proposal_state", ...state } as Record<string, unknown>;
76 const next = at === -1 ? [...steps, step] : steps.map((kept, index) => (index === at ? step : kept));
77 return { answer: { ...answer, steps: next }, state };
78}
79
80export function metaColumn(meta: MessageMeta): string | null {
81 return meta.answer || meta.page || meta.reset ? JSON.stringify(meta) : null;
82}
83
84/**
85 * The person in a direct message with g1t: one person and g1t, no one
86 * else. Null for any other conversation.
87 */
88export function g1tDmPerson(members: readonly string[]): string | null {
89 if (members.length !== 2 || !members.includes(G1T_KEY)) return null;
90 const person = members.find((member) => member !== G1T_KEY)!;
91 return person.startsWith("user:") ? person : null;
92}
93
94/**
95 * Whether a message just written starts an answer: a person's own words,
96 * at the top of a direct message with g1t. g1t's own messages, cards and
97 * thread replies never do; nor does anything anywhere else (mentioning
98 * @g1t in a channel does not, yet).
99 */
100export function startsAnswer(input: {
101 channelKind: "channel" | "dm";
102 members: readonly string[];
103 author: string;
104 threadRoot: string | null;
105 card: boolean;
106 body: string;
107}): boolean {
108 if (input.channelKind !== "dm" || input.threadRoot || input.card || !input.body.trim()) return false;
109 const person = g1tDmPerson(input.members);
110 return person != null && input.author === person;
111}
112
113/** A message as the conversation reads it: oldest first. */
114export type SaidRow = { id: string; author: string; kind: "text" | "card"; body: string; meta: string | null; deleted_at: string | null };
115
116/** The longest note of an earlier answer's reads, in characters. */
117const READS_NOTE_CHARS = 800;
118
119/**
120 * What an earlier answer read in g1t, as a line put before its text when
121 * it is read back: a provider is given only the words of past answers, so
122 * without it the model takes an answer made from tool reads for one made
123 * up, and takes it back. Null when the answer read nothing.
124 */
125export function readsNote(steps: ChatAnswer["steps"]): string | null {
126 const tools = (steps ?? []).filter((step) => step.type === "tool" && typeof step.label === "string");
127 if (tools.length === 0) return null;
128 const parts = tools.map((step) => {
129 const label = String(step.label).trim();
130 const summary = typeof step.summary === "string" ? step.summary.trim() : "";
131 if (step.status === "failed") return `${label} (failed${summary ? `: ${summary}` : ""})`;
132 if (step.status === "refused") return `${label} (not run)`;
133 return summary ? `${label}: ${summary}` : label;
134 });
135 let note = `[g1t's tool reads for this answer: ${parts.join("; ")}]`;
136 if (note.length > READS_NOTE_CHARS) note = `${note.slice(0, READS_NOTE_CHARS - 2)}…]`;
137 return note;
138}
139
140/**
141 * What an answer reads: the direct message's messages after the latest
142 * "New conversation" mark, the person's as `user` and g1t's answers as
143 * `assistant`, oldest first, alternating, starting and ending with the
144 * person, within Ask's limits (the newest kept). Answers that failed, the
145 * set-up note and the answer being written are left out; several messages
146 * in a row from one side are read as one, and each answer starts with what
147 * it read in g1t ([`readsNote`]). Empty when there is nothing to answer.
148 */
149export function conversation(rows: readonly SaidRow[], answering: string | null = null): AskMessage[] {
150 let start = 0;
151 rows.forEach((row, index) => {
152 if (row.author === G1T_KEY && parseMeta(row.meta).reset) start = index + 1;
153 });
154 const turns: AskMessage[] = [];
155 for (const row of rows.slice(start)) {
156 if (row.deleted_at || row.kind !== "text" || row.id === answering) continue;
157 let text = row.body.trim();
158 if (!text) continue;
159 let role: AskMessage["role"];
160 if (row.author === G1T_KEY) {
161 const answer = parseMeta(row.meta).answer;
162 if (!answer || answer.setup || answer.state === "error" || answer.state === "thinking" || answer.state === "streaming") continue;
163 role = "assistant";
164 // First, so cutting a long answer short keeps it.
165 const reads = readsNote(answer.steps);
166 if (reads) text = `${reads}\n\n${text}`;
167 } else if (row.author.startsWith("user:")) {
168 role = "user";
169 } else continue;
170 const last = turns.at(-1);
171 if (last?.role === role) last.content = `${last.content}\n\n${text}`;
172 else turns.push({ role, content: text });
173 }
174 for (const turn of turns) {
175 if (turn.content.length > ASK_MAX_MESSAGE_CHARS) {
176 // The person's newest words matter most: keep the end of a long question, the start of a long answer.
177 turn.content = turn.role === "user" ? turn.content.slice(-ASK_MAX_MESSAGE_CHARS) : turn.content.slice(0, ASK_MAX_MESSAGE_CHARS);
178 }
179 }
180 // The latest word is g1t's: already answered.
181 if (turns.at(-1)?.role !== "user") return [];
182 let kept = turns.slice(-ASK_MAX_MESSAGES);
183 let total = kept.reduce((sum, turn) => sum + turn.content.length, 0);
184 while (kept.length && (kept[0]!.role !== "user" || total > ASK_MAX_TOTAL_CHARS)) {
185 total -= kept[0]!.content.length;
186 kept = kept.slice(1);
187 }
188 return kept;
189}
190
191/** The page the newest question was asked from, if it was asked from one. */
192export function latestPage(rows: readonly SaidRow[]): AskPage | null {
193 for (let at = rows.length - 1; at >= 0; at--) {
194 const row = rows[at]!;
195 if (row.author.startsWith("user:") && !row.deleted_at) return parseMeta(row.meta).page ?? null;
196 }
197 return null;
198}
199
200/**
201 * The id of the question an answer is for: the person's newest message.
202 * Clearing the conversation deletes it, which is how the room knows an
203 * answer it was handed read what is gone (src/room.ts).
204 */
205export function latestQuestion(rows: readonly SaidRow[]): string | null {
206 for (let at = rows.length - 1; at >= 0; at--) {
207 const row = rows[at]!;
208 if (row.author.startsWith("user:") && !row.deleted_at) return row.id;
209 }
210 return null;
211}
212
213/**
214 * A page as a person's message may carry it: only what Ask reads, of the
215 * shapes it reads. Null when nothing of it is usable.
216 */
217export function pageFrom(value: unknown): AskPage | null {
218 if (!value || typeof value !== "object") return null;
219 const sent = value as Record<string, unknown>;
220 const number = (n: unknown) => (typeof n === "number" && Number.isInteger(n) && n > 0 && n < 2 ** 31 ? n : null);
221 const repo = sent.repo as { namespace?: unknown; name?: unknown } | null | undefined;
222 const page: AskPage = {
223 path: typeof sent.path === "string" && sent.path.startsWith("/") ? sent.path.slice(0, 500) : null,
224 repo:
225 repo && typeof repo.namespace === "string" && typeof repo.name === "string" && repo.namespace && repo.name
226 ? { namespace: repo.namespace.slice(0, 100), name: repo.name.slice(0, 100) }
227 : null,
228 issue: number(sent.issue),
229 pull: number(sent.pull),
230 time_zone: typeof sent.time_zone === "string" && /^[A-Za-z][A-Za-z0-9_+\-]*(\/[A-Za-z0-9_+\-]+){0,2}$/.test(sent.time_zone) && sent.time_zone.length <= 64 ? sent.time_zone : null,
231 };
232 return page.path || page.repo || page.time_zone ? page : null;
233}
234
235/** What g1t says when nobody chose an AI provider for Ask yet: once, and where to choose one. */
236export function notSetUpText(slug: string): string {
237 return [
238 "I answer with an AI provider your workspace connects, on its own key, and none is chosen for me yet.",
239 "",
240 `An owner can choose one under [Settings → AI → Ask g1t](/${slug}/-/ai#ask). If you're not an owner, ask one of them to.`,
241 ].join("\n");
242}
243
244/** What g1t says when Ask is set up but cannot answer now: the reason, short. */
245export function cannotAnswerText(problem: string): string {
246 return `I couldn't answer that: ${problem.trim() || "something went wrong."}`;
247}
248
249/**
250 * An answer as it streams in: the integrations service's newline-delimited
251 * events, read as they come. Events it does not know (a newer service's
252 * tool steps, say) are kept as steps, never shown as text, and never
253 * break the answer.
254 */
255export class AnswerReader {
256 body = "";
257 answer: ChatAnswer = { state: "thinking" };
258 private rest = "";
259 private ended = false;
260
261 /** Reads a chunk; true when something shown changed. */
262 feed(chunk: string): boolean {
263 const lines = (this.rest + chunk).split("\n");
264 this.rest = lines.pop() ?? "";
265 let changed = false;
266 for (const line of lines) changed = this.line(line) || changed;
267 return changed;
268 }
269
270 /** The stream ended: whatever is left of a line, and an answer that never said it was done. */
271 finish(): void {
272 if (this.rest) this.line(this.rest);
273 this.rest = "";
274 if (!this.ended) {
275 this.ended = true;
276 if (this.body.trim()) this.answer = { ...this.answer, state: "error", error: "The answer broke off." };
277 else this.answer = { ...this.answer, state: "error", error: this.answer.error ?? "No answer came." };
278 }
279 }
280
281 /** Stopped (Stop, or a newer question): what came stays. */
282 stop(): void {
283 if (this.ended) return;
284 this.ended = true;
285 this.answer = { ...this.answer, state: "stopped" };
286 }
287
288 get over(): boolean {
289 return this.ended;
290 }
291
292 private line(line: string): boolean {
293 if (!line.trim() || this.ended) return false;
294 let event: Record<string, unknown>;
295 try {
296 event = JSON.parse(line) as Record<string, unknown>;
297 } catch {
298 return false;
299 }
300 if (!event || typeof event !== "object" || typeof event.type !== "string") return false;
301 switch (event.type) {
302 case "meta":
303 this.answer = {
304 ...this.answer,
305 provider_label: typeof event.provider_label === "string" ? event.provider_label : null,
306 model: typeof event.model === "string" ? event.model : null,
307 ...(event.auto === true ? { auto: true } : {}),
308 };
309 return true;
310 case "text":
311 if (typeof event.text !== "string" || !event.text) return false;
312 this.body += event.text;
313 this.answer = { ...this.answer, state: "streaming" };
314 return true;
315 case "done":
316 this.ended = true;
317 this.answer = { ...this.answer, state: "done", stop_reason: typeof event.stop_reason === "string" ? event.stop_reason : null };
318 return true;
319 case "error":
320 this.ended = true;
321 this.answer = { ...this.answer, state: "error", error: typeof event.message === "string" ? event.message : "Something went wrong." };
322 return true;
323 default: {
324 const steps = this.answer.steps ?? [];
325 const json = JSON.stringify(event);
326 const step = json.length > MAX_STEP_BYTES ? { type: event.type, id: event.id, truncated: true } : event;
327 // A tool step comes again with its id when it ends, and sources come
328 // whole: each replaces the line it updates, so a step is kept once.
329 const at = steps.findIndex((kept) =>
330 event.type === "sources" ? kept.type === "sources" : event.type === "tool" && typeof event.id === "string" && kept.type === "tool" && kept.id === event.id,
331 );
332 if (at !== -1) {
333 this.answer = { ...this.answer, steps: steps.map((kept, index) => (index === at ? step : kept)) };
334 return true;
335 }
336 if (steps.length >= MAX_STEPS) return false;
337 this.answer = { ...this.answer, steps: [...steps, step] };
338 return true;
339 }
340 }
341 }
342}
343
344/**
345 * The text an answer is stored with at its end: what came, or, when nothing
346 * did, a short line saying why, so g1t never answers with silence.
347 */
348export function finalBody(body: string, answer: ChatAnswer): string {
349 if (body.trim()) return body;
350 if (answer.state === "error") return cannotAnswerText(answer.error ?? "");
351 if (answer.state === "stopped") return "_Stopped before answering._";
352 return "_No answer came._";
353}
354
355/** Whether `times` (ms, oldest first) leave room for another answer now; and the times to keep. */
356export function answerAllowed(times: readonly number[], now: number): { ok: boolean; times: number[] } {
357 const recent = times.filter((at) => now - at < 60_000);
358 if (recent.length >= ANSWERS_PER_MINUTE) return { ok: false, times: recent };
359 return { ok: true, times: [...recent, now] };
360}
361
362/**
363 * An answer older than this that still says it is being written was cut
364 * off (the room that wrote it went away): it shows as stopped.
365 */
366export const STALE_ANSWER_MS = 16 * 60_000;
367
368export function shownAnswer(answer: ChatAnswer | undefined, createdAt: string, now: number): ChatAnswer | null {
369 if (!answer) return null;
370 if ((answer.state === "thinking" || answer.state === "streaming") && now - Date.parse(createdAt) > STALE_ANSWER_MS) {
371 return { ...answer, state: "stopped" };
372 }
373 return answer;
374}