import { z } from 'zod';
import { defineTool, } from '../toolDefinition.js';
import { AgentToolNames } from '../shared/agentToolNames.js';
const iterationConfigSchema = z.object({
    iteration_type: z.enum(['fixed', 'variable']).meta({
        description: 'How to determine the number of iterations. "fixed" = a literal number, "variable" = the name of a variable that holds a number.',
    }),
    iteration_value: z.string().meta({
        description: 'The value for the iteration. For "fixed": a whole-number string (e.g. "3"). For "variable": a variable name without braces (e.g. "itemCount").',
    }),
});
export const executeFlowArgumentsSchema = z.object({
    flow_id: z.string().meta({
        description: 'The unique identifier of the flow to execute. Get this from request_flow_tools search results.',
    }),
    parameters: z.record(z.string(), z.string()).optional().meta({
        description: 'Parameters for the flow as a JSON object. Keys should match the parameter names from the flow. Values can reference variables using {{@variableName}} syntax.',
    }),
    loop: iterationConfigSchema.optional().meta({
        description: 'Optional loop configuration. When provided, the flow step is configured to execute multiple times. Omit this field entirely if the flow should run only once.',
    }),
});
export const executeFlowServerResultSchema = z.object({
    flow_name: z.string().optional().meta({
        description: 'The human-readable name of the flow, resolved from the flow ID.',
    }),
    flow_parameters: z
        .array(z.object({
        name: z.string(),
        default_value: z.string().optional(),
    }))
        .optional()
        .meta({
        description: 'Parameters defined on the flow, if any.',
    }),
    parameter_warning: z.string().optional().meta({
        description: 'Warning message if the flow has parameters but none were provided.',
    }),
});
/**
 * Response schema for execute_flow tool.
 * The client should include the new screenshot and any variables created by the flow
 * to provide context for the next agent decision.
 */
export const executeFlowClientResponseSchema = z.object({
    success: z.boolean(),
    message: z.string().optional(),
    // Context fields - client should include these after executing the action
    new_screenshot: z.string().optional().meta({
        description: 'Base64-encoded PNG screenshot of the page after flow execution',
    }),
    current_url: z.string().optional().meta({
        description: 'The current URL after flow execution',
    }),
    created_variables: z.record(z.string(), z.string()).optional().meta({
        description: 'Variables created or modified by the flow, as name-value pairs',
    }),
});
export const ExecuteFlowToolDefinition = defineTool({
    name: AgentToolNames.ExecuteFlow,
    description: `Executes a mabl flow. A flow is a reusable sequence of steps that automates a task in the application. The client response will include a new screenshot and any variables created by the flow.

**Workflow**:
1. First, use request_flow_tools to search for flows relevant to your current task
2. Review the search results to find a flow that matches your needs
3. Use this tool with the flow_id from the search results
4. Provide parameters as a JSON object matching the flow's parameter schema
5. Optionally provide a loop configuration to run the flow multiple times

**Parameter Values**:
- Parameters can be literal values: {"username": "test@example.com"}
- Parameters can reference existing variables: {"username": "{{@email}}"}
- Parameter names and requirements are shown in the flow search results

**Loop Configuration**:
- Omit \`loop\` entirely if the flow should run only once
- Use \`loop\` when you need to repeat the flow a fixed number of times, a variable number of times, or once per item in a list
- See the flow_loop skill for detailed guidance on when and how to use loops

**Example** (single run):
{
  "flow_id": "abc-123-f",
  "parameters": {
    "username": "{{@testUser}}",
    "password": "{{@testPassword}}"
  }
}

**Example** (loop 3 times):
{
  "flow_id": "abc-123-f",
  "parameters": {},
  "loop": {
    "iteration_type": "fixed",
    "iteration_value": "3"
  }
}`,
    argumentsSchema: executeFlowArgumentsSchema,
    serverPrepareResultSchema: executeFlowServerResultSchema,
    responseSchema: executeFlowClientResponseSchema,
});
//# sourceMappingURL=ExecuteFlowDefinition.js.map