//#region src/domains/run/agents/agent-types.ts
/**
* Creates a stopWhen condition that stops when any tool call name starts with the given prefix
* @param prefix - The prefix to check for in tool call names
* @returns A function that can be used as a stopWhen condition
*/
function hasToolCallWithPrefix(prefix) {
	return ({ steps }) => {
		const last = steps.at(-1);
		if (last && "toolCalls" in last && last.toolCalls) return last.toolCalls.some((tc) => tc.toolName.startsWith(prefix));
		return false;
	};
}
/**
* Resolves a generation response from either `generateText` or `streamText` into
* a plain object with all needed values as own properties.
*
* **Why this exists:** The AI SDK returns class instances whose key properties
* (`text`, `steps`, `finishReason`, `output`) are prototype getters.
* `StreamTextResult` getters return `PromiseLike` values; `GenerateTextResult`
* getters return direct values. In both cases, the spread operator `{ ...result }`
* silently drops them. This function uses `Promise.resolve()` to safely resolve
* both styles, then spreads them as explicit own properties so downstream code
* (and further spreads) never loses them.
*/
async function resolveGenerationResponse(response) {
	const stepsValue = response.steps;
	if (!stepsValue) return response;
	try {
		const [steps, text, finishReason, output, usage, totalUsage, responseObj] = await Promise.all([
			Promise.resolve(stepsValue),
			Promise.resolve(response.text),
			Promise.resolve(response.finishReason),
			Promise.resolve(response.output),
			Promise.resolve(response.usage),
			Promise.resolve(response.totalUsage),
			Promise.resolve(response.response)
		]);
		return {
			...response,
			steps,
			text,
			finishReason,
			output,
			usage,
			totalUsage,
			response: responseObj
		};
	} catch (error) {
		throw new Error(`Failed to resolve generation response: ${error instanceof Error ? error.message : String(error)}`);
	}
}
function validateModel(modelString, modelType) {
	if (!modelString?.trim()) throw new Error(`${modelType} model is required. Please configure models at the project level.`);
	return modelString.trim();
}
function isValidTool(tool) {
	if (!tool || typeof tool !== "object") return false;
	const t = tool;
	return typeof t.description === "string" && t.inputSchema != null && typeof t.execute === "function";
}

//#endregion
export { hasToolCallWithPrefix, isValidTool, resolveGenerationResponse, validateModel };