totalum-sdk 1.11.0 → 1.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/_types/{coerce.d-BKhuaDya.d.ts → coerce.d-Cp5NZQN-.d.ts} +1 -1
  3. package/dist/_types/{errors.d-RTRkidv5.d.ts → errors.d-ve4rY-Hx.d.ts} +37 -5
  4. package/dist/_types/{integrations.d-CzTa9T-L.d.ts → integrations.d-BRhNKT7K.d.ts} +2 -2
  5. package/dist/_types/{ops.d-CNsrT4Ma.d.ts → ops.d-DIC4u0go.d.ts} +2 -2
  6. package/dist/ai/index.d.ts +275 -23
  7. package/dist/ai/index.js +305 -24
  8. package/dist/analytics/index.d.ts +58 -12
  9. package/dist/analytics/index.js +46 -3
  10. package/dist/browser/index.d.ts +228 -20
  11. package/dist/browser/index.js +122 -13
  12. package/dist/connections/index.d.ts +305 -23
  13. package/dist/connections/index.js +302 -21
  14. package/dist/cron/index.d.ts +147 -20
  15. package/dist/cron/index.js +157 -15
  16. package/dist/d1/errors.js +15 -0
  17. package/dist/d1/index.d.ts +254 -10
  18. package/dist/d1/index.js +24 -4
  19. package/dist/d1/session.js +26 -2
  20. package/dist/email/index.d.ts +65 -10
  21. package/dist/email/index.js +60 -5
  22. package/dist/errors.js +17 -0
  23. package/dist/files/index.d.ts +243 -20
  24. package/dist/files/index.js +247 -14
  25. package/dist/index.d.ts +25 -10
  26. package/dist/index.js +19 -5
  27. package/dist/logs/index.d.ts +48 -10
  28. package/dist/logs/index.js +39 -4
  29. package/dist/payments/index.d.ts +221 -23
  30. package/dist/payments/index.js +216 -18
  31. package/dist/pdf/index.d.ts +59 -9
  32. package/dist/pdf/index.js +54 -4
  33. package/dist/react/index.d.ts +78 -6
  34. package/dist/react/index.js +51 -5
  35. package/dist/realtime/index.d.ts +55 -8
  36. package/dist/realtime/index.js +53 -6
  37. package/dist/scan/index.d.ts +78 -10
  38. package/dist/scan/index.js +63 -3
  39. package/dist/seo/index.d.ts +1118 -8
  40. package/dist/seo/index.js +325 -4
  41. package/dist/speech/index.d.ts +34 -10
  42. package/dist/speech/index.js +25 -5
  43. package/dist/vectors/index.d.ts +96 -9
  44. package/dist/vectors/index.js +93 -7
  45. package/dist/web/index.d.ts +168 -15
  46. package/dist/web/index.js +163 -10
  47. package/dist/webhooks/index.d.ts +122 -15
  48. package/dist/webhooks/index.js +119 -13
  49. package/package.json +2 -1
  50. package/totalum-sdk.md +10336 -754
  51. package/totalum-sdk.signatures.json +69 -1
@@ -1,32 +1,118 @@
1
1
  import { envelopeClient, jsonClient, withUsage } from '../http.js';
2
2
  export { TotalumError, isTotalumError } from '../errors.js';
3
3
  /**
4
- * `totalum.vectors` over `/v1/vectors/*` (scope `vectors`; server-side code only: it needs the project key). Every
4
+ * Creates the `vectors` namespace (the same object as `totalum.vectors`; scope `vectors`; server-side code only: it
5
+ * needs the project key). Every
5
6
  * project environment has its own private vector store: ids, namespaces and indexes are Totalum's, never yours to
6
7
  * name, and `x-totalum-env` (the `env` option) keeps dev and live apart. Vectors are 1,024 numbers; `text` is
7
8
  * embedded with the `embed` model and `image` (a project file key, a files URL, an https URL or a data URL) with the
8
9
  * `embed-image` model, whose space also embeds text: search images by words with `space: 'image'`. Filter on the
9
10
  * metadata keys `source`, `type`, `tag`, `group` (strings) and `page` (number).
11
+ *
12
+ * @param options - The project key, API URL and environment; each falls back to its environment variable.
13
+ * @returns The `vectors` namespace: the methods below.
14
+ * @example
15
+ * import { totalumVectors } from 'totalum-sdk/vectors';
16
+ *
17
+ * const vectors = totalumVectors();
18
+ * const { count } = await vectors.count();
10
19
  */
