@meistrari/tela-sdk-js 1.0.1 → 2.0.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/README.md CHANGED
@@ -7,12 +7,14 @@ The Tela SDK for JavaScript provides a simple and powerful way to interact with
7
7
  - [Installation](#installation)
8
8
  - [Usage](#usage)
9
9
  - [Examples](#examples)
10
- - [Simple Completion](#simple-completion)
11
- - [Chat Completion](#chat-completion)
12
- - [Webhook Completion](#webhook-completion)
13
- - [Streaming Completion](#streaming-completion)
14
- - [API Reference](#api-reference)
15
- - [Contributing](#contributing)
10
+ - [Canvas API](#canvas-api)
11
+ - [Synchronous Execution](#synchronous-execution)
12
+ - [Asynchronous Execution with Polling](#asynchronous-execution-with-polling)
13
+ - [Streaming Execution](#streaming-execution)
14
+ - [Schema Validation](#schema-validation)
15
+ - [Multiple Files](#multiple-files)
16
+ - [Webhook Notifications](#webhook-notifications)
17
+ - [File Handling](#file-handling)
16
18
  - [License](#license)
17
19
 
18
20
  ## Installation
@@ -30,130 +32,252 @@ pnpm add @meistrari/tela-sdk-js
30
32
  First, you need to import the SDK and initialize it with your API key:
31
33
 
32
34
  ```typescript
33
- import { createTelaClient } from '@meistrari/tela-sdk-js'
35
+ import { TelaSDK } from '@meistrari/tela-sdk-js'
34
36
 
35
- const tela = createTelaClient({ apiKey: 'your-api-key' })
37
+ const tela = new TelaSDK({ apiKey: 'your-api-key' })
36
38
  ```
37
39
 
38
40
  ## Examples
39
41
 
40
- ### Simple Completion
42
+ ### Canvas API
41
43
 
42
- This example demonstrates how to create a simple completion using a PDF document:
44
+ The Canvas API provides a type-safe way to execute prompts with schema validation and multiple execution modes.
45
+
46
+ #### Synchronous Execution
47
+
48
+ Execute a canvas and wait for the complete result:
43
49
 
44
50
  ```typescript
45
- import { createTelaClient } from '@meistrari/tela-sdk-js'
46
- import type { TelaFile } from '@meistrari/tela-sdk-js'
51
+ import { TelaSDK } from '@meistrari/tela-sdk-js'
47
52
 
48
- const tela = createTelaClient({
49
- apiKey: process.env.TELA_API_KEY,
53
+ const tela = new TelaSDK({
54
+ apiKey: 'your-api-key'
50
55
  })
51
56
 
52
- async function run() {
53
- const completion = await tela.completions.create<{ document: TelaFile }, { fileSummary: string }>({
54
- canvasId: process.env.TELA_CANVAS_ID,
55
- variables: {
56
- document: tela.createFile('https://www.wmaccess.com/downloads/sample-invoice.pdf'),
57
- },
58
- stream: false,
59
- })
60
- console.log(JSON.stringify(completion, null, 2))
61
- }
62
- run().catch(console.error)
57
+ // Get canvas with input/output schemas
58
+ const canvas = await tela.canvas.get({
59
+ id: 'canvas-id',
60
+ input: schema => schema.object({
61
+ query: schema.string()
62
+ }),
63
+ output: schema => schema.object({
64
+ result: schema.string()
65
+ }),
66
+ })
67
+
68
+ // Execute synchronously - direct result access
69
+ const result = await canvas.execute({ query: 'What is the capital of France?' }).result
70
+
71
+ console.log(result) // { result: "Paris" }
72
+
73
+ // Alternative: get execution object for more control
74
+ const execution = await canvas.execute({ query: 'What is the capital of France?' })
75
+ const result2 = await execution.result
63
76
  ```
64
77
 
65
- ### Chat Completion
78
+ #### Asynchronous Execution with Polling
66
79
 
67
- This example shows how to create a chat completion with a simple message:
80
+ For long-running operations, use async mode with automatic polling:
68
81
 
69
82
  ```typescript
70
- import { createTelaClient } from '@meistrari/tela-sdk-js'
71
- const tela = createTelaClient({
72
- apiKey: process.env.TELA_API_KEY,
83
+ const canvas = await tela.canvas.get({
84
+ id: 'canvas-id',
85
+ input: schema => schema.object({
86
+ document: schema.file(),
87
+ query: schema.string()
88
+ }),
89
+ output: schema => schema.object({
90
+ summary: schema.string()
91
+ }),
73
92
  })
74
- async function run() {
75
- const completion = await tela.completions.create({
76
- canvasId: process.env.TELA_CANVAS_ID,
77
- messages: [{ role: 'user', content: 'Hello!' }],
78
- })
79
- console.log(JSON.stringify(completion, null, 2))
80
- }
81
- run().catch(console.error)
93
+
94
+ // Execute asynchronously with polling - direct result access
95
+ const result = await canvas.execute(
96
+ {
97
+ document: TelaFile.create(documentBlob, { name: 'report.pdf' }),
98
+ query: 'Summarize this document'
99
+ },
100
+ {
101
+ async: true,
102
+ pollingInterval: 1000, // Poll every 1 second
103
+ pollingTimeout: 60000, // Timeout after 60 seconds
104
+ }
105
+ ).result
106
+
107
+ console.log(result.summary)
82
108
  ```
83
109
 
84
- ### Webhook Completion
110
+ #### Streaming Execution
85
111
 
86
- This example demonstrates how to create a completion that sends the result to a webhook URL:
112
+ Stream results as they're generated:
87
113
 
88
114
  ```typescript
89
- import { createTelaClient } from '@meistrari/tela-sdk-js'
90
-
91
- const tela = createTelaClient({
92
- apiKey: process.env.TELA_API_KEY,
115
+ const canvas = await tela.canvas.get({
116
+ id: 'canvas-id',
117
+ input: schema => schema.object({
118
+ prompt: schema.string()
119
+ }),
120
+ output: schema => schema.object({
121
+ response: schema.string()
122
+ }),
93
123
  })
94
124
 
95
- async function run() {
96
- const webhook = await tela.completions.create({
97
- canvasId: process.env.TELA_CANVAS_ID,
98
- variables: {
99
- document: tela.createFile('https://www.wmaccess.com/downloads/sample-invoice.pdf'),
100
- },
101
- webhookUrl: 'https://webhook.site/4294967295',
102
- stream: false,
103
- })
104
- console.log(JSON.stringify(webhook, null, 2))
125
+ // Execute with streaming - direct iteration
126
+ for await (const chunk of canvas.execute(
127
+ { prompt: 'Write a story about a robot' },
128
+ { stream: true }
129
+ ).result) {
130
+ process.stdout.write(chunk.response || '')
105
131
  }
106
- run().catch(console.error)
107
132
  ```
108
133
 
109
- ### Streaming Completion
134
+ #### Schema Validation
110
135
 
111
- This example shows how to handle streaming responses:
136
+ The Canvas API validates your inputs and outputs against the server schema:
112
137
 
113
138
  ```typescript
114
- import fs from 'node:fs'
115
- import { createTelaClient } from '@meistrari/tela-sdk-js'
116
- import type { TelaFile } from '@meistrari/tela-sdk-js'
117
-
118
- const tela = createTelaClient({
119
- apiKey: process.env.TELA_API_KEY,
139
+ const canvas = await tela.canvas.get({
140
+ id: 'canvas-id',
141
+ input: schema => schema.object({
142
+ text: schema.string(),
143
+ file: schema.file(), // TelaFile validator
144
+ settings: schema.object({
145
+ temperature: schema.number(),
146
+ maxTokens: schema.number(),
147
+ }),
148
+ }),
149
+ output: schema => schema.object({
150
+ analysis: schema.string(),
151
+ confidence: schema.number(),
152
+ }),
153
+ skipSchemaValidation: false, // Enable schema validation warnings during canvas retrieval (default)
120
154
  })
121
155
 
122
- const PATH = 'examples/stream/sample-invoice.pdf'
123
- const fileStream = fs.createReadStream(PATH)
124
-
125
- async function run() {
126
- const completion = await tela.completions.create<{ document: TelaFile }, { summary: string }>({
127
- canvasId: process.env.TELA_CANVAS_ID,
128
- variables: {
129
- document: tela.createFile(fileStream),
130
- },
131
- stream: true,
132
- })
133
- for await (const chunk of completion) {
134
- console.log(JSON.stringify(chunk))
156
+ // TypeScript will ensure you provide the correct types
157
+ const result = await canvas.execute({
158
+ text: 'Analyze this document',
159
+ file: TelaFile.create(blob, { name: 'data.pdf' }),
160
+ settings: {
161
+ temperature: 0.7,
162
+ maxTokens: 1000,
135
163
  }
136
- }
137
- run().catch(console.error)
164
+ }).result
165
+
166
+ // Skip result validation to trust API response without Zod validation
167
+ const resultWithoutValidation = await canvas.execute(
168
+ {
169
+ text: 'Analyze this document',
170
+ file: TelaFile.create(blob, { name: 'data.pdf' }),
171
+ settings: {
172
+ temperature: 0.7,
173
+ maxTokens: 1000,
174
+ }
175
+ },
176
+ {
177
+ skipResultValidation: true // Skip Zod validation on results, just typecast
178
+ }
179
+ ).result
180
+ ```
181
+
182
+ #### Multiple Files
183
+
184
+ Process multiple files in a single canvas execution:
185
+
186
+ ```typescript
187
+ const canvas = await tela.canvas.get({
188
+ id: 'canvas-id',
189
+ input: schema => schema.object({
190
+ documents: schema.array(schema.file()),
191
+ instructions: schema.string()
192
+ }),
193
+ output: schema => schema.object({
194
+ comparison: schema.string()
195
+ }),
196
+ })
197
+
198
+ const result = await canvas.execute({
199
+ documents: [
200
+ TelaFile.create('https://example.com/doc1.pdf', { range: [0, 5] }),
201
+ TelaFile.create('https://example.com/doc2.pdf', { range: [0, 5] }),
202
+ TelaFile.create(localFileBlob, { name: 'doc3.pdf' })
203
+ ],
204
+ instructions: 'Compare these documents and identify key differences'
205
+ }).result
206
+
207
+ console.log(result.comparison)
138
208
  ```
139
209
 
140
- ## API Reference
210
+ #### Webhook Notifications
141
211
 
142
- ### `createTelaClient`
212
+ Receive completion notifications via webhook for async executions:
143
213
 
144
- #### `createTelaClient(options: TelaSDKOptions)`
214
+ ```typescript
215
+ const canvas = await tela.canvas.get({
216
+ id: 'canvas-id',
217
+ input: schema => schema.object({
218
+ document: schema.file(),
219
+ query: schema.string()
220
+ }),
221
+ output: schema => schema.object({
222
+ analysis: schema.string()
223
+ }),
224
+ })
145
225
 
146
- Creates a new instance of the TelaSDK.
226
+ // Execute with webhook notification - you can still poll if needed
227
+ const result = await canvas.execute(
228
+ {
229
+ document: TelaFile.create('https://example.com/large-report.pdf'),
230
+ query: 'Analyze this report'
231
+ },
232
+ {
233
+ async: true,
234
+ webhookUrl: 'https://your-server.com/webhook',
235
+ pollingTimeout: 300000, // 5 minutes
236
+ }
237
+ ).result
238
+ // OR wait for the webhook notification on your server instead of polling
239
+ ```
240
+
241
+ ### File Handling
147
242
 
148
- - `options` (TelaSDKOptions): Configuration options for the SDK.
243
+ The `TelaFile` API provides flexible file handling with support for various input types:
244
+
245
+ ```typescript
246
+ import { TelaFile } from '@meistrari/tela-sdk-js'
149
247
 
150
- #### `completions.create<CanvasVariables, CanvasStructuredOutput>(params: CompletionCreateParams<CanvasVariables>, opts?: RequestOptions): Promise<CompletionCreateResponse<CanvasStructuredOutput>>`
248
+ // From a Blob or File
249
+ const fileFromBlob = TelaFile.create(blob, { name: 'custom.pdf' })
151
250
 
152
- Creates a chat completion with various input options and response formats.
251
+ // From a URL
252
+ const fileFromUrl = TelaFile.create('https://example.com/document.pdf')
153
253
 
154
- - `params` (CompletionCreateParams<CanvasVariables>): The parameters for creating a chat completion.
155
- - `opts` (RequestOptions): Additional request options.
254
+ // From bytes with explicit MIME type
255
+ const fileFromBytes = TelaFile.create(
256
+ new Uint8Array(['...']),
257
+ {
258
+ mimeType: 'application/pdf',
259
+ name: 'document.pdf'
260
+ }
261
+ )
262
+
263
+ // From a ReadableStream with explicit MIME type
264
+ const fileFromStream = TelaFile.create(
265
+ readableStream,
266
+ {
267
+ mimeType: 'image/jpeg',
268
+ name: 'photo.jpg'
269
+ }
270
+ )
156
271
 
157
- ## Contributing
272
+ // Vault reference (for files already uploaded)
273
+ const vaultFile = TelaFile.create('vault://file-id')
158
274
 
159
- We welcome contributions to the Tela SDK! Please see our [contributing guide](CONTRIBUTING.md) for more information.
275
+ // With page range for PDFs
276
+ const fileWithRange = TelaFile.create(
277
+ 'https://example.com/document.pdf',
278
+ {
279
+ range: [0, 5], // Pages 0-5
280
+ parserType: 'pdf'
281
+ }
282
+ )
283
+ ```