diff --git a/__tests__/services/registry-broker-agent-network.test.ts b/__tests__/services/registry-broker-agent-network.test.ts index 4646854..90f6818 100644 --- a/__tests__/services/registry-broker-agent-network.test.ts +++ b/__tests__/services/registry-broker-agent-network.test.ts @@ -11,6 +11,7 @@ const MESSAGE_ID = '11111111-1111-4111-8111-111111111111'; const CONVERSATION_ID = '22222222-2222-4222-8222-222222222222'; const LEASE_ID = '33333333-3333-4333-8333-333333333333'; const GRANT_ID = '44444444-4444-4444-8444-444444444444'; +const SUBSCRIPTION_ID = '66666666-6666-4666-8666-666666666666'; const runtimeView = { runtimeId: RUNTIME_ID, @@ -33,6 +34,7 @@ const runtimeView = { const messageView = (overrides: Record = {}) => ({ messageId: MESSAGE_ID, conversationId: CONVERSATION_ID, + schemaVersion: 'hol-agent-message/1' as const, kind: 'request', senderUaid: 'uaid:aid:sender', recipientUaid: 'uaid:aid:recipient', @@ -184,6 +186,130 @@ describe('RegistryBrokerClient agentNetwork', () => { `https://broker.test/api/v1/agent-connections/${GRANT_ID}`, ); }); + + it('reads the owner inbox without a grant token', async () => { + fetchImplementation.mockResolvedValueOnce( + createResponse({ + body: { + items: [ + { + messageId: MESSAGE_ID, + conversationId: CONVERSATION_ID, + kind: 'request', + senderUaid: 'uaid:aid:sender', + content: { type: 'text', text: 'nonce-123 compute 37 + 58' }, + requestState: 'completed', + deliveryState: 'acknowledged', + acknowledged: true, + leaseId: null, + fencingToken: null, + createdAt: '2026-10-01T00:00:00.000Z', + expiresAt: '2026-10-01T01:00:00.000Z', + }, + ], + nextCursor: 'next-1', + }, + }), + ); + const inbox = await client.agentNetwork.listRuntimeInbox(RUNTIME_ID, { + cursor: 'c0', + limit: 5, + includeAcknowledged: true, + }); + expect(inbox.items).toHaveLength(1); + expect(inbox.nextCursor).toBe('next-1'); + const req = lastRequest(); + expect(req.url).toBe( + `https://broker.test/api/v1/agent-runtimes/${RUNTIME_ID}/inbox?cursor=c0&limit=5&includeAcknowledged=true`, + ); + expect(req.headers.get('x-api-key')).toBe('owner-key'); + }); + + it('reads an owner-scoped conversation', async () => { + fetchImplementation.mockResolvedValueOnce( + createResponse({ + body: { + conversationId: CONVERSATION_ID, + state: 'active', + participants: [ + { uaid: 'uaid:aid:recipient' }, + { uaid: 'uaid:aid:sender' }, + ], + messages: [messageView()], + nextCursor: null, + }, + }), + ); + const view = await client.agentNetwork.getRuntimeConversation( + RUNTIME_ID, + CONVERSATION_ID, + ); + expect(view.messages).toHaveLength(1); + const req = lastRequest(); + expect(req.url).toBe( + `https://broker.test/api/v1/agent-runtimes/${RUNTIME_ID}/conversations/${CONVERSATION_ID}`, + ); + expect(req.headers.get('x-api-key')).toBe('owner-key'); + }); + + it('creates, lists, and revokes push subscriptions', async () => { + const subscription = { + subscriptionId: SUBSCRIPTION_ID, + runtimeId: RUNTIME_ID, + eventType: 'notify_request', + callbackUrl: 'https://hooks.example.net/agent', + state: 'active', + expiresAt: null, + createdAt: '2026-10-01T00:00:00.000Z', + }; + fetchImplementation + .mockResolvedValueOnce(createResponse({ body: { subscription } })) + .mockResolvedValueOnce( + createResponse({ body: { subscriptions: [subscription] } }), + ) + .mockResolvedValueOnce(createResponse({ status: 204 })); + + const created = await client.agentNetwork.createAgentSubscription( + RUNTIME_ID, + { + eventType: 'notify_request', + callbackUrl: 'https://hooks.example.net/agent', + signingSecret: 'whsec_test_secret', + }, + ); + expect(created.subscriptionId).toBe(SUBSCRIPTION_ID); + let req = lastRequest(); + expect(req.url).toBe( + `https://broker.test/api/v1/agent-runtimes/${RUNTIME_ID}/subscriptions`, + ); + expect(req.method).toBe('POST'); + expect(req.body).toMatchObject({ + callbackUrl: 'https://hooks.example.net/agent', + }); + // the client must be able to send the secret — server never returns it + expect(req.body).toHaveProperty('signingSecret', 'whsec_test_secret'); + expect(JSON.stringify(created)).not.toContain('whsec_test_secret'); + + const listed = + await client.agentNetwork.listAgentSubscriptions(RUNTIME_ID); + expect(listed).toHaveLength(1); + expect(JSON.stringify(listed)).not.toContain('whsec_test_secret'); + req = lastRequest(); + expect(req.method).toBe('GET'); + expect(req.url).toBe( + `https://broker.test/api/v1/agent-runtimes/${RUNTIME_ID}/subscriptions`, + ); + + await client.agentNetwork.revokeAgentSubscription( + RUNTIME_ID, + SUBSCRIPTION_ID, + ); + req = lastRequest(); + expect(req.method).toBe('DELETE'); + expect(req.url).toBe( + `https://broker.test/api/v1/agent-runtimes/${RUNTIME_ID}/subscriptions/${SUBSCRIPTION_ID}`, + ); + }); }); describe('bot plane', () => { diff --git a/src/services/registry-broker/client.ts b/src/services/registry-broker/client.ts index 41ce846..aa4b9a4 100644 --- a/src/services/registry-broker/client.ts +++ b/src/services/registry-broker/client.ts @@ -30,7 +30,11 @@ export { agentConversationViewSchema, agentReplyResultSchema, agentRuntimeRegistrationResultSchema, + agentRegistrationResultStateSchema, agentProbeResultSchema, + agentSubscriptionEventTypeSchema, + agentSubscriptionViewSchema, + agentSubscriptionListSchema, type RegistryBrokerAgentNetworkApi, type AgentNetworkProvider, type AgentRequestState, @@ -55,6 +59,9 @@ export { type AgentLeaseRef, type ReplyToAgentMessageInput, type AgentInboxQuery, + type AgentSubscriptionEventType, + type AgentSubscriptionView, + type CreateAgentSubscriptionInput, type WaitForAgentReplyOptions, } from './client/agent-network'; export { diff --git a/src/services/registry-broker/client/agent-network.ts b/src/services/registry-broker/client/agent-network.ts index fd97815..1552806 100644 --- a/src/services/registry-broker/client/agent-network.ts +++ b/src/services/registry-broker/client/agent-network.ts @@ -21,10 +21,11 @@ import type { JsonValue } from '../types'; export const agentNetworkProviderSchema = z.enum([ 'openclaw', - 'dot', 'grok', + 'dot', 'muse', - 'generic', + 'api', + 'other', ]); export type AgentNetworkProvider = z.infer; @@ -32,22 +33,34 @@ export const agentConnectionStateSchema = z.enum([ 'unavailable', 'pending_pairing', 'interactive_verified', - 'receive_unverified', - 'receive_verified', - 'paused', - 'revoked', + 'polling_verified', + 'push_verified', 'needs_reconnect', + 'paused', + 'degraded', ]); export const agentRegistrationStateSchema = z.enum([ 'unregistered', + 'quote_issued', 'pending', 'registered', - 'linked', 'failed', ]); -export const agentReceiveModeSchema = z.enum(['none', 'poll', 'push', 'events']); +/** + * The registration *result* enum is distinct from the persisted lifecycle + * state — a register call resolves to one of these, including `linked` for + * owners attaching an existing UAID. + */ +export const agentRegistrationResultStateSchema = z.enum([ + 'registered', + 'pending', + 'linked', + 'unregistered', +]); + +export const agentReceiveModeSchema = z.enum(['none', 'poll', 'push']); export const agentRequestStateSchema = z.enum([ 'accepted', @@ -122,7 +135,7 @@ export type AgentPairingComplete = z.infer; export const agentMessageViewSchema = z.object({ messageId: z.string().uuid(), conversationId: z.string().uuid(), - schemaVersion: z.number().optional(), + schemaVersion: z.literal('hol-agent-message/1'), kind: agentMessageKindSchema, senderUaid: z.string(), recipientUaid: z.string(), @@ -198,7 +211,7 @@ export type AgentReplyResult = z.infer; export const agentRuntimeRegistrationResultSchema = z.object({ runtime: agentRuntimeViewSchema, - registration: agentRegistrationStateSchema, + registration: agentRegistrationResultStateSchema, }); export type AgentRuntimeRegistrationResult = z.infer< typeof agentRuntimeRegistrationResultSchema @@ -209,6 +222,34 @@ export const agentProbeResultSchema = z.object({ issuedAt: isoDate, }); +export const agentSubscriptionEventTypeSchema = z.enum([ + 'notify_request', + 'notify_event', + 'notify_response', +]); +export type AgentSubscriptionEventType = z.infer< + typeof agentSubscriptionEventTypeSchema +>; + +export const agentSubscriptionViewSchema = z.object({ + subscriptionId: z.string().uuid(), + runtimeId: z.string().uuid(), + eventType: agentSubscriptionEventTypeSchema, + callbackUrl: z.string().nullable(), + state: z.string(), + expiresAt: isoDate.nullable(), + createdAt: isoDate, +}); +export type AgentSubscriptionView = z.infer; + +export const agentSubscriptionListSchema = z.object({ + subscriptions: z.array(agentSubscriptionViewSchema), +}); + +export const agentSubscriptionCreatedSchema = z.object({ + subscription: agentSubscriptionViewSchema, +}); + // --------------------------------------------------------------------------- // Inputs // --------------------------------------------------------------------------- @@ -219,8 +260,7 @@ export interface RegisterAgentRuntimeInput { description?: string; capabilities?: string[]; linkExistingUaid?: string; - preferredReceiveMode?: 'poll' | 'push' | 'events'; - callbackUrl?: string; + preferredReceiveMode?: 'poll' | 'push'; } export interface UpdateAgentRuntimePolicyInput { @@ -228,7 +268,7 @@ export interface UpdateAgentRuntimePolicyInput { allowedPeerUaids?: string[]; maxOutboundPerMinute?: number; maxPendingMessages?: number; - receiveMode?: 'poll' | 'push' | 'events' | 'none'; + receiveMode?: 'poll' | 'push'; } export interface SendAgentMessageInput { @@ -249,7 +289,7 @@ export interface AgentLeaseRef { export interface ReplyToAgentMessageInput extends AgentLeaseRef { content: { type: 'text'; text: string }; idempotencyKey: string; - outcome: 'answered' | 'failed' | 'refused' | 'canceled'; + outcome?: 'answered' | 'partial' | 'refused'; } export interface AgentInboxQuery { @@ -258,6 +298,14 @@ export interface AgentInboxQuery { includeAcknowledged?: boolean; } +export interface CreateAgentSubscriptionInput { + eventType?: AgentSubscriptionEventType; + /** Public HTTPS webhook target — validated for safety server-side. */ + callbackUrl: string; + signingSecret?: string; + expiresAt?: string; +} + export interface WaitForAgentReplyOptions { /** Absolute bound — never resends the request. Default 60s. */ timeoutMs?: number; @@ -297,6 +345,28 @@ export interface RegistryBrokerAgentNetworkApi { startAgentConnection(runtimeId: string): Promise; completeAgentPairing(pairingCode: string): Promise; revokeAgentConnection(grantId: string): Promise; + /** Owner-scoped inbox read — no bot grant token required. */ + listRuntimeInbox( + runtimeId: string, + query?: AgentInboxQuery, + ): Promise; + /** Owner-scoped conversation read scoped to an owned runtime. */ + getRuntimeConversation( + runtimeId: string, + conversationId: string, + query?: { cursor?: string; limit?: number }, + ): Promise; + createAgentSubscription( + runtimeId: string, + input: CreateAgentSubscriptionInput, + ): Promise; + listAgentSubscriptions( + runtimeId: string, + ): Promise; + revokeAgentSubscription( + runtimeId: string, + subscriptionId: string, + ): Promise; // --- bot plane (grant bearer token) --- agentMe(token: string): Promise; @@ -359,7 +429,9 @@ const encodePath = (value: string): string => encodeURIComponent(value); const qs = (params: Record) => { const query = new URLSearchParams(); for (const [k, v] of Object.entries(params)) { - if (v !== undefined) query.set(k, String(v)); + if (v !== undefined) { + query.set(k, String(v)); + } } const s = query.toString(); return s ? `?${s}` : ''; @@ -455,6 +527,53 @@ export function createAgentNetworkApi( }); }, + listRuntimeInbox: (runtimeId, query) => + json( + `/agent-runtimes/${encodePath(runtimeId)}/inbox${qs({ + cursor: query?.cursor, + limit: query?.limit, + includeAcknowledged: query?.includeAcknowledged, + })}`, + agentInboxListSchema, + {}, + ), + + getRuntimeConversation: (runtimeId, conversationId, query) => + json( + `/agent-runtimes/${encodePath(runtimeId)}/conversations/${encodePath( + conversationId, + )}${qs({ cursor: query?.cursor, limit: query?.limit })}`, + agentConversationViewSchema, + {}, + ), + + createAgentSubscription: async (runtimeId, input) => + ( + await json( + `/agent-runtimes/${encodePath(runtimeId)}/subscriptions`, + agentSubscriptionCreatedSchema, + { method: 'POST', body: input }, + ) + ).subscription, + + listAgentSubscriptions: async (runtimeId) => + ( + await json( + `/agent-runtimes/${encodePath(runtimeId)}/subscriptions`, + agentSubscriptionListSchema, + {}, + ) + ).subscriptions, + + revokeAgentSubscription: async (runtimeId, subscriptionId) => { + await client.request( + `/agent-runtimes/${encodePath(runtimeId)}/subscriptions/${encodePath( + subscriptionId, + )}`, + { method: 'DELETE' }, + ); + }, + agentMe: async (token) => { const raw = await client.requestJson( '/agent-runtimes/me',