anything-llm/server/utils/chats/agents.js
Timothy Carambat a5bb77f97a
Agent support for @agent default agent inside workspace chat (#1093)
V1 of agent support via built-in `@agent` that can be invoked alongside normal workspace RAG chat.
2024-04-16 10:50:10 -07:00

72 lines
1.7 KiB
JavaScript

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 };