-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathresponse.js
59 lines (53 loc) · 1.49 KB
/
response.js
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
// @ts-check
/** @type {import('..').CreateAckEventInterface} */
export function createAckEvent() {
return createTextEvent("");
}
/** @type {import('..').CreateTextEventInterface} */
export function createTextEvent(message) {
const data = {
choices: [
{
index: 0,
delta: { content: message, role: "assistant" },
},
],
};
return `data: ${JSON.stringify(data)}\n\n`;
}
/** @type {import('..').CreateConfirmationEventInterface} */
export function createConfirmationEvent({ id, title, message, metadata }) {
const event = "copilot_confirmation";
const data = {
type: "action",
title,
message,
confirmation: { id, ...metadata },
};
return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`;
}
/** @type {import('..').CreateReferencesEventInterface} */
export function createReferencesEvent(references) {
const event = "copilot_references";
const data = references;
return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`;
}
/** @type {import('..').CreateErrorsEventInterface} */
export function createErrorsEvent(errors) {
const event = "copilot_errors";
const data = errors;
return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`;
}
/** @type {import('..').CreateDoneEventInterface} */
export function createDoneEvent() {
const data = {
choices: [
{
index: 0,
finish_reason: "stop",
delta: { content: null },
},
],
};
return `data: ${JSON.stringify(data)}\n\ndata: [DONE]\n\n`;
}