import {z} from 'zod' export type GoalBase = {id: string, type: string, prompt: string, follow_up_depth?: (string | null) | undefined, schema?: ({[key: string]: unknown} | null) | undefined, walk_through?: (boolean | null) | undefined, probing_areas?: (Array | null) | undefined, entry_points?: (Array | null) | undefined, capture_mode?: (string | null) | undefined, track?: (Array | null) | undefined, commit_policy?: (string | null) | undefined, invariants?: (Array | null) | undefined, extraction_mode?: (string | null) | undefined}; export type EmbeddedSettings = {skip_join_screen?: boolean | undefined, auto_close?: boolean | undefined, fab_anchor?: string | undefined, persist_across_conversations?: boolean | undefined} | {[key: string]: unknown}; export type TemplateSettings = {greeting?: string | undefined, greeting_text?: string | undefined, language?: string | undefined, tone?: string | undefined, max_session_duration_minutes?: number | undefined, allow_screen_share?: boolean | undefined, suggest_screen_sharing?: boolean | undefined, instant_start?: boolean | undefined, multi_session?: boolean | undefined, system_instructions?: string | undefined, outputs?: Array<{[key: string]: unknown}> | undefined, speaker_skills?: Array | undefined, speaker_skillsets?: Array | undefined, advisors?: Array | undefined, skills?: Array | undefined, skill_execution_mode?: string | undefined, data_sources?: Array<{[key: string]: unknown}> | undefined, embedded?: EmbeddedSettings | undefined} | {[key: string]: unknown}; export type PhaseInline = {body_instructions?: string | undefined, goals?: Array | undefined, speaker_skills?: Array | undefined, speaker_skillsets?: Array | undefined, advisors?: Array | undefined, data_sources?: Array<{[key: string]: unknown}> | undefined, outputs?: Array<{[key: string]: unknown}> | undefined}; export type Phase = {phase_id: string, kind: string, ref?: (string | null) | undefined, inline?: (PhaseInline | null) | undefined, bridging_instructions?: (string | null) | undefined, display_name?: (string | null) | undefined, carry_summary?: boolean | undefined, required_goals?: (Array | null) | undefined}; export type TemplateCreate = {name: string, description?: string | undefined, goals?: Array | undefined, settings?: TemplateSettings | undefined, phases_enabled?: boolean | undefined, phases?: Array | undefined, intro_instructions?: string | undefined, outro_instructions?: string | undefined, default_use?: string | undefined}; export type CreateApiV1TemplatesArgs = {body: TemplateCreate}; export type CreateApiV1TemplatesOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const templateOut = z.object({id: z.string(), name: z.string(), description: z.string(), goals: z.array(z.record(z.string(), z.unknown())), settings: z.record(z.string(), z.unknown()), session_brief: z.string().optional(), tldr: z.string().optional(), status: z.string(), is_default: z.boolean().optional(), phases_enabled: z.boolean().optional(), phases: z.array(z.record(z.string(), z.unknown())).optional(), intro_instructions: z.string().optional(), outro_instructions: z.string().optional(), default_use: z.string().optional(), created_at: z.string(), updated_at: z.string(), warnings: z.array(z.record(z.string(), z.unknown())).optional(), parent_template_id: z.string().nullable().optional(), inheritance: z.record(z.string(), z.unknown()).nullable().optional(), created_by: z.string().nullable().optional(), creator_name: z.string().nullable().optional(), creator_email: z.string().nullable().optional(), speaker_model: z.string().nullable().optional(), advisor_model: z.string().nullable().optional(), turn_detector_backend: z.string().nullable().optional(), archetype_id: z.string().nullable().optional(), conversation_count: z.number().int().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 Template * * POST /v1/templates */ export const createApiV1Templates = async (args: CreateApiV1TemplatesArgs, options: CreateApiV1TemplatesOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/templates${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(templateOut, await res.json()) };