import {z} from 'zod' export type ProtosAiV1AiMemorySource = {note?: string | undefined, createdBy?: string | undefined, createdAt?: string | undefined, sampleIssueId?: string | undefined, prCommentUrl?: string | undefined, reviewCommentId?: string | undefined}; export type ProtosAiV1AiMemorySuggestionSources = {sources?: Array | undefined}; export type ProtosAiV1MemoryPreanalysisCounts = {newlyTruePositive?: string | undefined, newlyFalsePositive?: string | undefined, remainsTruePositive?: string | undefined, remainsFalsePositive?: string | undefined}; export type ProtosAiV1Memory = {id?: string | undefined, content?: string | undefined, ruleId?: string | undefined, ruleName?: string | undefined, repositoryId?: string | undefined, repositoryName?: string | undefined, createdAt?: string | undefined, createdBy?: string | undefined, context?: Array | undefined, stage?: ('AI_MEMORY_STAGE_LIVE' | 'AI_MEMORY_STAGE_SUGGESTED' | 'AI_MEMORY_STAGE_DELETED') | undefined, pathPrefixes?: Array | undefined, suggestionSources?: ProtosAiV1AiMemorySuggestionSources | undefined, preanalysisCounts?: ProtosAiV1MemoryPreanalysisCounts | undefined, vulnerabilityClass?: string | undefined, projectTagId?: number | undefined}; export type ProtosAiV1CreateMemoryRequest = {memory?: ProtosAiV1Memory | undefined, deploymentId?: string | undefined}; export type CreateApiApiAiDeploymentsDeploymentIdMemoriesArgs = {deploymentId: string, body: ProtosAiV1CreateMemoryRequest}; export type CreateApiApiAiDeploymentsDeploymentIdMemoriesOptions = {baseUrl: string, SemgrepWebToken?: string | undefined, fetch?: typeof fetch | undefined}; export const protosAiV1AiMemorySource = z.object({note: z.string().optional(), createdBy: z.string().optional(), createdAt: z.string().optional(), sampleIssueId: z.string().optional(), prCommentUrl: z.string().optional(), reviewCommentId: z.string().optional()}); export const protosAiV1AiMemorySuggestionSources = z.object({sources: z.array(protosAiV1AiMemorySource).optional()}); export const protosAiV1MemoryPreanalysisCounts = z.object({newlyTruePositive: z.string().optional(), newlyFalsePositive: z.string().optional(), remainsTruePositive: z.string().optional(), remainsFalsePositive: z.string().optional()}); export const protosAiV1Memory = z.object({id: z.string().optional(), content: z.string().optional(), ruleId: z.string().optional(), ruleName: z.string().optional(), repositoryId: z.string().optional(), repositoryName: z.string().optional(), createdAt: z.string().optional(), createdBy: z.string().optional(), context: z.array(z.string()).optional(), stage: z.enum(["AI_MEMORY_STAGE_LIVE", "AI_MEMORY_STAGE_SUGGESTED", "AI_MEMORY_STAGE_DELETED"]).optional(), pathPrefixes: z.array(z.string()).optional(), suggestionSources: protosAiV1AiMemorySuggestionSources.optional(), preanalysisCounts: protosAiV1MemoryPreanalysisCounts.optional(), vulnerabilityClass: z.string().optional(), projectTagId: z.number().int().optional()}); export const protosAiV1CreateMemoryResponse = z.object({memory: protosAiV1Memory.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 Memory * * POST /api/ai/deployments/{deploymentId}/memories * * Also accepts auth: SemgrepJWT */ export const createApiApiAiDeploymentsDeploymentIdMemories = async (args: CreateApiApiAiDeploymentsDeploymentIdMemoriesArgs, options: CreateApiApiAiDeploymentsDeploymentIdMemoriesOptions) => { const {baseUrl, fetch: fetchFn = fetch, SemgrepWebToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (SemgrepWebToken !== undefined) headers.set('authorization', `Bearer ${SemgrepWebToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/ai/deployments/${encodeURIComponent(String(args.deploymentId))}/memories${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(protosAiV1CreateMemoryResponse, await res.json()) };