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.cjs CHANGED
@@ -58,6 +58,8 @@ __export(src_exports, {
58
58
  injectTraceContext: () => injectTraceContext,
59
59
  isEnabled: () => isEnabled,
60
60
  registerSpan: () => registerSpan,
61
+ reportError: () => reportError,
62
+ score: () => score,
61
63
  session: () => session,
62
64
  shutdown: () => shutdown,
63
65
  traceAct: () => traceAct,
@@ -1119,10 +1121,66 @@ function getMetrics() {
1119
1121
  queueUtilization: 0
1120
1122
  };
1121
1123
  }
1124
+ function reportError(error, options) {
1125
+ try {
1126
+ const tracer = getTracer2();
1127
+ if (!tracer) return;
1128
+ const err = error instanceof Error ? error : new Error(String(error));
1129
+ const spanName = options?.name ?? `error:${err.constructor.name}`;
1130
+ tracer.startSpan({ name: spanName, kind: "internal" /* INTERNAL */ }, (span) => {
1131
+ span.setStatus("error" /* ERROR */, err.message);
1132
+ span.setAttribute("error", true);
1133
+ span.setAttribute("error.type", err.constructor.name);
1134
+ span.setAttribute("error.message", err.message.slice(0, 2e3));
1135
+ if (err.stack) span.setAttribute("error.stack", err.stack.slice(0, 4e3));
1136
+ span.setAttribute("risicare.reported_error", true);
1137
+ if (options?.attributes) {
1138
+ for (const [k, v] of Object.entries(options.attributes)) {
1139
+ span.setAttribute(k, v);
1140
+ }
1141
+ }
1142
+ });
1143
+ } catch {
1144
+ debug("reportError failed");
1145
+ }
1146
+ }
1147
+ function score(traceId, name, value, options) {
1148
+ try {
1149
+ if (typeof value !== "number" || value < 0 || value > 1) {
1150
+ debug(`score: value must be in [0.0, 1.0], got ${value}. Score not sent.`);
1151
+ return;
1152
+ }
1153
+ if (!traceId || !name) {
1154
+ debug("score: traceId and name are required");
1155
+ return;
1156
+ }
1157
+ const client = getClient();
1158
+ if (!client?.enabled || !client.config.apiKey) return;
1159
+ const endpoint = client.config.endpoint.replace(/\/$/, "");
1160
+ const url = `${endpoint}/api/v1/scores`;
1161
+ const body = JSON.stringify({
1162
+ trace_id: traceId,
1163
+ name,
1164
+ score: value,
1165
+ source: "sdk",
1166
+ ...options?.spanId && { span_id: options.spanId },
1167
+ ...options?.comment && { comment: options.comment }
1168
+ });
1169
+ fetch(url, {
1170
+ method: "POST",
1171
+ headers: {
1172
+ "Content-Type": "application/json",
1173
+ "Authorization": `Bearer ${client.config.apiKey}`
1174
+ },
1175
+ body
1176
+ }).catch((err) => debug(`score: send failed: ${err}`));
1177
+ } catch {
1178
+ debug("score failed");
1179
+ }
1180
+ }
1122
1181
 
1123
1182
  // src/context/agent.ts
1124
1183
  function withAgent(options, fn) {
1125
- if (!isEnabled()) return fn();
1126
1184
  const parent = getCurrentAgent();
1127
1185
  const agentName = options.name ?? "agent";
1128
1186
  const agent2 = {
@@ -1155,7 +1213,6 @@ function agent(options, fn) {
1155
1213
 
1156
1214
  // src/context/session.ts
1157
1215
  function withSession(options, fn) {
1158
- if (!isEnabled()) return fn();
1159
1216
  const session2 = {
1160
1217
  sessionId: options.sessionId,
1161
1218
  userId: options.userId,
@@ -1176,7 +1233,6 @@ function session(optionsOrResolver, fn) {
1176
1233
 
1177
1234
  // src/context/phase.ts
1178
1235
  function withPhase(phase, fn) {
1179
- if (!isEnabled()) return fn();
1180
1236
  return runWithContext({ phase }, fn);
1181
1237
  }
1182
1238
 
@@ -1413,6 +1469,8 @@ function maybeCleanup() {
1413
1469
  injectTraceContext,
1414
1470
  isEnabled,
1415
1471
  registerSpan,
1472
+ reportError,
1473
+ score,
1416
1474
  session,
1417
1475
  shutdown,
1418
1476
  traceAct,