@thinkhive/sdk 4.0.1 → 4.2.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 +364 -208
- package/dist/api/claims.js +2 -3
- package/dist/api/documents.d.ts +52 -0
- package/dist/api/documents.js +58 -0
- package/dist/api/drift.d.ts +70 -0
- package/dist/api/drift.js +71 -0
- package/dist/api/eval-runs.d.ts +126 -0
- package/dist/api/eval-runs.js +126 -0
- package/dist/api/llm-costs.d.ts +104 -0
- package/dist/api/llm-costs.js +81 -0
- package/dist/api/notifications.d.ts +103 -0
- package/dist/api/notifications.js +110 -0
- package/dist/api/roi-analytics.d.ts +99 -0
- package/dist/api/roi-analytics.js +64 -1
- package/dist/api/sessions.d.ts +57 -0
- package/dist/api/sessions.js +49 -0
- package/dist/api/shadow-tests.d.ts +78 -0
- package/dist/api/shadow-tests.js +80 -0
- package/dist/api/signals.d.ts +177 -0
- package/dist/api/signals.js +172 -0
- package/dist/core/client.d.ts +2 -0
- package/dist/core/client.js +10 -4
- package/dist/core/config.d.ts +1 -1
- package/dist/core/config.js +2 -2
- package/dist/guardrails.d.ts +70 -0
- package/dist/guardrails.js +34 -0
- package/dist/index.d.ts +209 -18
- package/dist/index.js +55 -7
- 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,12 +21,11 @@ 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
30
|
apiKey: 'thk_your_api_key',
|
|
31
31
|
serviceName: 'my-ai-agent',
|
|
@@ -38,23 +38,324 @@ 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
|
|
53
88
|
|
|
54
89
|
```typescript
|
|
55
|
-
import {
|
|
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');
|
|
56
147
|
|
|
57
|
-
|
|
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)
|
|
329
|
+
|
|
330
|
+
```typescript
|
|
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
|
+
});
|
|
342
|
+
|
|
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,258 +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 for a prediction type
|
|
198
|
-
const status = await calibration.status(agentId, 'churn_risk');
|
|
199
|
-
|
|
200
|
-
// Get all calibration metrics
|
|
201
|
-
const metrics = await calibration.allMetrics(agentId);
|
|
202
|
-
|
|
203
|
-
// Calculate Brier score for predictions
|
|
204
|
-
const brierScore = calculateBrierScore(predictions);
|
|
205
|
-
|
|
206
|
-
// Check if well calibrated
|
|
207
|
-
if (isWellCalibrated(brierScore)) {
|
|
208
|
-
console.log('Agent predictions are well calibrated');
|
|
209
|
-
}
|
|
459
|
+
if (hasPermission(key, 'write')) { /* can write */ }
|
|
460
|
+
if (canAccessAgent(key, 'agent-123')) { /* can access */ }
|
|
210
461
|
```
|
|
211
462
|
|
|
212
|
-
|
|
463
|
+
## Business Metrics
|
|
213
464
|
|
|
214
465
|
```typescript
|
|
215
|
-
import {
|
|
216
|
-
businessMetrics,
|
|
217
|
-
isMetricReady,
|
|
218
|
-
needsMoreTraces,
|
|
219
|
-
getStatusMessage
|
|
220
|
-
} from '@thinkhive/sdk';
|
|
466
|
+
import { businessMetrics, isMetricReady, getStatusMessage } from '@thinkhive/sdk';
|
|
221
467
|
|
|
222
|
-
// Get current metric value with status
|
|
223
468
|
const metric = await businessMetrics.current('agent-123', 'Deflection Rate');
|
|
224
|
-
console.log(`${metric.metricName}: ${metric.valueFormatted}`);
|
|
225
|
-
|
|
226
|
-
if (metric.status === 'insufficient_data') {
|
|
227
|
-
console.log(`Need ${metric.minTraceThreshold - metric.traceCount} more traces`);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// Get historical data for graphing
|
|
231
469
|
const history = await businessMetrics.history('agent-123', 'Deflection Rate', {
|
|
232
470
|
startDate: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000),
|
|
233
|
-
endDate: new Date(),
|
|
234
471
|
granularity: 'daily',
|
|
235
472
|
});
|
|
236
473
|
|
|
237
|
-
console.log(`${history.dataPoints.length} data points`);
|
|
238
|
-
console.log(`Change: ${history.summary.changePercent}%`);
|
|
239
|
-
|
|
240
|
-
// Record external metric values (from CRM, surveys, etc.)
|
|
241
474
|
await businessMetrics.record('agent-123', {
|
|
242
475
|
metricName: 'CSAT/NPS',
|
|
243
476
|
value: 4.5,
|
|
244
477
|
unit: 'score',
|
|
245
|
-
periodStart: '
|
|
246
|
-
periodEnd: '
|
|
478
|
+
periodStart: '2025-01-01T00:00:00Z',
|
|
479
|
+
periodEnd: '2025-01-07T23:59:59Z',
|
|
247
480
|
source: 'survey_system',
|
|
248
|
-
sourceDetails: { surveyId: 'survey_456', responseCount: 150 },
|
|
249
481
|
});
|
|
250
482
|
```
|
|
251
483
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
| Status | Description |
|
|
255
|
-
|--------|-------------|
|
|
256
|
-
| `ready` | Metric calculated and ready to display |
|
|
257
|
-
| `insufficient_data` | Need more traces before calculation |
|
|
258
|
-
| `awaiting_external` | External data source not connected |
|
|
259
|
-
| `stale` | Data is older than expected |
|
|
260
|
-
|
|
261
|
-
### Ticket Linking (Zendesk Integration)
|
|
262
|
-
|
|
263
|
-
```typescript
|
|
264
|
-
import {
|
|
265
|
-
linking,
|
|
266
|
-
generateZendeskMarker,
|
|
267
|
-
linkRunToZendeskTicket
|
|
268
|
-
} from '@thinkhive/sdk';
|
|
269
|
-
|
|
270
|
-
// Generate a marker to embed in ticket
|
|
271
|
-
const marker = generateZendeskMarker(runId);
|
|
272
|
-
// Returns: <!-- thinkhive:run:abc123 -->
|
|
273
|
-
|
|
274
|
-
// Link a run to a ticket
|
|
275
|
-
await linkRunToZendeskTicket(runId, ticketId);
|
|
276
|
-
|
|
277
|
-
// Get best linking method
|
|
278
|
-
import { getBestLinkMethod } from '@thinkhive/sdk';
|
|
279
|
-
const method = getBestLinkMethod(runData);
|
|
280
|
-
// Returns: 'conversation_id' | 'subject_hash' | 'marker' | etc.
|
|
281
|
-
```
|
|
282
|
-
|
|
283
|
-
### Auto-Instrumentation
|
|
484
|
+
## Auto-Instrumentation
|
|
284
485
|
|
|
285
486
|
```typescript
|
|
286
487
|
import { init } from '@thinkhive/sdk';
|
|
287
488
|
|
|
288
|
-
// Initialize with auto-instrumentation
|
|
289
489
|
init({
|
|
290
490
|
apiKey: 'thk_your_api_key',
|
|
291
491
|
serviceName: 'my-ai-agent',
|
|
292
492
|
autoInstrument: true,
|
|
293
|
-
frameworks: ['langchain', 'openai', 'anthropic']
|
|
493
|
+
frameworks: ['langchain', 'openai', 'anthropic'],
|
|
294
494
|
});
|
|
295
|
-
|
|
296
|
-
// Now all LangChain, OpenAI, and Anthropic calls are automatically traced!
|
|
495
|
+
// All LangChain, OpenAI, and Anthropic calls are now automatically traced
|
|
297
496
|
```
|
|
298
497
|
|
|
299
|
-
##
|
|
498
|
+
## Sub-package Imports
|
|
300
499
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
500
|
+
```typescript
|
|
501
|
+
// OpenAI instrumentation
|
|
502
|
+
import { instrumentOpenAIClient } from '@thinkhive/sdk/instrumentation/openai';
|
|
503
|
+
|
|
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
|
+
```
|
|
306
513
|
|
|
307
514
|
## Environment Variables
|
|
308
515
|
|
|
309
516
|
| Variable | Description |
|
|
310
517
|
|----------|-------------|
|
|
311
518
|
| `THINKHIVE_API_KEY` | Your ThinkHive API key |
|
|
312
|
-
| `THINKHIVE_ENDPOINT` | Custom API endpoint (default: https://app.thinkhive.ai) |
|
|
519
|
+
| `THINKHIVE_ENDPOINT` | Custom API endpoint (default: `https://app.thinkhive.ai`) |
|
|
313
520
|
| `THINKHIVE_SERVICE_NAME` | Service name for traces (optional) |
|
|
521
|
+
| `THINKHIVE_AGENT_ID` | Default agent ID (optional) |
|
|
314
522
|
|
|
315
|
-
##
|
|
316
|
-
|
|
317
|
-
### Key Concepts
|
|
318
|
-
|
|
319
|
-
**Run-Centric Model**: The atomic unit of work is a "Run" (not a trace). A run captures:
|
|
320
|
-
- Conversation messages
|
|
321
|
-
- Retrieved contexts
|
|
322
|
-
- Tool calls
|
|
323
|
-
- Outcome and metadata
|
|
324
|
-
|
|
325
|
-
**Facts vs Inferences**: Claims API separates:
|
|
326
|
-
- **Facts**: Verified information from retrieval or tool calls
|
|
327
|
-
- **Inferences**: LLM-generated conclusions
|
|
328
|
-
- **Computed**: Derived values from rules
|
|
329
|
-
|
|
330
|
-
**Calibrated Predictions**: Track prediction accuracy using:
|
|
331
|
-
- Brier scores for overall calibration
|
|
332
|
-
- ECE (Expected Calibration Error) for bucketed analysis
|
|
333
|
-
|
|
334
|
-
### API Structure
|
|
335
|
-
|
|
336
|
-
| API | Description |
|
|
337
|
-
|-----|-------------|
|
|
338
|
-
| `runs` | Create and manage runs (atomic work units) |
|
|
339
|
-
| `claims` | Manage facts/inferences for runs |
|
|
340
|
-
| `calibration` | Track prediction accuracy |
|
|
341
|
-
| `analyzer` | User-selected trace analysis |
|
|
342
|
-
| `issues` | Clustered failure patterns |
|
|
343
|
-
| `linking` | Connect runs to support tickets |
|
|
344
|
-
| `customerContext` | Time-series customer snapshots |
|
|
345
|
-
| `apiKeys` | API key management |
|
|
346
|
-
| `businessMetrics` | Industry-driven metrics with historical tracking |
|
|
347
|
-
| `roiAnalytics` | Business ROI and financial impact analysis |
|
|
348
|
-
| `qualityMetrics` | RAG evaluation and hallucination detection |
|
|
349
|
-
|
|
350
|
-
### Evaluation APIs
|
|
351
|
-
|
|
352
|
-
| API | Description |
|
|
353
|
-
|-----|-------------|
|
|
354
|
-
| `humanReview` | Human-in-the-loop review queues |
|
|
355
|
-
| `nondeterminism` | Multi-sample reliability testing |
|
|
356
|
-
| `evalHealth` | Evaluation metric health monitoring |
|
|
357
|
-
| `deterministicGraders` | Rule-based evaluation |
|
|
358
|
-
| `conversationEval` | Multi-turn conversation evaluation |
|
|
359
|
-
| `transcriptPatterns` | Pattern detection in transcripts |
|
|
360
|
-
|
|
361
|
-
## Upgrading from v3
|
|
362
|
-
|
|
363
|
-
See [MIGRATION.md](MIGRATION.md) for the full v3 → v4 migration guide. Key changes:
|
|
364
|
-
|
|
365
|
-
- **`apiVersion` removed** from `init()` options — routing is handled automatically per module
|
|
366
|
-
- **`calibration.recordOutcome()`** and **`calibration.reliabilityDiagram()`** removed — use `calibration.status()` and `calibration.allMetrics()` instead
|
|
367
|
-
- **Default endpoint** changed from staging to `https://app.thinkhive.ai`
|
|
368
|
-
- **OTLP init** now requires `apiKey` (agent ID alone is not sufficient)
|
|
369
|
-
|
|
370
|
-
## API Reference
|
|
523
|
+
## Upgrading from v4.0
|
|
371
524
|
|
|
372
|
-
|
|
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
|
|
373
529
|
|
|
374
530
|
## License
|
|
375
531
|
|