@cloudbase/agent-observability 1.0.1-alpha.10
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 +231 -0
- package/dist/chunk-NFEGQTCC.mjs +27 -0
- package/dist/chunk-NFEGQTCC.mjs.map +1 -0
- package/dist/chunk-ZGEMAYS4.mjs +716 -0
- package/dist/chunk-ZGEMAYS4.mjs.map +1 -0
- package/dist/esm-PGEDANAI.mjs +1030 -0
- package/dist/esm-PGEDANAI.mjs.map +1 -0
- package/dist/index.d.mts +728 -0
- package/dist/index.d.ts +728 -0
- package/dist/index.js +732 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +52 -0
- package/dist/index.mjs.map +1 -0
- package/dist/langchain.d.mts +108 -0
- package/dist/langchain.d.ts +108 -0
- package/dist/langchain.js +1237 -0
- package/dist/langchain.js.map +1 -0
- package/dist/langchain.mjs +535 -0
- package/dist/langchain.mjs.map +1 -0
- package/dist/server.d.mts +163 -0
- package/dist/server.d.ts +163 -0
- package/dist/server.js +1530 -0
- package/dist/server.js.map +1 -0
- package/dist/server.mjs +177 -0
- package/dist/server.mjs.map +1 -0
- package/package.json +91 -0
- package/src/core/attributes.ts +233 -0
- package/src/core/constants.ts +75 -0
- package/src/core/spanWrapper.ts +417 -0
- package/src/core/tracerProvider.ts +136 -0
- package/src/index.ts +775 -0
- package/src/langchain/CallbackHandler.ts +893 -0
- package/src/langchain/index.ts +7 -0
- package/src/server/config.ts +160 -0
- package/src/server/index.ts +21 -0
- package/src/server/setup.ts +349 -0
- package/src/types.ts +254 -0
package/README.md
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
# AG-Kit TypeScript Observability
|
|
2
|
+
|
|
3
|
+
OpenTelemetry-based observability for AG-Kit with OpenInference semantic conventions.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# Core package
|
|
9
|
+
npm install @cloudbase/agent-observability
|
|
10
|
+
|
|
11
|
+
# With LangChain integration
|
|
12
|
+
npm install @cloudbase/agent-observability @langchain/core
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Architecture Overview
|
|
16
|
+
|
|
17
|
+
This package provides a callback-based observability system that integrates with LangChain/LangGraph through the standard `BaseCallbackHandler` mechanism.
|
|
18
|
+
|
|
19
|
+
### Core Components
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
23
|
+
│ AG-Kit Application │
|
|
24
|
+
├─────────────────────────────────────────────────────────────────┤
|
|
25
|
+
│ │
|
|
26
|
+
│ ┌──────────────────────────────────────────────────────────┐ │
|
|
27
|
+
│ │ Server Layer │ │
|
|
28
|
+
│ │ Creates AG-UI.Server span, extracts SpanContext, │ │
|
|
29
|
+
│ │ propagates via forwardedProps │ │
|
|
30
|
+
│ └──────────────────────────────────────────────────────────┘ │
|
|
31
|
+
│ │ │
|
|
32
|
+
│ ▼ │
|
|
33
|
+
│ ┌──────────────────────────────────────────────────────────┐ │
|
|
34
|
+
│ │ Adapter Layer (LangGraph) │ │
|
|
35
|
+
│ │ Restores SpanContext, sets external parent context │ │
|
|
36
|
+
│ │ in CallbackHandler │ │
|
|
37
|
+
│ └──────────────────────────────────────────────────────────┘ │
|
|
38
|
+
│ │ │
|
|
39
|
+
│ ▼ │
|
|
40
|
+
│ ┌──────────────────────────────────────────────────────────┐ │
|
|
41
|
+
│ │ LangGraph Workflow (streamEvents) │ │
|
|
42
|
+
│ │ Events flow through patched astream_events │ │
|
|
43
|
+
│ └──────────────────────────────────────────────────────────┘ │
|
|
44
|
+
│ │ │
|
|
45
|
+
│ ▼ │
|
|
46
|
+
│ ┌──────────────────────────────────────────────────────────┐ │
|
|
47
|
+
│ │ CallbackHandler (BaseCallbackHandler) │ │
|
|
48
|
+
│ │ on_chain_start, on_llm_start, on_tool_start, etc. │ │
|
|
49
|
+
│ │ Creates spans with correct parent-child relationships │ │
|
|
50
|
+
│ └──────────────────────────────────────────────────────────┘ │
|
|
51
|
+
│ │ │
|
|
52
|
+
│ ▼ │
|
|
53
|
+
│ ┌──────────────────────────────────────────────────────────┐ │
|
|
54
|
+
│ │ OpenTelemetry │ │
|
|
55
|
+
│ │ SpanContext propagation, OTLP exporters │ │
|
|
56
|
+
│ └──────────────────────────────────────────────────────────┘ │
|
|
57
|
+
│ │
|
|
58
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Callback Mechanism
|
|
62
|
+
|
|
63
|
+
The observability system uses LangChain's standard `BaseCallbackHandler` to automatically capture tracing events:
|
|
64
|
+
|
|
65
|
+
1. **Callback Registration**
|
|
66
|
+
```typescript
|
|
67
|
+
import { CallbackHandler } from '@cloudbase/agent-observability/langchain';
|
|
68
|
+
|
|
69
|
+
const handler = new CallbackHandler({
|
|
70
|
+
adapterName: 'LangGraph'
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
2. **Span Hierarchy Construction**
|
|
75
|
+
- Server span created at request entry
|
|
76
|
+
- SpanContext serialized and passed through `forwardedProps`
|
|
77
|
+
- Adapter layer restores context and sets external parent
|
|
78
|
+
- CallbackHandler creates child spans with correct parent relationships
|
|
79
|
+
|
|
80
|
+
3. **Event Handling**
|
|
81
|
+
- `on_chain_start`: Creates CHAIN spans (Adapter.LangGraph, nodes)
|
|
82
|
+
- `on_llm_start`: Creates LLM spans (ChatOpenAI, etc.)
|
|
83
|
+
- `on_tool_start`: Creates TOOL spans (calculator, etc.)
|
|
84
|
+
- `on_chain_end` / `on_llm_end` / `on_tool_end`: End spans with duration
|
|
85
|
+
|
|
86
|
+
## Quick Start
|
|
87
|
+
|
|
88
|
+
### Basic Span Creation
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { startObservation } from '@cloudbase/agent-observability';
|
|
92
|
+
|
|
93
|
+
// Create a span
|
|
94
|
+
const span = startObservation(
|
|
95
|
+
'my-operation',
|
|
96
|
+
{ 'http.method': 'GET' },
|
|
97
|
+
{ asType: 'span' }
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
// Update and end
|
|
101
|
+
span.update({ 'http.status_code': 200 });
|
|
102
|
+
span.end();
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### LangChain Integration
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import { CallbackHandler } from '@cloudbase/agent-observability/langchain';
|
|
109
|
+
import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
|
|
110
|
+
|
|
111
|
+
const handler = CallbackHandler.create({
|
|
112
|
+
adapterName: 'LangGraph'
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// Use with LangChain
|
|
116
|
+
const chain = new LLMChain({ llm, prompt, callbacks: [handler] });
|
|
117
|
+
await chain.invoke({ input: 'Hello' });
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### LangGraph Integration
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
import { CallbackHandler } from '@cloudbase/agent-observability/langchain';
|
|
124
|
+
import { StateGraph } from '@langgraph/langgraph';
|
|
125
|
+
|
|
126
|
+
const workflow = new StateGraph(StateSchema)
|
|
127
|
+
.addNode('node_a', nodeA)
|
|
128
|
+
.addNode('node_b', nodeB)
|
|
129
|
+
.compile();
|
|
130
|
+
|
|
131
|
+
// The callback is automatically injected through the agent wrapper
|
|
132
|
+
const agent = new LangGraphAgent({ graph: workflow });
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Span Hierarchy Example
|
|
136
|
+
|
|
137
|
+
When using LangGraph with AG-Kit server:
|
|
138
|
+
|
|
139
|
+
```
|
|
140
|
+
AG-UI.Server (Request entry point)
|
|
141
|
+
└─ Adapter.LangGraph (Agent adapter layer)
|
|
142
|
+
├─ node_a (LangGraph node)
|
|
143
|
+
│ └─ ChatOpenAI (LLM call)
|
|
144
|
+
├─ node_b (LangGraph node)
|
|
145
|
+
│ ├─ ChatOpenAI (LLM call)
|
|
146
|
+
│ └─ calculator (Tool call)
|
|
147
|
+
└─ synthesizer (LangGraph node)
|
|
148
|
+
└─ ChatOpenAI (LLM call)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
**Internal spans:** LangGraph also emits internal spans like `__start__` and `ChannelWrite<...>` which are tagged with `metadata.tags: ["langsmith:hidden"]`. These can be filtered in visualization tools if desired.
|
|
152
|
+
|
|
153
|
+
## OpenInference Semantic Conventions
|
|
154
|
+
|
|
155
|
+
This package follows [OpenInference](https://github.com/Arize-ai/openinference) standards:
|
|
156
|
+
|
|
157
|
+
| Span Kind | Attribute | Description |
|
|
158
|
+
|-----------|-----------|-------------|
|
|
159
|
+
| `SPAN` | `openinference.span.kind: SPAN` | General operations |
|
|
160
|
+
| `CHAIN` | `openinference.span.kind: CHAIN` | Chain/workflow spans |
|
|
161
|
+
| `LLM` | `openinference.span.kind: LLM` | LLM calls |
|
|
162
|
+
| `TOOL` | `openinference.span.kind: TOOL` | Tool/function calls |
|
|
163
|
+
| `AGENT` | `openinference.span.kind: AGENT` | Agent workflows |
|
|
164
|
+
|
|
165
|
+
### Key Attributes
|
|
166
|
+
|
|
167
|
+
- `input.value` / `output.value`: Message content
|
|
168
|
+
- `llm.model_name`: Model identifier
|
|
169
|
+
- `llm.token_count.prompt` / `llm.token_count.completion`: Token usage
|
|
170
|
+
- `tool.name`: Tool function name
|
|
171
|
+
|
|
172
|
+
## Exporters
|
|
173
|
+
|
|
174
|
+
### Console (Local Debug)
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
import { ConsoleSpanExporter } from '@opentelemetry/sdk-trace-base';
|
|
178
|
+
import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
|
|
179
|
+
|
|
180
|
+
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### OTLP (Langfuse, etc.)
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
|
|
187
|
+
|
|
188
|
+
const exporter = new OTLPTraceExporter({
|
|
189
|
+
url: 'http://localhost:3000/api/public/otel/v1/traces',
|
|
190
|
+
headers: {
|
|
191
|
+
'Authorization': `Basic ${credentials}`
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## API Reference
|
|
197
|
+
|
|
198
|
+
### Core Functions
|
|
199
|
+
|
|
200
|
+
- `startObservation(name, attributes, options)`: Create a new span
|
|
201
|
+
- `getActiveTraceId()`: Get current trace ID
|
|
202
|
+
- `getActiveSpanId()`: Get current span ID
|
|
203
|
+
|
|
204
|
+
### CallbackHandler
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
interface CallbackHandlerOptions {
|
|
208
|
+
adapterName: string; // e.g., 'LangGraph', 'LangChain'
|
|
209
|
+
traceMetadata?: Record<string, string>;
|
|
210
|
+
observationCallbacks?: (obs: any) => void;
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### Span Types
|
|
215
|
+
|
|
216
|
+
- `ObservationSpan`: General spans
|
|
217
|
+
- `ObservationLLM`: LLM generation spans
|
|
218
|
+
- `ObservationTool`: Tool invocation spans
|
|
219
|
+
- `ObservationChain`: Chain/workflow spans
|
|
220
|
+
- `ObservationAgent`: Agent spans
|
|
221
|
+
|
|
222
|
+
## Dependencies
|
|
223
|
+
|
|
224
|
+
### Peer Dependencies
|
|
225
|
+
|
|
226
|
+
- `@langchain/core`: For LangChain callback integration
|
|
227
|
+
|
|
228
|
+
### Dev Dependencies
|
|
229
|
+
|
|
230
|
+
- `@opentelemetry/sdk-trace-base`: For testing exporters
|
|
231
|
+
- `@opentelemetry/exporter-trace-otlp-http`: For OTLP export
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __esm = (fn, res) => function __init() {
|
|
6
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
7
|
+
};
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
|
+
|
|
22
|
+
export {
|
|
23
|
+
__esm,
|
|
24
|
+
__export,
|
|
25
|
+
__toCommonJS
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=chunk-NFEGQTCC.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|