@nemesis-oss/ollama-sdk 1.25.0 → 1.25.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{anthropic-DTQ01pAS.d.cts → anthropic-DA9flTv_.d.ts} +4 -1
- package/dist/{anthropic-DdaNgdG_.d.ts → anthropic-DnsaK9Ui.d.cts} +4 -1
- package/dist/generated-api.d.cts +4 -4
- package/dist/generated-api.d.ts +4 -4
- package/dist/generated-runtime.cjs +14 -16
- package/dist/generated-runtime.cjs.map +1 -1
- package/dist/generated-runtime.d.cts +1 -1
- package/dist/generated-runtime.d.ts +1 -1
- package/dist/generated-runtime.js +14 -16
- package/dist/generated-runtime.js.map +1 -1
- package/dist/index.cjs +106 -61
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +106 -61
- package/dist/index.js.map +1 -1
- package/dist/mcp-generated.d.cts +1 -1
- package/dist/mcp-generated.d.ts +1 -1
- package/dist/{runtime-CUzw4GAE.d.cts → runtime-BoTPAvJH.d.cts} +9 -3
- package/dist/{runtime-D_rMXIVT.d.ts → runtime-DQRudJFP.d.ts} +9 -3
- package/package.json +10 -10
package/dist/index.cjs
CHANGED
|
@@ -1354,34 +1354,32 @@ async function* parseSseStream(stream) {
|
|
|
1354
1354
|
}
|
|
1355
1355
|
return void 0;
|
|
1356
1356
|
};
|
|
1357
|
-
|
|
1358
|
-
while (true) {
|
|
1359
|
-
const { done, value } = await reader.read();
|
|
1360
|
-
if (done) break;
|
|
1361
|
-
buffer += decoder.decode(value, { stream: true });
|
|
1357
|
+
function* drainLines(endOfStream) {
|
|
1362
1358
|
while (true) {
|
|
1363
1359
|
const lf = buffer.indexOf("\n");
|
|
1364
1360
|
const cr = buffer.indexOf("\r");
|
|
1365
|
-
if (lf === -1 && cr === -1)
|
|
1366
|
-
if (
|
|
1361
|
+
if (lf === -1 && cr === -1) return;
|
|
1362
|
+
if (!endOfStream && lf === -1 && cr === buffer.length - 1) return;
|
|
1367
1363
|
const lineEnd = lf === -1 ? cr : cr === -1 ? lf : Math.min(lf, cr);
|
|
1368
|
-
|
|
1369
|
-
if (buffer[lineEnd] === "\r" && buffer[lineEnd + 1] === "\n") {
|
|
1370
|
-
separatorLength = 2;
|
|
1371
|
-
}
|
|
1364
|
+
const separatorLength = buffer[lineEnd] === "\r" && buffer[lineEnd + 1] === "\n" ? 2 : 1;
|
|
1372
1365
|
const line = buffer.slice(0, lineEnd);
|
|
1373
1366
|
buffer = buffer.slice(lineEnd + separatorLength);
|
|
1374
1367
|
const event = processLine(line);
|
|
1375
1368
|
if (event) yield event;
|
|
1376
1369
|
}
|
|
1377
1370
|
}
|
|
1371
|
+
const _reader = __using(_stack, disposableReader(reader), true);
|
|
1372
|
+
while (true) {
|
|
1373
|
+
const { done, value } = await reader.read();
|
|
1374
|
+
if (done) break;
|
|
1375
|
+
buffer += decoder.decode(value, { stream: true });
|
|
1376
|
+
yield* drainLines(false);
|
|
1377
|
+
}
|
|
1378
1378
|
buffer += decoder.decode();
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1379
|
+
yield* drainLines(true);
|
|
1380
|
+
if (data.length > 0 || buffer.length > 0) {
|
|
1381
|
+
throw new OllamaStreamError("SSE stream ended mid-event; the incomplete event was discarded");
|
|
1382
1382
|
}
|
|
1383
|
-
const terminal = dispatch(eventType, data, lastEventId, retry);
|
|
1384
|
-
if (terminal) yield terminal;
|
|
1385
1383
|
} catch (_) {
|
|
1386
1384
|
var _error = _, _hasError = true;
|
|
1387
1385
|
} finally {
|
|
@@ -1538,6 +1536,52 @@ function retryAfterHeaderFrom(response) {
|
|
|
1538
1536
|
return void 0;
|
|
1539
1537
|
}
|
|
1540
1538
|
}
|
|
1539
|
+
function parseJsonOrText(raw) {
|
|
1540
|
+
try {
|
|
1541
|
+
return JSON.parse(raw);
|
|
1542
|
+
} catch {
|
|
1543
|
+
return raw;
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
function isAbortLike(error) {
|
|
1547
|
+
return typeof error === "object" && error !== null && error.name === "AbortError";
|
|
1548
|
+
}
|
|
1549
|
+
async function readErrorBody(response) {
|
|
1550
|
+
try {
|
|
1551
|
+
if (typeof response.text === "function") {
|
|
1552
|
+
const raw = await response.text();
|
|
1553
|
+
return { raw, parsed: parseJsonOrText(raw) };
|
|
1554
|
+
}
|
|
1555
|
+
if (typeof response.json === "function") {
|
|
1556
|
+
const parsed = await response.json();
|
|
1557
|
+
return { raw: JSON.stringify(parsed) ?? "", parsed };
|
|
1558
|
+
}
|
|
1559
|
+
} catch (err) {
|
|
1560
|
+
if (isAbortLike(err)) throw err;
|
|
1561
|
+
}
|
|
1562
|
+
return { raw: "", parsed: "" };
|
|
1563
|
+
}
|
|
1564
|
+
function bridgeAbortSignal(signal) {
|
|
1565
|
+
const controller = new AbortController();
|
|
1566
|
+
if (signal === void 0) {
|
|
1567
|
+
return { controller, detach: () => void 0 };
|
|
1568
|
+
}
|
|
1569
|
+
if (signal.aborted) {
|
|
1570
|
+
controller.abort(signal.reason);
|
|
1571
|
+
return { controller, detach: () => void 0 };
|
|
1572
|
+
}
|
|
1573
|
+
const onAbort = () => controller.abort(signal.reason);
|
|
1574
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
1575
|
+
let attached = true;
|
|
1576
|
+
return {
|
|
1577
|
+
controller,
|
|
1578
|
+
detach: () => {
|
|
1579
|
+
if (!attached) return;
|
|
1580
|
+
attached = false;
|
|
1581
|
+
signal.removeEventListener("abort", onAbort);
|
|
1582
|
+
}
|
|
1583
|
+
};
|
|
1584
|
+
}
|
|
1541
1585
|
var HttpClient = class {
|
|
1542
1586
|
baseUrl;
|
|
1543
1587
|
apiKey;
|
|
@@ -1707,12 +1751,7 @@ var HttpClient = class {
|
|
|
1707
1751
|
});
|
|
1708
1752
|
span?.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, response.status);
|
|
1709
1753
|
if (!response.ok) {
|
|
1710
|
-
|
|
1711
|
-
try {
|
|
1712
|
-
errorBody = await response.json();
|
|
1713
|
-
} catch {
|
|
1714
|
-
errorBody = await response.text();
|
|
1715
|
-
}
|
|
1754
|
+
const { parsed: errorBody } = await readErrorBody(response);
|
|
1716
1755
|
const message = typeof errorBody === "object" && errorBody !== null && "error" in errorBody ? String(errorBody.error) : `HTTP ${response.status} ${response.statusText}`;
|
|
1717
1756
|
throw mapError(new Error(message), {
|
|
1718
1757
|
request: { method, url },
|
|
@@ -1760,29 +1799,18 @@ var HttpClient = class {
|
|
|
1760
1799
|
`${method} ${routeTemplate(options.path)}`,
|
|
1761
1800
|
httpSpanAttributes(method, url),
|
|
1762
1801
|
async (span) => {
|
|
1802
|
+
const bridge = bridgeAbortSignal(options.signal);
|
|
1763
1803
|
try {
|
|
1764
|
-
const controller = new AbortController();
|
|
1765
|
-
const removeAbortListener = options.signal !== void 0 ? (() => {
|
|
1766
|
-
if (options.signal.aborted) {
|
|
1767
|
-
controller.abort(options.signal.reason);
|
|
1768
|
-
} else {
|
|
1769
|
-
const onAbort = () => controller.abort(options.signal?.reason);
|
|
1770
|
-
options.signal.addEventListener("abort", onAbort, { once: true });
|
|
1771
|
-
return () => options.signal?.removeEventListener("abort", onAbort);
|
|
1772
|
-
}
|
|
1773
|
-
return void 0;
|
|
1774
|
-
})() : void 0;
|
|
1775
1804
|
const response = await this.fetchWithMiddleware({
|
|
1776
1805
|
url,
|
|
1777
1806
|
method,
|
|
1778
1807
|
headers,
|
|
1779
1808
|
...options.body !== void 0 ? { body: JSON.stringify(options.body) } : {},
|
|
1780
|
-
signal: controller.signal
|
|
1809
|
+
signal: bridge.controller.signal
|
|
1781
1810
|
});
|
|
1782
1811
|
span?.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, response.status);
|
|
1783
1812
|
if (!response.ok) {
|
|
1784
|
-
|
|
1785
|
-
const errorText = await response.text();
|
|
1813
|
+
const errorText = (await readErrorBody(response)).raw;
|
|
1786
1814
|
throw mapError(new Error(errorText || `HTTP ${response.status}`), {
|
|
1787
1815
|
request: { method, url },
|
|
1788
1816
|
response: {
|
|
@@ -1793,16 +1821,16 @@ var HttpClient = class {
|
|
|
1793
1821
|
});
|
|
1794
1822
|
}
|
|
1795
1823
|
if (!response.body) {
|
|
1796
|
-
removeAbortListener?.();
|
|
1797
1824
|
throw mapError(new Error("Response body is null, cannot stream SSE"), {
|
|
1798
1825
|
request: { method, url }
|
|
1799
1826
|
});
|
|
1800
1827
|
}
|
|
1828
|
+
const body = response.body;
|
|
1801
1829
|
const stream = (async function* () {
|
|
1802
1830
|
try {
|
|
1803
|
-
yield* parseSseStream(
|
|
1831
|
+
yield* parseSseStream(body);
|
|
1804
1832
|
} finally {
|
|
1805
|
-
|
|
1833
|
+
bridge.detach();
|
|
1806
1834
|
}
|
|
1807
1835
|
})();
|
|
1808
1836
|
return {
|
|
@@ -1810,11 +1838,12 @@ var HttpClient = class {
|
|
|
1810
1838
|
return stream[Symbol.asyncIterator]();
|
|
1811
1839
|
},
|
|
1812
1840
|
abort: () => {
|
|
1813
|
-
|
|
1814
|
-
controller.abort();
|
|
1841
|
+
bridge.detach();
|
|
1842
|
+
bridge.controller.abort();
|
|
1815
1843
|
}
|
|
1816
1844
|
};
|
|
1817
1845
|
} catch (err) {
|
|
1846
|
+
bridge.detach();
|
|
1818
1847
|
throw mapError(err, { request: { method, url } });
|
|
1819
1848
|
}
|
|
1820
1849
|
}
|
|
@@ -1828,29 +1857,18 @@ var HttpClient = class {
|
|
|
1828
1857
|
`${method} ${routeTemplate(options.path)}`,
|
|
1829
1858
|
httpSpanAttributes(method, url),
|
|
1830
1859
|
async (span) => {
|
|
1860
|
+
const bridge = bridgeAbortSignal(options.signal);
|
|
1831
1861
|
try {
|
|
1832
|
-
const controller = new AbortController();
|
|
1833
|
-
const removeAbortListener = options.signal !== void 0 ? (() => {
|
|
1834
|
-
if (options.signal.aborted) {
|
|
1835
|
-
controller.abort(options.signal.reason);
|
|
1836
|
-
} else {
|
|
1837
|
-
const onAbort = () => controller.abort(options.signal?.reason);
|
|
1838
|
-
options.signal.addEventListener("abort", onAbort, { once: true });
|
|
1839
|
-
return () => options.signal?.removeEventListener("abort", onAbort);
|
|
1840
|
-
}
|
|
1841
|
-
return void 0;
|
|
1842
|
-
})() : void 0;
|
|
1843
1862
|
const response = await this.fetchWithMiddleware({
|
|
1844
1863
|
url,
|
|
1845
1864
|
method,
|
|
1846
1865
|
headers,
|
|
1847
1866
|
...options.body !== void 0 ? { body: JSON.stringify(options.body) } : {},
|
|
1848
|
-
signal: controller.signal
|
|
1867
|
+
signal: bridge.controller.signal
|
|
1849
1868
|
});
|
|
1850
1869
|
span?.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, response.status);
|
|
1851
1870
|
if (!response.ok) {
|
|
1852
|
-
|
|
1853
|
-
const errorText = await response.text();
|
|
1871
|
+
const errorText = (await readErrorBody(response)).raw;
|
|
1854
1872
|
throw mapError(new Error(errorText || `HTTP ${response.status}`), {
|
|
1855
1873
|
request: { method, url },
|
|
1856
1874
|
response: {
|
|
@@ -1861,7 +1879,6 @@ var HttpClient = class {
|
|
|
1861
1879
|
});
|
|
1862
1880
|
}
|
|
1863
1881
|
if (!response.body) {
|
|
1864
|
-
removeAbortListener?.();
|
|
1865
1882
|
throw mapError(new Error("Response body is null, cannot stream"), {
|
|
1866
1883
|
request: { method, url }
|
|
1867
1884
|
});
|
|
@@ -1873,17 +1890,18 @@ var HttpClient = class {
|
|
|
1873
1890
|
try {
|
|
1874
1891
|
yield* stream;
|
|
1875
1892
|
} finally {
|
|
1876
|
-
|
|
1893
|
+
bridge.detach();
|
|
1877
1894
|
}
|
|
1878
1895
|
})()[Symbol.asyncIterator]();
|
|
1879
1896
|
},
|
|
1880
1897
|
abort: () => {
|
|
1881
|
-
|
|
1882
|
-
controller.abort();
|
|
1898
|
+
bridge.detach();
|
|
1899
|
+
bridge.controller.abort();
|
|
1883
1900
|
}
|
|
1884
1901
|
};
|
|
1885
1902
|
return abortable;
|
|
1886
1903
|
} catch (err) {
|
|
1904
|
+
bridge.detach();
|
|
1887
1905
|
throw mapError(err, { request: { method, url } });
|
|
1888
1906
|
}
|
|
1889
1907
|
}
|
|
@@ -3490,10 +3508,12 @@ var OpenAIChatCompletionStream = class {
|
|
|
3490
3508
|
resolveFinal;
|
|
3491
3509
|
rejectFinal;
|
|
3492
3510
|
removeAbortListener;
|
|
3511
|
+
abortRequested = false;
|
|
3493
3512
|
get finalResult() {
|
|
3494
3513
|
return this.finalResultPromise;
|
|
3495
3514
|
}
|
|
3496
3515
|
abort() {
|
|
3516
|
+
this.abortRequested = true;
|
|
3497
3517
|
this.removeAbortListener?.();
|
|
3498
3518
|
this.removeAbortListener = void 0;
|
|
3499
3519
|
this.source.abort?.();
|
|
@@ -3507,9 +3527,13 @@ var OpenAIChatCompletionStream = class {
|
|
|
3507
3527
|
let systemFingerprint;
|
|
3508
3528
|
let usage;
|
|
3509
3529
|
let completed = false;
|
|
3530
|
+
let sawDone = false;
|
|
3510
3531
|
try {
|
|
3511
3532
|
for await (const event of this.source) {
|
|
3512
|
-
if (event.data === "[DONE]")
|
|
3533
|
+
if (event.data === "[DONE]") {
|
|
3534
|
+
sawDone = true;
|
|
3535
|
+
break;
|
|
3536
|
+
}
|
|
3513
3537
|
let chunk;
|
|
3514
3538
|
try {
|
|
3515
3539
|
chunk = JSON.parse(event.data);
|
|
@@ -3582,6 +3606,9 @@ var OpenAIChatCompletionStream = class {
|
|
|
3582
3606
|
}
|
|
3583
3607
|
yield chunk;
|
|
3584
3608
|
}
|
|
3609
|
+
if (!sawDone) {
|
|
3610
|
+
throw this.abortRequested ? new OllamaAbortError("OpenAI Chat Completion stream aborted") : new OllamaStreamError("OpenAI Chat Completion stream ended without [DONE]");
|
|
3611
|
+
}
|
|
3585
3612
|
const response = {
|
|
3586
3613
|
id,
|
|
3587
3614
|
object: "chat.completion",
|
|
@@ -3635,10 +3662,12 @@ var OpenAICompletionStream = class {
|
|
|
3635
3662
|
resolveFinal;
|
|
3636
3663
|
rejectFinal;
|
|
3637
3664
|
removeAbortListener;
|
|
3665
|
+
abortRequested = false;
|
|
3638
3666
|
get finalResult() {
|
|
3639
3667
|
return this.finalResultPromise;
|
|
3640
3668
|
}
|
|
3641
3669
|
abort() {
|
|
3670
|
+
this.abortRequested = true;
|
|
3642
3671
|
this.removeAbortListener?.();
|
|
3643
3672
|
this.removeAbortListener = void 0;
|
|
3644
3673
|
this.source.abort?.();
|
|
@@ -3651,9 +3680,13 @@ var OpenAICompletionStream = class {
|
|
|
3651
3680
|
let created = 0;
|
|
3652
3681
|
let usage;
|
|
3653
3682
|
let completed = false;
|
|
3683
|
+
let sawDone = false;
|
|
3654
3684
|
try {
|
|
3655
3685
|
for await (const event of this.source) {
|
|
3656
|
-
if (event.data === "[DONE]")
|
|
3686
|
+
if (event.data === "[DONE]") {
|
|
3687
|
+
sawDone = true;
|
|
3688
|
+
break;
|
|
3689
|
+
}
|
|
3657
3690
|
const chunk = JSON.parse(event.data);
|
|
3658
3691
|
id = chunk.id || id;
|
|
3659
3692
|
model = chunk.model || model;
|
|
@@ -3670,6 +3703,9 @@ var OpenAICompletionStream = class {
|
|
|
3670
3703
|
}
|
|
3671
3704
|
yield chunk;
|
|
3672
3705
|
}
|
|
3706
|
+
if (!sawDone) {
|
|
3707
|
+
throw this.abortRequested ? new OllamaAbortError("OpenAI Completion stream aborted") : new OllamaStreamError("OpenAI Completion stream ended without [DONE]");
|
|
3708
|
+
}
|
|
3673
3709
|
const response = {
|
|
3674
3710
|
id,
|
|
3675
3711
|
object: "text_completion",
|
|
@@ -4204,10 +4240,12 @@ var AnthropicMessagesStream = class {
|
|
|
4204
4240
|
resolveFinal;
|
|
4205
4241
|
rejectFinal;
|
|
4206
4242
|
removeAbortListener;
|
|
4243
|
+
abortRequested = false;
|
|
4207
4244
|
get finalResult() {
|
|
4208
4245
|
return this.finalResultPromise;
|
|
4209
4246
|
}
|
|
4210
4247
|
abort() {
|
|
4248
|
+
this.abortRequested = true;
|
|
4211
4249
|
this.removeAbortListener?.();
|
|
4212
4250
|
this.removeAbortListener = void 0;
|
|
4213
4251
|
this.source.abort?.();
|
|
@@ -4218,6 +4256,7 @@ var AnthropicMessagesStream = class {
|
|
|
4218
4256
|
const blocks = /* @__PURE__ */ new Map();
|
|
4219
4257
|
const toolInputJson = /* @__PURE__ */ new Map();
|
|
4220
4258
|
let completed = false;
|
|
4259
|
+
let sawMessageStop = false;
|
|
4221
4260
|
try {
|
|
4222
4261
|
for await (const rawEvent of this.source) {
|
|
4223
4262
|
let event;
|
|
@@ -4233,6 +4272,9 @@ var AnthropicMessagesStream = class {
|
|
|
4233
4272
|
if (event.type === "error") {
|
|
4234
4273
|
throw new Error(event.error.message);
|
|
4235
4274
|
}
|
|
4275
|
+
if (event.type === "message_stop") {
|
|
4276
|
+
sawMessageStop = true;
|
|
4277
|
+
}
|
|
4236
4278
|
if (event.type === "message_start") {
|
|
4237
4279
|
message = {
|
|
4238
4280
|
...event.message,
|
|
@@ -4293,6 +4335,9 @@ var AnthropicMessagesStream = class {
|
|
|
4293
4335
|
if (!message) {
|
|
4294
4336
|
throw new Error("Anthropic Messages stream ended without message_start");
|
|
4295
4337
|
}
|
|
4338
|
+
if (!sawMessageStop) {
|
|
4339
|
+
throw this.abortRequested ? new OllamaAbortError("Anthropic Messages stream aborted") : new OllamaStreamError("Anthropic Messages stream ended without message_stop");
|
|
4340
|
+
}
|
|
4296
4341
|
const final = {
|
|
4297
4342
|
...message,
|
|
4298
4343
|
content: [...blocks.entries()].sort(([a], [b]) => a - b).map(([, block]) => block)
|