import { Type } from '@sinclair/typebox';
import { SEND_MESSAGE } from '../../../tool-names.js';
import { taskManager as defaultTaskManager, getTaskScopeId } from './manager.js';
const SEND_MESSAGE_TOOL_DESCRIPTION = 'Send a follow-up message to a background subagent by task id or task name. ' +
    'If the target is still running, the message is queued for its next turn boundary. ' +
    'If the target completed and has a transcript, it is restarted from that transcript with the message. ' +
    'Do not repeatedly restart the same background subagent after a failed resume; ' +
    'try once, then stop and report the failure.';
const SEND_MESSAGE_TARGET_DESCRIPTION = 'Background subagent id or registered task name.';
const SEND_MESSAGE_MESSAGE_DESCRIPTION = 'Message to send to subagents.';
const schema = Type.Object({
    target: Type.String({
        description: SEND_MESSAGE_TARGET_DESCRIPTION
    }),
    message: Type.String({
        description: SEND_MESSAGE_MESSAGE_DESCRIPTION
    })
});
/** Build the `send_message` tool that delivers follow-up messages to background subagents. */
export function createSendMessageTool(options = {}) {
    const manager = options.taskManager ?? defaultTaskManager;
    return {
        name: SEND_MESSAGE,
        label: 'Send Message',
        description: SEND_MESSAGE_TOOL_DESCRIPTION,
        parameters: schema,
        async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
            const { target, message } = params;
            const scopeId = getTaskScopeId(ctx?.sessionManager);
            if (!scopeId) {
                return {
                    content: [{ type: 'text', text: 'send_message requires a session.' }],
                    details: undefined,
                    isError: true
                };
            }
            const result = manager.routeMessage(scopeId, target, message);
            if (!result.ok) {
                return {
                    content: [{ type: 'text', text: result.reason }],
                    details: undefined,
                    isError: true
                };
            }
            return {
                content: [
                    {
                        type: 'text',
                        text: `queued message for ${result.agentId}`
                    }
                ],
                details: undefined
            };
        }
    };
}
//# sourceMappingURL=send-message.js.map