11// @ts -check
22
33/** @type {import('..').PromptInterface } */
4- export async function prompt ( userPrompt , promptOptions ) {
5- const options = typeof userPrompt === "string" ? promptOptions : userPrompt ;
64
7- const promptFetch = options . request ?. fetch || fetch ;
8- const modelName = options . model || "gpt-4" ;
5+ function parsePromptArguments ( userPrompt , promptOptions ) {
6+ const { request : requestOptions , ...options } =
7+ typeof userPrompt === "string" ? promptOptions : userPrompt ;
8+
9+ const promptFetch = requestOptions ?. fetch || fetch ;
10+ const model = options . model || "gpt-4" ;
11+ const endpoint =
12+ options . endpoint || "https://api.githubcopilot.com/chat/completions" ;
913
1014 const systemMessage = options . tools
1115 ? "You are a helpful assistant. Use the supplied tools to assist the user."
1216 : "You are a helpful assistant." ;
17+ const toolsChoice = options . tools ? "auto" : undefined ;
1318
1419 const messages = [
1520 {
@@ -29,44 +34,87 @@ export async function prompt(userPrompt, promptOptions) {
2934 } ) ;
3035 }
3136
32- const response = await promptFetch (
33- "https://api.githubcopilot.com/chat/completions" ,
34- {
35- method : "POST" ,
36- headers : {
37- accept : "application/json" ,
38- "content-type" : "application/json; charset=UTF-8" ,
39- "user-agent" : "copilot-extensions/preview-sdk.js" ,
40- authorization : `Bearer ${ options . token } ` ,
41- } ,
42- body : JSON . stringify ( {
43- messages : messages ,
44- model : modelName ,
45- toolChoice : options . tools ? "auto" : undefined ,
46- tools : options . tools ,
47- } ) ,
48- }
49- ) ;
37+ return [ promptFetch , { ...options , messages, model, endpoint, toolsChoice } ] ;
38+ }
5039
51- if ( response . ok ) {
52- const data = await response . json ( ) ;
40+ async function sendPromptRequest ( promptFetch , options ) {
41+ const { endpoint, token, ...payload } = options ;
42+ const method = "POST" ;
43+ const headers = {
44+ accept : "application/json" ,
45+ "content-type" : "application/json; charset=UTF-8" ,
46+ "user-agent" : "copilot-extensions/preview-sdk.js" ,
47+ authorization : `Bearer ${ token } ` ,
48+ } ;
5349
54- return {
55- requestId : response . headers . get ( "x-request-id" ) ,
56- message : data . choices [ 0 ] . message ,
57- } ;
50+ const response = await promptFetch ( endpoint , {
51+ method,
52+ headers,
53+ body : JSON . stringify ( payload ) ,
54+ } ) ;
55+
56+ if ( response . ok ) {
57+ return response ;
5858 }
5959
60+ const body = await response . text ( ) ;
61+ console . log ( { body } ) ;
62+
63+ throw Object . assign (
64+ new Error (
65+ `[@copilot-extensions/preview-sdk] An error occured with the chat completions API` ,
66+ ) ,
67+ {
68+ name : "PromptError" ,
69+ request : {
70+ method : "POST" ,
71+ url : endpoint ,
72+ headers : {
73+ ...headers ,
74+ authorization : `Bearer [REDACTED]` ,
75+ } ,
76+ body : payload ,
77+ } ,
78+ response : {
79+ status : response . status ,
80+ headers : [ ...response . headers ] ,
81+ body : body ,
82+ } ,
83+ } ,
84+ ) ;
85+ }
86+ export async function prompt ( userPrompt , promptOptions ) {
87+ const [ promptFetch , options ] = parsePromptArguments (
88+ userPrompt ,
89+ promptOptions ,
90+ ) ;
91+ const response = await sendPromptRequest ( promptFetch , options ) ;
6092 const requestId = response . headers . get ( "x-request-id" ) ;
93+
94+ const data = await response . json ( ) ;
95+
6196 return {
62- requestId : requestId ,
63- message : {
64- role : "Sssistant" ,
65- content : `Sorry, an error occured with the chat completions API. (Status: ${ response . status } , request ID: ${ requestId } )` ,
66- } ,
97+ requestId,
98+ message : data . choices [ 0 ] . message ,
6799 } ;
68100}
69101
102+ prompt . stream = async function promptStream ( userPrompt , promptOptions ) {
103+ const [ promptFetch , options ] = parsePromptArguments (
104+ userPrompt ,
105+ promptOptions ,
106+ ) ;
107+ const response = await sendPromptRequest ( promptFetch , {
108+ ...options ,
109+ stream : true ,
110+ } ) ;
111+
112+ return {
113+ requestId : response . headers . get ( "x-request-id" ) ,
114+ stream : response . body ,
115+ } ;
116+ } ;
117+
70118/** @type {import('..').GetFunctionCallsInterface } */
71119export function getFunctionCalls ( payload ) {
72120 const functionCalls = payload . message . tool_calls ;
0 commit comments