@prefactor/core 0.1.0 → 0.1.1
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 +201 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# @prefactor/core
|
|
2
|
+
|
|
3
|
+
Framework-agnostic observability primitives for Prefactor. This package provides the foundational tracing infrastructure used by framework-specific integrations like `@prefactor/langchain`.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @prefactor/core
|
|
9
|
+
# or
|
|
10
|
+
bun add @prefactor/core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Most users should install `@prefactor/sdk` instead, which bundles both `@prefactor/core` and `@prefactor/langchain`.
|
|
14
|
+
|
|
15
|
+
## When to Use This Package
|
|
16
|
+
|
|
17
|
+
Use `@prefactor/core` directly when:
|
|
18
|
+
|
|
19
|
+
- Building a custom integration for a framework not yet supported
|
|
20
|
+
- You need manual instrumentation without LangChain.js
|
|
21
|
+
- You're implementing your own middleware or transport
|
|
22
|
+
|
|
23
|
+
For LangChain.js applications, use `@prefactor/sdk` or `@prefactor/langchain` instead.
|
|
24
|
+
|
|
25
|
+
## Exports
|
|
26
|
+
|
|
27
|
+
### Configuration
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import {
|
|
31
|
+
type Config,
|
|
32
|
+
ConfigSchema,
|
|
33
|
+
createConfig,
|
|
34
|
+
type HttpTransportConfig,
|
|
35
|
+
} from '@prefactor/core';
|
|
36
|
+
|
|
37
|
+
// Create configuration with defaults and environment variables
|
|
38
|
+
const config = createConfig({
|
|
39
|
+
transportType: 'http',
|
|
40
|
+
httpConfig: {
|
|
41
|
+
apiUrl: 'https://api.prefactor.ai',
|
|
42
|
+
apiToken: 'your-token',
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Tracing
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import {
|
|
51
|
+
Tracer,
|
|
52
|
+
SpanContext,
|
|
53
|
+
SpanType,
|
|
54
|
+
SpanStatus,
|
|
55
|
+
type Span,
|
|
56
|
+
type TokenUsage,
|
|
57
|
+
type ErrorInfo,
|
|
58
|
+
} from '@prefactor/core';
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Transports
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
import {
|
|
65
|
+
type Transport,
|
|
66
|
+
StdioTransport,
|
|
67
|
+
HttpTransport,
|
|
68
|
+
} from '@prefactor/core';
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Utilities
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import {
|
|
75
|
+
getLogger,
|
|
76
|
+
configureLogging,
|
|
77
|
+
serializeValue,
|
|
78
|
+
truncateString,
|
|
79
|
+
} from '@prefactor/core';
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Usage
|
|
83
|
+
|
|
84
|
+
### Manual Instrumentation
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
import {
|
|
88
|
+
Tracer,
|
|
89
|
+
SpanType,
|
|
90
|
+
StdioTransport,
|
|
91
|
+
createConfig,
|
|
92
|
+
} from '@prefactor/core';
|
|
93
|
+
|
|
94
|
+
// Create transport and tracer
|
|
95
|
+
const config = createConfig();
|
|
96
|
+
const transport = new StdioTransport();
|
|
97
|
+
const tracer = new Tracer(transport, config);
|
|
98
|
+
|
|
99
|
+
// Create a span
|
|
100
|
+
const span = tracer.startSpan({
|
|
101
|
+
name: 'my-operation',
|
|
102
|
+
spanType: SpanType.TOOL,
|
|
103
|
+
inputs: { query: 'example' },
|
|
104
|
+
metadata: { service: 'my-service' },
|
|
105
|
+
tags: ['production'],
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
try {
|
|
109
|
+
// Do work...
|
|
110
|
+
const result = await doSomething();
|
|
111
|
+
|
|
112
|
+
tracer.endSpan(span, {
|
|
113
|
+
outputs: { result },
|
|
114
|
+
});
|
|
115
|
+
} catch (error) {
|
|
116
|
+
tracer.endSpan(span, { error });
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Context Propagation
|
|
121
|
+
|
|
122
|
+
The SDK uses Node.js `AsyncLocalStorage` for context propagation. This ensures parent-child relationships are maintained across async boundaries.
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
import { SpanContext, Tracer, SpanType } from '@prefactor/core';
|
|
126
|
+
|
|
127
|
+
// Run code within a span context
|
|
128
|
+
await SpanContext.runAsync(parentSpan, async () => {
|
|
129
|
+
// Child spans automatically inherit from the current context
|
|
130
|
+
const current = SpanContext.getCurrent();
|
|
131
|
+
console.log(current?.spanId); // Parent span ID
|
|
132
|
+
|
|
133
|
+
// Create child span with automatic parent linkage
|
|
134
|
+
const childSpan = tracer.startSpan({
|
|
135
|
+
name: 'child-operation',
|
|
136
|
+
spanType: SpanType.TOOL,
|
|
137
|
+
inputs: {},
|
|
138
|
+
parentSpanId: current?.spanId,
|
|
139
|
+
traceId: current?.traceId,
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Custom Transport
|
|
145
|
+
|
|
146
|
+
Implement the `Transport` interface to create custom backends:
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
import type { Transport, Span } from '@prefactor/core';
|
|
150
|
+
|
|
151
|
+
class MyCustomTransport implements Transport {
|
|
152
|
+
async emit(span: Span): Promise<void> {
|
|
153
|
+
// Send span to your backend
|
|
154
|
+
await fetch('https://my-backend.com/spans', {
|
|
155
|
+
method: 'POST',
|
|
156
|
+
body: JSON.stringify(span),
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async flush(): Promise<void> {
|
|
161
|
+
// Ensure all pending spans are sent
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async shutdown(): Promise<void> {
|
|
165
|
+
// Clean up resources
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Span Types
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
enum SpanType {
|
|
174
|
+
AGENT = 'AGENT',
|
|
175
|
+
LLM = 'LLM',
|
|
176
|
+
TOOL = 'TOOL',
|
|
177
|
+
CHAIN = 'CHAIN',
|
|
178
|
+
RETRIEVER = 'RETRIEVER',
|
|
179
|
+
EMBEDDING = 'EMBEDDING',
|
|
180
|
+
OTHER = 'OTHER',
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## Span Status
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
enum SpanStatus {
|
|
188
|
+
PENDING = 'PENDING',
|
|
189
|
+
RUNNING = 'RUNNING',
|
|
190
|
+
SUCCESS = 'SUCCESS',
|
|
191
|
+
ERROR = 'ERROR',
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## Requirements
|
|
196
|
+
|
|
197
|
+
- Node.js >= 24.0.0
|
|
198
|
+
|
|
199
|
+
## License
|
|
200
|
+
|
|
201
|
+
MIT
|