11
20
  export function totalumVectors(options = {}) {
12
21
  const call = envelopeClient(options);
13
22
  const data = jsonClient(options);
14
23
  return {
15
24
  /**
16
- * Inserts or replaces up to 100 vectors (≤ 8 images) from `values`, `text` or `image`, with optional metadata.
17
- * The answer (and `count()`) means stored; `query` and `get` see the write later — often seconds, sometimes 10+
25
+ * Stores up to 100 items (at most 8 of them images) for semantic search: give each an `id` of yours (the row id),
26
+ * its content as `text`, `image` or ready-made `values`, and metadata to filter on. Writing an existing id replaces
27
+ * it. The answer (and `count()`) means stored; `query` and `get` see the write later — often seconds, sometimes 10+
18
28
  * minutes (the index applies writes asynchronously). Never wait or poll for a write to become searchable.
29
+ *
30
+ * @param vectors - The items to store; each has exactly one of `text`, `image` or `values`.
31
+ * @returns How many were written, how many the environment now holds, and what it cost.
32
+ * @throws VALIDATION_ERROR (400) More than 100 items or 8 images, duplicate ids, an item with no or several contents, a
33
+ * metadata key starting with `_`, or metadata over 9 KB.
34
+ * @throws VECTORS_LIMIT_REACHED (403) The plan's stored vectors per environment are used up; delete some or upgrade.
35
+ * @throws FILE_TOO_LARGE (413) An image is over 20 MB.
36
+ * @throws AI_PROVIDER_UNAVAILABLE (502) The text or image could not be embedded; retry.
37
+ * @throws PROVIDER_UNAVAILABLE (503) The vector store is unavailable (or being set up: retry in a minute).
38
+ * @example
39
+ * await totalum.vectors.upsert([
40
+ * { id: 'faq-12', text: 'Refunds are possible for 30 days after purchase.', metadata: { source: 'faq', tag: 'billing' } },
41
+ * { id: 'product-88', image: product.photo.key, metadata: { type: 'product' } },
42
+ * ]);
43
+ * //=>
44
+ * { "upserted": 2, "count": 2, "usage": { "credits": 0.0021 } }
19
45
  */
20
46
  upsert: async (vectors) => withUsage(await call('POST', '/v1/vectors/upsert', {
21
47
  vectors,
22
48
  })),
23
- /** The `topK` (≤ 50, default 5) closest vectors to `text`, `values` or `image`, with their metadata. */
49
+ /**
50
+ * Finds the stored items closest in meaning to a text, an image or a vector — "search by meaning". Results are best
51
+ * first with a similarity `score`; filter them on metadata. Pass exactly one of `text`, `image` or `values`.
52
+ *
53
+ * @param input - What to search for, how many results, and the metadata filter.
54
+ * @returns The closest items (best first) and what the query cost.
55
+ * @throws VALIDATION_ERROR (400) None or several of `text`/`image`/`values`, `topK` over 50, a filter over 2 KB or on
56
+ * another metadata key.
57
+ * @throws FILE_TOO_LARGE (413) The image is over 20 MB.
58
+ * @throws AI_PROVIDER_UNAVAILABLE (502) The query could not be embedded; retry.
59
+ * @throws PROVIDER_UNAVAILABLE (503) The vector store is unavailable; retry later.
60
+ * @example
61
+ * const { matches } = await totalum.vectors.query({
62
+ * text: 'Can I get my money back?',
63
+ * topK: 3,
64
+ * filter: { source: 'faq' },
65
+ * });
66
+ * //=>
67
+ * {
68
+ * "matches": [
69
+ * { "id": "faq-12", "score": 0.83, "space": "text", "metadata": { "source": "faq", "tag": "billing" } },
70
+ * { "id": "faq-07", "score": 0.61, "space": "text", "metadata": { "source": "faq", "tag": "orders" } }
71
+ * ],
72
+ * "usage": { "credits": 0.0006 }
73
+ * }
74
+ * @example
75
+ * // images that look like a description (image search by words)
76
+ * const { matches } = await totalum.vectors.query({ text: 'red running shoes', space: 'image', topK: 8 });
77
+ */
24
78
  query: async (input) => withUsage(await call('POST', '/v1/vectors/query', input)),
25
- /** The stored vectors (values and metadata) of up to 100 ids; unknown and just-written ids are left out. */
79
+ /**
80
+ * Reads stored items back by id, with their numbers and metadata. Unknown ids — and ids written in the last seconds
81
+ * or minutes — are left out of the answer. Free.
82
+ *
83
+ * @param ids - Up to 100 ids.
84
+ * @returns The items found (possibly fewer than asked).
85
+ * @throws VALIDATION_ERROR (400) No ids, or more than 100.
86
+ * @example
87
+ * const [doc] = await totalum.vectors.get(['faq-12']);
88
+ * //=>
89
+ * [
90
+ * { "id": "faq-12", "space": "text", "values": [0.0138, -0.0121, 0.0125, …], "metadata": { "source": "faq", "tag": "billing" } }
91
+ * ]
92
+ */
26
93
  get: async (ids) => (await data('POST', '/v1/vectors/get', { ids })).vectors,
27
- /** Deletes up to 1,000 ids; answers how many existed and how many are left. */
94
+ /**
95
+ * Deletes items by id — when the row they describe is deleted. Ids that do not exist are ignored.
96
+ *
97
+ * @param ids - Up to 1,000 ids.
98
+ * @returns How many existed and were deleted, how many the environment still holds, and what it cost.
99
+ * @throws VALIDATION_ERROR (400) No ids, or more than 1,000.
100
+ * @example
101
+ * const out = await totalum.vectors.delete(['faq-12', 'faq-13']);
102
+ * //=>
103
+ * { "deleted": 1, "count": 41, "usage": { "credits": 0 } }
104
+ */
28
105
  delete: async (ids) => withUsage(await call('POST', '/v1/vectors/delete', { ids })),
29
- /** How many vectors this environment stores (free). */
106
+ /**
107
+ * How many items this environment stores, in total and per space. Free.
108
+ *
109
+ * @returns The totals.
110
+ * @throws PROVIDER_UNAVAILABLE (503) The vector store is unavailable; retry later.
111
+ * @example
112
+ * const counts = await totalum.vectors.count();
113
+ * //=>
114
+ * { "count": 42, "text": 40, "image": 2 }
115
+ */
30
116
  count: () => data('GET', '/v1/vectors/count'),
31
117
  };
32
118
  }
