aigie-sdk 0.0.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/dist/index.d.mts +2268 -0
- package/dist/index.d.ts +2268 -0
- package/dist/index.js +7807 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +7711 -0
- package/dist/index.mjs.map +1 -0
- package/dist/testing/index.d.mts +7 -0
- package/dist/testing/index.d.ts +7 -0
- package/dist/testing/index.js +649 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/index.mjs +646 -0
- package/dist/testing/index.mjs.map +1 -0
- package/package.json +143 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,2268 @@
|
|
|
1
|
+
import { ZodType, ZodRawShape, ZodObject, z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Agent framework type definitions for the Aigie SDK.
|
|
5
|
+
*
|
|
6
|
+
* These types define the core abstractions for the agent system including
|
|
7
|
+
* messages, tools, guardrails, signals, evaluation, drift detection,
|
|
8
|
+
* loop detection, remediation, and streaming.
|
|
9
|
+
*
|
|
10
|
+
* @packageDocumentation
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
interface Message {
|
|
14
|
+
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
15
|
+
content: string;
|
|
16
|
+
toolCallId?: string;
|
|
17
|
+
toolCalls?: ToolCall[];
|
|
18
|
+
timestamp?: string;
|
|
19
|
+
metadata?: Record<string, unknown>;
|
|
20
|
+
}
|
|
21
|
+
interface ToolCall {
|
|
22
|
+
id: string;
|
|
23
|
+
name: string;
|
|
24
|
+
arguments: Record<string, unknown>;
|
|
25
|
+
}
|
|
26
|
+
interface ToolCallResult {
|
|
27
|
+
toolCallId: string;
|
|
28
|
+
name: string;
|
|
29
|
+
result: unknown;
|
|
30
|
+
error?: string;
|
|
31
|
+
durationMs?: number;
|
|
32
|
+
}
|
|
33
|
+
interface ModelSettings {
|
|
34
|
+
temperature?: number;
|
|
35
|
+
maxTokens?: number;
|
|
36
|
+
topP?: number;
|
|
37
|
+
frequencyPenalty?: number;
|
|
38
|
+
presencePenalty?: number;
|
|
39
|
+
stopSequences?: string[];
|
|
40
|
+
seed?: number;
|
|
41
|
+
responseFormat?: 'text' | 'json';
|
|
42
|
+
}
|
|
43
|
+
interface RunContext<T = unknown> {
|
|
44
|
+
deps: T;
|
|
45
|
+
model: string;
|
|
46
|
+
messages: Message[];
|
|
47
|
+
traceId: string;
|
|
48
|
+
spanId: string;
|
|
49
|
+
retryCount: number;
|
|
50
|
+
maxRetries: number;
|
|
51
|
+
isLastAttempt: boolean;
|
|
52
|
+
addMessage(msg: Message): void;
|
|
53
|
+
addUserMessage(content: string): void;
|
|
54
|
+
addAssistantMessage(content: string): void;
|
|
55
|
+
getMessagesForApi(): ApiMessage[];
|
|
56
|
+
withRetry(): RunContext<T>;
|
|
57
|
+
}
|
|
58
|
+
interface ApiMessage {
|
|
59
|
+
role: string;
|
|
60
|
+
content: string;
|
|
61
|
+
tool_call_id?: string;
|
|
62
|
+
tool_calls?: Array<{
|
|
63
|
+
id: string;
|
|
64
|
+
type: 'function';
|
|
65
|
+
function: {
|
|
66
|
+
name: string;
|
|
67
|
+
arguments: string;
|
|
68
|
+
};
|
|
69
|
+
}>;
|
|
70
|
+
}
|
|
71
|
+
interface ToolDefinition<TParams = any, TContext = any> {
|
|
72
|
+
name: string;
|
|
73
|
+
description: string;
|
|
74
|
+
parameters: ZodType<TParams>;
|
|
75
|
+
execute: (params: TParams, ctx: RunContext<TContext>) => unknown | Promise<unknown>;
|
|
76
|
+
requiresContext: boolean;
|
|
77
|
+
isAsync: boolean;
|
|
78
|
+
schema: Record<string, unknown>;
|
|
79
|
+
toOpenAITool(): OpenAIToolSchema;
|
|
80
|
+
toAnthropicTool(): AnthropicToolSchema;
|
|
81
|
+
}
|
|
82
|
+
interface ToolConfig<TParams extends ZodRawShape, TContext = unknown> {
|
|
83
|
+
name: string;
|
|
84
|
+
description: string;
|
|
85
|
+
parameters: ZodObject<TParams>;
|
|
86
|
+
execute: (params: z.infer<ZodObject<TParams>>, ctx: RunContext<TContext>) => unknown | Promise<unknown>;
|
|
87
|
+
}
|
|
88
|
+
interface OpenAIToolSchema {
|
|
89
|
+
type: 'function';
|
|
90
|
+
function: {
|
|
91
|
+
name: string;
|
|
92
|
+
description: string;
|
|
93
|
+
parameters: Record<string, unknown>;
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
interface AnthropicToolSchema {
|
|
97
|
+
name: string;
|
|
98
|
+
description: string;
|
|
99
|
+
input_schema: Record<string, unknown>;
|
|
100
|
+
}
|
|
101
|
+
interface AgentConfig<TContext = unknown, TOutput = string> {
|
|
102
|
+
name: string;
|
|
103
|
+
model: string;
|
|
104
|
+
systemPrompt?: string | ((ctx: RunContext<TContext>) => string | Promise<string>);
|
|
105
|
+
tools?: ToolDefinition[];
|
|
106
|
+
outputType?: ZodType<TOutput>;
|
|
107
|
+
modelSettings?: ModelSettings;
|
|
108
|
+
handoffs?: Array<{
|
|
109
|
+
name: string;
|
|
110
|
+
agent: any;
|
|
111
|
+
}>;
|
|
112
|
+
inputGuardrails?: InputGuardrail[];
|
|
113
|
+
outputGuardrails?: OutputGuardrail[];
|
|
114
|
+
maxRetries?: number;
|
|
115
|
+
maxTurns?: number;
|
|
116
|
+
}
|
|
117
|
+
interface RunOptions<TContext = unknown> {
|
|
118
|
+
deps?: TContext;
|
|
119
|
+
messageHistory?: Message[];
|
|
120
|
+
sessionId?: string;
|
|
121
|
+
userId?: string;
|
|
122
|
+
tags?: string[];
|
|
123
|
+
metadata?: Record<string, unknown>;
|
|
124
|
+
}
|
|
125
|
+
interface AgentResult<T = string> {
|
|
126
|
+
data: T;
|
|
127
|
+
usage: UsageInfo;
|
|
128
|
+
messages: Message[];
|
|
129
|
+
traceId: string;
|
|
130
|
+
durationMs: number;
|
|
131
|
+
toolCalls: number;
|
|
132
|
+
retries: number;
|
|
133
|
+
allMessages(): Message[];
|
|
134
|
+
withToolReturnContent(): Message[];
|
|
135
|
+
}
|
|
136
|
+
interface UsageInfo {
|
|
137
|
+
requestTokens: number;
|
|
138
|
+
responseTokens: number;
|
|
139
|
+
totalTokens: number;
|
|
140
|
+
costUsd: number;
|
|
141
|
+
cacheReadTokens?: number;
|
|
142
|
+
cacheCreationTokens?: number;
|
|
143
|
+
model: string;
|
|
144
|
+
}
|
|
145
|
+
declare enum GuardrailAction {
|
|
146
|
+
PASS = "pass",
|
|
147
|
+
WARN = "warn",
|
|
148
|
+
RETRY = "retry",
|
|
149
|
+
REDIRECT = "redirect",
|
|
150
|
+
ADJUST = "adjust",
|
|
151
|
+
ESCALATE = "escalate"
|
|
152
|
+
}
|
|
153
|
+
interface GuardrailResult {
|
|
154
|
+
action: GuardrailAction;
|
|
155
|
+
message?: string;
|
|
156
|
+
metadata?: Record<string, unknown>;
|
|
157
|
+
}
|
|
158
|
+
interface GuardrailContext {
|
|
159
|
+
traceId: string;
|
|
160
|
+
spanId: string;
|
|
161
|
+
durationMs: number;
|
|
162
|
+
model: string;
|
|
163
|
+
metadata: Record<string, unknown>;
|
|
164
|
+
}
|
|
165
|
+
interface InputGuardrail {
|
|
166
|
+
name: string;
|
|
167
|
+
execute: (input: string, context: GuardrailContext) => Promise<GuardrailResult>;
|
|
168
|
+
}
|
|
169
|
+
interface OutputGuardrail {
|
|
170
|
+
name: string;
|
|
171
|
+
execute: (output: unknown, context: GuardrailContext) => Promise<GuardrailResult>;
|
|
172
|
+
}
|
|
173
|
+
declare enum SignalType {
|
|
174
|
+
ERROR_CLUSTER = "error_cluster",
|
|
175
|
+
CONTEXT_DRIFT = "context_drift",
|
|
176
|
+
TOOL_LOOP = "tool_loop",
|
|
177
|
+
GOAL_DEVIATION = "goal_deviation",
|
|
178
|
+
HALLUCINATION = "hallucination",
|
|
179
|
+
QUALITY_DROP = "quality_drop",
|
|
180
|
+
LATENCY_SPIKE = "latency_spike",
|
|
181
|
+
TOKEN_OVERFLOW = "token_overflow",
|
|
182
|
+
SAFETY_VIOLATION = "safety_violation"
|
|
183
|
+
}
|
|
184
|
+
declare enum SignalSeverity {
|
|
185
|
+
LOW = "low",
|
|
186
|
+
MEDIUM = "medium",
|
|
187
|
+
HIGH = "high",
|
|
188
|
+
CRITICAL = "critical"
|
|
189
|
+
}
|
|
190
|
+
interface Signal {
|
|
191
|
+
id: string;
|
|
192
|
+
type: SignalType;
|
|
193
|
+
severity: SignalSeverity;
|
|
194
|
+
message: string;
|
|
195
|
+
traceId?: string;
|
|
196
|
+
spanId?: string;
|
|
197
|
+
metadata?: Record<string, unknown>;
|
|
198
|
+
timestamp: string;
|
|
199
|
+
}
|
|
200
|
+
interface EmitSignalRequest {
|
|
201
|
+
type: SignalType;
|
|
202
|
+
severity: SignalSeverity;
|
|
203
|
+
message: string;
|
|
204
|
+
traceId?: string;
|
|
205
|
+
spanId?: string;
|
|
206
|
+
metadata?: Record<string, unknown>;
|
|
207
|
+
}
|
|
208
|
+
type ScoreType = 'accuracy' | 'relevance' | 'coherence' | 'fluency' | 'groundedness' | 'hallucination' | 'toxicity' | 'helpfulness' | 'custom';
|
|
209
|
+
interface EvaluationScore {
|
|
210
|
+
traceId: string;
|
|
211
|
+
spanId?: string;
|
|
212
|
+
name: ScoreType | string;
|
|
213
|
+
value: number;
|
|
214
|
+
dataType?: 'NUMERIC' | 'CATEGORICAL' | 'BOOLEAN';
|
|
215
|
+
comment?: string;
|
|
216
|
+
metadata?: Record<string, unknown>;
|
|
217
|
+
}
|
|
218
|
+
interface JudgeConfig {
|
|
219
|
+
model: string;
|
|
220
|
+
criteria: ScoreType[];
|
|
221
|
+
systemPrompt?: string;
|
|
222
|
+
}
|
|
223
|
+
interface JudgeResult {
|
|
224
|
+
scores: Record<string, number>;
|
|
225
|
+
reasoning: string;
|
|
226
|
+
overallScore: number;
|
|
227
|
+
}
|
|
228
|
+
type DriftType = 'topic' | 'behavior' | 'quality' | 'coherence';
|
|
229
|
+
interface DriftDetectorConfig {
|
|
230
|
+
detectors: DriftType[];
|
|
231
|
+
thresholds?: Partial<Record<DriftType, number>>;
|
|
232
|
+
}
|
|
233
|
+
interface DriftResult {
|
|
234
|
+
hasDrift: boolean;
|
|
235
|
+
type?: DriftType;
|
|
236
|
+
score: number;
|
|
237
|
+
details: string;
|
|
238
|
+
}
|
|
239
|
+
interface PhaseContext {
|
|
240
|
+
name: string;
|
|
241
|
+
startTime: number;
|
|
242
|
+
setOutput(output: unknown): void;
|
|
243
|
+
setMetadata(metadata: Record<string, unknown>): void;
|
|
244
|
+
}
|
|
245
|
+
interface CycleMetrics {
|
|
246
|
+
thinkDurationMs: number;
|
|
247
|
+
actDurationMs: number;
|
|
248
|
+
observeDurationMs: number;
|
|
249
|
+
totalDurationMs: number;
|
|
250
|
+
cycleCount: number;
|
|
251
|
+
}
|
|
252
|
+
interface PlanMetrics {
|
|
253
|
+
completionRate: number;
|
|
254
|
+
deviations: Deviation[];
|
|
255
|
+
unexpectedSteps: number;
|
|
256
|
+
wrongOrderSteps: number;
|
|
257
|
+
totalDurationMs: number;
|
|
258
|
+
}
|
|
259
|
+
interface Deviation {
|
|
260
|
+
type: 'unexpected_step' | 'wrong_order' | 'missing_step' | 'repeated_step';
|
|
261
|
+
stepName: string;
|
|
262
|
+
message: string;
|
|
263
|
+
timestamp: string;
|
|
264
|
+
}
|
|
265
|
+
interface LoopDetectorConfig {
|
|
266
|
+
similarityThreshold?: number;
|
|
267
|
+
maxConsecutiveRepeats?: number;
|
|
268
|
+
action?: 'warn' | 'break' | 'auto_fix';
|
|
269
|
+
}
|
|
270
|
+
interface LoopDetectionResult {
|
|
271
|
+
isLoop: boolean;
|
|
272
|
+
similarity: number;
|
|
273
|
+
repeatCount: number;
|
|
274
|
+
action?: 'warn' | 'break' | 'auto_fix';
|
|
275
|
+
}
|
|
276
|
+
interface RemediationAction {
|
|
277
|
+
action: 'retry' | 'fallback' | 'reset' | 'redirect' | 'escalate';
|
|
278
|
+
newSystemPrompt?: string;
|
|
279
|
+
newModel?: string;
|
|
280
|
+
metadata?: Record<string, unknown>;
|
|
281
|
+
}
|
|
282
|
+
type RemediationHandler = (signal: Signal) => Promise<RemediationAction>;
|
|
283
|
+
interface UsageDetails {
|
|
284
|
+
input: number;
|
|
285
|
+
output: number;
|
|
286
|
+
cacheReadInputTokens?: number;
|
|
287
|
+
total?: number;
|
|
288
|
+
[key: string]: number | undefined;
|
|
289
|
+
}
|
|
290
|
+
interface CostDetails {
|
|
291
|
+
input: number;
|
|
292
|
+
output: number;
|
|
293
|
+
cacheReadInputTokens?: number;
|
|
294
|
+
total?: number;
|
|
295
|
+
[key: string]: number | undefined;
|
|
296
|
+
}
|
|
297
|
+
interface IntegrationInfo {
|
|
298
|
+
name: string;
|
|
299
|
+
displayName: string;
|
|
300
|
+
packageName: string;
|
|
301
|
+
patchFunction: string;
|
|
302
|
+
handlerClass?: string;
|
|
303
|
+
isPatched?: boolean;
|
|
304
|
+
}
|
|
305
|
+
interface StreamChunk {
|
|
306
|
+
type: 'text' | 'tool_call' | 'tool_result' | 'error' | 'done';
|
|
307
|
+
content?: string;
|
|
308
|
+
toolCall?: ToolCall;
|
|
309
|
+
toolResult?: ToolCallResult;
|
|
310
|
+
error?: string;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Kyte SDK Type Definitions
|
|
315
|
+
*/
|
|
316
|
+
interface AigieConfig {
|
|
317
|
+
/** Kyte API endpoint URL */
|
|
318
|
+
apiUrl: string;
|
|
319
|
+
/** API key for authentication */
|
|
320
|
+
apiKey: string;
|
|
321
|
+
/** Project name for grouping traces */
|
|
322
|
+
projectName?: string;
|
|
323
|
+
/** Default tags to add to all traces */
|
|
324
|
+
defaultTags?: string[];
|
|
325
|
+
/** Default metadata to add to all traces */
|
|
326
|
+
defaultMetadata?: Record<string, any>;
|
|
327
|
+
/** Enable/disable tracing */
|
|
328
|
+
enabled?: boolean;
|
|
329
|
+
/** Batch traces before sending */
|
|
330
|
+
batchEnabled?: boolean;
|
|
331
|
+
/** Batch size (number of traces to batch) */
|
|
332
|
+
batchSize?: number;
|
|
333
|
+
/** Batch timeout in milliseconds */
|
|
334
|
+
batchTimeout?: number;
|
|
335
|
+
/** Enable debug logging */
|
|
336
|
+
debug?: boolean;
|
|
337
|
+
/** Default user ID */
|
|
338
|
+
defaultUserId?: string;
|
|
339
|
+
/** Default session ID */
|
|
340
|
+
defaultSessionId?: string;
|
|
341
|
+
/** Environment (e.g., 'production', 'staging') */
|
|
342
|
+
environment?: string;
|
|
343
|
+
/** Release version */
|
|
344
|
+
release?: string;
|
|
345
|
+
/** Sampling rate (0-1) */
|
|
346
|
+
samplingRate?: number;
|
|
347
|
+
/** Enable compression */
|
|
348
|
+
compressionEnabled?: boolean;
|
|
349
|
+
/** Max retries for API calls */
|
|
350
|
+
maxRetries?: number;
|
|
351
|
+
/** Retry delay in milliseconds */
|
|
352
|
+
retryDelay?: number;
|
|
353
|
+
/** Callback for ingestion errors */
|
|
354
|
+
onError?: (error: Error) => void;
|
|
355
|
+
/** Maximum event queue size (default 1000) */
|
|
356
|
+
maxQueueSize?: number;
|
|
357
|
+
}
|
|
358
|
+
interface TraceOptions {
|
|
359
|
+
/** Trace name */
|
|
360
|
+
name?: string;
|
|
361
|
+
/** Trace type (chain, llm, tool, etc.) */
|
|
362
|
+
type?: SpanType;
|
|
363
|
+
/** Input data */
|
|
364
|
+
input?: Record<string, any>;
|
|
365
|
+
/** Tags for this trace */
|
|
366
|
+
tags?: string[];
|
|
367
|
+
/** Metadata for this trace */
|
|
368
|
+
metadata?: Record<string, any>;
|
|
369
|
+
/** User ID for this trace */
|
|
370
|
+
userId?: string;
|
|
371
|
+
/** Session ID for grouping multi-turn conversations */
|
|
372
|
+
sessionId?: string;
|
|
373
|
+
}
|
|
374
|
+
interface SpanOptions extends TraceOptions {
|
|
375
|
+
/** Parent span ID */
|
|
376
|
+
parentSpanId?: string;
|
|
377
|
+
/** Model name (for LLM spans) */
|
|
378
|
+
modelName?: string;
|
|
379
|
+
}
|
|
380
|
+
interface Trace {
|
|
381
|
+
/** Unique trace ID */
|
|
382
|
+
id: string;
|
|
383
|
+
/** Trace name */
|
|
384
|
+
name: string;
|
|
385
|
+
/** Trace type */
|
|
386
|
+
type: string;
|
|
387
|
+
/** Input data */
|
|
388
|
+
input: Record<string, any>;
|
|
389
|
+
/** Output data */
|
|
390
|
+
output?: Record<string, any>;
|
|
391
|
+
/** Status (success, failed, pending) */
|
|
392
|
+
status: TraceStatus;
|
|
393
|
+
/** Error message if failed */
|
|
394
|
+
errorMessage?: string;
|
|
395
|
+
/** Tags */
|
|
396
|
+
tags: string[];
|
|
397
|
+
/** Metadata */
|
|
398
|
+
metadata: Record<string, any>;
|
|
399
|
+
/** Project name */
|
|
400
|
+
projectName?: string;
|
|
401
|
+
/** User ID */
|
|
402
|
+
userId?: string;
|
|
403
|
+
/** Session ID */
|
|
404
|
+
sessionId?: string;
|
|
405
|
+
/** Start timestamp */
|
|
406
|
+
startTime: string;
|
|
407
|
+
/** End timestamp */
|
|
408
|
+
endTime?: string;
|
|
409
|
+
/** Duration in nanoseconds */
|
|
410
|
+
durationNs?: number;
|
|
411
|
+
/** Created timestamp */
|
|
412
|
+
createdAt: string;
|
|
413
|
+
}
|
|
414
|
+
interface Span {
|
|
415
|
+
/** Unique span ID */
|
|
416
|
+
id: string;
|
|
417
|
+
/** Parent trace ID */
|
|
418
|
+
traceId: string;
|
|
419
|
+
/** Parent span ID */
|
|
420
|
+
parentSpanId?: string;
|
|
421
|
+
/** Span name */
|
|
422
|
+
name: string;
|
|
423
|
+
/** Span type */
|
|
424
|
+
type: string;
|
|
425
|
+
/** Input data */
|
|
426
|
+
input: Record<string, any>;
|
|
427
|
+
/** Output data */
|
|
428
|
+
output?: Record<string, any>;
|
|
429
|
+
/** Status */
|
|
430
|
+
status: TraceStatus;
|
|
431
|
+
/** Error message if failed */
|
|
432
|
+
errorMessage?: string;
|
|
433
|
+
/** Tags */
|
|
434
|
+
tags: string[];
|
|
435
|
+
/** Metadata */
|
|
436
|
+
metadata: Record<string, any>;
|
|
437
|
+
/** Model name (for LLM spans) */
|
|
438
|
+
modelName?: string;
|
|
439
|
+
/** Token counts */
|
|
440
|
+
tokens?: TokenUsage;
|
|
441
|
+
/** Cost in USD */
|
|
442
|
+
cost?: number;
|
|
443
|
+
/** Start timestamp */
|
|
444
|
+
startTime: string;
|
|
445
|
+
/** End timestamp */
|
|
446
|
+
endTime?: string;
|
|
447
|
+
/** Duration in nanoseconds */
|
|
448
|
+
durationNs?: number;
|
|
449
|
+
/** Created timestamp */
|
|
450
|
+
createdAt: string;
|
|
451
|
+
}
|
|
452
|
+
interface TokenUsage {
|
|
453
|
+
prompt?: number;
|
|
454
|
+
completion?: number;
|
|
455
|
+
total?: number;
|
|
456
|
+
cached?: number;
|
|
457
|
+
}
|
|
458
|
+
interface Score {
|
|
459
|
+
/** Score name */
|
|
460
|
+
name: string;
|
|
461
|
+
/** Numeric value (0-1 or custom range) */
|
|
462
|
+
value?: number;
|
|
463
|
+
/** String value for categorical scores */
|
|
464
|
+
stringValue?: string;
|
|
465
|
+
/** Comment/explanation */
|
|
466
|
+
comment?: string;
|
|
467
|
+
/** Data type */
|
|
468
|
+
dataType?: 'numeric' | 'boolean' | 'categorical';
|
|
469
|
+
}
|
|
470
|
+
type SpanType = 'llm' | 'chain' | 'tool' | 'retriever' | 'embedding' | 'prompt' | 'agent' | 'guardrail' | 'evaluation' | 'remediation' | 'custom';
|
|
471
|
+
type TraceStatus = 'success' | 'failed' | 'pending';
|
|
472
|
+
interface TraceContext {
|
|
473
|
+
/** Current trace ID */
|
|
474
|
+
traceId: string;
|
|
475
|
+
/** Current span ID */
|
|
476
|
+
spanId?: string;
|
|
477
|
+
/** Add a child span */
|
|
478
|
+
addSpan: (name: string, options?: SpanOptions) => Promise<SpanContext>;
|
|
479
|
+
/** End the current trace */
|
|
480
|
+
end: (output?: Record<string, any>, error?: Error) => Promise<void>;
|
|
481
|
+
/** Add a score to the trace */
|
|
482
|
+
addScore: (score: Score) => Promise<void>;
|
|
483
|
+
}
|
|
484
|
+
interface SpanContext {
|
|
485
|
+
/** Current span ID */
|
|
486
|
+
spanId: string;
|
|
487
|
+
/** Parent trace ID */
|
|
488
|
+
traceId: string;
|
|
489
|
+
/** End the span */
|
|
490
|
+
end: (output?: Record<string, any>, error?: Error) => Promise<void>;
|
|
491
|
+
/** Add a score to the span */
|
|
492
|
+
addScore: (score: Score) => Promise<void>;
|
|
493
|
+
}
|
|
494
|
+
interface TraceableConfig {
|
|
495
|
+
/** Name override for the trace/span */
|
|
496
|
+
name?: string;
|
|
497
|
+
/** Type override */
|
|
498
|
+
type?: SpanType;
|
|
499
|
+
/** Tags to add */
|
|
500
|
+
tags?: string[];
|
|
501
|
+
/** Metadata to add */
|
|
502
|
+
metadata?: Record<string, any>;
|
|
503
|
+
/** Capture function arguments as input */
|
|
504
|
+
captureInput?: boolean;
|
|
505
|
+
/** Capture function output */
|
|
506
|
+
captureOutput?: boolean;
|
|
507
|
+
/** Capture errors */
|
|
508
|
+
captureErrors?: boolean;
|
|
509
|
+
}
|
|
510
|
+
interface AutoInstrumentConfig {
|
|
511
|
+
/** Auto-instrument OpenAI SDK */
|
|
512
|
+
openai?: boolean;
|
|
513
|
+
/** Auto-instrument Anthropic SDK */
|
|
514
|
+
anthropic?: boolean;
|
|
515
|
+
/** Auto-instrument LangChain */
|
|
516
|
+
langchain?: boolean;
|
|
517
|
+
/** Auto-instrument Vercel AI SDK */
|
|
518
|
+
vercelAI?: boolean;
|
|
519
|
+
/** Auto-instrument HTTP/fetch */
|
|
520
|
+
http?: boolean;
|
|
521
|
+
/** Auto-instrument LangGraph */
|
|
522
|
+
langgraph?: boolean;
|
|
523
|
+
/** Auto-instrument CrewAI */
|
|
524
|
+
crewai?: boolean;
|
|
525
|
+
/** Auto-instrument AutoGen */
|
|
526
|
+
autogen?: boolean;
|
|
527
|
+
/** Auto-instrument OpenAI Agents SDK */
|
|
528
|
+
openaiAgents?: boolean;
|
|
529
|
+
/** Auto-instrument Claude Agent SDK */
|
|
530
|
+
claudeAgentSdk?: boolean;
|
|
531
|
+
/** Auto-instrument Browser-Use */
|
|
532
|
+
browserUse?: boolean;
|
|
533
|
+
/** Auto-instrument LlamaIndex */
|
|
534
|
+
llamaindex?: boolean;
|
|
535
|
+
/** Auto-instrument DSPy */
|
|
536
|
+
dspy?: boolean;
|
|
537
|
+
/** Auto-instrument Pipecat */
|
|
538
|
+
pipecat?: boolean;
|
|
539
|
+
/** Auto-instrument Instructor */
|
|
540
|
+
instructor?: boolean;
|
|
541
|
+
/** Auto-instrument Semantic Kernel */
|
|
542
|
+
semanticKernel?: boolean;
|
|
543
|
+
/** Auto-instrument Strands Agents */
|
|
544
|
+
strands?: boolean;
|
|
545
|
+
/** Auto-instrument Google ADK */
|
|
546
|
+
googleAdk?: boolean;
|
|
547
|
+
/** Patch all registered integrations via IntegrationRegistry */
|
|
548
|
+
patchAll?: boolean;
|
|
549
|
+
/** Custom instrumentations */
|
|
550
|
+
custom?: CustomInstrumentation[];
|
|
551
|
+
}
|
|
552
|
+
interface CustomInstrumentation {
|
|
553
|
+
/** Name of the instrumentation */
|
|
554
|
+
name: string;
|
|
555
|
+
/** Module to instrument */
|
|
556
|
+
module: string;
|
|
557
|
+
/** Methods to wrap */
|
|
558
|
+
methods: string[];
|
|
559
|
+
/** Wrapper function */
|
|
560
|
+
wrapper: (original: Function, methodName: string) => Function;
|
|
561
|
+
}
|
|
562
|
+
interface SpanBuilder {
|
|
563
|
+
/** Set span name */
|
|
564
|
+
setName(name: string): SpanBuilder;
|
|
565
|
+
/** Set span type */
|
|
566
|
+
setType(type: SpanType): SpanBuilder;
|
|
567
|
+
/** Set input data */
|
|
568
|
+
setInput(input: Record<string, any>): SpanBuilder;
|
|
569
|
+
/** Set metadata */
|
|
570
|
+
setMetadata(metadata: Record<string, any>): SpanBuilder;
|
|
571
|
+
/** Add metadata (merge) */
|
|
572
|
+
addMetadata(key: string, value: any): SpanBuilder;
|
|
573
|
+
/** Set tags */
|
|
574
|
+
setTags(tags: string[]): SpanBuilder;
|
|
575
|
+
/** Add tag */
|
|
576
|
+
addTag(tag: string): SpanBuilder;
|
|
577
|
+
/** Set user ID */
|
|
578
|
+
setUserId(userId: string): SpanBuilder;
|
|
579
|
+
/** Set session ID */
|
|
580
|
+
setSessionId(sessionId: string): SpanBuilder;
|
|
581
|
+
/** Set model name */
|
|
582
|
+
setModelName(modelName: string): SpanBuilder;
|
|
583
|
+
/** Start the span */
|
|
584
|
+
start(): ActiveSpan;
|
|
585
|
+
}
|
|
586
|
+
interface ActiveSpan {
|
|
587
|
+
/** Span ID */
|
|
588
|
+
readonly id: string;
|
|
589
|
+
/** Trace ID */
|
|
590
|
+
readonly traceId: string;
|
|
591
|
+
/** Update span data */
|
|
592
|
+
update(data: Partial<SpanUpdateData>): ActiveSpan;
|
|
593
|
+
/** Set output */
|
|
594
|
+
setOutput(output: Record<string, any>): ActiveSpan;
|
|
595
|
+
/** Set tokens */
|
|
596
|
+
setTokens(tokens: TokenUsage): ActiveSpan;
|
|
597
|
+
/** Set cost */
|
|
598
|
+
setCost(cost: number): ActiveSpan;
|
|
599
|
+
/** Add score */
|
|
600
|
+
addScore(score: Score): ActiveSpan;
|
|
601
|
+
/** Set error */
|
|
602
|
+
setError(error: Error | string): ActiveSpan;
|
|
603
|
+
/** End the span */
|
|
604
|
+
end(): Promise<void>;
|
|
605
|
+
}
|
|
606
|
+
interface SpanUpdateData {
|
|
607
|
+
output?: Record<string, any>;
|
|
608
|
+
tokens?: TokenUsage;
|
|
609
|
+
cost?: number;
|
|
610
|
+
metadata?: Record<string, any>;
|
|
611
|
+
tags?: string[];
|
|
612
|
+
modelName?: string;
|
|
613
|
+
}
|
|
614
|
+
interface SmartCacheConfig<T> {
|
|
615
|
+
/** Time-to-live in milliseconds */
|
|
616
|
+
ttl: number;
|
|
617
|
+
/** Serve stale data while refreshing */
|
|
618
|
+
staleWhileRevalidate?: boolean;
|
|
619
|
+
/** Refresh in background when expired */
|
|
620
|
+
backgroundRefresh?: boolean;
|
|
621
|
+
/** Maximum cache size */
|
|
622
|
+
maxSize?: number;
|
|
623
|
+
/** Function to fetch fresh data */
|
|
624
|
+
onRefresh?: (key: string) => Promise<T>;
|
|
625
|
+
/** Called when cache entry expires */
|
|
626
|
+
onExpire?: (key: string, value: T) => void;
|
|
627
|
+
}
|
|
628
|
+
interface CacheEntry<T> {
|
|
629
|
+
value: T;
|
|
630
|
+
expiresAt: number;
|
|
631
|
+
createdAt: number;
|
|
632
|
+
lastAccessed: number;
|
|
633
|
+
}
|
|
634
|
+
type AnnotationType = 'correction' | 'approval' | 'rejection' | 'comment' | 'label' | 'score' | 'thumbs';
|
|
635
|
+
interface Annotation {
|
|
636
|
+
/** Unique annotation ID */
|
|
637
|
+
id: string;
|
|
638
|
+
/** Trace ID */
|
|
639
|
+
traceId: string;
|
|
640
|
+
/** Span ID (optional) */
|
|
641
|
+
spanId?: string;
|
|
642
|
+
/** Annotation type */
|
|
643
|
+
type: AnnotationType;
|
|
644
|
+
/** Annotation value */
|
|
645
|
+
value: string | number | boolean;
|
|
646
|
+
/** Comment/explanation */
|
|
647
|
+
comment?: string;
|
|
648
|
+
/** Annotator user ID */
|
|
649
|
+
annotatorId?: string;
|
|
650
|
+
/** Metadata */
|
|
651
|
+
metadata?: Record<string, any>;
|
|
652
|
+
/** Created timestamp */
|
|
653
|
+
createdAt: string;
|
|
654
|
+
/** Updated timestamp */
|
|
655
|
+
updatedAt?: string;
|
|
656
|
+
}
|
|
657
|
+
interface CreateAnnotationRequest {
|
|
658
|
+
traceId: string;
|
|
659
|
+
spanId?: string;
|
|
660
|
+
type: AnnotationType;
|
|
661
|
+
value: string | number | boolean;
|
|
662
|
+
comment?: string;
|
|
663
|
+
annotatorId?: string;
|
|
664
|
+
metadata?: Record<string, any>;
|
|
665
|
+
}
|
|
666
|
+
type FeedbackType = 'thumbs' | 'rating' | 'categorical' | 'text';
|
|
667
|
+
interface Feedback {
|
|
668
|
+
/** Unique feedback ID */
|
|
669
|
+
id: string;
|
|
670
|
+
/** Trace ID */
|
|
671
|
+
traceId: string;
|
|
672
|
+
/** Span ID (optional) */
|
|
673
|
+
spanId?: string;
|
|
674
|
+
/** Feedback type */
|
|
675
|
+
type: FeedbackType;
|
|
676
|
+
/** Feedback value */
|
|
677
|
+
value: 'up' | 'down' | number | string;
|
|
678
|
+
/** Rating scale (for rating type) */
|
|
679
|
+
scale?: [number, number];
|
|
680
|
+
/** Categories (for categorical type) */
|
|
681
|
+
categories?: string[];
|
|
682
|
+
/** Comment */
|
|
683
|
+
comment?: string;
|
|
684
|
+
/** User ID who provided feedback */
|
|
685
|
+
userId?: string;
|
|
686
|
+
/** Metadata */
|
|
687
|
+
metadata?: Record<string, any>;
|
|
688
|
+
/** Created timestamp */
|
|
689
|
+
createdAt: string;
|
|
690
|
+
}
|
|
691
|
+
interface CreateFeedbackRequest {
|
|
692
|
+
traceId: string;
|
|
693
|
+
spanId?: string;
|
|
694
|
+
type: FeedbackType;
|
|
695
|
+
value: 'up' | 'down' | number | string;
|
|
696
|
+
scale?: [number, number];
|
|
697
|
+
categories?: string[];
|
|
698
|
+
comment?: string;
|
|
699
|
+
userId?: string;
|
|
700
|
+
metadata?: Record<string, any>;
|
|
701
|
+
}
|
|
702
|
+
interface FeedbackStats {
|
|
703
|
+
totalCount: number;
|
|
704
|
+
thumbsUp: number;
|
|
705
|
+
thumbsDown: number;
|
|
706
|
+
averageRating?: number;
|
|
707
|
+
categoryBreakdown?: Record<string, number>;
|
|
708
|
+
}
|
|
709
|
+
interface MediaReference {
|
|
710
|
+
/** Unique media ID */
|
|
711
|
+
id: string;
|
|
712
|
+
/** SHA256 hash for deduplication */
|
|
713
|
+
hash: string;
|
|
714
|
+
/** MIME type */
|
|
715
|
+
mimeType: string;
|
|
716
|
+
/** File size in bytes */
|
|
717
|
+
size: number;
|
|
718
|
+
/** URL to access media */
|
|
719
|
+
url?: string;
|
|
720
|
+
/** Created timestamp */
|
|
721
|
+
createdAt: string;
|
|
722
|
+
}
|
|
723
|
+
interface ProcessedMedia {
|
|
724
|
+
/** Sanitized messages (media replaced with references) */
|
|
725
|
+
messages: any[];
|
|
726
|
+
/** Extracted media references */
|
|
727
|
+
mediaRefs: MediaReference[];
|
|
728
|
+
}
|
|
729
|
+
interface WrapperOptions {
|
|
730
|
+
/** Custom trace name */
|
|
731
|
+
traceName?: string;
|
|
732
|
+
/** User ID */
|
|
733
|
+
userId?: string;
|
|
734
|
+
/** Session ID */
|
|
735
|
+
sessionId?: string;
|
|
736
|
+
/** Tags */
|
|
737
|
+
tags?: string[];
|
|
738
|
+
/** Metadata */
|
|
739
|
+
metadata?: Record<string, any>;
|
|
740
|
+
}
|
|
741
|
+
interface ModelPricing {
|
|
742
|
+
/** Model name */
|
|
743
|
+
model: string;
|
|
744
|
+
/** Provider */
|
|
745
|
+
provider: 'openai' | 'anthropic' | 'google' | 'cohere' | 'bedrock' | 'other';
|
|
746
|
+
/** Input price per 1M tokens */
|
|
747
|
+
inputPricePerMillion: number;
|
|
748
|
+
/** Output price per 1M tokens */
|
|
749
|
+
outputPricePerMillion: number;
|
|
750
|
+
/** Cached input price per 1M tokens (optional) */
|
|
751
|
+
cachedInputPricePerMillion?: number;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* Kyte Client - Core SDK class for tracing and monitoring
|
|
756
|
+
*/
|
|
757
|
+
|
|
758
|
+
interface TracingContext {
|
|
759
|
+
traceId: string;
|
|
760
|
+
spanId?: string;
|
|
761
|
+
parentSpanId?: string;
|
|
762
|
+
}
|
|
763
|
+
/**
|
|
764
|
+
* Initialize the Kyte SDK
|
|
765
|
+
*/
|
|
766
|
+
declare function initKyte(config: AigieConfig): AigieClient;
|
|
767
|
+
/**
|
|
768
|
+
* Alias for initKyte
|
|
769
|
+
*/
|
|
770
|
+
declare const initAigie: typeof initKyte;
|
|
771
|
+
/**
|
|
772
|
+
* Get the global Kyte client
|
|
773
|
+
*/
|
|
774
|
+
declare function getKyte(): AigieClient;
|
|
775
|
+
/**
|
|
776
|
+
* Alias for getKyte
|
|
777
|
+
*/
|
|
778
|
+
declare const getAigie: typeof getKyte;
|
|
779
|
+
declare class AigieClient {
|
|
780
|
+
private config;
|
|
781
|
+
private eventQueue;
|
|
782
|
+
private batchTimer;
|
|
783
|
+
private enabled;
|
|
784
|
+
constructor(config: AigieConfig);
|
|
785
|
+
isEnabled(): boolean;
|
|
786
|
+
disable(): void;
|
|
787
|
+
enable(): void;
|
|
788
|
+
getCurrentContext(): TracingContext | undefined;
|
|
789
|
+
spanBuilder(name: string): SpanBuilder;
|
|
790
|
+
startSpan(options: SpanOptions): ActiveSpan;
|
|
791
|
+
trace<T>(name: string, fn: () => T | Promise<T>, options?: TraceOptions): Promise<T>;
|
|
792
|
+
span<T>(name: string, fn: () => T | Promise<T>, options?: SpanOptions): Promise<T>;
|
|
793
|
+
addScore(traceId: string, score: Score, spanId?: string): Promise<void>;
|
|
794
|
+
sendTraceUpdate(trace: Trace): Promise<void>;
|
|
795
|
+
sendSpanUpdate(span: Span): Promise<void>;
|
|
796
|
+
private queueEvent;
|
|
797
|
+
private mapSpanType;
|
|
798
|
+
private traceToEvent;
|
|
799
|
+
private buildTokenUsage;
|
|
800
|
+
private spanToEvent;
|
|
801
|
+
private traceUpdateEvent;
|
|
802
|
+
private spanUpdateEvent;
|
|
803
|
+
private flushBatch;
|
|
804
|
+
private sendToApi;
|
|
805
|
+
/**
|
|
806
|
+
* Force flush all pending spans and signals immediately.
|
|
807
|
+
* Unlike shutdown(), this does not disable the client afterwards.
|
|
808
|
+
*/
|
|
809
|
+
forceFlush(): Promise<void>;
|
|
810
|
+
shutdown(): Promise<void>;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
/**
|
|
814
|
+
* Decorators
|
|
815
|
+
*
|
|
816
|
+
* @traceable decorator and trace wrapper function for automatic tracing.
|
|
817
|
+
*/
|
|
818
|
+
|
|
819
|
+
/**
|
|
820
|
+
* @traceable decorator - Auto-trace class methods
|
|
821
|
+
*
|
|
822
|
+
* @example
|
|
823
|
+
* ```typescript
|
|
824
|
+
* class MyService {
|
|
825
|
+
* @traceable({ name: 'processData', type: 'chain' })
|
|
826
|
+
* async processData(input: string) {
|
|
827
|
+
* return await this.doWork(input);
|
|
828
|
+
* }
|
|
829
|
+
*
|
|
830
|
+
* @traceable({ captureInput: true, captureOutput: true })
|
|
831
|
+
* async doWork(input: string) {
|
|
832
|
+
* return `processed: ${input}`;
|
|
833
|
+
* }
|
|
834
|
+
* }
|
|
835
|
+
* ```
|
|
836
|
+
*/
|
|
837
|
+
declare function traceable(config?: TraceableConfig): (_target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
838
|
+
/**
|
|
839
|
+
* trace wrapper function - Wrap any function with tracing
|
|
840
|
+
*
|
|
841
|
+
* @example
|
|
842
|
+
* ```typescript
|
|
843
|
+
* const tracedFetch = trace(
|
|
844
|
+
* async (url: string) => fetch(url),
|
|
845
|
+
* { name: 'fetchData', type: 'tool' }
|
|
846
|
+
* );
|
|
847
|
+
*
|
|
848
|
+
* const response = await tracedFetch('https://api.example.com');
|
|
849
|
+
* ```
|
|
850
|
+
*/
|
|
851
|
+
declare function trace<T extends (...args: any[]) => any>(fn: T, config?: TraceableConfig): T;
|
|
852
|
+
/**
|
|
853
|
+
* observe decorator - Simpler alternative to @traceable (Laminar-style API)
|
|
854
|
+
*
|
|
855
|
+
* @example
|
|
856
|
+
* ```typescript
|
|
857
|
+
* class MyAgent {
|
|
858
|
+
* @observe()
|
|
859
|
+
* async think(input: string) {
|
|
860
|
+
* return await this.llm.generate(input);
|
|
861
|
+
* }
|
|
862
|
+
* }
|
|
863
|
+
* ```
|
|
864
|
+
*/
|
|
865
|
+
declare function observe(config?: {
|
|
866
|
+
name?: string;
|
|
867
|
+
type?: SpanType;
|
|
868
|
+
}): (_target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
869
|
+
/**
|
|
870
|
+
* withTracing - Higher-order function for wrapping functions with tracing
|
|
871
|
+
*
|
|
872
|
+
* @example
|
|
873
|
+
* ```typescript
|
|
874
|
+
* const myFunction = withTracing(
|
|
875
|
+
* async (x: number) => x * 2,
|
|
876
|
+
* 'doubleValue'
|
|
877
|
+
* );
|
|
878
|
+
* ```
|
|
879
|
+
*/
|
|
880
|
+
declare function withTracing<T extends (...args: any[]) => any>(fn: T, name?: string, options?: Omit<TraceableConfig, 'name'>): T;
|
|
881
|
+
|
|
882
|
+
/**
|
|
883
|
+
* Error hierarchy for the Aigie SDK agent framework.
|
|
884
|
+
*
|
|
885
|
+
* All errors extend AigieError, which ensures correct prototype chains
|
|
886
|
+
* and sets this.name to the constructor name for proper instanceof checks.
|
|
887
|
+
*/
|
|
888
|
+
declare class AigieError extends Error {
|
|
889
|
+
constructor(message: string);
|
|
890
|
+
}
|
|
891
|
+
declare class DriftError extends AigieError {
|
|
892
|
+
}
|
|
893
|
+
declare class ContextDriftDetected extends DriftError {
|
|
894
|
+
}
|
|
895
|
+
declare class TopicDriftDetected extends DriftError {
|
|
896
|
+
}
|
|
897
|
+
declare class BehaviorDriftDetected extends DriftError {
|
|
898
|
+
}
|
|
899
|
+
declare class LoopDetectedError extends DriftError {
|
|
900
|
+
}
|
|
901
|
+
declare class RemediationError extends AigieError {
|
|
902
|
+
}
|
|
903
|
+
declare class RemediationFailed extends RemediationError {
|
|
904
|
+
}
|
|
905
|
+
declare class RemediationRejected extends RemediationError {
|
|
906
|
+
}
|
|
907
|
+
declare class RetryExhausted extends RemediationError {
|
|
908
|
+
}
|
|
909
|
+
declare class ModelRetry extends AigieError {
|
|
910
|
+
}
|
|
911
|
+
declare class TracingError extends AigieError {
|
|
912
|
+
}
|
|
913
|
+
declare class InterceptionError extends AigieError {
|
|
914
|
+
}
|
|
915
|
+
declare class GuardrailError extends AigieError {
|
|
916
|
+
}
|
|
917
|
+
declare class AgentExecutionError extends AigieError {
|
|
918
|
+
}
|
|
919
|
+
declare class ConfigurationError extends AigieError {
|
|
920
|
+
}
|
|
921
|
+
declare class TimeoutError extends AigieError {
|
|
922
|
+
}
|
|
923
|
+
declare class ValidationError extends AigieError {
|
|
924
|
+
}
|
|
925
|
+
declare class ToolExecutionError extends AigieError {
|
|
926
|
+
readonly toolName: string;
|
|
927
|
+
constructor(message: string, toolName: string);
|
|
928
|
+
}
|
|
929
|
+
declare class ProviderError extends AigieError {
|
|
930
|
+
readonly provider: string;
|
|
931
|
+
readonly statusCode?: number | undefined;
|
|
932
|
+
constructor(message: string, provider: string, statusCode?: number | undefined);
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
/**
|
|
936
|
+
* Zod-based tool system for the Aigie SDK agent framework.
|
|
937
|
+
*
|
|
938
|
+
* Provides `tool()` and `toolPlain()` functions for defining tools with
|
|
939
|
+
* Zod schema validation and automatic JSON Schema generation.
|
|
940
|
+
*
|
|
941
|
+
* @packageDocumentation
|
|
942
|
+
*/
|
|
943
|
+
|
|
944
|
+
/**
|
|
945
|
+
* Create a tool definition with Zod schema validation.
|
|
946
|
+
*
|
|
947
|
+
* The execute function receives both the validated params and a RunContext.
|
|
948
|
+
*
|
|
949
|
+
* @example
|
|
950
|
+
* ```ts
|
|
951
|
+
* const searchTool = tool({
|
|
952
|
+
* name: 'search',
|
|
953
|
+
* description: 'Search the database',
|
|
954
|
+
* parameters: z.object({
|
|
955
|
+
* query: z.string().describe('Search query'),
|
|
956
|
+
* limit: z.number().optional().describe('Max results'),
|
|
957
|
+
* }),
|
|
958
|
+
* execute: async (params, ctx) => {
|
|
959
|
+
* return `Results for: ${params.query}`;
|
|
960
|
+
* },
|
|
961
|
+
* });
|
|
962
|
+
* ```
|
|
963
|
+
*/
|
|
964
|
+
declare function tool<TParams extends ZodRawShape, TContext = unknown>(config: ToolConfig<TParams, TContext>): ToolDefinition<z.infer<ZodObject<TParams>>, TContext>;
|
|
965
|
+
/**
|
|
966
|
+
* Create a tool definition without RunContext.
|
|
967
|
+
*
|
|
968
|
+
* The execute function receives only the validated params.
|
|
969
|
+
* Sets requiresContext to false.
|
|
970
|
+
*
|
|
971
|
+
* @example
|
|
972
|
+
* ```ts
|
|
973
|
+
* const uppercaseTool = toolPlain({
|
|
974
|
+
* name: 'uppercase',
|
|
975
|
+
* description: 'Convert to uppercase',
|
|
976
|
+
* parameters: z.object({ input: z.string() }),
|
|
977
|
+
* execute: async ({ input }) => input.toUpperCase(),
|
|
978
|
+
* });
|
|
979
|
+
* ```
|
|
980
|
+
*/
|
|
981
|
+
declare function toolPlain<TParams extends ZodRawShape>(config: Omit<ToolConfig<TParams, never>, 'execute'> & {
|
|
982
|
+
execute: (params: z.infer<ZodObject<TParams>>) => unknown | Promise<unknown>;
|
|
983
|
+
}): ToolDefinition<z.infer<ZodObject<TParams>>, never>;
|
|
984
|
+
/**
|
|
985
|
+
* Handle returned by `startToolCall()` for manual lifecycle tracking (Tier 3).
|
|
986
|
+
*/
|
|
987
|
+
interface ToolCallHandle {
|
|
988
|
+
/** The span ID for this tool call */
|
|
989
|
+
readonly spanId: string;
|
|
990
|
+
/** End the tool call successfully with the given output */
|
|
991
|
+
end(output: unknown): Promise<void>;
|
|
992
|
+
/** End the tool call with an error */
|
|
993
|
+
error(err: Error | string): Promise<void>;
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* Tier 2 (Retroactive): Track a tool call after the fact.
|
|
997
|
+
*
|
|
998
|
+
* Records a completed tool invocation as a span. Use this when you already
|
|
999
|
+
* have the input and output and just want to log it retroactively.
|
|
1000
|
+
*
|
|
1001
|
+
* @param name - Tool name
|
|
1002
|
+
* @param input - The input that was passed to the tool
|
|
1003
|
+
* @param output - The output that the tool returned
|
|
1004
|
+
* @param metadata - Optional additional metadata
|
|
1005
|
+
*
|
|
1006
|
+
* @example
|
|
1007
|
+
* ```ts
|
|
1008
|
+
* const result = await myExternalTool(params);
|
|
1009
|
+
* await trackToolCall('myExternalTool', params, result);
|
|
1010
|
+
* ```
|
|
1011
|
+
*/
|
|
1012
|
+
declare function trackToolCall(name: string, input: unknown, output: unknown, metadata?: Record<string, any>): Promise<void>;
|
|
1013
|
+
/**
|
|
1014
|
+
* Tier 3 (Manual Lifecycle): Start a tool call and get a handle to end it later.
|
|
1015
|
+
*
|
|
1016
|
+
* Use this when you need precise control over the tool call timing,
|
|
1017
|
+
* for example when the tool call is long-running or streaming.
|
|
1018
|
+
*
|
|
1019
|
+
* @param name - Tool name
|
|
1020
|
+
* @param input - The input being passed to the tool
|
|
1021
|
+
* @param metadata - Optional additional metadata
|
|
1022
|
+
* @returns A handle with `.end(output)` and `.error(err)` methods
|
|
1023
|
+
*
|
|
1024
|
+
* @example
|
|
1025
|
+
* ```ts
|
|
1026
|
+
* const handle = startToolCall('longRunningTool', { query: 'test' });
|
|
1027
|
+
* try {
|
|
1028
|
+
* const result = await longRunningTool({ query: 'test' });
|
|
1029
|
+
* await handle.end(result);
|
|
1030
|
+
* } catch (err) {
|
|
1031
|
+
* await handle.error(err);
|
|
1032
|
+
* }
|
|
1033
|
+
* ```
|
|
1034
|
+
*/
|
|
1035
|
+
declare function startToolCall(name: string, input: unknown, metadata?: Record<string, any>): ToolCallHandle;
|
|
1036
|
+
|
|
1037
|
+
/**
|
|
1038
|
+
* Wrap an Anthropic client instance for automatic tracing
|
|
1039
|
+
*
|
|
1040
|
+
* @example
|
|
1041
|
+
* ```typescript
|
|
1042
|
+
* import Anthropic from '@anthropic-ai/sdk';
|
|
1043
|
+
* import { wrapAnthropic } from '@kyte/sdk';
|
|
1044
|
+
*
|
|
1045
|
+
* const anthropic = wrapAnthropic(new Anthropic());
|
|
1046
|
+
*
|
|
1047
|
+
* // All calls are now automatically traced
|
|
1048
|
+
* const response = await anthropic.messages.create({
|
|
1049
|
+
* model: 'claude-3-sonnet-20240229',
|
|
1050
|
+
* messages: [{ role: 'user', content: 'Hello!' }],
|
|
1051
|
+
* max_tokens: 1024
|
|
1052
|
+
* });
|
|
1053
|
+
* ```
|
|
1054
|
+
*/
|
|
1055
|
+
declare function wrapAnthropic<T extends object>(client: T, options?: {
|
|
1056
|
+
traceName?: string;
|
|
1057
|
+
userId?: string;
|
|
1058
|
+
sessionId?: string;
|
|
1059
|
+
tags?: string[];
|
|
1060
|
+
metadata?: Record<string, any>;
|
|
1061
|
+
}): T;
|
|
1062
|
+
|
|
1063
|
+
/**
|
|
1064
|
+
* Wrap a specific fetch call with tracing
|
|
1065
|
+
*
|
|
1066
|
+
* @example
|
|
1067
|
+
* ```typescript
|
|
1068
|
+
* import { tracedFetch } from '@kyte/sdk';
|
|
1069
|
+
*
|
|
1070
|
+
* const response = await tracedFetch('https://api.example.com/data', {
|
|
1071
|
+
* method: 'POST',
|
|
1072
|
+
* body: JSON.stringify({ query: 'test' })
|
|
1073
|
+
* }, {
|
|
1074
|
+
* spanName: 'api.getData',
|
|
1075
|
+
* tags: ['api'],
|
|
1076
|
+
* });
|
|
1077
|
+
* ```
|
|
1078
|
+
*/
|
|
1079
|
+
declare function tracedFetch(input: RequestInfo | URL, init?: RequestInit, options?: {
|
|
1080
|
+
spanName?: string;
|
|
1081
|
+
tags?: string[];
|
|
1082
|
+
metadata?: Record<string, any>;
|
|
1083
|
+
}): Promise<Response>;
|
|
1084
|
+
|
|
1085
|
+
/**
|
|
1086
|
+
* Auto-Instrumentation System
|
|
1087
|
+
*
|
|
1088
|
+
* Zero-code tracing using ES Proxy pattern.
|
|
1089
|
+
* Automatically instruments LLM SDKs without modifying original code.
|
|
1090
|
+
*/
|
|
1091
|
+
|
|
1092
|
+
/**
|
|
1093
|
+
* Enable auto-instrumentation for specified SDKs
|
|
1094
|
+
*
|
|
1095
|
+
* @example
|
|
1096
|
+
* ```typescript
|
|
1097
|
+
* import { autoInstrument } from '@kyte/sdk';
|
|
1098
|
+
*
|
|
1099
|
+
* autoInstrument({
|
|
1100
|
+
* openai: true,
|
|
1101
|
+
* anthropic: true,
|
|
1102
|
+
* langchain: true
|
|
1103
|
+
* });
|
|
1104
|
+
*
|
|
1105
|
+
* // All subsequent SDK usage is automatically traced
|
|
1106
|
+
* import OpenAI from 'openai';
|
|
1107
|
+
* const openai = new OpenAI();
|
|
1108
|
+
* // Calls are now automatically traced!
|
|
1109
|
+
* ```
|
|
1110
|
+
*/
|
|
1111
|
+
declare function autoInstrument(config?: AutoInstrumentConfig): Promise<void>;
|
|
1112
|
+
/**
|
|
1113
|
+
* Disable all auto-instrumentation and restore original modules
|
|
1114
|
+
*/
|
|
1115
|
+
declare function disableAutoInstrument(): void;
|
|
1116
|
+
/**
|
|
1117
|
+
* Create a proxy wrapper that traces all method calls
|
|
1118
|
+
* This is the core pattern used by Langfuse
|
|
1119
|
+
*/
|
|
1120
|
+
declare function createTracingProxy<T extends object>(target: T, options?: {
|
|
1121
|
+
name?: string;
|
|
1122
|
+
spanType?: string;
|
|
1123
|
+
captureArgs?: boolean;
|
|
1124
|
+
captureResult?: boolean;
|
|
1125
|
+
}): T;
|
|
1126
|
+
|
|
1127
|
+
/**
|
|
1128
|
+
* LangChain Callback Handler for automatic tracing
|
|
1129
|
+
*
|
|
1130
|
+
* @example
|
|
1131
|
+
* ```typescript
|
|
1132
|
+
* import { AigieCallbackHandler } from '@kyte/sdk';
|
|
1133
|
+
* import { ChatOpenAI } from '@langchain/openai';
|
|
1134
|
+
*
|
|
1135
|
+
* const handler = new AigieCallbackHandler({
|
|
1136
|
+
* userId: 'user123',
|
|
1137
|
+
* sessionId: 'session456'
|
|
1138
|
+
* });
|
|
1139
|
+
*
|
|
1140
|
+
* const model = new ChatOpenAI();
|
|
1141
|
+
* const result = await model.invoke('Hello!', { callbacks: [handler] });
|
|
1142
|
+
* ```
|
|
1143
|
+
*/
|
|
1144
|
+
declare class AigieCallbackHandler {
|
|
1145
|
+
private userId?;
|
|
1146
|
+
private sessionId?;
|
|
1147
|
+
private tags;
|
|
1148
|
+
private metadata;
|
|
1149
|
+
private spanStack;
|
|
1150
|
+
name: string;
|
|
1151
|
+
constructor(options?: {
|
|
1152
|
+
userId?: string;
|
|
1153
|
+
sessionId?: string;
|
|
1154
|
+
tags?: string[];
|
|
1155
|
+
metadata?: Record<string, any>;
|
|
1156
|
+
});
|
|
1157
|
+
handleLLMStart(llm: {
|
|
1158
|
+
name: string;
|
|
1159
|
+
}, prompts: string[], runId: string, parentRunId?: string, extraParams?: Record<string, any>): Promise<void>;
|
|
1160
|
+
handleLLMEnd(output: any, runId: string): Promise<void>;
|
|
1161
|
+
handleLLMError(error: Error, runId: string): Promise<void>;
|
|
1162
|
+
handleChatModelStart(llm: {
|
|
1163
|
+
name: string;
|
|
1164
|
+
}, messages: any[][], runId: string, parentRunId?: string, extraParams?: Record<string, any>): Promise<void>;
|
|
1165
|
+
handleChainStart(chain: {
|
|
1166
|
+
name: string;
|
|
1167
|
+
}, inputs: Record<string, any>, runId: string, parentRunId?: string): Promise<void>;
|
|
1168
|
+
handleChainEnd(outputs: Record<string, any>, runId: string): Promise<void>;
|
|
1169
|
+
handleChainError(error: Error, runId: string): Promise<void>;
|
|
1170
|
+
handleToolStart(tool: {
|
|
1171
|
+
name: string;
|
|
1172
|
+
}, input: string, runId: string, parentRunId?: string): Promise<void>;
|
|
1173
|
+
handleToolEnd(output: string, runId: string): Promise<void>;
|
|
1174
|
+
handleToolError(error: Error, runId: string): Promise<void>;
|
|
1175
|
+
handleAgentAction(action: any, runId: string): Promise<void>;
|
|
1176
|
+
handleAgentEnd(_action: any, _runId: string): Promise<void>;
|
|
1177
|
+
handleRetrieverStart(retriever: {
|
|
1178
|
+
name: string;
|
|
1179
|
+
}, query: string, runId: string, parentRunId?: string): Promise<void>;
|
|
1180
|
+
handleRetrieverEnd(documents: any[], runId: string): Promise<void>;
|
|
1181
|
+
handleRetrieverError(error: Error, runId: string): Promise<void>;
|
|
1182
|
+
}
|
|
1183
|
+
/**
|
|
1184
|
+
* Create a new AigieCallbackHandler
|
|
1185
|
+
*/
|
|
1186
|
+
declare function createAigieHandler(options?: {
|
|
1187
|
+
userId?: string;
|
|
1188
|
+
sessionId?: string;
|
|
1189
|
+
tags?: string[];
|
|
1190
|
+
metadata?: Record<string, any>;
|
|
1191
|
+
}): AigieCallbackHandler;
|
|
1192
|
+
|
|
1193
|
+
/**
|
|
1194
|
+
* Wrap an OpenAI client instance for automatic tracing
|
|
1195
|
+
*
|
|
1196
|
+
* @example
|
|
1197
|
+
* ```typescript
|
|
1198
|
+
* import OpenAI from 'openai';
|
|
1199
|
+
* import { wrapOpenAI } from '@kyte/sdk';
|
|
1200
|
+
*
|
|
1201
|
+
* const openai = wrapOpenAI(new OpenAI());
|
|
1202
|
+
*
|
|
1203
|
+
* // All calls are now automatically traced
|
|
1204
|
+
* const response = await openai.chat.completions.create({
|
|
1205
|
+
* model: 'gpt-4',
|
|
1206
|
+
* messages: [{ role: 'user', content: 'Hello!' }]
|
|
1207
|
+
* });
|
|
1208
|
+
* ```
|
|
1209
|
+
*/
|
|
1210
|
+
declare function wrapOpenAI<T extends object>(client: T, options?: {
|
|
1211
|
+
traceName?: string;
|
|
1212
|
+
userId?: string;
|
|
1213
|
+
sessionId?: string;
|
|
1214
|
+
tags?: string[];
|
|
1215
|
+
metadata?: Record<string, any>;
|
|
1216
|
+
}): T;
|
|
1217
|
+
|
|
1218
|
+
/**
|
|
1219
|
+
* Cost Tracking
|
|
1220
|
+
*
|
|
1221
|
+
* Token counting and cost calculation for 50+ LLM models.
|
|
1222
|
+
*/
|
|
1223
|
+
|
|
1224
|
+
declare const MODEL_PRICING: ModelPricing[];
|
|
1225
|
+
/**
|
|
1226
|
+
* Get pricing for a model
|
|
1227
|
+
*/
|
|
1228
|
+
declare function getModelPricing(model: string, provider?: string): ModelPricing | null;
|
|
1229
|
+
/**
|
|
1230
|
+
* Calculate cost for a model call
|
|
1231
|
+
*
|
|
1232
|
+
* @param model - Model name
|
|
1233
|
+
* @param provider - Provider name (openai, anthropic, google, cohere, bedrock)
|
|
1234
|
+
* @param inputTokens - Number of input/prompt tokens
|
|
1235
|
+
* @param outputTokens - Number of output/completion tokens
|
|
1236
|
+
* @param cachedTokens - Number of cached tokens (optional)
|
|
1237
|
+
* @returns Cost in USD or null if pricing not found
|
|
1238
|
+
*/
|
|
1239
|
+
declare function calculateCost(model: string, provider: string, inputTokens: number, outputTokens: number, cachedTokens?: number): number | null;
|
|
1240
|
+
/**
|
|
1241
|
+
* Get all supported models for a provider
|
|
1242
|
+
*/
|
|
1243
|
+
declare function getModelsForProvider(provider: string): ModelPricing[];
|
|
1244
|
+
/**
|
|
1245
|
+
* Get all supported providers
|
|
1246
|
+
*/
|
|
1247
|
+
declare function getSupportedProviders(): string[];
|
|
1248
|
+
/**
|
|
1249
|
+
* Check if a model is supported
|
|
1250
|
+
*/
|
|
1251
|
+
declare function isModelSupported(model: string, provider?: string): boolean;
|
|
1252
|
+
/**
|
|
1253
|
+
* Format cost as currency string
|
|
1254
|
+
*/
|
|
1255
|
+
declare function formatCost(cost: number, currency?: string): string;
|
|
1256
|
+
|
|
1257
|
+
/**
|
|
1258
|
+
* Smart Cache
|
|
1259
|
+
*
|
|
1260
|
+
* Implements stale-while-revalidate pattern with background refresh.
|
|
1261
|
+
* Inspired by Langfuse's sophisticated caching strategy.
|
|
1262
|
+
*/
|
|
1263
|
+
|
|
1264
|
+
/**
|
|
1265
|
+
* Smart Cache with stale-while-revalidate and background refresh
|
|
1266
|
+
*
|
|
1267
|
+
* @example
|
|
1268
|
+
* ```typescript
|
|
1269
|
+
* const cache = new SmartCache<Prompt>({
|
|
1270
|
+
* ttl: 60000, // 1 minute
|
|
1271
|
+
* staleWhileRevalidate: true,
|
|
1272
|
+
* backgroundRefresh: true,
|
|
1273
|
+
* onRefresh: async (key) => fetchPromptFromServer(key)
|
|
1274
|
+
* });
|
|
1275
|
+
*
|
|
1276
|
+
* // First call fetches from server
|
|
1277
|
+
* const prompt1 = await cache.get('my-prompt');
|
|
1278
|
+
*
|
|
1279
|
+
* // Subsequent calls return cached value
|
|
1280
|
+
* const prompt2 = await cache.get('my-prompt'); // Instant!
|
|
1281
|
+
*
|
|
1282
|
+
* // After TTL expires, returns stale data while refreshing in background
|
|
1283
|
+
* const prompt3 = await cache.get('my-prompt'); // Returns stale, refreshes async
|
|
1284
|
+
* ```
|
|
1285
|
+
*/
|
|
1286
|
+
declare class SmartCache<T> {
|
|
1287
|
+
private cache;
|
|
1288
|
+
private refreshing;
|
|
1289
|
+
private config;
|
|
1290
|
+
constructor(config: SmartCacheConfig<T>);
|
|
1291
|
+
/**
|
|
1292
|
+
* Get a value from cache
|
|
1293
|
+
* If expired and staleWhileRevalidate is true, returns stale value while refreshing
|
|
1294
|
+
*/
|
|
1295
|
+
get(key: string, fallback?: () => Promise<T>): Promise<T | undefined>;
|
|
1296
|
+
/**
|
|
1297
|
+
* Set a value in cache
|
|
1298
|
+
*/
|
|
1299
|
+
set(key: string, value: T, ttl?: number): void;
|
|
1300
|
+
/**
|
|
1301
|
+
* Delete a value from cache
|
|
1302
|
+
*/
|
|
1303
|
+
delete(key: string): boolean;
|
|
1304
|
+
/**
|
|
1305
|
+
* Clear entire cache
|
|
1306
|
+
*/
|
|
1307
|
+
clear(): void;
|
|
1308
|
+
/**
|
|
1309
|
+
* Check if key exists and is fresh
|
|
1310
|
+
*/
|
|
1311
|
+
has(key: string): boolean;
|
|
1312
|
+
/**
|
|
1313
|
+
* Check if key exists (even if stale)
|
|
1314
|
+
*/
|
|
1315
|
+
hasStale(key: string): boolean;
|
|
1316
|
+
/**
|
|
1317
|
+
* Get cache size
|
|
1318
|
+
*/
|
|
1319
|
+
get size(): number;
|
|
1320
|
+
/**
|
|
1321
|
+
* Get cache statistics
|
|
1322
|
+
*/
|
|
1323
|
+
getStats(): {
|
|
1324
|
+
size: number;
|
|
1325
|
+
freshCount: number;
|
|
1326
|
+
staleCount: number;
|
|
1327
|
+
oldestEntry: number | null;
|
|
1328
|
+
newestEntry: number | null;
|
|
1329
|
+
};
|
|
1330
|
+
/**
|
|
1331
|
+
* Manually trigger refresh for a key
|
|
1332
|
+
*/
|
|
1333
|
+
refresh(key: string): Promise<T>;
|
|
1334
|
+
/**
|
|
1335
|
+
* Refresh in background (non-blocking)
|
|
1336
|
+
*/
|
|
1337
|
+
private refreshInBackground;
|
|
1338
|
+
/**
|
|
1339
|
+
* Evict least recently used entry
|
|
1340
|
+
*/
|
|
1341
|
+
private evictLRU;
|
|
1342
|
+
/**
|
|
1343
|
+
* Prune expired entries
|
|
1344
|
+
*/
|
|
1345
|
+
prune(): number;
|
|
1346
|
+
/**
|
|
1347
|
+
* Get all keys
|
|
1348
|
+
*/
|
|
1349
|
+
keys(): string[];
|
|
1350
|
+
/**
|
|
1351
|
+
* Get all entries (for serialization)
|
|
1352
|
+
*/
|
|
1353
|
+
entries(): Array<[string, CacheEntry<T>]>;
|
|
1354
|
+
/**
|
|
1355
|
+
* Restore cache from serialized data
|
|
1356
|
+
*/
|
|
1357
|
+
restore(entries: Array<[string, CacheEntry<T>]>): void;
|
|
1358
|
+
}
|
|
1359
|
+
/**
|
|
1360
|
+
* Create a smart cache with default settings
|
|
1361
|
+
*/
|
|
1362
|
+
declare function createSmartCache<T>(options: SmartCacheConfig<T>): SmartCache<T>;
|
|
1363
|
+
/**
|
|
1364
|
+
* Create a prompt cache with sensible defaults
|
|
1365
|
+
*/
|
|
1366
|
+
declare function createPromptCache<T>(onRefresh: (promptName: string) => Promise<T>, ttl?: number): SmartCache<T>;
|
|
1367
|
+
|
|
1368
|
+
/**
|
|
1369
|
+
* Annotations Client
|
|
1370
|
+
*
|
|
1371
|
+
* Human feedback and corrections on traces and spans.
|
|
1372
|
+
*/
|
|
1373
|
+
|
|
1374
|
+
/**
|
|
1375
|
+
* Annotations API Client
|
|
1376
|
+
*
|
|
1377
|
+
* @example
|
|
1378
|
+
* ```typescript
|
|
1379
|
+
* import { AnnotationsClient } from '@kyte/sdk';
|
|
1380
|
+
*
|
|
1381
|
+
* const annotations = new AnnotationsClient();
|
|
1382
|
+
*
|
|
1383
|
+
* // Create a correction annotation
|
|
1384
|
+
* await annotations.create({
|
|
1385
|
+
* traceId: 'trace_123',
|
|
1386
|
+
* type: 'correction',
|
|
1387
|
+
* value: 'The correct answer should be 42',
|
|
1388
|
+
* annotatorId: 'user@example.com'
|
|
1389
|
+
* });
|
|
1390
|
+
*
|
|
1391
|
+
* // List all annotations for a trace
|
|
1392
|
+
* const items = await annotations.list({ traceId: 'trace_123' });
|
|
1393
|
+
* ```
|
|
1394
|
+
*/
|
|
1395
|
+
declare class AnnotationsClient {
|
|
1396
|
+
private baseUrl;
|
|
1397
|
+
private apiKey;
|
|
1398
|
+
constructor();
|
|
1399
|
+
/**
|
|
1400
|
+
* Create a new annotation
|
|
1401
|
+
*/
|
|
1402
|
+
create(request: CreateAnnotationRequest): Promise<Annotation>;
|
|
1403
|
+
/**
|
|
1404
|
+
* Get annotation by ID
|
|
1405
|
+
*/
|
|
1406
|
+
get(annotationId: string): Promise<Annotation>;
|
|
1407
|
+
/**
|
|
1408
|
+
* List annotations
|
|
1409
|
+
*/
|
|
1410
|
+
list(filters?: {
|
|
1411
|
+
traceId?: string;
|
|
1412
|
+
spanId?: string;
|
|
1413
|
+
type?: AnnotationType;
|
|
1414
|
+
annotatorId?: string;
|
|
1415
|
+
limit?: number;
|
|
1416
|
+
offset?: number;
|
|
1417
|
+
}): Promise<{
|
|
1418
|
+
annotations: Annotation[];
|
|
1419
|
+
total: number;
|
|
1420
|
+
}>;
|
|
1421
|
+
/**
|
|
1422
|
+
* Update an annotation
|
|
1423
|
+
*/
|
|
1424
|
+
update(annotationId: string, updates: Partial<Pick<Annotation, 'value' | 'comment' | 'metadata'>>): Promise<Annotation>;
|
|
1425
|
+
/**
|
|
1426
|
+
* Delete an annotation
|
|
1427
|
+
*/
|
|
1428
|
+
delete(annotationId: string): Promise<void>;
|
|
1429
|
+
/**
|
|
1430
|
+
* Add a correction to a trace
|
|
1431
|
+
*/
|
|
1432
|
+
addCorrection(traceId: string, correction: string, options?: {
|
|
1433
|
+
spanId?: string;
|
|
1434
|
+
annotatorId?: string;
|
|
1435
|
+
metadata?: Record<string, any>;
|
|
1436
|
+
}): Promise<Annotation>;
|
|
1437
|
+
/**
|
|
1438
|
+
* Approve a trace/span
|
|
1439
|
+
*/
|
|
1440
|
+
approve(traceId: string, options?: {
|
|
1441
|
+
spanId?: string;
|
|
1442
|
+
comment?: string;
|
|
1443
|
+
annotatorId?: string;
|
|
1444
|
+
}): Promise<Annotation>;
|
|
1445
|
+
/**
|
|
1446
|
+
* Reject a trace/span
|
|
1447
|
+
*/
|
|
1448
|
+
reject(traceId: string, reason: string, options?: {
|
|
1449
|
+
spanId?: string;
|
|
1450
|
+
annotatorId?: string;
|
|
1451
|
+
}): Promise<Annotation>;
|
|
1452
|
+
/**
|
|
1453
|
+
* Add a comment to a trace
|
|
1454
|
+
*/
|
|
1455
|
+
addComment(traceId: string, comment: string, options?: {
|
|
1456
|
+
spanId?: string;
|
|
1457
|
+
annotatorId?: string;
|
|
1458
|
+
metadata?: Record<string, any>;
|
|
1459
|
+
}): Promise<Annotation>;
|
|
1460
|
+
/**
|
|
1461
|
+
* Add a label to a trace
|
|
1462
|
+
*/
|
|
1463
|
+
addLabel(traceId: string, label: string, options?: {
|
|
1464
|
+
spanId?: string;
|
|
1465
|
+
annotatorId?: string;
|
|
1466
|
+
metadata?: Record<string, any>;
|
|
1467
|
+
}): Promise<Annotation>;
|
|
1468
|
+
/**
|
|
1469
|
+
* Add a numeric score annotation
|
|
1470
|
+
*/
|
|
1471
|
+
addScore(traceId: string, score: number, options?: {
|
|
1472
|
+
spanId?: string;
|
|
1473
|
+
comment?: string;
|
|
1474
|
+
annotatorId?: string;
|
|
1475
|
+
metadata?: Record<string, any>;
|
|
1476
|
+
}): Promise<Annotation>;
|
|
1477
|
+
/**
|
|
1478
|
+
* Add thumbs up/down annotation
|
|
1479
|
+
*/
|
|
1480
|
+
addThumbs(traceId: string, thumbs: 'up' | 'down', options?: {
|
|
1481
|
+
spanId?: string;
|
|
1482
|
+
comment?: string;
|
|
1483
|
+
annotatorId?: string;
|
|
1484
|
+
}): Promise<Annotation>;
|
|
1485
|
+
/**
|
|
1486
|
+
* Get annotation statistics for a trace
|
|
1487
|
+
*/
|
|
1488
|
+
getStats(traceId: string): Promise<{
|
|
1489
|
+
total: number;
|
|
1490
|
+
byType: Record<AnnotationType, number>;
|
|
1491
|
+
approvalRate: number | null;
|
|
1492
|
+
averageScore: number | null;
|
|
1493
|
+
}>;
|
|
1494
|
+
}
|
|
1495
|
+
/**
|
|
1496
|
+
* Create an annotations client
|
|
1497
|
+
*/
|
|
1498
|
+
declare function createAnnotationsClient(): AnnotationsClient;
|
|
1499
|
+
|
|
1500
|
+
/**
|
|
1501
|
+
* Feedback Client
|
|
1502
|
+
*
|
|
1503
|
+
* Structured feedback collection with rating scales, categories, and analytics.
|
|
1504
|
+
*/
|
|
1505
|
+
|
|
1506
|
+
/**
|
|
1507
|
+
* Feedback API Client
|
|
1508
|
+
*
|
|
1509
|
+
* @example
|
|
1510
|
+
* ```typescript
|
|
1511
|
+
* import { FeedbackClient } from '@kyte/sdk';
|
|
1512
|
+
*
|
|
1513
|
+
* const feedback = new FeedbackClient();
|
|
1514
|
+
*
|
|
1515
|
+
* // Simple thumbs up/down
|
|
1516
|
+
* await feedback.thumbsUp('trace_123', { userId: 'user_456' });
|
|
1517
|
+
*
|
|
1518
|
+
* // Detailed rating
|
|
1519
|
+
* await feedback.rating('trace_123', 4, {
|
|
1520
|
+
* scale: [1, 5],
|
|
1521
|
+
* comment: 'Good response but could be more detailed',
|
|
1522
|
+
* userId: 'user_456'
|
|
1523
|
+
* });
|
|
1524
|
+
*
|
|
1525
|
+
* // Get feedback stats
|
|
1526
|
+
* const stats = await feedback.getStats({ traceIds: ['trace_123'] });
|
|
1527
|
+
* ```
|
|
1528
|
+
*/
|
|
1529
|
+
declare class FeedbackClient {
|
|
1530
|
+
private baseUrl;
|
|
1531
|
+
private apiKey;
|
|
1532
|
+
constructor();
|
|
1533
|
+
/**
|
|
1534
|
+
* Create feedback
|
|
1535
|
+
*/
|
|
1536
|
+
create(request: CreateFeedbackRequest): Promise<Feedback>;
|
|
1537
|
+
/**
|
|
1538
|
+
* Get feedback by ID
|
|
1539
|
+
*/
|
|
1540
|
+
get(feedbackId: string): Promise<Feedback>;
|
|
1541
|
+
/**
|
|
1542
|
+
* List feedback
|
|
1543
|
+
*/
|
|
1544
|
+
list(filters?: {
|
|
1545
|
+
traceId?: string;
|
|
1546
|
+
spanId?: string;
|
|
1547
|
+
type?: FeedbackType;
|
|
1548
|
+
userId?: string;
|
|
1549
|
+
limit?: number;
|
|
1550
|
+
offset?: number;
|
|
1551
|
+
}): Promise<{
|
|
1552
|
+
feedback: Feedback[];
|
|
1553
|
+
total: number;
|
|
1554
|
+
}>;
|
|
1555
|
+
/**
|
|
1556
|
+
* Delete feedback
|
|
1557
|
+
*/
|
|
1558
|
+
delete(feedbackId: string): Promise<void>;
|
|
1559
|
+
/**
|
|
1560
|
+
* Add thumbs up feedback
|
|
1561
|
+
*/
|
|
1562
|
+
thumbsUp(traceId: string, options?: {
|
|
1563
|
+
spanId?: string;
|
|
1564
|
+
comment?: string;
|
|
1565
|
+
userId?: string;
|
|
1566
|
+
metadata?: Record<string, any>;
|
|
1567
|
+
}): Promise<Feedback>;
|
|
1568
|
+
/**
|
|
1569
|
+
* Add thumbs down feedback
|
|
1570
|
+
*/
|
|
1571
|
+
thumbsDown(traceId: string, options?: {
|
|
1572
|
+
spanId?: string;
|
|
1573
|
+
comment?: string;
|
|
1574
|
+
userId?: string;
|
|
1575
|
+
metadata?: Record<string, any>;
|
|
1576
|
+
}): Promise<Feedback>;
|
|
1577
|
+
/**
|
|
1578
|
+
* Add rating feedback
|
|
1579
|
+
*/
|
|
1580
|
+
rating(traceId: string, value: number, options?: {
|
|
1581
|
+
spanId?: string;
|
|
1582
|
+
scale?: [number, number];
|
|
1583
|
+
comment?: string;
|
|
1584
|
+
userId?: string;
|
|
1585
|
+
metadata?: Record<string, any>;
|
|
1586
|
+
}): Promise<Feedback>;
|
|
1587
|
+
/**
|
|
1588
|
+
* Add categorical feedback
|
|
1589
|
+
*/
|
|
1590
|
+
categorical(traceId: string, category: string, options?: {
|
|
1591
|
+
spanId?: string;
|
|
1592
|
+
categories?: string[];
|
|
1593
|
+
comment?: string;
|
|
1594
|
+
userId?: string;
|
|
1595
|
+
metadata?: Record<string, any>;
|
|
1596
|
+
}): Promise<Feedback>;
|
|
1597
|
+
/**
|
|
1598
|
+
* Add text feedback
|
|
1599
|
+
*/
|
|
1600
|
+
text(traceId: string, text: string, options?: {
|
|
1601
|
+
spanId?: string;
|
|
1602
|
+
userId?: string;
|
|
1603
|
+
metadata?: Record<string, any>;
|
|
1604
|
+
}): Promise<Feedback>;
|
|
1605
|
+
/**
|
|
1606
|
+
* Get feedback statistics
|
|
1607
|
+
*/
|
|
1608
|
+
getStats(filters: {
|
|
1609
|
+
traceIds?: string[];
|
|
1610
|
+
spanIds?: string[];
|
|
1611
|
+
userId?: string;
|
|
1612
|
+
startDate?: Date;
|
|
1613
|
+
endDate?: Date;
|
|
1614
|
+
groupBy?: 'day' | 'week' | 'month';
|
|
1615
|
+
}): Promise<FeedbackStats>;
|
|
1616
|
+
/**
|
|
1617
|
+
* Calculate local statistics from feedback list
|
|
1618
|
+
*/
|
|
1619
|
+
calculateStats(feedbackList: Feedback[]): FeedbackStats;
|
|
1620
|
+
}
|
|
1621
|
+
/**
|
|
1622
|
+
* Create a feedback client
|
|
1623
|
+
*/
|
|
1624
|
+
declare function createFeedbackClient(): FeedbackClient;
|
|
1625
|
+
/**
|
|
1626
|
+
* Feedback Collector - Higher-level abstraction for collecting feedback
|
|
1627
|
+
*
|
|
1628
|
+
* @example
|
|
1629
|
+
* ```typescript
|
|
1630
|
+
* const collector = new FeedbackCollector({
|
|
1631
|
+
* traceId: 'trace_123',
|
|
1632
|
+
* userId: 'user_456'
|
|
1633
|
+
* });
|
|
1634
|
+
*
|
|
1635
|
+
* // Collect feedback at end of conversation
|
|
1636
|
+
* await collector.collectRating(4, 'Good response!');
|
|
1637
|
+
* ```
|
|
1638
|
+
*/
|
|
1639
|
+
declare class FeedbackCollector {
|
|
1640
|
+
private client;
|
|
1641
|
+
private traceId;
|
|
1642
|
+
private spanId?;
|
|
1643
|
+
private userId?;
|
|
1644
|
+
private sessionId?;
|
|
1645
|
+
constructor(options: {
|
|
1646
|
+
traceId: string;
|
|
1647
|
+
spanId?: string;
|
|
1648
|
+
userId?: string;
|
|
1649
|
+
sessionId?: string;
|
|
1650
|
+
});
|
|
1651
|
+
collectThumbsUp(comment?: string): Promise<Feedback>;
|
|
1652
|
+
collectThumbsDown(comment?: string): Promise<Feedback>;
|
|
1653
|
+
collectRating(value: number, comment?: string): Promise<Feedback>;
|
|
1654
|
+
collectCategory(category: string, comment?: string): Promise<Feedback>;
|
|
1655
|
+
collectText(text: string): Promise<Feedback>;
|
|
1656
|
+
}
|
|
1657
|
+
/**
|
|
1658
|
+
* Create a feedback collector
|
|
1659
|
+
*/
|
|
1660
|
+
declare function createFeedbackCollector(options: {
|
|
1661
|
+
traceId: string;
|
|
1662
|
+
spanId?: string;
|
|
1663
|
+
userId?: string;
|
|
1664
|
+
sessionId?: string;
|
|
1665
|
+
}): FeedbackCollector;
|
|
1666
|
+
|
|
1667
|
+
/**
|
|
1668
|
+
* Media Handling
|
|
1669
|
+
*
|
|
1670
|
+
* Base64 extraction, SHA256 deduplication, and media upload.
|
|
1671
|
+
* Inspired by Langfuse's media handling capabilities.
|
|
1672
|
+
*/
|
|
1673
|
+
|
|
1674
|
+
/**
|
|
1675
|
+
* Media Manager for handling multimodal content
|
|
1676
|
+
*
|
|
1677
|
+
* @example
|
|
1678
|
+
* ```typescript
|
|
1679
|
+
* import { MediaManager } from '@kyte/sdk';
|
|
1680
|
+
*
|
|
1681
|
+
* const media = new MediaManager();
|
|
1682
|
+
*
|
|
1683
|
+
* // Process messages with images
|
|
1684
|
+
* const processed = await media.processMessages(messages);
|
|
1685
|
+
*
|
|
1686
|
+
* // Upload media with deduplication
|
|
1687
|
+
* const ref = await media.upload(base64Data, 'image/png');
|
|
1688
|
+
* ```
|
|
1689
|
+
*/
|
|
1690
|
+
declare class MediaManager {
|
|
1691
|
+
private baseUrl;
|
|
1692
|
+
private apiKey;
|
|
1693
|
+
private uploadedHashes;
|
|
1694
|
+
constructor();
|
|
1695
|
+
/**
|
|
1696
|
+
* Process messages and extract media
|
|
1697
|
+
*/
|
|
1698
|
+
processMessages(messages: any[]): Promise<ProcessedMedia>;
|
|
1699
|
+
/**
|
|
1700
|
+
* Process a single message
|
|
1701
|
+
*/
|
|
1702
|
+
private processMessage;
|
|
1703
|
+
/**
|
|
1704
|
+
* Process a data URL (data:image/png;base64,...)
|
|
1705
|
+
*/
|
|
1706
|
+
private processBase64Url;
|
|
1707
|
+
/**
|
|
1708
|
+
* Process Anthropic-style base64 source
|
|
1709
|
+
*/
|
|
1710
|
+
private processBase64Source;
|
|
1711
|
+
/**
|
|
1712
|
+
* Upload media with deduplication
|
|
1713
|
+
*/
|
|
1714
|
+
upload(base64Data: string, mimeType: string): Promise<MediaReference>;
|
|
1715
|
+
/**
|
|
1716
|
+
* Calculate SHA256 hash of base64 data
|
|
1717
|
+
*/
|
|
1718
|
+
private calculateHash;
|
|
1719
|
+
/**
|
|
1720
|
+
* Get media by ID
|
|
1721
|
+
*/
|
|
1722
|
+
get(mediaId: string): Promise<MediaReference | null>;
|
|
1723
|
+
/**
|
|
1724
|
+
* Resolve media references in an object
|
|
1725
|
+
*/
|
|
1726
|
+
resolveReferences(obj: any, options?: {
|
|
1727
|
+
mode?: 'url' | 'base64DataUri';
|
|
1728
|
+
maxTraversalDepth?: number;
|
|
1729
|
+
}): Promise<any>;
|
|
1730
|
+
/**
|
|
1731
|
+
* Recursively traverse and resolve media references
|
|
1732
|
+
*/
|
|
1733
|
+
private traverseAndResolve;
|
|
1734
|
+
/**
|
|
1735
|
+
* Convert URL to data URI
|
|
1736
|
+
*/
|
|
1737
|
+
private urlToDataUri;
|
|
1738
|
+
/**
|
|
1739
|
+
* Clear the hash cache
|
|
1740
|
+
*/
|
|
1741
|
+
clearCache(): void;
|
|
1742
|
+
/**
|
|
1743
|
+
* Get cache statistics
|
|
1744
|
+
*/
|
|
1745
|
+
getCacheStats(): {
|
|
1746
|
+
size: number;
|
|
1747
|
+
totalBytes: number;
|
|
1748
|
+
};
|
|
1749
|
+
}
|
|
1750
|
+
/**
|
|
1751
|
+
* Create a media manager
|
|
1752
|
+
*/
|
|
1753
|
+
declare function createMediaManager(): MediaManager;
|
|
1754
|
+
/**
|
|
1755
|
+
* Extract base64 data from a data URL
|
|
1756
|
+
*/
|
|
1757
|
+
declare function extractBase64FromDataUrl(dataUrl: string): {
|
|
1758
|
+
mimeType: string;
|
|
1759
|
+
data: string;
|
|
1760
|
+
} | null;
|
|
1761
|
+
/**
|
|
1762
|
+
* Create a data URL from base64 data
|
|
1763
|
+
*/
|
|
1764
|
+
declare function createDataUrl(base64Data: string, mimeType: string): string;
|
|
1765
|
+
/**
|
|
1766
|
+
* Check if a string is a valid data URL
|
|
1767
|
+
*/
|
|
1768
|
+
declare function isDataUrl(str: string): boolean;
|
|
1769
|
+
/**
|
|
1770
|
+
* Get file extension from MIME type
|
|
1771
|
+
*/
|
|
1772
|
+
declare function mimeToExtension(mimeType: string): string;
|
|
1773
|
+
|
|
1774
|
+
/**
|
|
1775
|
+
* UUID v7 Implementation
|
|
1776
|
+
* Time-ordered UUIDs for better database indexing
|
|
1777
|
+
*/
|
|
1778
|
+
/**
|
|
1779
|
+
* Generate a UUID v7 (time-ordered)
|
|
1780
|
+
*/
|
|
1781
|
+
declare function uuidv7(): string;
|
|
1782
|
+
/**
|
|
1783
|
+
* Validate if a string is a valid UUID v7
|
|
1784
|
+
*/
|
|
1785
|
+
declare function isValidUuidv7(uuid: string): boolean;
|
|
1786
|
+
/**
|
|
1787
|
+
* Extract timestamp from UUID v7
|
|
1788
|
+
*/
|
|
1789
|
+
declare function extractTimestamp(uuid: string): number;
|
|
1790
|
+
/**
|
|
1791
|
+
* Convert UUID v7 to Date
|
|
1792
|
+
*/
|
|
1793
|
+
declare function uuidv7ToDate(uuid: string): Date;
|
|
1794
|
+
/**
|
|
1795
|
+
* Compare two UUID v7s by timestamp
|
|
1796
|
+
*/
|
|
1797
|
+
declare function compareUuidv7(a: string, b: string): number;
|
|
1798
|
+
/**
|
|
1799
|
+
* Generate batch of monotonically increasing UUID v7s
|
|
1800
|
+
*/
|
|
1801
|
+
declare function generateBatchUuidv7(count: number): string[];
|
|
1802
|
+
/**
|
|
1803
|
+
* Create UUID v7 with specific timestamp
|
|
1804
|
+
*/
|
|
1805
|
+
declare function uuidv7WithTimestamp(timestamp: number): string;
|
|
1806
|
+
/**
|
|
1807
|
+
* Get age of UUID v7 in milliseconds
|
|
1808
|
+
*/
|
|
1809
|
+
declare function getUuidv7Age(uuid: string): number;
|
|
1810
|
+
|
|
1811
|
+
declare function parseModelString(modelString: string): {
|
|
1812
|
+
provider: string;
|
|
1813
|
+
model: string;
|
|
1814
|
+
};
|
|
1815
|
+
interface ProviderAdapter {
|
|
1816
|
+
chat(params: {
|
|
1817
|
+
model: string;
|
|
1818
|
+
messages: Array<{
|
|
1819
|
+
role: string;
|
|
1820
|
+
content: string;
|
|
1821
|
+
tool_call_id?: string;
|
|
1822
|
+
tool_calls?: any[];
|
|
1823
|
+
}>;
|
|
1824
|
+
tools?: any[];
|
|
1825
|
+
settings?: ModelSettings;
|
|
1826
|
+
outputSchema?: any;
|
|
1827
|
+
}): Promise<{
|
|
1828
|
+
content: string;
|
|
1829
|
+
toolCalls?: ToolCall[];
|
|
1830
|
+
usage: {
|
|
1831
|
+
promptTokens: number;
|
|
1832
|
+
completionTokens: number;
|
|
1833
|
+
totalTokens: number;
|
|
1834
|
+
};
|
|
1835
|
+
finishReason: 'stop' | 'tool_calls' | 'length' | 'error';
|
|
1836
|
+
}>;
|
|
1837
|
+
}
|
|
1838
|
+
declare class Runner {
|
|
1839
|
+
private adapters;
|
|
1840
|
+
registerAdapter(provider: string, adapter: ProviderAdapter): void;
|
|
1841
|
+
getAdapter(provider: string): ProviderAdapter | undefined;
|
|
1842
|
+
run<TContext, TOutput>(params: {
|
|
1843
|
+
model: string;
|
|
1844
|
+
systemPrompt?: string;
|
|
1845
|
+
prompt: string;
|
|
1846
|
+
tools?: ToolDefinition[];
|
|
1847
|
+
outputType?: any;
|
|
1848
|
+
modelSettings?: ModelSettings;
|
|
1849
|
+
deps?: TContext;
|
|
1850
|
+
messageHistory?: Message[];
|
|
1851
|
+
inputGuardrails?: InputGuardrail[];
|
|
1852
|
+
outputGuardrails?: OutputGuardrail[];
|
|
1853
|
+
maxRetries?: number;
|
|
1854
|
+
maxTurns?: number;
|
|
1855
|
+
onToolCall?: (name: string, args: Record<string, unknown>) => void;
|
|
1856
|
+
onToolResult?: (name: string, result: unknown) => void;
|
|
1857
|
+
}): Promise<AgentResult<TOutput>>;
|
|
1858
|
+
}
|
|
1859
|
+
|
|
1860
|
+
declare class Agent<TContext = unknown, TOutput = string> {
|
|
1861
|
+
readonly name: string;
|
|
1862
|
+
readonly model: string;
|
|
1863
|
+
private _systemPrompt?;
|
|
1864
|
+
private _tools;
|
|
1865
|
+
private _outputType?;
|
|
1866
|
+
private _modelSettings?;
|
|
1867
|
+
private _inputGuardrails;
|
|
1868
|
+
private _outputGuardrails;
|
|
1869
|
+
private _maxRetries;
|
|
1870
|
+
private _maxTurns;
|
|
1871
|
+
private _instructionsFn?;
|
|
1872
|
+
private _resultValidatorFn?;
|
|
1873
|
+
private _runner;
|
|
1874
|
+
constructor(config: AgentConfig<TContext, TOutput>);
|
|
1875
|
+
get tools(): ToolDefinition[];
|
|
1876
|
+
get hasInstructions(): boolean;
|
|
1877
|
+
get hasResultValidator(): boolean;
|
|
1878
|
+
addTool(toolDef: ToolDefinition): void;
|
|
1879
|
+
setInstructions(fn: (ctx: RunContext<TContext>) => string | Promise<string>): void;
|
|
1880
|
+
setResultValidator(fn: (ctx: RunContext<TContext>, result: TOutput) => TOutput | Promise<TOutput>): void;
|
|
1881
|
+
setRunner(runner: Runner): void;
|
|
1882
|
+
run(prompt: string, options?: RunOptions<TContext>): Promise<AgentResult<TOutput>>;
|
|
1883
|
+
runStream(prompt: string, options?: RunOptions<TContext>): AsyncIterable<StreamChunk>;
|
|
1884
|
+
clone(): Agent<TContext, TOutput>;
|
|
1885
|
+
asTool(config: {
|
|
1886
|
+
description: string;
|
|
1887
|
+
}): ToolDefinition;
|
|
1888
|
+
}
|
|
1889
|
+
|
|
1890
|
+
interface AgentResultOptions<T> {
|
|
1891
|
+
data: T;
|
|
1892
|
+
usage: UsageInfo;
|
|
1893
|
+
messages: Message[];
|
|
1894
|
+
traceId: string;
|
|
1895
|
+
durationMs: number;
|
|
1896
|
+
toolCalls: number;
|
|
1897
|
+
retries: number;
|
|
1898
|
+
}
|
|
1899
|
+
declare class AgentResultImpl<T = string> implements AgentResult<T> {
|
|
1900
|
+
data: T;
|
|
1901
|
+
usage: UsageInfo;
|
|
1902
|
+
messages: Message[];
|
|
1903
|
+
traceId: string;
|
|
1904
|
+
durationMs: number;
|
|
1905
|
+
toolCalls: number;
|
|
1906
|
+
retries: number;
|
|
1907
|
+
constructor(options: AgentResultOptions<T>);
|
|
1908
|
+
allMessages(): Message[];
|
|
1909
|
+
withToolReturnContent(): Message[];
|
|
1910
|
+
}
|
|
1911
|
+
|
|
1912
|
+
interface RunContextOptions<T> {
|
|
1913
|
+
deps: T;
|
|
1914
|
+
model: string;
|
|
1915
|
+
traceId: string;
|
|
1916
|
+
spanId: string;
|
|
1917
|
+
messages?: Message[];
|
|
1918
|
+
retryCount?: number;
|
|
1919
|
+
maxRetries?: number;
|
|
1920
|
+
}
|
|
1921
|
+
declare class RunContextImpl<T = unknown> implements RunContext<T> {
|
|
1922
|
+
deps: T;
|
|
1923
|
+
model: string;
|
|
1924
|
+
messages: Message[];
|
|
1925
|
+
traceId: string;
|
|
1926
|
+
spanId: string;
|
|
1927
|
+
retryCount: number;
|
|
1928
|
+
maxRetries: number;
|
|
1929
|
+
constructor(options: RunContextOptions<T>);
|
|
1930
|
+
get isLastAttempt(): boolean;
|
|
1931
|
+
addMessage(msg: Message): void;
|
|
1932
|
+
addUserMessage(content: string): void;
|
|
1933
|
+
addAssistantMessage(content: string): void;
|
|
1934
|
+
getMessagesForApi(): ApiMessage[];
|
|
1935
|
+
withRetry(): RunContext<T>;
|
|
1936
|
+
}
|
|
1937
|
+
|
|
1938
|
+
declare class ExecutionCycle {
|
|
1939
|
+
currentPhase: string;
|
|
1940
|
+
private thinkDurationMs;
|
|
1941
|
+
private actDurationMs;
|
|
1942
|
+
private observeDurationMs;
|
|
1943
|
+
private cycleCount;
|
|
1944
|
+
think<T>(fn: (phase: PhaseContext) => Promise<T>): Promise<T>;
|
|
1945
|
+
act<T>(fn: (phase: PhaseContext) => Promise<T>): Promise<T>;
|
|
1946
|
+
observe<T>(fn: (phase: PhaseContext) => Promise<T>): Promise<T>;
|
|
1947
|
+
getMetrics(): CycleMetrics;
|
|
1948
|
+
}
|
|
1949
|
+
|
|
1950
|
+
declare class GoalTracker {
|
|
1951
|
+
goal: string;
|
|
1952
|
+
private expectedSteps;
|
|
1953
|
+
private completedSteps;
|
|
1954
|
+
private deviations;
|
|
1955
|
+
private startTime;
|
|
1956
|
+
private nextExpectedIndex;
|
|
1957
|
+
setPlan(goal: string, expectedSteps: string[]): void;
|
|
1958
|
+
markStepComplete(stepName: string): void;
|
|
1959
|
+
getMetrics(): PlanMetrics;
|
|
1960
|
+
}
|
|
1961
|
+
|
|
1962
|
+
declare class LoopDetector {
|
|
1963
|
+
private similarityThreshold;
|
|
1964
|
+
private maxConsecutiveRepeats;
|
|
1965
|
+
private action;
|
|
1966
|
+
constructor(config?: LoopDetectorConfig);
|
|
1967
|
+
checkState(_messages: Message[], toolCalls: ToolCall[]): LoopDetectionResult;
|
|
1968
|
+
/**
|
|
1969
|
+
* Build non-overlapping sequences of the given window size from the tool calls.
|
|
1970
|
+
*/
|
|
1971
|
+
private buildSequences;
|
|
1972
|
+
/**
|
|
1973
|
+
* Convert a set of tool calls into a feature set of tool names and argument keys
|
|
1974
|
+
* for Jaccard similarity comparison.
|
|
1975
|
+
*/
|
|
1976
|
+
private toolCallsToFeatureSet;
|
|
1977
|
+
/**
|
|
1978
|
+
* Compute Jaccard similarity between two feature sets.
|
|
1979
|
+
* J(A, B) = |A intersection B| / |A union B|
|
|
1980
|
+
*/
|
|
1981
|
+
private computeJaccardSimilarity;
|
|
1982
|
+
}
|
|
1983
|
+
|
|
1984
|
+
/**
|
|
1985
|
+
* Context passed to the drift detector for comparison.
|
|
1986
|
+
*/
|
|
1987
|
+
interface DriftCheckInput {
|
|
1988
|
+
/** The expected conversation context (e.g., system prompt, goal description, or reference messages). */
|
|
1989
|
+
expectedContext: string | Message[];
|
|
1990
|
+
/** The actual conversation context (e.g., recent messages or current output). */
|
|
1991
|
+
actualContext: string | Message[];
|
|
1992
|
+
}
|
|
1993
|
+
/**
|
|
1994
|
+
* Lightweight drift detection engine.
|
|
1995
|
+
*
|
|
1996
|
+
* Supports four drift detection strategies: topic, behavior, quality, and coherence.
|
|
1997
|
+
* Each detector produces a score between 0 and 1, where higher values indicate
|
|
1998
|
+
* greater drift from the expected context.
|
|
1999
|
+
*/
|
|
2000
|
+
declare class DriftDetector {
|
|
2001
|
+
private readonly detectors;
|
|
2002
|
+
private readonly thresholds;
|
|
2003
|
+
constructor(config: DriftDetectorConfig);
|
|
2004
|
+
/**
|
|
2005
|
+
* Checks for drift between expected and actual context.
|
|
2006
|
+
*
|
|
2007
|
+
* Runs all configured detectors and returns the result of the first one
|
|
2008
|
+
* whose score exceeds its threshold. If no detector triggers, returns
|
|
2009
|
+
* a no-drift result with the highest observed score.
|
|
2010
|
+
*/
|
|
2011
|
+
checkDrift(input: DriftCheckInput): Promise<DriftResult>;
|
|
2012
|
+
private runDetector;
|
|
2013
|
+
private detectTopicDrift;
|
|
2014
|
+
private detectBehaviorDrift;
|
|
2015
|
+
private detectQualityDrift;
|
|
2016
|
+
private detectCoherenceDrift;
|
|
2017
|
+
}
|
|
2018
|
+
|
|
2019
|
+
declare function inputGuardrail(config: {
|
|
2020
|
+
name: string;
|
|
2021
|
+
execute: (input: string, context: GuardrailContext) => Promise<GuardrailResult>;
|
|
2022
|
+
}): InputGuardrail;
|
|
2023
|
+
declare function outputGuardrail(config: {
|
|
2024
|
+
name: string;
|
|
2025
|
+
execute: (output: unknown, context: GuardrailContext) => Promise<GuardrailResult>;
|
|
2026
|
+
}): OutputGuardrail;
|
|
2027
|
+
|
|
2028
|
+
declare class EvaluationClient {
|
|
2029
|
+
private apiUrl;
|
|
2030
|
+
private apiKey;
|
|
2031
|
+
constructor(config: {
|
|
2032
|
+
apiUrl: string;
|
|
2033
|
+
apiKey: string;
|
|
2034
|
+
});
|
|
2035
|
+
addScore(score: EvaluationScore): Promise<void>;
|
|
2036
|
+
addScores(scores: EvaluationScore[]): Promise<void>;
|
|
2037
|
+
}
|
|
2038
|
+
|
|
2039
|
+
declare class LLMJudge {
|
|
2040
|
+
private config;
|
|
2041
|
+
private runner;
|
|
2042
|
+
constructor(config: JudgeConfig);
|
|
2043
|
+
setAdapter(provider: string, adapter: ProviderAdapter): void;
|
|
2044
|
+
evaluate(params: {
|
|
2045
|
+
input: string;
|
|
2046
|
+
output: string;
|
|
2047
|
+
context?: string;
|
|
2048
|
+
reference?: string;
|
|
2049
|
+
}): Promise<JudgeResult>;
|
|
2050
|
+
}
|
|
2051
|
+
|
|
2052
|
+
declare class SignalReporter {
|
|
2053
|
+
private apiUrl;
|
|
2054
|
+
private apiKey;
|
|
2055
|
+
private queue;
|
|
2056
|
+
private flushTimer;
|
|
2057
|
+
private batchSize;
|
|
2058
|
+
private flushInterval;
|
|
2059
|
+
constructor(config: {
|
|
2060
|
+
apiUrl: string;
|
|
2061
|
+
apiKey: string;
|
|
2062
|
+
batchSize?: number;
|
|
2063
|
+
flushInterval?: number;
|
|
2064
|
+
});
|
|
2065
|
+
emit(request: EmitSignalRequest): Promise<Signal>;
|
|
2066
|
+
flush(): Promise<void>;
|
|
2067
|
+
shutdown(): Promise<void>;
|
|
2068
|
+
}
|
|
2069
|
+
|
|
2070
|
+
/**
|
|
2071
|
+
* Reliability Metrics - Aggregate plan, cycle, loop, and drift metrics
|
|
2072
|
+
* into a single reliability score.
|
|
2073
|
+
*
|
|
2074
|
+
* @packageDocumentation
|
|
2075
|
+
*/
|
|
2076
|
+
|
|
2077
|
+
interface ReliabilityMetrics {
|
|
2078
|
+
planMetrics?: PlanMetrics;
|
|
2079
|
+
cycleMetrics?: CycleMetrics;
|
|
2080
|
+
loopMetrics?: LoopDetectionResult;
|
|
2081
|
+
driftMetrics?: DriftResult;
|
|
2082
|
+
overallScore: number;
|
|
2083
|
+
}
|
|
2084
|
+
declare class MetricsAggregator {
|
|
2085
|
+
private _planMetrics?;
|
|
2086
|
+
private _cycleMetrics?;
|
|
2087
|
+
private _loopMetrics?;
|
|
2088
|
+
private _driftMetrics?;
|
|
2089
|
+
setPlanMetrics(metrics: PlanMetrics): void;
|
|
2090
|
+
setCycleMetrics(metrics: CycleMetrics): void;
|
|
2091
|
+
setLoopMetrics(metrics: LoopDetectionResult): void;
|
|
2092
|
+
setDriftMetrics(metrics: DriftResult): void;
|
|
2093
|
+
getMetrics(): ReliabilityMetrics;
|
|
2094
|
+
reset(): void;
|
|
2095
|
+
}
|
|
2096
|
+
|
|
2097
|
+
/**
|
|
2098
|
+
* Prompt Management - Versioned prompt template registration and rendering.
|
|
2099
|
+
*
|
|
2100
|
+
* @packageDocumentation
|
|
2101
|
+
*/
|
|
2102
|
+
interface PromptTemplate {
|
|
2103
|
+
name: string;
|
|
2104
|
+
template: string;
|
|
2105
|
+
variables: string[];
|
|
2106
|
+
version: number;
|
|
2107
|
+
createdAt: string;
|
|
2108
|
+
}
|
|
2109
|
+
declare class PromptManager {
|
|
2110
|
+
private templates;
|
|
2111
|
+
register(name: string, template: string, variables: string[]): PromptTemplate;
|
|
2112
|
+
render(name: string, values: Record<string, string>, version?: number): string;
|
|
2113
|
+
getVersion(name: string, version?: number): PromptTemplate;
|
|
2114
|
+
list(): string[];
|
|
2115
|
+
}
|
|
2116
|
+
|
|
2117
|
+
/**
|
|
2118
|
+
* Remediation Engine - Real-time signal handling and automated remediation.
|
|
2119
|
+
*
|
|
2120
|
+
* Connects to the backend via WebSocket, receives signals, and dispatches
|
|
2121
|
+
* them to registered remediation handlers.
|
|
2122
|
+
*
|
|
2123
|
+
* @packageDocumentation
|
|
2124
|
+
*/
|
|
2125
|
+
|
|
2126
|
+
interface RemediationEngineConfig {
|
|
2127
|
+
url: string;
|
|
2128
|
+
token: string;
|
|
2129
|
+
}
|
|
2130
|
+
interface RetryConfig {
|
|
2131
|
+
maxRetries: number;
|
|
2132
|
+
backoffMs: number;
|
|
2133
|
+
}
|
|
2134
|
+
interface FallbackConfig {
|
|
2135
|
+
fallbackModel: string;
|
|
2136
|
+
}
|
|
2137
|
+
declare class RemediationEngine {
|
|
2138
|
+
private readonly config;
|
|
2139
|
+
private ws;
|
|
2140
|
+
private handlers;
|
|
2141
|
+
private actionListeners;
|
|
2142
|
+
constructor(config: RemediationEngineConfig);
|
|
2143
|
+
connect(): Promise<void>;
|
|
2144
|
+
disconnect(): void;
|
|
2145
|
+
onSignal(type: SignalType, handler: RemediationHandler): void;
|
|
2146
|
+
/**
|
|
2147
|
+
* Register a listener that is called whenever a remediation action is
|
|
2148
|
+
* emitted after a signal is processed.
|
|
2149
|
+
*/
|
|
2150
|
+
onAction(listener: (action: RemediationAction, signal: Signal) => void): void;
|
|
2151
|
+
static retry(config: RetryConfig): RemediationHandler;
|
|
2152
|
+
static fallback(config: FallbackConfig): RemediationHandler;
|
|
2153
|
+
static contextReset(): RemediationHandler;
|
|
2154
|
+
static goalReinjection(goal: string): RemediationHandler;
|
|
2155
|
+
static checkpointRestore(checkpointId: string): RemediationHandler;
|
|
2156
|
+
private handleMessage;
|
|
2157
|
+
private dispatch;
|
|
2158
|
+
private emitAction;
|
|
2159
|
+
}
|
|
2160
|
+
|
|
2161
|
+
/**
|
|
2162
|
+
* Local Rules Engine - Evaluate guardrail rules against a runtime context.
|
|
2163
|
+
*
|
|
2164
|
+
* @packageDocumentation
|
|
2165
|
+
*/
|
|
2166
|
+
|
|
2167
|
+
interface Rule {
|
|
2168
|
+
name: string;
|
|
2169
|
+
condition: (ctx: RuleContext) => boolean;
|
|
2170
|
+
action: GuardrailAction;
|
|
2171
|
+
message: string;
|
|
2172
|
+
}
|
|
2173
|
+
interface RuleContext {
|
|
2174
|
+
input?: string;
|
|
2175
|
+
output?: string;
|
|
2176
|
+
toolCalls?: Array<{
|
|
2177
|
+
name: string;
|
|
2178
|
+
arguments: Record<string, unknown>;
|
|
2179
|
+
}>;
|
|
2180
|
+
durationMs?: number;
|
|
2181
|
+
tokenCount?: number;
|
|
2182
|
+
metadata?: Record<string, unknown>;
|
|
2183
|
+
}
|
|
2184
|
+
interface RuleResult {
|
|
2185
|
+
ruleName: string;
|
|
2186
|
+
triggered: boolean;
|
|
2187
|
+
action: GuardrailAction;
|
|
2188
|
+
message: string;
|
|
2189
|
+
}
|
|
2190
|
+
declare class RulesEngine {
|
|
2191
|
+
private rules;
|
|
2192
|
+
addRule(rule: Rule): void;
|
|
2193
|
+
removeRule(name: string): void;
|
|
2194
|
+
evaluate(context: RuleContext): RuleResult[];
|
|
2195
|
+
evaluateAll(context: RuleContext): RuleResult[];
|
|
2196
|
+
}
|
|
2197
|
+
|
|
2198
|
+
interface DiagnosticSignal {
|
|
2199
|
+
description: string;
|
|
2200
|
+
sentiment: 'POSITIVE' | 'NEGATIVE' | 'NEUTRAL';
|
|
2201
|
+
}
|
|
2202
|
+
interface DiagnosticsConfig {
|
|
2203
|
+
signals: Record<string, DiagnosticSignal>;
|
|
2204
|
+
}
|
|
2205
|
+
declare function createSelfDiagnosticsTool(config: DiagnosticsConfig): {
|
|
2206
|
+
forOpenAI(): {
|
|
2207
|
+
type: "function";
|
|
2208
|
+
function: {
|
|
2209
|
+
name: string;
|
|
2210
|
+
description: string;
|
|
2211
|
+
parameters: Record<string, any>;
|
|
2212
|
+
};
|
|
2213
|
+
};
|
|
2214
|
+
forAnthropic(): {
|
|
2215
|
+
name: string;
|
|
2216
|
+
description: string;
|
|
2217
|
+
input_schema: Record<string, any>;
|
|
2218
|
+
};
|
|
2219
|
+
forVercelAI(): {
|
|
2220
|
+
description: string;
|
|
2221
|
+
parameters: any;
|
|
2222
|
+
execute: (params: any) => Promise<any>;
|
|
2223
|
+
};
|
|
2224
|
+
execute: (params: {
|
|
2225
|
+
signal_name: string;
|
|
2226
|
+
details?: string;
|
|
2227
|
+
}) => Promise<{
|
|
2228
|
+
error: string;
|
|
2229
|
+
signal_name?: undefined;
|
|
2230
|
+
description?: undefined;
|
|
2231
|
+
sentiment?: undefined;
|
|
2232
|
+
details?: undefined;
|
|
2233
|
+
timestamp?: undefined;
|
|
2234
|
+
} | {
|
|
2235
|
+
signal_name: string;
|
|
2236
|
+
description: string;
|
|
2237
|
+
sentiment: "POSITIVE" | "NEGATIVE" | "NEUTRAL";
|
|
2238
|
+
details: string | undefined;
|
|
2239
|
+
timestamp: string;
|
|
2240
|
+
error?: undefined;
|
|
2241
|
+
}>;
|
|
2242
|
+
signals: Record<string, DiagnosticSignal>;
|
|
2243
|
+
};
|
|
2244
|
+
|
|
2245
|
+
declare class IntegrationRegistryImpl {
|
|
2246
|
+
private integrations;
|
|
2247
|
+
private patchedSet;
|
|
2248
|
+
register(info: IntegrationInfo): void;
|
|
2249
|
+
get(name: string): IntegrationInfo | undefined;
|
|
2250
|
+
getAll(): IntegrationInfo[];
|
|
2251
|
+
markPatched(name: string): void;
|
|
2252
|
+
isPatched(name: string): boolean;
|
|
2253
|
+
patch(...names: string[]): Promise<void>;
|
|
2254
|
+
patchAll(): Promise<void>;
|
|
2255
|
+
}
|
|
2256
|
+
declare const IntegrationRegistry: IntegrationRegistryImpl;
|
|
2257
|
+
|
|
2258
|
+
/**
|
|
2259
|
+
* Kyte SDK for TypeScript/JavaScript
|
|
2260
|
+
*
|
|
2261
|
+
* Automatic tracing and monitoring for AI applications.
|
|
2262
|
+
*
|
|
2263
|
+
* @packageDocumentation
|
|
2264
|
+
*/
|
|
2265
|
+
|
|
2266
|
+
declare const VERSION = "1.1.0";
|
|
2267
|
+
|
|
2268
|
+
export { type ActiveSpan, Agent, type AgentConfig, AgentExecutionError, type AgentResult, AgentResultImpl, AigieCallbackHandler, AigieClient, type AigieConfig, AigieError, type Annotation, type AnnotationType, AnnotationsClient, type AnthropicToolSchema, type ApiMessage, type AutoInstrumentConfig, BehaviorDriftDetected, type CacheEntry, ConfigurationError, ContextDriftDetected, type CostDetails, type CreateAnnotationRequest, type CreateFeedbackRequest, type CustomInstrumentation, type CycleMetrics, type Deviation, DriftDetector, type DriftDetectorConfig, DriftError, type DriftResult, type DriftType, type EmitSignalRequest, EvaluationClient, type EvaluationScore, ExecutionCycle, type Feedback, FeedbackClient, FeedbackCollector, type FeedbackStats, type FeedbackType, GoalTracker, GuardrailAction, type GuardrailContext, GuardrailError, type GuardrailResult, type InputGuardrail, type IntegrationInfo, IntegrationRegistry, InterceptionError, type JudgeConfig, type JudgeResult, AigieCallbackHandler as KyteCallbackHandler, AigieClient as KyteClient, type AigieConfig as KyteConfig, LLMJudge, LoopDetectedError, type LoopDetectionResult, LoopDetector, type LoopDetectorConfig, MODEL_PRICING, MediaManager, type MediaReference, type Message, MetricsAggregator, type ModelPricing, ModelRetry, type ModelSettings, type OpenAIToolSchema, type OutputGuardrail, type PhaseContext, type PlanMetrics, type ProcessedMedia, PromptManager, type PromptTemplate, type ProviderAdapter, ProviderError, type ReliabilityMetrics, type RemediationAction, RemediationEngine, RemediationError, RemediationFailed, type RemediationHandler, RemediationRejected, RetryExhausted, type Rule, type RuleContext, type RuleResult, RulesEngine, type RunContext, RunContextImpl, type RunOptions, Runner, type Score, type ScoreType, type Signal, SignalReporter, SignalSeverity, SignalType, SmartCache, type SmartCacheConfig, type Span, type SpanBuilder, type SpanContext, type SpanOptions, type SpanType, type SpanUpdateData, type StreamChunk, TimeoutError, type TokenUsage, type ToolCall, type ToolCallHandle, type ToolCallResult, type ToolConfig, type ToolDefinition, ToolExecutionError, TopicDriftDetected, type Trace, type TraceContext, type TraceOptions, type TraceStatus, type TraceableConfig, TracingError, type UsageDetails, type UsageInfo, VERSION, ValidationError, type WrapperOptions, autoInstrument, calculateCost, compareUuidv7, createAigieHandler, createAnnotationsClient, createDataUrl, createFeedbackClient, createFeedbackCollector, createAigieHandler as createKyteHandler, createMediaManager, createPromptCache, createSelfDiagnosticsTool, createSmartCache, createTracingProxy, disableAutoInstrument, extractBase64FromDataUrl, extractTimestamp, formatCost, generateBatchUuidv7, getAigie, getKyte, getModelPricing, getModelsForProvider, getSupportedProviders, getUuidv7Age, initAigie, initKyte, inputGuardrail, isDataUrl, isModelSupported, isValidUuidv7, mimeToExtension, observe, outputGuardrail, parseModelString, startToolCall, tool, toolPlain, trace, traceable, tracedFetch, trackToolCall, uuidv7, uuidv7ToDate, uuidv7WithTimestamp, withTracing, wrapAnthropic, wrapOpenAI };
|