@zhachory1/mewrite-ai 0.65.3

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 (238) hide show
  1. package/README.md +1330 -0
  2. package/dist/api-registry.d.ts +20 -0
  3. package/dist/api-registry.d.ts.map +1 -0
  4. package/dist/api-registry.js +44 -0
  5. package/dist/api-registry.js.map +1 -0
  6. package/dist/bedrock-provider.d.ts +5 -0
  7. package/dist/bedrock-provider.d.ts.map +1 -0
  8. package/dist/bedrock-provider.js +6 -0
  9. package/dist/bedrock-provider.js.map +1 -0
  10. package/dist/cache/__tests__/compaction.test.d.ts +2 -0
  11. package/dist/cache/__tests__/compaction.test.d.ts.map +1 -0
  12. package/dist/cache/__tests__/compaction.test.js +100 -0
  13. package/dist/cache/__tests__/compaction.test.js.map +1 -0
  14. package/dist/cache/__tests__/layers.test.d.ts +2 -0
  15. package/dist/cache/__tests__/layers.test.d.ts.map +1 -0
  16. package/dist/cache/__tests__/layers.test.js +71 -0
  17. package/dist/cache/__tests__/layers.test.js.map +1 -0
  18. package/dist/cache/__tests__/policy.test.d.ts +2 -0
  19. package/dist/cache/__tests__/policy.test.d.ts.map +1 -0
  20. package/dist/cache/__tests__/policy.test.js +79 -0
  21. package/dist/cache/__tests__/policy.test.js.map +1 -0
  22. package/dist/cache/__tests__/tool-serializer.test.d.ts +2 -0
  23. package/dist/cache/__tests__/tool-serializer.test.d.ts.map +1 -0
  24. package/dist/cache/__tests__/tool-serializer.test.js +64 -0
  25. package/dist/cache/__tests__/tool-serializer.test.js.map +1 -0
  26. package/dist/cache/compaction.d.ts +30 -0
  27. package/dist/cache/compaction.d.ts.map +1 -0
  28. package/dist/cache/compaction.js +50 -0
  29. package/dist/cache/compaction.js.map +1 -0
  30. package/dist/cache/index.d.ts +5 -0
  31. package/dist/cache/index.d.ts.map +1 -0
  32. package/dist/cache/index.js +5 -0
  33. package/dist/cache/index.js.map +1 -0
  34. package/dist/cache/layers.d.ts +17 -0
  35. package/dist/cache/layers.d.ts.map +1 -0
  36. package/dist/cache/layers.js +48 -0
  37. package/dist/cache/layers.js.map +1 -0
  38. package/dist/cache/policy.d.ts +40 -0
  39. package/dist/cache/policy.d.ts.map +1 -0
  40. package/dist/cache/policy.js +50 -0
  41. package/dist/cache/policy.js.map +1 -0
  42. package/dist/cache/tool-serializer.d.ts +8 -0
  43. package/dist/cache/tool-serializer.d.ts.map +1 -0
  44. package/dist/cache/tool-serializer.js +35 -0
  45. package/dist/cache/tool-serializer.js.map +1 -0
  46. package/dist/cli.d.ts +3 -0
  47. package/dist/cli.d.ts.map +1 -0
  48. package/dist/cli.js +116 -0
  49. package/dist/cli.js.map +1 -0
  50. package/dist/env-api-keys.d.ts +27 -0
  51. package/dist/env-api-keys.d.ts.map +1 -0
  52. package/dist/env-api-keys.js +223 -0
  53. package/dist/env-api-keys.js.map +1 -0
  54. package/dist/index.d.ts +32 -0
  55. package/dist/index.d.ts.map +1 -0
  56. package/dist/index.js +21 -0
  57. package/dist/index.js.map +1 -0
  58. package/dist/models.d.ts +34 -0
  59. package/dist/models.d.ts.map +1 -0
  60. package/dist/models.generated.d.ts +14370 -0
  61. package/dist/models.generated.d.ts.map +1 -0
  62. package/dist/models.generated.js +14181 -0
  63. package/dist/models.generated.js.map +1 -0
  64. package/dist/models.js +140 -0
  65. package/dist/models.js.map +1 -0
  66. package/dist/oauth.d.ts +2 -0
  67. package/dist/oauth.d.ts.map +1 -0
  68. package/dist/oauth.js +2 -0
  69. package/dist/oauth.js.map +1 -0
  70. package/dist/providers/amazon-bedrock.d.ts +20 -0
  71. package/dist/providers/amazon-bedrock.d.ts.map +1 -0
  72. package/dist/providers/amazon-bedrock.js +651 -0
  73. package/dist/providers/amazon-bedrock.js.map +1 -0
  74. package/dist/providers/anthropic-capabilities.d.ts +65 -0
  75. package/dist/providers/anthropic-capabilities.d.ts.map +1 -0
  76. package/dist/providers/anthropic-capabilities.js +155 -0
  77. package/dist/providers/anthropic-capabilities.js.map +1 -0
  78. package/dist/providers/anthropic-discovery.d.ts +49 -0
  79. package/dist/providers/anthropic-discovery.d.ts.map +1 -0
  80. package/dist/providers/anthropic-discovery.js +218 -0
  81. package/dist/providers/anthropic-discovery.js.map +1 -0
  82. package/dist/providers/anthropic.d.ts +40 -0
  83. package/dist/providers/anthropic.d.ts.map +1 -0
  84. package/dist/providers/anthropic.js +766 -0
  85. package/dist/providers/anthropic.js.map +1 -0
  86. package/dist/providers/azure-openai-responses.d.ts +15 -0
  87. package/dist/providers/azure-openai-responses.d.ts.map +1 -0
  88. package/dist/providers/azure-openai-responses.js +176 -0
  89. package/dist/providers/azure-openai-responses.js.map +1 -0
  90. package/dist/providers/faux.d.ts +56 -0
  91. package/dist/providers/faux.d.ts.map +1 -0
  92. package/dist/providers/faux.js +367 -0
  93. package/dist/providers/faux.js.map +1 -0
  94. package/dist/providers/github-copilot-headers.d.ts +8 -0
  95. package/dist/providers/github-copilot-headers.d.ts.map +1 -0
  96. package/dist/providers/github-copilot-headers.js +29 -0
  97. package/dist/providers/github-copilot-headers.js.map +1 -0
  98. package/dist/providers/google-gemini-cli.d.ts +74 -0
  99. package/dist/providers/google-gemini-cli.d.ts.map +1 -0
  100. package/dist/providers/google-gemini-cli.js +776 -0
  101. package/dist/providers/google-gemini-cli.js.map +1 -0
  102. package/dist/providers/google-shared.d.ts +65 -0
  103. package/dist/providers/google-shared.d.ts.map +1 -0
  104. package/dist/providers/google-shared.js +312 -0
  105. package/dist/providers/google-shared.js.map +1 -0
  106. package/dist/providers/google-vertex.d.ts +15 -0
  107. package/dist/providers/google-vertex.d.ts.map +1 -0
  108. package/dist/providers/google-vertex.js +419 -0
  109. package/dist/providers/google-vertex.js.map +1 -0
  110. package/dist/providers/google.d.ts +13 -0
  111. package/dist/providers/google.d.ts.map +1 -0
  112. package/dist/providers/google.js +374 -0
  113. package/dist/providers/google.js.map +1 -0
  114. package/dist/providers/mistral.d.ts +22 -0
  115. package/dist/providers/mistral.d.ts.map +1 -0
  116. package/dist/providers/mistral.js +501 -0
  117. package/dist/providers/mistral.js.map +1 -0
  118. package/dist/providers/openai-codex-responses.d.ts +9 -0
  119. package/dist/providers/openai-codex-responses.d.ts.map +1 -0
  120. package/dist/providers/openai-codex-responses.js +741 -0
  121. package/dist/providers/openai-codex-responses.js.map +1 -0
  122. package/dist/providers/openai-completions.d.ts +15 -0
  123. package/dist/providers/openai-completions.d.ts.map +1 -0
  124. package/dist/providers/openai-completions.js +753 -0
  125. package/dist/providers/openai-completions.js.map +1 -0
  126. package/dist/providers/openai-responses-shared.d.ts +17 -0
  127. package/dist/providers/openai-responses-shared.d.ts.map +1 -0
  128. package/dist/providers/openai-responses-shared.js +470 -0
  129. package/dist/providers/openai-responses-shared.js.map +1 -0
  130. package/dist/providers/openai-responses.d.ts +13 -0
  131. package/dist/providers/openai-responses.d.ts.map +1 -0
  132. package/dist/providers/openai-responses.js +190 -0
  133. package/dist/providers/openai-responses.js.map +1 -0
  134. package/dist/providers/register-builtins.d.ts +38 -0
  135. package/dist/providers/register-builtins.d.ts.map +1 -0
  136. package/dist/providers/register-builtins.js +261 -0
  137. package/dist/providers/register-builtins.js.map +1 -0
  138. package/dist/providers/simple-options.d.ts +8 -0
  139. package/dist/providers/simple-options.d.ts.map +1 -0
  140. package/dist/providers/simple-options.js +35 -0
  141. package/dist/providers/simple-options.js.map +1 -0
  142. package/dist/providers/transform-messages.d.ts +8 -0
  143. package/dist/providers/transform-messages.d.ts.map +1 -0
  144. package/dist/providers/transform-messages.js +155 -0
  145. package/dist/providers/transform-messages.js.map +1 -0
  146. package/dist/registry/fetcher.d.ts +26 -0
  147. package/dist/registry/fetcher.d.ts.map +1 -0
  148. package/dist/registry/fetcher.js +69 -0
  149. package/dist/registry/fetcher.js.map +1 -0
  150. package/dist/registry/index.d.ts +8 -0
  151. package/dist/registry/index.d.ts.map +1 -0
  152. package/dist/registry/index.js +8 -0
  153. package/dist/registry/index.js.map +1 -0
  154. package/dist/registry/loader.d.ts +38 -0
  155. package/dist/registry/loader.d.ts.map +1 -0
  156. package/dist/registry/loader.js +90 -0
  157. package/dist/registry/loader.js.map +1 -0
  158. package/dist/registry/merger.d.ts +21 -0
  159. package/dist/registry/merger.d.ts.map +1 -0
  160. package/dist/registry/merger.js +90 -0
  161. package/dist/registry/merger.js.map +1 -0
  162. package/dist/registry/schema.d.ts +130 -0
  163. package/dist/registry/schema.d.ts.map +1 -0
  164. package/dist/registry/schema.js +103 -0
  165. package/dist/registry/schema.js.map +1 -0
  166. package/dist/stream.d.ts +8 -0
  167. package/dist/stream.d.ts.map +1 -0
  168. package/dist/stream.js +27 -0
  169. package/dist/stream.js.map +1 -0
  170. package/dist/types.d.ts +296 -0
  171. package/dist/types.d.ts.map +1 -0
  172. package/dist/types.js +2 -0
  173. package/dist/types.js.map +1 -0
  174. package/dist/utils/event-stream.d.ts +21 -0
  175. package/dist/utils/event-stream.d.ts.map +1 -0
  176. package/dist/utils/event-stream.js +81 -0
  177. package/dist/utils/event-stream.js.map +1 -0
  178. package/dist/utils/hash.d.ts +3 -0
  179. package/dist/utils/hash.d.ts.map +1 -0
  180. package/dist/utils/hash.js +14 -0
  181. package/dist/utils/hash.js.map +1 -0
  182. package/dist/utils/json-parse.d.ts +9 -0
  183. package/dist/utils/json-parse.d.ts.map +1 -0
  184. package/dist/utils/json-parse.js +29 -0
  185. package/dist/utils/json-parse.js.map +1 -0
  186. package/dist/utils/oauth/anthropic.d.ts +25 -0
  187. package/dist/utils/oauth/anthropic.d.ts.map +1 -0
  188. package/dist/utils/oauth/anthropic.js +336 -0
  189. package/dist/utils/oauth/anthropic.js.map +1 -0
  190. package/dist/utils/oauth/github-copilot.d.ts +30 -0
  191. package/dist/utils/oauth/github-copilot.d.ts.map +1 -0
  192. package/dist/utils/oauth/github-copilot.js +293 -0
  193. package/dist/utils/oauth/github-copilot.js.map +1 -0
  194. package/dist/utils/oauth/google-antigravity.d.ts +26 -0
  195. package/dist/utils/oauth/google-antigravity.d.ts.map +1 -0
  196. package/dist/utils/oauth/google-antigravity.js +376 -0
  197. package/dist/utils/oauth/google-antigravity.js.map +1 -0
  198. package/dist/utils/oauth/google-gemini-cli.d.ts +26 -0
  199. package/dist/utils/oauth/google-gemini-cli.d.ts.map +1 -0
  200. package/dist/utils/oauth/google-gemini-cli.js +482 -0
  201. package/dist/utils/oauth/google-gemini-cli.js.map +1 -0
  202. package/dist/utils/oauth/index.d.ts +61 -0
  203. package/dist/utils/oauth/index.d.ts.map +1 -0
  204. package/dist/utils/oauth/index.js +131 -0
  205. package/dist/utils/oauth/index.js.map +1 -0
  206. package/dist/utils/oauth/oauth-page.d.ts +3 -0
  207. package/dist/utils/oauth/oauth-page.d.ts.map +1 -0
  208. package/dist/utils/oauth/oauth-page.js +105 -0
  209. package/dist/utils/oauth/oauth-page.js.map +1 -0
  210. package/dist/utils/oauth/openai-codex.d.ts +34 -0
  211. package/dist/utils/oauth/openai-codex.d.ts.map +1 -0
  212. package/dist/utils/oauth/openai-codex.js +374 -0
  213. package/dist/utils/oauth/openai-codex.js.map +1 -0
  214. package/dist/utils/oauth/pkce.d.ts +13 -0
  215. package/dist/utils/oauth/pkce.d.ts.map +1 -0
  216. package/dist/utils/oauth/pkce.js +31 -0
  217. package/dist/utils/oauth/pkce.js.map +1 -0
  218. package/dist/utils/oauth/types.d.ts +49 -0
  219. package/dist/utils/oauth/types.d.ts.map +1 -0
  220. package/dist/utils/oauth/types.js +2 -0
  221. package/dist/utils/oauth/types.js.map +1 -0
  222. package/dist/utils/overflow.d.ts +53 -0
  223. package/dist/utils/overflow.d.ts.map +1 -0
  224. package/dist/utils/overflow.js +132 -0
  225. package/dist/utils/overflow.js.map +1 -0
  226. package/dist/utils/sanitize-unicode.d.ts +22 -0
  227. package/dist/utils/sanitize-unicode.d.ts.map +1 -0
  228. package/dist/utils/sanitize-unicode.js +26 -0
  229. package/dist/utils/sanitize-unicode.js.map +1 -0
  230. package/dist/utils/typebox-helpers.d.ts +17 -0
  231. package/dist/utils/typebox-helpers.d.ts.map +1 -0
  232. package/dist/utils/typebox-helpers.js +21 -0
  233. package/dist/utils/typebox-helpers.js.map +1 -0
  234. package/dist/utils/validation.d.ts +18 -0
  235. package/dist/utils/validation.d.ts.map +1 -0
  236. package/dist/utils/validation.js +80 -0
  237. package/dist/utils/validation.js.map +1 -0
  238. package/package.json +129 -0
