Skip to content

Commit 2a3f033

Browse files
committed
feat: options.messages for prompt() and prompt(options)
1 parent bb08b13 commit 2a3f033

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

lib/prompt.js

+27-16
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,32 @@
22

33
/** @type {import('..').PromptInterface} */
44
export async function prompt(userPrompt, promptOptions) {
5-
const promptFetch = promptOptions.request?.fetch || fetch;
5+
const options = typeof userPrompt === "string" ? promptOptions : userPrompt;
66

7-
const systemMessage = promptOptions.tools
7+
const promptFetch = options.request?.fetch || fetch;
8+
9+
const systemMessage = options.tools
810
? "You are a helpful assistant. Use the supplied tools to assist the user."
911
: "You are a helpful assistant.";
1012

13+
const messages = [
14+
{
15+
role: "system",
16+
content: systemMessage,
17+
},
18+
];
19+
20+
if (options.messages) {
21+
messages.push(...options.messages);
22+
}
23+
24+
if (typeof userPrompt === "string") {
25+
messages.push({
26+
role: "user",
27+
content: userPrompt,
28+
});
29+
}
30+
1131
const response = await promptFetch(
1232
"https://api.githubcopilot.com/chat/completions",
1333
{
@@ -16,22 +36,13 @@ export async function prompt(userPrompt, promptOptions) {
1636
accept: "application/json",
1737
"content-type": "application/json; charset=UTF-8",
1838
"user-agent": "copilot-extensions/preview-sdk.js",
19-
authorization: `Bearer ${promptOptions.token}`,
39+
authorization: `Bearer ${options.token}`,
2040
},
2141
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,
42+
messages: messages,
43+
model: options.model,
44+
toolChoice: options.tools ? "auto" : undefined,
45+
tools: options.tools,
3546
}),
3647
}
3748
);

0 commit comments

Comments
 (0)