linkque-cli-v2 1.3.3 → 1.3.4
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/linkquejs.js +106 -241
- package/package.json +1 -1
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{chunk-BWCMNWG2.js → chunk-23PMJWOG.js} +18 -30
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{chunk-JXQYQ563.js → chunk-VSZ3UDC7.js} +1 -1
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{chunk-CM4N2PT3.js → chunk-YSRA6KFZ.js} +1736 -1839
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/runtime/handler/chat.js +101 -140
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/runtime/handler/clientCapability.js +2 -2
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/runtime/protocol.js +2 -2
- package/vendor/linkque-agent-appwrite-integrate/package.json +1 -1
- package/worker.js +701 -502
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{anthropic-messages-5DOLTIGP.js → anthropic-messages-EL2IU4YU.js} +3 -3
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{azure-openai-responses-HVAVRLTK.js → azure-openai-responses-A4XURCOX.js} +1 -1
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{mistral-conversations-UJBDG2KY.js → mistral-conversations-TQA2MPJJ.js} +10 -10
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{openai-codex-responses-3KOC6KHN.js → openai-codex-responses-WLREQPZK.js} +1 -1
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{openai-completions-HG6OJRL5.js → openai-completions-U6LUBY4P.js} +3 -3
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{openai-responses-F6V2PG3Y.js → openai-responses-SFZAU5JA.js} +1 -1
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{pi-messages-DC4DVRAJ.js → pi-messages-QQA6ZGV2.js} +3 -3
package/vendor/linkque-agent-appwrite-integrate/dist/esm/{chunk-CM4N2PT3.js → chunk-YSRA6KFZ.js}
RENAMED
|
@@ -8866,6 +8866,164 @@ var EHookExecutorName;
|
|
|
8866
8866
|
EHookExecutorName2["ImageUnderstandingHookExecutor"] = "ImageUnderstandingHookExecutor";
|
|
8867
8867
|
})(EHookExecutorName || (EHookExecutorName = {}));
|
|
8868
8868
|
|
|
8869
|
+
// ../linkque-agent-runtime/dist/esm/core/followUpQuestions.js
|
|
8870
|
+
var FOLLOW_UP_QUESTION_MAX_COUNT = 3;
|
|
8871
|
+
var FOLLOW_UP_QUESTION_MAX_LENGTH = 20;
|
|
8872
|
+
var FOLLOW_UP_CONTEXT_MAX_HISTORY_RUNS = 2;
|
|
8873
|
+
var FOLLOW_UP_CONTEXT_TOKEN_BUDGET = 2e3;
|
|
8874
|
+
function prepareFollowUpQuestionContext(turns, history = []) {
|
|
8875
|
+
const visible = turns.flatMap((turn) => {
|
|
8876
|
+
if (turn.role !== "user" && turn.role !== "assistant")
|
|
8877
|
+
return [];
|
|
8878
|
+
if (typeof turn.content !== "string")
|
|
8879
|
+
return [];
|
|
8880
|
+
const content = turn.content.replace(/\s+/gu, " ").trim();
|
|
8881
|
+
return content ? [{ role: turn.role, content }] : [];
|
|
8882
|
+
});
|
|
8883
|
+
let assistantIndex = -1;
|
|
8884
|
+
let userIndex = -1;
|
|
8885
|
+
for (let index = visible.length - 1; index >= 0; index -= 1) {
|
|
8886
|
+
if (assistantIndex < 0 && visible[index].role === "assistant") {
|
|
8887
|
+
assistantIndex = index;
|
|
8888
|
+
continue;
|
|
8889
|
+
}
|
|
8890
|
+
if (assistantIndex >= 0 && index < assistantIndex && visible[index].role === "user") {
|
|
8891
|
+
userIndex = index;
|
|
8892
|
+
break;
|
|
8893
|
+
}
|
|
8894
|
+
}
|
|
8895
|
+
if (userIndex < 0 || assistantIndex < 0)
|
|
8896
|
+
return void 0;
|
|
8897
|
+
const lastUserMessage = visible[userIndex].content;
|
|
8898
|
+
const assistantTokens = estimateTokens(visible[assistantIndex].content);
|
|
8899
|
+
const userTokens = estimateTokens(lastUserMessage);
|
|
8900
|
+
const assistantBudget = Math.min(assistantTokens, 1500);
|
|
8901
|
+
const userBudget = Math.min(userTokens, FOLLOW_UP_CONTEXT_TOKEN_BUDGET - assistantBudget);
|
|
8902
|
+
let remaining = FOLLOW_UP_CONTEXT_TOKEN_BUDGET - assistantBudget - userBudget;
|
|
8903
|
+
const messages = [
|
|
8904
|
+
{
|
|
8905
|
+
role: "user",
|
|
8906
|
+
content: truncateToEstimatedTokens(lastUserMessage, userBudget)
|
|
8907
|
+
},
|
|
8908
|
+
{
|
|
8909
|
+
role: "assistant",
|
|
8910
|
+
content: truncateToEstimatedTokens(visible[assistantIndex].content, assistantBudget)
|
|
8911
|
+
}
|
|
8912
|
+
];
|
|
8913
|
+
const normalizedHistory = history.slice(-FOLLOW_UP_CONTEXT_MAX_HISTORY_RUNS).map(normalizeHistoryEntry).filter((entry) => Boolean(entry));
|
|
8914
|
+
const selectedHistory = [];
|
|
8915
|
+
for (let index = normalizedHistory.length - 1; index >= 0; index -= 1) {
|
|
8916
|
+
if (remaining <= 0)
|
|
8917
|
+
break;
|
|
8918
|
+
const entry = normalizedHistory[index];
|
|
8919
|
+
let entryRemaining = remaining;
|
|
8920
|
+
const followUpQuestions = [];
|
|
8921
|
+
for (const question of entry.followUpQuestions) {
|
|
8922
|
+
const tokens = estimateTokens(question);
|
|
8923
|
+
if (tokens > entryRemaining)
|
|
8924
|
+
break;
|
|
8925
|
+
followUpQuestions.push(question);
|
|
8926
|
+
entryRemaining -= tokens;
|
|
8927
|
+
}
|
|
8928
|
+
const userMessage = truncateToEstimatedTokens(entry.userMessage, entryRemaining);
|
|
8929
|
+
if (!userMessage)
|
|
8930
|
+
continue;
|
|
8931
|
+
entryRemaining -= estimateTokens(userMessage);
|
|
8932
|
+
remaining = entryRemaining;
|
|
8933
|
+
selectedHistory.unshift({ userMessage, followUpQuestions });
|
|
8934
|
+
}
|
|
8935
|
+
return {
|
|
8936
|
+
messages,
|
|
8937
|
+
lastUserMessage,
|
|
8938
|
+
...selectedHistory.length ? { history: selectedHistory } : {}
|
|
8939
|
+
};
|
|
8940
|
+
}
|
|
8941
|
+
function normalizeHistoryEntry(entry) {
|
|
8942
|
+
const userMessage = entry.userMessage.replace(/\s+/gu, " ").trim();
|
|
8943
|
+
if (!userMessage)
|
|
8944
|
+
return void 0;
|
|
8945
|
+
return {
|
|
8946
|
+
userMessage,
|
|
8947
|
+
followUpQuestions: normalizeFollowUpQuestionStrings(entry.followUpQuestions)
|
|
8948
|
+
};
|
|
8949
|
+
}
|
|
8950
|
+
function normalizeFollowUpQuestionStrings(value) {
|
|
8951
|
+
if (!Array.isArray(value))
|
|
8952
|
+
return [];
|
|
8953
|
+
const seen = /* @__PURE__ */ new Set();
|
|
8954
|
+
const result = [];
|
|
8955
|
+
for (const candidate of value) {
|
|
8956
|
+
if (typeof candidate !== "string")
|
|
8957
|
+
continue;
|
|
8958
|
+
const normalized = candidate.replace(/\s+/gu, " ").trim();
|
|
8959
|
+
if (!normalized || normalized.length > FOLLOW_UP_QUESTION_MAX_LENGTH || seen.has(normalized))
|
|
8960
|
+
continue;
|
|
8961
|
+
seen.add(normalized);
|
|
8962
|
+
result.push(normalized);
|
|
8963
|
+
if (result.length === FOLLOW_UP_QUESTION_MAX_COUNT)
|
|
8964
|
+
break;
|
|
8965
|
+
}
|
|
8966
|
+
return result;
|
|
8967
|
+
}
|
|
8968
|
+
function estimateTokens(text) {
|
|
8969
|
+
let tokens = 0;
|
|
8970
|
+
let asciiRun = 0;
|
|
8971
|
+
const flushAscii = () => {
|
|
8972
|
+
if (asciiRun > 0)
|
|
8973
|
+
tokens += Math.ceil(asciiRun / 4);
|
|
8974
|
+
asciiRun = 0;
|
|
8975
|
+
};
|
|
8976
|
+
for (const character of text) {
|
|
8977
|
+
if (/^[\x20-\x7e]$/u.test(character))
|
|
8978
|
+
asciiRun += 1;
|
|
8979
|
+
else {
|
|
8980
|
+
flushAscii();
|
|
8981
|
+
tokens += 1;
|
|
8982
|
+
}
|
|
8983
|
+
}
|
|
8984
|
+
flushAscii();
|
|
8985
|
+
return Math.max(tokens, 1);
|
|
8986
|
+
}
|
|
8987
|
+
function truncateToEstimatedTokens(text, budget) {
|
|
8988
|
+
if (estimateTokens(text) <= budget)
|
|
8989
|
+
return text;
|
|
8990
|
+
let low = 0;
|
|
8991
|
+
let high = text.length;
|
|
8992
|
+
while (low < high) {
|
|
8993
|
+
const middle = Math.ceil((low + high) / 2);
|
|
8994
|
+
if (estimateTokens(text.slice(0, middle)) <= budget)
|
|
8995
|
+
low = middle;
|
|
8996
|
+
else
|
|
8997
|
+
high = middle - 1;
|
|
8998
|
+
}
|
|
8999
|
+
return text.slice(0, low).trim();
|
|
9000
|
+
}
|
|
9001
|
+
|
|
9002
|
+
// ../linkque-agent-runtime/dist/esm/core/types.js
|
|
9003
|
+
var EDebugLogScope;
|
|
9004
|
+
(function(EDebugLogScope2) {
|
|
9005
|
+
EDebugLogScope2["A2UI_ACTION"] = "A2UI_ACTION";
|
|
9006
|
+
EDebugLogScope2["CONFIG"] = "CONFIG";
|
|
9007
|
+
EDebugLogScope2["LLM"] = "LLM";
|
|
9008
|
+
EDebugLogScope2["PI_EVENT"] = "PI_EVENT";
|
|
9009
|
+
EDebugLogScope2["MCP"] = "MCP";
|
|
9010
|
+
EDebugLogScope2["HTTP"] = "HTTP";
|
|
9011
|
+
EDebugLogScope2["HOOK"] = "HOOK";
|
|
9012
|
+
})(EDebugLogScope || (EDebugLogScope = {}));
|
|
9013
|
+
|
|
9014
|
+
// ../linkque-agent-runtime/dist/esm/core/cardTools.js
|
|
9015
|
+
import { randomUUID } from "node:crypto";
|
|
9016
|
+
|
|
9017
|
+
// ../agent-a2ui-card-contract/dist/esm/protocol.js
|
|
9018
|
+
var LINKQUE_A2UI_V4_CATALOG_ID = "urn:linkque:a2ui:runtime:4";
|
|
9019
|
+
|
|
9020
|
+
// ../agent-a2ui-card-contract/dist/esm/types.js
|
|
9021
|
+
var A2UI_CARD_TRANSFORM_LANGUAGE = "jmespath";
|
|
9022
|
+
var A2UI_CARD_TRANSFORM_PROFILE = "linkque-structural-v1";
|
|
9023
|
+
var A2UI_CARD_EXPRESSION_MAX_LENGTH = 4096;
|
|
9024
|
+
var A2UI_CARD_EXPRESSION_MAX_AST_NODES = 512;
|
|
9025
|
+
var A2UI_CARD_EXPRESSION_MAX_AST_DEPTH = 32;
|
|
9026
|
+
|
|
8869
9027
|
// ../linkque-agent-data-binding-contract/dist/esm/json.js
|
|
8870
9028
|
var BLOCKED_PATH_SEGMENTS = /* @__PURE__ */ new Set([
|
|
8871
9029
|
"__proto__",
|
|
@@ -8991,6 +9149,21 @@ function getJsonSchemaIssues(value, path2 = "schema") {
|
|
|
8991
9149
|
function isObjectJsonSchema(value) {
|
|
8992
9150
|
return isPlainRecord(value) && value.type === "object" && getJsonSchemaIssues(value).length === 0;
|
|
8993
9151
|
}
|
|
9152
|
+
function validateJsonSchemaValue(schema, value, path2 = "$") {
|
|
9153
|
+
let validate;
|
|
9154
|
+
try {
|
|
9155
|
+
validate = ajv.compile(schema);
|
|
9156
|
+
} catch (error) {
|
|
9157
|
+
return [
|
|
9158
|
+
issue(path2, "INVALID_JSON_SCHEMA", error instanceof Error ? error.message : "schema is invalid")
|
|
9159
|
+
];
|
|
9160
|
+
}
|
|
9161
|
+
return validate(value) ? [] : (validate.errors ?? []).map(errorToIssue.bind(null, path2));
|
|
9162
|
+
}
|
|
9163
|
+
function errorToIssue(root, error) {
|
|
9164
|
+
const suffix = error.instancePath.split("/").filter(Boolean).map((segment) => segment.replace(/~1/gu, "/").replace(/~0/gu, "~")).map((segment) => `[${JSON.stringify(segment)}]`).join("");
|
|
9165
|
+
return issue(`${root}${suffix}`, "JSON_SCHEMA_VALUE_INVALID", error.message ?? "value does not match schema");
|
|
9166
|
+
}
|
|
8994
9167
|
|
|
8995
9168
|
// ../linkque-agent-data-binding-contract/dist/esm/structural.js
|
|
8996
9169
|
var import_jmespath = __toESM(require_jmespath(), 1);
|
|
@@ -9172,343 +9345,28 @@ function issue2(path2, code, message) {
|
|
|
9172
9345
|
function exactKeys(value, allowed, path2, issues) {
|
|
9173
9346
|
}
|
|
9174
9347
|
|
|
9175
|
-
// ../agent-a2ui-card-contract/dist/esm/
|
|
9176
|
-
var
|
|
9177
|
-
|
|
9178
|
-
var A2UI_PAYMENT_CALLBACK_MAX_BYTES = 16 * 1024;
|
|
9179
|
-
var A2UIActionContractError = class extends Error {
|
|
9180
|
-
constructor(issues) {
|
|
9181
|
-
super(issues.join("; "));
|
|
9182
|
-
Object.defineProperty(this, "issues", {
|
|
9183
|
-
enumerable: true,
|
|
9184
|
-
configurable: true,
|
|
9185
|
-
writable: true,
|
|
9186
|
-
value: issues
|
|
9187
|
-
});
|
|
9188
|
-
Object.defineProperty(this, "code", {
|
|
9189
|
-
enumerable: true,
|
|
9190
|
-
configurable: true,
|
|
9191
|
-
writable: true,
|
|
9192
|
-
value: "A2UI_ACTION_CONTRACT_INVALID"
|
|
9193
|
-
});
|
|
9194
|
-
this.name = "A2UIActionContractError";
|
|
9195
|
-
}
|
|
9196
|
-
};
|
|
9197
|
-
function parseA2UIActionExecuteRequest(value) {
|
|
9198
|
-
const issues = getA2UIActionExecuteRequestIssues(value);
|
|
9199
|
-
if (issues.length)
|
|
9200
|
-
throw new A2UIActionContractError(issues);
|
|
9201
|
-
return JSON.parse(JSON.stringify(value));
|
|
9202
|
-
}
|
|
9203
|
-
function getA2UIActionExecuteRequestIssues(value) {
|
|
9204
|
-
if (!isPlainRecord(value))
|
|
9205
|
-
return ["request must be an object"];
|
|
9206
|
-
const issues = [];
|
|
9207
|
-
rejectExtraKeysAsStrings(value, [
|
|
9208
|
-
"schemaVersion",
|
|
9209
|
-
"agentId",
|
|
9210
|
-
"threadId",
|
|
9211
|
-
"invocationId",
|
|
9212
|
-
"actionId",
|
|
9213
|
-
"source",
|
|
9214
|
-
"actionContext"
|
|
9215
|
-
], "request", issues);
|
|
9216
|
-
if (value.schemaVersion !== "1")
|
|
9217
|
-
issues.push("schemaVersion must be 1");
|
|
9218
|
-
requiredIdentifier(value.agentId, "agentId", issues);
|
|
9219
|
-
requiredIdentifier(value.threadId, "threadId", issues);
|
|
9220
|
-
requiredIdentifier(value.invocationId, "invocationId", issues);
|
|
9221
|
-
requiredIdentifier(value.actionId, "actionId", issues);
|
|
9222
|
-
validateSource(value.source, issues);
|
|
9223
|
-
if (!cloneJsonObject(value.actionContext)) {
|
|
9224
|
-
issues.push("actionContext must be a JSON object");
|
|
9225
|
-
}
|
|
9226
|
-
return issues;
|
|
9227
|
-
}
|
|
9228
|
-
function parseA2UIPaymentResultEventPayload(value) {
|
|
9229
|
-
if (!isPlainRecord(value)) {
|
|
9230
|
-
throw new A2UIActionContractError([
|
|
9231
|
-
"payment result payload must be an object"
|
|
9232
|
-
]);
|
|
9233
|
-
}
|
|
9234
|
-
const issues = [];
|
|
9235
|
-
rejectExtraKeysAsStrings(value, ["actionId", "actionInvocationId", "tradeNO", "actionContext", "callback"], "payment result payload", issues);
|
|
9236
|
-
requiredIdentifier(value.actionId, "actionId", issues);
|
|
9237
|
-
requiredIdentifier(value.actionInvocationId, "actionInvocationId", issues);
|
|
9238
|
-
if (typeof value.tradeNO !== "string" || !value.tradeNO.trim() || value.tradeNO.length > 64) {
|
|
9239
|
-
issues.push("tradeNO must be a non-empty string up to 64 characters");
|
|
9240
|
-
}
|
|
9241
|
-
if (!cloneJsonObject(value.actionContext)) {
|
|
9242
|
-
issues.push("actionContext must be a JSON object");
|
|
9243
|
-
}
|
|
9244
|
-
const callback = cloneJsonObject(value.callback);
|
|
9245
|
-
if (!callback || typeof callback.resultCode !== "string") {
|
|
9246
|
-
issues.push("callback.resultCode must be a string");
|
|
9247
|
-
} else if (new TextEncoder().encode(JSON.stringify(callback)).byteLength > A2UI_PAYMENT_CALLBACK_MAX_BYTES) {
|
|
9248
|
-
issues.push(`callback must not exceed ${A2UI_PAYMENT_CALLBACK_MAX_BYTES} bytes`);
|
|
9249
|
-
}
|
|
9250
|
-
if (issues.length)
|
|
9251
|
-
throw new A2UIActionContractError(issues);
|
|
9252
|
-
return JSON.parse(JSON.stringify(value));
|
|
9253
|
-
}
|
|
9254
|
-
function getA2UICardActionBindingIssues(value, path2 = "actionBindings") {
|
|
9255
|
-
if (!Array.isArray(value)) {
|
|
9256
|
-
return [
|
|
9257
|
-
issue2(path2, "INVALID_ACTION_BINDINGS", "actionBindings must be an array")
|
|
9258
|
-
];
|
|
9259
|
-
}
|
|
9348
|
+
// ../agent-a2ui-card-contract/dist/esm/binding-analyzer.js
|
|
9349
|
+
var INPUT_COMPONENTS = /* @__PURE__ */ new Set(["ChoicePicker", "Stepper", "TextField"]);
|
|
9350
|
+
function analyzeA2UIBindings(protocol) {
|
|
9260
9351
|
const issues = [];
|
|
9261
|
-
const
|
|
9262
|
-
|
|
9263
|
-
|
|
9264
|
-
|
|
9265
|
-
|
|
9352
|
+
const occurrences = [];
|
|
9353
|
+
const surfaces = /* @__PURE__ */ new Set();
|
|
9354
|
+
const componentsBySurface = /* @__PURE__ */ new Map();
|
|
9355
|
+
protocol.forEach((message, messageIndex) => {
|
|
9356
|
+
if (!isPlainRecord(message) || !Array.isArray(message.datas)) {
|
|
9357
|
+
issues.push(issue2(`card[${messageIndex}]`, "INVALID_A2UI_MESSAGE", "A2UI message must contain a datas array"));
|
|
9266
9358
|
return;
|
|
9267
9359
|
}
|
|
9268
|
-
if (
|
|
9269
|
-
issues.push(issue2(
|
|
9270
|
-
} else if (actionIds.has(candidate.actionId)) {
|
|
9271
|
-
issues.push(issue2(`${itemPath}.actionId`, "DUPLICATE_ACTION_ID", "actionId must be unique"));
|
|
9272
|
-
} else {
|
|
9273
|
-
actionIds.add(candidate.actionId);
|
|
9274
|
-
}
|
|
9275
|
-
if (candidate.kind !== "payment") {
|
|
9276
|
-
issues.push(issue2(`${itemPath}.kind`, "INVALID_ACTION_KIND", "kind must be payment"));
|
|
9277
|
-
return;
|
|
9360
|
+
if (message.version !== "0.9") {
|
|
9361
|
+
issues.push(issue2(`card[${messageIndex}].version`, "UNSUPPORTED_PROTOCOL_VERSION", "A2UI card resources must use protocol version 0.9"));
|
|
9278
9362
|
}
|
|
9279
|
-
rejectExtraKeys(candidate, ["actionId", "kind", "createTrade", "queryTrade"], itemPath, issues);
|
|
9280
|
-
validateToolBinding(candidate.createTrade, `${itemPath}.createTrade`, "create", issues);
|
|
9281
|
-
validateToolBinding(candidate.queryTrade, `${itemPath}.queryTrade`, "query", issues);
|
|
9282
|
-
});
|
|
9283
|
-
return issues;
|
|
9284
|
-
}
|
|
9285
|
-
function getA2UIPaymentActionReferenceIssues(card, bindings, path2 = "a2uiTemplate.content") {
|
|
9286
|
-
const issues = [];
|
|
9287
|
-
const bindingIds = new Set(bindings.map((binding) => binding.actionId));
|
|
9288
|
-
const referenced = /* @__PURE__ */ new Set();
|
|
9289
|
-
card.forEach((message, messageIndex) => {
|
|
9290
9363
|
message.datas.forEach((instruction, instructionIndex) => {
|
|
9291
|
-
|
|
9292
|
-
|
|
9293
|
-
|
|
9294
|
-
|
|
9295
|
-
|
|
9296
|
-
|
|
9297
|
-
collectPaymentActionReferences(component.action, `${path2}[${messageIndex}].datas[${instructionIndex}].updateComponents.components[${componentIndex}].action`, referenced, issues);
|
|
9298
|
-
});
|
|
9299
|
-
});
|
|
9300
|
-
});
|
|
9301
|
-
for (const actionId of referenced) {
|
|
9302
|
-
if (!bindingIds.has(actionId)) {
|
|
9303
|
-
issues.push(issue2(path2, "PAYMENT_ACTION_BINDING_MISSING", `payment action ${actionId} has no action binding`));
|
|
9304
|
-
}
|
|
9305
|
-
}
|
|
9306
|
-
for (const actionId of bindingIds) {
|
|
9307
|
-
if (!referenced.has(actionId)) {
|
|
9308
|
-
issues.push(issue2(path2, "PAYMENT_ACTION_UNUSED", `payment action binding ${actionId} is not referenced`));
|
|
9309
|
-
}
|
|
9310
|
-
}
|
|
9311
|
-
return issues;
|
|
9312
|
-
}
|
|
9313
|
-
function validateToolBinding(value, path2, phase, issues) {
|
|
9314
|
-
if (!isPlainRecord(value)) {
|
|
9315
|
-
issues.push(issue2(path2, "INVALID_PAYMENT_TOOL", `${path2} must be an object`));
|
|
9316
|
-
return;
|
|
9317
|
-
}
|
|
9318
|
-
rejectExtraKeys(value, phase === "create" ? ["mcpResourceId", "toolName", "inputBindings", "tradeNoPath"] : [
|
|
9319
|
-
"mcpResourceId",
|
|
9320
|
-
"toolName",
|
|
9321
|
-
"inputBindings",
|
|
9322
|
-
"statusPath",
|
|
9323
|
-
"paidValues"
|
|
9324
|
-
], path2, issues);
|
|
9325
|
-
if (!isSafeIdentifier(value.mcpResourceId)) {
|
|
9326
|
-
issues.push(issue2(`${path2}.mcpResourceId`, "INVALID_MCP_RESOURCE_ID", "mcpResourceId is invalid"));
|
|
9327
|
-
}
|
|
9328
|
-
if (typeof value.toolName !== "string" || !value.toolName.trim()) {
|
|
9329
|
-
issues.push(issue2(`${path2}.toolName`, "INVALID_TOOL_NAME", "toolName is required"));
|
|
9330
|
-
}
|
|
9331
|
-
validateInputBindings(value.inputBindings, `${path2}.inputBindings`, phase, issues);
|
|
9332
|
-
if (phase === "create") {
|
|
9333
|
-
if (!isSafeJsonPointer(value.tradeNoPath)) {
|
|
9334
|
-
issues.push(issue2(`${path2}.tradeNoPath`, "INVALID_TRADE_NO_PATH", "tradeNoPath must be an absolute JSON Pointer"));
|
|
9335
|
-
}
|
|
9336
|
-
return;
|
|
9337
|
-
}
|
|
9338
|
-
if (!isSafeJsonPointer(value.statusPath)) {
|
|
9339
|
-
issues.push(issue2(`${path2}.statusPath`, "INVALID_PAYMENT_STATUS_PATH", "statusPath must be an absolute JSON Pointer"));
|
|
9340
|
-
}
|
|
9341
|
-
if (!Array.isArray(value.inputBindings) || !value.inputBindings.some((binding) => isPlainRecord(binding) && isPlainRecord(binding.source) && binding.source.kind === "trade-no")) {
|
|
9342
|
-
issues.push(issue2(`${path2}.inputBindings`, "PAYMENT_TRADE_NO_BINDING_MISSING", "queryTrade must map tradeNO"));
|
|
9343
|
-
}
|
|
9344
|
-
if (!Array.isArray(value.paidValues) || value.paidValues.length === 0 || value.paidValues.some((entry) => !isJsonPrimitive(entry))) {
|
|
9345
|
-
issues.push(issue2(`${path2}.paidValues`, "INVALID_PAID_VALUES", "paidValues must contain JSON primitives"));
|
|
9346
|
-
}
|
|
9347
|
-
}
|
|
9348
|
-
function collectPaymentActionReferences(value, path2, referenced, issues) {
|
|
9349
|
-
if (!isPlainRecord(value))
|
|
9350
|
-
return;
|
|
9351
|
-
if (isPlainRecord(value.client) && value.client.name === A2UI_PAYMENT_ACTION_NAME) {
|
|
9352
|
-
const context = value.client.context;
|
|
9353
|
-
if (!isPlainRecord(context) || !isSafeIdentifier(context.actionId)) {
|
|
9354
|
-
issues.push(issue2(path2, "INVALID_PAYMENT_ACTION", "payment action requires a safe actionId"));
|
|
9355
|
-
return;
|
|
9356
|
-
}
|
|
9357
|
-
rejectExtraKeys(context, ["actionId", "actionContext"], `${path2}.client.context`, issues);
|
|
9358
|
-
if (!isPlainRecord(context.actionContext)) {
|
|
9359
|
-
issues.push(issue2(path2, "INVALID_PAYMENT_ACTION", "payment action requires an object actionContext"));
|
|
9360
|
-
}
|
|
9361
|
-
referenced.add(context.actionId);
|
|
9362
|
-
return;
|
|
9363
|
-
}
|
|
9364
|
-
if (!isPlainRecord(value.select))
|
|
9365
|
-
return;
|
|
9366
|
-
if (Array.isArray(value.select.cases)) {
|
|
9367
|
-
value.select.cases.forEach((entry, index) => {
|
|
9368
|
-
if (isPlainRecord(entry)) {
|
|
9369
|
-
collectPaymentActionReferences(entry.action, `${path2}.select.cases[${index}].action`, referenced, issues);
|
|
9370
|
-
}
|
|
9371
|
-
});
|
|
9372
|
-
}
|
|
9373
|
-
collectPaymentActionReferences(value.select.default, `${path2}.select.default`, referenced, issues);
|
|
9374
|
-
}
|
|
9375
|
-
function validateInputBindings(value, path2, phase, issues) {
|
|
9376
|
-
if (!Array.isArray(value) || value.length > 128) {
|
|
9377
|
-
issues.push(issue2(path2, "INVALID_PAYMENT_INPUT_BINDINGS", "inputBindings must be an array of at most 128 entries"));
|
|
9378
|
-
return;
|
|
9379
|
-
}
|
|
9380
|
-
const targets = /* @__PURE__ */ new Set();
|
|
9381
|
-
value.forEach((candidate, index) => {
|
|
9382
|
-
const itemPath = `${path2}[${index}]`;
|
|
9383
|
-
if (!isPlainRecord(candidate) || !isSafeJsonPointer(candidate.inputPath)) {
|
|
9384
|
-
issues.push(issue2(itemPath, "INVALID_PAYMENT_INPUT_BINDING", "inputPath must be an absolute JSON Pointer"));
|
|
9385
|
-
return;
|
|
9386
|
-
}
|
|
9387
|
-
rejectExtraKeys(candidate, ["inputPath", "source"], itemPath, issues);
|
|
9388
|
-
if (targets.has(candidate.inputPath)) {
|
|
9389
|
-
issues.push(issue2(`${itemPath}.inputPath`, "DUPLICATE_PAYMENT_INPUT_PATH", "inputPath must be unique"));
|
|
9390
|
-
}
|
|
9391
|
-
targets.add(candidate.inputPath);
|
|
9392
|
-
validateInputSource(candidate.source, `${itemPath}.source`, phase, issues);
|
|
9393
|
-
});
|
|
9394
|
-
}
|
|
9395
|
-
function validateInputSource(value, path2, phase, issues) {
|
|
9396
|
-
if (!isPlainRecord(value) || typeof value.kind !== "string") {
|
|
9397
|
-
issues.push(issue2(path2, "INVALID_PAYMENT_INPUT_SOURCE", "source must be an object"));
|
|
9398
|
-
return;
|
|
9399
|
-
}
|
|
9400
|
-
if (value.kind === "action-context") {
|
|
9401
|
-
rejectExtraKeys(value, ["kind", "path"], path2, issues);
|
|
9402
|
-
if (!isSafeJsonPointer(value.path, true)) {
|
|
9403
|
-
issues.push(issue2(`${path2}.path`, "INVALID_PAYMENT_SOURCE_PATH", "action context path must be a JSON Pointer"));
|
|
9404
|
-
}
|
|
9405
|
-
return;
|
|
9406
|
-
}
|
|
9407
|
-
if (value.kind === "runtime-context") {
|
|
9408
|
-
rejectExtraKeys(value, ["kind", "key"], path2, issues);
|
|
9409
|
-
if (!RUNTIME_CONTEXT_KEYS.has(String(value.key))) {
|
|
9410
|
-
issues.push(issue2(`${path2}.key`, "INVALID_RUNTIME_CONTEXT_KEY", "runtime context key is unsupported"));
|
|
9411
|
-
}
|
|
9412
|
-
return;
|
|
9413
|
-
}
|
|
9414
|
-
if (value.kind === "literal") {
|
|
9415
|
-
rejectExtraKeys(value, ["kind", "value"], path2, issues);
|
|
9416
|
-
if (!isJsonValue(value.value)) {
|
|
9417
|
-
issues.push(issue2(`${path2}.value`, "INVALID_PAYMENT_LITERAL", "literal must be JSON"));
|
|
9418
|
-
}
|
|
9419
|
-
return;
|
|
9420
|
-
}
|
|
9421
|
-
if (phase === "query" && value.kind === "trade-no") {
|
|
9422
|
-
rejectExtraKeys(value, ["kind"], path2, issues);
|
|
9423
|
-
return;
|
|
9424
|
-
}
|
|
9425
|
-
if (phase === "query" && value.kind === "callback") {
|
|
9426
|
-
rejectExtraKeys(value, ["kind", "path"], path2, issues);
|
|
9427
|
-
if (!isSafeJsonPointer(value.path, true)) {
|
|
9428
|
-
issues.push(issue2(`${path2}.path`, "INVALID_PAYMENT_CALLBACK_PATH", "callback path must be a JSON Pointer"));
|
|
9429
|
-
}
|
|
9430
|
-
return;
|
|
9431
|
-
}
|
|
9432
|
-
issues.push(issue2(path2, "INVALID_PAYMENT_INPUT_SOURCE", `${value.kind} is unavailable for ${phase}`));
|
|
9433
|
-
}
|
|
9434
|
-
function rejectExtraKeys(value, allowed, path2, issues) {
|
|
9435
|
-
const allowedKeys = new Set(allowed);
|
|
9436
|
-
for (const key of Object.keys(value)) {
|
|
9437
|
-
if (!allowedKeys.has(key)) {
|
|
9438
|
-
issues.push(issue2(`${path2}.${key}`, "UNEXPECTED_ACTION_BINDING_FIELD", `${key} is not allowed`));
|
|
9439
|
-
}
|
|
9440
|
-
}
|
|
9441
|
-
}
|
|
9442
|
-
function validateSource(value, issues) {
|
|
9443
|
-
if (!isPlainRecord(value)) {
|
|
9444
|
-
issues.push("source must be an object");
|
|
9445
|
-
return;
|
|
9446
|
-
}
|
|
9447
|
-
rejectExtraKeysAsStrings(value, ["cardRef", "cardRevision", "cardInstanceId", "surfaceId", "componentId"], "source", issues);
|
|
9448
|
-
requiredIdentifier(value.cardRef, "source.cardRef", issues);
|
|
9449
|
-
requiredIdentifier(value.cardRevision, "source.cardRevision", issues);
|
|
9450
|
-
requiredText(value.cardInstanceId, "source.cardInstanceId", issues);
|
|
9451
|
-
requiredIdentifier(value.surfaceId, "source.surfaceId", issues);
|
|
9452
|
-
requiredIdentifier(value.componentId, "source.componentId", issues);
|
|
9453
|
-
}
|
|
9454
|
-
function rejectExtraKeysAsStrings(value, allowed, path2, issues) {
|
|
9455
|
-
const allowedKeys = new Set(allowed);
|
|
9456
|
-
for (const key of Object.keys(value)) {
|
|
9457
|
-
if (!allowedKeys.has(key))
|
|
9458
|
-
issues.push(`${path2}.${key} is not allowed`);
|
|
9459
|
-
}
|
|
9460
|
-
}
|
|
9461
|
-
function requiredIdentifier(value, path2, issues) {
|
|
9462
|
-
if (!isSafeIdentifier(value))
|
|
9463
|
-
issues.push(`${path2} must be a safe identifier`);
|
|
9464
|
-
}
|
|
9465
|
-
function requiredText(value, path2, issues) {
|
|
9466
|
-
if (typeof value !== "string" || !value.trim() || value.length > 256) {
|
|
9467
|
-
issues.push(`${path2} must be a non-empty string up to 256 characters`);
|
|
9468
|
-
}
|
|
9469
|
-
}
|
|
9470
|
-
function isJsonPrimitive(value) {
|
|
9471
|
-
return value === null || ["string", "number", "boolean"].includes(typeof value);
|
|
9472
|
-
}
|
|
9473
|
-
var RUNTIME_CONTEXT_KEYS = /* @__PURE__ */ new Set([
|
|
9474
|
-
"agentId",
|
|
9475
|
-
"tenantId",
|
|
9476
|
-
"threadId",
|
|
9477
|
-
"userId"
|
|
9478
|
-
]);
|
|
9479
|
-
|
|
9480
|
-
// ../agent-a2ui-card-contract/dist/esm/protocol.js
|
|
9481
|
-
var LINKQUE_A2UI_V4_CATALOG_ID = "urn:linkque:a2ui:runtime:4";
|
|
9482
|
-
|
|
9483
|
-
// ../agent-a2ui-card-contract/dist/esm/types.js
|
|
9484
|
-
var A2UI_CARD_TRANSFORM_LANGUAGE = "jmespath";
|
|
9485
|
-
var A2UI_CARD_TRANSFORM_PROFILE = "linkque-structural-v1";
|
|
9486
|
-
var A2UI_CARD_EXPRESSION_MAX_LENGTH = 4096;
|
|
9487
|
-
var A2UI_CARD_EXPRESSION_MAX_AST_NODES = 512;
|
|
9488
|
-
var A2UI_CARD_EXPRESSION_MAX_AST_DEPTH = 32;
|
|
9489
|
-
|
|
9490
|
-
// ../agent-a2ui-card-contract/dist/esm/binding-analyzer.js
|
|
9491
|
-
var INPUT_COMPONENTS = /* @__PURE__ */ new Set(["ChoicePicker", "Stepper", "TextField"]);
|
|
9492
|
-
function analyzeA2UIBindings(protocol) {
|
|
9493
|
-
const issues = [];
|
|
9494
|
-
const occurrences = [];
|
|
9495
|
-
const surfaces = /* @__PURE__ */ new Set();
|
|
9496
|
-
const componentsBySurface = /* @__PURE__ */ new Map();
|
|
9497
|
-
protocol.forEach((message, messageIndex) => {
|
|
9498
|
-
if (!isPlainRecord(message) || !Array.isArray(message.datas)) {
|
|
9499
|
-
issues.push(issue2(`card[${messageIndex}]`, "INVALID_A2UI_MESSAGE", "A2UI message must contain a datas array"));
|
|
9500
|
-
return;
|
|
9501
|
-
}
|
|
9502
|
-
if (message.version !== "0.9") {
|
|
9503
|
-
issues.push(issue2(`card[${messageIndex}].version`, "UNSUPPORTED_PROTOCOL_VERSION", "A2UI card resources must use protocol version 0.9"));
|
|
9504
|
-
}
|
|
9505
|
-
message.datas.forEach((instruction, instructionIndex) => {
|
|
9506
|
-
if (!isPlainRecord(instruction)) {
|
|
9507
|
-
issues.push(issue2(`card[${messageIndex}].datas[${instructionIndex}]`, "INVALID_A2UI_INSTRUCTION", "instruction must be an object"));
|
|
9508
|
-
return;
|
|
9509
|
-
}
|
|
9510
|
-
if ("createSurface" in instruction) {
|
|
9511
|
-
if (!isPlainRecord(instruction.createSurface))
|
|
9364
|
+
if (!isPlainRecord(instruction)) {
|
|
9365
|
+
issues.push(issue2(`card[${messageIndex}].datas[${instructionIndex}]`, "INVALID_A2UI_INSTRUCTION", "instruction must be an object"));
|
|
9366
|
+
return;
|
|
9367
|
+
}
|
|
9368
|
+
if ("createSurface" in instruction) {
|
|
9369
|
+
if (!isPlainRecord(instruction.createSurface))
|
|
9512
9370
|
return;
|
|
9513
9371
|
const surfaceId = instruction.createSurface.surfaceId;
|
|
9514
9372
|
if (!isSafeIdentifier(surfaceId)) {
|
|
@@ -10464,7 +10322,7 @@ function validateAction(value, path2, issues, depth) {
|
|
|
10464
10322
|
issues.push(issue2(path2, "INVALID_ACTION", "action must be an object with at most four nested selects"));
|
|
10465
10323
|
return;
|
|
10466
10324
|
}
|
|
10467
|
-
const branches = ["event", "local", "client", "select"].filter((key) => key in value);
|
|
10325
|
+
const branches = ["event", "function", "local", "client", "select"].filter((key) => key in value);
|
|
10468
10326
|
if (branches.length !== 1 || Object.keys(value).length !== 1) {
|
|
10469
10327
|
issues.push(issue2(path2, "INVALID_ACTION", "action must contain exactly one branch"));
|
|
10470
10328
|
return;
|
|
@@ -10485,6 +10343,10 @@ function validateAction(value, path2, issues, depth) {
|
|
|
10485
10343
|
issues.push(issue2(`${path2}.event.x2-user-message`, "INVALID_ACTION_CONTEXT", "x2-user-message must be a string or strict binding"));
|
|
10486
10344
|
if (body.target !== void 0)
|
|
10487
10345
|
validateOverlayTarget(body.target, `${path2}.event.target`, issues);
|
|
10346
|
+
} else if (branch === "function") {
|
|
10347
|
+
rejectUnknownKeys(body, ["ref"], `${path2}.function`, issues);
|
|
10348
|
+
if (!isFunctionActionRef(body.ref))
|
|
10349
|
+
issues.push(issue2(`${path2}.function.ref`, "INVALID_FUNCTION_ACTION_REF", "function ref must be a dot-separated identifier up to 128 characters"));
|
|
10488
10350
|
} else if (branch === "local") {
|
|
10489
10351
|
rejectUnknownKeys(body, ["name", "path", "value"], `${path2}.local`, issues);
|
|
10490
10352
|
if (body.name !== "data.set" || typeof body.path !== "string" || !isSafePath(body.path) || !Object.prototype.hasOwnProperty.call(body, "value"))
|
|
@@ -10511,6 +10373,9 @@ function validateAction(value, path2, issues, depth) {
|
|
|
10511
10373
|
validateAction(body.default, `${path2}.select.default`, issues, depth + 1);
|
|
10512
10374
|
}
|
|
10513
10375
|
}
|
|
10376
|
+
function isFunctionActionRef(value) {
|
|
10377
|
+
return typeof value === "string" && value.length <= 128 && /^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$/u.test(value);
|
|
10378
|
+
}
|
|
10514
10379
|
function validateClientAction(body, path2, issues) {
|
|
10515
10380
|
rejectUnknownKeys(body, ["name", "context"], path2, issues);
|
|
10516
10381
|
if (body.name === "ui.overlay.close") {
|
|
@@ -10531,14 +10396,50 @@ function validateClientAction(body, path2, issues) {
|
|
|
10531
10396
|
return;
|
|
10532
10397
|
}
|
|
10533
10398
|
if (body.name === "payment.trade-pay") {
|
|
10534
|
-
|
|
10535
|
-
issues.push(issue2(`${path2}.context`, "INVALID_CLIENT_ACTION_CONTEXT", "payment.trade-pay requires actionId and actionContext"));
|
|
10536
|
-
else
|
|
10537
|
-
validateActionDynamicValue(body.context.actionContext, `${path2}.context.actionContext`, issues);
|
|
10399
|
+
validatePaymentActionContext(body.context, `${path2}.context`, issues);
|
|
10538
10400
|
return;
|
|
10539
10401
|
}
|
|
10540
10402
|
issues.push(issue2(`${path2}.name`, "UNSUPPORTED_CLIENT_ACTION", `unsupported client action ${String(body.name)}`));
|
|
10541
10403
|
}
|
|
10404
|
+
function validatePaymentActionContext(value, path2, issues) {
|
|
10405
|
+
if (!isPlainRecord(value)) {
|
|
10406
|
+
issues.push(issue2(path2, "INVALID_CLIENT_ACTION_CONTEXT", "payment.trade-pay requires createTrade and onSuccess"));
|
|
10407
|
+
return;
|
|
10408
|
+
}
|
|
10409
|
+
rejectUnknownKeys(value, ["createTrade", "onSuccess"], path2, issues);
|
|
10410
|
+
const createTrade = isPlainRecord(value.createTrade) ? value.createTrade : void 0;
|
|
10411
|
+
if (!createTrade) {
|
|
10412
|
+
issues.push(issue2(`${path2}.createTrade`, "INVALID_PAYMENT_CREATE_TRADE", "createTrade must be an object"));
|
|
10413
|
+
} else {
|
|
10414
|
+
rejectUnknownKeys(createTrade, ["mcpResourceId", "toolName", "input", "tradeNoPath"], `${path2}.createTrade`, issues);
|
|
10415
|
+
if (!isSafeIdentifier(createTrade.mcpResourceId))
|
|
10416
|
+
issues.push(issue2(`${path2}.createTrade.mcpResourceId`, "INVALID_MCP_RESOURCE_ID", "mcpResourceId is invalid"));
|
|
10417
|
+
if (typeof createTrade.toolName !== "string" || !createTrade.toolName.trim())
|
|
10418
|
+
issues.push(issue2(`${path2}.createTrade.toolName`, "INVALID_TOOL_NAME", "toolName is required"));
|
|
10419
|
+
if (!isPlainRecord(createTrade.input))
|
|
10420
|
+
issues.push(issue2(`${path2}.createTrade.input`, "INVALID_PAYMENT_INPUT", "input must be an object"));
|
|
10421
|
+
else
|
|
10422
|
+
validateActionDynamicValue(createTrade.input, `${path2}.createTrade.input`, issues);
|
|
10423
|
+
if (!isSafeJsonPointer(createTrade.tradeNoPath))
|
|
10424
|
+
issues.push(issue2(`${path2}.createTrade.tradeNoPath`, "INVALID_TRADE_NO_PATH", "tradeNoPath must be an absolute JSON Pointer"));
|
|
10425
|
+
}
|
|
10426
|
+
const onSuccess = isPlainRecord(value.onSuccess) ? value.onSuccess : void 0;
|
|
10427
|
+
const event = onSuccess && isPlainRecord(onSuccess.event) ? onSuccess.event : void 0;
|
|
10428
|
+
if (!onSuccess || !event) {
|
|
10429
|
+
issues.push(issue2(`${path2}.onSuccess`, "INVALID_PAYMENT_SUCCESS_EVENT", "onSuccess.event is required"));
|
|
10430
|
+
return;
|
|
10431
|
+
}
|
|
10432
|
+
rejectUnknownKeys(onSuccess, ["event"], `${path2}.onSuccess`, issues);
|
|
10433
|
+
rejectUnknownKeys(event, ["name", "context", "x2-user-message"], `${path2}.onSuccess.event`, issues);
|
|
10434
|
+
if (typeof event.name !== "string" || !/^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$/.test(event.name) || event.name.length > 96)
|
|
10435
|
+
issues.push(issue2(`${path2}.onSuccess.event.name`, "INVALID_ACTION_NAME", "event name is invalid"));
|
|
10436
|
+
if (event.context !== void 0 && !isPlainRecord(event.context))
|
|
10437
|
+
issues.push(issue2(`${path2}.onSuccess.event.context`, "INVALID_ACTION_CONTEXT", "event context must be an object"));
|
|
10438
|
+
else if (event.context !== void 0)
|
|
10439
|
+
validateActionDynamicValue(event.context, `${path2}.onSuccess.event.context`, issues);
|
|
10440
|
+
if (typeof event["x2-user-message"] !== "string" || !event["x2-user-message"].trim())
|
|
10441
|
+
issues.push(issue2(`${path2}.onSuccess.event.x2-user-message`, "INVALID_ACTION_CONTEXT", "x2-user-message must be a non-empty string"));
|
|
10442
|
+
}
|
|
10542
10443
|
function validateActionDynamicValue(value, path2, issues) {
|
|
10543
10444
|
if (Array.isArray(value)) {
|
|
10544
10445
|
value.forEach((entry, index) => validateActionDynamicValue(entry, `${path2}[${index}]`, issues));
|
|
@@ -10757,7 +10658,7 @@ var FIELD_SOURCES = [
|
|
|
10757
10658
|
"runtime-context",
|
|
10758
10659
|
"local"
|
|
10759
10660
|
];
|
|
10760
|
-
var
|
|
10661
|
+
var RUNTIME_CONTEXT_KEYS = /* @__PURE__ */ new Set([
|
|
10761
10662
|
"agentId",
|
|
10762
10663
|
"tenantId",
|
|
10763
10664
|
"threadId",
|
|
@@ -10837,6 +10738,9 @@ function getA2UICardResourceConfigIssues(value) {
|
|
|
10837
10738
|
}
|
|
10838
10739
|
function getA2UICardResourceConfigV3Issues(value) {
|
|
10839
10740
|
const issues = [];
|
|
10741
|
+
if (value.actionBindings !== void 0) {
|
|
10742
|
+
issues.push(issue2("config.actionBindings", "LEGACY_PAYMENT_ACTION_BINDINGS_UNSUPPORTED", "payment actions must be defined inline in card JSON"));
|
|
10743
|
+
}
|
|
10840
10744
|
exactKeys(value, [
|
|
10841
10745
|
"schemaVersion",
|
|
10842
10746
|
"revision",
|
|
@@ -10847,7 +10751,6 @@ function getA2UICardResourceConfigV3Issues(value) {
|
|
|
10847
10751
|
"fields",
|
|
10848
10752
|
"dataSources",
|
|
10849
10753
|
"bindings",
|
|
10850
|
-
"actionBindings",
|
|
10851
10754
|
"a2uiTemplate"
|
|
10852
10755
|
], "config", issues);
|
|
10853
10756
|
if (value.schemaVersion !== "3" && value.schemaVersion !== "4") {
|
|
@@ -10871,11 +10774,6 @@ function getA2UICardResourceConfigV3Issues(value) {
|
|
|
10871
10774
|
issues.push(...getA2UIV4ProtocolIssues(card, "config.a2uiTemplate.content"));
|
|
10872
10775
|
issues.push(...getPersistedV4ProtocolIssues(card, "config.a2uiTemplate.content"));
|
|
10873
10776
|
}
|
|
10874
|
-
const actionBindings = value.actionBindings === void 0 ? [] : value.actionBindings;
|
|
10875
|
-
issues.push(...getA2UICardActionBindingIssues(actionBindings, "config.actionBindings"));
|
|
10876
|
-
if (card && Array.isArray(actionBindings)) {
|
|
10877
|
-
issues.push(...getA2UIPaymentActionReferenceIssues(card, actionBindings, "config.a2uiTemplate.content"));
|
|
10878
|
-
}
|
|
10879
10777
|
if (fields && dataSources && bindings) {
|
|
10880
10778
|
validateV3Coverage(fields, dataSources, bindings, issues, { card });
|
|
10881
10779
|
}
|
|
@@ -11095,7 +10993,7 @@ function validateFieldSource(candidate, itemPath, issues) {
|
|
|
11095
10993
|
issues.push(issue2(`${itemPath}.sourcePath`, "UNEXPECTED_FIELD_SOURCE_PATH", "sourcePath is only allowed for invocation fields"));
|
|
11096
10994
|
}
|
|
11097
10995
|
if (candidate.source === "runtime-context") {
|
|
11098
|
-
if (!
|
|
10996
|
+
if (!RUNTIME_CONTEXT_KEYS.has(candidate.runtimeContextKey)) {
|
|
11099
10997
|
issues.push(issue2(`${itemPath}.runtimeContextKey`, "INVALID_RUNTIME_CONTEXT", "runtime-context fields require an allowed runtimeContextKey"));
|
|
11100
10998
|
}
|
|
11101
10999
|
} else if (candidate.runtimeContextKey !== void 0) {
|
|
@@ -11147,11 +11045,11 @@ function validateResolverCalls(value, path2, issues) {
|
|
|
11147
11045
|
issues.push(issue2(`${itemPath}.mcpResourceId`, "INVALID_MCP_RESOURCE_ID", "mcpResourceId is invalid"));
|
|
11148
11046
|
}
|
|
11149
11047
|
requiredString(candidate.toolName, `${itemPath}.toolName`, issues);
|
|
11150
|
-
|
|
11048
|
+
validateInputBindings(candidate.inputBindings, `${itemPath}.inputBindings`, issues);
|
|
11151
11049
|
});
|
|
11152
11050
|
return value;
|
|
11153
11051
|
}
|
|
11154
|
-
function
|
|
11052
|
+
function validateInputBindings(value, path2, issues) {
|
|
11155
11053
|
if (!Array.isArray(value)) {
|
|
11156
11054
|
issues.push(issue2(path2, "INVALID_INPUT_BINDINGS", "inputBindings must be an array"));
|
|
11157
11055
|
return void 0;
|
|
@@ -11171,11 +11069,11 @@ function validateInputBindings2(value, path2, issues) {
|
|
|
11171
11069
|
} else {
|
|
11172
11070
|
paths.add(candidate.inputPath);
|
|
11173
11071
|
}
|
|
11174
|
-
|
|
11072
|
+
validateInputSource(candidate.source, `${itemPath}.source`, issues);
|
|
11175
11073
|
});
|
|
11176
11074
|
return value;
|
|
11177
11075
|
}
|
|
11178
|
-
function
|
|
11076
|
+
function validateInputSource(value, path2, issues) {
|
|
11179
11077
|
if (!isPlainRecord(value)) {
|
|
11180
11078
|
issues.push(issue2(path2, "INVALID_INPUT_SOURCE", "source must be an object"));
|
|
11181
11079
|
return;
|
|
@@ -11189,7 +11087,7 @@ function validateInputSource2(value, path2, issues) {
|
|
|
11189
11087
|
}
|
|
11190
11088
|
if (value.kind === "runtime-context") {
|
|
11191
11089
|
exactKeys(value, ["kind", "key"], path2, issues);
|
|
11192
|
-
if (!
|
|
11090
|
+
if (!RUNTIME_CONTEXT_KEYS.has(value.key)) {
|
|
11193
11091
|
issues.push(issue2(`${path2}.key`, "INVALID_RUNTIME_CONTEXT", "runtime context key is invalid"));
|
|
11194
11092
|
}
|
|
11195
11093
|
return;
|
|
@@ -11556,6 +11454,155 @@ function valueType2(value) {
|
|
|
11556
11454
|
return typeof value === "object" ? "object" : typeof value;
|
|
11557
11455
|
}
|
|
11558
11456
|
|
|
11457
|
+
// ../agent-a2ui-card-contract/dist/esm/payment-action.js
|
|
11458
|
+
var A2UIActionContractError = class extends Error {
|
|
11459
|
+
constructor(issues) {
|
|
11460
|
+
super(issues.join("; "));
|
|
11461
|
+
Object.defineProperty(this, "issues", {
|
|
11462
|
+
enumerable: true,
|
|
11463
|
+
configurable: true,
|
|
11464
|
+
writable: true,
|
|
11465
|
+
value: issues
|
|
11466
|
+
});
|
|
11467
|
+
Object.defineProperty(this, "code", {
|
|
11468
|
+
enumerable: true,
|
|
11469
|
+
configurable: true,
|
|
11470
|
+
writable: true,
|
|
11471
|
+
value: "A2UI_ACTION_CONTRACT_INVALID"
|
|
11472
|
+
});
|
|
11473
|
+
this.name = "A2UIActionContractError";
|
|
11474
|
+
}
|
|
11475
|
+
};
|
|
11476
|
+
function parseA2UIActionExecuteRequest(value) {
|
|
11477
|
+
const issues = getA2UIActionExecuteRequestIssues(value);
|
|
11478
|
+
if (issues.length)
|
|
11479
|
+
throw new A2UIActionContractError(issues);
|
|
11480
|
+
return JSON.parse(JSON.stringify(value));
|
|
11481
|
+
}
|
|
11482
|
+
function getA2UIActionExecuteRequestIssues(value) {
|
|
11483
|
+
if (!isPlainRecord(value))
|
|
11484
|
+
return ["request must be an object"];
|
|
11485
|
+
const issues = [];
|
|
11486
|
+
rejectExtraKeys(value, ["schemaVersion", "agentId", "threadId", "invocationId", "source", "input"], "request", issues);
|
|
11487
|
+
if (value.schemaVersion !== "2")
|
|
11488
|
+
issues.push("schemaVersion must be 2");
|
|
11489
|
+
requiredIdentifier(value.agentId, "agentId", issues);
|
|
11490
|
+
requiredIdentifier(value.threadId, "threadId", issues);
|
|
11491
|
+
requiredIdentifier(value.invocationId, "invocationId", issues);
|
|
11492
|
+
validateSource(value.source, issues);
|
|
11493
|
+
if (!cloneJsonObject(value.input))
|
|
11494
|
+
issues.push("input must be a JSON object");
|
|
11495
|
+
return issues;
|
|
11496
|
+
}
|
|
11497
|
+
function validateSource(value, issues) {
|
|
11498
|
+
if (!isPlainRecord(value)) {
|
|
11499
|
+
issues.push("source must be an object");
|
|
11500
|
+
return;
|
|
11501
|
+
}
|
|
11502
|
+
rejectExtraKeys(value, ["cardRef", "cardRevision", "cardInstanceId", "surfaceId", "componentId"], "source", issues);
|
|
11503
|
+
requiredIdentifier(value.cardRef, "source.cardRef", issues);
|
|
11504
|
+
requiredIdentifier(value.cardRevision, "source.cardRevision", issues);
|
|
11505
|
+
requiredText(value.cardInstanceId, "source.cardInstanceId", issues);
|
|
11506
|
+
requiredIdentifier(value.surfaceId, "source.surfaceId", issues);
|
|
11507
|
+
requiredIdentifier(value.componentId, "source.componentId", issues);
|
|
11508
|
+
}
|
|
11509
|
+
function rejectExtraKeys(value, allowed, path2, issues) {
|
|
11510
|
+
const allowedKeys = new Set(allowed);
|
|
11511
|
+
for (const key of Object.keys(value))
|
|
11512
|
+
if (!allowedKeys.has(key))
|
|
11513
|
+
issues.push(`${path2}.${key} is not allowed`);
|
|
11514
|
+
}
|
|
11515
|
+
function requiredIdentifier(value, path2, issues) {
|
|
11516
|
+
if (!isSafeIdentifier(value))
|
|
11517
|
+
issues.push(`${path2} must be a safe identifier`);
|
|
11518
|
+
}
|
|
11519
|
+
function requiredText(value, path2, issues) {
|
|
11520
|
+
if (typeof value !== "string" || !value.trim() || value.length > 256) {
|
|
11521
|
+
issues.push(`${path2} must be a non-empty string up to 256 characters`);
|
|
11522
|
+
}
|
|
11523
|
+
}
|
|
11524
|
+
|
|
11525
|
+
// ../agent-a2ui-card-contract/dist/esm/function-action.js
|
|
11526
|
+
function parseA2UIFunctionMcpExecuteRequest(value) {
|
|
11527
|
+
const issues = [];
|
|
11528
|
+
if (!isPlainRecord(value))
|
|
11529
|
+
throw new Error("request must be an object");
|
|
11530
|
+
rejectExtraKeys2(value, [
|
|
11531
|
+
"schemaVersion",
|
|
11532
|
+
"agentId",
|
|
11533
|
+
"threadId",
|
|
11534
|
+
"invocationId",
|
|
11535
|
+
"callId",
|
|
11536
|
+
"buildId",
|
|
11537
|
+
"action",
|
|
11538
|
+
"source",
|
|
11539
|
+
"tool"
|
|
11540
|
+
], "request", issues);
|
|
11541
|
+
if (value.schemaVersion !== "1")
|
|
11542
|
+
issues.push("schemaVersion must be 1");
|
|
11543
|
+
for (const key of [
|
|
11544
|
+
"agentId",
|
|
11545
|
+
"threadId",
|
|
11546
|
+
"invocationId",
|
|
11547
|
+
"callId"
|
|
11548
|
+
]) {
|
|
11549
|
+
if (!isSafeIdentifier(value[key]))
|
|
11550
|
+
issues.push(`${key} must be a safe identifier`);
|
|
11551
|
+
}
|
|
11552
|
+
if (value.buildId !== void 0 && (typeof value.buildId !== "string" || !/^[a-f0-9]{64}$/u.test(value.buildId))) {
|
|
11553
|
+
issues.push("buildId must be a sha256 digest");
|
|
11554
|
+
}
|
|
11555
|
+
if (!isPlainRecord(value.action))
|
|
11556
|
+
issues.push("action must be an object");
|
|
11557
|
+
else {
|
|
11558
|
+
rejectExtraKeys2(value.action, ["ref"], "action", issues);
|
|
11559
|
+
if (!isFunctionRef(value.action.ref))
|
|
11560
|
+
issues.push("action.ref is invalid");
|
|
11561
|
+
}
|
|
11562
|
+
validateSource2(value.source, issues);
|
|
11563
|
+
if (!isPlainRecord(value.tool))
|
|
11564
|
+
issues.push("tool must be an object");
|
|
11565
|
+
else {
|
|
11566
|
+
rejectExtraKeys2(value.tool, ["mcpResourceId", "toolName", "input"], "tool", issues);
|
|
11567
|
+
if (!isSafeIdentifier(value.tool.mcpResourceId))
|
|
11568
|
+
issues.push("tool.mcpResourceId is invalid");
|
|
11569
|
+
if (!isSafeIdentifier(value.tool.toolName))
|
|
11570
|
+
issues.push("tool.toolName is invalid");
|
|
11571
|
+
if (!cloneJsonObject(value.tool.input))
|
|
11572
|
+
issues.push("tool.input must be a JSON object");
|
|
11573
|
+
}
|
|
11574
|
+
if (issues.length)
|
|
11575
|
+
throw new Error(issues.join("; "));
|
|
11576
|
+
return JSON.parse(JSON.stringify(value));
|
|
11577
|
+
}
|
|
11578
|
+
function validateSource2(value, issues) {
|
|
11579
|
+
if (!isPlainRecord(value))
|
|
11580
|
+
return void issues.push("source must be an object");
|
|
11581
|
+
rejectExtraKeys2(value, ["cardRef", "cardRevision", "cardInstanceId", "surfaceId", "componentId"], "source", issues);
|
|
11582
|
+
for (const key of [
|
|
11583
|
+
"cardRef",
|
|
11584
|
+
"cardRevision",
|
|
11585
|
+
"surfaceId",
|
|
11586
|
+
"componentId"
|
|
11587
|
+
]) {
|
|
11588
|
+
if (!isSafeIdentifier(value[key]))
|
|
11589
|
+
issues.push(`source.${key} is invalid`);
|
|
11590
|
+
}
|
|
11591
|
+
if (typeof value.cardInstanceId !== "string" || !value.cardInstanceId.trim()) {
|
|
11592
|
+
issues.push("source.cardInstanceId is invalid");
|
|
11593
|
+
}
|
|
11594
|
+
}
|
|
11595
|
+
function isFunctionRef(value) {
|
|
11596
|
+
return typeof value === "string" && value.length <= 128 && /^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$/u.test(value);
|
|
11597
|
+
}
|
|
11598
|
+
function rejectExtraKeys2(value, allowed, path2, issues) {
|
|
11599
|
+
const keys = new Set(allowed);
|
|
11600
|
+
for (const key of Object.keys(value)) {
|
|
11601
|
+
if (!keys.has(key))
|
|
11602
|
+
issues.push(`${path2}.${key} is not allowed`);
|
|
11603
|
+
}
|
|
11604
|
+
}
|
|
11605
|
+
|
|
11559
11606
|
// ../agent-a2ui-card-contract/dist/esm/presentation.js
|
|
11560
11607
|
var MAX_FIELDS = 32;
|
|
11561
11608
|
var MAX_ENTITIES = 10;
|
|
@@ -11672,1620 +11719,1471 @@ function truncateText(value) {
|
|
|
11672
11719
|
return { value: `${value.slice(0, MAX_TEXT_LENGTH - 1)}\u2026`, truncated: true };
|
|
11673
11720
|
}
|
|
11674
11721
|
|
|
11675
|
-
// ../linkque-agent-runtime/dist/esm/core/
|
|
11676
|
-
|
|
11677
|
-
|
|
11678
|
-
const
|
|
11679
|
-
|
|
11680
|
-
|
|
11681
|
-
spec = {
|
|
11682
|
-
serverName,
|
|
11683
|
-
agentId: "",
|
|
11684
|
-
argsMappingRules: [],
|
|
11685
|
-
allowedTools: void 0,
|
|
11686
|
-
resourceIds: [],
|
|
11687
|
-
description: ""
|
|
11688
|
-
};
|
|
11689
|
-
specs.set(serverName, spec);
|
|
11690
|
-
}
|
|
11691
|
-
return spec;
|
|
11692
|
-
};
|
|
11693
|
-
for (const resource of resources) {
|
|
11694
|
-
const serverName = resource.config.serverName ?? resource.resourceId;
|
|
11695
|
-
const spec = touch(serverName);
|
|
11696
|
-
spec.resourceIds.push(resource.resourceId);
|
|
11697
|
-
if (!spec.agentId) {
|
|
11698
|
-
spec.agentId = protocolAgentId ?? resource.config.agentId;
|
|
11699
|
-
}
|
|
11700
|
-
if (!spec.description)
|
|
11701
|
-
spec.description = resource.config.description;
|
|
11702
|
-
if (resource.resourceType === EResourceType.MCP) {
|
|
11703
|
-
for (const rule of resource.config.argsMappingRules ?? []) {
|
|
11704
|
-
spec.argsMappingRules.push(rule);
|
|
11705
|
-
}
|
|
11706
|
-
const allowed = resource.config.allowedTools;
|
|
11707
|
-
if (allowed && allowed.length > 0) {
|
|
11708
|
-
if (!spec.allowedTools)
|
|
11709
|
-
spec.allowedTools = /* @__PURE__ */ new Set();
|
|
11710
|
-
for (const toolName of allowed)
|
|
11711
|
-
spec.allowedTools.add(toolName);
|
|
11712
|
-
}
|
|
11713
|
-
continue;
|
|
11714
|
-
}
|
|
11715
|
-
if (!spec.allowedTools)
|
|
11716
|
-
spec.allowedTools = /* @__PURE__ */ new Set();
|
|
11717
|
-
spec.allowedTools.add(resource.config.toolName);
|
|
11718
|
-
for (const rule of resource.config.argsMappingRules ?? []) {
|
|
11719
|
-
const fullRule = {
|
|
11720
|
-
mcpToolName: resource.config.toolName,
|
|
11721
|
-
argJsonPath: rule.argJsonPath,
|
|
11722
|
-
disabled: rule.disabled,
|
|
11723
|
-
valueType: rule.valueType,
|
|
11724
|
-
...rule.value !== void 0 ? { value: rule.value } : {}
|
|
11725
|
-
};
|
|
11726
|
-
spec.argsMappingRules.push(fullRule);
|
|
11727
|
-
}
|
|
11728
|
-
}
|
|
11729
|
-
return [...specs.values()];
|
|
11722
|
+
// ../linkque-agent-runtime/dist/esm/core/mcpNaming.js
|
|
11723
|
+
import { createHash } from "node:crypto";
|
|
11724
|
+
function stableToolName(prefix, raw) {
|
|
11725
|
+
const readable = raw.replace(/[^A-Za-z0-9_-]+/gu, "_").replace(/^_+|_+$/gu, "").slice(0, 38) || "resource";
|
|
11726
|
+
const digest = createHash("sha256").update(raw).digest("hex").slice(0, 10);
|
|
11727
|
+
return `${prefix}_${readable}_${digest}`;
|
|
11730
11728
|
}
|
|
11731
11729
|
|
|
11732
|
-
// ../linkque-agent-runtime/dist/esm/core/
|
|
11733
|
-
var
|
|
11734
|
-
|
|
11735
|
-
|
|
11736
|
-
|
|
11737
|
-
|
|
11738
|
-
|
|
11739
|
-
|
|
11740
|
-
|
|
11741
|
-
|
|
11742
|
-
|
|
11743
|
-
|
|
11744
|
-
|
|
11745
|
-
|
|
11746
|
-
|
|
11747
|
-
|
|
11748
|
-
|
|
11749
|
-
|
|
11750
|
-
|
|
11751
|
-
|
|
11752
|
-
|
|
11753
|
-
|
|
11754
|
-
|
|
11755
|
-
|
|
11756
|
-
debugLog;
|
|
11757
|
-
constructor(provider, specs, options = {}) {
|
|
11758
|
-
this.provider = provider;
|
|
11759
|
-
this.specs = specs;
|
|
11760
|
-
this.specByServerName = new Map(specs.map((spec) => [spec.serverName, spec]));
|
|
11761
|
-
this.specByResourceId = new Map(specs.flatMap((spec) => spec.resourceIds.map((id) => [id, spec])));
|
|
11762
|
-
this.debugLog = options.debugLog ?? (() => {
|
|
11763
|
-
});
|
|
11764
|
-
}
|
|
11765
|
-
/**
|
|
11766
|
-
* 产单个 `mcp` 代理工具描述符,注入 `agentReq.tools`。
|
|
11767
|
-
* `execute` 闭包按入参字段派发到 status/connect/list/search/describe/call。
|
|
11768
|
-
*/
|
|
11769
|
-
asTool() {
|
|
11770
|
-
return {
|
|
11771
|
-
name: MCP_PROXY_TOOL_NAME,
|
|
11772
|
-
modelName: MCP_PROXY_TOOL_NAME,
|
|
11773
|
-
label: "MCP",
|
|
11774
|
-
description: MCP_PROXY_DESCRIPTION,
|
|
11775
|
-
inputSchema: MCP_PROXY_INPUT_SCHEMA,
|
|
11776
|
-
execute: (args, signal) => this.dispatch(args, signal)
|
|
11777
|
-
};
|
|
11778
|
-
}
|
|
11779
|
-
/** {@link IMcpToolRegistry.resolve}:按 (resourceId, toolName) 解析可调用句柄。 */
|
|
11780
|
-
async resolve(resourceId, toolName, signal) {
|
|
11781
|
-
const spec = this.findSpec(resourceId);
|
|
11782
|
-
if (!spec)
|
|
11783
|
-
return void 0;
|
|
11784
|
-
const entry = await this.ensureEntry(spec.serverName, signal);
|
|
11785
|
-
if (!entry || !entry.server)
|
|
11786
|
-
return void 0;
|
|
11787
|
-
const tool = entry.tools.find((t) => t.name === toolName);
|
|
11788
|
-
if (!tool)
|
|
11789
|
-
return void 0;
|
|
11730
|
+
// ../linkque-agent-runtime/dist/esm/core/cardSemanticSnapshot.js
|
|
11731
|
+
var MAX_BYTES = 6e3;
|
|
11732
|
+
var MAX_LABEL = 160;
|
|
11733
|
+
function createCardSemanticSnapshot(receipt, fields) {
|
|
11734
|
+
const groups = receipt.coverage.map((coverage) => {
|
|
11735
|
+
const field = fields.find((f) => f.fieldId === coverage.fieldId);
|
|
11736
|
+
const entities = receipt.entities.filter((e) => e.fieldId === coverage.fieldId);
|
|
11737
|
+
const echo = field?.presentation?.visibility === "echo";
|
|
11738
|
+
const attributes = echo ? [...new Set(entities.flatMap((e) => Object.keys(e.attributes ?? {})))] : [];
|
|
11739
|
+
const hasValue = echo && entities.some((e) => Object.hasOwn(e, "value"));
|
|
11740
|
+
const hasLabel = echo && entities.some((e) => !Object.hasOwn(e, "value"));
|
|
11741
|
+
const columns = [
|
|
11742
|
+
"\u6761\u76EE\u5F15\u7528",
|
|
11743
|
+
...hasLabel ? [columnLabel(field, field?.presentation?.labelPath, "\u540D\u79F0")] : [],
|
|
11744
|
+
...hasValue ? [short(coverage.label)] : [],
|
|
11745
|
+
...attributes.map((path2) => columnLabel(field, path2, path2))
|
|
11746
|
+
];
|
|
11747
|
+
const rows = entities.map((e) => [
|
|
11748
|
+
e.ref,
|
|
11749
|
+
...hasLabel ? [e.label] : [],
|
|
11750
|
+
...hasValue ? [e.value ?? null] : [],
|
|
11751
|
+
...attributes.map((path2) => e.attributes?.[path2] ?? null)
|
|
11752
|
+
]);
|
|
11753
|
+
const total = coverage.itemCount ?? (coverage.present ? 1 : 0);
|
|
11790
11754
|
return {
|
|
11791
|
-
|
|
11792
|
-
|
|
11793
|
-
|
|
11755
|
+
field: coverage.fieldId,
|
|
11756
|
+
label: short(coverage.label),
|
|
11757
|
+
present: coverage.present,
|
|
11758
|
+
total,
|
|
11759
|
+
included: rows.length,
|
|
11760
|
+
complete: echo && coverage.present && rows.length === total && !entities.some((e) => e.truncated) && !receipt.omissions.some((o) => o.fieldId === coverage.fieldId),
|
|
11761
|
+
columns,
|
|
11762
|
+
rows
|
|
11794
11763
|
};
|
|
11795
|
-
}
|
|
11796
|
-
|
|
11797
|
-
|
|
11798
|
-
|
|
11799
|
-
|
|
11800
|
-
|
|
11801
|
-
|
|
11802
|
-
|
|
11803
|
-
|
|
11804
|
-
|
|
11805
|
-
|
|
11806
|
-
|
|
11807
|
-
}
|
|
11764
|
+
});
|
|
11765
|
+
const snapshot = {
|
|
11766
|
+
version: 1,
|
|
11767
|
+
groups,
|
|
11768
|
+
omissions: receipt.omissions.map((o) => ({ ...o }))
|
|
11769
|
+
};
|
|
11770
|
+
while (bytes(snapshot) > MAX_BYTES) {
|
|
11771
|
+
let group;
|
|
11772
|
+
for (let i = groups.length - 1; i >= 0; i--) {
|
|
11773
|
+
if (groups[i].rows.length > 0) {
|
|
11774
|
+
group = groups[i];
|
|
11775
|
+
break;
|
|
11808
11776
|
}
|
|
11809
|
-
}));
|
|
11810
|
-
}
|
|
11811
|
-
// —— 内部:连接与缓存 ——
|
|
11812
|
-
/** 确保某 server 已连;返回 connected 条目,依失败返回 null(不抛错)。 */
|
|
11813
|
-
async ensureEntry(serverName, signal) {
|
|
11814
|
-
const existing = this.entries.get(serverName);
|
|
11815
|
-
if (existing) {
|
|
11816
|
-
return existing.status === "connected" ? existing : null;
|
|
11817
11777
|
}
|
|
11818
|
-
|
|
11819
|
-
if (
|
|
11820
|
-
|
|
11821
|
-
|
|
11822
|
-
|
|
11823
|
-
|
|
11824
|
-
|
|
11825
|
-
|
|
11826
|
-
|
|
11778
|
+
snapshot.truncated = true;
|
|
11779
|
+
if (group) {
|
|
11780
|
+
group.rows.pop();
|
|
11781
|
+
group.included = group.rows.length;
|
|
11782
|
+
group.complete = false;
|
|
11783
|
+
} else if (groups.length) {
|
|
11784
|
+
groups.pop();
|
|
11785
|
+
} else if (snapshot.omissions.length) {
|
|
11786
|
+
snapshot.omissions.pop();
|
|
11787
|
+
} else
|
|
11788
|
+
break;
|
|
11827
11789
|
}
|
|
11828
|
-
|
|
11829
|
-
|
|
11830
|
-
|
|
11831
|
-
|
|
11832
|
-
|
|
11833
|
-
|
|
11834
|
-
|
|
11835
|
-
|
|
11836
|
-
|
|
11837
|
-
|
|
11838
|
-
|
|
11839
|
-
|
|
11840
|
-
|
|
11841
|
-
|
|
11842
|
-
|
|
11843
|
-
|
|
11844
|
-
|
|
11845
|
-
|
|
11846
|
-
|
|
11847
|
-
config: {
|
|
11848
|
-
agentId: spec.agentId,
|
|
11849
|
-
serverName: spec.serverName,
|
|
11850
|
-
description: spec.description,
|
|
11851
|
-
argsMappingRules: spec.argsMappingRules
|
|
11852
|
-
}
|
|
11853
|
-
};
|
|
11854
|
-
const servers = await this.provider.servers([synthetic]);
|
|
11855
|
-
const server = servers[0];
|
|
11856
|
-
if (!server) {
|
|
11857
|
-
throw new Error(`provider returned no server for ${spec.serverName}`);
|
|
11858
|
-
}
|
|
11859
|
-
const all = await server.listTools();
|
|
11860
|
-
const tools = spec.allowedTools ? all.filter((tool) => spec.allowedTools.has(tool.name)) : all;
|
|
11861
|
-
const entry = { server, tools, status: "connected" };
|
|
11862
|
-
this.entries.set(serverName, entry);
|
|
11863
|
-
this.debugLog(`[mcp] server ${spec.serverName} connected (${tools.length}/${all.length} tools)`, EDebugLogScope.MCP);
|
|
11864
|
-
return entry;
|
|
11865
|
-
} catch (error) {
|
|
11866
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
11867
|
-
const entry = {
|
|
11868
|
-
server: null,
|
|
11869
|
-
tools: [],
|
|
11870
|
-
status: "failed",
|
|
11871
|
-
error: message
|
|
11872
|
-
};
|
|
11873
|
-
this.entries.set(serverName, entry);
|
|
11874
|
-
this.debugLog(`[mcp] server ${spec.serverName} connect failed: ${message}`, EDebugLogScope.MCP);
|
|
11875
|
-
return entry;
|
|
11876
|
-
}
|
|
11790
|
+
return snapshot;
|
|
11791
|
+
}
|
|
11792
|
+
function bytes(value) {
|
|
11793
|
+
return Buffer.byteLength(JSON.stringify(value));
|
|
11794
|
+
}
|
|
11795
|
+
function short(value) {
|
|
11796
|
+
return value.length > MAX_LABEL ? `${value.slice(0, MAX_LABEL - 1)}\u2026` : value;
|
|
11797
|
+
}
|
|
11798
|
+
function columnLabel(field, path2, fallback) {
|
|
11799
|
+
if (!field || !path2)
|
|
11800
|
+
return short(fallback);
|
|
11801
|
+
let schema = field.type === "array" ? field.valueSchema.items : field.valueSchema;
|
|
11802
|
+
for (const token of path2.slice(1).split("/").map((t) => t.replaceAll("~1", "/").replaceAll("~0", "~"))) {
|
|
11803
|
+
if (!schema || typeof schema !== "object" || Array.isArray(schema))
|
|
11804
|
+
return short(fallback);
|
|
11805
|
+
const properties = schema.properties;
|
|
11806
|
+
if (!properties || typeof properties !== "object" || Array.isArray(properties) || !Object.hasOwn(properties, token))
|
|
11807
|
+
return short(fallback);
|
|
11808
|
+
schema = properties[token];
|
|
11877
11809
|
}
|
|
11878
|
-
|
|
11879
|
-
|
|
11880
|
-
|
|
11881
|
-
|
|
11882
|
-
|
|
11883
|
-
|
|
11884
|
-
|
|
11885
|
-
|
|
11886
|
-
|
|
11887
|
-
|
|
11888
|
-
|
|
11889
|
-
|
|
11890
|
-
|
|
11891
|
-
|
|
11892
|
-
|
|
11893
|
-
}
|
|
11894
|
-
if (params.tool !== void 0) {
|
|
11895
|
-
return this.executeCall(params.tool, params.args, params.server, signal);
|
|
11896
|
-
}
|
|
11897
|
-
if (params.server !== void 0) {
|
|
11898
|
-
return this.executeList(params.server, signal);
|
|
11899
|
-
}
|
|
11900
|
-
return this.executeStatus();
|
|
11810
|
+
const title = schema && typeof schema === "object" && !Array.isArray(schema) ? schema.title : void 0;
|
|
11811
|
+
return short(typeof title === "string" && title ? title : fallback);
|
|
11812
|
+
}
|
|
11813
|
+
|
|
11814
|
+
// ../linkque-agent-runtime/dist/esm/core/cardTools.js
|
|
11815
|
+
var FALLBACK_OBJECT_SCHEMA = {
|
|
11816
|
+
type: "object",
|
|
11817
|
+
additionalProperties: true
|
|
11818
|
+
};
|
|
11819
|
+
function createCardPresentationToolName(resourceId) {
|
|
11820
|
+
return stableToolName("a2ui_card", resourceId);
|
|
11821
|
+
}
|
|
11822
|
+
async function executeCardPresentation(resource, registry, runtimeContext, args, signal) {
|
|
11823
|
+
const config = parseCardConfig(resource);
|
|
11824
|
+
if (config.schemaVersion !== "2") {
|
|
11825
|
+
throw new Error(`Schema v${config.schemaVersion} cards must be executed through the card data-source loop`);
|
|
11901
11826
|
}
|
|
11902
|
-
|
|
11903
|
-
|
|
11904
|
-
|
|
11905
|
-
|
|
11906
|
-
|
|
11907
|
-
|
|
11908
|
-
|
|
11909
|
-
|
|
11910
|
-
|
|
11911
|
-
|
|
11912
|
-
|
|
11913
|
-
|
|
11914
|
-
if (entry.status === "connected") {
|
|
11915
|
-
connected += 1;
|
|
11916
|
-
totalTools += entry.tools.length;
|
|
11917
|
-
lines.push(`\u2713 ${label} (${entry.tools.length} tools)`);
|
|
11918
|
-
} else {
|
|
11919
|
-
lines.push(`\u2717 ${label} (failed${entry.error ? ": " + entry.error : ""})`);
|
|
11827
|
+
const invocation = cloneJsonObject2(args, "Presentation Tool arguments");
|
|
11828
|
+
const viewModel = await executeA2UICardResolver({
|
|
11829
|
+
resolver: config.resolver,
|
|
11830
|
+
fields: config.fields,
|
|
11831
|
+
presentationInputSchema: config.presentationInputSchema,
|
|
11832
|
+
argsSchema: config.a2uiTemplate.argsSchema,
|
|
11833
|
+
invocation,
|
|
11834
|
+
runtimeContext,
|
|
11835
|
+
tools: await Promise.all(config.resolver.calls.map(async (call) => {
|
|
11836
|
+
const handle = await registry.resolve(call.mcpResourceId, call.toolName, signal);
|
|
11837
|
+
if (!handle) {
|
|
11838
|
+
throw new Error(`MCP tool ${call.mcpResourceId}/${call.toolName} is unavailable`);
|
|
11920
11839
|
}
|
|
11921
|
-
|
|
11922
|
-
|
|
11923
|
-
|
|
11924
|
-
|
|
11925
|
-
|
|
11926
|
-
|
|
11840
|
+
const inputSchema = normalizeMcpInputSchema(handle.inputSchema);
|
|
11841
|
+
return {
|
|
11842
|
+
resourceId: call.mcpResourceId,
|
|
11843
|
+
toolName: call.toolName,
|
|
11844
|
+
// 旧 MCP server 可能只在开放平台元数据中声明 Schema,但
|
|
11845
|
+
// tools/list 未返回。运行时不因此拒绝调用:有实时 Schema 时用它
|
|
11846
|
+
// 做额外校验,缺失时依赖 CARD 已固化的字段 Schema 与最终
|
|
11847
|
+
// ViewModel Schema 校验。
|
|
11848
|
+
inputSchema,
|
|
11849
|
+
outputSchema: isJsonObject(handle.outputSchema) ? handle.outputSchema : FALLBACK_OBJECT_SCHEMA,
|
|
11850
|
+
call: async (input, callSignal) => {
|
|
11851
|
+
const result = await handle.execute(supplementMcpInput(input, invocation, config.presentationInputSchema, inputSchema), callSignal);
|
|
11852
|
+
return { structuredContent: readStructuredContent(result) };
|
|
11853
|
+
}
|
|
11854
|
+
};
|
|
11855
|
+
})),
|
|
11856
|
+
signal
|
|
11857
|
+
});
|
|
11858
|
+
return createPresentationResult(resource, config, viewModel);
|
|
11859
|
+
}
|
|
11860
|
+
async function executeCardPresentationFromSources(resource, runtimeContext, sourceResults, signal) {
|
|
11861
|
+
const config = parseCardConfig(resource);
|
|
11862
|
+
if (config.schemaVersion !== "3" && config.schemaVersion !== "4") {
|
|
11863
|
+
throw new Error("Source-result rendering is only available for schema v3/v4 cards");
|
|
11927
11864
|
}
|
|
11928
|
-
|
|
11929
|
-
|
|
11930
|
-
|
|
11931
|
-
if (!spec)
|
|
11932
|
-
return `Server "${server}" not found. Use mcp({}) to see available servers.`;
|
|
11933
|
-
this.entries.delete(spec.serverName);
|
|
11934
|
-
const entry = await this.ensureEntry(spec.serverName, signal);
|
|
11935
|
-
if (!entry) {
|
|
11936
|
-
const failed = this.entries.get(spec.serverName);
|
|
11937
|
-
const detail = failed?.error ? `: ${failed.error}` : "";
|
|
11938
|
-
return `Failed to connect to "${spec.serverName}"${detail}.`;
|
|
11939
|
-
}
|
|
11940
|
-
return this.formatToolList(spec.serverName, entry.tools);
|
|
11865
|
+
const missing = config.dataSources.filter((source) => source.required && !sourceResults.has(source.sourceId)).map((source) => source.sourceId);
|
|
11866
|
+
if (missing.length) {
|
|
11867
|
+
throw new Error(`Required card data sources are incomplete: ${missing.join(", ")}`);
|
|
11941
11868
|
}
|
|
11942
|
-
|
|
11943
|
-
|
|
11944
|
-
|
|
11945
|
-
|
|
11946
|
-
|
|
11947
|
-
|
|
11948
|
-
|
|
11949
|
-
|
|
11869
|
+
const resolver = {
|
|
11870
|
+
version: 2,
|
|
11871
|
+
calls: config.dataSources.filter((source) => sourceResults.has(source.sourceId)).map((source) => ({
|
|
11872
|
+
callId: source.sourceId,
|
|
11873
|
+
mcpResourceId: source.mcpResourceId,
|
|
11874
|
+
toolName: source.toolName,
|
|
11875
|
+
inputBindings: []
|
|
11876
|
+
})),
|
|
11877
|
+
outputBindings: config.bindings.filter((binding) => sourceResults.has(binding.sourceId)).map((binding) => ({
|
|
11878
|
+
fieldId: binding.fieldId,
|
|
11879
|
+
callId: binding.sourceId,
|
|
11880
|
+
transform: binding.transform
|
|
11881
|
+
}))
|
|
11882
|
+
};
|
|
11883
|
+
let viewModel;
|
|
11884
|
+
try {
|
|
11885
|
+
viewModel = await executeA2UICardResolver({
|
|
11886
|
+
resolver,
|
|
11887
|
+
fields: config.fields,
|
|
11888
|
+
presentationInputSchema: {
|
|
11889
|
+
type: "object",
|
|
11890
|
+
properties: {},
|
|
11891
|
+
additionalProperties: false
|
|
11892
|
+
},
|
|
11893
|
+
argsSchema: config.a2uiTemplate.argsSchema,
|
|
11894
|
+
invocation: {},
|
|
11895
|
+
runtimeContext,
|
|
11896
|
+
tools: config.dataSources.filter((source) => sourceResults.has(source.sourceId)).map((source) => ({
|
|
11897
|
+
resourceId: source.mcpResourceId,
|
|
11898
|
+
toolName: source.toolName,
|
|
11899
|
+
inputSchema: FALLBACK_OBJECT_SCHEMA,
|
|
11900
|
+
outputSchema: source.outputSchema ?? FALLBACK_OBJECT_SCHEMA,
|
|
11901
|
+
call: async () => ({
|
|
11902
|
+
structuredContent: sourceResults.get(source.sourceId)
|
|
11903
|
+
})
|
|
11904
|
+
})),
|
|
11905
|
+
signal
|
|
11906
|
+
});
|
|
11907
|
+
} catch (error) {
|
|
11908
|
+
if (error instanceof A2UICardResolverError) {
|
|
11909
|
+
const binding = config.bindings.find((candidate) => candidate.fieldId === error.fieldId);
|
|
11910
|
+
const field = config.fields.find((candidate) => candidate.fieldId === error.fieldId);
|
|
11911
|
+
throw new CardMappingError({
|
|
11912
|
+
code: error.code,
|
|
11913
|
+
cardId: resource.resourceId,
|
|
11914
|
+
fieldId: error.fieldId ?? "unknown",
|
|
11915
|
+
sourceId: binding?.sourceId ?? "unknown",
|
|
11916
|
+
expectedType: error.expectedType ?? field?.type ?? "unknown",
|
|
11917
|
+
actualType: error.actualType ?? "unknown",
|
|
11918
|
+
retryable: false
|
|
11919
|
+
});
|
|
11950
11920
|
}
|
|
11951
|
-
|
|
11921
|
+
throw error;
|
|
11952
11922
|
}
|
|
11953
|
-
|
|
11954
|
-
|
|
11955
|
-
|
|
11956
|
-
|
|
11957
|
-
|
|
11958
|
-
|
|
11959
|
-
|
|
11960
|
-
|
|
11961
|
-
|
|
11962
|
-
|
|
11963
|
-
|
|
11964
|
-
|
|
11965
|
-
|
|
11966
|
-
|
|
11967
|
-
|
|
11968
|
-
|
|
11969
|
-
|
|
11970
|
-
|
|
11971
|
-
|
|
11972
|
-
|
|
11973
|
-
|
|
11974
|
-
|
|
11975
|
-
|
|
11976
|
-
|
|
11977
|
-
|
|
11978
|
-
|
|
11979
|
-
|
|
11980
|
-
lines.push(` Parameters:
|
|
11981
|
-
${formatSchema(tool.inputSchema, " ")}`);
|
|
11923
|
+
return createPresentationResult(resource, config, viewModel);
|
|
11924
|
+
}
|
|
11925
|
+
function createPresentationResult(resource, config, viewModel) {
|
|
11926
|
+
const protocol = injectResolvedViewModel(JSON.parse(config.a2uiTemplate.content), viewModel);
|
|
11927
|
+
const cardInstanceId = `card-instance-${randomUUID()}`;
|
|
11928
|
+
const receipt = {
|
|
11929
|
+
version: 1,
|
|
11930
|
+
cardRef: resource.resourceId,
|
|
11931
|
+
cardRevision: config.revision,
|
|
11932
|
+
cardInstanceId,
|
|
11933
|
+
status: "emitted",
|
|
11934
|
+
...createA2UICardPresentationReceiptContent(config.fields, viewModel),
|
|
11935
|
+
responsePolicy: { verbatimEcho: "forbidden", hint: "acknowledge" }
|
|
11936
|
+
};
|
|
11937
|
+
receipt.semanticSnapshot = createCardSemanticSnapshot(receipt, config.fields);
|
|
11938
|
+
return {
|
|
11939
|
+
type: "linkque.presentation-tool-result",
|
|
11940
|
+
modelText: formatPresentationReceiptForModel(receipt),
|
|
11941
|
+
receipt,
|
|
11942
|
+
card: {
|
|
11943
|
+
type: "a2ui_card",
|
|
11944
|
+
itemId: cardInstanceId,
|
|
11945
|
+
payload: { protocol },
|
|
11946
|
+
metadata: {
|
|
11947
|
+
cardRef: resource.resourceId,
|
|
11948
|
+
cardRevision: config.revision,
|
|
11949
|
+
cardInstanceId
|
|
11982
11950
|
}
|
|
11983
|
-
lines.push("");
|
|
11984
|
-
}
|
|
11985
|
-
return lines.join("\n").trim();
|
|
11986
|
-
}
|
|
11987
|
-
/** `mcp({ searches: [...] })`:一次连接元数据并按 query 分组执行多组搜索。 */
|
|
11988
|
-
async executeBatchSearch(queries, serverHint) {
|
|
11989
|
-
const normalized = [
|
|
11990
|
-
...new Set(queries.map((query) => query.trim()).filter(Boolean))
|
|
11991
|
-
].slice(0, MCP_BATCH_SEARCH_MAX_ITEMS);
|
|
11992
|
-
if (normalized.length === 0) {
|
|
11993
|
-
return "Batch search requires at least one non-empty query.";
|
|
11994
|
-
}
|
|
11995
|
-
const specs = serverHint ? this.specsMatching(serverHint) : this.specs;
|
|
11996
|
-
await Promise.all(specs.map((spec) => this.ensureEntry(spec.serverName)));
|
|
11997
|
-
const results = await Promise.all(normalized.map((query) => this.executeSearch(query, serverHint)));
|
|
11998
|
-
return results.map((result, index) => `Query ${index + 1}: "${normalized[index]}"
|
|
11999
|
-
${result}`).join("\n\n");
|
|
12000
|
-
}
|
|
12001
|
-
/** `mcp({ describe: "tool" })`:输出工具所属 server + 参数 schema。 */
|
|
12002
|
-
async executeDescribe(toolName, serverHint, signal) {
|
|
12003
|
-
const found = await this.findTool(toolName, serverHint, signal);
|
|
12004
|
-
if (!found) {
|
|
12005
|
-
return `Tool "${toolName}" not found. Use mcp({ search: "..." }) to search.`;
|
|
12006
|
-
}
|
|
12007
|
-
const { server, tool } = found;
|
|
12008
|
-
const lines = [
|
|
12009
|
-
`${tool.name}`,
|
|
12010
|
-
`Server: ${server}`,
|
|
12011
|
-
"",
|
|
12012
|
-
tool.description ?? "(no description)"
|
|
12013
|
-
];
|
|
12014
|
-
if (tool.inputSchema) {
|
|
12015
|
-
lines.push("", `Parameters:
|
|
12016
|
-
${formatSchema(tool.inputSchema)}`);
|
|
12017
|
-
} else {
|
|
12018
|
-
lines.push("", "No parameters defined.");
|
|
12019
11951
|
}
|
|
12020
|
-
|
|
11952
|
+
};
|
|
11953
|
+
}
|
|
11954
|
+
function formatPresentationReceiptForModel(receipt) {
|
|
11955
|
+
return [
|
|
11956
|
+
"<a2ui-presentation-receipt>",
|
|
11957
|
+
JSON.stringify(projectPresentationReceiptForModel(receipt)),
|
|
11958
|
+
"</a2ui-presentation-receipt>",
|
|
11959
|
+
"\u5361\u7247\u5DF2\u53D1\u51FA\uFF08\u4E0D\u4EE3\u8868\u5BA2\u6237\u7AEF\u5DF2\u6E32\u67D3\uFF09\u3002\u8BED\u4E49\u5FEB\u7167\u6765\u81EA\u6388\u6743\u7684\u6700\u7EC8\u5361\u7247\u6570\u636E\uFF1Bcolumns \u4E0E rows \u6309\u4F4D\u7F6E\u5BF9\u5E94\uFF0Cnull \u8868\u793A\u7F3A\u5931\u6216\u7A7A\u503C\uFF0Ccomplete=false \u65F6\u4E0D\u5F97\u628A\u91C7\u6837\u5F53\u4F5C\u5168\u96C6\u3002\u4E0D\u8981\u7528 Markdown \u91CD\u590D\u5217\u51FA\u6216\u6539\u5199\u5361\u7247\u4E2D\u7684\u6761\u76EE\uFF1B\u53EA\u8865\u5145\u7528\u6237\u5C1A\u672A\u5F97\u5230\u7684\u6BD4\u8F83\u3001\u89E3\u91CA\u6216\u5FC5\u8981\u8FFD\u95EE\uFF0C\u7528\u6237\u660E\u786E\u8981\u6C42\u6587\u5B57\u8868\u683C\u65F6\u53EF\u4EE5\u63D0\u4F9B\u3002\u5FEB\u7167\u672A\u5305\u542B\u7684\u4FE1\u606F\u4FDD\u6301\u672A\u77E5\uFF0C\u6E90\u5DE5\u5177\u7684\u989D\u5916\u4FE1\u606F\u4E0D\u80FD\u79F0\u4E3A\u5361\u7247\u5DF2\u5C55\u793A\u5185\u5BB9\u3002"
|
|
11960
|
+
].join("\n");
|
|
11961
|
+
}
|
|
11962
|
+
function projectPresentationReceiptForModel(receipt) {
|
|
11963
|
+
if (!receipt.semanticSnapshot)
|
|
11964
|
+
return receipt;
|
|
11965
|
+
return {
|
|
11966
|
+
version: receipt.version,
|
|
11967
|
+
cardRef: receipt.cardRef,
|
|
11968
|
+
cardRevision: receipt.cardRevision,
|
|
11969
|
+
cardInstanceId: receipt.cardInstanceId,
|
|
11970
|
+
status: receipt.status,
|
|
11971
|
+
semanticSnapshot: receipt.semanticSnapshot
|
|
11972
|
+
};
|
|
11973
|
+
}
|
|
11974
|
+
function parseCardConfig(resource) {
|
|
11975
|
+
const parsed = parseA2UICardResourceConfig(resource.config);
|
|
11976
|
+
if (!parsed.success) {
|
|
11977
|
+
throw new Error(`Invalid CARD ${resource.resourceId}: ${parsed.issues.map((entry) => `${entry.path}: ${entry.message}`).join("; ")}`);
|
|
12021
11978
|
}
|
|
12022
|
-
|
|
12023
|
-
|
|
12024
|
-
|
|
12025
|
-
|
|
12026
|
-
|
|
12027
|
-
|
|
12028
|
-
|
|
12029
|
-
|
|
12030
|
-
|
|
12031
|
-
|
|
12032
|
-
|
|
12033
|
-
|
|
12034
|
-
|
|
12035
|
-
}
|
|
11979
|
+
return parsed.data;
|
|
11980
|
+
}
|
|
11981
|
+
function isAgentPresentationToolResult(value) {
|
|
11982
|
+
if (!isJsonObject(value))
|
|
11983
|
+
return false;
|
|
11984
|
+
return value.type === "linkque.presentation-tool-result" && typeof value.modelText === "string" && isJsonObject(value.card) && value.card.type === "a2ui_card";
|
|
11985
|
+
}
|
|
11986
|
+
function injectResolvedViewModel(protocol, viewModel) {
|
|
11987
|
+
const analysis = analyzeA2UIBindings(protocol);
|
|
11988
|
+
const declaredSurfaceIds = analysis.surfaceIds;
|
|
11989
|
+
const surfaceIds = Array.isArray(declaredSurfaceIds) && declaredSurfaceIds.every((surfaceId) => typeof surfaceId === "string") ? declaredSurfaceIds : collectA2UISurfaceIdsForLegacyContract(protocol);
|
|
11990
|
+
if (analysis.issues.length || surfaceIds.length === 0) {
|
|
11991
|
+
throw new Error("CARD template does not contain a valid A2UI surface");
|
|
12036
11992
|
}
|
|
12037
|
-
|
|
12038
|
-
|
|
12039
|
-
|
|
12040
|
-
|
|
12041
|
-
|
|
12042
|
-
|
|
12043
|
-
|
|
12044
|
-
continue;
|
|
12045
|
-
const tool = entry.tools.find((t) => t.name === toolName);
|
|
12046
|
-
if (tool) {
|
|
12047
|
-
return { entry, server: spec.serverName, tool };
|
|
12048
|
-
}
|
|
11993
|
+
return [
|
|
11994
|
+
...protocol,
|
|
11995
|
+
{
|
|
11996
|
+
version: "0.9",
|
|
11997
|
+
datas: surfaceIds.map((surfaceId) => ({
|
|
11998
|
+
updateDataModel: { surfaceId, path: "/", value: viewModel }
|
|
11999
|
+
}))
|
|
12049
12000
|
}
|
|
12050
|
-
|
|
12051
|
-
|
|
12052
|
-
|
|
12053
|
-
|
|
12001
|
+
];
|
|
12002
|
+
}
|
|
12003
|
+
function collectA2UISurfaceIdsForLegacyContract(protocol) {
|
|
12004
|
+
const surfaceIds = /* @__PURE__ */ new Set();
|
|
12005
|
+
for (const message of protocol) {
|
|
12006
|
+
if (!isJsonObject(message) || !Array.isArray(message.datas))
|
|
12007
|
+
continue;
|
|
12008
|
+
for (const instruction of message.datas) {
|
|
12009
|
+
if (!isJsonObject(instruction))
|
|
12054
12010
|
continue;
|
|
12055
|
-
const
|
|
12056
|
-
|
|
12057
|
-
|
|
12011
|
+
const operations = [
|
|
12012
|
+
"createSurface" in instruction ? instruction.createSurface : void 0,
|
|
12013
|
+
"updateComponents" in instruction ? instruction.updateComponents : void 0
|
|
12014
|
+
];
|
|
12015
|
+
for (const operation of operations) {
|
|
12016
|
+
if (isJsonObject(operation) && typeof operation.surfaceId === "string") {
|
|
12017
|
+
surfaceIds.add(operation.surfaceId);
|
|
12018
|
+
}
|
|
12058
12019
|
}
|
|
12059
12020
|
}
|
|
12060
|
-
return void 0;
|
|
12061
|
-
}
|
|
12062
|
-
/** 按 serverName 或原始 resourceId 找 spec(server 入参可为二者)。 */
|
|
12063
|
-
findSpec(server) {
|
|
12064
|
-
return this.specByServerName.get(server) ?? this.specByResourceId.get(server);
|
|
12065
|
-
}
|
|
12066
|
-
/** serverHint(serverName 或 resourceId)命中的 spec 子集。 */
|
|
12067
|
-
specsMatching(serverHint) {
|
|
12068
|
-
return this.specs.filter((spec) => spec.serverName === serverHint || spec.resourceIds.includes(serverHint));
|
|
12069
|
-
}
|
|
12070
|
-
/** 格式化某 server 的工具列表。 */
|
|
12071
|
-
formatToolList(server, tools) {
|
|
12072
|
-
if (tools.length === 0)
|
|
12073
|
-
return `Server "${server}" has no tools.`;
|
|
12074
|
-
const lines = [`${server} (${tools.length} tools):`, ""];
|
|
12075
|
-
for (const tool of tools) {
|
|
12076
|
-
const desc = truncate(tool.description ?? "", 50);
|
|
12077
|
-
lines.push(`- ${tool.name}${desc ? " - " + desc : ""}`);
|
|
12078
|
-
}
|
|
12079
|
-
return lines.join("\n").trim();
|
|
12080
|
-
}
|
|
12081
|
-
};
|
|
12082
|
-
function parseProxyArgs(args) {
|
|
12083
|
-
if (!isRecord(args))
|
|
12084
|
-
return {};
|
|
12085
|
-
const out = {};
|
|
12086
|
-
if (typeof args.connect === "string")
|
|
12087
|
-
out.connect = args.connect;
|
|
12088
|
-
if (typeof args.describe === "string")
|
|
12089
|
-
out.describe = args.describe;
|
|
12090
|
-
if (typeof args.search === "string")
|
|
12091
|
-
out.search = args.search;
|
|
12092
|
-
if (Array.isArray(args.searches)) {
|
|
12093
|
-
out.searches = args.searches.filter((query) => typeof query === "string").slice(0, MCP_BATCH_SEARCH_MAX_ITEMS);
|
|
12094
12021
|
}
|
|
12095
|
-
|
|
12096
|
-
out.tool = args.tool;
|
|
12097
|
-
if (typeof args.server === "string")
|
|
12098
|
-
out.server = args.server;
|
|
12099
|
-
if (args.args !== void 0)
|
|
12100
|
-
out.args = args.args;
|
|
12101
|
-
return out;
|
|
12022
|
+
return [...surfaceIds];
|
|
12102
12023
|
}
|
|
12103
|
-
function
|
|
12104
|
-
|
|
12105
|
-
|
|
12106
|
-
|
|
12107
|
-
|
|
12108
|
-
|
|
12024
|
+
function createPresentationDescription(config) {
|
|
12025
|
+
return [
|
|
12026
|
+
config.description,
|
|
12027
|
+
`\u9002\u7528\u573A\u666F\uFF1A${config.whenToUse}`,
|
|
12028
|
+
...config.whenNotToUse ? [`\u4E0D\u9002\u7528\u573A\u666F\uFF1A${config.whenNotToUse}`] : [],
|
|
12029
|
+
"\u53EA\u63D0\u4F9B\u5DF2\u58F0\u660E\u7684\u6807\u8BC6\u6216\u7B5B\u9009\u53C2\u6570\u3002\u6743\u5A01\u4E1A\u52A1\u6570\u636E\u7531\u5E73\u53F0\u89E3\u6790\u83B7\u53D6\u3002"
|
|
12030
|
+
].join("\n");
|
|
12031
|
+
}
|
|
12032
|
+
function cloneJsonObject2(value, label) {
|
|
12033
|
+
if (!isJsonObject(value))
|
|
12034
|
+
throw new Error(`${label} must be a JSON object`);
|
|
12035
|
+
return JSON.parse(JSON.stringify(value));
|
|
12036
|
+
}
|
|
12037
|
+
function supplementMcpInput(input, invocation, presentationSchema, mcpInputSchema) {
|
|
12038
|
+
const result = cloneJsonObject2(input, "MCP Tool arguments");
|
|
12039
|
+
const presentationProperties = schemaProperties(presentationSchema);
|
|
12040
|
+
const mcpProperties = schemaProperties(mcpInputSchema);
|
|
12041
|
+
if (!presentationProperties || !mcpProperties)
|
|
12042
|
+
return result;
|
|
12043
|
+
for (const [name, mcpProperty] of Object.entries(mcpProperties)) {
|
|
12044
|
+
if (result[name] !== void 0 || invocation[name] === void 0)
|
|
12045
|
+
continue;
|
|
12046
|
+
const presentationProperty = presentationProperties[name];
|
|
12047
|
+
if (isJsonObject(presentationProperty) && isJsonObject(mcpProperty) && compatibleSchemaTypes(presentationProperty.type, mcpProperty.type)) {
|
|
12048
|
+
result[name] = JSON.parse(JSON.stringify(invocation[name]));
|
|
12049
|
+
}
|
|
12109
12050
|
}
|
|
12110
|
-
|
|
12111
|
-
const lines = Object.entries(props).map(([key, value]) => {
|
|
12112
|
-
const type = isRecord(value) && typeof value.type === "string" ? value.type : "any";
|
|
12113
|
-
const desc = isRecord(value) && typeof value.description === "string" ? value.description : "";
|
|
12114
|
-
const req = required.has(key) ? " [required]" : "";
|
|
12115
|
-
return `${indent}- ${key} (${type})${req}${desc ? " - " + desc : ""}`;
|
|
12116
|
-
});
|
|
12117
|
-
return lines.join("\n");
|
|
12051
|
+
return result;
|
|
12118
12052
|
}
|
|
12119
|
-
function
|
|
12120
|
-
|
|
12121
|
-
|
|
12053
|
+
function schemaProperties(schema) {
|
|
12054
|
+
return schema.type === "object" && isJsonObject(schema.properties) ? schema.properties : void 0;
|
|
12055
|
+
}
|
|
12056
|
+
function compatibleSchemaTypes(source, target) {
|
|
12057
|
+
if (typeof source !== "string" || typeof target !== "string")
|
|
12058
|
+
return false;
|
|
12059
|
+
return source === target || source === "integer" && target === "number";
|
|
12060
|
+
}
|
|
12061
|
+
function readStructuredContent(result) {
|
|
12062
|
+
if (!isJsonObject(result))
|
|
12063
|
+
return void 0;
|
|
12064
|
+
if (Object.hasOwn(result, "structuredContent") && isJsonValue(result.structuredContent)) {
|
|
12065
|
+
return cloneJsonValue(result.structuredContent);
|
|
12066
|
+
}
|
|
12067
|
+
if (!Array.isArray(result.content) || result.content.length === 0) {
|
|
12068
|
+
return void 0;
|
|
12069
|
+
}
|
|
12070
|
+
const texts = [];
|
|
12071
|
+
for (const content of result.content) {
|
|
12072
|
+
if (!isJsonObject(content) || content.type !== "text" || typeof content.text !== "string")
|
|
12073
|
+
return void 0;
|
|
12074
|
+
texts.push(content.text);
|
|
12075
|
+
}
|
|
12076
|
+
if (texts.length > 1)
|
|
12077
|
+
return texts.join("\n");
|
|
12122
12078
|
try {
|
|
12123
|
-
const
|
|
12124
|
-
return
|
|
12079
|
+
const parsed = JSON.parse(texts[0]);
|
|
12080
|
+
return isJsonValue(parsed) ? cloneJsonValue(parsed) : texts[0];
|
|
12125
12081
|
} catch {
|
|
12126
|
-
return
|
|
12082
|
+
return texts[0];
|
|
12127
12083
|
}
|
|
12128
12084
|
}
|
|
12129
|
-
function
|
|
12130
|
-
|
|
12131
|
-
}
|
|
12132
|
-
|
|
12133
|
-
|
|
12085
|
+
function normalizeMcpInputSchema(value) {
|
|
12086
|
+
if (!isJsonObject(value) || getJsonSchemaIssues2(value).length > 0) {
|
|
12087
|
+
return { ...FALLBACK_OBJECT_SCHEMA };
|
|
12088
|
+
}
|
|
12089
|
+
if (value.type === "object" && (!isJsonObject(value.properties) || Object.keys(value.properties).length === 0))
|
|
12090
|
+
return { ...FALLBACK_OBJECT_SCHEMA };
|
|
12091
|
+
return JSON.parse(JSON.stringify(value));
|
|
12134
12092
|
}
|
|
12135
|
-
|
|
12093
|
+
var CardMappingError = class extends Error {
|
|
12094
|
+
details;
|
|
12095
|
+
retryable = false;
|
|
12096
|
+
constructor(details) {
|
|
12097
|
+
super(JSON.stringify(details));
|
|
12098
|
+
this.name = "CardMappingError";
|
|
12099
|
+
this.details = details;
|
|
12100
|
+
}
|
|
12101
|
+
};
|
|
12102
|
+
function isJsonObject(value) {
|
|
12136
12103
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
12137
12104
|
}
|
|
12138
|
-
|
|
12139
|
-
|
|
12140
|
-
|
|
12141
|
-
|
|
12142
|
-
|
|
12143
|
-
|
|
12144
|
-
|
|
12145
|
-
|
|
12146
|
-
|
|
12147
|
-
|
|
12148
|
-
|
|
12149
|
-
|
|
12150
|
-
]
|
|
12151
|
-
|
|
12152
|
-
|
|
12153
|
-
|
|
12154
|
-
|
|
12155
|
-
|
|
12156
|
-
|
|
12157
|
-
|
|
12158
|
-
|
|
12159
|
-
|
|
12160
|
-
|
|
12161
|
-
|
|
12162
|
-
|
|
12163
|
-
|
|
12164
|
-
|
|
12165
|
-
|
|
12166
|
-
|
|
12167
|
-
|
|
12168
|
-
|
|
12169
|
-
|
|
12170
|
-
|
|
12171
|
-
|
|
12172
|
-
|
|
12173
|
-
|
|
12174
|
-
|
|
12175
|
-
|
|
12176
|
-
|
|
12177
|
-
|
|
12178
|
-
|
|
12179
|
-
|
|
12180
|
-
|
|
12105
|
+
|
|
12106
|
+
// ../linkque-agent-runtime/dist/esm/core/mcpAggregator.js
|
|
12107
|
+
function aggregateMcpServers(resources, protocolAgentId) {
|
|
12108
|
+
const specs = /* @__PURE__ */ new Map();
|
|
12109
|
+
const touch = (serverName) => {
|
|
12110
|
+
let spec = specs.get(serverName);
|
|
12111
|
+
if (!spec) {
|
|
12112
|
+
spec = {
|
|
12113
|
+
serverName,
|
|
12114
|
+
agentId: "",
|
|
12115
|
+
argsMappingRules: [],
|
|
12116
|
+
allowedTools: void 0,
|
|
12117
|
+
resourceIds: [],
|
|
12118
|
+
description: ""
|
|
12119
|
+
};
|
|
12120
|
+
specs.set(serverName, spec);
|
|
12121
|
+
}
|
|
12122
|
+
return spec;
|
|
12123
|
+
};
|
|
12124
|
+
for (const resource of resources) {
|
|
12125
|
+
const serverName = resource.config.serverName ?? resource.resourceId;
|
|
12126
|
+
const spec = touch(serverName);
|
|
12127
|
+
spec.resourceIds.push(resource.resourceId);
|
|
12128
|
+
if (!spec.agentId) {
|
|
12129
|
+
spec.agentId = protocolAgentId ?? resource.config.agentId;
|
|
12130
|
+
}
|
|
12131
|
+
if (!spec.description)
|
|
12132
|
+
spec.description = resource.config.description;
|
|
12133
|
+
if (resource.resourceType === EResourceType.MCP) {
|
|
12134
|
+
for (const rule of resource.config.argsMappingRules ?? []) {
|
|
12135
|
+
spec.argsMappingRules.push(rule);
|
|
12136
|
+
}
|
|
12137
|
+
const allowed = resource.config.allowedTools;
|
|
12138
|
+
if (allowed && allowed.length > 0) {
|
|
12139
|
+
if (!spec.allowedTools)
|
|
12140
|
+
spec.allowedTools = /* @__PURE__ */ new Set();
|
|
12141
|
+
for (const toolName of allowed)
|
|
12142
|
+
spec.allowedTools.add(toolName);
|
|
12143
|
+
}
|
|
12144
|
+
continue;
|
|
12145
|
+
}
|
|
12146
|
+
if (!spec.allowedTools)
|
|
12147
|
+
spec.allowedTools = /* @__PURE__ */ new Set();
|
|
12148
|
+
spec.allowedTools.add(resource.config.toolName);
|
|
12149
|
+
for (const rule of resource.config.argsMappingRules ?? []) {
|
|
12150
|
+
const fullRule = {
|
|
12151
|
+
mcpToolName: resource.config.toolName,
|
|
12152
|
+
argJsonPath: rule.argJsonPath,
|
|
12153
|
+
disabled: rule.disabled,
|
|
12154
|
+
valueType: rule.valueType,
|
|
12155
|
+
...rule.value !== void 0 ? { value: rule.value } : {}
|
|
12156
|
+
};
|
|
12157
|
+
spec.argsMappingRules.push(fullRule);
|
|
12181
12158
|
}
|
|
12182
12159
|
}
|
|
12183
|
-
|
|
12160
|
+
return [...specs.values()];
|
|
12161
|
+
}
|
|
12184
12162
|
|
|
12185
|
-
// ../linkque-agent-runtime/dist/esm/core/
|
|
12186
|
-
var
|
|
12187
|
-
|
|
12188
|
-
|
|
12189
|
-
|
|
12190
|
-
|
|
12191
|
-
|
|
12163
|
+
// ../linkque-agent-runtime/dist/esm/core/mcpProxyTool.js
|
|
12164
|
+
var MCP_PROXY_TOOL_NAME = "mcp";
|
|
12165
|
+
var McpProxyTool = class {
|
|
12166
|
+
provider;
|
|
12167
|
+
/** serverName → 缓存条目(同 serverName 多 resourceId 聚合为一个连接)。 */
|
|
12168
|
+
entries = /* @__PURE__ */ new Map();
|
|
12169
|
+
/** 进行中的连接(去重并发 ensureEntry)。 */
|
|
12170
|
+
inFlight = /* @__PURE__ */ new Map();
|
|
12171
|
+
specByServerName;
|
|
12172
|
+
/** 原始 resourceId → 所属 spec(resolve(resourceId,...) 反查用)。 */
|
|
12173
|
+
specByResourceId;
|
|
12174
|
+
specs;
|
|
12192
12175
|
debugLog;
|
|
12193
|
-
|
|
12194
|
-
|
|
12195
|
-
|
|
12196
|
-
this.
|
|
12197
|
-
this.
|
|
12198
|
-
this.
|
|
12199
|
-
|
|
12200
|
-
if (sink) {
|
|
12201
|
-
this.debugLog = (line, scope) => {
|
|
12202
|
-
this.write("diagnostic", { message: line }, scope);
|
|
12203
|
-
};
|
|
12204
|
-
}
|
|
12176
|
+
constructor(provider, specs, options = {}) {
|
|
12177
|
+
this.provider = provider;
|
|
12178
|
+
this.specs = specs;
|
|
12179
|
+
this.specByServerName = new Map(specs.map((spec) => [spec.serverName, spec]));
|
|
12180
|
+
this.specByResourceId = new Map(specs.flatMap((spec) => spec.resourceIds.map((id) => [id, spec])));
|
|
12181
|
+
this.debugLog = options.debugLog ?? (() => {
|
|
12182
|
+
});
|
|
12205
12183
|
}
|
|
12206
|
-
/**
|
|
12207
|
-
|
|
12208
|
-
|
|
12209
|
-
|
|
12210
|
-
|
|
12211
|
-
|
|
12212
|
-
|
|
12213
|
-
|
|
12214
|
-
|
|
12215
|
-
|
|
12184
|
+
/**
|
|
12185
|
+
* 产单个 `mcp` 代理工具描述符,注入 `agentReq.tools`。
|
|
12186
|
+
* `execute` 闭包按入参字段派发到 status/connect/list/search/describe/call。
|
|
12187
|
+
*/
|
|
12188
|
+
asTool() {
|
|
12189
|
+
return {
|
|
12190
|
+
name: MCP_PROXY_TOOL_NAME,
|
|
12191
|
+
modelName: MCP_PROXY_TOOL_NAME,
|
|
12192
|
+
label: "MCP",
|
|
12193
|
+
description: MCP_PROXY_DESCRIPTION,
|
|
12194
|
+
inputSchema: MCP_PROXY_INPUT_SCHEMA,
|
|
12195
|
+
execute: (args, signal) => this.dispatch(args, signal)
|
|
12196
|
+
};
|
|
12197
|
+
}
|
|
12198
|
+
/** {@link IMcpToolRegistry.resolve}:按 (resourceId, toolName) 解析可调用句柄。 */
|
|
12199
|
+
async resolve(resourceId, toolName, signal) {
|
|
12200
|
+
const spec = this.findSpec(resourceId);
|
|
12201
|
+
if (!spec)
|
|
12202
|
+
return void 0;
|
|
12203
|
+
const entry = await this.ensureEntry(spec.serverName, signal);
|
|
12204
|
+
if (!entry || !entry.server)
|
|
12205
|
+
return void 0;
|
|
12206
|
+
const tool = entry.tools.find((t) => t.name === toolName);
|
|
12207
|
+
if (!tool)
|
|
12208
|
+
return void 0;
|
|
12209
|
+
return {
|
|
12210
|
+
inputSchema: tool.inputSchema,
|
|
12211
|
+
outputSchema: tool.outputSchema,
|
|
12212
|
+
execute: (args, sig) => entry.server.callTool(tool.name, args, sig)
|
|
12213
|
+
};
|
|
12214
|
+
}
|
|
12215
|
+
/** 关闭所有已连 server(忽略单 server 关闭错误)。 */
|
|
12216
|
+
async closeAll() {
|
|
12217
|
+
await Promise.all([...this.entries.values()].map(async (entry) => {
|
|
12218
|
+
if (!entry.server)
|
|
12219
|
+
return;
|
|
12220
|
+
try {
|
|
12221
|
+
await entry.server.close?.();
|
|
12222
|
+
} catch (error) {
|
|
12223
|
+
try {
|
|
12224
|
+
this.debugLog(`[mcp] server ${entry.server.serverName} close failed: ${error instanceof Error ? error.message : "unknown error"}`, EDebugLogScope.MCP);
|
|
12225
|
+
} catch {
|
|
12226
|
+
}
|
|
12227
|
+
}
|
|
12228
|
+
}));
|
|
12216
12229
|
}
|
|
12217
|
-
|
|
12218
|
-
|
|
12219
|
-
|
|
12220
|
-
|
|
12221
|
-
|
|
12222
|
-
|
|
12223
|
-
record2.message = sanitizeDiagnosticText(redactKnownValues(record2.message, this.sensitiveValues));
|
|
12224
|
-
if (record2.errorName)
|
|
12225
|
-
record2.errorName = sanitizeDiagnosticText(redactKnownValues(record2.errorName, this.sensitiveValues));
|
|
12226
|
-
if ("input" in record2)
|
|
12227
|
-
record2.input = sanitizeLogValue(record2.input, this.sensitiveValues);
|
|
12228
|
-
if ("output" in record2)
|
|
12229
|
-
record2.output = sanitizeLogValue(record2.output, this.sensitiveValues);
|
|
12230
|
-
this.sink(JSON.stringify({
|
|
12231
|
-
operation: this.operation,
|
|
12232
|
-
level: "info",
|
|
12233
|
-
layer: this.layer,
|
|
12234
|
-
event,
|
|
12235
|
-
stage: this.stage,
|
|
12236
|
-
...record2,
|
|
12237
|
-
costMs: Math.max(0, Math.round(performance.now() - this.startedAt))
|
|
12238
|
-
}), scope);
|
|
12239
|
-
} catch {
|
|
12230
|
+
// —— 内部:连接与缓存 ——
|
|
12231
|
+
/** 确保某 server 已连;返回 connected 条目,依失败返回 null(不抛错)。 */
|
|
12232
|
+
async ensureEntry(serverName, signal) {
|
|
12233
|
+
const existing = this.entries.get(serverName);
|
|
12234
|
+
if (existing) {
|
|
12235
|
+
return existing.status === "connected" ? existing : null;
|
|
12240
12236
|
}
|
|
12241
|
-
|
|
12242
|
-
|
|
12243
|
-
|
|
12244
|
-
|
|
12245
|
-
try {
|
|
12246
|
-
this.write(event, {
|
|
12247
|
-
errorName: error instanceof Error ? error.name : typeof error,
|
|
12248
|
-
message: error instanceof Error ? error.message : typeof error === "string" ? error : "\u975E\u6807\u51C6\u5F02\u5E38"
|
|
12237
|
+
let promise = this.inFlight.get(serverName);
|
|
12238
|
+
if (!promise) {
|
|
12239
|
+
promise = this.connect(serverName, signal).finally(() => {
|
|
12240
|
+
this.inFlight.delete(serverName);
|
|
12249
12241
|
});
|
|
12250
|
-
|
|
12242
|
+
this.inFlight.set(serverName, promise);
|
|
12251
12243
|
}
|
|
12244
|
+
const entry = await promise;
|
|
12245
|
+
return entry.status === "connected" ? entry : null;
|
|
12252
12246
|
}
|
|
12253
|
-
|
|
12254
|
-
|
|
12255
|
-
|
|
12256
|
-
|
|
12257
|
-
|
|
12258
|
-
|
|
12259
|
-
|
|
12260
|
-
|
|
12261
|
-
|
|
12262
|
-
|
|
12247
|
+
/** 连单个 server 并 listTools;按 spec.allowedTools 过滤后缓存;失败记为 failed。 */
|
|
12248
|
+
async connect(serverName, _signal) {
|
|
12249
|
+
const spec = this.specByServerName.get(serverName);
|
|
12250
|
+
if (!spec) {
|
|
12251
|
+
const entry = {
|
|
12252
|
+
server: null,
|
|
12253
|
+
tools: [],
|
|
12254
|
+
status: "failed",
|
|
12255
|
+
error: `unknown serverName: ${serverName}`
|
|
12256
|
+
};
|
|
12257
|
+
this.entries.set(serverName, entry);
|
|
12258
|
+
this.debugLog(`[mcp] server ${serverName} unknown serverName`, EDebugLogScope.MCP);
|
|
12259
|
+
return entry;
|
|
12263
12260
|
}
|
|
12264
|
-
|
|
12265
|
-
|
|
12266
|
-
|
|
12267
|
-
|
|
12268
|
-
|
|
12269
|
-
|
|
12270
|
-
|
|
12271
|
-
|
|
12272
|
-
|
|
12273
|
-
|
|
12274
|
-
} catch {
|
|
12275
|
-
return "<invalid JSON>";
|
|
12261
|
+
try {
|
|
12262
|
+
const synthetic = {
|
|
12263
|
+
resourceId: spec.serverName,
|
|
12264
|
+
resourceType: EResourceType.MCP,
|
|
12265
|
+
providerType: EResourceProviderType.ALIPAY_OPEN,
|
|
12266
|
+
config: {
|
|
12267
|
+
agentId: spec.agentId,
|
|
12268
|
+
serverName: spec.serverName,
|
|
12269
|
+
description: spec.description,
|
|
12270
|
+
argsMappingRules: spec.argsMappingRules
|
|
12276
12271
|
}
|
|
12272
|
+
};
|
|
12273
|
+
const servers = await this.provider.servers([synthetic]);
|
|
12274
|
+
const server = servers[0];
|
|
12275
|
+
if (!server) {
|
|
12276
|
+
throw new Error(`provider returned no server for ${spec.serverName}`);
|
|
12277
12277
|
}
|
|
12278
|
-
const
|
|
12279
|
-
const
|
|
12280
|
-
|
|
12281
|
-
|
|
12278
|
+
const all = await server.listTools();
|
|
12279
|
+
const tools = spec.allowedTools ? all.filter((tool) => spec.allowedTools.has(tool.name)) : all;
|
|
12280
|
+
const entry = { server, tools, status: "connected" };
|
|
12281
|
+
this.entries.set(serverName, entry);
|
|
12282
|
+
this.debugLog(`[mcp] server ${spec.serverName} connected (${tools.length}/${all.length} tools)`, EDebugLogScope.MCP);
|
|
12283
|
+
return entry;
|
|
12284
|
+
} catch (error) {
|
|
12285
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
12286
|
+
const entry = {
|
|
12287
|
+
server: null,
|
|
12288
|
+
tools: [],
|
|
12289
|
+
status: "failed",
|
|
12290
|
+
error: message
|
|
12291
|
+
};
|
|
12292
|
+
this.entries.set(serverName, entry);
|
|
12293
|
+
this.debugLog(`[mcp] server ${spec.serverName} connect failed: ${message}`, EDebugLogScope.MCP);
|
|
12294
|
+
return entry;
|
|
12282
12295
|
}
|
|
12283
|
-
|
|
12284
|
-
|
|
12285
|
-
|
|
12286
|
-
|
|
12287
|
-
|
|
12288
|
-
|
|
12289
|
-
|
|
12290
|
-
|
|
12291
|
-
|
|
12292
|
-
|
|
12293
|
-
|
|
12294
|
-
|
|
12295
|
-
|
|
12296
|
+
}
|
|
12297
|
+
// —— 派发 ——
|
|
12298
|
+
/** 按入参字段派发;返回 LLM 可读文本。 */
|
|
12299
|
+
async dispatch(args, signal) {
|
|
12300
|
+
const params = parseProxyArgs(args);
|
|
12301
|
+
if (params.connect !== void 0) {
|
|
12302
|
+
return this.executeConnect(params.connect, signal);
|
|
12303
|
+
}
|
|
12304
|
+
if (params.describe !== void 0) {
|
|
12305
|
+
return this.executeDescribe(params.describe, params.server, signal);
|
|
12306
|
+
}
|
|
12307
|
+
if (params.searches !== void 0) {
|
|
12308
|
+
return this.executeBatchSearch(params.searches, params.server);
|
|
12309
|
+
}
|
|
12310
|
+
if (params.search !== void 0) {
|
|
12311
|
+
return this.executeSearch(params.search, params.server);
|
|
12312
|
+
}
|
|
12313
|
+
if (params.tool !== void 0) {
|
|
12314
|
+
return this.executeCall(params.tool, params.args, params.server, signal);
|
|
12315
|
+
}
|
|
12316
|
+
if (params.server !== void 0) {
|
|
12317
|
+
return this.executeList(params.server, signal);
|
|
12318
|
+
}
|
|
12319
|
+
return this.executeStatus();
|
|
12320
|
+
}
|
|
12321
|
+
/** `mcp({})` / `mcp({ status: true })`:列各 server 连接状态 + 工具数。 */
|
|
12322
|
+
executeStatus() {
|
|
12323
|
+
const lines = [];
|
|
12324
|
+
let connected = 0;
|
|
12325
|
+
let totalTools = 0;
|
|
12326
|
+
for (const spec of this.specs) {
|
|
12327
|
+
const label = spec.serverName;
|
|
12328
|
+
const entry = this.entries.get(spec.serverName);
|
|
12329
|
+
if (!entry) {
|
|
12330
|
+
lines.push(`\u25CB ${label} (not connected)`);
|
|
12331
|
+
continue;
|
|
12296
12332
|
}
|
|
12297
|
-
if (
|
|
12298
|
-
|
|
12299
|
-
|
|
12300
|
-
|
|
12301
|
-
|
|
12333
|
+
if (entry.status === "connected") {
|
|
12334
|
+
connected += 1;
|
|
12335
|
+
totalTools += entry.tools.length;
|
|
12336
|
+
lines.push(`\u2713 ${label} (${entry.tools.length} tools)`);
|
|
12337
|
+
} else {
|
|
12338
|
+
lines.push(`\u2717 ${label} (failed${entry.error ? ": " + entry.error : ""})`);
|
|
12302
12339
|
}
|
|
12303
|
-
if (keys.length > 20)
|
|
12304
|
-
result2["<truncated>"] = true;
|
|
12305
|
-
return result2;
|
|
12306
|
-
} catch {
|
|
12307
|
-
return "<unreadable>";
|
|
12308
|
-
} finally {
|
|
12309
|
-
seen.delete(item);
|
|
12310
12340
|
}
|
|
12311
|
-
|
|
12312
|
-
|
|
12313
|
-
|
|
12314
|
-
return text.length > 6e3 ? { truncated: true, preview: text.slice(0, 2e3) } : result;
|
|
12315
|
-
}
|
|
12316
|
-
function sanitizeDiagnosticText(value) {
|
|
12317
|
-
return value.replace(/\b(?:body|arguments|payload|headers)\s*[:=][\s\S]*/giu, "<payload>").replace(/\{[\s\S]*/gu, "<payload>").replace(/https?:\/\/[^\s]+/giu, "<url>").replace(/\b(?:authorization|cookie|set-cookie)\s*[:=][^\r\n]*/giu, "<redacted>").replace(/\bbearer\s+[^\s,;]+/giu, "Bearer <redacted>").replace(/\b((?:api[_-]?key|access[_-]?key|token|secret|password|ak|sk|tradeNO|userId|orderId)\s*[:=]\s*)(?:"[^"]*"|'[^']*'|[^\s,;]+)/giu, "$1<redacted>").replace(/\s+/gu, " ").slice(0, 300);
|
|
12318
|
-
}
|
|
12319
|
-
function redactKnownValues(text, sensitiveValues) {
|
|
12320
|
-
let result = text;
|
|
12321
|
-
for (const sensitive of sensitiveValues)
|
|
12322
|
-
result = result.replaceAll(sensitive, "<redacted>");
|
|
12323
|
-
return result;
|
|
12324
|
-
}
|
|
12341
|
+
const header = `MCP: ${connected}/${this.specs.length} servers, ${totalTools} tools`;
|
|
12342
|
+
const hint = this.specs.length > 0 ? '\nmcp({ server: "name" }) to list tools, mcp({ search: "..." }) to search, mcp({ tool: "...", args: {...} }) to call' : "\n(no MCP resources configured)";
|
|
12343
|
+
return `${header}
|
|
12325
12344
|
|
|
12326
|
-
|
|
12327
|
-
async function executeCardAction(input) {
|
|
12328
|
-
const diagnostics = new CardActionDiagnostics(input.debugLog, {
|
|
12329
|
-
agentId: input.protocol.agentId,
|
|
12330
|
-
threadId: input.runtimeContext.threadId
|
|
12331
|
-
});
|
|
12332
|
-
diagnostics.protect(() => input.runtimeContext.userId);
|
|
12333
|
-
diagnostics.protect(() => input.runtimeContext.tenantId);
|
|
12334
|
-
diagnostics.write("started", {
|
|
12335
|
-
input: { request: input.request, runtimeContext: input.runtimeContext }
|
|
12336
|
-
});
|
|
12337
|
-
try {
|
|
12338
|
-
const response = await executeCardActionInternal(input, diagnostics);
|
|
12339
|
-
diagnostics.write(response.ok ? "completed" : "failed", response.ok ? { output: response } : {
|
|
12340
|
-
code: response.error.code,
|
|
12341
|
-
retryable: response.error.retryable,
|
|
12342
|
-
output: response
|
|
12343
|
-
});
|
|
12344
|
-
return response;
|
|
12345
|
-
} catch (error) {
|
|
12346
|
-
diagnostics.error("failed", error);
|
|
12347
|
-
throw error;
|
|
12348
|
-
}
|
|
12349
|
-
}
|
|
12350
|
-
async function executeCardActionInternal(input, diagnostics) {
|
|
12351
|
-
let request;
|
|
12352
|
-
try {
|
|
12353
|
-
request = parseA2UIActionExecuteRequest(input.request);
|
|
12354
|
-
} catch (error) {
|
|
12355
|
-
return failure("INVALID_INPUT", errorMessage(error, "\u652F\u4ED8\u52A8\u4F5C\u8BF7\u6C42\u65E0\u6548"), false);
|
|
12356
|
-
}
|
|
12357
|
-
Object.assign(diagnostics.context, {
|
|
12358
|
-
invocationId: request.invocationId,
|
|
12359
|
-
actionId: request.actionId,
|
|
12360
|
-
cardRef: request.source.cardRef,
|
|
12361
|
-
cardRevision: request.source.cardRevision
|
|
12362
|
-
});
|
|
12363
|
-
if (request.agentId !== input.protocol.agentId) {
|
|
12364
|
-
return failure("AGENT_MISMATCH", "\u652F\u4ED8\u52A8\u4F5C\u4E0E\u5F53\u524D Agent \u4E0D\u5339\u914D", false);
|
|
12365
|
-
}
|
|
12366
|
-
if (request.threadId !== input.runtimeContext.threadId) {
|
|
12367
|
-
return failure("INVALID_INPUT", "\u652F\u4ED8\u52A8\u4F5C\u4E0E\u5F53\u524D\u4F1A\u8BDD\u4E0D\u5339\u914D", false);
|
|
12345
|
+
${lines.join("\n")}${hint}`.trim();
|
|
12368
12346
|
}
|
|
12369
|
-
|
|
12370
|
-
|
|
12347
|
+
/** `mcp({ connect: "server" })`:显式连接并刷元数据,返回该 server 工具列表。 */
|
|
12348
|
+
async executeConnect(server, signal) {
|
|
12349
|
+
const spec = this.findSpec(server);
|
|
12350
|
+
if (!spec)
|
|
12351
|
+
return `Server "${server}" not found. Use mcp({}) to see available servers.`;
|
|
12352
|
+
this.entries.delete(spec.serverName);
|
|
12353
|
+
const entry = await this.ensureEntry(spec.serverName, signal);
|
|
12354
|
+
if (!entry) {
|
|
12355
|
+
const failed = this.entries.get(spec.serverName);
|
|
12356
|
+
const detail = failed?.error ? `: ${failed.error}` : "";
|
|
12357
|
+
return `Failed to connect to "${spec.serverName}"${detail}.`;
|
|
12358
|
+
}
|
|
12359
|
+
return this.formatToolList(spec.serverName, entry.tools);
|
|
12371
12360
|
}
|
|
12372
|
-
|
|
12373
|
-
|
|
12374
|
-
|
|
12375
|
-
|
|
12376
|
-
|
|
12377
|
-
|
|
12378
|
-
|
|
12379
|
-
|
|
12380
|
-
|
|
12361
|
+
/** `mcp({ server: "name" })`:列该 server 工具名 + 截断 description。 */
|
|
12362
|
+
async executeList(server, signal) {
|
|
12363
|
+
const spec = this.findSpec(server);
|
|
12364
|
+
if (!spec)
|
|
12365
|
+
return `Server "${server}" not found. Use mcp({}) to see available servers.`;
|
|
12366
|
+
const entry = await this.ensureEntry(spec.serverName, signal);
|
|
12367
|
+
if (!entry) {
|
|
12368
|
+
return `Server "${spec.serverName}" is not connected. Use mcp({ connect: "${spec.serverName}" }) to retry.`;
|
|
12369
|
+
}
|
|
12370
|
+
return this.formatToolList(spec.serverName, entry.tools);
|
|
12381
12371
|
}
|
|
12382
|
-
|
|
12383
|
-
|
|
12384
|
-
|
|
12385
|
-
|
|
12386
|
-
|
|
12387
|
-
|
|
12388
|
-
|
|
12389
|
-
|
|
12390
|
-
|
|
12391
|
-
|
|
12392
|
-
|
|
12393
|
-
|
|
12394
|
-
|
|
12395
|
-
|
|
12396
|
-
|
|
12397
|
-
|
|
12398
|
-
|
|
12399
|
-
outputSchema: tool?.outputSchema
|
|
12372
|
+
/** `mcp({ search: "query" })`:空格分词 OR 子串匹配 name/description(跨所有 server)。 */
|
|
12373
|
+
async executeSearch(query, serverHint) {
|
|
12374
|
+
const terms = query.trim().split(/\s+/u).filter((t) => t.length > 0);
|
|
12375
|
+
if (terms.length === 0)
|
|
12376
|
+
return "Search query cannot be empty.";
|
|
12377
|
+
const pattern = new RegExp(terms.map(escapeRegExp).join("|"), "iu");
|
|
12378
|
+
const specs = serverHint ? this.specsMatching(serverHint) : this.specs;
|
|
12379
|
+
await Promise.all(specs.map((s) => this.ensureEntry(s.serverName)));
|
|
12380
|
+
const matches = [];
|
|
12381
|
+
for (const spec of specs) {
|
|
12382
|
+
const entry = this.entries.get(spec.serverName);
|
|
12383
|
+
if (!entry || entry.status !== "connected")
|
|
12384
|
+
continue;
|
|
12385
|
+
for (const tool of entry.tools) {
|
|
12386
|
+
if (pattern.test(tool.name) || pattern.test(tool.description ?? "")) {
|
|
12387
|
+
matches.push({ server: spec.serverName, tool });
|
|
12388
|
+
}
|
|
12400
12389
|
}
|
|
12401
|
-
});
|
|
12402
|
-
if (!tool) {
|
|
12403
|
-
return failure("TOOL_NOT_ALLOWED", "\u521B\u5EFA\u4EA4\u6613 MCP Tool \u4E0D\u5B58\u5728\u6216\u672A\u6388\u6743", false);
|
|
12404
12390
|
}
|
|
12405
|
-
|
|
12406
|
-
|
|
12407
|
-
input: {
|
|
12408
|
-
bindings: resolved.binding.createTrade.inputBindings,
|
|
12409
|
-
actionContext: request.actionContext,
|
|
12410
|
-
runtimeContext: input.runtimeContext
|
|
12411
|
-
}
|
|
12412
|
-
});
|
|
12413
|
-
const args = createToolInput(resolved.binding.createTrade.inputBindings, request.actionContext, input.runtimeContext);
|
|
12414
|
-
diagnostics.write("tool_input_completed", {
|
|
12415
|
-
output: { mapped: Boolean(args), args }
|
|
12416
|
-
});
|
|
12417
|
-
if (!args)
|
|
12418
|
-
return failure("INVALID_INPUT", "\u65E0\u6CD5\u751F\u6210\u521B\u5EFA\u4EA4\u6613 MCP \u5165\u53C2", false);
|
|
12419
|
-
let result;
|
|
12420
|
-
diagnostics.stage = "tool_execute";
|
|
12421
|
-
diagnostics.write("tool_started", { input: args });
|
|
12422
|
-
try {
|
|
12423
|
-
result = await tool.execute(args, input.signal);
|
|
12424
|
-
diagnostics.protect(() => readJsonPointer(readStructuredContent(result), resolved.binding.createTrade.tradeNoPath));
|
|
12425
|
-
diagnostics.write("tool_completed", { output: result });
|
|
12426
|
-
} catch (error) {
|
|
12427
|
-
diagnostics.error("tool_failed", error);
|
|
12428
|
-
return failure("EXECUTION_FAILED", "\u521B\u5EFA\u4EA4\u6613\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5", true);
|
|
12391
|
+
if (matches.length === 0) {
|
|
12392
|
+
return serverHint ? `No tools matching "${query}" in "${serverHint}"` : `No tools matching "${query}"`;
|
|
12429
12393
|
}
|
|
12430
|
-
|
|
12431
|
-
|
|
12432
|
-
|
|
12433
|
-
|
|
12434
|
-
|
|
12435
|
-
|
|
12436
|
-
|
|
12437
|
-
output: {
|
|
12438
|
-
structured,
|
|
12439
|
-
tradeNO,
|
|
12440
|
-
valid: typeof tradeNO === "string" && Boolean(tradeNO.trim()) && tradeNO.length <= 64
|
|
12394
|
+
const lines = [`Found ${matches.length} tool(s) matching "${query}":`, ""];
|
|
12395
|
+
for (const { server, tool } of matches) {
|
|
12396
|
+
lines.push(`${tool.name} [${server}]`);
|
|
12397
|
+
lines.push(` ${tool.description ?? "(no description)"}`);
|
|
12398
|
+
if (tool.inputSchema) {
|
|
12399
|
+
lines.push(` Parameters:
|
|
12400
|
+
${formatSchema(tool.inputSchema, " ")}`);
|
|
12441
12401
|
}
|
|
12442
|
-
|
|
12443
|
-
if (typeof tradeNO !== "string" || !tradeNO.trim() || tradeNO.length > 64) {
|
|
12444
|
-
return failure("INVALID_OUTPUT", "\u521B\u5EFA\u4EA4\u6613 MCP \u672A\u8FD4\u56DE\u6709\u6548 tradeNO", false);
|
|
12402
|
+
lines.push("");
|
|
12445
12403
|
}
|
|
12446
|
-
return
|
|
12447
|
-
ok: true,
|
|
12448
|
-
invocationId: request.invocationId,
|
|
12449
|
-
result: { kind: "payment", tradeNO }
|
|
12450
|
-
};
|
|
12451
|
-
} finally {
|
|
12452
|
-
const stage = diagnostics.stage;
|
|
12453
|
-
diagnostics.stage = "cleanup";
|
|
12454
|
-
diagnostics.write("cleanup_started", {
|
|
12455
|
-
input: { mcpResourceId: diagnostics.context.mcpResourceId }
|
|
12456
|
-
});
|
|
12457
|
-
await registry.closeAll();
|
|
12458
|
-
diagnostics.write("cleanup_completed", {
|
|
12459
|
-
output: { closeAttempted: true }
|
|
12460
|
-
});
|
|
12461
|
-
diagnostics.stage = stage;
|
|
12404
|
+
return lines.join("\n").trim();
|
|
12462
12405
|
}
|
|
12463
|
-
}
|
|
12464
|
-
async
|
|
12465
|
-
|
|
12466
|
-
|
|
12467
|
-
|
|
12468
|
-
|
|
12469
|
-
|
|
12470
|
-
diagnostics.protect(() => input.runtimeContext.tenantId);
|
|
12471
|
-
diagnostics.write("started", {
|
|
12472
|
-
input: {
|
|
12473
|
-
payload: input.payload,
|
|
12474
|
-
source: input.source,
|
|
12475
|
-
runtimeContext: input.runtimeContext
|
|
12406
|
+
/** `mcp({ searches: [...] })`:一次连接元数据并按 query 分组执行多组搜索。 */
|
|
12407
|
+
async executeBatchSearch(queries, serverHint) {
|
|
12408
|
+
const normalized = [
|
|
12409
|
+
...new Set(queries.map((query) => query.trim()).filter(Boolean))
|
|
12410
|
+
].slice(0, MCP_BATCH_SEARCH_MAX_ITEMS);
|
|
12411
|
+
if (normalized.length === 0) {
|
|
12412
|
+
return "Batch search requires at least one non-empty query.";
|
|
12476
12413
|
}
|
|
12477
|
-
|
|
12478
|
-
|
|
12479
|
-
const
|
|
12480
|
-
|
|
12481
|
-
|
|
12482
|
-
} catch (error) {
|
|
12483
|
-
diagnostics.error("failed", error);
|
|
12484
|
-
throw error;
|
|
12414
|
+
const specs = serverHint ? this.specsMatching(serverHint) : this.specs;
|
|
12415
|
+
await Promise.all(specs.map((spec) => this.ensureEntry(spec.serverName)));
|
|
12416
|
+
const results = await Promise.all(normalized.map((query) => this.executeSearch(query, serverHint)));
|
|
12417
|
+
return results.map((result, index) => `Query ${index + 1}: "${normalized[index]}"
|
|
12418
|
+
${result}`).join("\n\n");
|
|
12485
12419
|
}
|
|
12486
|
-
}
|
|
12487
|
-
async
|
|
12488
|
-
|
|
12489
|
-
|
|
12490
|
-
|
|
12491
|
-
|
|
12492
|
-
|
|
12493
|
-
|
|
12494
|
-
|
|
12495
|
-
|
|
12496
|
-
|
|
12497
|
-
|
|
12420
|
+
/** `mcp({ describe: "tool" })`:输出工具所属 server + 参数 schema。 */
|
|
12421
|
+
async executeDescribe(toolName, serverHint, signal) {
|
|
12422
|
+
const found = await this.findTool(toolName, serverHint, signal);
|
|
12423
|
+
if (!found) {
|
|
12424
|
+
return `Tool "${toolName}" not found. Use mcp({ search: "..." }) to search.`;
|
|
12425
|
+
}
|
|
12426
|
+
const { server, tool } = found;
|
|
12427
|
+
const lines = [
|
|
12428
|
+
`${tool.name}`,
|
|
12429
|
+
`Server: ${server}`,
|
|
12430
|
+
"",
|
|
12431
|
+
tool.description ?? "(no description)"
|
|
12432
|
+
];
|
|
12433
|
+
if (tool.inputSchema) {
|
|
12434
|
+
lines.push("", `Parameters:
|
|
12435
|
+
${formatSchema(tool.inputSchema)}`);
|
|
12436
|
+
} else {
|
|
12437
|
+
lines.push("", "No parameters defined.");
|
|
12438
|
+
}
|
|
12439
|
+
return lines.join("\n").trim();
|
|
12498
12440
|
}
|
|
12499
|
-
|
|
12500
|
-
|
|
12501
|
-
|
|
12502
|
-
|
|
12503
|
-
|
|
12504
|
-
|
|
12505
|
-
|
|
12506
|
-
|
|
12507
|
-
|
|
12508
|
-
|
|
12509
|
-
|
|
12510
|
-
|
|
12511
|
-
|
|
12512
|
-
|
|
12513
|
-
|
|
12514
|
-
|
|
12515
|
-
|
|
12516
|
-
|
|
12517
|
-
|
|
12518
|
-
|
|
12519
|
-
|
|
12520
|
-
|
|
12521
|
-
|
|
12522
|
-
|
|
12523
|
-
|
|
12524
|
-
|
|
12525
|
-
}
|
|
12526
|
-
});
|
|
12527
|
-
if (!tool)
|
|
12528
|
-
throw new Error("\u67E5\u8BE2\u652F\u4ED8\u7ED3\u679C MCP Tool \u4E0D\u5B58\u5728\u6216\u672A\u6388\u6743");
|
|
12529
|
-
diagnostics.stage = "tool_input";
|
|
12530
|
-
diagnostics.write("tool_input_started", {
|
|
12531
|
-
input: {
|
|
12532
|
-
bindings: resolved.binding.queryTrade.inputBindings,
|
|
12533
|
-
actionContext: payload.actionContext,
|
|
12534
|
-
runtimeContext: input.runtimeContext,
|
|
12535
|
-
tradeNO: payload.tradeNO,
|
|
12536
|
-
callback: payload.callback
|
|
12441
|
+
/** `mcp({ tool: "name", args: {...} })`:调用工具,返回序列化结果。 */
|
|
12442
|
+
async executeCall(toolName, args, serverHint, signal) {
|
|
12443
|
+
const found = await this.findTool(toolName, serverHint, signal);
|
|
12444
|
+
if (!found) {
|
|
12445
|
+
return `Tool "${toolName}" not found. Use mcp({ search: "..." }) to search.`;
|
|
12446
|
+
}
|
|
12447
|
+
const { entry, tool } = found;
|
|
12448
|
+
try {
|
|
12449
|
+
const result = await entry.server.callTool(tool.name, args, signal);
|
|
12450
|
+
return serializeToolResult(result);
|
|
12451
|
+
} catch (error) {
|
|
12452
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
12453
|
+
return `Tool "${toolName}" call failed: ${message}`;
|
|
12454
|
+
}
|
|
12455
|
+
}
|
|
12456
|
+
// —— 辅助 ——
|
|
12457
|
+
/** 跨(或按 hint)已连 server 查找工具;未连则触发懒连接。 */
|
|
12458
|
+
async findTool(toolName, serverHint, signal) {
|
|
12459
|
+
const specs = serverHint ? this.specsMatching(serverHint) : this.specs;
|
|
12460
|
+
for (const spec of specs) {
|
|
12461
|
+
const entry = this.entries.get(spec.serverName);
|
|
12462
|
+
if (!entry || entry.status !== "connected")
|
|
12463
|
+
continue;
|
|
12464
|
+
const tool = entry.tools.find((t) => t.name === toolName);
|
|
12465
|
+
if (tool) {
|
|
12466
|
+
return { entry, server: spec.serverName, tool };
|
|
12537
12467
|
}
|
|
12538
|
-
}
|
|
12539
|
-
|
|
12540
|
-
|
|
12541
|
-
|
|
12542
|
-
|
|
12543
|
-
|
|
12544
|
-
|
|
12545
|
-
|
|
12546
|
-
|
|
12547
|
-
const result = await tool.execute(args, input.signal);
|
|
12548
|
-
diagnostics.write("tool_completed", { output: result });
|
|
12549
|
-
diagnostics.stage = "tool_output";
|
|
12550
|
-
diagnostics.write("tool_output_started", {
|
|
12551
|
-
input: {
|
|
12552
|
-
result,
|
|
12553
|
-
statusPath: resolved.binding.queryTrade.statusPath,
|
|
12554
|
-
paidValues: resolved.binding.queryTrade.paidValues
|
|
12468
|
+
}
|
|
12469
|
+
await Promise.all(specs.map((s) => this.ensureEntry(s.serverName, signal)));
|
|
12470
|
+
for (const spec of specs) {
|
|
12471
|
+
const entry = this.entries.get(spec.serverName);
|
|
12472
|
+
if (!entry || entry.status !== "connected")
|
|
12473
|
+
continue;
|
|
12474
|
+
const tool = entry.tools.find((t) => t.name === toolName);
|
|
12475
|
+
if (tool) {
|
|
12476
|
+
return { entry, server: spec.serverName, tool };
|
|
12555
12477
|
}
|
|
12556
|
-
}
|
|
12557
|
-
|
|
12558
|
-
const data = cloneJsonObject(structured);
|
|
12559
|
-
if (!data)
|
|
12560
|
-
throw new Error("\u67E5\u8BE2\u652F\u4ED8\u7ED3\u679C MCP \u5FC5\u987B\u8FD4\u56DE JSON object");
|
|
12561
|
-
const status = readJsonPointer(data, resolved.binding.queryTrade.statusPath);
|
|
12562
|
-
diagnostics.write("tool_output_completed", {
|
|
12563
|
-
output: { data, status, valid: isJsonPrimitive2(status) }
|
|
12564
|
-
});
|
|
12565
|
-
if (!isJsonPrimitive2(status))
|
|
12566
|
-
throw new Error("\u67E5\u8BE2\u652F\u4ED8\u7ED3\u679C MCP \u72B6\u6001\u5B57\u6BB5\u65E0\u6548");
|
|
12567
|
-
return {
|
|
12568
|
-
paid: resolved.binding.queryTrade.paidValues.some((value) => Object.is(value, status)),
|
|
12569
|
-
status,
|
|
12570
|
-
data
|
|
12571
|
-
};
|
|
12572
|
-
} finally {
|
|
12573
|
-
const stage = diagnostics.stage;
|
|
12574
|
-
diagnostics.stage = "cleanup";
|
|
12575
|
-
diagnostics.write("cleanup_started", {
|
|
12576
|
-
input: { mcpResourceId: diagnostics.context.mcpResourceId }
|
|
12577
|
-
});
|
|
12578
|
-
await registry.closeAll();
|
|
12579
|
-
diagnostics.write("cleanup_completed", {
|
|
12580
|
-
output: { closeAttempted: true }
|
|
12581
|
-
});
|
|
12582
|
-
diagnostics.stage = stage;
|
|
12478
|
+
}
|
|
12479
|
+
return void 0;
|
|
12583
12480
|
}
|
|
12584
|
-
|
|
12585
|
-
|
|
12586
|
-
|
|
12587
|
-
if (!card)
|
|
12588
|
-
return {
|
|
12589
|
-
ok: false,
|
|
12590
|
-
response: failure("CARD_NOT_FOUND", "\u652F\u4ED8\u5361\u7247\u4E0D\u5B58\u5728", false)
|
|
12591
|
-
};
|
|
12592
|
-
if (card.config.revision !== source.cardRevision) {
|
|
12593
|
-
return {
|
|
12594
|
-
ok: false,
|
|
12595
|
-
response: failure("CARD_REVISION_MISMATCH", "\u652F\u4ED8\u5361\u7247\u7248\u672C\u5DF2\u53D8\u5316\uFF0C\u8BF7\u5237\u65B0\u540E\u91CD\u8BD5", false)
|
|
12596
|
-
};
|
|
12481
|
+
/** 按 serverName 或原始 resourceId 找 spec(server 入参可为二者)。 */
|
|
12482
|
+
findSpec(server) {
|
|
12483
|
+
return this.specByServerName.get(server) ?? this.specByResourceId.get(server);
|
|
12597
12484
|
}
|
|
12598
|
-
|
|
12599
|
-
|
|
12600
|
-
return
|
|
12601
|
-
|
|
12602
|
-
|
|
12603
|
-
|
|
12604
|
-
|
|
12605
|
-
|
|
12606
|
-
|
|
12607
|
-
|
|
12608
|
-
|
|
12485
|
+
/** serverHint(serverName 或 resourceId)命中的 spec 子集。 */
|
|
12486
|
+
specsMatching(serverHint) {
|
|
12487
|
+
return this.specs.filter((spec) => spec.serverName === serverHint || spec.resourceIds.includes(serverHint));
|
|
12488
|
+
}
|
|
12489
|
+
/** 格式化某 server 的工具列表。 */
|
|
12490
|
+
formatToolList(server, tools) {
|
|
12491
|
+
if (tools.length === 0)
|
|
12492
|
+
return `Server "${server}" has no tools.`;
|
|
12493
|
+
const lines = [`${server} (${tools.length} tools):`, ""];
|
|
12494
|
+
for (const tool of tools) {
|
|
12495
|
+
const desc = truncate(tool.description ?? "", 50);
|
|
12496
|
+
lines.push(`- ${tool.name}${desc ? " - " + desc : ""}`);
|
|
12497
|
+
}
|
|
12498
|
+
return lines.join("\n").trim();
|
|
12609
12499
|
}
|
|
12610
|
-
|
|
12611
|
-
|
|
12612
|
-
|
|
12613
|
-
|
|
12614
|
-
|
|
12500
|
+
};
|
|
12501
|
+
function parseProxyArgs(args) {
|
|
12502
|
+
if (!isRecord(args))
|
|
12503
|
+
return {};
|
|
12504
|
+
const out = {};
|
|
12505
|
+
if (typeof args.connect === "string")
|
|
12506
|
+
out.connect = args.connect;
|
|
12507
|
+
if (typeof args.describe === "string")
|
|
12508
|
+
out.describe = args.describe;
|
|
12509
|
+
if (typeof args.search === "string")
|
|
12510
|
+
out.search = args.search;
|
|
12511
|
+
if (Array.isArray(args.searches)) {
|
|
12512
|
+
out.searches = args.searches.filter((query) => typeof query === "string").slice(0, MCP_BATCH_SEARCH_MAX_ITEMS);
|
|
12615
12513
|
}
|
|
12514
|
+
if (typeof args.tool === "string")
|
|
12515
|
+
out.tool = args.tool;
|
|
12516
|
+
if (typeof args.server === "string")
|
|
12517
|
+
out.server = args.server;
|
|
12518
|
+
if (args.args !== void 0)
|
|
12519
|
+
out.args = args.args;
|
|
12520
|
+
return out;
|
|
12616
12521
|
}
|
|
12617
|
-
function
|
|
12618
|
-
|
|
12619
|
-
|
|
12620
|
-
|
|
12621
|
-
|
|
12622
|
-
|
|
12623
|
-
for (const binding of bindings) {
|
|
12624
|
-
let value;
|
|
12625
|
-
switch (binding.source.kind) {
|
|
12626
|
-
case "action-context":
|
|
12627
|
-
value = readJsonPointer(actionContext, binding.source.path);
|
|
12628
|
-
break;
|
|
12629
|
-
case "runtime-context":
|
|
12630
|
-
value = runtimeContext[binding.source.key];
|
|
12631
|
-
break;
|
|
12632
|
-
case "literal":
|
|
12633
|
-
value = binding.source.value;
|
|
12634
|
-
break;
|
|
12635
|
-
case "trade-no":
|
|
12636
|
-
value = tradeNO;
|
|
12637
|
-
break;
|
|
12638
|
-
case "callback":
|
|
12639
|
-
value = callback ? readJsonPointer(callback, binding.source.path) : void 0;
|
|
12640
|
-
break;
|
|
12641
|
-
}
|
|
12642
|
-
if (value === void 0 || !writeJsonPointer(result, binding.inputPath, value)) {
|
|
12643
|
-
return void 0;
|
|
12644
|
-
}
|
|
12522
|
+
function formatSchema(schema, indent = " ") {
|
|
12523
|
+
if (!isRecord(schema))
|
|
12524
|
+
return `${indent}No parameters defined.`;
|
|
12525
|
+
const props = schema.properties;
|
|
12526
|
+
if (!isRecord(props) || Object.keys(props).length === 0) {
|
|
12527
|
+
return `${indent}No parameters defined.`;
|
|
12645
12528
|
}
|
|
12646
|
-
|
|
12529
|
+
const required = new Set(Array.isArray(schema.required) ? schema.required : []);
|
|
12530
|
+
const lines = Object.entries(props).map(([key, value]) => {
|
|
12531
|
+
const type = isRecord(value) && typeof value.type === "string" ? value.type : "any";
|
|
12532
|
+
const desc = isRecord(value) && typeof value.description === "string" ? value.description : "";
|
|
12533
|
+
const req = required.has(key) ? " [required]" : "";
|
|
12534
|
+
return `${indent}- ${key} (${type})${req}${desc ? " - " + desc : ""}`;
|
|
12535
|
+
});
|
|
12536
|
+
return lines.join("\n");
|
|
12647
12537
|
}
|
|
12648
|
-
function
|
|
12649
|
-
|
|
12650
|
-
|
|
12651
|
-
return void 0;
|
|
12652
|
-
if (cloneJsonObject(object3.structuredContent))
|
|
12653
|
-
return object3.structuredContent;
|
|
12654
|
-
if (!Array.isArray(object3.content) || object3.content.length !== 1)
|
|
12655
|
-
return void 0;
|
|
12656
|
-
const content = cloneJsonObject(object3.content[0]);
|
|
12657
|
-
if (!content || content.type !== "text" || typeof content.text !== "string")
|
|
12658
|
-
return void 0;
|
|
12538
|
+
function serializeToolResult(value) {
|
|
12539
|
+
if (typeof value === "string")
|
|
12540
|
+
return value;
|
|
12659
12541
|
try {
|
|
12660
|
-
|
|
12542
|
+
const serialized = JSON.stringify(value);
|
|
12543
|
+
return serialized === void 0 ? "\u5DE5\u5177\u6267\u884C\u5B8C\u6210\u3002" : serialized;
|
|
12661
12544
|
} catch {
|
|
12662
|
-
return
|
|
12545
|
+
return "\u5DE5\u5177\u6267\u884C\u5B8C\u6210\u3002";
|
|
12663
12546
|
}
|
|
12664
12547
|
}
|
|
12665
|
-
function
|
|
12666
|
-
return {
|
|
12548
|
+
function escapeRegExp(text) {
|
|
12549
|
+
return text.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
|
|
12667
12550
|
}
|
|
12668
|
-
function
|
|
12669
|
-
return
|
|
12551
|
+
function truncate(text, max2) {
|
|
12552
|
+
return text.length > max2 ? `${text.slice(0, max2)}\u2026` : text;
|
|
12670
12553
|
}
|
|
12671
|
-
function
|
|
12672
|
-
return value
|
|
12554
|
+
function isRecord(value) {
|
|
12555
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
12673
12556
|
}
|
|
12674
|
-
|
|
12675
|
-
|
|
12676
|
-
|
|
12677
|
-
|
|
12678
|
-
|
|
12679
|
-
|
|
12680
|
-
|
|
12681
|
-
|
|
12682
|
-
|
|
12683
|
-
|
|
12684
|
-
|
|
12685
|
-
|
|
12686
|
-
|
|
12687
|
-
|
|
12688
|
-
|
|
12689
|
-
|
|
12690
|
-
|
|
12691
|
-
|
|
12692
|
-
|
|
12693
|
-
|
|
12694
|
-
continue;
|
|
12695
|
-
}
|
|
12696
|
-
if (assistantIndex >= 0 && index < assistantIndex && visible[index].role === "user") {
|
|
12697
|
-
userIndex = index;
|
|
12698
|
-
break;
|
|
12699
|
-
}
|
|
12700
|
-
}
|
|
12701
|
-
if (userIndex < 0 || assistantIndex < 0)
|
|
12702
|
-
return void 0;
|
|
12703
|
-
const lastUserMessage = visible[userIndex].content;
|
|
12704
|
-
const assistantTokens = estimateTokens(visible[assistantIndex].content);
|
|
12705
|
-
const userTokens = estimateTokens(lastUserMessage);
|
|
12706
|
-
const assistantBudget = Math.min(assistantTokens, 1500);
|
|
12707
|
-
const userBudget = Math.min(userTokens, FOLLOW_UP_CONTEXT_TOKEN_BUDGET - assistantBudget);
|
|
12708
|
-
let remaining = FOLLOW_UP_CONTEXT_TOKEN_BUDGET - assistantBudget - userBudget;
|
|
12709
|
-
const messages = [
|
|
12710
|
-
{
|
|
12711
|
-
role: "user",
|
|
12712
|
-
content: truncateToEstimatedTokens(lastUserMessage, userBudget)
|
|
12557
|
+
var MCP_BATCH_SEARCH_MAX_ITEMS = 5;
|
|
12558
|
+
var MCP_PROXY_DESCRIPTION = [
|
|
12559
|
+
"MCP \u7F51\u5173:\u53D1\u73B0\u5E76\u8C03\u7528 MCP server \u63D0\u4F9B\u7684\u5DE5\u5177\u3002\u53EF\u7528\u52A8\u4F5C:",
|
|
12560
|
+
"- mcp({}) \u6216 mcp({ status: true }):\u67E5\u770B\u6240\u6709 server \u8FDE\u63A5\u72B6\u6001\u4E0E\u5DE5\u5177\u6570\u3002",
|
|
12561
|
+
'- mcp({ connect: "server" }):\u8FDE\u63A5\u5E76\u5237\u65B0\u67D0 server \u7684\u5DE5\u5177\u5217\u8868\u3002',
|
|
12562
|
+
'- mcp({ server: "name" }):\u5217\u51FA\u8BE5 server \u7684\u5DE5\u5177\u3002',
|
|
12563
|
+
'- mcp({ search: "\u5173\u952E\u8BCD" }):\u6309\u540D\u79F0/\u63CF\u8FF0\u6A21\u7CCA\u641C\u7D22\u5DE5\u5177(\u7A7A\u683C\u5206\u8BCD OR)\u3002',
|
|
12564
|
+
'- mcp({ searches: ["\u5173\u952E\u8BCD1", "\u5173\u952E\u8BCD2"] }):\u4E00\u6B21\u6267\u884C\u591A\u7EC4\u641C\u7D22\u5E76\u6309 query \u5206\u7EC4\u8FD4\u56DE(\u6700\u591A 5 \u7EC4)\u3002',
|
|
12565
|
+
'- mcp({ describe: "toolName" }):\u67E5\u770B\u67D0\u5DE5\u5177\u7684\u53C2\u6570 schema\u3002',
|
|
12566
|
+
'- mcp({ tool: "toolName", args: { ... } }):\u8C03\u7528\u67D0\u5DE5\u5177,args \u4E3A\u5BF9\u8C61\u3002',
|
|
12567
|
+
"\u53EF\u7528 server \u540D\u89C1 mcp({}) \u8F93\u51FA\u3002\u4EC5\u5728\u786E\u9700\u8C03\u7528 MCP \u5DE5\u5177\u65F6\u4F7F\u7528\u672C\u5DE5\u5177\u3002",
|
|
12568
|
+
"\u4EC5 mcp({}) \u5217\u51FA\u7684\u5DE5\u5177\u53EF\u8C03\u7528(\u672A\u58F0\u660E\u7684\u5DE5\u5177\u4E0D\u4F1A\u51FA\u73B0)\u3002"
|
|
12569
|
+
].join("\n");
|
|
12570
|
+
var MCP_PROXY_INPUT_SCHEMA = {
|
|
12571
|
+
type: "object",
|
|
12572
|
+
additionalProperties: false,
|
|
12573
|
+
properties: {
|
|
12574
|
+
status: {
|
|
12575
|
+
type: "boolean",
|
|
12576
|
+
description: "\u67E5\u770B\u6240\u6709 server \u8FDE\u63A5\u72B6\u6001(\u7B49\u4EF7\u4E8E\u7A7A\u5165\u53C2)"
|
|
12713
12577
|
},
|
|
12714
|
-
{
|
|
12715
|
-
|
|
12716
|
-
|
|
12717
|
-
}
|
|
12718
|
-
|
|
12719
|
-
|
|
12720
|
-
|
|
12721
|
-
|
|
12722
|
-
|
|
12723
|
-
|
|
12724
|
-
|
|
12725
|
-
|
|
12726
|
-
|
|
12727
|
-
|
|
12728
|
-
|
|
12729
|
-
|
|
12730
|
-
|
|
12731
|
-
|
|
12732
|
-
|
|
12578
|
+
connect: {
|
|
12579
|
+
type: "string",
|
|
12580
|
+
description: "\u663E\u5F0F\u8FDE\u63A5\u5E76\u5237\u65B0\u67D0 server(serverName \u6216 resourceId)"
|
|
12581
|
+
},
|
|
12582
|
+
server: { type: "string", description: "\u6307\u5B9A server:\u5217\u5DE5\u5177\u6216\u6D88\u6B67\u5DE5\u5177\u8C03\u7528" },
|
|
12583
|
+
search: {
|
|
12584
|
+
type: "string",
|
|
12585
|
+
description: "\u6309\u540D\u79F0/\u63CF\u8FF0\u6A21\u7CCA\u641C\u7D22\u5DE5\u5177(\u7A7A\u683C\u5206\u8BCD OR)"
|
|
12586
|
+
},
|
|
12587
|
+
searches: {
|
|
12588
|
+
type: "array",
|
|
12589
|
+
description: "\u6279\u91CF\u641C\u7D22\u5173\u952E\u8BCD\uFF1B\u4E00\u6B21\u63D0\u4EA4\u6240\u6709\u5019\u9009 query\uFF0C\u6309 query \u5206\u7EC4\u8FD4\u56DE",
|
|
12590
|
+
minItems: 1,
|
|
12591
|
+
maxItems: MCP_BATCH_SEARCH_MAX_ITEMS,
|
|
12592
|
+
items: { type: "string", minLength: 1 }
|
|
12593
|
+
},
|
|
12594
|
+
describe: { type: "string", description: "\u67E5\u770B\u67D0\u5DE5\u5177\u7684\u53C2\u6570 schema" },
|
|
12595
|
+
tool: { type: "string", description: "\u8981\u8C03\u7528\u7684\u5DE5\u5177\u540D(\u53EF\u914D\u5408 server \u6D88\u6B67)" },
|
|
12596
|
+
args: {
|
|
12597
|
+
type: "object",
|
|
12598
|
+
description: "\u5DE5\u5177\u5165\u53C2\u5BF9\u8C61(\u4EC5\u5728 tool \u6307\u5B9A\u65F6\u6709\u6548)",
|
|
12599
|
+
additionalProperties: true
|
|
12733
12600
|
}
|
|
12734
|
-
const userMessage = truncateToEstimatedTokens(entry.userMessage, entryRemaining);
|
|
12735
|
-
if (!userMessage)
|
|
12736
|
-
continue;
|
|
12737
|
-
entryRemaining -= estimateTokens(userMessage);
|
|
12738
|
-
remaining = entryRemaining;
|
|
12739
|
-
selectedHistory.unshift({ userMessage, followUpQuestions });
|
|
12740
12601
|
}
|
|
12741
|
-
|
|
12742
|
-
|
|
12743
|
-
|
|
12744
|
-
|
|
12745
|
-
|
|
12746
|
-
|
|
12747
|
-
|
|
12748
|
-
|
|
12749
|
-
|
|
12750
|
-
return void 0;
|
|
12751
|
-
return {
|
|
12752
|
-
userMessage,
|
|
12753
|
-
followUpQuestions: normalizeFollowUpQuestionStrings(entry.followUpQuestions)
|
|
12754
|
-
};
|
|
12755
|
-
}
|
|
12756
|
-
function normalizeFollowUpQuestionStrings(value) {
|
|
12757
|
-
if (!Array.isArray(value))
|
|
12758
|
-
return [];
|
|
12759
|
-
const seen = /* @__PURE__ */ new Set();
|
|
12760
|
-
const result = [];
|
|
12761
|
-
for (const candidate of value) {
|
|
12762
|
-
if (typeof candidate !== "string")
|
|
12763
|
-
continue;
|
|
12764
|
-
const normalized = candidate.replace(/\s+/gu, " ").trim();
|
|
12765
|
-
if (!normalized || normalized.length > FOLLOW_UP_QUESTION_MAX_LENGTH || seen.has(normalized))
|
|
12766
|
-
continue;
|
|
12767
|
-
seen.add(normalized);
|
|
12768
|
-
result.push(normalized);
|
|
12769
|
-
if (result.length === FOLLOW_UP_QUESTION_MAX_COUNT)
|
|
12770
|
-
break;
|
|
12602
|
+
};
|
|
12603
|
+
|
|
12604
|
+
// ../linkque-agent-runtime/dist/esm/core/homeData.js
|
|
12605
|
+
async function executeHomeData(input) {
|
|
12606
|
+
let request;
|
|
12607
|
+
try {
|
|
12608
|
+
request = parseHomeDataRequest(input.request);
|
|
12609
|
+
} catch (error) {
|
|
12610
|
+
return failure("INVALID_INPUT", error instanceof Error ? error.message : "\u9996\u9875\u5361\u7247\u8BF7\u6C42\u65E0\u6548", false);
|
|
12771
12611
|
}
|
|
12772
|
-
|
|
12773
|
-
|
|
12774
|
-
|
|
12775
|
-
|
|
12776
|
-
|
|
12777
|
-
|
|
12778
|
-
|
|
12779
|
-
|
|
12780
|
-
|
|
12781
|
-
|
|
12782
|
-
|
|
12783
|
-
|
|
12784
|
-
|
|
12785
|
-
|
|
12786
|
-
|
|
12787
|
-
|
|
12612
|
+
if (request.agentId !== input.protocol.agentId) {
|
|
12613
|
+
return failure("AGENT_MISMATCH", "\u9996\u9875\u6570\u636E\u8BF7\u6C42\u4E0E\u5F53\u524D Agent \u4E0D\u5339\u914D", false);
|
|
12614
|
+
}
|
|
12615
|
+
if (!input.userId.trim()) {
|
|
12616
|
+
return failure("AUTH_REQUIRED", "\u9996\u9875\u6570\u636E\u8BF7\u6C42\u7F3A\u5C11\u53EF\u4FE1\u7528\u6237\u8EAB\u4EFD", false);
|
|
12617
|
+
}
|
|
12618
|
+
const resources = input.protocol.agent.resources.filter((resource) => resource.resourceType === EResourceType.MCP || resource.resourceType === EResourceType.MCP_TOOL);
|
|
12619
|
+
const specs = aggregateMcpServers(resources, input.protocol.agentId);
|
|
12620
|
+
const spec = specs.find((candidate) => candidate.serverName === request.binding.mcpName);
|
|
12621
|
+
if (!spec) {
|
|
12622
|
+
return failure("MCP_NOT_BOUND", "\u5F53\u524D Agent \u672A\u7ED1\u5B9A\u8BE5 MCP", false);
|
|
12623
|
+
}
|
|
12624
|
+
if (spec.allowedTools && !spec.allowedTools.has(request.binding.toolName)) {
|
|
12625
|
+
return failure("TOOL_NOT_ALLOWED", "\u5F53\u524D Agent \u672A\u5141\u8BB8\u8C03\u7528\u8BE5 MCP \u5DE5\u5177", false);
|
|
12626
|
+
}
|
|
12627
|
+
const registry = new McpProxyTool(input.mcpServerProvider, specs);
|
|
12628
|
+
try {
|
|
12629
|
+
const handle = await registry.resolve(spec.resourceIds[0], request.binding.toolName, input.signal);
|
|
12630
|
+
if (!handle) {
|
|
12631
|
+
return failure("TOOL_NOT_ALLOWED", "MCP \u5DE5\u5177\u4E0D\u5B58\u5728\u6216\u4E0D\u53EF\u7528", false);
|
|
12788
12632
|
}
|
|
12633
|
+
const toolInput = {};
|
|
12634
|
+
for (const mapping of request.binding.input) {
|
|
12635
|
+
let value;
|
|
12636
|
+
switch (mapping.source.kind) {
|
|
12637
|
+
case "parameter":
|
|
12638
|
+
value = readJsonPointer(request.parameters, mapping.source.pointer);
|
|
12639
|
+
break;
|
|
12640
|
+
case "runtime-context":
|
|
12641
|
+
value = mapping.source.key === "agentId" ? input.protocol.agentId : input.userId;
|
|
12642
|
+
break;
|
|
12643
|
+
case "literal":
|
|
12644
|
+
value = mapping.source.value;
|
|
12645
|
+
break;
|
|
12646
|
+
}
|
|
12647
|
+
if (value === void 0 || !writeJsonPointer(toolInput, mapping.target, value)) {
|
|
12648
|
+
return failure("INVALID_INPUT", `\u65E0\u6CD5\u751F\u6210 MCP \u5165\u53C2 ${mapping.target}`, false);
|
|
12649
|
+
}
|
|
12650
|
+
}
|
|
12651
|
+
let result;
|
|
12652
|
+
try {
|
|
12653
|
+
result = await handle.execute(toolInput, input.signal);
|
|
12654
|
+
} catch (error) {
|
|
12655
|
+
return failure("EXECUTION_FAILED", error instanceof Error ? error.message : "MCP \u5DE5\u5177\u8C03\u7528\u5931\u8D25", true);
|
|
12656
|
+
}
|
|
12657
|
+
const structured = readStructuredContent2(result);
|
|
12658
|
+
let cardData = structured;
|
|
12659
|
+
if (request.binding.transform) {
|
|
12660
|
+
try {
|
|
12661
|
+
cardData = executeStructuralTransform(request.binding.transform, structured);
|
|
12662
|
+
} catch (error) {
|
|
12663
|
+
return failure("TRANSFORM_FAILED", error instanceof Error ? error.message : "\u9996\u9875\u5361\u7247\u6570\u636E\u8F6C\u6362\u5931\u8D25", false);
|
|
12664
|
+
}
|
|
12665
|
+
}
|
|
12666
|
+
const cardDataObject = cloneJsonObject(cardData);
|
|
12667
|
+
if (!cardDataObject) {
|
|
12668
|
+
return failure("INVALID_CARD_DATA", "\u9996\u9875\u5361\u7247\u6570\u636E\u5FC5\u987B\u662F JSON object", false);
|
|
12669
|
+
}
|
|
12670
|
+
return { ok: true, data: cardDataObject };
|
|
12671
|
+
} finally {
|
|
12672
|
+
await registry.closeAll();
|
|
12789
12673
|
}
|
|
12790
|
-
flushAscii();
|
|
12791
|
-
return Math.max(tokens, 1);
|
|
12792
12674
|
}
|
|
12793
|
-
function
|
|
12794
|
-
|
|
12795
|
-
|
|
12796
|
-
|
|
12797
|
-
|
|
12798
|
-
|
|
12799
|
-
|
|
12800
|
-
|
|
12801
|
-
|
|
12802
|
-
|
|
12803
|
-
|
|
12675
|
+
function readStructuredContent2(result) {
|
|
12676
|
+
const object3 = cloneJsonObject(result);
|
|
12677
|
+
if (!object3)
|
|
12678
|
+
return void 0;
|
|
12679
|
+
if (cloneJsonObject(object3.structuredContent)) {
|
|
12680
|
+
return object3.structuredContent;
|
|
12681
|
+
}
|
|
12682
|
+
if (!Array.isArray(object3.content) || object3.content.length !== 1) {
|
|
12683
|
+
return void 0;
|
|
12684
|
+
}
|
|
12685
|
+
const content = cloneJsonObject(object3.content[0]);
|
|
12686
|
+
if (!content || content.type !== "text" || typeof content.text !== "string") {
|
|
12687
|
+
return void 0;
|
|
12688
|
+
}
|
|
12689
|
+
try {
|
|
12690
|
+
return JSON.parse(content.text);
|
|
12691
|
+
} catch {
|
|
12692
|
+
return void 0;
|
|
12804
12693
|
}
|
|
12805
|
-
return text.slice(0, low).trim();
|
|
12806
12694
|
}
|
|
12807
|
-
|
|
12808
|
-
|
|
12809
|
-
import { randomUUID } from "node:crypto";
|
|
12810
|
-
|
|
12811
|
-
// ../linkque-agent-runtime/dist/esm/core/mcpNaming.js
|
|
12812
|
-
import { createHash } from "node:crypto";
|
|
12813
|
-
function stableToolName(prefix, raw) {
|
|
12814
|
-
const readable = raw.replace(/[^A-Za-z0-9_-]+/gu, "_").replace(/^_+|_+$/gu, "").slice(0, 38) || "resource";
|
|
12815
|
-
const digest = createHash("sha256").update(raw).digest("hex").slice(0, 10);
|
|
12816
|
-
return `${prefix}_${readable}_${digest}`;
|
|
12695
|
+
function failure(code, message, retryable) {
|
|
12696
|
+
return { ok: false, error: { code, message, retryable } };
|
|
12817
12697
|
}
|
|
12818
12698
|
|
|
12819
|
-
// ../linkque-agent-runtime/dist/esm/core/
|
|
12820
|
-
var
|
|
12821
|
-
|
|
12822
|
-
|
|
12823
|
-
|
|
12824
|
-
|
|
12825
|
-
|
|
12826
|
-
|
|
12827
|
-
|
|
12828
|
-
|
|
12829
|
-
|
|
12830
|
-
|
|
12831
|
-
|
|
12832
|
-
|
|
12833
|
-
|
|
12834
|
-
|
|
12835
|
-
|
|
12836
|
-
|
|
12837
|
-
|
|
12838
|
-
...hasLabel ? [e.label] : [],
|
|
12839
|
-
...hasValue ? [e.value ?? null] : [],
|
|
12840
|
-
...attributes.map((path2) => e.attributes?.[path2] ?? null)
|
|
12841
|
-
]);
|
|
12842
|
-
const total = coverage.itemCount ?? (coverage.present ? 1 : 0);
|
|
12843
|
-
return {
|
|
12844
|
-
field: coverage.fieldId,
|
|
12845
|
-
label: short(coverage.label),
|
|
12846
|
-
present: coverage.present,
|
|
12847
|
-
total,
|
|
12848
|
-
included: rows.length,
|
|
12849
|
-
complete: echo && coverage.present && rows.length === total && !entities.some((e) => e.truncated) && !receipt.omissions.some((o) => o.fieldId === coverage.fieldId),
|
|
12850
|
-
columns,
|
|
12851
|
-
rows
|
|
12852
|
-
};
|
|
12853
|
-
});
|
|
12854
|
-
const snapshot = {
|
|
12855
|
-
version: 1,
|
|
12856
|
-
groups,
|
|
12857
|
-
omissions: receipt.omissions.map((o) => ({ ...o }))
|
|
12858
|
-
};
|
|
12859
|
-
while (bytes(snapshot) > MAX_BYTES) {
|
|
12860
|
-
let group;
|
|
12861
|
-
for (let i = groups.length - 1; i >= 0; i--) {
|
|
12862
|
-
if (groups[i].rows.length > 0) {
|
|
12863
|
-
group = groups[i];
|
|
12864
|
-
break;
|
|
12865
|
-
}
|
|
12699
|
+
// ../linkque-agent-runtime/dist/esm/core/cardActionDiagnostics.js
|
|
12700
|
+
var CardActionDiagnostics = class {
|
|
12701
|
+
sink;
|
|
12702
|
+
layer;
|
|
12703
|
+
operation;
|
|
12704
|
+
stage = "validate";
|
|
12705
|
+
context;
|
|
12706
|
+
debugLog;
|
|
12707
|
+
startedAt = performance.now();
|
|
12708
|
+
sensitiveValues = /* @__PURE__ */ new Set();
|
|
12709
|
+
constructor(sink, context, layer = "runtime", operation = "a2uiAction.execute") {
|
|
12710
|
+
this.sink = sink;
|
|
12711
|
+
this.layer = layer;
|
|
12712
|
+
this.operation = operation;
|
|
12713
|
+
this.context = { ...context };
|
|
12714
|
+
if (sink) {
|
|
12715
|
+
this.debugLog = (line, scope) => {
|
|
12716
|
+
this.write("diagnostic", { message: line }, scope);
|
|
12717
|
+
};
|
|
12866
12718
|
}
|
|
12867
|
-
snapshot.truncated = true;
|
|
12868
|
-
if (group) {
|
|
12869
|
-
group.rows.pop();
|
|
12870
|
-
group.included = group.rows.length;
|
|
12871
|
-
group.complete = false;
|
|
12872
|
-
} else if (groups.length) {
|
|
12873
|
-
groups.pop();
|
|
12874
|
-
} else if (snapshot.omissions.length) {
|
|
12875
|
-
snapshot.omissions.pop();
|
|
12876
|
-
} else
|
|
12877
|
-
break;
|
|
12878
12719
|
}
|
|
12879
|
-
|
|
12880
|
-
|
|
12881
|
-
|
|
12882
|
-
|
|
12720
|
+
/** 根据绑定路径登记敏感值;解析仅为日志服务,失败不影响业务。 */
|
|
12721
|
+
protect(readValue) {
|
|
12722
|
+
if (!this.sink)
|
|
12723
|
+
return;
|
|
12724
|
+
try {
|
|
12725
|
+
const value = readValue();
|
|
12726
|
+
if (typeof value === "string" && value)
|
|
12727
|
+
this.sensitiveValues.add(value);
|
|
12728
|
+
} catch {
|
|
12729
|
+
}
|
|
12730
|
+
}
|
|
12731
|
+
write(event, fields = {}, scope = EDebugLogScope.A2UI_ACTION) {
|
|
12732
|
+
if (!this.sink)
|
|
12733
|
+
return;
|
|
12734
|
+
try {
|
|
12735
|
+
const record2 = { ...this.context, ...fields };
|
|
12736
|
+
if (record2.message)
|
|
12737
|
+
record2.message = sanitizeDiagnosticText(redactKnownValues(record2.message, this.sensitiveValues));
|
|
12738
|
+
if (record2.errorName)
|
|
12739
|
+
record2.errorName = sanitizeDiagnosticText(redactKnownValues(record2.errorName, this.sensitiveValues));
|
|
12740
|
+
if ("input" in record2)
|
|
12741
|
+
record2.input = sanitizeLogValue(record2.input, this.sensitiveValues);
|
|
12742
|
+
if ("output" in record2)
|
|
12743
|
+
record2.output = sanitizeLogValue(record2.output, this.sensitiveValues);
|
|
12744
|
+
this.sink(JSON.stringify({
|
|
12745
|
+
operation: this.operation,
|
|
12746
|
+
level: "info",
|
|
12747
|
+
layer: this.layer,
|
|
12748
|
+
event,
|
|
12749
|
+
stage: this.stage,
|
|
12750
|
+
...record2,
|
|
12751
|
+
costMs: Math.max(0, Math.round(performance.now() - this.startedAt))
|
|
12752
|
+
}), scope);
|
|
12753
|
+
} catch {
|
|
12754
|
+
}
|
|
12755
|
+
}
|
|
12756
|
+
error(event, error) {
|
|
12757
|
+
if (!this.sink)
|
|
12758
|
+
return;
|
|
12759
|
+
try {
|
|
12760
|
+
this.write(event, {
|
|
12761
|
+
errorName: error instanceof Error ? error.name : typeof error,
|
|
12762
|
+
message: error instanceof Error ? error.message : typeof error === "string" ? error : "\u975E\u6807\u51C6\u5F02\u5E38"
|
|
12763
|
+
});
|
|
12764
|
+
} catch {
|
|
12765
|
+
}
|
|
12766
|
+
}
|
|
12767
|
+
};
|
|
12768
|
+
function sanitizeLogValue(value, sensitiveValues) {
|
|
12769
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
12770
|
+
let nodes = 200;
|
|
12771
|
+
let characters = 4e3;
|
|
12772
|
+
const visit = (item, depth, key = "") => {
|
|
12773
|
+
if (/authorization|cookie|token|secret|password|api[_-]?key|access[_-]?key|private[_-]?key|^(?:ak|sk|userId|orderId|tradeNO|phone|mobile|email|idCard)$/iu.test(key)) {
|
|
12774
|
+
if (typeof item === "string" && item)
|
|
12775
|
+
sensitiveValues.add(item);
|
|
12776
|
+
return "<redacted>";
|
|
12777
|
+
}
|
|
12778
|
+
if (--nodes < 0 || depth > 8 || characters <= 0)
|
|
12779
|
+
return "<truncated>";
|
|
12780
|
+
if (item === null || typeof item === "number" || typeof item === "boolean")
|
|
12781
|
+
return item;
|
|
12782
|
+
if (typeof item === "string") {
|
|
12783
|
+
if (/^\s*[{[]/u.test(item)) {
|
|
12784
|
+
if (item.length > 16e3)
|
|
12785
|
+
return "<truncated JSON>";
|
|
12786
|
+
try {
|
|
12787
|
+
return JSON.stringify(visit(JSON.parse(item), depth + 1));
|
|
12788
|
+
} catch {
|
|
12789
|
+
return "<invalid JSON>";
|
|
12790
|
+
}
|
|
12791
|
+
}
|
|
12792
|
+
const text2 = sanitizeDiagnosticText(redactKnownValues(item, sensitiveValues));
|
|
12793
|
+
const result2 = text2.slice(0, Math.min(512, characters));
|
|
12794
|
+
characters -= result2.length;
|
|
12795
|
+
return result2.length < item.length ? `${result2}<truncated>` : result2;
|
|
12796
|
+
}
|
|
12797
|
+
if (typeof item !== "object")
|
|
12798
|
+
return `<${typeof item}>`;
|
|
12799
|
+
if (seen.has(item))
|
|
12800
|
+
return "<circular>";
|
|
12801
|
+
seen.add(item);
|
|
12802
|
+
try {
|
|
12803
|
+
const keys = Object.keys(item);
|
|
12804
|
+
const result2 = /* @__PURE__ */ Object.create(null);
|
|
12805
|
+
for (const name of keys.slice(0, 20)) {
|
|
12806
|
+
const descriptor = Object.getOwnPropertyDescriptor(item, name);
|
|
12807
|
+
if (name === "value" && Object.getOwnPropertyDescriptor(item, "kind")?.value === "literal" && typeof descriptor?.value === "string" && descriptor.value)
|
|
12808
|
+
sensitiveValues.add(descriptor.value);
|
|
12809
|
+
result2[name.slice(0, 100)] = name === "value" && Object.getOwnPropertyDescriptor(item, "kind")?.value === "literal" ? "<redacted>" : descriptor && "value" in descriptor ? visit(descriptor.value, depth + 1, name) : "<accessor>";
|
|
12810
|
+
}
|
|
12811
|
+
if (Array.isArray(item)) {
|
|
12812
|
+
const values = Object.values(result2);
|
|
12813
|
+
if (keys.length > 20)
|
|
12814
|
+
values.push("<truncated>");
|
|
12815
|
+
return values;
|
|
12816
|
+
}
|
|
12817
|
+
if (keys.length > 20)
|
|
12818
|
+
result2["<truncated>"] = true;
|
|
12819
|
+
return result2;
|
|
12820
|
+
} catch {
|
|
12821
|
+
return "<unreadable>";
|
|
12822
|
+
} finally {
|
|
12823
|
+
seen.delete(item);
|
|
12824
|
+
}
|
|
12825
|
+
};
|
|
12826
|
+
const result = visit(value, 0);
|
|
12827
|
+
const text = JSON.stringify(result);
|
|
12828
|
+
return text.length > 6e3 ? { truncated: true, preview: text.slice(0, 2e3) } : result;
|
|
12883
12829
|
}
|
|
12884
|
-
function
|
|
12885
|
-
return value.
|
|
12830
|
+
function sanitizeDiagnosticText(value) {
|
|
12831
|
+
return value.replace(/\b(?:body|arguments|payload|headers)\s*[:=][\s\S]*/giu, "<payload>").replace(/\{[\s\S]*/gu, "<payload>").replace(/https?:\/\/[^\s]+/giu, "<url>").replace(/\b(?:authorization|cookie|set-cookie)\s*[:=][^\r\n]*/giu, "<redacted>").replace(/\bbearer\s+[^\s,;]+/giu, "Bearer <redacted>").replace(/\b((?:api[_-]?key|access[_-]?key|token|secret|password|ak|sk|tradeNO|userId|orderId)\s*[:=]\s*)(?:"[^"]*"|'[^']*'|[^\s,;]+)/giu, "$1<redacted>").replace(/\s+/gu, " ").slice(0, 300);
|
|
12886
12832
|
}
|
|
12887
|
-
function
|
|
12888
|
-
|
|
12889
|
-
|
|
12890
|
-
|
|
12891
|
-
|
|
12892
|
-
if (!schema || typeof schema !== "object" || Array.isArray(schema))
|
|
12893
|
-
return short(fallback);
|
|
12894
|
-
const properties = schema.properties;
|
|
12895
|
-
if (!properties || typeof properties !== "object" || Array.isArray(properties) || !Object.hasOwn(properties, token))
|
|
12896
|
-
return short(fallback);
|
|
12897
|
-
schema = properties[token];
|
|
12898
|
-
}
|
|
12899
|
-
const title = schema && typeof schema === "object" && !Array.isArray(schema) ? schema.title : void 0;
|
|
12900
|
-
return short(typeof title === "string" && title ? title : fallback);
|
|
12833
|
+
function redactKnownValues(text, sensitiveValues) {
|
|
12834
|
+
let result = text;
|
|
12835
|
+
for (const sensitive of sensitiveValues)
|
|
12836
|
+
result = result.replaceAll(sensitive, "<redacted>");
|
|
12837
|
+
return result;
|
|
12901
12838
|
}
|
|
12902
12839
|
|
|
12903
|
-
// ../linkque-agent-runtime/dist/esm/core/
|
|
12904
|
-
|
|
12905
|
-
|
|
12906
|
-
|
|
12907
|
-
|
|
12908
|
-
|
|
12909
|
-
|
|
12910
|
-
|
|
12911
|
-
|
|
12912
|
-
|
|
12913
|
-
if (config.schemaVersion !== "2") {
|
|
12914
|
-
throw new Error(`Schema v${config.schemaVersion} cards must be executed through the card data-source loop`);
|
|
12915
|
-
}
|
|
12916
|
-
const invocation = cloneJsonObject2(args, "Presentation Tool arguments");
|
|
12917
|
-
const viewModel = await executeA2UICardResolver({
|
|
12918
|
-
resolver: config.resolver,
|
|
12919
|
-
fields: config.fields,
|
|
12920
|
-
presentationInputSchema: config.presentationInputSchema,
|
|
12921
|
-
argsSchema: config.a2uiTemplate.argsSchema,
|
|
12922
|
-
invocation,
|
|
12923
|
-
runtimeContext,
|
|
12924
|
-
tools: await Promise.all(config.resolver.calls.map(async (call) => {
|
|
12925
|
-
const handle = await registry.resolve(call.mcpResourceId, call.toolName, signal);
|
|
12926
|
-
if (!handle) {
|
|
12927
|
-
throw new Error(`MCP tool ${call.mcpResourceId}/${call.toolName} is unavailable`);
|
|
12928
|
-
}
|
|
12929
|
-
const inputSchema = normalizeMcpInputSchema(handle.inputSchema);
|
|
12930
|
-
return {
|
|
12931
|
-
resourceId: call.mcpResourceId,
|
|
12932
|
-
toolName: call.toolName,
|
|
12933
|
-
// 旧 MCP server 可能只在开放平台元数据中声明 Schema,但
|
|
12934
|
-
// tools/list 未返回。运行时不因此拒绝调用:有实时 Schema 时用它
|
|
12935
|
-
// 做额外校验,缺失时依赖 CARD 已固化的字段 Schema 与最终
|
|
12936
|
-
// ViewModel Schema 校验。
|
|
12937
|
-
inputSchema,
|
|
12938
|
-
outputSchema: isJsonObject(handle.outputSchema) ? handle.outputSchema : FALLBACK_OBJECT_SCHEMA,
|
|
12939
|
-
call: async (input, callSignal) => {
|
|
12940
|
-
const result = await handle.execute(supplementMcpInput(input, invocation, config.presentationInputSchema, inputSchema), callSignal);
|
|
12941
|
-
return { structuredContent: readStructuredContent2(result) };
|
|
12942
|
-
}
|
|
12943
|
-
};
|
|
12944
|
-
})),
|
|
12945
|
-
signal
|
|
12840
|
+
// ../linkque-agent-runtime/dist/esm/core/cardActionExecutor.js
|
|
12841
|
+
async function executeCardAction(input) {
|
|
12842
|
+
const diagnostics = new CardActionDiagnostics(input.debugLog, {
|
|
12843
|
+
agentId: input.protocol.agentId,
|
|
12844
|
+
threadId: input.runtimeContext.threadId
|
|
12845
|
+
});
|
|
12846
|
+
diagnostics.protect(() => input.runtimeContext.userId);
|
|
12847
|
+
diagnostics.protect(() => input.runtimeContext.tenantId);
|
|
12848
|
+
diagnostics.write("started", {
|
|
12849
|
+
input: { request: input.request, runtimeContext: input.runtimeContext }
|
|
12946
12850
|
});
|
|
12947
|
-
return createPresentationResult(resource, config, viewModel);
|
|
12948
|
-
}
|
|
12949
|
-
async function executeCardPresentationFromSources(resource, runtimeContext, sourceResults, signal) {
|
|
12950
|
-
const config = parseCardConfig(resource);
|
|
12951
|
-
if (config.schemaVersion !== "3" && config.schemaVersion !== "4") {
|
|
12952
|
-
throw new Error("Source-result rendering is only available for schema v3/v4 cards");
|
|
12953
|
-
}
|
|
12954
|
-
const missing = config.dataSources.filter((source) => source.required && !sourceResults.has(source.sourceId)).map((source) => source.sourceId);
|
|
12955
|
-
if (missing.length) {
|
|
12956
|
-
throw new Error(`Required card data sources are incomplete: ${missing.join(", ")}`);
|
|
12957
|
-
}
|
|
12958
|
-
const resolver = {
|
|
12959
|
-
version: 2,
|
|
12960
|
-
calls: config.dataSources.filter((source) => sourceResults.has(source.sourceId)).map((source) => ({
|
|
12961
|
-
callId: source.sourceId,
|
|
12962
|
-
mcpResourceId: source.mcpResourceId,
|
|
12963
|
-
toolName: source.toolName,
|
|
12964
|
-
inputBindings: []
|
|
12965
|
-
})),
|
|
12966
|
-
outputBindings: config.bindings.filter((binding) => sourceResults.has(binding.sourceId)).map((binding) => ({
|
|
12967
|
-
fieldId: binding.fieldId,
|
|
12968
|
-
callId: binding.sourceId,
|
|
12969
|
-
transform: binding.transform
|
|
12970
|
-
}))
|
|
12971
|
-
};
|
|
12972
|
-
let viewModel;
|
|
12973
12851
|
try {
|
|
12974
|
-
|
|
12975
|
-
|
|
12976
|
-
|
|
12977
|
-
|
|
12978
|
-
|
|
12979
|
-
properties: {},
|
|
12980
|
-
additionalProperties: false
|
|
12981
|
-
},
|
|
12982
|
-
argsSchema: config.a2uiTemplate.argsSchema,
|
|
12983
|
-
invocation: {},
|
|
12984
|
-
runtimeContext,
|
|
12985
|
-
tools: config.dataSources.filter((source) => sourceResults.has(source.sourceId)).map((source) => ({
|
|
12986
|
-
resourceId: source.mcpResourceId,
|
|
12987
|
-
toolName: source.toolName,
|
|
12988
|
-
inputSchema: FALLBACK_OBJECT_SCHEMA,
|
|
12989
|
-
outputSchema: source.outputSchema ?? FALLBACK_OBJECT_SCHEMA,
|
|
12990
|
-
call: async () => ({
|
|
12991
|
-
structuredContent: sourceResults.get(source.sourceId)
|
|
12992
|
-
})
|
|
12993
|
-
})),
|
|
12994
|
-
signal
|
|
12852
|
+
const response = await executeCardActionInternal(input, diagnostics);
|
|
12853
|
+
diagnostics.write(response.ok ? "completed" : "failed", response.ok ? { output: response } : {
|
|
12854
|
+
code: response.error.code,
|
|
12855
|
+
retryable: response.error.retryable,
|
|
12856
|
+
output: response
|
|
12995
12857
|
});
|
|
12858
|
+
return response;
|
|
12996
12859
|
} catch (error) {
|
|
12997
|
-
|
|
12998
|
-
const binding = config.bindings.find((candidate) => candidate.fieldId === error.fieldId);
|
|
12999
|
-
const field = config.fields.find((candidate) => candidate.fieldId === error.fieldId);
|
|
13000
|
-
throw new CardMappingError({
|
|
13001
|
-
code: error.code,
|
|
13002
|
-
cardId: resource.resourceId,
|
|
13003
|
-
fieldId: error.fieldId ?? "unknown",
|
|
13004
|
-
sourceId: binding?.sourceId ?? "unknown",
|
|
13005
|
-
expectedType: error.expectedType ?? field?.type ?? "unknown",
|
|
13006
|
-
actualType: error.actualType ?? "unknown",
|
|
13007
|
-
retryable: false
|
|
13008
|
-
});
|
|
13009
|
-
}
|
|
12860
|
+
diagnostics.error("failed", error);
|
|
13010
12861
|
throw error;
|
|
13011
12862
|
}
|
|
13012
|
-
return createPresentationResult(resource, config, viewModel);
|
|
13013
12863
|
}
|
|
13014
|
-
function
|
|
13015
|
-
|
|
13016
|
-
|
|
13017
|
-
|
|
13018
|
-
|
|
13019
|
-
|
|
13020
|
-
|
|
13021
|
-
|
|
13022
|
-
|
|
13023
|
-
|
|
13024
|
-
|
|
13025
|
-
};
|
|
13026
|
-
|
|
13027
|
-
|
|
13028
|
-
|
|
13029
|
-
|
|
13030
|
-
|
|
13031
|
-
|
|
13032
|
-
|
|
13033
|
-
|
|
13034
|
-
|
|
13035
|
-
|
|
13036
|
-
|
|
13037
|
-
|
|
13038
|
-
|
|
12864
|
+
async function executeCardActionInternal(input, diagnostics) {
|
|
12865
|
+
let request;
|
|
12866
|
+
try {
|
|
12867
|
+
request = parseA2UIActionExecuteRequest(input.request);
|
|
12868
|
+
} catch (error) {
|
|
12869
|
+
return failure2("INVALID_INPUT", errorMessage(error, "\u652F\u4ED8\u52A8\u4F5C\u8BF7\u6C42\u65E0\u6548"), false);
|
|
12870
|
+
}
|
|
12871
|
+
Object.assign(diagnostics.context, {
|
|
12872
|
+
invocationId: request.invocationId,
|
|
12873
|
+
cardRef: request.source.cardRef,
|
|
12874
|
+
cardRevision: request.source.cardRevision
|
|
12875
|
+
});
|
|
12876
|
+
if (request.agentId !== input.protocol.agentId)
|
|
12877
|
+
return failure2("AGENT_MISMATCH", "\u652F\u4ED8\u52A8\u4F5C\u4E0E\u5F53\u524D Agent \u4E0D\u5339\u914D", false);
|
|
12878
|
+
if (request.threadId !== input.runtimeContext.threadId)
|
|
12879
|
+
return failure2("INVALID_INPUT", "\u652F\u4ED8\u52A8\u4F5C\u4E0E\u5F53\u524D\u4F1A\u8BDD\u4E0D\u5339\u914D", false);
|
|
12880
|
+
if (!input.runtimeContext.userId.trim())
|
|
12881
|
+
return failure2("AUTH_REQUIRED", "\u652F\u4ED8\u52A8\u4F5C\u7F3A\u5C11\u53EF\u4FE1\u7528\u6237\u8EAB\u4EFD", false);
|
|
12882
|
+
diagnostics.write("validated", { output: request });
|
|
12883
|
+
diagnostics.stage = "binding";
|
|
12884
|
+
diagnostics.write("binding_started", { input: { source: request.source } });
|
|
12885
|
+
const resolved = resolvePaymentAction(input.protocol, request.source);
|
|
12886
|
+
if (!resolved.ok) {
|
|
12887
|
+
diagnostics.write("binding_failed", { output: resolved.response });
|
|
12888
|
+
return resolved.response;
|
|
12889
|
+
}
|
|
12890
|
+
const args = materializePaymentInput(resolved.payment.createTrade.input, request.input);
|
|
12891
|
+
if (!args)
|
|
12892
|
+
return failure2("INVALID_INPUT", "\u65E0\u6CD5\u751F\u6210\u521B\u5EFA\u4EA4\u6613 MCP \u5165\u53C2", false);
|
|
12893
|
+
const createTrade = resolved.payment.createTrade;
|
|
12894
|
+
Object.assign(diagnostics.context, {
|
|
12895
|
+
mcpResourceId: createTrade.mcpResourceId,
|
|
12896
|
+
toolName: createTrade.toolName
|
|
12897
|
+
});
|
|
12898
|
+
diagnostics.write("binding_resolved", { output: createTrade });
|
|
12899
|
+
diagnostics.stage = "tool_resolve";
|
|
12900
|
+
const registry = createRegistry(input.protocol, input.mcpServerProvider, diagnostics.debugLog);
|
|
12901
|
+
try {
|
|
12902
|
+
diagnostics.write("tool_resolve_started", { input: createTrade });
|
|
12903
|
+
const tool = await registry.resolve(createTrade.mcpResourceId, createTrade.toolName, input.signal);
|
|
12904
|
+
diagnostics.write("tool_resolved", {
|
|
12905
|
+
output: {
|
|
12906
|
+
found: Boolean(tool),
|
|
12907
|
+
inputSchema: tool?.inputSchema,
|
|
12908
|
+
outputSchema: tool?.outputSchema
|
|
13039
12909
|
}
|
|
12910
|
+
});
|
|
12911
|
+
if (!tool)
|
|
12912
|
+
return failure2("TOOL_NOT_ALLOWED", "\u521B\u5EFA\u4EA4\u6613 MCP Tool \u4E0D\u5B58\u5728\u6216\u672A\u6388\u6743", false);
|
|
12913
|
+
diagnostics.stage = "tool_input";
|
|
12914
|
+
const inputSchema = cloneJsonObject(tool.inputSchema);
|
|
12915
|
+
if (inputSchema && validateJsonSchemaValue(inputSchema, args).length > 0) {
|
|
12916
|
+
return failure2("INVALID_INPUT", "\u521B\u5EFA\u4EA4\u6613 MCP \u5165\u53C2\u4E0D\u7B26\u5408 Tool Schema", false);
|
|
13040
12917
|
}
|
|
13041
|
-
|
|
13042
|
-
|
|
13043
|
-
|
|
13044
|
-
|
|
13045
|
-
|
|
13046
|
-
|
|
13047
|
-
|
|
13048
|
-
|
|
13049
|
-
|
|
13050
|
-
|
|
13051
|
-
|
|
13052
|
-
|
|
13053
|
-
|
|
13054
|
-
|
|
13055
|
-
|
|
13056
|
-
|
|
13057
|
-
|
|
13058
|
-
|
|
13059
|
-
|
|
13060
|
-
|
|
13061
|
-
|
|
13062
|
-
}
|
|
13063
|
-
|
|
13064
|
-
|
|
13065
|
-
|
|
13066
|
-
|
|
12918
|
+
diagnostics.write("tool_input_completed", { output: { args } });
|
|
12919
|
+
let result;
|
|
12920
|
+
diagnostics.stage = "tool_execute";
|
|
12921
|
+
diagnostics.write("tool_started", { input: args });
|
|
12922
|
+
try {
|
|
12923
|
+
result = await tool.execute(args, input.signal);
|
|
12924
|
+
diagnostics.protect(() => readJsonPointer(readStructuredContent3(result), createTrade.tradeNoPath));
|
|
12925
|
+
diagnostics.write("tool_completed", { output: result });
|
|
12926
|
+
} catch (error) {
|
|
12927
|
+
diagnostics.error("tool_failed", error);
|
|
12928
|
+
return failure2("EXECUTION_FAILED", "\u521B\u5EFA\u4EA4\u6613\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5", true);
|
|
12929
|
+
}
|
|
12930
|
+
diagnostics.stage = "tool_output";
|
|
12931
|
+
const structured = readStructuredContent3(result);
|
|
12932
|
+
const tradeNO = readJsonPointer(structured, createTrade.tradeNoPath);
|
|
12933
|
+
diagnostics.write("tool_output_completed", {
|
|
12934
|
+
output: {
|
|
12935
|
+
structured,
|
|
12936
|
+
tradeNO,
|
|
12937
|
+
valid: typeof tradeNO === "string" && Boolean(tradeNO.trim()) && tradeNO.length <= 64
|
|
12938
|
+
}
|
|
12939
|
+
});
|
|
12940
|
+
if (typeof tradeNO !== "string" || !tradeNO.trim() || tradeNO.length > 64)
|
|
12941
|
+
return failure2("INVALID_OUTPUT", "\u521B\u5EFA\u4EA4\u6613 MCP \u672A\u8FD4\u56DE\u6709\u6548 tradeNO", false);
|
|
12942
|
+
return {
|
|
12943
|
+
ok: true,
|
|
12944
|
+
invocationId: request.invocationId,
|
|
12945
|
+
result: { kind: "payment", tradeNO }
|
|
12946
|
+
};
|
|
12947
|
+
} finally {
|
|
12948
|
+
const stage = diagnostics.stage;
|
|
12949
|
+
diagnostics.stage = "cleanup";
|
|
12950
|
+
diagnostics.write("cleanup_started", {
|
|
12951
|
+
input: { mcpResourceId: diagnostics.context.mcpResourceId }
|
|
12952
|
+
});
|
|
12953
|
+
await registry.closeAll();
|
|
12954
|
+
diagnostics.write("cleanup_completed", {
|
|
12955
|
+
output: { closeAttempted: true }
|
|
12956
|
+
});
|
|
12957
|
+
diagnostics.stage = stage;
|
|
13067
12958
|
}
|
|
13068
|
-
return parsed.data;
|
|
13069
12959
|
}
|
|
13070
|
-
function
|
|
13071
|
-
|
|
13072
|
-
|
|
13073
|
-
|
|
13074
|
-
|
|
13075
|
-
|
|
13076
|
-
|
|
13077
|
-
|
|
13078
|
-
|
|
13079
|
-
|
|
13080
|
-
|
|
12960
|
+
function resolvePaymentAction(protocol, source) {
|
|
12961
|
+
const card = protocol.agent.resources.find((resource) => resource.resourceType === EResourceType.CARD && resource.resourceId === source.cardRef);
|
|
12962
|
+
if (!card)
|
|
12963
|
+
return {
|
|
12964
|
+
ok: false,
|
|
12965
|
+
response: failure2("CARD_NOT_FOUND", "\u652F\u4ED8\u5361\u7247\u4E0D\u5B58\u5728", false)
|
|
12966
|
+
};
|
|
12967
|
+
if (card.config.revision !== source.cardRevision) {
|
|
12968
|
+
return {
|
|
12969
|
+
ok: false,
|
|
12970
|
+
response: failure2("CARD_REVISION_MISMATCH", "\u652F\u4ED8\u5361\u7247\u7248\u672C\u5DF2\u53D8\u5316\uFF0C\u8BF7\u5237\u65B0\u540E\u91CD\u8BD5", false)
|
|
12971
|
+
};
|
|
13081
12972
|
}
|
|
13082
|
-
|
|
13083
|
-
|
|
13084
|
-
|
|
13085
|
-
|
|
13086
|
-
|
|
13087
|
-
|
|
13088
|
-
|
|
13089
|
-
|
|
13090
|
-
|
|
12973
|
+
if (card.config.schemaVersion !== "4")
|
|
12974
|
+
return {
|
|
12975
|
+
ok: false,
|
|
12976
|
+
response: failure2("ACTION_NOT_FOUND", "\u652F\u4ED8\u52A8\u4F5C\u4E0D\u5B58\u5728", false)
|
|
12977
|
+
};
|
|
12978
|
+
const component = findSavedComponent(card.config.a2uiTemplate.content, source.surfaceId, source.componentId);
|
|
12979
|
+
if (!component)
|
|
12980
|
+
return {
|
|
12981
|
+
ok: false,
|
|
12982
|
+
response: failure2("ACTION_NOT_FOUND", "\u652F\u4ED8\u52A8\u4F5C\u4E0D\u5B58\u5728", false)
|
|
12983
|
+
};
|
|
12984
|
+
const payments = collectPaymentActions(component.action);
|
|
12985
|
+
if (payments.length !== 1)
|
|
12986
|
+
return {
|
|
12987
|
+
ok: false,
|
|
12988
|
+
response: failure2("ACTION_KIND_MISMATCH", "\u7EC4\u4EF6\u5FC5\u987B\u5305\u542B\u552F\u4E00\u652F\u4ED8\u52A8\u4F5C", false)
|
|
12989
|
+
};
|
|
12990
|
+
return { ok: true, payment: payments[0] };
|
|
13091
12991
|
}
|
|
13092
|
-
function
|
|
13093
|
-
|
|
13094
|
-
|
|
13095
|
-
|
|
12992
|
+
function findSavedComponent(content, surfaceId, componentId) {
|
|
12993
|
+
let parsed;
|
|
12994
|
+
try {
|
|
12995
|
+
parsed = JSON.parse(content);
|
|
12996
|
+
} catch {
|
|
12997
|
+
return void 0;
|
|
12998
|
+
}
|
|
12999
|
+
if (!Array.isArray(parsed))
|
|
13000
|
+
return void 0;
|
|
13001
|
+
const instructions = parsed.flatMap((entry) => {
|
|
13002
|
+
const record2 = cloneJsonObject(entry);
|
|
13003
|
+
return record2 && Array.isArray(record2.datas) ? record2.datas : [entry];
|
|
13004
|
+
});
|
|
13005
|
+
for (const instruction of instructions) {
|
|
13006
|
+
const record2 = cloneJsonObject(instruction);
|
|
13007
|
+
const update = record2 && cloneJsonObject(record2.updateComponents);
|
|
13008
|
+
if (!update || update.surfaceId !== surfaceId || !Array.isArray(update.components))
|
|
13096
13009
|
continue;
|
|
13097
|
-
|
|
13098
|
-
|
|
13099
|
-
|
|
13100
|
-
|
|
13101
|
-
|
|
13102
|
-
|
|
13103
|
-
|
|
13104
|
-
for (const operation of operations) {
|
|
13105
|
-
if (isJsonObject(operation) && typeof operation.surfaceId === "string") {
|
|
13106
|
-
surfaceIds.add(operation.surfaceId);
|
|
13107
|
-
}
|
|
13108
|
-
}
|
|
13109
|
-
}
|
|
13010
|
+
const component = update.components.find((candidate) => {
|
|
13011
|
+
const item = cloneJsonObject(candidate);
|
|
13012
|
+
return item?.id === componentId;
|
|
13013
|
+
});
|
|
13014
|
+
const result = cloneJsonObject(component);
|
|
13015
|
+
if (result)
|
|
13016
|
+
return result;
|
|
13110
13017
|
}
|
|
13111
|
-
return
|
|
13018
|
+
return void 0;
|
|
13112
13019
|
}
|
|
13113
|
-
function
|
|
13020
|
+
function collectPaymentActions(value) {
|
|
13021
|
+
const action = cloneJsonObject(value);
|
|
13022
|
+
if (!action)
|
|
13023
|
+
return [];
|
|
13024
|
+
const client = cloneJsonObject(action.client);
|
|
13025
|
+
if (client?.name === "payment.trade-pay") {
|
|
13026
|
+
const context = cloneJsonObject(client.context);
|
|
13027
|
+
return context ? [context] : [];
|
|
13028
|
+
}
|
|
13029
|
+
const select = cloneJsonObject(action.select);
|
|
13030
|
+
if (!select)
|
|
13031
|
+
return [];
|
|
13032
|
+
const cases = Array.isArray(select.cases) ? select.cases : [];
|
|
13114
13033
|
return [
|
|
13115
|
-
|
|
13116
|
-
|
|
13117
|
-
|
|
13118
|
-
"\u53EA\u63D0\u4F9B\u5DF2\u58F0\u660E\u7684\u6807\u8BC6\u6216\u7B5B\u9009\u53C2\u6570\u3002\u6743\u5A01\u4E1A\u52A1\u6570\u636E\u7531\u5E73\u53F0\u89E3\u6790\u83B7\u53D6\u3002"
|
|
13119
|
-
].join("\n");
|
|
13034
|
+
...cases.flatMap((entry) => collectPaymentActions(cloneJsonObject(entry)?.action)),
|
|
13035
|
+
...collectPaymentActions(select.default)
|
|
13036
|
+
];
|
|
13120
13037
|
}
|
|
13121
|
-
function
|
|
13122
|
-
|
|
13123
|
-
|
|
13124
|
-
return JSON.parse(JSON.stringify(value));
|
|
13038
|
+
function materializePaymentInput(definition, supplied) {
|
|
13039
|
+
const materialized = materializeValue(definition, supplied);
|
|
13040
|
+
return cloneJsonObject(materialized);
|
|
13125
13041
|
}
|
|
13126
|
-
function
|
|
13127
|
-
|
|
13128
|
-
|
|
13129
|
-
|
|
13130
|
-
|
|
13131
|
-
return result;
|
|
13132
|
-
|
|
13133
|
-
|
|
13134
|
-
|
|
13135
|
-
|
|
13136
|
-
|
|
13137
|
-
|
|
13042
|
+
function materializeValue(definition, supplied) {
|
|
13043
|
+
if (Array.isArray(definition)) {
|
|
13044
|
+
if (!Array.isArray(supplied) || supplied.length !== definition.length)
|
|
13045
|
+
return void 0;
|
|
13046
|
+
const result = definition.map((entry, index) => materializeValue(entry, supplied[index]));
|
|
13047
|
+
return result.some((entry) => entry === void 0) ? void 0 : result;
|
|
13048
|
+
}
|
|
13049
|
+
if (definition && typeof definition === "object") {
|
|
13050
|
+
const keys = Object.keys(definition);
|
|
13051
|
+
if (keys.length === 1 && keys[0] === "path" && typeof definition.path === "string")
|
|
13052
|
+
return supplied;
|
|
13053
|
+
const suppliedObject = cloneJsonObject(supplied);
|
|
13054
|
+
if (!suppliedObject)
|
|
13055
|
+
return void 0;
|
|
13056
|
+
const result = {};
|
|
13057
|
+
for (const [key, entry] of Object.entries(definition)) {
|
|
13058
|
+
const value = materializeValue(entry, suppliedObject[key]);
|
|
13059
|
+
if (value === void 0)
|
|
13060
|
+
return void 0;
|
|
13061
|
+
result[key] = value;
|
|
13138
13062
|
}
|
|
13063
|
+
return result;
|
|
13139
13064
|
}
|
|
13140
|
-
return
|
|
13141
|
-
}
|
|
13142
|
-
function schemaProperties(schema) {
|
|
13143
|
-
return schema.type === "object" && isJsonObject(schema.properties) ? schema.properties : void 0;
|
|
13065
|
+
return definition;
|
|
13144
13066
|
}
|
|
13145
|
-
function
|
|
13146
|
-
|
|
13147
|
-
|
|
13148
|
-
return source === target || source === "integer" && target === "number";
|
|
13067
|
+
function createRegistry(protocol, provider, debugLog) {
|
|
13068
|
+
const resources = protocol.agent.resources.filter((resource) => resource.resourceType === EResourceType.MCP || resource.resourceType === EResourceType.MCP_TOOL);
|
|
13069
|
+
return new McpProxyTool(provider, aggregateMcpServers(resources, protocol.agentId), { debugLog });
|
|
13149
13070
|
}
|
|
13150
|
-
function
|
|
13151
|
-
|
|
13071
|
+
function readStructuredContent3(result) {
|
|
13072
|
+
const object3 = cloneJsonObject(result);
|
|
13073
|
+
if (!object3)
|
|
13152
13074
|
return void 0;
|
|
13153
|
-
if (
|
|
13154
|
-
return
|
|
13155
|
-
|
|
13156
|
-
|
|
13075
|
+
if (cloneJsonObject(object3.structuredContent))
|
|
13076
|
+
return object3.structuredContent;
|
|
13077
|
+
if (!Array.isArray(object3.content) || object3.content.length !== 1)
|
|
13078
|
+
return void 0;
|
|
13079
|
+
const content = cloneJsonObject(object3.content[0]);
|
|
13080
|
+
if (!content || content.type !== "text" || typeof content.text !== "string")
|
|
13157
13081
|
return void 0;
|
|
13158
|
-
}
|
|
13159
|
-
const texts = [];
|
|
13160
|
-
for (const content of result.content) {
|
|
13161
|
-
if (!isJsonObject(content) || content.type !== "text" || typeof content.text !== "string")
|
|
13162
|
-
return void 0;
|
|
13163
|
-
texts.push(content.text);
|
|
13164
|
-
}
|
|
13165
|
-
if (texts.length > 1)
|
|
13166
|
-
return texts.join("\n");
|
|
13167
13082
|
try {
|
|
13168
|
-
|
|
13169
|
-
return isJsonValue(parsed) ? cloneJsonValue(parsed) : texts[0];
|
|
13083
|
+
return JSON.parse(content.text);
|
|
13170
13084
|
} catch {
|
|
13171
|
-
return
|
|
13085
|
+
return void 0;
|
|
13172
13086
|
}
|
|
13173
13087
|
}
|
|
13174
|
-
function
|
|
13175
|
-
|
|
13176
|
-
return { ...FALLBACK_OBJECT_SCHEMA };
|
|
13177
|
-
}
|
|
13178
|
-
if (value.type === "object" && (!isJsonObject(value.properties) || Object.keys(value.properties).length === 0))
|
|
13179
|
-
return { ...FALLBACK_OBJECT_SCHEMA };
|
|
13180
|
-
return JSON.parse(JSON.stringify(value));
|
|
13088
|
+
function failure2(code, message, retryable) {
|
|
13089
|
+
return { ok: false, error: { code, message, retryable } };
|
|
13181
13090
|
}
|
|
13182
|
-
|
|
13183
|
-
|
|
13184
|
-
retryable = false;
|
|
13185
|
-
constructor(details) {
|
|
13186
|
-
super(JSON.stringify(details));
|
|
13187
|
-
this.name = "CardMappingError";
|
|
13188
|
-
this.details = details;
|
|
13189
|
-
}
|
|
13190
|
-
};
|
|
13191
|
-
function isJsonObject(value) {
|
|
13192
|
-
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
13091
|
+
function errorMessage(error, fallback) {
|
|
13092
|
+
return error instanceof Error && error.message.trim() ? error.message : fallback;
|
|
13193
13093
|
}
|
|
13194
13094
|
|
|
13195
|
-
// ../linkque-agent-runtime/dist/esm/core/
|
|
13196
|
-
async function
|
|
13197
|
-
|
|
13198
|
-
|
|
13199
|
-
|
|
13200
|
-
|
|
13201
|
-
return failure2("INVALID_INPUT", error instanceof Error ? error.message : "\u9996\u9875\u5361\u7247\u8BF7\u6C42\u65E0\u6548", false);
|
|
13202
|
-
}
|
|
13203
|
-
if (request.agentId !== input.protocol.agentId) {
|
|
13204
|
-
return failure2("AGENT_MISMATCH", "\u9996\u9875\u6570\u636E\u8BF7\u6C42\u4E0E\u5F53\u524D Agent \u4E0D\u5339\u914D", false);
|
|
13205
|
-
}
|
|
13206
|
-
if (!input.userId.trim()) {
|
|
13207
|
-
return failure2("AUTH_REQUIRED", "\u9996\u9875\u6570\u636E\u8BF7\u6C42\u7F3A\u5C11\u53EF\u4FE1\u7528\u6237\u8EAB\u4EFD", false);
|
|
13208
|
-
}
|
|
13095
|
+
// ../linkque-agent-runtime/dist/esm/core/mcpExecution.js
|
|
13096
|
+
async function executeAuthorizedMcp(input) {
|
|
13097
|
+
if (input.agentId !== input.protocol.agentId)
|
|
13098
|
+
return failure3("AGENT_MISMATCH", "\u8BF7\u6C42\u4E0E\u5F53\u524D Agent \u4E0D\u5339\u914D", false);
|
|
13099
|
+
if (!input.userId.trim())
|
|
13100
|
+
return failure3("AUTH_REQUIRED", "\u7F3A\u5C11\u53EF\u4FE1\u7528\u6237\u8EAB\u4EFD", false);
|
|
13209
13101
|
const resources = input.protocol.agent.resources.filter((resource) => resource.resourceType === EResourceType.MCP || resource.resourceType === EResourceType.MCP_TOOL);
|
|
13210
|
-
const
|
|
13211
|
-
const spec = specs.find((candidate) => candidate.serverName === request.binding.mcpName);
|
|
13212
|
-
if (!spec) {
|
|
13213
|
-
return failure2("MCP_NOT_BOUND", "\u5F53\u524D Agent \u672A\u7ED1\u5B9A\u8BE5 MCP", false);
|
|
13214
|
-
}
|
|
13215
|
-
if (spec.allowedTools && !spec.allowedTools.has(request.binding.toolName)) {
|
|
13216
|
-
return failure2("TOOL_NOT_ALLOWED", "\u5F53\u524D Agent \u672A\u5141\u8BB8\u8C03\u7528\u8BE5 MCP \u5DE5\u5177", false);
|
|
13217
|
-
}
|
|
13218
|
-
const registry = new McpProxyTool(input.mcpServerProvider, specs);
|
|
13102
|
+
const registry = new McpProxyTool(input.mcpServerProvider, aggregateMcpServers(resources, input.protocol.agentId));
|
|
13219
13103
|
try {
|
|
13220
|
-
const
|
|
13221
|
-
if (!
|
|
13222
|
-
return
|
|
13223
|
-
|
|
13224
|
-
|
|
13225
|
-
|
|
13226
|
-
let value;
|
|
13227
|
-
switch (mapping.source.kind) {
|
|
13228
|
-
case "parameter":
|
|
13229
|
-
value = readJsonPointer(request.parameters, mapping.source.pointer);
|
|
13230
|
-
break;
|
|
13231
|
-
case "runtime-context":
|
|
13232
|
-
value = mapping.source.key === "agentId" ? input.protocol.agentId : input.userId;
|
|
13233
|
-
break;
|
|
13234
|
-
case "literal":
|
|
13235
|
-
value = mapping.source.value;
|
|
13236
|
-
break;
|
|
13237
|
-
}
|
|
13238
|
-
if (value === void 0 || !writeJsonPointer(toolInput, mapping.target, value)) {
|
|
13239
|
-
return failure2("INVALID_INPUT", `\u65E0\u6CD5\u751F\u6210 MCP \u5165\u53C2 ${mapping.target}`, false);
|
|
13240
|
-
}
|
|
13104
|
+
const tool = await registry.resolve(input.tool.mcpResourceId, input.tool.toolName, input.signal);
|
|
13105
|
+
if (!tool)
|
|
13106
|
+
return failure3("TOOL_NOT_ALLOWED", "MCP Tool \u4E0D\u5B58\u5728\u6216\u672A\u6388\u6743", false);
|
|
13107
|
+
const schema = cloneJsonObject(tool.inputSchema);
|
|
13108
|
+
if (schema && validateJsonSchemaValue(schema, input.tool.input).length > 0) {
|
|
13109
|
+
return failure3("INVALID_INPUT", "MCP \u5165\u53C2\u4E0D\u7B26\u5408 Tool Schema", false);
|
|
13241
13110
|
}
|
|
13242
13111
|
let result;
|
|
13243
13112
|
try {
|
|
13244
|
-
result = await
|
|
13245
|
-
} catch
|
|
13246
|
-
return
|
|
13247
|
-
}
|
|
13248
|
-
const structured = readStructuredContent3(result);
|
|
13249
|
-
let cardData = structured;
|
|
13250
|
-
if (request.binding.transform) {
|
|
13251
|
-
try {
|
|
13252
|
-
cardData = executeStructuralTransform(request.binding.transform, structured);
|
|
13253
|
-
} catch (error) {
|
|
13254
|
-
return failure2("TRANSFORM_FAILED", error instanceof Error ? error.message : "\u9996\u9875\u5361\u7247\u6570\u636E\u8F6C\u6362\u5931\u8D25", false);
|
|
13255
|
-
}
|
|
13113
|
+
result = await tool.execute(input.tool.input, input.signal);
|
|
13114
|
+
} catch {
|
|
13115
|
+
return failure3("EXECUTION_UNCERTAIN", "MCP \u6267\u884C\u7ED3\u679C\u4E0D\u786E\u5B9A\uFF0C\u8BF7\u52FF\u81EA\u52A8\u91CD\u8BD5", false);
|
|
13256
13116
|
}
|
|
13257
|
-
const
|
|
13258
|
-
if (!
|
|
13259
|
-
return
|
|
13117
|
+
const structured = readStructuredContent4(result);
|
|
13118
|
+
if (!isJsonValue2(structured)) {
|
|
13119
|
+
return failure3("INVALID_OUTPUT", "MCP \u672A\u8FD4\u56DE\u6709\u6548 JSON \u7ED3\u679C", false);
|
|
13260
13120
|
}
|
|
13261
|
-
return { ok: true,
|
|
13121
|
+
return { ok: true, callId: input.callId, result: structured };
|
|
13262
13122
|
} finally {
|
|
13263
13123
|
await registry.closeAll();
|
|
13264
13124
|
}
|
|
13265
13125
|
}
|
|
13266
|
-
function
|
|
13126
|
+
function readStructuredContent4(result) {
|
|
13267
13127
|
const object3 = cloneJsonObject(result);
|
|
13268
13128
|
if (!object3)
|
|
13269
13129
|
return void 0;
|
|
13270
|
-
if (
|
|
13130
|
+
if (object3.structuredContent !== void 0)
|
|
13271
13131
|
return object3.structuredContent;
|
|
13272
|
-
|
|
13273
|
-
if (!Array.isArray(object3.content) || object3.content.length !== 1) {
|
|
13132
|
+
if (!Array.isArray(object3.content) || object3.content.length !== 1)
|
|
13274
13133
|
return void 0;
|
|
13275
|
-
|
|
13276
|
-
|
|
13277
|
-
if (!content || content.type !== "text" || typeof content.text !== "string") {
|
|
13134
|
+
const text = cloneJsonObject(object3.content[0]);
|
|
13135
|
+
if (text?.type !== "text" || typeof text.text !== "string")
|
|
13278
13136
|
return void 0;
|
|
13279
|
-
}
|
|
13280
13137
|
try {
|
|
13281
|
-
return JSON.parse(
|
|
13138
|
+
return JSON.parse(text.text);
|
|
13282
13139
|
} catch {
|
|
13283
13140
|
return void 0;
|
|
13284
13141
|
}
|
|
13285
13142
|
}
|
|
13286
|
-
function
|
|
13143
|
+
function isJsonValue2(value) {
|
|
13144
|
+
if (value === null || ["string", "boolean"].includes(typeof value))
|
|
13145
|
+
return true;
|
|
13146
|
+
if (typeof value === "number")
|
|
13147
|
+
return Number.isFinite(value);
|
|
13148
|
+
if (Array.isArray(value))
|
|
13149
|
+
return value.every(isJsonValue2);
|
|
13150
|
+
const object3 = cloneJsonObject(value);
|
|
13151
|
+
return Boolean(object3) && Object.values(object3 ?? {}).every(isJsonValue2);
|
|
13152
|
+
}
|
|
13153
|
+
function failure3(code, message, retryable) {
|
|
13154
|
+
return { ok: false, error: { code, message, retryable } };
|
|
13155
|
+
}
|
|
13156
|
+
|
|
13157
|
+
// ../linkque-agent-runtime/dist/esm/core/functionActionExecutor.js
|
|
13158
|
+
async function executeFunctionActionMcp(input) {
|
|
13159
|
+
let request;
|
|
13160
|
+
try {
|
|
13161
|
+
request = parseA2UIFunctionMcpExecuteRequest(input.request);
|
|
13162
|
+
} catch (error) {
|
|
13163
|
+
return failure4("INVALID_INPUT", errorMessage2(error, "\u51FD\u6570 MCP \u8BF7\u6C42\u65E0\u6548"), false);
|
|
13164
|
+
}
|
|
13165
|
+
if (request.agentId !== input.protocol.agentId) {
|
|
13166
|
+
return failure4("AGENT_MISMATCH", "\u51FD\u6570\u52A8\u4F5C\u4E0E\u5F53\u524D Agent \u4E0D\u5339\u914D", false);
|
|
13167
|
+
}
|
|
13168
|
+
if (request.threadId !== input.runtimeContext.threadId || !input.runtimeContext.userId.trim()) {
|
|
13169
|
+
return failure4("AUTH_REQUIRED", "\u51FD\u6570\u52A8\u4F5C\u7F3A\u5C11\u53EF\u4FE1\u4F1A\u8BDD\u8EAB\u4EFD", false);
|
|
13170
|
+
}
|
|
13171
|
+
return executeAuthorizedMcp({
|
|
13172
|
+
protocol: input.protocol,
|
|
13173
|
+
agentId: request.agentId,
|
|
13174
|
+
userId: input.runtimeContext.userId,
|
|
13175
|
+
callId: request.callId,
|
|
13176
|
+
tool: request.tool,
|
|
13177
|
+
mcpServerProvider: input.mcpServerProvider,
|
|
13178
|
+
signal: input.signal
|
|
13179
|
+
});
|
|
13180
|
+
}
|
|
13181
|
+
function failure4(code, message, retryable) {
|
|
13287
13182
|
return { ok: false, error: { code, message, retryable } };
|
|
13288
13183
|
}
|
|
13184
|
+
function errorMessage2(error, fallback) {
|
|
13185
|
+
return error instanceof Error && error.message.trim() ? error.message : fallback;
|
|
13186
|
+
}
|
|
13289
13187
|
|
|
13290
13188
|
// ../linkque-agent-runtime/dist/esm/core/inMemorySessionManager.js
|
|
13291
13189
|
var InMemorySessionManager = class {
|
|
@@ -15140,9 +15038,9 @@ function createWebSearchTool(resource, searchPort, agentId, debugLog) {
|
|
|
15140
15038
|
outcome = "cancelled";
|
|
15141
15039
|
throw new DOMException("\u5168\u7F51\u641C\u7D22\u5DF2\u53D6\u6D88", "AbortError");
|
|
15142
15040
|
}
|
|
15143
|
-
const
|
|
15144
|
-
outcome =
|
|
15145
|
-
return { query, items: [], error:
|
|
15041
|
+
const failure5 = toWebSearchFailure(error);
|
|
15042
|
+
outcome = failure5.code;
|
|
15043
|
+
return { query, items: [], error: failure5 };
|
|
15146
15044
|
} finally {
|
|
15147
15045
|
try {
|
|
15148
15046
|
debugLog?.(JSON.stringify({
|
|
@@ -18202,8 +18100,8 @@ var Protocol = class {
|
|
|
18202
18100
|
if (queuedMessage.type === "response") {
|
|
18203
18101
|
resolver(message);
|
|
18204
18102
|
} else {
|
|
18205
|
-
const
|
|
18206
|
-
const error = new McpError(
|
|
18103
|
+
const errorMessage3 = message;
|
|
18104
|
+
const error = new McpError(errorMessage3.error.code, errorMessage3.error.message, errorMessage3.error.data);
|
|
18207
18105
|
resolver(error);
|
|
18208
18106
|
}
|
|
18209
18107
|
} else {
|
|
@@ -19489,8 +19387,8 @@ var Client = class extends Protocol {
|
|
|
19489
19387
|
const wrappedHandler = async (request, extra) => {
|
|
19490
19388
|
const validatedRequest = safeParse2(ElicitRequestSchema, request);
|
|
19491
19389
|
if (!validatedRequest.success) {
|
|
19492
|
-
const
|
|
19493
|
-
throw new McpError(ErrorCode.InvalidParams, `Invalid elicitation request: ${
|
|
19390
|
+
const errorMessage3 = validatedRequest.error instanceof Error ? validatedRequest.error.message : String(validatedRequest.error);
|
|
19391
|
+
throw new McpError(ErrorCode.InvalidParams, `Invalid elicitation request: ${errorMessage3}`);
|
|
19494
19392
|
}
|
|
19495
19393
|
const { params } = validatedRequest.data;
|
|
19496
19394
|
params.mode = params.mode ?? "form";
|
|
@@ -19505,15 +19403,15 @@ var Client = class extends Protocol {
|
|
|
19505
19403
|
if (params.task) {
|
|
19506
19404
|
const taskValidationResult = safeParse2(CreateTaskResultSchema, result);
|
|
19507
19405
|
if (!taskValidationResult.success) {
|
|
19508
|
-
const
|
|
19509
|
-
throw new McpError(ErrorCode.InvalidParams, `Invalid task creation result: ${
|
|
19406
|
+
const errorMessage3 = taskValidationResult.error instanceof Error ? taskValidationResult.error.message : String(taskValidationResult.error);
|
|
19407
|
+
throw new McpError(ErrorCode.InvalidParams, `Invalid task creation result: ${errorMessage3}`);
|
|
19510
19408
|
}
|
|
19511
19409
|
return taskValidationResult.data;
|
|
19512
19410
|
}
|
|
19513
19411
|
const validationResult = safeParse2(ElicitResultSchema, result);
|
|
19514
19412
|
if (!validationResult.success) {
|
|
19515
|
-
const
|
|
19516
|
-
throw new McpError(ErrorCode.InvalidParams, `Invalid elicitation result: ${
|
|
19413
|
+
const errorMessage3 = validationResult.error instanceof Error ? validationResult.error.message : String(validationResult.error);
|
|
19414
|
+
throw new McpError(ErrorCode.InvalidParams, `Invalid elicitation result: ${errorMessage3}`);
|
|
19517
19415
|
}
|
|
19518
19416
|
const validatedResult = validationResult.data;
|
|
19519
19417
|
const requestedSchema = params.mode === "form" ? params.requestedSchema : void 0;
|
|
@@ -19533,16 +19431,16 @@ var Client = class extends Protocol {
|
|
|
19533
19431
|
const wrappedHandler = async (request, extra) => {
|
|
19534
19432
|
const validatedRequest = safeParse2(CreateMessageRequestSchema, request);
|
|
19535
19433
|
if (!validatedRequest.success) {
|
|
19536
|
-
const
|
|
19537
|
-
throw new McpError(ErrorCode.InvalidParams, `Invalid sampling request: ${
|
|
19434
|
+
const errorMessage3 = validatedRequest.error instanceof Error ? validatedRequest.error.message : String(validatedRequest.error);
|
|
19435
|
+
throw new McpError(ErrorCode.InvalidParams, `Invalid sampling request: ${errorMessage3}`);
|
|
19538
19436
|
}
|
|
19539
19437
|
const { params } = validatedRequest.data;
|
|
19540
19438
|
const result = await Promise.resolve(handler(request, extra));
|
|
19541
19439
|
if (params.task) {
|
|
19542
19440
|
const taskValidationResult = safeParse2(CreateTaskResultSchema, result);
|
|
19543
19441
|
if (!taskValidationResult.success) {
|
|
19544
|
-
const
|
|
19545
|
-
throw new McpError(ErrorCode.InvalidParams, `Invalid task creation result: ${
|
|
19442
|
+
const errorMessage3 = taskValidationResult.error instanceof Error ? taskValidationResult.error.message : String(taskValidationResult.error);
|
|
19443
|
+
throw new McpError(ErrorCode.InvalidParams, `Invalid task creation result: ${errorMessage3}`);
|
|
19546
19444
|
}
|
|
19547
19445
|
return taskValidationResult.data;
|
|
19548
19446
|
}
|
|
@@ -19550,8 +19448,8 @@ var Client = class extends Protocol {
|
|
|
19550
19448
|
const resultSchema = hasTools ? CreateMessageResultWithToolsSchema : CreateMessageResultSchema;
|
|
19551
19449
|
const validationResult = safeParse2(resultSchema, result);
|
|
19552
19450
|
if (!validationResult.success) {
|
|
19553
|
-
const
|
|
19554
|
-
throw new McpError(ErrorCode.InvalidParams, `Invalid sampling result: ${
|
|
19451
|
+
const errorMessage3 = validationResult.error instanceof Error ? validationResult.error.message : String(validationResult.error);
|
|
19452
|
+
throw new McpError(ErrorCode.InvalidParams, `Invalid sampling result: ${errorMessage3}`);
|
|
19555
19453
|
}
|
|
19556
19454
|
return validationResult.data;
|
|
19557
19455
|
};
|
|
@@ -20884,8 +20782,8 @@ async function parseErrorResponse(input) {
|
|
|
20884
20782
|
const errorClass = OAUTH_ERRORS[error] || ServerError;
|
|
20885
20783
|
return new errorClass(error_description || "", error_uri);
|
|
20886
20784
|
} catch (error) {
|
|
20887
|
-
const
|
|
20888
|
-
return new ServerError(
|
|
20785
|
+
const errorMessage3 = `${statusCode ? `HTTP ${statusCode}: ` : ""}Invalid OAuth error response: ${error}. Raw body: ${body}`;
|
|
20786
|
+
return new ServerError(errorMessage3);
|
|
20889
20787
|
}
|
|
20890
20788
|
}
|
|
20891
20789
|
async function auth(provider, options) {
|
|
@@ -21985,9 +21883,9 @@ var LinkqueWebSearchPort = class {
|
|
|
21985
21883
|
outcome = "cancelled";
|
|
21986
21884
|
throw new DOMException("\u5168\u7F51\u641C\u7D22\u5DF2\u53D6\u6D88", "AbortError");
|
|
21987
21885
|
}
|
|
21988
|
-
const
|
|
21989
|
-
outcome =
|
|
21990
|
-
throw new WebSearchError(
|
|
21886
|
+
const failure5 = toWebSearchFailure(requestSignal.aborted ? requestSignal.reason : error);
|
|
21887
|
+
outcome = failure5.code;
|
|
21888
|
+
throw new WebSearchError(failure5.code);
|
|
21991
21889
|
} finally {
|
|
21992
21890
|
clearTimeout(timer);
|
|
21993
21891
|
signal?.removeEventListener("abort", onAbort);
|
|
@@ -23220,10 +23118,9 @@ export {
|
|
|
23220
23118
|
isJsonValue,
|
|
23221
23119
|
isSafeJsonPointer,
|
|
23222
23120
|
readJsonPointer,
|
|
23223
|
-
A2UI_PAYMENT_RESULT_EVENT_NAME,
|
|
23224
|
-
parseA2UIActionExecuteRequest,
|
|
23225
|
-
parseA2UIPaymentResultEventPayload,
|
|
23226
23121
|
executeA2UICardResolver,
|
|
23122
|
+
parseA2UIActionExecuteRequest,
|
|
23123
|
+
parseA2UIFunctionMcpExecuteRequest,
|
|
23227
23124
|
stableToolName,
|
|
23228
23125
|
createCardPresentationToolName,
|
|
23229
23126
|
executeCardPresentation,
|
|
@@ -23232,7 +23129,7 @@ export {
|
|
|
23232
23129
|
parseCardConfig,
|
|
23233
23130
|
isAgentPresentationToolResult,
|
|
23234
23131
|
createPresentationDescription,
|
|
23235
|
-
|
|
23132
|
+
readStructuredContent,
|
|
23236
23133
|
normalizeMcpInputSchema,
|
|
23237
23134
|
CardMappingError,
|
|
23238
23135
|
aggregateMcpServers,
|
|
@@ -23240,7 +23137,7 @@ export {
|
|
|
23240
23137
|
executeHomeData,
|
|
23241
23138
|
CardActionDiagnostics,
|
|
23242
23139
|
executeCardAction,
|
|
23243
|
-
|
|
23140
|
+
executeFunctionActionMcp,
|
|
23244
23141
|
FOLLOW_UP_CONTEXT_MAX_HISTORY_RUNS,
|
|
23245
23142
|
prepareFollowUpQuestionContext,
|
|
23246
23143
|
normalizeFollowUpQuestionStrings,
|