vue-llm 1.0.0 → 1.2.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.md +2 -1
- package/dist/composables/use-chat.d.ts +2 -1
- package/dist/composables/use-completion.d.ts +2 -1
- package/dist/composables/use-object.d.ts +2 -1
- package/dist/composables/use-prompt.d.ts +2 -1
- package/dist/index.cjs +3 -3
- package/dist/index.js +297 -287
- package/dist/providers/sse.d.ts +18 -0
- package/dist/providers/types.d.ts +5 -0
- package/dist/providers.cjs +4 -8
- package/dist/providers.d.ts +2 -0
- package/dist/providers.js +257 -321
- package/dist/server.cjs +2 -3
- package/dist/server.d.ts +8 -1
- package/dist/server.js +109 -100
- package/dist/sse-DU5m1n6r.js +104 -0
- package/dist/sse-PZGBuYKy.cjs +2 -0
- package/dist/utils/vue.d.ts +4 -0
- package/package.json +6 -4
package/dist/server.cjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("./sse-PZGBuYKy.cjs");function h(e){const a=e.rateLimit?S(e.rateLimit):void 0;return async n=>{if(n.method!=="POST")return new Response(JSON.stringify({error:"Method not allowed"}),{status:405,headers:{"Content-Type":"application/json"}});if(a){const s=a(n);if(s!==void 0)return new Response(JSON.stringify({error:"Too many requests"}),{status:429,headers:{"Content-Type":"application/json","Retry-After":String(Math.ceil(s/1e3))}})}let t;try{t=await n.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON body"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!t.messages?.length)return new Response(JSON.stringify({error:"messages array is required"}),{status:400,headers:{"Content-Type":"application/json"}});e.maxMessages&&t.messages.length>e.maxMessages&&(t.messages=t.messages.slice(-e.maxMessages)),e.onRequest&&(t=await e.onRequest(t));const l=[],u=t.system??e.system;u&&l.push({role:"system",content:u}),l.push(...t.messages);try{e.provider.validateConfig();const s={model:t.model??e.model??"gpt-4o",tools:e.tools?.map(({execute:c,...m})=>m),signal:n.signal},{stream:o}=await e.provider.chat({...s,messages:l}),f=new Map((e.tools??[]).filter(c=>c.execute).map(c=>[c.name,c])),d=y(f.size>0?w(o,{messages:l,executable:f,maxRoundtrips:e.maxToolRoundtrips??5,request:n,next:c=>e.provider.chat({...s,messages:c}).then(m=>m.stream)}):o);return new Response(d,{status:200,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}catch(s){const o=s instanceof Error?s.message:String(s);return new Response(JSON.stringify({error:o}),{status:500,headers:{"Content-Type":"application/json"}})}}}function w(e,a){let n;return new ReadableStream({async start(t){const l=[...a.messages];let u=e;try{for(let s=0;;s++){let o="";const f=new Map;let d;n=u.getReader();for(let i=await n.read();!i.done;i=await n.read()){const r=i.value;if(r.type==="finish"){d=r;continue}if(r.type==="text-delta"&&(o+=r.text),r.type==="tool-call-start"&&f.set(r.toolCallId,{name:r.toolName,args:""}),r.type==="tool-call-delta"){const g=f.get(r.toolCallId);g&&(g.args+=r.args)}t.enqueue(r)}const c=[...f].map(([i,r])=>({id:i,name:r.name,args:x(r.args)})),m=c.filter(i=>a.executable.has(i.name));if(d?.finishReason!=="tool-calls"||m.length===0||s>=a.maxRoundtrips){d&&t.enqueue(d);break}l.push({role:"assistant",content:o,toolCalls:c});for(const i of m)try{const r=await a.executable.get(i.name).execute(i.args,{request:a.request,toolCallId:i.id});t.enqueue({type:"tool-result",toolCallId:i.id,result:r}),l.push({role:"tool",toolCallId:i.id,content:JSON.stringify(r??null)})}catch(r){const g=r instanceof Error?r.message:String(r);t.enqueue({type:"tool-result",toolCallId:i.id,error:g}),l.push({role:"tool",toolCallId:i.id,content:JSON.stringify({error:g})})}if(m.length<c.length){t.enqueue(d);break}u=await a.next(l)}}catch(s){if(a.request.signal.aborted)return;t.enqueue({type:"error",error:s instanceof Error?s:new Error(String(s))})}t.close()},cancel(t){return n?.cancel(t)}})}function x(e){if(!e)return{};try{return JSON.parse(e)}catch{return e}}function S(e){const a=e.interval??6e4,n=e.key??C,t=new Map;return l=>{const u=Date.now(),s=n(l);if(t.size>1e4)for(const[f,d]of t)u-d.start>=a&&t.delete(f);let o=t.get(s);if((!o||u-o.start>=a)&&(o={start:u,count:0},t.set(s,o)),o.count>=e.maxRequests)return o.start+a-u;o.count++}}function C(e){return e.headers.get("x-forwarded-for")?.split(",")[0].trim()||e.headers.get("x-real-ip")||"global"}function y(e){const a=new TextEncoder;return e.pipeThrough(new TransformStream({transform(n,t){const l=v(n);l&&t.enqueue(a.encode(`data: ${l}
|
|
2
2
|
|
|
3
3
|
`))},flush(n){n.enqueue(a.encode(`data: [DONE]
|
|
4
4
|
|
|
5
|
-
`))}}))}function
|
|
6
|
-
`);this.buffer=o.pop();for(const c of o){const u=c.trim();u.startsWith("data: ")&&u!=="data: [DONE]"&&t.enqueue(u.slice(6))}},flush(s){const t=this.buffer.trim();t.startsWith("data: ")&&t!=="data: [DONE]"&&s.enqueue(t.slice(6))}})).pipeThrough(new TransformStream({transform(s,t){let o;try{o=JSON.parse(s)}catch{return}if(o.type==="error"){t.enqueue({type:"error",error:new Error(o.message??"Unknown proxy error")});return}o.type==="finish"&&r({finishReason:o.finishReason,usage:o.usage}),t.enqueue(o)}})),response:i}}}}exports.createLLMHandler=l;exports.createProxyProvider=h;exports.streamToSSE=d;
|
|
5
|
+
`))}}))}function v(e){return e.type==="error"?JSON.stringify({type:"error",message:e.error.message}):JSON.stringify(e)}function R(e){return p.createSSEProvider({id:"proxy",name:"Proxy",url:e})}exports.createLLMHandler=h;exports.createProxyProvider=R;exports.streamToSSE=y;
|
package/dist/server.d.ts
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import type { LLMProvider, ChatMessage, ChatStreamChunk } from './providers/types';
|
|
2
2
|
import type { ToolDefinition } from './types';
|
|
3
|
+
export interface ServerToolDefinition extends ToolDefinition {
|
|
4
|
+
execute?: (args: unknown, context: {
|
|
5
|
+
request: Request;
|
|
6
|
+
toolCallId: string;
|
|
7
|
+
}) => unknown | Promise<unknown>;
|
|
8
|
+
}
|
|
3
9
|
export interface LLMHandlerOptions {
|
|
4
10
|
provider: LLMProvider;
|
|
5
11
|
model?: string;
|
|
6
12
|
system?: string;
|
|
7
|
-
tools?:
|
|
13
|
+
tools?: ServerToolDefinition[];
|
|
14
|
+
maxToolRoundtrips?: number;
|
|
8
15
|
maxMessages?: number;
|
|
9
16
|
rateLimit?: ServerRateLimitOptions;
|
|
10
17
|
onRequest?: (body: LLMRequestBody) => LLMRequestBody | Promise<LLMRequestBody>;
|
package/dist/server.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { c as
|
|
2
|
-
function
|
|
3
|
-
const a = e.rateLimit ?
|
|
1
|
+
import { c as y } from "./sse-DU5m1n6r.js";
|
|
2
|
+
function v(e) {
|
|
3
|
+
const a = e.rateLimit ? w(e.rateLimit) : void 0;
|
|
4
4
|
return async (n) => {
|
|
5
5
|
if (n.method !== "POST")
|
|
6
6
|
return new Response(JSON.stringify({ error: "Method not allowed" }), {
|
|
@@ -18,32 +18,39 @@ function h(e) {
|
|
|
18
18
|
}
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
|
-
let
|
|
21
|
+
let t;
|
|
22
22
|
try {
|
|
23
|
-
|
|
23
|
+
t = await n.json();
|
|
24
24
|
} catch {
|
|
25
25
|
return new Response(JSON.stringify({ error: "Invalid JSON body" }), {
|
|
26
26
|
status: 400,
|
|
27
27
|
headers: { "Content-Type": "application/json" }
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
|
-
if (!
|
|
30
|
+
if (!t.messages?.length)
|
|
31
31
|
return new Response(JSON.stringify({ error: "messages array is required" }), {
|
|
32
32
|
status: 400,
|
|
33
33
|
headers: { "Content-Type": "application/json" }
|
|
34
34
|
});
|
|
35
|
-
e.maxMessages &&
|
|
36
|
-
const
|
|
37
|
-
|
|
35
|
+
e.maxMessages && t.messages.length > e.maxMessages && (t.messages = t.messages.slice(-e.maxMessages)), e.onRequest && (t = await e.onRequest(t));
|
|
36
|
+
const l = [], u = t.system ?? e.system;
|
|
37
|
+
u && l.push({ role: "system", content: u }), l.push(...t.messages);
|
|
38
38
|
try {
|
|
39
39
|
e.provider.validateConfig();
|
|
40
|
-
const
|
|
41
|
-
model:
|
|
42
|
-
|
|
43
|
-
tools: e.tools,
|
|
40
|
+
const s = {
|
|
41
|
+
model: t.model ?? e.model ?? "gpt-4o",
|
|
42
|
+
tools: e.tools?.map(({ execute: c, ...m }) => m),
|
|
44
43
|
signal: n.signal
|
|
45
|
-
}
|
|
46
|
-
|
|
44
|
+
}, { stream: o } = await e.provider.chat({ ...s, messages: l }), f = new Map(
|
|
45
|
+
(e.tools ?? []).filter((c) => c.execute).map((c) => [c.name, c])
|
|
46
|
+
), d = S(f.size > 0 ? p(o, {
|
|
47
|
+
messages: l,
|
|
48
|
+
executable: f,
|
|
49
|
+
maxRoundtrips: e.maxToolRoundtrips ?? 5,
|
|
50
|
+
request: n,
|
|
51
|
+
next: (c) => e.provider.chat({ ...s, messages: c }).then((m) => m.stream)
|
|
52
|
+
}) : o);
|
|
53
|
+
return new Response(d, {
|
|
47
54
|
status: 200,
|
|
48
55
|
headers: {
|
|
49
56
|
"Content-Type": "text/event-stream",
|
|
@@ -52,36 +59,99 @@ function h(e) {
|
|
|
52
59
|
}
|
|
53
60
|
});
|
|
54
61
|
} catch (s) {
|
|
55
|
-
const
|
|
56
|
-
return new Response(JSON.stringify({ error:
|
|
62
|
+
const o = s instanceof Error ? s.message : String(s);
|
|
63
|
+
return new Response(JSON.stringify({ error: o }), {
|
|
57
64
|
status: 500,
|
|
58
65
|
headers: { "Content-Type": "application/json" }
|
|
59
66
|
});
|
|
60
67
|
}
|
|
61
68
|
};
|
|
62
69
|
}
|
|
63
|
-
function
|
|
64
|
-
|
|
65
|
-
return (
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
70
|
+
function p(e, a) {
|
|
71
|
+
let n;
|
|
72
|
+
return new ReadableStream({
|
|
73
|
+
async start(t) {
|
|
74
|
+
const l = [...a.messages];
|
|
75
|
+
let u = e;
|
|
76
|
+
try {
|
|
77
|
+
for (let s = 0; ; s++) {
|
|
78
|
+
let o = "";
|
|
79
|
+
const f = /* @__PURE__ */ new Map();
|
|
80
|
+
let d;
|
|
81
|
+
n = u.getReader();
|
|
82
|
+
for (let i = await n.read(); !i.done; i = await n.read()) {
|
|
83
|
+
const r = i.value;
|
|
84
|
+
if (r.type === "finish") {
|
|
85
|
+
d = r;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (r.type === "text-delta" && (o += r.text), r.type === "tool-call-start" && f.set(r.toolCallId, { name: r.toolName, args: "" }), r.type === "tool-call-delta") {
|
|
89
|
+
const g = f.get(r.toolCallId);
|
|
90
|
+
g && (g.args += r.args);
|
|
91
|
+
}
|
|
92
|
+
t.enqueue(r);
|
|
93
|
+
}
|
|
94
|
+
const c = [...f].map(([i, r]) => ({ id: i, name: r.name, args: h(r.args) })), m = c.filter((i) => a.executable.has(i.name));
|
|
95
|
+
if (d?.finishReason !== "tool-calls" || m.length === 0 || s >= a.maxRoundtrips) {
|
|
96
|
+
d && t.enqueue(d);
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
l.push({ role: "assistant", content: o, toolCalls: c });
|
|
100
|
+
for (const i of m)
|
|
101
|
+
try {
|
|
102
|
+
const r = await a.executable.get(i.name).execute(i.args, { request: a.request, toolCallId: i.id });
|
|
103
|
+
t.enqueue({ type: "tool-result", toolCallId: i.id, result: r }), l.push({ role: "tool", toolCallId: i.id, content: JSON.stringify(r ?? null) });
|
|
104
|
+
} catch (r) {
|
|
105
|
+
const g = r instanceof Error ? r.message : String(r);
|
|
106
|
+
t.enqueue({ type: "tool-result", toolCallId: i.id, error: g }), l.push({ role: "tool", toolCallId: i.id, content: JSON.stringify({ error: g }) });
|
|
107
|
+
}
|
|
108
|
+
if (m.length < c.length) {
|
|
109
|
+
t.enqueue(d);
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
u = await a.next(l);
|
|
113
|
+
}
|
|
114
|
+
} catch (s) {
|
|
115
|
+
if (a.request.signal.aborted) return;
|
|
116
|
+
t.enqueue({ type: "error", error: s instanceof Error ? s : new Error(String(s)) });
|
|
117
|
+
}
|
|
118
|
+
t.close();
|
|
119
|
+
},
|
|
120
|
+
cancel(t) {
|
|
121
|
+
return n?.cancel(t);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
function h(e) {
|
|
126
|
+
if (!e) return {};
|
|
127
|
+
try {
|
|
128
|
+
return JSON.parse(e);
|
|
129
|
+
} catch {
|
|
130
|
+
return e;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function w(e) {
|
|
134
|
+
const a = e.interval ?? 6e4, n = e.key ?? x, t = /* @__PURE__ */ new Map();
|
|
135
|
+
return (l) => {
|
|
136
|
+
const u = Date.now(), s = n(l);
|
|
137
|
+
if (t.size > 1e4)
|
|
138
|
+
for (const [f, d] of t)
|
|
139
|
+
u - d.start >= a && t.delete(f);
|
|
140
|
+
let o = t.get(s);
|
|
141
|
+
if ((!o || u - o.start >= a) && (o = { start: u, count: 0 }, t.set(s, o)), o.count >= e.maxRequests)
|
|
142
|
+
return o.start + a - u;
|
|
143
|
+
o.count++;
|
|
74
144
|
};
|
|
75
145
|
}
|
|
76
|
-
function
|
|
146
|
+
function x(e) {
|
|
77
147
|
return e.headers.get("x-forwarded-for")?.split(",")[0].trim() || e.headers.get("x-real-ip") || "global";
|
|
78
148
|
}
|
|
79
|
-
function
|
|
149
|
+
function S(e) {
|
|
80
150
|
const a = new TextEncoder();
|
|
81
151
|
return e.pipeThrough(new TransformStream({
|
|
82
|
-
transform(n,
|
|
83
|
-
const
|
|
84
|
-
|
|
152
|
+
transform(n, t) {
|
|
153
|
+
const l = C(n);
|
|
154
|
+
l && t.enqueue(a.encode(`data: ${l}
|
|
85
155
|
|
|
86
156
|
`));
|
|
87
157
|
},
|
|
@@ -92,75 +162,14 @@ function p(e) {
|
|
|
92
162
|
}
|
|
93
163
|
}));
|
|
94
164
|
}
|
|
95
|
-
function
|
|
165
|
+
function C(e) {
|
|
96
166
|
return e.type === "error" ? JSON.stringify({ type: "error", message: e.error.message }) : JSON.stringify(e);
|
|
97
167
|
}
|
|
98
|
-
function
|
|
99
|
-
return {
|
|
100
|
-
id: "proxy",
|
|
101
|
-
supportsStreaming: !0,
|
|
102
|
-
validateConfig() {
|
|
103
|
-
},
|
|
104
|
-
async chat(a) {
|
|
105
|
-
const n = await fetch(e, {
|
|
106
|
-
method: "POST",
|
|
107
|
-
headers: { "Content-Type": "application/json" },
|
|
108
|
-
body: JSON.stringify({
|
|
109
|
-
messages: a.messages,
|
|
110
|
-
model: a.model
|
|
111
|
-
}),
|
|
112
|
-
signal: a.signal
|
|
113
|
-
});
|
|
114
|
-
if (!n.ok)
|
|
115
|
-
throw await u("proxy", "Proxy", n);
|
|
116
|
-
if (!n.body)
|
|
117
|
-
throw new Error("[vue-llm] No response body from proxy");
|
|
118
|
-
let r;
|
|
119
|
-
const i = new Promise((s) => {
|
|
120
|
-
r = s;
|
|
121
|
-
});
|
|
122
|
-
return { stream: n.body.pipeThrough(new TextDecoderStream()).pipeThrough(new TransformStream({
|
|
123
|
-
buffer: "",
|
|
124
|
-
transform(s, t) {
|
|
125
|
-
this.buffer += s;
|
|
126
|
-
const o = this.buffer.split(`
|
|
127
|
-
`);
|
|
128
|
-
this.buffer = o.pop();
|
|
129
|
-
for (const c of o) {
|
|
130
|
-
const m = c.trim();
|
|
131
|
-
m.startsWith("data: ") && m !== "data: [DONE]" && t.enqueue(m.slice(6));
|
|
132
|
-
}
|
|
133
|
-
},
|
|
134
|
-
flush(s) {
|
|
135
|
-
const t = this.buffer.trim();
|
|
136
|
-
t.startsWith("data: ") && t !== "data: [DONE]" && s.enqueue(t.slice(6));
|
|
137
|
-
}
|
|
138
|
-
})).pipeThrough(new TransformStream({
|
|
139
|
-
transform(s, t) {
|
|
140
|
-
let o;
|
|
141
|
-
try {
|
|
142
|
-
o = JSON.parse(s);
|
|
143
|
-
} catch {
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
if (o.type === "error") {
|
|
147
|
-
t.enqueue({
|
|
148
|
-
type: "error",
|
|
149
|
-
error: new Error(o.message ?? "Unknown proxy error")
|
|
150
|
-
});
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
o.type === "finish" && r({
|
|
154
|
-
finishReason: o.finishReason,
|
|
155
|
-
usage: o.usage
|
|
156
|
-
}), t.enqueue(o);
|
|
157
|
-
}
|
|
158
|
-
})), response: i };
|
|
159
|
-
}
|
|
160
|
-
};
|
|
168
|
+
function T(e) {
|
|
169
|
+
return y({ id: "proxy", name: "Proxy", url: e });
|
|
161
170
|
}
|
|
162
171
|
export {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
172
|
+
v as createLLMHandler,
|
|
173
|
+
T as createProxyProvider,
|
|
174
|
+
S as streamToSSE
|
|
166
175
|
};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { c as m } from "./errors-B3A_zmVE.js";
|
|
2
|
+
class y extends TransformStream {
|
|
3
|
+
constructor() {
|
|
4
|
+
let t = "", s = {};
|
|
5
|
+
const i = (e) => {
|
|
6
|
+
s.data !== void 0 && e.enqueue({
|
|
7
|
+
event: s.event,
|
|
8
|
+
data: s.data,
|
|
9
|
+
id: s.id,
|
|
10
|
+
retry: s.retry
|
|
11
|
+
}), s = {};
|
|
12
|
+
}, p = (e, a) => {
|
|
13
|
+
if (e === "") {
|
|
14
|
+
i(a);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (e.startsWith(":")) return;
|
|
18
|
+
const o = e.indexOf(":"), c = o === -1 ? e : e.slice(0, o), n = o === -1 ? "" : e.slice(o + 1).replace(/^ /, "");
|
|
19
|
+
switch (c) {
|
|
20
|
+
case "event":
|
|
21
|
+
s.event = n;
|
|
22
|
+
break;
|
|
23
|
+
case "data":
|
|
24
|
+
s.data = s.data !== void 0 ? s.data + `
|
|
25
|
+
` + n : n;
|
|
26
|
+
break;
|
|
27
|
+
case "id":
|
|
28
|
+
s.id = n;
|
|
29
|
+
break;
|
|
30
|
+
case "retry":
|
|
31
|
+
/^\d+$/.test(n) && (s.retry = parseInt(n, 10));
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
super({
|
|
36
|
+
transform(e, a) {
|
|
37
|
+
t += e;
|
|
38
|
+
const o = t.endsWith("\r") ? t.length - 1 : t.length, c = t.slice(0, o).split(/\r\n|\r|\n/);
|
|
39
|
+
t = (c.pop() ?? "") + t.slice(o);
|
|
40
|
+
for (const n of c) p(n, a);
|
|
41
|
+
},
|
|
42
|
+
flush(e) {
|
|
43
|
+
for (const a of t.split(/\r\n|\r|\n/))
|
|
44
|
+
a && p(a, e);
|
|
45
|
+
i(e);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function w(r) {
|
|
51
|
+
const t = r.id ?? "sse", s = r.mapEvent ?? S;
|
|
52
|
+
return {
|
|
53
|
+
id: t,
|
|
54
|
+
supportsStreaming: !0,
|
|
55
|
+
validateConfig() {
|
|
56
|
+
},
|
|
57
|
+
async chat(i) {
|
|
58
|
+
const p = typeof r.url == "function" ? r.url(i) : r.url, e = new Headers(typeof r.headers == "function" ? await r.headers() : r.headers);
|
|
59
|
+
e.has("Content-Type") || e.set("Content-Type", "application/json"), e.has("Accept") || e.set("Accept", "text/event-stream");
|
|
60
|
+
const a = await fetch(p, {
|
|
61
|
+
method: r.method ?? "POST",
|
|
62
|
+
headers: e,
|
|
63
|
+
credentials: r.credentials,
|
|
64
|
+
body: JSON.stringify(r.body ? r.body(i) : { messages: i.messages, model: i.model }),
|
|
65
|
+
signal: i.signal
|
|
66
|
+
});
|
|
67
|
+
if (!a.ok)
|
|
68
|
+
throw await m(t, r.name ?? "SSE backend", a);
|
|
69
|
+
if (!a.body)
|
|
70
|
+
throw new Error("[vue-llm] No response body from SSE backend");
|
|
71
|
+
let o;
|
|
72
|
+
const c = new Promise((d) => {
|
|
73
|
+
o = d;
|
|
74
|
+
});
|
|
75
|
+
let n = !1;
|
|
76
|
+
return { stream: a.body.pipeThrough(new TextDecoderStream()).pipeThrough(new y()).pipeThrough(new TransformStream({
|
|
77
|
+
transform(d, l) {
|
|
78
|
+
if (d.data === "[DONE]") return;
|
|
79
|
+
let h;
|
|
80
|
+
try {
|
|
81
|
+
h = JSON.parse(d.data);
|
|
82
|
+
} catch {
|
|
83
|
+
h = void 0;
|
|
84
|
+
}
|
|
85
|
+
const f = s({ ...d, json: h });
|
|
86
|
+
if (f)
|
|
87
|
+
for (const u of Array.isArray(f) ? f : [f])
|
|
88
|
+
u.type === "finish" && !n && (n = !0, o({ finishReason: u.finishReason, usage: u.usage })), l.enqueue(u);
|
|
89
|
+
},
|
|
90
|
+
flush(d) {
|
|
91
|
+
n || (n = !0, d.enqueue({ type: "finish", finishReason: "stop" }), o({ finishReason: "stop" }));
|
|
92
|
+
}
|
|
93
|
+
})), response: c };
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function S(r) {
|
|
98
|
+
const t = r.json;
|
|
99
|
+
return !t || typeof t != "object" || typeof t.type != "string" ? null : t.type === "error" ? { type: "error", error: new Error(t.message ?? "Unknown backend error") } : t;
|
|
100
|
+
}
|
|
101
|
+
export {
|
|
102
|
+
y as S,
|
|
103
|
+
w as c
|
|
104
|
+
};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";const y=require("./errors-CVDNhYqZ.cjs");class l extends TransformStream{constructor(){let r="",s={};const i=e=>{s.data!==void 0&&e.enqueue({event:s.event,data:s.data,id:s.id,retry:s.retry}),s={}},p=(e,a)=>{if(e===""){i(a);return}if(e.startsWith(":"))return;const o=e.indexOf(":"),c=o===-1?e:e.slice(0,o),n=o===-1?"":e.slice(o+1).replace(/^ /,"");switch(c){case"event":s.event=n;break;case"data":s.data=s.data!==void 0?s.data+`
|
|
2
|
+
`+n:n;break;case"id":s.id=n;break;case"retry":/^\d+$/.test(n)&&(s.retry=parseInt(n,10));break}};super({transform(e,a){r+=e;const o=r.endsWith("\r")?r.length-1:r.length,c=r.slice(0,o).split(/\r\n|\r|\n/);r=(c.pop()??"")+r.slice(o);for(const n of c)p(n,a)},flush(e){for(const a of r.split(/\r\n|\r|\n/))a&&p(a,e);i(e)}})}}function S(t){const r=t.id??"sse",s=t.mapEvent??v;return{id:r,supportsStreaming:!0,validateConfig(){},async chat(i){const p=typeof t.url=="function"?t.url(i):t.url,e=new Headers(typeof t.headers=="function"?await t.headers():t.headers);e.has("Content-Type")||e.set("Content-Type","application/json"),e.has("Accept")||e.set("Accept","text/event-stream");const a=await fetch(p,{method:t.method??"POST",headers:e,credentials:t.credentials,body:JSON.stringify(t.body?t.body(i):{messages:i.messages,model:i.model}),signal:i.signal});if(!a.ok)throw await y.createAPIError(r,t.name??"SSE backend",a);if(!a.body)throw new Error("[vue-llm] No response body from SSE backend");let o;const c=new Promise(d=>{o=d});let n=!1;return{stream:a.body.pipeThrough(new TextDecoderStream).pipeThrough(new l).pipeThrough(new TransformStream({transform(d,m){if(d.data==="[DONE]")return;let h;try{h=JSON.parse(d.data)}catch{h=void 0}const u=s({...d,json:h});if(u)for(const f of Array.isArray(u)?u:[u])f.type==="finish"&&!n&&(n=!0,o({finishReason:f.finishReason,usage:f.usage})),m.enqueue(f)},flush(d){n||(n=!0,d.enqueue({type:"finish",finishReason:"stop"}),o({finishReason:"stop"}))}})),response:c}}}}function v(t){const r=t.json;return!r||typeof r!="object"||typeof r.type!="string"?null:r.type==="error"?{type:"error",error:new Error(r.message??"Unknown backend error")}:r}exports.SSEParserStream=l;exports.createSSEProvider=S;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vue-llm",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "AI/LLM composables for Vue — useChat, useCompletion, streaming, multi-provider. The missing AI SDK for Vue.",
|
|
6
6
|
"keywords": [
|
|
@@ -77,11 +77,12 @@
|
|
|
77
77
|
"prepublishOnly": "npm run build",
|
|
78
78
|
"docs:dev": "vitepress dev docs",
|
|
79
79
|
"docs:build": "vitepress build docs",
|
|
80
|
-
"docs:preview": "vitepress preview docs"
|
|
80
|
+
"docs:preview": "vitepress preview docs",
|
|
81
|
+
"test:vue2": "vitest run --config vitest.vue2.config.ts"
|
|
81
82
|
},
|
|
82
83
|
"peerDependencies": {
|
|
83
84
|
"@nuxt/kit": "^3.0.0 || ^4.0.0",
|
|
84
|
-
"vue": "^3.3.0"
|
|
85
|
+
"vue": "^2.7.0 || ^3.3.0"
|
|
85
86
|
},
|
|
86
87
|
"peerDependenciesMeta": {
|
|
87
88
|
"@nuxt/kit": {
|
|
@@ -102,6 +103,7 @@
|
|
|
102
103
|
"vitepress": "^1.6.4",
|
|
103
104
|
"vitest": "^3.2.7",
|
|
104
105
|
"vue": "^3.5.13",
|
|
105
|
-
"vue-tsc": "^2.2.0"
|
|
106
|
+
"vue-tsc": "^2.2.0",
|
|
107
|
+
"vue2": "npm:vue@^2.7.16"
|
|
106
108
|
}
|
|
107
109
|
}
|