forked from CortexReach/memory-lancedb-pro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm-client.ts
More file actions
424 lines (385 loc) · 13.6 KB
/
llm-client.ts
File metadata and controls
424 lines (385 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/**
* LLM Client for memory extraction and dedup decisions.
* Uses OpenAI-compatible API (reuses the embedding provider config).
*/
import OpenAI from "openai";
import {
buildOauthEndpoint,
extractOutputTextFromSse,
loadOAuthSession,
needsRefresh,
normalizeOauthModel,
refreshOAuthSession,
saveOAuthSession,
} from "./llm-oauth.js";
export interface LlmClientConfig {
apiKey?: string;
model: string;
baseURL?: string;
auth?: "api-key" | "oauth";
oauthPath?: string;
oauthProvider?: string;
timeoutMs?: number;
log?: (msg: string) => void;
/** Warn-level logger for user-visible failures (timeouts, retries, network errors). */
warnLog?: (msg: string) => void;
}
export interface LlmClient {
/** Send a prompt and parse the JSON response. Returns null on failure. */
completeJson<T>(prompt: string, label?: string): Promise<T | null>;
/** Best-effort diagnostics for the most recent failure, if any. */
getLastError(): string | null;
}
/**
* Extract JSON from an LLM response that may be wrapped in markdown fences
* or contain surrounding text.
*/
function extractJsonFromResponse(text: string): string | null {
const fenceMatch = text.match(/```(?:json)?\s*\n?([\s\S]*?)```/);
if (fenceMatch) {
return fenceMatch[1].trim();
}
const firstBrace = text.indexOf("{");
if (firstBrace === -1) return null;
let depth = 0;
let lastBrace = -1;
for (let i = firstBrace; i < text.length; i++) {
if (text[i] === "{") depth++;
else if (text[i] === "}") {
depth--;
if (depth === 0) {
lastBrace = i;
break;
}
}
}
if (lastBrace === -1) return null;
return text.substring(firstBrace, lastBrace + 1);
}
function previewText(value: string, maxLen = 200): string {
const normalized = value.replace(/\s+/g, " ").trim();
if (normalized.length <= maxLen) return normalized;
return `${normalized.slice(0, maxLen - 3)}...`;
}
function nextNonWhitespaceChar(text: string, start: number): string | undefined {
for (let i = start; i < text.length; i++) {
const ch = text[i];
if (!/\s/.test(ch)) return ch;
}
return undefined;
}
/**
* Best-effort repair for common LLM JSON issues:
* - unescaped quotes inside string values
* - raw newlines / tabs inside strings
* - trailing commas before } or ]
*/
function repairCommonJson(text: string): string {
let result = "";
let inString = false;
let escaped = false;
for (let i = 0; i < text.length; i++) {
const ch = text[i];
if (escaped) {
result += ch;
escaped = false;
continue;
}
if (inString) {
if (ch === "\\") {
result += ch;
escaped = true;
continue;
}
if (ch === "\"") {
const nextCh = nextNonWhitespaceChar(text, i + 1);
if (
nextCh === undefined ||
nextCh === "," ||
nextCh === "}" ||
nextCh === "]" ||
nextCh === ":"
) {
result += ch;
inString = false;
} else {
result += "\\\"";
}
continue;
}
if (ch === "\n") {
result += "\\n";
continue;
}
if (ch === "\r") {
result += "\\r";
continue;
}
if (ch === "\t") {
result += "\\t";
continue;
}
result += ch;
continue;
}
if (ch === "\"") {
result += ch;
inString = true;
continue;
}
if (ch === ",") {
const nextCh = nextNonWhitespaceChar(text, i + 1);
if (nextCh === "}" || nextCh === "]") {
continue;
}
}
result += ch;
}
return result;
}
function looksLikeSseResponse(bodyText: string): boolean {
const trimmed = bodyText.trimStart();
return trimmed.startsWith("event:") || trimmed.startsWith("data:");
}
function createTimeoutSignal(timeoutMs?: number): { signal: AbortSignal; dispose: () => void } {
const effectiveTimeoutMs =
typeof timeoutMs === "number" && Number.isFinite(timeoutMs) && timeoutMs > 0 ? timeoutMs : 30_000;
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), effectiveTimeoutMs);
return {
signal: controller.signal,
dispose: () => clearTimeout(timer),
};
}
function createApiKeyClient(config: LlmClientConfig, log: (msg: string) => void, warnLog?: (msg: string) => void): LlmClient {
if (!config.apiKey) {
throw new Error("LLM api-key mode requires llm.apiKey or embedding.apiKey");
}
const client = new OpenAI({
apiKey: config.apiKey,
baseURL: config.baseURL,
timeout: config.timeoutMs ?? 30000,
});
let lastError: string | null = null;
return {
async completeJson<T>(prompt: string, label = "generic"): Promise<T | null> {
lastError = null;
try {
const response = await client.chat.completions.create({
model: config.model,
messages: [
{
role: "system",
content:
"You are a memory extraction assistant. Always respond with valid JSON only.",
},
{ role: "user", content: prompt },
],
temperature: 0.1,
});
const raw = response.choices?.[0]?.message?.content;
if (!raw) {
lastError =
`memory-lancedb-pro: llm-client [${label}] empty response content from model ${config.model}`;
log(lastError);
return null;
}
if (typeof raw !== "string") {
lastError =
`memory-lancedb-pro: llm-client [${label}] non-string response content type=${Array.isArray(raw) ? "array" : typeof raw} from model ${config.model}`;
log(lastError);
return null;
}
const jsonStr = extractJsonFromResponse(raw);
if (!jsonStr) {
lastError =
`memory-lancedb-pro: llm-client [${label}] no JSON object found (chars=${raw.length}, preview=${JSON.stringify(previewText(raw))})`;
log(lastError);
return null;
}
try {
return JSON.parse(jsonStr) as T;
} catch (err) {
const repairedJsonStr = repairCommonJson(jsonStr);
if (repairedJsonStr !== jsonStr) {
try {
const repaired = JSON.parse(repairedJsonStr) as T;
log(
`memory-lancedb-pro: llm-client [${label}] recovered malformed JSON via heuristic repair (jsonChars=${jsonStr.length})`,
);
return repaired;
} catch (repairErr) {
lastError =
`memory-lancedb-pro: llm-client [${label}] JSON.parse failed: ${err instanceof Error ? err.message : String(err)}; repair failed: ${repairErr instanceof Error ? repairErr.message : String(repairErr)} (jsonChars=${jsonStr.length}, jsonPreview=${JSON.stringify(previewText(jsonStr))})`;
log(lastError);
return null;
}
}
lastError =
`memory-lancedb-pro: llm-client [${label}] JSON.parse failed: ${err instanceof Error ? err.message : String(err)} (jsonChars=${jsonStr.length}, jsonPreview=${JSON.stringify(previewText(jsonStr))})`;
log(lastError);
return null;
}
} catch (err) {
lastError =
`memory-lancedb-pro: llm-client [${label}] request failed for model ${config.model}: ${err instanceof Error ? err.message : String(err)}`;
(warnLog ?? log)(lastError);
return null;
}
},
getLastError(): string | null {
return lastError;
},
};
}
function createOauthClient(config: LlmClientConfig, log: (msg: string) => void, warnLog?: (msg: string) => void): LlmClient {
if (!config.oauthPath) {
throw new Error("LLM oauth mode requires llm.oauthPath");
}
let cachedSessionPromise: Promise<Awaited<ReturnType<typeof loadOAuthSession>>> | null = null;
let lastError: string | null = null;
async function getSession() {
if (!cachedSessionPromise) {
cachedSessionPromise = loadOAuthSession(config.oauthPath!).catch((error) => {
cachedSessionPromise = null;
throw error;
});
}
let session = await cachedSessionPromise;
if (needsRefresh(session)) {
session = await refreshOAuthSession(session, config.timeoutMs);
await saveOAuthSession(config.oauthPath!, session);
cachedSessionPromise = Promise.resolve(session);
}
return session;
}
return {
async completeJson<T>(prompt: string, label = "generic"): Promise<T | null> {
lastError = null;
try {
const session = await getSession();
const { signal, dispose } = createTimeoutSignal(config.timeoutMs);
const endpoint = buildOauthEndpoint(config.baseURL, config.oauthProvider);
try {
const response = await fetch(endpoint, {
method: "POST",
headers: {
Authorization: `Bearer ${session.accessToken}`,
"Content-Type": "application/json",
Accept: "text/event-stream",
"OpenAI-Beta": "responses=experimental",
"chatgpt-account-id": session.accountId,
originator: "codex_cli_rs",
},
signal,
body: JSON.stringify({
model: normalizeOauthModel(config.model),
instructions:
"You are a memory extraction assistant. Always respond with valid JSON only.",
input: [
{
role: "user",
content: [
{
type: "input_text",
text: prompt,
},
],
},
],
store: false,
stream: true,
text: {
format: { type: "text" },
},
}),
});
if (!response.ok) {
const detail = await response.text().catch(() => "");
throw new Error(`HTTP ${response.status} ${response.statusText}: ${detail.slice(0, 500)}`);
}
const bodyText = await response.text();
const raw = (
response.headers.get("content-type")?.includes("text/event-stream") ||
looksLikeSseResponse(bodyText)
)
? extractOutputTextFromSse(bodyText)
: (() => {
try {
const parsed = JSON.parse(bodyText) as Record<string, unknown>;
const output = Array.isArray(parsed.output) ? parsed.output : [];
const first = output.find(
(item) =>
item &&
typeof item === "object" &&
Array.isArray((item as Record<string, unknown>).content),
) as Record<string, unknown> | undefined;
if (!first) return null;
const content = (first.content as Array<Record<string, unknown>>).find(
(part) => part?.type === "output_text" && typeof part.text === "string",
);
return typeof content?.text === "string" ? content.text : null;
} catch {
return null;
}
})();
if (!raw) {
lastError =
`memory-lancedb-pro: llm-client [${label}] empty OAuth response content from model ${config.model}`;
log(lastError);
return null;
}
const jsonStr = extractJsonFromResponse(raw);
if (!jsonStr) {
lastError =
`memory-lancedb-pro: llm-client [${label}] no JSON object found in OAuth response (chars=${raw.length}, preview=${JSON.stringify(previewText(raw))})`;
log(lastError);
return null;
}
try {
return JSON.parse(jsonStr) as T;
} catch (err) {
const repairedJsonStr = repairCommonJson(jsonStr);
if (repairedJsonStr !== jsonStr) {
try {
const repaired = JSON.parse(repairedJsonStr) as T;
log(
`memory-lancedb-pro: llm-client [${label}] recovered malformed OAuth JSON via heuristic repair (jsonChars=${jsonStr.length})`,
);
return repaired;
} catch (repairErr) {
lastError =
`memory-lancedb-pro: llm-client [${label}] OAuth JSON.parse failed: ${err instanceof Error ? err.message : String(err)}; repair failed: ${repairErr instanceof Error ? repairErr.message : String(repairErr)} (jsonChars=${jsonStr.length}, jsonPreview=${JSON.stringify(previewText(jsonStr))})`;
log(lastError);
return null;
}
}
lastError =
`memory-lancedb-pro: llm-client [${label}] OAuth JSON.parse failed: ${err instanceof Error ? err.message : String(err)} (jsonChars=${jsonStr.length}, jsonPreview=${JSON.stringify(previewText(jsonStr))})`;
log(lastError);
return null;
}
} finally {
dispose();
}
} catch (err) {
lastError =
`memory-lancedb-pro: llm-client [${label}] OAuth request failed for model ${config.model}: ${err instanceof Error ? err.message : String(err)}`;
(warnLog ?? log)(lastError);
return null;
}
},
getLastError(): string | null {
return lastError;
},
};
}
export function createLlmClient(config: LlmClientConfig): LlmClient {
const log = config.log ?? (() => {});
const warnLog = config.warnLog;
if (config.auth === "oauth") {
return createOauthClient(config, log, warnLog);
}
return createApiKeyClient(config, log, warnLog);
}
export { extractJsonFromResponse, repairCommonJson };