-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdebug.ts
72 lines (70 loc) · 1.92 KB
/
debug.ts
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
import { setTitle } from './set-title';
import { sendErrorEvent, sendMessageEvent, MatrixClient } from './matrix';
import OpenAI from 'openai';
import * as Sentry from '@sentry/node';
export async function handleDebugCommands(
openai: OpenAI,
eventBody: string,
client: MatrixClient,
roomId: string,
userId: string,
) {
// Explicitly set the room name
if (eventBody.startsWith('debug:title:set:')) {
return await client.setRoomName(
roomId,
eventBody.split('debug:title:set:')[1],
);
} else if (eventBody.startsWith('debug:boom')) {
await sendErrorEvent(
client,
roomId,
`Boom! Throwing an unhandled error`,
undefined,
);
throw new Error('Boom!');
}
// Use GPT to set the room title
else if (eventBody.startsWith('debug:title:create')) {
return await setTitle(openai, client, roomId, [], userId);
} else if (eventBody.startsWith('debug:patch:')) {
let patchMessage = eventBody.split('debug:patch:')[1];
// If there's a card attached, we need to split it off to parse the json
patchMessage = patchMessage.split('(Card')[0];
let toolArguments: {
attributes?: {
cardId?: string;
patch?: any;
};
description?: string;
} = {};
try {
toolArguments = JSON.parse(patchMessage);
if (
!toolArguments.attributes?.cardId ||
!toolArguments.attributes?.patch
) {
throw new Error(
'Invalid debug patch: attributes.cardId, or attributes.patch is missing.',
);
}
} catch (error) {
Sentry.captureException(error);
return await sendMessageEvent(
client,
roomId,
`Error parsing your debug patch, ${error} ${patchMessage}`,
undefined,
{},
[
{
id: 'patchCard-debug',
name: 'patchCard',
arguments: toolArguments,
},
],
);
}
}
return;
}