@anthonyhaussman/opencode-agy-auth 1.3.5-alpha.0 → 1.3.5-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -2098,6 +2098,35 @@ function getLatestSignature(sessionId) {
2098
2098
  }
2099
2099
  return void 0;
2100
2100
  }
2101
+ function getSignatureForText(sessionId, text) {
2102
+ if (!sessionId || !text) return void 0;
2103
+ const textHash = hashText(text);
2104
+ const sessionMemCache = signatureCache.get(sessionId);
2105
+ if (sessionMemCache) {
2106
+ const entry = sessionMemCache.get(textHash);
2107
+ if (entry) {
2108
+ if (Date.now() - entry.timestamp <= SIGNATURE_CACHE_TTL_MS) {
2109
+ return entry.signature;
2110
+ }
2111
+ sessionMemCache.delete(textHash);
2112
+ }
2113
+ }
2114
+ if (diskCache) {
2115
+ const diskKey = makeDiskKey(sessionId, textHash);
2116
+ const diskValue = diskCache.retrieve(diskKey);
2117
+ if (diskValue) {
2118
+ if (!sessionMemCache) {
2119
+ const newMemCache = /* @__PURE__ */ new Map();
2120
+ signatureCache.set(sessionId, newMemCache);
2121
+ newMemCache.set(textHash, { signature: diskValue, timestamp: Date.now() });
2122
+ } else {
2123
+ sessionMemCache.set(textHash, { signature: diskValue, timestamp: Date.now() });
2124
+ }
2125
+ return diskValue;
2126
+ }
2127
+ }
2128
+ return void 0;
2129
+ }
2101
2130
 
2102
2131
  // src/sdk/request/turn-state-tracker.ts
2103
2132
  import { existsSync as existsSync4, mkdirSync as mkdirSync3, readFileSync as readFileSync4, writeFileSync as writeFileSync4, renameSync as renameSync3, unlinkSync as unlinkSync3 } from "fs";
@@ -2136,7 +2165,7 @@ function hashString(str) {
2136
2165
  }
2137
2166
  function isThinkingPart(part) {
2138
2167
  if (!part || typeof part !== "object") return false;
2139
- return part.thought === true || part.type === "thinking" || part.type === "redacted_thinking";
2168
+ return part.thought === true || part.type === "thinking" || part.type === "redacted_thinking" || part.type === "reasoning";
2140
2169
  }
