@llml-browser/types
Version:
TypeScript types and schemas for the @llml-browser API
1 lines • 13.6 kB
Source Map (JSON)
{"version":3,"sources":["../src/routers/browse/types.ts"],"sourcesContent":["import { HTMLCleaningOptionsSchema } from '@/services/html-cleaning/types';\nimport {\n ExtractedLinksSchema,\n LinkExtractionOptionsSchema,\n} from '@/services/link/types';\nimport {\n MetadataOptionsSchema,\n PageMetadataSchema,\n} from '@/services/metadata/types';\nimport { z } from 'zod';\n\n/**\n * Base interface for all browse response types.\n * Defines common properties that exist in both success and error responses.\n *\n * @property success - Indicates whether the operation was successful\n * @property url - The URL that was requested to be browsed\n */\ninterface BrowseBaseResponse {\n /**\n * Indicates whether the operation was successful.\n */\n success: boolean;\n\n /**\n * The URL that was requested to be browsed.\n */\n url: string;\n}\n\n/**\n * Response returned when a browse operation is successful.\n * Extends the base response and includes the DataFormats directly in the response.\n *\n * @property success - Discriminator field to identify a successful response (always true)\n * @property url - The URL that was requested to be browsed\n * @property status - HTTP status code of the response (if applicable)\n * @property completedTimestamp - ISO timestamp when the browsing operation completed\n * @property browserSessionId - Unique identifier for the browser session that processed this request\n * @property markdown - Optional markdown representation of the page content\n * @property rawHtml - Optional raw HTML of the page as returned by the server\n * @property cleanedHtml - Optional cleaned HTML with unnecessary elements removed\n * @property links - Optional extracted links from the page\n * @property metadata - Optional metadata extracted from the page\n *\n * @example\n * ```typescript\n * const successResponse: BrowseSuccessResponse = {\n * success: true,\n * url: \"https://example.com\",\n * completedTimestamp: \"2025-04-02T14:28:23.000Z\",\n * browserSessionId: \"session-123\",\n * markdown: \"# Example Page\\nContent here...\"\n * };\n * ```\n */\nexport interface BrowseSuccessResponse extends BrowseBaseResponse, DataFormats {\n /**\n * Discriminator field to identify a successful response.\n * Will always be `true` for success responses.\n */\n success: true;\n\n /**\n * HTTP status code of the response (if applicable).\n * Typically 200 for successful requests.\n */\n status?: number;\n\n /**\n * ISO timestamp when the browsing operation completed.\n * Format: ISO 8601 string (e.g., \"2025-04-02T14:28:23.000Z\")\n */\n completedTimestamp: string;\n\n /**\n * Unique identifier for the browser session that processed this request.\n * Can be used for debugging or tracking purposes.\n */\n browserSessionId: string;\n}\n\n/**\n * Response returned when a browse operation fails.\n * Contains error information about what went wrong.\n *\n * @property success - Discriminator field to identify an error response (always false)\n * @property url - The URL that was requested to be browsed\n * @property error - Error message describing what went wrong\n *\n * @example\n * ```typescript\n * const errorResponse: BrowseErrorResponse = {\n * success: false,\n * url: \"https://example.com\",\n * error: \"Failed to connect to the server\"\n * };\n * ```\n */\nexport interface BrowseErrorResponse extends BrowseBaseResponse {\n /**\n * Discriminator field to identify an error response.\n * Will always be `false` for error responses.\n */\n success: false;\n\n /**\n * Error message describing what went wrong.\n * Provides details about the failure reason.\n */\n error: string;\n}\n\n/**\n * Union type representing either a successful or failed browse operation.\n * Uses a discriminated union pattern with the 'success' property as the discriminator.\n *\n * @example\n * ```typescript\n * function handleResponse(response: BrowseResponse) {\n * if (response.success) {\n * // TypeScript knows this is a BrowseSuccessResponse\n * console.log(response.markdown);\n * } else {\n * // TypeScript knows this is a BrowseErrorResponse\n * console.error(response.error);\n * }\n * }\n * ```\n */\nexport type BrowseResponse = BrowseSuccessResponse | BrowseErrorResponse;\n\n/**\n * Schema for validating the different data formats that can be returned from a browse operation.\n * Each format is optional and will only be included if requested.\n */\nconst DataFormatsSchema = z.object({\n /**\n * Markdown representation of the page content.\n * Contains the page content converted to Markdown format.\n */\n markdown: z.string().optional(),\n\n /**\n * Raw HTML of the page as returned by the server.\n * Contains the unmodified HTML response from the target URL.\n */\n rawHtml: z.string().optional(),\n\n /**\n * Cleaned HTML with unnecessary elements removed.\n * Contains a sanitized version of the HTML with ads, scripts, and other non-content elements removed.\n */\n cleanedHtml: z.string().optional(),\n\n /**\n * Extracted links from the page.\n * Contains information about links found on the page.\n */\n links: ExtractedLinksSchema.optional(),\n\n /**\n * Metadata extracted from the page.\n * Contains information like title, description, and other meta tags.\n */\n metadata: PageMetadataSchema.optional(),\n});\n\n/**\n * Represents the various data formats that can be returned from a browse operation.\n * All properties are optional and will only be included if specifically requested.\n *\n * @property markdown - Optional markdown representation of the page content\n * @property rawHtml - Optional raw HTML of the page as returned by the server\n * @property cleanedHtml - Optional cleaned HTML with unnecessary elements removed\n * @property links - Optional extracted links from the page\n * @property metadata - Optional metadata extracted from the page\n */\nexport type DataFormats = z.infer<typeof DataFormatsSchema>;\n\n/**\n * Enum of available data formats that can be requested in a browse operation.\n * Used to specify which formats should be included in the response.\n */\nexport const DataFormatsEnum = z.enum([\n 'markdown',\n 'rawHtml',\n 'cleanedHtml',\n 'links',\n 'metadata',\n]);\n\n/**\n * Schema for validating browse operation options.\n * Defines the structure of the options object that can be passed to the browse function.\n */\nexport const BrowseOptionsSchema = z.object({\n /**\n * Array of data formats to include in the response.\n * If not specified, defaults to ['markdown', 'metadata'].\n *\n * @example\n * ```typescript\n * const options = {\n * formats: ['markdown', 'links', 'metadata']\n * };\n * ```\n */\n formats: z\n .array(DataFormatsEnum)\n .optional()\n .default(['markdown', 'metadata']),\n\n /**\n * Options for metadata extraction.\n * If not specified, defaults to the default metadata options.\n */\n metadataOptions: MetadataOptionsSchema.optional(),\n\n /**\n * Options for link extraction.\n * If not specified, defaults to the default link extraction options.\n */\n linksOptions: LinkExtractionOptionsSchema.optional(),\n\n /**\n * Options for HTML cleaning.\n * If not specified, defaults to the default HTML cleaning options.\n */\n cleanedHtmlOptions: HTMLCleaningOptionsSchema.optional(),\n});\n\n/**\n * Represents the options that can be passed to the browse function.\n * Defines the structure of the options object.\n *\n * @property formats - Array of data formats to include in the response\n * @property metadataOptions - Options for metadata extraction\n * @property linksOptions - Options for link extraction\n * @property cleanedHtmlOptions - Options for HTML cleaning\n */\nexport type BrowseOptions = z.infer<typeof BrowseOptionsSchema>;\n\n/**\n * Type representing a successful text response from a GET query.\n * A string containing the response data.\n */\nexport type BrowseQuerySucessTextResponse = string;\n\n/**\n * Type representing a successful JSON response from a GET query.\n * Includes the browse response data and the request duration.\n *\n * @property success - Discriminator field to identify a successful response (always true)\n * @property url - The URL that was requested to be browsed\n * @property completedTimestamp - ISO timestamp when the browsing operation completed\n * @property browserSessionId - Unique identifier for the browser session\n * @property requestDuration - Duration of the request in milliseconds\n * @property markdown - Optional markdown representation of the page content\n * @property rawHtml - Optional raw HTML of the page as returned by the server\n * @property cleanedHtml - Optional cleaned HTML with unnecessary elements removed\n * @property links - Optional extracted links from the page\n * @property metadata - Optional metadata extracted from the page\n *\n * @example\n * ```typescript\n * const jsonResponse: BrowseQuerySucessJsonResponse = {\n * success: true,\n * url: \"https://example.com\",\n * completedTimestamp: \"2025-04-02T14:28:23.000Z\",\n * browserSessionId: \"session-123\",\n * markdown: \"# Example Page\\nContent here...\",\n * requestDuration: \"1234ms\"\n * };\n * ```\n */\nexport type BrowseQuerySucessJsonResponse = BrowseSuccessResponse & {\n /**\n * Duration of the request in milliseconds.\n * Format: string with \"ms\" suffix (e.g., \"1234ms\")\n */\n requestDuration: string;\n};\n\n/**\n * Union type representing the possible responses from a GET query.\n * Can be either a successful text response, a successful JSON response, or an error response.\n */\nexport type BrowseQueryResponse =\n | BrowseQuerySucessJsonResponse\n | BrowseQuerySucessTextResponse\n | BrowseErrorResponse;\n\n/**\n * Type representing an error response from a POST query.\n * Contains an error message describing what went wrong.\n *\n * @property success - Discriminator field to identify an error response (always false)\n * @property error - Error message describing what went wrong\n *\n * @example\n * ```typescript\n * const errorResponse: BrowsePostErrorResponse = {\n * success: false,\n * error: \"Invalid URL format in batch request\"\n * };\n * ```\n */\nexport type BrowsePostErrorResponse = {\n /**\n * Discriminator field to identify an error response.\n * Will always be `false` for error responses.\n */\n success: false;\n\n /**\n * Error message describing what went wrong.\n * Provides details about the failure reason.\n */\n error: string;\n};\n\n/**\n * Type representing a successful response from a POST query.\n * Includes the request duration, successful responses, and failed responses.\n *\n * @property requestDuration - Duration of the request in milliseconds\n * @property successful - Array of successful browse responses\n * @property failed - Array of failed browse responses\n *\n * @example\n * ```typescript\n * const successResponse: BrowsePostSuccessResponse = {\n * requestDuration: \"2345ms\",\n * successful: [\n * {\n * success: true,\n * url: \"https://example.com\",\n * completedTimestamp: \"2025-04-02T14:28:23.000Z\",\n * browserSessionId: \"session-123\",\n * markdown: \"# Example Page\\nContent here...\"\n * }\n * ],\n * failed: [\n * {\n * success: false,\n * url: \"https://invalid-url.com\",\n * error: \"Failed to connect to the server\"\n * }\n * ]\n * };\n * ```\n */\nexport type BrowsePostSuccessResponse = {\n /**\n * Duration of the request in milliseconds.\n * Format: string with \"ms\" suffix (e.g., \"2345ms\")\n */\n requestDuration: string;\n\n /**\n * Array of successful browse responses.\n * Contains details for each URL that was successfully processed.\n */\n successful: BrowseSuccessResponse[];\n\n /**\n * Array of failed browse responses.\n * Contains error details for each URL that failed processing.\n */\n failed: BrowseErrorResponse[];\n};\n\n/**\n * Union type representing the possible responses from a POST query.\n * Can be either a successful response or an error response.\n *\n * @example\n * ```typescript\n * function handleBatchResponse(response: BrowsePostResponse) {\n * if (response.success === false) {\n * // This is a BrowsePostErrorResponse\n * console.error(response.error);\n * } else {\n * // This is a BrowsePostSuccessResponse\n * console.log(`Processed ${response.successful.length} URLs successfully`);\n * console.log(`Failed to process ${response.failed.length} URLs`);\n * }\n * }\n * ```\n */\nexport type BrowsePostResponse =\n | BrowsePostSuccessResponse\n | BrowsePostErrorResponse;\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AASA,SAAS,SAAS;AA+HlB,IAAM,oBAAoB,EAAE,OAAO;AAAA;AAAA;AAAA;AAAA;AAAA,EAKjC,UAAU,EAAE,OAAO,EAAE,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA,EAM9B,SAAS,EAAE,OAAO,EAAE,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA,EAM7B,aAAa,EAAE,OAAO,EAAE,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA,EAMjC,OAAO,qBAAqB,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA,EAMrC,UAAU,mBAAmB,SAAS;AACxC,CAAC;AAkBM,IAAM,kBAAkB,EAAE,KAAK;AAAA,EACpC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAMM,IAAM,sBAAsB,EAAE,OAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAY1C,SAAS,EACN,MAAM,eAAe,EACrB,SAAS,EACT,QAAQ,CAAC,YAAY,UAAU,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMnC,iBAAiB,sBAAsB,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA,EAMhD,cAAc,4BAA4B,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA,EAMnD,oBAAoB,0BAA0B,SAAS;AACzD,CAAC;","names":[]}