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.

✨ feat(channel): expose the failure type and device on public Channel jobs (#23057)

Arvin Xu99a34fb1a53e
diff --git a/packages/database/src/repositories/channel/__tests__/viewErrors.test.ts b/packages/database/src/repositories/channel/__tests__/viewErrors.test.ts
index 3c4a3481627..f234102c4d2 100644
--- a/packages/database/src/repositories/channel/__tests__/viewErrors.test.ts
+++ b/packages/database/src/repositories/channel/__tests__/viewErrors.test.ts
@@ -45,4 +45,38 @@ describe('Channel failure receipts', () => {
await db.update(channelJobs).set({ status: 'completed' }).where(eq(channelJobs.id, job.id));
expect((await asOwner.detail(channel.id)).jobs[0].errorMessage).toBeNull();
});
+
+ it('names the failure type and the device a dispatch could not reach', async () => {
+ const { asOwner, channel } = await fixture.createTeamChannel();
+ await fixture.addAgent(channel.id, 'Device Agent');
+ const message = await asOwner.send(channel.id, {
+ content: 'Reply',
+ requestKey: 'device-offline',
+ routing: 'fallback',
+ });
+ const [job] = await jobsOf(db, message.id);
+ const execution = new ChannelExecutionRepository(db);
+ await execution.claim(channel.id, job.id, lease);
+ const [claimed] = await db.select().from(channelJobs).where(eq(channelJobs.id, job.id));
+ await db
+ .update(agentOperations)
+ .set({
+ status: 'error',
+ error: {
+ deviceRoute: { deviceId: 'device-offline-1', userId: 'private-user' },
+ message: 'The device this agent runs on is offline',
+ type: 'DeviceGatewayNotConfigured',
+ },
+ })
+ .where(eq(agentOperations.id, claimed.operationId!));
+ await execution.fail(channel.id, job.id, { message: 'Agent execution ended without a reply' });
+
+ const [failed] = (await asOwner.detail(channel.id)).jobs;
+ expect(failed.errorType).toBe('DeviceGatewayNotConfigured');
+ expect(failed.deviceId).toBe('device-offline-1');
+ expect(JSON.stringify(failed)).not.toMatch(/private-user/);
+
+ await db.update(channelJobs).set({ status: 'completed' }).where(eq(channelJobs.id, job.id));
+ expect((await asOwner.detail(channel.id)).jobs[0].errorType).toBeNull();
+ });
});
diff --git a/packages/database/src/repositories/channel/view.ts b/packages/database/src/repositories/channel/view.ts
index 221bf7d7261..7d3d081a142 100644
--- a/packages/database/src/repositories/channel/view.ts
+++ b/packages/database/src/repositories/channel/view.ts
@@ -238,6 +238,16 @@ export const readChannelView = async (
// Only a bounded diagnostic message crosses the public boundary, never the raw error.
errorMessage: sql<string | null>`CASE WHEN ${channelJobs.status} = 'failed'
THEN left(${agentOperations.error}->>'message', 2000) ELSE NULL END`,
+ // The error's category, so a client can explain a known failure in the viewer's language.
+ errorType: sql<string | null>`CASE WHEN ${channelJobs.status} = 'failed'
+ THEN ${agentOperations.error}->>'type' ELSE NULL END`,
+ // The device the reply runs on, waits for, or failed to reach: the claimed device, the
+ // one a failed dispatch was routed to, or the Agent's own bound device.
+ deviceId: sql<string | null>`COALESCE(
+ ${channelJobs.deviceId},
+ ${agentOperations.error}->'deviceRoute'->>'deviceId',
+ ${channelJobs.executionConfig}->'agencyConfig'->>'boundDeviceId'
+ )`,
writerHeld: channelJobs.writerHeld,
worktreeBranch: sql<string | null>`${channelJobs.executionConfig}->>'worktreeBranch'`,
operationStatus: agentOperations.status,
diff --git a/packages/types/src/channel.ts b/packages/types/src/channel.ts
index 443fc8fbe1d..e53cce8ca60 100644
--- a/packages/types/src/channel.ts
+++ b/packages/types/src/channel.ts
@@ -527,8 +527,15 @@ export interface ChannelPublicJob {
blockedCode: ChannelJobBlockCode | null;
/** Server-computed for the current member: its requester or an admin. */
canStop: boolean;
+ /**
+ * The device the reply runs on, waits for or failed to reach, when known: the claimed
+ * device, a failed dispatch's route, or the Agent's own bound device.
+ */
+ deviceId?: string | null;
/** Failure diagnostic only. */
errorMessage: string | null;
+ /** Category of a failure (a `ChatErrorType`), for a localized explanation. */
+ errorType?: string | null;
id: string;
memberId: string;
messageId: string;