import {z} from 'zod' export type ActionTicketHandoffRequest = {ticket_id: string, attribution_token: string, principal_handoff_consent: true, handoff_consent_version: 'nhs-provider-handoff-consent-v1', principal_controlled_intent_disclosure_consent?: boolean | undefined, controlled_intent_disclosure_consent_version?: 'nhs-provider-controlled-intent-disclosure-consent-v1' | undefined}; export type CreateApiActionTicketsHandoffArgs = {body: ActionTicketHandoffRequest}; export type CreateApiActionTicketsHandoffOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const publicActionTicket = z.object({id: z.string(), provider_claim_id: z.string(), provider_offer_id: z.string(), search_receipt_id: z.string().optional(), offer_version: z.number().int().gte(1), offer_name: z.string(), offer_summary: z.string(), action_type: z.enum(["lead", "demo", "trial", "signup", "purchase", "quote", "application", "booking"]), disclosure: z.literal("Provider-funded action"), charge_event: z.enum(["accepted", "activated", "converted"]), bounty_cents: z.number().int().gte(1), currency: z.literal("usd"), billing_mode: z.literal("terms"), commercial_terms_contract_version: z.literal("nhs-provider-commercial-terms-v1"), commercial_terms_sha256: z.string(), principal_price_mode: z.enum(["free", "fixed", "quote", "provider_pricing"]), principal_price_cents: z.number().int().gte(0).optional(), principal_currency: z.literal("usd"), demand_topic: z.enum(["payments", "commerce", "jobs", "data", "search", "weather", "maps", "email", "messaging", "image", "video", "audio", "documents", "security", "finance", "health", "education", "news", "analytics", "automation", "productivity", "identity", "storage", "ai-tools", "developer-tools", "other", "redacted"]), region_code: z.string().optional(), budget_band: z.enum(["unspecified", "under_100", "100_499", "500_1999", "2000_plus"]), urgency: z.enum(["unspecified", "now", "7_days", "30_days", "researching"]), requirement_flags: z.array(z.enum(["api_access", "mcp", "sandbox", "self_serve", "enterprise", "compliance", "multilingual", "human_support"])), principal_consent: z.literal(true), consent_version: z.literal("nhs-principal-consent-v1"), status: z.enum(["created", "redirected", "accepted", "activated", "converted", "rejected", "duplicate", "invalid"]), expires_at: z.string(), intent_redacted_at: z.string().optional(), authorization_revoked_at: z.string().optional(), created_at: z.string(), updated_at: z.string()}); export const providerActionHandoffReceipt = z.object({id: z.string(), action_ticket_id: z.string(), provider_offer_id: z.string(), offer_version: z.number().int().gte(1), commercial_terms_contract_version: z.literal("nhs-provider-commercial-terms-v1"), commercial_terms_sha256: z.string(), principal_handoff_consent: z.literal(true), handoff_consent_version: z.literal("nhs-provider-handoff-consent-v1"), principal_controlled_intent_disclosure_consent: z.boolean(), controlled_intent_disclosure_consent_version: z.literal("nhs-provider-controlled-intent-disclosure-consent-v1").optional(), event_contract_version: z.literal("nhs-action-handoff-v1"), observed_at: z.string(), created_at: z.string()}); export const actionTicketHandoffResponse = z.object({ticket: publicActionTicket, handoff_receipt: providerActionHandoffReceipt, action_url: z.string(), observed_handoff: z.literal(true), idempotent_replay: z.boolean(), principal_charged: z.literal(false), provider_charged: z.literal(false), organic_rank_affected: z.literal(false), direct_provider_access_is_free: z.literal(true)}); 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 }; /** * Record an NHS-observed handoff and reveal the provider action URL * * POST /action-tickets/handoff */ export const createApiActionTicketsHandoff = async (args: CreateApiActionTicketsHandoffArgs, options?: CreateApiActionTicketsHandoffOptions) => { const {baseUrl = "https://nothumansearch.ai/api/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/action-tickets/handoff${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(actionTicketHandoffResponse, await res.json()) };