@opengeni/runtime 6.7.0 → 6.8.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/anthropic-messages.d.ts +14 -1
- package/dist/assets/codemode-client.json +1 -1
- package/dist/{chunk-3MNZZ2B4.js → chunk-2MBI6D2F.js} +2 -2
- package/dist/{chunk-JY5NSZ37.js → chunk-5K2NI7NX.js} +15 -13
- package/dist/chunk-5K2NI7NX.js.map +1 -0
- package/dist/{chunk-77NQGL66.js → chunk-6MHS5OUN.js} +1 -1
- package/dist/chunk-6MHS5OUN.js.map +1 -0
- package/dist/{chunk-RZJXTSVA.js → chunk-E2YB7OIA.js} +2 -2
- package/dist/{chunk-JF5UTPEU.js → chunk-JRM6HEPY.js} +2 -2
- package/dist/{chunk-GFAFQMZV.js → chunk-QNQGZKHJ.js} +3 -3
- package/dist/{chunk-FNAYH3SN.js → chunk-RXCYGCX7.js} +1282 -477
- package/dist/chunk-RXCYGCX7.js.map +1 -0
- package/dist/{chunk-OAW6MMSY.js → chunk-V7MCFH4O.js} +3 -3
- package/dist/gmail-rest-mcp.js +3 -3
- package/dist/google-calendar-rest-mcp.js +2 -2
- package/dist/index.js +8 -8
- package/dist/metrics.d.ts +15 -0
- package/dist/model-call.js +1 -1
- package/dist/model-provider-client.d.ts +3 -0
- package/dist/provider-history-adapter.d.ts +0 -6
- package/dist/provider-invalid-request.d.ts +12 -0
- package/dist/provider-request-tool-names.d.ts +134 -0
- package/dist/provider-tool-names.d.ts +4 -5
- package/dist/replayable-json-body.d.ts +5 -1
- package/dist/sandbox/index.js +2 -2
- package/dist/sandbox/routing/backend-resolver.d.ts +6 -5
- package/dist/slack-rest-mcp.js +3 -3
- package/dist/workspace-tool-gateway.js +8 -8
- package/package.json +15 -15
- package/src/anthropic-messages.ts +98 -11
- package/src/index.ts +2 -1
- package/src/metrics.ts +15 -0
- package/src/model-provider-client.ts +16 -0
- package/src/model-provider-routing.ts +2 -3
- package/src/provider-history-adapter.ts +6 -21
- package/src/provider-invalid-request.ts +90 -19
- package/src/provider-request-tool-names.ts +900 -0
- package/src/provider-tool-names.ts +14 -20
- package/src/replayable-json-body.ts +171 -3
- package/src/sandbox/routing/backend-resolver.ts +9 -9
- package/src/single-model-call.ts +2 -1
- package/dist/chunk-77NQGL66.js.map +0 -1
- package/dist/chunk-FNAYH3SN.js.map +0 -1
- package/dist/chunk-JY5NSZ37.js.map +0 -1
- /package/dist/{chunk-3MNZZ2B4.js.map → chunk-2MBI6D2F.js.map} +0 -0
- /package/dist/{chunk-RZJXTSVA.js.map → chunk-E2YB7OIA.js.map} +0 -0
- /package/dist/{chunk-JF5UTPEU.js.map → chunk-JRM6HEPY.js.map} +0 -0
- /package/dist/{chunk-GFAFQMZV.js.map → chunk-QNQGZKHJ.js.map} +0 -0
- /package/dist/{chunk-OAW6MMSY.js.map → chunk-V7MCFH4O.js.map} +0 -0
|
@@ -9532,25 +9532,6 @@ async function* streamWithPreemption(request, preemption, open) {
|
|
|
9532
9532
|
}
|
|
9533
9533
|
}
|
|
9534
9534
|
|
|
9535
|
-
// src/mcp-model-tool-name.ts
|
|
9536
|
-
import { createHash as createHash4 } from "crypto";
|
|
9537
|
-
import { prefixedMcpToolName as plainPrefixedMcpToolName } from "@opengeni/contracts";
|
|
9538
|
-
var ALIASED_MCP_TOOL_LEAF_LENGTH = 34;
|
|
9539
|
-
var ALIASED_MCP_TOOL_NAME = /^mcp_[0-9a-f]{24}__/;
|
|
9540
|
-
function prefixedMcpToolName(registryId, toolName) {
|
|
9541
|
-
const name = plainPrefixedMcpToolName(registryId, toolName);
|
|
9542
|
-
if (name.length <= 64 && /^[A-Za-z0-9_]+$/.test(name)) return name;
|
|
9543
|
-
const digest2 = legacyMcpToolDigest(registryId, toolName).slice(0, 24);
|
|
9544
|
-
const leaf = toolName.replace(/[^A-Za-z0-9_]/g, "_").slice(0, ALIASED_MCP_TOOL_LEAF_LENGTH) || "tool";
|
|
9545
|
-
return `mcp_${digest2}__${leaf}`;
|
|
9546
|
-
}
|
|
9547
|
-
function legacyMcpToolDigest(registryId, toolName) {
|
|
9548
|
-
return createHash4("sha256").update(JSON.stringify([registryId, toolName])).digest("hex");
|
|
9549
|
-
}
|
|
9550
|
-
function isAliasedMcpToolName(name) {
|
|
9551
|
-
return ALIASED_MCP_TOOL_NAME.test(name);
|
|
9552
|
-
}
|
|
9553
|
-
|
|
9554
9535
|
// src/provider-history-adapter.ts
|
|
9555
9536
|
import { collapseRepeatedSessionGoalContexts as collapseRepeatedSessionGoalContexts2 } from "@opengeni/contracts";
|
|
9556
9537
|
|
|
@@ -9719,209 +9700,6 @@ function joinChatReasoningMessages(messages) {
|
|
|
9719
9700
|
return result.length === messages.length ? messages : result;
|
|
9720
9701
|
}
|
|
9721
9702
|
|
|
9722
|
-
// src/provider-tool-names.ts
|
|
9723
|
-
import { createHash as createHash5 } from "crypto";
|
|
9724
|
-
import { SECRET_VALUE_MASK_MIN_CHARS } from "@opengeni/contracts";
|
|
9725
|
-
var PROVIDER_TOOL_NAME = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
9726
|
-
var VALID_NAME_PART = /^[a-zA-Z0-9_-]*$/;
|
|
9727
|
-
var DOTTED_NAME = /^[a-zA-Z0-9_.-]+$/;
|
|
9728
|
-
var TOOL_NAME_SEPARATOR = "__";
|
|
9729
|
-
var ALIAS_READABLE_LENGTH = 40;
|
|
9730
|
-
var ALIAS_DIGEST_LENGTH = 16;
|
|
9731
|
-
var TOOL_DISCLOSURE_KEY_PREFIX = "opengeni.tool_disclosure";
|
|
9732
|
-
var MASKING_LABEL = /\{\{secret:[^{}]*\}\}|\[opengeni credential:[^\]]*\]|\[masked [^\]]*\]|\[redacted:[^\]]*\]|\[secret\]/g;
|
|
9733
|
-
function isProviderToolName(name) {
|
|
9734
|
-
return PROVIDER_TOOL_NAME.test(name);
|
|
9735
|
-
}
|
|
9736
|
-
function providerToolNameAlias(name) {
|
|
9737
|
-
const readable = name.replace(/[^a-zA-Z0-9_-]+/g, "_").slice(0, ALIAS_READABLE_LENGTH);
|
|
9738
|
-
const digest2 = createHash5("sha256").update(name).digest("hex").slice(0, ALIAS_DIGEST_LENGTH);
|
|
9739
|
-
return `${readable}_${digest2}`;
|
|
9740
|
-
}
|
|
9741
|
-
function escapeRegExp(text2) {
|
|
9742
|
-
return text2.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
9743
|
-
}
|
|
9744
|
-
function uniqueMatch(candidates, matches) {
|
|
9745
|
-
let match = null;
|
|
9746
|
-
for (const candidate of candidates) {
|
|
9747
|
-
if (!matches(candidate)) continue;
|
|
9748
|
-
if (match !== null && match !== candidate) return null;
|
|
9749
|
-
match = candidate;
|
|
9750
|
-
}
|
|
9751
|
-
return match;
|
|
9752
|
-
}
|
|
9753
|
-
function knownToolNameFor(name, known) {
|
|
9754
|
-
const valid = known.filter(isProviderToolName);
|
|
9755
|
-
if (name.includes(".") && DOTTED_NAME.test(name)) {
|
|
9756
|
-
const dotted = name.replaceAll(".", TOOL_NAME_SEPARATOR);
|
|
9757
|
-
if (valid.includes(dotted)) return dotted;
|
|
9758
|
-
const published = name.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
9759
|
-
return uniqueMatch(valid, (candidate) => {
|
|
9760
|
-
const separator = candidate.lastIndexOf(TOOL_NAME_SEPARATOR);
|
|
9761
|
-
if (separator < 0) return false;
|
|
9762
|
-
const leaf = candidate.slice(separator + TOOL_NAME_SEPARATOR.length);
|
|
9763
|
-
return leaf === published || isAliasedMcpToolName(candidate) && leaf === published.slice(0, ALIASED_MCP_TOOL_LEAF_LENGTH);
|
|
9764
|
-
});
|
|
9765
|
-
}
|
|
9766
|
-
if (!name.match(MASKING_LABEL)) return null;
|
|
9767
|
-
const parts = name.split(MASKING_LABEL);
|
|
9768
|
-
if (!parts.every((part) => VALID_NAME_PART.test(part))) return null;
|
|
9769
|
-
if (parts.every((part) => part.length === 0)) return null;
|
|
9770
|
-
const pattern = new RegExp(
|
|
9771
|
-
`^${parts.map(escapeRegExp).join(`[a-zA-Z0-9_-]{${SECRET_VALUE_MASK_MIN_CHARS},}`)}$`
|
|
9772
|
-
);
|
|
9773
|
-
return uniqueMatch(valid, (candidate) => pattern.test(candidate));
|
|
9774
|
-
}
|
|
9775
|
-
function requestFunctionToolNames(tools) {
|
|
9776
|
-
const names = [];
|
|
9777
|
-
for (const tool2 of tools ?? []) {
|
|
9778
|
-
if (isRecord6(tool2) && tool2.type === "function" && typeof tool2.name === "string") {
|
|
9779
|
-
names.push(tool2.name);
|
|
9780
|
-
}
|
|
9781
|
-
}
|
|
9782
|
-
return names;
|
|
9783
|
-
}
|
|
9784
|
-
function isRecord6(value) {
|
|
9785
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
9786
|
-
}
|
|
9787
|
-
var NAMED_CALL_TYPES = /* @__PURE__ */ new Set([
|
|
9788
|
-
"function_call",
|
|
9789
|
-
"function_call_result",
|
|
9790
|
-
"custom_tool_call"
|
|
9791
|
-
]);
|
|
9792
|
-
function hasNamespace(record4) {
|
|
9793
|
-
return typeof record4.namespace === "string" && record4.namespace.length > 0;
|
|
9794
|
-
}
|
|
9795
|
-
function rewriteNamed(record4, visit, scoped) {
|
|
9796
|
-
if (!isRecord6(record4)) return record4;
|
|
9797
|
-
const isNamespace = record4.type === "namespace";
|
|
9798
|
-
const nameScoped = scoped || isNamespace || hasNamespace(record4);
|
|
9799
|
-
let next = null;
|
|
9800
|
-
if (typeof record4.name === "string") {
|
|
9801
|
-
const definition = record4.type === "function" ? record4 : void 0;
|
|
9802
|
-
const valid = visit(record4.name, nameScoped ? "scoped" : "flat", definition);
|
|
9803
|
-
if (valid !== record4.name) next = { ...record4, name: valid };
|
|
9804
|
-
}
|
|
9805
|
-
if (typeof record4.namespace === "string") {
|
|
9806
|
-
const valid = visit(record4.namespace, "scoped");
|
|
9807
|
-
if (valid !== record4.namespace) next = { ...next ?? record4, namespace: valid };
|
|
9808
|
-
}
|
|
9809
|
-
if (Array.isArray(record4.tools)) {
|
|
9810
|
-
const tools = rewriteDefinitions(record4.tools, visit, scoped || isNamespace);
|
|
9811
|
-
if (tools !== record4.tools) next = { ...next ?? record4, tools };
|
|
9812
|
-
}
|
|
9813
|
-
return next ?? record4;
|
|
9814
|
-
}
|
|
9815
|
-
function rewriteDefinitions(tools, visit, scoped) {
|
|
9816
|
-
let changed = false;
|
|
9817
|
-
const rewritten = tools.map((tool2) => {
|
|
9818
|
-
const next = rewriteNamed(tool2, visit, scoped);
|
|
9819
|
-
if (next !== tool2) changed = true;
|
|
9820
|
-
return next;
|
|
9821
|
-
});
|
|
9822
|
-
if (!changed) return tools;
|
|
9823
|
-
const seen = /* @__PURE__ */ new Set();
|
|
9824
|
-
return rewritten.filter((tool2) => {
|
|
9825
|
-
if (!isRecord6(tool2) || typeof tool2.name !== "string") return true;
|
|
9826
|
-
if (seen.has(tool2.name)) return false;
|
|
9827
|
-
seen.add(tool2.name);
|
|
9828
|
-
return true;
|
|
9829
|
-
});
|
|
9830
|
-
}
|
|
9831
|
-
function withRewrittenTools(record4, visit) {
|
|
9832
|
-
if (!Array.isArray(record4.tools)) return record4;
|
|
9833
|
-
const tools = rewriteDefinitions(record4.tools, visit, false);
|
|
9834
|
-
return tools === record4.tools ? record4 : { ...record4, tools };
|
|
9835
|
-
}
|
|
9836
|
-
function declaresTools(providerData) {
|
|
9837
|
-
return providerData.type === "additional_tools" || providerData.type === "tool_search_output";
|
|
9838
|
-
}
|
|
9839
|
-
function rewriteItem(item, visit) {
|
|
9840
|
-
if (!isRecord6(item)) return item;
|
|
9841
|
-
const type = typeof item.type === "string" ? item.type : "";
|
|
9842
|
-
if (NAMED_CALL_TYPES.has(type)) return rewriteNamed(item, visit, false);
|
|
9843
|
-
let next = type === "tool_search_output" ? withRewrittenTools(item, visit) : item;
|
|
9844
|
-
if (isRecord6(next.providerData)) {
|
|
9845
|
-
const providerData = next.providerData;
|
|
9846
|
-
let data = declaresTools(providerData) ? withRewrittenTools(providerData, visit) : providerData;
|
|
9847
|
-
for (const [key, value] of Object.entries(providerData)) {
|
|
9848
|
-
if (!key.startsWith(TOOL_DISCLOSURE_KEY_PREFIX) || !isRecord6(value)) continue;
|
|
9849
|
-
const rewritten = withRewrittenTools(value, visit);
|
|
9850
|
-
if (rewritten !== value) data = { ...data, [key]: rewritten };
|
|
9851
|
-
}
|
|
9852
|
-
if (data !== providerData) next = { ...next, providerData: data };
|
|
9853
|
-
}
|
|
9854
|
-
return next;
|
|
9855
|
-
}
|
|
9856
|
-
function invalidHistoryToolNames(items) {
|
|
9857
|
-
const names = /* @__PURE__ */ new Set();
|
|
9858
|
-
const collect = (name) => {
|
|
9859
|
-
if (!isProviderToolName(name)) names.add(name);
|
|
9860
|
-
return name;
|
|
9861
|
-
};
|
|
9862
|
-
for (const item of items) rewriteItem(item, collect);
|
|
9863
|
-
return [...names];
|
|
9864
|
-
}
|
|
9865
|
-
function stableJson(value) {
|
|
9866
|
-
if (Array.isArray(value)) return `[${value.map(stableJson).join(",")}]`;
|
|
9867
|
-
if (isRecord6(value)) {
|
|
9868
|
-
return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${stableJson(value[key])}`).join(",")}}`;
|
|
9869
|
-
}
|
|
9870
|
-
return JSON.stringify(value) ?? "null";
|
|
9871
|
-
}
|
|
9872
|
-
var rewrittenItems = /* @__PURE__ */ new WeakMap();
|
|
9873
|
-
function withProviderValidToolNames(items, catalog = { names: [] }) {
|
|
9874
|
-
const known = new Set(catalog.names);
|
|
9875
|
-
const flat = /* @__PURE__ */ new Set();
|
|
9876
|
-
const scoped = /* @__PURE__ */ new Set();
|
|
9877
|
-
const storedParameters = /* @__PURE__ */ new Map();
|
|
9878
|
-
const collect = (name, position, definition) => {
|
|
9879
|
-
if (isProviderToolName(name) || known.has(name)) return name;
|
|
9880
|
-
(position === "flat" ? flat : scoped).add(name);
|
|
9881
|
-
if (position === "flat" && isRecord6(definition) && definition.parameters !== void 0) {
|
|
9882
|
-
const stored = storedParameters.get(name) ?? /* @__PURE__ */ new Set();
|
|
9883
|
-
stored.add(stableJson(definition.parameters));
|
|
9884
|
-
storedParameters.set(name, stored);
|
|
9885
|
-
}
|
|
9886
|
-
return name;
|
|
9887
|
-
};
|
|
9888
|
-
for (const item of items) rewriteItem(item, collect);
|
|
9889
|
-
if (flat.size === 0 && scoped.size === 0) return items;
|
|
9890
|
-
const aliasUnbound = catalog.aliasUnbound !== false;
|
|
9891
|
-
const flatNames = /* @__PURE__ */ new Map();
|
|
9892
|
-
for (const name of flat) {
|
|
9893
|
-
let denoted = catalog.denote ? knownToolNameFor(name, catalog.names) : null;
|
|
9894
|
-
const stored = storedParameters.get(name);
|
|
9895
|
-
if (denoted !== null && stored && catalog.parametersOf) {
|
|
9896
|
-
const parameters = catalog.parametersOf(denoted);
|
|
9897
|
-
if (parameters !== void 0 && [...stored].some((json) => json !== stableJson(parameters)))
|
|
9898
|
-
denoted = null;
|
|
9899
|
-
}
|
|
9900
|
-
flatNames.set(name, denoted ?? (aliasUnbound ? providerToolNameAlias(name) : name));
|
|
9901
|
-
}
|
|
9902
|
-
const rewrite = (name, position) => {
|
|
9903
|
-
if (isProviderToolName(name) || known.has(name)) return name;
|
|
9904
|
-
if (position === "flat") return flatNames.get(name) ?? providerToolNameAlias(name);
|
|
9905
|
-
return aliasUnbound ? providerToolNameAlias(name) : name;
|
|
9906
|
-
};
|
|
9907
|
-
const mapping = JSON.stringify([[...flatNames], [...scoped], aliasUnbound]);
|
|
9908
|
-
let changed = false;
|
|
9909
|
-
const next = items.map((item) => {
|
|
9910
|
-
if (item === null || typeof item !== "object") return item;
|
|
9911
|
-
const cached = rewrittenItems.get(item);
|
|
9912
|
-
let rewritten;
|
|
9913
|
-
if (cached?.mapping === mapping) {
|
|
9914
|
-
rewritten = cached.item;
|
|
9915
|
-
} else {
|
|
9916
|
-
rewritten = rewriteItem(item, rewrite);
|
|
9917
|
-
if (rewritten !== item) rewrittenItems.set(item, { mapping, item: rewritten });
|
|
9918
|
-
}
|
|
9919
|
-
if (rewritten !== item) changed = true;
|
|
9920
|
-
return rewritten;
|
|
9921
|
-
});
|
|
9922
|
-
return changed ? next : items;
|
|
9923
|
-
}
|
|
9924
|
-
|
|
9925
9703
|
// src/provider-history-adapter.ts
|
|
9926
9704
|
var CHAT_FUNCTION_NAME = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
9927
9705
|
var HISTORICAL_FACT_MAX_CHARS = 32e3;
|
|
@@ -10083,9 +9861,7 @@ function projectHistoryForProvider(items, providerApi, options = {}) {
|
|
|
10083
9861
|
})
|
|
10084
9862
|
);
|
|
10085
9863
|
const input = projected.every((item, index) => item === items[index]) ? items : projected;
|
|
10086
|
-
|
|
10087
|
-
const named = providerApi === "chat" ? wireValid : withProviderValidToolNames(wireValid, { names: options.knownToolNames ?? [] });
|
|
10088
|
-
return projectWireHistory(named, providerApi, options);
|
|
9864
|
+
return projectWireHistory(withWireValidFunctionCallArguments(input), providerApi, options);
|
|
10089
9865
|
}
|
|
10090
9866
|
function withoutClaudeCitationMetadata(item) {
|
|
10091
9867
|
if (item.type !== "message" || item.role !== "assistant" || !Array.isArray(item.content))
|
|
@@ -10151,143 +9927,1038 @@ function projectWireHistory(items, providerApi, options) {
|
|
|
10151
9927
|
});
|
|
10152
9928
|
return changed2 ? projected2 : items;
|
|
10153
9929
|
}
|
|
10154
|
-
const incompatibleCallIds = /* @__PURE__ */ new Set();
|
|
10155
|
-
for (const item of items) {
|
|
10156
|
-
if (item.type === "compaction") {
|
|
10157
|
-
throw new ProviderHistoryIncompatibleError(providerApi, "compaction");
|
|
10158
|
-
}
|
|
10159
|
-
if (isChatIncompatibleCall(item)) {
|
|
10160
|
-
const id = callId(item);
|
|
10161
|
-
if (id) incompatibleCallIds.add(id);
|
|
10162
|
-
}
|
|
9930
|
+
const incompatibleCallIds = /* @__PURE__ */ new Set();
|
|
9931
|
+
for (const item of items) {
|
|
9932
|
+
if (item.type === "compaction") {
|
|
9933
|
+
throw new ProviderHistoryIncompatibleError(providerApi, "compaction");
|
|
9934
|
+
}
|
|
9935
|
+
if (isChatIncompatibleCall(item)) {
|
|
9936
|
+
const id = callId(item);
|
|
9937
|
+
if (id) incompatibleCallIds.add(id);
|
|
9938
|
+
}
|
|
9939
|
+
}
|
|
9940
|
+
let changed = false;
|
|
9941
|
+
const projected = items.map((item) => {
|
|
9942
|
+
if (disclosedTools(item)) {
|
|
9943
|
+
changed = true;
|
|
9944
|
+
return options.hoistsSystemMessages ? { type: "message", role: "user", content: `${MACHINE_ORIGIN_PREFIX}
|
|
9945
|
+
${item.content}` } : { type: "message", role: "system", content: item.content };
|
|
9946
|
+
}
|
|
9947
|
+
const reasoning = foreignReasoningFact(item, providerApi);
|
|
9948
|
+
if (reasoning) {
|
|
9949
|
+
changed = true;
|
|
9950
|
+
return reasoning;
|
|
9951
|
+
}
|
|
9952
|
+
if (item.type === "message" && (item.role === "developer" || item.role === "system")) {
|
|
9953
|
+
const content = chatSystemContent(item.content);
|
|
9954
|
+
if (item.role === "developer" || content !== item.content) {
|
|
9955
|
+
changed = true;
|
|
9956
|
+
return { ...item, role: "system", content };
|
|
9957
|
+
}
|
|
9958
|
+
}
|
|
9959
|
+
const resultId = item.type === "function_call_result" ? callId(item) : null;
|
|
9960
|
+
if (CHAT_INCOMPATIBLE_ITEM_TYPES.has(String(item.type ?? "")) || isChatIncompatibleHostedToolCall(item) || isChatIncompatibleCall(item) || resultId !== null && incompatibleCallIds.has(resultId)) {
|
|
9961
|
+
changed = true;
|
|
9962
|
+
return historicalFact(item);
|
|
9963
|
+
}
|
|
9964
|
+
return item;
|
|
9965
|
+
});
|
|
9966
|
+
return changed ? projected : items;
|
|
9967
|
+
}
|
|
9968
|
+
|
|
9969
|
+
// src/mcp-model-tool-name.ts
|
|
9970
|
+
import { createHash as createHash4 } from "crypto";
|
|
9971
|
+
import { prefixedMcpToolName as plainPrefixedMcpToolName } from "@opengeni/contracts";
|
|
9972
|
+
var ALIASED_MCP_TOOL_LEAF_LENGTH = 34;
|
|
9973
|
+
var ALIASED_MCP_TOOL_NAME = /^mcp_[0-9a-f]{24}__/;
|
|
9974
|
+
function prefixedMcpToolName(registryId, toolName) {
|
|
9975
|
+
const name = plainPrefixedMcpToolName(registryId, toolName);
|
|
9976
|
+
if (name.length <= 64 && /^[A-Za-z0-9_]+$/.test(name)) return name;
|
|
9977
|
+
const digest2 = legacyMcpToolDigest(registryId, toolName).slice(0, 24);
|
|
9978
|
+
const leaf = toolName.replace(/[^A-Za-z0-9_]/g, "_").slice(0, ALIASED_MCP_TOOL_LEAF_LENGTH) || "tool";
|
|
9979
|
+
return `mcp_${digest2}__${leaf}`;
|
|
9980
|
+
}
|
|
9981
|
+
function legacyMcpToolDigest(registryId, toolName) {
|
|
9982
|
+
return createHash4("sha256").update(JSON.stringify([registryId, toolName])).digest("hex");
|
|
9983
|
+
}
|
|
9984
|
+
function isAliasedMcpToolName(name) {
|
|
9985
|
+
return ALIASED_MCP_TOOL_NAME.test(name);
|
|
9986
|
+
}
|
|
9987
|
+
|
|
9988
|
+
// src/system-prompt-cache-prefix.ts
|
|
9989
|
+
var MAX_RECORDED_PREFIXES = 256;
|
|
9990
|
+
var MIN_PREFIX_CHARS = 2048;
|
|
9991
|
+
var recorded = /* @__PURE__ */ new Map();
|
|
9992
|
+
function recordStableSystemPromptPrefix(prefix) {
|
|
9993
|
+
if (!prefix || prefix.length < MIN_PREFIX_CHARS) return;
|
|
9994
|
+
recorded.delete(prefix);
|
|
9995
|
+
recorded.set(prefix, true);
|
|
9996
|
+
while (recorded.size > MAX_RECORDED_PREFIXES) {
|
|
9997
|
+
recorded.delete(recorded.keys().next().value);
|
|
9998
|
+
}
|
|
9999
|
+
}
|
|
10000
|
+
function splitStableSystemPromptPrefix(instructions) {
|
|
10001
|
+
let end = 0;
|
|
10002
|
+
let matched;
|
|
10003
|
+
for (const prefix of recorded.keys()) {
|
|
10004
|
+
const at = instructions.indexOf(prefix);
|
|
10005
|
+
if (at >= 0 && at + prefix.length > end) {
|
|
10006
|
+
end = at + prefix.length;
|
|
10007
|
+
matched = prefix;
|
|
10008
|
+
}
|
|
10009
|
+
}
|
|
10010
|
+
if (!matched || !instructions.slice(end).trim()) return void 0;
|
|
10011
|
+
recorded.delete(matched);
|
|
10012
|
+
recorded.set(matched, true);
|
|
10013
|
+
return [instructions.slice(0, end), instructions.slice(end)];
|
|
10014
|
+
}
|
|
10015
|
+
|
|
10016
|
+
// src/anthropic-messages.ts
|
|
10017
|
+
import { createHash as createHash7, randomUUID as randomUUID3 } from "crypto";
|
|
10018
|
+
|
|
10019
|
+
// src/claude-code-identity.ts
|
|
10020
|
+
import { createHash as createHash5, randomUUID as randomUUID2 } from "crypto";
|
|
10021
|
+
var CLAUDE_CODE_HEADERS = {
|
|
10022
|
+
"user-agent": "claude-cli/2.1.285 (external, sdk-ts, agent-sdk/0.3.276)",
|
|
10023
|
+
"x-app": "cli",
|
|
10024
|
+
"x-stainless-lang": "js",
|
|
10025
|
+
"x-stainless-package-version": "0.127.0",
|
|
10026
|
+
"x-stainless-os": "MacOS",
|
|
10027
|
+
"x-stainless-arch": "arm64",
|
|
10028
|
+
"x-stainless-runtime": "node",
|
|
10029
|
+
"x-stainless-runtime-version": "v26.3.0",
|
|
10030
|
+
"x-stainless-retry-count": "0",
|
|
10031
|
+
"x-stainless-timeout": "600",
|
|
10032
|
+
"anthropic-dangerous-direct-browser-access": "true",
|
|
10033
|
+
"anthropic-dispatch-id": "v2d"
|
|
10034
|
+
};
|
|
10035
|
+
function claudeCodeFingerprint(input) {
|
|
10036
|
+
const message = typeof input === "string" ? input : input.find(
|
|
10037
|
+
(item) => (item.type === void 0 || item.type === "message") && "role" in item && item.role === "user"
|
|
10038
|
+
);
|
|
10039
|
+
const content = typeof message === "string" ? message : message && "content" in message ? message.content : "";
|
|
10040
|
+
const first = typeof content === "string" ? content : Array.isArray(content) ? content.find((block) => block.type === "input_text" || String(block.type) === "text") : void 0;
|
|
10041
|
+
const prompt = typeof first === "string" ? first : first && "text" in first ? String(first.text) : "";
|
|
10042
|
+
const sample = [4, 7, 20].map((index) => prompt[index] || "0").join("");
|
|
10043
|
+
return createHash5("sha256").update(`59cf53e54c78${sample}2.1.285`).digest("hex").slice(0, 3);
|
|
10044
|
+
}
|
|
10045
|
+
function applyClaudeCodeIdentity(body, headers, url, request, account, identity) {
|
|
10046
|
+
url.searchParams.set("beta", "true");
|
|
10047
|
+
for (const [name, value] of Object.entries(CLAUDE_CODE_HEADERS)) headers.set(name, value);
|
|
10048
|
+
headers.set("accept", "application/json");
|
|
10049
|
+
headers.set("x-claude-code-session-id", identity.sessionId);
|
|
10050
|
+
headers.set("x-claude-code-prompt-id", identity.promptId);
|
|
10051
|
+
headers.set("x-client-request-id", randomUUID2());
|
|
10052
|
+
body.metadata = {
|
|
10053
|
+
user_id: JSON.stringify({
|
|
10054
|
+
device_id: account.deviceId,
|
|
10055
|
+
account_uuid: account.accountUuid,
|
|
10056
|
+
session_id: identity.sessionId
|
|
10057
|
+
})
|
|
10058
|
+
};
|
|
10059
|
+
const betas = new Set((headers.get("anthropic-beta") ?? "").split(",").filter(Boolean));
|
|
10060
|
+
betas.add("claude-code-20250219");
|
|
10061
|
+
betas.add("oauth-2025-04-20");
|
|
10062
|
+
if (body.thinking) {
|
|
10063
|
+
betas.add("interleaved-thinking-2025-05-14");
|
|
10064
|
+
betas.add("thinking-token-count-2026-05-13");
|
|
10065
|
+
betas.add("effort-2025-11-24");
|
|
10066
|
+
betas.add("thinking-display-updates-2026-08-18");
|
|
10067
|
+
}
|
|
10068
|
+
if ([
|
|
10069
|
+
...body.system ?? [],
|
|
10070
|
+
...body.tools ?? [],
|
|
10071
|
+
...body.messages.flatMap((m) => m.content)
|
|
10072
|
+
].some((block) => block.cache_control?.ttl === "1h"))
|
|
10073
|
+
betas.add("extended-cache-ttl-2025-04-11");
|
|
10074
|
+
headers.set("anthropic-beta", [...betas].join(","));
|
|
10075
|
+
const previous = identity.previousRequestId && /^req_[A-Za-z0-9_-]{1,36}$/.test(identity.previousRequestId) ? ` cc_prev_req=${identity.previousRequestId};` : "";
|
|
10076
|
+
const billing = `x-anthropic-billing-header: cc_version=2.1.285.${claudeCodeFingerprint(request.input)}; cc_entrypoint=sdk-ts;${previous} cc_prompt_id=${identity.promptId}; cc_turn_origin=sdk;`;
|
|
10077
|
+
body.system = [{ type: "text", text: billing }, ...body.system ?? []];
|
|
10078
|
+
}
|
|
10079
|
+
|
|
10080
|
+
// src/anthropic-messages.ts
|
|
10081
|
+
import { claudeNativeModelProfile } from "@opengeni/config";
|
|
10082
|
+
import {
|
|
10083
|
+
hasClaudeCiteTag,
|
|
10084
|
+
rewriteClaudeCiteTags
|
|
10085
|
+
} from "@opengeni/contracts/claude-cite-tags";
|
|
10086
|
+
|
|
10087
|
+
// src/provider-invalid-request.ts
|
|
10088
|
+
var MAX_BODY_CHARACTERS = 64 * 1024;
|
|
10089
|
+
var MAX_PATH_CHARACTERS = 96;
|
|
10090
|
+
var PATH_CHARACTERS = /^[A-Za-z_][A-Za-z0-9_.[\]*]*$/;
|
|
10091
|
+
var CODE = /^[A-Za-z][A-Za-z0-9_.-]{0,63}$/;
|
|
10092
|
+
var EMBEDDED_PATH = /\b((?:input|messages|tools|tool_choice|contents|system|instructions)(?:\[\d+\]|\.\d+|\.[A-Za-z_]\w*)+)/;
|
|
10093
|
+
function record2(value) {
|
|
10094
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
10095
|
+
}
|
|
10096
|
+
function normalizedPath(raw) {
|
|
10097
|
+
const path = raw.trim().replace(/\[\d+\]/g, "[]").replace(/\.\d+(?=\.|\[|$)/g, "[]").replace(/\.properties\.[A-Za-z0-9_-]+/g, ".properties.*");
|
|
10098
|
+
return path.length <= MAX_PATH_CHARACTERS && PATH_CHARACTERS.test(path) ? path : null;
|
|
10099
|
+
}
|
|
10100
|
+
function errorCode(error) {
|
|
10101
|
+
for (const candidate of [error.code, error.type]) {
|
|
10102
|
+
if (typeof candidate === "string" && CODE.test(candidate)) return candidate;
|
|
10103
|
+
}
|
|
10104
|
+
return "unknown";
|
|
10105
|
+
}
|
|
10106
|
+
function errorRecord(body) {
|
|
10107
|
+
const envelope = record2(body);
|
|
10108
|
+
return record2(envelope?.error) ?? envelope ?? {};
|
|
10109
|
+
}
|
|
10110
|
+
function invalidRequestMessage(body) {
|
|
10111
|
+
const message = errorRecord(body).message;
|
|
10112
|
+
return typeof message === "string" ? message : "";
|
|
10113
|
+
}
|
|
10114
|
+
function invalidRequestRawPath(body) {
|
|
10115
|
+
const error = errorRecord(body);
|
|
10116
|
+
const message = typeof error.message === "string" ? error.message : "";
|
|
10117
|
+
return typeof error.param === "string" && error.param || EMBEDDED_PATH.exec(message)?.[1] || null;
|
|
10118
|
+
}
|
|
10119
|
+
function invalidRequestSignature(body) {
|
|
10120
|
+
const raw = invalidRequestRawPath(body);
|
|
10121
|
+
return {
|
|
10122
|
+
path: raw && normalizedPath(raw) || "unknown",
|
|
10123
|
+
code: errorCode(errorRecord(body))
|
|
10124
|
+
};
|
|
10125
|
+
}
|
|
10126
|
+
function parseInvalidRequestText(text2) {
|
|
10127
|
+
if (text2.length > MAX_BODY_CHARACTERS) return {};
|
|
10128
|
+
try {
|
|
10129
|
+
return JSON.parse(text2);
|
|
10130
|
+
} catch {
|
|
10131
|
+
return { message: text2 };
|
|
10132
|
+
}
|
|
10133
|
+
}
|
|
10134
|
+
var ERROR_BODY_READ_TIMEOUT_MS = 5e3;
|
|
10135
|
+
async function boundedCloneText(response) {
|
|
10136
|
+
const reader = response.clone().body?.getReader();
|
|
10137
|
+
if (!reader) return "";
|
|
10138
|
+
const chunks = [];
|
|
10139
|
+
let bytes = 0;
|
|
10140
|
+
let timer;
|
|
10141
|
+
const deadline = new Promise((resolve) => {
|
|
10142
|
+
timer = setTimeout(() => resolve(null), ERROR_BODY_READ_TIMEOUT_MS);
|
|
10143
|
+
});
|
|
10144
|
+
const read = (async () => {
|
|
10145
|
+
for (; ; ) {
|
|
10146
|
+
const next = await reader.read();
|
|
10147
|
+
if (next.done) break;
|
|
10148
|
+
bytes += next.value.byteLength;
|
|
10149
|
+
if (bytes > MAX_BODY_CHARACTERS) return null;
|
|
10150
|
+
chunks.push(next.value);
|
|
10151
|
+
}
|
|
10152
|
+
const joined = new Uint8Array(bytes);
|
|
10153
|
+
let offset = 0;
|
|
10154
|
+
for (const chunk of chunks) {
|
|
10155
|
+
joined.set(chunk, offset);
|
|
10156
|
+
offset += chunk.byteLength;
|
|
10157
|
+
}
|
|
10158
|
+
return new TextDecoder().decode(joined);
|
|
10159
|
+
})().catch(() => null);
|
|
10160
|
+
try {
|
|
10161
|
+
return await Promise.race([read, deadline]);
|
|
10162
|
+
} finally {
|
|
10163
|
+
clearTimeout(timer);
|
|
10164
|
+
void reader.cancel().catch(() => void 0);
|
|
10165
|
+
}
|
|
10166
|
+
}
|
|
10167
|
+
async function invalidRequestBodyOfResponse(response) {
|
|
10168
|
+
try {
|
|
10169
|
+
const text2 = await boundedCloneText(response);
|
|
10170
|
+
return text2 === null ? null : parseInvalidRequestText(text2);
|
|
10171
|
+
} catch {
|
|
10172
|
+
return null;
|
|
10173
|
+
}
|
|
10174
|
+
}
|
|
10175
|
+
async function invalidRequestSignatureOfResponse(response) {
|
|
10176
|
+
const body = await invalidRequestBodyOfResponse(response);
|
|
10177
|
+
return body === null ? null : invalidRequestSignature(body);
|
|
10178
|
+
}
|
|
10179
|
+
|
|
10180
|
+
// src/provider-tool-names.ts
|
|
10181
|
+
import { createHash as createHash6 } from "crypto";
|
|
10182
|
+
import { SECRET_VALUE_MASK_MIN_CHARS } from "@opengeni/contracts";
|
|
10183
|
+
var PROVIDER_TOOL_NAME = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
10184
|
+
var VALID_NAME_PART = /^[a-zA-Z0-9_-]*$/;
|
|
10185
|
+
var DOTTED_NAME = /^[a-zA-Z0-9_.-]+$/;
|
|
10186
|
+
var TOOL_NAME_SEPARATOR = "__";
|
|
10187
|
+
var ALIAS_READABLE_LENGTH = 40;
|
|
10188
|
+
var ALIAS_DIGEST_LENGTH = 16;
|
|
10189
|
+
var TOOL_DISCLOSURE_KEY_PREFIX = "opengeni.tool_disclosure";
|
|
10190
|
+
var MASKING_LABEL = /\{\{secret:[^{}]*\}\}|\[opengeni credential:[^\]]*\]|\[masked [^\]]*\]|\[redacted:[^\]]*\]|\[secret\]/g;
|
|
10191
|
+
function isProviderToolName(name) {
|
|
10192
|
+
return PROVIDER_TOOL_NAME.test(name);
|
|
10193
|
+
}
|
|
10194
|
+
function providerToolNameAlias(name) {
|
|
10195
|
+
const replaced = name.replace(/[^a-zA-Z0-9_]+/g, "_");
|
|
10196
|
+
const readable = (/^[a-zA-Z]/.test(replaced) ? replaced : `t${replaced}`).slice(
|
|
10197
|
+
0,
|
|
10198
|
+
ALIAS_READABLE_LENGTH
|
|
10199
|
+
);
|
|
10200
|
+
const digest2 = createHash6("sha256").update(name).digest("hex").slice(0, ALIAS_DIGEST_LENGTH);
|
|
10201
|
+
return `${readable}_${digest2}`;
|
|
10202
|
+
}
|
|
10203
|
+
function escapeRegExp(text2) {
|
|
10204
|
+
return text2.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
10205
|
+
}
|
|
10206
|
+
function uniqueMatch(candidates, matches) {
|
|
10207
|
+
let match = null;
|
|
10208
|
+
for (const candidate of candidates) {
|
|
10209
|
+
if (!matches(candidate)) continue;
|
|
10210
|
+
if (match !== null && match !== candidate) return null;
|
|
10211
|
+
match = candidate;
|
|
10212
|
+
}
|
|
10213
|
+
return match;
|
|
10214
|
+
}
|
|
10215
|
+
function knownToolNameFor(name, known) {
|
|
10216
|
+
const valid = known.filter(isProviderToolName);
|
|
10217
|
+
if (name.includes(".") && DOTTED_NAME.test(name)) {
|
|
10218
|
+
const dotted = name.replaceAll(".", TOOL_NAME_SEPARATOR);
|
|
10219
|
+
if (valid.includes(dotted)) return dotted;
|
|
10220
|
+
const published = name.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
10221
|
+
return uniqueMatch(valid, (candidate) => {
|
|
10222
|
+
const separator = candidate.lastIndexOf(TOOL_NAME_SEPARATOR);
|
|
10223
|
+
if (separator < 0) return false;
|
|
10224
|
+
const leaf = candidate.slice(separator + TOOL_NAME_SEPARATOR.length);
|
|
10225
|
+
return leaf === published || isAliasedMcpToolName(candidate) && leaf === published.slice(0, ALIASED_MCP_TOOL_LEAF_LENGTH);
|
|
10226
|
+
});
|
|
10227
|
+
}
|
|
10228
|
+
if (!name.match(MASKING_LABEL)) return null;
|
|
10229
|
+
const parts = name.split(MASKING_LABEL);
|
|
10230
|
+
if (!parts.every((part) => VALID_NAME_PART.test(part))) return null;
|
|
10231
|
+
if (parts.every((part) => part.length === 0)) return null;
|
|
10232
|
+
const pattern = new RegExp(
|
|
10233
|
+
`^${parts.map(escapeRegExp).join(`[a-zA-Z0-9_-]{${SECRET_VALUE_MASK_MIN_CHARS},}`)}$`
|
|
10234
|
+
);
|
|
10235
|
+
return uniqueMatch(valid, (candidate) => pattern.test(candidate));
|
|
10236
|
+
}
|
|
10237
|
+
function isRecord6(value) {
|
|
10238
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10239
|
+
}
|
|
10240
|
+
var NAMED_CALL_TYPES = /* @__PURE__ */ new Set([
|
|
10241
|
+
"function_call",
|
|
10242
|
+
"function_call_result",
|
|
10243
|
+
"custom_tool_call"
|
|
10244
|
+
]);
|
|
10245
|
+
function hasNamespace(record4) {
|
|
10246
|
+
return typeof record4.namespace === "string" && record4.namespace.length > 0;
|
|
10247
|
+
}
|
|
10248
|
+
function rewriteNamed(record4, visit, scoped) {
|
|
10249
|
+
if (!isRecord6(record4)) return record4;
|
|
10250
|
+
const isNamespace = record4.type === "namespace";
|
|
10251
|
+
const nameScoped = scoped || isNamespace || hasNamespace(record4);
|
|
10252
|
+
let next = null;
|
|
10253
|
+
if (typeof record4.name === "string") {
|
|
10254
|
+
const definition = record4.type === "function" ? record4 : void 0;
|
|
10255
|
+
const valid = visit(record4.name, nameScoped ? "scoped" : "flat", definition);
|
|
10256
|
+
if (valid !== record4.name) next = { ...record4, name: valid };
|
|
10257
|
+
}
|
|
10258
|
+
if (typeof record4.namespace === "string") {
|
|
10259
|
+
const valid = visit(record4.namespace, "scoped");
|
|
10260
|
+
if (valid !== record4.namespace) next = { ...next ?? record4, namespace: valid };
|
|
10261
|
+
}
|
|
10262
|
+
if (Array.isArray(record4.tools)) {
|
|
10263
|
+
const tools = rewriteDefinitions(record4.tools, visit, scoped || isNamespace);
|
|
10264
|
+
if (tools !== record4.tools) next = { ...next ?? record4, tools };
|
|
10265
|
+
}
|
|
10266
|
+
return next ?? record4;
|
|
10267
|
+
}
|
|
10268
|
+
function rewriteDefinitions(tools, visit, scoped) {
|
|
10269
|
+
let changed = false;
|
|
10270
|
+
const rewritten = tools.map((tool2) => {
|
|
10271
|
+
const next = rewriteNamed(tool2, visit, scoped);
|
|
10272
|
+
if (next !== tool2) changed = true;
|
|
10273
|
+
return next;
|
|
10274
|
+
});
|
|
10275
|
+
if (!changed) return tools;
|
|
10276
|
+
const seen = /* @__PURE__ */ new Set();
|
|
10277
|
+
return rewritten.filter((tool2) => {
|
|
10278
|
+
if (!isRecord6(tool2) || typeof tool2.name !== "string") return true;
|
|
10279
|
+
if (seen.has(tool2.name)) return false;
|
|
10280
|
+
seen.add(tool2.name);
|
|
10281
|
+
return true;
|
|
10282
|
+
});
|
|
10283
|
+
}
|
|
10284
|
+
function withRewrittenTools(record4, visit) {
|
|
10285
|
+
if (!Array.isArray(record4.tools)) return record4;
|
|
10286
|
+
const tools = rewriteDefinitions(record4.tools, visit, false);
|
|
10287
|
+
return tools === record4.tools ? record4 : { ...record4, tools };
|
|
10288
|
+
}
|
|
10289
|
+
function declaresTools(providerData) {
|
|
10290
|
+
return providerData.type === "additional_tools" || providerData.type === "tool_search_output";
|
|
10291
|
+
}
|
|
10292
|
+
function rewriteItem(item, visit) {
|
|
10293
|
+
if (!isRecord6(item)) return item;
|
|
10294
|
+
const type = typeof item.type === "string" ? item.type : "";
|
|
10295
|
+
if (NAMED_CALL_TYPES.has(type)) return rewriteNamed(item, visit, false);
|
|
10296
|
+
let next = type === "tool_search_output" ? withRewrittenTools(item, visit) : item;
|
|
10297
|
+
if (isRecord6(next.providerData)) {
|
|
10298
|
+
const providerData = next.providerData;
|
|
10299
|
+
let data = declaresTools(providerData) ? withRewrittenTools(providerData, visit) : providerData;
|
|
10300
|
+
for (const [key, value] of Object.entries(providerData)) {
|
|
10301
|
+
if (!key.startsWith(TOOL_DISCLOSURE_KEY_PREFIX) || !isRecord6(value)) continue;
|
|
10302
|
+
const rewritten = withRewrittenTools(value, visit);
|
|
10303
|
+
if (rewritten !== value) data = { ...data, [key]: rewritten };
|
|
10304
|
+
}
|
|
10305
|
+
if (data !== providerData) next = { ...next, providerData: data };
|
|
10306
|
+
}
|
|
10307
|
+
return next;
|
|
10308
|
+
}
|
|
10309
|
+
function invalidHistoryToolNames(items) {
|
|
10310
|
+
const names = /* @__PURE__ */ new Set();
|
|
10311
|
+
const collect = (name) => {
|
|
10312
|
+
if (!isProviderToolName(name)) names.add(name);
|
|
10313
|
+
return name;
|
|
10314
|
+
};
|
|
10315
|
+
for (const item of items) rewriteItem(item, collect);
|
|
10316
|
+
return [...names];
|
|
10317
|
+
}
|
|
10318
|
+
function stableJson(value) {
|
|
10319
|
+
if (Array.isArray(value)) return `[${value.map(stableJson).join(",")}]`;
|
|
10320
|
+
if (isRecord6(value)) {
|
|
10321
|
+
return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${stableJson(value[key])}`).join(",")}}`;
|
|
10322
|
+
}
|
|
10323
|
+
return JSON.stringify(value) ?? "null";
|
|
10324
|
+
}
|
|
10325
|
+
var rewrittenItems = /* @__PURE__ */ new WeakMap();
|
|
10326
|
+
function withProviderValidToolNames(items, catalog = { names: [] }) {
|
|
10327
|
+
const known = new Set(catalog.names);
|
|
10328
|
+
const flat = /* @__PURE__ */ new Set();
|
|
10329
|
+
const scoped = /* @__PURE__ */ new Set();
|
|
10330
|
+
const storedParameters = /* @__PURE__ */ new Map();
|
|
10331
|
+
const collect = (name, position, definition) => {
|
|
10332
|
+
if (isProviderToolName(name) || known.has(name)) return name;
|
|
10333
|
+
(position === "flat" ? flat : scoped).add(name);
|
|
10334
|
+
if (position === "flat" && isRecord6(definition) && definition.parameters !== void 0) {
|
|
10335
|
+
const stored = storedParameters.get(name) ?? /* @__PURE__ */ new Set();
|
|
10336
|
+
stored.add(stableJson(definition.parameters));
|
|
10337
|
+
storedParameters.set(name, stored);
|
|
10338
|
+
}
|
|
10339
|
+
return name;
|
|
10340
|
+
};
|
|
10341
|
+
for (const item of items) rewriteItem(item, collect);
|
|
10342
|
+
if (flat.size === 0 && scoped.size === 0) return items;
|
|
10343
|
+
const aliasUnbound = catalog.aliasUnbound !== false;
|
|
10344
|
+
const flatNames = /* @__PURE__ */ new Map();
|
|
10345
|
+
for (const name of flat) {
|
|
10346
|
+
let denoted = catalog.denote ? knownToolNameFor(name, catalog.names) : null;
|
|
10347
|
+
const stored = storedParameters.get(name);
|
|
10348
|
+
if (denoted !== null && stored && catalog.parametersOf) {
|
|
10349
|
+
const parameters = catalog.parametersOf(denoted);
|
|
10350
|
+
if (parameters !== void 0 && [...stored].some((json) => json !== stableJson(parameters)))
|
|
10351
|
+
denoted = null;
|
|
10352
|
+
}
|
|
10353
|
+
flatNames.set(name, denoted ?? (aliasUnbound ? providerToolNameAlias(name) : name));
|
|
10354
|
+
}
|
|
10355
|
+
const rewrite = (name, position) => {
|
|
10356
|
+
if (isProviderToolName(name) || known.has(name)) return name;
|
|
10357
|
+
if (position === "flat") return flatNames.get(name) ?? providerToolNameAlias(name);
|
|
10358
|
+
return aliasUnbound ? providerToolNameAlias(name) : name;
|
|
10359
|
+
};
|
|
10360
|
+
const mapping = JSON.stringify([[...flatNames], [...scoped], aliasUnbound]);
|
|
10361
|
+
let changed = false;
|
|
10362
|
+
const next = items.map((item) => {
|
|
10363
|
+
if (item === null || typeof item !== "object") return item;
|
|
10364
|
+
const cached = rewrittenItems.get(item);
|
|
10365
|
+
let rewritten;
|
|
10366
|
+
if (cached?.mapping === mapping) {
|
|
10367
|
+
rewritten = cached.item;
|
|
10368
|
+
} else {
|
|
10369
|
+
rewritten = rewriteItem(item, rewrite);
|
|
10370
|
+
if (rewritten !== item) rewrittenItems.set(item, { mapping, item: rewritten });
|
|
10371
|
+
}
|
|
10372
|
+
if (rewritten !== item) changed = true;
|
|
10373
|
+
return rewritten;
|
|
10374
|
+
});
|
|
10375
|
+
return changed ? next : items;
|
|
10376
|
+
}
|
|
10377
|
+
|
|
10378
|
+
// src/provider-request-tool-names.ts
|
|
10379
|
+
var ToolNameAliases = class {
|
|
10380
|
+
constructor(flat = /* @__PURE__ */ new Map(), namespaces = /* @__PURE__ */ new Map(), leaves = /* @__PURE__ */ new Map()) {
|
|
10381
|
+
this.flat = flat;
|
|
10382
|
+
this.namespaces = namespaces;
|
|
10383
|
+
this.leaves = leaves;
|
|
10384
|
+
}
|
|
10385
|
+
get size() {
|
|
10386
|
+
return this.flat.size + this.namespaces.size + this.leaves.size;
|
|
10387
|
+
}
|
|
10388
|
+
/** The original of a tool name the provider saw (unchanged when it is no alias). */
|
|
10389
|
+
restoreFlat(name) {
|
|
10390
|
+
return this.flat.get(name) ?? name;
|
|
10391
|
+
}
|
|
10392
|
+
restoreNamespace(name) {
|
|
10393
|
+
return this.namespaces.get(name) ?? name;
|
|
10394
|
+
}
|
|
10395
|
+
/** The original of a name inside a namespace, given the namespace's original. */
|
|
10396
|
+
restoreLeaf(namespace, name) {
|
|
10397
|
+
return this.leaves.get(leafKey(namespace, name)) ?? name;
|
|
10398
|
+
}
|
|
10399
|
+
};
|
|
10400
|
+
var NO_ALIASES = new ToolNameAliases();
|
|
10401
|
+
var REJECTED_TOOL_NAMES_LIMIT = 1024;
|
|
10402
|
+
var RejectedToolNames = class {
|
|
10403
|
+
entries = /* @__PURE__ */ new Set();
|
|
10404
|
+
get size() {
|
|
10405
|
+
return this.entries.size;
|
|
10406
|
+
}
|
|
10407
|
+
/** The names rejected for one model, as the guard's `rejected` option. */
|
|
10408
|
+
forModel(model) {
|
|
10409
|
+
const scope = typeof model === "string" ? model : "";
|
|
10410
|
+
return { has: (name) => this.entries.has(rejectedKey(scope, name)) };
|
|
10411
|
+
}
|
|
10412
|
+
/**
|
|
10413
|
+
* Remember the tool name at the path a provider's error names, in the
|
|
10414
|
+
* body that was sent, when it is one of the request's tool names. A path
|
|
10415
|
+
* that names no tool name of the request is ignored.
|
|
10416
|
+
*/
|
|
10417
|
+
rememberRejection(api, sent, aliases, error) {
|
|
10418
|
+
const name = rejectedToolName(api, sent, error);
|
|
10419
|
+
if (name === null) return null;
|
|
10420
|
+
const flat = aliases.restoreFlat(name);
|
|
10421
|
+
const original = flat !== name ? flat : aliases.restoreNamespace(name);
|
|
10422
|
+
const key = rejectedKey(typeof sent.model === "string" ? sent.model : "", original);
|
|
10423
|
+
this.entries.delete(key);
|
|
10424
|
+
this.entries.add(key);
|
|
10425
|
+
if (this.entries.size > REJECTED_TOOL_NAMES_LIMIT) {
|
|
10426
|
+
const oldest = this.entries.values().next().value;
|
|
10427
|
+
if (oldest !== void 0) this.entries.delete(oldest);
|
|
10428
|
+
}
|
|
10429
|
+
return original;
|
|
10430
|
+
}
|
|
10431
|
+
};
|
|
10432
|
+
function isVariantSegment(record4, segment) {
|
|
10433
|
+
return record4.type === segment || segment === "custom" && record4.type === void 0;
|
|
10434
|
+
}
|
|
10435
|
+
function rejectedKey(model, name) {
|
|
10436
|
+
return `${model}\0${name}`;
|
|
10437
|
+
}
|
|
10438
|
+
function rejectedToolName(api, sent, error) {
|
|
10439
|
+
const path = invalidRequestRawPath(error);
|
|
10440
|
+
if (path === null) return null;
|
|
10441
|
+
let current = sent;
|
|
10442
|
+
for (const match of path.matchAll(/\[(\d+)\]|([^.[\]]+)/g)) {
|
|
10443
|
+
const segment = match[1] ?? match[2] ?? "";
|
|
10444
|
+
if (Array.isArray(current) && /^\d+$/.test(segment)) {
|
|
10445
|
+
current = current[Number(segment)];
|
|
10446
|
+
} else if (isRecord7(current) && Object.hasOwn(current, segment)) {
|
|
10447
|
+
current = current[segment];
|
|
10448
|
+
} else if (!(isRecord7(current) && isVariantSegment(current, segment))) {
|
|
10449
|
+
return null;
|
|
10450
|
+
}
|
|
10451
|
+
}
|
|
10452
|
+
const candidate = typeof current === "string" ? current : isRecord7(current) && typeof current.name === "string" ? current.name : null;
|
|
10453
|
+
if (candidate === null) return null;
|
|
10454
|
+
let known = false;
|
|
10455
|
+
WALKERS[api](sent, (ref) => {
|
|
10456
|
+
if (ref.name === candidate) known = true;
|
|
10457
|
+
return ref.name;
|
|
10458
|
+
});
|
|
10459
|
+
return known ? candidate : null;
|
|
10460
|
+
}
|
|
10461
|
+
function observeToolNames(observer, observation) {
|
|
10462
|
+
try {
|
|
10463
|
+
observer?.(observation);
|
|
10464
|
+
} catch {
|
|
10465
|
+
}
|
|
10466
|
+
}
|
|
10467
|
+
function isRecord7(value) {
|
|
10468
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10469
|
+
}
|
|
10470
|
+
function namespaceOf(record4) {
|
|
10471
|
+
return typeof record4.namespace === "string" && record4.namespace.length > 0 ? record4.namespace : void 0;
|
|
10472
|
+
}
|
|
10473
|
+
function leafKey(namespace, name) {
|
|
10474
|
+
return `${namespace}\0${name}`;
|
|
10475
|
+
}
|
|
10476
|
+
function withValue(record4, key, value) {
|
|
10477
|
+
return record4[key] === value ? record4 : { ...record4, [key]: value };
|
|
10478
|
+
}
|
|
10479
|
+
function mapList(list, map) {
|
|
10480
|
+
let changed = false;
|
|
10481
|
+
const next = list.map((entry) => {
|
|
10482
|
+
const mapped = map(entry);
|
|
10483
|
+
if (mapped !== entry) changed = true;
|
|
10484
|
+
return mapped;
|
|
10485
|
+
});
|
|
10486
|
+
return changed ? next : list;
|
|
10487
|
+
}
|
|
10488
|
+
function definitionList(list, map, nameOf) {
|
|
10489
|
+
const next = mapList(list, map);
|
|
10490
|
+
if (next === list) return list;
|
|
10491
|
+
const seen = /* @__PURE__ */ new Set();
|
|
10492
|
+
return next.filter((entry) => {
|
|
10493
|
+
const name = nameOf(entry);
|
|
10494
|
+
if (name === void 0) return true;
|
|
10495
|
+
if (seen.has(name)) return false;
|
|
10496
|
+
seen.add(name);
|
|
10497
|
+
return true;
|
|
10498
|
+
});
|
|
10499
|
+
}
|
|
10500
|
+
function nameRef(name, namespace, field2) {
|
|
10501
|
+
return namespace === void 0 ? { kind: "flat", name, field: field2 } : { kind: "leaf", name, namespace, field: field2 };
|
|
10502
|
+
}
|
|
10503
|
+
function mcpScope(record4) {
|
|
10504
|
+
return `\0mcp:${typeof record4.server_label === "string" ? record4.server_label : ""}`;
|
|
10505
|
+
}
|
|
10506
|
+
function responsesDefinitionName(entry) {
|
|
10507
|
+
if (!isRecord7(entry)) return void 0;
|
|
10508
|
+
if (entry.type === "tool_reference" || typeof entry.name !== "string") return void 0;
|
|
10509
|
+
return `${entry.type === "namespace" ? "namespace" : "tool"}\0${entry.name}`;
|
|
10510
|
+
}
|
|
10511
|
+
function responsesDefinition(entry, visit, field2, inNamespace) {
|
|
10512
|
+
if (!isRecord7(entry)) return entry;
|
|
10513
|
+
if (entry.type === "namespace" && inNamespace === void 0 && typeof entry.name === "string") {
|
|
10514
|
+
const namespace2 = entry.name;
|
|
10515
|
+
let next2 = withValue(entry, "name", visit({ kind: "namespace", name: namespace2, field: field2 }));
|
|
10516
|
+
if (Array.isArray(entry.tools)) {
|
|
10517
|
+
next2 = withValue(
|
|
10518
|
+
next2,
|
|
10519
|
+
"tools",
|
|
10520
|
+
definitionList(
|
|
10521
|
+
entry.tools,
|
|
10522
|
+
(tool2) => responsesDefinition(tool2, visit, field2, namespace2),
|
|
10523
|
+
responsesDefinitionName
|
|
10524
|
+
)
|
|
10525
|
+
);
|
|
10526
|
+
}
|
|
10527
|
+
return next2;
|
|
10528
|
+
}
|
|
10529
|
+
const nameKey = entry.type === "tool_reference" ? "function_name" : entry.type === "function" || entry.type === "custom" ? "name" : null;
|
|
10530
|
+
if (nameKey === null || typeof entry[nameKey] !== "string") return entry;
|
|
10531
|
+
const own = inNamespace === void 0 ? namespaceOf(entry) : void 0;
|
|
10532
|
+
const namespace = inNamespace ?? own;
|
|
10533
|
+
let next = withValue(entry, nameKey, visit(nameRef(entry[nameKey], namespace, field2)));
|
|
10534
|
+
if (own !== void 0) {
|
|
10535
|
+
next = withValue(next, "namespace", visit({ kind: "namespace", name: own, field: field2 }));
|
|
10536
|
+
}
|
|
10537
|
+
return next;
|
|
10538
|
+
}
|
|
10539
|
+
function responsesNamedCall(record4, visit, field2) {
|
|
10540
|
+
if (typeof record4.name !== "string") return record4;
|
|
10541
|
+
const namespace = namespaceOf(record4);
|
|
10542
|
+
let next = withValue(record4, "name", visit(nameRef(record4.name, namespace, field2)));
|
|
10543
|
+
if (namespace !== void 0) {
|
|
10544
|
+
next = withValue(next, "namespace", visit({ kind: "namespace", name: namespace, field: field2 }));
|
|
10545
|
+
}
|
|
10546
|
+
return next;
|
|
10547
|
+
}
|
|
10548
|
+
function responsesMcpItem(record4, visit, field2) {
|
|
10549
|
+
const scope = mcpScope(record4);
|
|
10550
|
+
if (record4.type === "mcp_list_tools") {
|
|
10551
|
+
if (!Array.isArray(record4.tools)) return record4;
|
|
10552
|
+
return withValue(
|
|
10553
|
+
record4,
|
|
10554
|
+
"tools",
|
|
10555
|
+
definitionList(
|
|
10556
|
+
record4.tools,
|
|
10557
|
+
(tool2) => isRecord7(tool2) && typeof tool2.name === "string" ? withValue(tool2, "name", visit(nameRef(tool2.name, scope, field2))) : tool2,
|
|
10558
|
+
responsesDefinitionName
|
|
10559
|
+
)
|
|
10560
|
+
);
|
|
10561
|
+
}
|
|
10562
|
+
return typeof record4.name === "string" ? withValue(record4, "name", visit(nameRef(record4.name, scope, field2))) : record4;
|
|
10563
|
+
}
|
|
10564
|
+
function responsesItem(item, visit, field2) {
|
|
10565
|
+
if (!isRecord7(item)) return item;
|
|
10566
|
+
switch (item.type) {
|
|
10567
|
+
case "function_call":
|
|
10568
|
+
case "custom_tool_call":
|
|
10569
|
+
return responsesNamedCall(item, visit, field2);
|
|
10570
|
+
case "tool_search_output":
|
|
10571
|
+
case "additional_tools":
|
|
10572
|
+
return Array.isArray(item.tools) ? withValue(
|
|
10573
|
+
item,
|
|
10574
|
+
"tools",
|
|
10575
|
+
definitionList(
|
|
10576
|
+
item.tools,
|
|
10577
|
+
(tool2) => responsesDefinition(tool2, visit, field2),
|
|
10578
|
+
responsesDefinitionName
|
|
10579
|
+
)
|
|
10580
|
+
) : item;
|
|
10581
|
+
case "mcp_call":
|
|
10582
|
+
case "mcp_approval_request":
|
|
10583
|
+
case "mcp_list_tools":
|
|
10584
|
+
return responsesMcpItem(item, visit, field2);
|
|
10585
|
+
default:
|
|
10586
|
+
return item;
|
|
10587
|
+
}
|
|
10588
|
+
}
|
|
10589
|
+
function declaredNamespaces(tools) {
|
|
10590
|
+
const namespaces = /* @__PURE__ */ new Map();
|
|
10591
|
+
if (!Array.isArray(tools)) return namespaces;
|
|
10592
|
+
for (const tool2 of tools) {
|
|
10593
|
+
if (!isRecord7(tool2) || tool2.type !== "namespace" || typeof tool2.name !== "string") continue;
|
|
10594
|
+
const leaves = /* @__PURE__ */ new Set();
|
|
10595
|
+
for (const entry of Array.isArray(tool2.tools) ? tool2.tools : []) {
|
|
10596
|
+
if (isRecord7(entry) && typeof entry.name === "string") leaves.add(entry.name);
|
|
10597
|
+
}
|
|
10598
|
+
namespaces.set(tool2.name, leaves);
|
|
10599
|
+
}
|
|
10600
|
+
return namespaces;
|
|
10601
|
+
}
|
|
10602
|
+
function responsesChoiceEntry(entry, visit, namespaces) {
|
|
10603
|
+
if (!isRecord7(entry)) return entry;
|
|
10604
|
+
if (entry.type === "mcp") return responsesMcpItem(entry, visit, "tool_choice");
|
|
10605
|
+
if (entry.type !== "function" && entry.type !== "custom") return entry;
|
|
10606
|
+
if (typeof entry.name !== "string") return entry;
|
|
10607
|
+
if (namespaceOf(entry) === void 0) {
|
|
10608
|
+
const name = entry.name;
|
|
10609
|
+
for (let dot2 = name.indexOf("."); dot2 > 0; dot2 = name.indexOf(".", dot2 + 1)) {
|
|
10610
|
+
const namespace = name.slice(0, dot2);
|
|
10611
|
+
const leaf = name.slice(dot2 + 1);
|
|
10612
|
+
if (!namespaces.get(namespace)?.has(leaf)) continue;
|
|
10613
|
+
const qualifiedNamespace = visit({
|
|
10614
|
+
kind: "namespace",
|
|
10615
|
+
name: namespace,
|
|
10616
|
+
field: "tool_choice"
|
|
10617
|
+
});
|
|
10618
|
+
const qualifiedLeaf = visit({ kind: "leaf", name: leaf, namespace, field: "tool_choice" });
|
|
10619
|
+
return withValue(entry, "name", `${qualifiedNamespace}.${qualifiedLeaf}`);
|
|
10620
|
+
}
|
|
10621
|
+
}
|
|
10622
|
+
return responsesNamedCall(entry, visit, "tool_choice");
|
|
10623
|
+
}
|
|
10624
|
+
function responsesToolChoice(choice, visit, namespaces) {
|
|
10625
|
+
if (!isRecord7(choice)) return choice;
|
|
10626
|
+
if (choice.type === "allowed_tools" && Array.isArray(choice.tools)) {
|
|
10627
|
+
return withValue(
|
|
10628
|
+
choice,
|
|
10629
|
+
"tools",
|
|
10630
|
+
mapList(choice.tools, (entry) => responsesChoiceEntry(entry, visit, namespaces))
|
|
10631
|
+
);
|
|
10632
|
+
}
|
|
10633
|
+
return responsesChoiceEntry(choice, visit, namespaces);
|
|
10634
|
+
}
|
|
10635
|
+
var responsesBody = (body, visit) => {
|
|
10636
|
+
let next = body;
|
|
10637
|
+
if (Array.isArray(body.tools)) {
|
|
10638
|
+
next = withValue(
|
|
10639
|
+
next,
|
|
10640
|
+
"tools",
|
|
10641
|
+
definitionList(
|
|
10642
|
+
body.tools,
|
|
10643
|
+
(tool2) => responsesDefinition(tool2, visit, "tools"),
|
|
10644
|
+
responsesDefinitionName
|
|
10645
|
+
)
|
|
10646
|
+
);
|
|
10647
|
+
}
|
|
10648
|
+
if (isRecord7(body.tool_choice)) {
|
|
10649
|
+
next = withValue(
|
|
10650
|
+
next,
|
|
10651
|
+
"tool_choice",
|
|
10652
|
+
responsesToolChoice(body.tool_choice, visit, declaredNamespaces(body.tools))
|
|
10653
|
+
);
|
|
10654
|
+
}
|
|
10655
|
+
if (Array.isArray(body.input)) {
|
|
10656
|
+
next = withValue(
|
|
10657
|
+
next,
|
|
10658
|
+
"input",
|
|
10659
|
+
mapList(body.input, (item) => responsesItem(item, visit, "history"))
|
|
10660
|
+
);
|
|
10661
|
+
}
|
|
10662
|
+
return next;
|
|
10663
|
+
};
|
|
10664
|
+
function chatNamed(entry, visit, field2) {
|
|
10665
|
+
if (!isRecord7(entry)) return entry;
|
|
10666
|
+
const key = entry.type === "custom" ? "custom" : "function";
|
|
10667
|
+
const inner = entry[key];
|
|
10668
|
+
if (!isRecord7(inner) || typeof inner.name !== "string") return entry;
|
|
10669
|
+
return withValue(
|
|
10670
|
+
entry,
|
|
10671
|
+
key,
|
|
10672
|
+
withValue(inner, "name", visit(nameRef(inner.name, void 0, field2)))
|
|
10673
|
+
);
|
|
10674
|
+
}
|
|
10675
|
+
function chatDefinitionName(entry) {
|
|
10676
|
+
if (!isRecord7(entry)) return void 0;
|
|
10677
|
+
const inner = entry[entry.type === "custom" ? "custom" : "function"];
|
|
10678
|
+
return isRecord7(inner) && typeof inner.name === "string" ? inner.name : void 0;
|
|
10679
|
+
}
|
|
10680
|
+
function chatToolChoice(choice, visit) {
|
|
10681
|
+
if (!isRecord7(choice)) return choice;
|
|
10682
|
+
if (choice.type === "allowed_tools" && isRecord7(choice.allowed_tools)) {
|
|
10683
|
+
const allowed = choice.allowed_tools;
|
|
10684
|
+
if (!Array.isArray(allowed.tools)) return choice;
|
|
10685
|
+
return withValue(
|
|
10686
|
+
choice,
|
|
10687
|
+
"allowed_tools",
|
|
10688
|
+
withValue(
|
|
10689
|
+
allowed,
|
|
10690
|
+
"tools",
|
|
10691
|
+
mapList(allowed.tools, (entry) => chatNamed(entry, visit, "tool_choice"))
|
|
10692
|
+
)
|
|
10693
|
+
);
|
|
10694
|
+
}
|
|
10695
|
+
return chatNamed(choice, visit, "tool_choice");
|
|
10696
|
+
}
|
|
10697
|
+
function chatMessage(message, visit) {
|
|
10698
|
+
if (!isRecord7(message)) return message;
|
|
10699
|
+
let next = message;
|
|
10700
|
+
if (Array.isArray(message.tool_calls)) {
|
|
10701
|
+
next = withValue(
|
|
10702
|
+
next,
|
|
10703
|
+
"tool_calls",
|
|
10704
|
+
mapList(message.tool_calls, (call) => chatNamed(call, visit, "history"))
|
|
10705
|
+
);
|
|
10706
|
+
}
|
|
10707
|
+
if (isRecord7(message.function_call) && typeof message.function_call.name === "string") {
|
|
10708
|
+
next = withValue(
|
|
10709
|
+
next,
|
|
10710
|
+
"function_call",
|
|
10711
|
+
withValue(
|
|
10712
|
+
message.function_call,
|
|
10713
|
+
"name",
|
|
10714
|
+
visit(nameRef(message.function_call.name, void 0, "history"))
|
|
10715
|
+
)
|
|
10716
|
+
);
|
|
10717
|
+
}
|
|
10718
|
+
if (message.role === "function" && typeof message.name === "string") {
|
|
10719
|
+
next = withValue(next, "name", visit(nameRef(message.name, void 0, "history")));
|
|
10720
|
+
}
|
|
10721
|
+
return next;
|
|
10722
|
+
}
|
|
10723
|
+
var chatBody = (body, visit) => {
|
|
10724
|
+
let next = body;
|
|
10725
|
+
if (Array.isArray(body.tools)) {
|
|
10726
|
+
next = withValue(
|
|
10727
|
+
next,
|
|
10728
|
+
"tools",
|
|
10729
|
+
definitionList(body.tools, (tool2) => chatNamed(tool2, visit, "tools"), chatDefinitionName)
|
|
10730
|
+
);
|
|
10731
|
+
}
|
|
10732
|
+
if (Array.isArray(body.functions)) {
|
|
10733
|
+
next = withValue(
|
|
10734
|
+
next,
|
|
10735
|
+
"functions",
|
|
10736
|
+
definitionList(
|
|
10737
|
+
body.functions,
|
|
10738
|
+
(fn) => isRecord7(fn) && typeof fn.name === "string" ? withValue(fn, "name", visit(nameRef(fn.name, void 0, "tools"))) : fn,
|
|
10739
|
+
(fn) => isRecord7(fn) && typeof fn.name === "string" ? fn.name : void 0
|
|
10740
|
+
)
|
|
10741
|
+
);
|
|
10742
|
+
}
|
|
10743
|
+
if (isRecord7(body.tool_choice)) {
|
|
10744
|
+
next = withValue(next, "tool_choice", chatToolChoice(body.tool_choice, visit));
|
|
10745
|
+
}
|
|
10746
|
+
if (isRecord7(body.function_call) && typeof body.function_call.name === "string") {
|
|
10747
|
+
next = withValue(
|
|
10748
|
+
next,
|
|
10749
|
+
"function_call",
|
|
10750
|
+
withValue(
|
|
10751
|
+
body.function_call,
|
|
10752
|
+
"name",
|
|
10753
|
+
visit(nameRef(body.function_call.name, void 0, "tool_choice"))
|
|
10754
|
+
)
|
|
10755
|
+
);
|
|
10756
|
+
}
|
|
10757
|
+
if (Array.isArray(body.messages)) {
|
|
10758
|
+
next = withValue(
|
|
10759
|
+
next,
|
|
10760
|
+
"messages",
|
|
10761
|
+
mapList(body.messages, (message) => chatMessage(message, visit))
|
|
10762
|
+
);
|
|
10763
|
+
}
|
|
10764
|
+
return next;
|
|
10765
|
+
};
|
|
10766
|
+
function isClaudeClientTool(tool2) {
|
|
10767
|
+
return tool2.type === void 0 || tool2.type === "custom";
|
|
10768
|
+
}
|
|
10769
|
+
function claudeDefinitionName(entry) {
|
|
10770
|
+
return isRecord7(entry) && typeof entry.name === "string" ? entry.name : void 0;
|
|
10771
|
+
}
|
|
10772
|
+
function claudeBlock(block, visit) {
|
|
10773
|
+
if (!isRecord7(block) || block.type !== "tool_use" || typeof block.name !== "string") return block;
|
|
10774
|
+
return withValue(block, "name", visit(nameRef(block.name, void 0, "history")));
|
|
10775
|
+
}
|
|
10776
|
+
var claudeBody = (body, visit) => {
|
|
10777
|
+
let next = body;
|
|
10778
|
+
if (Array.isArray(body.tools)) {
|
|
10779
|
+
next = withValue(
|
|
10780
|
+
next,
|
|
10781
|
+
"tools",
|
|
10782
|
+
definitionList(
|
|
10783
|
+
body.tools,
|
|
10784
|
+
(tool2) => isRecord7(tool2) && isClaudeClientTool(tool2) && typeof tool2.name === "string" ? withValue(tool2, "name", visit(nameRef(tool2.name, void 0, "tools"))) : tool2,
|
|
10785
|
+
claudeDefinitionName
|
|
10786
|
+
)
|
|
10787
|
+
);
|
|
10163
10788
|
}
|
|
10164
|
-
|
|
10165
|
-
|
|
10166
|
-
|
|
10167
|
-
|
|
10168
|
-
|
|
10169
|
-
|
|
10170
|
-
|
|
10171
|
-
|
|
10172
|
-
|
|
10173
|
-
|
|
10174
|
-
|
|
10175
|
-
|
|
10176
|
-
|
|
10177
|
-
|
|
10178
|
-
|
|
10179
|
-
|
|
10180
|
-
|
|
10181
|
-
|
|
10182
|
-
|
|
10183
|
-
|
|
10184
|
-
|
|
10185
|
-
|
|
10186
|
-
|
|
10187
|
-
|
|
10188
|
-
|
|
10789
|
+
if (isRecord7(body.tool_choice) && body.tool_choice.type === "tool" && typeof body.tool_choice.name === "string") {
|
|
10790
|
+
next = withValue(
|
|
10791
|
+
next,
|
|
10792
|
+
"tool_choice",
|
|
10793
|
+
withValue(
|
|
10794
|
+
body.tool_choice,
|
|
10795
|
+
"name",
|
|
10796
|
+
visit(nameRef(body.tool_choice.name, void 0, "tool_choice"))
|
|
10797
|
+
)
|
|
10798
|
+
);
|
|
10799
|
+
}
|
|
10800
|
+
if (Array.isArray(body.messages)) {
|
|
10801
|
+
next = withValue(
|
|
10802
|
+
next,
|
|
10803
|
+
"messages",
|
|
10804
|
+
mapList(
|
|
10805
|
+
body.messages,
|
|
10806
|
+
(message) => isRecord7(message) && Array.isArray(message.content) ? withValue(
|
|
10807
|
+
message,
|
|
10808
|
+
"content",
|
|
10809
|
+
mapList(message.content, (block) => claudeBlock(block, visit))
|
|
10810
|
+
) : message
|
|
10811
|
+
)
|
|
10812
|
+
);
|
|
10813
|
+
}
|
|
10814
|
+
return next;
|
|
10815
|
+
};
|
|
10816
|
+
var WALKERS = {
|
|
10817
|
+
responses: responsesBody,
|
|
10818
|
+
chat: chatBody,
|
|
10819
|
+
"anthropic-messages": claudeBody
|
|
10820
|
+
};
|
|
10821
|
+
function identityKey(ref) {
|
|
10822
|
+
return ref.kind === "leaf" ? `l\0${leafKey(ref.namespace, ref.name)}` : `${ref.kind === "flat" ? "f" : "n"}\0${ref.name}`;
|
|
10823
|
+
}
|
|
10824
|
+
function scopeKey(ref) {
|
|
10825
|
+
return ref.kind === "leaf" ? `l\0${ref.namespace}` : "top";
|
|
10826
|
+
}
|
|
10827
|
+
function guardRequestToolNames(api, body, options = {}) {
|
|
10828
|
+
const walk = WALKERS[api];
|
|
10829
|
+
const strict = options.strict === true;
|
|
10830
|
+
const rejected = options.rejected;
|
|
10831
|
+
const refs = [];
|
|
10832
|
+
walk(body, (ref) => {
|
|
10833
|
+
refs.push(ref);
|
|
10834
|
+
return ref.name;
|
|
10189
10835
|
});
|
|
10190
|
-
|
|
10836
|
+
const renames = (ref) => strict || !isProviderToolName(ref.name) || rejected?.has(ref.name) === true;
|
|
10837
|
+
if (!refs.some(renames)) return { body, aliases: NO_ALIASES, repaired: [] };
|
|
10838
|
+
const taken = /* @__PURE__ */ new Map();
|
|
10839
|
+
const takenIn = (scope) => {
|
|
10840
|
+
let names = taken.get(scope);
|
|
10841
|
+
if (!names) taken.set(scope, names = /* @__PURE__ */ new Set());
|
|
10842
|
+
return names;
|
|
10843
|
+
};
|
|
10844
|
+
const pending = /* @__PURE__ */ new Map();
|
|
10845
|
+
const repaired = /* @__PURE__ */ new Set();
|
|
10846
|
+
for (const ref of refs) {
|
|
10847
|
+
if (!renames(ref)) {
|
|
10848
|
+
takenIn(scopeKey(ref)).add(ref.name);
|
|
10849
|
+
continue;
|
|
10850
|
+
}
|
|
10851
|
+
repaired.add(ref.field);
|
|
10852
|
+
const key = identityKey(ref);
|
|
10853
|
+
if (!pending.has(key)) pending.set(key, ref);
|
|
10854
|
+
}
|
|
10855
|
+
const forward = /* @__PURE__ */ new Map();
|
|
10856
|
+
const flat = /* @__PURE__ */ new Map();
|
|
10857
|
+
const namespaces = /* @__PURE__ */ new Map();
|
|
10858
|
+
const leaves = /* @__PURE__ */ new Map();
|
|
10859
|
+
for (const key of [...pending.keys()].sort()) {
|
|
10860
|
+
const ref = pending.get(key);
|
|
10861
|
+
const names = takenIn(scopeKey(ref));
|
|
10862
|
+
const base = providerToolNameAlias(ref.name);
|
|
10863
|
+
let alias = base;
|
|
10864
|
+
for (let suffix = 2; names.has(alias); suffix += 1) alias = `${base}_${suffix}`;
|
|
10865
|
+
names.add(alias);
|
|
10866
|
+
forward.set(key, alias);
|
|
10867
|
+
if (ref.kind === "flat") flat.set(alias, ref.name);
|
|
10868
|
+
else if (ref.kind === "namespace") namespaces.set(alias, ref.name);
|
|
10869
|
+
else leaves.set(leafKey(ref.namespace, alias), ref.name);
|
|
10870
|
+
}
|
|
10871
|
+
const guarded = walk(body, (ref) => forward.get(identityKey(ref)) ?? ref.name);
|
|
10872
|
+
return {
|
|
10873
|
+
body: guarded,
|
|
10874
|
+
aliases: new ToolNameAliases(flat, namespaces, leaves),
|
|
10875
|
+
repaired: [...repaired].sort()
|
|
10876
|
+
};
|
|
10191
10877
|
}
|
|
10192
|
-
|
|
10193
|
-
|
|
10194
|
-
|
|
10195
|
-
|
|
10196
|
-
|
|
10197
|
-
|
|
10198
|
-
|
|
10199
|
-
|
|
10200
|
-
|
|
10201
|
-
|
|
10202
|
-
|
|
10878
|
+
function restoring(aliases) {
|
|
10879
|
+
return (ref) => ref.kind === "flat" ? aliases.restoreFlat(ref.name) : ref.kind === "namespace" ? aliases.restoreNamespace(ref.name) : aliases.restoreLeaf(aliases.restoreNamespace(ref.namespace), ref.name);
|
|
10880
|
+
}
|
|
10881
|
+
function restoreResponsesToolNames(payload, aliases) {
|
|
10882
|
+
if (aliases.size === 0 || !isRecord7(payload)) return payload;
|
|
10883
|
+
const visit = restoring(aliases);
|
|
10884
|
+
let next = payload;
|
|
10885
|
+
if (Array.isArray(payload.output)) {
|
|
10886
|
+
next = withValue(
|
|
10887
|
+
next,
|
|
10888
|
+
"output",
|
|
10889
|
+
mapList(payload.output, (item) => responsesItem(item, visit, "history"))
|
|
10890
|
+
);
|
|
10203
10891
|
}
|
|
10204
|
-
|
|
10205
|
-
|
|
10206
|
-
|
|
10207
|
-
|
|
10208
|
-
|
|
10209
|
-
|
|
10210
|
-
|
|
10211
|
-
|
|
10212
|
-
|
|
10892
|
+
if (isRecord7(payload.item)) {
|
|
10893
|
+
next = withValue(next, "item", responsesItem(payload.item, visit, "history"));
|
|
10894
|
+
}
|
|
10895
|
+
if (isRecord7(payload.response)) {
|
|
10896
|
+
next = withValue(next, "response", restoreResponsesToolNames(payload.response, aliases));
|
|
10897
|
+
}
|
|
10898
|
+
if (typeof payload.type === "string" && payload.type.startsWith("response.") && typeof payload.name === "string") {
|
|
10899
|
+
if (typeof payload.namespace === "string") {
|
|
10900
|
+
const namespace = aliases.restoreNamespace(payload.namespace);
|
|
10901
|
+
next = withValue(next, "namespace", namespace);
|
|
10902
|
+
next = withValue(next, "name", aliases.restoreLeaf(namespace, payload.name));
|
|
10903
|
+
} else {
|
|
10904
|
+
next = withValue(next, "name", aliases.restoreFlat(payload.name));
|
|
10213
10905
|
}
|
|
10214
10906
|
}
|
|
10215
|
-
|
|
10216
|
-
recorded.delete(matched);
|
|
10217
|
-
recorded.set(matched, true);
|
|
10218
|
-
return [instructions.slice(0, end), instructions.slice(end)];
|
|
10907
|
+
return next;
|
|
10219
10908
|
}
|
|
10220
|
-
|
|
10221
|
-
|
|
10222
|
-
|
|
10223
|
-
|
|
10224
|
-
|
|
10225
|
-
|
|
10226
|
-
|
|
10227
|
-
|
|
10228
|
-
|
|
10229
|
-
|
|
10230
|
-
|
|
10231
|
-
|
|
10232
|
-
|
|
10233
|
-
|
|
10234
|
-
"x-stainless-runtime-version": "v26.3.0",
|
|
10235
|
-
"x-stainless-retry-count": "0",
|
|
10236
|
-
"x-stainless-timeout": "600",
|
|
10237
|
-
"anthropic-dangerous-direct-browser-access": "true",
|
|
10238
|
-
"anthropic-dispatch-id": "v2d"
|
|
10239
|
-
};
|
|
10240
|
-
function claudeCodeFingerprint(input) {
|
|
10241
|
-
const message = typeof input === "string" ? input : input.find(
|
|
10242
|
-
(item) => (item.type === void 0 || item.type === "message") && "role" in item && item.role === "user"
|
|
10909
|
+
function restoreChatToolNames(payload, aliases) {
|
|
10910
|
+
if (aliases.size === 0 || !isRecord7(payload) || !Array.isArray(payload.choices)) return payload;
|
|
10911
|
+
const visit = restoring(aliases);
|
|
10912
|
+
return withValue(
|
|
10913
|
+
payload,
|
|
10914
|
+
"choices",
|
|
10915
|
+
mapList(payload.choices, (choice) => {
|
|
10916
|
+
if (!isRecord7(choice)) return choice;
|
|
10917
|
+
let next = choice;
|
|
10918
|
+
for (const key of ["message", "delta"]) {
|
|
10919
|
+
if (isRecord7(choice[key])) next = withValue(next, key, chatMessage(choice[key], visit));
|
|
10920
|
+
}
|
|
10921
|
+
return next;
|
|
10922
|
+
})
|
|
10243
10923
|
);
|
|
10244
|
-
const content = typeof message === "string" ? message : message && "content" in message ? message.content : "";
|
|
10245
|
-
const first = typeof content === "string" ? content : Array.isArray(content) ? content.find((block) => block.type === "input_text" || String(block.type) === "text") : void 0;
|
|
10246
|
-
const prompt = typeof first === "string" ? first : first && "text" in first ? String(first.text) : "";
|
|
10247
|
-
const sample = [4, 7, 20].map((index) => prompt[index] || "0").join("");
|
|
10248
|
-
return createHash6("sha256").update(`59cf53e54c78${sample}2.1.285`).digest("hex").slice(0, 3);
|
|
10249
10924
|
}
|
|
10250
|
-
function
|
|
10251
|
-
|
|
10252
|
-
|
|
10253
|
-
|
|
10254
|
-
|
|
10255
|
-
|
|
10256
|
-
|
|
10257
|
-
|
|
10258
|
-
|
|
10259
|
-
|
|
10260
|
-
account_uuid: account.accountUuid,
|
|
10261
|
-
session_id: identity.sessionId
|
|
10262
|
-
})
|
|
10263
|
-
};
|
|
10264
|
-
const betas = new Set((headers.get("anthropic-beta") ?? "").split(",").filter(Boolean));
|
|
10265
|
-
betas.add("claude-code-20250219");
|
|
10266
|
-
betas.add("oauth-2025-04-20");
|
|
10267
|
-
if (body.thinking) {
|
|
10268
|
-
betas.add("interleaved-thinking-2025-05-14");
|
|
10269
|
-
betas.add("thinking-token-count-2026-05-13");
|
|
10270
|
-
betas.add("effort-2025-11-24");
|
|
10271
|
-
betas.add("thinking-display-updates-2026-08-18");
|
|
10925
|
+
function restoreClaudeToolNames(payload, aliases) {
|
|
10926
|
+
if (aliases.size === 0 || !isRecord7(payload)) return payload;
|
|
10927
|
+
const visit = restoring(aliases);
|
|
10928
|
+
let next = payload;
|
|
10929
|
+
if (Array.isArray(payload.content)) {
|
|
10930
|
+
next = withValue(
|
|
10931
|
+
next,
|
|
10932
|
+
"content",
|
|
10933
|
+
mapList(payload.content, (block) => claudeBlock(block, visit))
|
|
10934
|
+
);
|
|
10272
10935
|
}
|
|
10273
|
-
if (
|
|
10274
|
-
|
|
10275
|
-
|
|
10276
|
-
|
|
10277
|
-
|
|
10278
|
-
|
|
10279
|
-
|
|
10280
|
-
|
|
10281
|
-
|
|
10282
|
-
|
|
10936
|
+
if (isRecord7(payload.content_block)) {
|
|
10937
|
+
next = withValue(next, "content_block", claudeBlock(payload.content_block, visit));
|
|
10938
|
+
}
|
|
10939
|
+
return next;
|
|
10940
|
+
}
|
|
10941
|
+
function restoreToolNames(api, payload, aliases) {
|
|
10942
|
+
return api === "responses" ? restoreResponsesToolNames(payload, aliases) : api === "chat" ? restoreChatToolNames(payload, aliases) : restoreClaudeToolNames(payload, aliases);
|
|
10943
|
+
}
|
|
10944
|
+
var NAME_FIELD_PATH = /(?:^|\.)(?:name|namespace|function_name|tool_name)$/;
|
|
10945
|
+
var STRING_CONSTRAINT = /pattern|invalid_string|invalid string|maximum length|too long|string_above_max_length|invalid (?:tool|function) name/i;
|
|
10946
|
+
function isToolNameRejection(signature, message) {
|
|
10947
|
+
if (signature.path === "unknown") return false;
|
|
10948
|
+
const path = signature.path.replace(/\[\]$/, "");
|
|
10949
|
+
const namePath = NAME_FIELD_PATH.test(path) || /^tool_choice\b/.test(path);
|
|
10950
|
+
return namePath && (STRING_CONSTRAINT.test(signature.code) || STRING_CONSTRAINT.test(message));
|
|
10951
|
+
}
|
|
10952
|
+
function rejectsToolName(body) {
|
|
10953
|
+
return isToolNameRejection(invalidRequestSignature(body), invalidRequestMessage(body));
|
|
10954
|
+
}
|
|
10955
|
+
async function toolNameRejectionOfResponse(response) {
|
|
10956
|
+
if (response.status !== 400) return null;
|
|
10957
|
+
const body = await invalidRequestBodyOfResponse(response);
|
|
10958
|
+
return body !== null && rejectsToolName(body) ? body : null;
|
|
10283
10959
|
}
|
|
10284
10960
|
|
|
10285
10961
|
// src/anthropic-messages.ts
|
|
10286
|
-
import { claudeNativeModelProfile } from "@opengeni/config";
|
|
10287
|
-
import {
|
|
10288
|
-
hasClaudeCiteTag,
|
|
10289
|
-
rewriteClaudeCiteTags
|
|
10290
|
-
} from "@opengeni/contracts/claude-cite-tags";
|
|
10291
10962
|
var ANTHROPIC_PAUSE_TURN_MAX_REQUESTS = 8;
|
|
10292
10963
|
var AnthropicProtocolError = class extends Error {
|
|
10293
10964
|
code = "anthropic_protocol_error";
|
|
@@ -10745,9 +11416,7 @@ function buildAnthropicRequest(request, model, provider, stream) {
|
|
|
10745
11416
|
);
|
|
10746
11417
|
return matches[0][0];
|
|
10747
11418
|
};
|
|
10748
|
-
const input = typeof request.input === "string" ? request.input : projectHistoryForProvider(request.input, "anthropic-messages"
|
|
10749
|
-
knownToolNames: requestFunctionToolNames(request.tools)
|
|
10750
|
-
});
|
|
11419
|
+
const input = typeof request.input === "string" ? request.input : projectHistoryForProvider(request.input, "anthropic-messages");
|
|
10751
11420
|
const webSearch = request.tools.some(isHostedWebSearchTool);
|
|
10752
11421
|
let messages = anthropicMessages(input, {
|
|
10753
11422
|
webSearch,
|
|
@@ -11050,10 +11719,11 @@ function anthropicResponse(message, requestId, names = /* @__PURE__ */ new Map()
|
|
|
11050
11719
|
};
|
|
11051
11720
|
}
|
|
11052
11721
|
var AnthropicMessagesModel = class {
|
|
11053
|
-
constructor(provider, model, fetch2 = globalThis.fetch) {
|
|
11722
|
+
constructor(provider, model, fetch2 = globalThis.fetch, toolNameObservers = {}) {
|
|
11054
11723
|
this.provider = provider;
|
|
11055
11724
|
this.model = model;
|
|
11056
11725
|
this.fetch = fetch2;
|
|
11726
|
+
this.toolNameObservers = toolNameObservers;
|
|
11057
11727
|
}
|
|
11058
11728
|
fallbackSessionId = randomUUID3();
|
|
11059
11729
|
promptId = randomUUID3();
|
|
@@ -11061,6 +11731,15 @@ var AnthropicMessagesModel = class {
|
|
|
11061
11731
|
// The same bound applies from the first image onward. A growing conversation
|
|
11062
11732
|
// must not resize its old prefix when it crosses the many-image threshold.
|
|
11063
11733
|
sizeImage = createModelImageSizer(2e3, ANTHROPIC_IMAGE_MAX_ENCODED_BYTES);
|
|
11734
|
+
/** The tool-name aliases each response answers (see provider-request-tool-names.ts). */
|
|
11735
|
+
toolNameAliases = /* @__PURE__ */ new WeakMap();
|
|
11736
|
+
/** Names this model's route rejected (see RejectedToolNames). */
|
|
11737
|
+
rejectedToolNames = new RejectedToolNames();
|
|
11738
|
+
/** A Claude message or stream event with aliased tool names mapped back. */
|
|
11739
|
+
restoredToolNames(response, payload) {
|
|
11740
|
+
const aliases = this.toolNameAliases.get(response);
|
|
11741
|
+
return aliases ? restoreClaudeToolNames(payload, aliases) : payload;
|
|
11742
|
+
}
|
|
11064
11743
|
/** Project inline images only; never fetch arbitrary URLs or edit stored history. */
|
|
11065
11744
|
async sizeImageBlocks(blocks, projection, signal) {
|
|
11066
11745
|
const result = [];
|
|
@@ -11106,7 +11785,7 @@ var AnthropicMessagesModel = class {
|
|
|
11106
11785
|
buildAnthropicRequest(request, this.model, this.provider, false)
|
|
11107
11786
|
);
|
|
11108
11787
|
}
|
|
11109
|
-
async prepare(request, stream) {
|
|
11788
|
+
async prepare(request, stream, strictToolNames = false) {
|
|
11110
11789
|
request.signal?.throwIfAborted();
|
|
11111
11790
|
const body = buildAnthropicRequest(request, this.model, this.provider, stream);
|
|
11112
11791
|
const projection = { resized: false };
|
|
@@ -11175,15 +11854,46 @@ var AnthropicMessagesModel = class {
|
|
|
11175
11854
|
previousRequestId: this.previousRequestId
|
|
11176
11855
|
});
|
|
11177
11856
|
}
|
|
11178
|
-
const
|
|
11179
|
-
|
|
11857
|
+
const guarded = guardRequestToolNames("anthropic-messages", body, {
|
|
11858
|
+
strict: strictToolNames,
|
|
11859
|
+
rejected: this.rejectedToolNames.forModel(body.model)
|
|
11860
|
+
});
|
|
11861
|
+
const serialized = JSON.stringify(guarded.body);
|
|
11862
|
+
return {
|
|
11863
|
+
url,
|
|
11864
|
+
headers,
|
|
11865
|
+
serialized,
|
|
11866
|
+
size: anthropicRequestSize(guarded.body, serialized),
|
|
11867
|
+
toolNames: guarded,
|
|
11868
|
+
unguardedBody: body
|
|
11869
|
+
};
|
|
11180
11870
|
}
|
|
11181
11871
|
/** Uses the same final serialization as dispatch, but performs no network I/O. */
|
|
11182
11872
|
async measureRequest(request, stream = false) {
|
|
11183
11873
|
return (await this.prepare(request, stream)).size;
|
|
11184
11874
|
}
|
|
11185
|
-
|
|
11186
|
-
|
|
11875
|
+
/**
|
|
11876
|
+
* Send one request. A 400 that rejects a tool name is answered by sending
|
|
11877
|
+
* the request once more with every tool name aliased (strict mode); that
|
|
11878
|
+
* resend is never repeated, and its own answer is final.
|
|
11879
|
+
*/
|
|
11880
|
+
async send(request, stream, strictToolNames = false) {
|
|
11881
|
+
const {
|
|
11882
|
+
url,
|
|
11883
|
+
headers,
|
|
11884
|
+
serialized,
|
|
11885
|
+
size,
|
|
11886
|
+
toolNames: guardedToolNames,
|
|
11887
|
+
unguardedBody
|
|
11888
|
+
} = await this.prepare(request, stream, strictToolNames);
|
|
11889
|
+
if (!strictToolNames) {
|
|
11890
|
+
for (const field2 of guardedToolNames.repaired) {
|
|
11891
|
+
observeToolNames(this.toolNameObservers.onToolNameRepair, {
|
|
11892
|
+
api: "anthropic-messages",
|
|
11893
|
+
field: field2
|
|
11894
|
+
});
|
|
11895
|
+
}
|
|
11896
|
+
}
|
|
11187
11897
|
if (size.requestBytes > ANTHROPIC_REQUEST_MAX_BYTES)
|
|
11188
11898
|
throw new AnthropicRequestSizeError(size, "preflight");
|
|
11189
11899
|
request.signal?.throwIfAborted();
|
|
@@ -11210,6 +11920,30 @@ var AnthropicMessagesModel = class {
|
|
|
11210
11920
|
Math.min(this.provider.anthropic?.streamIdleTimeoutMs ?? 6e5, 5e3),
|
|
11211
11921
|
request.signal
|
|
11212
11922
|
);
|
|
11923
|
+
if (!strictToolNames && response.status === 400 && rejectsToolName(parseInvalidRequestText(detail)) && // Without a tool name to alias, a resend would repeat the same request.
|
|
11924
|
+
guardRequestToolNames("anthropic-messages", unguardedBody, { strict: true }).body !== unguardedBody) {
|
|
11925
|
+
let resent;
|
|
11926
|
+
try {
|
|
11927
|
+
resent = await this.send(request, stream, true);
|
|
11928
|
+
} catch (error) {
|
|
11929
|
+
observeToolNames(this.toolNameObservers.onToolNameRetry, {
|
|
11930
|
+
api: "anthropic-messages",
|
|
11931
|
+
outcome: "failed"
|
|
11932
|
+
});
|
|
11933
|
+
throw error;
|
|
11934
|
+
}
|
|
11935
|
+
observeToolNames(this.toolNameObservers.onToolNameRetry, {
|
|
11936
|
+
api: "anthropic-messages",
|
|
11937
|
+
outcome: "completed"
|
|
11938
|
+
});
|
|
11939
|
+
this.rejectedToolNames.rememberRejection(
|
|
11940
|
+
"anthropic-messages",
|
|
11941
|
+
guardedToolNames.body,
|
|
11942
|
+
guardedToolNames.aliases,
|
|
11943
|
+
parseInvalidRequestText(detail)
|
|
11944
|
+
);
|
|
11945
|
+
return resent;
|
|
11946
|
+
}
|
|
11213
11947
|
const contextExceeded = response.status === 400 && /prompt is too long|context_length_exceeded|exceeds.*context window/i.test(detail);
|
|
11214
11948
|
let providerError;
|
|
11215
11949
|
try {
|
|
@@ -11235,6 +11969,8 @@ var AnthropicMessagesModel = class {
|
|
|
11235
11969
|
);
|
|
11236
11970
|
}
|
|
11237
11971
|
this.previousRequestId = response.headers.get("request-id") ?? void 0;
|
|
11972
|
+
if (guardedToolNames.aliases.size > 0)
|
|
11973
|
+
this.toolNameAliases.set(response, guardedToolNames.aliases);
|
|
11238
11974
|
return response;
|
|
11239
11975
|
}
|
|
11240
11976
|
async getResponse(request) {
|
|
@@ -11247,7 +11983,7 @@ var AnthropicMessagesModel = class {
|
|
|
11247
11983
|
const current = merged ? pausedTurnRequest(request, merged, names, searchQueries, accountScope) : request;
|
|
11248
11984
|
const response = await this.send(current, false);
|
|
11249
11985
|
requestId = response.headers.get("request-id") ?? void 0;
|
|
11250
|
-
const message = object2(await response.json());
|
|
11986
|
+
const message = object2(this.restoredToolNames(response, await response.json()));
|
|
11251
11987
|
if (message.stop_reason === "refusal")
|
|
11252
11988
|
throw new AnthropicProviderRejection("content_policy_violation", 200, requestId);
|
|
11253
11989
|
merged = mergePausedMessage(merged, message);
|
|
@@ -11342,11 +12078,12 @@ var AnthropicMessagesModel = class {
|
|
|
11342
12078
|
partial.blocks = blocks;
|
|
11343
12079
|
let message;
|
|
11344
12080
|
let finalDelta = false;
|
|
11345
|
-
for await (const
|
|
12081
|
+
for await (const streamed of anthropicSse(
|
|
11346
12082
|
response.body,
|
|
11347
12083
|
this.provider.anthropic?.streamIdleTimeoutMs ?? 6e5,
|
|
11348
12084
|
request.signal
|
|
11349
12085
|
)) {
|
|
12086
|
+
const event = this.restoredToolNames(response, streamed);
|
|
11350
12087
|
request.signal?.throwIfAborted();
|
|
11351
12088
|
if (event.type === "error") {
|
|
11352
12089
|
const kind = event.error?.type;
|
|
@@ -12092,7 +12829,9 @@ import { modelEndpointDeployment } from "@opengeni/contracts";
|
|
|
12092
12829
|
|
|
12093
12830
|
// src/replayable-json-body.ts
|
|
12094
12831
|
import OpenAI6 from "openai";
|
|
12832
|
+
import { Stream } from "openai/core/streaming";
|
|
12095
12833
|
import { types as utilTypes } from "util";
|
|
12834
|
+
import { CODEX_REQUEST_ID_HEADER } from "@opengeni/codex";
|
|
12096
12835
|
import { hasResponsesTurn, markResponsesPrefixReady } from "@opengeni/network/responses-websocket";
|
|
12097
12836
|
import {
|
|
12098
12837
|
createJsonSafeSummary,
|
|
@@ -12109,6 +12848,81 @@ var replayableBodySources = /* @__PURE__ */ new WeakMap();
|
|
|
12109
12848
|
function replayableJsonBodySource(body) {
|
|
12110
12849
|
return body !== null && typeof body === "object" && replayableBodySources.has(body) ? { source: replayableBodySources.get(body) } : null;
|
|
12111
12850
|
}
|
|
12851
|
+
var toolNameRequests = /* @__PURE__ */ new WeakMap();
|
|
12852
|
+
function toolNameApi(path) {
|
|
12853
|
+
const pathname = path.split("?", 1)[0] ?? path;
|
|
12854
|
+
if (pathname.endsWith("/responses")) return "responses";
|
|
12855
|
+
if (pathname.endsWith("/chat/completions")) return "chat";
|
|
12856
|
+
return null;
|
|
12857
|
+
}
|
|
12858
|
+
function withJsonBody(init, body) {
|
|
12859
|
+
const replay = new ReplayableJsonBody(body);
|
|
12860
|
+
return {
|
|
12861
|
+
...init,
|
|
12862
|
+
body: replay.createStream(),
|
|
12863
|
+
[REPLAYABLE_REQUEST_BODY_FACTORY]: () => replay.createStream()
|
|
12864
|
+
};
|
|
12865
|
+
}
|
|
12866
|
+
var REQUEST_IDENTITY_HEADERS = ["idempotency-key", CODEX_REQUEST_ID_HEADER];
|
|
12867
|
+
function withStrictResend(init, body) {
|
|
12868
|
+
const next = withJsonBody(init, body);
|
|
12869
|
+
if (init?.headers === void 0) return next;
|
|
12870
|
+
const headers = new Headers(init.headers);
|
|
12871
|
+
for (const name of REQUEST_IDENTITY_HEADERS) {
|
|
12872
|
+
const value = headers.get(name);
|
|
12873
|
+
if (value !== null) headers.set(name, `${value}:tool-names`);
|
|
12874
|
+
}
|
|
12875
|
+
return { ...next, headers };
|
|
12876
|
+
}
|
|
12877
|
+
function toolNameRetryFetch(inner, onRetry) {
|
|
12878
|
+
return async (input, init) => {
|
|
12879
|
+
const source = replayableJsonBodySource(init?.body)?.source;
|
|
12880
|
+
const names = typeof source === "object" && source !== null ? toolNameRequests.get(source) : void 0;
|
|
12881
|
+
if (!names) return await inner(input, init);
|
|
12882
|
+
if (names.strict) return await inner(input, withStrictResend(init, names.strict));
|
|
12883
|
+
const response = await inner(input, init);
|
|
12884
|
+
const rejection = await toolNameRejectionOfResponse(response);
|
|
12885
|
+
if (rejection === null) return response;
|
|
12886
|
+
const sentAliases = names.aliases;
|
|
12887
|
+
const strict = guardRequestToolNames(names.api, names.original, { strict: true });
|
|
12888
|
+
if (strict.body === names.original) return response;
|
|
12889
|
+
names.strict = strict.body;
|
|
12890
|
+
names.aliases = strict.aliases;
|
|
12891
|
+
await response.body?.cancel().catch(() => void 0);
|
|
12892
|
+
let resent;
|
|
12893
|
+
try {
|
|
12894
|
+
resent = await inner(input, withStrictResend(init, strict.body));
|
|
12895
|
+
} catch (error) {
|
|
12896
|
+
observeToolNames(onRetry, { api: names.api, outcome: "failed" });
|
|
12897
|
+
throw error;
|
|
12898
|
+
}
|
|
12899
|
+
if (resent.ok && typeof source === "object" && source !== null) {
|
|
12900
|
+
names.rejected.rememberRejection(
|
|
12901
|
+
names.api,
|
|
12902
|
+
source,
|
|
12903
|
+
sentAliases,
|
|
12904
|
+
rejection
|
|
12905
|
+
);
|
|
12906
|
+
}
|
|
12907
|
+
observeToolNames(onRetry, { api: names.api, outcome: resent.ok ? "completed" : "failed" });
|
|
12908
|
+
return resent;
|
|
12909
|
+
};
|
|
12910
|
+
}
|
|
12911
|
+
async function* restoredStreamEvents(stream, names) {
|
|
12912
|
+
for await (const event of stream) yield restoreToolNames(names.api, event, names.aliases);
|
|
12913
|
+
}
|
|
12914
|
+
function restoredAnswer(data, names, client) {
|
|
12915
|
+
if (!names || names.aliases.size === 0) return data;
|
|
12916
|
+
if (data instanceof Stream) {
|
|
12917
|
+
const stream = data;
|
|
12918
|
+
return new Stream(
|
|
12919
|
+
() => restoredStreamEvents(stream, names),
|
|
12920
|
+
stream.controller,
|
|
12921
|
+
client
|
|
12922
|
+
);
|
|
12923
|
+
}
|
|
12924
|
+
return restoreToolNames(names.api, data, names.aliases);
|
|
12925
|
+
}
|
|
12112
12926
|
var ReplayableJsonBody = class {
|
|
12113
12927
|
source;
|
|
12114
12928
|
constructor(source) {
|
|
@@ -12142,12 +12956,19 @@ var ReplayableJsonBody = class {
|
|
|
12142
12956
|
};
|
|
12143
12957
|
var ReplayableJsonOpenAI = class extends OpenAI6 {
|
|
12144
12958
|
modelRequestPolicy;
|
|
12959
|
+
onToolNameRepair;
|
|
12960
|
+
/** Names this client's route rejected (see RejectedToolNames). */
|
|
12961
|
+
rejectedToolNames = new RejectedToolNames();
|
|
12145
12962
|
constructor(options, hooks = {}) {
|
|
12146
12963
|
super({
|
|
12147
12964
|
...options,
|
|
12148
|
-
fetch:
|
|
12965
|
+
fetch: toolNameRetryFetch(
|
|
12966
|
+
sdkModelTransportAdmissionFetch(options?.fetch ?? globalThis.fetch),
|
|
12967
|
+
hooks.onToolNameRetry
|
|
12968
|
+
)
|
|
12149
12969
|
});
|
|
12150
12970
|
this.modelRequestPolicy = hooks.modelRequestPolicy;
|
|
12971
|
+
this.onToolNameRepair = hooks.onToolNameRepair;
|
|
12151
12972
|
}
|
|
12152
12973
|
makeStatusError(status, error, message, headers) {
|
|
12153
12974
|
rethrowModelTransportAdmissionRefusal(headers);
|
|
@@ -12157,6 +12978,7 @@ var ReplayableJsonOpenAI = class extends OpenAI6 {
|
|
|
12157
12978
|
if (!isModelRequestPath(path) || opts === void 0) {
|
|
12158
12979
|
return super.post(path, opts);
|
|
12159
12980
|
}
|
|
12981
|
+
let names;
|
|
12160
12982
|
const wrapped = Promise.resolve(opts).then((resolved) => {
|
|
12161
12983
|
const body = resolved.body;
|
|
12162
12984
|
let prepared = resolved;
|
|
@@ -12170,13 +12992,27 @@ var ReplayableJsonOpenAI = class extends OpenAI6 {
|
|
|
12170
12992
|
};
|
|
12171
12993
|
}
|
|
12172
12994
|
}
|
|
12995
|
+
const api = toolNameApi(path);
|
|
12996
|
+
if (api && isJsonRecordBody(prepared.body)) {
|
|
12997
|
+
const original = prepared.body;
|
|
12998
|
+
const rejected = this.rejectedToolNames;
|
|
12999
|
+
const guarded = guardRequestToolNames(api, original, {
|
|
13000
|
+
rejected: rejected.forModel(original.model)
|
|
13001
|
+
});
|
|
13002
|
+
for (const field2 of guarded.repaired) {
|
|
13003
|
+
observeToolNames(this.onToolNameRepair, { api, field: field2 });
|
|
13004
|
+
}
|
|
13005
|
+
names = { api, original, aliases: guarded.aliases, rejected };
|
|
13006
|
+
toolNameRequests.set(guarded.body, names);
|
|
13007
|
+
if (guarded.body !== original) prepared = { ...prepared, body: guarded.body };
|
|
13008
|
+
}
|
|
12173
13009
|
if (path.split("?", 1)[0]?.endsWith("/responses")) {
|
|
12174
13010
|
markResponsesPrefixReady();
|
|
12175
13011
|
if (hasResponsesTurn()) prepared = { ...prepared, maxRetries: 0 };
|
|
12176
13012
|
}
|
|
12177
13013
|
return wrapJsonRequestOptions(prepared);
|
|
12178
13014
|
});
|
|
12179
|
-
return super.post(path, wrapped);
|
|
13015
|
+
return super.post(path, wrapped)._thenUnwrap((data) => restoredAnswer(data, names, this));
|
|
12180
13016
|
}
|
|
12181
13017
|
async buildRequest(inputOptions, context) {
|
|
12182
13018
|
const replayable = inputOptions.body instanceof ReplayableJsonBody ? inputOptions.body : void 0;
|
|
@@ -12517,21 +13353,21 @@ function hostedToolRoute(type) {
|
|
|
12517
13353
|
if (type === "image_generation") return "image_generation.openai_hosted";
|
|
12518
13354
|
return null;
|
|
12519
13355
|
}
|
|
12520
|
-
function
|
|
13356
|
+
function isRecord8(value) {
|
|
12521
13357
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
12522
13358
|
}
|
|
12523
13359
|
function clientExecutedTool(tool2) {
|
|
12524
|
-
if (!
|
|
13360
|
+
if (!isRecord8(tool2)) return false;
|
|
12525
13361
|
const type = tool2.type;
|
|
12526
13362
|
if (type === void 0) {
|
|
12527
|
-
return typeof tool2.name === "string" &&
|
|
13363
|
+
return typeof tool2.name === "string" && isRecord8(tool2.input_schema);
|
|
12528
13364
|
}
|
|
12529
13365
|
if (typeof type !== "string") return false;
|
|
12530
13366
|
if (["function", "custom", "local_shell", "apply_patch"].includes(type)) return true;
|
|
12531
13367
|
if (/^(computer|bash_|text_editor_|memory_)/u.test(type)) return true;
|
|
12532
13368
|
if (type === "tool_search") return tool2.execution === "client";
|
|
12533
13369
|
if (type === "shell") {
|
|
12534
|
-
return tool2.environment === void 0 || tool2.environment === null ||
|
|
13370
|
+
return tool2.environment === void 0 || tool2.environment === null || isRecord8(tool2.environment) && tool2.environment.type === "local";
|
|
12535
13371
|
}
|
|
12536
13372
|
if (type === "namespace") {
|
|
12537
13373
|
return Array.isArray(tool2.tools) && tool2.tools.every(clientExecutedTool);
|
|
@@ -12539,7 +13375,7 @@ function clientExecutedTool(tool2) {
|
|
|
12539
13375
|
return false;
|
|
12540
13376
|
}
|
|
12541
13377
|
function toolLabel(tool2) {
|
|
12542
|
-
if (!
|
|
13378
|
+
if (!isRecord8(tool2)) return "tool";
|
|
12543
13379
|
if (typeof tool2.type === "string") return tool2.type;
|
|
12544
13380
|
const [key] = Object.keys(tool2);
|
|
12545
13381
|
return key ?? "tool";
|
|
@@ -12743,7 +13579,7 @@ import { OPENGENI_GATEWAY_MODELS, gatewayRequestPolicyForUpstreamModel } from "@
|
|
|
12743
13579
|
import {
|
|
12744
13580
|
CODEX_REQUEST_BODY_NORMALIZED_HEADER,
|
|
12745
13581
|
CODEX_REQUEST_CALLER_STREAM_HEADER,
|
|
12746
|
-
CODEX_REQUEST_ID_HEADER,
|
|
13582
|
+
CODEX_REQUEST_ID_HEADER as CODEX_REQUEST_ID_HEADER2,
|
|
12747
13583
|
CODEX_REQUEST_MODEL_HEADER,
|
|
12748
13584
|
codexRequestStorage,
|
|
12749
13585
|
normalizedCodexRequestBody,
|
|
@@ -13062,7 +13898,7 @@ function modelRequestPolicyForProvider(provider, gatewayPolicies, opperOutputLim
|
|
|
13062
13898
|
[CODEX_REQUEST_BODY_NORMALIZED_HEADER]: "1",
|
|
13063
13899
|
[CODEX_REQUEST_CALLER_STREAM_HEADER]: callerWantsStream ? "1" : "0",
|
|
13064
13900
|
[CODEX_REQUEST_MODEL_HEADER]: typeof normalizedBody.model === "string" ? normalizedBody.model : fallbackModel,
|
|
13065
|
-
[
|
|
13901
|
+
[CODEX_REQUEST_ID_HEADER2]: requestId
|
|
13066
13902
|
}
|
|
13067
13903
|
};
|
|
13068
13904
|
}
|
|
@@ -13591,7 +14427,7 @@ function providerQuotaExhaustedMessage(scope) {
|
|
|
13591
14427
|
}
|
|
13592
14428
|
}
|
|
13593
14429
|
var MAX_RESPONSE_BODY_BYTES = 64 * 1024;
|
|
13594
|
-
async function
|
|
14430
|
+
async function boundedCloneText2(response, maxBytes) {
|
|
13595
14431
|
const body = response.clone().body;
|
|
13596
14432
|
if (!body) return "";
|
|
13597
14433
|
const reader = body.getReader();
|
|
@@ -13690,7 +14526,7 @@ function classifyProviderQuotaError(error, nowMs = Date.now()) {
|
|
|
13690
14526
|
}
|
|
13691
14527
|
async function classifyProviderQuotaResponse(response, nowMs = Date.now()) {
|
|
13692
14528
|
if (response.status !== 429) return null;
|
|
13693
|
-
const text2 = await
|
|
14529
|
+
const text2 = await boundedCloneText2(response, MAX_RESPONSE_BODY_BYTES);
|
|
13694
14530
|
if (text2 === null) return null;
|
|
13695
14531
|
let json;
|
|
13696
14532
|
try {
|
|
@@ -13759,54 +14595,16 @@ import {
|
|
|
13759
14595
|
XAI_RESPONSE_SDK_OUTER_TIMEOUT_MS,
|
|
13760
14596
|
xaiSubscriptionFetch
|
|
13761
14597
|
} from "@opengeni/xai-subscription";
|
|
13762
|
-
|
|
13763
|
-
// src/provider-invalid-request.ts
|
|
13764
|
-
var MAX_BODY_CHARACTERS = 64 * 1024;
|
|
13765
|
-
var MAX_PATH_CHARACTERS = 96;
|
|
13766
|
-
var PATH_CHARACTERS = /^[A-Za-z_][A-Za-z0-9_.[\]*]*$/;
|
|
13767
|
-
var CODE = /^[A-Za-z][A-Za-z0-9_.-]{0,63}$/;
|
|
13768
|
-
var EMBEDDED_PATH = /\b((?:input|messages|tools|contents|system|instructions)(?:\[\d+\]|\.\d+|\.[A-Za-z_]\w*)+)/;
|
|
13769
|
-
function record2(value) {
|
|
13770
|
-
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
13771
|
-
}
|
|
13772
|
-
function normalizedPath(raw) {
|
|
13773
|
-
const path = raw.trim().replace(/\[\d+\]/g, "[]").replace(/\.\d+(?=\.|\[|$)/g, "[]").replace(/\.properties\.[A-Za-z0-9_-]+/g, ".properties.*");
|
|
13774
|
-
return path.length <= MAX_PATH_CHARACTERS && PATH_CHARACTERS.test(path) ? path : null;
|
|
13775
|
-
}
|
|
13776
|
-
function errorCode(error) {
|
|
13777
|
-
for (const candidate of [error.code, error.type]) {
|
|
13778
|
-
if (typeof candidate === "string" && CODE.test(candidate)) return candidate;
|
|
13779
|
-
}
|
|
13780
|
-
return "unknown";
|
|
13781
|
-
}
|
|
13782
|
-
function invalidRequestSignature(body) {
|
|
13783
|
-
const envelope = record2(body);
|
|
13784
|
-
const error = record2(envelope?.error) ?? envelope ?? {};
|
|
13785
|
-
const message = typeof error.message === "string" ? error.message : "";
|
|
13786
|
-
const raw = typeof error.param === "string" && error.param || EMBEDDED_PATH.exec(message)?.[1] || null;
|
|
13787
|
-
return { path: raw && normalizedPath(raw) || "unknown", code: errorCode(error) };
|
|
13788
|
-
}
|
|
13789
|
-
async function invalidRequestSignatureOfResponse(response) {
|
|
13790
|
-
try {
|
|
13791
|
-
const text2 = await response.clone().text();
|
|
13792
|
-
if (text2.length > MAX_BODY_CHARACTERS) return { path: "unknown", code: "unknown" };
|
|
13793
|
-
let body;
|
|
13794
|
-
try {
|
|
13795
|
-
body = JSON.parse(text2);
|
|
13796
|
-
} catch {
|
|
13797
|
-
body = { message: text2 };
|
|
13798
|
-
}
|
|
13799
|
-
return invalidRequestSignature(body);
|
|
13800
|
-
} catch {
|
|
13801
|
-
return null;
|
|
13802
|
-
}
|
|
13803
|
-
}
|
|
13804
|
-
|
|
13805
|
-
// src/model-provider-client.ts
|
|
13806
14598
|
var runtimeMetricsHooks = null;
|
|
13807
14599
|
function configureRuntimeMetricsHooks(hooks) {
|
|
13808
14600
|
runtimeMetricsHooks = hooks ?? null;
|
|
13809
14601
|
}
|
|
14602
|
+
function modelToolNameObservers(provider) {
|
|
14603
|
+
return {
|
|
14604
|
+
onToolNameRepair: ({ api, field: field2 }) => runtimeMetricsHooks?.onModelToolNameRepair?.({ provider, api, field: field2 }),
|
|
14605
|
+
onToolNameRetry: ({ api, outcome }) => runtimeMetricsHooks?.onModelToolNameRetry?.({ provider, api, outcome })
|
|
14606
|
+
};
|
|
14607
|
+
}
|
|
13810
14608
|
function recordRuntimeMcpToolCallMetric(outcome, tool2, startedAt) {
|
|
13811
14609
|
const durationSeconds = Math.max(0, (performance.now() - startedAt) / 1e3);
|
|
13812
14610
|
try {
|
|
@@ -13857,6 +14655,7 @@ function buildOpenAIClientFromSettings(settings, providerId = settings.openaiPro
|
|
|
13857
14655
|
)
|
|
13858
14656
|
},
|
|
13859
14657
|
{
|
|
14658
|
+
...modelToolNameObservers(providerId),
|
|
13860
14659
|
modelRequestPolicy: (request) => chatModelRequestPolicy(request) ?? azureModelRequestPolicy(request)
|
|
13861
14660
|
}
|
|
13862
14661
|
);
|
|
@@ -13881,6 +14680,7 @@ function buildOpenAIClientFromSettings(settings, providerId = settings.openaiPro
|
|
|
13881
14680
|
)
|
|
13882
14681
|
},
|
|
13883
14682
|
{
|
|
14683
|
+
...modelToolNameObservers(providerId),
|
|
13884
14684
|
// Only the OpenAI platform; a compatible server keeps its own contract.
|
|
13885
14685
|
modelRequestPolicy: isDirectOpenAiApiBaseUrl(settings.openaiBaseUrl) ? withStorelessResponses(chatModelRequestPolicy) : chatModelRequestPolicy
|
|
13886
14686
|
}
|
|
@@ -14185,6 +14985,7 @@ function buildProviderClient(provider, settings) {
|
|
|
14185
14985
|
)
|
|
14186
14986
|
},
|
|
14187
14987
|
{
|
|
14988
|
+
...modelToolNameObservers(provider.id),
|
|
14188
14989
|
modelRequestPolicy: modelRequestPolicyForProvider(
|
|
14189
14990
|
provider,
|
|
14190
14991
|
gatewayPolicies,
|
|
@@ -14211,6 +15012,7 @@ function buildProviderClient(provider, settings) {
|
|
|
14211
15012
|
)
|
|
14212
15013
|
},
|
|
14213
15014
|
{
|
|
15015
|
+
...modelToolNameObservers(provider.id),
|
|
14214
15016
|
modelRequestPolicy: modelRequestPolicyForProvider(
|
|
14215
15017
|
provider,
|
|
14216
15018
|
gatewayPolicies,
|
|
@@ -14276,6 +15078,7 @@ function buildProviderClient(provider, settings) {
|
|
|
14276
15078
|
)
|
|
14277
15079
|
},
|
|
14278
15080
|
{
|
|
15081
|
+
...modelToolNameObservers(provider.id),
|
|
14279
15082
|
modelRequestPolicy: modelRequestPolicyForProvider(
|
|
14280
15083
|
provider,
|
|
14281
15084
|
gatewayPolicies,
|
|
@@ -15641,7 +16444,7 @@ function toolGroups(tools, serverIds) {
|
|
|
15641
16444
|
}
|
|
15642
16445
|
|
|
15643
16446
|
// src/deferred-tool-arguments.ts
|
|
15644
|
-
import { isRecord as
|
|
16447
|
+
import { isRecord as isRecord9 } from "@opengeni/contracts/values";
|
|
15645
16448
|
var NON_STRING_TYPES = /* @__PURE__ */ new Set(["integer", "number", "boolean", "null", "array", "object"]);
|
|
15646
16449
|
var MAX_DEPTH = 8;
|
|
15647
16450
|
function parseFiniteJson(text2) {
|
|
@@ -15653,7 +16456,7 @@ function parseFiniteJson(text2) {
|
|
|
15653
16456
|
});
|
|
15654
16457
|
}
|
|
15655
16458
|
function declaredTypes(schema) {
|
|
15656
|
-
if (!
|
|
16459
|
+
if (!isRecord9(schema)) return null;
|
|
15657
16460
|
const types = /* @__PURE__ */ new Set();
|
|
15658
16461
|
const type = schema.type;
|
|
15659
16462
|
if (typeof type === "string") types.add(type);
|
|
@@ -15701,11 +16504,11 @@ function coerceValue(value, schema, depth) {
|
|
|
15701
16504
|
}
|
|
15702
16505
|
return matchesAny(parsed, types) ? parsed : value;
|
|
15703
16506
|
}
|
|
15704
|
-
if (
|
|
16507
|
+
if (isRecord9(value)) return coerceObject(value, schema, depth + 1);
|
|
15705
16508
|
return value;
|
|
15706
16509
|
}
|
|
15707
16510
|
function coerceObject(value, schema, depth) {
|
|
15708
|
-
if (!
|
|
16511
|
+
if (!isRecord9(schema) || !isRecord9(schema.properties)) return value;
|
|
15709
16512
|
const properties = schema.properties;
|
|
15710
16513
|
let changed = false;
|
|
15711
16514
|
const next = /* @__PURE__ */ Object.create(null);
|
|
@@ -15724,13 +16527,13 @@ function restoreDeferredToolArgumentTypes(argumentsText, parameters) {
|
|
|
15724
16527
|
} catch {
|
|
15725
16528
|
return null;
|
|
15726
16529
|
}
|
|
15727
|
-
if (!
|
|
16530
|
+
if (!isRecord9(parsed)) return null;
|
|
15728
16531
|
const coerced = coerceObject(parsed, parameters, 0);
|
|
15729
16532
|
return coerced === parsed ? null : JSON.stringify(coerced);
|
|
15730
16533
|
}
|
|
15731
16534
|
|
|
15732
16535
|
// src/lazy-tool-transport.ts
|
|
15733
|
-
import { isRecord as
|
|
16536
|
+
import { isRecord as isRecord10 } from "@opengeni/contracts/values";
|
|
15734
16537
|
var TOOL_SEARCH_NAME = "tool_search";
|
|
15735
16538
|
var TOOL_LIST_NAME = "tool_list";
|
|
15736
16539
|
var TOOL_INVOKE_NAME = "tool_invoke";
|
|
@@ -15786,8 +16589,8 @@ var INTERNAL_REGISTRATION_TOOL_MARKER_KEY = "opengeni.internal_lazy_registration
|
|
|
15786
16589
|
var OPENING_TOOL_LOAD_MARKER_KEY = "opengeni.opening_tool_load.v1";
|
|
15787
16590
|
var OPENING_TOOL_LOAD_CALL_ID = "call_opening_tools";
|
|
15788
16591
|
function isOpeningToolLoadResponse(value) {
|
|
15789
|
-
const response =
|
|
15790
|
-
return
|
|
16592
|
+
const response = isRecord10(value) && value.type === "response_done" && isRecord10(value.response) ? value.response : value;
|
|
16593
|
+
return isRecord10(response) && isRecord10(response.providerData) && response.providerData[OPENING_TOOL_LOAD_MARKER_KEY] === true;
|
|
15791
16594
|
}
|
|
15792
16595
|
var INTERNAL_DISPATCH_REGISTRATION_CALL_PREFIX = "opengeni:lazy-dispatch:register:";
|
|
15793
16596
|
var SEARCH_DESCRIPTION = "Find currently authorized tools by capability. Describe the action and resource you need; separate independent tasks with semicolons and include useful context. Loads the strongest match and returns compact alternatives; ranking is not certainty, so check the match. If a decision model is configured, it can select tools for ambiguous searches. With schemasLoaded:true, only tools have loaded schemas; suggestions are unloaded alternatives. With schemasLoaded:false, load chosen exact names with query:'' before invoking. Known exact names bypass ranking and inference. Call loaded tools by name or through tool_invoke. Search is not exhaustive: tool_list view:'groups' shows available groups. Discovery never grants access.";
|
|
@@ -15881,22 +16684,22 @@ function alreadyAvailableNotice(names) {
|
|
|
15881
16684
|
return names.length === 1 ? `${names[0]} is already in your tool list; call it directly.` : `${names.join(", ")} are already in your tool list; call them directly.`;
|
|
15882
16685
|
}
|
|
15883
16686
|
function connectableIntegrationsFromCatalogResult(result) {
|
|
15884
|
-
const content = Array.isArray(result) ? result :
|
|
15885
|
-
if (
|
|
16687
|
+
const content = Array.isArray(result) ? result : isRecord10(result) && Array.isArray(result.content) ? result.content : [];
|
|
16688
|
+
if (isRecord10(result) && result.isError === true) return [];
|
|
15886
16689
|
const found = [];
|
|
15887
16690
|
for (const block of content) {
|
|
15888
|
-
if (!
|
|
16691
|
+
if (!isRecord10(block) || block.type !== "text" || typeof block.text !== "string") continue;
|
|
15889
16692
|
let parsed;
|
|
15890
16693
|
try {
|
|
15891
16694
|
parsed = JSON.parse(block.text);
|
|
15892
16695
|
} catch {
|
|
15893
16696
|
continue;
|
|
15894
16697
|
}
|
|
15895
|
-
if (!
|
|
16698
|
+
if (!isRecord10(parsed) || !Array.isArray(parsed.matches)) continue;
|
|
15896
16699
|
for (const match of parsed.matches) {
|
|
15897
|
-
if (!
|
|
15898
|
-
const setup =
|
|
15899
|
-
const next = setup &&
|
|
16700
|
+
if (!isRecord10(match) || match.approximate === true) continue;
|
|
16701
|
+
const setup = isRecord10(match.setup) ? match.setup : null;
|
|
16702
|
+
const next = setup && isRecord10(setup.nextAction) ? setup.nextAction : null;
|
|
15900
16703
|
if (next?.toolName !== "capability_authorization_request") continue;
|
|
15901
16704
|
if (setup.action === "enable") continue;
|
|
15902
16705
|
if (typeof match.capabilityId !== "string" || typeof match.name !== "string") continue;
|
|
@@ -15925,7 +16728,7 @@ function isFunctionTool(tool2) {
|
|
|
15925
16728
|
function parseJsonObject(value) {
|
|
15926
16729
|
try {
|
|
15927
16730
|
const parsed = JSON.parse(value);
|
|
15928
|
-
return
|
|
16731
|
+
return isRecord10(parsed) ? parsed : null;
|
|
15929
16732
|
} catch {
|
|
15930
16733
|
return null;
|
|
15931
16734
|
}
|
|
@@ -15941,7 +16744,7 @@ function modelVisibleToolDefinition(tool2) {
|
|
|
15941
16744
|
};
|
|
15942
16745
|
}
|
|
15943
16746
|
function unavailableToolResult(input) {
|
|
15944
|
-
const name =
|
|
16747
|
+
const name = isRecord10(input) && typeof input.name === "string" ? input.name : null;
|
|
15945
16748
|
return JSON.stringify({
|
|
15946
16749
|
ok: false,
|
|
15947
16750
|
error: {
|
|
@@ -16748,7 +17551,7 @@ ${result.notice}`
|
|
|
16748
17551
|
strict: false,
|
|
16749
17552
|
execute: async (input) => {
|
|
16750
17553
|
await this.ensurePrepared();
|
|
16751
|
-
const args =
|
|
17554
|
+
const args = isRecord10(input) ? input : {};
|
|
16752
17555
|
const prefix = typeof args.namePrefix === "string" ? args.namePrefix : "";
|
|
16753
17556
|
const group = typeof args.group === "string" ? args.group : void 0;
|
|
16754
17557
|
const serverIds = this.modelMcpServerIds();
|
|
@@ -16908,7 +17711,7 @@ ${result.notice}`
|
|
|
16908
17711
|
function createResolveMissingFunctionTool(runtime) {
|
|
16909
17712
|
return async ({ name, toolCall }) => {
|
|
16910
17713
|
const names = [name];
|
|
16911
|
-
if (
|
|
17714
|
+
if (isRecord10(toolCall) && typeof toolCall.name === "string" && toolCall.name !== name) {
|
|
16912
17715
|
names.push(toolCall.name);
|
|
16913
17716
|
}
|
|
16914
17717
|
for (const candidate of names) {
|
|
@@ -16976,7 +17779,7 @@ function installLazyToolRuntimeOnAgent(agent, runtime) {
|
|
|
16976
17779
|
}
|
|
16977
17780
|
}
|
|
16978
17781
|
function restoredProviderData(providerData) {
|
|
16979
|
-
if (!
|
|
17782
|
+
if (!isRecord10(providerData)) return void 0;
|
|
16980
17783
|
const restored = { ...providerData };
|
|
16981
17784
|
delete restored[DISPATCH_MARKER_KEY];
|
|
16982
17785
|
delete restored[SEARCH_MARKER_KEY];
|
|
@@ -16999,13 +17802,13 @@ function restoreGenericDispatchHistory(input) {
|
|
|
16999
17802
|
return changed ? restoredInput : input;
|
|
17000
17803
|
}
|
|
17001
17804
|
function restoreGenericDispatchHistoryItem(candidate) {
|
|
17002
|
-
if (!
|
|
17805
|
+
if (!isRecord10(candidate) || candidate.type !== "function_call" && candidate.type !== "tool_search_call")
|
|
17003
17806
|
return candidate;
|
|
17004
|
-
const providerData =
|
|
17807
|
+
const providerData = isRecord10(candidate.providerData) ? candidate.providerData : void 0;
|
|
17005
17808
|
const marker = providerData?.[DISPATCH_MARKER_KEY];
|
|
17006
|
-
const hasDispatchMarker =
|
|
17809
|
+
const hasDispatchMarker = isRecord10(marker) && marker.version === 1 && typeof marker.arguments === "string";
|
|
17007
17810
|
const searchMarker = providerData?.[SEARCH_MARKER_KEY];
|
|
17008
|
-
const hasSearchMarker =
|
|
17811
|
+
const hasSearchMarker = isRecord10(searchMarker) && (searchMarker.version === 1 || searchMarker.version === 2) && typeof searchMarker.output === "string";
|
|
17009
17812
|
const hasFamilyMarker = providerData !== void 0 && FAMILY_DISCLOSURE_MARKER_KEY in providerData;
|
|
17010
17813
|
if (!hasDispatchMarker && !hasSearchMarker && !hasFamilyMarker) {
|
|
17011
17814
|
return candidate;
|
|
@@ -17023,11 +17826,11 @@ function restoreGenericDispatchHistoryItem(candidate) {
|
|
|
17023
17826
|
function callId2(candidate) {
|
|
17024
17827
|
if (typeof candidate.callId === "string") return candidate.callId;
|
|
17025
17828
|
if (typeof candidate.call_id === "string") return candidate.call_id;
|
|
17026
|
-
const providerData =
|
|
17829
|
+
const providerData = isRecord10(candidate.providerData) ? candidate.providerData : null;
|
|
17027
17830
|
return providerData && typeof providerData.call_id === "string" ? providerData.call_id : null;
|
|
17028
17831
|
}
|
|
17029
17832
|
function isInternalGenericDispatchRegistrationItem(candidate) {
|
|
17030
|
-
if (!
|
|
17833
|
+
if (!isRecord10(candidate)) return false;
|
|
17031
17834
|
if (candidate.type !== "tool_search_call" && candidate.type !== "tool_search_output")
|
|
17032
17835
|
return false;
|
|
17033
17836
|
return callId2(candidate)?.startsWith(INTERNAL_DISPATCH_REGISTRATION_CALL_PREFIX) ?? false;
|
|
@@ -17036,17 +17839,17 @@ function restoreGenericSearchResults(input) {
|
|
|
17036
17839
|
if (!Array.isArray(input)) return input;
|
|
17037
17840
|
const disclosures = /* @__PURE__ */ new Map();
|
|
17038
17841
|
for (const candidate of input) {
|
|
17039
|
-
if (!
|
|
17842
|
+
if (!isRecord10(candidate) || candidate.type !== "function_call") continue;
|
|
17040
17843
|
const id = callId2(candidate);
|
|
17041
|
-
const marker =
|
|
17042
|
-
if (id &&
|
|
17844
|
+
const marker = isRecord10(candidate.providerData) ? candidate.providerData[SEARCH_MARKER_KEY] : void 0;
|
|
17845
|
+
if (id && isRecord10(marker) && (marker.version === 1 || marker.version === 2) && typeof marker.output === "string") {
|
|
17043
17846
|
disclosures.set(id, marker.output);
|
|
17044
17847
|
}
|
|
17045
17848
|
}
|
|
17046
17849
|
if (disclosures.size === 0) return input;
|
|
17047
17850
|
let changed = false;
|
|
17048
17851
|
const restored = input.map((candidate) => {
|
|
17049
|
-
if (!
|
|
17852
|
+
if (!isRecord10(candidate) || candidate.type !== "function_call_result") {
|
|
17050
17853
|
return candidate;
|
|
17051
17854
|
}
|
|
17052
17855
|
const id = callId2(candidate);
|
|
@@ -17061,9 +17864,9 @@ function restoreGenericDispatchHistoryItems(input) {
|
|
|
17061
17864
|
return restoreGenericDispatchHistory(input);
|
|
17062
17865
|
}
|
|
17063
17866
|
function transformGenericDispatchCall(candidate, runtime, preparedSearch) {
|
|
17064
|
-
if (!
|
|
17867
|
+
if (!isRecord10(candidate) || candidate.type !== "function_call") return [candidate];
|
|
17065
17868
|
if (candidate.name === TOOL_SEARCH_NAME && typeof candidate.arguments === "string") {
|
|
17066
|
-
const providerData2 =
|
|
17869
|
+
const providerData2 = isRecord10(candidate.providerData) ? candidate.providerData : {};
|
|
17067
17870
|
if (SEARCH_MARKER_KEY in providerData2) {
|
|
17068
17871
|
throw new Error("Provider function call collided with lazy-search metadata");
|
|
17069
17872
|
}
|
|
@@ -17088,10 +17891,10 @@ function transformGenericDispatchCall(candidate, runtime, preparedSearch) {
|
|
|
17088
17891
|
const name = dispatch2 && typeof dispatch2.name === "string" ? dispatch2.name : null;
|
|
17089
17892
|
const args = dispatch2?.arguments;
|
|
17090
17893
|
const originalCallId = callId2(candidate);
|
|
17091
|
-
if (!name || !
|
|
17894
|
+
if (!name || !isRecord10(args) || !runtime.resolveFunctionTool(name) || !originalCallId) {
|
|
17092
17895
|
return [candidate];
|
|
17093
17896
|
}
|
|
17094
|
-
const providerData =
|
|
17897
|
+
const providerData = isRecord10(candidate.providerData) ? candidate.providerData : {};
|
|
17095
17898
|
if (DISPATCH_MARKER_KEY in providerData) {
|
|
17096
17899
|
throw new Error("Provider function call collided with lazy-dispatch metadata");
|
|
17097
17900
|
}
|
|
@@ -17121,8 +17924,8 @@ function restoreDirectDeferredCallArgumentTypes(candidate, runtime) {
|
|
|
17121
17924
|
return restored === null ? candidate : { ...candidate, arguments: restored };
|
|
17122
17925
|
}
|
|
17123
17926
|
async function transformResponseItem(item, runtime, signal) {
|
|
17124
|
-
if (
|
|
17125
|
-
const providerData =
|
|
17927
|
+
if (isRecord10(item) && item.type === "tool_search_call") {
|
|
17928
|
+
const providerData = isRecord10(item.providerData) ? item.providerData : {};
|
|
17126
17929
|
if ((item.execution ?? providerData.execution) === "client") {
|
|
17127
17930
|
if (SEARCH_MARKER_KEY in providerData)
|
|
17128
17931
|
throw new Error("Provider tool-search call collided with lazy-search metadata");
|
|
@@ -17141,12 +17944,12 @@ async function transformResponseItem(item, runtime, signal) {
|
|
|
17141
17944
|
if (runtime.transport !== "generic_dispatch") {
|
|
17142
17945
|
return [
|
|
17143
17946
|
withFamilyDisclosure(
|
|
17144
|
-
|
|
17947
|
+
isRecord10(item) && item.type === "function_call" ? restoreDirectDeferredCallArgumentTypes(item, runtime) : item,
|
|
17145
17948
|
runtime
|
|
17146
17949
|
)
|
|
17147
17950
|
];
|
|
17148
17951
|
}
|
|
17149
|
-
const search =
|
|
17952
|
+
const search = isRecord10(item) && item.type === "function_call" && item.name === TOOL_SEARCH_NAME && typeof item.arguments === "string";
|
|
17150
17953
|
const items = transformGenericDispatchCall(
|
|
17151
17954
|
item,
|
|
17152
17955
|
runtime,
|
|
@@ -17160,11 +17963,11 @@ async function transformResponseOutput(output, runtime, signal) {
|
|
|
17160
17963
|
return unchanged ? output : transformed;
|
|
17161
17964
|
}
|
|
17162
17965
|
function withFamilyDisclosure(candidate, runtime) {
|
|
17163
|
-
if (!
|
|
17966
|
+
if (!isRecord10(candidate) || candidate.type !== "function_call") return candidate;
|
|
17164
17967
|
if (typeof candidate.name !== "string" || !isInteractionEntryToolName(candidate.name)) {
|
|
17165
17968
|
return candidate;
|
|
17166
17969
|
}
|
|
17167
|
-
const providerData =
|
|
17970
|
+
const providerData = isRecord10(candidate.providerData) ? candidate.providerData : {};
|
|
17168
17971
|
if (FAMILY_DISCLOSURE_MARKER_KEY in providerData) return candidate;
|
|
17169
17972
|
const tools = runtime.familyDisclosure(candidate.name);
|
|
17170
17973
|
if (tools.length === 0) return candidate;
|
|
@@ -17181,18 +17984,18 @@ function withFamilyDisclosure(candidate, runtime) {
|
|
|
17181
17984
|
};
|
|
17182
17985
|
}
|
|
17183
17986
|
function familyDisclosure(candidate) {
|
|
17184
|
-
const providerData =
|
|
17987
|
+
const providerData = isRecord10(candidate.providerData) ? candidate.providerData : null;
|
|
17185
17988
|
const marker = providerData?.[FAMILY_DISCLOSURE_MARKER_KEY];
|
|
17186
|
-
if (!
|
|
17989
|
+
if (!isRecord10(marker) || marker.version !== 1 || !Array.isArray(marker.tools)) return null;
|
|
17187
17990
|
const tools = marker.tools.filter(
|
|
17188
|
-
(tool2) =>
|
|
17991
|
+
(tool2) => isRecord10(tool2) && typeof tool2.name === "string"
|
|
17189
17992
|
);
|
|
17190
17993
|
return tools.length > 0 ? { tools, viaToolInvoke: marker.viaToolInvoke === true } : null;
|
|
17191
17994
|
}
|
|
17192
17995
|
function disclosedToolNames(tools) {
|
|
17193
17996
|
if (!Array.isArray(tools)) return [];
|
|
17194
17997
|
return tools.flatMap(
|
|
17195
|
-
(tool2) =>
|
|
17998
|
+
(tool2) => isRecord10(tool2) && typeof tool2.name === "string" ? [tool2.name] : []
|
|
17196
17999
|
);
|
|
17197
18000
|
}
|
|
17198
18001
|
function genericSearchOutputNames(output) {
|
|
@@ -17234,15 +18037,15 @@ function expandInteractionFamilyDisclosures(input) {
|
|
|
17234
18037
|
for (let index = 0; index < input.length; index += 1) {
|
|
17235
18038
|
const original = input[index];
|
|
17236
18039
|
const candidate = original;
|
|
17237
|
-
const result =
|
|
18040
|
+
const result = isRecord10(candidate) && STEP_RESULT_ITEM_TYPES.has(String(candidate.type));
|
|
17238
18041
|
if (!result) flush(index);
|
|
17239
18042
|
expanded?.push(original);
|
|
17240
|
-
if (!
|
|
18043
|
+
if (!isRecord10(candidate)) continue;
|
|
17241
18044
|
if (candidate.type === "tool_search_call") {
|
|
17242
|
-
const providerData =
|
|
18045
|
+
const providerData = isRecord10(candidate.providerData) ? candidate.providerData : null;
|
|
17243
18046
|
const searchMarker = providerData?.[SEARCH_MARKER_KEY];
|
|
17244
18047
|
const id2 = callId2(candidate);
|
|
17245
|
-
if (id2 &&
|
|
18048
|
+
if (id2 && isRecord10(searchMarker) && typeof searchMarker.notice === "string")
|
|
17246
18049
|
pendingNotices.set(id2, searchMarker.notice);
|
|
17247
18050
|
continue;
|
|
17248
18051
|
}
|
|
@@ -17258,15 +18061,15 @@ function expandInteractionFamilyDisclosures(input) {
|
|
|
17258
18061
|
continue;
|
|
17259
18062
|
}
|
|
17260
18063
|
if (candidate.type === "function_call") {
|
|
17261
|
-
const providerData =
|
|
18064
|
+
const providerData = isRecord10(candidate.providerData) ? candidate.providerData : null;
|
|
17262
18065
|
const searchMarker = providerData?.[SEARCH_MARKER_KEY];
|
|
17263
|
-
if (
|
|
18066
|
+
if (isRecord10(searchMarker) && typeof searchMarker.output === "string") {
|
|
17264
18067
|
if (searchMarker.version === 1) {
|
|
17265
18068
|
for (const name of genericSearchOutputNames(searchMarker.output)) disclosed.add(name);
|
|
17266
18069
|
} else if (searchMarker.version === 2 && Array.isArray(searchMarker.tools)) {
|
|
17267
18070
|
const id3 = callId2(candidate);
|
|
17268
18071
|
if (id3)
|
|
17269
|
-
pending.set(id3, { tools: searchMarker.tools.filter(
|
|
18072
|
+
pending.set(id3, { tools: searchMarker.tools.filter(isRecord10), viaToolInvoke: true });
|
|
17270
18073
|
}
|
|
17271
18074
|
}
|
|
17272
18075
|
const id2 = callId2(candidate);
|
|
@@ -17345,14 +18148,14 @@ function startsTurn(input) {
|
|
|
17345
18148
|
const items = input;
|
|
17346
18149
|
let lastUser = -1;
|
|
17347
18150
|
items.forEach((item, index) => {
|
|
17348
|
-
if (
|
|
18151
|
+
if (isRecord10(item) && item.role === "user") lastUser = index;
|
|
17349
18152
|
});
|
|
17350
|
-
return lastUser >= 0 && items.slice(lastUser + 1).every((item) =>
|
|
18153
|
+
return lastUser >= 0 && items.slice(lastUser + 1).every((item) => isRecord10(item) && (item.role === "developer" || item.role === "system"));
|
|
17351
18154
|
}
|
|
17352
18155
|
function holdsOpeningToolLoad(input) {
|
|
17353
18156
|
if (typeof input === "string") return false;
|
|
17354
18157
|
const items = input;
|
|
17355
|
-
return items.some((item) =>
|
|
18158
|
+
return items.some((item) => isRecord10(item) && callId2(item) === OPENING_TOOL_LOAD_CALL_ID);
|
|
17356
18159
|
}
|
|
17357
18160
|
var LazyToolModel = class {
|
|
17358
18161
|
constructor(inner, runtime) {
|
|
@@ -17451,7 +18254,7 @@ var LazyToolModel = class {
|
|
|
17451
18254
|
};
|
|
17452
18255
|
function responseRequiresToolPreparation(response, runtime) {
|
|
17453
18256
|
return response.output.some(
|
|
17454
|
-
(item) =>
|
|
18257
|
+
(item) => isRecord10(item) && item.type === "function_call" && typeof item.name === "string" && runtime.requiresPreparationForFunctionCall(item.name)
|
|
17455
18258
|
);
|
|
17456
18259
|
}
|
|
17457
18260
|
var LazyToolModelProvider = class {
|
|
@@ -18207,7 +19010,8 @@ async function runSingleModelCall(target, request, options = {}) {
|
|
|
18207
19010
|
new AnthropicMessagesModel(
|
|
18208
19011
|
provider,
|
|
18209
19012
|
modelId,
|
|
18210
|
-
instrumentedModelFetch(provider.id, globalThis.fetch, false, residency)
|
|
19013
|
+
instrumentedModelFetch(provider.id, globalThis.fetch, false, residency),
|
|
19014
|
+
modelToolNameObservers(provider.id)
|
|
18211
19015
|
),
|
|
18212
19016
|
request,
|
|
18213
19017
|
options,
|
|
@@ -19393,8 +20197,7 @@ var OpenGeniResponsesModel = class extends AppendOnlyOpenAIResponsesModel {
|
|
|
19393
20197
|
let outcome = "completed";
|
|
19394
20198
|
try {
|
|
19395
20199
|
const input = typeof request.input === "string" ? request.input : projectHistoryForProvider(request.input, "responses", {
|
|
19396
|
-
declaresAdditionalTools: responsesRouteDeclaresAdditionalTools(this.provider.kind)
|
|
19397
|
-
knownToolNames: requestFunctionToolNames(request.tools)
|
|
20200
|
+
declaresAdditionalTools: responsesRouteDeclaresAdditionalTools(this.provider.kind)
|
|
19398
20201
|
});
|
|
19399
20202
|
return super._buildResponsesCreateRequest(
|
|
19400
20203
|
input === request.input ? request : { ...request, input },
|
|
@@ -19418,7 +20221,8 @@ function buildModelInstance(provider, client, modelId, residency = null) {
|
|
|
19418
20221
|
return new AnthropicMessagesModel(
|
|
19419
20222
|
provider,
|
|
19420
20223
|
modelId,
|
|
19421
|
-
instrumentedModelFetch(provider.id, globalThis.fetch, false, residency)
|
|
20224
|
+
instrumentedModelFetch(provider.id, globalThis.fetch, false, residency),
|
|
20225
|
+
modelToolNameObservers(provider.id)
|
|
19422
20226
|
);
|
|
19423
20227
|
return provider.api === "chat" ? new OpenGeniChatCompletionsModel(client, modelId) : new OpenGeniResponsesModel(client, modelId, provider);
|
|
19424
20228
|
}
|
|
@@ -19642,10 +20446,10 @@ export {
|
|
|
19642
20446
|
isPreemptedModelResponse,
|
|
19643
20447
|
preemptedResponseCutOffText,
|
|
19644
20448
|
preemptedResponseDone,
|
|
19645
|
-
prefixedMcpToolName,
|
|
19646
|
-
legacyMcpToolDigest,
|
|
19647
20449
|
ProviderHistoryIncompatibleError,
|
|
19648
20450
|
projectHistoryForProvider,
|
|
20451
|
+
prefixedMcpToolName,
|
|
20452
|
+
legacyMcpToolDigest,
|
|
19649
20453
|
recordStableSystemPromptPrefix,
|
|
19650
20454
|
AnthropicStreamInterruptedError,
|
|
19651
20455
|
AnthropicProviderRejection,
|
|
@@ -19703,6 +20507,7 @@ export {
|
|
|
19703
20507
|
providerQuotaExhaustedMessage,
|
|
19704
20508
|
classifyProviderQuotaError,
|
|
19705
20509
|
configureRuntimeMetricsHooks,
|
|
20510
|
+
modelToolNameObservers,
|
|
19706
20511
|
recordRuntimeMcpToolCallMetric,
|
|
19707
20512
|
recordRuntimeMcpLifecycleMetric,
|
|
19708
20513
|
buildOpenAIClientFromSettings,
|
|
@@ -19773,4 +20578,4 @@ export {
|
|
|
19773
20578
|
resolveTurnModel,
|
|
19774
20579
|
MultiProviderModelProvider
|
|
19775
20580
|
};
|
|
19776
|
-
//# sourceMappingURL=chunk-
|
|
20581
|
+
//# sourceMappingURL=chunk-RXCYGCX7.js.map
|