@mastra/core
Version:
249 lines (248 loc) • 9.87 kB
JavaScript
const require_llm = require("./llm-CmflaXHA.cjs");
const require_trip_wire = require("./trip-wire-dxd_uCHj.cjs");
const require_agent = require("./agent-DCD4MApC.cjs");
const require_message_list = require("./message-list-BM7m-E-v.cjs");
const require_dist = require("./dist-BGdEcgoh.cjs");
//#region src/tool-loop-agent/utils.ts
function isToolLoopAgentLike(obj) {
if (!obj) return false;
if (obj instanceof require_message_list.ToolLoopAgent) return true;
return "version" in obj && typeof obj.version === "string" && (obj.version === "agent-v1" || obj.version.startsWith("agent-v"));
}
/**
* Extracts the settings from a ToolLoopAgent instance.
* ToolLoopAgent.settings is private in TypeScript but accessible at runtime.
*/
function getSettings(agent) {
const settings = agent.settings;
if (!settings) throw new Error("Could not extract settings from ToolLoopAgent. The agent may be from an incompatible version.");
return settings;
}
//#endregion
//#region src/tool-loop-agent/tool-loop-processor.ts
var ToolLoopAgentProcessor = class {
id = "tool-loop-agent-processor";
name = "ToolLoop to Mastra Agent Processor";
agent;
settings;
prepareCallResult;
constructor(agent) {
this.agent = agent;
this.settings = getSettings(agent);
}
getAgentConfig() {
const tools = "tools" in this.agent ? this.agent.tools : void 0;
const defaultOptions = {};
if (this.settings.toolChoice) defaultOptions.toolChoice = this.settings.toolChoice;
if (this.settings.providerOptions) defaultOptions.providerOptions = this.settings.providerOptions;
if (this.settings.temperature !== void 0) defaultOptions.modelSettings = {
...defaultOptions.modelSettings ?? {},
temperature: this.settings.temperature
};
if (this.settings.topP !== void 0) defaultOptions.modelSettings = {
...defaultOptions.modelSettings ?? {},
topP: this.settings.topP
};
if (this.settings.topK !== void 0) defaultOptions.modelSettings = {
...defaultOptions.modelSettings ?? {},
topK: this.settings.topK
};
if (this.settings.seed !== void 0) defaultOptions.modelSettings = {
...defaultOptions.modelSettings ?? {},
seed: this.settings.seed
};
if (this.settings.maxOutputTokens !== void 0) defaultOptions.modelSettings = {
...defaultOptions.modelSettings ?? {},
maxOutputTokens: this.settings.maxOutputTokens
};
if (this.settings.presencePenalty !== void 0) defaultOptions.modelSettings = {
...defaultOptions.modelSettings ?? {},
presencePenalty: this.settings.presencePenalty
};
if (this.settings.frequencyPenalty !== void 0) defaultOptions.modelSettings = {
...defaultOptions.modelSettings ?? {},
frequencyPenalty: this.settings.frequencyPenalty
};
if (this.settings.stopSequences !== void 0) defaultOptions.modelSettings = {
...defaultOptions.modelSettings ?? {},
stopSequences: this.settings.stopSequences
};
if (this.settings.stopWhen) defaultOptions.stopWhen = this.settings.stopWhen;
if (this.settings.onStepFinish) defaultOptions.onStepFinish = this.settings.onStepFinish;
if (this.settings.onFinish) defaultOptions.onFinish = this.settings.onFinish;
return {
id: this.settings.id,
name: this.settings.id,
instructions: this.settings.instructions ?? "",
model: this.settings.model,
tools,
maxRetries: this.settings.maxRetries,
defaultOptions: Object.keys(defaultOptions).length > 0 ? defaultOptions : void 0
};
}
/**
* Maps prepareCall or prepareStep result to ProcessInputStepResult.
* Both hooks return similar structures that can override model, tools, activeTools, etc.
*/
mapToProcessInputStepResult(result) {
if (!result) return {};
const stepResult = {};
if (result.model) stepResult.model = result.model;
if ("tools" in result && result.tools) stepResult.tools = result.tools;
if ("toolChoice" in result && result.toolChoice !== void 0) stepResult.toolChoice = result.toolChoice;
if (result.activeTools) stepResult.activeTools = result.activeTools;
if ("providerOptions" in result && result.providerOptions) stepResult.providerOptions = result.providerOptions;
const modelSettings = {};
if ("temperature" in result && result.temperature !== void 0) modelSettings.temperature = result.temperature;
if ("topP" in result && result.topP !== void 0) modelSettings.topP = result.topP;
if ("topK" in result && result.topK !== void 0) modelSettings.topK = result.topK;
if ("maxOutputTokens" in result && result.maxOutputTokens !== void 0) modelSettings.maxOutputTokens = result.maxOutputTokens;
if ("presencePenalty" in result && result.presencePenalty !== void 0) modelSettings.presencePenalty = result.presencePenalty;
if ("frequencyPenalty" in result && result.frequencyPenalty !== void 0) modelSettings.frequencyPenalty = result.frequencyPenalty;
if ("stopSequences" in result && result.stopSequences !== void 0) modelSettings.stopSequences = result.stopSequences;
if ("seed" in result && result.seed !== void 0) modelSettings.seed = result.seed;
if (Object.keys(modelSettings).length > 0) stepResult.modelSettings = modelSettings;
const systemContent = "instructions" in result ? result.instructions : "system" in result ? result.system : void 0;
if (systemContent) {
if (typeof systemContent === "string") stepResult.systemMessages = [{
role: "system",
content: systemContent
}];
else if (Array.isArray(systemContent)) stepResult.systemMessages = systemContent.map((msg) => typeof msg === "string" ? {
role: "system",
content: msg
} : msg);
else if (typeof systemContent === "object" && "role" in systemContent && "content" in systemContent) stepResult.systemMessages = [systemContent];
}
if ("messages" in result && result.messages && Array.isArray(result.messages)) stepResult.messages = result.messages;
return stepResult;
}
async handlePrepareCall(args) {
if (this.settings.prepareCall) {
const { model, messages, activeTools, providerOptions, modelSettings, tools } = args;
const prepareCallInput = {
messages,
model,
tools,
instructions: this.settings.instructions,
stopWhen: this.settings.stopWhen,
activeTools,
providerOptions,
temperature: modelSettings?.temperature,
topP: modelSettings?.topP,
topK: modelSettings?.topK,
maxOutputTokens: modelSettings?.maxOutputTokens,
presencePenalty: modelSettings?.presencePenalty,
frequencyPenalty: modelSettings?.frequencyPenalty,
stopSequences: modelSettings?.stopSequences,
seed: modelSettings?.seed
};
const prepareCallResult = await this.settings.prepareCall(prepareCallInput);
this.prepareCallResult = prepareCallResult;
}
}
async handlePrepareStep(args, currentResult) {
if (this.settings.prepareStep) {
const { messages, steps, stepNumber } = args;
let model = args.model;
if (currentResult.model) {
const resolvedModel = await require_llm.resolveModelConfig(currentResult.model);
if (!require_trip_wire.isSupportedLanguageModel(resolvedModel)) throw new Error("prepareStep returned an unsupported model version");
model = resolvedModel;
}
const prepareStepInputArgs = {
model,
messages,
steps,
stepNumber,
experimental_context: void 0
};
return await this.settings.prepareStep(prepareStepInputArgs);
}
}
async processInputStep(args) {
const { stepNumber } = args;
if (stepNumber === 0 && this.settings.prepareCall) await this.handlePrepareCall(args);
let result = {};
if (this.prepareCallResult) {
const mappedResult = this.mapToProcessInputStepResult(this.prepareCallResult);
if (Object.keys(mappedResult).length > 0) result = {
...result,
...mappedResult
};
}
if (this.settings.prepareStep) {
const prepareStepResult = await this.handlePrepareStep(args, result);
if (prepareStepResult) {
const mappedResult = this.mapToProcessInputStepResult(prepareStepResult);
result = {
...result,
...mappedResult
};
}
}
return result;
}
};
//#endregion
//#region src/tool-loop-agent/index.ts
/**
* Converts an AI SDK v6 ToolLoopAgent instance into a Mastra Agent.
*
* This enables users to create a ToolLoopAgent using AI SDK's API
* while gaining access to Mastra features like memory, processors, scorers, and observability.
*
* @example
* ```typescript
* import { ToolLoopAgent, tool } from 'ai';
* import { openai } from '@ai-sdk/openai';
* import { toolLoopAgentToMastraAgent } from '@mastra/core/tool-loop-agent';
*
* const toolLoopAgent = new ToolLoopAgent({
* id: 'weather-agent',
* model: openai('gpt-4o'),
* instructions: 'You are a helpful weather assistant.',
* tools: { weather: weatherTool },
* temperature: 0.7,
* });
*
* const mastraAgent = toolLoopAgentToMastraAgent(toolLoopAgent);
*
* const result = await mastraAgent.generate({ prompt: 'What is the weather in NYC?' });
* ```
*
* @param agent - The ToolLoopAgent instance
* @param options - Optional name fallback since Mastra Agent requires id/name but ToolLoopAgent doesn't
* @returns A Mastra Agent instance
*/
function toolLoopAgentToMastraAgent(agent, options) {
const processor = new ToolLoopAgentProcessor(agent);
const agentConfig = processor.getAgentConfig();
const id = agentConfig.id || options?.fallbackName || `tool-loop-agent-${require_dist.generateId()}`;
return new require_agent.Agent({
...agentConfig,
id,
name: agentConfig.name || id,
inputProcessors: [processor]
});
}
//#endregion
Object.defineProperty(exports, "getSettings", {
enumerable: true,
get: function() {
return getSettings;
}
});
Object.defineProperty(exports, "isToolLoopAgentLike", {
enumerable: true,
get: function() {
return isToolLoopAgentLike;
}
});
Object.defineProperty(exports, "toolLoopAgentToMastraAgent", {
enumerable: true,
get: function() {
return toolLoopAgentToMastraAgent;
}
});
//# sourceMappingURL=tool-loop-agent--q5ddGzV.cjs.map