import {z} from 'zod' export type GovernanceUpdateRequest = {governance_status?: ('active' | 'retired' | 'suppressed' | 'manual_hold' | 'expired' | null) | undefined, expires_at?: (string | null) | undefined, reason_code?: (string | null) | undefined, note?: (string | null) | undefined, idempotency_key?: (string | null) | undefined}; export type PatchApiV1KnowledgeBaseDocumentsDocIdVersionsVersionIdGovernanceArgs = {doc_id: string, version_id: string, body: GovernanceUpdateRequest}; export type PatchApiV1KnowledgeBaseDocumentsDocIdVersionsVersionIdGovernanceOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const governanceVersionResponse = z.object({id: z.string(), document_id: z.string(), knowledge_base_id: z.string(), version_number: z.number().int(), state: z.string(), scan_status: z.string(), governance_status: z.string(), expires_at: z.string().nullable().optional(), governance_reason_code: z.string().nullable().optional(), governance_note: z.string().nullable().optional(), governance_updated_by: z.string().nullable().optional(), governance_updated_at: z.string().nullable().optional(), governance_policy_version: z.string().nullable().optional(), effective_serving_status: z.string()}); 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 }; /** * Update a version's lifecycle governance status / expiry * * PATCH /v1/knowledge-base/documents/{doc_id}/versions/{version_id}/governance */ export const patchApiV1KnowledgeBaseDocumentsDocIdVersionsVersionIdGovernance = async (args: PatchApiV1KnowledgeBaseDocumentsDocIdVersionsVersionIdGovernanceArgs, options: PatchApiV1KnowledgeBaseDocumentsDocIdVersionsVersionIdGovernanceOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/knowledge-base/documents/${encodeURIComponent(String(args.doc_id))}/versions/${encodeURIComponent(String(args.version_id))}/governance${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(governanceVersionResponse, await res.json()) };