risicare 0.1.3 → 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,
@@ -81,6 +83,11 @@ function resolveConfig(config) {
81
83
  const apiKey = config?.apiKey ?? env.RISICARE_API_KEY;
82
84
  const endpoint = config?.endpoint ?? env.RISICARE_ENDPOINT ?? DEFAULT_ENDPOINT;
83
85
  const projectId = config?.projectId ?? env.RISICARE_PROJECT_ID;
86
+ if (config?.projectId || env.RISICARE_PROJECT_ID) {
87
+ console.warn(
88
+ "[risicare] projectId is deprecated and ignored by the gateway. Your API key determines the project. Use serviceName and environment for within-project organization. projectId will be removed in v1.0."
89
+ );
90
+ }
84
91
  const environment = config?.environment ?? env.RISICARE_ENVIRONMENT ?? "development";
85
92
  const serviceName = config?.serviceName ?? env.RISICARE_SERVICE_NAME;
86
93
  const serviceVersion = config?.serviceVersion ?? env.RISICARE_SERVICE_VERSION;
@@ -1114,10 +1121,66 @@ function getMetrics() {
1114
1121
  queueUtilization: 0
1115
1122
  };
1116
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
+ }
1117
1181
 
1118
1182
  // src/context/agent.ts
1119
1183
  function withAgent(options, fn) {
1120
- if (!isEnabled()) return fn();
1121
1184
  const parent = getCurrentAgent();
1122
1185
  const agentName = options.name ?? "agent";
1123
1186
  const agent2 = {
@@ -1150,7 +1213,6 @@ function agent(options, fn) {
1150
1213
 
1151
1214
  // src/context/session.ts
1152
1215
  function withSession(options, fn) {
1153
- if (!isEnabled()) return fn();
1154
1216
  const session2 = {
1155
1217
  sessionId: options.sessionId,
1156
1218
  userId: options.userId,
@@ -1171,7 +1233,6 @@ function session(optionsOrResolver, fn) {
1171
1233
 
1172
1234
  // src/context/phase.ts
1173
1235
  function withPhase(phase, fn) {
1174
- if (!isEnabled()) return fn();
1175
1236
  return runWithContext({ phase }, fn);
1176
1237
  }
1177
1238
 
@@ -1408,6 +1469,8 @@ function maybeCleanup() {
1408
1469
  injectTraceContext,
1409
1470
  isEnabled,
1410
1471
  registerSpan,
1472
+ reportError,
1473
+ score,
1411
1474
  session,
1412
1475
  shutdown,
1413
1476
  traceAct,