@omni-api/core
v0.0.2
Published
OmniAPI core: Procedure / Registry / Middleware / Context / Errors
Readme
@omni/core
OmniAPI 的核心包:定义 Procedure / 组合 Middleware / 注册到 Registry / 通过 runProcedure 执行。
本包不绑定任何具体协议(HTTP / MCP),所有出口由独立的 Adapter 包负责。
安装
pnpm add @omni/core zod5 分钟速览
import { z } from 'zod';
import {
defineProcedure,
createRouter,
Registry,
runByName,
createContext,
} from '@omni/core';
// 1. 定义一个 Procedure
const createOrder = defineProcedure({
name: 'order.create',
description: '创建一笔订单',
input: z.object({ sku: z.string(), qty: z.number().int().positive() }),
output: z.object({ orderId: z.string() }),
meta: {
http: { method: 'POST', path: '/orders' },
mcp: { category: 'commerce', requireConfirmation: true },
},
handler: async ({ input }) => ({ orderId: `O_${input.sku}` }),
});
// 2. 注册到 Registry
const registry = new Registry();
registry.registerRouter(
createRouter({
namespace: 'order',
procedures: [createOrder],
}),
);
// 3. 调用(任何 Adapter 内部都是这么调的)
const result = await runByName(
registry,
'order.create',
{ sku: 'X1', qty: 1 },
createContext({ source: 'test' }),
);
console.log(result); // { orderId: 'O_X1' }导出 LLM 工具 JSON(逃生出口)
给不支持 MCP 的老 Agent 平台用:
import { exportToolsJSON } from '@omni/core/utils';
const openaiTools = exportToolsJSON(registry, { format: 'openai' });
const claudeTools = exportToolsJSON(registry, { format: 'anthropic' });API
核心
defineProcedure(def)- 定义一个 ProcedurecreateRouter(def)- 把多个 Procedure 组合 + 共享中间件Registry- 全局注册中心runProcedure(proc, input, ctx, options?)- 直接执行runByName(registry, name, input, ctx, options?)- 通过 Registry 执行
工具
compose(middlewares)- 组合中间件链createContext(partial)- 创建 Context(测试 / Adapter 用)getNamespace(p)- 取 procedure 的 namespace
错误
OmniError及子类:ValidationError/UnauthorizedError/ForbiddenError/NotFoundError/ConflictError/RateLimitError/InternalErrortoOmniError(err)- 把任意错误规范化为 OmniError
@omni/core/utils
zodToJsonSchema(schema)- Zod → JSON SchemaexportToolsJSON(registry, opts)- 导出 LLM 工具数组(OpenAI / Anthropic / Generic)
设计文档
详见根目录 docs/。
