import {z} from 'zod' export type GetApiApiPreviewProjectsIdContextArgs = {id: string}; export type GetApiApiPreviewProjectsIdContextOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiPreviewProjectsIdContextResponse = z.object({capsules: z.array(z.object({content: z.object({bindings: z.record(z.string(), z.unknown()).optional(), fields: z.record(z.string(), z.unknown()).optional(), format: z.enum(["markdown", "composed", "structured", "log"]), source: z.string().max(128).optional(), summary: z.string().max(2000).nullable().optional(), text: z.string().max(100000).optional()}).and(z.record(z.string(), z.unknown())).optional(), content_md: z.string().max(100000).nullable().optional(), content_rendered: z.string().max(100000).nullable().optional(), created_at: z.string().max(64).optional(), id: z.string().max(36), incomplete: z.boolean().optional(), is_encrypted: z.boolean().optional(), is_stale: z.boolean().optional(), log: z.object({append_via: z.string().max(512), bound_type: z.object({id: z.string().max(36), name: z.string().max(200), slug: z.string().max(200)}), owned_log_ref: z.string().max(300).nullable(), owner_ref: z.string().max(300).nullable().optional(), role: z.enum(["owner", "log"])}).and(z.record(z.string(), z.unknown())).optional(), ref: z.string().max(232).optional(), ref_id: z.number().int().gte(-9007199254740991).lte(9007199254740991).optional(), stale_reasons: z.array(z.union([z.object({kind: z.literal("age"), last_updated_at: z.string().max(64), stale_after_days: z.number().int().gte(-9007199254740991).lte(9007199254740991)}).and(z.record(z.string(), z.unknown())), z.object({dependencies: z.array(z.object({changed_at: z.string().max(64).nullable(), id: z.string().max(128), ref: z.string().max(200), title: z.string().max(500), type_slug: z.string().max(200)}).and(z.record(z.string(), z.unknown()))), kind: z.literal("dependency_changed"), total: z.number().int().gte(-9007199254740991).lte(9007199254740991), truncated: z.boolean(), unavailable_count: z.number().int().gte(-9007199254740991).lte(9007199254740991)}).and(z.record(z.string(), z.unknown()))])).optional(), title: z.string().max(2000).optional(), type_id: z.string().max(36).optional(), type_name: z.string().max(200).optional(), type_slug: z.string().max(200).optional(), updated_at: z.string().max(64).optional(), viewer_can_write: z.boolean().optional()}).and(z.record(z.string(), z.unknown()))), field_schemas: z.record(z.string(), z.array(z.record(z.string(), z.unknown()))).optional(), hasMore: z.boolean().optional(), nextCursor: z.string().max(4096).nullable().optional(), project: z.record(z.string(), z.unknown()), total: z.number().int().gte(-9007199254740991).lte(9007199254740991).nullable()}).and(z.record(z.string(), z.unknown())); 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 project context (recent capsules + graph hints) for AI assistants * * GET /api/preview/projects/{id}/context * * Also accepts auth: cookieAuth, oauth2 */ export const getApiApiPreviewProjectsIdContext = async (args: GetApiApiPreviewProjectsIdContextArgs, options: GetApiApiPreviewProjectsIdContextOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/preview/projects/${encodeURIComponent(String(args.id))}/context${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(getApiApiPreviewProjectsIdContextResponse, await res.json()) };