import {z} from 'zod' export type AgentType = 'assistant' | 'specialist' | 'orchestrator' | 'tool' | 'bot'; export type AgentCapability = {name: string, description: string, version?: string | undefined, inputSchema?: Record | undefined, outputSchema?: Record | undefined}; export type AgentRegisterRequest = {name: string, description: string, agentType?: AgentType | undefined, capabilities?: Array | undefined, endpoint?: string | undefined, metadata?: Record | undefined}; export type CreateApiAgentsRegisterArgs = {body: AgentRegisterRequest}; export type CreateApiAgentsRegisterOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const agentType = z.enum(["assistant", "specialist", "orchestrator", "tool", "bot"]); export const agentCapability = z.object({name: z.string(), description: z.string(), version: z.string().optional(), inputSchema: z.object({}).optional(), outputSchema: z.object({}).optional()}); export const agentStatus = z.enum(["online", "busy", "idle", "offline"]); export const agentCard = z.object({id: z.string().optional(), imUserId: z.string().optional(), name: z.string().optional(), description: z.string().optional(), agentType: agentType.optional(), capabilities: z.array(agentCapability).optional(), protocolVersion: z.string().optional(), endpoint: z.string().optional(), metadata: z.object({}).optional(), status: agentStatus.optional(), load: z.number().optional(), lastHeartbeat: z.string().optional()}); export const agentRegisterResponse = z.object({ok: z.boolean().optional(), data: z.object({agentId: z.string().optional(), userId: z.string().optional(), protocolVersion: z.string().optional(), card: agentCard.optional()}).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * 注册 Agent * * POST /agents/register */ export const createApiAgentsRegister = async (args: CreateApiAgentsRegisterArgs, options?: CreateApiAgentsRegisterOptions) => { const {baseUrl = "http://localhost:3200/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/agents/register${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(agentRegisterResponse, await res.json()) };