Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/prerna/reactor/agent/AgentRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ private static AgentHarnessResult run(String roomId, String input, String engine
AgentConfig agentConfig = AgentConfigLoader.load(room, filePath, modelId, params, agentParams, maxTurns,
maxReflections, explicitWorkspaceId);

// A run that starts an empty room claims it for the agent, as CreateRoom would.
if (!resumeMode) {
persistWorkspaceOnEmptyRoom(room, insight, explicitWorkspaceId);
}

try {
SkillStager.stage(filePath, agentConfig.getSkills());
} catch (Exception e) {
Expand Down Expand Up @@ -410,6 +415,47 @@ private static final class WorkspaceOverlay {
}
}

/**
* Saves {@code workspaceId} on a room that has no messages yet, both as the
* {@code ROOM.WORKSPACE_ID} column and as {@code options.workspace}, so a room
* started by RunAgent matches one created with {@code CreateRoom(workspaceId)}.
* Rooms with messages keep their saved workspace; the run only overlays it.
* Skipped when the workspace is missing, disabled, or not viewable by the
* user.
*/
static void persistWorkspaceOnEmptyRoom(Room room, Insight insight, String workspaceId) {
if (workspaceId == null || workspaceId.trim().isEmpty()) {
return;
}
List<?> messages = room.getMessages();
if (messages != null && !messages.isEmpty()) {
return;
}
try {
Map<String, Object> ws = ModelInferenceLogsUtils.getWorkspaceEntry(workspaceId);
if (ws == null || !Boolean.TRUE.equals(ws.get("is_active")) || insight.getUser() == null
|| !SecurityProjectUtils.userCanViewProject(insight.getUser(), workspaceId)) {
return;
}

Map<String, Object> workspace = new HashMap<>();
workspace.put("workspace_id", workspaceId);
if (ws.get("name") != null) {
workspace.put("name", String.valueOf(ws.get("name")));
}
Map<String, Object> opts = new HashMap<>(room.getOptionsMap());
opts.put("workspace", workspace);

ModelInferenceLogsUtils.setRoomWorkspaceId(room.getId(), room.getUserId(), workspaceId);
ModelInferenceLogsUtils.setRoomOptions(room.getId(), room.getUserId(), opts);
room.setOptionsMap(opts);
logger.info("AgentRunner: saved workspace '{}' on empty room '{}'", workspaceId, room.getId());
} catch (Exception e) {
logger.warn("AgentRunner: could not save workspace '{}' on room '{}': {}", workspaceId, room.getId(),
e.getMessage(), e);
}
}

/**
* Applies an in-memory workspace override for one run when needed.
*/
Expand Down
Loading