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(agent-mail): send the recipient as a list so outbound mail stops 422-ing (#22780)

Arvin Xude0d8d4295a8
diff --git a/apps/server/src/services/agentIdentity/__tests__/agentMailProvider.test.ts b/apps/server/src/services/agentIdentity/__tests__/agentMailProvider.test.ts
index 15157effd1b..0648c4c76cf 100644
--- a/apps/server/src/services/agentIdentity/__tests__/agentMailProvider.test.ts
+++ b/apps/server/src/services/agentIdentity/__tests__/agentMailProvider.test.ts
@@ -558,7 +558,7 @@ describe('agent-mail provider β€” outbound', () => {
html: '<p>hi back</p>',
subject: 'Re: Hello',
text: 'hi back',
});
});
diff --git a/packages/agent-address-mail/src/api.ts b/packages/agent-address-mail/src/api.ts
index b56f2c816a7..c1b8f1195c9 100644
--- a/packages/agent-address-mail/src/api.ts
+++ b/packages/agent-address-mail/src/api.ts
@@ -131,6 +131,11 @@ export class LobeMailApiClient {
try {
const parsed = JSON.parse(body) as EmailApiError;
detail = parsed.error?.message || parsed.error?.code || body;
+ // A 422 only says "Invalid request"; the issues name the field at fault.
+ const issues = parsed.error?.issues
+ ?.map((issue) => (issue.path ? `${issue.path}: ${issue.message}` : issue.message))
+ .filter(Boolean);
+ if (issues?.length) detail = `${detail} (${issues.join('; ')})`;
} catch {
// keep the raw body
}
@@ -256,7 +261,8 @@ export class LobeMailApiClient {
input: { html?: string; subject: string; text?: string; to: string },
): Promise<EmailMessageDetail> =>
this.request<EmailMessageDetail>(`/v1/inboxes/${encodeURIComponent(inboxId)}/messages`, {
- body: JSON.stringify(input),
+ // The send route takes a recipient list and rejects a bare address with a 422.
+ body: JSON.stringify({ ...input, to: [input.to] }),
method: 'POST',
});
diff --git a/packages/agent-address-mail/src/types.ts b/packages/agent-address-mail/src/types.ts
index 0373c795bb5..402b38b9311 100644
--- a/packages/agent-address-mail/src/types.ts
+++ b/packages/agent-address-mail/src/types.ts
@@ -133,5 +133,10 @@ export interface EmailAdapterConfig {
}
export interface EmailApiError {
- error?: { code?: string; message?: string };
+ error?: {
+ code?: string;
+ /** Per-field zod failures on a `validation_error` (422). */
+ issues?: { message?: string; path?: string }[];
+ message?: string;
+ };
}
diff --git a/packages/agent-address-mail/src/units.test.ts b/packages/agent-address-mail/src/units.test.ts
index 8a534751381..45e214c9ef1 100644
--- a/packages/agent-address-mail/src/units.test.ts
+++ b/packages/agent-address-mail/src/units.test.ts
@@ -55,6 +55,39 @@ describe('LobeMailApiClient', () => {
await expect(client.getInbox('inb_missing')).rejects.toThrow(/404 β€” Inbox not found/);
});
+ it('names the failing fields of a validation error', async () => {
+ const fetchImpl = vi.fn(async () =>
+ Response.json(
+ {
+ error: {
+ code: 'validation_error',
+ issues: [{ message: 'Invalid input: expected array, received string', path: 'to' }],
+ message: 'Invalid request',
+ },
+ },
+ { status: 422 },
+ ),
+ ) as unknown as typeof fetch;
+
+ const client = new LobeMailApiClient({ apiKey: 'am_x', fetchImpl });
+ await expect(client.getInbox('inb_1')).rejects.toThrow(
+ /422 β€” Invalid request \(to: Invalid input: expected array, received string\)/,
+ );
+ });
+
+ it('sends the recipient as the list the send route requires', async () => {
+ let body = '';
+ const fetchImpl = vi.fn(async (_input: any, init: any) => {
+ body = init?.body;
+ return Response.json({ id: 'msg_1' }, { status: 201 });
+ }) as unknown as typeof fetch;
+
+ const client = new LobeMailApiClient({ apiKey: 'am_x', fetchImpl });
+ await client.sendMessage('inb_1', { subject: 'Hi', text: 'Hello', to: '[email protected]' });
+
+ expect(JSON.parse(body)).toEqual({ subject: 'Hi', text: 'Hello', to: ['[email protected]'] });
+ });
+
it('creates an inbox and registers a webhook with the bearer key', async () => {
const calls: Array<{ body?: string; url: string }> = [];
const fetchImpl = vi.fn(async (input: any, init: any) => {