@vacbo/opencode-anthropic-fix 0.0.44 → 0.0.45

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.
@@ -4,10 +4,16 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __getProtoOf = Object.getPrototypeOf;
6
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
8
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
9
+ }) : x)(function(x) {
10
+ if (typeof require !== "undefined") return require.apply(this, arguments);
11
+ throw Error('Dynamic require of "' + x + '" is not supported');
12
+ });
7
13
  var __esm = (fn, res) => function __init() {
8
14
  return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
9
15
  };
10
- var __commonJS = (cb, mod) => function __require() {
16
+ var __commonJS = (cb, mod) => function __require2() {
11
17
  return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
12
18
  };
13
19
  var __export = (target, all) => {
@@ -5558,14 +5564,25 @@ function buildAnthropicBillingHeader(claudeCliVersion, messages) {
5558
5564
  let versionSuffix = "";
5559
5565
  if (Array.isArray(messages)) {
5560
5566
  const firstUserMsg = messages.find(
5561
- (m) => m !== null && typeof m === "object" && m.role === "user" && typeof m.content === "string"
5567
+ (m) => m !== null && typeof m === "object" && m.role === "user"
5562
5568
  );
5563
5569
  if (firstUserMsg) {
5564
- const text = firstUserMsg.content;
5565
- const salt = "59cf53e54c78";
5566
- const picked = [4, 7, 20].map((i) => i < text.length ? text[i] : "0").join("");
5567
- const hash = createHash2("sha256").update(salt + picked + claudeCliVersion).digest("hex");
5568
- versionSuffix = `.${hash.slice(0, 3)}`;
5570
+ let text = "";
5571
+ const content = firstUserMsg.content;
5572
+ if (typeof content === "string") {
5573
+ text = content;
5574
+ } else if (Array.isArray(content)) {
5575
+ const textBlock = content.find((b) => b.type === "text");
5576
+ if (textBlock && typeof textBlock.text === "string") {
5577
+ text = textBlock.text;
5578
+ }
5579
+ }
5580
+ if (text) {
5581
+ const salt = "59cf53e54c78";
5582
+ const picked = [4, 7, 20].map((i) => i < text.length ? text[i] : "0").join("");
5583
+ const hash = createHash2("sha256").update(salt + picked + claudeCliVersion).digest("hex");
5584
+ versionSuffix = `.${hash.slice(0, 3)}`;
5585
+ }
5569
5586
  }
5570
5587
  }
5571
5588
  const entrypoint = process.env.CLAUDE_CODE_ENTRYPOINT || "cli";
@@ -6534,7 +6551,24 @@ function stopBunProxy() {
6534
6551
  async function fetchViaBun(input, init) {
6535
6552
  const port = await ensureBunProxy();
6536
6553
  const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
6537
- if (!port) return fetch(input, init);
6554
+ if (!port) {
6555
+ console.error("[bun-fetch] WARNING: No proxy available, using Node.js fetch (will route to extra usage!)");
6556
+ return fetch(input, init);
6557
+ }
6558
+ console.error(`[bun-fetch] Routing through Bun proxy at :${port} \u2192 ${url}`);
6559
+ if (init.body && url.includes("/v1/messages") && !url.includes("count_tokens")) {
6560
+ try {
6561
+ const { writeFileSync: writeFileSync6 } = __require("node:fs");
6562
+ writeFileSync6("/tmp/opencode-last-request.json", typeof init.body === "string" ? init.body : JSON.stringify(init.body));
6563
+ const hdrs = {};
6564
+ init.headers.forEach((v, k) => {
6565
+ hdrs[k] = k === "authorization" ? "Bearer ***" : v;
6566
+ });
6567
+ writeFileSync6("/tmp/opencode-last-headers.json", JSON.stringify(hdrs, null, 2));
6568
+ console.error("[bun-fetch] Dumped request to /tmp/opencode-last-request.json");
6569
+ } catch {
6570
+ }
6571
+ }
6538
6572
  const headers = new Headers(init.headers);
6539
6573
  headers.set("x-proxy-url", url);
6540
6574
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vacbo/opencode-anthropic-fix",
3
- "version": "0.0.44",
3
+ "version": "0.0.45",
4
4
  "main": "./dist/opencode-anthropic-auth-plugin.js",
5
5
  "bin": {
6
6
  "opencode-anthropic-auth": "./dist/opencode-anthropic-auth-cli.mjs",
package/src/bun-fetch.ts CHANGED
@@ -190,7 +190,24 @@ export async function fetchViaBun(
190
190
  const port = await ensureBunProxy();
191
191
  const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
192
192
 
193
- if (!port) return fetch(input, init as RequestInit);
193
+ if (!port) {
194
+ console.error("[bun-fetch] WARNING: No proxy available, using Node.js fetch (will route to extra usage!)");
195
+ return fetch(input, init as RequestInit);
196
+ }
197
+
198
+ console.error(`[bun-fetch] Routing through Bun proxy at :${port} → ${url}`);
199
+
200
+ // Dump full request for debugging
201
+ if (init.body && url.includes("/v1/messages") && !url.includes("count_tokens")) {
202
+ try {
203
+ const { writeFileSync } = require("node:fs");
204
+ writeFileSync("/tmp/opencode-last-request.json", typeof init.body === "string" ? init.body : JSON.stringify(init.body));
205
+ const hdrs: Record<string, string> = {};
206
+ init.headers.forEach((v: string, k: string) => { hdrs[k] = k === "authorization" ? "Bearer ***" : v; });
207
+ writeFileSync("/tmp/opencode-last-headers.json", JSON.stringify(hdrs, null, 2));
208
+ console.error("[bun-fetch] Dumped request to /tmp/opencode-last-request.json");
209
+ } catch { /* ignore */ }
210
+ }
194
211
 
195
212
  const headers = new Headers(init.headers);
196
213
  headers.set("x-proxy-url", url);
@@ -9,21 +9,33 @@ export function buildAnthropicBillingHeader(claudeCliVersion: string, messages:
9
9
  // the CLI version, then taking the first 3 hex chars of that combined string.
10
10
  let versionSuffix = "";
11
11
  if (Array.isArray(messages)) {
12
+ // Find first user message (CC uses first non-meta user turn)
12
13
  const firstUserMsg = messages.find(
13
14
  (m) =>
14
15
  m !== null &&
15
16
  typeof m === "object" &&
16
- (m as Record<string, unknown>).role === "user" &&
17
- typeof (m as Record<string, unknown>).content === "string",
17
+ (m as Record<string, unknown>).role === "user",
18
18
  ) as Record<string, unknown> | undefined;
19
19
  if (firstUserMsg) {
20
- const text = firstUserMsg.content as string;
21
- const salt = "59cf53e54c78";
22
- const picked = [4, 7, 20].map((i) => (i < text.length ? text[i] : "0")).join("");
23
- const hash = createHash("sha256")
24
- .update(salt + picked + claudeCliVersion)
25
- .digest("hex");
26
- versionSuffix = `.${hash.slice(0, 3)}`;
20
+ // Extract text from string or content-block array
21
+ let text = "";
22
+ const content = firstUserMsg.content;
23
+ if (typeof content === "string") {
24
+ text = content;
25
+ } else if (Array.isArray(content)) {
26
+ const textBlock = (content as Array<Record<string, unknown>>).find((b) => b.type === "text");
27
+ if (textBlock && typeof textBlock.text === "string") {
28
+ text = textBlock.text;
29
+ }
30
+ }
31
+ if (text) {
32
+ const salt = "59cf53e54c78";
33
+ const picked = [4, 7, 20].map((i) => (i < text.length ? text[i] : "0")).join("");
34
+ const hash = createHash("sha256")
35
+ .update(salt + picked + claudeCliVersion)
36
+ .digest("hex");
37
+ versionSuffix = `.${hash.slice(0, 3)}`;
38
+ }
27
39
  }
28
40
  }
29
41