pi-langfuse 1.5.10 → 1.5.11

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/index.ts CHANGED
@@ -16,7 +16,7 @@ import { shutdownRuntime } from "./src/langfuse.js";
16
16
  import { handleLangfusePrivacyCommand, handleLangfuseStatusCommand, handleLangfuseTestCommand } from "./src/commands.js";
17
17
  import { getMessageFromEvent, extractAssistantOutput, getCapturePolicy } from "./src/utils.js";
18
18
  import { applyCapturePolicy } from "./src/capture-policy.js";
19
- import { startAgentRun, finishAgentRun } from "./src/handlers/agent.js";
19
+ import { startAgentRun, finishAgentRun, recordSystemPrompt } from "./src/handlers/agent.js";
20
20
  import { startTurnObservation, finishTurnObservation } from "./src/handlers/turn.js";
21
21
  import {
22
22
  startGeneration,
@@ -112,6 +112,9 @@ export default async function (pi: ExtensionAPI) {
112
112
  if (!state.agentState?.root) {
113
113
  await startAgentRun(event, ctx);
114
114
  }
115
+ // The system prompt is only final here: before_agent_start handlers that
116
+ // run after this extension may still rewrite it.
117
+ await recordSystemPrompt(ctx);
115
118
  }));
116
119
 
117
120
  pi.on("turn_start", async (event, ctx) => withSession(ctx, async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-langfuse",
3
- "version": "1.5.10",
3
+ "version": "1.5.11",
4
4
  "description": "Langfuse extension for Pi coding agent",
5
5
  "repository": {
6
6
  "type": "git",
@@ -54,15 +54,6 @@ export async function startAgentRun(event: Record<string, unknown>, ctx: any) {
54
54
  state.currentProvider = ctx.model.provider || "";
55
55
  }
56
56
 
57
- let systemPrompt = undefined;
58
- try {
59
- if (ctx.getSystemPrompt) {
60
- systemPrompt = await ctx.getSystemPrompt();
61
- }
62
- } catch {
63
- // Ignore if getSystemPrompt is not available or fails
64
- }
65
-
66
57
  const rawPromptInput = shapePayload({
67
58
  prompt: event.prompt,
68
59
  images: event.images,
@@ -79,7 +70,6 @@ export async function startAgentRun(event: Record<string, unknown>, ctx: any) {
79
70
  ...(state.currentProvider ? { provider: state.currentProvider } : {}),
80
71
  sessionId: state.currentSessionId || undefined,
81
72
  },
82
- systemPrompt: systemPrompt ? truncate(String(systemPrompt), getLimits().maxString) : undefined,
83
73
  },
84
74
  getCapturePolicy(),
85
75
  );
@@ -106,10 +96,7 @@ export async function startAgentRun(event: Record<string, unknown>, ctx: any) {
106
96
  "pi-agent",
107
97
  {
108
98
  input: captured.input,
109
- metadata: {
110
- ...(captured.metadata ?? {}),
111
- ...(captured.systemPrompt ? { systemPrompt: captured.systemPrompt } : {}),
112
- },
99
+ metadata: captured.metadata ?? {},
113
100
  },
114
101
  { asType: "agent" },
115
102
  ),
@@ -124,6 +111,49 @@ export async function startAgentRun(event: Record<string, unknown>, ctx: any) {
124
111
  }
125
112
  }
126
113
 
114
+ /**
115
+ * Records the effective system prompt on the root agent observation.
116
+ *
117
+ * Deliberately called from `agent_start` rather than `before_agent_start`:
118
+ * during `before_agent_start` the extension runner hands each handler the
119
+ * prompt as it stands mid-chain, so extensions registered after this one
120
+ * (e.g. inline factories that rewrite the system prompt) are not reflected
121
+ * yet. By `agent_start` the session has applied the final override, and
122
+ * `ctx.getSystemPrompt()` returns the prompt actually sent to the model.
123
+ */
124
+ export async function recordSystemPrompt(ctx: any) {
125
+ const root = state.agentState?.root;
126
+ if (state.isTracingDisabled || !root) {
127
+ return;
128
+ }
129
+
130
+ let systemPrompt = undefined;
131
+ try {
132
+ if (ctx.getSystemPrompt) {
133
+ systemPrompt = await ctx.getSystemPrompt();
134
+ }
135
+ } catch {
136
+ // Ignore if getSystemPrompt is not available or fails
137
+ }
138
+ if (!systemPrompt) {
139
+ return;
140
+ }
141
+
142
+ const captured = applyCapturePolicy(
143
+ { systemPrompt: truncate(String(systemPrompt), getLimits().maxString) },
144
+ getCapturePolicy(),
145
+ );
146
+ if (!captured.systemPrompt) {
147
+ return;
148
+ }
149
+
150
+ try {
151
+ root.update({ metadata: { systemPrompt: captured.systemPrompt } });
152
+ } catch (e) {
153
+ console.warn("\u{1F4CA} Langfuse: Failed to record system prompt", e);
154
+ }
155
+ }
156
+
127
157
  export async function finishAgentRun(event: Record<string, unknown> = {}) {
128
158
  if (!state.agentState?.root) {
129
159
  resetRunState();