@anthonyhaussman/opencode-agy-auth 1.1.13 → 1.1.14-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -17756,7 +17756,7 @@ function normalizeRequestPayloadIdentifiers(payload) {
17756
17756
  }
17757
17757
 
17758
17758
  // src/sdk/request/openai.ts
17759
- function transformOpenAIToolCalls(requestPayload) {
17759
+ function transformOpenAIToolCalls(requestPayload, toolMapper) {
17760
17760
  const messages = requestPayload.messages;
17761
17761
  if (!messages || !Array.isArray(messages)) {
17762
17762
  return;
@@ -17782,10 +17782,11 @@ function transformOpenAIToolCalls(requestPayload) {
17782
17782
  if (!fn || typeof fn !== "object") {
17783
17783
  continue;
17784
17784
  }
17785
- const name = fn.name;
17785
+ const rawName = fn.name ?? "";
17786
+ const name = toolMapper ? toolMapper.toGemini(rawName) : rawName;
17786
17787
  const args = parseJsonObject(fn.arguments);
17787
17788
  const functionCallPart = {
17788
- name: name ?? "",
17789
+ name,
17789
17790
  args
17790
17791
  };
17791
17792
  if (typeof toolCall.id === "string" && toolCall.id.length > 0) {
@@ -17845,6 +17846,165 @@ function parseJsonObject(value) {
17845
17846
  }
17846
17847
  }
17847
17848
 
17849
+ // src/sdk/request/tool-mapper.ts
17850
+ function sanitizeToolName(name) {
17851
+ if (!name || typeof name !== "string") {
17852
+ return "unnamed_tool";
17853
+ }
17854
+ let sanitized = name.replace(/[^a-zA-Z0-9_]/g, "_");
17855
+ if (!/^[a-zA-Z_]/.test(sanitized)) {
17856
+ sanitized = `_${sanitized}`;
17857
+ }
17858
+ return sanitized;
17859
+ }
17860
+ var ToolMapper = class {
17861
+ originalToSanitized = /* @__PURE__ */ new Map();
17862
+ sanitizedToOriginal = /* @__PURE__ */ new Map();
17863
+ /**
17864
+ * Register a tool name and get its Gemini-compliant sanitized name.
17865
+ * Handles naming collisions by appending a numeric suffix if needed.
17866
+ */
17867
+ register(originalName) {
17868
+ if (!originalName || typeof originalName !== "string") {
17869
+ return originalName;
17870
+ }
17871
+ const existing = this.originalToSanitized.get(originalName);
17872
+ if (existing) {
17873
+ return existing;
17874
+ }
17875
+ const baseSanitized = sanitizeToolName(originalName);
17876
+ let sanitized = baseSanitized;
17877
+ let counter = 1;
17878
+ while (this.sanitizedToOriginal.has(sanitized) && this.sanitizedToOriginal.get(sanitized) !== originalName) {
17879
+ sanitized = `${baseSanitized}_${counter++}`;
17880
+ }
17881
+ this.originalToSanitized.set(originalName, sanitized);
17882
+ this.sanitizedToOriginal.set(sanitized, originalName);
17883
+ return sanitized;
17884
+ }
17885
+ /**
17886
+ * Map an original tool name to sanitized Gemini name.
17887
+ * If not already registered, registers it on the fly.
17888
+ */
17889
+ toGemini(originalName) {
17890
+ if (!originalName || typeof originalName !== "string") {
17891
+ return originalName;
17892
+ }
17893
+ const sanitized = this.originalToSanitized.get(originalName);
17894
+ if (sanitized) {
17895
+ return sanitized;
17896
+ }
17897
+ return this.register(originalName);
17898
+ }
17899
+ /**
17900
+ * Restore a sanitized Gemini tool name back to the original client tool name.
17901
+ */
17902
+ fromGemini(sanitizedName) {
17903
+ if (!sanitizedName || typeof sanitizedName !== "string") {
17904
+ return sanitizedName;
17905
+ }
17906
+ return this.sanitizedToOriginal.get(sanitizedName) ?? sanitizedName;
17907
+ }
17908
+ /**
17909
+ * Register tools from Gemini `tools[].functionDeclarations` array.
17910
+ */
17911
+ registerFromFunctionDeclarations(tools) {
17912
+ if (!Array.isArray(tools)) return;
17913
+ for (const tool2 of tools) {
17914
+ if (tool2 && Array.isArray(tool2.functionDeclarations)) {
17915
+ for (const fn of tool2.functionDeclarations) {
17916
+ if (fn && typeof fn.name === "string") {
17917
+ this.register(fn.name);
17918
+ }
17919
+ }
17920
+ }
17921
+ }
17922
+ }
17923
+ /**
17924
+ * Register tools from OpenAI format `tools[].function.name`.
17925
+ */
17926
+ registerFromOpenAITools(tools) {
17927
+ if (!Array.isArray(tools)) return;
17928
+ for (const tool2 of tools) {
17929
+ if (tool2 && typeof tool2 === "object") {
17930
+ const fn = tool2.function;
17931
+ if (fn && typeof fn.name === "string") {
17932
+ this.register(fn.name);
17933
+ }
17934
+ }
17935
+ }
17936
+ }
17937
+ /**
17938
+ * Scan contents/messages to register any previously used tool names.
17939
+ */
17940
+ registerFromContents(contents) {
17941
+ if (!Array.isArray(contents)) return;
17942
+ for (const content of contents) {
17943
+ if (!content || typeof content !== "object") continue;
17944
+ const parts = content.parts;
17945
+ if (Array.isArray(parts)) {
17946
+ for (const part of parts) {
17947
+ if (!part || typeof part !== "object") continue;
17948
+ const p = part;
17949
+ if (p.functionCall && typeof p.functionCall.name === "string") {
17950
+ this.register(p.functionCall.name);
17951
+ }
17952
+ if (p.functionResponse && typeof p.functionResponse.name === "string") {
17953
+ this.register(p.functionResponse.name);
17954
+ }
17955
+ }
17956
+ }
17957
+ }
17958
+ }
17959
+ };
17960
+ var sessionMappers = /* @__PURE__ */ new Map();
17961
+ var MAX_SESSION_AGE_MS = 24 * 60 * 60 * 1e3;
17962
+ function getToolMapper(sessionId) {
17963
+ if (!sessionId) {
17964
+ return new ToolMapper();
17965
+ }
17966
+ const now = Date.now();
17967
+ const existing = sessionMappers.get(sessionId);
17968
+ if (existing && now - existing.updatedAt < MAX_SESSION_AGE_MS) {
17969
+ existing.updatedAt = now;
17970
+ return existing.mapper;
17971
+ }
17972
+ if (sessionMappers.size > 1e3) {
17973
+ for (const [key, value] of sessionMappers.entries()) {
17974
+ if (now - value.updatedAt >= MAX_SESSION_AGE_MS) {
17975
+ sessionMappers.delete(key);
17976
+ }
17977
+ }
17978
+ }
17979
+ const mapper = new ToolMapper();
17980
+ sessionMappers.set(sessionId, { mapper, updatedAt: now });
17981
+ return mapper;
17982
+ }
17983
+ function restoreToolNamesInResponse(body, toolMapper) {
17984
+ if (!body || typeof body !== "object") return;
17985
+ const b = body;
17986
+ const target = b.response && typeof b.response === "object" ? b.response : b;
17987
+ const candidates = target.candidates;
17988
+ if (Array.isArray(candidates)) {
17989
+ for (const cand of candidates) {
17990
+ if (!cand || typeof cand !== "object") continue;
17991
+ const content = cand.content;
17992
+ if (!content || typeof content !== "object") continue;
17993
+ const parts = content.parts;
17994
+ if (Array.isArray(parts)) {
17995
+ for (const part of parts) {
17996
+ if (!part || typeof part !== "object") continue;
17997
+ const p = part;
17998
+ if (p.functionCall && typeof p.functionCall.name === "string") {
17999
+ const fnCall = p.functionCall;
18000
+ fnCall.name = toolMapper.fromGemini(fnCall.name);
18001
+ }
18002
+ }
18003
+ }
18004
+ }
18005
+ }
18006
+ }
18007
+
17848
18008
  // src/sdk/request/prepare.ts
17849
18009
  var STREAM_ACTION = "streamGenerateContent";
17850
18010
  function prepareAgyRequest(input, init, accessToken, projectId, thinkingConfigDefaults) {
@@ -17933,8 +18093,11 @@ function transformRequestBody(body, projectId, effectiveModel, requestedModel, t
17933
18093
  wrappedBody2.userAgent = wrappedBody2.userAgent || "antigravity";
17934
18094
  }
17935
18095
  const { userPromptId: userPromptId2, sessionId: sessionId2, requestId: requestId2 } = normalizeWrappedIdentifiers(wrappedBody2);
18096
+ const toolMapper2 = getToolMapper(sessionId2);
17936
18097
  const requestPayloadInside = wrappedBody2.request;
17937
18098
  if (requestPayloadInside) {
18099
+ toolMapper2.registerFromFunctionDeclarations(requestPayloadInside.tools);
18100
+ toolMapper2.registerFromContents(requestPayloadInside.contents);
17938
18101
  normalizeThinking(
17939
18102
  requestPayloadInside,
17940
18103
  resolveDefaultThinkingConfig(thinkingConfigDefaults, requestedModel, effectiveModel),
@@ -17952,10 +18115,14 @@ function transformRequestBody(body, projectId, effectiveModel, requestedModel, t
17952
18115
  };
17953
18116
  }
17954
18117
  if (requestPayloadInside && Array.isArray(requestPayloadInside.tools)) {
17955
- normalizeToolSchemaTypes(requestPayloadInside.tools);
18118
+ normalizeToolSchemaTypes(requestPayloadInside.tools, toolMapper2);
18119
+ }
18120
+ if (requestPayloadInside) {
18121
+ normalizeToolConfig(requestPayloadInside, toolMapper2);
17956
18122
  }
17957
18123
  if (requestPayloadInside && Array.isArray(requestPayloadInside.contents)) {
17958
18124
  let contents2 = requestPayloadInside.contents;
18125
+ normalizeToolNamesInContents(contents2, toolMapper2);
17959
18126
  injectMissingToolCallIds(contents2);
17960
18127
  fixOrphanedFunctionResponses(contents2);
17961
18128
  const tracker = getTurnStateTracker();
@@ -17975,10 +18142,16 @@ function transformRequestBody(body, projectId, effectiveModel, requestedModel, t
17975
18142
  return { body: JSON.stringify(wrappedBody2), userPromptId: userPromptId2, sessionId: sessionId2 };
17976
18143
  }
17977
18144
  const requestPayload = { ...parsedBody };
18145
+ const { userPromptId, sessionId, requestId } = normalizeRequestPayloadIdentifiers(requestPayload);
18146
+ const toolMapper = getToolMapper(sessionId);
18147
+ toolMapper.registerFromOpenAITools(requestPayload.tools);
18148
+ toolMapper.registerFromFunctionDeclarations(requestPayload.tools);
18149
+ toolMapper.registerFromContents(requestPayload.contents);
17978
18150
  if (Array.isArray(requestPayload.tools)) {
17979
- normalizeToolSchemaTypes(requestPayload.tools);
18151
+ normalizeToolSchemaTypes(requestPayload.tools, toolMapper);
17980
18152
  }
17981
- transformOpenAIToolCalls(requestPayload);
18153
+ normalizeToolConfig(requestPayload, toolMapper);
18154
+ transformOpenAIToolCalls(requestPayload, toolMapper);
17982
18155
  addThoughtSignaturesToFunctionCalls(requestPayload);
17983
18156
  normalizeThinking(
17984
18157
  requestPayload,
@@ -17987,9 +18160,9 @@ function transformRequestBody(body, projectId, effectiveModel, requestedModel, t
17987
18160
  );
17988
18161
  normalizeSystemInstruction(requestPayload);
17989
18162
  normalizeCachedContent(requestPayload);
17990
- const { userPromptId, sessionId, requestId } = normalizeRequestPayloadIdentifiers(requestPayload);
17991
18163
  let contents = requestPayload.contents;
17992
18164
  if (Array.isArray(contents)) {
18165
+ normalizeToolNamesInContents(contents, toolMapper);
17993
18166
  injectMissingToolCallIds(contents);
17994
18167
  fixOrphanedFunctionResponses(contents);
17995
18168
  const tracker = getTurnStateTracker();
@@ -18103,7 +18276,7 @@ function mergeThinkingConfigs(...configs) {
18103
18276
  function isRecord2(value) {
18104
18277
  return !!value && typeof value === "object" && !Array.isArray(value);
18105
18278
  }
18106
- function normalizeToolSchemaTypes(tools) {
18279
+ function normalizeToolSchemaTypes(tools, toolMapper) {
18107
18280
  if (!Array.isArray(tools)) return;
18108
18281
  const validSchemaKeys = /* @__PURE__ */ new Set([
18109
18282
  "type",
@@ -18154,7 +18327,7 @@ function normalizeToolSchemaTypes(tools) {
18154
18327
  if (tool2 && Array.isArray(tool2.functionDeclarations)) {
18155
18328
  for (const fn of tool2.functionDeclarations) {
18156
18329
  if (fn && typeof fn.name === "string") {
18157
- fn.name = fn.name.replace(/[^a-zA-Z0-9_]/g, "_");
18330
+ fn.name = toolMapper ? toolMapper.toGemini(fn.name) : sanitizeToolName(fn.name);
18158
18331
  }
18159
18332
  if (fn) {
18160
18333
  if (!fn.parameters) {
@@ -18166,6 +18339,37 @@ function normalizeToolSchemaTypes(tools) {
18166
18339
  }
18167
18340
  }
18168
18341
  }
18342
+ function normalizeToolConfig(requestPayload, toolMapper) {
18343
+ const toolConfig = requestPayload.toolConfig ?? requestPayload.tool_config;
18344
+ if (!toolConfig || typeof toolConfig !== "object") return;
18345
+ const fnCallingConfig = toolConfig.functionCallingConfig ?? toolConfig.function_calling_config;
18346
+ if (!fnCallingConfig || typeof fnCallingConfig !== "object") return;
18347
+ const allowedNames = fnCallingConfig.allowedFunctionNames ?? fnCallingConfig.allowed_function_names;
18348
+ if (Array.isArray(allowedNames)) {
18349
+ const mapped = allowedNames.map((name) => typeof name === "string" ? toolMapper.toGemini(name) : name);
18350
+ if (fnCallingConfig.allowedFunctionNames) {
18351
+ fnCallingConfig.allowedFunctionNames = mapped;
18352
+ }
18353
+ if (fnCallingConfig.allowed_function_names) {
18354
+ fnCallingConfig.allowed_function_names = mapped;
18355
+ }
18356
+ }
18357
+ }
18358
+ function normalizeToolNamesInContents(contents, toolMapper) {
18359
+ if (!Array.isArray(contents)) return;
18360
+ for (const msg of contents) {
18361
+ if (!msg || typeof msg !== "object" || !Array.isArray(msg.parts)) continue;
18362
+ for (const part of msg.parts) {
18363
+ if (!part || typeof part !== "object") continue;
18364
+ if (part.functionCall && typeof part.functionCall.name === "string") {
18365
+ part.functionCall.name = toolMapper.toGemini(part.functionCall.name);
18366
+ }
18367
+ if (part.functionResponse && typeof part.functionResponse.name === "string") {
18368
+ part.functionResponse.name = toolMapper.toGemini(part.functionResponse.name);
18369
+ }
18370
+ }
18371
+ }
18372
+ }
18169
18373
  function normalizeSystemInstruction(requestPayload) {
18170
18374
  if ("system_instruction" in requestPayload) {
18171
18375
  requestPayload.systemInstruction = requestPayload.system_instruction;
@@ -18342,6 +18546,10 @@ async function transformAgyResponse(response, streaming, _ignoredDebugContext, r
18342
18546
  const previewPatched = parsed ? rewriteGeminiPreviewAccessError(enhanced?.body ?? parsed, response.status, requestedModel) : null;
18343
18547
  const effectiveBodyRaw = previewPatched ?? enhanced?.body ?? parsed ?? void 0;
18344
18548
  const effectiveBody = effectiveBodyRaw && typeof effectiveBodyRaw === "object" ? injectResponseIdFromTrace(effectiveBodyRaw) : effectiveBodyRaw;
18549
+ if (effectiveBody) {
18550
+ const toolMapper = getToolMapper(sessionId);
18551
+ restoreToolNamesInResponse(effectiveBody, toolMapper);
18552
+ }
18345
18553
  attachUsageHeaders(headers, effectiveBody);
18346
18554
  if (!parsed) {
18347
18555
  return new Response(text, init);
@@ -18396,6 +18604,8 @@ function transformStreamingPayloadStream(stream, sessionId, chatLogger) {
18396
18604
  },
18397
18605
  transformThinkingParts: (response) => {
18398
18606
  if (response && typeof response === "object") {
18607
+ const toolMapper = getToolMapper(sessionId);
18608
+ restoreToolNamesInResponse(response, toolMapper);
18399
18609
  return injectResponseIdFromTrace(response);
18400
18610
  }
18401
18611
  return response;