@revealui/ai 0.7.0 → 0.8.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 +25 -12
- package/dist/client/hooks/useAgentStream.d.ts.map +1 -1
- package/dist/client/hooks/useAgentStream.js +1 -29
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/inference/context-assembly.d.ts.map +1 -1
- package/dist/inference/context-assembly.js +1 -3
- package/dist/inference/overflow-compressor.d.ts.map +1 -1
- package/dist/inference/overflow-compressor.js +1 -3
- package/dist/ingestion/pipeline.d.ts.map +1 -1
- package/dist/ingestion/pipeline.js +1 -3
- package/dist/ingestion/text-splitter.d.ts.map +1 -1
- package/dist/ingestion/text-splitter.js +1 -3
- package/dist/internal/token-estimate.d.ts +6 -0
- package/dist/internal/token-estimate.d.ts.map +1 -0
- package/dist/internal/token-estimate.js +7 -0
- package/dist/llm/client.d.ts +6 -4
- package/dist/llm/client.d.ts.map +1 -1
- package/dist/llm/client.js +25 -5
- package/dist/llm/local-ai-profile.d.ts +39 -0
- package/dist/llm/local-ai-profile.d.ts.map +1 -0
- package/dist/llm/local-ai-profile.js +165 -0
- package/dist/llm/providers/inference-snaps.d.ts +24 -13
- package/dist/llm/providers/inference-snaps.d.ts.map +1 -1
- package/dist/llm/providers/inference-snaps.js +21 -17
- package/dist/llm/providers/us-origin-snaps.d.ts +79 -0
- package/dist/llm/providers/us-origin-snaps.d.ts.map +1 -0
- package/dist/llm/providers/us-origin-snaps.js +108 -0
- package/dist/llm/providers/xai.d.ts +29 -0
- package/dist/llm/providers/xai.d.ts.map +1 -0
- package/dist/llm/providers/xai.js +44 -0
- package/dist/observability/export.d.ts +78 -0
- package/dist/observability/export.d.ts.map +1 -0
- package/dist/observability/export.js +229 -0
- package/dist/observability/index.d.ts +19 -0
- package/dist/observability/index.d.ts.map +1 -0
- package/dist/observability/index.js +16 -0
- package/dist/observability/instrumentation.d.ts +68 -0
- package/dist/observability/instrumentation.d.ts.map +1 -0
- package/dist/observability/instrumentation.js +219 -0
- package/dist/observability/storage.d.ts +39 -0
- package/dist/observability/storage.d.ts.map +1 -0
- package/dist/observability/storage.js +184 -0
- package/dist/orchestration/runtime.d.ts +7 -0
- package/dist/orchestration/runtime.d.ts.map +1 -1
- package/dist/orchestration/runtime.js +9 -2
- package/dist/orchestration/ticket-agent.d.ts +10 -0
- package/dist/orchestration/ticket-agent.d.ts.map +1 -1
- package/dist/orchestration/ticket-agent.js +7 -1
- package/dist/skills/index.d.ts +4 -0
- package/dist/skills/index.d.ts.map +1 -1
- package/dist/skills/index.js +4 -0
- package/dist/skills/integration/agent-skill-provider.d.ts +0 -5
- package/dist/skills/integration/agent-skill-provider.d.ts.map +1 -1
- package/dist/skills/integration/agent-skill-provider.js +3 -9
- package/dist/tools/admin/factory.d.ts +6 -0
- package/dist/tools/admin/factory.d.ts.map +1 -1
- package/dist/tools/admin/factory.js +3 -2
- package/dist/tools/coding/index.d.ts +6 -0
- package/dist/tools/coding/index.d.ts.map +1 -1
- package/dist/tools/coding/index.js +7 -2
- package/dist/tools/governance.d.ts +37 -0
- package/dist/tools/governance.d.ts.map +1 -0
- package/dist/tools/governance.js +42 -0
- package/dist/tools/integrity-audit.d.ts +27 -0
- package/dist/tools/integrity-audit.d.ts.map +1 -0
- package/dist/tools/integrity-audit.js +64 -0
- package/dist/tools/mcp-adapter.d.ts +23 -3
- package/dist/tools/mcp-adapter.d.ts.map +1 -1
- package/dist/tools/mcp-adapter.js +189 -132
- package/dist/tools/mcp-sampling.d.ts +3 -3
- package/dist/tools/mcp-sampling.js +3 -3
- package/dist/tools/web/duck-duck-go.d.ts.map +1 -1
- package/dist/tools/web/duck-duck-go.js +1 -4
- package/dist/tools/web/exa.d.ts.map +1 -1
- package/dist/tools/web/exa.js +1 -4
- package/dist/tools/web/tavily.d.ts.map +1 -1
- package/dist/tools/web/tavily.js +1 -4
- package/package.json +69 -34
|
@@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B,GAAG,MAAM;IAa7D;;OAEG;IACH,cAAc,CAAC,MAAM,EAAE,WAAW,EAAE,MAAM,GAAE,MAAM,GAAG,KAAK,GAAG,QAAiB,GAAG,MAAM;IAgBvF;;OAEG;IACG,YAAY,CAChB,QAAQ,EAAE,MAAM,EAChB,MAAM,GAAE,MAAM,GAAG,KAAK,GAAG,QAAiB,EAC1C,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,IAAI,CAAC;IAKhB;;OAEG;IACH,QAAQ,CACN,QAAQ,EAAE,MAAM,EAChB,MAAM,GAAE,MAAM,GAAG,KAAK,GAAG,QAAiB,EAC1C,MAAM,CAAC,EAAE,WAAW,GACnB,IAAI;IAKP;;OAEG;IACH,eAAe,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,GAAE,MAAM,GAAG,KAAK,GAAG,QAAiB,GAAG,MAAM;IAU3F;;OAEG;IACH,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,GAAE,MAAM,GAAG,KAAK,GAAG,QAAiB,GAAG,MAAM;IAIlF;;OAEG;IACH,eAAe,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,GAAE,MAAM,GAAG,KAAK,GAAG,QAAiB,GAAG,MAAM;IAItF;;OAEG;IACH,YAAY,CAAC,MAAM,GAAE,MAAM,GAAG,KAAK,GAAG,QAAiB,GAAG,MAAM;IAIhE;;OAEG;IACH,cAAc,CAAC,MAAM,GAAE,MAAM,GAAG,KAAK,GAAG,QAAiB,GAAG,MAAM;CAGnE"}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @revealui/ai - Export Utilities
|
|
3
|
+
*
|
|
4
|
+
* Export agent events to various formats (JSON, CSV, NDJSON).
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Export events to JSON format
|
|
8
|
+
*/
|
|
9
|
+
export function exportToJSON(events, options) {
|
|
10
|
+
const data = options?.metadata
|
|
11
|
+
? {
|
|
12
|
+
metadata: {
|
|
13
|
+
exportedAt: new Date().toISOString(),
|
|
14
|
+
eventCount: events.length,
|
|
15
|
+
...options.metadata,
|
|
16
|
+
},
|
|
17
|
+
events,
|
|
18
|
+
}
|
|
19
|
+
: events;
|
|
20
|
+
return options?.pretty ? JSON.stringify(data, null, 2) : JSON.stringify(data);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Export events to CSV format
|
|
24
|
+
*/
|
|
25
|
+
export function exportToCSV(events) {
|
|
26
|
+
if (events.length === 0) {
|
|
27
|
+
return '';
|
|
28
|
+
}
|
|
29
|
+
// Define common columns
|
|
30
|
+
const columns = [
|
|
31
|
+
'timestamp',
|
|
32
|
+
'eventType',
|
|
33
|
+
'agentId',
|
|
34
|
+
'sessionId',
|
|
35
|
+
'taskId',
|
|
36
|
+
// Decision-specific
|
|
37
|
+
'reasoning',
|
|
38
|
+
'chosenTool',
|
|
39
|
+
'confidence',
|
|
40
|
+
// Tool call-specific
|
|
41
|
+
'toolName',
|
|
42
|
+
'success',
|
|
43
|
+
'durationMs',
|
|
44
|
+
'errorMessage',
|
|
45
|
+
// LLM call-specific
|
|
46
|
+
'provider',
|
|
47
|
+
'model',
|
|
48
|
+
'promptTokens',
|
|
49
|
+
'completionTokens',
|
|
50
|
+
'cost',
|
|
51
|
+
'cacheHit',
|
|
52
|
+
// Error-specific
|
|
53
|
+
'message',
|
|
54
|
+
'recoverable',
|
|
55
|
+
'errorCode',
|
|
56
|
+
];
|
|
57
|
+
// Create CSV header
|
|
58
|
+
const header = columns.join(',');
|
|
59
|
+
// Create CSV rows
|
|
60
|
+
const rows = events.map((event) => {
|
|
61
|
+
const row = [];
|
|
62
|
+
for (const column of columns) {
|
|
63
|
+
const value = event[column];
|
|
64
|
+
if (value === undefined || value === null) {
|
|
65
|
+
row.push('');
|
|
66
|
+
}
|
|
67
|
+
else if (typeof value === 'string' && (value.includes(',') || value.includes('"'))) {
|
|
68
|
+
// Escape strings with commas or quotes
|
|
69
|
+
row.push(`"${value.replace(/"/g, '""')}"`);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
row.push(String(value));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return row.join(',');
|
|
76
|
+
});
|
|
77
|
+
return [header, ...rows].join('\n');
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Export events to NDJSON format (newline-delimited JSON)
|
|
81
|
+
*/
|
|
82
|
+
export function exportToNDJSON(events) {
|
|
83
|
+
return events.map((event) => JSON.stringify(event)).join('\n');
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Export events to a file (Node.js)
|
|
87
|
+
*/
|
|
88
|
+
export async function exportToFile(events, filePath, format = 'json') {
|
|
89
|
+
const fs = await import('node:fs/promises');
|
|
90
|
+
let content;
|
|
91
|
+
switch (format) {
|
|
92
|
+
case 'json':
|
|
93
|
+
content = exportToJSON(events, { pretty: true });
|
|
94
|
+
break;
|
|
95
|
+
case 'csv':
|
|
96
|
+
content = exportToCSV(events);
|
|
97
|
+
break;
|
|
98
|
+
case 'ndjson':
|
|
99
|
+
content = exportToNDJSON(events);
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
await fs.writeFile(filePath, content, 'utf-8');
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Create a downloadable file blob (Browser)
|
|
106
|
+
*/
|
|
107
|
+
export function createDownloadableBlob(events, format = 'json') {
|
|
108
|
+
let content;
|
|
109
|
+
let mimeType;
|
|
110
|
+
switch (format) {
|
|
111
|
+
case 'json':
|
|
112
|
+
content = exportToJSON(events, { pretty: true });
|
|
113
|
+
mimeType = 'application/json';
|
|
114
|
+
break;
|
|
115
|
+
case 'csv':
|
|
116
|
+
content = exportToCSV(events);
|
|
117
|
+
mimeType = 'text/csv';
|
|
118
|
+
break;
|
|
119
|
+
case 'ndjson':
|
|
120
|
+
content = exportToNDJSON(events);
|
|
121
|
+
mimeType = 'application/x-ndjson';
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
return new Blob([content], { type: mimeType });
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Trigger download in browser
|
|
128
|
+
*/
|
|
129
|
+
export function downloadEvents(events, filename, format = 'json') {
|
|
130
|
+
if (typeof window === 'undefined') {
|
|
131
|
+
throw new Error('downloadEvents can only be used in browser environments');
|
|
132
|
+
}
|
|
133
|
+
const blob = createDownloadableBlob(events, format);
|
|
134
|
+
const url = URL.createObjectURL(blob);
|
|
135
|
+
const a = document.createElement('a');
|
|
136
|
+
a.href = url;
|
|
137
|
+
a.download = filename;
|
|
138
|
+
document.body.appendChild(a);
|
|
139
|
+
a.click();
|
|
140
|
+
document.body.removeChild(a);
|
|
141
|
+
URL.revokeObjectURL(url);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Export helper class with logger integration
|
|
145
|
+
*/
|
|
146
|
+
export class EventExporter {
|
|
147
|
+
logger;
|
|
148
|
+
constructor(logger) {
|
|
149
|
+
this.logger = logger;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Export all events
|
|
153
|
+
*/
|
|
154
|
+
exportAll(format = 'json') {
|
|
155
|
+
const events = this.logger.getEvents();
|
|
156
|
+
switch (format) {
|
|
157
|
+
case 'json':
|
|
158
|
+
return exportToJSON(events, { pretty: true });
|
|
159
|
+
case 'csv':
|
|
160
|
+
return exportToCSV(events);
|
|
161
|
+
case 'ndjson':
|
|
162
|
+
return exportToNDJSON(events);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Export filtered events
|
|
167
|
+
*/
|
|
168
|
+
exportFiltered(filter, format = 'json') {
|
|
169
|
+
const events = this.logger.getEvents(filter);
|
|
170
|
+
switch (format) {
|
|
171
|
+
case 'json':
|
|
172
|
+
return exportToJSON(events, {
|
|
173
|
+
pretty: true,
|
|
174
|
+
metadata: { filter },
|
|
175
|
+
});
|
|
176
|
+
case 'csv':
|
|
177
|
+
return exportToCSV(events);
|
|
178
|
+
case 'ndjson':
|
|
179
|
+
return exportToNDJSON(events);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Export to file (Node.js)
|
|
184
|
+
*/
|
|
185
|
+
async exportToFile(filePath, format = 'json', filter) {
|
|
186
|
+
const events = filter ? this.logger.getEvents(filter) : this.logger.getEvents();
|
|
187
|
+
await exportToFile(events, filePath, format);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Download in browser
|
|
191
|
+
*/
|
|
192
|
+
download(filename, format = 'json', filter) {
|
|
193
|
+
const events = filter ? this.logger.getEvents(filter) : this.logger.getEvents();
|
|
194
|
+
downloadEvents(events, filename, format);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Export by time range
|
|
198
|
+
*/
|
|
199
|
+
exportTimeRange(start, end, format = 'json') {
|
|
200
|
+
return this.exportFiltered({
|
|
201
|
+
startTime: start.getTime(),
|
|
202
|
+
endTime: end.getTime(),
|
|
203
|
+
}, format);
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Export by agent
|
|
207
|
+
*/
|
|
208
|
+
exportByAgent(agentId, format = 'json') {
|
|
209
|
+
return this.exportFiltered({ agentId }, format);
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Export by session
|
|
213
|
+
*/
|
|
214
|
+
exportBySession(sessionId, format = 'json') {
|
|
215
|
+
return this.exportFiltered({ sessionId }, format);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Export errors only
|
|
219
|
+
*/
|
|
220
|
+
exportErrors(format = 'json') {
|
|
221
|
+
return this.exportFiltered({ eventType: 'error' }, format);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Export LLM calls only
|
|
225
|
+
*/
|
|
226
|
+
exportLLMCalls(format = 'json') {
|
|
227
|
+
return this.exportFiltered({ eventType: 'llm_call' }, format);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @revealui/ai - Observability
|
|
3
|
+
*
|
|
4
|
+
* **INCUBATING (fleet-redundancy C11 / ADR-007).** Agent event/metrics helpers
|
|
5
|
+
* are available as a library. No RevealUI app currently mounts this surface as
|
|
6
|
+
* the product observability path; prefer explicit wiring under a WIRE ticket.
|
|
7
|
+
*
|
|
8
|
+
* Agent operation tracking, logging, and metrics collection.
|
|
9
|
+
*/
|
|
10
|
+
export { createDownloadableBlob, downloadEvents, EventExporter, exportToCSV, exportToFile, exportToJSON, exportToNDJSON, } from './export.js';
|
|
11
|
+
export type { LLMCallOptions, LLMResponse } from './instrumentation.js';
|
|
12
|
+
export { instrumentAgent, instrumentLLMCall, instrumentTaskExecution, instrumentTool, LLMCostCalculators, logTaskDelegation, } from './instrumentation.js';
|
|
13
|
+
export { AgentEventLogger } from './logger.js';
|
|
14
|
+
export { AgentMetricsCollector } from './metrics.js';
|
|
15
|
+
export { AgentEventQuery } from './query.js';
|
|
16
|
+
export { FileSystemEventStorage, LocalStorageEventStorage, MemoryEventStorage, } from './storage.js';
|
|
17
|
+
export type { AgentEvent, AgentMetrics, DecisionEvent, ErrorEvent, EventFilter, EventStorage, LLMCallEvent, MetricsSummary, ToolCallEvent, } from './types.js';
|
|
18
|
+
export * from './types.js';
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/observability/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EACL,sBAAsB,EACtB,cAAc,EACd,aAAa,EACb,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,cAAc,GACf,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACxE,OAAO,EACL,eAAe,EACf,iBAAiB,EACjB,uBAAuB,EACvB,cAAc,EACd,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EACL,sBAAsB,EACtB,wBAAwB,EACxB,kBAAkB,GACnB,MAAM,cAAc,CAAC;AAGtB,YAAY,EACV,UAAU,EACV,YAAY,EACZ,aAAa,EACb,UAAU,EACV,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,cAAc,EACd,aAAa,GACd,MAAM,YAAY,CAAC;AACpB,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @revealui/ai - Observability
|
|
3
|
+
*
|
|
4
|
+
* **INCUBATING (fleet-redundancy C11 / ADR-007).** Agent event/metrics helpers
|
|
5
|
+
* are available as a library. No RevealUI app currently mounts this surface as
|
|
6
|
+
* the product observability path; prefer explicit wiring under a WIRE ticket.
|
|
7
|
+
*
|
|
8
|
+
* Agent operation tracking, logging, and metrics collection.
|
|
9
|
+
*/
|
|
10
|
+
export { createDownloadableBlob, downloadEvents, EventExporter, exportToCSV, exportToFile, exportToJSON, exportToNDJSON, } from './export.js';
|
|
11
|
+
export { instrumentAgent, instrumentLLMCall, instrumentTaskExecution, instrumentTool, LLMCostCalculators, logTaskDelegation, } from './instrumentation.js';
|
|
12
|
+
export { AgentEventLogger } from './logger.js';
|
|
13
|
+
export { AgentMetricsCollector } from './metrics.js';
|
|
14
|
+
export { AgentEventQuery } from './query.js';
|
|
15
|
+
export { FileSystemEventStorage, LocalStorageEventStorage, MemoryEventStorage, } from './storage.js';
|
|
16
|
+
export * from './types.js';
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @revealui/ai - Instrumentation Utilities
|
|
3
|
+
*
|
|
4
|
+
* Helper functions and wrappers for instrumenting agent operations with observability.
|
|
5
|
+
*/
|
|
6
|
+
import type { Agent, AgentResult, Task } from '../orchestration/agent.js';
|
|
7
|
+
import type { Tool } from '../tools/base.js';
|
|
8
|
+
import type { AgentEventLogger } from './logger.js';
|
|
9
|
+
/**
|
|
10
|
+
* Wrap a tool execution with observability logging
|
|
11
|
+
*/
|
|
12
|
+
export declare function instrumentTool(tool: Tool, logger: AgentEventLogger, agentId: string, sessionId: string): Tool;
|
|
13
|
+
/**
|
|
14
|
+
* Wrap an agent with observability logging
|
|
15
|
+
*/
|
|
16
|
+
export declare function instrumentAgent(agent: Agent, logger: AgentEventLogger): Agent & {
|
|
17
|
+
logger: AgentEventLogger;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Instrument a task delegation decision
|
|
21
|
+
*/
|
|
22
|
+
export declare function logTaskDelegation(logger: AgentEventLogger, task: Task, selectedAgent: Agent, reasoning: string, confidence?: number): void;
|
|
23
|
+
/**
|
|
24
|
+
* Instrument task execution
|
|
25
|
+
*/
|
|
26
|
+
export declare function instrumentTaskExecution<T extends AgentResult>(logger: AgentEventLogger, task: Task, agentId: string, executor: () => Promise<T>): Promise<T>;
|
|
27
|
+
/**
|
|
28
|
+
* Instrument LLM call with token counting
|
|
29
|
+
*/
|
|
30
|
+
export interface LLMCallOptions {
|
|
31
|
+
provider: string;
|
|
32
|
+
model: string;
|
|
33
|
+
agentId: string;
|
|
34
|
+
sessionId: string;
|
|
35
|
+
taskId?: string;
|
|
36
|
+
}
|
|
37
|
+
export interface LLMResponse {
|
|
38
|
+
content: string;
|
|
39
|
+
usage: {
|
|
40
|
+
promptTokens: number;
|
|
41
|
+
completionTokens: number;
|
|
42
|
+
totalTokens: number;
|
|
43
|
+
};
|
|
44
|
+
cacheHit?: boolean;
|
|
45
|
+
}
|
|
46
|
+
export declare function instrumentLLMCall<T extends LLMResponse>(logger: AgentEventLogger, options: LLMCallOptions, executor: () => Promise<T>, costCalculator?: (usage: T['usage']) => number): Promise<T>;
|
|
47
|
+
/**
|
|
48
|
+
* Cost calculators for popular LLM providers
|
|
49
|
+
*/
|
|
50
|
+
export declare const LLMCostCalculators: {
|
|
51
|
+
/**
|
|
52
|
+
* OpenAI cost calculator
|
|
53
|
+
* Prices as of 2024 (per 1M tokens)
|
|
54
|
+
*/
|
|
55
|
+
openai: (model: string, usage: {
|
|
56
|
+
promptTokens: number;
|
|
57
|
+
completionTokens: number;
|
|
58
|
+
}) => number;
|
|
59
|
+
/**
|
|
60
|
+
* Anthropic cost calculator
|
|
61
|
+
* Prices as of 2024 (per 1M tokens)
|
|
62
|
+
*/
|
|
63
|
+
anthropic: (model: string, usage: {
|
|
64
|
+
promptTokens: number;
|
|
65
|
+
completionTokens: number;
|
|
66
|
+
}) => number;
|
|
67
|
+
};
|
|
68
|
+
//# sourceMappingURL=instrumentation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"instrumentation.d.ts","sourceRoot":"","sources":["../../src/observability/instrumentation.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,2BAA2B,CAAC;AAC1E,OAAO,KAAK,EAAE,IAAI,EAAc,MAAM,kBAAkB,CAAC;AACzD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpD;;GAEG;AACH,wBAAgB,cAAc,CAC5B,IAAI,EAAE,IAAI,EACV,MAAM,EAAE,gBAAgB,EACxB,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,MAAM,GAChB,IAAI,CAuDN;AAED;;GAEG;AACH,wBAAgB,eAAe,CAC7B,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,gBAAgB,GACvB,KAAK,GAAG;IAAE,MAAM,EAAE,gBAAgB,CAAA;CAAE,CAOtC;AA4BD;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,gBAAgB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BAAgB,EAAE,MAAM,CAAA;KAAE;CAarF,CAAC"}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @revealui/ai - Instrumentation Utilities
|
|
3
|
+
*
|
|
4
|
+
* Helper functions and wrappers for instrumenting agent operations with observability.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Wrap a tool execution with observability logging
|
|
8
|
+
*/
|
|
9
|
+
export function instrumentTool(tool, logger, agentId, sessionId) {
|
|
10
|
+
return {
|
|
11
|
+
...tool,
|
|
12
|
+
execute: async (params) => {
|
|
13
|
+
const startTime = Date.now();
|
|
14
|
+
try {
|
|
15
|
+
const result = await tool.execute(params);
|
|
16
|
+
const durationMs = Date.now() - startTime;
|
|
17
|
+
// Log tool call
|
|
18
|
+
logger.logToolCall({
|
|
19
|
+
timestamp: Date.now(),
|
|
20
|
+
agentId,
|
|
21
|
+
sessionId,
|
|
22
|
+
toolName: tool.name,
|
|
23
|
+
params: params,
|
|
24
|
+
result: result.data,
|
|
25
|
+
durationMs,
|
|
26
|
+
success: result.success,
|
|
27
|
+
errorMessage: result.error,
|
|
28
|
+
});
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
const durationMs = Date.now() - startTime;
|
|
33
|
+
// Log failed tool call
|
|
34
|
+
logger.logToolCall({
|
|
35
|
+
timestamp: Date.now(),
|
|
36
|
+
agentId,
|
|
37
|
+
sessionId,
|
|
38
|
+
toolName: tool.name,
|
|
39
|
+
params: params,
|
|
40
|
+
durationMs,
|
|
41
|
+
success: false,
|
|
42
|
+
errorMessage: error instanceof Error ? error.message : String(error),
|
|
43
|
+
});
|
|
44
|
+
// Log error event
|
|
45
|
+
logger.logError({
|
|
46
|
+
timestamp: Date.now(),
|
|
47
|
+
agentId,
|
|
48
|
+
sessionId,
|
|
49
|
+
message: `Tool execution failed: ${tool.name}`,
|
|
50
|
+
stack: error instanceof Error ? error.stack : undefined,
|
|
51
|
+
recoverable: true,
|
|
52
|
+
errorCode: 'TOOL_EXECUTION_ERROR',
|
|
53
|
+
context: { toolName: tool.name, params },
|
|
54
|
+
});
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Wrap an agent with observability logging
|
|
62
|
+
*/
|
|
63
|
+
export function instrumentAgent(agent, logger) {
|
|
64
|
+
return {
|
|
65
|
+
...agent,
|
|
66
|
+
logger,
|
|
67
|
+
// Agent interface methods would be instrumented here
|
|
68
|
+
// This is a placeholder as the Agent interface may vary
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function taskSessionId(task) {
|
|
72
|
+
const sessionId = task.sessionId;
|
|
73
|
+
return typeof sessionId === 'string' && sessionId.length > 0 ? sessionId : 'session';
|
|
74
|
+
}
|
|
75
|
+
function taskInputParams(task) {
|
|
76
|
+
const extended = task;
|
|
77
|
+
if (extended.input !== undefined &&
|
|
78
|
+
extended.input !== null &&
|
|
79
|
+
typeof extended.input === 'object' &&
|
|
80
|
+
!Array.isArray(extended.input)) {
|
|
81
|
+
return extended.input;
|
|
82
|
+
}
|
|
83
|
+
return (task.parameters ?? {});
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Instrument a task delegation decision
|
|
87
|
+
*/
|
|
88
|
+
export function logTaskDelegation(logger, task, selectedAgent, reasoning, confidence) {
|
|
89
|
+
logger.logDecision({
|
|
90
|
+
timestamp: Date.now(),
|
|
91
|
+
agentId: selectedAgent.id,
|
|
92
|
+
sessionId: taskSessionId(task),
|
|
93
|
+
taskId: task.id,
|
|
94
|
+
reasoning,
|
|
95
|
+
chosenTool: selectedAgent.name,
|
|
96
|
+
confidence,
|
|
97
|
+
context: {
|
|
98
|
+
taskType: task.type,
|
|
99
|
+
taskDescription: task.description,
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Instrument task execution
|
|
105
|
+
*/
|
|
106
|
+
export async function instrumentTaskExecution(logger, task, agentId, executor) {
|
|
107
|
+
const startTime = Date.now();
|
|
108
|
+
const sessionId = taskSessionId(task);
|
|
109
|
+
try {
|
|
110
|
+
const result = await executor();
|
|
111
|
+
const durationMs = Date.now() - startTime;
|
|
112
|
+
// Log as a tool call (task execution is essentially a tool)
|
|
113
|
+
logger.logToolCall({
|
|
114
|
+
timestamp: Date.now(),
|
|
115
|
+
agentId,
|
|
116
|
+
sessionId,
|
|
117
|
+
taskId: task.id,
|
|
118
|
+
toolName: `task:${task.type}`,
|
|
119
|
+
params: taskInputParams(task),
|
|
120
|
+
result: result.output,
|
|
121
|
+
durationMs,
|
|
122
|
+
success: result.success,
|
|
123
|
+
errorMessage: result.error,
|
|
124
|
+
});
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
const durationMs = Date.now() - startTime;
|
|
129
|
+
logger.logError({
|
|
130
|
+
timestamp: Date.now(),
|
|
131
|
+
agentId,
|
|
132
|
+
sessionId,
|
|
133
|
+
taskId: task.id,
|
|
134
|
+
message: `Task execution failed: ${task.type}`,
|
|
135
|
+
stack: error instanceof Error ? error.stack : undefined,
|
|
136
|
+
recoverable: false,
|
|
137
|
+
errorCode: 'TASK_EXECUTION_ERROR',
|
|
138
|
+
context: {
|
|
139
|
+
taskType: task.type,
|
|
140
|
+
durationMs,
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
export async function instrumentLLMCall(logger, options, executor, costCalculator) {
|
|
147
|
+
const startTime = Date.now();
|
|
148
|
+
try {
|
|
149
|
+
const response = await executor();
|
|
150
|
+
const durationMs = Date.now() - startTime;
|
|
151
|
+
const cost = costCalculator ? costCalculator(response.usage) : undefined;
|
|
152
|
+
logger.logLLMCall({
|
|
153
|
+
timestamp: Date.now(),
|
|
154
|
+
agentId: options.agentId,
|
|
155
|
+
sessionId: options.sessionId,
|
|
156
|
+
taskId: options.taskId,
|
|
157
|
+
provider: options.provider,
|
|
158
|
+
model: options.model,
|
|
159
|
+
promptTokens: response.usage.promptTokens,
|
|
160
|
+
completionTokens: response.usage.completionTokens,
|
|
161
|
+
durationMs,
|
|
162
|
+
cost,
|
|
163
|
+
cacheHit: response.cacheHit,
|
|
164
|
+
});
|
|
165
|
+
return response;
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
const durationMs = Date.now() - startTime;
|
|
169
|
+
logger.logError({
|
|
170
|
+
timestamp: Date.now(),
|
|
171
|
+
agentId: options.agentId,
|
|
172
|
+
sessionId: options.sessionId,
|
|
173
|
+
taskId: options.taskId,
|
|
174
|
+
message: `LLM call failed: ${options.provider}/${options.model}`,
|
|
175
|
+
stack: error instanceof Error ? error.stack : undefined,
|
|
176
|
+
recoverable: true,
|
|
177
|
+
errorCode: 'LLM_CALL_ERROR',
|
|
178
|
+
context: {
|
|
179
|
+
provider: options.provider,
|
|
180
|
+
model: options.model,
|
|
181
|
+
durationMs,
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
throw error;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Cost calculators for popular LLM providers
|
|
189
|
+
*/
|
|
190
|
+
export const LLMCostCalculators = {
|
|
191
|
+
/**
|
|
192
|
+
* OpenAI cost calculator
|
|
193
|
+
* Prices as of 2024 (per 1M tokens)
|
|
194
|
+
*/
|
|
195
|
+
openai: (model, usage) => {
|
|
196
|
+
const prices = {
|
|
197
|
+
'gpt-4-turbo': { input: 10, output: 30 },
|
|
198
|
+
'gpt-4': { input: 30, output: 60 },
|
|
199
|
+
'gpt-3.5-turbo': { input: 0.5, output: 1.5 },
|
|
200
|
+
};
|
|
201
|
+
const price = prices[model] ?? prices['gpt-3.5-turbo'] ?? { input: 0.5, output: 1.5 };
|
|
202
|
+
return ((usage.promptTokens / 1_000_000) * price.input +
|
|
203
|
+
(usage.completionTokens / 1_000_000) * price.output);
|
|
204
|
+
},
|
|
205
|
+
/**
|
|
206
|
+
* Anthropic cost calculator
|
|
207
|
+
* Prices as of 2024 (per 1M tokens)
|
|
208
|
+
*/
|
|
209
|
+
anthropic: (model, usage) => {
|
|
210
|
+
const prices = {
|
|
211
|
+
'claude-3-opus': { input: 15, output: 75 },
|
|
212
|
+
'claude-3-sonnet': { input: 3, output: 15 },
|
|
213
|
+
'claude-3-haiku': { input: 0.25, output: 1.25 },
|
|
214
|
+
};
|
|
215
|
+
const price = prices[model] ?? prices['claude-3-sonnet'] ?? { input: 3, output: 15 };
|
|
216
|
+
return ((usage.promptTokens / 1_000_000) * price.input +
|
|
217
|
+
(usage.completionTokens / 1_000_000) * price.output);
|
|
218
|
+
},
|
|
219
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @revealui/ai - Event Storage Implementations
|
|
3
|
+
*
|
|
4
|
+
* Storage backends for persisting agent events.
|
|
5
|
+
*/
|
|
6
|
+
import type { AnyAgentEvent, EventFilter, EventStorage } from './types.js';
|
|
7
|
+
/**
|
|
8
|
+
* In-memory storage (no persistence)
|
|
9
|
+
*/
|
|
10
|
+
export declare class MemoryEventStorage implements EventStorage {
|
|
11
|
+
private events;
|
|
12
|
+
save(events: AnyAgentEvent[]): Promise<void>;
|
|
13
|
+
load(filter?: EventFilter): Promise<AnyAgentEvent[]>;
|
|
14
|
+
clear(): Promise<void>;
|
|
15
|
+
count(filter?: EventFilter): Promise<number>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Local storage (browser)
|
|
19
|
+
*/
|
|
20
|
+
export declare class LocalStorageEventStorage implements EventStorage {
|
|
21
|
+
private key;
|
|
22
|
+
constructor(key?: string);
|
|
23
|
+
save(events: AnyAgentEvent[]): Promise<void>;
|
|
24
|
+
load(filter?: EventFilter): Promise<AnyAgentEvent[]>;
|
|
25
|
+
clear(): Promise<void>;
|
|
26
|
+
count(filter?: EventFilter): Promise<number>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* File system storage (Node.js)
|
|
30
|
+
*/
|
|
31
|
+
export declare class FileSystemEventStorage implements EventStorage {
|
|
32
|
+
private filePath;
|
|
33
|
+
constructor(filePath: string);
|
|
34
|
+
save(events: AnyAgentEvent[]): Promise<void>;
|
|
35
|
+
load(filter?: EventFilter): Promise<AnyAgentEvent[]>;
|
|
36
|
+
clear(): Promise<void>;
|
|
37
|
+
count(filter?: EventFilter): Promise<number>;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../src/observability/storage.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE3E;;GAEG;AACH,qBAAa,kBAAmB,YAAW,YAAY;IACrD,OAAO,CAAC,MAAM,CAAuB;IAE/B,IAAI,CAAC,MAAM,EAAE,aAAa,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAI5C,IAAI,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,aAAa,EAAE,CAAC;IAoCpD,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAItB,KAAK,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC;CAInD;AAED;;GAEG;AACH,qBAAa,wBAAyB,YAAW,YAAY;IAC3D,OAAO,CAAC,GAAG,CAAS;gBAER,GAAG,SAA0B;IAInC,IAAI,CAAC,MAAM,EAAE,aAAa,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAO5C,IAAI,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,aAAa,EAAE,CAAC;IAqDpD,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAQtB,KA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