akemon 0.1.74 → 0.1.76
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/server.js +33 -2
- package/package.json +1 -1
package/dist/server.js
CHANGED
|
@@ -14,6 +14,19 @@ import { selfDir, initWorld, initBioState, initGuide, biosPath, loadBioState, sa
|
|
|
14
14
|
// Engine mutual exclusion — only one engine process at a time
|
|
15
15
|
let engineBusy = false;
|
|
16
16
|
let engineBusySince = 0;
|
|
17
|
+
let lastEngineTrace = []; // execution trace for order reporting
|
|
18
|
+
/** Report an execution log to the relay (fire-and-forget) */
|
|
19
|
+
function reportExecutionLog(relayHttp, secretKey, agentName, type, refId, status, error, trace) {
|
|
20
|
+
if (!relayHttp || !secretKey)
|
|
21
|
+
return;
|
|
22
|
+
const traceJson = trace.length > 0 ? JSON.stringify(trace).slice(0, 50000) : "";
|
|
23
|
+
fetch(`${relayHttp}/v1/agent/${encodeURIComponent(agentName)}/logs`, {
|
|
24
|
+
method: "POST",
|
|
25
|
+
headers: { Authorization: `Bearer ${secretKey}`, "Content-Type": "application/json" },
|
|
26
|
+
body: JSON.stringify({ type, ref_id: refId, status, error: error.slice(0, 2000), trace: traceJson }),
|
|
27
|
+
signal: AbortSignal.timeout(10_000),
|
|
28
|
+
}).catch(() => { }); // fire-and-forget
|
|
29
|
+
}
|
|
17
30
|
// Order push notification — urgent orders bypass 30s poll
|
|
18
31
|
const urgentOrderIds = new Set();
|
|
19
32
|
let triggerWork = null;
|
|
@@ -824,6 +837,8 @@ async function runRawEngine(task, model, workdir) {
|
|
|
824
837
|
const apiUrl = RAW_API_URL + "/chat/completions";
|
|
825
838
|
const modelName = model || "gemma4:4b";
|
|
826
839
|
console.log(`[raw] Task:\n${task}`);
|
|
840
|
+
const trace = [];
|
|
841
|
+
lastEngineTrace = trace;
|
|
827
842
|
const messages = [
|
|
828
843
|
{ role: "system", content: "You are a helpful agent. Use tools when needed to complete the task. When done, reply with your final answer in plain text." },
|
|
829
844
|
{ role: "user", content: task },
|
|
@@ -848,6 +863,7 @@ async function runRawEngine(task, model, workdir) {
|
|
|
848
863
|
}
|
|
849
864
|
catch (err) {
|
|
850
865
|
console.log(`[raw] API error: ${err.message}`);
|
|
866
|
+
trace.push({ role: "error", content: err.message });
|
|
851
867
|
throw err;
|
|
852
868
|
}
|
|
853
869
|
const choice = data.choices?.[0];
|
|
@@ -870,6 +886,7 @@ async function runRawEngine(task, model, workdir) {
|
|
|
870
886
|
}
|
|
871
887
|
console.log(`[raw] Tool call: ${fnName}(${JSON.stringify(fnArgs).slice(0, 100)})`);
|
|
872
888
|
const result = await executeRawTool(fnName, fnArgs, workdir);
|
|
889
|
+
trace.push({ role: "tool_call", name: fnName, args: fnArgs, result: result.slice(0, 2000) });
|
|
873
890
|
messages.push({
|
|
874
891
|
role: "tool",
|
|
875
892
|
tool_call_id: tc.id,
|
|
@@ -882,6 +899,7 @@ async function runRawEngine(task, model, workdir) {
|
|
|
882
899
|
const content = msg.content || "";
|
|
883
900
|
if (content.trim()) {
|
|
884
901
|
console.log(`[raw] Done in ${round + 1} round(s), response:\n${content}`);
|
|
902
|
+
trace.push({ role: "assistant", content: content.trim().slice(0, 4000) });
|
|
885
903
|
return content.trim();
|
|
886
904
|
}
|
|
887
905
|
}
|
|
@@ -1074,6 +1092,7 @@ Reply ONLY with JSON.`;
|
|
|
1074
1092
|
}
|
|
1075
1093
|
catch (err) {
|
|
1076
1094
|
console.log(`[self] Digestion engine failed: ${err.message}`);
|
|
1095
|
+
reportExecutionLog(relayHttp, secretKey, agentName, "self_cycle", "digestion", "failed", err.message, lastEngineTrace);
|
|
1077
1096
|
engineBusy = false;
|
|
1078
1097
|
return;
|
|
1079
1098
|
}
|
|
@@ -1189,6 +1208,7 @@ Reply ONLY with the summary text, no JSON, no markdown headers.`;
|
|
|
1189
1208
|
}
|
|
1190
1209
|
catch (err) {
|
|
1191
1210
|
console.log(`[self] Activity ${activity} failed: ${err.message}`);
|
|
1211
|
+
reportExecutionLog(relayHttp, secretKey, agentName, "self_cycle", activity, "failed", err.message, lastEngineTrace);
|
|
1192
1212
|
}
|
|
1193
1213
|
engineBusy = false;
|
|
1194
1214
|
}
|
|
@@ -1200,6 +1220,7 @@ Reply ONLY with the summary text, no JSON, no markdown headers.`;
|
|
|
1200
1220
|
}
|
|
1201
1221
|
catch (err) {
|
|
1202
1222
|
console.log(`[self] Digestion error: ${err.message}`);
|
|
1223
|
+
reportExecutionLog(relayHttp, secretKey, agentName, "self_cycle", "digestion", "failed", err.message, lastEngineTrace);
|
|
1203
1224
|
}
|
|
1204
1225
|
}
|
|
1205
1226
|
async function syncToRelay(workdir, agentName, sd, relayHttp, secretKey, bio) {
|
|
@@ -1370,7 +1391,9 @@ When sub-order completes, incorporate result_text into YOUR delivery. Then call
|
|
|
1370
1391
|
}
|
|
1371
1392
|
}
|
|
1372
1393
|
console.log(`[orders] Fulfilling order ${order.id}...`);
|
|
1394
|
+
lastEngineTrace = [];
|
|
1373
1395
|
const result = await runEngine(engine, model, allowAll, taskPrompt, workdir, ["Bash(curl *)"]);
|
|
1396
|
+
const trace = lastEngineTrace;
|
|
1374
1397
|
const checkRes = await fetch(`${relayHttp}/v1/orders/${order.id}`);
|
|
1375
1398
|
const orderStatus = await checkRes.json();
|
|
1376
1399
|
if (orderStatus.status === "completed") {
|
|
@@ -1383,13 +1406,15 @@ When sub-order completes, incorporate result_text into YOUR delivery. Then call
|
|
|
1383
1406
|
}
|
|
1384
1407
|
else if (result && result.trim() !== "") {
|
|
1385
1408
|
console.log(`[orders] Auto-delivering order ${order.id} (agent did not self-deliver)`);
|
|
1409
|
+
const traceJson = trace.length > 0 ? JSON.stringify(trace).slice(0, 50000) : "";
|
|
1386
1410
|
const deliverRes = await fetch(`${relayHttp}/v1/orders/${order.id}/deliver`, {
|
|
1387
1411
|
method: "POST",
|
|
1388
1412
|
headers: { Authorization: `Bearer ${secretKey}`, "Content-Type": "application/json" },
|
|
1389
|
-
body: JSON.stringify({ result }),
|
|
1413
|
+
body: JSON.stringify({ result, trace: traceJson }),
|
|
1390
1414
|
});
|
|
1391
1415
|
if (deliverRes.ok) {
|
|
1392
1416
|
console.log(`[orders] Delivered order ${order.id} (${result.length} bytes)`);
|
|
1417
|
+
reportExecutionLog(relayHttp, secretKey, agentName, "order", order.id, "success", "", trace);
|
|
1393
1418
|
retryState.delete(order.id);
|
|
1394
1419
|
try {
|
|
1395
1420
|
await onTaskCompleted(workdir, agentName, true);
|
|
@@ -1406,6 +1431,7 @@ When sub-order completes, incorporate result_text into YOUR delivery. Then call
|
|
|
1406
1431
|
}
|
|
1407
1432
|
catch (err) {
|
|
1408
1433
|
console.log(`[orders] Failed to fulfill ${order.id}: ${err.message}`);
|
|
1434
|
+
reportExecutionLog(relayHttp, secretKey, agentName, "order", order.id, "failed", err.message, lastEngineTrace);
|
|
1409
1435
|
// Check if agent self-delivered despite empty stdout
|
|
1410
1436
|
try {
|
|
1411
1437
|
const checkRes = await fetch(`${relayHttp}/v1/orders/${order.id}`);
|
|
@@ -1439,8 +1465,11 @@ When sub-order completes, incorporate result_text into YOUR delivery. Then call
|
|
|
1439
1465
|
retryState.delete(order.id);
|
|
1440
1466
|
gaveUp.add(order.id);
|
|
1441
1467
|
try {
|
|
1468
|
+
const failTrace = lastEngineTrace.length > 0 ? JSON.stringify(lastEngineTrace).slice(0, 50000) : "";
|
|
1442
1469
|
await fetch(`${relayHttp}/v1/orders/${order.id}/cancel`, {
|
|
1443
|
-
method: "POST",
|
|
1470
|
+
method: "POST",
|
|
1471
|
+
headers: { Authorization: `Bearer ${secretKey}`, "Content-Type": "application/json" },
|
|
1472
|
+
body: JSON.stringify({ trace: failTrace }),
|
|
1444
1473
|
});
|
|
1445
1474
|
console.log(`[orders] Cancelled ${order.id} on relay`);
|
|
1446
1475
|
}
|
|
@@ -1481,6 +1510,7 @@ When sub-order completes, incorporate result_text into YOUR delivery. Then call
|
|
|
1481
1510
|
}
|
|
1482
1511
|
catch (err) {
|
|
1483
1512
|
console.log(`[tasks] Failed to execute ${task.id}: ${err.message}`);
|
|
1513
|
+
reportExecutionLog(relayHttp, secretKey, agentName, "platform_task", task.id, "failed", err.message, lastEngineTrace);
|
|
1484
1514
|
}
|
|
1485
1515
|
finally {
|
|
1486
1516
|
engineBusy = false;
|
|
@@ -1503,6 +1533,7 @@ When sub-order completes, incorporate result_text into YOUR delivery. Then call
|
|
|
1503
1533
|
}
|
|
1504
1534
|
catch (err) {
|
|
1505
1535
|
console.log(`[user-tasks] Failed: ${task.title}: ${err.message}`);
|
|
1536
|
+
reportExecutionLog(relayHttp, secretKey, agentName, "user_task", task.title, "failed", err.message, lastEngineTrace);
|
|
1506
1537
|
}
|
|
1507
1538
|
finally {
|
|
1508
1539
|
engineBusy = false;
|