Make the skill runbook limits a general setting
The longest runbook a skill may save and how much of it load_skill hands back are now set on Settings > General (skill_body_max_chars, skill_body_shown_chars), defaulting to AppData.SkillBodyMaxChars and SkillBodyShownChars. Saving, confirming and proposing a skill check the install's limit; a skill saved before it was lowered can still be toggled. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { BUN_SQLITE_COBBLE_BOTS } from "@/db/types/db";
|
||||
import { grabSkillBodyShownChars } from "@/src/functions/backend/db/app-settings/grab-skill-limits";
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
import {
|
||||
ActionCheckNote,
|
||||
@@ -204,6 +205,7 @@ export default async function runBotReply({
|
||||
*/
|
||||
const skills = openBotSkills({
|
||||
skills: extensions.skills,
|
||||
shown_chars: await grabSkillBodyShownChars(),
|
||||
taken: [...tools, ...extensions.tools].map((tool) => tool.name),
|
||||
});
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import grabBotRunPowers from "@/src/functions/backend/db/bot-types/grab-bot-run-powers";
|
||||
import { grabSkillBodyShownChars } from "@/src/functions/backend/db/app-settings/grab-skill-limits";
|
||||
import grabWorkspaceAdminTools from "@/src/harness/tools/bot/grab-workspace-admin-tools";
|
||||
import workspaceAdminsNote from "./workspace-admins-note";
|
||||
import type { BUN_SQLITE_COBBLE_BOTS } from "@/db/types/db";
|
||||
@@ -207,6 +208,7 @@ export default async function runBotTask({
|
||||
|
||||
skills = openBotSkills({
|
||||
skills: extensions.skills,
|
||||
shown_chars: await grabSkillBodyShownChars(),
|
||||
taken: [...tools, ...extensions.tools].map((tool) => tool.name),
|
||||
});
|
||||
|
||||
|
||||
@@ -309,10 +309,14 @@ export const AppData = {
|
||||
/**
|
||||
* Skills (Settings, under Skills): how long one's body may be, and how
|
||||
* much of it `load_skill` hands back in one call. A runbook past that
|
||||
* is worth splitting into two skills.
|
||||
* is worth splitting into two skills. These are the defaults: the
|
||||
* install sets its own on Settings > General (`skill_body_max_chars`,
|
||||
* `skill_body_shown_chars`, `grab-skill-limits.ts`), never past
|
||||
* `SkillBodyLimitMax`.
|
||||
*/
|
||||
SkillBodyMaxChars: 60000,
|
||||
SkillBodyShownChars: 30000,
|
||||
SkillBodyLimitMax: 1000000,
|
||||
SkillDescriptionMaxChars: 300,
|
||||
/**
|
||||
* How long the super admin has to confirm an MCP server or tool the
|
||||
|
||||
@@ -14,3 +14,11 @@ export const LoadSkillToolName = "load_skill";
|
||||
* The slug format, for the settings form and its hint.
|
||||
*/
|
||||
export const SkillSlugPattern = /^[a-z0-9]+(-[a-z0-9]+)*$/;
|
||||
|
||||
/**
|
||||
* The `app_settings` keys for the longest runbook a skill may save, and
|
||||
* how much of it `load_skill` hands back at once (Settings > General).
|
||||
* Unset, `AppData.SkillBodyMaxChars` and `AppData.SkillBodyShownChars`.
|
||||
*/
|
||||
export const SkillBodyMaxCharsSettingKey = "skill_body_max_chars";
|
||||
export const SkillBodyShownCharsSettingKey = "skill_body_shown_chars";
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
import { SkillBodyMaxCharsSettingKey, SkillBodyShownCharsSettingKey } from "@/src/dict/skill-dict";
|
||||
import readStepLimit from "@/src/utils/read-step-limit";
|
||||
import type { SkillLimitsOverview } from "@/src/types";
|
||||
import grabAppSetting from "./grab-app-setting";
|
||||
|
||||
/**
|
||||
* The longest runbook a skill may save: the install's own limit
|
||||
* (`skill_body_max_chars`, Settings > General), else
|
||||
* `AppData.SkillBodyMaxChars`.
|
||||
*/
|
||||
export async function grabSkillBodyMaxChars() {
|
||||
return readStepLimit({
|
||||
saved: await grabAppSetting({ key: SkillBodyMaxCharsSettingKey }),
|
||||
fallback: AppData.SkillBodyMaxChars,
|
||||
max: AppData.SkillBodyLimitMax,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* How much of a runbook `load_skill` hands back in one call: the install's
|
||||
* own limit (`skill_body_shown_chars`, Settings > General), else
|
||||
* `AppData.SkillBodyShownChars`.
|
||||
*/
|
||||
export async function grabSkillBodyShownChars() {
|
||||
return readStepLimit({
|
||||
saved: await grabAppSetting({ key: SkillBodyShownCharsSettingKey }),
|
||||
fallback: AppData.SkillBodyShownChars,
|
||||
max: AppData.SkillBodyLimitMax,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Both limits with their defaults, for the settings pages.
|
||||
*/
|
||||
export default async function grabSkillLimitsOverview() {
|
||||
const [body_max_chars, body_shown_chars] = await Promise.all([
|
||||
grabSkillBodyMaxChars(),
|
||||
grabSkillBodyShownChars(),
|
||||
]);
|
||||
|
||||
const overview: SkillLimitsOverview = {
|
||||
body_max_chars,
|
||||
body_shown_chars,
|
||||
body_max_default: AppData.SkillBodyMaxChars,
|
||||
body_shown_default: AppData.SkillBodyShownChars,
|
||||
max: AppData.SkillBodyLimitMax,
|
||||
};
|
||||
|
||||
return overview;
|
||||
}
|
||||
|
||||
export type { SkillLimitsOverview };
|
||||
@@ -0,0 +1,20 @@
|
||||
import { grabSkillBodyMaxChars } from "../app-settings/grab-skill-limits";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
|
||||
type Params = {
|
||||
body: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Refuses a runbook longer than the install's limit (Settings > General).
|
||||
* The request schemas only hold the ceiling, since the limit is a setting.
|
||||
*/
|
||||
export default async function assertSkillBodyFits({ body }: Params) {
|
||||
const max = await grabSkillBodyMaxChars();
|
||||
|
||||
if (body.length > max) {
|
||||
throw new AppError(
|
||||
`Keep the runbook under ${max} characters (it has ${body.length}). A super admin can raise the limit on Settings > General.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { BUN_SQLITE_COBBLE_SKILLS } from "@/db/types/db";
|
||||
import type { SkillsOverviewPayload } from "@/src/types";
|
||||
import grabSkillLimitsOverview from "../app-settings/grab-skill-limits";
|
||||
import grabCrewRole from "../access/grab-crew-role";
|
||||
import grabExtensionAccess from "./grab-extension-access";
|
||||
import grabExtensionTargets from "./grab-extension-targets";
|
||||
@@ -17,14 +18,16 @@ type Params = {
|
||||
*/
|
||||
export default async function grabSkillsOverview({ user_id }: Params): Promise<SkillsOverviewPayload> {
|
||||
const role = await grabCrewRole({ user_id });
|
||||
const [rows, access, targets] = await Promise.all([
|
||||
const [rows, access, targets, limits] = await Promise.all([
|
||||
BunSQLite.sql<BUN_SQLITE_COBBLE_SKILLS>({ sql: `SELECT * FROM skills ORDER BY name COLLATE NOCASE ASC, id ASC` }),
|
||||
grabExtensionAccess({ kind: "skill" }),
|
||||
grabExtensionTargets({ role }),
|
||||
grabSkillLimitsOverview(),
|
||||
]);
|
||||
|
||||
return {
|
||||
...targets,
|
||||
limits,
|
||||
skills: (rows.payload || []).map((row) => {
|
||||
const { all_bots, ...skill } = parseSkillRow({ row });
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import attachToAllBots from "./attach-to-all-bots";
|
||||
import type { BUN_SQLITE_COBBLE_SKILLS, BunSQLiteTables } from "@/db/types/db";
|
||||
import type { SaveSkillRequest } from "@/src/schemas/extension-schema";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
import assertSkillBodyFits from "./assert-skill-body-fits";
|
||||
import grabSkill from "./grab-skill";
|
||||
|
||||
type Params = {
|
||||
@@ -15,7 +16,9 @@ type Params = {
|
||||
|
||||
/**
|
||||
* Adds or edits a skill and returns its id. Slugs are unique: a bot loads a
|
||||
* skill by its slug, so two skills can't answer to the same one.
|
||||
* skill by its slug, so two skills can't answer to the same one. A new or
|
||||
* changed runbook must fit the install's limit; one saved before the limit
|
||||
* was lowered keeps saving while its runbook is left as it is.
|
||||
*/
|
||||
export default async function saveSkill({ user_id, data }: Params) {
|
||||
const clash = (
|
||||
@@ -38,6 +41,9 @@ export default async function saveSkill({ user_id, data }: Params) {
|
||||
|
||||
if (data.skill_id) {
|
||||
const skill = await grabSkill({ skill_id: data.skill_id });
|
||||
|
||||
if (data.body != skill.body) await assertSkillBodyFits({ body: data.body });
|
||||
|
||||
const res = await BunSQLite.update<BUN_SQLITE_COBBLE_SKILLS, (typeof BunSQLiteTables)[number]>({
|
||||
table: "skills",
|
||||
targetId: skill.id,
|
||||
@@ -51,6 +57,8 @@ export default async function saveSkill({ user_id, data }: Params) {
|
||||
return skill.id;
|
||||
}
|
||||
|
||||
await assertSkillBodyFits({ body: data.body });
|
||||
|
||||
const res = await BunSQLite.insert<BUN_SQLITE_COBBLE_SKILLS, (typeof BunSQLiteTables)[number]>({
|
||||
table: "skills",
|
||||
data: [{ ...fields, user_id: Number(user_id) }],
|
||||
|
||||
@@ -2,6 +2,7 @@ import { z } from "zod";
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
import createExtensionProposal from "@/src/functions/backend/db/extension-proposals/create-extension-proposal";
|
||||
import grabExtensionProposalView from "@/src/functions/backend/db/extension-proposals/grab-extension-proposal-view";
|
||||
import assertSkillBodyFits from "@/src/functions/backend/db/extensions/assert-skill-body-fits";
|
||||
import grabSkill from "@/src/functions/backend/db/extensions/grab-skill";
|
||||
import { SaveSkillRequestSchema } from "@/src/schemas/extension-schema";
|
||||
import parseWithSchema from "@/src/utils/parse-with-schema";
|
||||
@@ -51,7 +52,7 @@ export default function proposeSkillTool({ user_id, bot_id, session_id, workspac
|
||||
name: z.string().trim().max(80).optional(),
|
||||
slug: z.string().trim().max(80).optional(),
|
||||
description: z.string().trim().max(AppData.SkillDescriptionMaxChars).optional(),
|
||||
body: z.string().max(AppData.SkillBodyMaxChars).optional().describe("The runbook, markdown"),
|
||||
body: z.string().max(AppData.SkillBodyLimitMax).optional().describe("The runbook, markdown"),
|
||||
enabled: z.boolean().optional(),
|
||||
all_bots: z.boolean().optional(),
|
||||
...SkillReviewFields,
|
||||
@@ -73,6 +74,8 @@ export default function proposeSkillTool({ user_id, bot_id, session_id, workspac
|
||||
},
|
||||
});
|
||||
|
||||
if (data.body != current?.body) await assertSkillBodyFits({ body: data.body });
|
||||
|
||||
const id = await createExtensionProposal({
|
||||
user_id,
|
||||
bot_id,
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
import type { SkillRecord } from "@/src/types";
|
||||
import openBotSkills from "./open-bot-skills";
|
||||
|
||||
const shown_chars = 30000;
|
||||
|
||||
function skill(overrides: Partial<SkillRecord> & { slug: string }): SkillRecord {
|
||||
return {
|
||||
id: 1,
|
||||
@@ -17,18 +18,18 @@ function skill(overrides: Partial<SkillRecord> & { slug: string }): SkillRecord
|
||||
|
||||
describe("openBotSkills", () => {
|
||||
test("no skills means no tool and no note", () => {
|
||||
expect(openBotSkills({ skills: [] })).toEqual({ tools: [], note: "" });
|
||||
expect(openBotSkills({ skills: [], shown_chars })).toEqual({ tools: [], note: "" });
|
||||
});
|
||||
|
||||
test("a disabled skill is left out entirely", () => {
|
||||
const opened = openBotSkills({ skills: [skill({ slug: "seo", enabled: false })] });
|
||||
const opened = openBotSkills({ skills: [skill({ slug: "seo", enabled: false })], shown_chars });
|
||||
|
||||
expect(opened.tools).toHaveLength(0);
|
||||
expect(opened.note).toBe("");
|
||||
});
|
||||
|
||||
test("one tool, named load_skill, whose description lists the slugs", () => {
|
||||
const opened = openBotSkills({ skills: [skill({ slug: "seo" }), skill({ slug: "inbox", id: 2 })] });
|
||||
const opened = openBotSkills({ skills: [skill({ slug: "seo" }), skill({ slug: "inbox", id: 2 })], shown_chars });
|
||||
|
||||
expect(opened.tools).toHaveLength(1);
|
||||
expect(opened.tools[0]!.name).toBe("load_skill");
|
||||
@@ -38,21 +39,21 @@ describe("openBotSkills", () => {
|
||||
});
|
||||
|
||||
test("a built-in that took the name wins", () => {
|
||||
expect(openBotSkills({ skills: [skill({ slug: "seo" })], taken: ["load_skill"] })).toEqual({
|
||||
expect(openBotSkills({ skills: [skill({ slug: "seo" })], taken: ["load_skill"], shown_chars })).toEqual({
|
||||
tools: [],
|
||||
note: "",
|
||||
});
|
||||
});
|
||||
|
||||
test("the tool returns the body of the slug it's given", async () => {
|
||||
const opened = openBotSkills({ skills: [skill({ slug: "seo", body: "1. Pull rankings" })] });
|
||||
const opened = openBotSkills({ skills: [skill({ slug: "seo", body: "1. Pull rankings" })], shown_chars });
|
||||
const output = await opened.tools[0]!.run!({ args: { slug: "seo" }, signal: AbortSignal.timeout(1000) });
|
||||
|
||||
expect(output).toBe("1. Pull rankings");
|
||||
});
|
||||
|
||||
test("an unknown slug fails with the slugs it does have", async () => {
|
||||
const opened = openBotSkills({ skills: [skill({ slug: "seo" })] });
|
||||
const opened = openBotSkills({ skills: [skill({ slug: "seo" })], shown_chars });
|
||||
|
||||
await expect(opened.tools[0]!.run!({ args: { slug: "nope" }, signal: AbortSignal.timeout(1000) })).rejects.toThrow(
|
||||
/no skill called "nope".*seo/,
|
||||
@@ -61,16 +62,24 @@ describe("openBotSkills", () => {
|
||||
|
||||
test("a long body is cut at the limit and says so", async () => {
|
||||
const opened = openBotSkills({
|
||||
skills: [skill({ slug: "seo", body: "x".repeat(AppData.SkillBodyShownChars + 500) })],
|
||||
skills: [skill({ slug: "seo", body: "x".repeat(shown_chars + 500) })],
|
||||
shown_chars,
|
||||
});
|
||||
const output = await opened.tools[0]!.run!({ args: { slug: "seo" }, signal: AbortSignal.timeout(1000) });
|
||||
|
||||
expect(output).toContain("Cut at");
|
||||
expect(output.length).toBeLessThan(AppData.SkillBodyShownChars + 200);
|
||||
expect(output.length).toBeLessThan(shown_chars + 200);
|
||||
});
|
||||
|
||||
test("the cut follows the install's limit", async () => {
|
||||
const opened = openBotSkills({ skills: [skill({ slug: "seo", body: "x".repeat(1500) })], shown_chars: 1000 });
|
||||
const output = await opened.tools[0]!.run!({ args: { slug: "seo" }, signal: AbortSignal.timeout(1000) });
|
||||
|
||||
expect(output).toContain("Cut at 1000 characters");
|
||||
});
|
||||
|
||||
test("an empty body says it is empty rather than returning nothing", async () => {
|
||||
const opened = openBotSkills({ skills: [skill({ slug: "seo", body: "" })] });
|
||||
const opened = openBotSkills({ skills: [skill({ slug: "seo", body: "" })], shown_chars });
|
||||
const output = await opened.tools[0]!.run!({ args: { slug: "seo" }, signal: AbortSignal.timeout(1000) });
|
||||
|
||||
expect(output).toContain("is empty");
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { z } from "zod";
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
import { SkillsNote } from "@/src/data/harness-prompts";
|
||||
import { LoadSkillToolName } from "@/src/dict/skill-dict";
|
||||
import type { SkillRecord } from "@/src/types";
|
||||
@@ -10,6 +9,11 @@ type Params = {
|
||||
* The skills the run reaches (`grabBotExtensions`).
|
||||
*/
|
||||
skills: SkillRecord[];
|
||||
/**
|
||||
* How much of a runbook `load_skill` hands back at once
|
||||
* (`grabSkillBodyShownChars`).
|
||||
*/
|
||||
shown_chars: number;
|
||||
/**
|
||||
* Names the run's built-in tools already use: they win.
|
||||
*/
|
||||
@@ -30,7 +34,7 @@ export type OpenBotSkills = {
|
||||
* which ones exist. A skill's body costs nothing until the bot asks for it,
|
||||
* so a run that never needs a long runbook never pays for it.
|
||||
*/
|
||||
export default function openBotSkills({ skills, taken = [] }: Params): OpenBotSkills {
|
||||
export default function openBotSkills({ skills, shown_chars, taken = [] }: Params): OpenBotSkills {
|
||||
const enabled = skills.filter((skill) => skill.enabled);
|
||||
|
||||
if (!enabled.length || taken.includes(LoadSkillToolName)) return { tools: [], note: "" };
|
||||
@@ -57,9 +61,9 @@ export default function openBotSkills({ skills, taken = [] }: Params): OpenBotSk
|
||||
|
||||
if (!body) return `The skill ${skill.name} (${skill.slug}) is empty.`;
|
||||
|
||||
if (body.length <= AppData.SkillBodyShownChars) return body;
|
||||
if (body.length <= shown_chars) return body;
|
||||
|
||||
return `${body.slice(0, AppData.SkillBodyShownChars)}\n\n[Cut at ${AppData.SkillBodyShownChars} characters. The skill continues: ask the user to split it if you need the rest.]`;
|
||||
return `${body.slice(0, shown_chars)}\n\n[Cut at ${shown_chars} characters. The skill continues: ask the user to split it if you need the rest.]`;
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import grabUserPasswordHash from "@/src/functions/backend/auth/grab-user-passwor
|
||||
import type { PagePropsType } from "@/src/types";
|
||||
import type { BunextPageServerFn } from "@moduletrace/bunext/types";
|
||||
import grabRunLimitsOverview from "@/src/functions/backend/db/app-settings/grab-run-limits";
|
||||
import grabSkillLimitsOverview from "@/src/functions/backend/db/app-settings/grab-skill-limits";
|
||||
import grabStorageQuotaOverview from "@/src/functions/backend/db/app-settings/grab-storage-quota-overview";
|
||||
import { grabSystemPromptAppendOverview } from "@/src/functions/backend/db/app-settings/grab-system-prompt-append";
|
||||
import runningVersion from "@/src/utils/running-version";
|
||||
@@ -47,6 +48,7 @@ export default async function grabSettingsServerProps({ req }: Params) {
|
||||
storage_quota: await grabStorageQuotaOverview().catch(() => null),
|
||||
system_prompt_append: await grabSystemPromptAppendOverview().catch(() => null),
|
||||
run_limits: await grabRunLimitsOverview().catch(() => null),
|
||||
skill_limits: await grabSkillLimitsOverview().catch(() => null),
|
||||
running_version: runningVersion({ env: process.env }),
|
||||
},
|
||||
};
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
import { useState } from "react";
|
||||
import fetchApi from "@moduletrace/twui/utils/fetch/fetchApi";
|
||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||
import Form from "@moduletrace/twui/form/Form";
|
||||
import Input from "@moduletrace/twui/form/Input";
|
||||
import Stack from "@moduletrace/twui/layout/Stack";
|
||||
import type { SkillLimitsOverview } from "@/src/types";
|
||||
import SettingsSaveBar from "../../(partials)/settings-save-bar";
|
||||
|
||||
type Props = {
|
||||
limits?: SkillLimitsOverview | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* The longest runbook a skill may save and how much of it a bot reads at
|
||||
* once, saved on its own without a reload. Zero puts the code's default
|
||||
* back for that one.
|
||||
*/
|
||||
export default function SkillLimitsForm({ limits }: Props) {
|
||||
const [overview, setOverview] = useState(limits || undefined);
|
||||
const [dirty, setDirty] = useState(false);
|
||||
const [body_max_chars, setBodyMaxChars] = useState(String(limits?.body_max_chars ?? ""));
|
||||
const [body_shown_chars, setBodyShownChars] = useState(String(limits?.body_shown_chars ?? ""));
|
||||
/**
|
||||
* The inputs only read `defaultValue` when they mount, so discarding or
|
||||
* saving remounts them with the saved numbers.
|
||||
*/
|
||||
const [form_key, setFormKey] = useState(0);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [status, setStatus] = useState<{
|
||||
msg?: string;
|
||||
error?: boolean;
|
||||
success?: boolean;
|
||||
}>();
|
||||
|
||||
const save = async () => {
|
||||
if (!window.confirm("Save the skill limits?")) return;
|
||||
|
||||
setLoading(true);
|
||||
setStatus(undefined);
|
||||
|
||||
const res = await fetchApi<{}, APIResponseObject<SkillLimitsOverview>>(
|
||||
"/api/admin/settings/skill-limits",
|
||||
{
|
||||
method: "POST",
|
||||
body: {
|
||||
body_max_chars: Number(body_max_chars) || 0,
|
||||
body_shown_chars: Number(body_shown_chars) || 0,
|
||||
},
|
||||
},
|
||||
).catch(() => null);
|
||||
|
||||
setLoading(false);
|
||||
|
||||
if (!res?.success || !res.singleRes) {
|
||||
setStatus({
|
||||
error: true,
|
||||
msg: res?.msg || `Couldn't save the limits.`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
setOverview(res.singleRes);
|
||||
setBodyMaxChars(String(res.singleRes.body_max_chars));
|
||||
setBodyShownChars(String(res.singleRes.body_shown_chars));
|
||||
setFormKey((key) => key + 1);
|
||||
setDirty(false);
|
||||
setStatus({
|
||||
success: true,
|
||||
msg: `Saved: runbooks up to ${res.singleRes.body_max_chars} characters, ${res.singleRes.body_shown_chars} read at once.`,
|
||||
});
|
||||
};
|
||||
|
||||
const discard = () => {
|
||||
setBodyMaxChars(String(overview?.body_max_chars ?? ""));
|
||||
setBodyShownChars(String(overview?.body_shown_chars ?? ""));
|
||||
setFormKey((key) => key + 1);
|
||||
setDirty(false);
|
||||
setStatus(undefined);
|
||||
};
|
||||
|
||||
return (
|
||||
<Form
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
save().catch(() => {});
|
||||
}}
|
||||
className="w-full gap-3! items-stretch"
|
||||
>
|
||||
<Stack className="w-full gap-3! items-stretch">
|
||||
<Input
|
||||
key={`max-${form_key}`}
|
||||
label="Longest runbook"
|
||||
type="number"
|
||||
min={0}
|
||||
max={overview?.max}
|
||||
step={1}
|
||||
numberText
|
||||
rawNumber
|
||||
defaultValue={body_max_chars}
|
||||
onChange={(event) => {
|
||||
setBodyMaxChars(event.target.value);
|
||||
setDirty(true);
|
||||
setStatus(undefined);
|
||||
}}
|
||||
suffix={`characters`}
|
||||
info={`How long a skill's runbook may be when it's saved or proposed. A skill already longer keeps working until its runbook is edited. 0 uses the default (${overview?.body_max_default ?? ""}).`}
|
||||
/>
|
||||
<Input
|
||||
key={`shown-${form_key}`}
|
||||
label="Runbook a bot reads at once"
|
||||
type="number"
|
||||
min={0}
|
||||
max={overview?.max}
|
||||
step={1}
|
||||
numberText
|
||||
rawNumber
|
||||
defaultValue={body_shown_chars}
|
||||
onChange={(event) => {
|
||||
setBodyShownChars(event.target.value);
|
||||
setDirty(true);
|
||||
setStatus(undefined);
|
||||
}}
|
||||
suffix={`characters`}
|
||||
info={`How much of a runbook a bot gets when it loads the skill; past that it's cut and the bot is told so. All of it goes into the model's context. 0 uses the default (${overview?.body_shown_default ?? ""}).`}
|
||||
/>
|
||||
</Stack>
|
||||
<SettingsSaveBar
|
||||
dirty={dirty}
|
||||
loading={loading}
|
||||
status={status}
|
||||
save_label="Save limits"
|
||||
onDiscard={discard}
|
||||
/>
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { useContext } from "react";
|
||||
import { AppContext } from "@/src/pages/__root";
|
||||
import isPassThroughUser from "@/src/utils/rbac/is-pass-through-user";
|
||||
import SettingsGroup from "../../(partials)/settings-group";
|
||||
import SkillLimitsForm from "../(partials)/skill-limits-form";
|
||||
|
||||
/**
|
||||
* How long a skill's runbook may be, and how much of it a bot reads at
|
||||
* once. Only the super admin sees it, since it changes every skill.
|
||||
*/
|
||||
export default function SkillLimitsSection() {
|
||||
const { pageProps } = useContext(AppContext);
|
||||
|
||||
if (!isPassThroughUser({ user_types: pageProps?.user_types })) return null;
|
||||
|
||||
return (
|
||||
<SettingsGroup
|
||||
title="Skill limits"
|
||||
description="How long a skill's runbook may be, and how much of it a bot reads when it loads the skill. A longer read costs more context on every load."
|
||||
id="skill-limits"
|
||||
>
|
||||
<SkillLimitsForm limits={pageProps?.skill_limits} />
|
||||
</SettingsGroup>
|
||||
);
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import AppearanceSection from "./(sections)/appearance-section";
|
||||
import NotificationsSection from "./(sections)/notifications-section";
|
||||
import PromptSection from "./(sections)/prompt-section";
|
||||
import RunLimitsSection from "./(sections)/run-limits-section";
|
||||
import SkillLimitsSection from "./(sections)/skill-limits-section";
|
||||
import StorageSection from "./(sections)/storage-section";
|
||||
import VersionSection from "./(sections)/version-section";
|
||||
import SettingsShell from "../(partials)/settings-shell";
|
||||
@@ -17,6 +18,7 @@ export default function AdminSettingsGeneralPage() {
|
||||
<NotificationsSection />
|
||||
<PromptSection />
|
||||
<RunLimitsSection />
|
||||
<SkillLimitsSection />
|
||||
<StorageSection />
|
||||
<VersionSection />
|
||||
</SettingsShell>
|
||||
|
||||
@@ -6,15 +6,18 @@ import Form from "@moduletrace/twui/form/Form";
|
||||
import Input from "@moduletrace/twui/form/Input";
|
||||
import Textarea from "@moduletrace/twui/form/Textarea";
|
||||
import CodeEditor from "@/src/components/general/code-editor";
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
import postAdminApi from "@/src/layouts/bots/(functions)/post-admin-api";
|
||||
import type { PublicSkill, SkillsOverviewPayload } from "@/src/types";
|
||||
import type { PublicSkill, SkillLimitsOverview, SkillsOverviewPayload } from "@/src/types";
|
||||
|
||||
type Props = {
|
||||
/**
|
||||
* Edits this skill; adds one when not given.
|
||||
*/
|
||||
skill?: PublicSkill;
|
||||
/**
|
||||
* The install's runbook limits, for the hint.
|
||||
*/
|
||||
limits: SkillLimitsOverview;
|
||||
onSaved: (overview: SkillsOverviewPayload, msg: string) => void;
|
||||
onCancel: () => void;
|
||||
};
|
||||
@@ -24,7 +27,7 @@ type Props = {
|
||||
* loads it by, the one line that tells the bot when it applies, and the
|
||||
* runbook itself in markdown.
|
||||
*/
|
||||
export default function SkillModal({ skill, onSaved, onCancel }: Props) {
|
||||
export default function SkillModal({ skill, limits, onSaved, onCancel }: Props) {
|
||||
const [name, setName] = useState(skill?.name || "");
|
||||
const [slug, setSlug] = useState(skill?.slug || "");
|
||||
const [description, setDescription] = useState(skill?.description || "");
|
||||
@@ -103,7 +106,7 @@ export default function SkillModal({ skill, onSaved, onCancel }: Props) {
|
||||
className="h-80"
|
||||
title="Runbook"
|
||||
content={body}
|
||||
info={`Markdown. The steps, the checks, and what good looks like. A bot reads it only when it loads the skill, up to ${AppData.SkillBodyShownChars} characters of it at a time.`}
|
||||
info={`Markdown. The steps, the checks, and what good looks like. Up to ${limits.body_max_chars} characters. A bot reads it only when it loads the skill, up to ${limits.body_shown_chars} characters of it at a time.`}
|
||||
onChange={(value) => setBody(value)}
|
||||
/>
|
||||
<Checkbox title="On" label="On: bots get it" checked={enabled} changeHandler={setEnabled} />
|
||||
|
||||
@@ -3,7 +3,7 @@ import Stack from "@moduletrace/twui/layout/Stack";
|
||||
import { BookOpen } from "lucide-react";
|
||||
import IconTile from "@/src/components/general/icon-tile";
|
||||
import postAdminApi from "@/src/layouts/bots/(functions)/post-admin-api";
|
||||
import type { ExtensionTargets, PublicSkill, SkillsOverviewPayload } from "@/src/types";
|
||||
import type { ExtensionTargets, PublicSkill, SkillLimitsOverview, SkillsOverviewPayload } from "@/src/types";
|
||||
import ExtensionAccessModal from "../../(partials)/extension-access-modal";
|
||||
import SettingsListRow from "../../(partials)/settings-list-row";
|
||||
import SettingsRowAction from "../../(partials)/settings-row-action";
|
||||
@@ -13,6 +13,7 @@ import SkillModal from "./skill-modal";
|
||||
type Props = {
|
||||
skill: PublicSkill;
|
||||
targets: ExtensionTargets;
|
||||
limits: SkillLimitsOverview;
|
||||
onChanged: (overview: SkillsOverviewPayload, msg: string) => void;
|
||||
onError: (msg: string) => void;
|
||||
};
|
||||
@@ -21,7 +22,7 @@ type Props = {
|
||||
* One skill: its slug and when it applies, and who gets it. Edit and Access
|
||||
* open in a modal.
|
||||
*/
|
||||
export default function SkillRow({ skill, targets, onChanged, onError }: Props) {
|
||||
export default function SkillRow({ skill, targets, limits, onChanged, onError }: Props) {
|
||||
const [open, setOpen] = useState<"edit" | "access">();
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
||||
@@ -73,7 +74,7 @@ export default function SkillRow({ skill, targets, onChanged, onError }: Props)
|
||||
) : null}
|
||||
</SettingsListRow>
|
||||
{open == "edit" ? (
|
||||
<SkillModal skill={skill} onSaved={saved} onCancel={() => setOpen(undefined)} />
|
||||
<SkillModal skill={skill} limits={limits} onSaved={saved} onCancel={() => setOpen(undefined)} />
|
||||
) : null}
|
||||
{open == "access" ? (
|
||||
<ExtensionAccessModal<SkillsOverviewPayload>
|
||||
|
||||
@@ -41,6 +41,7 @@ export default function SkillsSettingsSection() {
|
||||
>
|
||||
{adding ? (
|
||||
<SkillModal
|
||||
limits={overview.limits}
|
||||
onSaved={(next, msg) => {
|
||||
setAdding(false);
|
||||
changed(next, msg);
|
||||
@@ -54,6 +55,7 @@ export default function SkillsSettingsSection() {
|
||||
key={`${skill.id}-${version}`}
|
||||
skill={skill}
|
||||
targets={overview}
|
||||
limits={overview.limits}
|
||||
onChanged={changed}
|
||||
onError={failed}
|
||||
/>
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import grabSuperAdminUser from "@/src/functions/backend/backups/grab-super-admin-user";
|
||||
import grabSkillLimitsOverview, {
|
||||
type SkillLimitsOverview,
|
||||
} from "@/src/functions/backend/db/app-settings/grab-skill-limits";
|
||||
import saveAppSetting from "@/src/functions/backend/db/app-settings/save-app-setting";
|
||||
import logEvent, { grabRequestIP } from "@/src/functions/backend/logger";
|
||||
import { SkillBodyMaxCharsSettingKey, SkillBodyShownCharsSettingKey } from "@/src/dict/skill-dict";
|
||||
import { SaveSkillLimitsRequestSchema } from "@/src/schemas/extension-schema";
|
||||
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: the longest runbook a skill may save, and how much of
|
||||
* it `load_skill` hands back at once. Zero puts the code's default back for
|
||||
* that one.
|
||||
*/
|
||||
export const handler: BunextAPIRouteHandler<APIResponseObject<SkillLimitsOverview>> = 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 change the skill limits.`);
|
||||
|
||||
const { body_max_chars, body_shown_chars } = parseWithSchema({
|
||||
schema: SaveSkillLimitsRequestSchema,
|
||||
data: body,
|
||||
});
|
||||
|
||||
await saveAppSetting({
|
||||
key: SkillBodyMaxCharsSettingKey,
|
||||
value: body_max_chars > 0 ? String(body_max_chars) : null,
|
||||
});
|
||||
await saveAppSetting({
|
||||
key: SkillBodyShownCharsSettingKey,
|
||||
value: body_shown_chars > 0 ? String(body_shown_chars) : null,
|
||||
});
|
||||
|
||||
await logEvent({
|
||||
title: "Skill limits changed",
|
||||
category: "system",
|
||||
user_id: user.id,
|
||||
plain_text: `User #${user.id} set the skill runbook limit to ${body_max_chars || "default"} characters, ${body_shown_chars || "default"} read at once.`,
|
||||
metadata: { body_max_chars, body_shown_chars },
|
||||
ip_address: grabRequestIP(req),
|
||||
});
|
||||
|
||||
return { success: true, singleRes: await grabSkillLimitsOverview() };
|
||||
} catch (error: unknown) {
|
||||
return { success: false, msg: safeErrorMessage({ error }) };
|
||||
}
|
||||
};
|
||||
@@ -193,11 +193,24 @@ export const SaveSkillRequestSchema = z.object({
|
||||
.string()
|
||||
.trim()
|
||||
.min(1, "Write the runbook")
|
||||
.max(AppData.SkillBodyMaxChars, `Keep the runbook under ${AppData.SkillBodyMaxChars} characters`),
|
||||
/**
|
||||
* Only the ceiling: the install's own limit is a setting, checked by
|
||||
* `assertSkillBodyFits` when a runbook is saved or proposed.
|
||||
*/
|
||||
.max(AppData.SkillBodyLimitMax, `Keep the runbook under ${AppData.SkillBodyLimitMax} characters`),
|
||||
enabled: z.boolean().default(true),
|
||||
all_bots: z.boolean().default(false),
|
||||
});
|
||||
|
||||
/**
|
||||
* The install's skill limits (Settings > General). Zero puts the code's
|
||||
* default back for that one.
|
||||
*/
|
||||
export const SaveSkillLimitsRequestSchema = z.object({
|
||||
body_max_chars: z.coerce.number().int().min(0).max(AppData.SkillBodyLimitMax),
|
||||
body_shown_chars: z.coerce.number().int().min(0).max(AppData.SkillBodyLimitMax),
|
||||
});
|
||||
|
||||
export const SkillIdRequestSchema = z.object({
|
||||
skill_id: Id,
|
||||
});
|
||||
|
||||
@@ -803,8 +803,38 @@ export type McpServersOverviewPayload = ExtensionTargets & {
|
||||
servers: PublicMcpServer[];
|
||||
};
|
||||
|
||||
/**
|
||||
* The install's skill limits (Settings > General, `grab-skill-limits.ts`).
|
||||
*/
|
||||
export type SkillLimitsOverview = {
|
||||
/**
|
||||
* The longest runbook a skill may save right now.
|
||||
*/
|
||||
body_max_chars: number;
|
||||
/**
|
||||
* How much of a runbook `load_skill` hands back right now.
|
||||
*/
|
||||
body_shown_chars: number;
|
||||
/**
|
||||
* The longest runbook when the install saved no limit.
|
||||
*/
|
||||
body_max_default: number;
|
||||
/**
|
||||
* What `load_skill` hands back when the install saved no limit.
|
||||
*/
|
||||
body_shown_default: number;
|
||||
/**
|
||||
* The most the settings page accepts for either.
|
||||
*/
|
||||
max: number;
|
||||
};
|
||||
|
||||
export type SkillsOverviewPayload = ExtensionTargets & {
|
||||
skills: PublicSkill[];
|
||||
/**
|
||||
* The install's runbook limits, for the skill form's hints.
|
||||
*/
|
||||
limits: SkillLimitsOverview;
|
||||
};
|
||||
|
||||
export type CustomToolsOverviewPayload = ExtensionTargets & {
|
||||
@@ -1403,6 +1433,11 @@ export type PagePropsType = {
|
||||
reply_default: number;
|
||||
max: number;
|
||||
} | null;
|
||||
/**
|
||||
* The longest runbook a skill may save and how much of it a bot reads
|
||||
* at once, with the code's defaults (the General settings page).
|
||||
*/
|
||||
skill_limits?: SkillLimitsOverview | null;
|
||||
/**
|
||||
* The release the server is running, for the General settings page.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user