Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
266 lines (266 loc) · 11.5 KB
/
Copy pathscript.js
File metadata and controls
266 lines (266 loc) · 11.5 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
(() => {
"use strict";
const $ = (selector, root = document) => root.querySelector(selector);
const state = { agents: [] };
const api = async (path, method = "GET", body = undefined) => {
const options = { method, headers: { Accept: "application/json" } };
if (body !== undefined) {
options.headers["Content-Type"] = "application/json";
options.body = JSON.stringify(body);
}
const response = await fetch(path, options);
const payload = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(payload.error || "Request failed (" + response.status + ")");
return payload;
};
const node = (tag, className, text) => {
const element = document.createElement(tag);
if (className) element.className = className;
if (text !== undefined) element.textContent = text;
return element;
};
const setStatus = (element, message, kind = "") => {
element.textContent = message;
element.className = ("status " + kind).trim();
};
const globalStatus = message => { $("#global-status").textContent = message; };
const selectOptions = () => {
for (const id of ["#goal-owner", "#approval-agent", "#budget-agent"]) {
const select = $(id);
const current = select.value;
select.replaceChildren(new Option("Organization-wide", ""));
for (const agent of state.agents) select.add(new Option(agent.name, agent.id));
select.value = current;
}
};
const renderAgents = () => {
const container = $("#agents");
container.replaceChildren();
if (!state.agents.length) {
container.append(node("p", "muted", "No agents are registered."));
selectOptions();
return;
}
for (const agent of state.agents) {
const card = node("article", "agent-card");
card.append(node("h3", "", agent.name));
card.append(node("div", "agent-meta",
[agent.role, agent.adapter, agent.model || "default model", agent.state,
agent.heartbeat_seconds ? String(agent.heartbeat_seconds) + "s heartbeat" : "manual heartbeat"]
.filter(Boolean).join(" · ")));
if (agent.last_heartbeat) card.append(node("p", "status", "Last heartbeat: " + new Date(agent.last_heartbeat * 1000).toLocaleString()));
const actions = node("div", "agent-actions");
const toggle = node("button", "secondary", agent.state === "active" ? "Pause" : "Activate");
toggle.type = "button";
toggle.addEventListener("click", async () => {
toggle.disabled = true;
try {
await api("/api/agents/" + encodeURIComponent(agent.id), "PATCH",
{ state: agent.state === "active" ? "paused" : "active" });
await loadAgents();
globalStatus("Updated " + agent.name + ".");
} catch (error) { globalStatus(error.message); }
finally { toggle.disabled = false; }
});
actions.append(toggle);
const heartbeat = node("button", "secondary", "Heartbeat");
heartbeat.type = "button";
heartbeat.disabled = agent.state !== "active";
heartbeat.addEventListener("click", async () => {
try {
await api("/api/agents/" + encodeURIComponent(agent.id) + "/heartbeat", "POST", {});
await loadAgents();
globalStatus("Heartbeat recorded for " + agent.name + ".");
} catch (error) { globalStatus(error.message); }
});
actions.append(heartbeat);
card.append(actions);
const execute = node("form", "execute-box");
const prompt = node("input");
prompt.name = "prompt";
prompt.placeholder = "Task for this agent";
prompt.required = true;
prompt.maxLength = 10000;
const run = node("button", "", "Execute");
run.type = "submit";
run.disabled = agent.state !== "active";
execute.append(prompt, run);
const result = node("p", "status", "");
execute.addEventListener("submit", async event => {
event.preventDefault();
run.disabled = true;
setStatus(result, "Running…");
try {
const payload = await api("/api/agents/" + encodeURIComponent(agent.id) + "/execute", "POST", { prompt: prompt.value });
setStatus(result, String(payload.output || "Execution complete") + " · Run " + payload.run_id, "success");
prompt.value = "";
} catch (error) { setStatus(result, error.message, "error"); }
finally { run.disabled = agent.state !== "active"; }
});
card.append(execute, result);
container.append(card);
}
selectOptions();
};
async function loadAgents() {
const payload = await api("/api/agents");
state.agents = Array.isArray(payload.agents) ? payload.agents : [];
renderAgents();
}
async function loadGoals() {
const payload = await api("/api/goals");
const container = $("#goals");
container.replaceChildren();
for (const goal of payload.goals || []) {
const card = node("article", "compact-card");
const detail = node("div");
detail.append(node("h3", "", goal.title), node("p", "", goal.description || goal.status));
card.append(detail);
container.append(card);
}
if (!container.childNodes.length) container.append(node("p", "muted", "No goals yet."));
}
async function loadApprovals() {
const payload = await api("/api/approvals?status=pending");
const container = $("#approvals");
container.replaceChildren();
for (const approval of payload.approvals || []) {
const card = node("article", "compact-card");
const detail = node("div");
detail.append(node("h3", "", approval.action), node("p", "", JSON.stringify(approval.details)));
const actions = node("div", "actions");
for (const decision of ["approved", "rejected"]) {
const button = node("button", decision === "rejected" ? "danger" : "secondary", decision);
button.type = "button";
button.addEventListener("click", async () => {
button.disabled = true;
try {
await api("/api/approvals/" + encodeURIComponent(approval.id) + "/decision", "POST",
{ decision: decision, decided_by: "PanMedia operator" });
await loadApprovals();
} catch (error) { globalStatus(error.message); }
finally { button.disabled = false; }
});
actions.append(button);
}
card.append(detail, actions);
container.append(card);
}
if (!container.childNodes.length) container.append(node("p", "muted", "No pending approvals."));
}
async function loadBudgets() {
const payload = await api("/api/budgets");
const container = $("#budgets");
container.replaceChildren();
for (const budget of payload.budgets || []) {
const label = budget.agent_id
? (state.agents.find(agent => agent.id === budget.agent_id)?.name || budget.agent_id)
: "Organization-wide";
const card = node("article", "compact-card");
card.append(node("div", "", label + " · " + budget.period),
node("p", "", budget.spent_amount + " / " + budget.limit_amount + " " + budget.currency));
container.append(card);
}
if (!container.childNodes.length) container.append(node("p", "muted", "No budgets configured."));
}
async function refreshAll() {
globalStatus("Refreshing control-plane data…");
try {
await loadAgents();
await Promise.all([loadGoals(), loadApprovals(), loadBudgets()]);
globalStatus("Control plane is current.");
} catch (error) {
globalStatus(error.message);
}
}
$("#agent-form").addEventListener("submit", async event => {
event.preventDefault();
const form = new FormData(event.currentTarget);
const allowedTools = String(form.get("allowed_tools") || "").split(/\r?\n/).map(value => value.trim()).filter(Boolean);
const config = {};
if (allowedTools.length) config.allowed_tools = allowedTools;
const systemPrompt = String(form.get("system_prompt") || "").trim();
if (systemPrompt) config.system_prompt = systemPrompt;
config.max_tool_rounds = 4;
const payload = {
name: String(form.get("name")).trim(),
adapter: String(form.get("adapter")),
role: String(form.get("role") || "agent").trim() || "agent",
model: String(form.get("model") || "").trim() || null,
heartbeat_seconds: Number(form.get("heartbeat_seconds") || 0),
config: config
};
const status = $("#agent-form-status");
try {
await api("/api/agents", "POST", payload);
event.currentTarget.reset();
event.currentTarget.elements.role.value = "agent";
event.currentTarget.elements.heartbeat_seconds.value = "0";
setStatus(status, "Agent registered.", "success");
await loadAgents();
} catch (error) { setStatus(status, error.message, "error"); }
});
$("#goal-form").addEventListener("submit", async event => {
event.preventDefault();
const form = new FormData(event.currentTarget);
try {
await api("/api/goals", "POST", {
title: String(form.get("title")).trim(),
description: String(form.get("description") || "").trim(),
owner_agent_id: String(form.get("owner_agent_id") || "") || null
});
event.currentTarget.reset();
await loadGoals();
globalStatus("Goal created.");
} catch (error) { globalStatus(error.message); }
});
$("#approval-form").addEventListener("submit", async event => {
event.preventDefault();
const form = new FormData(event.currentTarget);
let details;
try { details = JSON.parse(String(form.get("details"))); }
catch { globalStatus("Approval details must be valid JSON."); return; }
if (!details || Array.isArray(details) || typeof details !== "object") {
globalStatus("Approval details must be a JSON object.");
return;
}
try {
await api("/api/approvals", "POST", {
action: String(form.get("action")).trim(),
details: details,
agent_id: String(form.get("agent_id") || "") || null
});
await loadApprovals();
globalStatus("Approval requested.");
} catch (error) { globalStatus(error.message); }
});
$("#budget-form").addEventListener("submit", async event => {
event.preventDefault();
const form = new FormData(event.currentTarget);
const status = $("#budget-form-status");
try {
await api("/api/budgets", "POST", {
limit_amount: Number(form.get("limit_amount")),
period: String(form.get("period")).trim(),
currency: String(form.get("currency") || "USD").trim().toUpperCase(),
agent_id: String(form.get("agent_id") || "") || null
});
setStatus(status, "Budget saved.", "success");
await loadBudgets();
} catch (error) { setStatus(status, error.message, "error"); }
});
$("#refresh-agents").addEventListener("click", () => loadAgents().catch(error => globalStatus(error.message)));
$("#refresh-goals").addEventListener("click", () => loadGoals().catch(error => globalStatus(error.message)));
$("#refresh-approvals").addEventListener("click", () => loadApprovals().catch(error => globalStatus(error.message)));
$("#refresh-budgets").addEventListener("click", () => loadBudgets().catch(error => globalStatus(error.message)));
api("/healthz").then(payload => {
const badge = $("#connection");
badge.textContent = payload.configured ? "Connected to Astrologically Intelligent" : "Server configuration required";
badge.className = "connection " + (payload.configured ? "ok" : "bad");
return refreshAll();
}).catch(error => {
$("#connection").textContent = "PanMedia server unavailable";
$("#connection").className = "connection bad";
globalStatus(error.message);
});
})();