feat: add external agent run worker entry
This commit is contained in:
+38
-1
@@ -35,6 +35,12 @@ function timeoutError(ms) {
|
||||
return err;
|
||||
}
|
||||
|
||||
function envFlag(value, fallback = false) {
|
||||
const raw = String(value ?? '').trim().toLowerCase();
|
||||
if (!raw) return fallback;
|
||||
return ['1', 'true', 'yes', 'on'].includes(raw);
|
||||
}
|
||||
|
||||
export function normalizeAgentRunToolMode(value) {
|
||||
const normalized = String(value ?? 'chat').trim().toLowerCase();
|
||||
if (!normalized || normalized === 'chat') return 'chat';
|
||||
@@ -101,7 +107,7 @@ export function createAgentRunGateway({
|
||||
userAuth,
|
||||
tkmindProxy,
|
||||
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,
|
||||
@@ -340,6 +346,7 @@ export function createAgentRunGateway({
|
||||
statusCounts[row.status] = Number(row.count ?? 0);
|
||||
}
|
||||
return {
|
||||
autoDispatch,
|
||||
maxConcurrentRuns,
|
||||
runTimeoutMs,
|
||||
inFlight: inFlight.size,
|
||||
@@ -349,10 +356,40 @@ export function createAgentRunGateway({
|
||||
};
|
||||
}
|
||||
|
||||
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 pool.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,
|
||||
dispatchQueuedRuns,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user