1
1
// @ts -check
2
2
3
3
/** @type {import('..').PromptInterface } */
4
- export async function prompt ( userPrompt , promptOptions ) {
5
- const options = typeof userPrompt === "string" ? promptOptions : userPrompt ;
6
4
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" ;
9
13
10
14
const systemMessage = options . tools
11
15
? "You are a helpful assistant. Use the supplied tools to assist the user."
12
16
: "You are a helpful assistant." ;
17
+ const toolsChoice = options . tools ? "auto" : undefined ;
13
18
14
19
const messages = [
15
20
{
@@ -29,44 +34,87 @@ export async function prompt(userPrompt, promptOptions) {
29
34
} ) ;
30
35
}
31
36
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
+ }
50
39
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
+ } ;
53
49
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 ;
58
58
}
59
59
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 ) ;
60
92
const requestId = response . headers . get ( "x-request-id" ) ;
93
+
94
+ const data = await response . json ( ) ;
95
+
61
96
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 ,
67
99
} ;
68
100
}
69
101
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
+
70
118
/** @type {import('..').GetFunctionCallsInterface } */
71
119
export function getFunctionCalls ( payload ) {
72
120
const functionCalls = payload . message . tool_calls ;
0 commit comments