-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
424 lines (389 loc) · 14.1 KB
/
Copy pathapp.js
File metadata and controls
424 lines (389 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
const form = document.querySelector("#request-form");
const requestInput = document.querySelector("#request-input");
const runButton = document.querySelector("#run-button");
const runtimeVersion = document.querySelector("#runtime-version");
const runStatus = document.querySelector("#run-status");
const eventCount = document.querySelector("#event-count");
const eventList = document.querySelector("#event-list");
const evidenceEmpty = document.querySelector("#evidence-empty");
const resultEmpty = document.querySelector("#result-empty");
const resultContent = document.querySelector("#result-content");
const resultClarification = document.querySelector("#result-clarification");
const resultError = document.querySelector("#result-error");
const resultTitle = document.querySelector("#result-title");
const resultMessage = document.querySelector("#result-message");
const clarificationMessage = document.querySelector("#clarification-message");
const errorMessage = document.querySelector("#error-message");
const evidenceFields = {
option: document.querySelector("#plan-option"),
source: document.querySelector("#plan-source"),
transport_cost: document.querySelector("#cost-transport"),
hotel_cost: document.querySelector("#cost-hotel"),
activity_cost: document.querySelector("#cost-activities"),
total_cost: document.querySelector("#cost-total"),
cost_weight: document.querySelector("#rank-cost-weight"),
duration_weight: document.querySelector("#rank-duration-weight"),
};
const planFields = {
destination: document.querySelector("#plan-destination"),
days: document.querySelector("#plan-days"),
total_cost: document.querySelector("#plan-cost"),
flight_type: document.querySelector("#plan-flight"),
hotel_tier: document.querySelector("#plan-hotel"),
poi_style: document.querySelector("#plan-style"),
};
let session = null;
let renderedSequence = 0;
const toolEvidence = new Map();
const plannerArguments = new Map();
const RUN_TIMEOUT_MS = 60_000;
const POLL_INTERVAL_MS = 220;
function delay(milliseconds) {
return new Promise((resolve) => window.setTimeout(resolve, milliseconds));
}
function randomId() {
if (window.crypto && typeof window.crypto.randomUUID === "function") {
return window.crypto.randomUUID();
}
return `${Date.now()}-${Math.random().toString(16).slice(2)}`;
}
async function responseJson(response) {
const body = await response.json().catch(() => ({}));
if (!response.ok) {
const detail = typeof body.detail === "string" ? body.detail : response.statusText;
throw new Error(detail || `Request failed with status ${response.status}`);
}
return body;
}
async function runtimeFetch(path, options = {}) {
if (!session) {
throw new Error("The local demo session is not ready.");
}
const headers = new Headers(options.headers || {});
headers.set("Authorization", `Bearer ${session.api_key}`);
if (options.body) {
headers.set("Content-Type", "application/json");
}
return responseJson(await fetch(path, { ...options, headers }));
}
function setStatus(status) {
const normalized = status || "idle";
const labels = {
idle: "Ready",
queued: "Queued",
running: "Running",
completed: "Completed",
failed: "Failed",
cancelled: "Cancelled",
};
runStatus.textContent = labels[normalized] || normalized;
runStatus.className = `run-status ${normalized}`;
}
function resetWorkspace() {
renderedSequence = 0;
toolEvidence.clear();
plannerArguments.clear();
eventList.replaceChildren();
evidenceEmpty.hidden = false;
eventCount.textContent = "0 events";
resultEmpty.hidden = false;
resultContent.hidden = true;
resultClarification.hidden = true;
resultError.hidden = true;
resultTitle.textContent = "Result";
setStatus("running");
}
function captureEvidence(event) {
const payload = event.payload || {};
if (
event.event_type === "tool.result" &&
typeof payload.tool_name === "string" &&
payload.result &&
typeof payload.result === "object"
) {
toolEvidence.set(payload.tool_name, payload.result);
}
const decision = payload.decision || {};
if (
event.event_type === "planner.decision" &&
decision.decision_type === "CALL_TOOL" &&
typeof decision.tool_name === "string" &&
decision.arguments &&
typeof decision.arguments === "object"
) {
plannerArguments.set(decision.tool_name, decision.arguments);
}
}
function eventSummary(event) {
const payload = event.payload || {};
switch (event.event_type) {
case "run.queued":
return `${payload.agent_id}:${payload.agent_version} accepted`;
case "run.started":
return "Worker claimed durable run";
case "run.recovered":
return "Interrupted run recovered";
case "checkpoint.loaded":
return `State loaded from ${payload.source || "durable store"}`;
case "checkpoint.saved":
return "Updated state committed";
case "memory.retrieved": {
const count = Array.isArray(payload.memories) ? payload.memories.length : 0;
return `${count} governed preference${count === 1 ? "" : "s"} retrieved`;
}
case "planner.decision": {
const decision = payload.decision || {};
if (decision.decision_type === "CALL_TOOL") {
return `CALL_TOOL · ${decision.tool_name}`;
}
return decision.decision_type || payload.outcome || "Planner decision";
}
case "policy.decision":
return `${payload.outcome || "checked"} · ${payload.tool_name || "tool call"}`;
case "tool.result":
return `${payload.tool_name || "registered tool"} · ${payload.status || "recorded"}`;
case "loop.outcome":
return payload.outcome || "Loop completed";
case "run.completed":
return "Run and checkpoint committed";
case "run.failed":
return payload.error_code || "Runtime execution failed";
case "run.cancelled":
return payload.reason || "Run cancelled";
default:
if (event.event_type.startsWith("external_action.")) {
return payload.status || payload.outcome || "Durable external action evidence";
}
return payload.evidence_id || "Runtime event";
}
}
function eventClass(event) {
if (event.event_type === "planner.decision") {
return "planner";
}
if (event.event_type === "policy.decision") {
return "policy";
}
if (event.event_type === "tool.result") {
return "tool";
}
if (event.event_type === "loop.outcome") {
return "outcome";
}
if (event.event_type === "run.completed") {
return "success";
}
if (["run.failed", "run.cancelled"].includes(event.event_type)) {
return "failure";
}
return "";
}
function appendEvent(event) {
const item = document.createElement("li");
item.className = `event-item ${eventClass(event)}`.trim();
const marker = document.createElement("span");
marker.className = "event-marker";
marker.setAttribute("aria-hidden", "true");
const details = document.createElement("details");
details.className = "event-card";
const summary = document.createElement("summary");
const type = document.createElement("span");
type.className = "event-type";
type.textContent = event.event_type;
const description = document.createElement("span");
description.className = "event-summary";
description.textContent = eventSummary(event);
const sequence = document.createElement("span");
sequence.className = "event-sequence";
sequence.textContent = `#${String(event.sequence).padStart(2, "0")}`;
const payload = document.createElement("pre");
payload.textContent = JSON.stringify(event.payload || {}, null, 2);
summary.append(sequence, type, description);
details.append(summary, payload);
item.append(marker, details);
eventList.append(item);
}
function renderEvents(events) {
for (const event of events) {
if (event.sequence <= renderedSequence) {
continue;
}
captureEvidence(event);
appendEvent(event);
renderedSequence = event.sequence;
}
const count = eventList.childElementCount;
evidenceEmpty.hidden = count > 0;
eventCount.textContent = `${count} event${count === 1 ? "" : "s"}`;
}
function formatSgd(value) {
const amount = Number(value);
return Number.isFinite(amount) ? `${amount.toLocaleString()} SGD` : "—";
}
function formatPercent(value) {
const weight = Number(value);
return Number.isFinite(weight) ? `${Math.round(weight * 100)}%` : "—";
}
function selectedOptionName(itinerary, ranking) {
const rankedName = ranking?.ranking?.[0]?.name;
if (typeof rankedName === "string" && rankedName) {
return rankedName;
}
const selectionNote = itinerary.notes?.find((note) =>
note.startsWith("Selected option: "),
);
return selectionNote?.slice("Selected option: ".length).replace(/\.$/, "") ||
"Selected reference option";
}
function renderPlanEvidence(itinerary) {
const search = toolEvidence.get("search_trip_options") || {};
const ranking = toolEvidence.get("rank_trip_options") || {};
const route = toolEvidence.get("route_cost_summary") || {};
const rankArguments = plannerArguments.get("rank_trip_options") || {};
const optionName = selectedOptionName(itinerary, ranking);
const selected = Array.isArray(search.options)
? search.options.find((option) => option?.name === optionName) || {}
: {};
evidenceFields.option.textContent = optionName;
evidenceFields.source.textContent =
search.source === "synthetic_reference_catalog"
? "Offline synthetic catalog"
: "Persisted tool evidence";
evidenceFields.transport_cost.textContent = formatSgd(selected.transport_cost);
evidenceFields.hotel_cost.textContent = formatSgd(selected.hotel_cost);
evidenceFields.activity_cost.textContent = formatSgd(selected.activity_cost);
evidenceFields.total_cost.textContent = formatSgd(
route.total_cost ?? itinerary.total_cost,
);
evidenceFields.cost_weight.textContent = formatPercent(rankArguments.cost_weight);
evidenceFields.duration_weight.textContent = formatPercent(
rankArguments.duration_weight,
);
}
function renderCompletedRun(run) {
const itinerary = run.state && run.state.itinerary;
if (!itinerary) {
showClarification(
run.output_message || "The Planner needs more information before it can commit a plan.",
);
return;
}
resultEmpty.hidden = true;
resultClarification.hidden = true;
resultError.hidden = true;
resultContent.hidden = false;
resultTitle.textContent = "Validated reference plan";
resultMessage.textContent =
run.output_message || "A validated synthetic reference plan is ready.";
planFields.destination.textContent = itinerary.destination;
planFields.days.textContent = `${itinerary.days} days`;
planFields.total_cost.textContent = `${Number(itinerary.total_cost).toLocaleString()} SGD`;
planFields.flight_type.textContent = itinerary.flight_type.replaceAll("_", " ");
planFields.hotel_tier.textContent = itinerary.hotel_tier;
planFields.poi_style.textContent = itinerary.poi_style;
renderPlanEvidence(itinerary);
}
function showClarification(message) {
resultEmpty.hidden = true;
resultContent.hidden = true;
resultError.hidden = true;
resultClarification.hidden = false;
resultTitle.textContent = "Clarification required";
clarificationMessage.textContent = message;
}
function showFailure(message) {
resultEmpty.hidden = true;
resultContent.hidden = true;
resultClarification.hidden = true;
resultError.hidden = false;
resultTitle.textContent = "Execution failed";
errorMessage.textContent = message;
}
// Native EventSource cannot attach the Bearer token required by the Runtime SSE endpoint,
// so the local console uses bounded cursor-based polling over the same persisted event store.
async function pollRun(runId) {
const deadline = Date.now() + RUN_TIMEOUT_MS;
const encodedRunId = encodeURIComponent(runId);
while (Date.now() < deadline) {
const [run, events] = await Promise.all([
runtimeFetch(`/runs/${encodedRunId}`),
runtimeFetch(
`/runs/${encodedRunId}/events?after_sequence=${renderedSequence}`,
),
]);
renderEvents(events);
setStatus(run.status);
if (["completed", "failed", "cancelled"].includes(run.status)) {
const finalEvents = await runtimeFetch(
`/runs/${encodedRunId}/events?after_sequence=${renderedSequence}`,
);
renderEvents(finalEvents);
if (run.status === "completed") {
renderCompletedRun(run);
} else {
showFailure(run.error_code || run.error || `Run ${run.status}.`);
}
return;
}
await delay(POLL_INTERVAL_MS);
}
throw new Error(
`Run did not reach a terminal state within ${RUN_TIMEOUT_MS / 1000}s.`,
);
}
async function submitRequest(event) {
event.preventDefault();
const message = requestInput.value.trim();
if (!message || !session) {
return;
}
resetWorkspace();
runButton.disabled = true;
requestInput.disabled = true;
const requestId = randomId();
try {
const run = await runtimeFetch("/runs", {
method: "POST",
body: JSON.stringify({
thread_id: `runtime-console-${requestId}`,
client_request_id: `runtime-console-${requestId}`,
agent_id: session.agent_id,
agent_version: session.agent_version,
input: {
user_message: message,
requested_action: session.requested_action,
},
}),
});
await pollRun(run.run_id);
} catch (error) {
setStatus("failed");
showFailure(error instanceof Error ? error.message : "Unexpected demo error.");
} finally {
runButton.disabled = false;
requestInput.disabled = false;
requestInput.focus();
}
}
async function initialize() {
try {
session = await responseJson(
await fetch("/demo/session", { headers: { Accept: "application/json" } }),
);
requestInput.value = session.default_message;
runtimeVersion.textContent = `${session.agent_id}:${session.agent_version}`;
} catch (error) {
runButton.disabled = true;
setStatus("failed");
showFailure(
error instanceof Error ? error.message : "The local demo session could not start.",
);
}
}
form.addEventListener("submit", submitRequest);
requestInput.addEventListener("keydown", (event) => {
if ((event.metaKey || event.ctrlKey) && event.key === "Enter") {
form.requestSubmit();
}
});
initialize();