anything-llm/server/utils/chats/agents.js

72 lines
1.7 KiB
JavaScript
Raw Normal View History

const pluralize = require("pluralize");
const {
WorkspaceAgentInvocation,
} = require("../../models/workspaceAgentInvocation");
const { writeResponseChunk } = require("../helpers/chat/responses");
async function grepAgents({
uuid,
response,
message,
workspace,
user = null,
thread = null,
}) {
const agentHandles = WorkspaceAgentInvocation.parseAgents(message);
if (agentHandles.length > 0) {
const { invocation: newInvocation } = await WorkspaceAgentInvocation.new({
prompt: message,
workspace: workspace,
user: user,
thread: thread,
});
if (!newInvocation) {
writeResponseChunk(response, {
id: uuid,
type: "statusResponse",
textResponse: `${pluralize(
"Agent",
agentHandles.length
)} ${agentHandles.join(
", "
)} could not be called. Chat will be handled as default chat.`,
sources: [],
close: true,
error: null,
});
return;
}
writeResponseChunk(response, {
id: uuid,
type: "agentInitWebsocketConnection",
textResponse: null,
sources: [],
close: false,
error: null,
websocketUUID: newInvocation.uuid,
});
// Close HTTP stream-able chunk response method because we will swap to agents now.
writeResponseChunk(response, {
id: uuid,
type: "statusResponse",
textResponse: `${pluralize(
"Agent",
agentHandles.length
)} ${agentHandles.join(
", "
)} invoked.\nSwapping over to agent chat. Type /exit to exit agent execution loop early.`,
sources: [],
close: true,
error: null,
});
return true;
}
return false;
}
module.exports = { grepAgents };