rhombus-node-mcp
Version:
MCP server for Rhombus API
196 lines (195 loc) • 7.29 kB
JavaScript
import { postApi } from "../network/network.js";
import { ChatFollowUpActionEnum, } from "../types/automated-prompts-tool-types.js";
import { formatIsoWithOffset } from "../util.js";
function extractNotifyUserUuids(followUpActions) {
if (!followUpActions)
return undefined;
const notifyAction = followUpActions.find((action) => action?.type === ChatFollowUpActionEnum.NOTIFY_USERS);
if (!notifyAction)
return undefined;
const userUuids = notifyAction.userUuids;
return userUuids?.filter((u) => u !== null) ?? [];
}
function toSummary(prompt, timeZone) {
if (!prompt)
return undefined;
return {
uuid: prompt.uuid ?? undefined,
prompt: prompt.prompt ?? undefined,
responseTemplate: prompt.responseTemplate ?? undefined,
frequency: prompt.frequency
? {
frequency: prompt.frequency.frequency ?? undefined,
unit: prompt.frequency.unit ?? undefined,
}
: undefined,
invokeAt: formatIsoWithOffset(prompt.invokeAtMs, timeZone),
permissionGroupUuid: prompt.permissionGroupUuid ?? undefined,
notifyUserUuids: extractNotifyUserUuids(prompt.followUpActions),
scheduleUuid: prompt.scheduleUuid ?? undefined,
orgUuid: prompt.orgUuid ?? undefined,
};
}
function toHistoryEntry(record, timeZone) {
if (!record)
return undefined;
return {
uuid: record.uuid ?? undefined,
automatedPromptUuid: record.automatedPromptUuid ?? undefined,
query: record.query ?? undefined,
response: record.response ?? undefined,
queriedAt: formatIsoWithOffset(record.queriedAtMs, timeZone),
respondedAt: formatIsoWithOffset(record.respondedAtMs, timeZone),
responseType: record.responseType ?? undefined,
};
}
function buildSettingsBody(input, includeUuid) {
const settings = {};
if (includeUuid && "promptUuid" in input && input.promptUuid) {
settings.uuid = input.promptUuid;
}
if (input.prompt !== undefined && input.prompt !== null) {
settings.prompt = input.prompt;
}
if (input.responseTemplate !== undefined && input.responseTemplate !== null) {
settings.responseTemplate = input.responseTemplate;
}
if (input.invokeAtMs !== undefined && input.invokeAtMs !== null) {
settings.invokeAtMs = input.invokeAtMs;
}
if (input.frequencyValue !== undefined &&
input.frequencyValue !== null &&
input.frequencyUnit !== undefined &&
input.frequencyUnit !== null) {
settings.frequency = {
frequency: input.frequencyValue,
unit: input.frequencyUnit,
};
}
if (input.permissionGroupUuid !== undefined && input.permissionGroupUuid !== null) {
settings.permissionGroupUuid = input.permissionGroupUuid;
}
if (input.notifyUserUuids !== undefined && input.notifyUserUuids !== null) {
settings.followUpActions = input.notifyUserUuids.length
? [
{
type: ChatFollowUpActionEnum.NOTIFY_USERS,
userUuids: input.notifyUserUuids,
},
]
: [];
}
return settings;
}
export async function listAutomatedPrompts(pageRequest, requestModifiers, sessionId, timeZone) {
const res = await postApi({
route: "/chatbot/automation/getAutomatedPromptsForOrg",
body: pageRequest,
modifiers: requestModifiers,
sessionId,
});
if (res.error)
throw new Error(JSON.stringify(res));
const settingsList = (res.settingsList ?? [])
.map((p) => toSummary(p, timeZone))
.filter((s) => s !== undefined);
return {
settingsList,
lastEvaluatedKey: res.lastEvaluatedKey ?? undefined,
};
}
export async function getAutomatedPrompt(promptUuid, requestModifiers, sessionId, timeZone) {
const res = await postApi({
route: "/chatbot/automation/getAutomatedPrompt",
body: { promptUuid },
modifiers: requestModifiers,
sessionId,
});
if (res.error)
throw new Error(JSON.stringify(res));
return toSummary(res.settings, timeZone);
}
export async function createAutomatedPrompt(input, requestModifiers, sessionId, timeZone) {
const settings = buildSettingsBody(input, /*includeUuid*/ false);
const res = await postApi({
route: "/chatbot/automation/createAutomatedPrompt",
body: { settings },
modifiers: requestModifiers,
sessionId,
});
if (res.error)
throw new Error(JSON.stringify(res));
return toSummary(res.settings, timeZone);
}
export async function updateAutomatedPrompt(input, requestModifiers, sessionId, timeZone) {
const selectiveUpdate = buildSettingsBody(input, /*includeUuid*/ true);
const res = await postApi({
route: "/chatbot/automation/updateAutomatedPrompt",
body: { selectiveUpdate },
modifiers: requestModifiers,
sessionId,
});
if (res.error)
throw new Error(JSON.stringify(res));
return toSummary(res.settings, timeZone);
}
export async function deleteAutomatedPrompt(promptUuid, requestModifiers, sessionId) {
const res = await postApi({
route: "/chatbot/automation/deleteAutomatedPrompt",
body: { promptUuid },
modifiers: requestModifiers,
sessionId,
});
if (res.error)
throw new Error(JSON.stringify(res));
return { ok: true, action: "deleted" };
}
export async function getAutomatedPromptChatHistory(promptUuid, pageRequest, requestModifiers, sessionId, timeZone) {
const res = await postApi({
route: "/chatbot/automation/getAutomatedPromptChatHistory",
body: {
promptUuid,
lastEvaluatedKey: pageRequest.lastEvaluatedKey ?? undefined,
maxPageSize: pageRequest.maxPageSize ?? undefined,
},
modifiers: requestModifiers,
sessionId,
});
if (res.error)
throw new Error(JSON.stringify(res));
const chatHistory = (res.chatHistory ?? [])
.map((r) => toHistoryEntry(r, timeZone))
.filter((e) => e !== undefined);
return {
chatHistory,
lastEvaluatedKey: res.lastEvaluatedKey ?? undefined,
};
}
export async function shareAutomatedPromptResponse(chatUuid, visibility, requestModifiers, sessionId) {
const res = await postApi({
route: "/chatbot/automation/shareAutomatedPromptResponse",
body: {
chatUuid,
privacy: { visibility },
},
modifiers: requestModifiers,
sessionId,
});
if (res.error)
throw new Error(JSON.stringify(res));
return { ok: true, action: `visibility set to ${visibility}` };
}
export async function verifyJobScheduled(promptUuid, requestModifiers, sessionId) {
const res = await postApi({
route: "/chatbot/automation/verifyJobScheduled",
body: { promptUuid },
modifiers: requestModifiers,
sessionId,
});
if (res.error)
throw new Error(JSON.stringify(res));
return {
scheduleExpression: res.scheduleExpression ?? undefined,
scheduleTimezone: res.scheduleTimezone ?? undefined,
};
}