@thinkhive/sdk 4.0.0 → 4.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/README.md +367 -199
- package/dist/api/claims.js +2 -3
- package/dist/api/roi-analytics.d.ts +99 -0
- package/dist/api/roi-analytics.js +64 -1
- package/dist/core/config.d.ts +1 -1
- package/dist/core/config.js +2 -2
- package/dist/index.d.ts +104 -16
- package/dist/index.js +6 -4
- package/dist/integrations/ticket-linking.d.ts +23 -17
- package/dist/integrations/ticket-linking.js +95 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# ThinkHive SDK v4.
|
|
1
|
+
# ThinkHive SDK v4.1.0
|
|
2
2
|
|
|
3
3
|
The official JavaScript/TypeScript SDK for [ThinkHive](https://thinkhive.ai) - AI Agent Observability Platform.
|
|
4
4
|
|
|
@@ -7,8 +7,9 @@ The official JavaScript/TypeScript SDK for [ThinkHive](https://thinkhive.ai) - A
|
|
|
7
7
|
- **OpenTelemetry-Based Tracing**: Built on OTLP for seamless integration with existing observability tools
|
|
8
8
|
- **Run-Centric Architecture**: Atomic unit of work tracking with claims, calibration, and linking
|
|
9
9
|
- **Facts vs Inferences**: Claims API for separating verified facts from inferences
|
|
10
|
-
- **Deterministic Ticket Linking**: 7 methods for linking runs to support tickets
|
|
11
|
-
- **Calibrated Predictions**: Brier scores for prediction accuracy
|
|
10
|
+
- **Deterministic Ticket Linking**: 7 methods for linking runs to support tickets (Zendesk, Intercom, Salesforce, Freshdesk)
|
|
11
|
+
- **Calibrated Predictions**: Brier scores and ECE for prediction accuracy
|
|
12
|
+
- **Configurable ROI Engine**: Business impact calculation with customizable cost, deflection, and attribution models
|
|
12
13
|
- **Auto-Instrumentation**: Works with LangChain, OpenAI, Anthropic, and more
|
|
13
14
|
- **Multi-Format Support**: Normalizes traces from 25+ observability platforms
|
|
14
15
|
|
|
@@ -20,14 +21,13 @@ npm install @thinkhive/sdk
|
|
|
20
21
|
|
|
21
22
|
## Quick Start
|
|
22
23
|
|
|
23
|
-
###
|
|
24
|
+
### Initialization
|
|
24
25
|
|
|
25
26
|
```typescript
|
|
26
|
-
import { init, runs,
|
|
27
|
+
import { init, runs, shutdown } from '@thinkhive/sdk';
|
|
27
28
|
|
|
28
|
-
// Initialize the SDK
|
|
29
29
|
init({
|
|
30
|
-
apiKey: '
|
|
30
|
+
apiKey: 'thk_your_api_key',
|
|
31
31
|
serviceName: 'my-ai-agent',
|
|
32
32
|
autoInstrument: true,
|
|
33
33
|
frameworks: ['langchain', 'openai'],
|
|
@@ -36,25 +36,326 @@ init({
|
|
|
36
36
|
// Create a run (atomic unit of work)
|
|
37
37
|
const run = await runs.create({
|
|
38
38
|
agentId: 'weather-agent',
|
|
39
|
-
|
|
39
|
+
conversationMessages: [
|
|
40
40
|
{ role: 'user', content: 'What is the weather in San Francisco?' },
|
|
41
|
-
{ role: 'assistant', content: 'The weather in San Francisco is currently
|
|
41
|
+
{ role: 'assistant', content: 'The weather in San Francisco is currently 65F and sunny.' }
|
|
42
42
|
],
|
|
43
43
|
outcome: 'success',
|
|
44
44
|
});
|
|
45
45
|
|
|
46
46
|
console.log(`Run ID: ${run.id}`);
|
|
47
|
-
|
|
48
|
-
// Shutdown when done
|
|
49
47
|
await shutdown();
|
|
50
48
|
```
|
|
51
49
|
|
|
52
|
-
|
|
50
|
+
## API Reference
|
|
51
|
+
|
|
52
|
+
### Core APIs (V3)
|
|
53
|
+
|
|
54
|
+
| API | Import | Description |
|
|
55
|
+
|-----|--------|-------------|
|
|
56
|
+
| `runs` | `@thinkhive/sdk` | Create and manage runs (atomic work units) |
|
|
57
|
+
| `claims` | `@thinkhive/sdk` | Manage facts/inferences for runs |
|
|
58
|
+
| `calibration` | `@thinkhive/sdk` | Track prediction accuracy (Brier scores) |
|
|
59
|
+
| `linking` | `@thinkhive/sdk` | Connect runs to support tickets (7 methods) |
|
|
60
|
+
| `customerContext` | `@thinkhive/sdk` | Time-series customer snapshots |
|
|
61
|
+
| `roiAnalytics` | `@thinkhive/sdk` | Configurable ROI calculation engine |
|
|
62
|
+
| `agents` | `@thinkhive/sdk` | Agent CRUD management |
|
|
63
|
+
| `apiKeys` | `@thinkhive/sdk` | API key management with scoped permissions |
|
|
64
|
+
|
|
65
|
+
### Assessment APIs
|
|
66
|
+
|
|
67
|
+
| API | Description |
|
|
68
|
+
|-----|-------------|
|
|
69
|
+
| `humanReview` | Human-in-the-loop review queues |
|
|
70
|
+
| `nondeterminism` | Multi-sample reliability testing |
|
|
71
|
+
| `evalHealth` | Metric health monitoring |
|
|
72
|
+
| `deterministicGraders` | Rule-based grading |
|
|
73
|
+
| `conversationEval` | Multi-turn conversation grading |
|
|
74
|
+
| `transcriptPatterns` | Pattern detection in transcripts |
|
|
75
|
+
|
|
76
|
+
### Legacy APIs (V2)
|
|
77
|
+
|
|
78
|
+
| API | Description |
|
|
79
|
+
|-----|-------------|
|
|
80
|
+
| `issues` | Clustered failure patterns |
|
|
81
|
+
| `analyzer` | User-selected trace analysis with cost estimation |
|
|
82
|
+
| `businessMetrics` | Industry-driven metrics with historical tracking |
|
|
83
|
+
| `qualityMetrics` | RAG scoring and hallucination detection |
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
## Runs API
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import { runs, createRunWithContext } from '@thinkhive/sdk';
|
|
91
|
+
|
|
92
|
+
// Create a run
|
|
93
|
+
const run = await runs.create({
|
|
94
|
+
agentId: 'support-agent',
|
|
95
|
+
conversationMessages: [
|
|
96
|
+
{ role: 'user', content: 'Help me cancel my order' },
|
|
97
|
+
{ role: 'assistant', content: 'I can help you cancel your order...' }
|
|
98
|
+
],
|
|
99
|
+
outcome: 'resolved',
|
|
100
|
+
durationMs: 1500,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// List runs with filters
|
|
104
|
+
const result = await runs.list({
|
|
105
|
+
agentId: 'support-agent',
|
|
106
|
+
outcome: 'resolved',
|
|
107
|
+
startedAfter: '2025-01-01',
|
|
108
|
+
limit: 50,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// Get a specific run
|
|
112
|
+
const existing = await runs.get('run_abc123');
|
|
113
|
+
|
|
114
|
+
// Get run statistics
|
|
115
|
+
const stats = await runs.stats('support-agent');
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Claims API (Facts vs Inferences)
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
import { claims, isFact, isInference, getHighConfidenceClaims } from '@thinkhive/sdk';
|
|
122
|
+
|
|
123
|
+
// List claims for a run
|
|
124
|
+
const claimList = await claims.list(runId);
|
|
125
|
+
|
|
126
|
+
// Filter by type
|
|
127
|
+
const facts = claimList.filter(isFact);
|
|
128
|
+
const inferences = claimList.filter(isInference);
|
|
129
|
+
|
|
130
|
+
// Get high confidence claims
|
|
131
|
+
const confident = getHighConfidenceClaims(claimList, 0.9);
|
|
132
|
+
|
|
133
|
+
// Group by category
|
|
134
|
+
const byCategory = groupClaimsByCategory(claimList);
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Calibration API
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
import { calibration, calculateBrierScore, isWellCalibrated } from '@thinkhive/sdk';
|
|
141
|
+
|
|
142
|
+
// Get calibration status for a prediction type
|
|
143
|
+
const status = await calibration.status('agent-123', 'churn_risk');
|
|
144
|
+
|
|
145
|
+
// Get all calibration metrics
|
|
146
|
+
const metrics = await calibration.allMetrics('agent-123');
|
|
147
|
+
|
|
148
|
+
// Calculate Brier score locally
|
|
149
|
+
const score = calculateBrierScore([
|
|
150
|
+
{ predicted: 0.8, actual: 1 },
|
|
151
|
+
{ predicted: 0.3, actual: 0 },
|
|
152
|
+
]);
|
|
153
|
+
|
|
154
|
+
if (isWellCalibrated(score)) {
|
|
155
|
+
console.log('Agent predictions are well calibrated');
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Ticket Linking (V3)
|
|
160
|
+
|
|
161
|
+
Deterministic linking between runs and support tickets using 7 methods with explicit confidence scores.
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
import {
|
|
165
|
+
linking,
|
|
166
|
+
generateZendeskMarker,
|
|
167
|
+
linkRunToTicket,
|
|
168
|
+
linkRunToZendeskTicket,
|
|
169
|
+
getBestLinkMethod,
|
|
170
|
+
LINK_METHOD_CONFIDENCE,
|
|
171
|
+
} from '@thinkhive/sdk';
|
|
172
|
+
|
|
173
|
+
// SDK explicit linking (confidence: 1.0)
|
|
174
|
+
const link = await linking.create({
|
|
175
|
+
runId: 'run_abc123',
|
|
176
|
+
ticketId: 'ticket_xyz',
|
|
177
|
+
method: 'sdk_explicit',
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
// Zendesk marker linking
|
|
181
|
+
const zdLink = await linking.create({
|
|
182
|
+
runId: 'run_abc123',
|
|
183
|
+
externalTicketId: '12345',
|
|
184
|
+
platform: 'zendesk',
|
|
185
|
+
method: 'zendesk_marker',
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
// Convenience functions
|
|
189
|
+
await linkRunToTicket('run_abc123', 'ticket_xyz');
|
|
190
|
+
await linkRunToZendeskTicket('run_abc123', '12345');
|
|
191
|
+
|
|
192
|
+
// Get links for a run
|
|
193
|
+
const links = await linking.getForRun('run_abc123');
|
|
194
|
+
|
|
195
|
+
// Get links for a ticket
|
|
196
|
+
const ticketLinks = await linking.getForTicket('ticket_xyz');
|
|
197
|
+
|
|
198
|
+
// Verify a link
|
|
199
|
+
await linking.verify('link_id', { verified: true, notes: 'Confirmed by agent' });
|
|
200
|
+
|
|
201
|
+
// Auto-link using all available evidence
|
|
202
|
+
const autoLinked = await linking.autoLink('run_abc123');
|
|
203
|
+
|
|
204
|
+
// Get link statistics
|
|
205
|
+
const stats = await linking.stats();
|
|
206
|
+
// { totalLinks, byMethod, avgConfidence, verifiedCount, unverifiedCount }
|
|
207
|
+
|
|
208
|
+
// Generate a Zendesk marker to embed in responses
|
|
209
|
+
const marker = generateZendeskMarker('run_abc123');
|
|
210
|
+
// Returns: '[THID:run_abc123]'
|
|
211
|
+
|
|
212
|
+
// Server-side marker generation with format options
|
|
213
|
+
const serverMarker = await linking.generateMarker({
|
|
214
|
+
traceId: 'trace_123',
|
|
215
|
+
format: 'html_comment',
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
// Delete a link
|
|
219
|
+
await linking.delete('link_id');
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Link Methods and Confidence
|
|
223
|
+
|
|
224
|
+
| Method | Confidence | Description |
|
|
225
|
+
|--------|-----------|-------------|
|
|
226
|
+
| `sdk_explicit` | 1.0 | Direct SDK call with ticket ID |
|
|
227
|
+
| `zendesk_marker` | 1.0 | Embedded THID marker in response |
|
|
228
|
+
| `custom_field` | 1.0 | Zendesk custom field |
|
|
229
|
+
| `manual` | 1.0 | Human-assigned |
|
|
230
|
+
| `middleware_stamp` | 0.98 | Middleware-injected trace ID |
|
|
231
|
+
| `session_match` | 0.95 | Session ID correlation |
|
|
232
|
+
| `email_time_window` | 0.6 | Email + 15min window |
|
|
233
|
+
|
|
234
|
+
## ROI Analytics (V3)
|
|
235
|
+
|
|
236
|
+
Configurable ROI calculation engine with customizable cost models, deflection definitions, and attribution rules.
|
|
237
|
+
|
|
238
|
+
### Configuration
|
|
239
|
+
|
|
240
|
+
```typescript
|
|
241
|
+
import { roiAnalytics } from '@thinkhive/sdk';
|
|
242
|
+
|
|
243
|
+
// Get active ROI config (auto-provisions default if none exists)
|
|
244
|
+
const config = await roiAnalytics.getConfig();
|
|
245
|
+
|
|
246
|
+
// Create custom ROI config
|
|
247
|
+
const newConfig = await roiAnalytics.createConfig({
|
|
248
|
+
costConfig: {
|
|
249
|
+
humanAgentCostPerHour: 35,
|
|
250
|
+
aiCostPer1kTokens: 0.01,
|
|
251
|
+
escalationCostMultiplier: 2.5,
|
|
252
|
+
infrastructureMonthlyCost: 200,
|
|
253
|
+
},
|
|
254
|
+
deflectionConfig: {
|
|
255
|
+
definition: 'resolved_without_human',
|
|
256
|
+
outcomesThatCount: ['resolved'],
|
|
257
|
+
minConfidence: 0.8,
|
|
258
|
+
excludeEscalated: true,
|
|
259
|
+
},
|
|
260
|
+
resolutionConfig: {
|
|
261
|
+
definition: 'customer_confirmed',
|
|
262
|
+
requiresSatisfaction: false,
|
|
263
|
+
minSatisfactionScore: null,
|
|
264
|
+
noRecontactWindowHours: 24,
|
|
265
|
+
},
|
|
266
|
+
attributionConfig: {
|
|
267
|
+
timeSavedValuePerHour: 50,
|
|
268
|
+
churnPreventionAttribution: 0.2,
|
|
269
|
+
upsellAttribution: 0.1,
|
|
270
|
+
useActualARR: true,
|
|
271
|
+
defaultARRIfUnknown: 10000,
|
|
272
|
+
},
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
// Update config (creates new version, keeps history)
|
|
276
|
+
const updated = await roiAnalytics.updateConfig({
|
|
277
|
+
costConfig: { humanAgentCostPerHour: 40 },
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
// List config version history
|
|
281
|
+
const versions = await roiAnalytics.configVersions({ limit: 10 });
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
### Calculate ROI
|
|
285
|
+
|
|
286
|
+
```typescript
|
|
287
|
+
// Calculate ROI for a date range
|
|
288
|
+
const result = await roiAnalytics.calculateV3({
|
|
289
|
+
startDate: '2025-01-01',
|
|
290
|
+
endDate: '2025-02-01',
|
|
291
|
+
includeBreakdown: true,
|
|
292
|
+
includeConfidenceIntervals: false,
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
console.log(`Net ROI: $${result.summary.netROI}`);
|
|
296
|
+
console.log(`ROI %: ${result.summary.roiPercentage}%`);
|
|
297
|
+
console.log(`Deflection Rate: ${result.metrics.deflectionRate}`);
|
|
298
|
+
console.log(`Total Runs: ${result.metrics.totalRuns}`);
|
|
299
|
+
|
|
300
|
+
// Filter by agent
|
|
301
|
+
const agentROI = await roiAnalytics.calculateV3({
|
|
302
|
+
agentId: 'support-agent',
|
|
303
|
+
startDate: '2025-01-01',
|
|
304
|
+
endDate: '2025-02-01',
|
|
305
|
+
});
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
### ROI Trends
|
|
309
|
+
|
|
310
|
+
```typescript
|
|
311
|
+
// Get ROI trend over time
|
|
312
|
+
const trend = await roiAnalytics.trendV3({
|
|
313
|
+
granularity: 'day', // 'day' | 'week' | 'month'
|
|
314
|
+
startDate: '2025-01-01',
|
|
315
|
+
endDate: '2025-02-01',
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
for (const bucket of trend.trend) {
|
|
319
|
+
console.log(`${bucket.periodStart}: ${bucket.runCount} runs, $${bucket.netROI} ROI`);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (trend.comparison) {
|
|
323
|
+
console.log(`Improving: ${trend.comparison.isImproving}`);
|
|
324
|
+
console.log(`Change: ${trend.comparison.changePercent}%`);
|
|
325
|
+
}
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
### Legacy V1 ROI (still available)
|
|
53
329
|
|
|
54
330
|
```typescript
|
|
55
|
-
|
|
331
|
+
// V1 summary endpoint
|
|
332
|
+
const summary = await roiAnalytics.summary({
|
|
333
|
+
startDate: '2025-01-01',
|
|
334
|
+
endDate: '2025-01-31',
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
// V1 trends
|
|
338
|
+
const trends = await roiAnalytics.trends({
|
|
339
|
+
startDate: '2025-01-01',
|
|
340
|
+
endDate: '2025-01-31',
|
|
341
|
+
});
|
|
56
342
|
|
|
57
|
-
|
|
343
|
+
// V1 per-trace ROI calculation
|
|
344
|
+
const impact = await roiAnalytics.calculate({
|
|
345
|
+
traceId: 'trace_abc123',
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
// V1 industry configs
|
|
349
|
+
const industries = await roiAnalytics.industries();
|
|
350
|
+
|
|
351
|
+
// V1 correlation analysis
|
|
352
|
+
const correlations = await roiAnalytics.correlations();
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
## Manual Tracing
|
|
356
|
+
|
|
357
|
+
```typescript
|
|
358
|
+
import { traceLLM, traceRetrieval, traceTool, traceChain } from '@thinkhive/sdk';
|
|
58
359
|
|
|
59
360
|
// Trace an LLM call
|
|
60
361
|
const response = await traceLLM({
|
|
@@ -63,7 +364,6 @@ const response = await traceLLM({
|
|
|
63
364
|
provider: 'openai',
|
|
64
365
|
input: { prompt: 'Hello!' }
|
|
65
366
|
}, async () => {
|
|
66
|
-
// Your LLM call here
|
|
67
367
|
return await openai.chat.completions.create({...});
|
|
68
368
|
});
|
|
69
369
|
|
|
@@ -84,16 +384,25 @@ const result = await traceTool({
|
|
|
84
384
|
}, async () => {
|
|
85
385
|
return await lookupOrder('12345');
|
|
86
386
|
});
|
|
387
|
+
|
|
388
|
+
// Trace a chain/workflow
|
|
389
|
+
const output = await traceChain({
|
|
390
|
+
name: 'support-workflow',
|
|
391
|
+
input: { query: 'help with refund' }
|
|
392
|
+
}, async () => {
|
|
393
|
+
const docs = await retrieveContext(query);
|
|
394
|
+
return await generateResponse(docs);
|
|
395
|
+
});
|
|
87
396
|
```
|
|
88
397
|
|
|
89
|
-
|
|
398
|
+
## Analyzer API
|
|
90
399
|
|
|
91
400
|
```typescript
|
|
92
401
|
import { analyzer } from '@thinkhive/sdk';
|
|
93
402
|
|
|
94
403
|
// Estimate cost before running analysis
|
|
95
404
|
const estimate = await analyzer.estimateCost({
|
|
96
|
-
traceIds: ['trace-1', 'trace-2'
|
|
405
|
+
traceIds: ['trace-1', 'trace-2'],
|
|
97
406
|
tier: 'standard',
|
|
98
407
|
});
|
|
99
408
|
console.log(`Estimated cost: $${estimate.estimatedCost}`);
|
|
@@ -109,8 +418,8 @@ const analysis = await analyzer.analyze({
|
|
|
109
418
|
// Analyze traces by time window with smart sampling
|
|
110
419
|
const windowAnalysis = await analyzer.analyzeWindow({
|
|
111
420
|
agentId: 'support-agent',
|
|
112
|
-
startDate: new Date('
|
|
113
|
-
endDate: new Date('
|
|
421
|
+
startDate: new Date('2025-01-01'),
|
|
422
|
+
endDate: new Date('2025-01-31'),
|
|
114
423
|
filters: { outcomes: ['failure'], minSeverity: 'medium' },
|
|
115
424
|
sampling: { strategy: 'smart', samplePercent: 10 },
|
|
116
425
|
});
|
|
@@ -118,246 +427,105 @@ const windowAnalysis = await analyzer.analyzeWindow({
|
|
|
118
427
|
// Get aggregated insights
|
|
119
428
|
const summary = await analyzer.summarize({
|
|
120
429
|
agentId: 'support-agent',
|
|
121
|
-
startDate: new Date('
|
|
122
|
-
endDate: new Date('
|
|
430
|
+
startDate: new Date('2025-01-01'),
|
|
431
|
+
endDate: new Date('2025-01-31'),
|
|
123
432
|
});
|
|
124
433
|
```
|
|
125
434
|
|
|
126
|
-
|
|
435
|
+
## Issues API
|
|
127
436
|
|
|
128
437
|
```typescript
|
|
129
438
|
import { issues } from '@thinkhive/sdk';
|
|
130
439
|
|
|
131
|
-
|
|
132
|
-
const issueList = await issues.list('support-agent', {
|
|
133
|
-
status: 'open',
|
|
134
|
-
limit: 10,
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
// Get a specific issue
|
|
440
|
+
const issueList = await issues.list('support-agent', { status: 'open', limit: 10 });
|
|
138
441
|
const issue = await issues.get('issue-123');
|
|
139
|
-
|
|
140
|
-
// Get fixes for an issue
|
|
141
442
|
const fixes = await issues.getFixes('issue-123');
|
|
142
443
|
```
|
|
143
444
|
|
|
144
|
-
|
|
445
|
+
## API Keys
|
|
145
446
|
|
|
146
447
|
```typescript
|
|
147
448
|
import { apiKeys, hasPermission, canAccessAgent } from '@thinkhive/sdk';
|
|
148
449
|
|
|
149
|
-
// Create a scoped API key
|
|
150
450
|
const result = await apiKeys.create({
|
|
151
451
|
name: 'CI Pipeline Key',
|
|
152
|
-
permissions: {
|
|
153
|
-
|
|
154
|
-
write: true,
|
|
155
|
-
delete: false
|
|
156
|
-
},
|
|
157
|
-
scopeType: 'agent', // Restrict to specific agents
|
|
452
|
+
permissions: { read: true, write: true, delete: false },
|
|
453
|
+
scopeType: 'agent',
|
|
158
454
|
allowedAgentIds: ['agent-prod-001'],
|
|
159
455
|
environment: 'production',
|
|
160
|
-
expiresAt: new Date(Date.now() + 90 * 24 * 60 * 60 * 1000)
|
|
456
|
+
expiresAt: new Date(Date.now() + 90 * 24 * 60 * 60 * 1000),
|
|
161
457
|
});
|
|
162
458
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
// Check permissions
|
|
166
|
-
if (hasPermission(key, 'write')) {
|
|
167
|
-
// Can write data
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// Check agent access
|
|
171
|
-
if (canAccessAgent(key, 'agent-123')) {
|
|
172
|
-
// Can access this agent
|
|
173
|
-
}
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
### Claims API (Facts vs Inferences)
|
|
177
|
-
|
|
178
|
-
```typescript
|
|
179
|
-
import { claims, isFact, isInference, getHighConfidenceClaims } from '@thinkhive/sdk';
|
|
180
|
-
|
|
181
|
-
// List claims for a run
|
|
182
|
-
const claimList = await claims.list(runId);
|
|
183
|
-
|
|
184
|
-
// Filter by type
|
|
185
|
-
const facts = claimList.filter(isFact);
|
|
186
|
-
const inferences = claimList.filter(isInference);
|
|
187
|
-
|
|
188
|
-
// Get high confidence claims
|
|
189
|
-
const confident = getHighConfidenceClaims(claimList, 0.9);
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
### Calibration API (Prediction Accuracy)
|
|
193
|
-
|
|
194
|
-
```typescript
|
|
195
|
-
import { calibration, calculateBrierScore, isWellCalibrated } from '@thinkhive/sdk';
|
|
196
|
-
|
|
197
|
-
// Get calibration status
|
|
198
|
-
const status = await calibration.status(agentId);
|
|
199
|
-
|
|
200
|
-
// Calculate Brier score for predictions
|
|
201
|
-
const brierScore = calculateBrierScore(predictions, outcomes);
|
|
202
|
-
|
|
203
|
-
// Check if well calibrated
|
|
204
|
-
if (isWellCalibrated(status)) {
|
|
205
|
-
console.log('Agent predictions are well calibrated');
|
|
206
|
-
}
|
|
459
|
+
if (hasPermission(key, 'write')) { /* can write */ }
|
|
460
|
+
if (canAccessAgent(key, 'agent-123')) { /* can access */ }
|
|
207
461
|
```
|
|
208
462
|
|
|
209
|
-
|
|
463
|
+
## Business Metrics
|
|
210
464
|
|
|
211
465
|
```typescript
|
|
212
|
-
import {
|
|
213
|
-
businessMetrics,
|
|
214
|
-
isMetricReady,
|
|
215
|
-
needsMoreTraces,
|
|
216
|
-
getStatusMessage
|
|
217
|
-
} from '@thinkhive/sdk';
|
|
466
|
+
import { businessMetrics, isMetricReady, getStatusMessage } from '@thinkhive/sdk';
|
|
218
467
|
|
|
219
|
-
// Get current metric value with status
|
|
220
468
|
const metric = await businessMetrics.current('agent-123', 'Deflection Rate');
|
|
221
|
-
console.log(`${metric.metricName}: ${metric.valueFormatted}`);
|
|
222
|
-
|
|
223
|
-
if (metric.status === 'insufficient_data') {
|
|
224
|
-
console.log(`Need ${metric.minTraceThreshold - metric.traceCount} more traces`);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// Get historical data for graphing
|
|
228
469
|
const history = await businessMetrics.history('agent-123', 'Deflection Rate', {
|
|
229
470
|
startDate: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000),
|
|
230
|
-
endDate: new Date(),
|
|
231
471
|
granularity: 'daily',
|
|
232
472
|
});
|
|
233
473
|
|
|
234
|
-
console.log(`${history.dataPoints.length} data points`);
|
|
235
|
-
console.log(`Change: ${history.summary.changePercent}%`);
|
|
236
|
-
|
|
237
|
-
// Record external metric values (from CRM, surveys, etc.)
|
|
238
474
|
await businessMetrics.record('agent-123', {
|
|
239
475
|
metricName: 'CSAT/NPS',
|
|
240
476
|
value: 4.5,
|
|
241
477
|
unit: 'score',
|
|
242
|
-
periodStart: '
|
|
243
|
-
periodEnd: '
|
|
478
|
+
periodStart: '2025-01-01T00:00:00Z',
|
|
479
|
+
periodEnd: '2025-01-07T23:59:59Z',
|
|
244
480
|
source: 'survey_system',
|
|
245
|
-
sourceDetails: { surveyId: 'survey_456', responseCount: 150 },
|
|
246
481
|
});
|
|
247
482
|
```
|
|
248
483
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
| Status | Description |
|
|
252
|
-
|--------|-------------|
|
|
253
|
-
| `ready` | Metric calculated and ready to display |
|
|
254
|
-
| `insufficient_data` | Need more traces before calculation |
|
|
255
|
-
| `awaiting_external` | External data source not connected |
|
|
256
|
-
| `stale` | Data is older than expected |
|
|
257
|
-
|
|
258
|
-
### Ticket Linking (Zendesk Integration)
|
|
259
|
-
|
|
260
|
-
```typescript
|
|
261
|
-
import {
|
|
262
|
-
linking,
|
|
263
|
-
generateZendeskMarker,
|
|
264
|
-
linkRunToZendeskTicket
|
|
265
|
-
} from '@thinkhive/sdk';
|
|
266
|
-
|
|
267
|
-
// Generate a marker to embed in ticket
|
|
268
|
-
const marker = generateZendeskMarker(runId);
|
|
269
|
-
// Returns: <!-- thinkhive:run:abc123 -->
|
|
270
|
-
|
|
271
|
-
// Link a run to a ticket
|
|
272
|
-
await linkRunToZendeskTicket(runId, ticketId);
|
|
273
|
-
|
|
274
|
-
// Get best linking method
|
|
275
|
-
import { getBestLinkMethod } from '@thinkhive/sdk';
|
|
276
|
-
const method = getBestLinkMethod(runData);
|
|
277
|
-
// Returns: 'conversation_id' | 'subject_hash' | 'marker' | etc.
|
|
278
|
-
```
|
|
279
|
-
|
|
280
|
-
### Auto-Instrumentation
|
|
484
|
+
## Auto-Instrumentation
|
|
281
485
|
|
|
282
486
|
```typescript
|
|
283
487
|
import { init } from '@thinkhive/sdk';
|
|
284
488
|
|
|
285
|
-
// Initialize with auto-instrumentation
|
|
286
489
|
init({
|
|
287
|
-
apiKey: '
|
|
490
|
+
apiKey: 'thk_your_api_key',
|
|
288
491
|
serviceName: 'my-ai-agent',
|
|
289
492
|
autoInstrument: true,
|
|
290
|
-
frameworks: ['langchain', 'openai', 'anthropic']
|
|
493
|
+
frameworks: ['langchain', 'openai', 'anthropic'],
|
|
291
494
|
});
|
|
292
|
-
|
|
293
|
-
// Now all LangChain, OpenAI, and Anthropic calls are automatically traced!
|
|
495
|
+
// All LangChain, OpenAI, and Anthropic calls are now automatically traced
|
|
294
496
|
```
|
|
295
497
|
|
|
296
|
-
##
|
|
498
|
+
## Sub-package Imports
|
|
499
|
+
|
|
500
|
+
```typescript
|
|
501
|
+
// OpenAI instrumentation
|
|
502
|
+
import { instrumentOpenAIClient } from '@thinkhive/sdk/instrumentation/openai';
|
|
297
503
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
504
|
+
// LangChain instrumentation
|
|
505
|
+
import { ThinkHiveCallbackHandler } from '@thinkhive/sdk/instrumentation/langchain';
|
|
506
|
+
|
|
507
|
+
// Ticket linking (standalone)
|
|
508
|
+
import { linking, generateZendeskMarker } from '@thinkhive/sdk/integrations/ticket-linking';
|
|
509
|
+
|
|
510
|
+
// Customer context (standalone)
|
|
511
|
+
import { customerContext, captureCustomerContext } from '@thinkhive/sdk/integrations/customer-context';
|
|
512
|
+
```
|
|
303
513
|
|
|
304
514
|
## Environment Variables
|
|
305
515
|
|
|
306
516
|
| Variable | Description |
|
|
307
517
|
|----------|-------------|
|
|
308
518
|
| `THINKHIVE_API_KEY` | Your ThinkHive API key |
|
|
309
|
-
| `THINKHIVE_ENDPOINT` | Custom API endpoint (default: https://app.thinkhive.ai) |
|
|
519
|
+
| `THINKHIVE_ENDPOINT` | Custom API endpoint (default: `https://app.thinkhive.ai`) |
|
|
310
520
|
| `THINKHIVE_SERVICE_NAME` | Service name for traces (optional) |
|
|
521
|
+
| `THINKHIVE_AGENT_ID` | Default agent ID (optional) |
|
|
311
522
|
|
|
312
|
-
##
|
|
313
|
-
|
|
314
|
-
### Key Concepts
|
|
315
|
-
|
|
316
|
-
**Run-Centric Model**: The atomic unit of work is a "Run" (not a trace). A run captures:
|
|
317
|
-
- Conversation messages
|
|
318
|
-
- Retrieved contexts
|
|
319
|
-
- Tool calls
|
|
320
|
-
- Outcome and metadata
|
|
321
|
-
|
|
322
|
-
**Facts vs Inferences**: Claims API separates:
|
|
323
|
-
- **Facts**: Verified information from retrieval or tool calls
|
|
324
|
-
- **Inferences**: LLM-generated conclusions
|
|
325
|
-
- **Computed**: Derived values from rules
|
|
326
|
-
|
|
327
|
-
**Calibrated Predictions**: Track prediction accuracy using:
|
|
328
|
-
- Brier scores for overall calibration
|
|
329
|
-
- ECE (Expected Calibration Error) for bucketed analysis
|
|
330
|
-
|
|
331
|
-
### API Structure
|
|
332
|
-
|
|
333
|
-
| API | Description |
|
|
334
|
-
|-----|-------------|
|
|
335
|
-
| `runs` | Create and manage runs (atomic work units) |
|
|
336
|
-
| `claims` | Manage facts/inferences for runs |
|
|
337
|
-
| `calibration` | Track prediction accuracy |
|
|
338
|
-
| `analyzer` | User-selected trace analysis |
|
|
339
|
-
| `issues` | Clustered failure patterns |
|
|
340
|
-
| `linking` | Connect runs to support tickets |
|
|
341
|
-
| `customerContext` | Time-series customer snapshots |
|
|
342
|
-
| `apiKeys` | API key management |
|
|
343
|
-
| `businessMetrics` | Industry-driven metrics with historical tracking |
|
|
344
|
-
| `roiAnalytics` | Business ROI and financial impact analysis |
|
|
345
|
-
| `qualityMetrics` | RAG evaluation and hallucination detection |
|
|
346
|
-
|
|
347
|
-
### Evaluation APIs
|
|
348
|
-
|
|
349
|
-
| API | Description |
|
|
350
|
-
|-----|-------------|
|
|
351
|
-
| `humanReview` | Human-in-the-loop review queues |
|
|
352
|
-
| `nondeterminism` | Multi-sample reliability testing |
|
|
353
|
-
| `evalHealth` | Evaluation metric health monitoring |
|
|
354
|
-
| `deterministicGraders` | Rule-based evaluation |
|
|
355
|
-
| `conversationEval` | Multi-turn conversation evaluation |
|
|
356
|
-
| `transcriptPatterns` | Pattern detection in transcripts |
|
|
357
|
-
|
|
358
|
-
## API Reference
|
|
523
|
+
## Upgrading from v4.0
|
|
359
524
|
|
|
360
|
-
|
|
525
|
+
- `roiAnalytics` now includes V3 methods: `getConfig()`, `createConfig()`, `updateConfig()`, `configVersions()`, `calculateV3()`, `trendV3()`
|
|
526
|
+
- `linking` now includes: `autoLink()`, `stats()`, `generateMarker()`
|
|
527
|
+
- GET `/roi/config` auto-provisions a default configuration if none exists
|
|
528
|
+
- ROI date range limit increased to 92 days
|
|
361
529
|
|
|
362
530
|
## License
|
|
363
531
|
|