feat: 添加对 langfuse 监控的支持 (#242)

* docs: 更新类型检查的 CLAUDE.md

* feat: 添加模型 1M 上下文切换

* chore: remove prefetchOfficialMcpUrls call on startup

* docs: 添加 git commit 规范

* feat: 第一次接入 langfuse

* fix: 修复 generation 的计时的错误

* feat: 添加多 agent 的监控

* feat: 添加 /poor 省流模式,toggle 关闭 extract_memories 和 prompt_suggestion

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* chore: 修复 lock 文件

* chore: 更新类型依赖

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
claude-code-best
2026-04-11 22:07:38 +08:00
committed by GitHub
parent 6a9da9d546
commit 2fea429dc6
23 changed files with 1242 additions and 6 deletions

View File

@@ -107,9 +107,12 @@ import {
getCurrentTurnTokenBudget,
getTurnOutputTokens,
incrementBudgetContinuationCount,
getSessionId,
} from './bootstrap/state.js'
import { createBudgetTracker, checkTokenBudget } from './query/tokenBudget.js'
import { count } from './utils/array.js'
import { createTrace, endTrace, isLangfuseEnabled } from './services/langfuse/index.js'
import { getAPIProvider } from './utils/model/providers.js'
/* eslint-disable @typescript-eslint/no-require-imports */
const snipModule = feature('HISTORY_SNIP')
@@ -227,7 +230,38 @@ export async function* query(
Terminal
> {
const consumedCommandUuids: string[] = []
const terminal = yield* queryLoop(params, consumedCommandUuids)
// Create Langfuse trace for this query turn (no-op if not configured).
// When called as a sub-agent, langfuseTrace is already set by runAgent()
// — reuse it instead of creating an independent trace.
const ownsTrace = !params.toolUseContext.langfuseTrace
const langfuseTrace = params.toolUseContext.langfuseTrace
?? (isLangfuseEnabled()
? createTrace({
sessionId: getSessionId(),
model: params.toolUseContext.options.mainLoopModel,
provider: getAPIProvider(),
input: params.messages,
querySource: params.querySource,
})
: null)
// Attach trace to toolUseContext so tool execution can record observations
const paramsWithTrace: QueryParams = langfuseTrace
? {
...params,
toolUseContext: { ...params.toolUseContext, langfuseTrace },
}
: params
let terminal: Terminal
try {
terminal = yield* queryLoop(paramsWithTrace, consumedCommandUuids)
} finally {
// Only end the trace if we created it — sub-agents own their traces
if (ownsTrace) endTrace(langfuseTrace)
}
// Only reached if queryLoop returned normally. Skipped on throw (error
// propagates through yield*) and on .return() (Return completion closes
// both generators). This gives the same asymmetric started-without-completed
@@ -704,6 +738,7 @@ async function* queryLoop(
}),
},
}),
langfuseTrace: toolUseContext.langfuseTrace,
},
})) {
// We won't use the tool_calls from the first attempt