@narumitw/pi-langfuse 0.50.5 → 0.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/langfuse.ts CHANGED
@@ -1,29 +1,24 @@
1
- import type {
2
- ExtensionAPI,
3
- ExtensionCommandContext,
4
- ExtensionContext,
5
- } from "@earendil-works/pi-coding-agent";
1
+ import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
6
2
  import {
7
- DEFAULT_BASE_URL,
8
- type LangfuseConfig,
9
- type LangfuseConfigPatch,
10
- type LangfuseConfigResult,
11
- loadLangfuseConfig,
12
- normalizeLangfuseConfig,
13
- writeLangfuseConfig,
3
+ DEFAULT_BASE_URL,
4
+ type LangfuseConfig,
5
+ type LangfuseConfigPatch,
6
+ type LangfuseConfigResult,
7
+ loadLangfuseConfig,
8
+ normalizeLangfuseConfig,
9
+ writeLangfuseConfig,
14
10
  } from "./config.js";
15
- import {
16
- type ContextSnapshot,
17
- type GitMetadata,
18
- type TraceBackend,
19
- TraceRecorder,
20
- } from "./tracing.js";
11
+ import { createPiLangfuseSessionController, type PiLangfuseSessionController } from "./pi-session.js";
12
+ import { createLangfuseRuntimeFromBackend } from "./runtime-core.js";
13
+ import type { GitMetadata, TraceBackend } from "./tracing.js";
14
+
15
+ export { resolveGitMetadata } from "./git.js";
21
16
 
22
17
  interface ExtensionDependencies {
23
- loadConfig(path?: string): Promise<LangfuseConfigResult>;
24
- writeConfig(config: LangfuseConfigPatch, path?: string): Promise<unknown>;
25
- createBackend(config: LangfuseConfig): Promise<TraceBackend>;
26
- resolveGitMetadata(cwd: string): Promise<GitMetadata | undefined>;
18
+ loadConfig(path?: string): Promise<LangfuseConfigResult>;
19
+ writeConfig(config: LangfuseConfigPatch, path?: string): Promise<unknown>;
20
+ createBackend(config: LangfuseConfig): Promise<TraceBackend>;
21
+ resolveGitMetadata(cwd: string): Promise<GitMetadata | undefined>;
27
22
  }
28
23
 
29
24
  const FLUSH_ACTION = "Flush completed traces for this session";
@@ -31,568 +26,317 @@ const SET_UP_ACTION = "Set up Langfuse for this Pi agent directory (restart requ
31
26
  const UPDATE_ACTION = "Update Langfuse for this Pi agent directory (restart required)";
32
27
  const HELP_ACTION = "Show setup and privacy help";
33
28
 
