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.
- package/CHANGELOG.md +20 -0
- package/dist/_types/{coerce.d-BKhuaDya.d.ts → coerce.d-Cp5NZQN-.d.ts} +1 -1
- package/dist/_types/{errors.d-RTRkidv5.d.ts → errors.d-ve4rY-Hx.d.ts} +37 -5
- package/dist/_types/{integrations.d-CzTa9T-L.d.ts → integrations.d-BRhNKT7K.d.ts} +2 -2
- package/dist/_types/{ops.d-CNsrT4Ma.d.ts → ops.d-DIC4u0go.d.ts} +2 -2
- package/dist/ai/index.d.ts +275 -23
- package/dist/ai/index.js +305 -24
- package/dist/analytics/index.d.ts +58 -12
- package/dist/analytics/index.js +46 -3
- package/dist/browser/index.d.ts +228 -20
- package/dist/browser/index.js +122 -13
- package/dist/connections/index.d.ts +305 -23
- package/dist/connections/index.js +302 -21
- package/dist/cron/index.d.ts +147 -20
- package/dist/cron/index.js +157 -15
- package/dist/d1/errors.js +15 -0
- package/dist/d1/index.d.ts +254 -10
- package/dist/d1/index.js +24 -4
- package/dist/d1/session.js +26 -2
- package/dist/email/index.d.ts +65 -10
- package/dist/email/index.js +60 -5
- package/dist/errors.js +17 -0
- package/dist/files/index.d.ts +243 -20
- package/dist/files/index.js +247 -14
- package/dist/index.d.ts +25 -10
- package/dist/index.js +19 -5
- package/dist/logs/index.d.ts +48 -10
- package/dist/logs/index.js +39 -4
- package/dist/payments/index.d.ts +221 -23
- package/dist/payments/index.js +216 -18
- package/dist/pdf/index.d.ts +59 -9
- package/dist/pdf/index.js +54 -4
- package/dist/react/index.d.ts +78 -6
- package/dist/react/index.js +51 -5
- package/dist/realtime/index.d.ts +55 -8
- package/dist/realtime/index.js +53 -6
- package/dist/scan/index.d.ts +78 -10
- package/dist/scan/index.js +63 -3
- package/dist/seo/index.d.ts +1118 -8
- package/dist/seo/index.js +325 -4
- package/dist/speech/index.d.ts +34 -10
- package/dist/speech/index.js +25 -5
- package/dist/vectors/index.d.ts +96 -9
- package/dist/vectors/index.js +93 -7
- package/dist/web/index.d.ts +168 -15
- package/dist/web/index.js +163 -10
- package/dist/webhooks/index.d.ts +122 -15
- package/dist/webhooks/index.js +119 -13
- package/package.json +2 -1
- package/totalum-sdk.md +10336 -754
- package/totalum-sdk.signatures.json +69 -1
package/dist/ai/index.js
CHANGED
|
@@ -83,30 +83,89 @@ function chatStream(body) {
|
|
|
83
83
|
};
|
|
84
84
|
}
|
|
85
85
|
/**
|
|
86
|
-
* `
|
|
86
|
+
* Creates the `ai` namespace (the same object as `totalum.ai`, scope `ai`): a `model` is a preset (`fast`,
|
|
87
87
|
* `smart`, `reasoning`, `vision`, `image`, `image-edit`, `video`, `tts`, `stt`, `embed` — each follows the newest model
|
|
88
|
-
* of its family, resolved by Totalum) or a pinned model id; every call
|
|
89
|
-
*
|
|
90
|
-
*
|
|
88
|
+
* of its family, resolved by Totalum) or a pinned model id; every call returns the Totalum credits it was charged
|
|
89
|
+
* (`usage.credits`, never tokens). There is no `ai.agent()`: an agent loop runs on an OpenAI-compatible library (the
|
|
90
|
+
* template's `src/agents/` uses the Vercel AI SDK) pointed at `gatewayConfig()`, which reaches the same models with the
|
|
91
|
+
* same key, billed in credits. Vector search is `totalum.vectors`.
|
|
91
92
|
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
93
|
+
* @param options - The project key, API URL, environment and AI gateway URL; each falls back to its environment
|
|
94
|
+
* variable.
|
|
95
|
+
* @returns The `ai` namespace: the methods below.
|
|
96
|
+
* @example
|
|
97
|
+
* import { totalumAi } from 'totalum-sdk/ai';
|
|
98
|
+
*
|
|
99
|
+
* const ai = totalumAi();
|
|
100
|
+
* const { text } = await ai.chat({ messages: [{ role: 'user', content: 'Hello' }] });
|
|
95
101
|
*/
|
|
96
102
|
export function totalumAi(options = {}) {
|
|
97
103
|
const call = jsonClient(options);
|
|
98
104
|
const raw = rawClient(options);
|
|
99
105
|
const speech = totalumSpeech(options);
|
|
100
106
|
/**
|
|
101
|
-
*
|
|
107
|
+
* Asks a model and waits for the whole answer: summaries, replies, rewriting, extraction, tool calling. `model` is a
|
|
108
|
+
* preset (`fast`, `smart` — the default — or `reasoning`) or a pinned model id from `models.list()`; `messages` is
|
|
109
|
+
* the conversation in the OpenAI format. Billed in credits by the model's use. To show the answer while it is
|
|
110
|
+
* written, use `chat.stream`.
|
|
102
111
|
*
|
|
112
|
+
* @param req - The model, the conversation and the generation options.
|
|
113
|
+
* @returns The answer's text, any tool calls, why it stopped, and what it cost.
|
|
114
|
+
* @throws AI_MODEL_UNKNOWN (400) No such preset or model id.
|
|
115
|
+
* @throws AI_MODEL_NOT_ALLOWED (403) The model is not available on this plan; `details` says why.
|
|
116
|
+
* @throws AI_PROVIDER_UNAVAILABLE (502) The model did not answer; retry, or use another model.
|
|
117
|
+
* @example
|
|
118
|
+
* const reply = await totalum.ai.chat({
|
|
119
|
+
* model: 'fast',
|
|
120
|
+
* messages: [
|
|
121
|
+
* { role: 'system', content: 'Answer in one word.' },
|
|
122
|
+
* { role: 'user', content: 'What is the capital of France?' },
|
|
123
|
+
* ],
|
|
124
|
+
* });
|
|
125
|
+
* //=>
|
|
126
|
+
* {
|
|
127
|
+
* "id": "gen-1791740364-UbQ0IKv75XZajyP5We6y",
|
|
128
|
+
* "model": "openai/gpt-6-luna",
|
|
129
|
+
* "text": "Paris",
|
|
130
|
+
* "toolCalls": [],
|
|
131
|
+
* "finishReason": "stop",
|
|
132
|
+
* "usage": { "credits": 0.0007, "inputTokens": 0, "outputTokens": 0 },
|
|
133
|
+
* "raw": { "id": "gen-1791740364-UbQ0IKv75XZajyP5We6y", "object": "chat.completion", "choices": [ … ], … }
|
|
134
|
+
* }
|
|
103
135
|
* @example
|
|
104
|
-
*
|
|
136
|
+
* // tool calling: the model asks for getWeather, you run it and send the answer back
|
|
137
|
+
* const first = await totalum.ai.chat({
|
|
138
|
+
* messages: [{ role: 'user', content: 'Weather in Madrid?' }],
|
|
139
|
+
* tools: [{ type: 'function', function: { name: 'getWeather', parameters: { type: 'object', properties: { city: { type: 'string' } }, required: ['city'] } } }],
|
|
140
|
+
* });
|
|
141
|
+
* //=>
|
|
142
|
+
* { "text": "", "toolCalls": [{ "id": "call_8f2k", "type": "function", "function": { "name": "getWeather", "arguments": "{\"city\":\"Madrid\"}" } }], "finishReason": "tool_calls", … }
|
|
105
143
|
*/
|
|
106
144
|
const chat = Object.assign(async (req) => resultOf(await call('POST', '/v1/ai/chat', { ...req, stream: false })), {
|
|
107
145
|
/**
|
|
108
|
-
* The same request streamed:
|
|
109
|
-
* `
|
|
146
|
+
* The same request, streamed: the answer arrives piece by piece, for a chat UI that types as it goes. `raw` is
|
|
147
|
+
* the OpenAI-format server-sent events (return it from a route), `text()` the text pieces, `result()` the final
|
|
148
|
+
* result with its credits.
|
|
149
|
+
*
|
|
150
|
+
* @param req - The same input as `chat()`.
|
|
151
|
+
* @returns The stream handle: `raw`, `text()` and `result()`.
|
|
152
|
+
* @throws AI_STREAM_ERROR (502) The model returned no stream.
|
|
153
|
+
* @throws AI_MODEL_UNKNOWN (400) No such preset or model id.
|
|
154
|
+
* @throws AI_MODEL_NOT_ALLOWED (403) The model is not available on this plan.
|
|
155
|
+
* @example
|
|
156
|
+
* // src/app/api/chat/route.ts — stream the answer to the browser
|
|
157
|
+
* export async function POST(request: Request) {
|
|
158
|
+
* const { messages } = await request.json();
|
|
159
|
+
* const stream = await totalum.ai.chat.stream({ model: 'fast', messages });
|
|
160
|
+
* return new Response(stream.raw, { headers: { 'content-type': 'text/event-stream' } });
|
|
161
|
+
* }
|
|
162
|
+
* @example
|
|
163
|
+
* const stream = await totalum.ai.chat.stream({ model: 'fast', messages: [{ role: 'user', content: 'Count 1 to 3.' }] });
|
|
164
|
+
* let text = '';
|
|
165
|
+
* for await (const piece of stream.text()) text += piece;
|
|
166
|
+
* const { usage } = await stream.result();
|
|
167
|
+
* //=>
|
|
168
|
+
* text = "1, 2, 3." usage = { credits: 0.0005, inputTokens: 0, outputTokens: 0 }
|
|
110
169
|
*/
|
|
111
170
|
stream: async (req) => {
|
|
112
171
|
const res = await raw('/v1/ai/chat', { ...req, stream: true });
|
|
@@ -115,13 +174,79 @@ export function totalumAi(options = {}) {
|
|
|
115
174
|
return chatStream(res.body);
|
|
116
175
|
},
|
|
117
176
|
});
|
|
177
|
+
/**
|
|
178
|
+
* Reads a video job: its status and, once `completed`, the stored clip (`video.key`, `video.url`) and its final cost.
|
|
179
|
+
* Call it when the user opens the page or from a cron job until the status is no longer `queued`/`running`.
|
|
180
|
+
*
|
|
181
|
+
* @param jobId - The `jobId` from `video.generate()`.
|
|
182
|
+
* @returns The job's status, and the clip when done.
|
|
183
|
+
* @throws AI_VIDEO_JOB_NOT_FOUND (404) No such job in this project.
|
|
184
|
+
* @throws AI_PROVIDER_UNAVAILABLE (503) The video service could not be reached; retry.
|
|
185
|
+
* @example
|
|
186
|
+
* const job = await totalum.ai.video.get('vid_8m2k4n6p8q0r');
|
|
187
|
+
* //=>
|
|
188
|
+
* {
|
|
189
|
+
* "jobId": "vid_8m2k4n6p8q0r",
|
|
190
|
+
* "status": "completed",
|
|
191
|
+
* "video": { "key": "p/acme-crm/01M4P1M5N6P7Q8R9S0T1U2V3WX.mp4", "url": "https://files.totalum-project.com/p/acme-crm/01M4P1M5N6P7Q8R9S0T1U2V3WX.mp4" },
|
|
192
|
+
* "usage": { "credits": 19.8 }
|
|
193
|
+
* }
|
|
194
|
+
*/
|
|
118
195
|
const videoJob = (jobId) => call('GET', `/v1/ai/video/${encodeURIComponent(jobId)}`);
|
|
119
|
-
/**
|
|
196
|
+
/**
|
|
197
|
+
* Asks a model for a JSON object that matches your JSON Schema — extract fields from text, classify with details,
|
|
198
|
+
* produce data for your UI. The output is checked against the schema (and repaired when slightly off). Pass
|
|
199
|
+
* `prompt` or `messages`.
|
|
200
|
+
*
|
|
201
|
+
* @param req - The schema, the prompt (or messages) and the model.
|
|
202
|
+
* @param req.schema - The JSON Schema of the object (at most 32 KB); use `additionalProperties: false` and `required`.
|
|
203
|
+
* @param req.prompt - The instruction and the input, as one text (or pass `messages`).
|
|
204
|
+
* @param req.messages - The conversation, instead of `prompt`.
|
|
205
|
+
* @param req.model - A preset or model id. Default `smart`.
|
|
206
|
+
* @param req.healing - `false` turns off the automatic repair of almost-valid JSON. Default `true`.
|
|
207
|
+
* @returns The object and what it cost.
|
|
208
|
+
* @throws AI_INVALID_SCHEMA (502) The model did not answer JSON for the schema; `details.usage` says what was charged.
|
|
209
|
+
* @throws AI_MODEL_UNKNOWN (400) No such preset or model id.
|
|
210
|
+
* @throws AI_PROVIDER_UNAVAILABLE (502) The model did not answer; retry.
|
|
211
|
+
* @example
|
|
212
|
+
* const { object } = await totalum.ai.generateObject({
|
|
213
|
+
* model: 'fast',
|
|
214
|
+
* prompt: 'Ana is 31 and lives in Madrid.',
|
|
215
|
+
* schema: {
|
|
216
|
+
* type: 'object',
|
|
217
|
+
* properties: { name: { type: 'string' }, age: { type: 'number' }, city: { type: 'string' } },
|
|
218
|
+
* required: ['name', 'age', 'city'],
|
|
219
|
+
* additionalProperties: false,
|
|
220
|
+
* },
|
|
221
|
+
* });
|
|
222
|
+
* //=>
|
|
223
|
+
* { "object": { "name": "Ana", "age": 31, "city": "Madrid" }, "usage": { "credits": 0.0014 } }
|
|
224
|
+
*/
|
|
120
225
|
const generateObject = (req) => call('POST', '/v1/ai/object', req);
|
|
121
226
|
return {
|
|
122
227
|
chat,
|
|
123
228
|
generateObject,
|
|
124
|
-
/**
|
|
229
|
+
/**
|
|
230
|
+
* Picks one label out of your list for an input — route a support ticket, tag a review, detect a language. Sugar
|
|
231
|
+
* over `generateObject`.
|
|
232
|
+
*
|
|
233
|
+
* @param req - The input, the labels and optional instructions.
|
|
234
|
+
* @param req.input - The text to classify.
|
|
235
|
+
* @param req.options - The labels to choose from.
|
|
236
|
+
* @param req.model - A preset or model id. Default `smart`.
|
|
237
|
+
* @param req.instructions - How to choose, in words. Default: "Classify the input into exactly one label."
|
|
238
|
+
* @returns `{ label }`: one of `options` (or `''` if the model gave none).
|
|
239
|
+
* @throws AI_INVALID_SCHEMA (502) The model did not answer a label.
|
|
240
|
+
* @throws AI_MODEL_UNKNOWN (400) No such preset or model id.
|
|
241
|
+
* @example
|
|
242
|
+
* const { label } = await totalum.ai.classify({
|
|
243
|
+
* input: 'My card was charged twice',
|
|
244
|
+
* options: ['billing', 'bug', 'other'],
|
|
245
|
+
* model: 'fast',
|
|
246
|
+
* });
|
|
247
|
+
* //=>
|
|
248
|
+
* { "label": "billing" }
|
|
249
|
+
*/
|
|
125
250
|
async classify(req) {
|
|
126
251
|
const { object } = await generateObject({
|
|
127
252
|
...(req.model ? { model: req.model } : {}),
|
|
@@ -134,25 +259,117 @@ export function totalumAi(options = {}) {
|
|
|
134
259
|
},
|
|
135
260
|
});
|
|
136
261
|
const label = object?.label;
|
|
137
|
-
return {
|
|
262
|
+
return {
|
|
263
|
+
/** The chosen label: one of `options`, or `''` when the model gave none. */
|
|
264
|
+
label: typeof label === 'string' ? label : '',
|
|
265
|
+
};
|
|
138
266
|
},
|
|
139
|
-
/**
|
|
267
|
+
/**
|
|
268
|
+
* Turns texts into embedding vectors (lists of numbers whose distance measures meaning). To store and search them,
|
|
269
|
+
* `totalum.vectors` does the embedding for you; use this for your own similarity maths.
|
|
270
|
+
*
|
|
271
|
+
* @param req - The texts and the model.
|
|
272
|
+
* @returns One vector per input, in order, and what it cost.
|
|
273
|
+
* @throws AI_MODEL_UNKNOWN (400) No such preset or model id.
|
|
274
|
+
* @throws AI_PROVIDER_UNAVAILABLE (502) The model did not answer; retry.
|
|
275
|
+
* @example
|
|
276
|
+
* const { embeddings } = await totalum.ai.embed({ input: ['red shoes', 'blue sandals'] });
|
|
277
|
+
* //=>
|
|
278
|
+
* { "embeddings": [[0.0138, -0.0121, 0.0125, …], [0.0042, -0.0337, 0.0198, …]], "usage": { "credits": 0.0001 } }
|
|
279
|
+
*/
|
|
140
280
|
embed: (req) => call('POST', '/v1/ai/embed', req),
|
|
141
281
|
image: {
|
|
142
|
-
/**
|
|
282
|
+
/**
|
|
283
|
+
* Generates images from a prompt — illustrations, product shots, avatars, hero images — and stores each one as a
|
|
284
|
+
* public project file. Generate once and save the `key`: never generate on every page view.
|
|
285
|
+
*
|
|
286
|
+
* @param req - The prompt, the model, the size and how many.
|
|
287
|
+
* @returns The stored images and what they cost.
|
|
288
|
+
* @throws AI_MODEL_UNKNOWN (400) No such preset or model id.
|
|
289
|
+
* @throws AI_MODEL_NOT_ALLOWED (403) The model is not available on this plan.
|
|
290
|
+
* @throws AI_PROVIDER_UNAVAILABLE (502) The model returned no image; retry or change the prompt.
|
|
291
|
+
* @example
|
|
292
|
+
* const { images } = await totalum.ai.image.generate({
|
|
293
|
+
* prompt: 'A watercolor fox reading a book, warm light',
|
|
294
|
+
* aspectRatio: '16:9',
|
|
295
|
+
* });
|
|
296
|
+
* //=>
|
|
297
|
+
* {
|
|
298
|
+
* "images": [
|
|
299
|
+
* { "key": "p/acme-crm/01M4P1J2K3L4M5N6P7Q8R9S0TV.png", "url": "https://files.totalum-project.com/p/acme-crm/01M4P1J2K3L4M5N6P7Q8R9S0TV.png", "mediaType": "image/png", "width": 1344, "height": 768 }
|
|
300
|
+
* ],
|
|
301
|
+
* "usage": { "credits": 0.2741 },
|
|
302
|
+
* "fileName": "01M4P1J2K3L4M5N6P7Q8R9S0TV.png",
|
|
303
|
+
* "imageUrl": "https://files.totalum-project.com/p/acme-crm/01M4P1J2K3L4M5N6P7Q8R9S0TV.png"
|
|
304
|
+
* }
|
|
305
|
+
*/
|
|
143
306
|
generate: (req) => call('POST', '/v1/ai/image', req),
|
|
144
|
-
/**
|
|
307
|
+
/**
|
|
308
|
+
* Edits images with a prompt — remove a background, change a colour, put a product in a new scene — and stores
|
|
309
|
+
* the result as a public project file.
|
|
310
|
+
*
|
|
311
|
+
* @param req - The instruction and the images to edit.
|
|
312
|
+
* @returns The stored result image(s) and what they cost.
|
|
313
|
+
* @throws AI_MODEL_UNKNOWN (400) No such preset or model id.
|
|
314
|
+
* @throws FILE_NOT_FOUND (404) An input image key does not exist.
|
|
315
|
+
* @throws AI_PROVIDER_UNAVAILABLE (502) The model returned no image; retry.
|
|
316
|
+
* @example
|
|
317
|
+
* const { images } = await totalum.ai.image.edit({
|
|
318
|
+
* prompt: 'Put this sneaker on a white marble pedestal, studio light',
|
|
319
|
+
* images: [product.photo.url],
|
|
320
|
+
* });
|
|
321
|
+
* //=>
|
|
322
|
+
* {
|
|
323
|
+
* "images": [{ "key": "p/acme-crm/01M4P1K3L4M5N6P7Q8R9S0T1UW.png", "url": "https://files.totalum-project.com/p/acme-crm/01M4P1K3L4M5N6P7Q8R9S0T1UW.png", "mediaType": "image/png" }],
|
|
324
|
+
* "usage": { "credits": 0.3012 },
|
|
325
|
+
* "fileName": "01M4P1K3L4M5N6P7Q8R9S0T1UW.png",
|
|
326
|
+
* "imageUrl": "https://files.totalum-project.com/p/acme-crm/01M4P1K3L4M5N6P7Q8R9S0T1UW.png"
|
|
327
|
+
* }
|
|
328
|
+
*/
|
|
145
329
|
edit: (req) => call('POST', '/v1/ai/image/edit', req),
|
|
146
330
|
},
|
|
147
331
|
video: {
|
|
148
|
-
/**
|
|
332
|
+
/**
|
|
333
|
+
* Starts generating a video clip (up to 30 s) from a prompt, optionally from a first/last frame or reference
|
|
334
|
+
* images. It takes minutes: store the `jobId` and read `get(jobId)` later (a page refresh, a cron job); the clip
|
|
335
|
+
* is stored as a project file when it completes.
|
|
336
|
+
*
|
|
337
|
+
* @param req - The prompt, the duration and the look.
|
|
338
|
+
* @returns The job id and the credits reserved for it (settled when it ends).
|
|
339
|
+
* @throws AI_MODEL_UNKNOWN (400) No such preset or model id.
|
|
340
|
+
* @throws AI_VIDEO_FAILED (502) The video could not be started.
|
|
341
|
+
* @throws AI_MEDIA_PAUSED_THIS_HOUR (429) Image, video and speech AI are paused until the hour ends.
|
|
342
|
+
* @example
|
|
343
|
+
* const job = await totalum.ai.video.generate({
|
|
344
|
+
* prompt: 'A paper boat sailing down a rainy street, cinematic',
|
|
345
|
+
* durationSec: 5,
|
|
346
|
+
* aspectRatio: '16:9',
|
|
347
|
+
* });
|
|
348
|
+
* // save job.jobId on the row, then read ai.video.get(jobId) later
|
|
349
|
+
* //=>
|
|
350
|
+
* { "jobId": "vid_8m2k4n6p8q0r", "status": "queued", "estimatedCredits": 21.5 }
|
|
351
|
+
*/
|
|
149
352
|
generate: (req) => call('POST', '/v1/ai/video', req),
|
|
150
|
-
/** The video job: `status`, and the stored clip once completed. */
|
|
151
353
|
get: videoJob,
|
|
152
354
|
/**
|
|
153
355
|
* ⚠ NODE-ONLY: polls `get(jobId)` until the job leaves `queued`/`running` (default every 5 s, at most 45 min —
|
|
154
|
-
* the longest clip, 30 s at 1080p, renders in about 22 min).
|
|
155
|
-
*
|
|
356
|
+
* the longest clip, 30 s at 1080p, renders in about 22 min). For scripts (`npx tsx scripts/…`); in a Worker (the
|
|
357
|
+
* preview and the published app) it throws `NODE_ONLY` — store the `jobId` and read `get(jobId)` later instead.
|
|
358
|
+
*
|
|
359
|
+
* @param jobId - The `jobId` from `video.generate()`.
|
|
360
|
+
* @param opts - How often and how long to poll.
|
|
361
|
+
* @param opts.intervalMs - Milliseconds between polls. Default 5000.
|
|
362
|
+
* @param opts.timeoutMs - Stop polling after this long and return the job as it is. Default 2700000 (45 min).
|
|
363
|
+
* @returns The job once it finished (or when the timeout passed).
|
|
364
|
+
* @throws NODE_ONLY Called inside a Worker; `details.replacement` says what to do instead.
|
|
365
|
+
* @throws AI_VIDEO_JOB_NOT_FOUND (404) No such job in this project.
|
|
366
|
+
* @example
|
|
367
|
+
* // scripts/make-intro-video.ts, run with: npx tsx scripts/make-intro-video.ts
|
|
368
|
+
* const { jobId } = await totalum.ai.video.generate({ prompt: 'Logo reveal on black', durationSec: 5 });
|
|
369
|
+
* const job = await totalum.ai.video.wait(jobId);
|
|
370
|
+
* console.log(job.status, job.video?.url);
|
|
371
|
+
* //=>
|
|
372
|
+
* completed https://files.totalum-project.com/p/acme-crm/01M4P1M5N6P7Q8R9S0T1U2V3WX.mp4
|
|
156
373
|
*/
|
|
157
374
|
async wait(jobId, opts = {}) {
|
|
158
375
|
assertNode('totalum.ai.video.wait()', 'Start the job with ai.video.generate(), store the jobId, and read ai.video.get(jobId) when the page is refreshed.', 'https://totalum.app/docs/sdk/ai#node-only');
|
|
@@ -166,7 +383,23 @@ export function totalumAi(options = {}) {
|
|
|
166
383
|
},
|
|
167
384
|
},
|
|
168
385
|
vision: {
|
|
169
|
-
/**
|
|
386
|
+
/**
|
|
387
|
+
* Describes an image in words, or answers a question about it — alt text, moderation, reading a chart, checking a
|
|
388
|
+
* photo. The image can be a project file key, a files URL, an `https://` URL or a data URL.
|
|
389
|
+
*
|
|
390
|
+
* @param req - The image, the question and the model.
|
|
391
|
+
* @returns The answer's text and what it cost.
|
|
392
|
+
* @throws FILE_NOT_FOUND (404) The image key does not exist.
|
|
393
|
+
* @throws AI_PROVIDER_UNAVAILABLE (502) The model could not read the image (for example the URL refused the
|
|
394
|
+
* download); retry with a project file.
|
|
395
|
+
* @example
|
|
396
|
+
* const { text } = await totalum.ai.vision.describe({
|
|
397
|
+
* image: 'p/acme-crm/01M4P1J2K3L4M5N6P7Q8R9S0TV.png',
|
|
398
|
+
* prompt: 'Write alt text for this image in one sentence.',
|
|
399
|
+
* });
|
|
400
|
+
* //=>
|
|
401
|
+
* { "text": "A watercolor fox sits under a lamp reading an open book.", "usage": { "credits": 0.0019 } }
|
|
402
|
+
*/
|
|
170
403
|
describe: (req) => call('POST', '/v1/ai/vision/describe', req),
|
|
171
404
|
},
|
|
172
405
|
/** @deprecated Since 1.0.0: use `totalum.speech` (the same calls). */
|
|
@@ -177,10 +410,58 @@ export function totalumAi(options = {}) {
|
|
|
177
410
|
transcribe: alias(speech.transcribe, 'ai.speech.transcribe()', 'totalum.speech.transcribe()'),
|
|
178
411
|
},
|
|
179
412
|
models: {
|
|
180
|
-
/**
|
|
413
|
+
/**
|
|
414
|
+
* Each preset's current model and the catalogue of models this project may call (their ids, names, inputs and
|
|
415
|
+
* context sizes) — to let an admin pick a model, or to pin one. Free.
|
|
416
|
+
*
|
|
417
|
+
* @returns The presets and the model catalogue.
|
|
418
|
+
* @throws AI_PROVIDER_UNAVAILABLE (503) The model catalogue is unavailable; retry.
|
|
419
|
+
* @example
|
|
420
|
+
* const { presets, models } = await totalum.ai.models.list();
|
|
421
|
+
* //=>
|
|
422
|
+
* {
|
|
423
|
+
* "presets": {
|
|
424
|
+
* "fast": "openai/gpt-6-luna",
|
|
425
|
+
* "smart": "openai/gpt-6.1-sol",
|
|
426
|
+
* "reasoning": "anthropic/claude-opus-5.5",
|
|
427
|
+
* "vision": "openai/gpt-6-luna",
|
|
428
|
+
* "image": "microsoft/mai-image-2.6-flash",
|
|
429
|
+
* "image-edit": "microsoft/mai-image-2.6-flash",
|
|
430
|
+
* "video": "alibaba/wan-3.0",
|
|
431
|
+
* "tts": "microsoft/mai-voice-2.1-flash",
|
|
432
|
+
* "stt": "openai/whisper-large-v3-turbo",
|
|
433
|
+
* "embed": "openai/text-embedding-3-small",
|
|
434
|
+
* "embed-image": "google/gemini-embedding-2"
|
|
435
|
+
* },
|
|
436
|
+
* "models": [
|
|
437
|
+
* { "slug": "anthropic/claude-haiku-5.5", "name": "Anthropic: Claude Haiku 5.5", "inputModalities": ["text", "image", "file"], "pricing": {}, "contextLength": 1000000 },
|
|
438
|
+
* …
|
|
439
|
+
* ]
|
|
440
|
+
* }
|
|
441
|
+
*/
|
|
181
442
|
list: () => call('GET', '/v1/ai/models'),
|
|
182
443
|
},
|
|
183
|
-
/**
|
|
444
|
+
/**
|
|
445
|
+
* The URL and key of Totalum's OpenAI-compatible AI gateway, for any library that takes an OpenAI client: the
|
|
446
|
+
* Vercel AI SDK, `openai`, LangChain. Same models, same key, billed in credits. Server-side only: the key is the
|
|
447
|
+
* project key. No request is made.
|
|
448
|
+
*
|
|
449
|
+
* @returns `{ baseURL, apiKey }` to pass to the client.
|
|
450
|
+
* @throws SDK_NOT_CONFIGURED No project key is set.
|
|
451
|
+
* @example
|
|
452
|
+
* import OpenAI from 'openai';
|
|
453
|
+
*
|
|
454
|
+
* const openai = new OpenAI(totalum.ai.gatewayConfig());
|
|
455
|
+
* const r = await openai.chat.completions.create({ model: 'fast', messages: [{ role: 'user', content: 'Hi' }] });
|
|
456
|
+
* @example
|
|
457
|
+
* import { createOpenAI } from '@ai-sdk/openai';
|
|
458
|
+
* import { generateText } from 'ai';
|
|
459
|
+
*
|
|
460
|
+
* const gateway = createOpenAI(totalum.ai.gatewayConfig());
|
|
461
|
+
* const { text } = await generateText({ model: gateway('smart'), prompt: 'Write a haiku about Mondays' });
|
|
462
|
+
* //=>
|
|
463
|
+
* { "baseURL": "https://ai.totalum.app/api/v1", "apiKey": "tlm_pk_…" }
|
|
464
|
+
*/
|
|
184
465
|
gatewayConfig: () => ({
|
|
185
466
|
baseURL: options.aiBaseUrl ?? readEnv('TOTALUM_AI_BASE_URL') ?? 'https://ai.totalum.app/api/v1',
|
|
186
467
|
apiKey: projectKey(options),
|
|
@@ -1,31 +1,77 @@
|
|
|
1
|
-
import { b as SiteMetric, a as SiteDimension, S as SiteAnalyticsOutput } from '../_types/ops.d-
|
|
2
|
-
import { T as TotalumClientOptions } from '../_types/errors.d-
|
|
3
|
-
export { a as TotalumError, b as TotalumErrorCode, i as isTotalumError } from '../_types/errors.d-
|
|
4
|
-
import '../_types/coerce.d-
|
|
1
|
+
import { b as SiteMetric, a as SiteDimension, S as SiteAnalyticsOutput } from '../_types/ops.d-DIC4u0go.js';
|
|
2
|
+
import { T as TotalumClientOptions } from '../_types/errors.d-ve4rY-Hx.js';
|
|
3
|
+
export { a as TotalumError, b as TotalumErrorCode, i as isTotalumError } from '../_types/errors.d-ve4rY-Hx.js';
|
|
4
|
+
import '../_types/coerce.d-Cp5NZQN-.js';
|
|
5
5
|
|
|
6
|
+
/** What `analytics.site()` reads: the window, the metrics and, for a top list, the dimension; all optional. */
|
|
6
7
|
interface SiteAnalyticsQuery {
|
|
7
|
-
/** ISO; default
|
|
8
|
+
/** The window's start (ISO date-time); default 7 days ago. Raw hits are kept 90 days. */
|
|
8
9
|
from?: string;
|
|
10
|
+
/** The window's end (ISO date-time); default now. */
|
|
9
11
|
to?: string;
|
|
10
|
-
/** `minute` only within the last 30 minutes. */
|
|
12
|
+
/** The series' step: `minute` (only within the last 30 minutes), `hour` or `day` (default). */
|
|
11
13
|
granularity?: 'minute' | 'hour' | 'day';
|
|
12
|
-
/**
|
|
14
|
+
/** What to count: one metric or up to 4. Default `page_views`. */
|
|
13
15
|
metric?: SiteMetric | SiteMetric[];
|
|
14
|
-
/** Omit for a time series (`series`); set for a top
|
|
16
|
+
/** Omit for a time series (`series`); set for a top list by this dimension (`top`). */
|
|
15
17
|
dimension?: SiteDimension;
|
|
16
|
-
/**
|
|
18
|
+
/** With `dimension`: how many entries, at most 100. Default 10. */
|
|
17
19
|
limit?: number;
|
|
20
|
+
/** Bot traffic: `exclude` (default), `include` or `only`. */
|
|
18
21
|
bots?: 'exclude' | 'include' | 'only';
|
|
19
22
|
/** `dev` = the preview host, `live` = the published and custom hosts; omitted = both. */
|
|
20
23
|
env?: 'dev' | 'live';
|
|
21
24
|
}
|
|
22
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* Creates the `analytics` namespace (the same object as `totalum.analytics`, scope `analytics:read`): the site's own
|
|
27
|
+
* traffic. Free.
|
|
28
|
+
*
|
|
29
|
+
* @param options - The project key, API URL and environment; each falls back to its environment variable.
|
|
30
|
+
* @returns The `analytics` namespace: the method below.
|
|
31
|
+
* @example
|
|
32
|
+
* import { totalumAnalytics } from 'totalum-sdk/analytics';
|
|
33
|
+
*
|
|
34
|
+
* const analytics = totalumAnalytics();
|
|
35
|
+
* const { series } = await analytics.site({ metric: 'page_views', granularity: 'day' });
|
|
36
|
+
*/
|
|
23
37
|
declare function totalumAnalytics(options?: TotalumClientOptions): {
|
|
24
38
|
/**
|
|
25
|
-
*
|
|
39
|
+
* The site's traffic: page views, hits, unique visitors, errors, bytes or response time (TTFB), as a time series
|
|
40
|
+
* (no `dimension`) or a top list by path, referrer, country, device, browser, OS, status or hostname. Counts come
|
|
41
|
+
* from sampled hits, so very small numbers are approximate. Free.
|
|
26
42
|
*
|
|
43
|
+
* @param q - The window, metrics and dimension; all optional.
|
|
44
|
+
* @returns `series` (a time series) or `top` (a top list), the window used and when the numbers were computed.
|
|
45
|
+
* @throws ANALYTICS_INVALID_QUERY (400) `from` after `to`, `minute` over more than 30 minutes, or a bad metric.
|
|
46
|
+
* @throws ANALYTICS_WINDOW_OUT_OF_RETENTION (400) The window starts more than 90 days ago.
|
|
47
|
+
* @throws ANALYTICS_UPSTREAM_ERROR (502) Analytics are unavailable; retry shortly.
|
|
27
48
|
* @example
|
|
28
|
-
* const {
|
|
49
|
+
* const { series } = await totalum.analytics.site({ metric: ['page_views', 'uniques'], granularity: 'day' });
|
|
50
|
+
* //=>
|
|
51
|
+
* {
|
|
52
|
+
* "series": [
|
|
53
|
+
* { "t": "2026-10-09T00:00:00.000Z", "page_views": 412, "uniques": 128 },
|
|
54
|
+
* { "t": "2026-10-10T00:00:00.000Z", "page_views": 538, "uniques": 171 }
|
|
55
|
+
* ],
|
|
56
|
+
* "window": { "from": "2026-10-04T17:39:00.000Z", "to": "2026-10-11T17:39:00.000Z", "granularity": "day" },
|
|
57
|
+
* "source": "wae",
|
|
58
|
+
* "approximate": { "uniques": true },
|
|
59
|
+
* "cachedAt": "2026-10-11T17:38:40.720Z"
|
|
60
|
+
* }
|
|
61
|
+
* @example
|
|
62
|
+
* const { top } = await totalum.analytics.site({ dimension: 'path', metric: 'page_views', limit: 3 });
|
|
63
|
+
* //=>
|
|
64
|
+
* {
|
|
65
|
+
* "top": [
|
|
66
|
+
* { "key": "/", "share": 0.41, "page_views": 1210 },
|
|
67
|
+
* { "key": "/pricing", "share": 0.18, "page_views": 532 },
|
|
68
|
+
* { "key": "/blog/launch", "share": 0.07, "page_views": 207 }
|
|
69
|
+
* ],
|
|
70
|
+
* "window": { "from": "2026-10-04T17:39:00.000Z", "to": "2026-10-11T17:39:00.000Z", "granularity": "day" },
|
|
71
|
+
* "source": "wae",
|
|
72
|
+
* "approximate": { "uniques": false },
|
|
73
|
+
* "cachedAt": "2026-10-11T17:38:41.388Z"
|
|
74
|
+
* }
|
|
29
75
|
*/
|
|
30
76
|
site: (q?: SiteAnalyticsQuery) => Promise<SiteAnalyticsOutput>;
|
|
31
77
|
};
|
package/dist/analytics/index.js
CHANGED
|
@@ -1,14 +1,57 @@
|
|
|
1
1
|
import { jsonClient, query } from '../http.js';
|
|
2
2
|
export { TotalumError, isTotalumError } from '../errors.js';
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Creates the `analytics` namespace (the same object as `totalum.analytics`, scope `analytics:read`): the site's own
|
|
5
|
+
* traffic. Free.
|
|
6
|
+
*
|
|
7
|
+
* @param options - The project key, API URL and environment; each falls back to its environment variable.
|
|
8
|
+
* @returns The `analytics` namespace: the method below.
|
|
9
|
+
* @example
|
|
10
|
+
* import { totalumAnalytics } from 'totalum-sdk/analytics';
|
|
11
|
+
*
|
|
12
|
+
* const analytics = totalumAnalytics();
|
|
13
|
+
* const { series } = await analytics.site({ metric: 'page_views', granularity: 'day' });
|
|
14
|
+
*/
|
|
4
15
|
export function totalumAnalytics(options = {}) {
|
|
5
16
|
const call = jsonClient(options);
|
|
6
17
|
return {
|
|
7
18
|
/**
|
|
8
|
-
*
|
|
19
|
+
* The site's traffic: page views, hits, unique visitors, errors, bytes or response time (TTFB), as a time series
|
|
20
|
+
* (no `dimension`) or a top list by path, referrer, country, device, browser, OS, status or hostname. Counts come
|
|
21
|
+
* from sampled hits, so very small numbers are approximate. Free.
|
|
9
22
|
*
|
|
23
|
+
* @param q - The window, metrics and dimension; all optional.
|
|
24
|
+
* @returns `series` (a time series) or `top` (a top list), the window used and when the numbers were computed.
|
|
25
|
+
* @throws ANALYTICS_INVALID_QUERY (400) `from` after `to`, `minute` over more than 30 minutes, or a bad metric.
|
|
26
|
+
* @throws ANALYTICS_WINDOW_OUT_OF_RETENTION (400) The window starts more than 90 days ago.
|
|
27
|
+
* @throws ANALYTICS_UPSTREAM_ERROR (502) Analytics are unavailable; retry shortly.
|
|
10
28
|
* @example
|
|
11
|
-
* const {
|
|
29
|
+
* const { series } = await totalum.analytics.site({ metric: ['page_views', 'uniques'], granularity: 'day' });
|
|
30
|
+
* //=>
|
|
31
|
+
* {
|
|
32
|
+
* "series": [
|
|
33
|
+
* { "t": "2026-10-09T00:00:00.000Z", "page_views": 412, "uniques": 128 },
|
|
34
|
+
* { "t": "2026-10-10T00:00:00.000Z", "page_views": 538, "uniques": 171 }
|
|
35
|
+
* ],
|
|
36
|
+
* "window": { "from": "2026-10-04T17:39:00.000Z", "to": "2026-10-11T17:39:00.000Z", "granularity": "day" },
|
|
37
|
+
* "source": "wae",
|
|
38
|
+
* "approximate": { "uniques": true },
|
|
39
|
+
* "cachedAt": "2026-10-11T17:38:40.720Z"
|
|
40
|
+
* }
|
|
41
|
+
* @example
|
|
42
|
+
* const { top } = await totalum.analytics.site({ dimension: 'path', metric: 'page_views', limit: 3 });
|
|
43
|
+
* //=>
|
|
44
|
+
* {
|
|
45
|
+
* "top": [
|
|
46
|
+
* { "key": "/", "share": 0.41, "page_views": 1210 },
|
|
47
|
+
* { "key": "/pricing", "share": 0.18, "page_views": 532 },
|
|
48
|
+
* { "key": "/blog/launch", "share": 0.07, "page_views": 207 }
|
|
49
|
+
* ],
|
|
50
|
+
* "window": { "from": "2026-10-04T17:39:00.000Z", "to": "2026-10-11T17:39:00.000Z", "granularity": "day" },
|
|
51
|
+
* "source": "wae",
|
|
52
|
+
* "approximate": { "uniques": false },
|
|
53
|
+
* "cachedAt": "2026-10-11T17:38:41.388Z"
|
|
54
|
+
* }
|
|
12
55
|
*/
|
|
13
56
|
site: (q = {}) => call('GET', `/v1/analytics/site${query({ ...q, metric: typeof q.metric === 'string' ? [q.metric] : q.metric })}`),
|
|
14
57
|
};
|