Repository navigation
feat(web): add a global project actions menu with headless runs #33284
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
farseenmanekhan1232
wants to merge
6
commits into
pingdotgg:factory/pr-33384-a1794597635
Choose a base branch
from
farseenmanekhan1232:feat/project-actions-runs
base: factory/pr-33384-a1794597635
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.
+1,851
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
19de9c5
feat(web): add a global project actions menu with headless runs
farseenmanekhan1232 3e089f8
fix(web): address project action review feedback
farseenmanekhan1232 a5b022e
fix(web): finish project action run integration
farseenmanekhan1232 3f5940e
fix(web): harden project action launches
farseenmanekhan1232 4d761f5
fix(web): preserve the project action shell context
maria-rcks 0d244d1
fix(web): settle invalid project actions and retain diagnostics
maria-rcks 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
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
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
202 changes: 202 additions & 0 deletions
202
apps/web/src/components/projectScriptRuns/ProjectScriptRunCoordinator.ts
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,202 @@ | ||
| import { runAtomCommand } from "@t3tools/client-runtime/state/runtime"; | ||
| import type { TerminalBufferState } from "@t3tools/client-runtime/state/terminal"; | ||
| import { | ||
| INITIAL_TERMINAL_OUTPUT_CURSOR, | ||
| readTerminalOutputUpdate, | ||
| } from "@t3tools/client-runtime/state/terminal"; | ||
| import { AsyncResult, type AtomRegistry } from "effect/reactivity"; | ||
|
|
||
| import { | ||
| isProjectScriptRunActive, | ||
| isProjectScriptWrapperLine, | ||
| PROJECT_SCRIPT_OUTPUT_TAIL_LINES, | ||
| projectScriptBeginMarker, | ||
| projectScriptSentinelPattern, | ||
| projectScriptStoppedPatch, | ||
| stripProjectScriptOutput, | ||
| type ProjectScriptRun, | ||
| type ProjectScriptRunPatch, | ||
| } from "../../projectScriptRuns"; | ||
| import { useProjectScriptRunsStore } from "../../projectScriptRunsStore"; | ||
| import { terminalEnvironment } from "../../state/terminal"; | ||
|
|
||
| const LOST_TERMINAL_GRACE_MS = 5_000; | ||
|
|
||
| export function startProjectScriptRunCoordinator(registry: AtomRegistry.AtomRegistry): () => void { | ||
| const watchers = new Map<string, () => void>(); | ||
| const sync = (runs: Record<string, ProjectScriptRun>) => { | ||
| for (const [runId, stop] of watchers) { | ||
| const run = runs[runId]; | ||
| if (run !== undefined && isProjectScriptRunActive(run)) continue; | ||
| watchers.delete(runId); | ||
| stop(); | ||
| } | ||
| for (const run of Object.values(runs)) { | ||
| if (!isProjectScriptRunActive(run) || watchers.has(run.id)) continue; | ||
| watchers.set(run.id, watchProjectScriptRun(registry, run)); | ||
| } | ||
| }; | ||
|
|
||
| sync(useProjectScriptRunsStore.getState().runs); | ||
| const unsubscribe = useProjectScriptRunsStore.subscribe((state, previous) => { | ||
| if (state.runs !== previous.runs) sync(state.runs); | ||
| }); | ||
|
|
||
| return () => { | ||
| unsubscribe(); | ||
| for (const stop of watchers.values()) stop(); | ||
| watchers.clear(); | ||
| }; | ||
| } | ||
|
|
||
| function watchProjectScriptRun( | ||
| registry: AtomRegistry.AtomRegistry, | ||
| run: ProjectScriptRun, | ||
| ): () => void { | ||
| const atom = terminalEnvironment.observe({ | ||
| environmentId: run.environmentId, | ||
| input: { threadId: run.threadId, terminalId: run.terminalId }, | ||
| }); | ||
| const sentinelPattern = projectScriptSentinelPattern(run.sentinel); | ||
| const beginMarker = projectScriptBeginMarker(run.sentinel); | ||
| let cursor = INITIAL_TERMINAL_OUTPUT_CURSOR; | ||
| let lineBuffer = ""; | ||
| let tail: string[] = []; | ||
| let sawBegin = false; | ||
| let sawLive = false; | ||
| let stopped = false; | ||
| let unsubscribe: (() => void) | null = null; | ||
| let lostTimer: ReturnType<typeof setTimeout> | null = null; | ||
|
|
||
| const stop = () => { | ||
| if (stopped) return; | ||
| stopped = true; | ||
| if (lostTimer !== null) { | ||
| clearTimeout(lostTimer); | ||
| lostTimer = null; | ||
| } | ||
| unsubscribe?.(); | ||
| }; | ||
|
|
||
| const settle = (patch: ProjectScriptRunPatch, closeTerminal = true) => { | ||
| if (stopped) return; | ||
| useProjectScriptRunsStore.getState().updateRun(run.id, { | ||
| ...patch, | ||
| outputTail: tail.slice(-PROJECT_SCRIPT_OUTPUT_TAIL_LINES).join("\n"), | ||
| endedAt: Date.now(), | ||
| }); | ||
| stop(); | ||
| if (!closeTerminal) return; | ||
| void runAtomCommand(registry, terminalEnvironment.close, { | ||
| environmentId: run.environmentId, | ||
| input: { threadId: run.threadId, terminalId: run.terminalId, deleteHistory: false }, | ||
| }); | ||
| }; | ||
|
|
||
| const stopPending = () => useProjectScriptRunsStore.getState().pendingStopRunIds.has(run.id); | ||
|
|
||
| const settleRequestedStop = () => { | ||
| const currentRun = useProjectScriptRunsStore.getState().runs[run.id]; | ||
| const stoppedPatch = | ||
| currentRun === undefined ? null : projectScriptStoppedPatch(currentRun, Date.now()); | ||
| if (stoppedPatch !== null) settle(stoppedPatch, false); | ||
| }; | ||
|
|
||
| lostTimer = setTimeout(() => { | ||
| if (!stopped && !sawLive) { | ||
| if (stopPending()) settleRequestedStop(); | ||
| else settle({ status: "lost", errorMessage: "The terminal is no longer available." }); | ||
| } | ||
| }, LOST_TERMINAL_GRACE_MS); | ||
|
|
||
| const handleLine = (rawLine: string): boolean => { | ||
| const sentinel = sentinelPattern.exec(rawLine); | ||
| if (sentinel !== null) { | ||
| const parsed = Number(sentinel[1]); | ||
| const exitCode = Number.isFinite(parsed) ? parsed : null; | ||
| settle({ | ||
| status: exitCode === 0 ? "succeeded" : "failed", | ||
| exitCode, | ||
| errorMessage: | ||
| exitCode === 0 ? null : (tail.at(-1) ?? `Exited with code ${exitCode ?? "unknown"}.`), | ||
| }); | ||
| return true; | ||
| } | ||
| if (!sawBegin) { | ||
| if (rawLine.includes(beginMarker)) sawBegin = true; | ||
| return false; | ||
| } | ||
| const cleaned = stripProjectScriptOutput(rawLine).trimEnd(); | ||
| if (cleaned.length === 0 || isProjectScriptWrapperLine(cleaned)) return false; | ||
| tail.push(cleaned.slice(0, 400)); | ||
| if (tail.length > PROJECT_SCRIPT_OUTPUT_TAIL_LINES) tail.shift(); | ||
| return false; | ||
| }; | ||
|
|
||
| const onResult = (result: AsyncResult.AsyncResult<TerminalBufferState, unknown>) => { | ||
| if (stopped) return; | ||
| if (result._tag === "Failure") { | ||
| if (sawLive) { | ||
| settle({ status: "lost", errorMessage: "The terminal is no longer available." }); | ||
| } | ||
| return; | ||
| } | ||
| if (result._tag !== "Success") return; | ||
| const state = result.value; | ||
|
|
||
| const update = readTerminalOutputUpdate(state.output, cursor); | ||
| cursor = update.cursor; | ||
| if (update.type === "reset") { | ||
| lineBuffer = ""; | ||
| tail = []; | ||
| } | ||
| if (update.type !== "none") { | ||
| lineBuffer += update.data; | ||
| const lines = lineBuffer.split(/\r\n|\r|\n/); | ||
| lineBuffer = (lines.pop() ?? "").slice(-4096); | ||
| for (const line of lines) { | ||
| if (handleLine(line)) return; | ||
| } | ||
| } | ||
| if (state.status === "error") { | ||
| settle({ | ||
| status: "failed", | ||
| errorMessage: state.error ?? "The terminal failed to start.", | ||
| }); | ||
| return; | ||
| } | ||
| if (state.status === "exited") { | ||
| settle({ | ||
| status: "failed", | ||
| errorMessage: "The terminal exited before the command reported a result.", | ||
| }); | ||
| return; | ||
| } | ||
| if (state.status === "closed") { | ||
| if (stopPending()) { | ||
| settleRequestedStop(); | ||
| } else if (sawLive) { | ||
| settle({ | ||
| status: "lost", | ||
| errorMessage: "The terminal closed before the command reported a result.", | ||
| }); | ||
| } | ||
| return; | ||
| } | ||
| sawLive = true; | ||
| if (lostTimer !== null) { | ||
| clearTimeout(lostTimer); | ||
| lostTimer = null; | ||
| } | ||
| if ( | ||
| state.status === "running" && | ||
| useProjectScriptRunsStore.getState().runs[run.id]?.status === "starting" | ||
| ) { | ||
| useProjectScriptRunsStore.getState().updateRun(run.id, { status: "running" }); | ||
| } | ||
| }; | ||
|
|
||
| unsubscribe = registry.subscribe(atom, onResult, { immediate: true }); | ||
| if (stopped) unsubscribe(); | ||
| return stop; | ||
| } | ||
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.