opencode-agy-auth 2.0.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.
Files changed (235) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +745 -0
  3. package/dist/index.d.ts +4 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +3 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/src/antigravity/oauth.d.ts +31 -0
  8. package/dist/src/antigravity/oauth.d.ts.map +1 -0
  9. package/dist/src/antigravity/oauth.js +171 -0
  10. package/dist/src/antigravity/oauth.js.map +1 -0
  11. package/dist/src/constants.d.ts +155 -0
  12. package/dist/src/constants.d.ts.map +1 -0
  13. package/dist/src/constants.js +251 -0
  14. package/dist/src/constants.js.map +1 -0
  15. package/dist/src/hooks/auto-update-checker/cache.d.ts +3 -0
  16. package/dist/src/hooks/auto-update-checker/cache.d.ts.map +1 -0
  17. package/dist/src/hooks/auto-update-checker/cache.js +71 -0
  18. package/dist/src/hooks/auto-update-checker/cache.js.map +1 -0
  19. package/dist/src/hooks/auto-update-checker/checker.d.ts +16 -0
  20. package/dist/src/hooks/auto-update-checker/checker.d.ts.map +1 -0
  21. package/dist/src/hooks/auto-update-checker/checker.js +237 -0
  22. package/dist/src/hooks/auto-update-checker/checker.js.map +1 -0
  23. package/dist/src/hooks/auto-update-checker/constants.d.ts +9 -0
  24. package/dist/src/hooks/auto-update-checker/constants.d.ts.map +1 -0
  25. package/dist/src/hooks/auto-update-checker/constants.js +23 -0
  26. package/dist/src/hooks/auto-update-checker/constants.js.map +1 -0
  27. package/dist/src/hooks/auto-update-checker/index.d.ts +34 -0
  28. package/dist/src/hooks/auto-update-checker/index.d.ts.map +1 -0
  29. package/dist/src/hooks/auto-update-checker/index.js +125 -0
  30. package/dist/src/hooks/auto-update-checker/index.js.map +1 -0
  31. package/dist/src/hooks/auto-update-checker/types.d.ts +25 -0
  32. package/dist/src/hooks/auto-update-checker/types.d.ts.map +1 -0
  33. package/dist/src/hooks/auto-update-checker/types.js +1 -0
  34. package/dist/src/hooks/auto-update-checker/types.js.map +1 -0
  35. package/dist/src/plugin/accounts.d.ts +174 -0
  36. package/dist/src/plugin/accounts.d.ts.map +1 -0
  37. package/dist/src/plugin/accounts.js +1243 -0
  38. package/dist/src/plugin/accounts.js.map +1 -0
  39. package/dist/src/plugin/auth.d.ts +21 -0
  40. package/dist/src/plugin/auth.d.ts.map +1 -0
  41. package/dist/src/plugin/auth.js +46 -0
  42. package/dist/src/plugin/auth.js.map +1 -0
  43. package/dist/src/plugin/cache/index.d.ts +5 -0
  44. package/dist/src/plugin/cache/index.d.ts.map +1 -0
  45. package/dist/src/plugin/cache/index.js +5 -0
  46. package/dist/src/plugin/cache/index.js.map +1 -0
  47. package/dist/src/plugin/cache/signature-cache.d.ts +111 -0
  48. package/dist/src/plugin/cache/signature-cache.d.ts.map +1 -0
  49. package/dist/src/plugin/cache/signature-cache.js +375 -0
  50. package/dist/src/plugin/cache/signature-cache.js.map +1 -0
  51. package/dist/src/plugin/cache.d.ts +44 -0
  52. package/dist/src/plugin/cache.d.ts.map +1 -0
  53. package/dist/src/plugin/cache.js +200 -0
  54. package/dist/src/plugin/cache.js.map +1 -0
  55. package/dist/src/plugin/cli.d.ts +27 -0
  56. package/dist/src/plugin/cli.d.ts.map +1 -0
  57. package/dist/src/plugin/cli.js +130 -0
  58. package/dist/src/plugin/cli.js.map +1 -0
  59. package/dist/src/plugin/config/index.d.ts +16 -0
  60. package/dist/src/plugin/config/index.d.ts.map +1 -0
  61. package/dist/src/plugin/config/index.js +16 -0
  62. package/dist/src/plugin/config/index.js.map +1 -0
  63. package/dist/src/plugin/config/loader.d.ts +38 -0
  64. package/dist/src/plugin/config/loader.d.ts.map +1 -0
  65. package/dist/src/plugin/config/loader.js +204 -0
  66. package/dist/src/plugin/config/loader.js.map +1 -0
  67. package/dist/src/plugin/config/models.d.ts +27 -0
  68. package/dist/src/plugin/config/models.d.ts.map +1 -0
  69. package/dist/src/plugin/config/models.js +79 -0
  70. package/dist/src/plugin/config/models.js.map +1 -0
  71. package/dist/src/plugin/config/schema.d.ts +134 -0
  72. package/dist/src/plugin/config/schema.d.ts.map +1 -0
  73. package/dist/src/plugin/config/schema.js +445 -0
  74. package/dist/src/plugin/config/schema.js.map +1 -0
  75. package/dist/src/plugin/config/updater.d.ts +55 -0
  76. package/dist/src/plugin/config/updater.d.ts.map +1 -0
  77. package/dist/src/plugin/config/updater.js +133 -0
  78. package/dist/src/plugin/config/updater.js.map +1 -0
  79. package/dist/src/plugin/core/streaming/index.d.ts +3 -0
  80. package/dist/src/plugin/core/streaming/index.d.ts.map +1 -0
  81. package/dist/src/plugin/core/streaming/index.js +3 -0
  82. package/dist/src/plugin/core/streaming/index.js.map +1 -0
  83. package/dist/src/plugin/core/streaming/transformer.d.ts +10 -0
  84. package/dist/src/plugin/core/streaming/transformer.d.ts.map +1 -0
  85. package/dist/src/plugin/core/streaming/transformer.js +271 -0
  86. package/dist/src/plugin/core/streaming/transformer.js.map +1 -0
  87. package/dist/src/plugin/core/streaming/types.d.ts +27 -0
  88. package/dist/src/plugin/core/streaming/types.d.ts.map +1 -0
  89. package/dist/src/plugin/core/streaming/types.js +1 -0
  90. package/dist/src/plugin/core/streaming/types.js.map +1 -0
  91. package/dist/src/plugin/debug.d.ts +94 -0
  92. package/dist/src/plugin/debug.d.ts.map +1 -0
  93. package/dist/src/plugin/debug.js +418 -0
  94. package/dist/src/plugin/debug.js.map +1 -0
  95. package/dist/src/plugin/errors.d.ts +28 -0
  96. package/dist/src/plugin/errors.d.ts.map +1 -0
  97. package/dist/src/plugin/errors.js +42 -0
  98. package/dist/src/plugin/errors.js.map +1 -0
  99. package/dist/src/plugin/fingerprint.d.ts +71 -0
  100. package/dist/src/plugin/fingerprint.d.ts.map +1 -0
  101. package/dist/src/plugin/fingerprint.js +140 -0
  102. package/dist/src/plugin/fingerprint.js.map +1 -0
  103. package/dist/src/plugin/image-saver.d.ts +25 -0
  104. package/dist/src/plugin/image-saver.d.ts.map +1 -0
  105. package/dist/src/plugin/image-saver.js +86 -0
  106. package/dist/src/plugin/image-saver.js.map +1 -0
  107. package/dist/src/plugin/logger.d.ts +54 -0
  108. package/dist/src/plugin/logger.d.ts.map +1 -0
  109. package/dist/src/plugin/logger.js +120 -0
  110. package/dist/src/plugin/logger.js.map +1 -0
  111. package/dist/src/plugin/project.d.ts +33 -0
  112. package/dist/src/plugin/project.d.ts.map +1 -0
  113. package/dist/src/plugin/project.js +234 -0
  114. package/dist/src/plugin/project.js.map +1 -0
  115. package/dist/src/plugin/proxy.d.ts +2 -0
  116. package/dist/src/plugin/proxy.d.ts.map +1 -0
  117. package/dist/src/plugin/proxy.js +20 -0
  118. package/dist/src/plugin/proxy.js.map +1 -0
  119. package/dist/src/plugin/quota.d.ts +58 -0
  120. package/dist/src/plugin/quota.d.ts.map +1 -0
  121. package/dist/src/plugin/quota.js +430 -0
  122. package/dist/src/plugin/quota.js.map +1 -0
  123. package/dist/src/plugin/recovery/constants.d.ts +22 -0
  124. package/dist/src/plugin/recovery/constants.d.ts.map +1 -0
  125. package/dist/src/plugin/recovery/constants.js +43 -0
  126. package/dist/src/plugin/recovery/constants.js.map +1 -0
  127. package/dist/src/plugin/recovery/index.d.ts +12 -0
  128. package/dist/src/plugin/recovery/index.d.ts.map +1 -0
  129. package/dist/src/plugin/recovery/index.js +12 -0
  130. package/dist/src/plugin/recovery/index.js.map +1 -0
  131. package/dist/src/plugin/recovery/storage.d.ts +24 -0
  132. package/dist/src/plugin/recovery/storage.d.ts.map +1 -0
  133. package/dist/src/plugin/recovery/storage.js +354 -0
  134. package/dist/src/plugin/recovery/storage.js.map +1 -0
  135. package/dist/src/plugin/recovery/types.d.ts +116 -0
  136. package/dist/src/plugin/recovery/types.d.ts.map +1 -0
  137. package/dist/src/plugin/recovery/types.js +6 -0
  138. package/dist/src/plugin/recovery/types.js.map +1 -0
  139. package/dist/src/plugin/recovery.d.ts +61 -0
  140. package/dist/src/plugin/recovery.d.ts.map +1 -0
  141. package/dist/src/plugin/recovery.js +378 -0
  142. package/dist/src/plugin/recovery.js.map +1 -0
  143. package/dist/src/plugin/refresh-queue.d.ts +101 -0
  144. package/dist/src/plugin/refresh-queue.d.ts.map +1 -0
  145. package/dist/src/plugin/refresh-queue.js +248 -0
  146. package/dist/src/plugin/refresh-queue.js.map +1 -0
  147. package/dist/src/plugin/request-helpers.d.ts +306 -0
  148. package/dist/src/plugin/request-helpers.d.ts.map +1 -0
  149. package/dist/src/plugin/request-helpers.js +2476 -0
  150. package/dist/src/plugin/request-helpers.js.map +1 -0
  151. package/dist/src/plugin/request.d.ts +98 -0
  152. package/dist/src/plugin/request.d.ts.map +1 -0
  153. package/dist/src/plugin/request.js +1513 -0
  154. package/dist/src/plugin/request.js.map +1 -0
  155. package/dist/src/plugin/rotation.d.ts +169 -0
  156. package/dist/src/plugin/rotation.d.ts.map +1 -0
  157. package/dist/src/plugin/rotation.js +328 -0
  158. package/dist/src/plugin/rotation.js.map +1 -0
  159. package/dist/src/plugin/search.d.ts +32 -0
  160. package/dist/src/plugin/search.d.ts.map +1 -0
  161. package/dist/src/plugin/search.js +195 -0
  162. package/dist/src/plugin/search.js.map +1 -0
  163. package/dist/src/plugin/server.d.ts +23 -0
  164. package/dist/src/plugin/server.d.ts.map +1 -0
  165. package/dist/src/plugin/server.js +324 -0
  166. package/dist/src/plugin/server.js.map +1 -0
  167. package/dist/src/plugin/storage.d.ts +137 -0
  168. package/dist/src/plugin/storage.d.ts.map +1 -0
  169. package/dist/src/plugin/storage.js +588 -0
  170. package/dist/src/plugin/storage.js.map +1 -0
  171. package/dist/src/plugin/stores/signature-store.d.ts +5 -0
  172. package/dist/src/plugin/stores/signature-store.d.ts.map +1 -0
  173. package/dist/src/plugin/stores/signature-store.js +25 -0
  174. package/dist/src/plugin/stores/signature-store.js.map +1 -0
  175. package/dist/src/plugin/thinking-recovery.d.ts +90 -0
  176. package/dist/src/plugin/thinking-recovery.d.ts.map +1 -0
  177. package/dist/src/plugin/thinking-recovery.js +316 -0
  178. package/dist/src/plugin/thinking-recovery.js.map +1 -0
  179. package/dist/src/plugin/token.d.ts +19 -0
  180. package/dist/src/plugin/token.d.ts.map +1 -0
  181. package/dist/src/plugin/token.js +128 -0
  182. package/dist/src/plugin/token.js.map +1 -0
  183. package/dist/src/plugin/transform/claude.d.ts +82 -0
  184. package/dist/src/plugin/transform/claude.d.ts.map +1 -0
  185. package/dist/src/plugin/transform/claude.js +278 -0
  186. package/dist/src/plugin/transform/claude.js.map +1 -0
  187. package/dist/src/plugin/transform/cross-model-sanitizer.d.ts +35 -0
  188. package/dist/src/plugin/transform/cross-model-sanitizer.d.ts.map +1 -0
  189. package/dist/src/plugin/transform/cross-model-sanitizer.js +225 -0
  190. package/dist/src/plugin/transform/cross-model-sanitizer.js.map +1 -0
  191. package/dist/src/plugin/transform/gemini.d.ts +100 -0
  192. package/dist/src/plugin/transform/gemini.d.ts.map +1 -0
  193. package/dist/src/plugin/transform/gemini.js +504 -0
  194. package/dist/src/plugin/transform/gemini.js.map +1 -0
  195. package/dist/src/plugin/transform/index.d.ts +15 -0
  196. package/dist/src/plugin/transform/index.d.ts.map +1 -0
  197. package/dist/src/plugin/transform/index.js +14 -0
  198. package/dist/src/plugin/transform/index.js.map +1 -0
  199. package/dist/src/plugin/transform/model-resolver.d.ts +104 -0
  200. package/dist/src/plugin/transform/model-resolver.d.ts.map +1 -0
  201. package/dist/src/plugin/transform/model-resolver.js +409 -0
  202. package/dist/src/plugin/transform/model-resolver.js.map +1 -0
  203. package/dist/src/plugin/transform/types.d.ts +111 -0
  204. package/dist/src/plugin/transform/types.d.ts.map +1 -0
  205. package/dist/src/plugin/transform/types.js +1 -0
  206. package/dist/src/plugin/transform/types.js.map +1 -0
  207. package/dist/src/plugin/types.d.ts +97 -0
  208. package/dist/src/plugin/types.d.ts.map +1 -0
  209. package/dist/src/plugin/types.js +1 -0
  210. package/dist/src/plugin/types.js.map +1 -0
  211. package/dist/src/plugin/ui/ansi.d.ts +32 -0
  212. package/dist/src/plugin/ui/ansi.d.ts.map +1 -0
  213. package/dist/src/plugin/ui/ansi.js +52 -0
  214. package/dist/src/plugin/ui/ansi.js.map +1 -0
  215. package/dist/src/plugin/ui/auth-menu.d.ts +37 -0
  216. package/dist/src/plugin/ui/auth-menu.d.ts.map +1 -0
  217. package/dist/src/plugin/ui/auth-menu.js +182 -0
  218. package/dist/src/plugin/ui/auth-menu.js.map +1 -0
  219. package/dist/src/plugin/ui/confirm.d.ts +2 -0
  220. package/dist/src/plugin/ui/confirm.d.ts.map +1 -0
  221. package/dist/src/plugin/ui/confirm.js +15 -0
  222. package/dist/src/plugin/ui/confirm.js.map +1 -0
  223. package/dist/src/plugin/ui/select.d.ts +23 -0
  224. package/dist/src/plugin/ui/select.d.ts.map +1 -0
  225. package/dist/src/plugin/ui/select.js +254 -0
  226. package/dist/src/plugin/ui/select.js.map +1 -0
  227. package/dist/src/plugin/version.d.ts +19 -0
  228. package/dist/src/plugin/version.d.ts.map +1 -0
  229. package/dist/src/plugin/version.js +74 -0
  230. package/dist/src/plugin/version.js.map +1 -0
  231. package/dist/src/plugin.d.ts +40 -0
  232. package/dist/src/plugin.d.ts.map +1 -0
  233. package/dist/src/plugin.js +3407 -0
  234. package/dist/src/plugin.js.map +1 -0
  235. package/package.json +73 -0
