vue-llm 0.4.0 → 1.0.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 +146 -2
- package/dist/components/StreamMarkdown.vue.d.ts +1 -0
- package/dist/components.cjs +6 -1
- package/dist/components.d.ts +1 -0
- package/dist/components.js +125 -17
- package/dist/composables/use-chat.d.ts +11 -6
- package/dist/composables/use-completion.d.ts +2 -2
- package/dist/composables/use-object.d.ts +2 -2
- package/dist/composables/use-prompt.d.ts +3 -0
- package/dist/core/errors.d.ts +16 -0
- package/dist/core/markdown.d.ts +3 -0
- package/dist/core/prompt.d.ts +32 -0
- package/dist/core/storage.d.ts +15 -0
- package/dist/errors-B3A_zmVE.js +37 -0
- package/dist/errors-CVDNhYqZ.cjs +1 -0
- package/dist/file-DusIs8wR.cjs +1 -0
- package/dist/file-x_kW7BHa.js +49 -0
- package/dist/index.cjs +4 -2
- package/dist/index.d.ts +12 -4
- package/dist/index.js +553 -312
- package/dist/middleware.d.ts +23 -1
- package/dist/nuxt.cjs +1 -1
- package/dist/nuxt.js +11 -7
- package/dist/providers/types.d.ts +5 -0
- package/dist/providers.cjs +9 -7
- package/dist/providers.js +384 -286
- package/dist/server.cjs +6 -0
- package/dist/server.d.ts +24 -0
- package/dist/server.js +166 -0
- package/dist/utils/file.d.ts +21 -0
- package/dist/vue-llm.css +1 -1
- package/package.json +11 -2
package/dist/server.cjs
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const m=require("./errors-CVDNhYqZ.cjs");function l(e){const a=e.rateLimit?y(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 r;try{r=await n.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON body"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!r.messages?.length)return new Response(JSON.stringify({error:"messages array is required"}),{status:400,headers:{"Content-Type":"application/json"}});e.maxMessages&&r.messages.length>e.maxMessages&&(r.messages=r.messages.slice(-e.maxMessages)),e.onRequest&&(r=await e.onRequest(r));const i=[],f=r.system??e.system;f&&i.push({role:"system",content:f}),i.push(...r.messages);try{e.provider.validateConfig();const{stream:s}=await e.provider.chat({model:r.model??e.model??"gpt-4o",messages:i,tools:e.tools,signal:n.signal}),t=d(s);return new Response(t,{status:200,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}catch(s){const t=s instanceof Error?s.message:String(s);return new Response(JSON.stringify({error:t}),{status:500,headers:{"Content-Type":"application/json"}})}}}function y(e){const a=e.interval??6e4,n=e.key??g,r=new Map;return i=>{const f=Date.now(),s=n(i);if(r.size>1e4)for(const[o,c]of r)f-c.start>=a&&r.delete(o);let t=r.get(s);if((!t||f-t.start>=a)&&(t={start:f,count:0},r.set(s,t)),t.count>=e.maxRequests)return t.start+a-f;t.count++}}function g(e){return e.headers.get("x-forwarded-for")?.split(",")[0].trim()||e.headers.get("x-real-ip")||"global"}function d(e){const a=new TextEncoder;return e.pipeThrough(new TransformStream({transform(n,r){const i=p(n);i&&r.enqueue(a.encode(`data: ${i}
|
|
2
|
+
|
|
3
|
+
`))},flush(n){n.enqueue(a.encode(`data: [DONE]
|
|
4
|
+
|
|
5
|
+
`))}}))}function p(e){return e.type==="error"?JSON.stringify({type:"error",message:e.error.message}):JSON.stringify(e)}function h(e){return{id:"proxy",supportsStreaming:!0,validateConfig(){},async chat(a){const n=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({messages:a.messages,model:a.model}),signal:a.signal});if(!n.ok)throw await m.createAPIError("proxy","Proxy",n);if(!n.body)throw new Error("[vue-llm] No response body from proxy");let r;const i=new Promise(s=>{r=s});return{stream:n.body.pipeThrough(new TextDecoderStream).pipeThrough(new TransformStream({buffer:"",transform(s,t){this.buffer+=s;const o=this.buffer.split(`
|
|
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;
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
rateLimit?: ServerRateLimitOptions;
|
|
10
|
+
onRequest?: (body: LLMRequestBody) => LLMRequestBody | Promise<LLMRequestBody>;
|
|
11
|
+
}
|
|
12
|
+
export interface ServerRateLimitOptions {
|
|
13
|
+
maxRequests: number;
|
|
14
|
+
interval?: number;
|
|
15
|
+
key?: (request: Request) => string;
|
|
16
|
+
}
|
|
17
|
+
export interface LLMRequestBody {
|
|
18
|
+
messages: ChatMessage[];
|
|
19
|
+
model?: string;
|
|
20
|
+
system?: string;
|
|
21
|
+
}
|
|
22
|
+
export declare function createLLMHandler(options: LLMHandlerOptions): (request: Request) => Promise<Response>;
|
|
23
|
+
export declare function streamToSSE(stream: ReadableStream<ChatStreamChunk>): ReadableStream<Uint8Array>;
|
|
24
|
+
export declare function createProxyProvider(baseURL: string): LLMProvider;
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { c as u } from "./errors-B3A_zmVE.js";
|
|
2
|
+
function h(e) {
|
|
3
|
+
const a = e.rateLimit ? d(e.rateLimit) : void 0;
|
|
4
|
+
return async (n) => {
|
|
5
|
+
if (n.method !== "POST")
|
|
6
|
+
return new Response(JSON.stringify({ error: "Method not allowed" }), {
|
|
7
|
+
status: 405,
|
|
8
|
+
headers: { "Content-Type": "application/json" }
|
|
9
|
+
});
|
|
10
|
+
if (a) {
|
|
11
|
+
const s = a(n);
|
|
12
|
+
if (s !== void 0)
|
|
13
|
+
return new Response(JSON.stringify({ error: "Too many requests" }), {
|
|
14
|
+
status: 429,
|
|
15
|
+
headers: {
|
|
16
|
+
"Content-Type": "application/json",
|
|
17
|
+
"Retry-After": String(Math.ceil(s / 1e3))
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
let r;
|
|
22
|
+
try {
|
|
23
|
+
r = await n.json();
|
|
24
|
+
} catch {
|
|
25
|
+
return new Response(JSON.stringify({ error: "Invalid JSON body" }), {
|
|
26
|
+
status: 400,
|
|
27
|
+
headers: { "Content-Type": "application/json" }
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
if (!r.messages?.length)
|
|
31
|
+
return new Response(JSON.stringify({ error: "messages array is required" }), {
|
|
32
|
+
status: 400,
|
|
33
|
+
headers: { "Content-Type": "application/json" }
|
|
34
|
+
});
|
|
35
|
+
e.maxMessages && r.messages.length > e.maxMessages && (r.messages = r.messages.slice(-e.maxMessages)), e.onRequest && (r = await e.onRequest(r));
|
|
36
|
+
const i = [], f = r.system ?? e.system;
|
|
37
|
+
f && i.push({ role: "system", content: f }), i.push(...r.messages);
|
|
38
|
+
try {
|
|
39
|
+
e.provider.validateConfig();
|
|
40
|
+
const { stream: s } = await e.provider.chat({
|
|
41
|
+
model: r.model ?? e.model ?? "gpt-4o",
|
|
42
|
+
messages: i,
|
|
43
|
+
tools: e.tools,
|
|
44
|
+
signal: n.signal
|
|
45
|
+
}), t = p(s);
|
|
46
|
+
return new Response(t, {
|
|
47
|
+
status: 200,
|
|
48
|
+
headers: {
|
|
49
|
+
"Content-Type": "text/event-stream",
|
|
50
|
+
"Cache-Control": "no-cache",
|
|
51
|
+
Connection: "keep-alive"
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
} catch (s) {
|
|
55
|
+
const t = s instanceof Error ? s.message : String(s);
|
|
56
|
+
return new Response(JSON.stringify({ error: t }), {
|
|
57
|
+
status: 500,
|
|
58
|
+
headers: { "Content-Type": "application/json" }
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function d(e) {
|
|
64
|
+
const a = e.interval ?? 6e4, n = e.key ?? l, r = /* @__PURE__ */ new Map();
|
|
65
|
+
return (i) => {
|
|
66
|
+
const f = Date.now(), s = n(i);
|
|
67
|
+
if (r.size > 1e4)
|
|
68
|
+
for (const [o, c] of r)
|
|
69
|
+
f - c.start >= a && r.delete(o);
|
|
70
|
+
let t = r.get(s);
|
|
71
|
+
if ((!t || f - t.start >= a) && (t = { start: f, count: 0 }, r.set(s, t)), t.count >= e.maxRequests)
|
|
72
|
+
return t.start + a - f;
|
|
73
|
+
t.count++;
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function l(e) {
|
|
77
|
+
return e.headers.get("x-forwarded-for")?.split(",")[0].trim() || e.headers.get("x-real-ip") || "global";
|
|
78
|
+
}
|
|
79
|
+
function p(e) {
|
|
80
|
+
const a = new TextEncoder();
|
|
81
|
+
return e.pipeThrough(new TransformStream({
|
|
82
|
+
transform(n, r) {
|
|
83
|
+
const i = g(n);
|
|
84
|
+
i && r.enqueue(a.encode(`data: ${i}
|
|
85
|
+
|
|
86
|
+
`));
|
|
87
|
+
},
|
|
88
|
+
flush(n) {
|
|
89
|
+
n.enqueue(a.encode(`data: [DONE]
|
|
90
|
+
|
|
91
|
+
`));
|
|
92
|
+
}
|
|
93
|
+
}));
|
|
94
|
+
}
|
|
95
|
+
function g(e) {
|
|
96
|
+
return e.type === "error" ? JSON.stringify({ type: "error", message: e.error.message }) : JSON.stringify(e);
|
|
97
|
+
}
|
|
98
|
+
function w(e) {
|
|
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
|
+
};
|
|
161
|
+
}
|
|
162
|
+
export {
|
|
163
|
+
h as createLLMHandler,
|
|
164
|
+
w as createProxyProvider,
|
|
165
|
+
p as streamToSSE
|
|
166
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { FilePart } from '../types';
|
|
2
|
+
export type FileInput = File | FileList | FilePart | (File | FilePart)[];
|
|
3
|
+
export declare function fileToDataURL(file: Blob): Promise<string>;
|
|
4
|
+
export declare function convertFilesToParts(input: FileInput | undefined): Promise<FilePart[]>;
|
|
5
|
+
export interface ResolvedFile {
|
|
6
|
+
url: string;
|
|
7
|
+
mediaType: string;
|
|
8
|
+
filename?: string;
|
|
9
|
+
base64?: string;
|
|
10
|
+
}
|
|
11
|
+
export declare function resolveFilePart(part: {
|
|
12
|
+
url: string;
|
|
13
|
+
mediaType?: string;
|
|
14
|
+
filename?: string;
|
|
15
|
+
}): ResolvedFile;
|
|
16
|
+
export declare function isTextMediaType(mediaType: string): boolean;
|
|
17
|
+
export declare function parseDataURL(url: string): {
|
|
18
|
+
mediaType: string;
|
|
19
|
+
data: string;
|
|
20
|
+
} | null;
|
|
21
|
+
export declare function decodeBase64Text(data: string): string;
|
package/dist/vue-llm.css
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
.vue-llm-cursor[data-v-
|
|
1
|
+
.vue-llm-cursor[data-v-52dbf793]{animation:vue-llm-blink-52dbf793 .7s step-end infinite}@keyframes vue-llm-blink-52dbf793{50%{opacity:0}}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vue-llm",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.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",
|
|
@@ -69,7 +74,10 @@
|
|
|
69
74
|
"typecheck": "vue-tsc --noEmit",
|
|
70
75
|
"lint": "eslint .",
|
|
71
76
|
"lint:fix": "eslint . --fix",
|
|
72
|
-
"prepublishOnly": "npm run build"
|
|
77
|
+
"prepublishOnly": "npm run build",
|
|
78
|
+
"docs:dev": "vitepress dev docs",
|
|
79
|
+
"docs:build": "vitepress build docs",
|
|
80
|
+
"docs:preview": "vitepress preview docs"
|
|
73
81
|
},
|
|
74
82
|
"peerDependencies": {
|
|
75
83
|
"@nuxt/kit": "^3.0.0 || ^4.0.0",
|
|
@@ -91,6 +99,7 @@
|
|
|
91
99
|
"typescript": "^5.7.2",
|
|
92
100
|
"typescript-eslint": "^8.18.1",
|
|
93
101
|
"vite": "^7.3.7",
|
|
102
|
+
"vitepress": "^1.6.4",
|
|
94
103
|
"vitest": "^3.2.7",
|
|
95
104
|
"vue": "^3.5.13",
|
|
96
105
|
"vue-tsc": "^2.2.0"
|