Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

3.1 KB
Raw Blame
// Draft skills generated by @three-ws/skill-import from an OpenAPI spec or a
// GitHub repo (table openapi_skill_imports). A row here is always a draft:
// `enabled` defaults to false and nothing in this module ever sets it true on
// create, so an imported skill is never live without someone flipping it on
// deliberately through PATCH. Bundle files are served at runtime by
// api/skills/import/[id]/[...file].js so src/skills/index.js's SkillRegistry
// can install them like any other hosted bundle.

import { sql } from './db.js';
import { isUuid } from './validate.js';

export class OpenApiSkillImportError extends Error {
	constructor(status, code, message) {
		super(message);
		this.status = status;
		this.code = code;
	}
}

function present(row) {
	return {
		id: row.id,
		sourceUrl: row.source_url,
		kind: row.kind,
		manifest: row.manifest,
		skillMd: row.skill_md,
		tools: row.tools,
		hasHandlers: Boolean(row.handlers_src),
		enabled: row.enabled,
		createdAt: row.created_at,
		bundleUri: `/api/skills/import/${row.id}/`,
	};
}

export async function createDraft(userId, bundle) {
	const rows = await sql`
		INSERT INTO openapi_skill_imports
			(user_id, source_url, kind, manifest, skill_md, tools, handlers_src, enabled)
		VALUES
			(${userId}, ${bundle.sourceUrl}, ${bundle.kind || (bundle.tools ? 'openapi' : 'declarative')},
			 ${JSON.stringify(bundle.manifest)}::jsonb, ${bundle.skillMd},
			 ${bundle.tools ? JSON.stringify(bundle.tools) : null}::jsonb, ${bundle.handlersSrc || null}, false)
		RETURNING *
	`;
	return present(rows[0]);
}

export async function listDrafts(userId) {
	const rows = await sql`
		SELECT * FROM openapi_skill_imports WHERE user_id = ${userId} ORDER BY created_at DESC
	`;
	return rows.map(present);
}

export async function getDraftRow(id, userId) {
	if (!isUuid(id)) throw new OpenApiSkillImportError(400, 'validation_error', 'valid id required');
	const rows = await sql`
		SELECT * FROM openapi_skill_imports WHERE id = ${id} AND user_id = ${userId}
	`;
	if (!rows.length) throw new OpenApiSkillImportError(404, 'not_found', 'import not found');
	return rows[0];
}

export async function getDraft(id, userId) {
	return present(await getDraftRow(id, userId));
}

// Public: served to whatever fetches the bundle (the skill runtime, which has
// no user session). Ownership is checked only on the authoring routes above.
export async function getPublicDraftRow(id) {
	if (!isUuid(id)) throw new OpenApiSkillImportError(400, 'validation_error', 'valid id required');
	const rows = await sql`SELECT * FROM openapi_skill_imports WHERE id = ${id}`;
	if (!rows.length) throw new OpenApiSkillImportError(404, 'not_found', 'import not found');
	return rows[0];
}

export async function setEnabled(id, userId, enabled) {
	await getDraftRow(id, userId);
	const rows = await sql`
		UPDATE openapi_skill_imports SET enabled = ${Boolean(enabled)}
		WHERE id = ${id} AND user_id = ${userId}
		RETURNING *
	`;
	return present(rows[0]);
}

export async function deleteDraft(id, userId) {
	await getDraftRow(id, userId);
	await sql`DELETE FROM openapi_skill_imports WHERE id = ${id} AND user_id = ${userId}`;
}