@qmediat.io/gemini-code-context-mcp 1.23.1 → 1.24.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/CHANGELOG.md +72 -1
- package/README.md +1 -1
- package/dist/cache/ttl-watcher.d.ts +29 -0
- package/dist/cache/ttl-watcher.d.ts.map +1 -1
- package/dist/cache/ttl-watcher.js +104 -43
- package/dist/cache/ttl-watcher.js.map +1 -1
- package/dist/gemini/client.d.ts +7 -0
- package/dist/gemini/client.d.ts.map +1 -1
- package/dist/gemini/client.js +8 -1
- package/dist/gemini/client.js.map +1 -1
- package/dist/gemini/long-decode.d.ts +153 -0
- package/dist/gemini/long-decode.d.ts.map +1 -0
- package/dist/gemini/long-decode.js +178 -0
- package/dist/gemini/long-decode.js.map +1 -0
- package/dist/gemini/model-registry.d.ts +2 -0
- package/dist/gemini/model-registry.d.ts.map +1 -1
- package/dist/gemini/model-registry.js +5 -2
- package/dist/gemini/model-registry.js.map +1 -1
- package/dist/gemini/models.d.ts.map +1 -1
- package/dist/gemini/models.js +1 -0
- package/dist/gemini/models.js.map +1 -1
- package/dist/gemini/retry.d.ts +8 -0
- package/dist/gemini/retry.d.ts.map +1 -1
- package/dist/gemini/retry.js +33 -1
- package/dist/gemini/retry.js.map +1 -1
- package/dist/tools/ask-agentic.tool.d.ts.map +1 -1
- package/dist/tools/ask-agentic.tool.js +10 -12
- package/dist/tools/ask-agentic.tool.js.map +1 -1
- package/dist/tools/ask.tool.d.ts.map +1 -1
- package/dist/tools/ask.tool.js +168 -379
- package/dist/tools/ask.tool.js.map +1 -1
- package/dist/tools/code.tool.d.ts +6 -0
- package/dist/tools/code.tool.d.ts.map +1 -1
- package/dist/tools/code.tool.js +146 -286
- package/dist/tools/code.tool.js.map +1 -1
- package/dist/tools/shared/abort-timeout.d.ts +6 -0
- package/dist/tools/shared/abort-timeout.d.ts.map +1 -1
- package/dist/tools/shared/abort-timeout.js +12 -0
- package/dist/tools/shared/abort-timeout.js.map +1 -1
- package/dist/tools/shared/call-ledger.d.ts +73 -0
- package/dist/tools/shared/call-ledger.d.ts.map +1 -0
- package/dist/tools/shared/call-ledger.js +115 -0
- package/dist/tools/shared/call-ledger.js.map +1 -0
- package/dist/tools/shared/context-decode.d.ts +77 -0
- package/dist/tools/shared/context-decode.d.ts.map +1 -0
- package/dist/tools/shared/context-decode.js +125 -0
- package/dist/tools/shared/context-decode.js.map +1 -0
- package/dist/tools/shared/continuation.d.ts +124 -0
- package/dist/tools/shared/continuation.d.ts.map +1 -0
- package/dist/tools/shared/continuation.js +302 -0
- package/dist/tools/shared/continuation.js.map +1 -0
- package/dist/tools/shared/dispatch-throttle.d.ts +43 -0
- package/dist/tools/shared/dispatch-throttle.d.ts.map +1 -0
- package/dist/tools/shared/dispatch-throttle.js +65 -0
- package/dist/tools/shared/dispatch-throttle.js.map +1 -0
- package/dist/tools/shared/settle-call.d.ts +63 -0
- package/dist/tools/shared/settle-call.d.ts.map +1 -0
- package/dist/tools/shared/settle-call.js +55 -0
- package/dist/tools/shared/settle-call.js.map +1 -0
- package/dist/tools/shared/stream-collector.d.ts +7 -0
- package/dist/tools/shared/stream-collector.d.ts.map +1 -1
- package/dist/tools/shared/stream-collector.js +5 -1
- package/dist/tools/shared/stream-collector.js.map +1 -1
- package/dist/tools/shared/thinking.d.ts +41 -11
- package/dist/tools/shared/thinking.d.ts.map +1 -1
- package/dist/tools/shared/thinking.js +62 -9
- package/dist/tools/shared/thinking.js.map +1 -1
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/cost-estimator.d.ts +17 -0
- package/dist/utils/cost-estimator.d.ts.map +1 -1
- package/dist/utils/cost-estimator.js +15 -2
- package/dist/utils/cost-estimator.js.map +1 -1
- package/package.json +4 -3
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,76 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [1.24.0] - 2026-11-17
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- **Long decode on Gemini 4** (`ask`, `code`): a `maxOutputTokens` above the per-request limit (65,536) is sent as
|
|
15
|
+
asked on Gemini 4 Argon, Carbon and Hydrogen, up to 1,048,576. A response that stops with
|
|
16
|
+
`finishReason: CONTINUATION` is continued with the same request and Google's continuation token until the model
|
|
17
|
+
stops or the cap is reached, at most `ceil(maxOutputTokens ÷ L)` requests (`L` the model's advertised per-request
|
|
18
|
+
limit: 65,536 on Hydrogen, 32,768 on Argon); the result is one answer with one thinking summary, its token fields
|
|
19
|
+
and `costEstimateUsd` summed over the requests, and `continuationHops` (`0` for a call that ended in one request).
|
|
20
|
+
Measured on gemini-4-hydrogen at `XHIGH`: review prompts of 99,712 and 153,906 tokens that stopped at 65,533
|
|
21
|
+
thinking tokens with no answer now answer in two requests (1.0485 and 1.3164 USD). Without `maxOutputTokens` a call
|
|
22
|
+
is one request, as before. Named limits: `ask_agentic` (its own loop, one model call per iteration) and the Vertex AI
|
|
23
|
+
backend are not covered — on a Vertex client the cap is clamped to the per-request limit whatever the shape of the
|
|
24
|
+
model id (a plain `gemini-4-…` id included). Each continuation request sends the prompt again plus everything decoded
|
|
25
|
+
before it, so a long decode is bounded by the model's input window as well: when the counted prompt leaves less room
|
|
26
|
+
than the cap, the decode runs to what the window leaves (never below one request) and the result names the cut in
|
|
27
|
+
`outputCapCutByInputWindow` (`from`, `to`, `inputTokenLimit`). A request that makes no progress —
|
|
28
|
+
it returns the continuation token it was sent with, or decodes nothing — ends the decode instead of being sent again
|
|
29
|
+
up to the request limit. A continuation request refused at its opening with 429 or 503 (nothing of it billed) is
|
|
30
|
+
sent again with the same token after Google's retry hint, once its TPM throttle reservation is taken again for the
|
|
31
|
+
minute it is sent in; one request's opening makes at most three SDK calls on `standard` (network retries and
|
|
32
|
+
reopenings together), one on `flex`.
|
|
33
|
+
`timeoutMs` (30 minutes at most) fits about 7 to 9 requests at the measured pace; when it fires the completed
|
|
34
|
+
requests are billed and reported. `stallMs` does not count the time before a continuation request's first chunk.
|
|
35
|
+
- `finishReason` and `truncated` on every `ask` and `code` result: `truncated: true` when the answer has text but
|
|
36
|
+
stopped at the output cap (`MAX_TOKENS`, or a long decode that ended on `CONTINUATION`) — the answer is cut. `code`
|
|
37
|
+
then leads its text with a `[truncated]` line: its edit list is incomplete and must not be applied as complete.
|
|
38
|
+
- `errorCode: OUTPUT_CAP` for a call that stops at the output cap with no answer (`MAX_TOKENS`, or a long decode cut
|
|
39
|
+
at its cap); the error carries the billed token fields, `costEstimateUsd`, `continuationHops` and `finishReason`,
|
|
40
|
+
and names the remedy for the cap that ran: raise `maxOutputTokens` below the model's per-request limit, ask above
|
|
41
|
+
the limit on a Gemini 4 model, raise it above the cap after a long decode, or lower the thinking level when the cap
|
|
42
|
+
is the most the model allows.
|
|
43
|
+
|
|
44
|
+
### Changed
|
|
45
|
+
|
|
46
|
+
- `@google/genai` 3.4 (from 1.52): its `FinishReason` has `CONTINUATION` and a candidate keeps its
|
|
47
|
+
`continuationToken`. Every request is sent with `automaticContinuation: false` (the SDK's default is on): `ask` and
|
|
48
|
+
`code` drive the continuation themselves so they can count the requests and bound them, and `ask_agentic` keeps one
|
|
49
|
+
request per model call.
|
|
50
|
+
- `GEMINI_DAILY_BUDGET_USD`: the reservation of a long decode adds the input every continuation request bills again
|
|
51
|
+
(the prompt plus what was decoded before it, plus 256 tokens a request for what the API bills beyond them), and its
|
|
52
|
+
output covers one request's decode past the cap where the cap is not a multiple of a request's decode; the formula
|
|
53
|
+
is in `docs/cost-model.md`. The reservation's prompt is the larger of the bytes ÷ 4 heuristic and the preflight's
|
|
54
|
+
count, and a decode that may take more than one request takes the exact `countTokens` count whatever
|
|
55
|
+
`preflightMode` asked for.
|
|
56
|
+
- `ask` / `code`: a call that fails after one or more of its requests completed (a continuation request that fails, a
|
|
57
|
+
timeout) keeps what those requests billed in the daily budget ledger and releases only the rest of its
|
|
58
|
+
reservation; the error carries their token fields, `costEstimateUsd` and `continuationHops`.
|
|
59
|
+
- `ask` / `code`: a context cache that expires after a request of a long decode completed ends the call with an error
|
|
60
|
+
and the completed requests billed — the decode is no longer restarted on a rebuilt cache, which billed every request
|
|
61
|
+
again under a reservation sized for one decode. A stale cache at the first request is still rebuilt and the call
|
|
62
|
+
sent once more. Before each request of a decode, the cache it reads is marked hot and, when it expires within 15
|
|
63
|
+
minutes, extended at once (the TTL watcher's own tick runs every 5 minutes).
|
|
64
|
+
- `ask` / `code`: each request of a long decode holds its own TPM throttle reservation (the prompt plus what was
|
|
65
|
+
decoded before it), settled at the input it reported.
|
|
66
|
+
- `ask` / `code`: a request that completed but reported no usage is booked in the daily budget ledger at the input it
|
|
67
|
+
was estimated to send — the rule `ask_agentic` already applied — instead of at 0 USD, which released the whole
|
|
68
|
+
reservation for billed work.
|
|
69
|
+
|
|
70
|
+
- Development only: `npm run check:supports` compares `server.json` (`version` and every `packages[].version`) and the
|
|
71
|
+
root of `package-lock.json` (`version`, and `packages[""].version` where the lockfile has that map) with
|
|
72
|
+
`package.json` too, beside the README heading, and prints every mismatch; both files are required. The release
|
|
73
|
+
workflow runs it instead of its own `server.json` step, which stays only for a tag older than the script.
|
|
74
|
+
`npm run check:addresses` (`scripts/check-addresses.mjs`, run by CI, `prepublishOnly` and by the release workflow)
|
|
75
|
+
fails on an email address outside `qmediat.io` in a tracked text file — a repository's fixture addresses are allowed
|
|
76
|
+
only in their own file — and exits 2 when a tracked file cannot be read. Every address-shaped text — a last label of
|
|
77
|
+
two or more letters — outside the allowed domains and their subdomains is reported, nothing is exempt by its shape,
|
|
78
|
+
and an exact per-file allowlist in the script holds what a file must show. Nothing in the published package changes.
|
|
79
|
+
|
|
10
80
|
## [1.23.1] - 2026-11-15
|
|
11
81
|
|
|
12
82
|
### Changed
|
|
@@ -1798,7 +1868,8 @@ Integration-test-surfaced and review-surfaced fixes landed before the v1.0.0 rel
|
|
|
1798
1868
|
- MIME type simplified to `text/plain` for all files.
|
|
1799
1869
|
- Unit test coverage gaps for cache-manager / files-uploader / ttl-watcher / profile-loader / parseEdits tracked in [`docs/FOLLOW-UP-PRS.md`](docs/FOLLOW-UP-PRS.md).
|
|
1800
1870
|
|
|
1801
|
-
[Unreleased]: https://github.com/qmediat/gemini-code-context-mcp/compare/v1.
|
|
1871
|
+
[Unreleased]: https://github.com/qmediat/gemini-code-context-mcp/compare/v1.24.0...HEAD
|
|
1872
|
+
[1.24.0]: https://github.com/qmediat/gemini-code-context-mcp/compare/v1.23.1...v1.24.0
|
|
1802
1873
|
[1.23.1]: https://github.com/qmediat/gemini-code-context-mcp/compare/v1.23.0...v1.23.1
|
|
1803
1874
|
[1.23.0]: https://github.com/qmediat/gemini-code-context-mcp/compare/v1.22.1...v1.23.0
|
|
1804
1875
|
[1.22.1]: https://github.com/qmediat/gemini-code-context-mcp/compare/v1.22.0...v1.22.1
|
package/README.md
CHANGED
|
@@ -14,10 +14,39 @@ export declare class TtlWatcher {
|
|
|
14
14
|
private timer;
|
|
15
15
|
/** Re-entrancy guard: a slow `caches.update` round-trip must not overlap the next tick. */
|
|
16
16
|
private tickInProgress;
|
|
17
|
+
/** The refresh running for each cache name: the tick and every keep-alive of that cache join it. */
|
|
18
|
+
private readonly refreshing;
|
|
17
19
|
constructor(client: GoogleGenAI, manifest: ManifestDb);
|
|
18
20
|
start(): void;
|
|
19
21
|
stop(): void;
|
|
20
22
|
markHot(workspaceRoot: string, cacheId: string, ttlSeconds: number): void;
|
|
23
|
+
/**
|
|
24
|
+
* A request is about to be sent on the cache: mark it hot and, when it expires within the refresh window
|
|
25
|
+
* (15 minutes), extend it now instead of at the next tick — so a decode of many requests, each opened through here,
|
|
26
|
+
* never sends one on a cache that expires before the request ends (a request of a decode lasts minutes). The
|
|
27
|
+
* keep-alive is best-effort: a failed refresh, or a manifest that cannot be read or written, is logged and leaves
|
|
28
|
+
* the request to meet the cache as it is.
|
|
29
|
+
*
|
|
30
|
+
* `signal` is the calling request's: an aborted signal starts no refresh, and the wait ends with its abort reason the
|
|
31
|
+
* moment it fires — the only rejection of this method, so a slow or hanging refresh never holds a call past its
|
|
32
|
+
* timeout. The refresh itself carries no caller's signal: it is shared by every keep-alive of the cache and by the
|
|
33
|
+
* tick, so it runs to its end for the others when the call that started it leaves.
|
|
34
|
+
*/
|
|
35
|
+
keepAlive(workspaceRoot: string, cacheId: string, ttlSeconds: number, signal?: AbortSignal): Promise<void>;
|
|
36
|
+
private hotEntry;
|
|
21
37
|
private tick;
|
|
38
|
+
/**
|
|
39
|
+
* The one refresh of the entry's cache: the running one when there is one, else a new one. It never rejects — a
|
|
40
|
+
* failure of the refresh or of the manifest is logged here, for the tick and the keep-alive alike.
|
|
41
|
+
*/
|
|
42
|
+
private refresh;
|
|
43
|
+
/** Extends the entry's cache when it expires within the refresh window; drops an entry whose cache is gone. */
|
|
44
|
+
private refreshIfDue;
|
|
45
|
+
/**
|
|
46
|
+
* The expiry the API answered goes to the manifest row as it is NOW — read again after the round-trip, so a row
|
|
47
|
+
* rewritten meanwhile keeps its other fields, and a row that moved to another cache is left alone.
|
|
48
|
+
*/
|
|
49
|
+
private storeExpiry;
|
|
50
|
+
private updateFailed;
|
|
22
51
|
}
|
|
23
52
|
//# sourceMappingURL=ttl-watcher.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ttl-watcher.d.ts","sourceRoot":"","sources":["../../src/cache/ttl-watcher.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"ttl-watcher.d.ts","sourceRoot":"","sources":["../../src/cache/ttl-watcher.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAiB,WAAW,EAAE,MAAM,eAAe,CAAC;AAEhE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAoBpD,qBAAa,UAAU;IASnB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAT3B,OAAO,CAAC,QAAQ,CAAC,GAAG,CAA+B;IACnD,OAAO,CAAC,KAAK,CAA+B;IAC5C,2FAA2F;IAC3F,OAAO,CAAC,cAAc,CAAS;IAC/B,oGAAoG;IACpG,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAoC;IAE/D,YACmB,MAAM,EAAE,WAAW,EACnB,QAAQ,EAAE,UAAU,EACnC;IAEJ,KAAK,IAAI,IAAI,CAOZ;IAED,IAAI,IAAI,IAAI,CAMX;IAED,OAAO,CAAC,aAAa,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI,CAExE;IAED;;;;;;;;;;;OAWG;IACG,SAAS,CACb,aAAa,EAAE,MAAM,EACrB,OAAO,EAAE,MAAM,EACf,UAAU,EAAE,MAAM,EAClB,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,IAAI,CAAC,CAIf;IAED,OAAO,CAAC,QAAQ;YAMF,IAAI;IAuBlB;;;OAGG;IACH,OAAO,CAAC,OAAO;IAYf,+GAA+G;YACjG,YAAY;IAuB1B;;;OAGG;IACH,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,YAAY;CAqBrB"}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* with a new `ttl` value. We only refresh caches that were used in the last
|
|
6
6
|
* 10 minutes — cold workspaces are allowed to expire.
|
|
7
7
|
*/
|
|
8
|
+
import { abortable, throwIfAborted } from '../gemini/retry.js';
|
|
8
9
|
import { logger, safeForLog } from '../utils/logger.js';
|
|
9
10
|
const TICK_MS = 5 * 60 * 1000; // 5 minutes
|
|
10
11
|
const HOT_WINDOW_MS = 10 * 60 * 1000; // used in last 10 minutes
|
|
@@ -21,6 +22,8 @@ export class TtlWatcher {
|
|
|
21
22
|
timer = null;
|
|
22
23
|
/** Re-entrancy guard: a slow `caches.update` round-trip must not overlap the next tick. */
|
|
23
24
|
tickInProgress = false;
|
|
25
|
+
/** The refresh running for each cache name: the tick and every keep-alive of that cache join it. */
|
|
26
|
+
refreshing = new Map();
|
|
24
27
|
constructor(client, manifest) {
|
|
25
28
|
this.client = client;
|
|
26
29
|
this.manifest = manifest;
|
|
@@ -42,12 +45,29 @@ export class TtlWatcher {
|
|
|
42
45
|
this.hot.clear();
|
|
43
46
|
}
|
|
44
47
|
markHot(workspaceRoot, cacheId, ttlSeconds) {
|
|
45
|
-
this.
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
this.hotEntry(workspaceRoot, cacheId, ttlSeconds);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* A request is about to be sent on the cache: mark it hot and, when it expires within the refresh window
|
|
52
|
+
* (15 minutes), extend it now instead of at the next tick — so a decode of many requests, each opened through here,
|
|
53
|
+
* never sends one on a cache that expires before the request ends (a request of a decode lasts minutes). The
|
|
54
|
+
* keep-alive is best-effort: a failed refresh, or a manifest that cannot be read or written, is logged and leaves
|
|
55
|
+
* the request to meet the cache as it is.
|
|
56
|
+
*
|
|
57
|
+
* `signal` is the calling request's: an aborted signal starts no refresh, and the wait ends with its abort reason the
|
|
58
|
+
* moment it fires — the only rejection of this method, so a slow or hanging refresh never holds a call past its
|
|
59
|
+
* timeout. The refresh itself carries no caller's signal: it is shared by every keep-alive of the cache and by the
|
|
60
|
+
* tick, so it runs to its end for the others when the call that started it leaves.
|
|
61
|
+
*/
|
|
62
|
+
async keepAlive(workspaceRoot, cacheId, ttlSeconds, signal) {
|
|
63
|
+
throwIfAborted(signal);
|
|
64
|
+
const entry = this.hotEntry(workspaceRoot, cacheId, ttlSeconds);
|
|
65
|
+
await abortable(this.refresh(entry, Date.now()), signal);
|
|
66
|
+
}
|
|
67
|
+
hotEntry(workspaceRoot, cacheId, ttlSeconds) {
|
|
68
|
+
const entry = { workspaceRoot, cacheId, lastUsed: Date.now(), ttlSeconds };
|
|
69
|
+
this.hot.set(workspaceRoot, entry);
|
|
70
|
+
return entry;
|
|
51
71
|
}
|
|
52
72
|
async tick() {
|
|
53
73
|
// Prevent overlap: if the previous tick is still running (slow Gemini
|
|
@@ -60,51 +80,92 @@ export class TtlWatcher {
|
|
|
60
80
|
this.tickInProgress = true;
|
|
61
81
|
try {
|
|
62
82
|
const now = Date.now();
|
|
63
|
-
for (const
|
|
83
|
+
for (const entry of [...this.hot.values()]) {
|
|
64
84
|
if (now - entry.lastUsed > HOT_WINDOW_MS) {
|
|
65
|
-
this.hot.delete(
|
|
85
|
+
this.hot.delete(entry.workspaceRoot);
|
|
66
86
|
continue;
|
|
67
87
|
}
|
|
68
|
-
|
|
69
|
-
if (!ws?.cacheId || ws.cacheId !== entry.cacheId) {
|
|
70
|
-
this.hot.delete(key);
|
|
71
|
-
continue;
|
|
72
|
-
}
|
|
73
|
-
if (ws.cacheExpiresAt !== null && ws.cacheExpiresAt - now > REFRESH_IF_EXPIRES_WITHIN_MS) {
|
|
74
|
-
continue; // plenty of time left
|
|
75
|
-
}
|
|
76
|
-
try {
|
|
77
|
-
await this.client.caches.update({
|
|
78
|
-
name: entry.cacheId,
|
|
79
|
-
config: { ttl: `${entry.ttlSeconds}s` },
|
|
80
|
-
});
|
|
81
|
-
const newExpires = now + entry.ttlSeconds * 1000;
|
|
82
|
-
this.manifest.upsertWorkspace({ ...ws, cacheExpiresAt: newExpires, updatedAt: now });
|
|
83
|
-
logger.debug(`refreshed TTL for ${safeForLog(entry.cacheId)}`);
|
|
84
|
-
}
|
|
85
|
-
catch (err) {
|
|
86
|
-
// Evict the entry when Gemini reports the cache is gone (deleted
|
|
87
|
-
// externally, admin quota action, etc). Without this, we'd retry
|
|
88
|
-
// the update every TICK_MS forever and log-spam the 404.
|
|
89
|
-
if (isNotFoundError(err)) {
|
|
90
|
-
logger.debug(`ttl refresh for ${entry.cacheId} returned not-found; dropping hot entry and nulling manifest cacheId`);
|
|
91
|
-
this.hot.delete(key);
|
|
92
|
-
this.manifest.upsertWorkspace({
|
|
93
|
-
...ws,
|
|
94
|
-
cacheId: null,
|
|
95
|
-
cacheExpiresAt: null,
|
|
96
|
-
updatedAt: now,
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
logger.debug(`ttl refresh failed for ${safeForLog(entry.cacheId)}: ${safeForLog(err)}`);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
88
|
+
await this.refresh(entry, now);
|
|
103
89
|
}
|
|
104
90
|
}
|
|
105
91
|
finally {
|
|
106
92
|
this.tickInProgress = false;
|
|
107
93
|
}
|
|
108
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* The one refresh of the entry's cache: the running one when there is one, else a new one. It never rejects — a
|
|
97
|
+
* failure of the refresh or of the manifest is logged here, for the tick and the keep-alive alike.
|
|
98
|
+
*/
|
|
99
|
+
refresh(entry, now) {
|
|
100
|
+
const running = this.refreshing.get(entry.cacheId);
|
|
101
|
+
if (running !== undefined)
|
|
102
|
+
return running;
|
|
103
|
+
const started = this.refreshIfDue(entry, now)
|
|
104
|
+
.catch((err) => {
|
|
105
|
+
logger.warn(`ttl refresh failed for ${safeForLog(entry.cacheId)}: ${safeForLog(err)}`);
|
|
106
|
+
})
|
|
107
|
+
.finally(() => this.refreshing.delete(entry.cacheId));
|
|
108
|
+
this.refreshing.set(entry.cacheId, started);
|
|
109
|
+
return started;
|
|
110
|
+
}
|
|
111
|
+
/** Extends the entry's cache when it expires within the refresh window; drops an entry whose cache is gone. */
|
|
112
|
+
async refreshIfDue(entry, now) {
|
|
113
|
+
const ws = this.manifest.getWorkspace(entry.workspaceRoot);
|
|
114
|
+
if (!ws?.cacheId || ws.cacheId !== entry.cacheId) {
|
|
115
|
+
this.hot.delete(entry.workspaceRoot);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (ws.cacheExpiresAt !== null && ws.cacheExpiresAt - now > REFRESH_IF_EXPIRES_WITHIN_MS) {
|
|
119
|
+
return; // plenty of time left
|
|
120
|
+
}
|
|
121
|
+
let answered;
|
|
122
|
+
try {
|
|
123
|
+
answered = await this.client.caches.update({
|
|
124
|
+
name: entry.cacheId,
|
|
125
|
+
config: { ttl: `${entry.ttlSeconds}s` },
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
catch (err) {
|
|
129
|
+
this.updateFailed(entry, err);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
this.storeExpiry(entry, expiryOf(answered, entry.ttlSeconds));
|
|
133
|
+
logger.debug(`refreshed TTL for ${safeForLog(entry.cacheId)}`);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The expiry the API answered goes to the manifest row as it is NOW — read again after the round-trip, so a row
|
|
137
|
+
* rewritten meanwhile keeps its other fields, and a row that moved to another cache is left alone.
|
|
138
|
+
*/
|
|
139
|
+
storeExpiry(entry, expiresAt) {
|
|
140
|
+
const current = this.manifest.getWorkspace(entry.workspaceRoot);
|
|
141
|
+
if (current?.cacheId !== entry.cacheId)
|
|
142
|
+
return;
|
|
143
|
+
this.manifest.upsertWorkspace({ ...current, cacheExpiresAt: expiresAt, updatedAt: Date.now() });
|
|
144
|
+
}
|
|
145
|
+
updateFailed(entry, err) {
|
|
146
|
+
// Evict the entry when Gemini reports the cache is gone (deleted
|
|
147
|
+
// externally, admin quota action, etc). Without this, we'd retry
|
|
148
|
+
// the update every TICK_MS forever and log-spam the 404.
|
|
149
|
+
if (!isNotFoundError(err)) {
|
|
150
|
+
logger.debug(`ttl refresh failed for ${safeForLog(entry.cacheId)}: ${safeForLog(err)}`);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
logger.debug(`ttl refresh for ${entry.cacheId} returned not-found; dropping hot entry and nulling manifest cacheId`);
|
|
154
|
+
this.hot.delete(entry.workspaceRoot);
|
|
155
|
+
const current = this.manifest.getWorkspace(entry.workspaceRoot);
|
|
156
|
+
if (current?.cacheId !== entry.cacheId)
|
|
157
|
+
return;
|
|
158
|
+
this.manifest.upsertWorkspace({
|
|
159
|
+
...current,
|
|
160
|
+
cacheId: null,
|
|
161
|
+
cacheExpiresAt: null,
|
|
162
|
+
updatedAt: Date.now(),
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/** The expiry `caches.update` answered; the requested TTL from now when the answer carries none. */
|
|
167
|
+
function expiryOf(answered, ttlSeconds) {
|
|
168
|
+
const parsed = answered.expireTime === undefined ? Number.NaN : Date.parse(answered.expireTime);
|
|
169
|
+
return Number.isFinite(parsed) ? parsed : Date.now() + ttlSeconds * 1000;
|
|
109
170
|
}
|
|
110
171
|
//# sourceMappingURL=ttl-watcher.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ttl-watcher.js","sourceRoot":"","sources":["../../src/cache/ttl-watcher.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;
|
|
1
|
+
{"version":3,"file":"ttl-watcher.js","sourceRoot":"","sources":["../../src/cache/ttl-watcher.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAE/D,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAExD,MAAM,OAAO,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,YAAY;AAC3C,MAAM,aAAa,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,0BAA0B;AAChE,MAAM,4BAA4B,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AASpD,sFAAsF;AACtF,SAAS,eAAe,CAAC,GAAY;IACnC,MAAM,GAAG,GAAG,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC7D,OAAO,2DAA2D,CAAC,IAAI,CAAC,GAAG,C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I,OAAO,EAAE,OAAO,KAAK,KAAK,CAAC,OAAO;YAAE,OAAO;QAC/C,IAAI,CAAC,QAAQ,CAAC,eAAe,CAAC;YAC5B,GAAG,OAAO;YACV,OAAO,EAAE,IAAI;YACb,cAAc,EAAE,IAAI;YACpB,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;SACtB,CAAC,CAAC;IACL,CAAC;CACF;AAED,oGAAoG;AACpG,SAAS,QAAQ,CAAC,QAAuB,EAAE,UAAkB;IAC3D,MAAM,MAAM,GAAG,QAAQ,CAAC,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;IAChG,OAAO,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,UAAU,GAAG,IAAI,CAAC;AAC3E,CAAC"}
|
package/dist/gemini/client.d.ts
CHANGED
|
@@ -3,5 +3,12 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { GoogleGenAI } from '@google/genai';
|
|
5
5
|
import type { AuthProfile } from '../types.js';
|
|
6
|
+
/**
|
|
7
|
+
* The Gemini Developer API version every request of an API-key client goes to. `@google/genai` defaults to the
|
|
8
|
+
* stable `v1`, which has no `cachedContents` resource (an explicit Context Cache answers 404 there); `v1beta`
|
|
9
|
+
* serves every surface this server calls — the Files API, `cachedContents`, `models`, `countTokens`,
|
|
10
|
+
* `generateContent` and its stream. Set in the constructor, it also wins over `GOOGLE_GENAI_API_VERSION`.
|
|
11
|
+
*/
|
|
12
|
+
export declare const GEMINI_API_VERSION = "v1beta";
|
|
6
13
|
export declare function createGeminiClient(profile: AuthProfile): GoogleGenAI;
|
|
7
14
|
//# sourceMappingURL=client.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/gemini/client.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/gemini/client.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAc/C;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,WAAW,CAAC;AAE3C,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,WAAW,GAAG,WAAW,CAapE"}
|
package/dist/gemini/client.js
CHANGED
|
@@ -13,10 +13,17 @@ import { GoogleGenAI } from '@google/genai';
|
|
|
13
13
|
// handled at the tool layer by `isGemini429` + `parseRetryDelayMs` (see
|
|
14
14
|
// `src/tools/shared/throttle.ts`). Pre-response network failures are handled
|
|
15
15
|
// by `withNetworkRetry` in `src/gemini/retry.ts`.
|
|
16
|
+
/**
|
|
17
|
+
* The Gemini Developer API version every request of an API-key client goes to. `@google/genai` defaults to the
|
|
18
|
+
* stable `v1`, which has no `cachedContents` resource (an explicit Context Cache answers 404 there); `v1beta`
|
|
19
|
+
* serves every surface this server calls — the Files API, `cachedContents`, `models`, `countTokens`,
|
|
20
|
+
* `generateContent` and its stream. Set in the constructor, it also wins over `GOOGLE_GENAI_API_VERSION`.
|
|
21
|
+
*/
|
|
22
|
+
export const GEMINI_API_VERSION = 'v1beta';
|
|
16
23
|
export function createGeminiClient(profile) {
|
|
17
24
|
switch (profile.kind) {
|
|
18
25
|
case 'api-key':
|
|
19
|
-
return new GoogleGenAI({ apiKey: profile.apiKey });
|
|
26
|
+
return new GoogleGenAI({ apiKey: profile.apiKey, apiVersion: GEMINI_API_VERSION });
|
|
20
27
|
case 'vertex':
|
|
21
28
|
// Vertex path implicitly uses ADC (gcloud application-default credentials).
|
|
22
29
|
// Set GEMINI_USE_VERTEX=true + GOOGLE_CLOUD_PROJECT to route through here.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/gemini/client.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAG5C,+EAA+E;AAC/E,8EAA8E;AAC9E,8EAA8E;AAC9E,gFAAgF;AAChF,4EAA4E;AAC5E,4EAA4E;AAC5E,+EAA+E;AAC/E,0EAA0E;AAC1E,wEAAwE;AACxE,6EAA6E;AAC7E,kDAAkD;
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/gemini/client.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAG5C,+EAA+E;AAC/E,8EAA8E;AAC9E,8EAA8E;AAC9E,gFAAgF;AAChF,4EAA4E;AAC5E,4EAA4E;AAC5E,+EAA+E;AAC/E,0EAA0E;AAC1E,wEAAwE;AACxE,6EAA6E;AAC7E,kDAAkD;AAElD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,QAAQ,CAAC;AAE3C,MAAM,UAAU,kBAAkB,CAAC,OAAoB;IACrD,QAAQ,OAAO,CAAC,IAAI,EAAE,CAAC;QACrB,KAAK,SAAS;YACZ,OAAO,IAAI,WAAW,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,UAAU,EAAE,kBAAkB,EAAE,CAAC,CAAC;QACrF,KAAK,QAAQ;YACX,4EAA4E;YAC5E,2EAA2E;YAC3E,OAAO,IAAI,WAAW,CAAC;gBACrB,QAAQ,EAAE,IAAI;gBACd,OAAO,EAAE,OAAO,CAAC,OAAO;gBACxB,QAAQ,EAAE,OAAO,CAAC,QAAQ;aAC3B,CAAC,CAAC;IACP,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Long decode — the facts of Gemini 4's continuation protocol and the output cap a call runs under.
|
|
3
|
+
*
|
|
4
|
+
* Google's reference (docs.cloud.google.com/vertex-ai/generative-ai/docs/model-reference/gemini): on a Gemini 4
|
|
5
|
+
* model a response that reaches the per-request decode limit stops with `finishReason: CONTINUATION` and a
|
|
6
|
+
* `continuationToken`; the ORIGINAL request sent again with `continuationToken` set to the latest token continues
|
|
7
|
+
* it, up to 1 048 576 decoded tokens in total. Thinking tokens count against the decode, and every request bills
|
|
8
|
+
* its whole input again: the prompt plus everything decoded before it.
|
|
9
|
+
*/
|
|
10
|
+
import type { PreflightInput } from './token-counter.js';
|
|
11
|
+
/** The most a Gemini 4 decode can produce over all its requests. */
|
|
12
|
+
export declare const LONG_DECODE_TOTAL_TOKENS = 1048576;
|
|
13
|
+
/** The most one streaming request decodes before it stops with `CONTINUATION` (a non-streaming one: 32 768). */
|
|
14
|
+
export declare const LONG_DECODE_STREAM_REQUEST_TOKENS = 65536;
|
|
15
|
+
/** The API a client was built for: the Gemini Developer API (an API key) or Vertex AI. */
|
|
16
|
+
export type ApiBackend = 'developer' | 'vertex';
|
|
17
|
+
/**
|
|
18
|
+
* The total a decode of `modelId` may reach through continuation, or `null` for a model without long decode. Long
|
|
19
|
+
* decode runs on the Gemini Developer API only: on Vertex AI every model is clamped to its per-request limit,
|
|
20
|
+
* whatever the shape of its id. The model registry carries it beside the advertised input and output limits.
|
|
21
|
+
*/
|
|
22
|
+
export declare function longDecodeTokenLimitOf(modelId: string, backend: ApiBackend): number | null;
|
|
23
|
+
/**
|
|
24
|
+
* Input a continuation request bills beyond the prompt and the tokens decoded before it. Measured on
|
|
25
|
+
* gemini-4-hydrogen in two runs: +125 tokens (99 712-token prompt, request 2 billed 165 373) and +89; reserved at
|
|
26
|
+
* twice the larger, rounded up.
|
|
27
|
+
*/
|
|
28
|
+
export declare const CONTINUATION_REQUEST_OVERHEAD_TOKENS = 256;
|
|
29
|
+
/** The output cap of one call: what goes on the wire and how many requests the decode may take. */
|
|
30
|
+
export interface OutputCap {
|
|
31
|
+
/**
|
|
32
|
+
* `maxOutputTokens` on the wire; `undefined` sends none and the model applies its own default. A cap the input window
|
|
33
|
+
* cut to one request sends the cut value (`boundByInputWindow`).
|
|
34
|
+
*/
|
|
35
|
+
readonly wire: number | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* The most the call decodes: the wire cap, else the model's per-request limit (its default) — less when the
|
|
38
|
+
* model's input window bounds a long decode (`boundByInputWindow`).
|
|
39
|
+
*/
|
|
40
|
+
readonly effective: number;
|
|
41
|
+
/** The model's advertised per-request output limit. */
|
|
42
|
+
readonly perRequest: number;
|
|
43
|
+
/**
|
|
44
|
+
* The most a caller may ask for: the long-decode total on a model with long decode, else `perRequest`; for a prompt
|
|
45
|
+
* the input window bounds, what that window leaves.
|
|
46
|
+
*/
|
|
47
|
+
readonly ceiling: number;
|
|
48
|
+
/** The requests the decode may take: 1 unless the cap is above the per-request limit on a long-decode model. */
|
|
49
|
+
readonly maxRequests: number;
|
|
50
|
+
}
|
|
51
|
+
export interface OutputCapInputs {
|
|
52
|
+
/** The caller's `maxOutputTokens`, if any. */
|
|
53
|
+
readonly requested: number | undefined;
|
|
54
|
+
/** `GEMINI_CODE_CONTEXT_FORCE_MAX_OUTPUT`: send the per-request limit when the caller sets no cap. */
|
|
55
|
+
readonly forceMax: boolean;
|
|
56
|
+
readonly outputTokenLimit: number | null;
|
|
57
|
+
/** `ResolvedModel.longDecodeTokenLimit`; `undefined` reads as none. */
|
|
58
|
+
readonly longDecodeTokenLimit: number | null | undefined;
|
|
59
|
+
/** The per-request limit assumed when the model advertises none. */
|
|
60
|
+
readonly fallbackLimit: number;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The caller's cap is clamped to the model's per-request limit, or — on a model with long decode — to its long
|
|
64
|
+
* decode total. Without a caller's cap nothing goes on the wire (one request, the model's default), unless the
|
|
65
|
+
* operator forces the per-request limit.
|
|
66
|
+
*/
|
|
67
|
+
export declare function resolveOutputCap(inputs: OutputCapInputs): OutputCap;
|
|
68
|
+
/** A long decode's cap cut so that no continuation request's input passes the model's input window. */
|
|
69
|
+
export interface InputWindowCut {
|
|
70
|
+
/** The cap before the cut: the caller's, clamped to the model's limits. */
|
|
71
|
+
readonly from: number;
|
|
72
|
+
/** The cap the decode runs under. */
|
|
73
|
+
readonly to: number;
|
|
74
|
+
/** The model's input window that cut it. */
|
|
75
|
+
readonly inputTokenLimit: number;
|
|
76
|
+
}
|
|
77
|
+
/** The cap a decode runs under, and the cut the input window made to it (`null`: none). */
|
|
78
|
+
export interface BoundedOutputCap {
|
|
79
|
+
readonly cap: OutputCap;
|
|
80
|
+
readonly cut: InputWindowCut | null;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The cap of a long decode bounded by the model's input window W. Request k sends the prompt P again plus every token
|
|
84
|
+
* decoded before it, and the client opens no further request once the decoded tokens reached the cap: under a cap of
|
|
85
|
+
* at most W − P − O no request's input passes W. The premise — the API refuses a continuation request whose prompt
|
|
86
|
+
* plus decoded tokens pass W — is assumed, not measured: the repository holds a measurement of what a continuation
|
|
87
|
+
* request bills, none of a refusal at the window (docs/FOLLOW-UP-PRS.md T41).
|
|
88
|
+
*
|
|
89
|
+
* The cap is never cut below one request's limit — the first request sends the prompt alone, which the preflight
|
|
90
|
+
* bounds. A cut that leaves several requests keeps the caller's `wire`: the client stops the decode at `effective`,
|
|
91
|
+
* after `maxRequests`. A cut that leaves one request sends the cut value as `maxOutputTokens`, so the server holds it
|
|
92
|
+
* (a stop at it is `MAX_TOKENS`) — one streaming request would otherwise decode past it. Without a counted prompt or
|
|
93
|
+
* an advertised window nothing is cut.
|
|
94
|
+
*/
|
|
95
|
+
export declare function boundByInputWindow(cap: OutputCap, promptTokens: number | undefined, inputTokenLimit: number | null): BoundedOutputCap;
|
|
96
|
+
/** The progress and log line of a cut cap. */
|
|
97
|
+
export declare function describeInputWindowCut(cut: InputWindowCut): string;
|
|
98
|
+
/** What `capThatRuns` bounds: the cap a tool resolved, the prompt's count and the model's input window. */
|
|
99
|
+
export interface CapToBound {
|
|
100
|
+
/** The tool's name, which prefixes the note of a cut. */
|
|
101
|
+
readonly tool: string;
|
|
102
|
+
readonly cap: OutputCap;
|
|
103
|
+
/** The preflight's count of the prompt, when it ran. */
|
|
104
|
+
readonly promptTokens: number | undefined;
|
|
105
|
+
readonly inputTokenLimit: number | null;
|
|
106
|
+
/** The call's progress channel. */
|
|
107
|
+
readonly emit: (message: string) => void;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The cap a call's decode runs under: a long decode never sends a continuation request past the model's input
|
|
111
|
+
* window (`boundByInputWindow`). A cut is said on the call's progress channel and in the log.
|
|
112
|
+
*/
|
|
113
|
+
export declare function capThatRuns(call: CapToBound): BoundedOutputCap;
|
|
114
|
+
/** The answer the output cap keeps beside a thinking budget: Gemini requires `thinkingBudget < maxOutputTokens`. */
|
|
115
|
+
export declare const ANSWER_RESERVE_TOKENS = 1024;
|
|
116
|
+
/**
|
|
117
|
+
* The refusal of a caller's positive `thinkingBudget` that leaves less than the answer reserve under the caller's own
|
|
118
|
+
* `maxOutputTokens` (`cap`: what that cap became under the model's limits) — refused, never clamped to 0, which
|
|
119
|
+
* Gemini 3 Pro answers with a 400 that hides the cause. `null` when it fits, or the caller set either one not.
|
|
120
|
+
*/
|
|
121
|
+
export declare function thinkingBudgetRefusal(budget: number | undefined, requestedCap: number | undefined, cap: number): string | null;
|
|
122
|
+
/** The preflight's count modes (`preflightMode` of `ask` and `code`). */
|
|
123
|
+
export type PreflightMode = NonNullable<PreflightInput['preflightMode']>;
|
|
124
|
+
/**
|
|
125
|
+
* The count mode the preflight runs in. A decode that may take more than one request bills its prompt once per
|
|
126
|
+
* request, so the budget reservation takes the exact count (`countTokens`) whatever mode was asked for; a
|
|
127
|
+
* one-request call keeps the caller's mode.
|
|
128
|
+
*/
|
|
129
|
+
export declare function preflightModeFor(requested: PreflightMode | undefined, cap: OutputCap): PreflightMode | undefined;
|
|
130
|
+
/** The requests a decode of `cap` tokens may take when each request decodes up to `perRequest` tokens. */
|
|
131
|
+
export declare function maxDecodeRequests(cap: number, perRequest: number): number;
|
|
132
|
+
/**
|
|
133
|
+
* The input tokens the continuation requests bill again, at most, for a prompt of `promptTokens` tokens:
|
|
134
|
+
*
|
|
135
|
+
* R = ceil(C / L) requests (the client stops there; L = the per-request limit)
|
|
136
|
+
* M = max(L, 65 536) the most one streaming request decodes
|
|
137
|
+
* O = CONTINUATION_REQUEST_OVERHEAD_TOKENS what a continuation request bills beyond the two terms below
|
|
138
|
+
* re-billed input = Σ_{k=1..R−1} (P + min(k·M, C) + O)
|
|
139
|
+
*
|
|
140
|
+
* Request k sends the prompt P again plus the tokens decoded before it, which are at most k·M and below the cap C
|
|
141
|
+
* (the client opens no request once the decoded tokens reached C). The first request's prompt is not part of this
|
|
142
|
+
* sum.
|
|
143
|
+
*/
|
|
144
|
+
export declare function continuationInputTokens(promptTokens: number, cap: number, perRequest: number): number;
|
|
145
|
+
/**
|
|
146
|
+
* The most a decode of cap C produces (thinking and answer). One request honours `maxOutputTokens` = C. Over several
|
|
147
|
+
* requests C stays the bound only while Google holds `maxOutputTokens` over the whole chain, which the client cannot
|
|
148
|
+
* check: it stops after a request ends, once the decoded tokens reached C, so a server that holds the cap per
|
|
149
|
+
* request lets the last request run up to one request's decode past it — at most C + M − 1, and never more than
|
|
150
|
+
* R requests of M.
|
|
151
|
+
*/
|
|
152
|
+
export declare function decodedTokensBound(cap: number, perRequest: number): number;
|
|
153
|
+
//# sourceMappingURL=long-decode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"long-decode.d.ts","sourceRoot":"","sources":["../../src/gemini/long-decode.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAA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