vue-llm 0.4.0 → 0.5.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/dist/server.cjs +6 -0
- package/dist/server.d.ts +18 -0
- package/dist/server.js +139 -0
- package/package.json +6 -1
package/dist/server.cjs
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function d(r){return async t=>{if(t.method!=="POST")return new Response(JSON.stringify({error:"Method not allowed"}),{status:405,headers:{"Content-Type":"application/json"}});let e;try{e=await t.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON body"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!e.messages?.length)return new Response(JSON.stringify({error:"messages array is required"}),{status:400,headers:{"Content-Type":"application/json"}});r.maxMessages&&e.messages.length>r.maxMessages&&(e.messages=e.messages.slice(-r.maxMessages)),r.onRequest&&(e=await r.onRequest(e));const a=[],i=e.system??r.system;i&&a.push({role:"system",content:i}),a.push(...e.messages);try{r.provider.validateConfig();const{stream:m}=await r.provider.chat({model:e.model??r.model??"gpt-4o",messages:a,tools:r.tools,signal:t.signal}),s=f(m);return new Response(s,{status:200,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}catch(m){const s=m instanceof Error?m.message:String(m);return new Response(JSON.stringify({error:s}),{status:500,headers:{"Content-Type":"application/json"}})}}}function f(r){const t=new TextEncoder;return r.pipeThrough(new TransformStream({transform(e,a){const i=p(e);i&&a.enqueue(t.encode(`data: ${i}
|
|
2
|
+
|
|
3
|
+
`))},flush(e){e.enqueue(t.encode(`data: [DONE]
|
|
4
|
+
|
|
5
|
+
`))}}))}function p(r){return r.type==="error"?JSON.stringify({type:"error",message:r.error.message}):JSON.stringify(r)}function l(r){return{id:"proxy",supportsStreaming:!0,validateConfig(){},async chat(t){const e=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({messages:t.messages,model:t.model}),signal:t.signal});if(!e.ok){const s=await e.text();throw new Error(`[vue-llm] Proxy error ${e.status}: ${s}`)}if(!e.body)throw new Error("[vue-llm] No response body from proxy");let a;const i=new Promise(s=>{a=s});return{stream:e.body.pipeThrough(new TextDecoderStream).pipeThrough(new TransformStream({buffer:"",transform(s,o){this.buffer+=s;const n=this.buffer.split(`
|
|
6
|
+
`);this.buffer=n.pop();for(const c of n){const u=c.trim();u.startsWith("data: ")&&u!=="data: [DONE]"&&o.enqueue(u.slice(6))}},flush(s){const o=this.buffer.trim();o.startsWith("data: ")&&o!=="data: [DONE]"&&s.enqueue(o.slice(6))}})).pipeThrough(new TransformStream({transform(s,o){let n;try{n=JSON.parse(s)}catch{return}if(n.type==="error"){o.enqueue({type:"error",error:new Error(n.message??"Unknown proxy error")});return}n.type==="finish"&&a({finishReason:n.finishReason,usage:n.usage}),o.enqueue(n)}})),response:i}}}}exports.createLLMHandler=d;exports.createProxyProvider=l;exports.streamToSSE=f;
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { LLMProvider, ChatMessage, ChatStreamChunk } from './providers/types';
|
|
2
|
+
import type { ToolDefinition } from './types';
|
|
3
|
+
export interface LLMHandlerOptions {
|
|
4
|
+
provider: LLMProvider;
|
|
5
|
+
model?: string;
|
|
6
|
+
system?: string;
|
|
7
|
+
tools?: ToolDefinition[];
|
|
8
|
+
maxMessages?: number;
|
|
9
|
+
onRequest?: (body: LLMRequestBody) => LLMRequestBody | Promise<LLMRequestBody>;
|
|
10
|
+
}
|
|
11
|
+
export interface LLMRequestBody {
|
|
12
|
+
messages: ChatMessage[];
|
|
13
|
+
model?: string;
|
|
14
|
+
system?: string;
|
|
15
|
+
}
|
|
16
|
+
export declare function createLLMHandler(options: LLMHandlerOptions): (request: Request) => Promise<Response>;
|
|
17
|
+
export declare function streamToSSE(stream: ReadableStream<ChatStreamChunk>): ReadableStream<Uint8Array>;
|
|
18
|
+
export declare function createProxyProvider(baseURL: string): LLMProvider;
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
function d(r) {
|
|
2
|
+
return async (t) => {
|
|
3
|
+
if (t.method !== "POST")
|
|
4
|
+
return new Response(JSON.stringify({ error: "Method not allowed" }), {
|
|
5
|
+
status: 405,
|
|
6
|
+
headers: { "Content-Type": "application/json" }
|
|
7
|
+
});
|
|
8
|
+
let e;
|
|
9
|
+
try {
|
|
10
|
+
e = await t.json();
|
|
11
|
+
} catch {
|
|
12
|
+
return new Response(JSON.stringify({ error: "Invalid JSON body" }), {
|
|
13
|
+
status: 400,
|
|
14
|
+
headers: { "Content-Type": "application/json" }
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
if (!e.messages?.length)
|
|
18
|
+
return new Response(JSON.stringify({ error: "messages array is required" }), {
|
|
19
|
+
status: 400,
|
|
20
|
+
headers: { "Content-Type": "application/json" }
|
|
21
|
+
});
|
|
22
|
+
r.maxMessages && e.messages.length > r.maxMessages && (e.messages = e.messages.slice(-r.maxMessages)), r.onRequest && (e = await r.onRequest(e));
|
|
23
|
+
const a = [], i = e.system ?? r.system;
|
|
24
|
+
i && a.push({ role: "system", content: i }), a.push(...e.messages);
|
|
25
|
+
try {
|
|
26
|
+
r.provider.validateConfig();
|
|
27
|
+
const { stream: m } = await r.provider.chat({
|
|
28
|
+
model: e.model ?? r.model ?? "gpt-4o",
|
|
29
|
+
messages: a,
|
|
30
|
+
tools: r.tools,
|
|
31
|
+
signal: t.signal
|
|
32
|
+
}), s = c(m);
|
|
33
|
+
return new Response(s, {
|
|
34
|
+
status: 200,
|
|
35
|
+
headers: {
|
|
36
|
+
"Content-Type": "text/event-stream",
|
|
37
|
+
"Cache-Control": "no-cache",
|
|
38
|
+
Connection: "keep-alive"
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
} catch (m) {
|
|
42
|
+
const s = m instanceof Error ? m.message : String(m);
|
|
43
|
+
return new Response(JSON.stringify({ error: s }), {
|
|
44
|
+
status: 500,
|
|
45
|
+
headers: { "Content-Type": "application/json" }
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function c(r) {
|
|
51
|
+
const t = new TextEncoder();
|
|
52
|
+
return r.pipeThrough(new TransformStream({
|
|
53
|
+
transform(e, a) {
|
|
54
|
+
const i = p(e);
|
|
55
|
+
i && a.enqueue(t.encode(`data: ${i}
|
|
56
|
+
|
|
57
|
+
`));
|
|
58
|
+
},
|
|
59
|
+
flush(e) {
|
|
60
|
+
e.enqueue(t.encode(`data: [DONE]
|
|
61
|
+
|
|
62
|
+
`));
|
|
63
|
+
}
|
|
64
|
+
}));
|
|
65
|
+
}
|
|
66
|
+
function p(r) {
|
|
67
|
+
return r.type === "error" ? JSON.stringify({ type: "error", message: r.error.message }) : JSON.stringify(r);
|
|
68
|
+
}
|
|
69
|
+
function g(r) {
|
|
70
|
+
return {
|
|
71
|
+
id: "proxy",
|
|
72
|
+
supportsStreaming: !0,
|
|
73
|
+
validateConfig() {
|
|
74
|
+
},
|
|
75
|
+
async chat(t) {
|
|
76
|
+
const e = await fetch(r, {
|
|
77
|
+
method: "POST",
|
|
78
|
+
headers: { "Content-Type": "application/json" },
|
|
79
|
+
body: JSON.stringify({
|
|
80
|
+
messages: t.messages,
|
|
81
|
+
model: t.model
|
|
82
|
+
}),
|
|
83
|
+
signal: t.signal
|
|
84
|
+
});
|
|
85
|
+
if (!e.ok) {
|
|
86
|
+
const s = await e.text();
|
|
87
|
+
throw new Error(`[vue-llm] Proxy error ${e.status}: ${s}`);
|
|
88
|
+
}
|
|
89
|
+
if (!e.body)
|
|
90
|
+
throw new Error("[vue-llm] No response body from proxy");
|
|
91
|
+
let a;
|
|
92
|
+
const i = new Promise((s) => {
|
|
93
|
+
a = s;
|
|
94
|
+
});
|
|
95
|
+
return { stream: e.body.pipeThrough(new TextDecoderStream()).pipeThrough(new TransformStream({
|
|
96
|
+
buffer: "",
|
|
97
|
+
transform(s, o) {
|
|
98
|
+
this.buffer += s;
|
|
99
|
+
const n = this.buffer.split(`
|
|
100
|
+
`);
|
|
101
|
+
this.buffer = n.pop();
|
|
102
|
+
for (const u of n) {
|
|
103
|
+
const f = u.trim();
|
|
104
|
+
f.startsWith("data: ") && f !== "data: [DONE]" && o.enqueue(f.slice(6));
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
flush(s) {
|
|
108
|
+
const o = this.buffer.trim();
|
|
109
|
+
o.startsWith("data: ") && o !== "data: [DONE]" && s.enqueue(o.slice(6));
|
|
110
|
+
}
|
|
111
|
+
})).pipeThrough(new TransformStream({
|
|
112
|
+
transform(s, o) {
|
|
113
|
+
let n;
|
|
114
|
+
try {
|
|
115
|
+
n = JSON.parse(s);
|
|
116
|
+
} catch {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (n.type === "error") {
|
|
120
|
+
o.enqueue({
|
|
121
|
+
type: "error",
|
|
122
|
+
error: new Error(n.message ?? "Unknown proxy error")
|
|
123
|
+
});
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
n.type === "finish" && a({
|
|
127
|
+
finishReason: n.finishReason,
|
|
128
|
+
usage: n.usage
|
|
129
|
+
}), o.enqueue(n);
|
|
130
|
+
}
|
|
131
|
+
})), response: i };
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
export {
|
|
136
|
+
d as createLLMHandler,
|
|
137
|
+
g as createProxyProvider,
|
|
138
|
+
c as streamToSSE
|
|
139
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vue-llm",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.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": [
|
|
@@ -50,6 +50,11 @@
|
|
|
50
50
|
"types": "./dist/components.d.ts",
|
|
51
51
|
"import": "./dist/components.js"
|
|
52
52
|
},
|
|
53
|
+
"./server": {
|
|
54
|
+
"types": "./dist/server.d.ts",
|
|
55
|
+
"import": "./dist/server.js",
|
|
56
|
+
"require": "./dist/server.cjs"
|
|
57
|
+
},
|
|
53
58
|
"./package.json": "./package.json"
|
|
54
59
|
},
|
|
55
60
|
"main": "./dist/index.cjs",
|