import {z} from 'zod' export type WebSearchTool = {type: 'web_search'}; export type ExpressAgentRunsRequest = {agent: 'express', input: string, stream?: boolean | undefined, tools?: Array | undefined}; export type ComputeTool = {type: 'compute'}; export type SearchEffort = 'auto' | 'low' | 'medium' | 'high'; export type ReportVerbosity = 'medium' | 'high'; export type ResearchTool = {type: 'research', search_effort: SearchEffort, report_verbosity: ReportVerbosity}; export type Verbosity = 'medium' | 'high'; export type AdvancedAgentRunsRequest = {agent: 'advanced', input: string, stream?: boolean | undefined, tools?: (Array) | undefined, verbosity?: Verbosity | undefined, workflow_config?: {max_workflow_steps: number} | undefined}; export type CustomAgentRunsRequest = {agent: string, input: string, stream?: boolean | undefined}; export type CreateApiV1AgentsRunsArgs = {body: ExpressAgentRunsRequest | AdvancedAgentRunsRequest | CustomAgentRunsRequest}; export type CreateApiV1AgentsRunsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const agentRunsResponseWebSearchResult = z.object({source_type: z.literal("web_search"), citation_uri: z.string(), provider: z.string().optional(), title: z.string(), snippet: z.string(), thumbnail_url: z.string().optional(), url: z.string()}); export const agentRunsResponseOutput = z.object({text: z.string().optional(), type: z.enum(["message.answer", "web_search.results"]), content: z.array(agentRunsResponseWebSearchResult).optional()}); export const agentRunsBatchResponse = z.object({agent: z.string(), mode: z.string().optional(), input: z.array(z.object({role: z.literal("user"), content: z.string()})), output: z.array(agentRunsResponseOutput)}); 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 }; /** * Run an Agent * * POST /v1/agents/runs */ export const createApiV1AgentsRuns = async (args: CreateApiV1AgentsRunsArgs, options?: CreateApiV1AgentsRunsOptions) => { const {baseUrl = "https://api.you.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/agents/runs${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(agentRunsBatchResponse, await res.json()) };