import {z} from 'zod' export type CallRequestPosition = 'NONE' | 'BEAR' | 'BULL' | 'SKEPTIC'; export type StanceSource = 'CALL_REQUEST' | 'LLM_INFERRED' | 'ADMIN_MANUAL'; export type StanceInput = {ticker: string, stance?: CallRequestPosition | undefined, stance_source?: StanceSource | undefined, is_high_conviction?: boolean | undefined, llm_stance?: (CallRequestPosition | null) | undefined}; export type SpeakerInput = {user_id?: (string | null) | undefined, guest_name?: (string | null) | undefined, speaker_label?: (string | null) | undefined, stances?: Array | undefined}; export type SetSpeakersRequest = {speakers: Array}; export type UpdateApiAdminTranscriptsTranscriptIdSpeakersArgs = {transcript_id: string, body: SetSpeakersRequest}; export type UpdateApiAdminTranscriptsTranscriptIdSpeakersOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const callRequestPosition = z.enum(["NONE", "BEAR", "BULL", "SKEPTIC"]); export const stanceSource = z.enum(["CALL_REQUEST", "LLM_INFERRED", "ADMIN_MANUAL"]); export const stanceResponse = z.object({stance_id: z.string(), association_id: z.string(), ticker: z.string(), stance: callRequestPosition, stance_source: stanceSource, is_high_conviction: z.boolean(), llm_stance: callRequestPosition.nullable()}); export const speakerResponse = z.object({association_id: z.string(), transcript_id: z.string(), user_id: z.string().nullable(), guest_name: z.string().nullable(), speaker_label: z.string().nullable(), stances: z.array(stanceResponse)}); export const updateApiAdminTranscriptsTranscriptIdSpeakersResponse = z.array(speakerResponse); 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 }; /** * Set Transcript Speakers * * PUT /admin/transcripts/{transcript_id}/speakers */ export const updateApiAdminTranscriptsTranscriptIdSpeakers = async (args: UpdateApiAdminTranscriptsTranscriptIdSpeakersArgs, options: UpdateApiAdminTranscriptsTranscriptIdSpeakersOptions) => { 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(/\/+$/, '')}/admin/transcripts/${encodeURIComponent(String(args.transcript_id))}/speakers${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiAdminTranscriptsTranscriptIdSpeakersResponse, await res.json()) };