-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
234 lines (214 loc) · 5.94 KB
/
index.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
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
// @ts-check
import { createServer } from "node:http";
import { Octokit } from "@octokit/core";
import {
createAckEvent,
createConfirmationEvent,
createDoneEvent,
createReferencesEvent,
createTextEvent,
createErrorsEvent,
verifyAndParseRequest,
getUserMessage,
getUserConfirmation,
getFunctionCalls,
prompt,
} from "@copilot-extensions/preview-sdk";
const functions = [
{
type: /** @type {const} */ ("function"),
function: {
name: "get_delivery_date",
description:
"Get the delivery date for a customer's order. Call this whenever you need to know the delivery date, for example when a customer asks 'Where is my package'",
parameters: {
type: "object",
properties: {
order_id: {
type: "string",
description: "The customer's order ID.",
},
},
required: ["order_id"],
additionalProperties: false,
},
},
},
];
// Create a local server to receive data from
const server = createServer(async (request, response) => {
if (request.method === "GET") {
response.statusCode = 200;
response.end(`Hello, there!`);
return;
}
const body = await getBody(request);
const signature = String(request.headers["github-public-key-signature"]);
const keyID = String(request.headers["github-public-key-identifier"]);
const tokenForUser = String(request.headers["x-github-token"]);
const { isValidRequest, payload } = await verifyAndParseRequest(
body,
signature,
keyID,
{
token: tokenForUser,
},
);
// debug log
// console.log(
// JSON.stringify(
// {
// headers: {
// ...request.headers,
// "x-github-token": "REDACTED",
// },
// body: payload,
// },
// null,
// 2
// )
// );
if (!isValidRequest) {
response.statusCode = 401;
response.end(`Signature verification failed`);
return;
}
console.log("Request verified and parsed");
// Acknowledge the request
response.write(createAckEvent());
console.log("Request acknowledged");
// get user info
const octokit = new Octokit({ auth: tokenForUser });
const { data: user } = await octokit.request("GET /user");
// get user's last message
const userConfirmation = getUserConfirmation(payload);
const userMessage = getUserMessage(payload);
const result = await prompt({
model: "gpt-4",
token: tokenForUser,
messages: payload.messages,
tools: functions,
});
const [functionCall] = getFunctionCalls(result);
if (functionCall) {
// simulate function call
const args = JSON.parse(functionCall.function.arguments);
const functionCallResultMessage = {
// CAPI currently does not accept `role: "function"` or `role: "tool"
// role: "function",
role: "system",
content: JSON.stringify({
order_id: args.order_id,
delivery_date: "2024-12-24",
}),
};
const result = await prompt({
model: "gpt-4",
token: tokenForUser,
messages: [...payload.messages, functionCallResultMessage],
tools: functions,
});
console.log(JSON.stringify(result, null, 2));
response.write(createTextEvent(result.message.content));
} else if (userConfirmation) {
// send text acknoledging the confirmation choice
response.write(
createTextEvent(
`ok, @${user.login}, ${
userConfirmation.accepted ? "accepted" : "dismissed"
}!`,
),
);
console.log(
"Text response acknowledged the confirmation choice sent",
userConfirmation,
);
} else if (/confirm/i.test(userMessage)) {
// send a confirmation message
response.write(
createConfirmationEvent({
title: `Are you @${user.login}?`,
message: "Just making sure",
id: "1",
}),
);
console.log("Confirmation response sent");
} else if (/reference/i.test(userMessage)) {
response.write(
createTextEvent(`ok, @${user.login}, a reference is incoming:`),
);
// send a reference
response.write(
createReferencesEvent([
{
type: "blackbeard.story",
id: "snippet",
data: {
file: "story.go",
start: "0",
end: "13",
content: "func main()...writeStory()...",
},
is_implicit: false,
metadata: {
display_name: "Lines 1-13 from story.go",
display_icon: "icon",
display_url: "http://blackbeard.com/story/1",
},
},
]),
);
console.log("Reference response sent");
} else if (/error/i.test(userMessage)) {
response.write(
createTextEvent(`ok, @${user.login}, here are some errors:`),
);
// send errors
const referenceError = {
type: "reference",
code: "1",
message: "test reference error",
identifier: "reference-identifier",
};
const functionError = {
type: "function",
code: "1",
message: "test function error",
identifier: "function-identifier",
};
const agentError = {
type: "agent",
code: "1",
message: "test agent error",
identifier: "agent-identifier",
};
response.write(
// @ts-expect-error
createErrorsEvent([referenceError, functionError, agentError]),
);
console.log("Confirmation response sent");
} else {
// send a text message
response.write(createTextEvent(result.message.content));
console.log("Text response sent");
}
// close the connection
response.end(createDoneEvent());
console.log("Socket closed");
});
server.listen(3000);
console.log("listening at http://localhost:3000");
function getBody(request) {
return new Promise((resolve) => {
const bodyParts = [];
let body;
request
.on("data", (chunk) => {
bodyParts.push(chunk);
})
.on("end", () => {
body = Buffer.concat(bodyParts).toString();
resolve(body);
});
});
}