@easbot/local-model-sdk 0.3.0 → 0.3.3
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/index.cjs +4 -4
- package/dist/index.d.cts +0 -2
- package/dist/index.d.ts +0 -2
- package/dist/index.mjs +3 -3
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
'use strict';var H=require('os'),A=require('path'),b=require('fs'),utils=require('@easbot/utils'),ai=require('ai');function _interopNamespace(e){if(e&&e.__esModule)return e;var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var H__namespace=/*#__PURE__*/_interopNamespace(H);var A__namespace=/*#__PURE__*/_interopNamespace(A);var b__namespace=/*#__PURE__*/_interopNamespace(b);var ee=Object.defineProperty;var te=(f,e,t)=>e in f?ee(f,e,{enumerable:true,configurable:true,writable:true,value:t}):f[e]=t;var c=(f,e,t)=>te(f,typeof e!="symbol"?e+"":e,t);var M=(d=>(d.MODEL_NOT_FOUND="MODEL_NOT_FOUND",d.MODEL_LOAD_ERROR="MODEL_LOAD_ERROR",d.MODEL_NOT_INITIALIZED="MODEL_NOT_INITIALIZED",d.GENERATION_ERROR="GENERATION_ERROR",d.EMBEDDING_ERROR="EMBEDDING_ERROR",d.INVALID_INPUT="INVALID_INPUT",d.UNSUPPORTED_FEATURE="UNSUPPORTED_FEATURE",d.CACHE_ERROR="CACHE_ERROR",d.PROCESSING_ERROR="PROCESSING_ERROR",d.ABORTED="ABORTED",d.OUT_OF_MEMORY="OUT_OF_MEMORY",d.UNKNOWN_ERROR="UNKNOWN_ERROR",d))(M||{});function B(f,e,t){let n;return e?n="error":f?n="stop":n="other",{unified:n,raw:t}}var L={"qwen3-0.6b":{modelId:"qwen3-0.6b",huggingFaceId:"onnx-community/Qwen3-0.6B-ONNX",contextLength:32768,device:"cpu",dtype:"q4f16",defaultSettings:{temperature:0,topP:.9,topK:40,maxTokens:32e3,repetitionPenalty:1.1,stopSequences:[]}},"qwen3.5-0.8b":{modelId:"qwen3.5-0.8b",huggingFaceId:"onnx-community/Qwen3.5-0.8B-ONNX",contextLength:32768,dtype:"q4",defaultSettings:{temperature:.7,topP:.9,topK:40,maxTokens:32e3,repetitionPenalty:1.1,stopSequences:[]}},"gemma-3-1b-it":{modelId:"gemma-3-1b-it",huggingFaceId:"onnx-community/gemma-3-1b-it-ONNX",contextLength:32768,dtype:"q4",defaultSettings:{temperature:.7,topP:.9,topK:40,maxTokens:32e3,repetitionPenalty:1.1,stopSequences:[]}}},P={"bge-small-zh-v1.5":{modelId:"bge-small-zh-v1.5",huggingFaceId:"Xenova/bge-small-zh-v1.5",dimensions:512,maxEmbeddingsPerCall:32},"bge-base-zh-v1.5":{modelId:"bge-base-zh-v1.5",huggingFaceId:"Xenova/bge-base-zh-v1.5",dimensions:768,maxEmbeddingsPerCall:32,dtype:"q4"},"bge-large-zh-v1.5":{modelId:"bge-large-zh-v1.5",huggingFaceId:"Xenova/bge-large-zh-v1.5",dimensions:1024,maxEmbeddingsPerCall:32,dtype:"q4"},"all-MiniLM-L6-v2":{modelId:"all-MiniLM-L6-v2",huggingFaceId:"sentence-transformers/all-MiniLM-L6-v2",dimensions:384,maxEmbeddingsPerCall:32,dtype:"q4"}};var s=class f extends Error{constructor(t,n,r){super(t);c(this,"code",n);c(this,"cause",r);this.name="LocalModelError",Error.captureStackTrace&&Error.captureStackTrace(this,f);}},T=class{static modelLoadError(e){return new s(`\u6A21\u578B\u52A0\u8F7D\u5931\u8D25: ${e.message}`,"MODEL_LOAD_ERROR",e)}static generationError(e){return new s(`\u751F\u6210\u5931\u8D25: ${e.message}`,"GENERATION_ERROR",e)}static embeddingError(e){return new s(`\u5D4C\u5165\u5931\u8D25: ${e.message}`,"EMBEDDING_ERROR",e)}static validationError(e){return new s(e,"INVALID_INPUT")}static abortError(){return new s("\u64CD\u4F5C\u5DF2\u4E2D\u6B62","ABORTED")}static unsupportedFeature(e){return new s(`\u4E0D\u652F\u6301\u7684\u529F\u80FD: ${e}`,"UNSUPPORTED_FEATURE")}};var ne=A__namespace.join(H__namespace.homedir(),".cache","easbot","models"),D=class{constructor(e){c(this,"cacheDir");this.cacheDir=e.cacheDir||ne;}getCachePath(e){return A__namespace.join(this.cacheDir,e)}isCached(e){try{let t=this.getCachePath(e);return b__namespace.existsSync(t)&&b__namespace.statSync(t).isDirectory()}catch{return false}}async clear(e){try{if(e){let t=this.getCachePath(e);b__namespace.existsSync(t)&&await this.removeDirectory(t);}else b__namespace.existsSync(this.cacheDir)&&await this.removeDirectory(this.cacheDir);}catch(t){throw new s(`\u6E05\u7406\u7F13\u5B58\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`,"CACHE_ERROR",t instanceof Error?t:void 0)}}async getCacheSize(e){try{let t=e?this.getCachePath(e):this.cacheDir;return b__namespace.existsSync(t)?await this.getDirectorySize(t):0}catch(t){throw new s(`\u83B7\u53D6\u7F13\u5B58\u5927\u5C0F\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`,"CACHE_ERROR",t instanceof Error?t:void 0)}}async getDirectorySize(e){let t=0,n=b__namespace.readdirSync(e);for(let r of n){let o=A__namespace.join(e,r),a=b__namespace.statSync(o);a.isDirectory()?t+=await this.getDirectorySize(o):t+=a.size;}return t}async removeDirectory(e){if(!b__namespace.existsSync(e))return;let t=b__namespace.readdirSync(e);for(let n of t){let r=A__namespace.join(e,n);b__namespace.statSync(r).isDirectory()?await this.removeDirectory(r):b__namespace.unlinkSync(r);}b__namespace.rmdirSync(e);}ensureCacheDir(){try{b__namespace.existsSync(this.cacheDir)||b__namespace.mkdirSync(this.cacheDir,{recursive:!0});}catch(e){throw new s(`\u521B\u5EFA\u7F13\u5B58\u76EE\u5F55\u5931\u8D25: ${e instanceof Error?e.message:String(e)}`,"CACHE_ERROR",e instanceof Error?e:void 0)}}};var I=null;async function re(){if(!I){I=(await import('@huggingface/transformers')).env;let e=A__namespace.join(H__namespace.homedir(),".cache","easbot","models");process.env.HF_HOME||(process.env.HF_HOME=e),I.allowLocalModels=true,I.allowRemoteModels=true,I.useBrowserCache=false,I.useFSCache=true;}return I}var j=null;async function J(){return j||(j=(await import('@huggingface/transformers')).pipeline),j}var y=utils.Log.create({service:"transformers-engine"});var
|
|
1
|
+
'use strict';var H=require('os'),A=require('path'),b=require('fs'),utils=require('@easbot/utils'),ai=require('ai');function _interopNamespace(e){if(e&&e.__esModule)return e;var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var H__namespace=/*#__PURE__*/_interopNamespace(H);var A__namespace=/*#__PURE__*/_interopNamespace(A);var b__namespace=/*#__PURE__*/_interopNamespace(b);var ee=Object.defineProperty;var te=(f,e,t)=>e in f?ee(f,e,{enumerable:true,configurable:true,writable:true,value:t}):f[e]=t;var c=(f,e,t)=>te(f,typeof e!="symbol"?e+"":e,t);var M=(d=>(d.MODEL_NOT_FOUND="MODEL_NOT_FOUND",d.MODEL_LOAD_ERROR="MODEL_LOAD_ERROR",d.MODEL_NOT_INITIALIZED="MODEL_NOT_INITIALIZED",d.GENERATION_ERROR="GENERATION_ERROR",d.EMBEDDING_ERROR="EMBEDDING_ERROR",d.INVALID_INPUT="INVALID_INPUT",d.UNSUPPORTED_FEATURE="UNSUPPORTED_FEATURE",d.CACHE_ERROR="CACHE_ERROR",d.PROCESSING_ERROR="PROCESSING_ERROR",d.ABORTED="ABORTED",d.OUT_OF_MEMORY="OUT_OF_MEMORY",d.UNKNOWN_ERROR="UNKNOWN_ERROR",d))(M||{});function B(f,e,t){let n;return e?n="error":f?n="stop":n="other",{unified:n,raw:t}}var L={"qwen3-0.6b":{modelId:"qwen3-0.6b",huggingFaceId:"onnx-community/Qwen3-0.6B-ONNX",contextLength:32768,device:"cpu",dtype:"q4f16",defaultSettings:{temperature:0,topP:.9,topK:40,maxTokens:32e3,repetitionPenalty:1.1,stopSequences:[]}},"qwen3.5-0.8b":{modelId:"qwen3.5-0.8b",huggingFaceId:"onnx-community/Qwen3.5-0.8B-ONNX",contextLength:32768,dtype:"q4",defaultSettings:{temperature:.7,topP:.9,topK:40,maxTokens:32e3,repetitionPenalty:1.1,stopSequences:[]}},"gemma-3-1b-it":{modelId:"gemma-3-1b-it",huggingFaceId:"onnx-community/gemma-3-1b-it-ONNX",contextLength:32768,dtype:"q4",defaultSettings:{temperature:.7,topP:.9,topK:40,maxTokens:32e3,repetitionPenalty:1.1,stopSequences:[]}}},P={"bge-small-zh-v1.5":{modelId:"bge-small-zh-v1.5",huggingFaceId:"Xenova/bge-small-zh-v1.5",dimensions:512,maxEmbeddingsPerCall:32},"bge-base-zh-v1.5":{modelId:"bge-base-zh-v1.5",huggingFaceId:"Xenova/bge-base-zh-v1.5",dimensions:768,maxEmbeddingsPerCall:32,dtype:"q4"},"bge-large-zh-v1.5":{modelId:"bge-large-zh-v1.5",huggingFaceId:"Xenova/bge-large-zh-v1.5",dimensions:1024,maxEmbeddingsPerCall:32,dtype:"q4"},"all-MiniLM-L6-v2":{modelId:"all-MiniLM-L6-v2",huggingFaceId:"sentence-transformers/all-MiniLM-L6-v2",dimensions:384,maxEmbeddingsPerCall:32,dtype:"q4"}};var s=class f extends Error{constructor(t,n,r){super(t);c(this,"code",n);c(this,"cause",r);this.name="LocalModelError",Error.captureStackTrace&&Error.captureStackTrace(this,f);}},T=class{static modelLoadError(e){return new s(`\u6A21\u578B\u52A0\u8F7D\u5931\u8D25: ${e.message}`,"MODEL_LOAD_ERROR",e)}static generationError(e){return new s(`\u751F\u6210\u5931\u8D25: ${e.message}`,"GENERATION_ERROR",e)}static embeddingError(e){return new s(`\u5D4C\u5165\u5931\u8D25: ${e.message}`,"EMBEDDING_ERROR",e)}static validationError(e){return new s(e,"INVALID_INPUT")}static abortError(){return new s("\u64CD\u4F5C\u5DF2\u4E2D\u6B62","ABORTED")}static unsupportedFeature(e){return new s(`\u4E0D\u652F\u6301\u7684\u529F\u80FD: ${e}`,"UNSUPPORTED_FEATURE")}};var ne=A__namespace.join(H__namespace.homedir(),".cache","easbot","models"),D=class{constructor(e){c(this,"cacheDir");this.cacheDir=e.cacheDir||ne;}getCachePath(e){return A__namespace.join(this.cacheDir,e)}isCached(e){try{let t=this.getCachePath(e);return b__namespace.existsSync(t)&&b__namespace.statSync(t).isDirectory()}catch{return false}}async clear(e){try{if(e){let t=this.getCachePath(e);b__namespace.existsSync(t)&&await this.removeDirectory(t);}else b__namespace.existsSync(this.cacheDir)&&await this.removeDirectory(this.cacheDir);}catch(t){throw new s(`\u6E05\u7406\u7F13\u5B58\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`,"CACHE_ERROR",t instanceof Error?t:void 0)}}async getCacheSize(e){try{let t=e?this.getCachePath(e):this.cacheDir;return b__namespace.existsSync(t)?await this.getDirectorySize(t):0}catch(t){throw new s(`\u83B7\u53D6\u7F13\u5B58\u5927\u5C0F\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`,"CACHE_ERROR",t instanceof Error?t:void 0)}}async getDirectorySize(e){let t=0,n=b__namespace.readdirSync(e);for(let r of n){let o=A__namespace.join(e,r),a=b__namespace.statSync(o);a.isDirectory()?t+=await this.getDirectorySize(o):t+=a.size;}return t}async removeDirectory(e){if(!b__namespace.existsSync(e))return;let t=b__namespace.readdirSync(e);for(let n of t){let r=A__namespace.join(e,n);b__namespace.statSync(r).isDirectory()?await this.removeDirectory(r):b__namespace.unlinkSync(r);}b__namespace.rmdirSync(e);}ensureCacheDir(){try{b__namespace.existsSync(this.cacheDir)||b__namespace.mkdirSync(this.cacheDir,{recursive:!0});}catch(e){throw new s(`\u521B\u5EFA\u7F13\u5B58\u76EE\u5F55\u5931\u8D25: ${e instanceof Error?e.message:String(e)}`,"CACHE_ERROR",e instanceof Error?e:void 0)}}};var I=null;async function re(){if(!I){I=(await import('@huggingface/transformers')).env;let e=A__namespace.join(H__namespace.homedir(),".cache","easbot","models");process.env.HF_HOME||(process.env.HF_HOME=e),I.allowLocalModels=true,I.allowRemoteModels=true,I.useBrowserCache=false,I.useFSCache=true;}return I}var j=null;async function J(){return j||(j=(await import('@huggingface/transformers')).pipeline),j}var y=utils.Log.create({service:"transformers-engine"});var V=class{constructor(e){c(this,"pipeline",null);c(this,"tokenizer",null);c(this,"isInitialized",false);c(this,"config");c(this,"modelCache");this.config=e,this.modelCache=new D({cacheDir:e.cacheDir});}async configureEnvironment(){let e=await re();this.modelCache.ensureCacheDir(),process.env.HF_HOME||(process.env.HF_HOME=this.config.cacheDir),e.cacheDir=this.config.cacheDir,e.allowLocalModels=true,e.allowRemoteModels=true,e.useBrowserCache=false,e.useFSCache=true,e.backends?.onnx&&(e.backends.onnx.logLevel="error"),this.config.verbose&&y.debug("\u73AF\u5883\u914D\u7F6E\u5B8C\u6210",{cacheDir:this.config.cacheDir,hfHome:process.env.HF_HOME,modelId:this.config.modelId,modelType:this.config.modelType});}async initialize(){if(this.isInitialized){this.config.verbose&&y.debug("\u6A21\u578B\u5DF2\u521D\u59CB\u5316\uFF0C\u8DF3\u8FC7\u91CD\u590D\u52A0\u8F7D");return}await this.configureEnvironment();try{this.config.verbose&&y.debug("\u5F00\u59CB\u521D\u59CB\u5316\u6A21\u578B");let e,t,n;if(this.config.modelType==="language"){let a=L[this.config.modelId];if(!a)throw new s(`\u4E0D\u652F\u6301\u7684 Language Model ID: ${this.config.modelId}`,"MODEL_NOT_FOUND");e=a.huggingFaceId,t=a.dtype,n=a.device;}else {let a=P[this.config.modelId];if(!a)throw new s(`\u4E0D\u652F\u6301\u7684 Embedding Model ID: ${this.config.modelId}`,"MODEL_NOT_FOUND");e=a.huggingFaceId,t=a.dtype;}this.config.verbose&&(y.debug("\u52A0\u8F7D\u6A21\u578B",{huggingFaceId:e,dtype:t||"auto"}),this.modelCache.isCached(e)?y.debug("\u4F7F\u7528\u7F13\u5B58\u7684\u6A21\u578B"):y.debug("\u9996\u6B21\u4E0B\u8F7D\u6A21\u578B\uFF0C\u53EF\u80FD\u9700\u8981\u4E00\u4E9B\u65F6\u95F4"));let r={cache_dir:this.config.cacheDir,local_files_only:!0};if(t&&(r.dtype=t),n&&(r.device=n),this.modelCache.isCached(e))this.config.verbose&&y.debug("\u4F7F\u7528\u672C\u5730\u7F13\u5B58\u6A21\u578B");else throw this.config.verbose&&y.warn("\u672C\u5730\u7F13\u5B58\u4E0D\u5B58\u5728",{huggingFaceId:e,cacheDir:this.config.cacheDir}),new s(`\u6A21\u578B ${e} \u672A\u627E\u5230\u672C\u5730\u7F13\u5B58\u3002\u8BF7\u5148\u4E0B\u8F7D\u6A21\u578B\u5230: ${this.config.cacheDir}`,"MODEL_NOT_FOUND");if(this.config.modelType==="language"){let a=this.config.pipelineType||"text-generation";this.config.verbose&&y.debug("\u52A0\u8F7D\u8BED\u8A00\u6A21\u578B pipeline",{pipelineType:a,huggingFaceId:e});let g=await J();this.pipeline=await g(a,e,r);}else {let a=await J();this.pipeline=await a("feature-extraction",e,r);}this.tokenizer=this.pipeline.tokenizer,this.isInitialized=!0,this.config.verbose&&y.debug("\u6A21\u578B\u521D\u59CB\u5316\u6210\u529F",{modelId:this.config.modelId});}catch(e){throw this.config.verbose&&y.error("\u6A21\u578B\u521D\u59CB\u5316\u5931\u8D25",{error:e}),T.modelLoadError(e instanceof Error?e:new Error(String(e)))}}extractAssistantResponse(e,t){let n=`<|im_start|>assistant
|
|
2
2
|
`,r=e.lastIndexOf(n);if(r!==-1){let u=e.slice(r+n.length);return u=u.replace(/<\|im_end\|>$/g,""),u.trim()}let o=`
|
|
3
3
|
assistant
|
|
4
4
|
`,a=e.lastIndexOf(o);if(a!==-1)return e.slice(a+o.length).trim();let g=`assistant
|
|
5
|
-
`,i=e.lastIndexOf(g);return i!==-1?e.slice(i+g.length).trim():(e.startsWith(t)?e.slice(t.length):e).trim()}async generate(e,t){let r=L[this.config.modelId]?.defaultSettings||{temperature:.7,topP:.9,topK:40,maxTokens:2048,repetitionPenalty:1.1,stopSequences:[]},o={temperature:t.temperature??r.temperature,topP:t.topP??r.topP,topK:t.topK??r.topK,maxTokens:t.maxTokens??r.maxTokens,repetitionPenalty:t.repetitionPenalty??r.repetitionPenalty,stopSequences:t.stopSequences??r.stopSequences,abortSignal:t.abortSignal};if(this.config.modelType!=="language")throw new s("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u6587\u672C\u751F\u6210","UNSUPPORTED_FEATURE");if(t.abortSignal?.aborted)throw T.abortError();try{this.config.verbose&&y.debug("\u5F00\u59CB\u751F\u6210\u6587\u672C");let a=await this.countTokens([e]),g=await this.pipeline(e,{max_new_tokens:o.maxTokens,temperature:o.temperature,top_p:o.topP,top_k:o.topK,repetition_penalty:o.repetitionPenalty,do_sample:o.temperature>0});if(t.abortSignal?.aborted)throw T.abortError();let i=g[0]?.generated_text||"",m=this.extractAssistantResponse(i,e),u=await this.countTokens([m]);return this.config.verbose&&y.debug("\u751F\u6210\u5B8C\u6210",{completionTokens:u}),{content:m,finishReason:B(!0,!1),usage:{promptTokens:a,completionTokens:u,totalTokens:a+u}}}catch(a){throw this.config.verbose&&y.error("\u751F\u6210\u5931\u8D25",{error:a}),a instanceof s&&a.code==="ABORTED"?a:T.generationError(a instanceof Error?a:new Error(String(a)))}}async generateStream(e,t,n){let o=L[this.config.modelId]?.defaultSettings||{temperature:.7,topP:.9,topK:40,maxTokens:2048,repetitionPenalty:1.1,stopSequences:[]};if(!this.isInitialized)throw new s("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");if(this.config.modelType!=="language")throw new s("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u6587\u672C\u751F\u6210","UNSUPPORTED_FEATURE");if(t.abortSignal?.aborted)throw T.abortError();try{this.config.verbose&&y.debug("\u5F00\u59CB\u6D41\u5F0F\u751F\u6210\u6587\u672C\uFF08\u6A21\u62DF\u6D41\u5F0F\u8F93\u51FA\uFF09");let{TextStreamer:a}=await import('@huggingface/transformers'),g=await this.pipeline(e,{max_new_tokens:t.maxTokens??o.maxTokens,temperature:t.temperature??o.temperature,top_p:t.topP??o.topP,top_k:t.topK??o.topK,repetition_penalty:t.repetitionPenalty??o.repetitionPenalty,do_sample:(t.temperature??o.temperature)>0});if(t.abortSignal?.aborted)throw T.abortError();let i=g[0]?.generated_text||"",m=this.extractAssistantResponse(i,e),u=m.split(/(\s+)/);for(let l of u){if(t.abortSignal?.aborted)throw T.abortError();l.length>0&&n({type:"text-delta",delta:l});}n({type:"text-end"});let p=await this.countTokens([e]),d=await this.countTokens([m]);n({type:"finish",finishReason:B(!0,!1),usage:{promptTokens:p,completionTokens:d,totalTokens:p+d}}),this.config.verbose&&y.debug("\u6D41\u5F0F\u751F\u6210\u5B8C\u6210",{promptTokens:p,completionTokens:d,totalTokens:p+d});}catch(a){throw this.config.verbose&&y.error("\u6D41\u5F0F\u751F\u6210\u5931\u8D25",{error:a}),a instanceof s&&a.code==="ABORTED"?a:(n({type:"finish",finishReason:B(false,true,"generation_error"),usage:{promptTokens:0,completionTokens:0,totalTokens:0}}),T.generationError(a instanceof Error?a:new Error(String(a))))}}async embed(e,t){if(!this.isInitialized)throw new s("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");if(this.config.modelType!=="embedding")throw new s("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u5D4C\u5165\u63A8\u7406","UNSUPPORTED_FEATURE");if(t?.aborted)throw T.abortError();try{this.config.verbose&&y.debug("\u5F00\u59CB\u63A8\u7406",{textCount:e.length});let n=await this.pipeline(e,{pooling:"mean",normalize:!0});if(t?.aborted)throw T.abortError();let r=[];if(n.dims&&n.dims.length===2){let[o,a]=n.dims,g=Array.from(n.data);for(let i=0;i<o;i++){let m=i*a,u=m+a;r.push(g.slice(m,u));}}else r.push(Array.from(n.data));return this.config.verbose&&y.debug("\u63A8\u7406\u5B8C\u6210",{embeddingCount:r.length}),r}catch(n){throw this.config.verbose&&y.error("\u63A8\u7406\u5931\u8D25",{error:n}),n instanceof s&&n.code==="ABORTED"?n:T.embeddingError(n instanceof Error?n:new Error(String(n)))}}async countTokens(e){if(!this.isInitialized)throw new s("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");try{let t=0;for(let n of e){let r=await this.tokenizer(n);t+=r.input_ids.data.length;}return t}catch(t){return this.config.verbose&&y.error("\u8BA1\u7B97 token \u5931\u8D25",{error:t}),e.reduce((n,r)=>n+Math.ceil(r.length/4),0)}}async dispose(){this.config.verbose&&y.debug("\u5378\u8F7D\u6A21\u578B"),this.pipeline=null,this.tokenizer=null,this.isInitialized=false,this.config.verbose&&y.debug("\u6A21\u578B\u5DF2\u5378\u8F7D");}isReady(){return this.isInitialized}};var E=class{static validateTexts(e){if(!Array.isArray(e))throw new s("\u8F93\u5165\u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6570\u7EC4","INVALID_INPUT");if(e.length===0)throw new s("\u8F93\u5165\u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A","INVALID_INPUT");for(let t=0;t<e.length;t++){let n=e[t];if(typeof n!="string")throw new s(`\u8F93\u5165\u6570\u7EC4\u7D22\u5F15 ${t} \u4E0D\u662F\u5B57\u7B26\u4E32\u7C7B\u578B`,"INVALID_INPUT");if(n.trim().length===0)throw new s(`\u8F93\u5165\u6570\u7EC4\u7D22\u5F15 ${t} \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}}static validateMessages(e){if(!Array.isArray(e))throw new s("\u6D88\u606F\u5FC5\u987B\u662F\u6570\u7EC4","INVALID_INPUT");if(e.length===0)throw new s("\u6D88\u606F\u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A","INVALID_INPUT");for(let t=0;t<e.length;t++){let n=e[t];if(!n||typeof n!="object")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u4E0D\u662F\u6709\u6548\u5BF9\u8C61`,"INVALID_INPUT");if(!n.role)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7F3A\u5C11 role \u5B57\u6BB5`,"INVALID_INPUT");if(!["system","user","assistant"].includes(n.role))throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 role "${n.role}" \u4E0D\u652F\u6301\uFF0C\u53EA\u652F\u6301 system\u3001user\u3001assistant`,"UNSUPPORTED_FEATURE");if(!n.content)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7F3A\u5C11 content \u5B57\u6BB5`,"INVALID_INPUT");if(typeof n.content=="string"){if(n.content.trim().length===0)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}else if(Array.isArray(n.content)){if(n.content.length===0)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u6570\u7EC4\u4E3A\u7A7A`,"INVALID_INPUT");for(let r=0;r<n.content.length;r++){let o=n.content[r];if(!o||typeof o!="object")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u4E0D\u662F\u6709\u6548\u5BF9\u8C61`,"INVALID_INPUT");if(!o.type)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7F3A\u5C11 type \u5B57\u6BB5`,"INVALID_INPUT");if(o.type!=="text"&&o.type!=="reasoning")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7C7B\u578B "${o.type}" \u4E0D\u652F\u6301\uFF0C\u53EA\u652F\u6301 text \u548C reasoning`,"UNSUPPORTED_FEATURE");if(!("text"in o)||typeof o.text!="string")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7F3A\u5C11\u6709\u6548\u7684 text \u5B57\u6BB5`,"INVALID_INPUT");if(o.text.trim().length===0)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7684 text \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}}else throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6216\u6570\u7EC4`,"INVALID_INPUT")}}static validateGenerateOptions(e){if(e.temperature!==void 0&&(typeof e.temperature!="number"||e.temperature<0||e.temperature>2))throw new s("temperature \u5FC5\u987B\u662F 0 \u5230 2 \u4E4B\u95F4\u7684\u6570\u5B57","INVALID_INPUT");if(e.topP!==void 0&&(typeof e.topP!="number"||e.topP<0||e.topP>1))throw new s("topP \u5FC5\u987B\u662F 0 \u5230 1 \u4E4B\u95F4\u7684\u6570\u5B57","INVALID_INPUT");if(e.topK!==void 0&&(typeof e.topK!="number"||e.topK<1||!Number.isInteger(e.topK)))throw new s("topK \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1 \u7684\u6574\u6570","INVALID_INPUT");if(e.maxTokens!==void 0&&(typeof e.maxTokens!="number"||e.maxTokens<1||!Number.isInteger(e.maxTokens)))throw new s("maxTokens \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1 \u7684\u6574\u6570","INVALID_INPUT");if(e.repetitionPenalty!==void 0&&(typeof e.repetitionPenalty!="number"||e.repetitionPenalty<0))throw new s("repetitionPenalty \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 0 \u7684\u6570\u5B57","INVALID_INPUT")}};var V=class{static createReadableStream(e,t){return new ReadableStream({async start(n){if(t?.aborted){n.error(new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));return}let r=false,o=()=>{r=true,n.error(new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));};t?.addEventListener("abort",o);try{if(await e(a=>{if(!r)try{n.enqueue(a),a.isFinished&&(n.close(),t?.removeEventListener("abort",o));}catch(g){n.error(g),t?.removeEventListener("abort",o);}}),!r)try{n.enqueue({textDelta:"",isFinished:!0,finishReason:"stop"}),n.close();}catch{}}catch(a){r||n.error(a instanceof s?a:new s(`\u751F\u6210\u5931\u8D25: ${a instanceof Error?a.message:String(a)}`,"GENERATION_ERROR",a instanceof Error?a:void 0));}finally{t?.removeEventListener("abort",o);}},cancel(){}})}static async*streamToAsyncIterator(e){let t=e.getReader();try{for(;;){let{done:n,value:r}=await t.read();if(n)break;yield r;}}finally{t.releaseLock();}}};function ae(){return `${Date.now()}-${Math.random().toString(36).substring(2,9)}`}var v=class{constructor(e,t,n){c(this,"specificationVersion","v3");c(this,"provider");c(this,"modelId");c(this,"defaultObjectGenerationMode","json");c(this,"supportedUrls",{});c(this,"engine");c(this,"modelConfig");if(this.provider=e,this.modelId=t,this.engine=n,this.modelConfig=L[t],!this.modelConfig)throw new s(`\u4E0D\u652F\u6301\u7684\u6A21\u578B ID: ${t}`,"MODEL_NOT_FOUND")}async generate(e){return this.doGenerate(e)}async stream(e){return this.doStream(e)}convertToQwenFormat(e){let t=[];for(let n of e){let r="";switch(typeof n.content=="string"?r=n.content:Array.isArray(n.content)&&(r=n.content.filter(o=>o.type==="text").map(o=>"text"in o?o.text:"").join(`
|
|
5
|
+
`,i=e.lastIndexOf(g);return i!==-1?e.slice(i+g.length).trim():(e.startsWith(t)?e.slice(t.length):e).trim()}async generate(e,t){let r=L[this.config.modelId]?.defaultSettings||{temperature:.7,topP:.9,topK:40,maxTokens:2048,repetitionPenalty:1.1,stopSequences:[]},o={temperature:t.temperature??r.temperature,topP:t.topP??r.topP,topK:t.topK??r.topK,maxTokens:t.maxTokens??r.maxTokens,repetitionPenalty:t.repetitionPenalty??r.repetitionPenalty,stopSequences:t.stopSequences??r.stopSequences,abortSignal:t.abortSignal};if(this.config.modelType!=="language")throw new s("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u6587\u672C\u751F\u6210","UNSUPPORTED_FEATURE");if(t.abortSignal?.aborted)throw T.abortError();try{this.config.verbose&&y.debug("\u5F00\u59CB\u751F\u6210\u6587\u672C");let a=await this.countTokens([e]),g=await this.pipeline(e,{max_new_tokens:o.maxTokens,temperature:o.temperature,top_p:o.topP,top_k:o.topK,repetition_penalty:o.repetitionPenalty,do_sample:o.temperature>0});if(t.abortSignal?.aborted)throw T.abortError();let i=g[0]?.generated_text||"",m=this.extractAssistantResponse(i,e),u=await this.countTokens([m]);return this.config.verbose&&y.debug("\u751F\u6210\u5B8C\u6210",{completionTokens:u}),{content:m,finishReason:B(!0,!1),usage:{promptTokens:a,completionTokens:u,totalTokens:a+u}}}catch(a){throw this.config.verbose&&y.error("\u751F\u6210\u5931\u8D25",{error:a}),a instanceof s&&a.code==="ABORTED"?a:T.generationError(a instanceof Error?a:new Error(String(a)))}}async generateStream(e,t,n){let o=L[this.config.modelId]?.defaultSettings||{temperature:.7,topP:.9,topK:40,maxTokens:2048,repetitionPenalty:1.1,stopSequences:[]};if(!this.isInitialized)throw new s("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");if(this.config.modelType!=="language")throw new s("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u6587\u672C\u751F\u6210","UNSUPPORTED_FEATURE");if(t.abortSignal?.aborted)throw T.abortError();try{this.config.verbose&&y.debug("\u5F00\u59CB\u6D41\u5F0F\u751F\u6210\u6587\u672C\uFF08\u6A21\u62DF\u6D41\u5F0F\u8F93\u51FA\uFF09");let{TextStreamer:a}=await import('@huggingface/transformers'),g=await this.pipeline(e,{max_new_tokens:t.maxTokens??o.maxTokens,temperature:t.temperature??o.temperature,top_p:t.topP??o.topP,top_k:t.topK??o.topK,repetition_penalty:t.repetitionPenalty??o.repetitionPenalty,do_sample:(t.temperature??o.temperature)>0});if(t.abortSignal?.aborted)throw T.abortError();let i=g[0]?.generated_text||"",m=this.extractAssistantResponse(i,e),u=m.split(/(\s+)/);for(let l of u){if(t.abortSignal?.aborted)throw T.abortError();l.length>0&&n({type:"text-delta",delta:l});}n({type:"text-end"});let p=await this.countTokens([e]),d=await this.countTokens([m]);n({type:"finish",finishReason:B(!0,!1),usage:{promptTokens:p,completionTokens:d,totalTokens:p+d}}),this.config.verbose&&y.debug("\u6D41\u5F0F\u751F\u6210\u5B8C\u6210",{promptTokens:p,completionTokens:d,totalTokens:p+d});}catch(a){throw this.config.verbose&&y.error("\u6D41\u5F0F\u751F\u6210\u5931\u8D25",{error:a}),a instanceof s&&a.code==="ABORTED"?a:(n({type:"finish",finishReason:B(false,true,"generation_error"),usage:{promptTokens:0,completionTokens:0,totalTokens:0}}),T.generationError(a instanceof Error?a:new Error(String(a))))}}async embed(e,t){if(!this.isInitialized)throw new s("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");if(this.config.modelType!=="embedding")throw new s("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u5D4C\u5165\u63A8\u7406","UNSUPPORTED_FEATURE");if(t?.aborted)throw T.abortError();try{this.config.verbose&&y.debug("\u5F00\u59CB\u63A8\u7406",{textCount:e.length});let n=await this.pipeline(e,{pooling:"mean",normalize:!0});if(t?.aborted)throw T.abortError();let r=[];if(n.dims&&n.dims.length===2){let[o,a]=n.dims,g=Array.from(n.data);for(let i=0;i<o;i++){let m=i*a,u=m+a;r.push(g.slice(m,u));}}else r.push(Array.from(n.data));return this.config.verbose&&y.debug("\u63A8\u7406\u5B8C\u6210",{embeddingCount:r.length}),r}catch(n){throw this.config.verbose&&y.error("\u63A8\u7406\u5931\u8D25",{error:n}),n instanceof s&&n.code==="ABORTED"?n:T.embeddingError(n instanceof Error?n:new Error(String(n)))}}async countTokens(e){if(!this.isInitialized)throw new s("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");try{let t=0;for(let n of e){let r=await this.tokenizer(n);t+=r.input_ids.data.length;}return t}catch(t){return this.config.verbose&&y.error("\u8BA1\u7B97 token \u5931\u8D25",{error:t}),e.reduce((n,r)=>n+Math.ceil(r.length/4),0)}}async dispose(){this.config.verbose&&y.debug("\u5378\u8F7D\u6A21\u578B"),this.pipeline=null,this.tokenizer=null,this.isInitialized=false,this.config.verbose&&y.debug("\u6A21\u578B\u5DF2\u5378\u8F7D");}isReady(){return this.isInitialized}};var E=class{static validateTexts(e){if(!Array.isArray(e))throw new s("\u8F93\u5165\u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6570\u7EC4","INVALID_INPUT");if(e.length===0)throw new s("\u8F93\u5165\u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A","INVALID_INPUT");for(let t=0;t<e.length;t++){let n=e[t];if(typeof n!="string")throw new s(`\u8F93\u5165\u6570\u7EC4\u7D22\u5F15 ${t} \u4E0D\u662F\u5B57\u7B26\u4E32\u7C7B\u578B`,"INVALID_INPUT");if(n.trim().length===0)throw new s(`\u8F93\u5165\u6570\u7EC4\u7D22\u5F15 ${t} \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}}static validateMessages(e){if(!Array.isArray(e))throw new s("\u6D88\u606F\u5FC5\u987B\u662F\u6570\u7EC4","INVALID_INPUT");if(e.length===0)throw new s("\u6D88\u606F\u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A","INVALID_INPUT");for(let t=0;t<e.length;t++){let n=e[t];if(!n||typeof n!="object")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u4E0D\u662F\u6709\u6548\u5BF9\u8C61`,"INVALID_INPUT");if(!n.role)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7F3A\u5C11 role \u5B57\u6BB5`,"INVALID_INPUT");if(!["system","user","assistant"].includes(n.role))throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 role "${n.role}" \u4E0D\u652F\u6301\uFF0C\u53EA\u652F\u6301 system\u3001user\u3001assistant`,"UNSUPPORTED_FEATURE");if(!n.content)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7F3A\u5C11 content \u5B57\u6BB5`,"INVALID_INPUT");if(typeof n.content=="string"){if(n.content.trim().length===0)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}else if(Array.isArray(n.content)){if(n.content.length===0)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u6570\u7EC4\u4E3A\u7A7A`,"INVALID_INPUT");for(let r=0;r<n.content.length;r++){let o=n.content[r];if(!o||typeof o!="object")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u4E0D\u662F\u6709\u6548\u5BF9\u8C61`,"INVALID_INPUT");if(!o.type)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7F3A\u5C11 type \u5B57\u6BB5`,"INVALID_INPUT");if(o.type!=="text"&&o.type!=="reasoning")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7C7B\u578B "${o.type}" \u4E0D\u652F\u6301\uFF0C\u53EA\u652F\u6301 text \u548C reasoning`,"UNSUPPORTED_FEATURE");if(!("text"in o)||typeof o.text!="string")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7F3A\u5C11\u6709\u6548\u7684 text \u5B57\u6BB5`,"INVALID_INPUT");if(o.text.trim().length===0)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7684 text \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}}else throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6216\u6570\u7EC4`,"INVALID_INPUT")}}static validateGenerateOptions(e){if(e.temperature!==void 0&&(typeof e.temperature!="number"||e.temperature<0||e.temperature>2))throw new s("temperature \u5FC5\u987B\u662F 0 \u5230 2 \u4E4B\u95F4\u7684\u6570\u5B57","INVALID_INPUT");if(e.topP!==void 0&&(typeof e.topP!="number"||e.topP<0||e.topP>1))throw new s("topP \u5FC5\u987B\u662F 0 \u5230 1 \u4E4B\u95F4\u7684\u6570\u5B57","INVALID_INPUT");if(e.topK!==void 0&&(typeof e.topK!="number"||e.topK<1||!Number.isInteger(e.topK)))throw new s("topK \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1 \u7684\u6574\u6570","INVALID_INPUT");if(e.maxTokens!==void 0&&(typeof e.maxTokens!="number"||e.maxTokens<1||!Number.isInteger(e.maxTokens)))throw new s("maxTokens \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1 \u7684\u6574\u6570","INVALID_INPUT");if(e.repetitionPenalty!==void 0&&(typeof e.repetitionPenalty!="number"||e.repetitionPenalty<0))throw new s("repetitionPenalty \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 0 \u7684\u6570\u5B57","INVALID_INPUT")}};var G=class{static createReadableStream(e,t){return new ReadableStream({async start(n){if(t?.aborted){n.error(new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));return}let r=false,o=()=>{r=true,n.error(new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));};t?.addEventListener("abort",o);try{if(await e(a=>{if(!r)try{n.enqueue(a),a.isFinished&&(n.close(),t?.removeEventListener("abort",o));}catch(g){n.error(g),t?.removeEventListener("abort",o);}}),!r)try{n.enqueue({textDelta:"",isFinished:!0,finishReason:"stop"}),n.close();}catch{}}catch(a){r||n.error(a instanceof s?a:new s(`\u751F\u6210\u5931\u8D25: ${a instanceof Error?a.message:String(a)}`,"GENERATION_ERROR",a instanceof Error?a:void 0));}finally{t?.removeEventListener("abort",o);}},cancel(){}})}static async*streamToAsyncIterator(e){let t=e.getReader();try{for(;;){let{done:n,value:r}=await t.read();if(n)break;yield r;}}finally{t.releaseLock();}}};function ae(){return `${Date.now()}-${Math.random().toString(36).substring(2,9)}`}var v=class{constructor(e,t,n){c(this,"specificationVersion","v3");c(this,"provider");c(this,"modelId");c(this,"defaultObjectGenerationMode","json");c(this,"supportedUrls",{});c(this,"engine");c(this,"modelConfig");if(this.provider=e,this.modelId=t,this.engine=n,this.modelConfig=L[t],!this.modelConfig)throw new s(`\u4E0D\u652F\u6301\u7684\u6A21\u578B ID: ${t}`,"MODEL_NOT_FOUND")}async generate(e){return this.doGenerate(e)}async stream(e){return this.doStream(e)}convertToQwenFormat(e){let t=[];for(let n of e){let r="";switch(typeof n.content=="string"?r=n.content:Array.isArray(n.content)&&(r=n.content.filter(o=>o.type==="text").map(o=>"text"in o?o.text:"").join(`
|
|
6
6
|
`)),n.role){case "system":t.push(`<|im_start|>system
|
|
7
7
|
${r}<|im_end|>`);break;case "user":t.push(`<|im_start|>user
|
|
8
8
|
${r}<|im_end|>`);break;case "assistant":t.push(`<|im_start|>assistant
|
|
9
9
|
${r}<|im_end|>`);break;default:throw new s(`\u4E0D\u652F\u6301\u7684\u6D88\u606F\u89D2\u8272: ${n.role}`,"UNSUPPORTED_FEATURE")}}return t.push(`<|im_start|>assistant
|
|
10
10
|
`),t.join(`
|
|
11
|
-
`)}async doGenerate(e){E.validateMessages(e.prompt);let t=this.convertToQwenFormat(e.prompt),n={temperature:e.temperature??this.modelConfig.defaultSettings.temperature,topP:e.topP??this.modelConfig.defaultSettings.topP,topK:e.topK??this.modelConfig.defaultSettings.topK,maxTokens:e.maxOutputTokens??this.modelConfig.defaultSettings.maxTokens,repetitionPenalty:this.modelConfig.defaultSettings.repetitionPenalty,stopSequences:this.modelConfig.defaultSettings.stopSequences,abortSignal:e.abortSignal};E.validateGenerateOptions(n),this.engine.isReady()||await this.engine.initialize();let r=await this.engine.generate(t,n);return {content:[{type:"text",text:r.content||""}],finishReason:r.finishReason,usage:{inputTokens:r.usage.promptTokens,inputTokenDetails:{noCacheTokens:void 0,cacheReadTokens:void 0,cacheWriteTokens:void 0},outputTokens:r.usage.completionTokens,outputTokenDetails:{textTokens:void 0,reasoningTokens:void 0},totalTokens:(r.usage.promptTokens||0)+(r.usage.completionTokens||0)},warnings:[],rawCall:{rawPrompt:t,rawSettings:n}}}async doStream(e){E.validateMessages(e.prompt);let t=this.convertToQwenFormat(e.prompt),n={temperature:e.temperature??this.modelConfig.defaultSettings.temperature,topP:e.topP??this.modelConfig.defaultSettings.topP,topK:e.topK??this.modelConfig.defaultSettings.topK,maxTokens:e.maxTokens??this.modelConfig.defaultSettings.maxTokens,repetitionPenalty:this.modelConfig.defaultSettings.repetitionPenalty,stopSequences:this.modelConfig.defaultSettings.stopSequences,abortSignal:e.abortSignal};E.validateGenerateOptions(n),this.engine.isReady()||await this.engine.initialize();let r=
|
|
11
|
+
`)}async doGenerate(e){E.validateMessages(e.prompt);let t=this.convertToQwenFormat(e.prompt),n={temperature:e.temperature??this.modelConfig.defaultSettings.temperature,topP:e.topP??this.modelConfig.defaultSettings.topP,topK:e.topK??this.modelConfig.defaultSettings.topK,maxTokens:e.maxOutputTokens??this.modelConfig.defaultSettings.maxTokens,repetitionPenalty:this.modelConfig.defaultSettings.repetitionPenalty,stopSequences:this.modelConfig.defaultSettings.stopSequences,abortSignal:e.abortSignal};E.validateGenerateOptions(n),this.engine.isReady()||await this.engine.initialize();let r=await this.engine.generate(t,n);return {content:[{type:"text",text:r.content||""}],finishReason:r.finishReason,usage:{inputTokens:r.usage.promptTokens,inputTokenDetails:{noCacheTokens:void 0,cacheReadTokens:void 0,cacheWriteTokens:void 0},outputTokens:r.usage.completionTokens,outputTokenDetails:{textTokens:void 0,reasoningTokens:void 0},totalTokens:(r.usage.promptTokens||0)+(r.usage.completionTokens||0)},warnings:[],rawCall:{rawPrompt:t,rawSettings:n}}}async doStream(e){E.validateMessages(e.prompt);let t=this.convertToQwenFormat(e.prompt),n={temperature:e.temperature??this.modelConfig.defaultSettings.temperature,topP:e.topP??this.modelConfig.defaultSettings.topP,topK:e.topK??this.modelConfig.defaultSettings.topK,maxTokens:e.maxTokens??this.modelConfig.defaultSettings.maxTokens,repetitionPenalty:this.modelConfig.defaultSettings.repetitionPenalty,stopSequences:this.modelConfig.defaultSettings.stopSequences,abortSignal:e.abortSignal};E.validateGenerateOptions(n),this.engine.isReady()||await this.engine.initialize();let r=G.createReadableStream(async g=>{await this.engine.generateStream(t,n,i=>{if(i.type==="text-delta"){let m=i.delta||"";g({textDelta:m,isFinished:false});}else i.type==="finish"&&g({textDelta:"",isFinished:true,finishReason:i.finishReason});});},e.abortSignal),o=null;return {stream:r.pipeThrough(new TransformStream({transform(g,i){g.isFinished?(o&&(i.enqueue({type:"text-end",id:o}),o=null),i.enqueue({type:"finish",finishReason:g.finishReason||"stop",usage:{inputTokens:{total:0,noCache:void 0,cacheRead:void 0,cacheWrite:void 0},outputTokens:{total:0,text:void 0,reasoning:void 0}}})):g.textDelta&&(o||(o=ae(),i.enqueue({type:"text-start",id:o})),i.enqueue({type:"text-delta",id:o,delta:g.textDelta}));}})),rawCall:{rawPrompt:t,rawSettings:n}}}};var _=class f{static buildEnhancedPrompt(e,t){let n=[];n.push(f.buildSystemPrompt(t));for(let r of e){let o=f.formatMessage(r);o&&n.push(o);}return n.push(`<|im_start|>assistant
|
|
12
12
|
`),n.join(`
|
|
13
13
|
`)}static buildSystemPrompt(e){return e&&e.length>0?`<|im_start|>system
|
|
14
14
|
Environment: ipython
|
|
@@ -193,4 +193,4 @@ ${n}
|
|
|
193
193
|
|
|
194
194
|
${n}
|
|
195
195
|
|
|
196
|
-
\u8F93\u51FA\u683C\u5F0F\uFF1A1,2,3,4,5\uFF08\u6700\u76F8\u5173\u7684\u6392\u5728\u524D\u9762\uFF09`}parseRerankResult(e,t,n){let r=[];try{let o=e.match(/\d+/g)?.map(d=>parseInt(d,10)-1).filter(d=>d>=0&&d<t.length)||[],a=[],g=new Set;for(let d of o)g.has(d)||(g.add(d),a.push(d));let i=[],m=new Set;for(let d of a)if(d!==void 0&&d>=0&&d<t.length&&!m.has(d)){m.add(d);let l=t[d];l&&i.push({index:d,score:this.options.totalScore*(1-i.length/a.length),document:l});}let u=new Set(a);for(let d=0;d<t.length;d++)if(!u.has(d)){let l=this.options.totalScore*.1*(1-d/t.length);i.push({index:d,score:l,document:t[d]});}let p=n?i.slice(0,n):i;return this.log.debug("Parsed rerank results",{extractedNumbers:o,uniqueIndices:a,resultCount:p.length,topK:n}),{results:p,warnings:r}}catch(o){return this.log.error("Failed to parse rerank result",{error:o.message,text:e}),{results:t.map((g,i)=>({index:i,score:this.options.totalScore*(1-i/t.length),document:g})),warnings:[`Failed to parse rerank result: ${o.message}`]}}}};var k=utils.Log.create({service:"local-model-provider"}),ce={cacheDir:A__namespace.default.join(process.env.EASBOT_CACHE_PATH??`${A__namespace.default.join(H__namespace.homedir(),".cache","easbot")}`,"models"),verbose:false,autoUnloadTimeout:300*1e3,maxCachedModels:3,memoryWarningThreshold:1.5*1024*1024*1024,useEnhancedModel:true
|
|
196
|
+
\u8F93\u51FA\u683C\u5F0F\uFF1A1,2,3,4,5\uFF08\u6700\u76F8\u5173\u7684\u6392\u5728\u524D\u9762\uFF09`}parseRerankResult(e,t,n){let r=[];try{let o=e.match(/\d+/g)?.map(d=>parseInt(d,10)-1).filter(d=>d>=0&&d<t.length)||[],a=[],g=new Set;for(let d of o)g.has(d)||(g.add(d),a.push(d));let i=[],m=new Set;for(let d of a)if(d!==void 0&&d>=0&&d<t.length&&!m.has(d)){m.add(d);let l=t[d];l&&i.push({index:d,score:this.options.totalScore*(1-i.length/a.length),document:l});}let u=new Set(a);for(let d=0;d<t.length;d++)if(!u.has(d)){let l=this.options.totalScore*.1*(1-d/t.length);i.push({index:d,score:l,document:t[d]});}let p=n?i.slice(0,n):i;return this.log.debug("Parsed rerank results",{extractedNumbers:o,uniqueIndices:a,resultCount:p.length,topK:n}),{results:p,warnings:r}}catch(o){return this.log.error("Failed to parse rerank result",{error:o.message,text:e}),{results:t.map((g,i)=>({index:i,score:this.options.totalScore*(1-i/t.length),document:g})),warnings:[`Failed to parse rerank result: ${o.message}`]}}}};var k=utils.Log.create({service:"local-model-provider"}),ce={cacheDir:A__namespace.default.join(process.env.EASBOT_CACHE_PATH??`${A__namespace.default.join(H__namespace.homedir(),".cache","easbot")}`,"models"),verbose:false,autoUnloadTimeout:300*1e3,maxCachedModels:3,memoryWarningThreshold:1.5*1024*1024*1024,useEnhancedModel:true},K=class{constructor(e){c(this,"config");c(this,"modelCache");c(this,"engines",new Map);c(this,"modelInfo",new Map);this.config={...ce,...e},this.modelCache=new D({cacheDir:this.config.cacheDir});}languageModel(e,t){if(!L[e])throw new s(`\u4E0D\u652F\u6301\u7684 Language Model ID: ${e}`,"MODEL_NOT_FOUND");let n=this.getOrCreateEngine(e,"language");return t?.useEnhancedModel??this.config.useEnhancedModel?(this.config.verbose&&k.debug("\u521B\u5EFA\u589E\u5F3A\u8BED\u8A00\u6A21\u578B",{modelId:e}),new U("local",e,n)):(this.config.verbose&&k.debug("\u521B\u5EFA\u57FA\u7840\u8BED\u8A00\u6A21\u578B",{modelId:e}),new v("local",e,n))}embeddingModel(e){if(!P[e])throw new s(`\u4E0D\u652F\u6301\u7684 Embedding Model ID: ${e}`,"MODEL_NOT_FOUND");let t=this.getOrCreateEngine(e,"embedding");return new $("local",e,t)}chat(e,t){return this.languageModel(e,t)}rerankingModel(e,t){if(this.config.verbose&&k.debug("\u521B\u5EFA\u91CD\u6392\u5E8F\u6A21\u578B",{modelId:e,hasExternalLlm:!!t?.llm}),t?.llm)return new F(e,t.llm,t);if(!L[e])throw new s(`\u4E0D\u652F\u6301\u7684 Language Model ID: ${e}`,"MODEL_NOT_FOUND");let n=this.getOrCreateEngine(e,"language");return new q(e,this.config,n,t)}getOrCreateEngine(e,t){let n=`${t}:${e}`;if(this.engines.has(n)){let o=this.modelInfo.get(n);return o&&(o.lastUsed=Date.now(),o.useCount++),this.config.verbose&&k.debug("\u590D\u7528\u5DF2\u52A0\u8F7D\u7684\u6A21\u578B",{engineKey:n}),this.engines.get(n)}this.engines.size>=this.config.maxCachedModels&&this.evictLeastRecentlyUsed(),this.config.verbose&&k.debug("\u521B\u5EFA\u65B0\u5F15\u64CE",{engineKey:n});let r=new V({modelId:e,modelType:t,cacheDir:this.config.cacheDir,verbose:this.config.verbose});return this.engines.set(n,r),this.modelInfo.set(n,{modelId:e,modelType:t,isLoaded:false,lastUsed:Date.now(),useCount:1}),r}evictLeastRecentlyUsed(){let e=null,t=Number.POSITIVE_INFINITY;for(let[n,r]of this.modelInfo.entries())r.lastUsed<t&&(t=r.lastUsed,e=n);if(e){this.config.verbose&&k.debug("\u6DD8\u6C70\u6A21\u578B",{engineKey:e});let n=this.engines.get(e);n&&n.dispose().catch(r=>{k.error("\u5378\u8F7D\u6A21\u578B\u5931\u8D25",{error:r});}),this.engines.delete(e),this.modelInfo.delete(e);}}getLoadedModels(){return Array.from(this.modelInfo.values())}async dispose(){this.config.verbose&&k.debug("\u5378\u8F7D\u6240\u6709\u6A21\u578B");let e=Array.from(this.engines.values()).map(t=>t.dispose());await Promise.all(e),this.engines.clear(),this.modelInfo.clear(),this.config.verbose&&k.debug("\u6240\u6709\u6A21\u578B\u5DF2\u5378\u8F7D");}};function ue(f){let e=new K(f);return Object.assign((n,r)=>e.languageModel(n,r),{languageModel:(n,r)=>e.languageModel(n,r),chat:(n,r)=>e.chat(n,r),embeddingModel:n=>e.embeddingModel(n),rerankingModel:(n,r)=>e.rerankingModel(n,r)})}var me="0.1.0",pe="@easbot/local-model-sdk",fe="Complete local model SDK for EASBot - Transformers.js + ONNX implementation";exports.ErrorCode=M;exports.ErrorHandler=T;exports.LLMRerankingModel=F;exports.LocalEmbeddingModel=$;exports.LocalEnhancedLanguageModel=U;exports.LocalLanguageModel=v;exports.LocalModelError=s;exports.LocalModelProvider=K;exports.LocalRerankingModel=q;exports.SDK_DESCRIPTION=fe;exports.SDK_NAME=pe;exports.SUPPORTED_EMBEDDING_MODELS=P;exports.SUPPORTED_LANGUAGE_MODELS=L;exports.VERSION=me;exports.createLocal=ue;
|
package/dist/index.d.cts
CHANGED
|
@@ -27,8 +27,6 @@ interface LocalModelProviderConfig {
|
|
|
27
27
|
maxCachedModels?: number;
|
|
28
28
|
memoryWarningThreshold?: number;
|
|
29
29
|
useEnhancedModel?: boolean;
|
|
30
|
-
logDir?: string;
|
|
31
|
-
logLevel?: 'DEBUG' | 'INFO' | 'WARN' | 'ERROR';
|
|
32
30
|
}
|
|
33
31
|
interface LanguageModelConfig {
|
|
34
32
|
modelId: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -27,8 +27,6 @@ interface LocalModelProviderConfig {
|
|
|
27
27
|
maxCachedModels?: number;
|
|
28
28
|
memoryWarningThreshold?: number;
|
|
29
29
|
useEnhancedModel?: boolean;
|
|
30
|
-
logDir?: string;
|
|
31
|
-
logLevel?: 'DEBUG' | 'INFO' | 'WARN' | 'ERROR';
|
|
32
30
|
}
|
|
33
31
|
interface LanguageModelConfig {
|
|
34
32
|
modelId: string;
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import*as J from'os';import*as _ from'path';import ___default from'path';import*as b from'fs';import {Log}from'@easbot/utils';import {streamText}from'ai';var te=Object.defineProperty;var ne=(f,e,t)=>e in f?te(f,e,{enumerable:true,configurable:true,writable:true,value:t}):f[e]=t;var c=(f,e,t)=>ne(f,typeof e!="symbol"?e+"":e,t);var w=(d=>(d.MODEL_NOT_FOUND="MODEL_NOT_FOUND",d.MODEL_LOAD_ERROR="MODEL_LOAD_ERROR",d.MODEL_NOT_INITIALIZED="MODEL_NOT_INITIALIZED",d.GENERATION_ERROR="GENERATION_ERROR",d.EMBEDDING_ERROR="EMBEDDING_ERROR",d.INVALID_INPUT="INVALID_INPUT",d.UNSUPPORTED_FEATURE="UNSUPPORTED_FEATURE",d.CACHE_ERROR="CACHE_ERROR",d.PROCESSING_ERROR="PROCESSING_ERROR",d.ABORTED="ABORTED",d.OUT_OF_MEMORY="OUT_OF_MEMORY",d.UNKNOWN_ERROR="UNKNOWN_ERROR",d))(w||{});function
|
|
1
|
+
import*as J from'os';import*as _ from'path';import ___default from'path';import*as b from'fs';import {Log}from'@easbot/utils';import {streamText}from'ai';var te=Object.defineProperty;var ne=(f,e,t)=>e in f?te(f,e,{enumerable:true,configurable:true,writable:true,value:t}):f[e]=t;var c=(f,e,t)=>ne(f,typeof e!="symbol"?e+"":e,t);var w=(d=>(d.MODEL_NOT_FOUND="MODEL_NOT_FOUND",d.MODEL_LOAD_ERROR="MODEL_LOAD_ERROR",d.MODEL_NOT_INITIALIZED="MODEL_NOT_INITIALIZED",d.GENERATION_ERROR="GENERATION_ERROR",d.EMBEDDING_ERROR="EMBEDDING_ERROR",d.INVALID_INPUT="INVALID_INPUT",d.UNSUPPORTED_FEATURE="UNSUPPORTED_FEATURE",d.CACHE_ERROR="CACHE_ERROR",d.PROCESSING_ERROR="PROCESSING_ERROR",d.ABORTED="ABORTED",d.OUT_OF_MEMORY="OUT_OF_MEMORY",d.UNKNOWN_ERROR="UNKNOWN_ERROR",d))(w||{});function V(f,e,t){let n;return e?n="error":f?n="stop":n="other",{unified:n,raw:t}}var x={"qwen3-0.6b":{modelId:"qwen3-0.6b",huggingFaceId:"onnx-community/Qwen3-0.6B-ONNX",contextLength:32768,device:"cpu",dtype:"q4f16",defaultSettings:{temperature:0,topP:.9,topK:40,maxTokens:32e3,repetitionPenalty:1.1,stopSequences:[]}},"qwen3.5-0.8b":{modelId:"qwen3.5-0.8b",huggingFaceId:"onnx-community/Qwen3.5-0.8B-ONNX",contextLength:32768,dtype:"q4",defaultSettings:{temperature:.7,topP:.9,topK:40,maxTokens:32e3,repetitionPenalty:1.1,stopSequences:[]}},"gemma-3-1b-it":{modelId:"gemma-3-1b-it",huggingFaceId:"onnx-community/gemma-3-1b-it-ONNX",contextLength:32768,dtype:"q4",defaultSettings:{temperature:.7,topP:.9,topK:40,maxTokens:32e3,repetitionPenalty:1.1,stopSequences:[]}}},I={"bge-small-zh-v1.5":{modelId:"bge-small-zh-v1.5",huggingFaceId:"Xenova/bge-small-zh-v1.5",dimensions:512,maxEmbeddingsPerCall:32},"bge-base-zh-v1.5":{modelId:"bge-base-zh-v1.5",huggingFaceId:"Xenova/bge-base-zh-v1.5",dimensions:768,maxEmbeddingsPerCall:32,dtype:"q4"},"bge-large-zh-v1.5":{modelId:"bge-large-zh-v1.5",huggingFaceId:"Xenova/bge-large-zh-v1.5",dimensions:1024,maxEmbeddingsPerCall:32,dtype:"q4"},"all-MiniLM-L6-v2":{modelId:"all-MiniLM-L6-v2",huggingFaceId:"sentence-transformers/all-MiniLM-L6-v2",dimensions:384,maxEmbeddingsPerCall:32,dtype:"q4"}};var s=class f extends Error{constructor(t,n,r){super(t);c(this,"code",n);c(this,"cause",r);this.name="LocalModelError",Error.captureStackTrace&&Error.captureStackTrace(this,f);}},E=class{static modelLoadError(e){return new s(`\u6A21\u578B\u52A0\u8F7D\u5931\u8D25: ${e.message}`,"MODEL_LOAD_ERROR",e)}static generationError(e){return new s(`\u751F\u6210\u5931\u8D25: ${e.message}`,"GENERATION_ERROR",e)}static embeddingError(e){return new s(`\u5D4C\u5165\u5931\u8D25: ${e.message}`,"EMBEDDING_ERROR",e)}static validationError(e){return new s(e,"INVALID_INPUT")}static abortError(){return new s("\u64CD\u4F5C\u5DF2\u4E2D\u6B62","ABORTED")}static unsupportedFeature(e){return new s(`\u4E0D\u652F\u6301\u7684\u529F\u80FD: ${e}`,"UNSUPPORTED_FEATURE")}};var re=_.join(J.homedir(),".cache","easbot","models"),N=class{constructor(e){c(this,"cacheDir");this.cacheDir=e.cacheDir||re;}getCachePath(e){return _.join(this.cacheDir,e)}isCached(e){try{let t=this.getCachePath(e);return b.existsSync(t)&&b.statSync(t).isDirectory()}catch{return false}}async clear(e){try{if(e){let t=this.getCachePath(e);b.existsSync(t)&&await this.removeDirectory(t);}else b.existsSync(this.cacheDir)&&await this.removeDirectory(this.cacheDir);}catch(t){throw new s(`\u6E05\u7406\u7F13\u5B58\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`,"CACHE_ERROR",t instanceof Error?t:void 0)}}async getCacheSize(e){try{let t=e?this.getCachePath(e):this.cacheDir;return b.existsSync(t)?await this.getDirectorySize(t):0}catch(t){throw new s(`\u83B7\u53D6\u7F13\u5B58\u5927\u5C0F\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`,"CACHE_ERROR",t instanceof Error?t:void 0)}}async getDirectorySize(e){let t=0,n=b.readdirSync(e);for(let r of n){let o=_.join(e,r),a=b.statSync(o);a.isDirectory()?t+=await this.getDirectorySize(o):t+=a.size;}return t}async removeDirectory(e){if(!b.existsSync(e))return;let t=b.readdirSync(e);for(let n of t){let r=_.join(e,n);b.statSync(r).isDirectory()?await this.removeDirectory(r):b.unlinkSync(r);}b.rmdirSync(e);}ensureCacheDir(){try{b.existsSync(this.cacheDir)||b.mkdirSync(this.cacheDir,{recursive:!0});}catch(e){throw new s(`\u521B\u5EFA\u7F13\u5B58\u76EE\u5F55\u5931\u8D25: ${e instanceof Error?e.message:String(e)}`,"CACHE_ERROR",e instanceof Error?e:void 0)}}};var v=null;async function oe(){if(!v){v=(await import('@huggingface/transformers')).env;let e=_.join(J.homedir(),".cache","easbot","models");process.env.HF_HOME||(process.env.HF_HOME=e),v.allowLocalModels=true,v.allowRemoteModels=true,v.useBrowserCache=false,v.useFSCache=true;}return v}var H=null;async function W(){return H||(H=(await import('@huggingface/transformers')).pipeline),H}var y=Log.create({service:"transformers-engine"});var G=class{constructor(e){c(this,"pipeline",null);c(this,"tokenizer",null);c(this,"isInitialized",false);c(this,"config");c(this,"modelCache");this.config=e,this.modelCache=new N({cacheDir:e.cacheDir});}async configureEnvironment(){let e=await oe();this.modelCache.ensureCacheDir(),process.env.HF_HOME||(process.env.HF_HOME=this.config.cacheDir),e.cacheDir=this.config.cacheDir,e.allowLocalModels=true,e.allowRemoteModels=true,e.useBrowserCache=false,e.useFSCache=true,e.backends?.onnx&&(e.backends.onnx.logLevel="error"),this.config.verbose&&y.debug("\u73AF\u5883\u914D\u7F6E\u5B8C\u6210",{cacheDir:this.config.cacheDir,hfHome:process.env.HF_HOME,modelId:this.config.modelId,modelType:this.config.modelType});}async initialize(){if(this.isInitialized){this.config.verbose&&y.debug("\u6A21\u578B\u5DF2\u521D\u59CB\u5316\uFF0C\u8DF3\u8FC7\u91CD\u590D\u52A0\u8F7D");return}await this.configureEnvironment();try{this.config.verbose&&y.debug("\u5F00\u59CB\u521D\u59CB\u5316\u6A21\u578B");let e,t,n;if(this.config.modelType==="language"){let a=x[this.config.modelId];if(!a)throw new s(`\u4E0D\u652F\u6301\u7684 Language Model ID: ${this.config.modelId}`,"MODEL_NOT_FOUND");e=a.huggingFaceId,t=a.dtype,n=a.device;}else {let a=I[this.config.modelId];if(!a)throw new s(`\u4E0D\u652F\u6301\u7684 Embedding Model ID: ${this.config.modelId}`,"MODEL_NOT_FOUND");e=a.huggingFaceId,t=a.dtype;}this.config.verbose&&(y.debug("\u52A0\u8F7D\u6A21\u578B",{huggingFaceId:e,dtype:t||"auto"}),this.modelCache.isCached(e)?y.debug("\u4F7F\u7528\u7F13\u5B58\u7684\u6A21\u578B"):y.debug("\u9996\u6B21\u4E0B\u8F7D\u6A21\u578B\uFF0C\u53EF\u80FD\u9700\u8981\u4E00\u4E9B\u65F6\u95F4"));let r={cache_dir:this.config.cacheDir,local_files_only:!0};if(t&&(r.dtype=t),n&&(r.device=n),this.modelCache.isCached(e))this.config.verbose&&y.debug("\u4F7F\u7528\u672C\u5730\u7F13\u5B58\u6A21\u578B");else throw this.config.verbose&&y.warn("\u672C\u5730\u7F13\u5B58\u4E0D\u5B58\u5728",{huggingFaceId:e,cacheDir:this.config.cacheDir}),new s(`\u6A21\u578B ${e} \u672A\u627E\u5230\u672C\u5730\u7F13\u5B58\u3002\u8BF7\u5148\u4E0B\u8F7D\u6A21\u578B\u5230: ${this.config.cacheDir}`,"MODEL_NOT_FOUND");if(this.config.modelType==="language"){let a=this.config.pipelineType||"text-generation";this.config.verbose&&y.debug("\u52A0\u8F7D\u8BED\u8A00\u6A21\u578B pipeline",{pipelineType:a,huggingFaceId:e});let g=await W();this.pipeline=await g(a,e,r);}else {let a=await W();this.pipeline=await a("feature-extraction",e,r);}this.tokenizer=this.pipeline.tokenizer,this.isInitialized=!0,this.config.verbose&&y.debug("\u6A21\u578B\u521D\u59CB\u5316\u6210\u529F",{modelId:this.config.modelId});}catch(e){throw this.config.verbose&&y.error("\u6A21\u578B\u521D\u59CB\u5316\u5931\u8D25",{error:e}),E.modelLoadError(e instanceof Error?e:new Error(String(e)))}}extractAssistantResponse(e,t){let n=`<|im_start|>assistant
|
|
2
2
|
`,r=e.lastIndexOf(n);if(r!==-1){let u=e.slice(r+n.length);return u=u.replace(/<\|im_end\|>$/g,""),u.trim()}let o=`
|
|
3
3
|
assistant
|
|
4
4
|
`,a=e.lastIndexOf(o);if(a!==-1)return e.slice(a+o.length).trim();let g=`assistant
|
|
5
|
-
`,i=e.lastIndexOf(g);return i!==-1?e.slice(i+g.length).trim():(e.startsWith(t)?e.slice(t.length):e).trim()}async generate(e,t){let r=x[this.config.modelId]?.defaultSettings||{temperature:.7,topP:.9,topK:40,maxTokens:2048,repetitionPenalty:1.1,stopSequences:[]},o={temperature:t.temperature??r.temperature,topP:t.topP??r.topP,topK:t.topK??r.topK,maxTokens:t.maxTokens??r.maxTokens,repetitionPenalty:t.repetitionPenalty??r.repetitionPenalty,stopSequences:t.stopSequences??r.stopSequences,abortSignal:t.abortSignal};if(this.config.modelType!=="language")throw new s("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u6587\u672C\u751F\u6210","UNSUPPORTED_FEATURE");if(t.abortSignal?.aborted)throw E.abortError();try{this.config.verbose&&y.debug("\u5F00\u59CB\u751F\u6210\u6587\u672C");let a=await this.countTokens([e]),g=await this.pipeline(e,{max_new_tokens:o.maxTokens,temperature:o.temperature,top_p:o.topP,top_k:o.topK,repetition_penalty:o.repetitionPenalty,do_sample:o.temperature>0});if(t.abortSignal?.aborted)throw E.abortError();let i=g[0]?.generated_text||"",m=this.extractAssistantResponse(i,e),u=await this.countTokens([m]);return this.config.verbose&&y.debug("\u751F\u6210\u5B8C\u6210",{completionTokens:u}),{content:m,finishReason:G(!0,!1),usage:{promptTokens:a,completionTokens:u,totalTokens:a+u}}}catch(a){throw this.config.verbose&&y.error("\u751F\u6210\u5931\u8D25",{error:a}),a instanceof s&&a.code==="ABORTED"?a:E.generationError(a instanceof Error?a:new Error(String(a)))}}async generateStream(e,t,n){let o=x[this.config.modelId]?.defaultSettings||{temperature:.7,topP:.9,topK:40,maxTokens:2048,repetitionPenalty:1.1,stopSequences:[]};if(!this.isInitialized)throw new s("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");if(this.config.modelType!=="language")throw new s("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u6587\u672C\u751F\u6210","UNSUPPORTED_FEATURE");if(t.abortSignal?.aborted)throw E.abortError();try{this.config.verbose&&y.debug("\u5F00\u59CB\u6D41\u5F0F\u751F\u6210\u6587\u672C\uFF08\u6A21\u62DF\u6D41\u5F0F\u8F93\u51FA\uFF09");let{TextStreamer:a}=await import('@huggingface/transformers'),g=await this.pipeline(e,{max_new_tokens:t.maxTokens??o.maxTokens,temperature:t.temperature??o.temperature,top_p:t.topP??o.topP,top_k:t.topK??o.topK,repetition_penalty:t.repetitionPenalty??o.repetitionPenalty,do_sample:(t.temperature??o.temperature)>0});if(t.abortSignal?.aborted)throw E.abortError();let i=g[0]?.generated_text||"",m=this.extractAssistantResponse(i,e),u=m.split(/(\s+)/);for(let l of u){if(t.abortSignal?.aborted)throw E.abortError();l.length>0&&n({type:"text-delta",delta:l});}n({type:"text-end"});let p=await this.countTokens([e]),d=await this.countTokens([m]);n({type:"finish",finishReason:G(!0,!1),usage:{promptTokens:p,completionTokens:d,totalTokens:p+d}}),this.config.verbose&&y.debug("\u6D41\u5F0F\u751F\u6210\u5B8C\u6210",{promptTokens:p,completionTokens:d,totalTokens:p+d});}catch(a){throw this.config.verbose&&y.error("\u6D41\u5F0F\u751F\u6210\u5931\u8D25",{error:a}),a instanceof s&&a.code==="ABORTED"?a:(n({type:"finish",finishReason:G(false,true,"generation_error"),usage:{promptTokens:0,completionTokens:0,totalTokens:0}}),E.generationError(a instanceof Error?a:new Error(String(a))))}}async embed(e,t){if(!this.isInitialized)throw new s("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");if(this.config.modelType!=="embedding")throw new s("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u5D4C\u5165\u63A8\u7406","UNSUPPORTED_FEATURE");if(t?.aborted)throw E.abortError();try{this.config.verbose&&y.debug("\u5F00\u59CB\u63A8\u7406",{textCount:e.length});let n=await this.pipeline(e,{pooling:"mean",normalize:!0});if(t?.aborted)throw E.abortError();let r=[];if(n.dims&&n.dims.length===2){let[o,a]=n.dims,g=Array.from(n.data);for(let i=0;i<o;i++){let m=i*a,u=m+a;r.push(g.slice(m,u));}}else r.push(Array.from(n.data));return this.config.verbose&&y.debug("\u63A8\u7406\u5B8C\u6210",{embeddingCount:r.length}),r}catch(n){throw this.config.verbose&&y.error("\u63A8\u7406\u5931\u8D25",{error:n}),n instanceof s&&n.code==="ABORTED"?n:E.embeddingError(n instanceof Error?n:new Error(String(n)))}}async countTokens(e){if(!this.isInitialized)throw new s("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");try{let t=0;for(let n of e){let r=await this.tokenizer(n);t+=r.input_ids.data.length;}return t}catch(t){return this.config.verbose&&y.error("\u8BA1\u7B97 token \u5931\u8D25",{error:t}),e.reduce((n,r)=>n+Math.ceil(r.length/4),0)}}async dispose(){this.config.verbose&&y.debug("\u5378\u8F7D\u6A21\u578B"),this.pipeline=null,this.tokenizer=null,this.isInitialized=false,this.config.verbose&&y.debug("\u6A21\u578B\u5DF2\u5378\u8F7D");}isReady(){return this.isInitialized}};var M=class{static validateTexts(e){if(!Array.isArray(e))throw new s("\u8F93\u5165\u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6570\u7EC4","INVALID_INPUT");if(e.length===0)throw new s("\u8F93\u5165\u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A","INVALID_INPUT");for(let t=0;t<e.length;t++){let n=e[t];if(typeof n!="string")throw new s(`\u8F93\u5165\u6570\u7EC4\u7D22\u5F15 ${t} \u4E0D\u662F\u5B57\u7B26\u4E32\u7C7B\u578B`,"INVALID_INPUT");if(n.trim().length===0)throw new s(`\u8F93\u5165\u6570\u7EC4\u7D22\u5F15 ${t} \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}}static validateMessages(e){if(!Array.isArray(e))throw new s("\u6D88\u606F\u5FC5\u987B\u662F\u6570\u7EC4","INVALID_INPUT");if(e.length===0)throw new s("\u6D88\u606F\u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A","INVALID_INPUT");for(let t=0;t<e.length;t++){let n=e[t];if(!n||typeof n!="object")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u4E0D\u662F\u6709\u6548\u5BF9\u8C61`,"INVALID_INPUT");if(!n.role)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7F3A\u5C11 role \u5B57\u6BB5`,"INVALID_INPUT");if(!["system","user","assistant"].includes(n.role))throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 role "${n.role}" \u4E0D\u652F\u6301\uFF0C\u53EA\u652F\u6301 system\u3001user\u3001assistant`,"UNSUPPORTED_FEATURE");if(!n.content)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7F3A\u5C11 content \u5B57\u6BB5`,"INVALID_INPUT");if(typeof n.content=="string"){if(n.content.trim().length===0)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}else if(Array.isArray(n.content)){if(n.content.length===0)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u6570\u7EC4\u4E3A\u7A7A`,"INVALID_INPUT");for(let r=0;r<n.content.length;r++){let o=n.content[r];if(!o||typeof o!="object")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u4E0D\u662F\u6709\u6548\u5BF9\u8C61`,"INVALID_INPUT");if(!o.type)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7F3A\u5C11 type \u5B57\u6BB5`,"INVALID_INPUT");if(o.type!=="text"&&o.type!=="reasoning")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7C7B\u578B "${o.type}" \u4E0D\u652F\u6301\uFF0C\u53EA\u652F\u6301 text \u548C reasoning`,"UNSUPPORTED_FEATURE");if(!("text"in o)||typeof o.text!="string")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7F3A\u5C11\u6709\u6548\u7684 text \u5B57\u6BB5`,"INVALID_INPUT");if(o.text.trim().length===0)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7684 text \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}}else throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6216\u6570\u7EC4`,"INVALID_INPUT")}}static validateGenerateOptions(e){if(e.temperature!==void 0&&(typeof e.temperature!="number"||e.temperature<0||e.temperature>2))throw new s("temperature \u5FC5\u987B\u662F 0 \u5230 2 \u4E4B\u95F4\u7684\u6570\u5B57","INVALID_INPUT");if(e.topP!==void 0&&(typeof e.topP!="number"||e.topP<0||e.topP>1))throw new s("topP \u5FC5\u987B\u662F 0 \u5230 1 \u4E4B\u95F4\u7684\u6570\u5B57","INVALID_INPUT");if(e.topK!==void 0&&(typeof e.topK!="number"||e.topK<1||!Number.isInteger(e.topK)))throw new s("topK \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1 \u7684\u6574\u6570","INVALID_INPUT");if(e.maxTokens!==void 0&&(typeof e.maxTokens!="number"||e.maxTokens<1||!Number.isInteger(e.maxTokens)))throw new s("maxTokens \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1 \u7684\u6574\u6570","INVALID_INPUT");if(e.repetitionPenalty!==void 0&&(typeof e.repetitionPenalty!="number"||e.repetitionPenalty<0))throw new s("repetitionPenalty \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 0 \u7684\u6570\u5B57","INVALID_INPUT")}};var z=class{static createReadableStream(e,t){return new ReadableStream({async start(n){if(t?.aborted){n.error(new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));return}let r=false,o=()=>{r=true,n.error(new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));};t?.addEventListener("abort",o);try{if(await e(a=>{if(!r)try{n.enqueue(a),a.isFinished&&(n.close(),t?.removeEventListener("abort",o));}catch(g){n.error(g),t?.removeEventListener("abort",o);}}),!r)try{n.enqueue({textDelta:"",isFinished:!0,finishReason:"stop"}),n.close();}catch{}}catch(a){r||n.error(a instanceof s?a:new s(`\u751F\u6210\u5931\u8D25: ${a instanceof Error?a.message:String(a)}`,"GENERATION_ERROR",a instanceof Error?a:void 0));}finally{t?.removeEventListener("abort",o);}},cancel(){}})}static async*streamToAsyncIterator(e){let t=e.getReader();try{for(;;){let{done:n,value:r}=await t.read();if(n)break;yield r;}}finally{t.releaseLock();}}};function ie(){return `${Date.now()}-${Math.random().toString(36).substring(2,9)}`}var O=class{constructor(e,t,n){c(this,"specificationVersion","v3");c(this,"provider");c(this,"modelId");c(this,"defaultObjectGenerationMode","json");c(this,"supportedUrls",{});c(this,"engine");c(this,"modelConfig");if(this.provider=e,this.modelId=t,this.engine=n,this.modelConfig=x[t],!this.modelConfig)throw new s(`\u4E0D\u652F\u6301\u7684\u6A21\u578B ID: ${t}`,"MODEL_NOT_FOUND")}async generate(e){return this.doGenerate(e)}async stream(e){return this.doStream(e)}convertToQwenFormat(e){let t=[];for(let n of e){let r="";switch(typeof n.content=="string"?r=n.content:Array.isArray(n.content)&&(r=n.content.filter(o=>o.type==="text").map(o=>"text"in o?o.text:"").join(`
|
|
5
|
+
`,i=e.lastIndexOf(g);return i!==-1?e.slice(i+g.length).trim():(e.startsWith(t)?e.slice(t.length):e).trim()}async generate(e,t){let r=x[this.config.modelId]?.defaultSettings||{temperature:.7,topP:.9,topK:40,maxTokens:2048,repetitionPenalty:1.1,stopSequences:[]},o={temperature:t.temperature??r.temperature,topP:t.topP??r.topP,topK:t.topK??r.topK,maxTokens:t.maxTokens??r.maxTokens,repetitionPenalty:t.repetitionPenalty??r.repetitionPenalty,stopSequences:t.stopSequences??r.stopSequences,abortSignal:t.abortSignal};if(this.config.modelType!=="language")throw new s("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u6587\u672C\u751F\u6210","UNSUPPORTED_FEATURE");if(t.abortSignal?.aborted)throw E.abortError();try{this.config.verbose&&y.debug("\u5F00\u59CB\u751F\u6210\u6587\u672C");let a=await this.countTokens([e]),g=await this.pipeline(e,{max_new_tokens:o.maxTokens,temperature:o.temperature,top_p:o.topP,top_k:o.topK,repetition_penalty:o.repetitionPenalty,do_sample:o.temperature>0});if(t.abortSignal?.aborted)throw E.abortError();let i=g[0]?.generated_text||"",m=this.extractAssistantResponse(i,e),u=await this.countTokens([m]);return this.config.verbose&&y.debug("\u751F\u6210\u5B8C\u6210",{completionTokens:u}),{content:m,finishReason:V(!0,!1),usage:{promptTokens:a,completionTokens:u,totalTokens:a+u}}}catch(a){throw this.config.verbose&&y.error("\u751F\u6210\u5931\u8D25",{error:a}),a instanceof s&&a.code==="ABORTED"?a:E.generationError(a instanceof Error?a:new Error(String(a)))}}async generateStream(e,t,n){let o=x[this.config.modelId]?.defaultSettings||{temperature:.7,topP:.9,topK:40,maxTokens:2048,repetitionPenalty:1.1,stopSequences:[]};if(!this.isInitialized)throw new s("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");if(this.config.modelType!=="language")throw new s("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u6587\u672C\u751F\u6210","UNSUPPORTED_FEATURE");if(t.abortSignal?.aborted)throw E.abortError();try{this.config.verbose&&y.debug("\u5F00\u59CB\u6D41\u5F0F\u751F\u6210\u6587\u672C\uFF08\u6A21\u62DF\u6D41\u5F0F\u8F93\u51FA\uFF09");let{TextStreamer:a}=await import('@huggingface/transformers'),g=await this.pipeline(e,{max_new_tokens:t.maxTokens??o.maxTokens,temperature:t.temperature??o.temperature,top_p:t.topP??o.topP,top_k:t.topK??o.topK,repetition_penalty:t.repetitionPenalty??o.repetitionPenalty,do_sample:(t.temperature??o.temperature)>0});if(t.abortSignal?.aborted)throw E.abortError();let i=g[0]?.generated_text||"",m=this.extractAssistantResponse(i,e),u=m.split(/(\s+)/);for(let l of u){if(t.abortSignal?.aborted)throw E.abortError();l.length>0&&n({type:"text-delta",delta:l});}n({type:"text-end"});let p=await this.countTokens([e]),d=await this.countTokens([m]);n({type:"finish",finishReason:V(!0,!1),usage:{promptTokens:p,completionTokens:d,totalTokens:p+d}}),this.config.verbose&&y.debug("\u6D41\u5F0F\u751F\u6210\u5B8C\u6210",{promptTokens:p,completionTokens:d,totalTokens:p+d});}catch(a){throw this.config.verbose&&y.error("\u6D41\u5F0F\u751F\u6210\u5931\u8D25",{error:a}),a instanceof s&&a.code==="ABORTED"?a:(n({type:"finish",finishReason:V(false,true,"generation_error"),usage:{promptTokens:0,completionTokens:0,totalTokens:0}}),E.generationError(a instanceof Error?a:new Error(String(a))))}}async embed(e,t){if(!this.isInitialized)throw new s("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");if(this.config.modelType!=="embedding")throw new s("\u5F53\u524D\u6A21\u578B\u4E0D\u652F\u6301\u5D4C\u5165\u63A8\u7406","UNSUPPORTED_FEATURE");if(t?.aborted)throw E.abortError();try{this.config.verbose&&y.debug("\u5F00\u59CB\u63A8\u7406",{textCount:e.length});let n=await this.pipeline(e,{pooling:"mean",normalize:!0});if(t?.aborted)throw E.abortError();let r=[];if(n.dims&&n.dims.length===2){let[o,a]=n.dims,g=Array.from(n.data);for(let i=0;i<o;i++){let m=i*a,u=m+a;r.push(g.slice(m,u));}}else r.push(Array.from(n.data));return this.config.verbose&&y.debug("\u63A8\u7406\u5B8C\u6210",{embeddingCount:r.length}),r}catch(n){throw this.config.verbose&&y.error("\u63A8\u7406\u5931\u8D25",{error:n}),n instanceof s&&n.code==="ABORTED"?n:E.embeddingError(n instanceof Error?n:new Error(String(n)))}}async countTokens(e){if(!this.isInitialized)throw new s("\u6A21\u578B\u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize() \u65B9\u6CD5","MODEL_NOT_INITIALIZED");try{let t=0;for(let n of e){let r=await this.tokenizer(n);t+=r.input_ids.data.length;}return t}catch(t){return this.config.verbose&&y.error("\u8BA1\u7B97 token \u5931\u8D25",{error:t}),e.reduce((n,r)=>n+Math.ceil(r.length/4),0)}}async dispose(){this.config.verbose&&y.debug("\u5378\u8F7D\u6A21\u578B"),this.pipeline=null,this.tokenizer=null,this.isInitialized=false,this.config.verbose&&y.debug("\u6A21\u578B\u5DF2\u5378\u8F7D");}isReady(){return this.isInitialized}};var M=class{static validateTexts(e){if(!Array.isArray(e))throw new s("\u8F93\u5165\u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6570\u7EC4","INVALID_INPUT");if(e.length===0)throw new s("\u8F93\u5165\u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A","INVALID_INPUT");for(let t=0;t<e.length;t++){let n=e[t];if(typeof n!="string")throw new s(`\u8F93\u5165\u6570\u7EC4\u7D22\u5F15 ${t} \u4E0D\u662F\u5B57\u7B26\u4E32\u7C7B\u578B`,"INVALID_INPUT");if(n.trim().length===0)throw new s(`\u8F93\u5165\u6570\u7EC4\u7D22\u5F15 ${t} \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}}static validateMessages(e){if(!Array.isArray(e))throw new s("\u6D88\u606F\u5FC5\u987B\u662F\u6570\u7EC4","INVALID_INPUT");if(e.length===0)throw new s("\u6D88\u606F\u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A","INVALID_INPUT");for(let t=0;t<e.length;t++){let n=e[t];if(!n||typeof n!="object")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u4E0D\u662F\u6709\u6548\u5BF9\u8C61`,"INVALID_INPUT");if(!n.role)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7F3A\u5C11 role \u5B57\u6BB5`,"INVALID_INPUT");if(!["system","user","assistant"].includes(n.role))throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 role "${n.role}" \u4E0D\u652F\u6301\uFF0C\u53EA\u652F\u6301 system\u3001user\u3001assistant`,"UNSUPPORTED_FEATURE");if(!n.content)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7F3A\u5C11 content \u5B57\u6BB5`,"INVALID_INPUT");if(typeof n.content=="string"){if(n.content.trim().length===0)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}else if(Array.isArray(n.content)){if(n.content.length===0)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u6570\u7EC4\u4E3A\u7A7A`,"INVALID_INPUT");for(let r=0;r<n.content.length;r++){let o=n.content[r];if(!o||typeof o!="object")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u4E0D\u662F\u6709\u6548\u5BF9\u8C61`,"INVALID_INPUT");if(!o.type)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7F3A\u5C11 type \u5B57\u6BB5`,"INVALID_INPUT");if(o.type!=="text"&&o.type!=="reasoning")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7C7B\u578B "${o.type}" \u4E0D\u652F\u6301\uFF0C\u53EA\u652F\u6301 text \u548C reasoning`,"UNSUPPORTED_FEATURE");if(!("text"in o)||typeof o.text!="string")throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7F3A\u5C11\u6709\u6548\u7684 text \u5B57\u6BB5`,"INVALID_INPUT");if(o.text.trim().length===0)throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content part ${r} \u7684 text \u662F\u7A7A\u5B57\u7B26\u4E32`,"INVALID_INPUT")}}else throw new s(`\u6D88\u606F\u7D22\u5F15 ${t} \u7684 content \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6216\u6570\u7EC4`,"INVALID_INPUT")}}static validateGenerateOptions(e){if(e.temperature!==void 0&&(typeof e.temperature!="number"||e.temperature<0||e.temperature>2))throw new s("temperature \u5FC5\u987B\u662F 0 \u5230 2 \u4E4B\u95F4\u7684\u6570\u5B57","INVALID_INPUT");if(e.topP!==void 0&&(typeof e.topP!="number"||e.topP<0||e.topP>1))throw new s("topP \u5FC5\u987B\u662F 0 \u5230 1 \u4E4B\u95F4\u7684\u6570\u5B57","INVALID_INPUT");if(e.topK!==void 0&&(typeof e.topK!="number"||e.topK<1||!Number.isInteger(e.topK)))throw new s("topK \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1 \u7684\u6574\u6570","INVALID_INPUT");if(e.maxTokens!==void 0&&(typeof e.maxTokens!="number"||e.maxTokens<1||!Number.isInteger(e.maxTokens)))throw new s("maxTokens \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1 \u7684\u6574\u6570","INVALID_INPUT");if(e.repetitionPenalty!==void 0&&(typeof e.repetitionPenalty!="number"||e.repetitionPenalty<0))throw new s("repetitionPenalty \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 0 \u7684\u6570\u5B57","INVALID_INPUT")}};var z=class{static createReadableStream(e,t){return new ReadableStream({async start(n){if(t?.aborted){n.error(new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));return}let r=false,o=()=>{r=true,n.error(new s("\u64CD\u4F5C\u5DF2\u53D6\u6D88","ABORTED"));};t?.addEventListener("abort",o);try{if(await e(a=>{if(!r)try{n.enqueue(a),a.isFinished&&(n.close(),t?.removeEventListener("abort",o));}catch(g){n.error(g),t?.removeEventListener("abort",o);}}),!r)try{n.enqueue({textDelta:"",isFinished:!0,finishReason:"stop"}),n.close();}catch{}}catch(a){r||n.error(a instanceof s?a:new s(`\u751F\u6210\u5931\u8D25: ${a instanceof Error?a.message:String(a)}`,"GENERATION_ERROR",a instanceof Error?a:void 0));}finally{t?.removeEventListener("abort",o);}},cancel(){}})}static async*streamToAsyncIterator(e){let t=e.getReader();try{for(;;){let{done:n,value:r}=await t.read();if(n)break;yield r;}}finally{t.releaseLock();}}};function ie(){return `${Date.now()}-${Math.random().toString(36).substring(2,9)}`}var O=class{constructor(e,t,n){c(this,"specificationVersion","v3");c(this,"provider");c(this,"modelId");c(this,"defaultObjectGenerationMode","json");c(this,"supportedUrls",{});c(this,"engine");c(this,"modelConfig");if(this.provider=e,this.modelId=t,this.engine=n,this.modelConfig=x[t],!this.modelConfig)throw new s(`\u4E0D\u652F\u6301\u7684\u6A21\u578B ID: ${t}`,"MODEL_NOT_FOUND")}async generate(e){return this.doGenerate(e)}async stream(e){return this.doStream(e)}convertToQwenFormat(e){let t=[];for(let n of e){let r="";switch(typeof n.content=="string"?r=n.content:Array.isArray(n.content)&&(r=n.content.filter(o=>o.type==="text").map(o=>"text"in o?o.text:"").join(`
|
|
6
6
|
`)),n.role){case "system":t.push(`<|im_start|>system
|
|
7
7
|
${r}<|im_end|>`);break;case "user":t.push(`<|im_start|>user
|
|
8
8
|
${r}<|im_end|>`);break;case "assistant":t.push(`<|im_start|>assistant
|
|
@@ -193,4 +193,4 @@ ${n}
|
|
|
193
193
|
|
|
194
194
|
${n}
|
|
195
195
|
|
|
196
|
-
\u8F93\u51FA\u683C\u5F0F\uFF1A1,2,3,4,5\uFF08\u6700\u76F8\u5173\u7684\u6392\u5728\u524D\u9762\uFF09`}parseRerankResult(e,t,n){let r=[];try{let o=e.match(/\d+/g)?.map(d=>parseInt(d,10)-1).filter(d=>d>=0&&d<t.length)||[],a=[],g=new Set;for(let d of o)g.has(d)||(g.add(d),a.push(d));let i=[],m=new Set;for(let d of a)if(d!==void 0&&d>=0&&d<t.length&&!m.has(d)){m.add(d);let l=t[d];l&&i.push({index:d,score:this.options.totalScore*(1-i.length/a.length),document:l});}let u=new Set(a);for(let d=0;d<t.length;d++)if(!u.has(d)){let l=this.options.totalScore*.1*(1-d/t.length);i.push({index:d,score:l,document:t[d]});}let p=n?i.slice(0,n):i;return this.log.debug("Parsed rerank results",{extractedNumbers:o,uniqueIndices:a,resultCount:p.length,topK:n}),{results:p,warnings:r}}catch(o){return this.log.error("Failed to parse rerank result",{error:o.message,text:e}),{results:t.map((g,i)=>({index:i,score:this.options.totalScore*(1-i/t.length),document:g})),warnings:[`Failed to parse rerank result: ${o.message}`]}}}};var R=Log.create({service:"local-model-provider"}),ue={cacheDir:___default.join(process.env.EASBOT_CACHE_PATH??`${___default.join(J.homedir(),".cache","easbot")}`,"models"),verbose:false,autoUnloadTimeout:300*1e3,maxCachedModels:3,memoryWarningThreshold:1.5*1024*1024*1024,useEnhancedModel:true
|
|
196
|
+
\u8F93\u51FA\u683C\u5F0F\uFF1A1,2,3,4,5\uFF08\u6700\u76F8\u5173\u7684\u6392\u5728\u524D\u9762\uFF09`}parseRerankResult(e,t,n){let r=[];try{let o=e.match(/\d+/g)?.map(d=>parseInt(d,10)-1).filter(d=>d>=0&&d<t.length)||[],a=[],g=new Set;for(let d of o)g.has(d)||(g.add(d),a.push(d));let i=[],m=new Set;for(let d of a)if(d!==void 0&&d>=0&&d<t.length&&!m.has(d)){m.add(d);let l=t[d];l&&i.push({index:d,score:this.options.totalScore*(1-i.length/a.length),document:l});}let u=new Set(a);for(let d=0;d<t.length;d++)if(!u.has(d)){let l=this.options.totalScore*.1*(1-d/t.length);i.push({index:d,score:l,document:t[d]});}let p=n?i.slice(0,n):i;return this.log.debug("Parsed rerank results",{extractedNumbers:o,uniqueIndices:a,resultCount:p.length,topK:n}),{results:p,warnings:r}}catch(o){return this.log.error("Failed to parse rerank result",{error:o.message,text:e}),{results:t.map((g,i)=>({index:i,score:this.options.totalScore*(1-i/t.length),document:g})),warnings:[`Failed to parse rerank result: ${o.message}`]}}}};var R=Log.create({service:"local-model-provider"}),ue={cacheDir:___default.join(process.env.EASBOT_CACHE_PATH??`${___default.join(J.homedir(),".cache","easbot")}`,"models"),verbose:false,autoUnloadTimeout:300*1e3,maxCachedModels:3,memoryWarningThreshold:1.5*1024*1024*1024,useEnhancedModel:true},j=class{constructor(e){c(this,"config");c(this,"modelCache");c(this,"engines",new Map);c(this,"modelInfo",new Map);this.config={...ue,...e},this.modelCache=new N({cacheDir:this.config.cacheDir});}languageModel(e,t){if(!x[e])throw new s(`\u4E0D\u652F\u6301\u7684 Language Model ID: ${e}`,"MODEL_NOT_FOUND");let n=this.getOrCreateEngine(e,"language");return t?.useEnhancedModel??this.config.useEnhancedModel?(this.config.verbose&&R.debug("\u521B\u5EFA\u589E\u5F3A\u8BED\u8A00\u6A21\u578B",{modelId:e}),new $("local",e,n)):(this.config.verbose&&R.debug("\u521B\u5EFA\u57FA\u7840\u8BED\u8A00\u6A21\u578B",{modelId:e}),new O("local",e,n))}embeddingModel(e){if(!I[e])throw new s(`\u4E0D\u652F\u6301\u7684 Embedding Model ID: ${e}`,"MODEL_NOT_FOUND");let t=this.getOrCreateEngine(e,"embedding");return new q("local",e,t)}chat(e,t){return this.languageModel(e,t)}rerankingModel(e,t){if(this.config.verbose&&R.debug("\u521B\u5EFA\u91CD\u6392\u5E8F\u6A21\u578B",{modelId:e,hasExternalLlm:!!t?.llm}),t?.llm)return new B(e,t.llm,t);if(!x[e])throw new s(`\u4E0D\u652F\u6301\u7684 Language Model ID: ${e}`,"MODEL_NOT_FOUND");let n=this.getOrCreateEngine(e,"language");return new F(e,this.config,n,t)}getOrCreateEngine(e,t){let n=`${t}:${e}`;if(this.engines.has(n)){let o=this.modelInfo.get(n);return o&&(o.lastUsed=Date.now(),o.useCount++),this.config.verbose&&R.debug("\u590D\u7528\u5DF2\u52A0\u8F7D\u7684\u6A21\u578B",{engineKey:n}),this.engines.get(n)}this.engines.size>=this.config.maxCachedModels&&this.evictLeastRecentlyUsed(),this.config.verbose&&R.debug("\u521B\u5EFA\u65B0\u5F15\u64CE",{engineKey:n});let r=new G({modelId:e,modelType:t,cacheDir:this.config.cacheDir,verbose:this.config.verbose});return this.engines.set(n,r),this.modelInfo.set(n,{modelId:e,modelType:t,isLoaded:false,lastUsed:Date.now(),useCount:1}),r}evictLeastRecentlyUsed(){let e=null,t=Number.POSITIVE_INFINITY;for(let[n,r]of this.modelInfo.entries())r.lastUsed<t&&(t=r.lastUsed,e=n);if(e){this.config.verbose&&R.debug("\u6DD8\u6C70\u6A21\u578B",{engineKey:e});let n=this.engines.get(e);n&&n.dispose().catch(r=>{R.error("\u5378\u8F7D\u6A21\u578B\u5931\u8D25",{error:r});}),this.engines.delete(e),this.modelInfo.delete(e);}}getLoadedModels(){return Array.from(this.modelInfo.values())}async dispose(){this.config.verbose&&R.debug("\u5378\u8F7D\u6240\u6709\u6A21\u578B");let e=Array.from(this.engines.values()).map(t=>t.dispose());await Promise.all(e),this.engines.clear(),this.modelInfo.clear(),this.config.verbose&&R.debug("\u6240\u6709\u6A21\u578B\u5DF2\u5378\u8F7D");}};function me(f){let e=new j(f);return Object.assign((n,r)=>e.languageModel(n,r),{languageModel:(n,r)=>e.languageModel(n,r),chat:(n,r)=>e.chat(n,r),embeddingModel:n=>e.embeddingModel(n),rerankingModel:(n,r)=>e.rerankingModel(n,r)})}var pe="0.1.0",fe="@easbot/local-model-sdk",he="Complete local model SDK for EASBot - Transformers.js + ONNX implementation";export{w as ErrorCode,E as ErrorHandler,B as LLMRerankingModel,q as LocalEmbeddingModel,$ as LocalEnhancedLanguageModel,O as LocalLanguageModel,s as LocalModelError,j as LocalModelProvider,F as LocalRerankingModel,he as SDK_DESCRIPTION,fe as SDK_NAME,I as SUPPORTED_EMBEDDING_MODELS,x as SUPPORTED_LANGUAGE_MODELS,pe as VERSION,me as createLocal};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@easbot/local-model-sdk",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "Complete local model SDK for EASBot - Transformers.js + ONNX implementation supporting both Language Model (text generation) and Embedding (text embedding) with full AI SDK v2 compatibility",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -57,8 +57,8 @@
|
|
|
57
57
|
"axios": "^1.16.1",
|
|
58
58
|
"onnxruntime-node": "^1.25.1",
|
|
59
59
|
"zod": "^4.4.3",
|
|
60
|
-
"@easbot/types": "0.3.
|
|
61
|
-
"@easbot/utils": "0.3.
|
|
60
|
+
"@easbot/types": "0.3.3",
|
|
61
|
+
"@easbot/utils": "0.3.3"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
64
|
"@biomejs/biome": "^2.4.14",
|