agents
Version:
A home for your AI agents
69 lines (68 loc) • 2.93 kB
JavaScript
import { t as __DO_NOT_USE_WILL_BREAK__agentContext } from "./current-agent-DhoDkSnH.js";
import "./internal_context.js";
import { asSchema, tool } from "ai";
//#region src/agent-tools.ts
/**
* Preserve the existing Zod-style `.parse()` output-schema contract while
* extending validation to the other formats supported by AI SDK FlexibleSchema.
*/
async function validateOutput(schema, value) {
if (typeof schema === "object" && schema !== null && "parse" in schema && typeof schema.parse === "function") return schema.parse(value);
const validate = asSchema(schema).validate;
if (!validate) throw new Error("agentTool outputSchema must provide runtime validation");
const result = await validate(value);
if (!result.success) throw result.error;
return result.value;
}
function currentAgentToolRunner() {
const agent = __DO_NOT_USE_WILL_BREAK__agentContext.getStore()?.agent;
if (agent === null || typeof agent !== "object" || typeof agent.runAgentTool !== "function") throw new Error("agentTool() can only run inside an Agent turn. Use it from getTools() on an Agent subclass.");
return agent;
}
function failure(status, error, retryable, extra) {
return {
ok: false,
status,
error,
retryable,
...extra?.reason !== void 0 ? { reason: extra.reason } : {},
...extra?.childStillRunning !== void 0 ? { childStillRunning: extra.childStillRunning } : {}
};
}
function agentTool(cls, options) {
return tool({
description: options.description,
inputSchema: options.inputSchema,
execute: async (input, executeOptions) => {
const display = options.displayName || options.icon || options.display ? {
...options.display,
...options.displayName ? { name: options.displayName } : {},
...options.icon ? { icon: options.icon } : {}
} : void 0;
const runId = executeOptions?.toolCallId ? `agent-tool:${executeOptions.toolCallId}` : void 0;
const result = await currentAgentToolRunner().runAgentTool(cls, {
input,
runId,
parentToolCallId: executeOptions?.toolCallId,
signal: executeOptions?.abortSignal,
display
});
if (result.status === "completed") {
if (options.outputSchema) {
if (result.output === void 0) return failure("error", "agent tool completed without structured output required by outputSchema", false);
return validateOutput(options.outputSchema, result.output);
}
return result.summary ?? "";
}
if (result.status === "aborted") return failure("aborted", result.error ?? "agent tool run was cancelled", false);
if (result.status === "interrupted") return failure("interrupted", result.error ?? "agent tool run was interrupted before it finished; it can be retried", true, {
reason: result.reason,
childStillRunning: result.childStillRunning
});
return failure("error", result.error ?? "agent tool run failed", false);
}
});
}
//#endregion
export { agentTool };
//# sourceMappingURL=agent-tools.js.map