@qmediat.io/gemini-code-context-mcp 1.2.0 → 1.3.1
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/CHANGELOG.md +63 -0
- package/dist/config.d.ts +12 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +8 -0
- package/dist/config.js.map +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +6 -0
- package/dist/server.js.map +1 -1
- package/dist/tools/ask.tool.d.ts.map +1 -1
- package/dist/tools/ask.tool.js +84 -1
- package/dist/tools/ask.tool.js.map +1 -1
- package/dist/tools/code.tool.d.ts.map +1 -1
- package/dist/tools/code.tool.js +57 -1
- package/dist/tools/code.tool.js.map +1 -1
- package/dist/tools/registry.d.ts +41 -2
- package/dist/tools/registry.d.ts.map +1 -1
- package/dist/tools/registry.js +27 -1
- package/dist/tools/registry.js.map +1 -1
- package/dist/tools/shared/throttle.d.ts +122 -0
- package/dist/tools/shared/throttle.d.ts.map +1 -0
- package/dist/tools/shared/throttle.js +410 -0
- package/dist/tools/shared/throttle.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-side TPM (tokens-per-minute) throttle for Gemini API calls.
|
|
3
|
+
*
|
|
4
|
+
* Google enforces a per-minute input-token quota on paid Tier 1 Gemini 3 Pro
|
|
5
|
+
* at 100_000 tokens/minute/model/project (per AI Studio dashboard, subject
|
|
6
|
+
* to change). Without a client-side preflight we discover the limit only
|
|
7
|
+
* when Gemini returns 429 `RESOURCE_EXHAUSTED` — by which point the call
|
|
8
|
+
* is already billed (per `@google/genai@1.50.x` genai.d.ts: "AbortSignal
|
|
9
|
+
* is a client-only operation … you will still be charged").
|
|
10
|
+
*
|
|
11
|
+
* The module keeps an in-memory sliding 60-second window of recent input-
|
|
12
|
+
* token usage per resolved model. The primary API is a three-method
|
|
13
|
+
* reservation lifecycle — `reserve` / `release` / `cancel` — because a
|
|
14
|
+
* read-only "peek" API (the obvious design) has a TOCTOU race: between
|
|
15
|
+
* `peek()` → `await sleep()` → `await generateContent()` → `record()`, a
|
|
16
|
+
* concurrent MCP tool call observes the same pre-peek window and both
|
|
17
|
+
* callers proceed, collectively overshooting the quota.
|
|
18
|
+
*
|
|
19
|
+
* `reserve` solves the race by inserting a provisional `WindowEntry` with
|
|
20
|
+
* the caller's ESTIMATE at the moment the reservation is taken (plus the
|
|
21
|
+
* computed delay) — so subsequent concurrent `reserve` calls see the
|
|
22
|
+
* provisional entry and back off. When the call completes, `release`
|
|
23
|
+
* overwrites the estimate with actual token usage; on failure, `cancel`
|
|
24
|
+
* removes the provisional entry so it doesn't keep blocking future calls.
|
|
25
|
+
*
|
|
26
|
+
* Multi-entry eviction math: when the window is over-limit, we find the
|
|
27
|
+
* smallest index `k` such that evicting `entries[0..k]` drops the remaining
|
|
28
|
+
* sum enough to admit the new estimate. Waiting for JUST the oldest entry
|
|
29
|
+
* to expire (the naïve design this module replaced) under-delays whenever
|
|
30
|
+
* two or more large entries are still inside the window after that oldest
|
|
31
|
+
* one ages out.
|
|
32
|
+
*
|
|
33
|
+
* Sorted-array invariant: `entries[model]` is always kept sorted ascending
|
|
34
|
+
* by `tsMs`. `reserve` uses binary-search insertion (not `push`) because
|
|
35
|
+
* future-dated provisionals (hint-driven or window-eviction-driven delay)
|
|
36
|
+
* can produce tsMs values that are out-of-order relative to a subsequent
|
|
37
|
+
* `delayMs = 0` reservation. Without sorted-insert, `prune`'s head-only
|
|
38
|
+
* fast-path silently skips expired entries buried mid-array, causing
|
|
39
|
+
* over-throttle; and `computeWindowDelay`'s oldest-first eviction picks
|
|
40
|
+
* the wrong `entries[k].tsMs` to wait for. Empirically demonstrated during
|
|
41
|
+
* PR #19 code review (Copilot + Grok + Gemini independently flagged).
|
|
42
|
+
*
|
|
43
|
+
* Retry-hint extend-only: `recordRetryHint` keeps the LONGER of existing
|
|
44
|
+
* and new expiry. Allowing a shorter hint to replace a longer one would
|
|
45
|
+
* let a future reserve compute a smaller `tsMs` than an entry previously
|
|
46
|
+
* appended under the longer hint — same ordering break as above via a
|
|
47
|
+
* different trigger path.
|
|
48
|
+
*
|
|
49
|
+
* Deliberate non-goals (tracked in docs/FOLLOW-UP-PRS.md T22 follow-ups):
|
|
50
|
+
* - Cross-process coordination. State is per-server-process, not shared
|
|
51
|
+
* across multiple MCP instances keyed on the same API key.
|
|
52
|
+
* - Persistence across restarts. The window is rebuilt from zero each
|
|
53
|
+
* server boot; a rapid restart during heavy usage can briefly exceed
|
|
54
|
+
* the limit until the window saturates again. Acceptable: startup is
|
|
55
|
+
* rare and the first 429 would re-teach us via `recordRetryHint`.
|
|
56
|
+
* - Server-side cancellation of in-flight Gemini calls. The throttle is
|
|
57
|
+
* preflight-only; it never aborts a `generateContent` in progress.
|
|
58
|
+
*/
|
|
59
|
+
const WINDOW_MS = 60_000;
|
|
60
|
+
/**
|
|
61
|
+
* Randomised jitter padded onto computed wait times so a cluster of
|
|
62
|
+
* concurrent waiters evicting the same entry don't all compute the
|
|
63
|
+
* identical wait and wake at the same millisecond — which would re-create
|
|
64
|
+
* the burst they were trying to avoid. Range picked for a human-noticeable
|
|
65
|
+
* spread (≥1 s) without inflating typical waits excessively (≤3 s).
|
|
66
|
+
*
|
|
67
|
+
* `computeJitterMs` is a function (not a constant) because `Math.random()`
|
|
68
|
+
* MUST be evaluated per-caller — a module-scope `const` would be
|
|
69
|
+
* deterministic, which is exactly the thundering-herd bug Gemini flagged
|
|
70
|
+
* during PR #19 review.
|
|
71
|
+
*/
|
|
72
|
+
const JITTER_MIN_MS = 1_000;
|
|
73
|
+
const JITTER_MAX_MS = 3_000;
|
|
74
|
+
/**
|
|
75
|
+
* Uniformly distributed integer in `[JITTER_MIN_MS, JITTER_MAX_MS]` —
|
|
76
|
+
* BOTH endpoints inclusive. The `+ 1` in the multiplier width ensures
|
|
77
|
+
* `JITTER_MAX_MS` is reachable (without it, `Math.floor` clamps to
|
|
78
|
+
* `MAX - 1`, producing the `[1000, 2999]` off-by-one Copilot flagged in
|
|
79
|
+
* the PR #19 round-2 review).
|
|
80
|
+
*/
|
|
81
|
+
function computeJitterMs() {
|
|
82
|
+
return JITTER_MIN_MS + Math.floor(Math.random() * (JITTER_MAX_MS - JITTER_MIN_MS + 1));
|
|
83
|
+
}
|
|
84
|
+
export function createTpmThrottle(limitTokensPerMinute) {
|
|
85
|
+
if (!Number.isFinite(limitTokensPerMinute) || limitTokensPerMinute < 0) {
|
|
86
|
+
throw new Error(`createTpmThrottle: limit must be a non-negative finite number, got ${limitTokensPerMinute}`);
|
|
87
|
+
}
|
|
88
|
+
const disabled = limitTokensPerMinute === 0;
|
|
89
|
+
const windows = new Map();
|
|
90
|
+
const hints = new Map();
|
|
91
|
+
const reservationIndex = new Map();
|
|
92
|
+
let nextReservationId = 1;
|
|
93
|
+
/**
|
|
94
|
+
* Highest `nowMs` ever observed from a public-method call. Used to clamp
|
|
95
|
+
* backwards-jumping clocks (NTP correction, VM suspend/resume) — when
|
|
96
|
+
* the caller supplies a `nowMs` smaller than what we already know to have
|
|
97
|
+
* happened, we pin to our floor. Tracked separately from `entry.tsMs`
|
|
98
|
+
* because entries can be FUTURE-dated (a provisional reservation with
|
|
99
|
+
* `delayMs > 0` inserts at `now + delayMs`), and using those to clamp
|
|
100
|
+
* would pin "now" past the current real-time into scheduled-call time —
|
|
101
|
+
* which breaks multi-entry eviction math by making all past entries look
|
|
102
|
+
* like they've already aged out.
|
|
103
|
+
*/
|
|
104
|
+
let lastObservedNowMs = Number.NEGATIVE_INFINITY;
|
|
105
|
+
function effectiveNow(nowMs) {
|
|
106
|
+
// Guard against non-finite caller input (NaN, ±Infinity). Consistent with
|
|
107
|
+
// `sanitizeTokens`' "coerce rather than crash/deadlock" philosophy:
|
|
108
|
+
// passing `Number.NEGATIVE_INFINITY` would otherwise poison
|
|
109
|
+
// `lastObservedNowMs` and cause every subsequent call to compute
|
|
110
|
+
// delays of +Infinity. Fall back to `Date.now()` — the same value
|
|
111
|
+
// the public-API defaults use when `nowMs` is omitted.
|
|
112
|
+
const candidate = Number.isFinite(nowMs) ? nowMs : Date.now();
|
|
113
|
+
const clamped = candidate > lastObservedNowMs ? candidate : lastObservedNowMs;
|
|
114
|
+
lastObservedNowMs = clamped;
|
|
115
|
+
return clamped;
|
|
116
|
+
}
|
|
117
|
+
function prune(model, nowMs) {
|
|
118
|
+
const existing = windows.get(model);
|
|
119
|
+
if (!existing || existing.length === 0)
|
|
120
|
+
return [];
|
|
121
|
+
const cutoff = nowMs - WINDOW_MS;
|
|
122
|
+
// Fast path: nothing expired → return the array as-is without allocating.
|
|
123
|
+
// Entries are append-sorted by tsMs (chronological), so the head is oldest.
|
|
124
|
+
const first = existing[0];
|
|
125
|
+
if (first && first.tsMs > cutoff)
|
|
126
|
+
return existing;
|
|
127
|
+
const kept = [];
|
|
128
|
+
for (const e of existing) {
|
|
129
|
+
if (e.tsMs > cutoff) {
|
|
130
|
+
kept.push(e);
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
// Drop expired reservations from the ID index too — otherwise a late
|
|
134
|
+
// release/cancel would silently resurrect or mutate a stale entry.
|
|
135
|
+
reservationIndex.delete(e.id);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (kept.length === 0) {
|
|
139
|
+
windows.delete(model);
|
|
140
|
+
return [];
|
|
141
|
+
}
|
|
142
|
+
windows.set(model, kept);
|
|
143
|
+
return kept;
|
|
144
|
+
}
|
|
145
|
+
function activeHint(model, nowMs) {
|
|
146
|
+
const hint = hints.get(model);
|
|
147
|
+
if (!hint)
|
|
148
|
+
return null;
|
|
149
|
+
if (hint.expiresAtMs <= nowMs) {
|
|
150
|
+
hints.delete(model);
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
return hint;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Compute the PURE (un-jittered) delay needed before a call estimated at
|
|
157
|
+
* `estimate` tokens can fit under `limitTokensPerMinute`, given the
|
|
158
|
+
* current `entries`.
|
|
159
|
+
*
|
|
160
|
+
* Returns `0` when the call fits immediately. Returns `0` when `estimate`
|
|
161
|
+
* alone exceeds the limit — sleeping forever would deadlock the tool;
|
|
162
|
+
* Gemini will either accept (actual < estimate) or 429, at which point
|
|
163
|
+
* `recordRetryHint` seeds the hint path.
|
|
164
|
+
*
|
|
165
|
+
* Otherwise: find the smallest `k` such that evicting `entries[0..k]`
|
|
166
|
+
* leaves `sum(entries[k+1..]) + estimate <= limit`, and wait just long
|
|
167
|
+
* enough for `entries[k]` to age out of the 60s window.
|
|
168
|
+
*
|
|
169
|
+
* Jitter is NOT applied here — that's the caller's responsibility. See
|
|
170
|
+
* `computeWindowDelay` for the jittered variant used by `reserve`, and
|
|
171
|
+
* `shouldDelay` for the un-jittered peek used by diagnostics.
|
|
172
|
+
*/
|
|
173
|
+
function computeWindowDelayPure(entries, estimate, nowMs) {
|
|
174
|
+
let sum = 0;
|
|
175
|
+
for (const e of entries)
|
|
176
|
+
sum += e.tokens;
|
|
177
|
+
if (sum + estimate <= limitTokensPerMinute)
|
|
178
|
+
return 0;
|
|
179
|
+
if (estimate >= limitTokensPerMinute)
|
|
180
|
+
return 0;
|
|
181
|
+
// Iterate oldest-first, evict one entry at a time, stop when remaining
|
|
182
|
+
// tail + estimate fits. Since entries are chronological (tsMs ascending),
|
|
183
|
+
// evicting `entries[k]` also evicts everything older (they're already
|
|
184
|
+
// past their 60s age when `entries[k]` is).
|
|
185
|
+
let remaining = sum;
|
|
186
|
+
for (let k = 0; k < entries.length; k++) {
|
|
187
|
+
const e = entries[k];
|
|
188
|
+
if (!e)
|
|
189
|
+
break; // TS narrowing, unreachable given loop bound
|
|
190
|
+
remaining -= e.tokens;
|
|
191
|
+
if (remaining + estimate <= limitTokensPerMinute) {
|
|
192
|
+
const wait = e.tsMs + WINDOW_MS - nowMs;
|
|
193
|
+
return Math.max(0, wait);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// Unreachable in practice: `estimate < limit` (checked above) means
|
|
197
|
+
// evicting ALL entries (remaining=0) always fits. Return 0 defensively.
|
|
198
|
+
return 0;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Jittered variant of `computeWindowDelayPure`. Used by `reserve` so that
|
|
202
|
+
* concurrent waiters evicting the same entry wake at slightly different
|
|
203
|
+
* times instead of thundering back into generateContent simultaneously.
|
|
204
|
+
* Pure delay of `0` stays `0` — jitter only applies when there's actual
|
|
205
|
+
* waiting to do.
|
|
206
|
+
*/
|
|
207
|
+
function computeWindowDelay(entries, estimate, nowMs) {
|
|
208
|
+
const pure = computeWindowDelayPure(entries, estimate, nowMs);
|
|
209
|
+
return pure === 0 ? 0 : pure + computeJitterMs();
|
|
210
|
+
}
|
|
211
|
+
return {
|
|
212
|
+
reserve(model, estimatedInputTokens, nowMs = Date.now()) {
|
|
213
|
+
if (disabled) {
|
|
214
|
+
// Return a no-op reservation that cancel/release will accept. We
|
|
215
|
+
// don't index it — lookups short-circuit on the disabled path.
|
|
216
|
+
return { delayMs: 0, releaseId: -1 };
|
|
217
|
+
}
|
|
218
|
+
const now = effectiveNow(nowMs);
|
|
219
|
+
const estimate = sanitizeTokens(estimatedInputTokens);
|
|
220
|
+
const entries = prune(model, now);
|
|
221
|
+
let delayMs = computeWindowDelay(entries, estimate, now);
|
|
222
|
+
const hint = activeHint(model, now);
|
|
223
|
+
if (hint) {
|
|
224
|
+
const hintDelayMs = Math.max(0, hint.expiresAtMs - now);
|
|
225
|
+
if (hintDelayMs > delayMs)
|
|
226
|
+
delayMs = hintDelayMs;
|
|
227
|
+
}
|
|
228
|
+
const id = nextReservationId++;
|
|
229
|
+
const entry = {
|
|
230
|
+
id,
|
|
231
|
+
tsMs: now + delayMs,
|
|
232
|
+
tokens: estimate,
|
|
233
|
+
};
|
|
234
|
+
// Sorted-insert by tsMs ascending. Append-order is NOT a reliable
|
|
235
|
+
// proxy for chronological tsMs order — a `delayMs = 0` reservation
|
|
236
|
+
// arriving after a future-dated provisional (hint-driven or
|
|
237
|
+
// eviction-driven delay) has a smaller tsMs than the provisional's.
|
|
238
|
+
// Without sorted-insert the head-only fast-path in `prune` silently
|
|
239
|
+
// skips expired entries buried mid-array and `computeWindowDelay`'s
|
|
240
|
+
// oldest-first eviction picks the wrong entry to wait for.
|
|
241
|
+
insertSortedByTsMs(entries, entry);
|
|
242
|
+
windows.set(model, entries);
|
|
243
|
+
reservationIndex.set(id, { model, entry });
|
|
244
|
+
return { delayMs, releaseId: id };
|
|
245
|
+
},
|
|
246
|
+
release(releaseId, actualInputTokens, nowMs = Date.now()) {
|
|
247
|
+
if (disabled || releaseId < 0)
|
|
248
|
+
return;
|
|
249
|
+
const ref = reservationIndex.get(releaseId);
|
|
250
|
+
if (!ref)
|
|
251
|
+
return;
|
|
252
|
+
const actual = sanitizeTokens(actualInputTokens);
|
|
253
|
+
// Mutate the entry in place so the window sum reflects reality for
|
|
254
|
+
// future `reserve` callers. Leave tsMs unchanged: the call happened
|
|
255
|
+
// at the reservation's effective time regardless of how long the
|
|
256
|
+
// response took to arrive.
|
|
257
|
+
ref.entry.tokens = actual;
|
|
258
|
+
// Delete from the index AFTER mutating so a duplicate `release` or a
|
|
259
|
+
// late `cancel` on the same id becomes a safe no-op — without this,
|
|
260
|
+
// `cancel(alreadyReleasedId)` would remove the already-accounted
|
|
261
|
+
// entry from the window and under-throttle subsequent callers. The
|
|
262
|
+
// entry itself remains in `windows[model]` to count against the
|
|
263
|
+
// running window until it ages out naturally.
|
|
264
|
+
reservationIndex.delete(releaseId);
|
|
265
|
+
// Opportunistically prune while we have a nowMs — amortises cleanup
|
|
266
|
+
// cost across release calls and avoids relying solely on future
|
|
267
|
+
// `reserve` calls for the same model.
|
|
268
|
+
prune(ref.model, effectiveNow(nowMs));
|
|
269
|
+
},
|
|
270
|
+
cancel(releaseId) {
|
|
271
|
+
if (disabled || releaseId < 0)
|
|
272
|
+
return;
|
|
273
|
+
const ref = reservationIndex.get(releaseId);
|
|
274
|
+
if (!ref)
|
|
275
|
+
return;
|
|
276
|
+
reservationIndex.delete(releaseId);
|
|
277
|
+
const existing = windows.get(ref.model);
|
|
278
|
+
if (!existing)
|
|
279
|
+
return;
|
|
280
|
+
const filtered = existing.filter((e) => e.id !== releaseId);
|
|
281
|
+
if (filtered.length === 0)
|
|
282
|
+
windows.delete(ref.model);
|
|
283
|
+
else
|
|
284
|
+
windows.set(ref.model, filtered);
|
|
285
|
+
},
|
|
286
|
+
shouldDelay(model, estimatedInputTokens, nowMs = Date.now()) {
|
|
287
|
+
if (disabled)
|
|
288
|
+
return 0;
|
|
289
|
+
const now = effectiveNow(nowMs);
|
|
290
|
+
const estimate = sanitizeTokens(estimatedInputTokens);
|
|
291
|
+
const entries = prune(model, now);
|
|
292
|
+
// Peek uses the PURE delay — callers polling for progress-bar
|
|
293
|
+
// updates or diagnostics would otherwise see bouncing values for
|
|
294
|
+
// unchanged state, which `computeWindowDelay`'s random jitter
|
|
295
|
+
// introduces for `reserve`'s thundering-herd avoidance.
|
|
296
|
+
const windowDelay = computeWindowDelayPure(entries, estimate, now);
|
|
297
|
+
const hint = activeHint(model, now);
|
|
298
|
+
const hintDelay = hint ? Math.max(0, hint.expiresAtMs - now) : 0;
|
|
299
|
+
return Math.max(windowDelay, hintDelay);
|
|
300
|
+
},
|
|
301
|
+
recordRetryHint(model, retryDelayMs, nowMs = Date.now()) {
|
|
302
|
+
if (disabled)
|
|
303
|
+
return;
|
|
304
|
+
if (!Number.isFinite(retryDelayMs) || retryDelayMs <= 0)
|
|
305
|
+
return;
|
|
306
|
+
const now = effectiveNow(nowMs);
|
|
307
|
+
const newExpiresAtMs = now + retryDelayMs;
|
|
308
|
+
// Extend-only: a shorter hint replacing a longer one would produce
|
|
309
|
+
// a `reserve` that computes a smaller `tsMs` than entries already
|
|
310
|
+
// appended under the longer hint, breaking the sorted-array
|
|
311
|
+
// invariant. Keep the longer expiry when both exist — even if the
|
|
312
|
+
// new hint's `retryDelayMs` is smaller numerically, the wall-clock
|
|
313
|
+
// expiry monotonically increases.
|
|
314
|
+
const existing = hints.get(model);
|
|
315
|
+
if (existing && existing.expiresAtMs >= newExpiresAtMs)
|
|
316
|
+
return;
|
|
317
|
+
hints.set(model, { expiresAtMs: newExpiresAtMs, retryDelayMs });
|
|
318
|
+
},
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
/** Coerce non-finite / negative token values to 0 so callers with bad
|
|
322
|
+
* instrumentation data can't crash the throttle. A 0-token reservation
|
|
323
|
+
* still takes an ID (for lifecycle symmetry) but contributes nothing. */
|
|
324
|
+
function sanitizeTokens(n) {
|
|
325
|
+
if (!Number.isFinite(n) || n <= 0)
|
|
326
|
+
return 0;
|
|
327
|
+
return n;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Floor clamp (ms) for a parsed retry-hint. Values below this usually
|
|
331
|
+
* indicate a Gemini-side instantaneous retry suggestion that, combined with
|
|
332
|
+
* our window math, would cause tight-loop retries without meaningful
|
|
333
|
+
* backoff. Round up to 1 s so the hint contributes a perceivable delay.
|
|
334
|
+
*/
|
|
335
|
+
const RETRY_HINT_MIN_MS = 1_000;
|
|
336
|
+
/**
|
|
337
|
+
* Ceiling clamp (ms) for a parsed retry-hint. Caps malformed or future-
|
|
338
|
+
* format values that could otherwise poison the hint (e.g. Gemini some-day
|
|
339
|
+
* returning `"retryDelay": "3600s"` under a tier-upgrade rollout). Aligned
|
|
340
|
+
* with the throttle's 60 s window since longer waits would be better
|
|
341
|
+
* served by the caller giving up and retrying fresh.
|
|
342
|
+
*/
|
|
343
|
+
const RETRY_HINT_MAX_MS = 60_000;
|
|
344
|
+
/**
|
|
345
|
+
* Extract a Gemini 429 `retryInfo.retryDelay` value from the `@google/genai`
|
|
346
|
+
* `ApiError.message` body. Gemini surfaces the retry hint in a structured
|
|
347
|
+
* error body that gets stringified into the Error message — there is no
|
|
348
|
+
* typed field on `ApiError` for it (as of `@google/genai@1.50.x`, the
|
|
349
|
+
* public surface is just `{ status: number; message: string }`). We regex
|
|
350
|
+
* the "retryDelay" value rather than JSON-parse the whole body because the
|
|
351
|
+
* body format has varied across Gemini API versions and we don't want a
|
|
352
|
+
* schema drift to silently disable the hint extraction.
|
|
353
|
+
*
|
|
354
|
+
* Returns `null` when no hint is present or parsing fails (malformed
|
|
355
|
+
* body, missing `retryDelay` field, non-numeric value). Returns a value
|
|
356
|
+
* in `[RETRY_HINT_MIN_MS, RETRY_HINT_MAX_MS]` on a successful parse.
|
|
357
|
+
*
|
|
358
|
+
* Example error body shape that this extracts from:
|
|
359
|
+
* ```
|
|
360
|
+
* {"error":{...,"details":[...,{"@type":"...RetryInfo","retryDelay":"2s"}]}}
|
|
361
|
+
* ```
|
|
362
|
+
* Only the `"retryDelay":"<seconds>s"` substring is matched — we don't
|
|
363
|
+
* walk the details array. This is deliberately lenient.
|
|
364
|
+
*/
|
|
365
|
+
const RETRY_DELAY_REGEX = /"retryDelay"\s*:\s*"(\d+(?:\.\d+)?)s"/;
|
|
366
|
+
export function parseRetryDelayMs(errorMessage) {
|
|
367
|
+
if (typeof errorMessage !== 'string' || errorMessage.length === 0)
|
|
368
|
+
return null;
|
|
369
|
+
const match = errorMessage.match(RETRY_DELAY_REGEX);
|
|
370
|
+
if (!match || !match[1])
|
|
371
|
+
return null;
|
|
372
|
+
const seconds = Number.parseFloat(match[1]);
|
|
373
|
+
if (!Number.isFinite(seconds) || seconds <= 0)
|
|
374
|
+
return null;
|
|
375
|
+
const ms = Math.round(seconds * 1000);
|
|
376
|
+
if (ms < RETRY_HINT_MIN_MS)
|
|
377
|
+
return RETRY_HINT_MIN_MS;
|
|
378
|
+
if (ms > RETRY_HINT_MAX_MS)
|
|
379
|
+
return RETRY_HINT_MAX_MS;
|
|
380
|
+
return ms;
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Insert `entry` into `entries` at the position that keeps the array
|
|
384
|
+
* sorted ascending by `tsMs`. Binary-search for the insertion point;
|
|
385
|
+
* `splice` to insert. O(log n) compares + O(n) shift, same asymptotic
|
|
386
|
+
* cost as `push + sort` but with a smaller constant factor on the
|
|
387
|
+
* append-to-end fast path (the common case when clocks and delays are
|
|
388
|
+
* monotonic).
|
|
389
|
+
*/
|
|
390
|
+
function insertSortedByTsMs(entries, entry) {
|
|
391
|
+
// Common case: new entry is latest. Skip the binary search.
|
|
392
|
+
const last = entries[entries.length - 1];
|
|
393
|
+
if (!last || entry.tsMs >= last.tsMs) {
|
|
394
|
+
entries.push(entry);
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
let lo = 0;
|
|
398
|
+
let hi = entries.length;
|
|
399
|
+
while (lo < hi) {
|
|
400
|
+
const mid = (lo + hi) >>> 1;
|
|
401
|
+
const probe = entries[mid];
|
|
402
|
+
// `probe` is defined because `mid < hi <= entries.length`.
|
|
403
|
+
if (probe && probe.tsMs <= entry.tsMs)
|
|
404
|
+
lo = mid + 1;
|
|
405
|
+
else
|
|
406
|
+
hi = mid;
|
|
407
|
+
}
|
|
408
|
+
entries.splice(lo, 0, entry);
|
|
409
|
+
}
|
|
410
|
+
//# sourceMappingURL=throttle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttle.js","sourceRoot":"","sources":["../../../src/tools/shared/throttle.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyDG;AAEH,MAAM,SAAS,GAAG,MAAM,CAAC;AAEzB;;;;;;;;;;;GAWG;AACH,MAAM,aAAa,GAAG,KAAK,CAAC;AAC5B,MAAM,aAAa,GAAG,KAAK,CAAC;AAC5B;;;;;;GAMG;AACH,SAAS,eAAe;IACtB,OAAO,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,aAAa,GAAG,aAAa,GAAG,CAAC,CAAC,CAAC,CAAC;AACzF,CAAC;AAyFD,MAAM,UAAU,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ACZ,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,kBAAkB,CAAC,OAAsB,EAAE,KAAkB;IACpE,4DAA4D;IAC5D,MAAM,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACzC,IAAI,CAAC,IAAI,IAAI,KAAK,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;QACrC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACpB,OAAO;IACT,CAAC;IACD,IAAI,EAAE,GAAG,CAAC,CAAC;IACX,IAAI,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC;IACxB,OAAO,EAAE,GAAG,EAAE,EAAE,CAAC;QACf,MAAM,GAAG,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC;QAC5B,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;QAC3B,2DAA2D;QAC3D,IAAI,KAAK,IAAI,KAAK,CAAC,IAAI,IAAI,KAAK,CAAC,IAAI;YAAE,EAAE,GAAG,GAAG,GAAG,CAAC,CAAC;;YAC/C,EAAE,GAAG,GAAG,CAAC;IAChB,CAAC;IACD,OAAO,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC;AAC/B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qmediat.io/gemini-code-context-mcp",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "MCP server that gives Claude Code persistent memory of your codebase, backed by Gemini's 2M-token context. Drop-in upgrade over the abandoned gemini-mcp-tool.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mcp",
|