import {z} from 'zod' export type Input = {input_parameters: {[key: string]: unknown}, invocation_mode: 'regular' | 'conversation-simulation', nonsensitive_user_variables: {[key: string]: string}, sensitive_user_variables: {[key: string]: string}}; export type SrcAppEndpointsToolTestToolRequest = {inputs: Array, commit_branch: string, project_path: string, required_envvars: {[key: string]: string}, required_secret_envvars: {[key: string]: string}}; export type CreateApiV1OrganizationToolTestArgs = {organization: string, 'x-mongo-cluster-name'?: (string | null) | undefined, 'Sec-WebSocket-Protocol'?: Array | undefined, body: SrcAppEndpointsToolTestToolRequest}; export type CreateApiV1OrganizationToolTestOptions = {baseUrl?: string | undefined, BearerAuthorization?: string | undefined, BearerAuthorizationOrganization?: string | undefined, Basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const succeededInvocationResult = z.object({succeeded: z.literal(true).optional(), output: z.string(), duration_ms: z.number().int()}); export const failedInvocationResult = z.object({succeeded: z.literal(false).optional(), exception_message: z.string(), exception_type: z.string(), exception_stack_trace: z.array(z.string()), duration_ms: z.number().int()}); export const invocationResult = z.discriminatedUnion("succeeded", [succeededInvocationResult, failedInvocationResult]); export const srcAppEndpointsToolTestToolResponse = z.object({invocation_results: z.array(invocationResult)}); 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 }; /** * Test a tool * * POST /v1/{organization}/tool/test */ export const createApiV1OrganizationToolTest = async (args: CreateApiV1OrganizationToolTestArgs, options?: CreateApiV1OrganizationToolTestOptions) => { const {baseUrl = "https://api.amigo.ai", fetch: fetchFn = fetch, BearerAuthorization, BearerAuthorizationOrganization, Basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-mongo-cluster-name'] != null) headers.set("x-mongo-cluster-name", String(args['x-mongo-cluster-name'])) if (args['Sec-WebSocket-Protocol'] != null) headers.set("Sec-WebSocket-Protocol", args['Sec-WebSocket-Protocol'].join(',')) if (BearerAuthorization !== undefined) headers.set('authorization', `Bearer ${BearerAuthorization}`) if (BearerAuthorizationOrganization !== undefined) headers.set("X-ORG-ID", BearerAuthorizationOrganization) if (Basic !== undefined) headers.set('authorization', `Basic ${btoa(`${Basic.username}:${Basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/${encodeURIComponent(String(args.organization))}/tool/test${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(srcAppEndpointsToolTestToolResponse, await res.json()) };