Merge pull request #284 from yunyingk/mac15/cli-langfuse-diagnose-ergonomics
Refactor: 优化 Langfuse 检索与审批单诊断的 CLI 人体工程学体验
This commit is contained in:
5 files changed
+49
-19
No files matched your search
@@ -187,7 +187,7 @@ export async function handleApprovalRunsCommand(
|
||||
}
|
||||
|
||||
if (action === "get") {
|
||||
const recordId = args.command[2];
|
||||
const recordId = args.command[2] ?? stringOption(args.options["record-id"]) ?? stringOption(args.options.id);
|
||||
if (!recordId) throw new Error("Missing approval run recordId");
|
||||
const rawJson = booleanOption(args.options["raw-json"]) ?? booleanOption(args.options.rawJson);
|
||||
if (rawJson) {
|
||||
@@ -226,21 +226,21 @@ export async function handleApprovalRunsCommand(
|
||||
}
|
||||
|
||||
if (action === "cited-rules") {
|
||||
const recordId = args.command[2];
|
||||
const recordId = args.command[2] ?? stringOption(args.options["record-id"]) ?? stringOption(args.options.id);
|
||||
if (!recordId) throw usageError("Usage: seal-use approval-runs cited-rules <recordId>");
|
||||
await runCapability("sealApprovalRunCitedRulesGet", client, corp, { recordId }, outputOptions(args.options));
|
||||
return;
|
||||
}
|
||||
|
||||
if (action === "document-summary") {
|
||||
const recordId = args.command[2];
|
||||
const recordId = args.command[2] ?? stringOption(args.options["record-id"]) ?? stringOption(args.options.id);
|
||||
if (!recordId) throw usageError("Usage: seal-use approval-runs document-summary <recordId>");
|
||||
await runCapability("sealApprovalRunDocumentSummaryGet", client, corp, { recordId }, outputOptions(args.options));
|
||||
return;
|
||||
}
|
||||
|
||||
if (action === "diagnose") {
|
||||
const recordId = args.command[2];
|
||||
const recordId = args.command[2] ?? stringOption(args.options["record-id"]) ?? stringOption(args.options.id);
|
||||
const sn = stringOption(args.options.sn) ?? stringOption(args.options["document-sn"]);
|
||||
if (!recordId && !sn) {
|
||||
throw usageError("Usage: seal-use approval-runs diagnose <recordId>|--sn <documentSN> [--full] [--corp <corpId>]");
|
||||
|
||||
@@ -20,7 +20,12 @@ export async function handleLangfuseCommand(
|
||||
const options = args.options;
|
||||
const out = outputOptions(options);
|
||||
const traceId = () => maybeName ?? stringOption(options["trace-id"]);
|
||||
const refreshIfMissing = () => booleanOption(options["refresh-if-missing"]);
|
||||
const refreshIfMissing = () => {
|
||||
if (booleanOption(options["no-refresh"]) || booleanOption(options.offline)) return false;
|
||||
const explicit = booleanOption(options["refresh-if-missing"]);
|
||||
if (explicit !== undefined) return explicit;
|
||||
return true;
|
||||
};
|
||||
|
||||
if (action === "check") {
|
||||
await runLangfuseCapability("sealLangfuseConnectionCheck", {}, out);
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { existsSync, readFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
import { CliError } from "../../core/errors.js";
|
||||
|
||||
/**
|
||||
@@ -9,8 +11,8 @@ import { CliError } from "../../core/errors.js";
|
||||
* embeds them into release binaries. End users therefore need zero setup.
|
||||
*
|
||||
* Precedence: injected build vars → plain `LANGFUSE_*` shell vars (development
|
||||
* convenience when running from source) → nothing. Reading user config files is
|
||||
* deliberately absent: this domain must work without any local configuration.
|
||||
* convenience when running from source) → gitignored local `.env.langfuse` → nothing.
|
||||
* Reading user config files is deliberately absent: this domain must work without any local configuration.
|
||||
*/
|
||||
export type LangfuseConfig = {
|
||||
baseUrl: string;
|
||||
@@ -31,6 +33,29 @@ function normalizeBaseUrl(baseUrl: string): string {
|
||||
return baseUrl.replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
function tryReadEnvLangfuse(): Record<string, string> {
|
||||
if (process.env.NODE_ENV === "test") return {};
|
||||
try {
|
||||
const envPath = resolve(process.cwd(), ".env.langfuse");
|
||||
if (!existsSync(envPath)) return {};
|
||||
const content = readFileSync(envPath, "utf8");
|
||||
const result: Record<string, string> = {};
|
||||
for (const line of content.split("\n")) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || trimmed.startsWith("#")) continue;
|
||||
const idx = trimmed.indexOf("=");
|
||||
if (idx !== -1) {
|
||||
const key = trimmed.slice(0, idx).trim();
|
||||
const val = trimmed.slice(idx + 1).trim().replace(/^["']|["']$/g, "");
|
||||
result[key] = val;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
export function hasLangfuseCredentials(): boolean {
|
||||
const { baseUrl, publicKey, secretKey } = readLangfuseCredentials();
|
||||
return Boolean(baseUrl && publicKey && secretKey);
|
||||
@@ -43,18 +68,12 @@ function readLangfuseCredentials(): {
|
||||
secretKey?: string;
|
||||
projectId?: string;
|
||||
} {
|
||||
// Literal `process.env.X` accesses on purpose: `bun build --env=SEAL_BUILD_*`
|
||||
// inlines static property reads at compile time, while a computed key
|
||||
// (`process.env["SEAL_BUILD_" + name]`) stays a runtime lookup and comes back
|
||||
// empty from a release binary. Reads happen per call so tests and dev shells
|
||||
// can change the environment between invocations. The `LANGFUSE_*` fallbacks
|
||||
// are deliberately outside the SEAL_BUILD_ prefix: they are never inlined and
|
||||
// remain live shell-env vars for development runs from source.
|
||||
const envFile = tryReadEnvLangfuse();
|
||||
return {
|
||||
baseUrl: pick(process.env.SEAL_BUILD_LANGFUSE_BASE_URL, process.env.LANGFUSE_BASE_URL),
|
||||
publicKey: pick(process.env.SEAL_BUILD_LANGFUSE_PUBLIC_KEY, process.env.LANGFUSE_PUBLIC_KEY),
|
||||
secretKey: pick(process.env.SEAL_BUILD_LANGFUSE_SECRET_KEY, process.env.LANGFUSE_SECRET_KEY),
|
||||
projectId: pick(process.env.SEAL_BUILD_LANGFUSE_PROJECT_ID, process.env.LANGFUSE_PROJECT_ID)
|
||||
baseUrl: pick(process.env.SEAL_BUILD_LANGFUSE_BASE_URL, process.env.LANGFUSE_BASE_URL, envFile.LANGFUSE_BASE_URL),
|
||||
publicKey: pick(process.env.SEAL_BUILD_LANGFUSE_PUBLIC_KEY, process.env.LANGFUSE_PUBLIC_KEY, envFile.LANGFUSE_PUBLIC_KEY),
|
||||
secretKey: pick(process.env.SEAL_BUILD_LANGFUSE_SECRET_KEY, process.env.LANGFUSE_SECRET_KEY, envFile.LANGFUSE_SECRET_KEY),
|
||||
projectId: pick(process.env.SEAL_BUILD_LANGFUSE_PROJECT_ID, process.env.LANGFUSE_PROJECT_ID, envFile.LANGFUSE_PROJECT_ID)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -787,6 +787,12 @@ export const approvalRunsCapabilities = [
|
||||
hoseDocument: `seal-use approval-runs url ${detail.id} --type hose-document --corp ${context.corp.id}`,
|
||||
diagnoseConflict: invoiceConflicts[0]?.nextCommand,
|
||||
traceInvoice: invoiceConflicts[0] ? `seal-use invoices trace ${invoiceConflicts[0].invoiceNumber} --corp ${context.corp.id}` : undefined,
|
||||
traceSearch: detail.sourceExtendData?._langfuseTraceId
|
||||
? `seal-use langfuse search ${detail.sourceExtendData._langfuseTraceId} --query <关键词>`
|
||||
: undefined,
|
||||
traceOutline: detail.sourceExtendData?._langfuseTraceId
|
||||
? `seal-use langfuse outline ${detail.sourceExtendData._langfuseTraceId} --include-io-preview`
|
||||
: undefined,
|
||||
replay: `seal-use approval-runs replay ${detail.id} --dry-run --corp ${context.corp.id}`
|
||||
}
|
||||
};
|
||||
|
||||
@@ -598,7 +598,7 @@ export function summarizeApprovalRunDetail(record: ApprovalRun) {
|
||||
export function summarizeApprovalRunResult(record: ApprovalRun) {
|
||||
const result = asRecord(record.result);
|
||||
return {
|
||||
...summarizeExtraFindings(record),
|
||||
...summarizeExtraFindings(record, true),
|
||||
recordId: record.id,
|
||||
sourceDocumentSN: record.sourceDocumentSN,
|
||||
sourceDocumentId: record.sourceDocumentId,
|
||||
|
||||
Reference in new issue
Block a user