import {z} from 'zod' export type CreateApiApiAgentDeepSignalArgs = {body: {name: string}}; export type CreateApiApiAgentDeepSignalOptions = {baseUrl?: string | undefined, creditPackKey?: string | undefined, fetch?: typeof fetch | undefined}; export const deepSignal = z.object({found: z.boolean(), name: z.string().optional(), sector: z.string().optional(), stage: z.string().optional(), geography: z.string().optional(), signalType: z.string().optional(), scores: z.object({velocity: z.number().int().gte(0).lte(100).optional(), growth: z.number().int().gte(0).lte(100).optional(), novelty: z.number().int().gte(0).lte(100).optional(), composite: z.number().int().gte(0).lte(100).optional()}).optional(), rank: z.object({inSector: z.number().int().optional(), sectorTotal: z.number().int().optional(), sectorPercentile: z.number().int().gte(0).lte(100).optional()}).optional(), thesis: z.string().optional(), comparables: z.array(z.object({name: z.string().optional(), commitVelocityChange: z.string().optional(), signalType: z.string().optional()})).optional(), balance: z.number().int().gte(0).optional(), charged: z.number().int().gte(0).lte(1).optional(), citation: z.string().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 }; /** * Get deep enriched signal (paid, per-request) * * POST /api/agent/deep-signal */ export const createApiApiAgentDeepSignal = async (args: CreateApiApiAgentDeepSignalArgs, options?: CreateApiApiAgentDeepSignalOptions) => { const {baseUrl = "https://signals.gitdealflow.com", fetch: fetchFn = fetch, creditPackKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (creditPackKey !== undefined) headers.set('authorization', `Bearer ${creditPackKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/agent/deep-signal${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(deepSignal, await res.json()) };