import {z} from 'zod' export type BranchProtectionStatus = 'writer_perms_required' | 'admin_perms_required'; export type BodyUpdateAgentBranchV1ConvaiAgentsAgentIdBranchesBranchIdPatch = {name?: (string | null) | undefined, description?: (string | null) | undefined, is_archived?: (boolean | null) | undefined, protection_status?: (BranchProtectionStatus | null) | undefined, auto_rebase?: (boolean | null) | undefined}; export type PatchApiV1ConvaiAgentsAgentIdBranchesBranchIdArgs = {agent_id: string, branch_id: string, 'xi-api-key'?: (string | null) | undefined, body: BodyUpdateAgentBranchV1ConvaiAgentsAgentIdBranchesBranchIdPatch}; export type PatchApiV1ConvaiAgentsAgentIdBranchesBranchIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const branchProtectionStatus = z.enum(["writer_perms_required", "admin_perms_required"]); export const autoRebaseOutcome = z.enum(["succeeded", "conflict", "no_common_ancestor", "rejected", "error"]); export const autoRebaseAttempt = z.object({outcome: autoRebaseOutcome, attempted_at: z.number().int()}); export const branchSyncStatus = z.object({last_attempt: autoRebaseAttempt.nullable().optional()}); export const resourceAccessInfo = z.object({is_creator: z.boolean(), creator_name: z.string(), creator_email: z.string(), role: z.enum(["admin", "editor", "commenter", "viewer"]), anonymous_access_level_override: z.enum(["admin", "editor", "commenter", "viewer"]).nullable().optional(), access_source: z.enum(["creator", "explicit", "workspace_admin", "workspace_default"]).nullable().optional()}); export const agentBranchBasicInfo = z.object({id: z.string(), name: z.string()}); export const agentVersionParents = z.object({in_branch_parent_id: z.string().nullable().optional(), out_of_branch_parent_id: z.string().nullable().optional(), merged_into_branch_id: z.string().nullable().optional(), merged_from_branch_id: z.string().nullable().optional(), merged_from_version_id: z.string().nullable().optional(), rebased_from_version_id: z.string().nullable().optional()}); export const agentVersionMetadata = z.object({id: z.string(), agent_id: z.string(), branch_id: z.string(), version_description: z.string(), seq_no_in_branch: z.number().int(), time_committed_secs: z.number().int(), parents: agentVersionParents, access_info: resourceAccessInfo.nullable().optional(), merged_authors: z.array(resourceAccessInfo).nullable().optional()}); export const agentBranchResponse = z.object({id: z.string(), name: z.string(), agent_id: z.string(), description: z.string(), created_at: z.number().int(), last_committed_at: z.number().int(), is_archived: z.boolean(), protection_status: branchProtectionStatus.optional(), auto_rebase: z.boolean().optional(), sync_status: branchSyncStatus.nullable().optional(), access_info: resourceAccessInfo.nullable().optional(), current_live_percentage: z.number().optional(), parent_branch: agentBranchBasicInfo.nullable().optional(), most_recent_versions: z.array(agentVersionMetadata).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 }; /** * Update Agent Branch * * PATCH /v1/convai/agents/{agent_id}/branches/{branch_id} */ export const patchApiV1ConvaiAgentsAgentIdBranchesBranchId = async (args: PatchApiV1ConvaiAgentsAgentIdBranchesBranchIdArgs, options: PatchApiV1ConvaiAgentsAgentIdBranchesBranchIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['xi-api-key'] != null) headers.set("xi-api-key", String(args['xi-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/convai/agents/${encodeURIComponent(String(args.agent_id))}/branches/${encodeURIComponent(String(args.branch_id))}${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(agentBranchResponse, await res.json()) };