|
| 1 | +import type { |
| 2 | + ApiCrawlOptions, |
| 3 | + ApiExtractOptions, |
| 4 | + ApiGenerateSchemaOptions, |
| 5 | + ApiHistoryFilterInput, |
| 6 | + ApiMonitorCreateInput, |
| 7 | + ApiScrapeFormat, |
| 8 | + ApiScrapeFormatEntry, |
| 9 | + ApiScrapeOptions, |
| 10 | + ApiSearchOptions, |
| 11 | + LegacyApiMonitorCreateInput, |
| 12 | +} from "../types/index.js"; |
| 13 | +import { toJsonSchema } from "../zod.js"; |
| 14 | + |
| 15 | +function normalizeSchema(schema: unknown) { |
| 16 | + return schema ? toJsonSchema(schema) : undefined; |
| 17 | +} |
| 18 | + |
| 19 | +function normalizeScrapeFormatEntry(format: ApiScrapeFormatEntry): ApiScrapeFormatEntry { |
| 20 | + if (format.type !== "json" || !format.schema) return format; |
| 21 | + return { |
| 22 | + ...format, |
| 23 | + schema: normalizeSchema(format.schema), |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +function buildSingleFormatEntry( |
| 28 | + format: ApiScrapeFormat, |
| 29 | + options?: Pick<ApiScrapeOptions, "html" | "json" | "markdown" | "screenshot" | "summary">, |
| 30 | +): ApiScrapeFormatEntry { |
| 31 | + switch (format) { |
| 32 | + case "markdown": |
| 33 | + return { type: "markdown", ...(options?.markdown ?? { mode: "normal" }) }; |
| 34 | + case "html": |
| 35 | + return { type: "html", ...(options?.html ?? { mode: "normal" }) }; |
| 36 | + case "screenshot": |
| 37 | + return { |
| 38 | + type: "screenshot", |
| 39 | + ...(options?.screenshot ?? { |
| 40 | + fullPage: false, |
| 41 | + width: 1440, |
| 42 | + height: 900, |
| 43 | + quality: 80, |
| 44 | + }), |
| 45 | + }; |
| 46 | + case "links": |
| 47 | + return { type: "links" }; |
| 48 | + case "images": |
| 49 | + return { type: "images" }; |
| 50 | + case "summary": |
| 51 | + return { type: "summary", ...(options?.summary ?? {}) }; |
| 52 | + case "branding": |
| 53 | + return { type: "branding" }; |
| 54 | + case "json": { |
| 55 | + if (!options?.json?.prompt) { |
| 56 | + throw new Error("JSON scrape format requires `json.prompt`"); |
| 57 | + } |
| 58 | + return { |
| 59 | + type: "json", |
| 60 | + ...options.json, |
| 61 | + schema: normalizeSchema(options.json.schema), |
| 62 | + }; |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +export function buildScrapeBody(url: string, scrapeOptions?: ApiScrapeOptions) { |
| 68 | + if (scrapeOptions?.formats?.length) { |
| 69 | + return { |
| 70 | + url, |
| 71 | + contentType: scrapeOptions.contentType, |
| 72 | + fetchConfig: scrapeOptions.fetchConfig, |
| 73 | + formats: scrapeOptions.formats.map(normalizeScrapeFormatEntry), |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + const format = scrapeOptions?.format ?? "markdown"; |
| 78 | + return { |
| 79 | + url, |
| 80 | + contentType: scrapeOptions?.contentType, |
| 81 | + fetchConfig: scrapeOptions?.fetchConfig, |
| 82 | + formats: [ |
| 83 | + buildSingleFormatEntry(format, { |
| 84 | + markdown: scrapeOptions?.markdown, |
| 85 | + html: scrapeOptions?.html, |
| 86 | + screenshot: scrapeOptions?.screenshot, |
| 87 | + json: scrapeOptions?.json, |
| 88 | + summary: scrapeOptions?.summary, |
| 89 | + }), |
| 90 | + ], |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +export function buildExtractBody(url: string | undefined, extractOptions: ApiExtractOptions) { |
| 95 | + const body: Record<string, unknown> = { prompt: extractOptions.prompt }; |
| 96 | + if (url) body.url = url; |
| 97 | + if (extractOptions.html) body.html = extractOptions.html; |
| 98 | + if (extractOptions.markdown) body.markdown = extractOptions.markdown; |
| 99 | + if (extractOptions.schema) body.schema = toJsonSchema(extractOptions.schema); |
| 100 | + if (extractOptions.mode) body.mode = extractOptions.mode; |
| 101 | + if (extractOptions.contentType) body.contentType = extractOptions.contentType; |
| 102 | + if (extractOptions.fetchConfig) body.fetchConfig = extractOptions.fetchConfig; |
| 103 | + return body; |
| 104 | +} |
| 105 | + |
| 106 | +export function buildSearchBody(query: string, searchOptions?: ApiSearchOptions) { |
| 107 | + const body: Record<string, unknown> = { query, ...searchOptions }; |
| 108 | + if (searchOptions?.schema) body.schema = toJsonSchema(searchOptions.schema); |
| 109 | + return body; |
| 110 | +} |
| 111 | + |
| 112 | +export function buildSchemaBody(prompt: string, schemaOptions?: ApiGenerateSchemaOptions) { |
| 113 | + const body: Record<string, unknown> = { prompt }; |
| 114 | + if (schemaOptions?.existingSchema) { |
| 115 | + body.existingSchema = toJsonSchema(schemaOptions.existingSchema); |
| 116 | + } |
| 117 | + if (schemaOptions?.model) body.model = schemaOptions.model; |
| 118 | + return body; |
| 119 | +} |
| 120 | + |
| 121 | +export function buildHistoryQuery(historyFilter?: ApiHistoryFilterInput) { |
| 122 | + const query = new URLSearchParams(); |
| 123 | + if (historyFilter?.page != null) query.set("page", String(historyFilter.page)); |
| 124 | + if (historyFilter?.limit != null) query.set("limit", String(historyFilter.limit)); |
| 125 | + if (historyFilter?.service) query.set("service", historyFilter.service); |
| 126 | + return query.toString(); |
| 127 | +} |
| 128 | + |
| 129 | +export function buildMonitorBody(monitorCreateInput: ApiMonitorCreateInput) { |
| 130 | + if ("formats" in monitorCreateInput && monitorCreateInput.formats?.length) { |
| 131 | + return { |
| 132 | + ...monitorCreateInput, |
| 133 | + formats: monitorCreateInput.formats.map(normalizeScrapeFormatEntry), |
| 134 | + }; |
| 135 | + } |
| 136 | + |
| 137 | + const legacyMonitorInput = monitorCreateInput as LegacyApiMonitorCreateInput; |
| 138 | + return { |
| 139 | + url: legacyMonitorInput.url, |
| 140 | + name: legacyMonitorInput.name, |
| 141 | + webhookUrl: legacyMonitorInput.webhookUrl, |
| 142 | + interval: legacyMonitorInput.interval, |
| 143 | + fetchConfig: legacyMonitorInput.fetchConfig, |
| 144 | + formats: [ |
| 145 | + { |
| 146 | + type: "json" as const, |
| 147 | + prompt: legacyMonitorInput.prompt, |
| 148 | + schema: normalizeSchema(legacyMonitorInput.schema), |
| 149 | + llmConfig: legacyMonitorInput.llmConfig, |
| 150 | + mode: "normal" as const, |
| 151 | + }, |
| 152 | + ], |
| 153 | + }; |
| 154 | +} |
| 155 | + |
| 156 | +export function buildCrawlBody(url: string, crawlOptions?: ApiCrawlOptions) { |
| 157 | + if (crawlOptions?.formats?.length) { |
| 158 | + return { |
| 159 | + url, |
| 160 | + ...crawlOptions, |
| 161 | + formats: crawlOptions.formats.map(normalizeScrapeFormatEntry), |
| 162 | + }; |
| 163 | + } |
| 164 | + |
| 165 | + const { format, ...rest } = crawlOptions ?? {}; |
| 166 | + return { |
| 167 | + url, |
| 168 | + ...rest, |
| 169 | + formats: [buildSingleFormatEntry(format ?? "markdown")], |
| 170 | + }; |
| 171 | +} |
0 commit comments