Give bots skills: runbooks they load when a job matches
A skill is a runbook: a name, a unique slug, a one-line description of when it applies, and the markdown body. A run only ever sees the slug and description (one load_skill tool carries them), and reads the whole body only when it calls the tool, so a long runbook costs nothing until needed. Skills are the third extension kind beside MCP servers and custom tools, on the same botExtensionsSql path, so per-bot, per-workspace and all-bots grants work the same way, and attachDefaultExtensions hands an every-bot skill to new bots. load_skill is a reserved tool name, so a custom tool cannot shadow it, and a built-in wins a clash. Managed on Settings > Skills; ExtensionRow links each kind to its own page and a skill never reports a failure, since nothing connects or runs.
This commit is contained in:
@@ -7,6 +7,7 @@ import type { BotExtensionsPayload } from "@/src/types";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
import parseWithSchema from "@/src/utils/parse-with-schema";
|
||||
import isReadOnlyUser from "@/src/utils/rbac/is-read-only-user";
|
||||
import extensionNoun, { extensionLogTitle } from "@/src/utils/extension-noun";
|
||||
import safeErrorMessage from "@/src/utils/safe-error-message";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
@@ -37,10 +38,10 @@ export const handler: BunextAPIRouteHandler<APIResponseObject<BotExtensionsPaylo
|
||||
const { bot, name } = await setBotExtension({ user_id: user.id, bot_id, kind, id, attached: true });
|
||||
|
||||
await logEvent({
|
||||
title: "Bot tool added",
|
||||
title: extensionLogTitle({ kind, verb: "added" }),
|
||||
category: "system",
|
||||
user_id: user.id,
|
||||
plain_text: `User #${user.id} gave ${kind == "mcp" ? "the MCP server" : "the tool"} ${name} to ${bot.name} (#${bot.id}).`,
|
||||
plain_text: `User #${user.id} gave ${extensionNoun({ kind, article: true })} ${name} to ${bot.name} (#${bot.id}).`,
|
||||
metadata: { bot_id, kind, id },
|
||||
ip_address: grabRequestIP(req),
|
||||
});
|
||||
|
||||
@@ -7,6 +7,7 @@ import type { BotExtensionsPayload } from "@/src/types";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
import parseWithSchema from "@/src/utils/parse-with-schema";
|
||||
import isReadOnlyUser from "@/src/utils/rbac/is-read-only-user";
|
||||
import extensionNoun, { extensionLogTitle } from "@/src/utils/extension-noun";
|
||||
import safeErrorMessage from "@/src/utils/safe-error-message";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
@@ -38,10 +39,10 @@ export const handler: BunextAPIRouteHandler<APIResponseObject<BotExtensionsPaylo
|
||||
const { bot, name } = await setBotExtension({ user_id: user.id, bot_id, kind, id, attached: false });
|
||||
|
||||
await logEvent({
|
||||
title: "Bot tool removed",
|
||||
title: extensionLogTitle({ kind, verb: "removed" }),
|
||||
category: "system",
|
||||
user_id: user.id,
|
||||
plain_text: `User #${user.id} took ${kind == "mcp" ? "the MCP server" : "the tool"} ${name} from ${bot.name} (#${bot.id}).`,
|
||||
plain_text: `User #${user.id} took ${extensionNoun({ kind, article: true })} ${name} from ${bot.name} (#${bot.id}).`,
|
||||
metadata: { bot_id, kind, id },
|
||||
ip_address: grabRequestIP(req),
|
||||
});
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import grabSuperAdminUser from "@/src/functions/backend/backups/grab-super-admin-user";
|
||||
import deleteSkill from "@/src/functions/backend/db/extensions/delete-skill";
|
||||
import grabSkillsOverview from "@/src/functions/backend/db/extensions/grab-skills-overview";
|
||||
import logEvent, { grabRequestIP } from "@/src/functions/backend/logger";
|
||||
import { SkillIdRequestSchema } from "@/src/schemas/extension-schema";
|
||||
import type { SkillsOverviewPayload } from "@/src/types";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
import parseWithSchema from "@/src/utils/parse-with-schema";
|
||||
import safeErrorMessage from "@/src/utils/safe-error-message";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
BunextAPIRouteHandler,
|
||||
} from "@moduletrace/bunext/types";
|
||||
|
||||
/**
|
||||
* Super admin only: deletes a skill. Bots lose it at once.
|
||||
*/
|
||||
export const handler: BunextAPIRouteHandler<
|
||||
APIResponseObject<SkillsOverviewPayload>
|
||||
> = async ({ req, body }) => {
|
||||
try {
|
||||
if (req.method != "POST") {
|
||||
throw new AppError(`Method not allowed`);
|
||||
}
|
||||
|
||||
const user = await grabSuperAdminUser({ req });
|
||||
|
||||
if (!user?.id) {
|
||||
throw new AppError(`Only a super admin can do that.`);
|
||||
}
|
||||
|
||||
const { skill_id } = parseWithSchema({ schema: SkillIdRequestSchema, data: body });
|
||||
const skill = await deleteSkill({ skill_id });
|
||||
|
||||
await logEvent({
|
||||
title: "Skill deleted",
|
||||
category: "system",
|
||||
user_id: user.id,
|
||||
plain_text: `User #${user.id} deleted the skill ${skill.name} (#${skill.id}, ${skill.slug}).`,
|
||||
metadata: { skill_id },
|
||||
ip_address: grabRequestIP(req),
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
msg: `Deleted ${skill.name}.`,
|
||||
singleRes: await grabSkillsOverview({ user_id: user.id }),
|
||||
};
|
||||
} catch (error: unknown) {
|
||||
return { success: false, msg: safeErrorMessage({ error }) };
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,52 @@
|
||||
import grabSuperAdminUser from "@/src/functions/backend/backups/grab-super-admin-user";
|
||||
import grabSkillsOverview from "@/src/functions/backend/db/extensions/grab-skills-overview";
|
||||
import saveSkill from "@/src/functions/backend/db/extensions/save-skill";
|
||||
import logEvent, { grabRequestIP } from "@/src/functions/backend/logger";
|
||||
import { SaveSkillRequestSchema } from "@/src/schemas/extension-schema";
|
||||
import type { SkillsOverviewPayload } from "@/src/types";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
import parseWithSchema from "@/src/utils/parse-with-schema";
|
||||
import safeErrorMessage from "@/src/utils/safe-error-message";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
BunextAPIRouteHandler,
|
||||
} from "@moduletrace/bunext/types";
|
||||
|
||||
/**
|
||||
* Super admin only: adds or edits a skill.
|
||||
*/
|
||||
export const handler: BunextAPIRouteHandler<
|
||||
APIResponseObject<SkillsOverviewPayload>
|
||||
> = async ({ req, body }) => {
|
||||
try {
|
||||
if (req.method != "POST") {
|
||||
throw new AppError(`Method not allowed`);
|
||||
}
|
||||
|
||||
const user = await grabSuperAdminUser({ req });
|
||||
|
||||
if (!user?.id) {
|
||||
throw new AppError(`Only a super admin can do that.`);
|
||||
}
|
||||
|
||||
const data = parseWithSchema({ schema: SaveSkillRequestSchema, data: body });
|
||||
const skill_id = await saveSkill({ user_id: user.id, data });
|
||||
|
||||
await logEvent({
|
||||
title: data.skill_id ? "Skill changed" : "Skill added",
|
||||
category: "system",
|
||||
user_id: user.id,
|
||||
plain_text: `User #${user.id} ${data.skill_id ? "changed" : "added"} the skill ${data.name} (#${skill_id}, ${data.slug}).`,
|
||||
metadata: { skill_id, slug: data.slug },
|
||||
ip_address: grabRequestIP(req),
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
msg: `Saved ${data.name}.`,
|
||||
singleRes: await grabSkillsOverview({ user_id: user.id }),
|
||||
};
|
||||
} catch (error: unknown) {
|
||||
return { success: false, msg: safeErrorMessage({ error }) };
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,59 @@
|
||||
import userAuth from "@/src/functions/backend/auth/user-auth";
|
||||
import grabSkillsOverview from "@/src/functions/backend/db/extensions/grab-skills-overview";
|
||||
import setExtensionAccess from "@/src/functions/backend/db/extensions/set-extension-access";
|
||||
import logEvent, { grabRequestIP } from "@/src/functions/backend/logger";
|
||||
import { SetExtensionAccessRequestSchema } from "@/src/schemas/extension-schema";
|
||||
import type { SkillsOverviewPayload } from "@/src/types";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
import parseWithSchema from "@/src/utils/parse-with-schema";
|
||||
import isReadOnlyUser from "@/src/utils/rbac/is-read-only-user";
|
||||
import safeErrorMessage from "@/src/utils/safe-error-message";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
BunextAPIRouteHandler,
|
||||
} from "@moduletrace/bunext/types";
|
||||
|
||||
/**
|
||||
* Super admins and admins: attaches a skill to exactly these bots and
|
||||
* workspaces. Only a super admin sets "all bots" (`setExtensionAccess`).
|
||||
*/
|
||||
export const handler: BunextAPIRouteHandler<
|
||||
APIResponseObject<SkillsOverviewPayload>
|
||||
> = async ({ req, body }) => {
|
||||
try {
|
||||
if (req.method != "POST") {
|
||||
throw new AppError(`Method not allowed`);
|
||||
}
|
||||
|
||||
const { user, user_types } = await userAuth({ req });
|
||||
|
||||
if (!user?.logged_in_status || !user.id) {
|
||||
return { success: false, msg: `Unauthorized`, logoutUser: true };
|
||||
}
|
||||
|
||||
if (isReadOnlyUser({ user_types: user_types || [] })) {
|
||||
throw new AppError(`This user can only read records.`);
|
||||
}
|
||||
|
||||
const access = parseWithSchema({ schema: SetExtensionAccessRequestSchema, data: body });
|
||||
|
||||
await setExtensionAccess({ user_id: user.id, kind: "skill", ...access });
|
||||
|
||||
await logEvent({
|
||||
title: "Skill access changed",
|
||||
category: "system",
|
||||
user_id: user.id,
|
||||
plain_text: `User #${user.id} changed who gets the skill #${access.id}.`,
|
||||
metadata: access,
|
||||
ip_address: grabRequestIP(req),
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
msg: `Saved who gets it.`,
|
||||
singleRes: await grabSkillsOverview({ user_id: user.id }),
|
||||
};
|
||||
} catch (error: unknown) {
|
||||
return { success: false, msg: safeErrorMessage({ error }) };
|
||||
}
|
||||
};
|
||||
@@ -7,6 +7,7 @@ import type { BotExtensionsPayload } from "@/src/types";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
import parseWithSchema from "@/src/utils/parse-with-schema";
|
||||
import isReadOnlyUser from "@/src/utils/rbac/is-read-only-user";
|
||||
import extensionNoun, { extensionLogTitle } from "@/src/utils/extension-noun";
|
||||
import safeErrorMessage from "@/src/utils/safe-error-message";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
@@ -37,10 +38,10 @@ export const handler: BunextAPIRouteHandler<APIResponseObject<BotExtensionsPaylo
|
||||
const { workspace, name } = await setWorkspaceExtension({ user_id: user.id, workspace_id, kind, id, attached: true });
|
||||
|
||||
await logEvent({
|
||||
title: "Workspace tool added",
|
||||
title: extensionLogTitle({ kind, verb: "added" }),
|
||||
category: "system",
|
||||
user_id: user.id,
|
||||
plain_text: `User #${user.id} gave ${kind == "mcp" ? "the MCP server" : "the tool"} ${name} to the workspace ${workspace.name} (#${workspace.id}).`,
|
||||
plain_text: `User #${user.id} gave ${extensionNoun({ kind, article: true })} ${name} to the workspace ${workspace.name} (#${workspace.id}).`,
|
||||
metadata: { workspace_id, kind, id },
|
||||
ip_address: grabRequestIP(req),
|
||||
});
|
||||
|
||||
@@ -7,6 +7,7 @@ import type { BotExtensionsPayload } from "@/src/types";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
import parseWithSchema from "@/src/utils/parse-with-schema";
|
||||
import isReadOnlyUser from "@/src/utils/rbac/is-read-only-user";
|
||||
import extensionNoun, { extensionLogTitle } from "@/src/utils/extension-noun";
|
||||
import safeErrorMessage from "@/src/utils/safe-error-message";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
@@ -36,10 +37,10 @@ export const handler: BunextAPIRouteHandler<APIResponseObject<BotExtensionsPaylo
|
||||
const { workspace, name } = await setWorkspaceExtension({ user_id: user.id, workspace_id, kind, id, attached: false });
|
||||
|
||||
await logEvent({
|
||||
title: "Workspace tool removed",
|
||||
title: extensionLogTitle({ kind, verb: "removed" }),
|
||||
category: "system",
|
||||
user_id: user.id,
|
||||
plain_text: `User #${user.id} took ${kind == "mcp" ? "the MCP server" : "the tool"} ${name} from the workspace ${workspace.name} (#${workspace.id}).`,
|
||||
plain_text: `User #${user.id} took ${extensionNoun({ kind, article: true })} ${name} from the workspace ${workspace.name} (#${workspace.id}).`,
|
||||
metadata: { workspace_id, kind, id },
|
||||
ip_address: grabRequestIP(req),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user