@@ -0,0 +1,1513 @@
1
+ import crypto from "node:crypto";
2
+ import { ANTIGRAVITY_ENDPOINT, GEMINI_CLI_ENDPOINT, GEMINI_CLI_HEADERS, EMPTY_SCHEMA_PLACEHOLDER_NAME, EMPTY_SCHEMA_PLACEHOLDER_DESCRIPTION, SKIP_THOUGHT_SIGNATURE, getDefaultModelLimits, getLearnedLimit, getRandomizedHeaders, } from "../constants";
3
+ import { cacheSignature, getCachedSignature } from "./cache";
4
+ import { getKeepThinking, isDebugTuiEnabled } from "./config";
5
+ import { createStreamingTransformer, transformSseLine, transformStreamingPayload, } from "./core/streaming";
6
+ import { defaultSignatureStore } from "./stores/signature-store";
7
+ import { DEBUG_MESSAGE_PREFIX, isDebugEnabled, logAntigravityDebugResponse, logCacheStats, } from "./debug";
8
+ import { createLogger } from "./logger";
9
+ import { cleanJSONSchemaForAntigravity, DEFAULT_THINKING_BUDGET, deepFilterThinkingBlocks, filterThinkingFromHistory, pruneOldTurns, truncateOldToolResponses, extractThinkingConfig, extractVariantThinkingConfig, extractUsageFromSsePayload, extractUsageMetadata, fixToolResponseGrouping, validateAndFixClaudeToolPairing, applyToolPairingFixes, createSyntheticErrorResponse, injectParameterSignatures, injectToolHardeningInstruction, isThinkingCapableModel, normalizeThinkingConfig, parseAntigravityApiBody, resolveThinkingConfig, rewriteAntigravityPreviewAccessError, transformThinkingParts, } from "./request-helpers";
10
+ import { CLAUDE_TOOL_SYSTEM_INSTRUCTION, CLAUDE_DESCRIPTION_PROMPT, ANTIGRAVITY_SYSTEM_INSTRUCTION, } from "../constants";
11
+ import { analyzeConversationState, closeToolLoopForThinking, needsThinkingRecovery, } from "./thinking-recovery";
12
+ import { sanitizeCrossModelPayloadInPlace } from "./transform/cross-model-sanitizer";
13
+ import { isGemini3Model, isImageGenerationModel, buildImageGenerationConfig, applyGeminiTransforms } from "./transform";
14
+ import { resolveModelWithTier, resolveModelWithVariant, resolveModelForHeaderStyle, isClaudeModel, isClaudeThinkingModel, CLAUDE_THINKING_MAX_OUTPUT_TOKENS, } from "./transform";
15
+ import { detectErrorType } from "./recovery";
16
+ import { getSessionFingerprint, buildFingerprintHeaders } from "./fingerprint";
17
+ const log = createLogger("request");
18
+ const PLUGIN_SESSION_ID = `-${crypto.randomUUID()}`;
19
+ const sessionDisplayedThinkingHashes = new Set();
20
+ const MIN_SIGNATURE_LENGTH = 50;
21
+ function buildSignatureSessionKey(sessionId, model, conversationKey, projectKey) {
22
+ const modelKey = typeof model === "string" && model.trim() ? model.toLowerCase() : "unknown";
23
+ const projectPart = typeof projectKey === "string" && projectKey.trim()
24
+ ? projectKey.trim()
25
+ : "default";
26
+ const conversationPart = typeof conversationKey === "string" && conversationKey.trim()
27
+ ? conversationKey.trim()
28
+ : "default";
29
+ return `${sessionId}:${modelKey}:${projectPart}:${conversationPart}`;
30
+ }
31
+ function shouldCacheThinkingSignatures(model) {
32
+ if (typeof model !== "string")
33
+ return false;
34
+ const lower = model.toLowerCase();
35
+ // Both Claude and Gemini 3 models require thought signature caching
36
+ // for multi-turn conversations with function calling
37
+ return lower.includes("claude") || lower.includes("gemini-3");
38
+ }
39
+ function hashConversationSeed(seed) {
40
+ return crypto.createHash("sha256").update(seed, "utf8").digest("hex").slice(0, 16);
41
+ }
42
+ function extractTextFromContent(content) {
43
+ if (typeof content === "string") {
44
+ return content;
45
+ }
46
+ if (!Array.isArray(content)) {
47
+ return "";
48
+ }
49
+ for (const block of content) {
50
+ if (!block || typeof block !== "object") {
51
+ continue;
52
+ }
53
+ const anyBlock = block;
54
+ if (typeof anyBlock.text === "string") {
55
+ return anyBlock.text;
56
+ }
57
+ if (anyBlock.text && typeof anyBlock.text === "object" && typeof anyBlock.text.text === "string") {
58
+ return anyBlock.text.text;
59
+ }
60
+ }
61
+ return "";
62
+ }
63
+ function extractConversationSeedFromMessages(messages) {
64
+ const system = messages.find((message) => message?.role === "system");
65
+ const users = messages.filter((message) => message?.role === "user");
66
+ const firstUser = users[0];
67
+ const lastUser = users.length > 0 ? users[users.length - 1] : undefined;
68
+ const systemText = system ? extractTextFromContent(system.content) : "";
69
+ const userText = firstUser ? extractTextFromContent(firstUser.content) : "";
70
+ const fallbackUserText = !userText && lastUser ? extractTextFromContent(lastUser.content) : "";
71
+ return [systemText, userText || fallbackUserText].filter(Boolean).join("|");
72
+ }
73
+ function extractConversationSeedFromContents(contents) {
74
+ const users = contents.filter((content) => content?.role === "user");
75
+ const firstUser = users[0];
76
+ const lastUser = users.length > 0 ? users[users.length - 1] : undefined;
77
+ const primaryUser = firstUser && Array.isArray(firstUser.parts) ? extractTextFromContent(firstUser.parts) : "";
78
+ if (primaryUser) {
79
+ return primaryUser;
80
+ }
81
+ if (lastUser && Array.isArray(lastUser.parts)) {
82
+ return extractTextFromContent(lastUser.parts);
83
+ }
84
+ return "";
85
+ }
86
+ function resolveConversationKey(requestPayload) {
87
+ const anyPayload = requestPayload;
88
+ const candidates = [
89
+ anyPayload.conversationId,
90
+ anyPayload.conversation_id,
91
+ anyPayload.thread_id,
92
+ anyPayload.threadId,
93
+ anyPayload.chat_id,
94
+ anyPayload.chatId,
95
+ anyPayload.sessionId,
96
+ anyPayload.session_id,
97
+ anyPayload.metadata?.conversation_id,
98
+ anyPayload.metadata?.conversationId,
99
+ anyPayload.metadata?.thread_id,
100
+ anyPayload.metadata?.threadId,
101
+ ];
102
+ for (const candidate of candidates) {
103
+ if (typeof candidate === "string" && candidate.trim()) {
104
+ return candidate.trim();
105
+ }
106
+ }
107
+ const systemSeed = extractTextFromContent(anyPayload.systemInstruction?.parts
108
+ ?? anyPayload.systemInstruction
109
+ ?? anyPayload.system
110
+ ?? anyPayload.system_instruction);
111
+ const messageSeed = Array.isArray(anyPayload.messages)
112
+ ? extractConversationSeedFromMessages(anyPayload.messages)
113
+ : Array.isArray(anyPayload.contents)
114
+ ? extractConversationSeedFromContents(anyPayload.contents)
115
+ : "";
116
+ const seed = [systemSeed, messageSeed].filter(Boolean).join("|");
117
+ if (!seed) {
118
+ return undefined;
119
+ }
120
+ return `seed-${hashConversationSeed(seed)}`;
121
+ }
122
+ function resolveConversationKeyFromRequests(requestObjects) {
123
+ for (const req of requestObjects) {
124
+ const key = resolveConversationKey(req);
125
+ if (key) {
126
+ return key;
127
+ }
128
+ }
129
+ return undefined;
130
+ }
131
+ function resolveProjectKey(candidate, fallback) {
132
+ if (typeof candidate === "string" && candidate.trim()) {
133
+ return candidate.trim();
134
+ }
135
+ if (typeof fallback === "string" && fallback.trim()) {
136
+ return fallback.trim();
137
+ }
138
+ return undefined;
139
+ }
140
+ function formatDebugLinesForThinking(lines) {
141
+ const cleaned = lines
142
+ .map((line) => line.trim())
143
+ .filter((line) => line.length > 0)
144
+ .slice(-50);
145
+ return `${DEBUG_MESSAGE_PREFIX}\n${cleaned.map((line) => `- ${line}`).join("\n")}`;
146
+ }
147
+ function injectDebugThinking(response, debugText) {
148
+ if (!response || typeof response !== "object") {
149
+ return response;
150
+ }
151
+ const resp = response;
152
+ if (Array.isArray(resp.candidates) && resp.candidates.length > 0) {
153
+ const candidates = resp.candidates.slice();
154
+ const first = candidates[0];
155
+ if (first &&
156
+ typeof first === "object" &&
157
+ first.content &&
158
+ typeof first.content === "object" &&
159
+ Array.isArray(first.content.parts)) {
160
+ const parts = [{ thought: true, text: debugText }, ...first.content.parts];
161
+ candidates[0] = { ...first, content: { ...first.content, parts } };
162
+ return { ...resp, candidates };
163
+ }
164
+ return resp;
165
+ }
166
+ if (Array.isArray(resp.content)) {
167
+ const content = [{ type: "thinking", thinking: debugText }, ...resp.content];
168
+ return { ...resp, content };
169
+ }
170
+ if (!resp.reasoning_content) {
171
+ return { ...resp, reasoning_content: debugText };
172
+ }
173
+ return resp;
174
+ }
175
+ /**
176
+ * Synthetic thinking placeholder text used when keep_thinking=true but debug mode is off.
177
+ * Injected via the same path as debug text (injectDebugThinking) to ensure consistent
178
+ * signature caching and multi-turn handling.
179
+ */
180
+ const SYNTHETIC_THINKING_PLACEHOLDER = "[Thinking preserved]\n";
181
+ function stripInjectedDebugFromParts(parts) {
182
+ if (!Array.isArray(parts)) {
183
+ return parts;
184
+ }
185
+ return parts.filter((part) => {
186
+ if (!part || typeof part !== "object") {
187
+ return true;
188
+ }
189
+ const record = part;
190
+ const text = typeof record.text === "string"
191
+ ? record.text
192
+ : typeof record.thinking === "string"
193
+ ? record.thinking
194
+ : undefined;
195
+ // Strip debug blocks and synthetic thinking placeholders
196
+ if (text && (text.startsWith(DEBUG_MESSAGE_PREFIX) || text.startsWith(SYNTHETIC_THINKING_PLACEHOLDER.trim()))) {
197
+ return false;
198
+ }
199
+ return true;
200
+ });
201
+ }
202
+ function stripInjectedDebugFromRequestPayload(payload) {
203
+ const anyPayload = payload;
204
+ if (Array.isArray(anyPayload.contents)) {
205
+ anyPayload.contents = anyPayload.contents.map((content) => {
206
+ if (!content || typeof content !== "object") {
207
+ return content;
208
+ }
209
+ if (Array.isArray(content.parts)) {
210
+ return { ...content, parts: stripInjectedDebugFromParts(content.parts) };
211
+ }
212
+ if (Array.isArray(content.content)) {
213
+ return { ...content, content: stripInjectedDebugFromParts(content.content) };
214
+ }
215
+ return content;
216
+ });
217
+ }
218
+ if (Array.isArray(anyPayload.messages)) {
219
+ anyPayload.messages = anyPayload.messages.map((message) => {
220
+ if (!message || typeof message !== "object") {
221
+ return message;
222
+ }
223
+ if (Array.isArray(message.content)) {
224
+ return { ...message, content: stripInjectedDebugFromParts(message.content) };
225
+ }
226
+ return message;
227
+ });
228
+ }
229
+ }
230
+ function isGeminiToolUsePart(part) {
231
+ return !!(part && typeof part === "object" && (part.functionCall || part.tool_use || part.toolUse));
232
+ }
233
+ function isGeminiThinkingPart(part) {
234
+ return !!(part &&
235
+ typeof part === "object" &&
236
+ (part.thought === true || part.type === "thinking" || part.type === "reasoning"));
237
+ }
238
+ // Sentinel value used when signature recovery fails - allows Claude to handle gracefully
239
+ // by redacting the thinking block instead of rejecting the request entirely.
240
+ // Reference: LLM-API-Key-Proxy uses this pattern for Gemini 3 tool calls.
241
+ const SENTINEL_SIGNATURE = "skip_thought_signature_validator";
242
+ export function ensureThoughtSignature(part, sessionId) {
243
+ if (!part || typeof part !== "object") {
244
+ return part;
245
+ }
246
+ const text = typeof part.text === "string" ? part.text : typeof part.thinking === "string" ? part.thinking : "";
247
+ if (!text) {
248
+ return part;
249
+ }
250
+ if (part.thought === true) {
251
+ if (!part.thoughtSignature) {
252
+ const cached = getCachedSignature(sessionId, text);
253
+ if (cached) {
254
+ return { ...part, thoughtSignature: cached };
255
+ }
256
+ // Fallback: use sentinel signature to prevent API rejection
257
+ // This allows Claude to redact the thinking block instead of failing
258
+ return { ...part, thoughtSignature: SENTINEL_SIGNATURE };
259
+ }
260
+ return part;
261
+ }
262
+ if ((part.type === "thinking" || part.type === "reasoning") && !part.signature) {
263
+ const cached = getCachedSignature(sessionId, text);
264
+ if (cached) {
265
+ return { ...part, signature: cached };
266
+ }
267
+ // Fallback: use sentinel signature to prevent API rejection
268
+ return { ...part, signature: SENTINEL_SIGNATURE };
269
+ }
270
+ return part;
271
+ }
272
+ function hasSignedThinkingPart(part) {
273
+ if (!part || typeof part !== "object") {
274
+ return false;
275
+ }
276
+ if (part.thought === true) {
277
+ return typeof part.thoughtSignature === "string" && part.thoughtSignature.length >= MIN_SIGNATURE_LENGTH;
278
+ }
279
+ if (part.type === "thinking" || part.type === "reasoning") {
280
+ return typeof part.signature === "string" && part.signature.length >= MIN_SIGNATURE_LENGTH;
281
+ }
282
+ return false;
283
+ }
284
+ function ensureThinkingBeforeToolUseInContents(contents, signatureSessionKey) {
285
+ return contents.map((content) => {
286
+ if (!content || typeof content !== "object" || !Array.isArray(content.parts)) {
287
+ return content;
288
+ }
289
+ const role = content.role;
290
+ if (role !== "model" && role !== "assistant") {
291
+ return content;
292
+ }
293
+ const parts = content.parts;
294
+ const hasToolUse = parts.some(isGeminiToolUsePart);
295
+ if (!hasToolUse) {
296
+ return content;
297
+ }
298
+ const thinkingParts = parts.filter(isGeminiThinkingPart).map((p) => ensureThoughtSignature(p, signatureSessionKey));
299
+ const otherParts = parts.filter((p) => !isGeminiThinkingPart(p));
300
+ const hasSignedThinking = thinkingParts.some(hasSignedThinkingPart);
301
+ if (hasSignedThinking) {
302
+ return { ...content, parts: [...thinkingParts, ...otherParts] };
303
+ }
304
+ const lastThinking = defaultSignatureStore.get(signatureSessionKey);
305
+ if (!lastThinking) {
306
+ // No cached signature available - strip thinking blocks entirely
307
+ // Claude requires valid signatures, and we can't fake them
308
+ // Return only tool_use parts without any thinking to avoid signature validation errors
309
+ log.debug("Stripping thinking from tool_use content (no valid cached signature)", { signatureSessionKey });
310
+ return { ...content, parts: otherParts };
311
+ }
312
+ const injected = {
313
+ thought: true,
314
+ text: lastThinking.text,
315
+ thoughtSignature: lastThinking.signature,
316
+ };
317
+ return { ...content, parts: [injected, ...otherParts] };
318
+ });
319
+ }
320
+ function ensureMessageThinkingSignature(block, sessionId) {
321
+ if (!block || typeof block !== "object") {
322
+ return block;
323
+ }
324
+ if (block.type !== "thinking" && block.type !== "redacted_thinking") {
325
+ return block;
326
+ }
327
+ if (typeof block.signature === "string" && block.signature.length >= MIN_SIGNATURE_LENGTH) {
328
+ return block;
329
+ }
330
+ const text = typeof block.thinking === "string" ? block.thinking : typeof block.text === "string" ? block.text : "";
331
+ if (!text) {
332
+ return block;
333
+ }
334
+ const cached = getCachedSignature(sessionId, text);
335
+ if (cached) {
336
+ return { ...block, signature: cached };
337
+ }
338
+ return block;
339
+ }
340
+ function hasToolUseInContents(contents) {
341
+ return contents.some((content) => {
342
+ if (!content || typeof content !== "object" || !Array.isArray(content.parts)) {
343
+ return false;
344
+ }
345
+ return content.parts.some(isGeminiToolUsePart);
346
+ });
347
+ }
348
+ function hasSignedThinkingInContents(contents) {
349
+ return contents.some((content) => {
350
+ if (!content || typeof content !== "object" || !Array.isArray(content.parts)) {
351
+ return false;
352
+ }
353
+ return content.parts.some(hasSignedThinkingPart);
354
+ });
355
+ }
356
+ function hasToolUseInMessages(messages) {
357
+ return messages.some((message) => {
358
+ if (!message || typeof message !== "object" || !Array.isArray(message.content)) {
359
+ return false;
360
+ }
361
+ return message.content.some((block) => block && typeof block === "object" && (block.type === "tool_use" || block.type === "tool_result"));
362
+ });
363
+ }
364
+ function hasSignedThinkingInMessages(messages) {
365
+ return messages.some((message) => {
366
+ if (!message || typeof message !== "object" || !Array.isArray(message.content)) {
367
+ return false;
368
+ }
369
+ return message.content.some((block) => block &&
370
+ typeof block === "object" &&
371
+ (block.type === "thinking" || block.type === "redacted_thinking") &&
372
+ typeof block.signature === "string" &&
373
+ block.signature.length >= MIN_SIGNATURE_LENGTH);
374
+ });
375
+ }
376
+ function ensureThinkingBeforeToolUseInMessages(messages, signatureSessionKey) {
377
+ return messages.map((message) => {
378
+ if (!message || typeof message !== "object" || !Array.isArray(message.content)) {
379
+ return message;
380
+ }
381
+ if (message.role !== "assistant") {
382
+ return message;
383
+ }
384
+ const blocks = message.content;
385
+ const hasToolUse = blocks.some((b) => b && typeof b === "object" && (b.type === "tool_use" || b.type === "tool_result"));
386
+ if (!hasToolUse) {
387
+ return message;
388
+ }
389
+ const thinkingBlocks = blocks
390
+ .filter((b) => b && typeof b === "object" && (b.type === "thinking" || b.type === "redacted_thinking"))
391
+ .map((b) => ensureMessageThinkingSignature(b, signatureSessionKey));
392
+ const otherBlocks = blocks.filter((b) => !(b && typeof b === "object" && (b.type === "thinking" || b.type === "redacted_thinking")));
393
+ const hasSignedThinking = thinkingBlocks.some((b) => typeof b.signature === "string" && b.signature.length >= MIN_SIGNATURE_LENGTH);
394
+ if (hasSignedThinking) {
395
+ return { ...message, content: [...thinkingBlocks, ...otherBlocks] };
396
+ }
397
+ const lastThinking = defaultSignatureStore.get(signatureSessionKey);
398
+ if (!lastThinking) {
399
+ // No cached signature available - use sentinel to bypass validation
400
+ // This handles cache miss scenarios (restart, session mismatch, expiry)
401
+ const existingThinking = thinkingBlocks[0];
402
+ const thinkingText = existingThinking?.thinking || existingThinking?.text || "";
403
+ log.debug("Injecting sentinel signature (cache miss)", { signatureSessionKey });
404
+ const sentinelBlock = {
405
+ type: "thinking",
406
+ thinking: thinkingText,
407
+ signature: SKIP_THOUGHT_SIGNATURE,
408
+ };
409
+ return { ...message, content: [sentinelBlock, ...otherBlocks] };
410
+ }
411
+ const injected = {
412
+ type: "thinking",
413
+ thinking: lastThinking.text,
414
+ signature: lastThinking.signature,
415
+ };
416
+ return { ...message, content: [injected, ...otherBlocks] };
417
+ });
418
+ }
419
+ /**
420
+ * Gets the stable session ID for this plugin instance.
421
+ */
422
+ export function getPluginSessionId() {
423
+ return PLUGIN_SESSION_ID;
424
+ }
425
+ function generateSyntheticProjectId() {
426
+ const adjectives = ["useful", "bright", "swift", "calm", "bold"];
427
+ const nouns = ["fuze", "wave", "spark", "flow", "core"];
428
+ const adj = adjectives[Math.floor(Math.random() * adjectives.length)];
429
+ const noun = nouns[Math.floor(Math.random() * nouns.length)];
430
+ const randomPart = crypto.randomUUID().slice(0, 5).toLowerCase();
431
+ return `${adj}-${noun}-${randomPart}`;
432
+ }
433
+ const STREAM_ACTION = "streamGenerateContent";
434
+ function sanitizeRequestPayloadForAntigravity(payload, sessionKey) {
435
+ if (!payload || typeof payload !== "object")
436
+ return;
437
+ if (Array.isArray(payload.contents)) {
438
+ for (const content of payload.contents) {
439
+ if (!content || !Array.isArray(content.parts))
440
+ continue;
441
+ let currentThoughtSignature;
442
+ // First pass: Find existing thoughtSignature or recover it from cache via ensureThoughtSignature
443
+ for (const part of content.parts) {
444
+ if (part && typeof part === "object") {
445
+ // Check standard Gemini thought parts
446
+ if (part.thought === true) {
447
+ currentThoughtSignature = part.thoughtSignature;
448
+ // If missing in payload but we have sessionKey, try to recover from cache
449
+ if (!currentThoughtSignature && sessionKey) {
450
+ const updatedPart = ensureThoughtSignature(part, sessionKey);
451
+ if (updatedPart.thoughtSignature && updatedPart.thoughtSignature !== "skip_thought_signature_validator") {
452
+ currentThoughtSignature = updatedPart.thoughtSignature;
453
+ part.thoughtSignature = currentThoughtSignature; // restore it
454
+ }
455
+ }
456
+ if (currentThoughtSignature)
457
+ break;
458
+ }
459
+ // Check wrapped/Anthropic style thinking parts
460
+ if (part.type === "thinking" || part.type === "reasoning") {
461
+ currentThoughtSignature = part.signature;
462
+ // Try to recover from cache
463
+ if (!currentThoughtSignature && sessionKey) {
464
+ const updatedPart = ensureThoughtSignature(part, sessionKey);
465
+ if (updatedPart.thoughtSignature && updatedPart.thoughtSignature !== "skip_thought_signature_validator") {
466
+ currentThoughtSignature = updatedPart.thoughtSignature;
467
+ part.signature = currentThoughtSignature; // also restore it to the thought part itself
468
+ }
469
+ }
470
+ if (currentThoughtSignature)
471
+ break;
472
+ }
473
+ }
474
+ }
475
+ // Second pass: If we found a thought signature, inject it into any functionCall parts in this turn
476
+ if (currentThoughtSignature) {
477
+ for (const part of content.parts) {
478
+ if (part && typeof part === "object" && part.functionCall && !part.thoughtSignature) {
479
+ part.thoughtSignature = currentThoughtSignature;
480
+ }
481
+ }
482
+ }
483
+ }
484
+ }
485
+ }
486
+ /**
487
+ * Detects requests headed to the Google Generative Language API so we can intercept them.
488
+ */
489
+ export function isGenerativeLanguageRequest(input) {
490
+ return typeof input === "string" && input.includes("generativelanguage.googleapis.com");
491
+ }
492
+ export function prepareAntigravityRequest(input, init, accessToken, projectId, endpointOverride, headerStyle = "antigravity", forceThinkingRecovery = false, options, modelLimits = getDefaultModelLimits()) {
493
+ const baseInit = { ...init };
494
+ const headers = new Headers(init?.headers ?? {});
495
+ let resolvedProjectId = projectId?.trim() || "";
496
+ let toolDebugMissing = 0;
497
+ const toolDebugSummaries = [];
498
+ let toolDebugPayload;
499
+ let sessionId;
500
+ let needsSignedThinkingWarmup = false;
501
+ let thinkingRecoveryMessage;
502
+ if (!isGenerativeLanguageRequest(input)) {
503
+ return {
504
+ request: input,
505
+ init: { ...baseInit, headers },
506
+ streaming: false,
507
+ headerStyle,
508
+ };
509
+ }
510
+ headers.set("Authorization", `Bearer ${accessToken}`);
511
+ headers.delete("x-api-key");
512
+ if (headerStyle === "antigravity") {
513
+ // Strip x-goog-user-project header to prevent 403 PERMISSION_DENIED errors.
514
+ // This header is added by OpenCode/AI SDK but causes auth conflicts on ALL endpoints
515
+ // (Daily, Autopush, Prod) when the user's GCP project doesn't have Cloud Code API enabled.
516
+ // Error: "Cloud Code Private API has not been used in project {user_project} before or it is disabled"
517
+ headers.delete("x-goog-user-project");
518
+ }
519
+ const match = input.match(/\/models\/([^:]+):(\w+)/);
520
+ if (!match) {
521
+ return {
522
+ request: input,
523
+ init: { ...baseInit, headers },
524
+ streaming: false,
525
+ headerStyle,
526
+ };
527
+ }
528
+ const [, rawModel = "", rawAction = ""] = match;
529
+ const requestedModel = rawModel;
530
+ const resolved = resolveModelForHeaderStyle(rawModel, headerStyle);
531
+ let effectiveModel = resolved.actualModel;
532
+ const applyGemini3ProTierToEffectiveModel = (level) => {
533
+ if (!level) {
534
+ return;
535
+ }
536
+ if (headerStyle !== "antigravity") {
537
+ return;
538
+ }
539
+ if (!/^gemini-3(?:\.\d+)?-pro/i.test(effectiveModel)) {
540
+ return;
541
+ }
542
+ const normalizedProTier = level.toLowerCase() === "high" ? "high" : "low";
543
+ const baseGemini3Pro = effectiveModel.replace(/-(minimal|low|medium|high)$/i, "");
544
+ effectiveModel = `${baseGemini3Pro}-${normalizedProTier}`;
545
+ };
546
+ const streaming = rawAction === STREAM_ACTION;
547
+ const defaultEndpoint = headerStyle === "gemini-cli" ? GEMINI_CLI_ENDPOINT : ANTIGRAVITY_ENDPOINT;
548
+ const baseEndpoint = endpointOverride ?? defaultEndpoint;
549
+ const transformedUrl = `${baseEndpoint}/v1internal:${rawAction}${streaming ? "?alt=sse" : ""}`;
550
+ const isClaude = isClaudeModel(resolved.actualModel);
551
+ const isClaudeThinking = isClaudeThinkingModel(resolved.actualModel);
552
+ // Tier-based thinking configuration from model resolver (can be overridden by variant config)
553
+ let tierThinkingBudget = resolved.thinkingBudget;
554
+ let tierThinkingLevel = resolved.thinkingLevel;
555
+ let signatureSessionKey = buildSignatureSessionKey(PLUGIN_SESSION_ID, effectiveModel, undefined, resolveProjectKey(projectId));
556
+ let body = baseInit.body;
557
+ if (typeof baseInit.body === "string" && baseInit.body) {
558
+ try {
559
+ const parsedBody = JSON.parse(baseInit.body);
560
+ const isWrapped = typeof parsedBody.project === "string" && "request" in parsedBody;
561
+ if (isWrapped) {
562
+ const wrappedBody = {
563
+ ...parsedBody,
564
+ model: effectiveModel,
565
+ };
566
+ // Some callers may already send an Antigravity-wrapped body.
567
+ // We still need to sanitize Claude thinking blocks (remove cache_control)
568
+ // and attach a stable sessionId so multi-turn signature caching works.
569
+ const requestRoot = wrappedBody.request;
570
+ const requestObjects = [];
571
+ if (requestRoot && typeof requestRoot === "object") {
572
+ requestObjects.push(requestRoot);
573
+ const nested = requestRoot.request;
574
+ if (nested && typeof nested === "object") {
575
+ requestObjects.push(nested);
576
+ }
577
+ }
578
+ const variantSources = [
579
+ wrappedBody,
580
+ ...requestObjects,
581
+ ];
582
+ for (const req of variantSources) {
583
+ const variantConfig = extractVariantThinkingConfig(req.providerOptions, req.generationConfig);
584
+ if (variantConfig?.thinkingLevel) {
585
+ applyGemini3ProTierToEffectiveModel(variantConfig.thinkingLevel);
586
+ break;
587
+ }
588
+ if (typeof variantConfig?.thinkingBudget === "number") {
589
+ const inferredLevel = variantConfig.thinkingBudget <= 8192
590
+ ? "low"
591
+ : variantConfig.thinkingBudget <= 16384
592
+ ? "medium"
593
+ : "high";
594
+ applyGemini3ProTierToEffectiveModel(inferredLevel);
595
+ break;
596
+ }
597
+ }
598
+ wrappedBody.model = effectiveModel;
599
+ const conversationKey = resolveConversationKeyFromRequests(requestObjects);
600
+ // Strip tier suffix from model for cache key to prevent cache misses on tier change
601
+ // e.g., "claude-opus-4-5-thinking-high" -> "claude-opus-4-5-thinking"
602
+ const modelForCacheKey = effectiveModel.replace(/-(minimal|low|medium|high)$/i, "");
603
+ signatureSessionKey = buildSignatureSessionKey(PLUGIN_SESSION_ID, modelForCacheKey, conversationKey, resolveProjectKey(parsedBody.project));
604
+ if (requestObjects.length > 0) {
605
+ sessionId = signatureSessionKey;
606
+ }
607
+ for (const req of requestObjects) {
608
+ // Use stable session ID for signature caching across multi-turn conversations
609
+ req.sessionId = signatureSessionKey;
610
+ stripInjectedDebugFromRequestPayload(req);
611
+ if (isClaude) {
612
+ // Step 0: Sanitize cross-model metadata (strips Gemini signatures when sending to Claude)
613
+ sanitizeCrossModelPayloadInPlace(req, { targetModel: effectiveModel });
614
+ // Step 1: Strip corrupted/unsigned thinking blocks FIRST
615
+ deepFilterThinkingBlocks(req, signatureSessionKey, getCachedSignature, true);
616
+ // Step 2: THEN inject signed thinking from cache (after stripping)
617
+ if (isClaudeThinking && Array.isArray(req.contents)) {
618
+ req.contents = ensureThinkingBeforeToolUseInContents(req.contents, signatureSessionKey);
619
+ }
620
+ if (isClaudeThinking && Array.isArray(req.messages)) {
621
+ req.messages = ensureThinkingBeforeToolUseInMessages(req.messages, signatureSessionKey);
622
+ }
623
+ // Step 3: Apply tool pairing fixes (ID assignment, response matching, orphan recovery)
624
+ applyToolPairingFixes(req, true);
625
+ }
626
+ else if (effectiveModel.toLowerCase().includes("gemini-3") || effectiveModel.toLowerCase().includes("gemini-experimental")) {
627
+ // Fix: Preserve thoughtSignature for Gemini thinking models when wrapped by OpenCode (Vercel AI SDK compatibility)
628
+ // The Vercel AI SDK strips thoughtSignature when building conversation history.
629
+ // We need to re-inject it by copying from the thinking part to the functionCall part in the same block.
630
+ sanitizeRequestPayloadForAntigravity(req, signatureSessionKey);
631
+ }
632
+ }
633
+ if (isClaudeThinking && sessionId) {
634
+ const hasToolUse = requestObjects.some((req) => (Array.isArray(req.contents) && hasToolUseInContents(req.contents)) ||
635
+ (Array.isArray(req.messages) && hasToolUseInMessages(req.messages)));
636
+ const hasSignedThinking = requestObjects.some((req) => (Array.isArray(req.contents) && hasSignedThinkingInContents(req.contents)) ||
637
+ (Array.isArray(req.messages) && hasSignedThinkingInMessages(req.messages)));
638
+ const hasCachedThinking = defaultSignatureStore.has(signatureSessionKey);
639
+ needsSignedThinkingWarmup = hasToolUse && !hasSignedThinking && !hasCachedThinking;
640
+ }
641
+ body = JSON.stringify(wrappedBody);
642
+ }
643
+ else {
644
+ const requestPayload = { ...parsedBody };
645
+ const rawGenerationConfig = requestPayload.generationConfig;
646
+ const extraBody = requestPayload.extra_body;
647
+ const variantConfig = extractVariantThinkingConfig(requestPayload.providerOptions, rawGenerationConfig);
648
+ const isGemini3 = effectiveModel.toLowerCase().includes("gemini-3");
649
+ if (variantConfig?.thinkingLevel && isGemini3) {
650
+ // Gemini 3 native format - use thinkingLevel directly
651
+ tierThinkingLevel = variantConfig.thinkingLevel;
652
+ tierThinkingBudget = undefined;
653
+ }
654
+ else if (variantConfig?.thinkingBudget) {
655
+ if (isGemini3) {
656
+ // Legacy format for Gemini 3 - convert with deprecation warning
657
+ log.warn("[Deprecated] Using thinkingBudget for Gemini 3 model. Use thinkingLevel instead.");
658
+ tierThinkingLevel = variantConfig.thinkingBudget <= 8192 ? "low"
659
+ : variantConfig.thinkingBudget <= 16384 ? "medium" : "high";
660
+ tierThinkingBudget = undefined;
661
+ }
662
+ else {
663
+ // Claude / Gemini 2.5 - use budget directly
664
+ tierThinkingBudget = variantConfig.thinkingBudget;
665
+ tierThinkingLevel = undefined;
666
+ }
667
+ }
668
+ applyGemini3ProTierToEffectiveModel(tierThinkingLevel);
669
+ if (isClaude) {
670
+ if (!requestPayload.toolConfig) {
671
+ requestPayload.toolConfig = {};
672
+ }
673
+ if (typeof requestPayload.toolConfig === "object" && requestPayload.toolConfig !== null) {
674
+ const toolConfig = requestPayload.toolConfig;
675
+ if (!toolConfig.functionCallingConfig) {
676
+ toolConfig.functionCallingConfig = {};
677
+ }
678
+ if (typeof toolConfig.functionCallingConfig === "object" && toolConfig.functionCallingConfig !== null) {
679
+ toolConfig.functionCallingConfig.mode = "VALIDATED";
680
+ }
681
+ }
682
+ }
683
+ // Resolve thinking configuration based on user settings and model capabilities
684
+ // Image generation models don't support thinking - skip thinking config entirely
685
+ const isImageModel = isImageGenerationModel(effectiveModel);
686
+ const userThinkingConfig = isImageModel ? undefined : extractThinkingConfig(requestPayload, rawGenerationConfig, extraBody);
687
+ const hasAssistantHistory = Array.isArray(requestPayload.contents) &&
688
+ requestPayload.contents.some((c) => c?.role === "model" || c?.role === "assistant");
689
+ // For claude-sonnet-4-6 (without -thinking suffix), ignore client's thinkingConfig
690
+ // Only claude-sonnet-4-6-thinking-* variants should have thinking enabled
691
+ const isClaudeSonnetNonThinking = effectiveModel.toLowerCase() === "claude-sonnet-4-6";
692
+ const effectiveUserThinkingConfig = (isClaudeSonnetNonThinking || isImageModel) ? undefined : userThinkingConfig;
693
+ // For image models, add imageConfig instead of thinkingConfig
694
+ if (isImageModel) {
695
+ const imageConfig = buildImageGenerationConfig();
696
+ const generationConfig = (rawGenerationConfig ?? {});
697
+ generationConfig.imageConfig = imageConfig;
698
+ // Remove any thinkingConfig that might have been set
699
+ delete generationConfig.thinkingConfig;
700
+ // Set reasonable defaults for image generation
701
+ if (!generationConfig.candidateCount) {
702
+ generationConfig.candidateCount = 1;
703
+ }
704
+ requestPayload.generationConfig = generationConfig;
705
+ // Add safety settings for image generation (permissive to allow creative content)
706
+ if (!requestPayload.safetySettings) {
707
+ requestPayload.safetySettings = [
708
+ { category: "HARM_CATEGORY_HARASSMENT", threshold: "BLOCK_ONLY_HIGH" },
709
+ { category: "HARM_CATEGORY_HATE_SPEECH", threshold: "BLOCK_ONLY_HIGH" },
710
+ { category: "HARM_CATEGORY_SEXUALLY_EXPLICIT", threshold: "BLOCK_ONLY_HIGH" },
711
+ { category: "HARM_CATEGORY_DANGEROUS_CONTENT", threshold: "BLOCK_ONLY_HIGH" },
712
+ { category: "HARM_CATEGORY_CIVIC_INTEGRITY", threshold: "BLOCK_ONLY_HIGH" },
713
+ ];
714
+ }
715
+ // Image models don't support tools - remove them entirely
716
+ delete requestPayload.tools;
717
+ delete requestPayload.toolConfig;
718
+ // Replace system instruction with a simple image generation prompt
719
+ // Image models should not receive agentic coding assistant instructions
720
+ requestPayload.systemInstruction = {
721
+ parts: [{ text: "You are an AI image generator. Generate images based on user descriptions. Focus on creating high-quality, visually appealing images that match the user's request." }]
722
+ };
723
+ }
724
+ else {
725
+ const finalThinkingConfig = resolveThinkingConfig(effectiveUserThinkingConfig, isClaudeSonnetNonThinking ? false : (resolved.isThinkingModel ?? isThinkingCapableModel(effectiveModel)), isClaude, hasAssistantHistory);
726
+ const normalizedThinking = normalizeThinkingConfig(finalThinkingConfig);
727
+ if (normalizedThinking) {
728
+ const variantBudget = userThinkingConfig?.thinkingBudget;
729
+ const thinkingBudget = typeof variantBudget === "number" && (variantBudget > 0 || variantBudget === -1)
730
+ ? (variantBudget > 0 && isClaudeThinking
731
+ ? Math.min(variantBudget, modelLimits.claude_thinking_budget_max)
732
+ : variantBudget)
733
+ : tierThinkingBudget ?? normalizedThinking.thinkingBudget;
734
+ // Build thinking config based on model type
735
+ let thinkingConfig;
736
+ if (isClaudeThinking) {
737
+ // Claude uses snake_case keys
738
+ thinkingConfig = {
739
+ include_thoughts: normalizedThinking.includeThoughts ?? true,
740
+ ...(typeof thinkingBudget === "number" && thinkingBudget > 0
741
+ ? { thinking_budget: thinkingBudget }
742
+ : thinkingBudget === -1
743
+ ? {} // -1 = server-controlled, omit budget (server decides)
744
+ : {}),
745
+ };
746
+ }
747
+ else if (tierThinkingLevel) {
748
+ // Gemini 3 uses thinkingLevel string (low/medium/high)
749
+ thinkingConfig = {
750
+ includeThoughts: normalizedThinking.includeThoughts,
751
+ thinkingLevel: tierThinkingLevel,
752
+ };
753
+ }
754
+ else {
755
+ // Gemini 2.5 and others use numeric budget
756
+ thinkingConfig = {
757
+ includeThoughts: normalizedThinking.includeThoughts,
758
+ ...(typeof thinkingBudget === "number" && thinkingBudget > 0
759
+ ? { thinkingBudget }
760
+ : thinkingBudget === -1
761
+ ? {} // -1 = server decides, omit thinking budget
762
+ : {}),
763
+ };
764
+ }
765
+ if (rawGenerationConfig) {
766
+ rawGenerationConfig.thinkingConfig = thinkingConfig;
767
+ if (isClaudeThinking && typeof thinkingBudget === "number" && thinkingBudget > 0) {
768
+ const currentMax = (rawGenerationConfig.maxOutputTokens ?? rawGenerationConfig.max_output_tokens);
769
+ if (!currentMax || currentMax <= thinkingBudget) {
770
+ rawGenerationConfig.maxOutputTokens = CLAUDE_THINKING_MAX_OUTPUT_TOKENS;
771
+ if (rawGenerationConfig.max_output_tokens !== undefined) {
772
+ delete rawGenerationConfig.max_output_tokens;
773
+ }
774
+ }
775
+ }
776
+ requestPayload.generationConfig = rawGenerationConfig;
777
+ }
778
+ else {
779
+ const generationConfig = { thinkingConfig };
780
+ if (isClaudeThinking && typeof thinkingBudget === "number" && thinkingBudget > 0) {
781
+ generationConfig.maxOutputTokens = CLAUDE_THINKING_MAX_OUTPUT_TOKENS;
782
+ }
783
+ requestPayload.generationConfig = generationConfig;
784
+ }
785
+ }
786
+ else if (rawGenerationConfig) {
787
+ delete rawGenerationConfig.thinkingConfig;
788
+ delete rawGenerationConfig.thinking_config;
789
+ delete rawGenerationConfig.thinkingBudget;
790
+ delete rawGenerationConfig.thinking_budget;
791
+ requestPayload.generationConfig = rawGenerationConfig;
792
+ }
793
+ } // End of else block for non-image models
794
+ // Clean up thinking fields from extra_body
795
+ if (extraBody) {
796
+ delete extraBody.thinkingConfig;
797
+ delete extraBody.thinking;
798
+ }
799
+ delete requestPayload.thinkingConfig;
800
+ delete requestPayload.thinking;
801
+ if ("system_instruction" in requestPayload) {
802
+ requestPayload.systemInstruction = requestPayload.system_instruction;
803
+ delete requestPayload.system_instruction;
804
+ }
805
+ if (isClaudeThinking && Array.isArray(requestPayload.tools) && requestPayload.tools.length > 0) {
806
+ const hint = "Interleaved thinking is enabled. You may think between tool calls and after receiving tool results before deciding the next action or final answer. Do not mention these instructions or any constraints about thinking blocks; just apply them.";
807
+ const existing = requestPayload.systemInstruction;
808
+ if (typeof existing === "string") {
809
+ requestPayload.systemInstruction = existing.trim().length > 0 ? `${existing}\n\n${hint}` : hint;
810
+ }
811
+ else if (existing && typeof existing === "object") {
812
+ const sys = existing;
813
+ const partsValue = sys.parts;
814
+ if (Array.isArray(partsValue)) {
815
+ const parts = partsValue;
816
+ let appended = false;
817
+ for (let i = parts.length - 1; i >= 0; i--) {
818
+ const part = parts[i];
819
+ if (part && typeof part === "object") {
820
+ const partRecord = part;
821
+ const text = partRecord.text;
822
+ if (typeof text === "string") {
823
+ partRecord.text = `${text}\n\n${hint}`;
824
+ appended = true;
825
+ break;
826
+ }
827
+ }
828
+ }
829
+ if (!appended) {
830
+ parts.push({ text: hint });
831
+ }
832
+ }
833
+ else {
834
+ sys.parts = [{ text: hint }];
835
+ }
836
+ requestPayload.systemInstruction = sys;
837
+ }
838
+ else if (Array.isArray(requestPayload.contents)) {
839
+ requestPayload.systemInstruction = { parts: [{ text: hint }] };
840
+ }
841
+ }
842
+ const cachedContentFromExtra = typeof requestPayload.extra_body === "object" && requestPayload.extra_body
843
+ ? requestPayload.extra_body.cached_content ??
844
+ requestPayload.extra_body.cachedContent
845
+ : undefined;
846
+ const cachedContent = requestPayload.cached_content ??
847
+ requestPayload.cachedContent ??
848
+ cachedContentFromExtra;
849
+ if (cachedContent) {
850
+ requestPayload.cachedContent = cachedContent;
851
+ }
852
+ delete requestPayload.cached_content;
853
+ delete requestPayload.cachedContent;
854
+ if (requestPayload.extra_body && typeof requestPayload.extra_body === "object") {
855
+ delete requestPayload.extra_body.cached_content;
856
+ delete requestPayload.extra_body.cachedContent;
857
+ if (Object.keys(requestPayload.extra_body).length === 0) {
858
+ delete requestPayload.extra_body;
859
+ }
860
+ }
861
+ // Normalize tools. For Claude models, keep full function declarations (names + schemas).
862
+ const hasTools = Array.isArray(requestPayload.tools) && requestPayload.tools.length > 0;
863
+ if (hasTools) {
864
+ if (isClaude) {
865
+ const functionDeclarations = [];
866
+ const passthroughTools = [];
867
+ const normalizeSchema = (schema) => {
868
+ const createPlaceholderSchema = (base = {}) => ({
869
+ ...base,
870
+ type: "object",
871
+ properties: {
872
+ [EMPTY_SCHEMA_PLACEHOLDER_NAME]: {
873
+ type: "boolean",
874
+ description: EMPTY_SCHEMA_PLACEHOLDER_DESCRIPTION,
875
+ },
876
+ },
877
+ required: [EMPTY_SCHEMA_PLACEHOLDER_NAME],
878
+ });
879
+ if (!schema || typeof schema !== "object" || Array.isArray(schema)) {
880
+ toolDebugMissing += 1;
881
+ return createPlaceholderSchema();
882
+ }
883
+ const cleaned = cleanJSONSchemaForAntigravity(schema);
884
+ if (!cleaned || typeof cleaned !== "object" || Array.isArray(cleaned)) {
885
+ toolDebugMissing += 1;
886
+ return createPlaceholderSchema();
887
+ }
888
+ // Claude VALIDATED mode requires tool parameters to be an object schema
889
+ // with at least one property.
890
+ const hasProperties = cleaned.properties &&
891
+ typeof cleaned.properties === "object" &&
892
+ Object.keys(cleaned.properties).length > 0;
893
+ cleaned.type = "object";
894
+ if (!hasProperties) {
895
+ cleaned.properties = {
896
+ [EMPTY_SCHEMA_PLACEHOLDER_NAME]: {
897
+ type: "boolean",
898
+ description: EMPTY_SCHEMA_PLACEHOLDER_DESCRIPTION,
899
+ },
900
+ };
901
+ cleaned.required = Array.isArray(cleaned.required)
902
+ ? Array.from(new Set([...cleaned.required, EMPTY_SCHEMA_PLACEHOLDER_NAME]))
903
+ : [EMPTY_SCHEMA_PLACEHOLDER_NAME];
904
+ }
905
+ return cleaned;
906
+ };
907
+ requestPayload.tools.forEach((tool) => {
908
+ const pushDeclaration = (decl, source) => {
909
+ const schema = decl?.parameters ||
910
+ decl?.parametersJsonSchema ||
911
+ decl?.input_schema ||
912
+ decl?.inputSchema ||
913
+ tool.parameters ||
914
+ tool.parametersJsonSchema ||
915
+ tool.input_schema ||
916
+ tool.inputSchema ||
917
+ tool.function?.parameters ||
918
+ tool.function?.parametersJsonSchema ||
919
+ tool.function?.input_schema ||
920
+ tool.function?.inputSchema ||
921
+ tool.custom?.parameters ||
922
+ tool.custom?.parametersJsonSchema ||
923
+ tool.custom?.input_schema;
924
+ let name = decl?.name ||
925
+ tool.name ||
926
+ tool.function?.name ||
927
+ tool.custom?.name ||
928
+ `tool-${functionDeclarations.length}`;
929
+ // Sanitize tool name: must be alphanumeric with underscores, no special chars
930
+ name = String(name).replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64);
931
+ const description = decl?.description ||
932
+ tool.description ||
933
+ tool.function?.description ||
934
+ tool.custom?.description ||
935
+ "";
936
+ functionDeclarations.push({
937
+ name,
938
+ description: String(description || ""),
939
+ parameters: normalizeSchema(schema),
940
+ });
941
+ toolDebugSummaries.push(`decl=${name},src=${source},hasSchema=${schema ? "y" : "n"}`);
942
+ };
943
+ if (Array.isArray(tool.functionDeclarations) && tool.functionDeclarations.length > 0) {
944
+ tool.functionDeclarations.forEach((decl) => pushDeclaration(decl, "functionDeclarations"));
945
+ return;
946
+ }
947
+ // Fall back to function/custom style definitions.
948
+ if (tool.function ||
949
+ tool.custom ||
950
+ tool.parameters ||
951
+ tool.input_schema ||
952
+ tool.inputSchema) {
953
+ pushDeclaration(tool.function ?? tool.custom ?? tool, "function/custom");
954
+ return;
955
+ }
956
+ // Preserve any non-function tool entries (e.g., codeExecution) untouched.
957
+ passthroughTools.push(tool);
958
+ });
959
+ const finalTools = [];
960
+ if (functionDeclarations.length > 0) {
961
+ finalTools.push({ functionDeclarations });
962
+ }
963
+ requestPayload.tools = finalTools.concat(passthroughTools);
964
+ }
965
+ else {
966
+ // Gemini-specific tool normalization and feature injection
967
+ const geminiResult = applyGeminiTransforms(requestPayload, {
968
+ model: effectiveModel,
969
+ normalizedThinking: undefined, // Thinking config already applied above (lines 816-880)
970
+ tierThinkingBudget,
971
+ tierThinkingLevel: tierThinkingLevel,
972
+ });
973
+ toolDebugMissing = geminiResult.toolDebugMissing;
974
+ toolDebugSummaries.push(...geminiResult.toolDebugSummaries);
975
+ }
976
+ try {
977
+ toolDebugPayload = JSON.stringify(requestPayload.tools);
978
+ }
979
+ catch {
980
+ toolDebugPayload = undefined;
981
+ }
982
+ // Apply Claude tool hardening (ported from LLM-API-Key-Proxy)
983
+ // Injects parameter signatures into descriptions and adds system instruction
984
+ // Can be disabled via config.claude_tool_hardening = false to reduce context size
985
+ const enableToolHardening = options?.claudeToolHardening ?? true;
986
+ if (enableToolHardening && isClaude && Array.isArray(requestPayload.tools) && requestPayload.tools.length > 0) {
987
+ // Inject parameter signatures into tool descriptions
988
+ requestPayload.tools = injectParameterSignatures(requestPayload.tools, CLAUDE_DESCRIPTION_PROMPT);
989
+ // Inject tool hardening system instruction
990
+ injectToolHardeningInstruction(requestPayload, CLAUDE_TOOL_SYSTEM_INSTRUCTION);
991
+ }
992
+ }
993
+ const conversationKey = resolveConversationKey(requestPayload);
994
+ signatureSessionKey = buildSignatureSessionKey(PLUGIN_SESSION_ID, effectiveModel, conversationKey, resolveProjectKey(projectId));
995
+ // For Claude models, filter out unsigned thinking blocks (required by Claude API)
996
+ // Attempts to restore signatures from cache for multi-turn conversations
997
+ // Handle both Gemini-style contents[] and Anthropic-style messages[] payloads.
998
+ if (isClaude) {
999
+ // Step 0: Sanitize cross-model metadata (strips Gemini signatures when sending to Claude)
1000
+ sanitizeCrossModelPayloadInPlace(requestPayload, { targetModel: effectiveModel });
1001
+ // Step 1: Strip corrupted/unsigned thinking blocks FIRST
1002
+ deepFilterThinkingBlocks(requestPayload, signatureSessionKey, getCachedSignature, true);
1003
+ // TASK 3+9: Strip thinking blocks from history based on keep_thinking config
1004
+ if (Array.isArray(requestPayload.contents)) {
1005
+ requestPayload.contents = filterThinkingFromHistory(requestPayload.contents, getKeepThinking());
1006
+ }
1007
+ if (Array.isArray(requestPayload.contents)) {
1008
+ requestPayload.contents = truncateOldToolResponses(requestPayload.contents);
1009
+ }
1010
+ // Step 2: THEN inject signed thinking from cache (after stripping)
1011
+ if (isClaudeThinking && Array.isArray(requestPayload.contents)) {
1012
+ requestPayload.contents = ensureThinkingBeforeToolUseInContents(requestPayload.contents, signatureSessionKey);
1013
+ }
1014
+ if (isClaudeThinking && Array.isArray(requestPayload.messages)) {
1015
+ requestPayload.messages = ensureThinkingBeforeToolUseInMessages(requestPayload.messages, signatureSessionKey);
1016
+ }
1017
+ // Step 3: Check if warmup needed (AFTER injection attempt)
1018
+ if (isClaudeThinking) {
1019
+ const hasToolUse = (Array.isArray(requestPayload.contents) && hasToolUseInContents(requestPayload.contents)) ||
1020
+ (Array.isArray(requestPayload.messages) && hasToolUseInMessages(requestPayload.messages));
1021
+ const hasSignedThinking = (Array.isArray(requestPayload.contents) && hasSignedThinkingInContents(requestPayload.contents)) ||
1022
+ (Array.isArray(requestPayload.messages) && hasSignedThinkingInMessages(requestPayload.messages));
1023
+ const hasCachedThinking = defaultSignatureStore.has(signatureSessionKey);
1024
+ needsSignedThinkingWarmup = hasToolUse && !hasSignedThinking && !hasCachedThinking;
1025
+ }
1026
+ }
1027
+ else {
1028
+ sanitizeRequestPayloadForAntigravity(requestPayload, signatureSessionKey);
1029
+ }
1030
+ // For Claude models, ensure functionCall/tool use parts carry IDs (required by Anthropic).
1031
+ // We use a two-pass approach: first collect all functionCalls and assign IDs,
1032
+ // then match functionResponses to their corresponding calls using a FIFO queue per function name.
1033
+ if (isClaude && Array.isArray(requestPayload.contents)) {
1034
+ let toolCallCounter = 0;
1035
+ // Track pending call IDs per function name as a FIFO queue
1036
+ const pendingCallIdsByName = new Map();
1037
+ // First pass: assign IDs to all functionCalls and collect them
1038
+ requestPayload.contents = requestPayload.contents.map((content) => {
1039
+ if (!content || !Array.isArray(content.parts)) {
1040
+ return content;
1041
+ }
1042
+ const newParts = content.parts.map((part) => {
1043
+ if (part && typeof part === "object" && part.functionCall) {
1044
+ const call = { ...part.functionCall };
1045
+ if (!call.id) {
1046
+ call.id = `tool-call-${++toolCallCounter}`;
1047
+ }
1048
+ const nameKey = typeof call.name === "string" ? call.name : `tool-${toolCallCounter}`;
1049
+ // Push to the queue for this function name
1050
+ const queue = pendingCallIdsByName.get(nameKey) || [];
1051
+ queue.push(call.id);
1052
+ pendingCallIdsByName.set(nameKey, queue);
1053
+ return { ...part, functionCall: call };
1054
+ }
1055
+ return part;
1056
+ });
1057
+ return { ...content, parts: newParts };
1058
+ });
1059
+ // Second pass: match functionResponses to their corresponding calls (FIFO order)
1060
+ requestPayload.contents = requestPayload.contents.map((content) => {
1061
+ if (!content || !Array.isArray(content.parts)) {
1062
+ return content;
1063
+ }
1064
+ const newParts = content.parts.map((part) => {
1065
+ if (part && typeof part === "object" && part.functionResponse) {
1066
+ const resp = { ...part.functionResponse };
1067
+ if (!resp.id && typeof resp.name === "string") {
1068
+ const queue = pendingCallIdsByName.get(resp.name);
1069
+ if (queue && queue.length > 0) {
1070
+ // Consume the first pending ID (FIFO order)
1071
+ resp.id = queue.shift();
1072
+ pendingCallIdsByName.set(resp.name, queue);
1073
+ }
1074
+ }
1075
+ return { ...part, functionResponse: resp };
1076
+ }
1077
+ return part;
1078
+ });
1079
+ return { ...content, parts: newParts };
1080
+ });
1081
+ // Third pass: Apply orphan recovery for mismatched tool IDs
1082
+ // This handles cases where context compaction or other processes
1083
+ // create ID mismatches between calls and responses.
1084
+ // Ported from LLM-API-Key-Proxy's _fix_tool_response_grouping()
1085
+ requestPayload.contents = fixToolResponseGrouping(requestPayload.contents);
1086
+ }
1087
+ // Fourth pass: Fix Claude format tool pairing (defense in depth)
1088
+ // Handles orphaned tool_use blocks in Claude's messages[] format
1089
+ if (Array.isArray(requestPayload.messages)) {
1090
+ requestPayload.messages = validateAndFixClaudeToolPairing(requestPayload.messages);
1091
+ }
1092
+ // =====================================================================
1093
+ // LAST RESORT RECOVERY: "Let it crash and start again"
1094
+ // =====================================================================
1095
+ // If after all our processing we're STILL in a bad state (tool loop without
1096
+ // thinking at turn start), don't try to fix it - just close the turn and
1097
+ // start fresh. This prevents permanent session breakage.
1098
+ //
1099
+ // This handles cases where:
1100
+ // - Context compaction stripped thinking blocks
1101
+ // - Signature cache miss
1102
+ // - Any other corruption we couldn't repair
1103
+ // - API error indicated thinking_block_order issue (forceThinkingRecovery=true)
1104
+ //
1105
+ // The synthetic messages allow Claude to generate fresh thinking on the
1106
+ // new turn instead of failing with "Expected thinking but found text".
1107
+ if (isClaudeThinking && Array.isArray(requestPayload.contents)) {
1108
+ const conversationState = analyzeConversationState(requestPayload.contents);
1109
+ // Force recovery if API returned thinking_block_order error (retry case)
1110
+ // or if proactive check detects we need recovery
1111
+ if (forceThinkingRecovery || needsThinkingRecovery(conversationState)) {
1112
+ // Set message for toast notification (shown in plugin.ts, respects quiet mode)
1113
+ thinkingRecoveryMessage = forceThinkingRecovery
1114
+ ? "Thinking recovery: retrying with fresh turn (API error)"
1115
+ : "Thinking recovery: restarting turn (corrupted context)";
1116
+ requestPayload.contents = closeToolLoopForThinking(requestPayload.contents);
1117
+ defaultSignatureStore.delete(signatureSessionKey);
1118
+ }
1119
+ }
1120
+ // Proactive context overflow guard for Claude models
1121
+ if (isClaude && headerStyle === "antigravity") {
1122
+ const configLimit = isClaude ? modelLimits.claude_context_limit : modelLimits.gemini_context_limit;
1123
+ const learnedLimit = getLearnedLimit(isClaude ? "claude" : "gemini");
1124
+ const HARD_LIMIT = learnedLimit ? Math.min(configLimit, learnedLimit) : configLimit;
1125
+ const estimateTokens = (obj) => Math.ceil(JSON.stringify(obj).length / 4);
1126
+ if (Array.isArray(requestPayload.contents)) {
1127
+ const estimatedTokens = estimateTokens(requestPayload);
1128
+ requestPayload.contents = pruneOldTurns(requestPayload.contents, estimatedTokens, HARD_LIMIT);
1129
+ }
1130
+ const resolvedThinkingBudget = typeof tierThinkingBudget === "number" && tierThinkingBudget > 0
1131
+ ? tierThinkingBudget
1132
+ : (isClaudeThinking ? 8_192 : 0);
1133
+ const safetyBuffer = 5_000;
1134
+ const effectiveLimit = HARD_LIMIT - resolvedThinkingBudget - safetyBuffer;
1135
+ let estimatedInputTokens = 0;
1136
+ if (requestPayload.systemInstruction)
1137
+ estimatedInputTokens += estimateTokens(requestPayload.systemInstruction);
1138
+ if (Array.isArray(requestPayload.contents))
1139
+ estimatedInputTokens += estimateTokens(requestPayload.contents);
1140
+ if (Array.isArray(requestPayload.messages))
1141
+ estimatedInputTokens += estimateTokens(requestPayload.messages);
1142
+ if (Array.isArray(requestPayload.tools))
1143
+ estimatedInputTokens += estimateTokens(requestPayload.tools);
1144
+ if (estimatedInputTokens > effectiveLimit) {
1145
+ const overBy = estimatedInputTokens - HARD_LIMIT;
1146
+ const overflowMsg = `[Antigravity] Context too long for ${requestedModel || effectiveModel}: ~${estimatedInputTokens.toLocaleString()} estimated tokens exceeds the ${HARD_LIMIT.toLocaleString()} token limit by ~${overBy.toLocaleString()} tokens.\n\nUse /compact to compress your context, then retry.`;
1147
+ const overflowToastMsg = `Context too long (~${Math.round(estimatedInputTokens / 1000)}k tokens). Use /compact to reduce size.`;
1148
+ return {
1149
+ request: input,
1150
+ init: { ...baseInit, headers },
1151
+ streaming,
1152
+ requestedModel,
1153
+ effectiveModel,
1154
+ projectId: resolvedProjectId,
1155
+ endpoint: transformedUrl,
1156
+ headerStyle,
1157
+ contextOverflowResponse: createSyntheticErrorResponse(overflowMsg, requestedModel || effectiveModel),
1158
+ contextOverflowMessage: overflowToastMsg,
1159
+ };
1160
+ }
1161
+ }
1162
+ if ("model" in requestPayload) {
1163
+ delete requestPayload.model;
1164
+ }
1165
+ stripInjectedDebugFromRequestPayload(requestPayload);
1166
+ const effectiveProjectId = projectId?.trim() || (headerStyle === "antigravity" ? generateSyntheticProjectId() : "");
1167
+ resolvedProjectId = effectiveProjectId;
1168
+ // Inject Antigravity system instruction with role "user" (CLIProxyAPI v6.6.89 compatibility)
1169
+ // This sets request.systemInstruction.role = "user" and request.systemInstruction.parts[0].text
1170
+ if (headerStyle === "antigravity") {
1171
+ const existingSystemInstruction = requestPayload.systemInstruction;
1172
+ if (existingSystemInstruction && typeof existingSystemInstruction === "object") {
1173
+ const sys = existingSystemInstruction;
1174
+ sys.role = "user";
1175
+ if (Array.isArray(sys.parts) && sys.parts.length > 0) {
1176
+ const firstPart = sys.parts[0];
1177
+ if (firstPart && typeof firstPart.text === "string") {
1178
+ firstPart.text = ANTIGRAVITY_SYSTEM_INSTRUCTION + "\n\n" + firstPart.text;
1179
+ }
1180
+ else {
1181
+ sys.parts = [{ text: ANTIGRAVITY_SYSTEM_INSTRUCTION }, ...sys.parts];
1182
+ }
1183
+ }
1184
+ else {
1185
+ sys.parts = [{ text: ANTIGRAVITY_SYSTEM_INSTRUCTION }];
1186
+ }
1187
+ }
1188
+ else if (typeof existingSystemInstruction === "string") {
1189
+ requestPayload.systemInstruction = {
1190
+ role: "user",
1191
+ parts: [{ text: ANTIGRAVITY_SYSTEM_INSTRUCTION + "\n\n" + existingSystemInstruction }],
1192
+ };
1193
+ }
1194
+ else {
1195
+ requestPayload.systemInstruction = {
1196
+ role: "user",
1197
+ parts: [{ text: ANTIGRAVITY_SYSTEM_INSTRUCTION }],
1198
+ };
1199
+ }
1200
+ }
1201
+ const wrappedBody = {
1202
+ project: effectiveProjectId,
1203
+ model: effectiveModel,
1204
+ request: requestPayload,
1205
+ };
1206
+ if (headerStyle === "antigravity") {
1207
+ wrappedBody.requestType = "agent";
1208
+ wrappedBody.userAgent = "antigravity";
1209
+ wrappedBody.requestId = "agent-" + crypto.randomUUID();
1210
+ }
1211
+ if (wrappedBody.request && typeof wrappedBody.request === 'object') {
1212
+ // Use stable session ID for signature caching across multi-turn conversations
1213
+ sessionId = signatureSessionKey;
1214
+ wrappedBody.request.sessionId = signatureSessionKey;
1215
+ }
1216
+ body = JSON.stringify(wrappedBody);
1217
+ }
1218
+ }
1219
+ catch (error) {
1220
+ throw error;
1221
+ }
1222
+ }
1223
+ if (streaming) {
1224
+ headers.set("Accept", "text/event-stream");
1225
+ }
1226
+ // Add interleaved thinking header for Claude thinking models
1227
+ // This enables real-time streaming of thinking tokens
1228
+ if (isClaudeThinking) {
1229
+ const existing = headers.get("anthropic-beta");
1230
+ const interleavedHeader = "interleaved-thinking-2025-05-14";
1231
+ if (existing) {
1232
+ if (!existing.includes(interleavedHeader)) {
1233
+ headers.set("anthropic-beta", `${existing},${interleavedHeader}`);
1234
+ }
1235
+ }
1236
+ else {
1237
+ headers.set("anthropic-beta", interleavedHeader);
1238
+ }
1239
+ }
1240
+ if (headerStyle === "antigravity") {
1241
+ // Use randomized headers as the fallback pool for Antigravity mode
1242
+ const selectedHeaders = getRandomizedHeaders("antigravity", requestedModel);
1243
+ // Antigravity mode: Match Antigravity Manager behavior
1244
+ // AM only sends User-Agent on content requests — no X-Goog-Api-Client, no Client-Metadata header
1245
+ // (ideType=ANTIGRAVITY goes in request body metadata via project.ts, not as a header)
1246
+ const fingerprint = options?.fingerprint ?? getSessionFingerprint();
1247
+ const fingerprintHeaders = buildFingerprintHeaders(fingerprint);
1248
+ headers.set("User-Agent", fingerprintHeaders["User-Agent"] || selectedHeaders["User-Agent"]);
1249
+ headers.set("Content-Type", "application/json");
1250
+ // Delete empty/blank X-Goog-Api-Key — antigravity uses OAuth Bearer tokens, not API keys
1251
+ const existingApiKey = headers.get("X-Goog-Api-Key");
1252
+ if (existingApiKey !== null && existingApiKey.trim() === "") {
1253
+ headers.delete("X-Goog-Api-Key");
1254
+ }
1255
+ }
1256
+ else {
1257
+ // Gemini CLI mode: match opencode-gemini-auth Code Assist header set exactly
1258
+ headers.set("User-Agent", GEMINI_CLI_HEADERS["User-Agent"]);
1259
+ headers.set("X-Goog-Api-Client", GEMINI_CLI_HEADERS["X-Goog-Api-Client"]);
1260
+ headers.set("Client-Metadata", GEMINI_CLI_HEADERS["Client-Metadata"]);
1261
+ }
1262
+ return {
1263
+ request: transformedUrl,
1264
+ init: {
1265
+ ...baseInit,
1266
+ headers,
1267
+ body,
1268
+ },
1269
+ streaming,
1270
+ requestedModel,
1271
+ effectiveModel: effectiveModel,
1272
+ projectId: resolvedProjectId,
1273
+ endpoint: transformedUrl,
1274
+ sessionId,
1275
+ toolDebugMissing,
1276
+ toolDebugSummary: toolDebugSummaries.slice(0, 20).join(" | "),
1277
+ toolDebugPayload,
1278
+ needsSignedThinkingWarmup,
1279
+ headerStyle,
1280
+ thinkingRecoveryMessage,
1281
+ };
1282
+ }
1283
+ export function buildThinkingWarmupBody(bodyText, isClaudeThinking) {
1284
+ if (!bodyText || !isClaudeThinking) {
1285
+ return null;
1286
+ }
1287
+ let parsed;
1288
+ try {
1289
+ parsed = JSON.parse(bodyText);
1290
+ }
1291
+ catch {
1292
+ return null;
1293
+ }
1294
+ const warmupPrompt = "Warmup request for thinking signature.";
1295
+ const updateRequest = (req) => {
1296
+ req.contents = [{ role: "user", parts: [{ text: warmupPrompt }] }];
1297
+ delete req.tools;
1298
+ delete req.toolConfig;
1299
+ const generationConfig = (req.generationConfig ?? {});
1300
+ generationConfig.thinkingConfig = {
1301
+ include_thoughts: true,
1302
+ thinking_budget: DEFAULT_THINKING_BUDGET,
1303
+ };
1304
+ generationConfig.maxOutputTokens = CLAUDE_THINKING_MAX_OUTPUT_TOKENS;
1305
+ req.generationConfig = generationConfig;
1306
+ };
1307
+ if (parsed.request && typeof parsed.request === "object") {
1308
+ updateRequest(parsed.request);
1309
+ const nested = parsed.request.request;
1310
+ if (nested && typeof nested === "object") {
1311
+ updateRequest(nested);
1312
+ }
1313
+ }
1314
+ else {
1315
+ updateRequest(parsed);
1316
+ }
1317
+ return JSON.stringify(parsed);
1318
+ }
1319
+ /**
1320
+ * Normalizes Antigravity responses: applies retry headers, extracts cache usage into headers,
1321
+ * rewrites preview errors, flattens streaming payloads, and logs debug metadata.
1322
+ *
1323
+ * For streaming SSE responses, uses TransformStream for true real-time incremental streaming.
1324
+ * Thinking/reasoning tokens are transformed and forwarded immediately as they arrive.
1325
+ */
1326
+ export async function transformAntigravityResponse(response, streaming, debugContext, requestedModel, projectId, endpoint, effectiveModel, sessionId, toolDebugMissing, toolDebugSummary, toolDebugPayload, debugLines) {
1327
+ const contentType = response.headers.get("content-type") ?? "";
1328
+ const isJsonResponse = contentType.includes("application/json");
1329
+ const isEventStreamResponse = contentType.includes("text/event-stream");
1330
+ // Generate text for thinking injection:
1331
+ const debugText = isDebugEnabled() && Array.isArray(debugLines) && debugLines.length > 0
1332
+ ? formatDebugLinesForThinking(debugLines)
1333
+ : isDebugTuiEnabled() || getKeepThinking()
1334
+ ? SYNTHETIC_THINKING_PLACEHOLDER
1335
+ : undefined;
1336
+ const cacheSignatures = shouldCacheThinkingSignatures(effectiveModel);
1337
+ if (!isJsonResponse && !isEventStreamResponse) {
1338
+ logAntigravityDebugResponse(debugContext, response, {
1339
+ note: "Non-JSON response (body omitted)",
1340
+ });
1341
+ return response;
1342
+ }
1343
+ // For successful streaming responses, use TransformStream to transform SSE events
1344
+ // while maintaining real-time streaming (no buffering of entire response).
1345
+ // This enables thinking tokens to be displayed as they arrive, like the Codex plugin.
1346
+ if (streaming && response.ok && isEventStreamResponse && response.body) {
1347
+ const headers = new Headers(response.headers);
1348
+ logAntigravityDebugResponse(debugContext, response, {
1349
+ note: "Streaming SSE response (real-time transform)",
1350
+ });
1351
+ const streamingTransformer = createStreamingTransformer(defaultSignatureStore, {
1352
+ onCacheSignature: cacheSignature,
1353
+ onInjectDebug: injectDebugThinking,
1354
+ // onInjectSyntheticThinking removed - keep_thinking now uses debugText path
1355
+ transformThinkingParts,
1356
+ }, {
1357
+ signatureSessionKey: sessionId,
1358
+ debugText,
1359
+ cacheSignatures,
1360
+ displayedThinkingHashes: effectiveModel && isGemini3Model(effectiveModel) ? sessionDisplayedThinkingHashes : undefined,
1361
+ // injectSyntheticThinking removed - keep_thinking now unified with debug via debugText
1362
+ });
1363
+ return new Response(response.body.pipeThrough(streamingTransformer), {
1364
+ status: response.status,
1365
+ statusText: response.statusText,
1366
+ headers,
1367
+ });
1368
+ }
1369
+ try {
1370
+ const headers = new Headers(response.headers);
1371
+ const text = await response.text();
1372
+ if (!response.ok) {
1373
+ let errorBody;
1374
+ try {
1375
+ errorBody = JSON.parse(text);
1376
+ }
1377
+ catch {
1378
+ errorBody = { error: { message: text } };
1379
+ }
1380
+ // Inject Debug Info
1381
+ if (errorBody?.error) {
1382
+ const debugInfo = `\n\n[Debug Info]\nRequested Model: ${requestedModel || "Unknown"}\nEffective Model: ${effectiveModel || "Unknown"}\nProject: ${projectId || "Unknown"}\nEndpoint: ${endpoint || "Unknown"}\nStatus: ${response.status}\nRequest ID: ${headers.get("x-request-id") || "N/A"}${toolDebugMissing !== undefined ? `\nTool Debug Missing: ${toolDebugMissing}` : ""}${toolDebugSummary ? `\nTool Debug Summary: ${toolDebugSummary}` : ""}${toolDebugPayload ? `\nTool Debug Payload: ${toolDebugPayload}` : ""}`;
1383
+ const injectedDebug = debugText ? `\n\n${debugText}` : "";
1384
+ errorBody.error.message = (errorBody.error.message || "Unknown error") + debugInfo + injectedDebug;
1385
+ const errorType = detectErrorType(errorBody.error.message || "");
1386
+ if (errorType === "thinking_block_order") {
1387
+ headers.set("x-antigravity-thinking-recovery", "needed");
1388
+ headers.set("x-antigravity-error-type", errorType);
1389
+ log.debug("Thinking recovery needed, signaling via header", {
1390
+ errorType,
1391
+ });
1392
+ }
1393
+ // Detect context length / prompt too long errors - signal to caller for toast
1394
+ const errorMessage = errorBody.error.message?.toLowerCase() || "";
1395
+ if (errorMessage.includes("prompt is too long") ||
1396
+ errorMessage.includes("context length exceeded") ||
1397
+ errorMessage.includes("context_length_exceeded") ||
1398
+ errorMessage.includes("maximum context length")) {
1399
+ headers.set("x-antigravity-context-error", "prompt_too_long");
1400
+ }
1401
+ // Detect tool pairing errors - signal to caller for toast
1402
+ if (errorMessage.includes("tool_use") &&
1403
+ errorMessage.includes("tool_result") &&
1404
+ (errorMessage.includes("without") || errorMessage.includes("immediately after"))) {
1405
+ headers.set("x-antigravity-context-error", "tool_pairing");
1406
+ }
1407
+ return new Response(JSON.stringify(errorBody), {
1408
+ status: response.status,
1409
+ statusText: response.statusText,
1410
+ headers
1411
+ });
1412
+ }
1413
+ if (errorBody?.error?.details && Array.isArray(errorBody.error.details)) {
1414
+ const retryInfo = errorBody.error.details.find((detail) => detail['@type'] === 'type.googleapis.com/google.rpc.RetryInfo');
1415
+ if (retryInfo?.retryDelay) {
1416
+ const match = retryInfo.retryDelay.match(/^([\d.]+)s$/);
1417
+ if (match && match[1]) {
1418
+ const retrySeconds = parseFloat(match[1]);
1419
+ if (!isNaN(retrySeconds) && retrySeconds > 0) {
1420
+ const retryAfterSec = Math.ceil(retrySeconds).toString();
1421
+ const retryAfterMs = Math.ceil(retrySeconds * 1000).toString();
1422
+ headers.set('Retry-After', retryAfterSec);
1423
+ headers.set('retry-after-ms', retryAfterMs);
1424
+ }
1425
+ }
1426
+ }
1427
+ }
1428
+ }
1429
+ const init = {
1430
+ status: response.status,
1431
+ statusText: response.statusText,
1432
+ headers,
1433
+ };
1434
+ const usageFromSse = streaming && isEventStreamResponse ? extractUsageFromSsePayload(text) : null;
1435
+ const parsed = !streaming || !isEventStreamResponse ? parseAntigravityApiBody(text) : null;
1436
+ const patched = parsed ? rewriteAntigravityPreviewAccessError(parsed, response.status, requestedModel) : null;
1437
+ const effectiveBody = patched ?? parsed ?? undefined;
1438
+ const usage = usageFromSse ?? (effectiveBody ? extractUsageMetadata(effectiveBody) : null);
1439
+ // Log cache stats when available
1440
+ if (usage && effectiveModel) {
1441
+ logCacheStats(effectiveModel, usage.cachedContentTokenCount ?? 0, 0, // API doesn't provide cache write tokens separately
1442
+ usage.promptTokenCount ?? usage.totalTokenCount ?? 0);
1443
+ }
1444
+ if (usage?.cachedContentTokenCount !== undefined) {
1445
+ headers.set("x-antigravity-cached-content-token-count", String(usage.cachedContentTokenCount));
1446
+ if (usage.totalTokenCount !== undefined) {
1447
+ headers.set("x-antigravity-total-token-count", String(usage.totalTokenCount));
1448
+ }
1449
+ if (usage.promptTokenCount !== undefined) {
1450
+ headers.set("x-antigravity-prompt-token-count", String(usage.promptTokenCount));
1451
+ }
1452
+ if (usage.candidatesTokenCount !== undefined) {
1453
+ headers.set("x-antigravity-candidates-token-count", String(usage.candidatesTokenCount));
1454
+ }
1455
+ }
1456
+ logAntigravityDebugResponse(debugContext, response, {
1457
+ body: text,
1458
+ note: streaming ? "Streaming SSE payload (buffered fallback)" : undefined,
1459
+ headersOverride: headers,
1460
+ });
1461
+ // Note: successful streaming responses are handled above via TransformStream.
1462
+ // This path only handles non-streaming responses or failed streaming responses.
1463
+ if (!parsed) {
1464
+ return new Response(text, init);
1465
+ }
1466
+ if (effectiveBody?.response !== undefined) {
1467
+ let responseBody = effectiveBody.response;
1468
+ // Inject thinking text (debug logs or "[Thinking preserved]" placeholder)
1469
+ // Both debug=true and keep_thinking=true use the same path now
1470
+ if (debugText) {
1471
+ responseBody = injectDebugThinking(responseBody, debugText);
1472
+ }
1473
+ const transformed = transformThinkingParts(responseBody);
1474
+ return new Response(JSON.stringify(transformed), init);
1475
+ }
1476
+ if (patched) {
1477
+ return new Response(JSON.stringify(patched), init);
1478
+ }
1479
+ return new Response(text, init);
1480
+ }
1481
+ catch (error) {
1482
+ logAntigravityDebugResponse(debugContext, response, {
1483
+ error,
1484
+ note: "Failed to transform Antigravity response",
1485
+ });
1486
+ return response;
1487
+ }
1488
+ }
1489
+ export const __testExports = {
1490
+ buildSignatureSessionKey,
1491
+ hashConversationSeed,
1492
+ extractTextFromContent,
1493
+ extractConversationSeedFromMessages,
1494
+ extractConversationSeedFromContents,
1495
+ resolveConversationKey,
1496
+ resolveProjectKey,
1497
+ isGeminiToolUsePart,
1498
+ isGeminiThinkingPart,
1499
+ ensureThoughtSignature,
1500
+ hasSignedThinkingPart,
1501
+ hasSignedThinkingInContents,
1502
+ hasSignedThinkingInMessages,
1503
+ hasToolUseInContents,
1504
+ hasToolUseInMessages,
1505
+ ensureThinkingBeforeToolUseInContents,
1506
+ ensureThinkingBeforeToolUseInMessages,
1507
+ generateSyntheticProjectId,
1508
+ MIN_SIGNATURE_LENGTH,
1509
+ transformSseLine,
1510
+ transformStreamingPayload,
1511
+ createStreamingTransformer,
1512
+ };
1513
+ //# sourceMappingURL=request.js.map