import type { JsonSerializable } from '@servicenow/sdk-core/app';
import type { Record, Table } from '@servicenow/sdk-core/db';
import type { AiAgentToolRecordType, AiAgentToolType, FieldType, OutputTransformationStrategy, ToolDocumentStatus, ToolExecutionMode, RecordOperationName, SortType } from './constants';
/**
 * Reference configuration for reference-type fields
 */
export type ReferenceConfig = {
    /** The reference table name */
    table: string | Table | Record<'sys_db_object'>;
    /** The field in the reference table to display */
    field: string;
};
/**
 * Input field definition for tool inputs
 */
export type ToolInputField = {
    /** Description of the input field */
    description: string;
    /** Optional: Invalid message to display when validation fails */
    invalidMessage?: string | null;
    /** Whether the input field is mandatory */
    mandatory: boolean;
    /** Optional: Column name in the target table to map this input to (for create/update operations) */
    mappedToColumn?: string;
    /** Name of the input field */
    name: string;
    /** Optional: Field type (string, reference, integer, etc.) */
    type?: FieldType;
    /** Optional: Default value for the input field */
    value?: string;
    /** Optional: Reference configuration for reference-type fields */
    referenceConfig?: ReferenceConfig;
};
/**
 * Return field definition for tool outputs
 * */
export type ToolReturnField = {
    name: string;
    /** Optional: Field type (string, reference, integer, etc.) */
    type?: FieldType;
    /** Optional: Reference configuration for reference-type fields */
    referenceConfig?: ReferenceConfig;
};
/**
 * Tool input configuration for CRUD operations.
 * This is the user-facing API that gets transformed into the complex inputs JSON.
 *
 * Supports all CRUD operations: create, update, lookup, delete
 */
export type ToolInputType = {
    /** CRUD operation name */
    operationName: RecordOperationName;
    /** Target table reference - can be a simple string (table name) or a TableReference object */
    table: string | Table | Record<'sys_db_object'>;
    /** Input fields that the user provides */
    inputFields: ToolInputField[];
    /** Query condition string (e.g., 'email={{email}}') - alternative to query object */
    queryCondition?: string;
    /** Fields to return after the operation */
    returnFields?: ToolReturnField[];
    /** Optional: Maximum number of records to return (alternative to pagination.limit) */
    limit?: number;
    /** Optional: Field name to order results by (alternative to pagination.orderBy) */
    orderBy?: string;
    /** Optional: Sort direction (alternative to pagination.sortType) */
    sortType?: SortType;
};
/**
 * Input configuration for script tools.
 * Can be either a simple array of input fields or a full ToolInputType for CRUD operations.
 */
export type ScriptToolInputType = ToolInputField[] | ToolInputType;
/**
 * Base type with common fields shared across all tool types.
 * Contains fields from both sn_aia_tool (tool definition) and sn_aia_agent_tool_m2m (tool usage in agent).
 */
