import { z } from 'zod';
import type { RegistryApiClient } from '../client/api.js';
export const sendInputSchema = z.object({
to: z.string().describe('Destination agent origin/domain or agent ID'),
subject: z.string().optional().describe('Message subject'),
body: z.string().describe('Message body content'),
threadId: z.string().uuid().optional().describe('Thread ID to continue an existing conversation'),
metadata: z.record(z.unknown()).optional().describe('Additional metadata to attach to the message'),
});
export type SendInput = z.infer<typeof sendInputSchema>;
export interface SendResult {
success: boolean;
messageId?: string;
threadId?: string;
error?: string;
}
/**
* Send a message to another agent.
*
* The message will be signed with this agent's private key and
* delivered to the recipient's inbox via the registry.
*/
export async function send(
input: SendInput,
client: RegistryApiClient
): Promise<SendResult> {
try {
// Determine if 'to' is a UUID or domain
const isUuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(input.to);
const response = await client.sendMessage({
to_origin: isUuid ? '' : input.to,
to_agent_id: isUuid ? input.to : undefined,
subject: input.subject,
body: input.body,
thread_id: input.threadId,
metadata: input.metadata,
});
return {
success: true,
messageId: response.message_id,
threadId: response.thread_id,
};
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Failed to send message',
};
}
}
export const sendTool = {
name: 'agents_registry_send',
description: 'Send a signed message to another agent via the registry',
inputSchema: {
type: 'object' as const,
properties: {
to: {
type: 'string',
description: 'Destination agent - can be a domain (e.g., "agent.example.com") or agent UUID',
},
subject: {
type: 'string',
description: 'Message subject (optional)',
},
body: {
type: 'string',
description: 'Message body content',
},
threadId: {
type: 'string',
description: 'Thread ID to continue an existing conversation (optional)',
},
metadata: {
type: 'object',
description: 'Additional metadata to attach to the message (optional)',
},
},
required: ['to', 'body'],
},
};