@retrivora-ai/rag-engine 2.0.4 → 2.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,9 +24,11 @@ export class OllamaProvider implements ILLMProvider {
24
24
  private readonly embeddingConfig?: EmbeddingConfig;
25
25
 
26
26
  constructor(llmConfig: LLMConfig, embeddingConfig?: EmbeddingConfig) {
27
- const baseURL = llmConfig.baseUrl ?? 'http://localhost:11434';
27
+ const rawBaseURL = (llmConfig.baseUrl ?? 'http://localhost:11434').replace(/\/+$/, '');
28
+ const baseURL = rawBaseURL.replace(/\/(?:api\/v1|v1|api)$/i, '');
28
29
  const timeout = Number(llmConfig.options?.timeout) || 300_000;
29
- this.http = axios.create({ baseURL, timeout });
30
+ const headers = (llmConfig.options?.headers as Record<string, string>) || {};
31
+ this.http = axios.create({ baseURL, timeout, headers });
30
32
  this.llmConfig = llmConfig;
31
33
  this.embeddingConfig = embeddingConfig;
32
34
  }
@@ -94,6 +94,27 @@ export class UniversalLLMAdapter implements ILLMProvider {
94
94
  };
95
95
  }
96
96
 
97
+ const isSelfHost = this.baseUrl.includes('retrivora.com') || this.baseUrl.includes('localhost');
98
+ if (isSelfHost || Boolean(process.env.VERCEL)) {
99
+ try {
100
+ const _g = globalThis as any;
101
+ if (typeof _g.__retrivoraDispatchChat === 'function') {
102
+ const res = await _g.__retrivoraDispatchChat({
103
+ model: this.model,
104
+ messages: formattedMessages as any,
105
+ max_tokens: this.maxTokens,
106
+ temperature: this.temperature,
107
+ }, this.apiKey);
108
+
109
+ if (res?.response?.choices?.[0]?.message?.content) {
110
+ return res.response.choices[0].message.content;
111
+ }
112
+ }
113
+ } catch {
114
+ /* proceed to HTTP fetch */
115
+ }
116
+ }
117
+
97
118
  const { data } = await this.http.post(path, payload);
98
119
 
99
120
  const extractPath = this.opts.responseExtractPath ?? 'choices[0].message.content';
@@ -145,22 +166,52 @@ export class UniversalLLMAdapter implements ILLMProvider {
145
166
  };
146
167
  }
147
168
 
148
- const response = await fetch(url, {
149
- method: 'POST',
150
- headers: this.resolvedHeaders,
151
- body: JSON.stringify(payload),
152
- });
153
-
154
- if (!response.ok) {
155
- const errorText = await response.text().catch(() => response.statusText);
156
- throw new Error(`[UniversalLLMAdapter] Streaming request failed (${response.status}): ${errorText}`);
169
+ const isSelfHost = this.baseUrl.includes('retrivora.com') || this.baseUrl.includes('localhost');
170
+ let streamBody: ReadableStream<Uint8Array> | null = null;
171
+
172
+ if (isSelfHost || Boolean(process.env.VERCEL)) {
173
+ try {
174
+ const _g = globalThis as any;
175
+ if (typeof _g.__retrivoraDispatchChat === 'function') {
176
+ const res = await _g.__retrivoraDispatchChat({
177
+ model: this.model,
178
+ messages: formattedMessages as any,
179
+ max_tokens: this.maxTokens,
180
+ temperature: this.temperature,
181
+ stream: true,
182
+ }, this.apiKey);
183
+
184
+ if (res?.stream) {
185
+ streamBody = res.stream as ReadableStream<Uint8Array>;
186
+ } else if (res?.response?.choices?.[0]?.message?.content) {
187
+ yield res.response.choices[0].message.content;
188
+ return;
189
+ }
190
+ }
191
+ } catch {
192
+ /* proceed to HTTP fetch */
193
+ }
157
194
  }
158
195
 
159
- if (!response.body) {
160
- throw new Error('[UniversalLLMAdapter] Response body is null — server did not send a streaming response.');
196
+ if (!streamBody) {
197
+ const response = await fetch(url, {
198
+ method: 'POST',
199
+ headers: this.resolvedHeaders,
200
+ body: JSON.stringify(payload),
201
+ });
202
+
203
+ if (!response.ok) {
204
+ const errorText = await response.text().catch(() => response.statusText);
205
+ throw new Error(`[UniversalLLMAdapter] Streaming request failed (${response.status}): ${errorText}`);
206
+ }
207
+
208
+ if (!response.body) {
209
+ throw new Error('[UniversalLLMAdapter] Response body is null — server did not send a streaming response.');
210
+ }
211
+ streamBody = response.body;
161
212
  }
162
213
 
163
- const reader = response.body.getReader();
214
+ const reader = streamBody.getReader();
164
215
  const decoder = new TextDecoder('utf-8');
165
216
  let buffer = '';
166
217
 
@@ -218,6 +269,25 @@ export class UniversalLLMAdapter implements ILLMProvider {
218
269
  };
219
270
  }
220
271
 
272
+ const isSelfHost = this.baseUrl.includes('retrivora.com') || this.baseUrl.includes('localhost');
273
+ if (isSelfHost || Boolean(process.env.VERCEL)) {
274
+ try {
275
+ const _g = globalThis as any;
276
+ if (typeof _g.__retrivoraDispatchEmbedding === 'function') {
277
+ const res = await _g.__retrivoraDispatchEmbedding({
278
+ model: this.model,
279
+ input: text,
280
+ }, this.apiKey);
281
+
282
+ if (res?.data?.[0]?.embedding) {
283
+ return res.data[0].embedding;
284
+ }
285
+ }
286
+ } catch {
287
+ /* proceed to HTTP fetch */
288
+ }
289
+ }
290
+
221
291
  const { data } = await this.http.post(path, payload);
222
292
 
223
293
  const extractPath = this.opts.embedExtractPath ?? 'data[0].embedding';