import {z} from 'zod' export type ReasoningConfiguration = {enabled?: (boolean | null) | undefined, custom_instructions?: (string | null) | undefined}; export type PeerCardConfiguration = {use?: (boolean | null) | undefined, create?: (boolean | null) | undefined}; export type SummaryConfiguration = {enabled?: (boolean | null) | undefined, messages_per_short_summary?: (number | null) | undefined, messages_per_long_summary?: (number | null) | undefined}; export type DreamConfiguration = {enabled?: (boolean | null) | undefined}; export type WorkspaceConfiguration = {reasoning?: (ReasoningConfiguration | null) | undefined, peer_card?: (PeerCardConfiguration | null) | undefined, summary?: (SummaryConfiguration | null) | undefined, dream?: (DreamConfiguration | null) | undefined} | {[key: string]: unknown}; export type WorkspaceCreate = {id: string, metadata?: {[key: string]: unknown} | undefined, configuration?: WorkspaceConfiguration | undefined}; export type CreateApiV3WorkspacesArgs = {body: WorkspaceCreate}; export type CreateApiV3WorkspacesOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const workspace = z.object({id: z.string(), metadata: z.record(z.string(), z.unknown()).optional(), configuration: z.record(z.string(), z.unknown()).optional(), created_at: 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 }; /** * Get Or Create Workspace * * POST /v3/workspaces */ export const createApiV3Workspaces = async (args: CreateApiV3WorkspacesArgs, options?: CreateApiV3WorkspacesOptions) => { const {baseUrl = "https://api.honcho.dev", 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(/\/+$/, '')}/v3/workspaces${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(workspace, await res.json()) };