-
-
Notifications
You must be signed in to change notification settings - Fork 392
feat(web): add auto-spawn session from URL params #437
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
woodgear
wants to merge
3
commits into
tiann:main
Choose a base branch
from
woodgear:feat/auto-spawn
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.
+93
−4
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,84 @@ | ||
| import { useEffect, useRef } from 'react' | ||
| import { useNavigate } from '@tanstack/react-router' | ||
| import type { ApiClient } from '@/api/client' | ||
|
|
||
| const AUTO_SPAWN_ACTIVE_POLL_INTERVAL_MS = 500 | ||
| const AUTO_SPAWN_ACTIVE_POLL_ATTEMPTS = 30 | ||
|
|
||
| // Capture spawn params at module load time, before any effect can clean the URL | ||
| const initialSearch = typeof window !== 'undefined' ? window.location.search : '' | ||
| const initialQuery = new URLSearchParams(initialSearch) | ||
| const SPAWN_PARAMS = { | ||
| spawn: initialQuery.get('spawn') === 'true', | ||
| machine: initialQuery.get('machine'), | ||
| dir: initialQuery.get('dir'), | ||
| boot: initialQuery.get('boot'), | ||
| } as const | ||
|
|
||
| function sleep(ms: number): Promise<void> { | ||
| return new Promise((resolve) => { | ||
| setTimeout(resolve, ms) | ||
| }) | ||
| } | ||
|
|
||
| async function waitForSessionActive(api: ApiClient, sessionId: string): Promise<boolean> { | ||
| for (let attempt = 0; attempt < AUTO_SPAWN_ACTIVE_POLL_ATTEMPTS; attempt += 1) { | ||
| try { | ||
| const { session } = await api.getSession(sessionId) | ||
| if (session.active) { | ||
| return true | ||
| } | ||
| } catch { | ||
| } | ||
|
|
||
| await sleep(AUTO_SPAWN_ACTIVE_POLL_INTERVAL_MS) | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| export function useAutoSpawn(api: ApiClient | null) { | ||
| const navigate = useNavigate() | ||
| const attemptedRef = useRef(false) | ||
|
|
||
| useEffect(() => { | ||
| if (!api || attemptedRef.current) return | ||
| if (!SPAWN_PARAMS.spawn || !SPAWN_PARAMS.machine || !SPAWN_PARAMS.dir) return | ||
| const machineId = SPAWN_PARAMS.machine | ||
| const directory = SPAWN_PARAMS.dir | ||
|
|
||
| attemptedRef.current = true | ||
|
|
||
| api.checkMachinePathsExists(machineId, [directory]).then(async (exists) => { | ||
| if (exists.exists[directory] === false) { | ||
| console.error('Auto-spawn blocked: missing directory requires explicit confirmation') | ||
| return | ||
| } | ||
|
|
||
| const result = await api.spawnSession(machineId, directory) | ||
| if (result.type === 'success') { | ||
| if (SPAWN_PARAMS.boot) { | ||
| try { | ||
| const active = await waitForSessionActive(api, result.sessionId) | ||
| if (!active) { | ||
| console.error('Auto-spawn boot message skipped: session did not become active in time') | ||
| } else { | ||
| await api.sendMessage(result.sessionId, SPAWN_PARAMS.boot) | ||
| } | ||
| } catch (err) { | ||
| console.error('Auto-spawn boot message failed:', err) | ||
| } | ||
| } | ||
| navigate({ | ||
| to: '/sessions/$sessionId', | ||
| params: { sessionId: result.sessionId }, | ||
| replace: true, | ||
| }) | ||
| } else { | ||
| console.error('Auto-spawn failed:', result.message) | ||
| } | ||
| }).catch((err) => { | ||
| console.error('Auto-spawn error:', err) | ||
| }) | ||
| }, [api, navigate]) | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[MAJOR] This still turns a top-level URL visit into an authenticated state-changing action.
useAutoSpawn()executes automatically on load, anduseAuthSource()restores stored access tokens fromlocalStorage, so a crafted?spawn=true&machine=...&dir=...&boot=...link can create a session and inject the first prompt without any explicit in-app confirmation.Suggested fix: