@tchavi/sdk 0.1.1
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 +247 -0
- package/dist/index.cjs +610 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +635 -0
- package/dist/index.d.ts +635 -0
- package/dist/index.js +599 -0
- package/dist/index.js.map +1 -0
- package/package.json +45 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,599 @@
|
|
|
1
|
+
// src/core/auth.ts
|
|
2
|
+
var AuthStrategy = class {
|
|
3
|
+
mode;
|
|
4
|
+
apiKey;
|
|
5
|
+
accessToken;
|
|
6
|
+
tokenExpiresAt;
|
|
7
|
+
baseURL;
|
|
8
|
+
email;
|
|
9
|
+
password;
|
|
10
|
+
constructor(options) {
|
|
11
|
+
this.baseURL = options.baseURL ?? "http://localhost:3001";
|
|
12
|
+
if (options.apiKey) {
|
|
13
|
+
this.mode = "api-key";
|
|
14
|
+
this.apiKey = options.apiKey;
|
|
15
|
+
} else if (options.email && options.password) {
|
|
16
|
+
this.mode = "jwt";
|
|
17
|
+
this.email = options.email;
|
|
18
|
+
this.password = options.password;
|
|
19
|
+
} else {
|
|
20
|
+
throw new Error(
|
|
21
|
+
"TchaviClientOptions must provide either `apiKey` or both `email` and `password`."
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Returns the Authorization header value for the current request.
|
|
27
|
+
* For JWT mode, lazily logs in and refreshes the token as needed.
|
|
28
|
+
*/
|
|
29
|
+
async getAuthHeader() {
|
|
30
|
+
if (this.mode === "api-key") {
|
|
31
|
+
return `Bearer ${this.apiKey}`;
|
|
32
|
+
}
|
|
33
|
+
if (!this.accessToken || this.isTokenExpired()) {
|
|
34
|
+
await this.loginOrRefresh();
|
|
35
|
+
}
|
|
36
|
+
return `Bearer ${this.accessToken}`;
|
|
37
|
+
}
|
|
38
|
+
/** Store a token (e.g. after explicit login() call from AuthResource) */
|
|
39
|
+
setAccessToken(token, expiresInSeconds = 900) {
|
|
40
|
+
this.accessToken = token;
|
|
41
|
+
this.tokenExpiresAt = Date.now() + (expiresInSeconds - 30) * 1e3;
|
|
42
|
+
}
|
|
43
|
+
getMode() {
|
|
44
|
+
return this.mode;
|
|
45
|
+
}
|
|
46
|
+
isTokenExpired() {
|
|
47
|
+
if (!this.tokenExpiresAt) return true;
|
|
48
|
+
return Date.now() >= this.tokenExpiresAt;
|
|
49
|
+
}
|
|
50
|
+
async loginOrRefresh() {
|
|
51
|
+
const refreshed = await this.tryRefresh();
|
|
52
|
+
if (!refreshed) {
|
|
53
|
+
await this.loginWithPassword();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async tryRefresh() {
|
|
57
|
+
try {
|
|
58
|
+
const resp = await fetch(`${this.baseURL}/auth/refresh`, {
|
|
59
|
+
method: "POST",
|
|
60
|
+
credentials: "include",
|
|
61
|
+
headers: { "Content-Type": "application/json" }
|
|
62
|
+
});
|
|
63
|
+
if (!resp.ok) return false;
|
|
64
|
+
const body = await resp.json();
|
|
65
|
+
this.setAccessToken(body.accessToken);
|
|
66
|
+
return true;
|
|
67
|
+
} catch {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async loginWithPassword() {
|
|
72
|
+
const resp = await fetch(`${this.baseURL}/auth/login`, {
|
|
73
|
+
method: "POST",
|
|
74
|
+
credentials: "include",
|
|
75
|
+
headers: { "Content-Type": "application/json" },
|
|
76
|
+
body: JSON.stringify({ email: this.email, password: this.password })
|
|
77
|
+
});
|
|
78
|
+
if (!resp.ok) {
|
|
79
|
+
const body2 = await resp.json().catch(() => ({}));
|
|
80
|
+
throw new Error(
|
|
81
|
+
`Tchavi auth failed: ${body2["message"] ?? resp.statusText}`
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
const body = await resp.json();
|
|
85
|
+
this.setAccessToken(body.accessToken);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
// src/errors/TchaviError.ts
|
|
90
|
+
var TchaviError = class extends Error {
|
|
91
|
+
constructor(message) {
|
|
92
|
+
super(message);
|
|
93
|
+
this.name = "TchaviError";
|
|
94
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// src/errors/TchaviAuthenticationError.ts
|
|
99
|
+
var TchaviAuthenticationError = class extends TchaviAPIError {
|
|
100
|
+
constructor(response, headers = {}) {
|
|
101
|
+
super(response, headers);
|
|
102
|
+
this.name = "TchaviAuthenticationError";
|
|
103
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// src/errors/TchaviRateLimitError.ts
|
|
108
|
+
var TchaviRateLimitError = class extends TchaviAPIError {
|
|
109
|
+
retryAfter;
|
|
110
|
+
constructor(response, headers = {}) {
|
|
111
|
+
super(response, headers);
|
|
112
|
+
this.name = "TchaviRateLimitError";
|
|
113
|
+
const details = response.details;
|
|
114
|
+
this.retryAfter = details?.retry_after ?? (headers["retry-after"] ? parseInt(headers["retry-after"], 10) : null);
|
|
115
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// src/errors/TchaviInsufficientCreditsError.ts
|
|
120
|
+
var TchaviInsufficientCreditsError = class extends TchaviAPIError {
|
|
121
|
+
creditsRemaining;
|
|
122
|
+
constructor(response, headers = {}) {
|
|
123
|
+
super(response, headers);
|
|
124
|
+
this.name = "TchaviInsufficientCreditsError";
|
|
125
|
+
const details = response.details;
|
|
126
|
+
this.creditsRemaining = details?.credits_remaining ?? 0;
|
|
127
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// src/errors/TchaviAPIError.ts
|
|
132
|
+
var TchaviAPIError = class _TchaviAPIError extends TchaviError {
|
|
133
|
+
statusCode;
|
|
134
|
+
errorCode;
|
|
135
|
+
details;
|
|
136
|
+
headers;
|
|
137
|
+
constructor(response, headers = {}) {
|
|
138
|
+
super(response.message);
|
|
139
|
+
this.name = "TchaviAPIError";
|
|
140
|
+
this.statusCode = response.statusCode;
|
|
141
|
+
this.errorCode = response.error;
|
|
142
|
+
this.details = response.details;
|
|
143
|
+
this.headers = headers;
|
|
144
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Factory: inspect the raw response and construct the most specific subclass.
|
|
148
|
+
*/
|
|
149
|
+
static fromResponse(response, headers) {
|
|
150
|
+
switch (response.statusCode) {
|
|
151
|
+
case 401:
|
|
152
|
+
return new TchaviAuthenticationError(response, headers);
|
|
153
|
+
case 402:
|
|
154
|
+
return new TchaviInsufficientCreditsError(response, headers);
|
|
155
|
+
case 429:
|
|
156
|
+
return new TchaviRateLimitError(response, headers);
|
|
157
|
+
default:
|
|
158
|
+
return new _TchaviAPIError(response, headers);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// src/core/http-client.ts
|
|
164
|
+
var RETRYABLE_STATUS = /* @__PURE__ */ new Set([429, 502, 503]);
|
|
165
|
+
var HttpClient = class {
|
|
166
|
+
baseURL;
|
|
167
|
+
maxRetries;
|
|
168
|
+
defaultTimeout;
|
|
169
|
+
auth;
|
|
170
|
+
constructor(auth, baseURL, maxRetries, timeout) {
|
|
171
|
+
this.auth = auth;
|
|
172
|
+
this.baseURL = baseURL.replace(/\/$/, "");
|
|
173
|
+
this.maxRetries = maxRetries;
|
|
174
|
+
this.defaultTimeout = timeout;
|
|
175
|
+
}
|
|
176
|
+
async request(options) {
|
|
177
|
+
const response = await this.requestRaw(options);
|
|
178
|
+
return response.json();
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Returns the raw Response — used for streaming so callers can pipe the body.
|
|
182
|
+
*/
|
|
183
|
+
async requestRaw(options) {
|
|
184
|
+
let lastError;
|
|
185
|
+
for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
|
|
186
|
+
if (attempt > 0) {
|
|
187
|
+
await this.sleep(this.backoffMs(attempt));
|
|
188
|
+
}
|
|
189
|
+
const controller = new AbortController();
|
|
190
|
+
const timeout = options.timeout ?? this.defaultTimeout;
|
|
191
|
+
const timerId = setTimeout(() => controller.abort(), timeout);
|
|
192
|
+
try {
|
|
193
|
+
const url = this.buildURL(options.path, options.query);
|
|
194
|
+
const authHeader = options.skipAuth ? void 0 : await this.auth.getAuthHeader();
|
|
195
|
+
const headers = {
|
|
196
|
+
"Content-Type": "application/json"
|
|
197
|
+
};
|
|
198
|
+
if (authHeader) {
|
|
199
|
+
headers["Authorization"] = authHeader;
|
|
200
|
+
}
|
|
201
|
+
const fetchOptions = {
|
|
202
|
+
method: options.method ?? "GET",
|
|
203
|
+
headers,
|
|
204
|
+
signal: controller.signal,
|
|
205
|
+
credentials: "include"
|
|
206
|
+
};
|
|
207
|
+
if (options.body !== void 0) {
|
|
208
|
+
fetchOptions.body = JSON.stringify(options.body);
|
|
209
|
+
}
|
|
210
|
+
const response = await fetch(url, fetchOptions);
|
|
211
|
+
clearTimeout(timerId);
|
|
212
|
+
if (response.ok) {
|
|
213
|
+
return response;
|
|
214
|
+
}
|
|
215
|
+
if (RETRYABLE_STATUS.has(response.status) && attempt < this.maxRetries) {
|
|
216
|
+
const retryAfter = response.headers.get("Retry-After");
|
|
217
|
+
if (retryAfter) {
|
|
218
|
+
const seconds = parseInt(retryAfter, 10);
|
|
219
|
+
if (!Number.isNaN(seconds) && seconds > 0) {
|
|
220
|
+
await this.sleep(seconds * 1e3);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
lastError = new TchaviError(`HTTP ${response.status} ${response.statusText}`);
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
let errorBody;
|
|
227
|
+
try {
|
|
228
|
+
errorBody = await response.json();
|
|
229
|
+
} catch {
|
|
230
|
+
errorBody = {
|
|
231
|
+
error: "unknown_error",
|
|
232
|
+
message: response.statusText,
|
|
233
|
+
statusCode: response.status
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
const headersObj = {};
|
|
237
|
+
response.headers.forEach((value, key) => {
|
|
238
|
+
headersObj[key] = value;
|
|
239
|
+
});
|
|
240
|
+
throw TchaviAPIError.fromResponse(errorBody, headersObj);
|
|
241
|
+
} catch (err) {
|
|
242
|
+
clearTimeout(timerId);
|
|
243
|
+
if (err instanceof TchaviAPIError) throw err;
|
|
244
|
+
if (err.name === "AbortError") {
|
|
245
|
+
throw new TchaviError(`Request timed out after ${timeout}ms`);
|
|
246
|
+
}
|
|
247
|
+
lastError = err;
|
|
248
|
+
if (attempt === this.maxRetries) {
|
|
249
|
+
throw new TchaviError(`Network error: ${err.message}`);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
throw new TchaviError(`Request failed after ${this.maxRetries} retries: ${String(lastError)}`);
|
|
254
|
+
}
|
|
255
|
+
buildURL(path, query) {
|
|
256
|
+
const url = new URL(`${this.baseURL}${path}`);
|
|
257
|
+
if (query) {
|
|
258
|
+
for (const [key, value] of Object.entries(query)) {
|
|
259
|
+
if (value !== void 0) {
|
|
260
|
+
url.searchParams.set(key, String(value));
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return url.toString();
|
|
265
|
+
}
|
|
266
|
+
/** Exponential backoff with full jitter */
|
|
267
|
+
backoffMs(attempt) {
|
|
268
|
+
const base = 500;
|
|
269
|
+
const cap = 1e4;
|
|
270
|
+
const ceiling = Math.min(cap, base * Math.pow(2, attempt));
|
|
271
|
+
return Math.random() * ceiling;
|
|
272
|
+
}
|
|
273
|
+
sleep(ms) {
|
|
274
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
// src/core/streaming.ts
|
|
279
|
+
var MAX_BUFFER_SIZE = 1048576;
|
|
280
|
+
async function* parseStream(body) {
|
|
281
|
+
const reader = body.getReader();
|
|
282
|
+
const decoder = new TextDecoder();
|
|
283
|
+
let buffer = "";
|
|
284
|
+
try {
|
|
285
|
+
while (true) {
|
|
286
|
+
const { done, value } = await reader.read();
|
|
287
|
+
if (done) break;
|
|
288
|
+
buffer += decoder.decode(value, { stream: true });
|
|
289
|
+
if (buffer.length > MAX_BUFFER_SIZE) {
|
|
290
|
+
throw new TchaviError(
|
|
291
|
+
`SSE buffer exceeded ${MAX_BUFFER_SIZE} bytes \u2014 possible malformed stream`
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
const events = buffer.split("\n\n");
|
|
295
|
+
buffer = events.pop() ?? "";
|
|
296
|
+
for (const rawEvent of events) {
|
|
297
|
+
const lines = rawEvent.split("\n");
|
|
298
|
+
let eventType;
|
|
299
|
+
let dataLine;
|
|
300
|
+
for (const line of lines) {
|
|
301
|
+
if (line.startsWith("event: ")) {
|
|
302
|
+
eventType = line.slice(7).trim();
|
|
303
|
+
} else if (line.startsWith("data: ")) {
|
|
304
|
+
dataLine = line.slice(6).trim();
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
if (!dataLine) continue;
|
|
308
|
+
if (dataLine === "[DONE]") {
|
|
309
|
+
yield { type: "done" };
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
if (eventType === "tchavi") {
|
|
313
|
+
try {
|
|
314
|
+
const meta = JSON.parse(dataLine);
|
|
315
|
+
yield { type: "tchavi", data: meta };
|
|
316
|
+
} catch {
|
|
317
|
+
}
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
try {
|
|
321
|
+
const parsed = JSON.parse(dataLine);
|
|
322
|
+
yield { type: "data", data: parsed };
|
|
323
|
+
} catch {
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
} finally {
|
|
328
|
+
reader.releaseLock();
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
var TchaviStream = class {
|
|
332
|
+
body;
|
|
333
|
+
_tchaviMeta = null;
|
|
334
|
+
constructor(body) {
|
|
335
|
+
this.body = body;
|
|
336
|
+
}
|
|
337
|
+
[Symbol.asyncIterator]() {
|
|
338
|
+
return this._consume();
|
|
339
|
+
}
|
|
340
|
+
async *_consume() {
|
|
341
|
+
try {
|
|
342
|
+
for await (const event of parseStream(this.body)) {
|
|
343
|
+
if (event.type === "data") {
|
|
344
|
+
yield event.data;
|
|
345
|
+
} else if (event.type === "tchavi") {
|
|
346
|
+
this._tchaviMeta = event.data;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
} finally {
|
|
350
|
+
this.body.cancel().catch(() => {
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
/** Returns Tchavi credit metadata once the stream is fully consumed. */
|
|
355
|
+
finalMeta() {
|
|
356
|
+
return this._tchaviMeta;
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
// src/resources/chat/completions.ts
|
|
361
|
+
var Completions = class {
|
|
362
|
+
constructor(http) {
|
|
363
|
+
this.http = http;
|
|
364
|
+
}
|
|
365
|
+
async create(params) {
|
|
366
|
+
if (params.stream === true) {
|
|
367
|
+
const response = await this.http.requestRaw({
|
|
368
|
+
method: "POST",
|
|
369
|
+
path: "/v1/chat/completions",
|
|
370
|
+
body: params,
|
|
371
|
+
timeout: 12e4
|
|
372
|
+
});
|
|
373
|
+
if (!response.body) {
|
|
374
|
+
throw new TchaviError("Streaming response has no body");
|
|
375
|
+
}
|
|
376
|
+
return new TchaviStream(response.body);
|
|
377
|
+
}
|
|
378
|
+
return this.http.request({
|
|
379
|
+
method: "POST",
|
|
380
|
+
path: "/v1/chat/completions",
|
|
381
|
+
body: params
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
// src/resources/chat/index.ts
|
|
387
|
+
var Chat = class {
|
|
388
|
+
completions;
|
|
389
|
+
constructor(http) {
|
|
390
|
+
this.completions = new Completions(http);
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
// src/resources/embeddings.ts
|
|
395
|
+
var Embeddings = class {
|
|
396
|
+
constructor(http) {
|
|
397
|
+
this.http = http;
|
|
398
|
+
}
|
|
399
|
+
create(params) {
|
|
400
|
+
return this.http.request({
|
|
401
|
+
method: "POST",
|
|
402
|
+
path: "/v1/embeddings",
|
|
403
|
+
body: params
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
// src/resources/images/generations.ts
|
|
409
|
+
var Generations = class {
|
|
410
|
+
constructor(http) {
|
|
411
|
+
this.http = http;
|
|
412
|
+
}
|
|
413
|
+
create(params) {
|
|
414
|
+
return this.http.request({
|
|
415
|
+
method: "POST",
|
|
416
|
+
path: "/v1/images/generations",
|
|
417
|
+
body: params
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
// src/resources/images/index.ts
|
|
423
|
+
var Images = class {
|
|
424
|
+
generations;
|
|
425
|
+
constructor(http) {
|
|
426
|
+
this.generations = new Generations(http);
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
// src/resources/credits.ts
|
|
431
|
+
var Credits = class {
|
|
432
|
+
constructor(http) {
|
|
433
|
+
this.http = http;
|
|
434
|
+
}
|
|
435
|
+
getBalance() {
|
|
436
|
+
return this.http.request({ path: "/credits/balance" });
|
|
437
|
+
}
|
|
438
|
+
listPacks() {
|
|
439
|
+
return this.http.request({ path: "/credits/packs", skipAuth: true });
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
|
|
443
|
+
// src/resources/api-keys.ts
|
|
444
|
+
var ApiKeys = class {
|
|
445
|
+
constructor(http) {
|
|
446
|
+
this.http = http;
|
|
447
|
+
}
|
|
448
|
+
list() {
|
|
449
|
+
return this.http.request({ path: "/api-keys" });
|
|
450
|
+
}
|
|
451
|
+
create(params = {}) {
|
|
452
|
+
return this.http.request({ method: "POST", path: "/api-keys", body: params });
|
|
453
|
+
}
|
|
454
|
+
retrieve(id) {
|
|
455
|
+
return this.http.request({ path: `/api-keys/${id}` });
|
|
456
|
+
}
|
|
457
|
+
update(id, params) {
|
|
458
|
+
return this.http.request({ method: "PUT", path: `/api-keys/${id}`, body: params });
|
|
459
|
+
}
|
|
460
|
+
delete(id) {
|
|
461
|
+
return this.http.request({ method: "DELETE", path: `/api-keys/${id}` });
|
|
462
|
+
}
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
// src/resources/usage.ts
|
|
466
|
+
var Usage = class {
|
|
467
|
+
constructor(http) {
|
|
468
|
+
this.http = http;
|
|
469
|
+
}
|
|
470
|
+
getStats(params = {}) {
|
|
471
|
+
return this.http.request({
|
|
472
|
+
path: "/usage/stats",
|
|
473
|
+
query: params
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
getHistory(params = {}) {
|
|
477
|
+
return this.http.request({
|
|
478
|
+
path: "/usage/history",
|
|
479
|
+
query: params
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
// src/resources/models.ts
|
|
485
|
+
var Models = class {
|
|
486
|
+
constructor(http) {
|
|
487
|
+
this.http = http;
|
|
488
|
+
}
|
|
489
|
+
/** Lists all active model tiers. Pass `all: true` to include inactive ones (admin only). */
|
|
490
|
+
list(params = {}) {
|
|
491
|
+
return this.http.request({
|
|
492
|
+
path: "/model-tiers",
|
|
493
|
+
query: params.all ? { all: "true" } : void 0,
|
|
494
|
+
skipAuth: true
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
// src/resources/payments.ts
|
|
500
|
+
var Payments = class {
|
|
501
|
+
constructor(http) {
|
|
502
|
+
this.http = http;
|
|
503
|
+
}
|
|
504
|
+
initiate(params) {
|
|
505
|
+
return this.http.request({ method: "POST", path: "/payments/initiate", body: params });
|
|
506
|
+
}
|
|
507
|
+
getStatus(id) {
|
|
508
|
+
return this.http.request({ path: `/payments/status/${id}` });
|
|
509
|
+
}
|
|
510
|
+
getHistory(params = {}) {
|
|
511
|
+
return this.http.request({
|
|
512
|
+
path: "/payments/history",
|
|
513
|
+
query: params
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
// src/resources/auth-resource.ts
|
|
519
|
+
var AuthResource = class {
|
|
520
|
+
constructor(http, authStrategy) {
|
|
521
|
+
this.http = http;
|
|
522
|
+
this.authStrategy = authStrategy;
|
|
523
|
+
}
|
|
524
|
+
async login(email, password) {
|
|
525
|
+
const result = await this.http.request({
|
|
526
|
+
method: "POST",
|
|
527
|
+
path: "/auth/login",
|
|
528
|
+
body: { email, password },
|
|
529
|
+
skipAuth: true
|
|
530
|
+
});
|
|
531
|
+
this.authStrategy.setAccessToken(result.accessToken);
|
|
532
|
+
return result;
|
|
533
|
+
}
|
|
534
|
+
register(params) {
|
|
535
|
+
return this.http.request({
|
|
536
|
+
method: "POST",
|
|
537
|
+
path: "/auth/register",
|
|
538
|
+
body: params,
|
|
539
|
+
skipAuth: true
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
logout() {
|
|
543
|
+
return this.http.request({ method: "POST", path: "/auth/logout" });
|
|
544
|
+
}
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
// src/resources/users.ts
|
|
548
|
+
var Users = class {
|
|
549
|
+
constructor(http) {
|
|
550
|
+
this.http = http;
|
|
551
|
+
}
|
|
552
|
+
me() {
|
|
553
|
+
return this.http.request({ path: "/users/me" });
|
|
554
|
+
}
|
|
555
|
+
update(params) {
|
|
556
|
+
return this.http.request({ method: "PUT", path: "/users/me", body: params });
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
// src/tchavi.ts
|
|
561
|
+
var DEFAULT_BASE_URL = "http://localhost:3001";
|
|
562
|
+
var DEFAULT_MAX_RETRIES = 2;
|
|
563
|
+
var DEFAULT_TIMEOUT = 6e4;
|
|
564
|
+
var Tchavi = class {
|
|
565
|
+
chat;
|
|
566
|
+
embeddings;
|
|
567
|
+
images;
|
|
568
|
+
credits;
|
|
569
|
+
apiKeys;
|
|
570
|
+
usage;
|
|
571
|
+
models;
|
|
572
|
+
payments;
|
|
573
|
+
auth;
|
|
574
|
+
users;
|
|
575
|
+
constructor(options) {
|
|
576
|
+
const baseURL = options.baseURL ?? DEFAULT_BASE_URL;
|
|
577
|
+
const maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
578
|
+
const timeout = options.timeout ?? DEFAULT_TIMEOUT;
|
|
579
|
+
const authStrategy = new AuthStrategy(options);
|
|
580
|
+
const http = new HttpClient(authStrategy, baseURL, maxRetries, timeout);
|
|
581
|
+
this.chat = new Chat(http);
|
|
582
|
+
this.embeddings = new Embeddings(http);
|
|
583
|
+
this.images = new Images(http);
|
|
584
|
+
this.credits = new Credits(http);
|
|
585
|
+
this.apiKeys = new ApiKeys(http);
|
|
586
|
+
this.usage = new Usage(http);
|
|
587
|
+
this.models = new Models(http);
|
|
588
|
+
this.payments = new Payments(http);
|
|
589
|
+
this.auth = new AuthResource(http, authStrategy);
|
|
590
|
+
this.users = new Users(http);
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
// src/index.ts
|
|
595
|
+
var index_default = Tchavi;
|
|
596
|
+
|
|
597
|
+
export { Tchavi, TchaviAPIError, TchaviAuthenticationError, TchaviError, TchaviInsufficientCreditsError, TchaviRateLimitError, TchaviStream, index_default as default };
|
|
598
|
+
//# sourceMappingURL=index.js.map
|
|
599
|
+
//# sourceMappingURL=index.js.map
|