risicare 0.1.4 → 0.1.5

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.ts CHANGED
@@ -345,6 +345,34 @@ declare function getMetrics(): {
345
345
  queueCapacity: number;
346
346
  queueUtilization: number;
347
347
  };
348
+ /**
349
+ * Report a caught exception to the self-healing pipeline.
350
+ *
351
+ * Creates an error span that triggers diagnosis and fix generation.
352
+ * This function never throws and is non-blocking.
353
+ *
354
+ * @param error - The caught exception (Error object or string)
355
+ * @param options - Optional attributes and context overrides
356
+ */
357
+ declare function reportError(error: unknown, options?: {
358
+ name?: string;
359
+ attributes?: Record<string, unknown>;
360
+ }): void;
361
+ /**
362
+ * Record a custom evaluation score on a trace.
363
+ *
364
+ * Sends the score to the server in a fire-and-forget fashion.
365
+ * This function never throws and is non-blocking.
366
+ *
367
+ * @param traceId - The trace to score
368
+ * @param name - Score name (e.g., "accuracy", "user_satisfaction")
369
+ * @param value - Score value between 0.0 and 1.0 inclusive
370
+ * @param options - Optional span_id and comment
371
+ */
372
+ declare function score(traceId: string, name: string, value: number, options?: {
373
+ spanId?: string;
374
+ comment?: string;
375
+ }): void;
348
376
 
349
377
  /**
350
378
  * Agent context — identifies which agent is executing.
@@ -590,4 +618,4 @@ declare function registerSpan(span: Span, ttlMs?: number): void;
590
618
  declare function getSpanById(spanId: string): Span | undefined;
591
619
  declare function unregisterSpan(spanId: string): void;
592
620
 
593
- 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, getMetrics, getSpanById, getTraceContent, getTraceContext, getTracer, init, injectTraceContext, isEnabled, registerSpan, session, shutdown, traceAct, traceCoordinate, traceDecide, traceDelegate, traceMessage, traceObserve, traceThink, unregisterSpan, withAgent, withPhase, withSession };
621
+ 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, getMetrics, getSpanById, getTraceContent, getTraceContext, getTracer, init, injectTraceContext, isEnabled, registerSpan, reportError, score, session, shutdown, traceAct, traceCoordinate, traceDecide, traceDelegate, traceMessage, traceObserve, traceThink, unregisterSpan, withAgent, withPhase, withSession };
package/dist/index.js CHANGED
@@ -1043,10 +1043,66 @@ function getMetrics() {
1043
1043
  queueUtilization: 0
1044
1044
  };
1045
1045
  }
1046
+ function reportError(error, options) {
1047
+ try {
1048
+ const tracer = getTracer2();
1049
+ if (!tracer) return;
1050
+ const err = error instanceof Error ? error : new Error(String(error));
1051
+ const spanName = options?.name ?? `error:${err.constructor.name}`;
1052
+ tracer.startSpan({ name: spanName, kind: "internal" /* INTERNAL */ }, (span) => {
1053
+ span.setStatus("error" /* ERROR */, err.message);
1054
+ span.setAttribute("error", true);
1055
+ span.setAttribute("error.type", err.constructor.name);
1056
+ span.setAttribute("error.message", err.message.slice(0, 2e3));
1057
+ if (err.stack) span.setAttribute("error.stack", err.stack.slice(0, 4e3));
1058
+ span.setAttribute("risicare.reported_error", true);
1059
+ if (options?.attributes) {
1060
+ for (const [k, v] of Object.entries(options.attributes)) {
1061
+ span.setAttribute(k, v);
1062
+ }
1063
+ }
1064
+ });
1065
+ } catch {
1066
+ debug("reportError failed");
1067
+ }
1068
+ }
1069
+ function score(traceId, name, value, options) {
1070
+ try {
1071
+ if (typeof value !== "number" || value < 0 || value > 1) {
1072
+ debug(`score: value must be in [0.0, 1.0], got ${value}. Score not sent.`);
1073
+ return;
1074
+ }
1075
+ if (!traceId || !name) {
1076
+ debug("score: traceId and name are required");
1077
+ return;
1078
+ }
1079
+ const client = getClient();
1080
+ if (!client?.enabled || !client.config.apiKey) return;
1081
+ const endpoint = client.config.endpoint.replace(/\/$/, "");
1082
+ const url = `${endpoint}/api/v1/scores`;
1083
+ const body = JSON.stringify({
1084
+ trace_id: traceId,
1085
+ name,
1086
+ score: value,
1087
+ source: "sdk",
1088
+ ...options?.spanId && { span_id: options.spanId },
1089
+ ...options?.comment && { comment: options.comment }
1090
+ });
1091
+ fetch(url, {
1092
+ method: "POST",
1093
+ headers: {
1094
+ "Content-Type": "application/json",
1095
+ "Authorization": `Bearer ${client.config.apiKey}`
1096
+ },
1097
+ body
1098
+ }).catch((err) => debug(`score: send failed: ${err}`));
1099
+ } catch {
1100
+ debug("score failed");
1101
+ }
1102
+ }
1046
1103
 
1047
1104
  // src/context/agent.ts
1048
1105
  function withAgent(options, fn) {
1049
- if (!isEnabled()) return fn();
1050
1106
  const parent = getCurrentAgent();
1051
1107
  const agentName = options.name ?? "agent";
1052
1108
  const agent2 = {
@@ -1079,7 +1135,6 @@ function agent(options, fn) {
1079
1135
 
1080
1136
  // src/context/session.ts
1081
1137
  function withSession(options, fn) {
1082
- if (!isEnabled()) return fn();
1083
1138
  const session2 = {
1084
1139
  sessionId: options.sessionId,
1085
1140
  userId: options.userId,
@@ -1100,7 +1155,6 @@ function session(optionsOrResolver, fn) {
1100
1155
 
1101
1156
  // src/context/phase.ts
1102
1157
  function withPhase(phase, fn) {
1103
- if (!isEnabled()) return fn();
1104
1158
  return runWithContext({ phase }, fn);
1105
1159
  }
1106
1160
 
@@ -1336,6 +1390,8 @@ export {
1336
1390
  injectTraceContext,
1337
1391
  isEnabled,
1338
1392
  registerSpan,
1393
+ reportError,
1394
+ score,
1339
1395
  session,
1340
1396
  shutdown,
1341
1397
  traceAct,