type BaseToolDetails = {
    /** Whether the tool is active */
    active?: boolean;
    /** Description of the tool */
    description?: string;
    /** Whether to display the tool output */
    displayOutput?: boolean;
    /** Document status for the tool */
    documentStatus?: ToolDocumentStatus;
    /** Execution mode for the tool (autopilot or copilot) */
    executionMode?: ToolExecutionMode;
    /** Reference to overrides for tool usage in agent (sn_aia_agent_tool_m2m) */
    m2mSysOverrides?: string | Record<'sn_aia_agent_tool_m2m'>;
    /** Maximum number of automatic executions */
    maxAutoExecutions?: number;
    /** Name of the tool */
    name: string;
    /** Strategy for transforming the tool output */
    outputTransformationStrategy?: OutputTransformationStrategy;
    /** Message to display after tool execution */
    postMessage?: string;
    /** Script to execute after the tool runs */
    postProcessingScript?: ((result: any, inputs: any, ...dependencies: any[]) => any) | string;
    /** Message to display before tool execution */
    preMessage?: string;
    /** Whether to run this tool before other tools */
    preRun?: boolean;
    /** Record type for the tool (custom, promoted, or template) */
    recordType?: AiAgentToolRecordType;
    /** Reference to overrides for tool definition (sn_aia_tool) */
    sysOverrides?: string | Record<'sn_aia_tool'>;
    /** Timeout duration for tool execution */
    timeout?: string;
    /** Additional attributes for the tool - structured as an object */
    toolAttributes?: {
        [key: string]: JsonSerializable;
    };
    /** Instructions for transforming the tool output */
    transformationInstructions?: string;
    /** Widgets associated with the tool */
    widgets?: (string | Record<'sn_aia_widget'>)[];
};
/** CRUD tool - uses inputs for database operations (script is auto-generated) */
type CrudToolDetails = BaseToolDetails & {
    type: 'crud';
    /** Input parameters for the CRUD operation */
    inputs: ToolInputType;
};
/** Script tool - uses custom script logic */
type ScriptToolDetails = BaseToolDetails & {
    type: 'script';
    /** Input parameters for the script - can be a simple array of fields or a full ToolInputType for CRUD operations */
    inputs?: ScriptToolInputType;
    /** Script code for the tool */
    script: ((inputs: any, ...dependencies: any[]) => any) | string;
};
/** Capability tool - references a Now Assist skill */
type CapabilityToolDetails = BaseToolDetails & {
    type: 'capability';
    /** Capability ID associated with the tool */
    capabilityId: string | Record<'sn_nowassist_skill_config'>;
};
/** Subflow tool - references a Flow Designer flow */
type SubflowToolDetails = BaseToolDetails & {
    type: 'subflow';
    /** Subflow ID associated with the tool */
    subflowId: string | Record<'sys_hub_flow'>;
};
/** Action tool - references a Flow Designer action */
type ActionToolDetails = BaseToolDetails & {
    type: 'action';
    /** Flow Action ID associated with the tool */
    flowActionId: string | Record<'sys_hub_action_type_definition'>;
};
/** Catalog tool - references a Service Catalog item */
type CatalogToolDetails = BaseToolDetails & {
    type: 'catalog';
    /** Catalog Item ID associated with the tool */
    catalogItemId: string | Record<'sc_cat_item'>;
};
/** Topic tool - references a Virtual Agent topic */
type TopicToolDetails = BaseToolDetails & {
    type: 'topic';
    /** Virtual Agent Topic ID associated with the tool */
    virtualAgentId: string | Record<'sys_cs_topic'>;
};
/** Topic Block tool - references a Virtual Agent topic block */
type TopicBlockToolDetails = BaseToolDetails & {
    type: 'topic_block';
    /** Virtual Agent Topic ID associated with the tool */
    virtualAgentId: string | Record<'sys_cs_topic'>;
};
/** Web Automation tool - OOB Web Search tool (display value: "Web Search") */
type WebAutomationToolDetails = BaseToolDetails & {
    type: 'web_automation';
};
/** Knowledge Graph tool - OOB Knowledge Graph tool */
type KnowledgeGraphToolDetails = BaseToolDetails & {
    type: 'knowledge_graph';
};
/** File Upload tool - OOB File Uploader tool */
type FileUploadToolDetails = BaseToolDetails & {
    type: 'file_upload';
};
/** Search Retrieval tool - OOB RAG Search Retrieval tool */
type RagToolDetails = BaseToolDetails & {
    type: 'rag';
};
/** Generic tool types without specific ID requirements */
type GenericToolDetails = BaseToolDetails & {
    type?: Exclude<AiAgentToolType, 'crud' | 'script' | 'capability' | 'subflow' | 'action' | 'catalog' | 'topic' | 'topic_block' | 'web_automation' | 'knowledge_graph' | 'file_upload' | 'rag'>;
    /** Script code for the tool (optional for generic types) */
    script?: ((inputs: any, ...dependencies: any[]) => any) | string;
};
/**
 * Type representing the combined tool details and usage configuration for an AI Agent Tool.
 * Uses discriminated unions based on the 'type' field to ensure type safety.
 * Merges fields from sn_aia_tool (tool definition) and sn_aia_agent_tool_m2m (tool usage in agent).
 */
export type AiAgentToolDetailsType = CrudToolDetails | ScriptToolDetails | CapabilityToolDetails | SubflowToolDetails | ActionToolDetails | CatalogToolDetails | TopicToolDetails | TopicBlockToolDetails | WebAutomationToolDetails | KnowledgeGraphToolDetails | FileUploadToolDetails | RagToolDetails | GenericToolDetails;
export {};
