mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-26 15:34:26 +00:00
e9dd584792
* feat(api): support global API key from config.yaml (api.global_api_key) Accept a config-defined global API key anywhere a web-UI key is accepted (X-API-Key / Bearer), with no login session and no DB record. Useful for automated deployments and AI agents (HTTP API + MCP). Defaults to empty (disabled); does not require the lbk_ prefix. - templates/config.yaml: add api.global_api_key with security notes - service/apikey.py: verify_api_key checks global key first (constant-time) - docs/API_KEY_AUTH.md: document the global key + security guidance - tests: cover global-key match, prefix-free, fallback-to-db, disabled * feat(mcp): expose LangBot management as an MCP server at /mcp Add an MCP (Model Context Protocol) server so external AI agents can manage a LangBot instance. Reuses the same API-key auth as the HTTP API (including the config.yaml global API key). - pkg/api/mcp/server.py: FastMCP server wrapping the service layer; 21 curated tools across system/bots/pipelines/models/knowledge/mcp-servers/skills - pkg/api/mcp/mount.py: ASGI dispatcher fronting Quart; authenticates /mcp requests with an API key, runs the streamable-HTTP session manager lifespan - controller/main.py: serve the wrapped ASGI app via hypercorn (was run_task) - web: new 'MCP' tab in the API integration dialog showing endpoint, auth, and client config; i18n for 8 locales - tests/manual/mcp_smoke.py: e2e check (401 unauth, list tools, call tools) Tool surface is intentionally curated (not all ~25 route groups) to keep the agent surface small, safe, and maintainable. Extend deliberately. * feat(skills): add in-repo skills/ as the single source of truth Migrate the agent skills + QA/e2e test harness from the (now archived) langbot-app/langbot-skills repo into LangBot/skills/, and add four new skills. Migrated: - langbot-plugin-dev, langbot-testing (e2e), langbot-env-setup, langbot-skills-maintenance, langbot-eba-adapter-dev - the bin/lbs CLI (src/, test/, scripts/, schemas/, qa-agent-docs/) New: - langbot-dev core backend + web development - langbot-deploy Docker/K8s deployment + config.yaml + global API key - langbot-mcp-ops operating the LangBot MCP server (/mcp) - langbot-space-ops operating the Space marketplace MCP server - src/cli.ts repoRoot(): recognize the skills assets root (skills.index.json + bin/lbs) so the CLI works when nested inside the LangBot repo - README.md: unified skill catalog; skills.index.json regenerated Parity with source verified: bin/lbs validate + node test suite match the source repo (only the uncommitted .lbpkg build-artifact fixture differs). * docs(agents): document agent-facing surfaces + API/MCP/skills sync rule * docs(readme): add 'Built for AI Agents' section across all locales Highlight MCP server, in-repo skills (single source of truth), AGENTS.md sync rule, and llms.txt. Cross-link LangBot Space MCP marketplace. * style(mcp): fix ruff format + prettier lint in MCP server and API panel * style(web): prettier format MCP i18n locale entries * docs(skills): note MCP instance control in dev/testing skills All development-guidance skills now point to the LangBot instance MCP server (/mcp) and the Space marketplace MCP server, reusing API keys.
227 lines
6.8 KiB
TypeScript
227 lines
6.8 KiB
TypeScript
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import type { ParsedYaml, Skill, StructuredItem, StructuredItemKind } from "./types.ts";
|
|
import { fail } from "./cli.ts";
|
|
|
|
const frontmatterRe = /^---\n([\s\S]*?)\n---\n/;
|
|
|
|
export function statIsDirectory(path: string): boolean {
|
|
try {
|
|
return statSync(path).isDirectory();
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function skillsRoot(root: string): string {
|
|
const nested = join(root, "skills");
|
|
return existsSync(nested) && statIsDirectory(nested) ? nested : root;
|
|
}
|
|
|
|
export function envPath(root: string): string {
|
|
return join(skillsRoot(root), ".env");
|
|
}
|
|
|
|
export function envLocalPath(root: string): string {
|
|
return join(skillsRoot(root), ".env.local");
|
|
}
|
|
|
|
export function envExamplePath(root: string): string {
|
|
return join(skillsRoot(root), ".env.example");
|
|
}
|
|
|
|
export function loadEnv(root: string): Record<string, string> {
|
|
return {
|
|
...parseEnvFile(envPath(root)),
|
|
...parseEnvFile(envLocalPath(root)),
|
|
};
|
|
}
|
|
|
|
export function listDirectories(root: string): string[] {
|
|
return readdirSync(root)
|
|
.filter((name) => !name.startsWith("."))
|
|
.filter((name) => statIsDirectory(join(root, name)))
|
|
.sort();
|
|
}
|
|
|
|
export function parseFrontmatter(text: string): { meta: Record<string, string>; body: string } {
|
|
const match = text.match(frontmatterRe);
|
|
if (!match) return { meta: {}, body: text };
|
|
|
|
const meta: Record<string, string> = {};
|
|
for (const line of match[1].split("\n")) {
|
|
const sep = line.indexOf(":");
|
|
if (sep === -1) continue;
|
|
const key = line.slice(0, sep).trim();
|
|
const value = line.slice(sep + 1).trim().replace(/^["']|["']$/g, "");
|
|
meta[key] = value;
|
|
}
|
|
|
|
return { meta, body: text.slice(match[0].length) };
|
|
}
|
|
|
|
export function loadSkills(root: string): Skill[] {
|
|
const skills: Skill[] = [];
|
|
const base = skillsRoot(root);
|
|
for (const directory of listDirectories(base)) {
|
|
const skillPath = join(base, directory);
|
|
const skillMd = join(skillPath, "SKILL.md");
|
|
if (!existsSync(skillMd)) continue;
|
|
const text = readFileSync(skillMd, "utf8");
|
|
const { meta, body } = parseFrontmatter(text);
|
|
skills.push({
|
|
path: skillPath,
|
|
directory,
|
|
name: meta.name ?? "",
|
|
description: meta.description ?? "",
|
|
body,
|
|
});
|
|
}
|
|
return skills;
|
|
}
|
|
|
|
export function getSkill(root: string, skillName: string): Skill {
|
|
const skill = loadSkills(root).find((item) => item.directory === skillName || item.name === skillName);
|
|
if (!skill) fail(`unknown skill: ${skillName}`);
|
|
return skill;
|
|
}
|
|
|
|
export function parseEnvFile(path: string): Record<string, string> {
|
|
if (!existsSync(path)) return {};
|
|
const env: Record<string, string> = {};
|
|
for (const rawLine of readFileSync(path, "utf8").split(/\r?\n/)) {
|
|
const line = rawLine.trim();
|
|
if (!line || line.startsWith("#")) continue;
|
|
const sep = line.indexOf("=");
|
|
if (sep === -1) continue;
|
|
const key = line.slice(0, sep).trim();
|
|
const value = line.slice(sep + 1).trim().replace(/^["']|["']$/g, "");
|
|
env[key] = value;
|
|
}
|
|
return env;
|
|
}
|
|
|
|
export function globMarkdownRefs(skillPath: string): string[] {
|
|
const refsDir = join(skillPath, "references");
|
|
if (!existsSync(refsDir)) return [];
|
|
return readdirSync(refsDir)
|
|
.filter((name) => name.endsWith(".md"))
|
|
.sort()
|
|
.map((name) => join("references", name));
|
|
}
|
|
|
|
export function globYamlFiles(dir: string): string[] {
|
|
if (!existsSync(dir)) return [];
|
|
return readdirSync(dir)
|
|
.filter((name) => name.endsWith(".yaml") || name.endsWith(".yml"))
|
|
.sort()
|
|
.map((name) => join(dir, name));
|
|
}
|
|
|
|
function unquote(value: string): string {
|
|
return value.trim().replace(/^["']|["']$/g, "");
|
|
}
|
|
|
|
function parseScalarValue(value: string): string | boolean {
|
|
const trimmed = value.trim();
|
|
if (/^["'].*["']$/.test(trimmed)) return unquote(trimmed);
|
|
if (trimmed === "true") return true;
|
|
if (trimmed === "false") return false;
|
|
return trimmed;
|
|
}
|
|
|
|
export function parseYamlLite(text: string): ParsedYaml {
|
|
const fields: ParsedYaml = {};
|
|
let currentList: string | null = null;
|
|
|
|
for (const rawLine of text.split(/\r?\n/)) {
|
|
const line = rawLine.replace(/\s+$/, "");
|
|
if (!line.trim() || line.trim().startsWith("#")) continue;
|
|
|
|
const pair = line.match(/^([A-Za-z0-9_]+):\s*(.*)$/);
|
|
if (pair) {
|
|
const key = pair[1];
|
|
const value = pair[2];
|
|
if (value === "") {
|
|
fields[key] = [];
|
|
currentList = key;
|
|
} else {
|
|
fields[key] = parseScalarValue(value);
|
|
currentList = null;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
const item = line.match(/^\s*-\s*(.*)$/);
|
|
if (item && currentList) {
|
|
const existing = fields[currentList];
|
|
if (Array.isArray(existing)) existing.push(unquote(item[1]));
|
|
}
|
|
}
|
|
|
|
return fields;
|
|
}
|
|
|
|
export function scalar(fields: ParsedYaml, key: string): string {
|
|
const value = fields[key];
|
|
return typeof value === "string" ? value : "";
|
|
}
|
|
|
|
export function boolValue(fields: ParsedYaml, key: string): boolean | undefined {
|
|
const value = fields[key];
|
|
return typeof value === "boolean" ? value : undefined;
|
|
}
|
|
|
|
export function listValue(fields: ParsedYaml, key: string): string[] {
|
|
const value = fields[key];
|
|
return Array.isArray(value) ? value : [];
|
|
}
|
|
|
|
export function yamlQuote(value: string): string {
|
|
return JSON.stringify(value);
|
|
}
|
|
|
|
export function yamlList(values: string[]): string {
|
|
return values.map((value) => ` - ${yamlQuote(value)}`).join("\n");
|
|
}
|
|
|
|
export function loadStructuredItems(root: string, kind: StructuredItemKind, skillFilter?: string): StructuredItem[] {
|
|
const skills = skillFilter ? [getSkill(root, skillFilter)] : loadSkills(root);
|
|
const items: StructuredItem[] = [];
|
|
for (const skill of skills) {
|
|
for (const path of globYamlFiles(join(skill.path, kind))) {
|
|
const raw = readFileSync(path, "utf8");
|
|
items.push({ path, skill: skill.directory, fields: parseYamlLite(raw), raw });
|
|
}
|
|
}
|
|
return items.sort((a, b) => `${a.skill}:${scalar(a.fields, "id")}`.localeCompare(`${b.skill}:${scalar(b.fields, "id")}`));
|
|
}
|
|
|
|
export function findStructuredItem(
|
|
root: string,
|
|
kind: StructuredItemKind,
|
|
skillOrId: string,
|
|
maybeId?: string,
|
|
): StructuredItem {
|
|
const skillFilter = maybeId ? skillOrId : undefined;
|
|
const id = maybeId ?? skillOrId;
|
|
const matches = loadStructuredItems(root, kind, skillFilter).filter((item) => scalar(item.fields, "id") === id);
|
|
if (matches.length === 0) fail(`unknown ${kind.slice(0, -1)}: ${id}`);
|
|
if (matches.length > 1) {
|
|
fail(`ambiguous ${kind.slice(0, -1)} '${id}', specify skill: ${matches.map((item) => item.skill).join(", ")}`);
|
|
}
|
|
return matches[0];
|
|
}
|
|
|
|
export function slugify(input: string): string {
|
|
return input
|
|
.trim()
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9\u4e00-\u9fa5]+/g, "-")
|
|
.replace(/^-+|-+$/g, "");
|
|
}
|
|
|
|
export function todayIso(): string {
|
|
return new Date().toISOString().slice(0, 10);
|
|
}
|