mirror of
https://github.com/stablyai/orca.git
synced 2026-11-18 11:54:46 +00:00
Merge pull request #142 from nwparker/cloud/w6-agent-firstrun
W6: agents work from the phone — pre-answer Claude/Codex first-run screens; Open terminal opens the terminal
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -29,6 +29,7 @@ tmp="$(mktemp "$dir/.session.env.XXXXXX")"
|
||||
count=0
|
||||
openai_base_url=''
|
||||
openai_model=''
|
||||
anthropic_key_tail=''
|
||||
while IFS= read -r line; do
|
||||
name="${line%%=*}" b64="${line#*=}"
|
||||
case "$name" in '' | *[!A-Z0-9_]* | [0-9]* | ORCA_* | PATH | HOME | LD_* | DYLD_*) continue ;; esac
|
||||
@@ -38,12 +39,15 @@ while IFS= read -r line; do
|
||||
case "$name" in
|
||||
OPENAI_BASE_URL) openai_base_url="$(printf '%s' "$b64" | base64 -d)" ;;
|
||||
OPENAI_MODEL) openai_model="$(printf '%s' "$b64" | base64 -d)" ;;
|
||||
# Claude Code asks once per key whether to use ANTHROPIC_API_KEY; it remembers the last 20 chars.
|
||||
ANTHROPIC_API_KEY) anthropic_key_tail="$(printf '%s' "$b64" | base64 -d | tail -c 20)" ;;
|
||||
esac
|
||||
done < <(printf '%s\n' "${ORCA_SECRET_ENV:-}")
|
||||
mv -f "$tmp" "$dir/session.env"
|
||||
printf '%s' "$generation" >"$dir/generation"
|
||||
|
||||
# ---- Codex provider block (no secret values: env_key names the variable) ----
|
||||
# ---- Codex: trust the workspace and use the configured provider (no secret values) ----
|
||||
PROJECT=/vercel/sandbox/project
|
||||
codex_config="$HOME/.codex/config.toml"
|
||||
toml_string() { # TOML basic string; the control plane only accepts https URLs and plain model ids
|
||||
local value="${1//\\/\\\\}"
|
||||
@@ -52,35 +56,82 @@ toml_string() { # TOML basic string; the control plane only accepts https URLs a
|
||||
strip_managed() { # drop both managed blocks
|
||||
awk '/^# >>> orca-cloud managed/{skip=1} !skip{print} /^# <<< orca-cloud managed/{skip=0}' "$1"
|
||||
}
|
||||
if [ -n "$openai_base_url" ] || grep -q '^# >>> orca-cloud managed' "$codex_config" 2>/dev/null; then
|
||||
# Always managed: an agent launched from the phone or the API has no one to answer Codex's
|
||||
# "trust this folder?" screen, so the workspace project is trusted up front.
|
||||
if true; then
|
||||
mkdir -p "$HOME/.codex"
|
||||
user_part="$(mktemp "$HOME/.codex/.config.XXXXXX")"
|
||||
if [ -f "$codex_config" ]; then strip_managed "$codex_config" >"$user_part"; fi
|
||||
next="$(mktemp "$HOME/.codex/.config.XXXXXX")"
|
||||
openai_host="${openai_base_url#https://}"
|
||||
openai_host="${openai_host%%/*}"
|
||||
if [ -n "$openai_base_url" ] && [ "$openai_host" != api.openai.com ]; then
|
||||
# Top-level keys must precede every table; skip ours if the user already chose a provider/model.
|
||||
head_keys="$(awk '/^\[/{exit} {print}' "$user_part")"
|
||||
{
|
||||
printf '# >>> orca-cloud managed (Orca Cloud provider; edit in Orca settings)\n'
|
||||
if ! grep -Eq '^[[:space:]]*model_provider[[:space:]]*=' <<<"$head_keys"; then
|
||||
printf 'model_provider = "orca_cloud"\n'
|
||||
fi
|
||||
if [ -n "$openai_model" ] && ! grep -Eq '^[[:space:]]*model[[:space:]]*=' <<<"$head_keys"; then
|
||||
custom_provider=''
|
||||
if [ -n "$openai_base_url" ] && [ "$openai_host" != api.openai.com ]; then custom_provider=1; fi
|
||||
# Top-level keys must precede every table; each is skipped when the user already set it.
|
||||
head_keys="$(awk '/^\[/{exit} {print}' "$user_part")"
|
||||
has_key() { grep -Eq "^[[:space:]]*$1[[:space:]]*=" <<<"$head_keys"; }
|
||||
{
|
||||
printf '# >>> orca-cloud managed (Orca Cloud; edit in Orca settings)\n'
|
||||
# The sandbox is the isolation boundary: Codex's own bwrap sandbox can't start in it ("Unexpected
|
||||
# capabilities"), and a prompt delivered from the phone or the API has no one to approve commands.
|
||||
has_key sandbox_mode || printf 'sandbox_mode = "danger-full-access"\n'
|
||||
has_key approval_policy || printf 'approval_policy = "never"\n'
|
||||
if [ -n "$custom_provider" ]; then
|
||||
has_key model_provider || printf 'model_provider = "orca_cloud"\n'
|
||||
if [ -n "$openai_model" ] && ! has_key model; then
|
||||
printf 'model = %s\n' "$(toml_string "$openai_model")"
|
||||
fi
|
||||
printf '# <<< orca-cloud managed\n'
|
||||
cat "$user_part"
|
||||
fi
|
||||
printf '# <<< orca-cloud managed\n'
|
||||
cat "$user_part"
|
||||
if [ -n "$custom_provider" ]; then
|
||||
printf '\n# >>> orca-cloud managed provider\n[model_providers.orca_cloud]\nname = "Orca Cloud"\n'
|
||||
printf 'base_url = %s\nenv_key = "OPENAI_API_KEY"\nwire_api = "responses"\n' "$(toml_string "$openai_base_url")"
|
||||
printf '# <<< orca-cloud managed\n'
|
||||
} >"$next"
|
||||
else
|
||||
cat "$user_part" >"$next"
|
||||
fi
|
||||
} >"$next"
|
||||
if ! grep -qF "[projects.\"$PROJECT\"]" "$user_part"; then
|
||||
printf '\n# >>> orca-cloud managed trust\n[projects."%s"]\ntrust_level = "trusted"\n# <<< orca-cloud managed\n' "$PROJECT" >>"$next"
|
||||
fi
|
||||
chmod 600 "$next"
|
||||
mv -f "$next" "$codex_config"
|
||||
rm -f "$user_part"
|
||||
fi
|
||||
# ---- Claude Code: first-run screens answered once (theme picker, folder trust, bypass-mode warning,
|
||||
# the "use this API key?" question). A prompt delivered from the phone or the API has no one to click
|
||||
# through them, and the agent would sit there doing nothing. Merges; never touches credentials.
|
||||
node_bin="$(command -v node || find /opt/orca/runtime/runtimes -type f -name node 2>/dev/null | head -n 1)"
|
||||
if [ -n "$node_bin" ]; then
|
||||
ORCA_PROJECT="$PROJECT" ORCA_KEY_TAIL="$anthropic_key_tail" "$node_bin" -e '
|
||||
const fs = require("fs"), path = require("path"), os = require("os")
|
||||
const home = os.homedir()
|
||||
const readJson = (file) => { try { const v = JSON.parse(fs.readFileSync(file, "utf8")); return v && typeof v === "object" && !Array.isArray(v) ? v : {} } catch { return {} } }
|
||||
const write = (file, value) => {
|
||||
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 })
|
||||
const tmp = `${file}.orca-${process.pid}`
|
||||
fs.writeFileSync(tmp, JSON.stringify(value, null, 2) + "\n", { mode: 0o600 })
|
||||
fs.renameSync(tmp, file)
|
||||
}
|
||||
const claudeFile = path.join(process.env.CLAUDE_CONFIG_DIR || home, ".claude.json")
|
||||
const c = readJson(claudeFile)
|
||||
const before = JSON.stringify(c)
|
||||
c.hasCompletedOnboarding = true
|
||||
c.theme = c.theme || "dark"
|
||||
c.bypassPermissionsModeAccepted = true
|
||||
c.projects = c.projects && typeof c.projects === "object" ? c.projects : {}
|
||||
const p = process.env.ORCA_PROJECT
|
||||
c.projects[p] = { ...(c.projects[p] || {}), hasTrustDialogAccepted: true, hasCompletedProjectOnboarding: true }
|
||||
const tail = process.env.ORCA_KEY_TAIL
|
||||
if (tail) {
|
||||
const r = c.customApiKeyResponses && typeof c.customApiKeyResponses === "object" ? c.customApiKeyResponses : {}
|
||||
const approved = Array.isArray(r.approved) ? r.approved : []
|
||||
const rejected = Array.isArray(r.rejected) ? r.rejected.filter((t) => t !== tail) : []
|
||||
c.customApiKeyResponses = { ...r, approved: approved.includes(tail) ? approved : [...approved, tail], rejected }
|
||||
}
|
||||
if (JSON.stringify(c) !== before) write(claudeFile, c)
|
||||
const settingsFile = path.join(home, ".claude", "settings.json")
|
||||
const s = readJson(settingsFile)
|
||||
if (s.skipDangerousModePermissionPrompt !== true) write(settingsFile, { ...s, skipDangerousModePermissionPrompt: true })
|
||||
' || echo 'agent first-run state not seeded' >&2
|
||||
fi
|
||||
printf '{"names":%d}\n' "$count"
|
||||
|
||||
@@ -122,6 +122,8 @@ managed_env=(
|
||||
ORCA_ROOT_PATH="$PROJECT" ORCA_WORKTREE_PATH="$PROJECT" ORCA_WORKSPACE_PATH="$PROJECT"
|
||||
ORCA_WORKSPACE_NAME="$workspace_name" ORCA_PORT=3000
|
||||
CONDUCTOR_IS_CLOUD=1
|
||||
# Agent CLIs are pinned by the image (smoke-tested); a self-update mid-session can break a running agent.
|
||||
DISABLE_AUTOUPDATER=1
|
||||
CONDUCTOR_ROOT_PATH="$PROJECT" CONDUCTOR_WORKSPACE_PATH="$PROJECT"
|
||||
CONDUCTOR_WORKSPACE_NAME="$workspace_name" CONDUCTOR_PORT=3000
|
||||
)
|
||||
|
||||
@@ -25,6 +25,8 @@ export default withWorkspaceRoute(function MobileSessionScreen() {
|
||||
created?: string | string[]
|
||||
warning?: string | string[]
|
||||
paneKey?: string | string[]
|
||||
/** `terminal` | `chat`: the view the paneKey tap asks for (it rides with paneKey and clears with it). */
|
||||
paneView?: string | string[]
|
||||
}>()
|
||||
const hostId = firstParam(params.hostId)
|
||||
const worktreeId = firstParam(params.worktreeId)
|
||||
@@ -37,7 +39,7 @@ export default withWorkspaceRoute(function MobileSessionScreen() {
|
||||
if (param !== 'paneKey' || paneKey === '' || value !== paneKey) {
|
||||
return
|
||||
}
|
||||
router.setParams({ paneKey: '' })
|
||||
router.setParams({ paneKey: '', paneView: '' })
|
||||
},
|
||||
[paneKey, router]
|
||||
)
|
||||
@@ -45,7 +47,7 @@ export default withWorkspaceRoute(function MobileSessionScreen() {
|
||||
// Omit empty params: created=1 is one-shot, warning is host text, absent name derives a label, and empty paneKey marks a spent tap.
|
||||
const routeParams: Record<string, string> = {
|
||||
...Object.fromEntries(
|
||||
(['name', 'created', 'warning', 'paneKey'] as const)
|
||||
(['name', 'created', 'warning', 'paneKey', 'paneView'] as const)
|
||||
.map((key) => [key, firstParam(params[key])] as const)
|
||||
.filter(([, value]) => value !== '')
|
||||
),
|
||||
@@ -64,7 +66,7 @@ export default withWorkspaceRoute(function MobileSessionScreen() {
|
||||
}
|
||||
// Remount for grant-bearing route changes, excluding paneKey: pane taps update the current page without discarding its bridge.
|
||||
// Keep shared shellScreenRouteKey unchanged because other screens treat param changes as identity changes.
|
||||
const { paneKey: _paneKey, ...identity } = routeParams
|
||||
const { paneKey: _paneKey, paneView: _paneView, ...identity } = routeParams
|
||||
return (
|
||||
<MobileWebShellScreen
|
||||
key={shellScreenRouteKey({ pathname: route.pathname, params: identity })}
|
||||
|
||||
@@ -275,6 +275,8 @@ export function useCloudWorkspaceRun(sandboxId: string, workspace: CloudWorkspac
|
||||
const paneKey = view === 'terminal' ? run.agent.paneKey : undefined
|
||||
if (paneKey) {
|
||||
params.set('paneKey', paneKey)
|
||||
// The agent waits in its terminal (sign-in); the page must not open the tab in chat view.
|
||||
params.set('paneView', 'terminal')
|
||||
}
|
||||
setRun({ kind: 'idle' })
|
||||
router.push(
|
||||
|
||||
@@ -26,8 +26,8 @@ const bridge = vi.hoisted(() => {
|
||||
// The page's own router: an in-page hop puts the tapped pane in its URL, not in the shell route.
|
||||
const pageUrl = vi.hoisted(() => {
|
||||
const url: {
|
||||
params: { paneKey?: string }
|
||||
setParams: (next: { paneKey?: string }) => void
|
||||
params: { paneKey?: string; paneView?: string }
|
||||
setParams: (next: { paneKey?: string; paneView?: string }) => void
|
||||
} = {
|
||||
params: {},
|
||||
setParams: (next) => {
|
||||
@@ -84,6 +84,7 @@ const TABS: MobileSessionTab[] = [
|
||||
]
|
||||
|
||||
const switched: MobileSessionTab[] = []
|
||||
const views: [string, string][] = []
|
||||
|
||||
function Probe({
|
||||
terminalsLoaded,
|
||||
@@ -99,7 +100,8 @@ function Probe({
|
||||
terminalsLoaded,
|
||||
switchSessionTab: (tab) => switched.push(tab),
|
||||
worktreeId: 'wt-1',
|
||||
openNativeSession
|
||||
openNativeSession,
|
||||
setTabView: (tabId, view) => views.push([tabId, view])
|
||||
})
|
||||
return null
|
||||
}
|
||||
@@ -128,6 +130,7 @@ beforeEach(() => {
|
||||
bridge.listeners = []
|
||||
bridge.cleared.length = 0
|
||||
switched.length = 0
|
||||
views.length = 0
|
||||
pageUrl.params = {}
|
||||
})
|
||||
|
||||
@@ -166,4 +169,22 @@ describe('the page pane hook', () => {
|
||||
expect(switched.map((tab) => tab.id)).toEqual(['second'])
|
||||
expect(pageUrl.params.paneKey).toBeUndefined()
|
||||
})
|
||||
|
||||
it('opens the tapped pane in the view the tap asked for (Open terminal to sign in)', () => {
|
||||
bridge.route = {
|
||||
pathname: '/h/host-1/session/wt-1',
|
||||
params: { paneKey: `tab-b:${LEAF}`, paneView: 'terminal' }
|
||||
}
|
||||
render(true)
|
||||
expect(views).toEqual([['second', 'terminal']])
|
||||
expect(switched.map((tab) => tab.id)).toEqual(['second'])
|
||||
})
|
||||
|
||||
it('a later tap without a view leaves the stored per-tab view alone', () => {
|
||||
pageUrl.params = { paneKey: `tab-a:${LEAF}`, paneView: 'terminal' }
|
||||
render(true)
|
||||
deliver(`tab-b:${LEAF}`)
|
||||
expect(views).toEqual([['first', 'terminal']])
|
||||
expect(switched.map((tab) => tab.id)).toEqual(['first', 'second'])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -16,7 +16,8 @@ export function useNotificationPaneNavigation({
|
||||
connState,
|
||||
switchSessionTab,
|
||||
worktreeId,
|
||||
openNativeSession
|
||||
openNativeSession,
|
||||
setTabView
|
||||
}: {
|
||||
sessionTabs: MobileSessionTab[]
|
||||
terminalsLoaded: boolean
|
||||
@@ -24,15 +25,24 @@ export function useNotificationPaneNavigation({
|
||||
switchSessionTab: (tab: MobileSessionTab) => void
|
||||
worktreeId?: string
|
||||
openNativeSession?: (source: StructuredSessionOpenSource) => Promise<void>
|
||||
/** Applies the view a tap asked for (paneView), over the stored per-tab override. */
|
||||
setTabView?: (tabId: string, view: 'terminal' | 'chat') => void
|
||||
}) {
|
||||
const client = usePageBridgeClient()
|
||||
// This screen's own navigation: a screen in a preloaded first commit mounts before the router's container reports ready.
|
||||
const navigation = useNavigation<{ setParams: (params: { paneKey: undefined }) => void }>()
|
||||
// An in-page hop (an agent row, a sender jump) carries the tap only in the page URL; the shell route never names it.
|
||||
const urlPaneKey =
|
||||
firstParam(useLocalSearchParams<{ paneKey?: string | string[] }>().paneKey) || null
|
||||
const urlParams = useLocalSearchParams<{
|
||||
paneKey?: string | string[]
|
||||
paneView?: string | string[]
|
||||
}>()
|
||||
const urlPaneKey = firstParam(urlParams.paneKey) || null
|
||||
const urlPaneView = firstParam(urlParams.paneView) || null
|
||||
// The shell's pane wins: the URL copied it at bootstrap and can be older than a later notification tap.
|
||||
const pending = useRef(client.getShellSession()?.route?.params?.paneKey || urlPaneKey)
|
||||
const shellParams = client.getShellSession()?.route?.params
|
||||
const pending = useRef(shellParams?.paneKey || urlPaneKey)
|
||||
// The view rides with its paneKey: taken from the same source the pending paneKey came from.
|
||||
const pendingView = useRef(shellParams?.paneKey ? (shellParams.paneView ?? null) : urlPaneView)
|
||||
const applied = useRef<string | null>(null)
|
||||
const [asked, setAsked] = useState(0)
|
||||
|
||||
@@ -45,12 +55,13 @@ export function useNotificationPaneNavigation({
|
||||
}
|
||||
if (urlPaneKey !== mountUrlPaneKey.current) {
|
||||
pending.current = urlPaneKey
|
||||
pendingView.current = urlPaneView
|
||||
applied.current = null
|
||||
setAsked((count) => count + 1)
|
||||
}
|
||||
// Spend the URL's tap so a remount of this screen cannot replay it over a later tab pick.
|
||||
navigation.setParams({ paneKey: undefined })
|
||||
}, [navigation, urlPaneKey])
|
||||
}, [navigation, urlPaneKey, urlPaneView])
|
||||
|
||||
useEffect(() => {
|
||||
// Acknowledge the initial init separately; read from the client to keep the effect keyed only on client.
|
||||
@@ -71,6 +82,7 @@ export function useNotificationPaneNavigation({
|
||||
return
|
||||
}
|
||||
pending.current = paneKey
|
||||
pendingView.current = route?.params?.paneView ?? null
|
||||
setAsked((count) => count + 1)
|
||||
})
|
||||
}, [client])
|
||||
@@ -93,6 +105,11 @@ export function useNotificationPaneNavigation({
|
||||
// A pre-reconnect inventory can predate the pane named by the push.
|
||||
if (tab) {
|
||||
pending.current = null
|
||||
const view = pendingView.current
|
||||
pendingView.current = null
|
||||
if (tab.type === 'terminal' && (view === 'terminal' || view === 'chat')) {
|
||||
setTabView?.(tab.id, view)
|
||||
}
|
||||
if (tab.type === 'agent-session' && openNativeSession && worktreeId) {
|
||||
void openNativeSession({
|
||||
kind: 'notification',
|
||||
@@ -117,6 +134,7 @@ export function useNotificationPaneNavigation({
|
||||
switchSessionTab,
|
||||
terminalsLoaded,
|
||||
worktreeId,
|
||||
openNativeSession
|
||||
openNativeSession,
|
||||
setTabView
|
||||
])
|
||||
}
|
||||
|
||||
@@ -99,7 +99,11 @@ export function useMobileSessionController() {
|
||||
const preferences = useMobileSessionPreferenceFocus(keyboardState)
|
||||
const tabSwitching = Object.assign(keyboardState, useMobileSessionTabSwitching(keyboardState))
|
||||
const openNativeSession = useMobileStructuredSessionOpening(tabSwitching)
|
||||
useNotificationPaneNavigation({ ...tabSwitching, openNativeSession })
|
||||
useNotificationPaneNavigation({
|
||||
...tabSwitching,
|
||||
openNativeSession,
|
||||
setTabView: tabSwitching.nativeChatController.setTabView
|
||||
})
|
||||
const terminalWebview = Object.assign(tabSwitching, useMobileSessionTerminalWebview(tabSwitching))
|
||||
const terminalSendActions = Object.assign(
|
||||
terminalWebview,
|
||||
|
||||
Reference in New Issue
Block a user