import { Badge, Button, CopyButton, Group, JsonInput, Modal, Stack, Table, Text, TextInput } from '@mantine/core'; import { IconCheck, IconCopy, IconPlus, IconRefresh } from '@/components/icons'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { useEffect, useState } from 'react'; import { Link } from 'react-router-dom'; import { getJson, sendJson } from '../../api/client'; import { ErrorAlert, fmtTs, PageHeader } from '../../components/common'; import './agent-studio.css'; interface FusionDelivery { base_url: string; token_url: string; client_id: string; scope: string; workflow_code: string; invocation_mode: string; workflow_status: string; has_secret: boolean } interface Client { client_id: string; name: string; callback_url: string | null; release_watch_url: string | null; created_at: string; last_used_at: string | null; revoked_at: string | null; delivery?: 'url' | 'fusion' | 'pull'; fusion?: FusionDelivery | null } interface Created extends Client { client_secret: string; signing_key: string } interface Cycle { cycle_id: string; run_id: string; areas: string[]; status: string | null; waiting_for: string | null; data_approved_by: string; scripts_approved_by: string | null; created_at: string; correlation_id: string | null } interface Delivery { id: number; delivery_id: string; event: string; cycle_id: string | null; attempts: number; status_code: number | null; error: string | null; created_at: string; delivered_at: string | null; job_id?: string | null; dead_at?: string | null; state?: 'queued' | 'picked_up' | 'retrying' | 'delivered' | 'superseded' | 'dead' } interface Watch { checked_at?: string; checked?: { release: string; area: string; changes?: number; new?: number; error?: string }[]; updates?: number } interface InboxItem { id: number; event: string; signature_ok: boolean; received_at: string; payload: Record & { cycle_id?: string; scenarios?: { id: string; title: string; area: string; written_by: string }[]; verdict?: string; areas?: { area: string; verdict: string }[] } } /** The workflow in Oracle Fusion, and which steps are Astra's. */ const FLOW: { n: number; text: string; who: 'fusion' | 'patchwise' | 'person' }[] = [ { n: 1, text: 'Read the module space', who: 'fusion' }, { n: 2, text: 'Generate synthetic test data', who: 'fusion' }, { n: 3, text: 'Mask and validate it', who: 'fusion' }, { n: 4, text: 'A person approves the test data', who: 'person' }, { n: 5, text: 'Astra writes the scripts; Fusion emails them', who: 'patchwise' }, { n: 6, text: 'A person approves start testing', who: 'person' }, { n: 7, text: 'Astra tests and reports; Fusion emails the results', who: 'patchwise' }, ]; const deliveryState = (d: Delivery) => (d.delivered_at ? { tone: 'green', words: `Delivered${d.attempts > 1 ? ` after ${d.attempts} tries` : ''}${d.job_id ? ` (Fusion job ${d.job_id})` : ''}` } : d.state === 'dead' || d.dead_at ? { tone: 'red', words: `Not delivered: ${d.error ?? ''}` } : d.state === 'superseded' ? { tone: 'neutral', words: 'Replaced by a newer event' } : d.attempts >= 6 ? { tone: 'red', words: `Failed: ${d.error ?? ''}` } : d.attempts ? { tone: 'orange', words: `Retrying (${d.error ?? 'no answer'})` } : { tone: 'neutral', words: 'Queued' }); function Secret({ label, value }: { label: string; value: string }) { return (
{label} {value} {({ copied, copy }) => }
); } /** The Fusion side's OAuth clients: create one (its secret and signing key are shown once), point its callbacks, revoke. */ function Clients() { const qc = useQueryClient(); const q = useQuery({ queryKey: ['as-clients'], queryFn: () => getJson<{ items: Client[] }>('/api/agent-studio/clients') }); const [open, setOpen] = useState(false); const [form, setForm] = useState({ name: '', callback_url: '', release_watch_url: '' }); const [made, setMade] = useState(null); const [editing, setEditing] = useState(null); const create = useMutation({ mutationFn: () => sendJson('POST', '/api/agent-studio/clients', { name: form.name, callback_url: form.callback_url || null, release_watch_url: form.release_watch_url || null }), onSuccess: (c) => { setMade(c); setOpen(false); void qc.invalidateQueries({ queryKey: ['as-clients'] }); }, }); const revoke = useMutation({ mutationFn: (id: string) => sendJson('DELETE', `/api/agent-studio/clients/${id}`), onSuccess: () => void qc.invalidateQueries({ queryKey: ['as-clients'] }) }); const items = (q.data?.items ?? []).filter((c) => !c.revoked_at); return (

OAuth clients

The Fusion workflow signs in with one of these (client credentials, scope patchwise.invoke).

{items.length === 0 && No client yet. Create one and paste its ID and secret into the Fusion workflow’s REST connection.} {items.length > 0 && ( ClientCallbacks go toLast used{items.map((c) => ( {c.name}
{c.client_id}
{c.delivery === 'fusion' && c.fusion ? <>Oracle Fusion directly
{c.fusion.workflow_code} on {c.fusion.base_url}
: c.delivery === 'pull' ? <>Fusion picks them up
events/claim, then events/ack
: <>
{c.callback_url ?? 'Not set'}
{c.release_watch_url &&
Release watch: {c.release_watch_url}
}}
{c.last_used_at ? fmtTs(c.last_used_at) : 'Never'}
))}
)} setOpen(false)} title="New OAuth client" centered> setForm({ ...form, name: e.currentTarget.value })} /> setForm({ ...form, callback_url: e.currentTarget.value })} /> setForm({ ...form, release_watch_url: e.currentTarget.value })} /> {editing && setEditing(null)} />} setMade(null)} title="Client created: copy these now" centered size="lg"> {made && ( The secret and the signing key are shown once. Astra keeps only the secret’s hash. )}
); } /** Where a client's events go: signed POSTs to its callback address (a relay), straight to Oracle Fusion, where * Astra signs in to the identity domain and starts the callback workflow (invokeAsync), or nowhere until the Fusion * workflow picks them up (pull: no OCI IAM app needed). The secret is write-only. */ function DeliverySheet({ c, onClose }: { c: Client; onClose: () => void }) { const qc = useQueryClient(); const f = c.fusion; const [mode, setMode] = useState<'url' | 'fusion' | 'pull'>(c.delivery ?? 'url'); const [form, setForm] = useState({ base_url: f?.base_url ?? '', token_url: f?.token_url ?? '', client_id: f?.client_id ?? '', client_secret: '', scope: f?.scope ?? '', workflow_code: f?.workflow_code ?? 'PATCH_TEST_CALLBACK_V3_7' }); const set = (k: keyof typeof form) => (e: React.ChangeEvent) => setForm({ ...form, [k]: e.currentTarget.value }); const save = useMutation({ mutationFn: () => sendJson('PUT', `/api/agent-studio/clients/${c.client_id}/delivery`, mode !== 'fusion' ? { mode } : { mode, fusion: { ...form, client_secret: form.client_secret || null, scope: form.scope || null } }), onSuccess: () => void qc.invalidateQueries({ queryKey: ['as-clients'] }), }); const test = useMutation({ mutationFn: () => sendJson<{ ok: boolean; detail: string }>('POST', `/api/agent-studio/clients/${c.client_id}/delivery/test`) }); return (
{mode === 'pull' ? ( Nothing is sent. The Fusion workflow asks for its events with this client’s own token (POST /api/agent-studio/v1/events/claim, then /events/ack), in a While Loop with a Wait. No Oracle identity domain app is needed. ) : mode === 'url' ? ( Signed POSTs go to the client’s callback address{c.callback_url ? <> ({c.callback_url}) : ' (not set)'}, for example the PATCH_TEST_RELAY. ) : ( <> Astra signs in to your Oracle identity domain with a confidential app (client credentials) and starts the callback workflow with invokeAsync. No relay is needed. )} {test.data && {test.data.detail}} {mode === 'fusion' && c.delivery === 'fusion' ? : } {save.isSuccess && Saved.}
); } function Cycles() { const q = useQuery({ queryKey: ['as-cycles'], queryFn: () => getJson<{ items: Cycle[] }>('/api/agent-studio/cycles'), refetchInterval: 10_000 }); const items = q.data?.items ?? []; return (

Test cycles

Each one started by the Fusion workflow with its approved synthetic test data.

{items.length === 0 ? No test cycle yet. : ( CycleAreasNowTest data approved byStart approved byStarted{items.map((c) => ( {c.cycle_id}{c.correlation_id &&
{c.correlation_id}
}
{c.areas.join(', ')} {c.waiting_for ? Waiting for {c.waiting_for} : {c.status}} {c.data_approved_by} {c.scripts_approved_by ?? '—'} {fmtTs(c.created_at)}
))}
)}
); } function Callbacks() { const qc = useQueryClient(); const q = useQuery({ queryKey: ['as-deliveries'], queryFn: () => getJson<{ items: Delivery[]; dead_letters?: number }>('/api/agent-studio/deliveries'), refetchInterval: 10_000 }); const requeue = useMutation({ mutationFn: (id: string) => sendJson('POST', `/api/agent-studio/deliveries/${id}/requeue`), onSuccess: () => void qc.invalidateQueries({ queryKey: ['as-deliveries'] }) }); const items = q.data?.items ?? []; return (

Callbacks to Fusion

Signed with the client’s key, retried for about half an hour when Fusion does not answer.

{!!q.data?.dead_letters && {q.data.dead_letters} event(s) could not be delivered after every retry. Send them again once Fusion answers.} {items.length === 0 ? Nothing sent yet. : ( EventCycleDeliveryWhen{items.map((d) => { const s = deliveryState(d); return ( {d.event.replace(/_/g, ' ')} {d.cycle_id ?? '—'} {s.words} {(d.state === 'dead' || !!d.dead_at) && !d.delivered_at && } {fmtTs(d.delivered_at ?? d.created_at)} ); })}
)}
); } function ReleaseWatch() { const qc = useQueryClient(); const q = useQuery({ queryKey: ['as-watch'], queryFn: () => getJson('/api/agent-studio/release-watch') }); const check = useMutation({ mutationFn: () => sendJson('POST', '/api/agent-studio/release-watch/check'), onSuccess: () => void qc.invalidateQueries({ queryKey: ['as-watch'] }) }); const w = q.data ?? {}; return (

Release watch

Reads Oracle’s What’s New for each test environment’s release; new items go to the Release Watch Agent, which drafts the impact assessment.

{w.checked_at ? `Last checked ${fmtTs(w.checked_at)} · ${w.updates ?? 0} update(s) sent` : 'Not checked yet. It checks every day once a client has a release watch address.'} {!!w.checked?.length && (
{w.checked.map((c) => (
{c.area}{c.release}{c.error ? 'Not published yet' : `${c.changes} items · ${c.new} new`}
))}
)}
); } /** Plays the Fusion side over the real API: a client of its own whose callbacks come back to this page. For tests and demos. */ function StandIn() { const qc = useQueryClient(); const [creds, setCreds] = useState<{ id: string; secret: string } | null>(() => { try { const s = sessionStorage.getItem('pw-standin'); return s ? JSON.parse(s) : null; } catch { return null; } }); const [data, setData] = useState(JSON.stringify({ AP: { business_unit: 'TEST_US_BU', supplier: 'Synthetic Supplier 01', currency: 'USD' } }, null, 2)); const [who, setWho] = useState(''); const [msg, setMsg] = useState(null); const [err, setErr] = useState(null); const inbox = useQuery({ queryKey: ['as-inbox'], queryFn: () => getJson<{ items: InboxItem[] }>('/api/agent-studio/standin/inbox'), refetchInterval: 5000 }); useEffect(() => { try { if (creds) sessionStorage.setItem('pw-standin', JSON.stringify(creds)); } catch { /* private window */ } }, [creds]); const setup = useMutation({ mutationFn: () => sendJson('POST', '/api/agent-studio/clients', { name: 'Stand-in (plays Oracle Fusion)', callback_url: `${window.location.origin}/api/agent-studio/standin/inbox`, release_watch_url: `${window.location.origin}/api/agent-studio/standin/inbox` }), onSuccess: (c) => { setCreds({ id: c.client_id, secret: c.client_secret }); void qc.invalidateQueries({ queryKey: ['as-clients'] }); }, }); const call = async (method: string, url: string, body?: unknown) => { if (!creds) throw new Error('Set up the stand-in first'); const t = await fetch('/oauth/token', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ grant_type: 'client_credentials', client_id: creds.id, client_secret: creds.secret, scope: 'patchwise.invoke' }) }); if (!t.ok) throw new Error(`Token refused (${t.status}): set up the stand-in again`); const { access_token } = await t.json(); const r = await fetch(url, { method, headers: { Authorization: `Bearer ${access_token}`, 'Content-Type': 'application/json' }, body: body ? JSON.stringify(body) : undefined }); const j = await r.json().catch(() => ({})); if (!r.ok) throw new Error(j.detail ?? `HTTP ${r.status}`); return j; }; const go = (fn: () => Promise) => { setErr(null); setMsg(null); fn().then(setMsg).catch((e: Error) => setErr(e.message)); }; const items = inbox.data?.items ?? []; const ready = items.find((i) => i.event === 'scripts_ready'); return (

Stand-in for Oracle Fusion

Plays the Agent Studio workflow against this Astra, over the same API, so the whole cycle can be tried and shown before the Fusion side is built.

{!creds && }
{creds && (

Steps 1–3 · Synthetic test data

Step 4 · A person approves the test data

setWho(e.currentTarget.value)} />

Steps 5–6 · Scripts ready, approve start testing

{ready ? ( <> {(ready.payload.scenarios ?? []).length} scenarios for cycle {String(ready.payload.cycle_id)}
    {(ready.payload.scenarios ?? []).slice(0, 6).map((s) =>
  • {s.id} {s.title}
  • )}
) : Waiting for the scripts_ready callback.}

Step 7 and the release watch · What Fusion received

{items.length === 0 ? Nothing received yet. : (
    {items.slice(0, 8).map((i) => (
  • {i.event.replace(/_/g, ' ')}{i.payload.verdict ? ` · ${String(i.payload.verdict)}` : ''} · {fmtTs(i.received_at)} · {i.signature_ok ? 'signature checked' : 'signature NOT valid'}
  • ))}
)}
)} {msg && {msg}} {err && {err}}
); } export function AgentStudioPage() { return ( Fusion’s AI Agent Studio drives each test cycle: it sends synthetic test data a person approved, approves start testing, and receives the scripts and the results. Integration guide} />
    {FLOW.map((s) =>
  1. {s.n}{s.text}{s.who === 'fusion' ? 'In Fusion' : s.who === 'person' ? 'A person, in Fusion' : 'Astra'}
  2. )}
); }