mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-17 05:45:51 +00:00
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:
37
src/query.ts
37
src/query.ts
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user