import {z} from 'zod' export type CreatePluginRequest = {name: string, enabled?: boolean | undefined, config?: {[key: string]: unknown} | undefined, path?: string | undefined}; export type CreateApiApiPluginsArgs = {body: CreatePluginRequest}; export type CreateApiApiPluginsOptions = {baseUrl?: string | undefined, ManagementBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const plugin = z.object({id: z.number().int().optional(), name: z.string().optional(), actualName: z.string().optional(), enabled: z.boolean().optional(), config: z.record(z.string(), z.unknown()).optional(), isCustom: z.boolean().optional(), path: z.string().optional(), status: z.object({name: z.string().optional(), status: z.enum(["active", "error", "disabled", "loading", "uninitialized", "unloaded", "loaded"]).optional(), logs: z.array(z.string()).optional(), types: z.array(z.enum(["llm", "mcp", "http", "observability"])).optional()}).optional(), created_at: z.string().optional(), version: z.number().int().optional(), updated_at: z.string().optional(), config_hash: z.string().optional()}); export const createApiApiPluginsResponse = z.object({message: z.string().optional(), plugin: plugin.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 a new plugin * * POST /api/plugins */ export const createApiApiPlugins = async (args: CreateApiApiPluginsArgs, options?: CreateApiApiPluginsOptions) => { const {baseUrl = "http://localhost:8080", fetch: fetchFn = fetch, ManagementBearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ManagementBearerAuth !== undefined) headers.set('authorization', `Bearer ${ManagementBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/plugins${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(createApiApiPluginsResponse, await res.json()) };