@acosmi/sdk-ts 1.0.2 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +58 -0
- package/README.md +105 -6
- package/dist/browser/index.mjs +559 -6
- package/dist/browser/index.mjs.map +1 -1
- package/dist/index.mjs +559 -6
- package/dist/index.mjs.map +1 -1
- package/dist/node/adapters/anthropic.cjs.map +1 -1
- package/dist/node/adapters/anthropic.d.cts +1 -1
- package/dist/node/adapters/anthropic.d.ts +1 -1
- package/dist/node/adapters/anthropic.mjs.map +1 -1
- package/dist/node/adapters/openai.cjs.map +1 -1
- package/dist/node/adapters/openai.d.cts +1 -1
- package/dist/node/adapters/openai.d.ts +1 -1
- package/dist/node/adapters/openai.mjs.map +1 -1
- package/dist/node/{index-C3Z_84Bv.d.cts → index-B2Uuh21R.d.cts} +36 -1
- package/dist/node/{index-C3Z_84Bv.d.ts → index-B2Uuh21R.d.ts} +36 -1
- package/dist/node/index.cjs +564 -5
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.d.cts +253 -7
- package/dist/node/index.d.ts +253 -7
- package/dist/node/index.mjs +559 -6
- package/dist/node/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/node/index.cjs
CHANGED
|
@@ -1015,6 +1015,45 @@ var init_adapters = __esm({
|
|
|
1015
1015
|
|
|
1016
1016
|
// src/index.ts
|
|
1017
1017
|
init_types();
|
|
1018
|
+
|
|
1019
|
+
// src/model-helpers.ts
|
|
1020
|
+
function modelSupportsInputModality(model, modality) {
|
|
1021
|
+
if (!model) return false;
|
|
1022
|
+
const mods = model.inputModalities;
|
|
1023
|
+
if (!Array.isArray(mods)) return false;
|
|
1024
|
+
return mods.includes(modality);
|
|
1025
|
+
}
|
|
1026
|
+
function modelSupportsImageInput(model) {
|
|
1027
|
+
return modelSupportsInputModality(model, "image");
|
|
1028
|
+
}
|
|
1029
|
+
function findFirstModelByInputModality(models, modality) {
|
|
1030
|
+
if (!Array.isArray(models)) return null;
|
|
1031
|
+
for (const m of models) {
|
|
1032
|
+
if (!m) continue;
|
|
1033
|
+
if (m.isEnabled === false) continue;
|
|
1034
|
+
if (!modelSupportsInputModality(m, modality)) continue;
|
|
1035
|
+
return m;
|
|
1036
|
+
}
|
|
1037
|
+
return null;
|
|
1038
|
+
}
|
|
1039
|
+
function findDesktopVisualUnderstandingModel(models) {
|
|
1040
|
+
if (!Array.isArray(models)) return null;
|
|
1041
|
+
const candidates = [];
|
|
1042
|
+
for (const m of models) {
|
|
1043
|
+
if (!m) continue;
|
|
1044
|
+
if (m.isEnabled === false) continue;
|
|
1045
|
+
if (m.capabilities?.supports_desktop_visual_understanding !== true) continue;
|
|
1046
|
+
if (!modelSupportsInputModality(m, "image")) continue;
|
|
1047
|
+
candidates.push(m);
|
|
1048
|
+
}
|
|
1049
|
+
if (candidates.length === 0) return null;
|
|
1050
|
+
for (const m of candidates) {
|
|
1051
|
+
if (m.isDefault === true) return m;
|
|
1052
|
+
}
|
|
1053
|
+
return candidates[0];
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
// src/index.ts
|
|
1018
1057
|
init_adapters();
|
|
1019
1058
|
|
|
1020
1059
|
// src/auth.ts
|
|
@@ -1974,8 +2013,25 @@ function extractAnthropicBlockMeta(eventType, data, blockTypeMap) {
|
|
|
1974
2013
|
}
|
|
1975
2014
|
}
|
|
1976
2015
|
|
|
1977
|
-
// src/
|
|
1978
|
-
|
|
2016
|
+
// src/agent-runs-types.ts
|
|
2017
|
+
var AgentRunStreamError = class extends Error {
|
|
2018
|
+
event;
|
|
2019
|
+
code;
|
|
2020
|
+
stage;
|
|
2021
|
+
retryable;
|
|
2022
|
+
constructor(event) {
|
|
2023
|
+
const err = event.error;
|
|
2024
|
+
super(err.stage ? `agent run failed: ${err.stage}: ${err.message}` : `agent run failed: ${err.message}`);
|
|
2025
|
+
this.name = "AgentRunStreamError";
|
|
2026
|
+
this.event = event;
|
|
2027
|
+
this.code = err.code ?? "";
|
|
2028
|
+
this.stage = err.stage ?? "";
|
|
2029
|
+
this.retryable = err.retryable ?? false;
|
|
2030
|
+
}
|
|
2031
|
+
};
|
|
2032
|
+
|
|
2033
|
+
// src/client/agent-runs.ts
|
|
2034
|
+
init_types();
|
|
1979
2035
|
|
|
1980
2036
|
// src/client.ts
|
|
1981
2037
|
init_types();
|
|
@@ -2573,14 +2629,15 @@ var Client = class _Client {
|
|
|
2573
2629
|
null,
|
|
2574
2630
|
signal
|
|
2575
2631
|
);
|
|
2576
|
-
|
|
2632
|
+
const normalized = normalizeInputModalities(result.data);
|
|
2633
|
+
this.modelCache = normalized;
|
|
2577
2634
|
this.modelCacheTimeMs = Date.now();
|
|
2578
2635
|
let status = "";
|
|
2579
2636
|
if (headers) {
|
|
2580
2637
|
const h = headers.get("X-Entitlement-Filter-Status");
|
|
2581
2638
|
if (h) status = h;
|
|
2582
2639
|
}
|
|
2583
|
-
return { models:
|
|
2640
|
+
return { models: normalized, status };
|
|
2584
2641
|
}
|
|
2585
2642
|
/** 查询当前用户账户级权益总览 (v0.19+) */
|
|
2586
2643
|
async getQuotaSummary(signal) {
|
|
@@ -3213,9 +3270,24 @@ function zeroModelCapabilities() {
|
|
|
3213
3270
|
supports_token_efficient: false,
|
|
3214
3271
|
supports_redact_thinking: false,
|
|
3215
3272
|
max_input_tokens: 0,
|
|
3216
|
-
max_output_tokens: 0
|
|
3273
|
+
max_output_tokens: 0,
|
|
3274
|
+
supports_desktop_visual_understanding: false
|
|
3217
3275
|
};
|
|
3218
3276
|
}
|
|
3277
|
+
function normalizeInputModalities(models) {
|
|
3278
|
+
if (!Array.isArray(models)) return models;
|
|
3279
|
+
for (const m of models) {
|
|
3280
|
+
if (!m || typeof m !== "object") continue;
|
|
3281
|
+
if (Array.isArray(m.inputModalities)) continue;
|
|
3282
|
+
const snake = m.input_modalities;
|
|
3283
|
+
if (Array.isArray(snake)) {
|
|
3284
|
+
m.inputModalities = snake.filter(
|
|
3285
|
+
(v) => v === "text" || v === "image"
|
|
3286
|
+
);
|
|
3287
|
+
}
|
|
3288
|
+
}
|
|
3289
|
+
return models;
|
|
3290
|
+
}
|
|
3219
3291
|
function withRequestTimeout(ms, parent) {
|
|
3220
3292
|
const ctl = new AbortController();
|
|
3221
3293
|
const timer = setTimeout(() => ctl.abort(), ms);
|
|
@@ -3256,6 +3328,487 @@ async function sleep(ms, signal) {
|
|
|
3256
3328
|
});
|
|
3257
3329
|
}
|
|
3258
3330
|
|
|
3331
|
+
// src/client/agent-runs.ts
|
|
3332
|
+
var agentRunsByClient = /* @__PURE__ */ new WeakMap();
|
|
3333
|
+
Object.defineProperty(Client.prototype, "agentRuns", {
|
|
3334
|
+
configurable: true,
|
|
3335
|
+
enumerable: false,
|
|
3336
|
+
get() {
|
|
3337
|
+
let existing = agentRunsByClient.get(this);
|
|
3338
|
+
if (!existing) {
|
|
3339
|
+
existing = new AgentRunsClient(this);
|
|
3340
|
+
agentRunsByClient.set(this, existing);
|
|
3341
|
+
}
|
|
3342
|
+
return existing;
|
|
3343
|
+
}
|
|
3344
|
+
});
|
|
3345
|
+
var AgentRunsClient = class {
|
|
3346
|
+
constructor(client) {
|
|
3347
|
+
this.client = client;
|
|
3348
|
+
}
|
|
3349
|
+
client;
|
|
3350
|
+
async create(req, signal) {
|
|
3351
|
+
const resp = await this.requestAPI(
|
|
3352
|
+
"POST",
|
|
3353
|
+
"/agent-runs",
|
|
3354
|
+
toWireCreateRequest(req),
|
|
3355
|
+
signal,
|
|
3356
|
+
{ retryOn401: false }
|
|
3357
|
+
);
|
|
3358
|
+
return fromWireCreateResponse(resp);
|
|
3359
|
+
}
|
|
3360
|
+
get(runId, signal) {
|
|
3361
|
+
return this.requestAPI(
|
|
3362
|
+
"GET",
|
|
3363
|
+
`/agent-runs/${encodeURIComponent(runId)}`,
|
|
3364
|
+
null,
|
|
3365
|
+
signal,
|
|
3366
|
+
{ retryOn401: true }
|
|
3367
|
+
).then(fromWireRun);
|
|
3368
|
+
}
|
|
3369
|
+
stream(runId, opts = {}, signal) {
|
|
3370
|
+
return {
|
|
3371
|
+
[Symbol.asyncIterator]: () => this.streamGen(runId, opts, signal)
|
|
3372
|
+
};
|
|
3373
|
+
}
|
|
3374
|
+
cancel(runId, signal) {
|
|
3375
|
+
return this.requestAPI(
|
|
3376
|
+
"POST",
|
|
3377
|
+
`/agent-runs/${encodeURIComponent(runId)}/cancel`,
|
|
3378
|
+
{},
|
|
3379
|
+
signal,
|
|
3380
|
+
{ retryOn401: false }
|
|
3381
|
+
).then(fromWireRun);
|
|
3382
|
+
}
|
|
3383
|
+
listArtifacts(runId, signal) {
|
|
3384
|
+
return this.requestAPI(
|
|
3385
|
+
"GET",
|
|
3386
|
+
`/agent-runs/${encodeURIComponent(runId)}/artifacts`,
|
|
3387
|
+
null,
|
|
3388
|
+
signal,
|
|
3389
|
+
{ retryOn401: true }
|
|
3390
|
+
).then((r) => (r.artifacts ?? []).map(fromWireArtifact));
|
|
3391
|
+
}
|
|
3392
|
+
async downloadArtifact(runId, artifactId, signal) {
|
|
3393
|
+
const resp = await this.requestRaw(
|
|
3394
|
+
"GET",
|
|
3395
|
+
`/agent-runs/${encodeURIComponent(runId)}/artifacts/${encodeURIComponent(artifactId)}`,
|
|
3396
|
+
null,
|
|
3397
|
+
signal,
|
|
3398
|
+
{ retryOn401: true }
|
|
3399
|
+
);
|
|
3400
|
+
const contentType = resp.headers.get("Content-Type") ?? void 0;
|
|
3401
|
+
const filename = filenameFromContentDisposition(resp.headers.get("Content-Disposition")) ?? artifactId;
|
|
3402
|
+
const data = await readLimited(resp.body, maxDownloadSize);
|
|
3403
|
+
return { data, filename, contentType };
|
|
3404
|
+
}
|
|
3405
|
+
submitLocalToolResult(runId, result, signal) {
|
|
3406
|
+
return this.requestAPI(
|
|
3407
|
+
"POST",
|
|
3408
|
+
`/agent-runs/${encodeURIComponent(runId)}/local-tool-results`,
|
|
3409
|
+
toWireLocalToolResult(result),
|
|
3410
|
+
signal,
|
|
3411
|
+
{ retryOn401: false }
|
|
3412
|
+
).then(fromWireRun);
|
|
3413
|
+
}
|
|
3414
|
+
run(req, opts = {}, signal) {
|
|
3415
|
+
return {
|
|
3416
|
+
[Symbol.asyncIterator]: () => this.runGen(req, opts, signal)
|
|
3417
|
+
};
|
|
3418
|
+
}
|
|
3419
|
+
runWithLocalTools(req, handlers, opts = {}, signal) {
|
|
3420
|
+
return {
|
|
3421
|
+
[Symbol.asyncIterator]: () => this.runWithLocalToolsGen(req, handlers, opts, signal)
|
|
3422
|
+
};
|
|
3423
|
+
}
|
|
3424
|
+
async *runGen(req, opts, signal) {
|
|
3425
|
+
const created = await this.create(req, signal);
|
|
3426
|
+
yield* this.stream(created.runId, opts, signal);
|
|
3427
|
+
}
|
|
3428
|
+
async *runWithLocalToolsGen(req, handlers, opts, signal) {
|
|
3429
|
+
let currentRunId = "";
|
|
3430
|
+
for await (const event of this.run(req, opts, signal)) {
|
|
3431
|
+
if (event.type === "run_started") {
|
|
3432
|
+
currentRunId = event.runId;
|
|
3433
|
+
}
|
|
3434
|
+
if (opts.onEvent) await opts.onEvent(event);
|
|
3435
|
+
let localToolTask;
|
|
3436
|
+
if (event.type === "local_tool_request") {
|
|
3437
|
+
if (currentRunId === "") {
|
|
3438
|
+
throw new Error("local tool request arrived before run_started");
|
|
3439
|
+
}
|
|
3440
|
+
localToolTask = this.invokeLocalTool(currentRunId, event, handlers, opts.timeoutMs, signal).then(async (result) => {
|
|
3441
|
+
await this.submitLocalToolResult(currentRunId, result, signal);
|
|
3442
|
+
});
|
|
3443
|
+
}
|
|
3444
|
+
yield event;
|
|
3445
|
+
if (localToolTask) await localToolTask;
|
|
3446
|
+
}
|
|
3447
|
+
}
|
|
3448
|
+
async invokeLocalTool(runId, event, handlers, timeoutMs = 3e4, signal) {
|
|
3449
|
+
const handler = handlers[event.name];
|
|
3450
|
+
if (!handler) {
|
|
3451
|
+
return {
|
|
3452
|
+
requestId: event.requestId,
|
|
3453
|
+
ok: false,
|
|
3454
|
+
error: `local tool rejected: no handler for ${event.name}`
|
|
3455
|
+
};
|
|
3456
|
+
}
|
|
3457
|
+
const ctl = new AbortController();
|
|
3458
|
+
const timer = setTimeout(() => ctl.abort(), timeoutMs);
|
|
3459
|
+
let parentAbort;
|
|
3460
|
+
if (signal) {
|
|
3461
|
+
if (signal.aborted) ctl.abort();
|
|
3462
|
+
else {
|
|
3463
|
+
parentAbort = () => ctl.abort();
|
|
3464
|
+
signal.addEventListener("abort", parentAbort);
|
|
3465
|
+
}
|
|
3466
|
+
}
|
|
3467
|
+
try {
|
|
3468
|
+
const content = await handler(event.input, {
|
|
3469
|
+
runId,
|
|
3470
|
+
requestId: event.requestId,
|
|
3471
|
+
name: event.name,
|
|
3472
|
+
signal: ctl.signal
|
|
3473
|
+
});
|
|
3474
|
+
return { requestId: event.requestId, ok: true, content };
|
|
3475
|
+
} catch (e) {
|
|
3476
|
+
if (signal?.aborted) throw e;
|
|
3477
|
+
const timedOut = ctl.signal.aborted;
|
|
3478
|
+
return {
|
|
3479
|
+
requestId: event.requestId,
|
|
3480
|
+
ok: false,
|
|
3481
|
+
error: timedOut ? `local tool timed out after ${timeoutMs}ms` : errorMessage(e)
|
|
3482
|
+
};
|
|
3483
|
+
} finally {
|
|
3484
|
+
clearTimeout(timer);
|
|
3485
|
+
if (parentAbort && signal) signal.removeEventListener("abort", parentAbort);
|
|
3486
|
+
}
|
|
3487
|
+
}
|
|
3488
|
+
async *streamGen(runId, opts, signal) {
|
|
3489
|
+
const resp = await this.requestRaw(
|
|
3490
|
+
"GET",
|
|
3491
|
+
`/agent-runs/${encodeURIComponent(runId)}/stream`,
|
|
3492
|
+
null,
|
|
3493
|
+
signal,
|
|
3494
|
+
{ retryOn401: true, accept: "text/event-stream" }
|
|
3495
|
+
);
|
|
3496
|
+
if (!resp.body) {
|
|
3497
|
+
throw new Error("agent run stream: empty response body");
|
|
3498
|
+
}
|
|
3499
|
+
for await (const event of readAgentRunEvents(resp.body)) {
|
|
3500
|
+
if (event.type === "error" && opts.throwOnError !== false) {
|
|
3501
|
+
throw new AgentRunStreamError(event);
|
|
3502
|
+
}
|
|
3503
|
+
yield event;
|
|
3504
|
+
}
|
|
3505
|
+
}
|
|
3506
|
+
async requestAPI(method, path, body, signal, opts) {
|
|
3507
|
+
const resp = await this.requestRaw(method, path, body, signal, opts);
|
|
3508
|
+
const text = await resp.text();
|
|
3509
|
+
const result = JSON.parse(text);
|
|
3510
|
+
const bizErr = apiResponseBusinessError(result);
|
|
3511
|
+
if (bizErr) throw bizErr;
|
|
3512
|
+
return result.data;
|
|
3513
|
+
}
|
|
3514
|
+
async requestRaw(method, path, body, signal, opts, retried = false) {
|
|
3515
|
+
const token = await this.client.ensureToken(signal);
|
|
3516
|
+
const url = this.client.apiURL(path);
|
|
3517
|
+
const headers = {
|
|
3518
|
+
Authorization: `Bearer ${token}`,
|
|
3519
|
+
Accept: opts.accept ?? "application/json"
|
|
3520
|
+
};
|
|
3521
|
+
let bodyStr;
|
|
3522
|
+
if (body != null) {
|
|
3523
|
+
bodyStr = typeof body === "string" ? body : JSON.stringify(body);
|
|
3524
|
+
headers["Content-Type"] = "application/json";
|
|
3525
|
+
}
|
|
3526
|
+
const resp = await this.client.doRequest({ method, url, headers, body: bodyStr }, signal);
|
|
3527
|
+
if (resp.status === 401 && opts.retryOn401 && !retried) {
|
|
3528
|
+
try {
|
|
3529
|
+
await resp.body?.cancel();
|
|
3530
|
+
} catch {
|
|
3531
|
+
}
|
|
3532
|
+
await this.client.forceRefresh(signal);
|
|
3533
|
+
return this.requestRaw(method, path, body, signal, opts, true);
|
|
3534
|
+
}
|
|
3535
|
+
if (resp.status < 200 || resp.status >= 300) {
|
|
3536
|
+
const bodyBytes = resp.body ? await readLimited(resp.body, maxErrorBodySize) : new Uint8Array();
|
|
3537
|
+
throw parseHTTPErrorWithHeader(resp.status, bodyBytes, resp.headers);
|
|
3538
|
+
}
|
|
3539
|
+
return resp;
|
|
3540
|
+
}
|
|
3541
|
+
};
|
|
3542
|
+
function toWireCreateRequest(req) {
|
|
3543
|
+
return {
|
|
3544
|
+
app_id: req.appId,
|
|
3545
|
+
mode: req.mode,
|
|
3546
|
+
session_id: req.sessionId,
|
|
3547
|
+
input: req.input,
|
|
3548
|
+
messages: req.messages,
|
|
3549
|
+
model: req.model,
|
|
3550
|
+
active_skill_ids: req.activeSkillIds,
|
|
3551
|
+
knowledge_base_ids: req.knowledgeBaseIds,
|
|
3552
|
+
metadata: req.metadata,
|
|
3553
|
+
local_context_policy: req.localContextPolicy ? {
|
|
3554
|
+
enabled: req.localContextPolicy.enabled,
|
|
3555
|
+
readonly: req.localContextPolicy.readonly,
|
|
3556
|
+
max_bytes: req.localContextPolicy.maxBytes,
|
|
3557
|
+
allowed_tools: req.localContextPolicy.allowedTools
|
|
3558
|
+
} : void 0,
|
|
3559
|
+
artifact_policy: req.artifactPolicy ? {
|
|
3560
|
+
enabled: req.artifactPolicy.enabled,
|
|
3561
|
+
max_files: req.artifactPolicy.maxFiles
|
|
3562
|
+
} : void 0
|
|
3563
|
+
};
|
|
3564
|
+
}
|
|
3565
|
+
function toWireLocalToolResult(result) {
|
|
3566
|
+
return {
|
|
3567
|
+
request_id: result.requestId,
|
|
3568
|
+
ok: result.ok,
|
|
3569
|
+
content: result.content,
|
|
3570
|
+
error: result.error
|
|
3571
|
+
};
|
|
3572
|
+
}
|
|
3573
|
+
function fromWireCreateResponse(resp) {
|
|
3574
|
+
return {
|
|
3575
|
+
runId: resp.run_id ?? "",
|
|
3576
|
+
sessionId: resp.session_id ?? "",
|
|
3577
|
+
status: toStatus(resp.status)
|
|
3578
|
+
};
|
|
3579
|
+
}
|
|
3580
|
+
function fromWireRun(resp) {
|
|
3581
|
+
return {
|
|
3582
|
+
runId: resp.run_id ?? "",
|
|
3583
|
+
sessionId: resp.session_id ?? "",
|
|
3584
|
+
appId: resp.app_id,
|
|
3585
|
+
mode: resp.mode,
|
|
3586
|
+
status: toStatus(resp.status),
|
|
3587
|
+
createdAt: resp.created_at,
|
|
3588
|
+
startedAt: resp.started_at,
|
|
3589
|
+
completedAt: resp.completed_at,
|
|
3590
|
+
error: normalizeError(resp.error),
|
|
3591
|
+
metadata: resp.metadata
|
|
3592
|
+
};
|
|
3593
|
+
}
|
|
3594
|
+
function fromWireArtifact(resp) {
|
|
3595
|
+
return {
|
|
3596
|
+
id: resp.id ?? resp.artifact_id ?? "",
|
|
3597
|
+
filename: resp.filename ?? resp.name ?? resp.id ?? resp.artifact_id ?? "artifact",
|
|
3598
|
+
contentType: resp.content_type ?? resp.mime_type,
|
|
3599
|
+
size: typeof resp.size === "number" ? resp.size : void 0,
|
|
3600
|
+
type: resp.type,
|
|
3601
|
+
metadata: resp.metadata
|
|
3602
|
+
};
|
|
3603
|
+
}
|
|
3604
|
+
async function* readAgentRunEvents(body) {
|
|
3605
|
+
let eventName = "";
|
|
3606
|
+
let dataLines = [];
|
|
3607
|
+
const flush = () => {
|
|
3608
|
+
if (dataLines.length === 0) return null;
|
|
3609
|
+
const data = dataLines.join("\n");
|
|
3610
|
+
dataLines = [];
|
|
3611
|
+
if (data === "[DONE]") return null;
|
|
3612
|
+
return parseAgentRunEvent(eventName, data);
|
|
3613
|
+
};
|
|
3614
|
+
for await (const line of iterSSELines(body)) {
|
|
3615
|
+
if (line === "") {
|
|
3616
|
+
const event2 = flush();
|
|
3617
|
+
eventName = "";
|
|
3618
|
+
if (event2) yield event2;
|
|
3619
|
+
continue;
|
|
3620
|
+
}
|
|
3621
|
+
if (line.startsWith(":")) continue;
|
|
3622
|
+
if (line.startsWith("event:")) {
|
|
3623
|
+
eventName = line.slice("event:".length).trim();
|
|
3624
|
+
continue;
|
|
3625
|
+
}
|
|
3626
|
+
if (line.startsWith("data:")) {
|
|
3627
|
+
dataLines.push(line.slice("data:".length).trimStart());
|
|
3628
|
+
}
|
|
3629
|
+
}
|
|
3630
|
+
const event = flush();
|
|
3631
|
+
if (event) yield event;
|
|
3632
|
+
}
|
|
3633
|
+
function parseAgentRunEvent(eventName, data) {
|
|
3634
|
+
const payload = JSON.parse(data);
|
|
3635
|
+
const obj = isRecord(payload) ? payload : { type: eventName, data: payload };
|
|
3636
|
+
const type = stringField(obj, "type") || eventName;
|
|
3637
|
+
switch (type) {
|
|
3638
|
+
case "run_started":
|
|
3639
|
+
return {
|
|
3640
|
+
type: "run_started",
|
|
3641
|
+
runId: stringField(obj, "run_id", "runId"),
|
|
3642
|
+
sessionId: stringField(obj, "session_id", "sessionId")
|
|
3643
|
+
};
|
|
3644
|
+
case "status":
|
|
3645
|
+
return {
|
|
3646
|
+
type: "status",
|
|
3647
|
+
status: stringField(obj, "status"),
|
|
3648
|
+
message: optionalStringField(obj, "message")
|
|
3649
|
+
};
|
|
3650
|
+
case "text_delta":
|
|
3651
|
+
return { type: "text_delta", text: stringField(obj, "text") };
|
|
3652
|
+
case "reasoning_delta":
|
|
3653
|
+
return { type: "reasoning_delta", text: stringField(obj, "text") };
|
|
3654
|
+
case "tool_call":
|
|
3655
|
+
return {
|
|
3656
|
+
type: "tool_call",
|
|
3657
|
+
id: stringField(obj, "id"),
|
|
3658
|
+
name: stringField(obj, "name"),
|
|
3659
|
+
input: obj.input
|
|
3660
|
+
};
|
|
3661
|
+
case "tool_result":
|
|
3662
|
+
return {
|
|
3663
|
+
type: "tool_result",
|
|
3664
|
+
id: stringField(obj, "id"),
|
|
3665
|
+
name: optionalStringField(obj, "name"),
|
|
3666
|
+
result: obj.result,
|
|
3667
|
+
error: optionalStringField(obj, "error")
|
|
3668
|
+
};
|
|
3669
|
+
case "local_tool_request":
|
|
3670
|
+
return {
|
|
3671
|
+
type: "local_tool_request",
|
|
3672
|
+
requestId: stringField(obj, "request_id", "requestId"),
|
|
3673
|
+
name: stringField(obj, "name"),
|
|
3674
|
+
input: obj.input
|
|
3675
|
+
};
|
|
3676
|
+
case "artifact":
|
|
3677
|
+
return {
|
|
3678
|
+
type: "artifact",
|
|
3679
|
+
artifact: fromWireArtifact(isRecord(obj.artifact) ? obj.artifact : obj)
|
|
3680
|
+
};
|
|
3681
|
+
case "sources":
|
|
3682
|
+
return { type: "sources", sources: obj.sources };
|
|
3683
|
+
case "usage":
|
|
3684
|
+
return {
|
|
3685
|
+
type: "usage",
|
|
3686
|
+
usage: normalizeUsage(isRecord(obj.usage) ? obj.usage : obj)
|
|
3687
|
+
};
|
|
3688
|
+
case "settle":
|
|
3689
|
+
return {
|
|
3690
|
+
type: "settle",
|
|
3691
|
+
settlement: normalizeSettlement(isRecord(obj.settlement) ? obj.settlement : obj)
|
|
3692
|
+
};
|
|
3693
|
+
case "error":
|
|
3694
|
+
return { type: "error", error: normalizeError(obj.error) ?? normalizeError(obj) };
|
|
3695
|
+
case "done":
|
|
3696
|
+
return {
|
|
3697
|
+
type: "done",
|
|
3698
|
+
runId: stringField(obj, "run_id", "runId"),
|
|
3699
|
+
status: stringField(obj, "status")
|
|
3700
|
+
};
|
|
3701
|
+
default:
|
|
3702
|
+
return {
|
|
3703
|
+
type: "error",
|
|
3704
|
+
error: {
|
|
3705
|
+
code: "unknown_event",
|
|
3706
|
+
message: `unknown agent run event: ${type}`,
|
|
3707
|
+
raw: obj
|
|
3708
|
+
}
|
|
3709
|
+
};
|
|
3710
|
+
}
|
|
3711
|
+
}
|
|
3712
|
+
function normalizeUsage(value) {
|
|
3713
|
+
return {
|
|
3714
|
+
...value,
|
|
3715
|
+
inputTokens: numberField(value, "input_tokens", "inputTokens"),
|
|
3716
|
+
outputTokens: numberField(value, "output_tokens", "outputTokens"),
|
|
3717
|
+
totalTokens: numberField(value, "total_tokens", "totalTokens"),
|
|
3718
|
+
cacheReadTokens: numberField(value, "cache_read_tokens", "cacheReadTokens"),
|
|
3719
|
+
cacheCreateTokens: numberField(value, "cache_create_tokens", "cacheCreateTokens"),
|
|
3720
|
+
exact: booleanField(value, "exact"),
|
|
3721
|
+
source: optionalStringField(value, "source")
|
|
3722
|
+
};
|
|
3723
|
+
}
|
|
3724
|
+
function normalizeSettlement(value) {
|
|
3725
|
+
return {
|
|
3726
|
+
...value,
|
|
3727
|
+
requestId: optionalStringField(value, "request_id", "requestId"),
|
|
3728
|
+
status: optionalStringField(value, "status"),
|
|
3729
|
+
consumeStatus: optionalStringField(value, "consume_status", "consumeStatus"),
|
|
3730
|
+
inputTokens: numberField(value, "input_tokens", "inputTokens"),
|
|
3731
|
+
outputTokens: numberField(value, "output_tokens", "outputTokens"),
|
|
3732
|
+
totalTokens: numberField(value, "total_tokens", "totalTokens"),
|
|
3733
|
+
cacheReadTokens: numberField(value, "cache_read_tokens", "cacheReadTokens"),
|
|
3734
|
+
cacheCreateTokens: numberField(value, "cache_create_tokens", "cacheCreateTokens"),
|
|
3735
|
+
tokenRemaining: numberField(value, "token_remaining", "tokenRemaining"),
|
|
3736
|
+
callRemaining: numberField(value, "call_remaining", "callRemaining"),
|
|
3737
|
+
retryQueued: booleanField(value, "retry_queued", "retryQueued"),
|
|
3738
|
+
exact: booleanField(value, "exact")
|
|
3739
|
+
};
|
|
3740
|
+
}
|
|
3741
|
+
function normalizeError(value) {
|
|
3742
|
+
if (value == null) return void 0;
|
|
3743
|
+
if (typeof value === "string") return { message: value, raw: value };
|
|
3744
|
+
if (!isRecord(value)) return { message: String(value), raw: value };
|
|
3745
|
+
return {
|
|
3746
|
+
code: optionalStringField(value, "code", "error_code", "errorCode"),
|
|
3747
|
+
message: stringField(value, "message", "error") || "agent run failed",
|
|
3748
|
+
stage: optionalStringField(value, "stage"),
|
|
3749
|
+
retryable: typeof value.retryable === "boolean" ? value.retryable : void 0,
|
|
3750
|
+
raw: value
|
|
3751
|
+
};
|
|
3752
|
+
}
|
|
3753
|
+
function toStatus(status) {
|
|
3754
|
+
switch (status) {
|
|
3755
|
+
case "running":
|
|
3756
|
+
case "completed":
|
|
3757
|
+
case "failed":
|
|
3758
|
+
case "cancelled":
|
|
3759
|
+
return status;
|
|
3760
|
+
default:
|
|
3761
|
+
return "queued";
|
|
3762
|
+
}
|
|
3763
|
+
}
|
|
3764
|
+
function isRecord(value) {
|
|
3765
|
+
return value != null && typeof value === "object" && !Array.isArray(value);
|
|
3766
|
+
}
|
|
3767
|
+
function stringField(obj, ...keys) {
|
|
3768
|
+
for (const key of keys) {
|
|
3769
|
+
const value = obj[key];
|
|
3770
|
+
if (typeof value === "string") return value;
|
|
3771
|
+
}
|
|
3772
|
+
return "";
|
|
3773
|
+
}
|
|
3774
|
+
function optionalStringField(obj, ...keys) {
|
|
3775
|
+
const value = stringField(obj, ...keys);
|
|
3776
|
+
return value === "" ? void 0 : value;
|
|
3777
|
+
}
|
|
3778
|
+
function numberField(obj, ...keys) {
|
|
3779
|
+
for (const key of keys) {
|
|
3780
|
+
const value = obj[key];
|
|
3781
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
3782
|
+
}
|
|
3783
|
+
return void 0;
|
|
3784
|
+
}
|
|
3785
|
+
function booleanField(obj, ...keys) {
|
|
3786
|
+
for (const key of keys) {
|
|
3787
|
+
const value = obj[key];
|
|
3788
|
+
if (typeof value === "boolean") return value;
|
|
3789
|
+
}
|
|
3790
|
+
return void 0;
|
|
3791
|
+
}
|
|
3792
|
+
function filenameFromContentDisposition(value) {
|
|
3793
|
+
if (!value) return null;
|
|
3794
|
+
const utf8 = /filename\*=UTF-8''([^;]+)/i.exec(value);
|
|
3795
|
+
if (utf8?.[1]) {
|
|
3796
|
+
try {
|
|
3797
|
+
return decodeURIComponent(utf8[1]);
|
|
3798
|
+
} catch {
|
|
3799
|
+
return utf8[1];
|
|
3800
|
+
}
|
|
3801
|
+
}
|
|
3802
|
+
const plain = /filename="?([^";]+)"?/i.exec(value);
|
|
3803
|
+
return plain?.[1] ?? null;
|
|
3804
|
+
}
|
|
3805
|
+
function errorMessage(e) {
|
|
3806
|
+
return e instanceof Error ? e.message : String(e);
|
|
3807
|
+
}
|
|
3808
|
+
|
|
3809
|
+
// src/index.ts
|
|
3810
|
+
init_betas();
|
|
3811
|
+
|
|
3259
3812
|
// src/client/entitlements.ts
|
|
3260
3813
|
Client.prototype.getBalance = async function(signal) {
|
|
3261
3814
|
const resp = await this.doJSON(
|
|
@@ -4080,6 +4633,8 @@ Client.prototype.getBugReport = async function(bugID, signal) {
|
|
|
4080
4633
|
return resp.data;
|
|
4081
4634
|
};
|
|
4082
4635
|
|
|
4636
|
+
exports.AgentRunStreamError = AgentRunStreamError;
|
|
4637
|
+
exports.AgentRunsClient = AgentRunsClient;
|
|
4083
4638
|
exports.Client = Client;
|
|
4084
4639
|
exports.DefaultRetryPolicy = DefaultRetryPolicy;
|
|
4085
4640
|
exports.ErrAuthDenied = ErrAuthDenied;
|
|
@@ -4136,10 +4691,14 @@ exports.effectivePolicy = effectivePolicy;
|
|
|
4136
4691
|
exports.exchangeCode = exchangeCode;
|
|
4137
4692
|
exports.extractAnthropicBlockMeta = extractAnthropicBlockMeta;
|
|
4138
4693
|
exports.fileLockDefaults = fileLockDefaults;
|
|
4694
|
+
exports.findDesktopVisualUnderstandingModel = findDesktopVisualUnderstandingModel;
|
|
4695
|
+
exports.findFirstModelByInputModality = findFirstModelByInputModality;
|
|
4139
4696
|
exports.getAdapter = getAdapter;
|
|
4140
4697
|
exports.getAdapterForModel = getAdapterForModel;
|
|
4141
4698
|
exports.isSSLError = isSSLError;
|
|
4142
4699
|
exports.modelScopes = modelScopes;
|
|
4700
|
+
exports.modelSupportsImageInput = modelSupportsImageInput;
|
|
4701
|
+
exports.modelSupportsInputModality = modelSupportsInputModality;
|
|
4143
4702
|
exports.newFileTokenStore = newFileTokenStore;
|
|
4144
4703
|
exports.newThinkingConfig = newThinkingConfig;
|
|
4145
4704
|
exports.newTokenSet = newTokenSet;
|