@astratra/ai 1.2.0 → 1.4.0
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.
- package/README.md +208 -0
- package/package.json +2 -1
- package/src/agentLoop.js +88 -11
- package/src/answerText.js +117 -0
- package/src/askLimit.js +69 -0
- package/src/breakers.js +97 -0
- package/src/formatInstructions.js +99 -0
- package/src/index.d.ts +324 -0
- package/src/index.js +11 -1
- package/src/language.js +85 -0
- package/src/masking.js +279 -0
- package/src/openaiCompatible.js +170 -0
- package/src/passages.js +101 -0
- package/src/providerRouter.js +77 -11
- package/src/responseCleaner.js +337 -0
- package/src/sources.js +243 -0
package/src/providerRouter.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const { AppError } = require('@astratra/core');
|
|
2
2
|
const { randomUUID } = require('crypto');
|
|
3
|
+
const { createBreakerPool, isCircuitOpen } = require('./breakers');
|
|
3
4
|
|
|
4
5
|
const DEFAULT_COOLDOWN_MS = 60 * 1000;
|
|
5
6
|
const DEFAULT_COOLDOWN_JITTER_MS = 8 * 1000;
|
|
@@ -46,6 +47,11 @@ function createProviderRouter(config = {}) {
|
|
|
46
47
|
intentRouting: config.intentRouting || {},
|
|
47
48
|
redisKeyPrefix: config.redisKeyPrefix || 'astratra:ai:provider'
|
|
48
49
|
};
|
|
50
|
+
/* One circuit per PROVIDER, never one for all: a provider that is down must
|
|
51
|
+
not take the others with it. Optional — the per-model cooldown and
|
|
52
|
+
degradation below work without it. Resolved BEFORE any timer or
|
|
53
|
+
connection starts: a wiring error must not leave a timer behind. */
|
|
54
|
+
const breakers = resolveBreakers(config.breakers);
|
|
49
55
|
let midnightTimer = null;
|
|
50
56
|
const resetDailyAtMidnight = () => {
|
|
51
57
|
resetDailyUsage(state);
|
|
@@ -56,26 +62,42 @@ function createProviderRouter(config = {}) {
|
|
|
56
62
|
const redis = createRedisLink(config, options);
|
|
57
63
|
const redisReady = redis.connect().then(() => redis.restoreState(state)).catch(() => {});
|
|
58
64
|
|
|
65
|
+
|
|
59
66
|
async function ask(prompt, request = {}, ctx = {}) {
|
|
60
67
|
await redisReady;
|
|
61
68
|
const candidates = selectCandidates(providers, options.intentRouting, request);
|
|
62
69
|
const errors = [];
|
|
70
|
+
const masker = ctx && ctx.masker && typeof ctx.masker.mask === 'function' ? ctx.masker : null;
|
|
71
|
+
const providerCtx = masker ? withoutMasker(ctx) : ctx;
|
|
63
72
|
|
|
64
73
|
for (const candidate of candidates) {
|
|
65
74
|
const { provider, model } = candidate;
|
|
66
75
|
const key = modelKey(provider, model);
|
|
67
76
|
if (!isModelAvailable(state, key, model, request)) continue;
|
|
68
|
-
|
|
77
|
+
const reservation = await redis.reserveUsage(key, model, request);
|
|
78
|
+
if (!reservation) continue;
|
|
79
|
+
|
|
80
|
+
const undo = reserveUsage(state, key, request);
|
|
69
81
|
|
|
70
|
-
|
|
82
|
+
/* Masked at the moment it leaves: a provider that runs on this machine
|
|
83
|
+
(`external: false`) gets the text as it is. */
|
|
84
|
+
const outbound = masker && provider.external !== false;
|
|
85
|
+
const send = async () => provider.call(outbound ? await masker.maskAsync(prompt) : prompt, providerCtx, model);
|
|
71
86
|
|
|
72
87
|
try {
|
|
73
|
-
const
|
|
88
|
+
const raw = breakers ? await breakers.run(provider.id, send) : await send();
|
|
74
89
|
clearFailures(state, key);
|
|
75
90
|
redis.mirrorFailures(key, state.models[key]);
|
|
76
|
-
return
|
|
91
|
+
return outbound ? unmaskResult(masker, raw) : raw;
|
|
77
92
|
} catch (error) {
|
|
78
93
|
errors.push(error);
|
|
94
|
+
if (isCircuitOpen(error)) {
|
|
95
|
+
/* The breaker refused before anything was sent: nothing was used,
|
|
96
|
+
and the model itself did not fail. */
|
|
97
|
+
undo();
|
|
98
|
+
redis.releaseUsage(reservation);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
79
101
|
markFailure(state, key, error, options);
|
|
80
102
|
redis.mirrorFailure(key, state.models[key]);
|
|
81
103
|
}
|
|
@@ -102,7 +124,8 @@ function createProviderRouter(config = {}) {
|
|
|
102
124
|
tpd_limit: model.tpd ?? null,
|
|
103
125
|
cooldown: isUntilActive(modelState.cooldownUntil, now),
|
|
104
126
|
degraded: isUntilActive(modelState.degradedUntil, now),
|
|
105
|
-
failures: modelState.failures || 0
|
|
127
|
+
failures: modelState.failures || 0,
|
|
128
|
+
circuit: breakers ? breakers.stateOf(provider.id) || 'closed' : null
|
|
106
129
|
};
|
|
107
130
|
});
|
|
108
131
|
});
|
|
@@ -117,7 +140,29 @@ function createProviderRouter(config = {}) {
|
|
|
117
140
|
redis.disconnect();
|
|
118
141
|
}
|
|
119
142
|
|
|
120
|
-
return { ask, getStats, stop };
|
|
143
|
+
return { ask, getStats, stop, breakers };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** A pool, a factory, or nothing. */
|
|
147
|
+
function resolveBreakers(value) {
|
|
148
|
+
if (!value) return null;
|
|
149
|
+
if (typeof value === 'function') return createBreakerPool({ create: value });
|
|
150
|
+
if (typeof value.run === 'function' && typeof value.get === 'function') return value;
|
|
151
|
+
throw new Error('createProviderRouter: breakers must be a factory (providerId) => breaker or a pool from createBreakerPool().');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/* The masker is the router's business; the provider never sees it. */
|
|
155
|
+
function withoutMasker(ctx) {
|
|
156
|
+
const rest = { ...ctx };
|
|
157
|
+
delete rest.masker;
|
|
158
|
+
return rest;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function unmaskResult(masker, value) {
|
|
162
|
+
if (value && typeof value[Symbol.asyncIterator] === 'function' && typeof masker.unmaskStream === 'function') {
|
|
163
|
+
return masker.unmaskStream(value);
|
|
164
|
+
}
|
|
165
|
+
return typeof masker.unmaskDeep === 'function' ? masker.unmaskDeep(value) : value;
|
|
121
166
|
}
|
|
122
167
|
|
|
123
168
|
function createInitialState(providers) {
|
|
@@ -148,6 +193,7 @@ function createRedisLink(config, options) {
|
|
|
148
193
|
connect: async () => {},
|
|
149
194
|
restoreState: async () => {},
|
|
150
195
|
reserveUsage: async () => true,
|
|
196
|
+
releaseUsage: () => {},
|
|
151
197
|
mirrorFailure: () => {},
|
|
152
198
|
mirrorFailures: () => {},
|
|
153
199
|
disconnect: () => {}
|
|
@@ -205,6 +251,8 @@ function createRedisLink(config, options) {
|
|
|
205
251
|
|
|
206
252
|
async function reserveUsage(key, model, request) {
|
|
207
253
|
if (!client || !client.isOpen) return true;
|
|
254
|
+
const reservationId = randomUUID();
|
|
255
|
+
const tokenCost = estimatedTokens(request);
|
|
208
256
|
try {
|
|
209
257
|
const reserved = await client.eval(RESERVE_USAGE_SCRIPT, {
|
|
210
258
|
keys: [
|
|
@@ -217,19 +265,29 @@ function createRedisLink(config, options) {
|
|
|
217
265
|
String(model.rpm || 0),
|
|
218
266
|
String(model.rpd || 0),
|
|
219
267
|
String(model.tpd || 0),
|
|
220
|
-
String(
|
|
221
|
-
|
|
268
|
+
String(tokenCost),
|
|
269
|
+
reservationId,
|
|
222
270
|
String(RPM_WINDOW_MS),
|
|
223
271
|
String(Math.ceil(RPM_WINDOW_MS / 1000)),
|
|
224
272
|
String(secondsUntilMidnight())
|
|
225
273
|
]
|
|
226
274
|
});
|
|
227
|
-
return Number(reserved) === 1;
|
|
275
|
+
return Number(reserved) === 1 ? { key, reservationId, tokenCost } : false;
|
|
228
276
|
} catch (_error) {
|
|
229
277
|
return true;
|
|
230
278
|
}
|
|
231
279
|
}
|
|
232
280
|
|
|
281
|
+
/* Best effort: a reservation for a call that never left is given back. */
|
|
282
|
+
function releaseUsage(reservation) {
|
|
283
|
+
if (!reservation || reservation === true || !client || !client.isOpen) return;
|
|
284
|
+
const { key, reservationId, tokenCost } = reservation;
|
|
285
|
+
const quietly = (fn) => { try { Promise.resolve(fn()).catch(() => {}); } catch (_error) { /* never surfaces */ } };
|
|
286
|
+
quietly(() => client.zRem(stateKey('rpm', key), reservationId));
|
|
287
|
+
quietly(() => client.decrBy(dayKey('rpd', key), 1));
|
|
288
|
+
if (tokenCost) quietly(() => client.decrBy(dayKey('tpd', key), tokenCost));
|
|
289
|
+
}
|
|
290
|
+
|
|
233
291
|
function mirrorFailures(key, modelState) {
|
|
234
292
|
if (!client || !client.isOpen) return;
|
|
235
293
|
client.del(stateKey('failures', key)).catch(() => {});
|
|
@@ -256,7 +314,7 @@ function createRedisLink(config, options) {
|
|
|
256
314
|
client = null;
|
|
257
315
|
}
|
|
258
316
|
|
|
259
|
-
return { connect, restoreState, reserveUsage, mirrorFailure, mirrorFailures, disconnect };
|
|
317
|
+
return { connect, restoreState, reserveUsage, releaseUsage, mirrorFailure, mirrorFailures, disconnect };
|
|
260
318
|
}
|
|
261
319
|
|
|
262
320
|
function selectCandidates(providers, intentRouting, request) {
|
|
@@ -303,11 +361,19 @@ function isModelAvailable(state, key, model, request) {
|
|
|
303
361
|
function reserveUsage(state, key, request) {
|
|
304
362
|
const modelState = state.models[key] || createModelState();
|
|
305
363
|
const now = Date.now();
|
|
364
|
+
const tokens = estimatedTokens(request);
|
|
306
365
|
modelState.rpmWindow = modelState.rpmWindow.filter(ts => now - ts < RPM_WINDOW_MS);
|
|
307
366
|
modelState.rpmWindow.push(now);
|
|
308
367
|
modelState.rpdUsed += 1;
|
|
309
|
-
modelState.tpdUsed +=
|
|
368
|
+
modelState.tpdUsed += tokens;
|
|
310
369
|
state.models[key] = modelState;
|
|
370
|
+
/* Undo, for a call the breaker refused before it left. */
|
|
371
|
+
return () => {
|
|
372
|
+
const index = modelState.rpmWindow.lastIndexOf(now);
|
|
373
|
+
if (index !== -1) modelState.rpmWindow.splice(index, 1);
|
|
374
|
+
modelState.rpdUsed = Math.max(0, modelState.rpdUsed - 1);
|
|
375
|
+
modelState.tpdUsed = Math.max(0, modelState.tpdUsed - tokens);
|
|
376
|
+
};
|
|
311
377
|
}
|
|
312
378
|
|
|
313
379
|
function estimatedTokens(request) {
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cleaning a model's reply before a human reads it.
|
|
3
|
+
*
|
|
4
|
+
* A rule written in the system prompt is a wish: the model follows it when it
|
|
5
|
+
* suits it. Three leaks kept reaching real screens despite explicit
|
|
6
|
+
* instructions, so this cleaner removes them deterministically:
|
|
7
|
+
*
|
|
8
|
+
* 1. Raw JSON. The model answers `{"title": "...", "features": [...]}` or wraps
|
|
9
|
+
* its real answer as `{"role": "...", "response": "..."}`. An earlier
|
|
10
|
+
* converter turned JSON into "title: ...", "features: ..." — the KEYS were
|
|
11
|
+
* still on screen. A JSON key is not a word for the user, even translated,
|
|
12
|
+
* so none is ever shown: a title becomes a bold line, text a paragraph, a
|
|
13
|
+
* list bullets, a named item "**Name** — description".
|
|
14
|
+
* 2. Labels copied from a structure the model invented for itself
|
|
15
|
+
* ("**introduction** : ..."). Removed at the start of a line only — the same
|
|
16
|
+
* word inside a sentence is never touched.
|
|
17
|
+
* 3. Reasoning drafts and robotic closings ("Wait, ...", "Final answer:",
|
|
18
|
+
* "Let me know if you need anything else.").
|
|
19
|
+
*
|
|
20
|
+
* The structural part (think blocks, JSON detection, prose conversion,
|
|
21
|
+
* whitespace) is language-neutral and built in. Every WORD — which keys carry
|
|
22
|
+
* the answer, which labels to drop, which closings are robotic — belongs to a
|
|
23
|
+
* language and is supplied by the caller, per language.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const ESCAPE = /[.*+?^${}()|[\]\\]/g;
|
|
27
|
+
|
|
28
|
+
/** A vocabulary entry is a literal word (escaped) or a RegExp (its source is used as-is). */
|
|
29
|
+
function toSource(entry) {
|
|
30
|
+
if (entry instanceof RegExp) return entry.source;
|
|
31
|
+
return String(entry).replace(ESCAPE, '\\$&');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function alternation(entries) {
|
|
35
|
+
return entries.map(toSource).join('|');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const VOCABULARY_FIELDS = [
|
|
39
|
+
'payloadKeys', 'titleKeys', 'lineLabels', 'inlineLabels', 'headingLabels',
|
|
40
|
+
'reasoningLabels', 'reasoningStarters', 'finalMarkers', 'planningStarters',
|
|
41
|
+
'leadingFillers', 'annotationMarkers', 'closingPhrases', 'openers'
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
function mergeVocabularies(parts) {
|
|
45
|
+
const merged = {};
|
|
46
|
+
for (const field of VOCABULARY_FIELDS) {
|
|
47
|
+
const seen = new Set();
|
|
48
|
+
merged[field] = [];
|
|
49
|
+
for (const part of parts) {
|
|
50
|
+
for (const entry of (part && part[field]) || []) {
|
|
51
|
+
const key = entry instanceof RegExp ? `re:${entry.source}` : `s:${entry}`;
|
|
52
|
+
if (seen.has(key)) continue;
|
|
53
|
+
seen.add(key);
|
|
54
|
+
merged[field].push(entry);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return merged;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/* Lenient parse: models emit trailing commas, single quotes and unquoted keys.
|
|
62
|
+
A strict JSON.parse alone let those through to the screen untouched. */
|
|
63
|
+
function lenientParse(str) {
|
|
64
|
+
try { return JSON.parse(str); } catch (_e) { /* try the repaired form */ }
|
|
65
|
+
try {
|
|
66
|
+
const fixed = str
|
|
67
|
+
.replace(/,\s*([}\]])/g, '$1')
|
|
68
|
+
.replace(/'/g, '"')
|
|
69
|
+
// Unquoted keys, only right after { or , so text values are not rewritten.
|
|
70
|
+
.replace(/([{,]\s*)(\w+)\s*:/g, '$1"$2":')
|
|
71
|
+
.replace(/""+/g, '"');
|
|
72
|
+
return JSON.parse(fixed);
|
|
73
|
+
} catch (_e) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function inlineValue(value) {
|
|
79
|
+
if (value === null || value === undefined) return '';
|
|
80
|
+
if (typeof value !== 'object') return String(value).trim();
|
|
81
|
+
if (Array.isArray(value)) return value.map(inlineValue).filter(Boolean).join(', ');
|
|
82
|
+
return Object.values(value).map(inlineValue).filter(Boolean).join(' — ');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function makeJsonToProse(titleKeys) {
|
|
86
|
+
const titles = titleKeys.map((k) => String(k).toLowerCase());
|
|
87
|
+
|
|
88
|
+
function jsonToProse(value, depth = 0) {
|
|
89
|
+
const indent = ' '.repeat(depth);
|
|
90
|
+
if (value === null || value === undefined) return '';
|
|
91
|
+
if (typeof value !== 'object') return String(value).trim();
|
|
92
|
+
|
|
93
|
+
if (Array.isArray(value)) {
|
|
94
|
+
return value.map((item) => {
|
|
95
|
+
if (item && typeof item === 'object' && !Array.isArray(item)) {
|
|
96
|
+
const nameKey = Object.keys(item).find((k) => titles.includes(k.toLowerCase())
|
|
97
|
+
&& typeof item[k] === 'string' && item[k].trim());
|
|
98
|
+
const rest = Object.entries(item)
|
|
99
|
+
.filter(([k]) => k !== nameKey)
|
|
100
|
+
.map(([, v]) => inlineValue(v))
|
|
101
|
+
.filter(Boolean)
|
|
102
|
+
.join(' — ');
|
|
103
|
+
const head = nameKey ? `**${item[nameKey].trim()}**` : '';
|
|
104
|
+
return `${indent}- ${head && rest ? `${head} — ${rest}` : head || rest}`;
|
|
105
|
+
}
|
|
106
|
+
return `${indent}- ${inlineValue(item)}`;
|
|
107
|
+
}).filter((line) => line.trim() !== '-').join('\n');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// An object: its values in order, WITHOUT their keys. A title opens in bold.
|
|
111
|
+
const blocks = [];
|
|
112
|
+
for (const [key, v] of Object.entries(value)) {
|
|
113
|
+
if (v === null || v === undefined || v === '') continue;
|
|
114
|
+
if (typeof v !== 'object') {
|
|
115
|
+
const text = String(v).trim();
|
|
116
|
+
if (!text) continue;
|
|
117
|
+
blocks.push(titles.includes(key.toLowerCase()) ? `**${text}**` : text);
|
|
118
|
+
} else {
|
|
119
|
+
const prose = jsonToProse(v, Array.isArray(v) ? depth : depth + 1);
|
|
120
|
+
if (prose) blocks.push(prose);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return blocks.join('\n\n');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return jsonToProse;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function makeJsonConverter(vocabulary) {
|
|
130
|
+
const jsonToProse = makeJsonToProse(vocabulary.titleKeys);
|
|
131
|
+
|
|
132
|
+
/* When the model wraps its answer ({"role": "...", "response": "..."}), the
|
|
133
|
+
payload field IS the answer; listing role/name alongside it is noise. */
|
|
134
|
+
function payloadOf(parsed) {
|
|
135
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return null;
|
|
136
|
+
const key = vocabulary.payloadKeys.find((k) => typeof parsed[k] === 'string' && parsed[k].trim());
|
|
137
|
+
return key ? parsed[key] : null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function render(parsed) {
|
|
141
|
+
return payloadOf(parsed) || jsonToProse(parsed, 0);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function convertRawJson(text) {
|
|
145
|
+
let trimmed = text.trim();
|
|
146
|
+
const fence = trimmed.match(/^```(?:json)?\s*\n([\s\S]*?)\n\s*```\s*$/);
|
|
147
|
+
if (fence) trimmed = fence[1].trim();
|
|
148
|
+
|
|
149
|
+
// The whole reply is JSON — only when it has at least one quoted key, so a
|
|
150
|
+
// reply that merely starts with "[" is left alone.
|
|
151
|
+
if ((trimmed.startsWith('{') || trimmed.startsWith('[')) && /"[\wÀ-ÿ_]+"\s*:/.test(trimmed)) {
|
|
152
|
+
const parsed = lenientParse(trimmed);
|
|
153
|
+
if (parsed) return render(parsed) || text;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// A short preamble, then a JSON block.
|
|
157
|
+
const jsonStart = trimmed.search(/\n\s*[{[]/);
|
|
158
|
+
if (jsonStart > 0 && jsonStart < 200) {
|
|
159
|
+
const preamble = trimmed.slice(0, jsonStart).trim();
|
|
160
|
+
const parsed = lenientParse(trimmed.slice(jsonStart).trim());
|
|
161
|
+
if (parsed) {
|
|
162
|
+
const body = render(parsed);
|
|
163
|
+
if (body) return (preamble ? `${preamble}\n\n` : '') + body;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Unparseable but clearly structured (3+ quoted keys): keep the values only.
|
|
168
|
+
// Keys are erased here too — this path used to be the last place they leaked.
|
|
169
|
+
const quotedKeys = (trimmed.match(/"[\wÀ-ÿ_]+"\s*:/g) || []).length;
|
|
170
|
+
if (quotedKeys >= 3 && /^\s*\{/.test(trimmed)) {
|
|
171
|
+
return trimmed
|
|
172
|
+
.replace(/^\s*\{/, '').replace(/\}\s*$/, '')
|
|
173
|
+
.replace(/"([\wÀ-ÿ_ ]+)"\s*:\s*/g, '')
|
|
174
|
+
.replace(/[[\]{}]/g, '')
|
|
175
|
+
.replace(/",?\s*/g, '\n')
|
|
176
|
+
.replace(/"/g, '')
|
|
177
|
+
.replace(/\n{3,}/g, '\n\n')
|
|
178
|
+
.trim();
|
|
179
|
+
}
|
|
180
|
+
return text;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/* JSON fragments pasted in the middle of prose (a tool result copied back). */
|
|
184
|
+
function convertInlineJson(text) {
|
|
185
|
+
return text.replace(
|
|
186
|
+
/\{(?:\s*"[\wÀ-ÿ_]+"\s*:\s*(?:"[^"]*"|[\d.]+|true|false|null|\[.*?\]|\{[^}]*\})\s*,?\s*){2,}\}/g,
|
|
187
|
+
(match) => {
|
|
188
|
+
const parsed = lenientParse(match);
|
|
189
|
+
if (!parsed) return match;
|
|
190
|
+
return render(parsed) || match;
|
|
191
|
+
}
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return { convertRawJson, convertInlineJson, jsonToProse };
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function compile(vocabulary) {
|
|
199
|
+
const v = vocabulary;
|
|
200
|
+
const any = (list) => list.length > 0;
|
|
201
|
+
const rx = {};
|
|
202
|
+
|
|
203
|
+
if (any(v.payloadKeys)) {
|
|
204
|
+
rx.payloadLabel = new RegExp(`^\\s*(?:\\*\\*)?(?:${alternation(v.payloadKeys)})(?:\\*\\*)?\\s*[:\\-—]\\s*`, 'i');
|
|
205
|
+
}
|
|
206
|
+
if (any(v.inlineLabels)) {
|
|
207
|
+
rx.inlineLabels = new RegExp(`(?:^|\\s)(?:\\*\\*)?(?:${alternation(v.inlineLabels)})(?:\\*\\*)?\\s*[:\\-—]\\s*`, 'gi');
|
|
208
|
+
}
|
|
209
|
+
if (any(v.lineLabels)) {
|
|
210
|
+
// Replaced by a paragraph break: the content stays, split as it was.
|
|
211
|
+
rx.lineLabels = new RegExp(`(^|\\n)[ \\t]*(?:\\*\\*)?(?:${alternation(v.lineLabels)})(?:\\*\\*)?[ \\t]*:[ \\t]*`, 'gi');
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/* Drafts chain corrections with no fixed marker ("Wait, ...", "Actually, ...",
|
|
215
|
+
"Final answer:"). Patching word by word never ends, so the LAST line that
|
|
216
|
+
looks like reasoning is found and only what follows it is kept: the model
|
|
217
|
+
writes its last meta comment right before the real answer. */
|
|
218
|
+
const boundary = [];
|
|
219
|
+
if (any(v.finalMarkers)) {
|
|
220
|
+
// The answer often follows on the same line after the colon: consume up to ':' only.
|
|
221
|
+
boundary.push(`^[ \\t]*(?:${alternation(v.finalMarkers)})\\s*(?:\\([^)]*\\))?\\s*[:\\-—]`);
|
|
222
|
+
}
|
|
223
|
+
const labelBlock = any(v.reasoningLabels)
|
|
224
|
+
? `^[ \\t]*\\(?(?:${alternation(v.reasoningLabels)})(?:[\\s-]+\\w+){0,6}(?:\\s*\\([^)]*\\))?\\)?\\s*[:\\-—]`
|
|
225
|
+
: null;
|
|
226
|
+
if (labelBlock) boundary.push(`${labelBlock}.*$`);
|
|
227
|
+
if (any(v.reasoningStarters)) boundary.push(`^[ \\t]*(?:${alternation(v.reasoningStarters)})\\b.*$`);
|
|
228
|
+
if (boundary.length) rx.boundary = new RegExp(boundary.join('|'), 'gim');
|
|
229
|
+
// Safety net: a labelled block still left (label + paragraph up to a blank line).
|
|
230
|
+
if (labelBlock) rx.labelledBlock = new RegExp(`${labelBlock}.*(?:\\n(?!\\s*\\n).*)*`, 'gim');
|
|
231
|
+
|
|
232
|
+
if (any(v.planningStarters)) {
|
|
233
|
+
rx.planning = new RegExp(`^[\\s]*[*\\-•]\\s*(?:${alternation(v.planningStarters)}).*$`, 'gim');
|
|
234
|
+
}
|
|
235
|
+
if (any(v.leadingFillers)) {
|
|
236
|
+
// Punctuation is required after the filler: "Bon" must not eat "Bonjour".
|
|
237
|
+
rx.filler = new RegExp(`^(?:${alternation(v.leadingFillers)})\\s*[.,!]+\\s*`, 'i');
|
|
238
|
+
}
|
|
239
|
+
if (any(v.annotationMarkers)) {
|
|
240
|
+
rx.annotation = new RegExp(`^.*\\((?:${alternation(v.annotationMarkers)}).*\\).*$`, 'gim');
|
|
241
|
+
}
|
|
242
|
+
if (any(v.headingLabels)) {
|
|
243
|
+
/* Bold may wrap the label, and the colon is absent when the label holds its
|
|
244
|
+
own line: an expression requiring "^label:" saw neither case. */
|
|
245
|
+
const h = alternation(v.headingLabels);
|
|
246
|
+
rx.headingAlone = new RegExp(`^\\s*(?:\\*\\*)?\\s*(?:${h})\\s*(?:\\*\\*)?\\s*:?\\s*(?:\\*\\*)?\\s*$`, 'gim');
|
|
247
|
+
rx.headingLead = new RegExp(`^\\s*(?:\\*\\*)?\\s*(?:${h})\\s*(?:\\*\\*)?\\s*:\\s*`, 'gim');
|
|
248
|
+
}
|
|
249
|
+
if (any(v.closingPhrases)) {
|
|
250
|
+
rx.closing = new RegExp(`\\s*(?:${alternation(v.closingPhrases)})\\s*$`, 'i');
|
|
251
|
+
}
|
|
252
|
+
if (any(v.openers)) {
|
|
253
|
+
rx.openers = new RegExp(`(?:^|\\n)\\s*(?:${alternation(v.openers)})\\s*[.!?]?\\s*`, 'gi');
|
|
254
|
+
}
|
|
255
|
+
return rx;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* @param {object} [options]
|
|
260
|
+
* @param {object} [options.shared] vocabulary applied whatever the language
|
|
261
|
+
* (JSON keys are usually English whatever language the user speaks).
|
|
262
|
+
* @param {Record<string, object>} [options.languages] vocabulary per language.
|
|
263
|
+
* @param {string} [options.fallbackLanguage] used when clean() gets an unknown language.
|
|
264
|
+
* @param {number} [options.maxClosingPasses=3] successive robotic closings removed.
|
|
265
|
+
*/
|
|
266
|
+
function createResponseCleaner(options = {}) {
|
|
267
|
+
const shared = options.shared || {};
|
|
268
|
+
const languages = options.languages || {};
|
|
269
|
+
const fallbackLanguage = options.fallbackLanguage || null;
|
|
270
|
+
const maxClosingPasses = options.maxClosingPasses ?? 3;
|
|
271
|
+
const cache = new Map();
|
|
272
|
+
|
|
273
|
+
function forLanguage(language) {
|
|
274
|
+
const lang = language && languages[language] ? language : fallbackLanguage;
|
|
275
|
+
const key = lang || '';
|
|
276
|
+
if (!cache.has(key)) {
|
|
277
|
+
const vocabulary = mergeVocabularies([shared, lang ? languages[lang] : null]);
|
|
278
|
+
cache.set(key, { vocabulary, rx: compile(vocabulary), json: makeJsonConverter(vocabulary) });
|
|
279
|
+
}
|
|
280
|
+
return cache.get(key);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function clean(text, cleanOptions = {}) {
|
|
284
|
+
if (text === null || text === undefined) return '';
|
|
285
|
+
const { rx, json } = forLanguage(cleanOptions.language);
|
|
286
|
+
let out = String(text);
|
|
287
|
+
|
|
288
|
+
// Reasoning blocks, closed or cut off mid-stream.
|
|
289
|
+
out = out.replace(/<think>[\s\S]*?<\/think>/gi, '');
|
|
290
|
+
out = out.replace(/<think>[\s\S]*/gi, '');
|
|
291
|
+
|
|
292
|
+
out = json.convertRawJson(out);
|
|
293
|
+
if (rx.payloadLabel) out = out.replace(rx.payloadLabel, '');
|
|
294
|
+
if (rx.inlineLabels) out = out.replace(rx.inlineLabels, ' ');
|
|
295
|
+
if (rx.lineLabels) out = out.replace(rx.lineLabels, '$1\n');
|
|
296
|
+
out = json.convertInlineJson(out);
|
|
297
|
+
|
|
298
|
+
if (rx.boundary) {
|
|
299
|
+
const matches = [...out.matchAll(rx.boundary)];
|
|
300
|
+
if (matches.length) {
|
|
301
|
+
const last = matches[matches.length - 1];
|
|
302
|
+
out = out.slice(last.index + last[0].length);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
if (rx.labelledBlock) out = out.replace(rx.labelledBlock, '');
|
|
306
|
+
if (rx.planning) out = out.replace(rx.planning, '');
|
|
307
|
+
if (rx.filler) out = out.replace(rx.filler, '');
|
|
308
|
+
if (rx.annotation) out = out.replace(rx.annotation, '');
|
|
309
|
+
if (rx.headingAlone) out = out.replace(rx.headingAlone, '');
|
|
310
|
+
if (rx.headingLead) out = out.replace(rx.headingLead, '');
|
|
311
|
+
|
|
312
|
+
/* Closings stack ("Need anything else? Don't hesitate to ask."): one pass
|
|
313
|
+
left the first one standing. Bounded, so a reply made only of phrases
|
|
314
|
+
that look robotic cannot be eaten whole by a loop. */
|
|
315
|
+
if (rx.closing) {
|
|
316
|
+
for (let i = 0; i < maxClosingPasses; i += 1) {
|
|
317
|
+
const next = out.replace(rx.closing, '');
|
|
318
|
+
if (next === out) break;
|
|
319
|
+
out = next;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
if (rx.openers) out = out.replace(rx.openers, '\n');
|
|
323
|
+
|
|
324
|
+
out = out.replace(/\n{3,}/g, '\n\n');
|
|
325
|
+
out = out.replace(/[ \t]{2,}/g, ' ');
|
|
326
|
+
return out.trim();
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return {
|
|
330
|
+
clean,
|
|
331
|
+
/** Converts a parsed JSON value to prose without any key (for tool results shown as text). */
|
|
332
|
+
jsonToProse: (value, language) => forLanguage(language).json.jsonToProse(value, 0),
|
|
333
|
+
languages: Object.keys(languages)
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
module.exports = { createResponseCleaner };
|