Two new settings pages, MCPs and Tools. Super admins define them; super
admins and admins attach them to bots, to workspaces (every bot there),
or to every bot. Bots get them in chat replies and tasks.
- Custom tools: a JSON-schema of arguments plus an HTTP request, a script
on the bot's computer, or a prompt, templated with {{args.x}} and
{{env.KEY}}, secrets redacted from results.
- MCP servers: remote (URL and headers) or stdio commands run on the host
with a setup command and an isolated env, in a per-process pool. Tools
are listed into a cache and connect on first call.
- Add, edit, access and try open in modals.
- Markdown, JSON and command fields use twui's AceEditor (CodeEditor),
its scripts loaded on demand.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
54 lines
1.8 KiB
TypeScript
54 lines
1.8 KiB
TypeScript
type Params = {
|
|
template: string;
|
|
/**
|
|
* `{{args.name}}`, dotted paths reach into objects. Missing ones fill
|
|
* in empty; objects and arrays as JSON.
|
|
*/
|
|
args?: Record<string, unknown>;
|
|
/**
|
|
* `{{env.KEY}}`. A key that isn't set throws, naming it.
|
|
*/
|
|
envs?: { key: string; value: string }[];
|
|
/**
|
|
* Applied to every filled-in argument, e.g. `encodeURIComponent` in a
|
|
* URL. Envs go in as they are (one may hold a whole base URL).
|
|
*/
|
|
encode?: (value: string) => string;
|
|
};
|
|
|
|
const Pattern = /\{\{\s*(args|env)\.([A-Za-z0-9_.-]+)\s*\}\}/g;
|
|
|
|
/**
|
|
* Fills a custom tool's or MCP server's template: `{{args.x}}` from the
|
|
* model's arguments, `{{env.KEY}}` from the envs. Anything else is left as
|
|
* written.
|
|
*/
|
|
export default function fillTemplate({ template, args = {}, envs = [], encode = (v) => v }: Params) {
|
|
return template.replace(Pattern, (_match, source: string, path: string) => {
|
|
if (source == "env") {
|
|
const env = envs.find((e) => e.key == path);
|
|
|
|
if (!env) throw new Error(`The env ${path} isn't set. Add it in Settings, under Environment.`);
|
|
|
|
return env.value;
|
|
}
|
|
|
|
return encode(stringify({ value: pick({ value: args, path }) }));
|
|
});
|
|
}
|
|
|
|
function pick({ value, path }: { value: unknown; path: string }) {
|
|
return path.split(".").reduce<unknown>((current, key) => {
|
|
if (current && typeof current == "object") return (current as Record<string, unknown>)[key];
|
|
return undefined;
|
|
}, value);
|
|
}
|
|
|
|
function stringify({ value }: { value: unknown }) {
|
|
if (value === undefined || value === null) return "";
|
|
if (typeof value == "string") return value;
|
|
if (typeof value == "number" || typeof value == "boolean") return String(value);
|
|
|
|
return JSON.stringify(value);
|
|
}
|