@@ -12,32 +12,39 @@ export class UnboundHandler implements ApiHandler {
1212 }
1313
1414 async * createMessage ( systemPrompt : string , messages : Anthropic . Messages . MessageParam [ ] ) : ApiStream {
15- const response = await fetch ( `${ this . unboundBaseUrl } /chat/completions` , {
16- method : "POST" ,
17- headers : {
18- Authorization : `Bearer ${ this . options . unboundApiKey } ` ,
19- "Content-Type" : "application/json" ,
20- } ,
21- body : JSON . stringify ( {
22- model : this . getModel ( ) . id ,
23- messages : [ { role : "system" , content : systemPrompt } , ...messages ] ,
24- } ) ,
25- } )
15+ try {
16+ const response = await fetch ( `${ this . unboundBaseUrl } /chat/completions` , {
17+ method : "POST" ,
18+ headers : {
19+ Authorization : `Bearer ${ this . options . unboundApiKey } ` ,
20+ "Content-Type" : "application/json" ,
21+ } ,
22+ body : JSON . stringify ( {
23+ model : this . getModel ( ) . id ,
24+ messages : [ { role : "system" , content : systemPrompt } , ...messages ] ,
25+ } ) ,
26+ } )
2627
27- if ( ! response . ok ) {
28- throw new Error ( `HTTP error! status: ${ response . status } ` )
29- }
28+ const data = await response . json ( )
3029
31- const data = await response . json ( )
30+ if ( ! response . ok ) {
31+ throw new Error ( data . error )
32+ }
3233
33- yield {
34- type : "text" ,
35- text : data . choices [ 0 ] ?. message ?. content || "" ,
36- }
37- yield {
38- type : "usage" ,
39- inputTokens : data . usage ?. prompt_tokens || 0 ,
40- outputTokens : data . usage ?. completion_tokens || 0 ,
34+ yield {
35+ type : "text" ,
36+ text : data . choices [ 0 ] ?. message ?. content || "" ,
37+ }
38+ yield {
39+ type : "usage" ,
40+ inputTokens : data . usage ?. prompt_tokens || 0 ,
41+ outputTokens : data . usage ?. completion_tokens || 0 ,
42+ }
43+ } catch ( error ) {
44+ if ( error instanceof Error ) {
45+ throw new Error ( `Unbound Gateway completion error: ${ error . message } ` )
46+ }
47+ throw error
4148 }
4249 }
4350
0 commit comments