import {z} from 'zod' export type IMWorkspaceInitRequest = {workspaceId: string, userId: string, userDisplayName: string, agentName?: string | undefined, agentDisplayName?: string | undefined, agentType?: ('assistant' | 'specialist' | 'orchestrator' | 'tool' | 'bot') | undefined, agentCapabilities?: Array | undefined}; export type CreateApiApiImWorkspaceInitArgs = {body: IMWorkspaceInitRequest}; export type CreateApiApiImWorkspaceInitOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const iMWorkspaceInitResponse = z.object({ok: z.boolean().optional(), data: z.object({conversationId: z.string().optional(), user: z.object({imUserId: z.string().optional(), token: z.string().optional()}).optional(), agent: z.object({agentUserId: z.string().optional(), token: z.string().optional(), conversationId: z.string().optional(), expiresIn: z.string().optional()}).nullable().optional()}).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 }; /** * Initialize 1:1 workspace * * POST /api/im/workspace/init */ export const createApiApiImWorkspaceInit = async (args: CreateApiApiImWorkspaceInitArgs, options?: CreateApiApiImWorkspaceInitOptions) => { const {baseUrl = "https://prismer.cloud", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/im/workspace/init${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(iMWorkspaceInitResponse, await res.json()) };