feat: add external agent run worker entry

This commit is contained in:
John
2026-07-02 08:02:03 +08:00
parent 3ea973968e
commit 05d29ce62d
9 changed files with 7190 additions and 5 deletions
+2
View File
@@ -47,3 +47,5 @@ Streaming runtime operations:
node scripts/check-tool-runtime.mjs
node scripts/check-agent-code-run-entry.mjs
curl -sk https://mm.tkmind.cn/api/runtime/status # includes toolRuntime.queue
node scripts/agent-run-worker.mjs --status
node scripts/agent-run-worker.mjs --once
File diff suppressed because it is too large Load Diff
+42 -4
View File
@@ -6534,6 +6534,11 @@ function timeoutError(ms) {
err.code = "AGENT_RUN_TIMEOUT";
return err;
}
function envFlag(value, fallback = false) {
const raw = String(value ?? "").trim().toLowerCase();
if (!raw) return fallback;
return ["1", "true", "yes", "on"].includes(raw);
}
function normalizeAgentRunToolMode(value) {
const normalized = String(value ?? "chat").trim().toLowerCase();
if (!normalized || normalized === "chat") return "chat";
@@ -6589,7 +6594,7 @@ function createAgentRunGateway({
userAuth: userAuth2,
tkmindProxy: tkmindProxy2,
retryDelaysMs = DEFAULT_RUN_RETRY_DELAYS_MS,
autoDispatch = true,
autoDispatch = envFlag(process.env.MEMIND_AGENT_RUN_AUTODISPATCH, true),
maxConcurrentRuns = positiveInteger(
process.env.MEMIND_AGENT_RUN_QUEUE_CONCURRENCY,
DEFAULT_MAX_CONCURRENT_RUNS
@@ -6810,6 +6815,7 @@ function createAgentRunGateway({
statusCounts[row.status] = Number(row.count ?? 0);
}
return {
autoDispatch,
maxConcurrentRuns,
runTimeoutMs,
inFlight: inFlight.size,
@@ -6818,22 +6824,51 @@ function createAgentRunGateway({
terminalStatuses: [...TERMINAL_STATUSES]
};
}
async function dispatchQueuedRuns({ limit = maxConcurrentRuns } = {}) {
const dispatchLimit = Math.max(1, Number(limit) || maxConcurrentRuns);
const available = Math.max(0, maxConcurrentRuns - inFlight.size - queuedDispatches.length);
if (available <= 0) {
return {
considered: 0,
dispatched: 0,
queue: await getQueueStatus()
};
}
const take = Math.min(dispatchLimit, available);
const [rows] = await pool2.query(
`SELECT id
FROM h5_agent_runs
WHERE status IN ('queued', 'retryable')
ORDER BY updated_at ASC
LIMIT ?`,
[take]
);
for (const row of rows) {
dispatchRun(row.id);
}
return {
considered: rows.length,
dispatched: rows.length,
queue: await getQueueStatus()
};
}
return {
createRun,
getRunForUser,
dispatchRun,
getQueueStatus
getQueueStatus,
dispatchQueuedRuns
};
}
// agent-run-routes.mjs
function envFlag(value) {
function envFlag2(value) {
return ["1", "true", "yes", "on"].includes(String(value ?? "").trim().toLowerCase());
}
function createPostAgentRunsHandler({
userAuth: userAuth2,
agentRunGateway: agentRunGateway2,
codeRunsEnabled = envFlag(process.env.MEMIND_AGENT_CODE_RUNS_ENABLED)
codeRunsEnabled = envFlag2(process.env.MEMIND_AGENT_CODE_RUNS_ENABLED)
}) {
return async function postAgentRuns(request, response) {
try {
@@ -35041,6 +35076,9 @@ async function bootstrapUserAuth() {
pool: pool2,
userAuth,
tkmindProxy,
autoDispatch: ["1", "true", "yes", "on"].includes(
String(process.env.MEMIND_AGENT_RUN_AUTODISPATCH ?? "1").trim().toLowerCase()
),
maxConcurrentRuns: Number(process.env.MEMIND_AGENT_RUN_QUEUE_CONCURRENCY ?? 1),
runTimeoutMs: Number(process.env.MEMIND_AGENT_RUN_TIMEOUT_MS ?? 15 * 60 * 1e3)
});