2141
2170
  function isFunctionResponsePart(part) {
2142
2171
  return part && typeof part === "object" && "functionResponse" in part;
@@ -2156,7 +2185,7 @@ function messageHasThinking(msg) {
2156
2185
  }
2157
2186
  if (Array.isArray(msg.content)) {
2158
2187
  return msg.content.some(
2159
- (block) => block?.type === "thinking" || block?.type === "redacted_thinking"
2188
+ (block) => block?.type === "thinking" || block?.type === "redacted_thinking" || block?.type === "reasoning"
2160
2189
  );
2161
2190
  }
2162
2191
  return false;
@@ -2263,7 +2292,7 @@ function deduplicateThinkingText(response, sentBuffer, displayedThinkingHashes)
2263
2292
  if (!Array.isArray(content.parts)) return candidate;
2264
2293
  const newParts = content.parts.map((part) => {
2265
2294
  const p = part;
2266
- if (p.thought === true || p.type === "thinking") {
2295
+ if (p.thought === true || p.type === "thinking" || p.type === "reasoning") {
2267
2296
  const fullText = p.text || p.thinking || "";
2268
2297
  if (displayedThinkingHashes) {
2269
2298
  const hash2 = hashString(fullText);
@@ -2299,7 +2328,7 @@ function deduplicateThinkingText(response, sentBuffer, displayedThinkingHashes)
2299
2328
  let thinkingIndex = 0;
2300
2329
  const newContent = resp.content.map((block) => {
2301
2330
  const b = block;
2302
- if (b?.type === "thinking") {
2331
+ if (b?.type === "thinking" || b?.type === "reasoning") {
2303
2332
  const fullText = b.thinking || b.text || "";
2304
2333
  if (displayedThinkingHashes) {
2305
2334
  const hash2 = hashString(fullText);
@@ -2345,17 +2374,18 @@ function cacheThinkingSignaturesFromResponse(response, signatureSessionKey, sign
2345
2374
  if (!Array.isArray(content.parts)) return;
2346
2375
  content.parts.forEach((part) => {
2347
2376
  const p = part;
2348
- if (p.thought === true || p.type === "thinking") {
2377
+ if (p.thought === true || p.type === "thinking" || p.type === "reasoning") {
2349
2378
  const text = p.text || p.thinking || "";
2350
2379
  if (text) {
2351
2380
  const current = thoughtBuffer.get(index) ?? "";
2352
2381
  thoughtBuffer.set(index, current + text);
2353
2382
  }
2354
2383
  }
2355
- if (p.thoughtSignature) {
2384
+ const partSignature = p.thoughtSignature || p.state?.thoughtSignature || p.signature;
2385
+ if (partSignature) {
2356
2386
  const fullText = thoughtBuffer.get(index) ?? "";
2357
2387
  if (fullText) {
2358
- const signature = p.thoughtSignature;
2388
+ const signature = partSignature;
2359
2389
  onCacheSignature?.(signatureSessionKey, fullText, signature);
2360
2390
  signatureStore.set(signatureSessionKey, { text: fullText, signature });
2361
2391
  }
@@ -2367,17 +2397,18 @@ function cacheThinkingSignaturesFromResponse(response, signatureSessionKey, sign
2367
2397
  const CLAUDE_BUFFER_KEY = 0;
2368
2398
  resp.content.forEach((block) => {
2369
2399
  const b = block;
2370
- if (b?.type === "thinking") {
2400
+ if (b?.type === "thinking" || b?.type === "reasoning") {
2371
2401
  const text = b.thinking || b.text || "";
2372
2402
  if (text) {
2373
2403
  const current = thoughtBuffer.get(CLAUDE_BUFFER_KEY) ?? "";
2374
2404
  thoughtBuffer.set(CLAUDE_BUFFER_KEY, current + text);
2375
2405
  }
2376
2406
  }
2377
- if (b?.signature) {
2407
+ const blockSignature = b?.signature || b?.thoughtSignature || b?.state?.thoughtSignature;
2408
+ if (blockSignature) {
2378
2409
  const fullText = thoughtBuffer.get(CLAUDE_BUFFER_KEY) ?? "";
2379
2410
  if (fullText) {
2380
- const signature = b.signature;
2411
+ const signature = blockSignature;
2381
2412
  onCacheSignature?.(signatureSessionKey, fullText, signature);
2382
2413
  signatureStore.set(signatureSessionKey, { text: fullText, signature });
2383
2414
  }
@@ -2453,7 +2484,7 @@ function createStreamingTransformer(signatureStore, callbacks, options = {}) {
2453
2484
  hasSeenUsageMetadata = true;
2454
2485
  }
2455
2486
  if (!streamHasThinking) {
2456
- streamHasThinking = event.includes('"thought":true') || event.includes('"type":"thinking"');
2487
+ streamHasThinking = event.includes('"thought":true') || event.includes('"type":"thinking"') || event.includes('"type":"reasoning"');
2457
2488
  }
2458
2489
  if (!streamHasToolCalls) {
2459
2490
  streamHasToolCalls = event.includes('"functionCall"');
@@ -2477,7 +2508,7 @@ function createStreamingTransformer(signatureStore, callbacks, options = {}) {
2477
2508
  hasSeenUsageMetadata = true;
2478
2509
  }
2479
2510
  if (!streamHasThinking) {
2480
- streamHasThinking = buffer.includes('"thought":true') || buffer.includes('"type":"thinking"');
2511
+ streamHasThinking = buffer.includes('"thought":true') || buffer.includes('"type":"thinking"') || buffer.includes('"type":"reasoning"');
2481
2512
  }
2482
2513
  if (!streamHasToolCalls) {
2483
2514
  streamHasToolCalls = buffer.includes('"functionCall"');
@@ -18903,7 +18934,7 @@ function getModelEnum(modelName) {
18903
18934
  function isClaudeModel(model) {
18904
18935
  return model.toLowerCase().includes("claude");
18905
18936
  }
18906
- function sanitizeClaudeThinkingParts(contents, model) {
18937
+ function sanitizeClaudeThinkingParts(contents, model, sessionId) {
18907
18938
  if (!isClaudeModel(model) || !Array.isArray(contents)) {
18908
18939
  return;
18909
18940
  }
@@ -18913,12 +18944,49 @@ function sanitizeClaudeThinkingParts(contents, model) {
18913
18944
  if (!Array.isArray(turn.parts)) continue;
18914
18945
  const filteredParts = turn.parts.filter((part) => {
18915
18946
  if (!part || typeof part !== "object") return true;
18916
- const isThinking = part.thought === true || part.type === "thinking" || typeof part.thinking === "string";
18947
+ const isThinking = part.thought === true || part.type === "thinking" || part.type === "reasoning" || typeof part.thinking === "string";
18917
18948
  if (!isThinking) {
18918
18949
  return true;
18919
18950
  }
18920
- const sig = typeof part.signature === "string" ? part.signature : typeof part.thoughtSignature === "string" ? part.thoughtSignature : void 0;
18951
+ let sig = typeof part.signature === "string" ? part.signature : typeof part.thoughtSignature === "string" ? part.thoughtSignature : typeof part.state?.thoughtSignature === "string" ? part.state.thoughtSignature : void 0;
18952
+ let recovered = false;
18953
+ if ((!sig || sig === "skip_thought_signature_validator") && sessionId) {
18954
+ const text = typeof part.text === "string" ? part.text : typeof part.thinking === "string" ? part.thinking : void 0;
18955
+ if (text) {
18956
+ const recoveredSig = getSignatureForText(sessionId, text);
18957
+ if (recoveredSig) {
18958
+ sig = recoveredSig;
18959
+ recovered = true;
18960
+ }
18961
+ }
18962
+ if (!sig) {
18963
+ const latest = getLatestSignature(sessionId);
18964
+ if (latest) {
18965
+ sig = latest;
18966
+ recovered = true;
18967
+ }
18968
+ }
18969
+ }
18921
18970
  const isValidSig = typeof sig === "string" && sig.length > 0 && sig !== "skip_thought_signature_validator";
18971
+ if (isValidSig) {
18972
+ if (part.type === "reasoning") {
18973
+ part.thoughtSignature = sig;
18974
+ part.signature = sig;
18975
+ if (part.state) {
18976
+ part.state.thoughtSignature = sig;
18977
+ }
18978
+ } else if (recovered) {
18979
+ if (typeof part.thoughtSignature === "string" || part.thought) {
18980
+ part.thoughtSignature = sig;
18981
+ }
18982
+ if (part.type === "thinking" || typeof part.thinking === "string") {
18983
+ part.signature = sig;
18984
+ }
18985
+ if (!part.thoughtSignature && !part.signature) {
18986
+ part.signature = sig;
18987
+ }
18988
+ }
18989
+ }
18922
18990
  return isValidSig;
18923
18991
  });
18924
18992
  if (filteredParts.length === 0) {
@@ -18989,7 +19057,7 @@ function transformRequestBody(body, projectId, effectiveModel, requestedModel, t
18989
19057
  contents2 = ensureTrailingUserTurn(contents2);
18990
19058
  const latestSig = getLatestSignature(sessionId2);
18991
19059
  applyLatestSignature(contents2, latestSig);
18992
- sanitizeClaudeThinkingParts(contents2, effectiveModel || requestedModel);
19060
+ sanitizeClaudeThinkingParts(contents2, effectiveModel || requestedModel, sessionId2);
18993
19061
  requestPayloadInside.contents = contents2;
18994
19062
  }
18995
19063
  return { body: JSON.stringify(wrappedBody2), userPromptId: userPromptId2, sessionId: sessionId2 };
@@ -19031,7 +19099,7 @@ function transformRequestBody(body, projectId, effectiveModel, requestedModel, t
19031
19099
  contents = ensureTrailingUserTurn(contents);
19032
19100
  const latestSig = getLatestSignature(sessionId);
19033
19101
  applyLatestSignature(contents, latestSig);
19034
- sanitizeClaudeThinkingParts(contents, effectiveModel || requestedModel);
19102
+ sanitizeClaudeThinkingParts(contents, effectiveModel || requestedModel, sessionId);
19035
19103
  requestPayload.contents = contents;
19036
19104
  }
19037
19105
  if ("model" in requestPayload) {
@@ -20469,6 +20537,26 @@ function createV2HttpRequestHook(state = {}) {
20469
20537
  }
20470
20538
  } catch {
20471
20539
  }
20540
+ let bodyPayload = event.body;
20541
+ if (bodyPayload === void 0 && event.request) {
20542
+ if (typeof event.request.clone === "function") {
20543
+ try {
20544
+ bodyPayload = await event.request.clone().text();
20545
+ } catch {
20546
+ }
20547
+ } else if (event.request.body !== void 0) {
20548
+ bodyPayload = event.request.body;
20549
+ }
20550
+ }
20551
+ if (bodyPayload) {
20552
+ try {
20553
+ const parsed2 = typeof bodyPayload === "string" ? JSON.parse(bodyPayload) : bodyPayload;
20554
+ if (parsed2 && typeof parsed2 === "object" && typeof parsed2.sessionId === "string") {
20555
+ state.lastSessionId = parsed2.sessionId;
20556
+ }
20557
+ } catch {
20558
+ }
20559
+ }
20472
20560
  if (authRecord && authRecord.access) {
20473
20561
  let projectContext;
20474
20562
  try {
@@ -20495,17 +20583,6 @@ function createV2HttpRequestHook(state = {}) {
20495
20583
  modifiedUrl = modifiedUrl.replace(`models/${originalBase}`, `models/${resolvedBase}`);
20496
20584
  }
20497
20585
  }
20498
- let bodyPayload = event.body;
20499
- if (bodyPayload === void 0 && event.request) {
20500
- if (typeof event.request.clone === "function") {
20501
- try {
20502
- bodyPayload = await event.request.clone().text();
20503
- } catch {
20504
- }
20505
- } else if (event.request.body !== void 0) {
20506
- bodyPayload = event.request.body;
20507
- }
20508
- }
20509
20586
  let preExistingUserAgent;
20510
20587
  if (typeof event.headers?.get === "function") {
20511
20588
  preExistingUserAgent = event.headers.get("user-agent") || event.headers.get("User-Agent") || void 0;
@@ -20529,6 +20606,9 @@ function createV2HttpRequestHook(state = {}) {
20529
20606
  authRecord.access,
20530
20607
  projectContext.effectiveProjectId
20531
20608
  );
20609
+ if (transformed.sessionId) {
20610
+ state.lastSessionId = transformed.sessionId;
20611
+ }
20532
20612
  const targetUrl = toUrlString(transformed.request);
20533
20613
  if (typeof event.url === "string") {
20534
20614
  event.url = targetUrl;
@@ -20645,7 +20725,7 @@ function createV2HttpResponseHook(state = {}) {
20645
20725
  !!isStreaming,
20646
20726
  null,
20647
20727
  parsedModel,
20648
- void 0,
20728
+ state.lastSessionId,
20649
20729
  void 0
20650
20730
  );
20651
20731
  event.response = transformed;
@@ -20687,6 +20767,26 @@ function registerV2SessionHooks(ctx) {
20687
20767
 
20688
20768
  // src/plugin/v2.ts
20689
20769
  async function setupOpenCodeV2(ctx) {
20770
+ try {
20771
+ initDiskSignatureCache({
20772
+ enabled: true,
20773
+ memory_ttl_seconds: 3600,
20774
+ disk_ttl_seconds: 86400,
20775
+ write_interval_seconds: 30
20776
+ });
20777
+ } catch (e) {
20778
+ console.warn(`[Agy Auth] initDiskSignatureCache failed, running without signature disk cache: ${e instanceof Error ? e.message : e}`);
20779
+ }
20780
+ try {
20781
+ initTurnStateTracker();
20782
+ } catch (e) {
20783
+ console.warn(`[Agy Auth] initTurnStateTracker failed, running without turn-state tracking: ${e instanceof Error ? e.message : e}`);
20784
+ }
20785
+ try {
20786
+ initCooldownPersistence();
20787
+ } catch (e) {
20788
+ console.warn(`[Agy Auth] initCooldownPersistence failed, running without cooldown disk persistence: ${e instanceof Error ? e.message : e}`);
20789
+ }
20690
20790
  const resolveVariants = (modelId) => {
20691
20791
  const mapping = TIER_MAPPING[modelId];
20692
20792
  if (!mapping) return void 0;