1
1
import type { Client } from "gel" ;
2
2
import { EventSourceParserStream } from "eventsource-parser/stream" ;
3
3
4
- import type { ResolvedConnectConfig } from "gel/dist/conUtils.js" ;
5
4
import {
6
5
getAuthenticatedFetch ,
7
6
type AuthenticatedFetch ,
@@ -46,9 +45,7 @@ export class RAGClient {
46
45
}
47
46
48
47
private static async getAuthenticatedFetch ( client : Client ) {
49
- const connectConfig : ResolvedConnectConfig = (
50
- await ( client as any ) . pool . _getNormalizedConnectConfig ( )
51
- ) . connectionParams ;
48
+ const connectConfig = await client . resolveConnectionParams ( ) ;
52
49
53
50
return getAuthenticatedFetch ( connectConfig , httpSCRAMAuth , "ext/ai/" ) ;
54
51
}
@@ -110,7 +107,7 @@ export class RAGClient {
110
107
! providedPrompt && {
111
108
name : "builtin::rag-default" ,
112
109
} ) ,
113
- custom : [ ...( this . options . prompt ?. custom || [ ] ) , ...messages ] ,
110
+ custom : [ ...( this . options . prompt ?. custom ?? [ ] ) , ...messages ] ,
114
111
} ,
115
112
query : [ ...messages ] . reverse ( ) . find ( ( msg ) => msg . role === "user" ) !
116
113
. content [ 0 ] . text ,
@@ -139,18 +136,9 @@ export class RAGClient {
139
136
) ;
140
137
}
141
138
142
- const data = await res . json ( ) ;
139
+ const data : unknown = await res . json ( ) ;
143
140
144
- if (
145
- ! data ||
146
- typeof data !== "object" ||
147
- typeof data . response !== "string"
148
- ) {
149
- throw new Error (
150
- "Expected response to be an object with response key of type string" ,
151
- ) ;
152
- }
153
- return data . response ;
141
+ return parseResponse ( data ) ;
154
142
}
155
143
156
144
streamRag (
@@ -190,6 +178,7 @@ export class RAGClient {
190
178
}
191
179
} ,
192
180
then < TResult1 = Response , TResult2 = never > (
181
+ /* eslint-disable @typescript-eslint/no-duplicate-type-constituents */
193
182
onfulfilled ?:
194
183
| ( ( value : Response ) => TResult1 | PromiseLike < TResult1 > )
195
184
| undefined
@@ -198,6 +187,7 @@ export class RAGClient {
198
187
| ( ( reason : any ) => TResult2 | PromiseLike < TResult2 > )
199
188
| undefined
200
189
| null ,
190
+ /* eslint-enable @typescript-eslint/no-duplicate-type-constituents */
201
191
) : Promise < TResult1 | TResult2 > {
202
192
return fetchRag (
203
193
{
@@ -225,7 +215,43 @@ export class RAGClient {
225
215
await handleResponseError ( response ) ;
226
216
}
227
217
228
- const data : { data : { embedding : number [ ] } [ ] } = await response . json ( ) ;
229
- return data . data [ 0 ] . embedding ;
218
+ const data : unknown = await response . json ( ) ;
219
+ return parseEmbeddingResponse ( data ) ;
220
+ }
221
+ }
222
+
223
+ function parseResponse ( data : unknown ) : string {
224
+ if (
225
+ typeof data === "object" &&
226
+ data != null &&
227
+ "response" in data &&
228
+ typeof data . response === "string"
229
+ ) {
230
+ return data . response ;
231
+ }
232
+
233
+ throw new Error (
234
+ "Expected response to be an object with response key of type string" ,
235
+ ) ;
236
+ }
237
+
238
+ function parseEmbeddingResponse ( responseData : unknown ) : number [ ] {
239
+ if (
240
+ typeof responseData === "object" &&
241
+ responseData != null &&
242
+ "data" in responseData &&
243
+ Array . isArray ( responseData . data )
244
+ ) {
245
+ const firstItem : unknown = responseData . data [ 0 ] ;
246
+ if (
247
+ typeof firstItem === "object" &&
248
+ firstItem != null &&
249
+ "embedding" in firstItem
250
+ ) {
251
+ return firstItem . embedding as number [ ] ;
252
+ }
230
253
}
254
+ throw new Error (
255
+ "Expected response to be an object with data key of type array of objects with embedding key of type number[]" ,
256
+ ) ;
231
257
}
0 commit comments