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
329 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/integrations";
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 * A page as a person's message may carry it: only what Ask reads, of the
171 * shapes it reads. Null when nothing of it is usable.
172 */
173export function pageFrom(value: unknown): AskPage | null {
174 if (!value || typeof value !== "object") return null;
175 const sent = value as Record<string, unknown>;
176 const number = (n: unknown) => (typeof n === "number" && Number.isInteger(n) && n > 0 && n < 2 ** 31 ? n : null);
177 const repo = sent.repo as { namespace?: unknown; name?: unknown } | null | undefined;
178 const page: AskPage = {
179 path: typeof sent.path === "string" && sent.path.startsWith("/") ? sent.path.slice(0, 500) : null,
180 repo:
181 repo && typeof repo.namespace === "string" && typeof repo.name === "string" && repo.namespace && repo.name
182 ? { namespace: repo.namespace.slice(0, 100), name: repo.name.slice(0, 100) }
183 : null,
184 issue: number(sent.issue),
185 pull: number(sent.pull),
186 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,
187 };
188 return page.path || page.repo || page.time_zone ? page : null;
189}
190
191/** What g1t says when nobody chose an AI provider for Ask yet: once, and where to choose one. */
192export function notSetUpText(slug: string): string {
193 return [
194 "I answer with an AI provider your workspace connects, on its own key, and none is chosen for me yet.",
195 "",
196 `An owner can choose one under [Integrations → Your AI providers → Ask g1t](/${slug}/-/integrations/models#ask). If you're not an owner, ask one of them to.`,
197 ].join("\n");
198}
199
200/** What g1t says when Ask is set up but cannot answer now: the reason, short. */
201export function cannotAnswerText(problem: string): string {
202 return `I couldn't answer that: ${problem.trim() || "something went wrong."}`;
203}
204
205/**
206 * An answer as it streams in: the integrations service's newline-delimited
207 * events, read as they come. Events it does not know (a newer service's
208 * tool steps, say) are kept as steps, never shown as text, and never
209 * break the answer.
210 */
211export class AnswerReader {
212 body = "";
213 answer: ChatAnswer = { state: "thinking" };
214 private rest = "";
215 private ended = false;
216
217 /** Reads a chunk; true when something shown changed. */
218 feed(chunk: string): boolean {
219 const lines = (this.rest + chunk).split("\n");
220 this.rest = lines.pop() ?? "";
221 let changed = false;
222 for (const line of lines) changed = this.line(line) || changed;
223 return changed;
224 }
225
226 /** The stream ended: whatever is left of a line, and an answer that never said it was done. */
227 finish(): void {
228 if (this.rest) this.line(this.rest);
229 this.rest = "";
230 if (!this.ended) {
231 this.ended = true;
232 if (this.body.trim()) this.answer = { ...this.answer, state: "error", error: "The answer broke off." };
233 else this.answer = { ...this.answer, state: "error", error: this.answer.error ?? "No answer came." };
234 }
235 }
236
237 /** Stopped (Stop, or a newer question): what came stays. */
238 stop(): void {
239 if (this.ended) return;
240 this.ended = true;
241 this.answer = { ...this.answer, state: "stopped" };
242 }
243
244 get over(): boolean {
245 return this.ended;
246 }
247
248 private line(line: string): boolean {
249 if (!line.trim() || this.ended) return false;
250 let event: Record<string, unknown>;
251 try {
252 event = JSON.parse(line) as Record<string, unknown>;
253 } catch {
254 return false;
255 }
256 if (!event || typeof event !== "object" || typeof event.type !== "string") return false;
257 switch (event.type) {
258 case "meta":
259 this.answer = {
260 ...this.answer,
261 provider_label: typeof event.provider_label === "string" ? event.provider_label : null,
262 model: typeof event.model === "string" ? event.model : null,
263 };
264 return true;
265 case "text":
266 if (typeof event.text !== "string" || !event.text) return false;
267 this.body += event.text;
268 this.answer = { ...this.answer, state: "streaming" };
269 return true;
270 case "done":
271 this.ended = true;
272 this.answer = { ...this.answer, state: "done", stop_reason: typeof event.stop_reason === "string" ? event.stop_reason : null };
273 return true;
274 case "error":
275 this.ended = true;
276 this.answer = { ...this.answer, state: "error", error: typeof event.message === "string" ? event.message : "Something went wrong." };
277 return true;
278 default: {
279 const steps = this.answer.steps ?? [];
280 const json = JSON.stringify(event);
281 const step = json.length > MAX_STEP_BYTES ? { type: event.type, id: event.id, truncated: true } : event;
282 // A tool step comes again with its id when it ends, and sources come
283 // whole: each replaces the line it updates, so a step is kept once.
284 const at = steps.findIndex((kept) =>
285 event.type === "sources" ? kept.type === "sources" : event.type === "tool" && typeof event.id === "string" && kept.type === "tool" && kept.id === event.id,
286 );
287 if (at !== -1) {
288 this.answer = { ...this.answer, steps: steps.map((kept, index) => (index === at ? step : kept)) };
289 return true;
290 }
291 if (steps.length >= MAX_STEPS) return false;
292 this.answer = { ...this.answer, steps: [...steps, step] };
293 return true;
294 }
295 }
296 }
297}
298
299/**
300 * The text an answer is stored with at its end: what came, or, when nothing
301 * did, a short line saying why, so g1t never answers with silence.
302 */
303export function finalBody(body: string, answer: ChatAnswer): string {
304 if (body.trim()) return body;
305 if (answer.state === "error") return cannotAnswerText(answer.error ?? "");
306 if (answer.state === "stopped") return "_Stopped before answering._";
307 return "_No answer came._";
308}
309
310/** Whether `times` (ms, oldest first) leave room for another answer now; and the times to keep. */
311export function answerAllowed(times: readonly number[], now: number): { ok: boolean; times: number[] } {
312 const recent = times.filter((at) => now - at < 60_000);
313 if (recent.length >= ANSWERS_PER_MINUTE) return { ok: false, times: recent };
314 return { ok: true, times: [...recent, now] };
315}
316
317/**
318 * An answer older than this that still says it is being written was cut
319 * off (the room that wrote it went away): it shows as stopped.
320 */
321export const STALE_ANSWER_MS = 16 * 60_000;
322
323export function shownAnswer(answer: ChatAnswer | undefined, createdAt: string, now: number): ChatAnswer | null {
324 if (!answer) return null;
325 if ((answer.state === "thinking" || answer.state === "streaming") && now - Date.parse(createdAt) > STALE_ANSWER_MS) {
326 return { ...answer, state: "stopped" };
327 }
328 return answer;
329}