@storvia/sdk 0.1.1 → 0.1.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.js +2 -1
- package/dist/index.esm.js +2 -1
- package/dist/index.umd.js +2 -1
- package/package.json +1 -1
package/dist/index.cjs.js
CHANGED
|
@@ -427,4 +427,5 @@
|
|
|
427
427
|
to { transform: rotate(360deg); }
|
|
428
428
|
}
|
|
429
429
|
.spin { animation: spin 0.8s linear infinite; }
|
|
430
|
-
`,D='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg>',j='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="23 4 23 10 17 10"/><polyline points="1 20 1 14 7 14"/><path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"/></svg>',z='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="spin"><path d="M21 12a9 9 0 1 1-6.219-8.56"/></svg>',G='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>',F='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg>',v=class{constructor(e){this.selectedModelId="";this.models=[];this.modelPanel=null;this.settings={temperature:1,reply_length:"medium",reply_language:"zh-CN",player_note:""};this.hasWorldUpdate=!1;this.fabDot=null;this.syncHint=null;this.displayMode="phone";this.displayModeBtns=[];this.tempValueEl=null;this.tempSlider=null;this.segmentBtns=[];this.langSelect=null;this.noteInput=null;this.noteCounter=null;this.saveBtn=null;this.syncBtn=null;this.syncState="idle";this.panel=null;this.callbacks=e,this.host=document.createElement("div"),this.host.id="storvia-settings",this.shadowRoot=this.host.attachShadow({mode:"closed"})}mount(e,t,n,s=!1){this.models=e,this.selectedModelId=t,this.settings={...n},this.hasWorldUpdate=s;try{let i=localStorage.getItem("storvia_display_mode");(i==="phone"||i==="desktop")&&(this.displayMode=i)}catch{}document.body.appendChild(this.host),this.render()}unmount(){this.host.remove()}getSelectedModelId(){return this.selectedModelId}getSettings(){return{...this.settings}}render(){let e=document.createElement("style");e.textContent=U;let t=document.createElement("div"),n=document.createElement("div");n.className="fab-wrap";let s=document.createElement("button");s.className="fab",s.innerHTML=D,s.title="\u6E38\u620F\u8BBE\u7F6E";let i=document.createElement("div");i.className=`fab-dot${this.hasWorldUpdate?" visible":""}`,this.fabDot=i,n.appendChild(s),n.appendChild(i);let a=document.createElement("div");a.className="panel",this.panel=a;let o=document.createElement("div");o.className="panel-header";let d=document.createElement("div");d.className="panel-title",d.textContent="\u6E38\u620F\u8BBE\u7F6E";let p=document.createElement("button");p.className="close-btn",p.innerHTML="\u2715",p.addEventListener("click",()=>a.classList.remove("open")),o.appendChild(d),o.appendChild(p),a.appendChild(o);let r=document.createElement("div");r.className="panel-body",this.models.length>0&&(r.appendChild(this.buildModelSection()),r.appendChild(this.makeDivider())),r.appendChild(this.buildTemperatureSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildReplyLengthSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildReplyLanguageSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildNoteSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildDisplayModeSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildSyncSection());let m=document.createElement("div");m.className="save-row";let h=document.createElement("button");h.className="save-btn",h.textContent="\u4FDD\u5B58",this.saveBtn=h,h.addEventListener("click",()=>this.handleSave()),m.appendChild(h),r.appendChild(m),a.appendChild(r),s.addEventListener("click",()=>a.classList.toggle("open")),document.addEventListener("click",u=>{this.host.contains(u.target)||a.classList.remove("open")}),t.appendChild(a),t.appendChild(n),this.shadowRoot.appendChild(e),this.shadowRoot.appendChild(t)}buildModelSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="AI \u6A21\u578B",e.appendChild(t);let n=document.createElement("div");return n.className="model-list",this.modelPanel=n,this.models.forEach(s=>{let i=document.createElement("button");i.className=`model-option${s.id===this.selectedModelId?" active":""}`,i.innerHTML=`<span class="dot"></span><span>${s.name}</span>`,i.addEventListener("click",()=>{this.selectedModelId=s.id,this.callbacks.onModelChange(s.id);try{localStorage.setItem("storvia_model_id",s.id)}catch{}this.updateModelActive()}),n.appendChild(i)}),e.appendChild(n),e}buildTemperatureSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label";let n=document.createElement("span");n.textContent="\u6E29\u5EA6";let s=document.createElement("span");s.className="value",s.textContent=this.settings.temperature.toFixed(1),this.tempValueEl=s,t.appendChild(n),t.appendChild(s),e.appendChild(t);let i=document.createElement("div");i.className="section-desc",i.textContent="\u66F4\u4F4E\u7684\u6E29\u5EA6\u4F1A\u8BA9 AI \u56DE\u590D\u66F4\u7A33\u5B9A\u3001\u7406\u667A\uFF1B\u66F4\u9AD8\u7684\u6E29\u5EA6\u4F1A\u8BA9 AI \u7684\u56DE\u590D\u66F4\u5177\u591A\u6837\u6027\u548C\u968F\u673A\u6027",e.appendChild(i);let a=document.createElement("div");a.className="slider-wrap";let o=document.createElement("input");o.type="range",o.className="slider",o.min="0",o.max="2",o.step="0.1",o.value=String(this.settings.temperature),this.tempSlider=o,o.addEventListener("input",()=>{let p=parseFloat(o.value);this.settings.temperature=p,this.tempValueEl&&(this.tempValueEl.textContent=p.toFixed(1))}),a.appendChild(o);let d=document.createElement("div");return d.className="slider-labels",d.innerHTML="<span>\u7CBE\u786E</span><span>\u521B\u610F</span>",a.appendChild(d),e.appendChild(a),e}buildReplyLengthSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u56DE\u590D\u957F\u5EA6",e.appendChild(t);let n=document.createElement("div");return n.className="segment",this.segmentBtns=[],N.forEach(s=>{let i=document.createElement("button");i.className=`segment-btn${s.value===this.settings.reply_length?" active":""}`,i.textContent=s.label,i.addEventListener("click",()=>{this.settings.reply_length=s.value,this.segmentBtns.forEach((a,o)=>{a.classList.toggle("active",N[o].value===s.value)})}),this.segmentBtns.push(i),n.appendChild(i)}),e.appendChild(n),e}buildReplyLanguageSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u56DE\u590D\u8BED\u8A00",e.appendChild(t);let n=document.createElement("div");n.className="select-wrap";let s=document.createElement("select");s.className="select",this.langSelect=s,A.forEach(a=>{let o=document.createElement("option");o.value=a.value,o.textContent=a.label,a.value===this.settings.reply_language&&(o.selected=!0),s.appendChild(o)}),s.addEventListener("change",()=>{this.settings.reply_language=s.value});let i=document.createElement("span");return i.className="select-arrow",i.textContent="\u25BE",n.appendChild(s),n.appendChild(i),e.appendChild(n),e}buildNoteSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label";let n=document.createElement("span");n.textContent="\u5907\u6CE8";let s=document.createElement("span");s.textContent=`${this.settings.player_note.length}/50`,this.noteCounter=s,t.appendChild(n),t.appendChild(s),e.appendChild(t);let i=document.createElement("div");i.className="section-desc",i.textContent="\u5199\u4E0B\u4F60\u5BF9\u8FD9\u4E2A\u6E38\u620F\u7684\u5907\u6CE8\uFF0C\u4F1A\u663E\u793A\u5728\u5B58\u6863\u5217\u8868",e.appendChild(i);let a=document.createElement("div");a.className="note-wrap";let o=document.createElement("textarea");return o.className="note-input",o.placeholder="\u5907\u6CE8",o.maxLength=50,o.value=this.settings.player_note,this.noteInput=o,o.addEventListener("input",()=>{this.settings.player_note=o.value,this.noteCounter&&(this.noteCounter.textContent=`${o.value.length}/50`)}),a.appendChild(o),e.appendChild(a),e}buildDisplayModeSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u663E\u793A\u6A21\u5F0F",e.appendChild(t);let n=document.createElement("div");n.className="section-desc",n.textContent="\u7535\u8111\u6A21\u5F0F\u4F1A\u81EA\u52A8\u6A2A\u5C4F\u663E\u793A",e.appendChild(n);let s=document.createElement("div");s.className="segment",this.displayModeBtns=[];let i=[{value:"phone",label:"\u624B\u673A"},{value:"desktop",label:"\u7535\u8111"}];return i.forEach(a=>{let o=document.createElement("button");o.className=`segment-btn${a.value===this.displayMode?" active":""}`,o.textContent=a.label,o.addEventListener("click",()=>{this.displayMode=a.value;try{localStorage.setItem("storvia_display_mode",a.value)}catch{}this.displayModeBtns.forEach((d,p)=>{d.classList.toggle("active",i[p].value===a.value)});try{window.parent.postMessage({type:"storvia-orientation",mode:a.value==="desktop"?"landscape":"portrait"},"*")}catch{}}),this.displayModeBtns.push(o),s.appendChild(o)}),e.appendChild(s),e}buildSyncSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u540C\u6B65\u6E38\u620F\u8BBE\u5B9A",e.appendChild(t);let n=document.createElement("div");n.className=`sync-hint${this.hasWorldUpdate?" visible":""}`,n.textContent="\u26A0 \u4F5C\u8005\u5DF2\u66F4\u65B0\u6E38\u620F\u5185\u5BB9\uFF0C\u5EFA\u8BAE\u70B9\u51FB\u540C\u6B65",this.syncHint=n,e.appendChild(n);let s=document.createElement("div");s.className="sync-desc",s.textContent="\u5982\u679C\u6E38\u620F\u4F5C\u8005\u66F4\u65B0\u4E86\u5185\u5BB9\uFF0C\u70B9\u51FB\u6B64\u6309\u94AE\u53EF\u5C06\u6700\u65B0\u7684\u6E38\u620F\u8BBE\u5B9A\u548C\u5F00\u5E55\u5C0F\u5267\u573A\u540C\u6B65\u5230\u5F53\u524D\u5BF9\u8BDD",e.appendChild(s);let i=document.createElement("button");return i.className="sync-btn",this.syncBtn=i,this.renderSyncBtn(),i.addEventListener("click",async()=>{if(this.syncState!=="loading"){this.setSyncState("loading");try{await this.callbacks.onSync(),this.setSyncState("success"),this.hasWorldUpdate=!1,this.fabDot&&this.fabDot.classList.remove("visible"),this.syncHint&&this.syncHint.classList.remove("visible"),setTimeout(()=>this.setSyncState("idle"),3e3)}catch{this.setSyncState("error"),setTimeout(()=>this.setSyncState("idle"),4e3)}}}),e.appendChild(i),e}makeDivider(){let e=document.createElement("div");return e.className="divider",e}updateModelActive(){this.modelPanel&&this.modelPanel.querySelectorAll(".model-option").forEach((e,t)=>{e.classList.toggle("active",this.models[t].id===this.selectedModelId)})}setSyncState(e){this.syncState=e,this.renderSyncBtn()}renderSyncBtn(){if(!this.syncBtn)return;let e=this.syncState,t={idle:j,loading:z,success:G,error:F},n={idle:"\u540C\u6B65\u6700\u65B0\u8BBE\u5B9A",loading:"\u540C\u6B65\u4E2D...",success:"\u5DF2\u540C\u6B65",error:"\u540C\u6B65\u5931\u8D25"};this.syncBtn.className=`sync-btn${e==="success"||e==="error"?` ${e}`:""}`,this.syncBtn.disabled=e==="loading",this.syncBtn.innerHTML=`${t[e]}<span>${n[e]}</span>`}async handleSave(){if(!(!this.saveBtn||this.saveBtn.disabled)){this.saveBtn.disabled=!0,this.saveBtn.textContent="\u4FDD\u5B58\u4E2D...";try{await this.callbacks.onSettingsChange({temperature:this.settings.temperature,reply_length:this.settings.reply_length,reply_language:this.settings.reply_language,player_note:this.settings.player_note}),this.saveBtn.textContent="\u5DF2\u4FDD\u5B58",this.saveBtn.classList.add("saved"),setTimeout(()=>{this.saveBtn&&(this.saveBtn.textContent="\u4FDD\u5B58",this.saveBtn.classList.remove("saved"),this.saveBtn.disabled=!1)},2e3)}catch{this.saveBtn.textContent="\u4FDD\u5B58\u5931\u8D25",setTimeout(()=>{this.saveBtn&&(this.saveBtn.textContent="\u4FDD\u5B58",this.saveBtn.disabled=!1)},2e3)}}}};var f=class{constructor(e,t,n,s,i=!1,a){this.http=e;this.getGameId=t;this.getConversationId=n;this.getModelId=s;this.isPreview=i;this.previewConfig=a}updatePreviewConfig(e){this.previewConfig=e}async send(e){if(this.isPreview)return this.sendPreview(e);let t={game_id:this.getGameId(),conversation_id:this.getConversationId(),model_id:this.getModelId(),message:e.message,topic:e.topic,context:e.context,stream:e.stream??!1};if(e.stream){await this.http.stream("/chat",t,e.onChunk??(()=>{}),e.onDone??(()=>{}));return}return this.http.post("/chat",t)}async save(e){return this.http.post("/messages/save",{conversation_id:this.getConversationId(),role:e.role,content:e.content,topic:e.topic})}async generate(e){let t={conversation_id:this.getConversationId(),model_id:this.getModelId(),topic:e.topic,context:e.context,stream:e.stream??!1,save:e.save??!0};if(e.stream){await this.http.stream("/generate",t,e.onChunk??(()=>{}),e.onDone??(()=>{}));return}return this.http.post("/generate",t)}async sendPreview(e){let t={model_id:this.getModelId(),message:e.message,topic:e.topic,context:e.context,prompt_config:this.previewConfig||{},history_messages:[]};if(e.stream){await this.http.stream("/preview",t,e.onChunk??(()=>{}),e.onDone??(()=>{}));return}let n="";return await this.http.stream("/preview",t,s=>{n+=s},()=>{}),{id:"",content:n}}};var R=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(e){return this.isPreview?{...this.mem[e]||{}}:this.http.get(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}/state`)}async update(e,t){if(this.isPreview){this.mem[e]={...this.mem[e]||{},...t};return}await this.http.patch(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}/state`,t)}},b=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem=[];this.state=new R(e,t,n)}async list(){return this.isPreview?this.mem.map(e=>({name:e.name,avatar:e.avatar})):this.http.get(`/conversations/${this.getConversationId()}/characters`)}async get(e){if(this.isPreview){let t=this.mem.find(n=>n.name===e);return{name:t?.name||e,avatar:t?.avatar}}return this.http.get(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}`)}async add(e){if(this.isPreview){this.mem.push(e);return}await this.http.post(`/conversations/${this.getConversationId()}/characters`,e)}async update(e,t){if(this.isPreview){let n=this.mem.find(s=>s.name===e);n&&Object.assign(n,t);return}await this.http.patch(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}`,t)}async remove(e){if(this.isPreview){this.mem=this.mem.filter(t=>t.name!==e);return}await this.http.delete(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}`)}async detail(e){if(this.isPreview){let t=this.mem.find(n=>n.name===e);return{name:e,avatar:t?.avatar,state:await this.state.get(e)}}return this.http.get(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}/detail`)}};var w=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem=[]}async list(){return this.isPreview?[...this.mem]:this.http.get(`/conversations/${this.getConversationId()}/relationships`)}async get(e,t){if(this.isPreview){let n=this.mem.find(s=>s.from===e&&s.to===t);if(n)return{...n};throw new Error("Relationship not found")}return this.http.get(`/conversations/${this.getConversationId()}/relationships/${encodeURIComponent(e)}/${encodeURIComponent(t)}`)}async add(e){if(this.isPreview){this.mem.push({...e});return}await this.http.post(`/conversations/${this.getConversationId()}/relationships`,e)}async update(e,t,n){if(this.isPreview){let s=this.mem.find(i=>i.from===e&&i.to===t);s?Object.assign(s,n):this.mem.push({from:e,to:t,...n});return}await this.http.patch(`/conversations/${this.getConversationId()}/relationships/${encodeURIComponent(e)}/${encodeURIComponent(t)}`,n)}async remove(e,t){if(this.isPreview){this.mem=this.mem.filter(n=>!(n.from===e&&n.to===t));return}await this.http.delete(`/conversations/${this.getConversationId()}/relationships/${encodeURIComponent(e)}/${encodeURIComponent(t)}`)}};var y=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem=[]}async list(e){if(this.isPreview)return e?this.mem.filter(n=>n.group===e):[...this.mem];let t=e?{group:e}:void 0;return this.http.get(`/conversations/${this.getConversationId()}/inventory`,t)}async add(e){if(this.isPreview){this.mem.push({...e});return}await this.http.post(`/conversations/${this.getConversationId()}/inventory`,e)}async update(e,t){if(this.isPreview){let n=this.mem.find(s=>s.name===e);n&&Object.assign(n,t);return}await this.http.patch(`/conversations/${this.getConversationId()}/inventory/${encodeURIComponent(e)}`,t)}async remove(e){if(this.isPreview){this.mem=this.mem.filter(t=>t.name!==e);return}await this.http.delete(`/conversations/${this.getConversationId()}/inventory/${encodeURIComponent(e)}`)}};var C=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(){return this.isPreview?{...this.mem}:this.http.get(`/conversations/${this.getConversationId()}/world`)}async update(e){if(this.isPreview){Object.assign(this.mem,e);return}await this.http.patch(`/conversations/${this.getConversationId()}/world`,e)}};var T=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(e){if(this.isPreview)return e?{...this.mem[e]||{}}:{...this.mem};let t=e?{group:e}:void 0;return this.http.get(`/conversations/${this.getConversationId()}/player/state`,t)}async update(e,t){if(this.isPreview){this.mem[e]={...this.mem[e]||{},...t};return}await this.http.patch(`/conversations/${this.getConversationId()}/player/state`,{group:e,values:t})}},x=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.memPersona={name:""};this.state=new T(e,t,n)}async get(){return this.isPreview?{...this.memPersona}:this.http.get(`/conversations/${this.getConversationId()}/player`)}};var I=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(e){if(this.isPreview)return e!==void 0?this.mem[e]:{...this.mem};let t=await this.http.get(`/conversations/${this.getConversationId()}/custom`);return e!==void 0?t[e]:t}async set(e){if(this.isPreview){for(let[t,n]of Object.entries(e))typeof n=="object"&&n!==null&&!Array.isArray(n)&&typeof this.mem[t]=="object"&&this.mem[t]!==null?this.mem[t]={...this.mem[t],...n}:this.mem[t]=n;return}await this.http.patch(`/conversations/${this.getConversationId()}/custom`,e)}async delete(e){if(this.isPreview){delete this.mem[e];return}await this.http.delete(`/conversations/${this.getConversationId()}/custom/${encodeURIComponent(e)}`)}async setGroupField(e,t,n){if(this.isPreview){(!this.mem[e]||typeof this.mem[e]!="object")&&(this.mem[e]={}),this.mem[e][t]=n;return}await this.http.patch(`/conversations/${this.getConversationId()}/custom/${encodeURIComponent(e)}/${encodeURIComponent(t)}`,{value:n})}async deleteGroupField(e,t){if(this.isPreview){if(typeof this.mem[e]=="object"&&this.mem[e]!==null){let n=this.mem[e];delete n[t],Object.keys(n).length===0&&delete this.mem[e]}return}await this.http.delete(`/conversations/${this.getConversationId()}/custom/${encodeURIComponent(e)}/${encodeURIComponent(t)}`)}};var V="/functions/v1/world-engine/v1";function W(){return window.__STORVIA__||{}}function J(l=5e3){let e=W();return e.apiBase?Promise.resolve(e):new Promise((t,n)=>{let s=setTimeout(()=>{window.removeEventListener("message",i),n(new c("NOT_INITIALIZED","Timed out waiting for __STORVIA__ config from parent frame."))},l);function i(a){let o=null;if(a.data?.type==="storvia-init"&&a.data.apiBase?o={apiBase:a.data.apiBase,gameId:a.data.gameId,accessToken:a.data.accessToken}:a.data?.type==="storvia-preview-init"&&a.data.config&&(o=a.data.config),o&&o.apiBase){clearTimeout(s),window.removeEventListener("message",i);let d=window;d.__STORVIA__=o,t(o)}}window.addEventListener("message",i)})}var k=class{constructor(){this.baseUrl="";this.gameId="";this.conversationId="";this.modelId="";this.accessToken="";this.initialized=!1;this.isPreview=!1;this.previewConfig=void 0}async init(){let e=await J(),t=e.apiBase;if(!t)throw new c("NOT_INITIALIZED","window.__STORVIA__.apiBase is not set. The platform must inject it before SDK init.");if(this.baseUrl=`${t}${V}`,this.isPreview=!!e.preview,this.previewConfig=e.previewConfig,!e.gameId)throw new c("GAME_NOT_FOUND","window.__STORVIA__.gameId is not set. The platform must inject it before SDK init.");if(this.gameId=e.gameId,!e.accessToken)throw new c("UNAUTHORIZED","window.__STORVIA__.accessToken is not set. The platform must inject the user token before SDK init.");if(this.accessToken=e.accessToken,this.http=new S(this.baseUrl,this.accessToken,!!e.debug),this.isPreview){this.conversationId="00000000-0000-0000-0000-000000000000";let r=e.models||[];this.modelId=e.selectedModelId||r[0]?.id||"",this.settingsSheet=new v({onModelChange:E=>{this.modelId=E},onSettingsChange:()=>Promise.resolve(),onSync:()=>Promise.resolve()}),r.length>0&&this.settingsSheet.mount(r,this.modelId,{temperature:1,reply_length:"medium",reply_language:"zh-CN",player_note:""},!1);let m=()=>this.gameId,h=()=>this.conversationId,u=()=>this.modelId;this.chat=new f(this.http,m,h,u,!0,this.previewConfig),this.characters=new b(this.http,h,!0),this.relationships=new w(this.http,h,!0),this.inventory=new y(this.http,h,!0),this.world=new C(this.http,h,!0),this.player=new x(this.http,h,!0),this.custom=new I(this.http,h,!0),this.initialized=!0,this.listenForPreviewMessages();return}let n=await this.authenticate();this.conversationId=n.conversationId;let s=n.models||[],i=this.getSavedModelId();this.modelId=i&&s.some(r=>r.id===i)?i:s[0]?.id||"";let a=n.settings||{temperature:1,reply_length:"medium",reply_language:"zh-CN",player_note:""};this.settingsSheet=new v({onModelChange:r=>{this.modelId=r},onSettingsChange:r=>this.saveSettings(r),onSync:()=>this.syncSettings()}),this.settingsSheet.mount(s,this.modelId,a,n.hasWorldUpdate??!1);let o=()=>this.gameId,d=()=>this.conversationId,p=()=>this.modelId;this.chat=new f(this.http,o,d,p),this.characters=new b(this.http,d),this.relationships=new w(this.http,d),this.inventory=new y(this.http,d),this.world=new C(this.http,d),this.player=new x(this.http,d),this.custom=new I(this.http,d),this.initialized=!0}listenForPreviewMessages(){window.addEventListener("message",e=>{!e.data||e.data.type!=="storvia-preview"||(e.data.action==="set-model"&&(this.modelId=e.data.modelId||""),e.data.action==="update-config"&&(this.previewConfig=e.data.previewConfig,this.chat&&this.chat.updatePreviewConfig(this.previewConfig)))})}async saveSettings(e){await this.http.patch("/settings",{conversation_id:this.conversationId,...e})}async syncSettings(){await this.http.post("/sync",{conversation_id:this.conversationId})}async authenticate(){let e=await fetch(`${this.baseUrl}/auth`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.accessToken}`},body:JSON.stringify({game_id:this.gameId})});if(!e.ok)throw e.status===401?new c("UNAUTHORIZED","User not authenticated"):e.status===404?new c("GAME_NOT_FOUND","Game not found or not published"):new c("INTERNAL_ERROR",`Auth failed: HTTP ${e.status}`);let t=await e.json();return t.data??t}getSavedModelId(){try{return localStorage.getItem("storvia_model_id")||""}catch{return""}}};0&&(module.exports={StorviaError,StorviaSDK});
|
|
430
|
+
`,D='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg>',j='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="23 4 23 10 17 10"/><polyline points="1 20 1 14 7 14"/><path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"/></svg>',z='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="spin"><path d="M21 12a9 9 0 1 1-6.219-8.56"/></svg>',G='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>',F='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg>',v=class{constructor(e){this.selectedModelId="";this.models=[];this.modelPanel=null;this.settings={temperature:1,reply_length:"medium",reply_language:"zh-CN",player_note:""};this.hasWorldUpdate=!1;this.fabDot=null;this.syncHint=null;this.displayMode="phone";this.displayModeBtns=[];this.tempValueEl=null;this.tempSlider=null;this.segmentBtns=[];this.langSelect=null;this.noteInput=null;this.noteCounter=null;this.saveBtn=null;this.syncBtn=null;this.syncState="idle";this.panel=null;this.callbacks=e,this.host=document.createElement("div"),this.host.id="storvia-settings",this.shadowRoot=this.host.attachShadow({mode:"closed"})}mount(e,t,n,s=!1){this.models=e,this.selectedModelId=t,this.settings={...n},this.hasWorldUpdate=s;try{let i=localStorage.getItem("storvia_display_mode");(i==="phone"||i==="desktop")&&(this.displayMode=i)}catch{}document.body.appendChild(this.host),this.render()}unmount(){this.host.remove()}getSelectedModelId(){return this.selectedModelId}getSettings(){return{...this.settings}}render(){let e=document.createElement("style");e.textContent=U;let t=document.createElement("div"),n=document.createElement("div");n.className="fab-wrap";let s=document.createElement("button");s.className="fab",s.innerHTML=D,s.title="\u6E38\u620F\u8BBE\u7F6E";let i=document.createElement("div");i.className=`fab-dot${this.hasWorldUpdate?" visible":""}`,this.fabDot=i,n.appendChild(s),n.appendChild(i);let a=document.createElement("div");a.className="panel",this.panel=a;let o=document.createElement("div");o.className="panel-header";let d=document.createElement("div");d.className="panel-title",d.textContent="\u6E38\u620F\u8BBE\u7F6E";let p=document.createElement("button");p.className="close-btn",p.innerHTML="\u2715",p.addEventListener("click",()=>a.classList.remove("open")),o.appendChild(d),o.appendChild(p),a.appendChild(o);let r=document.createElement("div");r.className="panel-body",this.models.length>0&&(r.appendChild(this.buildModelSection()),r.appendChild(this.makeDivider())),r.appendChild(this.buildTemperatureSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildReplyLengthSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildReplyLanguageSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildNoteSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildDisplayModeSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildSyncSection());let m=document.createElement("div");m.className="save-row";let h=document.createElement("button");h.className="save-btn",h.textContent="\u4FDD\u5B58",this.saveBtn=h,h.addEventListener("click",()=>this.handleSave()),m.appendChild(h),r.appendChild(m),a.appendChild(r),s.addEventListener("click",()=>a.classList.toggle("open")),document.addEventListener("click",u=>{this.host.contains(u.target)||a.classList.remove("open")}),t.appendChild(a),t.appendChild(n),this.shadowRoot.appendChild(e),this.shadowRoot.appendChild(t)}buildModelSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="AI \u6A21\u578B",e.appendChild(t);let n=document.createElement("div");return n.className="model-list",this.modelPanel=n,this.models.forEach(s=>{let i=document.createElement("button");i.className=`model-option${s.id===this.selectedModelId?" active":""}`,i.innerHTML=`<span class="dot"></span><span>${s.name}</span>`,i.addEventListener("click",()=>{this.selectedModelId=s.id,this.callbacks.onModelChange(s.id);try{localStorage.setItem("storvia_model_id",s.id)}catch{}this.updateModelActive()}),n.appendChild(i)}),e.appendChild(n),e}buildTemperatureSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label";let n=document.createElement("span");n.textContent="\u6E29\u5EA6";let s=document.createElement("span");s.className="value",s.textContent=this.settings.temperature.toFixed(1),this.tempValueEl=s,t.appendChild(n),t.appendChild(s),e.appendChild(t);let i=document.createElement("div");i.className="section-desc",i.textContent="\u66F4\u4F4E\u7684\u6E29\u5EA6\u4F1A\u8BA9 AI \u56DE\u590D\u66F4\u7A33\u5B9A\u3001\u7406\u667A\uFF1B\u66F4\u9AD8\u7684\u6E29\u5EA6\u4F1A\u8BA9 AI \u7684\u56DE\u590D\u66F4\u5177\u591A\u6837\u6027\u548C\u968F\u673A\u6027",e.appendChild(i);let a=document.createElement("div");a.className="slider-wrap";let o=document.createElement("input");o.type="range",o.className="slider",o.min="0",o.max="2",o.step="0.1",o.value=String(this.settings.temperature),this.tempSlider=o,o.addEventListener("input",()=>{let p=parseFloat(o.value);this.settings.temperature=p,this.tempValueEl&&(this.tempValueEl.textContent=p.toFixed(1))}),a.appendChild(o);let d=document.createElement("div");return d.className="slider-labels",d.innerHTML="<span>\u7CBE\u786E</span><span>\u521B\u610F</span>",a.appendChild(d),e.appendChild(a),e}buildReplyLengthSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u56DE\u590D\u957F\u5EA6",e.appendChild(t);let n=document.createElement("div");return n.className="segment",this.segmentBtns=[],N.forEach(s=>{let i=document.createElement("button");i.className=`segment-btn${s.value===this.settings.reply_length?" active":""}`,i.textContent=s.label,i.addEventListener("click",()=>{this.settings.reply_length=s.value,this.segmentBtns.forEach((a,o)=>{a.classList.toggle("active",N[o].value===s.value)})}),this.segmentBtns.push(i),n.appendChild(i)}),e.appendChild(n),e}buildReplyLanguageSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u56DE\u590D\u8BED\u8A00",e.appendChild(t);let n=document.createElement("div");n.className="select-wrap";let s=document.createElement("select");s.className="select",this.langSelect=s,A.forEach(a=>{let o=document.createElement("option");o.value=a.value,o.textContent=a.label,a.value===this.settings.reply_language&&(o.selected=!0),s.appendChild(o)}),s.addEventListener("change",()=>{this.settings.reply_language=s.value});let i=document.createElement("span");return i.className="select-arrow",i.textContent="\u25BE",n.appendChild(s),n.appendChild(i),e.appendChild(n),e}buildNoteSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label";let n=document.createElement("span");n.textContent="\u5907\u6CE8";let s=document.createElement("span");s.textContent=`${this.settings.player_note.length}/50`,this.noteCounter=s,t.appendChild(n),t.appendChild(s),e.appendChild(t);let i=document.createElement("div");i.className="section-desc",i.textContent="\u5199\u4E0B\u4F60\u5BF9\u8FD9\u4E2A\u6E38\u620F\u7684\u5907\u6CE8\uFF0C\u4F1A\u663E\u793A\u5728\u5B58\u6863\u5217\u8868",e.appendChild(i);let a=document.createElement("div");a.className="note-wrap";let o=document.createElement("textarea");return o.className="note-input",o.placeholder="\u5907\u6CE8",o.maxLength=50,o.value=this.settings.player_note,this.noteInput=o,o.addEventListener("input",()=>{this.settings.player_note=o.value,this.noteCounter&&(this.noteCounter.textContent=`${o.value.length}/50`)}),a.appendChild(o),e.appendChild(a),e}buildDisplayModeSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u663E\u793A\u6A21\u5F0F",e.appendChild(t);let n=document.createElement("div");n.className="section-desc",n.textContent="\u7535\u8111\u6A21\u5F0F\u4F1A\u81EA\u52A8\u6A2A\u5C4F\u663E\u793A",e.appendChild(n);let s=document.createElement("div");s.className="segment",this.displayModeBtns=[];let i=[{value:"phone",label:"\u624B\u673A"},{value:"desktop",label:"\u7535\u8111"}];return i.forEach(a=>{let o=document.createElement("button");o.className=`segment-btn${a.value===this.displayMode?" active":""}`,o.textContent=a.label,o.addEventListener("click",()=>{this.displayMode=a.value;try{localStorage.setItem("storvia_display_mode",a.value)}catch{}this.displayModeBtns.forEach((d,p)=>{d.classList.toggle("active",i[p].value===a.value)});try{window.parent.postMessage({type:"storvia-orientation",mode:a.value==="desktop"?"landscape":"portrait"},"*")}catch{}}),this.displayModeBtns.push(o),s.appendChild(o)}),e.appendChild(s),e}buildSyncSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u540C\u6B65\u6E38\u620F\u8BBE\u5B9A",e.appendChild(t);let n=document.createElement("div");n.className=`sync-hint${this.hasWorldUpdate?" visible":""}`,n.textContent="\u26A0 \u4F5C\u8005\u5DF2\u66F4\u65B0\u6E38\u620F\u5185\u5BB9\uFF0C\u5EFA\u8BAE\u70B9\u51FB\u540C\u6B65",this.syncHint=n,e.appendChild(n);let s=document.createElement("div");s.className="sync-desc",s.textContent="\u5982\u679C\u6E38\u620F\u4F5C\u8005\u66F4\u65B0\u4E86\u5185\u5BB9\uFF0C\u70B9\u51FB\u6B64\u6309\u94AE\u53EF\u5C06\u6700\u65B0\u7684\u6E38\u620F\u8BBE\u5B9A\u548C\u5F00\u5E55\u5C0F\u5267\u573A\u540C\u6B65\u5230\u5F53\u524D\u5BF9\u8BDD",e.appendChild(s);let i=document.createElement("button");return i.className="sync-btn",this.syncBtn=i,this.renderSyncBtn(),i.addEventListener("click",async()=>{if(this.syncState!=="loading"){this.setSyncState("loading");try{await this.callbacks.onSync(),this.setSyncState("success"),this.hasWorldUpdate=!1,this.fabDot&&this.fabDot.classList.remove("visible"),this.syncHint&&this.syncHint.classList.remove("visible"),setTimeout(()=>this.setSyncState("idle"),3e3)}catch{this.setSyncState("error"),setTimeout(()=>this.setSyncState("idle"),4e3)}}}),e.appendChild(i),e}makeDivider(){let e=document.createElement("div");return e.className="divider",e}updateModelActive(){this.modelPanel&&this.modelPanel.querySelectorAll(".model-option").forEach((e,t)=>{e.classList.toggle("active",this.models[t].id===this.selectedModelId)})}setSyncState(e){this.syncState=e,this.renderSyncBtn()}renderSyncBtn(){if(!this.syncBtn)return;let e=this.syncState,t={idle:j,loading:z,success:G,error:F},n={idle:"\u540C\u6B65\u6700\u65B0\u8BBE\u5B9A",loading:"\u540C\u6B65\u4E2D...",success:"\u5DF2\u540C\u6B65",error:"\u540C\u6B65\u5931\u8D25"};this.syncBtn.className=`sync-btn${e==="success"||e==="error"?` ${e}`:""}`,this.syncBtn.disabled=e==="loading",this.syncBtn.innerHTML=`${t[e]}<span>${n[e]}</span>`}async handleSave(){if(!(!this.saveBtn||this.saveBtn.disabled)){this.saveBtn.disabled=!0,this.saveBtn.textContent="\u4FDD\u5B58\u4E2D...";try{await this.callbacks.onSettingsChange({temperature:this.settings.temperature,reply_length:this.settings.reply_length,reply_language:this.settings.reply_language,player_note:this.settings.player_note}),this.saveBtn.textContent="\u5DF2\u4FDD\u5B58",this.saveBtn.classList.add("saved"),setTimeout(()=>{this.saveBtn&&(this.saveBtn.textContent="\u4FDD\u5B58",this.saveBtn.classList.remove("saved"),this.saveBtn.disabled=!1)},2e3)}catch{this.saveBtn.textContent="\u4FDD\u5B58\u5931\u8D25",setTimeout(()=>{this.saveBtn&&(this.saveBtn.textContent="\u4FDD\u5B58",this.saveBtn.disabled=!1)},2e3)}}}};var f=class{constructor(e,t,n,s,i=!1,a){this.http=e;this.getGameId=t;this.getConversationId=n;this.getModelId=s;this.isPreview=i;this.previewConfig=a}updatePreviewConfig(e){this.previewConfig=e}async send(e){if(this.isPreview)return this.sendPreview(e);let t={game_id:this.getGameId(),conversation_id:this.getConversationId(),model_id:this.getModelId(),message:e.message,topic:e.topic,context:e.context,stream:e.stream??!1};if(e.stream){await this.http.stream("/chat",t,e.onChunk??(()=>{}),e.onDone??(()=>{}));return}return this.http.post("/chat",t)}async save(e){return this.isPreview?{id:`preview-${Date.now()}`}:this.http.post("/messages/save",{conversation_id:this.getConversationId(),role:e.role,content:e.content,topic:e.topic})}async generate(e){if(this.isPreview)return this.sendPreview({message:"",topic:e.topic,context:(e.context||"")+`
|
|
431
|
+
[\u6307\u4EE4] \u8BF7\u76F4\u63A5\u751F\u6210\u56DE\u590D\uFF0C\u65E0\u9700\u7528\u6237\u8F93\u5165`,stream:e.stream,onChunk:e.onChunk,onDone:e.onDone});let t={conversation_id:this.getConversationId(),model_id:this.getModelId(),topic:e.topic,context:e.context,stream:e.stream??!1,save:e.save??!0};if(e.stream){await this.http.stream("/generate",t,e.onChunk??(()=>{}),e.onDone??(()=>{}));return}return this.http.post("/generate",t)}async sendPreview(e){let t={model_id:this.getModelId(),message:e.message,topic:e.topic,context:e.context,prompt_config:this.previewConfig||{},history_messages:[]};if(e.stream){await this.http.stream("/preview",t,e.onChunk??(()=>{}),e.onDone??(()=>{}));return}let n="";return await this.http.stream("/preview",t,s=>{n+=s},()=>{}),{id:"",content:n}}};var R=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(e){return this.isPreview?{...this.mem[e]||{}}:this.http.get(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}/state`)}async update(e,t){if(this.isPreview){this.mem[e]={...this.mem[e]||{},...t};return}await this.http.patch(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}/state`,t)}},b=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem=[];this.state=new R(e,t,n)}async list(){return this.isPreview?this.mem.map(e=>({name:e.name,avatar:e.avatar})):this.http.get(`/conversations/${this.getConversationId()}/characters`)}async get(e){if(this.isPreview){let t=this.mem.find(n=>n.name===e);return{name:t?.name||e,avatar:t?.avatar}}return this.http.get(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}`)}async add(e){if(this.isPreview){this.mem.push(e);return}await this.http.post(`/conversations/${this.getConversationId()}/characters`,e)}async update(e,t){if(this.isPreview){let n=this.mem.find(s=>s.name===e);n&&Object.assign(n,t);return}await this.http.patch(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}`,t)}async remove(e){if(this.isPreview){this.mem=this.mem.filter(t=>t.name!==e);return}await this.http.delete(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}`)}async detail(e){if(this.isPreview){let t=this.mem.find(n=>n.name===e);return{name:e,avatar:t?.avatar,state:await this.state.get(e)}}return this.http.get(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}/detail`)}};var w=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem=[]}async list(){return this.isPreview?[...this.mem]:this.http.get(`/conversations/${this.getConversationId()}/relationships`)}async get(e,t){if(this.isPreview){let n=this.mem.find(s=>s.from===e&&s.to===t);if(n)return{...n};throw new Error("Relationship not found")}return this.http.get(`/conversations/${this.getConversationId()}/relationships/${encodeURIComponent(e)}/${encodeURIComponent(t)}`)}async add(e){if(this.isPreview){this.mem.push({...e});return}await this.http.post(`/conversations/${this.getConversationId()}/relationships`,e)}async update(e,t,n){if(this.isPreview){let s=this.mem.find(i=>i.from===e&&i.to===t);s?Object.assign(s,n):this.mem.push({from:e,to:t,...n});return}await this.http.patch(`/conversations/${this.getConversationId()}/relationships/${encodeURIComponent(e)}/${encodeURIComponent(t)}`,n)}async remove(e,t){if(this.isPreview){this.mem=this.mem.filter(n=>!(n.from===e&&n.to===t));return}await this.http.delete(`/conversations/${this.getConversationId()}/relationships/${encodeURIComponent(e)}/${encodeURIComponent(t)}`)}};var y=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem=[]}async list(e){if(this.isPreview)return e?this.mem.filter(n=>n.group===e):[...this.mem];let t=e?{group:e}:void 0;return this.http.get(`/conversations/${this.getConversationId()}/inventory`,t)}async add(e){if(this.isPreview){this.mem.push({...e});return}await this.http.post(`/conversations/${this.getConversationId()}/inventory`,e)}async update(e,t){if(this.isPreview){let n=this.mem.find(s=>s.name===e);n&&Object.assign(n,t);return}await this.http.patch(`/conversations/${this.getConversationId()}/inventory/${encodeURIComponent(e)}`,t)}async remove(e){if(this.isPreview){this.mem=this.mem.filter(t=>t.name!==e);return}await this.http.delete(`/conversations/${this.getConversationId()}/inventory/${encodeURIComponent(e)}`)}};var C=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(){return this.isPreview?{...this.mem}:this.http.get(`/conversations/${this.getConversationId()}/world`)}async update(e){if(this.isPreview){Object.assign(this.mem,e);return}await this.http.patch(`/conversations/${this.getConversationId()}/world`,e)}};var T=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(e){if(this.isPreview)return e?{...this.mem[e]||{}}:{...this.mem};let t=e?{group:e}:void 0;return this.http.get(`/conversations/${this.getConversationId()}/player/state`,t)}async update(e,t){if(this.isPreview){this.mem[e]={...this.mem[e]||{},...t};return}await this.http.patch(`/conversations/${this.getConversationId()}/player/state`,{group:e,values:t})}},x=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.memPersona={name:""};this.state=new T(e,t,n)}async get(){return this.isPreview?{...this.memPersona}:this.http.get(`/conversations/${this.getConversationId()}/player`)}};var I=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(e){if(this.isPreview)return e!==void 0?this.mem[e]:{...this.mem};let t=await this.http.get(`/conversations/${this.getConversationId()}/custom`);return e!==void 0?t[e]:t}async set(e){if(this.isPreview){for(let[t,n]of Object.entries(e))typeof n=="object"&&n!==null&&!Array.isArray(n)&&typeof this.mem[t]=="object"&&this.mem[t]!==null?this.mem[t]={...this.mem[t],...n}:this.mem[t]=n;return}await this.http.patch(`/conversations/${this.getConversationId()}/custom`,e)}async delete(e){if(this.isPreview){delete this.mem[e];return}await this.http.delete(`/conversations/${this.getConversationId()}/custom/${encodeURIComponent(e)}`)}async setGroupField(e,t,n){if(this.isPreview){(!this.mem[e]||typeof this.mem[e]!="object")&&(this.mem[e]={}),this.mem[e][t]=n;return}await this.http.patch(`/conversations/${this.getConversationId()}/custom/${encodeURIComponent(e)}/${encodeURIComponent(t)}`,{value:n})}async deleteGroupField(e,t){if(this.isPreview){if(typeof this.mem[e]=="object"&&this.mem[e]!==null){let n=this.mem[e];delete n[t],Object.keys(n).length===0&&delete this.mem[e]}return}await this.http.delete(`/conversations/${this.getConversationId()}/custom/${encodeURIComponent(e)}/${encodeURIComponent(t)}`)}};var V="/functions/v1/world-engine/v1";function W(){return window.__STORVIA__||{}}function J(l=5e3){let e=W();return e.apiBase?Promise.resolve(e):new Promise((t,n)=>{let s=setTimeout(()=>{window.removeEventListener("message",i),n(new c("NOT_INITIALIZED","Timed out waiting for __STORVIA__ config from parent frame."))},l);function i(a){let o=null;if(a.data?.type==="storvia-init"&&a.data.apiBase?o={apiBase:a.data.apiBase,gameId:a.data.gameId,accessToken:a.data.accessToken}:a.data?.type==="storvia-preview-init"&&a.data.config&&(o=a.data.config),o&&o.apiBase){clearTimeout(s),window.removeEventListener("message",i);let d=window;d.__STORVIA__=o,t(o)}}window.addEventListener("message",i)})}var k=class{constructor(){this.baseUrl="";this.gameId="";this.conversationId="";this.modelId="";this.accessToken="";this.initialized=!1;this.isPreview=!1;this.previewConfig=void 0}async init(){let e=await J(),t=e.apiBase;if(!t)throw new c("NOT_INITIALIZED","window.__STORVIA__.apiBase is not set. The platform must inject it before SDK init.");if(this.baseUrl=`${t}${V}`,this.isPreview=!!e.preview,this.previewConfig=e.previewConfig,!e.gameId)throw new c("GAME_NOT_FOUND","window.__STORVIA__.gameId is not set. The platform must inject it before SDK init.");if(this.gameId=e.gameId,!e.accessToken)throw new c("UNAUTHORIZED","window.__STORVIA__.accessToken is not set. The platform must inject the user token before SDK init.");if(this.accessToken=e.accessToken,this.http=new S(this.baseUrl,this.accessToken,!!e.debug),this.isPreview){this.conversationId="00000000-0000-0000-0000-000000000000";let r=e.models||[];this.modelId=e.selectedModelId||r[0]?.id||"",this.settingsSheet=new v({onModelChange:E=>{this.modelId=E},onSettingsChange:()=>Promise.resolve(),onSync:()=>Promise.resolve()}),r.length>0&&this.settingsSheet.mount(r,this.modelId,{temperature:1,reply_length:"medium",reply_language:"zh-CN",player_note:""},!1);let m=()=>this.gameId,h=()=>this.conversationId,u=()=>this.modelId;this.chat=new f(this.http,m,h,u,!0,this.previewConfig),this.characters=new b(this.http,h,!0),this.relationships=new w(this.http,h,!0),this.inventory=new y(this.http,h,!0),this.world=new C(this.http,h,!0),this.player=new x(this.http,h,!0),this.custom=new I(this.http,h,!0),this.initialized=!0,this.listenForPreviewMessages();return}let n=await this.authenticate();this.conversationId=n.conversationId;let s=n.models||[],i=this.getSavedModelId();this.modelId=i&&s.some(r=>r.id===i)?i:s[0]?.id||"";let a=n.settings||{temperature:1,reply_length:"medium",reply_language:"zh-CN",player_note:""};this.settingsSheet=new v({onModelChange:r=>{this.modelId=r},onSettingsChange:r=>this.saveSettings(r),onSync:()=>this.syncSettings()}),this.settingsSheet.mount(s,this.modelId,a,n.hasWorldUpdate??!1);let o=()=>this.gameId,d=()=>this.conversationId,p=()=>this.modelId;this.chat=new f(this.http,o,d,p),this.characters=new b(this.http,d),this.relationships=new w(this.http,d),this.inventory=new y(this.http,d),this.world=new C(this.http,d),this.player=new x(this.http,d),this.custom=new I(this.http,d),this.initialized=!0}listenForPreviewMessages(){window.addEventListener("message",e=>{!e.data||e.data.type!=="storvia-preview"||(e.data.action==="set-model"&&(this.modelId=e.data.modelId||""),e.data.action==="update-config"&&(this.previewConfig=e.data.previewConfig,this.chat&&this.chat.updatePreviewConfig(this.previewConfig)))})}async saveSettings(e){await this.http.patch("/settings",{conversation_id:this.conversationId,...e})}async syncSettings(){await this.http.post("/sync",{conversation_id:this.conversationId})}async authenticate(){let e=await fetch(`${this.baseUrl}/auth`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.accessToken}`},body:JSON.stringify({game_id:this.gameId})});if(!e.ok)throw e.status===401?new c("UNAUTHORIZED","User not authenticated"):e.status===404?new c("GAME_NOT_FOUND","Game not found or not published"):new c("INTERNAL_ERROR",`Auth failed: HTTP ${e.status}`);let t=await e.json();return t.data??t}getSavedModelId(){try{return localStorage.getItem("storvia_model_id")||""}catch{return""}}};0&&(module.exports={StorviaError,StorviaSDK});
|
package/dist/index.esm.js
CHANGED
|
@@ -427,4 +427,5 @@ var l=class extends Error{constructor(e,t){super(t),this.name="StorviaError",thi
|
|
|
427
427
|
to { transform: rotate(360deg); }
|
|
428
428
|
}
|
|
429
429
|
.spin { animation: spin 0.8s linear infinite; }
|
|
430
|
-
`,$='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg>',L='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="23 4 23 10 17 10"/><polyline points="1 20 1 14 7 14"/><path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"/></svg>',H='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="spin"><path d="M21 12a9 9 0 1 1-6.219-8.56"/></svg>',O='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>',B='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg>',v=class{constructor(e){this.selectedModelId="";this.models=[];this.modelPanel=null;this.settings={temperature:1,reply_length:"medium",reply_language:"zh-CN",player_note:""};this.hasWorldUpdate=!1;this.fabDot=null;this.syncHint=null;this.displayMode="phone";this.displayModeBtns=[];this.tempValueEl=null;this.tempSlider=null;this.segmentBtns=[];this.langSelect=null;this.noteInput=null;this.noteCounter=null;this.saveBtn=null;this.syncBtn=null;this.syncState="idle";this.panel=null;this.callbacks=e,this.host=document.createElement("div"),this.host.id="storvia-settings",this.shadowRoot=this.host.attachShadow({mode:"closed"})}mount(e,t,n,s=!1){this.models=e,this.selectedModelId=t,this.settings={...n},this.hasWorldUpdate=s;try{let i=localStorage.getItem("storvia_display_mode");(i==="phone"||i==="desktop")&&(this.displayMode=i)}catch{}document.body.appendChild(this.host),this.render()}unmount(){this.host.remove()}getSelectedModelId(){return this.selectedModelId}getSettings(){return{...this.settings}}render(){let e=document.createElement("style");e.textContent=M;let t=document.createElement("div"),n=document.createElement("div");n.className="fab-wrap";let s=document.createElement("button");s.className="fab",s.innerHTML=$,s.title="\u6E38\u620F\u8BBE\u7F6E";let i=document.createElement("div");i.className=`fab-dot${this.hasWorldUpdate?" visible":""}`,this.fabDot=i,n.appendChild(s),n.appendChild(i);let a=document.createElement("div");a.className="panel",this.panel=a;let o=document.createElement("div");o.className="panel-header";let d=document.createElement("div");d.className="panel-title",d.textContent="\u6E38\u620F\u8BBE\u7F6E";let p=document.createElement("button");p.className="close-btn",p.innerHTML="\u2715",p.addEventListener("click",()=>a.classList.remove("open")),o.appendChild(d),o.appendChild(p),a.appendChild(o);let r=document.createElement("div");r.className="panel-body",this.models.length>0&&(r.appendChild(this.buildModelSection()),r.appendChild(this.makeDivider())),r.appendChild(this.buildTemperatureSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildReplyLengthSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildReplyLanguageSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildNoteSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildDisplayModeSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildSyncSection());let m=document.createElement("div");m.className="save-row";let h=document.createElement("button");h.className="save-btn",h.textContent="\u4FDD\u5B58",this.saveBtn=h,h.addEventListener("click",()=>this.handleSave()),m.appendChild(h),r.appendChild(m),a.appendChild(r),s.addEventListener("click",()=>a.classList.toggle("open")),document.addEventListener("click",u=>{this.host.contains(u.target)||a.classList.remove("open")}),t.appendChild(a),t.appendChild(n),this.shadowRoot.appendChild(e),this.shadowRoot.appendChild(t)}buildModelSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="AI \u6A21\u578B",e.appendChild(t);let n=document.createElement("div");return n.className="model-list",this.modelPanel=n,this.models.forEach(s=>{let i=document.createElement("button");i.className=`model-option${s.id===this.selectedModelId?" active":""}`,i.innerHTML=`<span class="dot"></span><span>${s.name}</span>`,i.addEventListener("click",()=>{this.selectedModelId=s.id,this.callbacks.onModelChange(s.id);try{localStorage.setItem("storvia_model_id",s.id)}catch{}this.updateModelActive()}),n.appendChild(i)}),e.appendChild(n),e}buildTemperatureSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label";let n=document.createElement("span");n.textContent="\u6E29\u5EA6";let s=document.createElement("span");s.className="value",s.textContent=this.settings.temperature.toFixed(1),this.tempValueEl=s,t.appendChild(n),t.appendChild(s),e.appendChild(t);let i=document.createElement("div");i.className="section-desc",i.textContent="\u66F4\u4F4E\u7684\u6E29\u5EA6\u4F1A\u8BA9 AI \u56DE\u590D\u66F4\u7A33\u5B9A\u3001\u7406\u667A\uFF1B\u66F4\u9AD8\u7684\u6E29\u5EA6\u4F1A\u8BA9 AI \u7684\u56DE\u590D\u66F4\u5177\u591A\u6837\u6027\u548C\u968F\u673A\u6027",e.appendChild(i);let a=document.createElement("div");a.className="slider-wrap";let o=document.createElement("input");o.type="range",o.className="slider",o.min="0",o.max="2",o.step="0.1",o.value=String(this.settings.temperature),this.tempSlider=o,o.addEventListener("input",()=>{let p=parseFloat(o.value);this.settings.temperature=p,this.tempValueEl&&(this.tempValueEl.textContent=p.toFixed(1))}),a.appendChild(o);let d=document.createElement("div");return d.className="slider-labels",d.innerHTML="<span>\u7CBE\u786E</span><span>\u521B\u610F</span>",a.appendChild(d),e.appendChild(a),e}buildReplyLengthSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u56DE\u590D\u957F\u5EA6",e.appendChild(t);let n=document.createElement("div");return n.className="segment",this.segmentBtns=[],_.forEach(s=>{let i=document.createElement("button");i.className=`segment-btn${s.value===this.settings.reply_length?" active":""}`,i.textContent=s.label,i.addEventListener("click",()=>{this.settings.reply_length=s.value,this.segmentBtns.forEach((a,o)=>{a.classList.toggle("active",_[o].value===s.value)})}),this.segmentBtns.push(i),n.appendChild(i)}),e.appendChild(n),e}buildReplyLanguageSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u56DE\u590D\u8BED\u8A00",e.appendChild(t);let n=document.createElement("div");n.className="select-wrap";let s=document.createElement("select");s.className="select",this.langSelect=s,N.forEach(a=>{let o=document.createElement("option");o.value=a.value,o.textContent=a.label,a.value===this.settings.reply_language&&(o.selected=!0),s.appendChild(o)}),s.addEventListener("change",()=>{this.settings.reply_language=s.value});let i=document.createElement("span");return i.className="select-arrow",i.textContent="\u25BE",n.appendChild(s),n.appendChild(i),e.appendChild(n),e}buildNoteSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label";let n=document.createElement("span");n.textContent="\u5907\u6CE8";let s=document.createElement("span");s.textContent=`${this.settings.player_note.length}/50`,this.noteCounter=s,t.appendChild(n),t.appendChild(s),e.appendChild(t);let i=document.createElement("div");i.className="section-desc",i.textContent="\u5199\u4E0B\u4F60\u5BF9\u8FD9\u4E2A\u6E38\u620F\u7684\u5907\u6CE8\uFF0C\u4F1A\u663E\u793A\u5728\u5B58\u6863\u5217\u8868",e.appendChild(i);let a=document.createElement("div");a.className="note-wrap";let o=document.createElement("textarea");return o.className="note-input",o.placeholder="\u5907\u6CE8",o.maxLength=50,o.value=this.settings.player_note,this.noteInput=o,o.addEventListener("input",()=>{this.settings.player_note=o.value,this.noteCounter&&(this.noteCounter.textContent=`${o.value.length}/50`)}),a.appendChild(o),e.appendChild(a),e}buildDisplayModeSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u663E\u793A\u6A21\u5F0F",e.appendChild(t);let n=document.createElement("div");n.className="section-desc",n.textContent="\u7535\u8111\u6A21\u5F0F\u4F1A\u81EA\u52A8\u6A2A\u5C4F\u663E\u793A",e.appendChild(n);let s=document.createElement("div");s.className="segment",this.displayModeBtns=[];let i=[{value:"phone",label:"\u624B\u673A"},{value:"desktop",label:"\u7535\u8111"}];return i.forEach(a=>{let o=document.createElement("button");o.className=`segment-btn${a.value===this.displayMode?" active":""}`,o.textContent=a.label,o.addEventListener("click",()=>{this.displayMode=a.value;try{localStorage.setItem("storvia_display_mode",a.value)}catch{}this.displayModeBtns.forEach((d,p)=>{d.classList.toggle("active",i[p].value===a.value)});try{window.parent.postMessage({type:"storvia-orientation",mode:a.value==="desktop"?"landscape":"portrait"},"*")}catch{}}),this.displayModeBtns.push(o),s.appendChild(o)}),e.appendChild(s),e}buildSyncSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u540C\u6B65\u6E38\u620F\u8BBE\u5B9A",e.appendChild(t);let n=document.createElement("div");n.className=`sync-hint${this.hasWorldUpdate?" visible":""}`,n.textContent="\u26A0 \u4F5C\u8005\u5DF2\u66F4\u65B0\u6E38\u620F\u5185\u5BB9\uFF0C\u5EFA\u8BAE\u70B9\u51FB\u540C\u6B65",this.syncHint=n,e.appendChild(n);let s=document.createElement("div");s.className="sync-desc",s.textContent="\u5982\u679C\u6E38\u620F\u4F5C\u8005\u66F4\u65B0\u4E86\u5185\u5BB9\uFF0C\u70B9\u51FB\u6B64\u6309\u94AE\u53EF\u5C06\u6700\u65B0\u7684\u6E38\u620F\u8BBE\u5B9A\u548C\u5F00\u5E55\u5C0F\u5267\u573A\u540C\u6B65\u5230\u5F53\u524D\u5BF9\u8BDD",e.appendChild(s);let i=document.createElement("button");return i.className="sync-btn",this.syncBtn=i,this.renderSyncBtn(),i.addEventListener("click",async()=>{if(this.syncState!=="loading"){this.setSyncState("loading");try{await this.callbacks.onSync(),this.setSyncState("success"),this.hasWorldUpdate=!1,this.fabDot&&this.fabDot.classList.remove("visible"),this.syncHint&&this.syncHint.classList.remove("visible"),setTimeout(()=>this.setSyncState("idle"),3e3)}catch{this.setSyncState("error"),setTimeout(()=>this.setSyncState("idle"),4e3)}}}),e.appendChild(i),e}makeDivider(){let e=document.createElement("div");return e.className="divider",e}updateModelActive(){this.modelPanel&&this.modelPanel.querySelectorAll(".model-option").forEach((e,t)=>{e.classList.toggle("active",this.models[t].id===this.selectedModelId)})}setSyncState(e){this.syncState=e,this.renderSyncBtn()}renderSyncBtn(){if(!this.syncBtn)return;let e=this.syncState,t={idle:L,loading:H,success:O,error:B},n={idle:"\u540C\u6B65\u6700\u65B0\u8BBE\u5B9A",loading:"\u540C\u6B65\u4E2D...",success:"\u5DF2\u540C\u6B65",error:"\u540C\u6B65\u5931\u8D25"};this.syncBtn.className=`sync-btn${e==="success"||e==="error"?` ${e}`:""}`,this.syncBtn.disabled=e==="loading",this.syncBtn.innerHTML=`${t[e]}<span>${n[e]}</span>`}async handleSave(){if(!(!this.saveBtn||this.saveBtn.disabled)){this.saveBtn.disabled=!0,this.saveBtn.textContent="\u4FDD\u5B58\u4E2D...";try{await this.callbacks.onSettingsChange({temperature:this.settings.temperature,reply_length:this.settings.reply_length,reply_language:this.settings.reply_language,player_note:this.settings.player_note}),this.saveBtn.textContent="\u5DF2\u4FDD\u5B58",this.saveBtn.classList.add("saved"),setTimeout(()=>{this.saveBtn&&(this.saveBtn.textContent="\u4FDD\u5B58",this.saveBtn.classList.remove("saved"),this.saveBtn.disabled=!1)},2e3)}catch{this.saveBtn.textContent="\u4FDD\u5B58\u5931\u8D25",setTimeout(()=>{this.saveBtn&&(this.saveBtn.textContent="\u4FDD\u5B58",this.saveBtn.disabled=!1)},2e3)}}}};var f=class{constructor(e,t,n,s,i=!1,a){this.http=e;this.getGameId=t;this.getConversationId=n;this.getModelId=s;this.isPreview=i;this.previewConfig=a}updatePreviewConfig(e){this.previewConfig=e}async send(e){if(this.isPreview)return this.sendPreview(e);let t={game_id:this.getGameId(),conversation_id:this.getConversationId(),model_id:this.getModelId(),message:e.message,topic:e.topic,context:e.context,stream:e.stream??!1};if(e.stream){await this.http.stream("/chat",t,e.onChunk??(()=>{}),e.onDone??(()=>{}));return}return this.http.post("/chat",t)}async save(e){return this.http.post("/messages/save",{conversation_id:this.getConversationId(),role:e.role,content:e.content,topic:e.topic})}async generate(e){let t={conversation_id:this.getConversationId(),model_id:this.getModelId(),topic:e.topic,context:e.context,stream:e.stream??!1,save:e.save??!0};if(e.stream){await this.http.stream("/generate",t,e.onChunk??(()=>{}),e.onDone??(()=>{}));return}return this.http.post("/generate",t)}async sendPreview(e){let t={model_id:this.getModelId(),message:e.message,topic:e.topic,context:e.context,prompt_config:this.previewConfig||{},history_messages:[]};if(e.stream){await this.http.stream("/preview",t,e.onChunk??(()=>{}),e.onDone??(()=>{}));return}let n="";return await this.http.stream("/preview",t,s=>{n+=s},()=>{}),{id:"",content:n}}};var k=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(e){return this.isPreview?{...this.mem[e]||{}}:this.http.get(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}/state`)}async update(e,t){if(this.isPreview){this.mem[e]={...this.mem[e]||{},...t};return}await this.http.patch(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}/state`,t)}},b=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem=[];this.state=new k(e,t,n)}async list(){return this.isPreview?this.mem.map(e=>({name:e.name,avatar:e.avatar})):this.http.get(`/conversations/${this.getConversationId()}/characters`)}async get(e){if(this.isPreview){let t=this.mem.find(n=>n.name===e);return{name:t?.name||e,avatar:t?.avatar}}return this.http.get(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}`)}async add(e){if(this.isPreview){this.mem.push(e);return}await this.http.post(`/conversations/${this.getConversationId()}/characters`,e)}async update(e,t){if(this.isPreview){let n=this.mem.find(s=>s.name===e);n&&Object.assign(n,t);return}await this.http.patch(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}`,t)}async remove(e){if(this.isPreview){this.mem=this.mem.filter(t=>t.name!==e);return}await this.http.delete(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}`)}async detail(e){if(this.isPreview){let t=this.mem.find(n=>n.name===e);return{name:e,avatar:t?.avatar,state:await this.state.get(e)}}return this.http.get(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}/detail`)}};var w=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem=[]}async list(){return this.isPreview?[...this.mem]:this.http.get(`/conversations/${this.getConversationId()}/relationships`)}async get(e,t){if(this.isPreview){let n=this.mem.find(s=>s.from===e&&s.to===t);if(n)return{...n};throw new Error("Relationship not found")}return this.http.get(`/conversations/${this.getConversationId()}/relationships/${encodeURIComponent(e)}/${encodeURIComponent(t)}`)}async add(e){if(this.isPreview){this.mem.push({...e});return}await this.http.post(`/conversations/${this.getConversationId()}/relationships`,e)}async update(e,t,n){if(this.isPreview){let s=this.mem.find(i=>i.from===e&&i.to===t);s?Object.assign(s,n):this.mem.push({from:e,to:t,...n});return}await this.http.patch(`/conversations/${this.getConversationId()}/relationships/${encodeURIComponent(e)}/${encodeURIComponent(t)}`,n)}async remove(e,t){if(this.isPreview){this.mem=this.mem.filter(n=>!(n.from===e&&n.to===t));return}await this.http.delete(`/conversations/${this.getConversationId()}/relationships/${encodeURIComponent(e)}/${encodeURIComponent(t)}`)}};var y=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem=[]}async list(e){if(this.isPreview)return e?this.mem.filter(n=>n.group===e):[...this.mem];let t=e?{group:e}:void 0;return this.http.get(`/conversations/${this.getConversationId()}/inventory`,t)}async add(e){if(this.isPreview){this.mem.push({...e});return}await this.http.post(`/conversations/${this.getConversationId()}/inventory`,e)}async update(e,t){if(this.isPreview){let n=this.mem.find(s=>s.name===e);n&&Object.assign(n,t);return}await this.http.patch(`/conversations/${this.getConversationId()}/inventory/${encodeURIComponent(e)}`,t)}async remove(e){if(this.isPreview){this.mem=this.mem.filter(t=>t.name!==e);return}await this.http.delete(`/conversations/${this.getConversationId()}/inventory/${encodeURIComponent(e)}`)}};var C=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(){return this.isPreview?{...this.mem}:this.http.get(`/conversations/${this.getConversationId()}/world`)}async update(e){if(this.isPreview){Object.assign(this.mem,e);return}await this.http.patch(`/conversations/${this.getConversationId()}/world`,e)}};var P=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(e){if(this.isPreview)return e?{...this.mem[e]||{}}:{...this.mem};let t=e?{group:e}:void 0;return this.http.get(`/conversations/${this.getConversationId()}/player/state`,t)}async update(e,t){if(this.isPreview){this.mem[e]={...this.mem[e]||{},...t};return}await this.http.patch(`/conversations/${this.getConversationId()}/player/state`,{group:e,values:t})}},x=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.memPersona={name:""};this.state=new P(e,t,n)}async get(){return this.isPreview?{...this.memPersona}:this.http.get(`/conversations/${this.getConversationId()}/player`)}};var I=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(e){if(this.isPreview)return e!==void 0?this.mem[e]:{...this.mem};let t=await this.http.get(`/conversations/${this.getConversationId()}/custom`);return e!==void 0?t[e]:t}async set(e){if(this.isPreview){for(let[t,n]of Object.entries(e))typeof n=="object"&&n!==null&&!Array.isArray(n)&&typeof this.mem[t]=="object"&&this.mem[t]!==null?this.mem[t]={...this.mem[t],...n}:this.mem[t]=n;return}await this.http.patch(`/conversations/${this.getConversationId()}/custom`,e)}async delete(e){if(this.isPreview){delete this.mem[e];return}await this.http.delete(`/conversations/${this.getConversationId()}/custom/${encodeURIComponent(e)}`)}async setGroupField(e,t,n){if(this.isPreview){(!this.mem[e]||typeof this.mem[e]!="object")&&(this.mem[e]={}),this.mem[e][t]=n;return}await this.http.patch(`/conversations/${this.getConversationId()}/custom/${encodeURIComponent(e)}/${encodeURIComponent(t)}`,{value:n})}async deleteGroupField(e,t){if(this.isPreview){if(typeof this.mem[e]=="object"&&this.mem[e]!==null){let n=this.mem[e];delete n[t],Object.keys(n).length===0&&delete this.mem[e]}return}await this.http.delete(`/conversations/${this.getConversationId()}/custom/${encodeURIComponent(e)}/${encodeURIComponent(t)}`)}};var A="/functions/v1/world-engine/v1";function U(){return window.__STORVIA__||{}}function D(c=5e3){let e=U();return e.apiBase?Promise.resolve(e):new Promise((t,n)=>{let s=setTimeout(()=>{window.removeEventListener("message",i),n(new l("NOT_INITIALIZED","Timed out waiting for __STORVIA__ config from parent frame."))},c);function i(a){let o=null;if(a.data?.type==="storvia-init"&&a.data.apiBase?o={apiBase:a.data.apiBase,gameId:a.data.gameId,accessToken:a.data.accessToken}:a.data?.type==="storvia-preview-init"&&a.data.config&&(o=a.data.config),o&&o.apiBase){clearTimeout(s),window.removeEventListener("message",i);let d=window;d.__STORVIA__=o,t(o)}}window.addEventListener("message",i)})}var R=class{constructor(){this.baseUrl="";this.gameId="";this.conversationId="";this.modelId="";this.accessToken="";this.initialized=!1;this.isPreview=!1;this.previewConfig=void 0}async init(){let e=await D(),t=e.apiBase;if(!t)throw new l("NOT_INITIALIZED","window.__STORVIA__.apiBase is not set. The platform must inject it before SDK init.");if(this.baseUrl=`${t}${A}`,this.isPreview=!!e.preview,this.previewConfig=e.previewConfig,!e.gameId)throw new l("GAME_NOT_FOUND","window.__STORVIA__.gameId is not set. The platform must inject it before SDK init.");if(this.gameId=e.gameId,!e.accessToken)throw new l("UNAUTHORIZED","window.__STORVIA__.accessToken is not set. The platform must inject the user token before SDK init.");if(this.accessToken=e.accessToken,this.http=new S(this.baseUrl,this.accessToken,!!e.debug),this.isPreview){this.conversationId="00000000-0000-0000-0000-000000000000";let r=e.models||[];this.modelId=e.selectedModelId||r[0]?.id||"",this.settingsSheet=new v({onModelChange:E=>{this.modelId=E},onSettingsChange:()=>Promise.resolve(),onSync:()=>Promise.resolve()}),r.length>0&&this.settingsSheet.mount(r,this.modelId,{temperature:1,reply_length:"medium",reply_language:"zh-CN",player_note:""},!1);let m=()=>this.gameId,h=()=>this.conversationId,u=()=>this.modelId;this.chat=new f(this.http,m,h,u,!0,this.previewConfig),this.characters=new b(this.http,h,!0),this.relationships=new w(this.http,h,!0),this.inventory=new y(this.http,h,!0),this.world=new C(this.http,h,!0),this.player=new x(this.http,h,!0),this.custom=new I(this.http,h,!0),this.initialized=!0,this.listenForPreviewMessages();return}let n=await this.authenticate();this.conversationId=n.conversationId;let s=n.models||[],i=this.getSavedModelId();this.modelId=i&&s.some(r=>r.id===i)?i:s[0]?.id||"";let a=n.settings||{temperature:1,reply_length:"medium",reply_language:"zh-CN",player_note:""};this.settingsSheet=new v({onModelChange:r=>{this.modelId=r},onSettingsChange:r=>this.saveSettings(r),onSync:()=>this.syncSettings()}),this.settingsSheet.mount(s,this.modelId,a,n.hasWorldUpdate??!1);let o=()=>this.gameId,d=()=>this.conversationId,p=()=>this.modelId;this.chat=new f(this.http,o,d,p),this.characters=new b(this.http,d),this.relationships=new w(this.http,d),this.inventory=new y(this.http,d),this.world=new C(this.http,d),this.player=new x(this.http,d),this.custom=new I(this.http,d),this.initialized=!0}listenForPreviewMessages(){window.addEventListener("message",e=>{!e.data||e.data.type!=="storvia-preview"||(e.data.action==="set-model"&&(this.modelId=e.data.modelId||""),e.data.action==="update-config"&&(this.previewConfig=e.data.previewConfig,this.chat&&this.chat.updatePreviewConfig(this.previewConfig)))})}async saveSettings(e){await this.http.patch("/settings",{conversation_id:this.conversationId,...e})}async syncSettings(){await this.http.post("/sync",{conversation_id:this.conversationId})}async authenticate(){let e=await fetch(`${this.baseUrl}/auth`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.accessToken}`},body:JSON.stringify({game_id:this.gameId})});if(!e.ok)throw e.status===401?new l("UNAUTHORIZED","User not authenticated"):e.status===404?new l("GAME_NOT_FOUND","Game not found or not published"):new l("INTERNAL_ERROR",`Auth failed: HTTP ${e.status}`);let t=await e.json();return t.data??t}getSavedModelId(){try{return localStorage.getItem("storvia_model_id")||""}catch{return""}}};export{l as StorviaError,R as StorviaSDK};
|
|
430
|
+
`,$='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg>',L='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="23 4 23 10 17 10"/><polyline points="1 20 1 14 7 14"/><path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"/></svg>',H='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="spin"><path d="M21 12a9 9 0 1 1-6.219-8.56"/></svg>',O='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>',B='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg>',v=class{constructor(e){this.selectedModelId="";this.models=[];this.modelPanel=null;this.settings={temperature:1,reply_length:"medium",reply_language:"zh-CN",player_note:""};this.hasWorldUpdate=!1;this.fabDot=null;this.syncHint=null;this.displayMode="phone";this.displayModeBtns=[];this.tempValueEl=null;this.tempSlider=null;this.segmentBtns=[];this.langSelect=null;this.noteInput=null;this.noteCounter=null;this.saveBtn=null;this.syncBtn=null;this.syncState="idle";this.panel=null;this.callbacks=e,this.host=document.createElement("div"),this.host.id="storvia-settings",this.shadowRoot=this.host.attachShadow({mode:"closed"})}mount(e,t,n,s=!1){this.models=e,this.selectedModelId=t,this.settings={...n},this.hasWorldUpdate=s;try{let i=localStorage.getItem("storvia_display_mode");(i==="phone"||i==="desktop")&&(this.displayMode=i)}catch{}document.body.appendChild(this.host),this.render()}unmount(){this.host.remove()}getSelectedModelId(){return this.selectedModelId}getSettings(){return{...this.settings}}render(){let e=document.createElement("style");e.textContent=M;let t=document.createElement("div"),n=document.createElement("div");n.className="fab-wrap";let s=document.createElement("button");s.className="fab",s.innerHTML=$,s.title="\u6E38\u620F\u8BBE\u7F6E";let i=document.createElement("div");i.className=`fab-dot${this.hasWorldUpdate?" visible":""}`,this.fabDot=i,n.appendChild(s),n.appendChild(i);let a=document.createElement("div");a.className="panel",this.panel=a;let o=document.createElement("div");o.className="panel-header";let d=document.createElement("div");d.className="panel-title",d.textContent="\u6E38\u620F\u8BBE\u7F6E";let p=document.createElement("button");p.className="close-btn",p.innerHTML="\u2715",p.addEventListener("click",()=>a.classList.remove("open")),o.appendChild(d),o.appendChild(p),a.appendChild(o);let r=document.createElement("div");r.className="panel-body",this.models.length>0&&(r.appendChild(this.buildModelSection()),r.appendChild(this.makeDivider())),r.appendChild(this.buildTemperatureSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildReplyLengthSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildReplyLanguageSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildNoteSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildDisplayModeSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildSyncSection());let m=document.createElement("div");m.className="save-row";let h=document.createElement("button");h.className="save-btn",h.textContent="\u4FDD\u5B58",this.saveBtn=h,h.addEventListener("click",()=>this.handleSave()),m.appendChild(h),r.appendChild(m),a.appendChild(r),s.addEventListener("click",()=>a.classList.toggle("open")),document.addEventListener("click",u=>{this.host.contains(u.target)||a.classList.remove("open")}),t.appendChild(a),t.appendChild(n),this.shadowRoot.appendChild(e),this.shadowRoot.appendChild(t)}buildModelSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="AI \u6A21\u578B",e.appendChild(t);let n=document.createElement("div");return n.className="model-list",this.modelPanel=n,this.models.forEach(s=>{let i=document.createElement("button");i.className=`model-option${s.id===this.selectedModelId?" active":""}`,i.innerHTML=`<span class="dot"></span><span>${s.name}</span>`,i.addEventListener("click",()=>{this.selectedModelId=s.id,this.callbacks.onModelChange(s.id);try{localStorage.setItem("storvia_model_id",s.id)}catch{}this.updateModelActive()}),n.appendChild(i)}),e.appendChild(n),e}buildTemperatureSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label";let n=document.createElement("span");n.textContent="\u6E29\u5EA6";let s=document.createElement("span");s.className="value",s.textContent=this.settings.temperature.toFixed(1),this.tempValueEl=s,t.appendChild(n),t.appendChild(s),e.appendChild(t);let i=document.createElement("div");i.className="section-desc",i.textContent="\u66F4\u4F4E\u7684\u6E29\u5EA6\u4F1A\u8BA9 AI \u56DE\u590D\u66F4\u7A33\u5B9A\u3001\u7406\u667A\uFF1B\u66F4\u9AD8\u7684\u6E29\u5EA6\u4F1A\u8BA9 AI \u7684\u56DE\u590D\u66F4\u5177\u591A\u6837\u6027\u548C\u968F\u673A\u6027",e.appendChild(i);let a=document.createElement("div");a.className="slider-wrap";let o=document.createElement("input");o.type="range",o.className="slider",o.min="0",o.max="2",o.step="0.1",o.value=String(this.settings.temperature),this.tempSlider=o,o.addEventListener("input",()=>{let p=parseFloat(o.value);this.settings.temperature=p,this.tempValueEl&&(this.tempValueEl.textContent=p.toFixed(1))}),a.appendChild(o);let d=document.createElement("div");return d.className="slider-labels",d.innerHTML="<span>\u7CBE\u786E</span><span>\u521B\u610F</span>",a.appendChild(d),e.appendChild(a),e}buildReplyLengthSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u56DE\u590D\u957F\u5EA6",e.appendChild(t);let n=document.createElement("div");return n.className="segment",this.segmentBtns=[],_.forEach(s=>{let i=document.createElement("button");i.className=`segment-btn${s.value===this.settings.reply_length?" active":""}`,i.textContent=s.label,i.addEventListener("click",()=>{this.settings.reply_length=s.value,this.segmentBtns.forEach((a,o)=>{a.classList.toggle("active",_[o].value===s.value)})}),this.segmentBtns.push(i),n.appendChild(i)}),e.appendChild(n),e}buildReplyLanguageSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u56DE\u590D\u8BED\u8A00",e.appendChild(t);let n=document.createElement("div");n.className="select-wrap";let s=document.createElement("select");s.className="select",this.langSelect=s,N.forEach(a=>{let o=document.createElement("option");o.value=a.value,o.textContent=a.label,a.value===this.settings.reply_language&&(o.selected=!0),s.appendChild(o)}),s.addEventListener("change",()=>{this.settings.reply_language=s.value});let i=document.createElement("span");return i.className="select-arrow",i.textContent="\u25BE",n.appendChild(s),n.appendChild(i),e.appendChild(n),e}buildNoteSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label";let n=document.createElement("span");n.textContent="\u5907\u6CE8";let s=document.createElement("span");s.textContent=`${this.settings.player_note.length}/50`,this.noteCounter=s,t.appendChild(n),t.appendChild(s),e.appendChild(t);let i=document.createElement("div");i.className="section-desc",i.textContent="\u5199\u4E0B\u4F60\u5BF9\u8FD9\u4E2A\u6E38\u620F\u7684\u5907\u6CE8\uFF0C\u4F1A\u663E\u793A\u5728\u5B58\u6863\u5217\u8868",e.appendChild(i);let a=document.createElement("div");a.className="note-wrap";let o=document.createElement("textarea");return o.className="note-input",o.placeholder="\u5907\u6CE8",o.maxLength=50,o.value=this.settings.player_note,this.noteInput=o,o.addEventListener("input",()=>{this.settings.player_note=o.value,this.noteCounter&&(this.noteCounter.textContent=`${o.value.length}/50`)}),a.appendChild(o),e.appendChild(a),e}buildDisplayModeSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u663E\u793A\u6A21\u5F0F",e.appendChild(t);let n=document.createElement("div");n.className="section-desc",n.textContent="\u7535\u8111\u6A21\u5F0F\u4F1A\u81EA\u52A8\u6A2A\u5C4F\u663E\u793A",e.appendChild(n);let s=document.createElement("div");s.className="segment",this.displayModeBtns=[];let i=[{value:"phone",label:"\u624B\u673A"},{value:"desktop",label:"\u7535\u8111"}];return i.forEach(a=>{let o=document.createElement("button");o.className=`segment-btn${a.value===this.displayMode?" active":""}`,o.textContent=a.label,o.addEventListener("click",()=>{this.displayMode=a.value;try{localStorage.setItem("storvia_display_mode",a.value)}catch{}this.displayModeBtns.forEach((d,p)=>{d.classList.toggle("active",i[p].value===a.value)});try{window.parent.postMessage({type:"storvia-orientation",mode:a.value==="desktop"?"landscape":"portrait"},"*")}catch{}}),this.displayModeBtns.push(o),s.appendChild(o)}),e.appendChild(s),e}buildSyncSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u540C\u6B65\u6E38\u620F\u8BBE\u5B9A",e.appendChild(t);let n=document.createElement("div");n.className=`sync-hint${this.hasWorldUpdate?" visible":""}`,n.textContent="\u26A0 \u4F5C\u8005\u5DF2\u66F4\u65B0\u6E38\u620F\u5185\u5BB9\uFF0C\u5EFA\u8BAE\u70B9\u51FB\u540C\u6B65",this.syncHint=n,e.appendChild(n);let s=document.createElement("div");s.className="sync-desc",s.textContent="\u5982\u679C\u6E38\u620F\u4F5C\u8005\u66F4\u65B0\u4E86\u5185\u5BB9\uFF0C\u70B9\u51FB\u6B64\u6309\u94AE\u53EF\u5C06\u6700\u65B0\u7684\u6E38\u620F\u8BBE\u5B9A\u548C\u5F00\u5E55\u5C0F\u5267\u573A\u540C\u6B65\u5230\u5F53\u524D\u5BF9\u8BDD",e.appendChild(s);let i=document.createElement("button");return i.className="sync-btn",this.syncBtn=i,this.renderSyncBtn(),i.addEventListener("click",async()=>{if(this.syncState!=="loading"){this.setSyncState("loading");try{await this.callbacks.onSync(),this.setSyncState("success"),this.hasWorldUpdate=!1,this.fabDot&&this.fabDot.classList.remove("visible"),this.syncHint&&this.syncHint.classList.remove("visible"),setTimeout(()=>this.setSyncState("idle"),3e3)}catch{this.setSyncState("error"),setTimeout(()=>this.setSyncState("idle"),4e3)}}}),e.appendChild(i),e}makeDivider(){let e=document.createElement("div");return e.className="divider",e}updateModelActive(){this.modelPanel&&this.modelPanel.querySelectorAll(".model-option").forEach((e,t)=>{e.classList.toggle("active",this.models[t].id===this.selectedModelId)})}setSyncState(e){this.syncState=e,this.renderSyncBtn()}renderSyncBtn(){if(!this.syncBtn)return;let e=this.syncState,t={idle:L,loading:H,success:O,error:B},n={idle:"\u540C\u6B65\u6700\u65B0\u8BBE\u5B9A",loading:"\u540C\u6B65\u4E2D...",success:"\u5DF2\u540C\u6B65",error:"\u540C\u6B65\u5931\u8D25"};this.syncBtn.className=`sync-btn${e==="success"||e==="error"?` ${e}`:""}`,this.syncBtn.disabled=e==="loading",this.syncBtn.innerHTML=`${t[e]}<span>${n[e]}</span>`}async handleSave(){if(!(!this.saveBtn||this.saveBtn.disabled)){this.saveBtn.disabled=!0,this.saveBtn.textContent="\u4FDD\u5B58\u4E2D...";try{await this.callbacks.onSettingsChange({temperature:this.settings.temperature,reply_length:this.settings.reply_length,reply_language:this.settings.reply_language,player_note:this.settings.player_note}),this.saveBtn.textContent="\u5DF2\u4FDD\u5B58",this.saveBtn.classList.add("saved"),setTimeout(()=>{this.saveBtn&&(this.saveBtn.textContent="\u4FDD\u5B58",this.saveBtn.classList.remove("saved"),this.saveBtn.disabled=!1)},2e3)}catch{this.saveBtn.textContent="\u4FDD\u5B58\u5931\u8D25",setTimeout(()=>{this.saveBtn&&(this.saveBtn.textContent="\u4FDD\u5B58",this.saveBtn.disabled=!1)},2e3)}}}};var f=class{constructor(e,t,n,s,i=!1,a){this.http=e;this.getGameId=t;this.getConversationId=n;this.getModelId=s;this.isPreview=i;this.previewConfig=a}updatePreviewConfig(e){this.previewConfig=e}async send(e){if(this.isPreview)return this.sendPreview(e);let t={game_id:this.getGameId(),conversation_id:this.getConversationId(),model_id:this.getModelId(),message:e.message,topic:e.topic,context:e.context,stream:e.stream??!1};if(e.stream){await this.http.stream("/chat",t,e.onChunk??(()=>{}),e.onDone??(()=>{}));return}return this.http.post("/chat",t)}async save(e){return this.isPreview?{id:`preview-${Date.now()}`}:this.http.post("/messages/save",{conversation_id:this.getConversationId(),role:e.role,content:e.content,topic:e.topic})}async generate(e){if(this.isPreview)return this.sendPreview({message:"",topic:e.topic,context:(e.context||"")+`
|
|
431
|
+
[\u6307\u4EE4] \u8BF7\u76F4\u63A5\u751F\u6210\u56DE\u590D\uFF0C\u65E0\u9700\u7528\u6237\u8F93\u5165`,stream:e.stream,onChunk:e.onChunk,onDone:e.onDone});let t={conversation_id:this.getConversationId(),model_id:this.getModelId(),topic:e.topic,context:e.context,stream:e.stream??!1,save:e.save??!0};if(e.stream){await this.http.stream("/generate",t,e.onChunk??(()=>{}),e.onDone??(()=>{}));return}return this.http.post("/generate",t)}async sendPreview(e){let t={model_id:this.getModelId(),message:e.message,topic:e.topic,context:e.context,prompt_config:this.previewConfig||{},history_messages:[]};if(e.stream){await this.http.stream("/preview",t,e.onChunk??(()=>{}),e.onDone??(()=>{}));return}let n="";return await this.http.stream("/preview",t,s=>{n+=s},()=>{}),{id:"",content:n}}};var k=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(e){return this.isPreview?{...this.mem[e]||{}}:this.http.get(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}/state`)}async update(e,t){if(this.isPreview){this.mem[e]={...this.mem[e]||{},...t};return}await this.http.patch(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}/state`,t)}},b=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem=[];this.state=new k(e,t,n)}async list(){return this.isPreview?this.mem.map(e=>({name:e.name,avatar:e.avatar})):this.http.get(`/conversations/${this.getConversationId()}/characters`)}async get(e){if(this.isPreview){let t=this.mem.find(n=>n.name===e);return{name:t?.name||e,avatar:t?.avatar}}return this.http.get(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}`)}async add(e){if(this.isPreview){this.mem.push(e);return}await this.http.post(`/conversations/${this.getConversationId()}/characters`,e)}async update(e,t){if(this.isPreview){let n=this.mem.find(s=>s.name===e);n&&Object.assign(n,t);return}await this.http.patch(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}`,t)}async remove(e){if(this.isPreview){this.mem=this.mem.filter(t=>t.name!==e);return}await this.http.delete(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}`)}async detail(e){if(this.isPreview){let t=this.mem.find(n=>n.name===e);return{name:e,avatar:t?.avatar,state:await this.state.get(e)}}return this.http.get(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}/detail`)}};var w=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem=[]}async list(){return this.isPreview?[...this.mem]:this.http.get(`/conversations/${this.getConversationId()}/relationships`)}async get(e,t){if(this.isPreview){let n=this.mem.find(s=>s.from===e&&s.to===t);if(n)return{...n};throw new Error("Relationship not found")}return this.http.get(`/conversations/${this.getConversationId()}/relationships/${encodeURIComponent(e)}/${encodeURIComponent(t)}`)}async add(e){if(this.isPreview){this.mem.push({...e});return}await this.http.post(`/conversations/${this.getConversationId()}/relationships`,e)}async update(e,t,n){if(this.isPreview){let s=this.mem.find(i=>i.from===e&&i.to===t);s?Object.assign(s,n):this.mem.push({from:e,to:t,...n});return}await this.http.patch(`/conversations/${this.getConversationId()}/relationships/${encodeURIComponent(e)}/${encodeURIComponent(t)}`,n)}async remove(e,t){if(this.isPreview){this.mem=this.mem.filter(n=>!(n.from===e&&n.to===t));return}await this.http.delete(`/conversations/${this.getConversationId()}/relationships/${encodeURIComponent(e)}/${encodeURIComponent(t)}`)}};var y=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem=[]}async list(e){if(this.isPreview)return e?this.mem.filter(n=>n.group===e):[...this.mem];let t=e?{group:e}:void 0;return this.http.get(`/conversations/${this.getConversationId()}/inventory`,t)}async add(e){if(this.isPreview){this.mem.push({...e});return}await this.http.post(`/conversations/${this.getConversationId()}/inventory`,e)}async update(e,t){if(this.isPreview){let n=this.mem.find(s=>s.name===e);n&&Object.assign(n,t);return}await this.http.patch(`/conversations/${this.getConversationId()}/inventory/${encodeURIComponent(e)}`,t)}async remove(e){if(this.isPreview){this.mem=this.mem.filter(t=>t.name!==e);return}await this.http.delete(`/conversations/${this.getConversationId()}/inventory/${encodeURIComponent(e)}`)}};var C=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(){return this.isPreview?{...this.mem}:this.http.get(`/conversations/${this.getConversationId()}/world`)}async update(e){if(this.isPreview){Object.assign(this.mem,e);return}await this.http.patch(`/conversations/${this.getConversationId()}/world`,e)}};var P=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(e){if(this.isPreview)return e?{...this.mem[e]||{}}:{...this.mem};let t=e?{group:e}:void 0;return this.http.get(`/conversations/${this.getConversationId()}/player/state`,t)}async update(e,t){if(this.isPreview){this.mem[e]={...this.mem[e]||{},...t};return}await this.http.patch(`/conversations/${this.getConversationId()}/player/state`,{group:e,values:t})}},x=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.memPersona={name:""};this.state=new P(e,t,n)}async get(){return this.isPreview?{...this.memPersona}:this.http.get(`/conversations/${this.getConversationId()}/player`)}};var I=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(e){if(this.isPreview)return e!==void 0?this.mem[e]:{...this.mem};let t=await this.http.get(`/conversations/${this.getConversationId()}/custom`);return e!==void 0?t[e]:t}async set(e){if(this.isPreview){for(let[t,n]of Object.entries(e))typeof n=="object"&&n!==null&&!Array.isArray(n)&&typeof this.mem[t]=="object"&&this.mem[t]!==null?this.mem[t]={...this.mem[t],...n}:this.mem[t]=n;return}await this.http.patch(`/conversations/${this.getConversationId()}/custom`,e)}async delete(e){if(this.isPreview){delete this.mem[e];return}await this.http.delete(`/conversations/${this.getConversationId()}/custom/${encodeURIComponent(e)}`)}async setGroupField(e,t,n){if(this.isPreview){(!this.mem[e]||typeof this.mem[e]!="object")&&(this.mem[e]={}),this.mem[e][t]=n;return}await this.http.patch(`/conversations/${this.getConversationId()}/custom/${encodeURIComponent(e)}/${encodeURIComponent(t)}`,{value:n})}async deleteGroupField(e,t){if(this.isPreview){if(typeof this.mem[e]=="object"&&this.mem[e]!==null){let n=this.mem[e];delete n[t],Object.keys(n).length===0&&delete this.mem[e]}return}await this.http.delete(`/conversations/${this.getConversationId()}/custom/${encodeURIComponent(e)}/${encodeURIComponent(t)}`)}};var A="/functions/v1/world-engine/v1";function U(){return window.__STORVIA__||{}}function D(c=5e3){let e=U();return e.apiBase?Promise.resolve(e):new Promise((t,n)=>{let s=setTimeout(()=>{window.removeEventListener("message",i),n(new l("NOT_INITIALIZED","Timed out waiting for __STORVIA__ config from parent frame."))},c);function i(a){let o=null;if(a.data?.type==="storvia-init"&&a.data.apiBase?o={apiBase:a.data.apiBase,gameId:a.data.gameId,accessToken:a.data.accessToken}:a.data?.type==="storvia-preview-init"&&a.data.config&&(o=a.data.config),o&&o.apiBase){clearTimeout(s),window.removeEventListener("message",i);let d=window;d.__STORVIA__=o,t(o)}}window.addEventListener("message",i)})}var R=class{constructor(){this.baseUrl="";this.gameId="";this.conversationId="";this.modelId="";this.accessToken="";this.initialized=!1;this.isPreview=!1;this.previewConfig=void 0}async init(){let e=await D(),t=e.apiBase;if(!t)throw new l("NOT_INITIALIZED","window.__STORVIA__.apiBase is not set. The platform must inject it before SDK init.");if(this.baseUrl=`${t}${A}`,this.isPreview=!!e.preview,this.previewConfig=e.previewConfig,!e.gameId)throw new l("GAME_NOT_FOUND","window.__STORVIA__.gameId is not set. The platform must inject it before SDK init.");if(this.gameId=e.gameId,!e.accessToken)throw new l("UNAUTHORIZED","window.__STORVIA__.accessToken is not set. The platform must inject the user token before SDK init.");if(this.accessToken=e.accessToken,this.http=new S(this.baseUrl,this.accessToken,!!e.debug),this.isPreview){this.conversationId="00000000-0000-0000-0000-000000000000";let r=e.models||[];this.modelId=e.selectedModelId||r[0]?.id||"",this.settingsSheet=new v({onModelChange:E=>{this.modelId=E},onSettingsChange:()=>Promise.resolve(),onSync:()=>Promise.resolve()}),r.length>0&&this.settingsSheet.mount(r,this.modelId,{temperature:1,reply_length:"medium",reply_language:"zh-CN",player_note:""},!1);let m=()=>this.gameId,h=()=>this.conversationId,u=()=>this.modelId;this.chat=new f(this.http,m,h,u,!0,this.previewConfig),this.characters=new b(this.http,h,!0),this.relationships=new w(this.http,h,!0),this.inventory=new y(this.http,h,!0),this.world=new C(this.http,h,!0),this.player=new x(this.http,h,!0),this.custom=new I(this.http,h,!0),this.initialized=!0,this.listenForPreviewMessages();return}let n=await this.authenticate();this.conversationId=n.conversationId;let s=n.models||[],i=this.getSavedModelId();this.modelId=i&&s.some(r=>r.id===i)?i:s[0]?.id||"";let a=n.settings||{temperature:1,reply_length:"medium",reply_language:"zh-CN",player_note:""};this.settingsSheet=new v({onModelChange:r=>{this.modelId=r},onSettingsChange:r=>this.saveSettings(r),onSync:()=>this.syncSettings()}),this.settingsSheet.mount(s,this.modelId,a,n.hasWorldUpdate??!1);let o=()=>this.gameId,d=()=>this.conversationId,p=()=>this.modelId;this.chat=new f(this.http,o,d,p),this.characters=new b(this.http,d),this.relationships=new w(this.http,d),this.inventory=new y(this.http,d),this.world=new C(this.http,d),this.player=new x(this.http,d),this.custom=new I(this.http,d),this.initialized=!0}listenForPreviewMessages(){window.addEventListener("message",e=>{!e.data||e.data.type!=="storvia-preview"||(e.data.action==="set-model"&&(this.modelId=e.data.modelId||""),e.data.action==="update-config"&&(this.previewConfig=e.data.previewConfig,this.chat&&this.chat.updatePreviewConfig(this.previewConfig)))})}async saveSettings(e){await this.http.patch("/settings",{conversation_id:this.conversationId,...e})}async syncSettings(){await this.http.post("/sync",{conversation_id:this.conversationId})}async authenticate(){let e=await fetch(`${this.baseUrl}/auth`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.accessToken}`},body:JSON.stringify({game_id:this.gameId})});if(!e.ok)throw e.status===401?new l("UNAUTHORIZED","User not authenticated"):e.status===404?new l("GAME_NOT_FOUND","Game not found or not published"):new l("INTERNAL_ERROR",`Auth failed: HTTP ${e.status}`);let t=await e.json();return t.data??t}getSavedModelId(){try{return localStorage.getItem("storvia_model_id")||""}catch{return""}}};export{l as StorviaError,R as StorviaSDK};
|
package/dist/index.umd.js
CHANGED
|
@@ -427,5 +427,6 @@
|
|
|
427
427
|
to { transform: rotate(360deg); }
|
|
428
428
|
}
|
|
429
429
|
.spin { animation: spin 0.8s linear infinite; }
|
|
430
|
-
`,D='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg>',j='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="23 4 23 10 17 10"/><polyline points="1 20 1 14 7 14"/><path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"/></svg>',z='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="spin"><path d="M21 12a9 9 0 1 1-6.219-8.56"/></svg>',G='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>',F='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg>',v=class{constructor(e){this.selectedModelId="";this.models=[];this.modelPanel=null;this.settings={temperature:1,reply_length:"medium",reply_language:"zh-CN",player_note:""};this.hasWorldUpdate=!1;this.fabDot=null;this.syncHint=null;this.displayMode="phone";this.displayModeBtns=[];this.tempValueEl=null;this.tempSlider=null;this.segmentBtns=[];this.langSelect=null;this.noteInput=null;this.noteCounter=null;this.saveBtn=null;this.syncBtn=null;this.syncState="idle";this.panel=null;this.callbacks=e,this.host=document.createElement("div"),this.host.id="storvia-settings",this.shadowRoot=this.host.attachShadow({mode:"closed"})}mount(e,t,n,s=!1){this.models=e,this.selectedModelId=t,this.settings={...n},this.hasWorldUpdate=s;try{let i=localStorage.getItem("storvia_display_mode");(i==="phone"||i==="desktop")&&(this.displayMode=i)}catch{}document.body.appendChild(this.host),this.render()}unmount(){this.host.remove()}getSelectedModelId(){return this.selectedModelId}getSettings(){return{...this.settings}}render(){let e=document.createElement("style");e.textContent=U;let t=document.createElement("div"),n=document.createElement("div");n.className="fab-wrap";let s=document.createElement("button");s.className="fab",s.innerHTML=D,s.title="\u6E38\u620F\u8BBE\u7F6E";let i=document.createElement("div");i.className=`fab-dot${this.hasWorldUpdate?" visible":""}`,this.fabDot=i,n.appendChild(s),n.appendChild(i);let a=document.createElement("div");a.className="panel",this.panel=a;let o=document.createElement("div");o.className="panel-header";let d=document.createElement("div");d.className="panel-title",d.textContent="\u6E38\u620F\u8BBE\u7F6E";let p=document.createElement("button");p.className="close-btn",p.innerHTML="\u2715",p.addEventListener("click",()=>a.classList.remove("open")),o.appendChild(d),o.appendChild(p),a.appendChild(o);let r=document.createElement("div");r.className="panel-body",this.models.length>0&&(r.appendChild(this.buildModelSection()),r.appendChild(this.makeDivider())),r.appendChild(this.buildTemperatureSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildReplyLengthSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildReplyLanguageSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildNoteSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildDisplayModeSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildSyncSection());let m=document.createElement("div");m.className="save-row";let h=document.createElement("button");h.className="save-btn",h.textContent="\u4FDD\u5B58",this.saveBtn=h,h.addEventListener("click",()=>this.handleSave()),m.appendChild(h),r.appendChild(m),a.appendChild(r),s.addEventListener("click",()=>a.classList.toggle("open")),document.addEventListener("click",u=>{this.host.contains(u.target)||a.classList.remove("open")}),t.appendChild(a),t.appendChild(n),this.shadowRoot.appendChild(e),this.shadowRoot.appendChild(t)}buildModelSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="AI \u6A21\u578B",e.appendChild(t);let n=document.createElement("div");return n.className="model-list",this.modelPanel=n,this.models.forEach(s=>{let i=document.createElement("button");i.className=`model-option${s.id===this.selectedModelId?" active":""}`,i.innerHTML=`<span class="dot"></span><span>${s.name}</span>`,i.addEventListener("click",()=>{this.selectedModelId=s.id,this.callbacks.onModelChange(s.id);try{localStorage.setItem("storvia_model_id",s.id)}catch{}this.updateModelActive()}),n.appendChild(i)}),e.appendChild(n),e}buildTemperatureSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label";let n=document.createElement("span");n.textContent="\u6E29\u5EA6";let s=document.createElement("span");s.className="value",s.textContent=this.settings.temperature.toFixed(1),this.tempValueEl=s,t.appendChild(n),t.appendChild(s),e.appendChild(t);let i=document.createElement("div");i.className="section-desc",i.textContent="\u66F4\u4F4E\u7684\u6E29\u5EA6\u4F1A\u8BA9 AI \u56DE\u590D\u66F4\u7A33\u5B9A\u3001\u7406\u667A\uFF1B\u66F4\u9AD8\u7684\u6E29\u5EA6\u4F1A\u8BA9 AI \u7684\u56DE\u590D\u66F4\u5177\u591A\u6837\u6027\u548C\u968F\u673A\u6027",e.appendChild(i);let a=document.createElement("div");a.className="slider-wrap";let o=document.createElement("input");o.type="range",o.className="slider",o.min="0",o.max="2",o.step="0.1",o.value=String(this.settings.temperature),this.tempSlider=o,o.addEventListener("input",()=>{let p=parseFloat(o.value);this.settings.temperature=p,this.tempValueEl&&(this.tempValueEl.textContent=p.toFixed(1))}),a.appendChild(o);let d=document.createElement("div");return d.className="slider-labels",d.innerHTML="<span>\u7CBE\u786E</span><span>\u521B\u610F</span>",a.appendChild(d),e.appendChild(a),e}buildReplyLengthSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u56DE\u590D\u957F\u5EA6",e.appendChild(t);let n=document.createElement("div");return n.className="segment",this.segmentBtns=[],N.forEach(s=>{let i=document.createElement("button");i.className=`segment-btn${s.value===this.settings.reply_length?" active":""}`,i.textContent=s.label,i.addEventListener("click",()=>{this.settings.reply_length=s.value,this.segmentBtns.forEach((a,o)=>{a.classList.toggle("active",N[o].value===s.value)})}),this.segmentBtns.push(i),n.appendChild(i)}),e.appendChild(n),e}buildReplyLanguageSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u56DE\u590D\u8BED\u8A00",e.appendChild(t);let n=document.createElement("div");n.className="select-wrap";let s=document.createElement("select");s.className="select",this.langSelect=s,A.forEach(a=>{let o=document.createElement("option");o.value=a.value,o.textContent=a.label,a.value===this.settings.reply_language&&(o.selected=!0),s.appendChild(o)}),s.addEventListener("change",()=>{this.settings.reply_language=s.value});let i=document.createElement("span");return i.className="select-arrow",i.textContent="\u25BE",n.appendChild(s),n.appendChild(i),e.appendChild(n),e}buildNoteSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label";let n=document.createElement("span");n.textContent="\u5907\u6CE8";let s=document.createElement("span");s.textContent=`${this.settings.player_note.length}/50`,this.noteCounter=s,t.appendChild(n),t.appendChild(s),e.appendChild(t);let i=document.createElement("div");i.className="section-desc",i.textContent="\u5199\u4E0B\u4F60\u5BF9\u8FD9\u4E2A\u6E38\u620F\u7684\u5907\u6CE8\uFF0C\u4F1A\u663E\u793A\u5728\u5B58\u6863\u5217\u8868",e.appendChild(i);let a=document.createElement("div");a.className="note-wrap";let o=document.createElement("textarea");return o.className="note-input",o.placeholder="\u5907\u6CE8",o.maxLength=50,o.value=this.settings.player_note,this.noteInput=o,o.addEventListener("input",()=>{this.settings.player_note=o.value,this.noteCounter&&(this.noteCounter.textContent=`${o.value.length}/50`)}),a.appendChild(o),e.appendChild(a),e}buildDisplayModeSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u663E\u793A\u6A21\u5F0F",e.appendChild(t);let n=document.createElement("div");n.className="section-desc",n.textContent="\u7535\u8111\u6A21\u5F0F\u4F1A\u81EA\u52A8\u6A2A\u5C4F\u663E\u793A",e.appendChild(n);let s=document.createElement("div");s.className="segment",this.displayModeBtns=[];let i=[{value:"phone",label:"\u624B\u673A"},{value:"desktop",label:"\u7535\u8111"}];return i.forEach(a=>{let o=document.createElement("button");o.className=`segment-btn${a.value===this.displayMode?" active":""}`,o.textContent=a.label,o.addEventListener("click",()=>{this.displayMode=a.value;try{localStorage.setItem("storvia_display_mode",a.value)}catch{}this.displayModeBtns.forEach((d,p)=>{d.classList.toggle("active",i[p].value===a.value)});try{window.parent.postMessage({type:"storvia-orientation",mode:a.value==="desktop"?"landscape":"portrait"},"*")}catch{}}),this.displayModeBtns.push(o),s.appendChild(o)}),e.appendChild(s),e}buildSyncSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u540C\u6B65\u6E38\u620F\u8BBE\u5B9A",e.appendChild(t);let n=document.createElement("div");n.className=`sync-hint${this.hasWorldUpdate?" visible":""}`,n.textContent="\u26A0 \u4F5C\u8005\u5DF2\u66F4\u65B0\u6E38\u620F\u5185\u5BB9\uFF0C\u5EFA\u8BAE\u70B9\u51FB\u540C\u6B65",this.syncHint=n,e.appendChild(n);let s=document.createElement("div");s.className="sync-desc",s.textContent="\u5982\u679C\u6E38\u620F\u4F5C\u8005\u66F4\u65B0\u4E86\u5185\u5BB9\uFF0C\u70B9\u51FB\u6B64\u6309\u94AE\u53EF\u5C06\u6700\u65B0\u7684\u6E38\u620F\u8BBE\u5B9A\u548C\u5F00\u5E55\u5C0F\u5267\u573A\u540C\u6B65\u5230\u5F53\u524D\u5BF9\u8BDD",e.appendChild(s);let i=document.createElement("button");return i.className="sync-btn",this.syncBtn=i,this.renderSyncBtn(),i.addEventListener("click",async()=>{if(this.syncState!=="loading"){this.setSyncState("loading");try{await this.callbacks.onSync(),this.setSyncState("success"),this.hasWorldUpdate=!1,this.fabDot&&this.fabDot.classList.remove("visible"),this.syncHint&&this.syncHint.classList.remove("visible"),setTimeout(()=>this.setSyncState("idle"),3e3)}catch{this.setSyncState("error"),setTimeout(()=>this.setSyncState("idle"),4e3)}}}),e.appendChild(i),e}makeDivider(){let e=document.createElement("div");return e.className="divider",e}updateModelActive(){this.modelPanel&&this.modelPanel.querySelectorAll(".model-option").forEach((e,t)=>{e.classList.toggle("active",this.models[t].id===this.selectedModelId)})}setSyncState(e){this.syncState=e,this.renderSyncBtn()}renderSyncBtn(){if(!this.syncBtn)return;let e=this.syncState,t={idle:j,loading:z,success:G,error:F},n={idle:"\u540C\u6B65\u6700\u65B0\u8BBE\u5B9A",loading:"\u540C\u6B65\u4E2D...",success:"\u5DF2\u540C\u6B65",error:"\u540C\u6B65\u5931\u8D25"};this.syncBtn.className=`sync-btn${e==="success"||e==="error"?` ${e}`:""}`,this.syncBtn.disabled=e==="loading",this.syncBtn.innerHTML=`${t[e]}<span>${n[e]}</span>`}async handleSave(){if(!(!this.saveBtn||this.saveBtn.disabled)){this.saveBtn.disabled=!0,this.saveBtn.textContent="\u4FDD\u5B58\u4E2D...";try{await this.callbacks.onSettingsChange({temperature:this.settings.temperature,reply_length:this.settings.reply_length,reply_language:this.settings.reply_language,player_note:this.settings.player_note}),this.saveBtn.textContent="\u5DF2\u4FDD\u5B58",this.saveBtn.classList.add("saved"),setTimeout(()=>{this.saveBtn&&(this.saveBtn.textContent="\u4FDD\u5B58",this.saveBtn.classList.remove("saved"),this.saveBtn.disabled=!1)},2e3)}catch{this.saveBtn.textContent="\u4FDD\u5B58\u5931\u8D25",setTimeout(()=>{this.saveBtn&&(this.saveBtn.textContent="\u4FDD\u5B58",this.saveBtn.disabled=!1)},2e3)}}}};var f=class{constructor(e,t,n,s,i=!1,a){this.http=e;this.getGameId=t;this.getConversationId=n;this.getModelId=s;this.isPreview=i;this.previewConfig=a}updatePreviewConfig(e){this.previewConfig=e}async send(e){if(this.isPreview)return this.sendPreview(e);let t={game_id:this.getGameId(),conversation_id:this.getConversationId(),model_id:this.getModelId(),message:e.message,topic:e.topic,context:e.context,stream:e.stream??!1};if(e.stream){await this.http.stream("/chat",t,e.onChunk??(()=>{}),e.onDone??(()=>{}));return}return this.http.post("/chat",t)}async save(e){return this.http.post("/messages/save",{conversation_id:this.getConversationId(),role:e.role,content:e.content,topic:e.topic})}async generate(e){let t={conversation_id:this.getConversationId(),model_id:this.getModelId(),topic:e.topic,context:e.context,stream:e.stream??!1,save:e.save??!0};if(e.stream){await this.http.stream("/generate",t,e.onChunk??(()=>{}),e.onDone??(()=>{}));return}return this.http.post("/generate",t)}async sendPreview(e){let t={model_id:this.getModelId(),message:e.message,topic:e.topic,context:e.context,prompt_config:this.previewConfig||{},history_messages:[]};if(e.stream){await this.http.stream("/preview",t,e.onChunk??(()=>{}),e.onDone??(()=>{}));return}let n="";return await this.http.stream("/preview",t,s=>{n+=s},()=>{}),{id:"",content:n}}};var R=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(e){return this.isPreview?{...this.mem[e]||{}}:this.http.get(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}/state`)}async update(e,t){if(this.isPreview){this.mem[e]={...this.mem[e]||{},...t};return}await this.http.patch(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}/state`,t)}},b=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem=[];this.state=new R(e,t,n)}async list(){return this.isPreview?this.mem.map(e=>({name:e.name,avatar:e.avatar})):this.http.get(`/conversations/${this.getConversationId()}/characters`)}async get(e){if(this.isPreview){let t=this.mem.find(n=>n.name===e);return{name:t?.name||e,avatar:t?.avatar}}return this.http.get(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}`)}async add(e){if(this.isPreview){this.mem.push(e);return}await this.http.post(`/conversations/${this.getConversationId()}/characters`,e)}async update(e,t){if(this.isPreview){let n=this.mem.find(s=>s.name===e);n&&Object.assign(n,t);return}await this.http.patch(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}`,t)}async remove(e){if(this.isPreview){this.mem=this.mem.filter(t=>t.name!==e);return}await this.http.delete(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}`)}async detail(e){if(this.isPreview){let t=this.mem.find(n=>n.name===e);return{name:e,avatar:t?.avatar,state:await this.state.get(e)}}return this.http.get(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}/detail`)}};var w=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem=[]}async list(){return this.isPreview?[...this.mem]:this.http.get(`/conversations/${this.getConversationId()}/relationships`)}async get(e,t){if(this.isPreview){let n=this.mem.find(s=>s.from===e&&s.to===t);if(n)return{...n};throw new Error("Relationship not found")}return this.http.get(`/conversations/${this.getConversationId()}/relationships/${encodeURIComponent(e)}/${encodeURIComponent(t)}`)}async add(e){if(this.isPreview){this.mem.push({...e});return}await this.http.post(`/conversations/${this.getConversationId()}/relationships`,e)}async update(e,t,n){if(this.isPreview){let s=this.mem.find(i=>i.from===e&&i.to===t);s?Object.assign(s,n):this.mem.push({from:e,to:t,...n});return}await this.http.patch(`/conversations/${this.getConversationId()}/relationships/${encodeURIComponent(e)}/${encodeURIComponent(t)}`,n)}async remove(e,t){if(this.isPreview){this.mem=this.mem.filter(n=>!(n.from===e&&n.to===t));return}await this.http.delete(`/conversations/${this.getConversationId()}/relationships/${encodeURIComponent(e)}/${encodeURIComponent(t)}`)}};var y=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem=[]}async list(e){if(this.isPreview)return e?this.mem.filter(n=>n.group===e):[...this.mem];let t=e?{group:e}:void 0;return this.http.get(`/conversations/${this.getConversationId()}/inventory`,t)}async add(e){if(this.isPreview){this.mem.push({...e});return}await this.http.post(`/conversations/${this.getConversationId()}/inventory`,e)}async update(e,t){if(this.isPreview){let n=this.mem.find(s=>s.name===e);n&&Object.assign(n,t);return}await this.http.patch(`/conversations/${this.getConversationId()}/inventory/${encodeURIComponent(e)}`,t)}async remove(e){if(this.isPreview){this.mem=this.mem.filter(t=>t.name!==e);return}await this.http.delete(`/conversations/${this.getConversationId()}/inventory/${encodeURIComponent(e)}`)}};var C=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(){return this.isPreview?{...this.mem}:this.http.get(`/conversations/${this.getConversationId()}/world`)}async update(e){if(this.isPreview){Object.assign(this.mem,e);return}await this.http.patch(`/conversations/${this.getConversationId()}/world`,e)}};var T=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(e){if(this.isPreview)return e?{...this.mem[e]||{}}:{...this.mem};let t=e?{group:e}:void 0;return this.http.get(`/conversations/${this.getConversationId()}/player/state`,t)}async update(e,t){if(this.isPreview){this.mem[e]={...this.mem[e]||{},...t};return}await this.http.patch(`/conversations/${this.getConversationId()}/player/state`,{group:e,values:t})}},x=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.memPersona={name:""};this.state=new T(e,t,n)}async get(){return this.isPreview?{...this.memPersona}:this.http.get(`/conversations/${this.getConversationId()}/player`)}};var I=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(e){if(this.isPreview)return e!==void 0?this.mem[e]:{...this.mem};let t=await this.http.get(`/conversations/${this.getConversationId()}/custom`);return e!==void 0?t[e]:t}async set(e){if(this.isPreview){for(let[t,n]of Object.entries(e))typeof n=="object"&&n!==null&&!Array.isArray(n)&&typeof this.mem[t]=="object"&&this.mem[t]!==null?this.mem[t]={...this.mem[t],...n}:this.mem[t]=n;return}await this.http.patch(`/conversations/${this.getConversationId()}/custom`,e)}async delete(e){if(this.isPreview){delete this.mem[e];return}await this.http.delete(`/conversations/${this.getConversationId()}/custom/${encodeURIComponent(e)}`)}async setGroupField(e,t,n){if(this.isPreview){(!this.mem[e]||typeof this.mem[e]!="object")&&(this.mem[e]={}),this.mem[e][t]=n;return}await this.http.patch(`/conversations/${this.getConversationId()}/custom/${encodeURIComponent(e)}/${encodeURIComponent(t)}`,{value:n})}async deleteGroupField(e,t){if(this.isPreview){if(typeof this.mem[e]=="object"&&this.mem[e]!==null){let n=this.mem[e];delete n[t],Object.keys(n).length===0&&delete this.mem[e]}return}await this.http.delete(`/conversations/${this.getConversationId()}/custom/${encodeURIComponent(e)}/${encodeURIComponent(t)}`)}};var V="/functions/v1/world-engine/v1";function W(){return window.__STORVIA__||{}}function J(l=5e3){let e=W();return e.apiBase?Promise.resolve(e):new Promise((t,n)=>{let s=setTimeout(()=>{window.removeEventListener("message",i),n(new c("NOT_INITIALIZED","Timed out waiting for __STORVIA__ config from parent frame."))},l);function i(a){let o=null;if(a.data?.type==="storvia-init"&&a.data.apiBase?o={apiBase:a.data.apiBase,gameId:a.data.gameId,accessToken:a.data.accessToken}:a.data?.type==="storvia-preview-init"&&a.data.config&&(o=a.data.config),o&&o.apiBase){clearTimeout(s),window.removeEventListener("message",i);let d=window;d.__STORVIA__=o,t(o)}}window.addEventListener("message",i)})}var k=class{constructor(){this.baseUrl="";this.gameId="";this.conversationId="";this.modelId="";this.accessToken="";this.initialized=!1;this.isPreview=!1;this.previewConfig=void 0}async init(){let e=await J(),t=e.apiBase;if(!t)throw new c("NOT_INITIALIZED","window.__STORVIA__.apiBase is not set. The platform must inject it before SDK init.");if(this.baseUrl=`${t}${V}`,this.isPreview=!!e.preview,this.previewConfig=e.previewConfig,!e.gameId)throw new c("GAME_NOT_FOUND","window.__STORVIA__.gameId is not set. The platform must inject it before SDK init.");if(this.gameId=e.gameId,!e.accessToken)throw new c("UNAUTHORIZED","window.__STORVIA__.accessToken is not set. The platform must inject the user token before SDK init.");if(this.accessToken=e.accessToken,this.http=new S(this.baseUrl,this.accessToken,!!e.debug),this.isPreview){this.conversationId="00000000-0000-0000-0000-000000000000";let r=e.models||[];this.modelId=e.selectedModelId||r[0]?.id||"",this.settingsSheet=new v({onModelChange:E=>{this.modelId=E},onSettingsChange:()=>Promise.resolve(),onSync:()=>Promise.resolve()}),r.length>0&&this.settingsSheet.mount(r,this.modelId,{temperature:1,reply_length:"medium",reply_language:"zh-CN",player_note:""},!1);let m=()=>this.gameId,h=()=>this.conversationId,u=()=>this.modelId;this.chat=new f(this.http,m,h,u,!0,this.previewConfig),this.characters=new b(this.http,h,!0),this.relationships=new w(this.http,h,!0),this.inventory=new y(this.http,h,!0),this.world=new C(this.http,h,!0),this.player=new x(this.http,h,!0),this.custom=new I(this.http,h,!0),this.initialized=!0,this.listenForPreviewMessages();return}let n=await this.authenticate();this.conversationId=n.conversationId;let s=n.models||[],i=this.getSavedModelId();this.modelId=i&&s.some(r=>r.id===i)?i:s[0]?.id||"";let a=n.settings||{temperature:1,reply_length:"medium",reply_language:"zh-CN",player_note:""};this.settingsSheet=new v({onModelChange:r=>{this.modelId=r},onSettingsChange:r=>this.saveSettings(r),onSync:()=>this.syncSettings()}),this.settingsSheet.mount(s,this.modelId,a,n.hasWorldUpdate??!1);let o=()=>this.gameId,d=()=>this.conversationId,p=()=>this.modelId;this.chat=new f(this.http,o,d,p),this.characters=new b(this.http,d),this.relationships=new w(this.http,d),this.inventory=new y(this.http,d),this.world=new C(this.http,d),this.player=new x(this.http,d),this.custom=new I(this.http,d),this.initialized=!0}listenForPreviewMessages(){window.addEventListener("message",e=>{!e.data||e.data.type!=="storvia-preview"||(e.data.action==="set-model"&&(this.modelId=e.data.modelId||""),e.data.action==="update-config"&&(this.previewConfig=e.data.previewConfig,this.chat&&this.chat.updatePreviewConfig(this.previewConfig)))})}async saveSettings(e){await this.http.patch("/settings",{conversation_id:this.conversationId,...e})}async syncSettings(){await this.http.post("/sync",{conversation_id:this.conversationId})}async authenticate(){let e=await fetch(`${this.baseUrl}/auth`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.accessToken}`},body:JSON.stringify({game_id:this.gameId})});if(!e.ok)throw e.status===401?new c("UNAUTHORIZED","User not authenticated"):e.status===404?new c("GAME_NOT_FOUND","Game not found or not published"):new c("INTERNAL_ERROR",`Auth failed: HTTP ${e.status}`);let t=await e.json();return t.data??t}getSavedModelId(){try{return localStorage.getItem("storvia_model_id")||""}catch{return""}}};return B(Y);})();
|
|
430
|
+
`,D='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg>',j='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="23 4 23 10 17 10"/><polyline points="1 20 1 14 7 14"/><path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"/></svg>',z='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="spin"><path d="M21 12a9 9 0 1 1-6.219-8.56"/></svg>',G='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>',F='<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg>',v=class{constructor(e){this.selectedModelId="";this.models=[];this.modelPanel=null;this.settings={temperature:1,reply_length:"medium",reply_language:"zh-CN",player_note:""};this.hasWorldUpdate=!1;this.fabDot=null;this.syncHint=null;this.displayMode="phone";this.displayModeBtns=[];this.tempValueEl=null;this.tempSlider=null;this.segmentBtns=[];this.langSelect=null;this.noteInput=null;this.noteCounter=null;this.saveBtn=null;this.syncBtn=null;this.syncState="idle";this.panel=null;this.callbacks=e,this.host=document.createElement("div"),this.host.id="storvia-settings",this.shadowRoot=this.host.attachShadow({mode:"closed"})}mount(e,t,n,s=!1){this.models=e,this.selectedModelId=t,this.settings={...n},this.hasWorldUpdate=s;try{let i=localStorage.getItem("storvia_display_mode");(i==="phone"||i==="desktop")&&(this.displayMode=i)}catch{}document.body.appendChild(this.host),this.render()}unmount(){this.host.remove()}getSelectedModelId(){return this.selectedModelId}getSettings(){return{...this.settings}}render(){let e=document.createElement("style");e.textContent=U;let t=document.createElement("div"),n=document.createElement("div");n.className="fab-wrap";let s=document.createElement("button");s.className="fab",s.innerHTML=D,s.title="\u6E38\u620F\u8BBE\u7F6E";let i=document.createElement("div");i.className=`fab-dot${this.hasWorldUpdate?" visible":""}`,this.fabDot=i,n.appendChild(s),n.appendChild(i);let a=document.createElement("div");a.className="panel",this.panel=a;let o=document.createElement("div");o.className="panel-header";let d=document.createElement("div");d.className="panel-title",d.textContent="\u6E38\u620F\u8BBE\u7F6E";let p=document.createElement("button");p.className="close-btn",p.innerHTML="\u2715",p.addEventListener("click",()=>a.classList.remove("open")),o.appendChild(d),o.appendChild(p),a.appendChild(o);let r=document.createElement("div");r.className="panel-body",this.models.length>0&&(r.appendChild(this.buildModelSection()),r.appendChild(this.makeDivider())),r.appendChild(this.buildTemperatureSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildReplyLengthSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildReplyLanguageSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildNoteSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildDisplayModeSection()),r.appendChild(this.makeDivider()),r.appendChild(this.buildSyncSection());let m=document.createElement("div");m.className="save-row";let h=document.createElement("button");h.className="save-btn",h.textContent="\u4FDD\u5B58",this.saveBtn=h,h.addEventListener("click",()=>this.handleSave()),m.appendChild(h),r.appendChild(m),a.appendChild(r),s.addEventListener("click",()=>a.classList.toggle("open")),document.addEventListener("click",u=>{this.host.contains(u.target)||a.classList.remove("open")}),t.appendChild(a),t.appendChild(n),this.shadowRoot.appendChild(e),this.shadowRoot.appendChild(t)}buildModelSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="AI \u6A21\u578B",e.appendChild(t);let n=document.createElement("div");return n.className="model-list",this.modelPanel=n,this.models.forEach(s=>{let i=document.createElement("button");i.className=`model-option${s.id===this.selectedModelId?" active":""}`,i.innerHTML=`<span class="dot"></span><span>${s.name}</span>`,i.addEventListener("click",()=>{this.selectedModelId=s.id,this.callbacks.onModelChange(s.id);try{localStorage.setItem("storvia_model_id",s.id)}catch{}this.updateModelActive()}),n.appendChild(i)}),e.appendChild(n),e}buildTemperatureSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label";let n=document.createElement("span");n.textContent="\u6E29\u5EA6";let s=document.createElement("span");s.className="value",s.textContent=this.settings.temperature.toFixed(1),this.tempValueEl=s,t.appendChild(n),t.appendChild(s),e.appendChild(t);let i=document.createElement("div");i.className="section-desc",i.textContent="\u66F4\u4F4E\u7684\u6E29\u5EA6\u4F1A\u8BA9 AI \u56DE\u590D\u66F4\u7A33\u5B9A\u3001\u7406\u667A\uFF1B\u66F4\u9AD8\u7684\u6E29\u5EA6\u4F1A\u8BA9 AI \u7684\u56DE\u590D\u66F4\u5177\u591A\u6837\u6027\u548C\u968F\u673A\u6027",e.appendChild(i);let a=document.createElement("div");a.className="slider-wrap";let o=document.createElement("input");o.type="range",o.className="slider",o.min="0",o.max="2",o.step="0.1",o.value=String(this.settings.temperature),this.tempSlider=o,o.addEventListener("input",()=>{let p=parseFloat(o.value);this.settings.temperature=p,this.tempValueEl&&(this.tempValueEl.textContent=p.toFixed(1))}),a.appendChild(o);let d=document.createElement("div");return d.className="slider-labels",d.innerHTML="<span>\u7CBE\u786E</span><span>\u521B\u610F</span>",a.appendChild(d),e.appendChild(a),e}buildReplyLengthSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u56DE\u590D\u957F\u5EA6",e.appendChild(t);let n=document.createElement("div");return n.className="segment",this.segmentBtns=[],N.forEach(s=>{let i=document.createElement("button");i.className=`segment-btn${s.value===this.settings.reply_length?" active":""}`,i.textContent=s.label,i.addEventListener("click",()=>{this.settings.reply_length=s.value,this.segmentBtns.forEach((a,o)=>{a.classList.toggle("active",N[o].value===s.value)})}),this.segmentBtns.push(i),n.appendChild(i)}),e.appendChild(n),e}buildReplyLanguageSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u56DE\u590D\u8BED\u8A00",e.appendChild(t);let n=document.createElement("div");n.className="select-wrap";let s=document.createElement("select");s.className="select",this.langSelect=s,A.forEach(a=>{let o=document.createElement("option");o.value=a.value,o.textContent=a.label,a.value===this.settings.reply_language&&(o.selected=!0),s.appendChild(o)}),s.addEventListener("change",()=>{this.settings.reply_language=s.value});let i=document.createElement("span");return i.className="select-arrow",i.textContent="\u25BE",n.appendChild(s),n.appendChild(i),e.appendChild(n),e}buildNoteSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label";let n=document.createElement("span");n.textContent="\u5907\u6CE8";let s=document.createElement("span");s.textContent=`${this.settings.player_note.length}/50`,this.noteCounter=s,t.appendChild(n),t.appendChild(s),e.appendChild(t);let i=document.createElement("div");i.className="section-desc",i.textContent="\u5199\u4E0B\u4F60\u5BF9\u8FD9\u4E2A\u6E38\u620F\u7684\u5907\u6CE8\uFF0C\u4F1A\u663E\u793A\u5728\u5B58\u6863\u5217\u8868",e.appendChild(i);let a=document.createElement("div");a.className="note-wrap";let o=document.createElement("textarea");return o.className="note-input",o.placeholder="\u5907\u6CE8",o.maxLength=50,o.value=this.settings.player_note,this.noteInput=o,o.addEventListener("input",()=>{this.settings.player_note=o.value,this.noteCounter&&(this.noteCounter.textContent=`${o.value.length}/50`)}),a.appendChild(o),e.appendChild(a),e}buildDisplayModeSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u663E\u793A\u6A21\u5F0F",e.appendChild(t);let n=document.createElement("div");n.className="section-desc",n.textContent="\u7535\u8111\u6A21\u5F0F\u4F1A\u81EA\u52A8\u6A2A\u5C4F\u663E\u793A",e.appendChild(n);let s=document.createElement("div");s.className="segment",this.displayModeBtns=[];let i=[{value:"phone",label:"\u624B\u673A"},{value:"desktop",label:"\u7535\u8111"}];return i.forEach(a=>{let o=document.createElement("button");o.className=`segment-btn${a.value===this.displayMode?" active":""}`,o.textContent=a.label,o.addEventListener("click",()=>{this.displayMode=a.value;try{localStorage.setItem("storvia_display_mode",a.value)}catch{}this.displayModeBtns.forEach((d,p)=>{d.classList.toggle("active",i[p].value===a.value)});try{window.parent.postMessage({type:"storvia-orientation",mode:a.value==="desktop"?"landscape":"portrait"},"*")}catch{}}),this.displayModeBtns.push(o),s.appendChild(o)}),e.appendChild(s),e}buildSyncSection(){let e=document.createElement("div");e.className="section";let t=document.createElement("div");t.className="section-label",t.textContent="\u540C\u6B65\u6E38\u620F\u8BBE\u5B9A",e.appendChild(t);let n=document.createElement("div");n.className=`sync-hint${this.hasWorldUpdate?" visible":""}`,n.textContent="\u26A0 \u4F5C\u8005\u5DF2\u66F4\u65B0\u6E38\u620F\u5185\u5BB9\uFF0C\u5EFA\u8BAE\u70B9\u51FB\u540C\u6B65",this.syncHint=n,e.appendChild(n);let s=document.createElement("div");s.className="sync-desc",s.textContent="\u5982\u679C\u6E38\u620F\u4F5C\u8005\u66F4\u65B0\u4E86\u5185\u5BB9\uFF0C\u70B9\u51FB\u6B64\u6309\u94AE\u53EF\u5C06\u6700\u65B0\u7684\u6E38\u620F\u8BBE\u5B9A\u548C\u5F00\u5E55\u5C0F\u5267\u573A\u540C\u6B65\u5230\u5F53\u524D\u5BF9\u8BDD",e.appendChild(s);let i=document.createElement("button");return i.className="sync-btn",this.syncBtn=i,this.renderSyncBtn(),i.addEventListener("click",async()=>{if(this.syncState!=="loading"){this.setSyncState("loading");try{await this.callbacks.onSync(),this.setSyncState("success"),this.hasWorldUpdate=!1,this.fabDot&&this.fabDot.classList.remove("visible"),this.syncHint&&this.syncHint.classList.remove("visible"),setTimeout(()=>this.setSyncState("idle"),3e3)}catch{this.setSyncState("error"),setTimeout(()=>this.setSyncState("idle"),4e3)}}}),e.appendChild(i),e}makeDivider(){let e=document.createElement("div");return e.className="divider",e}updateModelActive(){this.modelPanel&&this.modelPanel.querySelectorAll(".model-option").forEach((e,t)=>{e.classList.toggle("active",this.models[t].id===this.selectedModelId)})}setSyncState(e){this.syncState=e,this.renderSyncBtn()}renderSyncBtn(){if(!this.syncBtn)return;let e=this.syncState,t={idle:j,loading:z,success:G,error:F},n={idle:"\u540C\u6B65\u6700\u65B0\u8BBE\u5B9A",loading:"\u540C\u6B65\u4E2D...",success:"\u5DF2\u540C\u6B65",error:"\u540C\u6B65\u5931\u8D25"};this.syncBtn.className=`sync-btn${e==="success"||e==="error"?` ${e}`:""}`,this.syncBtn.disabled=e==="loading",this.syncBtn.innerHTML=`${t[e]}<span>${n[e]}</span>`}async handleSave(){if(!(!this.saveBtn||this.saveBtn.disabled)){this.saveBtn.disabled=!0,this.saveBtn.textContent="\u4FDD\u5B58\u4E2D...";try{await this.callbacks.onSettingsChange({temperature:this.settings.temperature,reply_length:this.settings.reply_length,reply_language:this.settings.reply_language,player_note:this.settings.player_note}),this.saveBtn.textContent="\u5DF2\u4FDD\u5B58",this.saveBtn.classList.add("saved"),setTimeout(()=>{this.saveBtn&&(this.saveBtn.textContent="\u4FDD\u5B58",this.saveBtn.classList.remove("saved"),this.saveBtn.disabled=!1)},2e3)}catch{this.saveBtn.textContent="\u4FDD\u5B58\u5931\u8D25",setTimeout(()=>{this.saveBtn&&(this.saveBtn.textContent="\u4FDD\u5B58",this.saveBtn.disabled=!1)},2e3)}}}};var f=class{constructor(e,t,n,s,i=!1,a){this.http=e;this.getGameId=t;this.getConversationId=n;this.getModelId=s;this.isPreview=i;this.previewConfig=a}updatePreviewConfig(e){this.previewConfig=e}async send(e){if(this.isPreview)return this.sendPreview(e);let t={game_id:this.getGameId(),conversation_id:this.getConversationId(),model_id:this.getModelId(),message:e.message,topic:e.topic,context:e.context,stream:e.stream??!1};if(e.stream){await this.http.stream("/chat",t,e.onChunk??(()=>{}),e.onDone??(()=>{}));return}return this.http.post("/chat",t)}async save(e){return this.isPreview?{id:`preview-${Date.now()}`}:this.http.post("/messages/save",{conversation_id:this.getConversationId(),role:e.role,content:e.content,topic:e.topic})}async generate(e){if(this.isPreview)return this.sendPreview({message:"",topic:e.topic,context:(e.context||"")+`
|
|
431
|
+
[\u6307\u4EE4] \u8BF7\u76F4\u63A5\u751F\u6210\u56DE\u590D\uFF0C\u65E0\u9700\u7528\u6237\u8F93\u5165`,stream:e.stream,onChunk:e.onChunk,onDone:e.onDone});let t={conversation_id:this.getConversationId(),model_id:this.getModelId(),topic:e.topic,context:e.context,stream:e.stream??!1,save:e.save??!0};if(e.stream){await this.http.stream("/generate",t,e.onChunk??(()=>{}),e.onDone??(()=>{}));return}return this.http.post("/generate",t)}async sendPreview(e){let t={model_id:this.getModelId(),message:e.message,topic:e.topic,context:e.context,prompt_config:this.previewConfig||{},history_messages:[]};if(e.stream){await this.http.stream("/preview",t,e.onChunk??(()=>{}),e.onDone??(()=>{}));return}let n="";return await this.http.stream("/preview",t,s=>{n+=s},()=>{}),{id:"",content:n}}};var R=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(e){return this.isPreview?{...this.mem[e]||{}}:this.http.get(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}/state`)}async update(e,t){if(this.isPreview){this.mem[e]={...this.mem[e]||{},...t};return}await this.http.patch(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}/state`,t)}},b=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem=[];this.state=new R(e,t,n)}async list(){return this.isPreview?this.mem.map(e=>({name:e.name,avatar:e.avatar})):this.http.get(`/conversations/${this.getConversationId()}/characters`)}async get(e){if(this.isPreview){let t=this.mem.find(n=>n.name===e);return{name:t?.name||e,avatar:t?.avatar}}return this.http.get(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}`)}async add(e){if(this.isPreview){this.mem.push(e);return}await this.http.post(`/conversations/${this.getConversationId()}/characters`,e)}async update(e,t){if(this.isPreview){let n=this.mem.find(s=>s.name===e);n&&Object.assign(n,t);return}await this.http.patch(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}`,t)}async remove(e){if(this.isPreview){this.mem=this.mem.filter(t=>t.name!==e);return}await this.http.delete(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}`)}async detail(e){if(this.isPreview){let t=this.mem.find(n=>n.name===e);return{name:e,avatar:t?.avatar,state:await this.state.get(e)}}return this.http.get(`/conversations/${this.getConversationId()}/characters/${encodeURIComponent(e)}/detail`)}};var w=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem=[]}async list(){return this.isPreview?[...this.mem]:this.http.get(`/conversations/${this.getConversationId()}/relationships`)}async get(e,t){if(this.isPreview){let n=this.mem.find(s=>s.from===e&&s.to===t);if(n)return{...n};throw new Error("Relationship not found")}return this.http.get(`/conversations/${this.getConversationId()}/relationships/${encodeURIComponent(e)}/${encodeURIComponent(t)}`)}async add(e){if(this.isPreview){this.mem.push({...e});return}await this.http.post(`/conversations/${this.getConversationId()}/relationships`,e)}async update(e,t,n){if(this.isPreview){let s=this.mem.find(i=>i.from===e&&i.to===t);s?Object.assign(s,n):this.mem.push({from:e,to:t,...n});return}await this.http.patch(`/conversations/${this.getConversationId()}/relationships/${encodeURIComponent(e)}/${encodeURIComponent(t)}`,n)}async remove(e,t){if(this.isPreview){this.mem=this.mem.filter(n=>!(n.from===e&&n.to===t));return}await this.http.delete(`/conversations/${this.getConversationId()}/relationships/${encodeURIComponent(e)}/${encodeURIComponent(t)}`)}};var y=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem=[]}async list(e){if(this.isPreview)return e?this.mem.filter(n=>n.group===e):[...this.mem];let t=e?{group:e}:void 0;return this.http.get(`/conversations/${this.getConversationId()}/inventory`,t)}async add(e){if(this.isPreview){this.mem.push({...e});return}await this.http.post(`/conversations/${this.getConversationId()}/inventory`,e)}async update(e,t){if(this.isPreview){let n=this.mem.find(s=>s.name===e);n&&Object.assign(n,t);return}await this.http.patch(`/conversations/${this.getConversationId()}/inventory/${encodeURIComponent(e)}`,t)}async remove(e){if(this.isPreview){this.mem=this.mem.filter(t=>t.name!==e);return}await this.http.delete(`/conversations/${this.getConversationId()}/inventory/${encodeURIComponent(e)}`)}};var C=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(){return this.isPreview?{...this.mem}:this.http.get(`/conversations/${this.getConversationId()}/world`)}async update(e){if(this.isPreview){Object.assign(this.mem,e);return}await this.http.patch(`/conversations/${this.getConversationId()}/world`,e)}};var T=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(e){if(this.isPreview)return e?{...this.mem[e]||{}}:{...this.mem};let t=e?{group:e}:void 0;return this.http.get(`/conversations/${this.getConversationId()}/player/state`,t)}async update(e,t){if(this.isPreview){this.mem[e]={...this.mem[e]||{},...t};return}await this.http.patch(`/conversations/${this.getConversationId()}/player/state`,{group:e,values:t})}},x=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.memPersona={name:""};this.state=new T(e,t,n)}async get(){return this.isPreview?{...this.memPersona}:this.http.get(`/conversations/${this.getConversationId()}/player`)}};var I=class{constructor(e,t,n=!1){this.http=e;this.getConversationId=t;this.isPreview=n;this.mem={}}async get(e){if(this.isPreview)return e!==void 0?this.mem[e]:{...this.mem};let t=await this.http.get(`/conversations/${this.getConversationId()}/custom`);return e!==void 0?t[e]:t}async set(e){if(this.isPreview){for(let[t,n]of Object.entries(e))typeof n=="object"&&n!==null&&!Array.isArray(n)&&typeof this.mem[t]=="object"&&this.mem[t]!==null?this.mem[t]={...this.mem[t],...n}:this.mem[t]=n;return}await this.http.patch(`/conversations/${this.getConversationId()}/custom`,e)}async delete(e){if(this.isPreview){delete this.mem[e];return}await this.http.delete(`/conversations/${this.getConversationId()}/custom/${encodeURIComponent(e)}`)}async setGroupField(e,t,n){if(this.isPreview){(!this.mem[e]||typeof this.mem[e]!="object")&&(this.mem[e]={}),this.mem[e][t]=n;return}await this.http.patch(`/conversations/${this.getConversationId()}/custom/${encodeURIComponent(e)}/${encodeURIComponent(t)}`,{value:n})}async deleteGroupField(e,t){if(this.isPreview){if(typeof this.mem[e]=="object"&&this.mem[e]!==null){let n=this.mem[e];delete n[t],Object.keys(n).length===0&&delete this.mem[e]}return}await this.http.delete(`/conversations/${this.getConversationId()}/custom/${encodeURIComponent(e)}/${encodeURIComponent(t)}`)}};var V="/functions/v1/world-engine/v1";function W(){return window.__STORVIA__||{}}function J(l=5e3){let e=W();return e.apiBase?Promise.resolve(e):new Promise((t,n)=>{let s=setTimeout(()=>{window.removeEventListener("message",i),n(new c("NOT_INITIALIZED","Timed out waiting for __STORVIA__ config from parent frame."))},l);function i(a){let o=null;if(a.data?.type==="storvia-init"&&a.data.apiBase?o={apiBase:a.data.apiBase,gameId:a.data.gameId,accessToken:a.data.accessToken}:a.data?.type==="storvia-preview-init"&&a.data.config&&(o=a.data.config),o&&o.apiBase){clearTimeout(s),window.removeEventListener("message",i);let d=window;d.__STORVIA__=o,t(o)}}window.addEventListener("message",i)})}var k=class{constructor(){this.baseUrl="";this.gameId="";this.conversationId="";this.modelId="";this.accessToken="";this.initialized=!1;this.isPreview=!1;this.previewConfig=void 0}async init(){let e=await J(),t=e.apiBase;if(!t)throw new c("NOT_INITIALIZED","window.__STORVIA__.apiBase is not set. The platform must inject it before SDK init.");if(this.baseUrl=`${t}${V}`,this.isPreview=!!e.preview,this.previewConfig=e.previewConfig,!e.gameId)throw new c("GAME_NOT_FOUND","window.__STORVIA__.gameId is not set. The platform must inject it before SDK init.");if(this.gameId=e.gameId,!e.accessToken)throw new c("UNAUTHORIZED","window.__STORVIA__.accessToken is not set. The platform must inject the user token before SDK init.");if(this.accessToken=e.accessToken,this.http=new S(this.baseUrl,this.accessToken,!!e.debug),this.isPreview){this.conversationId="00000000-0000-0000-0000-000000000000";let r=e.models||[];this.modelId=e.selectedModelId||r[0]?.id||"",this.settingsSheet=new v({onModelChange:E=>{this.modelId=E},onSettingsChange:()=>Promise.resolve(),onSync:()=>Promise.resolve()}),r.length>0&&this.settingsSheet.mount(r,this.modelId,{temperature:1,reply_length:"medium",reply_language:"zh-CN",player_note:""},!1);let m=()=>this.gameId,h=()=>this.conversationId,u=()=>this.modelId;this.chat=new f(this.http,m,h,u,!0,this.previewConfig),this.characters=new b(this.http,h,!0),this.relationships=new w(this.http,h,!0),this.inventory=new y(this.http,h,!0),this.world=new C(this.http,h,!0),this.player=new x(this.http,h,!0),this.custom=new I(this.http,h,!0),this.initialized=!0,this.listenForPreviewMessages();return}let n=await this.authenticate();this.conversationId=n.conversationId;let s=n.models||[],i=this.getSavedModelId();this.modelId=i&&s.some(r=>r.id===i)?i:s[0]?.id||"";let a=n.settings||{temperature:1,reply_length:"medium",reply_language:"zh-CN",player_note:""};this.settingsSheet=new v({onModelChange:r=>{this.modelId=r},onSettingsChange:r=>this.saveSettings(r),onSync:()=>this.syncSettings()}),this.settingsSheet.mount(s,this.modelId,a,n.hasWorldUpdate??!1);let o=()=>this.gameId,d=()=>this.conversationId,p=()=>this.modelId;this.chat=new f(this.http,o,d,p),this.characters=new b(this.http,d),this.relationships=new w(this.http,d),this.inventory=new y(this.http,d),this.world=new C(this.http,d),this.player=new x(this.http,d),this.custom=new I(this.http,d),this.initialized=!0}listenForPreviewMessages(){window.addEventListener("message",e=>{!e.data||e.data.type!=="storvia-preview"||(e.data.action==="set-model"&&(this.modelId=e.data.modelId||""),e.data.action==="update-config"&&(this.previewConfig=e.data.previewConfig,this.chat&&this.chat.updatePreviewConfig(this.previewConfig)))})}async saveSettings(e){await this.http.patch("/settings",{conversation_id:this.conversationId,...e})}async syncSettings(){await this.http.post("/sync",{conversation_id:this.conversationId})}async authenticate(){let e=await fetch(`${this.baseUrl}/auth`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.accessToken}`},body:JSON.stringify({game_id:this.gameId})});if(!e.ok)throw e.status===401?new c("UNAUTHORIZED","User not authenticated"):e.status===404?new c("GAME_NOT_FOUND","Game not found or not published"):new c("INTERNAL_ERROR",`Auth failed: HTTP ${e.status}`);let t=await e.json();return t.data??t}getSavedModelId(){try{return localStorage.getItem("storvia_model_id")||""}catch{return""}}};return B(Y);})();
|
|
431
432
|
if(typeof window!=="undefined"){window.StorviaSDK=StorviaSDK.StorviaSDK;}
|