@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/README.md +125 -62
- package/dist/chunks/chunk-IASLJKKB.js +316 -0
- package/dist/chunks/chunk-IASLJKKB.js.map +7 -0
- package/dist/chunks/runtime-6MIKLWNJ.js +352 -0
- package/dist/chunks/runtime-6MIKLWNJ.js.map +7 -0
- package/dist/config.d.ts +33 -0
- package/dist/git.d.ts +5 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1551 -0
- package/dist/index.js.map +7 -0
- package/dist/index.ts +594 -521
- package/dist/index.ts.map +4 -4
- package/dist/langfuse.d.ts +13 -0
- package/dist/pi-session.d.ts +43 -0
- package/dist/public-api.d.ts +4 -0
- package/dist/runtime-core.d.ts +26 -0
- package/dist/runtime.d.ts +22 -0
- package/dist/sanitizer.d.ts +2 -0
- package/dist/tracing.d.ts +202 -0
- package/package.json +63 -59
- package/src/config.ts +211 -232
- package/src/git.ts +60 -0
- package/src/index.ts +9 -0
- package/src/langfuse.ts +307 -563
- package/src/pi-session.ts +570 -0
- package/src/public-api.ts +17 -0
- package/src/runtime-core.ts +113 -0
- package/src/runtime.ts +393 -227
- package/src/sanitizer.ts +115 -117
- package/src/tracing.ts +828 -843
- package/dist/chunks/runtime-247EMSG3.js +0 -218
- package/dist/chunks/runtime-247EMSG3.js.map +0 -7
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { CreateLangfuseRuntimeOptions, LangfuseRuntime } from "./runtime-core.js";
|
|
2
|
+
export { createPiLangfuseSession, type PiLangfuseSession, type PiLangfuseSessionOptions, } from "./pi-session.js";
|
|
3
|
+
export type { CreateLangfuseRuntimeOptions, LangfuseRuntime, LangfuseRuntimeConfig, } from "./runtime-core.js";
|
|
4
|
+
export declare function createLangfuseRuntime(options?: CreateLangfuseRuntimeOptions): Promise<LangfuseRuntime>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { TraceBackend } from "./tracing.js";
|
|
2
|
+
export interface LangfuseRuntimeConfig {
|
|
3
|
+
publicKey?: string;
|
|
4
|
+
secretKey?: string;
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
environment?: string;
|
|
7
|
+
release?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface CreateLangfuseRuntimeOptions {
|
|
10
|
+
config?: LangfuseRuntimeConfig;
|
|
11
|
+
env?: NodeJS.ProcessEnv | false;
|
|
12
|
+
}
|
|
13
|
+
export interface LangfuseRuntime {
|
|
14
|
+
readonly closed: boolean;
|
|
15
|
+
flush(): Promise<void>;
|
|
16
|
+
shutdown(): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
type RuntimeSessionDisposer = (statusMessage: string) => void | Promise<void>;
|
|
19
|
+
export interface LangfuseRuntimeInternal extends LangfuseRuntime {
|
|
20
|
+
readonly backend: TraceBackend;
|
|
21
|
+
registerSession(disposer: RuntimeSessionDisposer): () => void;
|
|
22
|
+
}
|
|
23
|
+
export declare function getLangfuseRuntimeInternal(runtime: LangfuseRuntime): LangfuseRuntimeInternal;
|
|
24
|
+
export declare function createLangfuseRuntimeFromBackend(backend: TraceBackend): LangfuseRuntime;
|
|
25
|
+
export declare function registerLangfuseRuntime(runtime: LangfuseRuntimeInternal): LangfuseRuntimeInternal;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { SpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
2
|
+
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
|
|
3
|
+
import { type LangfuseConfig } from "./config.js";
|
|
4
|
+
import { type CreateLangfuseRuntimeOptions, type LangfuseRuntime } from "./runtime-core.js";
|
|
5
|
+
import type { TraceBackend } from "./tracing.js";
|
|
6
|
+
export type { CreateLangfuseRuntimeOptions, LangfuseRuntime, LangfuseRuntimeConfig, } from "./runtime-core.js";
|
|
7
|
+
interface ResolvedRuntimeConfig {
|
|
8
|
+
publicKey: string;
|
|
9
|
+
secretKey: string;
|
|
10
|
+
baseUrl: string;
|
|
11
|
+
environment?: string;
|
|
12
|
+
release?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface RuntimeFactories {
|
|
15
|
+
createProcessor(config: ResolvedRuntimeConfig): SpanProcessor;
|
|
16
|
+
createProvider(processor: SpanProcessor): NodeTracerProvider;
|
|
17
|
+
selectProvider(provider: NodeTracerProvider): void;
|
|
18
|
+
}
|
|
19
|
+
export declare function createLangfuseRuntime(options?: CreateLangfuseRuntimeOptions): Promise<LangfuseRuntime>;
|
|
20
|
+
export declare function createProductionBackend(config: LangfuseConfig, factoryOverrides?: Partial<RuntimeFactories>): Promise<TraceBackend>;
|
|
21
|
+
export declare function resolveLangfuseRuntimeConfig(options: CreateLangfuseRuntimeOptions): ResolvedRuntimeConfig;
|
|
22
|
+
export declare function maskSecrets(data: unknown, secrets: readonly string[]): unknown;
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
export { MAX_CAPTURE_BYTES, sanitizeTraceValue } from "./sanitizer.js";
|
|
2
|
+
export declare const TRACE_SCHEMA_VERSION = "2";
|
|
3
|
+
export interface ObservationAttributes {
|
|
4
|
+
input?: unknown;
|
|
5
|
+
output?: unknown;
|
|
6
|
+
metadata?: Record<string, unknown>;
|
|
7
|
+
level?: "DEBUG" | "DEFAULT" | "WARNING" | "ERROR";
|
|
8
|
+
statusMessage?: string;
|
|
9
|
+
completionStartTime?: Date;
|
|
10
|
+
model?: string;
|
|
11
|
+
modelParameters?: Record<string, string | number>;
|
|
12
|
+
usageDetails?: Record<string, number>;
|
|
13
|
+
costDetails?: Record<string, number>;
|
|
14
|
+
version?: string;
|
|
15
|
+
name?: string;
|
|
16
|
+
sessionId?: string;
|
|
17
|
+
userId?: string;
|
|
18
|
+
tags?: string[];
|
|
19
|
+
}
|
|
20
|
+
export interface Observation {
|
|
21
|
+
readonly traceId?: string;
|
|
22
|
+
update(attributes: ObservationAttributes): Observation;
|
|
23
|
+
updateTrace?(attributes: ObservationAttributes): Observation;
|
|
24
|
+
end(endTime?: number): Observation;
|
|
25
|
+
}
|
|
26
|
+
export type ObservationType = "agent" | "generation" | "span" | "tool";
|
|
27
|
+
export interface TraceBackend {
|
|
28
|
+
start(name: string, attributes: ObservationAttributes, options: {
|
|
29
|
+
asType: ObservationType;
|
|
30
|
+
parent?: Observation;
|
|
31
|
+
startTime?: Date;
|
|
32
|
+
}): Observation;
|
|
33
|
+
forceFlush(): Promise<void>;
|
|
34
|
+
shutdown(): Promise<void>;
|
|
35
|
+
}
|
|
36
|
+
export interface TraceRecorderOptions {
|
|
37
|
+
sessionId: string;
|
|
38
|
+
userId?: string;
|
|
39
|
+
cwd: string;
|
|
40
|
+
mode: string;
|
|
41
|
+
captureContent: boolean;
|
|
42
|
+
traceName?: string;
|
|
43
|
+
tags?: readonly string[];
|
|
44
|
+
metadata?: Readonly<Record<string, unknown>>;
|
|
45
|
+
onTraceId?: (traceId: string) => void;
|
|
46
|
+
}
|
|
47
|
+
interface ModelDescriptor {
|
|
48
|
+
provider?: string;
|
|
49
|
+
id?: string;
|
|
50
|
+
api?: string;
|
|
51
|
+
}
|
|
52
|
+
export interface GitMetadata {
|
|
53
|
+
branch?: string;
|
|
54
|
+
commit?: string;
|
|
55
|
+
detached: boolean;
|
|
56
|
+
}
|
|
57
|
+
export interface ContextSnapshot {
|
|
58
|
+
leafId?: string | null;
|
|
59
|
+
contextUsage?: {
|
|
60
|
+
tokens: number | null;
|
|
61
|
+
contextWindow: number;
|
|
62
|
+
percent: number | null;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
interface BeginAgentInput {
|
|
66
|
+
prompt: unknown;
|
|
67
|
+
images?: unknown;
|
|
68
|
+
model?: ModelDescriptor;
|
|
69
|
+
git?: GitMetadata;
|
|
70
|
+
snapshot?: ContextSnapshot;
|
|
71
|
+
requestId?: string;
|
|
72
|
+
}
|
|
73
|
+
interface AttemptInput {
|
|
74
|
+
reason?: string;
|
|
75
|
+
}
|
|
76
|
+
interface GenerationInput {
|
|
77
|
+
startedAt?: number;
|
|
78
|
+
payload?: unknown;
|
|
79
|
+
payloadStage?: "before_provider_request";
|
|
80
|
+
model?: ModelDescriptor;
|
|
81
|
+
thinkingLevel?: string;
|
|
82
|
+
}
|
|
83
|
+
interface AssistantMessage {
|
|
84
|
+
role: string;
|
|
85
|
+
content?: unknown;
|
|
86
|
+
provider?: string;
|
|
87
|
+
api?: string;
|
|
88
|
+
model?: string;
|
|
89
|
+
responseModel?: string;
|
|
90
|
+
responseId?: string;
|
|
91
|
+
usage?: {
|
|
92
|
+
input?: number;
|
|
93
|
+
output?: number;
|
|
94
|
+
cacheRead?: number;
|
|
95
|
+
cacheWrite?: number;
|
|
96
|
+
cacheWrite1h?: number;
|
|
97
|
+
reasoning?: number;
|
|
98
|
+
totalTokens?: number;
|
|
99
|
+
cost?: {
|
|
100
|
+
input?: number;
|
|
101
|
+
output?: number;
|
|
102
|
+
cacheRead?: number;
|
|
103
|
+
cacheWrite?: number;
|
|
104
|
+
total?: number;
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
stopReason?: string;
|
|
108
|
+
errorMessage?: string;
|
|
109
|
+
}
|
|
110
|
+
interface ToolResult {
|
|
111
|
+
content?: unknown;
|
|
112
|
+
details?: unknown;
|
|
113
|
+
isError?: boolean;
|
|
114
|
+
}
|
|
115
|
+
interface TurnResult {
|
|
116
|
+
message: {
|
|
117
|
+
role: string;
|
|
118
|
+
stopReason?: string;
|
|
119
|
+
errorMessage?: string;
|
|
120
|
+
};
|
|
121
|
+
toolResultCount: number;
|
|
122
|
+
}
|
|
123
|
+
interface CompactionStart {
|
|
124
|
+
reason: string;
|
|
125
|
+
willRetry: boolean;
|
|
126
|
+
tokensBefore?: number;
|
|
127
|
+
messagesToSummarize: number;
|
|
128
|
+
turnPrefixMessages: number;
|
|
129
|
+
branchEntries: number;
|
|
130
|
+
isSplitTurn: boolean;
|
|
131
|
+
}
|
|
132
|
+
interface CompactionFinish {
|
|
133
|
+
reason: string;
|
|
134
|
+
willRetry: boolean;
|
|
135
|
+
fromExtension: boolean;
|
|
136
|
+
tokensBefore?: number;
|
|
137
|
+
details?: unknown;
|
|
138
|
+
usage?: {
|
|
139
|
+
input?: number;
|
|
140
|
+
output?: number;
|
|
141
|
+
cacheRead?: number;
|
|
142
|
+
cacheWrite?: number;
|
|
143
|
+
totalTokens?: number;
|
|
144
|
+
cost?: {
|
|
145
|
+
input?: number;
|
|
146
|
+
output?: number;
|
|
147
|
+
cacheRead?: number;
|
|
148
|
+
cacheWrite?: number;
|
|
149
|
+
total?: number;
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
export declare class TraceRecorder {
|
|
154
|
+
private readonly backend;
|
|
155
|
+
private readonly context;
|
|
156
|
+
private root;
|
|
157
|
+
private attempt;
|
|
158
|
+
private attemptIndex;
|
|
159
|
+
private turn;
|
|
160
|
+
private turnIndex;
|
|
161
|
+
private generation;
|
|
162
|
+
private readonly tools;
|
|
163
|
+
private readonly duplicateToolIds;
|
|
164
|
+
private compaction;
|
|
165
|
+
private lastOutput;
|
|
166
|
+
private lastAssistant;
|
|
167
|
+
private lastAttemptOutcome;
|
|
168
|
+
private unresolvedToolErrors;
|
|
169
|
+
private startSnapshot;
|
|
170
|
+
private counters;
|
|
171
|
+
constructor(backend: TraceBackend, context: TraceRecorderOptions);
|
|
172
|
+
private startObservation;
|
|
173
|
+
hasActiveTrace(): boolean;
|
|
174
|
+
hasActiveAttempt(): boolean;
|
|
175
|
+
beginAgent(input: BeginAgentInput): void;
|
|
176
|
+
beginAttempt(input?: AttemptInput): void;
|
|
177
|
+
finishAttempt(message?: AssistantMessage): void;
|
|
178
|
+
beginTurn(turnIndex: number): void;
|
|
179
|
+
finishTurn(turnIndex: number, result: TurnResult): void;
|
|
180
|
+
beginGeneration(input?: GenerationInput): void;
|
|
181
|
+
recordProviderResponse(status: number, headers?: Record<string, string>): void;
|
|
182
|
+
markGenerationFirstOutput(timestamp?: number): void;
|
|
183
|
+
markGenerationEnd(endTime?: number): void;
|
|
184
|
+
finishAssistant(message: AssistantMessage): void;
|
|
185
|
+
beginTool(toolCallId: string, toolName: string, args?: unknown, startedAt?: number): void;
|
|
186
|
+
recordToolInput(toolCallId: string, input: unknown): void;
|
|
187
|
+
recordToolProgress(toolCallId: string, timestamp?: number): void;
|
|
188
|
+
finishTool(toolCallId: string, result: ToolResult): void;
|
|
189
|
+
beginCompaction(input: CompactionStart): void;
|
|
190
|
+
finishCompaction(input: CompactionFinish): void;
|
|
191
|
+
settle(snapshot?: ContextSnapshot): void;
|
|
192
|
+
interrupt(statusMessage: string, snapshot?: ContextSnapshot): void;
|
|
193
|
+
dispose(statusMessage?: string, snapshot?: ContextSnapshot): void;
|
|
194
|
+
private closeActiveTrace;
|
|
195
|
+
private closeAttempt;
|
|
196
|
+
private startToolObservation;
|
|
197
|
+
private closeTurn;
|
|
198
|
+
private closeTools;
|
|
199
|
+
private closeGeneration;
|
|
200
|
+
private closeCompaction;
|
|
201
|
+
private capture;
|
|
202
|
+
}
|
package/package.json
CHANGED
|
@@ -1,61 +1,65 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
2
|
+
"name": "@narumitw/pi-langfuse",
|
|
3
|
+
"version": "0.51.0",
|
|
4
|
+
"description": "Pi extension that traces LLM generations and tool activity to Langfuse.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"private": false,
|
|
16
|
+
"keywords": [
|
|
17
|
+
"pi-package",
|
|
18
|
+
"pi-extension",
|
|
19
|
+
"pi",
|
|
20
|
+
"langfuse",
|
|
21
|
+
"observability",
|
|
22
|
+
"tracing"
|
|
23
|
+
],
|
|
24
|
+
"files": [
|
|
25
|
+
"src",
|
|
26
|
+
"dist",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE"
|
|
29
|
+
],
|
|
30
|
+
"pi": {
|
|
31
|
+
"extensions": [
|
|
32
|
+
"./dist/index.ts"
|
|
33
|
+
]
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "node scripts/build-runtime.mjs",
|
|
37
|
+
"prepack": "npm run build",
|
|
38
|
+
"check": "npm run build && biome check .",
|
|
39
|
+
"format": "biome check --write .",
|
|
40
|
+
"typecheck": "tsc --noEmit"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@earendil-works/pi-coding-agent": "0.86.0",
|
|
47
|
+
"@types/node": "26.6.2",
|
|
48
|
+
"typescript": "7.0.2",
|
|
49
|
+
"esbuild": "0.28.2"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"@langfuse/otel": "^5.11.1",
|
|
53
|
+
"@langfuse/tracing": "^5.11.1",
|
|
54
|
+
"@narumitw/pi-tui-kit": "^0.59.0",
|
|
55
|
+
"@opentelemetry/api": "^1.9.1",
|
|
56
|
+
"@opentelemetry/exporter-trace-otlp-http": "^0.222.0",
|
|
57
|
+
"@opentelemetry/sdk-trace-base": "^2.11.0",
|
|
58
|
+
"@opentelemetry/sdk-trace-node": "^2.11.0"
|
|
59
|
+
},
|
|
60
|
+
"repository": {
|
|
61
|
+
"type": "git",
|
|
62
|
+
"url": "https://github.com/narumiruna/pi-extensions",
|
|
63
|
+
"directory": "packages/pi-langfuse"
|
|
64
|
+
}
|
|
61
65
|
}
|