Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(tool-execution): reject empty builtin tool arguments instead of dispatching {} (#19986)

Arvin Xu677cd5e0674c
diff --git a/apps/server/src/services/toolExecution/__tests__/builtin.test.ts b/apps/server/src/services/toolExecution/__tests__/builtin.test.ts
index 9d659e9f83f..2e0049ed174 100644
--- a/apps/server/src/services/toolExecution/__tests__/builtin.test.ts
+++ b/apps/server/src/services/toolExecution/__tests__/builtin.test.ts
@@ -14,6 +14,8 @@ vi.mock('../serverRuntimes', () => ({
hasServerRuntime: vi.fn().mockReturnValue(true),
getServerRuntime: vi.fn(async () => ({
createDocument: mocks.apiHandler,
+ getCommandOutput: mocks.apiHandler,
+ runCommand: mocks.apiHandler,
searchUserMemory: mocks.apiHandler,
})),
}));
@@ -48,6 +50,25 @@ vi.mock('@lobechat/builtin-tools', () => ({
identifier: 'lobe-notebook',
manifest: { api: [{ name: 'createDocument' }, { name: 'listDocuments' }] },
},
+ {
+ identifier: 'lobe-local-system',
+ manifest: {
+ api: [
+ {
+ name: 'runCommand',
+ parameters: {
+ properties: { command: { type: 'string' }, description: { type: 'string' } },
+ required: ['description', 'command'],
+ type: 'object',
+ },
+ },
+ {
+ name: 'getCommandOutput',
+ parameters: { properties: { shell_id: { type: 'string' } }, type: 'object' },
+ },
+ ],
+ },
+ },
{
identifier: 'lobe-user-memory',
manifest: {
@@ -193,6 +214,80 @@ describe('BuiltinToolsExecutor truncated arguments', () => {
expect(result.success).toBe(true);
});
+ describe('empty arguments string', () => {
+ const buildRunCommandPayload = (argsStr: string): ChatToolPayload => ({
+ apiName: 'runCommand',
+ arguments: argsStr,
+ id: 't-run',
+ identifier: 'lobe-local-system',
+ type: 'builtin' as any,
+ });
+
+ // A tool call whose argument deltas never arrived (e.g. an OpenAI-compatible
+ // proxy that drops them) accumulates to `arguments: ""`. Silently passing
+ // `{}` made the device reply "command is required", which misled the model
+ // into blaming the platform instead of resending the call.
+ it('returns EMPTY_ARGUMENTS without invoking an API that declares required params', async () => {
+ const result = await executor.execute(buildRunCommandPayload(''), context);
+
+ expect(mockApiHandler).not.toHaveBeenCalled();
+ expect(result.success).toBe(false);
+ expect(result.error?.code).toBe('EMPTY_ARGUMENTS');
+ expect(result.content).toMatch(/empty arguments string/);
+ expect(result.content).toMatch(/command/);
+ });
+
+ it('treats a whitespace-only arguments string as empty', async () => {
+ const result = await executor.execute(buildRunCommandPayload(' '), context);
+
+ expect(result.success).toBe(false);
+ expect(result.error?.code).toBe('EMPTY_ARGUMENTS');
+ expect(mockApiHandler).not.toHaveBeenCalled();
+ });
+
+ it('prefers the manifest from the execution context when present', async () => {
+ const result = await executor.execute(
+ { ...buildRunCommandPayload(''), apiName: 'createDocument', identifier: 'lobe-notebook' },
+ {
+ ...context,
+ toolManifestMap: {
+ 'lobe-notebook': {
+ api: [
+ {
+ description: '',
+ name: 'createDocument',
+ parameters: {
+ properties: { title: { type: 'string' } },
+ required: ['title'],
+ type: 'object',
+ },
+ },
+ ],
+ identifier: 'lobe-notebook',
+ meta: {},
+ type: 'builtin',
+ } as any,
+ },
+ },
+ );
+
+ expect(mockApiHandler).not.toHaveBeenCalled();
+ expect(result.error?.code).toBe('EMPTY_ARGUMENTS');
+ });
+
+ it('still dispatches with {} for an API without required params', async () => {
+ mockApiHandler.mockResolvedValueOnce({ content: 'ok', success: true });
+
+ const result = await executor.execute(
+ { ...buildRunCommandPayload(''), apiName: 'getCommandOutput' },
+ context,
+ );
+
+ expect(mockApiHandler).toHaveBeenCalledWith({}, context);
+ expect(result.success).toBe(true);
+ });
+ });
+
it('returns a recoverable UNKNOWN_API error for a hallucinated apiName', async () => {
// The runtime mock only exposes `createDocument`; calling a non-existent
// API (e.g. a model hallucinating `viewTopic`) must NOT throw a hard error
diff --git a/apps/server/src/services/toolExecution/builtin.ts b/apps/server/src/services/toolExecution/builtin.ts
index 076f2fb1280..0b3b3c2a777 100644
--- a/apps/server/src/services/toolExecution/builtin.ts
+++ b/apps/server/src/services/toolExecution/builtin.ts
@@ -41,6 +41,22 @@ const getManifestApiNames = (identifier: string): string[] =>
(api) => api.name,
);
+/**
+ * Required parameter names declared by a builtin API. Prefers the manifest the
+ * run was assembled with, falling back to the static builtin manifest.
+ */
+const getRequiredParams = (
+ identifier: string,
+ apiName: string,
+ context: ToolExecutionContext,
+): string[] => {
+ const manifest =
+ context.toolManifestMap?.[identifier] ??
+ builtinTools.find((tool) => tool.identifier === identifier)?.manifest;
+ const required = manifest?.api?.find((api) => api.name === apiName)?.parameters?.required;
+ return Array.isArray(required) ? required : [];
+};
+
/**
* Fallback when a manifest isn't available (e.g. a runtime registered without a
* matching manifest entry): collect callable names across the whole prototype
@@ -101,6 +117,29 @@ export class BuiltinToolsExecutor implements IToolExecutor {
context: ToolExecutionContext,
): Promise<ToolExecutionResult> {
const { identifier, apiName, arguments: argsStr, source } = payload;
+
+ // An empty arguments string means the call reached us without any argument
+ // deltas (not generated, or dropped by the provider / an OpenAI-compatible
+ // proxy in transit). Falling back to `{}` for an API with required params
+ // surfaced as a misleading tool error (e.g. "command is required") that the
+ // model blamed on the platform. APIs without required params keep `{}`.
+ if (!argsStr?.trim()) {
+ const required = getRequiredParams(identifier, apiName, context);
+ if (required.length > 0) {
+ const message =
+ `The tool call arrived with an empty arguments string, so the tool was not invoked. ` +
+ `The arguments were either not generated or lost in transit before reaching the tool. ` +
+ `Resend the call with the complete JSON arguments, including the required parameters: ` +
+ `${required.join(', ')}.`;
+ log('Rejected empty arguments for %s:%s', identifier, apiName);
+ return {
+ content: message,
+ error: { code: 'EMPTY_ARGUMENTS', message },
+ success: false,
+ };
+ }
+ }
+
const parsed = safeParseJSON(argsStr);
// When JSON.parse fails, return a dedicated error rather than silently
@@ -110,7 +149,7 @@ export class BuiltinToolsExecutor implements IToolExecutor {
// max_tokens is exhausted mid-tool-call) from plain malformed JSON, and
// echo the raw arguments string so the model can verify it is exactly
// what it produced.
- if (parsed === undefined && argsStr) {
+ if (parsed === undefined && argsStr?.trim()) {
const truncationReason = detectTruncatedJSON(argsStr);
const explanation = truncationReason
? `The tool call arguments JSON appears to be truncated (${truncationReason}), ` +