1
1
import { Google , REQUEST_TIMEOUT_MS } from "@/app/constant" ;
2
2
import { ChatOptions , getHeaders , LLMApi , LLMModel , LLMUsage } from "../api" ;
3
3
import { useAccessStore , useAppConfig , useChatStore } from "@/app/store" ;
4
- import {
5
- EventStreamContentType ,
6
- fetchEventSource ,
7
- } from "@fortaine/fetch-event-source" ;
8
- import { prettyObject } from "@/app/utils/format" ;
9
4
import { getClientConfig } from "@/app/config/client" ;
10
- import Locale from "../../locales" ;
11
- import { getServerSideConfig } from "@/app/config/server" ;
12
- import de from "@/app/locales/de" ;
5
+ import { DEFAULT_API_HOST } from "@/app/constant" ;
13
6
export class GeminiProApi implements LLMApi {
14
7
extractMessage ( res : any ) {
15
8
console . log ( "[Response] gemini-pro response: " , res ) ;
@@ -21,7 +14,7 @@ export class GeminiProApi implements LLMApi {
21
14
) ;
22
15
}
23
16
async chat ( options : ChatOptions ) : Promise < void > {
24
- const apiClient = this ;
17
+ // const apiClient = this;
25
18
const messages = options . messages . map ( ( v ) => ( {
26
19
role : v . role . replace ( "assistant" , "model" ) . replace ( "system" , "user" ) ,
27
20
parts : [ { text : v . content } ] ,
@@ -79,20 +72,31 @@ export class GeminiProApi implements LLMApi {
79
72
] ,
80
73
} ;
81
74
82
- console . log ( "[Request] google payload: " , requestPayload ) ;
75
+ const isApp = ! ! getClientConfig ( ) ?. isApp ;
83
76
84
77
const shouldStream = ! ! options . config . stream ;
85
78
const controller = new AbortController ( ) ;
86
79
options . onController ?.( controller ) ;
80
+ const accessStore = useAccessStore . getState ( ) ;
87
81
try {
88
- const chatPath = this . path ( Google . ChatPath ) ;
82
+ let chatPath = this . path ( Google . ChatPath ) ;
83
+
84
+ // let baseUrl = accessStore.googleUrl;
85
+
86
+ chatPath = isApp
87
+ ? DEFAULT_API_HOST +
88
+ "/api/proxy/google/" +
89
+ Google . ChatPath +
90
+ `?key=${ accessStore . googleApiKey } `
91
+ : chatPath ;
92
+
89
93
const chatPayload = {
90
94
method : "POST" ,
91
95
body : JSON . stringify ( requestPayload ) ,
92
96
signal : controller . signal ,
93
97
headers : getHeaders ( ) ,
94
98
} ;
95
-
99
+ console . log ( "[Request] google chatPath: " , chatPath , isApp ) ;
96
100
// make a fetch request
97
101
const requestTimeoutId = setTimeout (
98
102
( ) => controller . abort ( ) ,
@@ -134,6 +138,8 @@ export class GeminiProApi implements LLMApi {
134
138
135
139
// start animaion
136
140
animateResponseText ( ) ;
141
+
142
+ console . log ( "[Proxy Endpoint] " , streamChatPath ) ;
137
143
fetch ( streamChatPath , chatPayload )
138
144
. then ( ( response ) => {
139
145
const reader = response ?. body ?. getReader ( ) ;
@@ -187,9 +193,7 @@ export class GeminiProApi implements LLMApi {
187
193
} else {
188
194
const res = await fetch ( chatPath , chatPayload ) ;
189
195
clearTimeout ( requestTimeoutId ) ;
190
-
191
196
const resJson = await res . json ( ) ;
192
-
193
197
if ( resJson ?. promptFeedback ?. blockReason ) {
194
198
// being blocked
195
199
options . onError ?.(
0 commit comments