Skip to content

Commit 8fa0c37

Browse files
committed
fix(playground): pass token via URL fragment, capture currentIndex at schedule time
1 parent 5859df3 commit 8fa0c37

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

packages/moss-cli/src/moss_cli/commands/playground.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,6 @@ def _serve_index(self) -> None:
114114
self._send_json(500, {"error": "Playground HTML not found"})
115115
return
116116
html = PLAYGROUND_HTML.read_text(encoding="utf-8")
117-
html = html.replace(
118-
"</title>",
119-
f'</title>\n<meta name="moss-token" content="{self._token}" />',
120-
)
121117
self._send_html(html)
122118

123119
def _handle_list_indexes(self) -> None:
@@ -270,6 +266,7 @@ def playground_command(
270266

271267
server = HTTPServer(server_addr, PlaygroundHandler)
272268
url = f"http://127.0.0.1:{final_port}"
269+
frag_url = f"{url}/#{PlaygroundHandler._token}"
273270

274271
console.print()
275272
console.print(" [bold]Moss Playground[/bold]")
@@ -279,7 +276,7 @@ def playground_command(
279276
console.print()
280277

281278
if not no_open:
282-
webbrowser.open(url)
279+
webbrowser.open(frag_url)
283280

284281
try:
285282
server.serve_forever()

packages/moss-cli/src/moss_cli/playground/index.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ <h1>Moss <span>Playground</span></h1>
135135
</div>
136136

137137
<script type="module">
138-
const mossToken = document.querySelector('meta[name="moss-token"]')?.getAttribute('content') || '';
138+
const mossToken = location.hash.replace(/^#/, '') || '';
139139

140140
function apiFetch(path, options = {}) {
141141
const headers = { ...options.headers, 'X-Moss-Token': mossToken };
@@ -227,6 +227,9 @@ <h1>Moss <span>Playground</span></h1>
227227
const name = select.value;
228228
if (!name) return;
229229

230+
++requestId;
231+
if (searchTimeout) clearTimeout(searchTimeout);
232+
230233
loadBtn.disabled = true;
231234
loadBtn.innerHTML = '<span class="spinner">&#9696;</span> Loading...';
232235
loadStatus.classList.remove('hidden');
@@ -344,6 +347,7 @@ <h1>Moss <span>Playground</span></h1>
344347

345348
if (searchTimeout) clearTimeout(searchTimeout);
346349
const thisRequestId = ++requestId;
350+
const idx = currentIndex;
347351
searchTimeout = setTimeout(async () => {
348352
const placeholder = results.querySelector('.empty-state');
349353
if (placeholder) placeholder.innerHTML = '<p>Searching...</p>';
@@ -352,7 +356,7 @@ <h1>Moss <span>Playground</span></h1>
352356
const res = await apiFetch('/api/query', {
353357
method: 'POST',
354358
headers: { 'Content-Type': 'application/json' },
355-
body: JSON.stringify({ name: currentIndex, query, topK, alpha }),
359+
body: JSON.stringify({ name: idx, query, topK, alpha }),
356360
});
357361
if (!res.ok) {
358362
const err = await res.json();
@@ -370,6 +374,8 @@ <h1>Moss <span>Playground</span></h1>
370374
}
371375

372376
select.addEventListener('change', () => {
377+
++requestId;
378+
if (searchTimeout) clearTimeout(searchTimeout);
373379
updateLoadBtn();
374380
const name = select.value;
375381
const idx = indexes.find(i => i.name === name);

0 commit comments

Comments
 (0)