@wacry/codeks-bridge 0.0.18 → 0.0.19
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/tool-calls.js +38 -3
- package/docs/tools.md +2 -0
- package/package.json +1 -1
package/dist/tool-calls.js
CHANGED
|
@@ -24,6 +24,32 @@ export function toolCallDiagnostic(item) {
|
|
|
24
24
|
if (wire?.error !== undefined)
|
|
25
25
|
return { name: wire.name, message: wire.error };
|
|
26
26
|
}
|
|
27
|
+
function editDistance(a, b) {
|
|
28
|
+
const row = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
29
|
+
for (let i = 1; i <= a.length; i++) {
|
|
30
|
+
let diagonal = row[0];
|
|
31
|
+
row[0] = i;
|
|
32
|
+
for (let j = 1; j <= b.length; j++) {
|
|
33
|
+
const previous = row[j];
|
|
34
|
+
row[j] = Math.min(row[j] + 1, row[j - 1] + 1, diagonal + Number(a[i - 1] !== b[j - 1]));
|
|
35
|
+
diagonal = previous;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return row[b.length];
|
|
39
|
+
}
|
|
40
|
+
/** Rank hints only; approximate names never participate in dispatch. */
|
|
41
|
+
function suggestedNames(tools, name) {
|
|
42
|
+
const fullName = name.replaceAll(".", "__");
|
|
43
|
+
const separator = fullName.lastIndexOf("__");
|
|
44
|
+
const namespace = separator < 0 ? undefined : fullName.slice(0, separator);
|
|
45
|
+
const shortName = separator < 0 ? fullName : fullName.slice(separator + 2);
|
|
46
|
+
return tools.entries.map(tool => ({
|
|
47
|
+
name: tool.flatName,
|
|
48
|
+
distance: Math.min(editDistance(fullName, tool.flatName), editDistance(shortName, tool.name)),
|
|
49
|
+
sameNamespace: namespace === (tool.namespace || "functions"),
|
|
50
|
+
})).sort((a, b) => a.distance - b.distance || Number(b.sameNamespace) - Number(a.sameNamespace))
|
|
51
|
+
.map(tool => tool.name);
|
|
52
|
+
}
|
|
27
53
|
/** Resolve a model's name without changing the declared tool table. */
|
|
28
54
|
export function prepareToolCall(tools, id, name, args, argumentsText = JSON.stringify(args)) {
|
|
29
55
|
if (typeof id !== "string" || !id || typeof name !== "string" || !name) {
|
|
@@ -33,9 +59,18 @@ export function prepareToolCall(tools, id, name, args, argumentsText = JSON.stri
|
|
|
33
59
|
const tool = candidates.length === 1 ? candidates[0] : undefined;
|
|
34
60
|
let error;
|
|
35
61
|
if (!tool) {
|
|
36
|
-
const choices = (candidates.length ? candidates
|
|
37
|
-
|
|
38
|
-
|
|
62
|
+
const choices = (candidates.length ? candidates.map(tool => tool.flatName) : suggestedNames(tools, name)).slice(0, 5);
|
|
63
|
+
const total = candidates.length || tools.entries.length;
|
|
64
|
+
error = `Tool name ${JSON.stringify(name)} is ${candidates.length ? "ambiguous" : "unknown"}. No tool was executed.`;
|
|
65
|
+
if (choices.length) {
|
|
66
|
+
const heading = candidates.length ? "Matching tools" : "Closest available tools";
|
|
67
|
+
const count = choices.length < total ? ` (showing ${choices.length} of ${total})` : "";
|
|
68
|
+
error += `\n${heading}${count}:\n${choices.map(choice => `- ${choice}`).join("\n")}` +
|
|
69
|
+
"\nUse an exact tool name from the available tool definitions.";
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
error += "\nNo tools are available.";
|
|
73
|
+
}
|
|
39
74
|
// This identity is deliberately absent from codeks. Never dispatch an
|
|
40
75
|
// ambiguous bare name: it might accidentally select a default tool.
|
|
41
76
|
if (tools.get(ERROR_NAMESPACE, ERROR_NAME)) {
|
package/docs/tools.md
CHANGED
|
@@ -28,6 +28,8 @@ Messages/Chat 返回的调用还接受完整的 `namespace.name` 和唯一的原
|
|
|
28
28
|
|
|
29
29
|
歧义或未知调用通过普通工具错误反馈,提示可用的完整声明名。例如两个 namespace 都有 `read` 时,提示 `files__read`、`database__read`,让模型下一次明确选择。bridge 不擅自执行其中一个,也不把这类错误变成网络重试。它将失败调用交给客户端不存在的保留身份 `codeks_bridge_tool_error.invalid_call`,客户端记录普通未知工具结果;后续发包恢复原始调用并将该结果替换为保存的候选诊断。真实缺失 id、损坏 JSON 或 custom 输入仍是协议错误。
|
|
30
30
|
|
|
31
|
+
未知名称最多推荐 5 个工具:完整名称(将点号统一为 `__`)与去掉 namespace 的短名分别计算编辑距离,取较小值排序;距离相同时优先相同 namespace,其余同分项保留声明顺序。歧义名称只按声明顺序列出真正的同名候选,同样最多 5 个,不混入其他近似工具。始终显示可直接调用的完整声明名,截取时标出展示数量与候选总数(未知名称为全部工具数,歧义名称为同名候选数)。用户选择这一限制,是为了让相关候选更显眼,同时减少错误反馈长度;它只改变提示,不进行近似匹配执行,无需新增配置。
|
|
32
|
+
|
|
31
33
|
只有接受了别名或需要反馈的调用才将原始上游 id、名字和可选诊断编码为 `cxbt1_` call ID。客户端照常保存该 id 并配对结果,bridge 在 Messages/Chat 回放时恢复原始 id 和名字,避免把模型生成的 `exec` 悄悄改成 `functions__exec` 而改变签名前缀;诊断也随历史固定,不随以后工具表变化。载体不含参数、凭据或完整会话,不需要额外状态文件。检查器可查看原始上游调用、客户端失败身份与下一次上游收到的诊断。
|
|
32
34
|
|
|
33
35
|
Responses 的 `call_id` 最长 64 字符,`cxbt1_` 载体可能超过这个限制。GPT 出站时,对超过 64 字符的工具 ID(包括载体和其他上游的原始 ID),使用完整客户端 ID 的 SHA-256 生成固定 48 字符的 `call_`+base64url 摘要;function/custom 调用及对应结果使用同一映射。64 字符以内的 ID(包括短载体)保留原值,减少对已被 GPT 接受的历史前缀的改动;无 ID 的主动通知不生成 ID。映射不依赖调用位置、模型别名或内存状态,因此恢复会话、重试及后续 turn 都保持一致。
|
package/package.json
CHANGED