-
Notifications
You must be signed in to change notification settings - Fork 0
feat: redesign the web ui and fix preview/run/stop #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
efenocchi
wants to merge
22
commits into
main
Choose a base branch
from
feat/frontend-redesign
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
f9ffcc6
feat: add svg icon set for the web ui
efenocchi 1336171
feat: restyle web ui with a modern ide theme
efenocchi cc39c04
feat: refine app shell, chat and projects layout
efenocchi bc807c6
style: align accent to replit orange and enlarge display type
efenocchi 176ee4d
fix: keep the preview proxy on the session running the app
efenocchi ea43618
test: cover preview proxy ownership selection
efenocchi a820044
fix: type tab icons as components to satisfy tsc
efenocchi d8cb71f
feat: replay persisted conversation history on project open
efenocchi 2c630d2
fix: reset stale workflow selection when switching projects
efenocchi c4f949d
fix: push usage on project open so it is not empty until refresh
efenocchi 0117a8f
test: move preview-proxy tests into the server test suite
efenocchi 7dce4f7
fix: stop shell and agent-started apps on stop, not just workflow apps
efenocchi ca2d202
test: add pickPreview cases to the preview suite
efenocchi 46c8695
fix: rewrite root-absolute urls in preview so the app stays in the if…
efenocchi 44ed8d7
fix: own the running app at server level so any tab can stop it
efenocchi 434208f
fix: retry the preview upstream so a dev-server reloader gap is trans…
efenocchi fe0b93f
ci: color-code the coverage summary categories by threshold
efenocchi 8000162
fix: force identity encoding in the preview proxy and add clearPort
efenocchi a823a9f
fix: serialize app hub, clear stale preview port on stop, error on st…
efenocchi 428b60e
fix: stop a workspace app when its last session leaves, add hub edge …
efenocchi 6433300
style: use theme tokens for scrollbar, terminal and stop-hover colors
efenocchi 4de79de
fix: resilient app teardown so stop never rejects and start errors cl…
efenocchi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| import type { UiEvent, Workflow } from '@openrepl/shared'; | ||
| import { detectWorkflows, WorkflowManager } from './workflow.js'; | ||
| import { PreviewManager } from './preview.js'; | ||
| import { CommandRunner } from './runner.js'; | ||
|
|
||
| type AppStatus = Extract<UiEvent, { type: 'app_status' }>; | ||
|
|
||
| interface RunningApp { | ||
| mgr: WorkflowManager | null; | ||
| installer: CommandRunner | null; | ||
| preview: PreviewManager; | ||
| workflows: Workflow[]; | ||
| activeWorkflow: string | null; | ||
| status: AppStatus | null; | ||
| } | ||
|
|
||
| /** | ||
| * Server-level owner of the *running app*, keyed by workspace dir. The app is | ||
| * one thing per workspace; sessions (tabs, reconnects) are many. Keeping run / | ||
| * stop / preview here — instead of in a per-session mount — means every client | ||
| * viewing that workspace sees the same status and Stop button, and Stop always | ||
| * reaches the real process no matter which tab clicked it. | ||
| * | ||
| * Events are broadcast with the dir they belong to; a session forwards only the | ||
| * events for the workspace it currently has open. | ||
| */ | ||
| export class AppHub { | ||
| private apps = new Map<string, RunningApp>(); | ||
| private listeners = new Set<(dir: string, e: UiEvent) => void>(); | ||
| /** Per-dir promise chain so overlapping run()/stop() can't interleave and | ||
| * clear or swap `app.mgr` mid-start. */ | ||
| private locks = new Map<string, Promise<unknown>>(); | ||
| /** How many live sessions have each workspace open, so we can stop its app | ||
| * once the last tab for it goes away (no orphaned dev servers holding ports). */ | ||
| private attached = new Map<string, number>(); | ||
|
|
||
| /** A session opened `dir`. */ | ||
| attach(dir: string): void { | ||
| this.attached.set(dir, (this.attached.get(dir) ?? 0) + 1); | ||
| } | ||
|
|
||
| /** A session left `dir` (switched project or disconnected); stop its app when | ||
| * no session has it open anymore. */ | ||
| detach(dir: string): void { | ||
| const n = (this.attached.get(dir) ?? 1) - 1; | ||
| if (n > 0) { | ||
| this.attached.set(dir, n); | ||
| return; | ||
| } | ||
| this.attached.delete(dir); | ||
| void this.stop(dir); | ||
| } | ||
|
|
||
| subscribe(fn: (dir: string, e: UiEvent) => void): () => void { | ||
| this.listeners.add(fn); | ||
| return () => this.listeners.delete(fn); | ||
| } | ||
| private broadcast(dir: string, e: UiEvent): void { | ||
| for (const l of this.listeners) l(dir, e); | ||
| } | ||
|
|
||
| private appFor(dir: string): RunningApp { | ||
| const existing = this.apps.get(dir); | ||
| if (existing) return existing; | ||
| const created: RunningApp = { mgr: null, installer: null, preview: new PreviewManager(), workflows: [], activeWorkflow: null, status: null }; | ||
| this.apps.set(dir, created); | ||
| return created; | ||
| } | ||
|
|
||
| preview(dir: string): PreviewManager | null { | ||
| return this.apps.get(dir)?.preview ?? null; | ||
| } | ||
| status(dir: string): AppStatus | null { | ||
| return this.apps.get(dir)?.status ?? null; | ||
| } | ||
| activeWorkflow(dir: string): string | null { | ||
| return this.apps.get(dir)?.activeWorkflow ?? null; | ||
| } | ||
|
|
||
| /** The preview of whichever workspace currently has a running app, if any. */ | ||
| runningPreview(): PreviewManager | null { | ||
| for (const a of this.apps.values()) if (a.preview.getPort() != null) return a.preview; | ||
| return null; | ||
| } | ||
|
|
||
| /** A dev server the user started in the terminal — surface it in the preview. */ | ||
| notePort(dir: string, port: number): void { | ||
| const a = this.appFor(dir); | ||
| if (a.preview.getPort() !== port) { | ||
| a.preview.setPort(port); | ||
| this.broadcast(dir, { type: 'preview_ready', url: '/__preview/' }); | ||
| } | ||
| } | ||
|
|
||
| private setStatus(dir: string, status: AppStatus): void { | ||
| this.appFor(dir).status = status; | ||
| this.broadcast(dir, status); | ||
| } | ||
|
|
||
| /** Serialize run()/stop() per dir so they can't interleave. */ | ||
| private serialize<T>(dir: string, fn: () => Promise<T>): Promise<T> { | ||
| const prev = this.locks.get(dir) ?? Promise.resolve(); | ||
| const next = prev.then(fn, fn); | ||
| this.locks.set(dir, next.then(() => {}, () => {})); | ||
| return next; | ||
| } | ||
|
|
||
| /** Run (or re-run) the app for `dir`. Returns the detected workflows. */ | ||
| run(dir: string, getEnv: () => Promise<Record<string, string>>, name?: string): Promise<Workflow[]> { | ||
| return this.serialize(dir, () => this.runLocked(dir, getEnv, name)); | ||
| } | ||
|
|
||
| private async runLocked(dir: string, getEnv: () => Promise<Record<string, string>>, name?: string): Promise<Workflow[]> { | ||
| await this.stopLocked(dir); | ||
| const det = await detectWorkflows(dir); | ||
| const app = this.appFor(dir); | ||
| app.workflows = det.workflows; | ||
| if (det.self) { | ||
| this.setStatus(dir, { type: 'app_status', state: 'error', message: 'This is the OpenREPL folder itself — open your own app folder.' }); | ||
| return det.workflows; | ||
| } | ||
| const wf = name ? det.workflows.find((w) => w.name === name) : det.workflows[0]; | ||
| if (!wf) { | ||
| this.setStatus(dir, { type: 'app_status', state: 'error', message: 'No runnable app found. Ask the agent to create one (e.g. an index.html or a package.json with a dev/start script).' }); | ||
| return det.workflows; | ||
| } | ||
| try { | ||
| if (det.install) { | ||
| this.setStatus(dir, { type: 'app_status', state: 'installing', message: `Installing dependencies… (${det.install})` }); | ||
| app.installer = new CommandRunner(dir, getEnv, (data) => this.broadcast(dir, { type: 'term_data', data: `[install] ${data}` }), () => {}); | ||
| const code = await app.installer.run(det.install); | ||
| app.installer = null; | ||
| if (code !== 0) { | ||
| this.setStatus(dir, { type: 'app_status', state: 'error', message: 'Dependency install failed — see the terminal output.' }); | ||
| return det.workflows; | ||
| } | ||
| } | ||
| this.setStatus(dir, { type: 'app_status', state: 'starting', message: `Starting workflow "${wf.name}" — ${wf.steps.map((s) => s.name).join(' + ')}` }); | ||
| // Only the last spawned step exiting means the app is really down: a | ||
| // backend+frontend workflow shouldn't flip to "stopped" when one exits. | ||
| let live = wf.steps.filter((s) => s.command && !s.static).length; | ||
| const mgr = new WorkflowManager( | ||
| dir, | ||
| getEnv, | ||
| (step, data) => this.broadcast(dir, { type: 'term_data', data: `[${step}] ${data}` }), | ||
| (port) => { | ||
| this.notePort(dir, port); | ||
| this.setStatus(dir, { type: 'app_status', state: 'running', message: `"${wf.name}" running` }); | ||
| }, | ||
| (step, code) => { | ||
| if (mgr !== app.mgr) return; // a newer run replaced this workflow | ||
| if (--live > 0) return; | ||
| app.mgr = null; | ||
| app.activeWorkflow = null; | ||
| app.preview.clearPort(); | ||
| this.setStatus(dir, { type: 'app_status', state: 'stopped', message: `${step} exited (code ${code})` }); | ||
| }, | ||
| ); | ||
| app.mgr = mgr; | ||
| await mgr.start(wf); | ||
| app.activeWorkflow = wf.name; | ||
| } catch (e) { | ||
| // spawn ENOENT, a step throwing mid-start, etc. Tear the partial app down | ||
| // first (start() may have already spawned children) so we don't orphan | ||
| // processes/ports, then surface the error instead of a stuck "starting…". | ||
| await this.teardown(app); | ||
| this.setStatus(dir, { type: 'app_status', state: 'error', message: `Failed to start: ${e instanceof Error ? e.message : String(e)}` }); | ||
| } | ||
| return det.workflows; | ||
| } | ||
|
|
||
| /** Stop the app for `dir` — kills the workflow tree and any in-flight install. */ | ||
| stop(dir: string): Promise<void> { | ||
| return this.serialize(dir, () => this.stopLocked(dir)); | ||
| } | ||
|
|
||
| private async stopLocked(dir: string): Promise<void> { | ||
| const app = this.apps.get(dir); | ||
| if (app) await this.teardown(app); | ||
| this.setStatus(dir, { type: 'app_status', state: 'stopped' }); | ||
| } | ||
|
|
||
| /** | ||
| * Kill the running app and reset its refs. Clears state *before* awaiting the | ||
| * process kill so a rejecting `mgr.stop()` can't leave the app half-stopped — | ||
| * `stop()` must never reject (detach() calls it fire-and-forget). | ||
| */ | ||
| private async teardown(app: RunningApp): Promise<void> { | ||
| app.installer?.stopRuns(); | ||
| app.installer = null; | ||
| const mgr = app.mgr; | ||
| app.mgr = null; | ||
| app.activeWorkflow = null; | ||
| // Drop the stale port so runningPreview()/the proxy stop pointing at the | ||
| // now-dead server (the source of intermittent "target unreachable" 502s). | ||
| app.preview.clearPort(); | ||
| if (mgr) await mgr.stop().catch(() => {}); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.