@tbox.cn/app-contracts 0.1.3 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cards.d.ts +171 -4
- package/dist/context-slot.d.ts +23 -0
- package/dist/host-effect.d.ts +66 -0
- package/dist/identity.d.ts +15 -4
- package/dist/index.d.ts +9 -0
- package/dist/integrations-config.d.ts +3 -0
- package/dist/memory-tool.d.ts +64 -0
- package/dist/memory.d.ts +94 -0
- package/dist/no-inline-secrets.d.ts +1 -0
- package/dist/pre-tool.d.ts +56 -0
- package/dist/protocol.d.ts +90 -3
- package/dist/providers.d.ts +368 -0
- package/dist/runtime.d.ts +12 -2
- package/dist/runtime.js +509 -3
- package/dist/skill.d.ts +49 -0
- package/package.json +14 -10
- package/tests/__snapshots__/integrations-config-parity.test.ts.snap +354 -0
- package/tests/integrations-config-parity.test.ts +81 -0
- package/tests/integrations-config.test.ts +274 -0
package/dist/runtime.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
// src/runtime.ts
|
|
2
|
-
import { EventType } from "@ag-ui/core";
|
|
2
|
+
import { EventType as EventType2 } from "@ag-ui/core";
|
|
3
3
|
|
|
4
4
|
// src/cards.ts
|
|
5
5
|
var TBOX_CARD_EVENT_NAME = "tbox:card";
|
|
6
|
+
var TBOX_CARD_UPDATE_EVENT_NAME = "tbox:card-update";
|
|
7
|
+
var TBOX_NOTICE_EVENT_NAME = "tbox:notice";
|
|
6
8
|
|
|
7
9
|
// src/identity.ts
|
|
8
10
|
var AUTH_ERROR_CODES = {
|
|
@@ -35,9 +37,513 @@ function authCodeToStatus(code) {
|
|
|
35
37
|
return 401;
|
|
36
38
|
}
|
|
37
39
|
}
|
|
40
|
+
|
|
41
|
+
// src/providers.ts
|
|
42
|
+
function stableSerializeScope(scope) {
|
|
43
|
+
if (!scope) return "";
|
|
44
|
+
const keys = Object.keys(scope).sort();
|
|
45
|
+
const parts = keys.map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(String(scope[k]))}`);
|
|
46
|
+
return parts.join("&");
|
|
47
|
+
}
|
|
48
|
+
var PROVIDER_ERROR_CODES = {
|
|
49
|
+
/** 实例被禁用(enabled=false) */
|
|
50
|
+
INSTANCE_DISABLED: "PROVIDER_INSTANCE_DISABLED",
|
|
51
|
+
/** 无匹配实例(templateId + scope / service 槽无 instanceId 实例) */
|
|
52
|
+
INSTANCE_NOT_FOUND: "PROVIDER_INSTANCE_NOT_FOUND",
|
|
53
|
+
/** 凭证引用缺失(生产 fail-fast) */
|
|
54
|
+
CREDENTIAL_REF_MISSING: "PROVIDER_CREDENTIAL_REF_MISSING",
|
|
55
|
+
/** 凭证 scope 与实例 scope 不匹配 */
|
|
56
|
+
CREDENTIAL_SCOPE_MISMATCH: "PROVIDER_CREDENTIAL_SCOPE_MISMATCH",
|
|
57
|
+
/** 熔断开闸 */
|
|
58
|
+
CIRCUIT_OPEN: "PROVIDER_CIRCUIT_OPEN",
|
|
59
|
+
/** 响应超限(1MB) */
|
|
60
|
+
RESPONSE_TOO_LARGE: "PROVIDER_RESPONSE_TOO_LARGE",
|
|
61
|
+
/** 传输失败归类(429/5xx/NETWORK/TIMEOUT) */
|
|
62
|
+
TRANSPORT_FAILURE: "PROVIDER_TRANSPORT_FAILURE",
|
|
63
|
+
/** 服务槽未配置(integrations.services 无该 service) */
|
|
64
|
+
SERVICE_NOT_CONFIGURED: "PROVIDER_SERVICE_NOT_CONFIGURED",
|
|
65
|
+
/** provider 标识未命中 catalog(解引用失败) */
|
|
66
|
+
PROVIDER_NOT_FOUND: "PROVIDER_NOT_FOUND",
|
|
67
|
+
/** 内联声明未实施态(无 adapter 实现注册) */
|
|
68
|
+
PROVIDER_NOT_IMPLEMENTED: "PROVIDER_NOT_IMPLEMENTED",
|
|
69
|
+
/** 绑定未解析(绑定组无任何层 provider 声明,或 implementation 派生零命中/歧义/未注入查表——v3 起) */
|
|
70
|
+
BINDING_UNRESOLVED: "PROVIDER_BINDING_UNRESOLVED"
|
|
71
|
+
};
|
|
72
|
+
function isExpectedProviderAbsence(e) {
|
|
73
|
+
const code = e?.code;
|
|
74
|
+
return code === PROVIDER_ERROR_CODES.SERVICE_NOT_CONFIGURED || code === PROVIDER_ERROR_CODES.INSTANCE_NOT_FOUND || code === PROVIDER_ERROR_CODES.INSTANCE_DISABLED;
|
|
75
|
+
}
|
|
76
|
+
var WILDCARD_INSTANCE_KEY = "*";
|
|
77
|
+
function moduleDomainOf(service) {
|
|
78
|
+
const i = service.indexOf(".");
|
|
79
|
+
return i === -1 ? service : service.slice(0, i);
|
|
80
|
+
}
|
|
81
|
+
function unresolved(service, instanceId, detail) {
|
|
82
|
+
return { status: "binding-unresolved", message: `${service}@${instanceId}\uFF1A${detail}` };
|
|
83
|
+
}
|
|
84
|
+
function resolveEffectiveService(integrations, service, instanceId, supplies) {
|
|
85
|
+
const slot = integrations.services[service];
|
|
86
|
+
if (!slot) return { status: "service-not-configured" };
|
|
87
|
+
const instances = slot.instances ?? { [WILDCARD_INSTANCE_KEY]: {} };
|
|
88
|
+
const inst = instances[instanceId] ?? instances[WILDCARD_INSTANCE_KEY];
|
|
89
|
+
if (!inst) return { status: "instance-not-found" };
|
|
90
|
+
if (inst.enabled === false) return { status: "instance-disabled" };
|
|
91
|
+
const domain = integrations.domains?.[moduleDomainOf(service)];
|
|
92
|
+
const head = inst.provider != null ? { src: "instance", provider: inst.provider, impl: inst.implementation } : slot.provider != null ? { src: "slot", provider: slot.provider, impl: slot.implementation } : domain?.provider != null ? { src: "domain", provider: domain.provider, impl: void 0 } : integrations.provider != null ? { src: "root", provider: integrations.provider, impl: void 0 } : void 0;
|
|
93
|
+
if (!head) {
|
|
94
|
+
return unresolved(
|
|
95
|
+
service,
|
|
96
|
+
instanceId,
|
|
97
|
+
"\u65E0\u4EFB\u4F55\u5C42 provider \u58F0\u660E\uFF08root/domains/\u69FD\u7EA7/\u5B9E\u4F8B\u7EA7\uFF09\u2014\u2014\u914D\u7F6E\u7ED1\u5B9A\u6216 provider use \u8865\u58F0\u660E"
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
let implementation = head.impl;
|
|
101
|
+
if (implementation == null) {
|
|
102
|
+
if (typeof head.provider !== "string") {
|
|
103
|
+
return unresolved(service, instanceId, "\u5185\u8054\u5382\u5546\u58F0\u660E\u5FC5\u987B\u663E\u5F0F implementation\uFF08slot \u7EA7\uFF09");
|
|
104
|
+
}
|
|
105
|
+
if (!supplies) {
|
|
106
|
+
return unresolved(
|
|
107
|
+
service,
|
|
108
|
+
instanceId,
|
|
109
|
+
`\u7EE7\u627F\u7ED1\u5B9A\u9700 supplies \u67E5\u8868\uFF08${head.src} \u5C42 provider-only\uFF1B\u8C03\u7528\u65B9\u672A\u6CE8\u5165 supply lookup\uFF09`
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
const candidates = supplies[`${head.provider}\0${service}`];
|
|
113
|
+
if (!candidates?.length) {
|
|
114
|
+
return unresolved(
|
|
115
|
+
service,
|
|
116
|
+
instanceId,
|
|
117
|
+
`provider ${head.provider} \u672A\u4F9B\u7ED9\u670D\u52A1 ${service}\uFF08${head.src} \u5C42\u58F0\u660E\u2014\u2014\u88C5\u5BF9\u5E94 provider \u5305\u6216\u6539\u7ED1\uFF09`
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
if (candidates.length > 1) {
|
|
121
|
+
return unresolved(
|
|
122
|
+
service,
|
|
123
|
+
instanceId,
|
|
124
|
+
`provider ${head.provider} \u5BF9 ${service} \u5B58\u5728\u591A\u5B9E\u73B0\uFF08${candidates.join("\u3001")}\uFF09\u2014\u2014\u663E\u5F0F\u6307\u5B9A implementation`
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
implementation = candidates[0];
|
|
128
|
+
}
|
|
129
|
+
const config = {
|
|
130
|
+
...integrations.config,
|
|
131
|
+
...integrations.configByInstance?.[instanceId] ?? {},
|
|
132
|
+
...domain?.config ?? {},
|
|
133
|
+
...slot.config,
|
|
134
|
+
...inst.config
|
|
135
|
+
};
|
|
136
|
+
const credentialRef = inst.credentialRef ?? slot.credentialRef ?? integrations.credentialRefByInstance?.[instanceId] ?? integrations.credentialRef;
|
|
137
|
+
return {
|
|
138
|
+
status: "ok",
|
|
139
|
+
effective: {
|
|
140
|
+
provider: head.provider,
|
|
141
|
+
implementation,
|
|
142
|
+
config,
|
|
143
|
+
...credentialRef !== void 0 ? { credentialRef } : {}
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function normalizeInstances(config) {
|
|
148
|
+
const instances = config.instances ?? [];
|
|
149
|
+
const explicit = config.defaultInstanceId;
|
|
150
|
+
const defaultInstanceId = explicit !== void 0 ? explicit : instances.length === 1 ? instances[0].id : null;
|
|
151
|
+
return { instances, defaultInstanceId };
|
|
152
|
+
}
|
|
153
|
+
function buildSupplyLookup(catalog) {
|
|
154
|
+
const lookup = {};
|
|
155
|
+
for (const [vendor, impls] of Object.entries(catalog.providers)) {
|
|
156
|
+
for (const [impl, entry] of Object.entries(impls)) {
|
|
157
|
+
for (const svc of entry.services) {
|
|
158
|
+
(lookup[`${vendor}\0${svc}`] ??= []).push(impl);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return lookup;
|
|
163
|
+
}
|
|
164
|
+
var RESOURCE_TYPE_IDS = ["knowledge"];
|
|
165
|
+
|
|
166
|
+
// src/integrations-config.ts
|
|
167
|
+
import { z } from "zod";
|
|
168
|
+
|
|
169
|
+
// src/no-inline-secrets.ts
|
|
170
|
+
var SECRET_KEY_PATTERN = /api[-_]?key|private[-_]?key|secret|password|token|credential|encrypt[-_]?key/i;
|
|
171
|
+
var SECRET_KEY_ALLOWLIST = /* @__PURE__ */ new Set(["credentialType", "credentialRef", "credentialVersion", "credentialRefByInstance"]);
|
|
172
|
+
function assertNoInlineSecrets(config, path = "$") {
|
|
173
|
+
if (Array.isArray(config)) {
|
|
174
|
+
config.forEach((item, i) => assertNoInlineSecrets(item, `${path}[${i}]`));
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (config && typeof config === "object") {
|
|
178
|
+
for (const [k, v] of Object.entries(config)) {
|
|
179
|
+
if (SECRET_KEY_PATTERN.test(k) && !SECRET_KEY_ALLOWLIST.has(k)) {
|
|
180
|
+
throw new Error(`\u90E8\u7F72\u914D\u7F6E\u542B\u7591\u4F3C\u5185\u8054\u5BC6\u94A5\u5B57\u6BB5\uFF1A${path}.${k}\uFF08\u673A\u5BC6\u53EA\u5141\u8BB8\u7ECF credentialRef \u5F15\u7528\u73AF\u5883\u53D8\u91CF\uFF09`);
|
|
181
|
+
}
|
|
182
|
+
assertNoInlineSecrets(v, `${path}.${k}`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// src/integrations-config.ts
|
|
188
|
+
var inlineVendorSchema = z.strictObject({
|
|
189
|
+
provider: z.string().min(1),
|
|
190
|
+
credentialType: z.string().min(1),
|
|
191
|
+
configSchema: z.string().optional()
|
|
192
|
+
});
|
|
193
|
+
var domainSchema = z.strictObject({
|
|
194
|
+
provider: z.string().min(1).optional(),
|
|
195
|
+
config: z.record(z.string(), z.unknown()).optional()
|
|
196
|
+
}).refine((d) => d.provider !== void 0 || d.config !== void 0, {
|
|
197
|
+
message: "\u57DF\u914D\u7F6E provider/config \u81F3\u5C11\u5176\u4E00"
|
|
198
|
+
});
|
|
199
|
+
var instanceSchema = z.strictObject({
|
|
200
|
+
provider: z.string().optional(),
|
|
201
|
+
implementation: z.string().optional(),
|
|
202
|
+
config: z.record(z.string(), z.unknown()).optional(),
|
|
203
|
+
credentialRef: z.string().optional(),
|
|
204
|
+
enabled: z.boolean().optional()
|
|
205
|
+
}).refine((d) => d.implementation === void 0 || d.provider !== void 0, {
|
|
206
|
+
message: "\u5B9E\u4F8B\u7EA7 implementation \u9700\u4F34\u968F provider\uFF08impl-only \u7981\u6B62\u2014\u2014provider \u662F\u7ED1\u5B9A\u7EC4\u5934\uFF09"
|
|
207
|
+
});
|
|
208
|
+
var serviceSchema = z.strictObject({
|
|
209
|
+
provider: z.union([z.string().min(1), inlineVendorSchema]).optional(),
|
|
210
|
+
implementation: z.string().min(1).optional(),
|
|
211
|
+
config: z.record(z.string(), z.unknown()).optional(),
|
|
212
|
+
credentialRef: z.string().optional(),
|
|
213
|
+
instances: z.record(z.string(), instanceSchema).optional()
|
|
214
|
+
}).refine((d) => d.implementation === void 0 || d.provider !== void 0, {
|
|
215
|
+
message: "\u69FD\u7EA7 implementation \u9700\u4F34\u968F provider\uFF08impl-only \u7981\u6B62\uFF09"
|
|
216
|
+
}).refine((d) => typeof d.provider !== "object" || typeof d.implementation === "string", {
|
|
217
|
+
message: "\u5185\u8054\u5382\u5546\u58F0\u660E\u5FC5\u987B\u663E\u5F0F implementation\uFF08\u5185\u8054\u65E0\u6D3E\u751F\u6E90\uFF09"
|
|
218
|
+
});
|
|
219
|
+
var deploymentInstanceSchema = z.strictObject({
|
|
220
|
+
id: z.string().min(1),
|
|
221
|
+
name: z.string().min(1),
|
|
222
|
+
attributes: z.record(z.string(), z.unknown()).optional()
|
|
223
|
+
});
|
|
224
|
+
var knowledgeSpecSchema = z.strictObject({
|
|
225
|
+
type: z.literal("knowledge"),
|
|
226
|
+
datasetId: z.string().min(1).optional(),
|
|
227
|
+
ref: z.string().min(1).optional(),
|
|
228
|
+
topK: z.number().int().positive().optional(),
|
|
229
|
+
scoreThreshold: z.number().optional()
|
|
230
|
+
});
|
|
231
|
+
var resourceSpecSchema = z.discriminatedUnion("type", [knowledgeSpecSchema]).refine((d) => d.datasetId !== void 0 || d.ref !== void 0, {
|
|
232
|
+
message: "knowledge \u8D44\u6E90 datasetId/ref \u81F3\u5C11\u5176\u4E00"
|
|
233
|
+
});
|
|
234
|
+
var moduleConfigSchema = z.strictObject({
|
|
235
|
+
resources: z.record(z.string(), resourceSpecSchema)
|
|
236
|
+
});
|
|
237
|
+
var integrationsSchema = z.strictObject({
|
|
238
|
+
provider: z.string().min(1).optional(),
|
|
239
|
+
config: z.record(z.string(), z.unknown()).optional(),
|
|
240
|
+
configByInstance: z.record(z.string(), z.record(z.string(), z.unknown())).optional(),
|
|
241
|
+
credentialRef: z.string().optional(),
|
|
242
|
+
credentialRefByInstance: z.record(z.string(), z.string()).optional(),
|
|
243
|
+
domains: z.record(z.string(), domainSchema).optional(),
|
|
244
|
+
instances: z.array(deploymentInstanceSchema).optional(),
|
|
245
|
+
defaultInstanceId: z.string().min(1).nullable().optional(),
|
|
246
|
+
modules: z.record(z.string(), moduleConfigSchema).optional(),
|
|
247
|
+
services: z.record(z.string(), serviceSchema)
|
|
248
|
+
}).superRefine((cfg, ctx) => {
|
|
249
|
+
if (!cfg.instances) return;
|
|
250
|
+
if (cfg.instances.length === 0) {
|
|
251
|
+
ctx.addIssue({
|
|
252
|
+
code: z.ZodIssueCode.custom,
|
|
253
|
+
message: "instances \u7A7A\u6570\u7EC4\u975E\u6CD5\u2014\u2014\u65E0\u53C2\u8003\u8868\u8BF7\u5220\u9664\u8BE5\u952E\uFF08\u952E\u7F3A\u5E2D\u662F\u552F\u4E00\u5199\u6CD5\uFF09",
|
|
254
|
+
path: ["instances"]
|
|
255
|
+
});
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
const seen = /* @__PURE__ */ new Set();
|
|
259
|
+
cfg.instances.forEach((inst, i) => {
|
|
260
|
+
if (inst.id === WILDCARD_INSTANCE_KEY) {
|
|
261
|
+
ctx.addIssue({
|
|
262
|
+
code: z.ZodIssueCode.custom,
|
|
263
|
+
message: `instances[${i}].id \u7981\u7528\u901A\u914D\u4FDD\u7559\u5B57 '*'`,
|
|
264
|
+
path: ["instances", i, "id"]
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
if (seen.has(inst.id)) {
|
|
268
|
+
ctx.addIssue({
|
|
269
|
+
code: z.ZodIssueCode.custom,
|
|
270
|
+
message: `instances[${i}].id \u91CD\u590D\uFF1A'${inst.id}'`,
|
|
271
|
+
path: ["instances", i, "id"]
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
seen.add(inst.id);
|
|
275
|
+
});
|
|
276
|
+
if (typeof cfg.defaultInstanceId === "string" && !seen.has(cfg.defaultInstanceId)) {
|
|
277
|
+
ctx.addIssue({
|
|
278
|
+
code: z.ZodIssueCode.custom,
|
|
279
|
+
message: `defaultInstanceId '${cfg.defaultInstanceId}' \u4E0D\u5728 instances ids \u5185`,
|
|
280
|
+
path: ["defaultInstanceId"]
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
var LEGACY_BY_SCOPE_KEYS = ["configByScope", "credentialRefByScope"];
|
|
285
|
+
function assertNoLegacyByScopeKeys(raw) {
|
|
286
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return;
|
|
287
|
+
const keys = Object.keys(raw);
|
|
288
|
+
const legacy = keys.filter((k) => LEGACY_BY_SCOPE_KEYS.includes(k));
|
|
289
|
+
if (legacy.length > 0) {
|
|
290
|
+
throw new Error(
|
|
291
|
+
`integrations.json \u542B 0.9.0 \u5DF2\u66F4\u540D\u5B57\u6BB5\uFF1A${legacy.join("\u3001")}\u2014\u2014\u8BF7\u66F4\u540D configByInstance / credentialRefByInstance\uFF08scope\u2192instance \u8BCD\u6C47\u76F4\u5207\uFF0C\u65E7\u952E\u4E0D\u518D\u63A5\u53D7\uFF09`
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
function parseIntegrationsConfig(raw) {
|
|
296
|
+
assertNoLegacyByScopeKeys(raw);
|
|
297
|
+
assertNoInlineSecrets(raw);
|
|
298
|
+
return integrationsSchema.parse(raw);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// src/context-slot.ts
|
|
302
|
+
function defineContextSlot(opts) {
|
|
303
|
+
return {
|
|
304
|
+
key: opts.key,
|
|
305
|
+
read(attributes) {
|
|
306
|
+
const raw = attributes?.[opts.key];
|
|
307
|
+
return raw === void 0 ? void 0 : opts.parse(raw);
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// src/skill.ts
|
|
313
|
+
var SKILL_NAME_SRC = "[a-z0-9-]{1,64}";
|
|
314
|
+
var SKILL_NAME_PATTERN = new RegExp(`^${SKILL_NAME_SRC}$`);
|
|
315
|
+
var SKILL_TOOL_CALL_NAME = "__tbox_skill";
|
|
316
|
+
var SKILL_REMINDER_OPEN_TAG_SRC = String.raw`<system-reminder\s+skill="(${SKILL_NAME_SRC})">`;
|
|
317
|
+
function buildSkillReminderOpenTagPattern() {
|
|
318
|
+
return new RegExp(SKILL_REMINDER_OPEN_TAG_SRC, "g");
|
|
319
|
+
}
|
|
320
|
+
function buildSkillReminderBlockPattern() {
|
|
321
|
+
return new RegExp(
|
|
322
|
+
String.raw`${SKILL_REMINDER_OPEN_TAG_SRC}[\s\S]*?<\/system-reminder>`,
|
|
323
|
+
"g"
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
function parseSkillReminders(text) {
|
|
327
|
+
const skills = [];
|
|
328
|
+
if (!text) return { skills, cleanedText: text };
|
|
329
|
+
const re = buildSkillReminderBlockPattern();
|
|
330
|
+
let cleaned = text;
|
|
331
|
+
let m;
|
|
332
|
+
while ((m = re.exec(text)) !== null) {
|
|
333
|
+
if (!skills.includes(m[1])) skills.push(m[1]);
|
|
334
|
+
cleaned = cleaned.replace(m[0], "");
|
|
335
|
+
}
|
|
336
|
+
const cleanedText = skills.length > 0 ? cleaned.replace(/^\s+|\s+$/g, "") : text;
|
|
337
|
+
return { skills, cleanedText };
|
|
338
|
+
}
|
|
339
|
+
function buildSkillToolCallPayload(skill) {
|
|
340
|
+
const args = {
|
|
341
|
+
skillName: skill.name,
|
|
342
|
+
...skill.description ? { description: skill.description } : {}
|
|
343
|
+
};
|
|
344
|
+
const result = { skillName: skill.name, status: "loaded" };
|
|
345
|
+
return { args: JSON.stringify(args), result: JSON.stringify(result) };
|
|
346
|
+
}
|
|
347
|
+
function parseSkillToolCallArgs(args) {
|
|
348
|
+
if (!args) return void 0;
|
|
349
|
+
try {
|
|
350
|
+
const parsed = JSON.parse(args);
|
|
351
|
+
if (typeof parsed?.skillName !== "string" || !parsed.skillName) return void 0;
|
|
352
|
+
return {
|
|
353
|
+
skillName: parsed.skillName,
|
|
354
|
+
...typeof parsed.description === "string" ? { description: parsed.description } : {}
|
|
355
|
+
};
|
|
356
|
+
} catch {
|
|
357
|
+
return void 0;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// src/pre-tool.ts
|
|
362
|
+
var PRE_TOOL_CALL_NAME = "__tbox_pretool";
|
|
363
|
+
var PRE_TOOL_NAME_PATTERN = new RegExp(`^[A-Za-z0-9_-]{1,64}$`);
|
|
364
|
+
function buildPreToolArgsPayload(input) {
|
|
365
|
+
const args = {
|
|
366
|
+
toolName: input.toolName,
|
|
367
|
+
...input.description ? { description: input.description } : {},
|
|
368
|
+
...input.toolArgs !== void 0 ? { toolArgs: input.toolArgs } : {}
|
|
369
|
+
};
|
|
370
|
+
return JSON.stringify(args);
|
|
371
|
+
}
|
|
372
|
+
function buildPreToolResultPayload(input) {
|
|
373
|
+
const result = {
|
|
374
|
+
toolName: input.toolName,
|
|
375
|
+
status: input.status,
|
|
376
|
+
...input.result !== void 0 ? { result: input.result } : {},
|
|
377
|
+
...input.error ? { error: input.error } : {}
|
|
378
|
+
};
|
|
379
|
+
return JSON.stringify(result);
|
|
380
|
+
}
|
|
381
|
+
function buildPreToolToolCallPayload(record) {
|
|
382
|
+
return {
|
|
383
|
+
args: buildPreToolArgsPayload({
|
|
384
|
+
toolName: record.toolName,
|
|
385
|
+
...record.description ? { description: record.description } : {},
|
|
386
|
+
...record.args !== void 0 ? { toolArgs: record.args } : {}
|
|
387
|
+
}),
|
|
388
|
+
result: buildPreToolResultPayload({
|
|
389
|
+
toolName: record.toolName,
|
|
390
|
+
status: "done",
|
|
391
|
+
...record.result !== void 0 ? { result: record.result } : {}
|
|
392
|
+
})
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
function parsePreToolToolCallArgs(args) {
|
|
396
|
+
if (!args) return void 0;
|
|
397
|
+
try {
|
|
398
|
+
const parsed = JSON.parse(args);
|
|
399
|
+
if (typeof parsed?.toolName !== "string" || !parsed.toolName) return void 0;
|
|
400
|
+
return {
|
|
401
|
+
toolName: parsed.toolName,
|
|
402
|
+
...typeof parsed.description === "string" ? { description: parsed.description } : {},
|
|
403
|
+
...parsed.toolArgs !== void 0 ? { toolArgs: parsed.toolArgs } : {}
|
|
404
|
+
};
|
|
405
|
+
} catch {
|
|
406
|
+
return void 0;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
function parsePreToolToolCallResult(result) {
|
|
410
|
+
if (!result) return void 0;
|
|
411
|
+
try {
|
|
412
|
+
const parsed = JSON.parse(result);
|
|
413
|
+
if (typeof parsed?.toolName !== "string" || !parsed.toolName) return void 0;
|
|
414
|
+
if (parsed.status !== "done" && parsed.status !== "error") return void 0;
|
|
415
|
+
return {
|
|
416
|
+
toolName: parsed.toolName,
|
|
417
|
+
status: parsed.status,
|
|
418
|
+
...parsed.result !== void 0 ? { result: parsed.result } : {},
|
|
419
|
+
...typeof parsed.error === "string" ? { error: parsed.error } : {}
|
|
420
|
+
};
|
|
421
|
+
} catch {
|
|
422
|
+
return void 0;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// src/memory.ts
|
|
427
|
+
var MEMORY_ERROR_CODES = {
|
|
428
|
+
/** 知识库实现:segment upsert 接口未开放(平台缺口 #1)——save 不可用 */
|
|
429
|
+
KB_SEGMENT_UPSERT_NOT_SUPPORTED: "MEMORY_KB_SEGMENT_UPSERT_NOT_SUPPORTED",
|
|
430
|
+
/** 知识库实现:segment delete 接口未开放(平台缺口 #2)——delete 不可用 */
|
|
431
|
+
KB_SEGMENT_DELETE_NOT_SUPPORTED: "MEMORY_KB_SEGMENT_DELETE_NOT_SUPPORTED",
|
|
432
|
+
/** 知识库实现:检索通道无 pre filter 能力——search 硬失败(永不无过滤检索,INV-1) */
|
|
433
|
+
KB_SEARCH_FILTER_NOT_SUPPORTED: "MEMORY_KB_SEARCH_FILTER_NOT_SUPPORTED",
|
|
434
|
+
/** 知识库实现:排序通道不可用——list 硬失败 */
|
|
435
|
+
KB_LIST_FIELDSORT_NOT_SUPPORTED: "MEMORY_KB_LIST_FIELDSORT_NOT_SUPPORTED",
|
|
436
|
+
/** 知识库实现:启动校验失败(异步闸门降级态)——记忆功能不可用,调用时报错 */
|
|
437
|
+
MEMORY_KB_VALIDATION_FAILED: "MEMORY_KB_VALIDATION_FAILED"
|
|
438
|
+
};
|
|
439
|
+
var MEMORY_REMINDER_ATTR = "memory";
|
|
440
|
+
|
|
441
|
+
// src/memory-tool.ts
|
|
442
|
+
var MEMORY_TOOL_CALL_NAME = "__tbox_memory";
|
|
443
|
+
var MEMORY_RECORD_PREVIEW_MAX_CHARS = 200;
|
|
444
|
+
function buildMemoryToolCallPayload(input) {
|
|
445
|
+
const args = {
|
|
446
|
+
query: input.query,
|
|
447
|
+
...input.topK !== void 0 ? { topK: input.topK } : {}
|
|
448
|
+
};
|
|
449
|
+
const records = input.records?.map((r) => ({
|
|
450
|
+
id: r.id,
|
|
451
|
+
kind: r.kind,
|
|
452
|
+
text: r.content.length > MEMORY_RECORD_PREVIEW_MAX_CHARS ? `${r.content.slice(0, MEMORY_RECORD_PREVIEW_MAX_CHARS)}\u2026` : r.content
|
|
453
|
+
}));
|
|
454
|
+
const result = {
|
|
455
|
+
status: input.status,
|
|
456
|
+
...input.status === "hit" && input.records ? { hits: input.records.length } : {},
|
|
457
|
+
...records && records.length > 0 ? { records } : {},
|
|
458
|
+
...input.reason ? { reason: input.reason } : {},
|
|
459
|
+
...input.error ? { error: input.error } : {},
|
|
460
|
+
...input.durMs !== void 0 ? { durMs: input.durMs } : {}
|
|
461
|
+
};
|
|
462
|
+
return { args: JSON.stringify(args), result: JSON.stringify(result) };
|
|
463
|
+
}
|
|
464
|
+
function parseMemoryToolCallArgs(args) {
|
|
465
|
+
if (!args) return void 0;
|
|
466
|
+
try {
|
|
467
|
+
const parsed = JSON.parse(args);
|
|
468
|
+
if (typeof parsed?.query !== "string") return void 0;
|
|
469
|
+
return {
|
|
470
|
+
query: parsed.query,
|
|
471
|
+
...typeof parsed.topK === "number" ? { topK: parsed.topK } : {}
|
|
472
|
+
};
|
|
473
|
+
} catch {
|
|
474
|
+
return void 0;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
function parseMemoryToolCallResult(result) {
|
|
478
|
+
if (!result) return void 0;
|
|
479
|
+
try {
|
|
480
|
+
const parsed = JSON.parse(result);
|
|
481
|
+
if (parsed?.status !== "hit" && parsed?.status !== "miss" && parsed?.status !== "degraded" && parsed?.status !== "skipped") {
|
|
482
|
+
return void 0;
|
|
483
|
+
}
|
|
484
|
+
return {
|
|
485
|
+
status: parsed.status,
|
|
486
|
+
...typeof parsed.hits === "number" ? { hits: parsed.hits } : {},
|
|
487
|
+
...Array.isArray(parsed.records) ? { records: parsed.records } : {},
|
|
488
|
+
...typeof parsed.reason === "string" ? { reason: parsed.reason } : {},
|
|
489
|
+
...typeof parsed.error === "string" ? { error: parsed.error } : {},
|
|
490
|
+
...typeof parsed.durMs === "number" ? { durMs: parsed.durMs } : {}
|
|
491
|
+
};
|
|
492
|
+
} catch {
|
|
493
|
+
return void 0;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// src/host-effect.ts
|
|
498
|
+
var TBOX_EFFECT_EVENT_NAME = "tbox:effect";
|
|
499
|
+
|
|
500
|
+
// src/protocol.ts
|
|
501
|
+
import { EventType } from "@ag-ui/core";
|
|
502
|
+
var KEEPALIVE_MISS_LIMIT = 3;
|
|
503
|
+
function nextKeepaliveAction(idleMs, intervalMs) {
|
|
504
|
+
if (idleMs >= intervalMs * KEEPALIVE_MISS_LIMIT) return "dead";
|
|
505
|
+
return idleMs >= intervalMs ? "ping" : "none";
|
|
506
|
+
}
|
|
38
507
|
export {
|
|
39
508
|
AUTH_ERROR_CODES,
|
|
40
|
-
EventType,
|
|
509
|
+
EventType2 as EventType,
|
|
510
|
+
KEEPALIVE_MISS_LIMIT,
|
|
511
|
+
MEMORY_ERROR_CODES,
|
|
512
|
+
MEMORY_RECORD_PREVIEW_MAX_CHARS,
|
|
513
|
+
MEMORY_REMINDER_ATTR,
|
|
514
|
+
MEMORY_TOOL_CALL_NAME,
|
|
515
|
+
PRE_TOOL_CALL_NAME,
|
|
516
|
+
PRE_TOOL_NAME_PATTERN,
|
|
517
|
+
PROVIDER_ERROR_CODES,
|
|
518
|
+
RESOURCE_TYPE_IDS,
|
|
519
|
+
SKILL_NAME_PATTERN,
|
|
520
|
+
SKILL_TOOL_CALL_NAME,
|
|
41
521
|
TBOX_CARD_EVENT_NAME,
|
|
42
|
-
|
|
522
|
+
TBOX_CARD_UPDATE_EVENT_NAME,
|
|
523
|
+
TBOX_EFFECT_EVENT_NAME,
|
|
524
|
+
TBOX_NOTICE_EVENT_NAME,
|
|
525
|
+
WILDCARD_INSTANCE_KEY,
|
|
526
|
+
assertNoInlineSecrets,
|
|
527
|
+
authCodeToStatus,
|
|
528
|
+
buildMemoryToolCallPayload,
|
|
529
|
+
buildPreToolArgsPayload,
|
|
530
|
+
buildPreToolResultPayload,
|
|
531
|
+
buildPreToolToolCallPayload,
|
|
532
|
+
buildSkillReminderOpenTagPattern,
|
|
533
|
+
buildSkillToolCallPayload,
|
|
534
|
+
buildSupplyLookup,
|
|
535
|
+
defineContextSlot,
|
|
536
|
+
isExpectedProviderAbsence,
|
|
537
|
+
moduleDomainOf,
|
|
538
|
+
nextKeepaliveAction,
|
|
539
|
+
normalizeInstances,
|
|
540
|
+
parseIntegrationsConfig,
|
|
541
|
+
parseMemoryToolCallArgs,
|
|
542
|
+
parseMemoryToolCallResult,
|
|
543
|
+
parsePreToolToolCallArgs,
|
|
544
|
+
parsePreToolToolCallResult,
|
|
545
|
+
parseSkillReminders,
|
|
546
|
+
parseSkillToolCallArgs,
|
|
547
|
+
resolveEffectiveService,
|
|
548
|
+
stableSerializeScope
|
|
43
549
|
};
|
package/dist/skill.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* skill 前端表达协议(009 D9/D10):reminder 文本协议工具 + tool-call 表达形状。
|
|
3
|
+
* - 单一来源:server(合成事件/注入解析)与 client(历史解析/显示)共用,禁止两端各自 hardcode;
|
|
4
|
+
* - 纯协议无 UI 语义:文案/渲染留装配层。
|
|
5
|
+
*/
|
|
6
|
+
/** skill 名强校验(与 reminder 开标签解析正则一致,保证解析安全) */
|
|
7
|
+
export declare const SKILL_NAME_PATTERN: RegExp;
|
|
8
|
+
/**
|
|
9
|
+
* 前端 skill 表达专用 tool-call 名(内部伪工具)。
|
|
10
|
+
* 与 mastra 原生 skill_read 命名空间解耦(同名不同义风险),对齐 __a2uiFormSubmit 先例;
|
|
11
|
+
* progressive 模式下模型真实 skill_read 调用不做本协议适配(显示默认工具文案)。
|
|
12
|
+
*/
|
|
13
|
+
export declare const SKILL_TOOL_CALL_NAME = "__tbox_skill";
|
|
14
|
+
/** skill 加载记录(server 持久化 inputs.skills 与 client 历史消费共用 DTO) */
|
|
15
|
+
export interface SkillRecord {
|
|
16
|
+
name: string;
|
|
17
|
+
description?: string;
|
|
18
|
+
}
|
|
19
|
+
/** 构造开标签匹配正则(每次 new,无状态)——server scanInjectedSkills 同源使用 */
|
|
20
|
+
export declare function buildSkillReminderOpenTagPattern(): RegExp;
|
|
21
|
+
/**
|
|
22
|
+
* 剥离 reminder 块(纯函数):
|
|
23
|
+
* - 完整块(开标签 + 闭标签)整体移除;skills 返回命中 name 列表(保序去重);
|
|
24
|
+
* - 未闭合/非法 name 不剥离(宁留不误删——name 强校验锚定,SOP 正文禁用完整开标签为既有铁律)。
|
|
25
|
+
*/
|
|
26
|
+
export declare function parseSkillReminders(text: string): {
|
|
27
|
+
skills: string[];
|
|
28
|
+
cleanedText: string;
|
|
29
|
+
};
|
|
30
|
+
/** 合成 tool-call args(tbox 协议形状,实时/历史共用) */
|
|
31
|
+
export interface SkillToolCallArgs {
|
|
32
|
+
skillName: string;
|
|
33
|
+
description?: string;
|
|
34
|
+
}
|
|
35
|
+
/** 合成 tool-call result(tbox 协议形状) */
|
|
36
|
+
export interface SkillToolCallResult {
|
|
37
|
+
skillName: string;
|
|
38
|
+
status: 'loaded';
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* 单点序列化:实时合成事件与历史表达生成共用 → 两路径字节级同构
|
|
42
|
+
* (args/result 的 JSON 形状只存在于本函数)。
|
|
43
|
+
*/
|
|
44
|
+
export declare function buildSkillToolCallPayload(skill: SkillRecord): {
|
|
45
|
+
args: string;
|
|
46
|
+
result: string;
|
|
47
|
+
};
|
|
48
|
+
/** 容错解析(前端显示用):非法 JSON / 缺 skillName → undefined(调用方降级默认文案) */
|
|
49
|
+
export declare function parseSkillToolCallArgs(args?: string): SkillToolCallArgs | undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tbox.cn/app-contracts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "平台契约(TIER0,types-only 为主 + 协议常量运行时值):卡片协议 / SDK adapter / 平台 DTO。",
|
|
6
6
|
"exports": {
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
"files": [
|
|
13
13
|
"dist",
|
|
14
14
|
"README.md",
|
|
15
|
-
"LICENSE"
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"tests"
|
|
16
17
|
],
|
|
17
18
|
"engines": {
|
|
18
19
|
"node": ">=20.0.0"
|
|
@@ -21,18 +22,21 @@
|
|
|
21
22
|
"access": "public"
|
|
22
23
|
},
|
|
23
24
|
"dependencies": {
|
|
24
|
-
"@ag-ui/core": "
|
|
25
|
-
"zod": "
|
|
26
|
-
"react": "
|
|
25
|
+
"@ag-ui/core": "0.0.47",
|
|
26
|
+
"zod": "3.25.76",
|
|
27
|
+
"react": "18.3.1"
|
|
27
28
|
},
|
|
28
29
|
"devDependencies": {
|
|
29
|
-
"tsup": "
|
|
30
|
-
"typescript": "
|
|
31
|
-
"@types/react": "
|
|
30
|
+
"tsup": "8.5.1",
|
|
31
|
+
"typescript": "5.9.3",
|
|
32
|
+
"@types/react": "18.3.31",
|
|
33
|
+
"vitest": "4.1.10",
|
|
34
|
+
"@types/node": "24.13.3"
|
|
32
35
|
},
|
|
33
36
|
"license": "MIT",
|
|
34
37
|
"scripts": {
|
|
35
|
-
"build": "tsc -p tsconfig.build.json && tsup src/runtime.ts --format esm --external @ag-ui/core --out-dir dist",
|
|
36
|
-
"typecheck": "tsc --noEmit"
|
|
38
|
+
"build": "tsc -p tsconfig.build.json && tsup src/runtime.ts --format esm --external @ag-ui/core --external zod --out-dir dist && node scripts/verify-runtime.mjs",
|
|
39
|
+
"typecheck": "tsc --noEmit",
|
|
40
|
+
"test": "vitest run"
|
|
37
41
|
}
|
|
38
42
|
}
|