import { h as __exportAll } from "./pi-embedded-utils-CPkknmhn.js";
import { S as spawnSubagentDirect } from "./tools-B5lBxhIt.js";
import { a as jsonResult, u as readStringParam } from "./common-D7lLwt9D.js";
import { r as optionalStringEnum } from "./typebox-Dqngjw-j.js";
import { Type } from "@sinclair/typebox";

//#region src/agents/tools/sessions/sessions-spawn-tool.ts
var sessions_spawn_tool_exports = /* @__PURE__ */ __exportAll({ createSessionsSpawnTool: () => createSessionsSpawnTool });
const SessionsSpawnToolSchema = Type.Object({
	task: Type.String(),
	label: Type.Optional(Type.String()),
	agentId: Type.Optional(Type.String()),
	model: Type.Optional(Type.String()),
	thinking: Type.Optional(Type.String()),
	template: Type.Optional(Type.String()),
	runTimeoutSeconds: Type.Optional(Type.Number({ minimum: 0 })),
	timeoutSeconds: Type.Optional(Type.Number({ minimum: 0 })),
	cleanup: optionalStringEnum(["delete", "keep"])
});
function createSessionsSpawnTool(opts) {
	return {
		label: "Sessions",
		name: "sessions_spawn",
		description: "Spawn a session with another agent or a sub-agent. Use agentId to target a specific agent (e.g. agentId='carla_bot'). The spawned agent runs the given task and announces results back. Use this when you want to talk to or delegate work to another agent on the gateway.",
		parameters: SessionsSpawnToolSchema,
		execute: async (_toolCallId, args) => {
			const params = args;
			const task = readStringParam(params, "task", { required: true });
			const label = typeof params.label === "string" ? params.label.trim() : "";
			const requestedAgentId = readStringParam(params, "agentId");
			const modelOverride = readStringParam(params, "model");
			const thinkingOverrideRaw = readStringParam(params, "thinking");
			const templateName = readStringParam(params, "template");
			const cleanup = params.cleanup === "keep" || params.cleanup === "delete" ? params.cleanup : "keep";
			const timeoutSecondsCandidate = typeof params.runTimeoutSeconds === "number" ? params.runTimeoutSeconds : typeof params.timeoutSeconds === "number" ? params.timeoutSeconds : void 0;
			return jsonResult(await spawnSubagentDirect({
				task,
				label: label || void 0,
				agentId: requestedAgentId,
				model: modelOverride,
				thinking: thinkingOverrideRaw,
				template: templateName,
				runTimeoutSeconds: typeof timeoutSecondsCandidate === "number" && Number.isFinite(timeoutSecondsCandidate) ? Math.max(0, Math.floor(timeoutSecondsCandidate)) : void 0,
				cleanup,
				expectsCompletionMessage: true
			}, {
				agentSessionKey: opts?.agentSessionKey,
				agentChannel: opts?.agentChannel,
				agentAccountId: opts?.agentAccountId,
				agentTo: opts?.agentTo,
				agentThreadId: opts?.agentThreadId,
				agentGroupId: opts?.agentGroupId,
				agentGroupChannel: opts?.agentGroupChannel,
				agentGroupSpace: opts?.agentGroupSpace,
				requesterAgentIdOverride: opts?.requesterAgentIdOverride
			}));
		}
	};
}

//#endregion
export { sessions_spawn_tool_exports as n, createSessionsSpawnTool as t };