vue-llm 1.2.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.es.md +401 -0
- package/README.ja.md +401 -0
- package/README.md +48 -14
- package/README.pt-BR.md +401 -0
- package/README.ru.md +401 -0
- package/README.zh-CN.md +401 -0
- package/dist/components/MessageParts.vue.d.ts +33 -0
- package/dist/components.cjs +5 -5
- package/dist/components.d.ts +1 -0
- package/dist/components.js +146 -75
- package/dist/composables/use-auto-scroll.d.ts +17 -0
- package/dist/composables/use-chat.d.ts +11 -5
- package/dist/composables/use-completion.d.ts +1 -1
- package/dist/composables/use-object.d.ts +1 -1
- package/dist/composables/use-prompt-input.d.ts +51 -0
- package/dist/core/devtools.d.ts +86 -0
- package/dist/core/partial-json.d.ts +6 -0
- package/dist/core/stream-processor.d.ts +4 -0
- package/dist/devtools-C4JE26vO.cjs +1 -0
- package/dist/devtools-DnDvylCG.js +87 -0
- package/dist/devtools.cjs +1 -0
- package/dist/devtools.d.ts +15 -0
- package/dist/devtools.js +210 -0
- package/dist/index.cjs +3 -3
- package/dist/index.d.ts +7 -1
- package/dist/index.js +576 -441
- package/dist/nuxt.cjs +2 -1
- package/dist/nuxt.d.ts +4 -0
- package/dist/nuxt.js +34 -11
- package/dist/providers/webllm.d.ts +87 -0
- package/dist/providers.cjs +5 -4
- package/dist/providers.d.ts +2 -0
- package/dist/providers.js +529 -275
- package/dist/server.d.ts +4 -0
- package/dist/types.d.ts +37 -1
- package/dist/utils/browser.d.ts +1 -0
- package/dist/vue-llm.css +1 -1
- package/package.json +44 -5
package/dist/components.js
CHANGED
|
@@ -1,112 +1,112 @@
|
|
|
1
|
-
import { defineComponent as
|
|
2
|
-
const
|
|
3
|
-
function
|
|
1
|
+
import { defineComponent as I, computed as N, openBlock as a, createElementBlock as i, Fragment as g, createElementVNode as w, createCommentVNode as j, renderList as T, toDisplayString as _, createBlock as x, resolveDynamicComponent as B, mergeProps as W, renderSlot as q } from "vue";
|
|
2
|
+
const E = /^(?:https?:|mailto:|\/(?!\/)|#|\.\.?\/)/i;
|
|
3
|
+
function k(s) {
|
|
4
4
|
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
5
5
|
}
|
|
6
|
-
function
|
|
6
|
+
function M(s) {
|
|
7
7
|
const e = s.replace(/\r\n?/g, `
|
|
8
8
|
`).split(`
|
|
9
|
-
`),
|
|
9
|
+
`), o = [];
|
|
10
10
|
let t = 0;
|
|
11
11
|
for (; t < e.length; ) {
|
|
12
|
-
const
|
|
13
|
-
if (
|
|
14
|
-
const
|
|
15
|
-
for (t++; t < e.length && !e[t].trimStart().startsWith(
|
|
16
|
-
|
|
12
|
+
const l = e[t], m = /^\s*(`{3,}|~{3,})\s*([\w+#.-]*)/.exec(l);
|
|
13
|
+
if (m) {
|
|
14
|
+
const c = [];
|
|
15
|
+
for (t++; t < e.length && !e[t].trimStart().startsWith(m[1]); )
|
|
16
|
+
c.push(e[t]), t++;
|
|
17
17
|
t++;
|
|
18
|
-
const
|
|
19
|
-
|
|
18
|
+
const u = m[2] ? ` class="language-${k(m[2])}"` : "";
|
|
19
|
+
o.push(`<pre><code${u}>${k(c.join(`
|
|
20
20
|
`))}</code></pre>`);
|
|
21
21
|
continue;
|
|
22
22
|
}
|
|
23
|
-
if (!
|
|
23
|
+
if (!l.trim()) {
|
|
24
24
|
t++;
|
|
25
25
|
continue;
|
|
26
26
|
}
|
|
27
|
-
const
|
|
28
|
-
if (
|
|
29
|
-
const
|
|
30
|
-
|
|
27
|
+
const n = /^(#{1,6})\s+(.*?)\s*#*\s*$/.exec(l);
|
|
28
|
+
if (n) {
|
|
29
|
+
const c = n[1].length;
|
|
30
|
+
o.push(`<h${c}>${y(n[2])}</h${c}>`), t++;
|
|
31
31
|
continue;
|
|
32
32
|
}
|
|
33
|
-
if (/^\s*([-*_])(\s*\1){2,}\s*$/.test(
|
|
34
|
-
|
|
33
|
+
if (/^\s*([-*_])(\s*\1){2,}\s*$/.test(l)) {
|
|
34
|
+
o.push("<hr>"), t++;
|
|
35
35
|
continue;
|
|
36
36
|
}
|
|
37
|
-
if (/^\s*>/.test(
|
|
38
|
-
const
|
|
37
|
+
if (/^\s*>/.test(l)) {
|
|
38
|
+
const c = [];
|
|
39
39
|
for (; t < e.length && /^\s*>/.test(e[t]); )
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
c.push(e[t].replace(/^\s*>\s?/, "")), t++;
|
|
41
|
+
o.push(`<blockquote>${M(c.join(`
|
|
42
42
|
`))}</blockquote>`);
|
|
43
43
|
continue;
|
|
44
44
|
}
|
|
45
|
-
if (
|
|
46
|
-
const
|
|
45
|
+
if (C(e, t)) {
|
|
46
|
+
const c = v(e[t]), u = v(e[t + 1]).map((r) => r.endsWith(":") ? r.startsWith(":") ? "center" : "right" : r.startsWith(":") ? "left" : "");
|
|
47
47
|
t += 2;
|
|
48
|
-
const
|
|
48
|
+
const h = [];
|
|
49
49
|
for (; t < e.length && e[t].includes("|") && e[t].trim(); )
|
|
50
|
-
|
|
51
|
-
const
|
|
52
|
-
const
|
|
53
|
-
return `<${r}${
|
|
50
|
+
h.push(v(e[t])), t++;
|
|
51
|
+
const f = (r, $, p) => {
|
|
52
|
+
const L = u[p] ? ` style="text-align:${u[p]}"` : "";
|
|
53
|
+
return `<${r}${L}>${y($)}</${r}>`;
|
|
54
54
|
};
|
|
55
|
-
|
|
56
|
-
"<table><thead><tr>" +
|
|
55
|
+
o.push(
|
|
56
|
+
"<table><thead><tr>" + c.map((r, $) => f("th", r, $)).join("") + "</tr></thead><tbody>" + h.map((r) => "<tr>" + c.map(($, p) => f("td", r[p] ?? "", p)).join("") + "</tr>").join("") + "</tbody></table>"
|
|
57
57
|
);
|
|
58
58
|
continue;
|
|
59
59
|
}
|
|
60
|
-
const
|
|
61
|
-
if (
|
|
62
|
-
const
|
|
60
|
+
const d = /^\s*([-*+]|\d+[.)])\s+/.exec(l);
|
|
61
|
+
if (d) {
|
|
62
|
+
const c = /\d/.test(d[1]), u = [];
|
|
63
63
|
for (; t < e.length; ) {
|
|
64
64
|
const r = /^\s*([-*+]|\d+[.)])\s+(.*)$/.exec(e[t]);
|
|
65
|
-
if (r && /\d/.test(r[1]) ===
|
|
66
|
-
|
|
67
|
-
else if (e[t].trim() && /^\s{2,}/.test(e[t]) &&
|
|
68
|
-
|
|
65
|
+
if (r && /\d/.test(r[1]) === c)
|
|
66
|
+
u.push(r[2]);
|
|
67
|
+
else if (e[t].trim() && /^\s{2,}/.test(e[t]) && u.length > 0)
|
|
68
|
+
u[u.length - 1] += " " + e[t].trim();
|
|
69
69
|
else
|
|
70
70
|
break;
|
|
71
71
|
t++;
|
|
72
72
|
}
|
|
73
|
-
const
|
|
74
|
-
|
|
73
|
+
const h = c ? "ol" : "ul", f = c && parseInt(d[1], 10) !== 1 ? ` start="${parseInt(d[1], 10)}"` : "";
|
|
74
|
+
o.push(`<${h}${f}>${u.map((r) => `<li>${y(r)}</li>`).join("")}</${h}>`);
|
|
75
75
|
continue;
|
|
76
76
|
}
|
|
77
|
-
const
|
|
78
|
-
for (; t < e.length && e[t].trim() && !
|
|
79
|
-
|
|
80
|
-
|
|
77
|
+
const S = [];
|
|
78
|
+
for (; t < e.length && e[t].trim() && !P(e, t); )
|
|
79
|
+
S.push(e[t].trim()), t++;
|
|
80
|
+
o.push(`<p>${S.map(y).join("<br>")}</p>`);
|
|
81
81
|
}
|
|
82
|
-
return
|
|
82
|
+
return o.join(`
|
|
83
83
|
`);
|
|
84
84
|
}
|
|
85
|
-
function
|
|
86
|
-
return s.split(/(`+[^`]*`+)/).map((e,
|
|
85
|
+
function y(s) {
|
|
86
|
+
return s.split(/(`+[^`]*`+)/).map((e, o) => o % 2 === 1 ? `<code>${k(e.replace(/^`+|`+$/g, ""))}</code>` : F(k(e))).join("");
|
|
87
87
|
}
|
|
88
|
-
function
|
|
89
|
-
return s.replace(/!\[([^\]]*)\]\(([^)\s]+)\)/g, (e,
|
|
88
|
+
function F(s) {
|
|
89
|
+
return s.replace(/!\[([^\]]*)\]\(([^)\s]+)\)/g, (e, o, t) => b(t) ? `<img src="${t}" alt="${o}">` : o).replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (e, o, t) => b(t) ? `<a href="${t}" target="_blank" rel="noopener noreferrer">${o}</a>` : o).replace(new RegExp("(\\*\\*|__)(?=\\S)(.+?)(?<=\\S)\\1", "g"), "<strong>$2</strong>").replace(new RegExp("(?<![\\w*])\\*(?=\\S)(.+?)(?<=\\S)\\*(?![\\w*])", "g"), "<em>$1</em>").replace(new RegExp("(?<![\\w_])_(?=\\S)(.+?)(?<=\\S)_(?![\\w_])", "g"), "<em>$1</em>").replace(new RegExp("~~(?=\\S)(.+?)(?<=\\S)~~", "g"), "<del>$1</del>");
|
|
90
90
|
}
|
|
91
|
-
function
|
|
91
|
+
function b(s) {
|
|
92
92
|
const e = s.replace(/&/g, "&").replace(/&#(x?[0-9a-f]+);?/gi, "?");
|
|
93
|
-
return
|
|
93
|
+
return E.test(e.trim());
|
|
94
94
|
}
|
|
95
|
-
function
|
|
95
|
+
function v(s) {
|
|
96
96
|
return s.trim().replace(/^\||\|$/g, "").split("|").map((e) => e.trim());
|
|
97
97
|
}
|
|
98
|
-
function
|
|
98
|
+
function C(s, e) {
|
|
99
99
|
return s[e].includes("|") && e + 1 < s.length && /^\s*\|?\s*:?-{1,}:?\s*(\|\s*:?-{1,}:?\s*)*\|?\s*$/.test(s[e + 1]);
|
|
100
100
|
}
|
|
101
|
-
function
|
|
102
|
-
const
|
|
103
|
-
return /^\s*(`{3,}|~{3,})/.test(
|
|
101
|
+
function P(s, e) {
|
|
102
|
+
const o = s[e];
|
|
103
|
+
return /^\s*(`{3,}|~{3,})/.test(o) || /^#{1,6}\s/.test(o) || /^\s*>/.test(o) || /^\s*([-*+]|\d+[.)])\s+/.test(o) || /^\s*([-*_])(\s*\1){2,}\s*$/.test(o) || C(s, e);
|
|
104
104
|
}
|
|
105
|
-
const
|
|
105
|
+
const H = ["innerHTML"], D = {
|
|
106
106
|
key: 0,
|
|
107
107
|
class: "vue-llm-cursor",
|
|
108
108
|
"aria-hidden": "true"
|
|
109
|
-
},
|
|
109
|
+
}, U = /* @__PURE__ */ I({
|
|
110
110
|
__name: "StreamMarkdown",
|
|
111
111
|
props: {
|
|
112
112
|
content: {},
|
|
@@ -114,24 +114,95 @@ const T = ["innerHTML"], W = {
|
|
|
114
114
|
render: { type: Function }
|
|
115
115
|
},
|
|
116
116
|
setup(s) {
|
|
117
|
-
const e = s,
|
|
118
|
-
return (t,
|
|
119
|
-
|
|
117
|
+
const e = s, o = N(() => (e.render ?? M)(e.content));
|
|
118
|
+
return (t, l) => (a(), i(g, null, [
|
|
119
|
+
w("div", {
|
|
120
120
|
class: "vue-llm-markdown",
|
|
121
|
-
innerHTML:
|
|
122
|
-
}, null, 8,
|
|
123
|
-
s.streaming ? (
|
|
121
|
+
innerHTML: o.value
|
|
122
|
+
}, null, 8, H),
|
|
123
|
+
s.streaming ? (a(), i("span", D, "▊")) : j("", !0)
|
|
124
124
|
], 64));
|
|
125
125
|
}
|
|
126
|
-
}),
|
|
127
|
-
const
|
|
128
|
-
for (const [t,
|
|
129
|
-
|
|
130
|
-
return
|
|
131
|
-
},
|
|
126
|
+
}), R = (s, e) => {
|
|
127
|
+
const o = s.__vccOpts || s;
|
|
128
|
+
for (const [t, l] of e)
|
|
129
|
+
o[t] = l;
|
|
130
|
+
return o;
|
|
131
|
+
}, V = /* @__PURE__ */ R(U, [["__scopeId", "data-v-52dbf793"]]), A = ["data-role"], O = {
|
|
132
|
+
key: 0,
|
|
133
|
+
class: "vue-llm-text"
|
|
134
|
+
}, z = ["open"], G = ["src", "alt"], J = {
|
|
135
|
+
key: 1,
|
|
136
|
+
class: "vue-llm-file"
|
|
137
|
+
}, K = ["href"], Q = /* @__PURE__ */ I({
|
|
138
|
+
__name: "MessageParts",
|
|
139
|
+
props: {
|
|
140
|
+
message: {},
|
|
141
|
+
components: {},
|
|
142
|
+
render: { type: Function }
|
|
143
|
+
},
|
|
144
|
+
emits: ["tool-result"],
|
|
145
|
+
setup(s, { emit: e }) {
|
|
146
|
+
const o = e, t = (l) => ({
|
|
147
|
+
toolCallId: l.toolCallId,
|
|
148
|
+
toolName: l.toolName,
|
|
149
|
+
args: l.args,
|
|
150
|
+
state: l.state,
|
|
151
|
+
result: l.result,
|
|
152
|
+
error: l.error,
|
|
153
|
+
addResult: (m) => o("tool-result", { toolCallId: l.toolCallId, result: m })
|
|
154
|
+
});
|
|
155
|
+
return (l, m) => (a(), i("div", {
|
|
156
|
+
class: "vue-llm-message",
|
|
157
|
+
"data-role": s.message.role
|
|
158
|
+
}, [
|
|
159
|
+
(a(!0), i(g, null, T(s.message.parts, (n, d) => (a(), i(g, {
|
|
160
|
+
key: n.type === "tool-call" ? n.toolCallId : d
|
|
161
|
+
}, [
|
|
162
|
+
n.type === "text" ? (a(), i(g, { key: 0 }, [
|
|
163
|
+
s.message.role === "user" ? (a(), i("div", O, _(n.text), 1)) : (a(), x(V, {
|
|
164
|
+
key: 1,
|
|
165
|
+
content: n.text,
|
|
166
|
+
streaming: n.state === "streaming",
|
|
167
|
+
render: s.render
|
|
168
|
+
}, null, 8, ["content", "streaming", "render"]))
|
|
169
|
+
], 64)) : n.type === "reasoning" ? (a(), i("details", {
|
|
170
|
+
key: 1,
|
|
171
|
+
class: "vue-llm-reasoning",
|
|
172
|
+
open: n.state === "streaming"
|
|
173
|
+
}, [
|
|
174
|
+
m[0] || (m[0] = w("summary", null, "Reasoning", -1)),
|
|
175
|
+
w("div", null, _(n.text), 1)
|
|
176
|
+
], 8, z)) : n.type === "tool-call" ? (a(), i(g, { key: 2 }, [
|
|
177
|
+
s.components?.[n.toolName] ? (a(), x(B(s.components[n.toolName]), W({
|
|
178
|
+
key: 0,
|
|
179
|
+
ref_for: !0
|
|
180
|
+
}, t(n)), null, 16)) : q(l.$slots, "tool", {
|
|
181
|
+
part: n,
|
|
182
|
+
addResult: t(n).addResult
|
|
183
|
+
}, void 0, !0, 1)
|
|
184
|
+
], 64)) : n.type === "file" ? (a(), i(g, { key: 3 }, [
|
|
185
|
+
n.mediaType.startsWith("image/") ? (a(), i("img", {
|
|
186
|
+
key: 0,
|
|
187
|
+
class: "vue-llm-file",
|
|
188
|
+
src: n.url,
|
|
189
|
+
alt: n.filename ?? ""
|
|
190
|
+
}, null, 8, G)) : (a(), i("span", J, _(n.filename ?? n.mediaType), 1))
|
|
191
|
+
], 64)) : n.type === "source" && /^https?:/i.test(n.url) ? (a(), i("a", {
|
|
192
|
+
key: 4,
|
|
193
|
+
class: "vue-llm-source",
|
|
194
|
+
href: n.url,
|
|
195
|
+
target: "_blank",
|
|
196
|
+
rel: "noopener noreferrer"
|
|
197
|
+
}, _(n.title ?? n.url), 9, K)) : j("", !0)
|
|
198
|
+
], 64))), 128))
|
|
199
|
+
], 8, A));
|
|
200
|
+
}
|
|
201
|
+
}), Y = /* @__PURE__ */ R(Q, [["__scopeId", "data-v-3f30944b"]]);
|
|
132
202
|
export {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
203
|
+
Y as MessageParts,
|
|
204
|
+
V as StreamMarkdown,
|
|
205
|
+
k as escapeHTML,
|
|
206
|
+
y as renderInline,
|
|
207
|
+
M as renderMarkdown
|
|
137
208
|
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type Ref } from 'vue';
|
|
2
|
+
export interface UseAutoScrollOptions {
|
|
3
|
+
/** Distance from the bottom, in px, that still counts as "at the bottom". Default `48`. */
|
|
4
|
+
threshold?: number;
|
|
5
|
+
}
|
|
6
|
+
export interface UseAutoScrollReturn {
|
|
7
|
+
/** Bind to the scrollable element with `ref="container"` */
|
|
8
|
+
container: Ref<HTMLElement | null>;
|
|
9
|
+
/** False once the user scrolls up; use it to show a "scroll to bottom" button */
|
|
10
|
+
isAtBottom: Ref<boolean>;
|
|
11
|
+
scrollToBottom: (behavior?: ScrollBehavior) => void;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Keeps a message list pinned to the bottom while content streams in,
|
|
15
|
+
* and lets the user scroll up to read without being pulled back down.
|
|
16
|
+
*/
|
|
17
|
+
export declare function useAutoScroll(options?: UseAutoScrollOptions): UseAutoScrollReturn;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Ref } from 'vue';
|
|
2
2
|
import { type MaybeRef, type MaybeRefOrGetter } from '../utils/vue';
|
|
3
|
-
import type { ChatStatus,
|
|
3
|
+
import type { ChatStatus, ChatTool, FinishReason, UIMessage, Usage } from '../types';
|
|
4
4
|
import type { LLMProvider } from '../providers/types';
|
|
5
5
|
import type { ChatStorage } from '../core/storage';
|
|
6
6
|
import { type FileInput } from '../utils/file';
|
|
@@ -11,12 +11,14 @@ export type SendMessageInput = string | {
|
|
|
11
11
|
export interface UseChatOptions {
|
|
12
12
|
id?: string;
|
|
13
13
|
provider: LLMProvider;
|
|
14
|
-
model: string
|
|
14
|
+
model: MaybeRefOrGetter<string>;
|
|
15
15
|
system?: MaybeRefOrGetter<string | undefined>;
|
|
16
16
|
initialMessages?: UIMessage[];
|
|
17
17
|
body?: MaybeRef<Record<string, unknown>>;
|
|
18
18
|
providerOptions?: Record<string, unknown>;
|
|
19
|
-
|
|
19
|
+
/** Tools with `execute` run automatically; others go to `onToolCall` or wait for `addToolResult` */
|
|
20
|
+
tools?: ChatTool[];
|
|
21
|
+
/** Return a result to resolve the call, or `undefined` to resolve it later with `addToolResult` */
|
|
20
22
|
onToolCall?: (toolCall: {
|
|
21
23
|
toolCallId: string;
|
|
22
24
|
toolName: string;
|
|
@@ -28,6 +30,8 @@ export interface UseChatOptions {
|
|
|
28
30
|
}) => void;
|
|
29
31
|
onError?: (error: Error) => void;
|
|
30
32
|
maxToolRoundtrips?: number;
|
|
33
|
+
/** Continue the conversation once `addToolResult` has resolved every pending call. Default `true`. */
|
|
34
|
+
autoSendToolResults?: boolean;
|
|
31
35
|
storage?: ChatStorage;
|
|
32
36
|
}
|
|
33
37
|
export interface UseChatReturn {
|
|
@@ -42,10 +46,12 @@ export interface UseChatReturn {
|
|
|
42
46
|
setMessages: (messages: UIMessage[] | ((prev: UIMessage[]) => UIMessage[])) => void;
|
|
43
47
|
handleSubmit: (event?: Event) => void;
|
|
44
48
|
isLoading: Ref<boolean>;
|
|
49
|
+
/** Resolve a tool call from the UI. Continues the chat when no other call is pending. */
|
|
45
50
|
addToolResult: (options: {
|
|
46
51
|
toolCallId: string;
|
|
47
|
-
result
|
|
48
|
-
|
|
52
|
+
result?: unknown;
|
|
53
|
+
error?: string;
|
|
54
|
+
}) => Promise<void>;
|
|
49
55
|
clearError: () => void;
|
|
50
56
|
isRestored: Ref<boolean>;
|
|
51
57
|
}
|
|
@@ -5,7 +5,7 @@ import type { LLMProvider } from '../providers/types';
|
|
|
5
5
|
export interface UseCompletionOptions {
|
|
6
6
|
id?: string;
|
|
7
7
|
provider: LLMProvider;
|
|
8
|
-
model: string
|
|
8
|
+
model: MaybeRefOrGetter<string>;
|
|
9
9
|
system?: MaybeRefOrGetter<string | undefined>;
|
|
10
10
|
onFinish?: (completion: string, context: {
|
|
11
11
|
finishReason: FinishReason;
|
|
@@ -5,7 +5,7 @@ import type { LLMProvider } from '../providers/types';
|
|
|
5
5
|
export interface UseObjectOptions<T> {
|
|
6
6
|
id?: string;
|
|
7
7
|
provider: LLMProvider;
|
|
8
|
-
model: string
|
|
8
|
+
model: MaybeRefOrGetter<string>;
|
|
9
9
|
system?: MaybeRefOrGetter<string | undefined>;
|
|
10
10
|
schema: Record<string, unknown>;
|
|
11
11
|
onFinish?: (object: T, context: {
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type Ref } from 'vue';
|
|
2
|
+
import type { ChatStatus } from '../types';
|
|
3
|
+
import type { SendMessageInput } from './use-chat';
|
|
4
|
+
export interface UsePromptInputOptions {
|
|
5
|
+
/** Submit on Enter; Shift+Enter inserts a newline. Default `true`. */
|
|
6
|
+
submitOnEnter?: boolean;
|
|
7
|
+
/** Grow the textarea with its content up to this many rows. Default `8`. */
|
|
8
|
+
maxRows?: number;
|
|
9
|
+
/** Accepted file types, as in `<input accept>` (e.g. `'image/*,.pdf'`). Default: any. */
|
|
10
|
+
accept?: string;
|
|
11
|
+
/** Maximum number of attached files. Default: unlimited. */
|
|
12
|
+
maxFiles?: number;
|
|
13
|
+
/** Called when files are rejected by `accept` or `maxFiles` */
|
|
14
|
+
onReject?: (files: File[], reason: 'type' | 'count') => void;
|
|
15
|
+
}
|
|
16
|
+
/** The parts of `useChat` the prompt input needs */
|
|
17
|
+
export interface PromptInputChat {
|
|
18
|
+
input: Ref<string>;
|
|
19
|
+
status: Ref<ChatStatus>;
|
|
20
|
+
sendMessage: (message?: SendMessageInput) => Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
export interface UsePromptInputReturn {
|
|
23
|
+
/** Bind with `ref="textarea"` */
|
|
24
|
+
textarea: Ref<HTMLTextAreaElement | null>;
|
|
25
|
+
files: Ref<File[]>;
|
|
26
|
+
/** True while files are dragged over the drop zone */
|
|
27
|
+
isDragging: Ref<boolean>;
|
|
28
|
+
canSubmit: Ref<boolean>;
|
|
29
|
+
submit: () => Promise<void>;
|
|
30
|
+
addFiles: (files: FileList | File[] | null | undefined) => void;
|
|
31
|
+
removeFile: (file: File) => void;
|
|
32
|
+
/** Spread on the textarea: `v-bind="handlers"` (keydown, paste) */
|
|
33
|
+
handlers: {
|
|
34
|
+
onKeydown: (event: KeyboardEvent) => void;
|
|
35
|
+
onPaste: (event: ClipboardEvent) => void;
|
|
36
|
+
};
|
|
37
|
+
/** Spread on the drop zone: `v-bind="dropzone"` */
|
|
38
|
+
dropzone: {
|
|
39
|
+
onDragenter: (event: DragEvent) => void;
|
|
40
|
+
onDragover: (event: DragEvent) => void;
|
|
41
|
+
onDragleave: (event: DragEvent) => void;
|
|
42
|
+
onDrop: (event: DragEvent) => void;
|
|
43
|
+
};
|
|
44
|
+
/** Recalculate the textarea height */
|
|
45
|
+
resize: () => void;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Headless logic for a chat input: Enter to send, IME-safe, auto-growing textarea,
|
|
49
|
+
* attachments from the file picker, clipboard and drag-and-drop.
|
|
50
|
+
*/
|
|
51
|
+
export declare function usePromptInput(chat: PromptInputChat, options?: UsePromptInputOptions): UsePromptInputReturn;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { Ref } from 'vue';
|
|
2
|
+
import type { ChatRequest, ChatStreamResult, LLMProvider } from '../providers/types';
|
|
3
|
+
import type { ChatStatus, UIMessage } from '../types';
|
|
4
|
+
/** A composable instance shown in the devtools inspector */
|
|
5
|
+
export interface DevtoolsSession {
|
|
6
|
+
id: string;
|
|
7
|
+
kind: 'chat' | 'completion' | 'object';
|
|
8
|
+
providerId: string;
|
|
9
|
+
getModel: () => string;
|
|
10
|
+
status: Ref<ChatStatus>;
|
|
11
|
+
error: Ref<Error | undefined>;
|
|
12
|
+
messages?: Ref<UIMessage[]>;
|
|
13
|
+
/** Current output of useCompletion / useObject */
|
|
14
|
+
output?: Ref<unknown>;
|
|
15
|
+
stop: () => void;
|
|
16
|
+
clear?: () => void;
|
|
17
|
+
}
|
|
18
|
+
export type DevtoolsEvent = {
|
|
19
|
+
type: 'session-added';
|
|
20
|
+
session: DevtoolsSession;
|
|
21
|
+
} | {
|
|
22
|
+
type: 'session-removed';
|
|
23
|
+
sessionId: string;
|
|
24
|
+
} | {
|
|
25
|
+
type: 'request-start';
|
|
26
|
+
requestId: string;
|
|
27
|
+
sessionId: string;
|
|
28
|
+
providerId: string;
|
|
29
|
+
model: string;
|
|
30
|
+
messages: ChatRequest['messages'];
|
|
31
|
+
tools: string[];
|
|
32
|
+
time: number;
|
|
33
|
+
} | {
|
|
34
|
+
type: 'first-chunk';
|
|
35
|
+
requestId: string;
|
|
36
|
+
sessionId: string;
|
|
37
|
+
time: number;
|
|
38
|
+
} | {
|
|
39
|
+
type: 'tool-call';
|
|
40
|
+
requestId: string;
|
|
41
|
+
sessionId: string;
|
|
42
|
+
toolCallId: string;
|
|
43
|
+
toolName: string;
|
|
44
|
+
args: string;
|
|
45
|
+
time: number;
|
|
46
|
+
} | {
|
|
47
|
+
type: 'tool-result';
|
|
48
|
+
requestId: string;
|
|
49
|
+
sessionId: string;
|
|
50
|
+
toolCallId: string;
|
|
51
|
+
result?: unknown;
|
|
52
|
+
error?: string;
|
|
53
|
+
time: number;
|
|
54
|
+
} | {
|
|
55
|
+
type: 'request-end';
|
|
56
|
+
requestId: string;
|
|
57
|
+
sessionId: string;
|
|
58
|
+
finishReason?: string;
|
|
59
|
+
usage?: {
|
|
60
|
+
promptTokens: number;
|
|
61
|
+
completionTokens: number;
|
|
62
|
+
totalTokens: number;
|
|
63
|
+
};
|
|
64
|
+
/** Characters of text and reasoning received */
|
|
65
|
+
textLength: number;
|
|
66
|
+
time: number;
|
|
67
|
+
} | {
|
|
68
|
+
type: 'request-error';
|
|
69
|
+
requestId: string;
|
|
70
|
+
sessionId: string;
|
|
71
|
+
error: Error;
|
|
72
|
+
aborted: boolean;
|
|
73
|
+
time: number;
|
|
74
|
+
};
|
|
75
|
+
export interface DevtoolsBridge {
|
|
76
|
+
emit(event: DevtoolsEvent): void;
|
|
77
|
+
}
|
|
78
|
+
export declare function getDevtoolsBridge(): DevtoolsBridge | undefined;
|
|
79
|
+
export declare function setDevtoolsBridge(bridge: DevtoolsBridge | undefined): void;
|
|
80
|
+
/** Registers a composable with devtools. Returns an unregister function. Free when devtools are not installed. */
|
|
81
|
+
export declare function registerDevtoolsSession(session: DevtoolsSession): () => void;
|
|
82
|
+
/**
|
|
83
|
+
* Calls `provider.chat` and, when devtools are installed, reports the request and taps its stream.
|
|
84
|
+
* Without devtools this is a plain `provider.chat` call.
|
|
85
|
+
*/
|
|
86
|
+
export declare function tracedChat(provider: LLMProvider, request: ChatRequest, sessionId: string): Promise<ChatStreamResult>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses JSON that may be cut off mid-stream, e.g. `{"city":"Os` → `{ city: 'Os' }`.
|
|
3
|
+
* Open strings and containers are closed; an incomplete trailing key or value is dropped.
|
|
4
|
+
* Returns `undefined` when nothing usable has arrived yet.
|
|
5
|
+
*/
|
|
6
|
+
export declare function parsePartialJSON(text: string): unknown;
|
|
@@ -12,7 +12,11 @@ export interface StreamCallbacks {
|
|
|
12
12
|
toolCallId: string;
|
|
13
13
|
toolName: string;
|
|
14
14
|
args: unknown;
|
|
15
|
+
}, context: {
|
|
16
|
+
signal: AbortSignal;
|
|
15
17
|
}) => unknown | Promise<unknown>;
|
|
18
|
+
/** Whether `onToolCall` can handle a tool; calls it cannot handle stay `pending` without flashing `running` */
|
|
19
|
+
canHandleTool?: (toolName: string) => boolean;
|
|
16
20
|
}
|
|
17
21
|
export declare class StreamProcessor {
|
|
18
22
|
private messages;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const C=Symbol.for("vue-llm.devtools");function g(){return globalThis[C]}function D(r){globalThis[C]=r}function E(r){const t=g();return t?(t.emit({type:"session-added",session:r}),()=>g()?.emit({type:"session-removed",sessionId:r.id})):()=>{}}let h=0;async function S(r,t,f){const o=g();if(!o)return r.chat(t);const s={requestId:`${f}:${++h}`,sessionId:f},a=()=>Date.now();o.emit({type:"request-start",...s,providerId:r.id,model:t.model,messages:t.messages,tools:t.tools?.map(e=>e.name)??[],time:a()});const u=e=>{const c=e instanceof Error?e:new Error(String(e));o.emit({type:"request-error",...s,error:c,aborted:!!t.signal?.aborted,time:a()})};let d;try{d=await r.chat(t)}catch(e){throw u(e),e}let b=!1,l=!1,p=0,y,n;const m=new Map,I=()=>{l||(l=!0,o.emit({type:"request-end",...s,finishReason:y,usage:n,textLength:p,time:a()}))},w=new TransformStream({transform(e,c){switch(b||(b=!0,o.emit({type:"first-chunk",...s,time:a()})),e.type){case"text-delta":case"reasoning-delta":p+=e.text.length;break;case"tool-call-start":m.set(e.toolCallId,{name:e.toolName,args:""});break;case"tool-call-delta":{const i=m.get(e.toolCallId);i&&(i.args+=e.args);break}case"tool-call-end":{const i=m.get(e.toolCallId);o.emit({type:"tool-call",...s,toolCallId:e.toolCallId,toolName:i?.name??"",args:i?.args??"",time:a()});break}case"tool-result":o.emit({type:"tool-result",...s,toolCallId:e.toolCallId,result:e.result,error:e.error,time:a()});break;case"finish":y=e.finishReason,n=e.usage??n;break;case"error":l=!0,u(e.error);break}c.enqueue(e)},flush:I});d.response.then(e=>{n=e.usage??n},()=>{});const v=d.stream.pipeThrough(w);return t.signal?.addEventListener("abort",()=>{l||(l=!0,o.emit({type:"request-error",...s,error:new Error("Aborted"),aborted:!0,time:a()}))},{once:!0}),{stream:v,response:d.response}}exports.registerDevtoolsSession=E;exports.setDevtoolsBridge=D;exports.tracedChat=S;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
const I = /* @__PURE__ */ Symbol.for("vue-llm.devtools");
|
|
2
|
+
function g() {
|
|
3
|
+
return globalThis[I];
|
|
4
|
+
}
|
|
5
|
+
function h(r) {
|
|
6
|
+
globalThis[I] = r;
|
|
7
|
+
}
|
|
8
|
+
function x(r) {
|
|
9
|
+
const t = g();
|
|
10
|
+
return t ? (t.emit({ type: "session-added", session: r }), () => g()?.emit({ type: "session-removed", sessionId: r.id })) : () => {
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
let E = 0;
|
|
14
|
+
async function S(r, t, f) {
|
|
15
|
+
const o = g();
|
|
16
|
+
if (!o) return r.chat(t);
|
|
17
|
+
const s = { requestId: `${f}:${++E}`, sessionId: f }, a = () => Date.now();
|
|
18
|
+
o.emit({
|
|
19
|
+
type: "request-start",
|
|
20
|
+
...s,
|
|
21
|
+
providerId: r.id,
|
|
22
|
+
model: t.model,
|
|
23
|
+
messages: t.messages,
|
|
24
|
+
tools: t.tools?.map((e) => e.name) ?? [],
|
|
25
|
+
time: a()
|
|
26
|
+
});
|
|
27
|
+
const u = (e) => {
|
|
28
|
+
const c = e instanceof Error ? e : new Error(String(e));
|
|
29
|
+
o.emit({ type: "request-error", ...s, error: c, aborted: !!t.signal?.aborted, time: a() });
|
|
30
|
+
};
|
|
31
|
+
let d;
|
|
32
|
+
try {
|
|
33
|
+
d = await r.chat(t);
|
|
34
|
+
} catch (e) {
|
|
35
|
+
throw u(e), e;
|
|
36
|
+
}
|
|
37
|
+
let b = !1, l = !1, p = 0, y, n;
|
|
38
|
+
const m = /* @__PURE__ */ new Map(), C = () => {
|
|
39
|
+
l || (l = !0, o.emit({ type: "request-end", ...s, finishReason: y, usage: n, textLength: p, time: a() }));
|
|
40
|
+
}, w = new TransformStream({
|
|
41
|
+
transform(e, c) {
|
|
42
|
+
switch (b || (b = !0, o.emit({ type: "first-chunk", ...s, time: a() })), e.type) {
|
|
43
|
+
case "text-delta":
|
|
44
|
+
case "reasoning-delta":
|
|
45
|
+
p += e.text.length;
|
|
46
|
+
break;
|
|
47
|
+
case "tool-call-start":
|
|
48
|
+
m.set(e.toolCallId, { name: e.toolName, args: "" });
|
|
49
|
+
break;
|
|
50
|
+
case "tool-call-delta": {
|
|
51
|
+
const i = m.get(e.toolCallId);
|
|
52
|
+
i && (i.args += e.args);
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
case "tool-call-end": {
|
|
56
|
+
const i = m.get(e.toolCallId);
|
|
57
|
+
o.emit({ type: "tool-call", ...s, toolCallId: e.toolCallId, toolName: i?.name ?? "", args: i?.args ?? "", time: a() });
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
case "tool-result":
|
|
61
|
+
o.emit({ type: "tool-result", ...s, toolCallId: e.toolCallId, result: e.result, error: e.error, time: a() });
|
|
62
|
+
break;
|
|
63
|
+
case "finish":
|
|
64
|
+
y = e.finishReason, n = e.usage ?? n;
|
|
65
|
+
break;
|
|
66
|
+
case "error":
|
|
67
|
+
l = !0, u(e.error);
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
c.enqueue(e);
|
|
71
|
+
},
|
|
72
|
+
flush: C
|
|
73
|
+
});
|
|
74
|
+
d.response.then((e) => {
|
|
75
|
+
n = e.usage ?? n;
|
|
76
|
+
}, () => {
|
|
77
|
+
});
|
|
78
|
+
const v = d.stream.pipeThrough(w);
|
|
79
|
+
return t.signal?.addEventListener("abort", () => {
|
|
80
|
+
l || (l = !0, o.emit({ type: "request-error", ...s, error: new Error("Aborted"), aborted: !0, time: a() }));
|
|
81
|
+
}, { once: !0 }), { stream: v, response: d.response };
|
|
82
|
+
}
|
|
83
|
+
export {
|
|
84
|
+
x as r,
|
|
85
|
+
h as s,
|
|
86
|
+
S as t
|
|
87
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const q=require("@vue/devtools-api"),y=require("vue"),T=require("./devtools-C4JE26vO.cjs"),g="vue-llm",h="vue-llm:requests",C=4372611,L={install(a,r={}){const i=new Map,p=new Map,c=new Map,k=new Map;let l;const u=e=>{let s=p.get(e);return s||p.set(e,s={requests:0,errors:0,promptTokens:0,completionTokens:0}),s};let I=!1;const f=()=>{!l||I||(I=!0,setTimeout(()=>{I=!1,l?.sendInspectorTree(g),l?.sendInspectorState(g)},100))},d=e=>{l?.addTimelineEvent({layerId:h,event:e})},b=e=>{switch(e.type){case"session-added":{const{session:s}=e;i.set(s.id,s),u(s.id);const o=[s.status,s.error,s.messages,s.output].filter(Boolean);k.set(s.id,y.watch(o,f,{deep:!0}));break}case"session-removed":i.delete(e.sessionId),p.delete(e.sessionId),k.get(e.sessionId)?.(),k.delete(e.sessionId);break;case"request-start":{c.set(e.requestId,{start:e.time,sessionId:e.sessionId,model:e.model}),u(e.sessionId).requests++,d({time:e.time,title:"request",subtitle:`${e.providerId} · ${e.model}`,groupId:e.requestId,data:{session:e.sessionId,provider:e.providerId,model:e.model,messages:r.logMessages===!1?e.messages.length:e.messages,tools:e.tools}});break}case"first-chunk":{const s=c.get(e.requestId);s&&(s.firstChunk=e.time);const o=s?e.time-s.start:void 0;o!==void 0&&(u(e.sessionId).lastFirstChunk=o),d({time:e.time,title:"first token",subtitle:o!==void 0?`${o} ms`:void 0,groupId:e.requestId,data:{latency:o!==void 0?`${o} ms`:void 0}});break}case"tool-call":d({time:e.time,title:`tool: ${e.toolName}`,subtitle:e.args.length>60?`${e.args.slice(0,60)}…`:e.args,groupId:e.requestId,data:{toolCallId:e.toolCallId,name:e.toolName,args:w(e.args)}});break;case"tool-result":d({time:e.time,title:e.error?"tool error":"tool result",logType:e.error?"error":"default",groupId:e.requestId,data:{toolCallId:e.toolCallId,result:e.result,error:e.error}});break;case"request-end":{const s=c.get(e.requestId);c.delete(e.requestId);const o=s?e.time-s.start:void 0,t=u(e.sessionId);t.lastLatency=o,e.usage&&(t.promptTokens+=e.usage.promptTokens,t.completionTokens+=e.usage.completionTokens);const m=s?.firstChunk?(e.time-s.firstChunk)/1e3:0,n=e.usage&&m>0?Math.round(e.usage.completionTokens/m):void 0;t.lastTokensPerSecond=n,d({time:e.time,title:"finish",subtitle:[e.finishReason,o!==void 0&&`${o} ms`,e.usage&&`${e.usage.totalTokens} tokens`,n&&`${n} tok/s`].filter(Boolean).join(" · "),groupId:e.requestId,data:{finishReason:e.finishReason,duration:o!==void 0?`${o} ms`:void 0,usage:e.usage,tokensPerSecond:n,characters:e.textLength}});break}case"request-error":{c.delete(e.requestId),e.aborted||u(e.sessionId).errors++,d({time:e.time,title:e.aborted?"aborted":"error",subtitle:e.aborted?void 0:e.error.message,logType:e.aborted?"warning":"error",groupId:e.requestId,data:{error:e.error.message}});break}}f()};T.setDevtoolsBridge({emit:b}),q.setupDevtoolsPlugin({id:"vue-llm",label:"vue-llm",packageName:"vue-llm",homepage:"https://vue-llm.js.org/",logo:"https://vue-llm.js.org/logo.svg",app:a},e=>{l=e,l.addInspector({id:g,label:"LLM",icon:"smart_toy",treeFilterPlaceholder:"Search sessions",noSelectionText:"Select a useChat, useCompletion or useObject",nodeActions:[{icon:"stop",tooltip:"Stop generating",action:s=>i.get(s)?.stop()},{icon:"delete_sweep",tooltip:"Clear messages",action:s=>i.get(s)?.clear?.()}]}),l.addTimelineLayer({id:h,label:"LLM requests",color:C}),l.on.getInspectorTree(s=>{if(s.inspectorId!==g)return;const o=s.filter?.toLowerCase()??"";s.rootNodes=[...i.values()].filter(t=>!o||`${t.kind} ${t.id} ${t.getModel()}`.toLowerCase().includes(o)).map(t=>({id:t.id,label:`${$[t.kind]} #${t.id.length>12?t.id.slice(0,6):t.id}`,tags:[{label:t.status.value,textColor:16777215,backgroundColor:M[t.status.value]},{label:t.getModel(),textColor:0,backgroundColor:15067115}]}))}),l.on.getInspectorState(s=>{if(s.inspectorId!==g)return;const o=i.get(s.nodeId);if(!o)return;const t=u(o.id);s.state={state:[{key:"status",value:o.status.value},{key:"provider",value:o.providerId},{key:"model",value:o.getModel()},{key:"error",value:o.error.value?.message??null}],usage:[{key:"requests",value:t.requests},{key:"errors",value:t.errors},{key:"prompt tokens",value:t.promptTokens},{key:"completion tokens",value:t.completionTokens},{key:"last time to first token",value:t.lastFirstChunk!==void 0?`${t.lastFirstChunk} ms`:null},{key:"last duration",value:t.lastLatency!==void 0?`${t.lastLatency} ms`:null},{key:"last tokens/s",value:t.lastTokensPerSecond??null}],...o.messages&&{messages:o.messages.value.map((m,n)=>({key:`${n} ${m.role}`,value:S(m)}))},...o.output&&{output:[{key:"value",value:o.output.value}]}}})})}},$={chat:"useChat",completion:"useCompletion",object:"useObject"},M={ready:4372611,submitted:16096779,streaming:3900150,error:15680580};function S(a){return a.parts.map(r=>{switch(r.type){case"text":case"reasoning":return{[r.type]:r.text};case"tool-call":return{tool:r.toolName,state:r.state,args:r.args,result:r.result,error:r.error};case"file":return{file:r.filename??r.mediaType};case"source":return{source:r.url}}})}function w(a){try{return JSON.parse(a)}catch{return a}}exports.LLMDevtools=L;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type Plugin } from 'vue';
|
|
2
|
+
export interface LLMDevtoolsOptions {
|
|
3
|
+
/** Include the full message history in request timeline events. Default `true`; `false` logs only the count. */
|
|
4
|
+
logMessages?: boolean;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Vue DevTools integration: an "LLM" inspector with every useChat / useCompletion / useObject
|
|
8
|
+
* and a timeline of requests, first-token latency, tool calls and token usage.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { LLMDevtools } from 'vue-llm/devtools'
|
|
12
|
+
* if (import.meta.env.DEV) app.use(LLMDevtools)
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare const LLMDevtools: Plugin<[LLMDevtoolsOptions?]>;
|