@qmediat.io/gemini-code-context-mcp 1.23.1 → 1.25.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 +161 -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/agentic/dispatch.d.ts +68 -0
- package/dist/tools/agentic/dispatch.d.ts.map +1 -0
- package/dist/tools/agentic/dispatch.js +167 -0
- package/dist/tools/agentic/dispatch.js.map +1 -0
- package/dist/tools/agentic/input.d.ts +28 -0
- package/dist/tools/agentic/input.d.ts.map +1 -0
- package/dist/tools/agentic/input.js +66 -0
- package/dist/tools/agentic/input.js.map +1 -0
- package/dist/tools/agentic/instructions.d.ts +46 -0
- package/dist/tools/agentic/instructions.d.ts.map +1 -0
- package/dist/tools/agentic/instructions.js +170 -0
- package/dist/tools/agentic/instructions.js.map +1 -0
- package/dist/tools/agentic/iteration.d.ts +81 -0
- package/dist/tools/agentic/iteration.d.ts.map +1 -0
- package/dist/tools/agentic/iteration.js +317 -0
- package/dist/tools/agentic/iteration.js.map +1 -0
- package/dist/tools/agentic/ledger.d.ts +64 -0
- package/dist/tools/agentic/ledger.d.ts.map +1 -0
- package/dist/tools/agentic/ledger.js +110 -0
- package/dist/tools/agentic/ledger.js.map +1 -0
- package/dist/tools/agentic/loop.d.ts +10 -0
- package/dist/tools/agentic/loop.d.ts.map +1 -0
- package/dist/tools/agentic/loop.js +179 -0
- package/dist/tools/agentic/loop.js.map +1 -0
- package/dist/tools/agentic/no-progress.d.ts +90 -0
- package/dist/tools/agentic/no-progress.d.ts.map +1 -0
- package/dist/tools/agentic/no-progress.js +107 -0
- package/dist/tools/agentic/no-progress.js.map +1 -0
- package/dist/tools/agentic/pass-clock.d.ts +85 -0
- package/dist/tools/agentic/pass-clock.d.ts.map +1 -0
- package/dist/tools/agentic/pass-clock.js +162 -0
- package/dist/tools/agentic/pass-clock.js.map +1 -0
- package/dist/tools/agentic/phase-defaults.d.ts +29 -0
- package/dist/tools/agentic/phase-defaults.d.ts.map +1 -0
- package/dist/tools/agentic/phase-defaults.js +23 -0
- package/dist/tools/agentic/phase-defaults.js.map +1 -0
- package/dist/tools/agentic/rescue.d.ts +69 -0
- package/dist/tools/agentic/rescue.d.ts.map +1 -0
- package/dist/tools/agentic/rescue.js +203 -0
- package/dist/tools/agentic/rescue.js.map +1 -0
- package/dist/tools/agentic/response.d.ts +16 -0
- package/dist/tools/agentic/response.d.ts.map +1 -0
- package/dist/tools/agentic/response.js +25 -0
- package/dist/tools/agentic/response.js.map +1 -0
- package/dist/tools/agentic/results.d.ts +76 -0
- package/dist/tools/agentic/results.d.ts.map +1 -0
- package/dist/tools/agentic/results.js +228 -0
- package/dist/tools/agentic/results.js.map +1 -0
- package/dist/tools/agentic/run.d.ts +130 -0
- package/dist/tools/agentic/run.d.ts.map +1 -0
- package/dist/tools/agentic/run.js +131 -0
- package/dist/tools/agentic/run.js.map +1 -0
- package/dist/tools/agentic/sandbox.d.ts +13 -2
- package/dist/tools/agentic/sandbox.d.ts.map +1 -1
- package/dist/tools/agentic/sandbox.js +127 -111
- package/dist/tools/agentic/sandbox.js.map +1 -1
- package/dist/tools/agentic/workspace-tools.d.ts +30 -27
- package/dist/tools/agentic/workspace-tools.d.ts.map +1 -1
- package/dist/tools/agentic/workspace-tools.js +324 -385
- package/dist/tools/agentic/workspace-tools.js.map +1 -1
- package/dist/tools/ask-agentic-fallback.d.ts +33 -0
- package/dist/tools/ask-agentic-fallback.d.ts.map +1 -0
- package/dist/tools/ask-agentic-fallback.js +113 -0
- package/dist/tools/ask-agentic-fallback.js.map +1 -0
- package/dist/tools/ask-agentic.tool.d.ts +11 -44
- package/dist/tools/ask-agentic.tool.d.ts.map +1 -1
- package/dist/tools/ask-agentic.tool.js +143 -1618
- 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 +195 -555
- 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 +155 -306
- package/dist/tools/code.tool.js.map +1 -1
- package/dist/tools/shared/abort-timeout.d.ts +48 -4
- package/dist/tools/shared/abort-timeout.d.ts.map +1 -1
- package/dist/tools/shared/abort-timeout.js +116 -95
- package/dist/tools/shared/abort-timeout.js.map +1 -1
- package/dist/tools/shared/call-ledger.d.ts +88 -0
- package/dist/tools/shared/call-ledger.d.ts.map +1 -0
- package/dist/tools/shared/call-ledger.js +133 -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/question-fields.d.ts +10 -0
- package/dist/tools/shared/question-fields.d.ts.map +1 -0
- package/dist/tools/shared/question-fields.js +13 -0
- package/dist/tools/shared/question-fields.js.map +1 -0
- package/dist/tools/shared/settle-call.d.ts +68 -0
- package/dist/tools/shared/settle-call.d.ts.map +1 -0
- package/dist/tools/shared/settle-call.js +59 -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/tools/shared/throttle.d.ts +7 -0
- package/dist/tools/shared/throttle.d.ts.map +1 -1
- package/dist/tools/shared/throttle.js +18 -0
- package/dist/tools/shared/throttle.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
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ledger side of an `ask` / `code` call: what its completed requests billed, and how the call settles its budget
|
|
3
|
+
* reservation — on an answer, and on a failure.
|
|
4
|
+
*
|
|
5
|
+
* The ledger books completed requests: every request that streamed to its end was billed, so its reported usage, at
|
|
6
|
+
* list price, stays in the ledger whatever becomes of the call; only the unspent remainder of the reservation is
|
|
7
|
+
* released. A call that fails before any request completed releases its reservation whole — whether Google bills a
|
|
8
|
+
* request aborted after its stream opened is not measured, and the ledger books it 0 (`docs/cost-model.md`).
|
|
9
|
+
*/
|
|
10
|
+
import { estimateCostUsd, toMicrosUsd } from '../../utils/cost-estimator.js';
|
|
11
|
+
import { logger, safeForLog } from '../../utils/logger.js';
|
|
12
|
+
const tokens = (n) => (typeof n === 'number' ? n : 0);
|
|
13
|
+
/**
|
|
14
|
+
* The input a request that went through is billed at: the prompt tokens it reported, else — a response that carried
|
|
15
|
+
* no usage — the input it was estimated to send. Under-billing a request the API completed is the worse error, so it
|
|
16
|
+
* is booked at what its reservation covered, never 0. The estimate bounds the real input for `ask` / `code`; for an
|
|
17
|
+
* `ask_agentic` loop iteration it is a fixed 50,000 tokens and can be below it (`docs/cost-model.md`).
|
|
18
|
+
*/
|
|
19
|
+
export function billedInputTokens(reported, estimate) {
|
|
20
|
+
return typeof reported === 'number' && reported > 0 ? reported : estimate;
|
|
21
|
+
}
|
|
22
|
+
/** The tokens a (summed) usage reports and their cost: the one cost function of a settled call. */
|
|
23
|
+
export function billedUsage(usage, model, serviceTier) {
|
|
24
|
+
const prompt = tokens(usage?.promptTokenCount);
|
|
25
|
+
const cached = tokens(usage?.cachedContentTokenCount);
|
|
26
|
+
const uncached = Math.max(0, prompt - cached);
|
|
27
|
+
const output = tokens(usage?.candidatesTokenCount);
|
|
28
|
+
const thinking = tokens(usage?.thoughtsTokenCount);
|
|
29
|
+
const costUsd = estimateCostUsd({
|
|
30
|
+
model,
|
|
31
|
+
serviceTier,
|
|
32
|
+
uncachedInputTokens: uncached,
|
|
33
|
+
cachedInputTokens: cached,
|
|
34
|
+
outputTokens: output,
|
|
35
|
+
thinkingTokens: thinking,
|
|
36
|
+
});
|
|
37
|
+
return {
|
|
38
|
+
cachedTokens: cached,
|
|
39
|
+
uncachedTokens: uncached,
|
|
40
|
+
outputTokens: output,
|
|
41
|
+
thinkingTokens: thinking,
|
|
42
|
+
promptTokens: prompt,
|
|
43
|
+
costUsd,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** A billed usage as a result reports it, the cost rounded to 4 decimals. */
|
|
47
|
+
export function billedFields(billed) {
|
|
48
|
+
return {
|
|
49
|
+
cachedTokens: billed.cachedTokens,
|
|
50
|
+
uncachedTokens: billed.uncachedTokens,
|
|
51
|
+
outputTokens: billed.outputTokens,
|
|
52
|
+
thinkingTokens: billed.thinkingTokens,
|
|
53
|
+
costEstimateUsd: Math.round(billed.costUsd * 10000) / 10000,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export function usageFields(billed, continuationHops) {
|
|
57
|
+
return { ...billedFields(billed), continuationHops };
|
|
58
|
+
}
|
|
59
|
+
function settledRowOf(entry, billed, durationMs) {
|
|
60
|
+
return {
|
|
61
|
+
cachedTokens: billed.cachedTokens,
|
|
62
|
+
uncachedTokens: billed.uncachedTokens,
|
|
63
|
+
costUsdMicro: toMicrosUsd(billed.costUsd),
|
|
64
|
+
durationMs: Math.max(1, durationMs),
|
|
65
|
+
...(entry.cachingMode === null
|
|
66
|
+
? {}
|
|
67
|
+
: { cachingMode: entry.cachingMode, cachedContentTokenCount: billed.cachedTokens }),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/** What the log says of a booking that could not be written: what the ledger holds instead. */
|
|
71
|
+
function unwrittenBookingLine(label, reservationId, costUsdMicro, err) {
|
|
72
|
+
const held = reservationId === null
|
|
73
|
+
? 'insertUsageMetric failed; no usage row was written'
|
|
74
|
+
: `finalizeBudgetReservation failed for id=${reservationId}; the row keeps its reserved estimate`;
|
|
75
|
+
return `${label}: ${held}, the billed ${costUsdMicro} micros were not written. Error: ${safeForLog(err)}`;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Books a call at what it billed: its reservation finalised at that cost (the rest released), or — with no daily cap,
|
|
79
|
+
* so no reservation — a usage row. The booking is best-effort and never throws: it runs once the call's work is
|
|
80
|
+
* billed (an answer exists, or a request completed), and a failed write must not lose that answer nor replace the
|
|
81
|
+
* call's own error. A finalise that throws leaves the reservation's estimate booked; an insert that throws leaves no
|
|
82
|
+
* row. Either is logged.
|
|
83
|
+
*/
|
|
84
|
+
export function recordBilledCall(manifest, reservationId, entry, billed, durationMs, label = entry.toolName) {
|
|
85
|
+
const settled = settledRowOf(entry, billed, durationMs);
|
|
86
|
+
try {
|
|
87
|
+
if (reservationId === null) {
|
|
88
|
+
manifest.insertUsageMetric({
|
|
89
|
+
workspaceRoot: entry.workspaceRoot,
|
|
90
|
+
toolName: entry.toolName,
|
|
91
|
+
model: entry.model,
|
|
92
|
+
occurredAt: Date.now(),
|
|
93
|
+
...settled,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
manifest.finalizeBudgetReservation(reservationId, settled);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
catch (writeErr) {
|
|
101
|
+
logger.error(unwrittenBookingLine(label, reservationId, settled.costUsdMicro, writeErr));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Settles the ledger of a failed call. With no request completed the reservation is released whole; after one or
|
|
106
|
+
* more, the ledger keeps what they billed and the result gets their usage and the continuation requests whose
|
|
107
|
+
* stream opened.
|
|
108
|
+
* A ledger write that throws is logged and never replaces the call's own error.
|
|
109
|
+
*/
|
|
110
|
+
export function settleFailedCall(manifest, failed) {
|
|
111
|
+
if (failed.entry === null || failed.completed.count === 0) {
|
|
112
|
+
releaseReservation(manifest, failed.toolName, failed.reservationId);
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
const billed = billedUsage(failed.completed.usage(), failed.entry.model, failed.entry.serviceTier);
|
|
116
|
+
recordBilledCall(manifest, failed.reservationId, failed.entry, billed, failed.durationMs);
|
|
117
|
+
return usageFields(billed, failed.completed.continuationHops());
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Cancel a budget reservation (`null`: none was taken). A cancel that throws leaves the row at its estimate until the
|
|
121
|
+
* day ends; it is logged under `label`.
|
|
122
|
+
*/
|
|
123
|
+
export function releaseReservation(manifest, label, reservationId) {
|
|
124
|
+
if (reservationId === null)
|
|
125
|
+
return;
|
|
126
|
+
try {
|
|
127
|
+
manifest.cancelBudgetReservation(reservationId);
|
|
128
|
+
}
|
|
129
|
+
catch (cancelErr) {
|
|
130
|
+
logger.error(`${label}: cancelBudgetReservation failed for id=${reservationId}; reservation row keeps the estimate. Error: ${safeForLog(cancelErr)}`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=call-ledger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"call-ledger.js","sourceRoot":"","sources":["../../../src/tools/shared/call-ledger.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAC7E,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAoD3D,MAAM,MAAM,GAAG,CAAC,CAAqB,EAAU,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAElF;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,QAA4B,EAAE,QAAgB;IAC9E,OAAO,OAAO,QAAQ,KAAK,QAAQ,IAAI,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC;AAC5E,CAAC;AAED,mGAAmG;AACnG,MAAM,UAAU,WAAW,CACzB,KAA8B,EAC9B,KAAa,EACb,WAA4C;IAE5C,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,EAAE,gBAAgB,CAAC,CAAC;IAC/C,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,EAAE,uBAAuB,CA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**
|
|
2
|
+
* The decode of an `ask` / `code` call on its prepared context — the one place both tools run it.
|
|
3
|
+
*
|
|
4
|
+
* - Ledger: the booking of the running decode stays in `tracking`, which the call's failure path reads.
|
|
5
|
+
* - Cache: before each request is sent, the cache the decode reads is marked hot and, when it expires within the
|
|
6
|
+
* watcher's refresh window (15 minutes), extended at once (`TtlWatcher.keepAlive`) — so no request of a decode
|
|
7
|
+
* (minutes each: 188–241 s measured) is sent on a cache that expires before it ends; the watcher's own tick only
|
|
8
|
+
* refreshes caches used in its last 10 minutes, every 5.
|
|
9
|
+
* - Throttle: each request holds its own TPM reservation (`DispatchThrottle`).
|
|
10
|
+
* - Stall watchdog: paused when a request's stream ends; the next request's first chunk re-arms it.
|
|
11
|
+
* - Stale cache: a cache Gemini rejects is rebuilt and the decode run again ONCE, only while no request of the decode
|
|
12
|
+
* has completed. After a completed request the error goes to the caller with the completed requests booked: a
|
|
13
|
+
* restart would bill the whole decode again under a budget reservation sized for one decode.
|
|
14
|
+
*/
|
|
15
|
+
import type { GenerateContentConfig, GenerateContentParameters, GoogleGenAI } from '@google/genai';
|
|
16
|
+
import { type PreparedContext } from '../../cache/cache-manager.js';
|
|
17
|
+
import type { RanCachingMode } from '../../cache/caching-mode.js';
|
|
18
|
+
import type { TtlWatcher } from '../../cache/ttl-watcher.js';
|
|
19
|
+
import type { ManifestDb } from '../../manifest/db.js';
|
|
20
|
+
import { type TimeoutController } from './abort-timeout.js';
|
|
21
|
+
import type { LedgerEntry } from './call-ledger.js';
|
|
22
|
+
import { type CompletedRequests, type DecodeLimits, type DecodedResponse } from './continuation.js';
|
|
23
|
+
import type { DispatchThrottle } from './dispatch-throttle.js';
|
|
24
|
+
/** What the call's failure path reads of its running decode. */
|
|
25
|
+
export interface DecodeTracking {
|
|
26
|
+
/** The booking of the running decode; `null` before one starts and once the call has settled. */
|
|
27
|
+
ledgerEntry: LedgerEntry | null;
|
|
28
|
+
/** A request's stream opened: a later transport failure is mid-stream, billed work. */
|
|
29
|
+
streamOpened: boolean;
|
|
30
|
+
}
|
|
31
|
+
/** The request of a decode on one prepared context. */
|
|
32
|
+
export interface ContextRequest {
|
|
33
|
+
readonly contents: GenerateContentParameters['contents'];
|
|
34
|
+
readonly config: GenerateContentConfig;
|
|
35
|
+
}
|
|
36
|
+
/** The workspace cache a decode reads, and who keeps it alive. */
|
|
37
|
+
export interface CacheUpkeep {
|
|
38
|
+
readonly ttlWatcher: Pick<TtlWatcher, 'markHot' | 'keepAlive'>;
|
|
39
|
+
readonly manifest: ManifestDb;
|
|
40
|
+
readonly ttlSeconds: number;
|
|
41
|
+
/** Builds the context again (on a fresh cache): the call's own `prepareContext`. */
|
|
42
|
+
readonly prepare: () => Promise<PreparedContext>;
|
|
43
|
+
}
|
|
44
|
+
export interface ContextDecodeCall {
|
|
45
|
+
readonly client: GoogleGenAI;
|
|
46
|
+
/** The call's booking but its caching mode, which is the prepared context's. */
|
|
47
|
+
readonly booking: Omit<LedgerEntry, 'cachingMode'>;
|
|
48
|
+
readonly cachingMode: (prep: PreparedContext) => RanCachingMode;
|
|
49
|
+
readonly request: (prep: PreparedContext) => ContextRequest;
|
|
50
|
+
readonly cache: CacheUpkeep;
|
|
51
|
+
/** Attempts to OPEN each request (`networkAttempts` of the tier). */
|
|
52
|
+
readonly openAttempts: number;
|
|
53
|
+
readonly limits: DecodeLimits;
|
|
54
|
+
readonly timeout: TimeoutController;
|
|
55
|
+
/** The call's progress channel. */
|
|
56
|
+
readonly emit: (message: string) => void;
|
|
57
|
+
readonly completed: CompletedRequests;
|
|
58
|
+
readonly tracking: DecodeTracking;
|
|
59
|
+
readonly throttle: DispatchThrottle;
|
|
60
|
+
readonly inputTokens: FirstRequestInput;
|
|
61
|
+
}
|
|
62
|
+
/** What the first request's input is estimated at, before it reports its usage. */
|
|
63
|
+
export interface FirstRequestInput {
|
|
64
|
+
/** The bytes/4 heuristic: what the request's throttle reservation holds. */
|
|
65
|
+
readonly estimated: number;
|
|
66
|
+
/** The preflight's count, when it ran. */
|
|
67
|
+
readonly counted: number | undefined;
|
|
68
|
+
}
|
|
69
|
+
export interface ContextDecode {
|
|
70
|
+
readonly response: DecodedResponse;
|
|
71
|
+
/** The context the answer was decoded on: the prepared one, or the one rebuilt after a stale cache. */
|
|
72
|
+
readonly prep: PreparedContext;
|
|
73
|
+
readonly retriedOnStaleCache: boolean;
|
|
74
|
+
}
|
|
75
|
+
/** Decode on `initial`; on a stale cache before any request completed, once more on a rebuilt context. */
|
|
76
|
+
export declare function decodeOnContext(call: ContextDecodeCall, initial: PreparedContext): Promise<ContextDecode>;
|
|
77
|
+
//# sourceMappingURL=context-decode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-decode.d.ts","sourceRoot":"","sources":["../../../src/tools/shared/context-decode.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EAAE,qBAAqB,EAAE,yBAAyB,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACnG,OAAO,EACL,KAAK,eAAe,EAGrB,MAAM,8BAA8B,CAAC;AACtC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAGvD,OAAO,EAAE,KAAK,iBAAiB,EAAkB,MAAM,oBAAoB,CAAC;AAC5E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EACL,KAAK,iBAAiB,EAEtB,KAAK,YAAY,EACjB,KAAK,eAAe,EAErB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAG/D,gEAAgE;AAChE,MAAM,WAAW,cAAc;IAC7B,iGAAiG;IACjG,WAAW,EAAE,WAAW,GAAG,IAAI,CAAC;IAChC,uFA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AQ,CAAC,WAAW,EAAE,iBAAiB,CAAC;CACzC;AAED,mFAAmF;AACnF,MAAM,WAAW,iBAAiB;IAChC,4EAA4E;IAC5E,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,0CAA0C;IAC1C,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;CACtC;AAED,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,QAAQ,EAAE,eAAe,CAAC;IACnC,uGAAuG;IACvG,QAAQ,CAAC,IAAI,EAAE,eAAe,CAAC;IAC/B,QAAQ,CAAC,mBAAmB,EAAE,OAAO,CAAC;CACvC;AAED,0GAA0G;AAC1G,wBAAsB,eAAe,CACnC,IAAI,EAAE,iBAAiB,EACvB,OAAO,EAAE,eAAe,GACvB,OAAO,CAAC,aAAa,CAAC,CAmBxB"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The decode of an `ask` / `code` call on its prepared context — the one place both tools run it.
|
|
3
|
+
*
|
|
4
|
+
* - Ledger: the booking of the running decode stays in `tracking`, which the call's failure path reads.
|
|
5
|
+
* - Cache: before each request is sent, the cache the decode reads is marked hot and, when it expires within the
|
|
6
|
+
* watcher's refresh window (15 minutes), extended at once (`TtlWatcher.keepAlive`) — so no request of a decode
|
|
7
|
+
* (minutes each: 188–241 s measured) is sent on a cache that expires before it ends; the watcher's own tick only
|
|
8
|
+
* refreshes caches used in its last 10 minutes, every 5.
|
|
9
|
+
* - Throttle: each request holds its own TPM reservation (`DispatchThrottle`).
|
|
10
|
+
* - Stall watchdog: paused when a request's stream ends; the next request's first chunk re-arms it.
|
|
11
|
+
* - Stale cache: a cache Gemini rejects is rebuilt and the decode run again ONCE, only while no request of the decode
|
|
12
|
+
* has completed. After a completed request the error goes to the caller with the completed requests booked: a
|
|
13
|
+
* restart would bill the whole decode again under a budget reservation sized for one decode.
|
|
14
|
+
*/
|
|
15
|
+
import { isStaleCacheError, markCacheStale, } from '../../cache/cache-manager.js';
|
|
16
|
+
import { requestInputTokens } from '../../utils/cost-estimator.js';
|
|
17
|
+
import { logger } from '../../utils/logger.js';
|
|
18
|
+
import { isTimeoutAbort } from './abort-timeout.js';
|
|
19
|
+
import { runDecode, } from './continuation.js';
|
|
20
|
+
import { statusOf } from './service-tier.js';
|
|
21
|
+
/** Decode on `initial`; on a stale cache before any request completed, once more on a rebuilt context. */
|
|
22
|
+
export async function decodeOnContext(call, initial) {
|
|
23
|
+
await call.throttle.reserve(call.booking.model, call.inputTokens.estimated);
|
|
24
|
+
let decoded;
|
|
25
|
+
try {
|
|
26
|
+
const response = await decodeOn(call, initial, call.booking.toolName);
|
|
27
|
+
decoded = { response, prep: initial, retriedOnStaleCache: false };
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
if (!initial.cacheId || !isStaleCacheError(err))
|
|
31
|
+
throw err;
|
|
32
|
+
decoded = await retryOnFreshCache(call, initial.cacheId, err);
|
|
33
|
+
}
|
|
34
|
+
// used just now: the watcher keeps it warm for the calls that follow
|
|
35
|
+
if (decoded.prep.cacheId) {
|
|
36
|
+
call.cache.ttlWatcher.markHot(call.booking.workspaceRoot, decoded.prep.cacheId, call.cache.ttlSeconds);
|
|
37
|
+
}
|
|
38
|
+
return decoded;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* A request is about to be sent on `prep`: its cache, if it reads one, is kept alive for it — under the call's
|
|
42
|
+
* signal, so a timeout during the refresh ends the call as a timeout before the request is opened.
|
|
43
|
+
*/
|
|
44
|
+
async function keepAlive(call, prep) {
|
|
45
|
+
if (!prep.cacheId)
|
|
46
|
+
return;
|
|
47
|
+
await call.cache.ttlWatcher.keepAlive(call.booking.workspaceRoot, prep.cacheId, call.cache.ttlSeconds, call.timeout.signal);
|
|
48
|
+
}
|
|
49
|
+
async function decodeOn(call, prep, label) {
|
|
50
|
+
call.tracking.ledgerEntry = { ...call.booking, cachingMode: call.cachingMode(prep) };
|
|
51
|
+
await keepAlive(call, prep);
|
|
52
|
+
const { contents, config } = call.request(prep);
|
|
53
|
+
return runDecode({
|
|
54
|
+
client: call.client,
|
|
55
|
+
model: call.booking.model,
|
|
56
|
+
contents,
|
|
57
|
+
config,
|
|
58
|
+
openAttempts: call.openAttempts,
|
|
59
|
+
label,
|
|
60
|
+
limits: call.limits,
|
|
61
|
+
collector: collectorOf(call),
|
|
62
|
+
onRequestOpened: () => {
|
|
63
|
+
call.tracking.streamOpened = true;
|
|
64
|
+
},
|
|
65
|
+
completed: call.completed,
|
|
66
|
+
beforeReopen: () => call.throttle.renew(),
|
|
67
|
+
hooks: {
|
|
68
|
+
// a request that reports no usage is billed at the reservation's own input, so the ledger stays inside it
|
|
69
|
+
firstRequestInput: requestInputTokens(call.inputTokens.estimated, call.inputTokens.counted),
|
|
70
|
+
afterRequest: (usage) => {
|
|
71
|
+
call.timeout.pauseStall();
|
|
72
|
+
call.throttle.settle(usage?.promptTokenCount);
|
|
73
|
+
},
|
|
74
|
+
beforeContinuation: async (inputTokens) => {
|
|
75
|
+
await keepAlive(call, prep);
|
|
76
|
+
await call.throttle.reserve(call.booking.model, inputTokens);
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/** The collector of every request: the call's signal, its thinking on the progress channel, the stall watchdog. */
|
|
82
|
+
function collectorOf(call) {
|
|
83
|
+
return {
|
|
84
|
+
signal: call.timeout.signal,
|
|
85
|
+
// The model's reasoning surfaces live, throttled by the collector so the progress channel is not flooded.
|
|
86
|
+
onThoughtChunk: (text) => {
|
|
87
|
+
const trimmed = text.trim().slice(0, 80);
|
|
88
|
+
if (trimmed.length > 0)
|
|
89
|
+
call.emit(`thinking: ${trimmed}…`);
|
|
90
|
+
},
|
|
91
|
+
// Every chunk (text or thought) of every request proves the stream alive.
|
|
92
|
+
onChunkReceived: () => call.timeout.recordChunk(),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
async function retryOnFreshCache(call, staleCacheId, staleErr) {
|
|
96
|
+
const tool = call.booking.toolName;
|
|
97
|
+
const completedInDecode = call.completed.completedInLatestDecode;
|
|
98
|
+
if (completedInDecode > 0) {
|
|
99
|
+
markCacheStale({ manifest: call.cache.manifest, workspaceRoot: call.booking.workspaceRoot });
|
|
100
|
+
throw new Error(`${tool}: the context cache ${staleCacheId} expired during a long decode, after ${completedInDecode} completed request(s); the decode is not restarted (a restart would bill every request again). The completed requests are billed and reported; call again to build a fresh cache.`, { cause: staleErr });
|
|
101
|
+
}
|
|
102
|
+
logger.warn(`Gemini rejected cached content ${staleCacheId}; invalidating and retrying once.`);
|
|
103
|
+
try {
|
|
104
|
+
// The pointer only: the file rows stay, so the rebuild reuses uploaded files by content hash.
|
|
105
|
+
markCacheStale({ manifest: call.cache.manifest, workspaceRoot: call.booking.workspaceRoot });
|
|
106
|
+
const rebuilt = await call.cache.prepare();
|
|
107
|
+
// A fresh reservation, stamped when the retry is sent.
|
|
108
|
+
await call.throttle.reserve(call.booking.model, call.inputTokens.estimated);
|
|
109
|
+
const response = await decodeOn(call, rebuilt, `${tool} (stale-cache retry)`);
|
|
110
|
+
return { response, prep: rebuilt, retriedOnStaleCache: true };
|
|
111
|
+
}
|
|
112
|
+
catch (retryErr) {
|
|
113
|
+
throw retryFailure(tool, retryErr, staleErr);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The error of a failed stale-cache retry: a timeout and an HTTP error (a 429 / 503, whose status and retry hint the
|
|
118
|
+
* caller reads) as they came; anything else wrapped over the stale-cache error, which stays its cause.
|
|
119
|
+
*/
|
|
120
|
+
function retryFailure(tool, retryErr, staleErr) {
|
|
121
|
+
if (isTimeoutAbort(retryErr) || statusOf(retryErr) !== undefined)
|
|
122
|
+
return retryErr;
|
|
123
|
+
return new Error(`${tool} retry after stale cache failed: ${retryErr instanceof Error ? retryErr.message : String(retryErr)}`, { cause: staleErr });
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=context-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IAC/C,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,SAAS,YAAY,CAAC,IAAY,EAAE,QAAiB,EAAE,QAAiB;IACtE,IAAI,cAAc,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC,QAAQ,CAAC,KAAK,SAAS;QAAE,OAAO,QAAQ,CAAC;IAClF,OAAO,IAAI,KAAK,CACd,GAAG,IAAI,oCACL,QAAQ,YAAY,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAChE,EAAE,EACF,EAAE,KAAK,EAAE,QAAQ,EAAE,CACpB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One decode over one or more requests (Gemini 4 long decode, `src/gemini/long-decode.ts`).
|
|
3
|
+
*
|
|
4
|
+
* A request that stops with `finishReason: CONTINUATION` and a continuation token is followed by the same request
|
|
5
|
+
* with `continuationToken` set to the latest token — the payload is never changed and the text decoded so far is
|
|
6
|
+
* never appended to it. Every request is sent with `automaticContinuation: false`, so the SDK hands each request's
|
|
7
|
+
* end back here: this loop counts the requests, stops at the cap the budget reservation assumed, and
|
|
8
|
+
* never re-sends a request that already streamed (billed) work.
|
|
9
|
+
*
|
|
10
|
+
* The requests share the caller's abort signal and chunk callback, so `timeoutMs` covers the whole decode and the
|
|
11
|
+
* stall watchdog every gap between two chunks of a request; the caller's `afterRequest` hook pauses the watchdog
|
|
12
|
+
* between requests, as before the first. A request that made no progress (the same token again, nothing decoded)
|
|
13
|
+
* is not continued. The result is ONE response: the text and thought summary of every request in order, the usage
|
|
14
|
+
* summed over the requests (each one bills its own input again), the last request's finish reason.
|
|
15
|
+
*/
|
|
16
|
+
import { type GenerateContentConfig, type GenerateContentParameters, type GenerateContentResponse, type GoogleGenAI } from '@google/genai';
|
|
17
|
+
import { type CollectedResponse, type StreamCollectorOptions } from './stream-collector.js';
|
|
18
|
+
/** The long-decode sentences of the `maxOutputTokens` description of `ask` and `code`. */
|
|
19
|
+
export declare const LONG_DECODE_CAP_DESCRIPTION = "On a model with long decode (Gemini 4: Argon, Carbon, Hydrogen) a value above its per-request limit is sent up to 1,048,576: a response that reaches the per-request limit continues in further requests until the model stops or the cap is reached, and the result's `continuationHops` counts them. Each continuation request bills the prompt again plus everything decoded before it, so a long decode costs more than one request; the budget reservation counts that input too. Use it when a high thinking level leaves no room for the answer (`errorCode: 'OUTPUT_CAP'`, thinking tokens count against the cap). An answer cut at the cap comes back with `truncated: true` and its `finishReason`.";
|
|
20
|
+
/** One request's stream, as the SDK opens it. */
|
|
21
|
+
type DecodeStream = AsyncGenerator<GenerateContentResponse>;
|
|
22
|
+
/** Opens one request of the decode: the first with no token, each continuation with the latest one. */
|
|
23
|
+
export type OpenDecodeRequest = (continuationToken: string | undefined) => Promise<DecodeStream>;
|
|
24
|
+
/** Where the decode stops at the latest: `OutputCap.maxRequests` requests, `OutputCap.effective` decoded tokens. */
|
|
25
|
+
export interface DecodeLimits {
|
|
26
|
+
readonly maxRequests: number;
|
|
27
|
+
readonly cap: number;
|
|
28
|
+
}
|
|
29
|
+
export interface DecodedResponse extends CollectedResponse {
|
|
30
|
+
/** The requests after the first one: 0 for a call that ended in one request. */
|
|
31
|
+
readonly continuationHops: number;
|
|
32
|
+
}
|
|
33
|
+
/** One decode as `ask` and `code` run it: the request, how its opening is retried, and the decode's limits. */
|
|
34
|
+
export interface DecodeCall {
|
|
35
|
+
readonly client: GoogleGenAI;
|
|
36
|
+
readonly model: string;
|
|
37
|
+
readonly contents: GenerateContentParameters['contents'];
|
|
38
|
+
/** The request config; `automaticContinuation: false` is set on it here, the abort signal is the collector's. */
|
|
39
|
+
readonly config: GenerateContentConfig;
|
|
40
|
+
/**
|
|
41
|
+
* SDK calls that OPEN each request (a failure before any response), network retries and reopenings after a refusal
|
|
42
|
+
* together; a stream that opened is never re-sent.
|
|
43
|
+
*/
|
|
44
|
+
readonly openAttempts: number;
|
|
45
|
+
/** The log prefix of a retried opening (`ask`, `code (stale-cache retry)`). */
|
|
46
|
+
readonly label: string;
|
|
47
|
+
readonly limits: DecodeLimits;
|
|
48
|
+
/** The abort signal (required: it reaches every request), the thought and chunk callbacks. */
|
|
49
|
+
readonly collector: StreamCollectorOptions & {
|
|
50
|
+
readonly signal: AbortSignal;
|
|
51
|
+
};
|
|
52
|
+
/** Called each time a request's stream opened: from then on the request is never re-sent. */
|
|
53
|
+
readonly onRequestOpened: () => void;
|
|
54
|
+
/** The call's record of its requests, kept across its decodes and its failures. */
|
|
55
|
+
readonly completed: CompletedRequests;
|
|
56
|
+
/** What the caller does between the requests (`DecodeHooks`, its `completed` is the one above). */
|
|
57
|
+
readonly hooks?: Omit<DecodeHooks, 'completed'>;
|
|
58
|
+
/**
|
|
59
|
+
* A refused continuation request is about to be reopened, its wait over: what the request holds for the window it
|
|
60
|
+
* is sent in (the per-minute reservation) is taken again. A rejection ends the decode before the reopening.
|
|
61
|
+
*/
|
|
62
|
+
readonly beforeReopen?: () => Promise<void>;
|
|
63
|
+
}
|
|
64
|
+
/** What runs between the requests of a decode. */
|
|
65
|
+
export interface DecodeHooks {
|
|
66
|
+
/**
|
|
67
|
+
* The input the decode's first request is estimated to send: a request that reports no usage is billed at it, plus
|
|
68
|
+
* the tokens decoded before the request (`billedInputTokens`).
|
|
69
|
+
*/
|
|
70
|
+
readonly firstRequestInput?: number;
|
|
71
|
+
/** The call's record of its requests: each one that streamed to its end goes in at once. */
|
|
72
|
+
readonly completed?: CompletedRequests;
|
|
73
|
+
/** A request streamed to its end, with the usage it reported. */
|
|
74
|
+
readonly afterRequest?: (usage: DecodeUsage | undefined) => void;
|
|
75
|
+
/**
|
|
76
|
+
* A continuation request is about to open, with the input it sends: the first request's prompt plus every token
|
|
77
|
+
* decoded so far. It may wait (the per-minute window); a rejection ends the decode before the request is sent.
|
|
78
|
+
*/
|
|
79
|
+
readonly beforeContinuation?: (inputTokens: number) => Promise<void>;
|
|
80
|
+
}
|
|
81
|
+
/** The usage a request reports for itself. */
|
|
82
|
+
export type DecodeUsage = NonNullable<GenerateContentResponse['usageMetadata']>;
|
|
83
|
+
/**
|
|
84
|
+
* Every request of one call that streamed to its end. Each one is billed whatever becomes of the call — an answer,
|
|
85
|
+
* a later request that fails, a stale-cache retry that starts a new decode — so the call settles its ledger from
|
|
86
|
+
* here, never from the answer alone. A request that reported no input is billed at the input it was estimated to
|
|
87
|
+
* send (`billedInputTokens`).
|
|
88
|
+
*/
|
|
89
|
+
export declare class CompletedRequests {
|
|
90
|
+
private readonly billed;
|
|
91
|
+
private total;
|
|
92
|
+
private completedInDecode;
|
|
93
|
+
private sentInDecode;
|
|
94
|
+
/** A new decode of the call starts: its requests are counted from zero. */
|
|
95
|
+
beginDecode(): void;
|
|
96
|
+
/** A request of the latest decode went to the API (its stream opened or the API refused it). */
|
|
97
|
+
sent(): void;
|
|
98
|
+
/** A request streamed to its end with `usage`; `inputEstimate` is the input it was estimated to send. */
|
|
99
|
+
record(usage: DecodeUsage | undefined, inputEstimate: number): void;
|
|
100
|
+
/** The requests of the call that completed, over all its decodes. */
|
|
101
|
+
get count(): number;
|
|
102
|
+
/** The requests of the latest decode that completed. */
|
|
103
|
+
get completedInLatestDecode(): number;
|
|
104
|
+
/**
|
|
105
|
+
* The continuation requests the latest decode sent: every request that went to the API, but the first — whether
|
|
106
|
+
* the decode answered, failed in a request, or stopped before the next request was sent.
|
|
107
|
+
*/
|
|
108
|
+
continuationHops(): number;
|
|
109
|
+
/** Each counter summed over the completed requests that have it; `undefined` before any request completed. */
|
|
110
|
+
usage(): DecodeUsage | undefined;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Run the decode: each request opens through `withNetworkRetry` (only the opening is retried — a mid-stream
|
|
114
|
+
* failure would bill the work again), a continuation request refused at its opening is reopened with the same token
|
|
115
|
+
* (`reopenRefused`), and the requests are collected into one response.
|
|
116
|
+
*/
|
|
117
|
+
export declare function runDecode(call: DecodeCall): Promise<DecodedResponse>;
|
|
118
|
+
/**
|
|
119
|
+
* Collect the decode: one request, plus a continuation request after each one that stopped with CONTINUATION.
|
|
120
|
+
* Each request that streamed to its end goes into `completed` at once: a later request's failure leaves it there.
|
|
121
|
+
*/
|
|
122
|
+
export declare function collectDecode(open: OpenDecodeRequest, limits: DecodeLimits, opts: StreamCollectorOptions, hooks?: DecodeHooks): Promise<DecodedResponse>;
|
|
123
|
+
export {};
|
|
124
|
+
//# sourceMappingURL=continuation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"continuation.d.ts","sourceRoot":"","sources":["../../../src/tools/shared/continuation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAEL,KAAK,qBAAqB,EAC1B,KAAK,yBAAyB,EAC9B,KAAK,uBAAuB,EAC5B,KAAK,WAAW,EAEjB,MAAM,eAAe,CAAC;AAKvB,OAAO,EACL,KAAK,iBAAiB,EACtB,KAAK,sBAAsB,EAG5B,MAAM,uBAAuB,CAAC;AAG/B,0FAA0F;AAC1F,eAAO,MAAM,2BAA2B,krBACyoB,CAAC;AAElrB,iDAAiD;AACjD,KAAK,YAAY,GAAG,cAAc,CAAC,uBAAuB,CAAC,CAAC;AAE5D,uGAAuG;AACvG,MAAM,MAAM,iBAAiB,GAAG,CAAC,iBAAiB,EAAE,MAAM,GAAG,SAAS,KAAK,OAAO,CAAC,YAAY,CAAC,CAAC;AAEjG,oHAAoH;AACpH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,eAAgB,SAAQ,iBAAiB;IACxD,gFAAgF;IAChF,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;CACnC;AAED,+GAA+G;AAC/G,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,QAAQ,CA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