@vettvangur/ai-sdk 0.1.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/LICENSE +11 -0
- package/README.md +43 -0
- package/dist/index.d.ts +182 -0
- package/dist/index.js +1038 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Portions of this package rely on third-party open source libraries, which are licensed under their respective open source licenses (such as MIT). Use of those libraries is governed solely by their respective license terms.
|
|
2
|
+
|
|
3
|
+
All original code in this repository is licensed under the terms below:
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2025 Vettvangur
|
|
8
|
+
|
|
9
|
+
This software is provided for personal or internal non-commercial use only. You may not modify, distribute, sublicense, or use this software in commercial products or services without explicit written permission from Vettvangur.
|
|
10
|
+
|
|
11
|
+
All rights reserved.
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# @vettvangur/ai-sdk
|
|
2
|
+
|
|
3
|
+
Framework-agnostic, unstyled AI chat primitives for server-rendered shells.
|
|
4
|
+
|
|
5
|
+
It exports protocol-v1 contracts, parsing and SSE stream-reading primitives, a headless chat session,
|
|
6
|
+
and a DOM binding for a server-rendered shell. It does not include styles or project-specific tools.
|
|
7
|
+
|
|
8
|
+
```html
|
|
9
|
+
<section data-ai-chat data-ai-chat-endpoint="/webapi/aichat/stream"
|
|
10
|
+
data-ai-chat-antiforgery-header="RequestVerificationToken" data-ai-chat-antiforgery-token="…">
|
|
11
|
+
<div data-ai-chat-thread></div>
|
|
12
|
+
<form data-ai-chat-form><textarea data-ai-chat-input></textarea><button data-ai-chat-send>Send</button></form>
|
|
13
|
+
<button type="button" data-ai-chat-reset>Reset</button>
|
|
14
|
+
</section>
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { bindChat, createAiChatRegistry } from '@vettvangur/ai-sdk'
|
|
19
|
+
|
|
20
|
+
const registry = createAiChatRegistry()
|
|
21
|
+
registry.registerRenderer('weather', ({ document, data }) => {
|
|
22
|
+
const card = document.createElement('article')
|
|
23
|
+
card.textContent = String((data as { temperature?: unknown }).temperature ?? '')
|
|
24
|
+
return card
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const chat = bindChat(document.querySelector('[data-ai-chat]')!, { registry })
|
|
28
|
+
// Call chat.destroy() when its server-rendered shell is removed.
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
`createChatSession()` is available when a host owns its own rendering. It sends JSON POST requests
|
|
32
|
+
to the configured same-origin endpoint and reads its SSE response. Supply the antiforgery header
|
|
33
|
+
through `bindChat` options or the root attributes; blank header names or tokens are omitted.
|
|
34
|
+
|
|
35
|
+
`tool_result` payloads may select only registered tool renderers, an authored
|
|
36
|
+
`template[data-template]`, or an authored `template[data-ai-surface]`. Named actions come only
|
|
37
|
+
from authored `data-ai-action` markup and receive an optional authored `data-ai-action-value`.
|
|
38
|
+
Neither server payloads nor model output become executable markup. Generic answer cards, links and
|
|
39
|
+
Markdown are rendered as safe DOM nodes. Host callbacks must return DOM nodes, not HTML strings.
|
|
40
|
+
|
|
41
|
+
The package accepts protocol-v1 events (`delta`, `tool`, `tool_result`, `tool_done`, `done`, and
|
|
42
|
+
`error`). Host-specific card rendering, embedded-component activation and styling remain host
|
|
43
|
+
responsibilities. Eimskip migration is deliberately outside this package.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
//#region src/contracts.d.ts
|
|
2
|
+
type AiChatMessage = {
|
|
3
|
+
role: 'user' | 'assistant';
|
|
4
|
+
content: string;
|
|
5
|
+
};
|
|
6
|
+
type AiChatRequest = {
|
|
7
|
+
messages: AiChatMessage[];
|
|
8
|
+
provider?: string;
|
|
9
|
+
context?: Record<string, string>;
|
|
10
|
+
};
|
|
11
|
+
type AiChatContext = Record<string, string>;
|
|
12
|
+
type AiChatSessionStatus = 'idle' | 'sending' | 'disposed';
|
|
13
|
+
type AiChatSessionResult = {
|
|
14
|
+
provider: string | null;
|
|
15
|
+
canceled: boolean;
|
|
16
|
+
error: Error | null;
|
|
17
|
+
};
|
|
18
|
+
type AiChatSessionOptions = {
|
|
19
|
+
endpoint: string;
|
|
20
|
+
provider?: string;
|
|
21
|
+
headers?: Record<string, string>;
|
|
22
|
+
timeoutMs?: number;
|
|
23
|
+
fetch?: typeof fetch;
|
|
24
|
+
onEvent?: (event: AiChatStreamEvent) => void;
|
|
25
|
+
};
|
|
26
|
+
type AiChatSession = {
|
|
27
|
+
readonly history: readonly AiChatMessage[];
|
|
28
|
+
readonly context: Readonly<AiChatContext>;
|
|
29
|
+
readonly status: AiChatSessionStatus;
|
|
30
|
+
send(message: string): Promise<AiChatSessionResult>;
|
|
31
|
+
cancel(): void;
|
|
32
|
+
reset(): void;
|
|
33
|
+
setContext(name: string, value: string): void;
|
|
34
|
+
clearContext(name: string): void;
|
|
35
|
+
subscribe(listener: (event: AiChatStreamEvent) => void): () => void;
|
|
36
|
+
dispose(): void;
|
|
37
|
+
};
|
|
38
|
+
type AiAnswerCardFact = {
|
|
39
|
+
label: string;
|
|
40
|
+
value: string;
|
|
41
|
+
};
|
|
42
|
+
type AiAnswerCardLink = {
|
|
43
|
+
label: string;
|
|
44
|
+
url: string;
|
|
45
|
+
};
|
|
46
|
+
type AiAnswerCardTable = {
|
|
47
|
+
label?: string | null;
|
|
48
|
+
headers: string[];
|
|
49
|
+
rows: string[][];
|
|
50
|
+
};
|
|
51
|
+
type AiAnswerCardSection = {
|
|
52
|
+
title?: string | null;
|
|
53
|
+
text?: string | null;
|
|
54
|
+
facts?: AiAnswerCardFact[] | null;
|
|
55
|
+
tables?: AiAnswerCardTable[] | null;
|
|
56
|
+
links?: AiAnswerCardLink[] | null;
|
|
57
|
+
note?: string | null;
|
|
58
|
+
collapsible?: boolean;
|
|
59
|
+
};
|
|
60
|
+
type AiAnswerCard = {
|
|
61
|
+
kind: 'answer';
|
|
62
|
+
title: string;
|
|
63
|
+
summary?: string | null;
|
|
64
|
+
highlight?: AiAnswerCardFact | null;
|
|
65
|
+
sections?: AiAnswerCardSection[] | null;
|
|
66
|
+
note?: string | null;
|
|
67
|
+
actions?: AiAnswerCardLink[] | null;
|
|
68
|
+
sources?: AiAnswerCardLink[] | null;
|
|
69
|
+
sourceNote?: string | null;
|
|
70
|
+
};
|
|
71
|
+
type AiAnswerCardLabels = {
|
|
72
|
+
sources: string;
|
|
73
|
+
details: string;
|
|
74
|
+
table: string;
|
|
75
|
+
tableScroll: string;
|
|
76
|
+
};
|
|
77
|
+
type AiChatStreamEvent = {
|
|
78
|
+
name: 'delta';
|
|
79
|
+
data: {
|
|
80
|
+
text: string;
|
|
81
|
+
};
|
|
82
|
+
} | {
|
|
83
|
+
name: 'tool';
|
|
84
|
+
data: {
|
|
85
|
+
id: string;
|
|
86
|
+
name: string;
|
|
87
|
+
};
|
|
88
|
+
} | {
|
|
89
|
+
name: 'tool_result';
|
|
90
|
+
data: {
|
|
91
|
+
id: string;
|
|
92
|
+
name: string;
|
|
93
|
+
data: unknown;
|
|
94
|
+
reply: string | null;
|
|
95
|
+
};
|
|
96
|
+
} | {
|
|
97
|
+
name: 'tool_done';
|
|
98
|
+
data: {
|
|
99
|
+
id: string;
|
|
100
|
+
name: string;
|
|
101
|
+
isError: boolean;
|
|
102
|
+
};
|
|
103
|
+
} | {
|
|
104
|
+
name: 'done';
|
|
105
|
+
data: {
|
|
106
|
+
provider: string;
|
|
107
|
+
};
|
|
108
|
+
} | {
|
|
109
|
+
name: 'error';
|
|
110
|
+
data: {
|
|
111
|
+
message: string;
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
type AiChatLabels = AiAnswerCardLabels & {
|
|
115
|
+
error: string;
|
|
116
|
+
timeout: string;
|
|
117
|
+
toolRunning: string;
|
|
118
|
+
toolComplete: string;
|
|
119
|
+
toolFailed: string;
|
|
120
|
+
toolInterrupted: string;
|
|
121
|
+
};
|
|
122
|
+
type AiChatExtensionContext = {
|
|
123
|
+
document: Document;
|
|
124
|
+
root: HTMLElement;
|
|
125
|
+
tool: string;
|
|
126
|
+
data: unknown;
|
|
127
|
+
reply: string | null;
|
|
128
|
+
};
|
|
129
|
+
type AiChatRenderer = (context: AiChatExtensionContext) => Node | null;
|
|
130
|
+
type AiChatSurface = (context: AiChatExtensionContext) => Node | null;
|
|
131
|
+
type AiChatActionContext = {
|
|
132
|
+
root: HTMLElement;
|
|
133
|
+
element: HTMLElement;
|
|
134
|
+
value: string | null;
|
|
135
|
+
session: AiChatSession;
|
|
136
|
+
};
|
|
137
|
+
type AiChatAction = (context: AiChatActionContext) => undefined | {
|
|
138
|
+
context: [string, string];
|
|
139
|
+
} | Promise<undefined | {
|
|
140
|
+
context: [string, string];
|
|
141
|
+
}>;
|
|
142
|
+
type AiChatRegistry = {
|
|
143
|
+
registerRenderer(name: string, renderer: AiChatRenderer): () => void;
|
|
144
|
+
registerSurface(name: string, surface: AiChatSurface): () => void;
|
|
145
|
+
registerAction(name: string, action: AiChatAction): () => void;
|
|
146
|
+
};
|
|
147
|
+
type AiChatBindings = {
|
|
148
|
+
endpoint?: string;
|
|
149
|
+
provider?: string;
|
|
150
|
+
antiforgeryHeader?: string;
|
|
151
|
+
antiforgeryToken?: string;
|
|
152
|
+
timeoutMs?: number;
|
|
153
|
+
labels?: Partial<AiChatLabels>;
|
|
154
|
+
registry?: AiChatRegistry;
|
|
155
|
+
onEmbedded?: (element: HTMLElement) => void;
|
|
156
|
+
onError?: (error: Error) => void;
|
|
157
|
+
};
|
|
158
|
+
type AiChatBinding = {
|
|
159
|
+
session: AiChatSession;
|
|
160
|
+
destroy(): void;
|
|
161
|
+
};
|
|
162
|
+
//#endregion
|
|
163
|
+
//#region src/answer-card.d.ts
|
|
164
|
+
declare function renderAnswerCard(document: Document, data: unknown, labels: AiAnswerCardLabels): HTMLElement | null;
|
|
165
|
+
//#endregion
|
|
166
|
+
//#region src/bind.d.ts
|
|
167
|
+
declare function bindChat(root: HTMLElement, options?: AiChatBindings): AiChatBinding;
|
|
168
|
+
//#endregion
|
|
169
|
+
//#region src/chat.d.ts
|
|
170
|
+
declare function createChatSession(options: AiChatSessionOptions): AiChatSession;
|
|
171
|
+
//#endregion
|
|
172
|
+
//#region src/protocol.d.ts
|
|
173
|
+
declare const AI_CHAT_PROTOCOL_VERSION = 1;
|
|
174
|
+
declare function parseStreamEvent(name: string, data: unknown): AiChatStreamEvent | null;
|
|
175
|
+
//#endregion
|
|
176
|
+
//#region src/registry.d.ts
|
|
177
|
+
declare function createAiChatRegistry(): AiChatRegistry;
|
|
178
|
+
//#endregion
|
|
179
|
+
//#region src/stream.d.ts
|
|
180
|
+
declare function readEventStream(response: Response, signal: AbortSignal): AsyncGenerator<AiChatStreamEvent>;
|
|
181
|
+
//#endregion
|
|
182
|
+
export { AI_CHAT_PROTOCOL_VERSION, type AiAnswerCard, type AiAnswerCardFact, type AiAnswerCardLabels, type AiAnswerCardLink, type AiAnswerCardSection, type AiAnswerCardTable, type AiChatAction, type AiChatBinding, type AiChatBindings, type AiChatContext, type AiChatExtensionContext, type AiChatMessage, type AiChatRegistry, type AiChatRenderer, type AiChatRequest, type AiChatSession, type AiChatSessionOptions, type AiChatSessionResult, type AiChatSessionStatus, type AiChatStreamEvent, type AiChatSurface, bindChat, createAiChatRegistry, createChatSession, parseStreamEvent, readEventStream, renderAnswerCard };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,1038 @@
|
|
|
1
|
+
//#region src/dom.ts
|
|
2
|
+
function hasUnsafeLinkCharacter(value) {
|
|
3
|
+
for (const character of value) {
|
|
4
|
+
const code = character.charCodeAt(0);
|
|
5
|
+
if (/\s/.test(character) || character === "\\" || code <= 31 || code >= 127 && code <= 159) return true;
|
|
6
|
+
}
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
function isSafeLinkUrl(url) {
|
|
10
|
+
if (url.length === 0 || hasUnsafeLinkCharacter(url)) return false;
|
|
11
|
+
if (url.startsWith("/") || url.startsWith("#")) return !url.startsWith("//");
|
|
12
|
+
if (/^https?:\/\//i.test(url)) try {
|
|
13
|
+
const parsed = new URL(url);
|
|
14
|
+
return parsed.protocol === "http:" || parsed.protocol === "https:";
|
|
15
|
+
} catch {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
return /^(?:mailto|tel):[^:]+$/i.test(url);
|
|
19
|
+
}
|
|
20
|
+
function createSafeLink(document, label, url) {
|
|
21
|
+
if (!isSafeLinkUrl(url)) return null;
|
|
22
|
+
const link = document.createElement("a");
|
|
23
|
+
link.href = url;
|
|
24
|
+
link.append(document.createTextNode(label));
|
|
25
|
+
if (/^https?:\/\//i.test(url)) {
|
|
26
|
+
link.target = "_blank";
|
|
27
|
+
link.rel = "noopener noreferrer";
|
|
28
|
+
}
|
|
29
|
+
return link;
|
|
30
|
+
}
|
|
31
|
+
function isPlainObject(value) {
|
|
32
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
33
|
+
const prototype = Object.getPrototypeOf(value);
|
|
34
|
+
return prototype === Object.prototype || prototype === null;
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region src/markdown.ts
|
|
38
|
+
const MAX_MARKDOWN_CHARACTERS = 8e3;
|
|
39
|
+
function isWordCharacter(character) {
|
|
40
|
+
return character !== void 0 && /[\p{L}\p{N}]/u.test(character);
|
|
41
|
+
}
|
|
42
|
+
function delimitedToken(source, start, marker, type) {
|
|
43
|
+
if (!source.startsWith(marker, start)) return null;
|
|
44
|
+
const contentStart = start + marker.length;
|
|
45
|
+
const closing = source.indexOf(marker, contentStart);
|
|
46
|
+
if (closing === -1 || closing === contentStart) return null;
|
|
47
|
+
return {
|
|
48
|
+
type,
|
|
49
|
+
content: source.slice(contentStart, closing),
|
|
50
|
+
end: closing + marker.length
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function linkToken(source, start) {
|
|
54
|
+
if (source[start] !== "[") return null;
|
|
55
|
+
let labelEnd = start + 1;
|
|
56
|
+
while (labelEnd < source.length) {
|
|
57
|
+
if (source[labelEnd] === "[") return null;
|
|
58
|
+
if (source[labelEnd] === "]" && source[labelEnd + 1] === "(") break;
|
|
59
|
+
labelEnd += 1;
|
|
60
|
+
}
|
|
61
|
+
if (labelEnd === source.length) return {
|
|
62
|
+
type: "text",
|
|
63
|
+
content: source.slice(start),
|
|
64
|
+
end: source.length
|
|
65
|
+
};
|
|
66
|
+
if (labelEnd === start + 1) return null;
|
|
67
|
+
let depth = 1;
|
|
68
|
+
let end = labelEnd + 2;
|
|
69
|
+
while (end < source.length && depth > 0) {
|
|
70
|
+
if (source[end] === "(") depth += 1;
|
|
71
|
+
else if (source[end] === ")") depth -= 1;
|
|
72
|
+
end += 1;
|
|
73
|
+
}
|
|
74
|
+
if (depth !== 0) return {
|
|
75
|
+
type: "text",
|
|
76
|
+
content: source.slice(start),
|
|
77
|
+
end: source.length
|
|
78
|
+
};
|
|
79
|
+
const url = source.slice(labelEnd + 2, end - 1);
|
|
80
|
+
if (!isSafeLinkUrl(url)) return {
|
|
81
|
+
type: "text",
|
|
82
|
+
content: source.slice(start, end),
|
|
83
|
+
end
|
|
84
|
+
};
|
|
85
|
+
return {
|
|
86
|
+
type: "link",
|
|
87
|
+
label: source.slice(start + 1, labelEnd),
|
|
88
|
+
url,
|
|
89
|
+
end
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function inlineToken(source, start) {
|
|
93
|
+
const link = linkToken(source, start);
|
|
94
|
+
if (link) return link;
|
|
95
|
+
const code = delimitedToken(source, start, "`", "code");
|
|
96
|
+
if (code) return code;
|
|
97
|
+
const bold = delimitedToken(source, start, "**", "bold") ?? delimitedToken(source, start, "__", "bold");
|
|
98
|
+
if (bold) return bold;
|
|
99
|
+
const marker = source[start];
|
|
100
|
+
if ((marker === "*" || marker === "_") && !isWordCharacter(source[start - 1])) return delimitedToken(source, start, marker, "italic");
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
function inlineStructuralItemCount(source) {
|
|
104
|
+
let count = 0;
|
|
105
|
+
let index = 0;
|
|
106
|
+
while (index < source.length) {
|
|
107
|
+
const token = inlineToken(source, index);
|
|
108
|
+
if (!token) {
|
|
109
|
+
index += 1;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
switch (token.type) {
|
|
113
|
+
case "bold":
|
|
114
|
+
case "italic":
|
|
115
|
+
count += 1 + inlineStructuralItemCount(token.content);
|
|
116
|
+
break;
|
|
117
|
+
case "code":
|
|
118
|
+
case "link": count += 1;
|
|
119
|
+
}
|
|
120
|
+
index = token.end;
|
|
121
|
+
}
|
|
122
|
+
return count;
|
|
123
|
+
}
|
|
124
|
+
function appendInlineMarkdown(document, parent, source) {
|
|
125
|
+
let textStart = 0;
|
|
126
|
+
let index = 0;
|
|
127
|
+
while (index < source.length) {
|
|
128
|
+
const token = inlineToken(source, index);
|
|
129
|
+
if (!token) {
|
|
130
|
+
index += 1;
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (textStart < index) parent.append(document.createTextNode(source.slice(textStart, index)));
|
|
134
|
+
switch (token.type) {
|
|
135
|
+
case "code": {
|
|
136
|
+
const code = document.createElement("code");
|
|
137
|
+
code.append(document.createTextNode(token.content));
|
|
138
|
+
parent.append(code);
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
case "bold": {
|
|
142
|
+
const strong = document.createElement("strong");
|
|
143
|
+
appendInlineMarkdown(document, strong, token.content);
|
|
144
|
+
parent.append(strong);
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
case "italic": {
|
|
148
|
+
const emphasis = document.createElement("em");
|
|
149
|
+
appendInlineMarkdown(document, emphasis, token.content);
|
|
150
|
+
parent.append(emphasis);
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
case "link": {
|
|
154
|
+
const link = createSafeLink(document, token.label, token.url);
|
|
155
|
+
if (link) parent.append(link);
|
|
156
|
+
else parent.append(document.createTextNode(source.slice(index, token.end)));
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
case "text": parent.append(document.createTextNode(token.content));
|
|
160
|
+
}
|
|
161
|
+
index = token.end;
|
|
162
|
+
textStart = index;
|
|
163
|
+
}
|
|
164
|
+
if (textStart < source.length) parent.append(document.createTextNode(source.slice(textStart)));
|
|
165
|
+
}
|
|
166
|
+
function unorderedListItem(line) {
|
|
167
|
+
return /^\s*[-+*]\s+(.+)$/.exec(line)?.[1] ?? null;
|
|
168
|
+
}
|
|
169
|
+
function orderedListItem(line) {
|
|
170
|
+
return /^\s*\d+[.)]\s+(.+)$/.exec(line)?.[1] ?? null;
|
|
171
|
+
}
|
|
172
|
+
function heading(line) {
|
|
173
|
+
return /^\s{0,3}#{1,6}\s+(.+?)\s*#*\s*$/.exec(line)?.[1] ?? null;
|
|
174
|
+
}
|
|
175
|
+
function countMarkdownStructuralItems(source) {
|
|
176
|
+
const lines = source.slice(0, MAX_MARKDOWN_CHARACTERS).split(/\r\n?|\n/);
|
|
177
|
+
let count = 0;
|
|
178
|
+
let index = 0;
|
|
179
|
+
while (index < lines.length) {
|
|
180
|
+
const line = lines[index];
|
|
181
|
+
if (line.trim().length === 0) {
|
|
182
|
+
index += 1;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
const headingText = heading(line);
|
|
186
|
+
if (headingText !== null) {
|
|
187
|
+
count += 2 + inlineStructuralItemCount(headingText);
|
|
188
|
+
index += 1;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
const unorderedItem = unorderedListItem(line);
|
|
192
|
+
const orderedItem = orderedListItem(line);
|
|
193
|
+
if (unorderedItem !== null || orderedItem !== null) {
|
|
194
|
+
const itemForLine = unorderedItem === null ? orderedListItem : unorderedListItem;
|
|
195
|
+
count += 1;
|
|
196
|
+
while (index < lines.length) {
|
|
197
|
+
const item = itemForLine(lines[index]);
|
|
198
|
+
if (item === null) break;
|
|
199
|
+
count += 1 + inlineStructuralItemCount(item);
|
|
200
|
+
index += 1;
|
|
201
|
+
}
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
const paragraphLines = [];
|
|
205
|
+
while (index < lines.length) {
|
|
206
|
+
const paragraphLine = lines[index];
|
|
207
|
+
if (paragraphLine.trim().length === 0 || heading(paragraphLine) !== null || unorderedListItem(paragraphLine) !== null || orderedListItem(paragraphLine) !== null) break;
|
|
208
|
+
paragraphLines.push(paragraphLine);
|
|
209
|
+
index += 1;
|
|
210
|
+
}
|
|
211
|
+
count += 1 + inlineStructuralItemCount(paragraphLines.join(" "));
|
|
212
|
+
}
|
|
213
|
+
return count;
|
|
214
|
+
}
|
|
215
|
+
function renderMarkdown(document, source) {
|
|
216
|
+
const fragment = document.createDocumentFragment();
|
|
217
|
+
const lines = source.slice(0, MAX_MARKDOWN_CHARACTERS).split(/\r\n?|\n/);
|
|
218
|
+
let index = 0;
|
|
219
|
+
while (index < lines.length) {
|
|
220
|
+
const line = lines[index];
|
|
221
|
+
if (line.trim().length === 0) {
|
|
222
|
+
index += 1;
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
const headingText = heading(line);
|
|
226
|
+
if (headingText !== null) {
|
|
227
|
+
const paragraph = document.createElement("p");
|
|
228
|
+
const strong = document.createElement("strong");
|
|
229
|
+
appendInlineMarkdown(document, strong, headingText);
|
|
230
|
+
paragraph.append(strong);
|
|
231
|
+
fragment.append(paragraph);
|
|
232
|
+
index += 1;
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
const unorderedItem = unorderedListItem(line);
|
|
236
|
+
const orderedItem = orderedListItem(line);
|
|
237
|
+
if (unorderedItem !== null || orderedItem !== null) {
|
|
238
|
+
const list = document.createElement(unorderedItem === null ? "ol" : "ul");
|
|
239
|
+
const itemForLine = unorderedItem === null ? orderedListItem : unorderedListItem;
|
|
240
|
+
while (index < lines.length) {
|
|
241
|
+
const item = itemForLine(lines[index]);
|
|
242
|
+
if (item === null) break;
|
|
243
|
+
const listItem = document.createElement("li");
|
|
244
|
+
appendInlineMarkdown(document, listItem, item);
|
|
245
|
+
list.append(listItem);
|
|
246
|
+
index += 1;
|
|
247
|
+
}
|
|
248
|
+
fragment.append(list);
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
const paragraphLines = [];
|
|
252
|
+
while (index < lines.length) {
|
|
253
|
+
const paragraphLine = lines[index];
|
|
254
|
+
if (paragraphLine.trim().length === 0 || heading(paragraphLine) !== null || unorderedListItem(paragraphLine) !== null || orderedListItem(paragraphLine) !== null) break;
|
|
255
|
+
paragraphLines.push(paragraphLine);
|
|
256
|
+
index += 1;
|
|
257
|
+
}
|
|
258
|
+
const paragraph = document.createElement("p");
|
|
259
|
+
appendInlineMarkdown(document, paragraph, paragraphLines.join(" "));
|
|
260
|
+
fragment.append(paragraph);
|
|
261
|
+
}
|
|
262
|
+
return fragment;
|
|
263
|
+
}
|
|
264
|
+
//#endregion
|
|
265
|
+
//#region src/answer-card.ts
|
|
266
|
+
const ANSWER_CARD_KEYS = /* @__PURE__ */ new Set([
|
|
267
|
+
"kind",
|
|
268
|
+
"title",
|
|
269
|
+
"summary",
|
|
270
|
+
"highlight",
|
|
271
|
+
"sections",
|
|
272
|
+
"note",
|
|
273
|
+
"actions",
|
|
274
|
+
"sources",
|
|
275
|
+
"sourceNote"
|
|
276
|
+
]);
|
|
277
|
+
const HIGHLIGHT_KEYS = /* @__PURE__ */ new Set(["label", "value"]);
|
|
278
|
+
const SECTION_KEYS = /* @__PURE__ */ new Set([
|
|
279
|
+
"title",
|
|
280
|
+
"text",
|
|
281
|
+
"facts",
|
|
282
|
+
"tables",
|
|
283
|
+
"links",
|
|
284
|
+
"note",
|
|
285
|
+
"collapsible"
|
|
286
|
+
]);
|
|
287
|
+
const FACT_KEYS = /* @__PURE__ */ new Set(["label", "value"]);
|
|
288
|
+
const TABLE_KEYS = /* @__PURE__ */ new Set([
|
|
289
|
+
"label",
|
|
290
|
+
"headers",
|
|
291
|
+
"rows"
|
|
292
|
+
]);
|
|
293
|
+
const LINK_KEYS = /* @__PURE__ */ new Set(["label", "url"]);
|
|
294
|
+
const MAX_ANSWER_CARD_TOTAL_TEXT_CHARACTERS = 24e3;
|
|
295
|
+
function hasOnlyKeys(value, keys) {
|
|
296
|
+
let keyCount = 0;
|
|
297
|
+
for (const key in value) {
|
|
298
|
+
if (!Object.hasOwn(value, key)) continue;
|
|
299
|
+
keyCount += 1;
|
|
300
|
+
if (keyCount > keys.size || !keys.has(key)) return false;
|
|
301
|
+
}
|
|
302
|
+
return true;
|
|
303
|
+
}
|
|
304
|
+
function consumeStructuralItems(budget, count) {
|
|
305
|
+
budget.structuralItems += count;
|
|
306
|
+
return budget.structuralItems <= 512;
|
|
307
|
+
}
|
|
308
|
+
function consumeTableCells(budget, count) {
|
|
309
|
+
budget.tableCells += count;
|
|
310
|
+
return budget.tableCells <= 512;
|
|
311
|
+
}
|
|
312
|
+
function consumeTextCharacters(budget, count) {
|
|
313
|
+
budget.textCharacters += count;
|
|
314
|
+
return budget.textCharacters <= MAX_ANSWER_CARD_TOTAL_TEXT_CHARACTERS;
|
|
315
|
+
}
|
|
316
|
+
function isAnswerCardText(value, budget) {
|
|
317
|
+
if (typeof value !== "string" || value.length > 8e3) return false;
|
|
318
|
+
return consumeTextCharacters(budget, value.length);
|
|
319
|
+
}
|
|
320
|
+
function hasOptionalNullableText(value, key, budget) {
|
|
321
|
+
return !Object.hasOwn(value, key) || value[key] === null || isAnswerCardText(value[key], budget);
|
|
322
|
+
}
|
|
323
|
+
function hasOptionalNullableMarkdown(value, key, budget) {
|
|
324
|
+
if (!Object.hasOwn(value, key) || value[key] === null) return true;
|
|
325
|
+
const text = value[key];
|
|
326
|
+
if (!isAnswerCardText(text, budget)) return false;
|
|
327
|
+
return consumeStructuralItems(budget, countMarkdownStructuralItems(text));
|
|
328
|
+
}
|
|
329
|
+
function hasOptionalBoolean(value, key) {
|
|
330
|
+
return !Object.hasOwn(value, key) || typeof value[key] === "boolean";
|
|
331
|
+
}
|
|
332
|
+
function isLink(value, budget) {
|
|
333
|
+
return isPlainObject(value) && hasOnlyKeys(value, LINK_KEYS) && isAnswerCardText(value.label, budget) && typeof value.url === "string" && value.url.length <= 2048 && consumeTextCharacters(budget, value.url.length) && consumeStructuralItems(budget, 1);
|
|
334
|
+
}
|
|
335
|
+
function isFact(value, budget) {
|
|
336
|
+
return isPlainObject(value) && hasOnlyKeys(value, FACT_KEYS) && isAnswerCardText(value.label, budget) && isAnswerCardText(value.value, budget) && consumeStructuralItems(budget, 1);
|
|
337
|
+
}
|
|
338
|
+
function isTextArray(value, maximumLength, budget) {
|
|
339
|
+
if (!Array.isArray(value) || value.length > maximumLength) return false;
|
|
340
|
+
for (const item of value) if (!isAnswerCardText(item, budget)) return false;
|
|
341
|
+
return true;
|
|
342
|
+
}
|
|
343
|
+
function isBoundedArray(value, maximumLength, guard) {
|
|
344
|
+
if (!Array.isArray(value) || value.length > maximumLength) return false;
|
|
345
|
+
for (const item of value) if (!guard(item)) return false;
|
|
346
|
+
return true;
|
|
347
|
+
}
|
|
348
|
+
function isTable(value, budget) {
|
|
349
|
+
if (!isPlainObject(value) || !hasOnlyKeys(value, TABLE_KEYS) || !hasOptionalNullableText(value, "label", budget)) return false;
|
|
350
|
+
const headers = value.headers;
|
|
351
|
+
const rows = value.rows;
|
|
352
|
+
if (!isTextArray(headers, 12, budget) || headers.length === 0 || !Array.isArray(rows) || !isBoundedArray(rows, 50, (row) => isTextArray(row, 12, budget) && row.length === headers.length)) return false;
|
|
353
|
+
return consumeStructuralItems(budget, rows.length + 2) && consumeTableCells(budget, headers.length * (rows.length + 1));
|
|
354
|
+
}
|
|
355
|
+
function hasOptionalNullableArray(value, key, maximumLength, guard) {
|
|
356
|
+
return !Object.hasOwn(value, key) || value[key] === null || isBoundedArray(value[key], maximumLength, guard);
|
|
357
|
+
}
|
|
358
|
+
function isSection(value, budget) {
|
|
359
|
+
if (!isPlainObject(value) || !hasOnlyKeys(value, SECTION_KEYS) || !consumeStructuralItems(budget, 1)) return false;
|
|
360
|
+
return hasOptionalNullableText(value, "title", budget) && hasOptionalNullableMarkdown(value, "text", budget) && hasOptionalNullableArray(value, "facts", 24, (fact) => isFact(fact, budget)) && hasOptionalNullableArray(value, "tables", 8, (table) => isTable(table, budget)) && hasOptionalNullableArray(value, "links", 24, (link) => isLink(link, budget)) && hasOptionalNullableText(value, "note", budget) && hasOptionalBoolean(value, "collapsible");
|
|
361
|
+
}
|
|
362
|
+
function isAnswerCard(value) {
|
|
363
|
+
const budget = {
|
|
364
|
+
textCharacters: 0,
|
|
365
|
+
structuralItems: 0,
|
|
366
|
+
tableCells: 0
|
|
367
|
+
};
|
|
368
|
+
return isPlainObject(value) && hasOnlyKeys(value, ANSWER_CARD_KEYS) && value.kind === "answer" && consumeStructuralItems(budget, 1) && isAnswerCardText(value.title, budget) && hasOptionalNullableMarkdown(value, "summary", budget) && (!Object.hasOwn(value, "highlight") || value.highlight === null || isFact(value.highlight, budget) && hasOnlyKeys(value.highlight, HIGHLIGHT_KEYS)) && hasOptionalNullableArray(value, "sections", 12, (section) => isSection(section, budget)) && hasOptionalNullableText(value, "note", budget) && hasOptionalNullableArray(value, "actions", 24, (link) => isLink(link, budget)) && hasOptionalNullableArray(value, "sources", 24, (link) => isLink(link, budget)) && hasOptionalNullableText(value, "sourceNote", budget);
|
|
369
|
+
}
|
|
370
|
+
function appendMarkdown(document, parent, text) {
|
|
371
|
+
if (text !== null && text !== void 0) parent.append(renderMarkdown(document, text));
|
|
372
|
+
}
|
|
373
|
+
function appendFacts(document, parent, facts) {
|
|
374
|
+
if (facts.length === 0) return;
|
|
375
|
+
const definitions = document.createElement("dl");
|
|
376
|
+
for (const fact of facts) {
|
|
377
|
+
const term = document.createElement("dt");
|
|
378
|
+
term.append(document.createTextNode(fact.label));
|
|
379
|
+
const definition = document.createElement("dd");
|
|
380
|
+
definition.append(document.createTextNode(fact.value));
|
|
381
|
+
definitions.append(term, definition);
|
|
382
|
+
}
|
|
383
|
+
parent.append(definitions);
|
|
384
|
+
}
|
|
385
|
+
function appendLinks(document, parent, links) {
|
|
386
|
+
if (links.length === 0) return;
|
|
387
|
+
const list = document.createElement("ul");
|
|
388
|
+
for (const item of links) {
|
|
389
|
+
const listItem = document.createElement("li");
|
|
390
|
+
const link = createSafeLink(document, item.label, item.url);
|
|
391
|
+
listItem.append(link ?? document.createTextNode(item.label));
|
|
392
|
+
list.append(listItem);
|
|
393
|
+
}
|
|
394
|
+
parent.append(list);
|
|
395
|
+
}
|
|
396
|
+
function appendNote(document, parent, note) {
|
|
397
|
+
if (note === null || note === void 0) return;
|
|
398
|
+
const paragraph = document.createElement("p");
|
|
399
|
+
paragraph.append(document.createTextNode(note));
|
|
400
|
+
parent.append(paragraph);
|
|
401
|
+
}
|
|
402
|
+
function createTable(document, table) {
|
|
403
|
+
const element = document.createElement("table");
|
|
404
|
+
if (table.headers.length > 0) {
|
|
405
|
+
const head = document.createElement("thead");
|
|
406
|
+
const row = document.createElement("tr");
|
|
407
|
+
for (const header of table.headers) {
|
|
408
|
+
const cell = document.createElement("th");
|
|
409
|
+
cell.scope = "col";
|
|
410
|
+
cell.append(document.createTextNode(header));
|
|
411
|
+
row.append(cell);
|
|
412
|
+
}
|
|
413
|
+
head.append(row);
|
|
414
|
+
element.append(head);
|
|
415
|
+
}
|
|
416
|
+
if (table.rows.length > 0) {
|
|
417
|
+
const body = document.createElement("tbody");
|
|
418
|
+
for (const cells of table.rows) {
|
|
419
|
+
const row = document.createElement("tr");
|
|
420
|
+
for (const value of cells) {
|
|
421
|
+
const cell = document.createElement("td");
|
|
422
|
+
cell.append(document.createTextNode(value));
|
|
423
|
+
row.append(cell);
|
|
424
|
+
}
|
|
425
|
+
body.append(row);
|
|
426
|
+
}
|
|
427
|
+
element.append(body);
|
|
428
|
+
}
|
|
429
|
+
return element;
|
|
430
|
+
}
|
|
431
|
+
function tableRegionBaseName(table, sectionTitle, labels) {
|
|
432
|
+
return table.label?.trim() || sectionTitle?.trim() || labels.table;
|
|
433
|
+
}
|
|
434
|
+
function tableRegionNames(sections, labels) {
|
|
435
|
+
const counts = /* @__PURE__ */ new Map();
|
|
436
|
+
for (const section of sections) for (const table of section.tables ?? []) {
|
|
437
|
+
const name = tableRegionBaseName(table, section.title, labels);
|
|
438
|
+
counts.set(name, (counts.get(name) ?? 0) + 1);
|
|
439
|
+
}
|
|
440
|
+
return {
|
|
441
|
+
counts,
|
|
442
|
+
ordinals: /* @__PURE__ */ new Map()
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
function appendTables(document, parent, tables, sectionTitle, labels, names) {
|
|
446
|
+
for (const table of tables) {
|
|
447
|
+
const name = tableRegionBaseName(table, sectionTitle, labels);
|
|
448
|
+
const ordinal = (names.ordinals.get(name) ?? 0) + 1;
|
|
449
|
+
names.ordinals.set(name, ordinal);
|
|
450
|
+
const element = createTable(document, table);
|
|
451
|
+
const region = document.createElement("div");
|
|
452
|
+
region.className = "vv-ai-answer-card__table-region";
|
|
453
|
+
region.setAttribute("data-ai-table-region", "true");
|
|
454
|
+
region.setAttribute("role", "region");
|
|
455
|
+
region.setAttribute("tabindex", "0");
|
|
456
|
+
region.setAttribute("aria-label", (names.counts.get(name) ?? 0) > 1 ? `${name} (${ordinal})` : name);
|
|
457
|
+
region.style.overflowX = "auto";
|
|
458
|
+
const hint = document.createElement("p");
|
|
459
|
+
hint.append(document.createTextNode(labels.tableScroll));
|
|
460
|
+
region.append(hint);
|
|
461
|
+
region.append(element);
|
|
462
|
+
parent.append(region);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
function appendSection(document, article, section, labels, names) {
|
|
466
|
+
const content = document.createElement("section");
|
|
467
|
+
if (!section.collapsible && section.title !== null && section.title !== void 0) {
|
|
468
|
+
const heading = document.createElement("h3");
|
|
469
|
+
heading.append(document.createTextNode(section.title));
|
|
470
|
+
content.append(heading);
|
|
471
|
+
}
|
|
472
|
+
appendMarkdown(document, content, section.text);
|
|
473
|
+
appendFacts(document, content, section.facts ?? []);
|
|
474
|
+
appendTables(document, content, section.tables ?? [], section.title, labels, names);
|
|
475
|
+
appendLinks(document, content, section.links ?? []);
|
|
476
|
+
appendNote(document, content, section.note);
|
|
477
|
+
if (!section.collapsible) {
|
|
478
|
+
article.append(content);
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
const details = document.createElement("details");
|
|
482
|
+
const summary = document.createElement("summary");
|
|
483
|
+
summary.append(document.createTextNode(section.title?.trim() ? section.title : labels.details));
|
|
484
|
+
details.append(summary, content);
|
|
485
|
+
article.append(details);
|
|
486
|
+
}
|
|
487
|
+
function renderKnownAnswerCard(document, card, labels) {
|
|
488
|
+
const article = document.createElement("article");
|
|
489
|
+
const heading = document.createElement("h2");
|
|
490
|
+
heading.append(document.createTextNode(card.title));
|
|
491
|
+
article.append(heading);
|
|
492
|
+
appendMarkdown(document, article, card.summary);
|
|
493
|
+
if (card.highlight) appendFacts(document, article, [card.highlight]);
|
|
494
|
+
const names = tableRegionNames(card.sections ?? [], labels);
|
|
495
|
+
for (const section of card.sections ?? []) appendSection(document, article, section, labels, names);
|
|
496
|
+
appendNote(document, article, card.note);
|
|
497
|
+
appendLinks(document, article, card.actions ?? []);
|
|
498
|
+
if ((card.sources?.length ?? 0) > 0 || card.sourceNote !== null && card.sourceNote !== void 0) {
|
|
499
|
+
const sources = document.createElement("section");
|
|
500
|
+
const sourceHeading = document.createElement("h3");
|
|
501
|
+
sourceHeading.append(document.createTextNode(labels.sources));
|
|
502
|
+
sources.append(sourceHeading);
|
|
503
|
+
appendLinks(document, sources, card.sources ?? []);
|
|
504
|
+
appendNote(document, sources, card.sourceNote);
|
|
505
|
+
article.append(sources);
|
|
506
|
+
}
|
|
507
|
+
return article;
|
|
508
|
+
}
|
|
509
|
+
function scalarValue(value) {
|
|
510
|
+
if (typeof value === "string") return value.length <= 2e3 && value.trim().length > 0 ? value : null;
|
|
511
|
+
if (typeof value === "number") return Number.isFinite(value) ? String(value) : null;
|
|
512
|
+
return typeof value === "boolean" ? String(value) : null;
|
|
513
|
+
}
|
|
514
|
+
function humanizePropertyName(name) {
|
|
515
|
+
if (name.length > 2e3) return null;
|
|
516
|
+
const words = name.replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/[_-]+/g, " ").trim();
|
|
517
|
+
return words.length === 0 ? null : `${words[0].toUpperCase()}${words.slice(1)}`;
|
|
518
|
+
}
|
|
519
|
+
function genericFields(value) {
|
|
520
|
+
const fields = [];
|
|
521
|
+
let propertyCount = 0;
|
|
522
|
+
for (const key in value) {
|
|
523
|
+
if (!Object.hasOwn(value, key)) continue;
|
|
524
|
+
propertyCount += 1;
|
|
525
|
+
if (propertyCount > 24) break;
|
|
526
|
+
const label = humanizePropertyName(key);
|
|
527
|
+
const scalar = scalarValue(value[key]);
|
|
528
|
+
if (label !== null && scalar !== null) {
|
|
529
|
+
fields.push({
|
|
530
|
+
label,
|
|
531
|
+
value: scalar
|
|
532
|
+
});
|
|
533
|
+
if (fields.length === 12) break;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
return fields;
|
|
537
|
+
}
|
|
538
|
+
function genericEntries(data) {
|
|
539
|
+
if (isPlainObject(data)) {
|
|
540
|
+
const fields = genericFields(data);
|
|
541
|
+
return fields.length > 0 ? [fields] : [];
|
|
542
|
+
}
|
|
543
|
+
if (!Array.isArray(data) || data.length > 20) return [];
|
|
544
|
+
const entries = [];
|
|
545
|
+
for (const item of data) if (isPlainObject(item)) {
|
|
546
|
+
const fields = genericFields(item);
|
|
547
|
+
if (fields.length > 0) entries.push(fields);
|
|
548
|
+
if (entries.length === 5) break;
|
|
549
|
+
}
|
|
550
|
+
return entries;
|
|
551
|
+
}
|
|
552
|
+
function renderGenericAnswerCard(document, data) {
|
|
553
|
+
const entries = genericEntries(data);
|
|
554
|
+
if (entries.length === 0) return null;
|
|
555
|
+
const article = document.createElement("article");
|
|
556
|
+
const list = document.createElement("ul");
|
|
557
|
+
for (const fields of entries) {
|
|
558
|
+
const item = document.createElement("li");
|
|
559
|
+
appendFacts(document, item, fields);
|
|
560
|
+
list.append(item);
|
|
561
|
+
}
|
|
562
|
+
article.append(list);
|
|
563
|
+
return article;
|
|
564
|
+
}
|
|
565
|
+
function renderAnswerCard(document, data, labels) {
|
|
566
|
+
if (isPlainObject(data) && data.kind === "answer") return isAnswerCard(data) ? renderKnownAnswerCard(document, data, labels) : null;
|
|
567
|
+
return renderGenericAnswerCard(document, data);
|
|
568
|
+
}
|
|
569
|
+
//#endregion
|
|
570
|
+
//#region src/protocol.ts
|
|
571
|
+
const AI_CHAT_PROTOCOL_VERSION = 1;
|
|
572
|
+
function isRecord(value) {
|
|
573
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
574
|
+
}
|
|
575
|
+
function stringValue(data, key) {
|
|
576
|
+
const value = data[key];
|
|
577
|
+
return typeof value === "string" ? value : null;
|
|
578
|
+
}
|
|
579
|
+
function parseStreamEvent(name, data) {
|
|
580
|
+
if (!isRecord(data)) return null;
|
|
581
|
+
switch (name) {
|
|
582
|
+
case "delta": {
|
|
583
|
+
const text = stringValue(data, "text");
|
|
584
|
+
return text === null ? null : {
|
|
585
|
+
name,
|
|
586
|
+
data: { text }
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
case "tool": {
|
|
590
|
+
const id = stringValue(data, "id");
|
|
591
|
+
const toolName = stringValue(data, "name");
|
|
592
|
+
return id === null || toolName === null ? null : {
|
|
593
|
+
name,
|
|
594
|
+
data: {
|
|
595
|
+
id,
|
|
596
|
+
name: toolName
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
case "tool_result": {
|
|
601
|
+
const id = stringValue(data, "id");
|
|
602
|
+
const toolName = stringValue(data, "name");
|
|
603
|
+
const reply = data.reply;
|
|
604
|
+
if (id === null || toolName === null || !Object.hasOwn(data, "data") || typeof reply !== "string" && reply !== null) return null;
|
|
605
|
+
return {
|
|
606
|
+
name,
|
|
607
|
+
data: {
|
|
608
|
+
id,
|
|
609
|
+
name: toolName,
|
|
610
|
+
data: data.data,
|
|
611
|
+
reply
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
}
|
|
615
|
+
case "tool_done": {
|
|
616
|
+
const id = stringValue(data, "id");
|
|
617
|
+
const toolName = stringValue(data, "name");
|
|
618
|
+
const isError = data.isError;
|
|
619
|
+
return id === null || toolName === null || typeof isError !== "boolean" ? null : {
|
|
620
|
+
name,
|
|
621
|
+
data: {
|
|
622
|
+
id,
|
|
623
|
+
name: toolName,
|
|
624
|
+
isError
|
|
625
|
+
}
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
case "done": {
|
|
629
|
+
const provider = stringValue(data, "provider");
|
|
630
|
+
return provider === null ? null : {
|
|
631
|
+
name,
|
|
632
|
+
data: { provider }
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
case "error": {
|
|
636
|
+
const message = stringValue(data, "message");
|
|
637
|
+
return message === null ? null : {
|
|
638
|
+
name,
|
|
639
|
+
data: { message }
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
default: return null;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
//#endregion
|
|
646
|
+
//#region src/stream.ts
|
|
647
|
+
function findRecordBoundary(buffer) {
|
|
648
|
+
const lfIndex = buffer.indexOf("\n\n");
|
|
649
|
+
const crlfIndex = buffer.indexOf("\r\n\r\n");
|
|
650
|
+
if (lfIndex === -1 && crlfIndex === -1) return null;
|
|
651
|
+
if (lfIndex === -1 || crlfIndex !== -1 && crlfIndex < lfIndex) return {
|
|
652
|
+
index: crlfIndex,
|
|
653
|
+
length: 4
|
|
654
|
+
};
|
|
655
|
+
return {
|
|
656
|
+
index: lfIndex,
|
|
657
|
+
length: 2
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
function fieldValue(value) {
|
|
661
|
+
return value.startsWith(" ") ? value.slice(1) : value;
|
|
662
|
+
}
|
|
663
|
+
function parseRecord(record) {
|
|
664
|
+
let name;
|
|
665
|
+
const dataLines = [];
|
|
666
|
+
for (const line of record.split(/\r?\n/)) if (line.startsWith("event:")) name = fieldValue(line.slice(6));
|
|
667
|
+
else if (line.startsWith("data:")) dataLines.push(fieldValue(line.slice(5)));
|
|
668
|
+
if (!name || dataLines.length === 0) return null;
|
|
669
|
+
try {
|
|
670
|
+
return parseStreamEvent(name, JSON.parse(dataLines.join("\n")));
|
|
671
|
+
} catch {
|
|
672
|
+
return null;
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
function abortError$1() {
|
|
676
|
+
return /* @__PURE__ */ new Error("AI chat event stream was aborted");
|
|
677
|
+
}
|
|
678
|
+
async function* readEventStream(response, signal) {
|
|
679
|
+
if (!response.body) throw new Error("AI chat response has no body");
|
|
680
|
+
if (signal.aborted) throw abortError$1();
|
|
681
|
+
const decoder = new TextDecoder();
|
|
682
|
+
const reader = response.body.getReader();
|
|
683
|
+
let buffer = "";
|
|
684
|
+
let readerDone = false;
|
|
685
|
+
const cancelReader = () => {
|
|
686
|
+
reader.cancel().catch(() => void 0);
|
|
687
|
+
};
|
|
688
|
+
signal.addEventListener("abort", cancelReader, { once: true });
|
|
689
|
+
try {
|
|
690
|
+
while (true) {
|
|
691
|
+
if (signal.aborted) throw abortError$1();
|
|
692
|
+
const { done, value } = await reader.read();
|
|
693
|
+
if (signal.aborted) throw abortError$1();
|
|
694
|
+
if (done) {
|
|
695
|
+
readerDone = true;
|
|
696
|
+
buffer += decoder.decode();
|
|
697
|
+
} else buffer += decoder.decode(value, { stream: true });
|
|
698
|
+
let boundary = findRecordBoundary(buffer);
|
|
699
|
+
while (boundary) {
|
|
700
|
+
const event = parseRecord(buffer.slice(0, boundary.index));
|
|
701
|
+
buffer = buffer.slice(boundary.index + boundary.length);
|
|
702
|
+
if (event) {
|
|
703
|
+
if (event.name === "done" || event.name === "error") {
|
|
704
|
+
yield event;
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
yield event;
|
|
708
|
+
}
|
|
709
|
+
boundary = findRecordBoundary(buffer);
|
|
710
|
+
}
|
|
711
|
+
if (readerDone) throw new Error("AI chat event stream ended before a terminal event");
|
|
712
|
+
}
|
|
713
|
+
} finally {
|
|
714
|
+
signal.removeEventListener("abort", cancelReader);
|
|
715
|
+
if (!readerDone) try {
|
|
716
|
+
await reader.cancel();
|
|
717
|
+
} catch {}
|
|
718
|
+
reader.releaseLock();
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
//#endregion
|
|
722
|
+
//#region src/chat.ts
|
|
723
|
+
const DEFAULT_TIMEOUT_MS = 6e4;
|
|
724
|
+
function abortError() {
|
|
725
|
+
return /* @__PURE__ */ new Error("AI chat request was canceled");
|
|
726
|
+
}
|
|
727
|
+
function createChatSession(options) {
|
|
728
|
+
const history = [];
|
|
729
|
+
const context = {};
|
|
730
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
731
|
+
let status = "idle";
|
|
732
|
+
let controller = null;
|
|
733
|
+
let timeout = null;
|
|
734
|
+
const session = {
|
|
735
|
+
get history() {
|
|
736
|
+
return history;
|
|
737
|
+
},
|
|
738
|
+
get context() {
|
|
739
|
+
return context;
|
|
740
|
+
},
|
|
741
|
+
get status() {
|
|
742
|
+
return status;
|
|
743
|
+
},
|
|
744
|
+
async send(message) {
|
|
745
|
+
const content = message.trim();
|
|
746
|
+
if (!content) throw new Error("AI chat messages cannot be empty");
|
|
747
|
+
if (status === "disposed") throw new Error("AI chat session is disposed");
|
|
748
|
+
if (controller) throw new Error("AI chat request is already active");
|
|
749
|
+
const requestController = new AbortController();
|
|
750
|
+
controller = requestController;
|
|
751
|
+
status = "sending";
|
|
752
|
+
const fetcher = options.fetch ?? fetch;
|
|
753
|
+
const headers = {
|
|
754
|
+
"Content-Type": "application/json",
|
|
755
|
+
...options.headers
|
|
756
|
+
};
|
|
757
|
+
const requestHistory = [...history, {
|
|
758
|
+
role: "user",
|
|
759
|
+
content
|
|
760
|
+
}];
|
|
761
|
+
let provider = null;
|
|
762
|
+
let error = null;
|
|
763
|
+
let canceled = false;
|
|
764
|
+
let answer = "";
|
|
765
|
+
let replies = [];
|
|
766
|
+
timeout = setTimeout(() => requestController.abort(new DOMException("AI chat request timed out", "TimeoutError")), options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
|
767
|
+
try {
|
|
768
|
+
const response = await fetcher(options.endpoint, {
|
|
769
|
+
method: "POST",
|
|
770
|
+
headers,
|
|
771
|
+
body: JSON.stringify({
|
|
772
|
+
messages: requestHistory,
|
|
773
|
+
provider: options.provider,
|
|
774
|
+
context
|
|
775
|
+
}),
|
|
776
|
+
signal: requestController.signal
|
|
777
|
+
});
|
|
778
|
+
if (!response.ok) throw new Error(`AI chat request failed with ${response.status}`);
|
|
779
|
+
for await (const event of readEventStream(response, requestController.signal)) {
|
|
780
|
+
options.onEvent?.(event);
|
|
781
|
+
for (const listener of listeners) listener(event);
|
|
782
|
+
if (event.name === "delta") answer += event.data.text;
|
|
783
|
+
else if (event.name === "tool_result" && event.data.reply) replies.push(event.data.reply);
|
|
784
|
+
else if (event.name === "done") provider = event.data.provider;
|
|
785
|
+
else if (event.name === "error") throw new Error(event.data.message);
|
|
786
|
+
}
|
|
787
|
+
if (answer.trim()) replies = [...replies, answer.trim()];
|
|
788
|
+
history.push({
|
|
789
|
+
role: "user",
|
|
790
|
+
content
|
|
791
|
+
}, ...replies.map((reply) => ({
|
|
792
|
+
role: "assistant",
|
|
793
|
+
content: reply
|
|
794
|
+
})));
|
|
795
|
+
} catch (caught) {
|
|
796
|
+
canceled = requestController.signal.aborted;
|
|
797
|
+
error = caught instanceof Error ? caught : /* @__PURE__ */ new Error("AI chat request failed");
|
|
798
|
+
if (canceled && requestController.signal.reason?.name !== "TimeoutError") error = abortError();
|
|
799
|
+
} finally {
|
|
800
|
+
if (timeout) clearTimeout(timeout);
|
|
801
|
+
timeout = null;
|
|
802
|
+
if (controller === requestController) controller = null;
|
|
803
|
+
if (session.status !== "disposed") status = "idle";
|
|
804
|
+
}
|
|
805
|
+
return {
|
|
806
|
+
provider,
|
|
807
|
+
canceled,
|
|
808
|
+
error
|
|
809
|
+
};
|
|
810
|
+
},
|
|
811
|
+
cancel() {
|
|
812
|
+
controller?.abort();
|
|
813
|
+
},
|
|
814
|
+
reset() {
|
|
815
|
+
session.cancel();
|
|
816
|
+
history.splice(0);
|
|
817
|
+
for (const key of Object.keys(context)) delete context[key];
|
|
818
|
+
},
|
|
819
|
+
setContext(name, value) {
|
|
820
|
+
if (!name.trim()) throw new Error("AI chat context names cannot be empty");
|
|
821
|
+
context[name] = value;
|
|
822
|
+
},
|
|
823
|
+
clearContext(name) {
|
|
824
|
+
delete context[name];
|
|
825
|
+
},
|
|
826
|
+
subscribe(listener) {
|
|
827
|
+
listeners.add(listener);
|
|
828
|
+
return () => listeners.delete(listener);
|
|
829
|
+
},
|
|
830
|
+
dispose() {
|
|
831
|
+
session.cancel();
|
|
832
|
+
listeners.clear();
|
|
833
|
+
status = "disposed";
|
|
834
|
+
}
|
|
835
|
+
};
|
|
836
|
+
return session;
|
|
837
|
+
}
|
|
838
|
+
//#endregion
|
|
839
|
+
//#region src/registry.ts
|
|
840
|
+
const entries = /* @__PURE__ */ new WeakMap();
|
|
841
|
+
function register(map, name, value) {
|
|
842
|
+
if (!name.trim()) throw new Error("AI chat registry names cannot be empty");
|
|
843
|
+
map.set(name, value);
|
|
844
|
+
return () => {
|
|
845
|
+
if (map.get(name) === value) map.delete(name);
|
|
846
|
+
};
|
|
847
|
+
}
|
|
848
|
+
function createAiChatRegistry() {
|
|
849
|
+
const state = {
|
|
850
|
+
renderers: /* @__PURE__ */ new Map(),
|
|
851
|
+
surfaces: /* @__PURE__ */ new Map(),
|
|
852
|
+
actions: /* @__PURE__ */ new Map()
|
|
853
|
+
};
|
|
854
|
+
const registry = {
|
|
855
|
+
registerRenderer: (name, renderer) => register(state.renderers, name, renderer),
|
|
856
|
+
registerSurface: (name, surface) => register(state.surfaces, name, surface),
|
|
857
|
+
registerAction: (name, action) => register(state.actions, name, action)
|
|
858
|
+
};
|
|
859
|
+
entries.set(registry, state);
|
|
860
|
+
return registry;
|
|
861
|
+
}
|
|
862
|
+
function registryEntries(registry) {
|
|
863
|
+
return registry ? entries.get(registry) : void 0;
|
|
864
|
+
}
|
|
865
|
+
//#endregion
|
|
866
|
+
//#region src/bind.ts
|
|
867
|
+
const defaultLabels = {
|
|
868
|
+
error: "Something went wrong. Please try again.",
|
|
869
|
+
timeout: "The reply took too long. Please try again.",
|
|
870
|
+
toolRunning: "Looking that up",
|
|
871
|
+
toolComplete: "Done",
|
|
872
|
+
toolFailed: "Unavailable",
|
|
873
|
+
toolInterrupted: "Lookup interrupted",
|
|
874
|
+
sources: "Sources",
|
|
875
|
+
details: "Details",
|
|
876
|
+
table: "Published table",
|
|
877
|
+
tableScroll: "Scroll sideways to see all columns"
|
|
878
|
+
};
|
|
879
|
+
function dataString(data, name) {
|
|
880
|
+
if (typeof data !== "object" || data === null || Array.isArray(data)) return null;
|
|
881
|
+
const value = data[name];
|
|
882
|
+
return typeof value === "string" && value.length > 0 && value.length <= 128 ? value : null;
|
|
883
|
+
}
|
|
884
|
+
function bindChat(root, options = {}) {
|
|
885
|
+
const form = root.querySelector("[data-ai-chat-form]");
|
|
886
|
+
const input = root.querySelector("[data-ai-chat-input]");
|
|
887
|
+
const thread = root.querySelector("[data-ai-chat-thread]");
|
|
888
|
+
const send = root.querySelector("[data-ai-chat-send]");
|
|
889
|
+
if (!form || !input || !thread) throw new Error("AI chat shell requires form, input and thread elements");
|
|
890
|
+
const chatThread = thread;
|
|
891
|
+
const labels = {
|
|
892
|
+
...defaultLabels,
|
|
893
|
+
...options.labels
|
|
894
|
+
};
|
|
895
|
+
const endpoint = options.endpoint ?? root.dataset.aiChatEndpoint ?? form.action;
|
|
896
|
+
const header = options.antiforgeryHeader ?? root.dataset.aiChatAntiforgeryHeader;
|
|
897
|
+
const token = options.antiforgeryToken ?? root.dataset.aiChatAntiforgeryToken;
|
|
898
|
+
const headers = header && token ? { [header]: token } : void 0;
|
|
899
|
+
let activeAssistant = null;
|
|
900
|
+
let destroyed = false;
|
|
901
|
+
const session = createChatSession({
|
|
902
|
+
endpoint,
|
|
903
|
+
provider: options.provider ?? root.dataset.aiChatProvider,
|
|
904
|
+
headers,
|
|
905
|
+
timeoutMs: options.timeoutMs
|
|
906
|
+
});
|
|
907
|
+
function appendMessage(role) {
|
|
908
|
+
const message = root.ownerDocument.createElement("div");
|
|
909
|
+
message.dataset.aiChatMessage = role;
|
|
910
|
+
chatThread.append(message);
|
|
911
|
+
return message;
|
|
912
|
+
}
|
|
913
|
+
function appendMarkdown(parent, text) {
|
|
914
|
+
const bubble = root.ownerDocument.createElement("div");
|
|
915
|
+
bubble.dataset.aiChatText = "true";
|
|
916
|
+
bubble.append(renderMarkdown(root.ownerDocument, text));
|
|
917
|
+
parent.append(bubble);
|
|
918
|
+
}
|
|
919
|
+
function report(error) {
|
|
920
|
+
options.onError?.(error);
|
|
921
|
+
const message = root.ownerDocument.createElement("p");
|
|
922
|
+
message.setAttribute("role", "alert");
|
|
923
|
+
message.append(error.name === "TimeoutError" ? labels.timeout : labels.error);
|
|
924
|
+
chatThread.append(message);
|
|
925
|
+
}
|
|
926
|
+
function tool(id, _name) {
|
|
927
|
+
const element = root.ownerDocument.createElement("p");
|
|
928
|
+
element.dataset.aiChatTool = id;
|
|
929
|
+
element.append(root.ownerDocument.createTextNode(labels.toolRunning));
|
|
930
|
+
activeAssistant?.append(element);
|
|
931
|
+
return element;
|
|
932
|
+
}
|
|
933
|
+
function finishTool(id, failed, interrupted = false) {
|
|
934
|
+
const element = activeAssistant?.querySelector(`[data-ai-chat-tool="${CSS.escape(id)}"]`);
|
|
935
|
+
if (element) element.textContent = interrupted ? labels.toolInterrupted : failed ? labels.toolFailed : labels.toolComplete;
|
|
936
|
+
}
|
|
937
|
+
function renderResult(event) {
|
|
938
|
+
if (!activeAssistant) return;
|
|
939
|
+
const context = {
|
|
940
|
+
document: root.ownerDocument,
|
|
941
|
+
root,
|
|
942
|
+
tool: event.data.name,
|
|
943
|
+
data: event.data.data,
|
|
944
|
+
reply: event.data.reply
|
|
945
|
+
};
|
|
946
|
+
let content = null;
|
|
947
|
+
try {
|
|
948
|
+
const registered = registryEntries(options.registry)?.renderers.get(event.data.name);
|
|
949
|
+
content = registered?.(context) ?? null;
|
|
950
|
+
const templateName = dataString(event.data.data, "template");
|
|
951
|
+
const surfaceName = dataString(event.data.data, "surface");
|
|
952
|
+
if (!content && templateName) content = root.querySelector(`template[data-template="${CSS.escape(templateName)}"]`)?.content.cloneNode(true) ?? null;
|
|
953
|
+
if (!content && surfaceName) content = registryEntries(options.registry)?.surfaces.get(surfaceName)?.(context) ?? root.querySelector(`template[data-ai-surface="${CSS.escape(surfaceName)}"]`)?.content.cloneNode(true) ?? null;
|
|
954
|
+
content ??= renderAnswerCard(root.ownerDocument, event.data.data, labels);
|
|
955
|
+
} catch (caught) {
|
|
956
|
+
options.onError?.(caught instanceof Error ? caught : /* @__PURE__ */ new Error("AI chat extension failed"));
|
|
957
|
+
}
|
|
958
|
+
if (content) {
|
|
959
|
+
const wrapper = root.ownerDocument.createElement("div");
|
|
960
|
+
wrapper.dataset.aiChatCard = "true";
|
|
961
|
+
wrapper.append(content);
|
|
962
|
+
activeAssistant.append(wrapper);
|
|
963
|
+
options.onEmbedded?.(wrapper);
|
|
964
|
+
} else if (event.data.reply) appendMarkdown(activeAssistant, event.data.reply);
|
|
965
|
+
}
|
|
966
|
+
const unsubscribe = session.subscribe((event) => {
|
|
967
|
+
if (destroyed) return;
|
|
968
|
+
if (event.name === "delta" && activeAssistant) appendMarkdown(activeAssistant, event.data.text);
|
|
969
|
+
else if (event.name === "tool") tool(event.data.id, event.data.name);
|
|
970
|
+
else if (event.name === "tool_result") renderResult(event);
|
|
971
|
+
else if (event.name === "tool_done") finishTool(event.data.id, event.data.isError);
|
|
972
|
+
});
|
|
973
|
+
const submit = async (event) => {
|
|
974
|
+
event?.preventDefault();
|
|
975
|
+
const question = input.value.trim();
|
|
976
|
+
if (!question || session.status !== "idle") return;
|
|
977
|
+
input.value = "";
|
|
978
|
+
appendMarkdown(appendMessage("user"), question);
|
|
979
|
+
activeAssistant = appendMessage("assistant");
|
|
980
|
+
root.setAttribute("aria-busy", "true");
|
|
981
|
+
if (send) send.disabled = true;
|
|
982
|
+
const result = await session.send(question);
|
|
983
|
+
root.removeAttribute("aria-busy");
|
|
984
|
+
if (send) send.disabled = false;
|
|
985
|
+
if (result.error && !result.canceled) report(result.error);
|
|
986
|
+
if (result.canceled) activeAssistant?.querySelectorAll("[data-ai-chat-tool]").forEach((item) => {
|
|
987
|
+
item.textContent = labels.toolInterrupted;
|
|
988
|
+
});
|
|
989
|
+
};
|
|
990
|
+
const click = (event) => {
|
|
991
|
+
const target = event.target instanceof Element ? event.target : null;
|
|
992
|
+
const suggestion = target?.closest("[data-ai-chat-suggestion]");
|
|
993
|
+
if (suggestion) {
|
|
994
|
+
input.value = suggestion.dataset.aiChatSuggestion ?? suggestion.textContent?.trim() ?? "";
|
|
995
|
+
submit();
|
|
996
|
+
return;
|
|
997
|
+
}
|
|
998
|
+
if (target?.closest("[data-ai-chat-reset]")) {
|
|
999
|
+
session.reset();
|
|
1000
|
+
chatThread.replaceChildren();
|
|
1001
|
+
input.value = "";
|
|
1002
|
+
return;
|
|
1003
|
+
}
|
|
1004
|
+
const actionElement = target?.closest("[data-ai-action]");
|
|
1005
|
+
const action = actionElement ? registryEntries(options.registry)?.actions.get(actionElement.dataset.aiAction ?? "") : void 0;
|
|
1006
|
+
if (action && actionElement) Promise.resolve(action({
|
|
1007
|
+
root,
|
|
1008
|
+
element: actionElement,
|
|
1009
|
+
value: actionElement.dataset.aiActionValue ?? null,
|
|
1010
|
+
session
|
|
1011
|
+
})).then((result) => {
|
|
1012
|
+
if (result) session.setContext(...result.context);
|
|
1013
|
+
}).catch(report);
|
|
1014
|
+
};
|
|
1015
|
+
const keydown = (event) => {
|
|
1016
|
+
if (event.key === "Enter" && !event.shiftKey) {
|
|
1017
|
+
event.preventDefault();
|
|
1018
|
+
submit();
|
|
1019
|
+
}
|
|
1020
|
+
};
|
|
1021
|
+
form.addEventListener("submit", submit);
|
|
1022
|
+
root.addEventListener("click", click);
|
|
1023
|
+
input.addEventListener("keydown", keydown);
|
|
1024
|
+
return {
|
|
1025
|
+
session,
|
|
1026
|
+
destroy() {
|
|
1027
|
+
if (destroyed) return;
|
|
1028
|
+
destroyed = true;
|
|
1029
|
+
unsubscribe();
|
|
1030
|
+
session.dispose();
|
|
1031
|
+
form.removeEventListener("submit", submit);
|
|
1032
|
+
root.removeEventListener("click", click);
|
|
1033
|
+
input.removeEventListener("keydown", keydown);
|
|
1034
|
+
}
|
|
1035
|
+
};
|
|
1036
|
+
}
|
|
1037
|
+
//#endregion
|
|
1038
|
+
export { AI_CHAT_PROTOCOL_VERSION, bindChat, createAiChatRegistry, createChatSession, parseStreamEvent, readEventStream, renderAnswerCard };
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vettvangur/ai-sdk",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Framework-agnostic AI chat client for server-rendered shells",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"access": "public",
|
|
7
|
+
"author": "Vettvangur",
|
|
8
|
+
"license": "Proprietary",
|
|
9
|
+
"sideEffects": false,
|
|
10
|
+
"files": [
|
|
11
|
+
"dist/**/*",
|
|
12
|
+
"LICENSE"
|
|
13
|
+
],
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"rimraf": "6.1.3",
|
|
23
|
+
"tsdown": "0.22.14",
|
|
24
|
+
"typescript": "7.0.2"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"clean": "rimraf dist",
|
|
28
|
+
"bundle": "tsdown",
|
|
29
|
+
"dist": "pnpm publish",
|
|
30
|
+
"test": "vitest run",
|
|
31
|
+
"typecheck": "tsc --noEmit"
|
|
32
|
+
}
|
|
33
|
+
}
|