@ericsanchezok/synergy-util 2.4.4
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/binary.d.ts +7 -0
- package/dist/binary.js +40 -0
- package/dist/browser-protocol.d.ts +97 -0
- package/dist/browser-protocol.js +51 -0
- package/dist/capability.d.ts +289 -0
- package/dist/capability.js +841 -0
- package/dist/encode.d.ts +4 -0
- package/dist/encode.js +28 -0
- package/dist/error.d.ts +182 -0
- package/dist/error.js +43 -0
- package/dist/fn.d.ts +6 -0
- package/dist/fn.js +9 -0
- package/dist/identifier.d.ts +5 -0
- package/dist/identifier.js +41 -0
- package/dist/iife.d.ts +1 -0
- package/dist/iife.js +3 -0
- package/dist/json.d.ts +1 -0
- package/dist/json.js +209 -0
- package/dist/lazy.d.ts +1 -0
- package/dist/lazy.js +11 -0
- package/dist/model-limit.d.ts +37 -0
- package/dist/model-limit.js +35 -0
- package/dist/note-markdown.d.ts +24 -0
- package/dist/note-markdown.js +630 -0
- package/dist/path.d.ts +39 -0
- package/dist/path.js +84 -0
- package/dist/plugin-policy.d.ts +100 -0
- package/dist/plugin-policy.js +240 -0
- package/dist/retry.d.ts +8 -0
- package/dist/retry.js +33 -0
- package/package.json +22 -0
package/dist/encode.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function base64Encode(value: string): string;
|
|
2
|
+
export declare function base64Decode(value: string): string;
|
|
3
|
+
export declare function hash(content: string, algorithm?: string): Promise<string>;
|
|
4
|
+
export declare function checksum(content: string): string | undefined;
|
package/dist/encode.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export function base64Encode(value) {
|
|
2
|
+
const bytes = new TextEncoder().encode(value);
|
|
3
|
+
const binary = Array.from(bytes, (b) => String.fromCharCode(b)).join("");
|
|
4
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
5
|
+
}
|
|
6
|
+
export function base64Decode(value) {
|
|
7
|
+
const binary = atob(value.replace(/-/g, "+").replace(/_/g, "/"));
|
|
8
|
+
const bytes = Uint8Array.from(binary, (c) => c.charCodeAt(0));
|
|
9
|
+
return new TextDecoder().decode(bytes);
|
|
10
|
+
}
|
|
11
|
+
export async function hash(content, algorithm = "SHA-256") {
|
|
12
|
+
const encoder = new TextEncoder();
|
|
13
|
+
const data = encoder.encode(content);
|
|
14
|
+
const hashBuffer = await crypto.subtle.digest(algorithm, data);
|
|
15
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
16
|
+
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
17
|
+
return hashHex;
|
|
18
|
+
}
|
|
19
|
+
export function checksum(content) {
|
|
20
|
+
if (!content)
|
|
21
|
+
return undefined;
|
|
22
|
+
let hash = 0x811c9dc5;
|
|
23
|
+
for (let i = 0; i < content.length; i++) {
|
|
24
|
+
hash ^= content.charCodeAt(i);
|
|
25
|
+
hash = Math.imul(hash, 0x01000193);
|
|
26
|
+
}
|
|
27
|
+
return (hash >>> 0).toString(36);
|
|
28
|
+
}
|
package/dist/error.d.ts
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import z from "zod";
|
|
2
|
+
export declare abstract class NamedError extends Error {
|
|
3
|
+
abstract schema(): z.core.$ZodType;
|
|
4
|
+
abstract toObject(): {
|
|
5
|
+
name: string;
|
|
6
|
+
data: any;
|
|
7
|
+
};
|
|
8
|
+
static create<Name extends string, Data extends z.core.$ZodType>(name: Name, data: Data): {
|
|
9
|
+
new (data: z.input<Data>, options?: ErrorOptions): {
|
|
10
|
+
readonly name: Name;
|
|
11
|
+
readonly data: z.core.input<Data>;
|
|
12
|
+
schema(): z.ZodObject<{
|
|
13
|
+
name: z.ZodLiteral<Name>;
|
|
14
|
+
data: Data;
|
|
15
|
+
}, z.core.$strip>;
|
|
16
|
+
toObject(): {
|
|
17
|
+
name: Name;
|
|
18
|
+
data: z.core.input<Data>;
|
|
19
|
+
};
|
|
20
|
+
message: string;
|
|
21
|
+
stack?: string;
|
|
22
|
+
cause?: unknown;
|
|
23
|
+
};
|
|
24
|
+
readonly Schema: z.ZodObject<{
|
|
25
|
+
name: z.ZodLiteral<Name>;
|
|
26
|
+
data: Data;
|
|
27
|
+
}, z.core.$strip>;
|
|
28
|
+
isInstance(input: any): input is InstanceType</*elided*/ any>;
|
|
29
|
+
create<Name extends string, Data extends z.core.$ZodType>(name: Name, data: Data): /*elided*/ any;
|
|
30
|
+
readonly Unknown: {
|
|
31
|
+
new (data: {
|
|
32
|
+
message: string;
|
|
33
|
+
}, options?: ErrorOptions): {
|
|
34
|
+
readonly name: "UnknownError";
|
|
35
|
+
readonly data: {
|
|
36
|
+
message: string;
|
|
37
|
+
};
|
|
38
|
+
schema(): z.ZodObject<{
|
|
39
|
+
name: z.ZodLiteral<"UnknownError">;
|
|
40
|
+
data: z.ZodObject<{
|
|
41
|
+
message: z.ZodString;
|
|
42
|
+
}, z.core.$strip>;
|
|
43
|
+
}, z.core.$strip>;
|
|
44
|
+
toObject(): {
|
|
45
|
+
name: "UnknownError";
|
|
46
|
+
data: {
|
|
47
|
+
message: string;
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
message: string;
|
|
51
|
+
stack?: string;
|
|
52
|
+
cause?: unknown;
|
|
53
|
+
};
|
|
54
|
+
readonly Schema: z.ZodObject<{
|
|
55
|
+
name: z.ZodLiteral<"UnknownError">;
|
|
56
|
+
data: z.ZodObject<{
|
|
57
|
+
message: z.ZodString;
|
|
58
|
+
}, z.core.$strip>;
|
|
59
|
+
}, z.core.$strip>;
|
|
60
|
+
isInstance(input: any): input is {
|
|
61
|
+
readonly name: "UnknownError";
|
|
62
|
+
readonly data: {
|
|
63
|
+
message: string;
|
|
64
|
+
};
|
|
65
|
+
schema(): z.ZodObject<{
|
|
66
|
+
name: z.ZodLiteral<"UnknownError">;
|
|
67
|
+
data: z.ZodObject<{
|
|
68
|
+
message: z.ZodString;
|
|
69
|
+
}, z.core.$strip>;
|
|
70
|
+
}, z.core.$strip>;
|
|
71
|
+
toObject(): {
|
|
72
|
+
name: "UnknownError";
|
|
73
|
+
data: {
|
|
74
|
+
message: string;
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
message: string;
|
|
78
|
+
stack?: string;
|
|
79
|
+
cause?: unknown;
|
|
80
|
+
};
|
|
81
|
+
create<Name extends string, Data extends z.core.$ZodType>(name: Name, data: Data): /*elided*/ any;
|
|
82
|
+
readonly Unknown: /*elided*/ any;
|
|
83
|
+
isError(value: unknown): value is Error;
|
|
84
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
85
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
86
|
+
stackTraceLimit: number;
|
|
87
|
+
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
|
|
88
|
+
};
|
|
89
|
+
isError(value: unknown): value is Error;
|
|
90
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
91
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
92
|
+
stackTraceLimit: number;
|
|
93
|
+
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
|
|
94
|
+
};
|
|
95
|
+
static readonly Unknown: {
|
|
96
|
+
new (data: {
|
|
97
|
+
message: string;
|
|
98
|
+
}, options?: ErrorOptions): {
|
|
99
|
+
readonly name: "UnknownError";
|
|
100
|
+
readonly data: {
|
|
101
|
+
message: string;
|
|
102
|
+
};
|
|
103
|
+
schema(): z.ZodObject<{
|
|
104
|
+
name: z.ZodLiteral<"UnknownError">;
|
|
105
|
+
data: z.ZodObject<{
|
|
106
|
+
message: z.ZodString;
|
|
107
|
+
}, z.core.$strip>;
|
|
108
|
+
}, z.core.$strip>;
|
|
109
|
+
toObject(): {
|
|
110
|
+
name: "UnknownError";
|
|
111
|
+
data: {
|
|
112
|
+
message: string;
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
message: string;
|
|
116
|
+
stack?: string;
|
|
117
|
+
cause?: unknown;
|
|
118
|
+
};
|
|
119
|
+
readonly Schema: z.ZodObject<{
|
|
120
|
+
name: z.ZodLiteral<"UnknownError">;
|
|
121
|
+
data: z.ZodObject<{
|
|
122
|
+
message: z.ZodString;
|
|
123
|
+
}, z.core.$strip>;
|
|
124
|
+
}, z.core.$strip>;
|
|
125
|
+
isInstance(input: any): input is {
|
|
126
|
+
readonly name: "UnknownError";
|
|
127
|
+
readonly data: {
|
|
128
|
+
message: string;
|
|
129
|
+
};
|
|
130
|
+
schema(): z.ZodObject<{
|
|
131
|
+
name: z.ZodLiteral<"UnknownError">;
|
|
132
|
+
data: z.ZodObject<{
|
|
133
|
+
message: z.ZodString;
|
|
134
|
+
}, z.core.$strip>;
|
|
135
|
+
}, z.core.$strip>;
|
|
136
|
+
toObject(): {
|
|
137
|
+
name: "UnknownError";
|
|
138
|
+
data: {
|
|
139
|
+
message: string;
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
message: string;
|
|
143
|
+
stack?: string;
|
|
144
|
+
cause?: unknown;
|
|
145
|
+
};
|
|
146
|
+
create<Name extends string, Data extends z.core.$ZodType>(name: Name, data: Data): {
|
|
147
|
+
new (data: z.input<Data>, options?: ErrorOptions): {
|
|
148
|
+
readonly name: Name;
|
|
149
|
+
readonly data: z.core.input<Data>;
|
|
150
|
+
schema(): z.ZodObject<{
|
|
151
|
+
name: z.ZodLiteral<Name>;
|
|
152
|
+
data: Data;
|
|
153
|
+
}, z.core.$strip>;
|
|
154
|
+
toObject(): {
|
|
155
|
+
name: Name;
|
|
156
|
+
data: z.core.input<Data>;
|
|
157
|
+
};
|
|
158
|
+
message: string;
|
|
159
|
+
stack?: string;
|
|
160
|
+
cause?: unknown;
|
|
161
|
+
};
|
|
162
|
+
readonly Schema: z.ZodObject<{
|
|
163
|
+
name: z.ZodLiteral<Name>;
|
|
164
|
+
data: Data;
|
|
165
|
+
}, z.core.$strip>;
|
|
166
|
+
isInstance(input: any): input is InstanceType</*elided*/ any>;
|
|
167
|
+
create<Name extends string, Data extends z.core.$ZodType>(name: Name, data: Data): /*elided*/ any;
|
|
168
|
+
readonly Unknown: /*elided*/ any;
|
|
169
|
+
isError(value: unknown): value is Error;
|
|
170
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
171
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
172
|
+
stackTraceLimit: number;
|
|
173
|
+
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
|
|
174
|
+
};
|
|
175
|
+
readonly Unknown: /*elided*/ any;
|
|
176
|
+
isError(value: unknown): value is Error;
|
|
177
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
178
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function): void;
|
|
179
|
+
stackTraceLimit: number;
|
|
180
|
+
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
|
|
181
|
+
};
|
|
182
|
+
}
|
package/dist/error.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import z from "zod";
|
|
2
|
+
export class NamedError extends Error {
|
|
3
|
+
static create(name, data) {
|
|
4
|
+
const schema = z
|
|
5
|
+
.object({
|
|
6
|
+
name: z.literal(name),
|
|
7
|
+
data,
|
|
8
|
+
})
|
|
9
|
+
.meta({
|
|
10
|
+
ref: name,
|
|
11
|
+
});
|
|
12
|
+
const result = class extends NamedError {
|
|
13
|
+
data;
|
|
14
|
+
static Schema = schema;
|
|
15
|
+
name = name;
|
|
16
|
+
constructor(data, options) {
|
|
17
|
+
const msg = data && typeof data === "object" && "message" in data && typeof data.message === "string"
|
|
18
|
+
? data.message
|
|
19
|
+
: name;
|
|
20
|
+
super(msg, options);
|
|
21
|
+
this.data = data;
|
|
22
|
+
this.name = name;
|
|
23
|
+
}
|
|
24
|
+
static isInstance(input) {
|
|
25
|
+
return typeof input === "object" && "name" in input && input.name === name;
|
|
26
|
+
}
|
|
27
|
+
schema() {
|
|
28
|
+
return schema;
|
|
29
|
+
}
|
|
30
|
+
toObject() {
|
|
31
|
+
return {
|
|
32
|
+
name: name,
|
|
33
|
+
data: this.data,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(result, "name", { value: name });
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
static Unknown = NamedError.create("UnknownError", z.object({
|
|
41
|
+
message: z.string(),
|
|
42
|
+
}));
|
|
43
|
+
}
|
package/dist/fn.d.ts
ADDED
package/dist/fn.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { randomBytes } from "crypto";
|
|
2
|
+
export var Identifier;
|
|
3
|
+
(function (Identifier) {
|
|
4
|
+
const LENGTH = 26;
|
|
5
|
+
// State for monotonic ID generation
|
|
6
|
+
let lastTimestamp = 0;
|
|
7
|
+
let counter = 0;
|
|
8
|
+
function ascending() {
|
|
9
|
+
return create(false);
|
|
10
|
+
}
|
|
11
|
+
Identifier.ascending = ascending;
|
|
12
|
+
function descending() {
|
|
13
|
+
return create(true);
|
|
14
|
+
}
|
|
15
|
+
Identifier.descending = descending;
|
|
16
|
+
function randomBase62(length) {
|
|
17
|
+
const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
18
|
+
let result = "";
|
|
19
|
+
const bytes = randomBytes(length);
|
|
20
|
+
for (let i = 0; i < length; i++) {
|
|
21
|
+
result += chars[bytes[i] % 62];
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
function create(descending, timestamp) {
|
|
26
|
+
const currentTimestamp = timestamp ?? Date.now();
|
|
27
|
+
if (currentTimestamp !== lastTimestamp) {
|
|
28
|
+
lastTimestamp = currentTimestamp;
|
|
29
|
+
counter = 0;
|
|
30
|
+
}
|
|
31
|
+
counter++;
|
|
32
|
+
let now = BigInt(currentTimestamp) * BigInt(0x1000) + BigInt(counter);
|
|
33
|
+
now = descending ? ~now : now;
|
|
34
|
+
const timeBytes = Buffer.alloc(6);
|
|
35
|
+
for (let i = 0; i < 6; i++) {
|
|
36
|
+
timeBytes[i] = Number((now >> BigInt(40 - 8 * i)) & BigInt(0xff));
|
|
37
|
+
}
|
|
38
|
+
return timeBytes.toString("hex") + randomBase62(LENGTH - 12);
|
|
39
|
+
}
|
|
40
|
+
Identifier.create = create;
|
|
41
|
+
})(Identifier || (Identifier = {}));
|
package/dist/iife.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function iife<T>(fn: () => T): T;
|
package/dist/iife.js
ADDED
package/dist/json.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function parsePartialJson(raw: string): Record<string, any>;
|
package/dist/json.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
export function parsePartialJson(raw) {
|
|
2
|
+
try {
|
|
3
|
+
return JSON.parse(raw);
|
|
4
|
+
}
|
|
5
|
+
catch { }
|
|
6
|
+
let text = raw;
|
|
7
|
+
if (/(?:^|[^\\])\\$/.test(text)) {
|
|
8
|
+
text = text.slice(0, -1);
|
|
9
|
+
}
|
|
10
|
+
const stack = [];
|
|
11
|
+
let inString = false;
|
|
12
|
+
let escaped = false;
|
|
13
|
+
let depth = 0;
|
|
14
|
+
let afterColon = false;
|
|
15
|
+
let lastSafeEnd = -1;
|
|
16
|
+
for (let i = 0; i < text.length; i++) {
|
|
17
|
+
const char = text[i];
|
|
18
|
+
if (escaped) {
|
|
19
|
+
escaped = false;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (char === "\\") {
|
|
23
|
+
escaped = true;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (char === '"') {
|
|
27
|
+
if (inString) {
|
|
28
|
+
inString = false;
|
|
29
|
+
if (afterColon && depth === 1) {
|
|
30
|
+
lastSafeEnd = i + 1;
|
|
31
|
+
afterColon = false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
inString = true;
|
|
36
|
+
}
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (inString)
|
|
40
|
+
continue;
|
|
41
|
+
if (char === "{") {
|
|
42
|
+
depth++;
|
|
43
|
+
stack.push("}");
|
|
44
|
+
}
|
|
45
|
+
else if (char === "[") {
|
|
46
|
+
depth++;
|
|
47
|
+
stack.push("]");
|
|
48
|
+
}
|
|
49
|
+
else if (char === "}" || char === "]") {
|
|
50
|
+
depth--;
|
|
51
|
+
stack.pop();
|
|
52
|
+
if (afterColon && depth === 1) {
|
|
53
|
+
lastSafeEnd = i + 1;
|
|
54
|
+
afterColon = false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else if (char === ":" && depth === 1) {
|
|
58
|
+
afterColon = true;
|
|
59
|
+
}
|
|
60
|
+
else if (char === "," && depth === 1) {
|
|
61
|
+
lastSafeEnd = i;
|
|
62
|
+
afterColon = false;
|
|
63
|
+
}
|
|
64
|
+
else if (afterColon && depth === 1 && !/\s/.test(char)) {
|
|
65
|
+
let end = i;
|
|
66
|
+
while (end + 1 < text.length && /[^\s,}\]]/.test(text[end + 1]))
|
|
67
|
+
end++;
|
|
68
|
+
const token = text.slice(i, end + 1);
|
|
69
|
+
if (/^(?:true|false|null|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)$/.test(token)) {
|
|
70
|
+
lastSafeEnd = end + 1;
|
|
71
|
+
afterColon = false;
|
|
72
|
+
i = end;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const result = closeAndParse(text, inString, stack);
|
|
77
|
+
if (result !== undefined)
|
|
78
|
+
return result;
|
|
79
|
+
if (lastSafeEnd > 0) {
|
|
80
|
+
let safe = text.slice(0, lastSafeEnd).replace(/,\s*$/, "");
|
|
81
|
+
const safeStack = [];
|
|
82
|
+
let s = false;
|
|
83
|
+
let e = false;
|
|
84
|
+
for (let i = 0; i < safe.length; i++) {
|
|
85
|
+
const c = safe[i];
|
|
86
|
+
if (e) {
|
|
87
|
+
e = false;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (c === "\\") {
|
|
91
|
+
e = true;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (c === '"') {
|
|
95
|
+
s = !s;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (s)
|
|
99
|
+
continue;
|
|
100
|
+
if (c === "{")
|
|
101
|
+
safeStack.push("}");
|
|
102
|
+
if (c === "[")
|
|
103
|
+
safeStack.push("]");
|
|
104
|
+
if (c === "}" || c === "]")
|
|
105
|
+
safeStack.pop();
|
|
106
|
+
}
|
|
107
|
+
while (safeStack.length)
|
|
108
|
+
safe += safeStack.pop();
|
|
109
|
+
try {
|
|
110
|
+
const result = JSON.parse(safe);
|
|
111
|
+
if (typeof result === "object" && result !== null)
|
|
112
|
+
return result;
|
|
113
|
+
}
|
|
114
|
+
catch { }
|
|
115
|
+
}
|
|
116
|
+
return {};
|
|
117
|
+
}
|
|
118
|
+
function closeAndParse(text, inString, stack) {
|
|
119
|
+
let base = text;
|
|
120
|
+
if (inString)
|
|
121
|
+
base += '"';
|
|
122
|
+
{
|
|
123
|
+
let closed = base;
|
|
124
|
+
const s = [...stack];
|
|
125
|
+
while (s.length)
|
|
126
|
+
closed += s.pop();
|
|
127
|
+
try {
|
|
128
|
+
const result = JSON.parse(closed);
|
|
129
|
+
if (typeof result === "object" && result !== null)
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
catch { }
|
|
133
|
+
}
|
|
134
|
+
let trimmed = base;
|
|
135
|
+
for (let attempt = 0; attempt < 10; attempt++) {
|
|
136
|
+
const lastCut = findLastCutPoint(trimmed);
|
|
137
|
+
if (lastCut < 0)
|
|
138
|
+
break;
|
|
139
|
+
trimmed = trimmed.slice(0, lastCut).replace(/[,:\s]+$/, "");
|
|
140
|
+
const closed = trimmed + computeClosers(trimmed);
|
|
141
|
+
try {
|
|
142
|
+
const result = JSON.parse(closed);
|
|
143
|
+
if (typeof result === "object" && result !== null)
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
catch { }
|
|
147
|
+
}
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
function findLastCutPoint(text) {
|
|
151
|
+
let inStr = false;
|
|
152
|
+
let esc = false;
|
|
153
|
+
let lastComma = -1;
|
|
154
|
+
let lastOpen = -1;
|
|
155
|
+
for (let i = 0; i < text.length; i++) {
|
|
156
|
+
const c = text[i];
|
|
157
|
+
if (esc) {
|
|
158
|
+
esc = false;
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
if (c === "\\") {
|
|
162
|
+
esc = true;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (c === '"') {
|
|
166
|
+
inStr = !inStr;
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
if (inStr)
|
|
170
|
+
continue;
|
|
171
|
+
if (c === ",")
|
|
172
|
+
lastComma = i;
|
|
173
|
+
if (c === "[" || c === "{")
|
|
174
|
+
lastOpen = i;
|
|
175
|
+
}
|
|
176
|
+
return lastComma > lastOpen ? lastComma : lastOpen > 0 ? lastOpen + 1 : -1;
|
|
177
|
+
}
|
|
178
|
+
function computeClosers(text) {
|
|
179
|
+
const stack = [];
|
|
180
|
+
let inStr = false;
|
|
181
|
+
let esc = false;
|
|
182
|
+
for (let i = 0; i < text.length; i++) {
|
|
183
|
+
const c = text[i];
|
|
184
|
+
if (esc) {
|
|
185
|
+
esc = false;
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (c === "\\") {
|
|
189
|
+
esc = true;
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
if (c === '"') {
|
|
193
|
+
inStr = !inStr;
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (inStr)
|
|
197
|
+
continue;
|
|
198
|
+
if (c === "{")
|
|
199
|
+
stack.push("}");
|
|
200
|
+
if (c === "[")
|
|
201
|
+
stack.push("]");
|
|
202
|
+
if (c === "}" || c === "]")
|
|
203
|
+
stack.pop();
|
|
204
|
+
}
|
|
205
|
+
let result = "";
|
|
206
|
+
while (stack.length)
|
|
207
|
+
result += stack.pop();
|
|
208
|
+
return result;
|
|
209
|
+
}
|
package/dist/lazy.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function lazy<T>(fn: () => T): () => T;
|
package/dist/lazy.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export declare namespace ModelLimit {
|
|
2
|
+
interface Info {
|
|
3
|
+
context: number;
|
|
4
|
+
input?: number;
|
|
5
|
+
output?: number;
|
|
6
|
+
}
|
|
7
|
+
interface TokenUsage {
|
|
8
|
+
input: number;
|
|
9
|
+
output: number;
|
|
10
|
+
reasoning: number;
|
|
11
|
+
cache: {
|
|
12
|
+
read: number;
|
|
13
|
+
write: number;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
const OUTPUT_TOKEN_MAX = 32000;
|
|
17
|
+
/**
|
|
18
|
+
* Compute actual input tokens from a usage breakdown.
|
|
19
|
+
*
|
|
20
|
+
* For Anthropic-style providers, `input` excludes cached tokens — they are
|
|
21
|
+
* reported separately as `cache.read` (cache hit) and `cache.write` (first
|
|
22
|
+
* write). All three consume context window input space, so the true input
|
|
23
|
+
* footprint is their sum.
|
|
24
|
+
*/
|
|
25
|
+
function actualInput(tokens: TokenUsage): number;
|
|
26
|
+
/**
|
|
27
|
+
* Compute how many tokens are available for input within a model's context.
|
|
28
|
+
*
|
|
29
|
+
* Shared-context models (most LLMs) allow input to fill the entire context
|
|
30
|
+
* window — the API enforces the limit at request time based on actual
|
|
31
|
+
* input + output, not at input time. So we don't reserve output space.
|
|
32
|
+
*
|
|
33
|
+
* If the model config specifies an explicit `input` limit, use it directly.
|
|
34
|
+
* Otherwise, the entire `context` window is usable for input.
|
|
35
|
+
*/
|
|
36
|
+
function usableInput(limit?: Info): number;
|
|
37
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export var ModelLimit;
|
|
2
|
+
(function (ModelLimit) {
|
|
3
|
+
ModelLimit.OUTPUT_TOKEN_MAX = 32_000;
|
|
4
|
+
/**
|
|
5
|
+
* Compute actual input tokens from a usage breakdown.
|
|
6
|
+
*
|
|
7
|
+
* For Anthropic-style providers, `input` excludes cached tokens — they are
|
|
8
|
+
* reported separately as `cache.read` (cache hit) and `cache.write` (first
|
|
9
|
+
* write). All three consume context window input space, so the true input
|
|
10
|
+
* footprint is their sum.
|
|
11
|
+
*/
|
|
12
|
+
function actualInput(tokens) {
|
|
13
|
+
return tokens.input + tokens.cache.read + tokens.cache.write;
|
|
14
|
+
}
|
|
15
|
+
ModelLimit.actualInput = actualInput;
|
|
16
|
+
/**
|
|
17
|
+
* Compute how many tokens are available for input within a model's context.
|
|
18
|
+
*
|
|
19
|
+
* Shared-context models (most LLMs) allow input to fill the entire context
|
|
20
|
+
* window — the API enforces the limit at request time based on actual
|
|
21
|
+
* input + output, not at input time. So we don't reserve output space.
|
|
22
|
+
*
|
|
23
|
+
* If the model config specifies an explicit `input` limit, use it directly.
|
|
24
|
+
* Otherwise, the entire `context` window is usable for input.
|
|
25
|
+
*/
|
|
26
|
+
function usableInput(limit) {
|
|
27
|
+
if (!limit || limit.context === 0)
|
|
28
|
+
return 0;
|
|
29
|
+
if (typeof limit.input === "number" && limit.input > 0) {
|
|
30
|
+
return limit.input;
|
|
31
|
+
}
|
|
32
|
+
return limit.context;
|
|
33
|
+
}
|
|
34
|
+
ModelLimit.usableInput = usableInput;
|
|
35
|
+
})(ModelLimit || (ModelLimit = {}));
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare namespace NoteMarkdown {
|
|
2
|
+
interface TipTapNode {
|
|
3
|
+
type: string;
|
|
4
|
+
attrs?: Record<string, any>;
|
|
5
|
+
content?: TipTapNode[];
|
|
6
|
+
text?: string;
|
|
7
|
+
marks?: Array<{
|
|
8
|
+
type: string;
|
|
9
|
+
attrs?: Record<string, any>;
|
|
10
|
+
}>;
|
|
11
|
+
}
|
|
12
|
+
export function toMarkdown(content: any): string;
|
|
13
|
+
export function fromMarkdown(markdown: string): TipTapNode;
|
|
14
|
+
/**
|
|
15
|
+
* Generate safe, escaped HTML snippet from Tiptap JSON for note card thumbnails.
|
|
16
|
+
* Produces a compact multi-block preview. Text is HTML-escaped, media is limited
|
|
17
|
+
* to trusted image sources, and no external HTML is rendered.
|
|
18
|
+
*/
|
|
19
|
+
export function toPreviewHtml(content: any, options?: {
|
|
20
|
+
title?: string;
|
|
21
|
+
maxBlocks?: number;
|
|
22
|
+
}): string;
|
|
23
|
+
export {};
|
|
24
|
+
}
|