@arbidocs/sdk 0.5.54 → 0.5.56
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 +79 -0
- package/dist/{browser-CAjoEmx3.d.ts → browser-DEZ7kJKE.d.ts} +87 -12
- package/dist/{browser-CzRV0kfR.d.cts → browser-DvpIH-Qk.d.cts} +87 -12
- package/dist/browser.cjs +743 -164
- package/dist/browser.cjs.map +1 -1
- package/dist/browser.d.cts +1 -1
- package/dist/browser.d.ts +1 -1
- package/dist/browser.js +743 -165
- package/dist/browser.js.map +1 -1
- package/dist/index.cjs +751 -171
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +751 -172
- package/dist/index.js.map +1 -1
- package/dist/testing.cjs +1 -1
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.cts +1 -1
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +1 -1
- package/dist/testing.js.map +1 -1
- package/package.json +2 -2
package/dist/browser.cjs
CHANGED
|
@@ -3479,14 +3479,20 @@ var init_libsodium_wrappers = __esm({
|
|
|
3479
3479
|
|
|
3480
3480
|
// src/errors.ts
|
|
3481
3481
|
var ArbiError = class extends Error {
|
|
3482
|
-
|
|
3483
|
-
|
|
3482
|
+
code;
|
|
3483
|
+
requestId;
|
|
3484
|
+
responseId;
|
|
3485
|
+
constructor(message, options = {}) {
|
|
3486
|
+
super(message, options.cause === void 0 ? void 0 : { cause: options.cause });
|
|
3487
|
+
this.code = options.code;
|
|
3488
|
+
this.requestId = options.requestId;
|
|
3489
|
+
this.responseId = options.responseId;
|
|
3484
3490
|
this.name = "ArbiError";
|
|
3485
3491
|
}
|
|
3486
3492
|
};
|
|
3487
3493
|
var ArbiSessionExpiredError = class extends ArbiError {
|
|
3488
|
-
constructor(message = "Your session expired. Sign in again.") {
|
|
3489
|
-
super(message);
|
|
3494
|
+
constructor(message = "Your session expired. Sign in again.", options = {}) {
|
|
3495
|
+
super(message, { ...options, code: "SESSION_EXPIRED" });
|
|
3490
3496
|
this.name = "ArbiSessionExpiredError";
|
|
3491
3497
|
}
|
|
3492
3498
|
};
|
|
@@ -3515,7 +3521,7 @@ function detailText(detail) {
|
|
|
3515
3521
|
return messages.length ? messages.join("; ") : void 0;
|
|
3516
3522
|
}
|
|
3517
3523
|
if (!detail || typeof detail !== "object") return void 0;
|
|
3518
|
-
const message = typeof detail.message === "string" ? detail.message : void 0;
|
|
3524
|
+
const message = typeof detail.message === "string" ? detail.message : typeof detail.hint === "string" ? detail.hint : void 0;
|
|
3519
3525
|
const fields = detail.fields && typeof detail.fields === "object" && !Array.isArray(detail.fields) ? Object.entries(detail.fields).map(([name, problem]) => `${name}: ${String(problem)}`).join("; ") : "";
|
|
3520
3526
|
if (message) return fields ? `${message} (${fields})` : message;
|
|
3521
3527
|
return fields || void 0;
|
|
@@ -3524,21 +3530,80 @@ function apiErrorDetail(apiError) {
|
|
|
3524
3530
|
if (typeof apiError === "string") return apiError || void 0;
|
|
3525
3531
|
if (!apiError || typeof apiError !== "object") return void 0;
|
|
3526
3532
|
const body = apiError;
|
|
3527
|
-
return detailText(body.detail) || (typeof body.message === "string" ? body.message : void 0);
|
|
3533
|
+
return detailText(body.detail) || (typeof body.message === "string" ? body.message : void 0) || (typeof body.error === "string" ? body.error : detailText(errorRecord(body.error)));
|
|
3528
3534
|
}
|
|
3529
3535
|
var ArbiApiError = class extends ArbiError {
|
|
3530
3536
|
status;
|
|
3531
3537
|
apiError;
|
|
3532
3538
|
/** Why the server refused, in its words (e.g. "run_at must be in the future"), when it said. */
|
|
3533
3539
|
detail;
|
|
3534
|
-
constructor(message, apiError, status) {
|
|
3535
|
-
super(message
|
|
3540
|
+
constructor(message, apiError, status, options = {}) {
|
|
3541
|
+
super(message, {
|
|
3542
|
+
...apiErrorMetadata(apiError, status),
|
|
3543
|
+
...Object.fromEntries(Object.entries(options).filter(([, value]) => value !== void 0))
|
|
3544
|
+
});
|
|
3536
3545
|
this.name = "ArbiApiError";
|
|
3537
3546
|
this.apiError = apiError;
|
|
3538
3547
|
this.status = status;
|
|
3539
3548
|
this.detail = apiErrorDetail(apiError);
|
|
3540
3549
|
}
|
|
3541
3550
|
};
|
|
3551
|
+
function errorRecord(value) {
|
|
3552
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
3553
|
+
}
|
|
3554
|
+
function firstString(...values) {
|
|
3555
|
+
return values.find((value) => typeof value === "string" && value.length > 0);
|
|
3556
|
+
}
|
|
3557
|
+
function apiErrorMetadata(apiError, status) {
|
|
3558
|
+
const body = errorRecord(apiError);
|
|
3559
|
+
const response = errorRecord(body?.response);
|
|
3560
|
+
const detail = errorRecord(body?.detail);
|
|
3561
|
+
const error = errorRecord(response?.error) ?? errorRecord(body?.error);
|
|
3562
|
+
return {
|
|
3563
|
+
code: firstString(error?.code, detail?.code, body?.code) ?? (status !== void 0 ? `HTTP_${status}` : void 0),
|
|
3564
|
+
requestId: firstString(body?.request_id, detail?.request_id, error?.request_id),
|
|
3565
|
+
responseId: firstString(
|
|
3566
|
+
response?.id,
|
|
3567
|
+
body?.response_id,
|
|
3568
|
+
typeof body?.status === "string" ? body?.id : void 0,
|
|
3569
|
+
detail?.response_id,
|
|
3570
|
+
error?.response_id
|
|
3571
|
+
)
|
|
3572
|
+
};
|
|
3573
|
+
}
|
|
3574
|
+
async function apiErrorFromResponse(response, fallback, options = {}) {
|
|
3575
|
+
let body;
|
|
3576
|
+
try {
|
|
3577
|
+
const text2 = await response.text();
|
|
3578
|
+
if (text2) {
|
|
3579
|
+
try {
|
|
3580
|
+
body = JSON.parse(text2);
|
|
3581
|
+
} catch {
|
|
3582
|
+
body = text2.length < 200 ? text2 : void 0;
|
|
3583
|
+
}
|
|
3584
|
+
}
|
|
3585
|
+
} catch {
|
|
3586
|
+
}
|
|
3587
|
+
return new ArbiApiError(fallback, body, response.status, {
|
|
3588
|
+
requestId: response.headers.get("x-request-id") ?? response.headers.get("request-id") ?? void 0,
|
|
3589
|
+
...options
|
|
3590
|
+
});
|
|
3591
|
+
}
|
|
3592
|
+
var ArbiTransportError = class extends ArbiError {
|
|
3593
|
+
constructor(cause, responseId) {
|
|
3594
|
+
super(cause instanceof Error ? cause.message : String(cause), {
|
|
3595
|
+
code: "TRANSPORT_ERROR",
|
|
3596
|
+
cause,
|
|
3597
|
+
responseId
|
|
3598
|
+
});
|
|
3599
|
+
this.name = "ArbiTransportError";
|
|
3600
|
+
}
|
|
3601
|
+
};
|
|
3602
|
+
function transportError(error, responseId) {
|
|
3603
|
+
if (error instanceof ArbiError || error instanceof Error && error.name === "AbortError")
|
|
3604
|
+
return error;
|
|
3605
|
+
return new ArbiTransportError(error, responseId);
|
|
3606
|
+
}
|
|
3542
3607
|
var DuplicateContactError = class extends ArbiApiError {
|
|
3543
3608
|
contactId;
|
|
3544
3609
|
constructor(message, contactId, apiError) {
|
|
@@ -3553,13 +3618,17 @@ function refusalReason(err, fallback) {
|
|
|
3553
3618
|
}
|
|
3554
3619
|
function requireData(result, message) {
|
|
3555
3620
|
if (result.error || !result.data) {
|
|
3556
|
-
throw new ArbiApiError(message, result.error, result.response?.status
|
|
3621
|
+
throw new ArbiApiError(message, result.error, result.response?.status, {
|
|
3622
|
+
requestId: result.response?.headers?.get("x-request-id") ?? void 0
|
|
3623
|
+
});
|
|
3557
3624
|
}
|
|
3558
3625
|
return result.data;
|
|
3559
3626
|
}
|
|
3560
3627
|
function requireOk(result, message) {
|
|
3561
3628
|
if (result.error) {
|
|
3562
|
-
throw new ArbiApiError(message, result.error, result.response?.status
|
|
3629
|
+
throw new ArbiApiError(message, result.error, result.response?.status, {
|
|
3630
|
+
requestId: result.response?.headers?.get("x-request-id") ?? void 0
|
|
3631
|
+
});
|
|
3563
3632
|
}
|
|
3564
3633
|
}
|
|
3565
3634
|
function getErrorMessage(err) {
|
|
@@ -3590,39 +3659,30 @@ var STATUS_FALLBACKS = {
|
|
|
3590
3659
|
404: "Resource not found.",
|
|
3591
3660
|
503: "The selected LLM is not responding. Please retry or select another model."
|
|
3592
3661
|
};
|
|
3593
|
-
async function extractErrorMessage(res) {
|
|
3594
|
-
let bodyDetail;
|
|
3595
|
-
try {
|
|
3596
|
-
const text = await res.text();
|
|
3597
|
-
if (text) {
|
|
3598
|
-
try {
|
|
3599
|
-
const json2 = JSON.parse(text);
|
|
3600
|
-
bodyDetail = detailText(json2.detail) || json2.message || json2.error || void 0;
|
|
3601
|
-
} catch {
|
|
3602
|
-
if (text.length < 200) bodyDetail = text;
|
|
3603
|
-
}
|
|
3604
|
-
}
|
|
3605
|
-
} catch {
|
|
3606
|
-
}
|
|
3607
|
-
if (bodyDetail) return `Request failed (${res.status}): ${bodyDetail}`;
|
|
3608
|
-
const fallback = STATUS_FALLBACKS[res.status];
|
|
3609
|
-
if (fallback) return fallback;
|
|
3610
|
-
return `Request failed: ${res.status} ${res.statusText}`;
|
|
3611
|
-
}
|
|
3612
3662
|
async function authenticatedFetch(options) {
|
|
3613
3663
|
const { baseUrl, accessToken, path: path2, method, body, headers, signal } = options;
|
|
3614
|
-
|
|
3615
|
-
|
|
3616
|
-
|
|
3617
|
-
|
|
3618
|
-
|
|
3619
|
-
|
|
3620
|
-
|
|
3621
|
-
|
|
3622
|
-
|
|
3664
|
+
let res;
|
|
3665
|
+
try {
|
|
3666
|
+
res = await fetch(`${baseUrl}${path2}`, {
|
|
3667
|
+
method: method ?? (body ? "POST" : "GET"),
|
|
3668
|
+
headers: {
|
|
3669
|
+
Authorization: `Bearer ${accessToken}`,
|
|
3670
|
+
...headers
|
|
3671
|
+
},
|
|
3672
|
+
body,
|
|
3673
|
+
signal
|
|
3674
|
+
});
|
|
3675
|
+
} catch (error) {
|
|
3676
|
+
throw transportError(error);
|
|
3677
|
+
}
|
|
3623
3678
|
if (!res.ok) {
|
|
3624
|
-
const
|
|
3625
|
-
|
|
3679
|
+
const error = await apiErrorFromResponse(
|
|
3680
|
+
res,
|
|
3681
|
+
STATUS_FALLBACKS[res.status] ?? `Request failed: ${res.status} ${res.statusText}`
|
|
3682
|
+
);
|
|
3683
|
+
const detail = error.detail || (typeof error.apiError === "string" ? error.apiError : void 0);
|
|
3684
|
+
if (detail) error.message = `Request failed (${res.status}): ${detail}`;
|
|
3685
|
+
throw error;
|
|
3626
3686
|
}
|
|
3627
3687
|
return res;
|
|
3628
3688
|
}
|
|
@@ -3935,9 +3995,9 @@ function humanizeStep(identifier) {
|
|
|
3935
3995
|
const spaced = identifier.replace(/[_-]+/g, " ").trim();
|
|
3936
3996
|
return spaced.charAt(0).toUpperCase() + spaced.slice(1);
|
|
3937
3997
|
}
|
|
3938
|
-
function resolveDocRefs(
|
|
3939
|
-
if (!docNames) return
|
|
3940
|
-
return
|
|
3998
|
+
function resolveDocRefs(text2, docNames) {
|
|
3999
|
+
if (!docNames) return text2;
|
|
4000
|
+
return text2.replace(/\{(doc-[^}]+)\}/g, (_match, id) => docNames[id] ?? id);
|
|
3941
4001
|
}
|
|
3942
4002
|
function resolveStepDocName(step, docNames) {
|
|
3943
4003
|
if (!docNames) return void 0;
|
|
@@ -4016,13 +4076,16 @@ function parseSSEEvents(chunk, buffer) {
|
|
|
4016
4076
|
return { events, remaining };
|
|
4017
4077
|
}
|
|
4018
4078
|
async function streamSSE(response, callbacks = {}) {
|
|
4079
|
+
if (!response.ok) throw await apiErrorFromResponse(response, `HTTP ${response.status}`);
|
|
4019
4080
|
if (!response.body) {
|
|
4020
|
-
throw new
|
|
4081
|
+
throw new ArbiApiError("No response body", void 0, response.status, {
|
|
4082
|
+
code: "MISSING_RESPONSE_BODY"
|
|
4083
|
+
});
|
|
4021
4084
|
}
|
|
4022
4085
|
const reader = response.body.getReader();
|
|
4023
4086
|
const decoder = new TextDecoder("utf-8");
|
|
4024
4087
|
let sseBuffer = "";
|
|
4025
|
-
let
|
|
4088
|
+
let text2 = "";
|
|
4026
4089
|
let assistantMessageExtId = null;
|
|
4027
4090
|
const agentSteps = [];
|
|
4028
4091
|
let toolCallCount = 0;
|
|
@@ -4033,6 +4096,35 @@ async function streamSSE(response, callbacks = {}) {
|
|
|
4033
4096
|
let usage = null;
|
|
4034
4097
|
let context = null;
|
|
4035
4098
|
let completed = false;
|
|
4099
|
+
let terminal = false;
|
|
4100
|
+
let outcome;
|
|
4101
|
+
let acknowledged = false;
|
|
4102
|
+
const errorDetails = [];
|
|
4103
|
+
const reportError = (error) => {
|
|
4104
|
+
errorDetails.push(error);
|
|
4105
|
+
errors.push(error.message);
|
|
4106
|
+
callbacks.onErrorDetails?.(error);
|
|
4107
|
+
callbacks.onError?.(error.message);
|
|
4108
|
+
};
|
|
4109
|
+
const settle = (payload, fallback) => {
|
|
4110
|
+
const status = payload?.status;
|
|
4111
|
+
const resolved = status === "completed" || status === "failed" || status === "cancelled" || status === "incomplete" ? status : fallback;
|
|
4112
|
+
const id = typeof payload?.id === "string" && payload.id ? payload.id : assistantMessageExtId;
|
|
4113
|
+
assistantMessageExtId = id;
|
|
4114
|
+
const rawError = payload?.error;
|
|
4115
|
+
let error;
|
|
4116
|
+
if (resolved === "failed" || rawError) {
|
|
4117
|
+
const body = { response: payload };
|
|
4118
|
+
const message = rawError && typeof rawError === "object" && "message" in rawError && typeof rawError.message === "string" ? rawError.message : "Unknown streaming error";
|
|
4119
|
+
error = new ArbiApiError(message, body, void 0, {
|
|
4120
|
+
code: apiErrorMetadata(body).code ?? "RESPONSE_FAILED",
|
|
4121
|
+
responseId: id ?? void 0
|
|
4122
|
+
});
|
|
4123
|
+
reportError(error);
|
|
4124
|
+
}
|
|
4125
|
+
outcome = { status: resolved, responseId: id, response: payload, ...error ? { error } : {} };
|
|
4126
|
+
terminal = true;
|
|
4127
|
+
};
|
|
4036
4128
|
const eventHandlers = {
|
|
4037
4129
|
// OpenAI Responses API events (dot-separated names from server)
|
|
4038
4130
|
"response.created": (raw) => {
|
|
@@ -4053,7 +4145,7 @@ async function streamSSE(response, callbacks = {}) {
|
|
|
4053
4145
|
"response.output_text.delta": (raw) => {
|
|
4054
4146
|
const data = JSON.parse(raw);
|
|
4055
4147
|
if (data.delta) {
|
|
4056
|
-
|
|
4148
|
+
text2 += data.delta;
|
|
4057
4149
|
if (callbacks.onOutputTextDelta) {
|
|
4058
4150
|
callbacks.onOutputTextDelta({ item_id: data.item_id ?? "", delta: data.delta });
|
|
4059
4151
|
} else {
|
|
@@ -4063,7 +4155,7 @@ async function streamSSE(response, callbacks = {}) {
|
|
|
4063
4155
|
},
|
|
4064
4156
|
"response.output_text.done": (raw) => {
|
|
4065
4157
|
const data = JSON.parse(raw);
|
|
4066
|
-
|
|
4158
|
+
text2 = data.text;
|
|
4067
4159
|
callbacks.onTextDone?.(data.text);
|
|
4068
4160
|
},
|
|
4069
4161
|
"response.function_call_arguments.delta": (raw) => {
|
|
@@ -4084,6 +4176,7 @@ async function streamSSE(response, callbacks = {}) {
|
|
|
4084
4176
|
"response.completed": (raw) => {
|
|
4085
4177
|
const data = JSON.parse(raw);
|
|
4086
4178
|
completed = true;
|
|
4179
|
+
settle(data.response, "completed");
|
|
4087
4180
|
if (data.response?.usage) {
|
|
4088
4181
|
usage = data.response.usage;
|
|
4089
4182
|
callbacks.onUsage?.(data.response.usage);
|
|
@@ -4095,7 +4188,7 @@ async function streamSSE(response, callbacks = {}) {
|
|
|
4095
4188
|
}
|
|
4096
4189
|
const finalText = data.response?.metadata?.content;
|
|
4097
4190
|
if (finalText) {
|
|
4098
|
-
|
|
4191
|
+
text2 = finalText;
|
|
4099
4192
|
callbacks.onTextDone?.(finalText);
|
|
4100
4193
|
}
|
|
4101
4194
|
if (data.context) {
|
|
@@ -4107,12 +4200,17 @@ async function streamSSE(response, callbacks = {}) {
|
|
|
4107
4200
|
},
|
|
4108
4201
|
"response.failed": (raw) => {
|
|
4109
4202
|
const data = JSON.parse(raw);
|
|
4110
|
-
|
|
4111
|
-
|
|
4112
|
-
|
|
4203
|
+
settle(data.response, "failed");
|
|
4204
|
+
},
|
|
4205
|
+
"response.cancelled": (raw) => {
|
|
4206
|
+
settle(JSON.parse(raw).response, "cancelled");
|
|
4207
|
+
},
|
|
4208
|
+
"response.incomplete": (raw) => {
|
|
4209
|
+
settle(JSON.parse(raw).response, "incomplete");
|
|
4113
4210
|
},
|
|
4114
4211
|
// ARBI-specific events (dot-prefixed from server)
|
|
4115
4212
|
"arbi.message_queued": (raw) => {
|
|
4213
|
+
acknowledged = true;
|
|
4116
4214
|
const data = JSON.parse(raw);
|
|
4117
4215
|
if (data.user_message) {
|
|
4118
4216
|
userMessage = data.user_message;
|
|
@@ -4135,6 +4233,7 @@ async function streamSSE(response, callbacks = {}) {
|
|
|
4135
4233
|
callbacks.onUserInputRequest?.(data);
|
|
4136
4234
|
},
|
|
4137
4235
|
"arbi.probe_answer": (raw) => {
|
|
4236
|
+
acknowledged = true;
|
|
4138
4237
|
const data = JSON.parse(raw);
|
|
4139
4238
|
callbacks.onProbeAnswer?.(data);
|
|
4140
4239
|
},
|
|
@@ -4143,6 +4242,7 @@ async function streamSSE(response, callbacks = {}) {
|
|
|
4143
4242
|
callbacks.onDeferredInterjection?.(data);
|
|
4144
4243
|
},
|
|
4145
4244
|
"arbi.agent_control_state": (raw) => {
|
|
4245
|
+
acknowledged = true;
|
|
4146
4246
|
const data = JSON.parse(raw);
|
|
4147
4247
|
callbacks.onAgentControlState?.(data);
|
|
4148
4248
|
},
|
|
@@ -4158,47 +4258,81 @@ async function streamSSE(response, callbacks = {}) {
|
|
|
4158
4258
|
"arbi.error": (raw) => {
|
|
4159
4259
|
const data = JSON.parse(raw);
|
|
4160
4260
|
const message = typeof data?.message === "string" && data.message.length > 0 ? data.message : data.code;
|
|
4261
|
+
const error = new ArbiApiError(message, data, void 0, {
|
|
4262
|
+
responseId: assistantMessageExtId ?? void 0
|
|
4263
|
+
});
|
|
4264
|
+
errorDetails.push(error);
|
|
4161
4265
|
errors.push(message);
|
|
4266
|
+
callbacks.onErrorDetails?.(error);
|
|
4267
|
+
outcome = { status: "failed", responseId: assistantMessageExtId, error };
|
|
4162
4268
|
callbacks.onArbiError?.(data);
|
|
4163
4269
|
callbacks.onError?.(message);
|
|
4164
4270
|
}
|
|
4165
4271
|
};
|
|
4166
4272
|
const processEvents = (events) => {
|
|
4167
4273
|
for (const { event, data } of events) {
|
|
4274
|
+
if (terminal) break;
|
|
4275
|
+
if (event === "reasoning") {
|
|
4276
|
+
callbacks.onReasoningDelta?.(data);
|
|
4277
|
+
continue;
|
|
4278
|
+
}
|
|
4279
|
+
const handler = eventHandlers[event];
|
|
4280
|
+
if (!handler) {
|
|
4281
|
+
console.warn(`[arbi-sse] unhandled SSE event: ${event}`);
|
|
4282
|
+
continue;
|
|
4283
|
+
}
|
|
4168
4284
|
try {
|
|
4169
|
-
|
|
4170
|
-
callbacks.onReasoningDelta?.(data);
|
|
4171
|
-
continue;
|
|
4172
|
-
}
|
|
4173
|
-
const handler = eventHandlers[event];
|
|
4174
|
-
if (handler) {
|
|
4175
|
-
handler(data);
|
|
4176
|
-
} else {
|
|
4177
|
-
console.warn(`[arbi-sse] unhandled SSE event: ${event}`);
|
|
4178
|
-
}
|
|
4285
|
+
JSON.parse(data);
|
|
4179
4286
|
} catch {
|
|
4287
|
+
continue;
|
|
4180
4288
|
}
|
|
4289
|
+
handler(data);
|
|
4181
4290
|
}
|
|
4182
4291
|
};
|
|
4183
|
-
|
|
4184
|
-
|
|
4185
|
-
|
|
4186
|
-
|
|
4187
|
-
|
|
4188
|
-
|
|
4189
|
-
|
|
4190
|
-
|
|
4191
|
-
|
|
4192
|
-
|
|
4193
|
-
|
|
4292
|
+
try {
|
|
4293
|
+
while (true) {
|
|
4294
|
+
let chunk;
|
|
4295
|
+
try {
|
|
4296
|
+
chunk = await reader.read();
|
|
4297
|
+
} catch (cause) {
|
|
4298
|
+
const error = transportError(cause, assistantMessageExtId ?? void 0);
|
|
4299
|
+
const aborted = error.name === "AbortError";
|
|
4300
|
+
const disconnected = {
|
|
4301
|
+
status: "disconnected",
|
|
4302
|
+
responseId: assistantMessageExtId,
|
|
4303
|
+
reason: aborted ? "aborted" : "connection_error",
|
|
4304
|
+
...error instanceof ArbiError ? { error } : {}
|
|
4305
|
+
};
|
|
4306
|
+
if (error instanceof ArbiError) callbacks.onErrorDetails?.(error);
|
|
4307
|
+
callbacks.onOutcome?.(disconnected);
|
|
4308
|
+
throw error;
|
|
4309
|
+
}
|
|
4310
|
+
const { done, value } = chunk;
|
|
4311
|
+
if (done) break;
|
|
4312
|
+
const { events, remaining } = parseSSEEvents(
|
|
4313
|
+
decoder.decode(value, { stream: true }),
|
|
4314
|
+
sseBuffer
|
|
4315
|
+
);
|
|
4316
|
+
sseBuffer = remaining;
|
|
4317
|
+
processEvents(events);
|
|
4318
|
+
if (terminal) {
|
|
4319
|
+
sseBuffer = "";
|
|
4320
|
+
void reader.cancel().catch(() => {
|
|
4321
|
+
});
|
|
4322
|
+
break;
|
|
4323
|
+
}
|
|
4194
4324
|
}
|
|
4325
|
+
if (sseBuffer.trim()) {
|
|
4326
|
+
const { events } = parseSSEEvents(sseBuffer + "\n\n", "");
|
|
4327
|
+
processEvents(events);
|
|
4328
|
+
}
|
|
4329
|
+
} finally {
|
|
4330
|
+
reader.releaseLock();
|
|
4195
4331
|
}
|
|
4196
|
-
|
|
4197
|
-
|
|
4198
|
-
processEvents(events);
|
|
4199
|
-
}
|
|
4332
|
+
outcome ??= acknowledged && !assistantMessageExtId ? { status: "acknowledged", responseId: null } : { status: "disconnected", responseId: assistantMessageExtId, reason: "connection_closed" };
|
|
4333
|
+
callbacks.onOutcome?.(outcome);
|
|
4200
4334
|
return {
|
|
4201
|
-
text,
|
|
4335
|
+
text: text2,
|
|
4202
4336
|
assistantMessageExtId,
|
|
4203
4337
|
agentSteps,
|
|
4204
4338
|
toolCallCount,
|
|
@@ -4208,7 +4342,9 @@ async function streamSSE(response, callbacks = {}) {
|
|
|
4208
4342
|
artifacts,
|
|
4209
4343
|
usage,
|
|
4210
4344
|
context,
|
|
4211
|
-
completed
|
|
4345
|
+
completed,
|
|
4346
|
+
outcome,
|
|
4347
|
+
errorDetails
|
|
4212
4348
|
};
|
|
4213
4349
|
}
|
|
4214
4350
|
var AUTH_TIMEOUT_MS = 1e4;
|
|
@@ -4404,8 +4540,8 @@ function countCitations(metadata) {
|
|
|
4404
4540
|
const responses = modelCitations?.tool_responses;
|
|
4405
4541
|
return responses ? Object.keys(responses).length : 0;
|
|
4406
4542
|
}
|
|
4407
|
-
function stripCitationMarkdown(
|
|
4408
|
-
return
|
|
4543
|
+
function stripCitationMarkdown(text2) {
|
|
4544
|
+
return text2.replace(/\[([^\]]+)\]\(#cite-(\d+)\)/g, "$1[$2]");
|
|
4409
4545
|
}
|
|
4410
4546
|
function buildCitationChunkData(message) {
|
|
4411
4547
|
const merged = {};
|
|
@@ -4444,6 +4580,403 @@ function buildChunkLookup(tools) {
|
|
|
4444
4580
|
return lookup;
|
|
4445
4581
|
}
|
|
4446
4582
|
|
|
4583
|
+
// src/response-handle.ts
|
|
4584
|
+
var object = (value) => value !== null && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
4585
|
+
var text = (value) => typeof value === "string" ? value : void 0;
|
|
4586
|
+
var messageId = (value) => typeof value === "string" && /^msg-[\w-]+$/.test(value);
|
|
4587
|
+
function immutable(value) {
|
|
4588
|
+
if (value && typeof value === "object" && !(value instanceof Error) && !Object.isFrozen(value)) {
|
|
4589
|
+
Object.freeze(value);
|
|
4590
|
+
for (const child of Object.values(value)) immutable(child);
|
|
4591
|
+
}
|
|
4592
|
+
return value;
|
|
4593
|
+
}
|
|
4594
|
+
function reportObserverError(error) {
|
|
4595
|
+
queueMicrotask(() => {
|
|
4596
|
+
throw error;
|
|
4597
|
+
});
|
|
4598
|
+
}
|
|
4599
|
+
var ResponseEventDecoder = class {
|
|
4600
|
+
buffer = "";
|
|
4601
|
+
event = "";
|
|
4602
|
+
data = [];
|
|
4603
|
+
id;
|
|
4604
|
+
push(chunk, final = false) {
|
|
4605
|
+
this.buffer += chunk;
|
|
4606
|
+
const frames = [];
|
|
4607
|
+
const dispatch = () => {
|
|
4608
|
+
if (this.data.length)
|
|
4609
|
+
frames.push({
|
|
4610
|
+
event: this.event || "message",
|
|
4611
|
+
data: this.data.join("\n"),
|
|
4612
|
+
...this.id !== void 0 ? { id: this.id } : {}
|
|
4613
|
+
});
|
|
4614
|
+
this.event = "";
|
|
4615
|
+
this.data = [];
|
|
4616
|
+
this.id = void 0;
|
|
4617
|
+
};
|
|
4618
|
+
const line = (value) => {
|
|
4619
|
+
if (!value) {
|
|
4620
|
+
dispatch();
|
|
4621
|
+
return;
|
|
4622
|
+
}
|
|
4623
|
+
if (value.startsWith(":")) return;
|
|
4624
|
+
const colon = value.indexOf(":");
|
|
4625
|
+
const field = colon < 0 ? value : value.slice(0, colon);
|
|
4626
|
+
let content = colon < 0 ? "" : value.slice(colon + 1);
|
|
4627
|
+
if (content.startsWith(" ")) content = content.slice(1);
|
|
4628
|
+
if (field === "event") this.event = content;
|
|
4629
|
+
if (field === "data") this.data.push(content);
|
|
4630
|
+
if (field === "id" && !content.includes("\0")) this.id = content;
|
|
4631
|
+
};
|
|
4632
|
+
while (true) {
|
|
4633
|
+
const index = this.buffer.search(/[\r\n]/);
|
|
4634
|
+
if (index < 0 || !final && index === this.buffer.length - 1 && this.buffer[index] === "\r")
|
|
4635
|
+
break;
|
|
4636
|
+
const width = this.buffer[index] === "\r" && this.buffer[index + 1] === "\n" ? 2 : 1;
|
|
4637
|
+
line(this.buffer.slice(0, index));
|
|
4638
|
+
this.buffer = this.buffer.slice(index + width);
|
|
4639
|
+
}
|
|
4640
|
+
if (final) {
|
|
4641
|
+
if (this.buffer) line(this.buffer);
|
|
4642
|
+
this.buffer = "";
|
|
4643
|
+
dispatch();
|
|
4644
|
+
}
|
|
4645
|
+
return frames;
|
|
4646
|
+
}
|
|
4647
|
+
};
|
|
4648
|
+
function consumeResponse(response, controller, cancel, options = {}) {
|
|
4649
|
+
if (!response.body)
|
|
4650
|
+
return Promise.reject(
|
|
4651
|
+
new ArbiApiError("Response has no stream body.", void 0, response.status, {
|
|
4652
|
+
code: "MISSING_RESPONSE_BODY"
|
|
4653
|
+
})
|
|
4654
|
+
);
|
|
4655
|
+
const reader = response.body.getReader();
|
|
4656
|
+
let resolveStart;
|
|
4657
|
+
let rejectStart;
|
|
4658
|
+
const start = new Promise((resolve, reject) => {
|
|
4659
|
+
resolveStart = resolve;
|
|
4660
|
+
rejectStart = reject;
|
|
4661
|
+
});
|
|
4662
|
+
let resolveOutcome;
|
|
4663
|
+
const outcome = new Promise((resolve) => {
|
|
4664
|
+
resolveOutcome = resolve;
|
|
4665
|
+
});
|
|
4666
|
+
let state = immutable({
|
|
4667
|
+
id: options.id ?? "",
|
|
4668
|
+
conversationId: null,
|
|
4669
|
+
connection: "connecting",
|
|
4670
|
+
responseStatus: null,
|
|
4671
|
+
output: [],
|
|
4672
|
+
displayText: ""
|
|
4673
|
+
});
|
|
4674
|
+
let bound = false;
|
|
4675
|
+
let finished = false;
|
|
4676
|
+
let acknowledgement;
|
|
4677
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
4678
|
+
const queue = [];
|
|
4679
|
+
let claimed = false;
|
|
4680
|
+
let iteratorClosed = false;
|
|
4681
|
+
let iteratorError;
|
|
4682
|
+
let pending;
|
|
4683
|
+
const emit = (event) => {
|
|
4684
|
+
if (!claimed || iteratorClosed) return;
|
|
4685
|
+
if (pending) {
|
|
4686
|
+
pending.resolve({ value: immutable(event), done: false });
|
|
4687
|
+
pending = void 0;
|
|
4688
|
+
} else if (queue.length < 256) queue.push(immutable(event));
|
|
4689
|
+
else {
|
|
4690
|
+
iteratorError = new ArbiError("Raw response event consumer fell behind.", {
|
|
4691
|
+
code: "RESPONSE_EVENT_OVERFLOW"
|
|
4692
|
+
});
|
|
4693
|
+
queue.length = 0;
|
|
4694
|
+
iteratorClosed = true;
|
|
4695
|
+
}
|
|
4696
|
+
};
|
|
4697
|
+
const notify = () => {
|
|
4698
|
+
for (const listener of [...listeners]) {
|
|
4699
|
+
try {
|
|
4700
|
+
listener(state);
|
|
4701
|
+
} catch (error) {
|
|
4702
|
+
reportObserverError(error);
|
|
4703
|
+
}
|
|
4704
|
+
}
|
|
4705
|
+
};
|
|
4706
|
+
const update = (patch) => {
|
|
4707
|
+
state = immutable({ ...state, ...patch });
|
|
4708
|
+
notify();
|
|
4709
|
+
};
|
|
4710
|
+
const finish = (value, detached = false) => {
|
|
4711
|
+
if (finished) return;
|
|
4712
|
+
finished = true;
|
|
4713
|
+
controller.signal.removeEventListener("abort", abort);
|
|
4714
|
+
options.cleanup?.();
|
|
4715
|
+
update({ connection: detached ? "detached" : "closed", outcome: immutable(value) });
|
|
4716
|
+
listeners.clear();
|
|
4717
|
+
resolveOutcome(state.outcome);
|
|
4718
|
+
if (pending) {
|
|
4719
|
+
pending.resolve({ value: void 0, done: true });
|
|
4720
|
+
pending = void 0;
|
|
4721
|
+
}
|
|
4722
|
+
void reader.cancel().catch(() => {
|
|
4723
|
+
});
|
|
4724
|
+
};
|
|
4725
|
+
const abort = () => {
|
|
4726
|
+
if (!bound) rejectStart(new DOMException("The operation was aborted.", "AbortError"));
|
|
4727
|
+
finish({ status: "disconnected", responseId: bound ? state.id : null, reason: "aborted" }, true);
|
|
4728
|
+
queue.length = 0;
|
|
4729
|
+
};
|
|
4730
|
+
const handle = {
|
|
4731
|
+
get id() {
|
|
4732
|
+
return state.id;
|
|
4733
|
+
},
|
|
4734
|
+
snapshot: () => state,
|
|
4735
|
+
subscribe(listener) {
|
|
4736
|
+
if (!finished) listeners.add(listener);
|
|
4737
|
+
try {
|
|
4738
|
+
listener(state);
|
|
4739
|
+
} catch (error) {
|
|
4740
|
+
reportObserverError(error);
|
|
4741
|
+
}
|
|
4742
|
+
return () => {
|
|
4743
|
+
listeners.delete(listener);
|
|
4744
|
+
};
|
|
4745
|
+
},
|
|
4746
|
+
outcome,
|
|
4747
|
+
detach: () => {
|
|
4748
|
+
if (!finished) controller.abort();
|
|
4749
|
+
},
|
|
4750
|
+
cancel: () => cancel(state.id),
|
|
4751
|
+
events() {
|
|
4752
|
+
if (claimed)
|
|
4753
|
+
throw new ArbiError("A response handle supports one raw event iterator.", {
|
|
4754
|
+
code: "RESPONSE_EVENTS_ALREADY_CLAIMED"
|
|
4755
|
+
});
|
|
4756
|
+
claimed = true;
|
|
4757
|
+
const close = () => {
|
|
4758
|
+
iteratorClosed = true;
|
|
4759
|
+
queue.length = 0;
|
|
4760
|
+
if (pending) {
|
|
4761
|
+
pending.resolve({ value: void 0, done: true });
|
|
4762
|
+
pending = void 0;
|
|
4763
|
+
}
|
|
4764
|
+
};
|
|
4765
|
+
return {
|
|
4766
|
+
[Symbol.asyncIterator]() {
|
|
4767
|
+
return this;
|
|
4768
|
+
},
|
|
4769
|
+
next() {
|
|
4770
|
+
if (iteratorError) return Promise.reject(iteratorError);
|
|
4771
|
+
if (queue.length) return Promise.resolve({ value: queue.shift(), done: false });
|
|
4772
|
+
if (finished || iteratorClosed)
|
|
4773
|
+
return Promise.resolve({ value: void 0, done: true });
|
|
4774
|
+
if (pending)
|
|
4775
|
+
return Promise.reject(
|
|
4776
|
+
new ArbiError("Concurrent iterator reads are unsupported.", {
|
|
4777
|
+
code: "RESPONSE_EVENTS_CONCURRENT_READ"
|
|
4778
|
+
})
|
|
4779
|
+
);
|
|
4780
|
+
return new Promise((resolve, reject) => {
|
|
4781
|
+
pending = { resolve, reject };
|
|
4782
|
+
});
|
|
4783
|
+
},
|
|
4784
|
+
async return() {
|
|
4785
|
+
close();
|
|
4786
|
+
return { value: void 0, done: true };
|
|
4787
|
+
},
|
|
4788
|
+
async throw(error) {
|
|
4789
|
+
close();
|
|
4790
|
+
throw error;
|
|
4791
|
+
}
|
|
4792
|
+
};
|
|
4793
|
+
}
|
|
4794
|
+
};
|
|
4795
|
+
const bind = (id) => {
|
|
4796
|
+
if (bound) return;
|
|
4797
|
+
bound = true;
|
|
4798
|
+
state = immutable({ ...state, id, connection: "streaming" });
|
|
4799
|
+
resolveStart({ kind: "response", handle });
|
|
4800
|
+
};
|
|
4801
|
+
const diagnostic = (code, frame) => emit({ event: "arbi.sdk.protocol_error", data: { code, frame } });
|
|
4802
|
+
const process = (frame) => {
|
|
4803
|
+
if (finished) return;
|
|
4804
|
+
let data;
|
|
4805
|
+
if (frame.event === "reasoning") {
|
|
4806
|
+
emit({
|
|
4807
|
+
event: frame.event,
|
|
4808
|
+
data: frame.data,
|
|
4809
|
+
...frame.id !== void 0 ? { id: frame.id } : {}
|
|
4810
|
+
});
|
|
4811
|
+
return;
|
|
4812
|
+
}
|
|
4813
|
+
try {
|
|
4814
|
+
data = JSON.parse(frame.data);
|
|
4815
|
+
} catch {
|
|
4816
|
+
diagnostic("MALFORMED_JSON", frame);
|
|
4817
|
+
return;
|
|
4818
|
+
}
|
|
4819
|
+
const payload = object(data);
|
|
4820
|
+
const raw = object(payload?.response);
|
|
4821
|
+
const id = raw?.id;
|
|
4822
|
+
if (bound && typeof id === "string" && id !== state.id) {
|
|
4823
|
+
diagnostic("CONFLICTING_RESPONSE_ID", frame);
|
|
4824
|
+
return;
|
|
4825
|
+
}
|
|
4826
|
+
if (messageId(id)) bind(id);
|
|
4827
|
+
emit({ event: frame.event, data, ...frame.id !== void 0 ? { id: frame.id } : {} });
|
|
4828
|
+
if (frame.event === "arbi.message_queued") acknowledgement = payload;
|
|
4829
|
+
const patch = {};
|
|
4830
|
+
if (frame.event === "arbi.user_message" && !state.conversationId && typeof payload?.conversation_ext_id === "string")
|
|
4831
|
+
patch.conversationId = payload.conversation_ext_id;
|
|
4832
|
+
if (raw) {
|
|
4833
|
+
patch.rawResponse = raw;
|
|
4834
|
+
if (typeof raw.status === "string") patch.responseStatus = raw.status;
|
|
4835
|
+
if (Array.isArray(raw.output))
|
|
4836
|
+
patch.output = raw.output.filter((item2) => !!object(item2));
|
|
4837
|
+
const metadata = object(raw.metadata);
|
|
4838
|
+
const conversation = text(metadata?.conversation_ext_id) ?? text(metadata?.conversation_id) ?? text(raw.conversation_id);
|
|
4839
|
+
if (conversation && (!state.conversationId || state.conversationId === conversation))
|
|
4840
|
+
patch.conversationId = conversation;
|
|
4841
|
+
if (typeof metadata?.content === "string") patch.displayText = metadata.content;
|
|
4842
|
+
}
|
|
4843
|
+
const item = object(payload?.item);
|
|
4844
|
+
if ((frame.event === "response.output_item.added" || frame.event === "response.output_item.done") && item) {
|
|
4845
|
+
const output = [...state.output];
|
|
4846
|
+
const index = output.findIndex((existing) => existing.id === item.id);
|
|
4847
|
+
if (index >= 0 && item.id) output[index] = item;
|
|
4848
|
+
else
|
|
4849
|
+
output.splice(
|
|
4850
|
+
typeof payload?.output_index === "number" ? payload.output_index : output.length,
|
|
4851
|
+
0,
|
|
4852
|
+
item
|
|
4853
|
+
);
|
|
4854
|
+
patch.output = output;
|
|
4855
|
+
}
|
|
4856
|
+
const itemId = text(payload?.item_id);
|
|
4857
|
+
const delta = text(payload?.delta);
|
|
4858
|
+
if (itemId && [
|
|
4859
|
+
"response.content_part.added",
|
|
4860
|
+
"response.output_text.delta",
|
|
4861
|
+
"response.output_text.done",
|
|
4862
|
+
"response.function_call_arguments.delta",
|
|
4863
|
+
"response.reasoning_summary_text.delta"
|
|
4864
|
+
].includes(frame.event)) {
|
|
4865
|
+
const output = [...patch.output ?? state.output];
|
|
4866
|
+
let index = output.findIndex((existing) => existing.id === itemId);
|
|
4867
|
+
if (index < 0) {
|
|
4868
|
+
index = output.length;
|
|
4869
|
+
output.push({
|
|
4870
|
+
id: itemId,
|
|
4871
|
+
type: frame.event.includes("function_call") ? "function_call" : frame.event.includes("reasoning") ? "reasoning" : "message"
|
|
4872
|
+
});
|
|
4873
|
+
}
|
|
4874
|
+
const current = { ...output[index] };
|
|
4875
|
+
if (frame.event === "response.function_call_arguments.delta")
|
|
4876
|
+
current.arguments = (text(current.arguments) ?? "") + (delta ?? "");
|
|
4877
|
+
else {
|
|
4878
|
+
const key = frame.event.includes("reasoning") ? "summary" : "content";
|
|
4879
|
+
const content = Array.isArray(current[key]) ? [...current[key]] : [];
|
|
4880
|
+
const partIndex = typeof payload?.content_index === "number" ? payload.content_index : typeof payload?.summary_index === "number" ? payload.summary_index : 0;
|
|
4881
|
+
const part = {
|
|
4882
|
+
...object(content[partIndex]) ?? {
|
|
4883
|
+
type: key === "summary" ? "summary_text" : "output_text",
|
|
4884
|
+
text: ""
|
|
4885
|
+
}
|
|
4886
|
+
};
|
|
4887
|
+
if (frame.event === "response.content_part.added")
|
|
4888
|
+
content[partIndex] = object(payload?.part) ?? part;
|
|
4889
|
+
else {
|
|
4890
|
+
part.text = frame.event === "response.output_text.done" ? text(payload?.text) ?? "" : (text(part.text) ?? "") + (delta ?? "");
|
|
4891
|
+
content[partIndex] = part;
|
|
4892
|
+
}
|
|
4893
|
+
current[key] = content;
|
|
4894
|
+
}
|
|
4895
|
+
output[index] = current;
|
|
4896
|
+
patch.output = output;
|
|
4897
|
+
}
|
|
4898
|
+
if (frame.event === "response.output_text.delta" && delta)
|
|
4899
|
+
patch.displayText = state.displayText + delta;
|
|
4900
|
+
if (frame.event === "response.output_text.done" && typeof payload?.text === "string")
|
|
4901
|
+
patch.displayText = payload.text;
|
|
4902
|
+
update(patch);
|
|
4903
|
+
const terminal = /^response\.(completed|failed|cancelled|incomplete)$/.exec(
|
|
4904
|
+
frame.event
|
|
4905
|
+
)?.[1];
|
|
4906
|
+
if (terminal && bound) {
|
|
4907
|
+
const status = raw?.status;
|
|
4908
|
+
const resolved = status === "completed" || status === "failed" || status === "cancelled" || status === "incomplete" ? status : terminal;
|
|
4909
|
+
const error = resolved === "failed" || raw?.error ? new ArbiApiError(
|
|
4910
|
+
text(object(raw?.error)?.message) ?? "Response failed.",
|
|
4911
|
+
{ response: raw },
|
|
4912
|
+
void 0,
|
|
4913
|
+
{ ...apiErrorMetadata({ response: raw }), responseId: state.id }
|
|
4914
|
+
) : void 0;
|
|
4915
|
+
finish({
|
|
4916
|
+
status: resolved,
|
|
4917
|
+
responseId: state.id,
|
|
4918
|
+
...raw ? { response: raw } : {},
|
|
4919
|
+
...error ? { error } : {}
|
|
4920
|
+
});
|
|
4921
|
+
}
|
|
4922
|
+
};
|
|
4923
|
+
controller.signal.addEventListener("abort", abort, { once: true });
|
|
4924
|
+
if (controller.signal.aborted) abort();
|
|
4925
|
+
else if (options.id) bind(options.id);
|
|
4926
|
+
void (async () => {
|
|
4927
|
+
const decoder = new TextDecoder();
|
|
4928
|
+
const frames = new ResponseEventDecoder();
|
|
4929
|
+
try {
|
|
4930
|
+
while (!finished) {
|
|
4931
|
+
const { value, done } = await reader.read();
|
|
4932
|
+
for (const frame of frames.push(decoder.decode(value, { stream: !done }), done))
|
|
4933
|
+
process(frame);
|
|
4934
|
+
if (done) break;
|
|
4935
|
+
}
|
|
4936
|
+
if (!finished) {
|
|
4937
|
+
if (!bound && acknowledgement) {
|
|
4938
|
+
const value = { status: "acknowledged", responseId: null };
|
|
4939
|
+
resolveStart({
|
|
4940
|
+
kind: "acknowledged",
|
|
4941
|
+
acknowledgement: immutable(acknowledgement),
|
|
4942
|
+
outcome: immutable(value)
|
|
4943
|
+
});
|
|
4944
|
+
finish(value);
|
|
4945
|
+
} else {
|
|
4946
|
+
if (!bound)
|
|
4947
|
+
rejectStart(
|
|
4948
|
+
new ArbiApiError(
|
|
4949
|
+
"Stream ended before response identity was established.",
|
|
4950
|
+
void 0,
|
|
4951
|
+
void 0,
|
|
4952
|
+
{ code: "MISSING_RESPONSE_ID" }
|
|
4953
|
+
)
|
|
4954
|
+
);
|
|
4955
|
+
finish({
|
|
4956
|
+
status: "disconnected",
|
|
4957
|
+
responseId: bound ? state.id : null,
|
|
4958
|
+
reason: "connection_closed"
|
|
4959
|
+
});
|
|
4960
|
+
}
|
|
4961
|
+
}
|
|
4962
|
+
} catch (cause) {
|
|
4963
|
+
if (!finished) {
|
|
4964
|
+
const error = transportError(cause, bound ? state.id : void 0);
|
|
4965
|
+
if (!bound) rejectStart(error);
|
|
4966
|
+
finish({
|
|
4967
|
+
status: "disconnected",
|
|
4968
|
+
responseId: bound ? state.id : null,
|
|
4969
|
+
reason: error.name === "AbortError" ? "aborted" : "connection_error",
|
|
4970
|
+
...error instanceof ArbiError ? { error } : {}
|
|
4971
|
+
});
|
|
4972
|
+
}
|
|
4973
|
+
} finally {
|
|
4974
|
+
reader.releaseLock();
|
|
4975
|
+
}
|
|
4976
|
+
})();
|
|
4977
|
+
return start;
|
|
4978
|
+
}
|
|
4979
|
+
|
|
4447
4980
|
// src/operations/documents.ts
|
|
4448
4981
|
var documents_exports = {};
|
|
4449
4982
|
__export(documents_exports, {
|
|
@@ -5384,18 +5917,18 @@ async function searchConversations(arbi, body) {
|
|
|
5384
5917
|
"Failed to search conversations"
|
|
5385
5918
|
);
|
|
5386
5919
|
}
|
|
5387
|
-
async function getMessage(arbi,
|
|
5920
|
+
async function getMessage(arbi, messageId2) {
|
|
5388
5921
|
return requireData(
|
|
5389
5922
|
await arbi.fetch.GET("/v1/conversation/message/{message_ext_id}", {
|
|
5390
|
-
params: { path: { message_ext_id:
|
|
5923
|
+
params: { path: { message_ext_id: messageId2 } }
|
|
5391
5924
|
}),
|
|
5392
5925
|
"Failed to fetch message"
|
|
5393
5926
|
);
|
|
5394
5927
|
}
|
|
5395
|
-
async function deleteMessage(arbi,
|
|
5928
|
+
async function deleteMessage(arbi, messageId2) {
|
|
5396
5929
|
return requireData(
|
|
5397
5930
|
await arbi.fetch.DELETE("/v1/conversation/message/{message_ext_id}", {
|
|
5398
|
-
params: { path: { message_ext_id:
|
|
5931
|
+
params: { path: { message_ext_id: messageId2 } }
|
|
5399
5932
|
}),
|
|
5400
5933
|
"Failed to delete message"
|
|
5401
5934
|
);
|
|
@@ -5563,7 +6096,7 @@ async function retrieve(options) {
|
|
|
5563
6096
|
tools,
|
|
5564
6097
|
...model ? { model } : {}
|
|
5565
6098
|
};
|
|
5566
|
-
const { data, error } = await arbi.fetch.POST("/v1/assistant/retrieve", { body });
|
|
6099
|
+
const { data, error, response } = await arbi.fetch.POST("/v1/assistant/retrieve", { body });
|
|
5567
6100
|
if (error) {
|
|
5568
6101
|
const detail = error?.detail;
|
|
5569
6102
|
let message;
|
|
@@ -5575,7 +6108,9 @@ async function retrieve(options) {
|
|
|
5575
6108
|
} else {
|
|
5576
6109
|
message = JSON.stringify(error);
|
|
5577
6110
|
}
|
|
5578
|
-
throw new
|
|
6111
|
+
throw new ArbiApiError(`Retrieve failed: ${message}`, error, response.status, {
|
|
6112
|
+
requestId: response.headers.get("x-request-id") ?? void 0
|
|
6113
|
+
});
|
|
5579
6114
|
}
|
|
5580
6115
|
return data;
|
|
5581
6116
|
}
|
|
@@ -5612,7 +6147,7 @@ async function queryAssistant(options) {
|
|
|
5612
6147
|
return authenticatedFetch({
|
|
5613
6148
|
baseUrl,
|
|
5614
6149
|
accessToken,
|
|
5615
|
-
path: "/v1/
|
|
6150
|
+
path: "/v1/responses",
|
|
5616
6151
|
method: "POST",
|
|
5617
6152
|
body: JSON.stringify(buildTurnRequest(request)),
|
|
5618
6153
|
headers: { "Content-Type": "application/json" },
|
|
@@ -5622,8 +6157,8 @@ async function queryAssistant(options) {
|
|
|
5622
6157
|
var AssistantTurnRefused = class extends ArbiApiError {
|
|
5623
6158
|
/** Set when the refusal is the project's used-up AI usage. */
|
|
5624
6159
|
quota;
|
|
5625
|
-
constructor(message, status, quota = null) {
|
|
5626
|
-
super(message,
|
|
6160
|
+
constructor(message, status, quota = null, apiError, options = {}) {
|
|
6161
|
+
super(message, apiError, status, options);
|
|
5627
6162
|
this.name = "AssistantTurnRefused";
|
|
5628
6163
|
this.quota = quota;
|
|
5629
6164
|
}
|
|
@@ -5651,54 +6186,36 @@ function quotaFromStreamError(error) {
|
|
|
5651
6186
|
if (!Number.isFinite(spend) || !Number.isFinite(budget)) return null;
|
|
5652
6187
|
return { spend, budget, projectExtId: match.groups.project ?? null };
|
|
5653
6188
|
}
|
|
5654
|
-
async function readDetail(response) {
|
|
5655
|
-
try {
|
|
5656
|
-
return (await response.json())?.detail;
|
|
5657
|
-
} catch {
|
|
5658
|
-
return void 0;
|
|
5659
|
-
}
|
|
5660
|
-
}
|
|
5661
6189
|
async function refusal(response) {
|
|
5662
6190
|
const { status } = response;
|
|
5663
|
-
|
|
5664
|
-
|
|
5665
|
-
|
|
5666
|
-
|
|
5667
|
-
status
|
|
5668
|
-
);
|
|
5669
|
-
}
|
|
5670
|
-
if (status === 503) {
|
|
5671
|
-
return new AssistantTurnRefused(
|
|
5672
|
-
"The selected LLM is not responding. Please retry again in a minute or select another model.",
|
|
5673
|
-
status
|
|
5674
|
-
);
|
|
5675
|
-
}
|
|
5676
|
-
if (status === 402) {
|
|
5677
|
-
const detail2 = await readDetail(response);
|
|
5678
|
-
const quota = quotaExceededFrom(detail2);
|
|
5679
|
-
const message = quota?.message ?? (typeof detail2 === "string" ? detail2 : void 0) ?? detail2?.message ?? "This project's AI usage for this month is used up.";
|
|
5680
|
-
return new AssistantTurnRefused(
|
|
5681
|
-
message || "This project's AI usage for this month is used up.",
|
|
5682
|
-
status,
|
|
5683
|
-
quota
|
|
5684
|
-
);
|
|
5685
|
-
}
|
|
5686
|
-
const detail = await readDetail(response);
|
|
6191
|
+
const parsed = await apiErrorFromResponse(response, `HTTP error! status: ${status}`);
|
|
6192
|
+
const detail = parsed.apiError?.detail;
|
|
6193
|
+
const quota = status === 402 ? quotaExceededFrom(detail) : null;
|
|
6194
|
+
const fallback = status === 409 ? "This message has failed. Please retry from the previous message." : status === 503 ? "The selected LLM is not responding. Please retry again in a minute or select another model." : status === 402 ? "This project's AI usage for this month is used up." : parsed.message;
|
|
5687
6195
|
return new AssistantTurnRefused(
|
|
5688
|
-
|
|
5689
|
-
status
|
|
6196
|
+
quota?.message || parsed.detail || fallback,
|
|
6197
|
+
status,
|
|
6198
|
+
quota,
|
|
6199
|
+
parsed.apiError,
|
|
6200
|
+
{ code: parsed.code, requestId: parsed.requestId, responseId: parsed.responseId }
|
|
5690
6201
|
);
|
|
5691
6202
|
}
|
|
5692
6203
|
async function openTurnStream(auth, body, signal) {
|
|
5693
|
-
const response = await fetch(`${auth.baseUrl}/v1/
|
|
6204
|
+
const response = await fetch(`${auth.baseUrl}/v1/responses`, {
|
|
5694
6205
|
method: "POST",
|
|
5695
6206
|
headers: { "Content-Type": "application/json", Authorization: `Bearer ${auth.accessToken}` },
|
|
5696
6207
|
body: JSON.stringify(body),
|
|
5697
6208
|
signal,
|
|
5698
6209
|
credentials: "include"
|
|
6210
|
+
}).catch((error) => {
|
|
6211
|
+
throw transportError(error);
|
|
5699
6212
|
});
|
|
5700
|
-
if (response.status === 401) throw
|
|
5701
|
-
if (!response.ok
|
|
6213
|
+
if (response.status === 401) throw await apiErrorFromResponse(response, SESSION_EXPIRED_MESSAGE);
|
|
6214
|
+
if (!response.ok) throw await refusal(response);
|
|
6215
|
+
if (!response.body)
|
|
6216
|
+
throw new ArbiApiError("Response has no stream body.", void 0, response.status, {
|
|
6217
|
+
code: "MISSING_RESPONSE_BODY"
|
|
6218
|
+
});
|
|
5702
6219
|
return response;
|
|
5703
6220
|
}
|
|
5704
6221
|
async function openRunStream(auth, responseId, signal) {
|
|
@@ -5710,15 +6227,23 @@ async function openRunStream(auth, responseId, signal) {
|
|
|
5710
6227
|
signal,
|
|
5711
6228
|
credentials: "include"
|
|
5712
6229
|
}
|
|
5713
|
-
)
|
|
5714
|
-
|
|
5715
|
-
|
|
5716
|
-
|
|
6230
|
+
).catch((error) => {
|
|
6231
|
+
throw transportError(error, responseId);
|
|
6232
|
+
});
|
|
6233
|
+
if (response.status === 401)
|
|
6234
|
+
throw await apiErrorFromResponse(response, SESSION_EXPIRED_MESSAGE, { responseId });
|
|
6235
|
+
if (!response.ok) {
|
|
6236
|
+
throw await apiErrorFromResponse(
|
|
6237
|
+
response,
|
|
5717
6238
|
response.status === 404 ? "This run is no longer active." : `HTTP ${response.status}`,
|
|
5718
|
-
|
|
5719
|
-
response.status
|
|
6239
|
+
{ responseId }
|
|
5720
6240
|
);
|
|
5721
6241
|
}
|
|
6242
|
+
if (!response.body)
|
|
6243
|
+
throw new ArbiApiError("Response has no stream body.", void 0, response.status, {
|
|
6244
|
+
code: "MISSING_RESPONSE_BODY",
|
|
6245
|
+
responseId
|
|
6246
|
+
});
|
|
5722
6247
|
return response;
|
|
5723
6248
|
}
|
|
5724
6249
|
async function interject(arbi, assistantMessageExtId, answer) {
|
|
@@ -6740,7 +7265,7 @@ async function listChatMessages(arbi, chatExtId, options) {
|
|
|
6740
7265
|
"Failed to load messages"
|
|
6741
7266
|
);
|
|
6742
7267
|
}
|
|
6743
|
-
async function sendChatMessage(arbi, ring,
|
|
7268
|
+
async function sendChatMessage(arbi, ring, text2, options = {}) {
|
|
6744
7269
|
const attachments = options.attachments ?? [];
|
|
6745
7270
|
checkChatAttachmentLimits(attachments);
|
|
6746
7271
|
const key = keyFor(ring, ring.currentVersion);
|
|
@@ -6748,7 +7273,7 @@ async function sendChatMessage(arbi, ring, text, options = {}) {
|
|
|
6748
7273
|
await arbi.fetch.POST("/v1/chats/{chat_ext_id}/messages", {
|
|
6749
7274
|
params: { path: { chat_ext_id: ring.chatExtId } },
|
|
6750
7275
|
body: {
|
|
6751
|
-
content: await client.encryptWithChatKey(
|
|
7276
|
+
content: await client.encryptWithChatKey(text2, key),
|
|
6752
7277
|
key_version: ring.currentVersion,
|
|
6753
7278
|
kind: "text",
|
|
6754
7279
|
...attachments.length ? {
|
|
@@ -6763,7 +7288,7 @@ async function sendChatMessage(arbi, ring, text, options = {}) {
|
|
|
6763
7288
|
);
|
|
6764
7289
|
return {
|
|
6765
7290
|
...created,
|
|
6766
|
-
content:
|
|
7291
|
+
content: text2,
|
|
6767
7292
|
...attachments.length ? { attachmentFiles: attachments } : {}
|
|
6768
7293
|
};
|
|
6769
7294
|
}
|
|
@@ -6823,9 +7348,9 @@ async function resolveRecipient(arbi, ref) {
|
|
|
6823
7348
|
}
|
|
6824
7349
|
return null;
|
|
6825
7350
|
}
|
|
6826
|
-
async function sendDirectMessage(arbi, crypto2, peerExtId,
|
|
7351
|
+
async function sendDirectMessage(arbi, crypto2, peerExtId, text2, options) {
|
|
6827
7352
|
const chat = await openDirectChat(arbi, peerExtId);
|
|
6828
|
-
return sendChatMessage(arbi, await openChatKeyRing(chat, crypto2),
|
|
7353
|
+
return sendChatMessage(arbi, await openChatKeyRing(chat, crypto2), text2, options);
|
|
6829
7354
|
}
|
|
6830
7355
|
async function loadDecryptedChat(arbi, crypto2, chat, options) {
|
|
6831
7356
|
const ring = await openChatKeyRing(chat, crypto2);
|
|
@@ -7552,19 +8077,19 @@ function minorDigits(currency) {
|
|
|
7552
8077
|
throw new Error(`Unknown currency "${currency}": use an ISO 4217 code such as GBP or USD`);
|
|
7553
8078
|
}
|
|
7554
8079
|
}
|
|
7555
|
-
function parseBudget(
|
|
7556
|
-
const t2 =
|
|
8080
|
+
function parseBudget(text2) {
|
|
8081
|
+
const t2 = text2.trim();
|
|
7557
8082
|
const after = /^([0-9][0-9,_]*(?:\.[0-9]+)?)\s*([A-Za-z]{3})$/.exec(t2);
|
|
7558
8083
|
const before = /^([A-Za-z]{3})\s*([0-9][0-9,_]*(?:\.[0-9]+)?)$/.exec(t2);
|
|
7559
8084
|
const [amountText, code] = after ? [after[1], after[2]] : before ? [before[2], before[1]] : [];
|
|
7560
8085
|
if (!amountText || !code)
|
|
7561
|
-
throw new Error(`Invalid budget "${
|
|
8086
|
+
throw new Error(`Invalid budget "${text2}": use an amount and a currency, e.g. 6000GBP`);
|
|
7562
8087
|
const currency = code.toUpperCase();
|
|
7563
8088
|
const digits = minorDigits(currency);
|
|
7564
8089
|
const major = Number(amountText.replace(/[,_]/g, ""));
|
|
7565
8090
|
const amount = Math.round(major * 10 ** digits);
|
|
7566
8091
|
if (!Number.isFinite(major) || Math.abs(major * 10 ** digits - amount) > 1e-6)
|
|
7567
|
-
throw new Error(`Invalid budget "${
|
|
8092
|
+
throw new Error(`Invalid budget "${text2}": ${currency} has ${digits} decimal places`);
|
|
7568
8093
|
return { amount, currency };
|
|
7569
8094
|
}
|
|
7570
8095
|
function formatBudget(budget) {
|
|
@@ -8822,15 +9347,15 @@ function listMessages(mail, options) {
|
|
|
8822
9347
|
if (options.limit) params.set("limit", String(options.limit));
|
|
8823
9348
|
return json(mail(`${accountPath(options.accountId ?? NATIVE_ACCOUNT)}/messages?${params}`));
|
|
8824
9349
|
}
|
|
8825
|
-
function getMessage2(mail,
|
|
8826
|
-
return json(mail(`${accountPath(accountId)}/messages/${encodeURIComponent(
|
|
9350
|
+
function getMessage2(mail, messageId2, accountId = NATIVE_ACCOUNT) {
|
|
9351
|
+
return json(mail(`${accountPath(accountId)}/messages/${encodeURIComponent(messageId2)}`));
|
|
8827
9352
|
}
|
|
8828
|
-
async function downloadAttachment(mail,
|
|
8829
|
-
const path2 = `${accountPath(accountId)}/messages/${encodeURIComponent(
|
|
9353
|
+
async function downloadAttachment(mail, messageId2, attachmentId, accountId = NATIVE_ACCOUNT) {
|
|
9354
|
+
const path2 = `${accountPath(accountId)}/messages/${encodeURIComponent(messageId2)}/attachments/${encodeURIComponent(attachmentId)}`;
|
|
8830
9355
|
return new Uint8Array(await (await mail(path2)).arrayBuffer());
|
|
8831
9356
|
}
|
|
8832
|
-
async function updateMessage(mail,
|
|
8833
|
-
await mail(`${accountPath(accountId)}/messages/${encodeURIComponent(
|
|
9357
|
+
async function updateMessage(mail, messageId2, update, accountId = NATIVE_ACCOUNT) {
|
|
9358
|
+
await mail(`${accountPath(accountId)}/messages/${encodeURIComponent(messageId2)}`, {
|
|
8834
9359
|
method: "PATCH",
|
|
8835
9360
|
body: JSON.stringify(update)
|
|
8836
9361
|
});
|
|
@@ -9465,10 +9990,10 @@ var LANGUAGE_PATTERN = /^[a-z]{2}$/;
|
|
|
9465
9990
|
var PROMPT_MAX_CHARS = 500;
|
|
9466
9991
|
var PROMPT_CONTEXT_CHARS = 200;
|
|
9467
9992
|
function continuityPrompt(previous) {
|
|
9468
|
-
const
|
|
9469
|
-
if (!
|
|
9470
|
-
if (
|
|
9471
|
-
const tail =
|
|
9993
|
+
const text2 = previous?.replace(/\s+/g, " ").trim();
|
|
9994
|
+
if (!text2) return void 0;
|
|
9995
|
+
if (text2.length <= PROMPT_CONTEXT_CHARS) return text2;
|
|
9996
|
+
const tail = text2.slice(-PROMPT_CONTEXT_CHARS);
|
|
9472
9997
|
const space = tail.indexOf(" ");
|
|
9473
9998
|
const trimmed = space >= 0 && space < tail.length - 1 ? tail.slice(space + 1) : tail;
|
|
9474
9999
|
return trimmed || void 0;
|
|
@@ -11008,8 +11533,8 @@ var Arbi = class {
|
|
|
11008
11533
|
});
|
|
11009
11534
|
return runConversation(client, conversationId);
|
|
11010
11535
|
},
|
|
11011
|
-
getMessage: (
|
|
11012
|
-
deleteMessage: (
|
|
11536
|
+
getMessage: (messageId2) => getMessage(this.requireClient(), messageId2),
|
|
11537
|
+
deleteMessage: (messageId2) => deleteMessage(this.requireClient(), messageId2),
|
|
11013
11538
|
/**
|
|
11014
11539
|
* Post a human comment (no assistant run) — see `postConversationMessage`. A thread in another
|
|
11015
11540
|
* workspace than the current one works too: its key is handed to the session on demand.
|
|
@@ -11095,8 +11620,53 @@ var Arbi = class {
|
|
|
11095
11620
|
});
|
|
11096
11621
|
}
|
|
11097
11622
|
};
|
|
11623
|
+
async openResponseHandle(open, options, id) {
|
|
11624
|
+
const controller = new AbortController();
|
|
11625
|
+
const abort = () => controller.abort();
|
|
11626
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
11627
|
+
if (options.signal?.aborted) controller.abort();
|
|
11628
|
+
const cleanup = () => options.signal?.removeEventListener("abort", abort);
|
|
11629
|
+
try {
|
|
11630
|
+
const response = await this.recovering(() => open(controller.signal));
|
|
11631
|
+
return await consumeResponse(
|
|
11632
|
+
response,
|
|
11633
|
+
controller,
|
|
11634
|
+
(responseId) => this.assistant.cancel(responseId),
|
|
11635
|
+
{ id, cleanup }
|
|
11636
|
+
);
|
|
11637
|
+
} catch (error) {
|
|
11638
|
+
cleanup();
|
|
11639
|
+
controller.abort();
|
|
11640
|
+
throw error;
|
|
11641
|
+
}
|
|
11642
|
+
}
|
|
11098
11643
|
// ── Assistant ─────────────────────────────────────────────────────────
|
|
11099
11644
|
assistant = {
|
|
11645
|
+
/** Start one response with a composable observer; existing ask callbacks remain unchanged. */
|
|
11646
|
+
startResponse: async (request, options = {}) => {
|
|
11647
|
+
const body = buildTurnRequest({
|
|
11648
|
+
...request,
|
|
11649
|
+
workspaceId: request.workspaceId ?? this.requireWorkspace()
|
|
11650
|
+
});
|
|
11651
|
+
return this.openResponseHandle(
|
|
11652
|
+
(signal) => openTurnStream(this.getAuthHeaders(), body, signal),
|
|
11653
|
+
options
|
|
11654
|
+
);
|
|
11655
|
+
},
|
|
11656
|
+
/** Attach to retained replay/live events for an existing msg; never resubmits a turn. */
|
|
11657
|
+
attachResponse: async (id, options = {}) => {
|
|
11658
|
+
if (!/^msg-[\w-]+$/.test(id))
|
|
11659
|
+
throw new ArbiError("Expected an assistant message ID (msg-\u2026).", {
|
|
11660
|
+
code: "INVALID_RESPONSE_ID"
|
|
11661
|
+
});
|
|
11662
|
+
const result = await this.openResponseHandle(
|
|
11663
|
+
(signal) => openRunStream(this.getAuthHeaders(), id, signal),
|
|
11664
|
+
options,
|
|
11665
|
+
id
|
|
11666
|
+
);
|
|
11667
|
+
if (result.kind !== "response") throw new ArbiError("Expected a response attachment.");
|
|
11668
|
+
return result.handle;
|
|
11669
|
+
},
|
|
11100
11670
|
/**
|
|
11101
11671
|
* Ask the assistant: send one turn (the body is `buildTurnRequest`) and stream it into
|
|
11102
11672
|
* `handlers`. Resolves with the accumulated result when the stream ends.
|
|
@@ -11125,12 +11695,20 @@ var Arbi = class {
|
|
|
11125
11695
|
if (isAbort(error)) throw error;
|
|
11126
11696
|
if (error instanceof AssistantTurnRefused) {
|
|
11127
11697
|
if (error.quota) onQuotaExceeded?.(error.quota);
|
|
11128
|
-
|
|
11698
|
+
callbacks.onErrorDetails?.(error);
|
|
11699
|
+
return refuse(error.message, error);
|
|
11129
11700
|
}
|
|
11130
11701
|
if (error instanceof ArbiSessionExpiredError || error instanceof ArbiApiError && error.status === 401) {
|
|
11131
11702
|
const message = SESSION_EXPIRED_MESSAGE;
|
|
11132
|
-
|
|
11703
|
+
const expired = error instanceof ArbiSessionExpiredError ? error : new ArbiSessionExpiredError(message, {
|
|
11704
|
+
cause: error,
|
|
11705
|
+
requestId: error.requestId,
|
|
11706
|
+
responseId: error.responseId
|
|
11707
|
+
});
|
|
11708
|
+
callbacks.onErrorDetails?.(expired);
|
|
11709
|
+
return refuse(message, expired);
|
|
11133
11710
|
}
|
|
11711
|
+
if (error instanceof ArbiError) callbacks.onErrorDetails?.(error);
|
|
11134
11712
|
onError?.(error);
|
|
11135
11713
|
throw error;
|
|
11136
11714
|
}
|
|
@@ -11442,12 +12020,12 @@ var Arbi = class {
|
|
|
11442
12020
|
);
|
|
11443
12021
|
},
|
|
11444
12022
|
/** Post plaintext to a chat (encrypted under its current epoch). Returns it decrypted. */
|
|
11445
|
-
send: async (chat,
|
|
12023
|
+
send: async (chat, text2, options) => {
|
|
11446
12024
|
const ready = await this.readyChat(chat);
|
|
11447
12025
|
return sendChatMessage(
|
|
11448
12026
|
this.requireClient(),
|
|
11449
12027
|
await this.chatRing(ready),
|
|
11450
|
-
|
|
12028
|
+
text2,
|
|
11451
12029
|
options
|
|
11452
12030
|
);
|
|
11453
12031
|
},
|
|
@@ -11709,7 +12287,7 @@ var Arbi = class {
|
|
|
11709
12287
|
* Send an E2E-encrypted direct message: resolves the peer, opens (get-or-create) the direct
|
|
11710
12288
|
* chat and posts under the pair key. The caller passes plaintext and never handles keys.
|
|
11711
12289
|
*/
|
|
11712
|
-
send: async (recipient,
|
|
12290
|
+
send: async (recipient, text2) => {
|
|
11713
12291
|
const client = this.requireClient();
|
|
11714
12292
|
const resolved = await resolveRecipient(client, recipient);
|
|
11715
12293
|
if (!resolved) {
|
|
@@ -11721,15 +12299,15 @@ var Arbi = class {
|
|
|
11721
12299
|
client,
|
|
11722
12300
|
await this.requireChatCrypto(),
|
|
11723
12301
|
resolved.extId,
|
|
11724
|
-
|
|
12302
|
+
text2
|
|
11725
12303
|
);
|
|
11726
12304
|
},
|
|
11727
12305
|
/** Send to a peer whose external id is already known, skipping directory resolution. */
|
|
11728
|
-
sendTo: async (peerExtId,
|
|
12306
|
+
sendTo: async (peerExtId, text2) => sendDirectMessage(
|
|
11729
12307
|
this.requireClient(),
|
|
11730
12308
|
await this.requireChatCrypto(),
|
|
11731
12309
|
peerExtId,
|
|
11732
|
-
|
|
12310
|
+
text2
|
|
11733
12311
|
),
|
|
11734
12312
|
/** Resolve a recipient reference to `{ extId, pubKey }` (contacts → agents → workspace). */
|
|
11735
12313
|
resolveRecipient: (recipient) => resolveRecipient(this.requireClient(), recipient),
|
|
@@ -12403,8 +12981,8 @@ function getStepDisplay(step, docInfo = {}) {
|
|
|
12403
12981
|
function getAgentStepLabel(step, docInfo = {}) {
|
|
12404
12982
|
const { label, summary, isMultiLine } = getStepDisplay(step, docInfo);
|
|
12405
12983
|
const narrative = summary && !isMultiLine && !step.reasoning ? summary : null;
|
|
12406
|
-
const
|
|
12407
|
-
return
|
|
12984
|
+
const text2 = narrative ? `${label} \u2014 ${narrative}` : label;
|
|
12985
|
+
return text2.length > 120 ? `${text2.slice(0, 119)}\u2026` : text2;
|
|
12408
12986
|
}
|
|
12409
12987
|
function getCurrentStep(steps) {
|
|
12410
12988
|
const merged = mergeAgentSteps(steps);
|
|
@@ -12468,9 +13046,9 @@ function newestThread(threads) {
|
|
|
12468
13046
|
function pickMainThread(threads) {
|
|
12469
13047
|
return newestThread(threads.filter((t2) => !t2.history.some((m2) => getSubagentBranchRecord(m2)))) ?? threads.find((t2) => tailTaskMessageIndex(t2.history) < 0) ?? threads[0];
|
|
12470
13048
|
}
|
|
12471
|
-
function findSubagentRunId(threads,
|
|
13049
|
+
function findSubagentRunId(threads, messageId2) {
|
|
12472
13050
|
for (const thread of threads) {
|
|
12473
|
-
const index = thread.history.findIndex((m2) => m2.external_id ===
|
|
13051
|
+
const index = thread.history.findIndex((m2) => m2.external_id === messageId2);
|
|
12474
13052
|
if (index < 0) continue;
|
|
12475
13053
|
const taskIndex = tailTaskMessageIndex(thread.history);
|
|
12476
13054
|
if (taskIndex >= 0 && taskIndex <= index) {
|
|
@@ -12630,6 +13208,7 @@ exports.ACCOUNT_VENDOR = ACCOUNT_VENDOR;
|
|
|
12630
13208
|
exports.Arbi = Arbi;
|
|
12631
13209
|
exports.ArbiApiError = ArbiApiError;
|
|
12632
13210
|
exports.ArbiError = ArbiError;
|
|
13211
|
+
exports.ArbiTransportError = ArbiTransportError;
|
|
12633
13212
|
exports.ArbiWorkspaceChangedError = ArbiWorkspaceChangedError;
|
|
12634
13213
|
exports.ArbiWorkspaceSupersededError = ArbiWorkspaceSupersededError;
|
|
12635
13214
|
exports.CallApiError = CallApiError;
|