import {z} from 'zod' export type ProtosDeploymentV1CreateDeploymentRequest = {deploymentName?: string | undefined}; export type CreateApiApiAgentDeploymentsArgs = {body: ProtosDeploymentV1CreateDeploymentRequest}; export type CreateApiApiAgentDeploymentsOptions = {baseUrl: string, SemgrepJWT?: string | undefined, fetch?: typeof fetch | undefined}; export const protosDeploymentV1FeatureFlag = z.object({name: z.string().optional(), expiresAt: z.string().optional()}); export const protosDeploymentV1DeploymentFeatures = z.object({featureFlags: z.array(protosDeploymentV1FeatureFlag).optional(), hasAutofix: z.boolean().optional(), hasDeepsemgrep: z.boolean().optional(), hasTriageViaComment: z.boolean().optional(), hasDependencyQuery: z.boolean().optional(), defaultUserRole: z.enum(["ORG_ROLE_ADMIN", "ORG_ROLE_MEMBER", "ORG_ROLE_IMPERSONATOR", "ORG_ROLE_READONLY"]).optional(), editorSemgrepVersion: z.string().optional(), nosemgrepDisabled: z.boolean().optional()}); export const protosDeploymentV1Deployment = z.object({id: z.string().optional(), name: z.string().optional(), displayName: z.string().optional(), slug: z.string().optional(), sourceType: z.string().optional(), features: protosDeploymentV1DeploymentFeatures.optional(), organizationId: z.string().optional()}); export const protosDeploymentV1CreateDeploymentResponse = z.object({deployment: protosDeploymentV1Deployment.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 }; /** * Create Deployment * * POST /api/agent/deployments */ export const createApiApiAgentDeployments = async (args: CreateApiApiAgentDeploymentsArgs, options: CreateApiApiAgentDeploymentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, SemgrepJWT} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (SemgrepJWT !== undefined) headers.set('authorization', `Bearer ${SemgrepJWT}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/agent/deployments${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(protosDeploymentV1CreateDeploymentResponse, await res.json()) };