This document describes the current implementation and gives a code-trace map for debugging and maintenance.
index.html- Declares the app shell, toolbar controls, status pills, editor/terminal panes, splitter, and About modal.
- Loads
coi-serviceworker.js,styles.css, PyScript runtime, thenapp.js.
styles.css- Contains responsive layout, light/dark theme variables, editor/terminal styling, splitter behavior, focus-visible styles, and modal styles.
app.js- Main UI/runtime controller: template loading, run lifecycle, terminal runner creation, run-id sync, theme/modal behavior, and accessibility helpers.
llm.js- Local model runtime wrapper supporting dual-mode operation: WebLLM (Phi-3-mini on GPU) with automatic fallback to wllama (Phi-2 on CPU). Handles initialization, WebGPU detection, request translation to ChatML, streaming chunk queues, session reset/hard reset.
nopenai.py- Python OpenAI-compatible wrapper used inside PyScript execution, including sync/async APIs and bridge invocation logic.
coi-serviceworker.js- COI bootstrap service worker for static hosting scenarios.
- Browser loads
index.html. coi-serviceworker.jsexecutes first.- PyScript assets load.
app.jsimportsllm.jsand runsinitializeApp().
llm.js creates one ModelCoderLLM instance and exposes bridge methods:
modelCoderSetStatusListenermodelCoderInitmodelCoderSetActiveRunIdmodelCoderRequestmodelCoderResetSessionmodelCoderHardResetSessionmodelCoderNextStreamChunk
These are attached to globalThis, window, and self, and also grouped under modelCoderBridge.
initializeApp():
- Sets initial runtime/model status text.
- Registers model status listener.
- Registers
window.modelCoderMarkRunCompletecallback. - Applies saved theme, initializes splitter, wires UI handlers.
- Initializes About dialog interactions.
- Loads
nopenai.pysource into memory (state.nopenaiSource). - Waits for PyScript ready (event + fallback polling) and marks runtime ready.
- Initializes local model through bridge
modelCoderInit.
Key state fields:
- Readiness:
pyReady,terminalReady,modelReady - Run/session:
running,sessionActive,activeRunId - Template switching safety:
switchingTemplate - Theme/UI:
darkTheme,savedCode,selectedTemplate - Modal focus restore:
aboutReturnFocus
updateRunState() disables Run when switching templates or when run/session prerequisites are not satisfied.
loadSelectedTemplate() is transactional:
- Sets
switchingTemplate = trueand disables Run. - If sample changed:
- clears terminal output (
clearTerminalOutput({ resetModel: false })) - awaits
requestModelSessionReset({ hard: true })
- clears terminal output (
- Loads snippet into editor.
- Clears switching flag and re-enables controls.
This is intended to start the next sample from fresh model state.
syncActiveRunId() pushes state.activeRunId to JS bridge (modelCoderSetActiveRunId).
It is called when:
- app initializes
- a run starts
- stop is pressed
- terminal output is cleared
- PyScript editor is used for authoring only.
- Native embedded run controls are hidden and app-level Run controls execution.
launchTerminalScript(scriptCode, runId):
- Replaces
terminal-containerwith a fresh node. - Creates a unique per-run terminal target element:
terminal-run-${runId}. - Removes stale tagged runner scripts:
script[type="py"][data-model-coder-runner="true"]. - Creates a new runner script with:
type="py"terminalworkerifshouldUseTerminalWorker()target=<unique run target id>configpackages
- Tags runner with
data-model-coder-runner="true". - Hooks completion events (
py:done,py:error,error) tocompleteActiveRun(runId).
This unique-target strategy reduces cross-run terminal worker/proxy interference.
shouldUseTerminalWorker():
- GitHub Pages host detection (
github.ioor*.github.io) returns worker mode. - Otherwise, returns
window.crossOriginIsolated.
Every Run wraps user code with prelude logic:
- Creates in-memory module from fetched
nopenai.pysource. - Injects
_MODELCODER_RUN_IDinto that module. - Registers aliases:
sys.modules["nopenai"]sys.modules["openai"]
- Wraps
builtins.inputas__modelcoder_inputto normalize async-returning terminal input values. - Executes user code with
exec(__user_code, globals()). - In
finally, signals JS completion callback with run id.
This wrapper applies to all user-authored code, not only built-in samples.
- Sets
running = true. - Loads
nopenai.pysource if needed. - Generates new run id (
activeRunId + 1), syncs it to model bridge. - Sets
sessionActive = true. - Launches terminal runner script.
- Validates callback run id equals current active run id.
- Removes runner scripts.
- Clears run/session flags.
- Requests soft model session reset.
- Leaves terminal output visible.
- Increments run id, syncs run id, removes runners.
- Writes stop note to terminal.
- Clears run/session flags and requests soft reset.
- Increments run id and syncs run id.
- Removes runners and replaces terminal container.
- Optionally triggers soft reset (default true).
_loadWllamaModel() uses:
- model:
Felladrin/gguf-sharded-phi-2-orange-v2 - file:
phi-2-orange-v2.Q5_K_M.shard-00001-of-00025.gguf n_ctx: 384n_threads: dynamic (max(1, hardwareConcurrency - 2)when cross-origin isolated; fallback to1)
WASM_PATHS maps single-thread and multi-thread keys to their matching wasm paths.
_requestInternal(payload) supports:
chat.completions.createresponses.create
Flow:
- validates payload and model constraints
- validates run token (
payload.run_id) via_ensureActiveRun(...) - builds ChatML prompt
- executes full response path or streaming path
setActiveRunId(runId)stores current active run id._ensureActiveRun(runId, context)rejects stale calls from older runs.- stream sessions store
requestedRunId; chunk retrieval drops sessions that do not match active run.
This prevents stale scripts from previous samples from issuing new model requests/chunks.
_createStreamSession(...)initializes a queue-backed stream session._complete(...)pushes text deltas.nextStreamChunk(streamId, runId)drains queue with run-id and session-version checks.
Soft reset (resetSession):
- increments
sessionVersion - clears stream/response maps
- waits briefly for active generations to unwind
- clears KV cache
Hard reset (hardResetSession):
- runs soft reset
- clears current model instance references
- attempts engine cleanup (
dispose,destroy,unload, etc. when available) - re-initializes model
OpenAIandAsyncOpenAIchat.completions.create(...)responses.create(...)- sync and async stream iterators
_current_run_id()reads_MODELCODER_RUN_IDfrom module globals._request(...)injectsrun_idinto every model request payload._next_chunk(...)passes run id with stream chunk polling.
_bridge_call(method_name, *args):
- Enumerates candidate bridge objects across
pyscript.window,js,pyscript.sync, andjsglobals (globalThis/window/self/parent/top). - Prefers
modelCoderBridge.<method>when available. - Falls back to direct method calls.
- Falls back to
.call(...)style invocation. - Handles both sync and awaitable returns.
- Registers service worker when available in secure context.
- On first controller activation, triggers reload.
- Intercepts fetch and adds COI headers:
Cross-Origin-Embedder-Policy: require-corpCross-Origin-Opener-Policy: same-originCross-Origin-Resource-Policy: cross-origin
Implemented across index.html, styles.css, and app.js:
- semantic labels/landmarks
aria-livestatus pills- skip link
- keyboard splitter controls
- visible focus styles
- Escape handling:
- close About modal
- move focus out of editor to splitter (
enableEditorEscapeToTabOut())
Use this sequence when troubleshooting run hangs or stale-state behavior:
initializeApp()inapp.jsfor startup and listeners.runCurrentCode()inapp.jsfor run-id assignment and runner launch.buildExecutionCode()inapp.jsfor Python wrapper/run-id stamping._request(...)and_bridge_call(...)innopenai.pyfor payload + bridge path.modelCoderRequestand_requestInternal(...)inllm.jsfor model request execution._ensureActiveRun(...),_createStreamSession(...), andnextStreamChunk(...)inllm.jsfor stale-run filtering.loadSelectedTemplate()+requestModelSessionReset({ hard: true })inapp.jsfor sample-switch reset flow.hardResetSession()inllm.jsfor full model reinitialization path.