170 lines
5.3 KiB
TypeScript
170 lines
5.3 KiB
TypeScript
import { validationError, usageError } from "../../core/errors.js";
|
|
import { booleanOption, numberOption, splitOption, stringOption } from "../parser.js";
|
|
import { runLangfuseCapability } from "../helpers.js";
|
|
import { outputOptions } from "../printer.js";
|
|
import type { ParsedArgs } from "../types.js";
|
|
|
|
/**
|
|
* `seal-use langfuse <verb>` — read-only Langfuse trace inspection.
|
|
*
|
|
* The trace/session id is a positional argument (flag fallback accepted), all
|
|
* tuning knobs are flags. JSON goes to stdout via `runLangfuseCapability`
|
|
* (size-capped: `--full` / `--output-file` escape hatches); unknown verbs throw
|
|
* so the runner assigns exit 2.
|
|
*/
|
|
export async function handleLangfuseCommand(
|
|
action: string | undefined,
|
|
maybeName: string | undefined,
|
|
args: ParsedArgs
|
|
) {
|
|
const options = args.options;
|
|
const out = outputOptions(options);
|
|
const traceId = () => maybeName ?? stringOption(options["trace-id"]);
|
|
const refreshIfMissing = () => booleanOption(options["refresh-if-missing"]);
|
|
|
|
if (action === "check") {
|
|
await runLangfuseCapability("sealLangfuseConnectionCheck", {}, out);
|
|
return;
|
|
}
|
|
|
|
if (action === "cache-refresh") {
|
|
await runLangfuseCapability("sealLangfuseTraceCacheRefresh", { traceId: traceId() }, out);
|
|
return;
|
|
}
|
|
|
|
if (action === "url") {
|
|
await runLangfuseCapability("sealLangfuseTraceUrlGet", { traceId: traceId() }, out);
|
|
return;
|
|
}
|
|
|
|
if (action === "outline") {
|
|
await runLangfuseCapability(
|
|
"sealLangfuseTraceOutlineGet",
|
|
{
|
|
traceId: traceId(),
|
|
includeIoPreview: booleanOption(options["include-io-preview"]),
|
|
full: booleanOption(options.full),
|
|
maxPreviewChars: numberOption(options["max-preview-chars"]),
|
|
refreshIfMissing: refreshIfMissing()
|
|
},
|
|
out
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "search") {
|
|
await runLangfuseCapability(
|
|
"sealLangfuseTraceQrepSearch",
|
|
{
|
|
traceId: traceId(),
|
|
query: splitOption(options.query),
|
|
sessionId: stringOption(options["session-id"]),
|
|
fields: splitOption(options.fields),
|
|
observationTypes: splitOption(options["observation-types"]),
|
|
metadata: parseMetadataOption(stringOption(options.metadata)),
|
|
maxResults: numberOption(options["max-results"]),
|
|
contextChars: numberOption(options["context-chars"]),
|
|
refreshIfMissing: refreshIfMissing()
|
|
},
|
|
out
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "observation") {
|
|
const observationId = args.command[3] ?? stringOption(options["observation-id"]);
|
|
await runLangfuseCapability(
|
|
"sealLangfuseObservationIoGet",
|
|
{
|
|
traceId: traceId(),
|
|
observationId,
|
|
fields: splitOption(options.fields),
|
|
maxChars: numberOption(options["max-chars"]),
|
|
full: booleanOption(options.full),
|
|
jsonPath: stringOption(options["json-path"]) ?? stringOption(options.path),
|
|
refreshIfMissing: refreshIfMissing()
|
|
},
|
|
out
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "session") {
|
|
const sessionId = maybeName ?? stringOption(options["session-id"]);
|
|
await runLangfuseCapability(
|
|
"sealLangfuseSessionOutlineGet",
|
|
{
|
|
sessionId,
|
|
traceLimit: numberOption(options["trace-limit"])
|
|
},
|
|
out
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "observations") {
|
|
await runLangfuseCapability(
|
|
"sealLangfuseObservationsFind",
|
|
{
|
|
traceId: traceId(),
|
|
nameContains: stringOption(options["name-contains"]),
|
|
type: stringOption(options.type),
|
|
metadataKey: stringOption(options["metadata-key"]),
|
|
hasInput: booleanOption(options["has-input"]),
|
|
hasOutput: booleanOption(options["has-output"]),
|
|
maxResults: numberOption(options["max-results"]),
|
|
refreshIfMissing: refreshIfMissing()
|
|
},
|
|
out
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "attachment-flow") {
|
|
await runLangfuseCapability(
|
|
"sealLangfuseAttachmentFlowGet",
|
|
{
|
|
traceId: traceId(),
|
|
fileId: stringOption(options["file-id"]),
|
|
fileName: stringOption(options["file-name"]),
|
|
contentHints: splitOption(options["content-hints"]),
|
|
contextChars: numberOption(options["context-chars"]),
|
|
refreshIfMissing: refreshIfMissing()
|
|
},
|
|
out
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "rule-flow") {
|
|
await runLangfuseCapability(
|
|
"sealLangfuseTraceRuleFlowGet",
|
|
{
|
|
traceId: traceId(),
|
|
rule: stringOption(options.rule),
|
|
ruleHints: splitOption(options["rule-hints"]),
|
|
contextChars: numberOption(options["context-chars"]),
|
|
refreshIfMissing: refreshIfMissing()
|
|
},
|
|
out
|
|
);
|
|
return;
|
|
}
|
|
|
|
throw usageError(`Unknown langfuse command: ${action ?? ""}. Run 'seal-use help' for available commands.`);
|
|
}
|
|
|
|
function parseMetadataOption(raw: string | undefined): Record<string, string | number | boolean> | undefined {
|
|
if (raw === undefined) return undefined;
|
|
let parsed: unknown;
|
|
try {
|
|
parsed = JSON.parse(raw);
|
|
} catch {
|
|
throw validationError(`无效的 --metadata:需要 JSON 对象,例如 '{"key":"value"}'`);
|
|
}
|
|
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
throw validationError("无效的 --metadata:需要 JSON 对象,例如 '{\"key\":\"value\"}'");
|
|
}
|
|
return parsed as Record<string, string | number | boolean>;
|
|
}
|