@tanstack/ai
Version:
Type-safe TypeScript AI SDK for streaming chat, tool calling, agents, structured outputs, and multimodal generation.
46 lines (45 loc) • 1.31 kB
JavaScript
class CapabilityRegistry {
provided = /* @__PURE__ */ new Set();
onDuplicate;
/** Register a callback fired when a handle is provided more than once. */
setOnDuplicate(cb) {
this.onDuplicate = cb;
}
/** Record that `handle` was provided; fire the duplicate callback on repeats. */
markProvided(handle) {
if (this.provided.has(handle)) this.onDuplicate?.(handle.capabilityName);
this.provided.add(handle);
}
has(handle) {
return this.provided.has(handle);
}
}
function createCapability() {
return (name) => {
const values = /* @__PURE__ */ new WeakMap();
function get(ctx, opts) {
if (!values.has(ctx)) {
if (opts?.optional) return void 0;
throw new Error(
`Capability "${name}" was requested but never provided. Ensure a middleware provides it in setup(), ordered before this consumer.`
);
}
return values.get(ctx);
}
const provide = (ctx, value) => {
values.set(ctx, value);
ctx.capabilities.markProvided(handle);
};
const pair = [get, provide];
const handle = Object.assign(pair, {
capabilityName: name,
has: (ctx) => values.has(ctx)
});
return handle;
};
}
export {
CapabilityRegistry,
createCapability
};
//# sourceMappingURL=capabilities.js.map