diff --git a/lib/prompt.js b/lib/prompt.js index 77bbab9..e7c9db0 100644 --- a/lib/prompt.js +++ b/lib/prompt.js @@ -2,12 +2,32 @@ /** @type {import('..').PromptInterface} */ export async function prompt(userPrompt, promptOptions) { - const promptFetch = promptOptions.request?.fetch || fetch; + const options = typeof userPrompt === "string" ? promptOptions : userPrompt; - const systemMessage = promptOptions.tools + const promptFetch = options.request?.fetch || fetch; + + const systemMessage = options.tools ? "You are a helpful assistant. Use the supplied tools to assist the user." : "You are a helpful assistant."; + const messages = [ + { + role: "system", + content: systemMessage, + }, + ]; + + if (options.messages) { + messages.push(...options.messages); + } + + if (typeof userPrompt === "string") { + messages.push({ + role: "user", + content: userPrompt, + }); + } + const response = await promptFetch( "https://api.githubcopilot.com/chat/completions", { @@ -16,22 +36,13 @@ export async function prompt(userPrompt, promptOptions) { accept: "application/json", "content-type": "application/json; charset=UTF-8", "user-agent": "copilot-extensions/preview-sdk.js", - authorization: `Bearer ${promptOptions.token}`, + authorization: `Bearer ${options.token}`, }, body: JSON.stringify({ - messages: [ - { - role: "system", - content: systemMessage, - }, - { - role: "user", - content: userPrompt, - }, - ], - model: promptOptions.model, - toolChoice: promptOptions.tools ? "auto" : undefined, - tools: promptOptions.tools, + messages: messages, + model: options.model, + toolChoice: options.tools ? "auto" : undefined, + tools: options.tools, }), } );