@prefactor/sdk 0.1.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/LICENSE +14 -0
- package/README.md +313 -0
- package/dist/LICENSE +14 -0
- package/dist/README.md +313 -0
- package/dist/config.d.ts +247 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +107 -0
- package/dist/config.js.map +1 -0
- package/dist/index.cjs +1224 -0
- package/dist/index.cjs.map +23 -0
- package/dist/index.d.ts +82 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1200 -0
- package/dist/index.js.map +23 -0
- package/dist/instrumentation/langchain/metadata-extractor.d.ts +20 -0
- package/dist/instrumentation/langchain/metadata-extractor.d.ts.map +1 -0
- package/dist/instrumentation/langchain/metadata-extractor.js +54 -0
- package/dist/instrumentation/langchain/metadata-extractor.js.map +1 -0
- package/dist/instrumentation/langchain/middleware.d.ts +84 -0
- package/dist/instrumentation/langchain/middleware.d.ts.map +1 -0
- package/dist/instrumentation/langchain/middleware.js +181 -0
- package/dist/instrumentation/langchain/middleware.js.map +1 -0
- package/dist/package.json +56 -0
- package/dist/tracing/context.d.ts +53 -0
- package/dist/tracing/context.d.ts.map +1 -0
- package/dist/tracing/context.js +65 -0
- package/dist/tracing/context.js.map +1 -0
- package/dist/tracing/span.d.ts +68 -0
- package/dist/tracing/span.d.ts.map +1 -0
- package/dist/tracing/span.js +21 -0
- package/dist/tracing/span.js.map +1 -0
- package/dist/tracing/tracer.d.ts +100 -0
- package/dist/tracing/tracer.d.ts.map +1 -0
- package/dist/tracing/tracer.js +151 -0
- package/dist/tracing/tracer.js.map +1 -0
- package/dist/transport/base.d.ts +38 -0
- package/dist/transport/base.d.ts.map +1 -0
- package/dist/transport/base.js +2 -0
- package/dist/transport/base.js.map +1 -0
- package/dist/transport/http.d.ts +86 -0
- package/dist/transport/http.d.ts.map +1 -0
- package/dist/transport/http.js +331 -0
- package/dist/transport/http.js.map +1 -0
- package/dist/transport/stdio.d.ts +48 -0
- package/dist/transport/stdio.d.ts.map +1 -0
- package/dist/transport/stdio.js +71 -0
- package/dist/transport/stdio.js.map +1 -0
- package/dist/utils/logging.d.ts +29 -0
- package/dist/utils/logging.d.ts.map +1 -0
- package/dist/utils/logging.js +71 -0
- package/dist/utils/logging.js.map +1 -0
- package/dist/utils/serialization.d.ts +24 -0
- package/dist/utils/serialization.d.ts.map +1 -0
- package/dist/utils/serialization.js +60 -0
- package/dist/utils/serialization.js.map +1 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Copyright (c) 2026 Prefactor Pty Ltd
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
4
|
+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
|
5
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
6
|
+
persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
7
|
+
|
|
8
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
|
9
|
+
Software.
|
|
10
|
+
|
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
|
12
|
+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
13
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
14
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
# Prefactor SDK for TypeScript
|
|
2
|
+
|
|
3
|
+
Automatic observability for LangChain.js agents. Capture distributed traces of LLM calls, tool executions, and agent workflows with minimal integration effort.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ✅ Automatic tracing of LLM calls with token usage
|
|
8
|
+
- ✅ Tool execution tracking
|
|
9
|
+
- ✅ Agent workflow visualization
|
|
10
|
+
- ✅ Parent-child span relationships
|
|
11
|
+
- ✅ Error tracking and debugging
|
|
12
|
+
- ✅ Zero-overhead instrumentation
|
|
13
|
+
- ✅ TypeScript type safety
|
|
14
|
+
- ✅ Supports stdio and HTTP transports
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @prefactor/sdk
|
|
20
|
+
# or
|
|
21
|
+
bun add @prefactor/sdk
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { init } from '@prefactor/sdk';
|
|
28
|
+
import { ChatAnthropic } from '@langchain/anthropic';
|
|
29
|
+
import { createReactAgent } from '@langchain/langgraph/prebuilt';
|
|
30
|
+
|
|
31
|
+
// Initialize Prefactor (defaults to stdio transport)
|
|
32
|
+
const middleware = init();
|
|
33
|
+
|
|
34
|
+
// Create your agent with middleware
|
|
35
|
+
const model = new ChatAnthropic({ model: 'claude-sonnet-4-5-20250929' });
|
|
36
|
+
const agent = createReactAgent({
|
|
37
|
+
llm: model,
|
|
38
|
+
tools: [],
|
|
39
|
+
middleware: [middleware],
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// All operations are automatically traced!
|
|
43
|
+
const result = await agent.invoke({
|
|
44
|
+
messages: [{ role: 'user', content: 'What is 2+2?' }],
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
console.log(result.messages[result.messages.length - 1].content);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Configuration
|
|
51
|
+
|
|
52
|
+
### Environment Variables
|
|
53
|
+
|
|
54
|
+
The SDK can be configured using environment variables:
|
|
55
|
+
|
|
56
|
+
- `PREFACTOR_TRANSPORT`: `"stdio"` | `"http"` (default: `"stdio"`)
|
|
57
|
+
- `PREFACTOR_API_URL`: API endpoint for HTTP transport
|
|
58
|
+
- `PREFACTOR_API_TOKEN`: Authentication token for HTTP transport
|
|
59
|
+
- `PREFACTOR_SAMPLE_RATE`: Sampling rate 0.0-1.0 (default: `1.0`)
|
|
60
|
+
- `PREFACTOR_CAPTURE_INPUTS`: Capture span inputs (default: `true`)
|
|
61
|
+
- `PREFACTOR_CAPTURE_OUTPUTS`: Capture span outputs (default: `true`)
|
|
62
|
+
- `PREFACTOR_MAX_INPUT_LENGTH`: Max input string length (default: `10000`)
|
|
63
|
+
- `PREFACTOR_MAX_OUTPUT_LENGTH`: Max output string length (default: `10000`)
|
|
64
|
+
- `PREFACTOR_LOG_LEVEL`: `"debug"` | `"info"` | `"warn"` | `"error"` (default: `"info"`)
|
|
65
|
+
|
|
66
|
+
### Programmatic Configuration
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
import { init } from '@prefactor/sdk';
|
|
70
|
+
|
|
71
|
+
// HTTP Transport
|
|
72
|
+
const middleware = init({
|
|
73
|
+
transportType: 'http',
|
|
74
|
+
httpConfig: {
|
|
75
|
+
apiUrl: 'https://api.prefactor.ai',
|
|
76
|
+
apiToken: process.env.PREFACTOR_API_TOKEN!,
|
|
77
|
+
agentId: 'my-agent',
|
|
78
|
+
agentVersion: '1.0.0',
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Custom sampling
|
|
83
|
+
const middleware = init({
|
|
84
|
+
sampleRate: 0.1, // Sample 10% of traces
|
|
85
|
+
maxInputLength: 5000,
|
|
86
|
+
maxOutputLength: 5000,
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Transports
|
|
91
|
+
|
|
92
|
+
### STDIO Transport (Default)
|
|
93
|
+
|
|
94
|
+
The STDIO transport writes spans as newline-delimited JSON to stdout. This is useful for local development and piping to other tools.
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
import { init } from '@prefactor/sdk';
|
|
98
|
+
|
|
99
|
+
const middleware = init(); // Uses stdio by default
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### HTTP Transport
|
|
103
|
+
|
|
104
|
+
The HTTP transport sends spans to a remote API endpoint with retry logic and queue-based processing.
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
import { init } from '@prefactor/sdk';
|
|
108
|
+
|
|
109
|
+
const middleware = init({
|
|
110
|
+
transportType: 'http',
|
|
111
|
+
httpConfig: {
|
|
112
|
+
apiUrl: 'https://api.prefactor.ai',
|
|
113
|
+
apiToken: process.env.PREFACTOR_API_TOKEN!,
|
|
114
|
+
agentId: 'my-agent',
|
|
115
|
+
agentVersion: '1.0.0',
|
|
116
|
+
maxRetries: 3,
|
|
117
|
+
requestTimeout: 30000,
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## API Reference
|
|
123
|
+
|
|
124
|
+
### `init(config?: Partial<Config>): PrefactorMiddleware`
|
|
125
|
+
|
|
126
|
+
Initialize the SDK and return middleware instance.
|
|
127
|
+
|
|
128
|
+
**Parameters:**
|
|
129
|
+
- `config` - Optional configuration object
|
|
130
|
+
|
|
131
|
+
**Returns:**
|
|
132
|
+
- `PrefactorMiddleware` - Middleware instance to use with LangChain.js agents
|
|
133
|
+
|
|
134
|
+
**Example:**
|
|
135
|
+
```typescript
|
|
136
|
+
const middleware = init({
|
|
137
|
+
transportType: 'stdio',
|
|
138
|
+
sampleRate: 1.0,
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### `shutdown(): Promise<void>`
|
|
143
|
+
|
|
144
|
+
Flush pending spans and close connections. Call before application exit.
|
|
145
|
+
|
|
146
|
+
**Example:**
|
|
147
|
+
```typescript
|
|
148
|
+
import { shutdown } from '@prefactor/sdk';
|
|
149
|
+
|
|
150
|
+
process.on('SIGTERM', async () => {
|
|
151
|
+
await shutdown();
|
|
152
|
+
process.exit(0);
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### `getTracer(): Tracer`
|
|
157
|
+
|
|
158
|
+
Get the global tracer instance for manual instrumentation.
|
|
159
|
+
|
|
160
|
+
**Returns:**
|
|
161
|
+
- `Tracer` - Tracer instance
|
|
162
|
+
|
|
163
|
+
**Example:**
|
|
164
|
+
```typescript
|
|
165
|
+
import { getTracer, SpanType } from '@prefactor/sdk';
|
|
166
|
+
|
|
167
|
+
const tracer = getTracer();
|
|
168
|
+
const span = tracer.startSpan({
|
|
169
|
+
name: 'custom-operation',
|
|
170
|
+
spanType: SpanType.TOOL,
|
|
171
|
+
inputs: { data: 'example' },
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
try {
|
|
175
|
+
// ... do work ...
|
|
176
|
+
tracer.endSpan(span, { outputs: { result: 'done' } });
|
|
177
|
+
} catch (error) {
|
|
178
|
+
tracer.endSpan(span, { error });
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## Advanced Usage
|
|
183
|
+
|
|
184
|
+
### Manual Instrumentation
|
|
185
|
+
|
|
186
|
+
For operations not automatically traced by the middleware:
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
import { getTracer, SpanType } from '@prefactor/sdk';
|
|
190
|
+
|
|
191
|
+
const tracer = getTracer();
|
|
192
|
+
|
|
193
|
+
const span = tracer.startSpan({
|
|
194
|
+
name: 'database-query',
|
|
195
|
+
spanType: SpanType.TOOL,
|
|
196
|
+
inputs: { query: 'SELECT * FROM users' },
|
|
197
|
+
metadata: { database: 'postgres' },
|
|
198
|
+
tags: ['database', 'query'],
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
try {
|
|
202
|
+
const result = await db.query('SELECT * FROM users');
|
|
203
|
+
tracer.endSpan(span, { outputs: { rowCount: result.rows.length } });
|
|
204
|
+
} catch (error) {
|
|
205
|
+
tracer.endSpan(span, { error });
|
|
206
|
+
}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Context Propagation
|
|
210
|
+
|
|
211
|
+
The SDK automatically propagates span context through async operations using Node.js AsyncLocalStorage. Child spans automatically inherit the trace ID and parent span ID from the current context.
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
import { SpanContext } from '@prefactor/sdk';
|
|
215
|
+
|
|
216
|
+
// Get the current span (if any)
|
|
217
|
+
const currentSpan = SpanContext.getCurrent();
|
|
218
|
+
|
|
219
|
+
// Child spans automatically use the current span as parent
|
|
220
|
+
const child = tracer.startSpan({
|
|
221
|
+
name: 'child-operation',
|
|
222
|
+
spanType: SpanType.TOOL,
|
|
223
|
+
inputs: {},
|
|
224
|
+
parentSpanId: currentSpan?.spanId,
|
|
225
|
+
traceId: currentSpan?.traceId,
|
|
226
|
+
});
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## TypeScript Support
|
|
230
|
+
|
|
231
|
+
The SDK is written in TypeScript and provides full type definitions:
|
|
232
|
+
|
|
233
|
+
```typescript
|
|
234
|
+
import type {
|
|
235
|
+
Config,
|
|
236
|
+
HttpTransportConfig,
|
|
237
|
+
Span,
|
|
238
|
+
SpanType,
|
|
239
|
+
SpanStatus,
|
|
240
|
+
TokenUsage,
|
|
241
|
+
ErrorInfo
|
|
242
|
+
} from '@prefactor/sdk';
|
|
243
|
+
|
|
244
|
+
const config: Config = {
|
|
245
|
+
transportType: 'stdio',
|
|
246
|
+
sampleRate: 1.0,
|
|
247
|
+
captureInputs: true,
|
|
248
|
+
captureOutputs: true,
|
|
249
|
+
};
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## Examples
|
|
253
|
+
|
|
254
|
+
See the `examples/` directory for complete examples:
|
|
255
|
+
|
|
256
|
+
- `examples/basic.ts` - Simple LangChain.js agent with stdio transport
|
|
257
|
+
- `examples/http-transport.ts` - Using HTTP transport
|
|
258
|
+
- `examples/custom-tools.ts` - Tracing custom tools
|
|
259
|
+
- `examples/manual-instrumentation.ts` - Manual span creation
|
|
260
|
+
|
|
261
|
+
## Architecture
|
|
262
|
+
|
|
263
|
+
The SDK consists of five main layers:
|
|
264
|
+
|
|
265
|
+
1. **Tracing Layer**: Span data models, Tracer for lifecycle management, Context propagation
|
|
266
|
+
2. **Transport Layer**: Pluggable backends (stdio, HTTP) for span emission
|
|
267
|
+
3. **Instrumentation Layer**: LangChain.js middleware integration
|
|
268
|
+
4. **Configuration**: Environment variable support, validation with Zod
|
|
269
|
+
5. **Utilities**: Logging, serialization helpers
|
|
270
|
+
|
|
271
|
+
For more details, see `docs/architecture.md`.
|
|
272
|
+
|
|
273
|
+
## Requirements
|
|
274
|
+
|
|
275
|
+
- Node.js >= 18.0.0
|
|
276
|
+
- TypeScript >= 5.0.0 (for TypeScript projects)
|
|
277
|
+
- Bun >= 1.0.0 (optional, for development)
|
|
278
|
+
|
|
279
|
+
## Development
|
|
280
|
+
|
|
281
|
+
```bash
|
|
282
|
+
# Install dependencies
|
|
283
|
+
bun install
|
|
284
|
+
|
|
285
|
+
# Run tests
|
|
286
|
+
bun test
|
|
287
|
+
|
|
288
|
+
# Type check
|
|
289
|
+
bun run typecheck
|
|
290
|
+
|
|
291
|
+
# Lint
|
|
292
|
+
bun run lint
|
|
293
|
+
|
|
294
|
+
# Format
|
|
295
|
+
bun run format
|
|
296
|
+
|
|
297
|
+
# Build
|
|
298
|
+
bun run build
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
## Contributing
|
|
302
|
+
|
|
303
|
+
Contributions are welcome! Please see `CONTRIBUTING.md` for guidelines.
|
|
304
|
+
|
|
305
|
+
## License
|
|
306
|
+
|
|
307
|
+
MIT
|
|
308
|
+
|
|
309
|
+
## Support
|
|
310
|
+
|
|
311
|
+
- Documentation: [https://docs.prefactor.ai](https://docs.prefactor.ai)
|
|
312
|
+
- Issues: [GitHub Issues](https://github.com/prefactor/typescript-sdk/issues)
|
|
313
|
+
- Email: support@prefactor.ai
|
package/dist/LICENSE
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Copyright (c) 2026 Prefactor Pty Ltd
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
4
|
+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
|
5
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
6
|
+
persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
7
|
+
|
|
8
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
|
9
|
+
Software.
|
|
10
|
+
|
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
|
12
|
+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
13
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
14
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/dist/README.md
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
# Prefactor SDK for TypeScript
|
|
2
|
+
|
|
3
|
+
Automatic observability for LangChain.js agents. Capture distributed traces of LLM calls, tool executions, and agent workflows with minimal integration effort.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ✅ Automatic tracing of LLM calls with token usage
|
|
8
|
+
- ✅ Tool execution tracking
|
|
9
|
+
- ✅ Agent workflow visualization
|
|
10
|
+
- ✅ Parent-child span relationships
|
|
11
|
+
- ✅ Error tracking and debugging
|
|
12
|
+
- ✅ Zero-overhead instrumentation
|
|
13
|
+
- ✅ TypeScript type safety
|
|
14
|
+
- ✅ Supports stdio and HTTP transports
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @prefactor/sdk
|
|
20
|
+
# or
|
|
21
|
+
bun add @prefactor/sdk
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { init } from '@prefactor/sdk';
|
|
28
|
+
import { ChatAnthropic } from '@langchain/anthropic';
|
|
29
|
+
import { createReactAgent } from '@langchain/langgraph/prebuilt';
|
|
30
|
+
|
|
31
|
+
// Initialize Prefactor (defaults to stdio transport)
|
|
32
|
+
const middleware = init();
|
|
33
|
+
|
|
34
|
+
// Create your agent with middleware
|
|
35
|
+
const model = new ChatAnthropic({ model: 'claude-sonnet-4-5-20250929' });
|
|
36
|
+
const agent = createReactAgent({
|
|
37
|
+
llm: model,
|
|
38
|
+
tools: [],
|
|
39
|
+
middleware: [middleware],
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// All operations are automatically traced!
|
|
43
|
+
const result = await agent.invoke({
|
|
44
|
+
messages: [{ role: 'user', content: 'What is 2+2?' }],
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
console.log(result.messages[result.messages.length - 1].content);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Configuration
|
|
51
|
+
|
|
52
|
+
### Environment Variables
|
|
53
|
+
|
|
54
|
+
The SDK can be configured using environment variables:
|
|
55
|
+
|
|
56
|
+
- `PREFACTOR_TRANSPORT`: `"stdio"` | `"http"` (default: `"stdio"`)
|
|
57
|
+
- `PREFACTOR_API_URL`: API endpoint for HTTP transport
|
|
58
|
+
- `PREFACTOR_API_TOKEN`: Authentication token for HTTP transport
|
|
59
|
+
- `PREFACTOR_SAMPLE_RATE`: Sampling rate 0.0-1.0 (default: `1.0`)
|
|
60
|
+
- `PREFACTOR_CAPTURE_INPUTS`: Capture span inputs (default: `true`)
|
|
61
|
+
- `PREFACTOR_CAPTURE_OUTPUTS`: Capture span outputs (default: `true`)
|
|
62
|
+
- `PREFACTOR_MAX_INPUT_LENGTH`: Max input string length (default: `10000`)
|
|
63
|
+
- `PREFACTOR_MAX_OUTPUT_LENGTH`: Max output string length (default: `10000`)
|
|
64
|
+
- `PREFACTOR_LOG_LEVEL`: `"debug"` | `"info"` | `"warn"` | `"error"` (default: `"info"`)
|
|
65
|
+
|
|
66
|
+
### Programmatic Configuration
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
import { init } from '@prefactor/sdk';
|
|
70
|
+
|
|
71
|
+
// HTTP Transport
|
|
72
|
+
const middleware = init({
|
|
73
|
+
transportType: 'http',
|
|
74
|
+
httpConfig: {
|
|
75
|
+
apiUrl: 'https://api.prefactor.ai',
|
|
76
|
+
apiToken: process.env.PREFACTOR_API_TOKEN!,
|
|
77
|
+
agentId: 'my-agent',
|
|
78
|
+
agentVersion: '1.0.0',
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Custom sampling
|
|
83
|
+
const middleware = init({
|
|
84
|
+
sampleRate: 0.1, // Sample 10% of traces
|
|
85
|
+
maxInputLength: 5000,
|
|
86
|
+
maxOutputLength: 5000,
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Transports
|
|
91
|
+
|
|
92
|
+
### STDIO Transport (Default)
|
|
93
|
+
|
|
94
|
+
The STDIO transport writes spans as newline-delimited JSON to stdout. This is useful for local development and piping to other tools.
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
import { init } from '@prefactor/sdk';
|
|
98
|
+
|
|
99
|
+
const middleware = init(); // Uses stdio by default
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### HTTP Transport
|
|
103
|
+
|
|
104
|
+
The HTTP transport sends spans to a remote API endpoint with retry logic and queue-based processing.
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
import { init } from '@prefactor/sdk';
|
|
108
|
+
|
|
109
|
+
const middleware = init({
|
|
110
|
+
transportType: 'http',
|
|
111
|
+
httpConfig: {
|
|
112
|
+
apiUrl: 'https://api.prefactor.ai',
|
|
113
|
+
apiToken: process.env.PREFACTOR_API_TOKEN!,
|
|
114
|
+
agentId: 'my-agent',
|
|
115
|
+
agentVersion: '1.0.0',
|
|
116
|
+
maxRetries: 3,
|
|
117
|
+
requestTimeout: 30000,
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## API Reference
|
|
123
|
+
|
|
124
|
+
### `init(config?: Partial<Config>): PrefactorMiddleware`
|
|
125
|
+
|
|
126
|
+
Initialize the SDK and return middleware instance.
|
|
127
|
+
|
|
128
|
+
**Parameters:**
|
|
129
|
+
- `config` - Optional configuration object
|
|
130
|
+
|
|
131
|
+
**Returns:**
|
|
132
|
+
- `PrefactorMiddleware` - Middleware instance to use with LangChain.js agents
|
|
133
|
+
|
|
134
|
+
**Example:**
|
|
135
|
+
```typescript
|
|
136
|
+
const middleware = init({
|
|
137
|
+
transportType: 'stdio',
|
|
138
|
+
sampleRate: 1.0,
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### `shutdown(): Promise<void>`
|
|
143
|
+
|
|
144
|
+
Flush pending spans and close connections. Call before application exit.
|
|
145
|
+
|
|
146
|
+
**Example:**
|
|
147
|
+
```typescript
|
|
148
|
+
import { shutdown } from '@prefactor/sdk';
|
|
149
|
+
|
|
150
|
+
process.on('SIGTERM', async () => {
|
|
151
|
+
await shutdown();
|
|
152
|
+
process.exit(0);
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### `getTracer(): Tracer`
|
|
157
|
+
|
|
158
|
+
Get the global tracer instance for manual instrumentation.
|
|
159
|
+
|
|
160
|
+
**Returns:**
|
|
161
|
+
- `Tracer` - Tracer instance
|
|
162
|
+
|
|
163
|
+
**Example:**
|
|
164
|
+
```typescript
|
|
165
|
+
import { getTracer, SpanType } from '@prefactor/sdk';
|
|
166
|
+
|
|
167
|
+
const tracer = getTracer();
|
|
168
|
+
const span = tracer.startSpan({
|
|
169
|
+
name: 'custom-operation',
|
|
170
|
+
spanType: SpanType.TOOL,
|
|
171
|
+
inputs: { data: 'example' },
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
try {
|
|
175
|
+
// ... do work ...
|
|
176
|
+
tracer.endSpan(span, { outputs: { result: 'done' } });
|
|
177
|
+
} catch (error) {
|
|
178
|
+
tracer.endSpan(span, { error });
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## Advanced Usage
|
|
183
|
+
|
|
184
|
+
### Manual Instrumentation
|
|
185
|
+
|
|
186
|
+
For operations not automatically traced by the middleware:
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
import { getTracer, SpanType } from '@prefactor/sdk';
|
|
190
|
+
|
|
191
|
+
const tracer = getTracer();
|
|
192
|
+
|
|
193
|
+
const span = tracer.startSpan({
|
|
194
|
+
name: 'database-query',
|
|
195
|
+
spanType: SpanType.TOOL,
|
|
196
|
+
inputs: { query: 'SELECT * FROM users' },
|
|
197
|
+
metadata: { database: 'postgres' },
|
|
198
|
+
tags: ['database', 'query'],
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
try {
|
|
202
|
+
const result = await db.query('SELECT * FROM users');
|
|
203
|
+
tracer.endSpan(span, { outputs: { rowCount: result.rows.length } });
|
|
204
|
+
} catch (error) {
|
|
205
|
+
tracer.endSpan(span, { error });
|
|
206
|
+
}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Context Propagation
|
|
210
|
+
|
|
211
|
+
The SDK automatically propagates span context through async operations using Node.js AsyncLocalStorage. Child spans automatically inherit the trace ID and parent span ID from the current context.
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
import { SpanContext } from '@prefactor/sdk';
|
|
215
|
+
|
|
216
|
+
// Get the current span (if any)
|
|
217
|
+
const currentSpan = SpanContext.getCurrent();
|
|
218
|
+
|
|
219
|
+
// Child spans automatically use the current span as parent
|
|
220
|
+
const child = tracer.startSpan({
|
|
221
|
+
name: 'child-operation',
|
|
222
|
+
spanType: SpanType.TOOL,
|
|
223
|
+
inputs: {},
|
|
224
|
+
parentSpanId: currentSpan?.spanId,
|
|
225
|
+
traceId: currentSpan?.traceId,
|
|
226
|
+
});
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## TypeScript Support
|
|
230
|
+
|
|
231
|
+
The SDK is written in TypeScript and provides full type definitions:
|
|
232
|
+
|
|
233
|
+
```typescript
|
|
234
|
+
import type {
|
|
235
|
+
Config,
|
|
236
|
+
HttpTransportConfig,
|
|
237
|
+
Span,
|
|
238
|
+
SpanType,
|
|
239
|
+
SpanStatus,
|
|
240
|
+
TokenUsage,
|
|
241
|
+
ErrorInfo
|
|
242
|
+
} from '@prefactor/sdk';
|
|
243
|
+
|
|
244
|
+
const config: Config = {
|
|
245
|
+
transportType: 'stdio',
|
|
246
|
+
sampleRate: 1.0,
|
|
247
|
+
captureInputs: true,
|
|
248
|
+
captureOutputs: true,
|
|
249
|
+
};
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## Examples
|
|
253
|
+
|
|
254
|
+
See the `examples/` directory for complete examples:
|
|
255
|
+
|
|
256
|
+
- `examples/basic.ts` - Simple LangChain.js agent with stdio transport
|
|
257
|
+
- `examples/http-transport.ts` - Using HTTP transport
|
|
258
|
+
- `examples/custom-tools.ts` - Tracing custom tools
|
|
259
|
+
- `examples/manual-instrumentation.ts` - Manual span creation
|
|
260
|
+
|
|
261
|
+
## Architecture
|
|
262
|
+
|
|
263
|
+
The SDK consists of five main layers:
|
|
264
|
+
|
|
265
|
+
1. **Tracing Layer**: Span data models, Tracer for lifecycle management, Context propagation
|
|
266
|
+
2. **Transport Layer**: Pluggable backends (stdio, HTTP) for span emission
|
|
267
|
+
3. **Instrumentation Layer**: LangChain.js middleware integration
|
|
268
|
+
4. **Configuration**: Environment variable support, validation with Zod
|
|
269
|
+
5. **Utilities**: Logging, serialization helpers
|
|
270
|
+
|
|
271
|
+
For more details, see `docs/architecture.md`.
|
|
272
|
+
|
|
273
|
+
## Requirements
|
|
274
|
+
|
|
275
|
+
- Node.js >= 18.0.0
|
|
276
|
+
- TypeScript >= 5.0.0 (for TypeScript projects)
|
|
277
|
+
- Bun >= 1.0.0 (optional, for development)
|
|
278
|
+
|
|
279
|
+
## Development
|
|
280
|
+
|
|
281
|
+
```bash
|
|
282
|
+
# Install dependencies
|
|
283
|
+
bun install
|
|
284
|
+
|
|
285
|
+
# Run tests
|
|
286
|
+
bun test
|
|
287
|
+
|
|
288
|
+
# Type check
|
|
289
|
+
bun run typecheck
|
|
290
|
+
|
|
291
|
+
# Lint
|
|
292
|
+
bun run lint
|
|
293
|
+
|
|
294
|
+
# Format
|
|
295
|
+
bun run format
|
|
296
|
+
|
|
297
|
+
# Build
|
|
298
|
+
bun run build
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
## Contributing
|
|
302
|
+
|
|
303
|
+
Contributions are welcome! Please see `CONTRIBUTING.md` for guidelines.
|
|
304
|
+
|
|
305
|
+
## License
|
|
306
|
+
|
|
307
|
+
MIT
|
|
308
|
+
|
|
309
|
+
## Support
|
|
310
|
+
|
|
311
|
+
- Documentation: [https://docs.prefactor.ai](https://docs.prefactor.ai)
|
|
312
|
+
- Issues: [GitHub Issues](https://github.com/prefactor/typescript-sdk/issues)
|
|
313
|
+
- Email: support@prefactor.ai
|