forked from NSPG13/agent-bounties
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjective.js
More file actions
289 lines (265 loc) · 12.1 KB
/
Copy pathobjective.js
File metadata and controls
289 lines (265 loc) · 12.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
(() => {
"use strict";
const API = "https://api.agentbounties.app";
// Compatibility marker for canonical planner evidence: /v1/base/autonomous-bounties/claim-funnel
const EXAMPLE = {
objective: "Create a simple website where people can report broken streetlights and see when they are fixed.",
constraints: [
"Works well on a phone",
"Does not collect unnecessary personal data",
"The main actions have clear tests",
],
budget: "25.00",
};
const previewPlan = {
title: "Example task plan",
model: "Example",
parallel_layers: [["write_rules"], ["build_page", "test_page"], ["publish_result"]],
tasks: [
{
task_id: "write_rules",
title: "Write the clear task rules",
goal: "Describe what the streetlight report page must do and how completion will be checked.",
depends_on: [],
acceptance_criteria: [
"The required user actions are listed.",
"Each action has a clear completion check.",
],
verifier: { kind: "review" },
evidence_schema: { required: ["requirements_document"] },
suggested_solver_reward_usdc: "3.000000",
},
{
task_id: "build_page",
title: "Build the reporting page",
goal: "Create a phone-friendly page where a person can send a broken-streetlight report.",
depends_on: ["write_rules"],
acceptance_criteria: [
"A person can enter a location and description.",
"The page confirms that the report was sent.",
"The page works on a phone-sized screen.",
],
verifier: { kind: "command", command: "run the committed website tests" },
evidence_schema: { required: ["repository", "commit_sha", "test_result"] },
suggested_solver_reward_usdc: "14.000000",
},
{
task_id: "test_page",
title: "Test the main actions",
goal: "Prove that reporting, confirmation, and phone display work as promised.",
depends_on: ["write_rules"],
acceptance_criteria: [
"The report test passes.",
"The confirmation test passes.",
"The phone layout test passes.",
],
verifier: { kind: "test" },
evidence_schema: { required: ["test_report"] },
suggested_solver_reward_usdc: "5.000000",
},
{
task_id: "publish_result",
title: "Publish the finished result",
goal: "Make the tested page available and link the final proof.",
depends_on: ["build_page", "test_page"],
acceptance_criteria: [
"The live page opens.",
"The live page matches the tested version.",
],
verifier: { kind: "http" },
evidence_schema: { required: ["live_url", "release_commit"] },
suggested_solver_reward_usdc: "3.000000",
},
],
};
const form = document.getElementById("objective-compiler-form");
const graph = document.getElementById("objective-graph");
const inspector = document.getElementById("task-inspector");
const status = document.querySelector("[data-compiler-status]");
const submit = form && form.querySelector("button[type='submit']");
let currentPlan = previewPlan;
let selectedTask = previewPlan.tasks[0];
if (!form || !graph || !inspector || !status || !submit) return;
function formatUsdc(value) {
const number = Number(value);
return Number.isFinite(number) ? number.toFixed(2) : "";
}
function taskById(id) {
return currentPlan.tasks.find((task) => task.task_id === id);
}
function dependencyClosure(task) {
const related = new Set([task.task_id]);
const queue = [...(task.depends_on || [])];
while (queue.length) {
const id = queue.shift();
if (related.has(id)) continue;
related.add(id);
const dependency = taskById(id);
if (dependency) queue.push(...(dependency.depends_on || []));
}
currentPlan.tasks.forEach((candidate) => {
if ((candidate.depends_on || []).includes(task.task_id)) related.add(candidate.task_id);
});
return related;
}
function clearRelationshipHighlight() {
graph.querySelectorAll(".graph-node").forEach((node) => node.removeAttribute("data-related"));
}
function highlightTask(task) {
const related = dependencyClosure(task);
graph.querySelectorAll(".graph-node").forEach((node) => {
node.dataset.related = String(related.has(node.dataset.taskId));
node.dataset.selected = String(node.dataset.taskId === task.task_id);
});
}
function escapeHtml(value) {
const element = document.createElement("span");
element.textContent = String(value == null ? "" : value);
return element.innerHTML;
}
function inspectTask(task) {
selectedTask = task;
highlightTask(task);
const criteria = (task.acceptance_criteria || [])
.map((item) => `<li>${escapeHtml(item)}</li>`)
.join("");
const evidence = ((task.evidence_schema || {}).required || [])
.map(escapeHtml)
.join(", ") || "A link or record that shows the result";
const check = task.verifier || {};
const checkDetail = check.command || check.endpoint || check.kind || "review";
inspector.innerHTML = `<strong>${escapeHtml(task.title)}</strong><br><strong>Result:</strong> ${escapeHtml(task.goal)}<br><strong>How it is checked:</strong> ${escapeHtml(checkDetail)}<br><strong>Proof to provide:</strong> ${evidence}<br><strong>Done when:</strong><ol>${criteria}</ol>`;
}
function renderGraph(plan, graphState) {
currentPlan = plan;
graph.textContent = "";
const title = document.querySelector("[data-graph-title]");
const model = document.querySelector("[data-graph-model]");
if (title) title.textContent = plan.title || "Task plan";
if (model) model.textContent = `${plan.tasks.length} clear ${plan.tasks.length === 1 ? "task" : "tasks"}`;
const shell = document.querySelector(".graph-shell");
if (shell) shell.dataset.graphState = graphState;
(plan.parallel_layers || [plan.tasks.map((task) => task.task_id)]).forEach((layer, layerIndex) => {
const column = document.createElement("div");
column.className = "graph-layer";
column.dataset.layer = String(layerIndex);
layer.forEach((taskId, taskIndex) => {
const task = plan.tasks.find((candidate) => candidate.task_id === taskId);
if (!task) return;
const node = document.createElement("button");
node.type = "button";
node.className = "graph-node";
node.dataset.taskId = task.task_id;
node.style.animationDelay = `${(layerIndex * 80) + (taskIndex * 40)}ms`;
const reward = task.suggested_solver_reward_usdc
? `${formatUsdc(task.suggested_solver_reward_usdc)} USDC suggested reward`
: "Choose a reward below";
node.innerHTML = `<span class="graph-node-id">Task ${layerIndex + 1}</span><strong>${escapeHtml(task.title)}</strong><span class="graph-node-meta">${escapeHtml(reward)}</span>`;
node.addEventListener("click", () => inspectTask(task));
node.addEventListener("mouseenter", () => highlightTask(task));
node.addEventListener("mouseleave", clearRelationshipHighlight);
column.append(node);
});
graph.append(column);
});
selectedTask = plan.tasks[0] || null;
if (selectedTask) inspectTask(selectedTask);
}
async function loadReadiness() {
const container = document.querySelector(".compiler-readiness");
const text = document.querySelector("[data-readiness-text]");
if (!container || !text) return;
try {
const response = await fetch(`${API}/v1/cloud-agent/readiness`, { cache: "no-store" });
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const readiness = await response.json();
const ready = readiness.available
&& readiness.protocol === "open_ai_responses"
&& String(readiness.model || "").startsWith("gpt-5.6");
container.dataset.compilerReadiness = ready ? "ready" : "unavailable";
text.textContent = ready ? "AI helper ready" : "AI helper unavailable — you can still fill the form yourself";
} catch (_error) {
container.dataset.compilerReadiness = "unavailable";
text.textContent = "AI helper unavailable — you can still fill the form yourself";
}
}
async function compileObjective(event) {
event.preventDefault();
submit.disabled = true;
submit.setAttribute("aria-busy", "true");
status.textContent = "Turning your goal into clear tasks…";
const data = new FormData(form);
const constraints = String(data.get("constraints") || "")
.split("\n")
.map((item) => item.trim())
.filter(Boolean);
const body = {
objective: String(data.get("objective") || "").trim(),
context: "Create clear, measurable tasks that a person or AI agent can complete and prove.",
constraints,
max_tasks: Number(data.get("max_tasks") || 5),
solver_budget_usdc: String(data.get("solver_budget_usdc") || "").trim() || null,
source_url: "https://github.com/NSPG13/agent-bounties/issues/513",
idempotency_key: `goal-planner:${crypto.randomUUID()}`,
};
try {
const response = await fetch(`${API}/v1/cloud-agent/objective-plans`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(body),
});
const result = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(result.message || result.error_code || `HTTP ${response.status}`);
renderGraph(result, "compiled");
status.textContent = `${result.tasks.length} tasks are ready to review. Choose one, then press “Use this task.”`;
if (typeof window.agentBountiesTrack === "function") {
window.agentBountiesTrack("objective_compiled", { task_count: result.tasks.length });
}
} catch (error) {
status.textContent = `${error.message}. Nothing was posted. You can still fill the posting form below.`;
} finally {
submit.disabled = false;
submit.removeAttribute("aria-busy");
}
}
function resetExample() {
form.elements.objective.value = EXAMPLE.objective;
form.elements.constraints.value = EXAMPLE.constraints.join("\n");
form.elements.solver_budget_usdc.value = EXAMPLE.budget;
renderGraph(previewPlan, "preview");
status.textContent = "Example loaded. Edit it or turn it into a new plan.";
}
function useSelectedTask() {
const postForm = document.getElementById("autonomous-post-form");
const section = document.getElementById("post");
if (!postForm || !selectedTask || !section) return;
postForm.elements.draftObjective.value = form.elements.objective.value.trim();
postForm.elements.title.value = selectedTask.title || "";
postForm.elements.goal.value = selectedTask.goal || "";
postForm.elements.acceptance.value = (selectedTask.acceptance_criteria || []).join("\n");
if (selectedTask.suggested_solver_reward_usdc) {
postForm.elements.solverReward.value = formatUsdc(selectedTask.suggested_solver_reward_usdc);
}
if (selectedTask.evidence_schema && postForm.elements.evidenceSchema) {
postForm.elements.evidenceSchema.value = JSON.stringify(selectedTask.evidence_schema, null, 2);
}
const output = document.getElementById("autonomous-post-output");
if (output) {
output.textContent = "Task copied from the plan. Review every field before connecting a wallet.";
output.dataset.tone = "pending";
}
section.scrollIntoView({ behavior: "smooth", block: "start" });
window.setTimeout(() => postForm.elements.title.focus({ preventScroll: true }), 400);
}
form.addEventListener("submit", compileObjective);
document.querySelector("[data-load-example]")?.addEventListener("click", resetExample);
document.querySelector("[data-use-selected-task]")?.addEventListener("click", useSelectedTask);
const params = new URLSearchParams(window.location.search);
const suppliedGoal = params.get("goal");
if (suppliedGoal) form.elements.objective.value = suppliedGoal;
renderGraph(previewPlan, "preview");
loadReadiness();
if (window.location.hash === "#post") {
window.requestAnimationFrame(() => document.getElementById("post")?.scrollIntoView({ block: "start" }));
}
})();