import {z} from 'zod' export type AgentSerializationEnum = 'JsonConfluent' | 'AvroConfluent'; export type KBSourceConfig = {topic?: (string | null) | undefined, topicPattern?: (string | null) | undefined, inputSerialization?: AgentSerializationEnum | undefined, fields?: (Array | null) | undefined, textFields?: (Array | null) | undefined, metadataFields?: (Array | null) | undefined}; export type KBTextConfig = {textTemplate?: string | undefined}; export type KBEmbeddingConfig = {embeddingConnectionId: string, model?: (string | null) | undefined, dimensions?: (number | null) | undefined, batchSize?: (number | null) | undefined}; export type KBVectorStoreConfig = {vectorStoreConnectionId: string, indexName: string, namespace?: (string | null) | undefined, metric?: ('cosine' | 'dot_product' | 'euclidean') | undefined}; export type KBProcessingConfig = {parallelism?: number | undefined, checkpointIntervalMin?: number | undefined, chunkSize?: number | undefined, chunkOverlap?: number | undefined}; export type CreateKnowledgeBaseRequest = {name: string, description?: (string | null) | undefined, source: KBSourceConfig, text?: KBTextConfig | undefined, embedding: KBEmbeddingConfig, vectorStore: KBVectorStoreConfig, processing?: KBProcessingConfig | undefined}; export type CreateApiKnowledgeBasesConfigArgs = {deploy?: boolean | undefined, body: CreateKnowledgeBaseRequest}; export type CreateApiKnowledgeBasesConfigOptions = {baseUrl?: string | undefined, FronteggHTTPAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const appModelsApiAgentsApiModelsFlinkJobsFlinkJobTypeEnum = z.enum(["pyflink", "jar", "agent_config", "knowledge_base"]); export const flinkJobStatusEnum = z.enum(["CREATED", "DEPLOYING", "RUNNING", "CANCELLING", "CANCELLED", "FAILED", "FINISHED"]); export const knowledgeBaseResponse = z.object({_id: z.string(), name: z.string(), job_type: appModelsApiAgentsApiModelsFlinkJobsFlinkJobTypeEnum, status: flinkJobStatusEnum, desired_status: flinkJobStatusEnum.nullable().optional(), flink_job_name: z.string(), flink_job_id: z.string().nullable().optional(), parallelism: z.number().int().optional(), error_message: z.string().nullable().optional(), agent_config: z.record(z.string(), z.unknown()).nullable().optional(), created_by: z.string().nullable().optional(), created_timestamp: z.string().nullable().optional(), updated_timestamp: z.string().nullable().optional()}).and(z.record(z.string(), z.unknown())); 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 Knowledge Base * * POST /knowledge-bases/config */ export const createApiKnowledgeBasesConfig = async (args: CreateApiKnowledgeBasesConfigArgs, options?: CreateApiKnowledgeBasesConfigOptions) => { const {baseUrl = "https://api.streamkap.com", fetch: fetchFn = fetch, FronteggHTTPAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.deploy != null) query.append("deploy", String(args.deploy)) if (FronteggHTTPAuthentication !== undefined) headers.set('authorization', `Bearer ${FronteggHTTPAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/knowledge-bases/config${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(knowledgeBaseResponse, await res.json()) };