@zihin/agent-client 0.8.0 → 0.9.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/README.md +23 -0
- package/dist/chunk-E2AJSAPA.mjs +2220 -0
- package/dist/core.d.mts +1150 -0
- package/dist/core.d.ts +1150 -0
- package/dist/core.js +2255 -0
- package/dist/core.mjs +38 -0
- package/dist/index.d.mts +17 -1114
- package/dist/index.d.ts +17 -1114
- package/dist/index.js +97 -49
- package/dist/index.mjs +42 -2173
- package/package.json +18 -1
package/dist/index.js
CHANGED
|
@@ -24,10 +24,10 @@ __export(index_exports, {
|
|
|
24
24
|
DEFAULT_LIMITS: () => DEFAULT_LIMITS,
|
|
25
25
|
MAX_TASK_PAYLOAD_BYTES: () => MAX_TASK_PAYLOAD_BYTES,
|
|
26
26
|
OPERATION_TIMEOUT_MS: () => OPERATION_TIMEOUT_MS,
|
|
27
|
-
ZihinAgentClient: () =>
|
|
27
|
+
ZihinAgentClient: () => ZihinAgentClient2,
|
|
28
28
|
ZihinAgentError: () => ZihinAgentError,
|
|
29
29
|
classifyStreamErrorKind: () => classifyStreamErrorKind,
|
|
30
|
-
createClient: () =>
|
|
30
|
+
createClient: () => createClient2,
|
|
31
31
|
errorFromDoneChunk: () => errorFromDoneChunk,
|
|
32
32
|
errorFromOutcome: () => errorFromOutcome,
|
|
33
33
|
errorFromStreamChunk: () => errorFromStreamChunk,
|
|
@@ -91,16 +91,11 @@ function isTerminalStatus(status) {
|
|
|
91
91
|
return !RUNNING_STATUSES.has(status.toLowerCase().replace(/-/g, "_"));
|
|
92
92
|
}
|
|
93
93
|
var _warnedDeprecations = /* @__PURE__ */ new Set();
|
|
94
|
-
function warnDeprecatedOnce(code, message) {
|
|
94
|
+
function warnDeprecatedOnce(code, message, warn) {
|
|
95
95
|
if (_warnedDeprecations.has(code)) return;
|
|
96
96
|
_warnedDeprecations.add(code);
|
|
97
97
|
try {
|
|
98
|
-
|
|
99
|
-
if (typeof proc?.emitWarning === "function") {
|
|
100
|
-
proc.emitWarning(message, { type: "ZihinDeprecationWarning", code });
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
console.warn(`[${code}] ${message}`);
|
|
98
|
+
warn?.(code, message);
|
|
104
99
|
} catch {
|
|
105
100
|
}
|
|
106
101
|
}
|
|
@@ -498,8 +493,11 @@ function normalizeTimeoutMs(value, label = "timeoutMs") {
|
|
|
498
493
|
var DEFAULT_MAX_RETRIES = 2;
|
|
499
494
|
var DEFAULT_MAX_EVENT_BYTES = 1 * 1024 * 1024;
|
|
500
495
|
var DEFAULT_MAX_TOTAL_BYTES = 64 * 1024 * 1024;
|
|
496
|
+
function globalFetch() {
|
|
497
|
+
return typeof fetch === "function" ? fetch : void 0;
|
|
498
|
+
}
|
|
501
499
|
function resolveFetch(injected) {
|
|
502
|
-
const f = injected ??
|
|
500
|
+
const f = injected ?? globalFetch();
|
|
503
501
|
if (typeof f !== "function") {
|
|
504
502
|
throw new ZihinAgentError("config", "Global fetch is unavailable in this runtime.");
|
|
505
503
|
}
|
|
@@ -518,7 +516,13 @@ function resolveConfig(config) {
|
|
|
518
516
|
if (!urlCheck.valid) {
|
|
519
517
|
throw new ZihinAgentError("config", urlCheck.error || "Invalid base URL.");
|
|
520
518
|
}
|
|
521
|
-
if (typeof
|
|
519
|
+
if (typeof config.runtime?.sleep !== "function") {
|
|
520
|
+
throw new ZihinAgentError(
|
|
521
|
+
"config",
|
|
522
|
+
'A runtime is required: import from "@zihin/agent-client" (default runtime) or pass `runtime` to the "@zihin/agent-client/core" client.'
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
if (typeof (config.fetch ?? globalFetch()) !== "function") {
|
|
522
526
|
throw new ZihinAgentError(
|
|
523
527
|
"config",
|
|
524
528
|
"Global fetch is unavailable in this runtime. Use Node >=20.16 or pass a `fetch` implementation in the client config."
|
|
@@ -532,6 +536,7 @@ function resolveConfig(config) {
|
|
|
532
536
|
maxStreamEventBytes: config.maxStreamEventBytes ?? DEFAULT_MAX_EVENT_BYTES,
|
|
533
537
|
maxStreamTotalBytes: config.maxStreamTotalBytes ?? DEFAULT_MAX_TOTAL_BYTES,
|
|
534
538
|
fetchImpl: config.fetch,
|
|
539
|
+
runtime: config.runtime,
|
|
535
540
|
onRetry: config.onRetry,
|
|
536
541
|
onError: config.onError
|
|
537
542
|
};
|
|
@@ -564,35 +569,32 @@ function buildHeaders(apiKey, extra = {}) {
|
|
|
564
569
|
...extra
|
|
565
570
|
};
|
|
566
571
|
}
|
|
567
|
-
function delay(ms, signal, meta) {
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
reject(new ZihinAgentError("aborted", "Request was aborted by the caller.", meta));
|
|
576
|
-
};
|
|
577
|
-
const timer = setTimeout(() => {
|
|
578
|
-
if (signal) signal.removeEventListener("abort", onAbort);
|
|
579
|
-
resolve();
|
|
580
|
-
}, ms);
|
|
581
|
-
if (signal) signal.addEventListener("abort", onAbort, { once: true });
|
|
582
|
-
});
|
|
572
|
+
async function delay(runtime, ms, signal, meta) {
|
|
573
|
+
const aborted = () => new ZihinAgentError("aborted", "Request was aborted by the caller.", meta);
|
|
574
|
+
if (signal?.aborted) throw aborted();
|
|
575
|
+
try {
|
|
576
|
+
await runtime.sleep(ms, signal);
|
|
577
|
+
} catch {
|
|
578
|
+
throw aborted();
|
|
579
|
+
}
|
|
583
580
|
}
|
|
584
581
|
function backoffMs(attempt) {
|
|
585
582
|
const base = Math.min(1e3 * 2 ** attempt, 1e4);
|
|
586
583
|
return base / 2 + Math.random() * (base / 2);
|
|
587
584
|
}
|
|
588
|
-
function createDeadline(timeoutMs, callerSignal) {
|
|
585
|
+
function createDeadline(runtime, timeoutMs, callerSignal) {
|
|
589
586
|
const controller = new AbortController();
|
|
587
|
+
const clock = new AbortController();
|
|
590
588
|
let timedOut = false;
|
|
591
589
|
let abortedByCaller = false;
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
590
|
+
runtime.sleep(timeoutMs, clock.signal).then(
|
|
591
|
+
() => {
|
|
592
|
+
timedOut = true;
|
|
593
|
+
controller.abort();
|
|
594
|
+
},
|
|
595
|
+
() => {
|
|
596
|
+
}
|
|
597
|
+
);
|
|
596
598
|
const onAbort = () => {
|
|
597
599
|
abortedByCaller = true;
|
|
598
600
|
controller.abort();
|
|
@@ -604,7 +606,7 @@ function createDeadline(timeoutMs, callerSignal) {
|
|
|
604
606
|
return {
|
|
605
607
|
signal: controller.signal,
|
|
606
608
|
dispose: () => {
|
|
607
|
-
|
|
609
|
+
clock.abort();
|
|
608
610
|
if (callerSignal) callerSignal.removeEventListener("abort", onAbort);
|
|
609
611
|
},
|
|
610
612
|
timedOut: () => timedOut,
|
|
@@ -635,7 +637,7 @@ async function requestBuffered(args) {
|
|
|
635
637
|
const budget = budgetTracker({ ...args, endpoint });
|
|
636
638
|
let lastError = null;
|
|
637
639
|
for (let attempt = 0; attempt <= args.maxRetries; attempt++) {
|
|
638
|
-
const d = createDeadline(args.timeoutMs, args.callerSignal);
|
|
640
|
+
const d = createDeadline(args.runtime, args.timeoutMs, args.callerSignal);
|
|
639
641
|
try {
|
|
640
642
|
const res = await doFetch(args.url, {
|
|
641
643
|
method: args.method,
|
|
@@ -662,7 +664,7 @@ async function requestBuffered(args) {
|
|
|
662
664
|
}
|
|
663
665
|
d.dispose();
|
|
664
666
|
args.onRetry?.({ endpoint, attempt: attempt + 1, delayMs: wait, reason: "status", status: res.status });
|
|
665
|
-
await delay(wait, args.callerSignal, { endpoint, attempts: attempt + 1 });
|
|
667
|
+
await delay(args.runtime, wait, args.callerSignal, { endpoint, attempts: attempt + 1 });
|
|
666
668
|
continue;
|
|
667
669
|
}
|
|
668
670
|
const text = peeked ?? await res.text();
|
|
@@ -681,7 +683,7 @@ async function requestBuffered(args) {
|
|
|
681
683
|
const wait = backoffMs(attempt);
|
|
682
684
|
if (budget.overheadLeft() <= wait) throw lastError;
|
|
683
685
|
args.onRetry?.({ endpoint, attempt: attempt + 1, delayMs: wait, reason: "network" });
|
|
684
|
-
await delay(wait, args.callerSignal, { endpoint, attempts: attempt + 1 });
|
|
686
|
+
await delay(args.runtime, wait, args.callerSignal, { endpoint, attempts: attempt + 1 });
|
|
685
687
|
}
|
|
686
688
|
}
|
|
687
689
|
throw lastError ?? new ZihinAgentError("unknown", "Max retries exceeded.", { endpoint });
|
|
@@ -692,7 +694,7 @@ async function openStream(args) {
|
|
|
692
694
|
const budget = budgetTracker({ ...args, endpoint });
|
|
693
695
|
const policy = args.idempotent ? "idempotent" : "side-effecting";
|
|
694
696
|
for (let attempt = 0; attempt <= args.maxRetries; attempt++) {
|
|
695
|
-
const d = createDeadline(args.timeoutMs, args.callerSignal);
|
|
697
|
+
const d = createDeadline(args.runtime, args.timeoutMs, args.callerSignal);
|
|
696
698
|
try {
|
|
697
699
|
const response = await doFetch(args.url, {
|
|
698
700
|
method: "POST",
|
|
@@ -712,7 +714,7 @@ async function openStream(args) {
|
|
|
712
714
|
}
|
|
713
715
|
d.dispose();
|
|
714
716
|
args.onRetry?.({ endpoint, attempt: attempt + 1, delayMs: wait, reason: "status", status: 429 });
|
|
715
|
-
await delay(wait, args.callerSignal, { endpoint, attempts: attempt + 1 });
|
|
717
|
+
await delay(args.runtime, wait, args.callerSignal, { endpoint, attempts: attempt + 1 });
|
|
716
718
|
continue;
|
|
717
719
|
}
|
|
718
720
|
if (wait !== null && budget.overheadLeft() > wait) {
|
|
@@ -728,7 +730,7 @@ async function openStream(args) {
|
|
|
728
730
|
reason: "status",
|
|
729
731
|
status: response.status
|
|
730
732
|
});
|
|
731
|
-
await delay(wait, args.callerSignal, { endpoint, attempts: attempt + 1 });
|
|
733
|
+
await delay(args.runtime, wait, args.callerSignal, { endpoint, attempts: attempt + 1 });
|
|
732
734
|
continue;
|
|
733
735
|
}
|
|
734
736
|
return { response, deadline: d, endpoint };
|
|
@@ -743,7 +745,7 @@ async function openStream(args) {
|
|
|
743
745
|
const wait = backoffMs(attempt);
|
|
744
746
|
if (policy === "idempotent" && netErr.retryable && attempt < args.maxRetries && budget.overheadLeft() > wait) {
|
|
745
747
|
args.onRetry?.({ endpoint, attempt: attempt + 1, delayMs: wait, reason: "network" });
|
|
746
|
-
await delay(wait, args.callerSignal, { endpoint, attempts: attempt + 1 });
|
|
748
|
+
await delay(args.runtime, wait, args.callerSignal, { endpoint, attempts: attempt + 1 });
|
|
747
749
|
continue;
|
|
748
750
|
}
|
|
749
751
|
throw netErr;
|
|
@@ -772,6 +774,7 @@ async function requestJson(cfg, args) {
|
|
|
772
774
|
policy: args.policy,
|
|
773
775
|
callerSignal: args.signal,
|
|
774
776
|
fetchImpl: cfg.fetchImpl,
|
|
777
|
+
runtime: cfg.runtime,
|
|
775
778
|
onRetry: cfg.onRetry
|
|
776
779
|
});
|
|
777
780
|
if (!res.ok) {
|
|
@@ -806,6 +809,7 @@ async function listAgents(cfg, opts = {}) {
|
|
|
806
809
|
policy: "idempotent",
|
|
807
810
|
callerSignal: opts.signal,
|
|
808
811
|
fetchImpl: cfg.fetchImpl,
|
|
812
|
+
runtime: cfg.runtime,
|
|
809
813
|
onRetry: cfg.onRetry
|
|
810
814
|
});
|
|
811
815
|
if (!res.ok) {
|
|
@@ -1093,7 +1097,7 @@ function buildRequestBody(params, normalizedTimeoutMs) {
|
|
|
1093
1097
|
}
|
|
1094
1098
|
return body;
|
|
1095
1099
|
}
|
|
1096
|
-
function validateParams(params) {
|
|
1100
|
+
function validateParams(params, warn) {
|
|
1097
1101
|
if (!params.agentId) {
|
|
1098
1102
|
throw new ZihinAgentError("config", "agentId is required.");
|
|
1099
1103
|
}
|
|
@@ -1107,7 +1111,7 @@ function validateParams(params) {
|
|
|
1107
1111
|
}
|
|
1108
1112
|
}
|
|
1109
1113
|
validateStructuredInput(params);
|
|
1110
|
-
warnDeprecatedOptions(params);
|
|
1114
|
+
warnDeprecatedOptions(params, warn);
|
|
1111
1115
|
if (params.options?.timeoutMs !== void 0) {
|
|
1112
1116
|
normalizeTimeoutMs(params.options.timeoutMs, "options.timeoutMs");
|
|
1113
1117
|
}
|
|
@@ -1118,19 +1122,21 @@ function validateParams(params) {
|
|
|
1118
1122
|
}
|
|
1119
1123
|
}
|
|
1120
1124
|
}
|
|
1121
|
-
function warnDeprecatedOptions(params) {
|
|
1125
|
+
function warnDeprecatedOptions(params, warn) {
|
|
1122
1126
|
const o = params.options;
|
|
1123
1127
|
if (!o) return;
|
|
1124
1128
|
if (o.jitMode !== void 0) {
|
|
1125
1129
|
warnDeprecatedOnce(
|
|
1126
1130
|
"ZIHIN_DEP001",
|
|
1127
|
-
"`options.jitMode` is deprecated and has no effect (the server discontinued it). Remove it; it will be dropped from the SDK in 1.0."
|
|
1131
|
+
"`options.jitMode` is deprecated and has no effect (the server discontinued it). Remove it; it will be dropped from the SDK in 1.0.",
|
|
1132
|
+
warn
|
|
1128
1133
|
);
|
|
1129
1134
|
}
|
|
1130
1135
|
if (o.temperature !== void 0 || o.maxTokens !== void 0) {
|
|
1131
1136
|
warnDeprecatedOnce(
|
|
1132
1137
|
"ZIHIN_DEP002",
|
|
1133
|
-
"`options.temperature`/`options.maxTokens` have no effect: the agent's own configuration (`llm_config`) decides them \u2014 change it in the Zihin console or with `update_agent`. They will be dropped from the SDK in 1.0."
|
|
1138
|
+
"`options.temperature`/`options.maxTokens` have no effect: the agent's own configuration (`llm_config`) decides them \u2014 change it in the Zihin console or with `update_agent`. They will be dropped from the SDK in 1.0.",
|
|
1139
|
+
warn
|
|
1134
1140
|
);
|
|
1135
1141
|
}
|
|
1136
1142
|
}
|
|
@@ -1303,7 +1309,7 @@ function checkInputCapability(params, chunk) {
|
|
|
1303
1309
|
var TURN_EVENTS = /* @__PURE__ */ new Set(["phase", "thinking", "tool", "action_proposed", "action_forwarded", "token", "response", "metrics", "done"]);
|
|
1304
1310
|
async function* streamAgent(cfg, rawParams) {
|
|
1305
1311
|
const params = snapshotStructuredInput(rawParams);
|
|
1306
|
-
validateParams(params);
|
|
1312
|
+
validateParams(params, cfg.runtime.warn);
|
|
1307
1313
|
const callerTimeoutMs = params.options?.timeoutMs !== void 0 ? normalizeTimeoutMs(params.options.timeoutMs, "options.timeoutMs") : void 0;
|
|
1308
1314
|
const timeoutMs = callerTimeoutMs !== void 0 ? callerTimeoutMs + DEADLINE_GRACE_MS : resolveTimeout(cfg, "stream");
|
|
1309
1315
|
const reportedMs = callerTimeoutMs ?? timeoutMs;
|
|
@@ -1319,6 +1325,7 @@ async function* streamAgent(cfg, rawParams) {
|
|
|
1319
1325
|
maxRetries: cfg.maxRetries,
|
|
1320
1326
|
callerSignal: params.signal,
|
|
1321
1327
|
fetchImpl: cfg.fetchImpl,
|
|
1328
|
+
runtime: cfg.runtime,
|
|
1322
1329
|
onRetry: cfg.onRetry,
|
|
1323
1330
|
// Retry do POST só ANTES dos headers chegarem. O que o torna seguro é a ordem do BE: o SSE faz
|
|
1324
1331
|
// `flushHeaders()` antes de reservar o id e antes de qualquer trabalho do turno, então falhar
|
|
@@ -1868,6 +1875,7 @@ async function dispatchAgent(cfg, params) {
|
|
|
1868
1875
|
// S1: nunca duplica disparo por erro de rede
|
|
1869
1876
|
callerSignal: params.signal,
|
|
1870
1877
|
fetchImpl: cfg.fetchImpl,
|
|
1878
|
+
runtime: cfg.runtime,
|
|
1871
1879
|
onRetry: cfg.onRetry
|
|
1872
1880
|
});
|
|
1873
1881
|
if (!res.ok) {
|
|
@@ -2051,6 +2059,7 @@ async function deleteTrigger(cfg, params) {
|
|
|
2051
2059
|
// DELETE é idempotente
|
|
2052
2060
|
callerSignal: params.signal,
|
|
2053
2061
|
fetchImpl: cfg.fetchImpl,
|
|
2062
|
+
runtime: cfg.runtime,
|
|
2054
2063
|
onRetry: cfg.onRetry
|
|
2055
2064
|
});
|
|
2056
2065
|
if (!res.ok) {
|
|
@@ -2193,9 +2202,38 @@ var ZihinAgentClient = class {
|
|
|
2193
2202
|
return this.guard(listAgents(this.cfg, opts));
|
|
2194
2203
|
}
|
|
2195
2204
|
};
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2205
|
+
|
|
2206
|
+
// src/runtime-node.ts
|
|
2207
|
+
var defaultRuntime = {
|
|
2208
|
+
sleep: (ms, signal) => new Promise((resolve, reject) => {
|
|
2209
|
+
if (signal?.aborted) {
|
|
2210
|
+
reject(new Error("aborted"));
|
|
2211
|
+
return;
|
|
2212
|
+
}
|
|
2213
|
+
const onAbort = () => {
|
|
2214
|
+
clearTimeout(timer);
|
|
2215
|
+
reject(new Error("aborted"));
|
|
2216
|
+
};
|
|
2217
|
+
const timer = setTimeout(() => {
|
|
2218
|
+
signal?.removeEventListener("abort", onAbort);
|
|
2219
|
+
resolve();
|
|
2220
|
+
}, ms);
|
|
2221
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
2222
|
+
}),
|
|
2223
|
+
/**
|
|
2224
|
+
* Tipo PRÓPRIO (`ZihinDeprecationWarning`), não `DeprecationWarning`: com `--throw-deprecation`
|
|
2225
|
+
* o Node transformaria o aviso em exceção e quebraria a chamada de quem só precisava ser avisado.
|
|
2226
|
+
* Fora do Node (browser/edge), `console.warn`.
|
|
2227
|
+
*/
|
|
2228
|
+
warn: (code, message) => {
|
|
2229
|
+
const proc = globalThis.process;
|
|
2230
|
+
if (typeof proc?.emitWarning === "function") {
|
|
2231
|
+
proc.emitWarning(message, { type: "ZihinDeprecationWarning", code });
|
|
2232
|
+
return;
|
|
2233
|
+
}
|
|
2234
|
+
console.warn(`[${code}] ${message}`);
|
|
2235
|
+
}
|
|
2236
|
+
};
|
|
2199
2237
|
|
|
2200
2238
|
// src/signing.ts
|
|
2201
2239
|
function loadNodeCrypto() {
|
|
@@ -2224,6 +2262,16 @@ function signRequest(payload, secretKey, timestamp) {
|
|
|
2224
2262
|
"X-Zihin-Nonce": nonce
|
|
2225
2263
|
};
|
|
2226
2264
|
}
|
|
2265
|
+
|
|
2266
|
+
// src/index.ts
|
|
2267
|
+
var ZihinAgentClient2 = class extends ZihinAgentClient {
|
|
2268
|
+
constructor(config) {
|
|
2269
|
+
super({ ...config, runtime: config.runtime ?? defaultRuntime });
|
|
2270
|
+
}
|
|
2271
|
+
};
|
|
2272
|
+
function createClient2(config) {
|
|
2273
|
+
return new ZihinAgentClient2(config);
|
|
2274
|
+
}
|
|
2227
2275
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2228
2276
|
0 && (module.exports = {
|
|
2229
2277
|
DEADLINE_GRACE_MS,
|