@@ -0,0 +1,766 @@
1
+ import Anthropic from "@anthropic-ai/sdk";
2
+ import { getEnvApiKey } from "../env-api-keys.js";
3
+ import { calculateCost } from "../models.js";
4
+ import { AssistantMessageEventStream } from "../utils/event-stream.js";
5
+ import { parseStreamingJson } from "../utils/json-parse.js";
6
+ import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
7
+ import { getAnthropicCapabilities, supportsAdaptiveThinking } from "./anthropic-capabilities.js";
8
+ import { discoverAnthropicCapabilities } from "./anthropic-discovery.js";
9
+ import { buildCopilotDynamicHeaders, hasCopilotVisionInput } from "./github-copilot-headers.js";
10
+ import { adjustMaxTokensForThinking, buildBaseOptions } from "./simple-options.js";
11
+ import { transformMessages } from "./transform-messages.js";
12
+ /**
13
+ * Resolve cache retention preference.
14
+ * Defaults to "short" and uses PI_CACHE_RETENTION for backward compatibility.
15
+ */
16
+ function resolveCacheRetention(cacheRetention) {
17
+ if (cacheRetention) {
18
+ return cacheRetention;
19
+ }
20
+ if (typeof process !== "undefined" && process.env.PI_CACHE_RETENTION === "long") {
21
+ return "long";
22
+ }
23
+ return "short";
24
+ }
25
+ function getCacheControl(baseUrl, cacheRetention) {
26
+ const retention = resolveCacheRetention(cacheRetention);
27
+ if (retention === "none") {
28
+ return { retention };
29
+ }
30
+ const ttl = retention === "long" && baseUrl.includes("api.anthropic.com") ? "1h" : undefined;
31
+ return {
32
+ retention,
33
+ cacheControl: { type: "ephemeral", ...(ttl && { ttl }) },
34
+ };
35
+ }
36
+ // Stealth mode: Mimic Claude Code's tool naming exactly
37
+ const claudeCodeVersion = "2.1.75";
38
+ // Claude Code 2.x tool names (canonical casing)
39
+ // Source: https://cchistory.mariozechner.at/data/prompts-2.1.11.md
40
+ // To update: https://github.com/badlogic/cchistory
41
+ const claudeCodeTools = [
42
+ "Read",
43
+ "Write",
44
+ "Edit",
45
+ "Bash",
46
+ "Grep",
47
+ "Glob",
48
+ "AskUserQuestion",
49
+ "EnterPlanMode",
50
+ "ExitPlanMode",
51
+ "KillShell",
52
+ "NotebookEdit",
53
+ "Skill",
54
+ "Task",
55
+ "TaskOutput",
56
+ "TodoWrite",
57
+ "WebFetch",
58
+ "WebSearch",
59
+ ];
60
+ const ccToolLookup = new Map(claudeCodeTools.map((t) => [t.toLowerCase(), t]));
61
+ // Convert tool name to CC canonical casing if it matches (case-insensitive)
62
+ const toClaudeCodeName = (name) => ccToolLookup.get(name.toLowerCase()) ?? name;
63
+ const fromClaudeCodeName = (name, tools) => {
64
+ if (tools && tools.length > 0) {
65
+ const lowerName = name.toLowerCase();
66
+ const matchedTool = tools.find((tool) => tool.name.toLowerCase() === lowerName);
67
+ if (matchedTool)
68
+ return matchedTool.name;
69
+ }
70
+ return name;
71
+ };
72
+ /**
73
+ * Convert content blocks to Anthropic API format
74
+ */
75
+ function convertContentBlocks(content) {
76
+ // If only text blocks, return as concatenated string for simplicity
77
+ const hasImages = content.some((c) => c.type === "image");
78
+ if (!hasImages) {
79
+ return sanitizeSurrogates(content.map((c) => c.text).join("\n"));
80
+ }
81
+ // If we have images, convert to content block array
82
+ const blocks = content.map((block) => {
83
+ if (block.type === "text") {
84
+ return {
85
+ type: "text",
86
+ text: sanitizeSurrogates(block.text),
87
+ };
88
+ }
89
+ return {
90
+ type: "image",
91
+ source: {
92
+ type: "base64",
93
+ media_type: block.mimeType,
94
+ data: block.data,
95
+ },
96
+ };
97
+ });
98
+ // If only images (no text), add placeholder text block
99
+ const hasText = blocks.some((b) => b.type === "text");
100
+ if (!hasText) {
101
+ blocks.unshift({
102
+ type: "text",
103
+ text: "(see attached image)",
104
+ });
105
+ }
106
+ return blocks;
107
+ }
108
+ function mergeHeaders(...headerSources) {
109
+ const merged = {};
110
+ for (const headers of headerSources) {
111
+ if (headers) {
112
+ Object.assign(merged, headers);
113
+ }
114
+ }
115
+ return merged;
116
+ }
117
+ export const streamAnthropic = (model, context, options) => {
118
+ const stream = new AssistantMessageEventStream();
119
+ (async () => {
120
+ const output = {
121
+ role: "assistant",
122
+ content: [],
123
+ api: model.api,
124
+ provider: model.provider,
125
+ model: model.id,
126
+ usage: {
127
+ input: 0,
128
+ output: 0,
129
+ cacheRead: 0,
130
+ cacheWrite: 0,
131
+ totalTokens: 0,
132
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
133
+ },
134
+ stopReason: "stop",
135
+ timestamp: Date.now(),
136
+ };
137
+ try {
138
+ let client;
139
+ let isOAuth;
140
+ if (options?.client) {
141
+ client = options.client;
142
+ isOAuth = false;
143
+ }
144
+ else {
145
+ const apiKey = options?.apiKey ?? getEnvApiKey(model.provider) ?? "";
146
+ // Discover real per-account capabilities before building the
147
+ // request so the first call uses the right thinking schema,
148
+ // reasoning_effort ceiling, and context window. Memoized per
149
+ // (provider, baseUrl) for the rest of the process; subsequent
150
+ // calls resolve from cache immediately.
151
+ if (apiKey) {
152
+ await discoverAnthropicCapabilities(model.provider, model.baseUrl, apiKey);
153
+ }
154
+ let copilotDynamicHeaders;
155
+ if (model.provider === "github-copilot") {
156
+ const hasImages = hasCopilotVisionInput(context.messages);
157
+ copilotDynamicHeaders = buildCopilotDynamicHeaders({
158
+ messages: context.messages,
159
+ hasImages,
160
+ });
161
+ }
162
+ const created = createClient(model, apiKey, options?.interleavedThinking ?? true, options?.headers, copilotDynamicHeaders);
163
+ client = created.client;
164
+ isOAuth = created.isOAuthToken;
165
+ }
166
+ let params = buildParams(model, context, isOAuth, options);
167
+ const nextParams = await options?.onPayload?.(params, model);
168
+ if (nextParams !== undefined) {
169
+ params = nextParams;
170
+ }
171
+ const anthropicStream = client.messages.stream({ ...params, stream: true }, { signal: options?.signal });
172
+ stream.push({ type: "start", partial: output });
173
+ const blocks = output.content;
174
+ for await (const event of anthropicStream) {
175
+ if (event.type === "message_start") {
176
+ output.responseId = event.message.id;
177
+ // Capture initial token usage from message_start event
178
+ // This ensures we have input token counts even if the stream is aborted early
179
+ output.usage.input = event.message.usage.input_tokens || 0;
180
+ output.usage.output = event.message.usage.output_tokens || 0;
181
+ output.usage.cacheRead = event.message.usage.cache_read_input_tokens || 0;
182
+ output.usage.cacheWrite = event.message.usage.cache_creation_input_tokens || 0;
183
+ // Anthropic doesn't provide total_tokens, compute from components
184
+ output.usage.totalTokens =
185
+ output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
186
+ calculateCost(model, output.usage);
187
+ }
188
+ else if (event.type === "content_block_start") {
189
+ if (event.content_block.type === "text") {
190
+ const block = {
191
+ type: "text",
192
+ text: "",
193
+ index: event.index,
194
+ };
195
+ output.content.push(block);
196
+ stream.push({ type: "text_start", contentIndex: output.content.length - 1, partial: output });
197
+ }
198
+ else if (event.content_block.type === "thinking") {
199
+ const block = {
200
+ type: "thinking",
201
+ thinking: "",
202
+ thinkingSignature: "",
203
+ index: event.index,
204
+ };
205
+ output.content.push(block);
206
+ stream.push({ type: "thinking_start", contentIndex: output.content.length - 1, partial: output });
207
+ }
208
+ else if (event.content_block.type === "redacted_thinking") {
209
+ const block = {
210
+ type: "thinking",
211
+ thinking: "[Reasoning redacted]",
212
+ thinkingSignature: event.content_block.data,
213
+ redacted: true,
214
+ index: event.index,
215
+ };
216
+ output.content.push(block);
217
+ stream.push({ type: "thinking_start", contentIndex: output.content.length - 1, partial: output });
218
+ }
219
+ else if (event.content_block.type === "tool_use") {
220
+ const block = {
221
+ type: "toolCall",
222
+ id: event.content_block.id,
223
+ name: isOAuth
224
+ ? fromClaudeCodeName(event.content_block.name, context.tools)
225
+ : event.content_block.name,
226
+ arguments: event.content_block.input ?? {},
227
+ partialJson: "",
228
+ index: event.index,
229
+ };
230
+ output.content.push(block);
231
+ stream.push({ type: "toolcall_start", contentIndex: output.content.length - 1, partial: output });
232
+ }
233
+ }
234
+ else if (event.type === "content_block_delta") {
235
+ if (event.delta.type === "text_delta") {
236
+ const index = blocks.findIndex((b) => b.index === event.index);
237
+ const block = blocks[index];
238
+ if (block && block.type === "text") {
239
+ block.text += event.delta.text;
240
+ stream.push({
241
+ type: "text_delta",
242
+ contentIndex: index,
243
+ delta: event.delta.text,
244
+ partial: output,
245
+ });
246
+ }
247
+ }
248
+ else if (event.delta.type === "thinking_delta") {
249
+ const index = blocks.findIndex((b) => b.index === event.index);
250
+ const block = blocks[index];
251
+ if (block && block.type === "thinking") {
252
+ block.thinking += event.delta.thinking;
253
+ stream.push({
254
+ type: "thinking_delta",
255
+ contentIndex: index,
256
+ delta: event.delta.thinking,
257
+ partial: output,
258
+ });
259
+ }
260
+ }
261
+ else if (event.delta.type === "input_json_delta") {
262
+ const index = blocks.findIndex((b) => b.index === event.index);
263
+ const block = blocks[index];
264
+ if (block && block.type === "toolCall") {
265
+ block.partialJson += event.delta.partial_json;
266
+ block.arguments = parseStreamingJson(block.partialJson);
267
+ stream.push({
268
+ type: "toolcall_delta",
269
+ contentIndex: index,
270
+ delta: event.delta.partial_json,
271
+ partial: output,
272
+ });
273
+ }
274
+ }
275
+ else if (event.delta.type === "signature_delta") {
276
+ const index = blocks.findIndex((b) => b.index === event.index);
277
+ const block = blocks[index];
278
+ if (block && block.type === "thinking") {
279
+ block.thinkingSignature = block.thinkingSignature || "";
280
+ block.thinkingSignature += event.delta.signature;
281
+ }
282
+ }
283
+ }
284
+ else if (event.type === "content_block_stop") {
285
+ const index = blocks.findIndex((b) => b.index === event.index);
286
+ const block = blocks[index];
287
+ if (block) {
288
+ delete block.index;
289
+ if (block.type === "text") {
290
+ stream.push({
291
+ type: "text_end",
292
+ contentIndex: index,
293
+ content: block.text,
294
+ partial: output,
295
+ });
296
+ }
297
+ else if (block.type === "thinking") {
298
+ stream.push({
299
+ type: "thinking_end",
300
+ contentIndex: index,
301
+ content: block.thinking,
302
+ partial: output,
303
+ });
304
+ }
305
+ else if (block.type === "toolCall") {
306
+ block.arguments = parseStreamingJson(block.partialJson);
307
+ delete block.partialJson;
308
+ stream.push({
309
+ type: "toolcall_end",
310
+ contentIndex: index,
311
+ toolCall: block,
312
+ partial: output,
313
+ });
314
+ }
315
+ }
316
+ }
317
+ else if (event.type === "message_delta") {
318
+ if (event.delta.stop_reason) {
319
+ output.stopReason = mapStopReason(event.delta.stop_reason);
320
+ }
321
+ // Only update usage fields if present (not null).
322
+ // Preserves input_tokens from message_start when proxies omit it in message_delta.
323
+ if (event.usage.input_tokens != null) {
324
+ output.usage.input = event.usage.input_tokens;
325
+ }
326
+ if (event.usage.output_tokens != null) {
327
+ output.usage.output = event.usage.output_tokens;
328
+ }
329
+ if (event.usage.cache_read_input_tokens != null) {
330
+ output.usage.cacheRead = event.usage.cache_read_input_tokens;
331
+ }
332
+ if (event.usage.cache_creation_input_tokens != null) {
333
+ output.usage.cacheWrite = event.usage.cache_creation_input_tokens;
334
+ }
335
+ // Anthropic doesn't provide total_tokens, compute from components
336
+ output.usage.totalTokens =
337
+ output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
338
+ calculateCost(model, output.usage);
339
+ }
340
+ }
341
+ if (options?.signal?.aborted) {
342
+ throw new Error("Request was aborted");
343
+ }
344
+ if (output.stopReason === "aborted" || output.stopReason === "error") {
345
+ throw new Error("An unknown error occurred");
346
+ }
347
+ stream.push({ type: "done", reason: output.stopReason, message: output });
348
+ stream.end();
349
+ }
350
+ catch (error) {
351
+ for (const block of output.content)
352
+ delete block.index;
353
+ output.stopReason = options?.signal?.aborted ? "aborted" : "error";
354
+ output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
355
+ stream.push({ type: "error", reason: output.stopReason, error: output });
356
+ stream.end();
357
+ }
358
+ })();
359
+ return stream;
360
+ };
361
+ /**
362
+ * Map ThinkingLevel to Anthropic effort levels for adaptive thinking.
363
+ * Note: effort "max" is only valid on models whose capability table
364
+ * entry sets `xhighEffort: true` (today: Opus 4.6 / 4.7).
365
+ */
366
+ function mapThinkingLevelToEffort(level, model) {
367
+ switch (level) {
368
+ case "minimal":
369
+ return "low";
370
+ case "low":
371
+ return "low";
372
+ case "medium":
373
+ return "medium";
374
+ case "high":
375
+ return "high";
376
+ case "xhigh":
377
+ return getAnthropicCapabilities(model.id, model.provider).xhighEffort ? "max" : "high";
378
+ default:
379
+ return "high";
380
+ }
381
+ }
382
+ export const streamSimpleAnthropic = (model, context, options) => {
383
+ const apiKey = options?.apiKey || getEnvApiKey(model.provider);
384
+ if (!apiKey) {
385
+ throw new Error(`No API key for provider: ${model.provider}`);
386
+ }
387
+ const base = buildBaseOptions(model, options, apiKey);
388
+ if (!options?.reasoning) {
389
+ return streamAnthropic(model, context, { ...base, thinkingEnabled: false });
390
+ }
391
+ // For Opus 4.6 and Sonnet 4.6: use adaptive thinking with effort level
392
+ // For older models: use budget-based thinking
393
+ if (supportsAdaptiveThinking(model.id, model.provider)) {
394
+ const effort = mapThinkingLevelToEffort(options.reasoning, model);
395
+ return streamAnthropic(model, context, {
396
+ ...base,
397
+ thinkingEnabled: true,
398
+ effort,
399
+ });
400
+ }
401
+ const adjusted = adjustMaxTokensForThinking(base.maxTokens || 0, model.maxTokens, options.reasoning, options.thinkingBudgets);
402
+ return streamAnthropic(model, context, {
403
+ ...base,
404
+ maxTokens: adjusted.maxTokens,
405
+ thinkingEnabled: true,
406
+ thinkingBudgetTokens: adjusted.thinkingBudget,
407
+ });
408
+ };
409
+ function isOAuthToken(apiKey) {
410
+ return apiKey.includes("sk-ant-oat");
411
+ }
412
+ function createClient(model, apiKey, interleavedThinking, optionsHeaders, dynamicHeaders) {
413
+ // Adaptive thinking models (Opus 4.6, Sonnet 4.6) have interleaved thinking built-in.
414
+ // The beta header is deprecated on Opus 4.6 and redundant on Sonnet 4.6, so skip it.
415
+ const needsInterleavedBeta = interleavedThinking && !supportsAdaptiveThinking(model.id, model.provider);
416
+ // Opt into per-model capability betas (e.g. 1M context window).
417
+ const caps = getAnthropicCapabilities(model.id, model.provider);
418
+ const extraBetas = [];
419
+ if (caps.contextBeta) {
420
+ extraBetas.push(caps.contextBeta);
421
+ }
422
+ // Copilot: Bearer auth, selective betas (no fine-grained-tool-streaming)
423
+ if (model.provider === "github-copilot") {
424
+ const betaFeatures = [];
425
+ if (needsInterleavedBeta) {
426
+ betaFeatures.push("interleaved-thinking-2025-05-14");
427
+ }
428
+ betaFeatures.push(...extraBetas);
429
+ const client = new Anthropic({
430
+ apiKey: null,
431
+ authToken: apiKey,
432
+ baseURL: model.baseUrl,
433
+ dangerouslyAllowBrowser: true,
434
+ defaultHeaders: mergeHeaders({
435
+ accept: "application/json",
436
+ "anthropic-dangerous-direct-browser-access": "true",
437
+ ...(betaFeatures.length > 0 ? { "anthropic-beta": betaFeatures.join(",") } : {}),
438
+ }, model.headers, dynamicHeaders, optionsHeaders),
439
+ });
440
+ return { client, isOAuthToken: false };
441
+ }
442
+ const betaFeatures = ["fine-grained-tool-streaming-2025-05-14"];
443
+ if (needsInterleavedBeta) {
444
+ betaFeatures.push("interleaved-thinking-2025-05-14");
445
+ }
446
+ betaFeatures.push(...extraBetas);
447
+ // OAuth: Bearer auth, Claude Code identity headers
448
+ if (isOAuthToken(apiKey)) {
449
+ const client = new Anthropic({
450
+ apiKey: null,
451
+ authToken: apiKey,
452
+ baseURL: model.baseUrl,
453
+ dangerouslyAllowBrowser: true,
454
+ defaultHeaders: mergeHeaders({
455
+ accept: "application/json",
456
+ "anthropic-dangerous-direct-browser-access": "true",
457
+ "anthropic-beta": `claude-code-20250219,oauth-2025-04-20,${betaFeatures.join(",")}`,
458
+ "user-agent": `claude-cli/${claudeCodeVersion}`,
459
+ "x-app": "cli",
460
+ }, model.headers, optionsHeaders),
461
+ });
462
+ return { client, isOAuthToken: true };
463
+ }
464
+ // API key auth
465
+ const client = new Anthropic({
466
+ apiKey,
467
+ baseURL: model.baseUrl,
468
+ dangerouslyAllowBrowser: true,
469
+ defaultHeaders: mergeHeaders({
470
+ accept: "application/json",
471
+ "anthropic-dangerous-direct-browser-access": "true",
472
+ "anthropic-beta": betaFeatures.join(","),
473
+ }, model.headers, optionsHeaders),
474
+ });
475
+ return { client, isOAuthToken: false };
476
+ }
477
+ function buildParams(model, context, isOAuthToken, options) {
478
+ const { cacheControl } = getCacheControl(model.baseUrl, options?.cacheRetention);
479
+ const params = {
480
+ model: model.id,
481
+ messages: convertMessages(context.messages, model, isOAuthToken, cacheControl),
482
+ max_tokens: options?.maxTokens || (model.maxTokens / 3) | 0,
483
+ stream: true,
484
+ };
485
+ // For OAuth tokens, we MUST include Claude Code identity
486
+ if (isOAuthToken) {
487
+ params.system = [
488
+ {
489
+ type: "text",
490
+ text: "You are Claude Code, Anthropic's official CLI for Claude.",
491
+ ...(cacheControl ? { cache_control: cacheControl } : {}),
492
+ },
493
+ ];
494
+ if (context.systemPrompt) {
495
+ params.system.push({
496
+ type: "text",
497
+ text: sanitizeSurrogates(context.systemPrompt),
498
+ ...(cacheControl ? { cache_control: cacheControl } : {}),
499
+ });
500
+ }
501
+ }
502
+ else if (context.systemPrompt) {
503
+ // Add cache control to system prompt for non-OAuth tokens
504
+ params.system = [
505
+ {
506
+ type: "text",
507
+ text: sanitizeSurrogates(context.systemPrompt),
508
+ ...(cacheControl ? { cache_control: cacheControl } : {}),
509
+ },
510
+ ];
511
+ }
512
+ // Temperature is incompatible with extended thinking (adaptive or budget-based).
513
+ if (options?.temperature !== undefined && !options?.thinkingEnabled) {
514
+ params.temperature = options.temperature;
515
+ }
516
+ if (context.tools) {
517
+ params.tools = convertTools(context.tools, isOAuthToken);
518
+ // Experimental, env-gated (#42 cache probe): anchor a cache breakpoint on the
519
+ // last tool so [system+tools] is a named stable segment, independent of the
520
+ // rolling-tail breakpoint. Default OFF — no behavior change. Used only to
521
+ // measure whether this beats Anthropic's automatic prefix-matching.
522
+ if (cacheControl && process.env.CAVE_TOOLS_CACHE_BREAKPOINT === "1" && params.tools.length > 0) {
523
+ params.tools[params.tools.length - 1].cache_control =
524
+ cacheControl;
525
+ }
526
+ }
527
+ // Configure thinking mode: adaptive (Opus 4.6 and Sonnet 4.6),
528
+ // budget-based (older models), or explicitly disabled.
529
+ if (model.reasoning) {
530
+ if (options?.thinkingEnabled) {
531
+ if (supportsAdaptiveThinking(model.id, model.provider)) {
532
+ // Adaptive thinking: Claude decides when and how much to think
533
+ params.thinking = { type: "adaptive" };
534
+ if (options.effort) {
535
+ params.output_config = { effort: options.effort };
536
+ }
537
+ }
538
+ else {
539
+ // Budget-based thinking for older models
540
+ params.thinking = {
541
+ type: "enabled",
542
+ budget_tokens: options.thinkingBudgetTokens || 1024,
543
+ };
544
+ }
545
+ }
546
+ else if (options?.thinkingEnabled === false) {
547
+ params.thinking = { type: "disabled" };
548
+ }
549
+ }
550
+ if (options?.metadata) {
551
+ const userId = options.metadata.user_id;
552
+ if (typeof userId === "string") {
553
+ params.metadata = { user_id: userId };
554
+ }
555
+ }
556
+ if (options?.toolChoice) {
557
+ if (typeof options.toolChoice === "string") {
558
+ params.tool_choice = { type: options.toolChoice };
559
+ }
560
+ else {
561
+ params.tool_choice = options.toolChoice;
562
+ }
563
+ }
564
+ return params;
565
+ }
566
+ // Normalize tool call IDs to match Anthropic's required pattern and length
567
+ function normalizeToolCallId(id) {
568
+ return id.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64);
569
+ }
570
+ function convertMessages(messages, model, isOAuthToken, cacheControl) {
571
+ const params = [];
572
+ // Transform messages for cross-provider compatibility
573
+ const transformedMessages = transformMessages(messages, model, normalizeToolCallId);
574
+ for (let i = 0; i < transformedMessages.length; i++) {
575
+ const msg = transformedMessages[i];
576
+ if (msg.role === "user") {
577
+ if (typeof msg.content === "string") {
578
+ if (msg.content.trim().length > 0) {
579
+ params.push({
580
+ role: "user",
581
+ content: sanitizeSurrogates(msg.content),
582
+ });
583
+ }
584
+ }
585
+ else {
586
+ const blocks = msg.content.map((item) => {
587
+ if (item.type === "text") {
588
+ return {
589
+ type: "text",
590
+ text: sanitizeSurrogates(item.text),
591
+ };
592
+ }
593
+ else {
594
+ return {
595
+ type: "image",
596
+ source: {
597
+ type: "base64",
598
+ media_type: item.mimeType,
599
+ data: item.data,
600
+ },
601
+ };
602
+ }
603
+ });
604
+ let filteredBlocks = !model?.input.includes("image") ? blocks.filter((b) => b.type !== "image") : blocks;
605
+ filteredBlocks = filteredBlocks.filter((b) => {
606
+ if (b.type === "text") {
607
+ return b.text.trim().length > 0;
608
+ }
609
+ return true;
610
+ });
611
+ if (filteredBlocks.length === 0)
612
+ continue;
613
+ params.push({
614
+ role: "user",
615
+ content: filteredBlocks,
616
+ });
617
+ }
618
+ }
619
+ else if (msg.role === "assistant") {
620
+ const blocks = [];
621
+ for (const block of msg.content) {
622
+ if (block.type === "text") {
623
+ if (block.text.trim().length === 0)
624
+ continue;
625
+ blocks.push({
626
+ type: "text",
627
+ text: sanitizeSurrogates(block.text),
628
+ });
629
+ }
630
+ else if (block.type === "thinking") {
631
+ // Redacted thinking: pass the opaque payload back as redacted_thinking
632
+ if (block.redacted) {
633
+ blocks.push({
634
+ type: "redacted_thinking",
635
+ data: block.thinkingSignature,
636
+ });
637
+ continue;
638
+ }
639
+ if (block.thinking.trim().length === 0)
640
+ continue;
641
+ // If thinking signature is missing/empty (e.g., from aborted stream),
642
+ // convert to plain text block without <thinking> tags to avoid API rejection
643
+ // and prevent Claude from mimicking the tags in responses
644
+ if (!block.thinkingSignature || block.thinkingSignature.trim().length === 0) {
645
+ blocks.push({
646
+ type: "text",
647
+ text: sanitizeSurrogates(block.thinking),
648
+ });
649
+ }
650
+ else {
651
+ blocks.push({
652
+ type: "thinking",
653
+ thinking: sanitizeSurrogates(block.thinking),
654
+ signature: block.thinkingSignature,
655
+ });
656
+ }
657
+ }
658
+ else if (block.type === "toolCall") {
659
+ blocks.push({
660
+ type: "tool_use",
661
+ id: block.id,
662
+ name: isOAuthToken ? toClaudeCodeName(block.name) : block.name,
663
+ input: block.arguments ?? {},
664
+ });
665
+ }
666
+ }
667
+ if (blocks.length === 0)
668
+ continue;
669
+ params.push({
670
+ role: "assistant",
671
+ content: blocks,
672
+ });
673
+ }
674
+ else if (msg.role === "toolResult") {
675
+ // Collect all consecutive toolResult messages, needed for z.ai Anthropic endpoint
676
+ const toolResults = [];
677
+ // Add the current tool result
678
+ toolResults.push({
679
+ type: "tool_result",
680
+ tool_use_id: msg.toolCallId,
681
+ content: convertContentBlocks(msg.content),
682
+ is_error: msg.isError,
683
+ });
684
+ // Look ahead for consecutive toolResult messages
685
+ let j = i + 1;
686
+ while (j < transformedMessages.length && transformedMessages[j].role === "toolResult") {
687
+ const nextMsg = transformedMessages[j]; // We know it's a toolResult
688
+ toolResults.push({
689
+ type: "tool_result",
690
+ tool_use_id: nextMsg.toolCallId,
691
+ content: convertContentBlocks(nextMsg.content),
692
+ is_error: nextMsg.isError,
693
+ });
694
+ j++;
695
+ }
696
+ // Skip the messages we've already processed
697
+ i = j - 1;
698
+ // Add a single user message with all tool results
699
+ params.push({
700
+ role: "user",
701
+ content: toolResults,
702
+ });
703
+ }
704
+ }
705
+ // Add cache_control to the last user message to cache conversation history
706
+ if (cacheControl && params.length > 0) {
707
+ const lastMessage = params[params.length - 1];
708
+ if (lastMessage.role === "user") {
709
+ if (Array.isArray(lastMessage.content)) {
710
+ const lastBlock = lastMessage.content[lastMessage.content.length - 1];
711
+ if (lastBlock &&
712
+ (lastBlock.type === "text" || lastBlock.type === "image" || lastBlock.type === "tool_result")) {
713
+ lastBlock.cache_control = cacheControl;
714
+ }
715
+ }
716
+ else if (typeof lastMessage.content === "string") {
717
+ lastMessage.content = [
718
+ {
719
+ type: "text",
720
+ text: lastMessage.content,
721
+ cache_control: cacheControl,
722
+ },
723
+ ];
724
+ }
725
+ }
726
+ }
727
+ return params;
728
+ }
729
+ function convertTools(tools, isOAuthToken) {
730
+ if (!tools)
731
+ return [];
732
+ return tools.map((tool) => {
733
+ const jsonSchema = tool.parameters; // TypeBox already generates JSON Schema
734
+ return {
735
+ name: isOAuthToken ? toClaudeCodeName(tool.name) : tool.name,
736
+ description: tool.description,
737
+ input_schema: {
738
+ type: "object",
739
+ properties: jsonSchema.properties || {},
740
+ required: jsonSchema.required || [],
741
+ },
742
+ };
743
+ });
744
+ }
745
+ function mapStopReason(reason) {
746
+ switch (reason) {
747
+ case "end_turn":
748
+ return "stop";
749
+ case "max_tokens":
750
+ return "length";
751
+ case "tool_use":
752
+ return "toolUse";
753
+ case "refusal":
754
+ return "error";
755
+ case "pause_turn": // Stop is good enough -> resubmit
756
+ return "stop";
757
+ case "stop_sequence":
758
+ return "stop"; // We don't supply stop sequences, so this should never happen
759
+ case "sensitive": // Content flagged by safety filters (not yet in SDK types)
760
+ return "error";
761
+ default:
762
+ // Handle unknown stop reasons gracefully (API may add new values)
763
+ throw new Error(`Unhandled stop reason: ${reason}`);
764
+ }
765
+ }
766
+ //# sourceMappingURL=anthropic.js.map