import {z} from 'zod' export type GetApiV1AgentsAgentIdBranchesBranchIdKnowledgeBaseTopicsTopicIdOptions = {baseUrl?: string | undefined, polyApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createKnowledgeBaseTopicPublicResponse = z.object({id: z.string(), name: z.string(), content: z.string(), actions: z.string(), exampleQueries: z.object({queries: z.array(z.string())}), isActive: z.boolean()}); 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 knowledge base topic * * GET /v1/agents/{agentId}/branches/{branchId}/knowledge-base/topics/{topicId} */ export const getApiV1AgentsAgentIdBranchesBranchIdKnowledgeBaseTopicsTopicId = async (options?: GetApiV1AgentsAgentIdBranchesBranchIdKnowledgeBaseTopicsTopicIdOptions) => { const {baseUrl = "https://api.us.poly.ai", fetch: fetchFn = fetch, polyApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (polyApiKey !== undefined) headers.set("X-API-KEY", polyApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/agents/${encodeURIComponent(String(args.agentId))}/branches/${encodeURIComponent(String(args.branchId))}/knowledge-base/topics/${encodeURIComponent(String(args.topicId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createKnowledgeBaseTopicPublicResponse, await res.json()) };