@arizeai/phoenix-client
Version:
A client for the Phoenix API
57 lines • 2.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toSpanAnnotationData = toSpanAnnotationData;
exports.toDocumentAnnotationData = toDocumentAnnotationData;
/**
* Build and validate annotation result fields
*/
function buildAnnotationResult(annotation, annotationType) {
const result = {};
// Build result with trimming for string fields
if (annotation.label !== undefined) {
result.label = annotation.label.trim() || null;
}
if (annotation.score !== undefined) {
result.score = annotation.score;
}
if (annotation.explanation !== undefined) {
result.explanation = annotation.explanation.trim() || null;
}
// Validate that at least one result field is provided
const hasValidResult = result.label || result.score !== undefined || result.explanation;
if (!hasValidResult) {
throw new Error(`At least one of label, score, or explanation must be provided for ${annotationType} annotation`);
}
return result;
}
/**
* Convert a SpanAnnotation to the API format
*/
function toSpanAnnotationData(annotation) {
var _a, _b, _c, _d;
const result = buildAnnotationResult(annotation, "span");
return {
span_id: annotation.spanId.trim(),
name: annotation.name.trim(),
annotator_kind: (_a = annotation.annotatorKind) !== null && _a !== void 0 ? _a : "HUMAN",
result,
metadata: (_b = annotation.metadata) !== null && _b !== void 0 ? _b : null,
identifier: (_d = (_c = annotation.identifier) === null || _c === void 0 ? void 0 : _c.trim()) !== null && _d !== void 0 ? _d : "",
};
}
/**
* Convert a DocumentAnnotation to the API format
*/
function toDocumentAnnotationData(annotation) {
var _a, _b;
const result = buildAnnotationResult(annotation, "document");
return {
span_id: annotation.spanId.trim(),
document_position: annotation.documentPosition,
name: annotation.name.trim(),
annotator_kind: (_a = annotation.annotatorKind) !== null && _a !== void 0 ? _a : "HUMAN",
result,
metadata: (_b = annotation.metadata) !== null && _b !== void 0 ? _b : null,
};
}
//# sourceMappingURL=types.js.map