34
- export function createLangfuseExtension(
35
- dependencies: Partial<ExtensionDependencies> = {},
36
- ): (pi: ExtensionAPI) => void {
37
- const loadConfig = dependencies.loadConfig ?? loadLangfuseConfig;
38
- const writeConfig = dependencies.writeConfig ?? writeLangfuseConfig;
39
- const createBackend =
40
- dependencies.createBackend ??
41
- (async (config) => {
42
- const { createProductionBackend } = await import("./runtime.js");
43
- return createProductionBackend(config);
44
- });
45
-
46
- return function langfuse(pi: ExtensionAPI) {
47
- const resolveGit =
48
- dependencies.resolveGitMetadata ??
49
- ((cwd: string) =>
50
- resolveGitMetadata((command, args, options) => pi.exec(command, args, options), cwd));
51
- let recorder: TraceRecorder | undefined;
52
- let activeConfig: LangfuseConfig | undefined;
53
- let configPath: string | undefined;
54
- let initializationError: string | undefined;
55
- let hasStoredConfig = false;
56
- let configurationNotice: string | undefined;
57
- let sessionGeneration = 0;
58
- let menuController = new AbortController();
59
- let configWriteQueue = Promise.resolve();
60
- let nextAttemptReason: string | undefined;
61
-
62
- async function showLangfuseMenu(ctx: ExtensionCommandContext) {
63
- if (!ctx.hasUI) {
64
- ctx.ui.notify(
65
- formatNonInteractiveStatus(activeConfig, configPath, initializationError),
66
- "warning",
67
- );
68
- return;
69
- }
70
- const menuGeneration = sessionGeneration;
71
- const menuSignal = menuController.signal;
72
- const isCurrent = () => menuGeneration === sessionGeneration && !menuSignal.aborted;
73
- const { defineMenu, runMenu } = await import("@narumitw/pi-tui-kit");
74
- if (!isCurrent()) return;
75
- type Screen = "main";
76
- type Action = "flush" | "configure" | "help";
77
- const menu = defineMenu<undefined, Screen, Action>({
78
- start: "main",
79
- screens: {
80
- main: () => ({
81
- kind: "actions",
82
- title: "Langfuse",
83
- lines: formatMenuTitle(
84
- activeConfig,
85
- configPath,
86
- initializationError,
87
- configurationNotice,
88
- )
89
- .split("\n")
90
- .slice(1),
91
- items: [
92
- ...(recorder ? [{ id: "flush", label: FLUSH_ACTION, action: "flush" as const }] : []),
93
- {
94
- id: "configure",
95
- label: hasStoredConfig ? UPDATE_ACTION : SET_UP_ACTION,
96
- action: "configure",
97
- },
98
- { id: "help", label: HELP_ACTION, action: "help" },
99
- ],
100
- hint: "close",
101
- }),
102
- },
103
- actions: {
104
- flush: async () => {
105
- const menuRecorder = recorder;
106
- const menuConfig = activeConfig;
107
- if (!menuRecorder) {
108
- ctx.ui.notify("Langfuse tracing is not enabled for this session.", "warning");
109
- return { kind: "close" };
110
- }
111
- try {
112
- await menuRecorder.flush();
113
- if (menuGeneration !== sessionGeneration) return { kind: "close" };
114
- ctx.ui.notify("Langfuse traces flushed for this session.", "info");
115
- } catch (error) {
116
- if (menuGeneration !== sessionGeneration) return { kind: "close" };
117
- ctx.ui.notify(`Langfuse flush failed: ${formatError(error, menuConfig)}`, "error");
118
- }
119
- return { kind: "close" };
120
- },
121
- configure: async () => {
122
- const loaded = await loadConfig(configPath);
123
- if (menuGeneration !== sessionGeneration) return { kind: "close" };
124
- configPath = loaded.path;
125
- const next = await promptForConfig(
126
- ctx,
127
- loaded.ok ? loaded.config : undefined,
128
- () => menuGeneration === sessionGeneration,
129
- );
130
- if (!next || menuGeneration !== sessionGeneration) return { kind: "close" };
131
- try {
132
- const write = configWriteQueue.then(() => writeConfig(next, loaded.path));
133
- configWriteQueue = write.then(
134
- () => undefined,
135
- () => undefined,
136
- );
137
- await write;
138
- if (menuGeneration !== sessionGeneration) return { kind: "close" };
139
- hasStoredConfig = true;
140
- configurationNotice =
141
- "Saved; restart each Pi process to use it in subsequent sessions.";
142
- ctx.ui.notify(
143
- `Saved Langfuse config to ${loaded.path} for this Pi agent directory. Restart each Pi process to apply it to subsequent sessions.`,
144
- "info",
145
- );
146
- } catch (error) {
147
- if (menuGeneration !== sessionGeneration) return { kind: "close" };
148
- ctx.ui.notify(`Failed to save Langfuse config: ${formatError(error, next)}`, "error");
149
- }
150
- return { kind: "close" };
151
- },
152
- help: async () => {
153
- ctx.ui.notify(formatHelp(configPath), "info");
154
- return { kind: "close" };
155
- },
156
- },
157
- });
158
- await runMenu(ctx, menu, {
159
- getState: () => undefined,
160
- signal: menuSignal,
161
- isCurrent,
162
- });
163
- }
164
-
165
- pi.registerCommand("langfuse", {
166
- description: "Open interactive Langfuse tracing controls",
167
- handler: async (_args, ctx) => showLangfuseMenu(ctx),
168
- });
169
-
170
- pi.on("session_start", async (_event, ctx) => {
171
- const generation = ++sessionGeneration;
172
- menuController.abort(new DOMException("Langfuse session replaced", "AbortError"));
173
- menuController = new AbortController();
174
- recorder = undefined;
175
- activeConfig = undefined;
176
- configPath = undefined;
177
- initializationError = undefined;
178
- hasStoredConfig = false;
179
- configurationNotice = undefined;
180
- nextAttemptReason = undefined;
181
-
182
- await configWriteQueue;
183
- if (generation !== sessionGeneration) return;
184
- const result = await loadConfig();
185
- if (generation !== sessionGeneration) return;
186
- configPath = result.path;
187
- for (const warning of result.warnings) ctx.ui.notify(warning, "warning");
188
- if (!result.ok) {
189
- initializationError = formatConfigError(result);
190
- ctx.ui.notify(initializationError, "warning");
191
- return;
192
- }
193
-
194
- hasStoredConfig = true;
195
- try {
196
- const backend = await createBackend(result.config);
197
- if (generation !== sessionGeneration) {
198
- await backend.shutdown().catch(() => undefined);
199
- return;
200
- }
201
- activeConfig = result.config;
202
- recorder = new TraceRecorder(backend, {
203
- sessionId: ctx.sessionManager.getSessionId(),
204
- ...(result.config.userId ? { userId: result.config.userId } : {}),
205
- cwd: ctx.cwd,
206
- mode: ctx.mode,
207
- captureContent: result.config.captureContent,
208
- });
209
- } catch (error) {
210
- if (generation !== sessionGeneration) return;
211
- initializationError = `Langfuse tracing could not start: ${formatError(error, result.config)}`;
212
- ctx.ui.notify(initializationError, "warning");
213
- }
214
- });
215
-
216
- pi.on("before_agent_start", async (event, ctx) => {
217
- nextAttemptReason = undefined;
218
- const activeRecorder = recorder;
219
- if (!activeRecorder) return;
220
- const git = await resolveGit(ctx.cwd).catch(() => undefined);
221
- if (recorder !== activeRecorder) return;
222
- activeRecorder.beginAgent({
223
- prompt: event.prompt,
224
- images: event.images,
225
- model: ctx.model
226
- ? { provider: ctx.model.provider, id: ctx.model.id, api: ctx.model.api }
227
- : undefined,
228
- git,
229
- snapshot: contextSnapshot(ctx),
230
- });
231
- });
232
-
233
- pi.on("agent_start", () => {
234
- if (!recorder) return;
235
- recorder.beginAttempt(nextAttemptReason ? { reason: nextAttemptReason } : undefined);
236
- nextAttemptReason = undefined;
237
- });
238
-
239
- pi.on("turn_start", (event, ctx) => {
240
- if (!recorder) return;
241
- ensureActiveRun(recorder, ctx);
242
- recorder.beginTurn(event.turnIndex);
243
- });
244
-
245
- pi.on("before_provider_request", (event, ctx) => {
246
- if (!recorder) return;
247
- ensureActiveRun(recorder, ctx);
248
- recorder.beginGeneration({
249
- payload: event.payload,
250
- payloadStage: "before_provider_request",
251
- model: ctx.model
252
- ? { provider: ctx.model.provider, id: ctx.model.id, api: ctx.model.api }
253
- : undefined,
254
- thinkingLevel: pi.getThinkingLevel(),
255
- });
256
- });
257
-
258
- pi.on("after_provider_response", (event) => {
259
- recorder?.recordProviderResponse(event.status, event.headers);
260
- });
261
-
262
- pi.on("message_update", (event) => {
263
- if (isRealOutputDelta(event.assistantMessageEvent)) recorder?.markGenerationFirstOutput();
264
- });
265
-
266
- pi.on("message_end", (event) => {
267
- if (event.message.role === "assistant") recorder?.markGenerationEnd();
268
- });
269
-
270
- pi.on("turn_end", (event) => {
271
- if (event.message.role === "assistant") recorder?.finishAssistant(event.message);
272
- recorder?.finishTurn(event.turnIndex, {
273
- message: event.message,
274
- toolResultCount: event.toolResults.length,
275
- });
276
- });
277
-
278
- pi.on("tool_execution_start", (event) => {
279
- recorder?.beginTool(event.toolCallId, event.toolName, event.args);
280
- });
281
-
282
- pi.on("tool_execution_update", (event) => {
283
- recorder?.recordToolProgress(event.toolCallId);
284
- });
285
-
286
- pi.on("tool_result", (event) => {
287
- recorder?.recordToolInput(event.toolCallId, event.input);
288
- });
289
-
290
- pi.on("tool_execution_end", (event) => {
291
- recorder?.finishTool(event.toolCallId, {
292
- content: event.result.content,
293
- details: event.result.details,
294
- isError: event.isError,
295
- });
296
- });
297
-
298
- pi.on("agent_end", (event) => {
299
- const message = findLastAssistant(event.messages);
300
- recorder?.finishAttempt(message);
301
- });
302
-
303
- pi.on("session_before_compact", (event) => {
304
- recorder?.beginCompaction({
305
- reason: event.reason,
306
- willRetry: event.willRetry,
307
- tokensBefore: event.preparation.tokensBefore,
308
- messagesToSummarize: event.preparation.messagesToSummarize.length,
309
- turnPrefixMessages: event.preparation.turnPrefixMessages.length,
310
- branchEntries: event.branchEntries.length,
311
- isSplitTurn: event.preparation.isSplitTurn,
312
- });
313
- });
314
-
315
- pi.on("session_compact", (event) => {
316
- const entry = event.compactionEntry as typeof event.compactionEntry & {
317
- usage?: Parameters<TraceRecorder["finishCompaction"]>[0]["usage"];
318
- };
319
- recorder?.finishCompaction({
320
- reason: event.reason,
321
- willRetry: event.willRetry,
322
- fromExtension: event.fromExtension,
323
- tokensBefore: entry.tokensBefore,
324
- details: entry.details,
325
- usage: entry.usage,
326
- });
327
- if (event.willRetry && recorder?.hasActiveTrace()) nextAttemptReason = "post_compaction";
328
- });
329
-
330
- pi.on("agent_settled", (_event, ctx) => {
331
- recorder?.settle(contextSnapshot(ctx));
332
- nextAttemptReason = undefined;
333
- });
334
-
335
- pi.on("session_shutdown", async (event, ctx) => {
336
- const generation = ++sessionGeneration;
337
- menuController.abort(new DOMException("Langfuse session shut down", "AbortError"));
338
- const activeRecorder = recorder;
339
- const shutdownConfig = activeConfig;
340
- const pendingConfigWrite = configWriteQueue;
341
- const shutdownSnapshot = activeRecorder ? contextSnapshot(ctx) : undefined;
342
- recorder = undefined;
343
- activeConfig = undefined;
344
- await pendingConfigWrite;
345
- if (!activeRecorder) return;
346
- try {
347
- if (event.reason === "quit") await activeRecorder.shutdown(shutdownSnapshot);
348
- else {
349
- activeRecorder.interrupt(
350
- `Pi session ended before settlement (${event.reason}).`,
351
- shutdownSnapshot,
352
- );
353
- await activeRecorder.flush();
354
- }
355
- } catch (error) {
356
- if (generation !== sessionGeneration) return;
357
- ctx.ui.notify(
358
- `Langfuse shutdown export failed: ${formatError(error, shutdownConfig)}`,
359
- "error",
360
- );
361
- }
362
- });
363
- };
364
- }
365
-
366
- function ensureActiveRun(recorder: TraceRecorder, ctx: ExtensionContext): void {
367
- if (!recorder.hasActiveTrace()) {
368
- recorder.beginAgent({
369
- prompt: "[automatic continuation]",
370
- model: ctx.model
371
- ? { provider: ctx.model.provider, id: ctx.model.id, api: ctx.model.api }
372
- : undefined,
373
- snapshot: contextSnapshot(ctx),
374
- });
375
- }
376
- if (!recorder.hasActiveAttempt()) recorder.beginAttempt();
377
- }
378
-
379
- function contextSnapshot(ctx: ExtensionContext): ContextSnapshot {
380
- return {
381
- leafId:
382
- typeof ctx.sessionManager.getLeafId === "function"
383
- ? ctx.sessionManager.getLeafId()
384
- : undefined,
385
- contextUsage: ctx.getContextUsage(),
386
- };
387
- }
388
-
389
- function findLastAssistant<T extends { role?: string }>(messages: readonly T[]): T | undefined {
390
- for (let index = messages.length - 1; index >= 0; index -= 1) {
391
- const message = messages[index];
392
- if (message?.role === "assistant") return message;
393
- }
394
- return undefined;
395
- }
396
-
397
- function isRealOutputDelta(event: { type: string; delta?: unknown }): boolean {
398
- return (
399
- (event.type === "text_delta" ||
400
- event.type === "thinking_delta" ||
401
- event.type === "toolcall_delta") &&
402
- typeof event.delta === "string" &&
403
- event.delta.length > 0
404
- );
405
- }
406
-
407
- const GIT_LOOKUP_TIMEOUT_MS = 1_000;
408
- const MAX_GIT_BRANCH_LENGTH = 256;
409
-
410
- type GitExecutor = ExtensionAPI["exec"];
411
-
412
- export async function resolveGitMetadata(
413
- exec: GitExecutor,
414
- cwd: string,
415
- ): Promise<GitMetadata | undefined> {
416
- const [branchResult, commit] = await Promise.all([
417
- resolveGitBranch(exec, cwd),
418
- resolveGitCommit(exec, cwd),
419
- ]);
420
- if (!branchResult.resolved) return undefined;
421
- if (branchResult.branch) {
422
- return {
423
- branch: branchResult.branch,
424
- ...(commit ? { commit } : {}),
425
- detached: false,
426
- };
427
- }
428
- return commit ? { commit, detached: true } : undefined;
429
- }
430
-
431
- async function resolveGitBranch(
432
- exec: GitExecutor,
433
- cwd: string,
434
- ): Promise<{ resolved: boolean; branch?: string }> {
435
- try {
436
- const result = await exec("git", ["symbolic-ref", "--quiet", "--short", "HEAD"], {
437
- cwd,
438
- timeout: GIT_LOOKUP_TIMEOUT_MS,
439
- });
440
- if (result.killed) return { resolved: false };
441
- if (result.code === 1) return { resolved: true };
442
- if (result.code !== 0) return { resolved: false };
443
- const branch = normalizeGitBranch(result.stdout);
444
- return branch ? { resolved: true, branch } : { resolved: false };
445
- } catch {
446
- return { resolved: false };
447
- }
448
- }
449
-
450
- async function resolveGitCommit(exec: GitExecutor, cwd: string): Promise<string | undefined> {
451
- try {
452
- const result = await exec("git", ["rev-parse", "--verify", "--short=12", "HEAD"], {
453
- cwd,
454
- timeout: GIT_LOOKUP_TIMEOUT_MS,
455
- });
456
- if (result.code !== 0 || result.killed) return undefined;
457
- const commit = result.stdout.trim();
458
- return /^[0-9a-f]{4,64}$/iu.test(commit) ? commit.toLowerCase() : undefined;
459
- } catch {
460
- return undefined;
461
- }
462
- }
463
-
464
- function normalizeGitBranch(value: string): string | undefined {
465
- const branch = value.trim();
466
- if (!branch || branch.length > MAX_GIT_BRANCH_LENGTH) return undefined;
467
- for (const character of branch) {
468
- const code = character.codePointAt(0) ?? 0;
469
- if (code <= 31 || code === 127) return undefined;
470
- }
471
- return branch;
29
+ export function createLangfuseExtension(dependencies: Partial<ExtensionDependencies> = {}): (pi: ExtensionAPI) => void {
30
+ const loadConfig = dependencies.loadConfig ?? loadLangfuseConfig;
31
+ const writeConfig = dependencies.writeConfig ?? writeLangfuseConfig;
32
+ const resolveInjectedGitMetadata = dependencies.resolveGitMetadata;
33
+
34
+ return function langfuse(pi: ExtensionAPI) {
35
+ let activeConfig: LangfuseConfig | undefined;
36
+ let loadingConfig: LangfuseConfig | undefined;
37
+ let runtimeConfigForShutdown: LangfuseConfig | undefined;
38
+ let shutdownConfig: LangfuseConfig | undefined;
39
+ let configPath: string | undefined;
40
+ let initializationError: string | undefined;
41
+ let hasStoredConfig = false;
42
+ let configurationNotice: string | undefined;
43
+ let sessionGeneration = 0;
44
+ let menuController = new AbortController();
45
+ let configWriteQueue = Promise.resolve();
46
+
47
+ const tracing: PiLangfuseSessionController = createPiLangfuseSessionController({
48
+ onSessionStart() {
49
+ sessionGeneration += 1;
50
+ menuController.abort(new DOMException("Langfuse session replaced", "AbortError"));
51
+ menuController = new AbortController();
52
+ activeConfig = undefined;
53
+ loadingConfig = undefined;
54
+ shutdownConfig = undefined;
55
+ configPath = undefined;
56
+ initializationError = undefined;
57
+ hasStoredConfig = false;
58
+ configurationNotice = undefined;
59
+ },
60
+ onSessionShutdown() {
61
+ sessionGeneration += 1;
62
+ menuController.abort(new DOMException("Langfuse session shut down", "AbortError"));
63
+ shutdownConfig = activeConfig ?? runtimeConfigForShutdown ?? loadingConfig;
64
+ activeConfig = undefined;
65
+ },
66
+ async resolveSession(ctx, isCurrent) {
67
+ await configWriteQueue;
68
+ if (!isCurrent()) return undefined;
69
+ const result = await loadConfig();
70
+ if (!isCurrent()) return undefined;
71
+ configPath = result.path;
72
+ for (const warning of result.warnings) ctx.ui.notify(warning, "warning");
73
+ if (!result.ok) {
74
+ initializationError = formatConfigError(result);
75
+ ctx.ui.notify(initializationError, "warning");
76
+ return undefined;
77
+ }
78
+
79
+ hasStoredConfig = true;
80
+ loadingConfig = result.config;
81
+ const createBackend = dependencies.createBackend;
82
+ const ownsRuntime = createBackend !== undefined;
83
+ const runtime = createBackend
84
+ ? createLangfuseRuntimeFromBackend(await createBackend(result.config))
85
+ : await (await import("./runtime.js")).createLangfuseRuntime({ config: result.config, env: false });
86
+ if (isCurrent() || !runtimeConfigForShutdown) runtimeConfigForShutdown = result.config;
87
+ if (isCurrent()) {
88
+ activeConfig = result.config;
89
+ loadingConfig = undefined;
90
+ }
91
+ return {
92
+ runtime,
93
+ releaseIfStale: async (reason) => {
94
+ if (ownsRuntime || reason === "quit") await runtime.shutdown();
95
+ },
96
+ options: {
97
+ ...(result.config.userId ? { userId: result.config.userId } : {}),
98
+ captureContent: result.config.captureContent,
99
+ },
100
+ };
101
+ },
102
+ ...(resolveInjectedGitMetadata
103
+ ? { resolveGitMetadata: (_pi: ExtensionAPI, cwd: string) => resolveInjectedGitMetadata(cwd) }
104
+ : {}),
105
+ onInitializationError(error, ctx) {
106
+ const config = loadingConfig;
107
+ loadingConfig = undefined;
108
+ initializationError = `Langfuse tracing could not start: ${formatError(error, config)}`;
109
+ ctx.ui.notify(initializationError, "warning");
110
+ },
111
+ beforeSessionDispose: async () => {
112
+ await configWriteQueue;
113
+ },
114
+ onShutdownError(error, ctx) {
115
+ ctx.ui.notify(`Langfuse shutdown export failed: ${formatError(error, shutdownConfig)}`, "error");
116
+ },
117
+ flushOnReplacement: true,
118
+ shutdownRuntimeOnQuit: true,
119
+ });
120
+
121
+ async function showLangfuseMenu(ctx: ExtensionCommandContext) {
122
+ if (!ctx.hasUI) {
123
+ ctx.ui.notify(formatNonInteractiveStatus(activeConfig, configPath, initializationError), "warning");
124
+ return;
125
+ }
126
+ const menuGeneration = sessionGeneration;
127
+ const menuSignal = menuController.signal;
128
+ const isCurrent = () => menuGeneration === sessionGeneration && !menuSignal.aborted;
129
+ const { defineMenu, runMenu } = await import("@narumitw/pi-tui-kit");
130
+ if (!isCurrent()) return;
131
+ type Screen = "main";
132
+ type Action = "flush" | "configure" | "help";
133
+ const menu = defineMenu<undefined, Screen, Action>({
134
+ start: "main",
135
+ screens: {
136
+ main: () => ({
137
+ kind: "actions",
138
+ title: "Langfuse",
139
+ lines: formatMenuTitle(activeConfig, configPath, initializationError, configurationNotice)
140
+ .split("\n")
141
+ .slice(1),
142
+ items: [
143
+ ...(tracing.active ? [{ id: "flush", label: FLUSH_ACTION, action: "flush" as const }] : []),
144
+ {
145
+ id: "configure",
146
+ label: hasStoredConfig ? UPDATE_ACTION : SET_UP_ACTION,
147
+ action: "configure",
148
+ },
149
+ { id: "help", label: HELP_ACTION, action: "help" },
150
+ ],
151
+ hint: "close",
152
+ }),
153
+ },
154
+ actions: {
155
+ flush: async () => {
156
+ const menuConfig = activeConfig;
157
+ if (!tracing.active) {
158
+ ctx.ui.notify("Langfuse tracing is not enabled for this session.", "warning");
159
+ return { kind: "close" };
160
+ }
161
+ try {
162
+ await tracing.flush();
163
+ if (menuGeneration !== sessionGeneration) return { kind: "close" };
164
+ ctx.ui.notify("Langfuse traces flushed for this session.", "info");
165
+ } catch (error) {
166
+ if (menuGeneration !== sessionGeneration) return { kind: "close" };
167
+ ctx.ui.notify(`Langfuse flush failed: ${formatError(error, menuConfig)}`, "error");
168
+ }
169
+ return { kind: "close" };
170
+ },
171
+ configure: async () => {
172
+ const loaded = await loadConfig(configPath);
173
+ if (menuGeneration !== sessionGeneration) return { kind: "close" };
174
+ configPath = loaded.path;
175
+ const next = await promptForConfig(
176
+ ctx,
177
+ loaded.ok ? loaded.config : undefined,
178
+ () => menuGeneration === sessionGeneration,
179
+ );
180
+ if (!next || menuGeneration !== sessionGeneration) return { kind: "close" };
181
+ try {
182
+ const write = configWriteQueue.then(() => writeConfig(next, loaded.path));
183
+ configWriteQueue = write.then(
184
+ () => undefined,
185
+ () => undefined,
186
+ );
187
+ await write;
188
+ if (menuGeneration !== sessionGeneration) return { kind: "close" };
189
+ hasStoredConfig = true;
190
+ configurationNotice = "Saved; restart each Pi process to use it in subsequent sessions.";
191
+ ctx.ui.notify(
192
+ `Saved Langfuse config to ${loaded.path} for this Pi agent directory. Restart each Pi process to apply it to subsequent sessions.`,
193
+ "info",
194
+ );
195
+ } catch (error) {
196
+ if (menuGeneration !== sessionGeneration) return { kind: "close" };
197
+ ctx.ui.notify(`Failed to save Langfuse config: ${formatError(error, next)}`, "error");
198
+ }
199
+ return { kind: "close" };
200
+ },
201
+ help: async () => {
202
+ ctx.ui.notify(formatHelp(configPath), "info");
203
+ return { kind: "close" };
204
+ },
205
+ },
206
+ });
207
+ await runMenu(ctx, menu, {
208
+ getState: () => undefined,
209
+ signal: menuSignal,
210
+ isCurrent,
211
+ });
212
+ }
213
+
214
+ pi.registerCommand("langfuse", {
215
+ description: "Open interactive Langfuse tracing controls",
216
+ handler: async (_args, ctx) => showLangfuseMenu(ctx),
217
+ });
218
+
219
+ tracing.extension(pi);
220
+ };
472
221
  }
473
222
 
474
223
  async function promptForConfig(
475
- ctx: ExtensionCommandContext,
476
- current: LangfuseConfig | undefined,
477
- isCurrent: () => boolean,
224
+ ctx: ExtensionCommandContext,
225
+ current: LangfuseConfig | undefined,
226
+ isCurrent: () => boolean,
478
227
  ): Promise<LangfuseConfigPatch | undefined> {
479
- const secretKey = await ctx.ui.input("Langfuse secret key (leave blank to keep existing):");
480
- if (!isCurrent()) return undefined;
481
- if (secretKey === undefined) {
482
- ctx.ui.notify("Cancelled", "info");
483
- return undefined;
484
- }
485
- const publicKey = await ctx.ui.input("Langfuse public key (leave blank to keep existing):");
486
- if (!isCurrent()) return undefined;
487
- if (publicKey === undefined) {
488
- ctx.ui.notify("Cancelled", "info");
489
- return undefined;
490
- }
491
- const baseUrl = await ctx.ui.input(
492
- `Langfuse base URL (leave blank for default ${DEFAULT_BASE_URL}):`,
493
- DEFAULT_BASE_URL,
494
- );
495
- if (!isCurrent()) return undefined;
496
- if (baseUrl === undefined) {
497
- ctx.ui.notify("Cancelled", "info");
498
- return undefined;
499
- }
500
-
501
- const patch: LangfuseConfigPatch = {
502
- ...(publicKey.trim() ? { publicKey: publicKey.trim() } : {}),
503
- ...(secretKey.trim() ? { secretKey: secretKey.trim() } : {}),
504
- baseUrl: baseUrl.trim() || DEFAULT_BASE_URL,
505
- };
506
- const normalized = normalizeLangfuseConfig({ ...current, ...patch });
507
- if (!normalized.ok) {
508
- ctx.ui.notify(`Invalid Langfuse config: ${normalized.reason}`, "error");
509
- return undefined;
510
- }
511
- return patch;
228
+ const secretKey = await ctx.ui.input("Langfuse secret key (leave blank to keep existing):");
229
+ if (!isCurrent()) return undefined;
230
+ if (secretKey === undefined) {
231
+ ctx.ui.notify("Cancelled", "info");
232
+ return undefined;
233
+ }
234
+ const publicKey = await ctx.ui.input("Langfuse public key (leave blank to keep existing):");
235
+ if (!isCurrent()) return undefined;
236
+ if (publicKey === undefined) {
237
+ ctx.ui.notify("Cancelled", "info");
238
+ return undefined;
239
+ }
240
+ const baseUrl = await ctx.ui.input(
241
+ `Langfuse base URL (leave blank for default ${DEFAULT_BASE_URL}):`,
242
+ DEFAULT_BASE_URL,
243
+ );
244
+ if (!isCurrent()) return undefined;
245
+ if (baseUrl === undefined) {
246
+ ctx.ui.notify("Cancelled", "info");
247
+ return undefined;
248
+ }
249
+
250
+ const patch: LangfuseConfigPatch = {
251
+ ...(publicKey.trim() ? { publicKey: publicKey.trim() } : {}),
252
+ ...(secretKey.trim() ? { secretKey: secretKey.trim() } : {}),
253
+ baseUrl: baseUrl.trim() || DEFAULT_BASE_URL,
254
+ };
255
+ const normalized = normalizeLangfuseConfig({ ...current, ...patch });
256
+ if (!normalized.ok) {
257
+ ctx.ui.notify(`Invalid Langfuse config: ${normalized.reason}`, "error");
258
+ return undefined;
259
+ }
260
+ return patch;
512
261
  }
513
262
 
514
263
  function formatMenuTitle(
515
- activeConfig: LangfuseConfig | undefined,
516
- configPath: string | undefined,
517
- initializationError: string | undefined,
518
- configurationNotice: string | undefined,
264
+ activeConfig: LangfuseConfig | undefined,
265
+ configPath: string | undefined,
266
+ initializationError: string | undefined,
267
+ configurationNotice: string | undefined,
519
268
  ): string {
520
- const lines = [
521
- "Langfuse",
522
- "",
523
- "Current session:",
524
- ` Tracing: ${activeConfig ? "enabled" : "disabled"}`,
525
- ];
526
- if (activeConfig) {
527
- lines.push(
528
- ` Endpoint: ${activeConfig.baseUrl}`,
529
- ` Content capture: ${activeConfig.captureContent ? "enabled" : "disabled"}`,
530
- );
531
- } else if (configurationNotice) {
532
- lines.push(" State: tracing remains disabled until Pi restarts.");
533
- } else if (initializationError) {
534
- lines.push(` Reason: ${initializationError}`);
535
- }
536
- lines.push(
537
- "",
538
- "Agent-directory configuration:",
539
- ` Configuration: ${configPath ?? "unknown"}`,
540
- " Scope: this Pi agent directory; restart each Pi process to apply changes.",
541
- );
542
- if (configurationNotice) lines.push(` Pending: ${configurationNotice}`);
543
- lines.push("", "What do you want to do?");
544
- return lines.join("\n");
269
+ const lines = ["Langfuse", "", "Current session:", ` Tracing: ${activeConfig ? "enabled" : "disabled"}`];
270
+ if (activeConfig) {
271
+ lines.push(
272
+ ` Endpoint: ${activeConfig.baseUrl}`,
273
+ ` Content capture: ${activeConfig.captureContent ? "enabled" : "disabled"}`,
274
+ );
275
+ } else if (configurationNotice) {
276
+ lines.push(" State: tracing remains disabled until Pi restarts.");
277
+ } else if (initializationError) {
278
+ lines.push(` Reason: ${initializationError}`);
279
+ }
280
+ lines.push(
281
+ "",
282
+ "Agent-directory configuration:",
283
+ ` Configuration: ${configPath ?? "unknown"}`,
284
+ " Scope: this Pi agent directory; restart each Pi process to apply changes.",
285
+ );
286
+ if (configurationNotice) lines.push(` Pending: ${configurationNotice}`);
287
+ lines.push("", "What do you want to do?");
288
+ return lines.join("\n");
545
289
  }
546
290
 
547
291
  function formatNonInteractiveStatus(
548
- activeConfig: LangfuseConfig | undefined,
549
- configPath: string | undefined,
550
- initializationError: string | undefined,
292
+ activeConfig: LangfuseConfig | undefined,
293
+ configPath: string | undefined,
294
+ initializationError: string | undefined,
551
295
  ): string {
552
- return [
553
- "/langfuse requires interactive UI.",
554
- `Current session tracing: ${activeConfig ? "enabled" : "disabled"}`,
555
- ...(activeConfig
556
- ? [
557
- `Endpoint: ${activeConfig.baseUrl}`,
558
- `Content capture: ${activeConfig.captureContent ? "enabled" : "disabled"}`,
559
- ]
560
- : initializationError
561
- ? [`Reason: ${initializationError}`]
562
- : []),
563
- `Configuration: ${configPath ?? "unknown"}`,
564
- "Edit the private config manually, then restart each Pi process to apply it to subsequent sessions.",
565
- ].join("\n");
296
+ return [
297
+ "/langfuse requires interactive UI.",
298
+ `Current session tracing: ${activeConfig ? "enabled" : "disabled"}`,
299
+ ...(activeConfig
300
+ ? [
301
+ `Endpoint: ${activeConfig.baseUrl}`,
302
+ `Content capture: ${activeConfig.captureContent ? "enabled" : "disabled"}`,
303
+ ]
304
+ : initializationError
305
+ ? [`Reason: ${initializationError}`]
306
+ : []),
307
+ `Configuration: ${configPath ?? "unknown"}`,
308
+ "Edit the private config manually, then restart each Pi process to apply it to subsequent sessions.",
309
+ ].join("\n");
566
310
  }
567
311
 
568
312
  function formatHelp(configPath: string | undefined): string {
569
- return [
570
- "Langfuse setup and privacy:",
571
- "Run /langfuse in interactive Pi to manage tracing for the current session.",
572
- `Configuration: ${configPath ?? "pi-langfuse.json"}`,
573
- "The file belongs to this Pi agent directory; restart each Pi process after changing it.",
574
- "Trace content may contain prompts, responses, tool arguments, tool results, and source code.",
575
- "Git branch, commit, cwd, model, and usage remain in metadata when content capture is disabled.",
576
- 'Use "captureContent": false in the private config to export metadata only.',
577
- ].join("\n");
313
+ return [
314
+ "Langfuse setup and privacy:",
315
+ "Run /langfuse in interactive Pi to manage tracing for the current session.",
316
+ `Configuration: ${configPath ?? "pi-langfuse.json"}`,
317
+ "The file belongs to this Pi agent directory; restart each Pi process after changing it.",
318
+ "Trace content may contain prompts, responses, tool arguments, tool results, and source code.",
319
+ "Git branch, commit, cwd, model, and usage remain in metadata when content capture is disabled.",
320
+ 'Use "captureContent": false in the private config to export metadata only.',
321
+ ].join("\n");
578
322
  }
579
323
 
580
324
  function formatConfigError(result: Extract<LangfuseConfigResult, { ok: false }>): string {
581
- const setupHint = result.reason.startsWith("Configuration file not found:")
582
- ? " Run /langfuse and choose Set up Langfuse."
583
- : "";
584
- return `Langfuse tracing is disabled: ${result.reason}${setupHint}`;
325
+ const setupHint = result.reason.startsWith("Configuration file not found:")
326
+ ? " Run /langfuse and choose Set up Langfuse."
327
+ : "";
328
+ return `Langfuse tracing is disabled: ${result.reason}${setupHint}`;
585
329
  }
586
330
 
587
331
  function formatError(error: unknown, config?: LangfuseConfigPatch): string {
588
- let message = error instanceof Error ? error.message : String(error);
589
- const secrets = [config?.publicKey, config?.secretKey]
590
- .filter((secret): secret is string => Boolean(secret))
591
- .sort((left, right) => right.length - left.length);
592
- for (const secret of secrets) {
593
- message = message.split(secret).join("[LANGFUSE_KEY_REDACTED]");
594
- }
595
- return message;
332
+ let message = error instanceof Error ? error.message : String(error);
333
+ const secrets = [config?.publicKey, config?.secretKey]
334
+ .filter((secret): secret is string => Boolean(secret))
335
+ .sort((left, right) => right.length - left.length);
336
+ for (const secret of secrets) {
337
+ message = message.split(secret).join("[LANGFUSE_KEY_REDACTED]");
338
+ }
339
+ return message;
596
340
  }
597
341
 
598
342
  export default createLangfuseExtension();