vue-llm 1.1.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 +1 -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 +188 -185
- package/dist/utils/vue.d.ts +4 -0
- package/package.json +6 -4
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ AI/LLM composables for Vue. The missing AI SDK for the Vue ecosystem.
|
|
|
19
19
|
- **Streaming** — SSE parser, text/reasoning/tool-call deltas
|
|
20
20
|
- **Security** — `dangerouslyAllowBrowser` guard protects API keys
|
|
21
21
|
- **Nuxt module** — auto-imports, server-side runtime config
|
|
22
|
-
- **Zero runtime deps** — only `vue` as peer dependency
|
|
22
|
+
- **Zero runtime deps** — only `vue` as peer dependency (Vue 3.3+, composables also on Vue 2.7)
|
|
23
23
|
- **TypeScript** — full type safety, exported types
|
|
24
24
|
|
|
25
25
|
## Quick Start
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type Ref } from 'vue';
|
|
2
|
+
import { type MaybeRef, type MaybeRefOrGetter } from '../utils/vue';
|
|
2
3
|
import type { ChatStatus, FinishReason, ToolDefinition, UIMessage, Usage } from '../types';
|
|
3
4
|
import type { LLMProvider } from '../providers/types';
|
|
4
5
|
import type { ChatStorage } from '../core/storage';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type Ref } from 'vue';
|
|
2
|
+
import { type MaybeRefOrGetter } from '../utils/vue';
|
|
2
3
|
import type { ChatStatus, FinishReason, Usage } from '../types';
|
|
3
4
|
import type { LLMProvider } from '../providers/types';
|
|
4
5
|
export interface UseCompletionOptions {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type Ref } from 'vue';
|
|
2
|
+
import { type MaybeRefOrGetter } from '../utils/vue';
|
|
2
3
|
import type { ChatStatus, FinishReason, Usage } from '../types';
|
|
3
4
|
import type { LLMProvider } from '../providers/types';
|
|
4
5
|
export interface UseObjectOptions<T> {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
import { type ComputedRef
|
|
1
|
+
import { type ComputedRef } from 'vue';
|
|
2
|
+
import { type MaybeRefOrGetter } from '../utils/vue';
|
|
2
3
|
import { type PromptInput, type PromptTemplate, type PromptVariables } from '../core/prompt';
|
|
3
4
|
export declare function usePrompt<const T extends string, D = object>(template: T | PromptTemplate<T, D>, variables: MaybeRefOrGetter<PromptInput<PromptVariables<T>, D>>): ComputedRef<string>;
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const v=require("vue"),L=require("./file-DusIs8wR.cjs"),N=require("./errors-CVDNhYqZ.cjs");function O(t){return typeof t=="function"?t():v.unref(t)}class U{constructor(r,e,a){this.messages=r,this.status=e,this.generateId=a}messages;status;generateId;async processStream(r,e,a){this.status.value="streaming";const o=this.getOrCreateAssistantMessage(),s=new Map,l=[];let f="stop",i;const m=r.getReader();try{for(;!e.aborted;){const{done:d,value:c}=await m.read();if(d)break;if(this.applyChunk(o,c,s),c.type==="tool-call-end"){const y=s.get(c.toolCallId)??"{}";let p;try{p=JSON.parse(y)}catch{p=y}const h={toolCallId:c.toolCallId,toolName:this.getToolName(o,c.toolCallId),args:p};l.push(h);const g=this.findToolCallPart(o,c.toolCallId);if(g&&(g.args=p),a.onToolCall){g&&(g.state="running"),this.triggerReactivity();try{const C=await a.onToolCall(h);C!==void 0&&g&&(g.state="result",g.result=C,this.triggerReactivity())}catch(C){g&&(g.state="error",g.error=C instanceof Error?C.message:String(C),this.triggerReactivity())}}}if(c.type==="tool-result"){const y=this.findToolCallPart(o,c.toolCallId);y&&(y.state=c.error!==void 0?"error":"result",y.result=c.result,y.error=c.error);const p=l.findIndex(h=>h.toolCallId===c.toolCallId);p!==-1&&l.splice(p,1)}if(c.type==="finish"&&(f=c.finishReason,i=c.usage),c.type==="error")throw c.error;this.triggerReactivity()}}catch(d){if(!e.aborted){const c=d instanceof Error?d:new Error(String(d));throw a.onError?.(c),c}}finally{m.releaseLock()}return this.finalizeTextParts(o),this.triggerReactivity(),a.onFinish?.(o,{finishReason:f,usage:i}),{finishReason:f,usage:i,toolCalls:l}}getOrCreateAssistantMessage(){const r=this.messages.value,e=r[r.length-1];if(e?.role==="assistant")return e;const a={id:this.generateId(),role:"assistant",parts:[],createdAt:new Date};return r.push(a),a}applyChunk(r,e,a){switch(e.type){case"text-delta":{const o=r.parts[r.parts.length-1];o?.type==="text"&&o.state==="streaming"?o.text+=e.text:r.parts.push({type:"text",text:e.text,state:"streaming"});break}case"reasoning-delta":{const o=r.parts[r.parts.length-1];o?.type==="reasoning"&&o.state==="streaming"?o.text+=e.text:r.parts.push({type:"reasoning",text:e.text,state:"streaming"});break}case"tool-call-start":r.parts.push({type:"tool-call",toolCallId:e.toolCallId,toolName:e.toolName,args:void 0,state:"pending"}),a.set(e.toolCallId,"");break;case"tool-call-delta":a.set(e.toolCallId,(a.get(e.toolCallId)??"")+e.args);break}}finalizeTextParts(r){for(const e of r.parts)(e.type==="text"||e.type==="reasoning")&&e.state==="streaming"&&(e.state="done")}findToolCallPart(r,e){return r.parts.find(a=>a.type==="tool-call"&&a.toolCallId===e)}getToolName(r,e){return this.findToolCallPart(r,e)?.toolName??""}triggerReactivity(){this.messages.value=[...this.messages.value]}}const F="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";function A(t=16){const r=crypto.getRandomValues(new Uint8Array(t));let e="";for(let a=0;a<t;a++)e+=F[r[a]%F.length];return e}function V(t){const r=t.id??A(),e=v.ref(t.initialMessages??[]),a=v.ref("ready"),o=v.ref(void 0),s=v.ref("");let l=null;const f=v.ref(!t.storage),i=t.storage,m=n=>{t.onError?.(n instanceof Error?n:new Error(String(n)))};if(i){const n=e.value,S=u=>{u?.length&&e.value===n&&(e.value=u),f.value=!0};try{const u=i.load(r);u instanceof Promise?u.then(S,w=>{m(w),f.value=!0}):S(u)}catch(u){m(u),f.value=!0}v.watch([e,a,f],()=>{if(!(!f.value||a.value==="submitted"||a.value==="streaming"))try{const u=e.value.length>0?i.save(r,e.value):i.remove(r);u instanceof Promise&&u.catch(m)}catch(u){m(u)}},{deep:!0})}const d=new U(e,a,A),c=()=>{const n=[],S=[O(t.system),...e.value.filter(u=>u.role==="system").map(u=>u.parts.filter(w=>w.type==="text").map(w=>w.text).join(""))].filter(Boolean);S.length>0&&n.push({role:"system",content:S.join(`
|
|
2
2
|
|
|
3
|
-
`)});for(const u of e.value){if(u.role==="system")continue;const E=u.parts.filter(b=>b.type==="text").map(b=>b.text).join(""),x=u.parts.filter(b=>b.type==="tool-call").map(b=>({id:b.toolCallId,name:b.toolName,args:b.args}));if(u.role==="user"){const b=u.parts.filter(I=>I.type==="file");if(b.length===0){n.push({role:"user",content:E});continue}const
|
|
3
|
+
`)});for(const u of e.value){if(u.role==="system")continue;const E=u.parts.filter(b=>b.type==="text").map(b=>b.text).join(""),x=u.parts.filter(b=>b.type==="tool-call").map(b=>({id:b.toolCallId,name:b.toolName,args:b.args}));if(u.role==="user"){const b=u.parts.filter(I=>I.type==="file");if(b.length===0){n.push({role:"user",content:E});continue}const j=b.map(I=>({type:"file",url:I.url,mediaType:I.mediaType,filename:I.filename}));E&&j.push({type:"text",text:E}),n.push({role:"user",content:j})}else if(u.role==="assistant"){n.push({role:"assistant",content:E,toolCalls:x.length>0?x:void 0});for(const b of u.parts)b.type!=="tool-call"||b.state!=="result"||n.push({role:"tool",content:JSON.stringify(b.result),toolCallId:b.toolCallId})}}return n},y=async n=>{const S=t.maxToolRoundtrips??5;let u=0;for(;u<S;){const w=c();t.provider.validateConfig();const{stream:E}=await t.provider.chat({model:t.model,messages:w,tools:t.tools,signal:n,providerOptions:{...t.providerOptions,...O(t.body)}}),x=await d.processStream(E,n,{onFinish:t.onFinish,onError:t.onError,onToolCall:t.onToolCall});if(x.finishReason!=="tool-calls"||x.toolCalls.length===0)break;u++}},p=async n=>{const S=typeof n=="string"?n:n?.text??s.value;let u;try{u=typeof n=="object"?await L.convertFilesToParts(n.files):[]}catch(x){o.value=x instanceof Error?x:new Error(String(x)),a.value="error";return}if(!S.trim()&&u.length===0)return;(typeof n!="object"||n===null||!("text"in n))&&(s.value="");const w=[...u];S.trim()&&w.push({type:"text",text:S,state:"done"});const E={id:A(),role:"user",parts:w,createdAt:new Date};e.value=[...e.value,E],o.value=void 0,a.value="submitted",l?.abort(),l=new AbortController;try{await y(l.signal),a.value="ready"}catch(x){if(l.signal.aborted){a.value="ready";return}o.value=x instanceof Error?x:new Error(String(x)),a.value="error"}},h=async()=>{const n=e.value,S=n.reduce((u,w,E)=>w.role==="assistant"?E:u,-1);if(S!==-1){e.value=n.slice(0,S),o.value=void 0,a.value="submitted",l?.abort(),l=new AbortController;try{await y(l.signal),a.value="ready"}catch(u){if(l.signal.aborted){a.value="ready";return}o.value=u instanceof Error?u:new Error(String(u)),a.value="error"}}},g=()=>{l?.abort(),l=null,a.value="ready"},C=n=>{e.value=typeof n=="function"?n(e.value):n},R=n=>{n?.preventDefault?.(),s.value.trim()&&p()},P=v.computed(()=>a.value==="submitted"||a.value==="streaming"),T=({toolCallId:n,result:S})=>{for(const u of e.value)for(const w of u.parts)if(w.type==="tool-call"&&w.toolCallId===n){w.state="result",w.result=S,e.value=[...e.value];return}},M=()=>{o.value=void 0,a.value==="error"&&(a.value="ready")};return v.getCurrentScope()&&v.onScopeDispose(()=>{l?.abort()}),{messages:e,status:v.computed(()=>a.value),error:o,id:r,input:s,sendMessage:p,regenerate:h,stop:g,setMessages:C,handleSubmit:R,isLoading:P,addToolResult:T,clearError:M,isRestored:v.computed(()=>f.value)}}function W(t){const r=t.id??A(),e=v.ref(""),a=v.ref("ready"),o=v.ref(void 0),s=v.ref("");let l=null;const f=async y=>{const p=y??s.value;if(!p.trim())return e.value;y||(s.value=""),e.value="",o.value=void 0,a.value="submitted",l?.abort(),l=new AbortController;try{t.provider.validateConfig();const h=O(t.system),g=h?[{role:"system",content:h},{role:"user",content:p}]:[{role:"user",content:p}],{stream:C}=await t.provider.chat({model:t.model,messages:g,signal:l.signal});a.value="streaming";const R=C.getReader();let P="stop",T;for(;!l.signal.aborted;){const{done:M,value:n}=await R.read();if(M)break;if(n.type==="text-delta")e.value+=n.text;else if(n.type==="finish")P=n.finishReason,T=n.usage;else if(n.type==="error")throw n.error}return R.releaseLock(),a.value="ready",t.onFinish?.(e.value,{finishReason:P,usage:T}),e.value}catch(h){if(!l||l.signal.aborted)return a.value="ready",e.value;const g=h instanceof Error?h:new Error(String(h));return o.value=g,a.value="error",t.onError?.(g),e.value}},i=()=>{l?.abort(),l=null,a.value="ready"},m=()=>{o.value=void 0,a.value==="error"&&(a.value="ready")},d=y=>{y?.preventDefault?.(),s.value.trim()&&f()},c=v.computed(()=>a.value==="submitted"||a.value==="streaming");return v.getCurrentScope()&&v.onScopeDispose(()=>{l?.abort()}),{completion:e,status:v.computed(()=>a.value),error:o,isLoading:c,id:r,input:s,complete:f,stop:i,clearError:m,handleSubmit:d}}function _(t){const r=t.id??A(),e=v.ref(void 0),a=v.ref(void 0),o=v.ref("ready"),s=v.ref(void 0),l=v.ref("");let f=null;const i=async y=>{const p=y??l.value;if(!p.trim())return e.value;y||(l.value=""),e.value=void 0,a.value=void 0,s.value=void 0,o.value="submitted",f?.abort(),f=new AbortController;try{t.provider.validateConfig();const h=[O(t.system)??"","Respond with valid JSON matching this schema:",JSON.stringify(t.schema,null,2),"Output ONLY the JSON object, no other text."].filter(Boolean).join(`
|
|
4
4
|
|
|
5
|
-
`),{stream:
|
|
5
|
+
`),{stream:g}=await t.provider.chat({model:t.model,messages:[{role:"system",content:h},{role:"user",content:p}],signal:f.signal,providerOptions:{response_format:{type:"json_object"}}});o.value="streaming";const C=g.getReader();let R="",P="stop",T;for(;!f.signal.aborted;){const{done:M,value:n}=await C.read();if(M)break;if(n.type==="text-delta"){R+=n.text;try{a.value=k(R)}catch{}}else if(n.type==="finish")P=n.finishReason,T=n.usage;else if(n.type==="error")throw n.error}C.releaseLock();try{e.value=JSON.parse(R),a.value=e.value}catch{throw new Error(`Failed to parse JSON response: ${R.slice(0,200)}`)}return o.value="ready",t.onFinish?.(e.value,{finishReason:P,usage:T}),e.value}catch(h){if(f.signal.aborted)return o.value="ready",e.value;const g=h instanceof Error?h:new Error(String(h));s.value=g,o.value="error",t.onError?.(g);return}},m=()=>{f?.abort(),f=null,o.value="ready"},d=()=>{s.value=void 0,o.value==="error"&&(o.value="ready")},c=v.computed(()=>o.value==="submitted"||o.value==="streaming");return v.getCurrentScope()&&v.onScopeDispose(()=>{f?.abort()}),{object:e,partialObject:a,status:v.computed(()=>o.value),error:s,isLoading:c,id:r,input:l,submit:i,stop:m,clearError:d}}function k(t){let r=t.trim();if(!r.startsWith("{"))return;let e=0,a=!1,o=!1;for(let s=0;s<r.length;s++){const l=r[s];if(o){o=!1;continue}if(l==="\\"){o=!0;continue}if(l==='"'){a=!a;continue}a||(l==="{"&&e++,l==="}"&&e--)}for(;e>0;)r+="}",e--;try{return JSON.parse(r)}catch{return}}const J=/(\\)?\{\{\s*([A-Za-z_][\w-]*)\s*\}\}/g;function q(t,r={}){return{template:t,variables:$(t),render(e){return D(t,{...r.defaults,...e})}}}function H(t,r={}){const e=a=>{const o={...r.defaults,...a};return t.map(s=>({role:s.role,content:D(s.content,o)}))};return{messages:t,variables:[...new Set(t.flatMap(a=>$(a.content)))],render:e,toUIMessages(a){return e(a).map(o=>({id:A(),role:o.role,parts:[{type:"text",text:o.content,state:"done"}],createdAt:new Date}))}}}function D(t,r){return t.replace(J,(e,a,o)=>{if(a)return e.slice(1);const s=r[o];if(s==null)throw new Error(`[vue-llm] Missing prompt variable "${o}"`);return String(s)})}function $(t){const r=new Set;for(const[,e,a]of t.matchAll(J))e||r.add(a);return[...r]}function Y(t,r){const e=typeof t=="string"?q(t):t;return v.computed(()=>e.render(O(r)))}function Z(t,r){return r.length?{id:t.id,supportsStreaming:t.supportsStreaming,validateConfig(){t.validateConfig()},async chat(e){let a=e;for(const i of r)i.beforeRequest&&(a=await i.beforeRequest(a,t));const o=r.reduceRight((i,m)=>m.wrapChat?d=>m.wrapChat(i,d):i,i=>t.chat(i));let s;try{s=await o(a)}catch(i){let m=i instanceof Error?i:new Error(String(i));for(const d of r)if(d.onError){const c=await d.onError(m,a);c instanceof Error&&(m=c)}throw m}const f=r.some(i=>i.afterStream)?s.stream.pipeThrough(new TransformStream({async transform(i,m){let d=i;for(const c of r){if(!d)break;c.afterStream&&(d=await c.afterStream(d,a))}d&&m.enqueue(d)}})):s.stream;for(const i of r)i.onFinish&&i.onFinish(a,s);return{stream:f,response:s.response}}}:t}function G(t="vue-llm"){return{beforeRequest(r){return console.log(`[${t}] → ${r.model}`,r.messages.length,"messages"),r},afterStream(r){return r.type==="finish"&&console.log(`[${t}] ← finish:`,r.finishReason,r.usage??""),r},onError(r){console.error(`[${t}] error:`,r.message)}}}function K(t={},r){const e=typeof t=="number"?{maxRetries:t,initialDelay:r}:t,a=e.maxRetries??3,o=e.initialDelay??1e3,s=e.maxDelay??3e4,l=e.factor??2,f=e.jitter??!0,i=e.shouldRetry??z;return{async wrapChat(m,d){for(let c=1;;c++)try{return await m(d)}catch(y){const p=y instanceof Error?y:new Error(String(y));if(c>a||d.signal?.aborted||!i(p,c))throw p;const h=o*l**(c-1),g=p instanceof N.LLMAPIError?p.retryAfter:void 0,C=Math.min(s,g??(f?h*(.5+Math.random()*.5):h));e.onRetry?.({error:p,attempt:c,delay:C}),await ee(C,d.signal)}}}}function z(t){return t.name==="AbortError"?!1:t instanceof N.LLMAPIError?t.isRetryable:t instanceof TypeError}function Q(t){const r=t.interval??6e4,e=[];let a=0;const o=new Set,s=()=>{a--;for(const f of[...o])f()},l=async f=>{for(;;){f?.throwIfAborted();const i=Date.now();for(;e.length>0&&e[0]<=i-r;)e.shift();const m=t.maxRequests!==void 0&&e.length>=t.maxRequests,d=t.maxConcurrent!==void 0&&a>=t.maxConcurrent;if(!m&&!d){e.push(i),a++;return}await new Promise((c,y)=>{const p=()=>{clearTimeout(g),o.delete(p),f?.removeEventListener("abort",h),c()},h=()=>{p(),y(f.reason)},g=m?setTimeout(p,e[0]+r-i):void 0;o.add(p),f?.addEventListener("abort",h,{once:!0})})}};return{async wrapChat(f,i){await l(i.signal);let m;try{m=await f(i)}catch(d){throw s(),d}return{...m,stream:X(m.stream,s)}}}}function X(t,r){const e=t.getReader();let a=!1;const o=()=>{a||(a=!0,r())};return new ReadableStream({async pull(s){try{const{done:l,value:f}=await e.read();l?(o(),s.close()):s.enqueue(f)}catch(l){o(),s.error(l)}},cancel(s){return o(),e.cancel(s)}})}function ee(t,r){return new Promise((e,a)=>{if(r?.aborted)return a(r.reason);const o=setTimeout(()=>{r?.removeEventListener("abort",s),e()},t),s=()=>{clearTimeout(o),a(r.reason)};r?.addEventListener("abort",s,{once:!0})})}function te(t){return{beforeRequest(r){return{...r,providerOptions:{...r.providerOptions,headers:{...r.providerOptions?.headers,...t}}}}}}function re(t={}){const r=t.prefix??"vue-llm:chat:",e=()=>t.storage?t.storage:typeof localStorage<"u"?localStorage:void 0;return{load(a){const o=e()?.getItem(r+a);if(!o)return null;try{return B(o)}catch{return null}},save(a,o){e()?.setItem(r+a,JSON.stringify(o))},remove(a){e()?.removeItem(r+a)}}}function ae(){const t=new Map;return{load(r){const e=t.get(r);return e?B(e):null},save(r,e){t.set(r,JSON.stringify(e))},remove(r){t.delete(r)}}}function B(t){const r=JSON.parse(t);return Array.isArray(r)?r.map(e=>({...e,createdAt:e.createdAt?new Date(e.createdAt):void 0})):[]}exports.convertFilesToParts=L.convertFilesToParts;exports.fileToDataURL=L.fileToDataURL;exports.LLMAPIError=N.LLMAPIError;exports.createChatPrompt=H;exports.createMemoryStorage=ae;exports.createPrompt=q;exports.createWebStorage=re;exports.headerMiddleware=te;exports.isRetryableError=z;exports.loggingMiddleware=G;exports.rateLimitMiddleware=Q;exports.renderTemplate=D;exports.retryMiddleware=K;exports.useChat=V;exports.useCompletion=W;exports.useObject=_;exports.usePrompt=Y;exports.wrapProvider=Z;
|
package/dist/index.js
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
|
-
import { ref as x, watch as
|
|
2
|
-
import { c as
|
|
3
|
-
import { f as
|
|
4
|
-
import { L as
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { unref as q, ref as x, watch as U, computed as A, getCurrentScope as L, onScopeDispose as D } from "vue";
|
|
2
|
+
import { c as _ } from "./file-x_kW7BHa.js";
|
|
3
|
+
import { f as me } from "./file-x_kW7BHa.js";
|
|
4
|
+
import { L as F } from "./errors-B3A_zmVE.js";
|
|
5
|
+
function N(t) {
|
|
6
|
+
return typeof t == "function" ? t() : q(t);
|
|
7
|
+
}
|
|
8
|
+
class k {
|
|
9
|
+
constructor(r, e, a) {
|
|
10
|
+
this.messages = r, this.status = e, this.generateId = a;
|
|
8
11
|
}
|
|
9
12
|
messages;
|
|
10
13
|
status;
|
|
11
14
|
generateId;
|
|
12
|
-
async processStream(
|
|
15
|
+
async processStream(r, e, a) {
|
|
13
16
|
this.status.value = "streaming";
|
|
14
17
|
const o = this.getOrCreateAssistantMessage(), s = /* @__PURE__ */ new Map(), l = [];
|
|
15
18
|
let f = "stop", i;
|
|
16
|
-
const v =
|
|
19
|
+
const v = r.getReader();
|
|
17
20
|
try {
|
|
18
21
|
for (; !e.aborted; ) {
|
|
19
22
|
const { done: d, value: c } = await v.read();
|
|
@@ -60,7 +63,7 @@ class U {
|
|
|
60
63
|
return this.finalizeTextParts(o), this.triggerReactivity(), a.onFinish?.(o, { finishReason: f, usage: i }), { finishReason: f, usage: i, toolCalls: l };
|
|
61
64
|
}
|
|
62
65
|
getOrCreateAssistantMessage() {
|
|
63
|
-
const
|
|
66
|
+
const r = this.messages.value, e = r[r.length - 1];
|
|
64
67
|
if (e?.role === "assistant") return e;
|
|
65
68
|
const a = {
|
|
66
69
|
id: this.generateId(),
|
|
@@ -68,22 +71,22 @@ class U {
|
|
|
68
71
|
parts: [],
|
|
69
72
|
createdAt: /* @__PURE__ */ new Date()
|
|
70
73
|
};
|
|
71
|
-
return
|
|
74
|
+
return r.push(a), a;
|
|
72
75
|
}
|
|
73
|
-
applyChunk(
|
|
76
|
+
applyChunk(r, e, a) {
|
|
74
77
|
switch (e.type) {
|
|
75
78
|
case "text-delta": {
|
|
76
|
-
const o =
|
|
77
|
-
o?.type === "text" && o.state === "streaming" ? o.text += e.text :
|
|
79
|
+
const o = r.parts[r.parts.length - 1];
|
|
80
|
+
o?.type === "text" && o.state === "streaming" ? o.text += e.text : r.parts.push({ type: "text", text: e.text, state: "streaming" });
|
|
78
81
|
break;
|
|
79
82
|
}
|
|
80
83
|
case "reasoning-delta": {
|
|
81
|
-
const o =
|
|
82
|
-
o?.type === "reasoning" && o.state === "streaming" ? o.text += e.text :
|
|
84
|
+
const o = r.parts[r.parts.length - 1];
|
|
85
|
+
o?.type === "reasoning" && o.state === "streaming" ? o.text += e.text : r.parts.push({ type: "reasoning", text: e.text, state: "streaming" });
|
|
83
86
|
break;
|
|
84
87
|
}
|
|
85
88
|
case "tool-call-start":
|
|
86
|
-
|
|
89
|
+
r.parts.push({
|
|
87
90
|
type: "tool-call",
|
|
88
91
|
toolCallId: e.toolCallId,
|
|
89
92
|
toolName: e.toolName,
|
|
@@ -99,61 +102,61 @@ class U {
|
|
|
99
102
|
break;
|
|
100
103
|
}
|
|
101
104
|
}
|
|
102
|
-
finalizeTextParts(
|
|
103
|
-
for (const e of
|
|
105
|
+
finalizeTextParts(r) {
|
|
106
|
+
for (const e of r.parts)
|
|
104
107
|
(e.type === "text" || e.type === "reasoning") && e.state === "streaming" && (e.state = "done");
|
|
105
108
|
}
|
|
106
|
-
findToolCallPart(
|
|
107
|
-
return
|
|
109
|
+
findToolCallPart(r, e) {
|
|
110
|
+
return r.parts.find(
|
|
108
111
|
(a) => a.type === "tool-call" && a.toolCallId === e
|
|
109
112
|
);
|
|
110
113
|
}
|
|
111
|
-
getToolName(
|
|
112
|
-
return this.findToolCallPart(
|
|
114
|
+
getToolName(r, e) {
|
|
115
|
+
return this.findToolCallPart(r, e)?.toolName ?? "";
|
|
113
116
|
}
|
|
114
117
|
triggerReactivity() {
|
|
115
118
|
this.messages.value = [...this.messages.value];
|
|
116
119
|
}
|
|
117
120
|
}
|
|
118
|
-
const
|
|
119
|
-
function P(
|
|
120
|
-
const
|
|
121
|
+
const J = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
122
|
+
function P(t = 16) {
|
|
123
|
+
const r = crypto.getRandomValues(new Uint8Array(t));
|
|
121
124
|
let e = "";
|
|
122
|
-
for (let a = 0; a <
|
|
123
|
-
e +=
|
|
125
|
+
for (let a = 0; a < t; a++)
|
|
126
|
+
e += J[r[a] % J.length];
|
|
124
127
|
return e;
|
|
125
128
|
}
|
|
126
|
-
function
|
|
127
|
-
const
|
|
129
|
+
function ee(t) {
|
|
130
|
+
const r = t.id ?? P(), e = x(t.initialMessages ?? []), a = x("ready"), o = x(void 0), s = x("");
|
|
128
131
|
let l = null;
|
|
129
|
-
const f = x(!
|
|
130
|
-
|
|
132
|
+
const f = x(!t.storage), i = t.storage, v = (n) => {
|
|
133
|
+
t.onError?.(n instanceof Error ? n : new Error(String(n)));
|
|
131
134
|
};
|
|
132
135
|
if (i) {
|
|
133
136
|
const n = e.value, C = (u) => {
|
|
134
137
|
u?.length && e.value === n && (e.value = u), f.value = !0;
|
|
135
138
|
};
|
|
136
139
|
try {
|
|
137
|
-
const u = i.load(
|
|
140
|
+
const u = i.load(r);
|
|
138
141
|
u instanceof Promise ? u.then(C, (y) => {
|
|
139
142
|
v(y), f.value = !0;
|
|
140
143
|
}) : C(u);
|
|
141
144
|
} catch (u) {
|
|
142
145
|
v(u), f.value = !0;
|
|
143
146
|
}
|
|
144
|
-
|
|
147
|
+
U([e, a, f], () => {
|
|
145
148
|
if (!(!f.value || a.value === "submitted" || a.value === "streaming"))
|
|
146
149
|
try {
|
|
147
|
-
const u = e.value.length > 0 ? i.save(
|
|
150
|
+
const u = e.value.length > 0 ? i.save(r, e.value) : i.remove(r);
|
|
148
151
|
u instanceof Promise && u.catch(v);
|
|
149
152
|
} catch (u) {
|
|
150
153
|
v(u);
|
|
151
154
|
}
|
|
152
155
|
}, { deep: !0 });
|
|
153
156
|
}
|
|
154
|
-
const d = new
|
|
157
|
+
const d = new k(e, a, P), c = () => {
|
|
155
158
|
const n = [], C = [
|
|
156
|
-
N(
|
|
159
|
+
N(t.system),
|
|
157
160
|
...e.value.filter((u) => u.role === "system").map((u) => u.parts.filter((y) => y.type === "text").map((y) => y.text).join(""))
|
|
158
161
|
].filter(Boolean);
|
|
159
162
|
C.length > 0 && n.push({ role: "system", content: C.join(`
|
|
@@ -168,13 +171,13 @@ function Q(r) {
|
|
|
168
171
|
n.push({ role: "user", content: E });
|
|
169
172
|
continue;
|
|
170
173
|
}
|
|
171
|
-
const
|
|
174
|
+
const j = w.map((O) => ({
|
|
172
175
|
type: "file",
|
|
173
176
|
url: O.url,
|
|
174
177
|
mediaType: O.mediaType,
|
|
175
178
|
filename: O.filename
|
|
176
179
|
}));
|
|
177
|
-
E &&
|
|
180
|
+
E && j.push({ type: "text", text: E }), n.push({ role: "user", content: j });
|
|
178
181
|
} else if (u.role === "assistant") {
|
|
179
182
|
n.push({
|
|
180
183
|
role: "assistant",
|
|
@@ -191,24 +194,24 @@ function Q(r) {
|
|
|
191
194
|
}
|
|
192
195
|
return n;
|
|
193
196
|
}, p = async (n) => {
|
|
194
|
-
const C =
|
|
197
|
+
const C = t.maxToolRoundtrips ?? 5;
|
|
195
198
|
let u = 0;
|
|
196
199
|
for (; u < C; ) {
|
|
197
200
|
const y = c();
|
|
198
|
-
|
|
199
|
-
const { stream: E } = await
|
|
200
|
-
model:
|
|
201
|
+
t.provider.validateConfig();
|
|
202
|
+
const { stream: E } = await t.provider.chat({
|
|
203
|
+
model: t.model,
|
|
201
204
|
messages: y,
|
|
202
|
-
tools:
|
|
205
|
+
tools: t.tools,
|
|
203
206
|
signal: n,
|
|
204
207
|
providerOptions: {
|
|
205
|
-
...
|
|
206
|
-
...N(
|
|
208
|
+
...t.providerOptions,
|
|
209
|
+
...N(t.body)
|
|
207
210
|
}
|
|
208
211
|
}), S = await d.processStream(E, n, {
|
|
209
|
-
onFinish:
|
|
210
|
-
onError:
|
|
211
|
-
onToolCall:
|
|
212
|
+
onFinish: t.onFinish,
|
|
213
|
+
onError: t.onError,
|
|
214
|
+
onToolCall: t.onToolCall
|
|
212
215
|
});
|
|
213
216
|
if (S.finishReason !== "tool-calls" || S.toolCalls.length === 0)
|
|
214
217
|
break;
|
|
@@ -218,7 +221,7 @@ function Q(r) {
|
|
|
218
221
|
const C = typeof n == "string" ? n : n?.text ?? s.value;
|
|
219
222
|
let u;
|
|
220
223
|
try {
|
|
221
|
-
u = typeof n == "object" ? await
|
|
224
|
+
u = typeof n == "object" ? await _(n.files) : [];
|
|
222
225
|
} catch (S) {
|
|
223
226
|
o.value = S instanceof Error ? S : new Error(String(S)), a.value = "error";
|
|
224
227
|
return;
|
|
@@ -273,13 +276,13 @@ function Q(r) {
|
|
|
273
276
|
}, M = () => {
|
|
274
277
|
o.value = void 0, a.value === "error" && (a.value = "ready");
|
|
275
278
|
};
|
|
276
|
-
return L(() => {
|
|
279
|
+
return L() && D(() => {
|
|
277
280
|
l?.abort();
|
|
278
281
|
}), {
|
|
279
282
|
messages: e,
|
|
280
283
|
status: A(() => a.value),
|
|
281
284
|
error: o,
|
|
282
|
-
id:
|
|
285
|
+
id: r,
|
|
283
286
|
input: s,
|
|
284
287
|
sendMessage: m,
|
|
285
288
|
regenerate: h,
|
|
@@ -292,20 +295,20 @@ function Q(r) {
|
|
|
292
295
|
isRestored: A(() => f.value)
|
|
293
296
|
};
|
|
294
297
|
}
|
|
295
|
-
function
|
|
296
|
-
const
|
|
298
|
+
function te(t) {
|
|
299
|
+
const r = t.id ?? P(), e = x(""), a = x("ready"), o = x(void 0), s = x("");
|
|
297
300
|
let l = null;
|
|
298
301
|
const f = async (p) => {
|
|
299
302
|
const m = p ?? s.value;
|
|
300
303
|
if (!m.trim()) return e.value;
|
|
301
304
|
p || (s.value = ""), e.value = "", o.value = void 0, a.value = "submitted", l?.abort(), l = new AbortController();
|
|
302
305
|
try {
|
|
303
|
-
|
|
304
|
-
const h = N(
|
|
306
|
+
t.provider.validateConfig();
|
|
307
|
+
const h = N(t.system), g = h ? [
|
|
305
308
|
{ role: "system", content: h },
|
|
306
309
|
{ role: "user", content: m }
|
|
307
|
-
] : [{ role: "user", content: m }], { stream: b } = await
|
|
308
|
-
model:
|
|
310
|
+
] : [{ role: "user", content: m }], { stream: b } = await t.provider.chat({
|
|
311
|
+
model: t.model,
|
|
309
312
|
messages: g,
|
|
310
313
|
signal: l.signal
|
|
311
314
|
});
|
|
@@ -322,12 +325,12 @@ function X(r) {
|
|
|
322
325
|
else if (n.type === "error")
|
|
323
326
|
throw n.error;
|
|
324
327
|
}
|
|
325
|
-
return R.releaseLock(), a.value = "ready",
|
|
328
|
+
return R.releaseLock(), a.value = "ready", t.onFinish?.(e.value, { finishReason: T, usage: I }), e.value;
|
|
326
329
|
} catch (h) {
|
|
327
330
|
if (!l || l.signal.aborted)
|
|
328
331
|
return a.value = "ready", e.value;
|
|
329
332
|
const g = h instanceof Error ? h : new Error(String(h));
|
|
330
|
-
return o.value = g, a.value = "error",
|
|
333
|
+
return o.value = g, a.value = "error", t.onError?.(g), e.value;
|
|
331
334
|
}
|
|
332
335
|
}, i = () => {
|
|
333
336
|
l?.abort(), l = null, a.value = "ready";
|
|
@@ -336,14 +339,14 @@ function X(r) {
|
|
|
336
339
|
}, d = (p) => {
|
|
337
340
|
p?.preventDefault?.(), s.value.trim() && f();
|
|
338
341
|
}, c = A(() => a.value === "submitted" || a.value === "streaming");
|
|
339
|
-
return L(() => {
|
|
342
|
+
return L() && D(() => {
|
|
340
343
|
l?.abort();
|
|
341
344
|
}), {
|
|
342
345
|
completion: e,
|
|
343
346
|
status: A(() => a.value),
|
|
344
347
|
error: o,
|
|
345
348
|
isLoading: c,
|
|
346
|
-
id:
|
|
349
|
+
id: r,
|
|
347
350
|
input: s,
|
|
348
351
|
complete: f,
|
|
349
352
|
stop: i,
|
|
@@ -351,24 +354,24 @@ function X(r) {
|
|
|
351
354
|
handleSubmit: d
|
|
352
355
|
};
|
|
353
356
|
}
|
|
354
|
-
function
|
|
355
|
-
const
|
|
357
|
+
function re(t) {
|
|
358
|
+
const r = t.id ?? P(), e = x(void 0), a = x(void 0), o = x("ready"), s = x(void 0), l = x("");
|
|
356
359
|
let f = null;
|
|
357
360
|
const i = async (p) => {
|
|
358
361
|
const m = p ?? l.value;
|
|
359
362
|
if (!m.trim()) return e.value;
|
|
360
363
|
p || (l.value = ""), e.value = void 0, a.value = void 0, s.value = void 0, o.value = "submitted", f?.abort(), f = new AbortController();
|
|
361
364
|
try {
|
|
362
|
-
|
|
365
|
+
t.provider.validateConfig();
|
|
363
366
|
const h = [
|
|
364
|
-
N(
|
|
367
|
+
N(t.system) ?? "",
|
|
365
368
|
"Respond with valid JSON matching this schema:",
|
|
366
|
-
JSON.stringify(
|
|
369
|
+
JSON.stringify(t.schema, null, 2),
|
|
367
370
|
"Output ONLY the JSON object, no other text."
|
|
368
371
|
].filter(Boolean).join(`
|
|
369
372
|
|
|
370
|
-
`), { stream: g } = await
|
|
371
|
-
model:
|
|
373
|
+
`), { stream: g } = await t.provider.chat({
|
|
374
|
+
model: t.model,
|
|
372
375
|
messages: [
|
|
373
376
|
{ role: "system", content: h },
|
|
374
377
|
{ role: "user", content: m }
|
|
@@ -387,7 +390,7 @@ function ee(r) {
|
|
|
387
390
|
if (n.type === "text-delta") {
|
|
388
391
|
R += n.text;
|
|
389
392
|
try {
|
|
390
|
-
a.value =
|
|
393
|
+
a.value = W(R);
|
|
391
394
|
} catch {
|
|
392
395
|
}
|
|
393
396
|
} else if (n.type === "finish")
|
|
@@ -401,12 +404,12 @@ function ee(r) {
|
|
|
401
404
|
} catch {
|
|
402
405
|
throw new Error(`Failed to parse JSON response: ${R.slice(0, 200)}`);
|
|
403
406
|
}
|
|
404
|
-
return o.value = "ready",
|
|
407
|
+
return o.value = "ready", t.onFinish?.(e.value, { finishReason: T, usage: I }), e.value;
|
|
405
408
|
} catch (h) {
|
|
406
409
|
if (f.signal.aborted)
|
|
407
410
|
return o.value = "ready", e.value;
|
|
408
411
|
const g = h instanceof Error ? h : new Error(String(h));
|
|
409
|
-
s.value = g, o.value = "error",
|
|
412
|
+
s.value = g, o.value = "error", t.onError?.(g);
|
|
410
413
|
return;
|
|
411
414
|
}
|
|
412
415
|
}, v = () => {
|
|
@@ -414,7 +417,7 @@ function ee(r) {
|
|
|
414
417
|
}, d = () => {
|
|
415
418
|
s.value = void 0, o.value === "error" && (o.value = "ready");
|
|
416
419
|
}, c = A(() => o.value === "submitted" || o.value === "streaming");
|
|
417
|
-
return L(() => {
|
|
420
|
+
return L() && D(() => {
|
|
418
421
|
f?.abort();
|
|
419
422
|
}), {
|
|
420
423
|
object: e,
|
|
@@ -422,19 +425,19 @@ function ee(r) {
|
|
|
422
425
|
status: A(() => o.value),
|
|
423
426
|
error: s,
|
|
424
427
|
isLoading: c,
|
|
425
|
-
id:
|
|
428
|
+
id: r,
|
|
426
429
|
input: l,
|
|
427
430
|
submit: i,
|
|
428
431
|
stop: v,
|
|
429
432
|
clearError: d
|
|
430
433
|
};
|
|
431
434
|
}
|
|
432
|
-
function
|
|
433
|
-
let
|
|
434
|
-
if (!
|
|
435
|
+
function W(t) {
|
|
436
|
+
let r = t.trim();
|
|
437
|
+
if (!r.startsWith("{")) return;
|
|
435
438
|
let e = 0, a = !1, o = !1;
|
|
436
|
-
for (let s = 0; s <
|
|
437
|
-
const l =
|
|
439
|
+
for (let s = 0; s < r.length; s++) {
|
|
440
|
+
const l = r[s];
|
|
438
441
|
if (o) {
|
|
439
442
|
o = !1;
|
|
440
443
|
continue;
|
|
@@ -450,31 +453,31 @@ function _(r) {
|
|
|
450
453
|
a || (l === "{" && e++, l === "}" && e--);
|
|
451
454
|
}
|
|
452
455
|
for (; e > 0; )
|
|
453
|
-
|
|
456
|
+
r += "}", e--;
|
|
454
457
|
try {
|
|
455
|
-
return JSON.parse(
|
|
458
|
+
return JSON.parse(r);
|
|
456
459
|
} catch {
|
|
457
460
|
return;
|
|
458
461
|
}
|
|
459
462
|
}
|
|
460
|
-
const
|
|
461
|
-
function
|
|
463
|
+
const $ = /(\\)?\{\{\s*([A-Za-z_][\w-]*)\s*\}\}/g;
|
|
464
|
+
function H(t, r = {}) {
|
|
462
465
|
return {
|
|
463
|
-
template:
|
|
464
|
-
variables:
|
|
466
|
+
template: t,
|
|
467
|
+
variables: B(t),
|
|
465
468
|
render(e) {
|
|
466
|
-
return
|
|
469
|
+
return z(t, { ...r.defaults, ...e });
|
|
467
470
|
}
|
|
468
471
|
};
|
|
469
472
|
}
|
|
470
|
-
function
|
|
473
|
+
function ae(t, r = {}) {
|
|
471
474
|
const e = (a) => {
|
|
472
|
-
const o = { ...
|
|
473
|
-
return
|
|
475
|
+
const o = { ...r.defaults, ...a };
|
|
476
|
+
return t.map((s) => ({ role: s.role, content: z(s.content, o) }));
|
|
474
477
|
};
|
|
475
478
|
return {
|
|
476
|
-
messages:
|
|
477
|
-
variables: [...new Set(
|
|
479
|
+
messages: t,
|
|
480
|
+
variables: [...new Set(t.flatMap((a) => B(a.content)))],
|
|
478
481
|
render: e,
|
|
479
482
|
toUIMessages(a) {
|
|
480
483
|
return e(a).map((o) => ({
|
|
@@ -486,83 +489,83 @@ function te(r, t = {}) {
|
|
|
486
489
|
}
|
|
487
490
|
};
|
|
488
491
|
}
|
|
489
|
-
function
|
|
490
|
-
return
|
|
492
|
+
function z(t, r) {
|
|
493
|
+
return t.replace($, (e, a, o) => {
|
|
491
494
|
if (a) return e.slice(1);
|
|
492
|
-
const s =
|
|
495
|
+
const s = r[o];
|
|
493
496
|
if (s == null)
|
|
494
497
|
throw new Error(`[vue-llm] Missing prompt variable "${o}"`);
|
|
495
498
|
return String(s);
|
|
496
499
|
});
|
|
497
500
|
}
|
|
498
|
-
function
|
|
499
|
-
const
|
|
500
|
-
for (const [, e, a] of
|
|
501
|
-
e ||
|
|
502
|
-
return [...
|
|
501
|
+
function B(t) {
|
|
502
|
+
const r = /* @__PURE__ */ new Set();
|
|
503
|
+
for (const [, e, a] of t.matchAll($))
|
|
504
|
+
e || r.add(a);
|
|
505
|
+
return [...r];
|
|
503
506
|
}
|
|
504
|
-
function
|
|
505
|
-
const e = typeof
|
|
506
|
-
return A(() => e.render(N(
|
|
507
|
+
function oe(t, r) {
|
|
508
|
+
const e = typeof t == "string" ? H(t) : t;
|
|
509
|
+
return A(() => e.render(N(r)));
|
|
507
510
|
}
|
|
508
|
-
function
|
|
509
|
-
return
|
|
510
|
-
id:
|
|
511
|
-
supportsStreaming:
|
|
511
|
+
function ne(t, r) {
|
|
512
|
+
return r.length ? {
|
|
513
|
+
id: t.id,
|
|
514
|
+
supportsStreaming: t.supportsStreaming,
|
|
512
515
|
validateConfig() {
|
|
513
|
-
|
|
516
|
+
t.validateConfig();
|
|
514
517
|
},
|
|
515
518
|
async chat(e) {
|
|
516
519
|
let a = e;
|
|
517
|
-
for (const i of
|
|
518
|
-
i.beforeRequest && (a = await i.beforeRequest(a,
|
|
519
|
-
const o =
|
|
520
|
+
for (const i of r)
|
|
521
|
+
i.beforeRequest && (a = await i.beforeRequest(a, t));
|
|
522
|
+
const o = r.reduceRight(
|
|
520
523
|
(i, v) => v.wrapChat ? (d) => v.wrapChat(i, d) : i,
|
|
521
|
-
(i) =>
|
|
524
|
+
(i) => t.chat(i)
|
|
522
525
|
);
|
|
523
526
|
let s;
|
|
524
527
|
try {
|
|
525
528
|
s = await o(a);
|
|
526
529
|
} catch (i) {
|
|
527
530
|
let v = i instanceof Error ? i : new Error(String(i));
|
|
528
|
-
for (const d of
|
|
531
|
+
for (const d of r)
|
|
529
532
|
if (d.onError) {
|
|
530
533
|
const c = await d.onError(v, a);
|
|
531
534
|
c instanceof Error && (v = c);
|
|
532
535
|
}
|
|
533
536
|
throw v;
|
|
534
537
|
}
|
|
535
|
-
const f =
|
|
538
|
+
const f = r.some((i) => i.afterStream) ? s.stream.pipeThrough(new TransformStream({
|
|
536
539
|
async transform(i, v) {
|
|
537
540
|
let d = i;
|
|
538
|
-
for (const c of
|
|
541
|
+
for (const c of r) {
|
|
539
542
|
if (!d) break;
|
|
540
543
|
c.afterStream && (d = await c.afterStream(d, a));
|
|
541
544
|
}
|
|
542
545
|
d && v.enqueue(d);
|
|
543
546
|
}
|
|
544
547
|
})) : s.stream;
|
|
545
|
-
for (const i of
|
|
548
|
+
for (const i of r)
|
|
546
549
|
i.onFinish && i.onFinish(a, s);
|
|
547
550
|
return { stream: f, response: s.response };
|
|
548
551
|
}
|
|
549
|
-
} :
|
|
552
|
+
} : t;
|
|
550
553
|
}
|
|
551
|
-
function
|
|
554
|
+
function se(t = "vue-llm") {
|
|
552
555
|
return {
|
|
553
|
-
beforeRequest(
|
|
554
|
-
return console.log(`[${
|
|
556
|
+
beforeRequest(r) {
|
|
557
|
+
return console.log(`[${t}] → ${r.model}`, r.messages.length, "messages"), r;
|
|
555
558
|
},
|
|
556
|
-
afterStream(
|
|
557
|
-
return
|
|
559
|
+
afterStream(r) {
|
|
560
|
+
return r.type === "finish" && console.log(`[${t}] ← finish:`, r.finishReason, r.usage ?? ""), r;
|
|
558
561
|
},
|
|
559
|
-
onError(
|
|
560
|
-
console.error(`[${
|
|
562
|
+
onError(r) {
|
|
563
|
+
console.error(`[${t}] error:`, r.message);
|
|
561
564
|
}
|
|
562
565
|
};
|
|
563
566
|
}
|
|
564
|
-
function
|
|
565
|
-
const e = typeof
|
|
567
|
+
function le(t = {}, r) {
|
|
568
|
+
const e = typeof t == "number" ? { maxRetries: t, initialDelay: r } : t, a = e.maxRetries ?? 3, o = e.initialDelay ?? 1e3, s = e.maxDelay ?? 3e4, l = e.factor ?? 2, f = e.jitter ?? !0, i = e.shouldRetry ?? Y;
|
|
566
569
|
return {
|
|
567
570
|
async wrapChat(v, d) {
|
|
568
571
|
for (let c = 1; ; c++)
|
|
@@ -572,20 +575,20 @@ function ne(r = {}, t) {
|
|
|
572
575
|
const m = p instanceof Error ? p : new Error(String(p));
|
|
573
576
|
if (c > a || d.signal?.aborted || !i(m, c))
|
|
574
577
|
throw m;
|
|
575
|
-
const h = o * l ** (c - 1), g = m instanceof
|
|
578
|
+
const h = o * l ** (c - 1), g = m instanceof F ? m.retryAfter : void 0, b = Math.min(
|
|
576
579
|
s,
|
|
577
580
|
g ?? (f ? h * (0.5 + Math.random() * 0.5) : h)
|
|
578
581
|
);
|
|
579
|
-
e.onRetry?.({ error: m, attempt: c, delay: b }), await
|
|
582
|
+
e.onRetry?.({ error: m, attempt: c, delay: b }), await G(b, d.signal);
|
|
580
583
|
}
|
|
581
584
|
}
|
|
582
585
|
};
|
|
583
586
|
}
|
|
584
|
-
function
|
|
585
|
-
return
|
|
587
|
+
function Y(t) {
|
|
588
|
+
return t.name === "AbortError" ? !1 : t instanceof F ? t.isRetryable : t instanceof TypeError;
|
|
586
589
|
}
|
|
587
|
-
function
|
|
588
|
-
const
|
|
590
|
+
function ie(t) {
|
|
591
|
+
const r = t.interval ?? 6e4, e = [];
|
|
589
592
|
let a = 0;
|
|
590
593
|
const o = /* @__PURE__ */ new Set(), s = () => {
|
|
591
594
|
a--;
|
|
@@ -594,8 +597,8 @@ function se(r) {
|
|
|
594
597
|
for (; ; ) {
|
|
595
598
|
f?.throwIfAborted();
|
|
596
599
|
const i = Date.now();
|
|
597
|
-
for (; e.length > 0 && e[0] <= i -
|
|
598
|
-
const v =
|
|
600
|
+
for (; e.length > 0 && e[0] <= i - r; ) e.shift();
|
|
601
|
+
const v = t.maxRequests !== void 0 && e.length >= t.maxRequests, d = t.maxConcurrent !== void 0 && a >= t.maxConcurrent;
|
|
599
602
|
if (!v && !d) {
|
|
600
603
|
e.push(i), a++;
|
|
601
604
|
return;
|
|
@@ -605,7 +608,7 @@ function se(r) {
|
|
|
605
608
|
clearTimeout(g), o.delete(m), f?.removeEventListener("abort", h), c();
|
|
606
609
|
}, h = () => {
|
|
607
610
|
m(), p(f.reason);
|
|
608
|
-
}, g = v ? setTimeout(m, e[0] +
|
|
611
|
+
}, g = v ? setTimeout(m, e[0] + r - i) : void 0;
|
|
609
612
|
o.add(m), f?.addEventListener("abort", h, { once: !0 });
|
|
610
613
|
});
|
|
611
614
|
}
|
|
@@ -619,15 +622,15 @@ function se(r) {
|
|
|
619
622
|
} catch (d) {
|
|
620
623
|
throw s(), d;
|
|
621
624
|
}
|
|
622
|
-
return { ...v, stream:
|
|
625
|
+
return { ...v, stream: Z(v.stream, s) };
|
|
623
626
|
}
|
|
624
627
|
};
|
|
625
628
|
}
|
|
626
|
-
function
|
|
627
|
-
const e =
|
|
629
|
+
function Z(t, r) {
|
|
630
|
+
const e = t.getReader();
|
|
628
631
|
let a = !1;
|
|
629
632
|
const o = () => {
|
|
630
|
-
a || (a = !0,
|
|
633
|
+
a || (a = !0, r());
|
|
631
634
|
};
|
|
632
635
|
return new ReadableStream({
|
|
633
636
|
async pull(s) {
|
|
@@ -643,89 +646,89 @@ function H(r, t) {
|
|
|
643
646
|
}
|
|
644
647
|
});
|
|
645
648
|
}
|
|
646
|
-
function
|
|
649
|
+
function G(t, r) {
|
|
647
650
|
return new Promise((e, a) => {
|
|
648
|
-
if (
|
|
651
|
+
if (r?.aborted) return a(r.reason);
|
|
649
652
|
const o = setTimeout(() => {
|
|
650
|
-
|
|
651
|
-
},
|
|
652
|
-
clearTimeout(o), a(
|
|
653
|
+
r?.removeEventListener("abort", s), e();
|
|
654
|
+
}, t), s = () => {
|
|
655
|
+
clearTimeout(o), a(r.reason);
|
|
653
656
|
};
|
|
654
|
-
|
|
657
|
+
r?.addEventListener("abort", s, { once: !0 });
|
|
655
658
|
});
|
|
656
659
|
}
|
|
657
|
-
function
|
|
660
|
+
function ce(t) {
|
|
658
661
|
return {
|
|
659
|
-
beforeRequest(
|
|
662
|
+
beforeRequest(r) {
|
|
660
663
|
return {
|
|
661
|
-
...
|
|
664
|
+
...r,
|
|
662
665
|
providerOptions: {
|
|
663
|
-
...
|
|
664
|
-
headers: { ...
|
|
666
|
+
...r.providerOptions,
|
|
667
|
+
headers: { ...r.providerOptions?.headers, ...t }
|
|
665
668
|
}
|
|
666
669
|
};
|
|
667
670
|
}
|
|
668
671
|
};
|
|
669
672
|
}
|
|
670
|
-
function
|
|
671
|
-
const
|
|
673
|
+
function ue(t = {}) {
|
|
674
|
+
const r = t.prefix ?? "vue-llm:chat:", e = () => t.storage ? t.storage : typeof localStorage < "u" ? localStorage : void 0;
|
|
672
675
|
return {
|
|
673
676
|
load(a) {
|
|
674
|
-
const o = e()?.getItem(
|
|
677
|
+
const o = e()?.getItem(r + a);
|
|
675
678
|
if (!o) return null;
|
|
676
679
|
try {
|
|
677
|
-
return
|
|
680
|
+
return V(o);
|
|
678
681
|
} catch {
|
|
679
682
|
return null;
|
|
680
683
|
}
|
|
681
684
|
},
|
|
682
685
|
save(a, o) {
|
|
683
|
-
e()?.setItem(
|
|
686
|
+
e()?.setItem(r + a, JSON.stringify(o));
|
|
684
687
|
},
|
|
685
688
|
remove(a) {
|
|
686
|
-
e()?.removeItem(
|
|
689
|
+
e()?.removeItem(r + a);
|
|
687
690
|
}
|
|
688
691
|
};
|
|
689
692
|
}
|
|
690
|
-
function
|
|
691
|
-
const
|
|
693
|
+
function fe() {
|
|
694
|
+
const t = /* @__PURE__ */ new Map();
|
|
692
695
|
return {
|
|
693
|
-
load(
|
|
694
|
-
const e =
|
|
695
|
-
return e ?
|
|
696
|
+
load(r) {
|
|
697
|
+
const e = t.get(r);
|
|
698
|
+
return e ? V(e) : null;
|
|
696
699
|
},
|
|
697
|
-
save(
|
|
698
|
-
|
|
700
|
+
save(r, e) {
|
|
701
|
+
t.set(r, JSON.stringify(e));
|
|
699
702
|
},
|
|
700
|
-
remove(
|
|
701
|
-
|
|
703
|
+
remove(r) {
|
|
704
|
+
t.delete(r);
|
|
702
705
|
}
|
|
703
706
|
};
|
|
704
707
|
}
|
|
705
|
-
function
|
|
706
|
-
const
|
|
707
|
-
return Array.isArray(
|
|
708
|
+
function V(t) {
|
|
709
|
+
const r = JSON.parse(t);
|
|
710
|
+
return Array.isArray(r) ? r.map((e) => ({
|
|
708
711
|
...e,
|
|
709
712
|
createdAt: e.createdAt ? new Date(e.createdAt) : void 0
|
|
710
713
|
})) : [];
|
|
711
714
|
}
|
|
712
715
|
export {
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
716
|
+
F as LLMAPIError,
|
|
717
|
+
_ as convertFilesToParts,
|
|
718
|
+
ae as createChatPrompt,
|
|
719
|
+
fe as createMemoryStorage,
|
|
720
|
+
H as createPrompt,
|
|
721
|
+
ue as createWebStorage,
|
|
722
|
+
me as fileToDataURL,
|
|
723
|
+
ce as headerMiddleware,
|
|
724
|
+
Y as isRetryableError,
|
|
725
|
+
se as loggingMiddleware,
|
|
726
|
+
ie as rateLimitMiddleware,
|
|
727
|
+
z as renderTemplate,
|
|
728
|
+
le as retryMiddleware,
|
|
729
|
+
ee as useChat,
|
|
730
|
+
te as useCompletion,
|
|
731
|
+
re as useObject,
|
|
732
|
+
oe as usePrompt,
|
|
733
|
+
ne as wrapProvider
|
|
731
734
|
};
|
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
|
}
|