@prefactor/langchain 0.2.5 → 0.2.7
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 +1 -1
- package/dist/index.cjs +49 -32
- package/dist/index.cjs.map +4 -4
- package/dist/index.js +52 -33
- package/dist/index.js.map +4 -4
- package/dist/init.d.ts.map +1 -1
- package/dist/init.js +10 -0
- package/dist/init.js.map +1 -1
- package/dist/middleware.d.ts +4 -1
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +40 -31
- package/dist/middleware.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -82,53 +82,39 @@ function extractTokenUsage(response) {
|
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
// packages/langchain/src/middleware.ts
|
|
85
|
+
var toLangchainSpanType = import_core.createSpanTypePrefixer("langchain");
|
|
86
|
+
var logger = import_core.getLogger("middleware");
|
|
87
|
+
|
|
85
88
|
class PrefactorMiddleware {
|
|
86
89
|
tracer;
|
|
87
90
|
agentManager;
|
|
88
91
|
agentInfo;
|
|
89
|
-
|
|
92
|
+
agentInstanceStarted = false;
|
|
90
93
|
constructor(tracer, agentManager, agentInfo) {
|
|
91
94
|
this.tracer = tracer;
|
|
92
95
|
this.agentManager = agentManager;
|
|
93
96
|
this.agentInfo = agentInfo;
|
|
94
97
|
}
|
|
95
98
|
async beforeAgent(state) {
|
|
96
|
-
|
|
97
|
-
this.agentManager.startInstance(this.agentInfo);
|
|
98
|
-
const span = this.tracer.startSpan({
|
|
99
|
-
name: "langchain:agent",
|
|
100
|
-
spanType: `langchain:${import_core.SpanType.AGENT}`,
|
|
101
|
-
inputs: { messages: import_core.serializeValue(messages.slice(-3)) }
|
|
102
|
-
});
|
|
103
|
-
this.rootSpan = span;
|
|
104
|
-
import_core.SpanContext.enter(span);
|
|
99
|
+
this.ensureAgentInstanceStarted();
|
|
105
100
|
}
|
|
106
|
-
async afterAgent(state) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
const messages = state?.messages ?? [];
|
|
111
|
-
this.tracer.endSpan(this.rootSpan, {
|
|
112
|
-
outputs: { messages: import_core.serializeValue(messages.slice(-3)) }
|
|
113
|
-
});
|
|
114
|
-
this.agentManager.finishInstance();
|
|
115
|
-
import_core.SpanContext.exit();
|
|
116
|
-
this.rootSpan = null;
|
|
101
|
+
async afterAgent(state) {}
|
|
102
|
+
shutdown() {
|
|
103
|
+
this.finishAgentInstance();
|
|
117
104
|
}
|
|
118
105
|
async wrapModelCall(request, handler) {
|
|
106
|
+
this.ensureAgentInstanceStarted();
|
|
119
107
|
const modelName = this.extractModelName(request);
|
|
120
108
|
const span = this.tracer.startSpan({
|
|
121
109
|
name: "langchain:llm-call",
|
|
122
|
-
spanType:
|
|
110
|
+
spanType: toLangchainSpanType(import_core.SpanType.LLM),
|
|
123
111
|
inputs: {
|
|
124
112
|
...this.extractModelInputs(request),
|
|
125
113
|
"langchain.model.name": modelName
|
|
126
114
|
}
|
|
127
115
|
});
|
|
128
116
|
try {
|
|
129
|
-
const response = await import_core.SpanContext.runAsync(span,
|
|
130
|
-
return handler(request);
|
|
131
|
-
});
|
|
117
|
+
const response = await import_core.SpanContext.runAsync(span, () => handler(request));
|
|
132
118
|
const outputs = this.extractModelOutputs(response);
|
|
133
119
|
const tokenUsage = extractTokenUsage(response);
|
|
134
120
|
this.tracer.endSpan(span, { outputs, tokenUsage: tokenUsage ?? undefined });
|
|
@@ -139,19 +125,18 @@ class PrefactorMiddleware {
|
|
|
139
125
|
}
|
|
140
126
|
}
|
|
141
127
|
async wrapToolCall(request, handler) {
|
|
128
|
+
this.ensureAgentInstanceStarted();
|
|
142
129
|
const toolName = this.extractToolName(request);
|
|
143
130
|
const span = this.tracer.startSpan({
|
|
144
131
|
name: "langchain:tool-call",
|
|
145
|
-
spanType:
|
|
132
|
+
spanType: toLangchainSpanType(import_core.SpanType.TOOL),
|
|
146
133
|
inputs: {
|
|
147
134
|
...this.extractToolInputs(request),
|
|
148
135
|
"langchain.tool.name": toolName
|
|
149
136
|
}
|
|
150
137
|
});
|
|
151
138
|
try {
|
|
152
|
-
const response = await import_core.SpanContext.runAsync(span,
|
|
153
|
-
return handler(request);
|
|
154
|
-
});
|
|
139
|
+
const response = await import_core.SpanContext.runAsync(span, () => handler(request));
|
|
155
140
|
this.tracer.endSpan(span, {
|
|
156
141
|
outputs: this.extractToolOutputs(response)
|
|
157
142
|
});
|
|
@@ -197,10 +182,32 @@ class PrefactorMiddleware {
|
|
|
197
182
|
extractToolOutputs(response) {
|
|
198
183
|
return { output: response?.output ?? response };
|
|
199
184
|
}
|
|
185
|
+
ensureAgentInstanceStarted() {
|
|
186
|
+
if (this.agentInstanceStarted) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
try {
|
|
190
|
+
this.agentManager.startInstance(this.agentInfo);
|
|
191
|
+
this.agentInstanceStarted = true;
|
|
192
|
+
} catch (error) {
|
|
193
|
+
logger.error("Failed to start agent instance:", error);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
finishAgentInstance() {
|
|
197
|
+
if (!this.agentInstanceStarted) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
try {
|
|
201
|
+
this.agentManager.finishInstance();
|
|
202
|
+
this.agentInstanceStarted = false;
|
|
203
|
+
} catch (error) {
|
|
204
|
+
logger.error("Failed to finish agent instance:", error);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
200
207
|
}
|
|
201
208
|
|
|
202
209
|
// packages/langchain/src/init.ts
|
|
203
|
-
var
|
|
210
|
+
var logger2 = import_core2.getLogger("init");
|
|
204
211
|
var DEFAULT_LANGCHAIN_AGENT_SCHEMA = {
|
|
205
212
|
external_identifier: "langchain-schema",
|
|
206
213
|
span_schemas: {
|
|
@@ -208,18 +215,27 @@ var DEFAULT_LANGCHAIN_AGENT_SCHEMA = {
|
|
|
208
215
|
"langchain:llm": { type: "object", additionalProperties: true },
|
|
209
216
|
"langchain:tool": { type: "object", additionalProperties: true },
|
|
210
217
|
"langchain:chain": { type: "object", additionalProperties: true }
|
|
218
|
+
},
|
|
219
|
+
span_result_schemas: {
|
|
220
|
+
"langchain:agent": { type: "object", additionalProperties: true },
|
|
221
|
+
"langchain:llm": { type: "object", additionalProperties: true },
|
|
222
|
+
"langchain:tool": { type: "object", additionalProperties: true },
|
|
223
|
+
"langchain:chain": { type: "object", additionalProperties: true }
|
|
211
224
|
}
|
|
212
225
|
};
|
|
213
226
|
var globalCore = null;
|
|
214
227
|
var globalTracer = null;
|
|
215
228
|
var globalMiddleware = null;
|
|
229
|
+
var globalPrefactorMiddleware = null;
|
|
216
230
|
import_core2.registerShutdownHandler("prefactor-langchain", () => {
|
|
231
|
+
globalPrefactorMiddleware?.shutdown();
|
|
217
232
|
if (globalCore) {
|
|
218
|
-
|
|
233
|
+
logger2.info("Shutting down Prefactor SDK");
|
|
219
234
|
}
|
|
220
235
|
globalCore = null;
|
|
221
236
|
globalTracer = null;
|
|
222
237
|
globalMiddleware = null;
|
|
238
|
+
globalPrefactorMiddleware = null;
|
|
223
239
|
});
|
|
224
240
|
function init(config) {
|
|
225
241
|
import_core2.configureLogging();
|
|
@@ -286,6 +302,7 @@ function init(config) {
|
|
|
286
302
|
}
|
|
287
303
|
});
|
|
288
304
|
globalMiddleware = middleware;
|
|
305
|
+
globalPrefactorMiddleware = prefactorMiddleware;
|
|
289
306
|
return middleware;
|
|
290
307
|
}
|
|
291
308
|
function getTracer() {
|
|
@@ -303,4 +320,4 @@ process.on("beforeExit", () => {
|
|
|
303
320
|
});
|
|
304
321
|
});
|
|
305
322
|
|
|
306
|
-
//# debugId=
|
|
323
|
+
//# debugId=F4D712943DD18B9A64756E2164756E21
|
package/dist/index.cjs.map
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
"sources": ["../src/index.ts", "../src/init.ts", "../src/middleware.ts", "../src/metadata-extractor.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
5
|
"// Main entry points\n\n// Convenience re-exports from core\nexport {\n type Config,\n type CoreRuntime,\n type HttpTransportConfig,\n type Span,\n SpanStatus,\n SpanType,\n shutdown,\n} from '@prefactor/core';\nexport { getTracer, init, withSpan } from './init.js';\nexport { extractTokenUsage } from './metadata-extractor.js';\n// Middleware\nexport { PrefactorMiddleware } from './middleware.js';\n",
|
|
6
|
-
"import {\n type Config,\n type CoreRuntime,\n configureLogging,\n createConfig,\n createCore,\n getLogger,\n registerShutdownHandler,\n shutdown as shutdownCore,\n type Tracer,\n withSpan as withCoreSpan,\n} from '@prefactor/core';\nimport { type AgentMiddleware, createMiddleware } from 'langchain';\nimport { PrefactorMiddleware } from './middleware.js';\n\nconst logger = getLogger('init');\n\nconst DEFAULT_LANGCHAIN_AGENT_SCHEMA = {\n external_identifier: 'langchain-schema',\n span_schemas: {\n 'langchain:agent': { type: 'object', additionalProperties: true },\n 'langchain:llm': { type: 'object', additionalProperties: true },\n 'langchain:tool': { type: 'object', additionalProperties: true },\n 'langchain:chain': { type: 'object', additionalProperties: true },\n },\n} as const;\n\nlet globalCore: CoreRuntime | null = null;\nlet globalTracer: Tracer | null = null;\nlet globalMiddleware: AgentMiddleware | null = null;\n\nregisterShutdownHandler('prefactor-langchain', () => {\n if (globalCore) {\n logger.info('Shutting down Prefactor SDK');\n }\n\n globalCore = null;\n globalTracer = null;\n globalMiddleware = null;\n});\n\nexport type ManualSpanOptions = {\n name: string;\n spanType: string;\n inputs: Record<string, unknown>;\n metadata?: Record<string, unknown>;\n};\n\n/**\n * Initialize the Prefactor SDK and return middleware for LangChain.js\n *\n * This is the main entry point for the SDK. Call this function to create a middleware\n * instance that you can pass to your LangChain.js agents.\n *\n * @param config - Optional configuration object\n * @returns PrefactorMiddleware instance to use with LangChain.js agents\n *\n * @example\n * ```typescript\n * import { init } from '@prefactor/langchain';\n * import { createAgent } from 'langchain';\n *\n * // Initialize with HTTP transport\n * const middleware = init({\n * transportType: 'http',\n * httpConfig: {\n * apiUrl: 'https://api.prefactor.ai',\n * apiToken: process.env.PREFACTOR_API_TOKEN!,\n * agentIdentifier: 'my-langchain-agent',\n * },\n * });\n *\n * // Or configure HTTP transport\n * const middleware = init({\n * transportType: 'http',\n * httpConfig: {\n * apiUrl: 'https://api.prefactor.ai',\n * apiToken: process.env.PREFACTOR_API_TOKEN!,\n * agentIdentifier: 'my-langchain-agent', // Required\n * agentId: 'legacy-agent-id', // Optional legacy identifier\n * }\n * });\n *\n * const agent = createAgent({\n * model: 'claude-sonnet-4-5-20250929',\n * tools: [myTool],\n * middleware: [middleware],\n * });\n * ```\n */\nexport function init(config?: Partial<Config>): AgentMiddleware {\n configureLogging();\n\n let configWithHttp = config;\n const transportType = config?.transportType ?? process.env.PREFACTOR_TRANSPORT ?? 'http';\n\n if (transportType === 'http' && !config?.httpConfig) {\n const apiUrl = process.env.PREFACTOR_API_URL;\n const apiToken = process.env.PREFACTOR_API_TOKEN;\n\n if (!apiUrl || !apiToken) {\n throw new Error(\n 'HTTP transport requires PREFACTOR_API_URL and PREFACTOR_API_TOKEN environment variables, ' +\n 'or httpConfig to be provided in configuration'\n );\n }\n\n configWithHttp = {\n ...config,\n transportType: 'http',\n httpConfig: {\n apiUrl,\n apiToken,\n agentId: process.env.PREFACTOR_AGENT_ID,\n agentName: process.env.PREFACTOR_AGENT_NAME,\n agentIdentifier: process.env.PREFACTOR_AGENT_IDENTIFIER || '1.0.0',\n agentSchema: DEFAULT_LANGCHAIN_AGENT_SCHEMA,\n },\n };\n } else if (transportType === 'http' && config?.httpConfig && !config.httpConfig.agentSchema) {\n configWithHttp = {\n ...config,\n httpConfig: {\n ...config.httpConfig,\n agentSchema: DEFAULT_LANGCHAIN_AGENT_SCHEMA,\n },\n };\n }\n\n const finalConfig = createConfig(configWithHttp);\n\n if (globalMiddleware !== null) {\n return globalMiddleware;\n }\n\n const core = createCore(finalConfig);\n globalCore = core;\n globalTracer = core.tracer;\n\n const httpConfig = finalConfig.httpConfig;\n if (httpConfig?.agentSchema) {\n core.agentManager.registerSchema(httpConfig.agentSchema);\n }\n\n const agentInfo = finalConfig.httpConfig\n ? {\n agentId: finalConfig.httpConfig.agentId,\n agentIdentifier: finalConfig.httpConfig.agentIdentifier,\n agentName: finalConfig.httpConfig.agentName,\n agentDescription: finalConfig.httpConfig.agentDescription,\n }\n : undefined;\n\n const prefactorMiddleware = new PrefactorMiddleware(core.tracer, core.agentManager, agentInfo);\n\n const middleware = createMiddleware({\n name: 'prefactor',\n // biome-ignore lint/suspicious/noExplicitAny: LangChain middleware hooks use dynamic types\n wrapModelCall: async (request: any, handler: any) => {\n return prefactorMiddleware.wrapModelCall(request, handler);\n },\n // biome-ignore lint/suspicious/noExplicitAny: LangChain middleware hooks use dynamic types\n wrapToolCall: async (request: any, handler: any) => {\n return prefactorMiddleware.wrapToolCall(request, handler);\n },\n // biome-ignore lint/suspicious/noExplicitAny: LangChain middleware hooks use dynamic types\n beforeAgent: async (state: any) => {\n await prefactorMiddleware.beforeAgent(state);\n },\n // biome-ignore lint/suspicious/noExplicitAny: LangChain middleware hooks use dynamic types\n afterAgent: async (state: any) => {\n await prefactorMiddleware.afterAgent(state);\n },\n });\n\n globalMiddleware = middleware;\n return middleware;\n}\n\n/**\n * Get the current tracer instance.\n *\n * If no tracer has been created yet, this will call init() with default configuration.\n *\n * @returns Tracer instance\n *\n * @example\n * ```typescript\n * import { getTracer } from '@prefactor/langchain';\n *\n * const tracer = getTracer();\n * const span = tracer.startSpan({\n * name: 'custom-operation',\n * spanType: SpanType.TOOL,\n * inputs: { data: 'example' }\n * });\n * ```\n */\nexport function getTracer(): Tracer {\n if (!globalTracer) {\n init();\n }\n // Safe because init() always sets globalTracer\n return globalTracer as Tracer;\n}\n\nexport async function withSpan<T>(\n options: ManualSpanOptions,\n fn: () => Promise<T> | T\n): Promise<T> {\n return withCoreSpan(options, fn);\n}\n\nexport { shutdownCore as shutdown };\n\n// Automatic shutdown on process exit\nprocess.on('beforeExit', () => {\n shutdownCore().catch((error) => {\n console.error('Error during Prefactor SDK shutdown:', error);\n });\n});\n",
|
|
7
|
-
"import {\n type AgentInstanceManager,\n SpanContext,\n SpanType,\n serializeValue,\n type Tracer,\n} from '@prefactor/core';\nimport { extractTokenUsage } from './metadata-extractor.js';\n\n/**\n * Prefactor middleware for LangChain.js agents.\n *\n * This middleware automatically traces LLM calls, tool executions, and agent workflows.\n * It integrates with LangChain.js middleware API to provide transparent instrumentation.\n *\n * Features:\n * - Automatic parent-child span relationships via context propagation\n * - Token usage extraction for LLM calls\n * - Error tracking and debugging\n * - Zero-overhead instrumentation (graceful failure)\n *\n * @example\n * ```typescript\n * import { init } from '@prefactor/langchain';\n * import { createReactAgent } from '@langchain/langgraph/prebuilt';\n *\n * const middleware = init();\n * const agent = createReactAgent({\n * llm: model,\n * tools: [myTool],\n * middleware: [middleware],\n * });\n * ```\n */\nexport class PrefactorMiddleware {\n private
|
|
6
|
+
"import {\n type Config,\n type CoreRuntime,\n configureLogging,\n createConfig,\n createCore,\n getLogger,\n registerShutdownHandler,\n shutdown as shutdownCore,\n type Tracer,\n withSpan as withCoreSpan,\n} from '@prefactor/core';\nimport { type AgentMiddleware, createMiddleware } from 'langchain';\nimport { PrefactorMiddleware } from './middleware.js';\n\nconst logger = getLogger('init');\n\nconst DEFAULT_LANGCHAIN_AGENT_SCHEMA = {\n external_identifier: 'langchain-schema',\n span_schemas: {\n 'langchain:agent': { type: 'object', additionalProperties: true },\n 'langchain:llm': { type: 'object', additionalProperties: true },\n 'langchain:tool': { type: 'object', additionalProperties: true },\n 'langchain:chain': { type: 'object', additionalProperties: true },\n },\n span_result_schemas: {\n 'langchain:agent': { type: 'object', additionalProperties: true },\n 'langchain:llm': { type: 'object', additionalProperties: true },\n 'langchain:tool': { type: 'object', additionalProperties: true },\n 'langchain:chain': { type: 'object', additionalProperties: true },\n },\n} as const;\n\nlet globalCore: CoreRuntime | null = null;\nlet globalTracer: Tracer | null = null;\nlet globalMiddleware: AgentMiddleware | null = null;\nlet globalPrefactorMiddleware: PrefactorMiddleware | null = null;\n\nregisterShutdownHandler('prefactor-langchain', () => {\n globalPrefactorMiddleware?.shutdown();\n\n if (globalCore) {\n logger.info('Shutting down Prefactor SDK');\n }\n\n globalCore = null;\n globalTracer = null;\n globalMiddleware = null;\n globalPrefactorMiddleware = null;\n});\n\nexport type ManualSpanOptions = {\n name: string;\n spanType: string;\n inputs: Record<string, unknown>;\n metadata?: Record<string, unknown>;\n};\n\n/**\n * Initialize the Prefactor SDK and return middleware for LangChain.js\n *\n * This is the main entry point for the SDK. Call this function to create a middleware\n * instance that you can pass to your LangChain.js agents.\n *\n * @param config - Optional configuration object\n * @returns PrefactorMiddleware instance to use with LangChain.js agents\n *\n * @example\n * ```typescript\n * import { init } from '@prefactor/langchain';\n * import { createAgent } from 'langchain';\n *\n * // Initialize with HTTP transport\n * const middleware = init({\n * transportType: 'http',\n * httpConfig: {\n * apiUrl: 'https://api.prefactor.ai',\n * apiToken: process.env.PREFACTOR_API_TOKEN!,\n * agentIdentifier: 'my-langchain-agent',\n * },\n * });\n *\n * // Or configure HTTP transport\n * const middleware = init({\n * transportType: 'http',\n * httpConfig: {\n * apiUrl: 'https://api.prefactor.ai',\n * apiToken: process.env.PREFACTOR_API_TOKEN!,\n * agentIdentifier: 'my-langchain-agent', // Required\n * agentId: 'legacy-agent-id', // Optional legacy identifier\n * }\n * });\n *\n * const agent = createAgent({\n * model: 'claude-sonnet-4-5-20250929',\n * tools: [myTool],\n * middleware: [middleware],\n * });\n * ```\n */\nexport function init(config?: Partial<Config>): AgentMiddleware {\n configureLogging();\n\n let configWithHttp = config;\n const transportType = config?.transportType ?? process.env.PREFACTOR_TRANSPORT ?? 'http';\n\n if (transportType === 'http' && !config?.httpConfig) {\n const apiUrl = process.env.PREFACTOR_API_URL;\n const apiToken = process.env.PREFACTOR_API_TOKEN;\n\n if (!apiUrl || !apiToken) {\n throw new Error(\n 'HTTP transport requires PREFACTOR_API_URL and PREFACTOR_API_TOKEN environment variables, ' +\n 'or httpConfig to be provided in configuration'\n );\n }\n\n configWithHttp = {\n ...config,\n transportType: 'http',\n httpConfig: {\n apiUrl,\n apiToken,\n agentId: process.env.PREFACTOR_AGENT_ID,\n agentName: process.env.PREFACTOR_AGENT_NAME,\n agentIdentifier: process.env.PREFACTOR_AGENT_IDENTIFIER || '1.0.0',\n agentSchema: DEFAULT_LANGCHAIN_AGENT_SCHEMA,\n },\n };\n } else if (transportType === 'http' && config?.httpConfig && !config.httpConfig.agentSchema) {\n configWithHttp = {\n ...config,\n httpConfig: {\n ...config.httpConfig,\n agentSchema: DEFAULT_LANGCHAIN_AGENT_SCHEMA,\n },\n };\n }\n\n const finalConfig = createConfig(configWithHttp);\n\n if (globalMiddleware !== null) {\n return globalMiddleware;\n }\n\n const core = createCore(finalConfig);\n globalCore = core;\n globalTracer = core.tracer;\n\n const httpConfig = finalConfig.httpConfig;\n if (httpConfig?.agentSchema) {\n core.agentManager.registerSchema(httpConfig.agentSchema);\n }\n\n const agentInfo = finalConfig.httpConfig\n ? {\n agentId: finalConfig.httpConfig.agentId,\n agentIdentifier: finalConfig.httpConfig.agentIdentifier,\n agentName: finalConfig.httpConfig.agentName,\n agentDescription: finalConfig.httpConfig.agentDescription,\n }\n : undefined;\n\n const prefactorMiddleware = new PrefactorMiddleware(core.tracer, core.agentManager, agentInfo);\n\n const middleware = createMiddleware({\n name: 'prefactor',\n // biome-ignore lint/suspicious/noExplicitAny: LangChain middleware hooks use dynamic types\n wrapModelCall: async (request: any, handler: any) => {\n return prefactorMiddleware.wrapModelCall(request, handler);\n },\n // biome-ignore lint/suspicious/noExplicitAny: LangChain middleware hooks use dynamic types\n wrapToolCall: async (request: any, handler: any) => {\n return prefactorMiddleware.wrapToolCall(request, handler);\n },\n // biome-ignore lint/suspicious/noExplicitAny: LangChain middleware hooks use dynamic types\n beforeAgent: async (state: any) => {\n await prefactorMiddleware.beforeAgent(state);\n },\n // biome-ignore lint/suspicious/noExplicitAny: LangChain middleware hooks use dynamic types\n afterAgent: async (state: any) => {\n await prefactorMiddleware.afterAgent(state);\n },\n });\n\n globalMiddleware = middleware;\n globalPrefactorMiddleware = prefactorMiddleware;\n return middleware;\n}\n\n/**\n * Get the current tracer instance.\n *\n * If no tracer has been created yet, this will call init() with default configuration.\n *\n * @returns Tracer instance\n *\n * @example\n * ```typescript\n * import { getTracer } from '@prefactor/langchain';\n *\n * const tracer = getTracer();\n * const span = tracer.startSpan({\n * name: 'custom-operation',\n * spanType: SpanType.TOOL,\n * inputs: { data: 'example' }\n * });\n * ```\n */\nexport function getTracer(): Tracer {\n if (!globalTracer) {\n init();\n }\n // Safe because init() always sets globalTracer\n return globalTracer as Tracer;\n}\n\nexport async function withSpan<T>(\n options: ManualSpanOptions,\n fn: () => Promise<T> | T\n): Promise<T> {\n return withCoreSpan(options, fn);\n}\n\nexport { shutdownCore as shutdown };\n\n// Automatic shutdown on process exit\nprocess.on('beforeExit', () => {\n shutdownCore().catch((error) => {\n console.error('Error during Prefactor SDK shutdown:', error);\n });\n});\n",
|
|
7
|
+
"import {\n type AgentInstanceManager,\n createSpanTypePrefixer,\n getLogger,\n SpanContext,\n SpanType,\n serializeValue,\n type Tracer,\n} from '@prefactor/core';\nimport { extractTokenUsage } from './metadata-extractor.js';\n\nconst toLangchainSpanType = createSpanTypePrefixer('langchain');\nconst logger = getLogger('middleware');\n\n/**\n * Prefactor middleware for LangChain.js agents.\n *\n * This middleware automatically traces LLM calls, tool executions, and agent workflows.\n * It integrates with LangChain.js middleware API to provide transparent instrumentation.\n *\n * Features:\n * - Automatic parent-child span relationships via context propagation\n * - Token usage extraction for LLM calls\n * - Error tracking and debugging\n * - Zero-overhead instrumentation (graceful failure)\n *\n * @example\n * ```typescript\n * import { init } from '@prefactor/langchain';\n * import { createReactAgent } from '@langchain/langgraph/prebuilt';\n *\n * const middleware = init();\n * const agent = createReactAgent({\n * llm: model,\n * tools: [myTool],\n * middleware: [middleware],\n * });\n * ```\n */\nexport class PrefactorMiddleware {\n private agentInstanceStarted = false;\n\n constructor(\n private tracer: Tracer,\n private agentManager: AgentInstanceManager,\n private agentInfo?: Parameters<AgentInstanceManager['startInstance']>[0]\n ) {}\n\n /**\n * Called before agent execution starts\n *\n * @param state - Agent state containing messages\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain state can be any structure\n async beforeAgent(state: any): Promise<void> {\n this.ensureAgentInstanceStarted();\n }\n\n /**\n * Called after agent execution completes\n *\n * @param state - Agent state containing messages\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain state can be any structure\n async afterAgent(state: any): Promise<void> {\n // Root agent spans are intentionally not emitted.\n void state;\n }\n\n shutdown(): void {\n this.finishAgentInstance();\n }\n\n /**\n * Wrap a model call to trace LLM invocations\n *\n * @param request - Model invocation request\n * @param handler - The actual model call function\n * @returns Promise resolving to the model response\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain request/handler types are dynamic\n async wrapModelCall<T>(request: any, handler: (req: any) => Promise<T>): Promise<T> {\n this.ensureAgentInstanceStarted();\n\n const modelName = this.extractModelName(request);\n const span = this.tracer.startSpan({\n name: 'langchain:llm-call',\n spanType: toLangchainSpanType(SpanType.LLM),\n inputs: {\n ...this.extractModelInputs(request),\n 'langchain.model.name': modelName,\n },\n });\n\n try {\n const response = await SpanContext.runAsync(span, () => handler(request));\n\n const outputs = this.extractModelOutputs(response);\n const tokenUsage = extractTokenUsage(response);\n\n this.tracer.endSpan(span, { outputs, tokenUsage: tokenUsage ?? undefined });\n return response;\n } catch (error) {\n this.tracer.endSpan(span, { error: error as Error });\n throw error;\n }\n }\n\n /**\n * Wrap a tool call to trace tool executions\n *\n * @param request - Tool invocation request\n * @param handler - The actual tool call function\n * @returns Promise resolving to the tool response\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain request/handler types are dynamic\n async wrapToolCall<T>(request: any, handler: (req: any) => Promise<T>): Promise<T> {\n this.ensureAgentInstanceStarted();\n\n const toolName = this.extractToolName(request);\n const span = this.tracer.startSpan({\n name: 'langchain:tool-call',\n spanType: toLangchainSpanType(SpanType.TOOL),\n inputs: {\n ...this.extractToolInputs(request),\n 'langchain.tool.name': toolName,\n },\n });\n\n try {\n const response = await SpanContext.runAsync(span, () => handler(request));\n\n this.tracer.endSpan(span, {\n outputs: this.extractToolOutputs(response),\n });\n return response;\n } catch (error) {\n this.tracer.endSpan(span, { error: error as Error });\n throw error;\n }\n }\n\n /**\n * Extract model name from request\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain request structure is dynamic\n private extractModelName(request: any): string {\n const candidate = request?.model ?? request?.modelName;\n\n if (typeof candidate === 'string') {\n return candidate;\n }\n\n if (candidate && typeof candidate === 'object') {\n const modelObject = candidate as Record<string, unknown>;\n if (\n Array.isArray(modelObject.id) &&\n modelObject.id.every((item) => typeof item === 'string')\n ) {\n return (modelObject.id as string[]).join('.');\n }\n\n if (typeof modelObject.modelName === 'string') {\n return modelObject.modelName;\n }\n\n if (typeof modelObject.name === 'string') {\n return modelObject.name;\n }\n }\n\n return 'unknown';\n }\n\n /**\n * Extract model inputs from request\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain request structure is dynamic\n private extractModelInputs(request: any): Record<string, unknown> {\n const messages = request?.messages ?? [];\n return { messages: serializeValue(messages.slice(-3)) };\n }\n\n /**\n * Extract model outputs from response\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain response structure is dynamic\n private extractModelOutputs(response: any): Record<string, unknown> {\n const content = response?.content ?? response?.text ?? '';\n return { content: serializeValue(content) };\n }\n\n /**\n * Extract tool name from request\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain request structure is dynamic\n private extractToolName(request: any): string {\n return request?.name ?? request?.tool ?? 'unknown';\n }\n\n /**\n * Extract tool inputs from request\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain request structure is dynamic\n private extractToolInputs(request: any): Record<string, unknown> {\n return { input: request?.input ?? request?.args ?? {} };\n }\n\n /**\n * Extract tool outputs from response\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain response structure is dynamic\n private extractToolOutputs(response: any): Record<string, unknown> {\n return { output: response?.output ?? response };\n }\n\n private ensureAgentInstanceStarted(): void {\n if (this.agentInstanceStarted) {\n return;\n }\n\n try {\n this.agentManager.startInstance(this.agentInfo);\n this.agentInstanceStarted = true;\n } catch (error) {\n logger.error('Failed to start agent instance:', error);\n }\n }\n\n private finishAgentInstance(): void {\n if (!this.agentInstanceStarted) {\n return;\n }\n\n try {\n this.agentManager.finishInstance();\n this.agentInstanceStarted = false;\n } catch (error) {\n logger.error('Failed to finish agent instance:', error);\n }\n }\n}\n",
|
|
8
8
|
"import type { TokenUsage } from '@prefactor/core';\n\n/**\n * Extract token usage information from LLM responses.\n *\n * Handles multiple response formats from different LLM providers and LangChain versions.\n *\n * @param response - The LLM response object\n * @returns TokenUsage object or null if no usage data found\n *\n * @example\n * ```typescript\n * const response = await model.invoke(messages);\n * const tokenUsage = extractTokenUsage(response);\n * if (tokenUsage) {\n * console.log(`Tokens used: ${tokenUsage.totalTokens}`);\n * }\n * ```\n */\n// biome-ignore lint/suspicious/noExplicitAny: LLM response structure varies by provider\nexport function extractTokenUsage(response: any): TokenUsage | null {\n try {\n // Try token_usage field (common format)\n const tokenUsage = response?.token_usage ?? response?.usage;\n if (tokenUsage) {\n return {\n promptTokens: tokenUsage.prompt_tokens ?? 0,\n completionTokens: tokenUsage.completion_tokens ?? 0,\n totalTokens: tokenUsage.total_tokens ?? 0,\n };\n }\n\n // Try usage_metadata field (LangChain format)\n const usageMetadata = response?.usage_metadata;\n if (usageMetadata) {\n return {\n promptTokens: usageMetadata.input_tokens ?? 0,\n completionTokens: usageMetadata.output_tokens ?? 0,\n totalTokens: usageMetadata.total_tokens ?? 0,\n };\n }\n\n // Try response_metadata.token_usage (nested format)\n const responseMetadata = response?.response_metadata;\n if (responseMetadata?.token_usage) {\n return {\n promptTokens: responseMetadata.token_usage.prompt_tokens ?? 0,\n completionTokens: responseMetadata.token_usage.completion_tokens ?? 0,\n totalTokens: responseMetadata.token_usage.total_tokens ?? 0,\n };\n }\n\n return null;\n } catch {\n return null;\n }\n}\n"
|
|
9
9
|
],
|
|
10
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWO,IARP;;;ACQO,IAXP;AAYuD,IAAvD;;;
|
|
11
|
-
"debugId": "
|
|
10
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWO,IARP;;;ACQO,IAXP;AAYuD,IAAvD;;;ACJO,IARP;;;ACoBO,SAAS,iBAAiB,CAAC,UAAkC;AAAA,EAClE,IAAI;AAAA,IAEF,MAAM,aAAa,UAAU,eAAe,UAAU;AAAA,IACtD,IAAI,YAAY;AAAA,MACd,OAAO;AAAA,QACL,cAAc,WAAW,iBAAiB;AAAA,QAC1C,kBAAkB,WAAW,qBAAqB;AAAA,QAClD,aAAa,WAAW,gBAAgB;AAAA,MAC1C;AAAA,IACF;AAAA,IAGA,MAAM,gBAAgB,UAAU;AAAA,IAChC,IAAI,eAAe;AAAA,MACjB,OAAO;AAAA,QACL,cAAc,cAAc,gBAAgB;AAAA,QAC5C,kBAAkB,cAAc,iBAAiB;AAAA,QACjD,aAAa,cAAc,gBAAgB;AAAA,MAC7C;AAAA,IACF;AAAA,IAGA,MAAM,mBAAmB,UAAU;AAAA,IACnC,IAAI,kBAAkB,aAAa;AAAA,MACjC,OAAO;AAAA,QACL,cAAc,iBAAiB,YAAY,iBAAiB;AAAA,QAC5D,kBAAkB,iBAAiB,YAAY,qBAAqB;AAAA,QACpE,aAAa,iBAAiB,YAAY,gBAAgB;AAAA,MAC5D;AAAA,IACF;AAAA,IAEA,OAAO;AAAA,IACP,MAAM;AAAA,IACN,OAAO;AAAA;AAAA;;;AD3CX,IAAM,sBAAsB,mCAAuB,WAAW;AAC9D,IAAM,SAAS,sBAAU,YAAY;AAAA;AA2B9B,MAAM,oBAAoB;AAAA,EAIrB;AAA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AC9B,QAAQ,MAAM,wCAAwC,KAAK;AAAA,GAC5D;AAAA,CACF;",
|
|
11
|
+
"debugId": "F4D712943DD18B9A64756E2164756E21",
|
|
12
12
|
"names": []
|
|
13
13
|
}
|
package/dist/index.js
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
configureLogging,
|
|
11
11
|
createConfig,
|
|
12
12
|
createCore,
|
|
13
|
-
getLogger,
|
|
13
|
+
getLogger as getLogger2,
|
|
14
14
|
registerShutdownHandler,
|
|
15
15
|
shutdown as shutdownCore,
|
|
16
16
|
withSpan as withCoreSpan
|
|
@@ -19,6 +19,8 @@ import { createMiddleware } from "langchain";
|
|
|
19
19
|
|
|
20
20
|
// packages/langchain/src/middleware.ts
|
|
21
21
|
import {
|
|
22
|
+
createSpanTypePrefixer,
|
|
23
|
+
getLogger,
|
|
22
24
|
SpanContext,
|
|
23
25
|
SpanType,
|
|
24
26
|
serializeValue
|
|
@@ -58,53 +60,39 @@ function extractTokenUsage(response) {
|
|
|
58
60
|
}
|
|
59
61
|
|
|
60
62
|
// packages/langchain/src/middleware.ts
|
|
63
|
+
var toLangchainSpanType = createSpanTypePrefixer("langchain");
|
|
64
|
+
var logger = getLogger("middleware");
|
|
65
|
+
|
|
61
66
|
class PrefactorMiddleware {
|
|
62
67
|
tracer;
|
|
63
68
|
agentManager;
|
|
64
69
|
agentInfo;
|
|
65
|
-
|
|
70
|
+
agentInstanceStarted = false;
|
|
66
71
|
constructor(tracer, agentManager, agentInfo) {
|
|
67
72
|
this.tracer = tracer;
|
|
68
73
|
this.agentManager = agentManager;
|
|
69
74
|
this.agentInfo = agentInfo;
|
|
70
75
|
}
|
|
71
76
|
async beforeAgent(state) {
|
|
72
|
-
|
|
73
|
-
this.agentManager.startInstance(this.agentInfo);
|
|
74
|
-
const span = this.tracer.startSpan({
|
|
75
|
-
name: "langchain:agent",
|
|
76
|
-
spanType: `langchain:${SpanType.AGENT}`,
|
|
77
|
-
inputs: { messages: serializeValue(messages.slice(-3)) }
|
|
78
|
-
});
|
|
79
|
-
this.rootSpan = span;
|
|
80
|
-
SpanContext.enter(span);
|
|
77
|
+
this.ensureAgentInstanceStarted();
|
|
81
78
|
}
|
|
82
|
-
async afterAgent(state) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
const messages = state?.messages ?? [];
|
|
87
|
-
this.tracer.endSpan(this.rootSpan, {
|
|
88
|
-
outputs: { messages: serializeValue(messages.slice(-3)) }
|
|
89
|
-
});
|
|
90
|
-
this.agentManager.finishInstance();
|
|
91
|
-
SpanContext.exit();
|
|
92
|
-
this.rootSpan = null;
|
|
79
|
+
async afterAgent(state) {}
|
|
80
|
+
shutdown() {
|
|
81
|
+
this.finishAgentInstance();
|
|
93
82
|
}
|
|
94
83
|
async wrapModelCall(request, handler) {
|
|
84
|
+
this.ensureAgentInstanceStarted();
|
|
95
85
|
const modelName = this.extractModelName(request);
|
|
96
86
|
const span = this.tracer.startSpan({
|
|
97
87
|
name: "langchain:llm-call",
|
|
98
|
-
spanType:
|
|
88
|
+
spanType: toLangchainSpanType(SpanType.LLM),
|
|
99
89
|
inputs: {
|
|
100
90
|
...this.extractModelInputs(request),
|
|
101
91
|
"langchain.model.name": modelName
|
|
102
92
|
}
|
|
103
93
|
});
|
|
104
94
|
try {
|
|
105
|
-
const response = await SpanContext.runAsync(span,
|
|
106
|
-
return handler(request);
|
|
107
|
-
});
|
|
95
|
+
const response = await SpanContext.runAsync(span, () => handler(request));
|
|
108
96
|
const outputs = this.extractModelOutputs(response);
|
|
109
97
|
const tokenUsage = extractTokenUsage(response);
|
|
110
98
|
this.tracer.endSpan(span, { outputs, tokenUsage: tokenUsage ?? undefined });
|
|
@@ -115,19 +103,18 @@ class PrefactorMiddleware {
|
|
|
115
103
|
}
|
|
116
104
|
}
|
|
117
105
|
async wrapToolCall(request, handler) {
|
|
106
|
+
this.ensureAgentInstanceStarted();
|
|
118
107
|
const toolName = this.extractToolName(request);
|
|
119
108
|
const span = this.tracer.startSpan({
|
|
120
109
|
name: "langchain:tool-call",
|
|
121
|
-
spanType:
|
|
110
|
+
spanType: toLangchainSpanType(SpanType.TOOL),
|
|
122
111
|
inputs: {
|
|
123
112
|
...this.extractToolInputs(request),
|
|
124
113
|
"langchain.tool.name": toolName
|
|
125
114
|
}
|
|
126
115
|
});
|
|
127
116
|
try {
|
|
128
|
-
const response = await SpanContext.runAsync(span,
|
|
129
|
-
return handler(request);
|
|
130
|
-
});
|
|
117
|
+
const response = await SpanContext.runAsync(span, () => handler(request));
|
|
131
118
|
this.tracer.endSpan(span, {
|
|
132
119
|
outputs: this.extractToolOutputs(response)
|
|
133
120
|
});
|
|
@@ -173,10 +160,32 @@ class PrefactorMiddleware {
|
|
|
173
160
|
extractToolOutputs(response) {
|
|
174
161
|
return { output: response?.output ?? response };
|
|
175
162
|
}
|
|
163
|
+
ensureAgentInstanceStarted() {
|
|
164
|
+
if (this.agentInstanceStarted) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
try {
|
|
168
|
+
this.agentManager.startInstance(this.agentInfo);
|
|
169
|
+
this.agentInstanceStarted = true;
|
|
170
|
+
} catch (error) {
|
|
171
|
+
logger.error("Failed to start agent instance:", error);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
finishAgentInstance() {
|
|
175
|
+
if (!this.agentInstanceStarted) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
try {
|
|
179
|
+
this.agentManager.finishInstance();
|
|
180
|
+
this.agentInstanceStarted = false;
|
|
181
|
+
} catch (error) {
|
|
182
|
+
logger.error("Failed to finish agent instance:", error);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
176
185
|
}
|
|
177
186
|
|
|
178
187
|
// packages/langchain/src/init.ts
|
|
179
|
-
var
|
|
188
|
+
var logger2 = getLogger2("init");
|
|
180
189
|
var DEFAULT_LANGCHAIN_AGENT_SCHEMA = {
|
|
181
190
|
external_identifier: "langchain-schema",
|
|
182
191
|
span_schemas: {
|
|
@@ -184,18 +193,27 @@ var DEFAULT_LANGCHAIN_AGENT_SCHEMA = {
|
|
|
184
193
|
"langchain:llm": { type: "object", additionalProperties: true },
|
|
185
194
|
"langchain:tool": { type: "object", additionalProperties: true },
|
|
186
195
|
"langchain:chain": { type: "object", additionalProperties: true }
|
|
196
|
+
},
|
|
197
|
+
span_result_schemas: {
|
|
198
|
+
"langchain:agent": { type: "object", additionalProperties: true },
|
|
199
|
+
"langchain:llm": { type: "object", additionalProperties: true },
|
|
200
|
+
"langchain:tool": { type: "object", additionalProperties: true },
|
|
201
|
+
"langchain:chain": { type: "object", additionalProperties: true }
|
|
187
202
|
}
|
|
188
203
|
};
|
|
189
204
|
var globalCore = null;
|
|
190
205
|
var globalTracer = null;
|
|
191
206
|
var globalMiddleware = null;
|
|
207
|
+
var globalPrefactorMiddleware = null;
|
|
192
208
|
registerShutdownHandler("prefactor-langchain", () => {
|
|
209
|
+
globalPrefactorMiddleware?.shutdown();
|
|
193
210
|
if (globalCore) {
|
|
194
|
-
|
|
211
|
+
logger2.info("Shutting down Prefactor SDK");
|
|
195
212
|
}
|
|
196
213
|
globalCore = null;
|
|
197
214
|
globalTracer = null;
|
|
198
215
|
globalMiddleware = null;
|
|
216
|
+
globalPrefactorMiddleware = null;
|
|
199
217
|
});
|
|
200
218
|
function init(config) {
|
|
201
219
|
configureLogging();
|
|
@@ -262,6 +280,7 @@ function init(config) {
|
|
|
262
280
|
}
|
|
263
281
|
});
|
|
264
282
|
globalMiddleware = middleware;
|
|
283
|
+
globalPrefactorMiddleware = prefactorMiddleware;
|
|
265
284
|
return middleware;
|
|
266
285
|
}
|
|
267
286
|
function getTracer() {
|
|
@@ -289,4 +308,4 @@ export {
|
|
|
289
308
|
PrefactorMiddleware
|
|
290
309
|
};
|
|
291
310
|
|
|
292
|
-
//# debugId=
|
|
311
|
+
//# debugId=2BEAA5BD0BE2820164756E2164756E21
|
package/dist/index.js.map
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
"sources": ["../src/index.ts", "../src/init.ts", "../src/middleware.ts", "../src/metadata-extractor.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
5
|
"// Main entry points\n\n// Convenience re-exports from core\nexport {\n type Config,\n type CoreRuntime,\n type HttpTransportConfig,\n type Span,\n SpanStatus,\n SpanType,\n shutdown,\n} from '@prefactor/core';\nexport { getTracer, init, withSpan } from './init.js';\nexport { extractTokenUsage } from './metadata-extractor.js';\n// Middleware\nexport { PrefactorMiddleware } from './middleware.js';\n",
|
|
6
|
-
"import {\n type Config,\n type CoreRuntime,\n configureLogging,\n createConfig,\n createCore,\n getLogger,\n registerShutdownHandler,\n shutdown as shutdownCore,\n type Tracer,\n withSpan as withCoreSpan,\n} from '@prefactor/core';\nimport { type AgentMiddleware, createMiddleware } from 'langchain';\nimport { PrefactorMiddleware } from './middleware.js';\n\nconst logger = getLogger('init');\n\nconst DEFAULT_LANGCHAIN_AGENT_SCHEMA = {\n external_identifier: 'langchain-schema',\n span_schemas: {\n 'langchain:agent': { type: 'object', additionalProperties: true },\n 'langchain:llm': { type: 'object', additionalProperties: true },\n 'langchain:tool': { type: 'object', additionalProperties: true },\n 'langchain:chain': { type: 'object', additionalProperties: true },\n },\n} as const;\n\nlet globalCore: CoreRuntime | null = null;\nlet globalTracer: Tracer | null = null;\nlet globalMiddleware: AgentMiddleware | null = null;\n\nregisterShutdownHandler('prefactor-langchain', () => {\n if (globalCore) {\n logger.info('Shutting down Prefactor SDK');\n }\n\n globalCore = null;\n globalTracer = null;\n globalMiddleware = null;\n});\n\nexport type ManualSpanOptions = {\n name: string;\n spanType: string;\n inputs: Record<string, unknown>;\n metadata?: Record<string, unknown>;\n};\n\n/**\n * Initialize the Prefactor SDK and return middleware for LangChain.js\n *\n * This is the main entry point for the SDK. Call this function to create a middleware\n * instance that you can pass to your LangChain.js agents.\n *\n * @param config - Optional configuration object\n * @returns PrefactorMiddleware instance to use with LangChain.js agents\n *\n * @example\n * ```typescript\n * import { init } from '@prefactor/langchain';\n * import { createAgent } from 'langchain';\n *\n * // Initialize with HTTP transport\n * const middleware = init({\n * transportType: 'http',\n * httpConfig: {\n * apiUrl: 'https://api.prefactor.ai',\n * apiToken: process.env.PREFACTOR_API_TOKEN!,\n * agentIdentifier: 'my-langchain-agent',\n * },\n * });\n *\n * // Or configure HTTP transport\n * const middleware = init({\n * transportType: 'http',\n * httpConfig: {\n * apiUrl: 'https://api.prefactor.ai',\n * apiToken: process.env.PREFACTOR_API_TOKEN!,\n * agentIdentifier: 'my-langchain-agent', // Required\n * agentId: 'legacy-agent-id', // Optional legacy identifier\n * }\n * });\n *\n * const agent = createAgent({\n * model: 'claude-sonnet-4-5-20250929',\n * tools: [myTool],\n * middleware: [middleware],\n * });\n * ```\n */\nexport function init(config?: Partial<Config>): AgentMiddleware {\n configureLogging();\n\n let configWithHttp = config;\n const transportType = config?.transportType ?? process.env.PREFACTOR_TRANSPORT ?? 'http';\n\n if (transportType === 'http' && !config?.httpConfig) {\n const apiUrl = process.env.PREFACTOR_API_URL;\n const apiToken = process.env.PREFACTOR_API_TOKEN;\n\n if (!apiUrl || !apiToken) {\n throw new Error(\n 'HTTP transport requires PREFACTOR_API_URL and PREFACTOR_API_TOKEN environment variables, ' +\n 'or httpConfig to be provided in configuration'\n );\n }\n\n configWithHttp = {\n ...config,\n transportType: 'http',\n httpConfig: {\n apiUrl,\n apiToken,\n agentId: process.env.PREFACTOR_AGENT_ID,\n agentName: process.env.PREFACTOR_AGENT_NAME,\n agentIdentifier: process.env.PREFACTOR_AGENT_IDENTIFIER || '1.0.0',\n agentSchema: DEFAULT_LANGCHAIN_AGENT_SCHEMA,\n },\n };\n } else if (transportType === 'http' && config?.httpConfig && !config.httpConfig.agentSchema) {\n configWithHttp = {\n ...config,\n httpConfig: {\n ...config.httpConfig,\n agentSchema: DEFAULT_LANGCHAIN_AGENT_SCHEMA,\n },\n };\n }\n\n const finalConfig = createConfig(configWithHttp);\n\n if (globalMiddleware !== null) {\n return globalMiddleware;\n }\n\n const core = createCore(finalConfig);\n globalCore = core;\n globalTracer = core.tracer;\n\n const httpConfig = finalConfig.httpConfig;\n if (httpConfig?.agentSchema) {\n core.agentManager.registerSchema(httpConfig.agentSchema);\n }\n\n const agentInfo = finalConfig.httpConfig\n ? {\n agentId: finalConfig.httpConfig.agentId,\n agentIdentifier: finalConfig.httpConfig.agentIdentifier,\n agentName: finalConfig.httpConfig.agentName,\n agentDescription: finalConfig.httpConfig.agentDescription,\n }\n : undefined;\n\n const prefactorMiddleware = new PrefactorMiddleware(core.tracer, core.agentManager, agentInfo);\n\n const middleware = createMiddleware({\n name: 'prefactor',\n // biome-ignore lint/suspicious/noExplicitAny: LangChain middleware hooks use dynamic types\n wrapModelCall: async (request: any, handler: any) => {\n return prefactorMiddleware.wrapModelCall(request, handler);\n },\n // biome-ignore lint/suspicious/noExplicitAny: LangChain middleware hooks use dynamic types\n wrapToolCall: async (request: any, handler: any) => {\n return prefactorMiddleware.wrapToolCall(request, handler);\n },\n // biome-ignore lint/suspicious/noExplicitAny: LangChain middleware hooks use dynamic types\n beforeAgent: async (state: any) => {\n await prefactorMiddleware.beforeAgent(state);\n },\n // biome-ignore lint/suspicious/noExplicitAny: LangChain middleware hooks use dynamic types\n afterAgent: async (state: any) => {\n await prefactorMiddleware.afterAgent(state);\n },\n });\n\n globalMiddleware = middleware;\n return middleware;\n}\n\n/**\n * Get the current tracer instance.\n *\n * If no tracer has been created yet, this will call init() with default configuration.\n *\n * @returns Tracer instance\n *\n * @example\n * ```typescript\n * import { getTracer } from '@prefactor/langchain';\n *\n * const tracer = getTracer();\n * const span = tracer.startSpan({\n * name: 'custom-operation',\n * spanType: SpanType.TOOL,\n * inputs: { data: 'example' }\n * });\n * ```\n */\nexport function getTracer(): Tracer {\n if (!globalTracer) {\n init();\n }\n // Safe because init() always sets globalTracer\n return globalTracer as Tracer;\n}\n\nexport async function withSpan<T>(\n options: ManualSpanOptions,\n fn: () => Promise<T> | T\n): Promise<T> {\n return withCoreSpan(options, fn);\n}\n\nexport { shutdownCore as shutdown };\n\n// Automatic shutdown on process exit\nprocess.on('beforeExit', () => {\n shutdownCore().catch((error) => {\n console.error('Error during Prefactor SDK shutdown:', error);\n });\n});\n",
|
|
7
|
-
"import {\n type AgentInstanceManager,\n SpanContext,\n SpanType,\n serializeValue,\n type Tracer,\n} from '@prefactor/core';\nimport { extractTokenUsage } from './metadata-extractor.js';\n\n/**\n * Prefactor middleware for LangChain.js agents.\n *\n * This middleware automatically traces LLM calls, tool executions, and agent workflows.\n * It integrates with LangChain.js middleware API to provide transparent instrumentation.\n *\n * Features:\n * - Automatic parent-child span relationships via context propagation\n * - Token usage extraction for LLM calls\n * - Error tracking and debugging\n * - Zero-overhead instrumentation (graceful failure)\n *\n * @example\n * ```typescript\n * import { init } from '@prefactor/langchain';\n * import { createReactAgent } from '@langchain/langgraph/prebuilt';\n *\n * const middleware = init();\n * const agent = createReactAgent({\n * llm: model,\n * tools: [myTool],\n * middleware: [middleware],\n * });\n * ```\n */\nexport class PrefactorMiddleware {\n private
|
|
6
|
+
"import {\n type Config,\n type CoreRuntime,\n configureLogging,\n createConfig,\n createCore,\n getLogger,\n registerShutdownHandler,\n shutdown as shutdownCore,\n type Tracer,\n withSpan as withCoreSpan,\n} from '@prefactor/core';\nimport { type AgentMiddleware, createMiddleware } from 'langchain';\nimport { PrefactorMiddleware } from './middleware.js';\n\nconst logger = getLogger('init');\n\nconst DEFAULT_LANGCHAIN_AGENT_SCHEMA = {\n external_identifier: 'langchain-schema',\n span_schemas: {\n 'langchain:agent': { type: 'object', additionalProperties: true },\n 'langchain:llm': { type: 'object', additionalProperties: true },\n 'langchain:tool': { type: 'object', additionalProperties: true },\n 'langchain:chain': { type: 'object', additionalProperties: true },\n },\n span_result_schemas: {\n 'langchain:agent': { type: 'object', additionalProperties: true },\n 'langchain:llm': { type: 'object', additionalProperties: true },\n 'langchain:tool': { type: 'object', additionalProperties: true },\n 'langchain:chain': { type: 'object', additionalProperties: true },\n },\n} as const;\n\nlet globalCore: CoreRuntime | null = null;\nlet globalTracer: Tracer | null = null;\nlet globalMiddleware: AgentMiddleware | null = null;\nlet globalPrefactorMiddleware: PrefactorMiddleware | null = null;\n\nregisterShutdownHandler('prefactor-langchain', () => {\n globalPrefactorMiddleware?.shutdown();\n\n if (globalCore) {\n logger.info('Shutting down Prefactor SDK');\n }\n\n globalCore = null;\n globalTracer = null;\n globalMiddleware = null;\n globalPrefactorMiddleware = null;\n});\n\nexport type ManualSpanOptions = {\n name: string;\n spanType: string;\n inputs: Record<string, unknown>;\n metadata?: Record<string, unknown>;\n};\n\n/**\n * Initialize the Prefactor SDK and return middleware for LangChain.js\n *\n * This is the main entry point for the SDK. Call this function to create a middleware\n * instance that you can pass to your LangChain.js agents.\n *\n * @param config - Optional configuration object\n * @returns PrefactorMiddleware instance to use with LangChain.js agents\n *\n * @example\n * ```typescript\n * import { init } from '@prefactor/langchain';\n * import { createAgent } from 'langchain';\n *\n * // Initialize with HTTP transport\n * const middleware = init({\n * transportType: 'http',\n * httpConfig: {\n * apiUrl: 'https://api.prefactor.ai',\n * apiToken: process.env.PREFACTOR_API_TOKEN!,\n * agentIdentifier: 'my-langchain-agent',\n * },\n * });\n *\n * // Or configure HTTP transport\n * const middleware = init({\n * transportType: 'http',\n * httpConfig: {\n * apiUrl: 'https://api.prefactor.ai',\n * apiToken: process.env.PREFACTOR_API_TOKEN!,\n * agentIdentifier: 'my-langchain-agent', // Required\n * agentId: 'legacy-agent-id', // Optional legacy identifier\n * }\n * });\n *\n * const agent = createAgent({\n * model: 'claude-sonnet-4-5-20250929',\n * tools: [myTool],\n * middleware: [middleware],\n * });\n * ```\n */\nexport function init(config?: Partial<Config>): AgentMiddleware {\n configureLogging();\n\n let configWithHttp = config;\n const transportType = config?.transportType ?? process.env.PREFACTOR_TRANSPORT ?? 'http';\n\n if (transportType === 'http' && !config?.httpConfig) {\n const apiUrl = process.env.PREFACTOR_API_URL;\n const apiToken = process.env.PREFACTOR_API_TOKEN;\n\n if (!apiUrl || !apiToken) {\n throw new Error(\n 'HTTP transport requires PREFACTOR_API_URL and PREFACTOR_API_TOKEN environment variables, ' +\n 'or httpConfig to be provided in configuration'\n );\n }\n\n configWithHttp = {\n ...config,\n transportType: 'http',\n httpConfig: {\n apiUrl,\n apiToken,\n agentId: process.env.PREFACTOR_AGENT_ID,\n agentName: process.env.PREFACTOR_AGENT_NAME,\n agentIdentifier: process.env.PREFACTOR_AGENT_IDENTIFIER || '1.0.0',\n agentSchema: DEFAULT_LANGCHAIN_AGENT_SCHEMA,\n },\n };\n } else if (transportType === 'http' && config?.httpConfig && !config.httpConfig.agentSchema) {\n configWithHttp = {\n ...config,\n httpConfig: {\n ...config.httpConfig,\n agentSchema: DEFAULT_LANGCHAIN_AGENT_SCHEMA,\n },\n };\n }\n\n const finalConfig = createConfig(configWithHttp);\n\n if (globalMiddleware !== null) {\n return globalMiddleware;\n }\n\n const core = createCore(finalConfig);\n globalCore = core;\n globalTracer = core.tracer;\n\n const httpConfig = finalConfig.httpConfig;\n if (httpConfig?.agentSchema) {\n core.agentManager.registerSchema(httpConfig.agentSchema);\n }\n\n const agentInfo = finalConfig.httpConfig\n ? {\n agentId: finalConfig.httpConfig.agentId,\n agentIdentifier: finalConfig.httpConfig.agentIdentifier,\n agentName: finalConfig.httpConfig.agentName,\n agentDescription: finalConfig.httpConfig.agentDescription,\n }\n : undefined;\n\n const prefactorMiddleware = new PrefactorMiddleware(core.tracer, core.agentManager, agentInfo);\n\n const middleware = createMiddleware({\n name: 'prefactor',\n // biome-ignore lint/suspicious/noExplicitAny: LangChain middleware hooks use dynamic types\n wrapModelCall: async (request: any, handler: any) => {\n return prefactorMiddleware.wrapModelCall(request, handler);\n },\n // biome-ignore lint/suspicious/noExplicitAny: LangChain middleware hooks use dynamic types\n wrapToolCall: async (request: any, handler: any) => {\n return prefactorMiddleware.wrapToolCall(request, handler);\n },\n // biome-ignore lint/suspicious/noExplicitAny: LangChain middleware hooks use dynamic types\n beforeAgent: async (state: any) => {\n await prefactorMiddleware.beforeAgent(state);\n },\n // biome-ignore lint/suspicious/noExplicitAny: LangChain middleware hooks use dynamic types\n afterAgent: async (state: any) => {\n await prefactorMiddleware.afterAgent(state);\n },\n });\n\n globalMiddleware = middleware;\n globalPrefactorMiddleware = prefactorMiddleware;\n return middleware;\n}\n\n/**\n * Get the current tracer instance.\n *\n * If no tracer has been created yet, this will call init() with default configuration.\n *\n * @returns Tracer instance\n *\n * @example\n * ```typescript\n * import { getTracer } from '@prefactor/langchain';\n *\n * const tracer = getTracer();\n * const span = tracer.startSpan({\n * name: 'custom-operation',\n * spanType: SpanType.TOOL,\n * inputs: { data: 'example' }\n * });\n * ```\n */\nexport function getTracer(): Tracer {\n if (!globalTracer) {\n init();\n }\n // Safe because init() always sets globalTracer\n return globalTracer as Tracer;\n}\n\nexport async function withSpan<T>(\n options: ManualSpanOptions,\n fn: () => Promise<T> | T\n): Promise<T> {\n return withCoreSpan(options, fn);\n}\n\nexport { shutdownCore as shutdown };\n\n// Automatic shutdown on process exit\nprocess.on('beforeExit', () => {\n shutdownCore().catch((error) => {\n console.error('Error during Prefactor SDK shutdown:', error);\n });\n});\n",
|
|
7
|
+
"import {\n type AgentInstanceManager,\n createSpanTypePrefixer,\n getLogger,\n SpanContext,\n SpanType,\n serializeValue,\n type Tracer,\n} from '@prefactor/core';\nimport { extractTokenUsage } from './metadata-extractor.js';\n\nconst toLangchainSpanType = createSpanTypePrefixer('langchain');\nconst logger = getLogger('middleware');\n\n/**\n * Prefactor middleware for LangChain.js agents.\n *\n * This middleware automatically traces LLM calls, tool executions, and agent workflows.\n * It integrates with LangChain.js middleware API to provide transparent instrumentation.\n *\n * Features:\n * - Automatic parent-child span relationships via context propagation\n * - Token usage extraction for LLM calls\n * - Error tracking and debugging\n * - Zero-overhead instrumentation (graceful failure)\n *\n * @example\n * ```typescript\n * import { init } from '@prefactor/langchain';\n * import { createReactAgent } from '@langchain/langgraph/prebuilt';\n *\n * const middleware = init();\n * const agent = createReactAgent({\n * llm: model,\n * tools: [myTool],\n * middleware: [middleware],\n * });\n * ```\n */\nexport class PrefactorMiddleware {\n private agentInstanceStarted = false;\n\n constructor(\n private tracer: Tracer,\n private agentManager: AgentInstanceManager,\n private agentInfo?: Parameters<AgentInstanceManager['startInstance']>[0]\n ) {}\n\n /**\n * Called before agent execution starts\n *\n * @param state - Agent state containing messages\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain state can be any structure\n async beforeAgent(state: any): Promise<void> {\n this.ensureAgentInstanceStarted();\n }\n\n /**\n * Called after agent execution completes\n *\n * @param state - Agent state containing messages\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain state can be any structure\n async afterAgent(state: any): Promise<void> {\n // Root agent spans are intentionally not emitted.\n void state;\n }\n\n shutdown(): void {\n this.finishAgentInstance();\n }\n\n /**\n * Wrap a model call to trace LLM invocations\n *\n * @param request - Model invocation request\n * @param handler - The actual model call function\n * @returns Promise resolving to the model response\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain request/handler types are dynamic\n async wrapModelCall<T>(request: any, handler: (req: any) => Promise<T>): Promise<T> {\n this.ensureAgentInstanceStarted();\n\n const modelName = this.extractModelName(request);\n const span = this.tracer.startSpan({\n name: 'langchain:llm-call',\n spanType: toLangchainSpanType(SpanType.LLM),\n inputs: {\n ...this.extractModelInputs(request),\n 'langchain.model.name': modelName,\n },\n });\n\n try {\n const response = await SpanContext.runAsync(span, () => handler(request));\n\n const outputs = this.extractModelOutputs(response);\n const tokenUsage = extractTokenUsage(response);\n\n this.tracer.endSpan(span, { outputs, tokenUsage: tokenUsage ?? undefined });\n return response;\n } catch (error) {\n this.tracer.endSpan(span, { error: error as Error });\n throw error;\n }\n }\n\n /**\n * Wrap a tool call to trace tool executions\n *\n * @param request - Tool invocation request\n * @param handler - The actual tool call function\n * @returns Promise resolving to the tool response\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain request/handler types are dynamic\n async wrapToolCall<T>(request: any, handler: (req: any) => Promise<T>): Promise<T> {\n this.ensureAgentInstanceStarted();\n\n const toolName = this.extractToolName(request);\n const span = this.tracer.startSpan({\n name: 'langchain:tool-call',\n spanType: toLangchainSpanType(SpanType.TOOL),\n inputs: {\n ...this.extractToolInputs(request),\n 'langchain.tool.name': toolName,\n },\n });\n\n try {\n const response = await SpanContext.runAsync(span, () => handler(request));\n\n this.tracer.endSpan(span, {\n outputs: this.extractToolOutputs(response),\n });\n return response;\n } catch (error) {\n this.tracer.endSpan(span, { error: error as Error });\n throw error;\n }\n }\n\n /**\n * Extract model name from request\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain request structure is dynamic\n private extractModelName(request: any): string {\n const candidate = request?.model ?? request?.modelName;\n\n if (typeof candidate === 'string') {\n return candidate;\n }\n\n if (candidate && typeof candidate === 'object') {\n const modelObject = candidate as Record<string, unknown>;\n if (\n Array.isArray(modelObject.id) &&\n modelObject.id.every((item) => typeof item === 'string')\n ) {\n return (modelObject.id as string[]).join('.');\n }\n\n if (typeof modelObject.modelName === 'string') {\n return modelObject.modelName;\n }\n\n if (typeof modelObject.name === 'string') {\n return modelObject.name;\n }\n }\n\n return 'unknown';\n }\n\n /**\n * Extract model inputs from request\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain request structure is dynamic\n private extractModelInputs(request: any): Record<string, unknown> {\n const messages = request?.messages ?? [];\n return { messages: serializeValue(messages.slice(-3)) };\n }\n\n /**\n * Extract model outputs from response\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain response structure is dynamic\n private extractModelOutputs(response: any): Record<string, unknown> {\n const content = response?.content ?? response?.text ?? '';\n return { content: serializeValue(content) };\n }\n\n /**\n * Extract tool name from request\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain request structure is dynamic\n private extractToolName(request: any): string {\n return request?.name ?? request?.tool ?? 'unknown';\n }\n\n /**\n * Extract tool inputs from request\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain request structure is dynamic\n private extractToolInputs(request: any): Record<string, unknown> {\n return { input: request?.input ?? request?.args ?? {} };\n }\n\n /**\n * Extract tool outputs from response\n */\n // biome-ignore lint/suspicious/noExplicitAny: LangChain response structure is dynamic\n private extractToolOutputs(response: any): Record<string, unknown> {\n return { output: response?.output ?? response };\n }\n\n private ensureAgentInstanceStarted(): void {\n if (this.agentInstanceStarted) {\n return;\n }\n\n try {\n this.agentManager.startInstance(this.agentInfo);\n this.agentInstanceStarted = true;\n } catch (error) {\n logger.error('Failed to start agent instance:', error);\n }\n }\n\n private finishAgentInstance(): void {\n if (!this.agentInstanceStarted) {\n return;\n }\n\n try {\n this.agentManager.finishInstance();\n this.agentInstanceStarted = false;\n } catch (error) {\n logger.error('Failed to finish agent instance:', error);\n }\n }\n}\n",
|
|
8
8
|
"import type { TokenUsage } from '@prefactor/core';\n\n/**\n * Extract token usage information from LLM responses.\n *\n * Handles multiple response formats from different LLM providers and LangChain versions.\n *\n * @param response - The LLM response object\n * @returns TokenUsage object or null if no usage data found\n *\n * @example\n * ```typescript\n * const response = await model.invoke(messages);\n * const tokenUsage = extractTokenUsage(response);\n * if (tokenUsage) {\n * console.log(`Tokens used: ${tokenUsage.totalTokens}`);\n * }\n * ```\n */\n// biome-ignore lint/suspicious/noExplicitAny: LLM response structure varies by provider\nexport function extractTokenUsage(response: any): TokenUsage | null {\n try {\n // Try token_usage field (common format)\n const tokenUsage = response?.token_usage ?? response?.usage;\n if (tokenUsage) {\n return {\n promptTokens: tokenUsage.prompt_tokens ?? 0,\n completionTokens: tokenUsage.completion_tokens ?? 0,\n totalTokens: tokenUsage.total_tokens ?? 0,\n };\n }\n\n // Try usage_metadata field (LangChain format)\n const usageMetadata = response?.usage_metadata;\n if (usageMetadata) {\n return {\n promptTokens: usageMetadata.input_tokens ?? 0,\n completionTokens: usageMetadata.output_tokens ?? 0,\n totalTokens: usageMetadata.total_tokens ?? 0,\n };\n }\n\n // Try response_metadata.token_usage (nested format)\n const responseMetadata = response?.response_metadata;\n if (responseMetadata?.token_usage) {\n return {\n promptTokens: responseMetadata.token_usage.prompt_tokens ?? 0,\n completionTokens: responseMetadata.token_usage.completion_tokens ?? 0,\n totalTokens: responseMetadata.token_usage.total_tokens ?? 0,\n };\n }\n\n return null;\n } catch {\n return null;\n }\n}\n"
|
|
9
9
|
],
|
|
10
|
-
"mappings": ";AAGA;AAAA;AAAA,cAME;AAAA;AAAA;;;ACTF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,
|
|
11
|
-
"debugId": "
|
|
10
|
+
"mappings": ";AAGA;AAAA;AAAA,cAME;AAAA;AAAA;;;ACTF;AAAA;AAAA;AAAA;AAAA,eAME;AAAA;AAAA,cAEA;AAAA,cAEA;AAAA;AAEF;;;ACZA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACoBO,SAAS,iBAAiB,CAAC,UAAkC;AAAA,EAClE,IAAI;AAAA,IAEF,MAAM,aAAa,UAAU,eAAe,UAAU;AAAA,IACtD,IAAI,YAAY;AAAA,MACd,OAAO;AAAA,QACL,cAAc,WAAW,iBAAiB;AAAA,QAC1C,kBAAkB,WAAW,qBAAqB;AAAA,QAClD,aAAa,WAAW,gBAAgB;AAAA,MAC1C;AAAA,IACF;AAAA,IAGA,MAAM,gBAAgB,UAAU;AAAA,IAChC,IAAI,eAAe;AAAA,MACjB,OAAO;AAAA,QACL,cAAc,cAAc,gBAAgB;AAAA,QAC5C,kBAAkB,cAAc,iBAAiB;AAAA,QACjD,aAAa,cAAc,gBAAgB;AAAA,MAC7C;AAAA,IACF;AAAA,IAGA,MAAM,mBAAmB,UAAU;AAAA,IACnC,IAAI,kBAAkB,aAAa;AAAA,MACjC,OAAO;AAAA,QACL,cAAc,iBAAiB,YAAY,iBAAiB;AAAA,QAC5D,kBAAkB,iBAAiB,YAAY,qBAAqB;AAAA,QACpE,aAAa,iBAAiB,YAAY,gBAAgB;AAAA,MAC5D;AAAA,IACF;AAAA,IAEA,OAAO;AAAA,IACP,MAAM;AAAA,IACN,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,EACZ,OAAO,aAAa,SAAS,EAAE;AAAA;AAMjC,QAAQ,GAAG,cAAc,MAAM;AAAA,EAC7B,aAAa,EAAE,MAAM,CAAC,UAAU;AAAA,IAC9B,QAAQ,MAAM,wCAAwC,KAAK;AAAA,GAC5D;AAAA,CACF;",
|
|
11
|
+
"debugId": "2BEAA5BD0BE2820164756E2164756E21",
|
|
12
12
|
"names": []
|
|
13
13
|
}
|
package/dist/init.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../src/init.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,MAAM,EAOX,QAAQ,IAAI,YAAY,EACxB,KAAK,MAAM,EAEZ,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,KAAK,eAAe,EAAoB,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../src/init.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,MAAM,EAOX,QAAQ,IAAI,YAAY,EACxB,KAAK,MAAM,EAEZ,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,KAAK,eAAe,EAAoB,MAAM,WAAW,CAAC;AAuCnE,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,wBAAgB,IAAI,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,GAAG,eAAe,CAwF9D;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,SAAS,IAAI,MAAM,CAMlC;AAED,wBAAsB,QAAQ,CAAC,CAAC,EAC9B,OAAO,EAAE,iBAAiB,EAC1B,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,GACvB,OAAO,CAAC,CAAC,CAAC,CAEZ;AAED,OAAO,EAAE,YAAY,IAAI,QAAQ,EAAE,CAAC"}
|
package/dist/init.js
CHANGED
|
@@ -10,17 +10,26 @@ const DEFAULT_LANGCHAIN_AGENT_SCHEMA = {
|
|
|
10
10
|
'langchain:tool': { type: 'object', additionalProperties: true },
|
|
11
11
|
'langchain:chain': { type: 'object', additionalProperties: true },
|
|
12
12
|
},
|
|
13
|
+
span_result_schemas: {
|
|
14
|
+
'langchain:agent': { type: 'object', additionalProperties: true },
|
|
15
|
+
'langchain:llm': { type: 'object', additionalProperties: true },
|
|
16
|
+
'langchain:tool': { type: 'object', additionalProperties: true },
|
|
17
|
+
'langchain:chain': { type: 'object', additionalProperties: true },
|
|
18
|
+
},
|
|
13
19
|
};
|
|
14
20
|
let globalCore = null;
|
|
15
21
|
let globalTracer = null;
|
|
16
22
|
let globalMiddleware = null;
|
|
23
|
+
let globalPrefactorMiddleware = null;
|
|
17
24
|
registerShutdownHandler('prefactor-langchain', () => {
|
|
25
|
+
globalPrefactorMiddleware?.shutdown();
|
|
18
26
|
if (globalCore) {
|
|
19
27
|
logger.info('Shutting down Prefactor SDK');
|
|
20
28
|
}
|
|
21
29
|
globalCore = null;
|
|
22
30
|
globalTracer = null;
|
|
23
31
|
globalMiddleware = null;
|
|
32
|
+
globalPrefactorMiddleware = null;
|
|
24
33
|
});
|
|
25
34
|
/**
|
|
26
35
|
* Initialize the Prefactor SDK and return middleware for LangChain.js
|
|
@@ -137,6 +146,7 @@ export function init(config) {
|
|
|
137
146
|
},
|
|
138
147
|
});
|
|
139
148
|
globalMiddleware = middleware;
|
|
149
|
+
globalPrefactorMiddleware = prefactorMiddleware;
|
|
140
150
|
return middleware;
|
|
141
151
|
}
|
|
142
152
|
/**
|
package/dist/init.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init.js","sourceRoot":"","sources":["../src/init.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,gBAAgB,EAChB,YAAY,EACZ,UAAU,EACV,SAAS,EACT,uBAAuB,EACvB,QAAQ,IAAI,YAAY,EAExB,QAAQ,IAAI,YAAY,GACzB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAwB,gBAAgB,EAAE,MAAM,WAAW,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC;AAEjC,MAAM,8BAA8B,GAAG;IACrC,mBAAmB,EAAE,kBAAkB;IACvC,YAAY,EAAE;QACZ,iBAAiB,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,oBAAoB,EAAE,IAAI,EAAE;QACjE,eAAe,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,oBAAoB,EAAE,IAAI,EAAE;QAC/D,gBAAgB,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,oBAAoB,EAAE,IAAI,EAAE;QAChE,iBAAiB,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,oBAAoB,EAAE,IAAI,EAAE;KAClE;CACO,CAAC;AAEX,IAAI,UAAU,GAAuB,IAAI,CAAC;AAC1C,IAAI,YAAY,GAAkB,IAAI,CAAC;AACvC,IAAI,gBAAgB,GAA2B,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"init.js","sourceRoot":"","sources":["../src/init.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,gBAAgB,EAChB,YAAY,EACZ,UAAU,EACV,SAAS,EACT,uBAAuB,EACvB,QAAQ,IAAI,YAAY,EAExB,QAAQ,IAAI,YAAY,GACzB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAwB,gBAAgB,EAAE,MAAM,WAAW,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,i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FAA2F;QAC3F,WAAW,EAAE,KAAK,EAAE,KAAU,EAAE,EAAE;YAChC,MAAM,mBAAmB,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;QAC/C,CAAC;QACD,2FAA2F;QAC3F,UAAU,EAAE,KAAK,EAAE,KAAU,EAAE,EAAE;YAC/B,MAAM,mBAAmB,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;QAC9C,CAAC;KACF,CAAC,CAAC;IAEH,gBAAgB,GAAG,UAAU,CAAC;IAC9B,yBAAyB,GAAG,mBAAmB,CAAC;IAChD,OAAO,UAAU,CAAC;AACpB,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,SAAS;IACvB,IAAI,CAAC,YAAY,EAAE,CAAC;QAClB,IAAI,EAAE,CAAC;IACT,CAAC;IACD,+CAA+C;IAC/C,OAAO,YAAsB,CAAC;AAChC,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,QAAQ,CAC5B,OAA0B,EAC1B,EAAwB;IAExB,OAAO,YAAY,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;AACnC,CAAC;AAED,OAAO,EAAE,YAAY,IAAI,QAAQ,EAAE,CAAC;AAEpC,qCAAqC;AACrC,OAAO,CAAC,EAAE,CAAC,YAAY,EAAE,GAAG,EAAE;IAC5B,YAAY,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;QAC7B,OAAO,CAAC,KAAK,CAAC,sCAAsC,EAAE,KAAK,CAAC,CAAC;IAC/D,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/dist/middleware.d.ts
CHANGED
|
@@ -28,7 +28,7 @@ export declare class PrefactorMiddleware {
|
|
|
28
28
|
private tracer;
|
|
29
29
|
private agentManager;
|
|
30
30
|
private agentInfo?;
|
|
31
|
-
private
|
|
31
|
+
private agentInstanceStarted;
|
|
32
32
|
constructor(tracer: Tracer, agentManager: AgentInstanceManager, agentInfo?: Parameters<AgentInstanceManager['startInstance']>[0]);
|
|
33
33
|
/**
|
|
34
34
|
* Called before agent execution starts
|
|
@@ -42,6 +42,7 @@ export declare class PrefactorMiddleware {
|
|
|
42
42
|
* @param state - Agent state containing messages
|
|
43
43
|
*/
|
|
44
44
|
afterAgent(state: any): Promise<void>;
|
|
45
|
+
shutdown(): void;
|
|
45
46
|
/**
|
|
46
47
|
* Wrap a model call to trace LLM invocations
|
|
47
48
|
*
|
|
@@ -82,5 +83,7 @@ export declare class PrefactorMiddleware {
|
|
|
82
83
|
* Extract tool outputs from response
|
|
83
84
|
*/
|
|
84
85
|
private extractToolOutputs;
|
|
86
|
+
private ensureAgentInstanceStarted;
|
|
87
|
+
private finishAgentInstance;
|
|
85
88
|
}
|
|
86
89
|
//# sourceMappingURL=middleware.d.ts.map
|
package/dist/middleware.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,oBAAoB,
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,oBAAoB,EAMzB,KAAK,MAAM,EACZ,MAAM,iBAAiB,CAAC;AAMzB;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,mBAAmB;IAI5B,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,YAAY;IACpB,OAAO,CAAC,SAAS,CAAC;IALpB,OAAO,CAAC,oBAAoB,CAAS;gBAG3B,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,oBAAoB,EAClC,SAAS,CAAC,EAAE,UAAU,CAAC,oBAAoB,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC;IAG1E;;;;OAIG;IAEG,WAAW,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC;IAI5C;;;;OAIG;IAEG,UAAU,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC;IAK3C,QAAQ,IAAI,IAAI;IAIhB;;;;;;OAMG;IAEG,aAAa,CAAC,CAAC,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IA2BnF;;;;;;OAMG;IAEG,YAAY,CAAC,CAAC,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IA0BlF;;OAEG;IAEH,OAAO,CAAC,gBAAgB;IA4BxB;;OAEG;IAEH,OAAO,CAAC,kBAAkB;IAK1B;;OAEG;IAEH,OAAO,CAAC,mBAAmB;IAK3B;;OAEG;IAEH,OAAO,CAAC,eAAe;IAIvB;;OAEG;IAEH,OAAO,CAAC,iBAAiB;IAIzB;;OAEG;IAEH,OAAO,CAAC,kBAAkB;IAI1B,OAAO,CAAC,0BAA0B;IAalC,OAAO,CAAC,mBAAmB;CAY5B"}
|
package/dist/middleware.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { SpanContext, SpanType, serializeValue, } from '@prefactor/core';
|
|
1
|
+
import { createSpanTypePrefixer, getLogger, SpanContext, SpanType, serializeValue, } from '@prefactor/core';
|
|
2
2
|
import { extractTokenUsage } from './metadata-extractor.js';
|
|
3
|
+
const toLangchainSpanType = createSpanTypePrefixer('langchain');
|
|
4
|
+
const logger = getLogger('middleware');
|
|
3
5
|
/**
|
|
4
6
|
* Prefactor middleware for LangChain.js agents.
|
|
5
7
|
*
|
|
@@ -29,7 +31,7 @@ export class PrefactorMiddleware {
|
|
|
29
31
|
tracer;
|
|
30
32
|
agentManager;
|
|
31
33
|
agentInfo;
|
|
32
|
-
|
|
34
|
+
agentInstanceStarted = false;
|
|
33
35
|
constructor(tracer, agentManager, agentInfo) {
|
|
34
36
|
this.tracer = tracer;
|
|
35
37
|
this.agentManager = agentManager;
|
|
@@ -42,15 +44,7 @@ export class PrefactorMiddleware {
|
|
|
42
44
|
*/
|
|
43
45
|
// biome-ignore lint/suspicious/noExplicitAny: LangChain state can be any structure
|
|
44
46
|
async beforeAgent(state) {
|
|
45
|
-
|
|
46
|
-
this.agentManager.startInstance(this.agentInfo);
|
|
47
|
-
const span = this.tracer.startSpan({
|
|
48
|
-
name: 'langchain:agent',
|
|
49
|
-
spanType: `langchain:${SpanType.AGENT}`,
|
|
50
|
-
inputs: { messages: serializeValue(messages.slice(-3)) },
|
|
51
|
-
});
|
|
52
|
-
this.rootSpan = span;
|
|
53
|
-
SpanContext.enter(span);
|
|
47
|
+
this.ensureAgentInstanceStarted();
|
|
54
48
|
}
|
|
55
49
|
/**
|
|
56
50
|
* Called after agent execution completes
|
|
@@ -59,16 +53,11 @@ export class PrefactorMiddleware {
|
|
|
59
53
|
*/
|
|
60
54
|
// biome-ignore lint/suspicious/noExplicitAny: LangChain state can be any structure
|
|
61
55
|
async afterAgent(state) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
this.
|
|
67
|
-
outputs: { messages: serializeValue(messages.slice(-3)) },
|
|
68
|
-
});
|
|
69
|
-
this.agentManager.finishInstance();
|
|
70
|
-
SpanContext.exit();
|
|
71
|
-
this.rootSpan = null;
|
|
56
|
+
// Root agent spans are intentionally not emitted.
|
|
57
|
+
void state;
|
|
58
|
+
}
|
|
59
|
+
shutdown() {
|
|
60
|
+
this.finishAgentInstance();
|
|
72
61
|
}
|
|
73
62
|
/**
|
|
74
63
|
* Wrap a model call to trace LLM invocations
|
|
@@ -79,20 +68,18 @@ export class PrefactorMiddleware {
|
|
|
79
68
|
*/
|
|
80
69
|
// biome-ignore lint/suspicious/noExplicitAny: LangChain request/handler types are dynamic
|
|
81
70
|
async wrapModelCall(request, handler) {
|
|
71
|
+
this.ensureAgentInstanceStarted();
|
|
82
72
|
const modelName = this.extractModelName(request);
|
|
83
73
|
const span = this.tracer.startSpan({
|
|
84
74
|
name: 'langchain:llm-call',
|
|
85
|
-
spanType:
|
|
75
|
+
spanType: toLangchainSpanType(SpanType.LLM),
|
|
86
76
|
inputs: {
|
|
87
77
|
...this.extractModelInputs(request),
|
|
88
78
|
'langchain.model.name': modelName,
|
|
89
79
|
},
|
|
90
80
|
});
|
|
91
81
|
try {
|
|
92
|
-
|
|
93
|
-
const response = await SpanContext.runAsync(span, async () => {
|
|
94
|
-
return handler(request);
|
|
95
|
-
});
|
|
82
|
+
const response = await SpanContext.runAsync(span, () => handler(request));
|
|
96
83
|
const outputs = this.extractModelOutputs(response);
|
|
97
84
|
const tokenUsage = extractTokenUsage(response);
|
|
98
85
|
this.tracer.endSpan(span, { outputs, tokenUsage: tokenUsage ?? undefined });
|
|
@@ -112,20 +99,18 @@ export class PrefactorMiddleware {
|
|
|
112
99
|
*/
|
|
113
100
|
// biome-ignore lint/suspicious/noExplicitAny: LangChain request/handler types are dynamic
|
|
114
101
|
async wrapToolCall(request, handler) {
|
|
102
|
+
this.ensureAgentInstanceStarted();
|
|
115
103
|
const toolName = this.extractToolName(request);
|
|
116
104
|
const span = this.tracer.startSpan({
|
|
117
105
|
name: 'langchain:tool-call',
|
|
118
|
-
spanType:
|
|
106
|
+
spanType: toLangchainSpanType(SpanType.TOOL),
|
|
119
107
|
inputs: {
|
|
120
108
|
...this.extractToolInputs(request),
|
|
121
109
|
'langchain.tool.name': toolName,
|
|
122
110
|
},
|
|
123
111
|
});
|
|
124
112
|
try {
|
|
125
|
-
|
|
126
|
-
const response = await SpanContext.runAsync(span, async () => {
|
|
127
|
-
return handler(request);
|
|
128
|
-
});
|
|
113
|
+
const response = await SpanContext.runAsync(span, () => handler(request));
|
|
129
114
|
this.tracer.endSpan(span, {
|
|
130
115
|
outputs: this.extractToolOutputs(response),
|
|
131
116
|
});
|
|
@@ -197,5 +182,29 @@ export class PrefactorMiddleware {
|
|
|
197
182
|
extractToolOutputs(response) {
|
|
198
183
|
return { output: response?.output ?? response };
|
|
199
184
|
}
|
|
185
|
+
ensureAgentInstanceStarted() {
|
|
186
|
+
if (this.agentInstanceStarted) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
try {
|
|
190
|
+
this.agentManager.startInstance(this.agentInfo);
|
|
191
|
+
this.agentInstanceStarted = true;
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
logger.error('Failed to start agent instance:', error);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
finishAgentInstance() {
|
|
198
|
+
if (!this.agentInstanceStarted) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
try {
|
|
202
|
+
this.agentManager.finishInstance();
|
|
203
|
+
this.agentInstanceStarted = false;
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
logger.error('Failed to finish agent instance:', error);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
200
209
|
}
|
|
201
210
|
//# sourceMappingURL=middleware.js.map
|
package/dist/middleware.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,WAAW,EACX,QAAQ,EACR,cAAc,GAEf,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE5D;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,OAAO,mBAAmB;IAIpB;IACA;IACA;IALF,
|
|
1
|
+
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,sBAAsB,EACtB,SAAS,EACT,WAAW,EACX,QAAQ,EACR,cAAc,GAEf,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE5D,MA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C;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,CAAC,KAAK,CAAC,kCAAkC,EAAE,KAAK,CAAC,CAAC;QAC1D,CAAC;IACH,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prefactor/langchain",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
4
4
|
"description": "LangChain.js integration for Prefactor observability",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -28,13 +28,13 @@
|
|
|
28
28
|
"author": "Prefactor",
|
|
29
29
|
"license": "MIT",
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@prefactor/core": "0.2.
|
|
31
|
+
"@prefactor/core": "0.2.7",
|
|
32
32
|
"@prefactor/pfid": "^0.1.0"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
35
|
"langchain": "^1.0.0"
|
|
36
36
|
},
|
|
37
37
|
"engines": {
|
|
38
|
-
"node": ">=
|
|
38
|
+
"node": ">=22.0.0"
|
|
39
39
|
}
|
|
40
40
|
}
|