@tianshu-ai/tianshu 0.49.1 → 0.50.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/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAElD,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACxF,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACxE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAE7D,MAAM,MAAM,UAAU,GAAG,OAAO,CAAC;AACjC,MAAM,MAAM,YAAY,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,UAAU,CAAC,GAAG,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAElD,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACxF,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACxE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAE7D,MAAM,MAAM,UAAU,GAAG,OAAO,CAAC;AACjC,MAAM,MAAM,YAAY,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,UAAU,CAAC,GAAG,UAAU,CAAC;AAqF/F,MAAM,WAAW,OAAO;IACtB,6DAA6D;IAC7D,OAAO,EAAE,IA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IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;;;OAMG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED;;;GAGG;AACH,wBAAsB,YAAY,CAAC,IAAI,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC,CAoF3E"}
|
|
@@ -15,6 +15,77 @@
|
|
|
15
15
|
// `files` plugin disabled and no microsandbox), the agent simply
|
|
16
16
|
// gets an empty toolset \u2014 it can still answer questions, just
|
|
17
17
|
// can't touch files or run commands.
|
|
18
|
+
/**
|
|
19
|
+
* Wrap a raw tool executor with a safety net that:
|
|
20
|
+
* 1. Honors an aborted signal before invoking the tool.
|
|
21
|
+
* 2. Catches any throw / promise rejection from the tool body
|
|
22
|
+
* — including from inner async code paths the caller might
|
|
23
|
+
* have overlooked — and rethrows a normalized error message
|
|
24
|
+
* that reads as "tool X failed: <cause>. You may retry with
|
|
25
|
+
* different arguments or take an alternative approach."
|
|
26
|
+
*
|
|
27
|
+
* Why not just let pi handle it? pi-agent-core's executePreparedToolCall
|
|
28
|
+
* already catches sync/async throws and turns them into a
|
|
29
|
+
* `{ result: ErrorToolResult, isError: true }` for the agent — but
|
|
30
|
+
* the resulting `tool_result` content is just the bare Error.message,
|
|
31
|
+
* which for cases like bridge exec / network SSH failures reads as
|
|
32
|
+
* something opaque like "ECONNRESET" or "aborted" and the agent
|
|
33
|
+
* doesn't always realize the sensible next move is "try again" or
|
|
34
|
+
* "tell the user what failed".
|
|
35
|
+
*
|
|
36
|
+
* This wrapper does NOT swallow the error — it re-throws. pi still
|
|
37
|
+
* takes the standard path (isError=true tool_result gets sent back
|
|
38
|
+
* to the model). We only enrich the message so the model has
|
|
39
|
+
* enough context to decide what to do next instead of stalling.
|
|
40
|
+
*/
|
|
41
|
+
function wrapExecutorWithErrorGuard(toolName, origin, toolContext, raw) {
|
|
42
|
+
return async (args) => {
|
|
43
|
+
if (toolContext.signal?.aborted) {
|
|
44
|
+
throw new Error("aborted by user");
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
// Await here (rather than returning the Promise directly) so
|
|
48
|
+
// async rejections land in our catch block, not on the caller.
|
|
49
|
+
return await raw(args);
|
|
50
|
+
}
|
|
51
|
+
catch (err) {
|
|
52
|
+
const causeMessage = err instanceof Error ? err.message : String(err);
|
|
53
|
+
const causeStack = err instanceof Error && err.stack ? err.stack : undefined;
|
|
54
|
+
// Detailed context in the server log — the agent doesn't need
|
|
55
|
+
// stack traces, but a human debugging bridge/tool crashes does.
|
|
56
|
+
toolContext.log.warn(`[tool-guard] ${origin}:${toolName} threw: ${causeMessage}${causeStack ? `\n${causeStack.split("\n").slice(0, 6).join("\n")}` : ""}`);
|
|
57
|
+
// Rethrow with an agent-actionable message. pi's tool-loop
|
|
58
|
+
// catches this and returns it as isError=true tool_result,
|
|
59
|
+
// so the model sees this text as its next input.
|
|
60
|
+
const guidance = deriveRecoveryHint(toolName, causeMessage);
|
|
61
|
+
throw new Error(`${toolName} failed: ${causeMessage}. ${guidance}`);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Best-effort tool-specific guidance strings that tell the agent
|
|
67
|
+
* what a plausible recovery move looks like. Keeps the message
|
|
68
|
+
* short (<200 chars) so it doesn't dominate the tool_result.
|
|
69
|
+
*/
|
|
70
|
+
function deriveRecoveryHint(toolName, causeMessage) {
|
|
71
|
+
const lower = causeMessage.toLowerCase();
|
|
72
|
+
// Bridge exec / SSH / remote tools — usually transient network
|
|
73
|
+
// or process-crash failures.
|
|
74
|
+
if (/^bridge_.*_exec$|bridge_.*_shell$/i.test(toolName)) {
|
|
75
|
+
if (/econnrefused|econnreset|etimedout|network|socket|hang up|closed/i.test(lower)) {
|
|
76
|
+
return "The bridge connection may have dropped. You may retry the command once; if it still fails, tell the user the bridge is unreachable and stop retrying.";
|
|
77
|
+
}
|
|
78
|
+
if (/exit(ed)? .*(1|127|130|137)|non-zero exit|failed with code/i.test(lower)) {
|
|
79
|
+
return "The command completed but exited non-zero. Read the output above (if any) to decide whether to fix the arguments, run a different command, or report the failure to the user.";
|
|
80
|
+
}
|
|
81
|
+
return "Retry once with the same or corrected arguments; if it fails a second time, describe the failure to the user rather than looping.";
|
|
82
|
+
}
|
|
83
|
+
if (/aborted by user/i.test(lower)) {
|
|
84
|
+
return "The user cancelled this turn. Stop working and wait for the next user message.";
|
|
85
|
+
}
|
|
86
|
+
// Generic default — don't over-promise a fix.
|
|
87
|
+
return "You may retry with different arguments or take an alternative approach; do not repeat the exact same call more than twice in a row.";
|
|
88
|
+
}
|
|
18
89
|
/**
|
|
19
90
|
* Build a per-request toolset. Plugin tools are registered iff
|
|
20
91
|
* their `available()` hook (default: true) returns truthy.
|
|
@@ -72,23 +143,13 @@ export async function buildToolset(opts) {
|
|
|
72
143
|
const effectiveAccess = toolAccess ?? "member";
|
|
73
144
|
if (effectiveAccess === "admin" && toolContext.userRole === "member")
|
|
74
145
|
continue;
|
|
75
|
-
executors[name] = (args) =>
|
|
76
|
-
if (toolContext.signal?.aborted) {
|
|
77
|
-
throw new Error("aborted by user");
|
|
78
|
-
}
|
|
79
|
-
return tool.execute(args, ctx);
|
|
80
|
-
};
|
|
146
|
+
executors[name] = wrapExecutorWithErrorGuard(name, pluginId, toolContext, (args) => tool.execute(args, ctx));
|
|
81
147
|
}
|
|
82
148
|
// Host-level tools (always available, not from plugins).
|
|
83
149
|
for (const { schema, executor } of opts.hostTools ?? []) {
|
|
84
150
|
if (!executors[schema.name]) {
|
|
85
151
|
schemas.push(schema);
|
|
86
|
-
executors[schema.name] = (args) =>
|
|
87
|
-
if (toolContext.signal?.aborted) {
|
|
88
|
-
throw new Error("aborted by user");
|
|
89
|
-
}
|
|
90
|
-
return executor(args);
|
|
91
|
-
};
|
|
152
|
+
executors[schema.name] = wrapExecutorWithErrorGuard(schema.name, "host", toolContext, (args) => executor(args));
|
|
92
153
|
}
|
|
93
154
|
}
|
|
94
155
|
return { schemas, executors };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAAA,2BAA2B;AAC3B,EAAE;AACF,kEAAkE;AAClE,gEAAgE;AAChE,8DAA8D;AAC9D,uCAAuC;AACvC,EAAE;AACF,4DAA4D;AAC5D,mEAAmE;AACnE,yCAAyC;AACzC,gEAAgE;AAChE,cAAc;AACd,EAAE;AACF,8DAA8D;AAC9D,iEAAiE;AACjE,mEAAmE;AACnE,qCAAqC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAAA,2BAA2B;AAC3B,EAAE;AACF,kEAAkE;AAClE,gEAAgE;AAChE,8DAA8D;AAC9D,uCAAuC;AACvC,EAAE;AACF,4DAA4D;AAC5D,mEAAmE;AACnE,yCAAyC;AACzC,gEAAgE;AAChE,cAAc;AACd,EAAE;AACF,8DAA8D;AAC9D,iEAAiE;AACjE,mEAAmE;AACnE,qCAAqC;AAWrC;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,SAAS,0BAA0B,CACjC,QAAgB,EAChB,MAAc,EACd,WAAwD,EACxD,GAAwE;IAExE,OAAO,KAAK,EAAE,IAAI,EAAE,EAAE;QACpB,IAAI,WAAW,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC;YAChC,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;QACrC,CAAC;QACD,IAAI,CAAC;YACH,6DAA6D;YAC7D,+DAA+D;YAC/D,OAAO,MAAM,GAAG,CAAC,IAAI,CAAC,CAAC;QACzB,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,YAAY,GAAG,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YACtE,MAAM,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