-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathChatKitPanel.tsx
More file actions
577 lines (529 loc) · 18.3 KB
/
ChatKitPanel.tsx
File metadata and controls
577 lines (529 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
import { useCallback, useEffect, useMemo, useState } from "react";
import { ChatKit, useChatKit, ChatKitIcon } from "@openai/chatkit-react";
import { parser_pb, useCell } from "../../contexts/CellContext";
import { useNotebookContext } from "../../contexts/NotebookContext";
import { useOutput } from "../../contexts/OutputContext";
import { useCurrentDoc } from "../../contexts/CurrentDocContext";
import { create, fromJsonString, toJson } from "@bufbuild/protobuf";
import { getAccessToken, getAuthData } from "../../token";
import { getBrowserAdapter } from "../../browserAdapter.client";
import { type Cell } from "../../protogen/runme/parser/v1/parser_pb.js";
import {
ToolCallInputSchema,
ToolCallOutputSchema,
ToolCallOutput_Status,
UpdateCellsResponseSchema,
GetCellsResponseSchema,
ListCellsResponseSchema,
ChatkitStateSchema,
} from "../../protogen/oaiproto/aisre/notebooks_pb.js";
import { useAgentEndpointSnapshot } from "../../lib/agentEndpointManager";
import { getConfiguredChatKitDomainKey } from "../../lib/appConfig";
class UserNotLoggedInError extends Error {
constructor(message = "You must log in to use runme chat.") {
super(message);
this.name = "UserNotLoggedInError";
}
}
const CHATKIT_GREETING = "How can runme help you today?";
const CHATKIT_PLACEHOLDER =
"Describe the production issue or question you are investigating";
const CHATKIT_STARTER_PROMPTS = [
{
label: "Setup a local runner for runme to execute code",
prompt: "How do I setup a local runner to execute code with runme?",
icon: "circle-question",
},
{
label: "Plot metrics",
prompt: "Plot the requests for the o3 model.",
icon: "book-open",
},
{
label: "Handle an alert or incident",
prompt:
"I just got paged for TBT (time between tokens) being high. Search notion, the mono-repo, and slack for runbooks for dealing with this alert and give me instrunctions for dealing with it?",
icon: "search",
},
] as const;
const TOOL_PREFIX = "agent_tools_v1_NotebookService_";
const UPDATE_CELLS_TOOL = TOOL_PREFIX + "UpdateCells";
const LIST_CELLS_TOOL = TOOL_PREFIX + "ListCells";
const GET_CELLS_TOOL = TOOL_PREFIX + "GetCells";
type SSEInterceptor = (rawEvent: string) => void;
const useAuthorizedFetch = (
getChatkitState: () => ReturnType<(typeof ChatkitStateSchema)["create"]>,
options?: { onSSEEvent?: SSEInterceptor },
) => {
const { onSSEEvent } = options ?? {};
return useMemo(() => {
const authorizedFetch: typeof fetch = async (
input: RequestInfo | URL,
init?: RequestInit,
) => {
try {
const authData = await getAuthData();
const idToken = authData?.idToken ?? undefined;
const oaiAccessToken = await getAccessToken();
if (!oaiAccessToken) {
throw new UserNotLoggedInError();
}
const headers = new Headers(
init?.headers ??
(input instanceof Request ? input.headers : undefined),
);
if (idToken) {
headers.set("Authorization", `Bearer ${idToken}`);
}
headers.set("OpenAIAccessToken", oaiAccessToken);
let body = init?.body;
const method =
init?.method ?? (input instanceof Request ? input.method : "GET");
if (method.toUpperCase() === "POST") {
const state = getChatkitState();
const chatkitStateJson = toJson(ChatkitStateSchema, state);
if (body == null) {
body = JSON.stringify({ chatkit_state: chatkitStateJson });
headers.set("Content-Type", "application/json");
} else {
if (typeof body === "string") {
try {
const parsed = JSON.parse(body);
parsed.chatkit_state = chatkitStateJson;
body = JSON.stringify(parsed);
} catch {
const payload = new FormData();
payload.append("payload", body);
payload.append(
"chatkit_state",
JSON.stringify(chatkitStateJson),
);
body = payload;
}
} else if (body instanceof FormData) {
body.set("chatkit_state", JSON.stringify(chatkitStateJson));
} else if (body instanceof URLSearchParams) {
body.set("chatkit_state", JSON.stringify(chatkitStateJson));
} else if (body instanceof Blob || body instanceof ArrayBuffer) {
const payload = new FormData();
payload.append("payload", new Blob([body]));
payload.append("chatkit_state", JSON.stringify(chatkitStateJson));
body = payload;
}
}
}
const nextInit: RequestInit = {
...init,
headers,
body,
};
const response = await fetch(input, nextInit);
//return response;
const isSSE =
onSSEEvent &&
response.headers
.get("content-type")
?.toLowerCase()
.includes("text/event-stream") &&
response.body;
if (!isSSE || !response.body) {
return response;
}
const decoder = new TextDecoder();
const encoder = new TextEncoder();
const reader = response.body.getReader();
let buffer = "";
const stream = new ReadableStream<Uint8Array>({
async pull(controller) {
const { done, value } = await reader.read();
if (done) {
const tail = decoder.decode();
if (tail) {
buffer += tail;
}
if (buffer.length > 0) {
try {
onSSEEvent?.(buffer);
} catch (eventError) {
console.error("SSE interceptor error", eventError);
}
controller.enqueue(encoder.encode(buffer));
}
controller.close();
return;
}
if (value) {
buffer += decoder.decode(value, { stream: true });
let boundary;
while ((boundary = buffer.indexOf("\n\n")) !== -1) {
const rawEvent = buffer.slice(0, boundary + 2);
buffer = buffer.slice(boundary + 2);
try {
onSSEEvent?.(rawEvent);
} catch (eventError) {
console.error("SSE interceptor error", eventError);
}
controller.enqueue(encoder.encode(rawEvent));
}
}
},
async cancel(reason) {
try {
await reader.cancel(reason);
} catch (cancelError) {
console.error("Failed to cancel SSE reader", cancelError);
}
},
});
const interceptedResponse = new Response(stream, {
status: response.status,
statusText: response.statusText,
headers: new Headers(response.headers),
});
return interceptedResponse;
} catch (error) {
console.error("ChatKit authorized fetch failed", error);
throw error;
}
};
return authorizedFetch;
}, [onSSEEvent, getChatkitState]);
};
function ChatKitPanel() {
const [showLoginPrompt, setShowLoginPrompt] = useState(false);
const chatkitDomainKey = getConfiguredChatKitDomainKey();
const agentEndpoint = useAgentEndpointSnapshot();
const { getChatkitState, setChatkitState } = useCell();
const { getNotebookData, useNotebookSnapshot } = useNotebookContext();
const { getCurrentDoc } = useCurrentDoc();
const { getAllRenderers } = useOutput();
const currentDocUri = getCurrentDoc();
const notebookSnapshot = useNotebookSnapshot(currentDocUri ?? "");
const orderedCells = useMemo(
() => notebookSnapshot?.notebook.cells ?? [],
[notebookSnapshot],
);
const updateCell = useCallback(
(cell: Cell) => {
if (!cell?.refId || !currentDocUri) {
return;
}
const data = getNotebookData(currentDocUri);
if (!data) {
return;
}
for (const renderer of getAllRenderers().values()) {
renderer.onCellUpdate(cell as unknown as parser_pb.Cell);
}
data.updateCell(cell as unknown as parser_pb.Cell);
},
[currentDocUri, getAllRenderers, getNotebookData],
);
const handleSseEvent = useCallback(
(rawEvent: string) => {
const lines = rawEvent
.split(/\r?\n/)
.map((line) => line.trim())
.filter(Boolean);
for (const line of lines) {
if (!line.startsWith("data:")) {
continue;
}
const payload = line.slice("data:".length).trim();
if (!payload) {
continue;
}
try {
const parsed = JSON.parse(payload);
if (parsed?.type !== "aisre.chatkit.state") {
continue;
}
const item = parsed?.item ?? parsed?.Item;
if (!item) {
continue;
}
const stateData = item.state ?? item.State ?? item;
if (!stateData) {
continue;
}
const state = fromJsonString(
ChatkitStateSchema,
JSON.stringify(stateData),
);
setChatkitState(state);
if (state.previousResponseId || state.threadId) {
console.log(
"ChatKit state update",
JSON.stringify(
{
previous_response_id: state.previousResponseId,
thread_id: state.threadId,
},
null,
2,
),
);
}
} catch (error) {
console.error("Failed to parse SSE state event", error, payload);
}
}
},
[setChatkitState],
);
const authorizedFetch = useAuthorizedFetch(getChatkitState, {
onSSEEvent: handleSseEvent,
});
const chatkitApiUrl = useMemo(() => {
const base = agentEndpoint.endpoint ?? "";
try {
const url = new URL(base);
url.pathname = "/chatkit";
url.search = "";
url.hash = "";
return url.toString();
} catch {
return `${base.replace(/\/$/, "")}/chatkit`;
}
}, [agentEndpoint.endpoint]);
const chatkit = useChatKit({
api: {
url: chatkitApiUrl,
domainKey: chatkitDomainKey,
fetch: authorizedFetch,
},
theme: {
colorScheme: "light",
radius: "round",
},
startScreen: {
greeting: CHATKIT_GREETING,
prompts: CHATKIT_STARTER_PROMPTS,
},
// see: https://openai.github.io/chatkit-js/api/openai/chatkit/type-aliases/composeroption/#tools
composer: {
placeholder: CHATKIT_PLACEHOLDER,
models: [
{
id: "gpt-4o-mini",
label: "GPT-4o Mini",
},
{
id: "gpt-5",
label: "GPT-5",
},
// gpt-5.2 appears to be about 2x as slow as gpt-4.1-mini-2025-04-14
// but for a simple query that's 2s vs 1s so not a huge difference
// This is still 10x faster than gpt 5 which took about 10x and felt like
// molasses.
{
id: "gpt-5.2",
label: "GPT-5.2",
default: true,
},
{
id: "gpt-5-mini",
label: "GPT-5 Mini",
},
{
id: "gpt-5-nano",
label: "GPT-5 Nano",
},
],
// TODO(jlewi): We want to make the company knowledge tool optional but on by default.
// Unfortunately if we make it a tool it is not on by default and there doesn't seem to be a way to
// select it programmatically.
// tools: [
// {
// icon: "search" as ChatKitIcon,
// id: "company-knowledge",
// label: "Search Company Knowledge",
// persistent: true,
// pinned: true,
// },
// ],
},
header: {
enabled: true,
},
history: {
enabled: true,
},
onClientTool: async (invocation) => {
const toolOutput = create(ToolCallOutputSchema, {
callId: "",
previousResponseId: "",
status: ToolCallOutput_Status.SUCCESS,
clientError: "",
});
let decodedInput;
try {
const payload =
typeof invocation.params === "string"
? invocation.params
: JSON.stringify(invocation.params ?? {});
decodedInput = fromJsonString(ToolCallInputSchema, payload);
} catch (error) {
console.error("Failed to decode tool params", error, invocation.params);
toolOutput.status = ToolCallOutput_Status.FAILED;
toolOutput.clientError = `Failed to decode tool params: ${error}`;
return {
success: false,
result: toJson(ToolCallOutputSchema, toolOutput),
};
}
toolOutput.callId = decodedInput.callId;
toolOutput.previousResponseId = decodedInput.previousResponseId;
const inputCase = decodedInput.input?.case;
const cellMap = new Map<string, Cell>();
orderedCells.forEach((cell) => {
cellMap.set(cell.refId, cell);
});
switch (invocation.name) {
case UPDATE_CELLS_TOOL: {
console.log(`[ChatKit tool] ${invocation.name}`, decodedInput);
if (inputCase !== "updateCells") {
toolOutput.status = ToolCallOutput_Status.FAILED;
toolOutput.clientError =
"UpdateCells tool invoked without updateCells payload";
break;
}
const updateCellsRequest = decodedInput.input.value;
if (!updateCellsRequest) {
toolOutput.status = ToolCallOutput_Status.FAILED;
toolOutput.clientError = "UpdateCells request missing payload";
break;
}
const cells: Cell[] = updateCellsRequest.cells ?? [];
if (cells.length === 0) {
toolOutput.status = ToolCallOutput_Status.FAILED;
toolOutput.clientError =
"UpdateCells invoked without cells payload";
}
cells.forEach((updatedCell: Cell) => {
try {
if (!updatedCell?.refId) {
console.warn("Received cell without refId", updatedCell);
return;
}
updateCell(updatedCell);
} catch (error) {
console.error(
"Failed to process UpdateCell payload",
error,
updatedCell,
);
toolOutput.status = ToolCallOutput_Status.FAILED;
toolOutput.clientError = `Failed to process UpdateCell payload: ${error}`;
}
});
toolOutput.output = {
case: "updateCells",
value: create(UpdateCellsResponseSchema, {
cells,
}),
};
break;
}
case GET_CELLS_TOOL: {
console.log(`[ChatKit tool] ${invocation.name}`, decodedInput);
if (inputCase !== "getCells") {
toolOutput.status = ToolCallOutput_Status.FAILED;
toolOutput.clientError =
"GetCells tool invoked without getCells payload";
break;
}
const getCellsRequest = decodedInput.input.value;
if (!getCellsRequest) {
toolOutput.status = ToolCallOutput_Status.FAILED;
toolOutput.clientError = "GetCells request missing payload";
break;
}
const requestedRefs = getCellsRequest.refIds ?? [];
const foundCells = requestedRefs
.map((id) => {
const cell = cellMap.get(id);
if (!cell) {
console.warn(`Requested cell ${id} not found`);
}
return cell;
})
.filter((cell): cell is Cell => Boolean(cell));
toolOutput.output = {
case: "getCells",
value: create(GetCellsResponseSchema, {
cells: foundCells,
}),
};
break;
}
case LIST_CELLS_TOOL: {
console.log(`[ChatKit tool] ${invocation.name}`, decodedInput);
toolOutput.output = {
case: "listCells",
value: create(ListCellsResponseSchema, {
cells: orderedCells,
}),
};
break;
}
default: {
toolOutput.status = ToolCallOutput_Status.FAILED;
toolOutput.clientError = `Unknown tool ${invocation.name}`;
return toJson(ToolCallOutputSchema, toolOutput);
}
}
return toJson(ToolCallOutputSchema, toolOutput);
},
onError: ({ error }) => {
const promptForLogin = () => setShowLoginPrompt(true);
// This is a bit of a hacky way to check for authentication errors.
// Chatkit throws a StreamError if the user isn't logged in.
void (async () => {
const token = await getAccessToken();
if (!token) {
promptForLogin();
}
})();
console.error("ChatKit error", error);
},
});
const handleLogin = useCallback(() => {
setShowLoginPrompt(false);
getBrowserAdapter().loginWithRedirect();
}, []);
const handleDismissPrompt = useCallback(() => {
setShowLoginPrompt(false);
}, []);
return (
<div className="relative h-full w-full">
<ChatKit control={chatkit.control} className="block h-full w-full" />
{showLoginPrompt ? (
<div className="pointer-events-auto absolute inset-0 z-50 flex items-center justify-center bg-white/90 p-4 text-sm">
<div className="w-full max-w-sm rounded-nb-md border border-nb-cell-border bg-white p-4 shadow-nb-lg">
<p className="mb-4 text-nb-text">
Please log in to use runme chat features.
</p>
<div className="flex justify-end gap-2">
<button
type="button"
className="rounded border border-nb-text px-3 py-1 text-nb-text hover:bg-nb-surface-2"
onClick={handleLogin}
>
Log In
</button>
<button
type="button"
className="rounded border border-nb-cell-border px-3 py-1 text-nb-text-muted hover:bg-nb-surface-2"
onClick={handleDismissPrompt}
>
Cancel
</button>
</div>
</div>
</div>
) : null}
</div>
);
}
export default ChatKitPanel;