stitchkit 0.8.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +3 -7
- package/dist/contract/index.js +0 -1
- package/dist/{index-za2p453b.js → index-031q8xmx.js} +1 -1
- package/dist/{index-5qe31283.js → index-9zrq8x5z.js} +9 -6
- package/dist/{index-tr7r1530.js → index-f39j6twc.js} +4 -8
- package/dist/index-jgpsd7dy.js +105 -0
- package/dist/{index-mwmpw6j1.js → index-p9m9c0jw.js} +2 -4
- package/dist/{index-5789sbt8.js → index-svvpy5bz.js} +8 -16
- package/dist/index-tm7dqzxc.js +20 -0
- package/dist/index.js +429 -13
- package/dist/node.js +4 -8
- package/dist/observability/index.js +4 -7
- package/dist/react.js +0 -2
- package/dist/server/index.js +89 -34
- package/dist/tools/mcp-app.d.ts.map +1 -1
- package/dist/tools.js +253 -21
- package/package.json +5 -3
- package/dist/index-37x76zdn.js +0 -4
- package/dist/index-48ffdxgk.js +0 -6
- package/dist/index-809wc1tt.js +0 -18
- package/dist/index-a1702zj4.js +0 -72
- package/dist/index-kdkvp26v.js +0 -380
- package/dist/index-kzfs85xp.js +0 -9
- package/dist/index-wjwj5bz2.js +0 -37
- package/dist/index-x3fcszf8.js +0 -8
package/dist/tools.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
inputIsQuery,
|
|
3
|
+
signJwt,
|
|
4
|
+
verifyPkce
|
|
5
|
+
} from "./index-9zrq8x5z.js";
|
|
5
6
|
import {
|
|
6
7
|
coerceJsonArgs,
|
|
7
8
|
collectTools,
|
|
@@ -13,27 +14,18 @@ import {
|
|
|
13
14
|
pollUntil,
|
|
14
15
|
readCapped,
|
|
15
16
|
toolResultFromError
|
|
16
|
-
} from "./index-
|
|
17
|
-
import {
|
|
18
|
-
signJwt,
|
|
19
|
-
verifyPkce
|
|
20
|
-
} from "./index-5qe31283.js";
|
|
21
|
-
import"./index-48ffdxgk.js";
|
|
17
|
+
} from "./index-f39j6twc.js";
|
|
22
18
|
import {
|
|
23
19
|
toJsonSchema
|
|
24
20
|
} from "./index-0ed3bx43.js";
|
|
25
21
|
import {
|
|
22
|
+
AppError,
|
|
26
23
|
isWithinDir
|
|
27
|
-
} from "./index-
|
|
28
|
-
import"./index-wjwj5bz2.js";
|
|
29
|
-
import {
|
|
30
|
-
AppError
|
|
31
|
-
} from "./index-eq29zkrx.js";
|
|
32
|
-
import"./index-kzfs85xp.js";
|
|
24
|
+
} from "./index-jgpsd7dy.js";
|
|
33
25
|
import {
|
|
34
|
-
isRecord
|
|
35
|
-
|
|
36
|
-
|
|
26
|
+
isRecord,
|
|
27
|
+
typedEntries
|
|
28
|
+
} from "./index-tm7dqzxc.js";
|
|
37
29
|
|
|
38
30
|
// src/tools/agent.ts
|
|
39
31
|
import { tool, zodSchema } from "ai";
|
|
@@ -94,16 +86,15 @@ import { z } from "zod";
|
|
|
94
86
|
|
|
95
87
|
// src/tools/mcp-app.ts
|
|
96
88
|
import { readFileSync } from "node:fs";
|
|
97
|
-
import {
|
|
89
|
+
import { fileURLToPath } from "node:url";
|
|
98
90
|
var RESOURCE_MIME_TYPE = "text/html;profile=mcp-app";
|
|
99
91
|
var EXT_APPS_BUNDLE_PLACEHOLDER = "/*__EXT_APPS_BUNDLE__*/";
|
|
100
|
-
var require2 = createRequire(import.meta.url);
|
|
101
92
|
function inlineMcpAppBundle(html) {
|
|
102
93
|
if (!html.includes(EXT_APPS_BUNDLE_PLACEHOLDER))
|
|
103
94
|
return html;
|
|
104
95
|
let bundlePath;
|
|
105
96
|
try {
|
|
106
|
-
bundlePath =
|
|
97
|
+
bundlePath = fileURLToPath(import.meta.resolve("@modelcontextprotocol/ext-apps/app-with-deps"));
|
|
107
98
|
} catch {
|
|
108
99
|
throw new Error("[stitchkit] inlineMcpAppBundle: '@modelcontextprotocol/ext-apps' is not installed. " + "Add it as a dependency to serve MCP App widgets.");
|
|
109
100
|
}
|
|
@@ -836,6 +827,247 @@ function mountOAuthProvider(config) {
|
|
|
836
827
|
};
|
|
837
828
|
return [metadataRoute, registerRoute, authorizeRoute, tokenRoute];
|
|
838
829
|
}
|
|
830
|
+
// src/browser/http.ts
|
|
831
|
+
import ky, { isHTTPError } from "ky";
|
|
832
|
+
class ApiError extends Error {
|
|
833
|
+
code;
|
|
834
|
+
status;
|
|
835
|
+
details;
|
|
836
|
+
hint;
|
|
837
|
+
constructor(code, status = 0, details, message, hint) {
|
|
838
|
+
super(message ?? `API Error: ${code}`);
|
|
839
|
+
this.code = code;
|
|
840
|
+
this.status = status;
|
|
841
|
+
this.details = details;
|
|
842
|
+
this.hint = hint;
|
|
843
|
+
this.name = "ApiError";
|
|
844
|
+
}
|
|
845
|
+
static is(error) {
|
|
846
|
+
return error instanceof ApiError;
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
function parseApiErrorBody(body) {
|
|
850
|
+
if (!isRecord(body) || !isRecord(body.error))
|
|
851
|
+
return null;
|
|
852
|
+
const error = body.error;
|
|
853
|
+
if (typeof error.code !== "string")
|
|
854
|
+
return null;
|
|
855
|
+
return {
|
|
856
|
+
code: error.code,
|
|
857
|
+
message: typeof error.message === "string" ? error.message : undefined,
|
|
858
|
+
details: error.details,
|
|
859
|
+
hint: typeof error.hint === "string" ? error.hint : undefined
|
|
860
|
+
};
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
// src/browser/client.ts
|
|
864
|
+
function withTimeout(options, timeout) {
|
|
865
|
+
if (timeout === undefined)
|
|
866
|
+
return options;
|
|
867
|
+
return { ...options, timeout };
|
|
868
|
+
}
|
|
869
|
+
function isParamArray(value) {
|
|
870
|
+
return Array.isArray(value) && value.every((v) => typeof v === "string" || typeof v === "number");
|
|
871
|
+
}
|
|
872
|
+
function collectQueryParams(args, skipKeys) {
|
|
873
|
+
const params = {};
|
|
874
|
+
let hasParams = false;
|
|
875
|
+
for (const [key, value] of Object.entries(args)) {
|
|
876
|
+
if (skipKeys.has(key) || value === undefined || value === null)
|
|
877
|
+
continue;
|
|
878
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean" || isParamArray(value)) {
|
|
879
|
+
params[key] = value;
|
|
880
|
+
hasParams = true;
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
return hasParams ? params : undefined;
|
|
884
|
+
}
|
|
885
|
+
function createClient(contract, configOrClient, contractConfig) {
|
|
886
|
+
const client = {};
|
|
887
|
+
const makeMethod = isHttpAdapter(configOrClient) ? (endpoint) => createHttpMethod(endpoint, contract.meta.prefix, configOrClient, contractConfig) : (endpoint) => createFetchMethod(endpoint, contract.meta.prefix, configOrClient, contractConfig);
|
|
888
|
+
for (const [key, endpoint] of typedEntries(contract.endpoints)) {
|
|
889
|
+
if (endpoint.expose && !endpoint.expose.includes("HTTP"))
|
|
890
|
+
continue;
|
|
891
|
+
setClientMethod(client, key, makeMethod(endpoint));
|
|
892
|
+
}
|
|
893
|
+
return client;
|
|
894
|
+
}
|
|
895
|
+
function isHttpAdapter(value) {
|
|
896
|
+
return typeof value === "object" && "get" in value && typeof value.get === "function";
|
|
897
|
+
}
|
|
898
|
+
function setClientMethod(target, key, method) {
|
|
899
|
+
target[key] = method;
|
|
900
|
+
}
|
|
901
|
+
function createHttpMethod(endpoint, prefix, client, config) {
|
|
902
|
+
const httpMethod = endpoint.method.toLowerCase();
|
|
903
|
+
const isGet = httpMethod === "get";
|
|
904
|
+
const paramNames = extractParamNames(endpoint.path);
|
|
905
|
+
const prefixKeys = new Set([...config?.stripPrefixKeys ?? [], ...paramNames]);
|
|
906
|
+
return (...args) => {
|
|
907
|
+
const firstArg = args[0] ?? {};
|
|
908
|
+
let pathPrefixStr = "";
|
|
909
|
+
if (config?.pathPrefix) {
|
|
910
|
+
pathPrefixStr = typeof config.pathPrefix === "function" ? config.pathPrefix(firstArg) : config.pathPrefix;
|
|
911
|
+
if (pathPrefixStr && !pathPrefixStr.endsWith("/"))
|
|
912
|
+
pathPrefixStr += "/";
|
|
913
|
+
}
|
|
914
|
+
let url = `${pathPrefixStr}${prefix}${endpoint.path}`;
|
|
915
|
+
for (const name of paramNames) {
|
|
916
|
+
const value = firstArg[name];
|
|
917
|
+
if (value === undefined || value === null) {
|
|
918
|
+
throw new Error(`Missing path param: ${name}`);
|
|
919
|
+
}
|
|
920
|
+
url = url.replace(`:${name}`, encodeURIComponent(String(value)));
|
|
921
|
+
}
|
|
922
|
+
if (url.endsWith("/"))
|
|
923
|
+
url = url.slice(0, -1);
|
|
924
|
+
if (endpoint.multipart) {
|
|
925
|
+
const file = firstArg[endpoint.multipart];
|
|
926
|
+
if (!(file instanceof Blob)) {
|
|
927
|
+
throw new Error(`Missing multipart file field: ${endpoint.multipart}`);
|
|
928
|
+
}
|
|
929
|
+
const formData = new FormData;
|
|
930
|
+
formData.append(endpoint.multipart, file);
|
|
931
|
+
appendFormFields(formData, firstArg, new Set([...prefixKeys, endpoint.multipart]));
|
|
932
|
+
return client.post(url, formData, withTimeout(undefined, endpoint.timeout));
|
|
933
|
+
}
|
|
934
|
+
if (isGet) {
|
|
935
|
+
const params = collectQueryParams(firstArg, prefixKeys);
|
|
936
|
+
return client.get(url, withTimeout(params ? { params } : undefined, endpoint.timeout));
|
|
937
|
+
}
|
|
938
|
+
if (httpMethod === "delete") {
|
|
939
|
+
const params = collectQueryParams(firstArg, prefixKeys);
|
|
940
|
+
return client.delete(url, withTimeout(params ? { params } : undefined, endpoint.timeout));
|
|
941
|
+
}
|
|
942
|
+
const payload = {};
|
|
943
|
+
for (const [key, value] of Object.entries(firstArg)) {
|
|
944
|
+
if (!prefixKeys.has(key) && value !== undefined) {
|
|
945
|
+
payload[key] = value;
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
return client[httpMethod](url, Object.keys(payload).length > 0 ? payload : undefined, withTimeout(undefined, endpoint.timeout));
|
|
949
|
+
};
|
|
950
|
+
}
|
|
951
|
+
function createFetchMethod(endpoint, prefix, config, contractConfig) {
|
|
952
|
+
const prefixKeys = new Set(contractConfig?.stripPrefixKeys ?? []);
|
|
953
|
+
return async (args) => {
|
|
954
|
+
let pathPrefixStr = "";
|
|
955
|
+
if (contractConfig?.pathPrefix) {
|
|
956
|
+
pathPrefixStr = typeof contractConfig.pathPrefix === "function" ? contractConfig.pathPrefix(args ?? {}) : contractConfig.pathPrefix;
|
|
957
|
+
if (pathPrefixStr && !pathPrefixStr.endsWith("/"))
|
|
958
|
+
pathPrefixStr += "/";
|
|
959
|
+
}
|
|
960
|
+
let url = buildFetchUrl(config.baseUrl, prefix, endpoint.path, args, pathPrefixStr);
|
|
961
|
+
const headers = {
|
|
962
|
+
Accept: "application/json",
|
|
963
|
+
...typeof config.headers === "function" ? config.headers() : config.headers
|
|
964
|
+
};
|
|
965
|
+
const isQuery = inputIsQuery(endpoint.method);
|
|
966
|
+
const hasBody = !isQuery && !endpoint.multipart && endpoint.input && args;
|
|
967
|
+
if (isQuery && args) {
|
|
968
|
+
const remaining = stripParams(args, endpoint.path, prefixKeys);
|
|
969
|
+
const searchParams = new URLSearchParams;
|
|
970
|
+
for (const [k, v] of Object.entries(remaining)) {
|
|
971
|
+
if (v === undefined || v === null)
|
|
972
|
+
continue;
|
|
973
|
+
if (isParamArray(v)) {
|
|
974
|
+
for (const item of v)
|
|
975
|
+
searchParams.append(k, String(item));
|
|
976
|
+
} else if (typeof v !== "object") {
|
|
977
|
+
searchParams.set(k, String(v));
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
if (searchParams.size > 0)
|
|
981
|
+
url += `?${searchParams}`;
|
|
982
|
+
}
|
|
983
|
+
if (hasBody)
|
|
984
|
+
headers["Content-Type"] = "application/json";
|
|
985
|
+
if (endpoint.multipart && args) {
|
|
986
|
+
const file = args[endpoint.multipart];
|
|
987
|
+
if (!(file instanceof Blob)) {
|
|
988
|
+
throw new Error(`Missing multipart file field: ${endpoint.multipart}`);
|
|
989
|
+
}
|
|
990
|
+
const formData = new FormData;
|
|
991
|
+
formData.append(endpoint.multipart, file);
|
|
992
|
+
appendFormFields(formData, stripParams(args, endpoint.path, prefixKeys), new Set([endpoint.multipart]));
|
|
993
|
+
const res2 = await fetch(url, {
|
|
994
|
+
method: endpoint.method,
|
|
995
|
+
headers,
|
|
996
|
+
credentials: config.credentials,
|
|
997
|
+
body: formData
|
|
998
|
+
});
|
|
999
|
+
if (!res2.ok) {
|
|
1000
|
+
await throwForErrorResponse(res2, config, null);
|
|
1001
|
+
}
|
|
1002
|
+
if (res2.status === 204)
|
|
1003
|
+
return;
|
|
1004
|
+
const json3 = await res2.json();
|
|
1005
|
+
return endpoint.output ? endpoint.output.parse(json3) : json3;
|
|
1006
|
+
}
|
|
1007
|
+
const res = await fetch(url, {
|
|
1008
|
+
method: endpoint.method,
|
|
1009
|
+
headers,
|
|
1010
|
+
credentials: config.credentials,
|
|
1011
|
+
...hasBody && {
|
|
1012
|
+
body: JSON.stringify(stripParams(hasBody, endpoint.path, prefixKeys))
|
|
1013
|
+
}
|
|
1014
|
+
});
|
|
1015
|
+
if (!res.ok) {
|
|
1016
|
+
await throwForErrorResponse(res, config, { error: res.statusText });
|
|
1017
|
+
}
|
|
1018
|
+
if (res.status === 204)
|
|
1019
|
+
return;
|
|
1020
|
+
const json2 = await res.json();
|
|
1021
|
+
return endpoint.output ? endpoint.output.parse(json2) : json2;
|
|
1022
|
+
};
|
|
1023
|
+
}
|
|
1024
|
+
function appendFormFields(formData, values, skipKeys) {
|
|
1025
|
+
for (const [key, value] of Object.entries(values)) {
|
|
1026
|
+
if (skipKeys.has(key) || value === undefined || value === null)
|
|
1027
|
+
continue;
|
|
1028
|
+
formData.append(key, typeof value === "string" ? value : JSON.stringify(value));
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
async function throwForErrorResponse(res, config, fallbackBody) {
|
|
1032
|
+
const body = await res.json().catch(() => fallbackBody);
|
|
1033
|
+
config.onError?.(res.status, body);
|
|
1034
|
+
const parsed = parseApiErrorBody(body);
|
|
1035
|
+
if (parsed) {
|
|
1036
|
+
throw new ApiError(parsed.code, res.status, parsed.details, parsed.message, parsed.hint);
|
|
1037
|
+
}
|
|
1038
|
+
throw new ApiError("HTTP_ERROR", res.status, { body });
|
|
1039
|
+
}
|
|
1040
|
+
function extractParamNames(path) {
|
|
1041
|
+
const matches = path.match(/:(\w+)/g);
|
|
1042
|
+
return matches ? matches.map((m) => m.slice(1)) : [];
|
|
1043
|
+
}
|
|
1044
|
+
function buildFetchUrl(baseUrl, prefix, path, args, pathPrefix = "") {
|
|
1045
|
+
let fullPath = `/${pathPrefix}${prefix}${path === "/" ? "" : path}`;
|
|
1046
|
+
if (args) {
|
|
1047
|
+
fullPath = fullPath.replace(/:(\w+)/g, (_, key) => {
|
|
1048
|
+
const val = args[key];
|
|
1049
|
+
if (val === undefined || val === null) {
|
|
1050
|
+
throw new Error(`Missing path param: ${key}`);
|
|
1051
|
+
}
|
|
1052
|
+
return encodeURIComponent(String(val));
|
|
1053
|
+
});
|
|
1054
|
+
}
|
|
1055
|
+
return `${baseUrl}${fullPath}`;
|
|
1056
|
+
}
|
|
1057
|
+
function stripParams(args, path, extra) {
|
|
1058
|
+
const skip = new Set(extra);
|
|
1059
|
+
for (const match of path.matchAll(/:(\w+)/g)) {
|
|
1060
|
+
if (match[1])
|
|
1061
|
+
skip.add(match[1]);
|
|
1062
|
+
}
|
|
1063
|
+
const result = {};
|
|
1064
|
+
for (const [k, v] of Object.entries(args)) {
|
|
1065
|
+
if (!skip.has(k))
|
|
1066
|
+
result[k] = v;
|
|
1067
|
+
}
|
|
1068
|
+
return result;
|
|
1069
|
+
}
|
|
1070
|
+
|
|
839
1071
|
// src/tools/remote.ts
|
|
840
1072
|
function toArgs(ctx) {
|
|
841
1073
|
const { params, input } = ctx;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "stitchkit",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "Contract-first backend framework — one defineContract() into an HTTP API, MCP tools, AI-agent tools and a typed client. Bun and Node.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"bun",
|
|
@@ -82,9 +82,11 @@
|
|
|
82
82
|
},
|
|
83
83
|
"scripts": {
|
|
84
84
|
"check": "bun x tsc --noEmit",
|
|
85
|
-
"build:
|
|
85
|
+
"build:browser": "bun build src/index.ts src/react.ts src/contract/index.ts --outdir dist --target node --packages external --splitting --root src",
|
|
86
|
+
"build:server": "bun build src/server/index.ts src/node.ts src/tools.ts src/cli.ts src/observability/index.ts --outdir dist --target node --packages external --splitting --root src",
|
|
87
|
+
"build:js": "bun run build:browser && bun run build:server",
|
|
86
88
|
"build:types": "bun x tsc -p tsconfig.build.json --emitDeclarationOnly",
|
|
87
|
-
"build": "rm -rf dist && bun run build:js && bun run build:types",
|
|
89
|
+
"build": "rm -rf dist && bun run build:js && bun run build:types && bun scripts/check-browser-clean.mjs",
|
|
88
90
|
"dev": "bun run build:js -- --watch",
|
|
89
91
|
"prepublishOnly": "cp ../../README.md ./README.md && bun ../../scripts/gen-llms.ts && bun run build",
|
|
90
92
|
"test": "bun test",
|
package/dist/index-37x76zdn.js
DELETED
package/dist/index-48ffdxgk.js
DELETED
package/dist/index-809wc1tt.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
// src/internal/typed.ts
|
|
2
|
-
function typedEntries(value) {
|
|
3
|
-
return Object.entries(value);
|
|
4
|
-
}
|
|
5
|
-
function isRecord(value) {
|
|
6
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
7
|
-
}
|
|
8
|
-
function mapObject(source, mapper) {
|
|
9
|
-
const result = {};
|
|
10
|
-
for (const [key, value] of typedEntries(source)) {
|
|
11
|
-
const mapped = mapper(key, value);
|
|
12
|
-
if (mapped !== undefined)
|
|
13
|
-
result[key] = mapped;
|
|
14
|
-
}
|
|
15
|
-
return result;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export { typedEntries, isRecord, mapObject };
|
package/dist/index-a1702zj4.js
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
normalizeError
|
|
3
|
-
} from "./index-wjwj5bz2.js";
|
|
4
|
-
|
|
5
|
-
// src/server/stream.ts
|
|
6
|
-
function streamSSE(generator) {
|
|
7
|
-
const encoder = new TextEncoder;
|
|
8
|
-
const stream = new ReadableStream({
|
|
9
|
-
async start(controller) {
|
|
10
|
-
try {
|
|
11
|
-
for await (const chunk of generator) {
|
|
12
|
-
const data = JSON.stringify(chunk);
|
|
13
|
-
controller.enqueue(encoder.encode(`data: ${data}
|
|
14
|
-
|
|
15
|
-
`));
|
|
16
|
-
}
|
|
17
|
-
controller.enqueue(encoder.encode(`data: [DONE]
|
|
18
|
-
|
|
19
|
-
`));
|
|
20
|
-
controller.close();
|
|
21
|
-
} catch (err) {
|
|
22
|
-
const envelope = normalizeError(err).toJSON();
|
|
23
|
-
controller.enqueue(encoder.encode(`data: ${JSON.stringify(envelope)}
|
|
24
|
-
|
|
25
|
-
`));
|
|
26
|
-
controller.close();
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
return new Response(stream, {
|
|
31
|
-
headers: {
|
|
32
|
-
"Content-Type": "text/event-stream",
|
|
33
|
-
"Cache-Control": "no-cache",
|
|
34
|
-
Connection: "keep-alive"
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
async function* parseSSE(response, options) {
|
|
39
|
-
const reader = response.body?.getReader();
|
|
40
|
-
if (!reader)
|
|
41
|
-
return;
|
|
42
|
-
const decoder = new TextDecoder;
|
|
43
|
-
let buffer = "";
|
|
44
|
-
try {
|
|
45
|
-
while (true) {
|
|
46
|
-
const { done, value } = await reader.read();
|
|
47
|
-
if (done)
|
|
48
|
-
break;
|
|
49
|
-
buffer += decoder.decode(value, { stream: true });
|
|
50
|
-
const lines = buffer.split(`
|
|
51
|
-
`);
|
|
52
|
-
buffer = lines.pop() ?? "";
|
|
53
|
-
for (const rawLine of lines) {
|
|
54
|
-
const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine;
|
|
55
|
-
if (!line.startsWith("data:"))
|
|
56
|
-
continue;
|
|
57
|
-
const data = line.slice(5).replace(/^ /, "");
|
|
58
|
-
if (data === "[DONE]")
|
|
59
|
-
return;
|
|
60
|
-
try {
|
|
61
|
-
yield JSON.parse(data);
|
|
62
|
-
} catch (err) {
|
|
63
|
-
options?.onParseError?.(data, err instanceof Error ? err : new Error(String(err)));
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
} finally {
|
|
68
|
-
reader.releaseLock();
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export { streamSSE, parseSSE };
|