naisys
Version:
NAISYS - Autonomous AI agent runner with built-in context management and cost tracking
60 lines • 2.4 kB
JavaScript
import { HubEvents } from "@naisys/hub-protocol";
export async function createRunService({ agentConfig },
/** Host's hubClient for a main agent; parent's hubClient for a subagent
* (the only hub access a subagent gets, scoped to SESSION_CREATE/INCREMENT). */
sessionHubClient, localUserId, subagentContext,
/** Set on the main-agent path: the hub already allocated the runId via
* AGENT_START, so we adopt it instead of round-tripping SESSION_CREATE. */
preallocated) {
/** The run ID of an agent process (there could be multiple runs for the same user). Globally unique */
let runId = -1;
/** The session number, incremented when the agent calls ns-session compact */
let sessionId = -1;
await init();
async function init() {
if (preallocated) {
runId = preallocated.runId;
sessionId = preallocated.sessionId;
}
else if (sessionHubClient) {
const response = await sessionHubClient.sendRequest(HubEvents.SESSION_CREATE, {
userId: subagentContext?.parentUserId ?? localUserId,
modelName: agentConfig().shellModel,
subagentId: subagentContext?.subagentId,
parentRunId: subagentContext?.parentRunId,
});
if (!response.success) {
throw new Error(`Failed to create session via hub: ${response.error}`);
}
runId = response.runId;
sessionId = response.sessionId;
}
else {
runId = subagentContext?.parentRunId ?? 1;
sessionId = 1;
}
}
async function incrementSession() {
if (sessionHubClient) {
const response = await sessionHubClient.sendRequest(HubEvents.SESSION_INCREMENT, {
userId: subagentContext?.parentUserId ?? localUserId,
runId,
subagentId: subagentContext?.subagentId,
modelName: agentConfig().shellModel,
});
if (!response.success) {
throw new Error(`Failed to increment session via hub: ${response.error}`);
}
sessionId = response.sessionId;
}
else {
sessionId++;
}
}
return {
incrementSession,
getRunId: () => runId,
getSessionId: () => sessionId,
};
}
//# sourceMappingURL=runService.js.map