-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathparse.js
58 lines (48 loc) · 1.39 KB
/
parse.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
// @ts-check
import { verifyRequestByKeyId } from "./verification.js";
/** @type {import('..').ParseRequestBodyInterface} */
export function parseRequestBody(body) {
return JSON.parse(body);
}
/** @type {import('..').TransformPayloadForOpenAICompatibilityInterface} */
export function transformPayloadForOpenAICompatibility(payload) {
return {
messages: payload.messages.map((message) => {
return {
role: message.role,
name: message.name,
content: message.content,
};
}),
};
}
/** @type {import('..').VerifyAndParseRequestInterface} */
export async function verifyAndParseRequest(body, signature, keyID, options) {
const { isValid, cache } = await verifyRequestByKeyId(
body,
signature,
keyID,
options,
);
return {
isValidRequest: isValid,
payload: parseRequestBody(body),
cache,
};
}
/** @type {import('..').GetUserMessageInterface} */
export function getUserMessage(payload) {
return payload.messages[payload.messages.length - 1].content;
}
/** @type {import('..').GetUserConfirmationInterface} */
export function getUserConfirmation(payload) {
const confirmation =
payload.messages[payload.messages.length - 1].copilot_confirmations?.[0];
if (!confirmation) return;
const { id, ...metadata } = confirmation.confirmation;
return {
accepted: confirmation.state === "accepted",
id,
metadata,
};
}