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(lobe-agent): treat a blank callSubAgent subAgentId as a new sub-agent (#20066)

Arvin Xudccd37e50428
diff --git a/apps/server/src/services/aiAgent/__tests__/execSubAgent.test.ts b/apps/server/src/services/aiAgent/__tests__/execSubAgent.test.ts
index b702d4555dc..093689577e3 100644
--- a/apps/server/src/services/aiAgent/__tests__/execSubAgent.test.ts
+++ b/apps/server/src/services/aiAgent/__tests__/execSubAgent.test.ts
@@ -722,6 +722,17 @@ describe('AiAgentService.execSubAgent', () => {
expect(result.error).toContain(error);
});
+ // Models that can't omit the key invent ids like "new" or "exchange-rate-target";
+ // the error has to say how to start a fresh one instead.
+ it('tells the model how to start a new sub-agent when the id is unknown', async () => {
+ mockThreadModel.findById.mockResolvedValue(null);
+
+ const result = await service.execVirtualSubAgent({ ...followUp, threadId: 'new' });
+
+ expect(result.success).toBe(false);
+ expect(result.error).toContain('leave it empty to start a new sub-agent');
+ });
+
it('adds the new run usage to the totals of earlier runs', async () => {
mockThreadModel.findById.mockResolvedValue({
agentId: 'agent-1',
diff --git a/apps/server/src/services/aiAgent/subAgentRuns.ts b/apps/server/src/services/aiAgent/subAgentRuns.ts
index 182128c862a..c736797e729 100644
--- a/apps/server/src/services/aiAgent/subAgentRuns.ts
+++ b/apps/server/src/services/aiAgent/subAgentRuns.ts
@@ -112,7 +112,9 @@ const claimSubAgentThread = async (
thread.topicId !== topicId ||
thread.agentId !== agentId
) {
- return { error: `Sub-agent "${threadId}" was not found in this conversation.` };
+ return {
+ error: `Sub-agent "${threadId}" was not found in this conversation. subAgentId only takes the id from an earlier <sub_agent id="..." /> tag; leave it empty to start a new sub-agent.`,
+ };
}
const sourcePlugin = thread.sourceMessageId
diff --git a/apps/server/src/services/toolExecution/serverRuntimes/__tests__/lobeAgent.test.ts b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/lobeAgent.test.ts
index 7cc9b62d118..c326c2abbec 100644
--- a/apps/server/src/services/toolExecution/serverRuntimes/__tests__/lobeAgent.test.ts
+++ b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/lobeAgent.test.ts
@@ -968,12 +968,34 @@ describe('lobeAgentRuntime', () => {
);
});
- it('rejects an empty subAgentId instead of silently starting a new sub-agent', async () => {
+ // GPT-family models fill every declared field, so "start a new sub-agent"
+ // arrives as `subAgentId: ""` rather than an omitted key.
+ it.each(['', ' '])('starts a new sub-agent when subAgentId is blank (%j)', async (blank) => {
+ const runtime = lobeAgentRuntime.factory(baseContext);
+ const run = vi
+ .fn()
+ .mockResolvedValue({ started: true, subOperationId: 'sub-op-3', threadId: 'thread-3' });
+
+ const result = await runtime.callSubAgent(
+ { description: 'Research', instruction: 'Find the answer', subAgentId: blank },
+ { ...baseContext, subAgent: { run } } as ToolExecutionContext,
+ );
+
+ expect(run).toHaveBeenCalledWith({
+ description: 'Research',
+ instruction: 'Find the answer',
+ subAgentId: undefined,
+ timeout: undefined,
+ });
+ expect(result).toMatchObject({ deferred: true, success: true });
+ });
+
+ it('rejects a non-string subAgentId', async () => {
const runtime = lobeAgentRuntime.factory(baseContext);
const run = vi.fn();
const result = await runtime.callSubAgent(
- { description: 'Hand over', instruction: 'Summarize', subAgentId: ' ' },
+ { description: 'Hand over', instruction: 'Summarize', subAgentId: 42 as any },
{ ...baseContext, subAgent: { run } } as ToolExecutionContext,
);
diff --git a/apps/server/src/services/toolExecution/serverRuntimes/lobeAgent.ts b/apps/server/src/services/toolExecution/serverRuntimes/lobeAgent.ts
index d85f67ad4e5..2bef60b1300 100644
--- a/apps/server/src/services/toolExecution/serverRuntimes/lobeAgent.ts
+++ b/apps/server/src/services/toolExecution/serverRuntimes/lobeAgent.ts
@@ -266,18 +266,22 @@ class LobeAgentExecutionRuntime {
);
}
- const { description, instruction, subAgentId, timeout } = params;
+ const { description, instruction, timeout } = params;
if (!instruction || typeof instruction !== 'string') {
return buildError('instruction is required.', 'INVALID_ARGUMENTS');
}
- if (subAgentId !== undefined && (typeof subAgentId !== 'string' || !subAgentId.trim())) {
- return buildError('subAgentId must be a non-empty string.', 'INVALID_ARGUMENTS');
+ if (params.subAgentId !== undefined && typeof params.subAgentId !== 'string') {
+ return buildError('subAgentId must be a string.', 'INVALID_ARGUMENTS');
}
+ // Models trained on strict function schemas (the GPT family) fill every
+ // declared field, so "start a new sub-agent" arrives as `subAgentId: ""`
+ // rather than an omitted key. Blank means new.
+ const subAgentId = params.subAgentId?.trim() || undefined;
const { started, error, threadId, subOperationId, toolMessageId } = await ctx.subAgent.run({
description,
instruction,
- subAgentId: subAgentId?.trim(),
+ subAgentId,
timeout,
});
diff --git a/packages/builtin-tool-lobe-agent/src/client/executor/callSubAgent.test.ts b/packages/builtin-tool-lobe-agent/src/client/executor/callSubAgent.test.ts
index 974607946a8..8fa69458a5e 100644
--- a/packages/builtin-tool-lobe-agent/src/client/executor/callSubAgent.test.ts
+++ b/packages/builtin-tool-lobe-agent/src/client/executor/callSubAgent.test.ts
@@ -60,6 +60,35 @@ describe('lobeAgentExecutor.callSubAgent', () => {
expect(run).not.toHaveBeenCalled();
});
+ // GPT-family models fill every declared field, so a fresh dispatch arrives as
+ // `subAgentId: ""` (or whitespace) instead of an omitted key.
+ it('treats a blank subAgentId as starting a new sub-agent', async () => {
+ const run = vi.fn().mockResolvedValue({ result: 'done', success: true, threadId: 'thd_2' });
+
+ const result = await lobeAgentExecutor.callSubAgent(
+ { ...params, subAgentId: ' ' },
+ createContext(run),
+ );
+
+ expect(result.success).toBe(true);
+ expect(run).toHaveBeenCalledTimes(1);
+ });
+
+ it.each([42, true, {}])(
+ 'rejects a non-string subAgentId (%j) without dispatching',
+ async (id) => {
+ const run = vi.fn();
+
+ const result = await lobeAgentExecutor.callSubAgent(
+ { ...params, subAgentId: id as any },
+ createContext(run),
+ );
+
+ expect(result).toEqual({ content: 'subAgentId must be a string.', success: false });
+ expect(run).not.toHaveBeenCalled();
+ },
+ );
+
it('surfaces a failed run as a tool error without state', async () => {
const run = vi.fn().mockResolvedValue({
error: 'boom',
diff --git a/packages/builtin-tool-lobe-agent/src/client/executor/index.ts b/packages/builtin-tool-lobe-agent/src/client/executor/index.ts
index cacf79f6d01..1ce94f33418 100644
--- a/packages/builtin-tool-lobe-agent/src/client/executor/index.ts
+++ b/packages/builtin-tool-lobe-agent/src/client/executor/index.ts
@@ -456,19 +456,27 @@ class LobeAgentExecutor extends BaseExecutor<typeof LobeAgentApiName> {
return nestedSubAgentDisabledResult();
}
- const { description, instruction, inheritMessages, subAgentId, timeout } = params;
+ const { description, instruction, inheritMessages, timeout } = params;
if (!description || !instruction) {
return { content: 'Sub-agent description and instruction are required.', success: false };
}
+ // Tool-call JSON reaches this executor without schema validation; a
+ // malformed id must not silently start a fresh (billed) sub-agent.
+ if (params.subAgentId !== undefined && typeof params.subAgentId !== 'string') {
+ return { content: 'subAgentId must be a string.', success: false };
+ }
+ // Strict-schema models send `subAgentId: ""` to mean "start a new one".
+ const subAgentId = params.subAgentId?.trim();
+
// Continuing an earlier sub-agent is implemented by the server runtime only.
// Fail loudly instead of silently starting a fresh sub-agent that has none
// of the earlier sub-agent's history.
if (subAgentId) {
return {
content:
- 'Continuing an earlier sub-agent (subAgentId) is not supported in this runtime. Omit subAgentId to start a new sub-agent.',
+ 'Continuing an earlier sub-agent (subAgentId) is not supported in this runtime. Leave subAgentId empty to start a new sub-agent.',
success: false,
};
}
diff --git a/packages/builtin-tool-lobe-agent/src/manifest.ts b/packages/builtin-tool-lobe-agent/src/manifest.ts
index df3355c087f..ceb1ac2b15f 100644
--- a/packages/builtin-tool-lobe-agent/src/manifest.ts
+++ b/packages/builtin-tool-lobe-agent/src/manifest.ts
@@ -253,7 +253,7 @@ export const LobeAgentManifest: BuiltinToolManifest = {
},
subAgentId: {
description:
- 'Optional. The id from the `<sub_agent id="..." />` tag at the end of an earlier callSubAgent result. Sends `instruction` as a new message to that same sub-agent, which keeps all of its previous work and history β€” use it to continue a sub-agent that stopped or failed, ask it to hand over what it has found so far, or follow up on its answer. Omit to start a new sub-agent.',
+ 'Optional. The id from the `<sub_agent id="..." />` tag at the end of an earlier callSubAgent result. Sends `instruction` as a new message to that same sub-agent, which keeps all of its previous work and history β€” use it to continue a sub-agent that stopped or failed, ask it to hand over what it has found so far, or follow up on its answer. Leave empty (or omit) to start a new sub-agent; never invent an id.',
type: 'string',
},
...(isDesktop && {