import {z} from 'zod' export type GetApiApiAgentNameArgs = {name: string}; export type GetApiApiAgentNameOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const agentDetails = z.object({name: z.string().optional(), url: z.string().optional(), description: z.string().optional(), capabilities: z.array(z.string()).optional(), sources: z.array(z.string()).optional(), raw: z.record(z.string(), z.unknown()).optional(), blocker: z.object({status: z.enum(["alive", "dead", "timeout", "error"]).optional(), status_code: z.number().int().optional(), speed_ms: z.number().optional(), eliminated: z.boolean().optional()}).optional(), scores: z.object({fame: z.object({}).optional(), usability: z.object({}).optional(), functionality: z.object({}).optional(), call_graph: z.number().optional(), overall: z.number().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 }; /** * Get one agent profile * * GET /api/agent/{name} */ export const getApiApiAgentName = async (args: GetApiApiAgentNameArgs, options?: GetApiApiAgentNameOptions) => { const {baseUrl = "https://agentsearch.luthersystems.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/agent/${encodeURIComponent(String(args.name))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(agentDetails, await res.json()) };