@@ -1,19 +1,53 @@
1
- import { T as TotalumClientOptions, U as Usage } from '../_types/errors.d-RTRkidv5.js';
2
- export { a as TotalumError, b as TotalumErrorCode, i as isTotalumError } from '../_types/errors.d-RTRkidv5.js';
3
- import { L as WebScrapeInput, M as WebSearchInput, N as WebMapInput, Q as WebCrawlInput, w as ScreenshotInput } from '../_types/integrations.d-CzTa9T-L.js';
4
- export { J as JobCreated, x as ScreenshotOutput, W as WebJob, I as WebScrapeOutput, K as WebSearchOutput } from '../_types/integrations.d-CzTa9T-L.js';
5
- import '../_types/coerce.d-BKhuaDya.js';
1
+ import { T as TotalumClientOptions, U as Usage } from '../_types/errors.d-ve4rY-Hx.js';
2
+ export { a as TotalumError, b as TotalumErrorCode, i as isTotalumError } from '../_types/errors.d-ve4rY-Hx.js';
3
+ import { L as WebScrapeInput, M as WebSearchInput, N as WebMapInput, Q as WebCrawlInput, w as ScreenshotInput } from '../_types/integrations.d-BRhNKT7K.js';
4
+ export { J as JobCreated, x as ScreenshotOutput, W as WebJob, I as WebScrapeOutput, K as WebSearchOutput } from '../_types/integrations.d-BRhNKT7K.js';
5
+ import '../_types/coerce.d-Cp5NZQN-.js';
6
6
 
7
7
  /**
8
- * `totalum.web` over the Totalum API `/v1/web/*` (scope `web`): scrape, search, map, crawl and screenshots,
9
- * billed in credits — every result carries `usage.credits`.
8
+ * Creates the `web` namespace (the same object as `totalum.web`, scope `web`): scrape, search, map, crawl and
9
+ * screenshots, billed in credits — every result carries `usage.credits`.
10
+ *
11
+ * @param options - The project key, API URL and environment; each falls back to its environment variable.
12
+ * @returns The `web` namespace: the methods below.
13
+ * @example
14
+ * import { totalumWeb } from 'totalum-sdk/web';
15
+ *
16
+ * const web = totalumWeb();
17
+ * const page = await web.scrape('https://example.com');
10
18
  */
