244 lines
8.9 KiB
TypeScript
244 lines
8.9 KiB
TypeScript
import { z } from "zod";
|
||
import { resolveLangfuseConfig, type LangfuseConfig } from "./config.js";
|
||
import { LangfuseService } from "./service.js";
|
||
|
||
/**
|
||
* Registry of read-only Langfuse capabilities.
|
||
*
|
||
* Separate from `sealCapabilities` on purpose: these handlers need no Seal
|
||
* tenant, no `CorpConfig` and no Seal HTTP client — they talk to Langfuse with
|
||
* the compile-time injected key pair. They share the same shape (name /
|
||
* description / Zod `parameters` / `handler`) so the CLI dispatch helper can
|
||
* treat them uniformly.
|
||
*
|
||
* Naming keeps the `sealLangfuse*` prefix because these are internal registry
|
||
* identifiers for dispatch and allow-lists; the MCP surface only ever exposes
|
||
* the `seal_use` / `seal_use_write` gateway pair (see README "One registry,
|
||
* two doors").
|
||
*
|
||
* Every entry is read-only by construction: the underlying `LangfuseClient`
|
||
* issues `GET` requests only. `LANGFUSE_READ_ONLY_CAPABILITIES` lists the names
|
||
* explicitly so a newly added capability fails `capabilities.test.ts` until it
|
||
* is classified here.
|
||
*/
|
||
export const langfuseCapabilities = [
|
||
{
|
||
name: "sealLangfuseConnectionCheck",
|
||
description: "检查内置 Langfuse 只读凭据的连通性,返回项目信息",
|
||
parameters: z.object({}),
|
||
handler: async (_params: Record<string, never>, service: LangfuseService) =>
|
||
service.connectionCheck()
|
||
},
|
||
{
|
||
name: "sealLangfuseTraceCacheRefresh",
|
||
description: "按 traceId 拉取完整 Langfuse trace 并写入本地缓存,供离线 outline/search 使用",
|
||
parameters: z.object({
|
||
traceId: z.string().min(1)
|
||
}),
|
||
handler: async (params: { traceId: string }, service: LangfuseService) =>
|
||
service.refreshTraceCache(params)
|
||
},
|
||
{
|
||
name: "sealLangfuseTraceUrlGet",
|
||
description: "构建单条 trace 的 Langfuse 后台直达链接(优先使用缓存中的 projectId)",
|
||
parameters: z.object({
|
||
traceId: z.string().min(1)
|
||
}),
|
||
handler: async (params: { traceId: string }, service: LangfuseService) =>
|
||
service.getTraceUrl(params)
|
||
},
|
||
{
|
||
name: "sealLangfuseTraceOutlineGet",
|
||
description: "读取缓存 trace 的紧凑大纲:观测树、体量、用量、成本与可选 IO 预览",
|
||
parameters: z.object({
|
||
traceId: z.string().min(1),
|
||
includeIoPreview: z.boolean().optional(),
|
||
includeInput: z.boolean().optional(),
|
||
includeOutput: z.boolean().optional(),
|
||
full: z.boolean().optional(),
|
||
maxPreviewChars: z.number().int().min(20).max(5_000).optional(),
|
||
refreshIfMissing: z.boolean().optional()
|
||
}),
|
||
handler: async (
|
||
params: {
|
||
traceId: string;
|
||
includeIoPreview?: boolean;
|
||
includeInput?: boolean;
|
||
includeOutput?: boolean;
|
||
full?: boolean;
|
||
maxPreviewChars?: number;
|
||
refreshIfMissing?: boolean;
|
||
},
|
||
service: LangfuseService
|
||
) => service.getTraceOutline(params)
|
||
},
|
||
{
|
||
name: "sealLangfuseTraceQrepSearch",
|
||
description: "在单条缓存 trace 内做层级感知的关键词检索,返回带上下文的排序片段",
|
||
parameters: z.object({
|
||
traceId: z.string().min(1),
|
||
query: z.union([z.string().min(1), z.array(z.string().min(1)).min(1)]),
|
||
sessionId: z.string().optional(),
|
||
fields: z
|
||
.array(z.enum(["name", "input", "output", "metadata", "userId", "sessionId"]))
|
||
.optional(),
|
||
observationTypes: z.array(z.string().min(1)).optional(),
|
||
metadata: z.record(z.string(), z.union([z.string(), z.number(), z.boolean()])).optional(),
|
||
maxResults: z.number().int().min(1).max(30).optional(),
|
||
contextChars: z.number().int().min(20).max(500).optional(),
|
||
refreshIfMissing: z.boolean().optional()
|
||
}),
|
||
handler: async (
|
||
params: {
|
||
traceId: string;
|
||
query: string | string[];
|
||
sessionId?: string;
|
||
fields?: Array<"name" | "input" | "output" | "metadata" | "userId" | "sessionId">;
|
||
observationTypes?: string[];
|
||
metadata?: Record<string, string | number | boolean>;
|
||
maxResults?: number;
|
||
contextChars?: number;
|
||
refreshIfMissing?: boolean;
|
||
},
|
||
service: LangfuseService
|
||
) => service.searchTrace(params)
|
||
},
|
||
{
|
||
name: "sealLangfuseObservationIoGet",
|
||
description: "读取指定 observation 的聚焦 input/output/metadata,支持 jsonPath 定点取值",
|
||
parameters: z.object({
|
||
traceId: z.string().min(1),
|
||
observationId: z.string().min(1),
|
||
fields: z.array(z.enum(["input", "output", "metadata"])).optional(),
|
||
maxChars: z.number().int().min(100).max(500_000).optional(),
|
||
full: z.boolean().optional(),
|
||
jsonPath: z.string().optional(),
|
||
path: z.string().optional(),
|
||
refreshIfMissing: z.boolean().optional()
|
||
}),
|
||
handler: async (
|
||
params: {
|
||
traceId: string;
|
||
observationId: string;
|
||
fields?: Array<"input" | "output" | "metadata">;
|
||
maxChars?: number;
|
||
full?: boolean;
|
||
jsonPath?: string;
|
||
path?: string;
|
||
refreshIfMissing?: boolean;
|
||
},
|
||
service: LangfuseService
|
||
) => service.getObservationIo(params)
|
||
},
|
||
{
|
||
name: "sealLangfuseSessionOutlineGet",
|
||
description: "获取 Langfuse session 的紧凑 trace 摘要列表(不缓存整 session)",
|
||
parameters: z.object({
|
||
sessionId: z.string().min(1),
|
||
traceLimit: z.number().int().min(1).max(200).optional()
|
||
}),
|
||
handler: async (params: { sessionId: string; traceLimit?: number }, service: LangfuseService) =>
|
||
service.getSessionOutline(params)
|
||
},
|
||
{
|
||
name: "sealLangfuseObservationsFind",
|
||
description: "按名称/类型/metadata 键/是否含 IO 过滤缓存 trace 内的 observation,只返回摘要",
|
||
parameters: z.object({
|
||
traceId: z.string().min(1),
|
||
nameContains: z.string().optional(),
|
||
type: z.string().optional(),
|
||
metadataKey: z.string().optional(),
|
||
hasInput: z.boolean().optional(),
|
||
hasOutput: z.boolean().optional(),
|
||
maxResults: z.number().int().min(1).max(200).optional(),
|
||
refreshIfMissing: z.boolean().optional()
|
||
}),
|
||
handler: async (
|
||
params: {
|
||
traceId: string;
|
||
nameContains?: string;
|
||
type?: string;
|
||
metadataKey?: string;
|
||
hasInput?: boolean;
|
||
hasOutput?: boolean;
|
||
maxResults?: number;
|
||
refreshIfMissing?: boolean;
|
||
},
|
||
service: LangfuseService
|
||
) => service.findObservations(params)
|
||
},
|
||
{
|
||
name: "sealLangfuseAttachmentFlowGet",
|
||
description: "在缓存 trace 中追踪附件文件 id/名称/提示词经解析与审核阶段的文本证据",
|
||
parameters: z
|
||
.object({
|
||
traceId: z.string().min(1),
|
||
fileId: z.string().optional(),
|
||
fileName: z.string().optional(),
|
||
contentHints: z.array(z.string().min(1)).optional(),
|
||
contextChars: z.number().int().min(20).max(500).optional(),
|
||
refreshIfMissing: z.boolean().optional()
|
||
})
|
||
.refine(
|
||
(value) => Boolean(value.fileId || value.fileName || value.contentHints?.length),
|
||
{ message: "Provide at least one of fileId, fileName, or contentHints" }
|
||
),
|
||
handler: async (
|
||
params: {
|
||
traceId: string;
|
||
fileId?: string;
|
||
fileName?: string;
|
||
contentHints?: string[];
|
||
contextChars?: number;
|
||
refreshIfMissing?: boolean;
|
||
},
|
||
service: LangfuseService
|
||
) => service.getAttachmentFlow(params)
|
||
},
|
||
{
|
||
name: "sealLangfuseTraceRuleFlowGet",
|
||
description: "在缓存 trace 中追踪一条规则经规则匹配、文档解析、最终审核阶段的文本证据",
|
||
parameters: z.object({
|
||
traceId: z.string().min(1),
|
||
rule: z.string().min(1),
|
||
ruleHints: z.array(z.string().min(1)).optional(),
|
||
contextChars: z.number().int().min(20).max(500).optional(),
|
||
refreshIfMissing: z.boolean().optional()
|
||
}),
|
||
handler: async (
|
||
params: {
|
||
traceId: string;
|
||
rule: string;
|
||
ruleHints?: string[];
|
||
contextChars?: number;
|
||
refreshIfMissing?: boolean;
|
||
},
|
||
service: LangfuseService
|
||
) => service.getTraceRuleFlow(params)
|
||
}
|
||
];
|
||
|
||
export type LangfuseCapability = (typeof langfuseCapabilities)[number];
|
||
|
||
export const LANGFUSE_READ_ONLY_CAPABILITIES: ReadonlySet<string> = new Set([
|
||
"sealLangfuseConnectionCheck",
|
||
"sealLangfuseTraceCacheRefresh",
|
||
"sealLangfuseTraceUrlGet",
|
||
"sealLangfuseTraceOutlineGet",
|
||
"sealLangfuseTraceQrepSearch",
|
||
"sealLangfuseObservationIoGet",
|
||
"sealLangfuseSessionOutlineGet",
|
||
"sealLangfuseObservationsFind",
|
||
"sealLangfuseAttachmentFlowGet",
|
||
"sealLangfuseTraceRuleFlowGet"
|
||
]);
|
||
|
||
export function findLangfuseCapability(name: string): LangfuseCapability | undefined {
|
||
return langfuseCapabilities.find((capability) => capability.name === name);
|
||
}
|
||
|
||
/** Built lazily per invocation so tests can swap env vars before first use. */
|
||
export function createLangfuseService(config?: LangfuseConfig): LangfuseService {
|
||
return new LangfuseService(config ?? resolveLangfuseConfig());
|
||
}
|