agents
Version:
A home for your AI agents
71 lines (70 loc) • 2.8 kB
JavaScript
//#region src/callable-decorator.ts
const callableMetadata = /* @__PURE__ */ new WeakMap();
/**
* Decorator that marks a method as callable by clients
* @param metadata Optional metadata about the callable method
*/
function callable(metadata = {}) {
return function callableDecorator(target, _context) {
if (!callableMetadata.has(target)) callableMetadata.set(target, metadata);
return target;
};
}
let didWarnAboutUnstableCallable = false;
/**
* Decorator that marks a method as callable by clients
* @deprecated this has been renamed to callable, and unstable_callable will be removed in the next major version
* @param metadata Optional metadata about the callable method
*/
const unstable_callable = (metadata = {}) => {
if (!didWarnAboutUnstableCallable) {
didWarnAboutUnstableCallable = true;
console.warn("unstable_callable is deprecated, use callable instead. unstable_callable will be removed in the next major version.");
}
return callable(metadata);
};
/** @internal Read the metadata registered for a decorated method. */
function getCallableMetadata(method) {
return callableMetadata.get(method);
}
/** @internal Whether a method was registered with `@callable()`. */
function isCallableMethod(method) {
return callableMetadata.has(method);
}
/**
* @internal Preserve registration when a framework wraps a decorated
* method (e.g. Agent's context auto-wrapping).
*/
function copyCallableMetadata(source, target) {
const metadata = callableMetadata.get(source);
if (metadata) callableMetadata.set(target, metadata);
}
/**
* Every `@callable()`-registered method reachable on a host's prototype
* chain, with its metadata. The nearest declaration wins when a
* subclass overrides a decorated parent method.
*
* The canonical decorator scan — Agent introspection and the WebSockets
* capability's decorator-fallback target both consume it.
*
* @param host - The object whose prototype chain is scanned.
* @returns Method names mapped to their registered metadata.
*/
function decoratedMethods(host) {
const result = /* @__PURE__ */ new Map();
let prototype = Object.getPrototypeOf(host);
while (prototype && prototype !== Object.prototype) {
for (const name of Object.getOwnPropertyNames(prototype)) {
if (name === "constructor" || result.has(name)) continue;
const descriptor = Object.getOwnPropertyDescriptor(prototype, name);
if (!descriptor || typeof descriptor.value !== "function") continue;
const metadata = getCallableMetadata(descriptor.value);
if (metadata) result.set(name, metadata);
}
prototype = Object.getPrototypeOf(prototype);
}
return result;
}
//#endregion
export { callable, copyCallableMetadata, decoratedMethods, getCallableMetadata, isCallableMethod, unstable_callable };
//# sourceMappingURL=callable-decorator.js.map