mirror of
https://github.com/monoscope-tech/monoscope.git
synced 2026-11-16 10:18:01 +00:00
* feat: Add agentic tool calling for AI query generation Introduces an agentic LLM module that allows the AI to run exploratory queries before generating the final KQL query. This improves accuracy when users ask about specific data that the LLM may not know about. Key changes: - New Pkg.AI.Agentic module with tool definitions and execution - Tools: GetFieldValues, GetServices, CountQuery, SampleLogs - Context-aware mode selection (agentic vs fast mode) - Web explorer uses agentic mode for complex queries - Bot handlers continue using fast mode for lower latency The system intelligently decides when to use tools vs direct response based on query complexity and context (web UI vs bots). * Auto-format code with fourmolu * feat: Use FastMode by default with precomputed facet context Changes: - WebExplorer now uses FastMode by default for faster responses - Pre-include facet context (services, status codes, etc.) in prompts - Add GetFacets tool for agentic mode when deeper exploration needed - Fetch precomputed facets in aiSearchH and pass to agentic config - LLM now has real project data context without needing tool calls This makes queries faster while still providing accurate results by giving the LLM actual field values (services, methods, status codes) from the precomputed facets stored in the database. * Auto-format code with fourmolu * refactor: Use KQL instead of raw SQL for agentic tool execution - Update executeGetFieldValues to use KQL summarize query - Update executeCountQuery to use selectLogTable for count queries - Update executeGetServices to use KQL summarize by resource.service.name - Update executeSampleLogs to use selectLogTable with KQL parsing - Add helper functions for formatting query results - Add Time and Log effects to tool execution constraints - Remove unused SQL query building helpers This change ensures all tool queries go through the KQL infrastructure, enabling caching and optimizations built into the KQL workflow. * Auto-format code with fourmolu * refactor: Add RunQuery tool and apply code review improvements Key changes: - Add RunQuery tool that supports both KQL (preferred) and raw SQL for complex queries that KQL cannot express - Add security validation for SQL queries (SELECT only) - Extract constants (maxFieldValues, maxSampleLogs, etc.) - Add helper functions (successResult, errorResult, getTextArg, etc.) to reduce code duplication in tool execution - Apply LambdaCase extension for cleaner pattern matching - Use mapMaybe instead of filter + map for better efficiency - Extract keyFacetFields as a constant - Reduce overall code complexity from ~289 lines to ~233 lines The LLM can now choose between KQL and SQL based on query complexity, with strong preference for KQL due to its caching and optimizations. * Auto-format code with fourmolu * security: Inject project_id filter into SQL queries - Add injectProjectFilter to automatically add project_id filter - Handles queries with and without existing WHERE clauses - Wraps existing WHERE conditions in parentheses for correct precedence - Updates tool description to inform LLM that project_id is auto-injected This prevents any cross-project data access when using raw SQL queries. * security: Use parameterized CTE for SQL query project filtering Instead of string manipulation to inject project_id (which was vulnerable to SQL injection), use a CTE wrapper with parameterized queries: - Add executeSecuredQuery that wraps user queries in a CTE - The CTE shadows otel_logs_and_spans with a pre-filtered version - project_id is passed as a parameterized query value - Remove unsafe injectProjectFilter and findInsertPoint functions - Clean up unused imports This approach is robust because: 1. project_id cannot be injected - it's a query parameter 2. Any query referencing otel_logs_and_spans gets the filtered version 3. No brittle string parsing of WHERE clauses * Auto-format code with fourmolu * fix: Add missing runKqlQuery helper and reduce tool duplication - Add runKqlQuery helper function that was removed by linter - Refactor executeCountQuery to use runKqlQuery helper - Refactor executeGetServices to use runKqlQuery helper - Refactor executeSampleLogs to use runKqlQuery helper - Remove duplicate/leftover code from partial merge - Net reduction of ~114 lines through DRY refactoring * perf: Replace CTE with direct WHERE injection for project_id CTEs can cause performance issues on large tables because PostgreSQL may materialize the entire CTE before applying user predicates. New approach: - Inject "project_id = ?" directly into the WHERE clause - project_id value is still parameterized (safe from SQL injection) - limit is also parameterized - Query structure manipulation is for WHERE placement only, not values - PostgreSQL can now use indexes on project_id efficiently * security: Block UNION/EXCEPT/INTERSECT to prevent project_id filter bypass - Add UNION/EXCEPT/INTERSECT to blocked SQL patterns since they could allow querying data from other projects in the second query - Improve keyword detection with word boundary checking (prefix, space, newline, tab) instead of just substring matching - Provide specific error message for bypass attempts vs DDL/DML operations * Auto-format code with fourmolu * Refactor Agentic.hs for code succinctness and add unit tests - Group magic constants into ToolLimits config record - Simplify mode selection logic (~25 lines reduced) - Simplify SQL validation and runSqlQuery (~25 lines reduced) - Simplify format functions using ViewPatterns - Use bimap/either for cleaner error handling - Sanitize error messages (don't expose internal errors) - Add unit tests for suggestAgenticMode, tool parsing, SQL validation - Fix pre-existing build errors in RequestDumps.hs (try import, minimum) Total: ~80 lines reduced (663 -> 581 LOC, 12% reduction) * Auto-format code with fourmolu * Fix case-sensitivity bug in executeSecuredQuery The function was using lowerQuery for detection but applying positions to userQuery. While this worked for ASCII (toLower preserves length), it was fragile. Refactored to use a breakOnCI helper that handles case-insensitivity consistently and returns splits of the original text. * Fix hlint warnings in Agentic.hs * Refactor Agentic.hs to use langchain-hs native tool calling - Use langchain-hs LLM.chat API with OpenAI tool definitions instead of custom text-based tool protocol - Define tools using OAITool.Tool format passed to the chat API - Implement agentic loop that handles tool calls via LLM.toolCalls - Maintain conversation history using langchain-hs ChatHistory - Add GetSchema tool for field discovery - Update cabal.project to latest langchain-hs commit - Simplify tests to match updated exports * Auto-format code with fourmolu * Fix hlint warnings in Agentic.hs * Improve code quality in Agentic.hs - Remove fake SQL validation tests that tested non-existent functions - Add maxDisplayRows and maxBodyPreview to ToolLimits (removes magic numbers) - Add getTextArg helper to reduce duplicate pattern matching - Use bool function for jsonToText Bool case - Pass limits to formatSampleLogs and formatQueryResults * Improve code quality and reduce boilerplate in Agentic.hs - Add module-level Haddock documentation explaining the agentic system - Remove unused AgenticResponse type from exports - Add getLimitArg helper to consolidate repeated limit calculation pattern - Add mkSimpleTool and mkProp helpers to reduce tool definition boilerplate - Add toolError helper for consistent error messages with context - Rename getIntFromArgs to getIntArg for consistency with getTextArg * Auto-format code with fourmolu * Fix UTF-8 bug in executeSecuredQuery's breakOnCIKeyword The previous implementation calculated text length on the lowercase version but applied the split to the original text. This would fail for Unicode characters that change length when lowercased (e.g., German ẞ → ss, Turkish İ → i). Fixed by using character-by-character search with proper position tracking on the original string, avoiding the length mismatch issue. * Unify AI system with always-on tool calling - Merge Pkg.AI.Agentic into Pkg.AI for single AI module - Remove FastMode/AgenticMode distinction - always use tool calling - Remove suggestAgenticMode brittle keyword matching - LLM decides when to use tools - Add facet context fetching to all bots (Discord, Slack, WhatsApp) - All channels now have unified behavior with full project awareness - Simplify AgenticConfig by removing mode field - Increase maxIterations to 5 for better tool exploration * Auto-format code with fourmolu * Refactor bot modules to share AI query processing logic Extract common functionality into Pages.Bots.Utils: - AIQueryResult data type for unified response handling - processAIQuery for shared facet fetching and AI execution - formatThreadsPrompt for common thread context formatting Reduces duplication across Discord, Slack, and WhatsApp modules. * Auto-format code with fourmolu * Use langchain-hs TokenBufferMemory for thread context Replace manual formatThreadsPrompt with TokenBufferMemory to provide token-aware thread context management. This prevents prompt overflow by trimming messages to 3000 tokens while preserving system messages. * Auto-format code with fourmolu * Improve AI tool calling with SQL support and code cleanup - Add run_sql_query tool for complex queries not expressible in KQL - Make helper functions more succinct using LambdaCase - Use V.mapMaybe instead of mapMaybe . V.toList for better performance - Preserve diagnostic info in error handling (show parse/execution errors) * Use TokenBufferMemory consistently for chat history management - Add addMessagesToMemory helper in AI.hs using TokenBufferMemory - Replace manual message concatenation in runAgenticLoop with memory-based approach - Simplify roleToText in Utils.hs to use show * Simplify code patterns and reduce redundancy - Simplify executeSecuredQuery using subquery wrapper (34 -> 9 lines) - Use Semigroup for NonEmpty concatenation in addMessagesToMemory - Replace verbose case pattern with <&> in processAIQuery - Use tuple destructuring for Maybe handling * Fix hlint warning: use nonEmpty from Relude 🤖 Generated with [Claude Code](https://claude.com/claude-code) * Remove redundant Data.List.NonEmpty import 🤖 Generated with [Claude Code](https://claude.com/claude-code) * Remove unused TimeSpec import 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
113 lines
3 KiB
113 lines
3 KiB