import {z} from 'zod' export type SystemMessage = {role: 'system', content: string, name?: (string | null) | undefined}; export type TextContent = {type: 'text', text: string}; export type AudioData = {url: string}; export type AudioContent = {type: 'audio_url', audio_url: AudioData}; export type ImageData = {url: string}; export type ImageContent = {type: 'image_url', image_url: ImageData}; export type VideoData = {url: string}; export type VideoContent = {type: 'video_url', video_url: VideoData}; export type UserMessageContentMultiModal = TextContent | AudioContent | ImageContent | VideoContent; export type UserMessage = {role: 'user', content: string | Array, name?: (string | null) | undefined}; export type AssistantMessageToolCallFunction = {name: string, arguments: string}; export type AssistantMessageToolCall = {id: string, type: 'function', function: AssistantMessageToolCallFunction}; export type AssistantMessage = {role: 'assistant', content?: (string | null) | undefined, reasoning_content?: (string | null) | undefined, reasoning?: (string | null) | undefined, name?: (string | null) | undefined, tool_calls?: (Array | null) | undefined}; export type ToolMessage = {role: 'tool', content: string, tool_call_id: string, name?: (string | null) | undefined}; export type Message = SystemMessage | UserMessage | AssistantMessage | ToolMessage; export type Function = {description?: (string | null) | undefined, name: string, parameters: {[key: string]: unknown}}; export type Tool = {type: 'function', function: Function}; export type DedicatedChatRenderBody = {model: string, messages: Array, chat_template_kwargs?: ({[key: string]: unknown} | null) | undefined, tools?: (Array | null) | undefined}; export type CreateApiDedicatedV1ChatRenderArgs = {'X-Friendli-Team'?: (string | null) | undefined, body: DedicatedChatRenderBody}; export type CreateApiDedicatedV1ChatRenderOptions = {baseUrl?: string | undefined, token?: string | undefined, fetch?: typeof fetch | undefined}; export const dedicatedChatRenderSuccess = z.object({text: 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 }; /** * Chat render * * POST /dedicated/v1/chat/render */ export const createApiDedicatedV1ChatRender = async (args: CreateApiDedicatedV1ChatRenderArgs, options?: CreateApiDedicatedV1ChatRenderOptions) => { const {baseUrl = "https://api.friendli.ai", fetch: fetchFn = fetch, token} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Friendli-Team'] != null) headers.set("X-Friendli-Team", String(args['X-Friendli-Team'])) if (token !== undefined) headers.set('authorization', `Bearer ${token}`) const url = `${baseUrl.replace(/\/+$/, '')}/dedicated/v1/chat/render${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(dedicatedChatRenderSuccess, await res.json()) };