-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsend-response.ts
157 lines (144 loc) · 4.24 KB
/
send-response.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
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
import { cleanContent } from '../helpers';
import { logger } from '@cardstack/runtime-common';
import { MatrixClient, sendError, sendMessage, sendOption } from './matrix';
import * as Sentry from '@sentry/node';
import { OpenAIError } from 'openai/error';
import debounce from 'lodash/debounce';
import { ISendEventResponse } from 'matrix-js-sdk/lib/matrix';
import { ChatCompletionMessageToolCall } from 'openai/resources/chat/completions';
import { FunctionToolCall } from '@cardstack/runtime-common/helpers/ai';
let log = logger('ai-bot');
export class Responder {
// internally has a debounced function that will send the text messages
initialMessageId: string | undefined;
initialMessageReplaced = false;
client: MatrixClient;
roomId: string;
messagePromises: Promise<ISendEventResponse | void>[] = [];
debouncedMessageSender: (
content: string,
eventToUpdate: string | undefined,
isStreamingFinished?: boolean,
) => Promise<void>;
constructor(client: MatrixClient, roomId: string) {
this.roomId = roomId;
this.client = client;
this.debouncedMessageSender = debounce(
async (
content: string,
eventToUpdate: string | undefined,
isStreamingFinished = false,
) => {
const messagePromise = sendMessage(
this.client,
this.roomId,
content,
eventToUpdate,
{
isStreamingFinished: isStreamingFinished,
},
);
this.messagePromises.push(messagePromise);
await messagePromise;
},
250,
{ leading: true, maxWait: 250 },
);
}
async initialize() {
let initialMessage = await sendMessage(
this.client,
this.roomId,
'Thinking...',
undefined,
);
this.initialMessageId = initialMessage.event_id;
}
async onChunk(chunk: {
usage?: { prompt_tokens: number; completion_tokens: number };
}) {
// This usage value is set *once* and *only once* at the end of the conversation
// It will be null at all other times.
if (chunk.usage) {
log.info(
`Request used ${chunk.usage.prompt_tokens} prompt tokens and ${chunk.usage.completion_tokens}`,
);
}
}
async onContent(snapshot: string) {
await this.debouncedMessageSender(
cleanContent(snapshot),
this.initialMessageId,
);
this.initialMessageReplaced = true;
}
async onMessage(msg: {
role: string;
tool_calls?: ChatCompletionMessageToolCall[];
}) {
if (msg.role === 'assistant') {
await this.handleFunctionToolCalls(msg);
}
}
deserializeToolCall(
toolCall: ChatCompletionMessageToolCall,
): FunctionToolCall {
let { id, function: f } = toolCall;
return {
type: 'function',
id,
name: f.name,
arguments: JSON.parse(f.arguments),
};
}
async handleFunctionToolCalls(msg: {
role: string;
tool_calls?: ChatCompletionMessageToolCall[];
}) {
for (const toolCall of msg.tool_calls || []) {
log.debug('[Room Timeline] Function call', toolCall);
try {
let optionPromise = sendOption(
this.client,
this.roomId,
this.deserializeToolCall(toolCall),
this.initialMessageReplaced ? undefined : this.initialMessageId,
);
this.messagePromises.push(optionPromise);
await optionPromise;
this.initialMessageReplaced = true;
} catch (error) {
Sentry.captureException(error);
this.initialMessageReplaced = true;
let errorPromise = sendError(
this.client,
this.roomId,
error,
this.initialMessageReplaced ? undefined : this.initialMessageId,
);
this.messagePromises.push(errorPromise);
await errorPromise;
}
}
}
async onError(error: OpenAIError | string) {
Sentry.captureException(error);
return await sendError(
this.client,
this.roomId,
error,
this.initialMessageId,
);
}
async finalize(finalContent: string | void | null | undefined) {
if (finalContent) {
finalContent = cleanContent(finalContent);
await this.debouncedMessageSender(
finalContent,
this.initialMessageId,
true,
);
}
await Promise.all(this.messagePromises);
}
}