UNPKG

axiom

Version:

Axiom AI SDK provides - an API to wrap your AI calls with observability instrumentation. - offline evals

165 lines (160 loc) 5.24 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/feedback.ts var feedback_exports = {}; __export(feedback_exports, { Feedback: () => Feedback, createFeedbackClient: () => createFeedbackClient }); module.exports = __toCommonJS(feedback_exports); // src/schema.ts var SCHEMA_VERSION = "0.0.2"; var SCHEMA_BASE_URL = "https://axiom.co/ai/schemas/"; var SCHEMA_URL = `${SCHEMA_BASE_URL}${SCHEMA_VERSION}`; // src/util/errors.ts function getCircularReplacer() { const seen = /* @__PURE__ */ new WeakSet(); return (_k, v) => { if (typeof v === "object" && v !== null) { if (seen.has(v)) return "[Circular]"; seen.add(v); } return v; }; } function safeJson(x) { try { return JSON.stringify(x, getCircularReplacer()); } catch { return String(x); } } function errorToString(err) { try { if (typeof err === "string") return err; if (err instanceof Error) { return err.stack ?? err.message; } if (typeof err === "object" && err !== null) { const msg = err.message; const json = safeJson(err); return msg ? `${msg} (${json})` : json; } return String(err); } catch { return "[unserializable error]"; } } // src/util/feedback.ts function getSuffix(baseUrl, dataset) { if (baseUrl.includes("edge.axiom")) { return `/v1/ingest/${dataset}`; } return `/v1/datasets/${dataset}/ingest`; } // src/feedback.ts var withKind = (input, kind) => ({ ...input, kind }); var createFeedbackClient = (config, settings) => { const baseUrl = config.url ?? "https://api.axiom.co"; const url = `${baseUrl}${getSuffix(baseUrl, config.dataset)}`; const sendFeedback = async (links, feedback) => { const { metadata, ...feedbackFields } = feedback; const { traceId, spanId, conversationId, ...restLinks } = links; const serializedLinks = { trace_id: traceId, ...restLinks, ...spanId !== void 0 && { span_id: spanId }, ...conversationId !== void 0 && { conversation_id: conversationId } }; const payload = { schemaUrl: SCHEMA_URL, id: crypto.randomUUID(), ...feedbackFields, links: serializedLinks, event: "feedback", ...metadata !== void 0 && { metadata } }; try { const response = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${config.token}` }, body: JSON.stringify(payload) }); if (!response.ok) { const text = await response.text(); settings?.onError?.( new Error(`Failed to send feedback to Axiom: ${response.status} ${text}`), { links, feedback } ); } } catch (error) { const e = error instanceof Error ? error : new Error(errorToString(error)); if (settings?.onError) { settings.onError(e, { links, feedback }); } else { console.error(e); } } }; return { sendFeedback }; }; var thumbFeedback = ({ name, value, message, category, metadata }) => withKind({ name, value: value === "up" ? 1 : -1, message, category, metadata }, "thumb"); var thumbUpFeedback = (input) => thumbFeedback({ ...input, value: "up" }); var thumbDownFeedback = (input) => thumbFeedback({ ...input, value: "down" }); var enumFeedback = (input) => withKind(input, "text"); var numberFeedback = (input) => withKind(input, "number"); var boolFeedback = (input) => withKind(input, "boolean"); var textFeedback = (input) => withKind(input, "text"); var signalFeedback = (input) => withKind({ ...input, value: null }, "signal"); var Feedback = { /** Creates a signal feedback (no value, just indicates an event occurred). */ signal: signalFeedback, /** Creates a number feedback with a number value. */ number: numberFeedback, /** Creates a boolean feedback with a true/false value. */ bool: boolFeedback, /** Creates a text feedback with a free-form string value. */ text: textFeedback, /** Creates a text feedback from a string enum value. */ enum: enumFeedback, /** Creates a thumb feedback with 'up' or 'down' value. */ thumb: thumbFeedback, /** Creates a thumbs up (+1) feedback. */ thumbUp: thumbUpFeedback, /** Creates a thumbs down (-1) feedback. */ thumbDown: thumbDownFeedback }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Feedback, createFeedbackClient }); //# sourceMappingURL=feedback.cjs.map