{"version":3,"file":"tools.mjs","names":["SearchConstants","OpenAiToolDefinition","CONDITION_SCHEMA","type","additionalProperties","properties","field","description","operator","enum","value","values","items","required","PROPOSE_QUERY_TOOL_NAME","GET_ENTITY_SCHEMA_TOOL_NAME","ProposeQuerySection","ProposeQueryToolOptions","maxPageSize","sections","buildProposeQueryChangesTool","options","MAX_PAGE_SIZE","sectionSchemas","Record","filters","op","conditions","groups","sorts","direction","grouping","groupBy","aggregates","operation","columns","showOnly","show","hide","pageSize","explanation","section","function","name","parameters","buildGetEntitySchemaTool","entityName"],"sources":["../../src/searchIntent/tools.ts"],"sourcesContent":["import { SearchConstants } from '@univerus/udp-stencil-component-library';\nimport type { OpenAiToolDefinition } from '../aiAssistant/openai';\n\n// OpenAI-compatible tool definitions for the search-intent copilot pattern.\n// Kept deliberately union-free (no oneOf/anyOf) — small models are far more\n// reliable against flat schemas. Groups are a parallel array instead of\n// recursive conditions, which also matches the builder's max nesting depth.\n\nconst CONDITION_SCHEMA = {\n  type: 'object',\n  additionalProperties: false,\n  properties: {\n    field: {\n      type: 'string',\n      description:\n        'A field path from the \"Filterable fields\" list, exactly as written there.'\n    },\n    operator: {\n      type: 'string',\n      enum: [\n        'equals',\n        'not_equals',\n        'contains',\n        'starts_with',\n        'greater_than',\n        'greater_than_or_equal',\n        'less_than',\n        'less_than_or_equal',\n        'between',\n        'in',\n        'not_in',\n        'is_empty',\n        'is_not_empty'\n      ]\n    },\n    value: {\n      type: 'string',\n      description:\n        'Value for single-value operators. Booleans: \"true\"/\"false\". Dates: YYYY-MM-DD. Datetimes: YYYY-MM-DDTHH:mm:ss. Omit for is_empty/is_not_empty.'\n    },\n    values: {\n      type: 'array',\n      items: { type: 'string' },\n      description:\n        'Values for between (exactly 2, low then high) and in/not_in (1 or more).'\n    }\n  },\n  required: ['field', 'operator']\n};\n\nexport const PROPOSE_QUERY_TOOL_NAME = 'propose_query_changes';\nexport const GET_ENTITY_SCHEMA_TOOL_NAME = 'get_entity_schema';\n\nexport type ProposeQuerySection =\n  'filters' | 'sorts' | 'grouping' | 'columns' | 'pageSize';\n\nexport interface ProposeQueryToolOptions {\n  /** Upper bound mentioned in the pageSize description. Default: SearchConstants.MAX_PAGE_SIZE. */\n  maxPageSize?: number;\n  /**\n   * Which intent sections the host can actually apply — sections not listed are\n   * omitted from the tool schema entirely, so the model cannot propose them\n   * (e.g. a grid filter assistant passes ['filters']). Default: all sections.\n   */\n  sections?: ProposeQuerySection[];\n}\n\n/** The propose tool: declarative full-state-per-section query changes. */\nexport const buildProposeQueryChangesTool = (\n  options?: ProposeQueryToolOptions\n): OpenAiToolDefinition => {\n  const maxPageSize = options?.maxPageSize ?? SearchConstants.MAX_PAGE_SIZE;\n  const sections = options?.sections ?? [\n    'filters',\n    'sorts',\n    'grouping',\n    'columns',\n    'pageSize'\n  ];\n\n  const sectionSchemas: Record<ProposeQuerySection, object> = {\n    filters: {\n      type: ['object', 'null'],\n      description:\n        'Full replacement of the filter state. Use {\"op\":\"and\",\"conditions\":[]} to clear all filters. null = leave filters unchanged.',\n      additionalProperties: false,\n      properties: {\n        op: {\n          type: 'string',\n          enum: ['and', 'or'],\n          description: 'How root-level conditions and groups combine.'\n        },\n        conditions: { type: 'array', items: CONDITION_SCHEMA },\n        groups: {\n          type: 'array',\n          description:\n            'Optional nested condition groups (one level deep only).',\n          items: {\n            type: 'object',\n            additionalProperties: false,\n            properties: {\n              op: { type: 'string', enum: ['and', 'or'] },\n              conditions: { type: 'array', items: CONDITION_SCHEMA }\n            },\n            required: ['op', 'conditions']\n          }\n        }\n      },\n      required: ['op', 'conditions']\n    },\n    sorts: {\n      type: ['array', 'null'],\n      description:\n        'Full replacement of sorting. [] clears sorting. null = leave unchanged. Only columns marked orderable.',\n      items: {\n        type: 'object',\n        additionalProperties: false,\n        properties: {\n          field: {\n            type: 'string',\n            description: 'A root column name marked orderable.'\n          },\n          direction: { type: 'string', enum: ['ASC', 'DESC'] }\n        },\n        required: ['field', 'direction']\n      }\n    },\n    grouping: {\n      type: ['object', 'null'],\n      description:\n        'Full replacement of grouping. {\"groupBy\":[],\"aggregates\":[]} clears grouping. null = leave unchanged. Aggregates require at least one groupBy column.',\n      additionalProperties: false,\n      properties: {\n        groupBy: {\n          type: 'array',\n          items: { type: 'string' },\n          description: 'Root column names to group by.'\n        },\n        aggregates: {\n          type: 'array',\n          items: {\n            type: 'object',\n            additionalProperties: false,\n            properties: {\n              field: {\n                type: 'string',\n                description: 'A root column name marked aggregatable.'\n              },\n              operation: {\n                type: 'string',\n                enum: ['COUNT', 'SUM', 'MIN', 'MAX', 'AVG']\n              }\n            },\n            required: ['field', 'operation']\n          }\n        }\n      },\n      required: ['groupBy', 'aggregates']\n    },\n    columns: {\n      type: ['object', 'null'],\n      description:\n        'Column visibility changes. null = leave unchanged. showOnly: make exactly these visible; otherwise show/hide adjust individual columns.',\n      additionalProperties: false,\n      properties: {\n        showOnly: { type: 'array', items: { type: 'string' } },\n        show: { type: 'array', items: { type: 'string' } },\n        hide: { type: 'array', items: { type: 'string' } }\n      }\n    },\n    pageSize: {\n      type: ['number', 'null'],\n      description: `New page size (1-${maxPageSize}). null = leave unchanged.`\n    }\n  };\n\n  const properties: Record<string, object> = {\n    explanation: {\n      type: 'string',\n      description:\n        'One short sentence describing the proposed change, shown to the user.'\n    }\n  };\n  for (const section of sections) {\n    properties[section] = sectionSchemas[section];\n  }\n\n  return {\n    type: 'function',\n    function: {\n      name: PROPOSE_QUERY_TOOL_NAME,\n      description:\n        'Propose changes to the current query. Each top-level section is declarative: null (or omitted) leaves that section unchanged; a present value fully replaces it.',\n      parameters: {\n        type: 'object',\n        additionalProperties: false,\n        properties,\n        required: ['explanation']\n      }\n    }\n  };\n};\n\n/** Entity-schema lookup tool; the host supplies the execute implementation. */\nexport const buildGetEntitySchemaTool = (): OpenAiToolDefinition => ({\n  type: 'function',\n  function: {\n    name: GET_ENTITY_SCHEMA_TOOL_NAME,\n    description:\n      \"Look up an entity's schema: its fields (with types and filter/sort/aggregate capabilities) and how it relates to other entities. Use this to answer schema questions or to check an entity before discussing it. Results come back as a tool message; then answer the user in plain text.\",\n    parameters: {\n      type: 'object',\n      additionalProperties: false,\n      properties: {\n        entityName: {\n          type: 'string',\n          description:\n            'Simple entity name from the catalog list, e.g. \"ChargeCode\".'\n        }\n      },\n      required: ['entityName']\n    }\n  }\n});\n"],"mappings":";;AAQA,MAAME,mBAAmB;CACvBC,MAAM;CACNC,sBAAsB;CACtBC,YAAY;EACVC,OAAO;GACLH,MAAM;GACNI,aACE;GACH;EACDC,UAAU;GACRL,MAAM;GACNM,MAAM;IACJ;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IAAc;GAEjB;EACDC,OAAO;GACLP,MAAM;GACNI,aACE;GACH;EACDI,QAAQ;GACNR,MAAM;GACNS,OAAO,EAAET,MAAM,UAAU;GACzBI,aACE;GACJ;EACD;CACDM,UAAU,CAAC,SAAS,WAAU;CAC/B;AAED,MAAaC,0BAA0B;AACvC,MAAaC,8BAA8B;;AAiB3C,MAAaK,gCACXC,YACyB;CACzB,MAAMH,cAAcG,SAASH,eAAelB,gBAAgBsB;CAC5D,MAAMH,WAAWE,SAASF,YAAY;EACpC;EACA;EACA;EACA;EACA;EACD;CAED,MAAMI,iBAAsD;EAC1DE,SAAS;GACPtB,MAAM,CAAC,UAAU,OAAO;GACxBI,aACE;GACFH,sBAAsB;GACtBC,YAAY;IACVqB,IAAI;KACFvB,MAAM;KACNM,MAAM,CAAC,OAAO,KAAK;KACnBF,aAAa;KACd;IACDoB,YAAY;KAAExB,MAAM;KAASS,OAAOV;KAAkB;IACtD0B,QAAQ;KACNzB,MAAM;KACNI,aACE;KACFK,OAAO;MACLT,MAAM;MACNC,sBAAsB;MACtBC,YAAY;OACVqB,IAAI;QAAEvB,MAAM;QAAUM,MAAM,CAAC,OAAO,KAAI;QAAG;OAC3CkB,YAAY;QAAExB,MAAM;QAASS,OAAOV;QAAiB;OACtD;MACDW,UAAU,CAAC,M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