import {z} from 'zod' export type CreateAgentConnectionRequest = {oauth_token_id: string}; export type CreateApiV2AuthAgentsAgentIdConnectionsArgs = {agent_id: string, body: CreateAgentConnectionRequest}; export type CreateApiV2AuthAgentsAgentIdConnectionsOptions = {baseUrl: string, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const agentConnectionResponse = z.object({id: z.string(), agent_id: z.string(), oauth_token_id: z.string(), provider_id: z.string(), provider_account_label: z.string().nullable(), scopes: z.array(z.string()), expires_at: z.string().nullable(), created_by: z.string(), created_at: z.string()}); 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 }; /** * Create Connection * * POST /v2/auth/agents/{agent_id}/connections * * Also accepts auth: Tenant ID, Bearer Auth */ export const createApiV2AuthAgentsAgentIdConnections = async (args: CreateApiV2AuthAgentsAgentIdConnectionsArgs, options: CreateApiV2AuthAgentsAgentIdConnectionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKey !== undefined) headers.set("X-API-Key", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/auth/agents/${encodeURIComponent(String(args.agent_id))}/connections${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(agentConnectionResponse, await res.json()) };