@jsonstudio/llms 0.6.203 → 0.6.230
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/dist/conversion/codecs/gemini-openai-codec.js +128 -4
- package/dist/conversion/compat/actions/glm-web-search.d.ts +2 -0
- package/dist/conversion/compat/actions/glm-web-search.js +66 -0
- package/dist/conversion/compat/profiles/chat-glm.json +4 -1
- package/dist/conversion/hub/pipeline/compat/compat-pipeline-executor.js +6 -0
- package/dist/conversion/hub/pipeline/compat/compat-types.d.ts +2 -0
- package/dist/conversion/hub/pipeline/hub-pipeline.js +11 -3
- package/dist/conversion/hub/process/chat-process.js +131 -1
- package/dist/conversion/hub/response/provider-response.d.ts +22 -0
- package/dist/conversion/hub/response/provider-response.js +12 -1
- package/dist/conversion/hub/response/server-side-tools.d.ts +26 -0
- package/dist/conversion/hub/response/server-side-tools.js +326 -0
- package/dist/conversion/hub/semantic-mappers/gemini-mapper.js +118 -11
- package/dist/conversion/hub/types/standardized.d.ts +1 -0
- package/dist/conversion/responses/responses-openai-bridge.js +49 -3
- package/dist/conversion/shared/snapshot-utils.js +17 -47
- package/dist/conversion/shared/tool-mapping.js +25 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/router/virtual-router/bootstrap.js +273 -40
- package/dist/router/virtual-router/context-advisor.d.ts +0 -2
- package/dist/router/virtual-router/context-advisor.js +0 -12
- package/dist/router/virtual-router/engine.d.ts +8 -2
- package/dist/router/virtual-router/engine.js +176 -81
- package/dist/router/virtual-router/types.d.ts +21 -2
- package/dist/sse/json-to-sse/event-generators/responses.js +15 -3
- package/dist/sse/sse-to-json/gemini-sse-to-json-converter.js +27 -1
- package/dist/sse/types/gemini-types.d.ts +20 -1
- package/dist/sse/types/responses-types.js +1 -1
- package/dist/telemetry/stats-center.d.ts +73 -0
- package/dist/telemetry/stats-center.js +280 -0
- package/package.json +1 -1
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import os from 'node:os';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import fs from 'node:fs/promises';
|
|
4
|
+
function createEmptyRouterBucket() {
|
|
5
|
+
return {
|
|
6
|
+
requestCount: 0,
|
|
7
|
+
poolHitCount: {},
|
|
8
|
+
routeHitCount: {},
|
|
9
|
+
providerHitCount: {}
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function createEmptyProviderBucket() {
|
|
13
|
+
return {
|
|
14
|
+
requestCount: 0,
|
|
15
|
+
successCount: 0,
|
|
16
|
+
errorCount: 0,
|
|
17
|
+
latencySumMs: 0,
|
|
18
|
+
minLatencyMs: Number.POSITIVE_INFINITY,
|
|
19
|
+
maxLatencyMs: 0,
|
|
20
|
+
usage: {
|
|
21
|
+
promptTokens: 0,
|
|
22
|
+
completionTokens: 0,
|
|
23
|
+
totalTokens: 0
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function createEmptySnapshot() {
|
|
28
|
+
return {
|
|
29
|
+
router: {
|
|
30
|
+
global: createEmptyRouterBucket(),
|
|
31
|
+
byEntryEndpoint: {}
|
|
32
|
+
},
|
|
33
|
+
providers: {
|
|
34
|
+
global: createEmptyProviderBucket(),
|
|
35
|
+
byProviderKey: {},
|
|
36
|
+
byRoute: {},
|
|
37
|
+
byEntryEndpoint: {}
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
class NoopStatsCenter {
|
|
42
|
+
recordVirtualRouterHit() { }
|
|
43
|
+
recordProviderUsage() { }
|
|
44
|
+
getSnapshot() { return createEmptySnapshot(); }
|
|
45
|
+
async flushToDisk() { }
|
|
46
|
+
reset() { }
|
|
47
|
+
}
|
|
48
|
+
class DefaultStatsCenter {
|
|
49
|
+
snapshot = createEmptySnapshot();
|
|
50
|
+
dirty = false;
|
|
51
|
+
flushInFlight = false;
|
|
52
|
+
persistPath;
|
|
53
|
+
constructor(persistPath) {
|
|
54
|
+
if (persistPath === null) {
|
|
55
|
+
this.persistPath = null;
|
|
56
|
+
}
|
|
57
|
+
else if (typeof persistPath === 'string' && persistPath.trim().length) {
|
|
58
|
+
this.persistPath = persistPath.trim();
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
const base = path.join(os.homedir(), '.routecodex', 'stats');
|
|
62
|
+
this.persistPath = path.join(base, 'stats.json');
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
recordVirtualRouterHit(ev) {
|
|
66
|
+
if (!ev || !ev.routeName || !ev.providerKey) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const snap = this.snapshot;
|
|
70
|
+
this.applyRouterHitToBucket(snap.router.global, ev);
|
|
71
|
+
const entryKey = ev.entryEndpoint || 'unknown';
|
|
72
|
+
if (!snap.router.byEntryEndpoint[entryKey]) {
|
|
73
|
+
snap.router.byEntryEndpoint[entryKey] = createEmptyRouterBucket();
|
|
74
|
+
}
|
|
75
|
+
this.applyRouterHitToBucket(snap.router.byEntryEndpoint[entryKey], ev);
|
|
76
|
+
this.dirty = true;
|
|
77
|
+
}
|
|
78
|
+
recordProviderUsage(ev) {
|
|
79
|
+
if (!ev || !ev.providerKey || !ev.providerType) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const snap = this.snapshot;
|
|
83
|
+
this.applyProviderUsageToBucket(snap.providers.global, ev);
|
|
84
|
+
const providerKey = ev.providerKey;
|
|
85
|
+
if (!snap.providers.byProviderKey[providerKey]) {
|
|
86
|
+
snap.providers.byProviderKey[providerKey] = createEmptyProviderBucket();
|
|
87
|
+
}
|
|
88
|
+
this.applyProviderUsageToBucket(snap.providers.byProviderKey[providerKey], ev);
|
|
89
|
+
const routeKey = ev.routeName || 'unknown';
|
|
90
|
+
if (!snap.providers.byRoute[routeKey]) {
|
|
91
|
+
snap.providers.byRoute[routeKey] = createEmptyProviderBucket();
|
|
92
|
+
}
|
|
93
|
+
this.applyProviderUsageToBucket(snap.providers.byRoute[routeKey], ev);
|
|
94
|
+
const entryKey = ev.entryEndpoint || 'unknown';
|
|
95
|
+
if (!snap.providers.byEntryEndpoint[entryKey]) {
|
|
96
|
+
snap.providers.byEntryEndpoint[entryKey] = createEmptyProviderBucket();
|
|
97
|
+
}
|
|
98
|
+
this.applyProviderUsageToBucket(snap.providers.byEntryEndpoint[entryKey], ev);
|
|
99
|
+
this.dirty = true;
|
|
100
|
+
}
|
|
101
|
+
getSnapshot() {
|
|
102
|
+
return this.snapshot;
|
|
103
|
+
}
|
|
104
|
+
reset() {
|
|
105
|
+
this.snapshot = createEmptySnapshot();
|
|
106
|
+
this.dirty = false;
|
|
107
|
+
}
|
|
108
|
+
async flushToDisk() {
|
|
109
|
+
if (!this.persistPath || !this.dirty || this.flushInFlight) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
this.flushInFlight = true;
|
|
113
|
+
try {
|
|
114
|
+
const dir = path.dirname(this.persistPath);
|
|
115
|
+
await fs.mkdir(dir, { recursive: true });
|
|
116
|
+
const payload = JSON.stringify(this.snapshot, null, 2);
|
|
117
|
+
await fs.writeFile(this.persistPath, payload, 'utf-8');
|
|
118
|
+
this.dirty = false;
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
// ignore persistence errors
|
|
122
|
+
}
|
|
123
|
+
finally {
|
|
124
|
+
this.flushInFlight = false;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
applyRouterHitToBucket(bucket, ev) {
|
|
128
|
+
bucket.requestCount += 1;
|
|
129
|
+
if (ev.pool) {
|
|
130
|
+
bucket.poolHitCount[ev.pool] = (bucket.poolHitCount[ev.pool] || 0) + 1;
|
|
131
|
+
}
|
|
132
|
+
if (ev.routeName) {
|
|
133
|
+
bucket.routeHitCount[ev.routeName] = (bucket.routeHitCount[ev.routeName] || 0) + 1;
|
|
134
|
+
}
|
|
135
|
+
if (ev.providerKey) {
|
|
136
|
+
bucket.providerHitCount[ev.providerKey] = (bucket.providerHitCount[ev.providerKey] || 0) + 1;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
applyProviderUsageToBucket(bucket, ev) {
|
|
140
|
+
bucket.requestCount += 1;
|
|
141
|
+
if (ev.success) {
|
|
142
|
+
bucket.successCount += 1;
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
bucket.errorCount += 1;
|
|
146
|
+
}
|
|
147
|
+
if (Number.isFinite(ev.latencyMs) && ev.latencyMs >= 0) {
|
|
148
|
+
bucket.latencySumMs += ev.latencyMs;
|
|
149
|
+
if (ev.latencyMs < bucket.minLatencyMs) {
|
|
150
|
+
bucket.minLatencyMs = ev.latencyMs;
|
|
151
|
+
}
|
|
152
|
+
if (ev.latencyMs > bucket.maxLatencyMs) {
|
|
153
|
+
bucket.maxLatencyMs = ev.latencyMs;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (typeof ev.promptTokens === 'number' && Number.isFinite(ev.promptTokens)) {
|
|
157
|
+
bucket.usage.promptTokens += Math.max(0, ev.promptTokens);
|
|
158
|
+
}
|
|
159
|
+
if (typeof ev.completionTokens === 'number' && Number.isFinite(ev.completionTokens)) {
|
|
160
|
+
bucket.usage.completionTokens += Math.max(0, ev.completionTokens);
|
|
161
|
+
}
|
|
162
|
+
if (typeof ev.totalTokens === 'number' && Number.isFinite(ev.totalTokens)) {
|
|
163
|
+
bucket.usage.totalTokens += Math.max(0, ev.totalTokens);
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
const derivedTotal = (typeof ev.promptTokens === 'number' ? Math.max(0, ev.promptTokens) : 0) +
|
|
167
|
+
(typeof ev.completionTokens === 'number' ? Math.max(0, ev.completionTokens) : 0);
|
|
168
|
+
bucket.usage.totalTokens += derivedTotal;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
let instance = null;
|
|
173
|
+
function resolveEnableFlag(defaultValue) {
|
|
174
|
+
const raw = process.env.ROUTECODEX_STATS;
|
|
175
|
+
if (!raw)
|
|
176
|
+
return defaultValue;
|
|
177
|
+
const normalized = raw.trim().toLowerCase();
|
|
178
|
+
if (['1', 'true', 'yes', 'on'].includes(normalized))
|
|
179
|
+
return true;
|
|
180
|
+
if (['0', 'false', 'no', 'off'].includes(normalized))
|
|
181
|
+
return false;
|
|
182
|
+
return defaultValue;
|
|
183
|
+
}
|
|
184
|
+
function printStatsToConsole(snapshot) {
|
|
185
|
+
const router = snapshot.router;
|
|
186
|
+
const providers = snapshot.providers;
|
|
187
|
+
const totalRequests = router.global.requestCount;
|
|
188
|
+
const poolEntries = Object.entries(router.global.poolHitCount);
|
|
189
|
+
const providerEntries = Object.entries(router.global.providerHitCount);
|
|
190
|
+
// Router summary
|
|
191
|
+
// eslint-disable-next-line no-console
|
|
192
|
+
console.log('[stats] Virtual Router:');
|
|
193
|
+
// eslint-disable-next-line no-console
|
|
194
|
+
console.log(` total requests: ${totalRequests}`);
|
|
195
|
+
if (poolEntries.length) {
|
|
196
|
+
// eslint-disable-next-line no-console
|
|
197
|
+
console.log(' pools:');
|
|
198
|
+
for (const [pool, count] of poolEntries) {
|
|
199
|
+
const ratio = totalRequests > 0 ? (count / totalRequests) * 100 : 0;
|
|
200
|
+
// eslint-disable-next-line no-console
|
|
201
|
+
console.log(` ${pool}: ${count} (${ratio.toFixed(2)}%)`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (providerEntries.length) {
|
|
205
|
+
// eslint-disable-next-line no-console
|
|
206
|
+
console.log(' top providers:');
|
|
207
|
+
const sorted = providerEntries.sort((a, b) => b[1] - a[1]).slice(0, 5);
|
|
208
|
+
for (const [providerKey, count] of sorted) {
|
|
209
|
+
// eslint-disable-next-line no-console
|
|
210
|
+
console.log(` ${providerKey}: ${count}`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
const globalProvider = providers.global;
|
|
214
|
+
const totalProviderRequests = globalProvider.requestCount;
|
|
215
|
+
const avgLatency = globalProvider.successCount > 0 ? globalProvider.latencySumMs / globalProvider.successCount : 0;
|
|
216
|
+
// Provider summary
|
|
217
|
+
// eslint-disable-next-line no-console
|
|
218
|
+
console.log('\n[stats] Providers:');
|
|
219
|
+
// eslint-disable-next-line no-console
|
|
220
|
+
console.log(` total requests : ${totalProviderRequests} (success=${globalProvider.successCount}, error=${globalProvider.errorCount})`);
|
|
221
|
+
// eslint-disable-next-line no-console
|
|
222
|
+
console.log(` avg latency : ${avgLatency.toFixed(1)} ms`);
|
|
223
|
+
// eslint-disable-next-line no-console
|
|
224
|
+
console.log(` total tokens : prompt=${globalProvider.usage.promptTokens} completion=${globalProvider.usage.completionTokens} total=${globalProvider.usage.totalTokens}`);
|
|
225
|
+
}
|
|
226
|
+
export function initStatsCenter(options) {
|
|
227
|
+
if (instance) {
|
|
228
|
+
return instance;
|
|
229
|
+
}
|
|
230
|
+
const enabled = resolveEnableFlag(options?.enable ?? true);
|
|
231
|
+
if (!enabled) {
|
|
232
|
+
instance = new NoopStatsCenter();
|
|
233
|
+
return instance;
|
|
234
|
+
}
|
|
235
|
+
const center = new DefaultStatsCenter(options?.persistPath);
|
|
236
|
+
instance = center;
|
|
237
|
+
const autoPrint = options?.autoPrintOnExit !== false;
|
|
238
|
+
if (autoPrint && typeof process !== 'undefined' && typeof process.on === 'function') {
|
|
239
|
+
const handler = async () => {
|
|
240
|
+
try {
|
|
241
|
+
await center.flushToDisk();
|
|
242
|
+
}
|
|
243
|
+
catch {
|
|
244
|
+
// ignore
|
|
245
|
+
}
|
|
246
|
+
try {
|
|
247
|
+
const snapshot = center.getSnapshot();
|
|
248
|
+
printStatsToConsole(snapshot);
|
|
249
|
+
}
|
|
250
|
+
catch {
|
|
251
|
+
// ignore
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
try {
|
|
255
|
+
process.once('beforeExit', handler);
|
|
256
|
+
}
|
|
257
|
+
catch {
|
|
258
|
+
// ignore
|
|
259
|
+
}
|
|
260
|
+
try {
|
|
261
|
+
process.once('SIGINT', handler);
|
|
262
|
+
}
|
|
263
|
+
catch {
|
|
264
|
+
// ignore
|
|
265
|
+
}
|
|
266
|
+
try {
|
|
267
|
+
process.once('SIGTERM', handler);
|
|
268
|
+
}
|
|
269
|
+
catch {
|
|
270
|
+
// ignore
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return instance;
|
|
274
|
+
}
|
|
275
|
+
export function getStatsCenter() {
|
|
276
|
+
if (!instance) {
|
|
277
|
+
return initStatsCenter();
|
|
278
|
+
}
|
|
279
|
+
return instance;
|
|
280
|
+
}
|