11
19
  declare function totalumWeb(options?: TotalumClientOptions): {
12
20
  /**
13
- * One page as `markdown`, `html`, `links`, ….
21
+ * Reads one web page and returns its content as clean Markdown (the default), HTML, its links, a screenshot, a
22
+ * summary or JSON extracted with your schema. Use it to import an article, read a product page or feed a page to
23
+ * AI. Pages that need a click or a login need the cloud browser instead.
14
24
  *
25
+ * @param url - The page's `https://` URL.
26
+ * @param opts - What to return and how to load the page.
27
+ * @returns The page in the formats asked for, its metadata, and the credits it cost.
28
+ * @throws URL_NOT_ALLOWED (400) Not a public `http(s)` URL (a private address, `localhost`, a Totalum host).
29
+ * @throws SCRAPE_BLOCKED (502) The site refused the request; try `proxy: 'stealth'`.
30
+ * @throws SCRAPE_FAILED (502) The page could not be read.
31
+ * @throws PROVIDER_TIMEOUT (504) The page took too long; raise `timeout` or retry.
32
+ * @throws PROVIDER_UNAVAILABLE (503) The scraping service is unavailable; retry later.
15
33
  * @example
16
- * const { markdown } = await web.scrape('https://example.com', { formats: ['markdown'] });
34
+ * const page = await totalum.web.scrape('https://example.com', { formats: ['markdown'] });
35
+ * //=>
36
+ * {
37
+ * "url": "https://example.com/",
38
+ * "statusCode": 200,
39
+ * "metadata": { "sourceURL": "https://example.com/", "title": "Example Domain", "language": "en" },
40
+ * "markdown": "# Example Domain\n\nThis domain is for use in documentation examples without needing permission. …",
41
+ * "usage": { "credits": 0.0343 }
42
+ * }
43
+ * @example
44
+ * // structured data with a JSON schema
45
+ * const { json } = await totalum.web.scrape('https://shop.example.com/p/red-shoes', {
46
+ * formats: ['json'],
47
+ * schema: { type: 'object', properties: { name: { type: 'string' }, price: { type: 'number' } } },
48
+ * });
49
+ * //=>
50
+ * { "name": "Red running shoes", "price": 79.9 }
17
51
  */
18
52
  scrape: (url: string, opts?: Omit<WebScrapeInput, "url">) => Promise<{
19
53
  url: string;
@@ -36,7 +70,37 @@ declare function totalumWeb(options?: TotalumClientOptions): {
36
70
  summary?: string | undefined;
37
71
  contentUrl?: string | undefined;
38
72
  }>;
39
- /** A web search; with `scrapeOptions` each result also carries its page content. */
73
+ /**
74
+ * Searches the web and returns the results (title, URL, snippet); with `scrapeOptions`, each result's page content
75
+ * too. Use it for research features, lead finding or to ground an AI answer in current pages.
76
+ *
77
+ * @param query - What to search for, as you would type it in a search engine.
78
+ * @param opts - How many results, which sources, where and how recent.
79
+ * @returns The results by source and the credits the search cost.
80
+ * @throws SEARCH_FAILED (502) The search could not be run.
81
+ * @throws PROVIDER_TIMEOUT (504) The search took too long; retry.
82
+ * @throws PROVIDER_UNAVAILABLE (503) The search service is unavailable; retry later.
83
+ * @example
84
+ * const results = await totalum.web.search('totalum app builder', { limit: 2 });
85
+ * //=>
86
+ * {
87
+ * "web": [
88
+ * {
89
+ * "title": "Totalum - The most powerful AI app builder for humans and for agents",
90
+ * "url": "https://www.totalum.app/en",
91
+ * "snippet": "Create any web or app. The first AI app builder you can use via API and MCP…",
92
+ * "position": 1
93
+ * },
94
+ * {
95
+ * "title": "Totalum Blog: AI app builder, AI coding agents, MCP guides",
96
+ * "url": "https://www.totalum.app/blog",
97
+ * "snippet": "An AI app builder that generates real code lets you export and own the source…",
98
+ * "position": 2
99
+ * }
100
+ * ],
101
+ * "usage": { "credits": 0.0685 }
102
+ * }
103
+ */
40
104
  search: (query: string, opts?: Omit<WebSearchInput, "query">) => Promise<Omit<{
41
105
  web: {
42
106
  title: string;
@@ -72,18 +136,76 @@ declare function totalumWeb(options?: TotalumClientOptions): {
72
136
  }, "usage"> & {
73
137
  usage: Usage;
74
138
  }>;
75
- /** The URLs of a site (sitemap and links), without fetching the pages. */
139
+ /**
140
+ * Lists the URLs of a site (from its sitemap and links) without reading the pages — to choose what to scrape, or
141
+ * to build a site index.
142
+ *
143
+ * @param url - The site's `https://` URL.
144
+ * @param opts - A search filter and the maximum number of links.
145
+ * @returns The URLs found and the credits the call cost.
146
+ * @throws URL_NOT_ALLOWED (400) Not a public `http(s)` URL.
147
+ * @throws SCRAPE_FAILED (502) The site could not be mapped.
148
+ * @throws PROVIDER_UNAVAILABLE (503) The scraping service is unavailable; retry later.
149
+ * @example
150
+ * const { links } = await totalum.web.map('https://docs.example.com', { search: 'pricing', limit: 5 });
151
+ * //=>
152
+ * {
153
+ * "links": ["https://docs.example.com/pricing", "https://docs.example.com/pricing/teams"],
154
+ * "usage": { "credits": 0.0343 }
155
+ * }
156
+ */
76
157
  map: (url: string, opts?: Omit<WebMapInput, "url">) => Promise<Omit<{
77
158
  links: string[];
78
159
  }, "usage"> & {
79
160
  usage: Usage;
80
161
  }>;
81
- /** `202 {jobId}`: poll `crawlStatus` until the job leaves `running`; that read settles its charge. */
162
+ /**
163
+ * Starts reading a whole site (or part of it) in the background and returns a job id at once. Store the id, then
164
+ * call `crawlStatus(jobId)` (from a later request or a cron job) until `status` is no longer `running`; the job
165
+ * is charged by the pages read, settled on that read. The plan caps the pages per crawl: 20 on Free, 500 on
166
+ * Starter, 2,000 on Builder, 10,000 on Professional and 50,000 on Enterprise.
167
+ *
168
+ * @param url - The `https://` URL to start from.
169
+ * @param opts - How many pages, how deep, which paths, and how to read each page.
170
+ * @returns The job id and the credits reserved for it (the real charge is settled when it ends).
171
+ * @throws VALIDATION_ERROR (400) `limit` is above the plan's pages per crawl; `details.maxPages`.
172
+ * @throws URL_NOT_ALLOWED (400) Not a public `http(s)` URL.
173
+ * @throws SCRAPE_FAILED (502) The crawl could not be started.
174
+ * @throws PROVIDER_UNAVAILABLE (503) The scraping service is unavailable; retry later.
175
+ * @example
176
+ * const { jobId } = await totalum.web.crawl('https://docs.example.com', { limit: 50, includePaths: ['/guides/.*'] });
177
+ * // save jobId, then poll crawlStatus(jobId) later
178
+ * //=>
179
+ * { "jobId": "01M4P1C8Z3X7Y2W5V9T6S4R0QA", "estimatedCredits": 1.716 }
180
+ */
82
181
  crawl: (url: string, opts?: Omit<WebCrawlInput, "url">) => Promise<{
83
182
  jobId: string;
84
183
  estimatedCredits?: number | undefined;
85
184
  }>;
86
- /** The crawl job: `status`, pages so far and their content. */
185
+ /**
186
+ * Reads a crawl job: its status, how many pages are done and their content. Poll it every few seconds or from a cron
187
+ * job until `status` is `completed`, `failed`, `cancelled` or `expired`. Reading a finished job settles its charge.
188
+ *
189
+ * @param jobId - The `jobId` from `crawl()`.
190
+ * @returns The job's progress and the pages read so far.
191
+ * @throws JOB_NOT_FOUND (404) No such job for this project (job ids are kept 7 days).
192
+ * @throws SCRAPE_FAILED (502) The job could not be read; retry.
193
+ * @example
194
+ * const job = await totalum.web.crawlStatus('01M4P1C8Z3X7Y2W5V9T6S4R0QA');
195
+ * if (job.status === 'completed') for (const page of job.data) await savePage(page.url, page.markdown);
196
+ * //=>
197
+ * {
198
+ * "jobId": "01M4P1C8Z3X7Y2W5V9T6S4R0QA",
199
+ * "status": "completed",
200
+ * "total": 2,
201
+ * "completed": 2,
202
+ * "creditsUsed": 2,
203
+ * "data": [
204
+ * { "url": "https://docs.example.com/guides/start", "statusCode": 200, "metadata": { "sourceURL": "https://docs.example.com/guides/start", "title": "Getting started" }, "markdown": "# Getting started\n…" },
205
+ * { "url": "https://docs.example.com/guides/deploy", "statusCode": 200, "metadata": { "sourceURL": "https://docs.example.com/guides/deploy", "title": "Deploy" }, "markdown": "# Deploy\n…" }
206
+ * ]
207
+ * }
208
+ */
87
209
  crawlStatus: (jobId: string) => Promise<{
88
210
  jobId: string;
89
211
  status: "queued" | "running" | "completed" | "failed" | "cancelled" | "expired";
@@ -111,7 +233,17 @@ declare function totalumWeb(options?: TotalumClientOptions): {
111
233
  next?: string | undefined;
112
234
  resultUrl?: string | undefined;
113
235
  }>;
114
- /** Stops a running crawl; pages already crawled are charged. */
236
+ /**
237
+ * Stops a running crawl. Pages already read are kept in the answer and charged.
238
+ *
239
+ * @param jobId - The `jobId` from `crawl()`.
240
+ * @returns The job, now `cancelled`, with the pages read before it stopped.
241
+ * @throws JOB_NOT_FOUND (404) No such job for this project.
242
+ * @example
243
+ * const job = await totalum.web.cancelCrawl('01M4P1C8Z3X7Y2W5V9T6S4R0QA');
244
+ * //=>
245
+ * { "jobId": "01M4P1C8Z3X7Y2W5V9T6S4R0QA", "status": "cancelled", "total": 50, "completed": 12, "creditsUsed": 12, "data": […] }
246
+ */
115
247
  cancelCrawl: (jobId: string) => Promise<{
116
248
  jobId: string;
117
249
  status: "queued" | "running" | "completed" | "failed" | "cancelled" | "expired";
@@ -139,7 +271,28 @@ declare function totalumWeb(options?: TotalumClientOptions): {
139
271
  next?: string | undefined;
140
272
  resultUrl?: string | undefined;
141
273
  }>;
142
- /** A screenshot of a `url` or of inline `html`, stored as a project file (`key`, `url`). */
274
+ /**
275
+ * Takes a screenshot of a web page (`url`) or of HTML you pass (`html`) and stores it as a public project file:
276
+ * link previews, social images, visual checks.
277
+ *
278
+ * @param input - What to capture and how.
279
+ * @returns The stored image's `key` and `url`, its format, and the credits it cost.
280
+ * @throws URL_NOT_ALLOWED (400) Not a public `http(s)` URL.
281
+ * @throws SCREENSHOT_FAILED (502) The page could not be captured.
282
+ * @throws VALIDATION_ERROR (400) Neither `url` nor `html` was given.
283
+ * @example
284
+ * const shot = await totalum.web.screenshot({
285
+ * html: '<div style="font:48px sans-serif;padding:40px">Launch day 🚀</div>',
286
+ * viewport: { width: 1200, height: 630 },
287
+ * });
288
+ * //=>
289
+ * {
290
+ * "key": "p/acme-crm/01M4P0SFVZXNRS871ZSKZNWHY0.png",
291
+ * "url": "https://files.totalum-project.com/p/acme-crm/01M4P0SFVZXNRS871ZSKZNWHY0.png",
292
+ * "format": "png",
293
+ * "usage": { "credits": 0.0002 }
294
+ * }
295
+ */
143
296
  screenshot: (input: ScreenshotInput) => Promise<Omit<{
144
297
  key: string;
145
298
  url: string;
package/dist/web/index.js CHANGED
@@ -1,31 +1,184 @@
1
1
  import { envelopeClient, withUsage } from '../http.js';
2
2
  export { TotalumError, isTotalumError } from '../errors.js';
3
3
  /**
4
- * `totalum.web` over the Totalum API `/v1/web/*` (scope `web`): scrape, search, map, crawl and screenshots,
5
- * billed in credits — every result carries `usage.credits`.
4
+ * Creates the `web` namespace (the same object as `totalum.web`, scope `web`): scrape, search, map, crawl and
5
+ * screenshots, billed in credits — every result carries `usage.credits`.
6
+ *
7
+ * @param options - The project key, API URL and environment; each falls back to its environment variable.
8
+ * @returns The `web` namespace: the methods below.
9
+ * @example
10
+ * import { totalumWeb } from 'totalum-sdk/web';
11
+ *
12
+ * const web = totalumWeb();
13
+ * const page = await web.scrape('https://example.com');
6
14
  */
7
15
  export function totalumWeb(options = {}) {
8
16
  const call = envelopeClient(options);
9
17
  const job = (jobId) => `/v1/web/jobs/${encodeURIComponent(jobId)}`;
10
18
  return {
11
19
  /**
12
- * One page as `markdown`, `html`, `links`, ….
20
+ * Reads one web page and returns its content as clean Markdown (the default), HTML, its links, a screenshot, a
21
+ * summary or JSON extracted with your schema. Use it to import an article, read a product page or feed a page to
22
+ * AI. Pages that need a click or a login need the cloud browser instead.
13
23
  *
24
+ * @param url - The page's `https://` URL.
25
+ * @param opts - What to return and how to load the page.
26
+ * @returns The page in the formats asked for, its metadata, and the credits it cost.
27
+ * @throws URL_NOT_ALLOWED (400) Not a public `http(s)` URL (a private address, `localhost`, a Totalum host).
28
+ * @throws SCRAPE_BLOCKED (502) The site refused the request; try `proxy: 'stealth'`.
29
+ * @throws SCRAPE_FAILED (502) The page could not be read.
30
+ * @throws PROVIDER_TIMEOUT (504) The page took too long; raise `timeout` or retry.
31
+ * @throws PROVIDER_UNAVAILABLE (503) The scraping service is unavailable; retry later.
14
32
  * @example
15
- * const { markdown } = await web.scrape('https://example.com', { formats: ['markdown'] });
33
+ * const page = await totalum.web.scrape('https://example.com', { formats: ['markdown'] });
34
+ * //=>
35
+ * {
36
+ * "url": "https://example.com/",
37
+ * "statusCode": 200,
38
+ * "metadata": { "sourceURL": "https://example.com/", "title": "Example Domain", "language": "en" },
39
+ * "markdown": "# Example Domain\n\nThis domain is for use in documentation examples without needing permission. …",
40
+ * "usage": { "credits": 0.0343 }
41
+ * }
42
+ * @example
43
+ * // structured data with a JSON schema
44
+ * const { json } = await totalum.web.scrape('https://shop.example.com/p/red-shoes', {
45
+ * formats: ['json'],
46
+ * schema: { type: 'object', properties: { name: { type: 'string' }, price: { type: 'number' } } },
47
+ * });
48
+ * //=>
49
+ * { "name": "Red running shoes", "price": 79.9 }
16
50
  */
17
51
  scrape: async (url, opts = {}) => (await call('POST', '/v1/web/scrape', { ...opts, url })).data,
18
- /** A web search; with `scrapeOptions` each result also carries its page content. */
52
+ /**
53
+ * Searches the web and returns the results (title, URL, snippet); with `scrapeOptions`, each result's page content
54
+ * too. Use it for research features, lead finding or to ground an AI answer in current pages.
55
+ *
56
+ * @param query - What to search for, as you would type it in a search engine.
57
+ * @param opts - How many results, which sources, where and how recent.
58
+ * @returns The results by source and the credits the search cost.
59
+ * @throws SEARCH_FAILED (502) The search could not be run.
60
+ * @throws PROVIDER_TIMEOUT (504) The search took too long; retry.
61
+ * @throws PROVIDER_UNAVAILABLE (503) The search service is unavailable; retry later.
62
+ * @example
63
+ * const results = await totalum.web.search('totalum app builder', { limit: 2 });
64
+ * //=>
65
+ * {
66
+ * "web": [
67
+ * {
68
+ * "title": "Totalum - The most powerful AI app builder for humans and for agents",
69
+ * "url": "https://www.totalum.app/en",
70
+ * "snippet": "Create any web or app. The first AI app builder you can use via API and MCP…",
71
+ * "position": 1
72
+ * },
73
+ * {
74
+ * "title": "Totalum Blog: AI app builder, AI coding agents, MCP guides",
75
+ * "url": "https://www.totalum.app/blog",
76
+ * "snippet": "An AI app builder that generates real code lets you export and own the source…",
77
+ * "position": 2
78
+ * }
79
+ * ],
80
+ * "usage": { "credits": 0.0685 }
81
+ * }
82
+ */
19
83
  search: async (query, opts = {}) => withUsage(await call('POST', '/v1/web/search', { ...opts, query })),
20
- /** The URLs of a site (sitemap and links), without fetching the pages. */
84
+ /**
85
+ * Lists the URLs of a site (from its sitemap and links) without reading the pages — to choose what to scrape, or
86
+ * to build a site index.
87
+ *
88
+ * @param url - The site's `https://` URL.
89
+ * @param opts - A search filter and the maximum number of links.
90
+ * @returns The URLs found and the credits the call cost.
91
+ * @throws URL_NOT_ALLOWED (400) Not a public `http(s)` URL.
92
+ * @throws SCRAPE_FAILED (502) The site could not be mapped.
93
+ * @throws PROVIDER_UNAVAILABLE (503) The scraping service is unavailable; retry later.
94
+ * @example
95
+ * const { links } = await totalum.web.map('https://docs.example.com', { search: 'pricing', limit: 5 });
96
+ * //=>
97
+ * {
98
+ * "links": ["https://docs.example.com/pricing", "https://docs.example.com/pricing/teams"],
99
+ * "usage": { "credits": 0.0343 }
100
+ * }
101
+ */
21
102
  map: async (url, opts = {}) => withUsage(await call('POST', '/v1/web/map', { ...opts, url })),
22
- /** `202 {jobId}`: poll `crawlStatus` until the job leaves `running`; that read settles its charge. */
103
+ /**
104
+ * Starts reading a whole site (or part of it) in the background and returns a job id at once. Store the id, then
105
+ * call `crawlStatus(jobId)` (from a later request or a cron job) until `status` is no longer `running`; the job
106
+ * is charged by the pages read, settled on that read. The plan caps the pages per crawl: 20 on Free, 500 on
107
+ * Starter, 2,000 on Builder, 10,000 on Professional and 50,000 on Enterprise.
108
+ *
109
+ * @param url - The `https://` URL to start from.
110
+ * @param opts - How many pages, how deep, which paths, and how to read each page.
111
+ * @returns The job id and the credits reserved for it (the real charge is settled when it ends).
112
+ * @throws VALIDATION_ERROR (400) `limit` is above the plan's pages per crawl; `details.maxPages`.
113
+ * @throws URL_NOT_ALLOWED (400) Not a public `http(s)` URL.
114
+ * @throws SCRAPE_FAILED (502) The crawl could not be started.
115
+ * @throws PROVIDER_UNAVAILABLE (503) The scraping service is unavailable; retry later.
116
+ * @example
117
+ * const { jobId } = await totalum.web.crawl('https://docs.example.com', { limit: 50, includePaths: ['/guides/.*'] });
118
+ * // save jobId, then poll crawlStatus(jobId) later
119
+ * //=>
120
+ * { "jobId": "01M4P1C8Z3X7Y2W5V9T6S4R0QA", "estimatedCredits": 1.716 }
121
+ */
23
122
  crawl: async (url, opts = {}) => (await call('POST', '/v1/web/crawl', { ...opts, url })).data,
24
- /** The crawl job: `status`, pages so far and their content. */
123
+ /**
124
+ * Reads a crawl job: its status, how many pages are done and their content. Poll it every few seconds or from a cron
125
+ * job until `status` is `completed`, `failed`, `cancelled` or `expired`. Reading a finished job settles its charge.
126
+ *
127
+ * @param jobId - The `jobId` from `crawl()`.
128
+ * @returns The job's progress and the pages read so far.
129
+ * @throws JOB_NOT_FOUND (404) No such job for this project (job ids are kept 7 days).
130
+ * @throws SCRAPE_FAILED (502) The job could not be read; retry.
131
+ * @example
132
+ * const job = await totalum.web.crawlStatus('01M4P1C8Z3X7Y2W5V9T6S4R0QA');
133
+ * if (job.status === 'completed') for (const page of job.data) await savePage(page.url, page.markdown);
134
+ * //=>
135
+ * {
136
+ * "jobId": "01M4P1C8Z3X7Y2W5V9T6S4R0QA",
137
+ * "status": "completed",
138
+ * "total": 2,
139
+ * "completed": 2,
140
+ * "creditsUsed": 2,
141
+ * "data": [
142
+ * { "url": "https://docs.example.com/guides/start", "statusCode": 200, "metadata": { "sourceURL": "https://docs.example.com/guides/start", "title": "Getting started" }, "markdown": "# Getting started\n…" },
143
+ * { "url": "https://docs.example.com/guides/deploy", "statusCode": 200, "metadata": { "sourceURL": "https://docs.example.com/guides/deploy", "title": "Deploy" }, "markdown": "# Deploy\n…" }
144
+ * ]
145
+ * }
146
+ */
25
147
  crawlStatus: async (jobId) => (await call('GET', job(jobId))).data,
26
- /** Stops a running crawl; pages already crawled are charged. */
148
+ /**
149
+ * Stops a running crawl. Pages already read are kept in the answer and charged.
150
+ *
151
+ * @param jobId - The `jobId` from `crawl()`.
152
+ * @returns The job, now `cancelled`, with the pages read before it stopped.
153
+ * @throws JOB_NOT_FOUND (404) No such job for this project.
154
+ * @example
155
+ * const job = await totalum.web.cancelCrawl('01M4P1C8Z3X7Y2W5V9T6S4R0QA');
156
+ * //=>
157
+ * { "jobId": "01M4P1C8Z3X7Y2W5V9T6S4R0QA", "status": "cancelled", "total": 50, "completed": 12, "creditsUsed": 12, "data": […] }
158
+ */
27
159
  cancelCrawl: async (jobId) => (await call('DELETE', job(jobId))).data,
28
- /** A screenshot of a `url` or of inline `html`, stored as a project file (`key`, `url`). */
160
+ /**
161
+ * Takes a screenshot of a web page (`url`) or of HTML you pass (`html`) and stores it as a public project file:
162
+ * link previews, social images, visual checks.
163
+ *
164
+ * @param input - What to capture and how.
165
+ * @returns The stored image's `key` and `url`, its format, and the credits it cost.
166
+ * @throws URL_NOT_ALLOWED (400) Not a public `http(s)` URL.
167
+ * @throws SCREENSHOT_FAILED (502) The page could not be captured.
168
+ * @throws VALIDATION_ERROR (400) Neither `url` nor `html` was given.
169
+ * @example
170
+ * const shot = await totalum.web.screenshot({
171
+ * html: '<div style="font:48px sans-serif;padding:40px">Launch day 🚀</div>',
172
+ * viewport: { width: 1200, height: 630 },
173
+ * });
174
+ * //=>
175
+ * {
176
+ * "key": "p/acme-crm/01M4P0SFVZXNRS871ZSKZNWHY0.png",
177
+ * "url": "https://files.totalum-project.com/p/acme-crm/01M4P0SFVZXNRS871ZSKZNWHY0.png",
178
+ * "format": "png",
179
+ * "usage": { "credits": 0.0002 }
180
+ * }
181
+ */
29
182
  screenshot: async (input) => withUsage(await call('POST', '/v1/web/screenshot', input)),
30
183
  };
31
184
  }