pi-langfuse 1.7.1 → 1.8.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/README.md +20 -4
- package/README_CN.md +20 -4
- package/dist/index.d.ts +14 -0
- package/dist/index.js +216 -0
- package/dist/src/capture-policy.d.ts +43 -0
- package/dist/src/capture-policy.js +125 -0
- package/dist/src/commands.d.ts +25 -0
- package/dist/src/commands.js +374 -0
- package/dist/src/config.d.ts +13 -0
- package/dist/src/config.js +157 -0
- package/dist/src/constants.d.ts +9 -0
- package/dist/src/constants.js +11 -0
- package/dist/src/handlers/agent.d.ts +18 -0
- package/dist/src/handlers/agent.js +310 -0
- package/dist/src/handlers/cache.d.ts +5 -0
- package/dist/src/handlers/cache.js +134 -0
- package/dist/src/handlers/generation.d.ts +9 -0
- package/dist/src/handlers/generation.js +249 -0
- package/dist/src/handlers/session.d.ts +4 -0
- package/dist/src/handlers/session.js +121 -0
- package/dist/src/handlers/system-state.d.ts +3 -0
- package/dist/src/handlers/system-state.js +185 -0
- package/dist/src/handlers/tool.d.ts +3 -0
- package/dist/src/handlers/tool.js +168 -0
- package/dist/src/handlers/turn.d.ts +2 -0
- package/dist/src/handlers/turn.js +48 -0
- package/dist/src/langfuse.d.ts +42 -0
- package/dist/src/langfuse.js +837 -0
- package/dist/src/limits.d.ts +43 -0
- package/dist/src/limits.js +63 -0
- package/dist/src/observation.d.ts +24 -0
- package/dist/src/observation.js +24 -0
- package/dist/src/redaction.d.ts +14 -0
- package/dist/src/redaction.js +254 -0
- package/dist/src/session-lineage.d.ts +15 -0
- package/dist/src/session-lineage.js +87 -0
- package/dist/src/source-metadata.d.ts +2 -0
- package/dist/src/source-metadata.js +55 -0
- package/dist/src/state.d.ts +52 -0
- package/dist/src/state.js +144 -0
- package/dist/src/types.d.ts +157 -0
- package/dist/src/types.js +1 -0
- package/dist/src/usage-options.d.ts +27 -0
- package/dist/src/usage-options.js +21 -0
- package/dist/src/utils.d.ts +61 -0
- package/dist/src/utils.js +600 -0
- package/dist/src/workflow.d.ts +24 -0
- package/dist/src/workflow.js +62 -0
- package/examples/workflow-sdk.mjs +56 -0
- package/index.ts +40 -12
- package/package.json +12 -1
- package/src/commands.ts +14 -3
- package/src/config.ts +1 -1
- package/src/handlers/agent.ts +60 -32
- package/src/handlers/cache.ts +12 -10
- package/src/handlers/session.ts +2 -2
- package/src/handlers/tool.ts +6 -1
- package/src/langfuse.ts +35 -16
- package/src/observation.ts +4 -0
- package/src/state.ts +12 -2
- package/src/types.ts +1 -1
- package/src/workflow.ts +78 -0
package/src/observation.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getSessionRunState } from "./state.js";
|
|
1
2
|
import type { LangfuseObservation, LangfuseRuntime, ObservationUpdate } from "./types.js";
|
|
2
3
|
|
|
3
4
|
export async function startChildObservation({
|
|
@@ -17,6 +18,9 @@ export async function startChildObservation({
|
|
|
17
18
|
return parent.startObservation(name, body, { asType });
|
|
18
19
|
}
|
|
19
20
|
|
|
21
|
+
if (getSessionRunState().runBinding?.parent) {
|
|
22
|
+
throw new Error("Cannot attach child observation to its workflow parent");
|
|
23
|
+
}
|
|
20
24
|
return (await runtime()).startObservation(name, body, { asType });
|
|
21
25
|
}
|
|
22
26
|
|
package/src/state.ts
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
2
|
import type { Config, AgentState } from "./types.js";
|
|
3
|
+
import type { WorkflowBinding } from "./workflow.js";
|
|
3
4
|
|
|
4
5
|
export interface SessionRunState {
|
|
6
|
+
instance: symbol;
|
|
7
|
+
startupBinding?: WorkflowBinding;
|
|
8
|
+
runBinding?: WorkflowBinding;
|
|
9
|
+
startingRun?: Promise<void>;
|
|
10
|
+
finishingRun?: boolean;
|
|
11
|
+
workflowWarnings?: Set<string>;
|
|
5
12
|
currentModel: string;
|
|
6
13
|
currentProvider: string;
|
|
7
14
|
agentState: AgentState | null;
|
|
@@ -26,8 +33,9 @@ const DEFAULT_SESSION_ID = "__pi_langfuse_default_session__";
|
|
|
26
33
|
let activeSessionId = DEFAULT_SESSION_ID;
|
|
27
34
|
const sessionScope = new AsyncLocalStorage<string>();
|
|
28
35
|
|
|
29
|
-
function createSessionRunState(): SessionRunState {
|
|
36
|
+
function createSessionRunState(instance = Symbol("Pi session")): SessionRunState {
|
|
30
37
|
return {
|
|
38
|
+
instance,
|
|
31
39
|
currentModel: "",
|
|
32
40
|
currentProvider: "",
|
|
33
41
|
agentState: null,
|
|
@@ -146,7 +154,9 @@ export function resetRunState(sessionId = getActiveSessionId()) {
|
|
|
146
154
|
const normalizedSessionId = normalizeSessionId(sessionId);
|
|
147
155
|
const previous = state.sessionStates.get(normalizedSessionId);
|
|
148
156
|
state.sessionStates.set(normalizedSessionId, {
|
|
149
|
-
...createSessionRunState(),
|
|
157
|
+
...createSessionRunState(previous?.instance),
|
|
158
|
+
startupBinding: previous?.startupBinding,
|
|
159
|
+
workflowWarnings: previous?.workflowWarnings,
|
|
150
160
|
setupAttemptedThisSession: previous?.setupAttemptedThisSession ?? false,
|
|
151
161
|
lastPromptStateHash: previous?.lastPromptStateHash,
|
|
152
162
|
lastToolStateHash: previous?.lastToolStateHash,
|
package/src/types.ts
CHANGED
|
@@ -83,7 +83,7 @@ export interface LangfuseRuntime {
|
|
|
83
83
|
startObservation: (
|
|
84
84
|
name: string,
|
|
85
85
|
body?: ObservationUpdate,
|
|
86
|
-
options?: { asType?: "agent" | "event" | "generation" | "tool" | "span" },
|
|
86
|
+
options?: { asType?: "agent" | "event" | "generation" | "tool" | "span"; parentSpanContext?: import("@opentelemetry/api").SpanContext },
|
|
87
87
|
) => LangfuseObservation;
|
|
88
88
|
propagateAttributes: (
|
|
89
89
|
params: {
|
package/src/workflow.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { defaultTextMapGetter, ROOT_CONTEXT, trace, TraceFlags, type SpanContext } from "@opentelemetry/api";
|
|
2
|
+
import { W3CTraceContextPropagator } from "@opentelemetry/core";
|
|
3
|
+
import { redactString } from "./redaction.js";
|
|
4
|
+
import { getSessionRunState, state } from "./state.js";
|
|
5
|
+
import { truncate } from "./utils.js";
|
|
6
|
+
|
|
7
|
+
export interface WorkflowContext {
|
|
8
|
+
sessionId?: string;
|
|
9
|
+
traceparent?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface LangfuseExtensionOptions {
|
|
13
|
+
getRunContext?(run: { piSessionId: string; messageType: "user" | "custom" | "unknown" }): WorkflowContext | undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface WorkflowBinding {
|
|
17
|
+
sessionId?: string;
|
|
18
|
+
parent?: SpanContext;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const propagator = new W3CTraceContextPropagator();
|
|
22
|
+
|
|
23
|
+
export function warnWorkflow(reason: string) {
|
|
24
|
+
const session = getSessionRunState();
|
|
25
|
+
const warnings = session.workflowWarnings ??= new Set<string>();
|
|
26
|
+
if (warnings.has(reason)) return;
|
|
27
|
+
warnings.add(reason);
|
|
28
|
+
console.warn(`📊 Langfuse: ${reason}`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function parseWorkflowContext(input: { sessionId?: unknown; traceparent?: unknown }): WorkflowBinding {
|
|
32
|
+
const binding: WorkflowBinding = {};
|
|
33
|
+
const { sessionId, traceparent } = input;
|
|
34
|
+
if (sessionId !== undefined) {
|
|
35
|
+
if (typeof sessionId === "string" && sessionId.length > 0 && sessionId.length <= 200
|
|
36
|
+
&& !/[^\x20-\x7e]/.test(sessionId) && sessionId.trim() === sessionId && redactString(sessionId) === sessionId) {
|
|
37
|
+
binding.sessionId = sessionId;
|
|
38
|
+
} else {
|
|
39
|
+
warnWorkflow("Ignoring invalid workflow session ID; using the default grouping for this trace.");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (traceparent !== undefined) {
|
|
43
|
+
if (typeof traceparent === "string" && traceparent.length <= 512 && !/[^\x21-\x7e]/.test(traceparent)) {
|
|
44
|
+
const context = propagator.extract(ROOT_CONTEXT, { traceparent }, defaultTextMapGetter);
|
|
45
|
+
binding.parent = trace.getSpanContext(context);
|
|
46
|
+
}
|
|
47
|
+
if (!binding.parent) warnWorkflow("Ignoring invalid traceparent; creating an independent trace.");
|
|
48
|
+
}
|
|
49
|
+
return binding;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function resolveRunBinding(event: Record<string, unknown>, getRunContext?: LangfuseExtensionOptions["getRunContext"]): WorkflowBinding {
|
|
53
|
+
const session = getSessionRunState();
|
|
54
|
+
if (getRunContext && state.currentSessionId) {
|
|
55
|
+
try {
|
|
56
|
+
const messageType = event.messageType === "custom" ? "custom"
|
|
57
|
+
: typeof event.prompt === "string" || event.messageType === "user" ? "user" : "unknown";
|
|
58
|
+
const input = getRunContext({ piSessionId: state.currentSessionId, messageType });
|
|
59
|
+
if (input !== undefined) return parseWorkflowContext(input);
|
|
60
|
+
} catch {
|
|
61
|
+
warnWorkflow("Workflow context callback failed; using an independent trace.");
|
|
62
|
+
return {};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return session.startupBinding ?? {};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function sessionGroupId() {
|
|
69
|
+
return getSessionRunState().startupBinding?.sessionId ?? localSessionGroupId();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function localSessionGroupId() {
|
|
73
|
+
return state.currentSessionId ? truncate(state.currentSessionId, 200) : undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function isUnsampled(binding?: WorkflowBinding) {
|
|
77
|
+
return !!binding?.parent && (binding.parent.traceFlags & TraceFlags.SAMPLED) === 0;
|
|
78
|
+
}
|