@axiom-lattice/client-sdk 4.2.1 → 4.3.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/dist/__tests__/project-rooms.test.d.ts +2 -0
- package/dist/__tests__/project-rooms.test.d.ts.map +1 -0
- package/dist/__tests__/project-rooms.test.js +144 -0
- package/dist/__tests__/project-rooms.test.js.map +1 -0
- package/dist/__tests__/sse-parser.test.d.ts +2 -0
- package/dist/__tests__/sse-parser.test.d.ts.map +1 -0
- package/dist/__tests__/sse-parser.test.js +17 -0
- package/dist/__tests__/sse-parser.test.js.map +1 -0
- package/dist/client.d.ts +2 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +17 -134
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +153 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +401 -131
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +399 -131
- package/dist/index.mjs.map +1 -1
- package/dist/project-rooms.d.ts +152 -0
- package/dist/project-rooms.d.ts.map +1 -0
- package/dist/project-rooms.js +240 -0
- package/dist/project-rooms.js.map +1 -0
- package/dist/sse-parser.d.ts +15 -0
- package/dist/sse-parser.d.ts.map +1 -0
- package/dist/sse-parser.js +101 -0
- package/dist/sse-parser.js.map +1 -0
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -949,7 +949,7 @@ var require_encoding = __commonJS({
|
|
|
949
949
|
}
|
|
950
950
|
});
|
|
951
951
|
}
|
|
952
|
-
TextEncoder.prototype.encode = function
|
|
952
|
+
TextEncoder.prototype.encode = function encode2(opt_string, options) {
|
|
953
953
|
opt_string = opt_string === void 0 ? "" : String(opt_string);
|
|
954
954
|
options = ToDictionary(options);
|
|
955
955
|
if (!this._do_not_flush)
|
|
@@ -2336,7 +2336,7 @@ var AbstractClient = class {
|
|
|
2336
2336
|
const query = searchParams.toString();
|
|
2337
2337
|
const response = await this.makeRequest(`/api/web-apps${query ? `?${query}` : ""}`);
|
|
2338
2338
|
return {
|
|
2339
|
-
records: response.data.records.map((
|
|
2339
|
+
records: response.data.records.map((record2) => this.hydrateAgentWebApp(record2)),
|
|
2340
2340
|
total: response.data.total
|
|
2341
2341
|
};
|
|
2342
2342
|
},
|
|
@@ -2977,19 +2977,19 @@ var AbstractClient = class {
|
|
|
2977
2977
|
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
2978
2978
|
throw new ApiError("Invalid Agent Web App response", 500, value);
|
|
2979
2979
|
}
|
|
2980
|
-
const
|
|
2980
|
+
const record2 = value;
|
|
2981
2981
|
const hydrateDate = (field) => {
|
|
2982
|
-
const raw =
|
|
2983
|
-
const
|
|
2982
|
+
const raw = record2[field];
|
|
2983
|
+
const date2 = raw instanceof Date ? new Date(raw.getTime()) : new Date(
|
|
2984
2984
|
typeof raw === "string" || typeof raw === "number" ? raw : Number.NaN
|
|
2985
2985
|
);
|
|
2986
|
-
if (Number.isNaN(
|
|
2986
|
+
if (Number.isNaN(date2.getTime())) {
|
|
2987
2987
|
throw new ApiError(`Invalid Agent Web App ${field}`, 500, value);
|
|
2988
2988
|
}
|
|
2989
|
-
return
|
|
2989
|
+
return date2;
|
|
2990
2990
|
};
|
|
2991
2991
|
return {
|
|
2992
|
-
...
|
|
2992
|
+
...record2,
|
|
2993
2993
|
createdAt: hydrateDate("createdAt"),
|
|
2994
2994
|
updatedAt: hydrateDate("updatedAt")
|
|
2995
2995
|
};
|
|
@@ -3429,6 +3429,381 @@ var ExportImportClient = class {
|
|
|
3429
3429
|
}
|
|
3430
3430
|
};
|
|
3431
3431
|
|
|
3432
|
+
// src/sse-parser.ts
|
|
3433
|
+
var SseParseError = class extends Error {
|
|
3434
|
+
constructor(message) {
|
|
3435
|
+
super(message);
|
|
3436
|
+
this.code = "INVALID_EVENT";
|
|
3437
|
+
this.name = "SseParseError";
|
|
3438
|
+
}
|
|
3439
|
+
};
|
|
3440
|
+
async function* parseSseBody(body, options = {}) {
|
|
3441
|
+
const reader = body.getReader();
|
|
3442
|
+
const decoder = new TextDecoder();
|
|
3443
|
+
let buffer = "";
|
|
3444
|
+
let event = "";
|
|
3445
|
+
let id;
|
|
3446
|
+
let retry;
|
|
3447
|
+
let data = [];
|
|
3448
|
+
const dispatch = () => {
|
|
3449
|
+
if (data.length === 0) {
|
|
3450
|
+
event = "";
|
|
3451
|
+
id = void 0;
|
|
3452
|
+
retry = void 0;
|
|
3453
|
+
return void 0;
|
|
3454
|
+
}
|
|
3455
|
+
let parsed;
|
|
3456
|
+
try {
|
|
3457
|
+
parsed = JSON.parse(data.join("\n"));
|
|
3458
|
+
} catch {
|
|
3459
|
+
throw new SseParseError("Invalid SSE JSON");
|
|
3460
|
+
}
|
|
3461
|
+
if (options.strictJsonObject && (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)))
|
|
3462
|
+
throw new SseParseError("SSE data must be an object");
|
|
3463
|
+
const result = { event, data: parsed, ...id === void 0 ? {} : { id }, ...retry === void 0 ? {} : { retry } };
|
|
3464
|
+
event = "";
|
|
3465
|
+
id = void 0;
|
|
3466
|
+
retry = void 0;
|
|
3467
|
+
data = [];
|
|
3468
|
+
return result;
|
|
3469
|
+
};
|
|
3470
|
+
const processLine = (raw) => {
|
|
3471
|
+
const line = raw.endsWith("\r") ? raw.slice(0, -1) : raw;
|
|
3472
|
+
if (line === "")
|
|
3473
|
+
return dispatch();
|
|
3474
|
+
if (line.startsWith(":"))
|
|
3475
|
+
return void 0;
|
|
3476
|
+
const separator = line.indexOf(":");
|
|
3477
|
+
const field = separator < 0 ? line : line.slice(0, separator);
|
|
3478
|
+
let value = separator < 0 ? "" : line.slice(separator + 1);
|
|
3479
|
+
if (value.startsWith(" "))
|
|
3480
|
+
value = value.slice(1);
|
|
3481
|
+
if (field === "event")
|
|
3482
|
+
event = value;
|
|
3483
|
+
else if (field === "data")
|
|
3484
|
+
data.push(value);
|
|
3485
|
+
else if (field === "id")
|
|
3486
|
+
id = value;
|
|
3487
|
+
else if (field === "retry") {
|
|
3488
|
+
const parsed = Number(value);
|
|
3489
|
+
if (!Number.isInteger(parsed) || parsed < 0 || !Number.isFinite(parsed))
|
|
3490
|
+
throw new SseParseError("Invalid SSE retry");
|
|
3491
|
+
retry = parsed;
|
|
3492
|
+
}
|
|
3493
|
+
return void 0;
|
|
3494
|
+
};
|
|
3495
|
+
try {
|
|
3496
|
+
while (true) {
|
|
3497
|
+
const chunk = await reader.read();
|
|
3498
|
+
if (chunk.done) {
|
|
3499
|
+
buffer += decoder.decode();
|
|
3500
|
+
break;
|
|
3501
|
+
}
|
|
3502
|
+
buffer += decoder.decode(chunk.value, { stream: true });
|
|
3503
|
+
let index = findLineBreak(buffer);
|
|
3504
|
+
while (index >= 0) {
|
|
3505
|
+
if (buffer[index] === "\r" && index === buffer.length - 1)
|
|
3506
|
+
break;
|
|
3507
|
+
const frame2 = processLine(buffer.slice(0, index));
|
|
3508
|
+
buffer = buffer.slice(index + lineBreakLength(buffer, index));
|
|
3509
|
+
if (frame2)
|
|
3510
|
+
yield frame2;
|
|
3511
|
+
index = findLineBreak(buffer);
|
|
3512
|
+
}
|
|
3513
|
+
}
|
|
3514
|
+
if (buffer) {
|
|
3515
|
+
const frame2 = processLine(buffer);
|
|
3516
|
+
if (frame2)
|
|
3517
|
+
yield frame2;
|
|
3518
|
+
}
|
|
3519
|
+
const frame = processLine("");
|
|
3520
|
+
if (frame)
|
|
3521
|
+
yield frame;
|
|
3522
|
+
} finally {
|
|
3523
|
+
await reader.cancel().catch(() => void 0);
|
|
3524
|
+
reader.releaseLock();
|
|
3525
|
+
}
|
|
3526
|
+
}
|
|
3527
|
+
function findLineBreak(value) {
|
|
3528
|
+
const lf = value.indexOf("\n");
|
|
3529
|
+
const cr = value.indexOf("\r");
|
|
3530
|
+
if (lf < 0)
|
|
3531
|
+
return cr;
|
|
3532
|
+
if (cr < 0)
|
|
3533
|
+
return lf;
|
|
3534
|
+
return Math.min(lf, cr);
|
|
3535
|
+
}
|
|
3536
|
+
function lineBreakLength(value, index) {
|
|
3537
|
+
return value[index] === "\r" && value[index + 1] === "\n" ? 2 : 1;
|
|
3538
|
+
}
|
|
3539
|
+
|
|
3540
|
+
// src/project-rooms.ts
|
|
3541
|
+
var ProjectRoomClientError = class extends Error {
|
|
3542
|
+
constructor(message, status, code, retryable, details) {
|
|
3543
|
+
super(message);
|
|
3544
|
+
this.status = status;
|
|
3545
|
+
this.code = code;
|
|
3546
|
+
this.retryable = retryable;
|
|
3547
|
+
this.details = details;
|
|
3548
|
+
this.name = "ProjectRoomClientError";
|
|
3549
|
+
}
|
|
3550
|
+
};
|
|
3551
|
+
var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3552
|
+
var date = (value, field) => {
|
|
3553
|
+
const result = value instanceof Date ? new Date(value.getTime()) : new Date(typeof value === "string" || typeof value === "number" ? value : Number.NaN);
|
|
3554
|
+
if (!Number.isFinite(result.getTime()))
|
|
3555
|
+
throw new ProjectRoomClientError(`Invalid ${field}`, 500, "INVALID_RESPONSE", false);
|
|
3556
|
+
return result;
|
|
3557
|
+
};
|
|
3558
|
+
var requiredString = (record2, field) => {
|
|
3559
|
+
if (typeof record2[field] !== "string")
|
|
3560
|
+
throw new ProjectRoomClientError(`Invalid ${field}`, 500, "INVALID_RESPONSE", false);
|
|
3561
|
+
return record2[field];
|
|
3562
|
+
};
|
|
3563
|
+
var oneOf = (value, values, field) => {
|
|
3564
|
+
if (typeof value !== "string" || !values.includes(value))
|
|
3565
|
+
throw new ProjectRoomClientError(`Invalid ${field}`, 500, "INVALID_RESPONSE", false);
|
|
3566
|
+
return value;
|
|
3567
|
+
};
|
|
3568
|
+
var messageSources = ["user", "agent", "task", "routine", "system"];
|
|
3569
|
+
var humanRoles = ["owner", "admin", "member", "viewer"];
|
|
3570
|
+
var memberStatuses = ["active", "removed"];
|
|
3571
|
+
var botRoles = ["coordinator", "specialist"];
|
|
3572
|
+
var botStatuses = ["active", "paused", "removed"];
|
|
3573
|
+
var exactKeys = (row, required, optional = []) => {
|
|
3574
|
+
if (required.some((key) => !Object.prototype.hasOwnProperty.call(row, key)) || Object.keys(row).some((key) => !required.includes(key) && !optional.includes(key)))
|
|
3575
|
+
throw new ProjectRoomClientError("Invalid Project Room response", 500, "INVALID_RESPONSE", false);
|
|
3576
|
+
};
|
|
3577
|
+
var hydrateAuthor = (value) => {
|
|
3578
|
+
const row = record(value);
|
|
3579
|
+
const type = oneOf(row.type, ["human", "bot", "system"], "author.type");
|
|
3580
|
+
if (type === "human") {
|
|
3581
|
+
exactKeys(row, ["type", "userId"]);
|
|
3582
|
+
return { type, userId: requiredString(row, "userId") };
|
|
3583
|
+
}
|
|
3584
|
+
if (type === "bot")
|
|
3585
|
+
return { type, membershipId: requiredString(row, "membershipId") };
|
|
3586
|
+
if (Object.keys(row).some((key) => key !== "type"))
|
|
3587
|
+
throw new ProjectRoomClientError("Invalid author", 500, "INVALID_RESPONSE", false);
|
|
3588
|
+
return { type };
|
|
3589
|
+
};
|
|
3590
|
+
var hydrateMentions = (value) => {
|
|
3591
|
+
if (!Array.isArray(value))
|
|
3592
|
+
throw new ProjectRoomClientError("Invalid mentions", 500, "INVALID_RESPONSE", false);
|
|
3593
|
+
return value.map((entry) => {
|
|
3594
|
+
const row = record(entry);
|
|
3595
|
+
const type = oneOf(row.type, ["bot", "team"], "mention.type");
|
|
3596
|
+
if (type === "team") {
|
|
3597
|
+
if (Object.keys(row).length !== 1)
|
|
3598
|
+
throw new ProjectRoomClientError("Invalid mention", 500, "INVALID_RESPONSE", false);
|
|
3599
|
+
return { type };
|
|
3600
|
+
}
|
|
3601
|
+
return { type, membershipId: requiredString(row, "membershipId") };
|
|
3602
|
+
});
|
|
3603
|
+
};
|
|
3604
|
+
var record = (value) => {
|
|
3605
|
+
if (!isRecord(value))
|
|
3606
|
+
throw new ProjectRoomClientError("Invalid Project Room response", 500, "INVALID_RESPONSE", false);
|
|
3607
|
+
return value;
|
|
3608
|
+
};
|
|
3609
|
+
var hydrateRoom = (value) => {
|
|
3610
|
+
const row = record(value);
|
|
3611
|
+
exactKeys(row, ["id", "tenantId", "workspaceId", "projectId", "type", "name", "createdAt", "updatedAt"]);
|
|
3612
|
+
return { id: requiredString(row, "id"), tenantId: requiredString(row, "tenantId"), workspaceId: requiredString(row, "workspaceId"), projectId: requiredString(row, "projectId"), type: oneOf(row.type, ["main"], "type"), name: requiredString(row, "name"), createdAt: date(row.createdAt, "createdAt"), updatedAt: date(row.updatedAt, "updatedAt") };
|
|
3613
|
+
};
|
|
3614
|
+
var hydrateMessage = (value) => {
|
|
3615
|
+
const row = record(value);
|
|
3616
|
+
exactKeys(row, ["id", "roomId", "author", "content", "mentions", "source", "createdAt"], ["replyToMessageId"]);
|
|
3617
|
+
const content = record(row.content);
|
|
3618
|
+
if (oneOf(content.type, ["text"], "content.type") !== "text")
|
|
3619
|
+
throw new ProjectRoomClientError("Invalid content", 500, "INVALID_RESPONSE", false);
|
|
3620
|
+
return { id: requiredString(row, "id"), roomId: requiredString(row, "roomId"), author: hydrateAuthor(row.author), content: { type: "text", text: requiredString(content, "text") }, mentions: hydrateMentions(row.mentions), ...row.replyToMessageId === void 0 ? {} : { replyToMessageId: requiredString(row, "replyToMessageId") }, source: oneOf(row.source, messageSources, "source"), createdAt: date(row.createdAt, "createdAt") };
|
|
3621
|
+
};
|
|
3622
|
+
var hydrateMember = (value) => {
|
|
3623
|
+
const row = record(value);
|
|
3624
|
+
exactKeys(row, ["id", "tenantId", "projectId", "userId", "role", "status", "joinedAt", "updatedAt"]);
|
|
3625
|
+
return { id: requiredString(row, "id"), tenantId: requiredString(row, "tenantId"), projectId: requiredString(row, "projectId"), userId: requiredString(row, "userId"), role: oneOf(row.role, humanRoles, "role"), status: oneOf(row.status, memberStatuses, "status"), joinedAt: date(row.joinedAt, "joinedAt"), updatedAt: date(row.updatedAt, "updatedAt") };
|
|
3626
|
+
};
|
|
3627
|
+
var hydrateBot = (value) => {
|
|
3628
|
+
const row = record(value);
|
|
3629
|
+
exactKeys(row, ["id", "tenantId", "workspaceId", "projectId", "roomId", "assistantId", "role", "title", "mentionName", "status", "roomThreadId", "joinedAt", "updatedAt"], ["responsibility"]);
|
|
3630
|
+
return { id: requiredString(row, "id"), tenantId: requiredString(row, "tenantId"), workspaceId: requiredString(row, "workspaceId"), projectId: requiredString(row, "projectId"), roomId: requiredString(row, "roomId"), assistantId: requiredString(row, "assistantId"), role: oneOf(row.role, botRoles, "role"), title: requiredString(row, "title"), ...row.responsibility === void 0 ? {} : { responsibility: requiredString(row, "responsibility") }, mentionName: requiredString(row, "mentionName"), status: oneOf(row.status, botStatuses, "status"), roomThreadId: requiredString(row, "roomThreadId"), joinedAt: date(row.joinedAt, "joinedAt"), updatedAt: date(row.updatedAt, "updatedAt") };
|
|
3631
|
+
};
|
|
3632
|
+
var iso = (value) => date(value, "date").toISOString();
|
|
3633
|
+
var hydrateDispatch = (value) => {
|
|
3634
|
+
const row = record(value);
|
|
3635
|
+
if (typeof row.success !== "boolean" || row.membershipId !== void 0 && typeof row.membershipId !== "string" || row.errorCode !== void 0 && typeof row.errorCode !== "string")
|
|
3636
|
+
throw new ProjectRoomClientError("Invalid dispatch response", 500, "INVALID_RESPONSE", false);
|
|
3637
|
+
return { success: row.success, ...row.membershipId === void 0 ? {} : { membershipId: row.membershipId }, ...row.errorCode === void 0 ? {} : { errorCode: row.errorCode } };
|
|
3638
|
+
};
|
|
3639
|
+
var encode = encodeURIComponent;
|
|
3640
|
+
var ProjectRoomsClient = class {
|
|
3641
|
+
constructor(baseURL, getHeaders) {
|
|
3642
|
+
this.baseURL = baseURL;
|
|
3643
|
+
this.getHeaders = getHeaders;
|
|
3644
|
+
this.messages = {
|
|
3645
|
+
list: async (projectId, options = {}) => {
|
|
3646
|
+
const query = new URLSearchParams();
|
|
3647
|
+
if (options.cursor) {
|
|
3648
|
+
query.set("beforeCreatedAt", iso(options.cursor.createdAt));
|
|
3649
|
+
query.set("beforeId", options.cursor.id);
|
|
3650
|
+
}
|
|
3651
|
+
if (options.limit !== void 0)
|
|
3652
|
+
query.set("limit", String(options.limit));
|
|
3653
|
+
const result = await this.request(`/api/projects/${encode(projectId)}/room/messages${query.toString() ? `?${query}` : ""}`);
|
|
3654
|
+
if (!Array.isArray(result))
|
|
3655
|
+
throw new ProjectRoomClientError("Invalid messages response", 500, "INVALID_RESPONSE", false);
|
|
3656
|
+
return result.map(hydrateMessage);
|
|
3657
|
+
},
|
|
3658
|
+
send: async (projectId, input) => {
|
|
3659
|
+
const result = await this.request(`/api/projects/${encode(projectId)}/room/messages`, { method: "POST", body: { text: input.text, mentions: input.mentions, ...input.replyToMessageId === void 0 ? {} : { replyToMessageId: input.replyToMessageId } }, headers: { "Idempotency-Key": input.idempotencyKey.trim() } });
|
|
3660
|
+
if (!Array.isArray(result.dispatch) || typeof result.replayed !== "boolean")
|
|
3661
|
+
throw new ProjectRoomClientError("Invalid send response", 500, "INVALID_RESPONSE", false);
|
|
3662
|
+
return { message: hydrateMessage(result.message), dispatch: result.dispatch.map(hydrateDispatch), replayed: result.replayed };
|
|
3663
|
+
},
|
|
3664
|
+
retry: async (projectId, messageId) => {
|
|
3665
|
+
const result = await this.request(`/api/projects/${encode(projectId)}/room/messages/${encode(messageId)}/retry`, { method: "POST", body: {} });
|
|
3666
|
+
if (!Array.isArray(result))
|
|
3667
|
+
throw new ProjectRoomClientError("Invalid retry response", 500, "INVALID_RESPONSE", false);
|
|
3668
|
+
return result.map(hydrateDispatch);
|
|
3669
|
+
}
|
|
3670
|
+
};
|
|
3671
|
+
this.members = {
|
|
3672
|
+
list: async (projectId) => this.records(`/api/projects/${encode(projectId)}/members`, hydrateMember),
|
|
3673
|
+
add: async (projectId, input) => hydrateMember(await this.request(`/api/projects/${encode(projectId)}/members`, { method: "POST", body: input })),
|
|
3674
|
+
update: async (projectId, membershipId, input) => hydrateMember(await this.request(`/api/projects/${encode(projectId)}/members/${encode(membershipId)}`, { method: "PATCH", body: { role: input.role, expectedUpdatedAt: iso(input.expectedUpdatedAt) } })),
|
|
3675
|
+
remove: async (projectId, membershipId, input) => this.request(`/api/projects/${encode(projectId)}/members/${encode(membershipId)}?expectedUpdatedAt=${encode(iso(input.expectedUpdatedAt))}`, { method: "DELETE" })
|
|
3676
|
+
};
|
|
3677
|
+
this.bots = {
|
|
3678
|
+
list: async (projectId) => this.records(`/api/projects/${encode(projectId)}/bots`, hydrateBot),
|
|
3679
|
+
add: async (projectId, input) => hydrateBot(await this.request(`/api/projects/${encode(projectId)}/bots`, { method: "POST", body: input })),
|
|
3680
|
+
update: async (projectId, membershipId, input) => hydrateBot(await this.request(`/api/projects/${encode(projectId)}/bots/${encode(membershipId)}`, { method: "PATCH", body: { ...input, expectedUpdatedAt: iso(input.expectedUpdatedAt) } })),
|
|
3681
|
+
pause: async (projectId, membershipId, input) => hydrateBot(await this.request(`/api/projects/${encode(projectId)}/bots/${encode(membershipId)}/pause`, { method: "POST", body: { expectedUpdatedAt: iso(input.expectedUpdatedAt) } })),
|
|
3682
|
+
resume: async (projectId, membershipId, input) => hydrateBot(await this.request(`/api/projects/${encode(projectId)}/bots/${encode(membershipId)}/resume`, { method: "POST", body: { expectedUpdatedAt: iso(input.expectedUpdatedAt) } })),
|
|
3683
|
+
remove: async (projectId, membershipId, input) => this.request(`/api/projects/${encode(projectId)}/bots/${encode(membershipId)}?expectedUpdatedAt=${encode(iso(input.expectedUpdatedAt))}`, { method: "DELETE" })
|
|
3684
|
+
};
|
|
3685
|
+
this.events = { connect: (projectId, options) => this.connect(projectId, options) };
|
|
3686
|
+
}
|
|
3687
|
+
async getRoom(projectId) {
|
|
3688
|
+
return hydrateRoom(await this.request(`/api/projects/${encode(projectId)}/room`));
|
|
3689
|
+
}
|
|
3690
|
+
async getRealtimeMode(projectId) {
|
|
3691
|
+
return this.request(`/api/projects/${encode(projectId)}/room/realtime-mode`);
|
|
3692
|
+
}
|
|
3693
|
+
async records(path, hydrate) {
|
|
3694
|
+
const result = await this.request(path);
|
|
3695
|
+
if (!Array.isArray(result))
|
|
3696
|
+
throw new ProjectRoomClientError("Invalid records response", 500, "INVALID_RESPONSE", false);
|
|
3697
|
+
return result.map(hydrate);
|
|
3698
|
+
}
|
|
3699
|
+
async request(path, options = {}) {
|
|
3700
|
+
const headers = { ...this.getHeaders(), ...options.headers ?? {} };
|
|
3701
|
+
const body = options.body === void 0 ? void 0 : JSON.parse(JSON.stringify(options.body));
|
|
3702
|
+
const response = await fetch(`${this.baseURL}${path}`, { method: options.method ?? "GET", headers, ...body === void 0 ? {} : { body: JSON.stringify(body) } });
|
|
3703
|
+
const payload = await response.json().catch(() => void 0);
|
|
3704
|
+
if (!response.ok || !payload?.success) {
|
|
3705
|
+
const error = payload?.error;
|
|
3706
|
+
throw new ProjectRoomClientError(typeof error?.message === "string" ? error.message : "Project Room request failed", response.status, typeof error?.code === "string" ? error.code : response.ok ? "INVALID_RESPONSE" : "HTTP_ERROR", error?.retryable === true, error?.details);
|
|
3707
|
+
}
|
|
3708
|
+
if (!Object.prototype.hasOwnProperty.call(payload, "data"))
|
|
3709
|
+
throw new ProjectRoomClientError("Missing response data", 500, "INVALID_RESPONSE", false);
|
|
3710
|
+
return payload.data;
|
|
3711
|
+
}
|
|
3712
|
+
connect(projectId, options) {
|
|
3713
|
+
const headers = { ...this.getHeaders(), Accept: "text/event-stream", ...options.lastEventId ? { "Last-Event-ID": options.lastEventId } : {} };
|
|
3714
|
+
let resolveOpen;
|
|
3715
|
+
let rejectOpen;
|
|
3716
|
+
const opened = new Promise((resolve, reject) => {
|
|
3717
|
+
resolveOpen = resolve;
|
|
3718
|
+
rejectOpen = reject;
|
|
3719
|
+
});
|
|
3720
|
+
const responsePromise = fetch(`${this.baseURL}/api/projects/${encode(projectId)}/room/events`, { headers, signal: options.signal }).then(async (response) => {
|
|
3721
|
+
if (!response.ok) {
|
|
3722
|
+
const payload = await response.json().catch(() => void 0);
|
|
3723
|
+
const error = payload?.error;
|
|
3724
|
+
throw new ProjectRoomClientError(typeof error?.message === "string" ? error.message : "Project Room stream failed", response.status, typeof error?.code === "string" ? error.code : "HTTP_ERROR", error?.retryable === true, error?.details);
|
|
3725
|
+
}
|
|
3726
|
+
if (response.headers?.get && response.headers.get("content-type")?.split(";")[0].trim() !== "text/event-stream")
|
|
3727
|
+
throw new ProjectRoomClientError("Project Room stream has invalid content type", response.status, "INVALID_RESPONSE", false);
|
|
3728
|
+
if (!response.body)
|
|
3729
|
+
throw new ProjectRoomClientError("Project Room stream has no body", 500, "INVALID_RESPONSE", false);
|
|
3730
|
+
resolveOpen({ status: 200, ...options.lastEventId === void 0 ? {} : { lastEventId: options.lastEventId } });
|
|
3731
|
+
return response;
|
|
3732
|
+
}).catch((error) => {
|
|
3733
|
+
rejectOpen(error);
|
|
3734
|
+
throw error;
|
|
3735
|
+
});
|
|
3736
|
+
const stream = async function* () {
|
|
3737
|
+
const response = await responsePromise;
|
|
3738
|
+
const body = response.body;
|
|
3739
|
+
if (!body)
|
|
3740
|
+
throw new ProjectRoomClientError("Project Room stream has no body", 500, "INVALID_RESPONSE", false);
|
|
3741
|
+
for await (const raw of parseSseBody(body, { strictJsonObject: true })) {
|
|
3742
|
+
try {
|
|
3743
|
+
const event = raw.event;
|
|
3744
|
+
const payload = isRecord(raw.data) ? raw.data : void 0;
|
|
3745
|
+
let value = payload;
|
|
3746
|
+
if (event === "ready" || event === "resync" || event === "access.revoked") {
|
|
3747
|
+
if (!payload || Object.prototype.hasOwnProperty.call(payload, "type"))
|
|
3748
|
+
throw new ProjectRoomClientError("Invalid SSE control", 200, "INVALID_EVENT", false);
|
|
3749
|
+
value = { type: event, data: payload };
|
|
3750
|
+
} else {
|
|
3751
|
+
if (typeof payload?.type !== "string" || payload.type !== event || typeof payload.id !== "string" || raw.id !== payload.id)
|
|
3752
|
+
throw new ProjectRoomClientError("Invalid SSE business event", 200, "INVALID_EVENT", false);
|
|
3753
|
+
value = payload;
|
|
3754
|
+
}
|
|
3755
|
+
yield { event: hydrateEvent(value), ...raw.id === void 0 ? {} : { id: raw.id }, ...raw.retry === void 0 ? {} : { retry: raw.retry } };
|
|
3756
|
+
} catch (error) {
|
|
3757
|
+
if (error instanceof ProjectRoomClientError && error.code === "INVALID_EVENT")
|
|
3758
|
+
throw new ProjectRoomClientError(error.message, 400, "INVALID_EVENT", false, error.details);
|
|
3759
|
+
if (error instanceof SseParseError)
|
|
3760
|
+
throw new ProjectRoomClientError(error.message, 400, "INVALID_EVENT", false);
|
|
3761
|
+
if (error instanceof ProjectRoomClientError)
|
|
3762
|
+
throw new ProjectRoomClientError(error.message, 400, "INVALID_EVENT", false, error.details);
|
|
3763
|
+
throw new ProjectRoomClientError("Invalid SSE event", 400, "INVALID_EVENT", false);
|
|
3764
|
+
}
|
|
3765
|
+
}
|
|
3766
|
+
}();
|
|
3767
|
+
return { opened, [Symbol.asyncIterator]: () => stream };
|
|
3768
|
+
}
|
|
3769
|
+
};
|
|
3770
|
+
function hydrateEvent(value) {
|
|
3771
|
+
const row = record(value);
|
|
3772
|
+
if (row.type === "ready") {
|
|
3773
|
+
exactKeys(row, ["type", "data"]);
|
|
3774
|
+
const data2 = record(row.data);
|
|
3775
|
+
exactKeys(data2, ["epoch", "headEventId"]);
|
|
3776
|
+
if (typeof data2.epoch !== "string" || data2.headEventId !== null && typeof data2.headEventId !== "string")
|
|
3777
|
+
throw new ProjectRoomClientError("Invalid SSE control", 200, "INVALID_EVENT", false);
|
|
3778
|
+
return { type: "ready", data: { epoch: data2.epoch, headEventId: data2.headEventId } };
|
|
3779
|
+
}
|
|
3780
|
+
if (row.type === "resync" || row.type === "access.revoked") {
|
|
3781
|
+
exactKeys(row, ["type", "data"]);
|
|
3782
|
+
const data2 = record(row.data);
|
|
3783
|
+
exactKeys(data2, ["reason"]);
|
|
3784
|
+
const reasons = row.type === "resync" ? ["SERVER_RESTART", "CURSOR_EXPIRED", "SLOW_CONSUMER"] : ["PROJECT_ACCESS_REVOKED", "TOKEN_EXPIRED"];
|
|
3785
|
+
return { type: row.type, data: { reason: oneOf(data2.reason, reasons, "reason") } };
|
|
3786
|
+
}
|
|
3787
|
+
const data = record(row.data);
|
|
3788
|
+
if (row.type === "message.created")
|
|
3789
|
+
return { ...row, occurredAt: date(row.occurredAt, "occurredAt"), data: { message: hydrateMessage(data.message) } };
|
|
3790
|
+
if (row.type === "roster.changed")
|
|
3791
|
+
return { ...row, occurredAt: date(row.occurredAt, "occurredAt"), data: { change: data.change, membership: hydratePublicBot(data.membership) } };
|
|
3792
|
+
if (row.type === "membership.changed")
|
|
3793
|
+
return { ...row, occurredAt: date(row.occurredAt, "occurredAt"), data: { change: data.change, membership: hydratePublicMember(data.membership) } };
|
|
3794
|
+
if (row.type === "task.changed")
|
|
3795
|
+
return { ...row, occurredAt: date(row.occurredAt, "occurredAt"), data: { taskId: requiredString(data, "taskId"), status: data.status, ownerMembershipId: requiredString(data, "ownerMembershipId"), updatedAt: date(data.updatedAt, "updatedAt") } };
|
|
3796
|
+
throw new ProjectRoomClientError("Invalid SSE event", 200, "INVALID_EVENT", false);
|
|
3797
|
+
}
|
|
3798
|
+
function hydratePublicMember(value) {
|
|
3799
|
+
const row = record(value);
|
|
3800
|
+
return { id: requiredString(row, "id"), userId: requiredString(row, "userId"), role: row.role, status: row.status, joinedAt: date(row.joinedAt, "joinedAt"), updatedAt: date(row.updatedAt, "updatedAt") };
|
|
3801
|
+
}
|
|
3802
|
+
function hydratePublicBot(value) {
|
|
3803
|
+
const row = record(value);
|
|
3804
|
+
return { id: requiredString(row, "id"), role: row.role, title: requiredString(row, "title"), ...row.responsibility === void 0 ? {} : { responsibility: row.responsibility }, mentionName: requiredString(row, "mentionName"), status: row.status, joinedAt: date(row.joinedAt, "joinedAt"), updatedAt: date(row.updatedAt, "updatedAt") };
|
|
3805
|
+
}
|
|
3806
|
+
|
|
3432
3807
|
// src/client.ts
|
|
3433
3808
|
var _Client = class extends AbstractClient {
|
|
3434
3809
|
/**
|
|
@@ -3444,6 +3819,7 @@ var _Client = class extends AbstractClient {
|
|
|
3444
3819
|
};
|
|
3445
3820
|
this.resources = new ResourcesClient(this.config.baseURL, () => this.getAllHeaders());
|
|
3446
3821
|
this.exportImport = new ExportImportClient(this.config, () => this.getAllHeaders());
|
|
3822
|
+
this.projectRooms = new ProjectRoomsClient(this.config.baseURL, () => this.getAllHeaders());
|
|
3447
3823
|
}
|
|
3448
3824
|
/**
|
|
3449
3825
|
* Helper method to handle fetch responses and errors
|
|
@@ -3659,41 +4035,8 @@ var _Client = class extends AbstractClient {
|
|
|
3659
4035
|
if (!response.body) {
|
|
3660
4036
|
throw new Error("Response body is null");
|
|
3661
4037
|
}
|
|
3662
|
-
const
|
|
3663
|
-
|
|
3664
|
-
let buffer = "";
|
|
3665
|
-
while (true) {
|
|
3666
|
-
const { done, value } = await reader.read();
|
|
3667
|
-
if (done)
|
|
3668
|
-
break;
|
|
3669
|
-
const chunk = decoder.decode(value, { stream: true });
|
|
3670
|
-
buffer += chunk;
|
|
3671
|
-
const lines = buffer.split("\n");
|
|
3672
|
-
buffer = lines.pop() || "";
|
|
3673
|
-
for (const line of lines) {
|
|
3674
|
-
if (line.trim().startsWith("data: ")) {
|
|
3675
|
-
try {
|
|
3676
|
-
const eventData = JSON.parse(line.trim().slice(6));
|
|
3677
|
-
onEvent(eventData);
|
|
3678
|
-
} catch (error) {
|
|
3679
|
-
console.error("Error parsing SSE data:", line, error);
|
|
3680
|
-
if (onError) {
|
|
3681
|
-
onError(
|
|
3682
|
-
error instanceof Error ? error : new Error(String(error))
|
|
3683
|
-
);
|
|
3684
|
-
}
|
|
3685
|
-
}
|
|
3686
|
-
}
|
|
3687
|
-
}
|
|
3688
|
-
}
|
|
3689
|
-
if (buffer && buffer.trim().startsWith("data: ")) {
|
|
3690
|
-
try {
|
|
3691
|
-
const eventData = JSON.parse(buffer.trim().slice(6));
|
|
3692
|
-
onEvent(eventData);
|
|
3693
|
-
} catch (error) {
|
|
3694
|
-
console.error("Error parsing SSE data:", buffer, error);
|
|
3695
|
-
}
|
|
3696
|
-
}
|
|
4038
|
+
for await (const frame of parseSseBody(response.body, { strictJsonObject: true }))
|
|
4039
|
+
onEvent(frame.data);
|
|
3697
4040
|
if (onComplete) {
|
|
3698
4041
|
onComplete();
|
|
3699
4042
|
}
|
|
@@ -3736,62 +4079,18 @@ var _Client = class extends AbstractClient {
|
|
|
3736
4079
|
const res = await fetch(`${this.config.baseURL}${path}`, { headers, signal });
|
|
3737
4080
|
if (!res.ok || !res.body)
|
|
3738
4081
|
throw new Error("Stream connection failed");
|
|
3739
|
-
|
|
3740
|
-
|
|
3741
|
-
|
|
3742
|
-
let event = "";
|
|
3743
|
-
let dataLines = [];
|
|
3744
|
-
const processLine = (rawLine) => {
|
|
3745
|
-
const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine;
|
|
3746
|
-
if (line === "") {
|
|
3747
|
-
if (dataLines.length === 0) {
|
|
3748
|
-
event = "";
|
|
3749
|
-
return null;
|
|
3750
|
-
}
|
|
3751
|
-
const data = dataLines.join("\n");
|
|
3752
|
-
const eventName = event;
|
|
3753
|
-
event = "";
|
|
3754
|
-
dataLines = [];
|
|
3755
|
-
try {
|
|
3756
|
-
return { event: eventName, data: JSON.parse(data) };
|
|
3757
|
-
} catch {
|
|
3758
|
-
return null;
|
|
3759
|
-
}
|
|
3760
|
-
}
|
|
3761
|
-
if (line.startsWith(":"))
|
|
3762
|
-
return null;
|
|
3763
|
-
const separator = line.indexOf(":");
|
|
3764
|
-
const field = separator === -1 ? line : line.slice(0, separator);
|
|
3765
|
-
let value = separator === -1 ? "" : line.slice(separator + 1);
|
|
3766
|
-
if (value.startsWith(" "))
|
|
3767
|
-
value = value.slice(1);
|
|
3768
|
-
if (field === "event")
|
|
3769
|
-
event = value;
|
|
3770
|
-
else if (field === "data")
|
|
3771
|
-
dataLines.push(value);
|
|
3772
|
-
return null;
|
|
3773
|
-
};
|
|
3774
|
-
while (true) {
|
|
3775
|
-
const { done, value } = await reader.read();
|
|
3776
|
-
if (done) {
|
|
3777
|
-
buffer += decoder.decode();
|
|
3778
|
-
break;
|
|
4082
|
+
try {
|
|
4083
|
+
for await (const frame of parseSseBody(res.body, { strictJsonObject: true })) {
|
|
4084
|
+
yield { event: frame.event, data: frame.data };
|
|
3779
4085
|
}
|
|
3780
|
-
|
|
3781
|
-
|
|
3782
|
-
|
|
3783
|
-
|
|
3784
|
-
|
|
3785
|
-
if (parsed)
|
|
3786
|
-
yield parsed;
|
|
3787
|
-
newline = buffer.indexOf("\n");
|
|
4086
|
+
} catch (error) {
|
|
4087
|
+
if (error instanceof SseParseError) {
|
|
4088
|
+
const parseError = new Error(error.message);
|
|
4089
|
+
parseError.name = "StreamParseError";
|
|
4090
|
+
throw parseError;
|
|
3788
4091
|
}
|
|
4092
|
+
throw error;
|
|
3789
4093
|
}
|
|
3790
|
-
if (buffer.length > 0)
|
|
3791
|
-
processLine(buffer);
|
|
3792
|
-
const finalEvent = processLine("");
|
|
3793
|
-
if (finalEvent)
|
|
3794
|
-
yield finalEvent;
|
|
3795
4094
|
}
|
|
3796
4095
|
/**
|
|
3797
4096
|
* Get all headers including workspace context
|
|
@@ -3833,41 +4132,8 @@ var _Client = class extends AbstractClient {
|
|
|
3833
4132
|
if (!response.body) {
|
|
3834
4133
|
throw new Error("Response body is null");
|
|
3835
4134
|
}
|
|
3836
|
-
const
|
|
3837
|
-
|
|
3838
|
-
let buffer = "";
|
|
3839
|
-
while (true) {
|
|
3840
|
-
const { done, value } = await reader.read();
|
|
3841
|
-
if (done)
|
|
3842
|
-
break;
|
|
3843
|
-
const chunk = decoder.decode(value, { stream: true });
|
|
3844
|
-
buffer += chunk;
|
|
3845
|
-
const lines = buffer.split("\n");
|
|
3846
|
-
buffer = lines.pop() || "";
|
|
3847
|
-
for (const line of lines) {
|
|
3848
|
-
if (line.trim().startsWith("data: ")) {
|
|
3849
|
-
try {
|
|
3850
|
-
const eventData = JSON.parse(line.trim().slice(6));
|
|
3851
|
-
onEvent(eventData);
|
|
3852
|
-
} catch (error) {
|
|
3853
|
-
console.error("Error parsing SSE data:", line, error);
|
|
3854
|
-
if (onError) {
|
|
3855
|
-
onError(
|
|
3856
|
-
error instanceof Error ? error : new Error(String(error))
|
|
3857
|
-
);
|
|
3858
|
-
}
|
|
3859
|
-
}
|
|
3860
|
-
}
|
|
3861
|
-
}
|
|
3862
|
-
}
|
|
3863
|
-
if (buffer && buffer.trim().startsWith("data: ")) {
|
|
3864
|
-
try {
|
|
3865
|
-
const eventData = JSON.parse(buffer.trim().slice(6));
|
|
3866
|
-
onEvent(eventData);
|
|
3867
|
-
} catch (error) {
|
|
3868
|
-
console.error("Error parsing SSE data:", buffer, error);
|
|
3869
|
-
}
|
|
3870
|
-
}
|
|
4135
|
+
for await (const frame of parseSseBody(response.body, { strictJsonObject: true }))
|
|
4136
|
+
onEvent(frame.data);
|
|
3871
4137
|
if (onComplete) {
|
|
3872
4138
|
if (options.enableReturnStateWhenSteamCompleted) {
|
|
3873
4139
|
try {
|
|
@@ -4638,6 +4904,8 @@ export {
|
|
|
4638
4904
|
Client,
|
|
4639
4905
|
ExportImportClient,
|
|
4640
4906
|
NetworkError,
|
|
4907
|
+
ProjectRoomClientError,
|
|
4908
|
+
ProjectRoomsClient,
|
|
4641
4909
|
ResourcesClient,
|
|
4642
4910
|
ScheduleExecutionType,
|
|
4643
4911
|
ScheduledTaskStatus,
|