risicare 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +99 -0
- package/dist/index.cjs +1268 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +564 -0
- package/dist/index.d.ts +564 -0
- package/dist/index.js +1193 -0
- package/dist/index.js.map +1 -0
- package/dist/providers/anthropic/index.cjs +237 -0
- package/dist/providers/anthropic/index.cjs.map +1 -0
- package/dist/providers/anthropic/index.d.cts +23 -0
- package/dist/providers/anthropic/index.d.ts +23 -0
- package/dist/providers/anthropic/index.js +210 -0
- package/dist/providers/anthropic/index.js.map +1 -0
- package/dist/providers/openai/index.cjs +296 -0
- package/dist/providers/openai/index.cjs.map +1 -0
- package/dist/providers/openai/index.d.cts +27 -0
- package/dist/providers/openai/index.d.ts +27 -0
- package/dist/providers/openai/index.js +269 -0
- package/dist/providers/openai/index.js.map +1 -0
- package/dist/providers/vercel-ai/index.cjs +245 -0
- package/dist/providers/vercel-ai/index.cjs.map +1 -0
- package/dist/providers/vercel-ai/index.d.cts +33 -0
- package/dist/providers/vercel-ai/index.d.ts +33 -0
- package/dist/providers/vercel-ai/index.js +218 -0
- package/dist/providers/vercel-ai/index.js.map +1 -0
- package/package.json +101 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SDK configuration with environment variable loading.
|
|
3
|
+
*
|
|
4
|
+
* Environment variables match the Python SDK exactly:
|
|
5
|
+
* RISICARE_API_KEY, RISICARE_ENDPOINT, RISICARE_PROJECT_ID, etc.
|
|
6
|
+
*/
|
|
7
|
+
interface RisicareConfig {
|
|
8
|
+
/** API key for authentication (format: "rsk-{random}") */
|
|
9
|
+
apiKey?: string;
|
|
10
|
+
/** Gateway endpoint URL */
|
|
11
|
+
endpoint?: string;
|
|
12
|
+
/** Project ID */
|
|
13
|
+
projectId?: string;
|
|
14
|
+
/** Environment name (production, staging, development) */
|
|
15
|
+
environment?: string;
|
|
16
|
+
/** Service name for identification */
|
|
17
|
+
serviceName?: string;
|
|
18
|
+
/** Service version */
|
|
19
|
+
serviceVersion?: string;
|
|
20
|
+
/** Whether tracing is enabled (default: true if apiKey provided) */
|
|
21
|
+
enabled?: boolean;
|
|
22
|
+
/** Whether to capture prompt/completion content (default: true) */
|
|
23
|
+
traceContent?: boolean;
|
|
24
|
+
/** Sample rate 0.0–1.0 (default: 1.0 = trace all) */
|
|
25
|
+
sampleRate?: number;
|
|
26
|
+
/** Batch size threshold before flush (default: 100) */
|
|
27
|
+
batchSize?: number;
|
|
28
|
+
/** Batch timeout in ms before flush (default: 1000) */
|
|
29
|
+
batchTimeoutMs?: number;
|
|
30
|
+
/** Maximum queue size (default: 10000) */
|
|
31
|
+
maxQueueSize?: number;
|
|
32
|
+
/** Enable debug logging (default: false) */
|
|
33
|
+
debug?: boolean;
|
|
34
|
+
/** Custom metadata attached to all spans */
|
|
35
|
+
metadata?: Record<string, unknown>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Core type definitions for the Risicare SDK.
|
|
40
|
+
*
|
|
41
|
+
* These enums and types mirror the Python SDK's risicare-core types
|
|
42
|
+
* and align with the Rust gateway's SpanData struct.
|
|
43
|
+
*/
|
|
44
|
+
declare enum SpanKind {
|
|
45
|
+
INTERNAL = "internal",
|
|
46
|
+
CLIENT = "client",
|
|
47
|
+
SERVER = "server",
|
|
48
|
+
PRODUCER = "producer",
|
|
49
|
+
CONSUMER = "consumer",
|
|
50
|
+
AGENT = "agent",
|
|
51
|
+
LLM_CALL = "llm_call",
|
|
52
|
+
TOOL_CALL = "tool_call",
|
|
53
|
+
RETRIEVAL = "retrieval",
|
|
54
|
+
DECISION = "decision",
|
|
55
|
+
MESSAGE = "message",
|
|
56
|
+
DELEGATION = "delegation",
|
|
57
|
+
COORDINATION = "coordination",
|
|
58
|
+
THINK = "think",
|
|
59
|
+
DECIDE = "decide",
|
|
60
|
+
OBSERVE = "observe",
|
|
61
|
+
REFLECT = "reflect"
|
|
62
|
+
}
|
|
63
|
+
declare enum SpanStatus {
|
|
64
|
+
UNSET = "unset",
|
|
65
|
+
OK = "ok",
|
|
66
|
+
ERROR = "error"
|
|
67
|
+
}
|
|
68
|
+
declare enum SemanticPhase {
|
|
69
|
+
THINK = "think",
|
|
70
|
+
DECIDE = "decide",
|
|
71
|
+
ACT = "act",
|
|
72
|
+
OBSERVE = "observe"
|
|
73
|
+
}
|
|
74
|
+
declare enum AgentRole {
|
|
75
|
+
ORCHESTRATOR = "orchestrator",
|
|
76
|
+
WORKER = "worker",
|
|
77
|
+
REVIEWER = "reviewer",
|
|
78
|
+
PLANNER = "planner",
|
|
79
|
+
EXECUTOR = "executor",
|
|
80
|
+
CRITIC = "critic",
|
|
81
|
+
CUSTOM = "custom"
|
|
82
|
+
}
|
|
83
|
+
declare enum MessageType {
|
|
84
|
+
REQUEST = "request",
|
|
85
|
+
RESPONSE = "response",
|
|
86
|
+
DELEGATE = "delegate",
|
|
87
|
+
COORDINATE = "coordinate",
|
|
88
|
+
BROADCAST = "broadcast"
|
|
89
|
+
}
|
|
90
|
+
interface SpanEvent {
|
|
91
|
+
name: string;
|
|
92
|
+
timestamp: string;
|
|
93
|
+
attributes?: Record<string, unknown>;
|
|
94
|
+
}
|
|
95
|
+
interface SpanLink {
|
|
96
|
+
traceId: string;
|
|
97
|
+
spanId: string;
|
|
98
|
+
attributes?: Record<string, unknown>;
|
|
99
|
+
}
|
|
100
|
+
interface SpanPayload {
|
|
101
|
+
traceId: string;
|
|
102
|
+
spanId: string;
|
|
103
|
+
parentSpanId?: string;
|
|
104
|
+
name: string;
|
|
105
|
+
kind: string;
|
|
106
|
+
startTime: string;
|
|
107
|
+
endTime?: string;
|
|
108
|
+
durationMs?: number;
|
|
109
|
+
status: string;
|
|
110
|
+
statusMessage?: string;
|
|
111
|
+
attributes: Record<string, unknown>;
|
|
112
|
+
events: SpanEvent[];
|
|
113
|
+
links: SpanLink[];
|
|
114
|
+
sessionId?: string;
|
|
115
|
+
agentId?: string;
|
|
116
|
+
agentName?: string;
|
|
117
|
+
agentType?: string;
|
|
118
|
+
semanticPhase?: string;
|
|
119
|
+
llmProvider?: string;
|
|
120
|
+
llmModel?: string;
|
|
121
|
+
llmPromptTokens?: number;
|
|
122
|
+
llmCompletionTokens?: number;
|
|
123
|
+
llmTotalTokens?: number;
|
|
124
|
+
llmCostUsd?: number;
|
|
125
|
+
toolName?: string;
|
|
126
|
+
toolSuccess?: boolean;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Span class — the core unit of tracing.
|
|
131
|
+
*
|
|
132
|
+
* A span represents a single operation (LLM call, tool use, agent step).
|
|
133
|
+
* Spans are mutable: attributes, events, and status can be set after creation.
|
|
134
|
+
* Call end() to finalize the span and trigger export.
|
|
135
|
+
*/
|
|
136
|
+
|
|
137
|
+
interface SpanOptions {
|
|
138
|
+
name: string;
|
|
139
|
+
kind?: SpanKind;
|
|
140
|
+
traceId?: string;
|
|
141
|
+
spanId?: string;
|
|
142
|
+
parentSpanId?: string;
|
|
143
|
+
attributes?: Record<string, unknown>;
|
|
144
|
+
links?: SpanLink[];
|
|
145
|
+
sessionId?: string;
|
|
146
|
+
agentId?: string;
|
|
147
|
+
agentName?: string;
|
|
148
|
+
agentType?: string;
|
|
149
|
+
semanticPhase?: string;
|
|
150
|
+
}
|
|
151
|
+
type SpanEndCallback = (span: Span) => void;
|
|
152
|
+
declare class Span {
|
|
153
|
+
readonly traceId: string;
|
|
154
|
+
readonly spanId: string;
|
|
155
|
+
readonly parentSpanId: string | undefined;
|
|
156
|
+
readonly name: string;
|
|
157
|
+
readonly kind: SpanKind;
|
|
158
|
+
readonly startTime: string;
|
|
159
|
+
readonly startHrtime: number;
|
|
160
|
+
endTime: string | undefined;
|
|
161
|
+
status: SpanStatus;
|
|
162
|
+
statusMessage: string | undefined;
|
|
163
|
+
attributes: Record<string, unknown>;
|
|
164
|
+
events: SpanEvent[];
|
|
165
|
+
links: SpanLink[];
|
|
166
|
+
sessionId: string | undefined;
|
|
167
|
+
agentId: string | undefined;
|
|
168
|
+
agentName: string | undefined;
|
|
169
|
+
agentType: string | undefined;
|
|
170
|
+
semanticPhase: string | undefined;
|
|
171
|
+
llmProvider: string | undefined;
|
|
172
|
+
llmModel: string | undefined;
|
|
173
|
+
llmPromptTokens: number | undefined;
|
|
174
|
+
llmCompletionTokens: number | undefined;
|
|
175
|
+
llmTotalTokens: number | undefined;
|
|
176
|
+
llmCostUsd: number | undefined;
|
|
177
|
+
toolName: string | undefined;
|
|
178
|
+
toolSuccess: boolean | undefined;
|
|
179
|
+
private _ended;
|
|
180
|
+
private _endHrtime;
|
|
181
|
+
private _onEnd;
|
|
182
|
+
constructor(options: SpanOptions, onEnd?: SpanEndCallback);
|
|
183
|
+
get isEnded(): boolean;
|
|
184
|
+
get durationMs(): number;
|
|
185
|
+
setAttribute(key: string, value: unknown): this;
|
|
186
|
+
setAttributes(attrs: Record<string, unknown>): this;
|
|
187
|
+
setStatus(status: SpanStatus, message?: string): this;
|
|
188
|
+
addEvent(name: string, attributes?: Record<string, unknown>): this;
|
|
189
|
+
addLink(traceId: string, spanId: string, attributes?: Record<string, unknown>): this;
|
|
190
|
+
recordException(error: Error | string): this;
|
|
191
|
+
setLlmFields(fields: {
|
|
192
|
+
provider?: string;
|
|
193
|
+
model?: string;
|
|
194
|
+
promptTokens?: number;
|
|
195
|
+
completionTokens?: number;
|
|
196
|
+
totalTokens?: number;
|
|
197
|
+
costUsd?: number;
|
|
198
|
+
}): this;
|
|
199
|
+
setToolFields(fields: {
|
|
200
|
+
name?: string;
|
|
201
|
+
success?: boolean;
|
|
202
|
+
}): this;
|
|
203
|
+
end(): void;
|
|
204
|
+
/**
|
|
205
|
+
* Serialize to the wire format expected by the Rust gateway.
|
|
206
|
+
*/
|
|
207
|
+
toPayload(): SpanPayload;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Tracer — creates spans with automatic context nesting.
|
|
212
|
+
*
|
|
213
|
+
* The Tracer is the primary API for creating spans. It automatically:
|
|
214
|
+
* - Links child spans to parent spans via parentSpanId
|
|
215
|
+
* - Propagates the trace ID from the active context
|
|
216
|
+
* - Injects session/agent/phase context into new spans
|
|
217
|
+
* - Runs the callback within the span's context scope
|
|
218
|
+
*
|
|
219
|
+
* Usage:
|
|
220
|
+
* const tracer = new Tracer(processor);
|
|
221
|
+
* await tracer.startSpan('my-operation', async (span) => {
|
|
222
|
+
* span.setAttribute('key', 'value');
|
|
223
|
+
* // Nested spans automatically become children
|
|
224
|
+
* await tracer.startSpan('sub-op', async (child) => { ... });
|
|
225
|
+
* });
|
|
226
|
+
*/
|
|
227
|
+
|
|
228
|
+
interface StartSpanOptions {
|
|
229
|
+
name: string;
|
|
230
|
+
kind?: SpanKind;
|
|
231
|
+
attributes?: Record<string, unknown>;
|
|
232
|
+
/** Override trace ID (default: inherit from parent or generate new) */
|
|
233
|
+
traceId?: string;
|
|
234
|
+
/** Override parent span ID (default: current span in context) */
|
|
235
|
+
parentSpanId?: string;
|
|
236
|
+
}
|
|
237
|
+
interface TracerConfig {
|
|
238
|
+
/** Called when a span ends (typically feeds into batch processor) */
|
|
239
|
+
onSpanEnd: SpanEndCallback;
|
|
240
|
+
/** Sample rate 0.0–1.0 (default: 1.0) */
|
|
241
|
+
sampleRate?: number;
|
|
242
|
+
/** Whether tracing is enabled */
|
|
243
|
+
enabled?: boolean;
|
|
244
|
+
}
|
|
245
|
+
declare class Tracer {
|
|
246
|
+
private _onSpanEnd;
|
|
247
|
+
private _sampleRate;
|
|
248
|
+
private _enabled;
|
|
249
|
+
constructor(config: TracerConfig);
|
|
250
|
+
get enabled(): boolean;
|
|
251
|
+
set enabled(value: boolean);
|
|
252
|
+
/**
|
|
253
|
+
* Start a span, run the callback within its context, and auto-end on completion.
|
|
254
|
+
*
|
|
255
|
+
* The span is automatically:
|
|
256
|
+
* - Linked to the current parent span (if any)
|
|
257
|
+
* - Given the current trace ID (or a new one for root spans)
|
|
258
|
+
* - Enriched with session/agent/phase context
|
|
259
|
+
* - Ended when the callback returns (or throws)
|
|
260
|
+
* - Marked as ERROR if the callback throws
|
|
261
|
+
*
|
|
262
|
+
* @returns The callback's return value
|
|
263
|
+
*/
|
|
264
|
+
startSpan<T>(options: StartSpanOptions | string, fn: (span: Span) => T): T;
|
|
265
|
+
/**
|
|
266
|
+
* Create a span without auto-ending. Caller is responsible for calling span.end().
|
|
267
|
+
*
|
|
268
|
+
* Useful for long-running operations where you need manual control.
|
|
269
|
+
* The span is still linked to context and enriched with session/agent/phase.
|
|
270
|
+
*/
|
|
271
|
+
createSpan(options: StartSpanOptions | string): Span;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* RisicareClient — singleton client managing SDK lifecycle.
|
|
276
|
+
*
|
|
277
|
+
* Handles initialization, shutdown, and the connection between
|
|
278
|
+
* the Tracer and the export pipeline (batch processor + HTTP exporter).
|
|
279
|
+
*
|
|
280
|
+
* Usage:
|
|
281
|
+
* import { init, shutdown } from 'risicare';
|
|
282
|
+
* init({ apiKey: 'rsk-...', projectId: 'my-project' });
|
|
283
|
+
* // ... instrument code ...
|
|
284
|
+
* await shutdown(); // flush remaining spans
|
|
285
|
+
*/
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Initialize the Risicare SDK. Call once at application startup.
|
|
289
|
+
*
|
|
290
|
+
* @example
|
|
291
|
+
* import { init } from 'risicare';
|
|
292
|
+
* init({ apiKey: 'rsk-...', projectId: 'my-project' });
|
|
293
|
+
*/
|
|
294
|
+
declare function init(config?: Partial<RisicareConfig>): void;
|
|
295
|
+
/**
|
|
296
|
+
* Gracefully shut down the SDK. Flushes pending spans before resolving.
|
|
297
|
+
*/
|
|
298
|
+
declare function shutdown(): Promise<void>;
|
|
299
|
+
/**
|
|
300
|
+
* Flush all pending spans without shutting down.
|
|
301
|
+
*/
|
|
302
|
+
declare function flush(): Promise<void>;
|
|
303
|
+
/**
|
|
304
|
+
* Enable tracing at runtime.
|
|
305
|
+
*/
|
|
306
|
+
declare function enable(): void;
|
|
307
|
+
/**
|
|
308
|
+
* Disable tracing at runtime. Spans will not be created or exported.
|
|
309
|
+
*/
|
|
310
|
+
declare function disable(): void;
|
|
311
|
+
/**
|
|
312
|
+
* Check whether tracing is currently enabled.
|
|
313
|
+
*/
|
|
314
|
+
declare function isEnabled(): boolean;
|
|
315
|
+
/**
|
|
316
|
+
* Get the global tracer instance. Returns undefined if not initialized.
|
|
317
|
+
*/
|
|
318
|
+
declare function getTracer(): Tracer | undefined;
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Agent context — identifies which agent is executing.
|
|
322
|
+
* Supports nesting: inner agents automatically track parent.
|
|
323
|
+
*/
|
|
324
|
+
interface AgentOptions {
|
|
325
|
+
agentId?: string;
|
|
326
|
+
name?: string;
|
|
327
|
+
role?: string;
|
|
328
|
+
agentType?: string;
|
|
329
|
+
version?: number;
|
|
330
|
+
metadata?: Record<string, unknown>;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Run a callback within an agent context.
|
|
334
|
+
* If nested inside another agent, parentAgentId is set automatically.
|
|
335
|
+
*
|
|
336
|
+
* @example
|
|
337
|
+
* await withAgent({ name: 'researcher', role: 'worker' }, async () => {
|
|
338
|
+
* const result = await llm.invoke(query);
|
|
339
|
+
* return result;
|
|
340
|
+
* });
|
|
341
|
+
*/
|
|
342
|
+
declare function withAgent<T>(options: AgentOptions, fn: () => T): T;
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* agent() — Higher-order function wrapper for agent identity (Tier 2).
|
|
346
|
+
*
|
|
347
|
+
* Wraps a function to execute within an agent context. All spans created
|
|
348
|
+
* inside will be tagged with the agent's identity.
|
|
349
|
+
*
|
|
350
|
+
* @example
|
|
351
|
+
* const research = agent({ name: 'researcher', role: 'worker' }, async (query: string) => {
|
|
352
|
+
* const result = await openai.chat.completions.create({ ... });
|
|
353
|
+
* return result.choices[0].message.content;
|
|
354
|
+
* });
|
|
355
|
+
*
|
|
356
|
+
* const answer = await research('What is quantum computing?');
|
|
357
|
+
*/
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Wrap a function with agent context and an automatic span.
|
|
361
|
+
*/
|
|
362
|
+
declare function agent<TArgs extends unknown[], TReturn>(options: AgentOptions, fn: (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Session context — groups multiple traces from the same user interaction.
|
|
366
|
+
*/
|
|
367
|
+
interface SessionOptions {
|
|
368
|
+
sessionId: string;
|
|
369
|
+
userId?: string;
|
|
370
|
+
metadata?: Record<string, unknown>;
|
|
371
|
+
parentSessionId?: string;
|
|
372
|
+
turnNumber?: number;
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Run a callback within a session context.
|
|
376
|
+
*
|
|
377
|
+
* @example
|
|
378
|
+
* await withSession({ sessionId: 'sess-123', userId: 'user-1' }, async () => {
|
|
379
|
+
* const result = await agent.run(query);
|
|
380
|
+
* return result;
|
|
381
|
+
* });
|
|
382
|
+
*/
|
|
383
|
+
declare function withSession<T>(options: SessionOptions, fn: () => T): T;
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* session() — Higher-order function wrapper for session context (Tier 3).
|
|
387
|
+
*
|
|
388
|
+
* Wraps a function to execute within a session context. All spans created
|
|
389
|
+
* inside will be tagged with the session identity.
|
|
390
|
+
*
|
|
391
|
+
* @example
|
|
392
|
+
* const handleRequest = session(
|
|
393
|
+
* (req) => ({ sessionId: req.sessionId, userId: req.userId }),
|
|
394
|
+
* async (req: Request) => {
|
|
395
|
+
* return await agent.run(req.query);
|
|
396
|
+
* },
|
|
397
|
+
* );
|
|
398
|
+
*/
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Wrap a function with session context.
|
|
402
|
+
*
|
|
403
|
+
* @param optionsOrResolver - Static session options, or a function that derives them from args
|
|
404
|
+
* @param fn - The function to wrap
|
|
405
|
+
*/
|
|
406
|
+
declare function session<TArgs extends unknown[], TReturn>(optionsOrResolver: SessionOptions | ((...args: TArgs) => SessionOptions), fn: (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Phase decorators — decision phase tracking (Tier 4).
|
|
410
|
+
*
|
|
411
|
+
* Wraps functions to execute within a semantic phase context (THINK/DECIDE/ACT/OBSERVE).
|
|
412
|
+
* Each creates a span named after the phase.
|
|
413
|
+
*
|
|
414
|
+
* @example
|
|
415
|
+
* const analyze = traceThink(async (data: string) => {
|
|
416
|
+
* return await llm.invoke(`Analyze: ${data}`);
|
|
417
|
+
* });
|
|
418
|
+
*
|
|
419
|
+
* const decide = traceDecide(async (analysis: string) => {
|
|
420
|
+
* return await llm.invoke(`Decide action: ${analysis}`);
|
|
421
|
+
* });
|
|
422
|
+
*/
|
|
423
|
+
/** Wrap a function in a THINK phase context with auto-span. */
|
|
424
|
+
declare function traceThink<TArgs extends unknown[], TReturn>(fn: (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
|
|
425
|
+
/** Wrap a function in a DECIDE phase context with auto-span. */
|
|
426
|
+
declare function traceDecide<TArgs extends unknown[], TReturn>(fn: (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
|
|
427
|
+
/** Wrap a function in an ACT phase context with auto-span. */
|
|
428
|
+
declare function traceAct<TArgs extends unknown[], TReturn>(fn: (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
|
|
429
|
+
/** Wrap a function in an OBSERVE phase context with auto-span. */
|
|
430
|
+
declare function traceObserve<TArgs extends unknown[], TReturn>(fn: (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Phase context — cognitive phase tracking (THINK/DECIDE/ACT/OBSERVE).
|
|
434
|
+
*/
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Run a callback within a semantic phase context.
|
|
438
|
+
*/
|
|
439
|
+
declare function withPhase<T>(phase: SemanticPhase, fn: () => T): T;
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Multi-agent communication decorators (Tier 5).
|
|
443
|
+
*
|
|
444
|
+
* Track inter-agent messages, delegations, and coordination.
|
|
445
|
+
*
|
|
446
|
+
* @example
|
|
447
|
+
* const sendToWorker = traceMessage({ to: 'worker-1', type: MessageType.DELEGATE },
|
|
448
|
+
* async (task: string) => { ... }
|
|
449
|
+
* );
|
|
450
|
+
*/
|
|
451
|
+
|
|
452
|
+
interface MessageOptions {
|
|
453
|
+
to: string;
|
|
454
|
+
type?: MessageType;
|
|
455
|
+
metadata?: Record<string, unknown>;
|
|
456
|
+
}
|
|
457
|
+
interface DelegateOptions {
|
|
458
|
+
to: string;
|
|
459
|
+
metadata?: Record<string, unknown>;
|
|
460
|
+
}
|
|
461
|
+
interface CoordinateOptions {
|
|
462
|
+
participants: string[];
|
|
463
|
+
metadata?: Record<string, unknown>;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Wrap a function that sends a message to another agent.
|
|
467
|
+
*/
|
|
468
|
+
declare function traceMessage<TArgs extends unknown[], TReturn>(options: MessageOptions, fn: (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
|
|
469
|
+
/**
|
|
470
|
+
* Wrap a function that delegates work to a sub-agent.
|
|
471
|
+
*/
|
|
472
|
+
declare function traceDelegate<TArgs extends unknown[], TReturn>(options: DelegateOptions, fn: (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
|
|
473
|
+
/**
|
|
474
|
+
* Wrap a function that coordinates multiple agents.
|
|
475
|
+
*/
|
|
476
|
+
declare function traceCoordinate<TArgs extends unknown[], TReturn>(options: CoordinateOptions, fn: (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* AsyncLocalStorage-based context propagation.
|
|
480
|
+
*
|
|
481
|
+
* Uses a single AsyncLocalStorage instance with a composite state object.
|
|
482
|
+
* This is simpler and more performant than multiple separate stores.
|
|
483
|
+
*
|
|
484
|
+
* Node.js AsyncLocalStorage automatically propagates through:
|
|
485
|
+
* - Promise / async-await
|
|
486
|
+
* - setTimeout / setImmediate
|
|
487
|
+
* - EventEmitter callbacks
|
|
488
|
+
* - process.nextTick
|
|
489
|
+
* - async generators (unlike Python's contextvars!)
|
|
490
|
+
*/
|
|
491
|
+
|
|
492
|
+
interface SessionContext {
|
|
493
|
+
sessionId: string;
|
|
494
|
+
userId?: string;
|
|
495
|
+
metadata?: Record<string, unknown>;
|
|
496
|
+
parentSessionId?: string;
|
|
497
|
+
turnNumber?: number;
|
|
498
|
+
}
|
|
499
|
+
interface AgentContext {
|
|
500
|
+
agentId: string;
|
|
501
|
+
agentName?: string;
|
|
502
|
+
agentRole?: string;
|
|
503
|
+
agentType?: string;
|
|
504
|
+
parentAgentId?: string;
|
|
505
|
+
version?: number;
|
|
506
|
+
metadata?: Record<string, unknown>;
|
|
507
|
+
}
|
|
508
|
+
declare function getCurrentSession(): SessionContext | undefined;
|
|
509
|
+
declare function getCurrentAgent(): AgentContext | undefined;
|
|
510
|
+
declare function getCurrentSpan(): Span | undefined;
|
|
511
|
+
declare function getCurrentPhase(): SemanticPhase | undefined;
|
|
512
|
+
declare function getCurrentSessionId(): string | undefined;
|
|
513
|
+
declare function getCurrentAgentId(): string | undefined;
|
|
514
|
+
declare function getCurrentTraceId(): string | undefined;
|
|
515
|
+
declare function getCurrentSpanId(): string | undefined;
|
|
516
|
+
/**
|
|
517
|
+
* Get all current context as a plain object (for debugging/serialization).
|
|
518
|
+
*/
|
|
519
|
+
declare function getCurrentContext(): Record<string, unknown>;
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* Serializable trace context + W3C Trace Context headers.
|
|
523
|
+
*
|
|
524
|
+
* TraceContext is a plain object that can be passed across process
|
|
525
|
+
* boundaries (JSON serialization, HTTP headers, message queues).
|
|
526
|
+
*
|
|
527
|
+
* W3C Trace Context:
|
|
528
|
+
* traceparent: {version}-{trace_id}-{span_id}-{flags}
|
|
529
|
+
* tracestate: risicare=session_id={...};agent_id={...}
|
|
530
|
+
*/
|
|
531
|
+
interface TraceContext {
|
|
532
|
+
traceId: string;
|
|
533
|
+
spanId: string;
|
|
534
|
+
sessionId?: string;
|
|
535
|
+
agentId?: string;
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Get the current trace context as a serializable object.
|
|
539
|
+
*/
|
|
540
|
+
declare function getTraceContext(): TraceContext;
|
|
541
|
+
/**
|
|
542
|
+
* Inject trace context into HTTP headers (W3C format).
|
|
543
|
+
*/
|
|
544
|
+
declare function injectTraceContext(headers: Record<string, string>): Record<string, string>;
|
|
545
|
+
/**
|
|
546
|
+
* Extract trace context from HTTP headers (W3C format).
|
|
547
|
+
*/
|
|
548
|
+
declare function extractTraceContext(headers: Record<string, string>): Record<string, string | undefined>;
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* Span registry — TTL-based span store for edge cases.
|
|
552
|
+
*
|
|
553
|
+
* While AsyncLocalStorage handles most context propagation in Node.js,
|
|
554
|
+
* the registry is useful for:
|
|
555
|
+
* - Cross-process span tracking
|
|
556
|
+
* - Explicit span passing via ID
|
|
557
|
+
* - Long-running operations that outlive their original context
|
|
558
|
+
*/
|
|
559
|
+
|
|
560
|
+
declare function registerSpan(span: Span, ttlMs?: number): void;
|
|
561
|
+
declare function getSpanById(spanId: string): Span | undefined;
|
|
562
|
+
declare function unregisterSpan(spanId: string): void;
|
|
563
|
+
|
|
564
|
+
export { type AgentContext, type AgentOptions, AgentRole, MessageType, type RisicareConfig, SemanticPhase, type SessionContext, type SessionOptions, Span, SpanKind, type SpanOptions, SpanStatus, type StartSpanOptions, type TraceContext, Tracer, agent, disable, enable, extractTraceContext, flush, getCurrentAgent, getCurrentAgentId, getCurrentContext, getCurrentPhase, getCurrentSession, getCurrentSessionId, getCurrentSpan, getCurrentSpanId, getCurrentTraceId, getSpanById, getTraceContext, getTracer, init, injectTraceContext, isEnabled, registerSpan, session, shutdown, traceAct, traceCoordinate, traceDecide, traceDelegate, traceMessage, traceObserve, traceThink, unregisterSpan, withAgent, withPhase, withSession };
|