@tanstack/ai
Version:
Type-safe TypeScript AI SDK for streaming chat, tool calling, agents, structured outputs, and multimodal generation.
77 lines (76 loc) • 2.54 kB
JavaScript
//#region src/activities/evaluate/adapter.ts
/** `EvaluateError.code` when the provider declined to answer a question. */
var EVALUATE_REFUSAL_CODE = "refusal";
/**
* Thrown by an evaluate adapter when the provider answered, but the call
* cannot become a result. It carries what the provider already returned,
* so callers can still read the billed usage and the answers that came back.
*
* The `name` is always `EvaluateError`.
* Another package can read this shape without an import of this class.
* Use {@link isEvaluateError} to narrow a caught value. `instanceof` fails
* when the app has more than one copy of `@tanstack/ai`.
*/
var EvaluateError = class extends Error {
/** Stable code to branch on, for example `EVALUATE_REFUSAL_CODE`. */
code;
/** Tokens the provider reported for this call. */
usage;
/** Resolved model id from the provider. */
model;
/**
* Wire answers the provider did return, before `decide()` maps them. A
* `choice()` answer has `choice`, and a `boolean()` answer is
* `{ type: 'noul', noul }`. A refused question has no key.
*/
answers;
constructor(init) {
super(init.message);
this.name = "EvaluateError";
this.code = init.code;
this.usage = init.usage;
this.model = init.model;
this.answers = init.answers;
}
};
/**
* This function returns true when `value` has the evaluate error shape.
*
* The check reads `name` and `code`.
* A plain object with those fields passes.
* The check does not use `instanceof`, so it also matches an error from
* another copy of `@tanstack/ai`.
*
* @param value - A thrown value, or a plain object
*
* @example
* if (isEvaluateError(error) && error.code === EVALUATE_REFUSAL_CODE) {
* error.usage
* }
*/
function isEvaluateError(value) {
if (typeof value !== "object" || value === null) return false;
if (!("name" in value) || value.name !== "EvaluateError") return false;
return "code" in value && typeof value.code === "string";
}
/**
* Abstract base class for evaluate adapters.
* Extend this class to implement an evaluate adapter for a specific provider.
*
* Generic parameters match EvaluateAdapter. The provider function resolves them.
*/
var BaseEvaluateAdapter = class {
kind = "evaluate";
model;
config;
constructor(config = {}, model) {
this.config = config;
this.model = model;
}
generateId() {
return `${this.name}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
}
};
//#endregion
export { BaseEvaluateAdapter, EVALUATE_REFUSAL_CODE, EvaluateError, isEvaluateError };
//# sourceMappingURL=adapter.js.map