@gpt-core/client 0.1.0-alpha.1 → 0.1.0-alpha.2
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 +306 -23
- package/dist/index.d.mts +390 -2
- package/dist/index.d.ts +390 -2
- package/dist/index.js +525 -54
- package/dist/index.mjs +487 -53
- package/package.json +10 -3
package/dist/index.js
CHANGED
|
@@ -22,7 +22,35 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
22
22
|
// src/index.ts
|
|
23
23
|
var index_exports = {};
|
|
24
24
|
__export(index_exports, {
|
|
25
|
+
AgentCreateSchema: () => AgentCreateSchema,
|
|
26
|
+
ApiKeyAllocateSchema: () => ApiKeyAllocateSchema,
|
|
27
|
+
ApiKeyCreateSchema: () => ApiKeyCreateSchema,
|
|
28
|
+
ApplicationCreateSchema: () => ApplicationCreateSchema,
|
|
29
|
+
AuthenticationError: () => AuthenticationError,
|
|
30
|
+
AuthorizationError: () => AuthorizationError,
|
|
31
|
+
BucketCreateSchema: () => BucketCreateSchema,
|
|
32
|
+
DEFAULT_RETRY_CONFIG: () => DEFAULT_RETRY_CONFIG,
|
|
33
|
+
DocumentUploadBase64Schema: () => DocumentUploadBase64Schema,
|
|
34
|
+
EmbedRequestSchema: () => EmbedRequestSchema,
|
|
25
35
|
GptClient: () => GptClient,
|
|
36
|
+
GptCoreError: () => GptCoreError,
|
|
37
|
+
InvitationCreateSchema: () => InvitationCreateSchema,
|
|
38
|
+
LoginRequestSchema: () => LoginRequestSchema,
|
|
39
|
+
MessageSendSchema: () => MessageSendSchema,
|
|
40
|
+
NetworkError: () => NetworkError,
|
|
41
|
+
NotFoundError: () => NotFoundError,
|
|
42
|
+
PresignedDownloadSchema: () => PresignedDownloadSchema,
|
|
43
|
+
PresignedUploadSchema: () => PresignedUploadSchema,
|
|
44
|
+
RateLimitError: () => RateLimitError,
|
|
45
|
+
RegisterRequestSchema: () => RegisterRequestSchema,
|
|
46
|
+
SearchRequestSchema: () => SearchRequestSchema,
|
|
47
|
+
ServerError: () => ServerError,
|
|
48
|
+
ThreadCreateSchema: () => ThreadCreateSchema,
|
|
49
|
+
TimeoutError: () => TimeoutError,
|
|
50
|
+
ValidationError: () => ValidationError,
|
|
51
|
+
WorkspaceCreateSchema: () => WorkspaceCreateSchema,
|
|
52
|
+
calculateBackoff: () => calculateBackoff,
|
|
53
|
+
collectStreamedMessage: () => collectStreamedMessage,
|
|
26
54
|
eq: () => eq,
|
|
27
55
|
eq2: () => eq2,
|
|
28
56
|
eq3: () => eq3,
|
|
@@ -48,6 +76,8 @@ __export(index_exports, {
|
|
|
48
76
|
greater_than_or_equal6: () => greater_than_or_equal6,
|
|
49
77
|
greater_than_or_equal7: () => greater_than_or_equal7,
|
|
50
78
|
greater_than_or_equal8: () => greater_than_or_equal8,
|
|
79
|
+
handleApiError: () => handleApiError,
|
|
80
|
+
isRetryableError: () => isRetryableError,
|
|
51
81
|
less_than: () => less_than,
|
|
52
82
|
less_than2: () => less_than2,
|
|
53
83
|
less_than3: () => less_than3,
|
|
@@ -72,10 +102,17 @@ __export(index_exports, {
|
|
|
72
102
|
not_eq6: () => not_eq6,
|
|
73
103
|
not_eq7: () => not_eq7,
|
|
74
104
|
not_eq8: () => not_eq8,
|
|
105
|
+
paginateAll: () => paginateAll,
|
|
106
|
+
paginateToArray: () => paginateToArray,
|
|
107
|
+
retryWithBackoff: () => retryWithBackoff,
|
|
75
108
|
role: () => role,
|
|
109
|
+
sleep: () => sleep,
|
|
76
110
|
status: () => status,
|
|
77
111
|
status2: () => status2,
|
|
78
|
-
|
|
112
|
+
streamMessage: () => streamMessage,
|
|
113
|
+
streamSSE: () => streamSSE,
|
|
114
|
+
type: () => type,
|
|
115
|
+
withRetry: () => withRetry
|
|
79
116
|
});
|
|
80
117
|
module.exports = __toCommonJS(index_exports);
|
|
81
118
|
|
|
@@ -1431,9 +1468,131 @@ var EmbeddingService = class {
|
|
|
1431
1468
|
}
|
|
1432
1469
|
};
|
|
1433
1470
|
|
|
1471
|
+
// src/errors/index.ts
|
|
1472
|
+
var GptCoreError = class extends Error {
|
|
1473
|
+
constructor(message, options) {
|
|
1474
|
+
super(message);
|
|
1475
|
+
this.name = this.constructor.name;
|
|
1476
|
+
this.statusCode = options?.statusCode;
|
|
1477
|
+
this.code = options?.code;
|
|
1478
|
+
this.requestId = options?.requestId;
|
|
1479
|
+
this.headers = options?.headers;
|
|
1480
|
+
this.body = options?.body;
|
|
1481
|
+
if (options?.cause) {
|
|
1482
|
+
this.cause = options.cause;
|
|
1483
|
+
}
|
|
1484
|
+
if (Error.captureStackTrace) {
|
|
1485
|
+
Error.captureStackTrace(this, this.constructor);
|
|
1486
|
+
}
|
|
1487
|
+
}
|
|
1488
|
+
};
|
|
1489
|
+
var AuthenticationError = class extends GptCoreError {
|
|
1490
|
+
constructor(message = "Authentication failed", options) {
|
|
1491
|
+
super(message, { statusCode: 401, ...options });
|
|
1492
|
+
}
|
|
1493
|
+
};
|
|
1494
|
+
var AuthorizationError = class extends GptCoreError {
|
|
1495
|
+
constructor(message = "Permission denied", options) {
|
|
1496
|
+
super(message, { statusCode: 403, ...options });
|
|
1497
|
+
}
|
|
1498
|
+
};
|
|
1499
|
+
var NotFoundError = class extends GptCoreError {
|
|
1500
|
+
constructor(message = "Resource not found", options) {
|
|
1501
|
+
super(message, { statusCode: 404, ...options });
|
|
1502
|
+
}
|
|
1503
|
+
};
|
|
1504
|
+
var ValidationError = class extends GptCoreError {
|
|
1505
|
+
constructor(message = "Validation failed", errors, options) {
|
|
1506
|
+
super(message, { statusCode: 422, ...options });
|
|
1507
|
+
this.errors = errors;
|
|
1508
|
+
}
|
|
1509
|
+
};
|
|
1510
|
+
var RateLimitError = class extends GptCoreError {
|
|
1511
|
+
constructor(message = "Rate limit exceeded", retryAfter, options) {
|
|
1512
|
+
super(message, { statusCode: 429, ...options });
|
|
1513
|
+
this.retryAfter = retryAfter;
|
|
1514
|
+
}
|
|
1515
|
+
};
|
|
1516
|
+
var NetworkError = class extends GptCoreError {
|
|
1517
|
+
constructor(message = "Network request failed", options) {
|
|
1518
|
+
super(message, options);
|
|
1519
|
+
}
|
|
1520
|
+
};
|
|
1521
|
+
var TimeoutError = class extends GptCoreError {
|
|
1522
|
+
constructor(message = "Request timeout", options) {
|
|
1523
|
+
super(message, options);
|
|
1524
|
+
}
|
|
1525
|
+
};
|
|
1526
|
+
var ServerError = class extends GptCoreError {
|
|
1527
|
+
constructor(message = "Internal server error", options) {
|
|
1528
|
+
super(message, { statusCode: 500, ...options });
|
|
1529
|
+
}
|
|
1530
|
+
};
|
|
1531
|
+
function handleApiError(error) {
|
|
1532
|
+
const response = error?.response || error;
|
|
1533
|
+
const statusCode = response?.status || error?.status || error?.statusCode;
|
|
1534
|
+
const headers = response?.headers || error?.headers;
|
|
1535
|
+
const requestId = headers?.get?.("x-request-id") || headers?.["x-request-id"];
|
|
1536
|
+
const body = response?.body || response?.data || error?.body || error?.data || error;
|
|
1537
|
+
let message = "An error occurred";
|
|
1538
|
+
let errors;
|
|
1539
|
+
if (body?.errors && Array.isArray(body.errors)) {
|
|
1540
|
+
const firstError = body.errors[0];
|
|
1541
|
+
message = firstError?.title || firstError?.detail || message;
|
|
1542
|
+
errors = body.errors.map((err) => ({
|
|
1543
|
+
field: err.source?.pointer?.split("/").pop(),
|
|
1544
|
+
message: err.detail || err.title || "Unknown error"
|
|
1545
|
+
}));
|
|
1546
|
+
} else if (body?.message) {
|
|
1547
|
+
message = body.message;
|
|
1548
|
+
} else if (typeof body === "string") {
|
|
1549
|
+
message = body;
|
|
1550
|
+
} else if (error?.message) {
|
|
1551
|
+
message = error.message;
|
|
1552
|
+
}
|
|
1553
|
+
const errorOptions = {
|
|
1554
|
+
statusCode,
|
|
1555
|
+
requestId,
|
|
1556
|
+
headers: headers ? headers.entries ? Object.fromEntries(headers.entries()) : Object.fromEntries(Object.entries(headers)) : void 0,
|
|
1557
|
+
body,
|
|
1558
|
+
cause: error
|
|
1559
|
+
};
|
|
1560
|
+
switch (statusCode) {
|
|
1561
|
+
case 401:
|
|
1562
|
+
throw new AuthenticationError(message, errorOptions);
|
|
1563
|
+
case 403:
|
|
1564
|
+
throw new AuthorizationError(message, errorOptions);
|
|
1565
|
+
case 404:
|
|
1566
|
+
throw new NotFoundError(message, errorOptions);
|
|
1567
|
+
case 400:
|
|
1568
|
+
case 422:
|
|
1569
|
+
throw new ValidationError(message, errors, errorOptions);
|
|
1570
|
+
case 429:
|
|
1571
|
+
const retryAfter = headers?.get?.("retry-after") || headers?.["retry-after"];
|
|
1572
|
+
throw new RateLimitError(message, retryAfter ? parseInt(retryAfter, 10) : void 0, errorOptions);
|
|
1573
|
+
case 500:
|
|
1574
|
+
case 502:
|
|
1575
|
+
case 503:
|
|
1576
|
+
case 504:
|
|
1577
|
+
throw new ServerError(message, errorOptions);
|
|
1578
|
+
default:
|
|
1579
|
+
if (statusCode && statusCode >= 400) {
|
|
1580
|
+
throw new GptCoreError(message, errorOptions);
|
|
1581
|
+
}
|
|
1582
|
+
throw new NetworkError(message, errorOptions);
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
|
|
1434
1586
|
// src/base-client.ts
|
|
1435
1587
|
var BaseClient = class {
|
|
1436
1588
|
constructor(config = {}) {
|
|
1589
|
+
this.config = config;
|
|
1590
|
+
this.retryConfig = config.retry !== void 0 ? config.retry : {
|
|
1591
|
+
maxRetries: 3,
|
|
1592
|
+
initialDelay: 1e3,
|
|
1593
|
+
maxDelay: 32e3,
|
|
1594
|
+
retryableStatusCodes: [429, 500, 502, 503, 504]
|
|
1595
|
+
};
|
|
1437
1596
|
if (config.baseUrl) {
|
|
1438
1597
|
client.setConfig({ baseUrl: config.baseUrl });
|
|
1439
1598
|
}
|
|
@@ -1448,6 +1607,12 @@ var BaseClient = class {
|
|
|
1448
1607
|
}
|
|
1449
1608
|
return req;
|
|
1450
1609
|
});
|
|
1610
|
+
client.interceptors.response.use((response) => {
|
|
1611
|
+
if (response.error) {
|
|
1612
|
+
handleApiError(response.error);
|
|
1613
|
+
}
|
|
1614
|
+
return response;
|
|
1615
|
+
});
|
|
1451
1616
|
}
|
|
1452
1617
|
unwrap(resource) {
|
|
1453
1618
|
if (!resource) return null;
|
|
@@ -1456,64 +1621,176 @@ var BaseClient = class {
|
|
|
1456
1621
|
}
|
|
1457
1622
|
return resource;
|
|
1458
1623
|
}
|
|
1624
|
+
handleError(error) {
|
|
1625
|
+
return handleApiError(error);
|
|
1626
|
+
}
|
|
1459
1627
|
};
|
|
1460
1628
|
|
|
1629
|
+
// src/pagination.ts
|
|
1630
|
+
async function* paginateAll(fetcher, options = {}) {
|
|
1631
|
+
const pageSize = options.pageSize || 20;
|
|
1632
|
+
const limit = options.limit;
|
|
1633
|
+
let page = 1;
|
|
1634
|
+
let totalYielded = 0;
|
|
1635
|
+
while (true) {
|
|
1636
|
+
const response = await fetcher(page, pageSize);
|
|
1637
|
+
for (const item of response.data) {
|
|
1638
|
+
yield item;
|
|
1639
|
+
totalYielded++;
|
|
1640
|
+
if (limit && totalYielded >= limit) {
|
|
1641
|
+
return;
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
if (!response.links?.next || response.data.length === 0) {
|
|
1645
|
+
break;
|
|
1646
|
+
}
|
|
1647
|
+
page++;
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
async function paginateToArray(fetcher, options = {}) {
|
|
1651
|
+
const results = [];
|
|
1652
|
+
for await (const item of paginateAll(fetcher, options)) {
|
|
1653
|
+
results.push(item);
|
|
1654
|
+
}
|
|
1655
|
+
return results;
|
|
1656
|
+
}
|
|
1657
|
+
|
|
1658
|
+
// src/schemas/requests.ts
|
|
1659
|
+
var import_zod = require("zod");
|
|
1660
|
+
var LoginRequestSchema = import_zod.z.object({
|
|
1661
|
+
email: import_zod.z.string().email(),
|
|
1662
|
+
password: import_zod.z.string().min(8)
|
|
1663
|
+
});
|
|
1664
|
+
var RegisterRequestSchema = import_zod.z.object({
|
|
1665
|
+
email: import_zod.z.string().email(),
|
|
1666
|
+
password: import_zod.z.string().min(8),
|
|
1667
|
+
password_confirmation: import_zod.z.string().min(8)
|
|
1668
|
+
}).refine((data) => data.password === data.password_confirmation, {
|
|
1669
|
+
message: "Passwords don't match",
|
|
1670
|
+
path: ["password_confirmation"]
|
|
1671
|
+
});
|
|
1672
|
+
var ApiKeyCreateSchema = import_zod.z.object({
|
|
1673
|
+
name: import_zod.z.string().min(1).max(255)
|
|
1674
|
+
});
|
|
1675
|
+
var ApiKeyAllocateSchema = import_zod.z.object({
|
|
1676
|
+
amount: import_zod.z.number().positive(),
|
|
1677
|
+
description: import_zod.z.string().min(1)
|
|
1678
|
+
});
|
|
1679
|
+
var ApplicationCreateSchema = import_zod.z.object({
|
|
1680
|
+
name: import_zod.z.string().min(1).max(255),
|
|
1681
|
+
slug: import_zod.z.string().regex(/^[a-z0-9-]+$/).optional(),
|
|
1682
|
+
description: import_zod.z.string().optional()
|
|
1683
|
+
});
|
|
1684
|
+
var WorkspaceCreateSchema = import_zod.z.object({
|
|
1685
|
+
name: import_zod.z.string().min(1).max(255),
|
|
1686
|
+
slug: import_zod.z.string().regex(/^[a-z0-9-]+$/).optional()
|
|
1687
|
+
});
|
|
1688
|
+
var InvitationCreateSchema = import_zod.z.object({
|
|
1689
|
+
email: import_zod.z.string().email(),
|
|
1690
|
+
role: import_zod.z.string(),
|
|
1691
|
+
scope_type: import_zod.z.enum(["tenant", "workspace"]),
|
|
1692
|
+
scope_id: import_zod.z.string().uuid()
|
|
1693
|
+
});
|
|
1694
|
+
var AgentCreateSchema = import_zod.z.object({
|
|
1695
|
+
name: import_zod.z.string().min(1).max(255),
|
|
1696
|
+
prompt_template: import_zod.z.string().min(1)
|
|
1697
|
+
});
|
|
1698
|
+
var ThreadCreateSchema = import_zod.z.object({
|
|
1699
|
+
title: import_zod.z.string().max(255).optional()
|
|
1700
|
+
});
|
|
1701
|
+
var MessageSendSchema = import_zod.z.object({
|
|
1702
|
+
content: import_zod.z.string().min(1)
|
|
1703
|
+
});
|
|
1704
|
+
var SearchRequestSchema = import_zod.z.object({
|
|
1705
|
+
query: import_zod.z.string().min(1),
|
|
1706
|
+
top_k: import_zod.z.number().int().positive().max(100).default(5)
|
|
1707
|
+
});
|
|
1708
|
+
var EmbedRequestSchema = import_zod.z.object({
|
|
1709
|
+
text: import_zod.z.string().min(1),
|
|
1710
|
+
workspace_id: import_zod.z.string().uuid().optional()
|
|
1711
|
+
});
|
|
1712
|
+
var DocumentUploadBase64Schema = import_zod.z.object({
|
|
1713
|
+
filename: import_zod.z.string().min(1),
|
|
1714
|
+
content: import_zod.z.string().min(1)
|
|
1715
|
+
// base64 content
|
|
1716
|
+
});
|
|
1717
|
+
var BucketCreateSchema = import_zod.z.object({
|
|
1718
|
+
name: import_zod.z.string().min(1).max(255),
|
|
1719
|
+
type: import_zod.z.enum(["public", "private"])
|
|
1720
|
+
});
|
|
1721
|
+
var PresignedUploadSchema = import_zod.z.object({
|
|
1722
|
+
filename: import_zod.z.string().min(1),
|
|
1723
|
+
content_type: import_zod.z.string().min(1)
|
|
1724
|
+
});
|
|
1725
|
+
var PresignedDownloadSchema = import_zod.z.object({
|
|
1726
|
+
file_id: import_zod.z.string().uuid()
|
|
1727
|
+
});
|
|
1728
|
+
|
|
1461
1729
|
// src/gpt-client.ts
|
|
1462
1730
|
var GptClient = class extends BaseClient {
|
|
1463
1731
|
constructor(config) {
|
|
1464
1732
|
super(config);
|
|
1465
1733
|
this.identity = {
|
|
1466
1734
|
login: async (email, password) => {
|
|
1735
|
+
const validated = LoginRequestSchema.parse({ email, password });
|
|
1467
1736
|
const { data, error } = await UserService.postUsersAuthLogin({
|
|
1468
|
-
body: {
|
|
1737
|
+
body: {
|
|
1738
|
+
data: {
|
|
1739
|
+
type: "user",
|
|
1740
|
+
attributes: validated
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1469
1743
|
});
|
|
1470
|
-
if (error)
|
|
1744
|
+
if (error) this.handleError(error);
|
|
1471
1745
|
return {
|
|
1472
1746
|
user: this.unwrap(data?.data),
|
|
1473
1747
|
token: data?.meta?.token
|
|
1474
1748
|
};
|
|
1475
1749
|
},
|
|
1476
1750
|
register: async (email, password, password_confirmation) => {
|
|
1751
|
+
const validated = RegisterRequestSchema.parse({ email, password, password_confirmation });
|
|
1477
1752
|
const { data, error } = await UserService.postUsersAuthRegister({
|
|
1478
1753
|
body: {
|
|
1479
1754
|
data: {
|
|
1480
1755
|
type: "user",
|
|
1481
|
-
attributes:
|
|
1756
|
+
attributes: validated
|
|
1482
1757
|
}
|
|
1483
1758
|
}
|
|
1484
1759
|
});
|
|
1485
|
-
if (error)
|
|
1760
|
+
if (error) this.handleError(error);
|
|
1486
1761
|
return this.unwrap(data?.data);
|
|
1487
1762
|
},
|
|
1488
1763
|
me: async () => {
|
|
1489
1764
|
const { data, error } = await UserService.getUsersMe();
|
|
1490
|
-
if (error)
|
|
1765
|
+
if (error) this.handleError(error);
|
|
1491
1766
|
return this.unwrap(data?.data);
|
|
1492
1767
|
},
|
|
1493
1768
|
profile: async () => {
|
|
1494
1769
|
const { data, error } = await UserProfileService.getUserProfilesMe();
|
|
1495
|
-
if (error)
|
|
1770
|
+
if (error) this.handleError(error);
|
|
1496
1771
|
return this.unwrap(data?.data);
|
|
1497
1772
|
},
|
|
1498
1773
|
apiKeys: {
|
|
1499
1774
|
list: async () => {
|
|
1500
1775
|
const { data, error } = await ApiKeyService.getApiKeys();
|
|
1501
|
-
if (error)
|
|
1776
|
+
if (error) this.handleError(error);
|
|
1502
1777
|
return this.unwrap(data?.data);
|
|
1503
1778
|
},
|
|
1504
1779
|
create: async (name) => {
|
|
1780
|
+
const validated = ApiKeyCreateSchema.parse({ name });
|
|
1505
1781
|
const { data, error } = await ApiKeyService.postApiKeys({
|
|
1506
|
-
body: { data: { type: "api_key", attributes:
|
|
1782
|
+
body: { data: { type: "api_key", attributes: validated } }
|
|
1507
1783
|
});
|
|
1508
|
-
if (error)
|
|
1784
|
+
if (error) this.handleError(error);
|
|
1509
1785
|
return this.unwrap(data?.data);
|
|
1510
1786
|
},
|
|
1511
1787
|
allocate: async (id, amount, description) => {
|
|
1788
|
+
const validated = ApiKeyAllocateSchema.parse({ amount, description });
|
|
1512
1789
|
const { error } = await ApiKeyService.patchApiKeysByIdAllocate({
|
|
1513
1790
|
path: { id },
|
|
1514
|
-
body: { data: { type: "api_key", id, attributes:
|
|
1791
|
+
body: { data: { type: "api_key", id, attributes: validated } }
|
|
1515
1792
|
});
|
|
1516
|
-
if (error)
|
|
1793
|
+
if (error) this.handleError(error);
|
|
1517
1794
|
return true;
|
|
1518
1795
|
}
|
|
1519
1796
|
}
|
|
@@ -1522,68 +1799,76 @@ var GptClient = class extends BaseClient {
|
|
|
1522
1799
|
applications: {
|
|
1523
1800
|
list: async () => {
|
|
1524
1801
|
const { data, error } = await ApplicationService.getApplications();
|
|
1525
|
-
if (error)
|
|
1802
|
+
if (error) this.handleError(error);
|
|
1526
1803
|
return this.unwrap(data?.data);
|
|
1527
1804
|
},
|
|
1528
1805
|
create: async (attributes) => {
|
|
1806
|
+
const validated = ApplicationCreateSchema.parse(attributes);
|
|
1529
1807
|
const { data, error } = await ApplicationService.postApplications({
|
|
1530
|
-
body: { data: { type: "application", attributes } }
|
|
1808
|
+
body: { data: { type: "application", attributes: validated } }
|
|
1531
1809
|
});
|
|
1532
|
-
if (error)
|
|
1810
|
+
if (error) this.handleError(error);
|
|
1533
1811
|
return this.unwrap(data?.data);
|
|
1534
1812
|
},
|
|
1535
1813
|
getBySlug: async (slug) => {
|
|
1536
1814
|
const { data, error } = await ApplicationService.getApplicationsBySlugBySlug({
|
|
1537
1815
|
path: { slug }
|
|
1538
1816
|
});
|
|
1539
|
-
if (error)
|
|
1817
|
+
if (error) this.handleError(error);
|
|
1540
1818
|
return this.unwrap(data?.data);
|
|
1541
1819
|
}
|
|
1542
1820
|
},
|
|
1543
1821
|
workspaces: {
|
|
1544
1822
|
list: async () => {
|
|
1545
1823
|
const { data, error } = await WorkspaceService.getWorkspaces();
|
|
1546
|
-
if (error)
|
|
1824
|
+
if (error) this.handleError(error);
|
|
1547
1825
|
return this.unwrap(data?.data);
|
|
1548
1826
|
},
|
|
1549
1827
|
mine: async () => {
|
|
1550
1828
|
const { data, error } = await WorkspaceService.getWorkspacesMine();
|
|
1551
|
-
if (error)
|
|
1829
|
+
if (error) this.handleError(error);
|
|
1552
1830
|
return this.unwrap(data?.data);
|
|
1553
1831
|
},
|
|
1554
1832
|
create: async (name, slug) => {
|
|
1833
|
+
const validated = WorkspaceCreateSchema.parse({ name, slug });
|
|
1555
1834
|
const { data, error } = await WorkspaceService.postWorkspaces({
|
|
1556
|
-
body: { data: { type: "workspace", attributes:
|
|
1835
|
+
body: { data: { type: "workspace", attributes: validated } }
|
|
1557
1836
|
});
|
|
1558
|
-
if (error)
|
|
1837
|
+
if (error) this.handleError(error);
|
|
1559
1838
|
return this.unwrap(data?.data);
|
|
1560
1839
|
}
|
|
1561
1840
|
},
|
|
1562
1841
|
tenants: {
|
|
1563
1842
|
list: async () => {
|
|
1564
1843
|
const { data, error } = await TenantService.getTenants();
|
|
1565
|
-
if (error)
|
|
1844
|
+
if (error) this.handleError(error);
|
|
1566
1845
|
return this.unwrap(data?.data);
|
|
1567
1846
|
}
|
|
1568
1847
|
},
|
|
1569
1848
|
invitations: {
|
|
1570
1849
|
list: async () => {
|
|
1571
1850
|
const { data, error } = await InvitationService.getInvitations();
|
|
1572
|
-
if (error)
|
|
1851
|
+
if (error) this.handleError(error);
|
|
1573
1852
|
return this.unwrap(data?.data);
|
|
1574
1853
|
},
|
|
1575
1854
|
invite: async (email, role2, scopeType, scopeId) => {
|
|
1855
|
+
const validated = InvitationCreateSchema.parse({
|
|
1856
|
+
email,
|
|
1857
|
+
role: role2,
|
|
1858
|
+
scope_type: scopeType,
|
|
1859
|
+
scope_id: scopeId
|
|
1860
|
+
});
|
|
1576
1861
|
const { error } = await InvitationService.postInvitationsInvite({
|
|
1577
|
-
body:
|
|
1862
|
+
body: validated
|
|
1578
1863
|
});
|
|
1579
|
-
if (error)
|
|
1864
|
+
if (error) this.handleError(error);
|
|
1580
1865
|
return true;
|
|
1581
1866
|
}
|
|
1582
1867
|
},
|
|
1583
1868
|
auditLogs: {
|
|
1584
1869
|
list: async () => {
|
|
1585
1870
|
const { data, error } = await AuditLogService.getAuditLogs();
|
|
1586
|
-
if (error)
|
|
1871
|
+
if (error) this.handleError(error);
|
|
1587
1872
|
return this.unwrap(data?.data);
|
|
1588
1873
|
}
|
|
1589
1874
|
}
|
|
@@ -1592,51 +1877,75 @@ var GptClient = class extends BaseClient {
|
|
|
1592
1877
|
agents: {
|
|
1593
1878
|
list: async () => {
|
|
1594
1879
|
const { data, error } = await AgentService.getAgents();
|
|
1595
|
-
if (error)
|
|
1880
|
+
if (error) this.handleError(error);
|
|
1596
1881
|
return this.unwrap(data?.data);
|
|
1597
1882
|
},
|
|
1598
1883
|
create: async (name, promptTemplate) => {
|
|
1884
|
+
const validated = AgentCreateSchema.parse({ name, prompt_template: promptTemplate });
|
|
1599
1885
|
const { data, error } = await AgentService.postAgents({
|
|
1600
|
-
body: { data: { type: "agent", attributes:
|
|
1886
|
+
body: { data: { type: "agent", attributes: validated } }
|
|
1601
1887
|
});
|
|
1602
|
-
if (error)
|
|
1888
|
+
if (error) this.handleError(error);
|
|
1603
1889
|
return this.unwrap(data?.data);
|
|
1604
1890
|
}
|
|
1605
1891
|
},
|
|
1606
1892
|
threads: {
|
|
1607
1893
|
list: async () => {
|
|
1608
1894
|
const { data, error } = await ThreadService.getThreads();
|
|
1609
|
-
if (error)
|
|
1895
|
+
if (error) this.handleError(error);
|
|
1610
1896
|
return this.unwrap(data?.data);
|
|
1611
1897
|
},
|
|
1612
1898
|
create: async (title) => {
|
|
1899
|
+
const validated = ThreadCreateSchema.parse({ title });
|
|
1613
1900
|
const { data, error } = await ThreadService.postThreads({
|
|
1614
|
-
body: { data: { type: "thread", attributes:
|
|
1901
|
+
body: { data: { type: "thread", attributes: validated } }
|
|
1615
1902
|
});
|
|
1616
|
-
if (error)
|
|
1903
|
+
if (error) this.handleError(error);
|
|
1617
1904
|
return this.unwrap(data?.data);
|
|
1618
1905
|
},
|
|
1619
1906
|
sendMessage: async (threadId, content) => {
|
|
1907
|
+
const validated = MessageSendSchema.parse({ content });
|
|
1620
1908
|
const { data, error } = await ThreadService.postThreadsByIdMessages({
|
|
1621
1909
|
path: { id: threadId },
|
|
1622
|
-
body: { data: { attributes:
|
|
1910
|
+
body: { data: { attributes: validated } }
|
|
1623
1911
|
});
|
|
1624
|
-
if (error)
|
|
1912
|
+
if (error) this.handleError(error);
|
|
1625
1913
|
return this.unwrap(data);
|
|
1626
1914
|
}
|
|
1915
|
+
/**
|
|
1916
|
+
* Note: For streaming message responses, use the streaming utilities:
|
|
1917
|
+
*
|
|
1918
|
+
* ```typescript
|
|
1919
|
+
* import { streamMessage } from '@gpt-core/client';
|
|
1920
|
+
*
|
|
1921
|
+
* // Make streaming request to backend
|
|
1922
|
+
* const response = await fetch(`${baseUrl}/threads/${threadId}/messages/stream`, {
|
|
1923
|
+
* method: 'POST',
|
|
1924
|
+
* headers: { 'Accept': 'text/event-stream' },
|
|
1925
|
+
* body: JSON.stringify({ content: 'Hello' })
|
|
1926
|
+
* });
|
|
1927
|
+
*
|
|
1928
|
+
* // Stream the response
|
|
1929
|
+
* for await (const chunk of streamMessage(response)) {
|
|
1930
|
+
* console.log(chunk.content);
|
|
1931
|
+
* }
|
|
1932
|
+
* ```
|
|
1933
|
+
*/
|
|
1627
1934
|
},
|
|
1628
1935
|
search: async (query, top_k = 5) => {
|
|
1936
|
+
const validated = SearchRequestSchema.parse({ query, top_k });
|
|
1629
1937
|
const { data, error } = await SearchService.postAiSearch({
|
|
1630
|
-
body:
|
|
1938
|
+
body: validated
|
|
1631
1939
|
});
|
|
1632
|
-
if (error)
|
|
1940
|
+
if (error) this.handleError(error);
|
|
1633
1941
|
return this.unwrap(data);
|
|
1634
1942
|
},
|
|
1635
1943
|
embed: async (text, workspaceId) => {
|
|
1944
|
+
const validated = EmbedRequestSchema.parse({ text, workspace_id: workspaceId });
|
|
1636
1945
|
const { data, error } = await EmbeddingService.postAiEmbed({
|
|
1637
|
-
body:
|
|
1946
|
+
body: validated
|
|
1638
1947
|
});
|
|
1639
|
-
if (error)
|
|
1948
|
+
if (error) this.handleError(error);
|
|
1640
1949
|
return this.unwrap(data);
|
|
1641
1950
|
}
|
|
1642
1951
|
};
|
|
@@ -1644,39 +1953,40 @@ var GptClient = class extends BaseClient {
|
|
|
1644
1953
|
documents: {
|
|
1645
1954
|
list: async () => {
|
|
1646
1955
|
const { data, error } = await DocumentService.getDocuments();
|
|
1647
|
-
if (error)
|
|
1956
|
+
if (error) this.handleError(error);
|
|
1648
1957
|
return this.unwrap(data?.data);
|
|
1649
1958
|
},
|
|
1650
1959
|
upload: async (_file, _filename) => {
|
|
1651
1960
|
throw new Error("Use uploadBase64 for now");
|
|
1652
1961
|
},
|
|
1653
1962
|
uploadBase64: async (filename, base64Content) => {
|
|
1963
|
+
const validated = DocumentUploadBase64Schema.parse({ filename, content: base64Content });
|
|
1654
1964
|
const { data, error } = await DocumentService.postDocuments({
|
|
1655
|
-
body: { data: { type: "document", attributes:
|
|
1965
|
+
body: { data: { type: "document", attributes: validated } }
|
|
1656
1966
|
});
|
|
1657
|
-
if (error)
|
|
1967
|
+
if (error) this.handleError(error);
|
|
1658
1968
|
return this.unwrap(data?.data);
|
|
1659
1969
|
},
|
|
1660
1970
|
get: async (id) => {
|
|
1661
1971
|
const { data, error } = await DocumentService.getDocumentsById({ path: { id } });
|
|
1662
|
-
if (error)
|
|
1972
|
+
if (error) this.handleError(error);
|
|
1663
1973
|
return this.unwrap(data?.data);
|
|
1664
1974
|
},
|
|
1665
1975
|
delete: async (id) => {
|
|
1666
1976
|
const { error } = await DocumentService.deleteDocumentsById({ path: { id } });
|
|
1667
|
-
if (error)
|
|
1977
|
+
if (error) this.handleError(error);
|
|
1668
1978
|
return true;
|
|
1669
1979
|
},
|
|
1670
1980
|
analyze: async (id) => {
|
|
1671
1981
|
const { error } = await DocumentService.postDocumentsByIdAnalyze({ path: { id }, body: {} });
|
|
1672
|
-
if (error)
|
|
1982
|
+
if (error) this.handleError(error);
|
|
1673
1983
|
return true;
|
|
1674
1984
|
}
|
|
1675
1985
|
},
|
|
1676
1986
|
results: {
|
|
1677
1987
|
list: async () => {
|
|
1678
1988
|
const { data, error } = await ExtractionResultService.getExtractionResults();
|
|
1679
|
-
if (error)
|
|
1989
|
+
if (error) this.handleError(error);
|
|
1680
1990
|
return this.unwrap(data?.data);
|
|
1681
1991
|
}
|
|
1682
1992
|
}
|
|
@@ -1685,30 +1995,33 @@ var GptClient = class extends BaseClient {
|
|
|
1685
1995
|
buckets: {
|
|
1686
1996
|
list: async () => {
|
|
1687
1997
|
const { data, error } = await BucketService.getBuckets();
|
|
1688
|
-
if (error)
|
|
1998
|
+
if (error) this.handleError(error);
|
|
1689
1999
|
return this.unwrap(data?.data);
|
|
1690
2000
|
},
|
|
1691
2001
|
create: async (name, isPublic = false) => {
|
|
2002
|
+
const validated = BucketCreateSchema.parse({ name, type: isPublic ? "public" : "private" });
|
|
1692
2003
|
const { data, error } = await BucketService.postBuckets({
|
|
1693
|
-
body: { data: { type: "bucket", attributes:
|
|
2004
|
+
body: { data: { type: "bucket", attributes: validated } }
|
|
1694
2005
|
});
|
|
1695
|
-
if (error)
|
|
2006
|
+
if (error) this.handleError(error);
|
|
1696
2007
|
return this.unwrap(data?.data);
|
|
1697
2008
|
}
|
|
1698
2009
|
},
|
|
1699
2010
|
presigned: {
|
|
1700
2011
|
upload: async (filename, contentType) => {
|
|
2012
|
+
const validated = PresignedUploadSchema.parse({ filename, content_type: contentType });
|
|
1701
2013
|
const { data, error } = await PresignedUrlService.postStorageSignUpload({
|
|
1702
|
-
body:
|
|
2014
|
+
body: validated
|
|
1703
2015
|
});
|
|
1704
|
-
if (error)
|
|
2016
|
+
if (error) this.handleError(error);
|
|
1705
2017
|
return this.unwrap(data);
|
|
1706
2018
|
},
|
|
1707
2019
|
download: async (fileId) => {
|
|
2020
|
+
const validated = PresignedDownloadSchema.parse({ file_id: fileId });
|
|
1708
2021
|
const { data, error } = await PresignedUrlService.postStorageSignDownload({
|
|
1709
|
-
body:
|
|
2022
|
+
body: validated
|
|
1710
2023
|
});
|
|
1711
|
-
if (error)
|
|
2024
|
+
if (error) this.handleError(error);
|
|
1712
2025
|
return this.unwrap(data);
|
|
1713
2026
|
}
|
|
1714
2027
|
}
|
|
@@ -1717,14 +2030,14 @@ var GptClient = class extends BaseClient {
|
|
|
1717
2030
|
wallet: {
|
|
1718
2031
|
get: async () => {
|
|
1719
2032
|
const { data, error } = await WalletService.getWallet();
|
|
1720
|
-
if (error)
|
|
2033
|
+
if (error) this.handleError(error);
|
|
1721
2034
|
return this.unwrap(data?.data);
|
|
1722
2035
|
}
|
|
1723
2036
|
},
|
|
1724
2037
|
plans: {
|
|
1725
2038
|
list: async () => {
|
|
1726
2039
|
const { data, error } = await PlanService.getPlans();
|
|
1727
|
-
if (error)
|
|
2040
|
+
if (error) this.handleError(error);
|
|
1728
2041
|
return this.unwrap(data?.data);
|
|
1729
2042
|
}
|
|
1730
2043
|
}
|
|
@@ -2005,9 +2318,158 @@ var type = {
|
|
|
2005
2318
|
PUBLIC: "public",
|
|
2006
2319
|
PRIVATE: "private"
|
|
2007
2320
|
};
|
|
2321
|
+
|
|
2322
|
+
// src/utils/retry.ts
|
|
2323
|
+
var DEFAULT_RETRY_CONFIG = {
|
|
2324
|
+
maxRetries: 3,
|
|
2325
|
+
initialDelay: 1e3,
|
|
2326
|
+
// 1 second
|
|
2327
|
+
maxDelay: 32e3,
|
|
2328
|
+
// 32 seconds
|
|
2329
|
+
retryableStatusCodes: [429, 500, 502, 503, 504]
|
|
2330
|
+
};
|
|
2331
|
+
function isRetryableError(error, retryableStatusCodes) {
|
|
2332
|
+
const statusCode = error?.status || error?.statusCode || error?.response?.status;
|
|
2333
|
+
return retryableStatusCodes.includes(statusCode);
|
|
2334
|
+
}
|
|
2335
|
+
function calculateBackoff(attempt, initialDelay, maxDelay, retryAfter) {
|
|
2336
|
+
if (retryAfter) {
|
|
2337
|
+
return Math.min(retryAfter * 1e3, maxDelay);
|
|
2338
|
+
}
|
|
2339
|
+
const exponentialDelay = initialDelay * Math.pow(2, attempt);
|
|
2340
|
+
const jitter = Math.random() * 0.3 * exponentialDelay;
|
|
2341
|
+
const delay = exponentialDelay + jitter;
|
|
2342
|
+
return Math.min(delay, maxDelay);
|
|
2343
|
+
}
|
|
2344
|
+
function sleep(ms) {
|
|
2345
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
2346
|
+
}
|
|
2347
|
+
async function retryWithBackoff(fn, config = DEFAULT_RETRY_CONFIG) {
|
|
2348
|
+
let lastError;
|
|
2349
|
+
for (let attempt = 0; attempt <= config.maxRetries; attempt++) {
|
|
2350
|
+
try {
|
|
2351
|
+
return await fn();
|
|
2352
|
+
} catch (error) {
|
|
2353
|
+
lastError = error;
|
|
2354
|
+
if (attempt === config.maxRetries || !isRetryableError(error, config.retryableStatusCodes)) {
|
|
2355
|
+
throw error;
|
|
2356
|
+
}
|
|
2357
|
+
const errorWithHeaders = error;
|
|
2358
|
+
const retryAfter = errorWithHeaders?.response?.headers?.get?.("retry-after") || errorWithHeaders?.response?.headers?.["retry-after"] || errorWithHeaders?.headers?.get?.("retry-after") || errorWithHeaders?.headers?.["retry-after"];
|
|
2359
|
+
const retryAfterSeconds = retryAfter ? parseInt(retryAfter, 10) : void 0;
|
|
2360
|
+
const delay = calculateBackoff(
|
|
2361
|
+
attempt,
|
|
2362
|
+
config.initialDelay,
|
|
2363
|
+
config.maxDelay,
|
|
2364
|
+
retryAfterSeconds
|
|
2365
|
+
);
|
|
2366
|
+
await sleep(delay);
|
|
2367
|
+
}
|
|
2368
|
+
}
|
|
2369
|
+
throw lastError;
|
|
2370
|
+
}
|
|
2371
|
+
function withRetry(fn, config) {
|
|
2372
|
+
const retryConfig = { ...DEFAULT_RETRY_CONFIG, ...config };
|
|
2373
|
+
return async (...args) => {
|
|
2374
|
+
return retryWithBackoff(() => fn(...args), retryConfig);
|
|
2375
|
+
};
|
|
2376
|
+
}
|
|
2377
|
+
|
|
2378
|
+
// src/streaming.ts
|
|
2379
|
+
async function* streamSSE(response, options = {}) {
|
|
2380
|
+
if (!response.body) {
|
|
2381
|
+
throw new Error("Response body is null");
|
|
2382
|
+
}
|
|
2383
|
+
const reader = response.body.getReader();
|
|
2384
|
+
const decoder = new TextDecoder();
|
|
2385
|
+
let buffer = "";
|
|
2386
|
+
try {
|
|
2387
|
+
while (true) {
|
|
2388
|
+
const { done, value } = await reader.read();
|
|
2389
|
+
if (done) {
|
|
2390
|
+
break;
|
|
2391
|
+
}
|
|
2392
|
+
if (options.signal?.aborted) {
|
|
2393
|
+
reader.cancel();
|
|
2394
|
+
throw new Error("Stream aborted");
|
|
2395
|
+
}
|
|
2396
|
+
buffer += decoder.decode(value, { stream: true });
|
|
2397
|
+
const lines = buffer.split("\n");
|
|
2398
|
+
buffer = lines.pop() || "";
|
|
2399
|
+
for (const line of lines) {
|
|
2400
|
+
if (line.startsWith("data: ")) {
|
|
2401
|
+
const data = line.slice(6);
|
|
2402
|
+
if (data === "[DONE]" || data.trim() === "") {
|
|
2403
|
+
continue;
|
|
2404
|
+
}
|
|
2405
|
+
try {
|
|
2406
|
+
const parsed = JSON.parse(data);
|
|
2407
|
+
yield parsed;
|
|
2408
|
+
} catch (e) {
|
|
2409
|
+
yield data;
|
|
2410
|
+
}
|
|
2411
|
+
}
|
|
2412
|
+
}
|
|
2413
|
+
}
|
|
2414
|
+
} catch (error) {
|
|
2415
|
+
if (options.onError) {
|
|
2416
|
+
options.onError(error);
|
|
2417
|
+
}
|
|
2418
|
+
throw error;
|
|
2419
|
+
} finally {
|
|
2420
|
+
reader.releaseLock();
|
|
2421
|
+
}
|
|
2422
|
+
}
|
|
2423
|
+
async function* streamMessage(response, options = {}) {
|
|
2424
|
+
for await (const chunk of streamSSE(response, options)) {
|
|
2425
|
+
yield chunk;
|
|
2426
|
+
if (chunk.type === "done" || chunk.type === "error") {
|
|
2427
|
+
break;
|
|
2428
|
+
}
|
|
2429
|
+
}
|
|
2430
|
+
}
|
|
2431
|
+
async function collectStreamedMessage(stream) {
|
|
2432
|
+
let fullMessage = "";
|
|
2433
|
+
for await (const chunk of stream) {
|
|
2434
|
+
if (chunk.type === "content" && chunk.content) {
|
|
2435
|
+
fullMessage += chunk.content;
|
|
2436
|
+
} else if (chunk.type === "error") {
|
|
2437
|
+
throw new Error(chunk.error || "Stream error");
|
|
2438
|
+
}
|
|
2439
|
+
}
|
|
2440
|
+
return fullMessage;
|
|
2441
|
+
}
|
|
2008
2442
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2009
2443
|
0 && (module.exports = {
|
|
2444
|
+
AgentCreateSchema,
|
|
2445
|
+
ApiKeyAllocateSchema,
|
|
2446
|
+
ApiKeyCreateSchema,
|
|
2447
|
+
ApplicationCreateSchema,
|
|
2448
|
+
AuthenticationError,
|
|
2449
|
+
AuthorizationError,
|
|
2450
|
+
BucketCreateSchema,
|
|
2451
|
+
DEFAULT_RETRY_CONFIG,
|
|
2452
|
+
DocumentUploadBase64Schema,
|
|
2453
|
+
EmbedRequestSchema,
|
|
2010
2454
|
GptClient,
|
|
2455
|
+
GptCoreError,
|
|
2456
|
+
InvitationCreateSchema,
|
|
2457
|
+
LoginRequestSchema,
|
|
2458
|
+
MessageSendSchema,
|
|
2459
|
+
NetworkError,
|
|
2460
|
+
NotFoundError,
|
|
2461
|
+
PresignedDownloadSchema,
|
|
2462
|
+
PresignedUploadSchema,
|
|
2463
|
+
RateLimitError,
|
|
2464
|
+
RegisterRequestSchema,
|
|
2465
|
+
SearchRequestSchema,
|
|
2466
|
+
ServerError,
|
|
2467
|
+
ThreadCreateSchema,
|
|
2468
|
+
TimeoutError,
|
|
2469
|
+
ValidationError,
|
|
2470
|
+
WorkspaceCreateSchema,
|
|
2471
|
+
calculateBackoff,
|
|
2472
|
+
collectStreamedMessage,
|
|
2011
2473
|
eq,
|
|
2012
2474
|
eq2,
|
|
2013
2475
|
eq3,
|
|
@@ -2033,6 +2495,8 @@ var type = {
|
|
|
2033
2495
|
greater_than_or_equal6,
|
|
2034
2496
|
greater_than_or_equal7,
|
|
2035
2497
|
greater_than_or_equal8,
|
|
2498
|
+
handleApiError,
|
|
2499
|
+
isRetryableError,
|
|
2036
2500
|
less_than,
|
|
2037
2501
|
less_than2,
|
|
2038
2502
|
less_than3,
|
|
@@ -2057,8 +2521,15 @@ var type = {
|
|
|
2057
2521
|
not_eq6,
|
|
2058
2522
|
not_eq7,
|
|
2059
2523
|
not_eq8,
|
|
2524
|
+
paginateAll,
|
|
2525
|
+
paginateToArray,
|
|
2526
|
+
retryWithBackoff,
|
|
2060
2527
|
role,
|
|
2528
|
+
sleep,
|
|
2061
2529
|
status,
|
|
2062
2530
|
status2,
|
|
2063
|
-
|
|
2531
|
+
streamMessage,
|
|
2532
|
+
streamSSE,
|
|
2533
|
+
type,
|
|
2534
|
+
withRetry
|
|
2064
2535
|
});
|