// Optimal Trade — AI Agent Mission (cinematic multi-phase sourcing experience)
const { useState: useStateA, useEffect: useEffectA, useRef: useRefA } = React;
const A_PHASES = [
{ key: "understand", label: "Brief", icon: "sparkles" },
{ key: "scan", label: "Scan", icon: "radar" },
{ key: "reach", label: "Reach out", icon: "send" },
{ key: "negotiate", label: "Negotiate", icon: "handshake" },
{ key: "verdict", label: "Verdict", icon: "badge-check" },
];
/* The scripted mission timeline. Each beat renders into the live feed. */
const BEATS = [
{ phase: "understand", t: "agent", typing: true, wait: 1500, text: "Got it. I'll source 50 industrial water pumps — 2.2 kW, CE-certified — within 4 weeks, target $8,000." },
{ phase: "understand", t: "think", wait: 1300, text: "Building a search profile from your reference model…" },
{ phase: "scan", t: "phasediv", wait: 500, label: "Scanning the market", icon: "radar" },
{ phase: "scan", t: "scan", wait: 2900 },
{ phase: "scan", t: "agent", typing: true, wait: 1500, text: "Matched your spec against 38 product lines. Found 1 exact match and 2 close analogs." },
{ phase: "scan", t: "candidates", wait: 2000 },
{ phase: "reach", t: "phasediv", wait: 500, label: "Reaching out", icon: "send" },
{ phase: "reach", t: "agent", typing: true, wait: 1400, text: "Contacting all three supplier agents on your behalf." },
{ phase: "reach", t: "roster", wait: 1600 },
{ phase: "negotiate", t: "phasediv", wait: 500, label: "Live negotiation · Ningbo", icon: "message-square" },
{ phase: "negotiate", t: "supplier", id: "a1", typing: true, wait: 1700, cn: "您好!我们生产 2.2kW 工业水泵,CE 认证齐全,可供 50 台。", text: "Hello! We make 2.2 kW industrial water pumps, fully CE-certified — 50 units available." },
{ phase: "negotiate", t: "doc", id: "a1", wait: 1500, cnName: "产品规格书.pdf", name: "Product spec sheet" },
{ phase: "negotiate", t: "translate", wait: 3000 },
{ phase: "negotiate", t: "agent", typing: true, wait: 1700, text: "Your reference uses a stainless-steel impeller — theirs is cast iron. Can they match it?" },
{ phase: "negotiate", t: "supplier", id: "a1", typing: true, wait: 1500, cn: "不锈钢叶轮可以做,每台加 6 美元,交期 +3 天。", text: "Stainless impeller is possible: +$6 per unit, +3 days lead." },
{ phase: "negotiate", t: "agent", typing: true, wait: 1700, text: "Counter: hold the unit price if we commit to all 50 units and ship within 21 days?" },
{ phase: "negotiate", t: "supplier", id: "a1", typing: true, wait: 1600, cn: "成交!不锈钢叶轮包含在内,168 美元/台,21 天交货。", text: "Deal — stainless impeller included, $168/unit, 21-day lead." },
{ phase: "negotiate", t: "win", wait: 1600, text: "Negotiated the stainless upgrade at no extra cost — saved you ~$300." },
{ phase: "negotiate", t: "think", wait: 1800, text: "Jebel Ali confirmed an exact 1:1 match in stock; Bursa offered a higher-efficiency EU analog." },
{ phase: "verdict", t: "phasediv", wait: 500, label: "Verdict", icon: "badge-check" },
{ phase: "verdict", t: "agent", typing: true, wait: 1500, text: "Done. I negotiated with all three and read every datasheet. Here's my honest take." },
{ phase: "verdict", t: "done", wait: 300 },
];
/* ---------- small pieces ---------- */
function MatchRing({ pct, size = 40 }) {
const r = (size - 5) / 2, c = 2 * Math.PI * r;
const col = pct >= 97 ? "var(--ot-success)" : pct >= 92 ? "var(--ot-orange)" : "var(--ot-route-purple)";
return (
{pct}
);
}
function ScanCard() {
const [n, setN] = useStateA(0);
useEffectA(() => {
const target = AGENT_MARKET.scanned, t0 = performance.now(), dur = 2200; let raf;
const tick = (t) => { const k = Math.min(1, (t - t0) / dur); setN(Math.round(target * (1 - Math.pow(1 - k, 3)))); if (k < 1) raf = requestAnimationFrame(tick); };
raf = requestAnimationFrame(tick); return () => cancelAnimationFrame(raf);
}, []);
return (
Verified suppliers analysed
{fmt(n)}
{AGENT_MARKET.regions.map(r => {r} )}
);
}
function DocTranslate() {
const [done, setDone] = useStateA(false);
useEffectA(() => { const t = setTimeout(() => setDone(true), 1500); return () => clearTimeout(t); }, []);
if (!done) return (
Translating documentation 中文 → EN
);
return (
Datasheet translated & analysed EN
2.2 kW match
CE valid
Impeller differs
);
}
function FeedBubble({ side, name, flag, cn, text, agent }) {
return (
{agent
?
:
}
{name}
{cn && <>
{cn}
Auto-translated by agent
>}
{text}
);
}
/* ============================== AGENT SCENE ==================================== */
function AgentScene({ app }) {
const sups = AGENT_SUPPLIERS;
const supById = id => sups.find(s => s.id === id);
const [idx, setIdx] = useStateA(0);
const [stage, setStage] = useStateA("live"); // live | report
const [elapsed, setElapsed] = useStateA(0);
const bodyRef = useRefA(null);
const done = idx >= BEATS.length;
// drive the timeline
useEffectA(() => {
if (done) return;
const b = BEATS[idx];
const t = setTimeout(() => setIdx(i => i + 1), b.wait || 1200);
return () => clearTimeout(t);
}, [idx]);
// elapsed timer
useEffectA(() => { if (done) return; const t = setInterval(() => setElapsed(e => e + 1), 1000); return () => clearInterval(t); }, [done]);
// auto-scroll
useEffectA(() => { if (bodyRef.current) bodyRef.current.scrollTop = bodyRef.current.scrollHeight; }, [idx]);
const revealed = BEATS.slice(0, idx);
const curPhase = (revealed.length ? revealed[revealed.length - 1].phase : "understand");
const curPhaseIdx = A_PHASES.findIndex(p => p.key === curPhase);
const nextBeat = !done ? BEATS[idx] : null;
const showTyping = nextBeat && nextBeat.typing;
const typingIsAgent = showTyping && nextBeat.t === "agent";
const typingFlag = showTyping && nextBeat.t === "supplier" ? supById(nextBeat.id).flag : null;
const best = sups.find(s => s.offer.best);
const mm = Math.floor(elapsed / 60), ss = String(elapsed % 60).padStart(2, "0");
if (stage === "report") return setStage("live")} />;
return (
{mm}:{ss}
} />
{/* live header */}
Optimal Agent
{done ? "Mission complete · 3 offers secured" : (A_PHASES[curPhaseIdx] && A_PHASES[curPhaseIdx].label) + " · working for you…"}
{/* phase rail */}
{A_PHASES.map((p, i) => (
{p.label}
))}
{/* feed */}
{revealed.map((b, i) => {
if (b.t === "agent") return
;
if (b.t === "supplier") { const s = supById(b.id); return
; }
if (b.t === "think") return
{b.text}
;
if (b.t === "phasediv") return
{b.label}
;
if (b.t === "scan") return
;
if (b.t === "candidates") return (
{sups.map(s => (
{s.name}
{s.verdictKind === "exact" ? "Exact match" : "Close analog"} · ★ {s.rating}
))}
);
if (b.t === "roster") return (
{sups.map(s => (
{s.short}
online
))}
);
if (b.t === "doc") { const s = supById(b.id); return (
{b.cnName}
{b.name} · PDF · 1.2 MB
中文
); }
if (b.t === "translate") return
;
if (b.t === "win") return
{b.text}
;
return null;
})}
{/* typing indicator */}
{showTyping && (
)}
{/* footer CTA */}
{done
? setStage("report")}>See the agent's verdict
: setIdx(BEATS.length)} style={{ width: "100%", background: "none", border: "none", color: "var(--ot-fg-2)", fontWeight: 700, fontSize: 14, cursor: "pointer", fontFamily: "var(--ot-font-body)", padding: "6px" }}>Skip to results › }
);
}
/* ============================== AGENT REPORT (verdict) ========================= */
function AgentReport({ app, sups, best, onBack }) {
return (
{/* summary banner */}
Mission complete
I reached 3 verified suppliers, translated their datasheets from Chinese, and negotiated terms. One is a true 1:1 match; two are close analogs. My pick balances price, lead time and spec.
3 offers ranked
{sups.map(s => (
{s.offer.best &&
Agent pick}
{s.name}
{s.offer.terms} · ★ {s.rating}
{s.offer.was > s.offer.total &&
{money("$", s.offer.was)}
}
{money("$", s.offer.total)}
{/* verdict label */}
{s.verdictKind === "exact"
? 1:1 match to reference
: {s.verdict} }
{s.nuance}
{s.id === "a1" &&
Datasheet translated & verified by agent
}
))}
app.acceptOffer(best)}>Accept {best.short} · {money("$", best.offer.total)}
No payment until you accept · protected by mandate
);
}
function Metric({ label, value, good }) {
return ;
}
Object.assign(window, { AgentScene, AgentReport, Metric, MatchRing });