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.

refactor for execSubAgentTask

arvinxx0f57b8aacc98
diff --git a/packages/types/src/agentGroup/index.ts b/packages/types/src/agentGroup/index.ts
index 46d263e98d5..676a2e06a58 100644
--- a/packages/types/src/agentGroup/index.ts
+++ b/packages/types/src/agentGroup/index.ts
@@ -242,20 +242,23 @@ export interface ExecGroupAgentResponse {
userMessageId: string;
}
-// ============ Group SubAgent Task Execution Types ============
+// ============ SubAgent Task Execution Types ============
/**
- * Parameters for execGroupSubAgentTask - execute SubAgent task in Group chat
- * This is called by Supervisor to delegate tasks to SubAgents
+ * Parameters for execSubAgentTask - execute SubAgent task
+ * Supports both Group mode and Single Agent mode
+ *
+ * - Group mode: pass groupId, Thread will be associated with the Group
+ * - Single Agent mode: omit groupId, Thread will only be associated with the Agent
*/
-export interface ExecGroupSubAgentTaskParams {
+export interface ExecSubAgentTaskParams {
/** The SubAgent ID to execute the task */
agentId: string;
- /** The Group ID (required) */
- groupId: string;
+ /** The Group ID (optional, only for Group mode) */
+ groupId?: string;
/** Task instruction/prompt for the SubAgent */
instruction: string;
- /** The parent message ID (Supervisor's tool call message) */
+ /** The parent message ID (Supervisor's tool call message or task message) */
parentMessageId: string;
/** Timeout in milliseconds (optional) */
timeout?: number;
@@ -264,9 +267,9 @@ export interface ExecGroupSubAgentTaskParams {
}
/**
- * Result from execGroupSubAgentTask
+ * Result from execSubAgentTask
*/
-export interface ExecGroupSubAgentTaskResult {
+export interface ExecSubAgentTaskResult {
/** The assistant message ID created for this task */
assistantMessageId: string;
/** Error message if task failed to start */
@@ -279,6 +282,16 @@ export interface ExecGroupSubAgentTaskResult {
threadId: string;
}
+/**
+ * @deprecated Use ExecSubAgentTaskParams instead
+ */
+export type ExecGroupSubAgentTaskParams = ExecSubAgentTaskParams;
+
+/**
+ * @deprecated Use ExecSubAgentTaskResult instead
+ */
+export type ExecGroupSubAgentTaskResult = ExecSubAgentTaskResult;
+
/**
* Current activity for real-time progress display
* Only returned when task is processing
diff --git a/src/server/routers/lambda/__tests__/aiAgent.execGroupSubAgentTask.test.ts b/src/server/routers/lambda/__tests__/aiAgent.execGroupSubAgentTask.test.ts
index 4e09be88ed9..63e70e937f9 100644
--- a/src/server/routers/lambda/__tests__/aiAgent.execGroupSubAgentTask.test.ts
+++ b/src/server/routers/lambda/__tests__/aiAgent.execGroupSubAgentTask.test.ts
@@ -18,7 +18,7 @@ vi.mock('@/database/core/db-adaptor', () => ({
const mockExecGroupSubAgentTask = vi.fn();
vi.mock('@/server/services/aiAgent', () => ({
AiAgentService: vi.fn().mockImplementation(() => ({
- execGroupSubAgentTask: mockExecGroupSubAgentTask,
+ execSubAgentTask: mockExecGroupSubAgentTask,
})),
}));
@@ -32,7 +32,7 @@ vi.mock('@/server/services/aiChat', () => ({
AiChatService: vi.fn().mockImplementation(() => ({})),
}));
-describe('aiAgentRouter.execGroupSubAgentTask', () => {
+describe('aiAgentRouter.execSubAgentTask', () => {
let serverDB: LobeChatDatabase;
let userId: string;
let testAgentId: string;
@@ -108,7 +108,7 @@ describe('aiAgentRouter.execGroupSubAgentTask', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- await caller.execGroupSubAgentTask({
+ await caller.execSubAgentTask({
agentId: testAgentId,
groupId: testGroupId,
instruction: 'Test instruction',
@@ -136,7 +136,7 @@ describe('aiAgentRouter.execGroupSubAgentTask', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const result = await caller.execGroupSubAgentTask({
+ const result = await caller.execSubAgentTask({
agentId: testAgentId,
groupId: testGroupId,
instruction: 'Test instruction',
@@ -162,7 +162,7 @@ describe('aiAgentRouter.execGroupSubAgentTask', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- await caller.execGroupSubAgentTask({
+ await caller.execSubAgentTask({
agentId: testAgentId,
groupId: testGroupId,
instruction: 'Test instruction',
@@ -184,7 +184,7 @@ describe('aiAgentRouter.execGroupSubAgentTask', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
await expect(
- caller.execGroupSubAgentTask({
+ caller.execSubAgentTask({
agentId: undefined,
groupId: testGroupId,
instruction: 'Test instruction',
@@ -198,7 +198,7 @@ describe('aiAgentRouter.execGroupSubAgentTask', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
await expect(
- caller.execGroupSubAgentTask({
+ caller.execSubAgentTask({
agentId: testAgentId,
groupId: undefined,
instruction: 'Test instruction',
@@ -212,7 +212,7 @@ describe('aiAgentRouter.execGroupSubAgentTask', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
await expect(
- caller.execGroupSubAgentTask({
+ caller.execSubAgentTask({
agentId: testAgentId,
groupId: testGroupId,
instruction: undefined,
@@ -226,7 +226,7 @@ describe('aiAgentRouter.execGroupSubAgentTask', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
await expect(
- caller.execGroupSubAgentTask({
+ caller.execSubAgentTask({
agentId: testAgentId,
groupId: testGroupId,
instruction: 'Test instruction',
@@ -240,7 +240,7 @@ describe('aiAgentRouter.execGroupSubAgentTask', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
await expect(
- caller.execGroupSubAgentTask({
+ caller.execSubAgentTask({
agentId: testAgentId,
groupId: testGroupId,
instruction: 'Test instruction',
@@ -262,7 +262,7 @@ describe('aiAgentRouter.execGroupSubAgentTask', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
await expect(
- caller.execGroupSubAgentTask({
+ caller.execSubAgentTask({
agentId: testAgentId,
groupId: testGroupId,
instruction: 'Test instruction',
@@ -278,7 +278,7 @@ describe('aiAgentRouter.execGroupSubAgentTask', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
await expect(
- caller.execGroupSubAgentTask({
+ caller.execSubAgentTask({
agentId: testAgentId,
groupId: testGroupId,
instruction: 'Test instruction',
diff --git a/src/server/routers/lambda/__tests__/aiAgent.getTaskStatus.test.ts b/src/server/routers/lambda/__tests__/aiAgent.getTaskStatus.test.ts
index 2aebdd73a72..78ab353f227 100644
--- a/src/server/routers/lambda/__tests__/aiAgent.getTaskStatus.test.ts
+++ b/src/server/routers/lambda/__tests__/aiAgent.getTaskStatus.test.ts
@@ -40,7 +40,7 @@ vi.mock('@/server/services/aiChat', () => ({
AiChatService: vi.fn().mockImplementation(() => ({})),
}));
-describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
+describe('aiAgentRouter.getSubAgentTaskStatus', () => {
let serverDB: LobeChatDatabase;
let userId: string;
let testAgentId: string;
@@ -126,7 +126,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
it('should return task status from Thread table', async () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const result = await caller.getGroupSubAgentTaskStatus({
+ const result = await caller.getSubAgentTaskStatus({
threadId: testThreadId,
});
@@ -140,7 +140,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
await expect(
- caller.getGroupSubAgentTaskStatus({
+ caller.getSubAgentTaskStatus({
threadId: 'non-existent-thread-id',
}),
).rejects.toThrow('Thread not found');
@@ -164,7 +164,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const result = await caller.getGroupSubAgentTaskStatus({
+ const result = await caller.getSubAgentTaskStatus({
threadId: testThreadId,
});
@@ -197,7 +197,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const result = await caller.getGroupSubAgentTaskStatus({
+ const result = await caller.getSubAgentTaskStatus({
threadId: testThreadId,
});
@@ -222,7 +222,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const result = await caller.getGroupSubAgentTaskStatus({
+ const result = await caller.getSubAgentTaskStatus({
threadId: testThreadId,
});
@@ -261,7 +261,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const result = await caller.getGroupSubAgentTaskStatus({
+ const result = await caller.getSubAgentTaskStatus({
threadId: testThreadId,
});
@@ -287,7 +287,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const result = await caller.getGroupSubAgentTaskStatus({
+ const result = await caller.getSubAgentTaskStatus({
threadId: testThreadId,
});
@@ -308,7 +308,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- await caller.getGroupSubAgentTaskStatus({
+ await caller.getSubAgentTaskStatus({
threadId: testThreadId,
});
@@ -332,7 +332,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const result = await caller.getGroupSubAgentTaskStatus({
+ const result = await caller.getSubAgentTaskStatus({
threadId: testThreadId,
});
@@ -367,7 +367,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const result = await caller.getGroupSubAgentTaskStatus({
+ const result = await caller.getSubAgentTaskStatus({
threadId: testThreadId,
});
@@ -391,7 +391,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
it('should require threadId', async () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- await expect(caller.getGroupSubAgentTaskStatus({} as any)).rejects.toThrow();
+ await expect(caller.getSubAgentTaskStatus({} as any)).rejects.toThrow();
});
});
@@ -443,7 +443,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const result = await caller.getGroupSubAgentTaskStatus({
+ const result = await caller.getSubAgentTaskStatus({
threadId: testThreadId,
});
@@ -476,7 +476,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const result = await caller.getGroupSubAgentTaskStatus({
+ const result = await caller.getSubAgentTaskStatus({
threadId: testThreadId,
});
@@ -499,7 +499,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
// Thread is in processing status (default from beforeEach)
const caller = aiAgentRouter.createCaller(createTestContext());
- const result = await caller.getGroupSubAgentTaskStatus({
+ const result = await caller.getSubAgentTaskStatus({
threadId: testThreadId,
});
@@ -522,7 +522,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const result = await caller.getGroupSubAgentTaskStatus({
+ const result = await caller.getSubAgentTaskStatus({
threadId: testThreadId,
});
@@ -548,7 +548,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const result = await caller.getGroupSubAgentTaskStatus({
+ const result = await caller.getSubAgentTaskStatus({
threadId: testThreadId,
});
@@ -605,7 +605,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const result = await caller.getGroupSubAgentTaskStatus({
+ const result = await caller.getSubAgentTaskStatus({
threadId: testThreadId,
});
@@ -631,7 +631,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const result = await caller.getGroupSubAgentTaskStatus({
+ const result = await caller.getSubAgentTaskStatus({
threadId: testThreadId,
});
@@ -656,7 +656,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const result = await caller.getGroupSubAgentTaskStatus({
+ const result = await caller.getSubAgentTaskStatus({
threadId: testThreadId,
});
@@ -688,7 +688,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const result = await caller.getGroupSubAgentTaskStatus({
+ const result = await caller.getSubAgentTaskStatus({
threadId: testThreadId,
});
@@ -699,7 +699,7 @@ describe('aiAgentRouter.getGroupSubAgentTaskStatus', () => {
it('should not return currentActivity when no messages in thread', async () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const result = await caller.getGroupSubAgentTaskStatus({
+ const result = await caller.getSubAgentTaskStatus({
threadId: testThreadId,
});
diff --git a/src/server/routers/lambda/__tests__/integration/aiAgent.task.integration.test.ts b/src/server/routers/lambda/__tests__/integration/aiAgent.task.integration.test.ts
index 5a6040c2a08..66a47265aca 100644
--- a/src/server/routers/lambda/__tests__/integration/aiAgent.task.integration.test.ts
+++ b/src/server/routers/lambda/__tests__/integration/aiAgent.task.integration.test.ts
@@ -20,7 +20,7 @@ const mockExecGroupSubAgentTask = vi.fn();
const mockInterruptTask = vi.fn();
vi.mock('@/server/services/aiAgent', () => ({
AiAgentService: vi.fn().mockImplementation(() => ({
- execGroupSubAgentTask: mockExecGroupSubAgentTask,
+ execSubAgentTask: mockExecGroupSubAgentTask,
interruptTask: mockInterruptTask,
})),
}));
@@ -146,7 +146,7 @@ describe('Agent Task Integration', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
// Create task
- const createResult = await caller.execGroupSubAgentTask({
+ const createResult = await caller.execSubAgentTask({
agentId: testAgentId,
groupId: testGroupId,
instruction: 'Test instruction',
@@ -169,7 +169,7 @@ describe('Agent Task Integration', () => {
metadata: {},
});
- const processingStatus = await caller.getGroupSubAgentTaskStatus({
+ const processingStatus = await caller.getSubAgentTaskStatus({
threadId,
});
@@ -204,7 +204,7 @@ describe('Agent Task Integration', () => {
metadata: { lastActiveAt: '2024-01-01T12:00:00Z' },
});
- const completedStatus = await caller.getGroupSubAgentTaskStatus({
+ const completedStatus = await caller.getSubAgentTaskStatus({
threadId,
});
@@ -254,7 +254,7 @@ describe('Agent Task Integration', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const createResult = await caller.execGroupSubAgentTask({
+ const createResult = await caller.execSubAgentTask({
agentId: testAgentId,
groupId: testGroupId,
instruction: 'Long running task',
@@ -283,7 +283,7 @@ describe('Agent Task Integration', () => {
.set({ status: ThreadStatus.Cancel })
.where(eq(threads.id, threadId));
- const statusResult = await caller.getGroupSubAgentTaskStatus({
+ const statusResult = await caller.getSubAgentTaskStatus({
threadId,
});
@@ -325,7 +325,7 @@ describe('Agent Task Integration', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const createResult = await caller.execGroupSubAgentTask({
+ const createResult = await caller.execSubAgentTask({
agentId: testAgentId,
groupId: testGroupId,
instruction: 'Failing task',
@@ -364,7 +364,7 @@ describe('Agent Task Integration', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const statusResult = await caller.getGroupSubAgentTaskStatus({
+ const statusResult = await caller.getSubAgentTaskStatus({
threadId,
});
@@ -435,14 +435,14 @@ describe('Agent Task Integration', () => {
// Execute tasks concurrently
const [task1, task2] = await Promise.all([
- caller.execGroupSubAgentTask({
+ caller.execSubAgentTask({
agentId: testAgentId,
groupId: testGroupId,
instruction: 'Task 1',
parentMessageId: 'parent-msg-5',
topicId: testTopicId,
}),
- caller.execGroupSubAgentTask({
+ caller.execSubAgentTask({
agentId: testAgent2Id,
groupId: testGroupId,
instruction: 'Task 2',
@@ -498,7 +498,7 @@ describe('Agent Task Integration', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
// Query by threadId
- const status = await caller.getGroupSubAgentTaskStatus({
+ const status = await caller.getSubAgentTaskStatus({
threadId,
});
@@ -539,7 +539,7 @@ describe('Agent Task Integration', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
// Query task status
- const status = await caller.getGroupSubAgentTaskStatus({ threadId });
+ const status = await caller.getSubAgentTaskStatus({ threadId });
// Verify all metadata fields are accessible
expect(status.status).toBe('completed');
@@ -595,7 +595,7 @@ describe('Agent Task Integration', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const status = await caller.getGroupSubAgentTaskStatus({ threadId });
+ const status = await caller.getSubAgentTaskStatus({ threadId });
// Should be processing with partial metadata
expect(status.status).toBe('processing');
@@ -640,7 +640,7 @@ describe('Agent Task Integration', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const status = await caller.getGroupSubAgentTaskStatus({ threadId });
+ const status = await caller.getSubAgentTaskStatus({ threadId });
// Should be failed with error in metadata
expect(status.status).toBe('failed');
@@ -680,7 +680,7 @@ describe('Agent Task Integration', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const status = await caller.getGroupSubAgentTaskStatus({ threadId });
+ const status = await caller.getSubAgentTaskStatus({ threadId });
expect(status.status).toBe('cancel');
expect(status.taskDetail?.status).toBe(ThreadStatus.Cancel);
@@ -727,7 +727,7 @@ describe('Agent Task Integration', () => {
const caller = aiAgentRouter.createCaller(createTestContext());
- const status = await caller.getGroupSubAgentTaskStatus({ threadId });
+ const status = await caller.getSubAgentTaskStatus({ threadId });
// Status should reflect InReview from Thread table
expect(status.taskDetail?.status).toBe(ThreadStatus.InReview);
diff --git a/src/server/routers/lambda/aiAgent.ts b/src/server/routers/lambda/aiAgent.ts
index 5c5fd09cac9..211e9269028 100644
--- a/src/server/routers/lambda/aiAgent.ts
+++ b/src/server/routers/lambda/aiAgent.ts
@@ -134,16 +134,17 @@ const ExecAgentsSchema = z.object({
});
/**
- * Schema for execGroupSubAgentTask - execute SubAgent task in Group chat
+ * Schema for execSubAgentTask - execute SubAgent task
+ * Supports both Group mode (with groupId) and Single Agent mode (without groupId)
*/
-const ExecGroupSubAgentTaskSchema = z.object({
+const ExecSubAgentTaskSchema = z.object({
/** The SubAgent ID to execute the task */
agentId: z.string(),
- /** The Group ID (required) */
- groupId: z.string(),
+ /** The Group ID (optional, only for Group mode) */
+ groupId: z.string().optional(),
/** Task instruction/prompt for the SubAgent */
instruction: z.string(),
- /** The parent message ID (Supervisor's tool call message) */
+ /** The parent message ID (Supervisor's tool call message or task message) */
parentMessageId: z.string(),
/** Timeout in milliseconds (optional) */
timeout: z.number().optional(),
@@ -420,20 +421,23 @@ export const aiAgentRouter = router({
}),
/**
- * Execute SubAgent task in Group chat
+ * Execute SubAgent task (supports both Group and Single Agent mode)
+ *
+ * This endpoint is called by Supervisor (Group mode) or Agent (Single mode)
+ * to delegate tasks to SubAgents. Each task runs in an isolated Thread context.
*
- * This endpoint is called by Supervisor to delegate tasks to SubAgents.
- * Each task runs in an isolated Thread context.
+ * - Group mode: pass groupId, Thread will be associated with the Group
+ * - Single Agent mode: omit groupId, Thread will only be associated with the Agent
*/
- execGroupSubAgentTask: aiAgentProcedure
- .input(ExecGroupSubAgentTaskSchema)
+ execSubAgentTask: aiAgentProcedure
+ .input(ExecSubAgentTaskSchema)
.mutation(async ({ input, ctx }) => {
const { agentId, groupId, instruction, parentMessageId, topicId, timeout } = input;
- log('execGroupSubAgentTask: agentId=%s, groupId=%s', agentId, groupId);
+ log('execSubAgentTask: agentId=%s, groupId=%s', agentId, groupId);
try {
- return await ctx.aiAgentService.execGroupSubAgentTask({
+ return await ctx.aiAgentService.execSubAgentTask({
agentId,
groupId,
instruction,
@@ -442,7 +446,7 @@ export const aiAgentRouter = router({
topicId,
});
} catch (error: any) {
- log('execGroupSubAgentTask failed: %O', error);
+ log('execSubAgentTask failed: %O', error);
if (error instanceof TRPCError) {
throw error;
@@ -463,12 +467,14 @@ export const aiAgentRouter = router({
* It queries from Thread table (PostgreSQL) for persistence,
* and supplements with real-time status from Redis if available.
*
+ * Works for both Group mode and Single Agent mode tasks.
+ *
* IMPORTANT: In QStash queue mode, step lifecycle callbacks cannot fire
* because each HTTP request creates a new AgentRuntimeService instance.
* As a workaround, this endpoint also updates Thread metadata from Redis
* when real-time status is available.
*/
- getGroupSubAgentTaskStatus: aiAgentProcedure
+ getSubAgentTaskStatus: aiAgentProcedure
.input(
z.object({
/** Thread ID */
@@ -478,7 +484,7 @@ export const aiAgentRouter = router({
.query(async ({ input, ctx }) => {
const { threadId } = input;
- log('getGroupSubAgentTaskStatus: threadId=%s', threadId);
+ log('getSubAgentTaskStatus: threadId=%s', threadId);
// 1. Find thread by threadId
const thread = await ctx.threadModel.findById(threadId);
@@ -561,7 +567,7 @@ export const aiAgentRouter = router({
status: ThreadStatus.Completed,
});
- log('getGroupSubAgentTaskStatus: marked thread %s as completed', threadId);
+ log('getSubAgentTaskStatus: marked thread %s as completed', threadId);
} else if (realtimeStatus.hasError || redisState.status === 'error') {
updatedMetadata.error = redisState.error;
updatedMetadata.completedAt = new Date().toISOString();
@@ -573,18 +579,18 @@ export const aiAgentRouter = router({
metadata: updatedMetadata,
status: ThreadStatus.Failed,
});
- log('getGroupSubAgentTaskStatus: marked thread %s as failed', threadId);
+ log('getSubAgentTaskStatus: marked thread %s as failed', threadId);
} else {
// Still processing, just update metrics
await ctx.threadModel.update(threadId, {
metadata: updatedMetadata,
});
- log('getGroupSubAgentTaskStatus: updated thread %s metadata', threadId);
+ log('getSubAgentTaskStatus: updated thread %s metadata', threadId);
}
} else {
// Redis status not available (expired), use Thread data only
log(
- 'getGroupSubAgentTaskStatus: Redis operation %s expired, using Thread data only',
+ 'getSubAgentTaskStatus: Redis operation %s expired, using Thread data only',
resolvedOperationId,
);
}
diff --git a/src/server/services/aiAgent/__tests__/execGroupSubAgentTask.test.ts b/src/server/services/aiAgent/__tests__/execGroupSubAgentTask.test.ts
index 47a9fd3d40d..7300ba70f53 100644
--- a/src/server/services/aiAgent/__tests__/execGroupSubAgentTask.test.ts
+++ b/src/server/services/aiAgent/__tests__/execGroupSubAgentTask.test.ts
@@ -53,7 +53,7 @@ vi.mock('@/server/services/agentRuntime', () => ({
})),
}));
-describe('AiAgentService.execGroupSubAgentTask', () => {
+describe('AiAgentService.execSubAgentTask', () => {
let service: AiAgentService;
const mockDb = {} as any;
const userId = 'test-user-id';
@@ -94,7 +94,7 @@ describe('AiAgentService.execGroupSubAgentTask', () => {
userMessageId: 'user-msg-1',
});
- await service.execGroupSubAgentTask({
+ await service.execSubAgentTask({
agentId: 'agent-1',
groupId: 'group-1',
instruction: 'Test instruction',
@@ -127,7 +127,7 @@ describe('AiAgentService.execGroupSubAgentTask', () => {
userMessageId: 'user-msg-1',
});
- await service.execGroupSubAgentTask({
+ await service.execSubAgentTask({
agentId: 'agent-1',
groupId: 'group-1',
instruction: 'Test instruction',
@@ -157,7 +157,7 @@ describe('AiAgentService.execGroupSubAgentTask', () => {
userMessageId: 'user-msg-1',
});
- await service.execGroupSubAgentTask({
+ await service.execSubAgentTask({
agentId: 'agent-1',
groupId: 'group-1',
instruction: 'Test instruction',
@@ -197,7 +197,7 @@ describe('AiAgentService.execGroupSubAgentTask', () => {
userMessageId: 'user-msg-1',
});
- await service.execGroupSubAgentTask({
+ await service.execSubAgentTask({
agentId: 'agent-1',
groupId: 'group-1',
instruction: 'Test instruction',
@@ -227,7 +227,7 @@ describe('AiAgentService.execGroupSubAgentTask', () => {
userMessageId: 'user-msg-1',
});
- const result = await service.execGroupSubAgentTask({
+ const result = await service.execSubAgentTask({
agentId: 'agent-1',
groupId: 'group-1',
instruction: 'Test instruction',
@@ -262,7 +262,7 @@ describe('AiAgentService.execGroupSubAgentTask', () => {
userMessageId: 'user-msg-1',
});
- await service.execGroupSubAgentTask({
+ await service.execSubAgentTask({
agentId: 'agent-1',
groupId: 'group-1',
instruction: 'Test instruction',
@@ -299,7 +299,7 @@ describe('AiAgentService.execGroupSubAgentTask', () => {
userMessageId: 'user-msg-1',
});
- await service.execGroupSubAgentTask({
+ await service.execSubAgentTask({
agentId: 'agent-1',
groupId: 'group-1',
instruction: 'Test instruction',
@@ -337,7 +337,7 @@ describe('AiAgentService.execGroupSubAgentTask', () => {
userMessageId: 'user-msg-1',
});
- const result = await service.execGroupSubAgentTask({
+ const result = await service.execSubAgentTask({
agentId: 'agent-1',
groupId: 'group-1',
instruction: 'Test instruction',
@@ -360,7 +360,7 @@ describe('AiAgentService.execGroupSubAgentTask', () => {
mockThreadModel.create.mockResolvedValue(null);
await expect(
- service.execGroupSubAgentTask({
+ service.execSubAgentTask({
agentId: 'agent-1',
groupId: 'group-1',
instruction: 'Test instruction',
@@ -374,7 +374,7 @@ describe('AiAgentService.execGroupSubAgentTask', () => {
mockThreadModel.create.mockRejectedValue(new Error('Database connection failed'));
await expect(
- service.execGroupSubAgentTask({
+ service.execSubAgentTask({
agentId: 'agent-1',
groupId: 'group-1',
instruction: 'Test instruction',
@@ -402,7 +402,7 @@ describe('AiAgentService.execGroupSubAgentTask', () => {
userMessageId: 'user-msg-1',
});
- await service.execGroupSubAgentTask({
+ await service.execSubAgentTask({
agentId: 'agent-1',
groupId: 'group-1',
instruction: 'Test instruction',
diff --git a/src/server/services/aiAgent/index.ts b/src/server/services/aiAgent/index.ts
index 2ab82116995..5aa9ff2e7ca 100644
--- a/src/server/services/aiAgent/index.ts
+++ b/src/server/services/aiAgent/index.ts
@@ -5,8 +5,8 @@ import type {
ExecAgentResult,
ExecGroupAgentParams,
ExecGroupAgentResult,
- ExecGroupSubAgentTaskParams,
- ExecGroupSubAgentTaskResult,
+ ExecSubAgentTaskParams,
+ ExecSubAgentTaskResult,
} from '@lobechat/types';
import { ThreadStatus, ThreadType } from '@lobechat/types';
import { nanoid } from '@lobechat/utils';
@@ -425,23 +425,24 @@ export class AiAgentService {
}
/**
- * Execute SubAgent task in Group chat
+ * Execute SubAgent task (supports both Group and Single Agent mode)
*
- * This method is called by Supervisor to delegate tasks to SubAgents.
- * Each task runs in an isolated Thread context.
+ * This method is called by Supervisor (Group mode) or Agent (Single mode)
+ * to delegate tasks to SubAgents. Each task runs in an isolated Thread context.
+ *
+ * - Group mode: pass groupId, Thread will be associated with the Group
+ * - Single Agent mode: omit groupId, Thread will only be associated with the Agent
*
* Flow:
* 1. Create Thread (type='isolation', status='processing')
* 2. Delegate to execAgent with threadId in appContext
* 3. Store operationId in Thread metadata
*/
- async execGroupSubAgentTask(
- params: ExecGroupSubAgentTaskParams,
- ): Promise<ExecGroupSubAgentTaskResult> {
+ async execSubAgentTask(params: ExecSubAgentTaskParams): Promise<ExecSubAgentTaskResult> {
const { groupId, topicId, parentMessageId, agentId, instruction } = params;
log(
- 'execGroupSubAgentTask: agentId=%s, groupId=%s, topicId=%s, instruction=%s',
+ 'execSubAgentTask: agentId=%s, groupId=%s, topicId=%s, instruction=%s',
agentId,
groupId,
topicId,
@@ -461,7 +462,7 @@ export class AiAgentService {
throw new Error('Failed to create thread for task execution');
}
- log('execGroupSubAgentTask: created thread %s', thread.id);
+ log('execSubAgentTask: created thread %s', thread.id);
// 2. Update Thread status to processing with startedAt timestamp
const startedAt = new Date().toISOString();
@@ -484,7 +485,7 @@ export class AiAgentService {
});
log(
- 'execGroupSubAgentTask: delegated to execAgent, operationId=%s, success=%s',
+ 'execSubAgentTask: delegated to execAgent, operationId=%s, success=%s',
result.operationId,
result.success,
);
@@ -556,12 +557,12 @@ export class AiAgentService {
},
});
log(
- 'execGroupSubAgentTask: updated thread %s metadata after step %d',
+ 'execSubAgentTask: updated thread %s metadata after step %d',
threadId,
state.stepCount,
);
} catch (error) {
- log('execGroupSubAgentTask: failed to update thread metadata: %O', error);
+ log('execSubAgentTask: failed to update thread metadata: %O', error);
}
},
@@ -604,7 +605,7 @@ export class AiAgentService {
await this.messageModel.update(sourceMessageId, {
content: lastAssistantMessage.content,
});
- log('execGroupSubAgentTask: updated task message %s with summary', sourceMessageId);
+ log('execSubAgentTask: updated task message %s with summary', sourceMessageId);
}
// Update Thread metadata
@@ -624,13 +625,13 @@ export class AiAgentService {
});
log(
- 'execGroupSubAgentTask: thread %s completed with status %s, reason: %s',
+ 'execSubAgentTask: thread %s completed with status %s, reason: %s',
threadId,
status,
reason,
);
} catch (error) {
- console.error('execGroupSubAgentTask: failed to update thread on completion: %O', error);
+ console.error('execSubAgentTask: failed to update thread on completion: %O', error);
}
},
};
diff --git a/src/services/aiAgent.ts b/src/services/aiAgent.ts
index e4726000d75..e76bc6cc512 100644
--- a/src/services/aiAgent.ts
+++ b/src/services/aiAgent.ts
@@ -15,16 +15,21 @@ export interface ExecAgentTaskParams {
slug?: string;
}
-export interface ExecGroupSubAgentTaskParams {
+/**
+ * Parameters for execSubAgentTask
+ * Supports both Group mode (with groupId) and Single Agent mode (without groupId)
+ */
+export interface ExecSubAgentTaskParams {
agentId: string;
- groupId: string;
+ /** Optional for Single Agent mode, required for Group mode */
+ groupId?: string;
instruction: string;
parentMessageId: string;
timeout?: number;
topicId: string;
}
-export interface GetGroupSubAgentTaskStatusParams {
+export interface GetSubAgentTaskStatusParams {
threadId: string;
}
@@ -42,17 +47,21 @@ class AiAgentService {
}
/**
- * Execute a sub-agent task in a group
+ * Execute a sub-agent task (supports both Group and Single Agent mode)
+ *
+ * - Group mode: pass groupId, Thread will be associated with the Group
+ * - Single Agent mode: omit groupId, Thread will only be associated with the Agent
*/
- async execGroupSubAgentTask(params: ExecGroupSubAgentTaskParams) {
- return await lambdaClient.aiAgent.execGroupSubAgentTask.mutate(params);
+ async execSubAgentTask(params: ExecSubAgentTaskParams) {
+ return await lambdaClient.aiAgent.execSubAgentTask.mutate(params);
}
/**
* Get SubAgent task status by threadId
+ * Works for both Group and Single Agent mode tasks
*/
- async getGroupSubAgentTaskStatus(params: GetGroupSubAgentTaskStatusParams) {
- return await lambdaClient.aiAgent.getGroupSubAgentTaskStatus.query(params);
+ async getSubAgentTaskStatus(params: GetSubAgentTaskStatusParams) {
+ return await lambdaClient.aiAgent.getSubAgentTaskStatus.query(params);
}
/**
diff --git a/src/store/chat/agents/GroupOrchestration/createGroupOrchestrationExecutors.ts b/src/store/chat/agents/GroupOrchestration/createGroupOrchestrationExecutors.ts
index 4de28305ec1..0d0279015fc 100644
--- a/src/store/chat/agents/GroupOrchestration/createGroupOrchestrationExecutors.ts
+++ b/src/store/chat/agents/GroupOrchestration/createGroupOrchestrationExecutors.ts
@@ -427,7 +427,7 @@ export const createGroupOrchestrationExecutors = (
log(`[${sessionLogId}] Created task message: ${taskMessageId}`);
// 2. Create task via backend API (backend creates thread with sourceMessageId)
- const createResult = await aiAgentService.execGroupSubAgentTask({
+ const createResult = await aiAgentService.execSubAgentTask({
agentId,
groupId,
instruction: task,
@@ -476,7 +476,7 @@ export const createGroupOrchestrationExecutors = (
};
}
- const status = await aiAgentService.getGroupSubAgentTaskStatus({
+ const status = await aiAgentService.getSubAgentTaskStatus({
threadId: createResult.threadId,
});
diff --git a/src/store/chat/slices/aiAgent/actions/groupOrchestration.ts b/src/store/chat/slices/aiAgent/actions/groupOrchestration.ts
index db4aa20cdc3..222b39218b4 100644
--- a/src/store/chat/slices/aiAgent/actions/groupOrchestration.ts
+++ b/src/store/chat/slices/aiAgent/actions/groupOrchestration.ts
@@ -387,7 +387,7 @@ export const groupOrchestrationSlice: StateCreator<
return useClientDataSWR<TaskStatusResult>(
enabled && threadId && messageId ? [SWR_USE_POLLING_TASK_STATUS, threadId] : null,
async ([, tid]: [string, string]) => {
- return aiAgentService.getGroupSubAgentTaskStatus({ threadId: tid });
+ return aiAgentService.getSubAgentTaskStatus({ threadId: tid });
},
{
revalidateOnFocus: false,