Skip to content

Commit 42533d0

Browse files
committed
feat: prompt()
1 parent 3882b61 commit 42533d0

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

index.d.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ export interface Message {
158158
content: string
159159
copilot_references: MessageCopilotReference[]
160160
copilot_confirmations?: MessageCopilotConfirmation[]
161+
tool_calls?: {
162+
"function": {
163+
"arguments": string,
164+
"name": string
165+
},
166+
"id": string,
167+
"type": "function"
168+
}[]
161169
name?: string
162170
}
163171

@@ -244,6 +252,42 @@ export interface GetUserConfirmationInterface {
244252
(payload: CopilotRequestPayload): UserConfirmation | undefined;
245253
}
246254

255+
// prompt
256+
257+
/** model names supported by Copilot API */
258+
export type ModelName =
259+
| "gpt-4"
260+
| "gpt-3.5-turbo"
261+
262+
export interface PromptFunction {
263+
type: "function"
264+
function: {
265+
name: string;
266+
description?: string;
267+
/** @see https://platform.openai.com/docs/guides/structured-outputs/supported-schemas */
268+
parameters?: Record<string, unknown>;
269+
strict?: boolean | null;
270+
}
271+
}
272+
273+
export type PromptOptions = {
274+
model: ModelName
275+
token: string
276+
tools?: PromptFunction[]
277+
request?: {
278+
fetch?: Function
279+
}
280+
}
281+
282+
export type PromptResult = {
283+
requestId: string
284+
message: Message
285+
}
286+
287+
interface PromptInterface {
288+
(userPrompt: string, options: PromptOptions): Promise<PromptResult>;
289+
}
290+
247291
// exported methods
248292

249293
export declare const verifyRequest: VerifyRequestInterface;
@@ -261,4 +305,6 @@ export declare const parseRequestBody: ParseRequestBodyInterface;
261305
export declare const transformPayloadForOpenAICompatibility: TransformPayloadForOpenAICompatibilityInterface;
262306
export declare const verifyAndParseRequest: VerifyAndParseRequestInterface;
263307
export declare const getUserMessage: GetUserMessageInterface;
264-
export declare const getUserConfirmation: GetUserConfirmationInterface;
308+
export declare const getUserConfirmation: GetUserConfirmationInterface;
309+
310+
export declare const prompt: PromptInterface;

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
export * from "./lib/parse.js";
44
export * from "./lib/response.js";
55
export * from "./lib/verification.js";
6+
export * from "./lib/prompt.js";

lib/prompt.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// @ts-check
2+
3+
/** @type {import('..').PromptInterface} */
4+
export async function prompt(userPrompt, promptOptions) {
5+
const promptFetch = promptOptions.request?.fetch || fetch;
6+
7+
const systemMessage = promptOptions.tools
8+
? "You are a helpful assistant. Use the supplied tools to assist the user."
9+
: "You are a helpful assistant.";
10+
11+
const response = await promptFetch(
12+
"https://api.githubcopilot.com/chat/completions",
13+
{
14+
method: "POST",
15+
headers: {
16+
accept: "application/json",
17+
"content-type": "application/json; charset=UTF-8",
18+
"user-agent": "copilot-extensions/preview-sdk.js",
19+
authorization: `Bearer ${promptOptions.token}`,
20+
},
21+
body: JSON.stringify({
22+
messages: [
23+
{
24+
role: "system",
25+
content: systemMessage,
26+
},
27+
{
28+
role: "user",
29+
content: userPrompt,
30+
},
31+
],
32+
model: promptOptions.model,
33+
toolChoice: promptOptions.tools ? "auto" : undefined,
34+
tools: promptOptions.tools,
35+
}),
36+
}
37+
);
38+
39+
const data = await response.json();
40+
41+
return {
42+
requestId: response.headers.get("x-request-id"),
43+
message: data.choices[0].message,
44+
};
45+
}

0 commit comments

Comments
 (0)