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.

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