network-ai 5.5.8 → 5.6.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.
@@ -0,0 +1,207 @@
1
+ "use strict";
2
+ /**
3
+ * Telemetry Provider — BYOT (Bring Your Own Telemetry) abstraction.
4
+ *
5
+ * Defines a minimal interface over distributed tracing providers such as
6
+ * OpenTelemetry, Datadog APM, Honeycomb, etc. Network-AI core never imports
7
+ * a concrete telemetry SDK — only this interface — preserving the zero-
8
+ * dependency BYOC design.
9
+ *
10
+ * ## Wiring into adapter lifecycle hooks
11
+ *
12
+ * ```typescript
13
+ * import { createOtelHooks, CapturingTelemetryProvider } from 'network-ai';
14
+ * import { AdapterHookManager } from 'network-ai';
15
+ *
16
+ * const provider = new CapturingTelemetryProvider(); // or your own impl
17
+ * const hookManager = new AdapterHookManager();
18
+ * createOtelHooks(provider).forEach(h => hookManager.register(h));
19
+ * ```
20
+ *
21
+ * ## Implementing for OpenTelemetry
22
+ *
23
+ * ```typescript
24
+ * import { trace, SpanStatusCode } from '@opentelemetry/api';
25
+ * import type { ITelemetryProvider, SpanAttributes } from 'network-ai';
26
+ *
27
+ * class OtelProvider implements ITelemetryProvider {
28
+ * private tracer = trace.getTracer('network-ai');
29
+ * private spans = new Map<string, Span>();
30
+ *
31
+ * startSpan(name: string, attrs: SpanAttributes = {}): string {
32
+ * const span = this.tracer.startSpan(name, { attributes: attrs as Attributes });
33
+ * const id = `span_${Date.now()}`;
34
+ * this.spans.set(id, span);
35
+ * return id;
36
+ * }
37
+ * endSpan(id: string, status: 'ok' | 'error'): void {
38
+ * const span = this.spans.get(id);
39
+ * if (!span) return;
40
+ * span.setStatus({ code: status === 'ok' ? SpanStatusCode.OK : SpanStatusCode.ERROR });
41
+ * span.end();
42
+ * this.spans.delete(id);
43
+ * }
44
+ * recordEvent(id: string, name: string, attrs: SpanAttributes = {}): void {
45
+ * this.spans.get(id)?.addEvent(name, attrs as Attributes);
46
+ * }
47
+ * }
48
+ * ```
49
+ */
50
+ Object.defineProperty(exports, "__esModule", { value: true });
51
+ exports.CapturingTelemetryProvider = exports.NullTelemetryProvider = void 0;
52
+ exports.createOtelHooks = createOtelHooks;
53
+ // ============================================================================
54
+ // NULL PROVIDER (default — zero overhead when no telemetry is configured)
55
+ // ============================================================================
56
+ /**
57
+ * No-op implementation. Used as the default when no telemetry provider is
58
+ * supplied so the instrumentation path compiles to a handful of dead calls
59
+ * that the JIT eliminates.
60
+ */
61
+ class NullTelemetryProvider {
62
+ /** @inheritdoc */
63
+ startSpan(_name, _attributes) { return ''; }
64
+ /** @inheritdoc */
65
+ endSpan(_spanId, _status, _attributes) { }
66
+ /** @inheritdoc */
67
+ recordEvent(_spanId, _name, _attributes) { }
68
+ }
69
+ exports.NullTelemetryProvider = NullTelemetryProvider;
70
+ // ============================================================================
71
+ // CAPTURING PROVIDER (for testing)
72
+ // ============================================================================
73
+ /**
74
+ * In-memory provider that stores every span and event for test assertions.
75
+ *
76
+ * @example
77
+ * ```typescript
78
+ * const provider = new CapturingTelemetryProvider();
79
+ * createOtelHooks(provider).forEach(h => hookManager.register(h));
80
+ *
81
+ * await registry.executeAgent('agent:foo', payload, ctx);
82
+ *
83
+ * const span = provider.spans.find(s => s.name === 'adapter.execute');
84
+ * expect(span?.status).toBe('ok');
85
+ * ```
86
+ */
87
+ class CapturingTelemetryProvider {
88
+ /** All spans created since construction or last `clear()`. */
89
+ spans = [];
90
+ counter = 0;
91
+ /** @inheritdoc */
92
+ startSpan(name, attributes = {}) {
93
+ const spanId = `span_${++this.counter}`;
94
+ this.spans.push({ spanId, name, attributes: { ...attributes }, startedAt: Date.now(), events: [] });
95
+ return spanId;
96
+ }
97
+ /** @inheritdoc */
98
+ endSpan(spanId, status, attributes = {}) {
99
+ const span = this.spans.find(s => s.spanId === spanId);
100
+ if (!span)
101
+ return;
102
+ span.endedAt = Date.now();
103
+ span.status = status;
104
+ Object.assign(span.attributes, attributes);
105
+ }
106
+ /** @inheritdoc */
107
+ recordEvent(spanId, name, attributes = {}) {
108
+ const span = this.spans.find(s => s.spanId === spanId);
109
+ if (!span)
110
+ return;
111
+ span.events.push({ name, attributes: { ...attributes }, ts: Date.now() });
112
+ }
113
+ /** Clear all captured data and reset span counter. */
114
+ clear() {
115
+ this.spans.length = 0;
116
+ this.counter = 0;
117
+ }
118
+ }
119
+ exports.CapturingTelemetryProvider = CapturingTelemetryProvider;
120
+ // ============================================================================
121
+ // HOOK FACTORY
122
+ // ============================================================================
123
+ /** Metadata key used to propagate spanId through `HookContext`. @internal */
124
+ const SPAN_ID_META_KEY = '_otelSpanId';
125
+ /**
126
+ * Create a set of `ExecutionHook` objects that emit traces to `provider`.
127
+ *
128
+ * Register the returned hooks with an `AdapterHookManager`:
129
+ * ```typescript
130
+ * createOtelHooks(provider).forEach(h => hookManager.register(h));
131
+ * ```
132
+ *
133
+ * Three hooks are created — one per `HookPhase`:
134
+ * - `otel:beforeExecute` — calls `provider.startSpan('adapter.execute', {...})`
135
+ * - `otel:afterExecute` — calls `provider.endSpan(spanId, 'ok')`
136
+ * - `otel:onError` — calls `provider.endSpan(spanId, 'error')`
137
+ *
138
+ * Each hook has `priority: 100` so it runs before most user-defined hooks.
139
+ * The spanId is stored in `ctx.metadata._otelSpanId` for downstream hooks
140
+ * that wish to add their own `recordEvent` calls.
141
+ *
142
+ * **Permission check semantics:** `beforeExecute` fires once when execution
143
+ * begins — not per streaming chunk — matching the documented "once at start"
144
+ * semantics of `StreamingBaseAdapter`.
145
+ */
146
+ function createOtelHooks(provider) {
147
+ return [
148
+ {
149
+ name: 'otel:beforeExecute',
150
+ phase: 'beforeExecute',
151
+ priority: 100,
152
+ handler(ctx) {
153
+ try {
154
+ const spanId = provider.startSpan('adapter.execute', {
155
+ agentId: ctx.agentId,
156
+ action: ctx.payload.action ?? '',
157
+ depth: ctx.depth,
158
+ });
159
+ ctx.metadata[SPAN_ID_META_KEY] = spanId;
160
+ }
161
+ catch {
162
+ // Telemetry must never break execution
163
+ }
164
+ return ctx;
165
+ },
166
+ },
167
+ {
168
+ name: 'otel:afterExecute',
169
+ phase: 'afterExecute',
170
+ priority: 100,
171
+ handler(ctx) {
172
+ try {
173
+ const spanId = ctx.metadata[SPAN_ID_META_KEY];
174
+ if (spanId) {
175
+ provider.endSpan(spanId, 'ok', {
176
+ success: ctx.result?.success === true,
177
+ });
178
+ }
179
+ }
180
+ catch {
181
+ // no-op
182
+ }
183
+ return ctx;
184
+ },
185
+ },
186
+ {
187
+ name: 'otel:onError',
188
+ phase: 'onError',
189
+ priority: 100,
190
+ handler(ctx) {
191
+ try {
192
+ const spanId = ctx.metadata[SPAN_ID_META_KEY];
193
+ if (spanId) {
194
+ provider.endSpan(spanId, 'error', {
195
+ errorMessage: ctx.error?.message ?? 'unknown',
196
+ });
197
+ }
198
+ }
199
+ catch {
200
+ // no-op
201
+ }
202
+ return ctx;
203
+ },
204
+ },
205
+ ];
206
+ }
207
+ //# sourceMappingURL=telemetry-provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"telemetry-provider.js","sourceRoot":"","sources":["../../lib/telemetry-provider.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;;;AAsKH,0CAyDC;AA5JD,+EAA+E;AAC/E,0EAA0E;AAC1E,+EAA+E;AAE/E;;;;GAIG;AACH,MAAa,qBAAqB;IAChC,kBAAkB;IAClB,SAAS,CAAC,KAAa,EAAE,WAA4B,IAAY,OAAO,EAAE,CAAC,CAAC,CAAC;IAC7E,kBAAkB;IAClB,OAAO,CAAC,OAAe,EAAE,OAAuB,EAAE,WAA4B,IAAS,CAAC;IACxF,kBAAkB;IAClB,WAAW,CAAC,OAAe,EAAE,KAAa,EAAE,WAA4B,IAAS,CAAC;CACnF;AAPD,sDAOC;AAED,+EAA+E;AAC/E,mCAAmC;AACnC,+EAA+E;AAE/E;;;;;;;;;;;;;GAaG;AACH,MAAa,0BAA0B;IACr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gBACH,CAAC;gBAAC,MAAM,CAAC;oBACP,QAAQ;gBACV,CAAC;gBACD,OAAO,GAAG,CAAC;YACb,CAAC;SACF;QACD;YACE,IAAI,EAAE,cAAc;YACpB,KAAK,EAAE,SAAS;YAChB,QAAQ,EAAE,GAAG;YACb,OAAO,CAAC,GAAgB;gBACtB,IAAI,CAAC;oBACH,MAAM,MAAM,GAAG,GAAG,CAAC,QAAQ,CAAC,gBAAgB,CAAuB,CAAC;oBACpE,IAAI,MAAM,EAAE,CAAC;wBACX,QAAQ,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE;4BAChC,YAAY,EAAG,GAAG,CAAC,KAA2B,EAAE,OAAO,IAAI,SAAS;yBACrE,CAAC,CAAC;oBACL,CAAC;gBACH,CAAC;gBAAC,MAAM,CAAC;oBACP,QAAQ;gBACV,CAAC;gBACD,OAAO,GAAG,CAAC;YACb,CAAC;SACF;KACF,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "network-ai",
3
- "version": "5.5.8",
3
+ "version": "5.6.0",
4
4
  "description": "AI agent orchestration framework for TypeScript/Node.js - 29 adapters (LangChain, AutoGen, CrewAI, OpenAI Assistants, LlamaIndex, Semantic Kernel, Haystack, DSPy, Agno, MCP, OpenClaw, A2A, Codex, MiniMax, NemoClaw, APS, Copilot, LangGraph, Anthropic Computer Use, OpenAI Agents SDK, Vertex AI, Pydantic AI, Browser Agent, Hermes, Orchestrator, RLM + streaming variants). Built-in CLI, security, swarm intelligence, real-time streaming, and agentic workflow patterns.",
5
5
  "homepage": "https://network-ai.org",
6
6
  "main": "dist/index.js",
@@ -102,9 +102,9 @@
102
102
  "python": ">=3.9"
103
103
  },
104
104
  "devDependencies": {
105
- "@types/node": "^25.7.0",
105
+ "@types/node": "^25.9.0",
106
106
  "dotenv": "^17.4.2",
107
- "openai": "^6.37.0",
107
+ "openai": "^6.38.0",
108
108
  "ts-node": "^10.9.2",
109
109
  "typescript": "^6.0.3"
110
110
  },
@@ -238,7 +238,10 @@ export type AdapterEventType =
238
238
  | 'agent:execution:retry'
239
239
  | 'agent:execution:fallback'
240
240
  | 'agent:discovered'
241
- | 'agent:unavailable';
241
+ | 'agent:unavailable'
242
+ | 'circuit:open'
243
+ | 'circuit:half-open'
244
+ | 'circuit:close';
242
245
 
243
246
  export interface AdapterEvent {
244
247
  type: AdapterEventType;