@storvia/sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,432 @@
1
+ "use strict";var StorviaSDK=(()=>{var P=Object.defineProperty;var M=Object.getOwnPropertyDescriptor;var $=Object.getOwnPropertyNames;var L=Object.prototype.hasOwnProperty;var H=(l,e)=>{for(var t in e)P(l,t,{get:e[t],enumerable:!0})},O=(l,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of $(e))!L.call(l,s)&&s!==t&&P(l,s,{get:()=>e[s],enumerable:!(n=M(e,s))||n.enumerable});return l};var B=l=>O(P({},"__esModule",{value:!0}),l);var Y={};H(Y,{StorviaError:()=>c,StorviaSDK:()=>k});var c=class extends Error{constructor(e,t){super(t),this.name="StorviaError",this.code=e}};var S=class{constructor(e,t,n=!1){this.baseUrl=e,this.token=t,this.debug=n}setToken(e){this.token=e}setDebug(e){this.debug=e}buildHeaders(){return{"Content-Type":"application/json",Authorization:`Bearer ${this.token}`}}log(e,t,n,s,i){if(!this.debug)return;let a="%c[Storvia SDK]",o="color: #6366f1; font-weight: bold";i?(console.groupCollapsed(`${a} %c${e} ${t} %c\u2717`,o,"color: #e0e0e0","color: #ef4444"),n&&console.log("Request:",n),console.error("Error:",i),console.groupEnd()):(console.groupCollapsed(`${a} %c${e} ${t} %c\u2713`,o,"color: #e0e0e0","color: #22c55e"),n&&console.log("Request:",n),s!==void 0&&console.log("Response:",s),console.groupEnd())}async get(e,t){let n=new URL(`${this.baseUrl}${e}`);t&&Object.entries(t).forEach(([s,i])=>n.searchParams.set(s,i));try{let s=await fetch(n.toString(),{method:"GET",headers:this.buildHeaders()}),i=await this.handleResponse(s);return this.log("GET",e,t,i),i}catch(s){throw this.log("GET",e,t,void 0,s),s}}async post(e,t){try{let n=await fetch(`${this.baseUrl}${e}`,{method:"POST",headers:this.buildHeaders(),body:t?JSON.stringify(t):void 0}),s=await this.handleResponse(n);return this.log("POST",e,t,s),s}catch(n){throw this.log("POST",e,t,void 0,n),n}}async patch(e,t){try{let n=await fetch(`${this.baseUrl}${e}`,{method:"PATCH",headers:this.buildHeaders(),body:t?JSON.stringify(t):void 0}),s=await this.handleResponse(n);return this.log("PATCH",e,t,s),s}catch(n){throw this.log("PATCH",e,t,void 0,n),n}}async delete(e){try{let t=await fetch(`${this.baseUrl}${e}`,{method:"DELETE",headers:this.buildHeaders()}),n=await this.handleResponse(t);return this.log("DELETE",e,void 0,n),n}catch(t){throw this.log("DELETE",e,void 0,void 0,t),t}}async stream(e,t,n,s){this.log("STREAM",e,t);let i=await fetch(`${this.baseUrl}${e}`,{method:"POST",headers:this.buildHeaders(),body:JSON.stringify(t)});if(!i.ok){let r=await i.json().catch(()=>({})),m=new c(r.code||"INTERNAL_ERROR",r.message||`HTTP ${i.status}`);throw this.log("STREAM",e,t,void 0,m),m}let a=i.body?.getReader();if(!a)throw new c("INTERNAL_ERROR","No response body for stream");let o=new TextDecoder,d="",p="";for(;;){let{done:r,value:m}=await a.read();if(r)break;let u=o.decode(m,{stream:!0}).split(`
2
+ `);for(let E of u){if(!E.startsWith("data: "))continue;let _=E.slice(6);if(_==="[DONE]"){this.debug&&this.log("STREAM",e+" [done]",void 0,{id:d,contentLength:p.length}),s(d);return}try{let g=JSON.parse(_);g.type==="done"&&g.id?d=g.id:g.content&&(p+=g.content,n(g.content))}catch{}}}d&&s(d)}async handleResponse(e){if(!e.ok){let n={};try{n=await e.json()}catch{}let s={401:"UNAUTHORIZED",402:"INSUFFICIENT_BALANCE",404:"GAME_NOT_FOUND",429:"RATE_LIMITED"},i=n.code||s[e.status]||"INTERNAL_ERROR";throw new c(i,n.message||`HTTP ${e.status}`)}let t=await e.json();return t&&typeof t=="object"&&"data"in t?t.data:t}};var A=[{value:"zh-CN",label:"\u7B80\u4F53\u4E2D\u6587"},{value:"zh-TW",label:"\u7E41\u9AD4\u4E2D\u6587"},{value:"en",label:"English"},{value:"ja",label:"\u65E5\u672C\u8A9E"},{value:"ko",label:"\uD55C\uAD6D\uC5B4"}],N=[{value:"low",label:"\u7B80\u77ED"},{value:"medium",label:"\u9002\u4E2D"},{value:"high",label:"\u8BE6\u7EC6"}],U=`
3
+ :host {
4
+ position: fixed;
5
+ bottom: 20px;
6
+ right: 20px;
7
+ z-index: 2147483647;
8
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
9
+ }
10
+
11
+ .fab {
12
+ width: 48px;
13
+ height: 48px;
14
+ border-radius: 50%;
15
+ background: #6366f1;
16
+ color: #fff;
17
+ border: none;
18
+ cursor: pointer;
19
+ display: flex;
20
+ align-items: center;
21
+ justify-content: center;
22
+ box-shadow: 0 4px 12px rgba(0,0,0,0.3);
23
+ transition: transform 0.2s, box-shadow 0.2s;
24
+ }
25
+
26
+ .fab:hover {
27
+ transform: scale(1.08);
28
+ box-shadow: 0 6px 16px rgba(0,0,0,0.4);
29
+ }
30
+
31
+ .fab svg {
32
+ width: 22px;
33
+ height: 22px;
34
+ }
35
+
36
+ .panel {
37
+ position: absolute;
38
+ bottom: 60px;
39
+ right: 0;
40
+ width: 320px;
41
+ max-height: 80vh;
42
+ overflow-y: auto;
43
+ background: #1e1e1e;
44
+ border: 1px solid #333;
45
+ border-radius: 16px;
46
+ box-shadow: 0 8px 32px rgba(0,0,0,0.6);
47
+ display: none;
48
+ }
49
+
50
+ .panel.open {
51
+ display: block;
52
+ }
53
+
54
+ .panel-header {
55
+ display: flex;
56
+ align-items: center;
57
+ justify-content: space-between;
58
+ padding: 16px 16px 8px;
59
+ }
60
+
61
+ .panel-title {
62
+ font-size: 16px;
63
+ font-weight: 600;
64
+ color: #fff;
65
+ }
66
+
67
+ .close-btn {
68
+ width: 28px;
69
+ height: 28px;
70
+ border-radius: 50%;
71
+ border: none;
72
+ background: transparent;
73
+ color: #888;
74
+ cursor: pointer;
75
+ display: flex;
76
+ align-items: center;
77
+ justify-content: center;
78
+ font-size: 18px;
79
+ transition: background 0.15s;
80
+ }
81
+
82
+ .close-btn:hover {
83
+ background: #2a2a2a;
84
+ color: #fff;
85
+ }
86
+
87
+ .panel-body {
88
+ padding: 4px 16px 16px;
89
+ }
90
+
91
+ .section {
92
+ margin-bottom: 16px;
93
+ }
94
+
95
+ .section:last-child {
96
+ margin-bottom: 0;
97
+ }
98
+
99
+ .section-label {
100
+ font-size: 13px;
101
+ font-weight: 500;
102
+ color: #c0c0c0;
103
+ margin-bottom: 6px;
104
+ display: flex;
105
+ align-items: center;
106
+ justify-content: space-between;
107
+ }
108
+
109
+ .section-label .value {
110
+ color: #6366f1;
111
+ font-weight: 600;
112
+ }
113
+
114
+ .section-desc {
115
+ font-size: 11px;
116
+ color: #666;
117
+ margin-bottom: 8px;
118
+ line-height: 1.4;
119
+ }
120
+
121
+ .divider {
122
+ height: 1px;
123
+ background: #2a2a2a;
124
+ margin: 12px 0;
125
+ }
126
+
127
+ /* Model options */
128
+ .model-list {
129
+ display: flex;
130
+ flex-direction: column;
131
+ gap: 2px;
132
+ }
133
+
134
+ .model-option {
135
+ display: flex;
136
+ align-items: center;
137
+ gap: 8px;
138
+ padding: 8px;
139
+ border-radius: 8px;
140
+ cursor: pointer;
141
+ border: none;
142
+ background: transparent;
143
+ color: #e0e0e0;
144
+ width: 100%;
145
+ font-size: 13px;
146
+ text-align: left;
147
+ transition: background 0.15s;
148
+ }
149
+
150
+ .model-option:hover {
151
+ background: #2a2a2a;
152
+ }
153
+
154
+ .model-option.active {
155
+ background: #6366f1;
156
+ color: #fff;
157
+ }
158
+
159
+ .model-option .dot {
160
+ width: 8px;
161
+ height: 8px;
162
+ border-radius: 50%;
163
+ background: #555;
164
+ flex-shrink: 0;
165
+ }
166
+
167
+ .model-option.active .dot {
168
+ background: #fff;
169
+ }
170
+
171
+ /* Slider */
172
+ .slider-wrap {
173
+ position: relative;
174
+ padding: 0 2px;
175
+ }
176
+
177
+ .slider {
178
+ -webkit-appearance: none;
179
+ appearance: none;
180
+ width: 100%;
181
+ height: 4px;
182
+ border-radius: 2px;
183
+ background: #333;
184
+ outline: none;
185
+ }
186
+
187
+ .slider::-webkit-slider-thumb {
188
+ -webkit-appearance: none;
189
+ appearance: none;
190
+ width: 20px;
191
+ height: 20px;
192
+ border-radius: 50%;
193
+ background: #6366f1;
194
+ cursor: pointer;
195
+ border: 2px solid #1e1e1e;
196
+ box-shadow: 0 2px 4px rgba(0,0,0,0.3);
197
+ }
198
+
199
+ .slider::-moz-range-thumb {
200
+ width: 20px;
201
+ height: 20px;
202
+ border-radius: 50%;
203
+ background: #6366f1;
204
+ cursor: pointer;
205
+ border: 2px solid #1e1e1e;
206
+ box-shadow: 0 2px 4px rgba(0,0,0,0.3);
207
+ }
208
+
209
+ .slider-labels {
210
+ display: flex;
211
+ justify-content: space-between;
212
+ font-size: 11px;
213
+ color: #666;
214
+ margin-top: 4px;
215
+ }
216
+
217
+ /* Segment control (reply length) */
218
+ .segment {
219
+ display: flex;
220
+ background: #2a2a2a;
221
+ border-radius: 10px;
222
+ padding: 3px;
223
+ gap: 2px;
224
+ }
225
+
226
+ .segment-btn {
227
+ flex: 1;
228
+ padding: 8px 0;
229
+ border: none;
230
+ border-radius: 8px;
231
+ background: transparent;
232
+ color: #999;
233
+ font-size: 13px;
234
+ cursor: pointer;
235
+ transition: all 0.2s;
236
+ }
237
+
238
+ .segment-btn.active {
239
+ background: #6366f1;
240
+ color: #fff;
241
+ font-weight: 500;
242
+ }
243
+
244
+ .segment-btn:not(.active):hover {
245
+ color: #ddd;
246
+ }
247
+
248
+ /* Select */
249
+ .select-wrap {
250
+ position: relative;
251
+ }
252
+
253
+ .select {
254
+ width: 100%;
255
+ padding: 10px 32px 10px 12px;
256
+ border-radius: 10px;
257
+ border: 1px solid #333;
258
+ background: #2a2a2a;
259
+ color: #e0e0e0;
260
+ font-size: 13px;
261
+ appearance: none;
262
+ -webkit-appearance: none;
263
+ cursor: pointer;
264
+ outline: none;
265
+ transition: border-color 0.15s;
266
+ }
267
+
268
+ .select:focus {
269
+ border-color: #6366f1;
270
+ }
271
+
272
+ .select-arrow {
273
+ position: absolute;
274
+ right: 12px;
275
+ top: 50%;
276
+ transform: translateY(-50%);
277
+ pointer-events: none;
278
+ color: #666;
279
+ font-size: 12px;
280
+ }
281
+
282
+ /* Textarea (note) */
283
+ .note-wrap {
284
+ position: relative;
285
+ }
286
+
287
+ .note-input {
288
+ width: 100%;
289
+ padding: 10px 12px;
290
+ border-radius: 10px;
291
+ border: 1px solid #333;
292
+ background: #2a2a2a;
293
+ color: #e0e0e0;
294
+ font-size: 13px;
295
+ font-family: inherit;
296
+ resize: none;
297
+ height: 64px;
298
+ outline: none;
299
+ transition: border-color 0.15s;
300
+ box-sizing: border-box;
301
+ }
302
+
303
+ .note-input::placeholder {
304
+ color: #555;
305
+ }
306
+
307
+ .note-input:focus {
308
+ border-color: #6366f1;
309
+ }
310
+
311
+ .note-counter {
312
+ text-align: right;
313
+ font-size: 11px;
314
+ color: #555;
315
+ margin-top: 4px;
316
+ }
317
+
318
+ /* Sync section */
319
+ .sync-desc {
320
+ font-size: 11px;
321
+ color: #666;
322
+ margin-bottom: 8px;
323
+ line-height: 1.4;
324
+ }
325
+
326
+ .sync-btn {
327
+ width: 100%;
328
+ padding: 10px 0;
329
+ border-radius: 10px;
330
+ border: 1px solid #333;
331
+ background: #2a2a2a;
332
+ color: #e0e0e0;
333
+ font-size: 13px;
334
+ cursor: pointer;
335
+ display: flex;
336
+ align-items: center;
337
+ justify-content: center;
338
+ gap: 8px;
339
+ transition: background 0.15s;
340
+ }
341
+
342
+ .sync-btn:hover:not(:disabled) {
343
+ background: #333;
344
+ }
345
+
346
+ .sync-btn:disabled {
347
+ cursor: default;
348
+ opacity: 0.6;
349
+ }
350
+
351
+ .sync-btn svg {
352
+ width: 16px;
353
+ height: 16px;
354
+ }
355
+
356
+ .sync-btn.success { color: #4ade80; border-color: #4ade80; }
357
+ .sync-btn.error { color: #f87171; border-color: #f87171; }
358
+
359
+ /* Save button */
360
+ .save-row {
361
+ display: flex;
362
+ justify-content: flex-end;
363
+ margin-top: 16px;
364
+ }
365
+
366
+ .save-btn {
367
+ padding: 8px 24px;
368
+ border-radius: 10px;
369
+ border: none;
370
+ background: #6366f1;
371
+ color: #fff;
372
+ font-size: 13px;
373
+ font-weight: 500;
374
+ cursor: pointer;
375
+ transition: background 0.15s;
376
+ }
377
+
378
+ .save-btn:hover:not(:disabled) {
379
+ background: #5558e6;
380
+ }
381
+
382
+ .save-btn:disabled {
383
+ opacity: 0.6;
384
+ cursor: default;
385
+ }
386
+
387
+ .save-btn.saved {
388
+ background: #4ade80;
389
+ }
390
+
391
+ /* FAB red dot */
392
+ .fab-dot {
393
+ position: absolute;
394
+ top: 2px;
395
+ right: 2px;
396
+ width: 10px;
397
+ height: 10px;
398
+ border-radius: 50%;
399
+ background: #f87171;
400
+ border: 2px solid #1e1e1e;
401
+ display: none;
402
+ }
403
+
404
+ .fab-dot.visible {
405
+ display: block;
406
+ }
407
+
408
+ .fab-wrap {
409
+ position: relative;
410
+ display: inline-block;
411
+ }
412
+
413
+ /* Sync update hint */
414
+ .sync-hint {
415
+ font-size: 11px;
416
+ color: #f59e0b;
417
+ margin-bottom: 6px;
418
+ line-height: 1.4;
419
+ display: none;
420
+ }
421
+
422
+ .sync-hint.visible {
423
+ display: block;
424
+ }
425
+
426
+ @keyframes spin {
427
+ to { transform: rotate(360deg); }
428
+ }
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);})();
431
+ if(typeof window!=="undefined"){window.StorviaSDK=StorviaSDK.StorviaSDK;}
432
+ //# sourceMappingURL=index.umd.js.map