fmode-ng 0.0.237 → 0.0.238
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/esm2022/lib/core/agent/chat/completion/fmode-completion.mjs +1 -1
- package/esm2022/lib/core/parse/fmode.user.mjs +1 -1
- package/fesm2022/fmode-ng.mjs +1 -1
- package/fesm2022/fmode-ng.mjs.map +1 -1
- package/lib/core/agent/chat/completion/fmode-completion.d.ts +3 -0
- package/lib/core/parse/fmode.user.d.ts +38 -0
- package/lib/core/social/wxwork/wxwork.sdk.d.ts +1 -1
- package/lib/user/comp-user-avatar/comp-user-avatar.component.d.ts +1 -1
- package/package.json +1 -1
|
@@ -5,6 +5,6 @@
|
|
|
5
5
|
* 保留所有权利 All Rights Reserved.
|
|
6
6
|
* /home/ryan/workspace/nova/nova-admin/dist/fmode-ng/esm2022/lib/core/agent/chat/completion/fmode-completion.mjs
|
|
7
7
|
*/
|
|
8
|
-
import{bufferTime,concatMap,delay,Observable}from"rxjs";const API_BASE="https://server.fmode.cn/api/apig/aigc/gpt";import{FmodeParse}from"../../../../core/parse";const Parse=FmodeParse.with("nova");export class FmodeChatCompletion{constructor(e,t){this.content="",this.contentBuffer=[],this.isCompleted=!1,this.indexOfList=Number(e.length),this.messages=e,this.model=t?.model||"fmode-1.6-cn",this.max_tokens=t?.max_tokens||32768,this.tools=t?.tools}sendCompletion(e={messages:[]}){e.intTime=e?.intTime||50,e.isDirect=e?.isDirect||!1,e?.isDirect&&(e.intTime=1);const t={messages:this.messages,stream
|
|
8
|
+
import{bufferTime,concatMap,delay,Observable}from"rxjs";const API_BASE="https://server.fmode.cn/api/apig/aigc/gpt";import{FmodeParse}from"../../../../core/parse";const Parse=FmodeParse.with("nova");export class FmodeChatCompletion{constructor(e,t){this.content="",this.contentBuffer=[],this.isCompleted=!1,this.indexOfList=Number(e.length),this.messages=e,this.model=t?.model||"fmode-1.6-cn",this.max_tokens=t?.max_tokens||32768,this.tools=t?.tools}sendCompletion(e={messages:[]}){e.intTime=e?.intTime||50,e.isDirect=e?.isDirect||!1,e?.isDirect&&(e.intTime=1),void 0===e.stream&&(e.stream=!0);const t={messages:this.messages,stream:e.stream,model:this.model,temperature:e?.temperature||.5,presence_penalty:e?.presence_penalty||0,frequency_penalty:e?.frequency_penalty||0};return this.model.indexOf("1.6")>-1&&(t.thinking=t.thinking||{type:"disabled"}),e?.max_tokens&&(t.max_tokens=e?.max_tokens||this.max_tokens),e?.tools?.length&&(t.tools=e?.tools||this.tools),new Observable((n=>{let o=RequestFmodeChatApi("/v1/chat/completions",t).subscribe((t=>{let s,r=String(t);try{s=chunkToJson(r)}catch(e){}if(("data: [DONE]"==r||s?.created&&s?.choices?.[0]?.finish_reason)&&(this.isCompleted=!0,e?.isDirect&&this.isCompleted&&(n.next({role:"assistant",content:this.content,complete:!0,createdAt:new Date}),o.unsubscribe(),e?.onComplete&&e.onComplete({role:"assistant",content:this.content,complete:!0,createdAt:new Date}),n.complete())),r.indexOf("data: {")>-1){let t=s?.choices?.[0]?.delta?.content||"";this.contentBuffer.push(t),e?.isDirect&&(this.content+=t||"",this.isCompleted||n.next({role:"assistant",cid:s?.id,content:this.content,createdAt:new Date})),e?.isDirect||this.contentPusher||(this.contentPusher=setInterval((()=>{this.isCompleted&&0==this.contentBuffer?.length&&(n.next({role:"assistant",cid:s?.id,content:this.content,complete:!0,createdAt:new Date}),o.unsubscribe(),clearInterval(this.contentPusher),n.complete()),this.contentBuffer?.length>=0&&(this.contentBuffer?.length>0&&(this.content+=this.contentBuffer.shift()),n.next({role:"assistant",cid:s?.id,content:this.content,createdAt:new Date}))}),e?.intTime))}}))})).pipe(bufferTime(100),concatMap((e=>e)),delay(200))}}function chunkToJson(e){let t;try{t=JSON.parse(e.replaceAll("data: ",""))}catch(e){}return t||{}}function RequestFmodeChatApi(e,t,n="POST"){return new Observable((o=>{let s=API_BASE+e,r=`Bearer ${localStorage.getItem("FMODE_AI_TOKEN")||Parse.User.current()?.getSessionToken()}`;return t.token=r,t&&(t=JSON.stringify(t)),console.log(s,t),fetch(s,{headers:{"Content-Type":"text/plain; charset=utf-8","Cache-Control":"no-cache"},body:t||null,method:n,credentials:"omit",mode:"cors"}).then((async e=>{console.log(e);let t=null;try{t=await e.json()}catch(e){console.error(e)}console.log(e,t);let n=!t,s="";if(console.log("isStream",n),n){const t=e.body?.getReader();if(!t)return void o.error(new Error("Response body reader is null"));const n=new TextDecoder;let r=new ReadableStream({start(e){!function read(){try{e&&t.read().then((({done:t,value:n})=>{if(t)return e?.close(),void o.complete();e?.enqueue(n),read()})).catch((e=>{}))}catch(e){console.log("err2",e)}}()}}).getReader();r.read().then((function processStream(e){let{done:t,value:c}=e;t||(!function processData(e){let t=(s+e).split("\n");if(t?.length>1){for(let e=0;e<t.length-1;e++){let n=t[e];o.next(n)}s=t[t.length-1]}}(n.decode(c)),r.read().then(processStream))}))}else{console.log("response",e);let n=t;if(console.log("json data",n),n.choices&&n.choices.length>0){const e=n.choices[0],t=e.message?.content||"",s={id:n.id,object:"chat.completion.chunk",created:n.created,model:n.model,choices:[{index:0,delta:{content:t},finish_reason:null}]};o.next("data: "+JSON.stringify(s));const r={id:n.id,object:"chat.completion.chunk",created:n.created,model:n.model,choices:[{index:0,delta:{},finish_reason:e.finish_reason||"stop"}]};o.next("data: "+JSON.stringify(r)),o.next("data: [DONE]")}o.complete()}})).catch((e=>o.error(e))),()=>{}}))}export async function completionJSON(e,t,n,o=1,s){void 0===(s=s||{}).stream&&(s.stream=!1);let r=0;const attemptCompletion=async()=>{const o=`请严格按照以下要求生成JSON数据:\n- 数据结构要求:${t}\n- 返回一个完整的JSON对象,请确保JSON格式完整,不要遗漏任何标点符号\n- 注意返回的JSON格式每个KEY都有""包裹和Value之间都有:\n- 注意""字符串内再出现"符号需要用\\"转义\n`;let c;if(Array.isArray(e))c=e;else if(s.vision&&s.images&&s.images.length>0){c=[{role:"user",content:[...s.images.map((e=>({type:"image_url",image_url:{url:e}}))),{type:"text",text:e+o}]}]}else c=[{role:"user",content:e+o}];s.response_format={type:"json_object"};const i=new FmodeChatCompletion(c,s);let a="";return new Promise(((e,t)=>{const o=i.sendCompletion({isDirect:!0,max_tokens:32768,stream:s.stream}).subscribe({next:c=>{if("string"==typeof c?.content&&(a=c.content,n&&(!1===s.stream?c.complete&&n(a):n(a))),c.complete){try{const t=extractJSON(a);if(!t)throw new Error("未能从响应中提取有效的JSON对象");e(t)}catch(e){console.error(`JSON解析失败 (第${r+1}次尝试):`,e),t(new Error(`生成的响应不符合JSON格式: ${e.message}`))}o.unsubscribe()}},error:e=>{console.error("Completion请求失败:",e),o.unsubscribe(),t(e)}})}))};for(;r<=o;)try{const e=await attemptCompletion();return r>0&&console.log(`JSON生成成功,重试了 ${r} 次`),e}catch(e){if(r++,!(r<=o))throw console.error(`JSON生成失败,已重试 ${o} 次,最终错误:`,e),new Error(`经过 ${o} 次重试后仍无法生成有效JSON: ${e.message}`);console.warn(`第 ${r} 次尝试失败,正在重试... 错误信息:`,e.message),await new Promise((e=>setTimeout(e,1e3)))}}export function extractJSON(e){try{return JSON.parse(e.trim())}catch(e){}let t=0,n=-1,o=null;for(let s=0;s<e.length;s++)if("{"===e[s])0===t&&(n=s),t++;else if("}"===e[s]&&(t--,0===t&&-1!==n))try{const t=e.slice(n,s+1);o=JSON.parse(t);break}catch(e){n=-1}return o||(o=tryFixAndParseJSON(e)),o}function tryFixAndParseJSON(e){const t=e.match(/\{[\s\S]*\}/);if(!t)return null;let n=t[0];const o=[e=>e.replace(/,(\s*[}\]])/g,"$1"),e=>e.replace(/([{,]\s*)([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:/g,'$1"$2":'),e=>e.replace(/'/g,'"'),e=>e.replace(/\/\*[\s\S]*?\*\//g,"").replace(/\/\/.*$/gm,"")];for(const e of o)try{const t=e(n);return JSON.parse(t)}catch(e){}return null}
|
|
9
9
|
var MODULE_PATH_NEED = `6K+l5paH5Lu25piv5pys6aG555uu55qE5LiA6YOo5YiGIFRoaXMgZmlsZSBpcyBwYXJ0IG9mIHRoZSBDb21wb25lbnRzIGluIEZtb2RlIEluYy4KICAgIOeJiOadg+aJgOaciSDCqSDmnKrmnaXpo57pqawgwqkg5rGf6KW/6ISR5o6n56eR5oqA5pyJ6ZmQ5YWs5Y+4IENvcHlyaWdodCDCqSBGbW9kZSBUZWNobm9sb2d5IENvLiwgTHRkLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCBSaWdodHMgUmVzZXJ2ZWQuCiAgICDkuKXnpoHlnKjmnKrnu4/mjojmnYPnmoTmg4XlhrXkuIvvvIzpgJrov4fku7vkvZXlqpLku4vlpI3liLbmraTmlofku7YgVW5hdXRob3JpemVkIGNvcHlpbmcgb2YgdGhpcyBmaWxlLCB2aWEgYW55IG1lZGl1bSBpcyBzdHJpY3RseSBwcm9oaWJpdGVkCiAgICDor6Xmlofku7bmmK/kuJPmnInnmoTmnLrlr4bmlofku7YgUHJvcHJpZXRhcnkgYW5kIGNvbmZpZGVudGlhbAogICAKICAgIENvcHlyaWdodCAyMDIxLW5vdyBGbW9kZSBJbmMuIHN1cHBvcnRAZm1vZGUuY24uIDE4NjA3MDA3MDczLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCByaWdodHMgcmVzZXJ2ZWQuCgogICAgUEFUSDovaG9tZS9yeWFuL3dvcmtzcGFjZS9ub3ZhL25vdmEtYWRtaW4vZGlzdC9mbW9kZS1uZy9lc20yMDIyL2xpYi9jb3JlL2FnZW50L2NoYXQvY29tcGxldGlvbi9mbW9kZS1jb21wbGV0aW9uLm1qcw==`
|
|
10
10
|
|
|
@@ -5,6 +5,6 @@
|
|
|
5
5
|
* 保留所有权利 All Rights Reserved.
|
|
6
6
|
* /home/ryan/workspace/nova/nova-admin/dist/fmode-ng/esm2022/lib/core/parse/fmode.user.mjs
|
|
7
7
|
*/
|
|
8
|
-
import{FmodeObject}from"./fmode.object";export class FmodeUser extends FmodeObject{static{this._instance=null}static{this.instanceKey=()=>{if(!this._instance)throw new Error("FmodeParse not initialized before using User");return"Parse/"+this._instance.appId+"/currentUser"}}static bindInstance(e){this._instance=e}static get instance(){if(!this._instance)throw new Error("FmodeParse not initialized");return this._instance}get username(){return this.get("username")}set username(e){this.set("username",e)}get email(){return this.get("email")}set email(e){this.set("email",e)}get password(){return this.get("password")}set password(e){this.set("password",e)}constructor(e){super("_User"),e&&(this.id=e?.objectId,this.createdAt=new Date(e?.createdAt),this.updatedAt=new Date(e?.updatedAt),e.sessionToken&&(this.sessionToken=e.sessionToken,delete e.sessionToken),this.set(e))}static async logIn(e,s){const t=`${this.instance.serverURL}/login`,n=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json","X-Parse-Application-Id":this.instance.appId},body:JSON.stringify({username:e,password:s})}),
|
|
8
|
+
import{FmodeObject}from"./fmode.object";export class FmodeUser extends FmodeObject{static{this._instance=null}static{this.instanceKey=()=>{if(!this._instance)throw new Error("FmodeParse not initialized before using User");return"Parse/"+this._instance.appId+"/currentUser"}}static bindInstance(e){this._instance=e}static get instance(){if(!this._instance)throw new Error("FmodeParse not initialized");return this._instance}get username(){return this.get("username")}set username(e){this.set("username",e)}get email(){return this.get("email")}set email(e){this.set("email",e)}get password(){return this.get("password")}set password(e){this.set("password",e)}constructor(e){super("_User"),e&&(this.id=e?.objectId,this.createdAt=new Date(e?.createdAt),this.updatedAt=new Date(e?.updatedAt),e.sessionToken&&(this.sessionToken=e.sessionToken,delete e.sessionToken),this.set(e))}static async logIn(e,s){const t=`${this.instance.serverURL}/login`,n=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json","X-Parse-Application-Id":this.instance.appId},body:JSON.stringify({username:e,password:s})}),i=await n.json();if(i.error)throw i;const r=new this({...i,username:e,password:s});return r._saveToCache(),r}static async become(e){const s=`${this.instance.serverURL}/users/me`;let t={"X-Parse-Session-Token":e,"X-Parse-Application-Id":this.instance.appId};const n=await fetch(s,{headers:t}),i=await n.json();if(i.error)throw i.error;const r=new this({...i,sessionToken:e});return r._saveToCache(),r}static async signUp(e,s,t={}){return new this({...t,username:e,password:s}).signUp()}static async logOut(){if(this.current())try{await fetch(`${this.instance.serverURL}/logout`,{method:"POST",headers:{"X-Parse-Session-Token":this.current().sessionToken,"X-Parse-Application-Id":this.instance.appId}})}finally{this._clearCurrentUser()}}static async requestPasswordReset(e){const s=`${this.instance.serverURL}/requestPasswordReset`,t=await fetch(s,{method:"POST",headers:{"Content-Type":"application/json","X-Parse-Application-Id":this.instance.appId},body:JSON.stringify({email:e})}),n=await t.json();if(n.error)throw n}static{this._currentUser=null}static current(){if(this._currentUser)return this._currentUser;try{const e=localStorage.getItem(this.instanceKey());if(!e)return null;const s=JSON.parse(e);if(!s.sessionToken)return null;const t=new this(s);return this._currentUser=t,t.sessionToken&&this._instance&&(this._instance.sessionToken=t.sessionToken),t}catch(e){return console.warn("Failed to restore user from cache",e),null}}static _clearCurrentUser(){this._currentUser=null,this._instance&&(this._instance.sessionToken=void 0),localStorage.removeItem(this.instanceKey())}async signUp(e){if(e&&this.set(e),!this.username)throw new Error("Username is required");if(!this.password)throw new Error("Password is required");const s=this._parseInstance||FmodeUser.instance;let t=this.toJSON();delete t.__type,delete t.className;const n=`${s.serverURL}/users`,i=await fetch(n,{method:"POST",headers:{"Content-Type":"application/json","X-Parse-Application-Id":s.appId},body:JSON.stringify(t)}),r=await i.json();if(r.error)throw r;return this.id=r.objectId,this.createdAt=new Date(r.createdAt),this.sessionToken=r.sessionToken,this._saveToCache(),this}async save(){const e=this._parseInstance||FmodeUser.instance,s=`${e.serverURL}/users/${this.id||this.data.objectId}`,t={"Content-Type":"application/json","X-Parse-Application-Id":e.appId,"X-Parse-Session-Token":this.sessionToken},n=this.toJSON();delete n.__type,delete n.objectId,delete n.className,delete n.sessionToken,delete n.username,delete n.createdAt,delete n.updatedAt;const i=await fetch(s,{method:"PUT",headers:t,body:JSON.stringify(n)}),r=await i.json();if(r.error)throw 209===r.code&&FmodeUser._clearCurrentUser(),new Error(r.error);return this.set("updatedAt",new Date(r.updatedAt)),this._saveToCache(),this}_saveToCache(){if(!this.sessionToken)return;FmodeUser._currentUser=this;const e=this._parseInstance||FmodeUser._instance;e&&(e.sessionToken=this.sessionToken),localStorage.setItem(FmodeUser.instanceKey(),JSON.stringify(this.toJSONForCache()))}toJSONForCache(){return{...super.toJSON(),sessionToken:this.sessionToken}}getSessionToken(){return this.sessionToken}static async getMobileCode(e,s){const t=`${this.instance.serverURL.split("parse")[0]}api/apig/message`,n=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json","X-Parse-Application-Id":this.instance.appId},body:JSON.stringify({company:s,mobile:e})}),i=await n.json();if(i.error)throw console.error(i),i;return i}static async loginMobile(e,s,t){const n=`${this.instance.serverURL.split("parse")[0]}api/auth/mobile?company=${t}&mobile=${e}&code=${s}`,i=await fetch(n,{method:"GET",headers:{"X-Parse-Application-Id":this.instance.appId}}),r=await i.json();if(console.log(r),200!==r.code||!r.data?.token)throw r;return await this.become(r.data.token)}static async getMobileUsername(e,s){const t=`${this.instance.serverURL.split("parse")[0]}api/auth/getusername`,n=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json","X-Parse-Application-Id":this.instance.appId},body:JSON.stringify({company:s,mobile:e})}),i=await n.json();return i?.username}static async loginMobilePassword(e,s,t){const n=await this.getMobileUsername(e,t);if(!n)throw new Error("用户不存在,请尝试其他登录方式");return await this.logIn(n,s)}static async bindInvite(e,s,t){if(!e||!s)return;if(e===s)return;const n=`${this.instance.serverURL.split("parse")[0]}api/auth/invite`,i=await fetch(n,{method:"POST",headers:{"Content-Type":"application/json","X-Parse-Application-Id":this.instance.appId},body:JSON.stringify({current:e,invite:s,type:"code"})}),r=await i.json();if(r.error)throw r;return r}async destroy(e){if(!this.id)throw new Error("Cannot destroy unsaved user");const s=this._parseInstance||FmodeUser.instance,t=`${s.serverURL}/users/${this.id}`,n={"X-Parse-Session-Token":this.sessionToken,"X-Parse-Application-Id":s.appId};e?.useMasterKey&&s.config.masterKey&&(n["X-Parse-Master-Key"]=s.config.masterKey);const i=await fetch(t,{method:"DELETE",headers:n});if(!i.ok){const e=await i.json();throw new Error(e.error||"Failed to delete user")}FmodeUser._clearCurrentUser()}}
|
|
9
9
|
var MODULE_PATH_NEED = `6K+l5paH5Lu25piv5pys6aG555uu55qE5LiA6YOo5YiGIFRoaXMgZmlsZSBpcyBwYXJ0IG9mIHRoZSBDb21wb25lbnRzIGluIEZtb2RlIEluYy4KICAgIOeJiOadg+aJgOaciSDCqSDmnKrmnaXpo57pqawgwqkg5rGf6KW/6ISR5o6n56eR5oqA5pyJ6ZmQ5YWs5Y+4IENvcHlyaWdodCDCqSBGbW9kZSBUZWNobm9sb2d5IENvLiwgTHRkLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCBSaWdodHMgUmVzZXJ2ZWQuCiAgICDkuKXnpoHlnKjmnKrnu4/mjojmnYPnmoTmg4XlhrXkuIvvvIzpgJrov4fku7vkvZXlqpLku4vlpI3liLbmraTmlofku7YgVW5hdXRob3JpemVkIGNvcHlpbmcgb2YgdGhpcyBmaWxlLCB2aWEgYW55IG1lZGl1bSBpcyBzdHJpY3RseSBwcm9oaWJpdGVkCiAgICDor6Xmlofku7bmmK/kuJPmnInnmoTmnLrlr4bmlofku7YgUHJvcHJpZXRhcnkgYW5kIGNvbmZpZGVudGlhbAogICAKICAgIENvcHlyaWdodCAyMDIxLW5vdyBGbW9kZSBJbmMuIHN1cHBvcnRAZm1vZGUuY24uIDE4NjA3MDA3MDczLgogICAg5L+d55WZ5omA5pyJ5p2D5YipIEFsbCByaWdodHMgcmVzZXJ2ZWQuCgogICAgUEFUSDovaG9tZS9yeWFuL3dvcmtzcGFjZS9ub3ZhL25vdmEtYWRtaW4vZGlzdC9mbW9kZS1uZy9lc20yMDIyL2xpYi9jb3JlL3BhcnNlL2Ztb2RlLnVzZXIubWpz`
|
|
10
10
|
|