@superatomai/sdk-node 0.0.3 → 0.0.4-dsp

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"sources":["../node_modules/dotenv/package.json","../node_modules/dotenv/lib/main.js","../src/websocket.ts","../src/types.ts","../src/dashboards/types.ts","../src/reports/types.ts","../src/utils/logger.ts","../src/threads/uiblock.ts","../src/config/storage.ts","../src/threads/thread.ts","../src/threads/thread-manager.ts","../src/handlers/data-request.ts","../src/bundle.ts","../src/handlers/bundle-request.ts","../src/auth/utils.ts","../src/auth/user-storage.ts","../src/auth/validator.ts","../src/handlers/auth-login-requests.ts","../src/handlers/auth-verify-request.ts","../src/userResponse/groq.ts","../src/userResponse/utils.ts","../src/userResponse/schema.ts","../src/userResponse/prompt-loader.ts","../src/llm.ts","../src/userResponse/base-llm.ts","../src/userResponse/anthropic.ts","../src/userResponse/index.ts","../src/utils/log-collector.ts","../src/config/context.ts","../src/handlers/user-prompt-request.ts","../src/handlers/user-prompt-suggestions.ts","../src/userResponse/next-questions.ts","../src/handlers/actions-request.ts","../src/handlers/components-list-response.ts","../src/handlers/users.ts","../src/dashboards/dashboard-storage.ts","../src/handlers/dashboards.ts","../src/reports/report-storage.ts","../src/handlers/reports.ts","../src/auth/user-manager.ts","../src/dashboards/dashboard-manager.ts","../src/reports/report-manager.ts","../src/services/cleanup-service.ts","../src/index.ts"],"sourcesContent":["{\n \"name\": \"dotenv\",\n \"version\": \"17.2.3\",\n \"description\": \"Loads environment variables from .env file\",\n \"main\": \"lib/main.js\",\n \"types\": \"lib/main.d.ts\",\n \"exports\": {\n \".\": {\n \"types\": \"./lib/main.d.ts\",\n \"require\": \"./lib/main.js\",\n \"default\": \"./lib/main.js\"\n },\n \"./config\": \"./config.js\",\n \"./config.js\": \"./config.js\",\n \"./lib/env-options\": \"./lib/env-options.js\",\n \"./lib/env-options.js\": \"./lib/env-options.js\",\n \"./lib/cli-options\": \"./lib/cli-options.js\",\n \"./lib/cli-options.js\": \"./lib/cli-options.js\",\n \"./package.json\": \"./package.json\"\n },\n \"scripts\": {\n \"dts-check\": \"tsc --project tests/types/tsconfig.json\",\n \"lint\": \"standard\",\n \"pretest\": \"npm run lint && npm run dts-check\",\n \"test\": \"tap run tests/**/*.js --allow-empty-coverage --disable-coverage --timeout=60000\",\n \"test:coverage\": \"tap run tests/**/*.js --show-full-coverage --timeout=60000 --coverage-report=text --coverage-report=lcov\",\n \"prerelease\": \"npm test\",\n \"release\": \"standard-version\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git://github.com/motdotla/dotenv.git\"\n },\n \"homepage\": \"https://github.com/motdotla/dotenv#readme\",\n \"funding\": \"https://dotenvx.com\",\n \"keywords\": [\n \"dotenv\",\n \"env\",\n \".env\",\n \"environment\",\n \"variables\",\n \"config\",\n \"settings\"\n ],\n \"readmeFilename\": \"README.md\",\n \"license\": \"BSD-2-Clause\",\n \"devDependencies\": {\n \"@types/node\": \"^18.11.3\",\n \"decache\": \"^4.6.2\",\n \"sinon\": \"^14.0.1\",\n \"standard\": \"^17.0.0\",\n \"standard-version\": \"^9.5.0\",\n \"tap\": \"^19.2.0\",\n \"typescript\": \"^4.8.4\"\n },\n \"engines\": {\n \"node\": \">=12\"\n },\n \"browser\": {\n \"fs\": false\n }\n}\n","const fs = require('fs')\nconst path = require('path')\nconst os = require('os')\nconst crypto = require('crypto')\nconst packageJson = require('../package.json')\n\nconst version = packageJson.version\n\n// Array of tips to display randomly\nconst TIPS = [\n '🔐 encrypt with Dotenvx: https://dotenvx.com',\n '🔐 prevent committing .env to code: https://dotenvx.com/precommit',\n '🔐 prevent building .env in docker: https://dotenvx.com/prebuild',\n '📡 add observability to secrets: https://dotenvx.com/ops',\n '👥 sync secrets across teammates & machines: https://dotenvx.com/ops',\n '🗂️ backup and recover secrets: https://dotenvx.com/ops',\n '✅ audit secrets and track compliance: https://dotenvx.com/ops',\n '🔄 add secrets lifecycle management: https://dotenvx.com/ops',\n '🔑 add access controls to secrets: https://dotenvx.com/ops',\n '🛠️ run anywhere with `dotenvx run -- yourcommand`',\n '⚙️ specify custom .env file path with { path: \\'/custom/path/.env\\' }',\n '⚙️ enable debug logging with { debug: true }',\n '⚙️ override existing env vars with { override: true }',\n '⚙️ suppress all logs with { quiet: true }',\n '⚙️ write to custom object with { processEnv: myObject }',\n '⚙️ load multiple .env files with { path: [\\'.env.local\\', \\'.env\\'] }'\n]\n\n// Get a random tip from the tips array\nfunction _getRandomTip () {\n return TIPS[Math.floor(Math.random() * TIPS.length)]\n}\n\nfunction parseBoolean (value) {\n if (typeof value === 'string') {\n return !['false', '0', 'no', 'off', ''].includes(value.toLowerCase())\n }\n return Boolean(value)\n}\n\nfunction supportsAnsi () {\n return process.stdout.isTTY // && process.env.TERM !== 'dumb'\n}\n\nfunction dim (text) {\n return supportsAnsi() ? `\\x1b[2m${text}\\x1b[0m` : text\n}\n\nconst LINE = /(?:^|^)\\s*(?:export\\s+)?([\\w.-]+)(?:\\s*=\\s*?|:\\s+?)(\\s*'(?:\\\\'|[^'])*'|\\s*\"(?:\\\\\"|[^\"])*\"|\\s*`(?:\\\\`|[^`])*`|[^#\\r\\n]+)?\\s*(?:#.*)?(?:$|$)/mg\n\n// Parse src into an Object\nfunction parse (src) {\n const obj = {}\n\n // Convert buffer to string\n let lines = src.toString()\n\n // Convert line breaks to same format\n lines = lines.replace(/\\r\\n?/mg, '\\n')\n\n let match\n while ((match = LINE.exec(lines)) != null) {\n const key = match[1]\n\n // Default undefined or null to empty string\n let value = (match[2] || '')\n\n // Remove whitespace\n value = value.trim()\n\n // Check if double quoted\n const maybeQuote = value[0]\n\n // Remove surrounding quotes\n value = value.replace(/^(['\"`])([\\s\\S]*)\\1$/mg, '$2')\n\n // Expand newlines if double quoted\n if (maybeQuote === '\"') {\n value = value.replace(/\\\\n/g, '\\n')\n value = value.replace(/\\\\r/g, '\\r')\n }\n\n // Add to object\n obj[key] = value\n }\n\n return obj\n}\n\nfunction _parseVault (options) {\n options = options || {}\n\n const vaultPath = _vaultPath(options)\n options.path = vaultPath // parse .env.vault\n const result = DotenvModule.configDotenv(options)\n if (!result.parsed) {\n const err = new Error(`MISSING_DATA: Cannot parse ${vaultPath} for an unknown reason`)\n err.code = 'MISSING_DATA'\n throw err\n }\n\n // handle scenario for comma separated keys - for use with key rotation\n // example: DOTENV_KEY=\"dotenv://:key_1234@dotenvx.com/vault/.env.vault?environment=prod,dotenv://:key_7890@dotenvx.com/vault/.env.vault?environment=prod\"\n const keys = _dotenvKey(options).split(',')\n const length = keys.length\n\n let decrypted\n for (let i = 0; i < length; i++) {\n try {\n // Get full key\n const key = keys[i].trim()\n\n // Get instructions for decrypt\n const attrs = _instructions(result, key)\n\n // Decrypt\n decrypted = DotenvModule.decrypt(attrs.ciphertext, attrs.key)\n\n break\n } catch (error) {\n // last key\n if (i + 1 >= length) {\n throw error\n }\n // try next key\n }\n }\n\n // Parse decrypted .env string\n return DotenvModule.parse(decrypted)\n}\n\nfunction _warn (message) {\n console.error(`[dotenv@${version}][WARN] ${message}`)\n}\n\nfunction _debug (message) {\n console.log(`[dotenv@${version}][DEBUG] ${message}`)\n}\n\nfunction _log (message) {\n console.log(`[dotenv@${version}] ${message}`)\n}\n\nfunction _dotenvKey (options) {\n // prioritize developer directly setting options.DOTENV_KEY\n if (options && options.DOTENV_KEY && options.DOTENV_KEY.length > 0) {\n return options.DOTENV_KEY\n }\n\n // secondary infra already contains a DOTENV_KEY environment variable\n if (process.env.DOTENV_KEY && process.env.DOTENV_KEY.length > 0) {\n return process.env.DOTENV_KEY\n }\n\n // fallback to empty string\n return ''\n}\n\nfunction _instructions (result, dotenvKey) {\n // Parse DOTENV_KEY. Format is a URI\n let uri\n try {\n uri = new URL(dotenvKey)\n } catch (error) {\n if (error.code === 'ERR_INVALID_URL') {\n const err = new Error('INVALID_DOTENV_KEY: Wrong format. Must be in valid uri format like dotenv://:key_1234@dotenvx.com/vault/.env.vault?environment=development')\n err.code = 'INVALID_DOTENV_KEY'\n throw err\n }\n\n throw error\n }\n\n // Get decrypt key\n const key = uri.password\n if (!key) {\n const err = new Error('INVALID_DOTENV_KEY: Missing key part')\n err.code = 'INVALID_DOTENV_KEY'\n throw err\n }\n\n // Get environment\n const environment = uri.searchParams.get('environment')\n if (!environment) {\n const err = new Error('INVALID_DOTENV_KEY: Missing environment part')\n err.code = 'INVALID_DOTENV_KEY'\n throw err\n }\n\n // Get ciphertext payload\n const environmentKey = `DOTENV_VAULT_${environment.toUpperCase()}`\n const ciphertext = result.parsed[environmentKey] // DOTENV_VAULT_PRODUCTION\n if (!ciphertext) {\n const err = new Error(`NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment ${environmentKey} in your .env.vault file.`)\n err.code = 'NOT_FOUND_DOTENV_ENVIRONMENT'\n throw err\n }\n\n return { ciphertext, key }\n}\n\nfunction _vaultPath (options) {\n let possibleVaultPath = null\n\n if (options && options.path && options.path.length > 0) {\n if (Array.isArray(options.path)) {\n for (const filepath of options.path) {\n if (fs.existsSync(filepath)) {\n possibleVaultPath = filepath.endsWith('.vault') ? filepath : `${filepath}.vault`\n }\n }\n } else {\n possibleVaultPath = options.path.endsWith('.vault') ? options.path : `${options.path}.vault`\n }\n } else {\n possibleVaultPath = path.resolve(process.cwd(), '.env.vault')\n }\n\n if (fs.existsSync(possibleVaultPath)) {\n return possibleVaultPath\n }\n\n return null\n}\n\nfunction _resolveHome (envPath) {\n return envPath[0] === '~' ? path.join(os.homedir(), envPath.slice(1)) : envPath\n}\n\nfunction _configVault (options) {\n const debug = parseBoolean(process.env.DOTENV_CONFIG_DEBUG || (options && options.debug))\n const quiet = parseBoolean(process.env.DOTENV_CONFIG_QUIET || (options && options.quiet))\n\n if (debug || !quiet) {\n _log('Loading env from encrypted .env.vault')\n }\n\n const parsed = DotenvModule._parseVault(options)\n\n let processEnv = process.env\n if (options && options.processEnv != null) {\n processEnv = options.processEnv\n }\n\n DotenvModule.populate(processEnv, parsed, options)\n\n return { parsed }\n}\n\nfunction configDotenv (options) {\n const dotenvPath = path.resolve(process.cwd(), '.env')\n let encoding = 'utf8'\n let processEnv = process.env\n if (options && options.processEnv != null) {\n processEnv = options.processEnv\n }\n let debug = parseBoolean(processEnv.DOTENV_CONFIG_DEBUG || (options && options.debug))\n let quiet = parseBoolean(processEnv.DOTENV_CONFIG_QUIET || (options && options.quiet))\n\n if (options && options.encoding) {\n encoding = options.encoding\n } else {\n if (debug) {\n _debug('No encoding is specified. UTF-8 is used by default')\n }\n }\n\n let optionPaths = [dotenvPath] // default, look for .env\n if (options && options.path) {\n if (!Array.isArray(options.path)) {\n optionPaths = [_resolveHome(options.path)]\n } else {\n optionPaths = [] // reset default\n for (const filepath of options.path) {\n optionPaths.push(_resolveHome(filepath))\n }\n }\n }\n\n // Build the parsed data in a temporary object (because we need to return it). Once we have the final\n // parsed data, we will combine it with process.env (or options.processEnv if provided).\n let lastError\n const parsedAll = {}\n for (const path of optionPaths) {\n try {\n // Specifying an encoding returns a string instead of a buffer\n const parsed = DotenvModule.parse(fs.readFileSync(path, { encoding }))\n\n DotenvModule.populate(parsedAll, parsed, options)\n } catch (e) {\n if (debug) {\n _debug(`Failed to load ${path} ${e.message}`)\n }\n lastError = e\n }\n }\n\n const populated = DotenvModule.populate(processEnv, parsedAll, options)\n\n // handle user settings DOTENV_CONFIG_ options inside .env file(s)\n debug = parseBoolean(processEnv.DOTENV_CONFIG_DEBUG || debug)\n quiet = parseBoolean(processEnv.DOTENV_CONFIG_QUIET || quiet)\n\n if (debug || !quiet) {\n const keysCount = Object.keys(populated).length\n const shortPaths = []\n for (const filePath of optionPaths) {\n try {\n const relative = path.relative(process.cwd(), filePath)\n shortPaths.push(relative)\n } catch (e) {\n if (debug) {\n _debug(`Failed to load ${filePath} ${e.message}`)\n }\n lastError = e\n }\n }\n\n _log(`injecting env (${keysCount}) from ${shortPaths.join(',')} ${dim(`-- tip: ${_getRandomTip()}`)}`)\n }\n\n if (lastError) {\n return { parsed: parsedAll, error: lastError }\n } else {\n return { parsed: parsedAll }\n }\n}\n\n// Populates process.env from .env file\nfunction config (options) {\n // fallback to original dotenv if DOTENV_KEY is not set\n if (_dotenvKey(options).length === 0) {\n return DotenvModule.configDotenv(options)\n }\n\n const vaultPath = _vaultPath(options)\n\n // dotenvKey exists but .env.vault file does not exist\n if (!vaultPath) {\n _warn(`You set DOTENV_KEY but you are missing a .env.vault file at ${vaultPath}. Did you forget to build it?`)\n\n return DotenvModule.configDotenv(options)\n }\n\n return DotenvModule._configVault(options)\n}\n\nfunction decrypt (encrypted, keyStr) {\n const key = Buffer.from(keyStr.slice(-64), 'hex')\n let ciphertext = Buffer.from(encrypted, 'base64')\n\n const nonce = ciphertext.subarray(0, 12)\n const authTag = ciphertext.subarray(-16)\n ciphertext = ciphertext.subarray(12, -16)\n\n try {\n const aesgcm = crypto.createDecipheriv('aes-256-gcm', key, nonce)\n aesgcm.setAuthTag(authTag)\n return `${aesgcm.update(ciphertext)}${aesgcm.final()}`\n } catch (error) {\n const isRange = error instanceof RangeError\n const invalidKeyLength = error.message === 'Invalid key length'\n const decryptionFailed = error.message === 'Unsupported state or unable to authenticate data'\n\n if (isRange || invalidKeyLength) {\n const err = new Error('INVALID_DOTENV_KEY: It must be 64 characters long (or more)')\n err.code = 'INVALID_DOTENV_KEY'\n throw err\n } else if (decryptionFailed) {\n const err = new Error('DECRYPTION_FAILED: Please check your DOTENV_KEY')\n err.code = 'DECRYPTION_FAILED'\n throw err\n } else {\n throw error\n }\n }\n}\n\n// Populate process.env with parsed values\nfunction populate (processEnv, parsed, options = {}) {\n const debug = Boolean(options && options.debug)\n const override = Boolean(options && options.override)\n const populated = {}\n\n if (typeof parsed !== 'object') {\n const err = new Error('OBJECT_REQUIRED: Please check the processEnv argument being passed to populate')\n err.code = 'OBJECT_REQUIRED'\n throw err\n }\n\n // Set process.env\n for (const key of Object.keys(parsed)) {\n if (Object.prototype.hasOwnProperty.call(processEnv, key)) {\n if (override === true) {\n processEnv[key] = parsed[key]\n populated[key] = parsed[key]\n }\n\n if (debug) {\n if (override === true) {\n _debug(`\"${key}\" is already defined and WAS overwritten`)\n } else {\n _debug(`\"${key}\" is already defined and was NOT overwritten`)\n }\n }\n } else {\n processEnv[key] = parsed[key]\n populated[key] = parsed[key]\n }\n }\n\n return populated\n}\n\nconst DotenvModule = {\n configDotenv,\n _configVault,\n _parseVault,\n config,\n decrypt,\n parse,\n populate\n}\n\nmodule.exports.configDotenv = DotenvModule.configDotenv\nmodule.exports._configVault = DotenvModule._configVault\nmodule.exports._parseVault = DotenvModule._parseVault\nmodule.exports.config = DotenvModule.config\nmodule.exports.decrypt = DotenvModule.decrypt\nmodule.exports.parse = DotenvModule.parse\nmodule.exports.populate = DotenvModule.populate\n\nmodule.exports = DotenvModule\n","import WebSocket from 'ws';\nimport type { WebSocketLike } from './types';\n\n/**\n * Creates a WebSocket instance for Node.js using the ws package\n */\nexport function createWebSocket(url: string): WebSocketLike {\n return new WebSocket(url) as WebSocketLike;\n}\n","import { z } from 'zod';\n\n// From/To object schema for WebSocket messages\nexport const MessageParticipantSchema = z.object({\n id: z.string().optional(),\n type: z.string().optional(),\n});\n\nexport type MessageParticipant = z.infer<typeof MessageParticipantSchema>;\n\n// Message schemas for WebSocket communication\nexport const MessageSchema = z.object({\n id: z.string().optional(),\n type: z.string(),\n from: MessageParticipantSchema,\n to: MessageParticipantSchema.optional(),\n payload: z.unknown(),\n});\n\nexport type Message = z.infer<typeof MessageSchema>;\n\nexport const IncomingMessageSchema = z.object({\n id: z.string().optional(),\n type: z.string(),\n from: MessageParticipantSchema,\n to: MessageParticipantSchema.optional(),\n payload: z.unknown(),\n});\n\nexport type IncomingMessage = z.infer<typeof IncomingMessageSchema>;\n\n// Data request/response schemas\nexport const DataRequestPayloadSchema = z.object({\n collection: z.string(),\n op: z.string(),\n params: z.record(z.unknown()).optional(),\n SA_RUNTIME: z.record(z.unknown()).optional(),\n});\n\nexport type DataRequestPayload = z.infer<typeof DataRequestPayloadSchema>;\n\nexport const DataRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('DATA_REQ'),\n payload: DataRequestPayloadSchema,\n});\n\nexport type DataRequestMessage = z.infer<typeof DataRequestMessageSchema>;\n\nexport const AuthLoginRequestPayloadSchema = z.object({\n login_data: z.string(),\n});\n\nexport type AuthLoginRequestPayload = z.infer<typeof AuthLoginRequestPayloadSchema>;\n\nexport const AuthLoginRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('AUTH_LOGIN_REQ'),\n payload: AuthLoginRequestPayloadSchema,\n});\n\nexport type AuthLoginRequestMessage = z.infer<typeof AuthLoginRequestMessageSchema>;\n\nexport const AuthVerifyRequestPayloadSchema = z.object({\n token: z.string(),\n});\n\nexport type AuthVerifyRequestPayload = z.infer<typeof AuthVerifyRequestPayloadSchema>;\n\nexport const AuthVerifyRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('AUTH_VERIFY_REQ'),\n payload: AuthVerifyRequestPayloadSchema,\n});\n\nexport type AuthVerifyRequestMessage = z.infer<typeof AuthVerifyRequestMessageSchema>;\n\nexport const UserPromptRequestPayloadSchema = z.object({\n prompt: z.string(),\n SA_RUNTIME: z.object({\n threadId: z.string(),\n uiBlockId: z.string(),\n }).optional(),\n});\n\nexport type UserPromptRequestPayload = z.infer<typeof UserPromptRequestPayloadSchema>;\n\nexport const UserPromptRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('USER_PROMPT_REQ'),\n payload: UserPromptRequestPayloadSchema,\n});\n\nexport type UserPromptRequestMessage = z.infer<typeof UserPromptRequestMessageSchema>;\n\nexport const UserPromptSuggestionsPayloadSchema = z.object({\n prompt: z.string(),\n limit: z.number().int().positive().default(5),\n});\n\nexport type UserPromptSuggestionsPayload = z.infer<typeof UserPromptSuggestionsPayloadSchema>;\n\nexport const UserPromptSuggestionsMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('USER_PROMPT_SUGGESTIONS_REQ'),\n payload: UserPromptSuggestionsPayloadSchema,\n});\n\nexport type UserPromptSuggestionsMessage = z.infer<typeof UserPromptSuggestionsMessageSchema>;\n\n\nexport const ComponentPropsSchema = z.object({\n query: z.string().optional(),\n title: z.string().optional(),\n description: z.string().optional(),\n config: z.record(z.unknown()).optional(),\n});\n\nexport const ComponentSchema = z.object({\n id: z.string(),\n name: z.string(),\n type: z.string(),\n description: z.string(),\n props: ComponentPropsSchema,\n category: z.string().optional(),\n keywords: z.array(z.string()).optional(),\n});\n\nexport const ComponentsSchema = z.array(ComponentSchema);\n\nexport type Component = z.infer<typeof ComponentSchema>;\n\nexport const ComponentListResponsePayloadSchema = z.object({\n components: z.array(ComponentSchema),\n});\n\nexport type ComponentListResponsePayload = z.infer<typeof ComponentListResponsePayloadSchema>; \n\nexport const ComponentListResponseMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('COMPONENT_LIST_RES'),\n payload: ComponentListResponsePayloadSchema,\n});\n\nexport type ComponentListResponseMessage = z.infer<typeof ComponentListResponseMessageSchema>;\n\n// Admin User Management Request/Response schemas (Unified)\nexport const UsersRequestPayloadSchema = z.object({\n operation: z.enum(['create', 'update', 'delete', 'getAll', 'getOne']),\n data: z.object({\n username: z.string().optional(),\n password: z.string().optional(),\n }).optional(),\n});\n\nexport type UsersRequestPayload = z.infer<typeof UsersRequestPayloadSchema>;\n\nexport const UsersRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('USERS'),\n payload: UsersRequestPayloadSchema,\n});\n\nexport type UsersRequestMessage = z.infer<typeof UsersRequestMessageSchema>;\n\n// UI Logs schema for tracking user request execution\nexport const UILogsPayloadSchema = z.object({\n logs: z.array(z.object({\n timestamp: z.number(),\n level: z.enum(['info', 'error', 'warn', 'debug']),\n message: z.string(),\n type: z.enum(['explanation', 'query', 'general']).optional(),\n data: z.record(z.unknown()).optional(),\n })),\n});\n\nexport type UILogsPayload = z.infer<typeof UILogsPayloadSchema>;\n\nexport const UILogsMessageSchema = z.object({\n id: z.string(), // uiBlockId\n from: MessageParticipantSchema,\n type: z.literal('UI_LOGS'),\n payload: UILogsPayloadSchema,\n});\n\nexport type UILogsMessage = z.infer<typeof UILogsMessageSchema>;\n\n// Actions request schema - for runtime to send component actions and request next questions\nexport const ActionsRequestPayloadSchema = z.object({\n SA_RUNTIME: z.object({\n threadId: z.string(),\n uiBlockId: z.string(),\n }).optional(),\n});\n\nexport type ActionsRequestPayload = z.infer<typeof ActionsRequestPayloadSchema>;\n\nexport const ActionsRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('ACTIONS'),\n payload: ActionsRequestPayloadSchema,\n});\n\nexport type ActionsRequestMessage = z.infer<typeof ActionsRequestMessageSchema>;\n\n// Collection operation types\nexport type CollectionOperation =\n | 'getMany'\n | 'getOne'\n | 'query'\n | 'mutation'\n | 'updateOne'\n | 'deleteOne'\n | 'createOne';\n\nexport type CollectionHandler<TParams = any, TResult = any> = (\n params: TParams\n) => Promise<TResult> | TResult;\n\nexport interface CollectionRegistry {\n [collectionName: string]: {\n [operation: string]: CollectionHandler;\n };\n}\n\nexport type LLMProvider = 'anthropic' | 'groq';\n\nexport interface SuperatomSDKConfig {\n url?: string;\n apiKey: string;\n projectId: string;\n userId?: string;\n type?: string;\n bundleDir?: string;\n promptsDir?: string; // Path to custom prompts directory (defaults to .prompts in SDK)\n ANTHROPIC_API_KEY?: string;\n GROQ_API_KEY?: string;\n LLM_PROVIDERS?: LLMProvider[];\n}\n\n// ==================== Dashboard CRUD Message Schemas ====================\n\n// Import DSL types from dashboards module\nimport { DSLRendererPropsSchema } from './dashboards/types';\nexport type { DSLRendererProps } from './dashboards/types';\n\n// Dashboard CRUD request payload\n\nexport const DashboardsRequestPayloadSchema = z.object({\n operation: z.enum(['create', 'update', 'delete', 'getAll', 'getOne']),\n data: z.object({\n dashboardId: z.string().optional(),\n dashboard: DSLRendererPropsSchema.optional(),\n }).optional(),\n});\n\nexport type DashboardsRequestPayload = z.infer<typeof DashboardsRequestPayloadSchema>;\n\nexport const DashboardsRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('DASHBOARDS'),\n payload: DashboardsRequestPayloadSchema,\n});\n\nexport type DashboardsRequestMessage = z.infer<typeof DashboardsRequestMessageSchema>;\n\n// ==================== Report CRUD Message Schemas ====================\n\n// Import DSL types from reports module\nimport { DSLRendererPropsSchema as ReportDSLRendererPropsSchema } from './reports/types';\nexport type { DSLRendererProps as ReportDSLRendererProps } from './reports/types';\n\n// Report CRUD request payload\nexport const ReportsRequestPayloadSchema = z.object({\n operation: z.enum(['create', 'update', 'delete', 'getAll', 'getOne']),\n data: z.object({\n reportId: z.string().optional(),\n report: ReportDSLRendererPropsSchema.optional(),\n }).optional(),\n});\n\nexport type ReportsRequestPayload = z.infer<typeof ReportsRequestPayloadSchema>;\n\nexport const ReportsRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('REPORTS'),\n payload: ReportsRequestPayloadSchema,\n});\n\nexport type ReportsRequestMessage = z.infer<typeof ReportsRequestMessageSchema>;\n\nexport interface WebSocketLike {\n send(data: string): void;\n close(): void;\n addEventListener(event: string, listener: (event: any) => void): void;\n removeEventListener(event: string, listener: (event: any) => void): void;\n readyState: number;\n CONNECTING: number;\n OPEN: number;\n CLOSING: number;\n CLOSED: number;\n}\n\n","import { z } from 'zod';\n\n// ==================== Dashboard DSL Schemas ====================\n\n// Expression schema for dynamic values\nexport const ExpressionSchema = z.object({\n $exp: z.string(),\n $deps: z.array(z.string()).optional(),\n});\n\nexport type Expression = z.infer<typeof ExpressionSchema>;\n\n// Binding schema for data binding\nexport const BindingSchema = z.object({\n $bind: z.string(),\n $transform: z\n .array(\n z.object({\n name: z.string(),\n args: z.array(z.any()).optional(),\n }),\n )\n .optional(),\n});\n\nexport type Binding = z.infer<typeof BindingSchema>;\n\n// For directive schema\nexport const ForDirectiveSchema = z.object({\n in: z.union([ExpressionSchema, BindingSchema, z.string()]),\n as: z.string(),\n key: z.string().optional(),\n index: z.string().optional(),\n});\n\nexport type ForDirective = z.infer<typeof ForDirectiveSchema>;\n\n// Query specification schema\nexport const QuerySpecSchema = z.object({\n graphql: z.string().optional(),\n sql: z.string().optional(),\n variables: z.record(z.string(), z.any()).optional(),\n params: z.record(z.string(), z.any()).optional(),\n key: z.string().optional(),\n refetchPolicy: z\n .enum(['cache-first', 'network-only', 'cache-and-network'])\n .optional(),\n dependencies: z.array(z.string()).optional(),\n});\n\nexport type QuerySpec = z.infer<typeof QuerySpecSchema>;\n\n// UI Element schema\nexport const UIElementSchema: z.ZodType<any> = z.lazy(() =>\n z.object({\n id: z.string(),\n type: z.string(),\n key: z.union([z.string(), ExpressionSchema]).optional(),\n props: z.record(z.string(), z.any()).optional(),\n query: QuerySpecSchema.optional(),\n if: ExpressionSchema.optional(),\n elseIf: ExpressionSchema.optional(),\n for: ForDirectiveSchema.optional(),\n 'link-to': z\n .union([\n z.string(),\n ExpressionSchema,\n BindingSchema,\n z.object({\n ui: z.union([z.string(), ExpressionSchema, BindingSchema]),\n params: z.record(z.string(), z.any()).optional(),\n }),\n ])\n .optional(),\n _meta: z\n .object({\n id: z.string().optional(),\n version: z.string().optional(),\n created: z.string().optional(),\n lastModified: z.string().optional(),\n })\n .optional(),\n children: z.any().optional(),\n else: UIElementSchema.optional(),\n slots: z\n .record(z.string(), z.union([UIElementSchema, z.array(UIElementSchema)]))\n .optional(),\n platform: z\n .object({\n web: z.any().optional(),\n ios: z.any().optional(),\n android: z.any().optional(),\n })\n .optional(),\n })\n);\n\nexport type UIElement = z.infer<typeof UIElementSchema>;\n\n// UI Component schema\nexport const UIComponentSchema = z.object({\n id: z.string(),\n name: z.string().optional(),\n props: z.record(z.string(), z.any()).optional(),\n states: z.record(z.string(), z.any()).optional(),\n methods: z\n .record(\n z.string(),\n z.object({\n fn: z.string(),\n params: z.record(z.string(), z.any()).optional(),\n }),\n )\n .optional(),\n effects: z\n .array(\n z.object({\n fn: z.string(),\n deps: z.array(z.string()).optional(),\n }),\n )\n .optional(),\n data: z.record(z.string(), z.any()).optional(),\n render: UIElementSchema,\n query: QuerySpecSchema.optional(),\n});\n\nexport type UIComponent = z.infer<typeof UIComponentSchema>;\n\n// DSL Renderer Props schema\nexport const DSLRendererPropsSchema = z.object({\n dsl: UIComponentSchema,\n data: z.record(z.string(), z.any()).optional(),\n context: z.record(z.string(), z.any()).optional(),\n});\n\nexport type DSLRendererProps = z.infer<typeof DSLRendererPropsSchema>;\n","import { z } from 'zod';\n\n// ==================== Report DSL Schemas ====================\n\n// Expression schema for dynamic values\nexport const ExpressionSchema = z.object({\n $exp: z.string(),\n $deps: z.array(z.string()).optional(),\n});\n\nexport type Expression = z.infer<typeof ExpressionSchema>;\n\n// Binding schema for data binding\nexport const BindingSchema = z.object({\n $bind: z.string(),\n $transform: z\n .array(\n z.object({\n name: z.string(),\n args: z.array(z.any()).optional(),\n }),\n )\n .optional(),\n});\n\nexport type Binding = z.infer<typeof BindingSchema>;\n\n// For directive schema\nexport const ForDirectiveSchema = z.object({\n in: z.union([ExpressionSchema, BindingSchema, z.string()]),\n as: z.string(),\n key: z.string().optional(),\n index: z.string().optional(),\n});\n\nexport type ForDirective = z.infer<typeof ForDirectiveSchema>;\n\n// Query specification schema\nexport const QuerySpecSchema = z.object({\n graphql: z.string().optional(),\n sql: z.string().optional(),\n variables: z.record(z.string(), z.any()).optional(),\n params: z.record(z.string(), z.any()).optional(),\n key: z.string().optional(),\n refetchPolicy: z\n .enum(['cache-first', 'network-only', 'cache-and-network'])\n .optional(),\n dependencies: z.array(z.string()).optional(),\n});\n\nexport type QuerySpec = z.infer<typeof QuerySpecSchema>;\n\n// UI Element schema\nexport const UIElementSchema: z.ZodType<any> = z.lazy(() =>\n z.object({\n id: z.string(),\n type: z.string(),\n key: z.union([z.string(), ExpressionSchema]).optional(),\n props: z.record(z.string(), z.any()).optional(),\n query: QuerySpecSchema.optional(),\n if: ExpressionSchema.optional(),\n elseIf: ExpressionSchema.optional(),\n for: ForDirectiveSchema.optional(),\n 'link-to': z\n .union([\n z.string(),\n ExpressionSchema,\n BindingSchema,\n z.object({\n ui: z.union([z.string(), ExpressionSchema, BindingSchema]),\n params: z.record(z.string(), z.any()).optional(),\n }),\n ])\n .optional(),\n _meta: z\n .object({\n id: z.string().optional(),\n version: z.string().optional(),\n created: z.string().optional(),\n lastModified: z.string().optional(),\n })\n .optional(),\n children: z.any().optional(),\n else: UIElementSchema.optional(),\n slots: z\n .record(z.string(), z.union([UIElementSchema, z.array(UIElementSchema)]))\n .optional(),\n platform: z\n .object({\n web: z.any().optional(),\n ios: z.any().optional(),\n android: z.any().optional(),\n })\n .optional(),\n })\n);\n\nexport type UIElement = z.infer<typeof UIElementSchema>;\n\n// UI Component schema\nexport const UIComponentSchema = z.object({\n id: z.string(),\n name: z.string().optional(),\n props: z.record(z.string(), z.any()).optional(),\n states: z.record(z.string(), z.any()).optional(),\n methods: z\n .record(\n z.string(),\n z.object({\n fn: z.string(),\n params: z.record(z.string(), z.any()).optional(),\n }),\n )\n .optional(),\n effects: z\n .array(\n z.object({\n fn: z.string(),\n deps: z.array(z.string()).optional(),\n }),\n )\n .optional(),\n data: z.record(z.string(), z.any()).optional(),\n render: UIElementSchema,\n query: QuerySpecSchema.optional(),\n});\n\nexport type UIComponent = z.infer<typeof UIComponentSchema>;\n\n// DSL Renderer Props schema\nexport const DSLRendererPropsSchema = z.object({\n dsl: UIComponentSchema,\n data: z.record(z.string(), z.any()).optional(),\n context: z.record(z.string(), z.any()).optional(),\n});\n\nexport type DSLRendererProps = z.infer<typeof DSLRendererPropsSchema>;\n","const PREFIX = '[SuperatomSDK]';\n\nexport const logger = {\n info: (...args: any[]) => {\n console.log(PREFIX, ...args);\n },\n\n error: (...args: any[]) => {\n console.error(PREFIX, ...args);\n },\n\n warn: (...args: any[]) => {\n console.warn(PREFIX, ...args);\n },\n\n debug: (...args: any[]) => {\n console.log(PREFIX, '[DEBUG]', ...args);\n },\n};\n","import { randomUUID } from 'crypto';\nimport { logger } from '../utils/logger';\nimport { STORAGE_CONFIG } from '../config/storage';\nimport { Action } from './action';\n\n/**\n * UIBlock represents a single user and assistant message block in a thread\n * Contains user question, component metadata, component data, and available actions\n */\nexport class UIBlock {\n private id: string;\n private userQuestion: string;\n private generatedComponentMetadata: Record<string, any>;\n private componentData: Record<string, any>;\n private actions: Action[] | null | Promise<Action[]>;\n private createdAt: Date;\n\n /**\n * Creates a new UIBlock instance\n * @param userQuestion - The user's question or input\n * @param componentData - The component data object\n * @param generatedComponentMetadata - Optional metadata about the generated component\n * @param actions - Optional array of available actions\n * @param id - Optional custom ID, generates UUID if not provided\n */\n constructor(\n userQuestion: string,\n componentData: Record<string, any> = {},\n generatedComponentMetadata: Record<string, any> = {},\n actions: Action[] = [],\n id?: string\n ) {\n this.id = id || randomUUID();\n this.userQuestion = userQuestion;\n this.componentData = componentData;\n this.generatedComponentMetadata = generatedComponentMetadata;\n this.actions = actions;\n this.createdAt = new Date();\n }\n\n /**\n * Get the UIBlock ID\n */\n getId(): string {\n return this.id;\n }\n\n /**\n * Get the user question\n */\n getUserQuestion(): string {\n return this.userQuestion;\n }\n\n /**\n * Set or update the user question\n */\n setUserQuestion(question: string): void {\n this.userQuestion = question;\n }\n\n /**\n * Get component metadata\n */\n getComponentMetadata(): Record<string, any> {\n return this.generatedComponentMetadata;\n }\n\n /**\n * Set or update component metadata\n */\n setComponentMetadata(metadata: Record<string, any>): void {\n this.generatedComponentMetadata = { ...this.generatedComponentMetadata, ...metadata };\n }\n\n /**\n * Get component data\n */\n getComponentData(): Record<string, any> {\n return this.componentData;\n }\n\n /**\n * Calculate size of data in bytes\n */\n private getDataSizeInBytes(data: any): number {\n try {\n const jsonString = JSON.stringify(data);\n return Buffer.byteLength(jsonString, 'utf8');\n } catch (error) {\n logger.error('Error calculating data size:', error);\n return 0;\n }\n }\n\n /**\n * Limit array data to maximum rows\n */\n private limitArrayData(data: any[]): { data: any[]; metadata: any } {\n const totalRows = data.length;\n const limitedData = data.slice(0, STORAGE_CONFIG.MAX_ROWS_PER_BLOCK);\n\n return {\n data: limitedData,\n metadata: {\n totalRows,\n storedRows: limitedData.length,\n isTruncated: totalRows > STORAGE_CONFIG.MAX_ROWS_PER_BLOCK,\n },\n };\n }\n\n /**\n * Check if data exceeds size limit\n */\n private exceedsSizeLimit(data: any): boolean {\n const size = this.getDataSizeInBytes(data);\n return size > STORAGE_CONFIG.MAX_SIZE_PER_BLOCK_BYTES;\n }\n\n /**\n * Process and limit data before storing\n */\n private processDataForStorage(data: any): any {\n // If data is an array, limit rows\n if (Array.isArray(data)) {\n const { data: limitedData, metadata } = this.limitArrayData(data);\n\n // Check size after limiting rows\n const size = this.getDataSizeInBytes(limitedData);\n\n logger.info(\n `UIBlock ${this.id}: Storing ${metadata.storedRows}/${metadata.totalRows} rows (${(size / 1024).toFixed(2)} KB)`\n );\n\n // If still too large, store only metadata\n if (this.exceedsSizeLimit(limitedData)) {\n logger.warn(\n `UIBlock ${this.id}: Data too large (${(size / 1024 / 1024).toFixed(2)} MB), storing metadata only`\n );\n return {\n ...metadata,\n preview: limitedData.slice(0, 3), // Store only first 3 rows as preview\n dataTooLarge: true,\n };\n }\n\n return {\n data: limitedData,\n ...metadata,\n };\n }\n\n // For non-array data, check size\n const size = this.getDataSizeInBytes(data);\n\n if (this.exceedsSizeLimit(data)) {\n logger.warn(\n `UIBlock ${this.id}: Data too large (${(size / 1024 / 1024).toFixed(2)} MB), storing summary only`\n );\n return {\n dataTooLarge: true,\n dataType: typeof data,\n keys: typeof data === 'object' ? Object.keys(data) : undefined,\n };\n }\n\n return data;\n }\n\n /**\n * Set or update component data with size and row limits\n */\n setComponentData(data: Record<string, any>): void {\n const processedData = this.processDataForStorage(data);\n this.componentData = { ...this.componentData, ...processedData };\n }\n\n /**\n * Get all actions (only if they are resolved, not if fetching)\n */\n getActions(): Action[] | null | Promise<Action[]> {\n return this.actions;\n }\n\n /**\n * Get or fetch actions\n * If actions don't exist or are a Promise, calls the generateFn and stores the promise\n * If actions already exist, returns them\n * @param generateFn - Async function to generate actions\n * @returns Promise resolving to Action[]\n */\n async getOrFetchActions(generateFn: () => Promise<Action[]>): Promise<Action[]> {\n // If actions already exist and are not a Promise, return them\n\n if (this.actions && !(this.actions instanceof Promise) && Array.isArray(this.actions) && this.actions.length > 0) {\n return this.actions;\n }\n\n // If already fetching, cancel and start new fetch\n // Set new promise for fetching\n const fetchPromise = generateFn();\n this.actions = fetchPromise;\n\n try {\n // Wait for the promise to resolve\n const resolvedActions = await fetchPromise;\n // Store the resolved actions\n logger.info(`Fetched ${resolvedActions.length} actions for UIBlock: ${this.id}`);\n this.actions = resolvedActions;\n return resolvedActions;\n } catch (error) {\n // If generation fails, reset to null\n this.actions = null;\n throw error;\n }\n }\n\n /**\n * Add a single action (only if actions are resolved)\n */\n addAction(action: Action): void {\n if (this.actions && Array.isArray(this.actions)) {\n this.actions.push(action);\n }\n }\n\n /**\n * Add multiple actions (only if actions are resolved)\n */\n addActions(actions: Action[]): void {\n if (this.actions && Array.isArray(this.actions)) {\n this.actions.push(...actions);\n }\n }\n\n /**\n * Remove an action by ID (only if actions are resolved)\n */\n removeAction(actionId: string): boolean {\n if (this.actions && Array.isArray(this.actions)) {\n const index = this.actions.findIndex(a => a.id === actionId);\n if (index > -1) {\n this.actions.splice(index, 1);\n return true;\n }\n }\n return false;\n }\n\n /**\n * Clear all actions\n */\n clearActions(): void {\n this.actions = null;\n }\n\n /**\n * Get creation timestamp\n */\n getCreatedAt(): Date {\n return this.createdAt;\n }\n\n /**\n * Convert UIBlock to JSON-serializable object\n */\n toJSON(): Record<string, any> {\n // Handle Promise case for serialization\n let actionsValue: Action[] | null = null;\n if (this.actions && !(this.actions instanceof Promise) && Array.isArray(this.actions)) {\n actionsValue = this.actions;\n }\n\n return {\n id: this.id,\n userQuestion: this.userQuestion,\n generatedComponentMetadata: this.generatedComponentMetadata,\n componentData: this.componentData,\n actions: actionsValue,\n isFetchingActions: this.actions instanceof Promise,\n createdAt: this.createdAt.toISOString(),\n };\n }\n}\n","/**\n * Configuration for data storage limits in UIBlocks\n */\nexport const STORAGE_CONFIG = {\n /**\n * Maximum number of rows to store in UIBlock data\n */\n MAX_ROWS_PER_BLOCK: 10,\n\n /**\n * Maximum size in bytes per UIBlock (1MB)\n */\n MAX_SIZE_PER_BLOCK_BYTES: 1 * 1024 * 1024, // 1MB\n\n /**\n * Number of days to keep threads before cleanup\n */\n THREAD_RETENTION_DAYS: 7,\n\n /**\n * Number of days to keep UIBlocks before cleanup\n */\n UIBLOCK_RETENTION_DAYS: 7,\n};\n","import { randomUUID } from 'crypto';\nimport { UIBlock } from './uiblock';\n\n/**\n * Thread represents a conversation thread containing multiple UIBlocks\n * Each UIBlock in a thread represents a user question and assistant response pair\n */\nexport class Thread {\n private id: string;\n private uiblocks: Map<string, UIBlock>;\n private createdAt: Date;\n\n /**\n * Creates a new Thread instance\n * @param id - Optional custom ID, generates UUID if not provided\n */\n constructor(id?: string) {\n this.id = id || randomUUID();\n this.uiblocks = new Map();\n this.createdAt = new Date();\n }\n\n /**\n * Get the thread ID\n */\n getId(): string {\n return this.id;\n }\n\n /**\n * Add a UIBlock to the thread\n */\n addUIBlock(uiblock: UIBlock): void {\n this.uiblocks.set(uiblock.getId(), uiblock);\n }\n\n /**\n * Get a UIBlock by ID\n */\n getUIBlock(id: string): UIBlock | undefined {\n return this.uiblocks.get(id);\n }\n\n /**\n * Get all UIBlocks in the thread\n */\n getUIBlocks(): UIBlock[] {\n return Array.from(this.uiblocks.values());\n }\n\n /**\n * Get UIBlocks as a Map\n */\n getUIBlocksMap(): Map<string, UIBlock> {\n return new Map(this.uiblocks);\n }\n\n /**\n * Remove a UIBlock by ID\n */\n removeUIBlock(id: string): boolean {\n return this.uiblocks.delete(id);\n }\n\n /**\n * Check if UIBlock exists\n */\n hasUIBlock(id: string): boolean {\n return this.uiblocks.has(id);\n }\n\n /**\n * Get number of UIBlocks in the thread\n */\n getUIBlockCount(): number {\n return this.uiblocks.size;\n }\n\n /**\n * Clear all UIBlocks from the thread\n */\n clear(): void {\n this.uiblocks.clear();\n }\n\n /**\n * Get creation timestamp\n */\n getCreatedAt(): Date {\n return this.createdAt;\n }\n\n /**\n * Get conversation context from recent UIBlocks (excluding current one)\n * Returns formatted string with previous questions and component summaries\n * @param limit - Maximum number of previous UIBlocks to include (default: 5)\n * @param currentUIBlockId - ID of current UIBlock to exclude from context (optional)\n * @returns Formatted conversation history string\n */\n getConversationContext(limit: number = 5, currentUIBlockId?: string): string {\n if (limit === 0) {\n return '';\n }\n\n // Get all UIBlocks sorted by creation time (oldest first)\n const allBlocks = Array.from(this.uiblocks.values())\n .filter(block => !currentUIBlockId || block.getId() !== currentUIBlockId)\n .sort((a, b) => a.getCreatedAt().getTime() - b.getCreatedAt().getTime());\n\n if (allBlocks.length === 0) {\n return '';\n }\n\n // Take the last N blocks (most recent)\n const recentBlocks = allBlocks.slice(-limit);\n\n // Format as conversation history\n const contextLines: string[] = [];\n\n recentBlocks.forEach((block, index) => {\n const questionNum = index + 1;\n const question = block.getUserQuestion();\n const metadata = block.getComponentMetadata();\n\n // Build component summary\n let componentSummary = 'No component generated';\n if (metadata && Object.keys(metadata).length > 0) {\n const parts: string[] = [];\n\n if (metadata.type) {\n parts.push(`Component Type: ${metadata.type}`);\n }\n if (metadata.name) {\n parts.push(`Name: ${metadata.name}`);\n }\n if (metadata.props?.title) {\n parts.push(`Title: \"${metadata.props.title}\"`);\n }\n if (metadata.props?.query) {\n // Truncate long queries\n const query = metadata.props.query;\n const truncatedQuery = query.length > 200 ? query.substring(0, 200) + '...' : query;\n parts.push(`Query: ${truncatedQuery}`);\n }\n if (metadata.props?.config?.components && Array.isArray(metadata.props.config.components)) {\n // Multi-component container\n const componentTypes = metadata.props.config.components.map((c: any) => c.type).join(', ');\n parts.push(`Multi-component with: ${componentTypes}`);\n }\n\n componentSummary = parts.join(', ');\n }\n\n contextLines.push(`Q${questionNum}: ${question}`);\n contextLines.push(`A${questionNum}: ${componentSummary}`);\n contextLines.push(''); // Empty line for readability\n });\n\n return contextLines.join('\\n').trim();\n }\n\n /**\n * Convert Thread to JSON-serializable object\n */\n toJSON(): Record<string, any> {\n return {\n id: this.id,\n uiblocks: Array.from(this.uiblocks.values()).map(block => block.toJSON()),\n createdAt: this.createdAt.toISOString(),\n };\n }\n}\n","import { Thread } from './thread';\nimport { UIBlock } from './uiblock';\n\n/**\n * ThreadManager manages all threads globally\n * Provides methods to create, retrieve, and delete threads\n */\nexport class ThreadManager {\n private static instance: ThreadManager;\n private threads: Map<string, Thread>;\n\n private constructor() {\n this.threads = new Map();\n\n // Initialize cleanup service\n // new CleanupService(this.threads);\n }\n\n /**\n * Get singleton instance of ThreadManager\n */\n static getInstance(): ThreadManager {\n if (!ThreadManager.instance) {\n ThreadManager.instance = new ThreadManager();\n }\n return ThreadManager.instance;\n }\n\n /**\n * Create a new thread\n * @param id - Optional custom ID, generates UUID if not provided\n * @returns The created Thread instance\n */\n createThread(id?: string): Thread {\n const thread = new Thread(id);\n this.threads.set(thread.getId(), thread);\n return thread;\n }\n\n /**\n * Get a thread by ID\n */\n getThread(id: string): Thread | undefined {\n return this.threads.get(id);\n }\n\n /**\n * Get all threads\n */\n getAllThreads(): Thread[] {\n return Array.from(this.threads.values());\n }\n\n /**\n * Get threads as a Map\n */\n getThreadsMap(): Map<string, Thread> {\n return new Map(this.threads);\n }\n\n /**\n * Delete a thread by ID\n */\n deleteThread(id: string): boolean {\n return this.threads.delete(id);\n }\n\n /**\n * Check if thread exists\n */\n hasThread(id: string): boolean {\n return this.threads.has(id);\n }\n\n /**\n * Get number of threads\n */\n getThreadCount(): number {\n return this.threads.size;\n }\n\n /**\n * Clear all threads\n */\n clearAll(): void {\n this.threads.clear();\n }\n\n /**\n * Find a UIBlock by ID across all threads\n * @param uiBlockId - The UIBlock ID to search for\n * @returns Object with thread and uiBlock if found, undefined otherwise\n */\n findUIBlockById(uiBlockId: string): { thread: Thread; uiBlock: UIBlock } | undefined {\n for (const thread of this.threads.values()) {\n const uiBlock = thread.getUIBlock(uiBlockId);\n if (uiBlock) {\n return { thread, uiBlock };\n }\n }\n return undefined;\n }\n\n /**\n * Convert all threads to JSON-serializable object\n */\n toJSON(): Record<string, any> {\n return {\n threads: Array.from(this.threads.values()).map(thread => thread.toJSON()),\n count: this.threads.size,\n };\n }\n}\n","import { DataRequestMessageSchema, type CollectionRegistry, type Message } from '../types';\nimport { logger } from '../utils/logger';\nimport { ThreadManager } from '../threads';\n\n/**\n * Handle incoming data_req messages and execute collection handlers\n */\nexport async function handleDataRequest(\n data: any,\n collections: CollectionRegistry,\n sendMessage: (message: Message) => void\n): Promise<void> {\n try {\n const dataRequest = DataRequestMessageSchema.parse(data);\n const { id, payload } = dataRequest;\n const { collection, op, params, SA_RUNTIME } = payload;\n\n // Check if collection and operation exist\n if (!collections[collection]) {\n sendDataResponse(id, collection, op, null, {\n error: `Collection '${collection}' not found`,\n }, sendMessage);\n return;\n }\n\n if (!collections[collection][op]) {\n sendDataResponse(id, collection, op, null, {\n error: `Operation '${op}' not found for collection '${collection}'`,\n }, sendMessage);\n return;\n }\n\n // Execute the handler and measure execution time\n const startTime = performance.now();\n const handler = collections[collection][op];\n const result = await handler(params || {});\n const executionMs = Math.round(performance.now() - startTime);\n\n logger.info(`Executed ${collection}.${op} in ${executionMs}ms`);\n\n // Update UIBlock with component data if SA_RUNTIME has uiBlockId\n if (SA_RUNTIME && typeof SA_RUNTIME === 'object' && 'uiBlockId' in SA_RUNTIME) {\n const uiBlockId = (SA_RUNTIME as any).uiBlockId;\n const threadId = (SA_RUNTIME as any).threadId;\n\n const threadManager = ThreadManager.getInstance();\n let uiBlock = null;\n let thread = null;\n\n // If threadId is provided, get the specific thread\n if (threadId) {\n thread = threadManager.getThread(threadId);\n if (thread) {\n uiBlock = thread.getUIBlock(uiBlockId);\n }\n } else {\n // Otherwise search across all threads\n const result = threadManager.findUIBlockById(uiBlockId);\n if (result) {\n thread = result.thread;\n uiBlock = result.uiBlock;\n }\n }\n\n // Update UIBlock's componentData with the response\n if (uiBlock) {\n uiBlock.setComponentData(result || {});\n logger.info(`Updated UIBlock ${uiBlockId} with component data from ${collection}.${op}`);\n } else {\n logger.warn(`UIBlock ${uiBlockId} not found in threads`);\n }\n }\n\n // Send response\n sendDataResponse(id, collection, op, result, { executionMs }, sendMessage);\n } catch (error) {\n logger.error('Failed to handle data request:', error);\n // Not a data_req message or invalid format\n }\n}\n\n/**\n * Send a data_res response message\n */\nfunction sendDataResponse(\n id: string,\n collection: string,\n op: string,\n data: any,\n meta: { executionMs?: number; error?: string },\n sendMessage: (message: Message) => void\n): void {\n const response: Message = {\n id,\n from: { type: 'data-agent' },\n type: 'DATA_RES',\n payload: {\n collection,\n op,\n data,\n ...meta,\n },\n };\n\n sendMessage(response);\n}\n","import * as fs from 'fs';\nimport * as path from 'path';\nimport { logger } from './utils/logger';\n\n/**\n * Get the bundle directory from config or environment variable\n */\nexport function getBundleDir(configDir?: string): string {\n const bundleDir = configDir || process.env.SA_BUNDLE_DIR;\n\n if (!bundleDir) {\n throw new Error(\n 'Bundle directory not configured. Please provide bundleDir in config or set SA_BUNDLE_DIR environment variable.'\n );\n }\n\n return bundleDir;\n}\n\n/**\n * Load the JavaScript bundle from the configured directory\n */\nexport function getJS(bundleDir: string): string {\n try {\n // Check if directory exists\n if (!fs.existsSync(bundleDir)) {\n throw new Error(`Bundle directory does not exist: ${bundleDir}`);\n }\n\n // Check if it's actually a directory\n const stats = fs.statSync(bundleDir);\n if (!stats.isDirectory()) {\n throw new Error(`Bundle path is not a directory: ${bundleDir}`);\n }\n\n // Read directory contents\n let files: string[];\n try {\n files = fs.readdirSync(bundleDir);\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n throw new Error(`Failed to read bundle directory: ${errorMessage}`);\n }\n\n // Find the bundle file\n const indexFile = files.find((file) => file.startsWith('index-') && file.endsWith('.js'));\n\n if (!indexFile) {\n logger.warn(`Available files in ${bundleDir}:`, files);\n throw new Error(\n `Could not find index-*.js file in ${bundleDir}. ` +\n `Expected a file matching pattern: index-*.js`\n );\n }\n\n // Read the bundle file\n const filePath = path.join(bundleDir, indexFile);\n logger.info(`Loading bundle from ${filePath}`);\n\n try {\n return fs.readFileSync(filePath, 'utf8');\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n throw new Error(`Failed to read bundle file: ${errorMessage}`);\n }\n } catch (error) {\n if (error instanceof Error) {\n logger.error('Failed to load bundle:', error.message);\n } else {\n logger.error('Failed to load bundle:', error);\n }\n throw error;\n }\n}\n","import { getJS, getBundleDir } from '../bundle';\nimport { logger } from '../utils/logger';\nimport type { Message } from '../types';\n\nconst CHUNK_SIZE = 900 * 1024; // 900 KB chunks (leaving room for metadata, max 1MB per message)\n\n/**\n * Handle incoming bundle_req messages and send chunked bundle response\n */\nexport async function handleBundleRequest(\n data: any,\n bundleDir: string | undefined,\n sendMessage: (message: Message) => void\n): Promise<void> {\n try {\n const id = data.id || 'unknown';\n const fromId = data.from?.id;\n\n // Get bundle directory and load bundle\n const dir = getBundleDir(bundleDir);\n const js = getJS(dir);\n const bundleSize = Buffer.byteLength(js, 'utf8');\n\n logger.info(`Bundle size: ${(bundleSize / 1024).toFixed(2)} KB`);\n\n // Split bundle into chunks\n const totalChunks = Math.ceil(bundleSize / CHUNK_SIZE);\n logger.info(`Splitting bundle into ${totalChunks} chunks`);\n\n for (let i = 0; i < totalChunks; i++) {\n const start = i * CHUNK_SIZE;\n const end = Math.min(start + CHUNK_SIZE, js.length);\n const chunk = js.substring(start, end);\n const isComplete = i === totalChunks - 1;\n const progress = ((i + 1) / totalChunks) * 100;\n\n const chunkMessage: Message = {\n id: `${id}-chunk-${i}`,\n type: 'BUNDLE_CHUNK',\n from: { type: 'data-agent' },\n to: fromId ? { id: fromId } : undefined,\n payload: {\n chunk: chunk,\n chunkIndex: i,\n totalChunks: totalChunks,\n isComplete: isComplete,\n progress: parseFloat(progress.toFixed(2)),\n },\n };\n\n sendMessage(chunkMessage);\n logger.debug(`Sent chunk ${i + 1}/${totalChunks} (${progress.toFixed(2)}%)`);\n }\n\n logger.info('Bundle sending complete');\n } catch (error) {\n logger.error('Failed to handle bundle request:', error);\n\n // Send error response\n const errorMessage: Message = {\n id: data.id || 'unknown',\n type: 'BUNDLE_RES',\n from: { type: 'data-agent' },\n to: data.from?.id ? { id: data.from.id } : undefined,\n payload: {\n error: error instanceof Error ? error.message : 'Unknown error',\n },\n };\n\n sendMessage(errorMessage);\n }\n}\n","import crypto from 'crypto';\n\n/**\n * Decode base64 encoded string and parse JSON\n * @param base64Data - Base64 encoded string\n * @returns Parsed JSON object\n */\nexport function decodeBase64ToJson(base64Data: string): any {\n try {\n const decodedString = Buffer.from(base64Data, 'base64').toString('utf-8');\n return JSON.parse(decodedString);\n } catch (error) {\n throw new Error(`Failed to decode base64 data: ${error instanceof Error ? error.message : 'Unknown error'}`);\n }\n}\n\n/**\n * Hash password using SHA1\n * @param password - Plain text password\n * @returns SHA1 hashed password\n */\nexport function hashPassword(password: string): string {\n return crypto.createHash('sha1').update(password).digest('hex');\n}\n","import { UserManager, type User, type UsersData } from './user-manager';\nimport { logger } from '../utils/logger';\n\n// Global reference to the current SDK's UserManager instance\nlet currentUserManager: UserManager | null = null;\n\n/**\n * Set the UserManager instance (called by SuperatomSDK during initialization)\n * This should be called with the SDK's UserManager instance\n * @param userManager - UserManager instance from SuperatomSDK\n */\nexport function setUserManager(userManager: UserManager): void {\n if (!userManager) {\n throw new Error('userManager cannot be null');\n }\n currentUserManager = userManager;\n logger.debug('UserManager instance set');\n}\n\n/**\n * Get the current UserManager instance\n */\nexport function getUserManager(): UserManager {\n if (!currentUserManager) {\n throw new Error(\n 'UserManager not initialized. Make sure SuperatomSDK is initialized before using user storage.'\n );\n }\n return currentUserManager;\n}\n\n/**\n * Load users from memory (UserManager maintains the cache)\n * @returns UsersData object containing all users\n */\nexport function loadUsers(): UsersData {\n const manager = getUserManager();\n return {\n users: manager.getAllUsers()\n };\n}\n\n/**\n * Save users to file immediately (forces sync)\n * @param usersData - UsersData object to save\n */\nexport async function saveUsers(usersData: UsersData): Promise<void> {\n const manager = getUserManager();\n\n // Clear existing users and repopulate\n manager.deleteAllUsers();\n\n // Add all users from the provided data\n for (const user of usersData.users) {\n try {\n manager.createUser(user);\n } catch (error) {\n // User might already exist, update instead\n if (manager.userExists(user.username)) {\n manager.updateUser(user.username, user);\n }\n }\n }\n\n // Force immediate sync to file\n await manager.forceSync();\n}\n\n/**\n * Find a user by username\n * @param username - Username to search for\n * @returns User object if found, null otherwise\n */\nexport function findUserByUsername(username: string): User | null {\n const manager = getUserManager();\n const user = manager.getUser(username);\n return user || null;\n}\n\n/**\n * Add WebSocket ID to a user's wsIds array\n * @param username - Username to update\n * @param wsId - WebSocket ID to add\n * @returns true if successful, false otherwise\n */\nexport function addWsIdToUser(username: string, wsId: string): boolean {\n try {\n const manager = getUserManager();\n return manager.addWsId(username, wsId);\n } catch (error) {\n logger.error('Error adding WebSocket ID:', error);\n return false;\n }\n}\n\n/**\n * Remove WebSocket ID from a user's wsIds array\n * @param username - Username to update\n * @param wsId - WebSocket ID to remove\n * @returns true if successful, false otherwise\n */\nexport function removeWsIdFromUser(username: string, wsId: string): boolean {\n try {\n const manager = getUserManager();\n return manager.removeWsId(username, wsId);\n } catch (error) {\n logger.error('Error removing WebSocket ID:', error);\n return false;\n }\n}\n\n/**\n * Cleanup and destroy the UserManager\n */\nexport async function cleanupUserStorage(): Promise<void> {\n if (currentUserManager) {\n await currentUserManager.destroy();\n currentUserManager = null;\n logger.info('UserManager cleaned up');\n }\n}\n\n// Export types\nexport type { User, UsersData } from './user-manager';\n","import { findUserByUsername, addWsIdToUser } from \"./user-storage\";\nimport { hashPassword } from \"./utils\";\nimport { logger } from \"../utils/logger\";\n\nexport interface LoginCredentials {\n username: string;\n password: string;\n}\n\nexport interface ValidationResult {\n success: boolean;\n error?: string;\n data?: any;\n username?: string;\n}\n\n/**\n * Validate user credentials against stored user data\n * @param credentials - Login credentials with username and password\n * @returns ValidationResult indicating success or failure\n */\nexport function validateUser(credentials: LoginCredentials): ValidationResult {\n const { username, password } = credentials;\n\n // Check if username and password are provided\n if (!username || !password) {\n logger.warn('Validation failed: Username and password are required');\n return {\n success: false,\n error: 'Username and password are required'\n };\n }\n\n // Find user by username from in-memory cache\n const user = findUserByUsername(username);\n\n if (!user) {\n logger.warn(`Validation failed: User not found - ${username}`);\n return {\n success: false,\n error: 'Invalid username'\n };\n }\n\n // Hash the stored password and compare with provided password\n const hashedPassword = hashPassword(user.password);\n\n if (hashedPassword !== password) {\n logger.warn(`Validation failed: Invalid password for user - ${username}`);\n return {\n success: false,\n error: 'Invalid password'\n };\n }\n\n logger.debug(`User validated successfully: ${username}`);\n return {\n success: true,\n data: user.username\n };\n}\n\n/**\n * Authenticate user and store WebSocket ID\n * Uses UserManager's in-memory cache with automatic file sync\n * @param credentials - Login credentials\n * @param wsId - WebSocket ID to store\n * @returns ValidationResult with authentication status\n */\nexport function authenticateAndStoreWsId(credentials: LoginCredentials, wsId: string): ValidationResult {\n const validationResult = validateUser(credentials);\n\n if (!validationResult.success) {\n return validationResult;\n }\n\n // Store wsId in user's wsIds array using UserManager\n // Changes are automatically synced to file via setInterval\n const stored = addWsIdToUser(credentials.username, wsId);\n\n if (!stored) {\n logger.error(`Failed to store WebSocket ID for user: ${credentials.username}`);\n return {\n success: false,\n error: 'Failed to store user session'\n };\n }\n\n logger.info(`WebSocket ID stored for user: ${credentials.username}`);\n return validationResult;\n}\n\n/**\n * Verify authentication token\n * @param authToken - Base64 encoded auth token containing username and password\n * @returns ValidationResult indicating if token is valid\n */\nexport function verifyAuthToken(authToken: string): ValidationResult {\n try {\n // Decode base64 token\n const decodedString = Buffer.from(authToken, 'base64').toString('utf-8');\n const credentials = JSON.parse(decodedString);\n\n logger.debug('Token decoded and parsed successfully');\n // Validate credentials\n return validateUser(credentials);\n } catch (error) {\n logger.error('Failed to verify auth token:', error);\n return {\n success: false,\n error: 'Invalid token format'\n };\n }\n}\n","import { decodeBase64ToJson } from \"../auth/utils\";\nimport { authenticateAndStoreWsId } from \"../auth/validator\";\nimport { AuthLoginRequestMessageSchema, Message } from \"../types\";\nimport { logger } from \"../utils/logger\";\n\n\n\nexport async function handleAuthLoginRequest(\n data: any,\n sendMessage: (message: Message) => void\n): Promise<void> {\n try {\n const authRequest = AuthLoginRequestMessageSchema.parse(data);\n const { id, payload } = authRequest;\n \n const login_data = payload.login_data;\n\n const wsId = authRequest.from.id ;\n\n if(!login_data){\n sendDataResponse(id, {\n success: false,\n error: 'Login data not found'\n }, sendMessage, wsId);\n return;\n }\n\n\n // Decode base64 data and parse JSON\n let loginData: any;\n try {\n loginData = decodeBase64ToJson(login_data);\n } catch (error) {\n sendDataResponse(id, {\n success: false,\n error: 'Invalid login data format'\n }, sendMessage, wsId);\n return;\n }\n\n // Extract username and password from decoded data\n const { username, password } = loginData;\n\n if (!username) {\n sendDataResponse(id, {\n success: false,\n error: 'Username not found in login data'\n }, sendMessage, wsId);\n return;\n }\n\n if (!password) {\n sendDataResponse(id, {\n success: false,\n error: 'Password not found in login data'\n }, sendMessage, wsId);\n return;\n }\n\n\n if(!wsId){\n sendDataResponse(id, {\n success: false,\n error: 'WebSocket ID not found'\n }, sendMessage, wsId);\n return;\n }\n\n // Authenticate user and store wsId\n const authResult = authenticateAndStoreWsId(\n { username, password },\n wsId\n );\n\n // Send response\n\n sendDataResponse(id, authResult, sendMessage, wsId);\n return ;\n }\n catch (error) {\n logger.error('Failed to handle auth login request:', error);\n }\n}\n\n\n/**\n * Send a data_res response message\n */\nfunction sendDataResponse(\n id: string,\n res: {success: boolean; error?: string; data?: any},\n sendMessage: (message: Message) => void,\n clientId?: string,\n): void {\n const response: Message = {\n id,\n type: 'AUTH_LOGIN_RES',\n from: { type: 'data-agent' },\n to: {\n type: 'runtime',\n id: clientId\n },\n payload:{\n ...res,\n }\n };\n\n sendMessage(response);\n}\n\n","import { verifyAuthToken } from \"../auth/validator\";\nimport { AuthVerifyRequestMessageSchema, Message } from \"../types\";\nimport { logger } from \"../utils/logger\";\n\n\nexport async function handleAuthVerifyRequest(\n data: any,\n sendMessage: (message: Message) => void\n): Promise<void> {\n try {\n const authRequest = AuthVerifyRequestMessageSchema.parse(data);\n const { id, payload } = authRequest;\n \n const token = payload.token;\n\n const wsId = authRequest.from.id ;\n\n if(!token){\n sendDataResponse(id, {\n success: false,\n error: 'Token not found'\n }, sendMessage, wsId);\n return;\n }\n\n\n if(!wsId){\n sendDataResponse(id, {\n success: false,\n error: 'WebSocket ID not found'\n }, sendMessage, wsId);\n return;\n }\n\n // Verify token\n const authResult = verifyAuthToken(token);\n\n // Send response\n sendDataResponse(id, authResult, sendMessage, wsId);\n return ;\n }\n catch (error) {\n logger.error('Failed to handle auth verify request:', error);\n }\n}\n\n/**\n * Send a data_res response message\n */\nfunction sendDataResponse(\n id: string,\n res: {success: boolean; error?: string; data?: any},\n sendMessage: (message: Message) => void,\n clientId?: string,\n): void { \n const response: Message = {\n id,\n type: 'AUTH_VERIFY_RES',\n from: { type: 'data-agent' },\n to: {\n type: 'runtime',\n id: clientId\n },\n payload:{\n ...res,\n }\n };\n\n sendMessage(response);\n} ","import dotenv from 'dotenv';\nimport { BaseLLM, BaseLLMConfig } from './base-llm';\n\ndotenv.config();\n\nexport interface GroqLLMConfig extends BaseLLMConfig {}\n\n/**\n * GroqLLM class for handling AI-powered component generation and matching using Groq\n */\nexport class GroqLLM extends BaseLLM {\n\tconstructor(config?: GroqLLMConfig) {\n\t\tsuper(config);\n\t}\n\n\tprotected getDefaultModel(): string {\n\t\treturn 'groq/openai/gpt-oss-120b';\n\t}\n\n\tprotected getDefaultApiKey(): string | undefined {\n\t\treturn process.env.GROQ_API_KEY;\n\t}\n\n\tprotected getProviderName(): string {\n\t\treturn 'Groq';\n\t}\n}\n\n// Export a singleton instance\nexport const groqLLM = new GroqLLM();\n","/**\n * Converts T-SQL TOP syntax to Snowflake LIMIT syntax\n * Snowflake doesn't support TOP keyword - it must use LIMIT\n * @param query - The SQL query to check\n * @returns The query with TOP converted to LIMIT\n */\nexport function convertTopToLimit(query: string): string {\n\tif (!query || query.trim().length === 0) {\n\t\treturn query;\n\t}\n\n\t// Replace \"TOP N\" with nothing (we'll add LIMIT at the end)\n\t// Pattern: SELECT TOP number or SELECT TOP (number)\n\tlet modifiedQuery = query.replace(/\\bSELECT\\s+TOP\\s+(\\d+)\\b/gi, 'SELECT');\n\n\tif (modifiedQuery !== query) {\n\t\tconsole.warn(`⚠️ Query had TOP syntax. Converting to LIMIT for Snowflake compatibility.`);\n\t}\n\n\treturn modifiedQuery;\n}\n\n/**\n * Ensures a SQL query has a LIMIT clause to prevent large result sets\n * Only applies to SELECT queries - leaves INSERT, UPDATE, DELETE, etc. unchanged\n * Also removes any duplicate LIMIT clauses to prevent SQL errors\n * Converts T-SQL TOP syntax to Snowflake LIMIT syntax\n * @param query - The SQL query to check\n * @param defaultLimit - Default limit to apply if none exists (default: 50)\n * @returns The query with a single LIMIT clause (if it's a SELECT query)\n */\nexport function ensureQueryLimit(query: string, defaultLimit: number = 50): string {\n\tif (!query || query.trim().length === 0) {\n\t\treturn query;\n\t}\n\n\tlet trimmedQuery = query.trim();\n\n\t// Only apply LIMIT to SELECT queries\n\t// Check if the query is a SELECT statement (not INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, etc.)\n\tconst isSelectQuery = /^\\s*SELECT\\b/i.test(trimmedQuery) ||\n\t\t/^\\s*WITH\\b.*\\bSELECT\\b/is.test(trimmedQuery); // Also handle CTEs (WITH clause)\n\n\tif (!isSelectQuery) {\n\t\t// Not a SELECT query, return as-is\n\t\treturn query;\n\t}\n\n\t// Step 1: Convert TOP syntax to standard format\n\ttrimmedQuery = convertTopToLimit(trimmedQuery);\n\n\t// Remove any trailing semicolon for processing\n\tconst hadSemicolon = trimmedQuery.endsWith(';');\n\tif (hadSemicolon) {\n\t\ttrimmedQuery = trimmedQuery.slice(0, -1).trim();\n\t}\n\n\t// Remove any duplicate LIMIT clauses (keep only the last one, or remove all to add a fresh one)\n\t// This regex matches LIMIT followed by a number, case-insensitive\n\tconst limitMatches = trimmedQuery.match(/\\bLIMIT\\s+\\d+\\b/gi);\n\n\tif (limitMatches && limitMatches.length > 0) {\n\t\t// If there are multiple LIMIT clauses, remove them all and add a fresh one\n\t\tif (limitMatches.length > 1) {\n\t\t\tconsole.warn(`⚠️ Query had ${limitMatches.length} LIMIT clauses. Removing duplicates...`);\n\t\t\ttrimmedQuery = trimmedQuery.replace(/\\s*\\bLIMIT\\s+\\d+\\b/gi, '').trim();\n\t\t} else {\n\t\t\t// Single LIMIT exists, keep it as-is\n\t\t\tif (hadSemicolon) {\n\t\t\t\ttrimmedQuery += ';';\n\t\t\t}\n\t\t\treturn trimmedQuery;\n\t\t}\n\t}\n\n\t// Add LIMIT clause at the end\n\ttrimmedQuery = `${trimmedQuery} LIMIT ${defaultLimit}`;\n\n\t// Add back the semicolon if it was there\n\tif (hadSemicolon) {\n\t\ttrimmedQuery += ';';\n\t}\n\n\treturn trimmedQuery;\n}\n\n/**\n * Calculates the size of a JSON object in bytes\n * @param obj - The object to measure\n * @returns Size in bytes\n */\nexport function getJsonSizeInBytes(obj: any): number {\n\tconst jsonString = JSON.stringify(obj);\n\treturn Buffer.byteLength(jsonString, 'utf8');\n}\n\n/**\n * Checks if a message exceeds the WebSocket size limit\n * @param message - The message object to check\n * @param maxSize - Maximum size in bytes (default: 1MB)\n * @returns Object with isValid flag and size information\n */\nexport function validateMessageSize(message: any, maxSize: number = 1048576): { isValid: boolean; size: number; maxSize: number } {\n\tconst size = getJsonSizeInBytes(message);\n\treturn {\n\t\tisValid: size <= maxSize,\n\t\tsize,\n\t\tmaxSize\n\t};\n}","import path from 'path';\nimport fs from 'fs';\nimport { logger } from '../utils/logger';\n\n/**\n * Schema class for managing database schema operations\n */\nexport class Schema {\n private schemaFilePath: string;\n private cachedSchema: any = null;\n\n constructor(schemaFilePath?: string) {\n this.schemaFilePath = schemaFilePath || path.join(process.cwd(), '../analysis/data/schema.json');\n }\n\n /**\n * Gets the database schema from the schema file\n * @returns Parsed schema object or null if error occurs\n */\n getDatabaseSchema(): any | null {\n logger.info(`SCHEMA_FILE_PATH: ${this.schemaFilePath}`);\n\n try {\n // Create directory structure if it doesn't exist\n const dir = path.dirname(this.schemaFilePath);\n if (!fs.existsSync(dir)) {\n logger.info(`Creating directory structure: ${dir}`);\n fs.mkdirSync(dir, { recursive: true });\n }\n\n // Create file with empty schema if it doesn't exist\n if (!fs.existsSync(this.schemaFilePath)) {\n logger.info(`Schema file does not exist at ${this.schemaFilePath}, creating with empty schema`);\n const initialSchema = {\n database: '',\n schema: '',\n description: '',\n tables: [],\n relationships: []\n };\n fs.writeFileSync(this.schemaFilePath, JSON.stringify(initialSchema, null, 4));\n this.cachedSchema = initialSchema;\n return initialSchema;\n }\n\n const fileContent = fs.readFileSync(this.schemaFilePath, 'utf-8');\n const schema = JSON.parse(fileContent);\n this.cachedSchema = schema;\n return schema;\n } catch (error) {\n logger.error('Error parsing schema file:', error);\n return null;\n }\n }\n\n /**\n * Gets the cached schema or loads it if not cached\n * @returns Cached schema or freshly loaded schema\n */\n getSchema(): any | null {\n if (this.cachedSchema) {\n return this.cachedSchema;\n }\n return this.getDatabaseSchema();\n }\n\n /**\n * Generates database schema documentation for LLM from Snowflake JSON schema\n * @returns Formatted schema documentation string\n */\n generateSchemaDocumentation(): string {\n const schema = this.getSchema();\n\n if (!schema) {\n logger.warn('No database schema found.');\n return 'No database schema available.';\n }\n\n const tables: string[] = [];\n\n // Header information\n tables.push(`Database: ${schema.database}`);\n tables.push(`Schema: ${schema.schema}`);\n tables.push(`Description: ${schema.description}`);\n tables.push('');\n tables.push('='.repeat(80));\n tables.push('');\n\n // Process each table\n for (const table of schema.tables) {\n const tableInfo: string[] = [];\n\n tableInfo.push(`TABLE: ${table.fullName}`);\n tableInfo.push(`Description: ${table.description}`);\n tableInfo.push(`Row Count: ~${table.rowCount.toLocaleString()}`);\n tableInfo.push('');\n tableInfo.push('Columns:');\n\n // Process columns\n for (const column of table.columns) {\n let columnLine = ` - ${column.name}: ${column.type}`;\n\n if ((column as any).isPrimaryKey) {\n columnLine += ' (PRIMARY KEY)';\n }\n\n if ((column as any).isForeignKey && (column as any).references) {\n columnLine += ` (FK -> ${(column as any).references.table}.${(column as any).references.column})`;\n }\n\n if (!column.nullable) {\n columnLine += ' NOT NULL';\n }\n\n if (column.description) {\n columnLine += ` - ${column.description}`;\n }\n\n tableInfo.push(columnLine);\n\n // Add value examples for categorical columns\n if ((column as any).sampleValues && (column as any).sampleValues.length > 0) {\n tableInfo.push(` Sample values: [${(column as any).sampleValues.join(', ')}]`);\n }\n\n // Add statistics if available\n if ((column as any).statistics) {\n const stats = (column as any).statistics;\n if (stats.min !== undefined && stats.max !== undefined) {\n tableInfo.push(` Range: ${stats.min} to ${stats.max}`);\n }\n if (stats.distinct !== undefined) {\n tableInfo.push(` Distinct values: ${stats.distinct.toLocaleString()}`);\n }\n }\n }\n\n tableInfo.push('');\n tables.push(tableInfo.join('\\n'));\n }\n\n // Add relationships section\n tables.push('='.repeat(80));\n tables.push('');\n tables.push('TABLE RELATIONSHIPS:');\n tables.push('');\n\n for (const rel of schema.relationships) {\n tables.push(`${rel.from} -> ${rel.to} (${rel.type}): ${rel.keys.join(' = ')}`);\n }\n\n return tables.join('\\n');\n }\n\n /**\n * Clears the cached schema, forcing a reload on next access\n */\n clearCache(): void {\n this.cachedSchema = null;\n }\n\n /**\n * Sets a custom schema file path\n * @param filePath - Path to the schema file\n */\n setSchemaPath(filePath: string): void {\n this.schemaFilePath = filePath;\n this.clearCache();\n }\n}\n\n// Export a singleton instance for use across the application\nexport const schema = new Schema();\n","import fs from 'fs';\nimport path from 'path';\nimport { logger } from '../utils/logger';\n\nexport interface PromptLoaderConfig {\n\tpromptsDir?: string;\n}\n\ninterface CachedPromptTemplate {\n\tsystem: string;\n\tuser: string;\n}\n\n/**\n * PromptLoader class for loading and processing prompt templates\n * Caches prompts in memory at initialization for better performance\n */\nexport class PromptLoader {\n\tprivate promptsDir: string;\n\tprivate defaultPromptsDir: string;\n\tprivate promptCache: Map<string, CachedPromptTemplate> = new Map();\n\tprivate isInitialized: boolean = false;\n\n\tconstructor(config?: PromptLoaderConfig) {\n\t\tlogger.debug('Initializing PromptLoader...', process.cwd());\n\t\tthis.promptsDir = config?.promptsDir || path.join(process.cwd(), '.prompts');\n\t\t// Default prompts directory within the SDK\n\t\tthis.defaultPromptsDir = path.join(__dirname, '..', '..', '.prompts');\n\t}\n\n\t/**\n\t * Initialize and cache all prompts into memory\n\t * This should be called once at SDK startup\n\t */\n\tasync initialize(): Promise<void> {\n\t\tif (this.isInitialized) {\n\t\t\tlogger.debug('PromptLoader already initialized, skipping...');\n\t\t\treturn;\n\t\t}\n\n\t\tlogger.info('Loading prompts into memory...');\n\n\t\tconst promptTypes = [\n\t\t\t'classify',\n\t\t\t'match-component',\n\t\t\t'modify-props',\n\t\t\t'single-component',\n\t\t\t'mutli-component',\n\t\t\t'actions',\n\t\t\t'container-metadata'\n\t\t];\n\n\t\tfor (const promptType of promptTypes) {\n\t\t\ttry {\n\t\t\t\tconst template = await this.loadPromptTemplate(promptType);\n\t\t\t\tthis.promptCache.set(promptType, template);\n\t\t\t\tlogger.debug(`Cached prompt: ${promptType}`);\n\t\t\t} catch (error) {\n\t\t\t\tlogger.error(`Failed to load prompt '${promptType}':`, error);\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t}\n\n\t\tthis.isInitialized = true;\n\t\tlogger.info(`Successfully loaded ${this.promptCache.size} prompt templates into memory`);\n\t}\n\n\t/**\n\t * Load a prompt template from file system (tries custom dir first, then defaults to SDK dir)\n\t * @param promptName - Name of the prompt folder\n\t * @returns Template with system and user prompts\n\t */\n\tprivate async loadPromptTemplate(promptName: string): Promise<CachedPromptTemplate> {\n\t\tconst tryLoadFromDir = (dir: string): CachedPromptTemplate | null => {\n\t\t\ttry {\n\t\t\t\tconst systemPath = path.join(dir, promptName, 'system.md');\n\t\t\t\tconst userPath = path.join(dir, promptName, 'user.md');\n\n\t\t\t\tif (fs.existsSync(systemPath) && fs.existsSync(userPath)) {\n\t\t\t\t\tconst system = fs.readFileSync(systemPath, 'utf-8');\n\t\t\t\t\tconst user = fs.readFileSync(userPath, 'utf-8');\n\t\t\t\t\tlogger.debug(`Loaded prompt '${promptName}' from ${dir}`);\n\t\t\t\t\treturn { system, user };\n\t\t\t\t}\n\t\t\t\treturn null;\n\t\t\t} catch (error) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t};\n\t\t// Try loading from custom directory first\n\t\tlet template = tryLoadFromDir(this.promptsDir);\n\n\t\t// If not found, try default SDK directory\n\t\tif (!template) {\n\t\t\tlogger.warn(`Prompt '${promptName}' not found in ${this.promptsDir}, trying default location...`);\n\t\t\ttemplate = tryLoadFromDir(this.defaultPromptsDir);\n\t\t}\n\n\t\tif (!template) {\n\t\t\tthrow new Error(`Prompt template '${promptName}' not found in either ${this.promptsDir} or ${this.defaultPromptsDir}`);\n\t\t}\n\n\t\treturn template;\n\t}\n\n\t/**\n\t * Replace variables in a template string using {{VARIABLE_NAME}} pattern\n\t * @param template - Template string with placeholders\n\t * @param variables - Variables to replace in the template\n\t * @returns Processed string\n\t */\n\tprivate replaceVariables(\n\t\ttemplate: string,\n\t\tvariables: Record<string, string | number | boolean | any[]>\n\t): string {\n\t\tlet content = template;\n\n\t\t// Replace all variables matching {{VARIABLE_NAME}} pattern\n\t\tfor (const [key, value] of Object.entries(variables)) {\n\t\t\tconst pattern = new RegExp(`{{${key}}}`, 'g');\n\t\t\tconst replacementValue = typeof value === 'string' ? value : JSON.stringify(value);\n\t\t\tcontent = content.replace(pattern, replacementValue);\n\t\t}\n\n\t\treturn content;\n\t}\n\n\t/**\n\t * Load both system and user prompts from cache and replace variables\n\t * @param promptName - Name of the prompt folder\n\t * @param variables - Variables to replace in the templates\n\t * @returns Object containing both system and user prompts\n\t */\n\tasync loadPrompts(\n\t\tpromptName: string,\n\t\tvariables: Record<string, string | number | boolean | any[]>\n\t): Promise<{ system: string; user: string }> {\n\t\tif (!this.isInitialized) {\n\t\t\tlogger.warn('PromptLoader not initialized, loading prompts on-demand (not recommended)');\n\t\t\tawait this.initialize();\n\t\t}\n\n\t\tconst template = this.promptCache.get(promptName);\n\n\t\tif (!template) {\n\t\t\tthrow new Error(`Prompt template '${promptName}' not found in cache. Available prompts: ${Array.from(this.promptCache.keys()).join(', ')}`);\n\t\t}\n\n\t\treturn {\n\t\t\tsystem: this.replaceVariables(template.system, variables),\n\t\t\tuser: this.replaceVariables(template.user, variables)\n\t\t};\n\t}\n\n\t/**\n\t * DEPRECATED: Use loadPrompts instead\n\t * Load a single prompt file and replace variables using {{VARIABLE_NAME}} pattern\n\t */\n\tasync loadPrompt(\n\t\tpromptName: string,\n\t\tpromptType: 'system' | 'user',\n\t\tvariables: Record<string, string | number | boolean | any[]>\n\t): Promise<string> {\n\t\tconst prompts = await this.loadPrompts(promptName, variables);\n\t\treturn promptType === 'system' ? prompts.system : prompts.user;\n\t}\n\n\t/**\n\t * Set custom prompts directory (requires re-initialization)\n\t * @param dir - Path to the prompts directory\n\t */\n\tsetPromptsDir(dir: string): void {\n\t\tthis.promptsDir = dir;\n\t\tthis.isInitialized = false;\n\t\tthis.promptCache.clear();\n\t}\n\n\t/**\n\t * Get current prompts directory\n\t * @returns Path to the prompts directory\n\t */\n\tgetPromptsDir(): string {\n\t\treturn this.promptsDir;\n\t}\n\n\t/**\n\t * Check if prompts are loaded in memory\n\t */\n\tisReady(): boolean {\n\t\treturn this.isInitialized;\n\t}\n\n\t/**\n\t * Get the number of cached prompts\n\t */\n\tgetCacheSize(): number {\n\t\treturn this.promptCache.size;\n\t}\n}\n\n// Export a singleton instance\n// Default to backend's .prompts directory (where the SDK is being used)\n// If prompts are not found there, the loader will fallback to SDK's built-in .prompts\nconst defaultPromptsPath = process.env.PROMPTS_DIR || path.join(process.cwd(), '.prompts');\n\nexport const promptLoader = new PromptLoader({\n\tpromptsDir: defaultPromptsPath\n});\n","import Anthropic from \"@anthropic-ai/sdk\";\nimport Groq from \"groq-sdk\";\n\ninterface LLMMessages {\n sys: string;\n user: string;\n}\n\ninterface LLMOptions {\n model?: string;\n maxTokens?: number;\n temperature?: number;\n topP?: number;\n apiKey?: string;\n partial?: (chunk: string) => void; // Callback for each chunk\n}\n\nexport class LLM {\n /* Get a complete text response from an LLM (Anthropic or Groq) */\n static async text(messages: LLMMessages, options: LLMOptions = {}): Promise<string> {\n const [provider, modelName] = this._parseModel(options.model);\n\n if (provider === 'anthropic') {\n return this._anthropicText(messages, modelName, options);\n } else if (provider === 'groq') {\n return this._groqText(messages, modelName, options);\n } else {\n throw new Error(`Unsupported provider: ${provider}. Use \"anthropic\" or \"groq\"`);\n }\n }\n\n /* Stream response from an LLM (Anthropic or Groq) */\n static async stream<T = string>(\n messages: LLMMessages,\n options: LLMOptions = {},\n json?: boolean\n ): Promise<T extends string ? string : any> {\n const [provider, modelName] = this._parseModel(options.model);\n\n if (provider === 'anthropic') {\n return this._anthropicStream(messages, modelName, options, json);\n } else if (provider === 'groq') {\n return this._groqStream(messages, modelName, options, json);\n } else {\n throw new Error(`Unsupported provider: ${provider}. Use \"anthropic\" or \"groq\"`);\n }\n }\n\n // ============================================================\n // PRIVATE HELPER METHODS\n // ============================================================\n\n /**\n * Parse model string to extract provider and model name\n * @param modelString - Format: \"provider/model-name\" or just \"model-name\"\n * @returns [provider, modelName]\n *\n * @example\n * \"anthropic/claude-sonnet-4-5\" → [\"anthropic\", \"claude-sonnet-4-5\"]\n * \"groq/openai/gpt-oss-120b\" → [\"groq\", \"openai/gpt-oss-120b\"]\n * \"claude-sonnet-4-5\" → [\"anthropic\", \"claude-sonnet-4-5\"] (default)\n */\n private static _parseModel(modelString?: string): [string, string] {\n if (!modelString) {\n // Default to Anthropic Claude\n return ['anthropic', 'claude-sonnet-4-5'];\n }\n\n // Check if model string has provider prefix\n if (modelString.includes('/')) {\n // Split only on the FIRST slash to handle models like \"groq/openai/gpt-oss-120b\"\n const firstSlashIndex = modelString.indexOf('/');\n const provider = modelString.substring(0, firstSlashIndex).toLowerCase().trim();\n const model = modelString.substring(firstSlashIndex + 1).trim();\n return [provider, model];\n }\n\n // No prefix, assume Anthropic\n return ['anthropic', modelString];\n }\n\n // ============================================================\n // ANTHROPIC IMPLEMENTATION\n // ============================================================\n\n private static async _anthropicText(\n messages: LLMMessages,\n modelName: string,\n options: LLMOptions\n ): Promise<string> {\n const apiKey = options.apiKey || process.env.ANTHROPIC_API_KEY || \"\";\n const client = new Anthropic({\n apiKey: apiKey,\n });\n\n const response = await client.messages.create({\n model: modelName,\n max_tokens: options.maxTokens || 1000,\n temperature: options.temperature,\n system: messages.sys,\n messages: [{\n role: \"user\",\n content: messages.user\n }]\n });\n\n const textBlock = response.content.find(block => block.type === 'text');\n return textBlock?.type === 'text' ? textBlock.text : '';\n }\n\n private static async _anthropicStream(\n messages: LLMMessages,\n modelName: string,\n options: LLMOptions,\n json?: boolean\n ): Promise<any> {\n const apiKey = options.apiKey || process.env.ANTHROPIC_API_KEY || \"\";\n const client = new Anthropic({\n apiKey: apiKey,\n });\n\n const stream = await client.messages.create({\n model: modelName,\n max_tokens: options.maxTokens || 1000,\n temperature: options.temperature,\n system: messages.sys,\n messages: [{\n role: \"user\",\n content: messages.user\n }],\n stream: true,\n });\n\n let fullText = '';\n\n // Process stream\n for await (const chunk of stream) {\n if (chunk.type === 'content_block_delta' && chunk.delta.type === 'text_delta') {\n const text = chunk.delta.text;\n fullText += text;\n\n // Call partial callback if provided\n if (options.partial) {\n options.partial(text);\n }\n }\n }\n\n // Return parsed JSON or text\n if (json) {\n return this._parseJSON(fullText);\n }\n\n return fullText;\n }\n\n // ============================================================\n // GROQ IMPLEMENTATION\n // ============================================================\n\n private static async _groqText(\n messages: LLMMessages,\n modelName: string,\n options: LLMOptions\n ): Promise<string> {\n const client = new Groq({\n apiKey: options.apiKey || process.env.GROQ_API_KEY || \"\",\n });\n\n const response = await client.chat.completions.create({\n model: modelName,\n messages: [\n { role: 'system', content: messages.sys },\n { role: 'user', content: messages.user }\n ],\n temperature: options.temperature,\n max_tokens: options.maxTokens || 1000,\n });\n\n return response.choices[0]?.message?.content || '';\n }\n\n private static async _groqStream(\n messages: LLMMessages,\n modelName: string,\n options: LLMOptions,\n json?: boolean\n ): Promise<any> {\n const apiKey = options.apiKey || process.env.GROQ_API_KEY || \"\";\n const client = new Groq({\n apiKey: apiKey,\n });\n\n const stream = await client.chat.completions.create({\n model: modelName,\n messages: [\n { role: 'system', content: messages.sys },\n { role: 'user', content: messages.user }\n ],\n temperature: options.temperature,\n max_tokens: options.maxTokens || 1000,\n stream: true,\n response_format: json ? { type: 'json_object' } : undefined\n });\n\n let fullText = '';\n\n // Process stream\n for await (const chunk of stream) {\n const text = chunk.choices[0]?.delta?.content || '';\n if (text) {\n fullText += text;\n\n // Call partial callback if provided\n if (options.partial) {\n options.partial(text);\n }\n }\n }\n\n // Return parsed JSON or text\n if (json) {\n return this._parseJSON(fullText);\n }\n\n return fullText;\n }\n\n // ============================================================\n // JSON PARSING HELPER\n // ============================================================\n\n /**\n * Parse JSON string, handling markdown code blocks and surrounding text\n * Enhanced version from anthropic.ts implementation\n * @param text - Text that may contain JSON wrapped in ```json...``` or with surrounding text\n * @returns Parsed JSON object\n */\n private static _parseJSON(text: string): any {\n let jsonText = text.trim();\n\n // Remove markdown code blocks\n if (jsonText.startsWith('```json')) {\n jsonText = jsonText.replace(/^```json\\s*\\n?/, '').replace(/\\n?```\\s*$/, '');\n } else if (jsonText.startsWith('```')) {\n jsonText = jsonText.replace(/^```\\s*\\n?/, '').replace(/\\n?```\\s*$/, '');\n }\n\n // Extract JSON if there's surrounding text - find the first { and last }\n const firstBrace = jsonText.indexOf('{');\n const lastBrace = jsonText.lastIndexOf('}');\n if (firstBrace !== -1 && lastBrace !== -1 && firstBrace < lastBrace) {\n jsonText = jsonText.substring(firstBrace, lastBrace + 1);\n }\n\n return JSON.parse(jsonText);\n }\n}","import { Component } from '../types';\nimport { ensureQueryLimit } from './utils';\nimport { schema } from './schema';\nimport { promptLoader } from './prompt-loader';\nimport { LLM } from '../llm';\nimport { logger } from '../utils/logger';\n\nexport interface BaseLLMConfig {\n\tmodel?: string;\n\tdefaultLimit?: number;\n\tapiKey?: string;\n}\n\n/**\n * BaseLLM abstract class for AI-powered component generation and matching\n * Provides common functionality for all LLM providers\n */\nexport abstract class BaseLLM {\n\tprotected model: string;\n\tprotected defaultLimit: number;\n\tprotected apiKey?: string;\n\n\tconstructor(config?: BaseLLMConfig) {\n\t\tthis.model = config?.model || this.getDefaultModel();\n\t\tthis.defaultLimit = config?.defaultLimit || 50;\n\t\tthis.apiKey = config?.apiKey;\n\t}\n\n\t/**\n\t * Get the default model for this provider\n\t */\n\tprotected abstract getDefaultModel(): string;\n\n\t/**\n\t * Get the default API key from environment\n\t */\n\tprotected abstract getDefaultApiKey(): string | undefined;\n\n\t/**\n\t * Get the provider name (for logging)\n\t */\n\tprotected abstract getProviderName(): string;\n\n\t/**\n\t * Get the API key (from instance, parameter, or environment)\n\t */\n\tprotected getApiKey(apiKey?: string): string | undefined {\n\t\treturn apiKey || this.apiKey || this.getDefaultApiKey();\n\t}\n\n\t/**\n\t * Classify user question to determine the type and required visualizations\n\t */\n\tasync classifyUserQuestion(\n\t\tuserPrompt: string,\n\t\tapiKey?: string,\n\t\tlogCollector?: any,\n\t\tconversationHistory?: string\n\t): Promise<{\n\t\tquestionType: 'analytical' | 'data_modification' | 'general';\n\t\tvisualizations: string[];\n\t\treasoning: string;\n\t\tneedsMultipleComponents: boolean;\n\t}> {\n\t\t\n\t\ttry {\n\t\t\tconst prompts = await promptLoader.loadPrompts('classify', {\n\t\t\t\tUSER_PROMPT: userPrompt,\n\t\t\t\tCONVERSATION_HISTORY: conversationHistory || 'No previous conversation'\n\t\t\t});\n\n\t\t\tconst result = await LLM.stream<any>(\n\t\t\t\t{\n\t\t\t\t\tsys: prompts.system,\n\t\t\t\t\tuser: prompts.user\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tmodel: this.model,\n\t\t\t\t\tmaxTokens: 800,\n\t\t\t\t\ttemperature: 0.2,\n\t\t\t\t\tapiKey: this.getApiKey(apiKey)\n\t\t\t\t},\n\t\t\t\ttrue // Parse as JSON\n\t\t\t) as any;\n\n\t\t\t// Log the LLM explanation with type\n\t\t\tlogCollector?.logExplanation(\n\t\t\t\t'User question classified',\n\t\t\t\tresult.reasoning || 'No reasoning provided',\n\t\t\t\t{\n\t\t\t\t\tquestionType: result.questionType || 'general',\n\t\t\t\t\tvisualizations: result.visualizations || [],\n\t\t\t\t\tneedsMultipleComponents: result.needsMultipleComponents || false\n\t\t\t\t}\n\t\t\t);\n\n\t\t\treturn {\n\t\t\t\tquestionType: result.questionType || 'general',\n\t\t\t\tvisualizations: result.visualizations || [],\n\t\t\t\treasoning: result.reasoning || 'No reasoning provided',\n\t\t\t\tneedsMultipleComponents: result.needsMultipleComponents || false\n\t\t\t};\n\t\t} catch (error) {\n\t\t\tconsole.error('Error classifying user question:', error);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t/**\n\t * Enhanced function that validates and modifies the entire props object based on user request\n\t * This includes query, title, description, and config properties\n\t */\n\tasync validateAndModifyProps(\n\t\tuserPrompt: string,\n\t\toriginalProps: any,\n\t\tcomponentName: string,\n\t\tcomponentType: string,\n\t\tcomponentDescription?: string,\n\t\tapiKey?: string,\n\t\tlogCollector?: any,\n\t\tconversationHistory?: string\n\t): Promise<{ props: any; isModified: boolean; reasoning: string; modifications: string[] }> {\n\n\t\tconst schemaDoc = schema.generateSchemaDocumentation();\n\t\ttry {\n\t\t\tconst prompts = await promptLoader.loadPrompts('modify-props', {\n\t\t\t\tCOMPONENT_NAME: componentName,\n\t\t\t\tCOMPONENT_TYPE: componentType,\n\t\t\t\tCOMPONENT_DESCRIPTION: componentDescription || 'No description',\n\t\t\t\tSCHEMA_DOC: schemaDoc || 'No schema available',\n\t\t\t\tDEFAULT_LIMIT: this.defaultLimit,\n\t\t\t\tUSER_PROMPT: userPrompt,\n\t\t\t\tCURRENT_PROPS: JSON.stringify(originalProps, null, 2),\n\t\t\t\tCONVERSATION_HISTORY: conversationHistory || 'No previous conversation'\n\t\t\t});\n\n\t\t\tlogger.debug('props-modification: System prompt\\n',prompts.system.substring(0, 100), '\\n\\n\\n', 'User prompt:', prompts.user.substring(0, 50));\t\n\t\t\tconst result = await LLM.stream<any>(\n\t\t\t\t{\n\t\t\t\t\tsys: prompts.system,\n\t\t\t\t\tuser: prompts.user\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tmodel: this.model,\n\t\t\t\t\tmaxTokens: 2500,\n\t\t\t\t\ttemperature: 0.2,\n\t\t\t\t\tapiKey: this.getApiKey(apiKey)\n\t\t\t\t},\n\t\t\t\ttrue // Parse as JSON\n\t\t\t) as any;\n\n\t\t\t// Ensure all queries have a LIMIT clause\n\t\t\tconst props = result.props || originalProps;\n\t\t\tif (props && props.query) {\n\t\t\t\tprops.query = ensureQueryLimit(props.query, this.defaultLimit);\n\t\t\t}\n\n\t\t\t// Log the generated query and explanation with types\n\t\t\tif (props && props.query) {\n\t\t\t\tlogCollector?.logQuery(\n\t\t\t\t\t'Props query modified',\n\t\t\t\t\tprops.query,\n\t\t\t\t\t{\n\t\t\t\t\t\tmodifications: result.modifications || [],\n\t\t\t\t\t\treasoning: result.reasoning || 'No modifications needed'\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tif (result.reasoning) {\n\t\t\t\tlogCollector?.logExplanation(\n\t\t\t\t\t'Props modification explanation',\n\t\t\t\t\tresult.reasoning,\n\t\t\t\t\t{ modifications: result.modifications || [] }\n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tprops: props,\n\t\t\t\tisModified: result.isModified || false,\n\t\t\t\treasoning: result.reasoning || 'No modifications needed',\n\t\t\t\tmodifications: result.modifications || []\n\t\t\t};\n\t\t} catch (error) {\n\t\t\tconsole.error(`Error validating/modifying props with ${this.getProviderName()}:`, error);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t/**\n\t * Match and select a component from available components filtered by type\n\t * This picks the best matching component based on user prompt and modifies its props\n\t */\n\tasync generateAnalyticalComponent(\n\t\tuserPrompt: string,\n\t\tcomponents: Component[],\n\t\tpreferredVisualizationType?: string,\n\t\tapiKey?: string,\n\t\tlogCollector?: any,\n\t\tconversationHistory?: string\n\t): Promise<{\n\t\tcomponent: Component | null;\n\t\treasoning: string;\n\t\tisGenerated: boolean;\n\t}> {\n\t\ttry {\n\t\t\t// Filter components by the preferred visualization type\n\t\t\tconst filteredComponents = preferredVisualizationType\n\t\t\t\t? components.filter(c => c.type === preferredVisualizationType)\n\t\t\t\t: components;\n\n\t\t\tif (filteredComponents.length === 0) {\n\t\t\t\tlogCollector?.warn(\n\t\t\t\t\t`No components found of type ${preferredVisualizationType}`,\n\t\t\t\t\t'explanation',\n\t\t\t\t\t{ reason: 'No matching components available for this visualization type' }\n\t\t\t\t);\n\t\t\t\treturn {\n\t\t\t\t\tcomponent: null,\n\t\t\t\t\treasoning: `No components available of type ${preferredVisualizationType}`,\n\t\t\t\t\tisGenerated: false\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Format filtered components for the prompt\n\t\t\tconst componentsText = filteredComponents\n\t\t\t\t.map((comp, idx) => {\n\t\t\t\t\tconst keywords = comp.keywords ? comp.keywords.join(', ') : '';\n\t\t\t\t\tconst category = comp.category || 'general';\n\t\t\t\t\tconst propsPreview = comp.props ? JSON.stringify(comp.props, null, 2) : 'No props';\n\t\t\t\t\treturn `${idx + 1}. ID: ${comp.id}\n Name: ${comp.name}\n Type: ${comp.type}\n Category: ${category}\n Description: ${comp.description || 'No description'}\n Keywords: ${keywords}\n Props Preview: ${propsPreview}`;\n\t\t\t\t})\n\t\t\t\t.join('\\n\\n');\n\n\t\t\tconst visualizationConstraint = preferredVisualizationType\n\t\t\t\t? `\\n**IMPORTANT: Components are filtered to type ${preferredVisualizationType}. Select the best match.**\\n`\n\t\t\t\t: '';\n\n\t\t\tconst prompts = await promptLoader.loadPrompts('single-component', {\n\t\t\t\tCOMPONENT_TYPE: preferredVisualizationType || 'any',\n\t\t\t\tCOMPONENTS_LIST: componentsText,\n\t\t\t\tVISUALIZATION_CONSTRAINT: visualizationConstraint,\n\t\t\t\tUSER_PROMPT: userPrompt,\n\t\t\t\tCONVERSATION_HISTORY: conversationHistory || 'No previous conversation'\n\t\t\t});\n\n\t\t\tlogger.debug('single-component: System prompt\\n',prompts.system.substring(0, 100), '\\n\\n\\n', 'User prompt:', prompts.user.substring(0, 50));\n\t\t\tconst result = await LLM.stream<any>(\n\t\t\t\t{\n\t\t\t\t\tsys: prompts.system,\n\t\t\t\t\tuser: prompts.user\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tmodel: this.model,\n\t\t\t\t\tmaxTokens: 2000,\n\t\t\t\t\ttemperature: 0.2,\n\t\t\t\t\tapiKey: this.getApiKey(apiKey)\n\t\t\t\t},\n\t\t\t\ttrue // Parse as JSON\n\t\t\t) as any;\n\n\t\t\tif (!result.canGenerate || result.confidence < 50) {\n\t\t\t\tlogCollector?.warn(\n\t\t\t\t\t'Cannot match component',\n\t\t\t\t\t'explanation',\n\t\t\t\t\t{ reason: result.reasoning || 'Unable to find matching component for this question' }\n\t\t\t\t);\n\t\t\t\treturn {\n\t\t\t\t\tcomponent: null,\n\t\t\t\t\treasoning: result.reasoning || 'Unable to find matching component for this question',\n\t\t\t\t\tisGenerated: false\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Get the matched component\n\t\t\tconst componentIndex = result.componentIndex;\n\t\t\tconst componentId = result.componentId;\n\t\t\tlet matchedComponent = null;\n\n\t\t\t// Prefer componentId over componentIndex\n\t\t\tif (componentId) {\n\t\t\t\tmatchedComponent = filteredComponents.find(c => c.id === componentId);\n\t\t\t}\n\n\t\t\t// Fallback to componentIndex\n\t\t\tif (!matchedComponent && componentIndex) {\n\t\t\t\tmatchedComponent = filteredComponents[componentIndex - 1];\n\t\t\t}\n\n\t\t\tif (!matchedComponent) {\n\t\t\t\tlogCollector?.warn('Component not found in filtered list');\n\t\t\t\treturn {\n\t\t\t\t\tcomponent: null,\n\t\t\t\t\treasoning: 'Component not found in filtered list',\n\t\t\t\t\tisGenerated: false\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tlogCollector?.info(`Matched component: ${matchedComponent.name} (confidence: ${result.confidence}%)`);\n\n\t\t\t// Now modify the component's props based on user prompt\n\t\t\tconst propsValidation = await this.validateAndModifyProps(\n\t\t\t\tuserPrompt,\n\t\t\t\tmatchedComponent.props,\n\t\t\t\tmatchedComponent.name,\n\t\t\t\tmatchedComponent.type,\n\t\t\t\tmatchedComponent.description,\n\t\t\t\tapiKey,\n\t\t\t\tlogCollector,\n\t\t\t\tconversationHistory\n\t\t\t);\n\n\t\t\t// Create modified component\n\t\t\tconst modifiedComponent: Component = {\n\t\t\t\t...matchedComponent,\n\t\t\t\tprops: propsValidation.props\n\t\t\t};\n\n\t\t\tlogCollector?.logExplanation(\n\t\t\t\t'Analytical component selected and modified',\n\t\t\t\tresult.reasoning || 'Selected component based on analytical question',\n\t\t\t\t{\n\t\t\t\t\tcomponentName: matchedComponent.name,\n\t\t\t\t\tcomponentType: matchedComponent.type,\n\t\t\t\t\tconfidence: result.confidence,\n\t\t\t\t\tpropsModified: propsValidation.isModified\n\t\t\t\t}\n\t\t\t);\n\n\t\t\treturn {\n\t\t\t\tcomponent: modifiedComponent,\n\t\t\t\treasoning: result.reasoning || 'Selected and modified component based on analytical question',\n\t\t\t\tisGenerated: true\n\t\t\t};\n\t\t} catch (error) {\n\t\t\tconsole.error('Error generating analytical component:', error);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t/**\n\t * Generate container metadata (title and description) for multi-component dashboard\n\t */\n\tasync generateContainerMetadata(\n\t\tuserPrompt: string,\n\t\tvisualizationTypes: string[],\n\t\tapiKey?: string,\n\t\tlogCollector?: any,\n\t\tconversationHistory?: string\n\t): Promise<{\n\t\ttitle: string;\n\t\tdescription: string;\n\t}> {\n\t\ttry {\n\t\t\tconst prompts = await promptLoader.loadPrompts('container-metadata', {\n\t\t\t\tUSER_PROMPT: userPrompt,\n\t\t\t\tVISUALIZATION_TYPES: visualizationTypes.join(', '),\n\t\t\t\tCONVERSATION_HISTORY: conversationHistory || 'No previous conversation'\n\t\t\t});\n\n\t\t\tconst result = await LLM.stream<any>(\n\t\t\t\t{\n\t\t\t\t\tsys: prompts.system,\n\t\t\t\t\tuser: prompts.user\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tmodel: this.model,\n\t\t\t\t\tmaxTokens: 500,\n\t\t\t\t\ttemperature: 0.3,\n\t\t\t\t\tapiKey: this.getApiKey(apiKey)\n\t\t\t\t},\n\t\t\t\ttrue // Parse as JSON\n\t\t\t) as any;\n\n\t\t\tlogCollector?.logExplanation(\n\t\t\t\t'Container metadata generated',\n\t\t\t\t`Generated title and description for multi-component dashboard`,\n\t\t\t\t{\n\t\t\t\t\ttitle: result.title,\n\t\t\t\t\tdescription: result.description,\n\t\t\t\t\tvisualizationTypes\n\t\t\t\t}\n\t\t\t);\n\n\t\t\treturn {\n\t\t\t\ttitle: result.title || `${userPrompt} - Dashboard`,\n\t\t\t\tdescription: result.description || `Multi-component dashboard showing ${visualizationTypes.join(', ')}`\n\t\t\t};\n\t\t} catch (error) {\n\t\t\tconsole.error('Error generating container metadata:', error);\n\t\t\t// Return fallback values\n\t\t\treturn {\n\t\t\t\ttitle: `${userPrompt} - Dashboard`,\n\t\t\t\tdescription: `Multi-component dashboard showing ${visualizationTypes.join(', ')}`\n\t\t\t};\n\t\t}\n\t}\n\n\t/**\n\t * Match component from a list with enhanced props modification\n\t */\n\tasync matchComponent(\n\t\tuserPrompt: string,\n\t\tcomponents: Component[],\n\t\tapiKey?: string,\n\t\tlogCollector?: any,\n\t\tconversationHistory?: string\n\t): Promise<{\n\t\tcomponent: Component | null;\n\t\treasoning: string;\n\t\tqueryModified?: boolean;\n\t\tqueryReasoning?: string;\n\t\tpropsModified?: boolean;\n\t\tpropsModifications?: string[];\n\t\tmethod: string;\n\t\tconfidence?: number;\n\t}> {\n\t\ttry {\n\t\t\t// Step 1: Enhanced component matching with scoring and multiple candidates\n\t\t\tconst componentsText = components\n\t\t\t\t.map((comp, idx) => {\n\t\t\t\t\tconst keywords = comp.keywords ? comp.keywords.join(', ') : '';\n\t\t\t\t\tconst category = comp.category || 'general';\n\t\t\t\t\treturn `${idx + 1}. ID: ${comp.id}\n Name: ${comp.name}\n Type: ${comp.type}\n Category: ${category}\n Description: ${comp.description || 'No description'}\n Keywords: ${keywords}`;\n\t\t\t\t})\n\t\t\t\t.join('\\n\\n');\n\n\t\t\tconst prompts = await promptLoader.loadPrompts('match-component', {\n\t\t\t\tCOMPONENTS_TEXT: componentsText,\n\t\t\t\tUSER_PROMPT: userPrompt,\n\t\t\t\tCONVERSATION_HISTORY: conversationHistory || 'No previous conversation'\n\t\t\t});\n\n\t\t\tconst result = await LLM.stream<any>(\n\t\t\t\t{\n\t\t\t\t\tsys: prompts.system,\n\t\t\t\t\tuser: prompts.user\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tmodel: this.model,\n\t\t\t\t\tmaxTokens: 800,\n\t\t\t\t\ttemperature: 0.2,\n\t\t\t\t\tapiKey: this.getApiKey(apiKey)\n\t\t\t\t},\n\t\t\t\ttrue // Parse as JSON\n\t\t\t) as any;\n\n\t\t\tconst componentIndex = result.componentIndex;\n\t\t\tconst componentId = result.componentId;\n\t\t\tconst confidence = result.confidence || 0;\n\n\t\t\t// Prefer componentId over componentIndex for accuracy\n\t\t\tlet component = null;\n\t\t\tif (componentId) {\n\t\t\t\tcomponent = components.find(c => c.id === componentId);\n\t\t\t}\n\n\t\t\t// Fallback to componentIndex if ID not found\n\t\t\tif (!component && componentIndex) {\n\t\t\t\tcomponent = components[componentIndex - 1];\n\t\t\t}\n\n\t\t\tconst matchedMsg = `${this.getProviderName()} matched component: ${component?.name || 'None'}`;\n\t\t\tconsole.log('✓', matchedMsg);\n\t\t\tlogCollector?.info(matchedMsg);\n\n\t\t\tif (result.alternativeMatches && result.alternativeMatches.length > 0) {\n\t\t\t\tconsole.log(' Alternative matches:');\n\t\t\t\tconst altMatches = result.alternativeMatches.map((alt: any) =>\n\t\t\t\t\t`${components[alt.index - 1]?.name} (${alt.score}%): ${alt.reason}`\n\t\t\t\t).join(' | ');\n\t\t\t\tlogCollector?.info(`Alternative matches: ${altMatches}`);\n\t\t\t\tresult.alternativeMatches.forEach((alt: any) => {\n\t\t\t\t\tconsole.log(` - ${components[alt.index - 1]?.name} (${alt.score}%): ${alt.reason}`);\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tif (!component) {\n\t\t\t\tconst noMatchMsg = `No matching component found (confidence: ${confidence}%)`;\n\t\t\t\tconsole.log('✗', noMatchMsg);\n\t\t\t\tlogCollector?.warn(noMatchMsg);\n\t\t\t\tconst genMsg = 'Attempting to match component from analytical question...';\n\t\t\t\tconsole.log('✓', genMsg);\n\t\t\t\tlogCollector?.info(genMsg);\n\n\t\t\t\t// Try to match a component for the analytical question\n\t\t\t\tconst generatedResult = await this.generateAnalyticalComponent(userPrompt, components, undefined, apiKey, logCollector, conversationHistory);\n\n\t\t\t\tif (generatedResult.component) {\n\t\t\t\t\tconst genSuccessMsg = `Successfully matched component: ${generatedResult.component.name}`;\n\t\t\t\t\tlogCollector?.info(genSuccessMsg);\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcomponent: generatedResult.component,\n\t\t\t\t\t\treasoning: generatedResult.reasoning,\n\t\t\t\t\t\tmethod: `${this.getProviderName()}-generated`,\n\t\t\t\t\t\tconfidence: 100, // Generated components are considered 100% match to the question\n\t\t\t\t\t\tpropsModified: false,\n\t\t\t\t\t\tqueryModified: false\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\t// If matching also failed, return null\n\t\t\t\tlogCollector?.error('Failed to match component');\n\t\t\t\treturn {\n\t\t\t\t\tcomponent: null,\n\t\t\t\t\treasoning: result.reasoning || 'No matching component found and unable to match component',\n\t\t\t\t\tmethod: `${this.getProviderName()}-llm`,\n\t\t\t\t\tconfidence\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Step 2: Validate and modify the entire props object based on user request\n\t\t\tlet propsModified = false;\n\t\t\tlet propsModifications: string[] = [];\n\t\t\tlet queryModified = false;\n\t\t\tlet queryReasoning = '';\n\n\t\t\tif (component && component.props) {\n\n\t\t\t\tconst propsValidation = await this.validateAndModifyProps(\n\t\t\t\t\tuserPrompt,\n\t\t\t\t\tcomponent.props,\n\t\t\t\t\tcomponent.name,\n\t\t\t\t\tcomponent.type,\n\t\t\t\t\tcomponent.description,\n\t\t\t\t\tapiKey,\n\t\t\t\t\tlogCollector,\n\t\t\t\t\tconversationHistory\n\t\t\t\t);\n\n\t\t\t\t// Create a new component object with the modified props\n\t\t\t\tconst originalQuery = component.props.query;\n\t\t\t\tconst modifiedQuery = propsValidation.props.query;\n\n\t\t\t\tcomponent = {\n\t\t\t\t\t...component,\n\t\t\t\t\tprops: propsValidation.props\n\t\t\t\t};\n\n\t\t\t\tpropsModified = propsValidation.isModified;\n\t\t\t\tpropsModifications = propsValidation.modifications;\n\t\t\t\tqueryModified = originalQuery !== modifiedQuery;\n\t\t\t\tqueryReasoning = propsValidation.reasoning;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tcomponent,\n\t\t\t\treasoning: result.reasoning || 'No reasoning provided',\n\t\t\t\tqueryModified,\n\t\t\t\tqueryReasoning,\n\t\t\t\tpropsModified,\n\t\t\t\tpropsModifications,\n\t\t\t\tmethod: `${this.getProviderName()}-llm`,\n\t\t\t\tconfidence\n\t\t\t};\n\t\t} catch (error) {\n\t\t\tconsole.error(`Error matching component with ${this.getProviderName()}:`, error);\n\t\t\tlogCollector?.error(`Error matching component: ${(error as Error).message}`);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t/**\n\t * Match multiple components for analytical questions by visualization types\n\t * This is used when the user needs multiple visualizations\n\t */\n\tasync generateMultipleAnalyticalComponents(\n\t\tuserPrompt: string,\n\t\tavailableComponents: Component[],\n\t\tvisualizationTypes: string[],\n\t\tapiKey?: string,\n\t\tlogCollector?: any,\n\t\tconversationHistory?: string\n\t): Promise<{\n\t\tcomponents: Component[];\n\t\treasoning: string;\n\t\tisGenerated: boolean;\n\t}> {\n\t\ttry {\n\t\t\tconsole.log('✓ Matching multiple components:', visualizationTypes);\n\n\t\t\tconst components: Component[] = [];\n\n\t\t\t// Match each component type requested\n\t\t\tfor (const vizType of visualizationTypes) {\n\t\t\t\tconst result = await this.generateAnalyticalComponent(userPrompt, availableComponents, vizType, apiKey, logCollector, conversationHistory);\n\n\t\t\t\tif (result.component) {\n\t\t\t\t\tcomponents.push(result.component);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (components.length === 0) {\n\t\t\t\treturn {\n\t\t\t\t\tcomponents: [],\n\t\t\t\t\treasoning: 'Failed to match any components',\n\t\t\t\t\tisGenerated: false\n\t\t\t\t};\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tcomponents,\n\t\t\t\treasoning: `Matched ${components.length} components: ${visualizationTypes.join(', ')}`,\n\t\t\t\tisGenerated: true\n\t\t\t};\n\t\t} catch (error) {\n\t\t\tconsole.error('Error matching multiple analytical components:', error);\n\t\t\treturn {\n\t\t\t\tcomponents: [],\n\t\t\t\treasoning: 'Error occurred while matching components',\n\t\t\t\tisGenerated: false\n\t\t\t};\n\t\t}\n\t}\n\n\t/**\n\t * Match multiple components and wrap them in a container\n\t */\n\tasync generateMultiComponentResponse(\n\t\tuserPrompt: string,\n\t\tavailableComponents: Component[],\n\t\tvisualizationTypes: string[],\n\t\tapiKey?: string,\n\t\tlogCollector?: any,\n\t\tconversationHistory?: string\n\t): Promise<{\n\t\tcontainerComponent: Component | null;\n\t\treasoning: string;\n\t\tisGenerated: boolean;\n\t}> {\n\t\ttry {\n\t\t\t// Match multiple components for each visualization type\n\t\t\tconst matchResult = await this.generateMultipleAnalyticalComponents(\n\t\t\t\tuserPrompt,\n\t\t\t\tavailableComponents,\n\t\t\t\tvisualizationTypes,\n\t\t\t\tapiKey,\n\t\t\t\tlogCollector,\n\t\t\t\tconversationHistory\n\t\t\t);\n\n\t\t\tif (!matchResult.isGenerated || matchResult.components.length === 0) {\n\t\t\t\treturn {\n\t\t\t\t\tcontainerComponent: null,\n\t\t\t\t\treasoning: matchResult.reasoning || 'Unable to match multi-component dashboard',\n\t\t\t\t\tisGenerated: false\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst generatedComponents = matchResult.components;\n\n\t\t\t// Log each generated component's query\n\t\t\tgeneratedComponents.forEach((component, index) => {\n\t\t\t\tif (component.props.query) {\n\t\t\t\t\tlogCollector?.logQuery(\n\t\t\t\t\t\t`Multi-component query generated (${index + 1}/${generatedComponents.length})`,\n\t\t\t\t\t\tcomponent.props.query,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcomponentType: component.type,\n\t\t\t\t\t\t\ttitle: component.props.title,\n\t\t\t\t\t\t\tposition: index + 1,\n\t\t\t\t\t\t\ttotalComponents: generatedComponents.length\n\t\t\t\t\t\t}\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\t// Generate container title and description\n\t\t\tconst containerTitle = `${userPrompt} - Dashboard`;\n\t\t\tconst containerDescription = `Multi-component dashboard showing ${visualizationTypes.join(', ')}`;\n\n\t\t\t// Log the overall explanation for the multi-component dashboard\n\t\t\tlogCollector?.logExplanation(\n\t\t\t\t'Multi-component dashboard matched',\n\t\t\t\tmatchResult.reasoning || `Matched ${generatedComponents.length} components for comprehensive analysis`,\n\t\t\t\t{\n\t\t\t\t\ttotalComponents: generatedComponents.length,\n\t\t\t\t\tcomponentTypes: generatedComponents.map(c => c.type),\n\t\t\t\t\tcomponentNames: generatedComponents.map(c => c.name),\n\t\t\t\t\tcontainerTitle,\n\t\t\t\t\tcontainerDescription\n\t\t\t\t}\n\t\t\t);\n\n\t\t\t// Create the MultiComponentContainer wrapper\n\t\t\tconst containerComponent: Component = {\n\t\t\t\tid: `multi_container_${Date.now()}`,\n\t\t\t\tname: 'MultiComponentContainer',\n\t\t\t\ttype: 'Container',\n\t\t\t\tdescription: containerDescription,\n\t\t\t\tcategory: 'dynamic',\n\t\t\t\tkeywords: ['multi', 'container', 'dashboard'],\n\t\t\t\tprops: {\n\t\t\t\t\tconfig: {\n\t\t\t\t\t\tcomponents: generatedComponents,\n\t\t\t\t\t\tlayout: 'grid',\n\t\t\t\t\t\tspacing: 24,\n\t\t\t\t\t\ttitle: containerTitle,\n\t\t\t\t\t\tdescription: containerDescription\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t};\n\n\t\t\treturn {\n\t\t\t\tcontainerComponent,\n\t\t\t\treasoning: matchResult.reasoning || `Matched multi-component dashboard with ${generatedComponents.length} components`,\n\t\t\t\tisGenerated: true\n\t\t\t};\n\t\t} catch (error) {\n\t\t\tconsole.error('Error generating multi-component response:', error);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t/**\n\t * Main orchestration function that classifies question and routes to appropriate handler\n\t * This is the NEW recommended entry point for handling user requests\n\t * ALWAYS returns a SINGLE component (wraps multiple in MultiComponentContainer)\n\t */\n\tasync handleUserRequest(\n\t\tuserPrompt: string,\n\t\tcomponents: Component[],\n\t\tapiKey?: string,\n\t\tlogCollector?: any,\n\t\tconversationHistory?: string\n\t): Promise<{\n\t\tcomponent: Component | null;\n\t\treasoning: string;\n\t\tmethod: string;\n\t\tquestionType: string;\n\t\tneedsMultipleComponents: boolean;\n\t\tpropsModified?: boolean;\n\t\tqueryModified?: boolean;\n\t}> {\n\t\ttry {\n\t\t\t// Step 1: Classify the user's question\n\t\t\tconst classifyMsg = 'Classifying user question...';\n\t\t\tlogCollector?.info(classifyMsg);\n\t\t\tconst classification = await this.classifyUserQuestion(userPrompt, apiKey, logCollector, conversationHistory);\n\t\t\tconst classInfo = `Question type: ${classification.questionType}, Visualizations: ${classification.visualizations.join(', ') || 'None'}, Multiple components: ${classification.needsMultipleComponents}`;\n\t\t\tlogCollector?.info(classInfo);\n\n\t\t\t// Step 2: Route based on question type\n\t\t\tif (classification.questionType === 'analytical') {\n\t\t\t\t// For analytical questions with specific visualization types\n\t\t\t\tif (classification.visualizations.length > 1) {\n\t\t\t\t\t// Multiple visualization types - match component for each type\n\t\t\t\t\tconst multiMsg = `Matching ${classification.visualizations.length} components for types: ${classification.visualizations.join(', ')}`;\n\t\t\t\t\tlogCollector?.info(multiMsg);\n\n\t\t\t\t\tconst matchedComponents: Component[] = [];\n\n\t\t\t\t\t// Loop through each visualization type and match a component\n\t\t\t\t\tfor (const vizType of classification.visualizations) {\n\t\t\t\t\t\tlogCollector?.info(`Matching component for type: ${vizType}`);\n\t\t\t\t\t\tconst result = await this.generateAnalyticalComponent(\n\t\t\t\t\t\t\tuserPrompt,\n\t\t\t\t\t\t\tcomponents,\n\t\t\t\t\t\t\tvizType,\n\t\t\t\t\t\t\tapiKey,\n\t\t\t\t\t\t\tlogCollector,\n\t\t\t\t\t\t\tconversationHistory\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tif (result.component) {\n\t\t\t\t\t\t\tmatchedComponents.push(result.component);\n\t\t\t\t\t\t\tlogCollector?.info(`Matched: ${result.component.name}`);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tlogCollector?.warn(`Failed to match component for type: ${vizType}`);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif (matchedComponents.length === 0) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcomponent: null,\n\t\t\t\t\t\t\treasoning: 'Failed to match any components for the requested visualization types',\n\t\t\t\t\t\t\tmethod: 'classification-multi-failed',\n\t\t\t\t\t\t\tquestionType: classification.questionType,\n\t\t\t\t\t\t\tneedsMultipleComponents: true,\n\t\t\t\t\t\t\tpropsModified: false,\n\t\t\t\t\t\t\tqueryModified: false\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\t// Generate container metadata from user request\n\t\t\t\t\tlogCollector?.info('Generating container metadata...');\n\t\t\t\t\tconst containerMetadata = await this.generateContainerMetadata(\n\t\t\t\t\t\tuserPrompt,\n\t\t\t\t\t\tclassification.visualizations,\n\t\t\t\t\t\tapiKey,\n\t\t\t\t\t\tlogCollector,\n\t\t\t\t\t\tconversationHistory\n\t\t\t\t\t);\n\n\n\t\t\t\t\tconst containerComponent: Component = {\n\t\t\t\t\t\tid: `multi_container_${Date.now()}`,\n\t\t\t\t\t\tname: 'MultiComponentContainer',\n\t\t\t\t\t\ttype: 'Container',\n\t\t\t\t\t\tdescription: containerMetadata.description,\n\t\t\t\t\t\tcategory: 'dynamic',\n\t\t\t\t\t\tkeywords: ['multi', 'container', 'dashboard'],\n\t\t\t\t\t\tprops: {\n\t\t\t\t\t\t\tconfig: {\n\t\t\t\t\t\t\t\tcomponents: matchedComponents,\n\t\t\t\t\t\t\t\tlayout: 'grid',\n\t\t\t\t\t\t\t\tspacing: 24,\n\t\t\t\t\t\t\t\ttitle: containerMetadata.title,\n\t\t\t\t\t\t\t\tdescription: containerMetadata.description\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\n\t\t\t\t\tlogCollector?.info(`Created multi-component container with ${matchedComponents.length} components: \"${containerMetadata.title}\"`);\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcomponent: containerComponent,\n\t\t\t\t\t\treasoning: `Matched ${matchedComponents.length} components for visualization types: ${classification.visualizations.join(', ')}`,\n\t\t\t\t\t\tmethod: 'classification-multi-generated',\n\t\t\t\t\t\tquestionType: classification.questionType,\n\t\t\t\t\t\tneedsMultipleComponents: true,\n\t\t\t\t\t\tpropsModified: false,\n\t\t\t\t\t\tqueryModified: false\n\t\t\t\t\t};\n\t\t\t\t} else if (classification.visualizations.length === 1) {\n\t\t\t\t\t// Single visualization type - match one component\n\t\t\t\t\tconst vizType = classification.visualizations[0];\n\t\t\t\t\tlogCollector?.info(`Matching single component for type: ${vizType}`);\n\t\t\t\t\tconst result = await this.generateAnalyticalComponent(userPrompt, components, vizType, apiKey, logCollector, conversationHistory);\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcomponent: result.component,\n\t\t\t\t\t\treasoning: result.reasoning,\n\t\t\t\t\t\tmethod: 'classification-generated',\n\t\t\t\t\t\tquestionType: classification.questionType,\n\t\t\t\t\t\tneedsMultipleComponents: false,\n\t\t\t\t\t\tpropsModified: false,\n\t\t\t\t\t\tqueryModified: false\n\t\t\t\t\t};\n\t\t\t\t} else {\n\t\t\t\t\t// No specific visualization type, match from all components\n\t\t\t\t\tlogCollector?.info('No specific visualization type - matching from all components');\n\t\t\t\t\tconst result = await this.generateAnalyticalComponent(userPrompt, components, undefined, apiKey, logCollector, conversationHistory);\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcomponent: result.component,\n\t\t\t\t\t\treasoning: result.reasoning,\n\t\t\t\t\t\tmethod: 'classification-generated-auto',\n\t\t\t\t\t\tquestionType: classification.questionType,\n\t\t\t\t\t\tneedsMultipleComponents: false,\n\t\t\t\t\t\tpropsModified: false,\n\t\t\t\t\t\tqueryModified: false\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t} else if (classification.questionType === 'data_modification' || classification.questionType === 'general') {\n\t\t\t\t// For data modification, use the old component matching flow\n\t\t\t\tconst matchMsg = 'Using component matching for data modification...';\n\t\t\t\tlogCollector?.info(matchMsg);\n\t\t\t\tconst matchResult = await this.matchComponent(userPrompt, components, apiKey, logCollector, conversationHistory);\n\n\t\t\t\treturn {\n\t\t\t\t\tcomponent: matchResult.component,\n\t\t\t\t\treasoning: matchResult.reasoning,\n\t\t\t\t\tmethod: 'classification-matched',\n\t\t\t\t\tquestionType: classification.questionType,\n\t\t\t\t\tneedsMultipleComponents: false,\n\t\t\t\t\tpropsModified: matchResult.propsModified,\n\t\t\t\t\tqueryModified: matchResult.queryModified\n\t\t\t\t};\n\t\t\t} else {\n\t\t\t\tlogCollector?.info('General question - no component needed');\n\t\t\t\treturn {\n\t\t\t\t\tcomponent: null,\n\t\t\t\t\treasoning: 'General question - no component needed',\n\t\t\t\t\tmethod: 'classification-general',\n\t\t\t\t\tquestionType: classification.questionType,\n\t\t\t\t\tneedsMultipleComponents: false\n\t\t\t\t};\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tlogCollector?.error(`Error handling user request: ${(error as Error).message}`);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t/**\n\t * Generate next questions that the user might ask based on the original prompt and generated component\n\t * This helps provide intelligent suggestions for follow-up queries\n\t */\n\tasync generateNextQuestions(\n\t\toriginalUserPrompt: string,\n\t\tcomponent: Component,\n\t\tcomponentData?: Record<string, unknown>,\n\t\tapiKey?: string,\n\t\tlogCollector?: any,\n\t\tconversationHistory?: string\n\t): Promise<string[]> {\n\t\ttry {\n\t\t\tconst component_info = `\n\t\t\t\tComponent Name: ${component.name}\n\t\t\t\tComponent Type: ${component.type}\n\t\t\t\tComponent Description: ${component.description || 'No description'}\n\t\t\t\tComponent Props: ${component.props ? JSON.stringify(component.props, null, 2) : 'No props'}\n\t\t\t`;\n\n\t\t\tconst component_data = componentData ? `Component Data: ${JSON.stringify(componentData, null, 2)}` : '';\n\n\t\t\tconst prompts = await promptLoader.loadPrompts('actions', {\n\t\t\t\tORIGINAL_USER_PROMPT: originalUserPrompt,\n\t\t\t\tCOMPONENT_INFO: component_info,\n\t\t\t\tCOMPONENT_DATA: component_data,\n\t\t\t\tCONVERSATION_HISTORY: conversationHistory || 'No previous conversation'\n\t\t\t});\n\n\t\t\tconst result = await LLM.stream<any>(\n\t\t\t\t{\n\t\t\t\t\tsys: prompts.system,\n\t\t\t\t\tuser: prompts.user\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tmodel: this.model,\n\t\t\t\t\tmaxTokens: 800,\n\t\t\t\t\ttemperature: 0.7,\n\t\t\t\t\tapiKey: this.getApiKey(apiKey)\n\t\t\t\t},\n\t\t\t\ttrue // Parse as JSON\n\t\t\t) as any;\n\n\t\t\tconst nextQuestions = result.nextQuestions || [];\n\n\t\t\tlogCollector?.logExplanation(\n\t\t\t\t'Next questions generated',\n\t\t\t\t'Generated intelligent follow-up questions based on component',\n\t\t\t\t{\n\t\t\t\t\tcount: nextQuestions.length,\n\t\t\t\t\tquestions: nextQuestions\n\t\t\t\t}\n\t\t\t);\n\n\t\t\treturn nextQuestions;\n\t\t} catch (error) {\n\t\t\tconsole.error(`Error generating next questions with ${this.getProviderName()}:`, error);\n\t\t\tlogCollector?.error(`Error generating next questions: ${(error as Error).message}`);\n\t\t\t// Return empty array on error instead of throwing\n\t\t\treturn [];\n\t\t}\n\t}\n}\n","import dotenv from 'dotenv';\nimport { BaseLLM, BaseLLMConfig } from './base-llm';\n\ndotenv.config();\n\nexport interface AnthropicLLMConfig extends BaseLLMConfig {}\n\n/**\n * AnthropicLLM class for handling AI-powered component generation and matching using Anthropic Claude\n */\nexport class AnthropicLLM extends BaseLLM {\n\tconstructor(config?: AnthropicLLMConfig) {\n\t\tsuper(config);\n\t}\n\n\tprotected getDefaultModel(): string {\n\t\treturn 'anthropic/claude-haiku-4-5-20251001';\n\t}\n\n\tprotected getDefaultApiKey(): string | undefined {\n\t\treturn process.env.ANTHROPIC_API_KEY;\n\t}\n\n\tprotected getProviderName(): string {\n\t\treturn 'Anthropic';\n\t}\n}\n\n// Export a singleton instance\nexport const anthropicLLM = new AnthropicLLM();\n","import { groqLLM } from \"./groq\";\nimport { anthropicLLM } from \"./anthropic\";\nimport { Component, LLMProvider } from \"../types\";\nimport dotenv from 'dotenv';\nimport { logger } from \"../utils/logger\";\n\ndotenv.config();\n\n\n\n/**\n * Parse LLM_PROVIDERS from environment variable\n * Expects a stringified JSON array like: '[\"anthropic\",\"groq\"]'\n */\nexport function getLLMProviders(): LLMProvider[] {\n const envProviders = process.env.LLM_PROVIDERS;\n\n const DEFAULT_PROVIDERS: LLMProvider[] = ['anthropic', 'groq'];\n if (!envProviders) {\n // Default to anthropic if not specified\n return DEFAULT_PROVIDERS;\n }\n\n try {\n const providers = JSON.parse(envProviders) as LLMProvider[];\n\n // Validate providers\n const validProviders = providers.filter(p => p === 'anthropic' || p === 'groq');\n\n if (validProviders.length === 0) {\n return DEFAULT_PROVIDERS;\n }\n\n return validProviders;\n } catch (error) {\n logger.error('Failed to parse LLM_PROVIDERS, defaulting to [\"anthropic\"]:', error);\n return DEFAULT_PROVIDERS;\n }\n}\n\n/**\n * Method 1: Use Anthropic Claude LLM\n */\nexport const useAnthropicMethod = async (prompt: string, components: Component[], apiKey?: string, logCollector?: any, conversationHistory?: string) => {\n const msg = 'Using Anthropic Claude matching method...';\n console.log(msg);\n logCollector?.info(msg);\n\n if (components.length === 0) {\n const emptyMsg = 'Components not loaded in memory. Please ensure components are fetched first.';\n logCollector?.error(emptyMsg);\n return { success: false, reason: emptyMsg };\n }\n\n try {\n const matchResult = await anthropicLLM.handleUserRequest(prompt, components, apiKey, logCollector, conversationHistory);\n return { success: true, data: matchResult };\n } catch (error) {\n const errorMsg = error instanceof Error ? error.message : String(error);\n logCollector?.error(`Anthropic method failed: ${errorMsg}`);\n throw error; // Re-throw to be caught by the provider fallback mechanism\n }\n};\n\n/**\n * Method 2: Use Groq LLM\n */\nexport const useGroqMethod = async (prompt: string, components: Component[], apiKey?: string, logCollector?: any, conversationHistory?: string) => {\n const msg = 'Using Groq LLM matching method...';\n console.log(msg);\n logCollector?.info(msg);\n\n if (components.length === 0) {\n const emptyMsg = 'Components not loaded in memory. Please ensure components are fetched first.';\n logCollector?.error(emptyMsg);\n return { success: false, reason: emptyMsg };\n }\n\n try {\n const matchResult = await groqLLM.handleUserRequest(prompt, components, apiKey, logCollector, conversationHistory);\n return { success: true, data: matchResult };\n } catch (error) {\n const errorMsg = error instanceof Error ? error.message : String(error);\n logCollector?.error(`Groq method failed: ${errorMsg}`);\n throw error; // Re-throw to be caught by the provider fallback mechanism\n }\n};\n\n//@to-do\nexport const getUserResponseFromCache = async (prompt: string) => {\n return false\n}\n\n/**\n * Get user response with automatic fallback between LLM providers\n * Tries providers in order specified by LLM_PROVIDERS or passed parameter\n */\nexport const get_user_response = async (\n prompt: string,\n components: Component[],\n anthropicApiKey?: string,\n groqApiKey?: string,\n llmProviders?: LLMProvider[],\n logCollector?: any,\n conversationHistory?: string\n) => {\n\n //first checking if he same prompt is already asked and we got successful response.\n const userResponse = await getUserResponseFromCache(prompt);\n if(userResponse){\n logCollector?.info('User response found in cache');\n return {\n success: true,\n data: userResponse\n };\n }\n\n const providers = llmProviders || getLLMProviders();\n const errors: { provider: LLMProvider; error: string }[] = [];\n\n const providerOrder = providers.join(', ');\n logCollector?.info(`LLM Provider order: [${providerOrder}]`);\n\n // Log conversation context info if available\n if (conversationHistory && conversationHistory.length > 0) {\n logCollector?.info(`Using conversation history with ${conversationHistory.split('\\n').filter((l: string) => l.startsWith('Q')).length} previous exchanges`);\n }\n\n for (let i = 0; i < providers.length; i++) {\n const provider = providers[i];\n const isLastProvider = i === providers.length - 1;\n\n try {\n const attemptMsg = `Attempting provider: ${provider} (${i + 1}/${providers.length})`;\n logCollector?.info(attemptMsg);\n\n let result;\n if (provider === 'anthropic') {\n result = await useAnthropicMethod(prompt, components, anthropicApiKey, logCollector, conversationHistory);\n } else if (provider === 'groq') {\n result = await useGroqMethod(prompt, components, groqApiKey, logCollector, conversationHistory);\n } else {\n continue; // Skip unknown providers\n }\n\n if (result.success) {\n const successMsg = `Success with provider: ${provider}`;\n logCollector?.info(successMsg);\n return result;\n } else {\n errors.push({ provider, error: result.reason || 'Unknown error' });\n const warnMsg = `Provider ${provider} returned unsuccessful result: ${result.reason}`;\n logCollector?.warn(warnMsg);\n }\n } catch (error) {\n const errorMessage = (error as Error).message;\n errors.push({ provider, error: errorMessage });\n\n const errorMsg = `Provider ${provider} failed: ${errorMessage}`;\n logCollector?.error(errorMsg);\n\n // If this is not the last provider, try the next one\n if (!isLastProvider) {\n const fallbackMsg = 'Falling back to next provider...';\n logCollector?.info(fallbackMsg);\n continue;\n }\n }\n }\n\n // All providers failed\n const errorSummary = errors\n .map(e => `${e.provider}: ${e.error}`)\n .join('; ');\n\n const failureMsg = `All LLM providers failed. Errors: ${errorSummary}`;\n logCollector?.error(failureMsg);\n\n return {\n success: false,\n reason: failureMsg\n };\n}","import { Message } from '../types';\n\nexport interface CapturedLog {\n timestamp: number;\n level: 'info' | 'error' | 'warn' | 'debug';\n message: string;\n type?: 'explanation' | 'query' | 'general';\n data?: Record<string, any>;\n}\n\n/**\n * UILogCollector captures logs during user prompt processing\n * and sends them to runtime via ui_logs message with uiBlockId as the message id\n * Logs are sent in real-time for streaming effect in the UI\n */\nexport class UILogCollector {\n private logs: CapturedLog[] = [];\n private uiBlockId: string | null;\n private clientId: string;\n private sendMessage: (message: Message) => void;\n\n constructor(\n clientId: string,\n sendMessage: (message: Message) => void,\n uiBlockId?: string\n ) {\n this.uiBlockId = uiBlockId || null;\n this.clientId = clientId;\n this.sendMessage = sendMessage;\n }\n\n /**\n * Check if logging is enabled (uiBlockId is provided)\n */\n isEnabled(): boolean {\n return this.uiBlockId !== null;\n }\n\n /**\n * Add a log entry with timestamp and immediately send to runtime\n */\n private addLog(\n level: 'info' | 'error' | 'warn' | 'debug',\n message: string,\n type?: 'explanation' | 'query' | 'general',\n data?: Record<string, any>\n ): void {\n const log: CapturedLog = {\n timestamp: Date.now(),\n level,\n message,\n ...(type && { type }),\n ...(data && { data }),\n };\n\n this.logs.push(log);\n\n // Send the log immediately to runtime for streaming effect\n this.sendLogImmediately(log);\n }\n\n /**\n * Send a single log to runtime immediately\n */\n private sendLogImmediately(log: CapturedLog): void {\n if (!this.isEnabled()) {\n return;\n }\n\n const response: Message = {\n id: this.uiBlockId!,\n type: 'UI_LOGS',\n from: { type: 'data-agent' },\n to: {\n type: 'runtime',\n id: this.clientId,\n },\n payload: {\n logs: [log], // Send single log in array\n },\n };\n\n this.sendMessage(response);\n }\n\n /**\n * Log info message\n */\n info(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void {\n if (this.isEnabled()) {\n this.addLog('info', message, type, data);\n }\n }\n\n /**\n * Log error message\n */\n error(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void {\n if (this.isEnabled()) {\n this.addLog('error', message, type, data);\n }\n }\n\n /**\n * Log warning message\n */\n warn(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void {\n if (this.isEnabled()) {\n this.addLog('warn', message, type, data);\n }\n }\n\n /**\n * Log debug message\n */\n debug(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void {\n if (this.isEnabled()) {\n this.addLog('debug', message, type, data);\n }\n }\n\n /**\n * Log LLM explanation with typed metadata\n */\n logExplanation(message: string, explanation: string, data?: Record<string, any>): void {\n if (this.isEnabled()) {\n this.addLog('info', message, 'explanation', {\n explanation,\n ...data,\n });\n }\n }\n\n /**\n * Log generated query with typed metadata\n */\n logQuery(message: string, query: string, data?: Record<string, any>): void {\n if (this.isEnabled()) {\n this.addLog('info', message, 'query', {\n query,\n ...data,\n });\n }\n }\n\n /**\n * Send all collected logs at once (optional, for final summary)\n */\n sendAllLogs(): void {\n if (!this.isEnabled() || this.logs.length === 0) {\n return;\n }\n\n const response: Message = {\n id: this.uiBlockId!,\n type: 'UI_LOGS',\n from: { type: 'data-agent' },\n to: {\n type: 'runtime',\n id: this.clientId,\n },\n payload: {\n logs: this.logs,\n },\n };\n\n this.sendMessage(response);\n }\n\n /**\n * Get all collected logs\n */\n getLogs(): CapturedLog[] {\n return [...this.logs];\n }\n\n /**\n * Clear all logs\n */\n clearLogs(): void {\n this.logs = [];\n }\n\n /**\n * Set uiBlockId (in case it's provided later)\n */\n setUIBlockId(uiBlockId: string): void {\n this.uiBlockId = uiBlockId;\n }\n}\n","/**\n * Configuration for conversation context and history management\n */\nexport const CONTEXT_CONFIG = {\n /**\n * Maximum number of previous UIBlocks to include as conversation context\n * Set to 0 to disable conversation history\n * Higher values provide more context but may increase token usage\n */\n MAX_CONVERSATION_CONTEXT_BLOCKS: 2,\n};\n","import { Component, LLMProvider, Message, UserPromptRequestMessageSchema } from \"../types\";\nimport { get_user_response } from \"../userResponse\";\nimport { logger } from \"../utils/logger\";\nimport { UILogCollector } from \"../utils/log-collector\";\nimport { ThreadManager, UIBlock } from \"../threads\";\nimport { CONTEXT_CONFIG } from \"../config/context\";\n\n// Track processed message IDs to prevent duplicates\nconst processedMessageIds = new Set<string>();\n\nexport async function handleUserPromptRequest(\n\tdata: any,\n\tcomponents: Component[],\n\tsendMessage: (message: Message) => void,\n\tanthropicApiKey?: string,\n\tgroqApiKey?: string,\n\tllmProviders?: LLMProvider[]\n): Promise<void> {\n\ttry {\n\t\tconst userPromptRequest = UserPromptRequestMessageSchema.parse(data);\n\t\tconst { id, payload } = userPromptRequest;\n\n\t\tconst prompt = payload.prompt;\n\t\tconst SA_RUNTIME = payload.SA_RUNTIME;\n\n\t\tconst wsId = userPromptRequest.from.id || 'unknown';\n\n\t\tlogger.info(`[REQUEST ${id}] Processing user prompt: \"${prompt.substring(0, 50)}...\"`);\n\n\t\t// Check if this message ID has already been processed\n\t\tif (processedMessageIds.has(id)) {\n\t\t\tlogger.warn(`[REQUEST ${id}] Duplicate request detected - ignoring`);\n\t\t\treturn;\n\t\t}\n\n\t\t// Mark this message ID as processed\n\t\tprocessedMessageIds.add(id);\n\n\t\t// Clean up old message IDs (keep only last 100)\n\t\tif (processedMessageIds.size > 100) {\n\t\t\tconst firstId = processedMessageIds.values().next().value;\n\t\t\tif (firstId) {\n\t\t\t\tprocessedMessageIds.delete(firstId);\n\t\t\t}\n\t\t}\n\n\t\t// Validate SA_RUNTIME and extract threadId and uiBlockId\n\t\tif (!SA_RUNTIME) {\n\t\t\tsendDataResponse(id, {\n\t\t\t\tsuccess: false,\n\t\t\t\terror: 'SA_RUNTIME is required'\n\t\t\t}, sendMessage, wsId);\n\t\t\treturn;\n\t\t}\n\n\t\tconst threadId = SA_RUNTIME.threadId;\n\t\tconst existingUiBlockId = SA_RUNTIME.uiBlockId;\n\n\t\tif (!threadId) {\n\t\t\tsendDataResponse(id, {\n\t\t\t\tsuccess: false,\n\t\t\t\terror: 'threadId in SA_RUNTIME is required'\n\t\t\t}, sendMessage, wsId);\n\t\t\treturn;\n\t\t}\n\n\t\tif (!existingUiBlockId) {\n\t\t\tsendDataResponse(id, {\n\t\t\t\tsuccess: false,\n\t\t\t\terror: 'uiBlockId in SA_RUNTIME is required'\n\t\t\t}, sendMessage, wsId);\n\t\t\treturn;\n\t\t}\n\n\t\t// Create log collector with uiBlockId\n\t\tconst logCollector = new UILogCollector(wsId, sendMessage, existingUiBlockId);\n\n\t\tif (!prompt) {\n\t\t\tsendDataResponse(id, {\n\t\t\t\tsuccess: false,\n\t\t\t\terror: 'Prompt not found'\n\t\t\t}, sendMessage, wsId);\n\t\t\treturn;\n\t\t}\n\n\t\tif (!components || components.length === 0) {\n\t\t\tsendDataResponse(id, {\n\t\t\t\tsuccess: false,\n\t\t\t\terror: 'Components not found'\n\t\t\t}, sendMessage, wsId);\n\t\t\treturn;\n\t\t}\n\n\t\tlogCollector.info(`Starting user prompt request with ${components.length} components`);\n\n\t\t// Get or create thread BEFORE processing to enable conversation context\n\t\tconst threadManager = ThreadManager.getInstance();\n\t\tlet thread = threadManager.getThread(threadId);\n\t\tif (!thread) {\n\t\t\tthread = threadManager.createThread(threadId);\n\t\t\tlogger.info(`Created new thread: ${threadId}`);\n\t\t}\n\n\t\t// Extract conversation context from thread (excluding current UIBlock)\n\t\tconst conversationHistory = thread.getConversationContext(CONTEXT_CONFIG.MAX_CONVERSATION_CONTEXT_BLOCKS, existingUiBlockId);\n\n\t\t// Get user response with log collector and conversation context\n\t\tconst userResponse = await get_user_response(prompt, components, anthropicApiKey, groqApiKey, llmProviders, logCollector, conversationHistory);\n\n\t\t// Log completion (sent immediately if uiBlockId was provided)\n\t\tlogCollector.info('User prompt request completed');\n\n\t\t// If response is successful, create UIBlock and add to Thread\n\t\tif (userResponse.success && userResponse.data && typeof userResponse.data === 'object' && 'component' in userResponse.data) {\n\t\t\tconst component = (userResponse.data as any).component;\n\n\t\t\t// Use the uiBlockId from SA_RUNTIME (already validated)\n\t\t\tconst uiBlockId = existingUiBlockId;\n\n\t\t\t// Create UIBlock with component metadata and empty component data (will fill later)\n\t\t\tconst uiBlock = new UIBlock(\n\t\t\t\tprompt,\n\t\t\t\t{}, // componentData: initially empty, will be filled later\n\t\t\t\tcomponent || {}, // generatedComponentMetadata: full component object (ComponentSchema)\n\t\t\t\t[], // actions: empty initially\n\t\t\t\tuiBlockId\n\t\t\t);\n\n\t\t\t// Add UIBlock to Thread\n\t\t\tthread.addUIBlock(uiBlock);\n\n\t\t\tlogger.info(`Created UIBlock: ${uiBlockId} in Thread: ${threadId}`);\n\n\t\t\t// Send response with uiBlockId and threadId\n\t\t\tsendDataResponse(id, {\n\t\t\t\t...userResponse,\n\t\t\t\tuiBlockId,\n\t\t\t\tthreadId\n\t\t\t}, sendMessage, wsId);\n\t\t} else {\n\t\t\t// If response failed, still send it but without UIBlock/Thread data\n\t\t\tsendDataResponse(id, userResponse, sendMessage, wsId);\n\t\t}\n\n\t\treturn;\n\t}\n\tcatch (error) {\n\t\tlogger.error('Failed to handle user prompt request:', error);\n\t}\n}\n\n/**\n * Send a data_res response message\n */\nfunction sendDataResponse(\n\tid: string,\n\tres: { success: boolean; error?: string; data?: any; uiBlockId?: string; threadId?: string },\n\tsendMessage: (message: Message) => void,\n\tclientId?: string,\n): void {\n\tconst response: Message = {\n\t\tid,\n\t\ttype: 'USER_PROMPT_RES',\n\t\tfrom: { type: 'data-agent' },\n\t\tto: {\n\t\t\ttype: 'runtime',\n\t\t\tid: clientId\n\t\t},\n\t\tpayload: {\n\t\t\t...res,\n\t\t}\n\t};\n\tsendMessage(response);\n} ","import { Component, Message, UserPromptSuggestionsMessageSchema } from '../types';\nimport { logger } from '../utils/logger';\n\n/**\n * Handle user prompt suggestions request\n * Searches components based on prompt keywords and returns top matches\n */\nexport async function handleUserPromptSuggestions(\n data: any,\n components: Component[],\n sendMessage: (message: Message) => void\n): Promise<void> {\n try {\n const request = UserPromptSuggestionsMessageSchema.parse(data);\n const { id, payload, from } = request;\n\n const { prompt, limit = 5 } = payload;\n const wsId = from.id;\n\n // Validate input\n if (!prompt || prompt.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Prompt is required and cannot be empty'\n }, sendMessage, wsId);\n return;\n }\n\n if (!components || components.length === 0) {\n sendResponse(id, {\n success: true,\n data: {\n prompt,\n suggestions: [],\n count: 0,\n message: 'No components available'\n }\n }, sendMessage, wsId);\n return;\n }\n\n // Search components based on prompt\n const suggestions = searchComponents(prompt, components, limit);\n\n\n sendResponse(id, {\n success: true,\n data: {\n prompt,\n suggestions,\n count: suggestions.length,\n message: `Found ${suggestions.length} matching components`\n }\n }, sendMessage, wsId);\n\n } catch (error) {\n logger.error('Failed to handle user prompt suggestions request:', error);\n sendResponse(null, {\n success: false,\n error: error instanceof Error ? error.message : 'Unknown error occurred'\n }, sendMessage);\n }\n}\n\n/**\n * Search components based on prompt keywords\n * Searches in component name, description, keywords, and category\n * @param prompt - Search prompt\n * @param components - List of components to search in\n * @param limit - Maximum number of results to return\n * @returns Matching components sorted by relevance\n */\nfunction searchComponents(prompt: string, components: Component[], limit: number): Component[] {\n const promptLower = prompt.toLowerCase();\n const promptTokens = promptLower.split(/\\s+/).filter(token => token.length > 0);\n\n // Score each component based on keyword matches\n const scoredComponents = components.map(component => {\n let score = 0;\n\n const componentName = component.name.toLowerCase();\n const componentDesc = component.description.toLowerCase();\n const componentKeywords = (component.keywords || []).map(k => k.toLowerCase());\n const componentCategory = (component.category || '').toLowerCase();\n\n // Search in each field with different weights\n for (const token of promptTokens) {\n // Exact name match (highest weight)\n if (componentName === token) {\n score += 10;\n }\n // Name contains token\n else if (componentName.includes(token)) {\n score += 5;\n }\n\n // Exact keyword match\n if (componentKeywords.includes(token)) {\n score += 8;\n }\n // Keywords contain token\n else if (componentKeywords.some(k => k.includes(token))) {\n score += 4;\n }\n\n // Description contains token\n if (componentDesc.includes(token)) {\n score += 2;\n }\n\n // Category contains token\n if (componentCategory.includes(token)) {\n score += 3;\n }\n }\n\n return { component, score };\n });\n\n // Filter out components with score 0, sort by score (descending), and take top results\n return scoredComponents\n .filter(({ score }) => score > 0)\n .sort((a, b) => b.score - a.score)\n .slice(0, limit)\n .map(({ component }) => component);\n}\n\n/**\n * Send user prompt suggestions response\n */\nfunction sendResponse(\n id: string | null,\n res: { success: boolean; error?: string; data?: any },\n sendMessage: (message: Message) => void,\n clientId?: string,\n): void {\n const response: Message = {\n id: id || 'unknown',\n type: 'USER_PROMPT_SUGGESTIONS_RES',\n from: { type: 'data-agent' },\n to: {\n type: 'runtime',\n id: clientId\n },\n payload: {\n ...res,\n }\n };\n\n sendMessage(response);\n}\n","import { anthropicLLM } from './anthropic';\nimport { groqLLM } from './groq';\nimport { LLMProvider, Component } from '../types';\nimport { logger } from '../utils/logger';\n\n/**\n * Generate next questions based on the original user prompt and generated component\n * Routes to the appropriate LLM provider (Anthropic or Groq)\n * Falls back to next provider if current provider fails or returns empty results\n */\nexport async function generateNextQuestions(\n\toriginalUserPrompt: string,\n\tcomponent: Component,\n\tcomponentData?: Record<string, unknown>,\n\tanthropicApiKey?: string,\n\tgroqApiKey?: string,\n\tllmProviders?: LLMProvider[],\n\tlogCollector?: any,\n\tconversationHistory?: string\n): Promise<string[]> {\n\ttry {\n\t\t// Determine which providers to use\n\t\tconst providers = llmProviders || ['anthropic'];\n\n\t\t// Try each provider in order\n\t\tfor (const provider of providers) {\n\t\t\ttry {\n\t\t\t\tlogger.info(`Generating next questions using provider: ${provider}`);\n\n\t\t\t\tlet result: string[] = [];\n\n\t\t\t\tif (provider === 'groq') {\n\t\t\t\t\tresult = await groqLLM.generateNextQuestions(\n\t\t\t\t\t\toriginalUserPrompt,\n\t\t\t\t\t\tcomponent,\n\t\t\t\t\t\tcomponentData,\n\t\t\t\t\t\tgroqApiKey,\n\t\t\t\t\t\tlogCollector,\n\t\t\t\t\t\tconversationHistory\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\t// Default to Anthropic\n\t\t\t\t\tresult = await anthropicLLM.generateNextQuestions(\n\t\t\t\t\t\toriginalUserPrompt,\n\t\t\t\t\t\tcomponent,\n\t\t\t\t\t\tcomponentData,\n\t\t\t\t\t\tanthropicApiKey,\n\t\t\t\t\t\tlogCollector,\n\t\t\t\t\t\tconversationHistory\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\t// If we got results, return them\n\t\t\t\tif (result && result.length > 0) {\n\t\t\t\t\tlogger.info(`Successfully generated ${result.length} questions with ${provider}`);\n\t\t\t\t\treturn result;\n\t\t\t\t}\n\n\t\t\t\tlogger.warn(`No questions generated from ${provider}, trying next provider...`);\n\t\t\t} catch (providerError) {\n\t\t\t\tlogger.warn(`Provider ${provider} failed:`, providerError);\n\t\t\t\tlogCollector?.warn(`Provider ${provider} failed, trying next provider...`);\n\t\t\t\t// Continue to next provider\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\n\t\t// All providers failed or returned empty results\n\t\tlogger.warn('All providers failed or returned no questions');\n\t\treturn [];\n\t} catch (error) {\n\t\tlogger.error('Error generating next questions:', error);\n\t\tlogCollector?.error(`Error generating next questions: ${(error as Error).message}`);\n\t\t// Return empty array on error\n\t\treturn [];\n\t}\n}\n","import { ActionsRequestMessageSchema, type LLMProvider, type Message } from '../types';\nimport { generateNextQuestions } from '../userResponse/next-questions';\nimport { logger } from '../utils/logger';\nimport { UILogCollector } from '../utils/log-collector';\nimport { ThreadManager } from '../threads';\nimport { CONTEXT_CONFIG } from '../config/context';\n\n/**\n * Handle incoming actions messages from runtime\n * Generates suggested next questions based on the original user prompt and generated component\n */\nexport async function handleActionsRequest(\n data: any,\n sendMessage: (message: Message) => void,\n anthropicApiKey?: string,\n groqApiKey?: string,\n llmProviders?: LLMProvider[]\n): Promise<void> {\n try {\n const actionsRequest = ActionsRequestMessageSchema.parse(data);\n const { id, payload } = actionsRequest;\n const { SA_RUNTIME } = payload;\n\n const wsId = actionsRequest.from.id || 'unknown';\n\n // SA_RUNTIME is required to fetch actions from UIBlock\n if (!SA_RUNTIME) {\n sendResponse(id, {\n success: false,\n error: 'SA_RUNTIME with threadId and uiBlockId is required'\n }, sendMessage, wsId);\n return;\n }\n\n const uiBlockId = SA_RUNTIME.uiBlockId;\n const threadId = SA_RUNTIME.threadId;\n\n // Get UIBlock from ThreadManager\n const threadManager = ThreadManager.getInstance();\n const thread = threadManager.getThread(threadId);\n\n if (!thread) {\n sendResponse(id, {\n success: false,\n error: `Thread '${threadId}' not found`\n }, sendMessage, wsId);\n return;\n }\n\n const uiBlock = thread.getUIBlock(uiBlockId);\n if (!uiBlock) {\n sendResponse(id, {\n success: false,\n error: `UIBlock '${uiBlockId}' not found in thread '${threadId}'`\n }, sendMessage, wsId);\n return;\n }\n\n // Create log collector with uiBlockId\n const logCollector = new UILogCollector(wsId, sendMessage, uiBlockId);\n\n // Extract data from UIBlock\n const userQuestion = uiBlock.getUserQuestion();\n const component = uiBlock.getComponentMetadata();\n const componentData = uiBlock.getComponentData();\n\n // Extract conversation context from thread (excluding current UIBlock)\n const conversationHistory = thread.getConversationContext(CONTEXT_CONFIG.MAX_CONVERSATION_CONTEXT_BLOCKS, uiBlockId);\n\n logCollector.info(`Generating actions for UIBlock: ${uiBlockId}`);\n logger.info(`Generating actions for component: ${component?.name || 'unknown'}`);\n\n // Use getOrFetchActions to manage action state\n const actions = await uiBlock.getOrFetchActions(async () => {\n // Generate next questions using extracted data from UIBlock and conversation history\n const nextQuestions = await generateNextQuestions(\n userQuestion,\n component as any,\n componentData,\n anthropicApiKey,\n groqApiKey,\n llmProviders,\n logCollector,\n conversationHistory\n );\n\n // Convert questions to actions format\n return nextQuestions.map((question: string, index: number) => ({\n id: `action_${index}_${Date.now()}`,\n name: question,\n type: 'next_question',\n question\n }));\n });\n\n logCollector.info(`Generated ${actions.length} actions successfully`);\n\n sendResponse(id, {\n success: true,\n data: {\n actions,\n componentName: component?.name,\n componentId: component?.id,\n uiBlockId,\n threadId\n }\n }, sendMessage, wsId);\n\n } catch (error) {\n logger.error('Failed to handle actions request:', error);\n sendResponse(null, {\n success: false,\n error: error instanceof Error ? error.message : 'Unknown error occurred'\n }, sendMessage);\n }\n}\n\n/**\n * Send actions response\n */\nfunction sendResponse(\n id: string | null,\n res: { success: boolean; error?: string; data?: any },\n sendMessage: (message: Message) => void,\n clientId?: string,\n): void {\n const response: Message = {\n id: id || 'unknown',\n type: 'ACTIONS_RES',\n from: { type: 'data-agent' },\n to: {\n type: 'runtime',\n id: clientId\n },\n payload: {\n ...res,\n }\n };\n\n sendMessage(response);\n}\n","import { Component, ComponentListResponseMessageSchema, ComponentSchema, ComponentsSchema, Message } from \"../types\";\nimport { logger } from \"../utils/logger\";\n\n\nexport async function handleComponentListResponse(\n data: any,\n storeComponents:(components: Component[])=>void\n ): Promise<void> {\n try {\n const componentListResponse = ComponentListResponseMessageSchema.parse(data);\n const { id, payload } = componentListResponse;\n \n const componentsList = payload.components;\n\n if(!componentsList){\n logger.error('Components list not found in the response');\n return;\n }\n\n const components = ComponentsSchema.parse(componentsList);\n storeComponents(components);\n\n return;\n } catch (error) {\n logger.error('Failed to handle user prompt request:', error);\n }\n } \n\n\n","import { UsersRequestMessageSchema, Message } from '../types';\nimport { getUserManager } from '../auth/user-storage';\nimport { logger } from '../utils/logger';\n\n/**\n * Handle unified users management request\n * Supports operations: create, update, delete, getAll, getOne\n * Only accepts requests from 'admin' type\n */\nexport async function handleUsersRequest(\n data: any,\n sendMessage: (message: Message) => void\n): Promise<void> {\n try {\n const request = UsersRequestMessageSchema.parse(data);\n const { id, payload, from } = request;\n const { operation, data: requestData } = payload;\n const username = requestData?.username;\n const password = requestData?.password;\n\n // Verify request is from admin\n if (from.type !== 'admin') {\n sendResponse(id, {\n success: false,\n error: 'Unauthorized: Only admin can manage users'\n }, sendMessage, from.id);\n logger.warn(`Unauthorized user management attempt from: ${from.type}`);\n return;\n }\n\n const userManager = getUserManager();\n\n // Route to appropriate operation handler\n switch (operation) {\n case 'create':\n await handleCreate(id, username, password, userManager, sendMessage, from.id);\n break;\n\n case 'update':\n await handleUpdate(id, username, password, userManager, sendMessage, from.id);\n break;\n\n case 'delete':\n await handleDelete(id, username, userManager, sendMessage, from.id);\n break;\n\n case 'getAll':\n await handleGetAll(id, userManager, sendMessage, from.id);\n break;\n\n case 'getOne':\n await handleGetOne(id, username, userManager, sendMessage, from.id);\n break;\n\n default:\n sendResponse(id, {\n success: false,\n error: `Unknown operation: ${operation}`\n }, sendMessage, from.id);\n }\n\n } catch (error) {\n logger.error('Failed to handle users request:', error);\n sendResponse(null, {\n success: false,\n error: error instanceof Error ? error.message : 'Unknown error occurred'\n }, sendMessage);\n }\n}\n\n/**\n * Handle create user operation\n */\nasync function handleCreate(\n id: string,\n username: string | undefined,\n password: string | undefined,\n userManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!username || username.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Username is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n if (!password || password.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Password is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n // Check if user already exists\n if (userManager.userExists(username)) {\n sendResponse(id, {\n success: false,\n error: `User '${username}' already exists`\n }, sendMessage, clientId);\n return;\n }\n\n let wsIds: string[] = [];\n if (clientId) {\n wsIds.push(clientId);\n }\n\n // Create user\n const newUser = userManager.createUser({\n username,\n password: password,\n wsIds\n });\n\n logger.info(`User created by admin: ${username}`);\n\n sendResponse(id, {\n success: true,\n data: {\n username: newUser.username,\n message: `User '${username}' created successfully`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Handle update user operation\n */\nasync function handleUpdate(\n id: string,\n username: string | undefined,\n password: string | undefined,\n userManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!username || username.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Username is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n // Check if user exists\n if (!userManager.userExists(username)) {\n sendResponse(id, {\n success: false,\n error: `User '${username}' not found`\n }, sendMessage, clientId);\n return;\n }\n\n // Build update object\n const updates: any = {};\n\n // Only update password if provided\n if (password && password.trim().length > 0) {\n updates.password = password;\n }\n\n // If nothing to update, return error\n if (Object.keys(updates).length === 0) {\n sendResponse(id, {\n success: false,\n error: 'No fields to update. Please provide password or other valid fields.'\n }, sendMessage, clientId);\n return;\n }\n\n // Update user\n const updatedUser = userManager.updateUser(username, updates);\n\n logger.info(`User updated by admin: ${username}`);\n\n sendResponse(id, {\n success: true,\n data: {\n username: updatedUser.username,\n message: `User '${username}' updated successfully`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Handle delete user operation\n */\nasync function handleDelete(\n id: string,\n username: string | undefined,\n userManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!username || username.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Username is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n // Check if user exists\n if (!userManager.userExists(username)) {\n sendResponse(id, {\n success: false,\n error: `User '${username}' not found`\n }, sendMessage, clientId);\n return;\n }\n\n // Delete user\n const deleted = userManager.deleteUser(username);\n\n if (!deleted) {\n sendResponse(id, {\n success: false,\n error: `Failed to delete user '${username}'`\n }, sendMessage, clientId);\n return;\n }\n\n logger.info(`User deleted by admin: ${username}`);\n\n sendResponse(id, {\n success: true,\n data: {\n username: username,\n message: `User '${username}' deleted successfully`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Handle get all users operation\n */\nasync function handleGetAll(\n id: string,\n userManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n const users = userManager.getAllUsers();\n\n // Remove sensitive information like passwords\n const sanitizedUsers = users.map((user: any) => ({\n username: user.username,\n wsIds: user.wsIds || []\n }));\n\n logger.info(`Admin retrieved all users (count: ${sanitizedUsers.length})`);\n\n sendResponse(id, {\n success: true,\n data: {\n users: sanitizedUsers,\n count: sanitizedUsers.length,\n message: `Retrieved ${sanitizedUsers.length} users`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Handle get one user operation\n */\nasync function handleGetOne(\n id: string,\n username: string | undefined,\n userManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!username || username.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Username is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n // Check if user exists\n if (!userManager.userExists(username)) {\n sendResponse(id, {\n success: false,\n error: `User '${username}' not found`\n }, sendMessage, clientId);\n return;\n }\n\n const user = userManager.getUser(username);\n\n // Remove sensitive information\n const sanitizedUser = {\n username: user.username,\n wsIds: user.wsIds || []\n };\n\n logger.info(`Admin retrieved user: ${username}`);\n\n sendResponse(id, {\n success: true,\n data: {\n user: sanitizedUser,\n message: `Retrieved user '${username}'`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Send users response\n */\nfunction sendResponse(\n id: string | null,\n res: { success: boolean; error?: string; data?: any },\n sendMessage: (message: Message) => void,\n clientId?: string,\n): void {\n const response: Message = {\n id: id || 'unknown',\n type: 'USERS_RES',\n from: { type: 'data-agent' },\n to: {\n type: 'admin',\n id: clientId\n },\n payload: {\n ...res,\n }\n };\n\n sendMessage(response);\n}\n","import { DashboardManager } from './dashboard-manager';\nimport { logger } from '../utils/logger';\n\nlet dashboardManager: DashboardManager | null = null;\n\n/**\n * Set the dashboard manager instance\n * @param manager - DashboardManager instance to use\n */\nexport function setDashboardManager(manager: DashboardManager): void {\n dashboardManager = manager;\n logger.info('DashboardManager instance set');\n}\n\n/**\n * Get the dashboard manager instance\n * @throws Error if dashboard manager is not initialized\n * @returns DashboardManager instance\n */\nexport function getDashboardManager(): DashboardManager {\n if (!dashboardManager) {\n throw new Error('DashboardManager not initialized. Call setDashboardManager first.');\n }\n return dashboardManager;\n}\n","import { DashboardsRequestMessageSchema, Message } from '../types';\nimport { getDashboardManager } from '../dashboards/dashboard-storage';\nimport { logger } from '../utils/logger';\n\n/**\n * Handle unified dashboards management request\n * Supports operations: create, update, delete, getAll, getOne\n * Only accepts requests from 'admin' type\n */\nexport async function handleDashboardsRequest(\n data: any,\n sendMessage: (message: Message) => void\n): Promise<void> {\n try {\n const request = DashboardsRequestMessageSchema.parse(data);\n const { id, payload, from } = request;\n const { operation, data: requestData } = payload;\n const dashboardId = requestData?.dashboardId;\n const dashboard = requestData?.dashboard;\n\n // Verify request is from admin\n if (from.type !== 'admin') {\n sendResponse(id, {\n success: false,\n error: 'Unauthorized: Only admin can manage dashboards'\n }, sendMessage, from.id);\n logger.warn(`Unauthorized dashboard management attempt from: ${from.type}`);\n return;\n }\n\n const dashboardManager = getDashboardManager();\n\n // Route to appropriate operation handler\n switch (operation) {\n case 'create':\n await handleCreate(id, dashboardId, dashboard, dashboardManager, sendMessage, from.id);\n break;\n\n case 'update':\n await handleUpdate(id, dashboardId, dashboard, dashboardManager, sendMessage, from.id);\n break;\n\n case 'delete':\n await handleDelete(id, dashboardId, dashboardManager, sendMessage, from.id);\n break;\n\n case 'getAll':\n await handleGetAll(id, dashboardManager, sendMessage, from.id);\n break;\n\n case 'getOne':\n await handleGetOne(id, dashboardId, dashboardManager, sendMessage, from.id);\n break;\n\n default:\n sendResponse(id, {\n success: false,\n error: `Unknown operation: ${operation}`\n }, sendMessage, from.id);\n }\n\n } catch (error) {\n logger.error('Failed to handle dashboards request:', error);\n sendResponse(null, {\n success: false,\n error: error instanceof Error ? error.message : 'Unknown error occurred'\n }, sendMessage);\n }\n}\n\n/**\n * Handle create dashboard operation\n */\nasync function handleCreate(\n id: string,\n dashboardId: string | undefined,\n dashboard: any,\n dashboardManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!dashboardId || dashboardId.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Dashboard ID is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n if (!dashboard) {\n sendResponse(id, {\n success: false,\n error: 'Dashboard data is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const createdDashboard = dashboardManager.createDashboard(dashboardId, dashboard);\n\n sendResponse(id, {\n success: true,\n data: {\n dashboardId,\n dashboard: createdDashboard,\n message: `Dashboard '${dashboardId}' created successfully`\n }\n }, sendMessage, clientId);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to create dashboard'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle update dashboard operation\n */\nasync function handleUpdate(\n id: string,\n dashboardId: string | undefined,\n dashboard: any,\n dashboardManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!dashboardId || dashboardId.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Dashboard ID is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n if (!dashboard) {\n sendResponse(id, {\n success: false,\n error: 'Dashboard data is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const updatedDashboard = dashboardManager.updateDashboard(dashboardId, dashboard);\n\n if (!updatedDashboard) {\n sendResponse(id, {\n success: false,\n error: `Dashboard '${dashboardId}' not found`\n }, sendMessage, clientId);\n return;\n }\n\n sendResponse(id, {\n success: true,\n data: {\n dashboardId,\n dashboard: updatedDashboard,\n message: `Dashboard '${dashboardId}' updated successfully`\n }\n }, sendMessage, clientId);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to update dashboard'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle delete dashboard operation\n */\nasync function handleDelete(\n id: string,\n dashboardId: string | undefined,\n dashboardManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!dashboardId || dashboardId.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Dashboard ID is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n const deleted = dashboardManager.deleteDashboard(dashboardId);\n\n if (!deleted) {\n sendResponse(id, {\n success: false,\n error: `Dashboard '${dashboardId}' not found`\n }, sendMessage, clientId);\n return;\n }\n\n sendResponse(id, {\n success: true,\n data: {\n dashboardId,\n message: `Dashboard '${dashboardId}' deleted successfully`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Handle get all dashboards operation\n */\nasync function handleGetAll(\n id: string,\n dashboardManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n const dashboards = dashboardManager.getAllDashboards();\n\n logger.info(`Admin retrieved all dashboards (count: ${dashboards.length})`);\n\n sendResponse(id, {\n success: true,\n data: {\n dashboards,\n count: dashboards.length,\n message: `Retrieved ${dashboards.length} dashboards`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Handle get one dashboard operation\n */\nasync function handleGetOne(\n id: string,\n dashboardId: string | undefined,\n dashboardManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!dashboardId || dashboardId.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Dashboard ID is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n const dashboard = dashboardManager.getDashboard(dashboardId);\n\n if (!dashboard) {\n sendResponse(id, {\n success: false,\n error: `Dashboard '${dashboardId}' not found`\n }, sendMessage, clientId);\n return;\n }\n\n logger.info(`Admin retrieved dashboard: ${dashboardId}`);\n\n sendResponse(id, {\n success: true,\n data: {\n dashboardId,\n dashboard,\n message: `Retrieved dashboard '${dashboardId}'`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Send dashboards response\n */\nfunction sendResponse(\n id: string | null,\n res: { success: boolean; error?: string; data?: any },\n sendMessage: (message: Message) => void,\n clientId?: string,\n): void {\n const response: Message = {\n id: id || 'unknown',\n type: 'DASHBOARDS_RES',\n from: { type: 'data-agent' },\n to: {\n type: 'admin',\n id: clientId\n },\n payload: {\n ...res,\n }\n };\n\n sendMessage(response);\n}\n","import { ReportManager } from './report-manager';\n\n/**\n * Global report manager instance\n */\nlet reportManager: ReportManager | null = null;\n\n/**\n * Get the global report manager instance\n * @returns ReportManager instance\n * @throws Error if report manager is not initialized\n */\nexport function getReportManager(): ReportManager {\n if (!reportManager) {\n throw new Error('Report manager not initialized. Call setReportManager first.');\n }\n return reportManager;\n}\n\n/**\n * Set the global report manager instance\n * @param manager - ReportManager instance to set\n */\nexport function setReportManager(manager: ReportManager): void {\n reportManager = manager;\n}\n","import { ReportsRequestMessageSchema, Message } from '../types';\nimport { getReportManager } from '../reports/report-storage';\nimport { logger } from '../utils/logger';\n\n/**\n * Handle unified reports management request\n * Supports operations: create, update, delete, getAll, getOne\n * Only accepts requests from 'admin' type\n */\nexport async function handleReportsRequest(\n data: any,\n sendMessage: (message: Message) => void\n): Promise<void> {\n try {\n const request = ReportsRequestMessageSchema.parse(data);\n const { id, payload, from } = request;\n const { operation, data: requestData } = payload;\n const reportId = requestData?.reportId;\n const report = requestData?.report;\n\n // Verify request is from admin\n if (from.type !== 'admin') {\n sendResponse(id, {\n success: false,\n error: 'Unauthorized: Only admin can manage reports'\n }, sendMessage, from.id);\n logger.warn(`Unauthorized report management attempt from: ${from.type}`);\n return;\n }\n\n const reportManager = getReportManager();\n\n // Route to appropriate operation handler\n switch (operation) {\n case 'create':\n await handleCreate(id, reportId, report, reportManager, sendMessage, from.id);\n break;\n\n case 'update':\n await handleUpdate(id, reportId, report, reportManager, sendMessage, from.id);\n break;\n\n case 'delete':\n await handleDelete(id, reportId, reportManager, sendMessage, from.id);\n break;\n\n case 'getAll':\n await handleGetAll(id, reportManager, sendMessage, from.id);\n break;\n\n case 'getOne':\n await handleGetOne(id, reportId, reportManager, sendMessage, from.id);\n break;\n\n default:\n sendResponse(id, {\n success: false,\n error: `Unknown operation: ${operation}`\n }, sendMessage, from.id);\n }\n\n } catch (error) {\n logger.error('Failed to handle reports request:', error);\n sendResponse(null, {\n success: false,\n error: error instanceof Error ? error.message : 'Unknown error occurred'\n }, sendMessage);\n }\n}\n\n/**\n * Handle create report operation\n */\nasync function handleCreate(\n id: string,\n reportId: string | undefined,\n report: any,\n reportManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!reportId || reportId.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Report ID is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n if (!report) {\n sendResponse(id, {\n success: false,\n error: 'Report data is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const createdReport = reportManager.createReport(reportId, report);\n\n sendResponse(id, {\n success: true,\n data: {\n reportId,\n report: createdReport,\n message: `Report '${reportId}' created successfully`\n }\n }, sendMessage, clientId);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to create report'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle update report operation\n */\nasync function handleUpdate(\n id: string,\n reportId: string | undefined,\n report: any,\n reportManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!reportId || reportId.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Report ID is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n if (!report) {\n sendResponse(id, {\n success: false,\n error: 'Report data is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const updatedReport = reportManager.updateReport(reportId, report);\n\n if (!updatedReport) {\n sendResponse(id, {\n success: false,\n error: `Report '${reportId}' not found`\n }, sendMessage, clientId);\n return;\n }\n\n sendResponse(id, {\n success: true,\n data: {\n reportId,\n report: updatedReport,\n message: `Report '${reportId}' updated successfully`\n }\n }, sendMessage, clientId);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to update report'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle delete report operation\n */\nasync function handleDelete(\n id: string,\n reportId: string | undefined,\n reportManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!reportId || reportId.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Report ID is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n const deleted = reportManager.deleteReport(reportId);\n\n if (!deleted) {\n sendResponse(id, {\n success: false,\n error: `Report '${reportId}' not found`\n }, sendMessage, clientId);\n return;\n }\n\n sendResponse(id, {\n success: true,\n data: {\n reportId,\n message: `Report '${reportId}' deleted successfully`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Handle get all reports operation\n */\nasync function handleGetAll(\n id: string,\n reportManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n const reports = reportManager.getAllReports();\n\n logger.info(`Admin retrieved all reports (count: ${reports.length})`);\n\n sendResponse(id, {\n success: true,\n data: {\n reports,\n count: reports.length,\n message: `Retrieved ${reports.length} reports`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Handle get one report operation\n */\nasync function handleGetOne(\n id: string,\n reportId: string | undefined,\n reportManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!reportId || reportId.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Report ID is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n const report = reportManager.getReport(reportId);\n\n if (!report) {\n sendResponse(id, {\n success: false,\n error: `Report '${reportId}' not found`\n }, sendMessage, clientId);\n return;\n }\n\n logger.info(`Admin retrieved report: ${reportId}`);\n\n sendResponse(id, {\n success: true,\n data: {\n reportId,\n report,\n message: `Retrieved report '${reportId}'`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Send reports response\n */\nfunction sendResponse(\n id: string | null,\n res: { success: boolean; error?: string; data?: any },\n sendMessage: (message: Message) => void,\n clientId?: string,\n): void {\n const response: Message = {\n id: id || 'unknown',\n type: 'REPORTS_RES',\n from: { type: 'data-agent' },\n to: {\n type: 'admin',\n id: clientId\n },\n payload: {\n ...res,\n }\n };\n\n sendMessage(response);\n}\n","import fs from 'fs';\nimport path from 'path';\nimport os from 'os';\nimport { logger } from '../utils/logger';\n\nexport interface User {\n username: string;\n password: string;\n wsIds: string[];\n}\n\nexport interface UsersData {\n users: User[];\n}\n\n/**\n * UserManager class to handle CRUD operations on users with file persistence\n * and in-memory caching. Changes are synced to file periodically.\n */\nexport class UserManager {\n private users: User[] = [];\n private filePath: string;\n private hasChanged: boolean = false;\n private syncInterval: ReturnType<typeof setInterval> | null = null;\n private syncIntervalMs: number;\n private isInitialized: boolean = false;\n\n /**\n * Initialize UserManager with file path and sync interval\n * @param projectId - Project ID to use in file path (default: 'snowflake-dataset')\n * @param syncIntervalMs - Interval in milliseconds to sync changes to file (default: 5000ms)\n */\n constructor(projectId: string = 'snowflake-dataset', syncIntervalMs: number = 5000) {\n this.filePath = path.join(os.homedir(), '.superatom', 'projects', projectId, 'users.json');\n this.syncIntervalMs = syncIntervalMs;\n }\n\n /**\n * Initialize the UserManager by loading users from file and starting sync interval\n */\n async init(): Promise<void> {\n if (this.isInitialized) {\n return;\n }\n\n try {\n // Load users from file into memory\n await this.loadUsersFromFile();\n logger.info(`UserManager initialized with ${this.users.length} users`);\n\n // Start the sync interval\n this.startSyncInterval();\n this.isInitialized = true;\n } catch (error) {\n logger.error('Failed to initialize UserManager:', error);\n throw error;\n }\n }\n\n /**\n * Load users from the JSON file into memory\n */\n private async loadUsersFromFile(): Promise<void> {\n try {\n // Create directory structure if it doesn't exist\n const dir = path.dirname(this.filePath);\n if (!fs.existsSync(dir)) {\n logger.info(`Creating directory structure: ${dir}`);\n fs.mkdirSync(dir, { recursive: true });\n }\n\n // Create file with empty users array if it doesn't exist\n if (!fs.existsSync(this.filePath)) {\n logger.info(`Users file does not exist at ${this.filePath}, creating with empty users`);\n const initialData: UsersData = { users: [] };\n fs.writeFileSync(this.filePath, JSON.stringify(initialData, null, 4));\n this.users = [];\n this.hasChanged = false;\n return;\n }\n\n const fileContent = fs.readFileSync(this.filePath, 'utf-8');\n const data = JSON.parse(fileContent) as UsersData;\n\n this.users = Array.isArray(data.users) ? data.users : [];\n this.hasChanged = false;\n logger.debug(`Loaded ${this.users.length} users from file`);\n } catch (error) {\n logger.error('Failed to load users from file:', error);\n throw new Error(`Failed to load users from file: ${error instanceof Error ? error.message : 'Unknown error'}`);\n }\n }\n\n /**\n * Save users from memory to the JSON file\n */\n private async saveUsersToFile(): Promise<void> {\n if (!this.hasChanged) {\n return;\n }\n\n try {\n // Create directory if it doesn't exist\n const dir = path.dirname(this.filePath);\n if (!fs.existsSync(dir)) {\n fs.mkdirSync(dir, { recursive: true });\n }\n\n const data: UsersData = { users: this.users };\n fs.writeFileSync(this.filePath, JSON.stringify(data, null, 4));\n\n this.hasChanged = false;\n logger.debug(`Synced ${this.users.length} users to file`);\n } catch (error) {\n logger.error('Failed to save users to file:', error);\n throw new Error(`Failed to save users to file: ${error instanceof Error ? error.message : 'Unknown error'}`);\n }\n }\n\n /**\n * Start the periodic sync interval\n */\n private startSyncInterval(): void {\n if (this.syncInterval) {\n return;\n }\n\n this.syncInterval = setInterval(async () => {\n if (this.hasChanged) {\n try {\n await this.saveUsersToFile();\n logger.debug('Auto-sync: Users saved to file');\n } catch (error) {\n logger.error('Auto-sync failed:', error);\n }\n }\n }, this.syncIntervalMs);\n\n logger.debug(`Sync interval started (${this.syncIntervalMs}ms)`);\n }\n\n /**\n * Stop the periodic sync interval\n */\n public stopSyncInterval(): void {\n if (this.syncInterval) {\n clearInterval(this.syncInterval);\n this.syncInterval = null;\n logger.debug('Sync interval stopped');\n }\n }\n\n /**\n * Force sync users to file immediately\n */\n public async forceSync(): Promise<void> {\n await this.saveUsersToFile();\n }\n\n /**\n * Create a new user\n * @param user - User object to create\n * @returns The created user\n */\n public createUser(user: User): User {\n if (this.users.some(u => u.username === user.username)) {\n throw new Error(`User with username ${user.username} already exists`);\n }\n\n this.users.push(user);\n this.hasChanged = true;\n logger.debug(`User created: ${user.username}`);\n\n return user;\n }\n\n /**\n * Read a user by username\n * @param username - Username to retrieve\n * @returns The user if found, undefined otherwise\n */\n public getUser(username: string): User | undefined {\n return this.users.find(u => u.username === username);\n }\n\n /**\n * Read all users\n * @returns Array of all users\n */\n public getAllUsers(): User[] {\n return [...this.users];\n }\n\n /**\n * Find users by a predicate function\n * @param predicate - Function to filter users\n * @returns Array of matching users\n */\n public findUsers(predicate: (user: User) => boolean): User[] {\n return this.users.filter(predicate);\n }\n\n /**\n * Update an existing user by username\n * @param username - Username of user to update\n * @param updates - Partial user object with fields to update\n * @returns The updated user\n */\n public updateUser(username: string, updates: Partial<User>): User {\n const userIndex = this.users.findIndex(u => u.username === username);\n if (userIndex === -1) {\n throw new Error(`User with username ${username} not found`);\n }\n\n const updatedUser = { ...this.users[userIndex], ...updates };\n this.users[userIndex] = updatedUser;\n this.hasChanged = true;\n logger.debug(`User updated: ${username}`);\n\n return updatedUser;\n }\n\n /**\n * Delete a user by username\n * @param username - Username of user to delete\n * @returns true if user was deleted, false if not found\n */\n public deleteUser(username: string): boolean {\n const initialLength = this.users.length;\n this.users = this.users.filter(u => u.username !== username);\n\n if (this.users.length < initialLength) {\n this.hasChanged = true;\n logger.debug(`User deleted: ${username}`);\n return true;\n }\n return false;\n }\n\n /**\n * Delete all users\n */\n public deleteAllUsers(): void {\n if (this.users.length > 0) {\n this.users = [];\n this.hasChanged = true;\n logger.debug('All users deleted');\n }\n }\n\n /**\n * Get the count of users\n * @returns Number of users in memory\n */\n public getUserCount(): number {\n return this.users.length;\n }\n\n /**\n * Check if a user exists\n * @param username - Username to check\n * @returns true if user exists, false otherwise\n */\n public userExists(username: string): boolean {\n return this.users.some(u => u.username === username);\n }\n\n /**\n * Add a WebSocket ID to a user's wsIds array\n * @param username - Username to update\n * @param wsId - WebSocket ID to add\n * @returns true if successful, false if user not found\n */\n public addWsId(username: string, wsId: string): boolean {\n const user = this.getUser(username);\n if (!user) {\n return false;\n }\n\n if(!user.wsIds || !Array.isArray(user.wsIds)) {\n user.wsIds = [];\n }\n\n if (!user.wsIds.includes(wsId)) {\n user.wsIds.push(wsId);\n this.hasChanged = true;\n logger.debug(`WebSocket ID added to user ${username}: ${wsId}`);\n }\n\n return true;\n }\n\n /**\n * Remove a WebSocket ID from a user's wsIds array\n * @param username - Username to update\n * @param wsId - WebSocket ID to remove\n * @returns true if successful, false if user not found\n */\n public removeWsId(username: string, wsId: string): boolean {\n const user = this.getUser(username);\n if (!user) {\n return false;\n }\n\n if(!user.wsIds || !Array.isArray(user.wsIds)) {\n return false;\n }\n\n const initialLength = user.wsIds.length;\n user.wsIds = user.wsIds.filter(id => id !== wsId);\n\n if (user.wsIds.length < initialLength) {\n this.hasChanged = true;\n logger.debug(`WebSocket ID removed from user ${username}: ${wsId}`);\n }\n\n return true;\n }\n\n /**\n * Get the change status\n * @returns true if there are unsaved changes, false otherwise\n */\n public hasUnsavedChanges(): boolean {\n return this.hasChanged;\n }\n\n /**\n * Cleanup resources and stop sync interval\n */\n public async destroy(): Promise<void> {\n this.stopSyncInterval();\n // Final sync before cleanup\n if (this.hasChanged) {\n await this.saveUsersToFile();\n }\n logger.info('UserManager destroyed');\n }\n}\n","import fs from 'fs';\nimport path from 'path';\nimport os from 'os';\nimport { logger } from '../utils/logger';\nimport { DSLRendererProps, DSLRendererPropsSchema } from './types';\n\n/**\n * DashboardManager class to handle CRUD operations on dashboards\n * All operations read/write directly to files (no in-memory caching)\n */\nexport class DashboardManager {\n private dashboardsBasePath: string;\n private projectId: string;\n\n /**\n * Initialize DashboardManager with project ID\n * @param projectId - Project ID to use in file path\n */\n constructor(projectId: string = 'snowflake-dataset') {\n this.projectId = projectId;\n this.dashboardsBasePath = path.join(\n os.homedir(),\n '.superatom',\n 'projects',\n projectId,\n 'dashboards'\n );\n }\n\n /**\n * Get the file path for a specific dashboard\n * @param dashboardId - Dashboard ID\n * @returns Full path to dashboard data.json file\n */\n private getDashboardPath(dashboardId: string): string {\n return path.join(this.dashboardsBasePath, dashboardId, 'data.json');\n }\n\n /**\n * Create a new dashboard\n * @param dashboardId - Unique dashboard ID\n * @param dashboard - Dashboard data\n * @returns Created dashboard with metadata\n */\n createDashboard(dashboardId: string, dashboard: DSLRendererProps): DSLRendererProps {\n const dashboardPath = this.getDashboardPath(dashboardId);\n const dashboardDir = path.dirname(dashboardPath);\n\n // Check if dashboard already exists\n if (fs.existsSync(dashboardPath)) {\n throw new Error(`Dashboard '${dashboardId}' already exists`);\n }\n\n // Validate dashboard structure\n const validated = DSLRendererPropsSchema.parse(dashboard);\n\n // Create directory structure\n fs.mkdirSync(dashboardDir, { recursive: true });\n\n // Write dashboard to file\n fs.writeFileSync(dashboardPath, JSON.stringify(validated, null, 4));\n\n logger.info(`Dashboard created: ${dashboardId}`);\n return validated;\n }\n\n /**\n * Get a specific dashboard by ID\n * @param dashboardId - Dashboard ID\n * @returns Dashboard data or null if not found\n */\n getDashboard(dashboardId: string): DSLRendererProps | null {\n const dashboardPath = this.getDashboardPath(dashboardId);\n\n if (!fs.existsSync(dashboardPath)) {\n logger.warn(`Dashboard not found: ${dashboardId}`);\n return null;\n }\n\n try {\n const fileContent = fs.readFileSync(dashboardPath, 'utf-8');\n const dashboard = JSON.parse(fileContent) as DSLRendererProps;\n\n // Validate structure\n const validated = DSLRendererPropsSchema.parse(dashboard);\n return validated;\n } catch (error) {\n logger.error(`Failed to read dashboard ${dashboardId}:`, error);\n return null;\n }\n }\n\n /**\n * Get all dashboards\n * @returns Array of dashboard objects with their IDs\n */\n getAllDashboards(): Array<{ dashboardId: string; dashboard: DSLRendererProps }> {\n // Create base directory if it doesn't exist\n if (!fs.existsSync(this.dashboardsBasePath)) {\n fs.mkdirSync(this.dashboardsBasePath, { recursive: true });\n return [];\n }\n\n const dashboards: Array<{ dashboardId: string; dashboard: DSLRendererProps }> = [];\n\n try {\n const dashboardDirs = fs.readdirSync(this.dashboardsBasePath);\n\n for (const dashboardId of dashboardDirs) {\n const dashboardPath = this.getDashboardPath(dashboardId);\n\n if (fs.existsSync(dashboardPath)) {\n const dashboard = this.getDashboard(dashboardId);\n if (dashboard) {\n dashboards.push({ dashboardId, dashboard });\n }\n }\n }\n\n logger.debug(`Retrieved ${dashboards.length} dashboards`);\n return dashboards;\n } catch (error) {\n logger.error('Failed to get all dashboards:', error);\n return [];\n }\n }\n\n /**\n * Update an existing dashboard\n * @param dashboardId - Dashboard ID\n * @param dashboard - Updated dashboard data\n * @returns Updated dashboard or null if not found\n */\n updateDashboard(dashboardId: string, dashboard: DSLRendererProps): DSLRendererProps | null {\n const dashboardPath = this.getDashboardPath(dashboardId);\n\n if (!fs.existsSync(dashboardPath)) {\n logger.warn(`Dashboard not found for update: ${dashboardId}`);\n return null;\n }\n\n try {\n // Validate dashboard structure\n const validated = DSLRendererPropsSchema.parse(dashboard);\n\n // Write updated dashboard to file\n fs.writeFileSync(dashboardPath, JSON.stringify(validated, null, 4));\n\n logger.info(`Dashboard updated: ${dashboardId}`);\n return validated;\n } catch (error) {\n logger.error(`Failed to update dashboard ${dashboardId}:`, error);\n throw error;\n }\n }\n\n /**\n * Delete a dashboard\n * @param dashboardId - Dashboard ID\n * @returns True if deleted, false if not found\n */\n deleteDashboard(dashboardId: string): boolean {\n const dashboardPath = this.getDashboardPath(dashboardId);\n const dashboardDir = path.dirname(dashboardPath);\n\n if (!fs.existsSync(dashboardPath)) {\n logger.warn(`Dashboard not found for deletion: ${dashboardId}`);\n return false;\n }\n\n try {\n // Delete the entire dashboard directory\n fs.rmSync(dashboardDir, { recursive: true, force: true });\n\n logger.info(`Dashboard deleted: ${dashboardId}`);\n return true;\n } catch (error) {\n logger.error(`Failed to delete dashboard ${dashboardId}:`, error);\n return false;\n }\n }\n\n /**\n * Check if a dashboard exists\n * @param dashboardId - Dashboard ID\n * @returns True if dashboard exists, false otherwise\n */\n dashboardExists(dashboardId: string): boolean {\n const dashboardPath = this.getDashboardPath(dashboardId);\n return fs.existsSync(dashboardPath);\n }\n\n /**\n * Get dashboard count\n * @returns Number of dashboards\n */\n getDashboardCount(): number {\n if (!fs.existsSync(this.dashboardsBasePath)) {\n return 0;\n }\n\n try {\n const dashboardDirs = fs.readdirSync(this.dashboardsBasePath);\n return dashboardDirs.filter((dir) => {\n const dashboardPath = this.getDashboardPath(dir);\n return fs.existsSync(dashboardPath);\n }).length;\n } catch (error) {\n logger.error('Failed to get dashboard count:', error);\n return 0;\n }\n }\n}\n","import fs from 'fs';\nimport path from 'path';\nimport os from 'os';\nimport { logger } from '../utils/logger';\nimport { DSLRendererProps, DSLRendererPropsSchema } from './types';\n\n/**\n * ReportManager class to handle CRUD operations on reports\n * All operations read/write directly to files (no in-memory caching)\n */\nexport class ReportManager {\n private reportsBasePath: string;\n private projectId: string;\n\n /**\n * Initialize ReportManager with project ID\n * @param projectId - Project ID to use in file path\n */\n constructor(projectId: string = 'snowflake-dataset') {\n this.projectId = projectId;\n this.reportsBasePath = path.join(\n os.homedir(),\n '.superatom',\n 'projects',\n projectId,\n 'reports'\n );\n }\n\n /**\n * Get the file path for a specific report\n * @param reportId - Report ID\n * @returns Full path to report data.json file\n */\n private getReportPath(reportId: string): string {\n return path.join(this.reportsBasePath, reportId, 'data.json');\n }\n\n /**\n * Create a new report\n * @param reportId - Unique report ID\n * @param report - Report data\n * @returns Created report with metadata\n */\n createReport(reportId: string, report: DSLRendererProps): DSLRendererProps {\n const reportPath = this.getReportPath(reportId);\n const reportDir = path.dirname(reportPath);\n\n // Check if report already exists\n if (fs.existsSync(reportPath)) {\n throw new Error(`Report '${reportId}' already exists`);\n }\n\n // Validate report structure\n const validated = DSLRendererPropsSchema.parse(report);\n\n // Create directory structure\n fs.mkdirSync(reportDir, { recursive: true });\n\n // Write report to file\n fs.writeFileSync(reportPath, JSON.stringify(validated, null, 4));\n\n logger.info(`Report created: ${reportId}`);\n return validated;\n }\n\n /**\n * Get a specific report by ID\n * @param reportId - Report ID\n * @returns Report data or null if not found\n */\n getReport(reportId: string): DSLRendererProps | null {\n const reportPath = this.getReportPath(reportId);\n\n if (!fs.existsSync(reportPath)) {\n logger.warn(`Report not found: ${reportId}`);\n return null;\n }\n\n try {\n const fileContent = fs.readFileSync(reportPath, 'utf-8');\n const report = JSON.parse(fileContent) as DSLRendererProps;\n\n // Validate structure\n const validated = DSLRendererPropsSchema.parse(report);\n return validated;\n } catch (error) {\n logger.error(`Failed to read report ${reportId}:`, error);\n return null;\n }\n }\n\n /**\n * Get all reports\n * @returns Array of report objects with their IDs\n */\n getAllReports(): Array<{ reportId: string; report: DSLRendererProps }> {\n // Create base directory if it doesn't exist\n if (!fs.existsSync(this.reportsBasePath)) {\n fs.mkdirSync(this.reportsBasePath, { recursive: true });\n return [];\n }\n\n const reports: Array<{ reportId: string; report: DSLRendererProps }> = [];\n\n try {\n const reportDirs = fs.readdirSync(this.reportsBasePath);\n\n for (const reportId of reportDirs) {\n const reportPath = this.getReportPath(reportId);\n\n if (fs.existsSync(reportPath)) {\n const report = this.getReport(reportId);\n if (report) {\n reports.push({ reportId, report });\n }\n }\n }\n\n logger.debug(`Retrieved ${reports.length} reports`);\n return reports;\n } catch (error) {\n logger.error('Failed to get all reports:', error);\n return [];\n }\n }\n\n /**\n * Update an existing report\n * @param reportId - Report ID\n * @param report - Updated report data\n * @returns Updated report or null if not found\n */\n updateReport(reportId: string, report: DSLRendererProps): DSLRendererProps | null {\n const reportPath = this.getReportPath(reportId);\n\n if (!fs.existsSync(reportPath)) {\n logger.warn(`Report not found for update: ${reportId}`);\n return null;\n }\n\n try {\n // Validate report structure\n const validated = DSLRendererPropsSchema.parse(report);\n\n // Write updated report to file\n fs.writeFileSync(reportPath, JSON.stringify(validated, null, 4));\n\n logger.info(`Report updated: ${reportId}`);\n return validated;\n } catch (error) {\n logger.error(`Failed to update report ${reportId}:`, error);\n throw error;\n }\n }\n\n /**\n * Delete a report\n * @param reportId - Report ID\n * @returns True if deleted, false if not found\n */\n deleteReport(reportId: string): boolean {\n const reportPath = this.getReportPath(reportId);\n const reportDir = path.dirname(reportPath);\n\n if (!fs.existsSync(reportPath)) {\n logger.warn(`Report not found for deletion: ${reportId}`);\n return false;\n }\n\n try {\n // Delete the entire report directory\n fs.rmSync(reportDir, { recursive: true, force: true });\n\n logger.info(`Report deleted: ${reportId}`);\n return true;\n } catch (error) {\n logger.error(`Failed to delete report ${reportId}:`, error);\n return false;\n }\n }\n\n /**\n * Check if a report exists\n * @param reportId - Report ID\n * @returns True if report exists, false otherwise\n */\n reportExists(reportId: string): boolean {\n const reportPath = this.getReportPath(reportId);\n return fs.existsSync(reportPath);\n }\n\n /**\n * Get report count\n * @returns Number of reports\n */\n getReportCount(): number {\n if (!fs.existsSync(this.reportsBasePath)) {\n return 0;\n }\n\n try {\n const reportDirs = fs.readdirSync(this.reportsBasePath);\n return reportDirs.filter((dir) => {\n const reportPath = this.getReportPath(dir);\n return fs.existsSync(reportPath);\n }).length;\n } catch (error) {\n logger.error('Failed to get report count:', error);\n return 0;\n }\n }\n}\n","import { ThreadManager } from '../threads';\nimport { logger } from '../utils/logger';\nimport { STORAGE_CONFIG } from '../config/storage';\n\n/**\n * CleanupService handles cleanup of old threads and UIBlocks\n * to prevent memory bloat and maintain optimal performance\n */\nexport class CleanupService {\n private static instance: CleanupService;\n private cleanupInterval: NodeJS.Timeout | null = null;\n\n private constructor() {}\n\n /**\n * Get singleton instance of CleanupService\n */\n static getInstance(): CleanupService {\n if (!CleanupService.instance) {\n CleanupService.instance = new CleanupService();\n }\n return CleanupService.instance;\n }\n\n /**\n * Clean up old threads based on retention period\n * @param retentionDays - Number of days to keep threads (defaults to config)\n * @returns Number of threads deleted\n */\n cleanupOldThreads(retentionDays: number = STORAGE_CONFIG.THREAD_RETENTION_DAYS): number {\n const threadManager = ThreadManager.getInstance();\n const cutoffDate = new Date();\n cutoffDate.setDate(cutoffDate.getDate() - retentionDays);\n\n const threads = threadManager.getAllThreads();\n let deletedCount = 0;\n\n for (const thread of threads) {\n if (thread.getCreatedAt() < cutoffDate) {\n const threadId = thread.getId();\n if (threadManager.deleteThread(threadId)) {\n deletedCount++;\n logger.info(`Deleted old thread: ${threadId} (created: ${thread.getCreatedAt().toISOString()})`);\n }\n }\n }\n\n if (deletedCount > 0) {\n logger.info(`Cleanup: Deleted ${deletedCount} old threads (older than ${retentionDays} days)`);\n }\n\n return deletedCount;\n }\n\n /**\n * Clean up old UIBlocks within threads based on retention period\n * @param retentionDays - Number of days to keep UIBlocks (defaults to config)\n * @returns Object with number of UIBlocks deleted per thread\n */\n cleanupOldUIBlocks(retentionDays: number = STORAGE_CONFIG.UIBLOCK_RETENTION_DAYS): { [threadId: string]: number } {\n const threadManager = ThreadManager.getInstance();\n const cutoffDate = new Date();\n cutoffDate.setDate(cutoffDate.getDate() - retentionDays);\n\n const threads = threadManager.getAllThreads();\n const deletionStats: { [threadId: string]: number } = {};\n\n for (const thread of threads) {\n const uiblocks = thread.getUIBlocks();\n let deletedInThread = 0;\n\n for (const uiblock of uiblocks) {\n if (uiblock.getCreatedAt() < cutoffDate) {\n if (thread.removeUIBlock(uiblock.getId())) {\n deletedInThread++;\n }\n }\n }\n\n if (deletedInThread > 0) {\n deletionStats[thread.getId()] = deletedInThread;\n logger.info(\n `Deleted ${deletedInThread} old UIBlocks from thread ${thread.getId()} (older than ${retentionDays} days)`\n );\n }\n }\n\n const totalDeleted = Object.values(deletionStats).reduce((sum, count) => sum + count, 0);\n if (totalDeleted > 0) {\n logger.info(`Cleanup: Deleted ${totalDeleted} old UIBlocks across ${Object.keys(deletionStats).length} threads`);\n }\n\n return deletionStats;\n }\n\n /**\n * Clear all component data from UIBlocks to free memory\n * Keeps metadata but removes the actual data\n * @param retentionDays - Number of days to keep full data (defaults to config)\n * @returns Number of UIBlocks whose data was cleared\n */\n clearOldUIBlockData(retentionDays: number = STORAGE_CONFIG.UIBLOCK_RETENTION_DAYS): number {\n const threadManager = ThreadManager.getInstance();\n const cutoffDate = new Date();\n cutoffDate.setDate(cutoffDate.getDate() - retentionDays);\n\n const threads = threadManager.getAllThreads();\n let clearedCount = 0;\n\n for (const thread of threads) {\n const uiblocks = thread.getUIBlocks();\n\n for (const uiblock of uiblocks) {\n if (uiblock.getCreatedAt() < cutoffDate) {\n const componentData = uiblock.getComponentData();\n\n // Only clear if data exists\n if (componentData && Object.keys(componentData).length > 0) {\n // Keep metadata but clear actual data\n const metadata = {\n dataCleared: true,\n clearedAt: new Date().toISOString(),\n originalDataInfo: {\n totalRows: componentData.totalRows,\n storedRows: componentData.storedRows,\n isTruncated: componentData.isTruncated,\n },\n };\n\n uiblock.setComponentData({ ...metadata, data: null });\n clearedCount++;\n }\n }\n }\n }\n\n if (clearedCount > 0) {\n logger.info(`Cleanup: Cleared data from ${clearedCount} old UIBlocks (older than ${retentionDays} days)`);\n }\n\n return clearedCount;\n }\n\n /**\n * Run full cleanup (threads, UIBlocks, and data)\n * @returns Cleanup statistics\n */\n runFullCleanup(): {\n threadsDeleted: number;\n uiblocksDeleted: { [threadId: string]: number };\n dataCleared: number;\n } {\n logger.info('Starting full cleanup...');\n\n const stats = {\n threadsDeleted: this.cleanupOldThreads(),\n uiblocksDeleted: this.cleanupOldUIBlocks(),\n dataCleared: this.clearOldUIBlockData(),\n };\n\n const totalUIBlocksDeleted = Object.values(stats.uiblocksDeleted).reduce((sum, count) => sum + count, 0);\n\n logger.info(\n `Full cleanup completed: ${stats.threadsDeleted} threads, ${totalUIBlocksDeleted} UIBlocks deleted, ${stats.dataCleared} UIBlock data cleared`\n );\n\n return stats;\n }\n\n /**\n * Start automatic cleanup at regular intervals\n * @param intervalHours - Hours between cleanup runs (default: 24)\n */\n startAutoCleanup(intervalHours: number = 24): void {\n if (this.cleanupInterval) {\n logger.warn('Auto cleanup is already running');\n\n //stop this and run with new interval\n\n \n return;\n }\n\n const intervalMs = intervalHours * 60 * 60 * 1000;\n\n // Run initial cleanup\n this.runFullCleanup();\n\n // Schedule recurring cleanup\n this.cleanupInterval = setInterval(() => {\n this.runFullCleanup();\n }, intervalMs);\n\n logger.info(`Auto cleanup started: running every ${intervalHours} hours`);\n }\n\n /**\n * Stop automatic cleanup\n */\n stopAutoCleanup(): void {\n if (this.cleanupInterval) {\n clearInterval(this.cleanupInterval);\n this.cleanupInterval = null;\n logger.info('Auto cleanup stopped');\n }\n }\n\n /**\n * Check if auto cleanup is running\n */\n isAutoCleanupRunning(): boolean {\n return this.cleanupInterval !== null;\n }\n\n /**\n * Get current memory usage statistics\n */\n getMemoryStats(): {\n threadCount: number;\n totalUIBlocks: number;\n avgUIBlocksPerThread: number;\n } {\n const threadManager = ThreadManager.getInstance();\n const threads = threadManager.getAllThreads();\n const threadCount = threads.length;\n\n let totalUIBlocks = 0;\n for (const thread of threads) {\n totalUIBlocks += thread.getUIBlockCount();\n }\n\n return {\n threadCount,\n totalUIBlocks,\n avgUIBlocksPerThread: threadCount > 0 ? totalUIBlocks / threadCount : 0,\n };\n }\n}\n","import { createWebSocket } from './websocket';\nimport {\n IncomingMessageSchema,\n type Message,\n type IncomingMessage,\n type SuperatomSDKConfig,\n type CollectionRegistry,\n type CollectionHandler,\n type CollectionOperation,\n Component,\n LLMProvider,\n} from './types';\nimport { logger } from './utils/logger';\nimport { handleDataRequest } from './handlers/data-request';\nimport { handleBundleRequest } from './handlers/bundle-request';\nimport { handleAuthLoginRequest } from './handlers/auth-login-requests';\nimport { handleAuthVerifyRequest } from './handlers/auth-verify-request';\nimport { handleUserPromptRequest } from './handlers/user-prompt-request';\nimport { handleUserPromptSuggestions } from './handlers/user-prompt-suggestions';\nimport { handleActionsRequest } from './handlers/actions-request';\nimport { handleComponentListResponse } from './handlers/components-list-response';\nimport { handleUsersRequest } from './handlers/users';\nimport { handleDashboardsRequest } from './handlers/dashboards';\nimport { handleReportsRequest } from './handlers/reports';\nimport { getLLMProviders } from './userResponse';\nimport { setUserManager, cleanupUserStorage } from './auth/user-storage';\nimport { UserManager } from './auth/user-manager';\nimport { setDashboardManager } from './dashboards/dashboard-storage';\nimport { DashboardManager } from './dashboards/dashboard-manager';\nimport { setReportManager } from './reports/report-storage';\nimport { ReportManager } from './reports/report-manager';\nimport { promptLoader } from './userResponse/prompt-loader';\n\nexport const SDK_VERSION = '0.0.8';\n\nconst DEFAULT_WS_URL = 'wss://ws.superatom.ai/websocket';\n\ntype MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;\n\nexport class SuperatomSDK {\n private ws: ReturnType<typeof createWebSocket> | null = null;\n private url: string;\n private apiKey: string;\n private projectId: string;\n private userId: string;\n private type: string;\n private bundleDir: string | undefined;\n private messageHandlers: Map<string, (message: IncomingMessage) => void> = new Map();\n private messageTypeHandlers: Map<string, MessageTypeHandler> = new Map();\n private connected: boolean = false;\n private reconnectAttempts: number = 0;\n private maxReconnectAttempts: number = 5;\n private collections: CollectionRegistry = {};\n\tprivate components: Component[] = [];\n private anthropicApiKey: string;\n private groqApiKey: string;\n private llmProviders: LLMProvider[];\n private userManager: UserManager;\n private dashboardManager: DashboardManager;\n private reportManager: ReportManager;\n\n constructor(config: SuperatomSDKConfig) {\n this.apiKey = config.apiKey;\n this.projectId = config.projectId;\n this.userId = config.userId || 'anonymous';\n this.type = config.type || 'data-agent';\n this.bundleDir = config.bundleDir;\n this.url = config.url || process.env.SA_WEBSOCKET_URL || DEFAULT_WS_URL;\n this.anthropicApiKey = config.ANTHROPIC_API_KEY || process.env.ANTHROPIC_API_KEY || '';\n this.groqApiKey = config.GROQ_API_KEY || process.env.GROQ_API_KEY || '';\n this.llmProviders = config.LLM_PROVIDERS || getLLMProviders();\n\n // Initialize UserManager for this SDK instance\n this.userManager = new UserManager(this.projectId, 5000);\n\n // Initialize DashboardManager for this SDK instance\n this.dashboardManager = new DashboardManager(this.projectId);\n\n // Initialize ReportManager for this SDK instance\n this.reportManager = new ReportManager(this.projectId);\n\n // Initialize PromptLoader (load prompts into memory)\n this.initializePromptLoader(config.promptsDir).catch((error) => {\n logger.error('Failed to initialize PromptLoader:', error);\n });\n\n // Initialize UserManager with projectId (startup)\n this.initializeUserManager().catch((error) => {\n logger.error('Failed to initialize UserManager:', error);\n });\n\n // Initialize DashboardManager\n this.initializeDashboardManager();\n\n // Initialize ReportManager\n this.initializeReportManager();\n\n // Automatically connect on instantiation\n this.connect().catch((error) => {\n logger.error('Failed to connect to Superatom:', error);\n });\n\n //\n }\n\n /**\n * Initialize PromptLoader and load prompts into memory\n */\n private async initializePromptLoader(promptsDir?: string): Promise<void> {\n try {\n // Set custom prompts directory if provided\n if (promptsDir) {\n promptLoader.setPromptsDir(promptsDir);\n }\n\n await promptLoader.initialize();\n logger.info(`PromptLoader initialized with ${promptLoader.getCacheSize()} prompts from ${promptLoader.getPromptsDir()}`);\n } catch (error) {\n logger.error('Failed to initialize PromptLoader:', error);\n throw error;\n }\n }\n\n /**\n * Initialize UserManager for the project\n */\n private async initializeUserManager(): Promise<void> {\n try {\n await this.userManager.init();\n // Set the global reference for backward compatibility with existing auth code\n setUserManager(this.userManager);\n logger.info(`UserManager initialized for project: ${this.projectId}`);\n } catch (error) {\n logger.error('Failed to initialize UserManager:', error);\n throw error;\n }\n }\n\n /**\n * Get the UserManager instance for this SDK\n */\n public getUserManager(): UserManager {\n return this.userManager;\n }\n\n /**\n * Initialize DashboardManager for the project\n */\n private initializeDashboardManager(): void {\n // Set the global reference for dashboard operations\n setDashboardManager(this.dashboardManager);\n logger.info(`DashboardManager initialized for project: ${this.projectId}`);\n }\n\n /**\n * Get the DashboardManager instance for this SDK\n */\n public getDashboardManager(): DashboardManager {\n return this.dashboardManager;\n }\n\n /**\n * Initialize ReportManager for the project\n */\n private initializeReportManager(): void {\n // Set the global reference for report operations\n setReportManager(this.reportManager);\n logger.info(`ReportManager initialized for project: ${this.projectId}`);\n }\n\n /**\n * Get the ReportManager instance for this SDK\n */\n public getReportManager(): ReportManager {\n return this.reportManager;\n }\n\n /**\n * Connect to the Superatom WebSocket service\n */\n async connect(): Promise<void> {\n return new Promise((resolve, reject) => {\n try {\n // Add all required query parameters\n const url = new URL(this.url);\n url.searchParams.set('apiKey', this.apiKey);\n url.searchParams.set('projectId', this.projectId);\n url.searchParams.set('userId', this.userId);\n url.searchParams.set('type', this.type);\n\n logger.info(`Connecting to WebSocket: ${url.host}`);\n\n this.ws = createWebSocket(url.toString());\n\n this.ws.addEventListener('open', () => {\n this.connected = true;\n this.reconnectAttempts = 0;\n logger.info('WebSocket connected successfully');\n resolve();\n });\n\n this.ws.addEventListener('message', (event: any) => {\n this.handleMessage(event.data);\n });\n\n this.ws.addEventListener('error', (error: any) => {\n logger.error('WebSocket error:', error);\n reject(error);\n });\n\n this.ws.addEventListener('close', () => {\n this.connected = false;\n logger.warn('WebSocket closed');\n this.handleReconnect();\n });\n } catch (error) {\n reject(error);\n }\n });\n }\n\n /**\n * Handle incoming WebSocket messages\n */\n private handleMessage(data: string): void {\n try {\n const parsed = JSON.parse(data);\n const message = IncomingMessageSchema.parse(parsed);\n\n logger.debug('Received message:', message.type);\n\n // Route message by type\n switch (message.type) {\n case 'DATA_REQ':\n handleDataRequest(parsed, this.collections, (msg) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle data request:', error);\n });\n break;\n\n case 'BUNDLE_REQ':\n handleBundleRequest(parsed, this.bundleDir, (msg) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle bundle request:', error);\n });\n break;\n\n case 'AUTH_LOGIN_REQ':\n handleAuthLoginRequest(parsed, (msg) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle auth login request:', error);\n });\n break;\n\n case 'AUTH_VERIFY_REQ':\n handleAuthVerifyRequest(parsed, (msg) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle auth verify request:', error);\n });\n break;\n\n case 'USER_PROMPT_REQ':\n handleUserPromptRequest(parsed, this.components, (msg) => this.send(msg), this.anthropicApiKey, this.groqApiKey, this.llmProviders).catch((error) => {\n logger.error('Failed to handle user prompt request:', error);\n });\n break;\n\n case 'ACTIONS':\n handleActionsRequest(parsed, (msg) => this.send(msg), this.anthropicApiKey, this.groqApiKey, this.llmProviders).catch((error) => {\n logger.error('Failed to handle actions request:', error);\n });\n break;\n\n case 'USER_PROMPT_SUGGESTIONS_REQ':\n handleUserPromptSuggestions(parsed, this.components, (msg) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle user prompt suggestions request:', error);\n });\n break;\n\n case 'COMPONENT_LIST_RES':\n handleComponentListResponse(parsed, (com) => this.storeComponents(com)).catch((error) => {\n logger.error('Failed to handle component list request:', error);\n });\n break;\n\n case 'USERS':\n handleUsersRequest(parsed, (msg) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle users request:', error);\n });\n break;\n\n case 'DASHBOARDS':\n handleDashboardsRequest(parsed, (msg) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle dashboards request:', error);\n });\n break;\n\n case 'REPORTS':\n handleReportsRequest(parsed, (msg) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle reports request:', error);\n });\n break;\n\n default:\n // Check for custom message type handlers\n const handler = this.messageTypeHandlers.get(message.type);\n if (handler) {\n Promise.resolve(handler(message)).catch((error) => {\n logger.error(`Failed to handle ${message.type}:`, error);\n });\n }\n break;\n }\n\n // Call registered message handlers\n this.messageHandlers.forEach((handler) => {\n handler(message);\n });\n } catch (error) {\n logger.error('Failed to parse incoming message:', error);\n }\n }\n\n /**\n * Send a message to the Superatom service\n */\n send(message: Message): void {\n if (!this.ws || !this.connected) {\n throw new Error('WebSocket is not connected. Call connect() first.');\n }\n\n if (this.ws.readyState !== this.ws.OPEN) {\n throw new Error('WebSocket is not ready to send messages.');\n }\n\n const payload = JSON.stringify(message);\n this.ws.send(payload);\n }\n\n /**\n * Register a message handler to receive all messages\n */\n onMessage(handler: (message: IncomingMessage) => void): () => void {\n const id = Math.random().toString(36).substring(7);\n this.messageHandlers.set(id, handler);\n\n // Return unsubscribe function\n return () => {\n this.messageHandlers.delete(id);\n };\n }\n\n /**\n * Register a handler for a specific message type\n */\n onMessageType(type: string, handler: MessageTypeHandler): () => void {\n this.messageTypeHandlers.set(type, handler);\n\n // Return unsubscribe function\n return () => {\n this.messageTypeHandlers.delete(type);\n };\n }\n\n /**\n * Disconnect from the WebSocket service\n */\n disconnect(): void {\n if (this.ws) {\n this.ws.close();\n this.ws = null;\n this.connected = false;\n }\n }\n\n /**\n * Cleanup and disconnect - stops reconnection attempts and closes the connection\n */\n async destroy(): Promise<void> {\n // Prevent further reconnection attempts\n this.maxReconnectAttempts = 0;\n this.reconnectAttempts = 0;\n\n // Clear all message handlers\n this.messageHandlers.clear();\n\n // Clear all collections\n this.collections = {};\n\n // Cleanup UserManager\n try {\n await cleanupUserStorage();\n logger.info('UserManager cleanup completed');\n } catch (error) {\n logger.error('Error during UserManager cleanup:', error);\n }\n\n // Disconnect\n this.disconnect();\n }\n\n /**\n * Check if the SDK is currently connected\n */\n isConnected(): boolean {\n return this.connected && this.ws !== null && this.ws.readyState === this.ws.OPEN;\n }\n\n /**\n * Register a collection handler for data operations\n */\n addCollection<TParams = any, TResult = any>(\n collectionName: string,\n operation: CollectionOperation | string,\n handler: CollectionHandler<TParams, TResult>\n ): void {\n if (!this.collections[collectionName]) {\n this.collections[collectionName] = {};\n }\n this.collections[collectionName][operation] = handler;\n }\n\n private handleReconnect(): void {\n if (this.reconnectAttempts < this.maxReconnectAttempts) {\n this.reconnectAttempts++;\n const delay = Math.min(1000 * Math.pow(2, this.reconnectAttempts), 10000);\n\n setTimeout(() => {\n logger.info(`Attempting to reconnect (${this.reconnectAttempts}/${this.maxReconnectAttempts})...`);\n this.connect().catch((error) => {\n logger.error('Reconnection failed:', error);\n });\n }, delay);\n } else {\n logger.error('Max reconnection attempts reached');\n }\n }\n\n private storeComponents(components: Component[]){\n this.components = components;\n }\n \n}\n\n// Export types\nexport type { Message, IncomingMessage, SuperatomSDKConfig, CollectionHandler, CollectionOperation } from './types';\nexport {LLM} from './llm';\nexport { UserManager, type User, type UsersData } from './auth/user-manager';\nexport { UILogCollector, type CapturedLog } from './utils/log-collector';\nexport { Thread, UIBlock, ThreadManager, type Action } from './threads';\nexport { CleanupService } from './services/cleanup-service';\nexport { STORAGE_CONFIG } from './config/storage';\nexport { CONTEXT_CONFIG } from './config/context';"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA,MACE,MAAQ;AAAA,MACR,SAAW;AAAA,MACX,aAAe;AAAA,MACf,MAAQ;AAAA,MACR,OAAS;AAAA,MACT,SAAW;AAAA,QACT,KAAK;AAAA,UACH,OAAS;AAAA,UACT,SAAW;AAAA,UACX,SAAW;AAAA,QACb;AAAA,QACA,YAAY;AAAA,QACZ,eAAe;AAAA,QACf,qBAAqB;AAAA,QACrB,wBAAwB;AAAA,QACxB,qBAAqB;AAAA,QACrB,wBAAwB;AAAA,QACxB,kBAAkB;AAAA,MACpB;AAAA,MACA,SAAW;AAAA,QACT,aAAa;AAAA,QACb,MAAQ;AAAA,QACR,SAAW;AAAA,QACX,MAAQ;AAAA,QACR,iBAAiB;AAAA,QACjB,YAAc;AAAA,QACd,SAAW;AAAA,MACb;AAAA,MACA,YAAc;AAAA,QACZ,MAAQ;AAAA,QACR,KAAO;AAAA,MACT;AAAA,MACA,UAAY;AAAA,MACZ,SAAW;AAAA,MACX,UAAY;AAAA,QACV;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,MACA,gBAAkB;AAAA,MAClB,SAAW;AAAA,MACX,iBAAmB;AAAA,QACjB,eAAe;AAAA,QACf,SAAW;AAAA,QACX,OAAS;AAAA,QACT,UAAY;AAAA,QACZ,oBAAoB;AAAA,QACpB,KAAO;AAAA,QACP,YAAc;AAAA,MAChB;AAAA,MACA,SAAW;AAAA,QACT,MAAQ;AAAA,MACV;AAAA,MACA,SAAW;AAAA,QACT,IAAM;AAAA,MACR;AAAA,IACF;AAAA;AAAA;;;AC7DA;AAAA;AAAA;AAAA,Q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WAAW;AAAA,EAClB;AAAA;AAAA;AAAA;AAAA,EAKA,cAAuB;AACrB,WAAO,KAAK,aAAa,KAAK,OAAO,QAAQ,KAAK,GAAG,eAAe,KAAK,GAAG;AAAA,EAC9E;AAAA;AAAA;AAAA;AAAA,EAKA,cACE,gBACA,WACA,SACM;AACN,QAAI,CAAC,KAAK,YAAY,cAAc,GAAG;AACrC,WAAK,YAAY,cAAc,IAAI,CAAC;AAAA,IACtC;AACA,SAAK,YAAY,cAAc,EAAE,SAAS,IAAI;AAAA,EAChD;AAAA,EAEQ,kBAAwB;AAC9B,QAAI,KAAK,oBAAoB,KAAK,sBAAsB;AACtD,WAAK;AACL,YAAM,QAAQ,KAAK,IAAI,MAAO,KAAK,IAAI,GAAG,KAAK,iBAAiB,GAAG,GAAK;AAExE,iBAAW,MAAM;AACf,eAAO,KAAK,4BAA4B,KAAK,iBAAiB,IAAI,KAAK,oBAAoB,MAAM;AACjG,aAAK,QAAQ,EAAE,MAAM,CAAC,UAAU;AAC9B,iBAAO,MAAM,wBAAwB,KAAK;AAAA,QAC5C,CAAC;AAAA,MACH,GAAG,KAAK;AAAA,IACV,OAAO;AACL,aAAO,MAAM,mCAAmC;AAAA,IAClD;AAAA,EACF;AAAA,EAEQ,gBAAgB,YAAwB;AAC9C,SAAK,aAAa;AAAA,EACpB;AAEF;","names":["fs","path","os","crypto","z","z","ExpressionSchema","BindingSchema","ForDirectiveSchema","QuerySpecSchema","UIElementSchema","UIComponentSchema","DSLRendererPropsSchema","z","DSLRendererPropsSchema","size","randomUUID","result","sendDataResponse","sendDataResponse","path","fs","path","fs","schema","fs","path","path","fs","dotenv","import_dotenv","dotenv","import_dotenv","dotenv","sendDataResponse","sendResponse","sendResponse","sendResponse","dashboardManager","handleCreate","handleUpdate","handleDelete","handleGetAll","handleGetOne","sendResponse","reportManager","handleCreate","handleUpdate","handleDelete","handleGetAll","handleGetOne","fs","path","path","fs","fs","path","os","path","os","fs","fs","path","os","path","os","fs","DSLRendererPropsSchema"]}
1
+ {"version":3,"sources":["../src/websocket.ts","../src/types.ts","../src/dashboards/types.ts","../src/reports/types.ts","../src/utils/logger.ts","../src/threads/uiblock.ts","../src/config/storage.ts","../src/threads/thread.ts","../src/threads/thread-manager.ts","../src/utils/query-cache.ts","../src/utils/surrogate.ts","../src/userResponse/llm-result-truncator.ts","../src/utils/cache-key.ts","../src/handlers/data-request.ts","../src/bundle.ts","../src/handlers/bundle-request.ts","../src/auth/utils.ts","../src/auth/user-storage.ts","../src/auth/validator.ts","../src/handlers/auth-login-requests.ts","../src/handlers/auth-verify-request.ts","../src/userResponse/prompt-loader.ts","../src/userResponse/prompts.ts","../src/userResponse/utils.ts","../src/utils/user-prompt-error-logger.ts","../src/utils/bm25l-reranker.ts","../src/userResponse/conversation-search.ts","../src/userResponse/constants.ts","../src/userResponse/stream-buffer.ts","../src/userResponse/knowledge-base.ts","../src/llm.ts","../src/utils/llm-usage-logger.ts","../src/utils/datetime.ts","../src/userResponse/prompt-extractor.ts","../src/userResponse/agents/data-summary.ts","../src/userResponse/agents/agent-prompt-builder.ts","../src/userResponse/agents/source-agent.ts","../src/userResponse/scripts/script-runner.ts","../src/userResponse/scripts/script-ipc.ts","../src/userResponse/agents/main-agent.ts","../src/userResponse/agents/types.ts","../src/userResponse/utils/component-props-processor.ts","../src/userResponse/agents/agent-component-generator.ts","../src/userResponse/scripts/script-metadata-store.ts","../src/userResponse/scripts/script-store.ts","../src/userResponse/scripts/script-matcher.ts","../src/userResponse/scripts/script-component-generator.ts","../src/userResponse/anthropic.ts","../src/userResponse/schema.ts","../src/userResponse/services/query-execution-service.ts","../src/userResponse/services/tool-executor-service.ts","../src/userResponse/base-llm.ts","../src/userResponse/groq.ts","../src/userResponse/gemini.ts","../src/userResponse/openai.ts","../src/userResponse/agent-user-response.ts","../src/utils/analytics-client.ts","../src/utils/conversation-saver.ts","../src/config/context.ts","../src/handlers/user-prompt-request.ts","../src/handlers/user-prompt-suggestions.ts","../src/userResponse/next-questions.ts","../src/handlers/actions-request.ts","../src/handlers/components-list-response.ts","../src/handlers/workflow-list-response.ts","../src/handlers/users.ts","../src/dashboards/dashboard-storage.ts","../src/handlers/dashboards.ts","../src/reports/report-storage.ts","../src/handlers/reports.ts","../src/handlers/uis.ts","../src/handlers/bookmarks.ts","../src/handlers/artifacts.ts","../src/handlers/kb-nodes.ts","../src/handlers/menus.ts","../src/dashComp/types.ts","../src/dashComp/utils.ts","../src/dashComp/pick-component.ts","../src/dashComp/create-filter.ts","../src/dashComp/dashboard-conversation-history.ts","../src/dashComp/index.ts","../src/reportComp/types.ts","../src/reportComp/utils.ts","../src/reportComp/generate-report.ts","../src/reportComp/index.ts","../src/handlers/schema-request.ts","../src/userResponse/index.ts","../src/auth/user-manager.ts","../src/dashboards/dashboard-manager.ts","../src/reports/report-manager.ts","../src/utils/log-collector.ts","../src/services/cleanup-service.ts","../src/index.ts"],"sourcesContent":["import WebSocket from 'ws';\nimport type { WebSocketLike } from './types';\n\n/**\n * Creates a WebSocket instance for Node.js using the ws package\n */\nexport function createWebSocket(url: string): WebSocketLike {\n return new WebSocket(url) as WebSocketLike;\n}\n","import { z } from 'zod';\n\n// Re-export DBUIBlock for external use\nexport type { DBUIBlock } from './utils/conversation-saver';\n\n// User schema for authentication and management\nexport const UserSchema = z.object({\n username: z.string().min(1, 'Username is required'),\n email: z.string().email('Invalid email format').optional(),\n password: z.string().min(1, 'Password is required'),\n fullname: z.string().optional(),\n role: z.string().optional(),\n userInfo: z.record(z.unknown()).optional(),\n wsIds: z.array(z.string()).optional(), // Only in memory, not persisted to file\n});\n\nexport type User = z.infer<typeof UserSchema>;\n\nexport const UsersDataSchema = z.object({\n users: z.array(UserSchema),\n});\n\nexport type UsersData = z.infer<typeof UsersDataSchema>;\n\n// From/To object schema for WebSocket messages\nexport const MessageParticipantSchema = z.object({\n id: z.string().optional(),\n type: z.string().optional(),\n});\n\nexport type MessageParticipant = z.infer<typeof MessageParticipantSchema>;\n\n// Message schemas for WebSocket communication\nexport const MessageSchema = z.object({\n id: z.string().optional(),\n type: z.string(),\n from: MessageParticipantSchema,\n to: MessageParticipantSchema.optional(),\n payload: z.unknown(),\n});\n\nexport type Message = z.infer<typeof MessageSchema>;\n\nexport const IncomingMessageSchema = z.object({\n id: z.string().optional(),\n type: z.string(),\n from: MessageParticipantSchema,\n to: MessageParticipantSchema.optional(),\n payload: z.unknown(),\n});\n\nexport type IncomingMessage = z.infer<typeof IncomingMessageSchema>;\n\n// Data request/response schemas\nexport const DataRequestPayloadSchema = z.object({\n collection: z.string(),\n op: z.string(),\n params: z.record(z.unknown()).optional(),\n SA_RUNTIME: z.record(z.unknown()).optional(),\n});\n\nexport type DataRequestPayload = z.infer<typeof DataRequestPayloadSchema>;\n\nexport const DataRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('DATA_REQ'),\n payload: DataRequestPayloadSchema,\n});\n\nexport type DataRequestMessage = z.infer<typeof DataRequestMessageSchema>;\n\nexport const AuthLoginRequestPayloadSchema = z.object({\n login_data: z.string(),\n});\n\nexport type AuthLoginRequestPayload = z.infer<typeof AuthLoginRequestPayloadSchema>;\n\nexport const AuthLoginRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('AUTH_LOGIN_REQ'),\n payload: AuthLoginRequestPayloadSchema,\n});\n\nexport type AuthLoginRequestMessage = z.infer<typeof AuthLoginRequestMessageSchema>;\n\nexport const AuthVerifyRequestPayloadSchema = z.object({\n token: z.string(),\n});\n\nexport type AuthVerifyRequestPayload = z.infer<typeof AuthVerifyRequestPayloadSchema>;\n\nexport const AuthVerifyRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('AUTH_VERIFY_REQ'),\n payload: AuthVerifyRequestPayloadSchema,\n});\n\nexport type AuthVerifyRequestMessage = z.infer<typeof AuthVerifyRequestMessageSchema>;\n\nexport const UserPromptRequestPayloadSchema = z.object({\n prompt: z.string(),\n userId: z.string().optional(),\n SA_RUNTIME: z.object({\n threadId: z.string(),\n uiBlockId: z.string(),\n }).optional(),\n responseMode: z.enum(['component', 'text']).optional(),\n});\n\nexport type UserPromptRequestPayload = z.infer<typeof UserPromptRequestPayloadSchema>;\n\nexport const UserPromptRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('USER_PROMPT_REQ'),\n payload: UserPromptRequestPayloadSchema,\n});\n\nexport type UserPromptRequestMessage = z.infer<typeof UserPromptRequestMessageSchema>;\n\nexport const UserPromptSuggestionsPayloadSchema = z.object({\n prompt: z.string(),\n userId: z.string().optional(),\n limit: z.number().int().positive().default(5),\n similarityThreshold: z.number().min(0).max(1).default(0.4),\n});\n\nexport type UserPromptSuggestionsPayload = z.infer<typeof UserPromptSuggestionsPayloadSchema>;\n\nexport const UserPromptSuggestionsMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('USER_PROMPT_SUGGESTIONS_REQ'),\n payload: UserPromptSuggestionsPayloadSchema,\n});\n\nexport type UserPromptSuggestionsMessage = z.infer<typeof UserPromptSuggestionsMessageSchema>;\n\n\nexport const ComponentPropsSchema = z.object({\n query: z.string().or(z.object({})).nullable().optional(),\n title: z.string().optional(),\n description: z.string().optional(),\n config: z.record(z.unknown()).optional(),\n actions: z.array(z.any()).optional(),\n}).passthrough(); // Allow additional props beyond the defined ones\n\nexport const ComponentSchema = z.object({\n id: z.string(),\n name: z.string(),\n displayName: z.string().optional(),\n isDisplayComp: z.boolean().optional(),\n type: z.string(),\n description: z.string(),\n props: ComponentPropsSchema,\n category: z.string().optional(),\n keywords: z.array(z.string()).optional(),\n});\n\nexport const ComponentsSchema = z.array(ComponentSchema);\n\nexport type Component = z.infer<typeof ComponentSchema>;\n\nexport const ComponentListResponsePayloadSchema = z.object({\n components: z.array(ComponentSchema),\n});\n\nexport type ComponentListResponsePayload = z.infer<typeof ComponentListResponsePayloadSchema>; \n\nexport const ComponentListResponseMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('COMPONENT_LIST_RES'),\n payload: ComponentListResponsePayloadSchema,\n});\n\nexport type ComponentListResponseMessage = z.infer<typeof ComponentListResponseMessageSchema>;\n\n// ==================== Workflow Descriptors ====================\n// Workflow components are pre-built multi-step UI flows the main agent can\n// pick when the user wants to *do* something end-to-end (initiate a transfer,\n// create an order, etc.). The descriptor declares routing metadata; the React\n// component itself lives in the frontend bundle.\n\nexport const WorkflowDescriptorSchema = z.object({\n id: z.string(),\n name: z.string(),\n description: z.string(),\n whenToUse: z.string(),\n propsSchema: z.record(z.string()),\n defaultProps: z.record(z.unknown()).optional(),\n});\n\nexport const WorkflowsSchema = z.array(WorkflowDescriptorSchema);\n\nexport type WorkflowDescriptorPayload = z.infer<typeof WorkflowDescriptorSchema>;\n\nexport const WorkflowListResponsePayloadSchema = z.object({\n workflows: z.array(WorkflowDescriptorSchema),\n});\n\nexport type WorkflowListResponsePayload = z.infer<typeof WorkflowListResponsePayloadSchema>;\n\nexport const WorkflowListResponseMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('WORKFLOW_LIST_RES'),\n payload: WorkflowListResponsePayloadSchema,\n});\n\nexport type WorkflowListResponseMessage = z.infer<typeof WorkflowListResponseMessageSchema>;\n\n// Tool output field schema - describes a single field/column in the tool's output\nexport const OutputFieldSchema = z.object({\n name: z.string(), // Field name (column name in the result)\n type: z.enum(['string', 'number', 'boolean', 'date']),\n description: z.string(), // What this field contains\n});\n\nexport type OutputField = z.infer<typeof OutputFieldSchema>;\n\n// Tool output schema - describes the array of records returned by a tool\n// Tools ALWAYS return an array of records (like SQL query results)\n// This helps the LLM understand what data is available for component generation\nexport const OutputSchema = z.object({\n description: z.string(), // Brief description of what the output represents\n fields: z.array(OutputFieldSchema), // List of fields in each record (like columns in a table)\n});\n\nexport type ToolOutputSchema = z.infer<typeof OutputSchema>;\n\n// Tool schemas\nexport const ToolSchema = z.object({\n id: z.string(),\n name: z.string(),\n description: z.string(),\n /** Tool type: \"source\" = routed through SourceAgent, \"direct\" = called directly by MainAgent */\n toolType: z.enum(['source', 'direct']).optional(),\n /** Full untruncated schema for source agent (all columns visible) */\n fullSchema: z.string().optional(),\n params: z.record(z.string()),\n fn: z.function().args(z.any()).returns(z.any()),\n outputSchema: OutputSchema.optional(), // Optional: describes the data structure returned by this tool\n /** Cache policy. `false` = never cache (live data, write ops). Mirrors HTTP `Cache-Control: no-store`. */\n cache: z.union([z.literal(false), z.object({ ttlMs: z.number().optional() })]).optional(),\n});\n\nexport type Tool = z.infer<typeof ToolSchema>;\n\n// Admin User Management Request/Response schemas (Unified)\n\n// Query filters schema for users\nexport const UserQueryFiltersSchema = z.object({\n username: z.string().optional(),\n email: z.string().optional(),\n role: z.string().optional(),\n fullname: z.string().optional(),\n id: z.number().optional()\n});\n\nexport type UserQueryFilters = z.infer<typeof UserQueryFiltersSchema>;\n\nexport const UsersRequestPayloadSchema = z.object({\n operation: z.enum(['create', 'update', 'delete', 'getAll', 'getOne', 'query']),\n data: z.object({\n id: z.number().optional(),\n username: z.string().optional(),\n email: z.string().email('Invalid email format').optional(),\n password: z.string().optional(),\n fullname: z.string().optional(),\n role: z.string().optional(),\n userInfo: z.record(z.unknown()).optional(),\n // Query operation fields\n filters: UserQueryFiltersSchema.optional(),\n limit: z.number().optional(),\n sort: z.enum(['ASC', 'DESC']).optional(),\n }).optional(),\n});\n\nexport type UsersRequestPayload = z.infer<typeof UsersRequestPayloadSchema>;\n\nexport const UsersRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('USERS'),\n payload: UsersRequestPayloadSchema,\n});\n\nexport type UsersRequestMessage = z.infer<typeof UsersRequestMessageSchema>;\n\n// UI Logs schema for tracking user request execution\nexport const UILogsPayloadSchema = z.object({\n logs: z.array(z.object({\n timestamp: z.number(),\n level: z.enum(['info', 'error', 'warn', 'debug']),\n message: z.string(),\n type: z.enum(['explanation', 'query', 'general']).optional(),\n data: z.record(z.unknown()).optional(),\n })),\n});\n\nexport type UILogsPayload = z.infer<typeof UILogsPayloadSchema>;\n\nexport const UILogsMessageSchema = z.object({\n id: z.string(), // uiBlockId\n from: MessageParticipantSchema,\n type: z.literal('UI_LOGS'),\n payload: UILogsPayloadSchema,\n});\n\nexport type UILogsMessage = z.infer<typeof UILogsMessageSchema>;\n\n// Actions request schema - for runtime to send component actions and request next questions\nexport const ActionsRequestPayloadSchema = z.object({\n userId: z.string().optional(),\n SA_RUNTIME: z.object({\n threadId: z.string(),\n uiBlockId: z.string(),\n }).optional(),\n});\n\nexport type ActionsRequestPayload = z.infer<typeof ActionsRequestPayloadSchema>;\n\nexport const ActionsRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('ACTIONS'),\n payload: ActionsRequestPayloadSchema,\n});\n\nexport type ActionsRequestMessage = z.infer<typeof ActionsRequestMessageSchema>;\n\n// Collection operation types\nexport type CollectionOperation =\n | 'getMany'\n | 'getOne'\n | 'query'\n | 'mutation'\n | 'updateOne'\n | 'deleteOne'\n | 'createOne';\n\nexport type CollectionHandler<TParams = any, TResult = any> = (\n params: TParams\n) => Promise<TResult> | TResult;\n\nexport interface CollectionRegistry {\n [collectionName: string]: {\n [operation: string]: CollectionHandler;\n };\n}\n\nexport type LLMProvider = 'anthropic' | 'groq' | 'gemini' | 'openai';\n\nimport type { LogLevel } from './utils/logger';\n\nexport type DatabaseType = 'postgresql' | 'mssql' | 'snowflake' | 'mysql';\n\n/**\n * Model strategy for controlling which models are used for different tasks\n * - 'best': Use the best model (e.g., Sonnet) for all tasks - highest quality, higher cost\n * - 'fast': Use the fast model (e.g., Haiku) for all tasks - lower quality, lower cost\n * - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)\n */\nexport type ModelStrategy = 'best' | 'fast' | 'balanced';\n\n/**\n * Model configuration for DASH_COMP flow (dashboard component picking)\n * Allows separate control of models used for component selection\n */\nexport interface DashCompModelConfig {\n /**\n * Primary model for DASH_COMP requests\n * Format: \"provider/model-name\" (e.g., \"anthropic/claude-sonnet-4-5-20250929\")\n */\n model?: string;\n /**\n * Fast model for simpler DASH_COMP tasks (optional)\n * Format: \"provider/model-name\" (e.g., \"anthropic/claude-haiku-4-5-20251001\")\n */\n fastModel?: string;\n}\n\nexport interface SuperatomSDKConfig {\n url?: string;\n apiKey?: string;\n projectId: string;\n type?: string;\n bundleDir?: string;\n promptsDir?: string; // Path to custom prompts directory (defaults to .prompts in SDK)\n databaseType?: DatabaseType; // Database type for SQL generation rules (defaults to 'postgresql')\n ANTHROPIC_API_KEY?: string;\n GROQ_API_KEY?: string;\n GEMINI_API_KEY?: string;\n OPENAI_API_KEY?: string;\n LLM_PROVIDERS?: LLMProvider[];\n logLevel?: LogLevel; // Log level for SDK logging (errors, warnings, info, verbose)\n /**\n * Model selection strategy for LLM API calls:\n * - 'best': Use best model for all tasks (highest quality, higher cost)\n * - 'fast': Use fast model for all tasks (lower quality, lower cost)\n * - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)\n */\n modelStrategy?: ModelStrategy;\n /**\n * Model for the main agent (routing + analysis).\n * Format: \"provider/model-name\" (e.g., \"anthropic/claude-haiku-4-5-20251001\")\n * If not set, uses the provider's default model.\n */\n mainAgentModel?: string;\n /**\n * Model for source agents (per-source query generation).\n * Format: \"provider/model-name\" (e.g., \"anthropic/claude-haiku-4-5-20251001\")\n * If not set, uses the provider's default model.\n */\n sourceAgentModel?: string;\n /**\n * Separate model configuration for DASH_COMP flow (dashboard component picking)\n * If not provided, falls back to provider-based model selection\n */\n dashCompModels?: DashCompModelConfig;\n /**\n * Similarity threshold for conversation search (semantic matching)\n * Value between 0 and 1 (e.g., 0.8 = 80% similarity required)\n * Higher values require closer matches, lower values allow more distant matches\n * Default: 0.8\n */\n conversationSimilarityThreshold?: number;\n /**\n * Query cache TTL (Time To Live) in minutes\n * Cached query results expire after this duration\n * Default: 5 minutes\n */\n queryCacheTTL?: number;\n /**\n * Dashboard conversation history TTL (Time To Live) in minutes\n * Per-dashboard conversation histories expire after this duration\n * Default: 30 minutes\n */\n dashboardHistoryTTL?: number;\n}\n\n// ==================== Dashboard CRUD Message Schemas ====================\n\n// Import DSL types from dashboards module\nimport { DSLRendererPropsSchema } from './dashboards/types';\nexport type { DSLRendererProps } from './dashboards/types';\n\n// Query filters schema for dashboards\nexport const DashboardQueryFiltersSchema = z.object({\n dashboardId: z.string().optional(),\n projectId: z.string().optional(),\n createdBy: z.string().optional(),\n updatedBy: z.string().optional(),\n name: z.string().optional(),\n published: z.boolean().optional(),\n});\n\nexport type DashboardQueryFilters = z.infer<typeof DashboardQueryFiltersSchema>;\n\n// Dashboard CRUD request payload\nexport const DashboardsRequestPayloadSchema = z.object({\n operation: z.enum(['create', 'update', 'delete', 'getAll', 'getOne', 'query']),\n data: z.object({\n id: z.number().optional(),\n dashboardId: z.string().optional(),\n projectId: z.string().optional(),\n name: z.string().optional(),\n description: z.string().optional(),\n published: z.boolean().optional(),\n createdBy: z.string().optional(),\n updatedBy: z.string().optional(),\n allowedUsers: z.array(z.string()).optional(),\n dashboard: DSLRendererPropsSchema.optional(),\n // Query operation fields\n filters: DashboardQueryFiltersSchema.optional(),\n limit: z.number().optional(),\n sort: z.enum(['ASC', 'DESC']).optional(),\n }).optional(),\n});\n\nexport type DashboardsRequestPayload = z.infer<typeof DashboardsRequestPayloadSchema>;\n\nexport const DashboardsRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('DASHBOARDS'),\n payload: DashboardsRequestPayloadSchema,\n});\n\nexport type DashboardsRequestMessage = z.infer<typeof DashboardsRequestMessageSchema>;\n\n// ==================== Report CRUD Message Schemas ====================\n\n// Import DSL types from reports module\nimport { DSLRendererPropsSchema as ReportDSLRendererPropsSchema } from './reports/types';\nexport type { DSLRendererProps as ReportDSLRendererProps } from './reports/types';\n\n// Query filters schema for reports\nexport const ReportQueryFiltersSchema = z.object({\n reportId: z.string().optional(),\n projectId: z.string().optional(),\n createdBy: z.string().optional(),\n updatedBy: z.string().optional(),\n name: z.string().optional(),\n published: z.boolean().optional(),\n});\n\nexport type ReportQueryFilters = z.infer<typeof ReportQueryFiltersSchema>;\n\n// Report CRUD request payload\nexport const ReportsRequestPayloadSchema = z.object({\n operation: z.enum(['create', 'update', 'delete', 'getAll', 'getOne', 'query']),\n data: z.object({\n id: z.number().optional(),\n reportId: z.string().optional(),\n projectId: z.string().optional(),\n name: z.string().optional(),\n description: z.string().optional(),\n published: z.boolean().optional(),\n createdBy: z.string().optional(),\n updatedBy: z.string().optional(),\n report: ReportDSLRendererPropsSchema.optional(),\n // Query operation fields\n filters: ReportQueryFiltersSchema.optional(),\n limit: z.number().optional(),\n sort: z.enum(['ASC', 'DESC']).optional(),\n }).optional(),\n});\n\nexport type ReportsRequestPayload = z.infer<typeof ReportsRequestPayloadSchema>;\n\nexport const ReportsRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('REPORTS'),\n payload: ReportsRequestPayloadSchema,\n});\n\nexport type ReportsRequestMessage = z.infer<typeof ReportsRequestMessageSchema>;\n\n// ==================== UIs CRUD Message Schemas ====================\n\n// Import DSL types from uis module (same as dashboards)\nimport { DSLRendererPropsSchema as UIDSLRendererPropsSchema } from './dashboards/types';\nexport type { DSLRendererProps as UIDSLRendererProps } from './dashboards/types';\n\n// Query filters schema for UIs\nexport const UIQueryFiltersSchema = z.object({\n uiId: z.string().optional(),\n projectId: z.string().optional(),\n createdBy: z.string().optional(),\n updatedBy: z.string().optional(),\n name: z.string().optional(),\n});\n\nexport type UIQueryFilters = z.infer<typeof UIQueryFiltersSchema>;\n\n// UI CRUD request payload\nexport const UIsRequestPayloadSchema = z.object({\n operation: z.enum(['create', 'update', 'delete', 'getAll', 'getOne', 'query']),\n data: z.object({\n id: z.number().optional(),\n uiId: z.string().optional(),\n projectId: z.string().optional(),\n name: z.string().optional(),\n description: z.string().optional(),\n createdBy: z.string().optional(),\n updatedBy: z.string().optional(),\n ui: UIDSLRendererPropsSchema.optional(),\n // Query operation fields\n filters: UIQueryFiltersSchema.optional(),\n limit: z.number().optional(),\n sort: z.enum(['ASC', 'DESC']).optional(),\n }).optional(),\n});\n\nexport type UIsRequestPayload = z.infer<typeof UIsRequestPayloadSchema>;\n\nexport const UIsRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('UIS'),\n payload: UIsRequestPayloadSchema,\n});\n\nexport type UIsRequestMessage = z.infer<typeof UIsRequestMessageSchema>;\n\n// ==================== UI Block (Conversation History) ====================\n\n/**\n * UIBlock represents a stored conversation response with component and actions\n * Used for semantic search matching in conversation history\n */\nexport const UIBlockSchema = z.object({\n id: z.string().optional(),\n userQuestion: z.string().optional(),\n user_prompt: z.string().optional(), // DB field name\n text: z.string().optional(),\n textResponse: z.string().optional(),\n analysis: z.string().optional(), // DB field name for textResponse\n component: ComponentSchema.optional(), // DB field name / Legacy field\n generatedComponentMetadata: ComponentSchema.optional(), // Actual field used by UIBlock class\n componentData: z.record(z.any()).optional(),\n actions: z.array(z.any()).optional(),\n isFetchingActions: z.boolean().optional(),\n createdAt: z.string().optional(),\n metadata: z.object({\n timestamp: z.number().optional(),\n userPrompt: z.string().optional(),\n similarity: z.number().optional(),\n }).optional(),\n});\n\nexport type UIBlock = z.infer<typeof UIBlockSchema>;\n\n// ==================== Bookmarks CRUD Message Schemas ====================\n\n// DBUIBlock schema for database storage (matches DBUIBlock interface from conversation-saver.ts)\nexport const DBUIBlockSchema = z.object({\n id: z.string(),\n component: z.record(z.string(), z.any()).nullable(),\n analysis: z.string().nullable(),\n user_prompt: z.string(),\n});\n\n// Bookmark data schema\nexport const BookmarkDataSchema = z.object({\n id: z.number().optional(),\n uiblock: DBUIBlockSchema, // Typed JSON object\n created_at: z.string().optional(),\n updated_at: z.string().optional(),\n});\n\nexport type BookmarkData = z.infer<typeof BookmarkDataSchema>;\n\n// Query filters schema for bookmarks\nexport const BookmarkQueryFiltersSchema = z.object({\n userId: z.string().optional(),\n threadId: z.string().optional(),\n name: z.string().optional(),\n});\n\nexport type BookmarkQueryFilters = z.infer<typeof BookmarkQueryFiltersSchema>;\n\n// Bookmarks CRUD request payload\nexport const BookmarksRequestPayloadSchema = z.object({\n operation: z.enum(['create', 'update', 'delete', 'getAll', 'getOne', 'query']),\n data: z.object({\n id: z.number().optional(),\n userId: z.string().optional(),\n threadId: z.string().optional(),\n name: z.string().optional(),\n description: z.string().optional(),\n uiblock: DBUIBlockSchema.optional(),\n // Query operation fields\n filters: BookmarkQueryFiltersSchema.optional(),\n limit: z.number().optional(),\n sort: z.enum(['ASC', 'DESC']).optional(),\n }).optional(),\n});\n\nexport type BookmarksRequestPayload = z.infer<typeof BookmarksRequestPayloadSchema>;\n\nexport const BookmarksRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('BOOKMARKS'),\n payload: BookmarksRequestPayloadSchema,\n});\n\nexport type BookmarksRequestMessage = z.infer<typeof BookmarksRequestMessageSchema>;\n\n// Artifacts request schema\nexport const ArtifactsQueryFiltersSchema = z.object({\n createdBy: z.string().optional(),\n type: z.string().optional(),\n status: z.string().optional(),\n name: z.string().optional(),\n deleted: z.boolean().optional(),\n createdAt: z.string().optional(),\n});\n\nexport type ArtifactsQueryFilters = z.infer<typeof ArtifactsQueryFiltersSchema>;\n\nexport const ArtifactsRequestPayloadSchema = z.object({\n operation: z.enum(['create', 'update', 'delete', 'getAll', 'getOne', 'query']),\n data: z.object({\n id: z.number().optional(),\n name: z.string().optional(),\n createdBy: z.string().optional(),\n dsl: z.record(z.any()).optional(),\n status: z.string().optional(),\n deleted: z.boolean().optional(),\n limit: z.number().optional(),\n offset: z.number().optional(),\n // Query operation fields\n filters: ArtifactsQueryFiltersSchema.optional(),\n sort: z.enum(['ASC', 'DESC']).optional(),\n // Menu grouping fields\n type: z.string().optional(),\n menuId: z.number().optional(),\n artifactGroupName: z.string().optional(),\n artifactGroupId: z.string().optional(),\n artifactGroupIcon: z.string().optional(),\n }).optional(),\n});\n\nexport type ArtifactsRequestPayload = z.infer<typeof ArtifactsRequestPayloadSchema>;\n\nexport const ArtifactsRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('ARTIFACTS'),\n payload: ArtifactsRequestPayloadSchema,\n});\n\nexport type ArtifactsRequestMessage = z.infer<typeof ArtifactsRequestMessageSchema>;\n\n// KB Nodes (Knowledge Base) request schema\nexport const KbNodeTypeSchema = z.enum(['global', 'user', 'query']);\nexport type KbNodeType = z.infer<typeof KbNodeTypeSchema>;\n\nexport const KbNodesQueryFiltersSchema = z.object({\n query: z.string().optional(),\n category: z.string().optional(),\n tags: z.array(z.string()).optional(),\n type: KbNodeTypeSchema.optional(),\n createdBy: z.string().optional(),\n});\n\nexport type KbNodesQueryFilters = z.infer<typeof KbNodesQueryFiltersSchema>;\n\nexport const KbNodesRequestPayloadSchema = z.object({\n operation: z.enum(['create', 'update', 'delete', 'getAll', 'getOne', 'search', 'getByCategory', 'getByUser', 'getCategories', 'getTags']),\n data: z.object({\n id: z.number().optional(),\n title: z.string().optional(),\n content: z.string().optional(),\n category: z.string().optional(),\n tags: z.array(z.string()).optional(),\n type: KbNodeTypeSchema.optional(),\n createdBy: z.string().optional(),\n updatedBy: z.string().optional(),\n userId: z.string().optional(),\n // Query/search operation fields\n query: z.string().optional(),\n filters: KbNodesQueryFiltersSchema.optional(),\n limit: z.number().optional(),\n offset: z.number().optional(),\n }).optional(),\n});\n\nexport type KbNodesRequestPayload = z.infer<typeof KbNodesRequestPayloadSchema>;\n\nexport const KbNodesRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('KB_NODES'),\n payload: KbNodesRequestPayloadSchema,\n});\n\nexport type KbNodesRequestMessage = z.infer<typeof KbNodesRequestMessageSchema>;\n\n// ==================== Menus CRUD Message Schemas ====================\n\n// Query filters schema for menus\nexport const MenusQueryFiltersSchema = z.object({\n parentId: z.number().nullable().optional(),\n isActive: z.boolean().optional(),\n});\n\nexport type MenusQueryFilters = z.infer<typeof MenusQueryFiltersSchema>;\n\n// Menus CRUD request payload\nexport const MenusRequestPayloadSchema = z.object({\n operation: z.enum([\n 'create',\n 'update',\n 'delete',\n 'getAll',\n 'getOne',\n 'getRootMenus',\n 'getChildMenus',\n 'getHierarchy',\n 'query',\n 'reorder'\n ]),\n data: z.object({\n id: z.number().optional(),\n name: z.string().optional(),\n componentName: z.string().optional(),\n icon: z.string().optional(),\n userMessage: z.string().optional(),\n parentId: z.number().nullable().optional(),\n sortOrder: z.number().optional(),\n props: z.record(z.unknown()).optional(),\n isActive: z.boolean().optional(),\n // Query operation fields\n filters: MenusQueryFiltersSchema.optional(),\n limit: z.number().optional(),\n sort: z.enum(['ASC', 'DESC']).optional(),\n // Reorder operation fields\n items: z.array(z.object({\n id: z.number(),\n sortOrder: z.number(),\n })).optional(),\n menuJson: z.record(z.unknown()).optional(),\n version: z.number().optional(),\n }).optional(),\n\n});\n\nexport type MenusRequestPayload = z.infer<typeof MenusRequestPayloadSchema>;\n\nexport const MenusRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('MENUS'),\n payload: MenusRequestPayloadSchema,\n});\n\nexport type MenusRequestMessage = z.infer<typeof MenusRequestMessageSchema>;\n\n// Menus response payload\nexport const MenusResponsePayloadSchema = z.object({\n success: z.boolean(),\n error: z.string().optional(),\n data: z.any().optional(),\n count: z.number().optional(),\n message: z.string().optional(),\n});\n\nexport type MenusResponsePayload = z.infer<typeof MenusResponsePayloadSchema>;\n\n// ==================== Dashboard Component Request Schema ====================\n\nexport const DashCompRequestPayloadSchema = z.object({\n prompt: z.string(),\n userId: z.string().optional(),\n /** Dashboard ID for scoping conversation history */\n dashboardId: z.string().optional(),\n SA_RUNTIME: z.object({\n threadId: z.string().optional(),\n uiBlockId: z.string().optional(),\n }).optional(),\n /** Existing components in the dashboard (for update/filter operations) */\n existingComponents: z.array(ComponentSchema).optional(),\n /** Request type: create (new component), update (modify existing), filter (create filter + update components) */\n req_type: z.enum(['create', 'update', 'filter']).optional(),\n});\n\nexport type DashCompRequestPayload = z.infer<typeof DashCompRequestPayloadSchema>;\n\nexport const DashCompRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('DASH_COMP_REQ'),\n payload: DashCompRequestPayloadSchema,\n});\n\nexport type DashCompRequestMessage = z.infer<typeof DashCompRequestMessageSchema>;\n\n// ==================== Report Component Request Schema ====================\n\nexport const ReportCompRequestPayloadSchema = z.object({\n prompt: z.string(),\n userId: z.string().optional(),\n reportId: z.string().optional(),\n projectId: z.string().optional(),\n SA_RUNTIME: z.object({\n threadId: z.string().optional(),\n uiBlockId: z.string().optional(),\n }).optional(),\n});\n\nexport type ReportCompRequestPayload = z.infer<typeof ReportCompRequestPayloadSchema>;\n\nexport const ReportCompRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('REPORT_COMP_REQ'),\n payload: ReportCompRequestPayloadSchema,\n});\n\nexport type ReportCompRequestMessage = z.infer<typeof ReportCompRequestMessageSchema>;\n\n// ==================== Schema Request/Response ====================\n\n/**\n * Schema column statistics\n */\nexport const SchemaColumnStatisticsSchema = z.object({\n distinct: z.number().optional(),\n min: z.number().optional(),\n max: z.number().optional(),\n});\n\nexport type SchemaColumnStatistics = z.infer<typeof SchemaColumnStatisticsSchema>;\n\n/**\n * Schema column definition\n */\nexport const SchemaColumnSchema = z.object({\n name: z.string(),\n type: z.string(),\n nativeType: z.string(),\n nullable: z.boolean(),\n description: z.string(),\n statistics: SchemaColumnStatisticsSchema.optional(),\n cardinality: z.enum(['unique', 'high', 'medium', 'low']).optional(),\n sampleValues: z.array(z.string()).optional(),\n});\n\nexport type SchemaColumn = z.infer<typeof SchemaColumnSchema>;\n\n/**\n * Schema table definition\n */\nexport const SchemaTableSchema = z.object({\n name: z.string(),\n fullName: z.string(),\n rowCount: z.number(),\n description: z.string(),\n columns: z.array(SchemaColumnSchema),\n});\n\nexport type SchemaTable = z.infer<typeof SchemaTableSchema>;\n\n/**\n * Schema relationship definition\n */\nexport const SchemaRelationshipSchema = z.object({\n from: z.string(),\n to: z.string(),\n type: z.string(),\n keys: z.array(z.string()),\n});\n\nexport type SchemaRelationship = z.infer<typeof SchemaRelationshipSchema>;\n\n/**\n * Database schema definition\n */\nexport const DatabaseSchemaSchema = z.object({\n database: z.string(),\n databaseType: z.string().optional(),\n schema: z.string(),\n description: z.string(),\n tables: z.array(SchemaTableSchema),\n relationships: z.array(SchemaRelationshipSchema).optional(),\n});\n\nexport type DatabaseSchema = z.infer<typeof DatabaseSchemaSchema>;\n\n/**\n * Schema request payload\n */\nexport const SchemaRequestPayloadSchema = z.object({\n /** If true, returns the formatted documentation string in addition to raw JSON */\n formatted: z.boolean().optional(),\n});\n\nexport type SchemaRequestPayload = z.infer<typeof SchemaRequestPayloadSchema>;\n\n/**\n * Schema request message\n */\nexport const SchemaRequestMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('SCHEMA_REQ'),\n payload: SchemaRequestPayloadSchema,\n});\n\nexport type SchemaRequestMessage = z.infer<typeof SchemaRequestMessageSchema>;\n\n/**\n * Schema response payload\n */\nexport const SchemaResponsePayloadSchema = z.object({\n success: z.boolean(),\n error: z.string().optional(),\n data: z.object({\n schema: DatabaseSchemaSchema,\n /** Formatted schema documentation (only if formatted: true was requested) */\n formatted: z.string().optional(),\n }).optional(),\n});\n\nexport type SchemaResponsePayload = z.infer<typeof SchemaResponsePayloadSchema>;\n\n/**\n * Schema response message\n */\nexport const SchemaResponseMessageSchema = z.object({\n id: z.string(),\n from: MessageParticipantSchema,\n type: z.literal('SCHEMA_RES'),\n payload: SchemaResponsePayloadSchema,\n});\n\nexport type SchemaResponseMessage = z.infer<typeof SchemaResponseMessageSchema>;\n\n// ==================== Response Types ====================\n\n// Standard response format for LLM operations\nexport interface T_RESPONSE {\n success: boolean;\n data?: any;\n errors: string[];\n}\n\nexport interface WebSocketLike {\n send(data: string): void;\n close(): void;\n addEventListener(event: string, listener: (event: any) => void): void;\n removeEventListener(event: string, listener: (event: any) => void): void;\n readyState: number;\n CONNECTING: number;\n OPEN: number;\n CLOSING: number;\n CLOSED: number;\n}\n\n","import { z } from 'zod';\n\n// ==================== Dashboard DSL Schemas ====================\n\n// Expression schema for dynamic values\nexport const ExpressionSchema = z.object({\n $exp: z.string(),\n $deps: z.array(z.string()).optional(),\n});\n\nexport type Expression = z.infer<typeof ExpressionSchema>;\n\n// Binding schema for data binding\nexport const BindingSchema = z.object({\n $bind: z.string(),\n $transform: z\n .array(\n z.object({\n name: z.string(),\n args: z.array(z.any()).optional(),\n }),\n )\n .optional(),\n});\n\nexport type Binding = z.infer<typeof BindingSchema>;\n\n// For directive schema\nexport const ForDirectiveSchema = z.object({\n in: z.union([ExpressionSchema, BindingSchema, z.string()]),\n as: z.string(),\n key: z.string().optional(),\n index: z.string().optional(),\n});\n\nexport type ForDirective = z.infer<typeof ForDirectiveSchema>;\n\n// Query specification schema\nexport const QuerySpecSchema = z.object({\n graphql: z.string().optional(),\n sql: z.string().optional(),\n variables: z.record(z.string(), z.any()).optional(),\n params: z.record(z.string(), z.any()).optional(),\n key: z.string().optional(),\n refetchPolicy: z\n .enum(['cache-first', 'network-only', 'cache-and-network'])\n .optional(),\n dependencies: z.array(z.string()).optional(),\n});\n\nexport type QuerySpec = z.infer<typeof QuerySpecSchema>;\n\n// UI Element schema\nexport const UIElementSchema: z.ZodType<any> = z.lazy(() =>\n z.object({\n id: z.string(),\n type: z.string(),\n key: z.union([z.string(), ExpressionSchema]).optional(),\n props: z.record(z.string(), z.any()).optional(),\n query: QuerySpecSchema.optional(),\n if: ExpressionSchema.optional(),\n elseIf: ExpressionSchema.optional(),\n for: ForDirectiveSchema.optional(),\n 'link-to': z\n .union([\n z.string(),\n ExpressionSchema,\n BindingSchema,\n z.object({\n ui: z.union([z.string(), ExpressionSchema, BindingSchema]),\n params: z.record(z.string(), z.any()).optional(),\n }),\n ])\n .optional(),\n _meta: z\n .object({\n id: z.string().optional(),\n version: z.string().optional(),\n created: z.string().optional(),\n lastModified: z.string().optional(),\n })\n .optional(),\n children: z.any().optional(),\n else: UIElementSchema.optional(),\n slots: z\n .record(z.string(), z.union([UIElementSchema, z.array(UIElementSchema)]))\n .optional(),\n platform: z\n .object({\n web: z.any().optional(),\n ios: z.any().optional(),\n android: z.any().optional(),\n })\n .optional(),\n })\n);\n\nexport type UIElement = z.infer<typeof UIElementSchema>;\n\n// Page schema for multi-page dashboards\nexport const PageSchema = z.object({\n id: z.string(),\n name: z.string(),\n order: z.number(),\n icon: z.string().optional(),\n render: UIElementSchema,\n});\n\nexport type Page = z.infer<typeof PageSchema>;\n\n// UI Component schema\nexport const UIComponentSchema = z.object({\n id: z.string(),\n name: z.string().optional(),\n props: z.record(z.string(), z.any()).optional(),\n states: z.record(z.string(), z.any()).optional(),\n methods: z\n .record(\n z.string(),\n z.object({\n fn: z.string(),\n params: z.record(z.string(), z.any()).optional(),\n }),\n )\n .optional(),\n effects: z\n .array(\n z.object({\n fn: z.string(),\n deps: z.array(z.string()).optional(),\n }),\n )\n .optional(),\n data: z.record(z.string(), z.any()).optional(),\n render: UIElementSchema.optional(),\n pages: z.array(PageSchema).optional(),\n defaultPageId: z.string().optional(),\n query: QuerySpecSchema.optional(),\n});\n\nexport type UIComponent = z.infer<typeof UIComponentSchema>;\n\n// DSL Renderer Props schema\nexport const DSLRendererPropsSchema = z.object({\n dsl: UIComponentSchema,\n data: z.record(z.string(), z.any()).optional(),\n context: z.record(z.string(), z.any()).optional(),\n});\n\nexport type DSLRendererProps = z.infer<typeof DSLRendererPropsSchema>;\n","import { z } from 'zod';\n\n// ==================== Report DSL Schemas ====================\n\n// Expression schema for dynamic values\nexport const ExpressionSchema = z.object({\n $exp: z.string(),\n $deps: z.array(z.string()).optional(),\n});\n\nexport type Expression = z.infer<typeof ExpressionSchema>;\n\n// Binding schema for data binding\nexport const BindingSchema = z.object({\n $bind: z.string(),\n $transform: z\n .array(\n z.object({\n name: z.string(),\n args: z.array(z.any()).optional(),\n }),\n )\n .optional(),\n});\n\nexport type Binding = z.infer<typeof BindingSchema>;\n\n// For directive schema\nexport const ForDirectiveSchema = z.object({\n in: z.union([ExpressionSchema, BindingSchema, z.string()]),\n as: z.string(),\n key: z.string().optional(),\n index: z.string().optional(),\n});\n\nexport type ForDirective = z.infer<typeof ForDirectiveSchema>;\n\n// Query specification schema\nexport const QuerySpecSchema = z.object({\n graphql: z.string().optional(),\n sql: z.string().optional(),\n variables: z.record(z.string(), z.any()).optional(),\n params: z.record(z.string(), z.any()).optional(),\n key: z.string().optional(),\n refetchPolicy: z\n .enum(['cache-first', 'network-only', 'cache-and-network'])\n .optional(),\n dependencies: z.array(z.string()).optional(),\n});\n\nexport type QuerySpec = z.infer<typeof QuerySpecSchema>;\n\n// UI Element schema\nexport const UIElementSchema: z.ZodType<any> = z.lazy(() =>\n z.object({\n id: z.string(),\n type: z.string(),\n key: z.union([z.string(), ExpressionSchema]).optional(),\n props: z.record(z.string(), z.any()).optional(),\n query: QuerySpecSchema.optional(),\n if: ExpressionSchema.optional(),\n elseIf: ExpressionSchema.optional(),\n for: ForDirectiveSchema.optional(),\n 'link-to': z\n .union([\n z.string(),\n ExpressionSchema,\n BindingSchema,\n z.object({\n ui: z.union([z.string(), ExpressionSchema, BindingSchema]),\n params: z.record(z.string(), z.any()).optional(),\n }),\n ])\n .optional(),\n _meta: z\n .object({\n id: z.string().optional(),\n version: z.string().optional(),\n created: z.string().optional(),\n lastModified: z.string().optional(),\n })\n .optional(),\n children: z.any().optional(),\n else: UIElementSchema.optional(),\n slots: z\n .record(z.string(), z.union([UIElementSchema, z.array(UIElementSchema)]))\n .optional(),\n platform: z\n .object({\n web: z.any().optional(),\n ios: z.any().optional(),\n android: z.any().optional(),\n })\n .optional(),\n })\n);\n\nexport type UIElement = z.infer<typeof UIElementSchema>;\n\n// UI Component schema\nexport const UIComponentSchema = z.object({\n id: z.string(),\n name: z.string().optional(),\n props: z.record(z.string(), z.any()).optional(),\n states: z.record(z.string(), z.any()).optional(),\n methods: z\n .record(\n z.string(),\n z.object({\n fn: z.string(),\n params: z.record(z.string(), z.any()).optional(),\n }),\n )\n .optional(),\n effects: z\n .array(\n z.object({\n fn: z.string(),\n deps: z.array(z.string()).optional(),\n }),\n )\n .optional(),\n data: z.record(z.string(), z.any()).optional(),\n render: UIElementSchema,\n query: QuerySpecSchema.optional(),\n});\n\nexport type UIComponent = z.infer<typeof UIComponentSchema>;\n\n// DSL Renderer Props schema\nexport const DSLRendererPropsSchema = z.object({\n dsl: UIComponentSchema,\n data: z.record(z.string(), z.any()).optional(),\n context: z.record(z.string(), z.any()).optional(),\n});\n\nexport type DSLRendererProps = z.infer<typeof DSLRendererPropsSchema>;\n","import fs from 'fs';\nconst PREFIX = '[SuperatomSDK]';\nconst LOG_FILE_PATH = 'superatom-sdk.log';\n\n/**\n * Log levels in hierarchical order\n * - errors: only error logs\n * - warnings: warning + error logs\n * - info: info + warning + error logs\n * - verbose: all logs including debug\n */\nexport type LogLevel = 'errors' | 'warnings' | 'info' | 'verbose';\n\n// open a file in stream write mode. if no file is specified then create it.\nlet LOGSTREAM = fs.createWriteStream(LOG_FILE_PATH, { flags: 'a' });\n\n/**\n * Internal log level hierarchy mapping\n */\nconst LOG_LEVEL_PRIORITY: Record<LogLevel, number> = {\n errors: 0,\n warnings: 1,\n info: 2,\n verbose: 3,\n};\n\nconst MESSAGE_LEVEL_PRIORITY: Record<'error' | 'warn' | 'info' | 'debug', number> = {\n error: 0,\n warn: 1,\n info: 2,\n debug: 3,\n};\n\n/**\n * Logger class with environment-based log level support\n */\nclass Logger {\n private currentLevel: LogLevel;\n private currentLevelPriority: number;\n\n constructor() {\n // Read log level from environment variable, default to 'info'\n const envLevel = (process.env.SUPERATOM_LOG_LEVEL || 'info').toLowerCase();\n\n // Validate and set log level\n if (this.isValidLogLevel(envLevel)) {\n this.currentLevel = envLevel as LogLevel;\n } else {\n this.currentLevel = 'info';\n console.warn(\n `${PREFIX} Invalid log level \"${envLevel}\". Using default \"info\". Valid levels: errors, warnings, info, verbose`\n );\n }\n\n this.currentLevelPriority = LOG_LEVEL_PRIORITY[this.currentLevel];\n }\n\n /**\n * Check if a string is a valid log level\n */\n private isValidLogLevel(level: string): level is LogLevel {\n return level === 'errors' || level === 'warnings' || level === 'info' || level === 'verbose';\n }\n\n /**\n * Check if a message should be logged based on current log level\n */\n private shouldLog(messageLevel: 'error' | 'warn' | 'info' | 'debug'): boolean {\n const messagePriority = MESSAGE_LEVEL_PRIORITY[messageLevel];\n return messagePriority <= this.currentLevelPriority;\n }\n\n /**\n * Get current log level\n */\n getLogLevel(): LogLevel {\n return this.currentLevel;\n }\n\n /**\n * Set log level programmatically\n */\n setLogLevel(level: LogLevel): void {\n this.currentLevel = level;\n this.currentLevelPriority = LOG_LEVEL_PRIORITY[level];\n }\n\n /**\n * Log info message (shown for info and verbose levels)\n */\n info(...args: any[]): void {\n if (this.shouldLog('info')) {\n console.log(PREFIX, ...args);\n }\n }\n\n /**\n * Log error message (shown for all levels)\n */\n error(...args: any[]): void {\n if (this.shouldLog('error')) {\n console.error(PREFIX, ...args);\n }\n }\n\n /**\n * Log warning message (shown for warnings, info, and verbose levels)\n */\n warn(...args: any[]): void {\n if (this.shouldLog('warn')) {\n console.warn(PREFIX, ...args);\n }\n }\n\n /**\n * Log debug message (only shown for verbose level)\n */\n debug(...args: any[]): void {\n if (this.shouldLog('debug')) {\n console.log(PREFIX, '[DEBUG]', ...args);\n }\n }\n\n /**\n * Write to log file\n */\n file(...args: any[]): void {\n LOGSTREAM.write(args.join(' ') + '\\n');\n }\n\n /**\n * Clear the log file (call at start of new user request)\n */\n clearFile(): void {\n LOGSTREAM.end();\n LOGSTREAM = fs.createWriteStream(LOG_FILE_PATH, { flags: 'w' }); // 'w' flag truncates the file\n LOGSTREAM.write(`\\n${'='.repeat(80)}\\n`);\n LOGSTREAM.write(`NEW REQUEST - ${new Date().toISOString()}\\n`);\n LOGSTREAM.write(`${'='.repeat(80)}\\n\\n`);\n }\n\n /**\n * Log LLM method prompts with clear labeling\n */\n logLLMPrompt(methodName: string, promptType: 'system' | 'user', content: string | object | any[]): void {\n const header = `\\n${'#'.repeat(80)}\\n[LLM METHOD: ${methodName}] - ${promptType.toUpperCase()} PROMPT\\n${'#'.repeat(80)}\\n`;\n LOGSTREAM.write(header);\n\n // Handle different content types\n let contentStr: string;\n if (typeof content === 'string') {\n contentStr = content;\n } else if (Array.isArray(content)) {\n // Array of message blocks - extract text content\n contentStr = content.map((item: any) => {\n if (typeof item === 'string') return item;\n if (item.text) return item.text;\n if (item.content) return typeof item.content === 'string' ? item.content : JSON.stringify(item.content, null, 2);\n return JSON.stringify(item, null, 2);\n }).join('\\n\\n');\n } else if (typeof content === 'object') {\n contentStr = JSON.stringify(content, null, 2);\n } else {\n contentStr = String(content);\n }\n\n LOGSTREAM.write(contentStr);\n LOGSTREAM.write(`\\n${'#'.repeat(80)}\\n\\n`);\n }\n}\n\n// Export singleton instance\nexport const logger = new Logger();\n","import { randomUUID } from 'crypto';\nimport { logger } from '../utils/logger';\nimport { STORAGE_CONFIG } from '../config/storage';\nimport { Action } from './action';\n\n/**\n * UIBlock represents a single user and assistant message block in a thread\n * Contains user question, component metadata, component data, text response, and available actions\n */\nexport class UIBlock {\n private id: string;\n private userQuestion: string;\n private generatedComponentMetadata: Record<string, any>;\n private componentData: Record<string, any>;\n private textResponse: string | null;\n private actions: Action[] | null | Promise<Action[]>;\n private createdAt: Date;\n\n /**\n * Creates a new UIBlock instance\n * @param userQuestion - The user's question or input\n * @param componentData - The component data object\n * @param generatedComponentMetadata - Optional metadata about the generated component\n * @param actions - Optional array of available actions\n * @param id - Optional custom ID, generates UUID if not provided\n * @param textResponse - Optional text response from LLM\n */\n constructor(\n userQuestion: string,\n componentData: Record<string, any> = {},\n generatedComponentMetadata: Record<string, any> = {},\n actions: Action[] = [],\n id?: string,\n textResponse: string | null = null\n ) {\n this.id = id || randomUUID();\n this.userQuestion = userQuestion;\n this.componentData = componentData;\n this.generatedComponentMetadata = generatedComponentMetadata;\n this.actions = actions;\n this.textResponse = textResponse;\n this.createdAt = new Date();\n }\n\n /**\n * Get the UIBlock ID\n */\n getId(): string {\n return this.id;\n }\n\n /**\n * Get the user question\n */\n getUserQuestion(): string {\n return this.userQuestion;\n }\n\n /**\n * Set or update the user question\n */\n setUserQuestion(question: string): void {\n this.userQuestion = question;\n }\n\n /**\n * Get component metadata\n */\n getComponentMetadata(): Record<string, any> {\n return this.generatedComponentMetadata;\n }\n\n getTextResponse(): string {\n return this.textResponse || '';\n }\n\n /**\n * Set or update component metadata\n */\n setComponentMetadata(metadata: Record<string, any>): void {\n this.generatedComponentMetadata = { ...this.generatedComponentMetadata, ...metadata };\n }\n\n /**\n * Get component data\n */\n getComponentData(): Record<string, any> {\n return this.componentData;\n }\n\n /**\n * Calculate size of data in bytes\n */\n private getDataSizeInBytes(data: any): number {\n try {\n const jsonString = JSON.stringify(data);\n return Buffer.byteLength(jsonString, 'utf8');\n } catch (error) {\n logger.error('Error calculating data size:', error);\n return 0;\n }\n }\n\n /**\n * Limit array data to maximum rows\n */\n private limitArrayData(data: any[]): { data: any[]; metadata: any } {\n const totalRows = data.length;\n const limitedData = data.slice(0, STORAGE_CONFIG.MAX_ROWS_PER_BLOCK);\n\n return {\n data: limitedData,\n metadata: {\n totalRows,\n storedRows: limitedData.length,\n isTruncated: totalRows > STORAGE_CONFIG.MAX_ROWS_PER_BLOCK,\n },\n };\n }\n\n /**\n * Check if data exceeds size limit\n */\n private exceedsSizeLimit(data: any): boolean {\n const size = this.getDataSizeInBytes(data);\n return size > STORAGE_CONFIG.MAX_SIZE_PER_BLOCK_BYTES;\n }\n\n /**\n * Process and limit data before storing\n */\n private processDataForStorage(data: any): any {\n // If data is an array, limit rows\n if (Array.isArray(data)) {\n const { data: limitedData, metadata } = this.limitArrayData(data);\n\n // Check size after limiting rows\n const size = this.getDataSizeInBytes(limitedData);\n\n logger.info(\n `UIBlock ${this.id}: Storing ${metadata.storedRows}/${metadata.totalRows} rows (${(size / 1024).toFixed(2)} KB)`\n );\n\n // If still too large, store only metadata\n if (this.exceedsSizeLimit(limitedData)) {\n logger.warn(\n `UIBlock ${this.id}: Data too large (${(size / 1024 / 1024).toFixed(2)} MB), storing metadata only`\n );\n return {\n ...metadata,\n preview: limitedData.slice(0, 3), // Store only first 3 rows as preview\n dataTooLarge: true,\n };\n }\n\n return {\n data: limitedData,\n ...metadata,\n };\n }\n\n // For non-array data, check size\n const size = this.getDataSizeInBytes(data);\n\n if (this.exceedsSizeLimit(data)) {\n logger.warn(\n `UIBlock ${this.id}: Data too large (${(size / 1024 / 1024).toFixed(2)} MB), storing summary only`\n );\n return {\n dataTooLarge: true,\n dataType: typeof data,\n keys: typeof data === 'object' ? Object.keys(data) : undefined,\n };\n }\n\n return data;\n }\n\n /**\n * Set or update component data with size and row limits\n */\n setComponentData(data: Record<string, any>): void {\n const processedData = this.processDataForStorage(data);\n this.componentData = { ...this.componentData, ...processedData };\n }\n\n\n\n /**\n * Set or update text response\n */\n setTextResponse(textResponse: string | null): void {\n this.textResponse = textResponse;\n }\n\n /**\n * Get all actions (only if they are resolved, not if fetching)\n */\n getActions(): Action[] | null | Promise<Action[]> {\n return this.actions;\n }\n\n /**\n * Get or fetch actions\n * If actions don't exist or are a Promise, calls the generateFn and stores the promise\n * If actions already exist, returns them\n * @param generateFn - Async function to generate actions\n * @returns Promise resolving to Action[]\n */\n async getOrFetchActions(generateFn: () => Promise<Action[]>): Promise<Action[]> {\n // If actions already exist and are not a Promise, return them\n\n if (this.actions && !(this.actions instanceof Promise) && Array.isArray(this.actions) && this.actions.length > 0) {\n return this.actions;\n }\n\n // If already fetching, cancel and start new fetch\n // Set new promise for fetching\n const fetchPromise = generateFn();\n this.actions = fetchPromise;\n\n try {\n // Wait for the promise to resolve\n const resolvedActions = await fetchPromise;\n // Store the resolved actions\n logger.info(`Fetched ${resolvedActions.length} actions for UIBlock: ${this.id}`);\n this.actions = resolvedActions;\n return resolvedActions;\n } catch (error) {\n // If generation fails, reset to null\n this.actions = null;\n throw error;\n }\n }\n\n /**\n * Set or replace all actions\n */\n setActions(actions: Action[]): void {\n this.actions = actions;\n }\n\n /**\n * Add a single action (only if actions are resolved)\n */\n addAction(action: Action): void {\n if (this.actions && Array.isArray(this.actions)) {\n this.actions.push(action);\n }\n }\n\n /**\n * Add multiple actions (only if actions are resolved)\n */\n addActions(actions: Action[]): void {\n if (this.actions && Array.isArray(this.actions)) {\n this.actions.push(...actions);\n }\n }\n\n /**\n * Remove an action by ID (only if actions are resolved)\n */\n removeAction(actionId: string): boolean {\n if (this.actions && Array.isArray(this.actions)) {\n const index = this.actions.findIndex(a => a.id === actionId);\n if (index > -1) {\n this.actions.splice(index, 1);\n return true;\n }\n }\n return false;\n }\n\n /**\n * Clear all actions\n */\n clearActions(): void {\n this.actions = null;\n }\n\n /**\n * Get creation timestamp\n */\n getCreatedAt(): Date {\n return this.createdAt;\n }\n\n /**\n * Convert UIBlock to JSON-serializable object\n */\n toJSON(): Record<string, any> {\n // Handle Promise case for serialization\n let actionsValue: Action[] | null = null;\n if (this.actions && !(this.actions instanceof Promise) && Array.isArray(this.actions)) {\n actionsValue = this.actions;\n }\n\n return {\n id: this.id,\n userQuestion: this.userQuestion,\n generatedComponentMetadata: this.generatedComponentMetadata,\n componentData: this.componentData,\n textResponse: this.textResponse,\n actions: actionsValue,\n isFetchingActions: this.actions instanceof Promise,\n createdAt: this.createdAt.toISOString(),\n };\n }\n}\n","/**\n * Configuration for data storage limits in UIBlocks\n */\nexport const STORAGE_CONFIG = {\n /**\n * Maximum number of rows to store in UIBlock data\n */\n MAX_ROWS_PER_BLOCK: 10,\n\n /**\n * Maximum size in bytes per UIBlock (500KB - reduced to save memory)\n */\n MAX_SIZE_PER_BLOCK_BYTES: 500 * 1024, // 500KB\n\n /**\n * Number of days to keep threads before cleanup\n * Note: This is for in-memory storage. Conversations are also persisted to database.\n */\n THREAD_RETENTION_DAYS: 2, // Reduced from 7 to 1 day for memory efficiency\n\n /**\n * Number of days to keep UIBlocks before cleanup\n * Note: This is for in-memory storage. Data is also persisted to database.\n */\n UIBLOCK_RETENTION_DAYS: 2, // Reduced from 7 to 1 day for memory efficiency\n};\n","import { randomUUID } from 'crypto';\nimport { UIBlock } from './uiblock';\n\n/**\n * Thread represents a conversation thread containing multiple UIBlocks\n * Each UIBlock in a thread represents a user question and assistant response pair\n */\nexport class Thread {\n private id: string;\n private uiblocks: Map<string, UIBlock>;\n private createdAt: Date;\n\n /**\n * Creates a new Thread instance\n * @param id - Optional custom ID, generates UUID if not provided\n */\n constructor(id?: string) {\n this.id = id || randomUUID();\n this.uiblocks = new Map();\n this.createdAt = new Date();\n }\n\n /**\n * Get the thread ID\n */\n getId(): string {\n return this.id;\n }\n\n /**\n * Add a UIBlock to the thread\n */\n addUIBlock(uiblock: UIBlock): void {\n this.uiblocks.set(uiblock.getId(), uiblock);\n }\n\n /**\n * Get a UIBlock by ID\n */\n getUIBlock(id: string): UIBlock | undefined {\n return this.uiblocks.get(id);\n }\n\n /**\n * Get all UIBlocks in the thread\n */\n getUIBlocks(): UIBlock[] {\n return Array.from(this.uiblocks.values());\n }\n\n /**\n * Get UIBlocks as a Map\n */\n getUIBlocksMap(): Map<string, UIBlock> {\n return new Map(this.uiblocks);\n }\n\n /**\n * Remove a UIBlock by ID\n */\n removeUIBlock(id: string): boolean {\n return this.uiblocks.delete(id);\n }\n\n /**\n * Check if UIBlock exists\n */\n hasUIBlock(id: string): boolean {\n return this.uiblocks.has(id);\n }\n\n /**\n * Get number of UIBlocks in the thread\n */\n getUIBlockCount(): number {\n return this.uiblocks.size;\n }\n\n /**\n * Clear all UIBlocks from the thread\n */\n clear(): void {\n this.uiblocks.clear();\n }\n\n /**\n * Get creation timestamp\n */\n getCreatedAt(): Date {\n return this.createdAt;\n }\n\n /**\n * Get conversation context from recent UIBlocks (excluding current one)\n * Returns formatted string with previous questions and component summaries\n * @param limit - Maximum number of previous UIBlocks to include (default: 5)\n * @param currentUIBlockId - ID of current UIBlock to exclude from context (optional)\n * @returns Formatted conversation history string\n */\n getConversationContext(limit: number = 5, currentUIBlockId?: string): string {\n if (limit === 0) {\n return '';\n }\n\n // Get all UIBlocks sorted by creation time (oldest first)\n const allBlocks = Array.from(this.uiblocks.values())\n .filter(block => !currentUIBlockId || block.getId() !== currentUIBlockId)\n .sort((a, b) => a.getCreatedAt().getTime() - b.getCreatedAt().getTime());\n\n if (allBlocks.length === 0) {\n return '';\n }\n\n // Take the last N blocks (most recent)\n const recentBlocks = allBlocks.slice(-limit);\n\n // Format as conversation history\n const contextLines: string[] = [];\n\n recentBlocks.forEach((block, index) => {\n const questionNum = index + 1;\n const question = block.getUserQuestion();\n const metadata = block.getComponentMetadata();\n const textResponse = block.getTextResponse();\n\n // Determine what was generated and build appropriate response\n let assistantResponse = '';\n\n // Check if component was generated (metadata exists and has meaningful content)\n const hasComponent = metadata && Object.keys(metadata).length > 0 && metadata.type;\n\n // Check if text response was generated\n const hasTextResponse = textResponse && textResponse.trim().length > 0;\n\n const responseParts: string[] = [];\n\n if (hasComponent) {\n // Component was generated - show brief summary only.\n // Do NOT include full props JSON — it contains SQL queries, queryIds,\n // component configs, and layout data that bloats conversation history\n // by ~13K tokens per previous question. The main agent only needs to\n // know what was asked and what type of answer was generated.\n const parts: string[] = [];\n\n if (metadata.type) {\n parts.push(`Component Type: ${metadata.type}`);\n }\n if (metadata.name) {\n parts.push(`Name: ${metadata.name}`);\n }\n if (metadata.description) {\n parts.push(`Description: ${metadata.description}`);\n }\n\n // Extract only the layout title from props (if available) — not the full JSON\n const layoutTitle = metadata.props?.config?.title;\n if (layoutTitle) {\n parts.push(`Dashboard Title: ${layoutTitle}`);\n }\n\n responseParts.push(parts.join('\\n'));\n }\n\n if (hasTextResponse) {\n // Text response was generated - add a trimmed version.\n // The full text can contain source agent internals (SQL, schema searches,\n // data previews, stream markers) which bloat conversation history.\n // Truncate to first 500 chars — enough for the main agent to understand\n // what was previously discussed without re-sending full analysis.\n const trimmedText = textResponse.length > 500\n ? textResponse.substring(0, 500) + '...[truncated]'\n : textResponse;\n responseParts.push(trimmedText);\n }\n\n if (responseParts.length > 0) {\n // Join both component and text response if both exist\n assistantResponse = responseParts.join('\\n');\n } else {\n // Nothing was generated\n assistantResponse = 'No response generated';\n }\n\n contextLines.push(`User:\\n ${question}`);\n contextLines.push(`Assistant:\\n ${assistantResponse}`);\n contextLines.push('---'); // Empty line for readability\n });\n\n return contextLines.join('\\n').trim();\n }\n\n /**\n * Convert Thread to JSON-serializable object\n */\n toJSON(): Record<string, any> {\n return {\n id: this.id,\n uiblocks: Array.from(this.uiblocks.values()).map(block => block.toJSON()),\n createdAt: this.createdAt.toISOString(),\n };\n }\n}\n","import { Thread } from './thread';\nimport { UIBlock } from './uiblock';\n\n/**\n * ThreadManager manages all threads globally\n * Provides methods to create, retrieve, and delete threads.\n * Includes automatic cleanup to prevent unbounded memory growth.\n */\nexport class ThreadManager {\n private static instance: ThreadManager;\n private threads: Map<string, Thread>;\n private cleanupInterval: NodeJS.Timeout | null = null;\n\n // Threads older than 7 days are cleaned up\n private readonly threadTtlMs = 7 * 24 * 60 * 60 * 1000;\n\n private constructor() {\n this.threads = new Map();\n this.startCleanup();\n }\n\n /**\n * Periodically remove threads older than 7 days.\n * Runs every hour to avoid frequent iteration over the map.\n */\n private startCleanup(): void {\n this.cleanupInterval = setInterval(() => {\n const now = Date.now();\n let removedCount = 0;\n for (const [id, thread] of this.threads.entries()) {\n if (now - thread.getCreatedAt().getTime() > this.threadTtlMs) {\n this.threads.delete(id);\n removedCount++;\n }\n }\n if (removedCount > 0) {\n console.log(`[ThreadManager] Cleaned up ${removedCount} threads older than 7 days (${this.threads.size} remaining)`);\n }\n }, 24 * 60 * 60 * 1000); // Run once per day\n }\n\n /**\n * Get singleton instance of ThreadManager\n */\n static getInstance(): ThreadManager {\n if (!ThreadManager.instance) {\n ThreadManager.instance = new ThreadManager();\n }\n return ThreadManager.instance;\n }\n\n /**\n * Create a new thread\n * @param id - Optional custom ID, generates UUID if not provided\n * @returns The created Thread instance\n */\n createThread(id?: string): Thread {\n const thread = new Thread(id);\n this.threads.set(thread.getId(), thread);\n return thread;\n }\n\n /**\n * Get a thread by ID\n */\n getThread(id: string): Thread | undefined {\n return this.threads.get(id);\n }\n\n /**\n * Get all threads\n */\n getAllThreads(): Thread[] {\n return Array.from(this.threads.values());\n }\n\n /**\n * Get threads as a Map\n */\n getThreadsMap(): Map<string, Thread> {\n return new Map(this.threads);\n }\n\n /**\n * Delete a thread by ID\n */\n deleteThread(id: string): boolean {\n return this.threads.delete(id);\n }\n\n /**\n * Check if thread exists\n */\n hasThread(id: string): boolean {\n return this.threads.has(id);\n }\n\n /**\n * Get number of threads\n */\n getThreadCount(): number {\n return this.threads.size;\n }\n\n /**\n * Clear all threads\n */\n clearAll(): void {\n this.threads.clear();\n }\n\n /**\n * Find a UIBlock by ID across all threads\n * @param uiBlockId - The UIBlock ID to search for\n * @returns Object with thread and uiBlock if found, undefined otherwise\n */\n findUIBlockById(uiBlockId: string): { thread: Thread; uiBlock: UIBlock } | undefined {\n for (const thread of this.threads.values()) {\n const uiBlock = thread.getUIBlock(uiBlockId);\n if (uiBlock) {\n return { thread, uiBlock };\n }\n }\n return undefined;\n }\n\n /**\n * Convert all threads to JSON-serializable object\n */\n toJSON(): Record<string, any> {\n return {\n threads: Array.from(this.threads.values()).map(thread => thread.toJSON()),\n count: this.threads.size,\n };\n }\n}\n","import crypto from 'crypto';\nimport { logger } from './logger';\n\ninterface CacheEntry {\n\tquery: string; // The query string (for debugging/inspection)\n\tdata: any;\n\ttimestamp: number;\n}\n\n/**\n * Query Cache — Two mechanisms:\n *\n * 1. `cache` (query string → result data) — TTL-based with max size, for avoiding re-execution\n * of recently validated queries. True LRU eviction: reads bubble entries to the back via\n * delete+re-set so the oldest *unused* entry is evicted, not the oldest *inserted*.\n *\n * 2. Encrypted queryId tokens — SQL is encrypted into the queryId itself (self-contained).\n * No server-side storage needed for SQL mappings. The token is decrypted on each request.\n * This eliminates the unbounded queryIdCache that previously grew forever and caused\n * memory bloat (hundreds of MBs after thousands of queries).\n *\n * Result data can still be cached temporarily via the data cache (mechanism 1).\n */\nclass QueryCache {\n\tprivate cache: Map<string, CacheEntry> = new Map();\n\tprivate ttlMs: number = 10 * 60 * 1000; // Default: 10 minutes\n\tprivate maxCacheSize: number = 5000; // Max data cache entries\n\tprivate cleanupInterval: NodeJS.Timeout | null = null;\n\n\t// Encryption for queryId tokens\n\tprivate readonly algorithm = 'aes-256-gcm';\n\tprivate encryptionKey: Buffer;\n\n\tconstructor() {\n\t\t// Derive a stable encryption key from an env variable or a default.\n\t\t// The key must be consistent across the process lifetime so tokens\n\t\t// generated before a hot-reload still decrypt correctly.\n\t\t// On PM2 restart, old tokens in cached conversations are re-registered\n\t\t// via _queryMap rehydration, so key rotation on restart is acceptable.\n\t\tconst keySource = process.env.QUERY_TOKEN_SECRET || 'superatom-query-cache-default-key-v1';\n\t\tthis.encryptionKey = crypto.createHash('sha256').update(keySource).digest();\n\n\t\tthis.startCleanup();\n\t}\n\n\t// ============================================\n\t// Data Cache (TTL-based, max size)\n\t// ============================================\n\n\t/**\n\t * Set the cache TTL (Time To Live)\n\t * @param minutes - TTL in minutes (default: 10)\n\t */\n\tsetTTL(minutes: number): void {\n\t\tthis.ttlMs = minutes * 60 * 1000;\n\t\tlogger.info(`[QueryCache] TTL set to ${minutes} minutes`);\n\t}\n\n\t/**\n\t * Get the current TTL in minutes\n\t */\n\tgetTTL(): number {\n\t\treturn this.ttlMs / 60 / 1000;\n\t}\n\n\t/**\n\t * Store query result in data cache.\n\t * If the key already exists, it's removed first so the re-insert places it\n\t * at the back of the iteration order (LRU). Eviction only fires when adding\n\t * a genuinely new key past the size limit.\n\t */\n\tset(query: string, data: any): void {\n\t\t// Drop the existing entry (if any) so re-insert moves it to the back.\n\t\t// Also keeps the size check accurate for genuinely-new keys.\n\t\tthis.cache.delete(query);\n\n\t\t// Evict oldest only if we're about to grow past the cap.\n\t\tif (this.cache.size >= this.maxCacheSize) {\n\t\t\tconst oldestKey = this.cache.keys().next().value;\n\t\t\tif (oldestKey) this.cache.delete(oldestKey);\n\t\t}\n\n\t\tthis.cache.set(query, {\n\t\t\tquery,\n\t\t\tdata,\n\t\t\ttimestamp: Date.now()\n\t\t});\n\t\tlogger.debug(`[QueryCache] Stored result for query (${query.substring(0, 50)}...)`);\n\t}\n\n\t/**\n\t * Get cached result if exists and not expired.\n\t * On hit, re-inserts the entry so it moves to the back of the Map's\n\t * iteration order — turning FIFO eviction into true LRU.\n\t */\n\tget(query: string): any | null {\n\t\tconst entry = this.cache.get(query);\n\t\tif (!entry) return null;\n\n\t\tif (Date.now() - entry.timestamp > this.ttlMs) {\n\t\t\tthis.cache.delete(query);\n\t\t\treturn null;\n\t\t}\n\n\t\t// LRU bump: re-insert so this key becomes the newest in iteration order.\n\t\tthis.cache.delete(query);\n\t\tthis.cache.set(query, entry);\n\n\t\tlogger.info(`[QueryCache] Cache HIT for query (${query.substring(0, 50)}...)`);\n\t\treturn entry.data;\n\t}\n\n\t/**\n\t * Check if query exists in cache (not expired)\n\t */\n\thas(query: string): boolean {\n\t\treturn this.get(query) !== null;\n\t}\n\n\t/**\n\t * Remove a specific query from cache\n\t */\n\tdelete(query: string): void {\n\t\tthis.cache.delete(query);\n\t}\n\n\t/**\n\t * Clear all cached entries\n\t */\n\tclear(): void {\n\t\tthis.cache.clear();\n\t\tlogger.info('[QueryCache] Cache cleared');\n\t}\n\n\t/**\n\t * Get cache statistics\n\t */\n\tgetStats(): { size: number; queryIdCount: number; oldestEntryAge: number | null } {\n\t\tlet oldestTimestamp: number | null = null;\n\t\tfor (const entry of this.cache.values()) {\n\t\t\tif (oldestTimestamp === null || entry.timestamp < oldestTimestamp) {\n\t\t\t\toldestTimestamp = entry.timestamp;\n\t\t\t}\n\t\t}\n\t\treturn {\n\t\t\tsize: this.cache.size,\n\t\t\tqueryIdCount: 0, // No longer stored in memory\n\t\t\toldestEntryAge: oldestTimestamp ? Date.now() - oldestTimestamp : null\n\t\t};\n\t}\n\n\t/**\n\t * Start periodic cleanup of expired data cache entries.\n\t */\n\tprivate startCleanup(): void {\n\t\tthis.cleanupInterval = setInterval(() => {\n\t\t\tconst now = Date.now();\n\t\t\tlet expiredCount = 0;\n\t\t\tfor (const [key, entry] of this.cache.entries()) {\n\t\t\t\tif (now - entry.timestamp > this.ttlMs) {\n\t\t\t\t\tthis.cache.delete(key);\n\t\t\t\t\texpiredCount++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (expiredCount > 0) {\n\t\t\t\tlogger.debug(`[QueryCache] Cleaned up ${expiredCount} expired data-cache entries (${this.cache.size} remaining)`);\n\t\t\t}\n\t\t}, 2 * 60 * 1000);\n\t}\n\n\t// ============================================\n\t// Encrypted Query ID Tokens (zero memory)\n\t// ============================================\n\n\t/**\n\t * Encrypt a payload into a self-contained token.\n\t */\n\tprivate encrypt(payload: string): string {\n\t\tconst iv = crypto.randomBytes(12);\n\t\tconst cipher = crypto.createCipheriv(this.algorithm, this.encryptionKey, iv);\n\t\tlet encrypted = cipher.update(payload, 'utf8', 'base64');\n\t\tencrypted += cipher.final('base64');\n\t\tconst authTag = cipher.getAuthTag();\n\t\t// Pack: iv(12) + authTag(16) + ciphertext — all base64\n\t\treturn iv.toString('base64') + '.' + authTag.toString('base64') + '.' + encrypted;\n\t}\n\n\t/**\n\t * Decrypt a token back to the original payload.\n\t */\n\tprivate decrypt(token: string): string | null {\n\t\ttry {\n\t\t\tconst parts = token.split('.');\n\t\t\tif (parts.length !== 3) return null;\n\n\t\t\tconst iv = Buffer.from(parts[0], 'base64');\n\t\t\tconst authTag = Buffer.from(parts[1], 'base64');\n\t\t\tconst encrypted = parts[2];\n\n\t\t\tconst decipher = crypto.createDecipheriv(this.algorithm, this.encryptionKey, iv);\n\t\t\tdecipher.setAuthTag(authTag);\n\t\t\tlet decrypted = decipher.update(encrypted, 'base64', 'utf8');\n\t\t\tdecrypted += decipher.final('utf8');\n\t\t\treturn decrypted;\n\t\t} catch (err) {\n\t\t\tlogger.warn(`[QueryCache] Failed to decrypt queryId token: ${err instanceof Error ? err.message : String(err)}`);\n\t\t\treturn null;\n\t\t}\n\t}\n\n\t/**\n\t * Store a query by generating an encrypted token as queryId.\n\t * The SQL is encrypted INTO the token — nothing stored in memory.\n\t * If data is provided, it's cached temporarily in the data cache.\n\t */\n\tstoreQuery(query: any, data?: any): string {\n\t\tconst payload = typeof query === 'string' ? query : JSON.stringify(query);\n\t\tconst queryId = this.encrypt(payload);\n\n\t\t// Cache the result data temporarily (if provided) using the token as key\n\t\tif (data) {\n\t\t\tthis.set(queryId, data);\n\t\t}\n\n\t\tconst queryPreview = payload.substring(0, 50);\n\t\tlogger.debug(`[QueryCache] Stored query as encrypted token (${queryPreview}...)`);\n\t\treturn queryId;\n\t}\n\n\t/**\n\t * Get a stored query by decrypting its token.\n\t * Returns the SQL + any cached result data.\n\t */\n\tgetQuery(queryId: string): { query: any; data: any } | null {\n\t\tconst decrypted = this.decrypt(queryId);\n\t\tif (!decrypted) return null;\n\n\t\t// Try to parse as JSON (for object queries), fall back to plain string\n\t\tlet query: any;\n\t\ttry {\n\t\t\tquery = JSON.parse(decrypted);\n\t\t} catch {\n\t\t\tquery = decrypted;\n\t\t}\n\n\t\t// Check data cache for cached result\n\t\tconst cachedData = this.get(queryId);\n\n\t\treturn { query, data: cachedData };\n\t}\n\n\t/**\n\t * Update cached data for a queryId token\n\t */\n\tsetQueryData(queryId: string, data: any): void {\n\t\tthis.set(queryId, data);\n\t}\n\n\t/**\n\t * Stop cleanup interval (for graceful shutdown)\n\t */\n\tdestroy(): void {\n\t\tif (this.cleanupInterval) {\n\t\t\tclearInterval(this.cleanupInterval);\n\t\t\tthis.cleanupInterval = null;\n\t\t}\n\t\tthis.cache.clear();\n\t}\n}\n\n// Export singleton instance\nexport const queryCache = new QueryCache();\n","/**\n * UTF-16 surrogate safety utilities.\n *\n * Characters outside the Basic Multilingual Plane (emoji, some CJK/symbols) are\n * stored in JS strings as a *pair* of UTF-16 code units: a high surrogate\n * (U+D800–U+DBFF) immediately followed by a low surrogate (U+DC00–U+DFFF).\n *\n * A \"lone\" surrogate — a high with no following low, or a low with no preceding\n * high — is ill-formed UTF-16. `JSON.stringify` happily serializes it into a bare\n * `\\udXXX` escape, but strict JSON parsers (including the Python parser on\n * Anthropic's API servers) reject it with:\n *\n * The request body is not valid JSON: no low surrogate in string ...\n *\n * which fails the entire LLM request. Lone surrogates reach us two ways:\n * 1. Slicing a string mid-pair (`.substring`/`.slice` cut between high & low).\n * 2. Corrupt source data (Excel/CSV/DB cells with bad encoding).\n *\n * `stripLoneSurrogates` is the safety net (call before anything is JSON-encoded\n * for an LLM); `safeTruncate` is the prevention (cut without splitting a pair).\n */\n\n// Matches a high surrogate NOT followed by a low surrogate, OR a low surrogate\n// NOT preceded by a high surrogate. Valid pairs fail both alternatives and are\n// left untouched.\nconst LONE_SURROGATE_RE = /[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?<![\\uD800-\\uDBFF])[\\uDC00-\\uDFFF]/g;\n\n/**\n * Replace every unpaired UTF-16 surrogate with the Unicode replacement\n * character (U+FFFD), making the string safe to `JSON.stringify` and send to an\n * LLM API. Valid surrogate pairs (emoji, etc.) are preserved.\n *\n * Non-strings are returned unchanged so this is safe to call defensively.\n */\nexport function stripLoneSurrogates<T>(value: T): T {\n\tif (typeof value !== 'string') return value;\n\t// Fast path: most strings have no surrogates at all.\n\tif (!/[\\uD800-\\uDFFF]/.test(value)) return value;\n\treturn value.replace(LONE_SURROGATE_RE, '�') as unknown as T;\n}\n\n/**\n * Truncate a string to at most `maxUnits` UTF-16 code units WITHOUT splitting a\n * surrogate pair. If the cut would land between a high and low surrogate, the\n * dangling high surrogate is dropped (so the result is one unit shorter).\n *\n * Returns only the truncated slice — no ellipsis/suffix — so callers keep full\n * control over any \"… (N more)\" indicator they append.\n */\nexport function safeTruncate(text: string, maxUnits: number): string {\n\tif (typeof text !== 'string' || text.length <= maxUnits || maxUnits < 0) return text;\n\tlet end = maxUnits;\n\tconst lastCode = text.charCodeAt(end - 1);\n\t// Dangling high surrogate at the cut boundary → drop it to avoid a lone half.\n\tif (lastCode >= 0xd800 && lastCode <= 0xdbff) end -= 1;\n\treturn text.slice(0, end);\n}\n","/**\n * LLM Result Truncator\n *\n * Utilities for truncating query and external tool results before passing to LLM.\n * Ensures data is compact while preserving essential information for analysis.\n */\n\nimport { safeTruncate, stripLoneSurrogates } from '../utils/surrogate';\n\n// Default configuration\nconst DEFAULT_MAX_ROWS = 10;\nconst DEFAULT_MAX_CHARS_PER_FIELD = 500;\n\n/**\n * Field type enumeration for schema inference\n */\nexport type FieldType = 'string' | 'number' | 'boolean' | 'date' | 'array' | 'object' | 'null' | 'unknown';\n\n/**\n * Schema field information\n */\nexport interface SchemaField {\n\tname: string;\n\ttype: FieldType;\n\ttruncated?: boolean;\n}\n\n/**\n * Options for truncating data\n */\nexport interface TruncateOptions {\n\tmaxRows?: number;\n\tmaxCharsPerField?: number;\n}\n\n/**\n * Formatted result structure for LLM consumption\n */\nexport interface FormattedResultForLLM {\n\tsummary: {\n\t\ttotalRecords: number;\n\t\trecordsShown: number;\n\t\tschema: SchemaField[];\n\t};\n\tmetadata?: Record<string, unknown>; // Top-level non-array properties (e.g., totalItems, totalDeadstockItems)\n\tdata: Record<string, unknown>[];\n\ttruncationNote: string | null;\n}\n\n/**\n * Options for formatting external tool results\n */\nexport interface FormatToolResultOptions extends TruncateOptions {\n\ttoolName?: string;\n\ttoolLimit?: number; // Limit specified by the external tool itself\n}\n\n/**\n * Infers the type of a field value\n * @param value - The value to infer type from\n * @returns The inferred field type\n */\nexport function inferFieldType(value: unknown): FieldType {\n\tif (value === null || value === undefined) {\n\t\treturn 'null';\n\t}\n\n\tif (typeof value === 'string') {\n\t\t// Check if it looks like a date\n\t\tif (isDateString(value)) {\n\t\t\treturn 'date';\n\t\t}\n\t\treturn 'string';\n\t}\n\n\tif (typeof value === 'number') {\n\t\treturn 'number';\n\t}\n\n\tif (typeof value === 'boolean') {\n\t\treturn 'boolean';\n\t}\n\n\tif (Array.isArray(value)) {\n\t\treturn 'array';\n\t}\n\n\tif (typeof value === 'object') {\n\t\treturn 'object';\n\t}\n\n\treturn 'unknown';\n}\n\n/**\n * Checks if a string value looks like a date\n * @param value - The string value to check\n * @returns True if the string appears to be a date\n */\nexport function isDateString(value: string): boolean {\n\t// Common date patterns\n\tconst datePatterns = [\n\t\t/^\\d{4}-\\d{2}-\\d{2}$/, // YYYY-MM-DD\n\t\t/^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}/, // ISO 8601\n\t\t/^\\d{2}\\/\\d{2}\\/\\d{4}$/, // MM/DD/YYYY\n\t\t/^\\d{4}\\/\\d{2}\\/\\d{2}$/, // YYYY/MM/DD\n\t];\n\n\treturn datePatterns.some(pattern => pattern.test(value));\n}\n\n/**\n * Truncates a text field to specified length with indicator\n * @param value - The string value to truncate\n * @param maxLength - Maximum allowed length\n * @returns Truncated string with indicator if truncated\n */\nexport function truncateTextField(value: string, maxLength: number): { text: string; wasTruncated: boolean } {\n\tif (value.length <= maxLength) {\n\t\t// Sanitize even when not truncating: the cell may already carry lone\n\t\t// surrogates from corrupt source data (Excel/CSV/DB) that would break\n\t\t// the JSON request body.\n\t\treturn { text: stripLoneSurrogates(value), wasTruncated: false };\n\t}\n\n\t// Surrogate-aware cut: never split a UTF-16 pair (which would emit a lone\n\t// surrogate that breaks JSON.stringify → invalid LLM request body).\n\tconst truncated = safeTruncate(value, maxLength);\n\tconst remaining = value.length - truncated.length;\n\n\treturn {\n\t\ttext: `${stripLoneSurrogates(truncated)}... (${remaining} more chars)`,\n\t\twasTruncated: true\n\t};\n}\n\n/**\n * Truncates a single field value based on its type\n * @param value - The field value to truncate\n * @param maxCharsPerField - Maximum characters for text fields\n * @returns Object with truncated value and truncation status\n */\nexport function truncateFieldValue(\n\tvalue: unknown,\n\tmaxCharsPerField: number\n): { value: unknown; wasTruncated: boolean } {\n\t// Keep null/undefined as-is\n\tif (value === null || value === undefined) {\n\t\treturn { value, wasTruncated: false };\n\t}\n\n\t// Keep numbers and booleans intact - needed for analysis\n\tif (typeof value === 'number' || typeof value === 'boolean') {\n\t\treturn { value, wasTruncated: false };\n\t}\n\n\t// Truncate strings\n\tif (typeof value === 'string') {\n\t\tconst result = truncateTextField(value, maxCharsPerField);\n\t\treturn { value: result.text, wasTruncated: result.wasTruncated };\n\t}\n\n\t// For arrays, show count and first few items\n\tif (Array.isArray(value)) {\n\t\tif (value.length === 0) {\n\t\t\treturn { value: [], wasTruncated: false };\n\t\t}\n\n\t\tconst preview = value.slice(0, 3);\n\t\tconst hasMore = value.length > 3;\n\n\t\treturn {\n\t\t\tvalue: hasMore ? `[${preview.join(', ')}... (${value.length} items)]` : value,\n\t\t\twasTruncated: hasMore\n\t\t};\n\t}\n\n\t// For objects, stringify and truncate\n\tif (typeof value === 'object') {\n\t\tconst jsonStr = JSON.stringify(value);\n\t\tconst result = truncateTextField(jsonStr, maxCharsPerField);\n\t\treturn { value: result.text, wasTruncated: result.wasTruncated };\n\t}\n\n\t// Default: convert to string and truncate\n\tconst strValue = String(value);\n\tconst result = truncateTextField(strValue, maxCharsPerField);\n\treturn { value: result.text, wasTruncated: result.wasTruncated };\n}\n\n/**\n * Truncates all fields in a single row\n * @param row - The data row to truncate\n * @param maxCharsPerField - Maximum characters for text fields\n * @returns Object with truncated row and set of truncated field names\n */\nexport function truncateRow(\n\trow: Record<string, unknown>,\n\tmaxCharsPerField: number\n): { row: Record<string, unknown>; truncatedFields: Set<string> } {\n\tconst truncatedRow: Record<string, unknown> = {};\n\tconst truncatedFields = new Set<string>();\n\n\tfor (const [key, value] of Object.entries(row)) {\n\t\tconst result = truncateFieldValue(value, maxCharsPerField);\n\t\ttruncatedRow[key] = result.value;\n\n\t\tif (result.wasTruncated) {\n\t\t\ttruncatedFields.add(key);\n\t\t}\n\t}\n\n\treturn { row: truncatedRow, truncatedFields };\n}\n\n/**\n * Extracts metadata (non-array top-level properties) from an object result\n * This preserves important aggregate values like totalItems, totalCount, etc.\n * @param obj - The object to extract metadata from\n * @param dataKey - The key containing the data array (to exclude from metadata)\n * @param maxCharsPerField - Maximum characters for text fields\n * @returns Object with metadata and truncated fields\n */\nexport function extractMetadataFromObject(\n\tobj: Record<string, unknown>,\n\tdataKey: string,\n\tmaxCharsPerField: number\n): { metadata: Record<string, unknown>; truncatedFields: Set<string> } {\n\tconst metadata: Record<string, unknown> = {};\n\tconst truncatedFields = new Set<string>();\n\n\tfor (const [key, value] of Object.entries(obj)) {\n\t\t// Skip the data array key\n\t\tif (key === dataKey) {\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Skip arrays (we only want scalar metadata)\n\t\tif (Array.isArray(value)) {\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Truncate and add to metadata\n\t\tconst result = truncateFieldValue(value, maxCharsPerField);\n\t\tmetadata[key] = result.value;\n\n\t\tif (result.wasTruncated) {\n\t\t\ttruncatedFields.add(key);\n\t\t}\n\t}\n\n\treturn { metadata, truncatedFields };\n}\n\n/**\n * Extracts schema information from data array\n * @param data - Array of data records\n * @param truncatedFields - Set of field names that were truncated\n * @returns Array of schema field information\n */\nexport function extractSchema(\n\tdata: Record<string, unknown>[],\n\ttruncatedFields: Set<string> = new Set()\n): SchemaField[] {\n\tif (!data || data.length === 0) {\n\t\treturn [];\n\t}\n\n\t// Use first row to extract field names and types\n\tconst firstRow = data[0];\n\tconst schema: SchemaField[] = [];\n\n\tfor (const [name, value] of Object.entries(firstRow)) {\n\t\tschema.push({\n\t\t\tname,\n\t\t\ttype: inferFieldType(value),\n\t\t\ttruncated: truncatedFields.has(name) ? true : undefined\n\t\t});\n\t}\n\n\treturn schema;\n}\n\n/**\n * Truncates an array of data records\n * @param data - Array of data records to truncate\n * @param options - Truncation options\n * @returns Object with truncated data and metadata\n */\nexport function truncateDataArray(\n\tdata: Record<string, unknown>[],\n\toptions: TruncateOptions = {}\n): {\n\tdata: Record<string, unknown>[];\n\ttotalRecords: number;\n\trecordsShown: number;\n\ttruncatedFields: Set<string>;\n} {\n\tconst maxRows = options.maxRows ?? DEFAULT_MAX_ROWS;\n\tconst maxCharsPerField = options.maxCharsPerField ?? DEFAULT_MAX_CHARS_PER_FIELD;\n\n\tif (!data || !Array.isArray(data)) {\n\t\treturn {\n\t\t\tdata: [],\n\t\t\ttotalRecords: 0,\n\t\t\trecordsShown: 0,\n\t\t\ttruncatedFields: new Set()\n\t\t};\n\t}\n\n\tconst totalRecords = data.length;\n\tconst rowsToProcess = data.slice(0, maxRows);\n\tconst truncatedData: Record<string, unknown>[] = [];\n\tconst allTruncatedFields = new Set<string>();\n\n\tfor (const row of rowsToProcess) {\n\t\tconst { row: truncatedRow, truncatedFields } = truncateRow(row, maxCharsPerField);\n\t\ttruncatedData.push(truncatedRow);\n\n\t\t// Merge truncated fields\n\t\tfor (const field of truncatedFields) {\n\t\t\tallTruncatedFields.add(field);\n\t\t}\n\t}\n\n\treturn {\n\t\tdata: truncatedData,\n\t\ttotalRecords,\n\t\trecordsShown: truncatedData.length,\n\t\ttruncatedFields: allTruncatedFields\n\t};\n}\n\n/**\n * Builds a truncation note describing what was truncated\n * @param totalRecords - Total number of records in original data\n * @param recordsShown - Number of records included\n * @param truncatedFields - Set of field names that were truncated\n * @param maxCharsPerField - Max chars per field setting\n * @param sourceName - Optional source name (tool name or 'query')\n * @returns Human-readable truncation note or null if nothing truncated\n */\nexport function buildTruncationNote(\n\ttotalRecords: number,\n\trecordsShown: number,\n\ttruncatedFields: Set<string>,\n\tmaxCharsPerField: number,\n\tsourceName?: string\n): string | null {\n\tconst parts: string[] = [];\n\n\t// Record truncation\n\tif (totalRecords > recordsShown) {\n\t\tconst source = sourceName ? ` from ${sourceName}` : '';\n\t\tparts.push(`Showing ${recordsShown} of ${totalRecords} total records${source}`);\n\t}\n\n\t// Field truncation\n\tif (truncatedFields.size > 0) {\n\t\tconst fieldList = Array.from(truncatedFields).join(', ');\n\t\tparts.push(`Fields truncated to ${maxCharsPerField} chars: ${fieldList}`);\n\t}\n\n\treturn parts.length > 0 ? parts.join('. ') + '.' : null;\n}\n\n/**\n * Formats query result for LLM consumption\n * @param data - Raw query result data\n * @param options - Truncation options\n * @returns Formatted result structure for LLM\n */\nexport function formatQueryResultForLLM(\n\tdata: unknown,\n\toptions: TruncateOptions = {}\n): FormattedResultForLLM {\n\tconst maxCharsPerField = options.maxCharsPerField ?? DEFAULT_MAX_CHARS_PER_FIELD;\n\n\t// Handle non-array results\n\tif (!Array.isArray(data)) {\n\t\t// Single value result (e.g., COUNT(*))\n\t\tif (data !== null && data !== undefined) {\n\t\t\treturn {\n\t\t\t\tsummary: {\n\t\t\t\t\ttotalRecords: 1,\n\t\t\t\t\trecordsShown: 1,\n\t\t\t\t\tschema: [{ name: 'result', type: inferFieldType(data) }]\n\t\t\t\t},\n\t\t\t\tdata: [{ result: data }],\n\t\t\t\ttruncationNote: null\n\t\t\t};\n\t\t}\n\n\t\treturn {\n\t\t\tsummary: {\n\t\t\t\ttotalRecords: 0,\n\t\t\t\trecordsShown: 0,\n\t\t\t\tschema: []\n\t\t\t},\n\t\t\tdata: [],\n\t\t\ttruncationNote: null\n\t\t};\n\t}\n\n\t// Process array data\n\tconst {\n\t\tdata: truncatedData,\n\t\ttotalRecords,\n\t\trecordsShown,\n\t\ttruncatedFields\n\t} = truncateDataArray(data as Record<string, unknown>[], options);\n\n\tconst schema = extractSchema(truncatedData, truncatedFields);\n\tconst truncationNote = buildTruncationNote(\n\t\ttotalRecords,\n\t\trecordsShown,\n\t\ttruncatedFields,\n\t\tmaxCharsPerField,\n\t\t'query'\n\t);\n\n\treturn {\n\t\tsummary: {\n\t\t\ttotalRecords,\n\t\t\trecordsShown,\n\t\t\tschema\n\t\t},\n\t\tdata: truncatedData,\n\t\ttruncationNote\n\t};\n}\n\n/**\n * Formats external tool result for LLM consumption\n * @param result - Raw tool result\n * @param options - Formatting options including tool-specific limits\n * @returns Formatted result structure for LLM\n */\nexport function formatToolResultForLLM(\n\tresult: unknown,\n\toptions: FormatToolResultOptions = {}\n): FormattedResultForLLM & { toolName?: string } {\n\tconst { toolName, toolLimit } = options;\n\n\t// Use tool's limit if specified, otherwise use default\n\tconst effectiveMaxRows = toolLimit ?? options.maxRows ?? DEFAULT_MAX_ROWS;\n\tconst maxCharsPerField = options.maxCharsPerField ?? DEFAULT_MAX_CHARS_PER_FIELD;\n\n\t// Handle null/undefined result\n\tif (result === null || result === undefined) {\n\t\treturn {\n\t\t\ttoolName,\n\t\t\tsummary: {\n\t\t\t\ttotalRecords: 0,\n\t\t\t\trecordsShown: 0,\n\t\t\t\tschema: []\n\t\t\t},\n\t\t\tdata: [],\n\t\t\ttruncationNote: null\n\t\t};\n\t}\n\n\t// Handle string result\n\tif (typeof result === 'string') {\n\t\tconst { text, wasTruncated } = truncateTextField(result, maxCharsPerField);\n\t\treturn {\n\t\t\ttoolName,\n\t\t\tsummary: {\n\t\t\t\ttotalRecords: 1,\n\t\t\t\trecordsShown: 1,\n\t\t\t\tschema: [{ name: 'result', type: 'string', truncated: wasTruncated || undefined }]\n\t\t\t},\n\t\t\tdata: [{ result: text }],\n\t\t\ttruncationNote: wasTruncated ? `Result truncated to ${maxCharsPerField} chars.` : null\n\t\t};\n\t}\n\n\t// Handle array result (most common for external tools)\n\tif (Array.isArray(result)) {\n\t\tconst {\n\t\t\tdata: truncatedData,\n\t\t\ttotalRecords,\n\t\t\trecordsShown,\n\t\t\ttruncatedFields\n\t\t} = truncateDataArray(result as Record<string, unknown>[], {\n\t\t\tmaxRows: effectiveMaxRows,\n\t\t\tmaxCharsPerField\n\t\t});\n\n\t\tconst schema = extractSchema(truncatedData, truncatedFields);\n\t\tconst truncationNote = buildTruncationNote(\n\t\t\ttotalRecords,\n\t\t\trecordsShown,\n\t\t\ttruncatedFields,\n\t\t\tmaxCharsPerField,\n\t\t\ttoolName\n\t\t);\n\n\t\treturn {\n\t\t\ttoolName,\n\t\t\tsummary: {\n\t\t\t\ttotalRecords,\n\t\t\t\trecordsShown,\n\t\t\t\tschema\n\t\t\t},\n\t\t\tdata: truncatedData,\n\t\t\ttruncationNote\n\t\t};\n\t}\n\n\t// Handle object result - check for common wrapper patterns\n\tif (typeof result === 'object') {\n\t\tconst objResult = result as Record<string, unknown>;\n\n\t\t// Check for common data wrapper patterns: { data: [...] }, { results: [...] }, { items: [...] }\n\t\tconst dataWrapperKeys = ['data', 'results', 'items', 'records', 'rows', 'list'];\n\t\tfor (const key of dataWrapperKeys) {\n\t\t\tif (Array.isArray(objResult[key])) {\n\t\t\t\tconst innerData = objResult[key] as Record<string, unknown>[];\n\t\t\t\tconst {\n\t\t\t\t\tdata: truncatedData,\n\t\t\t\t\ttotalRecords,\n\t\t\t\t\trecordsShown,\n\t\t\t\t\ttruncatedFields: dataTruncatedFields\n\t\t\t\t} = truncateDataArray(innerData, {\n\t\t\t\t\tmaxRows: effectiveMaxRows,\n\t\t\t\t\tmaxCharsPerField\n\t\t\t\t});\n\n\t\t\t\t// Extract metadata (non-array top-level properties like totalItems, totalDeadstockItems)\n\t\t\t\tconst {\n\t\t\t\t\tmetadata,\n\t\t\t\t\ttruncatedFields: metadataTruncatedFields\n\t\t\t\t} = extractMetadataFromObject(objResult, key, maxCharsPerField);\n\n\t\t\t\t// Merge truncated fields from both data and metadata\n\t\t\t\tconst allTruncatedFields = new Set([...dataTruncatedFields, ...metadataTruncatedFields]);\n\n\t\t\t\tconst schema = extractSchema(truncatedData, dataTruncatedFields);\n\t\t\t\tconst truncationNote = buildTruncationNote(\n\t\t\t\t\ttotalRecords,\n\t\t\t\t\trecordsShown,\n\t\t\t\t\tallTruncatedFields,\n\t\t\t\t\tmaxCharsPerField,\n\t\t\t\t\ttoolName\n\t\t\t\t);\n\n\t\t\t\t// Only include metadata if there are non-array properties\n\t\t\t\tconst hasMetadata = Object.keys(metadata).length > 0;\n\n\t\t\t\treturn {\n\t\t\t\t\ttoolName,\n\t\t\t\t\tsummary: {\n\t\t\t\t\t\ttotalRecords,\n\t\t\t\t\t\trecordsShown,\n\t\t\t\t\t\tschema\n\t\t\t\t\t},\n\t\t\t\t\t...(hasMetadata && { metadata }),\n\t\t\t\t\tdata: truncatedData,\n\t\t\t\t\ttruncationNote\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\n\t\t// Single object result - wrap in array and truncate fields\n\t\tconst { row: truncatedRow, truncatedFields } = truncateRow(objResult, maxCharsPerField);\n\t\tconst schema = extractSchema([truncatedRow], truncatedFields);\n\t\tconst truncationNote = truncatedFields.size > 0\n\t\t\t? `Fields truncated to ${maxCharsPerField} chars: ${Array.from(truncatedFields).join(', ')}.`\n\t\t\t: null;\n\n\t\treturn {\n\t\t\ttoolName,\n\t\t\tsummary: {\n\t\t\t\ttotalRecords: 1,\n\t\t\t\trecordsShown: 1,\n\t\t\t\tschema\n\t\t\t},\n\t\t\tdata: [truncatedRow],\n\t\t\ttruncationNote\n\t\t};\n\t}\n\n\t// Fallback for other types\n\treturn {\n\t\ttoolName,\n\t\tsummary: {\n\t\t\ttotalRecords: 1,\n\t\t\trecordsShown: 1,\n\t\t\tschema: [{ name: 'result', type: inferFieldType(result) }]\n\t\t},\n\t\tdata: [{ result }],\n\t\ttruncationNote: null\n\t};\n}\n\n/**\n * Converts formatted result to a string for LLM consumption\n * @param formattedResult - The formatted result object\n * @returns JSON string representation\n */\nexport function formatResultAsString(formattedResult: FormattedResultForLLM): string {\n\treturn JSON.stringify(formattedResult, null, 2);\n}\n","/**\n * Deterministic JSON.stringify with sorted keys (recursive). Two objects with\n * the same content but different key insertion order produce the same string\n * — important for cache keys so we don't miss hits when the LLM happens to\n * emit params in a different order than the frontend.\n */\nexport function stableStringify(value: any): string {\n\tif (value === null || typeof value !== 'object') {\n\t\treturn JSON.stringify(value);\n\t}\n\tif (Array.isArray(value)) {\n\t\treturn '[' + value.map(stableStringify).join(',') + ']';\n\t}\n\tconst keys = Object.keys(value).sort();\n\treturn '{' + keys.map(k => JSON.stringify(k) + ':' + stableStringify(value[k])).join(',') + '}';\n}\n\n/**\n * Build the cache key for a direct external tool execution. Mirrors the key\n * built in handlers/data-request.ts so that pre-populating from the dash-comp\n * picker hits the same slot the frontend looks up at render time.\n */\nexport function buildDirectToolCacheKey(toolId: string, parameters: Record<string, any> | undefined): string {\n\treturn `et-direct:${toolId}:${stableStringify(parameters || {})}`;\n}\n","import { DataRequestMessageSchema, type CollectionRegistry, type Message } from '../types';\nimport { logger } from '../utils/logger';\nimport { ThreadManager } from '../threads';\nimport { queryCache } from '../utils/query-cache';\nimport { formatToolResultForLLM } from '../userResponse/llm-result-truncator';\nimport { stableStringify } from '../utils/cache-key';\n\n// Loose tool shape used solely for cache-policy lookup. Tools registered via\n// sdk.setTools() may carry richer fields (id, cache, etc.) beyond the LLM\n// `Tool` type — we read `id` and `cache` here without depending on a strict\n// type to avoid coupling caching to the LLM tool definition.\ntype ToolWithCachePolicy = { id?: string; cache?: false | { ttlMs?: number }; [key: string]: any };\n\n/**\n * Get cache key for a query - handles both string and object queries\n * Must match the logic in base-llm.ts getQueryCacheKey\n */\nfunction getQueryCacheKey(query: any): string {\n if (typeof query === 'string') {\n return query;\n } else if (query?.sql) {\n const values = query.values || query.params;\n if (values && Object.keys(values).length > 0) {\n return JSON.stringify({ sql: query.sql, values });\n }\n return query.sql;\n }\n return '';\n}\n\n/**\n * Get cache key for any cacheable operation.\n * Only database queries are cached — non-database tools (Excel, API, forms) are NOT cached.\n *\n * @param tools — tool registry (passed in from sdk.tools); used to honor the\n * `cache: false` flag declared on individual tool definitions (server-side\n * `Cache-Control: no-store` semantics).\n */\nfunction getCacheKey(collection: string, op: string, params: any, tools?: ToolWithCachePolicy[]): string {\n // database.execute — existing behavior\n if (collection === 'database' && op === 'execute' && params?.sql) {\n return getQueryCacheKey(params.sql);\n }\n\n // external-tools.execute — sql-based tool (sql comes through as a param)\n if (collection === 'external-tools' && op === 'execute' && params?.sql) {\n const toolId = params.toolId || '';\n const sqlKey = getQueryCacheKey(params.sql);\n // Include params/filterParams in key for parameterized queries\n const paramsKey = params.params ? JSON.stringify(params.params) : '';\n return sqlKey ? `et:${toolId}:${sqlKey}:${paramsKey}` : '';\n }\n\n // external-tools.execute — direct tool (sql is built internally, not passed as param)\n // Cache key must include ALL params that affect the result, not just `data`.\n // Direct tools receive filter params (branch, targetBranch, itemCodes, etc.)\n // as siblings of `data` via tool-executor's spread of externalTool.parameters.\n // Earlier versions keyed only on `data`, so every call to a tool collided in\n // the cache regardless of filters — workflows and dashboard components were\n // silently returning stale results from the first call.\n if (collection === 'external-tools' && op === 'execute' && params?.toolId && !params?.sql) {\n // Server-declared opt-out: tool definitions can set `cache: false` to\n // mark themselves non-cacheable (live data like orders lists, write\n // operations whose responses must not be memoized). Mirrors HTTP\n // `Cache-Control: no-store`; the data owner declares the policy, every\n // caller automatically gets correct behavior. Empty key = skip cache\n // lookup AND skip storing the result.\n const tool = tools?.find((t) => t.id === params.toolId);\n if (tool?.cache === false) return '';\n\n // Strip toolId/toolName (toolId is already in the key prefix; toolName is\n // a label, not part of the query identity) and stably stringify the rest.\n const { toolId, toolName, ...rest } = params;\n const paramsKey = stableStringify(rest);\n return `et-direct:${toolId}:${paramsKey}`;\n }\n\n // external-tools.executeByQueryId — always a database query\n if (collection === 'external-tools' && op === 'executeByQueryId' && params?.queryId) {\n const toolId = params.toolId || '';\n const filterKey = params.filterParams ? JSON.stringify(params.filterParams) : '';\n const paramsKey = params.params ? JSON.stringify(params.params) : '';\n return `etq:${toolId}:${params.queryId}:${paramsKey}:${filterKey}`;\n }\n\n return '';\n}\n\n/**\n * Handle incoming data_req messages and execute collection handlers\n *\n * @param tools — tool registry from `sdk.tools`. Optional; passed when\n * available so per-tool cache policies (`cache: false`) are honored.\n */\nexport async function handleDataRequest(\n data: any,\n collections: CollectionRegistry,\n sendMessage: (message: Message) => void,\n tools?: ToolWithCachePolicy[]\n): Promise<void> {\n // Extract request info before try block so catch can send error responses\n let requestId: string | undefined;\n let collection: string | undefined;\n let op: string | undefined;\n\n try {\n const dataRequest = DataRequestMessageSchema.parse(data);\n const { id, payload } = dataRequest;\n requestId = id;\n const { collection: col, op: operation, params, SA_RUNTIME } = payload;\n collection = col;\n op = operation;\n\n // Check if collection and operation exist\n if (!collections[collection]) {\n sendDataResponse(id, collection, op, null, {\n error: `Collection '${collection}' not found`,\n }, sendMessage);\n return;\n }\n\n if (!collections[collection][op]) {\n sendDataResponse(id, collection, op, null, {\n error: `Operation '${op}' not found for collection '${collection}'`,\n }, sendMessage);\n return;\n }\n\n // Check cache for database and external-tools database queries\n const startTime = performance.now();\n let result: any;\n let fromCache = false;\n const cacheKey = getCacheKey(collection, op, params, tools);\n\n if (cacheKey) {\n const cachedResult = queryCache.get(cacheKey);\n if (cachedResult !== null) {\n result = cachedResult;\n fromCache = true;\n logger.info(`[QueryCache] Returning cached result for ${collection}.${op}`);\n }\n }\n\n // Execute the handler if not cached\n if (!fromCache) {\n const handler = collections[collection][op];\n\n // For database execute, ensure params.sql is a string (not object)\n // The database handler expects either plain SQL or JSON string\n let handlerParams = params || {};\n if (collection === 'database' && op === 'execute' && params?.sql && typeof params.sql !== 'string') {\n // Convert object query to JSON string format\n const queryKey = getQueryCacheKey(params.sql);\n handlerParams = { ...params, sql: queryKey };\n logger.debug(`[data-request] Converted object query to JSON string for database handler`);\n }\n\n result = await handler(handlerParams);\n\n // Store result in cache for database query operations\n if (cacheKey && result) {\n queryCache.set(cacheKey, result);\n }\n }\n\n const executionMs = Math.round(performance.now() - startTime);\n\n logger.info(`Executed ${collection}.${op} in ${executionMs}ms${fromCache ? ' (from cache)' : ''}`);\n\n // Update UIBlock with component data if SA_RUNTIME has uiBlockId\n if (SA_RUNTIME && typeof SA_RUNTIME === 'object' && 'uiBlockId' in SA_RUNTIME) {\n const uiBlockId = (SA_RUNTIME as any).uiBlockId;\n const threadId = (SA_RUNTIME as any).threadId;\n\n const threadManager = ThreadManager.getInstance();\n let uiBlock = null;\n let thread = null;\n\n // If threadId is provided, get the specific thread\n if (threadId) {\n thread = threadManager.getThread(threadId);\n if (thread) {\n uiBlock = thread.getUIBlock(uiBlockId);\n }\n } else {\n // Otherwise search across all threads\n const result = threadManager.findUIBlockById(uiBlockId);\n if (result) {\n thread = result.thread;\n uiBlock = result.uiBlock;\n }\n }\n\n // Update UIBlock's componentData with a summary (not full result) to save memory\n // The full result is sent to the frontend via WebSocket, no need to store it\n if (uiBlock) {\n // Use formatter to extract proper metadata including totalItems, totalDeadstockItems, etc.\n const formattedResult = formatToolResultForLLM(result, {\n maxRows: 3, // Only need a few sample rows for summary\n maxCharsPerField: 100 // Short truncation for summary\n });\n\n const dataSummary = {\n _dataReceived: true,\n _timestamp: new Date().toISOString(),\n _collection: collection,\n _operation: op,\n _totalRecords: formattedResult.summary.totalRecords,\n _recordsShown: formattedResult.summary.recordsShown,\n _metadata: formattedResult.metadata, // Preserve totalItems, totalDeadstockItems, etc.\n _schema: formattedResult.summary.schema\n };\n uiBlock.setComponentData(dataSummary);\n logger.info(`Updated UIBlock ${uiBlockId} with data summary from ${collection}.${op} (${formattedResult.summary.totalRecords} total records)`);\n } else {\n logger.warn(`UIBlock ${uiBlockId} not found in threads`);\n }\n }\n\n // Send response\n sendDataResponse(id, collection, op, result, { executionMs }, sendMessage);\n } catch (error) {\n const errorMsg = error instanceof Error ? error.message : String(error);\n logger.error('Failed to handle data request:', error);\n\n // Send error response back to the frontend (instead of silently dropping it)\n if (requestId) {\n sendDataResponse(requestId, collection || 'unknown', op || 'unknown', null, {\n error: errorMsg,\n }, sendMessage);\n }\n }\n}\n\n/**\n * Send a data_res response message\n */\nfunction sendDataResponse(\n id: string,\n collection: string,\n op: string,\n data: any,\n meta: { executionMs?: number; error?: string },\n sendMessage: (message: Message) => void\n): void {\n const response: Message = {\n id,\n from: { type: 'data-agent' },\n type: 'DATA_RES',\n payload: {\n collection,\n op,\n data,\n ...meta,\n },\n };\n\n sendMessage(response);\n}\n","import * as fs from 'fs';\nimport * as path from 'path';\nimport { logger } from './utils/logger';\n\n/**\n * Get the bundle directory from config or environment variable\n */\nexport function getBundleDir(configDir?: string): string {\n const bundleDir = configDir || process.env.SA_BUNDLE_DIR;\n\n if (!bundleDir) {\n throw new Error(\n 'Bundle directory not configured. Please provide bundleDir in config or set SA_BUNDLE_DIR environment variable.'\n );\n }\n\n return bundleDir;\n}\n\n/**\n * Load the JavaScript bundle from the configured directory\n */\nexport function getJS(bundleDir: string): string {\n try {\n // Check if directory exists\n if (!fs.existsSync(bundleDir)) {\n throw new Error(`Bundle directory does not exist: ${bundleDir}`);\n }\n\n // Check if it's actually a directory\n const stats = fs.statSync(bundleDir);\n if (!stats.isDirectory()) {\n throw new Error(`Bundle path is not a directory: ${bundleDir}`);\n }\n\n // Read directory contents\n let files: string[];\n try {\n files = fs.readdirSync(bundleDir);\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n throw new Error(`Failed to read bundle directory: ${errorMessage}`);\n }\n\n // Find the bundle file\n const indexFile = files.find((file) => file.startsWith('index-') && file.endsWith('.js'));\n\n if (!indexFile) {\n logger.warn(`Available files in ${bundleDir}:`, files);\n throw new Error(\n `Could not find index-*.js file in ${bundleDir}. ` +\n `Expected a file matching pattern: index-*.js`\n );\n }\n\n // Read the bundle file\n const filePath = path.join(bundleDir, indexFile);\n logger.info(`Loading bundle from ${filePath}`);\n\n try {\n return fs.readFileSync(filePath, 'utf8');\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n throw new Error(`Failed to read bundle file: ${errorMessage}`);\n }\n } catch (error) {\n if (error instanceof Error) {\n logger.error('Failed to load bundle:', error.message);\n } else {\n logger.error('Failed to load bundle:', error);\n }\n throw error;\n }\n}\n","import { getJS, getBundleDir } from '../bundle';\nimport { logger } from '../utils/logger';\nimport type { Message } from '../types';\n\nconst CHUNK_SIZE = 900 * 1024; // 900 KB chunks (leaving room for metadata, max 1MB per message)\n\n/**\n * Handle incoming bundle_req messages and send chunked bundle response\n */\nexport async function handleBundleRequest(\n data: any,\n bundleDir: string | undefined,\n sendMessage: (message: Message) => void\n): Promise<void> {\n try {\n const id = data.id || 'unknown';\n const fromId = data.from?.id;\n\n // Get bundle directory and load bundle\n const dir = getBundleDir(bundleDir);\n const js = getJS(dir);\n const bundleSize = Buffer.byteLength(js, 'utf8');\n\n logger.info(`Bundle size: ${(bundleSize / 1024).toFixed(2)} KB`);\n\n // Split bundle into chunks\n const totalChunks = Math.ceil(bundleSize / CHUNK_SIZE);\n logger.info(`Splitting bundle into ${totalChunks} chunks`);\n\n for (let i = 0; i < totalChunks; i++) {\n const start = i * CHUNK_SIZE;\n const end = Math.min(start + CHUNK_SIZE, js.length);\n const chunk = js.substring(start, end);\n const isComplete = i === totalChunks - 1;\n const progress = ((i + 1) / totalChunks) * 100;\n\n const chunkMessage: Message = {\n id: `${id}-chunk-${i}`,\n type: 'BUNDLE_CHUNK',\n from: { type: 'data-agent' },\n to: fromId ? { id: fromId } : undefined,\n payload: {\n chunk: chunk,\n chunkIndex: i,\n totalChunks: totalChunks,\n isComplete: isComplete,\n progress: parseFloat(progress.toFixed(2)),\n },\n };\n\n sendMessage(chunkMessage);\n logger.debug(`Sent chunk ${i + 1}/${totalChunks} (${progress.toFixed(2)}%)`);\n }\n\n logger.info('Bundle sending complete');\n } catch (error) {\n logger.error('Failed to handle bundle request:', error);\n\n // Send error response\n const errorMessage: Message = {\n id: data.id || 'unknown',\n type: 'BUNDLE_RES',\n from: { type: 'data-agent' },\n to: data.from?.id ? { id: data.from.id } : undefined,\n payload: {\n error: error instanceof Error ? error.message : 'Unknown error',\n },\n };\n\n sendMessage(errorMessage);\n }\n}\n","import crypto from 'crypto';\n\n/**\n * Decode base64 encoded string and parse JSON\n * @param base64Data - Base64 encoded string\n * @returns Parsed JSON object\n */\nexport function decodeBase64ToJson(base64Data: string): any {\n try {\n const decodedString = Buffer.from(base64Data, 'base64').toString('utf-8');\n return JSON.parse(decodedString);\n } catch (error) {\n throw new Error(`Failed to decode base64 data: ${error instanceof Error ? error.message : 'Unknown error'}`);\n }\n}\n\n/**\n * Hash password using SHA1\n * @param password - Plain text password\n * @returns SHA1 hashed password\n */\nexport function hashPassword(password: string): string {\n return crypto.createHash('sha1').update(password).digest('hex');\n}\n","import { UserManager } from './user-manager';\nimport { type User, type UsersData } from '../types';\nimport { logger } from '../utils/logger';\n\n// Global reference to the current SDK's UserManager instance\nlet currentUserManager: UserManager | null = null;\n\n/**\n * Set the UserManager instance (called by SuperatomSDK during initialization)\n * This should be called with the SDK's UserManager instance\n * @param userManager - UserManager instance from SuperatomSDK\n */\nexport function setUserManager(userManager: UserManager): void {\n if (!userManager) {\n throw new Error('userManager cannot be null');\n }\n currentUserManager = userManager;\n logger.debug('UserManager instance set');\n}\n\n/**\n * Get the current UserManager instance\n */\nexport function getUserManager(): UserManager {\n if (!currentUserManager) {\n throw new Error(\n 'UserManager not initialized. Make sure SuperatomSDK is initialized before using user storage.'\n );\n }\n return currentUserManager;\n}\n\n/**\n * Load users from memory (UserManager maintains the cache)\n * @returns UsersData object containing all users\n */\nexport function loadUsers(): UsersData {\n const manager = getUserManager();\n return {\n users: manager.getAllUsers()\n };\n}\n\n/**\n * Save users to file immediately (forces sync)\n * @param usersData - UsersData object to save\n */\nexport async function saveUsers(usersData: UsersData): Promise<void> {\n const manager = getUserManager();\n\n // Clear existing users and repopulate\n manager.deleteAllUsers();\n\n // Add all users from the provided data\n for (const user of usersData.users) {\n try {\n manager.createUser(user);\n } catch (error) {\n // User might already exist, update instead\n if (manager.userExists(user.username)) {\n manager.updateUser(user.username, user);\n }\n }\n }\n\n // Force immediate sync to file\n await manager.forceSync();\n}\n\n/**\n * Find a user by username\n * @param username - Username to search for\n * @returns User object if found, null otherwise\n */\nexport function findUserByUsername(username: string): User | null {\n const manager = getUserManager();\n const user = manager.getUser(username);\n return user || null;\n}\n\n/**\n * Find a user by email\n * @param email - Email to search for\n * @returns User object if found, null otherwise\n */\nexport function findUserByEmail(email: string): User | null {\n const manager = getUserManager();\n const user = manager.getUserByEmail(email);\n return user || null;\n}\n\n/**\n * Find a user by username or email\n * @param identifier - Username or email to search for\n * @returns User object if found, null otherwise\n */\nexport function findUserByUsernameOrEmail(identifier: string): User | null {\n const manager = getUserManager();\n const user = manager.getUserByUsernameOrEmail(identifier);\n return user || null;\n}\n\n/**\n * Add WebSocket ID to a user's wsIds array\n * @param username - Username to update\n * @param wsId - WebSocket ID to add\n * @returns true if successful, false otherwise\n */\nexport function addWsIdToUser(username: string, wsId: string): boolean {\n try {\n const manager = getUserManager();\n return manager.addWsId(username, wsId);\n } catch (error) {\n logger.error('Error adding WebSocket ID:', error);\n return false;\n }\n}\n\n/**\n * Remove WebSocket ID from a user's wsIds array\n * @param username - Username to update\n * @param wsId - WebSocket ID to remove\n * @returns true if successful, false otherwise\n */\nexport function removeWsIdFromUser(username: string, wsId: string): boolean {\n try {\n const manager = getUserManager();\n return manager.removeWsId(username, wsId);\n } catch (error) {\n logger.error('Error removing WebSocket ID:', error);\n return false;\n }\n}\n\n/**\n * Cleanup and destroy the UserManager\n */\nexport async function cleanupUserStorage(): Promise<void> {\n if (currentUserManager) {\n await currentUserManager.destroy();\n currentUserManager = null;\n logger.info('UserManager cleaned up');\n }\n}\n\n// Export types\nexport type { User, UsersData } from '../types';\n","import { findUserByUsernameOrEmail, addWsIdToUser } from \"./user-storage\";\nimport { hashPassword } from \"./utils\";\nimport { logger } from \"../utils/logger\";\nimport type { CollectionRegistry } from \"../types\";\n\nexport interface LoginCredentials {\n username?: string;\n email?: string;\n password: string;\n}\n\nexport interface ValidationResult {\n success: boolean;\n error?: string;\n data?: any;\n username?: string;\n userId?: string | number;\n}\n\n/**\n * Validate user credentials against database first, then file storage as fallback\n * @param credentials - Login credentials with username/email and password\n * @param collections - Optional collection registry for database lookup\n * @returns ValidationResult indicating success or failure\n */\nexport async function validateUser(\n credentials: LoginCredentials,\n collections?: CollectionRegistry\n): Promise<ValidationResult> {\n const { username, email, password } = credentials;\n const identifier = username || email;\n\n logger.debug('[validateUser] Starting user validation');\n logger.debug(`[validateUser] Username provided: ${username ? '✓' : '✗'}, Email provided: ${email ? '✓' : '✗'}, Password provided: ${password ? '✓' : '✗'}`);\n\n // Check if identifier (username or email) and password are provided\n if (!identifier || !password) {\n logger.warn('[validateUser] Validation failed: Username/email and password are required');\n return {\n success: false,\n error: 'Username or email and password are required'\n };\n }\n\n // Try database lookup first if collections are available\n if (collections && collections['users'] && collections['users']['authenticate']) {\n logger.debug(`[validateUser] Attempting database authentication for: ${identifier}`);\n try {\n const dbResult = await collections['users']['authenticate']({\n identifier,\n password\n });\n\n logger.info('[validateUser] Database authentication attempt completed', dbResult);\n\n if (dbResult && dbResult.success && dbResult.data) {\n logger.info(`[validateUser] ✓ User authenticated via database: ${dbResult.data.username}`);\n return {\n success: true,\n data: dbResult.data.username,\n username: dbResult.data.username,\n userId: dbResult.data.id\n };\n } else {\n logger.debug(`[validateUser] Database auth failed for ${identifier}: ${dbResult?.error || 'Invalid credentials'}`);\n // Don't fall back to file if DB explicitly says invalid credentials\n if (dbResult && dbResult.error === 'Invalid credentials') {\n return {\n success: false,\n error: 'Invalid credentials'\n };\n }\n }\n } catch (error) {\n const errorMsg = error instanceof Error ? error.message : String(error);\n logger.debug(`[validateUser] Database lookup error: ${errorMsg}, falling back to file storage`);\n }\n } else {\n logger.debug('[validateUser] No users collection available, using file storage only');\n }\n\n // Fall back to file-based validation\n logger.info(`[validateUser] Attempting file-based validation for: ${identifier}`);\n\n // Find user by username or email from in-memory cache\n const user = findUserByUsernameOrEmail(identifier);\n\n if (!user) {\n logger.warn(`[validateUser] Validation failed: User not found - ${identifier}`);\n return {\n success: false,\n error: 'Invalid username or email'\n };\n }\n\n logger.debug(`[validateUser] User found in file storage: ${user.username}, verifying password`);\n\n // Hash the stored password and compare with provided password\n const hashedPassword = hashPassword(user.password);\n\n if (hashedPassword !== password) {\n logger.warn(`[validateUser] Validation failed: Invalid password for user - ${user.username}`);\n logger.debug(`[validateUser] Password hash mismatch for user: ${user.username}`);\n return {\n success: false,\n error: 'Invalid password'\n };\n }\n\n logger.info(`[validateUser] ✓ User validated via file storage: ${user.username}`);\n return {\n success: true,\n data: user.username,\n username: user.username\n };\n}\n\n/**\n * Authenticate user and store WebSocket ID\n * Uses database first via collections, then falls back to file storage\n * @param credentials - Login credentials\n * @param wsId - WebSocket ID to store\n * @param collections - Optional collection registry for database lookup\n * @returns ValidationResult with authentication status\n */\nexport async function authenticateAndStoreWsId(\n credentials: LoginCredentials,\n wsId: string,\n collections?: CollectionRegistry\n): Promise<ValidationResult> {\n const identifier = credentials.username || credentials.email;\n logger.debug('[authenticateAndStoreWsId] Starting authentication and WebSocket ID storage');\n\n // Validate user credentials first\n logger.debug('[authenticateAndStoreWsId] Validating user credentials');\n const validationResult = await validateUser(credentials, collections);\n\n if (!validationResult.success) {\n logger.warn(`[authenticateAndStoreWsId] User validation failed for: ${identifier}`);\n return validationResult;\n }\n\n // Use the username from validation result (guaranteed to exist if validation succeeded)\n const username = validationResult.username!;\n logger.info(`[authenticateAndStoreWsId] User ${username} validated, storing WebSocket ID`);\n\n // Store wsId in user's wsIds array using UserManager\n // Changes are automatically synced to file via setInterval\n logger.debug(`[authenticateAndStoreWsId] Calling addWsIdToUser for ${username}`);\n addWsIdToUser(username, wsId);\n\n logger.debug(`[authenticateAndStoreWsId] WebSocket ID ${wsId} associated with user ${username}`);\n return validationResult;\n}\n\n/**\n * Verify authentication token\n * @param authToken - Base64 encoded auth token containing username and password\n * @param collections - Optional collection registry for database lookup\n * @returns ValidationResult indicating if token is valid\n */\nexport async function verifyAuthToken(\n authToken: string,\n collections?: CollectionRegistry\n): Promise<ValidationResult> {\n try {\n logger.debug('[verifyAuthToken] Starting token verification');\n\n // Decode base64 token\n logger.debug('[verifyAuthToken] Decoding base64 token');\n const decodedString = Buffer.from(authToken, 'base64').toString('utf-8');\n\n logger.debug('[verifyAuthToken] Parsing decoded token as JSON');\n const credentials = JSON.parse(decodedString);\n\n logger.debug('[verifyAuthToken] Token decoded and parsed successfully');\n logger.debug(`[verifyAuthToken] Token contains username: ${credentials.username ? '✓' : '✗'}`);\n\n // Validate credentials\n logger.debug('[verifyAuthToken] Validating credentials from token');\n const result = await validateUser(credentials, collections);\n\n if (result.success) {\n logger.info(`[verifyAuthToken] ✓ Token verified successfully for user: ${credentials.username || 'unknown'}`);\n } else {\n logger.warn(`[verifyAuthToken] Token verification failed: ${result.error}`);\n }\n\n return result;\n } catch (error) {\n const errorMsg = error instanceof Error ? error.message : String(error);\n logger.error(`[verifyAuthToken] Failed to verify auth token: ${errorMsg}`);\n logger.debug('[verifyAuthToken] Token verification error details:', error);\n\n return {\n success: false,\n error: 'Invalid token format'\n };\n }\n}\n","import { decodeBase64ToJson } from \"../auth/utils\";\nimport { authenticateAndStoreWsId } from \"../auth/validator\";\nimport { AuthLoginRequestMessageSchema, Message, CollectionRegistry } from \"../types\";\nimport { logger } from \"../utils/logger\";\n\n\n\nexport async function handleAuthLoginRequest(\n data: any,\n collections: CollectionRegistry,\n sendMessage: (message: Message) => void\n): Promise<void> {\n try {\n logger.debug('[AUTH_LOGIN_REQ] Parsing incoming auth login request');\n const authRequest = AuthLoginRequestMessageSchema.parse(data);\n const { id, payload } = authRequest;\n\n const login_data = payload.login_data;\n\n const wsId = authRequest.from.id ;\n\n logger.info(`[AUTH_LOGIN_REQ ${id}] Processing auth login request from client: ${wsId}`);\n logger.debug(`[AUTH_LOGIN_REQ ${id}] Login data present: ${!!login_data}`);\n\n if(!login_data){\n logger.error(`[AUTH_LOGIN_REQ ${id}] Login data not found in request`);\n sendDataResponse(id, {\n success: false,\n error: 'Login data not found'\n }, sendMessage, wsId);\n return;\n }\n\n\n // Decode base64 data and parse JSON\n logger.debug(`[AUTH_LOGIN_REQ ${id}] Decoding base64 login data`);\n let loginData: any;\n try {\n loginData = decodeBase64ToJson(login_data);\n logger.debug(`[AUTH_LOGIN_REQ ${id}] Login data decoded successfully`);\n } catch (error) {\n const errorMsg = error instanceof Error ? error.message : String(error);\n logger.error(`[AUTH_LOGIN_REQ ${id}] Failed to decode login data: ${errorMsg}`);\n logger.debug(`[AUTH_LOGIN_REQ ${id}] Decode error details:`, error);\n sendDataResponse(id, {\n success: false,\n error: 'Invalid login data format'\n }, sendMessage, wsId);\n return;\n }\n\n // Extract username/email and password from decoded data\n const { username, email, password } = loginData;\n const identifier = username || email;\n\n logger.debug(`[AUTH_LOGIN_REQ ${id}] Validating credentials - username: ${username ? '✓' : '✗'}, email: ${email ? '✓' : '✗'}, password: ${password ? '✓' : '✗'}`);\n\n if (!identifier) {\n logger.error(`[AUTH_LOGIN_REQ ${id}] Username or email not found in login data`);\n sendDataResponse(id, {\n success: false,\n error: 'Username or email is required'\n }, sendMessage, wsId);\n return;\n }\n\n if (!password) {\n logger.error(`[AUTH_LOGIN_REQ ${id}] Password not found in login data`);\n sendDataResponse(id, {\n success: false,\n error: 'Password not found in login data'\n }, sendMessage, wsId);\n return;\n }\n\n\n if(!wsId){\n logger.error(`[AUTH_LOGIN_REQ ${id}] WebSocket ID not found in request`);\n sendDataResponse(id, {\n success: false,\n error: 'WebSocket ID not found'\n }, sendMessage, wsId);\n return;\n }\n\n logger.info(`[AUTH_LOGIN_REQ ${id}] Credentials validated, authenticating user: ${identifier} username: ${username} email: ${email} password: ${password}`);\n\n // Authenticate user and store wsId\n logger.debug(`[AUTH_LOGIN_REQ ${id}] Calling authenticateAndStoreWsId for user: ${identifier}`);\n const authResult = await authenticateAndStoreWsId(\n { username, email, password },\n wsId,\n collections\n );\n\n logger.info(`[AUTH_LOGIN_REQ ${id}] Authentication result for ${identifier}: ${authResult.success ? 'success' : 'failed'}`);\n if (!authResult.success) {\n logger.warn(`[AUTH_LOGIN_REQ ${id}] Authentication failed for ${identifier}: ${authResult.error}`);\n } else {\n logger.info(`[AUTH_LOGIN_REQ ${id}] User ${authResult.username || identifier} authenticated successfully`);\n }\n\n // Send response\n logger.debug(`[AUTH_LOGIN_REQ ${id}] Sending auth response to client`);\n sendDataResponse(id, authResult, sendMessage, wsId);\n\n logger.info(`[AUTH_LOGIN_REQ ${id}] ${authResult.success ? '✓' : '✗'} Auth login request completed`);\n return ;\n }\n catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';\n const errorStack = error instanceof Error ? error.stack : undefined;\n\n logger.error(`[AUTH_LOGIN_REQ] Failed to handle auth login request: ${errorMessage}`);\n logger.debug(`[AUTH_LOGIN_REQ] Error stack trace:`, errorStack);\n\n // Try to send error response\n try {\n const parsedData = data as any;\n if (parsedData?.id) {\n sendDataResponse(parsedData.id, {\n success: false,\n error: `Internal error: ${errorMessage}`\n }, sendMessage, parsedData.from?.id);\n }\n } catch (sendError) {\n logger.error('[AUTH_LOGIN_REQ] Failed to send error response:', sendError);\n }\n }\n}\n\n\n/**\n * Send a data_res response message\n */\nfunction sendDataResponse(\n id: string,\n res: {success: boolean; error?: string; data?: any},\n sendMessage: (message: Message) => void,\n clientId?: string,\n): void {\n const response: Message = {\n id,\n type: 'AUTH_LOGIN_RES',\n from: { type: 'data-agent' },\n to: {\n type: 'runtime',\n id: clientId\n },\n payload:{\n ...res,\n }\n };\n\n logger.debug(`[AUTH_LOGIN_RES ${id}] Sending ${res.success ? 'successful' : 'failed'} auth response to client: ${clientId}`);\n logger.debug(`[AUTH_LOGIN_RES ${id}] Response payload size: ${JSON.stringify(response).length} bytes`);\n\n if (res.error) {\n logger.debug(`[AUTH_LOGIN_RES ${id}] Error message: ${res.error}`);\n }\n\n sendMessage(response);\n}\n\n","import { verifyAuthToken } from \"../auth/validator\";\nimport { AuthVerifyRequestMessageSchema, Message, CollectionRegistry } from \"../types\";\nimport { logger } from \"../utils/logger\";\n\n\nexport async function handleAuthVerifyRequest(\n data: any,\n collections: CollectionRegistry,\n sendMessage: (message: Message) => void\n): Promise<void> {\n try {\n logger.debug('[AUTH_VERIFY_REQ] Parsing incoming auth verify request');\n const authRequest = AuthVerifyRequestMessageSchema.parse(data);\n const { id, payload } = authRequest;\n\n const token = payload.token;\n\n const wsId = authRequest.from.id ;\n\n logger.info(`[AUTH_VERIFY_REQ ${id}] Processing auth verify request from client: ${wsId}`);\n logger.debug(`[AUTH_VERIFY_REQ ${id}] Token present: ${!!token}`);\n logger.debug(`[AUTH_VERIFY_REQ ${id}] Token length: ${token ? token.length : 0} characters`);\n\n if(!token){\n logger.error(`[AUTH_VERIFY_REQ ${id}] Token not found in request`);\n sendDataResponse(id, {\n success: false,\n error: 'Token not found'\n }, sendMessage, wsId);\n return;\n }\n\n\n if(!wsId){\n logger.error(`[AUTH_VERIFY_REQ ${id}] WebSocket ID not found in request`);\n sendDataResponse(id, {\n success: false,\n error: 'WebSocket ID not found'\n }, sendMessage, wsId);\n return;\n }\n\n logger.info(`[AUTH_VERIFY_REQ ${id}] Token validation starting`);\n logger.debug(`[AUTH_VERIFY_REQ ${id}] WebSocket ID: ${wsId}`);\n\n // Verify token\n logger.debug(`[AUTH_VERIFY_REQ ${id}] Calling verifyAuthToken`);\n const startTime = Date.now();\n\n const authResult = await verifyAuthToken(token, collections);\n\n const verificationTime = Date.now() - startTime;\n logger.info(`[AUTH_VERIFY_REQ ${id}] Token verification completed in ${verificationTime}ms - ${authResult.success ? 'valid' : 'invalid'}`);\n\n if (!authResult.success) {\n logger.warn(`[AUTH_VERIFY_REQ ${id}] Token verification failed: ${authResult.error}`);\n } else {\n logger.info(`[AUTH_VERIFY_REQ ${id}] Token verified successfully for user: ${authResult.data || 'unknown'}`);\n }\n\n // Send response\n logger.debug(`[AUTH_VERIFY_REQ ${id}] Sending verification response to client`);\n sendDataResponse(id, authResult, sendMessage, wsId);\n\n logger.info(`[AUTH_VERIFY_REQ ${id}] ${authResult.success ? '✓' : '✗'} Auth verify request completed`);\n return ;\n }\n catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';\n const errorStack = error instanceof Error ? error.stack : undefined;\n\n logger.error(`[AUTH_VERIFY_REQ] Failed to handle auth verify request: ${errorMessage}`);\n logger.debug(`[AUTH_VERIFY_REQ] Error stack trace:`, errorStack);\n\n // Try to send error response\n try {\n const parsedData = data as any;\n if (parsedData?.id) {\n sendDataResponse(parsedData.id, {\n success: false,\n error: `Internal error: ${errorMessage}`\n }, sendMessage, parsedData.from?.id);\n }\n } catch (sendError) {\n logger.error('[AUTH_VERIFY_REQ] Failed to send error response:', sendError);\n }\n }\n}\n\n/**\n * Send a data_res response message\n */\nfunction sendDataResponse(\n id: string,\n res: {success: boolean; error?: string; data?: any},\n sendMessage: (message: Message) => void,\n clientId?: string,\n): void {\n const response: Message = {\n id,\n type: 'AUTH_VERIFY_RES',\n from: { type: 'data-agent' },\n to: {\n type: 'runtime',\n id: clientId\n },\n payload:{\n ...res,\n }\n };\n\n logger.debug(`[AUTH_VERIFY_RES ${id}] Sending ${res.success ? 'successful' : 'failed'} verification response to client: ${clientId}`);\n logger.debug(`[AUTH_VERIFY_RES ${id}] Response payload size: ${JSON.stringify(response).length} bytes`);\n\n if (res.error) {\n logger.debug(`[AUTH_VERIFY_RES ${id}] Error message: ${res.error}`);\n }\n\n if (res.data) {\n logger.debug(`[AUTH_VERIFY_RES ${id}] User verified: ${res.data}`);\n }\n\n sendMessage(response);\n} ","import fs from 'fs';\nimport path from 'path';\nimport { logger } from '../utils/logger';\nimport { PROMPTS, DATABASE_RULES } from './prompts';\n\nexport interface PromptLoaderConfig {\n\tpromptsDir?: string;\n}\n\ninterface CachedPromptTemplate {\n\tsystem: string;\n\tuser: string;\n}\n\n/**\n * PromptLoader class for loading and processing prompt templates\n * Tries to load from file system first, then falls back to hardcoded prompts\n */\nexport class PromptLoader {\n\tprivate promptsDir: string;\n\tprivate promptCache: Map<string, CachedPromptTemplate> = new Map();\n\tprivate databaseRulesCache: Map<string, string> = new Map();\n\tprivate isInitialized: boolean = false;\n\tprivate databaseType: string = 'postgresql';\n\n\tconstructor(config?: PromptLoaderConfig) {\n\t\t// Default to backend's .prompts directory (where the SDK is being used)\n\t\tthis.promptsDir = config?.promptsDir || path.join(process.cwd(), '.prompts');\n\t}\n\n\t/**\n\t * Load a prompt template from file system OR fallback to hardcoded prompts\n\t * @param promptName - Name of the prompt folder\n\t * @returns Template with system and user prompts\n\t */\n\tprivate loadPromptTemplate(promptName: string): CachedPromptTemplate {\n\t\t// First, try to load from file system (backend's .prompts directory)\n\t\ttry {\n\t\t\tconst systemPath = path.join(this.promptsDir, promptName, 'system.md');\n\t\t\tconst userPath = path.join(this.promptsDir, promptName, 'user.md');\n\n\t\t\tif (fs.existsSync(systemPath) && fs.existsSync(userPath)) {\n\t\t\t\tconst system = fs.readFileSync(systemPath, 'utf-8');\n\t\t\t\tconst user = fs.readFileSync(userPath, 'utf-8');\n\t\t\t\treturn { system, user };\n\t\t\t}\n\t\t} catch (error) {\n\t\t\t// Ignore file system errors and try fallback\n\t\t\tlogger.error(`Could not load '${promptName}' from file system, trying fallback...`);\n\t\t}\n\n\t\t// Fallback to hardcoded prompts\n\t\tconst hardcodedPrompt = PROMPTS[promptName];\n\t\tif (hardcodedPrompt) {\n\t\t\treturn hardcodedPrompt;\n\t\t}\n\n\t\tthrow new Error(`Prompt template '${promptName}' not found in either ${this.promptsDir} or hardcoded prompts. Available prompts: ${Object.keys(PROMPTS).join(', ')}`);\n\t}\n\n\t/**\n\t * Initialize and cache all prompts into memory\n\t * Tries file system first, then falls back to hardcoded prompts\n\t */\n\tasync initialize(): Promise<void> {\n\t\tif (this.isInitialized) {\n\t\t\tlogger.debug('PromptLoader already initialized, skipping...');\n\t\t\treturn;\n\t\t}\n\n\n\t\tconst promptTypes = Object.keys(PROMPTS);\n\n\t\tfor (const promptName of promptTypes) {\n\t\t\ttry {\n\t\t\t\tconst template = this.loadPromptTemplate(promptName);\n\t\t\t\tthis.promptCache.set(promptName, template);\n\t\t\t} catch (error) {\n\t\t\t\tlogger.error(`Failed to load prompt '${promptName}':`, error);\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t}\n\n\t\tthis.isInitialized = true;\n\t}\n\n\t/**\n\t * Replace variables in a template string using {{VARIABLE_NAME}} pattern\n\t * @param template - Template string with placeholders\n\t * @param variables - Variables to replace in the template\n\t * @returns Processed string\n\t */\n\tprivate replaceVariables(\n\t\ttemplate: string,\n\t\tvariables: Record<string, string | number | boolean | any[]>\n\t): string {\n\t\t// Single-pass replacement of every {{VARIABLE_NAME}} placeholder.\n\t\t//\n\t\t// Using a REPLACER FUNCTION is critical: String.replace() interprets `$`\n\t\t// sequences ($&, $', $`, $1, $$) in a replacement *string*, but inserts a\n\t\t// function's return value VERBATIM. Injected values routinely contain `$`\n\t\t// (e.g. DuckDB regex anchors like '(\\S+)$' in KB recipe nodes); the old\n\t\t// per-key string replace mangled those — splicing later sections of the\n\t\t// prompt into the middle of the value and destroying the content.\n\t\t//\n\t\t// A single pass (vs a loop of per-key replaces) also prevents value-injection:\n\t\t// the regex scans the ORIGINAL template once, so a value that happens to\n\t\t// contain `{{OTHER_KEY}}` is never re-substituted, and an unknown placeholder\n\t\t// is left intact rather than blanked.\n\t\treturn template.replace(/\\{\\{(\\w+)\\}\\}/g, (match, key) => {\n\t\t\tif (!(key in variables)) return match;\n\t\t\tconst value = variables[key];\n\t\t\treturn typeof value === 'string' ? value : JSON.stringify(value);\n\t\t});\n\t}\n\n\t/**\n\t * Load both system and user prompts from cache and replace variables\n\t * Supports prompt caching by splitting static and dynamic content\n\t * @param promptName - Name of the prompt\n\t * @param variables - Variables to replace in the templates\n\t * @returns Object containing both system and user prompts (system can be string or array for caching)\n\t */\n\tasync loadPrompts(\n\t\tpromptName: string,\n\t\tvariables: Record<string, string | number | boolean | any[]>\n\t): Promise<{ system: string | any[]; user: string }> {\n\t\tif (!this.isInitialized) {\n\t\t\tlogger.warn('PromptLoader not initialized, initializing now...');\n\t\t\tawait this.initialize();\n\t\t}\n\n\t\tconst template = this.promptCache.get(promptName);\n\n\t\tif (!template) {\n\t\t\tthrow new Error(`Prompt template '${promptName}' not found in cache. Available prompts: ${Array.from(this.promptCache.keys()).join(', ')}`);\n\t\t}\n\n\t\t// Check if the prompt has a context section marker (for caching)\n\t\tconst contextMarker = '---\\n\\n## CONTEXT';\n\n\t\tif (template.system.includes(contextMarker)) {\n\t\t\t// Split into static (cacheable) and dynamic (context) sections\n\t\t\tconst [staticPart, contextPart] = template.system.split(contextMarker);\n\n\t\t\t// Replace variables in BOTH parts\n\t\t\t// Static part may contain schema/rules which are per-project but stable\n\t\t\tconst processedStatic = this.replaceVariables(staticPart, variables);\n\t\t\tconst processedContext = this.replaceVariables(contextMarker + contextPart, variables);\n\n\t\t\t// Return as array of blocks for Anthropic caching\n\t\t\t// The static part (with schema) will be cached after first request\n\t\t\treturn {\n\t\t\t\tsystem: [\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'text',\n\t\t\t\t\t\ttext: processedStatic.trim(),\n\t\t\t\t\t\tcache_control: { type: 'ephemeral' }\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: 'text',\n\t\t\t\t\t\ttext: processedContext.trim()\n\t\t\t\t\t}\n\t\t\t\t],\n\t\t\t\tuser: this.replaceVariables(template.user, variables)\n\t\t\t};\n\t\t}\n\n\t\t// Fallback: No caching, return as simple string\n\t\treturn {\n\t\t\tsystem: this.replaceVariables(template.system, variables),\n\t\t\tuser: this.replaceVariables(template.user, variables)\n\t\t};\n\t}\n\n\t/**\n\t * DEPRECATED: Use loadPrompts instead\n\t * Load a single prompt file and replace variables using {{VARIABLE_NAME}} pattern\n\t */\n\tasync loadPrompt(\n\t\tpromptName: string,\n\t\tpromptType: 'system' | 'user',\n\t\tvariables: Record<string, string | number | boolean | any[]>\n\t): Promise<string | any[]> {\n\t\tconst prompts = await this.loadPrompts(promptName, variables);\n\t\treturn promptType === 'system' ? prompts.system : prompts.user;\n\t}\n\n\t/**\n\t * Set custom prompts directory (requires re-initialization)\n\t * @param dir - Path to the prompts directory\n\t */\n\tsetPromptsDir(dir: string): void {\n\t\tthis.promptsDir = dir;\n\t\tthis.isInitialized = false;\n\t\tthis.promptCache.clear();\n\t}\n\n\t/**\n\t * Get current prompts directory\n\t * @returns Path to the prompts directory\n\t */\n\tgetPromptsDir(): string {\n\t\treturn this.promptsDir;\n\t}\n\n\t/**\n\t * Check if prompts are loaded in memory\n\t */\n\tisReady(): boolean {\n\t\treturn this.isInitialized;\n\t}\n\n\t/**\n\t * Get the number of cached prompts\n\t */\n\tgetCacheSize(): number {\n\t\treturn this.promptCache.size;\n\t}\n\n\t/**\n\t * Set the database type for SQL rules loading\n\t * @param type - Database type ('postgresql' | 'mssql')\n\t */\n\tsetDatabaseType(type: string): void {\n\t\tthis.databaseType = type;\n\t\tthis.databaseRulesCache.clear();\n\t}\n\n\t/**\n\t * Get current database type\n\t * @returns Database type string\n\t */\n\tgetDatabaseType(): string {\n\t\treturn this.databaseType;\n\t}\n\n\t/**\n\t * Load database-specific SQL rules from file system\n\t * Falls back to minimal default rules if file not found\n\t * @returns Database rules as a string\n\t */\n\tasync loadDatabaseRules(): Promise<string> {\n\t\t// Check cache first\n\t\tif (this.databaseRulesCache.has(this.databaseType)) {\n\t\t\treturn this.databaseRulesCache.get(this.databaseType)!;\n\t\t}\n\n\t\t// Try loading from file system\n\t\tconst rulesPath = path.join(this.promptsDir, 'database-rules', `${this.databaseType}.md`);\n\n\t\ttry {\n\t\t\tif (fs.existsSync(rulesPath)) {\n\t\t\t\tconst rules = fs.readFileSync(rulesPath, 'utf-8');\n\t\t\t\tthis.databaseRulesCache.set(this.databaseType, rules);\n\t\t\t\treturn rules;\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tlogger.warn(`Could not load database rules for '${this.databaseType}' from file system: ${error}`);\n\t\t}\n\n\t\t// Fallback to default rules\n\t\tconst defaultRules = this.getDefaultDatabaseRules();\n\t\tthis.databaseRulesCache.set(this.databaseType, defaultRules);\n\t\tlogger.warn(`Using default database rules for '${this.databaseType}' (file not found at ${rulesPath})`);\n\t\treturn defaultRules;\n\t}\n\n\t/**\n\t * Load database-specific SQL rules for a given source type.\n\t * Used by the agent architecture where multiple source types can coexist.\n\t * @param sourceType - Source type from tool ID (e.g., 'postgres', 'mssql', 'mysql')\n\t * @returns Database rules as a string\n\t */\n\tasync loadDatabaseRulesForType(sourceType: string): Promise<string> {\n\t\t// Map source types to database rule file names\n\t\tconst typeMap: Record<string, string> = {\n\t\t\t'postgres': 'postgresql',\n\t\t\t'postgresql': 'postgresql',\n\t\t\t'mssql': 'mssql',\n\t\t\t'mysql': 'postgresql', // MySQL uses similar rules to PostgreSQL\n\t\t\t'excel': 'duckdb', // Excel sources use DuckDB engine\n\t\t\t'csv': 'duckdb', // CSV sources use DuckDB engine\n\t\t};\n\t\tconst dbType = typeMap[sourceType] || 'postgresql';\n\n\t\t// Check cache first\n\t\tif (this.databaseRulesCache.has(dbType)) {\n\t\t\treturn this.databaseRulesCache.get(dbType)!;\n\t\t}\n\n\t\t// Try loading from file system\n\t\tconst rulesPath = path.join(this.promptsDir, 'database-rules', `${dbType}.md`);\n\n\t\ttry {\n\t\t\tif (fs.existsSync(rulesPath)) {\n\t\t\t\tconst rules = fs.readFileSync(rulesPath, 'utf-8');\n\t\t\t\tthis.databaseRulesCache.set(dbType, rules);\n\t\t\t\treturn rules;\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tlogger.warn(`Could not load database rules for '${dbType}' from file system: ${error}`);\n\t\t}\n\n\t\t// Fallback to default rules\n\t\tconst defaultRules = this.getDefaultDatabaseRulesForType(dbType);\n\t\tthis.databaseRulesCache.set(dbType, defaultRules);\n\t\treturn defaultRules;\n\t}\n\n\t/**\n\t * Get default database rules as fallback (mirror of\n\t * backend/.prompts/database-rules/<engine>.md, embedded via DATABASE_RULES).\n\t * @returns Database rules for the configured databaseType\n\t */\n\tprivate getDefaultDatabaseRules(): string {\n\t\treturn DATABASE_RULES[this.databaseType] ?? DATABASE_RULES['postgresql'];\n\t}\n\n\t/**\n\t * Get default database rules for a specific engine type (mirror of\n\t * backend/.prompts/database-rules/<engine>.md, embedded via DATABASE_RULES).\n\t */\n\tprivate getDefaultDatabaseRulesForType(dbType: string): string {\n\t\treturn DATABASE_RULES[dbType] ?? DATABASE_RULES['postgresql'];\n\t}\n}\n\n// Export a singleton instance\n// Default to backend's .prompts directory (where the SDK is being used)\n// If prompts are not found there, the loader will fallback to hardcoded prompts\nconst defaultPromptsPath = process.env.PROMPTS_DIR || path.join(process.cwd(), '.prompts');\n\nexport const promptLoader = new PromptLoader({\n\tpromptsDir: defaultPromptsPath\n});\n","/**\n * Hardcoded prompt templates\n * These prompts are embedded in the SDK to avoid file system dependencies\n *\n * NOTE: This is a FALLBACK mirror of backend/.prompts/. The live prompts are the\n * .md files in backend/.prompts/<name>/{system,user}.md; PromptLoader only uses\n * these when those files are absent. Regenerated via _prompt-regen.ts — keep in\n * sync with backend/.prompts/ when prompts change.\n */\n\nexport interface PromptTemplate {\n\tsystem: string;\n\tuser: string;\n}\n\nexport const PROMPTS: Record<string, PromptTemplate> = {\n\t'text-response': {\n\t\tsystem: `You are an AI assistant that answers user questions using data source tools.\n\n## 🚨 Multi-Source Architecture\n\nThis system supports MULTIPLE data sources per project. Each data source provides its own tool:\n\n**Source Types:**\n- **Database Sources** (PostgreSQL, MSSQL): Tool ID ends with \\`_query\\`\n- **File Sources** (Excel, CSV): Tool ID ends with \\`_read\\`\n- **API Sources** (Salesforce, HubSpot, REST): Tool ID ends with \\`_call\\`\n\n**IMPORTANT**: Each tool's description contains its schema (tables, columns, fields). Use the correct tool for your data need.\n\n## Core Rules\n- Be concise and direct\n- Use conversation history for context\n- Use the appropriate source tool for each data request\n\n## Using Source Tools\n\n### Database Tools (\\`*_query\\`)\n- Execute SQL queries against the specific database\n- Schema is in the tool description\n- Use proper SQL with LIMIT (default 10, max 10)\n\n### File Tools (\\`*_read\\`)\n- Read data from Excel/CSV files\n- Use parameters: \\`sheet\\`, \\`columns\\`, \\`filters\\`, \\`limit\\`\n- NEVER write SQL for file data\n\n### API Tools (\\`*_call\\`)\n- Call external APIs (Salesforce, HubSpot, etc.)\n- Use tool-specific parameters from description\n\n## External Tool Execution\n\n**IMMEDIATE** (Read/Fetch): Execute right away, use results in response.\n\n**DEFERRED** (Create/Update/Delete):\n- With \\`userProvidedData\\` → Execute with provided data\n- Without \\`userProvidedData\\` → DO NOT execute. Acknowledge intent, inform user a form will collect required fields.\n\n**Combined workflow**: Execute immediate tools first. Use their results to populate deferred tool parameters when identifiable.\n\n## Query Guidelines (for database tools)\n- Use correct table/column names from the tool's schema\n- ALWAYS limit results - default 10 rows, max 10 rows\n- Use \\`$paramName\\` placeholders for parameterized queries\n\n## Response Flow\n\n**Data viewing**:\n1. Identify which source has the data\n2. Execute the appropriate source tool\n3. Present results clearly\n\n**Data modification**:\n- Database: Acknowledge form support, fetch current values for UPDATE/DELETE\n- External tool (deferred): Acknowledge intent, list required fields\n\n## Component Suggestions\n\nAfter answering, suggest dashboard components:\n\n<DashboardComponents>\n**Dashboard Components:**\n1.ComponentType : reasoning\n2.ComponentType : reasoning\n</DashboardComponents>\n\n**Rules:**\n- First component answers user's question directly\n- Add context/supporting components for exploration\n- For modifications: 1-2 context components (KPICard), then Form\n\n## Output Format\n\nPlain text with:\n1. Tool/query analysis (if applicable)\n2. Results summary\n3. Dashboard components (in tags)\n\n**CRITICAL:** Return ONLY plain text (no JSON, no code blocks)\n\n## Database Rules (for database source tools)\n{{DATABASE_RULES}}\n\n---\n\n## CONTEXT (for this specific request)\n\n### Available External Tools\n{{AVAILABLE_EXTERNAL_TOOLS}}\n\n### Knowledge Base Context\n{{KNOWLEDGE_BASE_CONTEXT}}\n\n### Previous Conversation\n{{CONVERSATION_HISTORY}}`,\n\t\tuser: `Current Date and Time: {{CURRENT_DATETIME}}\n\n{{USER_PROMPT}}`\n\t},\n\t'match-text-components': {\n\t\tsystem: `You create dashboard visualizations from analysis results. Respond with ONLY valid JSON.\n\n## CRITICAL: NEVER EMBED DATA IN PROPS\n**Every visualization component MUST have a data source - either \\`query\\` OR \\`externalTool\\`. NEVER both null.**\n**Exception: \\`DynamicMarkdownBlock\\` only needs a \\`content\\` prop (markdown string) — no query or externalTool.**\n**NEVER put \\`data: [...]\\` in props. Components fetch their own data from the data source.**\n\n## CRITICAL: componentId MUST come from AVAILABLE_COMPONENTS\n**You MUST use EXACT \\`componentId\\` values from the AVAILABLE_COMPONENTS list below.**\n**NEVER invent or generate new component IDs. Only use IDs that exist in AVAILABLE_COMPONENTS.**\n**The same componentId CAN be reused multiple times with different props.**\n\n## CRITICAL: Multi-Source Architecture\n\nThis system supports MULTIPLE data sources per project. Each data source provides its own tool:\n\n| Tool ID | Source Type | Data Access Method |\n|---------|-------------|-------------------|\n| \\`*_query\\` | Database (PostgreSQL, MSSQL, MySQL) | SQL in \\`parameters.sql\\` |\n| \\`*_query\\` | File (Excel, CSV) via DuckDB | SQL in \\`parameters.sql\\` |\n| \\`*_call\\` | REST API | \\`parameters.endpoint\\`, \\`pathParams\\`, \\`queryParams\\`, \\`body\\` |\n| \\`*_graphql\\` | GraphQL API | \\`parameters.operation\\`, \\`variables\\` |\n| \\`federation_query\\` | **Cross-Source** (joins across multiple sources) | DuckDB SQL with schema-qualified table names in \\`parameters.sql\\` |\n\n**Each source has its own tool.** When a tool was executed in EXECUTED_TOOLS, components MUST reference that SAME tool via \\`externalTool\\`.\n\n### Cross-Source Federation Tool (\\`federation_query\\`)\n\nWhen a component needs data from **multiple sources** (e.g., JOIN an Excel table with a Postgres table), use the \\`federation_query\\` tool instead of individual source tools.\n\n| Tool ID | When to Use |\n|---------|-------------|\n| \\`<source_id>_query\\` | Data from a SINGLE source |\n| \\`federation_query\\` | Data from MULTIPLE sources (cross-source JOINs) |\n\n**Federation table naming:**\n- Excel/CSV tables: \\`\"schema_prefix\".\"sheet_or_table_name\"\\` (e.g., \\`\"sales_data\".\"Sheet1\"\\`)\n- Postgres tables: \\`\"schema_prefix\".\"db_schema\".\"table_name\"\\` (e.g., \\`\"my_postgres\".\"public\".\"orders\"\\`)\n- MySQL tables: \\`\"schema_prefix\".\"db_name\".\"table_name\"\\` (e.g., \\`\"my_mysql\".\"mydb\".\"customers\"\\`)\n\nCheck the \\`federation_query\\` tool in EXECUTED_TOOLS for the exact schema prefixes and table names available.\n\n---\n\n## Input\n\nYou receive TWO key inputs:\n1. **Analysis text** — The main agent's final analysis and interpretation of the data. Use this for understanding what to visualize and for the DynamicMarkdownBlock summary.\n2. **EXECUTED_TOOLS** — The complete record of all tool executions including: exact SQL queries (\\`parameters.sql\\`), tool IDs, field names and types (\\`outputSchema\\`), row counts, and sample data. **Use this as the primary source for building component SQL queries and referencing data sources.**\n\n## Component Count Rules\n\n**Strictly respect these visualization limits** (DynamicMarkdownBlock does NOT count toward the limit):\n\n- **simple**: Generate **2-3 visualizations** + 1 DynamicMarkdownBlock = **3-4 components total**\n- **medium**: Generate **3-5 visualizations** + 1 DynamicMarkdownBlock = **4-6 components total**\n- **complex**: Generate **5-7 visualizations** + 1 DynamicMarkdownBlock = **6-8 components total**\n\n## Your Task\n\n1. **Answer Component**: Identify the single best visualization that directly answers the user's question and generate it FIRST. Choose the component type based on what the data looks like in EXECUTED_TOOLS:\n - **Single aggregate value** (1 row, 1-2 columns) → KPI Card\n - **Categorical breakdown** (multiple rows, 1 category + 1 numeric column) → Bar Chart or Pie Chart\n - **Time series** (rows with date + numeric column) → Line Chart or Area Chart\n - **Detailed records** (multiple rows, 3+ columns) → Table\n - If EXECUTED_TOOLS returned **0 rows or all NULL values**, do NOT generate a visualization — only generate a DynamicMarkdownBlock explaining that no data was found\n2. **Conclusion Markdown**: ALWAYS include a \\`DynamicMarkdownBlock\\` component with a brief summary. The \\`content\\` must be 2-4 concise bullet points (using markdown \\`- \\` syntax) summarizing the key takeaways from the data — NO headings, NO paragraphs. Bold text is fine for emphasis within bullets. Each bullet should be 1 sentence max. This component does NOT need a \\`query\\` or \\`externalTool\\` — only \\`content\\` (markdown string). **CRITICAL: The markdown content must contain ONLY the final analysis and insights from the analysis text. Never include SQL queries, schema details, query logs, or raw tool output.**\n3. **Additional visualizations**: Based on the analysis text and EXECUTED_TOOLS data, generate additional visualizations that show different aspects of the data (e.g., if the answer is a KPI total, add a breakdown chart and a detail table). Stay within the complexity limit above.\n4. **Generate props**: Use \\`externalTool\\` to reference the executed source tool. Get SQL from \\`EXECUTED_TOOLS.parameters\\` — reuse exact SQL when possible, write new SQL only when a different aggregation or grouping is needed.\n5. **Dashboard metadata**: Title (5-10 words) and description (1-2 sentences)\n6. **Actions**: Exactly 3 simple follow-up questions. Each question must be answerable by running a SQL query on the available data — no consulting, opinion, or \"why\" questions. Explore different dimensions of the data (different grouping, time range, filter, comparison). Use specific column names or values from the data when relevant. Keep questions short and direct — one sentence, one clear ask.\n\n### Choosing Complexity\nDetermine complexity from the analysis text and EXECUTED_TOOLS:\n- **simple**: Analysis covers 1 metric or 1 list → 2-3 visualizations\n- **medium**: Analysis covers 2-3 aspects of the data → 3-5 visualizations\n- **complex**: Analysis covers 4+ aspects or multiple sources → 5-7 visualizations\n\n### When Data is Empty or Insufficient\nIf EXECUTED_TOOLS shows 0 rows returned or all key fields are NULL:\n- Do NOT generate chart/table components for that tool — they will render empty\n- Generate ONLY a DynamicMarkdownBlock explaining what was queried and that no data was found\n- If some tools returned data and others didn't, only generate visualizations for tools that returned data\n\n---\n\n## MANDATORY: Use externalTool for ALL Source Tools\n\n**When EXECUTED_TOOLS contains any tool execution, ALL components MUST use \\`externalTool\\`.**\n\nThis is REQUIRED because:\n- Multiple data sources may exist (Sales DB, Inventory DB, Excel files, APIs)\n- Each component must specify WHICH source to query\n- Raw SQL in \\`query\\` prop doesn't know which database to target\n\n### Tool Types and externalTool Structure\n\n**1. SQL Sources** (\\`*_query\\` suffix) — PostgreSQL, MSSQL, MySQL, Excel (DuckDB), CSV (DuckDB)\n\\`\\`\\`json\n{\n \"query\": null,\n \"externalTool\": {\n \"toolId\": \"source_uuid_query\",\n \"toolName\": \"Query Sales Database\",\n \"parameters\": {\n \"sql\": \"SELECT column1, column2 FROM table WHERE condition\"\n }\n }\n}\n\\`\\`\\`\n**IMPORTANT:** Use the correct row-limiting syntax for the database type:\n- **MSSQL:** \\`SELECT TOP 24 column1, column2 FROM table\\` (NEVER use LIMIT — MSSQL does not support it)\n- **PostgreSQL/MySQL/DuckDB (Excel, CSV):** \\`SELECT column1, column2 FROM table LIMIT 24\\`\n- Copy \\`toolId\\` and \\`toolName\\` from EXECUTED_TOOLS\n- Put SQL in \\`parameters.sql\\`\n- Can use GROUP BY, COUNT, SUM, AVG, JOINs\n\n**4. Cross-Source Federation** (\\`federation_query\\`)\n\\`\\`\\`json\n{\n \"query\": null,\n \"externalTool\": {\n \"toolId\": \"federation_query\",\n \"toolName\": \"Cross-Source Query\",\n \"parameters\": {\n \"sql\": \"SELECT e.\\\\\"Product\\\\\", p.inventory_count FROM \\\\\"sales_data\\\\\".\\\\\"Orders\\\\\" e JOIN \\\\\"pg_source\\\\\".\\\\\"public\\\\\".\\\\\"products\\\\\" p ON e.\\\\\"SKU\\\\\" = p.sku LIMIT 24\"\n }\n }\n}\n\\`\\`\\`\n- Use ONLY when the component needs data from multiple sources in one query\n- Always use schema-qualified table names (check \\`federation_query\\` tool description for prefixes)\n- Uses DuckDB SQL syntax (supports JOINs, CTEs, window functions, aggregations)\n- Double-quote table/column names that contain spaces\n\n## CRITICAL: SQL Query Rules for Components\n\n**If a component needs the same data that was already fetched -> REUSE the exact SQL from \\`EXECUTED_TOOLS.parameters.sql\\`.** Do not rewrite it.\n\n**If a component needs different data** (e.g., a KPI card needs a single aggregated value, a pie chart needs a different GROUP BY) -> **Write a new query using only real table and column names from \\`outputSchema\\`.** Never use column aliases from the analysis text as if they are real database columns — aliases from CTEs or computed expressions only exist within the query that created them.\n\n**2. REST API Sources** (\\`*_call\\` suffix)\n\\`\\`\\`json\n{\n \"query\": null,\n \"externalTool\": {\n \"toolId\": \"source_uuid_call\",\n \"toolName\": \"Call API Name\",\n \"parameters\": {\n \"endpoint\": \"endpoint_id\",\n \"queryParams\": { \"key\": \"value\" },\n \"limit\": 10\n }\n }\n}\n\\`\\`\\`\n- Use \\`endpoint\\`, \\`pathParams\\`, \\`queryParams\\`, \\`body\\`, \\`limit\\` parameters\n- Copy exact parameter values from EXECUTED_TOOLS when reusing the same data\n\n**3. GraphQL Sources** (\\`*_graphql\\` suffix)\n\\`\\`\\`json\n{\n \"query\": null,\n \"externalTool\": {\n \"toolId\": \"source_uuid_graphql\",\n \"toolName\": \"Query GraphQL API\",\n \"parameters\": {\n \"operation\": \"operation_id\",\n \"variables\": { \"key\": \"value\" },\n \"limit\": 10\n }\n }\n}\n\\`\\`\\`\n- Use \\`operation\\`, \\`variables\\`, \\`limit\\` parameters\n- Copy exact parameter values from EXECUTED_TOOLS when reusing the same data\n\n---\n\n## When to Use Direct \\`query\\` Prop (RARE)\n\n**ONLY use direct \\`query\\` prop when:**\n- EXECUTED_TOOLS is empty (no source tools were used)\n- System has a fallback \\`execute_query\\` tool\n\nIf EXECUTED_TOOLS contains ANY tool -> Use \\`externalTool\\` for ALL components\n\n---\n\n## Field Name Rules\n\nWhen using \\`externalTool\\`, use field names from the tool's \\`outputSchema\\` in EXECUTED_TOOLS:\n\n**For SQL Sources (databases, Excel, CSV):** Use SQL column names/aliases\n**For REST API Sources:** Use field names from the tool's response schema\n**For GraphQL Sources:** Use field names from the operation's response schema\n\n---\n\n## Component Decision by Source Type\n\n### For SQL Sources (\\`*_query\\`) — PostgreSQL, MSSQL, MySQL, Excel, CSV\n\n| Component Type | externalTool Usage |\n|----------------|-------------------|\n| KPI Card | \\`parameters.sql\\` with \\`SELECT COUNT/SUM/AVG ...\\` (single row) |\n| Bar/Pie Chart | \\`parameters.sql\\` with \\`GROUP BY\\` for distributions |\n| Table | \\`parameters.sql\\` with row limit (use \\`TOP 24\\` for MSSQL, \\`LIMIT 24\\` for PostgreSQL/MySQL/DuckDB) |\n| Form | \\`parameters.sql\\` with \\`INSERT/UPDATE/DELETE\\` and \\`$placeholders\\` |\n\n### For Cross-Source (\\`federation_query\\`) — JOINs across sources\n\n| Component Type | externalTool Usage |\n|----------------|-------------------|\n| Any visualization | \\`parameters.sql\\` with schema-qualified table names and DuckDB SQL syntax. Use \\`LIMIT 24\\` for row limits. |\n\n### For REST API Sources (\\`*_call\\`)\n\n| Component Type | externalTool Usage |\n|----------------|-------------------|\n| All types | Use \\`externalTool\\` with \\`endpoint\\`, \\`queryParams\\`, etc. |\n\n### For GraphQL Sources (\\`*_graphql\\`)\n\n| Component Type | externalTool Usage |\n|----------------|-------------------|\n| All types | Use \\`externalTool\\` with \\`operation\\`, \\`variables\\`, etc. |\n\n---\n\n## Props Generation Rules\n\n### Config Keys\n- String/category columns -> \\`xAxisKey\\`, \\`nameKey\\`, \\`labelKey\\`\n- Numeric columns -> \\`yAxisKey\\`, \\`valueKey\\`, \\`dataKey\\`\n- Use EXACT field names from the source tool's schema\n\n### SQL Rules (for SQL tools only)\n- Default 24 rows for dashboard components\n- SQL on SINGLE LINE (no newlines)\n- KPI queries MUST return exactly 1 row\n\n---\n\n## Form Components\n\n### Database Form\n\\`\\`\\`json\n{\n \"query\": null,\n \"externalTool\": {\n \"toolId\": \"db_source_query\",\n \"toolName\": \"Query Database\",\n \"parameters\": {\n \"sql\": \"INSERT INTO table (col1) VALUES ($col1)\"\n }\n },\n \"fields\": [...]\n}\n\\`\\`\\`\n\n### External Tool Form (deferred tools)\n\\`\\`\\`json\n{\n \"query\": null,\n \"externalTool\": {\n \"toolId\": \"from_DEFERRED_TOOLS\",\n \"toolName\": \"Tool Name\",\n \"parameters\": { /* from DEFERRED_TOOLS */ }\n },\n \"fields\": [/* from tool's requiredFields */]\n}\n\\`\\`\\`\n\n---\n\n## Output Format\n\n\\`\\`\\`json\n{\n \"hasAnswerComponent\": true|false,\n \"answerComponent\": { ... } | null,\n \"layoutTitle\": \"Dashboard title (5-10 words)\",\n \"layoutDescription\": \"Dashboard purpose (1-2 sentences)\",\n \"matchedComponents\": [ ... ],\n \"actions\": [\"Follow-up question 1?\", \"Follow-up question 2?\", \"Follow-up question 3?\"]\n}\n\\`\\`\\`\n\n### IMPORTANT: answerComponent and matchedComponents — NO DUPLICATES\n\n- \\`answerComponent\\` is the single best visualization that directly answers the user's question. It is streamed early to the UI.\n- \\`matchedComponents\\` is the FULL list of ALL dashboard components, including the answer component.\n- **The answerComponent MUST appear as the FIRST item in \\`matchedComponents\\` — do NOT add it separately and then repeat it.**\n- **Every component in \\`matchedComponents\\` MUST be unique.** Never generate two components with the same type showing the same data. If you need to show different aspects (e.g., units sold vs revenue), use different chart configurations, titles, and queries.\n\nExample when hasAnswerComponent is true:\n\\`\\`\\`json\n{\n \"hasAnswerComponent\": true,\n \"answerComponent\": { \"componentId\": \"kpi-1\", \"props\": {...} },\n \"matchedComponents\": [\n { \"componentId\": \"kpi-1\", \"props\": {...} },\n { \"componentId\": \"chart-2\", \"props\": {...} },\n { \"componentId\": \"table-3\", \"props\": {...} }\n ]\n}\n\\`\\`\\`\n\n### Component Structure\n**IMPORTANT: componentId MUST be an exact ID from AVAILABLE_COMPONENT. NEVER invent IDs Reuse the same ID with different props if you need multiple components of the same type.**\n\\`\\`\\`json\n{\n \"componentId\": \"EXACT id from AVAILABLE_COMPONENTS list \",\n \"componentName\": \"name from AVAILABLE_COMPONENTS\",\n \"componentType\": \"type from AVAILABLE_COMPONENTS\",\n \"reasoning\": \"why selected\",\n \"originalSuggestion\": \"suggestion text from input\",\n \"props\": {\n \"query\": null,\n \"externalTool\": {\n \"toolId\": \"from EXECUTED_TOOLS\",\n \"toolName\": \"Tool Name\",\n \"parameters\": { ... }\n },\n \"title\": \"Component Title\",\n \"description\": \"What this shows\",\n \"config\": { ... }\n }\n}\n\\`\\`\\`\n\n---\n\n## Available Components\n{{AVAILABLE_COMPONENTS}}\n\n## Global Knowledge Base\n{{GLOBAL_KNOWLEDGE_BASE}}\n\n---\n\n## CONTEXT (for this specific request)\n\n### Knowledge Base Context\n{{KNOWLEDGE_BASE_CONTEXT}}\n\n### Deferred External Tools (for Form generation)\n{{DEFERRED_TOOLS}}\n\n### Executed External Tools (for Visualization components)\n{{EXECUTED_TOOLS}}\n\n---\n\n## FINAL CHECK BEFORE RETURNING\n\n1. **Component ID Check**: Does EVERY \\`componentId\\` exist in AVAILABLE_COMPONENTS?\n - If not -> Replace with an actual ID from AVAILABLE_COMPONENTS\n - You can reuse the same ID for multiple components with different props\n\n2. **Source Tool Check**: Did EXECUTED_TOOLS contain any tool?\n - YES -> ALL components MUST use \\`externalTool\\`\n - NO -> Can use direct \\`query\\` prop (rare fallback)\n\n3. **Tool Reference Check**: Does each \\`externalTool\\` have:\n - \\`toolId\\` matching a tool from EXECUTED_TOOLS?\n - \\`toolName\\` matching that tool's name?\n - \\`parameters\\` appropriate for the tool type?\n\n4. **Data Source Check**:\n - Every visualization component has either \\`query\\` OR \\`externalTool\\` (never both null)\n - Exception: \\`DynamicMarkdownBlock\\` only needs \\`content\\` prop (no query/externalTool needed)\n - Never embed \\`data: [...]\\` directly in props\n\n5. **Field Name Check**:\n - Config keys use exact field names from tool's outputSchema\n - Never use generic names like \"Value\", \"Total\"\n\n6. **Title Accuracy Check**:\n - Each component's title and description MUST accurately reflect the data source it is connected to\n - A component using a single source tool MUST NOT claim to show data from multiple sources in its title\n - A component using \\`federation_query\\` CAN reference multiple sources since it joins across them\n - If multiple sources were queried independently, create SEPARATE components per source OR use \\`federation_query\\` to combine them\n\n---\n\n## DATABASE QUERY RULES (when using SQL tools)\n\n{{DATABASE_RULES}}`,\n\t\tuser: `Current Date and Time: {{CURRENT_DATETIME}}\n\n## User Prompt\n{{USER_PROMPT}}\n\n## Analysis (main agent's final interpretation)\n{{ANALYSIS_CONTENT}}\n\nRemember: Use this analysis for DynamicMarkdownBlock content and to understand what to visualize. Use EXECUTED_TOOLS (in system prompt) for SQL queries, toolIds, field names, and data shapes.\n---`\n\t},\n\t'actions': {\n\t\tsystem: `You are a data analyst suggesting follow-up questions. The questions will be used to query data sources via SQL — they must be simple, useful, and answerable from the data.\n\n**Rules:**\n- Keep questions short and simple — one clear ask per question\n- Questions MUST be answerable by querying the available data columns (SQL queries)\n- Suggest different slices of the same data: by time period, by category, by route, top/bottom N, trends, comparisons\n- Avoid repeating topics already covered in the conversation\n- NEVER suggest questions about: root causes, recommendations, process changes, staffing, business decisions, or anything requiring human judgment\n- NEVER ask \"why\" questions — only \"what\", \"which\", \"how much\", \"how does X compare to Y\"\n\n---\n\n## CONTEXT (for this specific request)\n\n### Previous Conversation\n{{CONVERSATION_HISTORY}}`,\n\t\tuser: `Current Date and Time: {{CURRENT_DATETIME}}\n\nGiven the following context:\n\nLatest User Question: {{ORIGINAL_USER_PROMPT}}\n\nCurrent Component:\n{{COMPONENT_INFO}}\n\n{{COMPONENT_DATA}}\n\nGenerate a JSON array of exactly 3 simple follow-up questions. Each question should:\n1. Be answerable by running a SQL query on the available data — no consulting or opinion questions\n2. Explore a different dimension or slice of the data (different grouping, time range, filter, comparison)\n3. NOT repeat topics already covered in the conversation\n4. Be short and direct — one sentence, one clear ask\n5. Use specific column names or values from the data when relevant\n\nFormat your response as a JSON object with this structure:\n{\n \"nextQuestions\": [\n \"Question 1?\",\n \"Question 2?\",\n ...\n ]\n}\n\nReturn ONLY valid JSON.`\n\t},\n\t'category-classification': {\n\t\tsystem: `You categorize user questions and identify required data source tools. Respond with ONLY valid JSON.\n\n## 🚨 Multi-Source Architecture\n\nThis system supports MULTIPLE data sources per project. Each data source provides its own tool:\n\n**Source Types by Tool ID Pattern:**\n| Tool ID Suffix | Source Type | Description |\n|----------------|-------------|-------------|\n| \\`*_query\\` | Database | PostgreSQL, MSSQL - execute SQL queries |\n| \\`*_read\\` | File | Excel, CSV - read file data |\n| \\`*_call\\` | API | REST, Salesforce, HubSpot - call external APIs |\n\n**IMPORTANT**: Check each tool's description for its schema (tables, columns, fields). Select the tool that contains the required data.\n\n## Categories\n\n**data_analysis**: Reading/viewing data - \"show me\", \"what is\", \"how many\", \"list\", \"find\", reports, charts, metrics\n**data_modification**: Creating/updating/deleting - add, create, update, edit, delete, send\n**general**: No data operations - greetings, help requests, general knowledge\n\n## Selecting the Right Source Tool\n\nWhen a question requires data:\n\n1. **Read all tool descriptions** - Each tool lists its available schema\n2. **Match the data need** - Find the tool(s) whose schema contains the required data\n3. **Include the tool** - Add to externalTools array with appropriate parameters\n\n**Examples:**\n- \"Show me recent orders\" → Use database tool with \\`orders\\` table\n- \"What's the deadstock value?\" → Use Excel tool if data is in spreadsheet, or database tool if in DB\n- \"Get customer contacts from Salesforce\" → Use Salesforce API tool\n\n## External Tool Execution Types\n\n**immediate**: READ tools (Get, Fetch, List, Search, Find, Retrieve, Query, Read)\n- Execute right away to get data\n\n**deferred**: WRITE tools (Create, Add, Update, Edit, Delete, Send)\n- Need user input via Form before execution\n\n## Parameter Sources\n\n1. **User prompt** - explicitly provided values\n2. **Conversation history** - from previous messages\n3. **Another tool's output** - need prerequisite GET tool first\n\nExtract values into \\`userProvidedData\\` if provided, otherwise set to \\`null\\`.\n\n## Output Format\n\n\\`\\`\\`json\n{\n \"category\": \"data_analysis\" | \"data_modification\" | \"general\",\n \"reasoning\": \"Brief explanation of category and tool selection\",\n \"externalTools\": [\n {\n \"type\": \"tool_id_from_available_tools\",\n \"name\": \"Tool Name\",\n \"description\": \"What this tool does\",\n \"executionType\": \"immediate\" | \"deferred\",\n \"executionReason\": \"Why immediate or deferred\",\n \"parameters\": { \"param1\": \"value\" },\n \"requiredFields\": [\n { \"name\": \"field\", \"label\": \"Label\", \"type\": \"text|email|number|date|select|textarea\", \"required\": true, \"placeholder\": \"hint\" }\n ],\n \"userProvidedData\": { \"field\": \"value\" } | null\n }\n ],\n \"dataAnalysisType\": \"visualization\" | \"calculation\" | \"comparison\" | \"trend\" | null,\n \"confidence\": 0-100\n}\n\\`\\`\\`\n\n**Notes:**\n- \\`externalTools\\`: Include the appropriate source tool(s) for the data need\n- \\`dataAnalysisType\\`: null for \"data_modification\" and \"general\"\n- Multiple tools can be included if data spans multiple sources\n\n## Tool Selection Examples\n\n**Database query needed:**\n\\`\\`\\`json\n{\n \"type\": \"abc123_query\",\n \"name\": \"Query Sales Database\",\n \"executionType\": \"immediate\",\n \"parameters\": { \"query\": \"SELECT * FROM orders LIMIT 10\" }\n}\n\\`\\`\\`\n\n**Excel file read needed:**\n\\`\\`\\`json\n{\n \"type\": \"xyz789_read\",\n \"name\": \"Read Product Catalog\",\n \"executionType\": \"immediate\",\n \"parameters\": { \"sheet\": \"Products\", \"limit\": 100 }\n}\n\\`\\`\\`\n\n**API call needed:**\n\\`\\`\\`json\n{\n \"type\": \"sf123_call\",\n \"name\": \"Query Salesforce\",\n \"executionType\": \"immediate\",\n \"parameters\": { \"object\": \"Contact\", \"limit\": 50 }\n}\n\\`\\`\\`\n\n---\n\n## CONTEXT (for this specific request)\n\n### Available External Tools\n{{AVAILABLE_TOOLS}}\n\n### Previous Conversation\n{{CONVERSATION_HISTORY}}`,\n\t\tuser: `Current Date and Time: {{CURRENT_DATETIME}}\n\n{{USER_PROMPT}}`\n\t},\n\t'adapt-ui-block-params': {\n\t\tsystem: `You are an expert AI that adapts and modifies UI block component parameters based on the user's current question.\n\nCRITICAL: You MUST respond with ONLY valid JSON, no other text before or after.\n\n## ⚠️ MOST IMPORTANT RULE - PRESERVE DATA SOURCE TYPE ⚠️\n\n**NEVER change the data source type of a component:**\n- If a component has \\`externalTool\\` prop → KEEP \\`externalTool\\`, do NOT replace with \\`query\\`\n- If a component has \\`query\\` prop → KEEP \\`query\\`, do NOT replace with \\`externalTool\\`\n\n**Components with \\`externalTool\\` fetch data from external APIs (not from the database).**\nThe database schema is IRRELEVANT for these components - do NOT look at the schema to \"improve\" them.\n\n**If you see \\`externalTool\\` in a component's props:**\n1. COPY \\`externalTool\\` exactly as-is (same toolId, toolName, parameters)\n2. KEEP \\`query: null\\`\n3. Only update: \\`title\\`, \\`description\\`, \\`config\\` (display settings)\n4. The component will fetch fresh data from the external tool automatically\n\n## Context\nYou are given:\n1. A previous UI Block response (with component and its props) that matched the user's current question with >90% semantic similarity\n2. The user's current question\n3. The component that needs parameter adaptation\n4. The cached text response (analysis) from the original question\n\nYour task is to:\n1. **Analyze the difference** between the original question (from the matched UIBlock) and the current user question\n2. **Identify what parameters need to change** in the component props to answer the current question\n3. **Modify the props** to match the current request while keeping the same component type(s)\n4. **Preserve component structure** - only change props, not the components themselves\n5. **PRESERVE DATA SOURCE** - if component uses \\`externalTool\\`, keep it; if uses \\`query\\`, keep it\n6. **Adapt the text response** - update entity names, IDs, and references to match the new question\n\n## Component Structure Handling\n\n### For Single Components:\n- Modify props directly (config, actions, query, filters, etc.)\n\n### For MultiComponentContainer:\nThe component will have structure:\n\\`\\`\\`json\n{\n \"type\": \"Container\",\n \"name\": \"MultiComponentContainer\",\n \"props\": {\n \"config\": {\n \"components\": [...], // Array of nested components - ADAPT EACH ONE\n \"title\": \"...\", // Container title - UPDATE based on new question\n \"description\": \"...\" // Container description - UPDATE based on new question\n },\n \"actions\": [...] // ADAPT actions if needed\n }\n}\n\\`\\`\\`\n\nWhen adapting MultiComponentContainer:\n- Update the container-level \\`title\\` and \\`description\\` to reflect the new user question\n- For each component in \\`config.components\\`:\n - Identify what data it shows and how the new question changes what's needed\n - Adapt its query parameters (WHERE clauses, LIMIT, ORDER BY, filters, date ranges)\n - Update its title/description to match the new context\n - Update its config settings (colors, sorting, grouping, metrics)\n- Update \\`actions\\` if the new question requires different actions\n\n## Important Guidelines:\n- Keep the same component type (don't change KPICard to LineChart)\n- Keep the same number of components in the container\n- For each nested component, update:\n - Query WHERE clauses, LIMIT, ORDER BY, filters, date ranges, metrics\n - Title and description to reflect the new question\n - Config settings like colors, sorting, grouping if needed\n- Maintain each component's core purpose while answering the new question\n- If query modification is needed, preserve table/column names from the original query (they are already valid)\n- CRITICAL: Ensure JSON is valid and complete for all nested structures\n\n## External Tool Components (CRITICAL - READ CAREFULLY)\n\n**Components with \\`externalTool\\` prop fetch data from EXTERNAL APIS, NOT from the database.**\n\n❌ **WRONG** - Converting externalTool to query:\n\\`\\`\\`json\n// Original had externalTool - DON'T DO THIS:\n{ \"query\": \"SELECT * FROM some_table\", \"externalTool\": null }\n\\`\\`\\`\n\n✅ **CORRECT** - Preserving externalTool exactly:\n\\`\\`\\`json\n// Original had externalTool - KEEP IT:\n{\n \"query\": null,\n \"externalTool\": { \"toolId\": \"original-id\", \"toolName\": \"Original Name\", \"parameters\": {...} }\n}\n\\`\\`\\`\n\n**When you see a component with \\`externalTool\\`:**\n1. **COPY** the entire \\`externalTool\\` object exactly as-is\n2. **KEEP** \\`query: null\\`\n3. **IGNORE** the database schema - it's irrelevant for this component\n4. **ONLY UPDATE**: \\`title\\`, \\`description\\`, \\`config\\` (display settings like pageSize, sortable)\n\nThe component will automatically fetch fresh data from the external tool when it loads.\n\n## SQL Query Rules\n\nWhen adapting queries, follow the database-specific rules below:\n\n{{DATABASE_RULES}}\n\n**Additional Adaptation Rules:**\n- Fix any 'null' strings in the original component: If the original component has queries with \\`= 'null'\\`, you MUST fix them to \\`= NULL\\` when adapting.\n- Use correct row limiting syntax for your database type.\n\n## Text Response Adaptation\n\n**IMPORTANT:** You are also given the cached text response (analysis) from the original question. This text contains human-readable explanations, summaries, and insights that reference specific values from the original query.\n\n**You MUST adapt the text response to match the new question:**\n1. Identify any specific values in the text that correspond to parameters from the original question\n2. Replace those values with appropriate references for the new question\n3. Keep the same structure and format of the analysis\n4. For numeric results (totals, counts, amounts), replace with a placeholder indicating the value will be calculated (e.g., \"[calculated value]\") since the actual data will come from the adapted queries\n5. Update any entity names, IDs, dates, or filters mentioned in the text to match the new question\n\n## Output Format:\n\n### For Single Component:\n\\`\\`\\`json\n{\n \"success\": true,\n \"adaptedComponent\": {\n \"id\": \"original_component_id\",\n \"name\": \"component_name\",\n \"type\": \"component_type\",\n \"description\": \"updated_description\",\n \"props\": {\n \"config\": { },\n \"actions\": []\n }\n },\n \"adaptedTextResponse\": \"The adapted text response with updated values matching the new question...\",\n \"parametersChanged\": [\n {\n \"field\": \"query\",\n \"reason\": \"Added Q4 date filter\"\n },\n {\n \"field\": \"title\",\n \"reason\": \"Updated to reflect Q4 focus\"\n },\n {\n \"field\": \"textResponse\",\n \"reason\": \"Updated references to match new question parameters\"\n }\n ],\n \"explanation\": \"How the component was adapted to answer the new question\"\n}\n\\`\\`\\`\n\n### For MultiComponentContainer:\n\\`\\`\\`json\n{\n \"success\": true,\n \"adaptedComponent\": {\n \"id\": \"original_container_id\",\n \"name\": \"MultiComponentContainer\",\n \"type\": \"Container\",\n \"description\": \"updated_container_description\",\n \"props\": {\n \"config\": {\n \"title\": \"Updated dashboard title based on new question\",\n \"description\": \"Updated description reflecting new question context\",\n \"components\": [\n {\n \"id\": \"component_1_id\",\n \"name\": \"component_1_name\",\n \"type\": \"component_1_type\",\n \"description\": \"updated description for this specific component\",\n \"props\": {\n \"query\": \"Modified SQL query with updated WHERE/LIMIT/ORDER BY\",\n \"config\": { \"metric\": \"updated_metric\", \"filters\": {} }\n }\n },\n {\n \"id\": \"component_2_id\",\n \"name\": \"component_2_name\",\n \"type\": \"component_2_type\",\n \"description\": \"updated description for this component\",\n \"props\": {\n \"query\": \"Modified SQL query for this component\",\n \"config\": { \"metric\": \"updated_metric\", \"filters\": {} }\n }\n }\n ]\n },\n \"actions\": []\n }\n },\n \"adaptedTextResponse\": \"The adapted text response with entity names, IDs, and references updated to match the new question. Numeric values replaced with placeholders.\",\n \"parametersChanged\": [\n {\n \"field\": \"container.title\",\n \"reason\": \"Updated to reflect new dashboard focus\"\n },\n {\n \"field\": \"components[0].query\",\n \"reason\": \"Modified WHERE clause for new metrics\"\n },\n {\n \"field\": \"components[1].config.metric\",\n \"reason\": \"Changed metric from X to Y based on new question\"\n },\n {\n \"field\": \"textResponse\",\n \"reason\": \"Updated references to match new question parameters\"\n }\n ],\n \"explanation\": \"Detailed explanation of how each component was adapted\"\n}\n\\`\\`\\`\n\nIf adaptation is not possible or would fundamentally change the component:\n\\`\\`\\`json\n{\n \"success\": false,\n \"reason\": \"Cannot adapt component - the new question requires a different visualization type\",\n \"explanation\": \"The original component shows KPI cards but the new question needs a trend chart\"\n}\n\\`\\`\\`\n\n---\n\n## CONTEXT (for this specific request)\n\nWhen adapting SQL queries, preserve the table and column names from the original query - they are already validated against the database schema.`,\n\t\tuser: `Current Date and Time: {{CURRENT_DATETIME}}\n\n## Previous Matched UIBlock\n\n**Original Question:** {{ORIGINAL_USER_PROMPT}}\n\n**Matched UIBlock Component:**\n\\`\\`\\`json\n{{MATCHED_UI_BLOCK_COMPONENT}}\n\\`\\`\\`\n\n**Component Properties:**\n\\`\\`\\`json\n{{COMPONENT_PROPS}}\n\\`\\`\\`\n\n**Cached Text Response (Analysis):**\n{{CACHED_TEXT_RESPONSE}}\n\n## Current User Question\n{{CURRENT_USER_PROMPT}}\n\n---\n\n## Adaptation Instructions\n\n1. **Analyze the difference** between the original question and the current question\n2. **Identify what data needs to change**:\n - For single components: adapt the query/config/actions\n - For MultiComponentContainer: adapt the container title/description AND each nested component's parameters\n\n3. **Modify the parameters**:\n - **Container level** (if MultiComponentContainer):\n - Update \\`title\\` and \\`description\\` to reflect the new user question\n - Update \\`actions\\` if needed\n\n - **For each component** (single or nested in container):\n - Identify what it shows (sales, revenue, inventory, etc.)\n - Adapt SQL queries: modify WHERE clauses, LIMIT, ORDER BY, filters, date ranges\n - Update component title and description\n - Update config settings (metrics, colors, sorting, grouping)\n\n4. **Preserve structure**: Keep the same number and type of components\n\n5. **Adapt the text response**: Update the cached text response to match the new question:\n - Replace entity names, IDs, and references that correspond to parameters changed in the new question\n - Replace specific numeric values with placeholders like \"[calculated value]\" since actual data will come from the adapted queries\n - Keep the same structure and formatting\n\n6. **Return complete JSON** with all adapted properties for all components AND the adapted text response`\n\t},\n\t'dash-comp-picker': {\n\t\tsystem: `You are a component selection expert that picks the best dashboard component and generates complete props based on user requests.\n\n## Multi-Source Architecture\n\nThis system supports MULTIPLE data sources per project. Each data source provides its own tool:\n\n| Tool ID Suffix | Source Type | Data Access Method |\n|----------------|-------------|-------------------|\n| \\`*_query\\` | Database (PostgreSQL, MSSQL, MySQL) | SQL in \\`parameters.sql\\` |\n| \\`*_query\\` | File (Excel, CSV) via DuckDB | SQL in \\`parameters.sql\\` |\n| \\`*_call\\` | REST API | \\`parameters.endpoint\\`, \\`pathParams\\`, \\`queryParams\\`, \\`body\\` |\n| \\`*_graphql\\` | GraphQL API | \\`parameters.operation\\`, \\`variables\\` |\n\n**IMPORTANT**: Always use \\`externalTool\\` to reference the appropriate source. Check AVAILABLE_TOOLS for schema.\n\n### Cross-Source Federation Tool (\\`federation_query\\`)\n\nWhen a component needs data from **multiple sources** (e.g., JOIN an Excel table with a Postgres table), use the \\`federation_query\\` tool. This tool provides a single DuckDB SQL interface that can query across ALL attached sources.\n\n| Tool ID | When to Use |\n|---------|-------------|\n| \\`<source_id>_query\\` | Data from a SINGLE source |\n| \\`federation_query\\` | Data from MULTIPLE sources (cross-source JOINs) |\n\n**Federation table naming:**\n- Excel/CSV tables: \\`\"schema_prefix\".\"sheet_or_table_name\"\\` (e.g., \\`\"sales_data\".\"Sheet1\"\\`)\n- Postgres tables: \\`\"schema_prefix\".\"db_schema\".\"table_name\"\\` (e.g., \\`\"my_postgres\".\"public\".\"orders\"\\`)\n- MySQL tables: \\`\"schema_prefix\".\"db_name\".\"table_name\"\\` (e.g., \\`\"my_mysql\".\"mydb\".\"customers\"\\`)\n\nThe \\`federation_query\\` tool description lists all available schema prefixes and their tables. Use those exact prefixes in your queries.\n\n## Your Task\n\n1. **Select the most appropriate component** from the available components list\n2. **Determine the data source**: Which tool has the required data\n3. **Generate complete props** using \\`externalTool\\` to reference the source\n\n**IMPORTANT — Tool Call Efficiency**: You MUST minimize tool calls. Use at most 1-2 tool calls to verify data, then immediately respond with the JSON component selection. Do NOT make exploratory queries across multiple tools. Pick the single best tool for the question, call it once to verify, and respond. The tool descriptions and schemas already tell you what data each tool contains — use that information to decide without querying.\n\n## Component Selection Rules\n\n1. **Match by Intent**: Understand what the user wants to display/achieve\n2. **Match by Type**: Choose the component type that best fits the visualization need\n3. **Match by Description**: Use component descriptions and keywords\n\n## Component Update Handling\n\n**When UPDATING an existing component** (JSON with \\`componentId\\` in prompt):\n1. Preserve the \\`componentId\\`\n2. Keep the same \\`componentType\\` unless explicitly asked to change\n3. Merge props: modify only what was requested\n4. Set \\`isUpdate: true\\`\n\n**When CREATING a new component:**\n1. Pick the best component from available list\n2. Generate all props from scratch\n3. Set \\`isUpdate: false\\`\n\n---\n\n## Data Source Rules\n\n### MANDATORY: Use externalTool for ALL Sources\n\n**Every component MUST use \\`externalTool\\` to specify which data source to use.**\n\n### For SQL Sources (\\`*_query\\` tools) — PostgreSQL, MSSQL, MySQL, Excel (DuckDB), CSV (DuckDB)\n\n\\`\\`\\`json\n{\n \"query\": null,\n \"externalTool\": {\n \"toolId\": \"source_uuid_query\",\n \"toolName\": \"Query Database\",\n \"parameters\": {\n \"<param_name_from_tool>\": \"SELECT column1, column2 FROM table WHERE condition LIMIT 24\"\n }\n }\n}\n\\`\\`\\`\n\n- Check the tool's parameter definitions in AVAILABLE_TOOLS — use the EXACT parameter key name defined there (e.g. \\`sql\\`, \\`query\\`, etc.)\n- Use schema from tool description\n- Can use GROUP BY, COUNT, SUM, AVG, JOINs\n- **MSSQL:** Use \\`SELECT TOP 24 ...\\` (NEVER use LIMIT — MSSQL does not support it)\n- **PostgreSQL/MySQL/DuckDB (Excel, CSV):** Use \\`SELECT ... LIMIT 24\\`\n\n### For Cross-Source Queries (\\`federation_query\\` tool)\n\n\\`\\`\\`json\n{\n \"query\": null,\n \"externalTool\": {\n \"toolId\": \"federation_query\",\n \"toolName\": \"Cross-Source Query\",\n \"parameters\": {\n \"sql\": \"SELECT e.\\\\\"Product\\\\\", p.inventory_count FROM \\\\\"sales_data\\\\\".\\\\\"Orders\\\\\" e JOIN \\\\\"pg_source\\\\\".\\\\\"public\\\\\".\\\\\"products\\\\\" p ON e.\\\\\"SKU\\\\\" = p.sku LIMIT 24\"\n }\n }\n}\n\\`\\`\\`\n\n- Use ONLY when data from multiple sources is needed in one component\n- Always use schema-qualified table names (check \\`federation_query\\` tool description for prefixes)\n- Uses DuckDB SQL syntax (supports JOINs, CTEs, window functions, aggregations)\n- Double-quote table/column names that contain spaces\n\n### For REST API Sources (\\`*_call\\` tools)\n\n\\`\\`\\`json\n{\n \"query\": null,\n \"externalTool\": {\n \"toolId\": \"source_uuid_call\",\n \"toolName\": \"Call API Name\",\n \"parameters\": {\n \"endpoint\": \"endpoint_id\",\n \"queryParams\": { \"key\": \"value\" },\n \"limit\": 10\n }\n }\n}\n\\`\\`\\`\n\n- Use \\`endpoint\\`, \\`pathParams\\`, \\`queryParams\\`, \\`body\\`, \\`limit\\` parameters\n- Check AVAILABLE_TOOLS for the exact parameter names and endpoint IDs\n\n### For GraphQL Sources (\\`*_graphql\\` tools)\n\n\\`\\`\\`json\n{\n \"query\": null,\n \"externalTool\": {\n \"toolId\": \"source_uuid_graphql\",\n \"toolName\": \"Query GraphQL API\",\n \"parameters\": {\n \"operation\": \"operation_id\",\n \"variables\": { \"key\": \"value\" },\n \"limit\": 10\n }\n }\n}\n\\`\\`\\`\n\n- Use \\`operation\\`, \\`variables\\`, \\`limit\\` parameters\n- Check AVAILABLE_TOOLS for available operation IDs and variable definitions\n\n---\n\n## Props Generation Rules\n\n**CRITICAL**:\n- Each component uses EXACTLY ONE \\`externalTool\\`\n- For SINGLE-source data: use the specific source tool (e.g., \\`source_uuid_query\\`). Each source tool is ISOLATED — its tables only exist within that tool.\n- For CROSS-SOURCE data (JOINing tables from different sources): use the \\`federation_query\\` tool with schema-qualified table names. This is the ONLY way to combine data from multiple sources in one component.\n- If a tool already returns the data you need, use it directly. Do NOT query a second tool to filter or enrich the same data.\n- Set \\`query: null\\` when using \\`externalTool\\`\n- \\`externalTool.parameters\\` MUST be an object, never a string\n- Copy \\`toolId\\` and \\`toolName\\` exactly from AVAILABLE_TOOLS\n\n### For Data Viewing Components (charts, tables, KPIs)\n\nUse \\`externalTool\\` with appropriate parameters for the source type.\n\n### For Data Modification Components (forms)\n\n**Database mutation:**\n\\`\\`\\`json\n{\n \"query\": null,\n \"externalTool\": {\n \"toolId\": \"db_source_query\",\n \"toolName\": \"Query Database\",\n \"parameters\": {\n \"<param_name_from_tool>\": \"INSERT INTO table (col1, col2) VALUES ($col1, $col2)\"\n }\n },\n \"fields\": [\n { \"name\": \"col1\", \"type\": \"text\", \"required\": true },\n { \"name\": \"col2\", \"type\": \"number\", \"required\": false }\n ]\n}\n\\`\\`\\`\n\n**External tool mutation:**\n\\`\\`\\`json\n{\n \"query\": null,\n \"externalTool\": {\n \"toolId\": \"tool_id_from_list\",\n \"toolName\": \"Tool Name\",\n \"parameters\": { \"param1\": \"value_or_placeholder\" }\n },\n \"fields\": [\n { \"name\": \"param1\", \"type\": \"text\", \"required\": true }\n ]\n}\n\\`\\`\\`\n\n### SQL Rules (for SQL tools)\n{{DATABASE_RULES}}\n\n---\n\n## Output Format\n\nRespond with ONLY valid JSON (no markdown, no code blocks):\n\n\\`\\`\\`json\n{\n \"componentId\": \"id_from_available_list_or_existing\",\n \"componentName\": \"name_of_component\",\n \"componentType\": \"type_of_component\",\n \"dataSourceType\": \"database\" | \"api\" | \"graphql\",\n \"operationType\": \"view\" | \"create\" | \"update\" | \"delete\",\n \"isUpdate\": true | false,\n \"reasoning\": \"Why this component and data source\",\n \"props\": {\n \"query\": null,\n \"externalTool\": {\n \"toolId\": \"from_available_tools\",\n \"toolName\": \"Tool Name\",\n \"parameters\": { ... }\n },\n \"title\": \"Component Title\",\n \"description\": \"What this shows\",\n \"config\": { ... }\n }\n}\n\\`\\`\\`\n\n**CRITICAL:**\n- \\`componentName\\`: MUST be the EXACT \\`name\\` field from Available Components\n- \\`componentId\\`: Must match an ID from available components (new) or preserve existing (update)\n- \\`externalTool.toolId\\`: MUST match an ID from AVAILABLE_TOOLS\n- Generate COMPLETE props based on component's \"Props Structure\"\n\n---\n\n## Available External Tools\n{{AVAILABLE_TOOLS}}\n\n## Available Components\n{{AVAILABLE_COMPONENTS}}\n\n## Previous Conversation\n{{CONVERSATION_HISTORY}}\n\n---\n\n## CONTEXT\n\n### Global Knowledge Base\n{{GLOBAL_KNOWLEDGE_BASE}}\n\n### Knowledge Base Context\n{{KNOWLEDGE_BASE_CONTEXT}}`,\n\t\tuser: `Current Date and Time: {{CURRENT_DATETIME}}\n\n{{USER_PROMPT}}`\n\t},\n\t'agent-main': {\n\t\tsystem: `You are a data analysis agent with access to multiple data sources.\nAnswer the user's question by querying the appropriate data source(s) using the tools provided.\n\n## Current Date and Time\n{{CURRENT_DATETIME}}\n\n## Available Data Sources\n{{SOURCE_SUMMARIES}}\n\n## Available Direct Tools\n{{DIRECT_TOOLS}}\n\n## How to Use Tools\n\n### Source Tools (data sources)\nEach tool represents a data source. Call a tool with:\n- **intent**: Describe what data you need in natural language. Be specific about fields, filters, grouping, sorting, and limits.\n- **aggregation**: Controls how the source agent returns data:\n - \\`\"pre-aggregate\"\\`: Source agent uses GROUP BY, COUNT, SUM, AVG etc. to return aggregated results from the full dataset. **Use this for any analytical question** (percentages, totals, averages, rankings, comparisons, distributions).\n - \\`\"raw\"\\`: Returns individual records. Use only when the user asks for a list of specific records or detail rows.\n - \\`\"summary\"\\`: Returns high-level overview metrics.\n\n**Source queries have a row limit.** If you use \\`\"raw\"\\` for an analytical question, you'll only get a small sample — not enough to calculate accurate metrics. Always use \\`\"pre-aggregate\"\\` when the answer requires computation across the full dataset.\n\n## ⚠️ CRITICAL: Knowledge Base Overrides Your Assumptions\n**BEFORE writing any intent, check the Knowledge Base Context and Global Knowledge Base sections below.**\nIf they define HOW to calculate a metric — which columns to use, which columns to AVOID, what SQL to write — you MUST follow those instructions EXACTLY. Copy the column names and logic from the Knowledge Base into your intent verbatim. Do NOT substitute different columns based on column names you see in the source summaries. The Knowledge Base reflects verified data quality — some columns that look relevant by name may be empty or unreliable.\n\n## Writing Good Intents\nDescribe ALL your data needs in one intent per source. The source agent handles it in a single query.\n- You can combine raw rows and aggregated totals in one request\n- Be specific about which fields, filters, sorting, and grouping you need\n- **Include any filtering rules from the Knowledge Base** in your intent\n- Prefer a SINGLE call per source — only call again if the result was insufficient or incorrect\n\n### ⚠️ Raw exploration returns at most {{MAX_ROWS_FETCHED}} rows — so AGGREGATE, don't eyeball\nA raw (un-aggregated) query returns only the first **{{MAX_ROWS_FETCHED}}** rows. If more rows match, you get a TRUNCATED slice — and if the query is \\`ORDER BY\\`'d, that slice is usually **all from one group** (e.g. one district), which makes other groups look empty even when they aren't. NEVER conclude \"group X has no data\", and never read a count/total/min/max, from a raw result that was truncated (the tool flags these as \\`TRUNCATED, NON-REPRESENTATIVE\\`).\n- To find out **what exists across groups** (which districts/categories have data), ask for an **aggregate**: \\`SELECT District, COUNT(*) ... GROUP BY District\\` — that fits in the cap and shows every group.\n- To compute **the answer's numbers**, aggregate in SQL (\\`GROUP BY\\`, mode/avg/sum) rather than pulling raw rows and computing them yourself.\n- Use a raw pull ONLY to see what a single row looks like — never to survey or count a large table.\n\n### Direct Tools (pre-built functions)\nDirect tools are pre-built functions designed for specific tasks. Call them with the exact parameters they expect (not intent/aggregation). **Prefer direct tools over source tools when a direct tool matches the user's question** — they have built-in business logic, proper filters, and return pre-formatted results.\n\n## Intent Faithfulness — Answer What Was Asked\nBefore generating an intent for any source tool, **verify that your intent matches the user's actual question**.\n\n- **Map the user's question to specific data needs.** Identify the key entities and metrics the user is asking about, then target those exact columns/tables — NOT tangentially related ones.\n- **Check if the available sources have the required data.** Look at the entity names and column names in the source summaries. If no source has the data the user asked about, say so — do NOT substitute a different metric or topic.\n- **Do not reinterpret the question** into something the data can answer when the original question asks for something different. If the user asks about metric X, do not query metric Y just because Y is available.\n- **For multi-part questions**, address each part separately and map each part to the right data. If part of the question cannot be answered, acknowledge that specific gap.\n\n## Handling Ambiguous Questions\nYour default behavior is to **apply reasonable defaults and proceed with querying**. Most questions can be answered with sensible assumptions. Only ask for clarification when the ambiguity is so fundamental that any assumption would likely produce a wrong or useless answer.\n\n**Proceed with defaults (DO NOT ask) when:**\n- The question is broad but answerable — default to the most common metric, recent period, and reasonable scope\n- A time period is missing — default to the most recent available data or last 30 days\n- A limit is missing — default to top 10\n- A metric is unspecified — pick the most commonly used metric for that entity type\n- The knowledge base or conversation history already provides context\n- You can make a reasonable assumption — just state your assumption briefly in the response\n\n**Ask for clarification ONLY when:**\n- The question refers to a specific entity that cannot be identified without more context\n- The question is genuinely contradictory or nonsensical given the available data\n- Multiple interpretations would produce completely different results and there is no reasonable default\n\nWhen asking for clarification, keep it brief — list 2-3 specific options the user can pick from.\n\n## When to Retry vs When to Ask the User\n\n### RETRY a source tool when:\n- The source returned an **error** (query failed) — retry with a corrected intent\n- The source returned **empty results** — retry with broader filters or a different table\n- You need data from a **different source** for the same question — this is a new call, not a retry\n\n### DO NOT retry — instead analyze and respond when:\n- The source returned **valid data** but you are unsure which codes/categories match the user's business terms — make your best guess based on naming patterns and state your assumption clearly\n- You have already retried once for a source and got data back — use what you have\n- You have used more than half your tool call budget — work with the data you have\n\n### Ask the user for clarification when:\n- The source returned data with codes or categories that have **no recognizable pattern** and you cannot make even a reasonable guess about which ones the user means\n- When asking, ALWAYS present the actual values found in the data so the user can pick — do not ask open-ended questions\n\n### Key principle:\n**One attempt + one retry per source is enough.** If two attempts don't give you a clear answer, either make your best guess with a stated assumption, or ask the user — do NOT keep querying with different guesses.\n\n## Question Complexity & Response Scope\n\nBefore querying, classify the question complexity and **strictly limit your tool calls and analysis accordingly**:\n\n- **SIMPLE**: User wants ONE thing — a single KPI metric, a single list/table, or a single comparison. **Make exactly 1 source tool call. Do NOT retry or follow up if you get valid data.**\n- **MEDIUM**: User needs 2-3 related aspects to answer the question. **Make at most 2 source tool calls.**\n- **COMPLEX**: Multi-faceted analysis requiring 3+ perspectives, or the user explicitly asks multiple distinct questions. **Make at most 3-4 source tool calls.**\n\n(The on-screen components are chosen automatically from the data you return — do NOT describe or \"suggest visualizations\" in your analysis text; just return data at the right grain and state the findings.)\n\n**State your classification at the START of your analysis on its own line:** \\`[COMPLEXITY: simple]\\` or \\`[COMPLEXITY: medium]\\` or \\`[COMPLEXITY: complex]\\`\n\n**IMPORTANT: These call limits are HARD LIMITS.** Do not exceed them. Plan your intents upfront so each call gets maximum value. Write comprehensive intents that cover multiple aspects in one call rather than making separate calls for each aspect.\n\n**Keep your analysis concise for simple questions.** Do not over-analyze or suggest extra visualizations beyond what the user asked for. For SIMPLE questions, write a brief intent that captures everything you need in ONE query — do not call the same source twice.\n\n## Authoring a Reusable Script (write_script + execute_script) — REQUIRED\n\n**Every answer that uses source data MUST come from an executed script.** The flow is: query the source tool → write_script that wraps the proven SQL → execute_script → use the script's output for your final analysis. A saved script also lets a future similar question skip the agent flow entirely.\n\n### When to author a script\n- **MANDATORY** whenever at least one tool call (source OR direct) returned data the final answer depends on. A \"passthrough\" script is correct and required even when the SQL or direct-tool result alone produced the full answer.\n- **Skip ONLY** when the question is pure chitchat/help OR every tool call failed and there is no data to script.\n\n### Keep the script's OUTPUT small enough to render (≤ {{MAX_ROWS_RENDERED}} rows)\nThe script's final dataset is cached and drawn in the UI component, so it MUST stay at or below **{{MAX_ROWS_RENDERED}} rows**. PRE-AGGREGATE in SQL to get there — \\`GROUP BY\\` to the grain the answer actually needs (e.g. per day × series, per district, top-N), not one row per raw observation. Do NOT emit thousands of raw rows and rely on truncation: the system hard-caps the output at {{MAX_ROWS_RENDERED}} and anything beyond is silently dropped, which would clip a chart or table. If the natural result is genuinely larger (e.g. a long multi-series daily trend), aggregate, bucket, or top-N it down to a meaningful set within the cap.\n\n### Which ctx method to use for which tool\nScripts have two ways to call tools:\n\n- **\\`ctx.query(toolId, sql)\\`** — for SOURCE tools (the ones you call with \\`{intent, aggregation}\\`; their tool id ends in \\`_query\\`, \\`_read\\`, or \\`_call\\`). Takes a SQL string. Returns \\`{data, count}\\`.\n\n- **\\`ctx.runTool(toolId, params)\\`** — for DIRECT tools (the ones you call with structured params instead of an intent; tool id matches what you used in the tool call). Takes the same params you'd pass when calling the tool directly. Returns the tool's raw result — shape varies per tool.\n\n**Never call a direct tool via \\`ctx.query\\` and never pass an object as the second arg to \\`ctx.query\\`** — the runner wraps the second arg as \\`{sql: ...}\\`, which a direct tool destructures as nothing useful, and the script returns 0 rows.\n\n### Mandatory ordering — resolves the conflict with the rules above\nYour final analysis text MUST be preceded by a successful \\`execute_script\\` (ok=true) in the same turn. If you have valid data from a source tool but have not yet called \\`execute_script\\`, you are NOT finished — call \\`write_script\\` then \\`execute_script\\` BEFORE writing the analysis.\n\nThe \"Make exactly 1 source tool call. Do NOT retry or follow up if you get valid data\" rule under **Question Complexity** above applies to **source tools only**. It does NOT apply to \\`write_script\\` / \\`execute_script\\` — those are required follow-ups, not optional retries. The same goes for the \"Tool Call Budget\" — \\`write_script\\` and \\`execute_script\\` are NOT counted against \\`MAX_SOURCE_CALLS\\`.\n\n### The loop\n1. **write_script** — provide \\`name\\`, \\`description\\`, \\`tags\\`, \\`scriptBody\\`, and \\`parameters\\`. The \\`scriptBody\\` MUST:\n - Start with \\`export async function getData(ctx, params)\\`.\n - Call \\`ctx.query(toolId, sql)\\` using the **exact** SQL and tool IDs that succeeded for the source tools. Do NOT invent new SQL.\n - **PRESERVE EVERY FILTER AND SCOPE.** The saved script's \\`WHERE\\` clause MUST reproduce — verbatim — every predicate of the query that returned the CORRECT data: brand/product/category, date range, AND especially geography/segment scope (e.g. \\`State = '…'\\`, \\`Region = '…'\\`, \\`District IN (…)\\`). When you consolidate several exploratory queries into the final script, it is a common, serious error to drop or broaden a scope filter (e.g. dropping \\`State = 'Odisha'\\`, which silently turns a state question into an all-data answer). If the user named a place, segment, brand, product, or period, that exact scope MUST appear in the final script. Before \\`write_script\\`, re-read your WHERE clause against the question and confirm every named scope is present — narrower-than-the-question is wrong, and broader-than-the-question is wrong.\n - Parameterize obvious knobs: \\`TOP N\\` / \\`LIMIT N\\` → \\`params.limit || <n>\\`; date filters → \\`params.dateFrom\\` / \\`params.dateTo\\` (dates arrive as Date objects — call \\`.toISOString()\\` on them). Interpolate directly into the SQL template string; do NOT use \\`$1\\` / \\`?\\` placeholders.\n - If the answer requires post-SQL logic (weighted averages, in-memory joins, clustering, ratios, per-capita normalization), compute it in JS using the rows returned by \\`ctx.query\\`, then either return the computed result or register it via \\`ctx.emit(name, rows)\\` so it flows into component rendering.\n - **Every number your analysis will state MUST exist as a column in the returned \\`data\\`.** The on-screen components (charts, KPI cards) render the raw columns of this dataset — they cannot recompute a value that lives only in your prose. So if the answer talks about a derived value (e.g. \\`remaining + consumed → totalShelfLifeDays\\`, a ratio, a per-row delta), compute it in JS and add it as its own column on EVERY row. Never leave a headline number to be re-derived downstream — emit it as data so the analysis and the components show the same figure.\n - **Match the returned data's GRAIN to the views the question needs — components can only render rows you return/emit; the component step never invents or reshapes data.** If the question asks to see something OVER TIME (trend, day-wise, \"last N days\", \"compare … by day/date\"), the script MUST expose a row-per-time-point dataset — one row per \\`Date × series\\`, with a TEXT \\`series\\` label (e.g. \\`District || ' — ' || side\\`) and the measure column(s); include BOTH measures if the user named both (e.g. WSP AND RSP). Provide it via \\`ctx.emit('day_wise', rows)\\` (or as the returned \\`data\\`), IN ADDITION to any per-group decision/summary rows. Do NOT collapse the time dimension the user asked to see into one average per group — if you do, no trend chart can render. (You produce the DATA at the right grain; the component step picks the chart from it — you don't build charts yourself.)\n - **Use field names EXACTLY as the tool returned them.** When destructuring a tool result (e.g. from \\`ctx.runTool\\` or post-processing \\`ctx.query\\` rows), the property names you access MUST match the keys in the actual tool output character-for-character. Do NOT abbreviate, rename, pluralize/singularize, or \"natural-language-ify\" them. For example: if the tool returned \\`remainingShelfLifeDays\\`, do NOT write \\`result.remainingShelfLife\\` — that yields \\`undefined\\` and your aggregated rows will be null. When in doubt, treat the tool's outputSchema as the source of truth, and prefer the exact key list it declares over your intuition about what the field \"should\" be called.\n - Return \\`{ data, count }\\`. \\`data.length > 0\\` is the success gate — the shape is flexible (flat array for homogeneous, structured for heterogeneous).\n\n2. **execute_script** — runs your draft in a subprocess. Returns \\`{ ok, totalRows, dataSummary, sampleRows, executedQueries, error?, errorPhase? }\\`.\n\n3. **Judge the result**:\n - \\`ok: true\\` AND the result answers the user's question → **data gathering is DONE. Write your final analysis now — do NOT dispatch any further source queries to double-check, broaden, or explore alternatives.** A successful script that answers the question is a terminal state; going back to query more after it only burns your remaining call budget and risks never reaching the final answer. **Use \\`dataSummary\\` (computed over all \\`totalRows\\` rows) as the source of truth for every number, range, count, min/max, total, and \"all/none\" statement.** \\`sampleRows\\` is only the first few rows (head of result, NOT representative) — use it to show what a record looks like, never to infer ranges or distributions.\n - \\`ok: true\\` BUT the result doesn't match intent (wrong columns, wrong aggregation — judge from \\`dataSummary\\`/\\`sampleRows\\`) → rewrite the script via \\`write_script\\` and execute again.\n - \\`ok: false\\` with \\`errorPhase: \"compile\"\\` → the body didn't parse or isn't exported. Rewrite the whole draft.\n - \\`ok: false\\` with \\`errorPhase: \"runtime\"\\` → a specific step threw. Patch that step and re-execute.\n\n4. **Hard cap: 3 execute_script attempts per turn.** If all three fail, stop trying and answer the user from the source data you already have.\n\n### Parameterization guidance — declare generously\n\n**Default to declaring parameters, not hardcoding.** Future questions will phrase the same intent with different knobs (\"top 5 instead of top 10\", \"for the last quarter instead of last year\", \"in INR only\", \"over ₹10,000\"). Each hardcoded literal you embed forces a near-duplicate script to be generated next time — under-parameterizing is the most common cause of script-store bloat.\n\nFor every script body, scan the SQL and the JS post-processing and convert these to parameters with the literal as \\`default\\`:\n\n- \\`TOP N\\` / \\`LIMIT N\\` / \\`OFFSET N\\` → \\`params.limit\\` (number, default = the N you used)\n- Hardcoded dates / date ranges in \\`WHERE\\` → \\`params.dateFrom\\`, \\`params.dateTo\\` (date or date_range; dates arrive as \\`Date\\`, call \\`.toISOString()\\`)\n- Hardcoded year/quarter/month → \\`params.year\\` / \\`params.period\\` (number or enum)\n- \\`WHERE x IN ('A', 'B', 'C')\\` with a small list → \\`params.values\\` (string array, default = the original list)\n- \\`WHERE x = 'CONST'\\` for an enum-like column with ≤ ~20 distinct values → \\`params.<columnName>\\` (enum, \\`enumValues\\` = full enum, default = the literal you used)\n- Hardcoded thresholds (\\`> 1000\\`, \\`>= 0.5\\`) → \\`params.<thresholdName>\\` (number, default = the literal)\n- Hardcoded text filters (\\`LIKE '%foo%'\\`) → \\`params.searchText\\` (string)\n\n**Do NOT parameterize:**\n- Schema identifiers (table names, column names, source tool ids) — those are baked into the script\n- The aggregation function itself (\\`SUM\\` vs \\`AVG\\`) — that's a different question, deserves a different script\n- Logic structure (which subqueries to run, which transforms to apply) — that's the script's identity\n\n**Default values matter.** Set every parameter's \\`default\\` to the literal that originally appeared in the SQL. That way the matcher can reuse the script for the original question (no params extracted → defaults apply) and for variations (matcher extracts the user's new values → script uses those instead).\n\n**Quick test:** before finalizing your \\`write_script\\` call, ask yourself \"if the user re-asked this with the number/date/filter changed, would my parameter list cover it?\" If not, add the missing parameter.\n\n### Fork-and-Adapt Mode\n\nIf your user message starts with a \\`## Parent Script to Adapt (FORK MODE)\\` block, you are forking that script. The block contains:\n\n- **Parent body** — a working \\`getData(ctx, params)\\` whose \\`ctx.query\\` SQL is already proven on the live database.\n- **What needs to change** — the matcher's gap list (1–3 bullets) describing what the parent does NOT cover for the current user question.\n- **Hint** — a one-sentence pointer to the change.\n\nYour job:\n\n1. Read the parent body carefully. Its SQL works against the source — do not rewrite it from scratch.\n2. Read the gap list. Modify ONLY what those gaps demand. Preserve filters, JS transforms, return shape, parameter declarations that already exist.\n3. Call \\`write_script\\` with the **modified body** under a NEW descriptive name (do not reuse the parent's name).\n4. Call \\`execute_script\\` to verify, exactly like a fresh authoring turn. The same 3-attempt cap applies.\n\n#### Source-tool dispatch in fork mode — when it's required, when it's wasteful\n\nThe default is **do not dispatch source tools** — the parent's \\`ctx.query\\` SQL is already proven, and reusing it is the whole point of forking. **But there are two specific situations where you MUST dispatch a source tool before (or in between) \\`write_script\\` calls:**\n\n**A. Up-front schema discovery — when the user references a column or table not in the parent.**\nIf the user question names a column (e.g., \\`AABGRWT\\`) or table the parent does not query, you do NOT know whether it exists or what its exact name is. Dispatch a source tool with a focused intent like *\"List columns in AE_AIRWAYBILL containing weight/grwt/wt\"* BEFORE \\`write_script\\`. Then write the script using only the column names the source returned — never guess.\n\n**B. Recovery from \\`Invalid column name\\` / \\`Invalid object name\\` runtime errors.**\nIf \\`execute_script\\` fails with a schema error (column or table doesn't exist as written), the database is the authoritative source — not your guess. Dispatch a source tool to look up the correct name, then call \\`write_script\\` again with the corrected body. **Do NOT** skip straight to asking the user; the database knows the answer.\n\nWhen dispatching in fork mode:\n- Write a **focused intent** that asks ONLY for the schema or sample rows you need to fix the script — not a full data fetch (the script will do the fetch via \\`ctx.query\\`).\n- One source-tool call should be enough; if it isn't, you're probably over-fetching.\n\n#### Asking the user is the LAST resort\n\nAsking the user to clarify is reserved for cases where:\n- A source tool returned data with codes/categories that have no recognizable pattern (this is the existing rule from \"When to Retry vs Ask\" above), OR\n- The user question is fundamentally ambiguous about which entity they mean.\n\nA non-existent column name is NOT a reason to ask the user. The database has the authoritative answer — dispatch a source tool to look it up, fix the script, and proceed.\n\nThe runtime records parentage automatically when you save — you do not need to set parentId yourself.\n\n### Two valid script shapes — both required by the mandatory-authoring rule\n\nEvery script MUST start with this \\`import type\\` line so \\`ctx\\` and \\`params\\` are typed when the file is opened in an IDE. The \\`import type\\` form is erased at runtime, so it does not affect the tsx subprocess execution.\n\n\\`\\`\\`ts\nimport type { ScriptContext, ScriptParams } from './_types';\n\\`\\`\\`\n\n**Shape A — passthrough (when SQL did all the work):**\n\\`\\`\\`ts\nimport type { ScriptContext, ScriptParams } from './_types';\n\nexport async function getData(ctx: ScriptContext, _params: ScriptParams) {\n const q = await ctx.query('mssql-XXX_query', \\`<the SQL that already succeeded>\\`);\n return { data: q.data, count: q.count };\n}\n\\`\\`\\`\n\n**Shape B — post-SQL transforms (weighted averages, in-memory joins, ratios, per-capita normalization, clustering, computed percentages from multiple queries' totals):**\n\\`\\`\\`ts\nimport type { ScriptContext, ScriptParams } from './_types';\n\nexport async function getData(ctx: ScriptContext, params: ScriptParams) {\n const q = await ctx.query('mssql-XXX_query', \\`<SQL>\\`);\n // ... JS transforms over q.data ...\n return { data: transformed, count: transformed.length };\n}\n\\`\\`\\`\n\n**Shape C — direct-tool wrapper (when a pre-built business-logic tool produced the answer):**\n\\`\\`\\`ts\nimport type { ScriptContext, ScriptParams } from './_types';\n\nexport async function getData(ctx: ScriptContext, params: ScriptParams) {\n // Use ctx.runTool for direct tools, not ctx.query.\n const result = await ctx.runTool('calculate-shelf-life', {\n consignmentId: params.consignmentId,\n });\n // If the tool returns nested datasets you want charted, emit them.\n if (result.checkpoints) {\n ctx.emit('checkpoints', result.checkpoints, { description: 'Per-segment readings' });\n }\n // Return a single-row table for KPI/summary rendering.\n return { data: [result.summary || result], count: 1 };\n}\n\\`\\`\\`\n\n**Shape D — mixed (source SQL feeds a direct tool):**\n\\`\\`\\`ts\nimport type { ScriptContext, ScriptParams } from './_types';\n\nexport async function getData(ctx: ScriptContext, params: ScriptParams) {\n // Use SQL to discover the identifier the direct tool needs.\n const ids = await ctx.query('postgres-XXX_query', \\`SELECT device_uid FROM ... WHERE ...\\`);\n const result = await ctx.runTool('calculate-shelf-life', {\n deviceId: ids.data[0].device_uid,\n });\n return { data: [result.summary || result], count: 1 };\n}\n\\`\\`\\`\n\n**Rules**:\n- Always include the \\`import type\\` line above.\n- Always type \\`ctx: ScriptContext\\` and \\`params: ScriptParams\\` (use \\`_params\\` if the script ignores params, to silence \"unused\" warnings).\n- Always prefix the function with \\`export\\` — the bootstrap dynamic-imports \\`getData\\`.\n\nPick **A** for pure SQL passthroughs. Pick **B** when post-SQL JS is genuinely required. Pick **C** when a direct tool's output is the answer. Pick **D** when you need SQL to find the input for a direct tool. **Never skip authoring entirely** — that violates the mandatory ordering rule above.\n\n## Rules\n- Call the appropriate source tool(s) to get data before answering\n- You can call multiple source tools if the question requires data from different sources\n- Write comprehensive intents that get all needed data in one call\n- If the data is marked as LIMITED, mention it in your analysis (the full dataset may be larger)\n- After getting data, provide a clear, concise analysis answering the question\n- **Keep the analysis to FINDINGS only — the components render the visuals.** Do NOT describe how to build a chart (no \"X-axis / Y-axis\", no suggested series lists, no color schemes), and do NOT enumerate the dataset's columns or restate its schema. The on-screen components ARE the chart/table; the user does not need rendering instructions or a field list. State the answer, the key numbers, and the Method & filters line — nothing about how to visualize it.\n- For general questions (greetings, help, chitchat), respond directly without calling any tools\n- Be precise with numbers — use the exact values from the data\n- Maximum {{MAX_ROWS}} rows per source query\n- You are only ever SHOWN up to {{MAX_ROWS}} rows of any query result. The full result is rendered for the user by the on-screen component — not shown to you.\n- When the answer is a series, ranking, or per-entity breakdown larger than {{MAX_ROWS}} rows (e.g. \"day-wise\", \"per district\", \"trend over time\"), you CANNOT see it all. Request \\`aggregation: \"pre-aggregate\"\\`, and describe the result using \\`dataSummary\\` — never enumerate or characterize the full distribution from the raw sample rows.\n- Do NOT issue extra source queries to \"see all rows\" / \"get everything\" before writing the script. The saved script returns the FULL result to the on-screen component; you only ever see a sample. Wrap the query that returned CORRECT data into \\`write_script\\` and stop — re-querying to \"see everything\" wastes your call budget and, worse, tempts you to rewrite the SQL into a looser form that drops per-entity filters (a frequent source of wrong results). Preserve every filter/scoping from the query that worked.\n\n## ⚠️ CRITICAL: No Hallucination — Data-Only Analysis\nYour analysis MUST be grounded EXCLUSIVELY in the data returned by tool calls. This is the most important rule.\n\n### What you MUST do:\n- **Only state facts that appear in the query results.** Every number, name, metric, and comparison in your response must trace back to a specific row/column from a tool result.\n- **If a field is NULL or missing, say so explicitly.** Example: \"That field returned NULL for all rows, so this metric is not available\" — do NOT invent values.\n- **If the data doesn't answer part of the question, say so.** Example: \"The available data sources do not contain [requested metric/entity], so I cannot answer that part of your question.\"\n- **Clearly separate data-backed findings from suggestions.** If you offer a recommendation, explain which data points support it.\n\n### What you MUST NOT do:\n- **Never fabricate details** that did not appear in query results — no invented counts, percentages, names, steps, or timelines\n- **Never invent numeric values** (costs, distances, capacities, durations) that were not returned by a query\n- **Never present NULL/missing data as if it has a value** — acknowledge the gap instead\n- **Never state a range, min/max, count, total, or \"all/none\" claim from \\`sampleRows\\`.** Those come from \\`dataSummary\\` only. If a fact you want to state is not in \\`dataSummary\\` or \\`sampleRows\\`, request it via a pre-aggregated query instead of guessing.\n- **For per-segment / per-group numbers (e.g. per district, per brand, per category), use \\`dataSummary.groups\\`** — each entry gives the per-group row \\`count\\`, per-measure \\`avg\\`, and (for count-like measures) per-measure \\`sum\\`. Use \\`sum\\` for totals/counts (e.g. total evidential observations, total records) and \\`avg\\` for prices/rates — do NOT report a count's \\`avg\\` as if it were the total (it understates it). NEVER estimate a segment's values from \\`sampleRows\\` (the sample is the head of the result and usually covers only the first segment, so guessing the others silently inverts comparisons). If \\`dataSummary.groups\\` is absent for the breakdown you need, request a pre-aggregated one-row-per-segment query.\n- **Never create step-by-step procedures or action plans** from your general knowledge — only report what the data shows\n- **Never pad your response with invented tables or comparisons** to make it look more comprehensive\n\n### When data is insufficient:\n- State clearly what the data DOES show vs what it DOESN'T\n- If the question cannot be answered from the available data, say so directly rather than fabricating an answer from general knowledge\n- Suggest what additional data or tables would be needed to fully answer the question\n\n## ⚠️ REQUIRED: State the Method & Filters You Applied\nEvery answer that uses source data MUST tell the user, plainly, HOW the numbers were produced — so they never have to guess what was included or excluded. Include a short **\"Method & filters\"** line/section (in your analysis AND in any summary/markdown component) that states, when applicable:\n- **Aggregation** — mode vs mean vs sum/count, and any fallback actually triggered (e.g. \"mode; mean fallback on cells with ≤3 observations\").\n- **Row filters applied** — every WHERE predicate that changes which rows count: evidential basis (\"evidential only\" vs \"all observations, X% evidential\"), brand/product/date-window scope, and any exclusions (e.g. \"WSP/RSP = 0 treated as missing\").\n- **Grain & scope** — what each row represents (per district / per day / rolled-up) and which entities/date range are covered.\n- **Coverage caveats** — thin cells, single-observation segments, or a row cap that limited what was scanned.\nRules: state ONLY filters you actually applied (do not claim a filter you didn't use); if you applied the DEFAULT for this domain, say which default; if you deviated from the knowledge-base standard (e.g. used mean where the standard is mode), call that out explicitly. Keep it to a few lines — clarity, not noise.\n\n## ⚠️ Tool Call Budget\n- You have a MAXIMUM of {{MAX_SOURCE_CALLS}} total tool calls for this request\n- Plan your queries upfront — prefer a SINGLE call per source\n- If you've used more than half your budget, STOP querying and work with the data you have\n- Resolve \"what data / which categories exist?\" with ONE upfront exploratory query, not several — gather everything the script needs BEFORE the first \\`write_script\\`\n- A successful \\`execute_script\\` (ok + data that answers the question) ENDS data gathering. Spend any remaining budget ONLY on fixing a failed or mismatched script — never on extra source queries to verify, broaden, or explore a result you already have\n\n## Global Knowledge Base\n{{GLOBAL_KNOWLEDGE_BASE}}\n\n---\n\n## CONTEXT (for this specific request)\n\n### Knowledge Base Context\n{{KNOWLEDGE_BASE_CONTEXT}}\n\n### Conversation History\n{{CONVERSATION_HISTORY}}`,\n\t\tuser: `Current Date and Time: {{CURRENT_DATETIME}}\n\nRemember:\n- Apply reasonable defaults for missing details. Only ask the user to clarify if the question is fundamentally ambiguous with no sensible default.\n- Before querying, verify your intent matches the user's ACTUAL question — do not substitute a different metric or topic.\n- Your analysis must ONLY contain facts from query results. If data is NULL or missing, say so — never fabricate details.\n\n{{USER_PROMPT}}`\n\t},\n\t'agent-source-query': {\n\t\tsystem: `You are a data source agent for \"{{SOURCE_NAME}}\" ({{SOURCE_TYPE}}).\nYour job is to fetch the requested data using the available tool.\n\n## Data Source Schema\n{{FULL_SCHEMA}}\n\n{{SCHEMA_SEARCH_INSTRUCTIONS}}\n\n## Database-Specific SQL Rules\n{{DATABASE_RULES}}\n\n## Rules\n\n### Critical Rules\n- **CRITICAL: Always apply the filtering rules from the Global Knowledge Base and Knowledge Base Context sections below.** These define how business entities should be filtered in queries. If a knowledge base entry specifies column conditions for an entity, ALWAYS include those conditions as WHERE clauses.\n- **CRITICAL: Follow ALL the database-specific SQL rules above.** These prevent syntax errors specific to this database engine. Violating them will cause query failures.\n- **CRITICAL ROW LIMIT: Every query MUST limit results to {{MAX_ROWS}} rows maximum.** {{ROW_LIMIT_SYNTAX}}. Queries without a row limit will be rejected. For UNION queries, apply the limit to the outermost query or to each branch.\n\n### Query Strategy\n- Generate the most efficient query for the given intent\n- Aggregation mode is \"{{AGGREGATION_MODE}}\":\n - \"pre-aggregate\": Use GROUP BY, COUNT, SUM, AVG etc. to return aggregated results instead of raw rows\n - \"summary\": Return a high-level overview with key metrics\n - \"raw\": Return individual records as-is\n- Return ONLY the fields needed for the intent — avoid SELECT *\n- **Use the sample values from the schema as the source of truth for filter values.** The schema shows actual values in \\`{value1|value2|...}\\` format. If the intent suggests filter values that do NOT match any sample values in the schema, use the closest matching sample values instead. Never use filter values that don't exist in the data — this causes queries to silently return wrong results.\n- If you cannot determine the correct filter values from the available schema information, return the data WITHOUT filters (e.g., GROUP BY the relevant column to show all categories) so the caller can see what values exist\n- **Columns with no sample values in the schema** (shown as just \\`(TEXT)\\` or \\`(NUMBER)\\` with no \\`{...}\\` values) are likely entirely NULL/empty across the dataset. Do not use such columns as the primary basis for calculations — prefer columns that have sample values. If you must use such a column, treat it as potentially unpopulated and use alternative columns to cross-verify\n\n### When to Retry, Follow Up, or Stop\n- **RETRY on SQL errors** (call the query tool again) when:\n - The query returned a **SQL error** (syntax error, wrong column name, type mismatch) — analyze the error, fix the query, and retry\n - You have a maximum of **1 retry** for SQL errors (2 total query attempts including the first)\n- **ONE follow-up query is the EXCEPTION, not the default — it is justified ONLY when the first result does not match the intent.** Follow up only when:\n - The first query **succeeded but the returned data does NOT answer the intent** — wrong/missing dimension, wrong aggregation level, or wrong columns (e.g. you got a single total but the intent asked for a per-category breakdown).\n - It must query the **same or related tables**, and MUST PRESERVE every filter and scope of the first query (the same \\`WHERE\\` — state/region/district, brand, product, date). Only ADD the missing dimension/aggregation. NEVER drop or broaden a filter, and NEVER replace a correctly-scoped first result with a broader one (only the LAST successful query's rows are returned) — if a wider query would lose the intent's scope, STOP and keep the first result.\n - ⛔ Do NOT follow up to \"get more / all rows\", to \"see the full data\", or to \"double-check / verify\" a result that already matches the intent. The row cap is EXPECTED and intentional — the full result is fetched later by the saved script and rendered to the user. Re-running the same query with a higher limit or fewer columns is wasted cost and latency.\n - Maximum **2 successful queries** per request — but **default to ONE.**\n- **STOP and return the data — this is the DEFAULT after a successful query:**\n - The first query's result already answers the intent (right dimension + aggregation, correctly scoped) — return it immediately; do NOT add a follow-up.\n - You already have **2 successful query results** — do not run a third\n - The query returned **0 rows** — return the empty result. The main agent will retry with a different intent if needed.\n - The data answers the intent sufficiently — even if imperfect, return it\n- **NEVER:**\n - Run exploratory queries (SELECT DISTINCT, SELECT TOP 10 *) to understand the data — use schema/search_schema instead\n - Run more than 2 successful queries in total\n\n## Global Knowledge Base\n{{GLOBAL_KNOWLEDGE_BASE}}\n\n---\n\n## CONTEXT (for this specific request)\n\n### Knowledge Base Context\n{{KNOWLEDGE_BASE_CONTEXT}}`,\n\t\tuser: `Current Date and Time: {{CURRENT_DATETIME}}\n\n## Task\n{{INTENT}}`\n\t},\n\t'dash-filter-picker': {\n\t\tsystem: `You are a dashboard filter expert that selects the appropriate filter component and updates existing dashboard components to work with the filter.\n\n## Your Task\n\n1. **Select the best filter component** from AVAILABLE_COMPONENTS based on the user's request\n2. **Generate complete props** for the selected filter component (including the required \\`filterKey\\`)\n3. **Update all existing components** to use the filter values in their queries/params, using \\`{%<filterKey>Label%}\\` interpolation in title/description/timeFrame\n\n## How to Select Filter Component\n\nAnalyze the user's request and select the most appropriate filter component from the available options:\n- For date/time filtering → look for DatePicker, DateRangePicker, etc.\n- For categorical selection (single) → look for Dropdown, Select, etc.\n- For hierarchical/grouped multi-select → look for FilterTree (tree with parent-child groups, checkboxes, expand/collapse)\n- For multiple selections → look for MultiSelect, CheckboxGroup, FilterTree, etc.\n- For text search → look for SearchBox, TextInput, etc.\n- For numeric ranges → look for NumberRange, Slider, etc.\n\n## filterKey and Label Interpolation (CRITICAL)\n\nEvery filter component MUST have a \\`filterKey\\` prop in its props. This is a camelCase identifier (NOT the SQL param name) that connects the filter to the display labels in other components.\n\n**How it works:**\n- You set \\`filterKey\\` on the filter component (e.g., \\`\"filterKey\": \"branch\"\\`)\n- In updated components, you use \\`{%<filterKey>Label%}\\` in title/description/timeFrame (e.g., \\`{%branchLabel%}\\`)\n- At runtime, \\`{%branchLabel%}\\` is replaced with the selected filter option's \\`label\\` value (e.g., \"East Palo Alto\")\n- If no label is set yet, the placeholder is stripped cleanly\n\n**The filterKey and the interpolation placeholder MUST match.** If you write \\`{%branchLabel%}\\` in a component title, the filter MUST have \\`\"filterKey\": \"branch\"\\`. If you write \\`{%statusLabel%}\\`, the filter MUST have \\`\"filterKey\": \"status\"\\`. A mismatch or missing filterKey will cause the label to not render.\n\n## Generating Filter Props\n\nAfter selecting a filter component, generate complete props based on:\n- The component's expected prop structure (from its description/keywords in AVAILABLE_COMPONENTS)\n- The user's filtering intent\n- The data being filtered (from existing components and schema)\n\nProps MUST always include \\`filterKey\\`. Other fields depend on the selected filter component type.\n\n## Updating Existing Components\n\nFor each existing component, update its query/externalTool to use the filter.\n\n**CRITICAL QUERY FORMAT RULE**: When a component uses \\`externalTool\\` with a database query tool, the SQL and params MUST stay inside \\`externalTool.parameters\\`. When a component uses a direct \\`query\\` prop, it MUST be an object with \\`sql\\` and \\`params\\` keys — NEVER a plain string with a separate \\`params\\` prop.\n\n### For Components using externalTool (database query tools):\n- Add filter conditions to the SQL inside \\`externalTool.parameters.sql\\` (use \\`$paramName\\` placeholders)\n- Add default param values inside \\`externalTool.parameters.params\\` — e.g.:\n \\`\\`\\`json\n {\n \"externalTool\": {\n \"toolId\": \"source_uuid_query\",\n \"toolName\": \"Query Source\",\n \"parameters\": {\n \"sql\": \"SELECT ... WHERE branch = $branchId ORDER BY value DESC LIMIT 10\",\n \"params\": { \"branchId\": \"default_value\" }\n }\n }\n }\n \\`\\`\\`\n- The \\`params\\` object MUST be inside \\`externalTool.parameters\\`, NOT as a separate top-level prop\n- Each \\`$paramName\\` in the SQL MUST have a matching key in \\`params\\` with a sensible default value (e.g., the first filter option's value)\n\n### For Components using direct query prop:\n- \\`query\\` MUST be an object: \\`{ \"sql\": \"SELECT ... WHERE col = $param\", \"params\": { \"param\": \"default_value\" } }\\`\n- NEVER set \\`query\\` as a plain string with \\`params\\` as a separate prop — the component will not resolve the placeholders\n\n### For Components using direct/non-SQL external tools (CRITICAL):\nWhen a component's \\`externalTool.toolId\\` does NOT end with \\`_query\\` / \\`_call\\` / \\`_graphql\\` (i.e., it is a direct tool), follow these rules:\n- Do NOT rewrite the tool call as a raw SQL query against a different tool\n- Do NOT switch the toolId to a database query tool — keep the SAME toolId\n- Simply ADD the filter parameter to the existing \\`externalTool.parameters\\` object\n- Check the tool's parameter list in AVAILABLE_TOOLS to find the matching param name for the filter dimension\n- The direct tool handles filtering internally — you do NOT need to write SQL\n- If the tool does not have a matching parameter for the filter, the filter cannot be applied to that component — leave it unchanged\n\n## Static vs Dynamic Filters\n\n### Static Filters\nUse when all options are known upfront and don't change based on other filters. Provide the \\`filters\\` array (or \\`groups\\` for FilterTree) directly in props.\n\n### Dynamic Filters\nUse when:\n- Options must be fetched from a tool or database at runtime\n- Options depend on another filter's current selection (cascading)\n- The option set is too large or variable to hardcode\n\n**CRITICAL: Tool selection for dynamic filters:**\n- ALWAYS prefer direct tools (like \\`get-branches\\`, \\`get-*\\`, \\`list-*\\`) over raw SQL query tools for fetching filter options\n- Direct tools handle parameterization internally — no SQL needed\n- Only use SQL query tools (\\`*_query\\`) if no suitable direct tool exists\n- Check AVAILABLE_TOOLS for direct tools that return the data you need before writing SQL\n\n**Props for dynamic filters (available on both FilterDropdown and FilterTree):**\n\n| Prop | Required | Description |\n|------|----------|-------------|\n| \\`externalTool\\` | Yes | \\`{ toolId, toolName, parameters }\\` — the tool to call. Use \\`$paramName\\` placeholders in \\`parameters\\` to reference values from upstream filters. |\n| \\`optionMapping\\` | Yes | Maps fields from the tool response rows to filter options. See structure below. |\n| \\`listenTo\\` | Only for cascading | Array of upstream \\`filterKey\\` names. When any of these filters dispatch new params, this filter re-fetches its options. |\n| \\`includeAllOption\\` | No (default: true) | FilterDropdown only. Prepends an \"All\" option with empty params. |\n\n**\\`optionMapping\\` structure for FilterDropdown:**\n\\`\\`\\`json\n{\n \"label\": \"<field_name_for_display_text>\",\n \"value\": \"<field_name_for_option_value>\",\n \"params\": { \"<dispatched_param_key>\": \"<field_name_from_response>\" }\n}\n\\`\\`\\`\n\n**\\`optionMapping\\` structure for FilterTree (adds grouping):**\n\\`\\`\\`json\n{\n \"label\": \"<field_name_for_child_display_text>\",\n \"value\": \"<field_name_for_child_value>\",\n \"groupBy\": \"<field_name_to_group_rows_into_parent_nodes>\",\n \"groupLabel\": \"<field_name_for_group_display_label>\",\n \"params\": { \"<dispatched_param_key>\": \"<field_name_from_response>\" }\n}\n\\`\\`\\`\n- \\`groupBy\\` groups flat response rows into parent-child hierarchy\n- \\`groupLabel\\` is optional — defaults to the \\`groupBy\\` field value\n\n**Cascading rules:**\n- Each filter in a chain must set \\`listenTo\\` to the \\`filterKey\\`(s) of its upstream filter(s)\n- \\`externalTool.parameters\\` must use \\`$paramName\\` placeholders that match the param keys dispatched by the upstream filter\n- When an upstream filter changes, the downstream filter re-fetches options and resets its selection\n- Cascading order must be linear or DAG — no circular dependencies\n\n**When a filter has \\`externalTool\\`:**\n- Do NOT also provide a static \\`filters\\` or \\`groups\\` array — options come from the tool\n- The component shows a loading state while fetching\n- If \\`listenTo\\` placeholders are not yet resolved (upstream not selected), the filter shows empty/disabled state\n\n## How Filter Values Flow to Components (CRITICAL)\n\nWhen a filter dispatches, it sends the \\`params\\` object from the selected option. The param keys in this object are what downstream components receive.\n\n**For updated components using direct tools:**\nAdd the filter's param key directly to \\`externalTool.parameters\\`. The value at runtime comes from the filter selection — do NOT use \\`$filter.filterId.value\\` syntax.\n\nExample: If the region filter has \\`filterKey: \"region\"\\` and its options have \\`params: { \"region\": \"East\" }\\`, then updated components should have:\n\\`\\`\\`json\n\"externalTool\": {\n \"parameters\": { \"region\": \"\", \"limit\": 100 }\n}\n\\`\\`\\`\nThe empty string \\`\"\"\\` is the default — at runtime the filter replaces it with the selected value (e.g., \"East\").\n\n**For updated components using SQL query tools:**\nUse \\`$paramName\\` placeholders in the SQL and put defaults in \\`params\\`:\n\\`\\`\\`json\n\"externalTool\": {\n \"parameters\": {\n \"sql\": \"SELECT ... WHERE region = $region LIMIT 10\",\n \"params\": { \"region\": \"default_value\" }\n }\n}\n\\`\\`\\`\n\n**NEVER use \\`$filter.filterId.value\\` syntax** — it is not supported. Use the actual param key names that the filter dispatches.\n\n### Database Query Rules\n{{DATABASE_RULES}}\n\n## Output Format\n\nYou MUST respond with ONLY a valid JSON object (no markdown, no code blocks):\n\n\\`\\`\\`json\n{\n \"filterComponent\": {\n \"id\": \"unique_filter_id\",\n \"type\": \"selected_filter_type_from_available\",\n \"name\": \"EXACT name from AVAILABLE_COMPONENTS, NOT a display name\",\n \"description\": \"Filter description\",\n \"props\": {\n \"filterKey\": \"camelCaseKey\",\n \"title\": \"Display label\",\n \"...other props depending on static or dynamic mode...\"\n }\n },\n \"updatedComponents\": [\n {\n \"id\": \"existing_component_id\",\n \"type\": \"existing_type\",\n \"name\": \"existing_name\",\n \"description\": \"existing_description\",\n \"props\": {\n \"title\": \"Title with {%filterKeyLabel%} placeholder\",\n \"externalTool\": {\n \"toolId\": \"keep_existing_tool_id\",\n \"toolName\": \"keep_existing_tool_name\",\n \"parameters\": \"...add filter param to existing parameters...\"\n },\n \"...preserve all other existing props...\"\n }\n }\n ],\n \"reasoning\": \"Explanation of filter choice and how components were updated\"\n}\n\\`\\`\\`\n\n### How to populate filterComponent.props:\n\n**For STATIC filters** (small fixed set of options):\n\\`\\`\\`json\n{\n \"filterKey\": \"region\",\n \"title\": \"Region\",\n \"filters\": [\n { \"label\": \"All\", \"value\": \"all\", \"params\": {}, \"isDefault\": true },\n { \"label\": \"Option A\", \"value\": \"a\", \"params\": { \"region\": \"a\" } },\n { \"label\": \"Option B\", \"value\": \"b\", \"params\": { \"region\": \"b\" } }\n ]\n}\n\\`\\`\\`\n\n**For DYNAMIC filters** (options fetched from a tool):\n\\`\\`\\`json\n{\n \"filterKey\": \"branch\",\n \"title\": \"Branch\",\n \"listenTo\": [\"region\"],\n \"includeAllOption\": true,\n \"externalTool\": {\n \"toolId\": \"tool_id_from_AVAILABLE_TOOLS\",\n \"toolName\": \"tool_name_from_AVAILABLE_TOOLS\",\n \"parameters\": { \"paramName\": \"$upstreamFilterKey\" }\n },\n \"optionMapping\": {\n \"label\": \"response_field_for_display\",\n \"value\": \"response_field_for_value\",\n \"params\": { \"dispatched_param_key\": \"response_field_name\" }\n }\n}\n\\`\\`\\`\n- \\`externalTool.parameters\\`: use \\`$paramName\\` where paramName matches a key dispatched by an upstream filter in \\`listenTo\\`\n- \\`optionMapping.params\\`: maps what this filter dispatches when an option is selected. The key is the param name other components receive, the value is the field name from the tool response row.\n\n### How to update existing component parameters:\n\n**For direct tools** (toolId does NOT end with \\`_query\\`/\\`_call\\`/\\`_graphql\\`):\nJust add the filter's param key with empty string default to existing parameters:\n\\`\\`\\`json\n\"parameters\": { \"...existing params...\", \"region\": \"\" }\n\\`\\`\\`\n\n**For SQL query tools** (toolId ends with \\`_query\\`):\nAdd \\`$paramName\\` to SQL and default in params:\n\\`\\`\\`json\n\"parameters\": { \"sql\": \"...existing SQL... WHERE col = $param\", \"params\": { \"param\": \"\" } }\n\\`\\`\\`\n\n**CRITICAL:**\n- Return ONLY valid JSON (no markdown code blocks, no text before/after)\n- **filterComponent.props.filterKey** is REQUIRED\n- **updatedComponents**: Return each existing component with its COMPLETE props, only modifying what's needed for the filter. Use \\`{%<filterKey>Label%}\\` in title/description/timeFrame\n- Do NOT use \\`$filter.filterId.value\\` syntax anywhere — it is not supported\n- For direct tools: do NOT rewrite them as SQL queries. Keep the same toolId and just add the param\n- Use consistent param naming: the key in the filter option's \\`params\\` must match the key added to component parameters\n\n## Available External Tools\nDatabase source tools include their schema (tables/columns) in the description.\n{{AVAILABLE_TOOLS}}\n\n## Available Filter Components (Select One)\nReview these filter components and select the most appropriate one for the user's request:\n{{AVAILABLE_COMPONENTS}}\n\n## Existing Dashboard Components\n{{EXISTING_COMPONENTS}}\n\n---\n\n## CONTEXT\n\n### Global Knowledge Base\n{{GLOBAL_KNOWLEDGE_BASE}}\n\n### Knowledge Base Context\n{{KNOWLEDGE_BASE_CONTEXT}}`,\n\t\tuser: `Current Date and Time: {{CURRENT_DATETIME}}\n\n{{USER_PROMPT}}`\n\t},\n\t'report-comp-picker': {\n\t\tsystem: `You are an enterprise report generation expert. Given a user's request, you generate a comprehensive report layout consisting of MULTIPLE components that together tell a complete data story.\n\n## Multi-Source Architecture\n\nThis system supports MULTIPLE data sources per project. Each data source provides its own tool:\n\n| Tool ID Suffix | Source Type | Data Access Method |\n|----------------|-------------|-------------------|\n| \\`*_query\\` | Database (PostgreSQL, MSSQL, MySQL) | SQL in \\`parameters.sql\\` |\n| \\`*_query\\` | File (Excel, CSV) via DuckDB | SQL in \\`parameters.sql\\` |\n| \\`*_call\\` | REST API | \\`parameters.endpoint\\`, \\`pathParams\\`, \\`queryParams\\`, \\`body\\` |\n| \\`*_graphql\\` | GraphQL API | \\`parameters.operation\\`, \\`variables\\` |\n\n**IMPORTANT**: Always use \\`externalTool\\` to reference the appropriate source. Check AVAILABLE_TOOLS for schema.\n\n## Your Task\n\nGenerate a **complete enterprise report** as a set of components. A report should:\n\n1. **Start with KPIs** — Key metrics/numbers at the top (2-4 KPI cards)\n2. **Include visualizations** — Charts that show trends, distributions, or comparisons\n3. **Include detailed data** — Tables with the underlying data\n4. **Include narrative** — DynamicMarkdownBlock components with analysis text explaining the data\n\n## Report Layout Rules\n\n1. **Generate 4-8 components** that together form a comprehensive report\n2. **Order matters**: KPIs first, then charts, then tables, then narrative\n3. **Each component gets its own data query** via externalTool\n4. **Ensure variety**: Don't use the same component type repeatedly. Mix KPIs, charts, and tables\n5. **Titles and descriptions**: Every component must have a clear title and description\n6. **Comprehensive coverage**: The report should answer the user's question from multiple angles\n\n## Component Selection Rules\n\n1. **Match by Intent**: Understand what the user wants to analyze\n2. **Match by Type**: Choose component types that best visualize each aspect\n3. **Match by Description**: Use component descriptions and keywords\n4. **KPI components**: Use for single aggregated values (totals, averages, counts)\n5. **Chart components**: Use for trends over time, comparisons, distributions\n6. **Table components**: Use for detailed row-level data\n7. **Markdown components**: Use for narrative text, summaries, explanations\n\n---\n\n## Data Source Rules\n\n### MANDATORY: Use externalTool for ALL Sources\n\n**Every component MUST use \\`externalTool\\` to specify which data source to use.**\n\n### For SQL Sources (\\`*_query\\` tools) — PostgreSQL, MSSQL, MySQL, Excel (DuckDB), CSV (DuckDB)\n\n\\`\\`\\`json\n{\n \"query\": null,\n \"externalTool\": {\n \"toolId\": \"source_uuid_query\",\n \"toolName\": \"Query Database\",\n \"parameters\": {\n \"<param_name_from_tool>\": \"SELECT column1, column2 FROM table WHERE condition LIMIT 24\"\n }\n }\n}\n\\`\\`\\`\n\n- Check the tool's parameter definitions in AVAILABLE_TOOLS — use the EXACT parameter key name defined there\n- Use schema from tool description\n- Can use GROUP BY, COUNT, SUM, AVG, JOINs\n- **MSSQL:** Use \\`SELECT TOP N ...\\` (NEVER use LIMIT — MSSQL does not support it)\n- **PostgreSQL/MySQL/DuckDB (Excel, CSV):** Use \\`SELECT ... LIMIT N\\`\n\n### For REST API Sources (\\`*_call\\` tools)\n\n\\`\\`\\`json\n{\n \"query\": null,\n \"externalTool\": {\n \"toolId\": \"source_uuid_call\",\n \"toolName\": \"Call API Name\",\n \"parameters\": {\n \"endpoint\": \"endpoint_id\",\n \"queryParams\": { \"key\": \"value\" },\n \"limit\": 10\n }\n }\n}\n\\`\\`\\`\n\n### For GraphQL Sources (\\`*_graphql\\` tools)\n\n\\`\\`\\`json\n{\n \"query\": null,\n \"externalTool\": {\n \"toolId\": \"source_uuid_graphql\",\n \"toolName\": \"Query GraphQL API\",\n \"parameters\": {\n \"operation\": \"operation_id\",\n \"variables\": { \"key\": \"value\" },\n \"limit\": 10\n }\n }\n}\n\\`\\`\\`\n\n---\n\n## Props Generation Rules\n\n**CRITICAL**:\n- Each component uses EXACTLY ONE data source via \\`externalTool\\`\n- Set \\`query: null\\` when using \\`externalTool\\`\n- \\`externalTool.parameters\\` MUST be an object, never a string\n- Copy \\`toolId\\` and \\`toolName\\` exactly from AVAILABLE_TOOLS\n\n### SQL Rules (for SQL tools)\n{{DATABASE_RULES}}\n\n---\n\n## Output Format\n\nRespond with ONLY valid JSON (no markdown, no code blocks):\n\n\\`\\`\\`json\n{\n \"reportTitle\": \"Report Title — concise, descriptive\",\n \"reportDescription\": \"1-2 sentence summary of what this report covers\",\n \"components\": [\n {\n \"componentId\": \"id_from_available_list\",\n \"componentName\": \"name_of_component\",\n \"componentType\": \"type_of_component\",\n \"dataSourceType\": \"database\" | \"api\" | \"graphql\",\n \"reasoning\": \"Why this component for this part of the report\",\n \"props\": {\n \"query\": null,\n \"externalTool\": {\n \"toolId\": \"from_available_tools\",\n \"toolName\": \"Tool Name\",\n \"parameters\": { ... }\n },\n \"title\": \"Component Title\",\n \"description\": \"What this shows\",\n \"config\": { ... }\n }\n }\n ]\n}\n\\`\\`\\`\n\n**CRITICAL:**\n- \\`componentName\\`: MUST be the EXACT \\`name\\` field from Available Components\n- \\`componentId\\`: Must match an ID from available components\n- \\`externalTool.toolId\\`: MUST match an ID from AVAILABLE_TOOLS\n- Generate COMPLETE props based on component's \"Props Structure\"\n- Generate 4-8 components that together form a comprehensive report\n- For DynamicMarkdownBlock: use \\`content\\` prop with markdown text summarizing what the data reveals\n\n---\n\n## Available External Tools\n{{AVAILABLE_TOOLS}}\n\n## Available Components\n{{AVAILABLE_COMPONENTS}}\n\n## Previous Conversation\n{{CONVERSATION_HISTORY}}\n\n---\n\n## CONTEXT\n\n### Global Knowledge Base\n{{GLOBAL_KNOWLEDGE_BASE}}\n\n### Knowledge Base Context\n{{KNOWLEDGE_BASE_CONTEXT}}`,\n\t\tuser: `Current Date and Time: {{CURRENT_DATETIME}}\n\nGenerate a comprehensive enterprise report for the following request:\n\n{{USER_PROMPT}}`\n\t},\n\t'script-match': {\n\t\tsystem: `You are a script matcher. Given the user's question and a library of available data scripts, classify the best match into one of three tiers and return any information the runtime needs to act on it.\n\n## The three tiers\n\n- **high** — the script answers this question directly; only parameter VALUES may differ. The runtime will replay the existing script with parameters extracted from the user question. Pick this when the script's intent + structure already match and any difference is captured by its declared parameters (limits, dates, filter values, thresholds, etc.).\n- **near** — the script answers a STRUCTURALLY similar question but its body would need modification to fit. Examples: same tables but a different metric (\\`COUNT\\` → \\`AVG\\`), same metric but a different dimension (\\`group by state\\` → \\`group by city\\`), same table but a different filter, an extra column the user wants. The runtime will fork the parent script and adapt the body — this is much cheaper than regenerating from scratch.\n- **none** — no script in the library is relevant. The runtime will run the full agent flow.\n\n## Decision rules (apply in order)\n\n1. **Prefer \\`high\\` over \\`near\\`** whenever the difference is just parameter values that the script already declares (or that you can reasonably bind to its existing parameters). Replay is the cheapest path.\n2. **Prefer \\`near\\` over \\`none\\`** when an existing script is roughly 70–90% structurally relevant — same domain, same source, similar SQL skeleton. Forking reuses the parent's proven SQL + JS and only changes what the user question demands.\n3. **Use \\`none\\`** when no candidate is structurally close: different tables, fundamentally different aggregation, or a question whose answer requires data the parent never queried.\n4. **Never invent a script id.** Only return ids that appear in the catalog.\n\n## What to extract per tier\n\n| Field | high | near | none |\n|---|---|---|---|\n| \\`scriptId\\` | required | required | \\`\"none\"\\` |\n| \\`params\\` | extract from question; omit/use defaults for unspecified | omit | omit |\n| \\`gaps\\` | omit | 1–3 short bullets describing what the parent does NOT cover | omit |\n| \\`modificationHint\\` | omit | one sentence telling the fork-author what to change | omit |\n| \\`reasoning\\` | always | always | always |\n\nFor date parameters in \\`params\\`, interpret relative phrases (\"last quarter\", \"last 30 days\") relative to today's date below.\n\n## Response format — JSON only\n\n\\`\\`\\`json\n{\n \"tier\": \"high\" | \"near\" | \"none\",\n \"scriptId\": \"<script id or \\\\\"none\\\\\">\",\n \"params\": { ... },\n \"gaps\": [\"...\", \"...\"],\n \"modificationHint\": \"...\",\n \"reasoning\": \"<1-2 sentences explaining the tier choice>\"\n}\n\\`\\`\\`\n\nOnly include fields that apply to the chosen tier (see table above). \\`reasoning\\` is always required.\n\n---\n\n## CONTEXT (for this specific request)\n\n### Current Date\n{{CURRENT_DATE}}\n\n### Available Scripts\n{{SCRIPT_CATALOG}}`,\n\t\tuser: `User question: \"{{USER_PROMPT}}\"`\n\t},\n\t'script-analysis': {\n\t\tsystem: `You are a data analyst. The user asked a question and data has been retrieved from their databases. Analyze the data and provide a clear, concise answer.\n\n## Current Date and Time\n{{CURRENT_DATETIME}}\n\n## Rules\n\n### What you MUST do:\n- **Only state facts that appear in the query results.** Every number, name, metric, and comparison in your response must trace back to a specific row/column from the data.\n- **If a field is NULL or missing, say so explicitly.** Do NOT invent values.\n- **If the data doesn't answer part of the question, say so.**\n- **Be concise** — answer the question directly, then provide supporting details.\n- **Be precise with numbers** — use the exact values from the data, not approximations.\n\n### What you MUST NOT do:\n- **Never fabricate details** that did not appear in query results\n- **Never invent numeric values** not returned by a query\n- **Never present NULL/missing data as if it has a value**\n- **Never create step-by-step procedures or action plans** from general knowledge\n- **Never headline a derived roll-up that isn't a column.** Do not compute a new total / average / sum / ratio across rows and present it as the answer unless that value appears as its OWN column in the data. The components rendered beside your text show raw columns — a derived number that exists only in your prose will disagree with the chart/KPI next to it. If a roll-up matters, it is already a column the script returned; cite that column instead of recomputing.\n\n### When data is insufficient:\n- State clearly what the data DOES show vs what it DOESN'T\n- If the question cannot be answered from the available data, say so directly\n\n## Global Knowledge Base\n{{GLOBAL_KNOWLEDGE_BASE}}\n\n---\n\n## CONTEXT (for this specific request)\n\n### Knowledge Base Context\n{{KNOWLEDGE_BASE_CONTEXT}}\n\n### Conversation History\n{{CONVERSATION_HISTORY}}\n\n### Query Results\n{{DATA_CONTEXT}}`,\n\t\tuser: `Analyze the data above and answer this question: \"{{USER_PROMPT}}\"`\n\t},\n\t'script-components': {\n\t\tsystem: `You turn a script's OUTPUT DATA into the MINIMAL set of UI components that directly answer the user's question. Represent ONLY the data the script returned — never invent metrics, recompute or derive columns, or add visualizations the question did not ask for. Faithful and minimal beats comprehensive.\n\n## Available Components\n{{AVAILABLE_COMPONENTS}}\n\n## Rules\n- **HONOR AN EXPLICIT CHART-TYPE REQUEST FIRST — it overrides every data-shape default below.** If the user's question names a chart type (\"bar chart\", \"line chart\", \"pie chart\", \"table\", etc.), the answer component AND every per-measure chart MUST be that exact type. Example: a \"day-wise **bar chart**\" request → a **BarChart** per measure, NOT a LineChart, even though the data is a date+numeric time series. Only fall back to the data-shape guidelines below when the user did NOT name a type.\n- **FIRST, identify the single best component that directly answers the user's question — the ANSWER COMPONENT.** Choose its type from the data shape (UNLESS the user named a type — then use that):\n - Single aggregate value (1 row, 1-2 cols) → KPICard\n - Categorical breakdown (category + numeric) → BarChart or PieChart\n - Time series (date + numeric) → LineChart\n - Detailed records (3+ cols) → DataTable\n Set \\`hasAnswerComponent: true\\` and \\`answerComponent\\` to it, and ALSO include it as the **FIRST** entry in \\`components\\` (do not omit it from the list).\n- **If the data has a DATE/time column + numeric measure(s) AND the user did NOT name a chart type, the ANSWER COMPONENT is a LineChart — NOT a DataTable**, even when the data also has many columns (a DataTable can still be added as a SECONDARY detail view). \"Day-wise\", \"trend\", \"over time\", \"vs … by day\" mean LineChart **by default** — but if the user explicitly asked for a bar chart (or any other type), use THAT type instead (see the explicit-chart-type rule above).\n - **Multi-group/multi-series time data**: set \\`seriesKey\\` to the single TEXT column that labels each line/series (e.g. a combined \\`series\\` column like \"Anjaw — JSW\"). \\`seriesKey\\` must be a TEXT column, different from \\`xAxisKey\\`. Do NOT set \\`seriesKey\\` to a numeric/measure column.\n - **Multiple measures** (e.g. WSP and RSP): emit ONE chart per measure — of the user's requested type (BarChart if they asked for a bar chart, else LineChart): one chart \\`yAxisKey=WSP\\`, another \\`yAxisKey=RSP\\` — do not cram both onto one axis.\n - \\`xAxisKey\\` is the DATE column; \\`yAxisKey\\` is the numeric measure.\n- **Let the user's question and the returned data decide how many components — there is NO fixed limit.** Match the ask:\n - A focused/decision question (\"should we…\", \"what is the gap…\", \"is there an opportunity…\") → a focused answer (often the markdown + one component that shows the key number/result).\n - An analysis / overview / breakdown / \"show me…\" request, or rich multi-dimensional data → use AS MANY components as needed to faithfully represent the results across their dimensions (e.g. a trend + a breakdown + a detail table).\n Do NOT pad with speculative views the question didn't ask for, and do NOT under-represent the data either. The driver is the question + the data, not a target count.\n- **Represent ONLY the data the script returned.** Do NOT create new data, recompute/derive columns, or invent metrics. If the script already computed the answer (e.g. a gap, a recommendation), just display that — don't re-derive it.\n- **ALWAYS include exactly one DynamicMarkdownBlock** — REQUIRED in every response. It summarizes the findings + the direct answer/recommendation in 2-4 concise markdown bullets, grounded in the ANALYSIS below. It does NOT count toward the component count (it is additional). If there is no chartable data, the response may be just the DynamicMarkdownBlock.\n - If the ANALYSIS states a **\"Method & filters\"** note (aggregation used, evidential basis, scope/grain, exclusions), carry it into the markdown as a final one-line italic footnote (e.g. \\`_Method & filters: mode (mean fallback ≤3 obs); all observations (23% evidential); per district × day; WSP/RSP=0 excluded._\\`) so the user always sees how the numbers were produced. Do not invent filters — copy only what the analysis states.\n- **DynamicMarkdownBlock**: put the narrative/summary text in \\`props.content\\` (a markdown string) — NOT under \\`props.config\\`. This is the right component for the answer write-up.\n- **DynamicDataTable**: put columns in \\`props.config.columns\\` as objects \\`[{ \"key\": \"<exact data column>\", \"label\": \"<Heading>\" }]\\`, or omit \\`columns\\` to show all.\n- For each component, specify:\n - componentId: exact ID from the available list\n - componentName: the component name\n - sourceIndex: which source's data to use (0-based index from the data sources below)\n - config: axis mappings, title, description using the EXACT column names from that source's data\n- Each component binds to ONE source's data via its \\`sourceIndex\\`. The script's SQL is reused via queryId — do NOT write new SQL. Just specify the sourceIndex.\n- Always use the exact column names from the data — do NOT reference columns that aren't listed in \"Data Returned\" below\n- When the script computed derived columns (percentages, ratios, gaps, etc.), they appear as their own source — usually labelled \"Script final dataset\" — alongside the raw SQL sources. Pick that source's sourceIndex when the user's question is about the computed values. The script already did any cross-source joins/derivations in code; just display the result — never re-derive or recompute it.\n\n## Component Selection Guidelines (pick the ONE that fits the answer)\n- For ranking/top-N data: horizontal BarChart\n- For time series data: LineChart\n- For part-of-whole data (< 8 categories): PieChart\n- For detailed records or many columns: DataTable\n- For single aggregate values / a single decision or metric: KPICard (or just the markdown)\n- Add a DataTable when the result has detailed rows the user would want to inspect — skip it for a single-value / already-summarized answer.\n- Use multiple visualizations when the question or the data warrants it (an analysis/overview often needs several — e.g. a trend, a breakdown, and a detail table). Add each one because it shows a distinct, asked-for view of the returned data — never as filler.\n\n## CRITICAL: Axis Key Mapping for BarChart (and LineChart)\n- **xAxisKey is ALWAYS the CATEGORY/label column** — a TEXT column (the thing you're grouping BY)\n- **yAxisKey is ALWAYS the NUMERIC/value column** — a NUMBER column (the measured quantity)\n- **DO NOT swap these keys based on orientation.** Orientation only rotates the visual layout; the key roles stay the same.\n- Swapping them causes parseFloat on a text column → NaN → empty chart.\n\n## PieChart Axis Keys\n- \\`nameKey\\` = category column (TEXT)\n- \\`valueKey\\` = numeric column (NUMBER)\n\n## KPICard Value\n- \\`valueKey\\` = the column whose value to display (usually a NUMBER column)\n- **If the data has MORE THAN ONE ROW** and the card is a roll-up over all rows (title says \"total\", \"average\", \"overall\", \"count\", \"all ...\"), you MUST set an aggregation — otherwise the card shows ONLY the first row's value:\n - \\`aggregation\\`: one of \\`\"sum\"\\`, \\`\"avg\"\\`, \\`\"count\"\\`, \\`\"min\"\\`, \\`\"max\"\\`\n - \\`aggregationField\\`: the NUMBER column to aggregate (omit only for \\`\"count\"\\`)\n- Example: \"Total Revenue (all branches)\" over 20 branch rows → \\`{ \"valueKey\": \"Revenue\", \"aggregation\": \"sum\", \"aggregationField\": \"Revenue\" }\\`\n- If the number to show is the SAME on every row (an invariant the script already computed), no aggregation is needed — \\`valueKey\\` alone is fine.\n\n## WaterfallChart Keys\n- \\`categoryKey\\` = step/label column (TEXT)\n- \\`valueKey\\` = numeric column (NUMBER)\n\n## Component prop shapes — use EXACTLY these\nEach component reads its props from a specific place. Match these shapes exactly, or the component renders blank / its demo placeholder.\n\n- **DynamicMarkdownBlock** — the narrative / answer write-up; NO data fetch. Text goes in \\`props.content\\` (a markdown string). No \\`sourceIndex\\`, no \\`config\\`, no \\`externalTool\\`:\n \\`{ \"componentId\": \"...\", \"componentName\": \"DynamicMarkdownBlock\", \"props\": { \"content\": \"## Heading\\\\n\\\\n- point one\\\\n- point two\" } }\\`\n- **DynamicDataTable** — columns are \\`{key,label}\\` objects in \\`props.config.columns\\` (or omit \\`columns\\` to show all):\n \\`{ \"componentId\": \"...\", \"componentName\": \"DynamicDataTable\", \"sourceIndex\": 0, \"props\": { \"title\": \"...\", \"description\": \"...\", \"config\": { \"columns\": [ { \"key\": \"<exact data column>\", \"label\": \"<Heading>\" } ] } } }\\`\n- **DynamicBarChart / DynamicLineChart** — \\`props.config\\`: \\`{ \"xAxisKey\": \"<TEXT col>\", \"yAxisKey\": \"<NUMBER col>\", \"seriesKey\": \"<optional group col>\" }\\`\n- **DynamicPieChart** — \\`props.config\\`: \\`{ \"nameKey\": \"<TEXT col>\", \"valueKey\": \"<NUMBER col>\" }\\`\n- **DynamicKPICard** — \\`props.config\\`: \\`{ \"valueKey\": \"<NUMBER col>\", \"aggregation\": \"sum|avg|count|min|max\", \"aggregationField\": \"<NUMBER col>\" }\\`\n- **DynamicWaterfallChart** — \\`props.config\\`: \\`{ \"categoryKey\": \"<TEXT col>\", \"valueKey\": \"<NUMBER col>\" }\\`\n\nAll DATA components (table, charts, KPI) need \\`sourceIndex\\` and receive rows via queryId automatically — NEVER put \\`data\\`, \\`sql\\`, or \\`query\\` in props. Only \\`DynamicMarkdownBlock\\` carries its own text (\\`props.content\\`).\n\n## Response Format\nReturn JSON. Shape each component per the section above (markdown uses \\`props.content\\`; data components use \\`props.config\\`). **Put \\`hasAnswerComponent\\` and \\`answerComponent\\` FIRST** (they are streamed to the UI early, before the rest of the dashboard):\n{\n \"hasAnswerComponent\": true,\n \"answerComponent\": {\n \"componentId\": \"...\",\n \"componentName\": \"...\",\n \"sourceIndex\": 0,\n \"props\": {\n \"title\": \"...\",\n \"description\": \"...\",\n \"config\": { \"xAxisKey\": \"...\", \"yAxisKey\": \"...\", ... }\n }\n },\n \"layoutTitle\": \"...\",\n \"layoutDescription\": \"...\",\n \"components\": [\n { \"componentId\": \"<the answerComponent, repeated as the FIRST entry>\", \"componentName\": \"...\", \"sourceIndex\": 0, \"props\": { \"title\": \"...\", \"config\": { \"xAxisKey\": \"...\", \"yAxisKey\": \"...\" } } },\n { \"componentId\": \"...\", \"componentName\": \"DynamicMarkdownBlock\", \"props\": { \"content\": \"## Recommendation\\\\n\\\\n- key point\\\\n- key point\" } },\n { \"componentId\": \"...\", \"componentName\": \"DynamicDataTable\", \"sourceIndex\": 0, \"props\": { \"title\": \"...\", \"config\": { \"columns\": [ { \"key\": \"<exact data column>\", \"label\": \"<Heading>\" } ] } } }\n ]\n}\n\n---\n\n## CONTEXT (for this specific request)\n\n### Analysis (the answer the script produced — ground your components and the markdown summary in this; do not contradict or re-derive it)\n{{ANALYSIS_TEXT}}\n\n### Data Returned\n{{COLUMN_DESCRIPTIONS}}`,\n\t\tuser: `User question: \"{{USER_PROMPT}}\"\n\nPick the best components to display this data.`\n\t},\n};\n\n/**\n * Fallback mirror of backend/.prompts/database-rules/<engine>.md.\n * PromptLoader uses these only when the .md files are absent. Keyed by the\n * engine name PromptLoader resolves to (postgresql | mssql | duckdb).\n */\nexport const DATABASE_RULES: Record<string, string> = {\n\t'postgresql': `# PostgreSQL - Query Rules\n\n## CRITICAL ERRORS (Query Will Fail)\n\n### 1. Aggregate Nesting is Forbidden\nPostgreSQL cannot evaluate an aggregate function that contains another aggregate, a subquery, or any expression using an aggregate.\n\n**Invalid patterns:**\n- Aggregate inside aggregate: \\`AVG(MAX(...))\\`, \\`SUM(COUNT(...))\\`, \\`MIN(SUM(...))\\`\n- Aggregate inside function inside aggregate: \\`AVG(DATE_PART(..., MAX(...)))\\`\n- Aggregate in CASE inside aggregate: \\`SUM(CASE WHEN COUNT(...) > 0 THEN ...)\\`\n\n**Solution:** Use a subquery or CTE to compute inner aggregates first, then apply outer aggregate to the result.\n\n### 2. Aggregates Cannot Appear in GROUP BY\nGROUP BY expressions cannot contain any aggregate function - neither directly nor inside CASE, functions, or other expressions.\n\n**Invalid patterns:**\n- Direct: \\`GROUP BY MAX(column)\\`\n- Inside CASE: \\`GROUP BY CASE WHEN MAX(...) > value THEN ... END\\`\n- Inside function: \\`GROUP BY DATE_PART('day', MAX(...))\\`\n\n**Solution:** Compute aggregates in a subquery first, expose as a column, then GROUP BY that column in the outer query.\n\n### 3. Aggregates Cannot Appear in WHERE\nWHERE clause cannot reference any aggregate function, even nested inside other functions.\n\n**Invalid:** \\`WHERE COUNT(*) > 0\\`, \\`WHERE SUM(x) > 100\\`\n\n**Solution:** Use HAVING after GROUP BY for aggregate conditions, or filter via subquery.\n\n### 4. Scalar Subquery Requirement\nSubqueries in SELECT clause must return exactly one row and one column.\n\n**Solution:** Use LIMIT 1, or ensure aggregation guarantees single value.\n\n### 5. SELECT-GROUP BY Consistency\nEvery non-aggregated column in SELECT must appear in GROUP BY. Omitting columns causes \"must appear in GROUP BY clause\" error.\n\n---\n\n## REQUIRED PATTERNS\n\n### Row Limits\nAlways end every SELECT with a LIMIT clause, using the exact row count given in the query-generation row-limit instruction (do NOT invent a larger number — the system enforces that cap regardless, so a bigger LIMIT only produces a misleading query).\n\n### Table Aliases\nAlways prefix columns with table alias in multi-table queries to avoid ambiguity.\n\n### Balanced Parentheses\nEvery \\`(\\` must have a matching \\`)\\`. Verify carefully in nested subqueries and CASE expressions.\n\n### ORDER BY Placement\n- ORDER BY is valid inside subqueries only when paired with LIMIT\n- Can use column aliases in ORDER BY\n\n### Column Alias References\n- Cannot use column aliases in WHERE or GROUP BY (use the original expression)\n- Can use column aliases in ORDER BY and HAVING\n\n### String Comparisons\n- Always use \\`ILIKE\\` with \\`%\\` wildcards instead of \\`=\\` for string comparisons and matching to ensure case-insensitive queries, partial matching, and handle variable inputs gracefully.\n - **Incorrect (no wildcards):** \\`col ILIKE 'value'\\`\n - **Correct (with wildcards):** \\`col ILIKE '%value%'\\`\n\n---\n\n## SYNTAX REFERENCE\n\n| Feature | PostgreSQL Syntax |\n|---------|-------------------|\n| Row limit | \\`SELECT ... LIMIT n\\` |\n| Pagination | \\`LIMIT n OFFSET m\\` |\n| Boolean | \\`true\\`, \\`false\\` — lowercase, no quotes |\n| NULL check | \\`IS NULL\\`, \\`IS NOT NULL\\` — never \\`= NULL\\` or \\`= 'null'\\` |\n| String escape | \\`'O''Brien'\\` — double single-quotes |\n| Current datetime | \\`NOW()\\` or \\`CURRENT_TIMESTAMP\\` |\n| Current date only | \\`CURRENT_DATE\\` |\n| Date arithmetic | \\`date_col + INTERVAL '7 days'\\` or \\`date_col - INTERVAL '1 month'\\` |\n| Date difference | \\`date1 - date2\\` returns integer days, or \\`AGE(date1, date2)\\` |\n| Date extract | \\`EXTRACT(unit FROM date_col)\\` or \\`DATE_PART('unit', date_col)\\` |\n| Date format | \\`TO_CHAR(date_col, 'YYYY-MM-DD')\\` |\n| Date truncate | \\`DATE_TRUNC('month', date_col)\\` |\n| String concat | \\`a || b\\` or \\`CONCAT(a, b)\\` |\n| Type conversion | \\`col::type\\` or \\`CAST(col AS type)\\` |\n| Conditional | \\`CASE WHEN cond THEN val ELSE val END\\` |\n| Coalesce | \\`COALESCE(a, b, c)\\` |\n| String contains | \\`column LIKE '%text%'\\` |\n| Case-insensitive | \\`column ILIKE '%text%'\\` |\n| Insert with return | \\`INSERT ... RETURNING *\\` |\n| Upsert | \\`ON CONFLICT (col) DO UPDATE SET col = EXCLUDED.col\\` |\n\n---\n\n## COMMON PITFALLS\n\n1. **Division by zero** - Wrap with \\`NULLIF(denominator, 0)\\` to return NULL instead of error\n2. **Integer division** - \\`5/2 = 2\\` — cast to NUMERIC for precise division: \\`5::NUMERIC/2 = 2.5\\`\n3. **String comparison** - PostgreSQL is case-sensitive by default; use ILIKE or LOWER() for case-insensitive\n4. **Date strings** - Use ISO format 'YYYY-MM-DD' or explicit casting\n5. **Empty string vs NULL** - They are different in PostgreSQL; check for both when needed\n6. **Array syntax** - Use \\`ARRAY[...]\\` or \\`'{...}'::type[]\\`\n\n---\n\n## PERFORMANCE\n\n- Avoid \\`SELECT *\\` — specify only needed columns\n- Use indexed columns in WHERE and JOIN conditions\n- Prefer JOINs over correlated subqueries when possible\n- Limit result sets appropriately\n- Use EXPLAIN ANALYZE to verify query plans`,\n\t'mssql': `# Microsoft SQL Server - Query Rules\n\n## CRITICAL ERRORS (Query Will Fail)\n\n### 1. Aggregate Nesting is Forbidden\nMSSQL cannot evaluate an aggregate function that contains another aggregate, a subquery, or any expression using an aggregate.\n\n**Invalid patterns:**\n- Aggregate inside aggregate: \\`AVG(MAX(...))\\`, \\`SUM(COUNT(...))\\`, \\`MIN(SUM(...))\\`\n- Aggregate inside function inside aggregate: \\`AVG(DATEDIFF(..., MAX(...), ...))\\`\n- Aggregate in CASE inside aggregate: \\`SUM(CASE WHEN COUNT(...) > 0 THEN ...)\\`\n\n**Solution:** Use a subquery or CTE to compute inner aggregates first, then apply outer aggregate to the result.\n\n### 2. Aggregates Cannot Appear in GROUP BY\nGROUP BY expressions cannot contain any aggregate function - neither directly nor inside CASE, functions, or other expressions.\n\n**Invalid patterns:**\n- Direct: \\`GROUP BY MAX(column)\\`\n- Inside CASE: \\`GROUP BY CASE WHEN MAX(...) > value THEN ... END\\`\n- Inside function: \\`GROUP BY DATEDIFF(..., MAX(...), ...)\\`\n\n**Solution:** Compute aggregates in a subquery first, expose as a column, then GROUP BY that column in the outer query.\n\n### 3. Aggregates Cannot Appear in WHERE\nWHERE clause cannot reference any aggregate function, even nested inside other functions.\n\n**Invalid:** \\`WHERE COUNT(*) > 0\\`, \\`WHERE SUM(x) > 100\\`\n\n**Solution:** Use HAVING after GROUP BY for aggregate conditions, or filter via subquery.\n\n### 4. Scalar Subquery Requirement\nSubqueries in SELECT clause must return exactly one row and one column.\n\n**Solution:** Use TOP 1, or ensure aggregation guarantees single value.\n\n### 5. Subqueries with Comparison Operators Must Return Single Value\nWhen a subquery is used with \\`=\\`, \\`!=\\`, \\`<\\`, \\`<=\\`, \\`>\\`, \\`>=\\` operators, it MUST return exactly one row.\n\n**Invalid:**\n- \\`WHERE BranchFK = (SELECT BranchSK FROM DimBranch WHERE BranchName = 'Name')\\` — may return multiple rows\n\n**Valid patterns:**\n- Use \\`TOP 1\\`: \\`WHERE BranchFK = (SELECT TOP 1 BranchSK FROM DimBranch WHERE BranchName = 'Name')\\`\n- Use \\`IN\\` operator: \\`WHERE BranchFK IN (SELECT BranchSK FROM DimBranch WHERE BranchName = 'Name')\\`\n- Use aggregate: \\`WHERE BranchFK = (SELECT MAX(BranchSK) FROM DimBranch WHERE BranchName = 'Name')\\`\n\n**Solution:** Always use \\`TOP 1\\` in subqueries used with comparison operators, or use \\`IN\\` if multiple matches are acceptable.\n\n### 6. Derived Tables Must Have an Alias\nAny subquery used in a FROM clause (derived table) MUST have an alias.\n\n**Invalid:**\n- \\`SELECT * FROM (SELECT col FROM table)\\` — missing alias\n\n**Valid:**\n- \\`SELECT * FROM (SELECT col FROM table) AS subq\\` — has alias \\`subq\\`\n\n**Solution:** Always add an alias after the closing parenthesis of a derived table.\n\n### 7. SELECT-GROUP BY Consistency\nEvery non-aggregated column in SELECT must appear in GROUP BY. Omitting columns causes ambiguous results error.\n\n### 7b. ORDER BY Must Use GROUP BY Expressions or Aggregates\nWhen a query has GROUP BY, ORDER BY can ONLY reference:\n- Expressions that exactly match a GROUP BY expression\n- Column aliases from SELECT\n- Aggregate functions\n\nMSSQL does NOT recognize two CASE expressions as \"the same\" if they produce different values, even if they reference the same column.\n\n**Invalid pattern:**\n\\`\\`\\`sql\nSELECT CASE WHEN MONTH(dt) IN (4,5,6) THEN 'Q1' ... END AS Quarter\nFROM table\nGROUP BY CASE WHEN MONTH(dt) IN (4,5,6) THEN 'Q1' ... END\nORDER BY CASE WHEN MONTH(dt) IN (4,5,6) THEN 1 ... END -- FAILS: different CASE, references raw column\n\\`\\`\\`\n\n**Valid patterns:**\n\\`\\`\\`sql\n-- Option 1: ORDER BY the column alias\nSELECT CASE WHEN MONTH(dt) IN (4,5,6) THEN 'Q1' ... END AS Quarter\n...\nGROUP BY CASE WHEN MONTH(dt) IN (4,5,6) THEN 'Q1' ... END\nORDER BY Quarter\n\n-- Option 2: Use an aggregate\n...\nGROUP BY CASE WHEN MONTH(dt) IN (4,5,6) THEN 'Q1' ... END\nORDER BY MIN(dt)\n\n-- Option 3: Include the ordering expression in GROUP BY\nSELECT CASE ... END AS Quarter, CASE ... END AS SortOrder\nGROUP BY CASE ... END, CASE ... END -- both expressions in GROUP BY\nORDER BY SortOrder\n\\`\\`\\`\n\n**Solution:** Always use column aliases from SELECT in ORDER BY when the query has GROUP BY. This is the simplest and safest approach.\n\n### 8. text, ntext, and image Columns Cannot Be Compared or Sorted\nMSSQL's legacy LOB types (\\`text\\`, \\`ntext\\`, \\`image\\`) do NOT support \\`=\\`, \\`<>\\`, \\`>\\`, \\`<\\`, \\`>=\\`, \\`<=\\`, \\`GROUP BY\\`, \\`ORDER BY\\`, \\`DISTINCT\\`, or \\`UNION\\`. Using these operators on such columns causes: *\"The text, ntext, and image data types cannot be compared or sorted\"*.\n\n**Check the schema's \\`nativeType\\` for each column before writing the query.** If a column has nativeType \\`text\\`, \\`ntext\\`, or \\`image\\`:\n\n**Invalid patterns:**\n- \\`WHERE notes = 'value'\\`\n- \\`ORDER BY description\\`\n- \\`GROUP BY remarks\\`\n- \\`SELECT DISTINCT comments\\`\n\n**Valid patterns (CAST to nvarchar(max) first):**\n- \\`WHERE CAST(notes AS NVARCHAR(MAX)) = 'value'\\`\n- \\`WHERE CAST(notes AS NVARCHAR(MAX)) LIKE '%value%'\\`\n- \\`ORDER BY CAST(description AS NVARCHAR(MAX))\\`\n- \\`GROUP BY CAST(remarks AS NVARCHAR(MAX))\\`\n- \\`SELECT DISTINCT CAST(comments AS NVARCHAR(MAX))\\`\n\n**Solution:** Always wrap \\`text\\`/\\`ntext\\` columns in \\`CAST(column AS NVARCHAR(MAX))\\` before any comparison, sorting, grouping, or DISTINCT. For \\`image\\` columns, avoid comparisons entirely — they are binary data.\n\n### 9. nvarchar to Numeric Conversion Errors\nWhen a column has a string type (\\`nvarchar\\`, \\`varchar\\`, \\`char\\`) but may contain numeric-looking values, using it in arithmetic or numeric comparisons causes: *\"Error converting data type nvarchar to numeric\"*.\n\n**Check the schema's \\`nativeType\\` before numeric operations.** If the column is a string type:\n\n**Invalid patterns:**\n- \\`WHERE price > 100\\` (when \\`price\\` is nvarchar)\n- \\`SUM(amount)\\` (when \\`amount\\` is varchar)\n- \\`column * 1.1\\` (when \\`column\\` is nvarchar)\n\n**Valid patterns (use TRY_CAST for safe conversion):**\n- \\`WHERE TRY_CAST(price AS DECIMAL(18,2)) > 100\\`\n- \\`SUM(TRY_CAST(amount AS DECIMAL(18,2)))\\`\n- \\`TRY_CAST(column AS DECIMAL(18,2)) * 1.1\\`\n\n**Solution:** Always use \\`TRY_CAST(column AS DECIMAL(18,2))\\` or \\`TRY_CONVERT\\` for numeric operations on string-typed columns. \\`TRY_CAST\\` returns NULL instead of raising an error for non-numeric values. Never use plain \\`CAST\\` — it will error on non-numeric rows.\n\n### 10. Date-Integer Type Clash\nMSSQL does NOT allow comparing or combining \\`date\\`/\\`datetime\\` columns with \\`int\\` values directly. This causes: *\"Operand type clash: date is incompatible with int\"*.\n\n**Check the schema's \\`nativeType\\` before date operations.** If a column is \\`date\\`, \\`datetime\\`, \\`datetime2\\`, or \\`smalldatetime\\`:\n\n**Invalid patterns:**\n- \\`WHERE OrderDate = 2024\\` — comparing date with integer\n- \\`WHERE OrderDate > 20240101\\` — integer that looks like a date\n- \\`WHERE OrderDate BETWEEN 2024 AND 2025\\` — integers, not date strings\n- \\`WHERE OrderDate = YEAR(GETDATE())\\` — YEAR() returns int\n- \\`WHERE OrderDate + 30 > GETDATE()\\` — adding int to date without DATEADD\n\n**Valid patterns:**\n- \\`WHERE OrderDate >= '2024-01-01' AND OrderDate < '2025-01-01'\\` — use date string literals\n- \\`WHERE YEAR(OrderDate) = 2024\\` — extract int from date, compare int to int\n- \\`WHERE OrderDate > DATEADD(DAY, -30, GETDATE())\\` — use DATEADD for date arithmetic\n- \\`WHERE OrderDate BETWEEN '2024-01-01' AND '2024-12-31'\\` — string date literals in BETWEEN\n\n**Solution:** Never compare a date column directly with an integer. Either use string date literals (\\`'2024-01-01'\\`) or extract the integer part with \\`YEAR()\\`/\\`MONTH()\\`/\\`DAY()\\` and compare integers to integers.\n\n### 11. ORDER BY is Forbidden in Subqueries Without TOP/OFFSET\nORDER BY cannot appear inside any subquery, CTE, derived table, or inline function unless that same query level also has TOP or OFFSET-FETCH.\n\n**Invalid patterns:**\n- \\`SELECT * FROM (SELECT col FROM table ORDER BY col) t\\` — ORDER BY without TOP\n- Nested subqueries where inner has ORDER BY but no TOP\n\n**Valid patterns:**\n- \\`SELECT * FROM (SELECT TOP 100 col FROM table ORDER BY col) t\\` — TOP makes ORDER BY valid\n- Place ORDER BY only in the outermost SELECT statement\n- If you need ordered data in a subquery, you MUST add TOP to that subquery\n\n**Solution:** Either add TOP to the subquery that has ORDER BY, or move ORDER BY to the outermost query.\n\n### 12. UNION / UNION ALL — Row Limits and ORDER BY\nIn UNION queries, \\`TOP\\` and \\`ORDER BY\\` can ONLY appear in the **outermost query** or in subqueries wrapped in parentheses.\n\n**Invalid patterns:**\n\\`\\`\\`sql\n-- TOP in individual UNION branches (ambiguous syntax)\nSELECT TOP 10 col FROM tableA\nUNION ALL\nSELECT TOP 10 col FROM tableB\n\n-- ORDER BY in individual UNION branches\nSELECT col FROM tableA ORDER BY col\nUNION ALL\nSELECT col FROM tableB\n\\`\\`\\`\n\n**Valid patterns:**\n\\`\\`\\`sql\n-- Option 1: Wrap entire UNION in a subquery, apply TOP/ORDER BY outside\nSELECT TOP 10 * FROM (\n SELECT col FROM tableA\n UNION ALL\n SELECT col FROM tableB\n) AS combined\nORDER BY col\n\n-- Option 2: Use CTEs\n;WITH combined AS (\n SELECT col FROM tableA\n UNION ALL\n SELECT col FROM tableB\n)\nSELECT TOP 10 * FROM combined ORDER BY col\n\\`\\`\\`\n\n**Solution:** Always apply TOP and ORDER BY to the outermost query when using UNION/UNION ALL, not to individual branches.\n\n---\n\n## REQUIRED PATTERNS\n\n### Row Limits\nAlways limit results with TOP clause. Default to 10 rows if user doesn't specify, max 10 rows\n\n### Table Aliases\nAlways prefix columns with table alias in multi-table queries to avoid ambiguity.\n\n### Balanced Parentheses\nEvery \\`(\\` must have a matching \\`)\\`. Verify carefully in nested subqueries and CASE expressions.\n\n### Column Alias References\n- Cannot use column aliases in WHERE (use the original expression)\n- Can use column aliases in ORDER BY\n\n---\n\n## SYNTAX REFERENCE\n\n| Feature | MSSQL Syntax |\n|---------|--------------|\n| Row limit | \\`SELECT TOP n ...\\` |\n| Boolean | \\`1\\` (true), \\`0\\` (false) — no \\`TRUE\\`/\\`FALSE\\` keywords |\n| NULL check | \\`IS NULL\\`, \\`IS NOT NULL\\` — never \\`= NULL\\` or \\`= 'null'\\` |\n| String escape | \\`'O''Brien'\\` — double single-quotes |\n| Current datetime | \\`GETDATE()\\` |\n| Current date only | \\`CAST(GETDATE() AS DATE)\\` |\n| Date arithmetic | \\`DATEADD(unit, count, date)\\` — units: day, month, year, hour, etc. |\n| Last day of month | \\`EOMONTH(date)\\` — returns the last day of that month (handles Feb 28/29 etc.) |\n| Date difference | \\`DATEDIFF(unit, start_date, end_date)\\` |\n| String concat | \\`CONCAT(a, b, ...)\\` or \\`a + b\\` |\n| Type conversion | \\`CAST(expr AS type)\\` or \\`CONVERT(type, expr)\\` |\n| Conditional | \\`CASE WHEN cond THEN val ELSE val END\\` |\n| Coalesce | \\`COALESCE(a, b, c)\\` or \\`ISNULL(a, b)\\` |\n| String contains | \\`column LIKE '%text%'\\` |\n| Case-insensitive | Use \\`LOWER()\\` or \\`COLLATE\\` for explicit case handling |\n| Insert with return | \\`INSERT ... OUTPUT INSERTED.*\\` |\n| Pagination | \\`OFFSET n ROWS FETCH NEXT m ROWS ONLY\\` (requires ORDER BY) |\n\n---\n\n## COMMON PITFALLS\n\n1. **Division by zero** - Wrap with \\`NULLIF(denominator, 0)\\` to return NULL instead of error\n2. **String to number** - Always check schema nativeType. Use \\`TRY_CAST(col AS DECIMAL(18,2))\\` for numeric operations on string columns — never plain \\`CAST\\`\n3. **Date format** - Use \\`FORMAT(date, 'yyyy-MM-dd')\\` for consistent string representation\n3b. **Month-end dates** - NEVER hardcode month-end days (e.g., \\`'2025-02-29'\\`, \\`'2025-04-31'\\`). Use \\`EOMONTH('2025-02-01')\\` to get the correct last day. Invalid date literals cause: *\"Conversion failed when converting date/time from character string\"*\n4. **Integer division** - \\`5/2 = 2\\` — cast to DECIMAL for precise division: \\`CAST(5 AS DECIMAL)/2 = 2.5\\`\n5. **Empty string vs NULL** - They are different in MSSQL; check for both when needed\n6. **OPENJSON column references** - When using OPENJSON, you MUST use JSON_VALUE() to extract values in both SELECT and GROUP BY:\n - **Invalid:** \\`SELECT MajorCode FROM OPENJSON('[...]')\\`\n - **Valid:** \\`SELECT JSON_VALUE(value, '$.MajorCode') as MajorCode FROM OPENJSON('[...]')\\`\n - In GROUP BY: \\`GROUP BY JSON_VALUE(value, '$.MajorCode')\\`\n7. **Balanced parentheses** - Always verify parentheses match, especially with nested subqueries and OPENJSON\n8. **Reserved keyword aliases** - Never use MSSQL reserved keywords as column aliases. Common offenders: \\`RowCount\\`, \\`Key\\`, \\`Value\\`, \\`Index\\`, \\`Plan\\`, \\`User\\`, \\`Name\\`, \\`Type\\`, \\`Status\\`, \\`Date\\`, \\`Order\\`, \\`Group\\`, \\`Table\\`, \\`Column\\`, \\`Count\\`, \\`Sum\\`, \\`Percent\\`, \\`Transaction\\`. Use descriptive alternatives instead: \\`TotalRows\\`, \\`ItemKey\\`, \\`ItemValue\\`, \\`RecordCount\\`, \\`TotalCount\\`, \\`PctValue\\`, etc.\n\n---\n\n## PERFORMANCE\n\n- Avoid \\`SELECT *\\` — specify only needed columns\n- Use indexed columns in WHERE and JOIN conditions\n- Prefer JOINs over correlated subqueries when possible\n- Limit result sets appropriately\n- **Date filtering: NEVER wrap date columns in functions** like \\`YEAR(col)\\` or \\`MONTH(col)\\` in WHERE — this prevents index usage and causes full table scans. Use range comparisons instead: \\`WHERE col >= '2025-04-01' AND col < '2026-04-01'\\`\n- **Avoid FULL OUTER JOIN** — it is almost never needed and is extremely slow on large tables. Use LEFT JOIN or INNER JOIN instead\n- **Avoid OR across different columns in WHERE** — each OR branch causes a separate scan. Split into UNION ALL if needed, or restructure the query`,\n\t'duckdb': `# DuckDB - Query Rules\n\n## CRITICAL ERRORS (Query Will Fail)\n\n### 1. INTERVAL Cannot Be Cast to INTEGER or DOUBLE\nSubtracting two dates/timestamps in DuckDB returns an \\`INTERVAL\\`, NOT a number. You CANNOT cast an INTERVAL to INTEGER or DOUBLE.\n\n**Invalid patterns:**\n- \\`CAST(date1 - date2 AS INTEGER)\\` — fails with \"Unimplemented type for cast (INTERVAL -> INTEGER)\"\n- \\`CAST(date1 - date2 AS DOUBLE)\\` — fails with \"Unimplemented type for cast (INTERVAL -> DOUBLE)\"\n- \\`(dispatch_date - placement_date)::INTEGER\\` — same error\n\n**Valid patterns:**\n- \\`DATE_DIFF('day', date2, date1)\\` — returns integer number of days between two dates\n- \\`DATE_DIFF('hour', date2, date1)\\` — returns integer number of hours\n- \\`EXTRACT(EPOCH FROM (date1 - date2)) / 86400\\` — alternative for fractional days\n\n**Solution:** Always use \\`DATE_DIFF('unit', start_date, end_date)\\` for date arithmetic that needs a numeric result. The unit MUST be a quoted string.\n\n### 2. DATEDIFF / DATE_DIFF Unit Must Be a Quoted String\nThe date part argument in \\`DATE_DIFF\\` and \\`DATEDIFF\\` MUST be a quoted string literal, NOT a bare identifier.\n\n**Invalid patterns:**\n- \\`DATEDIFF(day, date1, date2)\\` — \\`day\\` is interpreted as a column name, fails with \"Referenced column 'day' not found\"\n- \\`DATEDIFF(month, date1, date2)\\` — same error\n- \\`DATE_PART(year, date_col)\\` — same error\n\n**Valid patterns:**\n- \\`DATEDIFF('day', date1, date2)\\` — unit as quoted string\n- \\`DATE_DIFF('day', date1, date2)\\` — preferred DuckDB syntax\n- \\`DATE_PART('year', date_col)\\` — unit as quoted string\n- \\`EXTRACT(DAY FROM interval_value)\\` — EXTRACT syntax uses unquoted keywords (this is different)\n\n**Solution:** ALWAYS quote the unit argument in DATE_DIFF, DATEDIFF, DATE_PART, DATE_TRUNC, and similar functions: \\`'day'\\`, \\`'month'\\`, \\`'year'\\`, \\`'hour'\\`, \\`'minute'\\`, \\`'second'\\`.\n\n### 3. TRY_CAST(string AS DATE) Silently Fails on Non-ISO Dates\n\\`TRY_CAST(col AS DATE)\\` only recognizes ISO format (\\`YYYY-MM-DD\\`). For ANY other date format (e.g., \\`DD-MM-YYYY\\`, \\`DD/MM/YYYY\\`, \\`DD Mon YYYY\\`), TRY_CAST silently returns **NULL** — it does NOT error, it just gives NULL for every row. This leads to incorrect results (e.g., 0% match rates) without any error message.\n\n**Invalid patterns (silently wrong results):**\n- \\`TRY_CAST(\"Actual Date of Arrival\" AS DATE)\\` — returns NULL if column contains \\`02-04-2025\\` (DD-MM-YYYY)\n- \\`CAST(col AS DATE)\\` — errors on non-ISO formats\n- \\`TRY_CAST(SUBSTR(col, 1, 10) AS DATE)\\` — only works if the substring is already \\`YYYY-MM-DD\\`\n\n**Valid patterns:**\n- \\`TRY_STRPTIME(col, '%d-%m-%Y')::DATE\\` — explicitly parse DD-MM-YYYY format, then cast to DATE\n- \\`TRY_STRPTIME(col, '%d/%m/%Y')::DATE\\` — for DD/MM/YYYY format\n- \\`TRY_STRPTIME(col, '%d %b %Y')::DATE\\` — for \"01 Apr 2025\" format\n- \\`COALESCE(TRY_STRPTIME(col, '%d-%m-%Y'), TRY_STRPTIME(col, '%d/%m/%Y'), TRY_STRPTIME(col, '%Y-%m-%d'))::DATE\\` — handle multiple possible formats\n\n**Solution:** NEVER use \\`TRY_CAST(col AS DATE)\\` or \\`CAST(col AS DATE)\\` for date string columns. ALWAYS use \\`TRY_STRPTIME\\` with the correct format string. Check sample values in the schema to determine the format. If the format is unclear, use COALESCE with multiple TRY_STRPTIME patterns.\n\n### 4. Aggregate Nesting is Forbidden\nDuckDB cannot evaluate an aggregate function that contains another aggregate.\n\n**Invalid patterns:**\n- \\`AVG(MAX(...))\\`, \\`SUM(COUNT(...))\\`, \\`MIN(SUM(...))\\`\n\n**Solution:** Use a subquery or CTE to compute inner aggregates first, then apply outer aggregate to the result.\n\n### 5. Aggregates Cannot Appear in WHERE\nWHERE clause cannot reference any aggregate function.\n\n**Invalid:** \\`WHERE COUNT(*) > 0\\`, \\`WHERE SUM(x) > 100\\`\n\n**Solution:** Use HAVING after GROUP BY for aggregate conditions, or filter via subquery.\n\n### 6. SELECT-GROUP BY Consistency\nEvery non-aggregated column in SELECT must appear in GROUP BY.\n\n### 7. PERCENTILE_CONT Returns DECIMAL, Not DOUBLE\n\\`PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY col)\\` may return a DECIMAL type that doesn't implicitly convert in all contexts.\n\n**Solution:** Wrap with \\`CAST(... AS DOUBLE)\\` if needed for further arithmetic.\n\n---\n\n## REQUIRED PATTERNS\n\n### Row Limits\nAlways end every SELECT with a LIMIT clause, using the exact row count given in the query-generation row-limit instruction (do NOT invent a larger number — the system enforces that cap regardless, so a bigger LIMIT only produces a misleading query).\n\n### Date Parsing from Strings — CRITICAL\nExcel/CSV data has dates as strings in various formats. **NEVER use TRY_CAST(col AS DATE)** — it only works for ISO format and silently returns NULL for everything else, producing wrong results with no error.\n\n**ALWAYS use \\`TRY_STRPTIME\\` with the correct format:**\n- \\`TRY_STRPTIME(col, '%d %b %Y')\\` — \"01 Apr 2025\"\n- \\`TRY_STRPTIME(col, '%d-%m-%Y')\\` — \"01-04-2025\"\n- \\`TRY_STRPTIME(col, '%d/%m/%Y')\\` — \"01/04/2025\"\n- \\`TRY_STRPTIME(col, '%Y-%m-%d')\\` — \"2025-04-01\"\n- \\`TRY_STRPTIME(col, '%Y-%m-%d %H:%M:%S')\\` — \"2025-04-01 07:15:46\"\n- Use \\`COALESCE(TRY_STRPTIME(col, fmt1), TRY_STRPTIME(col, fmt2))\\` to handle multiple possible formats\n- \\`TRY_STRPTIME\\` returns NULL on parse failure instead of error\n- To get a DATE from a parsed timestamp: \\`TRY_STRPTIME(col, '%d-%m-%Y')::DATE\\`\n\n**Determine the format from sample values in the schema.** Different columns in the same table may use different date formats — parse each column with its own format string.\n\n### Time Parsing from Strings\nTime columns in Excel/CSV are often stored as \\`HH:MM\\` strings (e.g., \\`09:57\\`, \\`14:30\\`). To extract the hour as an integer for time-slot grouping, use the simplest approach:\n\n\\`\\`\\`sql\nTRY_CAST(SPLIT_PART(\"Time Column\", ':', 1) AS INTEGER) AS hour_of_day\n\\`\\`\\`\n\nThen use the integer hour for time-slot buckets:\n\\`\\`\\`sql\nCASE\n WHEN hour_of_day >= 0 AND hour_of_day < 6 THEN '00-06 (Night)'\n WHEN hour_of_day >= 6 AND hour_of_day < 12 THEN '06-12 (Morning)'\n WHEN hour_of_day >= 12 AND hour_of_day < 18 THEN '12-18 (Afternoon)'\n WHEN hour_of_day >= 18 AND hour_of_day < 24 THEN '18-24 (Evening)'\nEND\n\\`\\`\\`\n\n**Do NOT use complex nested casts** like \\`TRY_CAST(CAST(STRPTIME(...) AS TIME) AS VARCHAR)\\` for time-slot grouping — it is error-prone and unnecessarily complex. Simple string splitting is more reliable.\n\n### Table Aliases\nAlways prefix columns with table alias in multi-table queries to avoid ambiguity.\n\n### Balanced Parentheses\nEvery \\`(\\` must have a matching \\`)\\`. Verify carefully in nested subqueries and CASE expressions.\n\n### Column Alias References\n- Cannot use column aliases in WHERE (use the original expression)\n- Can use column aliases in ORDER BY and HAVING\n\n---\n\n## SYNTAX REFERENCE\n\n| Feature | DuckDB Syntax |\n|---------|---------------|\n| Row limit | \\`SELECT ... LIMIT n\\` |\n| Pagination | \\`LIMIT n OFFSET m\\` |\n| Boolean | \\`true\\`, \\`false\\` — lowercase, no quotes |\n| NULL check | \\`IS NULL\\`, \\`IS NOT NULL\\` — never \\`= NULL\\` or \\`= 'null'\\` |\n| String escape | \\`'O''Brien'\\` — double single-quotes |\n| Current datetime | \\`NOW()\\` or \\`CURRENT_TIMESTAMP\\` |\n| Current date only | \\`CURRENT_DATE\\` or \\`TODAY()\\` |\n| Date difference (days) | \\`DATE_DIFF('day', start_date, end_date)\\` — returns INTEGER |\n| Date difference (hours) | \\`DATE_DIFF('hour', start_date, end_date)\\` — returns INTEGER |\n| Date arithmetic | \\`date_col + INTERVAL '7 days'\\` or \\`date_col - INTERVAL '1 month'\\` |\n| Date extract | \\`EXTRACT(unit FROM date_col)\\` or \\`DATE_PART('unit', date_col)\\` |\n| Day of week | \\`DAYOFWEEK(date_col)\\` — 0=Sunday, 6=Saturday |\n| Day name | \\`DAYNAME(date_col)\\` — returns 'Monday', 'Tuesday', etc. |\n| Date format | \\`STRFTIME(date_col, '%Y-%m-%d')\\` |\n| Date truncate | \\`DATE_TRUNC('month', date_col)\\` — unit MUST be quoted |\n| Parse date string | \\`TRY_STRPTIME(string, format)\\` — returns NULL on failure |\n| String concat | \\`a \\\\|\\\\| b\\` or \\`CONCAT(a, b)\\` |\n| Type conversion | \\`CAST(col AS type)\\` or \\`col::type\\` |\n| Safe type conversion | \\`TRY_CAST(col AS type)\\` — returns NULL on failure |\n| Conditional | \\`CASE WHEN cond THEN val ELSE val END\\` |\n| Coalesce | \\`COALESCE(a, b, c)\\` |\n| String contains | \\`column LIKE '%text%'\\` |\n| Case-insensitive | \\`column ILIKE '%text%'\\` |\n| Regex match | \\`REGEXP_MATCHES(col, 'pattern')\\` |\n| List aggregation | \\`LIST(col)\\` or \\`ARRAY_AGG(col)\\` |\n| String split | \\`SPLIT_PART(string, delimiter, index)\\` — 1-based index |\n| Median | \\`MEDIAN(col)\\` or \\`PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY col)\\` |\n\n---\n\n## COMMON PITFALLS\n\n1. **Division by zero** - Wrap with \\`NULLIF(denominator, 0)\\` to return NULL instead of error\n2. **Integer division** - \\`5/2 = 2\\` — cast to DOUBLE for precise division: \\`CAST(5 AS DOUBLE)/2 = 2.5\\`\n3. **String comparison** - DuckDB is case-sensitive by default; use ILIKE or LOWER() for case-insensitive\n4. **Date strings** - ALWAYS use \\`TRY_STRPTIME(col, format)\\` to parse dates. NEVER use \\`TRY_CAST(col AS DATE)\\` — it silently returns NULL for non-ISO formats, producing wrong results with no error\n5. **Empty string vs NULL** - They are different; check for both when needed\n6. **Column names with spaces** - Must be quoted with double quotes: \\`\"Column Name\"\\`\n7. **INTERVAL arithmetic** - \\`date1 - date2\\` gives INTERVAL; use \\`DATE_DIFF('day', date2, date1)\\` for a number\n8. **Bare date units** - NEVER use \\`day\\`, \\`month\\`, \\`year\\` without quotes in functions — always \\`'day'\\`, \\`'month'\\`, \\`'year'\\`\n9. **String-to-numeric** - Use \\`TRY_CAST(col AS DOUBLE)\\` for numeric operations on string columns — never plain \\`CAST\\` on user data\n10. **DAYOFWEEK convention** - DuckDB's \\`DAYOFWEEK\\` returns 0=Sunday through 6=Saturday (ISO convention). Use \\`DAYNAME()\\` for readable names.\n11. **Non-existent functions** - DuckDB does NOT have \\`FORMAT_TIMESTAMP\\`, \\`TO_CHAR\\`, or \\`FORMAT_DATE\\`. Use \\`STRFTIME(date_col, format)\\` for date-to-string formatting. Example: \\`STRFTIME(date_col, '%B %Y')\\` → \"April 2025\"\n12. **Month-year grouping** - For month-over-month analysis, use \\`DATE_TRUNC('month', date_col)\\` in GROUP BY and \\`STRFTIME(date_col, '%B %Y')\\` for display. Do NOT group by the formatted string — group by the truncated date for correct sorting.\n13. **COALESCE type mismatch** - All arguments in COALESCE must be the same type. \\`COALESCE(\"varchar_col\", 0)\\` fails because VARCHAR and INTEGER can't mix. Cast first: \\`COALESCE(TRY_CAST(\"varchar_col\" AS DOUBLE), 0)\\`. Same applies to CASE WHEN, BETWEEN, and IN — always ensure consistent types.\n\n---\n\n## PERFORMANCE\n\n- Avoid \\`SELECT *\\` — specify only needed columns\n- Use indexed columns in WHERE and JOIN conditions\n- Prefer JOINs over correlated subqueries when possible\n- Limit result sets appropriately\n- DuckDB is columnar — queries touching fewer columns are significantly faster`,\n};\n","import type { Action } from '../threads/action';\nimport type { DatabaseType } from '../types';\nimport { promptLoader } from './prompt-loader';\n\nexport type { DatabaseType };\n\n/**\n * Gets the current database type from the prompt loader\n * @returns The database type string\n */\nexport function getDatabaseType(): DatabaseType {\n\treturn promptLoader.getDatabaseType() as DatabaseType;\n}\n\n/**\n * Detects the database type from a tool ID prefix (e.g., 'mssql-662cba0bcea8_query' → 'mssql')\n * Falls back to the global database type from prompt loader if no match found.\n * @param toolId - The tool ID string\n * @returns The detected database type\n */\nexport function getDbTypeFromToolId(toolId?: string): DatabaseType {\n\tif (!toolId) return getDatabaseType();\n\tconst prefix = toolId.split('-')[0]?.toLowerCase();\n\t// Normalize variant source types (e.g., mssql_bridge → mssql)\n\tif (prefix.startsWith('mssql')) return 'mssql';\n\tif (prefix.startsWith('postgres')) return 'postgresql';\n\tif (prefix.startsWith('mysql')) return 'mysql';\n\tif (prefix.startsWith('snowflake')) return 'snowflake';\n\treturn getDatabaseType();\n}\n\n/**\n * Converts LIMIT syntax to TOP syntax for MSSQL\n * MSSQL doesn't support LIMIT keyword - it must use TOP\n * @param query - The SQL query to check\n * @param limit - The limit value to use\n * @returns The query with LIMIT converted to TOP\n */\nexport function convertLimitToTop(query: string, limit: number): string {\n\tif (!query || query.trim().length === 0) {\n\t\treturn query;\n\t}\n\n\t// Remove LIMIT clause and add TOP after SELECT\n\tlet modifiedQuery = query.replace(/\\s*\\bLIMIT\\s+\\d+\\b/gi, '').trim();\n\n\t// Add TOP N after SELECT (handles SELECT DISTINCT as well)\n\tmodifiedQuery = modifiedQuery.replace(/^\\s*(SELECT)\\s+(DISTINCT\\s+)?/i, `$1 TOP ${limit} $2`);\n\n\treturn modifiedQuery;\n}\n\n/**\n * Converts TOP syntax to LIMIT syntax for PostgreSQL/Snowflake\n * These databases don't support TOP keyword - they must use LIMIT\n * @param query - The SQL query to check\n * @returns The query with TOP removed (LIMIT will be added separately)\n */\nexport function convertTopToLimit(query: string): string {\n\tif (!query || query.trim().length === 0) {\n\t\treturn query;\n\t}\n\n\t// Remove \"TOP N\" from query - LIMIT will be added at the end\n\t// Pattern: SELECT TOP number or SELECT TOP (number)\n\tlet modifiedQuery = query.replace(/\\bSELECT\\s+TOP\\s+\\(?\\d+\\)?\\s*/gi, 'SELECT ');\n\n\treturn modifiedQuery;\n}\n\n/**\n * Ensures a SQL query has a row limiting clause to prevent large result sets\n * Only applies to SELECT queries - leaves INSERT, UPDATE, DELETE, etc. unchanged\n * Database-aware: Uses TOP for MSSQL, LIMIT for PostgreSQL/Snowflake/MySQL\n * Enforces a maximum limit to prevent excessive data retrieval\n * @param query - The SQL query to check\n * @param defaultLimit - Default limit to apply if none exists (default: 10)\n * @param maxLimit - Maximum allowed limit (default: 100)\n * @param dbType - Database type (optional, defaults to promptLoader's database type)\n * @returns The query with appropriate row limiting clause (if it's a SELECT query)\n */\nexport function ensureQueryLimit(\n\tquery: string,\n\tdefaultLimit: number = 10,\n\tmaxLimit: number = 10,\n\tdbType?: DatabaseType\n): string {\n\tif (!query || query.trim().length === 0) {\n\t\treturn query;\n\t}\n\n\t// Get database type from parameter or prompt loader\n\tconst databaseType = dbType || getDatabaseType();\n\tconst isMssql = databaseType === 'mssql';\n\n\tlet trimmedQuery = query.trim();\n\n\t// Only apply limits to SELECT queries\n\t// Check if the query is a SELECT statement (not INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, etc.)\n\tconst isSelectQuery = /^\\s*SELECT\\b/i.test(trimmedQuery) ||\n\t\t/^\\s*WITH\\b.*\\bSELECT\\b/is.test(trimmedQuery); // Also handle CTEs (WITH clause)\n\n\tif (!isSelectQuery) {\n\t\t// Not a SELECT query, return as-is\n\t\treturn query;\n\t}\n\n\t// Remove any trailing semicolon for processing\n\tconst hadSemicolon = trimmedQuery.endsWith(';');\n\tif (hadSemicolon) {\n\t\ttrimmedQuery = trimmedQuery.slice(0, -1).trim();\n\t}\n\n\tif (isMssql) {\n\t\t// MSSQL: Use TOP syntax\n\t\treturn ensureMssqlTop(trimmedQuery, defaultLimit, maxLimit, hadSemicolon);\n\t} else {\n\t\t// PostgreSQL/Snowflake/MySQL: Use LIMIT syntax\n\t\treturn ensurePostgresLimit(trimmedQuery, defaultLimit, maxLimit, hadSemicolon);\n\t}\n}\n\n/**\n * Ensures MSSQL query has TOP clause for row limiting\n */\nfunction ensureMssqlTop(query: string, defaultLimit: number, maxLimit: number, hadSemicolon: boolean): string {\n\tlet trimmedQuery = query;\n\n\t// First, remove any LIMIT clauses (might be added by LLM incorrectly)\n\ttrimmedQuery = trimmedQuery.replace(/\\s*\\bLIMIT\\s+\\d+\\b/gi, '').trim();\n\n\t// Check if TOP already exists\n\tconst topMatch = trimmedQuery.match(/\\bSELECT\\s+TOP\\s+\\(?\\s*(\\d+)\\s*\\)?/i);\n\n\tif (topMatch) {\n\t\tconst existingTop = parseInt(topMatch[1], 10);\n\n\t\t// If existing TOP is within limits, keep it\n\t\tif (existingTop <= maxLimit) {\n\t\t\tif (hadSemicolon) {\n\t\t\t\ttrimmedQuery += ';';\n\t\t\t}\n\t\t\treturn trimmedQuery;\n\t\t}\n\n\t\t// If existing TOP exceeds maxLimit, replace it\n\t\tconsole.warn(`⚠️ Query TOP ${existingTop} exceeds maximum of ${maxLimit}. Reducing to ${maxLimit}...`);\n\t\ttrimmedQuery = trimmedQuery.replace(/\\bSELECT\\s+TOP\\s+\\(?\\s*\\d+\\s*\\)?/i, `SELECT TOP ${maxLimit}`);\n\n\t\tif (hadSemicolon) {\n\t\t\ttrimmedQuery += ';';\n\t\t}\n\t\treturn trimmedQuery;\n\t}\n\n\t// No TOP exists, add it after SELECT (handle SELECT DISTINCT)\n\ttrimmedQuery = trimmedQuery.replace(\n\t\t/^\\s*(SELECT)\\s+(DISTINCT\\s+)?/i,\n\t\t`$1 TOP ${defaultLimit} $2`\n\t);\n\n\tif (hadSemicolon) {\n\t\ttrimmedQuery += ';';\n\t}\n\treturn trimmedQuery;\n}\n\n/**\n * Ensures PostgreSQL/Snowflake/MySQL query has LIMIT clause for row limiting\n */\nfunction ensurePostgresLimit(query: string, defaultLimit: number, maxLimit: number, hadSemicolon: boolean): string {\n\tlet trimmedQuery = query;\n\n\t// First, convert any TOP syntax to standard format (remove TOP, will add LIMIT)\n\ttrimmedQuery = convertTopToLimit(trimmedQuery);\n\n\t// Remove any duplicate LIMIT clauses\n\tconst limitMatches = trimmedQuery.match(/\\bLIMIT\\s+(\\d+)\\b/gi);\n\n\tif (limitMatches && limitMatches.length > 0) {\n\t\tif (limitMatches.length > 1) {\n\t\t\t// Multiple LIMIT clauses, remove all and add fresh one\n\t\t\tconsole.warn(`⚠️ Query had ${limitMatches.length} LIMIT clauses. Removing duplicates...`);\n\t\t\ttrimmedQuery = trimmedQuery.replace(/\\s*\\bLIMIT\\s+\\d+\\b/gi, '').trim();\n\t\t} else {\n\t\t\t// Single LIMIT exists - check if it exceeds maxLimit\n\t\t\tconst existingLimitMatch = trimmedQuery.match(/\\bLIMIT\\s+(\\d+)\\b/i);\n\t\t\tif (existingLimitMatch) {\n\t\t\t\tconst existingLimit = parseInt(existingLimitMatch[1], 10);\n\n\t\t\t\tif (existingLimit <= maxLimit) {\n\t\t\t\t\tif (hadSemicolon) {\n\t\t\t\t\t\ttrimmedQuery += ';';\n\t\t\t\t\t}\n\t\t\t\t\treturn trimmedQuery;\n\t\t\t\t}\n\n\t\t\t\tconsole.warn(`⚠️ Query LIMIT ${existingLimit} exceeds maximum of ${maxLimit}. Reducing to ${maxLimit}...`);\n\t\t\t\ttrimmedQuery = trimmedQuery.replace(/\\bLIMIT\\s+\\d+\\b/i, `LIMIT ${maxLimit}`);\n\n\t\t\t\tif (hadSemicolon) {\n\t\t\t\t\ttrimmedQuery += ';';\n\t\t\t\t}\n\t\t\t\treturn trimmedQuery;\n\t\t\t}\n\t\t}\n\t}\n\n\t// Add LIMIT clause at the end\n\ttrimmedQuery = `${trimmedQuery} LIMIT ${defaultLimit}`;\n\n\tif (hadSemicolon) {\n\t\ttrimmedQuery += ';';\n\t}\n\treturn trimmedQuery;\n}\n\n/**\n * Validates and fixes scalar subqueries to ensure they return exactly one row\n * Database-aware: Adds TOP 1 for MSSQL, LIMIT 1 for PostgreSQL/Snowflake/MySQL\n * Fixes subqueries used with scalar comparison operators (=, <, >, <=, >=, <>)\n *\n * @param query - The SQL query to validate\n * @param dbType - Database type (optional, defaults to promptLoader's database type)\n * @returns The query with fixed scalar subqueries\n */\nexport function fixScalarSubqueries(query: string, dbType?: DatabaseType): string {\n\tif (!query || query.trim().length === 0) {\n\t\treturn query;\n\t}\n\n\t// Get database type from parameter or prompt loader\n\tconst databaseType = dbType || getDatabaseType();\n\tconst isMssql = databaseType === 'mssql';\n\n\tlet modifiedQuery = query;\n\tlet hasChanges = false;\n\n\t// Find all subqueries preceded by scalar comparison operators\n\t// Pattern: operator followed by (SELECT ...)\n\tconst scalarOperatorPattern = /([=<>!]=?|<>)\\s*\\(\\s*SELECT\\s/gi;\n\n\tconst matches = [...modifiedQuery.matchAll(scalarOperatorPattern)];\n\n\t// Process matches in reverse order to maintain string positions\n\tfor (let i = matches.length - 1; i >= 0; i--) {\n\t\tconst match = matches[i];\n\t\tconst startPos = match.index! + match[0].length - 'SELECT '.length;\n\n\t\t// Find the matching closing parenthesis\n\t\tlet parenDepth = 1;\n\t\tlet endPos = startPos;\n\t\tlet foundEnd = false;\n\n\t\tfor (let j = startPos; j < modifiedQuery.length; j++) {\n\t\t\tconst char = modifiedQuery[j];\n\t\t\tif (char === '(') parenDepth++;\n\t\t\tif (char === ')') {\n\t\t\t\tparenDepth--;\n\t\t\t\tif (parenDepth === 0) {\n\t\t\t\t\tendPos = j;\n\t\t\t\t\tfoundEnd = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (!foundEnd) continue;\n\n\t\t// Extract the subquery content\n\t\tconst subquery = modifiedQuery.substring(startPos, endPos);\n\n\t\tif (isMssql) {\n\t\t\t// Check if it already has TOP\n\t\t\tif (/\\bSELECT\\s+TOP\\s+\\d+/i.test(subquery)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// Add TOP 1 after SELECT for MSSQL\n\t\t\tconst fixedSubquery = subquery.replace(/^\\s*SELECT\\s+/i, 'SELECT TOP 1 ');\n\t\t\tmodifiedQuery =\n\t\t\t\tmodifiedQuery.substring(0, startPos) +\n\t\t\t\tfixedSubquery +\n\t\t\t\tmodifiedQuery.substring(endPos);\n\t\t} else {\n\t\t\t// Check if it already has LIMIT\n\t\t\tif (/\\bLIMIT\\s+\\d+/i.test(subquery)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// Add LIMIT 1 at the end for PostgreSQL/Snowflake/MySQL\n\t\t\tconst fixedSubquery = subquery.trim() + ' LIMIT 1';\n\t\t\tmodifiedQuery =\n\t\t\t\tmodifiedQuery.substring(0, startPos) +\n\t\t\t\tfixedSubquery +\n\t\t\t\tmodifiedQuery.substring(endPos);\n\t\t}\n\n\t\thasChanges = true;\n\t\tconst limitType = isMssql ? 'TOP 1' : 'LIMIT 1';\n\t\tconsole.warn(`⚠️ Fixed scalar subquery: added ${limitType} to prevent multiple row error`);\n\t}\n\n\tif (hasChanges) {\n\t\tconst dbName = isMssql ? 'MSSQL' : 'PostgreSQL';\n\t\tconsole.log(`✓ Query validated and fixed for ${dbName} scalar subquery compatibility`);\n\t}\n\n\treturn modifiedQuery;\n}\n\n/**\n * Converts question strings to Action objects format\n * Used for follow-up questions generated by LLM\n * @param questions - Array of question strings\n * @returns Array of Action objects\n */\nexport function convertQuestionsToActions(questions: string[]): Action[] {\n\treturn questions.map((question: string, index: number) => ({\n\t\tid: `action_${index}_${Date.now()}`,\n\t\tname: question,\n\t\ttype: 'next_question',\n\t\tquestion\n\t}));\n}\n\n/**\n * Calculates the size of a JSON object in bytes\n * @param obj - The object to measure\n * @returns Size in bytes\n */\nexport function getJsonSizeInBytes(obj: any): number {\n\tconst jsonString = JSON.stringify(obj);\n\treturn Buffer.byteLength(jsonString, 'utf8');\n}\n\n/**\n * Checks if a message exceeds the WebSocket size limit\n * @param message - The message object to check\n * @param maxSize - Maximum size in bytes (default: 1MB)\n * @returns Object with isValid flag and size information\n */\nexport function validateMessageSize(message: any, maxSize: number = 1048576): { isValid: boolean; size: number; maxSize: number } {\n\tconst size = getJsonSizeInBytes(message);\n\treturn {\n\t\tisValid: size <= maxSize,\n\t\tsize,\n\t\tmaxSize\n\t};\n}\n\n/**\n * Validates and fixes common SQL syntax errors without executing the query\n * This is a fast, synchronous function that catches common LLM mistakes\n * @param query - The SQL query to validate and fix\n * @param dbType - Database type (optional, defaults to promptLoader's database type)\n * @returns Object with fixed query and list of fixes applied\n */\nexport function validateAndFixSqlQuery(\n\tquery: string,\n\tdbType?: DatabaseType\n): { query: string; fixed: boolean; fixes: string[] } {\n\tif (!query || query.trim().length === 0) {\n\t\treturn { query, fixed: false, fixes: [] };\n\t}\n\n\tconst databaseType = dbType || getDatabaseType();\n\t// Note: isMssql can be used for database-specific fixes in the future\n\tconst _isMssql = databaseType === 'mssql';\n\n\tlet fixedQuery = query;\n\tconst fixes: string[] = [];\n\n\t// ============================================\n\t// FIX 1: Move aggregate functions from WHERE to HAVING\n\t// ============================================\n\t// Detect patterns like: WHERE ... AND SUM(x) > 0 or WHERE ... AND COUNT(*) > 5\n\tconst aggregateFunctions = ['SUM', 'COUNT', 'AVG', 'MIN', 'MAX'];\n\tconst aggregateInWherePattern = new RegExp(\n\t\t`\\\\bWHERE\\\\b[^]*?\\\\b(${aggregateFunctions.join('|')})\\\\s*\\\\([^)]*\\\\)\\\\s*(?:=|>|<|>=|<=|<>|!=)`,\n\t\t'i'\n\t);\n\n\tif (aggregateInWherePattern.test(fixedQuery)) {\n\t\t// Find and extract aggregate conditions from WHERE\n\t\t// Pattern: AND/OR followed by aggregate condition\n\t\tconst aggregateConditionPattern = new RegExp(\n\t\t\t`\\\\s+(AND|OR)\\\\s+((?:COALESCE\\\\s*\\\\(\\\\s*)?(?:${aggregateFunctions.join('|')})\\\\s*\\\\([^)]+\\\\)[^)]*(?:\\\\))?\\\\s*(?:=|>|<|>=|<=|<>|!=)\\\\s*[^\\\\s,)]+)`,\n\t\t\t'gi'\n\t\t);\n\n\t\tconst aggregateConditions: string[] = [];\n\t\tlet match;\n\n\t\t// Extract all aggregate conditions\n\t\twhile ((match = aggregateConditionPattern.exec(fixedQuery)) !== null) {\n\t\t\taggregateConditions.push(match[2].trim());\n\t\t}\n\n\t\tif (aggregateConditions.length > 0) {\n\t\t\t// Remove aggregate conditions from WHERE\n\t\t\tfixedQuery = fixedQuery.replace(aggregateConditionPattern, '');\n\n\t\t\t// Clean up any leftover \"WHERE AND\" or \"WHERE OR\" patterns\n\t\t\tfixedQuery = fixedQuery.replace(/\\bWHERE\\s+(AND|OR)\\s+/gi, 'WHERE ');\n\n\t\t\t// Clean up empty WHERE clause\n\t\t\tfixedQuery = fixedQuery.replace(/\\bWHERE\\s+(GROUP\\s+BY|ORDER\\s+BY|HAVING|$)/gi, '$1');\n\n\t\t\t// Check if GROUP BY exists\n\t\t\tconst hasGroupBy = /\\bGROUP\\s+BY\\b/i.test(fixedQuery);\n\n\t\t\tif (hasGroupBy) {\n\t\t\t\t// Check if HAVING already exists\n\t\t\t\tconst hasHaving = /\\bHAVING\\b/i.test(fixedQuery);\n\t\t\t\tconst havingClause = aggregateConditions.join(' AND ');\n\n\t\t\t\tif (hasHaving) {\n\t\t\t\t\t// Add to existing HAVING\n\t\t\t\t\tfixedQuery = fixedQuery.replace(\n\t\t\t\t\t\t/\\bHAVING\\b/i,\n\t\t\t\t\t\t`HAVING ${havingClause} AND`\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\t// Insert HAVING after GROUP BY (before ORDER BY if exists)\n\t\t\t\t\tconst orderByMatch = fixedQuery.match(/(\\s+ORDER\\s+BY\\s+)/i);\n\t\t\t\t\tif (orderByMatch) {\n\t\t\t\t\t\tfixedQuery = fixedQuery.replace(\n\t\t\t\t\t\t\torderByMatch[1],\n\t\t\t\t\t\t\t` HAVING ${havingClause}${orderByMatch[1]}`\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// Append HAVING at the end\n\t\t\t\t\t\tfixedQuery = fixedQuery.trimEnd() + ` HAVING ${havingClause}`;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tfixes.push(`Moved aggregate condition(s) from WHERE to HAVING: ${havingClause}`);\n\t\t\t}\n\t\t}\n\t}\n\n\t// ============================================\n\t// FIX 2: Balance parentheses\n\t// ============================================\n\tlet openCount = 0;\n\tlet closeCount = 0;\n\tlet inString = false;\n\tlet stringChar = '';\n\n\tfor (let i = 0; i < fixedQuery.length; i++) {\n\t\tconst char = fixedQuery[i];\n\t\tconst prevChar = i > 0 ? fixedQuery[i - 1] : '';\n\n\t\t// Handle string literals\n\t\tif ((char === \"'\" || char === '\"') && prevChar !== '\\\\') {\n\t\t\tif (!inString) {\n\t\t\t\tinString = true;\n\t\t\t\tstringChar = char;\n\t\t\t} else if (char === stringChar) {\n\t\t\t\tif (i + 1 < fixedQuery.length && fixedQuery[i + 1] === char) {\n\t\t\t\t\ti++; // Skip escaped quote\n\t\t\t\t} else {\n\t\t\t\t\tinString = false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (!inString) {\n\t\t\tif (char === '(') openCount++;\n\t\t\telse if (char === ')') closeCount++;\n\t\t}\n\t}\n\n\tif (openCount > closeCount) {\n\t\tconst missingClose = openCount - closeCount;\n\t\tfixedQuery = fixedQuery.trimEnd();\n\t\tconst hasSemicolon = fixedQuery.endsWith(';');\n\t\tif (hasSemicolon) {\n\t\t\tfixedQuery = fixedQuery.slice(0, -1);\n\t\t}\n\t\tfixedQuery = fixedQuery + ')'.repeat(missingClose);\n\t\tif (hasSemicolon) {\n\t\t\tfixedQuery = fixedQuery + ';';\n\t\t}\n\t\tfixes.push(`Added ${missingClose} missing closing parenthesis(es)`);\n\t}\n\n\t// ============================================\n\t// FIX 3: ORDER BY in subqueries without TOP (MSSQL specific)\n\t// ============================================\n\t// MSSQL: ORDER BY is forbidden in subqueries unless TOP or OFFSET is present\n\t// Pattern: Find subqueries with ORDER BY but no TOP/OFFSET and add TOP\n\tconst subqueryOrderByPattern = /\\(\\s*SELECT\\s+(?!TOP\\s)([^()]*?)\\s+ORDER\\s+BY\\s+[^()]+\\)/gi;\n\tlet subqueryMatch;\n\tlet subqueryFixed = false;\n\n\twhile ((subqueryMatch = subqueryOrderByPattern.exec(fixedQuery)) !== null) {\n\t\tconst fullMatch = subqueryMatch[0];\n\t\t// Check if this subquery already has TOP or OFFSET\n\t\tif (!/\\bTOP\\s+\\d+/i.test(fullMatch) && !/\\bOFFSET\\b/i.test(fullMatch)) {\n\t\t\t// Add TOP 100 after SELECT in this subquery\n\t\t\tconst fixedSubquery = fullMatch.replace(\n\t\t\t\t/\\(\\s*SELECT\\s+/i,\n\t\t\t\t'(SELECT TOP 100 '\n\t\t\t);\n\t\t\tfixedQuery = fixedQuery.replace(fullMatch, fixedSubquery);\n\t\t\tsubqueryFixed = true;\n\t\t}\n\t}\n\n\tif (subqueryFixed) {\n\t\tfixes.push('Added TOP to subquery with ORDER BY (MSSQL requirement)');\n\t}\n\n\t// ============================================\n\t// FIX 4: Clean up double spaces and formatting\n\t// ============================================\n\tconst originalLength = fixedQuery.length;\n\tfixedQuery = fixedQuery.replace(/\\s+/g, ' ').trim();\n\tif (fixedQuery.length !== originalLength) {\n\t\t// Don't add to fixes as this is just formatting\n\t}\n\n\treturn {\n\t\tquery: fixedQuery,\n\t\tfixed: fixes.length > 0,\n\t\tfixes\n\t};\n}","import fs from 'fs';\nimport path from 'path';\n\n/**\n * User Prompt Error Logger - Captures detailed errors for USER_PROMPT_REQ\n * Logs full error details including raw strings for parse failures\n */\n\nclass UserPromptErrorLogger {\n private logStream: fs.WriteStream | null = null;\n private logPath: string;\n private enabled: boolean;\n private hasErrors: boolean = false;\n\n constructor() {\n // Get log path from environment or use default\n this.logPath = process.env.USER_PROMPT_ERROR_LOG_PATH || path.join(process.cwd(), 'user-prompt-req-errors');\n this.enabled = process.env.USER_PROMPT_ERROR_LOGGING !== 'false';\n }\n\n /**\n * Reset the error log file for a new request\n */\n resetLogFile(requestContext?: string): void {\n if (!this.enabled) return;\n\n try {\n // Close existing stream\n if (this.logStream) {\n this.logStream.end();\n this.logStream = null;\n }\n\n // Ensure directory exists\n const dir = path.dirname(this.logPath);\n if (dir !== '.' && !fs.existsSync(dir)) {\n fs.mkdirSync(dir, { recursive: true });\n }\n\n // Truncate the file by opening in write mode (not append)\n this.logStream = fs.createWriteStream(this.logPath, { flags: 'w' });\n this.hasErrors = false;\n\n // Write header\n const header = `================================================================================\nUSER PROMPT REQUEST ERROR LOG\nRequest Started: ${new Date().toISOString()}\n${requestContext ? `Context: ${requestContext}` : ''}\n================================================================================\n\n`;\n this.logStream.write(header);\n\n } catch (error) {\n console.error('[UserPromptErrorLogger] Failed to reset log file:', error);\n }\n }\n\n /**\n * Log a JSON parse error with the raw string that failed\n */\n logJsonParseError(context: string, rawString: string, error: Error): void {\n if (!this.enabled) return;\n this.hasErrors = true;\n\n const entry = `\n--------------------------------------------------------------------------------\n[${new Date().toISOString()}] JSON PARSE ERROR\n--------------------------------------------------------------------------------\nContext: ${context}\nError: ${error.message}\n\nRaw String (${rawString.length} chars):\n--------------------------------------------------------------------------------\n${rawString}\n--------------------------------------------------------------------------------\n\nStack Trace:\n${error.stack || 'No stack trace available'}\n\n`;\n this.write(entry);\n console.error(`[UserPromptError] JSON Parse Error in ${context}: ${error.message}`);\n }\n\n /**\n * Log a general error with full details\n */\n logError(context: string, error: Error | string, additionalData?: Record<string, any>): void {\n if (!this.enabled) return;\n this.hasErrors = true;\n\n const errorMessage = error instanceof Error ? error.message : error;\n const errorStack = error instanceof Error ? error.stack : undefined;\n\n let entry = `\n--------------------------------------------------------------------------------\n[${new Date().toISOString()}] ERROR\n--------------------------------------------------------------------------------\nContext: ${context}\nError: ${errorMessage}\n`;\n\n if (additionalData) {\n entry += `\nAdditional Data:\n${JSON.stringify(additionalData, null, 2)}\n`;\n }\n\n if (errorStack) {\n entry += `\nStack Trace:\n${errorStack}\n`;\n }\n\n entry += `--------------------------------------------------------------------------------\n\n`;\n this.write(entry);\n console.error(`[UserPromptError] ${context}: ${errorMessage}`);\n }\n\n /**\n * Log a SQL query error with the full query\n */\n logSqlError(query: string, error: Error | string, params?: any[]): void {\n if (!this.enabled) return;\n this.hasErrors = true;\n\n const errorMessage = error instanceof Error ? error.message : error;\n\n const entry = `\n--------------------------------------------------------------------------------\n[${new Date().toISOString()}] SQL QUERY ERROR\n--------------------------------------------------------------------------------\nError: ${errorMessage}\n\nQuery (${query.length} chars):\n--------------------------------------------------------------------------------\n${query}\n--------------------------------------------------------------------------------\n${params ? `\\nParameters: ${JSON.stringify(params)}` : ''}\n\n`;\n this.write(entry);\n console.error(`[UserPromptError] SQL Error: ${errorMessage}`);\n }\n\n /**\n * Log an LLM API error\n */\n logLlmError(provider: string, model: string, method: string, error: Error | string, requestData?: any): void {\n if (!this.enabled) return;\n this.hasErrors = true;\n\n const errorMessage = error instanceof Error ? error.message : error;\n const errorStack = error instanceof Error ? error.stack : undefined;\n\n let entry = `\n--------------------------------------------------------------------------------\n[${new Date().toISOString()}] LLM API ERROR\n--------------------------------------------------------------------------------\nProvider: ${provider}\nModel: ${model}\nMethod: ${method}\nError: ${errorMessage}\n`;\n\n if (requestData) {\n // Truncate large request data\n const dataStr = JSON.stringify(requestData, null, 2);\n const truncated = dataStr.length > 5000 ? dataStr.substring(0, 5000) + '\\n... [truncated]' : dataStr;\n entry += `\nRequest Data:\n${truncated}\n`;\n }\n\n if (errorStack) {\n entry += `\nStack Trace:\n${errorStack}\n`;\n }\n\n entry += `--------------------------------------------------------------------------------\n\n`;\n this.write(entry);\n console.error(`[UserPromptError] LLM Error (${provider}/${model}): ${errorMessage}`);\n }\n\n /**\n * Log tool execution error\n */\n logToolError(toolName: string, toolInput: any, error: Error | string): void {\n if (!this.enabled) return;\n this.hasErrors = true;\n\n const errorMessage = error instanceof Error ? error.message : error;\n const errorStack = error instanceof Error ? error.stack : undefined;\n\n const entry = `\n--------------------------------------------------------------------------------\n[${new Date().toISOString()}] TOOL EXECUTION ERROR\n--------------------------------------------------------------------------------\nTool: ${toolName}\nError: ${errorMessage}\n\nTool Input:\n${JSON.stringify(toolInput, null, 2)}\n${errorStack ? `\\nStack Trace:\\n${errorStack}` : ''}\n--------------------------------------------------------------------------------\n\n`;\n this.write(entry);\n console.error(`[UserPromptError] Tool Error (${toolName}): ${errorMessage}`);\n }\n\n /**\n * Write final summary if there were errors\n */\n writeSummary(): void {\n if (!this.enabled || !this.hasErrors) return;\n\n const summary = `\n================================================================================\nREQUEST COMPLETED WITH ERRORS\nTime: ${new Date().toISOString()}\n================================================================================\n`;\n this.write(summary);\n }\n\n /**\n * Check if any errors were logged\n */\n hadErrors(): boolean {\n return this.hasErrors;\n }\n\n private write(content: string): void {\n if (this.logStream) {\n this.logStream.write(content);\n }\n }\n}\n\n// Export singleton instance\nexport const userPromptErrorLogger = new UserPromptErrorLogger();\n","/**\n * BM25L Reranker for hybrid semantic search\n *\n * BM25L is an improved variant of BM25 that provides better handling of\n * long documents and term frequency saturation. This implementation is\n * designed to rerank semantic search results from ChromaDB.\n *\n * The hybrid approach combines:\n * 1. Semantic similarity from ChromaDB embeddings (dense vectors)\n * 2. Lexical matching from BM25L (sparse, keyword-based)\n *\n * This addresses the weakness of pure semantic search which may miss\n * exact keyword matches that are important for user intent.\n */\n\nexport interface BM25LOptions {\n\t/** Term frequency saturation parameter (default: 1.5) */\n\tk1?: number;\n\t/** Length normalization parameter (default: 0.75) */\n\tb?: number;\n\t/** Lower-bound adjustment from BM25L paper (default: 0.5) */\n\tdelta?: number;\n}\n\nexport interface RerankedResult<T> {\n\titem: T;\n\toriginalIndex: number;\n\tsemanticScore: number;\n\tbm25Score: number;\n\thybridScore: number;\n}\n\nexport interface HybridSearchOptions extends BM25LOptions {\n\t/** Weight for semantic score (0-1, default: 0.7) */\n\tsemanticWeight?: number;\n\t/** Weight for BM25 score (0-1, default: 0.3) */\n\tbm25Weight?: number;\n\t/** Minimum hybrid score threshold (0-1, default: 0) */\n\tminScore?: number;\n}\n\n/**\n * BM25L implementation for lexical scoring\n */\nexport class BM25L {\n\tprivate k1: number;\n\tprivate b: number;\n\tprivate delta: number;\n\tprivate documents: string[][];\n\tprivate docLengths: number[];\n\tprivate avgDocLength: number;\n\tprivate termDocFreq: Record<string, number>;\n\n\t/**\n\t * @param documents - Array of raw documents (strings)\n\t * @param opts - Optional BM25L parameters\n\t */\n\tconstructor(documents: string[] = [], opts: BM25LOptions = {}) {\n\t\tif (!Array.isArray(documents)) {\n\t\t\tthrow new Error('BM25L: documents must be an array of strings.');\n\t\t}\n\n\t\tthis.k1 = typeof opts.k1 === 'number' ? opts.k1 : 1.5;\n\t\tthis.b = typeof opts.b === 'number' ? opts.b : 0.75;\n\t\tthis.delta = typeof opts.delta === 'number' ? opts.delta : 0.5;\n\n\t\t// Tokenize documents safely\n\t\tthis.documents = documents.map(d => (typeof d === 'string' ? this.tokenize(d) : []));\n\n\t\t// Precompute stats\n\t\tthis.docLengths = this.documents.map(doc => doc.length);\n\t\tthis.avgDocLength =\n\t\t\tthis.docLengths.reduce((a, b) => a + b, 0) / (this.docLengths.length || 1);\n\n\t\t// Compute document frequency (df)\n\t\tthis.termDocFreq = {};\n\t\tthis.documents.forEach(doc => {\n\t\t\tconst seen = new Set<string>();\n\t\t\tdoc.forEach(term => {\n\t\t\t\tif (!seen.has(term)) {\n\t\t\t\t\tseen.add(term);\n\t\t\t\t\tthis.termDocFreq[term] = (this.termDocFreq[term] || 0) + 1;\n\t\t\t\t}\n\t\t\t});\n\t\t});\n\t}\n\n\t/**\n\t * Tokenize text into lowercase alphanumeric tokens\n\t */\n\ttokenize(text: string): string[] {\n\t\tif (typeof text !== 'string') return [];\n\t\treturn text\n\t\t\t.toLowerCase()\n\t\t\t.replace(/[^a-z0-9\\s]/g, ' ')\n\t\t\t.split(/\\s+/)\n\t\t\t.filter(Boolean);\n\t}\n\n\t/**\n\t * Compute IDF (Inverse Document Frequency) with smoothing\n\t */\n\tprivate idf(term: string): number {\n\t\tconst df = this.termDocFreq[term] || 0;\n\t\tconst N = this.documents.length || 1;\n\t\treturn Math.log(1 + (N - df + 0.5) / (df + 0.5));\n\t}\n\n\t/**\n\t * Compute BM25L score for a single document\n\t */\n\tscore(query: string, docIndex: number): number {\n\t\tif (typeof query !== 'string') return 0;\n\t\tif (docIndex < 0 || docIndex >= this.documents.length) return 0;\n\n\t\tconst tokens = this.tokenize(query);\n\t\tif (tokens.length === 0) return 0;\n\n\t\tconst doc = this.documents[docIndex];\n\t\tconst docLength = this.docLengths[docIndex] || 1;\n\n\t\t// Term frequencies\n\t\tconst freq: Record<string, number> = {};\n\t\tfor (const t of doc) {\n\t\t\tfreq[t] = (freq[t] || 0) + 1;\n\t\t}\n\n\t\tlet sum = 0;\n\n\t\tfor (const term of tokens) {\n\t\t\tconst tf = freq[term] || 0;\n\t\t\tif (tf === 0) continue;\n\n\t\t\tconst idfVal = this.idf(term);\n\n\t\t\t// BM25L adjusted TF\n\t\t\tlet tfL = tf - this.b * (docLength / this.avgDocLength) + this.delta;\n\t\t\tif (tfL < 0) tfL = 0;\n\n\t\t\tsum += idfVal * (tfL / (this.k1 + tfL));\n\t\t}\n\n\t\treturn sum;\n\t}\n\n\t/**\n\t * Search and rank all documents\n\t */\n\tsearch(query: string): Array<{ index: number; score: number }> {\n\t\treturn this.documents\n\t\t\t.map((_, i) => ({\n\t\t\t\tindex: i,\n\t\t\t\tscore: this.score(query, i)\n\t\t\t}))\n\t\t\t.sort((a, b) => b.score - a.score);\n\t}\n}\n\n/**\n * Normalize scores to 0-1 range using min-max normalization\n */\nfunction normalizeScores(scores: number[]): number[] {\n\tif (scores.length === 0) return [];\n\n\tconst min = Math.min(...scores);\n\tconst max = Math.max(...scores);\n\n\t// If all scores are the same, return array of 1s (or 0s if all are 0)\n\tif (max === min) {\n\t\treturn scores.map(() => (max === 0 ? 0 : 1));\n\t}\n\n\treturn scores.map(score => (score - min) / (max - min));\n}\n\n/**\n * Hybrid reranker that combines semantic and BM25L scores\n *\n * @param query - The search query\n * @param items - Array of items to rerank\n * @param getDocument - Function to extract document text from an item\n * @param getSemanticScore - Function to extract semantic similarity score from an item\n * @param options - Hybrid search options\n * @returns Reranked items with hybrid scores\n */\nexport function hybridRerank<T>(\n\tquery: string,\n\titems: T[],\n\tgetDocument: (item: T) => string,\n\tgetSemanticScore: (item: T) => number,\n\toptions: HybridSearchOptions = {}\n): RerankedResult<T>[] {\n\tconst {\n\t\tsemanticWeight = 0.7,\n\t\tbm25Weight = 0.3,\n\t\tminScore = 0,\n\t\tk1 = 1.5,\n\t\tb = 0.75,\n\t\tdelta = 0.5\n\t} = options;\n\n\tif (items.length === 0) return [];\n\n\t// Extract documents and semantic scores\n\tconst documents = items.map(getDocument);\n\tconst semanticScores = items.map(getSemanticScore);\n\n\t// Create BM25L index and compute scores\n\tconst bm25 = new BM25L(documents, { k1, b, delta });\n\tconst bm25Scores = items.map((_, i) => bm25.score(query, i));\n\n\t// Normalize both score sets to 0-1 range\n\tconst normalizedSemantic = normalizeScores(semanticScores);\n\tconst normalizedBM25 = normalizeScores(bm25Scores);\n\n\t// Compute hybrid scores and create result objects\n\tconst results: RerankedResult<T>[] = items.map((item, i) => {\n\t\tconst hybridScore =\n\t\t\tsemanticWeight * normalizedSemantic[i] + bm25Weight * normalizedBM25[i];\n\n\t\treturn {\n\t\t\titem,\n\t\t\toriginalIndex: i,\n\t\t\tsemanticScore: semanticScores[i],\n\t\t\tbm25Score: bm25Scores[i],\n\t\t\thybridScore\n\t\t};\n\t});\n\n\t// Sort by hybrid score (descending) and filter by minimum score\n\treturn results\n\t\t.filter(r => r.hybridScore >= minScore)\n\t\t.sort((a, b) => b.hybridScore - a.hybridScore);\n}\n\n/**\n * Simple reranking function for ChromaDB results\n *\n * This is a convenience wrapper for reranking ChromaDB query results\n * that follow the standard { ids, documents, metadatas, distances } format.\n *\n * @param query - The search query\n * @param chromaResults - ChromaDB query results\n * @param options - Hybrid search options\n * @returns Reranked results with hybrid scores\n */\nexport function rerankChromaResults(\n\tquery: string,\n\tchromaResults: {\n\t\tids: string[][];\n\t\tdocuments: (string | null)[][];\n\t\tmetadatas: Record<string, any>[][];\n\t\tdistances: number[][];\n\t},\n\toptions: HybridSearchOptions = {}\n): Array<{\n\tid: string;\n\tdocument: string | null;\n\tmetadata: Record<string, any>;\n\tdistance: number;\n\tsemanticScore: number;\n\tbm25Score: number;\n\thybridScore: number;\n}> {\n\t// Flatten the nested arrays (ChromaDB returns nested arrays for batch queries)\n\tconst ids = chromaResults.ids[0] || [];\n\tconst documents = chromaResults.documents[0] || [];\n\tconst metadatas = chromaResults.metadatas[0] || [];\n\tconst distances = chromaResults.distances[0] || [];\n\n\tif (ids.length === 0) return [];\n\n\t// Create items array\n\tconst items = ids.map((id, i) => ({\n\t\tid,\n\t\tdocument: documents[i],\n\t\tmetadata: metadatas[i],\n\t\tdistance: distances[i]\n\t}));\n\n\t// Rerank using hybrid approach\n\tconst reranked = hybridRerank(\n\t\tquery,\n\t\titems,\n\t\titem => item.document || '',\n\t\t// Convert L2 distance to similarity score\n\t\titem => 1 / (1 + item.distance),\n\t\toptions\n\t);\n\n\t// Return flattened results\n\treturn reranked.map(r => ({\n\t\tid: r.item.id,\n\t\tdocument: r.item.document,\n\t\tmetadata: r.item.metadata,\n\t\tdistance: r.item.distance,\n\t\tsemanticScore: r.semanticScore,\n\t\tbm25Score: r.bm25Score,\n\t\thybridScore: r.hybridScore\n\t}));\n}\n\n/**\n * Rerank conversation search results specifically\n *\n * This function is designed to work with the conversation-history.search collection\n * where we need to fetch more results initially and then rerank them.\n *\n * @param query - The user's search query\n * @param results - Array of conversation search results from ChromaDB\n * @param options - Hybrid search options\n * @returns Reranked results sorted by hybrid score\n */\nexport function rerankConversationResults<T extends { userPrompt?: string; similarity?: number }>(\n\tquery: string,\n\tresults: T[],\n\toptions: HybridSearchOptions = {}\n): Array<T & { hybridScore: number; bm25Score: number }> {\n\tif (results.length === 0) return [];\n\n\tconst reranked = hybridRerank(\n\t\tquery,\n\t\tresults,\n\t\titem => item.userPrompt || '',\n\t\titem => item.similarity || 0,\n\t\toptions\n\t);\n\n\treturn reranked.map(r => ({\n\t\t...r.item,\n\t\thybridScore: r.hybridScore,\n\t\tbm25Score: r.bm25Score\n\t}));\n}\n\nexport default {\n\tBM25L,\n\thybridRerank,\n\trerankChromaResults,\n\trerankConversationResults\n};\n","import { logger } from '../utils/logger';\nimport { rerankConversationResults, type HybridSearchOptions } from '../utils/bm25l-reranker';\nimport type { UIBlock } from '../types';\n\n/**\n * Conversation Search result interface\n */\nexport interface ConversationSearchResult {\n\tuiBlock: UIBlock;\n\tsimilarity: number;\n\tmetadata?: {\n\t\tuserPrompt?: string;\n\t\ttimestamp?: number;\n\t};\n\t/** Hybrid score combining semantic and BM25L lexical matching */\n\thybridScore?: number;\n\t/** BM25L lexical score */\n\tbm25Score?: number;\n}\n\n/**\n * Options for conversation search with reranking\n */\nexport interface ConversationSearchOptions {\n\tuserPrompt: string;\n\tcollections?: any;\n\tuserId?: string;\n\tsimilarityThreshold?: number;\n\t/** Enable hybrid reranking with BM25L (default: true) */\n\tenableReranking?: boolean;\n\t/** Number of candidates to fetch for reranking (default: 5) */\n\trerankCandidates?: number;\n\t/** Hybrid search options for BM25L reranking */\n\thybridOptions?: HybridSearchOptions;\n}\n\n/**\n * Search for semantically similar previous conversations for a user\n * Calls the registered 'conversation-history.search' collection\n * Returns the best match if similarity > threshold (default: 0.6)\n *\n * @param userPrompt - The current user's question/prompt\n * @param collections - The collection registry containing conversation-history.search\n * @param similarityThreshold - Minimum similarity threshold (0-1, default: 0.6)\n * @returns Matching UIBlock if found with similarity > threshold, null otherwise\n */\nconst searchConversations = async ({\n\tuserPrompt,\n\tcollections,\n\tuserId,\n\tsimilarityThreshold = 0.6\n}: {\n\tuserPrompt: string;\n\tcollections?: any;\n\tuserId?: string;\n\tsimilarityThreshold?: number;\n}): Promise<ConversationSearchResult | null> => {\n\ttry {\n\t\t// Check if conversation-history collection is registered\n\t\tif (\n\t\t\t!collections ||\n\t\t\t!collections['conversation-history'] ||\n\t\t\t!collections['conversation-history']['search']\n\t\t) {\n\t\t\tlogger.info('[ConversationSearch] conversation-history.search collection not registered, skipping');\n\t\t\treturn null;\n\t\t}\n\n\t\tlogger.info(`[ConversationSearch] Searching conversations for: \"${userPrompt.substring(0, 50)}...\"`);\n\t\tlogger.info(`[ConversationSearch] Using similarity threshold: ${(similarityThreshold! * 100).toFixed(0)}%`);\n\n\t\t// Call the registered conversation-history.search collection\n\t\tconst result: ConversationSearchResult = await collections['conversation-history']['search']({\n\t\t\tuserPrompt,\n\t\t\tuserId,\n\t\t\tthreshold: similarityThreshold\n\t\t});\n\n\t\tif (!result) {\n\t\t\tlogger.info('[ConversationSearch] No matching conversations found');\n\t\t\treturn null;\n\t\t}\n\n\t\tif (!result.uiBlock) {\n\t\t\tlogger.error('[ConversationSearch] No UI block in conversation search result');\n\t\t\treturn null;\n\t\t}\n\n\t\tconst similarity = result.similarity || 0;\n\t\tlogger.info(`[ConversationSearch] Best match similarity: ${(similarity * 100).toFixed(2)}%`);\n\t\t// Check if similarity meets threshold\n\t\tif (similarity < similarityThreshold) {\n\t\t\tlogger.info(\n\t\t\t\t`[ConversationSearch] Best match has similarity ${(similarity * 100).toFixed(2)}% but below threshold ${(similarityThreshold * 100).toFixed(2)}%`\n\t\t\t);\n\t\t\treturn null;\n\t\t}\n\n\t\tlogger.info(\n\t\t\t`[ConversationSearch] Found matching conversation with similarity ${(similarity * 100).toFixed(2)}%`\n\t\t);\n\t\tlogger.debug(`[ConversationSearch] Matched prompt: \"${result.metadata?.userPrompt?.substring(0, 50)}...\"`);\n\n\t\treturn result;\n\t} catch (error) {\n\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\t\tlogger.warn(`[ConversationSearch] Error searching conversations: ${errorMsg}`);\n\t\t// Return null on error - don't fail the entire request\n\t\treturn null;\n\t}\n};\n\n/**\n * Search for semantically similar previous conversations with hybrid reranking\n *\n * This is an enhanced version of searchConversations that uses BM25L reranking\n * to improve search accuracy by combining semantic similarity with lexical matching.\n *\n * Flow:\n * 1. Fetch a large pool of candidates from ChromaDB (default: 50) with no threshold\n * 2. Apply BM25L reranking on all candidates to find the best lexical + semantic match\n * 3. Return the top result only if it meets the similarity threshold\n *\n * This approach ensures we don't miss good lexical matches that might have\n * lower semantic similarity but are actually more relevant to the query.\n *\n * @param options - Search options including userPrompt, collections, userId, and reranking settings\n * @returns Matching UIBlock if found with hybrid score > threshold, null otherwise\n */\nconst searchConversationsWithReranking = async (options: ConversationSearchOptions): Promise<ConversationSearchResult | null> => {\n\tconst {\n\t\tuserPrompt,\n\t\tcollections,\n\t\tuserId,\n\t\tsimilarityThreshold = 0.6,\n\t\trerankCandidates = 50, // Fetch more candidates for better reranking\n\t\thybridOptions = {\n\t\t\tsemanticWeight: 0.7,\n\t\t\tbm25Weight: 0.3\n\t\t}\n\t} = options;\n\n\ttry {\n\t\t// Check if conversation-history collection is registered\n\t\tif (\n\t\t\t!collections ||\n\t\t\t!collections['conversation-history']\n\t\t) {\n\t\t\tlogger.warn('[ConversationSearch] conversation-history collection not registered, skipping');\n\t\t\treturn null;\n\t\t}\n\n\t\t// Check if searchMultiple is available for reranking\n\t\tif (!collections['conversation-history']['searchMultiple']) {\n\t\t\tlogger.warn('[ConversationSearch] searchMultiple not available, falling back to standard search');\n\t\t\treturn searchConversations({\n\t\t\t\tuserPrompt,\n\t\t\t\tcollections,\n\t\t\t\tuserId,\n\t\t\t\tsimilarityThreshold\n\t\t\t});\n\t\t}\n\n\t\t// Fetch a large pool of candidates with NO threshold - let reranking decide\n\t\tconst results: ConversationSearchResult[] = await collections['conversation-history']['searchMultiple']({\n\t\t\tuserPrompt,\n\t\t\tuserId,\n\t\t\tlimit: rerankCandidates,\n\t\t\tthreshold: 0 // No threshold - get all candidates for reranking\n\t\t});\n\n\t\tif (!results || results.length === 0) {\n\t\t\tlogger.info('[ConversationSearch] No conversations found in database');\n\t\t\treturn null;\n\t\t}\n\n\t\tlogger.info(`[ConversationSearch] Retrieved ${results.length} candidates for reranking`);\n\n\t\t// Prepare results for reranking - extract userPrompt from metadata\n\t\tconst candidatesForReranking = results.map(r => ({\n\t\t\t...r,\n\t\t\tuserPrompt: r.metadata?.userPrompt || ''\n\t\t}));\n\n\t\t// Rerank using hybrid BM25L + semantic scoring\n\t\tconst reranked = rerankConversationResults(userPrompt, candidatesForReranking, hybridOptions);\n\n\t\tif (reranked.length === 0) {\n\t\t\tlogger.info('[ConversationSearch] No results after reranking');\n\t\t\treturn null;\n\t\t}\n\n\t\t// Get the best result after reranking\n\t\tconst best = reranked[0];\n\t\tconst hybridScore = best.hybridScore;\n\t\tconst semanticScore = best.similarity || 0;\n\t\tconst matchedUserPrompt = (best as any).userPrompt || best.metadata?.userPrompt || '';\n\n\t\tlogger.info(`[ConversationSearch] Best match after reranking:`);\n\t\tlogger.info(` - Hybrid score: ${(hybridScore * 100).toFixed(2)}%`);\n\t\tlogger.info(` - Semantic score: ${(semanticScore * 100).toFixed(2)}%`);\n\t\tlogger.info(` - BM25L score: ${best.bm25Score.toFixed(4)}`);\n\t\tlogger.info(` - Matched prompt: \"${matchedUserPrompt}\"`);\n\t\tlogger.info(` - Query prompt: \"${userPrompt}\"`);\n\n\t\t// Apply threshold on SEMANTIC score (not hybrid)\n\t\t// Hybrid score can be inflated by normalization with few candidates\n\t\t// Semantic score is the true measure of meaning similarity\n\t\t// BM25 reranking only helps pick the best among candidates that already pass semantic threshold\n\t\tif (semanticScore < similarityThreshold) {\n\t\t\tlogger.info(\n\t\t\t\t`[ConversationSearch] Semantic score ${(semanticScore * 100).toFixed(2)}% below threshold ${(similarityThreshold * 100).toFixed(2)}% - rejecting match`\n\t\t\t);\n\t\t\treturn null;\n\t\t}\n\n\t\tlogger.info(\n\t\t\t`[ConversationSearch] ✓ Found match with semantic score ${(semanticScore * 100).toFixed(2)}%`\n\t\t);\n\n\t\treturn {\n\t\t\tuiBlock: best.uiBlock,\n\t\t\tsimilarity: semanticScore,\n\t\t\thybridScore: hybridScore,\n\t\t\tbm25Score: best.bm25Score,\n\t\t\tmetadata: best.metadata\n\t\t};\n\t} catch (error) {\n\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\t\tlogger.warn(`[ConversationSearch] Error in hybrid search: ${errorMsg}`);\n\t\t// Return null on error - don't fail the entire request\n\t\treturn null;\n\t}\n};\n\n/**\n * Find a previously-stored conversation whose userPrompt is byte-for-byte\n * identical to the current prompt. Backed by an indexed DB lookup via the\n * 'conversation-history.exactMatch' collection — no embeddings, no rerank.\n *\n * Use this for the cached-result short-circuit in the agent flow when only\n * exact (100%) matches should replay. Returns null when the collection isn't\n * registered, when no match exists, or on error.\n */\nconst findExactMatch = async ({\n\tuserPrompt,\n\tcollections,\n\tuserId,\n}: {\n\tuserPrompt: string;\n\tcollections?: any;\n\tuserId?: string;\n}): Promise<ConversationSearchResult | null> => {\n\ttry {\n\t\tif (\n\t\t\t!collections ||\n\t\t\t!collections['conversation-history'] ||\n\t\t\t!collections['conversation-history']['exactMatch']\n\t\t) {\n\t\t\tlogger.info('[ConversationSearch] conversation-history.exactMatch collection not registered, skipping');\n\t\t\treturn null;\n\t\t}\n\n\t\tconst result: ConversationSearchResult | null = await collections['conversation-history']['exactMatch']({\n\t\t\tuserPrompt,\n\t\t\tuserId,\n\t\t});\n\n\t\tif (!result || !result.uiBlock) {\n\t\t\tlogger.info('[ConversationSearch] No exact match found');\n\t\t\treturn null;\n\t\t}\n\n\t\tlogger.info(`[ConversationSearch] ✓ Exact prompt match found for \"${userPrompt.substring(0, 50)}...\"`);\n\t\treturn result;\n\t} catch (error) {\n\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\t\tlogger.warn(`[ConversationSearch] Error in exact match lookup: ${errorMsg}`);\n\t\treturn null;\n\t}\n};\n\nconst ConversationSearch = {\n\tsearchConversations,\n\tsearchConversationsWithReranking,\n\tfindExactMatch,\n};\n\nexport default ConversationSearch;\n","/**\n * Constants for LLM response generation\n * Centralized configuration values for the userResponse module\n */\n\n// ============================================\n// Retry & Attempt Limits\n// ============================================\n\n/** Maximum attempts for query validation with LLM fix */\nexport const MAX_QUERY_VALIDATION_RETRIES = 3;\n\n/** Maximum attempts for query execution in text response generation */\nexport const MAX_QUERY_ATTEMPTS = 6;\n\n/** Maximum attempts for external tool execution */\nexport const MAX_TOOL_ATTEMPTS = 3;\n\n// ============================================\n// Streaming Configuration\n// ============================================\n\n/** Flush interval for stream buffer in milliseconds */\nexport const STREAM_FLUSH_INTERVAL_MS = 50;\n\n/** Heartbeat interval for progress updates during long operations */\nexport const PROGRESS_HEARTBEAT_INTERVAL_MS = 800;\n\n/** Small delay for smoother streaming UX */\nexport const STREAM_DELAY_MS = 50;\n\n/** Chunk size threshold for immediate flush (characters) */\nexport const STREAM_IMMEDIATE_FLUSH_THRESHOLD = 100;\n\n// ============================================\n// LLM Token Limits\n// ============================================\n\n/** Max tokens for query fix requests */\nexport const MAX_TOKENS_QUERY_FIX = 2048;\n\n/** Max tokens for component matching */\nexport const MAX_TOKENS_COMPONENT_MATCHING = 8192;\n\n/** Max tokens for category classification */\nexport const MAX_TOKENS_CLASSIFICATION = 1500;\n\n/** Max tokens for UI block parameter adaptation */\nexport const MAX_TOKENS_ADAPTATION = 8192;\n\n/** Max tokens for text response generation */\nexport const MAX_TOKENS_TEXT_RESPONSE = 4000;\n\n/** Max tokens for next questions generation */\nexport const MAX_TOKENS_NEXT_QUESTIONS = 1200;\n\n// ============================================\n// Canonical row limits — ONE source of truth, by PIPELINE STAGE.\n//\n// The stages legitimately differ, so this is NOT a single number:\n// • FETCH — pulled from the DB for the agent to reason over. Summarized for\n// the LLM and NEVER rendered, so it can be large.\n// • TO LLM — anything that enters an LLM prompt. Must stay tiny (token cost).\n// • RENDER — cached and drawn in a UI component. No token cost, but bounds\n// memory + render size; this also caps what the data cache holds.\n// Every other row-limit name below is an ALIAS onto one of these three, so the\n// whole pipeline stays consistent from one place.\n// ============================================\n\n/** STAGE 1 — FETCH: max rows an exploration/source query pulls from the DB.\n * A raw sample is a SAMPLE at any size — accuracy comes from the agent\n * AGGREGATING (GROUP BY), not from fetching more biased rows. Kept small; when a\n * query hits this cap the result is flagged to the LLM as a partial, ordered\n * sample so it aggregates instead of concluding from it. */\nexport const MAX_ROWS_FETCHED = 50;\n\n/** STAGE 2 — TO LLM: largest result shown to an LLM VERBATIM (complete). Above\n * this the LLM receives a bounded summary + a few samples instead of the rows.\n * Small lookups still arrive complete; large ones are resolved via SQL JOINs,\n * so the LLM never needs to eyeball them. */\nexport const MAX_ROWS_TO_LLM = 10;\n\n/** STAGE 2 — small sample inlined for LLM context / stream preview / comp-gen. */\nexport const LLM_SAMPLE_ROWS = 10;\n\n/** STAGE 3 — RENDER: max rows cached and drawn in a UI component. The script's\n * final dataset and every component query are capped to this before caching. */\nexport const MAX_ROWS_RENDERED = 250;\n\n// ---- Field-level (character) truncation ----\nexport const DEFAULT_MAX_CHARS_PER_FIELD = 500;\nexport const STREAM_PREVIEW_MAX_CHARS = 200;\nexport const TOOL_TRACKING_MAX_CHARS = 200;\n\n// ---- Back-compat aliases → mapped onto the canonical stage limits above ----\n/** @deprecated use LLM_SAMPLE_ROWS */\nexport const DEFAULT_MAX_ROWS_FOR_LLM = LLM_SAMPLE_ROWS;\n/** Rows in a live `<DataTable>` stream preview (sample-sized). */\nexport const STREAM_PREVIEW_MAX_ROWS = LLM_SAMPLE_ROWS;\n/** Sample rows attached to `_sampleData` (shown to the component-generator LLM). */\nexport const TOOL_TRACKING_SAMPLE_ROWS = LLM_SAMPLE_ROWS;\n/** Tighter sample for executed-tool / component-matching context. */\nexport const TOOL_TRACKING_MAX_ROWS = 5;\n\n/** Default row limit when a query specifies none (supports multi-series charts). */\nexport const DEFAULT_QUERY_LIMIT = 24;\n/** External-tool/agent-flow query cap — result goes back to the LLM, so tiny. */\nexport const MAX_AGENT_QUERY_LIMIT = LLM_SAMPLE_ROWS;\n/** Dashboard/report component query cap — rendered in UI, no LLM token cost. */\nexport const MAX_COMPONENT_QUERY_LIMIT = MAX_ROWS_RENDERED;\n\n// ============================================\n// Similarity Thresholds\n// ============================================\n\n/** Threshold for exact match in conversation search (99%) */\nexport const EXACT_MATCH_SIMILARITY_THRESHOLD = 0.99;\n\n/** Default conversation similarity threshold */\nexport const DEFAULT_CONVERSATION_SIMILARITY_THRESHOLD = 0.8;\n\n// ============================================\n// Tool Calling Configuration\n// ============================================\n\n/** Max iterations for LLM tool calling loop */\nexport const MAX_TOOL_CALLING_ITERATIONS = 20;\n\n// ============================================\n// Knowledge Base Configuration\n// ============================================\n\n/** Default top K results for knowledge base queries */\nexport const KNOWLEDGE_BASE_TOP_K = 3;\n","/**\n * StreamBuffer - Buffered streaming utility for smoother text delivery\n * Batches small chunks together and flushes at regular intervals\n */\n\nimport {\n\tSTREAM_FLUSH_INTERVAL_MS,\n\tSTREAM_IMMEDIATE_FLUSH_THRESHOLD,\n\tSTREAM_DELAY_MS,\n\tPROGRESS_HEARTBEAT_INTERVAL_MS\n} from './constants';\n\nexport type StreamCallback = (chunk: string) => void;\n\n/**\n * StreamBuffer class for managing buffered streaming output\n * Provides smooth text delivery by batching small chunks\n */\nexport class StreamBuffer {\n\tprivate buffer: string = '';\n\tprivate flushTimer: ReturnType<typeof setTimeout> | null = null;\n\tprivate callback: StreamCallback | undefined;\n\tprivate fullText: string = '';\n\n\tconstructor(callback?: StreamCallback) {\n\t\tthis.callback = callback;\n\t}\n\n\t/**\n\t * Check if the buffer has a callback configured\n\t */\n\thasCallback(): boolean {\n\t\treturn !!this.callback;\n\t}\n\n\t/**\n\t * Get all text that has been written (including already flushed)\n\t */\n\tgetFullText(): string {\n\t\treturn this.fullText;\n\t}\n\n\t/**\n\t * Write a chunk to the buffer\n\t * Large chunks or chunks with newlines are flushed immediately\n\t * Small chunks are batched and flushed after a short interval\n\t *\n\t * @param chunk - Text chunk to write\n\t */\n\twrite(chunk: string): void {\n\t\tthis.fullText += chunk;\n\n\t\tif (!this.callback) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.buffer += chunk;\n\n\t\t// Flush immediately for large chunks or chunks with newlines\n\t\tif (chunk.includes('\\n') || chunk.length > STREAM_IMMEDIATE_FLUSH_THRESHOLD) {\n\t\t\tthis.flushNow();\n\t\t} else if (!this.flushTimer) {\n\t\t\t// Schedule flush for small chunks\n\t\t\tthis.flushTimer = setTimeout(() => this.flushNow(), STREAM_FLUSH_INTERVAL_MS);\n\t\t}\n\t}\n\n\t/**\n\t * Flush the buffer immediately\n\t * Call this before tool execution or other operations that need clean output\n\t */\n\tflush(): void {\n\t\tthis.flushNow();\n\t}\n\n\t/**\n\t * Internal flush implementation\n\t */\n\tprivate flushNow(): void {\n\t\tif (this.flushTimer) {\n\t\t\tclearTimeout(this.flushTimer);\n\t\t\tthis.flushTimer = null;\n\t\t}\n\n\t\tif (this.buffer && this.callback) {\n\t\t\tthis.callback(this.buffer);\n\t\t\tthis.buffer = '';\n\t\t}\n\t}\n\n\t/**\n\t * Clean up resources\n\t * Call this when done with the buffer\n\t */\n\tdispose(): void {\n\t\tthis.flush();\n\t\tthis.callback = undefined;\n\t}\n}\n\n/**\n * TaggedStreamBuffer - Wraps a parent StreamBuffer and automatically tags every write\n * with a unique call ID. Used by source agents so the frontend can group\n * interleaved messages from parallel source agents into separate blocks.\n *\n * Format: __SB_{callId}_{type}__{content}__SB_END__\n * - type: \"START\" for the first message (includes source name + intent)\n * - \"MSG\" for subsequent messages\n *\n * The frontend parses these markers to group content by callId and render\n * each group as a collapsible section.\n */\nexport class TaggedStreamBuffer extends StreamBuffer {\n\tprivate callId: string;\n\tprivate parent: StreamBuffer;\n\tprivate sourceName: string;\n\tprivate intent: string;\n\tprivate started: boolean = false;\n\n\tconstructor(callId: string, parent: StreamBuffer, sourceName: string, intent: string) {\n\t\t// Pass no callback — we don't use the base class buffering\n\t\tsuper();\n\t\tthis.callId = callId;\n\t\tthis.parent = parent;\n\t\tthis.sourceName = sourceName;\n\t\tthis.intent = intent;\n\t}\n\n\thasCallback(): boolean {\n\t\treturn this.parent.hasCallback();\n\t}\n\n\tgetFullText(): string {\n\t\treturn this.parent.getFullText();\n\t}\n\n\twrite(chunk: string): void {\n\t\tif (!this.parent.hasCallback()) return;\n\n\t\tif (!this.started) {\n\t\t\t// First write: emit START marker with source name and intent\n\t\t\tthis.started = true;\n\t\t\tconst escapedName = this.sourceName.replace(/\\|/g, '-');\n\t\t\tconst escapedIntent = this.intent\n\t\t\t\t.replace(/\\|/g, ' ') // Pipe used as separator — must be sanitized\n\t\t\t\t.replace(/\"/g, '&quot;');\n\t\t\tthis.parent.write(`__SB_${this.callId}_START__${escapedName}|${escapedIntent}__SB_END__`);\n\t\t}\n\n\t\t// Tag the content\n\t\tthis.parent.write(`__SB_${this.callId}_MSG__${chunk}__SB_END__`);\n\t}\n\n\tflush(): void {\n\t\tthis.parent.flush();\n\t}\n\n\tdispose(): void {\n\t\tthis.parent.flush();\n\t}\n}\n\n/**\n * Helper function for small delays to improve streaming UX\n * @param ms - Delay in milliseconds (default from constants)\n */\nexport function streamDelay(ms: number = STREAM_DELAY_MS): Promise<void> {\n\treturn new Promise((resolve) => setTimeout(resolve, ms));\n}\n\n/**\n * Execute an async operation with progress heartbeat\n * Sends periodic updates to keep the user informed during long-running operations\n *\n * @param operation - The async operation to execute\n * @param progressMessage - Message to show during execution\n * @param streamBuffer - StreamBuffer instance for output\n * @param intervalMs - Heartbeat interval (default from constants)\n * @returns The result of the operation\n */\nexport async function withProgressHeartbeat<T>(\n\toperation: () => Promise<T>,\n\tprogressMessage: string,\n\tstreamBuffer: StreamBuffer,\n\tintervalMs: number = PROGRESS_HEARTBEAT_INTERVAL_MS\n): Promise<T> {\n\tif (!streamBuffer.hasCallback()) {\n\t\t// No streaming callback, just execute the operation\n\t\treturn operation();\n\t}\n\n\tconst startTime = Date.now();\n\n\t// Send initial progress indicator\n\tawait streamDelay(30);\n\tstreamBuffer.write(`⏳ ${progressMessage}`);\n\n\ttry {\n\t\tconst result = await operation();\n\t\treturn result;\n\t} finally {\n\t\t// Show final elapsed time\n\t\tconst totalSeconds = Math.floor((Date.now() - startTime) / 1000);\n\t\tif (totalSeconds >= 1) {\n\t\t\tstreamBuffer.write(` (${totalSeconds}s)`);\n\t\t}\n\t\tstreamBuffer.write('\\n\\n');\n\t}\n}\n","import { logger } from '../utils/logger';\n\n/**\n * Knowledge Base result interface\n */\nexport interface KnowledgeBaseResult {\n content: string;\n metadata?: {\n sources?: Array<{\n title: string;\n similarity?: number;\n kb_id?: string | number;\n }>;\n };\n}\n\n/**\n * Knowledge Base by type result interface\n */\nexport interface KnowledgeBaseByTypeResult {\n content: string;\n nodes?: any[];\n count?: number;\n}\n\n/**\n * Combined knowledge base context result\n */\nexport interface CombinedKnowledgeBaseResult {\n globalContext: string;\n userContext: string;\n queryContext: string;\n combinedContext: string;\n}\n\n/**\n * Get relevant knowledge base context for a user prompt (semantic search only - query type)\n * Calls the registered 'knowledge-base.query' collection\n *\n * @param prompt - The user's question/prompt\n * @param collections - The collection registry containing knowledge-base.query\n * @param topK - Number of top results to retrieve (default: 1)\n * @returns Formatted knowledge base context string, or empty string if collection not registered\n */\nconst getKnowledgeBase = async ({\n prompt,\n collections,\n topK = 1\n}: {\n prompt: string;\n collections?: any;\n topK?: number;\n}): Promise<string> => {\n try {\n // Check if knowledge-base collection is registered\n if (!collections || !collections['knowledge-base'] || !collections['knowledge-base']['query']) {\n logger.warn('[KnowledgeBase] knowledge-base.query collection not registered, skipping');\n return '';\n }\n\n\n // Call the registered knowledge-base.query collection\n const result: KnowledgeBaseResult = await collections['knowledge-base']['query']({\n prompt,\n topK\n });\n\n if (!result || !result.content) {\n logger.warn('[KnowledgeBase] No knowledge base results returned');\n return '';\n }\n\n logger.info(`[KnowledgeBase] Retrieved knowledge base context (${result.content.length} chars)`);\n\n // Log sources if available\n if (result.metadata?.sources && result.metadata.sources.length > 0) {\n logger.warn(`[KnowledgeBase] Sources: ${result.metadata.sources.map(s => s.title).join(', ')}`);\n }\n\n return result.content;\n } catch (error) {\n const errorMsg = error instanceof Error ? error.message : String(error);\n logger.warn(`[KnowledgeBase] Error querying knowledge base: ${errorMsg}`);\n // Return empty string on error - don't fail the entire request\n return '';\n }\n};\n\n/**\n * Get global knowledge base nodes (type='global')\n * These are always included in system prompts for all users\n *\n * @param collections - The collection registry containing knowledge-base.getGlobal\n * @param limit - Maximum number of nodes to retrieve (default: 100)\n * @returns Formatted global knowledge base context string\n */\nconst getGlobalKnowledgeBase = async ({\n collections,\n limit = 100\n}: {\n collections?: any;\n limit?: number;\n}): Promise<string> => {\n try {\n // Check if knowledge-base.getGlobal collection is registered\n if (!collections || !collections['knowledge-base'] || !collections['knowledge-base']['getGlobal']) {\n logger.warn('[KnowledgeBase] knowledge-base.getGlobal collection not registered, skipping');\n return '';\n }\n\n\n const result: KnowledgeBaseByTypeResult = await collections['knowledge-base']['getGlobal']({ limit });\n\n if (!result || !result.content) {\n logger.warn('[KnowledgeBase] No global knowledge base nodes found');\n return '';\n }\n\n logger.info(`[KnowledgeBase] Retrieved ${result.count || 0} global knowledge base nodes`);\n return result.content;\n } catch (error) {\n const errorMsg = error instanceof Error ? error.message : String(error);\n logger.warn(`[KnowledgeBase] Error fetching global knowledge base: ${errorMsg}`);\n return '';\n }\n};\n\n/**\n * Get user-specific knowledge base nodes (type='user')\n * These are included only for the specific user based on createdBy field\n *\n * @param collections - The collection registry containing knowledge-base.getByUser\n * @param userId - The user ID to get nodes for\n * @param limit - Maximum number of nodes to retrieve (default: 100)\n * @returns Formatted user knowledge base context string\n */\nconst getUserKnowledgeBase = async ({\n collections,\n userId,\n limit = 100\n}: {\n collections?: any;\n userId?: string | number;\n limit?: number;\n}): Promise<string> => {\n try {\n if (!userId) {\n logger.warn('[KnowledgeBase] No userId provided, skipping user knowledge base');\n return '';\n }\n\n // Check if knowledge-base.getByUser collection is registered\n if (!collections || !collections['knowledge-base'] || !collections['knowledge-base']['getByUser']) {\n logger.warn('[KnowledgeBase] knowledge-base.getByUser collection not registered, skipping');\n return '';\n }\n\n\n const result: KnowledgeBaseByTypeResult = await collections['knowledge-base']['getByUser']({\n userId: String(userId),\n limit\n });\n\n if (!result || !result.content) {\n logger.info(`[KnowledgeBase] No user knowledge base nodes found for userId: ${userId}`);\n return '';\n }\n\n logger.info(`[KnowledgeBase] Retrieved ${result.count || 0} user knowledge base nodes for userId: ${userId}`);\n return result.content;\n } catch (error) {\n const errorMsg = error instanceof Error ? error.message : String(error);\n logger.warn(`[KnowledgeBase] Error fetching user knowledge base: ${errorMsg}`);\n return '';\n }\n};\n\n/**\n * Get all knowledge base context combining global, user, and query-based nodes\n * - Global nodes: Always included for all users\n * - User nodes: Included for the specific user based on createdBy\n * - Query nodes: Semantically searched based on the user prompt\n *\n * @param prompt - The user's question/prompt (for semantic search)\n * @param collections - The collection registry\n * @param userId - The user ID (for user-specific nodes)\n * @param topK - Number of top semantic search results (default: 3)\n * @returns Combined knowledge base context object with all types\n */\nconst getAllKnowledgeBase = async ({\n prompt,\n collections,\n userId,\n topK = 3\n}: {\n prompt: string;\n collections?: any;\n userId?: string | number;\n topK?: number;\n}): Promise<CombinedKnowledgeBaseResult> => {\n\n // Fetch all three types in parallel for better performance\n const [globalContext, userContext, queryContext] = await Promise.all([\n getGlobalKnowledgeBase({ collections }),\n getUserKnowledgeBase({ collections, userId }),\n getKnowledgeBase({ prompt, collections, topK })\n ]);\n\n // Combine all contexts with clear section headers\n let combinedContext = '';\n\n if (globalContext) {\n combinedContext += '## Global Knowledge Base\\n';\n combinedContext += 'The following information applies to all queries:\\n\\n';\n combinedContext += globalContext + '\\n\\n';\n }\n\n if (userContext) {\n combinedContext += '## User-Specific Knowledge Base\\n';\n combinedContext += 'The following information is specific to this user:\\n\\n';\n combinedContext += userContext + '\\n\\n';\n }\n\n if (queryContext) {\n combinedContext += '## Relevant Knowledge Base (Query-Matched)\\n';\n combinedContext += 'The following information is semantically relevant to the current query:\\n\\n';\n combinedContext += queryContext + '\\n\\n';\n }\n\n return {\n globalContext,\n userContext,\n queryContext,\n combinedContext: combinedContext.trim()\n };\n};\n\nconst KB = {\n getKnowledgeBase,\n getGlobalKnowledgeBase,\n getUserKnowledgeBase,\n getAllKnowledgeBase\n};\n\nexport default KB;","import Anthropic from \"@anthropic-ai/sdk\";\nimport Groq from \"groq-sdk\";\nimport { GoogleGenerativeAI, SchemaType, FunctionDeclarationSchemaProperty } from \"@google/generative-ai\";\nimport OpenAI from \"openai\";\nimport { jsonrepair } from 'jsonrepair';\nimport { llmUsageLogger, LLMUsageEntry } from './utils/llm-usage-logger';\nimport { userPromptErrorLogger } from './utils/user-prompt-error-logger';\nimport { stripLoneSurrogates, safeTruncate } from './utils/surrogate';\n\n// System prompt can be either a simple string or an array of content blocks for caching\ntype SystemPrompt = string | Anthropic.Messages.TextBlockParam[];\n\ninterface LLMMessages {\n sys: SystemPrompt;\n user: string;\n prefill?: string; // Optional assistant prefill to force specific output format (e.g., \"{\" for JSON)\n}\n\ninterface LLMOptions {\n model?: string;\n maxTokens?: number;\n temperature?: number;\n topP?: number;\n apiKey?: string;\n partial?: (chunk: string) => void; // Callback for each chunk\n}\n\ninterface Tool {\n name: string;\n description: string;\n input_schema: {\n type: string;\n properties: Record<string, any>;\n required?: string[];\n };\n}\n\ninterface ToolResult {\n tool_use_id: string;\n content: string;\n is_error?: boolean;\n}\n\nexport class LLM {\n /* Get a complete text response from an LLM (Anthropic or Groq) */\n static async text(messages: LLMMessages, options: LLMOptions = {}): Promise<string> {\n const [provider, modelName] = this._parseModel(options.model);\n messages = this._sanitizeMessages(messages);\n\n if (provider === 'anthropic') {\n return this._anthropicText(messages, modelName, options);\n } else if (provider === 'groq') {\n return this._groqText(messages, modelName, options);\n } else if (provider === 'gemini') {\n return this._geminiText(messages, modelName, options);\n } else if (provider === 'openai') {\n return this._openaiText(messages, modelName, options);\n } else {\n throw new Error(`Unsupported provider: ${provider}. Use \"anthropic\", \"groq\", \"gemini\", or \"openai\"`);\n }\n }\n\n /* Stream response from an LLM (Anthropic or Groq) */\n static async stream<T = string>(\n messages: LLMMessages,\n options: LLMOptions = {},\n json?: boolean\n ): Promise<T extends string ? string : any> {\n const [provider, modelName] = this._parseModel(options.model);\n messages = this._sanitizeMessages(messages);\n\n if (provider === 'anthropic') {\n return this._anthropicStream(messages, modelName, options, json);\n } else if (provider === 'groq') {\n return this._groqStream(messages, modelName, options, json);\n } else if (provider === 'gemini') {\n return this._geminiStream(messages, modelName, options, json);\n } else if (provider === 'openai') {\n return this._openaiStream(messages, modelName, options, json);\n } else {\n throw new Error(`Unsupported provider: ${provider}. Use \"anthropic\", \"groq\", \"gemini\", or \"openai\"`);\n }\n }\n\n /* Stream response with tool calling support (Anthropic and Gemini) */\n static async streamWithTools(\n messages: LLMMessages,\n tools: Tool[],\n toolHandler: (toolName: string, toolInput: any) => Promise<any>,\n options: LLMOptions = {},\n maxIterations: number = 3\n ): Promise<string> {\n const [provider, modelName] = this._parseModel(options.model);\n messages = this._sanitizeMessages(messages);\n\n if (provider === 'anthropic') {\n return this._anthropicStreamWithTools(messages, tools, toolHandler, modelName, options, maxIterations);\n } else if (provider === 'gemini') {\n return this._geminiStreamWithTools(messages, tools, toolHandler, modelName, options, maxIterations);\n } else if (provider === 'openai') {\n return this._openaiStreamWithTools(messages, tools, toolHandler, modelName, options, maxIterations);\n } else {\n throw new Error(`Tool calling is only supported for Anthropic, Gemini, and OpenAI models`);\n }\n }\n\n // ============================================================\n // PRIVATE HELPER METHODS\n // ============================================================\n\n /**\n * Normalize system prompt to Anthropic format\n * Converts string to array format if needed\n * @param sys - System prompt (string or array of blocks)\n * @returns Normalized system prompt for Anthropic API\n */\n private static _normalizeSystemPrompt(sys: SystemPrompt): string | Anthropic.Messages.TextBlockParam[] {\n // If it's already a string, return as-is for backward compatibility\n if (typeof sys === 'string') {\n return sys;\n }\n\n // If it's an array, return it for caching support\n return sys;\n }\n\n /**\n * Strip unpaired UTF-16 surrogates from every text field of a message set.\n *\n * A lone surrogate (from mid-pair string slicing or corrupt source data)\n * serializes to a bare `\\udXXX` escape that strict JSON parsers — including\n * the one on Anthropic's API — reject with \"no low surrogate in string\",\n * failing the whole request. Sanitizing here, at the single boundary every\n * provider call flows through, guarantees no request can carry one.\n */\n private static _sanitizeMessages(messages: LLMMessages): LLMMessages {\n const sys = typeof messages.sys === 'string'\n ? stripLoneSurrogates(messages.sys)\n : messages.sys.map(block =>\n block?.type === 'text' && typeof block.text === 'string'\n ? { ...block, text: stripLoneSurrogates(block.text) }\n : block\n );\n return {\n ...messages,\n sys,\n user: stripLoneSurrogates(messages.user),\n ...(messages.prefill !== undefined && { prefill: stripLoneSurrogates(messages.prefill) }),\n };\n }\n\n /**\n * Log cache usage metrics from Anthropic API response\n * Shows cache hits, costs, and savings\n */\n private static _logCacheUsage(usage: any): void {\n if (!usage) return;\n\n const inputTokens = usage.input_tokens || 0;\n const cacheCreationTokens = usage.cache_creation_input_tokens || 0;\n const cacheReadTokens = usage.cache_read_input_tokens || 0;\n const outputTokens = usage.output_tokens || 0;\n\n // Pricing (per 1M tokens) for Claude Haiku 4.5\n const INPUT_PRICE = 0.80; // $0.80 per 1M tokens\n const OUTPUT_PRICE = 4.00; // $4.00 per 1M tokens\n const CACHE_WRITE_PRICE = 1.00; // $1.00 per 1M tokens (25% markup)\n const CACHE_READ_PRICE = 0.08; // $0.08 per 1M tokens (90% discount)\n\n // Calculate costs\n const regularInputCost = (inputTokens / 1_000_000) * INPUT_PRICE;\n const cacheWriteCost = (cacheCreationTokens / 1_000_000) * CACHE_WRITE_PRICE;\n const cacheReadCost = (cacheReadTokens / 1_000_000) * CACHE_READ_PRICE;\n const outputCost = (outputTokens / 1_000_000) * OUTPUT_PRICE;\n const totalCost = regularInputCost + cacheWriteCost + cacheReadCost + outputCost;\n\n // Calculate what it would have cost without caching\n const totalInputTokens = inputTokens + cacheCreationTokens + cacheReadTokens;\n const costWithoutCache = (totalInputTokens / 1_000_000) * INPUT_PRICE + outputCost;\n const savings = costWithoutCache - totalCost;\n const savingsPercent = costWithoutCache > 0 ? (savings / costWithoutCache * 100) : 0;\n\n // Log metrics\n console.log('\\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');\n console.log('💰 PROMPT CACHING METRICS');\n console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');\n\n console.log('\\n📊 Token Usage:');\n console.log(` Input (regular): ${inputTokens.toLocaleString()} tokens`);\n if (cacheCreationTokens > 0) {\n console.log(` Cache write: ${cacheCreationTokens.toLocaleString()} tokens (first request)`);\n }\n if (cacheReadTokens > 0) {\n console.log(` Cache read: ${cacheReadTokens.toLocaleString()} tokens ⚡ HIT!`);\n }\n console.log(` Output: ${outputTokens.toLocaleString()} tokens`);\n console.log(` Total input: ${totalInputTokens.toLocaleString()} tokens`);\n\n console.log('\\n💵 Cost Breakdown:');\n console.log(` Input (regular): $${regularInputCost.toFixed(6)}`);\n if (cacheCreationTokens > 0) {\n console.log(` Cache write: $${cacheWriteCost.toFixed(6)}`);\n }\n if (cacheReadTokens > 0) {\n console.log(` Cache read: $${cacheReadCost.toFixed(6)} (90% off!)`);\n }\n console.log(` Output: $${outputCost.toFixed(6)}`);\n console.log(` ─────────────────────────────────────`);\n console.log(` Total cost: $${totalCost.toFixed(6)}`);\n\n if (cacheReadTokens > 0) {\n console.log(`\\n💎 Savings: $${savings.toFixed(6)} (${savingsPercent.toFixed(1)}% off)`);\n console.log(` Without cache: $${costWithoutCache.toFixed(6)}`);\n } else if (cacheCreationTokens > 0) {\n console.log(`\\n⏱️ Cache created - next request will be ~90% cheaper!`);\n }\n\n console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n');\n }\n\n /**\n * Parse model string to extract provider and model name\n * @param modelString - Format: \"provider/model-name\" or just \"model-name\"\n * @returns [provider, modelName]\n *\n * @example\n * \"anthropic/claude-sonnet-4-5\" → [\"anthropic\", \"claude-sonnet-4-5\"]\n * \"groq/openai/gpt-oss-120b\" → [\"groq\", \"openai/gpt-oss-120b\"]\n * \"claude-sonnet-4-5\" → [\"anthropic\", \"claude-sonnet-4-5\"] (default)\n */\n private static _parseModel(modelString?: string): [string, string] {\n if (!modelString) {\n // Default to Anthropic Claude\n return ['anthropic', 'claude-sonnet-4-5'];\n }\n\n // Check if model string has provider prefix\n if (modelString.includes('/')) {\n // Split only on the FIRST slash to handle models like \"groq/openai/gpt-oss-120b\"\n const firstSlashIndex = modelString.indexOf('/');\n const provider = modelString.substring(0, firstSlashIndex).toLowerCase().trim();\n const model = modelString.substring(firstSlashIndex + 1).trim();\n return [provider, model];\n }\n\n // No prefix, assume Anthropic\n return ['anthropic', modelString];\n }\n\n // ============================================================\n // ANTHROPIC IMPLEMENTATION\n // ============================================================\n\n private static async _anthropicText(\n messages: LLMMessages,\n modelName: string,\n options: LLMOptions\n ): Promise<string> {\n const startTime = Date.now();\n const requestId = llmUsageLogger.generateRequestId();\n const apiKey = options.apiKey || process.env.ANTHROPIC_API_KEY || \"\";\n const client = new Anthropic({\n apiKey: apiKey,\n });\n\n try {\n const response = await client.messages.create({\n model: modelName,\n max_tokens: options.maxTokens || 1000,\n temperature: options.temperature,\n system: this._normalizeSystemPrompt(messages.sys) as any,\n messages: [{\n role: \"user\",\n content: messages.user\n }]\n });\n\n const durationMs = Date.now() - startTime;\n const usage = response.usage as any;\n\n // Log usage\n const inputTokens = usage?.input_tokens || 0;\n const outputTokens = usage?.output_tokens || 0;\n const cacheReadTokens = usage?.cache_read_input_tokens || 0;\n const cacheWriteTokens = usage?.cache_creation_input_tokens || 0;\n\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'anthropic',\n model: modelName,\n method: 'text',\n inputTokens,\n outputTokens,\n cacheReadTokens,\n cacheWriteTokens,\n totalTokens: inputTokens + outputTokens + cacheReadTokens + cacheWriteTokens,\n costUSD: llmUsageLogger.calculateCost(modelName, inputTokens, outputTokens, cacheReadTokens, cacheWriteTokens),\n durationMs,\n success: true,\n });\n\n const textBlock = response.content.find(block => block.type === 'text');\n return textBlock?.type === 'text' ? textBlock.text : '';\n } catch (error) {\n const durationMs = Date.now() - startTime;\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'anthropic',\n model: modelName,\n method: 'text',\n inputTokens: 0,\n outputTokens: 0,\n totalTokens: 0,\n costUSD: 0,\n durationMs,\n success: false,\n error: error instanceof Error ? error.message : String(error),\n });\n throw error;\n }\n }\n\n private static async _anthropicStream(\n messages: LLMMessages,\n modelName: string,\n options: LLMOptions,\n json?: boolean\n ): Promise<any> {\n const startTime = Date.now();\n const requestId = llmUsageLogger.generateRequestId();\n const apiKey = options.apiKey || process.env.ANTHROPIC_API_KEY || \"\";\n const client = new Anthropic({\n apiKey: apiKey,\n });\n\n try {\n // Build messages array\n const apiMessages: Anthropic.Messages.MessageParam[] = [{\n role: \"user\",\n content: messages.user\n }];\n\n // Add prefill to force specific output format (e.g., \"{\" for JSON)\n // For JSON mode, automatically prefill with \"{\" if no explicit prefill provided\n const prefill = messages.prefill || (json ? \"{\" : undefined);\n if (prefill) {\n apiMessages.push({\n role: \"assistant\",\n content: prefill\n });\n }\n\n const stream = await client.messages.create({\n model: modelName,\n max_tokens: options.maxTokens || 1000,\n temperature: options.temperature,\n system: this._normalizeSystemPrompt(messages.sys) as any,\n messages: apiMessages,\n stream: true,\n });\n\n // Start with prefill if provided (since model continues from there)\n let fullText = prefill || '';\n let usage: any = null;\n let inputTokens = 0;\n let outputTokens = 0;\n let cacheReadTokens = 0;\n let cacheWriteTokens = 0;\n\n // Process stream\n for await (const chunk of stream) {\n if (chunk.type === 'content_block_delta' && chunk.delta.type === 'text_delta') {\n const text = chunk.delta.text;\n fullText += text;\n\n // Call partial callback if provided\n if (options.partial) {\n options.partial(text);\n }\n } else if (chunk.type === 'message_start' && (chunk as any).message?.usage) {\n // Capture input tokens from message_start\n const msgUsage = (chunk as any).message.usage;\n inputTokens = msgUsage.input_tokens || 0;\n cacheReadTokens = msgUsage.cache_read_input_tokens || 0;\n cacheWriteTokens = msgUsage.cache_creation_input_tokens || 0;\n } else if (chunk.type === 'message_delta' && (chunk as any).usage) {\n // Capture output tokens from message_delta event\n usage = (chunk as any).usage;\n outputTokens = usage.output_tokens || 0;\n }\n }\n\n const durationMs = Date.now() - startTime;\n\n // Log usage\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'anthropic',\n model: modelName,\n method: 'stream',\n inputTokens,\n outputTokens,\n cacheReadTokens,\n cacheWriteTokens,\n totalTokens: inputTokens + outputTokens + cacheReadTokens + cacheWriteTokens,\n costUSD: llmUsageLogger.calculateCost(modelName, inputTokens, outputTokens, cacheReadTokens, cacheWriteTokens),\n durationMs,\n success: true,\n });\n\n // Return parsed JSON or text\n if (json) {\n return this._parseJSON(fullText);\n }\n\n return fullText;\n } catch (error) {\n const durationMs = Date.now() - startTime;\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'anthropic',\n model: modelName,\n method: 'stream',\n inputTokens: 0,\n outputTokens: 0,\n totalTokens: 0,\n costUSD: 0,\n durationMs,\n success: false,\n error: error instanceof Error ? error.message : String(error),\n });\n throw error;\n }\n }\n\n private static async _anthropicStreamWithTools(\n messages: LLMMessages,\n tools: Tool[],\n toolHandler: (toolName: string, toolInput: any) => Promise<any>,\n modelName: string,\n options: LLMOptions,\n maxIterations: number\n ): Promise<string> {\n const methodStartTime = Date.now();\n const apiKey = options.apiKey || process.env.ANTHROPIC_API_KEY || \"\";\n const client = new Anthropic({\n apiKey: apiKey,\n });\n\n // Conversation history for tool calling loop\n const conversationMessages: Anthropic.MessageParam[] = [{\n role: \"user\",\n content: messages.user\n }];\n\n let iterations = 0;\n let finalText = '';\n let totalToolCalls = 0;\n\n // Track cumulative usage across all iterations\n let totalInputTokens = 0;\n let totalOutputTokens = 0;\n let totalCacheReadTokens = 0;\n let totalCacheWriteTokens = 0;\n\n while (iterations < maxIterations) {\n iterations++;\n const iterationStartTime = Date.now();\n const requestId = llmUsageLogger.generateRequestId();\n\n // Use streaming API\n const stream = await client.messages.create({\n model: modelName,\n max_tokens: options.maxTokens || 4000,\n temperature: options.temperature,\n system: this._normalizeSystemPrompt(messages.sys) as any,\n messages: conversationMessages,\n tools: tools as any,\n stream: true, // Enable streaming\n });\n\n // Collect the response content and stop reason\n let stopReason: string | null = null;\n const contentBlocks: any[] = [];\n let currentTextBlock = '';\n let currentToolUse: any = null;\n let inputTokens = 0;\n let outputTokens = 0;\n let cacheReadTokens = 0;\n let cacheWriteTokens = 0;\n\n // Process the stream\n for await (const chunk of stream) {\n if (chunk.type === 'message_start') {\n // Reset for new message\n contentBlocks.length = 0;\n currentTextBlock = '';\n currentToolUse = null;\n // Capture input tokens from message_start\n const msgUsage = (chunk as any).message?.usage;\n if (msgUsage) {\n inputTokens = msgUsage.input_tokens || 0;\n cacheReadTokens = msgUsage.cache_read_input_tokens || 0;\n cacheWriteTokens = msgUsage.cache_creation_input_tokens || 0;\n }\n }\n\n if (chunk.type === 'content_block_start') {\n if (chunk.content_block.type === 'text') {\n currentTextBlock = '';\n } else if (chunk.content_block.type === 'tool_use') {\n currentToolUse = {\n type: 'tool_use',\n id: chunk.content_block.id,\n name: chunk.content_block.name,\n input: {}\n };\n }\n }\n\n if (chunk.type === 'content_block_delta') {\n if (chunk.delta.type === 'text_delta') {\n // Stream text chunks to callback\n const text = chunk.delta.text;\n currentTextBlock += text;\n if (options.partial) {\n options.partial(text);\n }\n } else if (chunk.delta.type === 'input_json_delta' && currentToolUse) {\n // Accumulate tool input JSON\n currentToolUse.inputJson = (currentToolUse.inputJson || '') + chunk.delta.partial_json;\n }\n }\n\n if (chunk.type === 'content_block_stop') {\n if (currentTextBlock) {\n contentBlocks.push({\n type: 'text',\n text: currentTextBlock\n });\n finalText = currentTextBlock; // Keep track of final text\n currentTextBlock = '';\n } else if (currentToolUse) {\n // Parse the accumulated JSON input\n try {\n currentToolUse.input = currentToolUse.inputJson ? JSON.parse(currentToolUse.inputJson) : {};\n } catch (error) {\n currentToolUse.input = {};\n }\n delete currentToolUse.inputJson;\n contentBlocks.push(currentToolUse);\n currentToolUse = null;\n }\n }\n\n if (chunk.type === 'message_delta') {\n stopReason = chunk.delta.stop_reason || stopReason;\n // Capture output tokens from message_delta\n if ((chunk as any).usage) {\n outputTokens = (chunk as any).usage.output_tokens || 0;\n }\n }\n\n if (chunk.type === 'message_stop') {\n // Message complete\n break;\n }\n }\n\n const iterationDuration = Date.now() - iterationStartTime;\n const toolUsesInIteration = contentBlocks.filter(block => block.type === 'tool_use').length;\n totalToolCalls += toolUsesInIteration;\n\n // Update cumulative totals\n totalInputTokens += inputTokens;\n totalOutputTokens += outputTokens;\n totalCacheReadTokens += cacheReadTokens;\n totalCacheWriteTokens += cacheWriteTokens;\n\n // Log this iteration\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'anthropic',\n model: modelName,\n method: `streamWithTools[iter=${iterations}]`,\n inputTokens,\n outputTokens,\n cacheReadTokens,\n cacheWriteTokens,\n totalTokens: inputTokens + outputTokens + cacheReadTokens + cacheWriteTokens,\n costUSD: llmUsageLogger.calculateCost(modelName, inputTokens, outputTokens, cacheReadTokens, cacheWriteTokens),\n durationMs: iterationDuration,\n toolCalls: toolUsesInIteration,\n success: true,\n });\n\n // Check stop reason\n if (stopReason === 'end_turn') {\n // Final text response already streamed\n break;\n }\n\n if (stopReason === 'tool_use') {\n // Find all tool use blocks\n const toolUses = contentBlocks.filter(block => block.type === 'tool_use');\n\n if (toolUses.length === 0) {\n break;\n }\n\n // Add assistant's response to conversation\n conversationMessages.push({\n role: \"assistant\",\n content: contentBlocks\n });\n\n // Execute all tools and collect results\n const toolResults: Anthropic.MessageParam = {\n role: \"user\",\n content: []\n };\n\n // Execute all tools in parallel for lower latency\n const toolResultEntries = await Promise.all(toolUses.map(async (toolUse) => {\n try {\n const result = await toolHandler(toolUse.name, toolUse.input);\n // Truncate very large results to reduce memory and token usage\n let resultContent = typeof result === 'string' ? result : JSON.stringify(result);\n const MAX_RESULT_LENGTH = 50000; // ~12k tokens max per tool result\n if (resultContent.length > MAX_RESULT_LENGTH) {\n // Surrogate-aware cut so the truncation can't split a UTF-16 pair.\n resultContent = safeTruncate(resultContent, MAX_RESULT_LENGTH) +\n '\\n\\n... [Result truncated - showing first 50000 characters of ' + resultContent.length + ' total]';\n }\n return {\n type: 'tool_result' as const,\n tool_use_id: toolUse.id,\n // Final safety net: tool results carry source data and are built\n // mid-loop (after entry-point sanitize), so strip lone surrogates here.\n content: stripLoneSurrogates(resultContent)\n };\n } catch (error) {\n return {\n type: 'tool_result' as const,\n tool_use_id: toolUse.id,\n content: error instanceof Error ? error.message : String(error),\n is_error: true\n };\n }\n }));\n toolResultEntries.forEach(entry => (toolResults.content as any[]).push(entry));\n\n // Add tool results to conversation\n conversationMessages.push(toolResults);\n } else {\n // Unexpected stop reason, final text already streamed\n break;\n }\n }\n\n // Log total summary for all iterations\n const totalDuration = Date.now() - methodStartTime;\n if (iterations > 1) {\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId: llmUsageLogger.generateRequestId(),\n provider: 'anthropic',\n model: modelName,\n method: `streamWithTools[TOTAL:${iterations}iters]`,\n inputTokens: totalInputTokens,\n outputTokens: totalOutputTokens,\n cacheReadTokens: totalCacheReadTokens,\n cacheWriteTokens: totalCacheWriteTokens,\n totalTokens: totalInputTokens + totalOutputTokens + totalCacheReadTokens + totalCacheWriteTokens,\n costUSD: llmUsageLogger.calculateCost(modelName, totalInputTokens, totalOutputTokens, totalCacheReadTokens, totalCacheWriteTokens),\n durationMs: totalDuration,\n toolCalls: totalToolCalls,\n success: true,\n });\n }\n\n if (iterations >= maxIterations) {\n throw new Error(`Max iterations (${maxIterations}) reached in tool calling loop`);\n }\n\n return finalText;\n }\n\n // ============================================================\n // GROQ IMPLEMENTATION\n // ============================================================\n\n private static async _groqText(\n messages: LLMMessages,\n modelName: string,\n options: LLMOptions\n ): Promise<string> {\n const startTime = Date.now();\n const requestId = llmUsageLogger.generateRequestId();\n const client = new Groq({\n apiKey: options.apiKey || process.env.GROQ_API_KEY || \"\",\n });\n\n try {\n const response = await client.chat.completions.create({\n model: modelName,\n messages: [\n { role: 'system', content: messages.sys as string },\n { role: 'user', content: messages.user }\n ],\n temperature: options.temperature,\n max_tokens: options.maxTokens || 1000,\n });\n\n const durationMs = Date.now() - startTime;\n const usage = response.usage;\n const inputTokens = usage?.prompt_tokens || 0;\n const outputTokens = usage?.completion_tokens || 0;\n\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'groq',\n model: modelName,\n method: 'text',\n inputTokens,\n outputTokens,\n totalTokens: inputTokens + outputTokens,\n costUSD: llmUsageLogger.calculateCost(modelName, inputTokens, outputTokens),\n durationMs,\n success: true,\n });\n\n return response.choices[0]?.message?.content || '';\n } catch (error) {\n const durationMs = Date.now() - startTime;\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'groq',\n model: modelName,\n method: 'text',\n inputTokens: 0,\n outputTokens: 0,\n totalTokens: 0,\n costUSD: 0,\n durationMs,\n success: false,\n error: error instanceof Error ? error.message : String(error),\n });\n throw error;\n }\n }\n\n private static async _groqStream(\n messages: LLMMessages,\n modelName: string,\n options: LLMOptions,\n json?: boolean\n ): Promise<any> {\n const startTime = Date.now();\n const requestId = llmUsageLogger.generateRequestId();\n const apiKey = options.apiKey || process.env.GROQ_API_KEY || \"\";\n const client = new Groq({\n apiKey: apiKey,\n });\n\n try {\n const stream = await client.chat.completions.create({\n model: modelName,\n messages: [\n { role: 'system', content: messages.sys as string },\n { role: 'user', content: messages.user }\n ],\n temperature: options.temperature,\n max_tokens: options.maxTokens || 1000,\n stream: true,\n response_format: json ? { type: 'json_object' } : undefined\n });\n\n let fullText = '';\n let inputTokens = 0;\n let outputTokens = 0;\n\n // Process stream\n for await (const chunk of stream) {\n const text = chunk.choices[0]?.delta?.content || '';\n if (text) {\n fullText += text;\n\n // Call partial callback if provided\n if (options.partial) {\n options.partial(text);\n }\n }\n // Groq provides usage in the last chunk\n if ((chunk as any).x_groq?.usage) {\n inputTokens = (chunk as any).x_groq.usage.prompt_tokens || 0;\n outputTokens = (chunk as any).x_groq.usage.completion_tokens || 0;\n }\n }\n\n const durationMs = Date.now() - startTime;\n\n // Estimate tokens if not provided (Groq streaming may not always provide usage)\n if (inputTokens === 0) {\n // Rough estimate: 1 token ≈ 4 characters\n const sysPrompt = typeof messages.sys === 'string' ? messages.sys : messages.sys.map(b => b.text).join('');\n inputTokens = Math.ceil((sysPrompt.length + messages.user.length) / 4);\n }\n if (outputTokens === 0) {\n outputTokens = Math.ceil(fullText.length / 4);\n }\n\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'groq',\n model: modelName,\n method: 'stream',\n inputTokens,\n outputTokens,\n totalTokens: inputTokens + outputTokens,\n costUSD: llmUsageLogger.calculateCost(modelName, inputTokens, outputTokens),\n durationMs,\n success: true,\n });\n\n // Return parsed JSON or text\n if (json) {\n return this._parseJSON(fullText);\n }\n\n return fullText;\n } catch (error) {\n const durationMs = Date.now() - startTime;\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'groq',\n model: modelName,\n method: 'stream',\n inputTokens: 0,\n outputTokens: 0,\n totalTokens: 0,\n costUSD: 0,\n durationMs,\n success: false,\n error: error instanceof Error ? error.message : String(error),\n });\n throw error;\n }\n }\n\n // ============================================================\n // GEMINI IMPLEMENTATION\n // ============================================================\n\n private static async _geminiText(\n messages: LLMMessages,\n modelName: string,\n options: LLMOptions\n ): Promise<string> {\n const startTime = Date.now();\n const requestId = llmUsageLogger.generateRequestId();\n const apiKey = options.apiKey || process.env.GEMINI_API_KEY || \"\";\n const genAI = new GoogleGenerativeAI(apiKey);\n\n // Convert system prompt to string if it's an array\n const systemPrompt = typeof messages.sys === 'string'\n ? messages.sys\n : messages.sys.map(block => block.text).join('\\n');\n\n try {\n const model = genAI.getGenerativeModel({\n model: modelName,\n systemInstruction: systemPrompt,\n generationConfig: {\n maxOutputTokens: options.maxTokens || 1000,\n temperature: options.temperature,\n topP: options.topP,\n }\n });\n\n const result = await model.generateContent(messages.user);\n const response = await result.response;\n const text = response.text();\n const durationMs = Date.now() - startTime;\n\n // Gemini provides usage metadata\n const usage = response.usageMetadata;\n const inputTokens = usage?.promptTokenCount || Math.ceil((systemPrompt.length + messages.user.length) / 4);\n const outputTokens = usage?.candidatesTokenCount || Math.ceil(text.length / 4);\n\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'gemini',\n model: modelName,\n method: 'text',\n inputTokens,\n outputTokens,\n totalTokens: inputTokens + outputTokens,\n costUSD: llmUsageLogger.calculateCost(modelName, inputTokens, outputTokens),\n durationMs,\n success: true,\n });\n\n return text;\n } catch (error) {\n const durationMs = Date.now() - startTime;\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'gemini',\n model: modelName,\n method: 'text',\n inputTokens: 0,\n outputTokens: 0,\n totalTokens: 0,\n costUSD: 0,\n durationMs,\n success: false,\n error: error instanceof Error ? error.message : String(error),\n });\n throw error;\n }\n }\n\n private static async _geminiStream(\n messages: LLMMessages,\n modelName: string,\n options: LLMOptions,\n json?: boolean\n ): Promise<any> {\n const startTime = Date.now();\n const requestId = llmUsageLogger.generateRequestId();\n const apiKey = options.apiKey || process.env.GEMINI_API_KEY || \"\";\n const genAI = new GoogleGenerativeAI(apiKey);\n\n // Convert system prompt to string if it's an array\n const systemPrompt = typeof messages.sys === 'string'\n ? messages.sys\n : messages.sys.map(block => block.text).join('\\n');\n\n try {\n // For JSON mode with partial callback, use streaming for early answer component extraction\n // but wait for complete response before parsing JSON\n if (json && options.partial) {\n const model = genAI.getGenerativeModel({\n model: modelName,\n systemInstruction: systemPrompt,\n generationConfig: {\n maxOutputTokens: options.maxTokens || 1000,\n temperature: options.temperature,\n topP: options.topP,\n responseMimeType: 'application/json',\n }\n });\n\n const result = await model.generateContentStream(messages.user);\n let fullText = '';\n let inputTokens = 0;\n let outputTokens = 0;\n\n // Stream chunks to partial callback for early answer component extraction\n for await (const chunk of result.stream) {\n try {\n const text = chunk.text();\n if (text) {\n fullText += text;\n options.partial(text); // Enable early answer component detection\n }\n } catch (chunkError) {\n // Some chunks may not have text, continue\n }\n if (chunk.usageMetadata) {\n inputTokens = chunk.usageMetadata.promptTokenCount || 0;\n outputTokens = chunk.usageMetadata.candidatesTokenCount || 0;\n }\n }\n\n const durationMs = Date.now() - startTime;\n if (inputTokens === 0) {\n inputTokens = Math.ceil((systemPrompt.length + messages.user.length) / 4);\n }\n if (outputTokens === 0) {\n outputTokens = Math.ceil(fullText.length / 4);\n }\n\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'gemini',\n model: modelName,\n method: 'stream-json-partial',\n inputTokens,\n outputTokens,\n totalTokens: inputTokens + outputTokens,\n costUSD: llmUsageLogger.calculateCost(modelName, inputTokens, outputTokens),\n durationMs,\n success: true,\n });\n\n // Parse complete JSON after stream finishes\n return this._parseJSON(fullText);\n }\n\n // For JSON mode without partial callback, use non-streaming for reliability\n if (json) {\n const model = genAI.getGenerativeModel({\n model: modelName,\n systemInstruction: systemPrompt,\n generationConfig: {\n maxOutputTokens: options.maxTokens || 1000,\n temperature: options.temperature,\n topP: options.topP,\n responseMimeType: 'application/json',\n }\n });\n\n const result = await model.generateContent(messages.user);\n const response = result.response;\n const fullText = response.text();\n const durationMs = Date.now() - startTime;\n\n // Capture usage\n const usage = response.usageMetadata;\n const inputTokens = usage?.promptTokenCount || Math.ceil((systemPrompt.length + messages.user.length) / 4);\n const outputTokens = usage?.candidatesTokenCount || Math.ceil(fullText.length / 4);\n\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'gemini',\n model: modelName,\n method: 'stream-json',\n inputTokens,\n outputTokens,\n totalTokens: inputTokens + outputTokens,\n costUSD: llmUsageLogger.calculateCost(modelName, inputTokens, outputTokens),\n durationMs,\n success: true,\n });\n\n return this._parseJSON(fullText);\n }\n\n // For text mode, use streaming as before\n const model = genAI.getGenerativeModel({\n model: modelName,\n systemInstruction: systemPrompt,\n generationConfig: {\n maxOutputTokens: options.maxTokens || 1000,\n temperature: options.temperature,\n topP: options.topP,\n }\n });\n\n const result = await model.generateContentStream(messages.user);\n\n let fullText = '';\n let inputTokens = 0;\n let outputTokens = 0;\n\n // Process stream\n for await (const chunk of result.stream) {\n try {\n const text = chunk.text();\n if (text) {\n fullText += text;\n\n // Call partial callback if provided\n if (options.partial) {\n options.partial(text);\n }\n }\n } catch (chunkError) {\n // Some chunks may not have text (e.g., thinking tokens)\n // Continue processing other chunks\n }\n // Capture usage from final chunk\n if (chunk.usageMetadata) {\n inputTokens = chunk.usageMetadata.promptTokenCount || 0;\n outputTokens = chunk.usageMetadata.candidatesTokenCount || 0;\n }\n }\n\n const durationMs = Date.now() - startTime;\n\n // Estimate tokens if not provided\n if (inputTokens === 0) {\n inputTokens = Math.ceil((systemPrompt.length + messages.user.length) / 4);\n }\n if (outputTokens === 0) {\n outputTokens = Math.ceil(fullText.length / 4);\n }\n\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'gemini',\n model: modelName,\n method: 'stream',\n inputTokens,\n outputTokens,\n totalTokens: inputTokens + outputTokens,\n costUSD: llmUsageLogger.calculateCost(modelName, inputTokens, outputTokens),\n durationMs,\n success: true,\n });\n\n return fullText;\n } catch (error) {\n const durationMs = Date.now() - startTime;\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'gemini',\n model: modelName,\n method: 'stream',\n inputTokens: 0,\n outputTokens: 0,\n totalTokens: 0,\n costUSD: 0,\n durationMs,\n success: false,\n error: error instanceof Error ? error.message : String(error),\n });\n throw error;\n }\n }\n\n /**\n * Recursively strip unsupported JSON Schema properties for Gemini\n * Gemini doesn't support: additionalProperties, $schema, etc.\n */\n private static _cleanSchemaForGemini(obj: any): any {\n if (obj === null || typeof obj !== 'object') {\n return obj;\n }\n\n if (Array.isArray(obj)) {\n return obj.map(item => this._cleanSchemaForGemini(item));\n }\n\n const cleaned: any = {};\n for (const [key, value] of Object.entries(obj)) {\n // Skip properties that Gemini doesn't support\n if (key === 'additionalProperties' || key === '$schema') {\n continue;\n }\n cleaned[key] = this._cleanSchemaForGemini(value);\n }\n return cleaned;\n }\n\n private static async _geminiStreamWithTools(\n messages: LLMMessages,\n tools: Tool[],\n toolHandler: (toolName: string, toolInput: any) => Promise<any>,\n modelName: string,\n options: LLMOptions,\n maxIterations: number\n ): Promise<string> {\n const methodStartTime = Date.now();\n const apiKey = options.apiKey || process.env.GEMINI_API_KEY || \"\";\n const genAI = new GoogleGenerativeAI(apiKey);\n\n // Convert system prompt to string if it's an array\n const systemPrompt = typeof messages.sys === 'string'\n ? messages.sys\n : messages.sys.map(block => block.text).join('\\n');\n\n // Convert tools to Gemini function declarations format\n // Clean schemas to remove unsupported properties (additionalProperties, $schema, etc.)\n const functionDeclarations = tools.map(tool => ({\n name: tool.name,\n description: tool.description,\n parameters: {\n type: SchemaType.OBJECT,\n properties: this._cleanSchemaForGemini(tool.input_schema.properties) as { [key: string]: FunctionDeclarationSchemaProperty },\n required: tool.input_schema.required || []\n }\n }));\n\n const model = genAI.getGenerativeModel({\n model: modelName,\n systemInstruction: systemPrompt,\n tools: [{ functionDeclarations }],\n generationConfig: {\n maxOutputTokens: options.maxTokens || 4000,\n temperature: options.temperature,\n topP: options.topP,\n }\n });\n\n // Start a chat session to maintain conversation history for tool calling loop\n const chat = model.startChat({\n history: []\n });\n\n let iterations = 0;\n let finalText = '';\n let currentUserMessage = messages.user;\n let totalToolCalls = 0;\n\n // Track cumulative usage across all iterations\n let totalInputTokens = 0;\n let totalOutputTokens = 0;\n\n while (iterations < maxIterations) {\n iterations++;\n const iterationStartTime = Date.now();\n const requestId = llmUsageLogger.generateRequestId();\n\n // Send message and stream the response\n const result = await chat.sendMessageStream(currentUserMessage);\n\n let responseText = '';\n const functionCalls: Array<{ name: string; args: any }> = [];\n let inputTokens = 0;\n let outputTokens = 0;\n\n // Process the stream\n for await (const chunk of result.stream) {\n const candidate = chunk.candidates?.[0];\n if (!candidate) continue;\n\n for (const part of candidate.content?.parts || []) {\n if (part.text) {\n responseText += part.text;\n // Stream text chunks to callback\n if (options.partial) {\n options.partial(part.text);\n }\n } else if (part.functionCall) {\n functionCalls.push({\n name: part.functionCall.name,\n args: part.functionCall.args\n });\n }\n }\n\n // Capture usage from chunks\n if (chunk.usageMetadata) {\n inputTokens = chunk.usageMetadata.promptTokenCount || 0;\n outputTokens = chunk.usageMetadata.candidatesTokenCount || 0;\n }\n }\n\n const iterationDuration = Date.now() - iterationStartTime;\n const toolCallsInIteration = functionCalls.length;\n totalToolCalls += toolCallsInIteration;\n\n // Estimate tokens if not provided by API\n if (inputTokens === 0) {\n const userMsg = typeof currentUserMessage === 'string' ? currentUserMessage : JSON.stringify(currentUserMessage);\n inputTokens = Math.ceil((systemPrompt.length + userMsg.length) / 4);\n }\n if (outputTokens === 0) {\n outputTokens = Math.ceil(responseText.length / 4);\n }\n\n // Update cumulative totals\n totalInputTokens += inputTokens;\n totalOutputTokens += outputTokens;\n\n // Log this iteration\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'gemini',\n model: modelName,\n method: `streamWithTools[iter=${iterations}]`,\n inputTokens,\n outputTokens,\n totalTokens: inputTokens + outputTokens,\n costUSD: llmUsageLogger.calculateCost(modelName, inputTokens, outputTokens),\n durationMs: iterationDuration,\n toolCalls: toolCallsInIteration,\n success: true,\n });\n\n // If no function calls, we're done\n if (functionCalls.length === 0) {\n finalText = responseText;\n break;\n }\n\n // Execute all function calls and collect results\n const functionResponses: Array<{ name: string; response: any }> = [];\n\n // Execute all function calls in parallel for lower latency\n const responses = await Promise.all(functionCalls.map(async (fc) => {\n try {\n const result = await toolHandler(fc.name, fc.args);\n // Truncate very large results to reduce memory and token usage\n let resultContent = typeof result === 'string' ? result : JSON.stringify(result);\n const MAX_RESULT_LENGTH = 50000; // ~12k tokens max per tool result\n if (resultContent.length > MAX_RESULT_LENGTH) {\n // Surrogate-aware cut so the truncation can't split a UTF-16 pair.\n resultContent = safeTruncate(resultContent, MAX_RESULT_LENGTH) +\n '\\n\\n... [Result truncated - showing first 50000 characters of ' + resultContent.length + ' total]';\n }\n return {\n name: fc.name,\n // Final safety net: strip lone surrogates from source-data results.\n response: { result: stripLoneSurrogates(resultContent) }\n };\n } catch (error) {\n return {\n name: fc.name,\n response: { error: error instanceof Error ? error.message : String(error) }\n };\n }\n }));\n functionResponses.push(...responses);\n\n // Send function responses back to the model\n // Build the function response parts for the next message\n const functionResponseParts = functionResponses.map(fr => ({\n functionResponse: {\n name: fr.name,\n response: fr.response\n }\n }));\n\n // Send function responses as the next user message\n currentUserMessage = functionResponseParts as any;\n }\n\n // Log total summary for all iterations\n const totalDuration = Date.now() - methodStartTime;\n if (iterations > 1) {\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId: llmUsageLogger.generateRequestId(),\n provider: 'gemini',\n model: modelName,\n method: `streamWithTools[TOTAL:${iterations}iters]`,\n inputTokens: totalInputTokens,\n outputTokens: totalOutputTokens,\n totalTokens: totalInputTokens + totalOutputTokens,\n costUSD: llmUsageLogger.calculateCost(modelName, totalInputTokens, totalOutputTokens),\n durationMs: totalDuration,\n toolCalls: totalToolCalls,\n success: true,\n });\n }\n\n if (iterations >= maxIterations) {\n throw new Error(`Max iterations (${maxIterations}) reached in tool calling loop`);\n }\n\n return finalText;\n }\n\n // ============================================================\n // OPENAI IMPLEMENTATION\n // ============================================================\n\n private static async _openaiText(\n messages: LLMMessages,\n modelName: string,\n options: LLMOptions\n ): Promise<string> {\n const startTime = Date.now();\n const requestId = llmUsageLogger.generateRequestId();\n const apiKey = options.apiKey || process.env.OPENAI_API_KEY || \"\";\n const openai = new OpenAI({ apiKey });\n\n // Convert system prompt to string if it's an array\n const systemPrompt = typeof messages.sys === 'string'\n ? messages.sys\n : messages.sys.map(block => block.text).join('\\n');\n\n try {\n const response = await openai.chat.completions.create({\n model: modelName,\n messages: [\n { role: 'system', content: systemPrompt },\n { role: 'user', content: messages.user }\n ],\n max_tokens: options.maxTokens || 1000,\n temperature: options.temperature,\n top_p: options.topP,\n });\n\n const durationMs = Date.now() - startTime;\n const usage = response.usage;\n const inputTokens = usage?.prompt_tokens || 0;\n const outputTokens = usage?.completion_tokens || 0;\n\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'openai',\n model: modelName,\n method: 'text',\n inputTokens,\n outputTokens,\n totalTokens: inputTokens + outputTokens,\n costUSD: llmUsageLogger.calculateCost(modelName, inputTokens, outputTokens),\n durationMs,\n success: true,\n });\n\n return response.choices[0]?.message?.content || '';\n } catch (error) {\n const durationMs = Date.now() - startTime;\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'openai',\n model: modelName,\n method: 'text',\n inputTokens: 0,\n outputTokens: 0,\n totalTokens: 0,\n costUSD: 0,\n durationMs,\n success: false,\n error: error instanceof Error ? error.message : String(error),\n });\n throw error;\n }\n }\n\n private static async _openaiStream(\n messages: LLMMessages,\n modelName: string,\n options: LLMOptions,\n json?: boolean\n ): Promise<any> {\n const startTime = Date.now();\n const requestId = llmUsageLogger.generateRequestId();\n const apiKey = options.apiKey || process.env.OPENAI_API_KEY || \"\";\n const openai = new OpenAI({ apiKey });\n\n // Convert system prompt to string if it's an array\n const systemPrompt = typeof messages.sys === 'string'\n ? messages.sys\n : messages.sys.map(block => block.text).join('\\n');\n\n try {\n const stream = await openai.chat.completions.create({\n model: modelName,\n messages: [\n { role: 'system', content: systemPrompt },\n { role: 'user', content: messages.user }\n ],\n max_tokens: options.maxTokens || 1000,\n temperature: options.temperature,\n top_p: options.topP,\n response_format: json ? { type: 'json_object' } : undefined,\n stream: true,\n stream_options: { include_usage: true }, // Request usage info in stream\n });\n\n let fullText = '';\n let inputTokens = 0;\n let outputTokens = 0;\n\n for await (const chunk of stream) {\n const content = chunk.choices[0]?.delta?.content || '';\n if (content) {\n fullText += content;\n\n // Call partial callback if provided\n if (options.partial) {\n options.partial(content);\n }\n }\n // OpenAI provides usage in the final chunk when stream_options.include_usage is true\n if (chunk.usage) {\n inputTokens = chunk.usage.prompt_tokens || 0;\n outputTokens = chunk.usage.completion_tokens || 0;\n }\n }\n\n const durationMs = Date.now() - startTime;\n\n // Estimate tokens if not provided\n if (inputTokens === 0) {\n inputTokens = Math.ceil((systemPrompt.length + messages.user.length) / 4);\n }\n if (outputTokens === 0) {\n outputTokens = Math.ceil(fullText.length / 4);\n }\n\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'openai',\n model: modelName,\n method: 'stream',\n inputTokens,\n outputTokens,\n totalTokens: inputTokens + outputTokens,\n costUSD: llmUsageLogger.calculateCost(modelName, inputTokens, outputTokens),\n durationMs,\n success: true,\n });\n\n // Return parsed JSON or text\n if (json) {\n return this._parseJSON(fullText);\n }\n\n return fullText;\n } catch (error) {\n const durationMs = Date.now() - startTime;\n llmUsageLogger.log({\n timestamp: new Date().toISOString(),\n requestId,\n provider: 'openai',\n model: modelName,\n method: 'stream',\n inputTokens: 0,\n outputTokens: 0,\n totalTokens: 0,\n costUSD: 0,\n durationMs,\n success: false,\n error: error instanceof Error ? error.message : String(error),\n });\n throw error;\n }\n }\n\n private static async _openaiStreamWithTools(\n messages: LLMMessages,\n tools: Tool[],\n toolHandler: (toolName: string, toolInput: any) => Promise<any>,\n modelName: string,\n options: LLMOptions,\n maxIterations: number\n ): Promise<string> {\n const apiKey = options.apiKey || process.env.OPENAI_API_KEY || \"\";\n const openai = new OpenAI({ apiKey });\n\n // Convert system prompt to string if it's an array\n const systemPrompt = typeof messages.sys === 'string'\n ? messages.sys\n : messages.sys.map(block => block.text).join('\\n');\n\n // Convert tools to OpenAI function format\n const openaiTools: OpenAI.Chat.Completions.ChatCompletionTool[] = tools.map(tool => ({\n type: 'function' as const,\n function: {\n name: tool.name,\n description: tool.description,\n parameters: {\n type: tool.input_schema.type,\n properties: tool.input_schema.properties,\n required: tool.input_schema.required || []\n }\n }\n }));\n\n // Build conversation messages\n const conversationMessages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [\n { role: 'system', content: systemPrompt },\n { role: 'user', content: messages.user }\n ];\n\n let iterations = 0;\n let finalText = '';\n\n while (iterations < maxIterations) {\n iterations++;\n\n const stream = await openai.chat.completions.create({\n model: modelName,\n messages: conversationMessages,\n max_tokens: options.maxTokens || 4000,\n temperature: options.temperature,\n top_p: options.topP,\n tools: openaiTools,\n stream: true,\n });\n\n let responseText = '';\n const toolCalls: Array<{\n id: string;\n name: string;\n arguments: string;\n }> = [];\n\n // Track tool call assembly (OpenAI streams tool calls in chunks)\n const toolCallsInProgress: Map<number, { id: string; name: string; arguments: string }> = new Map();\n\n for await (const chunk of stream) {\n const delta = chunk.choices[0]?.delta;\n\n // Handle text content\n if (delta?.content) {\n responseText += delta.content;\n if (options.partial) {\n options.partial(delta.content);\n }\n }\n\n // Handle tool calls\n if (delta?.tool_calls) {\n for (const toolCallDelta of delta.tool_calls) {\n const index = toolCallDelta.index;\n\n if (!toolCallsInProgress.has(index)) {\n toolCallsInProgress.set(index, {\n id: toolCallDelta.id || '',\n name: toolCallDelta.function?.name || '',\n arguments: ''\n });\n }\n\n const tc = toolCallsInProgress.get(index)!;\n\n if (toolCallDelta.id) {\n tc.id = toolCallDelta.id;\n }\n if (toolCallDelta.function?.name) {\n tc.name = toolCallDelta.function.name;\n }\n if (toolCallDelta.function?.arguments) {\n tc.arguments += toolCallDelta.function.arguments;\n }\n }\n }\n }\n\n // Collect completed tool calls\n for (const tc of toolCallsInProgress.values()) {\n if (tc.id && tc.name) {\n toolCalls.push(tc);\n }\n }\n\n // If no tool calls, we're done\n if (toolCalls.length === 0) {\n finalText = responseText;\n break;\n }\n\n // Add assistant message with tool calls to conversation\n conversationMessages.push({\n role: 'assistant',\n content: responseText || null,\n tool_calls: toolCalls.map(tc => ({\n id: tc.id,\n type: 'function' as const,\n function: {\n name: tc.name,\n arguments: tc.arguments\n }\n }))\n });\n\n // Execute all tool calls in parallel for lower latency\n const toolCallResults = await Promise.all(toolCalls.map(async (tc) => {\n let result: string;\n try {\n const args = JSON.parse(tc.arguments);\n const toolResult = await toolHandler(tc.name, args);\n result = typeof toolResult === 'string' ? toolResult : JSON.stringify(toolResult);\n // Truncate very large results to reduce memory and token usage\n const MAX_RESULT_LENGTH = 50000; // ~12k tokens max per tool result\n if (result.length > MAX_RESULT_LENGTH) {\n // Surrogate-aware cut so the truncation can't split a UTF-16 pair.\n result = safeTruncate(result, MAX_RESULT_LENGTH) +\n '\\n\\n... [Result truncated - showing first 50000 characters of ' + result.length + ' total]';\n }\n } catch (error) {\n result = JSON.stringify({ error: error instanceof Error ? error.message : String(error) });\n }\n // Final safety net: strip lone surrogates from source-data results.\n return { role: 'tool' as const, tool_call_id: tc.id, content: stripLoneSurrogates(result) };\n }));\n toolCallResults.forEach(r => conversationMessages.push(r as any));\n }\n\n if (iterations >= maxIterations) {\n throw new Error(`Max iterations (${maxIterations}) reached in tool calling loop`);\n }\n\n return finalText;\n }\n\n // ============================================================\n // JSON PARSING HELPER\n // ============================================================\n\n /**\n * Parse JSON string, handling markdown code blocks and surrounding text\n * Enhanced version with jsonrepair to handle malformed JSON from LLMs\n * @param text - Text that may contain JSON wrapped in ```json...``` or with surrounding text\n * @returns Parsed JSON object or array\n */\n private static _parseJSON(text: string): any {\n let jsonText = text.trim();\n\n // Step 1: Remove markdown code blocks (jsonrepair doesn't handle markdown)\n if (jsonText.startsWith('```json')) {\n jsonText = jsonText.replace(/^```json\\s*\\n?/, '').replace(/\\n?```\\s*$/, '');\n } else if (jsonText.startsWith('```')) {\n jsonText = jsonText.replace(/^```\\s*\\n?/, '').replace(/\\n?```\\s*$/, '');\n }\n\n // Step 2: Extract JSON from surrounding text using proper brace matching\n // Find the first JSON object or array and extract only that\n const firstBrace = jsonText.indexOf('{');\n const firstBracket = jsonText.indexOf('[');\n\n // Determine if it's an object or array\n let startIdx = -1;\n let openChar = '';\n let closeChar = '';\n\n if (firstBrace !== -1 && (firstBracket === -1 || firstBrace < firstBracket)) {\n // Object starts first\n startIdx = firstBrace;\n openChar = '{';\n closeChar = '}';\n } else if (firstBracket !== -1) {\n // Array starts first\n startIdx = firstBracket;\n openChar = '[';\n closeChar = ']';\n }\n\n // If no JSON structure found at all, throw a clear error\n if (startIdx === -1) {\n const error = new Error(`No JSON found in response. LLM returned plain text instead of JSON.`);\n userPromptErrorLogger.logJsonParseError('LLM._parseJSON - No JSON structure found', text, error);\n throw error;\n }\n\n // Find the MATCHING closing brace/bracket (not the last one in the document)\n let depth = 0;\n let inString = false;\n let endIdx = -1;\n\n for (let i = startIdx; i < jsonText.length; i++) {\n const char = jsonText[i];\n const prevChar = i > 0 ? jsonText[i - 1] : '';\n\n // Handle string literals (skip content inside quotes)\n if (char === '\"' && prevChar !== '\\\\') {\n inString = !inString;\n continue;\n }\n\n if (!inString) {\n if (char === openChar) {\n depth++;\n } else if (char === closeChar) {\n depth--;\n if (depth === 0) {\n endIdx = i;\n break;\n }\n }\n }\n }\n\n if (endIdx !== -1) {\n jsonText = jsonText.substring(startIdx, endIdx + 1);\n } else {\n // No matching closing brace found\n const error = new Error(`Incomplete JSON - no matching closing ${closeChar} found.`);\n userPromptErrorLogger.logJsonParseError('LLM._parseJSON - Incomplete JSON', text, error);\n throw error;\n }\n\n // Step 3: Use jsonrepair to fix malformed JSON before parsing\n // This handles: missing commas, trailing commas, incomplete JSON, improper escaping, etc.\n try {\n const repairedJson = jsonrepair(jsonText);\n return JSON.parse(repairedJson);\n } catch (error) {\n // If jsonrepair fails, log the full raw string and throw\n const parseError = error instanceof Error ? error : new Error(String(error));\n userPromptErrorLogger.logJsonParseError('LLM._parseJSON - JSON parse/repair failed', text, parseError);\n throw new Error(`Failed to parse JSON: ${parseError.message}`);\n }\n }\n}","import fs from 'fs';\nimport path from 'path';\n\n/**\n * LLM Usage Logger - Tracks token usage, costs, and timing for all LLM API calls\n */\n\nexport interface LLMUsageEntry {\n timestamp: string;\n requestId: string;\n provider: string;\n model: string;\n method: string; // 'text' | 'stream' | 'streamWithTools'\n inputTokens: number;\n outputTokens: number;\n cacheReadTokens?: number;\n cacheWriteTokens?: number;\n totalTokens: number;\n costUSD: number;\n durationMs: number;\n toolCalls?: number;\n success: boolean;\n error?: string;\n}\n\n// Pricing per 1M tokens\nconst PRICING: Record<string, { input: number; output: number; cacheRead?: number; cacheWrite?: number }> = {\n // Anthropic (December 2025)\n 'claude-opus-4-5': { input: 5.00, output: 25.00, cacheRead: 0.50, cacheWrite: 6.25 },\n 'claude-opus-4-5-20251101': { input: 5.00, output: 25.00, cacheRead: 0.50, cacheWrite: 6.25 },\n 'claude-sonnet-4-5': { input: 3.00, output: 15.00, cacheRead: 0.30, cacheWrite: 3.75 },\n 'claude-sonnet-4-5-20250929': { input: 3.00, output: 15.00, cacheRead: 0.30, cacheWrite: 3.75 },\n 'claude-haiku-4-5': { input: 1.00, output: 5.00, cacheRead: 0.10, cacheWrite: 1.25 },\n 'claude-haiku-4-5-20251001': { input: 1.00, output: 5.00, cacheRead: 0.10, cacheWrite: 1.25 },\n 'claude-3-5-sonnet-20241022': { input: 3.00, output: 15.00, cacheRead: 0.30, cacheWrite: 3.75 },\n 'claude-3-5-haiku-20241022': { input: 1.00, output: 5.00, cacheRead: 0.10, cacheWrite: 1.25 },\n 'claude-3-opus-20240229': { input: 15.00, output: 75.00, cacheRead: 1.50, cacheWrite: 18.75 },\n 'claude-3-sonnet-20240229': { input: 3.00, output: 15.00, cacheRead: 0.30, cacheWrite: 3.75 },\n 'claude-3-haiku-20240307': { input: 0.25, output: 1.25, cacheRead: 0.03, cacheWrite: 0.30 },\n\n // OpenAI (December 2025)\n 'gpt-5': { input: 1.25, output: 10.00 },\n 'gpt-5-mini': { input: 0.25, output: 2.00 },\n 'gpt-4o': { input: 5.00, output: 15.00 }, // Updated pricing as of late 2025\n 'gpt-4o-mini': { input: 0.15, output: 0.60 },\n 'gpt-4-turbo': { input: 10.00, output: 30.00 },\n 'gpt-4': { input: 30.00, output: 60.00 },\n 'gpt-3.5-turbo': { input: 0.50, output: 1.50 },\n\n // Google Gemini (January 2026)\n 'gemini-3-pro-preview': { input: 2.00, output: 12.00 }, // New Gemini 3\n 'gemini-3-flash-preview': { input: 0.50, output: 3.00 }, // For prompts ≤200K tokens, 2x for >200K\n 'gemini-2.5-flash': { input: 0.30, output: 2.50 }, // Paid tier: $0.30 input (text/image/video), $2.50 output (includes thinking)\n 'gemini-2.5-flash-lite': { input: 0.10, output: 0.40 },\n 'gemini-2.0-flash': { input: 0.10, output: 0.40 },\n 'gemini-2.0-flash-lite': { input: 0.075, output: 0.30 },\n 'gemini-1.5-pro': { input: 1.25, output: 5.00 },\n 'gemini-1.5-flash': { input: 0.075, output: 0.30 },\n\n // Groq (December 2025)\n 'llama-3.3-70b-versatile': { input: 0.59, output: 0.79 },\n 'llama-3.1-70b-versatile': { input: 0.59, output: 0.79 },\n 'llama-3.1-8b-instant': { input: 0.05, output: 0.08 },\n 'llama-4-scout-17b-16e': { input: 0.11, output: 0.34 },\n 'llama-4-maverick-17b-128e': { input: 0.20, output: 0.60 },\n 'mixtral-8x7b-32768': { input: 0.27, output: 0.27 },\n 'qwen3-32b': { input: 0.29, output: 0.59 },\n};\n\n// Default pricing for unknown models\nconst DEFAULT_PRICING = { input: 3.00, output: 15.00 };\n\nclass LLMUsageLogger {\n private logStream: fs.WriteStream | null = null;\n private logPath: string;\n private enabled: boolean;\n private sessionStats = {\n totalCalls: 0,\n totalInputTokens: 0,\n totalOutputTokens: 0,\n totalCacheReadTokens: 0,\n totalCacheWriteTokens: 0,\n totalCostUSD: 0,\n totalDurationMs: 0,\n };\n\n constructor() {\n // Get log path from environment or use default\n this.logPath = process.env.LLM_USAGE_LOG_PATH || path.join(process.cwd(), 'llm-usage-logs');\n this.enabled = process.env.LLM_USAGE_LOGGING !== 'false';\n\n if (this.enabled) {\n this.initLogStream();\n }\n }\n\n private initLogStream(): void {\n try {\n // Ensure directory exists\n const dir = path.dirname(this.logPath);\n if (!fs.existsSync(dir)) {\n fs.mkdirSync(dir, { recursive: true });\n }\n\n this.logStream = fs.createWriteStream(this.logPath, { flags: 'a' });\n\n // Write header if file is new/empty\n if (!fs.existsSync(this.logPath) || fs.statSync(this.logPath).size === 0) {\n this.writeHeader();\n }\n } catch (error) {\n console.error('[LLM-Usage-Logger] Failed to initialize log stream:', error);\n this.enabled = false;\n }\n }\n\n private writeHeader(): void {\n const header = `\n================================================================================\nLLM USAGE LOG - Session Started: ${new Date().toISOString()}\n================================================================================\nFormat: [TIMESTAMP] [REQUEST_ID] [PROVIDER/MODEL] [METHOD]\n Tokens: IN=input OUT=output CACHE_R=cache_read CACHE_W=cache_write TOTAL=total\n Cost: $X.XXXXXX | Time: Xms\n================================================================================\n\n`;\n this.logStream?.write(header);\n }\n\n /**\n * Calculate cost based on token usage and model\n */\n calculateCost(\n model: string,\n inputTokens: number,\n outputTokens: number,\n cacheReadTokens = 0,\n cacheWriteTokens = 0\n ): number {\n // Find pricing for model (try exact match, then partial match)\n let pricing = PRICING[model];\n if (!pricing) {\n // Try to find a partial match\n const modelLower = model.toLowerCase();\n for (const [key, value] of Object.entries(PRICING)) {\n if (modelLower.includes(key.toLowerCase()) || key.toLowerCase().includes(modelLower)) {\n pricing = value;\n break;\n }\n }\n }\n pricing = pricing || DEFAULT_PRICING;\n\n const inputCost = (inputTokens / 1_000_000) * pricing.input;\n const outputCost = (outputTokens / 1_000_000) * pricing.output;\n const cacheReadCost = (cacheReadTokens / 1_000_000) * (pricing.cacheRead || pricing.input * 0.1);\n const cacheWriteCost = (cacheWriteTokens / 1_000_000) * (pricing.cacheWrite || pricing.input * 1.25);\n\n return inputCost + outputCost + cacheReadCost + cacheWriteCost;\n }\n\n /**\n * Log an LLM API call\n */\n log(entry: LLMUsageEntry): void {\n if (!this.enabled) return;\n\n // Update session stats\n this.sessionStats.totalCalls++;\n this.sessionStats.totalInputTokens += entry.inputTokens;\n this.sessionStats.totalOutputTokens += entry.outputTokens;\n this.sessionStats.totalCacheReadTokens += entry.cacheReadTokens || 0;\n this.sessionStats.totalCacheWriteTokens += entry.cacheWriteTokens || 0;\n this.sessionStats.totalCostUSD += entry.costUSD;\n this.sessionStats.totalDurationMs += entry.durationMs;\n\n // Format log entry\n const cacheInfo = entry.cacheReadTokens || entry.cacheWriteTokens\n ? ` CACHE_R=${entry.cacheReadTokens || 0} CACHE_W=${entry.cacheWriteTokens || 0}`\n : '';\n\n const toolInfo = entry.toolCalls ? ` | Tools: ${entry.toolCalls}` : '';\n const errorInfo = entry.error ? ` | ERROR: ${entry.error}` : '';\n const status = entry.success ? '✓' : '✗';\n\n // Calculate cache savings\n let cacheStatus = '';\n if (entry.cacheReadTokens && entry.cacheReadTokens > 0) {\n const savedCost = (entry.cacheReadTokens / 1_000_000) * 2.70; // Difference between regular and cache price\n cacheStatus = ` ⚡ CACHE HIT! Saved ~$${savedCost.toFixed(4)}`;\n } else if (entry.cacheWriteTokens && entry.cacheWriteTokens > 0) {\n cacheStatus = ' 📝 Cache created (next request will be cheaper)';\n }\n\n const logLine = `[${entry.timestamp}] [${entry.requestId}] ${status} ${entry.provider}/${entry.model} [${entry.method}]\n Tokens: IN=${entry.inputTokens} OUT=${entry.outputTokens}${cacheInfo} TOTAL=${entry.totalTokens}\n Cost: $${entry.costUSD.toFixed(6)} | Time: ${entry.durationMs}ms${toolInfo}${errorInfo}${cacheStatus}\n`;\n\n this.logStream?.write(logLine);\n }\n\n /**\n * Log session summary (call at end of request)\n */\n logSessionSummary(requestContext?: string): void {\n if (!this.enabled || this.sessionStats.totalCalls === 0) return;\n\n // Calculate cache savings\n const cacheReadSavings = (this.sessionStats.totalCacheReadTokens / 1_000_000) * 2.70; // Saved per 1M tokens\n const hasCaching = this.sessionStats.totalCacheReadTokens > 0 || this.sessionStats.totalCacheWriteTokens > 0;\n\n let cacheSection = '';\n if (hasCaching) {\n cacheSection = `\nCache Statistics:\n Cache Read Tokens: ${this.sessionStats.totalCacheReadTokens.toLocaleString()}${this.sessionStats.totalCacheReadTokens > 0 ? ' ⚡' : ''}\n Cache Write Tokens: ${this.sessionStats.totalCacheWriteTokens.toLocaleString()}${this.sessionStats.totalCacheWriteTokens > 0 ? ' 📝' : ''}\n Estimated Savings: $${cacheReadSavings.toFixed(4)}`;\n }\n\n const summary = `\n--------------------------------------------------------------------------------\nSESSION SUMMARY${requestContext ? ` (${requestContext})` : ''}\n--------------------------------------------------------------------------------\nTotal LLM Calls: ${this.sessionStats.totalCalls}\nTotal Input Tokens: ${this.sessionStats.totalInputTokens.toLocaleString()}\nTotal Output Tokens: ${this.sessionStats.totalOutputTokens.toLocaleString()}\nTotal Tokens: ${(this.sessionStats.totalInputTokens + this.sessionStats.totalOutputTokens).toLocaleString()}\nTotal Cost: $${this.sessionStats.totalCostUSD.toFixed(6)}\nTotal Time: ${this.sessionStats.totalDurationMs}ms (${(this.sessionStats.totalDurationMs / 1000).toFixed(2)}s)\nAvg Cost/Call: $${(this.sessionStats.totalCostUSD / this.sessionStats.totalCalls).toFixed(6)}\nAvg Time/Call: ${Math.round(this.sessionStats.totalDurationMs / this.sessionStats.totalCalls)}ms${cacheSection}\n--------------------------------------------------------------------------------\n\n`;\n this.logStream?.write(summary);\n }\n\n /**\n * Reset session stats (call at start of new user request)\n */\n resetSession(): void {\n this.sessionStats = {\n totalCalls: 0,\n totalInputTokens: 0,\n totalOutputTokens: 0,\n totalCacheReadTokens: 0,\n totalCacheWriteTokens: 0,\n totalCostUSD: 0,\n totalDurationMs: 0,\n };\n }\n\n /**\n * Reset the log file for a new request (clears previous logs)\n * Call this at the start of each USER_PROMPT_REQ\n */\n resetLogFile(requestContext?: string): void {\n if (!this.enabled) return;\n\n try {\n // Close existing stream\n if (this.logStream) {\n this.logStream.end();\n this.logStream = null;\n }\n\n // Truncate the file by opening in write mode (not append)\n this.logStream = fs.createWriteStream(this.logPath, { flags: 'w' });\n\n // Write new header\n const header = `\n================================================================================\nLLM USAGE LOG - Request Started: ${new Date().toISOString()}\n${requestContext ? `Context: ${requestContext}` : ''}\n================================================================================\nFormat: [TIMESTAMP] [REQUEST_ID] [PROVIDER/MODEL] [METHOD]\n Tokens: IN=input OUT=output CACHE_R=cache_read CACHE_W=cache_write TOTAL=total\n Cost: $X.XXXXXX | Time: Xms\n================================================================================\n\n`;\n this.logStream.write(header);\n\n // Reset session stats\n this.resetSession();\n } catch (error) {\n console.error('[LLM-Usage-Logger] Failed to reset log file:', error);\n }\n }\n\n /**\n * Get current session stats\n */\n getSessionStats() {\n return { ...this.sessionStats };\n }\n\n /**\n * Generate a unique request ID\n */\n generateRequestId(): string {\n return `req-${Date.now()}-${Math.random().toString(36).substring(2, 8)}`;\n }\n}\n\n// Export singleton instance\nexport const llmUsageLogger = new LLMUsageLogger();\n","/**\n * Datetime utility functions for the SDK\n */\n\n/**\n * Get the current datetime formatted for LLM prompts\n * This format helps the LLM understand time-related queries\n *\n * @returns Formatted datetime string (e.g., \"Sunday, January 5, 2025, 10:30 AM EST\")\n */\nexport function getCurrentDateTimeForPrompt(): string {\n return new Date().toLocaleString('en-US', {\n weekday: 'long',\n year: 'numeric',\n month: 'long',\n day: 'numeric',\n hour: '2-digit',\n minute: '2-digit',\n timeZoneName: 'short'\n });\n}\n\n/**\n * Get current date in ISO format (YYYY-MM-DD)\n * Useful for SQL date comparisons\n *\n * @returns ISO date string (e.g., \"2025-01-05\")\n */\nexport function getCurrentDateISO(): string {\n return new Date().toISOString().split('T')[0];\n}\n\n/**\n * Get current datetime in ISO format\n *\n * @returns ISO datetime string (e.g., \"2025-01-05T10:30:00.000Z\")\n */\nexport function getCurrentDateTimeISO(): string {\n return new Date().toISOString();\n}\n","/**\n * Utility for extracting text content from LLM prompt structures\n * Handles various formats: strings, arrays, objects with text/content properties\n */\n\n/**\n * Extracts text content from various prompt formats\n * Handles:\n * - Plain strings\n * - Arrays of strings or content blocks\n * - Objects with text or content properties\n * - Nested structures\n *\n * @param content - The prompt content in any supported format\n * @returns Extracted text as a single string\n *\n * @example\n * // Plain string\n * extractPromptText(\"Hello\") // \"Hello\"\n *\n * @example\n * // Array of strings\n * extractPromptText([\"Hello\", \"World\"]) // \"Hello\\n\\n---\\n\\nWorld\"\n *\n * @example\n * // Array of content blocks\n * extractPromptText([{ text: \"Hello\" }, { text: \"World\" }]) // \"Hello\\n\\n---\\n\\nWorld\"\n *\n * @example\n * // Object with text property\n * extractPromptText({ text: \"Hello\" }) // \"Hello\"\n */\nexport function extractPromptText(content: unknown): string {\n\t// Handle null/undefined\n\tif (content === null || content === undefined) {\n\t\treturn '';\n\t}\n\n\t// Handle plain strings\n\tif (typeof content === 'string') {\n\t\treturn content;\n\t}\n\n\t// Handle arrays (of strings or content blocks)\n\tif (Array.isArray(content)) {\n\t\treturn content\n\t\t\t.map((item: unknown) => extractContentBlockText(item))\n\t\t\t.filter((text) => text.length > 0)\n\t\t\t.join('\\n\\n---\\n\\n');\n\t}\n\n\t// Handle objects with text or content properties\n\tif (content && typeof content === 'object') {\n\t\treturn extractObjectText(content as Record<string, unknown>);\n\t}\n\n\t// Fallback: convert to string\n\treturn String(content);\n}\n\n/**\n * Extracts text from a single content block\n * Content blocks can be strings, objects with text property, or objects with content property\n */\nfunction extractContentBlockText(item: unknown): string {\n\tif (typeof item === 'string') {\n\t\treturn item;\n\t}\n\n\tif (item && typeof item === 'object') {\n\t\tconst obj = item as Record<string, unknown>;\n\n\t\t// Check for 'text' property (common in Anthropic message format)\n\t\tif (typeof obj.text === 'string') {\n\t\t\treturn obj.text;\n\t\t}\n\n\t\t// Check for 'content' property (alternative format)\n\t\tif (typeof obj.content === 'string') {\n\t\t\treturn obj.content;\n\t\t}\n\n\t\t// Fallback to JSON stringification for complex objects\n\t\treturn JSON.stringify(item, null, 2);\n\t}\n\n\treturn String(item);\n}\n\n/**\n * Extracts text from an object\n * Checks for common text properties before falling back to JSON\n */\nfunction extractObjectText(obj: Record<string, unknown>): string {\n\t// Check for 'text' property\n\tif (typeof obj.text === 'string') {\n\t\treturn obj.text;\n\t}\n\n\t// Check for 'content' property\n\tif (typeof obj.content === 'string') {\n\t\treturn obj.content;\n\t}\n\n\t// Fallback to JSON stringification\n\treturn JSON.stringify(obj, null, 2);\n}\n\n/**\n * Truncates text to a maximum length with ellipsis\n * Useful for logging previews\n *\n * @param text - The text to truncate\n * @param maxLength - Maximum length (default: 100)\n * @returns Truncated text with ellipsis if needed\n */\nexport function truncateForPreview(text: string, maxLength: number = 100): string {\n\tif (text.length <= maxLength) {\n\t\treturn text;\n\t}\n\treturn text.substring(0, maxLength) + '...';\n}\n","/**\n * Shared result-summarization utilities for the agent flow.\n *\n * Lives in its own module (not main-agent.ts) so BOTH the main agent and the\n * source agent can summarize a full result set without a circular import.\n */\n\nimport { MAX_ROWS_TO_LLM } from '../constants';\n\n/**\n * Largest result the main agent is shown VERBATIM (every row). At or below this\n * the rows are sent complete — so small dimension/lookup results (benchmark\n * mappings, enum lists) arrive whole and the agent never generalizes from a\n * biased head-slice. Above it, the agent gets a bounded `summarizeRows` summary\n * (complete STRUCTURE, not rows) + a few samples instead.\n * Alias of the canonical STAGE-2 limit (see constants.ts).\n */\nexport const MAIN_AGENT_COMPLETE_ROWS = MAX_ROWS_TO_LLM;\n\n/**\n * Profile a full result set into a compact, LLM-readable summary.\n * Computed over ALL rows so the agent can state ranges, counts, and\n * distributions truthfully even though it is only shown a few sample rows.\n * Source-agnostic — infers number / date / category / text per column.\n *\n * Output size is BOUNDED regardless of input size (categories and groups are\n * capped), so a 1M-row result and a 1K-row result yield a same-sized summary.\n */\nexport function summarizeRows(rows: Record<string, any>[]): Record<string, any> {\n\tif (!rows.length) return { totalRows: 0, columns: {} };\n\tconst MAX_CATEGORIES = 20;\n\tconst keys = Object.keys(rows[0]);\n\tconst columns: Record<string, any> = {};\n\t// Local-time date formatter — avoids the UTC shift that toISOString() applies\n\t// to a locally-parsed \"YYYY-MM-DD HH:mm:ss\" string (which would roll the date\n\t// back a day in positive-offset timezones).\n\tconst fmtDate = (ms: number) => {\n\t\tconst d = new Date(ms);\n\t\tconst p = (n: number) => String(n).padStart(2, '0');\n\t\treturn `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())}`;\n\t};\n\n\tfor (const key of keys) {\n\t\tlet nonNull = 0, zeros = 0, isNumeric = true, isDateLike = true, sum = 0;\n\t\tlet min: number | undefined, max: number | undefined;\n\t\tlet dMin: number | undefined, dMax: number | undefined;\n\t\tconst counts = new Map<string, number>();\n\t\tlet overflow = false;\n\n\t\tfor (const row of rows) {\n\t\t\tconst v = row[key];\n\t\t\tif (v === null || v === undefined || v === '') continue;\n\t\t\tnonNull++;\n\n\t\t\tconst num = typeof v === 'number' ? v\n\t\t\t\t: (typeof v === 'string' && v.trim() !== '' && !isNaN(Number(v)) ? Number(v) : NaN);\n\t\t\tif (!Number.isNaN(num) && typeof v !== 'boolean') {\n\t\t\t\tif (num === 0) zeros++;\n\t\t\t\tmin = min === undefined ? num : Math.min(min, num);\n\t\t\t\tmax = max === undefined ? num : Math.max(max, num);\n\t\t\t\tsum += num;\n\t\t\t} else { isNumeric = false; }\n\n\t\t\tconst t = v instanceof Date ? v.getTime()\n\t\t\t\t: (typeof v === 'string' ? Date.parse(v) : NaN);\n\t\t\tif (!Number.isNaN(t) && typeof v !== 'number') {\n\t\t\t\tdMin = dMin === undefined ? t : Math.min(dMin, t);\n\t\t\t\tdMax = dMax === undefined ? t : Math.max(dMax, t);\n\t\t\t} else { isDateLike = false; }\n\n\t\t\tif (!overflow) {\n\t\t\t\tconst sv = String(v);\n\t\t\t\tcounts.set(sv, (counts.get(sv) || 0) + 1);\n\t\t\t\tif (counts.size > MAX_CATEGORIES * 5) overflow = true;\n\t\t\t}\n\t\t}\n\n\t\tconst nulls = rows.length - nonNull;\n\t\tif (nonNull === 0) { columns[key] = { type: 'empty', nulls }; continue; }\n\n\t\tif (isNumeric) {\n\t\t\tcolumns[key] = { type: 'number', nonNull, nulls, zeros, min, max,\n\t\t\t\tavg: Math.round((sum / nonNull) * 100) / 100 };\n\t\t} else if (isDateLike) {\n\t\t\tcolumns[key] = { type: 'date', nonNull,\n\t\t\t\tmin: fmtDate(dMin!),\n\t\t\t\tmax: fmtDate(dMax!),\n\t\t\t\tdistinct: overflow ? `${MAX_CATEGORIES * 5}+` : counts.size };\n\t\t} else if (!overflow && counts.size <= MAX_CATEGORIES) {\n\t\t\tconst dist: Record<string, number> = {};\n\t\t\tfor (const [k, c] of [...counts.entries()].sort((a, b) => b[1] - a[1])) dist[k] = c;\n\t\t\tcolumns[key] = { type: 'category', distinct: counts.size, counts: dist };\n\t\t} else {\n\t\t\tcolumns[key] = { type: 'text',\n\t\t\t\tdistinct: overflow ? `${MAX_CATEGORIES * 5}+` : counts.size,\n\t\t\t\texamples: [...counts.keys()].slice(0, 3) };\n\t\t}\n\t}\n\n\t// Group-aware measure stats: per-segment averages so the agent can state\n\t// per-group numbers truthfully (the global column stats above are NOT enough\n\t// for \"JSW vs benchmark per district\" — it would otherwise guess from the\n\t// head sample). Group by the low-cardinality category columns; report the\n\t// mean of each numeric measure per group.\n\tconst dims = keys\n\t\t.filter(k => columns[k]?.type === 'category' && (columns[k].distinct as number) <= 6)\n\t\t.sort((a, b) => (columns[a].distinct as number) - (columns[b].distinct as number));\n\tconst measures = keys.filter(k => columns[k]?.type === 'number');\n\tlet groups: Record<string, any> | undefined;\n\tif (dims.length && measures.length) {\n\t\tconst MAX_GROUPS = 30;\n\t\tlet useDims = dims.slice(0, 2);\n\t\tconst keyOf = (row: Record<string, any>) => useDims.map(d => `${d}=${row[d]}`).join(' | ');\n\t\tif (new Set(rows.map(keyOf)).size > MAX_GROUPS) useDims = dims.slice(0, 1); // fall back to 1 dim\n\t\tconst agg = new Map<string, { count: number; sums: Record<string, number>; ns: Record<string, number> }>();\n\t\tfor (const row of rows) {\n\t\t\tconst key = useDims.map(d => `${d}=${row[d]}`).join(' | ');\n\t\t\tlet g = agg.get(key);\n\t\t\tif (!g) { g = { count: 0, sums: {}, ns: {} }; agg.set(key, g); }\n\t\t\tg.count++;\n\t\t\tfor (const m of measures) {\n\t\t\t\tconst v = row[m];\n\t\t\t\tif (typeof v === 'number' && !Number.isNaN(v)) {\n\t\t\t\t\tg.sums[m] = (g.sums[m] || 0) + v;\n\t\t\t\t\tg.ns[m] = (g.ns[m] || 0) + 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (agg.size <= MAX_GROUPS) {\n\t\t\t// Count-like measures (counts/totals) need SUM per group; rate/price\n\t\t\t// measures need AVG. Averaging a count per row understates the true\n\t\t\t// total (e.g. evidential_count avg 0.7/day reads as \"~0\" when the\n\t\t\t// district actually has dozens of evidential observations).\n\t\t\tconst isCountLike = (name: string) => /count|\\bobs\\b|observation|qty|quantity|\\bnum|total|evidential|volume|units/i.test(name);\n\t\t\tgroups = {\n\t\t\t\tby: useDims,\n\t\t\t\trows: [...agg.entries()].map(([key, g]) => {\n\t\t\t\t\tconst avg: Record<string, number> = {};\n\t\t\t\t\tconst sum: Record<string, number> = {};\n\t\t\t\t\tfor (const m of measures) {\n\t\t\t\t\t\tif (!g.ns[m]) continue;\n\t\t\t\t\t\tavg[m] = Math.round((g.sums[m] / g.ns[m]) * 100) / 100;\n\t\t\t\t\t\tif (isCountLike(m)) sum[m] = Math.round(g.sums[m] * 100) / 100;\n\t\t\t\t\t}\n\t\t\t\t\treturn Object.keys(sum).length ? { key, count: g.count, avg, sum } : { key, count: g.count, avg };\n\t\t\t\t}),\n\t\t\t};\n\t\t}\n\t}\n\n\treturn groups ? { totalRows: rows.length, columns, groups } : { totalRows: rows.length, columns };\n}\n","/**\n * Agent Utilities\n *\n * Helper functions for the multi-agent system:\n * - Extract source summaries (with entity names + column names) from external tools\n * - Format summaries and results for prompt variables\n *\n * Prompts themselves live in backend/.prompts/agent-main/ and agent-source-query/\n * and are loaded via the existing promptLoader.\n */\n\nimport type { SourceSummary, SourceAgentResult, EntityDetail } from './types';\nimport type { ExternalTool } from '../services/tool-executor-service';\n\n// ============================================\n// Source Summary Extraction\n// ============================================\n\n/**\n * Build source summaries from external tool definitions.\n * Parses tool descriptions to extract entity names AND column names\n * so the main agent can route accurately to the right source.\n */\nexport function buildSourceSummaries(externalTools: ExternalTool[]): SourceSummary[] {\n\treturn externalTools.map(tool => {\n\t\tconst description = tool.description || '';\n\t\tconst type = extractSourceType(tool.id);\n\t\tconst entityDetails = extractEntityDetails(description);\n\t\tconst dataContext = extractDataContext(description, tool.name, type);\n\n\t\treturn {\n\t\t\tid: extractSourceId(tool.id),\n\t\t\tname: tool.name,\n\t\t\ttype,\n\t\t\tdescription: dataContext,\n\t\t\tentityDetails,\n\t\t\ttoolId: tool.id,\n\t\t};\n\t});\n}\n\n// ============================================\n// Formatting for Prompt Variables\n// ============================================\n\n/**\n * Format source summaries into a string for {{SOURCE_SUMMARIES}} variable.\n * Includes entity names with their column names for accurate routing.\n */\nexport function formatSummariesForPrompt(summaries: SourceSummary[]): string {\n\treturn summaries.map((s, idx) => {\n\t\tconst totalRows = s.entityDetails.reduce((sum, e) => sum + (e.rowCount || 0), 0);\n\t\tconst rowInfo = totalRows > 0 ? ` (~${totalRows.toLocaleString()} total rows)` : '';\n\n\t\tlet entitiesBlock = '';\n\t\tif (s.entityDetails.length > 0) {\n\t\t\tentitiesBlock = '\\n' + s.entityDetails.map(e => {\n\t\t\t\tconst rows = e.rowCount ? ` (${e.rowCount.toLocaleString()} rows)` : '';\n\t\t\t\tconst cols = e.columns.length > 0 ? `: ${e.columns.join(', ')}` : '';\n\t\t\t\treturn ` - ${e.name}${rows}${cols}`;\n\t\t\t}).join('\\n');\n\t\t}\n\n\t\treturn `${idx + 1}. **${s.name}** (tool: ${s.toolId}, type: ${s.type})${rowInfo}\\n ${s.description}${entitiesBlock}`;\n\t}).join('\\n\\n');\n}\n\n/**\n * Format source agent results into a string for {{SOURCE_RESULTS}} variable.\n */\nexport function formatSourceResultsForPrompt(sourceResults: SourceAgentResult[]): string {\n\treturn sourceResults.map(r => {\n\t\tif (!r.success) {\n\t\t\treturn `### ${r.sourceName} (FAILED)\\nError: ${r.error}`;\n\t\t}\n\n\t\tconst limitWarning = r.metadata.isLimited\n\t\t\t? `\\n⚠️ Data is LIMITED: showing ${r.metadata.rowsReturned} of ${r.metadata.totalRowsMatched} total matching rows. Aggregations may be partial.`\n\t\t\t: '';\n\n\t\tconst dataPreview = JSON.stringify(r.data.slice(0, 10), null, 2);\n\t\treturn `### ${r.sourceName} (${r.metadata.rowsReturned} rows, ${r.metadata.executionTimeMs}ms)${limitWarning}\\nQuery: ${r.metadata.queryExecuted || 'N/A'}\\nData:\\n${dataPreview}`;\n\t}).join('\\n\\n');\n}\n\n// ============================================\n// Entity Detail Extraction (name + columns)\n// ============================================\n\n/**\n * Extract entity details (name, row count, column names) from a tool description.\n * Handles all source types:\n * - Database (postgres/mysql/mssql): \"• schema.table [~N rows] - desc:\\n col(TYPE), ...\"\n * - Excel/CSV: \"• SheetName [~N rows]:\\n Col Name(TYPE), ...\"\n * - REST API: \"Endpoint: /path\\n field(TYPE), ...\"\n * - Table catalog: \"tableName(1.2K rows), tableName2(500 rows), ...\"\n */\nfunction extractEntityDetails(description: string): EntityDetail[] {\n\tconst details: EntityDetail[] = [];\n\tconst seenNames = new Set<string>();\n\n\t// 1. Parse detailed bullet-point entities (with columns)\n\tconst bulletSections = description.split(/(?=•\\s)/);\n\n\tfor (const section of bulletSections) {\n\t\tif (!section.trim().startsWith('•')) continue;\n\n\t\t// Matches: \"• public.table_name [~N rows]\" or \"• SheetName [~N rows]\"\n\t\tconst headerMatch = section.match(/•\\s+(?:\\w+\\.)?(.+?)\\s*\\[~?([\\d,]+)\\s*rows?\\]/);\n\t\tif (!headerMatch) continue;\n\n\t\tconst name = headerMatch[1].trim();\n\t\tconst rowCount = parseInt(headerMatch[2].replace(/,/g, ''), 10);\n\t\tconst columns = extractColumnNames(section);\n\n\t\tdetails.push({\n\t\t\tname,\n\t\t\trowCount: rowCount > 0 ? rowCount : undefined,\n\t\t\tcolumns,\n\t\t});\n\t\tseenNames.add(name.toLowerCase());\n\t}\n\n\t// 2. Parse table catalog entries: \"tableName(1.2K rows)\" or \"schema.tableName(500 rows)\"\n\t// These appear in \"All N tables...\" or \"Other tables...\" sections\n\tconst catalogMatch = description.match(/(?:All \\d+ tables|Other tables|Remaining \\d+ tables)[^:]*:\\n?([\\s\\S]*?)(?:\\n\\n|$)/i);\n\tif (catalogMatch) {\n\t\tconst catalogText = catalogMatch[1];\n\t\t// Match: name(Nrows) where N can be \"1.2K\", \"500\", \"1.5M\", etc.\n\t\tconst entryPattern = /([\\w.]+)\\(([\\d.]+[KMB]?)\\s*rows?\\)/gi;\n\t\tlet match;\n\t\twhile ((match = entryPattern.exec(catalogText)) !== null) {\n\t\t\tconst fullName = match[1];\n\t\t\t// Extract short name (after last dot)\n\t\t\tconst name = fullName.includes('.') ? fullName.split('.').pop()! : fullName;\n\t\t\tif (seenNames.has(name.toLowerCase())) continue; // Skip duplicates from detailed section\n\n\t\t\t// Parse abbreviated row count\n\t\t\tconst rowStr = match[2];\n\t\t\tlet rowCount = 0;\n\t\t\tif (rowStr.endsWith('B')) rowCount = parseFloat(rowStr) * 1_000_000_000;\n\t\t\telse if (rowStr.endsWith('M')) rowCount = parseFloat(rowStr) * 1_000_000;\n\t\t\telse if (rowStr.endsWith('K')) rowCount = parseFloat(rowStr) * 1_000;\n\t\t\telse rowCount = parseInt(rowStr, 10);\n\n\t\t\tdetails.push({\n\t\t\t\tname,\n\t\t\t\trowCount: rowCount > 0 ? Math.round(rowCount) : undefined,\n\t\t\t\tcolumns: [], // Catalog entries have no column details\n\t\t\t});\n\t\t\tseenNames.add(name.toLowerCase());\n\t\t}\n\t}\n\n\t// 3. If no bullet entities found, try REST API endpoint pattern\n\tif (details.length === 0) {\n\t\tconst endpointPattern = /Endpoint:\\s*(\\S+)/g;\n\t\tlet match;\n\t\twhile ((match = endpointPattern.exec(description)) !== null) {\n\t\t\tconst columns = extractColumnNames(description);\n\t\t\tdetails.push({\n\t\t\t\tname: match[1].trim(),\n\t\t\t\tcolumns,\n\t\t\t});\n\t\t}\n\t}\n\n\treturn details;\n}\n\n/**\n * Extract column/field names from a description block.\n * Matches patterns like: column_name(TYPE), \"Column Name\"(TYPE), etc.\n * TYPE is one of: NUMBER, TEXT, BOOLEAN, TIMESTAMP, DATE, INTEGER, FLOAT, etc.\n *\n * For TEXT columns with sample values (low cardinality), includes the values\n * in the output so the main agent can write accurate filter intents.\n * Format: \"POD Status{Verified|Not Verified|POD Issues}\" instead of just \"POD Status\"\n */\nfunction extractColumnNames(block: string): string[] {\n\tconst columns: string[] = [];\n\tconst seen = new Set<string>();\n\n\t// Match: name(TYPE) optionally followed by [...]{sample1|sample2|...}\n\t// Captures: [1]=column name, [2]=type, [3]=optional sample values\n\tconst colPattern = /(?:^|,)\\s*([^,()\\[\\]{}\\n]+?)\\s*\\((?:NUMBER|TEXT|BOOLEAN|TIMESTAMP|INTEGER|FLOAT|DECIMAL|DATE|BIGINT|VARCHAR|CHAR|DOUBLE|REAL|ARRAY|OBJECT)(?:[^)]*)\\)(?:\\[[^\\]]*\\])?\\{([^}]+)\\}|(?:^|,)\\s*([^,()\\[\\]{}\\n]+?)\\s*\\((?:NUMBER|TEXT|BOOLEAN|TIMESTAMP|INTEGER|FLOAT|DECIMAL|DATE|BIGINT|VARCHAR|CHAR|DOUBLE|REAL|ARRAY|OBJECT)(?:[^)]*)\\)/gi;\n\tlet match;\n\twhile ((match = colPattern.exec(block)) !== null) {\n\t\tconst col = (match[1] || match[3] || '').trim();\n\t\tconst samples = match[2] || '';\n\n\t\t// Sanity checks: not too long, not a bullet marker, not already seen\n\t\tif (col && col.length < 60 && !col.includes('•') && !seen.has(col)) {\n\t\t\t// Include sample values for low-cardinality TEXT columns (≤10 samples)\n\t\t\tconst sampleValues = samples.split('|').map(s => s.trim()).filter(Boolean);\n\t\t\tif (sampleValues.length > 0 && sampleValues.length <= 10) {\n\t\t\t\tcolumns.push(`${col}{${sampleValues.join('|')}}`);\n\t\t\t} else {\n\t\t\t\tcolumns.push(col);\n\t\t\t}\n\t\t\tseen.add(col);\n\t\t}\n\t}\n\n\treturn columns;\n}\n\n// ============================================\n// Description / Context Extraction\n// ============================================\n\n/**\n * Extract the most useful description for routing from a tool description.\n * Priority: \"Data Context:\" line > file name > generated fallback\n */\nfunction extractDataContext(description: string, name: string, type: string): string {\n\t// Database sources: use \"Data Context:\" line\n\tconst contextMatch = description.match(/Data Context:\\s*(.+?)(?:\\n|$)/);\n\tif (contextMatch) return contextMatch[1].trim();\n\n\t// Excel: extract file name\n\tconst excelMatch = description.match(/Excel file:\\s*(.+?)\\)/);\n\tif (excelMatch) return `Excel file: ${excelMatch[1].trim()})`;\n\n\t// CSV: extract file name\n\tconst csvMatch = description.match(/CSV file:\\s*(.+?)\\)/);\n\tif (csvMatch) return `CSV file: ${csvMatch[1].trim()})`;\n\n\t// REST API / GraphQL: use first line or \"Use this source for\" line\n\tconst useForMatch = description.match(/Use this source for[^:]*:\\s*(.+?)(?:\\n|$)/);\n\tif (useForMatch) return useForMatch[1].trim();\n\n\t// Fallback\n\tconst typeLabels: Record<string, string> = {\n\t\tpostgres: 'PostgreSQL database',\n\t\tmysql: 'MySQL database',\n\t\tmssql: 'SQL Server database',\n\t\texcel: 'Excel spreadsheet',\n\t\tcsv: 'CSV file',\n\t\trest_api: 'REST API',\n\t\tgraphql: 'GraphQL API',\n\t};\n\treturn `${typeLabels[type] || type}: ${name}`;\n}\n\n// ============================================\n// ID / Type Helpers\n// ============================================\n\n/**\n * Extract source type from tool ID.\n * Tool IDs: \"{type}-{hash}_query\" or \"{type}-{hash}_read\"\n */\nfunction extractSourceType(toolId: string): string {\n\tconst match = toolId.match(/^(\\w+)-[a-f0-9]+_/);\n\tif (match) return match[1];\n\n\tconst types = ['postgres', 'mysql', 'mssql', 'excel', 'csv', 'rest_api', 'graphql'];\n\tfor (const type of types) {\n\t\tif (toolId.toLowerCase().includes(type)) return type;\n\t}\n\treturn 'unknown';\n}\n\n/**\n * Extract source ID from tool ID (strip the action suffix).\n * \"postgres-abc123_query\" → \"postgres-abc123\"\n */\nfunction extractSourceId(toolId: string): string {\n\treturn toolId.replace(/_(query|read|call)$/, '');\n}\n","/**\n * Source Agent\n *\n * An independent per-source agent that handles querying a single data source.\n * It receives an intent from the main agent, has the full schema for its source,\n * uses its OWN LLM call with tool calling to generate and execute queries,\n * retries on failure independently, and returns clean, limited data.\n *\n * For very_large sources (500+ tables), the source agent also gets a search_schema\n * tool to look up table/column details by keyword before writing queries.\n *\n * The main agent never sees: schema, SQL, errors, retries — only the final data.\n */\n\nimport { LLM } from '../../llm';\nimport { promptLoader } from '../prompt-loader';\nimport { getCurrentDateTimeForPrompt } from '../../utils/datetime';\nimport { logger } from '../../utils/logger';\nimport { extractPromptText } from '../prompt-extractor';\nimport { StreamBuffer, streamDelay, withProgressHeartbeat } from '../stream-buffer';\nimport { formatToolResultForLLM, formatQueryResultForLLM } from '../llm-result-truncator';\nimport type { ExternalTool, ExecutedToolInfo } from '../services/tool-executor-service';\nimport type { SourceAgentResult, AgentConfig, SourceToolInput } from './types';\nimport { TOOL_TRACKING_SAMPLE_ROWS, STREAM_PREVIEW_MAX_ROWS, STREAM_PREVIEW_MAX_CHARS } from '../constants';\nimport { ensureQueryLimit } from '../utils';\nimport { summarizeRows, MAIN_AGENT_COMPLETE_ROWS } from './data-summary';\n\n// ============================================\n// Source Agent\n// ============================================\n\n/**\n * Options to tune SourceAgent behavior when the MainAgent has already\n * resolved relevant tables via embedding search.\n *\n * - preResolvedSchema: replaces `tool.fullSchema` in the prompt — a compact\n * block of just the tables relevant to this question.\n * - skipSchemaSearch: drops the search_schema tool (and reduces the\n * iteration budget) because the LLM already has what it needs.\n */\nexport interface SourceAgentOptions {\n\tpreResolvedSchema?: string;\n\tskipSchemaSearch?: boolean;\n}\n\nexport class SourceAgent {\n\tprivate tool: ExternalTool;\n\tprivate config: AgentConfig;\n\tprivate streamBuffer: StreamBuffer;\n\tprivate attempts: number = 0;\n\tprivate options: SourceAgentOptions;\n\n\tconstructor(\n\t\ttool: ExternalTool,\n\t\tconfig: AgentConfig,\n\t\tstreamBuffer: StreamBuffer,\n\t\toptions?: SourceAgentOptions,\n\t) {\n\t\tthis.tool = tool;\n\t\tthis.config = config;\n\t\tthis.streamBuffer = streamBuffer;\n\t\tthis.options = options || {};\n\t}\n\n\t/**\n\t * Execute a query against this source based on the intent from the main agent.\n\t *\n\t * Flow:\n\t * 1. Build prompt with full schema (from tool.description) + intent\n\t * 2. Source agent's OWN LLM generates query via tool calling\n\t * 3. Execute with retry — all handled internally\n\t * 4. Return SourceAgentResult with data + isLimited metadata\n\t *\n\t * For very_large tier: LLM first calls search_schema to discover tables,\n\t * then writes SQL with correct table/column names.\n\t */\n\tasync execute(input: SourceToolInput): Promise<SourceAgentResult> {\n\t\tconst startTime = Date.now();\n\t\tconst { intent, aggregation = 'raw' } = input;\n\n\t\tlogger.info(`[SourceAgent:${this.tool.name}] Starting | intent: \"${intent}\" | aggregation: ${aggregation}`);\n\n\t\ttry {\n\t\t\t// Build prompt using template from backend/.prompts/agent-source-query/\n\t\t\tconst prompts = await this.buildPrompt(intent, aggregation);\n\n\t\t\tlogger.logLLMPrompt(`sourceAgent:${this.tool.name}`, 'system', extractPromptText(prompts.system));\n\t\t\tlogger.logLLMPrompt(`sourceAgent:${this.tool.name}`, 'user', prompts.user);\n\n\t\t\t// Build tool definitions — query tool + optional search_schema tool\n\t\t\tconst tools = this.buildToolDefinitions();\n\n\t\t\t// Track executed tool info — collect ALL successful queries\n\t\t\tlet executedTool: ExecutedToolInfo | null = null;\n\t\t\tlet allExecutedTools: ExecutedToolInfo[] = [];\n\t\t\tlet resultData: any[] = [];\n\t\t\tlet queryExecuted: string | undefined;\n\t\t\tlet totalRowsMatched = 0;\n\t\t\tlet querySucceeded = false; // Tracks whether a query has succeeded (for streaming label)\n\t\t\tlet successfulQueries = 0; // Count of successful queries (max 2: primary + follow-up)\n\n\t\t\t// Schema search function (available for large + very_large tiers).\n\t\t\t// Suppressed when the MainAgent pre-resolved the schema via embedding search.\n\t\t\tconst schemaSearchFn = this.options.skipSchemaSearch\n\t\t\t\t? undefined\n\t\t\t\t: ((this.tool as any).schemaSearchFn as\n\t\t\t\t\t| ((keywords: string[]) => string)\n\t\t\t\t\t| undefined);\n\n\t\t\t// Create tool handler — routes to query execution or schema search\n\t\t\tconst toolHandler = async (toolName: string, toolInput: any): Promise<string> => {\n\t\t\t\t// Handle schema search tool (does NOT count as a retry attempt)\n\t\t\t\tif (toolName.endsWith('_search_schema') && schemaSearchFn) {\n\t\t\t\t\tconst keywords = toolInput.keywords || [];\n\t\t\t\t\tlogger.info(`[SourceAgent:${this.tool.name}] Schema search: ${keywords.join(', ')}`);\n\n\t\t\t\t\tif (this.streamBuffer.hasCallback()) {\n\t\t\t\t\t\tthis.streamBuffer.write(`🔍 **Looking up tables:** ${keywords.join(', ')}\\n\\n`);\n\t\t\t\t\t\tawait streamDelay();\n\t\t\t\t\t}\n\n\t\t\t\t\tconst result = schemaSearchFn(keywords);\n\n\t\t\t\t\treturn `Schema search results:\\n\\n${result}`;\n\t\t\t\t}\n\n\t\t\t\t// Handle query execution tool\n\t\t\t\t// Block if already got 2 successful queries (1 primary + 1 follow-up)\n\t\t\t\tif (successfulQueries >= 2) {\n\t\t\t\t\treturn `⛔ Maximum successful queries reached (2). Use the data already retrieved.`;\n\t\t\t\t}\n\n\t\t\t\tthis.attempts++;\n\n\t\t\t\tif (this.attempts > this.config.maxRetries) {\n\t\t\t\t\tthrow new Error(`Max retry attempts (${this.config.maxRetries}) reached for ${this.tool.name}`);\n\t\t\t\t}\n\n\t\t\t\tif (this.attempts > 1 && this.streamBuffer.hasCallback()) {\n\t\t\t\t\tif (querySucceeded) {\n\t\t\t\t\t\t// Previous query succeeded — this is a follow-up for more data\n\t\t\t\t\t\tconst reason = toolInput.reason || '';\n\t\t\t\t\t\tthis.streamBuffer.write(`\\n\\n📝 **Follow-up query${reason ? `:** ${reason}` : '**'}\\n\\n`);\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// Previous query failed — this is an error retry\n\t\t\t\t\t\tthis.streamBuffer.write(`\\n\\n🔄 **Retrying query** (attempt ${this.attempts}/${this.config.maxRetries})\\n\\n`);\n\t\t\t\t\t}\n\t\t\t\t\tawait streamDelay();\n\t\t\t\t}\n\n\t\t\t\t// Enforce the SERVER-SIDE fetch cap (not the small LLM-show cap).\n\t\t\t\t// We fetch the full result so it can be summarized completely; only\n\t\t\t\t// a small/complete slice is later inlined into the main agent's\n\t\t\t\t// context (see formatResultForMainAgent). Capping the SQL at the\n\t\t\t\t// tiny show-limit is what truncated lookups and caused the main\n\t\t\t\t// agent to generalize from a biased head-slice.\n\t\t\t\tconst cappedInput = { ...toolInput };\n\t\t\t\tif (cappedInput.limit === undefined || cappedInput.limit > this.config.maxRowsFetched) {\n\t\t\t\t\tcappedInput.limit = this.config.maxRowsFetched;\n\t\t\t\t}\n\t\t\t\t// Enforce the cap IN the SQL text too. The agent often writes a big\n\t\t\t\t// LIMIT (e.g. 500) chasing \"all rows\"; capping only the `limit` param\n\t\t\t\t// left the executed/displayed SQL saying LIMIT 500 and reported a\n\t\t\t\t// misleading \"N of 500 total\". ensureQueryLimit rewrites an over-cap\n\t\t\t\t// LIMIT down to the fetch cap, keeps a smaller one, and adds one if\n\t\t\t\t// absent — so the SQL we run, show, and count are all consistent.\n\t\t\t\tconst _st = this.extractSourceType();\n\t\t\t\tif (typeof cappedInput.sql === 'string') {\n\t\t\t\t\tcappedInput.sql = ensureQueryLimit(cappedInput.sql, this.config.maxRowsFetched, this.config.maxRowsFetched, _st as any);\n\t\t\t\t}\n\t\t\t\tif (typeof cappedInput.query === 'string') {\n\t\t\t\t\tcappedInput.query = ensureQueryLimit(cappedInput.query, this.config.maxRowsFetched, this.config.maxRowsFetched, _st as any);\n\t\t\t\t}\n\n\t\t\t\t// Track the query for metadata\n\t\t\t\tqueryExecuted = cappedInput.sql || cappedInput.query || JSON.stringify(cappedInput);\n\n\t\t\t\t// Stream the generated query\n\t\t\t\tif (this.streamBuffer.hasCallback() && queryExecuted) {\n\t\t\t\t\tconst queryDisplay = cappedInput.sql || cappedInput.query;\n\t\t\t\t\tif (queryDisplay) {\n\t\t\t\t\t\tthis.streamBuffer.write(`📝 **Generated SQL query:**\\n\\`\\`\\`sql\\n${queryDisplay}\\n\\`\\`\\`\\n\\n`);\n\t\t\t\t\t}\n\t\t\t\t\tawait streamDelay();\n\t\t\t\t}\n\n\t\t\t\tlet queryStartTime = Date.now();\n\t\t\t\ttry {\n\t\t\t\t\t// Send timer markers — frontend renders \"⏳ Executing query\" label with a live timer pill\n\t\t\t\t\tif (this.streamBuffer.hasCallback()) {\n\t\t\t\t\t\tthis.streamBuffer.write(`__QUERY_TIMER_START_Executing query__`);\n\t\t\t\t\t}\n\t\t\t\t\tqueryStartTime = Date.now();\n\t\t\t\t\tconst result = await this.tool.fn(cappedInput);\n\t\t\t\t\tif (this.streamBuffer.hasCallback()) {\n\t\t\t\t\t\tconst querySeconds = ((Date.now() - queryStartTime) / 1000).toFixed(1);\n\t\t\t\t\t\tthis.streamBuffer.write(`__QUERY_TIMER_DONE_${querySeconds}__\\n\\n`);\n\t\t\t\t\t}\n\n\t\t\t\t\t// Check for error in result object (some tools return { error: ... } instead of throwing)\n\t\t\t\t\tif (result && result.error) {\n\t\t\t\t\t\tconst errorMsg = typeof result.error === 'string' ? result.error : JSON.stringify(result.error);\n\t\t\t\t\t\tlogger.warn(`[SourceAgent:${this.tool.name}] Tool returned error (attempt ${this.attempts}/${this.config.maxRetries}): ${errorMsg}`);\n\t\t\t\t\t\tif (this.streamBuffer.hasCallback()) {\n\t\t\t\t\t\t\tthis.streamBuffer.write(`❌ **Query failed:** ${errorMsg}\\n\\n`);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn `❌ ERROR: ${errorMsg}\\n\\nAnalyze the error and try again with a corrected query.`;\n\t\t\t\t\t}\n\n\t\t\t\t\t// Extract data\n\t\t\t\t\tresultData = result.data || [];\n\t\t\t\t\ttotalRowsMatched = result.metadata?.totalCount || result.count || resultData.length;\n\t\t\t\t\tquerySucceeded = true;\n\t\t\t\t\tsuccessfulQueries++;\n\n\t\t\t\t\t// Stream success with data preview\n\t\t\t\t\tif (this.streamBuffer.hasCallback()) {\n\t\t\t\t\t\tconst totalInfo = totalRowsMatched > resultData.length ? ` of ${totalRowsMatched} total` : '';\n\t\t\t\t\t\tthis.streamBuffer.write(`✅ **${resultData.length} rows${totalInfo} from ${this.tool.name}**\\n\\n`);\n\n\t\t\t\t\t\t// Stream data preview as DataTable (frontend renders with 2-row preview + \"Show All\" button).\n\t\t\t\t\t\t// Bounded to the canonical stream-preview limits (rows AND chars/field) so a\n\t\t\t\t\t\t// wide/large result can't bloat the stream — same shape as every other preview.\n\t\t\t\t\t\tif (resultData.length > 0) {\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tconst preview = formatQueryResultForLLM(resultData, {\n\t\t\t\t\t\t\t\t\tmaxRows: STREAM_PREVIEW_MAX_ROWS,\n\t\t\t\t\t\t\t\t\tmaxCharsPerField: STREAM_PREVIEW_MAX_CHARS,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tthis.streamBuffer.write(`<DataTable>${JSON.stringify(preview.data)}</DataTable>\\n\\n`);\n\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\t// BigInt, circular refs, or other serialization issues — skip DataTable\n\t\t\t\t\t\t\t\tthis.streamBuffer.write(`_Data preview not available_\\n\\n`);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\t// Build executed tool info (for component generation compatibility)\n\t\t\t\t\tconst formattedResult = formatToolResultForLLM(result, {\n\t\t\t\t\t\ttoolName: this.tool.name,\n\t\t\t\t\t\tmaxRows: 5,\n\t\t\t\t\t\tmaxCharsPerField: 200,\n\t\t\t\t\t});\n\n\t\t\t\t\texecutedTool = {\n\t\t\t\t\t\tid: this.tool.id,\n\t\t\t\t\t\tname: this.tool.name,\n\t\t\t\t\t\tparams: cappedInput,\n\t\t\t\t\t\tresult: {\n\t\t\t\t\t\t\t_totalRecords: totalRowsMatched,\n\t\t\t\t\t\t\t_recordsShown: resultData.length,\n\t\t\t\t\t\t\t_metadata: result.metadata,\n\t\t\t\t\t\t\t_sampleData: resultData.slice(0, TOOL_TRACKING_SAMPLE_ROWS),\n\t\t\t\t\t\t\t// For the main agent: a bounded summary over the FULL fetched\n\t\t\t\t\t\t\t// result (complete structure regardless of size) + a complete\n\t\t\t\t\t\t\t// slice for small results (so lookups arrive whole, not biased).\n\t\t\t\t\t\t\t_summary: summarizeRows(resultData),\n\t\t\t\t\t\t\t_mainAgentRows: resultData.slice(0, MAIN_AGENT_COMPLETE_ROWS),\n\t\t\t\t\t\t},\n\t\t\t\t\t\toutputSchema: this.tool.outputSchema,\n\t\t\t\t\t\tsourceSchema: this.tool.description,\n\t\t\t\t\t\tsourceType: this.extractSourceType(),\n\t\t\t\t\t};\n\t\t\t\t\tallExecutedTools.push(executedTool);\n\n\t\t\t\t\t// Return success signal with context on whether follow-up is allowed\n\t\t\t\t\tconst formatted = typeof formattedResult === 'string' ? formattedResult : JSON.stringify(formattedResult);\n\t\t\t\t\tconst followUpNote = successfulQueries < 2\n\t\t\t\t\t\t? 'STOP now and return this data — it satisfies the intent in the common case. Make a second query ONLY if a column or row the intent explicitly requires is verifiably MISSING from these results (not merely to widen, re-sort, or double-check). A redundant follow-up wastes a full round-trip.'\n\t\t\t\t\t\t: 'Maximum queries reached. Use the data you have.';\n\t\t\t\t\treturn `✅ Query executed successfully. ${resultData.length} rows returned (${totalRowsMatched} total matched). ${followUpNote}\\n\\n${formatted}`;\n\n\t\t\t\t} catch (execError) {\n\t\t\t\t\tconst errorMsg = execError instanceof Error\n\t\t\t\t\t\t? execError.message\n\t\t\t\t\t\t: (typeof execError === 'object' && execError !== null)\n\t\t\t\t\t\t\t? (execError as any).message || (execError as any).error || JSON.stringify(execError)\n\t\t\t\t\t\t\t: String(execError);\n\t\t\t\t\tlogger.warn(`[SourceAgent:${this.tool.name}] Tool execution failed (attempt ${this.attempts}/${this.config.maxRetries}): ${errorMsg}`);\n\t\t\t\t\tif (this.streamBuffer.hasCallback()) {\n\t\t\t\t\t\t// Close the timer before showing the error — otherwise the frontend spinner runs forever\n\t\t\t\t\t\tconst querySeconds = ((Date.now() - queryStartTime) / 1000).toFixed(1);\n\t\t\t\t\t\tthis.streamBuffer.write(`__QUERY_TIMER_DONE_${querySeconds}__\\n\\n`);\n\t\t\t\t\t\tthis.streamBuffer.write(`❌ **Query failed:** ${errorMsg}\\n\\n`);\n\t\t\t\t\t}\n\t\t\t\t\t// Return error to LLM so it can analyze and retry with a corrected query\n\t\t\t\t\treturn `❌ ERROR: ${errorMsg}\\n\\nAnalyze the error and try again with a corrected query.`;\n\t\t\t\t}\n\t\t\t};\n\n\t\t\t// maxIterations = total LLM round-trips allowed.\n\t\t\t// Each query attempt may need a preceding schema search, so they're paired.\n\t\t\t// With schema search: LLM often does search→query→search→query→response\n\t\t\t// Budget must cover: 2 successful queries (cap) + retries + schema searches + final response\n\t\t\t// With maxRetries=2, hasSchemaSearch: (2+2 queries × 2 cost) + 2 extra searches + 1 = 11\n\t\t\t// Without schema search: (2+2 queries × 1 cost) + 1 = 5\n\t\t\tconst hasSchemaSearch = !!schemaSearchFn;\n\t\t\tconst totalQueryAttempts = this.config.maxRetries + 2; // initial + retries + 1 follow-up\n\t\t\tconst perQueryCost = hasSchemaSearch ? 2 : 1; // schema search + query per attempt\n\t\t\tconst extraSearches = hasSchemaSearch ? 2 : 0; // LLM may do extra searches for broader intents\n\t\t\tconst responseIterations = 1; // final response\n\t\t\tconst maxIterations = (totalQueryAttempts * perQueryCost) + extraSearches + responseIterations;\n\n\t\t\tawait LLM.streamWithTools(\n\t\t\t\t{ sys: prompts.system, user: prompts.user },\n\t\t\t\ttools,\n\t\t\t\ttoolHandler,\n\t\t\t\t{\n\t\t\t\t\tmodel: this.config.sourceAgentModel || undefined,\n\t\t\t\t\tmaxTokens: 2048,\n\t\t\t\t\ttemperature: 0,\n\t\t\t\t\tapiKey: this.config.apiKey,\n\t\t\t\t},\n\t\t\t\tmaxIterations\n\t\t\t);\n\n\t\t\tconst executionTimeMs = Date.now() - startTime;\n\n\t\t\t// If tool was never called, return empty result\n\t\t\tif (!executedTool) {\n\t\t\t\tlogger.warn(`[SourceAgent:${this.tool.name}] LLM did not call the tool`);\n\t\t\t\treturn {\n\t\t\t\t\tsourceId: this.tool.id,\n\t\t\t\t\tsourceName: this.tool.name,\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\tdata: [],\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\ttotalRowsMatched: 0,\n\t\t\t\t\t\trowsReturned: 0,\n\t\t\t\t\t\tisLimited: false,\n\t\t\t\t\t\texecutionTimeMs,\n\t\t\t\t\t},\n\t\t\t\t\texecutedTool: this.buildEmptyExecutedTool(),\n\t\t\t\t\terror: 'Source agent did not execute any query',\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tlogger.info(`[SourceAgent:${this.tool.name}] Success | ${resultData.length} rows in ${executionTimeMs}ms`);\n\n\t\t\treturn {\n\t\t\t\tsourceId: this.tool.id,\n\t\t\t\tsourceName: this.tool.name,\n\t\t\t\tsuccess: true,\n\t\t\t\t// Don't retain the full fetched result — only the bounded slice the\n\t\t\t\t// main agent can actually use. The complete-structure summary and the\n\t\t\t\t// complete-small rows already live on each executedTool (`_summary` /\n\t\t\t\t// `_mainAgentRows`); keeping the full resultData here would hold up to\n\t\t\t\t// MAX_ROWS_FETCHED rows × every query in memory for the whole request\n\t\t\t\t// for no functional gain.\n\t\t\t\tdata: resultData.slice(0, MAIN_AGENT_COMPLETE_ROWS),\n\t\t\t\tmetadata: {\n\t\t\t\t\ttotalRowsMatched,\n\t\t\t\t\trowsReturned: resultData.length,\n\t\t\t\t\tisLimited: resultData.length < totalRowsMatched,\n\t\t\t\t\tqueryExecuted,\n\t\t\t\t\texecutionTimeMs,\n\t\t\t\t},\n\t\t\t\texecutedTool,\n\t\t\t\tallExecutedTools,\n\t\t\t};\n\n\t\t} catch (error) {\n\t\t\tconst executionTimeMs = Date.now() - startTime;\n\t\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\n\t\t\tlogger.error(`[SourceAgent:${this.tool.name}] Failed: ${errorMsg}`);\n\n\t\t\tif (this.streamBuffer.hasCallback()) {\n\t\t\t\tthis.streamBuffer.write(`\\n\\n❌ **Could not retrieve data from ${this.tool.name}:** ${errorMsg}\\n\\n`);\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tsourceId: this.tool.id,\n\t\t\t\tsourceName: this.tool.name,\n\t\t\t\tsuccess: false,\n\t\t\t\tdata: [],\n\t\t\t\tmetadata: {\n\t\t\t\t\ttotalRowsMatched: 0,\n\t\t\t\t\trowsReturned: 0,\n\t\t\t\t\tisLimited: false,\n\t\t\t\t\tqueryExecuted: undefined,\n\t\t\t\t\texecutionTimeMs,\n\t\t\t\t},\n\t\t\t\texecutedTool: this.buildEmptyExecutedTool(),\n\t\t\t\terror: errorMsg,\n\t\t\t};\n\t\t}\n\t}\n\n\t// ============================================\n\t// Private Helpers\n\t// ============================================\n\n\t/**\n\t * Build prompt using the prompt loader (file system → hardcoded fallback in prompts.ts).\n\t */\n\tprivate async buildPrompt(intent: string, aggregation: string): Promise<{ system: any; user: string }> {\n\t\tconst sourceName = this.tool.name;\n\t\tconst sourceType = this.extractSourceType();\n\t\t// When MainAgent pre-resolved tables via embedding search, prefer the compact\n\t\t// pre-resolved block. Otherwise use the full schema (all columns).\n\t\tconst fullSchema = this.options.preResolvedSchema\n\t\t\t|| (this.tool as any).fullSchema\n\t\t\t|| this.tool.description\n\t\t\t|| 'No schema available';\n\n\t\t// Load database-specific SQL rules for this source type\n\t\tconst databaseRules = await promptLoader.loadDatabaseRulesForType(sourceType);\n\n\t\t// Database-specific row limit syntax instruction\n\t\tconst rowLimitSyntax: Record<string, string> = {\n\t\t\t'mssql': `Use SELECT TOP ${this.config.maxRowsFetched} in every SELECT statement`,\n\t\t\t'postgres': `Add LIMIT ${this.config.maxRowsFetched} at the end of every query`,\n\t\t\t'mysql': `Add LIMIT ${this.config.maxRowsFetched} at the end of every query`,\n\t\t\t'excel': `Add LIMIT ${this.config.maxRowsFetched} at the end of every query`,\n\t\t\t'csv': `Add LIMIT ${this.config.maxRowsFetched} at the end of every query`,\n\t\t};\n\t\tconst rowLimitInstruction = rowLimitSyntax[sourceType] || `Limit results to ${this.config.maxRowsFetched} rows`;\n\n\t\t// Build schema search instructions based on tier.\n\t\t// If MainAgent pre-resolved the schema, we skip search_schema entirely.\n\t\tconst hasSchemaSearch = !this.options.skipSchemaSearch && !!(this.tool as any).schemaSearchFn;\n\t\tconst schemaTier = (this.tool as any).schemaTier || '';\n\t\tlet schemaSearchInstructions = '';\n\t\tif (this.options.preResolvedSchema) {\n\t\t\tschemaSearchInstructions = `## Pre-Resolved Schema\nThe tables shown above were pre-selected based on the user's intent. Use ONLY these tables and the columns shown — do NOT invent column names. Write your SQL query directly using these exact column names.`;\n\t\t} else if (hasSchemaSearch && schemaTier === 'very_large') {\n\t\t\tschemaSearchInstructions = `## Schema Search — REQUIRED\nThis source has a very large schema. The schema above shows only table names — no column details.\n**You MUST use search_schema BEFORE writing any query.** Do NOT guess or fabricate column names.\n1. FIRST: Call search_schema with keywords related to the requested data\n2. Review the returned column details, types, and sample values carefully\n3. THEN write your SQL query using the EXACT column names from the search results\n\n**CRITICAL: Column names in this database may NOT follow predictable patterns.**\n- Do NOT assume column name prefixes based on table name abbreviations\n- Do NOT invent column names by combining a table alias with a guessed suffix\n- ONLY use column names that appeared in search_schema results — copy them exactly\n- If you need columns you haven't searched for, call search_schema again before writing the query\n\nYou may search multiple times with different keywords if the first search doesn't find what you need.`;\n\t\t} else if (hasSchemaSearch) {\n\t\t\tschemaSearchInstructions = `## Schema Search — REQUIRED\nThis source has a large schema. The top tables are shown in detail above, but not all tables have full column details.\n**You MUST use search_schema BEFORE writing any query** to verify the exact column names for the tables you plan to query.\n1. FIRST: Call search_schema with the table names or keywords you intend to use\n2. Review the returned column names, types, and sample values\n3. THEN write your SQL query using the EXACT column names from the search results\nEven if a table appears in the detailed schema above, search_schema returns sample values that help you write correct WHERE filters.\n**Never skip the search step — it prevents failed queries and retries.**`;\n\t\t}\n\n\t\t// For Excel/CSV sources, clarify that the SQL engine is DuckDB\n\t\tconst displaySourceType = (sourceType === 'excel' || sourceType === 'csv')\n\t\t\t? `${sourceType} — uses DuckDB SQL engine`\n\t\t\t: sourceType;\n\n\t\tconst prompts = await promptLoader.loadPrompts('agent-source-query', {\n\t\t\tSOURCE_NAME: sourceName,\n\t\t\tSOURCE_TYPE: displaySourceType,\n\t\t\tFULL_SCHEMA: fullSchema,\n\t\t\tDATABASE_RULES: databaseRules,\n\t\t\tROW_LIMIT_SYNTAX: rowLimitInstruction,\n\t\t\t// NOTE: MAX_ROWS here governs the SQL row cap (the {{MAX_ROWS}} in the\n\t\t\t// \"limit results to N rows\" line), so it MUST match the fetch cap that\n\t\t\t// ROW_LIMIT_SYNTAX states — not the smaller LLM-show cap — or the prompt\n\t\t\t// contradicts itself (\"max 10 rows\" + \"LIMIT 50\") and the LLM picks its\n\t\t\t// own number. See maxRowsFetched override below.\n\t\t\tSCHEMA_SEARCH_INSTRUCTIONS: schemaSearchInstructions,\n\t\t\tMAX_ROWS: String(this.config.maxRowsFetched),\n\t\t\tAGGREGATION_MODE: aggregation,\n\t\t\tGLOBAL_KNOWLEDGE_BASE: this.config.globalKnowledgeBase || 'No global knowledge base available.',\n\t\t\tCURRENT_DATETIME: getCurrentDateTimeForPrompt(),\n\t\t\tKNOWLEDGE_BASE_CONTEXT: this.config.knowledgeBaseContext || 'No additional knowledge base context available.',\n\t\t\tINTENT: intent,\n\t\t});\n\t\treturn { system: prompts.system, user: prompts.user };\n\t}\n\n\t/**\n\t * Build all LLM tool definitions for this source agent.\n\t * Returns: [queryTool] for most tiers, [queryTool, searchSchemaTool] for very_large.\n\t */\n\tprivate buildToolDefinitions(): any[] {\n\t\tconst tools = [this.buildLLMToolDefinition()];\n\n\t\t// Add schema search tool for large + very_large tiers.\n\t\t// Suppress when MainAgent pre-resolved tables via embedding search.\n\t\tif ((this.tool as any).schemaSearchFn && !this.options.skipSchemaSearch) {\n\t\t\ttools.push(this.buildSchemaSearchToolDefinition());\n\t\t}\n\n\t\treturn tools;\n\t}\n\n\t/**\n\t * Build the query tool definition from the external tool.\n\t * Parses param descriptions like \"string - Sheet name\" or \"array (optional) - Columns\"\n\t * to extract the correct JSON schema type and required/optional status.\n\t */\n\tprivate buildLLMToolDefinition(): any {\n\t\tconst properties: Record<string, any> = {};\n\t\tconst required: string[] = [];\n\n\t\tconst toolParams: Record<string, any> = (this.tool as any).params || {};\n\t\tObject.entries(toolParams).forEach(([key, typeOrValue]) => {\n\t\t\tconst valueStr = String(typeOrValue).toLowerCase();\n\n\t\t\t// Extract type from description like \"string - Sheet name\" or \"array (optional) - Columns\"\n\t\t\tlet schemaType = 'string';\n\t\t\tconst typeMatch = valueStr.match(/^(string|number|integer|boolean|array|object)\\b/);\n\t\t\tif (typeMatch) {\n\t\t\t\tschemaType = typeMatch[1];\n\t\t\t} else if (typeof typeOrValue === 'number') {\n\t\t\t\tschemaType = Number.isInteger(typeOrValue) ? 'integer' : 'number';\n\t\t\t} else if (typeof typeOrValue === 'boolean') {\n\t\t\t\tschemaType = 'boolean';\n\t\t\t}\n\n\t\t\t// Check if optional\n\t\t\tconst isOptional = valueStr.includes('(optional)') || valueStr.includes('optional');\n\n\t\t\t// Use the full param value as description\n\t\t\tconst description = typeof typeOrValue === 'string' ? typeOrValue : `Parameter: ${key}`;\n\n\t\t\t// Build property schema based on type\n\t\t\tif (schemaType === 'array') {\n\t\t\t\tproperties[key] = { type: 'array', items: { type: 'string' }, description };\n\t\t\t} else if (schemaType === 'object') {\n\t\t\t\tproperties[key] = { type: 'object', description };\n\t\t\t} else {\n\t\t\t\tproperties[key] = { type: schemaType, description };\n\t\t\t}\n\n\t\t\tif (!isOptional) {\n\t\t\t\trequired.push(key);\n\t\t\t}\n\t\t});\n\n\t\t// Add optional 'reason' field — LLM provides this on follow-up queries to explain intent\n\t\tproperties['reason'] = {\n\t\t\ttype: 'string',\n\t\t\tdescription: 'string (optional) - Brief reason for this query. Required for follow-up queries after a successful first query. Explain what additional data you need and why.',\n\t\t};\n\n\t\treturn {\n\t\t\tname: this.tool.id,\n\t\t\tdescription: this.tool.description || `Query ${this.tool.name}`,\n\t\t\tinput_schema: {\n\t\t\t\ttype: 'object',\n\t\t\t\tproperties,\n\t\t\t\trequired: required.length > 0 ? required : undefined,\n\t\t\t},\n\t\t};\n\t}\n\n\t/**\n\t * Build the search_schema tool definition for very_large tier sources.\n\t * Allows the LLM to discover table/column names before writing SQL.\n\t */\n\tprivate buildSchemaSearchToolDefinition(): any {\n\t\treturn {\n\t\t\tname: `${this.tool.id}_search_schema`,\n\t\t\tdescription: 'Search the database schema for tables and columns matching keywords. Returns full column details for matching tables. Use this BEFORE writing a query to find exact table and column names.',\n\t\t\tinput_schema: {\n\t\t\t\ttype: 'object',\n\t\t\t\tproperties: {\n\t\t\t\t\tkeywords: {\n\t\t\t\t\t\ttype: 'array',\n\t\t\t\t\t\titems: { type: 'string' },\n\t\t\t\t\t\tdescription: 'Keywords to search for in table names and column names (e.g., [\"quotation\", \"amount\", \"customer\"]). Use multiple keywords for broader results.',\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\trequired: ['keywords'],\n\t\t\t},\n\t\t};\n\t}\n\n\t/**\n\t * Extract source type from tool ID.\n\t */\n\tprivate extractSourceType(): string {\n\t\tconst match = this.tool.id.match(/^(\\w+)-[a-f0-9]+_/);\n\t\tif (!match) return 'unknown';\n\n\t\tconst raw = match[1].toLowerCase();\n\n\t\t// Normalize variant source types (e.g., mssql_bridge → mssql)\n\t\tif (raw.startsWith('mssql')) return 'mssql';\n\t\tif (raw.startsWith('postgres')) return 'postgres';\n\t\tif (raw.startsWith('mysql')) return 'mysql';\n\t\tif (raw.startsWith('snowflake')) return 'snowflake';\n\n\t\treturn raw;\n\t}\n\n\t/**\n\t * Build an empty ExecutedToolInfo for error cases.\n\t */\n\tprivate buildEmptyExecutedTool(): ExecutedToolInfo {\n\t\treturn {\n\t\t\tid: this.tool.id,\n\t\t\tname: this.tool.name,\n\t\t\tparams: {},\n\t\t\tresult: {\n\t\t\t\t_totalRecords: 0,\n\t\t\t\t_recordsShown: 0,\n\t\t\t\t_sampleData: [],\n\t\t\t},\n\t\t\toutputSchema: this.tool.outputSchema,\n\t\t};\n\t}\n}\n","/**\n * ScriptRunner — Execute scripts in an isolated tsx subprocess.\n *\n * The subprocess approach replaces the earlier `new Function()` eval and gives us:\n * - Real sandbox (separate process, SIGKILL on timeout).\n * - Real TypeScript (tsx transpiles on the fly).\n * - npm imports available to scripts (clustering, stats, geo, etc.).\n *\n * Protocol: NDJSON over the child's stdin/stdout. See script-ipc.ts + backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md.\n */\n\nimport { spawn, type ChildProcessWithoutNullStreams } from 'child_process';\nimport * as path from 'path';\nimport * as os from 'os';\nimport type { ExternalTool } from '../services/tool-executor-service';\nimport type { StreamBuffer } from '../stream-buffer';\nimport type { ScriptRecipe, ScriptResult, ScriptQueryResult } from './types';\nimport {\n\tencodeMessage,\n\tLineSplitter,\n\ttype ChildToParentMessage,\n\ttype ExecutedQuery,\n} from './script-ipc';\nimport { logger } from '../../utils/logger';\nimport { formatQueryResultForLLM } from '../llm-result-truncator';\nimport { STREAM_PREVIEW_MAX_ROWS, STREAM_PREVIEW_MAX_CHARS } from '../constants';\n\n/**\n * Wall-clock cap for a single script execution.\n *\n * Default 60s — long enough for a multi-query analytical script, short enough\n * that a hung or looping script frees its concurrency slot quickly (see #2 in\n * backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md). The previous 5-minute value let\n * slow scripts pile up faster than they drained.\n *\n * Override globally with the `SCRIPT_TIMEOUT_MS` env var, or per-call via\n * `RunScriptOptions.timeoutMs`.\n */\nconst DEFAULT_SCRIPT_TIMEOUT_MS = 60_000;\nconst SCRIPT_TIMEOUT_MS = (() => {\n\tconst envVal = Number(process.env.SCRIPT_TIMEOUT_MS);\n\treturn Number.isFinite(envVal) && envVal > 0 ? envVal : DEFAULT_SCRIPT_TIMEOUT_MS;\n})();\n\n// ============================================\n// Concurrency cap (#2)\n// ============================================\n// Every runScript() spawns a full node+tsx process. With no cap, inbound load\n// translates 1:1 into spawned processes — a self-inflicted fork-bomb that\n// exhausts RAM/PIDs/CPU. The semaphore bounds how many run at once; excess\n// callers queue, and past a hard queue ceiling we shed (reject) rather than\n// pile up an unbounded backlog. See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md #2.\n\nconst MAX_CONCURRENT_SCRIPTS = (() => {\n\tconst envVal = Number(process.env.SCRIPT_MAX_CONCURRENCY);\n\tif (Number.isFinite(envVal) && envVal > 0) return Math.floor(envVal);\n\treturn Math.max(2, Math.min(8, (os.cpus()?.length || 4) - 1));\n})();\n\nconst MAX_SCRIPT_QUEUE = (() => {\n\tconst envVal = Number(process.env.SCRIPT_MAX_QUEUE);\n\tif (Number.isFinite(envVal) && envVal >= 0) return Math.floor(envVal);\n\treturn 100;\n})();\n\n/** Thrown by the semaphore when the wait queue is full — caller sheds the load. */\nexport class ScriptCapacityError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = 'ScriptCapacityError';\n\t}\n}\n\n/**\n * Counting semaphore with a bounded wait queue. A released slot is handed\n * directly to the next waiter (so `active` only changes when a slot is taken\n * from / returned to the free pool, never on transfer).\n */\nclass Semaphore {\n\tprivate active = 0;\n\tprivate waiters: Array<() => void> = [];\n\n\tconstructor(private readonly max: number, private readonly maxQueue: number) {}\n\n\tasync acquire(): Promise<void> {\n\t\tif (this.active < this.max) {\n\t\t\tthis.active++;\n\t\t\treturn;\n\t\t}\n\t\tif (this.waiters.length >= this.maxQueue) {\n\t\t\tthrow new ScriptCapacityError(\n\t\t\t\t`Script execution at capacity (${this.max} running, ${this.waiters.length} queued). Try again shortly.`,\n\t\t\t);\n\t\t}\n\t\treturn new Promise<void>((resolve) => this.waiters.push(resolve));\n\t}\n\n\trelease(): void {\n\t\tconst next = this.waiters.shift();\n\t\tif (next) next(); // transfer the slot to the next waiter (active unchanged)\n\t\telse this.active--; // no waiter — return the slot to the pool\n\t}\n\n\tget queued(): number { return this.waiters.length; }\n}\n\nconst scriptSemaphore = new Semaphore(MAX_CONCURRENT_SCRIPTS, MAX_SCRIPT_QUEUE);\nlogger.info(`[ScriptRunner] Concurrency cap: ${MAX_CONCURRENT_SCRIPTS} concurrent, queue ${MAX_SCRIPT_QUEUE}`);\n\n/**\n * Lazily resolved path to the `tsx` CLI binary. Resolved once at first use\n * so we don't pay `require.resolve` on every execution.\n */\nlet tsxBinaryPath: string | null = null;\nfunction resolveTsxBinary(): string {\n\tif (tsxBinaryPath) return tsxBinaryPath;\n\ttry {\n\t\t// tsx exposes its CLI via a distinct export — find the package root and look there\n\t\tconst tsxPkg = require.resolve('tsx/package.json');\n\t\tconst tsxDir = path.dirname(tsxPkg);\n\t\t// tsx 4.x ships the CLI as ./dist/cli.mjs\n\t\ttsxBinaryPath = path.join(tsxDir, 'dist', 'cli.mjs');\n\t\treturn tsxBinaryPath;\n\t} catch (err) {\n\t\tthrow new Error(\n\t\t\t`Could not resolve tsx binary. Install tsx as a dependency. Underlying error: ${(err as Error).message}`,\n\t\t);\n\t}\n}\n\n/**\n * Path to the bootstrap file that runs inside the child process.\n * Resolved relative to __dirname — when tsup bundles `index.ts`, this file\n * lives at dist/index.js and the bootstrap is emitted alongside at\n * dist/userResponse/scripts/script-bootstrap.js (mirroring the src tree).\n */\nfunction resolveBootstrapPath(): string {\n\t// In CJS output, __dirname is available. In ESM output, tsup shims it.\n\treturn path.resolve(__dirname, 'userResponse/scripts/script-bootstrap.js');\n}\n\n// ============================================\n// Public API\n// ============================================\n\nexport interface RunScriptOptions {\n\t/** Data sources the script is allowed to query via ctx.query */\n\texternalTools: ExternalTool[];\n\t/** Optional — for propagating per-query UI progress to the user */\n\tstreamBuffer?: StreamBuffer;\n\t/** Override the wall-clock timeout (default `SCRIPT_TIMEOUT_MS`, 60s). */\n\ttimeoutMs?: number;\n}\n\n/**\n * Execute a recipe by spawning a tsx child on the script's .ts file.\n * `scriptPath` is the absolute path to the saved `.ts` body.\n */\nexport async function runScript(\n\trecipe: ScriptRecipe,\n\tscriptPath: string,\n\tparams: Record<string, any>,\n\toptions: RunScriptOptions,\n): Promise<ScriptResult> {\n\tconst startedAt = Date.now();\n\tconst resolvedParams = coerceParams(recipe, applyDefaults(recipe, params));\n\tconst paramSchema = buildParamSchema(recipe);\n\n\tlogger.info(`[ScriptRunner] Executing \"${recipe.name}\" (${recipe.id}) with params: ${JSON.stringify(resolvedParams)}`);\n\n\t// Concurrency gate (#2) — bounds live subprocesses; sheds under sustained\n\t// overload. A capacity rejection returns a normal failure ScriptResult so the\n\t// caller falls through to the agent flow (which spawns no subprocess) instead\n\t// of crashing or piling up.\n\ttry {\n\t\tif (scriptSemaphore.queued > 0) {\n\t\t\tlogger.info(`[ScriptRunner] \"${recipe.name}\" queued (${scriptSemaphore.queued} ahead)`);\n\t\t}\n\t\tawait scriptSemaphore.acquire();\n\t} catch (capErr) {\n\t\tconst msg = capErr instanceof Error ? capErr.message : String(capErr);\n\t\tlogger.warn(`[ScriptRunner] \"${recipe.name}\" shed: ${msg}`);\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\tdata: [],\n\t\t\texecutedQueries: [],\n\t\t\terror: msg,\n\t\t\texecutionTimeMs: Date.now() - startedAt,\n\t\t};\n\t}\n\n\ttry {\n\t\tconst result = await executeInSubprocess(\n\t\t\tscriptPath,\n\t\t\tresolvedParams,\n\t\t\tparamSchema,\n\t\t\toptions.externalTools,\n\t\t\toptions.streamBuffer,\n\t\t\toptions.timeoutMs ?? SCRIPT_TIMEOUT_MS,\n\t\t);\n\n\t\tconst totalMs = Date.now() - startedAt;\n\n\t\tif (result.kind === 'ok') {\n\t\t\t// Enrich each executedQuery with the real source name (child only had toolId)\n\t\t\tconst executedQueries = enrichQueries(result.executedQueries, options.externalTools);\n\t\t\tlogger.info(`[ScriptRunner] \"${recipe.name}\" succeeded — ${result.data.length} rows in ${totalMs}ms`);\n\t\t\treturn {\n\t\t\t\tsuccess: true,\n\t\t\t\tdata: result.data,\n\t\t\t\texecutedQueries,\n\t\t\t\texecutionTimeMs: totalMs,\n\t\t\t};\n\t\t}\n\n\t\tconst executedQueries = enrichQueries(result.executedQueries, options.externalTools);\n\t\tlogger.error(`[ScriptRunner] \"${recipe.name}\" failed in ${totalMs}ms [${result.phase}]: ${result.message}`);\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\tdata: [],\n\t\t\texecutedQueries,\n\t\t\terror: result.message,\n\t\t\terrorPhase: result.phase,\n\t\t\texecutionTimeMs: totalMs,\n\t\t};\n\n\t} catch (err) {\n\t\tconst totalMs = Date.now() - startedAt;\n\t\tconst msg = err instanceof Error ? err.message : String(err);\n\t\tlogger.error(`[ScriptRunner] \"${recipe.name}\" crashed in ${totalMs}ms: ${msg}`);\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\tdata: [],\n\t\t\texecutedQueries: [],\n\t\t\terror: msg,\n\t\t\texecutionTimeMs: totalMs,\n\t\t};\n\t} finally {\n\t\t// Always free the slot — success, failure, timeout, or crash.\n\t\tscriptSemaphore.release();\n\t}\n}\n\n// ============================================\n// Subprocess orchestration\n// ============================================\n\ntype SubprocessResult =\n\t| { kind: 'ok'; data: any[]; count: number; executedQueries: ExecutedQuery[] }\n\t| {\n\t\tkind: 'err';\n\t\tphase: 'compile' | 'runtime' | 'timeout' | 'ipc';\n\t\tmessage: string;\n\t\texecutedQueries: ExecutedQuery[];\n\t};\n\nfunction executeInSubprocess(\n\tscriptPath: string,\n\tparams: Record<string, any>,\n\tparamSchema: Record<string, 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean'>,\n\texternalTools: ExternalTool[],\n\tstreamBuffer: StreamBuffer | undefined,\n\ttimeoutMs: number,\n): Promise<SubprocessResult> {\n\treturn new Promise((resolve) => {\n\t\tconst tsxBin = resolveTsxBinary();\n\t\tconst bootstrap = resolveBootstrapPath();\n\n\t\tconst child: ChildProcessWithoutNullStreams = spawn(\n\t\t\tprocess.execPath, // node — tsx runs via its own cli, we invoke it with node\n\t\t\t[tsxBin, bootstrap, scriptPath],\n\t\t\t{\n\t\t\t\tstdio: ['pipe', 'pipe', 'pipe'],\n\t\t\t\t// `detached: true` makes the child a process-group LEADER. tsx 4.x\n\t\t\t\t// re-execs node to run the user script, so the script is a GRANDCHILD;\n\t\t\t\t// a plain `child.kill()` would hit only the tsx wrapper and orphan the\n\t\t\t\t// grandchild (a runaway/looping script that outlives the timeout — #4).\n\t\t\t\t// With its own group we can SIGKILL the whole tree via `-pid` in\n\t\t\t\t// cleanup(). We deliberately do NOT unref() — the parent still tracks\n\t\t\t\t// the child's exit and pipes normally. (POSIX; Windows falls back to\n\t\t\t\t// a direct kill in cleanup().)\n\t\t\t\tdetached: process.platform !== 'win32',\n\t\t\t\tenv: {\n\t\t\t\t\t...process.env,\n\t\t\t\t\t// Keep the child quiet about dotenv etc.\n\t\t\t\t\tNODE_NO_WARNINGS: '1',\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\n\t\tlet resolved = false;\n\t\tlet executedQueries: ExecutedQuery[] = [];\n\t\tlet stderrBuffer = '';\n\n\t\tconst cleanup = () => {\n\t\t\tif (child.killed) return;\n\t\t\tconst pid = child.pid;\n\t\t\t// Kill the whole process group (negative pid) so the tsx wrapper AND the\n\t\t\t// user-script grandchild both die — a runaway script can't survive the\n\t\t\t// timeout as an orphan (#4). Falls back to a direct kill on Windows, when\n\t\t\t// pid is unknown, or if the group is already gone (ESRCH).\n\t\t\tif (pid && process.platform !== 'win32') {\n\t\t\t\ttry {\n\t\t\t\t\tprocess.kill(-pid, 'SIGKILL');\n\t\t\t\t\treturn;\n\t\t\t\t} catch { /* group gone or not a leader — fall through */ }\n\t\t\t}\n\t\t\ttry { child.kill('SIGKILL'); } catch { /* ignore */ }\n\t\t};\n\n\t\tconst finish = (r: SubprocessResult) => {\n\t\t\tif (resolved) return;\n\t\t\tresolved = true;\n\t\t\tclearTimeout(timer);\n\t\t\tcleanup();\n\t\t\tresolve(r);\n\t\t};\n\n\t\tconst timer = setTimeout(() => {\n\t\t\tfinish({\n\t\t\t\tkind: 'err',\n\t\t\t\tphase: 'timeout',\n\t\t\t\tmessage: `Script execution timed out after ${timeoutMs}ms`,\n\t\t\t\texecutedQueries,\n\t\t\t});\n\t\t}, timeoutMs);\n\n\t\t// ---- stdout: parse NDJSON from child ----\n\t\tconst stdoutSplitter = new LineSplitter();\n\t\tchild.stdout.setEncoding('utf-8');\n\t\tchild.stdout.on('data', (chunk: string) => {\n\t\t\tlet lines: string[];\n\t\t\ttry {\n\t\t\t\tlines = stdoutSplitter.push(chunk);\n\t\t\t} catch (err) {\n\t\t\t\t// Oversized/runaway child output (#9) — abort and SIGKILL the group.\n\t\t\t\tfinish({\n\t\t\t\t\tkind: 'err',\n\t\t\t\t\tphase: 'ipc',\n\t\t\t\t\tmessage: err instanceof Error ? err.message : String(err),\n\t\t\t\t\texecutedQueries,\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfor (const line of lines) {\n\t\t\t\tlet msg: ChildToParentMessage;\n\t\t\t\ttry {\n\t\t\t\t\tmsg = JSON.parse(line);\n\t\t\t\t} catch {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\thandleChildMessage(msg);\n\t\t\t}\n\t\t});\n\n\t\t// ---- stderr: collect for error reporting ----\n\t\tchild.stderr.setEncoding('utf-8');\n\t\tchild.stderr.on('data', (chunk: string) => {\n\t\t\tstderrBuffer += chunk;\n\t\t\t// Avoid unbounded growth\n\t\t\tif (stderrBuffer.length > 10_000) {\n\t\t\t\tstderrBuffer = stderrBuffer.slice(-10_000);\n\t\t\t}\n\t\t});\n\n\t\tchild.on('error', (err) => {\n\t\t\tfinish({\n\t\t\t\tkind: 'err',\n\t\t\t\tphase: 'ipc',\n\t\t\t\tmessage: `Failed to spawn child process: ${err.message}`,\n\t\t\t\texecutedQueries,\n\t\t\t});\n\t\t});\n\n\t\tchild.on('exit', (code, signal) => {\n\t\t\tif (resolved) return;\n\t\t\t// Child exited without sending RESULT or ERROR. Use stderr as a clue.\n\t\t\tconst tail = stderrBuffer.trim().split('\\n').slice(-8).join('\\n');\n\t\t\tfinish({\n\t\t\t\tkind: 'err',\n\t\t\t\tphase: 'runtime',\n\t\t\t\tmessage: `Script child exited (code=${code}, signal=${signal}) without a result.${tail ? ' stderr: ' + tail : ''}`,\n\t\t\t\texecutedQueries,\n\t\t\t});\n\t\t});\n\n\t\t// ---- send INIT to kick the child off ----\n\t\tconst init = encodeMessage({\n\t\t\ttype: 'init',\n\t\t\tparams,\n\t\t\tnow: new Date().toISOString(),\n\t\t\tparamSchema,\n\t\t});\n\t\tchild.stdin.write(init);\n\n\t\t// ============================================\n\t\t// Handlers\n\t\t// ============================================\n\n\t\tfunction handleChildMessage(msg: ChildToParentMessage): void {\n\t\t\tswitch (msg.type) {\n\t\t\t\tcase 'stream':\n\t\t\t\t\tif (streamBuffer?.hasCallback()) {\n\t\t\t\t\t\tstreamBuffer.write(msg.chunk);\n\t\t\t\t\t}\n\t\t\t\t\treturn;\n\n\t\t\t\tcase 'result':\n\t\t\t\t\texecutedQueries = withSynthesizedFinalData(msg.executedQueries, msg.data, msg.count);\n\t\t\t\t\tfinish({\n\t\t\t\t\t\tkind: 'ok',\n\t\t\t\t\t\tdata: msg.data,\n\t\t\t\t\t\tcount: msg.count,\n\t\t\t\t\t\texecutedQueries,\n\t\t\t\t\t});\n\t\t\t\t\treturn;\n\n\t\t\t\tcase 'error':\n\t\t\t\t\tfinish({\n\t\t\t\t\t\tkind: 'err',\n\t\t\t\t\t\tphase: msg.phase === 'compile' ? 'compile' : 'runtime',\n\t\t\t\t\t\tmessage: msg.message + (msg.stack ? `\\n${msg.stack}` : ''),\n\t\t\t\t\t\texecutedQueries,\n\t\t\t\t\t});\n\t\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t});\n}\n\n// ============================================\n// Tool authorization\n// ============================================\n\nfunction resolveAuthorizedTool(\n\ttoolId: string,\n\tbyId: Map<string, ExternalTool>,\n\tbyName: Map<string, ExternalTool>,\n): ExternalTool | null {\n\tconst exact = byId.get(toolId);\n\tif (exact) return exact;\n\n\tconst exactName = byName.get(toolId.toLowerCase());\n\tif (exactName) return exactName;\n\n\t// Partial ID match — kept for resilience when source IDs rotate\n\tfor (const [id, tool] of byId) {\n\t\tif (id.includes(toolId) || toolId.includes(id)) return tool;\n\t}\n\n\t// Partial name match\n\tconst lower = toolId.toLowerCase();\n\tfor (const [name, tool] of byName) {\n\t\tif (name.includes(lower) || lower.includes(name)) return tool;\n\t}\n\n\treturn null;\n}\n\n// ============================================\n// Streaming helpers (replicated from old ScriptContext so per-query UI progress still flows)\n// ============================================\n\nfunction streamQueryStart(sb: StreamBuffer | undefined, sourceName: string, sql: string): void {\n\tif (!sb?.hasCallback()) return;\n\tsb.write(`\\n📝 **Querying ${sourceName}:**\\n\\`\\`\\`sql\\n${sql}\\n\\`\\`\\`\\n\\n`);\n\tsb.write(`__QUERY_TIMER_START_Executing query__`);\n}\n\nfunction streamQueryDone(sb: StreamBuffer | undefined, ms: number): void {\n\tif (!sb?.hasCallback()) return;\n\tsb.write(`__QUERY_TIMER_DONE_${(ms / 1000).toFixed(1)}__\\n\\n`);\n}\n\nfunction streamQuerySuccess(sb: StreamBuffer | undefined, sourceName: string, rows: number, total: number): void {\n\tif (!sb?.hasCallback()) return;\n\tconst totalInfo = total > rows ? ` of ${total} total` : '';\n\tsb.write(`✅ **${rows} rows${totalInfo} from ${sourceName}**\\n\\n`);\n}\n\nfunction streamQueryError(sb: StreamBuffer | undefined, sourceName: string, msg: string): void {\n\tif (!sb?.hasCallback()) return;\n\tsb.write(`❌ **Query failed on ${sourceName}:** ${msg}\\n\\n`);\n}\n\nfunction streamRunToolStart(sb: StreamBuffer | undefined, toolName: string): void {\n\tif (!sb?.hasCallback()) return;\n\tsb.write(`\\n🔧 **Running ${toolName}...**\\n\\n`);\n\tsb.write(`__QUERY_TIMER_START_Executing tool__`);\n}\n\nfunction streamRunToolDone(sb: StreamBuffer | undefined, toolName: string, ms: number): void {\n\tif (!sb?.hasCallback()) return;\n\tsb.write(`__QUERY_TIMER_DONE_${(ms / 1000).toFixed(1)}__\\n\\n`);\n\tsb.write(`✅ **${toolName}** completed\\n\\n`);\n}\n\nfunction streamDataPreview(sb: StreamBuffer | undefined, data: any[]): void {\n\tif (!sb?.hasCallback() || data.length === 0) return;\n\ttry {\n\t\tconst preview = formatQueryResultForLLM(data, {\n\t\t\tmaxRows: STREAM_PREVIEW_MAX_ROWS,\n\t\t\tmaxCharsPerField: STREAM_PREVIEW_MAX_CHARS,\n\t\t});\n\t\tsb.write(`<DataTable>${JSON.stringify(preview.data)}</DataTable>\\n\\n`);\n\t} catch {\n\t\tsb.write(`_Data preview not available_\\n\\n`);\n\t}\n}\n\n// ============================================\n// Result enrichment\n// ============================================\n\n/**\n * Prepend a synthetic \"computed:_final\" entry carrying the script's post-JS\n * returned data so the component generator and the queryCache-backed UI hydration\n * path always have something to bind to.\n *\n * Two situations where we add it:\n * 1. Script ran SQL via ctx.query and then transformed the rows in JS (e.g.,\n * HHI contributions, weighted averages). Without the synthetic entry the\n * LLM only sees raw SQL output and either picks the wrong columns or\n * invents federation SQL to recompute what the script already produced.\n * 2. Script never called ctx.query at all (only ctx.runTool / pure JS). The\n * bootstrap's executedQueries is empty; without a synthetic entry the\n * component path falls back to MainAgent's executedTools and tries to\n * build a queryId from `t.params.sql`, which is empty for direct tools\n * (their params are structured, not SQL). That yields an empty-payload\n * encrypted token and the UI renders \"Query expired or not found\".\n *\n * Pass-through SQL scripts (final shape === raw SQL shape) skip the synthetic\n * entry to keep the prompt lean — the raw SQL row IS the answer.\n *\n * See backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md.\n */\nfunction withSynthesizedFinalData(\n\tqueries: ExecutedQuery[],\n\tfinalData: any[],\n\tfinalCount: number,\n): ExecutedQuery[] {\n\tif (!finalData || finalData.length === 0) return queries;\n\n\t// Case 2 — no tracked queries at all (runTool-only or pure-JS script). The\n\t// script's return value is the only data; always surface it through the\n\t// script_dataset sentinel so the cached payload can hydrate the components\n\t// on UI replay.\n\tif (!queries || queries.length === 0) {\n\t\treturn [{\n\t\t\tsourceId: 'computed:_final',\n\t\t\tsourceName: 'Script final dataset',\n\t\t\tsql: '-- script final returned data (runTool / pure JS, no ctx.query)',\n\t\t\tdata: finalData,\n\t\t\tcount: finalCount,\n\t\t\texecutionTimeMs: 0,\n\t\t\tvirtual: true,\n\t\t}];\n\t}\n\n\t// Case 1 — there is at least one real SQL query. Compare column sets from\n\t// the first SQL query's first row vs the script's final first row. If the\n\t// script added or changed columns, surface the final.\n\tconst firstSqlQuery = queries.find(q => !q.virtual);\n\tconst rawSample = firstSqlQuery?.data?.[0];\n\tconst finalSample = finalData[0];\n\tif (!rawSample || !finalSample || typeof rawSample !== 'object' || typeof finalSample !== 'object') {\n\t\treturn queries;\n\t}\n\n\tconst rawCols = new Set(Object.keys(rawSample));\n\tconst finalCols = Object.keys(finalSample);\n\tconst transformed =\n\t\tfinalCols.length !== rawCols.size ||\n\t\tfinalCols.some(c => !rawCols.has(c));\n\n\tif (!transformed) return queries;\n\n\tconst synthetic: ExecutedQuery = {\n\t\tsourceId: 'computed:_final',\n\t\tsourceName: 'Script final dataset',\n\t\tsql: '-- script final returned data (post-JS transforms)',\n\t\tdata: finalData,\n\t\tcount: finalCount,\n\t\texecutionTimeMs: 0,\n\t\tvirtual: true,\n\t};\n\treturn [synthetic, ...queries];\n}\n\nfunction enrichQueries(\n\tqueries: ExecutedQuery[],\n\texternalTools: ExternalTool[],\n): ScriptQueryResult[] {\n\tconst byId = new Map(externalTools.map(t => [t.id, t]));\n\treturn queries.map(q => {\n\t\tconst tool = byId.get(q.sourceId);\n\t\treturn {\n\t\t\tsourceId: q.sourceId,\n\t\t\tsourceName: tool?.name ?? q.sourceName ?? q.sourceId,\n\t\t\tsql: q.sql,\n\t\t\tdata: q.data,\n\t\t\tcount: q.count,\n\t\t\ttotalCount: q.totalCount,\n\t\t\texecutionTimeMs: q.executionTimeMs,\n\t\t\tvirtual: q.virtual,\n\t\t};\n\t});\n}\n\n// ============================================\n// Param handling (preserved from the pre-subprocess runner)\n// ============================================\n\nfunction buildParamSchema(recipe: ScriptRecipe): Record<string, 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean'> {\n\tconst schema: Record<string, any> = {};\n\tfor (const p of recipe.parameters) {\n\t\tschema[p.name] = p.type;\n\t}\n\treturn schema;\n}\n\nfunction applyDefaults(recipe: ScriptRecipe, params: Record<string, any>): Record<string, any> {\n\tconst resolved = { ...params };\n\tfor (const paramDef of recipe.parameters) {\n\t\tif (resolved[paramDef.name] === undefined && paramDef.default !== undefined) {\n\t\t\tresolved[paramDef.name] = paramDef.default;\n\t\t}\n\t}\n\treturn resolved;\n}\n\nfunction coerceParams(recipe: ScriptRecipe, params: Record<string, any>): Record<string, any> {\n\tconst coerced = { ...params };\n\n\tfor (const paramDef of recipe.parameters) {\n\t\tconst val = coerced[paramDef.name];\n\t\tif (val === undefined || val === null) continue;\n\n\t\tswitch (paramDef.type) {\n\t\t\tcase 'date': {\n\t\t\t\tif (!(val instanceof Date)) {\n\t\t\t\t\tconst d = new Date(val);\n\t\t\t\t\tif (!isNaN(d.getTime())) coerced[paramDef.name] = d.toISOString();\n\t\t\t\t} else {\n\t\t\t\t\tcoerced[paramDef.name] = val.toISOString();\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase 'date_range': {\n\t\t\t\tif (val && typeof val === 'object') {\n\t\t\t\t\tconst from = (val as any).from ?? (Array.isArray(val) ? val[0] : undefined);\n\t\t\t\t\tconst to = (val as any).to ?? (Array.isArray(val) ? val[1] : undefined);\n\t\t\t\t\tconst fromD = from instanceof Date ? from : from != null ? new Date(from) : undefined;\n\t\t\t\t\tconst toD = to instanceof Date ? to : to != null ? new Date(to) : undefined;\n\t\t\t\t\tcoerced[paramDef.name] = {\n\t\t\t\t\t\tfrom: fromD && !isNaN(fromD.getTime()) ? fromD.toISOString() : undefined,\n\t\t\t\t\t\tto: toD && !isNaN(toD.getTime()) ? toD.toISOString() : undefined,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase 'number': {\n\t\t\t\tif (typeof val !== 'number') {\n\t\t\t\t\tconst n = Number(val);\n\t\t\t\t\tif (!isNaN(n)) coerced[paramDef.name] = n;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase 'boolean': {\n\t\t\t\tif (typeof val !== 'boolean') {\n\t\t\t\t\tcoerced[paramDef.name] = val === 'true' || val === 1 || val === '1';\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn coerced;\n}\n","/**\n * script-ipc.ts — Protocol definitions for parent/child IPC.\n *\n * Transport: newline-delimited JSON over the child's stdin/stdout.\n * stderr is passed through for uncaught exceptions and compile errors.\n *\n * See backend/docs/SCRIPT-FLOW-IMPLEMENTATION.md § IPC Bridge.\n */\n\n// ============================================\n// Parent → Child\n// ============================================\n\nexport interface InitMessage {\n\ttype: 'init';\n\t/**\n\t * Params passed to getData(). Date/date_range values cross the IPC boundary\n\t * as ISO strings and are restored to Date objects in the child via paramSchema.\n\t */\n\tparams: Record<string, any>;\n\t/** Frozen timestamp for ctx.now — ISO string */\n\tnow: string;\n\t/**\n\t * Maps each parameter name to its declared type so the child can restore\n\t * Date objects (lost during JSON serialization).\n\t */\n\tparamSchema?: Record<string, 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean'>;\n}\n\nexport interface QueryResultMessage {\n\ttype: 'query_result';\n\t/** Matches the `id` from the child's QUERY message */\n\tid: string;\n\tdata: any[];\n\tcount: number;\n\tmetadata?: {\n\t\ttotalCount?: number;\n\t\texecutionTimeMs?: number;\n\t};\n}\n\nexport interface QueryErrorMessage {\n\ttype: 'query_error';\n\tid: string;\n\terror: string;\n}\n\n/**\n * Response to a `run_tool` request — carries whatever the direct-tool's fn()\n * returned. Shape is arbitrary; the script is expected to know what its chosen\n * tool yields and unwrap accordingly.\n */\nexport interface RunToolResultMessage {\n\ttype: 'run_tool_result';\n\tid: string;\n\tresult: any;\n}\n\nexport type ParentToChildMessage =\n\t| InitMessage\n\t| QueryResultMessage\n\t| QueryErrorMessage\n\t| RunToolResultMessage;\n\n// ============================================\n// Child → Parent\n// ============================================\n\nexport interface QueryMessage {\n\ttype: 'query';\n\t/** Short uuid — parent uses this to correlate its response */\n\tid: string;\n\ttoolId: string;\n\tsql: string;\n}\n\n/**\n * Run an arbitrary tool with structured params. Used by ctx.runTool() to call\n * direct tools (and any tool whose `fn(params)` doesn't expect a SQL string).\n * Reply is RunToolResultMessage or QueryErrorMessage.\n */\nexport interface RunToolMessage {\n\ttype: 'run_tool';\n\tid: string;\n\ttoolId: string;\n\tparams: Record<string, any>;\n}\n\nexport interface StreamMessage {\n\ttype: 'stream';\n\tchunk: string;\n}\n\nexport interface ExecutedQuery {\n\tsourceId: string;\n\tsourceName: string;\n\tsql: string;\n\tdata: any[];\n\tcount: number;\n\ttotalCount?: number;\n\texecutionTimeMs: number;\n\t/**\n\t * When the script registered a computed dataset via ctx.emit(), this is\n\t * marked virtual so component generation can distinguish it from a real SQL query.\n\t */\n\tvirtual?: boolean;\n}\n\nexport interface ResultMessage {\n\ttype: 'result';\n\tdata: any[];\n\tcount: number;\n\texecutedQueries: ExecutedQuery[];\n\texecutionTimeMs: number;\n\t/** First 5 rows of the final `data` — lets the parent judge intent-match cheaply */\n\tsampleRows: any[];\n}\n\nexport interface ErrorMessage {\n\ttype: 'error';\n\tmessage: string;\n\tstack?: string;\n\t/** \"compile\" → tsx couldn't parse; \"runtime\" → thrown during getData */\n\tphase?: 'compile' | 'runtime';\n}\n\nexport type ChildToParentMessage =\n\t| QueryMessage\n\t| RunToolMessage\n\t| StreamMessage\n\t| ResultMessage\n\t| ErrorMessage;\n\n// ============================================\n// Utilities\n// ============================================\n\n/**\n * Encode a message for transport: JSON + newline.\n */\nexport function encodeMessage(msg: ParentToChildMessage | ChildToParentMessage): string {\n\treturn JSON.stringify(msg) + '\\n';\n}\n\n/**\n * Thrown when a single NDJSON line grows past the byte ceiling without a\n * newline — a runaway writer or a pathologically large message. Callers abort\n * (kill the child / exit) instead of buffering toward OOM. See #9 in\n * backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md.\n */\nexport class LineOverflowError extends Error {\n\tconstructor(public readonly buffered: number, public readonly limit: number) {\n\t\tsuper(`IPC message exceeded ${limit} bytes without a newline (${buffered} buffered) — aborting to avoid unbounded memory.`);\n\t\tthis.name = 'LineOverflowError';\n\t}\n}\n\n/** Default per-message ceiling — generous enough for large result sets, low\n * enough to catch a runaway/infinite writer. Override via SCRIPT_MAX_IPC_BYTES. */\nconst DEFAULT_MAX_IPC_BYTES = (() => {\n\tconst envVal = Number(process.env.SCRIPT_MAX_IPC_BYTES);\n\treturn Number.isFinite(envVal) && envVal > 0 ? Math.floor(envVal) : 64 * 1024 * 1024;\n})();\n\n/**\n * Split a stream of data chunks on newlines and emit each complete JSON line.\n * Partial lines are buffered until the next chunk arrives, up to `maxBytes` —\n * past which `push` throws `LineOverflowError` rather than grow unbounded.\n */\nexport class LineSplitter {\n\tprivate buffer = '';\n\n\tconstructor(private readonly maxBytes: number = DEFAULT_MAX_IPC_BYTES) {}\n\n\tpush(chunk: string): string[] {\n\t\tthis.buffer += chunk;\n\t\tconst lines: string[] = [];\n\t\tlet idx: number;\n\t\twhile ((idx = this.buffer.indexOf('\\n')) !== -1) {\n\t\t\tconst line = this.buffer.slice(0, idx);\n\t\t\tthis.buffer = this.buffer.slice(idx + 1);\n\t\t\tif (line.length > 0) lines.push(line);\n\t\t}\n\t\t// A partial line past the ceiling means no newline is coming (or the\n\t\t// message is pathologically large). Drop the buffer and signal abort.\n\t\tif (this.buffer.length > this.maxBytes) {\n\t\t\tconst buffered = this.buffer.length;\n\t\t\tthis.buffer = '';\n\t\t\tthrow new LineOverflowError(buffered, this.maxBytes);\n\t\t}\n\t\treturn lines;\n\t}\n\n\t/** Flush any remaining partial data (useful on stream close) */\n\tflush(): string | null {\n\t\tif (this.buffer.length === 0) return null;\n\t\tconst rest = this.buffer;\n\t\tthis.buffer = '';\n\t\treturn rest;\n\t}\n}\n\n/**\n * Generate a short correlation id for in-flight queries.\n * Collision within a single execution is astronomically unlikely.\n */\nexport function shortId(): string {\n\treturn Math.random().toString(36).slice(2, 10);\n}\n","/**\n * Main Agent (Orchestrator)\n *\n * A single LLM.streamWithTools() call that handles everything:\n * - Routing: decides which source(s) to query based on summaries\n * - Querying: calls source tools (each wraps an independent SourceAgent)\n * - Direct tools: calls pre-built function tools directly with LLM-provided params\n * - Re-querying: if data is wrong/incomplete, calls tools again with modified intent\n * - Analysis: generates final text response from the data\n *\n * Two tool types:\n * - \"source\" tools: main agent sees summaries, SourceAgent handles SQL generation independently\n * - \"direct\" tools: main agent calls fn() directly with structured params (no SourceAgent)\n */\n\nimport { LLM } from '../../llm';\nimport { promptLoader } from '../prompt-loader';\nimport { getCurrentDateTimeForPrompt } from '../../utils/datetime';\nimport { logger } from '../../utils/logger';\nimport { extractPromptText } from '../prompt-extractor';\nimport { StreamBuffer, TaggedStreamBuffer, streamDelay } from '../stream-buffer';\nimport { formatToolResultForLLM } from '../llm-result-truncator';\nimport { safeTruncate } from '../../utils/surrogate';\nimport { summarizeRows, MAIN_AGENT_COMPLETE_ROWS } from './data-summary';\nimport type { ExternalTool, ExecutedToolInfo } from '../services/tool-executor-service';\nimport type {\n\tSourceSummary,\n\tSourceAgentResult,\n\tAgentResponse,\n\tAgentConfig,\n\tSourceToolInput,\n\tAgentWrittenScript,\n\tWorkflowDescriptor,\n\tSelectedWorkflow,\n} from './types';\nimport { buildSourceSummaries, formatSummariesForPrompt } from './agent-prompt-builder';\nimport { SourceAgent } from './source-agent';\nimport { runScript } from '../scripts/script-runner';\nimport { ScriptStore } from '../scripts/script-store';\nimport type { ScriptParameter, ScriptResult } from '../scripts/types';\nimport { TOOL_TRACKING_SAMPLE_ROWS, MAX_ROWS_RENDERED, MAX_ROWS_FETCHED } from '../constants';\n\n// ============================================\n// Script-writing tool schemas (exposed to the main-agent LLM)\n// ============================================\n\nconst WRITE_SCRIPT_TOOL_DEF = {\n\tname: 'write_script',\n\tdescription:\n\t\t'Save a draft script that reproduces the answer to the user question. You MUST call this after every successful source query and BEFORE writing your final analysis — the final answer must come from the script\\'s output, not from the source tool result directly. Even passthrough scripts (just ctx.query + return) are correct and required: they get saved for future matching. ' +\n\t\t'The scriptBody must start with `export async function getData(ctx, params)` — it runs in a tsx subprocess with ctx.query (authorized queries), ctx.emit (register computed datasets), ctx.log (stream progress), and ctx.now. ' +\n\t\t'Any query SQL used here MUST match a SQL that already succeeded via a source tool in this turn.',\n\tinput_schema: {\n\t\ttype: 'object',\n\t\tproperties: {\n\t\t\tname: { type: 'string', description: 'Short, descriptive title (3–5 words).' },\n\t\t\tdescription: { type: 'string', description: 'One-sentence description of what this script retrieves.' },\n\t\t\ttags: { type: 'array', items: { type: 'string' }, description: '5–8 keywords for future matching.' },\n\t\t\tscriptBody: { type: 'string', description: 'Full TS script body starting with `export async function getData(ctx, params)`. No imports needed for ctx.query / ctx.emit / ctx.now.' },\n\t\t\tparameters: {\n\t\t\t\ttype: 'array',\n\t\t\t\tdescription: 'Declared parameters the script consumes.',\n\t\t\t\titems: {\n\t\t\t\t\ttype: 'object',\n\t\t\t\t\tproperties: {\n\t\t\t\t\t\tname: { type: 'string' },\n\t\t\t\t\t\ttype: { type: 'string', enum: ['string', 'number', 'date', 'date_range', 'enum', 'boolean'] },\n\t\t\t\t\t\trequired: { type: 'boolean' },\n\t\t\t\t\t\tdefault: {},\n\t\t\t\t\t\tdescription: { type: 'string' },\n\t\t\t\t\t},\n\t\t\t\t\trequired: ['name', 'type', 'description'],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\trequired: ['name', 'description', 'scriptBody'],\n\t},\n};\n\nconst EXECUTE_SCRIPT_TOOL_DEF = {\n\tname: 'execute_script',\n\tdescription:\n\t\t'Run the current draft script in a subprocess and return { ok, sampleRows, executedQueries, error?, errorPhase? }. ' +\n\t\t'Call this after write_script. If ok is false, fix the draft with a fresh write_script (max 3 attempts per turn). ' +\n\t\t'If ok is true AND the sample rows answer the user question, proceed to your final analysis.',\n\tinput_schema: {\n\t\ttype: 'object',\n\t\tproperties: {},\n\t},\n};\n\nconst MAX_SCRIPT_ATTEMPTS = 3;\n\n/**\n * Per-turn draft state. The script body itself lives on disk in\n * `scripts-store/<slug>-<turnId>.ts` from the moment `write_script` fires —\n * this state is just the bookkeeping needed to find it again on\n * `execute_script` and to surface the verified result to the caller.\n */\ninterface ScriptState {\n\t/** Recipe id of the on-disk draft. Stable across retries within the turn. */\n\trecipeId: string | null;\n\tattempts: number;\n\tlastSuccessfulResult: ScriptResult | null;\n}\n\n// ============================================\n// Main Agent\n// ============================================\n\nexport class MainAgent {\n\tprivate externalTools: ExternalTool[];\n\tprivate workflows: WorkflowDescriptor[];\n\tprivate config: AgentConfig;\n\tprivate streamBuffer: StreamBuffer;\n\t/**\n\t * Optional: when provided, MainAgent exposes the `write_script` /\n\t * `execute_script` tools to the LLM and persists drafts to disk via the\n\t * store. Headless callers (alert analyzer, metric resolver) omit these to\n\t * suppress script authoring entirely — drafts would otherwise leak onto\n\t * disk with no caller to promote or clean them up.\n\t */\n\tprivate scriptStore: ScriptStore | null;\n\tprivate turnId: string | null;\n\tprivate createdFromPrompt: string = '';\n\tprivate scriptState: ScriptState = { recipeId: null, attempts: 0, lastSuccessfulResult: null };\n\n\tconstructor(\n\t\texternalTools: ExternalTool[],\n\t\tconfig: AgentConfig,\n\t\tscriptStore?: ScriptStore,\n\t\tturnId?: string,\n\t\tstreamBuffer?: StreamBuffer,\n\t\tworkflows: WorkflowDescriptor[] = [],\n\t) {\n\t\tthis.externalTools = externalTools;\n\t\tthis.workflows = workflows;\n\t\tthis.config = config;\n\t\tthis.streamBuffer = streamBuffer || new StreamBuffer();\n\t\tthis.scriptStore = scriptStore ?? null;\n\t\tthis.turnId = turnId ?? null;\n\t}\n\n\tprivate get scriptingEnabled(): boolean {\n\t\treturn this.scriptStore !== null && this.turnId !== null;\n\t}\n\n\t/**\n\t * Handle a user question using the multi-agent system.\n\t *\n\t * This is ONE LLM.streamWithTools() call. The LLM:\n\t * 1. Sees source summaries + direct tool descriptions in system prompt\n\t * 2. Decides which tool(s) to call (routing)\n\t * 3. Source tools → SourceAgent runs independently → returns data\n\t * 4. Direct tools → fn() called directly with LLM params → returns data\n\t * 5. Generates final analysis text\n\t */\n\tasync handleQuestion(\n\t\tuserPrompt: string,\n\t\tapiKey?: string,\n\t\tconversationHistory?: string,\n\t\tstreamCallback?: (chunk: string) => void,\n\t): Promise<AgentResponse> {\n\t\tconst startTime = Date.now();\n\t\tlogger.info(`[MainAgent] Starting | prompt: \"${userPrompt.substring(0, 50)}...\"`);\n\n\t\t// Reset per-turn script state so a new question starts fresh\n\t\tthis.scriptState = { recipeId: null, attempts: 0, lastSuccessfulResult: null };\n\t\tthis.createdFromPrompt = userPrompt;\n\n\t\t// Separate source tools from direct tools\n\t\tconst sourceTools = this.externalTools.filter(t => (t as any).toolType !== 'direct');\n\t\tconst directTools = this.externalTools.filter(t => (t as any).toolType === 'direct');\n\n\t\tlogger.info(`[MainAgent] ${sourceTools.length} source tool(s), ${directTools.length} direct tool(s), ${this.workflows.length} workflow(s)`);\n\n\t\t// Build source summaries for the system prompt\n\t\tconst summaries = buildSourceSummaries(sourceTools);\n\n\t\t// Build the system prompt with summaries + direct tool descriptions + workflow descriptions\n\t\tconst systemPrompt = await this.buildSystemPrompt(summaries, directTools, this.workflows, conversationHistory);\n\n\t\tlogger.logLLMPrompt('mainAgent', 'system', extractPromptText(systemPrompt));\n\t\tlogger.logLLMPrompt('mainAgent', 'user', userPrompt);\n\n\t\t// Build LLM tool definitions — source tools + direct tools + workflow tools + script-authoring tools\n\t\tconst sourceToolDefs = this.buildSourceToolDefinitions(summaries);\n\t\tconst directToolDefs = this.buildDirectToolDefinitions(directTools);\n\t\tconst workflowToolDefs = this.buildWorkflowToolDefinitions(this.workflows);\n\t\tconst tools = [\n\t\t\t...sourceToolDefs,\n\t\t\t...directToolDefs,\n\t\t\t...workflowToolDefs,\n\t\t\t...(this.scriptingEnabled ? [WRITE_SCRIPT_TOOL_DEF, EXECUTE_SCRIPT_TOOL_DEF] : []),\n\t\t];\n\n\t\t// Track all results for component generation\n\t\tconst sourceResults: SourceAgentResult[] = [];\n\t\tconst executedTools: ExecutedToolInfo[] = [];\n\t\tlet sourceCallCounter = 0;\n\t\t// Captured when the LLM picks a workflow tool. Short-circuits the response\n\t\t// downstream — we skip analysis text and component generation.\n\t\tlet selectedWorkflow: SelectedWorkflow | undefined;\n\n\t\t// Tool handler — routes to SourceAgent, direct execution, workflow capture, or script-authoring\n\t\tconst toolHandler = async (toolName: string, toolInput: any): Promise<string> => {\n\t\t\t// Workflow lookup first — workflows have priority over tools with the same id\n\t\t\tconst workflow = this.workflows.find(w => w.id === toolName);\n\t\t\tif (workflow) {\n\t\t\t\treturn this.handleWorkflow(workflow, toolInput, w => { selectedWorkflow = w; });\n\t\t\t}\n\t\t\tif (toolName === 'write_script') {\n\t\t\t\tif (!this.scriptingEnabled) return 'Scripting is not enabled for this request.';\n\t\t\t\treturn this.handleWriteScript(toolInput);\n\t\t\t}\n\t\t\tif (toolName === 'execute_script') {\n\t\t\t\tif (!this.scriptingEnabled) return 'Scripting is not enabled for this request.';\n\t\t\t\treturn this.handleExecuteScript();\n\t\t\t}\n\n\t\t\tconst externalTool = this.externalTools.find(t => t.id === toolName);\n\t\t\tif (!externalTool) {\n\t\t\t\tlogger.error(`[MainAgent] Unknown tool called: ${toolName}`);\n\t\t\t\treturn `Error: Unknown tool \"${toolName}\"`;\n\t\t\t}\n\n\t\t\t// Direct tool — call fn() directly with LLM-provided params\n\t\t\tif ((externalTool as any).toolType === 'direct') {\n\t\t\t\treturn this.handleDirectTool(externalTool, toolInput, executedTools);\n\t\t\t}\n\n\t\t\t// Source tool — dispatch to independent SourceAgent\n\t\t\tconst sourceInput: SourceToolInput = {\n\t\t\t\tintent: toolInput.intent || toolInput.query || JSON.stringify(toolInput),\n\t\t\t\taggregation: toolInput.aggregation || 'raw',\n\t\t\t};\n\n\t\t\tlogger.info(`[MainAgent] Dispatching SourceAgent for \"${externalTool.name}\" | intent: \"${sourceInput.intent}\"`);\n\n\t\t\t// Pre-resolve schema via embedding search when available.\n\t\t\t// Large/very_large sources benefit the most — avoids LLM-driven search_schema iterations.\n\t\t\tconst preResolved = await this.preResolveSchema(externalTool, sourceInput.intent);\n\n\t\t\t// Create a tagged stream buffer for this specific source agent call.\n\t\t\t// Each call gets a unique ID so the frontend can group interleaved\n\t\t\t// messages from parallel source agents into separate blocks.\n\t\t\tsourceCallCounter++;\n\t\t\tconst callId = `src_${sourceCallCounter}`;\n\t\t\tconst taggedBuffer = new TaggedStreamBuffer(\n\t\t\t\tcallId,\n\t\t\t\tthis.streamBuffer,\n\t\t\t\texternalTool.name,\n\t\t\t\tsourceInput.intent\n\t\t\t);\n\n\t\t\tconst sourceAgent = new SourceAgent(\n\t\t\t\texternalTool,\n\t\t\t\tthis.config,\n\t\t\t\ttaggedBuffer,\n\t\t\t\tpreResolved\n\t\t\t\t\t? { preResolvedSchema: preResolved, skipSchemaSearch: true }\n\t\t\t\t\t: undefined,\n\t\t\t);\n\t\t\tconst result = await sourceAgent.execute(sourceInput);\n\n\t\t\tsourceResults.push(result);\n\t\t\tif (result.success) {\n\t\t\t\t// Push ALL successful queries (primary + follow-up) so component generator\n\t\t\t\t// has the SQL and sample data from every query, not just the last one.\n\t\t\t\tif (result.allExecutedTools && result.allExecutedTools.length > 0) {\n\t\t\t\t\texecutedTools.push(...result.allExecutedTools);\n\t\t\t\t} else {\n\t\t\t\t\texecutedTools.push(result.executedTool);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn this.formatResultForMainAgent(result);\n\t\t};\n\n\t\t// ONE LLM.streamWithTools() call — the LLM handles everything\n\t\tconst text = await LLM.streamWithTools(\n\t\t\t{\n\t\t\t\tsys: systemPrompt,\n\t\t\t\tuser: userPrompt,\n\t\t\t},\n\t\t\ttools,\n\t\t\ttoolHandler,\n\t\t\t{\n\t\t\t\tmodel: this.config.mainAgentModel || undefined,\n\t\t\t\tmaxTokens: 4000,\n\t\t\t\ttemperature: 0,\n\t\t\t\tapiKey: apiKey || this.config.apiKey,\n\t\t\t\tpartial: streamCallback,\n\t\t\t},\n\t\t\tthis.config.maxIterations\n\t\t);\n\n\t\tconst totalTime = Date.now() - startTime;\n\t\tlogger.info(\n\t\t\t`[MainAgent] Complete | ${sourceResults.length} source queries, ${executedTools.length} successful${selectedWorkflow ? ` | workflow=\"${selectedWorkflow.name}\"` : ''} | ${totalTime}ms`,\n\t\t);\n\n\t\tconst savedScript = await this.buildSavedScript();\n\t\tif (savedScript) {\n\t\t\tlogger.info(`[MainAgent] Script authored: \"${savedScript.name}\" (${savedScript.parameters.length} params, ${this.scriptState.attempts} attempt${this.scriptState.attempts === 1 ? '' : 's'})`);\n\t\t} else if (sourceResults.some(r => r.success)) {\n\t\t\tlogger.warn(`[MainAgent] Source query succeeded but no script was authored — LLM skipped write_script/execute_script. Prompt policy may need tightening.`);\n\t\t}\n\n\t\t// Immediate cleanup of a failed draft (#5). If the agent authored a draft\n\t\t// this turn but it never verified (no savedScript → never promoted), discard\n\t\t// it now — the row + on-disk body. Failed drafts are never matched\n\t\t// (status='verified' only), so nothing of value is lost, and we avoid\n\t\t// unbounded draft accumulation + squatted file_base slug slots. Successful\n\t\t// drafts are promoted to verified by the caller, so we never touch those.\n\t\t// NOTE: this runs once at end-of-turn, NOT on each execute_script failure —\n\t\t// intra-turn retries reuse the same draft, so deleting mid-loop would break them.\n\t\tif (!savedScript && this.scriptStore && this.scriptState.recipeId) {\n\t\t\ttry {\n\t\t\t\tawait this.scriptStore.discardDraft(this.scriptState.recipeId);\n\t\t\t} catch (err) {\n\t\t\t\tlogger.warn(`[MainAgent] Failed to discard failed draft ${this.scriptState.recipeId}: ${err}`);\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\ttext,\n\t\t\texecutedTools,\n\t\t\tsourceResults,\n\t\t\tsavedScript,\n\t\t\tworkflow: selectedWorkflow,\n\t\t};\n\t}\n\n\t// ============================================\n\t// Script-authoring tool handlers\n\t// ============================================\n\n\tprivate async handleWriteScript(toolInput: any): Promise<string> {\n\t\tconst scriptBody = typeof toolInput?.scriptBody === 'string' ? toolInput.scriptBody.trim() : '';\n\t\tif (!scriptBody) {\n\t\t\treturn 'write_script requires a non-empty `scriptBody` starting with `export async function getData(ctx, params)`.';\n\t\t}\n\t\tconst hasGetDataExport =\n\t\t\t/\\bexport\\s+(async\\s+)?function\\s+getData\\b/.test(scriptBody) ||\n\t\t\t/\\bexport\\s+default\\s+(async\\s+)?function\\b/.test(scriptBody);\n\t\tif (!hasGetDataExport) {\n\t\t\treturn \"write_script rejected: scriptBody does not export `getData`. The script MUST contain `export async function getData(ctx, params) { ... }` as a named export — the subprocess runner imports this function by name. Your current scriptBody is missing the `export` keyword or uses a non-function export (const, class, module.exports). Rewrite the body with exactly: `export async function getData(ctx, params) { ... }`.\";\n\t\t}\n\n\t\tconst name = toolInput.name || 'Untitled Script';\n\t\tconst intentDescription = toolInput.description || '';\n\t\tconst tags = Array.isArray(toolInput.tags) ? toolInput.tags : [];\n\t\tconst parameters = Array.isArray(toolInput.parameters)\n\t\t\t? this.normalizeParameterList(toolInput.parameters)\n\t\t\t: [];\n\n\t\t// Persist the draft to disk immediately so it's visible in the IDE.\n\t\t// On retries within the same turn we pass the existing recipeId so the\n\t\t// store overwrites the same file rather than minting a new one.\n\t\t// scriptingEnabled is checked upstream — store/turnId are non-null here.\n\t\tconst draft = await this.scriptStore!.saveDraft({\n\t\t\trecipeId: this.scriptState.recipeId ?? undefined,\n\t\t\tturnId: this.turnId!,\n\t\t\tname,\n\t\t\tintentDescription,\n\t\t\ttags,\n\t\t\tparameters,\n\t\t\tscriptBody,\n\t\t\tcreatedFrom: this.createdFromPrompt,\n\t\t});\n\t\tthis.scriptState.recipeId = draft.id;\n\t\t// A new draft invalidates any previous successful run — it must be re-executed.\n\t\tthis.scriptState.lastSuccessfulResult = null;\n\n\t\tlogger.info(\n\t\t\t`[MainAgent] write_script: draft \"${draft.name}\" persisted to disk (${draft.parameters.length} params, recipeId=${draft.id})`,\n\t\t);\n\t\treturn `Draft \"${draft.name}\" saved to disk. Call execute_script to verify it works.`;\n\t}\n\n\tprivate async handleExecuteScript(): Promise<string> {\n\t\tif (!this.scriptState.recipeId) {\n\t\t\treturn 'No draft found. Call write_script first with the script you want to verify.';\n\t\t}\n\n\t\tconst draftRecipe = await this.scriptStore!.get(this.scriptState.recipeId);\n\t\tif (!draftRecipe) {\n\t\t\tlogger.error(`[MainAgent] execute_script: draft \"${this.scriptState.recipeId}\" missing from store`);\n\t\t\treturn 'Draft was lost from the store. Call write_script again.';\n\t\t}\n\n\t\tif (this.scriptState.attempts >= MAX_SCRIPT_ATTEMPTS) {\n\t\t\treturn `Reached the max of ${MAX_SCRIPT_ATTEMPTS} execute_script attempts for this turn. Stop calling execute_script and answer the user directly from the source data you already have.`;\n\t\t}\n\t\tthis.scriptState.attempts++;\n\n\t\t// Subprocess reads the on-disk draft path directly — no temp file dance.\n\t\tconst scriptPath = this.scriptStore!.getScriptPath(draftRecipe);\n\n\t\tconst result: ScriptResult = await runScript(\n\t\t\tdraftRecipe,\n\t\t\tscriptPath,\n\t\t\t{},\n\t\t\t{ externalTools: this.externalTools, streamBuffer: this.streamBuffer },\n\t\t);\n\n\t\tif (result.success) {\n\t\t\tthis.scriptState.lastSuccessfulResult = result;\n\t\t\tconst totalRows = result.data.length;\n\t\t\tconst summary = {\n\t\t\t\tok: true,\n\t\t\t\texecutionTimeMs: result.executionTimeMs,\n\t\t\t\ttotalRows,\n\t\t\t\t// dataSummary is computed over ALL rows — use it for any range/count/\n\t\t\t\t// min/max/total/\"all-or-none\" claim. sampleRows is just the first few\n\t\t\t\t// rows (head of result, NOT representative) for illustrating shape.\n\t\t\t\tdataSummary: summarizeRows(result.data),\n\t\t\t\tsampleRows: result.data.slice(0, 5),\n\t\t\t\tanalysisGuidance:\n\t\t\t\t\t`Showing ${Math.min(totalRows, 5)} of ${totalRows} rows. The sample is the ` +\n\t\t\t\t\t`HEAD of the result and is NOT representative — do not generalize from it. ` +\n\t\t\t\t\t`Base every number, range, count, and \"all/none\" statement on dataSummary, ` +\n\t\t\t\t\t`which is computed over all ${totalRows} rows. The full result is rendered ` +\n\t\t\t\t\t`for the user by the on-screen component.`,\n\t\t\t\texecutedQueries: result.executedQueries.map(q => ({\n\t\t\t\t\tsourceId: q.sourceId,\n\t\t\t\t\tsourceName: q.sourceName,\n\t\t\t\t\trows: q.count,\n\t\t\t\t\tsample: q.data.slice(0, 2),\n\t\t\t\t})),\n\t\t\t};\n\t\t\tlogger.info(`[MainAgent] execute_script: ok — ${totalRows} rows in ${result.executionTimeMs}ms (attempt ${this.scriptState.attempts})`);\n\t\t\treturn JSON.stringify(summary, null, 2);\n\t\t}\n\n\t\t// Stamp the error onto the draft metadata so users can read it without grepping logs.\n\t\tawait this.scriptStore!.recordDraftError(draftRecipe.id, {\n\t\t\tphase: (result.errorPhase ?? 'runtime'),\n\t\t\tmessage: result.error ?? 'Unknown error',\n\t\t\tattempt: this.scriptState.attempts,\n\t\t});\n\n\t\tconst attemptsRemaining = MAX_SCRIPT_ATTEMPTS - this.scriptState.attempts;\n\t\tconst summary = {\n\t\t\tok: false,\n\t\t\terrorPhase: result.errorPhase,\n\t\t\terror: result.error,\n\t\t\tattemptsRemaining,\n\t\t\texecutedQueries: result.executedQueries.map(q => ({\n\t\t\t\tsourceId: q.sourceId,\n\t\t\t\tsourceName: q.sourceName,\n\t\t\t\trows: q.count,\n\t\t\t})),\n\t\t\thint:\n\t\t\t\tattemptsRemaining > 0\n\t\t\t\t\t? 'Call write_script with a corrected draft, then execute_script again. If you cannot fix it, stop and answer from the source data you already have.'\n\t\t\t\t\t: 'No attempts remaining — stop calling execute_script and answer the user directly.',\n\t\t};\n\t\tlogger.warn(`[MainAgent] execute_script: failed [${result.errorPhase}] — ${result.error} (attempt ${this.scriptState.attempts}/${MAX_SCRIPT_ATTEMPTS})`);\n\t\treturn JSON.stringify(summary, null, 2);\n\t}\n\n\t/**\n\t * Build the AgentWrittenScript payload the caller will hand to\n\t * `ScriptStore.promoteToVerified()`. Only returned when a verified\n\t * successful execution is on record.\n\t */\n\tprivate async buildSavedScript(): Promise<AgentWrittenScript | undefined> {\n\t\tconst { recipeId, lastSuccessfulResult } = this.scriptState;\n\t\tif (!recipeId || !lastSuccessfulResult) return undefined;\n\n\t\tif (!this.scriptStore) return undefined;\n\t\tconst draft = await this.scriptStore.get(recipeId);\n\t\tif (!draft) return undefined;\n\n\t\tconst executedQueries = lastSuccessfulResult.executedQueries.map(q => ({\n\t\t\tsourceId: q.sourceId,\n\t\t\tsourceName: q.sourceName,\n\t\t\tsql: q.sql,\n\t\t\tdata: q.data,\n\t\t\tcount: q.count,\n\t\t\ttotalCount: q.totalCount,\n\t\t\texecutionTimeMs: q.executionTimeMs,\n\t\t\tvirtual: q.virtual,\n\t\t}));\n\t\tconst sourceIds = Array.from(new Set(executedQueries.map(q => q.sourceId).filter(id => !id.startsWith('computed:'))));\n\t\tconst tables = extractTablesFromSQL(executedQueries.map(q => q.sql));\n\n\t\treturn {\n\t\t\trecipeId: draft.id,\n\t\t\tname: draft.name,\n\t\t\tintentDescription: draft.intentDescription,\n\t\t\ttags: draft.tags,\n\t\t\tparameters: draft.parameters,\n\t\t\tscriptBody: draft.scriptBody,\n\t\t\tsourceIds,\n\t\t\ttables,\n\t\t\texecutedQueries,\n\t\t};\n\t}\n\n\tprivate normalizeParameterList(input: any[]): ScriptParameter[] {\n\t\treturn input\n\t\t\t.filter(p => p && typeof p === 'object' && typeof p.name === 'string')\n\t\t\t.map(p => ({\n\t\t\t\tname: p.name,\n\t\t\t\ttype: (p.type || 'string') as ScriptParameter['type'],\n\t\t\t\trequired: Boolean(p.required),\n\t\t\t\tdefault: p.default,\n\t\t\t\tenumValues: p.enumValues,\n\t\t\t\tdescription: typeof p.description === 'string' ? p.description : '',\n\t\t\t}));\n\t}\n\n\t// ============================================\n\t// Pre-Resolved Schema (embedding search)\n\t// ============================================\n\n\t/**\n\t * Use the schema embedding collection to pre-select relevant tables for\n\t * this source + intent. Returns a formatted schema block if confidence is\n\t * high (top match ≥ 0.55 and ≥3 candidates), otherwise null.\n\t *\n\t * When this returns a block, we can skip the SourceAgent's `search_schema`\n\t * loop and reduce iteration budget. When it returns null, the SourceAgent\n\t * falls back to the existing LLM-driven keyword search (same as today).\n\t */\n\tprivate async preResolveSchema(\n\t\texternalTool: ExternalTool,\n\t\tintent: string,\n\t): Promise<string | null> {\n\t\tconst collections = this.config.collections;\n\t\tconst search = collections?.['schema-embeddings']?.['search'];\n\t\tif (!search) return null;\n\n\t\t// Only worth doing for large/very_large tiers — small/medium already fit in prompt.\n\t\tconst tier = (externalTool as any).schemaTier;\n\t\tif (tier !== 'large' && tier !== 'very_large') return null;\n\n\t\ttry {\n\t\t\t// Tool ID has a `_query` / `_read` / `_call` suffix — strip it to get the real source ID,\n\t\t\t// which is the field schema embeddings are stored under.\n\t\t\tconst sourceId = externalTool.id.replace(/_(query|read|call)$/, '');\n\t\t\tconst res = await search({\n\t\t\t\tquery: intent,\n\t\t\t\tsourceId,\n\t\t\t\tprojectId: this.config.projectId,\n\t\t\t\ttopK: 10,\n\t\t\t});\n\t\t\tconst results = res?.results || [];\n\t\t\tif (!Array.isArray(results) || results.length === 0) {\n\t\t\t\tlogger.info(`[MainAgent] Schema embedding returned 0 results for ${externalTool.name} (sourceId=${sourceId}) — falling back to LLM search_schema`);\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst top = results[0];\n\t\t\t// Similarity = 1/(1+L2distance) — typical MiniLM range 0.3-0.7 for relevant matches.\n\t\t\t// 0.4 is a loose bar that still filters irrelevant domains.\n\t\t\tif (!top || top.similarity < 0.4) {\n\t\t\t\tlogger.info(`[MainAgent] Schema embedding low confidence (top=${top?.similarity?.toFixed(2)}) — falling back to LLM search_schema`);\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\tconst highConfidence = results.filter((r: any) => r.similarity >= 0.35);\n\t\t\tif (highConfidence.length < 3) {\n\t\t\t\tlogger.info(`[MainAgent] Schema embedding too few relevant tables (${highConfidence.length} >= 0.35) — falling back to LLM search_schema`);\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst lines = highConfidence.slice(0, 10).map((r: any) =>\n\t\t\t\t`• ${r.description} [similarity: ${r.similarity.toFixed(2)}]`\n\t\t\t);\n\t\t\tconst header = `[Pre-resolved via embedding search — ${highConfidence.length} most relevant tables for this intent.]`;\n\t\t\tlogger.info(`[MainAgent] Schema embedding pre-resolved ${highConfidence.length} tables for ${externalTool.name} (top=${top.similarity.toFixed(2)})`);\n\t\t\treturn `${header}\\n${lines.join('\\n')}`;\n\t\t} catch (err) {\n\t\t\tconst msg = err instanceof Error ? err.message : String(err);\n\t\t\tlogger.warn(`[MainAgent] Schema embedding search failed (non-blocking): ${msg}`);\n\t\t\treturn null;\n\t\t}\n\t}\n\n\t// ============================================\n\t// Direct Tool Execution\n\t// ============================================\n\n\t/**\n\t * Execute a direct tool — call fn() with LLM-provided params, no SourceAgent.\n\t */\n\tprivate async handleDirectTool(\n\t\ttool: ExternalTool,\n\t\ttoolInput: any,\n\t\texecutedTools: ExecutedToolInfo[]\n\t): Promise<string> {\n\t\tconst startTime = Date.now();\n\t\tlogger.info(`[MainAgent] Executing direct tool \"${tool.name}\" | params: ${JSON.stringify(toolInput).substring(0, 200)}`);\n\n\t\tif (this.streamBuffer.hasCallback()) {\n\t\t\tthis.streamBuffer.write(`\\n\\n🔧 **Running ${tool.name}...**\\n\\n`);\n\t\t\tawait streamDelay();\n\t\t}\n\n\t\ttry {\n\t\t\tconst result = await tool.fn(toolInput);\n\t\t\tconst executionTimeMs = Date.now() - startTime;\n\n\t\t\t// Check for error in result\n\t\t\tif (result && result.error) {\n\t\t\t\tconst errorMsg = typeof result.error === 'string' ? result.error : JSON.stringify(result.error);\n\t\t\t\tlogger.warn(`[MainAgent] Direct tool \"${tool.name}\" returned error: ${errorMsg}`);\n\t\t\t\treturn `❌ Tool \"${tool.name}\" error: ${errorMsg}`;\n\t\t\t}\n\n\t\t\tconst resultData = result.data || result;\n\t\t\tconst rowCount = Array.isArray(resultData) ? resultData.length : 1;\n\n\t\t\tif (this.streamBuffer.hasCallback()) {\n\t\t\t\tthis.streamBuffer.write(`✅ **${tool.name}** completed (${rowCount} results, ${executionTimeMs}ms)\\n\\n`);\n\t\t\t}\n\n\t\t\t// Track for component generation\n\t\t\tconst formattedResult = formatToolResultForLLM(result, {\n\t\t\t\ttoolName: tool.name,\n\t\t\t\tmaxRows: 5,\n\t\t\t\tmaxCharsPerField: 200,\n\t\t\t});\n\n\t\t\t// _sampleData feeds the component-generator prompt. Use the already-truncated\n\t\t\t// formattedResult.data so a tool returning a non-array object with large nested\n\t\t\t// arrays cannot blow the prompt window. Falls back to slicing the raw resultData\n\t\t\t// only when it is itself an array.\n\t\t\tconst sampleData: unknown[] = Array.isArray(resultData)\n\t\t\t\t? resultData.slice(0, TOOL_TRACKING_SAMPLE_ROWS)\n\t\t\t\t: (Array.isArray(formattedResult.data) ? formattedResult.data.slice(0, TOOL_TRACKING_SAMPLE_ROWS) : []);\n\n\t\t\texecutedTools.push({\n\t\t\t\tid: tool.id,\n\t\t\t\tname: tool.name,\n\t\t\t\tparams: toolInput,\n\t\t\t\tresult: {\n\t\t\t\t\t_totalRecords: result.totalItems || result.count || rowCount,\n\t\t\t\t\t_recordsShown: rowCount,\n\t\t\t\t\t_metadata: result.metadata,\n\t\t\t\t\t_sampleData: sampleData,\n\t\t\t\t},\n\t\t\t\toutputSchema: tool.outputSchema,\n\t\t\t});\n\n\t\t\tconst formatted = typeof formattedResult === 'string' ? formattedResult : JSON.stringify(formattedResult);\n\t\t\treturn `✅ Tool \"${tool.name}\" executed successfully. ${rowCount} results returned.\\n\\n${formatted}`;\n\n\t\t} catch (error) {\n\t\t\tconst executionTimeMs = Date.now() - startTime;\n\t\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\t\t\tlogger.error(`[MainAgent] Direct tool \"${tool.name}\" failed in ${executionTimeMs}ms: ${errorMsg}`);\n\n\t\t\tif (this.streamBuffer.hasCallback()) {\n\t\t\t\tthis.streamBuffer.write(`\\n\\n❌ **${tool.name} failed:** ${errorMsg}\\n\\n`);\n\t\t\t}\n\n\t\t\treturn `❌ Tool \"${tool.name}\" failed: ${errorMsg}`;\n\t\t}\n\t}\n\n\t// ============================================\n\t// System Prompt\n\t// ============================================\n\n\t/**\n\t * Build the main agent's system prompt with source summaries, direct tool descriptions,\n\t * and workflow component descriptions.\n\t */\n\tprivate async buildSystemPrompt(\n\t\tsummaries: SourceSummary[],\n\t\tdirectTools: ExternalTool[],\n\t\tworkflows: WorkflowDescriptor[],\n\t\tconversationHistory?: string\n\t): Promise<string | any[]> {\n\t\tconst summariesText = formatSummariesForPrompt(summaries);\n\n\t\t// Tool call budget: reserve 2 iterations for the final analysis response\n\t\tconst maxSourceCalls = Math.max(2, this.config.maxIterations - 2);\n\n\t\t// Build direct tools section for system prompt\n\t\tlet directToolsText = '';\n\t\tif (directTools.length > 0) {\n\t\t\tdirectToolsText = directTools.map((t, idx) => {\n\t\t\t\tconst params = (t as any).params || {};\n\t\t\t\tconst paramList = Object.entries(params).map(([k, v]) => ` - ${k}: ${v}`).join('\\n');\n\t\t\t\treturn `${idx + 1}. **${t.name}** (tool: ${t.id})\\n ${t.description || 'No description'}${paramList ? '\\n Parameters:\\n' + paramList : ''}`;\n\t\t\t}).join('\\n\\n');\n\t\t}\n\n\t\t// Build workflows section for system prompt\n\t\tlet workflowsText = '';\n\t\tif (workflows.length > 0) {\n\t\t\tworkflowsText = workflows.map((w, idx) => {\n\t\t\t\tconst propLines = Object.entries(w.propsSchema || {})\n\t\t\t\t\t.map(([k, v]) => ` - ${k}: ${v}`)\n\t\t\t\t\t.join('\\n');\n\t\t\t\treturn [\n\t\t\t\t\t`${idx + 1}. **${w.name}** (tool: ${w.id})`,\n\t\t\t\t\t` ${w.description}`,\n\t\t\t\t\t` When to use: ${w.whenToUse}`,\n\t\t\t\t\tpropLines ? ` Props:\\n${propLines}` : '',\n\t\t\t\t]\n\t\t\t\t\t.filter(Boolean)\n\t\t\t\t\t.join('\\n');\n\t\t\t}).join('\\n\\n');\n\t\t} else {\n\t\t\tworkflowsText = 'No workflow components registered for this project.';\n\t\t}\n\n\t\tconst prompts = await promptLoader.loadPrompts('agent-main', {\n\t\t\tSOURCE_SUMMARIES: summariesText,\n\t\t\tDIRECT_TOOLS: directToolsText,\n\t\t\tWORKFLOW_COMPONENTS: workflowsText,\n\t\t\tMAX_ROWS: String(MAIN_AGENT_COMPLETE_ROWS),\n\t\t\tMAX_ROWS_RENDERED: String(MAX_ROWS_RENDERED),\n\t\t\tMAX_ROWS_FETCHED: String(MAX_ROWS_FETCHED),\n\t\t\tMAX_SOURCE_CALLS: String(maxSourceCalls),\n\t\t\tGLOBAL_KNOWLEDGE_BASE: this.config.globalKnowledgeBase || 'No global knowledge base available.',\n\t\t\tCURRENT_DATETIME: getCurrentDateTimeForPrompt(),\n\t\t\tKNOWLEDGE_BASE_CONTEXT: this.config.knowledgeBaseContext || 'No additional knowledge base context available.',\n\t\t\tCONVERSATION_HISTORY: conversationHistory || 'No previous conversation',\n\t\t});\n\t\treturn prompts.system;\n\t}\n\n\t// ============================================\n\t// Tool Definitions\n\t// ============================================\n\n\t/**\n\t * Build tool definitions for source tools — summary-only descriptions.\n\t * The full schema is inside the SourceAgent which runs independently.\n\t */\n\tprivate buildSourceToolDefinitions(summaries: SourceSummary[]): any[] {\n\t\treturn summaries.map(summary => {\n\t\t\tconst totalRows = summary.entityDetails.reduce((sum, e) => sum + (e.rowCount || 0), 0);\n\t\t\tconst rowInfo = totalRows > 0 ? ` (~${totalRows.toLocaleString()} total rows)` : '';\n\n\t\t\tconst entitiesList = summary.entityDetails.length > 0\n\t\t\t\t? summary.entityDetails.map(e => {\n\t\t\t\t\tconst cols = e.columns.length > 0 ? ` [${e.columns.join(', ')}]` : '';\n\t\t\t\t\treturn `${e.name}${cols}`;\n\t\t\t\t}).join('; ')\n\t\t\t\t: 'no entities listed';\n\n\t\t\treturn {\n\t\t\t\tname: summary.toolId,\n\t\t\t\tdescription: `Query \"${summary.name}\" (${summary.type})${rowInfo}. ${summary.description}. Contains: ${entitiesList}.`,\n\t\t\t\tinput_schema: {\n\t\t\t\t\ttype: 'object',\n\t\t\t\t\tproperties: {\n\t\t\t\t\t\tintent: {\n\t\t\t\t\t\t\ttype: 'string',\n\t\t\t\t\t\t\tdescription: 'Describe what data you need from this source in natural language. Be specific about fields, filters, aggregations, and limits.',\n\t\t\t\t\t\t},\n\t\t\t\t\t\taggregation: {\n\t\t\t\t\t\t\ttype: 'string',\n\t\t\t\t\t\t\tenum: ['raw', 'pre-aggregate', 'summary'],\n\t\t\t\t\t\t\tdescription: 'How to return data. \"pre-aggregate\": use GROUP BY/SUM/COUNT for totals. \"summary\": high-level metrics. \"raw\": individual records.',\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\trequired: ['intent'],\n\t\t\t\t},\n\t\t\t};\n\t\t});\n\t}\n\n\t/**\n\t * Build tool definitions for direct tools — expose their actual params.\n\t * These are called directly by the main agent LLM, no SourceAgent.\n\t */\n\tprivate buildDirectToolDefinitions(directTools: ExternalTool[]): any[] {\n\t\treturn directTools.map(tool => {\n\t\t\tconst properties: Record<string, any> = {};\n\t\t\tconst required: string[] = [];\n\n\t\t\tconst toolParams: Record<string, any> = (tool as any).params || {};\n\t\t\tObject.entries(toolParams).forEach(([key, typeOrValue]) => {\n\t\t\t\tconst valueStr = String(typeOrValue).toLowerCase();\n\n\t\t\t\t// Extract type\n\t\t\t\tlet schemaType = 'string';\n\t\t\t\tconst typeMatch = valueStr.match(/^(string|number|integer|boolean|array|object)\\b/);\n\t\t\t\tif (typeMatch) {\n\t\t\t\t\tschemaType = typeMatch[1];\n\t\t\t\t}\n\n\t\t\t\t// Check if optional\n\t\t\t\tconst isOptional = valueStr.includes('(optional)') || valueStr.includes('optional');\n\n\t\t\t\tconst description = typeof typeOrValue === 'string' ? typeOrValue : `Parameter: ${key}`;\n\n\t\t\t\tif (schemaType === 'array') {\n\t\t\t\t\tproperties[key] = { type: 'array', items: { type: 'string' }, description };\n\t\t\t\t} else if (schemaType === 'object') {\n\t\t\t\t\tproperties[key] = { type: 'object', description };\n\t\t\t\t} else {\n\t\t\t\t\tproperties[key] = { type: schemaType, description };\n\t\t\t\t}\n\n\t\t\t\tif (!isOptional) {\n\t\t\t\t\trequired.push(key);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\tname: tool.id,\n\t\t\t\tdescription: tool.description || `Call ${tool.name}`,\n\t\t\t\tinput_schema: {\n\t\t\t\t\ttype: 'object',\n\t\t\t\t\tproperties,\n\t\t\t\t\trequired: required.length > 0 ? required : undefined,\n\t\t\t\t},\n\t\t\t};\n\t\t});\n\t}\n\n\t// ============================================\n\t// Workflow Handling\n\t// ============================================\n\n\t/**\n\t * Capture a workflow selection. We do NOT execute anything — the LLM has\n\t * already extracted the props it wants the workflow rendered with. We\n\t * record the selection (via the capture callback) and return a short\n\t * acknowledgement so the LLM ends its turn cleanly without writing\n\t * analysis text or calling more tools.\n\t */\n\tprivate async handleWorkflow(\n\t\tworkflow: WorkflowDescriptor,\n\t\ttoolInput: any,\n\t\tcapture: (selected: SelectedWorkflow) => void,\n\t): Promise<string> {\n\t\tconst props = { ...(workflow.defaultProps || {}), ...(toolInput || {}) };\n\t\tlogger.info(\n\t\t\t`[MainAgent] Workflow selected: \"${workflow.name}\" | props: ${JSON.stringify(props).substring(0, 200)}`,\n\t\t);\n\n\t\tif (this.streamBuffer.hasCallback()) {\n\t\t\tthis.streamBuffer.write(`\\n\\n🧭 **Launching ${workflow.name} workflow...**\\n\\n`);\n\t\t\tawait streamDelay();\n\t\t}\n\n\t\tcapture({ name: workflow.name, props });\n\n\t\t// Returning this string tells the main agent its job is done — the UI\n\t\t// will render the workflow component, no analysis text needed.\n\t\treturn `✅ Workflow \"${workflow.name}\" selected. The UI will render now — do NOT write analysis text or call any other tools. End your turn.`;\n\t}\n\n\t/**\n\t * Build LLM tool definitions for workflow components. The workflow's\n\t * propsSchema becomes the tool's input_schema so the LLM extracts props\n\t * directly from the prompt — same mechanic as direct tools.\n\t */\n\tprivate buildWorkflowToolDefinitions(workflows: WorkflowDescriptor[]): any[] {\n\t\treturn workflows.map(workflow => {\n\t\t\tconst properties: Record<string, any> = {};\n\t\t\tconst required: string[] = [];\n\n\t\t\tObject.entries(workflow.propsSchema || {}).forEach(([key, typeOrValue]) => {\n\t\t\t\tconst valueStr = String(typeOrValue).toLowerCase();\n\n\t\t\t\tlet schemaType = 'string';\n\t\t\t\tconst typeMatch = valueStr.match(/^(string|number|integer|boolean|array|object)\\b/);\n\t\t\t\tif (typeMatch) {\n\t\t\t\t\tschemaType = typeMatch[1];\n\t\t\t\t}\n\n\t\t\t\tconst isOptional = valueStr.includes('(optional)') || valueStr.includes('optional');\n\t\t\t\tconst description = typeof typeOrValue === 'string' ? typeOrValue : `Prop: ${key}`;\n\n\t\t\t\tif (schemaType === 'array') {\n\t\t\t\t\tproperties[key] = { type: 'array', items: {}, description };\n\t\t\t\t} else if (schemaType === 'object') {\n\t\t\t\t\tproperties[key] = { type: 'object', description };\n\t\t\t\t} else {\n\t\t\t\t\tproperties[key] = { type: schemaType, description };\n\t\t\t\t}\n\n\t\t\t\tif (!isOptional) required.push(key);\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\tname: workflow.id,\n\t\t\t\tdescription: `[WORKFLOW] ${workflow.description} — When to use: ${workflow.whenToUse}`,\n\t\t\t\tinput_schema: {\n\t\t\t\t\ttype: 'object',\n\t\t\t\t\tproperties,\n\t\t\t\t\trequired: required.length > 0 ? required : undefined,\n\t\t\t\t},\n\t\t\t};\n\t\t});\n\t}\n\n\t// ============================================\n\t// Format Result for Main Agent\n\t// ============================================\n\n\t/**\n\t * Format a source agent's result as a clean string for the main agent LLM.\n\t */\n\tprivate formatResultForMainAgent(result: SourceAgentResult): string {\n\t\tif (!result.success) {\n\t\t\treturn `Data source \"${result.sourceName}\" could not fulfill the request: ${result.error}. Try rephrasing your intent or querying a different source.`;\n\t\t}\n\n\t\tconst { metadata } = result;\n\n\t\tlet output = `## Data from \"${result.sourceName}\"\\n`;\n\t\toutput += `Rows returned: ${metadata.rowsReturned}`;\n\n\t\tif (metadata.isLimited) {\n\t\t\toutput += ` (LIMITED — ${metadata.totalRowsMatched} total matched, only ${metadata.rowsReturned} returned)`;\n\t\t}\n\t\toutput += `\\nExecution time: ${metadata.executionTimeMs}ms\\n\\n`;\n\n\t\t// Pair EVERY successful query's SQL with the rows IT returned, under one\n\t\t// `### Query N` header each. Two reasons this must be per-query:\n\t\t// 1. The main agent re-authors SQL at write_script time, so it needs the\n\t\t// proven SQL of every query verbatim — not just the last — or it\n\t\t// silently drops filters/scope (e.g. per-district benchmark coupling).\n\t\t// 2. A follow-up query overwrites `result.data` (source-agent keeps only\n\t\t// the last query's rows). Reading each query's own `_sampleData` here\n\t\t// means an earlier query's rows are never hidden by a later one, and\n\t\t// SQL ↔ data are never mixed up across queries.\n\t\tconst successfulTools = (result.allExecutedTools && result.allExecutedTools.length > 0)\n\t\t\t? result.allExecutedTools\n\t\t\t: (result.executedTool ? [result.executedTool] : []);\n\t\tconst queries = successfulTools\n\t\t\t.map(t => ({\n\t\t\t\tsql: (t?.params?.sql || t?.params?.query) as string | undefined,\n\t\t\t\trows: (t?.result?._mainAgentRows ?? t?.result?._sampleData ?? []) as Record<string, any>[],\n\t\t\t\tsummary: t?.result?._summary,\n\t\t\t\ttotal: t?.result?._totalRecords as number | undefined,\n\t\t\t\tshown: t?.result?._recordsShown as number | undefined,\n\t\t\t}))\n\t\t\t.filter(q => Boolean(q.sql));\n\n\t\tif (queries.length === 0) {\n\t\t\toutput += 'No data returned.';\n\t\t\treturn output;\n\t\t}\n\n\t\t// Field-level truncation so one wide text cell can't bloat the context.\n\t\tconst truncRows = (rowsArr: Record<string, any>[]) => rowsArr.map(row => {\n\t\t\tconst out: Record<string, any> = {};\n\t\t\tfor (const [key, value] of Object.entries(row)) {\n\t\t\t\t// Surrogate-aware truncation — splitting a UTF-16 pair here would\n\t\t\t\t// emit a lone surrogate and break the JSON request body.\n\t\t\t\tout[key] = (typeof value === 'string' && value.length > 200)\n\t\t\t\t\t? safeTruncate(value, 200) + '...'\n\t\t\t\t\t: value;\n\t\t\t}\n\t\t\treturn out;\n\t\t});\n\t\tconst jsonBlock = (obj: any) => {\n\t\t\ttry { return '```json\\n' + JSON.stringify(obj, null, 2) + '\\n```\\n'; }\n\t\t\tcatch { return '```\\n[Data could not be serialized]\\n```\\n'; }\n\t\t};\n\n\t\tconst scopeWarning = `PRESERVE every filter and scope — brand, product, date range, AND geography/segment (\\`State\\`/\\`Region\\`/\\`District\\`). Do NOT broaden, drop, or \"simplify\" any predicate.`;\n\t\toutput += queries.length === 1\n\t\t\t? `### The SQL below and its result — copy the SQL VERBATIM into \\`write_script\\` (inside \\`ctx.query(...)\\`)\\n${scopeWarning}\\n\\n`\n\t\t\t: `### ${queries.length} successful queries — each block pairs ONE query's SQL with ITS OWN result. Do NOT mix a query's SQL with another query's data.\\n${scopeWarning} The script re-runs them, so you can include more than one \\`ctx.query(...)\\`.\\n\\n`;\n\n\t\tqueries.forEach((q, i) => {\n\t\t\tconst total = q.total ?? q.rows.length;\n\t\t\t// A query that returned exactly the fetch cap was almost certainly\n\t\t\t// TRUNCATED — and if it was ORDER BY'd, the rows are one slice of the\n\t\t\t// data (e.g. all from the first district), NOT a representative sample.\n\t\t\t// Flag it loudly so the agent re-queries with aggregation instead of\n\t\t\t// concluding a group is absent / reading totals from a partial slice.\n\t\t\tconst capped = (q.shown ?? q.rows.length) >= MAX_ROWS_FETCHED;\n\t\t\toutput += queries.length === 1\n\t\t\t\t? `**SQL:**\\n`\n\t\t\t\t: `### Query ${i + 1} — ${capped ? `${MAX_ROWS_FETCHED}+ (TRUNCATED)` : `${total}`} rows\\n**SQL for Query ${i + 1}:**\\n`;\n\t\t\toutput += '```sql\\n' + String(q.sql) + '\\n```\\n';\n\t\t\tif (capped) {\n\t\t\t\toutput += `> ⚠️ **TRUNCATED, NON-REPRESENTATIVE SAMPLE.** This query hit the ${MAX_ROWS_FETCHED}-row exploration cap, so it returned only the FIRST ${MAX_ROWS_FETCHED} rows — and because it is \\`ORDER BY\\`'d, those rows are likely ALL from one group (e.g. one District/category), not a cross-section. Do NOT conclude any group \"has no data\" or read any count/total/min/max from this. To reason over all rows, re-query with **aggregation** (\\`GROUP BY\\` the dimension, e.g. \\`SELECT District, COUNT(*) ... GROUP BY District\\`) or compute the metric in SQL — never from this slice.\\n`;\n\t\t\t}\n\n\t\t\tif (total <= MAIN_AGENT_COMPLETE_ROWS) {\n\t\t\t\t// Small result → send it COMPLETE so the agent never generalizes\n\t\t\t\t// from a biased head-slice (this is what dimension/lookup tables hit).\n\t\t\t\tconst rows = truncRows(q.rows);\n\t\t\t\toutput += `**All ${rows.length} rows (COMPLETE result — this is the entire result set, nothing hidden):**\\n`;\n\t\t\t\toutput += jsonBlock(rows);\n\t\t\t} else {\n\t\t\t\t// Large result → a BOUNDED summary over ALL rows (complete structure:\n\t\t\t\t// every distinct category, per-group counts/avgs/sums, ranges) plus a\n\t\t\t\t// few shape-only samples. The full data is NOT sent — it reaches the UI\n\t\t\t\t// via the script's queryId, never the LLM context.\n\t\t\t\tconst samples = truncRows(q.rows.slice(0, 3));\n\t\t\t\toutput += `**Result summary — computed over ALL ${total} rows. Use THIS for every count, range, total, \"all/none\", and per-group number; do NOT infer them from the samples:**\\n`;\n\t\t\t\toutput += jsonBlock(q.summary ?? { totalRows: total });\n\t\t\t\toutput += `**Sample rows (first ${samples.length} of ${total} — to show row SHAPE only, NOT representative of the distribution):**\\n`;\n\t\t\t\toutput += jsonBlock(samples);\n\t\t\t}\n\t\t\toutput += '\\n';\n\t\t});\n\n\t\treturn output;\n\t}\n\n\t/**\n\t * Get source summaries (for external inspection/debugging).\n\t */\n\tgetSourceSummaries(): SourceSummary[] {\n\t\treturn buildSourceSummaries(this.externalTools);\n\t}\n}\n\n\n/**\n * Best-effort extraction of table names from a set of SQL statements.\n * Matches FROM/JOIN references, including schema-qualified names.\n */\nfunction extractTablesFromSQL(sqls: string[]): string[] {\n\tconst tables = new Set<string>();\n\tconst pattern = /(?:FROM|JOIN)\\s+\"?([a-zA-Z_][\\w.]*)\"?/gi;\n\tfor (const sql of sqls) {\n\t\tif (!sql) continue;\n\t\tlet match: RegExpExecArray | null;\n\t\twhile ((match = pattern.exec(sql)) !== null) {\n\t\t\tif (match[1]) tables.add(match[1]);\n\t\t}\n\t}\n\treturn Array.from(tables);\n}\n","/**\n * Multi-Agent Architecture Types\n *\n * Defines interfaces for the hierarchical agent system:\n * - Main Agent: ONE LLM.streamWithTools() call with source agent tools\n * - Source Agents: independent agents that query individual data sources\n *\n * The main agent sees only source summaries. When it calls a source tool,\n * the SourceAgent runs independently (own LLM, own retries) and returns clean data.\n */\n\nimport type { ExecutedToolInfo } from '../services/tool-executor-service';\nimport { MAX_ROWS_FETCHED } from '../constants';\n\n// ============================================\n// Source Summary (for routing decisions)\n// ============================================\n\n/**\n * Per-entity detail: name, row count, and column names.\n * Gives the main agent enough context to route to the right source.\n */\nexport interface EntityDetail {\n\t/** Entity name (table, sheet, endpoint) */\n\tname: string;\n\t/** Approximate row count */\n\trowCount?: number;\n\t/** Column/field names */\n\tcolumns: string[];\n}\n\n/**\n * Representation of a data source for the main agent.\n * Contains entity names WITH column names so the LLM can route accurately.\n */\nexport interface SourceSummary {\n\t/** Source ID (matches tool ID prefix) */\n\tid: string;\n\t/** Human-readable source name */\n\tname: string;\n\t/** Source type: postgres, excel, rest_api, etc. */\n\ttype: string;\n\t/** Brief description of what data this source contains */\n\tdescription: string;\n\t/** Detailed entity info with column names for routing */\n\tentityDetails: EntityDetail[];\n\t/** The tool ID associated with this source */\n\ttoolId: string;\n}\n\n// ============================================\n// Source Tool Input (what the main agent passes to a source tool)\n// ============================================\n\n/**\n * Input the main agent sends when calling a source tool.\n * This is what the source agent receives to do its work.\n */\nexport interface SourceToolInput {\n\t/** What data to fetch — natural language description */\n\tintent: string;\n\t/** How to return data */\n\taggregation?: 'raw' | 'pre-aggregate' | 'summary';\n}\n\n// ============================================\n// Source Agent Result\n// ============================================\n\n/**\n * What a source agent returns after querying its data source.\n * The main agent uses this to analyze and compose the final response.\n */\nexport interface SourceAgentResult {\n\t/** Source ID */\n\tsourceId: string;\n\t/** Source name */\n\tsourceName: string;\n\t/** Whether the query succeeded */\n\tsuccess: boolean;\n\t/** Result data rows */\n\tdata: any[];\n\t/** Metadata about the query execution */\n\tmetadata: SourceAgentMetadata;\n\t/** Tool execution info for the last successful query (backward compat) */\n\texecutedTool: ExecutedToolInfo;\n\t/** All successful tool executions (primary + follow-up queries) */\n\tallExecutedTools?: ExecutedToolInfo[];\n\t/** Error message if failed */\n\terror?: string;\n}\n\nexport interface SourceAgentMetadata {\n\t/** Total rows that matched the query (before limit) */\n\ttotalRowsMatched: number;\n\t/** Rows actually returned (after limit) */\n\trowsReturned: number;\n\t/** Whether the result was truncated by the row limit */\n\tisLimited: boolean;\n\t/** The query/params that were executed */\n\tqueryExecuted?: string;\n\t/** Execution time in milliseconds */\n\texecutionTimeMs: number;\n}\n\n// ============================================\n// Workflow Components\n// ============================================\n\n/**\n * A pre-built, multi-step UI flow registered with the SDK.\n *\n * When the main agent decides a user's question matches a workflow's whenToUse\n * trigger, it picks the workflow instead of running source agents / generating\n * dashboard components. The LLM extracts the workflow's required props from the\n * prompt (using `propsSchema` as the tool input_schema) and the SDK returns the\n * workflow component directly — no analysis text, no chart generation. The\n * frontend renders the registered workflow component with the LLM-extracted\n * props.\n */\nexport interface WorkflowDescriptor {\n\t/** Unique workflow id (used as the LLM tool name) */\n\tid: string;\n\t/** Component name on the frontend (matches the registered React component) */\n\tname: string;\n\t/** Short human-readable description of what this workflow does */\n\tdescription: string;\n\t/**\n\t * 1–2 sentence trigger condition. The LLM uses this to decide if the\n\t * user's prompt matches this workflow. Be specific — e.g.\n\t * \"User wants to *initiate* an inventory transfer (review + submit POs),\n\t * not just see analysis or charts.\"\n\t */\n\twhenToUse: string;\n\t/**\n\t * JSON-schema-style description of the props the workflow needs. Becomes\n\t * the LLM tool's input_schema, so the model fills these from the prompt.\n\t * Use the same shape as `params` on direct tools — string descriptors with\n\t * an optional \"(optional)\" suffix.\n\t *\n\t * Example:\n\t * ```\n\t * {\n\t * selectedStore: 'object — { id, name } of the source branch',\n\t * minROI: 'number (optional) — only show transfers with ROI ≥ this',\n\t * }\n\t * ```\n\t */\n\tpropsSchema: Record<string, string>;\n\t/**\n\t * Optional: static prop defaults merged with LLM-extracted props before\n\t * the component is returned. Useful for things like the embedded\n\t * `externalTool` config that the workflow uses to fetch its own data.\n\t */\n\tdefaultProps?: Record<string, any>;\n}\n\n/**\n * The workflow selection captured during a routing call.\n * Set on AgentResponse when the LLM picks a workflow tool.\n */\nexport interface SelectedWorkflow {\n\t/** Component name (matches WorkflowDescriptor.name) */\n\tname: string;\n\t/** Props extracted from the prompt + merged with workflow.defaultProps */\n\tprops: Record<string, any>;\n}\n\n// ============================================\n// Agent Response (Final output)\n// ============================================\n\n/**\n * The complete response from the multi-agent system.\n * Contains everything needed for text display + component generation.\n */\nexport interface AgentResponse {\n\t/** Generated text response (analysis of the data) */\n\ttext: string;\n\t/** All executed tools across all source agents (for component generation) */\n\texecutedTools: ExecutedToolInfo[];\n\t/** Individual results from each source agent */\n\tsourceResults: SourceAgentResult[];\n\t/**\n\t * Populated when MainAgent wrote AND successfully executed a script during its turn.\n\t * Caller (agent-user-response.ts) persists it via ScriptStore.save().\n\t * Absent when MainAgent didn't write one (trivial question / all attempts failed).\n\t */\n\tsavedScript?: AgentWrittenScript;\n\t/**\n\t * Set when the LLM routed the question to a registered workflow component.\n\t * When present, the upstream caller should skip component generation and\n\t * return this workflow as the response.\n\t */\n\tworkflow?: SelectedWorkflow;\n}\n\n/**\n * A script MainAgent authored + verified during its turn. Shape aligns with\n * what ScriptStore.save() needs — minus store-assigned fields (id, timestamps, counts).\n */\nexport interface AgentWrittenScript {\n\t/**\n\t * `ScriptRecipe.id` of the draft that was authored + verified during this turn.\n\t * The caller passes this to `ScriptStore.promoteToVerified(recipeId, …)` to\n\t * flip the draft to verified status and (when possible) drop the turn-suffix\n\t * from its filename.\n\t */\n\trecipeId: string;\n\tname: string;\n\tintentDescription: string;\n\ttags: string[];\n\tparameters: Array<{\n\t\tname: string;\n\t\ttype: 'string' | 'number' | 'date' | 'date_range' | 'enum' | 'boolean';\n\t\trequired: boolean;\n\t\tdefault?: any;\n\t\tenumValues?: Record<string, string>;\n\t\tdescription: string;\n\t}>;\n\tscriptBody: string;\n\t/** Source IDs referenced by the script (extracted from ctx.query calls) */\n\tsourceIds: string[];\n\t/** Tables referenced in the script's SQL (regex-extracted) */\n\ttables: string[];\n\t/** Executed queries from the verified run — fed to component generation */\n\texecutedQueries: Array<{\n\t\tsourceId: string;\n\t\tsourceName: string;\n\t\tsql: string;\n\t\tdata: any[];\n\t\tcount: number;\n\t\ttotalCount?: number;\n\t\texecutionTimeMs: number;\n\t\t/**\n\t\t * True for synthetic entries (ctx.emit datasets, the computed:_final\n\t\t * post-JS data). The component generator routes virtual sources through\n\t\t * the script_dataset sentinel toolId so the frontend resolves them via\n\t\t * queryCache instead of attempting to re-execute SQL.\n\t\t */\n\t\tvirtual?: boolean;\n\t}>;\n}\n\n// ============================================\n// Agent Configuration\n// ============================================\n\n/**\n * Configuration for the multi-agent system.\n * Controls limits, models, and behavior.\n */\nexport interface AgentConfig {\n\t/** Max rows shown to the UI preview / inlined per source (default: 10) */\n\tmaxRowsPerSource: number;\n\t/**\n\t * Max rows a source query may FETCH from the DB server-side (default: 2000).\n\t * Decoupled from what the main agent is shown: the full result is fetched and\n\t * summarized (bounded), but only a small/complete slice enters LLM context.\n\t * This lets small lookups (benchmark maps) arrive COMPLETE without letting\n\t * large results blow up context.\n\t */\n\tmaxRowsFetched: number;\n\t/** Model for the main agent (routing + analysis in one LLM call) */\n\tmainAgentModel: string;\n\t/** Model for source agent query generation */\n\tsourceAgentModel: string;\n\t/** API key for LLM calls */\n\tapiKey?: string;\n\t/** Max retry attempts per source agent */\n\tmaxRetries: number;\n\t/** Max tool calling iterations for the main agent loop */\n\tmaxIterations: number;\n\t/** Global knowledge base context (static, same for all users/questions — cached in system prompt) */\n\tglobalKnowledgeBase?: string;\n\t/** Per-request knowledge base context (user-specific + query-matched — dynamic, not cached) */\n\tknowledgeBaseContext?: string;\n\t/** Collections registry (ChromaDB search hooks) for embedding-based schema + source search */\n\tcollections?: any;\n\t/** Optional project ID for scoping embedding searches */\n\tprojectId?: string;\n}\n\n/**\n * Default agent configuration\n */\nexport const DEFAULT_AGENT_CONFIG: AgentConfig = {\n\tmaxRowsPerSource: 10,\n\tmaxRowsFetched: MAX_ROWS_FETCHED,\n\tmainAgentModel: '', // will use the provider's default model\n\tsourceAgentModel: '', // will use the provider's default model\n\tmaxRetries: 2, // 2 retries = 3 total query attempts (1 initial + 2 retries for SQL errors)\n\tmaxIterations: 12, // schema search (2-3) + query attempts (2) + write_script/execute_script (2-3) + LLM responses + final\n};\n","/**\n * ComponentPropsProcessor - Validates and cleans component props\n * Handles field name mapping, query validation, and external tool validation\n */\n\nimport { ensureQueryLimit, validateAndFixSqlQuery } from '../utils';\nimport { logger } from '../../utils/logger';\nimport { MAX_AGENT_QUERY_LIMIT } from '../constants';\n\n/**\n * Executed tool info for validation\n */\nexport interface ExecutedToolInfo {\n\tid: string;\n\tname: string;\n\toutputSchema?: {\n\t\tfields?: Array<{\n\t\t\tname: string;\n\t\t\ttype: string;\n\t\t}>;\n\t};\n}\n\n/**\n * Configuration for props processing\n */\nexport interface PropsProcessorConfig {\n\tproviderName: string;\n\tdefaultLimit: number;\n\tmaxLimit?: number; // Maximum allowed row limit (defaults to MAX_AGENT_QUERY_LIMIT)\n}\n\n/**\n * Config keys that expect numeric fields\n */\nconst NUMERIC_CONFIG_KEYS = ['yAxisKey', 'valueKey', 'aggregationField', 'sizeKey'];\n\n/**\n * Config keys that expect string fields\n */\nconst STRING_CONFIG_KEYS = ['xAxisKey', 'nameKey', 'labelKey', 'groupBy'];\n\n/**\n * All config fields that need validation\n */\nconst CONFIG_FIELDS_TO_VALIDATE = [\n\t'xAxisKey', 'yAxisKey', 'valueKey', 'nameKey', 'labelKey',\n\t'groupBy', 'aggregationField', 'seriesKey', 'sizeKey',\n\t'xAggregationField', 'yAggregationField'\n];\n\n/**\n * Find the best matching field name from valid fields\n */\nfunction findMatchingField(\n\tfieldName: string,\n\tconfigKey: string,\n\tvalidFieldNames: string[],\n\tfieldTypes: Record<string, string>,\n\tproviderName: string\n): string | null {\n\tif (!fieldName) return null;\n\tconst lowerField = fieldName.toLowerCase();\n\tconst validFieldNamesLower = validFieldNames.map(n => n.toLowerCase());\n\n\t// 1. Exact match (case-insensitive)\n\tconst exactIdx = validFieldNamesLower.indexOf(lowerField);\n\tif (exactIdx !== -1) return validFieldNames[exactIdx];\n\n\t// 2. Partial match (contains)\n\tconst containsMatches = validFieldNames.filter((_, i) =>\n\t\tvalidFieldNamesLower[i].includes(lowerField) || lowerField.includes(validFieldNamesLower[i])\n\t);\n\tif (containsMatches.length === 1) return containsMatches[0];\n\n\t// 3. Based on configKey type, pick the best field by data type\n\tif (NUMERIC_CONFIG_KEYS.includes(configKey)) {\n\t\tconst numericFields = validFieldNames.filter(f => fieldTypes[f] === 'number');\n\t\tconst match = numericFields.find(f =>\n\t\t\tf.toLowerCase().includes(lowerField) || lowerField.includes(f.toLowerCase())\n\t\t);\n\t\tif (match) return match;\n\t\tif (numericFields.length > 0) {\n\t\t\tlogger.warn(`[${providerName}] No match for \"${fieldName}\", using first numeric field: ${numericFields[0]}`);\n\t\t\treturn numericFields[0];\n\t\t}\n\t}\n\n\tif (STRING_CONFIG_KEYS.includes(configKey)) {\n\t\tconst stringFields = validFieldNames.filter(f => fieldTypes[f] === 'string');\n\t\tconst match = stringFields.find(f =>\n\t\t\tf.toLowerCase().includes(lowerField) || lowerField.includes(f.toLowerCase())\n\t\t);\n\t\tif (match) return match;\n\t\tif (stringFields.length > 0) {\n\t\t\tlogger.warn(`[${providerName}] No match for \"${fieldName}\", using first string field: ${stringFields[0]}`);\n\t\t\treturn stringFields[0];\n\t\t}\n\t}\n\n\t// Last resort: return the first field\n\tlogger.warn(`[${providerName}] No match for \"${fieldName}\", using first field: ${validFieldNames[0]}`);\n\treturn validFieldNames[0];\n}\n\n/**\n * Validate and correct field names in config against tool's outputSchema\n */\nexport function validateConfigFieldNames(\n\tconfig: any,\n\toutputSchema: { fields?: Array<{ name: string; type: string }> },\n\tproviderName: string\n): any {\n\tif (!outputSchema?.fields || !config) return config;\n\n\tconst validFieldNames = outputSchema.fields.map(f => f.name);\n\tconst fieldTypes = outputSchema.fields.reduce((acc: Record<string, string>, f) => {\n\t\tacc[f.name] = f.type;\n\t\treturn acc;\n\t}, {});\n\n\tconst correctedConfig = { ...config };\n\n\t// Validate standard config fields\n\tfor (const configKey of CONFIG_FIELDS_TO_VALIDATE) {\n\t\tconst fieldValue = correctedConfig[configKey];\n\t\tif (fieldValue && typeof fieldValue === 'string') {\n\t\t\tif (!validFieldNames.includes(fieldValue)) {\n\t\t\t\tconst correctedField = findMatchingField(fieldValue, configKey, validFieldNames, fieldTypes, providerName);\n\t\t\t\tif (correctedField) {\n\t\t\t\t\tlogger.warn(`[${providerName}] Correcting config.${configKey}: \"${fieldValue}\" → \"${correctedField}\"`);\n\t\t\t\t\tcorrectedConfig[configKey] = correctedField;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// Also check series array if present (for multi-series charts)\n\tif (Array.isArray(correctedConfig.series)) {\n\t\tcorrectedConfig.series = correctedConfig.series.map((s: any) => {\n\t\t\tif (s.dataKey && typeof s.dataKey === 'string' && !validFieldNames.includes(s.dataKey)) {\n\t\t\t\tconst correctedField = findMatchingField(s.dataKey, 'yAxisKey', validFieldNames, fieldTypes, providerName);\n\t\t\t\tif (correctedField) {\n\t\t\t\t\tlogger.warn(`[${providerName}] Correcting series.dataKey: \"${s.dataKey}\" → \"${correctedField}\"`);\n\t\t\t\t\treturn { ...s, dataKey: correctedField };\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn s;\n\t\t});\n\t}\n\n\treturn correctedConfig;\n}\n\n/**\n * Validate externalTool reference against executed tools\n */\nexport function validateExternalTool(\n\texternalTool: { toolId: string } | null | undefined,\n\texecutedTools: ExecutedToolInfo[] | undefined,\n\tproviderName: string\n): { valid: boolean; executedTool?: ExecutedToolInfo } {\n\tif (!externalTool) {\n\t\treturn { valid: true };\n\t}\n\n\tconst toolId = externalTool.toolId;\n\tconst validToolIds = (executedTools || []).map(t => t.id);\n\tconst isValidTool = toolId && typeof toolId === 'string' && validToolIds.includes(toolId);\n\n\tif (!isValidTool) {\n\t\tlogger.warn(`[${providerName}] externalTool.toolId \"${toolId}\" not found in executed tools [${validToolIds.join(', ')}], setting to null`);\n\t\treturn { valid: false };\n\t}\n\n\tconst executedTool = executedTools?.find(t => t.id === toolId);\n\treturn { valid: true, executedTool };\n}\n\n/**\n * Validate and fix query - removes invalid patterns and fixes SQL errors\n */\nexport function validateAndCleanQuery(\n\tquery: string | { sql: string; values?: any; params?: any } | null | undefined,\n\tconfig: PropsProcessorConfig\n): { query: any; wasModified: boolean } {\n\tif (!query) {\n\t\treturn { query: null, wasModified: false };\n\t}\n\n\tlet wasModified = false;\n\tlet cleanedQuery = query;\n\tconst queryStr = typeof query === 'string' ? query : query?.sql || '';\n\n\t// Check for invalid JSON parsing patterns\n\tif (queryStr.includes('OPENJSON') || queryStr.includes('JSON_VALUE')) {\n\t\tlogger.warn(`[${config.providerName}] Query contains OPENJSON/JSON_VALUE (invalid - cannot parse tool result), setting query to null`);\n\t\treturn { query: null, wasModified: true };\n\t}\n\n\t// Validate and fix common SQL syntax errors\n\tconst { query: fixedQuery, fixed, fixes } = validateAndFixSqlQuery(queryStr);\n\tif (fixed) {\n\t\tlogger.warn(`[${config.providerName}] SQL fixes applied to component query: ${fixes.join('; ')}`);\n\t\twasModified = true;\n\t\tif (typeof cleanedQuery === 'string') {\n\t\t\tcleanedQuery = fixedQuery;\n\t\t} else if ((cleanedQuery as any)?.sql) {\n\t\t\tcleanedQuery = { ...(cleanedQuery as any), sql: fixedQuery };\n\t\t}\n\t}\n\n\t// Ensure query has proper LIMIT clause\n\tconst maxLimit = config.maxLimit ?? MAX_AGENT_QUERY_LIMIT;\n\tif (typeof cleanedQuery === 'string') {\n\t\tconst limitedQuery = ensureQueryLimit(cleanedQuery, config.defaultLimit, maxLimit);\n\t\tif (limitedQuery !== cleanedQuery) wasModified = true;\n\t\tcleanedQuery = limitedQuery;\n\t} else if ((cleanedQuery as any)?.sql) {\n\t\tconst limitedSql = ensureQueryLimit((cleanedQuery as any).sql, config.defaultLimit, maxLimit);\n\t\tif (limitedSql !== (cleanedQuery as any).sql) wasModified = true;\n\t\tcleanedQuery = { ...(cleanedQuery as any), sql: limitedSql };\n\t}\n\n\treturn { query: cleanedQuery, wasModified };\n}\n\n/**\n * Process and clean component props\n * - Validates externalTool references\n * - Corrects field names in config\n * - Validates and fixes queries\n */\nexport function processComponentProps(\n\tprops: any,\n\texecutedTools: ExecutedToolInfo[] | undefined,\n\tconfig: PropsProcessorConfig\n): any {\n\tlet cleanedProps = { ...props };\n\n\t// Validate externalTool\n\tif (cleanedProps.externalTool) {\n\t\tconst { valid } = validateExternalTool(\n\t\t\tcleanedProps.externalTool,\n\t\t\texecutedTools,\n\t\t\tconfig.providerName\n\t\t);\n\n\t\tif (!valid) {\n\t\t\tcleanedProps.externalTool = null;\n\t\t}\n\t\t// Skip field name validation for all external tools\n\t\t// - Database tools: fields come from SQL query aliases\n\t\t// - Excel/CSV tools: fields come from actual column names\n\t\t// - API tools: fields come from API response\n\t\t// The LLM generates correct field names based on tool schema/output\n\t}\n\n\t// Validate and clean query\n\tif (cleanedProps.query) {\n\t\tconst { query } = validateAndCleanQuery(cleanedProps.query, config);\n\t\tcleanedProps.query = query;\n\t}\n\n\t// Log if both query and externalTool exist\n\tif (cleanedProps.query && cleanedProps.externalTool) {\n\t\tlogger.info(`[${config.providerName}] Both query and externalTool exist, keeping both - frontend will decide`);\n\t}\n\n\treturn cleanedProps;\n}\n","/**\n * Agent Component Generator\n *\n * Dedicated component generation + validation pipeline for the agent architecture.\n * Replaces the use of base-llm's matchComponentsFromAnalysis() in the agent flow.\n *\n * Key differences from the old flow:\n * - Validates externalTool SQL via external-tools.execute (not database.execute)\n * - Uses per-source schema and database rules for LLM query fixes\n * - Components without valid SQL are excluded (not sent to frontend broken)\n */\n\nimport { Component } from '../../types';\nimport { ensureQueryLimit, validateAndFixSqlQuery, convertQuestionsToActions, type DatabaseType } from '../utils';\nimport { promptLoader } from '../prompt-loader';\nimport { LLM } from '../../llm';\nimport { logger } from '../../utils/logger';\nimport { getCurrentDateTimeForPrompt } from '../../utils/datetime';\nimport { queryCache } from '../../utils/query-cache';\nimport { processComponentProps } from '../utils/component-props-processor';\nimport { extractPromptText } from '../prompt-extractor';\nimport KB from '../knowledge-base';\nimport type { Action } from '../../threads/action';\nimport type { ExecutedToolInfo } from '../services/tool-executor-service';\nimport {\n\tMAX_QUERY_VALIDATION_RETRIES,\n\tMAX_TOKENS_COMPONENT_MATCHING,\n\tMAX_COMPONENT_QUERY_LIMIT,\n\tDEFAULT_QUERY_LIMIT,\n\tKNOWLEDGE_BASE_TOP_K,\n} from '../constants';\n\n// ============================================\n// Types\n// ============================================\n\nexport interface AgentComponentGeneratorParams {\n\tanalysisContent: string;\n\tcomponents: Component[];\n\tuserPrompt: string;\n\texecutedTools: ExecutedToolInfo[];\n\tcollections: any;\n\tapiKey?: string;\n\tcomponentStreamCallback?: (component: Component) => void;\n\tuserId?: string;\n}\n\nexport interface AgentComponentGeneratorResult {\n\tcomponents: Component[];\n\tlayoutTitle: string;\n\tlayoutDescription: string;\n\tactions: Action[];\n}\n\n// ============================================\n// Main Entry Point\n// ============================================\n\n/**\n * Generate and validate components for the agent architecture.\n *\n * Steps:\n * A. Build prompt context (available components, executed tools, KB, rules)\n * B. LLM call to generate components JSON\n * C. Build final Component objects from LLM output\n * D. Validate externalTool SQL queries (execute, retry with LLM fix, exclude on failure)\n * E. Return validated components + layout + actions\n */\nexport async function generateAgentComponents(\n\tparams: AgentComponentGeneratorParams\n): Promise<AgentComponentGeneratorResult> {\n\tconst startTime = Date.now();\n\tconst {\n\t\tanalysisContent, components, userPrompt,\n\t\texecutedTools, collections, apiKey,\n\t\tcomponentStreamCallback, userId,\n\t} = params;\n\n\tlogger.info(`[AgentComponentGen] Starting | ${executedTools.length} executed tools | ${components.length} available components`);\n\n\ttry {\n\t\t// ============================================\n\t\t// STEP A: Build prompt context\n\t\t// ============================================\n\n\t\tconst availableComponentsText = formatAvailableComponents(components);\n\t\tconst executedToolsText = formatExecutedTools(executedTools);\n\n\t\t// Collect database rules from all source types used\n\t\tconst sourceTypes = [...new Set(executedTools.map(t => t.sourceType).filter(Boolean))];\n\t\tlet databaseRules = '';\n\t\tif (sourceTypes.length > 0) {\n\t\t\tconst rulesArr = await Promise.all(\n\t\t\t\tsourceTypes.map(st => promptLoader.loadDatabaseRulesForType(st!))\n\t\t\t);\n\t\t\tdatabaseRules = rulesArr.join('\\n\\n');\n\t\t} else {\n\t\t\tdatabaseRules = await promptLoader.loadDatabaseRules();\n\t\t}\n\n\t\t// Knowledge base context — split global (static/cached) vs user+query (dynamic)\n\t\tlet globalKnowledgeBase = 'No global knowledge base available.';\n\t\tlet knowledgeBaseContext = 'No additional knowledge base context available.';\n\t\tif (collections) {\n\t\t\tconst kbResult = await KB.getAllKnowledgeBase({\n\t\t\t\tprompt: userPrompt || analysisContent,\n\t\t\t\tcollections,\n\t\t\t\tuserId,\n\t\t\t\ttopK: KNOWLEDGE_BASE_TOP_K,\n\t\t\t});\n\t\t\tglobalKnowledgeBase = kbResult.globalContext || globalKnowledgeBase;\n\t\t\tconst dynamicParts: string[] = [];\n\t\t\tif (kbResult.userContext) {\n\t\t\t\tdynamicParts.push('## User-Specific Knowledge Base\\n' + kbResult.userContext);\n\t\t\t}\n\t\t\tif (kbResult.queryContext) {\n\t\t\t\tdynamicParts.push('## Relevant Knowledge Base (Query-Matched)\\n' + kbResult.queryContext);\n\t\t\t}\n\t\t\tknowledgeBaseContext = dynamicParts.join('\\n\\n') || knowledgeBaseContext;\n\t\t}\n\n\t\tconst prompts = await promptLoader.loadPrompts('match-text-components', {\n\t\t\tUSER_PROMPT: userPrompt || '',\n\t\t\tANALYSIS_CONTENT: analysisContent,\n\t\t\tAVAILABLE_COMPONENTS: availableComponentsText,\n\t\t\tSCHEMA_DOC: 'Use column names from executed query results in EXECUTED_TOOLS.',\n\t\t\tDATABASE_RULES: databaseRules,\n\t\t\tDEFERRED_TOOLS: 'No deferred external tools for this request.',\n\t\t\tEXECUTED_TOOLS: executedToolsText,\n\t\t\tGLOBAL_KNOWLEDGE_BASE: globalKnowledgeBase,\n\t\t\tKNOWLEDGE_BASE_CONTEXT: knowledgeBaseContext,\n\t\t\tCURRENT_DATETIME: getCurrentDateTimeForPrompt(),\n\t\t});\n\n\t\tlogger.logLLMPrompt('agentComponentGen', 'system', extractPromptText(prompts.system));\n\t\tlogger.logLLMPrompt('agentComponentGen', 'user', `Text Analysis:\\n${analysisContent}\\n\\nExecuted Tools:\\n${executedToolsText}`);\n\n\t\t// ============================================\n\t\t// STEP B: LLM call — generate components JSON\n\t\t// ============================================\n\n\t\tlet fullResponseText = '';\n\t\tlet answerComponentStreamed = false;\n\n\t\tconst partialCallback = componentStreamCallback ? (chunk: string) => {\n\t\t\tfullResponseText += chunk;\n\n\t\t\t// Stream answerComponent early if detected\n\t\t\tif (!answerComponentStreamed && componentStreamCallback) {\n\t\t\t\tconst streamed = tryStreamAnswerComponent(\n\t\t\t\t\tfullResponseText, components, executedTools,\n\t\t\t\t\tcollections, apiKey, componentStreamCallback\n\t\t\t\t);\n\t\t\t\tif (streamed) answerComponentStreamed = true;\n\t\t\t}\n\t\t} : undefined;\n\n\t\tconst result = await LLM.stream<any>(\n\t\t\t{ sys: prompts.system, user: prompts.user },\n\t\t\t{\n\t\t\t\tmodel: 'anthropic/claude-haiku-4-5-20251001',\n\t\t\t\tmaxTokens: MAX_TOKENS_COMPONENT_MATCHING,\n\t\t\t\ttemperature: 0,\n\t\t\t\tapiKey,\n\t\t\t\tpartial: partialCallback,\n\t\t\t},\n\t\t\ttrue // Parse as JSON\n\t\t) as any;\n\n\t\tlet matchedComponents = result.matchedComponents || [];\n\t\tconst layoutTitle = result.layoutTitle || 'Dashboard';\n\t\tconst layoutDescription = result.layoutDescription || 'Multi-component dashboard';\n\n\t\tconst vizComps = matchedComponents.filter((mc: any) => mc.componentName !== 'DynamicMarkdownBlock');\n\t\tlogger.info(`[AgentComponentGen] ${vizComps.length} visualizations, ${matchedComponents.length - vizComps.length} markdown blocks`);\n\n\t\t// Ensure answerComponent is in matchedComponents (avoid duplicates)\n\t\tif (result.hasAnswerComponent && result.answerComponent?.componentId) {\n\t\t\tconst answer = result.answerComponent;\n\t\t\tconst answerSql = answer.props?.externalTool?.parameters?.sql || '';\n\t\t\tconst answerTitle = answer.props?.title || '';\n\t\t\tconst answerType = answer.componentName || '';\n\n\t\t\tconst isDuplicate = matchedComponents.some((mc: any) => {\n\t\t\t\tconst mcSql = mc.props?.externalTool?.parameters?.sql || '';\n\t\t\t\tconst mcTitle = mc.props?.title || '';\n\t\t\t\tconst mcType = mc.componentName || '';\n\t\t\t\treturn mcType === answerType && (mcTitle === answerTitle || mcSql === answerSql);\n\t\t\t});\n\n\t\t\tif (!isDuplicate) {\n\t\t\t\tmatchedComponents.unshift(answer);\n\t\t\t}\n\t\t}\n\n\t\tlogger.info(`[AgentComponentGen] LLM returned ${matchedComponents.length} components`);\n\t\tmatchedComponents.forEach((comp: any, idx: number) => {\n\t\t\tlogger.info(`[AgentComponentGen] ${idx + 1}. ${comp.componentType} (${comp.componentName})`);\n\t\t});\n\n\t\tlogger.file('\\n=============================\\nFull LLM response:', JSON.stringify(result, null, 2));\n\n\t\tconst rawActions = result.actions || [];\n\t\tconst actions = convertQuestionsToActions(rawActions);\n\n\t\t// ============================================\n\t\t// STEP C: Build final Component objects\n\t\t// ============================================\n\n\t\tconst finalComponents: Component[] = matchedComponents.map((mc: any) => {\n\t\t\tconst originalComponent = components.find(c => c.name === mc.componentName);\n\t\t\tif (!originalComponent) {\n\t\t\t\tlogger.warn(`[AgentComponentGen] Component \"${mc.componentName}\" not found in available components`);\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst cleanedProps = processComponentProps(\n\t\t\t\tmc.props, executedTools,\n\t\t\t\t{ providerName: 'AgentComponentGen', defaultLimit: DEFAULT_QUERY_LIMIT, maxLimit: MAX_COMPONENT_QUERY_LIMIT }\n\t\t\t);\n\n\t\t\treturn {\n\t\t\t\t...originalComponent,\n\t\t\t\tprops: { ...originalComponent.props, ...cleanedProps },\n\t\t\t};\n\t\t}).filter(Boolean) as Component[];\n\n\t\t// ============================================\n\t\t// STEP D: Validate externalTool SQL queries\n\t\t// ============================================\n\n\t\tconst validatedComponents = await validateExternalToolQueries(\n\t\t\tfinalComponents, collections, executedTools, apiKey\n\t\t);\n\n\t\tconst elapsed = Date.now() - startTime;\n\t\tlogger.info(`[AgentComponentGen] Complete | ${validatedComponents.length}/${finalComponents.length} validated | ${elapsed}ms`);\n\n\t\treturn {\n\t\t\tcomponents: validatedComponents,\n\t\t\tlayoutTitle,\n\t\t\tlayoutDescription,\n\t\t\tactions,\n\t\t};\n\n\t} catch (error) {\n\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\t\tlogger.error(`[AgentComponentGen] Error: ${errorMsg}`);\n\t\treturn {\n\t\t\tcomponents: [],\n\t\t\tlayoutTitle: 'Dashboard',\n\t\t\tlayoutDescription: '',\n\t\t\tactions: [],\n\t\t};\n\t}\n}\n\n// ============================================\n// Prompt Formatting Helpers\n// ============================================\n\nfunction formatAvailableComponents(components: Component[]): string {\n\tif (!components || components.length === 0) return 'No components available';\n\n\treturn components.map((comp, idx) => {\n\t\tconst keywords = comp.keywords ? comp.keywords.join(', ') : '';\n\t\tconst propsPreview = comp.props ? JSON.stringify(comp.props, null, 2) : 'No props';\n\t\treturn `${idx + 1}. ID: ${comp.id}\n Name: ${comp.name}\n Type: ${comp.type}\n Description: ${comp.description || 'No description'}\n Keywords: ${keywords}\n Props Structure: ${propsPreview}`;\n\t}).join('\\n\\n');\n}\n\nfunction formatExecutedTools(executedTools: ExecutedToolInfo[]): string {\n\tif (!executedTools || executedTools.length === 0) {\n\t\treturn 'No external tools were executed for data fetching.';\n\t}\n\n\treturn 'The following external tools were executed to fetch data.\\n' +\n\t\texecutedTools.map((tool, idx) => {\n\t\t\tlet outputSchemaText = 'Not available';\n\t\t\tlet fieldNamesList = '';\n\n\t\t\tconst recordCount = tool.result?._totalRecords ?? 'unknown';\n\n\t\t\tlet metadataText = '';\n\t\t\tif (tool.result?._metadata && Object.keys(tool.result._metadata).length > 0) {\n\t\t\t\tconst metadataEntries = Object.entries(tool.result._metadata)\n\t\t\t\t\t.map(([key, value]) => `${key}: ${value}`)\n\t\t\t\t\t.join(', ');\n\t\t\t\tmetadataText = `\\n 📋 METADATA: ${metadataEntries}`;\n\t\t\t}\n\n\t\t\tif (tool.outputSchema) {\n\t\t\t\tconst fields = tool.outputSchema.fields || [];\n\t\t\t\tconst numericFields = fields.filter((f: any) => f.type === 'number').map((f: any) => f.name);\n\t\t\t\tconst stringFields = fields.filter((f: any) => f.type === 'string').map((f: any) => f.name);\n\n\t\t\t\tfieldNamesList = `\n 📊 NUMERIC FIELDS (use for yAxisKey, valueKey, aggregationField): ${numericFields.join(', ') || 'none'}\n 📝 STRING FIELDS (use for xAxisKey, groupBy, nameKey): ${stringFields.join(', ') || 'none'}`;\n\n\t\t\t\tconst fieldsText = fields.map((f: any) =>\n\t\t\t\t\t` \"${f.name}\" (${f.type}): ${f.description}`\n\t\t\t\t).join('\\n');\n\t\t\t\toutputSchemaText = `${tool.outputSchema.description}\\n Fields:\\n${fieldsText}`;\n\t\t\t}\n\n\t\t\t// Show sample rows so the LLM picks correct xAxis/yAxis/valueKey fields and sees\n\t\t\t// type + range variation. Defensive cap: even if a tool slipped a large object\n\t\t\t// past upstream truncation, never inject more than ~8 KB of sample JSON into\n\t\t\t// the prompt.\n\t\t\tconst MAX_SAMPLE_BLOCK_CHARS = 8000;\n\t\t\tlet sampleDataText = '';\n\t\t\tconst sampleData = tool.result?._sampleData;\n\t\t\tif (Array.isArray(sampleData) && sampleData.length > 0) {\n\t\t\t\tconst sampleFields = Object.keys(sampleData[0]);\n\t\t\t\tsampleDataText = `\\n 🔑 RESULT FIELDS: ${sampleFields.join(', ')}`;\n\t\t\t\ttry {\n\t\t\t\t\tconst stringified = JSON.stringify(sampleData, null, 2);\n\t\t\t\t\tconst capped = stringified.length > MAX_SAMPLE_BLOCK_CHARS\n\t\t\t\t\t\t? `${stringified.substring(0, MAX_SAMPLE_BLOCK_CHARS)}\\n... (truncated; ${stringified.length - MAX_SAMPLE_BLOCK_CHARS} more chars)`\n\t\t\t\t\t\t: stringified;\n\t\t\t\t\tsampleDataText += `\\n 📄 SAMPLE ROWS (${sampleData.length}): ${capped}`;\n\t\t\t\t} catch { /* skip if serialization fails */ }\n\t\t\t}\n\n\t\t\treturn `${idx + 1}. **${tool.name}**\n toolId: \"${tool.id}\"\n toolName: \"${tool.name}\"\n parameters: ${JSON.stringify(tool.params || {})}\n recordCount: ${recordCount} rows returned${metadataText}\n outputSchema: ${outputSchemaText}${fieldNamesList}${sampleDataText}`;\n\t\t}).join('\\n\\n');\n}\n\n// ============================================\n// Answer Component Streaming\n// ============================================\n\n/**\n * Try to extract and stream answerComponent from partial LLM response.\n * Returns true if successfully streamed.\n */\nfunction tryStreamAnswerComponent(\n\ttext: string,\n\tcomponents: Component[],\n\texecutedTools: ExecutedToolInfo[],\n\tcollections: any,\n\tapiKey: string | undefined,\n\tcallback: (component: Component) => void\n): boolean {\n\t// Check hasAnswerComponent flag\n\tconst hasMatch = text.match(/\"hasAnswerComponent\"\\s*:\\s*(true|false)/);\n\tif (!hasMatch || hasMatch[1] !== 'true') return false;\n\n\t// Find answerComponent object\n\tconst startMatch = text.match(/\"answerComponent\"\\s*:\\s*\\{/);\n\tif (!startMatch) return false;\n\n\tconst startPos = startMatch.index! + startMatch[0].length - 1;\n\n\t// Track braces to find complete object\n\tlet depth = 0;\n\tlet inString = false;\n\tlet escapeNext = false;\n\tlet endPos = -1;\n\n\tfor (let i = startPos; i < text.length; i++) {\n\t\tconst char = text[i];\n\t\tif (escapeNext) { escapeNext = false; continue; }\n\t\tif (char === '\\\\') { escapeNext = true; continue; }\n\t\tif (char === '\"') { inString = !inString; continue; }\n\t\tif (!inString) {\n\t\t\tif (char === '{') depth++;\n\t\t\telse if (char === '}') {\n\t\t\t\tdepth--;\n\t\t\t\tif (depth === 0) { endPos = i + 1; break; }\n\t\t\t}\n\t\t}\n\t}\n\n\tif (endPos <= startPos) return false;\n\n\ttry {\n\t\tconst answerData = JSON.parse(text.substring(startPos, endPos));\n\t\tif (!answerData?.componentId) return false;\n\n\t\tconst original = components.find(c => c.id === answerData.componentId);\n\t\tif (!original) return false;\n\n\t\tconst answerComponent: Component = {\n\t\t\t...original,\n\t\t\tprops: { ...original.props, ...answerData.props },\n\t\t};\n\n\t\t// Validate externalTool query before streaming\n\t\tconst answerProps = answerComponent.props as any;\n\t\tlet sql = answerProps?.externalTool?.parameters?.sql;\n\t\tif (sql && collections?.['external-tools']?.['execute']) {\n\t\t\tconst toolId = answerProps.externalTool.toolId;\n\t\t\tconst toolName = answerProps.externalTool.toolName;\n\n\t\t\t// Apply the same query limit that final validation uses,\n\t\t\t// so the streamed answer and final component show identical data\n\t\t\tconst executedTool = executedTools.find(t => t.id === toolId);\n\t\t\tconst dbType = executedTool?.sourceType as DatabaseType | undefined;\n\t\t\tif (dbType) {\n\t\t\t\tsql = ensureQueryLimit(sql, DEFAULT_QUERY_LIMIT, MAX_COMPONENT_QUERY_LIMIT, dbType);\n\t\t\t\tanswerProps.externalTool.parameters.sql = sql;\n\t\t\t}\n\n\t\t\t// Async validation — stream on success, skip on failure\n\t\t\t(async () => {\n\t\t\t\ttry {\n\t\t\t\t\tconst result = await collections['external-tools']['execute']({\n\t\t\t\t\t\ttoolId, toolName, sql, data: {},\n\t\t\t\t\t});\n\t\t\t\t\tif (result?.success === false || result?.error) {\n\t\t\t\t\t\tlogger.warn(`[AgentComponentGen] Answer component query failed: ${result?.error}`);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tconst cacheKey = toolId ? `${toolId}:${sql}` : sql;\n\t\t\t\t\tqueryCache.set(cacheKey, result?.data ?? result);\n\t\t\t\t\tcallback(answerComponent);\n\t\t\t\t} catch (err) {\n\t\t\t\t\tconst msg = err instanceof Error ? err.message : String(err);\n\t\t\t\t\tlogger.warn(`[AgentComponentGen] Answer component validation failed: ${msg}`);\n\t\t\t\t}\n\t\t\t})();\n\t\t} else {\n\t\t\t// No SQL to validate (markdown, non-SQL tools) — stream immediately\n\t\t\tcallback(answerComponent);\n\t\t}\n\n\t\treturn true;\n\t} catch {\n\t\treturn false;\n\t}\n}\n\n// ============================================\n// ExternalTool Query Validation\n// ============================================\n\n/**\n * Validate all component externalTool SQL queries.\n * Executes each query, retries with LLM fix on failure, excludes on max retries.\n */\nasync function validateExternalToolQueries(\n\tcomponents: Component[],\n\tcollections: any,\n\texecutedTools: ExecutedToolInfo[],\n\tapiKey?: string\n): Promise<Component[]> {\n\tif (!collections?.['external-tools']?.['execute']) {\n\t\tlogger.warn(`[AgentComponentGen] external-tools.execute not available, skipping validation`);\n\t\treturn components;\n\t}\n\n\tconst validated: Component[] = [];\n\n\t// Separate into components with and without externalTool SQL\n\tconst withSql: Component[] = [];\n\tconst withoutSql: Component[] = [];\n\n\tfor (const comp of components) {\n\t\tconst sql = (comp.props as any)?.externalTool?.parameters?.sql;\n\t\tif (sql) {\n\t\t\twithSql.push(comp);\n\t\t} else {\n\t\t\twithoutSql.push(comp);\n\t\t}\n\t}\n\n\t// Components without SQL pass through\n\tvalidated.push(...withoutSql);\n\n\tif (withSql.length === 0) return validated;\n\n\t// Deduplicate: group components by SQL so identical queries are validated only once\n\tconst sqlGroups = new Map<string, Component[]>();\n\tfor (const comp of withSql) {\n\t\tconst sql = (comp.props as any).externalTool.parameters.sql;\n\t\tconst normalized = sql.replace(/\\s+/g, ' ').trim();\n\t\tif (!sqlGroups.has(normalized)) {\n\t\t\tsqlGroups.set(normalized, []);\n\t\t}\n\t\tsqlGroups.get(normalized)!.push(comp);\n\t}\n\n\tconst uniqueQueries = Array.from(sqlGroups.entries());\n\tlogger.info(`[AgentComponentGen] Validating ${uniqueQueries.length} unique queries (${withSql.length} components)...`);\n\n\t// Validate unique queries in parallel (use first component from each group)\n\tconst results = await Promise.allSettled(\n\t\tuniqueQueries.map(([_, comps]) => validateSingleExternalToolQuery(comps[0], collections, executedTools, apiKey))\n\t);\n\n\tfor (let i = 0; i < results.length; i++) {\n\t\tconst result = results[i];\n\t\tconst [_, groupComps] = uniqueQueries[i];\n\n\t\tif (result.status === 'fulfilled' && result.value) {\n\t\t\t// First component gets the validated result, others reuse it\n\t\t\tvalidated.push(result.value);\n\t\t\tfor (let j = 1; j < groupComps.length; j++) {\n\t\t\t\t// Copy the (possibly fixed) SQL to sibling components\n\t\t\t\tconst fixedSql = (result.value.props as any)?.externalTool?.parameters?.sql;\n\t\t\t\tif (fixedSql) {\n\t\t\t\t\tconst siblingProps = groupComps[j].props as any;\n\t\t\t\t\tvalidated.push({\n\t\t\t\t\t\t...groupComps[j],\n\t\t\t\t\t\tprops: {\n\t\t\t\t\t\t\t...groupComps[j].props,\n\t\t\t\t\t\t\texternalTool: {\n\t\t\t\t\t\t\t\t...siblingProps.externalTool,\n\t\t\t\t\t\t\t\tparameters: { ...siblingProps.externalTool.parameters, sql: fixedSql },\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\tvalidated.push(groupComps[j]);\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tconst reason = result.status === 'rejected' ? result.reason : 'validation failed';\n\t\t\tfor (const comp of groupComps) {\n\t\t\t\tlogger.warn(`[AgentComponentGen] Excluded ${comp.name}: ${reason}`);\n\t\t\t}\n\t\t}\n\t}\n\n\tlogger.info(`[AgentComponentGen] Validation complete: ${validated.length}/${components.length} components passed`);\n\treturn validated;\n}\n\n/**\n * Validate a single component's externalTool SQL query with retry logic.\n * Returns the (possibly fixed) component on success, null on failure.\n */\nasync function validateSingleExternalToolQuery(\n\tcomponent: Component,\n\tcollections: any,\n\texecutedTools: ExecutedToolInfo[],\n\tapiKey?: string\n): Promise<Component | null> {\n\tconst compProps = component.props as any;\n\tconst toolId = compProps?.externalTool?.toolId;\n\tconst toolName = compProps?.externalTool?.toolName;\n\tlet currentSql = compProps?.externalTool?.parameters?.sql;\n\n\tif (!toolId || !currentSql) return component;\n\n\t// Determine database type from executed tool for correct SQL syntax (TOP vs LIMIT)\n\tconst executedTool = executedTools.find(t => t.id === toolId);\n\tconst dbType = executedTool?.sourceType as DatabaseType | undefined;\n\n\t// Only enforce query limit when source type is known — unknown sources\n\t// may have syntax that ensureQueryLimit doesn't understand\n\tif (dbType) {\n\t\tcurrentSql = ensureQueryLimit(currentSql, DEFAULT_QUERY_LIMIT, MAX_COMPONENT_QUERY_LIMIT, dbType);\n\t}\n\n\tlet attempts = 0;\n\n\twhile (attempts < MAX_QUERY_VALIDATION_RETRIES) {\n\t\tattempts++;\n\n\t\ttry {\n\t\t\tlogger.info(`[AgentComponentGen] Validating ${component.name} (attempt ${attempts}/${MAX_QUERY_VALIDATION_RETRIES})`);\n\n\t\t\tconst result = await collections['external-tools']['execute']({\n\t\t\t\ttoolId, toolName, sql: currentSql, data: {},\n\t\t\t});\n\n\t\t\tif (result?.success === false || result?.error) {\n\t\t\t\tconst errorMsg = result?.error || 'Unknown error';\n\t\t\t\tthrow new Error(typeof errorMsg === 'string' ? errorMsg : JSON.stringify(errorMsg));\n\t\t\t}\n\n\t\t\t// Success — cache the raw tool data (not the { success, data, message } wrapper)\n\t\t\t// so that executeByQueryId returns the same structure cached vs non-cached\n\t\t\tconst rawToolData = result?.data ?? result;\n\t\t\tconst cacheKey = toolId ? `${toolId}:${currentSql}` : currentSql;\n\t\t\tqueryCache.set(cacheKey, rawToolData);\n\t\t\tlogger.info(`[AgentComponentGen] ✓ ${component.name} validated (attempt ${attempts})`);\n\n\t\t\t// Update component with (possibly fixed) SQL\n\t\t\treturn {\n\t\t\t\t...component,\n\t\t\t\tprops: {\n\t\t\t\t\t...component.props,\n\t\t\t\t\texternalTool: {\n\t\t\t\t\t\t...compProps.externalTool,\n\t\t\t\t\t\tparameters: {\n\t\t\t\t\t\t\t...compProps.externalTool.parameters,\n\t\t\t\t\t\t\tsql: currentSql,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\n\t\t} catch (error) {\n\t\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\t\t\tlogger.warn(`[AgentComponentGen] ✗ ${component.name} failed (attempt ${attempts}): ${errorMsg}`);\n\n\t\t\tif (attempts >= MAX_QUERY_VALIDATION_RETRIES) {\n\t\t\t\tlogger.error(`[AgentComponentGen] Max retries reached for ${component.name}, excluding`);\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\t// Try to fix with LLM\n\t\t\ttry {\n\t\t\t\tconst fixedSql = await requestExternalToolQueryFix(\n\t\t\t\t\tcurrentSql, errorMsg, component, executedTools, toolId, apiKey\n\t\t\t\t);\n\n\t\t\t\tif (fixedSql && fixedSql !== currentSql) {\n\t\t\t\t\tcurrentSql = dbType ? ensureQueryLimit(fixedSql, DEFAULT_QUERY_LIMIT, MAX_COMPONENT_QUERY_LIMIT, dbType) : fixedSql;\n\t\t\t\t\tlogger.info(`[AgentComponentGen] LLM provided fix for ${component.name}, retrying...`);\n\t\t\t\t} else {\n\t\t\t\t\tlogger.warn(`[AgentComponentGen] LLM returned same or empty query, stopping retries`);\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\t\t\t} catch (fixError) {\n\t\t\t\tconst fixMsg = fixError instanceof Error ? fixError.message : String(fixError);\n\t\t\t\tlogger.error(`[AgentComponentGen] Failed to get LLM fix: ${fixMsg}`);\n\t\t\t\treturn null;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn null;\n}\n\n// ============================================\n// LLM Query Fix\n// ============================================\n\n/**\n * Request LLM to fix a failed SQL query using source-specific schema and rules.\n */\nasync function requestExternalToolQueryFix(\n\tfailedSql: string,\n\terrorMessage: string,\n\tcomponent: Component,\n\texecutedTools: ExecutedToolInfo[],\n\ttoolId: string,\n\tapiKey?: string\n): Promise<string> {\n\t// Find the executed tool to get source-specific schema and type\n\tconst executedTool = executedTools.find(t => t.id === toolId);\n\tconst sourceSchema = executedTool?.sourceSchema || 'Schema not available';\n\tconst sourceType = executedTool?.sourceType || 'postgresql';\n\n\t// Load database rules for this specific source type\n\tconst databaseRules = await promptLoader.loadDatabaseRulesForType(sourceType);\n\n\tconst prompt = `You are a SQL expert. Fix the following SQL query that failed execution.\n\n## Database Schema\n${sourceSchema}\n\n## Database-Specific SQL Rules\n${databaseRules}\n\n## Component Context\n- Component Name: ${component.name}\n- Component Type: ${component.type}\n- Title: ${component.props?.title || 'N/A'}\n\n## Failed Query\n\\`\\`\\`sql\n${failedSql}\n\\`\\`\\`\n\n## Error Message\n${errorMessage}\n\n## Instructions\n1. Analyze the error message and identify what caused the query to fail\n2. Fix the query to resolve the error while preserving the original intent\n3. Ensure the fixed query follows the database-specific SQL rules above\n4. Return ONLY the fixed SQL query, no explanations or markdown\n\nFixed SQL query:`;\n\n\tconst response = await LLM.text(\n\t\t{\n\t\t\tsys: 'You are a SQL expert. Return only the fixed SQL query with no additional text, explanations, or markdown formatting.',\n\t\t\tuser: prompt,\n\t\t},\n\t\t{\n\t\t\tmodel: 'anthropic/claude-haiku-4-5-20251001',\n\t\t\tmaxTokens: 2048,\n\t\t\ttemperature: 0,\n\t\t\tapiKey,\n\t\t}\n\t);\n\n\t// Clean up response\n\tlet fixedQuery = response.trim();\n\tfixedQuery = fixedQuery.replace(/^```sql\\s*/i, '').replace(/\\s*```$/i, '');\n\tfixedQuery = fixedQuery.replace(/^```\\s*/i, '').replace(/\\s*```$/i, '');\n\n\tconst { query: validatedQuery } = validateAndFixSqlQuery(fixedQuery);\n\treturn validatedQuery;\n}\n","/**\n * ScriptRecipeStore — injected metadata backend for the script flow.\n *\n * The SDK is standalone (no DB dependency). The backend implements this\n * interface over Postgres (full-text search + atomic counters) and injects it\n * via `collections['script-recipes']`, exactly like `collections['source-embeddings']`.\n * `ScriptStore` consumes it for all METADATA operations while keeping the\n * executable body on disk as scripts-store/<fileBase>.ts.\n *\n * All metadata rows are plain JSON (no scriptBody — that lives on disk).\n * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md (#1, #3, #7).\n */\n\nimport type { ScriptParameter, ScriptComponentSpec } from './types';\n\n/** One recipe's metadata as stored in Postgres (mirrors the script_recipes table). */\nexport interface ScriptRecipeMetaRow {\n\tid: string;\n\tprojectId?: string | null;\n\tversion: number;\n\tname: string;\n\tintentDescription: string;\n\ttags: string[] | null;\n\tcreatedFrom: string | null;\n\tsourceIds: string[] | null;\n\ttables: string[] | null;\n\tparameters: ScriptParameter[] | null;\n\tcomponents?: ScriptComponentSpec[] | null;\n\tfileBase: string;\n\tbodyHash?: string | null;\n\tsuccessCount: number;\n\tfailureCount: number;\n\tlastUsed: string | null;\n\tparentId?: string | null;\n\tforkDepth?: number | null;\n\tforkReason?: string | null;\n\tstatus: 'draft' | 'verified' | string;\n\tturnId?: string | null;\n\tlastError?: {\n\t\tphase: 'compile' | 'runtime' | 'timeout' | 'ipc';\n\t\tmessage: string;\n\t\tat: string;\n\t\tattempt: number;\n\t} | null;\n\tcreatedAt?: string | null;\n\tupdatedAt?: string | null;\n}\n\nexport interface ScriptRecipeStore {\n\t/** FTS shortlist of healthy verified recipes for the matcher (metadata only). */\n\tsearch(params: { prompt: string; projectId?: string; limit?: number }): Promise<ScriptRecipeMetaRow[]>;\n\t/** Fetch one recipe by id (any status). */\n\tgetById(id: string): Promise<ScriptRecipeMetaRow | null>;\n\t/** Count healthy verified recipes (drives the \"any scripts?\" gate). */\n\tcount(params?: { projectId?: string }): Promise<number>;\n\t/** Insert or update a recipe row (keyed by id). */\n\tupsert(row: ScriptRecipeMetaRow): Promise<void>;\n\t/** Atomically bump counters / last-used. */\n\tupdateStats(id: string, patch: { successDelta?: number; failureDelta?: number; lastUsed?: string }): Promise<void>;\n\t/** Flip a draft to verified, applying provenance + optional fork lineage. */\n\tpromote(id: string, patch: {\n\t\tsourceIds: string[];\n\t\ttables: string[];\n\t\tfileBase?: string;\n\t\tparentId?: string;\n\t\tforkDepth?: number;\n\t\tforkReason?: string;\n\t\tcomponents?: ScriptComponentSpec[];\n\t}): Promise<ScriptRecipeMetaRow | null>;\n\t/** Stamp a draft's last execution error. */\n\trecordDraftError(id: string, err: { phase: string; message: string; attempt: number; at: string }): Promise<void>;\n\t/** Delete a recipe row (body file removed separately). */\n\tremove(id: string): Promise<void>;\n\t/** True if `fileBase` is taken by a different recipe in this project. */\n\tfileBaseTaken(fileBase: string, excludeId: string, projectId?: string): Promise<boolean>;\n}\n\n/** Pull the injected store off the collections bag (or null if not wired). */\nexport function resolveScriptRecipeStore(collections: any): ScriptRecipeStore | null {\n\tconst s = collections?.['script-recipes'];\n\tif (s && typeof s.search === 'function' && typeof s.getById === 'function') {\n\t\treturn s as ScriptRecipeStore;\n\t}\n\treturn null;\n}\n","/**\n * ScriptStore — Postgres metadata + on-disk body for script recipes.\n *\n * Split of responsibilities:\n * - METADATA → injected `ScriptRecipeStore` (Postgres FTS + atomic counters),\n * resolved from `collections['script-recipes']`.\n * - BODY → scripts-store/<fileBase>.ts, editable in your IDE. Written\n * atomically (temp + rename); `bodyHash` (sha256) detects edits.\n *\n * The old \"read every file every turn + send the whole catalog to the LLM\"\n * matcher is gone — matching is `store.search(prompt)` (FTS shortlist). The\n * draft/verified filename dance is gone too: `status` is a DB column and the\n * file keeps a stable `<fileBase>.ts` name across promotion.\n *\n * When no metadata store is injected, the store degrades to a safe no-op\n * (count 0 → script flow disabled) instead of crashing.\n *\n * See backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md.\n */\n\nimport type { ScriptRecipe, ScriptParameter, ScriptComponentSpec } from './types';\nimport type { ScriptRecipeStore, ScriptRecipeMetaRow } from './script-metadata-store';\nimport { resolveScriptRecipeStore } from './script-metadata-store';\nimport { logger } from '../../utils/logger';\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport { createHash, randomBytes } from 'crypto';\n\nconst DEFAULT_STORE_DIR = 'scripts-store';\n\nexport interface SaveDraftInput {\n\t/** Reuse an existing draft (retry); omit to mint a new one. */\n\trecipeId?: string;\n\t/** Per-turn unique suffix, stable across retries within the turn. */\n\tturnId: string;\n\tname: string;\n\tintentDescription: string;\n\ttags: string[];\n\tparameters: ScriptParameter[];\n\tscriptBody: string;\n\tcreatedFrom: string;\n}\n\nexport interface PromoteToVerifiedInput {\n\tsourceIds: string[];\n\ttables: string[];\n\tparentId?: string;\n\tforkDepth?: number;\n\tforkReason?: string;\n\tcomponents?: ScriptComponentSpec[];\n}\n\nexport interface ScriptStoreOptions {\n\t/** Explicit metadata store, or resolved from `collections['script-recipes']`. */\n\tstore?: ScriptRecipeStore | null;\n\tcollections?: any;\n\t/** Body directory (defaults to <cwd>/scripts-store). */\n\tbaseDir?: string;\n\t/** Project scope stamped on every row. */\n\tprojectId?: string;\n}\n\n/**\n * Normalize a scriptBody into the on-disk form (strip a leading comment block,\n * ensure `export async function getData`). Exported for MainAgent.\n */\nexport function normalizeScriptBody(scriptBody: string): string {\n\tlet i = 0;\n\tconst n = scriptBody.length;\n\twhile (i < n) {\n\t\twhile (i < n && /\\s/.test(scriptBody[i])) i++;\n\t\tif (i >= n) break;\n\t\tif (scriptBody.startsWith('//', i)) {\n\t\t\tconst nl = scriptBody.indexOf('\\n', i);\n\t\t\ti = nl === -1 ? n : nl + 1;\n\t\t\tcontinue;\n\t\t}\n\t\tif (scriptBody.startsWith('/*', i)) {\n\t\t\tconst end = scriptBody.indexOf('*/', i + 2);\n\t\t\ti = end === -1 ? n : end + 2;\n\t\t\tcontinue;\n\t\t}\n\t\tbreak;\n\t}\n\tlet cleanBody = scriptBody.slice(i);\n\n\t// Ensure `getData` is exported — the bootstrap loads it via `mod.getData`.\n\t// The model doesn't always emit `export`, and `getData` is NOT always the\n\t// first declaration (imports, helper functions, or a `const getData = ...`\n\t// arrow form can precede it). If it's already exported in any form, leave\n\t// the body untouched (it may legitimately have other declarations). Only\n\t// when no export of `getData` exists do we add one, matching the\n\t// declaration wherever it sits.\n\tconst alreadyExported =\n\t\t/\\bexport\\s+(async\\s+)?function\\s+getData\\b/.test(cleanBody) ||\n\t\t/\\bexport\\s+(const|let|var)\\s+getData\\b/.test(cleanBody) ||\n\t\t/\\bexport\\s+default\\b/.test(cleanBody) ||\n\t\t/\\bexport\\s*\\{[^}]*\\bgetData\\b/.test(cleanBody);\n\n\tif (!alreadyExported) {\n\t\tconst fnDecl = /(^|\\n)([ \\t]*)(async\\s+)?function\\s+getData\\b/;\n\t\tconst varDecl = /(^|\\n)([ \\t]*)(const|let|var)\\s+getData\\b/;\n\t\tif (fnDecl.test(cleanBody)) {\n\t\t\tcleanBody = cleanBody.replace(fnDecl, (_m, lead, ws, asyncKw) =>\n\t\t\t\t`${lead}${ws}export ${asyncKw || ''}function getData`,\n\t\t\t);\n\t\t} else if (varDecl.test(cleanBody)) {\n\t\t\tcleanBody = cleanBody.replace(varDecl, (_m, lead, ws, kw) => `${lead}${ws}export ${kw} getData`);\n\t\t} else if (/\\bgetData\\b/.test(cleanBody)) {\n\t\t\t// Identifier is referenced but in a form we couldn't rewrite — export\n\t\t\t// it explicitly so the bootstrap can still pick it up.\n\t\t\tcleanBody = `${cleanBody.replace(/\\s*$/, '')}\\n\\nexport { getData };\\n`;\n\t\t}\n\t}\n\n\t// \\d, \\w, \\s etc. inside JS template literals lose their backslash at runtime\n\t// (unrecognized escape sequences are silently dropped). Double them so the\n\t// on-disk .ts file produces the correct regex string when tsx executes it.\n\t// Only targets regex metacharacters — valid JS escapes (\\n, \\t, \\r, etc.) are left alone.\n\tcleanBody = cleanBody.replace(/(?<!\\\\)\\\\([dwsDWS])/g, '\\\\\\\\$1');\n\n\treturn cleanBody;\n}\n\nexport class ScriptStore {\n\tprivate store: ScriptRecipeStore | null;\n\tprivate storeDir: string;\n\tprivate projectId?: string;\n\n\tconstructor(opts?: ScriptStoreOptions) {\n\t\tthis.store = opts?.store ?? resolveScriptRecipeStore(opts?.collections) ?? null;\n\t\tthis.storeDir = opts?.baseDir || path.join(process.cwd(), DEFAULT_STORE_DIR);\n\t\tthis.projectId = opts?.projectId;\n\t\tif (!this.store) {\n\t\t\tlogger.warn('[ScriptStore] No script-recipes metadata store injected — script reuse disabled this run.');\n\t\t}\n\t}\n\n\t/** Whether a metadata store is wired (matcher / authoring are gated on this). */\n\thasStore(): boolean {\n\t\treturn this.store !== null;\n\t}\n\n\t// ============================================\n\t// Read\n\t// ============================================\n\n\t/** Number of healthy verified recipes (gates the script-matching path). */\n\tasync count(): Promise<number> {\n\t\tif (!this.store) return 0;\n\t\ttry {\n\t\t\treturn await this.store.count({ projectId: this.projectId });\n\t\t} catch (err) {\n\t\t\tlogger.warn(`[ScriptStore] count failed: ${err}`);\n\t\t\treturn 0;\n\t\t}\n\t}\n\n\t/**\n\t * FTS shortlist for the matcher (metadata only — bodies are loaded lazily by\n\t * `get()` once the LLM picks one). Returns verified, healthy recipes ranked\n\t * by relevance.\n\t */\n\tasync search(prompt: string, limit?: number): Promise<ScriptRecipe[]> {\n\t\tif (!this.store) return [];\n\t\ttry {\n\t\t\tconst rows = await this.store.search({ prompt, projectId: this.projectId, limit });\n\t\t\treturn rows.map(r => this.rowToRecipe(r, '')); // body omitted for the catalog\n\t\t} catch (err) {\n\t\t\tlogger.warn(`[ScriptStore] search failed: ${err}`);\n\t\t\treturn [];\n\t\t}\n\t}\n\n\t/** Fetch one recipe by id with its body loaded from disk. */\n\tasync get(id: string): Promise<ScriptRecipe | null> {\n\t\tif (!this.store) return null;\n\t\ttry {\n\t\t\tconst row = await this.store.getById(id);\n\t\t\tif (!row) return null;\n\t\t\tconst body = this.readBody(row.fileBase);\n\t\t\treturn this.rowToRecipe(row, body);\n\t\t} catch (err) {\n\t\t\tlogger.warn(`[ScriptStore] get(${id}) failed: ${err}`);\n\t\t\treturn null;\n\t\t}\n\t}\n\n\t// ============================================\n\t// Write\n\t// ============================================\n\n\t/** Create or update a recipe (metadata upsert + body write when changed). */\n\tasync save(recipe: ScriptRecipe): Promise<void> {\n\t\tif (!this.store) return;\n\t\ttry {\n\t\t\tif (!recipe.fileBase) {\n\t\t\t\trecipe.fileBase = await this.computeFileBase(recipe.name, recipe.id);\n\t\t\t}\n\t\t\tif (recipe.scriptBody) {\n\t\t\t\tconst normalized = normalizeScriptBody(recipe.scriptBody);\n\t\t\t\tconst hash = this.hash(normalized);\n\t\t\t\tif (hash !== recipe.bodyHash) {\n\t\t\t\t\tthis.writeBody(recipe.fileBase, normalized);\n\t\t\t\t\trecipe.bodyHash = hash;\n\t\t\t\t}\n\t\t\t}\n\t\t\trecipe.updatedAt = new Date().toISOString();\n\t\t\tawait this.store.upsert(this.recipeToRow(recipe));\n\t\t\tlogger.info(`[ScriptStore] Saved \"${recipe.name}\" (${recipe.id})`);\n\t\t} catch (err) {\n\t\t\tlogger.warn(`[ScriptStore] save(\"${recipe.name}\") failed: ${err}`);\n\t\t}\n\t}\n\n\t/**\n\t * Persist (or update) a draft. Within a turn, retries that pass the same\n\t * `recipeId` overwrite the same row + file; a fresh `recipeId` mints a new\n\t * draft. The body is visible at scripts-store/<fileBase>.ts immediately.\n\t */\n\tasync saveDraft(input: SaveDraftInput): Promise<ScriptRecipe> {\n\t\tif (!this.store) {\n\t\t\tthrow new Error('[ScriptStore] saveDraft called with no metadata store injected');\n\t\t}\n\t\tconst now = new Date().toISOString();\n\t\tconst existing = input.recipeId ? await this.store.getById(input.recipeId) : null;\n\t\tconst reuse = existing && existing.status === 'draft' && existing.turnId === input.turnId;\n\n\t\tconst id = reuse ? existing!.id : `script_${Date.now()}_${randomBytes(3).toString('hex')}`;\n\t\tconst fileBase = reuse ? existing!.fileBase : await this.computeFileBase(input.name, id);\n\n\t\tconst normalized = normalizeScriptBody(input.scriptBody);\n\t\tconst bodyHash = this.hash(normalized);\n\t\tthis.writeBody(fileBase, normalized);\n\n\t\tconst recipe: ScriptRecipe = {\n\t\t\tid,\n\t\t\tversion: existing?.version ?? 1,\n\t\t\tname: input.name,\n\t\t\tintentDescription: input.intentDescription,\n\t\t\ttags: input.tags,\n\t\t\tsourceIds: existing?.sourceIds ?? [],\n\t\t\ttables: existing?.tables ?? [],\n\t\t\tparameters: input.parameters,\n\t\t\tscriptBody: normalized,\n\t\t\tfileBase,\n\t\t\tbodyHash,\n\t\t\tprojectId: this.projectId,\n\t\t\tsuccessCount: existing?.successCount ?? 0,\n\t\t\tfailureCount: existing?.failureCount ?? 0,\n\t\t\tlastUsed: now,\n\t\t\tcreatedFrom: input.createdFrom,\n\t\t\tcreatedAt: existing?.createdAt ?? now,\n\t\t\tupdatedAt: now,\n\t\t\tforkDepth: existing?.forkDepth ?? 0,\n\t\t\tstatus: 'draft',\n\t\t\tturnId: input.turnId,\n\t\t};\n\n\t\tawait this.store.upsert(this.recipeToRow(recipe));\n\t\tlogger.info(`[ScriptStore] ${reuse ? 'Updated' : 'Saved'} draft \"${recipe.name}\" (${id}) at ${this.getScriptPath(recipe)}`);\n\t\treturn recipe;\n\t}\n\n\t/** Stamp a draft's last execution error (metadata only). */\n\tasync recordDraftError(\n\t\trecipeId: string,\n\t\terr: { phase: 'compile' | 'runtime' | 'timeout' | 'ipc'; message: string; attempt: number },\n\t): Promise<void> {\n\t\tif (!this.store) return;\n\t\ttry {\n\t\t\tawait this.store.recordDraftError(recipeId, { ...err, at: new Date().toISOString() });\n\t\t} catch (e) {\n\t\t\tlogger.warn(`[ScriptStore] recordDraftError failed: ${e}`);\n\t\t}\n\t}\n\n\t/**\n\t * Promote a successfully-executed draft into a verified script.\n\t * The on-disk body already exists at <fileBase>.ts (written at write_script\n\t * time) and keeps its name — only the DB row flips status + provenance.\n\t */\n\tasync promoteToVerified(recipeId: string, input: PromoteToVerifiedInput): Promise<ScriptRecipe | null> {\n\t\tif (!this.store) return null;\n\t\ttry {\n\t\t\tconst row = await this.store.promote(recipeId, {\n\t\t\t\tsourceIds: input.sourceIds,\n\t\t\t\ttables: input.tables,\n\t\t\t\tparentId: input.parentId,\n\t\t\t\tforkDepth: input.forkDepth,\n\t\t\t\tforkReason: input.forkReason,\n\t\t\t\tcomponents: input.components,\n\t\t\t});\n\t\t\tif (!row) {\n\t\t\t\tlogger.warn(`[ScriptStore] promoteToVerified: recipe \"${recipeId}\" not found`);\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\tlogger.info(`[ScriptStore] Promoted \"${row.name}\" (${recipeId}) → verified`);\n\t\t\treturn this.rowToRecipe(row, this.readBody(row.fileBase));\n\t\t} catch (err) {\n\t\t\tlogger.warn(`[ScriptStore] promoteToVerified failed: ${err}`);\n\t\t\treturn null;\n\t\t}\n\t}\n\n\t/**\n\t * Drop a draft (row + body file). MainAgent calls this at end-of-turn when a\n\t * draft was authored but never verified — failed drafts are never matched, so\n\t * deleting them immediately avoids unbounded accumulation (#5). No-op if the\n\t * recipe isn't a draft (so a promoted/verified script is never removed here).\n\t */\n\tasync discardDraft(recipeId: string): Promise<void> {\n\t\tawait this.removeById(recipeId, 'draft');\n\t}\n\n\t/** Delete a recipe (row + body file). */\n\tasync delete(id: string): Promise<void> {\n\t\tawait this.removeById(id);\n\t}\n\n\t/** Record a successful execution (atomic counter bump). */\n\tasync recordSuccess(id: string): Promise<void> {\n\t\tif (!this.store) return;\n\t\ttry {\n\t\t\tawait this.store.updateStats(id, { successDelta: 1, lastUsed: new Date().toISOString() });\n\t\t} catch (err) {\n\t\t\tlogger.warn(`[ScriptStore] recordSuccess failed: ${err}`);\n\t\t}\n\t}\n\n\t/** Record a failed execution (atomic counter bump). */\n\tasync recordFailure(id: string): Promise<void> {\n\t\tif (!this.store) return;\n\t\ttry {\n\t\t\tawait this.store.updateStats(id, { failureDelta: 1, lastUsed: new Date().toISOString() });\n\t\t} catch (err) {\n\t\t\tlogger.warn(`[ScriptStore] recordFailure failed: ${err}`);\n\t\t}\n\t}\n\n\t// ============================================\n\t// Paths (sync — body lives on disk)\n\t// ============================================\n\n\t/** Absolute path to the .ts body for a recipe (used by the runner/MainAgent). */\n\tgetScriptPath(recipe: ScriptRecipe): string {\n\t\tconst base = recipe.fileBase || this.toSlug(recipe.name);\n\t\treturn path.join(this.storeDir, `${base}.ts`);\n\t}\n\n\t// ============================================\n\t// Internals\n\t// ============================================\n\n\tprivate async removeById(id: string, requireStatus?: 'draft'): Promise<void> {\n\t\tif (!this.store) return;\n\t\ttry {\n\t\t\tconst row = await this.store.getById(id);\n\t\t\tif (!row) return;\n\t\t\tif (requireStatus && row.status !== requireStatus) return;\n\t\t\tawait this.store.remove(id);\n\t\t\tthis.unlinkBody(row.fileBase);\n\t\t\tlogger.info(`[ScriptStore] Removed \"${row.name}\" (${id})`);\n\t\t} catch (err) {\n\t\t\tlogger.warn(`[ScriptStore] remove(${id}) failed: ${err}`);\n\t\t}\n\t}\n\n\tprivate rowToRecipe(row: ScriptRecipeMetaRow, body: string): ScriptRecipe {\n\t\treturn {\n\t\t\tid: row.id,\n\t\t\tversion: row.version ?? 1,\n\t\t\tname: row.name,\n\t\t\tintentDescription: row.intentDescription,\n\t\t\ttags: row.tags ?? [],\n\t\t\tsourceIds: row.sourceIds ?? [],\n\t\t\ttables: row.tables ?? [],\n\t\t\tparameters: (row.parameters as ScriptParameter[]) ?? [],\n\t\t\tscriptBody: body,\n\t\t\tfileBase: row.fileBase,\n\t\t\tbodyHash: row.bodyHash ?? undefined,\n\t\t\tprojectId: row.projectId ?? undefined,\n\t\t\tsuccessCount: row.successCount ?? 0,\n\t\t\tfailureCount: row.failureCount ?? 0,\n\t\t\tlastUsed: row.lastUsed ?? new Date().toISOString(),\n\t\t\tcreatedFrom: row.createdFrom ?? '',\n\t\t\tcreatedAt: row.createdAt ?? new Date().toISOString(),\n\t\t\tupdatedAt: row.updatedAt ?? new Date().toISOString(),\n\t\t\tparentId: row.parentId ?? undefined,\n\t\t\tforkDepth: typeof row.forkDepth === 'number' ? row.forkDepth : 0,\n\t\t\tforkReason: row.forkReason ?? undefined,\n\t\t\tcomponents: (row.components as ScriptComponentSpec[]) ?? undefined,\n\t\t\tstatus: (row.status as 'draft' | 'verified') ?? 'verified',\n\t\t\tturnId: row.turnId ?? undefined,\n\t\t\tlastError: row.lastError ?? undefined,\n\t\t};\n\t}\n\n\tprivate recipeToRow(recipe: ScriptRecipe): ScriptRecipeMetaRow {\n\t\treturn {\n\t\t\tid: recipe.id,\n\t\t\tprojectId: recipe.projectId ?? this.projectId ?? null,\n\t\t\tversion: recipe.version ?? 1,\n\t\t\tname: recipe.name,\n\t\t\tintentDescription: recipe.intentDescription,\n\t\t\ttags: recipe.tags ?? null,\n\t\t\tcreatedFrom: recipe.createdFrom ?? null,\n\t\t\tsourceIds: recipe.sourceIds ?? null,\n\t\t\ttables: recipe.tables ?? null,\n\t\t\tparameters: recipe.parameters ?? null,\n\t\t\tcomponents: recipe.components ?? null,\n\t\t\tfileBase: recipe.fileBase || this.toSlug(recipe.name),\n\t\t\tbodyHash: recipe.bodyHash ?? null,\n\t\t\tsuccessCount: recipe.successCount ?? 0,\n\t\t\tfailureCount: recipe.failureCount ?? 0,\n\t\t\tlastUsed: recipe.lastUsed ?? null,\n\t\t\tparentId: recipe.parentId ?? null,\n\t\t\tforkDepth: typeof recipe.forkDepth === 'number' ? recipe.forkDepth : 0,\n\t\t\tforkReason: recipe.forkReason ?? null,\n\t\t\tstatus: recipe.status ?? 'verified',\n\t\t\tturnId: recipe.turnId ?? null,\n\t\t\tlastError: recipe.lastError ?? null,\n\t\t};\n\t}\n\n\t/** slug of name, with a short id suffix when the bare slug is already taken. */\n\tprivate async computeFileBase(name: string, id: string): Promise<string> {\n\t\tconst slug = this.toSlug(name);\n\t\tif (this.store) {\n\t\t\ttry {\n\t\t\t\tconst taken = await this.store.fileBaseTaken(slug, id, this.projectId);\n\t\t\t\tif (taken) return `${slug}-${id.slice(-6)}`;\n\t\t\t} catch { /* fall through to bare slug */ }\n\t\t}\n\t\treturn slug;\n\t}\n\n\tprivate toSlug(name: string): string {\n\t\treturn name\n\t\t\t.toLowerCase()\n\t\t\t.replace(/[^a-z0-9]+/g, '-')\n\t\t\t.replace(/^-+|-+$/g, '')\n\t\t\t|| 'unnamed-script';\n\t}\n\n\tprivate hash(body: string): string {\n\t\treturn createHash('sha256').update(body).digest('hex');\n\t}\n\n\tprivate bodyPath(fileBase: string): string {\n\t\treturn path.join(this.storeDir, `${fileBase}.ts`);\n\t}\n\n\tprivate readBody(fileBase: string): string {\n\t\ttry {\n\t\t\treturn fs.readFileSync(this.bodyPath(fileBase), 'utf-8');\n\t\t} catch (err) {\n\t\t\tlogger.warn(`[ScriptStore] body file ${fileBase}.ts missing/unreadable: ${err}`);\n\t\t\treturn '';\n\t\t}\n\t}\n\n\t/** Atomic body write (temp + rename) so concurrent reads never see a partial file. */\n\tprivate writeBody(fileBase: string, normalizedBody: string): void {\n\t\tif (!fs.existsSync(this.storeDir)) fs.mkdirSync(this.storeDir, { recursive: true });\n\t\tconst finalPath = this.bodyPath(fileBase);\n\t\tconst tmpPath = `${finalPath}.tmp-${randomBytes(4).toString('hex')}`;\n\t\tfs.writeFileSync(tmpPath, normalizedBody);\n\t\tfs.renameSync(tmpPath, finalPath);\n\t}\n\n\tprivate unlinkBody(fileBase: string): void {\n\t\ttry {\n\t\t\tconst p = this.bodyPath(fileBase);\n\t\t\tif (fs.existsSync(p)) fs.unlinkSync(p);\n\t\t} catch (err) {\n\t\t\tlogger.warn(`[ScriptStore] failed to delete body ${fileBase}.ts: ${err}`);\n\t\t}\n\t}\n}\n","/**\n * ScriptMatcher — LLM-Based Script Matching + Parameter Extraction\n *\n * Uses ONE LLM call to:\n * 1. Pick the best matching script from the library (or \"none\")\n * 2. Extract parameter values from the user question\n *\n * Why LLM over embeddings:\n * - Embeddings capture topic similarity (\"overstock\" ≈ \"inventory\" ≈ \"revenue\")\n * but can't distinguish structurally different questions about the same domain\n * - LLM understands that \"overstock by warehouse\" needs a different script than\n * \"revenue by warehouse\" even though they're semantically close\n * - One call does both matching AND parameter extraction\n *\n * When script library grows past ~50, add an embedding pre-filter\n * (ChromaDB narrows to top 10 → LLM picks from those 10).\n */\n\nimport { LLM } from '../../llm';\nimport { promptLoader } from '../prompt-loader';\nimport { getCurrentDateTimeForPrompt } from '../../utils/datetime';\nimport type { ScriptRecipe, ScriptMatch, MatchTier } from './types';\nimport type { ScriptStore } from './script-store';\nimport { logger } from '../../utils/logger';\n\nconst MAX_FAILURE_RATE = 0.2;\n\nexport class ScriptMatcher {\n\tprivate store: ScriptStore;\n\n\tconstructor(store: ScriptStore) {\n\t\tthis.store = store;\n\t}\n\n\t/**\n\t * Find the best matching script for a user question.\n\t * Uses ONE LLM call that picks the script AND extracts parameters.\n\t * Returns null if no script matches.\n\t */\n\tasync match(\n\t\tuserPrompt: string,\n\t\tapiKey?: string,\n\t\tmodel?: string,\n\t): Promise<ScriptMatch | null> {\n\t\t// FTS shortlist — Postgres ranks the healthy verified recipes by relevance\n\t\t// to the prompt and returns the top-K. The catalog handed to the LLM is now\n\t\t// O(K), not O(all scripts). Health-filtering happens in SQL; we keep a light\n\t\t// client-side guard as defense in depth.\n\t\tconst recipes = await this.store.search(userPrompt);\n\t\tif (recipes.length === 0) return null;\n\n\t\tconst healthyRecipes = recipes.filter(r => {\n\t\t\tconst total = r.successCount + r.failureCount;\n\t\t\treturn total < 5 || r.failureCount / total <= MAX_FAILURE_RATE;\n\t\t});\n\n\t\tif (healthyRecipes.length === 0) {\n\t\t\tlogger.info(`[ScriptMatcher] No healthy candidates among ${recipes.length} FTS matches — skipping`);\n\t\t\treturn null;\n\t\t}\n\n\t\t// Build the script catalog for the LLM\n\t\tconst scriptCatalog = this.buildScriptCatalog(healthyRecipes);\n\n\t\ttry {\n\t\t\t// Load prompts from file system → falls back to hardcoded in prompts.ts\n\t\t\tconst prompts = await promptLoader.loadPrompts('script-match', {\n\t\t\t\tSCRIPT_CATALOG: scriptCatalog,\n\t\t\t\tUSER_PROMPT: userPrompt,\n\t\t\t\tCURRENT_DATE: getCurrentDateTimeForPrompt(),\n\t\t\t});\n\n\t\t\tlogger.info(`[ScriptMatcher] Matching against ${healthyRecipes.length} scripts`);\n\n\t\t\tconst response = await LLM.stream<any>(\n\t\t\t\t{ sys: prompts.system, user: prompts.user },\n\t\t\t\t{\n\t\t\t\t\tmodel: model || undefined,\n\t\t\t\t\tmaxTokens: 500,\n\t\t\t\t\ttemperature: 0,\n\t\t\t\t\tapiKey,\n\t\t\t\t},\n\t\t\t\ttrue, // Parse as JSON\n\t\t\t);\n\n\t\t\t// Resolve tier — accept the new `tier` field, fall back to legacy\n\t\t\t// `confidence`-based parsing if an older prompt happens to be loaded.\n\t\t\tconst rawTier = (response?.tier || '').toString().toLowerCase();\n\t\t\tlet tier: MatchTier;\n\t\t\tif (rawTier === 'high' || rawTier === 'near' || rawTier === 'none') {\n\t\t\t\ttier = rawTier;\n\t\t\t} else if (response?.scriptId && response.scriptId !== 'none') {\n\t\t\t\t// Legacy fallback: treat any matched script as 'high'\n\t\t\t\ttier = response.confidence === 'low' ? 'none' : 'high';\n\t\t\t} else {\n\t\t\t\ttier = 'none';\n\t\t\t}\n\n\t\t\tconst reasoning = response?.reasoning || 'unknown';\n\n\t\t\tif (tier === 'none' || !response?.scriptId || response.scriptId === 'none') {\n\t\t\t\tlogger.info(`[ScriptMatcher] No match. Reason: ${reasoning}`);\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst recipe = await this.store.get(response.scriptId);\n\t\t\tif (!recipe) {\n\t\t\t\tlogger.warn(`[ScriptMatcher] LLM returned unknown script ID: \"${response.scriptId}\"`);\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tif (tier === 'near') {\n\t\t\t\tconst gaps = Array.isArray(response.gaps) ? response.gaps : [];\n\t\t\t\tconst hint = response.modificationHint || '(no hint provided)';\n\t\t\t\tlogger.info(\n\t\t\t\t\t`[ScriptMatcher] Near match \"${recipe.name}\". ` +\n\t\t\t\t\t`Gaps: ${gaps.length ? gaps.join('; ') : '(none listed)'}. ` +\n\t\t\t\t\t`Hint: ${hint}.`\n\t\t\t\t);\n\t\t\t\treturn {\n\t\t\t\t\trecipe,\n\t\t\t\t\ttier: 'near',\n\t\t\t\t\tsimilarity: 0.75,\n\t\t\t\t\t// Legacy `confidence` is kept for backwards compatibility with any\n\t\t\t\t\t// caller still reading it; treat 'near' as 'medium' confidence.\n\t\t\t\t\tconfidence: 'medium',\n\t\t\t\t\tgaps,\n\t\t\t\t\tmodificationHint: response.modificationHint,\n\t\t\t\t\treasoning,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// tier === 'high'\n\t\t\tlogger.info(`[ScriptMatcher] Matched \"${recipe.name}\" (tier: high). Reason: ${reasoning}`);\n\n\t\t\treturn {\n\t\t\t\trecipe,\n\t\t\t\ttier: 'high',\n\t\t\t\tsimilarity: 0.95,\n\t\t\t\tconfidence: 'high',\n\t\t\t\textractedParams: response.params || {},\n\t\t\t\treasoning,\n\t\t\t};\n\n\t\t} catch (error) {\n\t\t\tconst msg = error instanceof Error ? error.message : String(error);\n\t\t\tlogger.error(`[ScriptMatcher] LLM matching failed: ${msg}`);\n\t\t\treturn null;\n\t\t}\n\t}\n\n\t/**\n\t * Build the script catalog string for the LLM prompt.\n\t * Each script gets: index, ID, name, description, and parameter definitions.\n\t */\n\tprivate buildScriptCatalog(recipes: ScriptRecipe[]): string {\n\t\treturn recipes.map((r, idx) => {\n\t\t\tconst paramList = r.parameters.length > 0\n\t\t\t\t? r.parameters.map(p => {\n\t\t\t\t\tlet desc = `${p.name} (${p.type}`;\n\t\t\t\t\tif (!p.required) desc += ', optional';\n\t\t\t\t\tif (p.default !== undefined) desc += `, default: ${JSON.stringify(p.default)}`;\n\t\t\t\t\tif (p.enumValues) desc += `, values: ${Object.keys(p.enumValues).join('/')}`;\n\t\t\t\t\tdesc += `) — ${p.description}`;\n\t\t\t\t\treturn ` - ${desc}`;\n\t\t\t\t}).join('\\n')\n\t\t\t\t: ' (no parameters)';\n\n\t\t\treturn `${idx + 1}. [${r.id}] \"${r.name}\"\\n ${r.intentDescription}\\n Parameters:\\n${paramList}`;\n\t\t}).join('\\n\\n');\n\t}\n}\n","/**\n * Script Component Generator — Lightweight Component Suggestion\n *\n * Unlike the full agent component generator (generateAgentComponents), this:\n * - Reuses the script's proven query rows via queryId — writes NO new SQL\n * - Binds each component to ONE source (the script already did any cross-source\n * work in JS and returned a combined dataset — no federation at render time)\n * - Uses a simpler prompt — LLM picks component types and maps columns\n * - Faithfully represents the script's output; never re-derives or invents data\n *\n * Falls back to generateAgentComponents if this fails.\n */\n\nimport { LLM } from '../../llm';\nimport { promptLoader } from '../prompt-loader';\nimport { queryCache } from '../../utils/query-cache';\nimport { logger } from '../../utils/logger';\nimport type { Component } from '../../types';\nimport type { Action } from '../../threads/action';\nimport type { ScriptResult, ScriptComponentSpec } from './types';\nimport { TOOL_TRACKING_SAMPLE_ROWS } from '../constants';\nimport type { ExternalTool } from '../services/tool-executor-service';\n\nexport interface ScriptComponentResult {\n\tcomponents: Component[];\n\tlayoutTitle: string;\n\tlayoutDescription: string;\n\tactions: Action[];\n\t/**\n\t * Reusable, source-bound specs (no ephemeral queryId) for each component\n\t * that can be deterministically replayed. Persisted on the recipe so a\n\t * tier-high replay can rebind them to fresh queryIds without the LLM.\n\t * Components that can't be replayed cleanly (e.g. federation) are omitted.\n\t */\n\tcomponentSpecs: ScriptComponentSpec[];\n}\n\n// ============================================\n// Deterministic component validation + repair\n// ============================================\n\ntype ColType = 'NUMBER' | 'TEXT' | 'DATE' | 'BOOLEAN';\ninterface ColInfo {\n\tname: string;\n\ttype: ColType;\n\t/** True when every sampled non-null value is identical — a per-row invariant\n\t * (e.g. a \"total\" the script computed the same on every row). Such a column\n\t * must NOT be summed for a KPI — its single value IS the answer. */\n\tinvariant: boolean;\n}\n\n/** Infer column name + type + invariance from a sample of rows. */\nfunction inferColumns(rows: any[]): Map<string, ColInfo> {\n\tconst cols = new Map<string, ColInfo>();\n\tif (!Array.isArray(rows) || rows.length === 0) return cols;\n\tconst sample = rows.slice(0, 20);\n\tconst keys = new Set<string>();\n\tfor (const r of sample) {\n\t\tif (r && typeof r === 'object') for (const k of Object.keys(r)) keys.add(k);\n\t}\n\tfor (const key of keys) {\n\t\tlet type: ColType = 'TEXT';\n\t\tlet typed = false;\n\t\tconst distinct = new Set<string>();\n\t\tfor (const r of sample) {\n\t\t\tconst v = r?.[key];\n\t\t\tif (v === null || v === undefined) continue;\n\t\t\tif (!typed) {\n\t\t\t\tif (typeof v === 'number') type = 'NUMBER';\n\t\t\t\telse if (typeof v === 'boolean') type = 'BOOLEAN';\n\t\t\t\telse if (v instanceof Date) type = 'DATE';\n\t\t\t\telse if (typeof v === 'string' && v.trim() !== '' && !isNaN(Number(v))) type = 'NUMBER';\n\t\t\t\telse type = 'TEXT';\n\t\t\t\ttyped = true;\n\t\t\t}\n\t\t\tdistinct.add(v instanceof Date ? v.toISOString() : String(v));\n\t\t}\n\t\tcols.set(key.toLowerCase(), { name: key, type, invariant: distinct.size <= 1 });\n\t}\n\treturn cols;\n}\n\nfunction resolveCol(cols: Map<string, ColInfo>, key: any): ColInfo | undefined {\n\tif (key == null) return undefined;\n\treturn cols.get(String(key).toLowerCase());\n}\n\nfunction firstOfType(cols: Map<string, ColInfo>, type: ColType): ColInfo | undefined {\n\tfor (const c of cols.values()) if (c.type === type) return c;\n\treturn undefined;\n}\n\nconst ALL_KEY_NAMES = ['xAxisKey', 'yAxisKey', 'valueKey', 'nameKey', 'seriesKey', 'groupBy', 'aggregationField', 'categoryKey'];\nconst AVG_KEYWORDS = /\\b(average|avg|mean)\\b/i;\nconst COUNT_KEYWORDS = /\\b(count|number of|how many|# of|no\\.? of)\\b/i;\nconst SUM_KEYWORDS = /\\b(total|sum|overall|combined|aggregate|all)\\b/i;\n\n/**\n * Deterministically validate + repair one component's config against the real\n * columns of its bound data source. Rewrites every config key to the EXACT\n * column name (case-insensitive match), drops keys that reference non-existent\n * columns, enforces TEXT/NUMBER axis roles (with swap), and forces aggregation\n * on multi-row \"total/average/count\" KPI cards — otherwise the card silently\n * renders data[0]. Returns the repaired component, or null when it cannot be\n * rendered from the available columns (caller drops it).\n *\n * Frontends are already case-insensitive, but we canonicalize here so the\n * stored recipe (Phase 3) is correct and type-role fixes are baked in rather\n * than left to per-component runtime heuristics.\n */\nfunction validateAndRepairComponent(\n\tcomp: any,\n\tcomponentType: string,\n\tcols: Map<string, ColInfo>,\n\trowCount: number,\n): any | null {\n\tif (cols.size === 0) return comp; // no columns to validate against — leave untouched\n\tconst props = comp.props || (comp.props = {});\n\tconst cfg = props.config || (props.config = {});\n\n\t// Generic pass — rewrite every present key to its exact column name; drop unknowns.\n\tfor (const k of ALL_KEY_NAMES) {\n\t\tif (cfg[k] == null) continue;\n\t\tconst r = resolveCol(cols, cfg[k]);\n\t\tif (r) cfg[k] = r.name; else delete cfg[k];\n\t}\n\tif (Array.isArray(cfg.metricKeys)) {\n\t\tcfg.metricKeys = cfg.metricKeys.map((m: any) => resolveCol(cols, m)?.name).filter(Boolean);\n\t\tif (cfg.metricKeys.length === 0) delete cfg.metricKeys;\n\t}\n\n\tconst numeric = () => firstOfType(cols, 'NUMBER');\n\tconst category = () => firstOfType(cols, 'TEXT') || firstOfType(cols, 'DATE');\n\tconst hasMetrics = Array.isArray(cfg.metricKeys) && cfg.metricKeys.length > 0;\n\n\tswitch (componentType) {\n\t\tcase 'KPICard':\n\t\tcase 'GaugeChart': {\n\t\t\tconst vk = resolveCol(cols, cfg.valueKey) || numeric() || category();\n\t\t\tif (!vk) return null;\n\t\t\tcfg.valueKey = vk.name;\n\t\t\tconst title = String(props.title || '');\n\t\t\t// Multi-row KPI with an aggregate-sounding title must aggregate, else it\n\t\t\t// silently shows data[0]. BUT only sum/avg a column that actually VARIES\n\t\t\t// across rows — an invariant column (e.g. a per-device \"total\" the script\n\t\t\t// computed identically on every row) should display its value as-is, not\n\t\t\t// be summed. `count` ignores invariance (it counts rows).\n\t\t\tif (rowCount > 1 && !cfg.aggregation) {\n\t\t\t\tif (COUNT_KEYWORDS.test(title)) cfg.aggregation = 'count';\n\t\t\t\telse if (!vk.invariant) {\n\t\t\t\t\tif (AVG_KEYWORDS.test(title)) cfg.aggregation = 'avg';\n\t\t\t\t\telse if (SUM_KEYWORDS.test(title)) cfg.aggregation = 'sum';\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (cfg.aggregation === 'sum' || cfg.aggregation === 'avg') {\n\t\t\t\tconst af = resolveCol(cols, cfg.aggregationField);\n\t\t\t\tif (af && af.type === 'NUMBER') {\n\t\t\t\t\tcfg.aggregationField = af.name;\n\t\t\t\t} else {\n\t\t\t\t\tconst n = vk.type === 'NUMBER' ? vk : numeric();\n\t\t\t\t\tif (n) cfg.aggregationField = n.name;\n\t\t\t\t\telse { delete cfg.aggregation; delete cfg.aggregationField; }\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn comp;\n\t\t}\n\t\tcase 'PieChart':\n\t\tcase 'TreemapChart': {\n\t\t\tlet nameC = resolveCol(cols, cfg.nameKey);\n\t\t\tlet valC = resolveCol(cols, cfg.valueKey);\n\t\t\tif (nameC?.type === 'NUMBER' && valC && valC.type !== 'NUMBER') { const t = nameC; nameC = valC; valC = t; }\n\t\t\tif (!nameC) nameC = category();\n\t\t\tif (!valC || valC.type !== 'NUMBER') valC = numeric() || valC;\n\t\t\tif (!nameC || !valC) return null;\n\t\t\tcfg.nameKey = nameC.name;\n\t\t\tcfg.valueKey = valC.name;\n\t\t\treturn comp;\n\t\t}\n\t\tcase 'WaterfallChart': {\n\t\t\tconst catC = resolveCol(cols, cfg.categoryKey) || category();\n\t\t\tlet valC = resolveCol(cols, cfg.valueKey);\n\t\t\tif (!valC || valC.type !== 'NUMBER') valC = numeric() || valC;\n\t\t\tif (!catC || !valC) return null;\n\t\t\tcfg.categoryKey = catC.name;\n\t\t\tcfg.valueKey = valC.name;\n\t\t\treturn comp;\n\t\t}\n\t\tcase 'ScatterChart': {\n\t\t\t// Scatter plots numeric vs numeric.\n\t\t\tlet xC = resolveCol(cols, cfg.xAxisKey);\n\t\t\tlet yC = resolveCol(cols, cfg.yAxisKey);\n\t\t\tif (!xC || xC.type !== 'NUMBER') xC = numeric() || xC;\n\t\t\tif (!yC || yC.type !== 'NUMBER') {\n\t\t\t\t// pick a numeric column distinct from x if possible\n\t\t\t\tyC = [...cols.values()].find(c => c.type === 'NUMBER' && c.name !== xC?.name) || numeric() || yC;\n\t\t\t}\n\t\t\tif (!xC || !yC) return null;\n\t\t\tcfg.xAxisKey = xC.name;\n\t\t\tcfg.yAxisKey = yC.name;\n\t\t\treturn comp;\n\t\t}\n\t\tcase 'HeatmapChart': {\n\t\t\t// x = category, y = category, value = numeric.\n\t\t\tconst xC = resolveCol(cols, cfg.xAxisKey) || category();\n\t\t\tconst yC = resolveCol(cols, cfg.yAxisKey) || [...cols.values()].find(c => (c.type === 'TEXT' || c.type === 'DATE') && c.name !== xC?.name);\n\t\t\tlet valC = resolveCol(cols, cfg.valueKey);\n\t\t\tif (!valC || valC.type !== 'NUMBER') valC = numeric() || valC;\n\t\t\tif (!xC || !yC || !valC) return null;\n\t\t\tcfg.xAxisKey = xC.name;\n\t\t\tcfg.yAxisKey = yC.name;\n\t\t\tcfg.valueKey = valC.name;\n\t\t\treturn comp;\n\t\t}\n\t\tcase 'BarChart':\n\t\tcase 'LineChart': {\n\t\t\tlet xC = resolveCol(cols, cfg.xAxisKey);\n\t\t\tlet yC = resolveCol(cols, cfg.yAxisKey);\n\t\t\t// y must be numeric. If y is text and x is numeric, the LLM swapped them.\n\t\t\tif (!hasMetrics && xC?.type === 'NUMBER' && yC && yC.type !== 'NUMBER') { const t = xC; xC = yC; yC = t; }\n\t\t\t// LineChart is a time series — prefer a DATE column for the x-axis.\n\t\t\tif (!xC) xC = (componentType === 'LineChart' ? (firstOfType(cols, 'DATE') || category()) : category()) || firstOfType(cols, 'NUMBER');\n\t\t\tif (!xC) return null;\n\t\t\tcfg.xAxisKey = xC.name;\n\t\t\tif (!hasMetrics) {\n\t\t\t\tif (!yC || yC.type !== 'NUMBER') yC = numeric();\n\t\t\t\tif (!yC) return null;\n\t\t\t\tcfg.yAxisKey = yC.name;\n\t\t\t}\n\t\t\t// seriesKey (multi-line) must be a TEXT column distinct from x — else it\n\t\t\t// produces a broken/overlapping chart. Drop it if invalid; the generic\n\t\t\t// pass above already resolved it to an exact column name (or dropped it).\n\t\t\tif (cfg.seriesKey) {\n\t\t\t\tconst sC = resolveCol(cols, cfg.seriesKey);\n\t\t\t\tif (!sC || sC.type !== 'TEXT' || sC.name === cfg.xAxisKey) delete cfg.seriesKey;\n\t\t\t\telse cfg.seriesKey = sC.name;\n\t\t\t}\n\t\t\treturn comp;\n\t\t}\n\t\tcase 'RadarChart': {\n\t\t\tif (!hasMetrics) {\n\t\t\t\tconst vk = resolveCol(cols, cfg.valueKey) || numeric();\n\t\t\t\tif (!vk) return null;\n\t\t\t\tcfg.valueKey = vk.name;\n\t\t\t}\n\t\t\treturn comp;\n\t\t}\n\t\tdefault:\n\t\t\t// DataTable, MapView, MarkdownBlock, etc. — generic key fixing above is sufficient.\n\t\t\treturn comp;\n\t}\n}\n\n/**\n * Generate components using the lightweight script-components prompt.\n * Each component binds to one of the script's sources by sourceIndex.\n */\n/** Shared context for building a single script component (loop + early streamer). */\ninterface ScriptCompCtx {\n\tqueries: any[];\n\tqueryIds: Record<string, string>;\n\tcolumnsByIndex: Map<string, ColInfo>[];\n\tavailableComponents: Component[];\n}\n\n/** Stable identity used to dedup the early-streamed answer against the main loop. */\nfunction scriptCompKey(comp: any): string {\n\tconst id = comp?.componentId || comp?.componentName || '';\n\tconst src = comp?.sourceIndex ?? '';\n\treturn `${id}|${src}|${JSON.stringify(comp?.props?.config ?? comp?.config ?? {})}`;\n}\n\n/**\n * Extract the `answerComponent` object from a partial/streaming LLM response,\n * once `hasAnswerComponent: true` has appeared. Brace-matched JSON slice — same\n * technique as the agent generator's tryStreamAnswerComponent. Returns null\n * until the object is complete/parseable.\n */\nfunction extractAnswerComponentObject(text: string): any | null {\n\tconst hasMatch = text.match(/\"hasAnswerComponent\"\\s*:\\s*(true|false)/);\n\tif (!hasMatch || hasMatch[1] !== 'true') return null;\n\tconst startMatch = text.match(/\"answerComponent\"\\s*:\\s*\\{/);\n\tif (!startMatch) return null;\n\tconst startPos = startMatch.index! + startMatch[0].length - 1;\n\tlet depth = 0, inString = false, escapeNext = false, endPos = -1;\n\tfor (let i = startPos; i < text.length; i++) {\n\t\tconst c = text[i];\n\t\tif (escapeNext) { escapeNext = false; continue; }\n\t\tif (c === '\\\\') { escapeNext = true; continue; }\n\t\tif (c === '\"') { inString = !inString; continue; }\n\t\tif (!inString) {\n\t\t\tif (c === '{') depth++;\n\t\t\telse if (c === '}') { depth--; if (depth === 0) { endPos = i + 1; break; } }\n\t\t}\n\t}\n\tif (endPos <= startPos) return null;\n\ttry { return JSON.parse(text.substring(startPos, endPos)); } catch { return null; }\n}\n\n/**\n * Build one Component from an LLM component spec: resolve the template, bind the\n * source's queryId, and validate/repair the config against the real columns.\n * Returns null when it can't be built (unknown template, missing queryId, failed\n * validation). Shared by the early answer-streamer and the main loop so the two\n * never drift.\n */\nasync function buildScriptComponent(\n\tcomp: any,\n\tctx: ScriptCompCtx,\n): Promise<{ component: Component; spec?: ScriptComponentSpec } | null> {\n\tconst { queries, queryIds, columnsByIndex, availableComponents } = ctx;\n\n\tlet validationCols: Map<string, ColInfo> = new Map();\n\tlet validationRowCount = 0;\n\tlet specSourceRef = '';\n\n\t// Be permissive — the LLM may use componentId or componentName, id or name.\n\tconst wantedId = comp.componentId || '';\n\tconst wantedName = comp.componentName || '';\n\tconst template = availableComponents.find(c =>\n\t\tc.id === wantedId || c.name === wantedId || c.id === wantedName || c.name === wantedName,\n\t);\n\tif (!template) {\n\t\tlogger.warn(`[ScriptComponentGen] Component \"${wantedId || wantedName}\" not found — skipping`);\n\t\treturn null;\n\t}\n\n\t// Markdown blocks have no data source.\n\tif (template.type === 'MarkdownBlock') {\n\t\tconst p: any = comp.props || {};\n\t\t// DynamicMarkdownBlock reads props.content. The LLM often nests the text\n\t\t// under config.content (mirroring the chart shape), which makes the\n\t\t// component fall back to its demo DEFAULT_CONTENT (\"Overview / Revenue\n\t\t// grew 15% ...\"). Lift the real text up to props.content.\n\t\tconst content = p.content ?? p.config?.content ?? p.config?.markdown ?? p.config?.text ?? '';\n\t\treturn {\n\t\t\tcomponent: {\n\t\t\t\tid: `comp_${Math.random().toString(36).substring(2, 8)}`,\n\t\t\t\tname: template.name,\n\t\t\t\tdisplayName: template.displayName,\n\t\t\t\ttype: template.type,\n\t\t\t\tdescription: template.description,\n\t\t\t\tprops: { title: p.title, description: p.description, content },\n\t\t\t\tcategory: template.category,\n\t\t\t\tkeywords: template.keywords,\n\t\t\t},\n\t\t\t// Persist a content-bearing spec so the markdown survives deterministic\n\t\t\t// replay (it has no data source, so its text must be stored — otherwise\n\t\t\t// re-running a matched script rebuilds only the source-bound components\n\t\t\t// and silently drops the narrative).\n\t\t\tspec: {\n\t\t\t\tcomponentType: template.name,\n\t\t\t\tsourceRef: 'markdown',\n\t\t\t\ttitle: p.title,\n\t\t\t\tdescription: p.description,\n\t\t\t\tcontent,\n\t\t\t\tconfig: {},\n\t\t\t},\n\t\t};\n\t}\n\n\t// Every script component binds to one source's pre-executed rows via queryId.\n\t// (No federation: the script already did any cross-source work in JS and\n\t// returned a single combined dataset — the component just renders it.)\n\tconst sourceIdx = typeof comp.sourceIndex === 'number' ? comp.sourceIndex : 0;\n\tconst targetQuery = queries[sourceIdx] || queries[0];\n\tconst targetQueryId = queryIds[targetQuery.sourceId];\n\tif (!targetQueryId) {\n\t\tlogger.warn(`[ScriptComponentGen] No queryId for source \"${targetQuery.sourceName}\" — skipping`);\n\t\treturn null;\n\t}\n\tvalidationCols = columnsByIndex[sourceIdx] || inferColumns(targetQuery.data);\n\tvalidationRowCount = targetQuery.count ?? (targetQuery.data?.length || 0);\n\tspecSourceRef = targetQuery.sourceId;\n\t// Virtual sources route through the script_dataset sentinel toolId.\n\tconst renderedToolId = targetQuery.virtual ? 'script_dataset' : targetQuery.sourceId;\n\tconst externalToolProp = { toolId: renderedToolId, toolName: targetQuery.sourceName, parameters: { queryId: targetQueryId } };\n\n\tconst repaired = validateAndRepairComponent(comp, template.type, validationCols, validationRowCount);\n\tif (!repaired) {\n\t\tlogger.warn(`[ScriptComponentGen] Component \"${template.name}\" dropped — config keys could not be mapped to the data columns`);\n\t\treturn null;\n\t}\n\n\t// DataTable columns: DynamicDataTable expects [{key,label}] objects. The LLM\n\t// sometimes emits plain strings, which the table can't map and so renders its\n\t// demo defaults. Normalize string columns to {key,label}.\n\tif (template.type === 'DataTable' && Array.isArray(comp.props?.config?.columns)) {\n\t\tcomp.props.config.columns = comp.props.config.columns.map((c: any) =>\n\t\t\ttypeof c === 'string'\n\t\t\t\t? { key: c, label: c.replace(/[_-]+/g, ' ').replace(/\\b\\w/g, (m: string) => m.toUpperCase()) }\n\t\t\t\t: c,\n\t\t);\n\t}\n\n\tconst component: Component = {\n\t\tid: `comp_${Math.random().toString(36).substring(2, 8)}`,\n\t\tname: template.name,\n\t\tdisplayName: template.displayName,\n\t\ttype: template.type,\n\t\tdescription: template.description,\n\t\tprops: { ...comp.props, externalTool: externalToolProp },\n\t\tcategory: template.category,\n\t\tkeywords: template.keywords,\n\t};\n\n\t// Reusable, queryId-free spec for deterministic replay (skipped for federation).\n\tconst spec: ScriptComponentSpec | undefined = specSourceRef\n\t\t? {\n\t\t\tcomponentType: template.name,\n\t\t\tsourceRef: specSourceRef,\n\t\t\ttitle: comp.props?.title,\n\t\t\tdescription: comp.props?.description,\n\t\t\tconfig: { ...(comp.props?.config || {}) },\n\t\t}\n\t\t: undefined;\n\n\treturn { component, spec };\n}\n\nexport async function generateScriptComponents(params: {\n\tuserPrompt: string;\n\tscriptResult: ScriptResult;\n\tqueryIds: Record<string, string>;\n\tavailableComponents: Component[];\n\tapiKey?: string;\n\tmodel?: string;\n\tcomponentStreamCallback?: (component: Component) => void;\n\t/** Accepted for call-site compatibility; no longer used (federation removed). */\n\texternalTools?: ExternalTool[];\n\t/** Accepted for call-site compatibility; no longer used (federation removed). */\n\tcollections?: any;\n\t/** Cleaned analysis/narrative text — used to inject a fallback DynamicMarkdownBlock if the LLM omits one (markdown is required in every response). */\n\tanalysisText?: string;\n}): Promise<ScriptComponentResult> {\n\t// `externalTools`/`collections` remain on the params type for call-site compat\n\t// but are no longer used here (federation was removed from the script flow).\n\tconst { userPrompt, scriptResult, queryIds, availableComponents, apiKey, model, componentStreamCallback, analysisText } = params;\n\n\tconst queries = scriptResult.executedQueries;\n\tif (!queries || queries.length === 0) {\n\t\tlogger.warn(`[ScriptComponentGen] No query data available — returning empty`);\n\t\treturn { components: [], layoutTitle: '', layoutDescription: '', actions: [], componentSpecs: [] };\n\t}\n\n\tlogger.info(`[ScriptComponentGen] Starting | ${queries.length} queries | ${availableComponents.length} available components`);\n\n\t// Pre-infer columns+types per source for deterministic component validation.\n\t// Virtual (computed:_final / script_dataset) sources may arrive without inline\n\t// rows when the saved script's executedQueries were reconstructed — fall back to\n\t// the script's final dataset so chart configs are still validated (and dangling\n\t// keys like a non-existent seriesKey get stripped instead of breaking the chart).\n\tconst columnsByIndex = queries.map(q => {\n\t\tconst data = (q.data && q.data.length) ? q.data : (q.virtual ? scriptResult.data : q.data);\n\t\treturn inferColumns(data || []);\n\t});\n\n\t// Build available components description\n\tconst availableCompsText = availableComponents\n\t\t.map(c => `- ${c.name} (${c.type}): ${c.description || ''}`)\n\t\t.join('\\n');\n\n\t// Build data description for ALL sources\n\tconst sourceDescriptions = queries.map((q, idx) => {\n\t\tif (!q.data || q.data.length === 0) return null;\n\n\t\t// Use the SAME inferred column types validateAndRepairComponent uses, so the\n\t\t// LLM sees NUMBER for numeric columns even when DuckDB returns ints/counts as\n\t\t// strings (e.g. \"232\"). The naive first-row typeof mislabeled those as TEXT,\n\t\t// which pushed numeric columns onto the wrong axis. Fall back to bare keys if\n\t\t// inference is empty.\n\t\tconst cols = columnsByIndex[idx];\n\t\tconst columns = (cols && cols.size > 0)\n\t\t\t? Array.from(cols.values()).map(c => `${c.name} (${c.type})`).join(', ')\n\t\t\t: Object.keys(q.data[0]).join(', ');\n\n\t\tconst sampleData = JSON.stringify(q.data.slice(0, TOOL_TRACKING_SAMPLE_ROWS), null, 2);\n\n\t\treturn `### Source ${idx}: \"${q.sourceName}\" (sourceIndex: ${idx})\n- Rows: ${q.count}${q.totalCount && q.totalCount > q.count ? ` of ${q.totalCount} total` : ''}\n- Columns: ${columns}\n- Sample data:\n\\`\\`\\`json\n${sampleData}\n\\`\\`\\``;\n\t}).filter(Boolean).join('\\n\\n');\n\n\ttry {\n\t\tconst prompts = await promptLoader.loadPrompts('script-components', {\n\t\t\tUSER_PROMPT: userPrompt,\n\t\t\tAVAILABLE_COMPONENTS: availableCompsText,\n\t\t\tSOURCE_NAME: queries.map(q => q.sourceName).join(', '),\n\t\t\tROW_COUNT: queries.map(q => `${q.sourceName}: ${q.count}`).join(', '),\n\t\t\tCOLUMN_DESCRIPTIONS: sourceDescriptions,\n\t\t\tSAMPLE_DATA: '',\n\t\t\tANALYSIS_TEXT: (analysisText && analysisText.trim())\n\t\t\t\t? analysisText.trim()\n\t\t\t\t: 'No analysis text provided — infer intent from the user question and the returned data.',\n\t\t});\n\n\t\t// Shared build context for the early answer-streamer and the loop.\n\t\tconst ctx: ScriptCompCtx = {\n\t\t\tqueries, queryIds, columnsByIndex, availableComponents,\n\t\t};\n\n\t\t// Answer-component early streaming (parity with the agent flow): as the LLM\n\t\t// streams, detect `answerComponent` and emit it to the UI first, before the\n\t\t// rest of the dashboard. The main loop then skips re-streaming it.\n\t\tlet fullText = '';\n\t\tlet answerAttempted = false;\n\t\tlet streamedAnswerKey: string | null = null;\n\t\tconst partial = componentStreamCallback ? (chunk: string) => {\n\t\t\tfullText += chunk;\n\t\t\tif (answerAttempted) return;\n\t\t\tconst answerComp = extractAnswerComponentObject(fullText);\n\t\t\tif (!answerComp) return;\n\t\t\tanswerAttempted = true; // sync guard — attempt the early stream only once\n\t\t\tstreamedAnswerKey = scriptCompKey(answerComp);\n\t\t\tbuildScriptComponent(answerComp, ctx)\n\t\t\t\t.then(built => {\n\t\t\t\t\tif (built) {\n\t\t\t\t\t\tcomponentStreamCallback(built.component);\n\t\t\t\t\t\tlogger.info('[ScriptComponentGen] Streamed answer component early');\n\t\t\t\t\t} else {\n\t\t\t\t\t\tstreamedAnswerKey = null; // build failed — let the loop produce it\n\t\t\t\t\t}\n\t\t\t\t})\n\t\t\t\t.catch(() => { streamedAnswerKey = null; });\n\t\t} : undefined;\n\n\t\tconst response = await LLM.stream<any>(\n\t\t\t{ sys: prompts.system, user: prompts.user },\n\t\t\t{\n\t\t\t\tmodel: model || undefined,\n\t\t\t\tmaxTokens: 5000,\n\t\t\t\ttemperature: 0,\n\t\t\t\tapiKey,\n\t\t\t\tpartial,\n\t\t\t},\n\t\t\ttrue,\n\t\t);\n\n\t\tif (!response || !response.components || !Array.isArray(response.components)) {\n\t\t\tlogger.warn(`[ScriptComponentGen] LLM returned invalid response`);\n\t\t\treturn { components: [], layoutTitle: '', layoutDescription: '', actions: [], componentSpecs: [] };\n\t\t}\n\n\t\tconst layoutTitle = response.layoutTitle || 'Dashboard';\n\t\tconst layoutDescription = response.layoutDescription || '';\n\t\tconst matchedComponents: Component[] = [];\n\t\tconst componentSpecs: ScriptComponentSpec[] = [];\n\n\t\tfor (const comp of response.components) {\n\t\t\tconst built = await buildScriptComponent(comp, ctx);\n\t\t\tif (!built) continue;\n\t\t\tmatchedComponents.push(built.component);\n\t\t\tif (built.spec) componentSpecs.push(built.spec);\n\t\t\t// Skip re-streaming the answer component that was already emitted early.\n\t\t\tif (componentStreamCallback && scriptCompKey(comp) !== streamedAnswerKey) {\n\t\t\t\tcomponentStreamCallback(built.component);\n\t\t\t}\n\t\t}\n\n\t\t// Safety: the LLM declared an answer component but didn't list it in\n\t\t// `components` — build + prepend it so the primary answer is always present.\n\t\tif (response.hasAnswerComponent && response.answerComponent) {\n\t\t\tconst aKey = scriptCompKey(response.answerComponent);\n\t\t\tconst alreadyIncluded = response.components.some((c: any) => scriptCompKey(c) === aKey);\n\t\t\tif (!alreadyIncluded) {\n\t\t\t\tconst built = await buildScriptComponent(response.answerComponent, ctx);\n\t\t\t\tif (built) {\n\t\t\t\t\tmatchedComponents.unshift(built.component);\n\t\t\t\t\tif (built.spec) componentSpecs.unshift(built.spec);\n\t\t\t\t\tif (componentStreamCallback && aKey !== streamedAnswerKey) componentStreamCallback(built.component);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Guarantee a DynamicMarkdownBlock in EVERY response. The prompt asks for one,\n\t\t// but enforce it so the UI always carries the narrative summary even when the\n\t\t// LLM omits it. Uses the caller's cleaned analysis text as the content.\n\t\tconst hasMarkdown = matchedComponents.some(c => c.type === 'MarkdownBlock');\n\t\tif (!hasMarkdown && analysisText && analysisText.trim()) {\n\t\t\tconst mdTemplate = availableComponents.find(c => c.type === 'MarkdownBlock' || c.name === 'DynamicMarkdownBlock');\n\t\t\tif (mdTemplate) {\n\t\t\t\tconst mdComponent: Component = {\n\t\t\t\t\tid: `comp_${Math.random().toString(36).substring(2, 8)}`,\n\t\t\t\t\tname: mdTemplate.name,\n\t\t\t\t\tdisplayName: mdTemplate.displayName,\n\t\t\t\t\ttype: mdTemplate.type,\n\t\t\t\t\tdescription: mdTemplate.description,\n\t\t\t\t\tprops: { content: analysisText.trim() },\n\t\t\t\t\tcategory: mdTemplate.category,\n\t\t\t\t\tkeywords: mdTemplate.keywords,\n\t\t\t\t};\n\t\t\t\tmatchedComponents.push(mdComponent);\n\t\t\t\t// Persist it too, so deterministic replay keeps the narrative.\n\t\t\t\tcomponentSpecs.push({\n\t\t\t\t\tcomponentType: mdTemplate.name,\n\t\t\t\t\tsourceRef: 'markdown',\n\t\t\t\t\tcontent: analysisText.trim(),\n\t\t\t\t\tconfig: {},\n\t\t\t\t});\n\t\t\t\tif (componentStreamCallback) componentStreamCallback(mdComponent);\n\t\t\t\tlogger.info('[ScriptComponentGen] Injected fallback DynamicMarkdownBlock (LLM omitted one)');\n\t\t\t}\n\t\t}\n\n\t\tlogger.info(`[ScriptComponentGen] Generated ${matchedComponents.length} components`);\n\n\t\tconst actions: Action[] = (response.actions || []).map((a: any, idx: number) => ({\n\t\t\tid: `action_${idx}_${Date.now()}`,\n\t\t\tname: a.name || a.question || '',\n\t\t\ttype: 'next_question' as const,\n\t\t\tquestion: a.question || a.name || '',\n\t\t}));\n\n\t\treturn {\n\t\t\tcomponents: matchedComponents,\n\t\t\tlayoutTitle,\n\t\t\tlayoutDescription,\n\t\t\tactions,\n\t\t\tcomponentSpecs,\n\t\t};\n\n\t} catch (error) {\n\t\tconst msg = error instanceof Error ? error.message : String(error);\n\t\tlogger.error(`[ScriptComponentGen] Failed: ${msg}`);\n\t\treturn { components: [], layoutTitle: '', layoutDescription: '', actions: [], componentSpecs: [] };\n\t}\n}\n\n/**\n * Deterministically rebuild components from previously-validated specs (the\n * component recipe), binding each to a FRESH queryId from this run's data.\n * No LLM call. Returns null when the specs can't be cleanly replayed (missing\n * source, unknown component, federation spec, or column drift) — the caller\n * should then fall back to `generateScriptComponents`.\n *\n * See backend/docs/SCRIPT-COMPONENT-CONSISTENCY.md (Phase 3).\n */\nexport function assembleComponentsFromSpecs(params: {\n\tspecs: ScriptComponentSpec[];\n\tscriptResult: ScriptResult;\n\tqueryIds: Record<string, string>;\n\tavailableComponents: Component[];\n\tcomponentStreamCallback?: (component: Component) => void;\n\t/** Narrative text — used to rebuild a markdown block whose content wasn't persisted (older recipes), so EVERY replay still carries a summary. */\n\tanalysisText?: string;\n}): Component[] | null {\n\tconst { specs, scriptResult, queryIds, availableComponents, componentStreamCallback, analysisText } = params;\n\tif (!specs || specs.length === 0) return null;\n\n\tconst queries = scriptResult.executedQueries || [];\n\tconst byId = new Map(queries.map(q => [q.sourceId, q]));\n\tconst out: Component[] = [];\n\tlet answerComponentStreamed = false;\n\n\tconst mdTemplate = availableComponents.find(c => c.type === 'MarkdownBlock' || c.name === 'DynamicMarkdownBlock');\n\tconst buildMarkdown = (content: string, title?: string, description?: string): Component | null => {\n\t\tif (!mdTemplate || !content || !content.trim()) return null;\n\t\treturn {\n\t\t\tid: `comp_${Math.random().toString(36).substring(2, 8)}`,\n\t\t\tname: mdTemplate.name,\n\t\t\tdisplayName: mdTemplate.displayName,\n\t\t\ttype: mdTemplate.type,\n\t\t\tdescription: mdTemplate.description,\n\t\t\tprops: { title, description, content: content.trim() },\n\t\t\tcategory: mdTemplate.category,\n\t\t\tkeywords: mdTemplate.keywords,\n\t\t};\n\t};\n\n\tfor (const spec of specs) {\n\t\tif (spec.sourceRef === 'federation') {\n\t\t\tlogger.info('[ScriptComponentGen] Recipe has a federation spec — not replayable deterministically, falling back to LLM');\n\t\t\treturn null;\n\t\t}\n\n\t\t// Markdown is content-only (no data source) — rebuild from the persisted text.\n\t\tif (spec.sourceRef === 'markdown') {\n\t\t\tconst md = buildMarkdown(spec.content || analysisText || '', spec.title, spec.description);\n\t\t\tif (md) {\n\t\t\t\tout.push(md);\n\t\t\t\tif (componentStreamCallback) componentStreamCallback(md);\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst q = byId.get(spec.sourceRef) || queries.find(x => x.virtual);\n\t\tif (!q || !q.data || q.data.length === 0) {\n\t\t\tlogger.info(`[ScriptComponentGen] Recipe spec source \"${spec.sourceRef}\" missing/empty on replay — falling back to LLM`);\n\t\t\treturn null;\n\t\t}\n\n\t\tconst template = availableComponents.find(c => c.name === spec.componentType || c.id === spec.componentType);\n\t\tif (!template) {\n\t\t\tlogger.info(`[ScriptComponentGen] Recipe spec component \"${spec.componentType}\" not in library — falling back to LLM`);\n\t\t\treturn null;\n\t\t}\n\n\t\t// Cheap drift check — re-validate the stored config against this run's columns.\n\t\tconst cols = inferColumns(q.data);\n\t\tconst comp: any = { props: { title: spec.title, description: spec.description, config: { ...(spec.config || {}) } } };\n\t\tconst repaired = validateAndRepairComponent(comp, template.type, cols, q.count ?? q.data.length);\n\t\tif (!repaired) {\n\t\t\tlogger.info(`[ScriptComponentGen] Recipe spec \"${spec.componentType}\" no longer maps to columns (shape drift) — falling back to LLM`);\n\t\t\treturn null;\n\t\t}\n\n\t\tconst queryId = queryIds[q.sourceId] || queryCache.storeQuery(q.sql, { data: q.data, count: q.count });\n\t\tconst toolId = q.virtual ? 'script_dataset' : q.sourceId;\n\n\t\tconst component: Component = {\n\t\t\tid: `comp_${Math.random().toString(36).substring(2, 8)}`,\n\t\t\tname: template.name,\n\t\t\tdisplayName: template.displayName,\n\t\t\ttype: template.type,\n\t\t\tdescription: template.description,\n\t\t\tprops: {\n\t\t\t\t...comp.props,\n\t\t\t\texternalTool: { toolId, toolName: q.sourceName, parameters: { queryId } },\n\t\t\t},\n\t\t\tcategory: template.category,\n\t\t\tkeywords: template.keywords,\n\t\t};\n\t\tout.push(component);\n\t\tif (componentStreamCallback && !answerComponentStreamed) {\n\t\t\tcomponentStreamCallback(component);\n\t\t\tanswerComponentStreamed = true;\n\t\t}\n\t}\n\n\t// Guarantee a markdown narrative on EVERY replay — covers older recipes saved\n\t// before markdown was persisted as a spec. Uses this run's analysis text.\n\tif (out.length > 0 && !out.some(c => c.type === 'MarkdownBlock')) {\n\t\tconst md = buildMarkdown(analysisText || '');\n\t\tif (md) {\n\t\t\tout.push(md);\n\t\t\tif (componentStreamCallback) componentStreamCallback(md);\n\t\t\tlogger.info('[ScriptComponentGen] Injected markdown on replay (recipe had no markdown spec)');\n\t\t}\n\t}\n\n\tlogger.info(`[ScriptComponentGen] Deterministically assembled ${out.length} component(s) from recipe — no LLM call`);\n\treturn out.length > 0 ? out : null;\n}\n","import dotenv from 'dotenv';\nimport { BaseLLM, BaseLLMConfig } from './base-llm';\n\ndotenv.config({ quiet: true });\n\nexport interface AnthropicLLMConfig extends BaseLLMConfig {}\n\n/**\n * AnthropicLLM class for handling AI-powered component generation and matching using Anthropic Claude\n */\nexport class AnthropicLLM extends BaseLLM {\n\tconstructor(config?: AnthropicLLMConfig) {\n\t\tsuper(config);\n\t}\n\n\tprotected getDefaultModel(): string {\n\t\treturn 'anthropic/claude-sonnet-4-5-20250929';\n\t}\n\n\tprotected getDefaultFastModel(): string {\n\t\t// Haiku is 75% cheaper and faster - ideal for simple tasks\n\t\treturn 'anthropic/claude-haiku-4-5-20251001';\n\t}\n\n\tprotected getDefaultApiKey(): string | undefined {\n\t\treturn process.env.ANTHROPIC_API_KEY;\n\t}\n\n\tprotected getProviderName(): string {\n\t\treturn 'Anthropic';\n\t}\n}\n\n// Export a singleton instance\nexport const anthropicLLM = new AnthropicLLM();\n","import path from 'path';\nimport fs from 'fs';\nimport { logger } from '../utils/logger';\n\n/**\n * Schema class for managing database schema operations\n */\nexport class Schema {\n private schemaFilePath: string;\n private cachedSchema: any = null;\n\n constructor(schemaFilePath?: string) {\n this.schemaFilePath = schemaFilePath || path.join(process.cwd(), '../analysis/data/schema.json');\n }\n\n /**\n * Gets the database schema from the schema file\n * @returns Parsed schema object or null if error occurs\n */\n getDatabaseSchema(): any | null {\n\n try {\n // Create directory structure if it doesn't exist\n const dir = path.dirname(this.schemaFilePath);\n if (!fs.existsSync(dir)) {\n logger.info(`Creating directory structure: ${dir}`);\n fs.mkdirSync(dir, { recursive: true });\n }\n\n // Create file with empty schema if it doesn't exist\n if (!fs.existsSync(this.schemaFilePath)) {\n logger.info(`Schema file does not exist at ${this.schemaFilePath}, creating with empty schema`);\n const initialSchema = {\n database: '',\n schema: '',\n description: '',\n tables: [],\n relationships: []\n };\n fs.writeFileSync(this.schemaFilePath, JSON.stringify(initialSchema, null, 4));\n this.cachedSchema = initialSchema;\n return initialSchema;\n }\n\n const fileContent = fs.readFileSync(this.schemaFilePath, 'utf-8');\n const schema = JSON.parse(fileContent);\n this.cachedSchema = schema;\n return schema;\n } catch (error) {\n logger.error('Error parsing schema file:', error);\n return null;\n }\n }\n\n /**\n * Gets the cached schema or loads it if not cached\n * @returns Cached schema or freshly loaded schema\n */\n getSchema(): any | null {\n if (this.cachedSchema) {\n return this.cachedSchema;\n }\n return this.getDatabaseSchema();\n }\n\n /**\n * Generates database schema documentation for LLM from Snowflake JSON schema\n * @returns Formatted schema documentation string\n */\n generateSchemaDocumentation(): string {\n const schema = this.getSchema();\n\n if (!schema) {\n logger.warn('No database schema found.');\n return 'No database schema available.';\n }\n\n const tables: string[] = [];\n\n // Header information\n tables.push(`Database: ${schema.database}`);\n tables.push(`Schema: ${schema.schema}`);\n tables.push(`Description: ${schema.description}`);\n tables.push('');\n tables.push('='.repeat(80));\n tables.push('');\n\n // Process each table\n for (const table of schema.tables) {\n const tableInfo: string[] = [];\n\n tableInfo.push(`TABLE: ${table.fullName}`);\n tableInfo.push(`Description: ${table.description}`);\n tableInfo.push(`Row Count: ~${table.rowCount.toLocaleString()}`);\n tableInfo.push('');\n tableInfo.push('Columns:');\n\n // Process columns\n for (const column of table.columns) {\n let columnLine = ` - ${column.name}: ${column.type}`;\n\n if ((column as any).isPrimaryKey) {\n columnLine += ' (PRIMARY KEY)';\n }\n\n if ((column as any).isForeignKey && (column as any).references) {\n columnLine += ` (FK -> ${(column as any).references.table}.${(column as any).references.column})`;\n }\n\n if (!column.nullable) {\n columnLine += ' NOT NULL';\n }\n\n if (column.description) {\n columnLine += ` - ${column.description}`;\n }\n\n tableInfo.push(columnLine);\n\n // Add value examples for categorical columns\n if ((column as any).sampleValues && (column as any).sampleValues.length > 0) {\n tableInfo.push(` Sample values: [${(column as any).sampleValues.join(', ')}]`);\n }\n\n // Add statistics if available\n if ((column as any).statistics) {\n const stats = (column as any).statistics;\n if (stats.min !== undefined && stats.max !== undefined) {\n tableInfo.push(` Range: ${stats.min} to ${stats.max}`);\n }\n if (stats.distinct !== undefined) {\n tableInfo.push(` Distinct values: ${stats.distinct.toLocaleString()}`);\n }\n }\n }\n\n tableInfo.push('');\n tables.push(tableInfo.join('\\n'));\n }\n\n // Add relationships section\n tables.push('='.repeat(80));\n tables.push('');\n tables.push('TABLE RELATIONSHIPS:');\n tables.push('');\n\n for (const rel of schema.relationships) {\n tables.push(`${rel.from} -> ${rel.to} (${rel.type}): ${rel.keys.join(' = ')}`);\n }\n\n return tables.join('\\n');\n }\n\n /**\n * Clears the cached schema, forcing a reload on next access\n */\n clearCache(): void {\n this.cachedSchema = null;\n }\n\n /**\n * Sets a custom schema file path\n * @param filePath - Path to the schema file\n */\n setSchemaPath(filePath: string): void {\n this.schemaFilePath = filePath;\n this.clearCache();\n }\n}\n\n// Export a singleton instance for use across the application\nexport const schema = new Schema();\n","/**\n * QueryExecutionService - Handles all query execution, validation, and retry logic\n * Extracted from BaseLLM for better separation of concerns\n */\n\nimport { Component } from '../../types';\nimport { ensureQueryLimit, validateAndFixSqlQuery } from '../utils';\nimport { schema } from '../schema';\nimport { promptLoader } from '../prompt-loader';\nimport { LLM } from '../../llm';\nimport { logger } from '../../utils/logger';\nimport { queryCache } from '../../utils/query-cache';\nimport {\n\tMAX_QUERY_VALIDATION_RETRIES,\n\tMAX_TOKENS_QUERY_FIX,\n\tMAX_AGENT_QUERY_LIMIT\n} from '../constants';\n\n/**\n * Context for component when requesting query fix\n */\nexport interface ComponentContext {\n\tname: string;\n\ttype: string;\n\ttitle?: string;\n}\n\n/**\n * Result of query validation\n */\nexport interface QueryValidationResult {\n\tcomponent: Component | null;\n\tqueryKey: string;\n\tresult: any;\n\tvalidated: boolean;\n}\n\n/**\n * Result of batch query validation\n */\nexport interface BatchValidationResult {\n\tcomponents: Component[];\n\tqueryResults: Map<string, any>;\n}\n\n/**\n * Configuration for QueryExecutionService\n */\nexport interface QueryExecutionServiceConfig {\n\tdefaultLimit: number;\n\tgetModelForTask: (taskType: 'simple' | 'complex') => string;\n\tgetApiKey: (apiKey?: string) => string | undefined;\n\tproviderName: string;\n}\n\n/**\n * QueryExecutionService handles all query-related operations\n */\nexport class QueryExecutionService {\n\tprivate config: QueryExecutionServiceConfig;\n\n\tconstructor(config: QueryExecutionServiceConfig) {\n\t\tthis.config = config;\n\t}\n\n\t/**\n\t * Get the cache key for a query\n\t * This ensures the cache key matches what the frontend will send\n\t */\n\tgetQueryCacheKey(query: any): string {\n\t\tif (typeof query === 'string') {\n\t\t\treturn query;\n\t\t} else if (query?.sql) {\n\t\t\tconst values = query.values || query.params;\n\t\t\tif (values && Object.keys(values).length > 0) {\n\t\t\t\treturn JSON.stringify({ sql: query.sql, values });\n\t\t\t} else {\n\t\t\t\treturn query.sql;\n\t\t\t}\n\t\t}\n\t\treturn '';\n\t}\n\n\t/**\n\t * Execute a query against the database\n\t * @param query - The SQL query to execute (string or object with sql/values)\n\t * @param collections - Collections object containing database execute function\n\t * @returns Object with result data and cache key\n\t */\n\tasync executeQuery(\n\t\tquery: any,\n\t\tcollections: any\n\t): Promise<{ result: any; cacheKey: string }> {\n\t\tconst cacheKey = this.getQueryCacheKey(query);\n\n\t\tif (!cacheKey) {\n\t\t\tthrow new Error('Invalid query format: expected string or object with sql property');\n\t\t}\n\n\t\tif (!collections?.['database']?.['execute']) {\n\t\t\tthrow new Error('Database collection not registered. Please register database.execute collection to execute queries.');\n\t\t}\n\n\t\tconst result = await collections['database']['execute']({ sql: cacheKey });\n\t\treturn { result, cacheKey };\n\t}\n\n\t/**\n\t * Request the LLM to fix a failed SQL query\n\t * @param failedQuery - The query that failed execution\n\t * @param errorMessage - The error message from the failed execution\n\t * @param componentContext - Context about the component\n\t * @param apiKey - Optional API key\n\t * @returns Fixed query string\n\t */\n\tasync requestQueryFix(\n\t\tfailedQuery: string,\n\t\terrorMessage: string,\n\t\tcomponentContext: ComponentContext,\n\t\tapiKey?: string\n\t): Promise<string> {\n\t\tconst schemaDoc = schema.generateSchemaDocumentation();\n\t\tconst databaseRules = await promptLoader.loadDatabaseRules();\n\n\t\tconst prompt = `You are a SQL expert. Fix the following SQL query that failed execution.\n\n## Database Schema\n${schemaDoc}\n\n## Database-Specific SQL Rules\n${databaseRules}\n\n## Component Context\n- Component Name: ${componentContext.name}\n- Component Type: ${componentContext.type}\n- Title: ${componentContext.title || 'N/A'}\n\n## Failed Query\n\\`\\`\\`sql\n${failedQuery}\n\\`\\`\\`\n\n## Error Message\n${errorMessage}\n\n## Instructions\n1. Analyze the error message and identify what caused the query to fail\n2. Fix the query to resolve the error while preserving the original intent\n3. Ensure the fixed query follows the database-specific SQL rules above\n4. Return ONLY the fixed SQL query, no explanations or markdown\n\nFixed SQL query:`;\n\n\t\tconst response = await LLM.text(\n\t\t\t{\n\t\t\t\tsys: 'You are a SQL expert. Return only the fixed SQL query with no additional text, explanations, or markdown formatting.',\n\t\t\t\tuser: prompt\n\t\t\t},\n\t\t\t{\n\t\t\t\tmodel: this.config.getModelForTask('simple'),\n\t\t\t\tmaxTokens: MAX_TOKENS_QUERY_FIX,\n\t\t\t\ttemperature: 0,\n\t\t\t\tapiKey: this.config.getApiKey(apiKey)\n\t\t\t}\n\t\t);\n\n\t\t// Clean up the response\n\t\tlet fixedQuery = response.trim();\n\t\tfixedQuery = fixedQuery.replace(/^```sql\\s*/i, '').replace(/\\s*```$/i, '');\n\t\tfixedQuery = fixedQuery.replace(/^```\\s*/i, '').replace(/\\s*```$/i, '');\n\n\t\t// Apply static SQL fixes\n\t\tconst { query: validatedQuery } = validateAndFixSqlQuery(fixedQuery);\n\n\t\treturn validatedQuery;\n\t}\n\n\t/**\n\t * Validate a single component's query with retry logic\n\t * @param component - The component to validate\n\t * @param collections - Collections object containing database execute function\n\t * @param apiKey - Optional API key for LLM calls\n\t * @returns Validation result with component, query key, and result\n\t */\n\tasync validateSingleQuery(\n\t\tcomponent: Component,\n\t\tcollections: any,\n\t\tapiKey?: string\n\t): Promise<QueryValidationResult> {\n\t\tconst query = component.props?.query;\n\t\tconst originalQueryKey = this.getQueryCacheKey(query);\n\t\tconst queryStr = typeof query === 'string' ? query : (query as any)?.sql || '';\n\t\tlet finalQueryKey = originalQueryKey;\n\n\t\tlet currentQuery: string | { sql: string; values?: any; params?: any } =\n\t\t\ttypeof query === 'string' ? query : { sql: (query as any)?.sql || '', values: (query as any)?.values, params: (query as any)?.params };\n\t\tlet currentQueryStr = queryStr;\n\t\tlet validated = false;\n\t\tlet lastError = '';\n\t\tlet result: any = null;\n\t\tlet attempts = 0;\n\n\t\tlogger.info(`[${this.config.providerName}] Validating query for component: ${component.name} (${component.type})`);\n\n\t\twhile (attempts < MAX_QUERY_VALIDATION_RETRIES && !validated) {\n\t\t\tattempts++;\n\n\t\t\ttry {\n\t\t\t\tlogger.debug(`[${this.config.providerName}] Query validation attempt ${attempts}/${MAX_QUERY_VALIDATION_RETRIES} for ${component.name}`);\n\t\t\t\tconst validationResult = await this.executeQuery(currentQuery, collections);\n\t\t\t\tresult = validationResult.result;\n\n\t\t\t\tvalidated = true;\n\t\t\t\tqueryCache.set(validationResult.cacheKey, result);\n\n\t\t\t\tlogger.info(`[${this.config.providerName}] ✓ Query validated for ${component.name} (attempt ${attempts}) - cached for frontend`);\n\n\t\t\t\t// Update component with the fixed query if it was modified\n\t\t\t\tif (currentQueryStr !== queryStr) {\n\t\t\t\t\tconst fixedQuery = typeof query === 'string' ? currentQueryStr : { ...(query as any), sql: currentQueryStr };\n\t\t\t\t\tcomponent.props = {\n\t\t\t\t\t\t...component.props,\n\t\t\t\t\t\tquery: fixedQuery\n\t\t\t\t\t};\n\t\t\t\t\tfinalQueryKey = this.getQueryCacheKey(fixedQuery);\n\t\t\t\t\tlogger.info(`[${this.config.providerName}] Updated ${component.name} with fixed query`);\n\t\t\t\t}\n\n\t\t\t} catch (error) {\n\t\t\t\tlastError = error instanceof Error ? error.message : String(error);\n\t\t\t\tlogger.warn(`[${this.config.providerName}] Query validation failed for ${component.name} (attempt ${attempts}/${MAX_QUERY_VALIDATION_RETRIES}): ${lastError}`);\n\n\t\t\t\tif (attempts >= MAX_QUERY_VALIDATION_RETRIES) {\n\t\t\t\t\tlogger.error(`[${this.config.providerName}] ✗ Max retries reached for ${component.name}, excluding from response`);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\t// Request LLM to fix the query\n\t\t\t\tlogger.info(`[${this.config.providerName}] Requesting query fix from LLM for ${component.name}...`);\n\n\t\t\t\ttry {\n\t\t\t\t\tconst fixedQueryStr = await this.requestQueryFix(\n\t\t\t\t\t\tcurrentQueryStr,\n\t\t\t\t\t\tlastError,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: component.name,\n\t\t\t\t\t\t\ttype: component.type,\n\t\t\t\t\t\t\ttitle: component.props?.title\n\t\t\t\t\t\t},\n\t\t\t\t\t\tapiKey\n\t\t\t\t\t);\n\n\t\t\t\t\tif (fixedQueryStr && fixedQueryStr !== currentQueryStr) {\n\t\t\t\t\t\tlogger.info(`[${this.config.providerName}] Received fixed query for ${component.name}, retrying...`);\n\t\t\t\t\t\tconst limitedFixedQuery = ensureQueryLimit(fixedQueryStr, this.config.defaultLimit, MAX_AGENT_QUERY_LIMIT);\n\t\t\t\t\t\tcurrentQueryStr = limitedFixedQuery;\n\t\t\t\t\t\tif (typeof currentQuery === 'string') {\n\t\t\t\t\t\t\tcurrentQuery = limitedFixedQuery;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tcurrentQuery = { ...currentQuery, sql: limitedFixedQuery };\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tlogger.warn(`[${this.config.providerName}] LLM returned same or empty query, stopping retries`);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t} catch (fixError) {\n\t\t\t\t\tconst fixErrorMsg = fixError instanceof Error ? fixError.message : String(fixError);\n\t\t\t\t\tlogger.error(`[${this.config.providerName}] Failed to get query fix from LLM: ${fixErrorMsg}`);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (!validated) {\n\t\t\tlogger.warn(`[${this.config.providerName}] Component ${component.name} excluded from response due to failed query validation`);\n\t\t}\n\n\t\treturn {\n\t\t\tcomponent: validated ? component : null,\n\t\t\tqueryKey: finalQueryKey,\n\t\t\tresult,\n\t\t\tvalidated\n\t\t};\n\t}\n\n\t/**\n\t * Validate multiple component queries in parallel\n\t * @param components - Array of components with potential queries\n\t * @param collections - Collections object containing database execute function\n\t * @param apiKey - Optional API key for LLM calls\n\t * @returns Object with validated components and query results map\n\t */\n\tasync validateComponentQueries(\n\t\tcomponents: Component[],\n\t\tcollections: any,\n\t\tapiKey?: string\n\t): Promise<BatchValidationResult> {\n\t\tconst queryResults = new Map<string, any>();\n\t\tconst validatedComponents: Component[] = [];\n\n\t\t// Separate components with and without queries\n\t\tconst componentsWithoutQuery: Component[] = [];\n\t\tconst componentsWithQuery: Component[] = [];\n\n\t\tfor (const component of components) {\n\t\t\tif (!component.props?.query) {\n\t\t\t\tcomponentsWithoutQuery.push(component);\n\t\t\t} else {\n\t\t\t\tcomponentsWithQuery.push(component);\n\t\t\t}\n\t\t}\n\n\t\t// Components without queries are automatically valid\n\t\tvalidatedComponents.push(...componentsWithoutQuery);\n\n\t\tif (componentsWithQuery.length === 0) {\n\t\t\treturn { components: validatedComponents, queryResults };\n\t\t}\n\n\t\tlogger.info(`[${this.config.providerName}] Validating ${componentsWithQuery.length} component queries in parallel...`);\n\n\t\t// Execute all validations in parallel\n\t\tconst validationPromises = componentsWithQuery.map(component =>\n\t\t\tthis.validateSingleQuery(component, collections, apiKey)\n\t\t);\n\n\t\tconst results = await Promise.allSettled(validationPromises);\n\n\t\t// Process results\n\t\tfor (let i = 0; i < results.length; i++) {\n\t\t\tconst result = results[i];\n\t\t\tconst component = componentsWithQuery[i];\n\n\t\t\tif (result.status === 'fulfilled') {\n\t\t\t\tconst { component: validatedComponent, queryKey, result: queryResult, validated } = result.value;\n\n\t\t\t\tif (validated && validatedComponent) {\n\t\t\t\t\tvalidatedComponents.push(validatedComponent);\n\t\t\t\t\tif (queryResult) {\n\t\t\t\t\t\tqueryResults.set(queryKey, queryResult);\n\t\t\t\t\t\tqueryResults.set(`${component.id}:${queryKey}`, queryResult);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tlogger.error(`[${this.config.providerName}] Unexpected error validating ${component.name}: ${result.reason}`);\n\t\t\t}\n\t\t}\n\n\t\tlogger.info(`[${this.config.providerName}] Parallel validation complete: ${validatedComponents.length}/${components.length} components validated`);\n\n\t\treturn {\n\t\t\tcomponents: validatedComponents,\n\t\t\tqueryResults\n\t\t};\n\t}\n}\n","/**\n * ToolExecutorService - Handles execution of SQL queries and external tools\n * Extracted from BaseLLM.generateTextResponse for better separation of concerns\n */\n\nimport { ensureQueryLimit } from '../utils';\nimport { logger } from '../../utils/logger';\nimport { userPromptErrorLogger } from '../../utils/user-prompt-error-logger';\nimport { StreamBuffer, streamDelay, withProgressHeartbeat } from '../stream-buffer';\nimport {\n\tformatQueryResultForLLM,\n\tformatToolResultForLLM,\n\tformatResultAsString\n} from '../llm-result-truncator';\nimport {\n\tMAX_QUERY_ATTEMPTS,\n\tMAX_TOOL_ATTEMPTS,\n\tMAX_AGENT_QUERY_LIMIT,\n\tDEFAULT_MAX_ROWS_FOR_LLM,\n\tDEFAULT_MAX_CHARS_PER_FIELD,\n\tSTREAM_PREVIEW_MAX_ROWS,\n\tSTREAM_PREVIEW_MAX_CHARS,\n\tTOOL_TRACKING_MAX_ROWS,\n\tTOOL_TRACKING_MAX_CHARS,\n\tTOOL_TRACKING_SAMPLE_ROWS\n} from '../constants';\n\n/**\n * External tool definition\n */\nexport interface ExternalTool {\n\tid: string;\n\tname: string;\n\tdescription?: string;\n\t/** Tool type: \"source\" = routed through SourceAgent, \"direct\" = called directly by MainAgent */\n\ttoolType?: 'source' | 'direct';\n\t/** Full untruncated schema for source agent (all columns visible) */\n\tfullSchema?: string;\n\t/** Schema size tier: small (≤50 tables), medium (51-200), large (201-500), very_large (500+) */\n\tschemaTier?: string;\n\t/** Schema search function for very_large tier — keyword search over entities */\n\tschemaSearchFn?: (keywords: string[]) => string;\n\tfn: (input: any) => Promise<any>;\n\tlimit?: number;\n\toutputSchema?: any;\n\texecutionType?: 'immediate' | 'deferred';\n\tuserProvidedData?: any;\n\tparams?: Record<string, any>;\n}\n\n/**\n * Executed tool tracking info\n */\nexport interface ExecutedToolInfo {\n\tid: string;\n\tname: string;\n\tparams: any;\n\tresult: {\n\t\t_totalRecords: number;\n\t\t_recordsShown: number;\n\t\t_metadata?: any;\n\t\t_sampleData: any[];\n\t\t/** Bounded summary over the FULL fetched result (complete structure). */\n\t\t_summary?: any;\n\t\t/** Up to MAIN_AGENT_COMPLETE_ROWS rows — the complete result when small. */\n\t\t_mainAgentRows?: any[];\n\t};\n\toutputSchema?: any;\n\tsourceSchema?: string;\n\tsourceType?: string;\n}\n\n/**\n * Configuration for ToolExecutorService\n */\nexport interface ToolExecutorServiceConfig {\n\tproviderName: string;\n\tcollections: any;\n\tstreamBuffer: StreamBuffer;\n}\n\n/**\n * Result of tool handler execution\n */\nexport interface ToolHandlerResult {\n\thandler: (toolName: string, toolInput: any) => Promise<string>;\n\tgetExecutedTools: () => ExecutedToolInfo[];\n\tisMaxAttemptsReached: () => boolean;\n}\n\n/**\n * ToolExecutorService handles SQL query and external tool execution\n */\nexport class ToolExecutorService {\n\tprivate config: ToolExecutorServiceConfig;\n\tprivate queryAttempts = new Map<string, number>();\n\tprivate toolAttempts = new Map<string, number>();\n\tprivate executedToolsList: ExecutedToolInfo[] = [];\n\tprivate maxAttemptsReached = false;\n\n\tconstructor(config: ToolExecutorServiceConfig) {\n\t\tthis.config = config;\n\t}\n\n\t/**\n\t * Reset state for a new execution\n\t */\n\treset(): void {\n\t\tthis.queryAttempts.clear();\n\t\tthis.toolAttempts.clear();\n\t\tthis.executedToolsList = [];\n\t\tthis.maxAttemptsReached = false;\n\t}\n\n\t/**\n\t * Get list of successfully executed tools\n\t */\n\tgetExecutedTools(): ExecutedToolInfo[] {\n\t\treturn this.executedToolsList;\n\t}\n\n\t/**\n\t * Check if max attempts were reached\n\t */\n\tisMaxAttemptsReached(): boolean {\n\t\treturn this.maxAttemptsReached;\n\t}\n\n\t/**\n\t * Create a tool handler function for LLM.streamWithTools\n\t * @param externalTools - List of available external tools\n\t * @returns Tool handler function\n\t */\n\tcreateToolHandler(externalTools?: ExternalTool[]): (toolName: string, toolInput: any) => Promise<string> {\n\t\treturn async (toolName: string, toolInput: any): Promise<string> => {\n\t\t\tif (toolName === 'execute_query') {\n\t\t\t\treturn this.executeQuery(toolInput);\n\t\t\t} else {\n\t\t\t\treturn this.executeExternalTool(toolName, toolInput, externalTools);\n\t\t\t}\n\t\t};\n\t}\n\n\t/**\n\t * Execute a SQL query with retry tracking and streaming feedback\n\t */\n\tprivate async executeQuery(toolInput: any): Promise<string> {\n\t\tlet sql = toolInput.sql;\n\t\tconst params = toolInput.params || {};\n\t\tconst reasoning = toolInput.reasoning;\n\t\tconst { streamBuffer, collections, providerName } = this.config;\n\n\t\t// Ensure query has proper LIMIT clause\n\t\tsql = ensureQueryLimit(sql, MAX_AGENT_QUERY_LIMIT, MAX_AGENT_QUERY_LIMIT);\n\n\t\t// Track attempts for this query pattern (normalized)\n\t\tconst queryKey = sql.toLowerCase().replace(/\\s+/g, ' ').trim();\n\t\tconst attempts = (this.queryAttempts.get(queryKey) || 0) + 1;\n\t\tthis.queryAttempts.set(queryKey, attempts);\n\n\t\tif (Object.keys(params).length > 0) {\n\t\t\tlogger.info(`[${providerName}] Query params: ${JSON.stringify(params)}`);\n\t\t}\n\n\t\t// Check if max attempts reached\n\t\tif (attempts > MAX_QUERY_ATTEMPTS) {\n\t\t\tconst errorMsg = `Maximum query attempts (${MAX_QUERY_ATTEMPTS}) reached. Unable to generate a valid query for your question.`;\n\t\t\tlogger.error(`[${providerName}] ${errorMsg}`);\n\n\t\t\tthis.maxAttemptsReached = true;\n\n\t\t\tif (streamBuffer.hasCallback()) {\n\t\t\t\tstreamBuffer.write(`\\n\\n❌ ${errorMsg}\\n\\nPlease try rephrasing your question or simplifying your request.\\n\\n`);\n\t\t\t}\n\n\t\t\tthrow new Error(errorMsg);\n\t\t}\n\n\t\ttry {\n\t\t\t// Flush any buffered LLM text before showing tool execution messages\n\t\t\tstreamBuffer.flush();\n\n\t\t\t// Stream query generation message to frontend\n\t\t\tif (streamBuffer.hasCallback()) {\n\t\t\t\tconst paramsDisplay = Object.keys(params).length > 0 ? `\\n**Parameters:** ${JSON.stringify(params)}` : '';\n\t\t\t\tif (attempts === 1) {\n\t\t\t\t\tstreamBuffer.write(`\\n\\n🔍 **Analyzing your question...**\\n\\n`);\n\t\t\t\t\tawait streamDelay();\n\t\t\t\t\tif (reasoning) {\n\t\t\t\t\t\tstreamBuffer.write(`💭 ${reasoning}\\n\\n`);\n\t\t\t\t\t\tawait streamDelay();\n\t\t\t\t\t}\n\t\t\t\t\tstreamBuffer.write(`📝 **Generated SQL Query:**\\n\\`\\`\\`sql\\n${sql}\\n\\`\\`\\`${paramsDisplay}\\n\\n`);\n\t\t\t\t\tawait streamDelay();\n\t\t\t\t} else {\n\t\t\t\t\tstreamBuffer.write(`\\n\\n🔄 **Retrying with corrected query (attempt ${attempts}/${MAX_QUERY_ATTEMPTS})...**\\n\\n`);\n\t\t\t\t\tawait streamDelay();\n\t\t\t\t\tif (reasoning) {\n\t\t\t\t\t\tstreamBuffer.write(`💭 ${reasoning}\\n\\n`);\n\t\t\t\t\t\tawait streamDelay();\n\t\t\t\t\t}\n\t\t\t\t\tstreamBuffer.write(`📝 **Corrected SQL Query:**\\n\\`\\`\\`sql\\n${sql}\\n\\`\\`\\`${paramsDisplay}\\n\\n`);\n\t\t\t\t\tawait streamDelay();\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Validate collections\n\t\t\tif (!collections?.['database']?.['execute']) {\n\t\t\t\tthrow new Error('Database collection not registered. Please register database.execute collection to execute queries.');\n\t\t\t}\n\n\t\t\t// Build the query payload\n\t\t\tconst queryPayload = Object.keys(params).length > 0\n\t\t\t\t? { sql: JSON.stringify({ sql, values: params }) }\n\t\t\t\t: { sql };\n\n\t\t\t// Execute query with progress heartbeat\n\t\t\tconst result = await withProgressHeartbeat(\n\t\t\t\t() => collections['database']['execute'](queryPayload),\n\t\t\t\t'Executing database query',\n\t\t\t\tstreamBuffer\n\t\t\t);\n\n\t\t\t// Extract data from result\n\t\t\tconst data = (result as any)?.data || result;\n\t\t\tconst rowCount = (result as any)?.count ?? (Array.isArray(data) ? data.length : 'N/A');\n\n\n\t\t\t// Stream success message and data preview\n\t\t\tif (streamBuffer.hasCallback()) {\n\t\t\t\tstreamBuffer.write(`\\n✅ **Query executed successfully!**\\n\\n`);\n\t\t\t\tawait streamDelay();\n\n\t\t\t\tif (Array.isArray(data) && data.length > 0) {\n\t\t\t\t\tconst firstRow = data[0];\n\t\t\t\t\tconst columns = Object.keys(firstRow);\n\n\t\t\t\t\tif (data.length === 1 && columns.length === 1) {\n\t\t\t\t\t\tconst value = firstRow[columns[0]];\n\t\t\t\t\t\tstreamBuffer.write(`**Result:** ${value}\\n\\n`);\n\t\t\t\t\t\tawait streamDelay();\n\t\t\t\t\t} else if (data.length > 0) {\n\t\t\t\t\t\tstreamBuffer.write(`**Retrieved ${rowCount} rows**\\n\\n`);\n\t\t\t\t\t\tawait streamDelay();\n\n\t\t\t\t\t\tconst streamPreview = formatQueryResultForLLM(data, {\n\t\t\t\t\t\t\tmaxRows: STREAM_PREVIEW_MAX_ROWS,\n\t\t\t\t\t\t\tmaxCharsPerField: STREAM_PREVIEW_MAX_CHARS\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tstreamBuffer.write(`<DataTable>${JSON.stringify(streamPreview.data)}</DataTable>\\n\\n`);\n\t\t\t\t\t\tif (streamPreview.truncationNote) {\n\t\t\t\t\t\t\tstreamBuffer.write(`*${streamPreview.truncationNote}*\\n\\n`);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tawait streamDelay();\n\t\t\t\t\t}\n\t\t\t\t} else if (Array.isArray(data) && data.length === 0) {\n\t\t\t\t\tstreamBuffer.write(`**No rows returned.**\\n\\n`);\n\t\t\t\t\tawait streamDelay();\n\t\t\t\t}\n\n\t\t\t\tstreamBuffer.write(`📊 **Analyzing results...**\\n\\n`);\n\t\t\t}\n\n\t\t\t// Format and truncate result for LLM consumption\n\t\t\tconst formattedResult = formatQueryResultForLLM(data, {\n\t\t\t\tmaxRows: DEFAULT_MAX_ROWS_FOR_LLM,\n\t\t\t\tmaxCharsPerField: DEFAULT_MAX_CHARS_PER_FIELD\n\t\t\t});\n\n\t\t\tif (formattedResult.truncationNote) {\n\t\t\t\tlogger.info(`[${providerName}] Truncation: ${formattedResult.truncationNote}`);\n\t\t\t}\n\n\t\t\treturn formatResultAsString(formattedResult);\n\t\t} catch (error) {\n\t\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\t\t\tlogger.error(`[${providerName}] Query execution failed (attempt ${attempts}/${MAX_QUERY_ATTEMPTS}): ${errorMsg}`);\n\n\t\t\tuserPromptErrorLogger.logSqlError(sql, error instanceof Error ? error : new Error(errorMsg), Object.keys(params).length > 0 ? Object.values(params) : undefined);\n\n\t\t\tif (streamBuffer.hasCallback()) {\n\t\t\t\tstreamBuffer.write(`❌ **Query execution failed:**\\n\\`\\`\\`\\n${errorMsg}\\n\\`\\`\\`\\n\\n`);\n\n\t\t\t\tif (attempts < MAX_QUERY_ATTEMPTS) {\n\t\t\t\t\tstreamBuffer.write(`🔧 **Generating corrected query...**\\n\\n`);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tthrow new Error(`Query execution failed: ${errorMsg}`);\n\t\t}\n\t}\n\n\t/**\n\t * Execute an external tool with retry tracking and streaming feedback\n\t */\n\tprivate async executeExternalTool(\n\t\ttoolName: string,\n\t\ttoolInput: any,\n\t\texternalTools?: ExternalTool[]\n\t): Promise<string> {\n\t\tconst { streamBuffer, providerName } = this.config;\n\n\t\tconst externalTool = externalTools?.find(t => t.id === toolName);\n\t\tif (!externalTool) {\n\t\t\tthrow new Error(`Unknown tool: ${toolName}`);\n\t\t}\n\n\t\t// Track attempts for this tool\n\t\tconst attempts = (this.toolAttempts.get(toolName) || 0) + 1;\n\t\tthis.toolAttempts.set(toolName, attempts);\n\n\n\t\t// Check if max attempts reached\n\t\tif (attempts > MAX_TOOL_ATTEMPTS) {\n\t\t\tconst errorMsg = `Maximum attempts (${MAX_TOOL_ATTEMPTS}) reached for tool: ${externalTool.name}`;\n\t\t\tlogger.error(`[${providerName}] ${errorMsg}`);\n\n\t\t\tif (streamBuffer.hasCallback()) {\n\t\t\t\tstreamBuffer.write(`\\n\\n❌ ${errorMsg}\\n\\nPlease try rephrasing your request or contact support.\\n\\n`);\n\t\t\t}\n\n\t\t\tthrow new Error(errorMsg);\n\t\t}\n\n\t\ttry {\n\t\t\tstreamBuffer.flush();\n\n\t\t\tif (streamBuffer.hasCallback()) {\n\t\t\t\tif (attempts === 1) {\n\t\t\t\t\tstreamBuffer.write(`\\n\\n🔗 **Executing ${externalTool.name}...**\\n\\n`);\n\t\t\t\t} else {\n\t\t\t\t\tstreamBuffer.write(`\\n\\n🔄 **Retrying ${externalTool.name} (attempt ${attempts}/${MAX_TOOL_ATTEMPTS})...**\\n\\n`);\n\t\t\t\t}\n\t\t\t\tawait streamDelay();\n\t\t\t}\n\n\t\t\t// Cap the limit parameter for LLM-initiated tool calls\n\t\t\t// This ensures external tools called by LLM don't return more than MAX_AGENT_QUERY_LIMIT rows\n\t\t\t// UI direct calls bypass this and can use higher limits\n\t\t\tconst cappedToolInput = { ...toolInput };\n\t\t\tif (cappedToolInput.limit !== undefined && cappedToolInput.limit > MAX_AGENT_QUERY_LIMIT) {\n\t\t\t\tlogger.info(`[${providerName}] Capping external tool limit from ${cappedToolInput.limit} to ${MAX_AGENT_QUERY_LIMIT}`);\n\t\t\t\tcappedToolInput.limit = MAX_AGENT_QUERY_LIMIT;\n\t\t\t} else if (cappedToolInput.limit === undefined) {\n\t\t\t\t// If no limit specified by LLM, set the max limit to ensure bounded results\n\t\t\t\tcappedToolInput.limit = MAX_AGENT_QUERY_LIMIT;\n\t\t\t}\n\n\t\t\t// Execute the external tool function with progress heartbeat\n\t\t\tconst result = await withProgressHeartbeat(\n\t\t\t\t() => externalTool.fn(cappedToolInput),\n\t\t\t\t`Running ${externalTool.name}`,\n\t\t\t\tstreamBuffer\n\t\t\t);\n\n\n\t\t\t// Track successfully executed tool (for passing to component matching)\n\t\t\tif (!this.executedToolsList.find(t => t.id === externalTool.id)) {\n\t\t\t\tconst formattedForTracking = formatToolResultForLLM(result, {\n\t\t\t\t\ttoolName: externalTool.name,\n\t\t\t\t\ttoolLimit: externalTool.limit,\n\t\t\t\t\tmaxRows: TOOL_TRACKING_MAX_ROWS,\n\t\t\t\t\tmaxCharsPerField: TOOL_TRACKING_MAX_CHARS\n\t\t\t\t});\n\n\t\t\t\tthis.executedToolsList.push({\n\t\t\t\t\tid: externalTool.id,\n\t\t\t\t\tname: externalTool.name,\n\t\t\t\t\tparams: cappedToolInput,\n\t\t\t\t\tresult: {\n\t\t\t\t\t\t_totalRecords: formattedForTracking.summary.totalRecords,\n\t\t\t\t\t\t_recordsShown: formattedForTracking.summary.recordsShown,\n\t\t\t\t\t\t_metadata: formattedForTracking.metadata,\n\t\t\t\t\t\t_sampleData: formattedForTracking.data.slice(0, TOOL_TRACKING_SAMPLE_ROWS)\n\t\t\t\t\t},\n\t\t\t\t\toutputSchema: externalTool.outputSchema\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tif (streamBuffer.hasCallback()) {\n\t\t\t\tstreamBuffer.write(`✅ **${externalTool.name} completed successfully**\\n\\n`);\n\t\t\t\tawait streamDelay();\n\t\t\t}\n\n\t\t\t// Format and truncate result for LLM consumption\n\t\t\tconst formattedToolResult = formatToolResultForLLM(result, {\n\t\t\t\ttoolName: externalTool.name,\n\t\t\t\ttoolLimit: externalTool.limit,\n\t\t\t\tmaxRows: DEFAULT_MAX_ROWS_FOR_LLM,\n\t\t\t\tmaxCharsPerField: DEFAULT_MAX_CHARS_PER_FIELD\n\t\t\t});\n\n\t\t\tif (formattedToolResult.truncationNote) {\n\t\t\t\tlogger.info(`[${providerName}] Truncation: ${formattedToolResult.truncationNote}`);\n\t\t\t}\n\n\t\t\treturn formatResultAsString(formattedToolResult);\n\t\t} catch (error) {\n\t\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\t\t\tlogger.error(`[${providerName}] External tool ${externalTool.name} failed (attempt ${attempts}/${MAX_TOOL_ATTEMPTS}): ${errorMsg}`);\n\n\t\t\tuserPromptErrorLogger.logToolError(externalTool.name, toolInput, error instanceof Error ? error : new Error(errorMsg));\n\n\t\t\tif (streamBuffer.hasCallback()) {\n\t\t\t\tstreamBuffer.write(`❌ **${externalTool.name} failed:**\\n\\`\\`\\`\\n${errorMsg}\\n\\`\\`\\`\\n\\n`);\n\n\t\t\t\tif (attempts < MAX_TOOL_ATTEMPTS) {\n\t\t\t\t\tstreamBuffer.write(`🔧 **Retrying with adjusted parameters...**\\n\\n`);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tthrow new Error(`Tool execution failed: ${errorMsg}`);\n\t\t}\n\t}\n}\n","import { Component, T_RESPONSE, ModelStrategy } from '../types';\nimport { ensureQueryLimit, fixScalarSubqueries, convertQuestionsToActions } from './utils';\nimport { schema } from './schema';\nimport { promptLoader } from './prompt-loader';\nimport { LLM } from '../llm';\nimport { logger } from '../utils/logger';\nimport { userPromptErrorLogger } from '../utils/user-prompt-error-logger';\nimport { getCurrentDateTimeForPrompt } from '../utils/datetime';\nimport type { Action } from '../threads/action';\nimport KB from './knowledge-base';\nimport ConversationSearch from './conversation-search';\nimport { queryCache } from '../utils/query-cache';\n// Utilities\nimport { extractPromptText } from './prompt-extractor';\nimport { StreamBuffer } from './stream-buffer';\nimport { processComponentProps } from './utils/component-props-processor';\n\n// Services\nimport { QueryExecutionService, ToolExecutorService, type ExternalTool } from './services';\nimport {\n\tMAX_QUERY_VALIDATION_RETRIES,\n\tMAX_QUERY_ATTEMPTS,\n\tMAX_TOKENS_COMPONENT_MATCHING,\n\tMAX_TOKENS_CLASSIFICATION,\n\tMAX_TOKENS_ADAPTATION,\n\tMAX_TOKENS_TEXT_RESPONSE,\n\tMAX_TOKENS_NEXT_QUESTIONS,\n\tMAX_AGENT_QUERY_LIMIT,\n\tEXACT_MATCH_SIMILARITY_THRESHOLD,\n\tDEFAULT_CONVERSATION_SIMILARITY_THRESHOLD,\n\tMAX_TOOL_CALLING_ITERATIONS,\n\tKNOWLEDGE_BASE_TOP_K\n} from './constants';\n\n// Re-export ModelStrategy for backward compatibility\nexport type { ModelStrategy } from '../types';\n\n/**\n * Task types for model selection\n * - 'complex': Text generation, component matching, parameter adaptation (uses best model in balanced mode)\n * - 'simple': Classification, action generation (uses fast model in balanced mode)\n */\nexport type TaskType = 'complex' | 'simple';\n\nexport interface BaseLLMConfig {\n\tmodel?: string;\n\tfastModel?: string; // Cheaper/faster model for simple tasks (classification, matching, actions)\n\tdefaultLimit?: number;\n\tapiKey?: string;\n\t/**\n\t * Model selection strategy:\n\t * - 'best': Use best model for all tasks (highest quality, higher cost)\n\t * - 'fast': Use fast model for all tasks (lower quality, lower cost)\n\t * - 'balanced': Use best model for complex tasks, fast model for simple tasks (default)\n\t */\n\tmodelStrategy?: ModelStrategy;\n\tconversationSimilarityThreshold?: number;\n}\n\n/**\n * BaseLLM abstract class for AI-powered component generation and matching\n * Provides common functionality for all LLM providers\n */\nexport abstract class BaseLLM {\n\tprotected model: string;\n\tprotected fastModel: string; // Used for classification, matching, actions (cheaper)\n\tprotected defaultLimit: number;\n\tprotected apiKey?: string;\n\tprotected modelStrategy: ModelStrategy;\n\tprotected conversationSimilarityThreshold: number;\n\tprotected queryService: QueryExecutionService;\n\n\tconstructor(config?: BaseLLMConfig) {\n\t\tthis.model = config?.model || this.getDefaultModel();\n\t\tthis.fastModel = config?.fastModel || this.getDefaultFastModel();\n\t\tthis.defaultLimit = config?.defaultLimit || 10;\n\t\tthis.apiKey = config?.apiKey;\n\t\tthis.modelStrategy = config?.modelStrategy || 'fast';\n\t\tthis.conversationSimilarityThreshold = config?.conversationSimilarityThreshold || DEFAULT_CONVERSATION_SIMILARITY_THRESHOLD;\n\n\t\t// Initialize query execution service\n\t\tthis.queryService = new QueryExecutionService({\n\t\t\tdefaultLimit: this.defaultLimit,\n\t\t\tgetModelForTask: (taskType) => this.getModelForTask(taskType),\n\t\t\tgetApiKey: (apiKey) => this.getApiKey(apiKey),\n\t\t\tproviderName: this.getProviderName()\n\t\t});\n\t}\n\n\t/**\n\t * Get the appropriate model based on task type and model strategy\n\t * @param taskType - 'complex' for text generation/matching, 'simple' for classification/actions\n\t * @returns The model string to use for this task\n\t */\n\tprotected getModelForTask(taskType: TaskType): string {\n\t\tswitch (this.modelStrategy) {\n\t\t\tcase 'best':\n\t\t\t\t// Use best model for all tasks\n\t\t\t\treturn this.model;\n\t\t\tcase 'fast':\n\t\t\t\t// Use fast model for all tasks\n\t\t\t\treturn this.fastModel;\n\t\t\tcase 'balanced':\n\t\t\tdefault:\n\t\t\t\t// Use best model for complex tasks, fast model for simple tasks\n\t\t\t\treturn taskType === 'complex' ? this.model : this.fastModel;\n\t\t}\n\t}\n\n\t/**\n\t * Set the model strategy at runtime\n\t * @param strategy - 'best', 'fast', or 'balanced'\n\t */\n\tpublic setModelStrategy(strategy: ModelStrategy): void {\n\t\tthis.modelStrategy = strategy;\n\t\tlogger.info(`[${this.getProviderName()}] Model strategy set to: ${strategy}`);\n\t}\n\n\t/**\n\t * Get the current model strategy\n\t * @returns The current model strategy\n\t */\n\tpublic getModelStrategy(): ModelStrategy {\n\t\treturn this.modelStrategy;\n\t}\n\n\n\t/**\n\t * Set the conversation similarity threshold at runtime\n\t * @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)\n\t */\n\tpublic setConversationSimilarityThreshold(threshold: number): void {\n\t\tif (threshold < 0 || threshold > 1) {\n\t\t\tlogger.warn(`[${this.getProviderName()}] Invalid threshold ${threshold}, must be between 0 and 1. Using default ${DEFAULT_CONVERSATION_SIMILARITY_THRESHOLD}`);\n\t\t\tthis.conversationSimilarityThreshold = DEFAULT_CONVERSATION_SIMILARITY_THRESHOLD;\n\t\t\treturn;\n\t\t}\n\t\tthis.conversationSimilarityThreshold = threshold;\n\t}\n\n\t/**\n\t * Get the current conversation similarity threshold\n\t * @returns The current threshold value\n\t */\n\tpublic getConversationSimilarityThreshold(): number {\n\t\treturn this.conversationSimilarityThreshold;\n\t}\n\n\t/**\n\t * Get the default model for this provider (used for complex tasks like text generation)\n\t */\n\tprotected abstract getDefaultModel(): string;\n\n\t/**\n\t * Get the default fast model for this provider (used for simple tasks: classification, matching, actions)\n\t * Should return a cheaper/faster model like Haiku for Anthropic\n\t */\n\tprotected abstract getDefaultFastModel(): string;\n\n\t/**\n\t * Get the default API key from environment\n\t */\n\tprotected abstract getDefaultApiKey(): string | undefined;\n\n\t/**\n\t * Get the provider name (for logging)\n\t */\n\tprotected abstract getProviderName(): string;\n\n\t/**\n\t * Get the API key (from instance, parameter, or environment)\n\t */\n\tprotected getApiKey(apiKey?: string): string | undefined {\n\t\treturn apiKey || this.apiKey || this.getDefaultApiKey();\n\t}\n\n\t/**\n\t * Check if a component contains a Form (data_modification component)\n\t * Forms have hardcoded defaultValues that become stale when cached\n\t * This checks both single Form components and Forms inside MultiComponentContainer\n\t */\n\tprotected containsFormComponent(component: any): boolean {\n\t\tif (!component) return false;\n\n\t\t// Check if this component itself is a Form\n\t\tif (component.type === 'Form' || component.name === 'DynamicForm') {\n\t\t\treturn true;\n\t\t}\n\n\t\t// Check if this is a Container with nested components\n\t\tif (component.type === 'Container' || component.name === 'MultiComponentContainer') {\n\t\t\tconst nestedComponents = component.props?.config?.components || [];\n\t\t\tfor (const nested of nestedComponents) {\n\t\t\t\tif (nested.type === 'Form' || nested.name === 'DynamicForm') {\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn false;\n\t}\n\n\t/**\n\t * Match components from text response suggestions and generate follow-up questions\n\t * Takes a text response with component suggestions (c1:type format) and matches with available components\n\t * Also generates title, description, and intelligent follow-up questions (actions) based on the analysis\n\t * All components are placed in a default MultiComponentContainer layout\n\t * @param analysisContent - The text response containing component suggestions\n\t * @param components - List of available components\n\t * @param apiKey - Optional API key\n\t * @param componentStreamCallback - Optional callback to stream primary KPI component as soon as it's identified\n\t * @returns Object containing matched components, layout title/description, and follow-up actions\n\t */\n\tasync matchComponentsFromAnalysis(\n\t\tanalysisContent: string,\n\t\tcomponents: Component[],\n\t\tuserPrompt: string,\n\t\tapiKey?: string,\n\t\tcomponentStreamCallback?: (component: Component) => void,\n\t\tdeferredTools?: any[],\n\t\texecutedTools?: any[],\n\t\tcollections?: any,\n\t\tuserId?: string,\n\t): Promise<{\n\t\tcomponents: Component[];\n\t\tlayoutTitle: string;\n\t\tlayoutDescription: string;\n\t\tactions: Action[];\n\t}> {\n\t\tconst methodStartTime = Date.now();\n\t\tconst methodName = 'matchComponentsFromAnalysis';\n\t\tlogger.info(`[${this.getProviderName()}] [TIMING] START ${methodName} | model: ${this.getModelForTask('complex')}`);\n\n\t\ttry {\n\t\t\t// Format available components for the prompt\n\t\t\tlet availableComponentsText = 'No components available';\n\t\t\tif (components && components.length > 0) {\n\t\t\t\tavailableComponentsText = components\n\t\t\t\t\t.map((comp, idx) => {\n\t\t\t\t\t\tconst keywords = comp.keywords ? comp.keywords.join(', ') : '';\n\t\t\t\t\t\tconst propsPreview = comp.props ? JSON.stringify(comp.props, null, 2) : 'No props';\n\t\t\t\t\t\treturn `${idx + 1}. ID: ${comp.id}\n Name: ${comp.name}\n Type: ${comp.type}\n Description: ${comp.description || 'No description'}\n Keywords: ${keywords}\n Props Structure: ${propsPreview}`;\n\t\t\t\t\t})\n\t\t\t\t\t.join('\\n\\n');\n\t\t\t}\n\n\t\t\t// Format deferred tools for Form generation\n\t\t\tlet deferredToolsText = 'No deferred external tools for this request.';\n\t\t\tif (deferredTools && deferredTools.length > 0) {\n\t\t\t\tdeferredToolsText = 'The following external tools need user input via a Form component.\\n' +\n\t\t\t\t\t'**IMPORTANT: Use these EXACT values when generating Form externalTool prop.**\\n\\n' +\n\t\t\t\t\tdeferredTools.map((tool, idx) => {\n\t\t\t\t\t\treturn `${idx + 1}. **${tool.name}**\n\t\t\t\t\t\t\ttoolId: \"${tool.id}\" (USE THIS EXACT VALUE - do not modify!)\n\t\t\t\t\t\t\ttoolName: \"${tool.name}\"\n\t\t\t\t\t\t\tparameters: ${JSON.stringify(tool.params || {})}\n\t\t\t\t\t\t\trequiredFields:\n\t\t\t\t\t\t\t${JSON.stringify(tool.requiredFields || [], null, 2)}`;\n\t\t\t\t\t\t}).join('\\n\\n');\n\t\t\t}\n\n\t\t\t// Format executed tools for data visualization components\n\t\t\tlet executedToolsText = 'No external tools were executed for data fetching.';\n\t\t\tif (executedTools && executedTools.length > 0) {\n\t\t\t\texecutedToolsText = 'The following external tools were executed to fetch data.\\n' +\n\t\t\t\t\texecutedTools.map((tool, idx) => {\n\t\t\t\t\t\t// Format outputSchema if available - this tells LLM exactly what fields are in the result\n\t\t\t\t\t\tlet outputSchemaText = 'Not available';\n\t\t\t\t\t\tlet fieldNamesList = '';\n\n\t\t\t\t\t\t// Get record count from formatted result (new structure)\n\t\t\t\t\t\tconst recordCount = tool.result?._totalRecords ?? 'unknown';\n\n\t\t\t\t\t\t// Format metadata if available (e.g., totalItems, totalDeadstockItems)\n\t\t\t\t\t\tlet metadataText = '';\n\t\t\t\t\t\tif (tool.result?._metadata && Object.keys(tool.result._metadata).length > 0) {\n\t\t\t\t\t\t\tconst metadataEntries = Object.entries(tool.result._metadata)\n\t\t\t\t\t\t\t\t.map(([key, value]) => `${key}: ${value}`)\n\t\t\t\t\t\t\t\t.join(', ');\n\t\t\t\t\t\t\tmetadataText = `\\n 📋 METADATA: ${metadataEntries}`;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (tool.outputSchema) {\n\t\t\t\t\t\t\tconst fields = tool.outputSchema.fields || [];\n\n\t\t\t\t\t\t\t// Create a simple list of field names for easy reference\n\t\t\t\t\t\t\tconst numericFields = fields.filter((f: any) => f.type === 'number').map((f: any) => f.name);\n\t\t\t\t\t\t\tconst stringFields = fields.filter((f: any) => f.type === 'string').map((f: any) => f.name);\n\n\t\t\t\t\t\t\tfieldNamesList = `\n 📊 NUMERIC FIELDS (use for yAxisKey, valueKey, aggregationField): ${numericFields.join(', ') || 'none'}\n 📝 STRING FIELDS (use for xAxisKey, groupBy, nameKey): ${stringFields.join(', ') || 'none'}`;\n\n\t\t\t\t\t\t\t// Format fields with details\n\t\t\t\t\t\t\tconst fieldsText = fields.map((f: any) =>\n\t\t\t\t\t\t\t\t` \"${f.name}\" (${f.type}): ${f.description}`\n\t\t\t\t\t\t\t).join('\\n');\n\t\t\t\t\t\t\toutputSchemaText = `${tool.outputSchema.description}\\n Fields:\\n${fieldsText}`;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn `${idx + 1}. **${tool.name}**\n toolId: \"${tool.id}\"\n toolName: \"${tool.name}\"\n parameters: ${JSON.stringify(tool.params || {})}\n recordCount: ${recordCount} rows returned${metadataText}\n outputSchema: ${outputSchemaText}${fieldNamesList}`;\n\t\t\t\t\t}).join('\\n\\n');\n\t\t\t}\n\n\t\t\t// Get database-specific SQL rules\n\t\t\tconst databaseRules = await promptLoader.loadDatabaseRules();\n\n\t\t\t// Get all knowledge base contexts (global, user-specific, and query-matched)\n\t\t\tlet knowledgeBaseContext = 'No additional knowledge base context available.';\n\t\t\tif (collections) {\n\t\t\t\tconst kbResult = await KB.getAllKnowledgeBase({\n\t\t\t\t\tprompt: userPrompt || analysisContent,\n\t\t\t\t\tcollections,\n\t\t\t\t\tuserId,\n\t\t\t\t\ttopK: KNOWLEDGE_BASE_TOP_K\n\t\t\t\t});\n\t\t\t\tknowledgeBaseContext = kbResult.combinedContext || knowledgeBaseContext;\n\t\t\t}\n\n\t\t\t// Schema is no longer needed - queries were already executed and schema info is in EXECUTED_TOOLS\n\t\t\tconst prompts = await promptLoader.loadPrompts('match-text-components', {\n\t\t\t\tUSER_PROMPT: userPrompt || '',\n\t\t\t\tANALYSIS_CONTENT: analysisContent,\n\t\t\t\tAVAILABLE_COMPONENTS: availableComponentsText,\n\t\t\t\tSCHEMA_DOC: 'Use column names from executed query results in EXECUTED_TOOLS.',\n\t\t\t\tDATABASE_RULES: databaseRules,\n\t\t\t\tDEFERRED_TOOLS: deferredToolsText,\n\t\t\t\tEXECUTED_TOOLS: executedToolsText,\n\t\t\t\tKNOWLEDGE_BASE_CONTEXT: knowledgeBaseContext,\n\t\t\t\tCURRENT_DATETIME: getCurrentDateTimeForPrompt()\n\t\t\t});\n\n\t\t\t// Log system prompt with clear labeling\n\t\t\tlogger.logLLMPrompt('matchComponentsFromAnalysis', 'system', extractPromptText(prompts.system));\n\t\t\tlogger.logLLMPrompt('matchComponentsFromAnalysis', 'user', `Text Analysis:\\n${analysisContent}\\n\\nExecuted Tools:\\n${executedToolsText}`);\n\n\t\t\t// Partial JSON streaming to extract answerComponent as soon as it's available\n\t\t\tlet fullResponseText = '';\n\t\t\tlet answerComponentExtracted = false;\n\n\t\t\t// Streaming callback to detect and extract answerComponent early\n\t\t\t// Capture componentStreamCallback in closure\n\t\t\tconst answerCallback = componentStreamCallback;\n\t\t\tconst partialCallback = answerCallback ? (chunk: string) => {\n\t\t\t\tfullResponseText += chunk;\n\n\t\t\t\t// Only try to extract answerComponent if we haven't already and if we have a callback\n\t\t\t\tif (!answerComponentExtracted && answerCallback) {\n\t\t\t\t\t// Check if hasAnswerComponent is true first\n\t\t\t\t\tconst hasAnswerComponentMatch = fullResponseText.match(/\"hasAnswerComponent\"\\s*:\\s*(true|false)/);\n\t\t\t\t\tif (!hasAnswerComponentMatch || hasAnswerComponentMatch[1] !== 'true') {\n\t\t\t\t\t\treturn; // No answer component or not true yet\n\t\t\t\t\t}\n\n\t\t\t\t\t// Look for \"answerComponent\": { and extract the complete object\n\t\t\t\t\tconst answerComponentStartMatch = fullResponseText.match(/\"answerComponent\"\\s*:\\s*\\{/);\n\t\t\t\t\tif (!answerComponentStartMatch) {\n\t\t\t\t\t\treturn; // answerComponent field not found yet\n\t\t\t\t\t}\n\n\t\t\t\t\t// Find the start position of the object\n\t\t\t\t\tconst startPos = answerComponentStartMatch.index! + answerComponentStartMatch[0].length - 1; // Position of opening {\n\n\t\t\t\t\t// Track brace depth to find the matching closing brace\n\t\t\t\t\tlet braceDepth = 0;\n\t\t\t\t\tlet inString = false;\n\t\t\t\t\tlet escapeNext = false;\n\t\t\t\t\tlet endPos = -1;\n\n\t\t\t\t\tfor (let i = startPos; i < fullResponseText.length; i++) {\n\t\t\t\t\t\tconst char = fullResponseText[i];\n\n\t\t\t\t\t\tif (escapeNext) {\n\t\t\t\t\t\t\tescapeNext = false;\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (char === '\\\\') {\n\t\t\t\t\t\t\tescapeNext = true;\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (char === '\"') {\n\t\t\t\t\t\t\tinString = !inString;\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (!inString) {\n\t\t\t\t\t\t\tif (char === '{') {\n\t\t\t\t\t\t\t\tbraceDepth++;\n\t\t\t\t\t\t\t} else if (char === '}') {\n\t\t\t\t\t\t\t\tbraceDepth--;\n\t\t\t\t\t\t\t\tif (braceDepth === 0) {\n\t\t\t\t\t\t\t\t\tendPos = i + 1;\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif (endPos > startPos) {\n\t\t\t\t\t\t// We found the complete answerComponent object\n\t\t\t\t\t\tconst answerComponentString = fullResponseText.substring(startPos, endPos);\n\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst answerComponentData = JSON.parse(answerComponentString);\n\n\t\t\t\t\t\t\tif (answerComponentData && answerComponentData.componentId) {\n\t\t\t\t\t\t\t\t// Find the component and build it\n\t\t\t\t\t\t\t\tconst originalComponent = components.find(c => c.id === answerComponentData.componentId);\n\n\t\t\t\t\t\t\t\tif (originalComponent) {\n\t\t\t\t\t\t\t\t\tconst answerComponent: Component = {\n\t\t\t\t\t\t\t\t\t\t...originalComponent,\n\t\t\t\t\t\t\t\t\t\tprops: {\n\t\t\t\t\t\t\t\t\t\t\t...originalComponent.props,\n\t\t\t\t\t\t\t\t\t\t\t...answerComponentData.props\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t};\n\n\n\t\t\t\t\t\t\t\t\t// VALIDATE ANSWER COMPONENT QUERY BEFORE STREAMING\n\t\t\t\t\t\t\t\t\t// If the component has a query, validate it first (with retry logic)\n\t\t\t\t\t\t\t\t\tlet answerQuery = answerComponent.props?.query;\n\n\t\t\t\t\t\t\t\t\t// Ensure query limit BEFORE first validation attempt\n\t\t\t\t\t\t\t\t\tif (answerQuery) {\n\t\t\t\t\t\t\t\t\t\tif (typeof answerQuery === 'string') {\n\t\t\t\t\t\t\t\t\t\t\tanswerQuery = ensureQueryLimit(answerQuery, this.defaultLimit, MAX_AGENT_QUERY_LIMIT);\n\t\t\t\t\t\t\t\t\t\t} else if ((answerQuery as any)?.sql) {\n\t\t\t\t\t\t\t\t\t\t\tconst queryObj = answerQuery as { sql: string; values?: any; params?: any };\n\t\t\t\t\t\t\t\t\t\t\tanswerQuery = { ...queryObj, sql: ensureQueryLimit(queryObj.sql, this.defaultLimit, MAX_AGENT_QUERY_LIMIT) };\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\tanswerComponent.props.query = answerQuery;\n\t\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t\tif (answerQuery && collections?.['database']?.['execute']) {\n\t\t\t\t\t\t\t\t\t\t// Validate the query asynchronously before streaming (with LLM fix retries)\n\t\t\t\t\t\t\t\t\t\t(async () => {\n\t\t\t\t\t\t\t\t\t\t\tconst maxRetries = MAX_QUERY_VALIDATION_RETRIES;\n\t\t\t\t\t\t\t\t\t\t\tlet attempts = 0;\n\t\t\t\t\t\t\t\t\t\t\tlet validated = false;\n\t\t\t\t\t\t\t\t\t\t\tlet currentQuery = answerQuery;\n\t\t\t\t\t\t\t\t\t\t\tlet currentQueryStr = typeof answerQuery === 'string' ? answerQuery : (answerQuery as any)?.sql || '';\n\t\t\t\t\t\t\t\t\t\t\tlet lastError = '';\n\n\n\t\t\t\t\t\t\t\t\t\t\twhile (attempts < maxRetries && !validated) {\n\t\t\t\t\t\t\t\t\t\t\t\tattempts++;\n\t\t\t\t\t\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\t\t\t\t\t\tconst cacheKey = this.queryService.getQueryCacheKey(currentQuery);\n\t\t\t\t\t\t\t\t\t\t\t\t\tif (cacheKey) {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tconst result = await collections['database']['execute']({ sql: cacheKey });\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t// Cache the result for frontend use\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tqueryCache.set(cacheKey, result);\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tvalidated = true;\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t// Update the answer component with the validated (possibly fixed) query\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tif (currentQuery !== answerQuery) {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tanswerComponent.props.query = currentQuery;\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t}\n\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t// Now stream the validated answer component\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tanswerCallback(answerComponent);\n\t\t\t\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\t\t\t} catch (validationError) {\n\t\t\t\t\t\t\t\t\t\t\t\t\tlastError = validationError instanceof Error ? validationError.message : String(validationError);\n\t\t\t\t\t\t\t\t\t\t\t\t\tlogger.warn(`[${this.getProviderName()}] Answer component query validation failed (attempt ${attempts}/${maxRetries}): ${lastError}`);\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t// Try to fix the query using LLM if we have retries left\n\t\t\t\t\t\t\t\t\t\t\t\t\tif (attempts < maxRetries) {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tlogger.info(`[${this.getProviderName()}] Requesting LLM to fix answer component query...`);\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tconst fixedQueryStr = await this.queryService.requestQueryFix(\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tcurrentQueryStr,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tlastError,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tname: answerComponent.name,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: answerComponent.type,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\ttitle: answerComponent.props?.title\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tapiKey\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t// Update the query for next attempt (with limit enforcement)\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tconst limitedFixedQuery = ensureQueryLimit(fixedQueryStr, this.defaultLimit, MAX_AGENT_QUERY_LIMIT);\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tif (typeof currentQuery === 'string') {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tcurrentQuery = limitedFixedQuery;\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tcurrentQuery = { ...(currentQuery as any), sql: limitedFixedQuery };\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tcurrentQueryStr = limitedFixedQuery;\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tlogger.info(`[${this.getProviderName()}] LLM provided fixed query for answer component, retrying...`);\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t} catch (fixError) {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tconst fixErrorMsg = fixError instanceof Error ? fixError.message : String(fixError);\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tlogger.error(`[${this.getProviderName()}] Failed to get LLM query fix for answer component: ${fixErrorMsg}`);\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tbreak; // Stop retrying if we can't get a fix\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t\t\t\tif (!validated) {\n\t\t\t\t\t\t\t\t\t\t\t\tlogger.warn(`[${this.getProviderName()}] Answer component query validation failed after ${attempts} attempts - component will be excluded`);\n\t\t\t\t\t\t\t\t\t\t\t\t// Don't stream - component will be excluded in batch validation\n\t\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\t})();\n\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\t// No query to validate (e.g., uses externalTool) - stream immediately\n\t\t\t\t\t\t\t\t\t\tlogger.info(`[${this.getProviderName()}] Answer component has no query - STREAMING TO FRONTEND NOW`);\n\t\t\t\t\t\t\t\t\t\tanswerCallback(answerComponent);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\tanswerComponentExtracted = true;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} catch (e) {\n\t\t\t\t\t\t\t// JSON not complete or malformed, will try again with more data\n\t\t\t\t\t\t\tlogger.error(`[${this.getProviderName()}] Partial answerComponent parse failed, waiting for more data...`);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} : undefined;\n\n\t\t\t// Let LLM.stream handle JSON parsing automatically, but also use partial callback\n\t\t// Use full model for component matching (requires complex SQL generation and prop mapping)\n\t\t\tconst result = await LLM.stream<any>(\n\t\t\t\t{\n\t\t\t\t\tsys: prompts.system,\n\t\t\t\t\tuser: prompts.user\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tmodel: this.getModelForTask('complex'),\n\t\t\t\t\tmaxTokens: MAX_TOKENS_COMPONENT_MATCHING,\n\t\t\t\t\ttemperature: 0,\n\t\t\t\t\tapiKey: this.getApiKey(apiKey),\n\t\t\t\t\tpartial: partialCallback\n\t\t\t\t},\n\t\t\t\ttrue // Parse as JSON\n\t\t\t) as any;\n\n\n\t\t\tconst matchedComponents = result.matchedComponents || [];\n\t\t\tconst layoutTitle = result.layoutTitle || 'Dashboard';\n\t\t\tconst layoutDescription = result.layoutDescription || 'Multi-component dashboard';\n\n\t\t\t// Safety fallback: Ensure answerComponent is in matchedComponents\n\t\t\t// The LLM prompt now instructs to include answerComponent as first item in matchedComponents,\n\t\t\t// but this fallback handles cases where LLM doesn't follow instructions\n\t\t\tif (result.hasAnswerComponent && result.answerComponent?.componentId) {\n\t\t\t\t// Check if first component matches answerComponent (as instructed in prompt)\n\t\t\t\tconst firstComponent = matchedComponents[0];\n\t\t\t\tconst answerComponentStr = JSON.stringify(result.answerComponent);\n\t\t\t\tconst firstComponentStr = firstComponent ? JSON.stringify(firstComponent) : '';\n\n\t\t\t\tif (answerComponentStr !== firstComponentStr) {\n\t\t\t\t\tlogger.info(`[${this.getProviderName()}] Fallback: Adding answerComponent to matchedComponents (LLM didn't include it as first item)`);\n\t\t\t\t\tmatchedComponents.unshift(result.answerComponent);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlogger.info(`[${this.getProviderName()}] 📦 Matched Components from LLM: ${matchedComponents.length}`);\n\t\t\tmatchedComponents.forEach((comp: any, idx: number) => {\n\t\t\t\tlogger.info(`[${this.getProviderName()}] ${idx + 1}. ${comp.componentType} (${comp.componentName}) - ${comp.originalSuggestion || 'N/A'}`);\n\t\t\t});\n\n\t\t\tlogger.file('\\n=============================\\nFull LLM response:', JSON.stringify(result, null, 2));\n\t\t\t// Convert question strings to Action objects]\n\t\t\tconst rawActions = result.actions || [];\n\t\t\tconst actions = convertQuestionsToActions(rawActions);\n\n\n\n\t\t\t// Build final component objects with full metadata\n\t\t\tconst finalComponents: Component[] = matchedComponents.map((mc: any) => {\n\t\t\t\t// Find the original component by name\n\t\t\t\tconst originalComponent = components.find(c => c.name === mc.componentName);\n\n\t\t\t\tif (!originalComponent) {\n\t\t\t\t\tlogger.warn(`[${this.getProviderName()}] Component \"${mc.componentName}\" not found in available components`);\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\n\t\t\t\t// Validate and clean up props using the props processor utility\n\t\t\t\tconst cleanedProps = processComponentProps(\n\t\t\t\t\tmc.props,\n\t\t\t\t\texecutedTools,\n\t\t\t\t\t{\n\t\t\t\t\t\tproviderName: this.getProviderName(),\n\t\t\t\t\t\tdefaultLimit: this.defaultLimit\n\t\t\t\t\t}\n\t\t\t\t);\n\n\t\t\t\t// Merge generated props with original component\n\t\t\t\treturn {\n\t\t\t\t\t...originalComponent,\n\t\t\t\t\tprops: {\n\t\t\t\t\t\t...originalComponent.props,\n\t\t\t\t\t\t...cleanedProps\n\t\t\t\t\t}\n\t\t\t\t};\n\t\t\t}).filter(Boolean) as Component[];\n\n\t\t\t// Validate component queries against the database if execute function is available\n\t\t\tlet validatedComponents = finalComponents;\n\t\t\tif (collections?.['database']?.['execute']) {\n\n\t\t\t\ttry {\n\t\t\t\t\tconst validationResult = await this.queryService.validateComponentQueries(\n\t\t\t\t\t\tfinalComponents,\n\t\t\t\t\t\tcollections,\n\t\t\t\t\t\tapiKey\n\t\t\t\t\t);\n\t\t\t\t\tvalidatedComponents = validationResult.components;\n\n\t\t\t\t\t// Count successes and failures for logging\n\t\t\t\t\tconst queriedComponents = finalComponents.filter(c => c.props?.query);\n\t\t\t\t\tconst validatedQueries = validatedComponents.filter(c => c.props?.query);\n\t\t\t\t\tlogger.info(`[${this.getProviderName()}] Query validation complete: ${validatedQueries.length}/${queriedComponents.length} queries validated`);\n\t\t\t\t} catch (validationError) {\n\t\t\t\t\tconst validationErrorMsg = validationError instanceof Error ? validationError.message : String(validationError);\n\t\t\t\t\tlogger.error(`[${this.getProviderName()}] Query validation error: ${validationErrorMsg}`);\n\t\t\t\t\t// Continue with unvalidated components on error\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tlogger.error(`[${this.getProviderName()}] Skipping query validation - database execute function not available`);\n\t\t\t}\n\n\t\t\tconst methodDuration = Date.now() - methodStartTime;\n\t\t\tlogger.info(`[${this.getProviderName()}] [TIMING] DONE ${methodName} in ${methodDuration}ms | components: ${validatedComponents.length} | actions: ${actions.length}`);\n\n\t\t\treturn {\n\t\t\t\tcomponents: validatedComponents,\n\t\t\t\tlayoutTitle,\n\t\t\t\tlayoutDescription,\n\t\t\t\tactions\n\t\t\t};\n\t\t} catch (error) {\n\t\t\tconst methodDuration = Date.now() - methodStartTime;\n\t\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\t\t\tlogger.error(`[${this.getProviderName()}] [TIMING] FAILED ${methodName} in ${methodDuration}ms | error: ${errorMsg}`);\n\n\t\t\t// Return empty results on error\n\t\t\treturn {\n\t\t\t\tcomponents: [],\n\t\t\t\tlayoutTitle: 'Dashboard',\n\t\t\t\tlayoutDescription: 'Failed to generate dashboard',\n\t\t\t\tactions: []\n\t\t\t};\n\t\t}\n\t}\n\n\t/**\n\t * Classify user question into category and detect external tools needed\n\t * Determines if question is for data analysis, requires external tools, or needs text response\n\t */\n\tasync classifyQuestionCategory(\n\t\tuserPrompt: string,\n\t\tapiKey?: string,\n\t\tconversationHistory?: string,\n\t\texternalTools?: any[]\n\t): Promise<{\n\t\tcategory: 'data_analysis' | 'data_modification' | 'general';\n\t\texternalTools: Array<{\n\t\t\ttype: string;\n\t\t\tname: string;\n\t\t\tdescription: string;\n\t\t\tparameters: Record<string, any>;\n\t\t}>;\n\t\tdataAnalysisType?: 'visualization' | 'calculation' | 'comparison' | 'trend';\n\t\treasoning: string;\n\t\tconfidence: number;\n\t}> {\n\t\tconst methodStartTime = Date.now();\n\t\tconst methodName = 'classifyQuestionCategory';\n\t\tconst promptPreview = userPrompt.substring(0, 50) + (userPrompt.length > 50 ? '...' : '');\n\t\tlogger.info(`[${this.getProviderName()}] [TIMING] START ${methodName} | model: ${this.getModelForTask('simple')} | prompt: \"${promptPreview}\"`);\n\n\t\ttry {\n\t\t\t// Check if any external tool is a database source tool (has 'sql' parameter)\n\t\t\t// Database source tools include their schema in tool descriptions\n\t\t\tconst hasDatabaseSourceTool = externalTools?.some(tool => {\n\t\t\t\tconst params = tool.params || {};\n\t\t\t\treturn 'sql' in params || Object.keys(params).some(key => key.toLowerCase() === 'sql');\n\t\t\t}) ?? false;\n\n\t\t\t// Get database schema documentation - skip if database source tools are available\n\t\t\t// (schema is included in source tool descriptions)\n\t\t\tlet schemaDoc: string;\n\t\t\tif (hasDatabaseSourceTool) {\n\t\t\t\tschemaDoc = 'Schema is available in the database source tool descriptions below.';\n\t\t\t} else {\n\t\t\t\tschemaDoc = schema.generateSchemaDocumentation();\n\t\t\t}\n\n\t\t\t// Format available external tools for the system prompt\n\t\t\tconst availableToolsDoc = externalTools && externalTools.length > 0\n\t\t\t\t? externalTools.map(tool => {\n\t\t\t\t\tconst paramsStr = Object.entries(tool.params || {})\n\t\t\t\t\t\t.map(([key, type]) => `${key}: ${type}`)\n\t\t\t\t\t\t.join(', ');\n\t\t\t\t\treturn `- **${tool.name}** (id: ${tool.id})\\n Description: ${tool.description}\\n Parameters: ${paramsStr}`;\n\t\t\t\t}).join('\\n\\n')\n\t\t\t\t: 'No external tools available';\n\n\t\t\tconst prompts = await promptLoader.loadPrompts('category-classification', {\n\t\t\t\tUSER_PROMPT: userPrompt,\n\t\t\t\tCONVERSATION_HISTORY: conversationHistory || 'No previous conversation',\n\t\t\t\tAVAILABLE_TOOLS: availableToolsDoc,\n\t\t\t\tSCHEMA_DOC: schemaDoc || 'No database schema available',\n\t\t\t\tCURRENT_DATETIME: getCurrentDateTimeForPrompt()\n\t\t\t});\n\n\t\t\t// Log prompts for debugging\n\t\t\tlogger.logLLMPrompt('classifyQuestionCategory', 'system', extractPromptText(prompts.system));\n\t\t\tlogger.logLLMPrompt('classifyQuestionCategory', 'user', extractPromptText(prompts.user));\n\n\t\t\t// Use appropriate model based on strategy (simple task)\n\t\t\tconst result = await LLM.stream<any>(\n\t\t\t\t{\n\t\t\t\t\tsys: prompts.system,\n\t\t\t\t\tuser: prompts.user\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tmodel: this.getModelForTask('simple'),\n\t\t\t\t\tmaxTokens: MAX_TOKENS_CLASSIFICATION,\n\t\t\t\t\ttemperature: 0,\n\t\t\t\t\tapiKey: this.getApiKey(apiKey)\n\t\t\t\t},\n\t\t\t\ttrue // Parse as JSON\n\t\t\t) as any;\n\n\t\t\tconst methodDuration = Date.now() - methodStartTime;\n\t\t\tlogger.info(`[${this.getProviderName()}] [TIMING] DONE ${methodName} in ${methodDuration}ms | category: ${result.category} | confidence: ${result.confidence}% | tools: ${(result.externalTools || []).length}`);\n\n\t\t\treturn {\n\t\t\t\tcategory: result.category || 'data_analysis',\n\t\t\t\texternalTools: result.externalTools || [],\n\t\t\t\tdataAnalysisType: result.dataAnalysisType,\n\t\t\t\treasoning: result.reasoning || 'No reasoning provided',\n\t\t\t\tconfidence: result.confidence || 0\n\t\t\t};\n\t\t} catch (error) {\n\t\t\tconst methodDuration = Date.now() - methodStartTime;\n\t\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\t\t\tlogger.error(`[${this.getProviderName()}] [TIMING] FAILED ${methodName} in ${methodDuration}ms | error: ${errorMsg}`);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t/**\n\t * Adapt UI block parameters based on current user question\n\t * Takes a matched UI block from semantic search and modifies its props to answer the new question\n\t * Also adapts the cached text response to match the new question\n\t */\n\tasync adaptUIBlockParameters(\n\t\tcurrentUserPrompt: string,\n\t\toriginalUserPrompt: string,\n\t\tmatchedUIBlock: any,\n\t\tapiKey?: string,\n\t\tcachedTextResponse?: string\n\t): Promise<{\n\t\tsuccess: boolean;\n\t\tadaptedComponent?: Component;\n\t\tadaptedTextResponse?: string;\n\t\tparametersChanged?: Array<{ field: string; reason: string }>;\n\t\texplanation: string;\n\t}> {\n\t\tconst methodStartTime = Date.now();\n\t\tconst methodName = 'adaptUIBlockParameters';\n\t\tconst promptPreview = currentUserPrompt.substring(0, 50) + (currentUserPrompt.length > 50 ? '...' : '');\n\t\tlogger.info(`[${this.getProviderName()}] [TIMING] START ${methodName} | model: ${this.getModelForTask('complex')} | prompt: \"${promptPreview}\"`);\n\n\t\ttry {\n\t\t\t// Support both old format (generatedComponentMetadata) and new format (component)\n\t\t\tconst component = matchedUIBlock?.generatedComponentMetadata || matchedUIBlock?.component;\n\n\t\t\tif (!matchedUIBlock || !component) {\n\t\t\t\tconst methodDuration = Date.now() - methodStartTime;\n\t\t\t\tlogger.info(`[${this.getProviderName()}] [TIMING] DONE ${methodName} in ${methodDuration}ms | result: no component found`);\n\t\t\t\treturn {\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\texplanation: 'No component found in matched UI block'\n\t\t\t\t};\n\t\t\t}\n\n\t\t// Get database-specific SQL rules\n\t\tconst databaseRules = await promptLoader.loadDatabaseRules();\n\n\t\t// Schema is no longer needed - prompt instructs to preserve table/column names from original query\n\t\tconst prompts = await promptLoader.loadPrompts('adapt-ui-block-params', {\n\t\t\tORIGINAL_USER_PROMPT: originalUserPrompt,\n\t\t\tCURRENT_USER_PROMPT: currentUserPrompt,\n\t\t\tMATCHED_UI_BLOCK_COMPONENT: JSON.stringify(component, null, 2),\n\t\t\tCOMPONENT_PROPS: JSON.stringify(component.props, null, 2),\n\t\t\tCACHED_TEXT_RESPONSE: cachedTextResponse || 'No cached text response available',\n\t\t\tSCHEMA_DOC: 'Preserve table/column names from the original query - they are already validated.',\n\t\t\tDATABASE_RULES: databaseRules,\n\t\t\tCURRENT_DATETIME: getCurrentDateTimeForPrompt()\n\t\t});\n\n\t\t\tconst result = await LLM.stream<any>(\n\t\t\t\t{\n\t\t\t\t\tsys: prompts.system,\n\t\t\t\t\tuser: prompts.user\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tmodel: this.getModelForTask('complex'),\n\t\t\t\t\tmaxTokens: MAX_TOKENS_ADAPTATION,\n\t\t\t\t\ttemperature: 0,\n\t\t\t\t\tapiKey: this.getApiKey(apiKey)\n\t\t\t\t},\n\t\t\t\ttrue // Parse as JSON\n\t\t\t) as any;\n\n\t\t\tif (!result.success) {\n\t\t\t\tconst methodDuration = Date.now() - methodStartTime;\n\t\t\t\tlogger.info(`[${this.getProviderName()}] [TIMING] DONE ${methodName} in ${methodDuration}ms | result: adaptation failed - ${result.reason}`);\n\t\t\t\treturn {\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\texplanation: result.explanation || 'Adaptation not possible'\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Ensure query has proper LIMIT clause if modified\n\t\t\tif (result.adaptedComponent?.props?.query) {\n\t\t\t\tresult.adaptedComponent.props.query = ensureQueryLimit(\n\t\t\t\t\tresult.adaptedComponent.props.query,\n\t\t\t\t\tthis.defaultLimit\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst methodDuration = Date.now() - methodStartTime;\n\t\t\tlogger.info(`[${this.getProviderName()}] [TIMING] DONE ${methodName} in ${methodDuration}ms | result: success | changes: ${(result.parametersChanged || []).length}`);\n\n\t\t\treturn {\n\t\t\t\tsuccess: true,\n\t\t\t\tadaptedComponent: result.adaptedComponent,\n\t\t\t\tadaptedTextResponse: result.adaptedTextResponse,\n\t\t\t\tparametersChanged: result.parametersChanged,\n\t\t\t\texplanation: result.explanation || 'Parameters adapted successfully'\n\t\t\t};\n\t\t} catch (error) {\n\t\t\tconst methodDuration = Date.now() - methodStartTime;\n\t\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\t\t\tlogger.error(`[${this.getProviderName()}] [TIMING] FAILED ${methodName} in ${methodDuration}ms | error: ${errorMsg}`);\n\t\t\treturn {\n\t\t\t\tsuccess: false,\n\t\t\t\texplanation: `Error adapting parameters: ${errorMsg}`\n\t\t\t};\n\t\t}\n\t}\n\n\t/**\n\t * Generate text-based response for user question\n\t * This provides conversational text responses instead of component generation\n\t * Supports tool calling for query execution with automatic retry on errors (max 3 attempts)\n\t * After generating text response, if components are provided, matches suggested components\n\t */\n\tasync generateTextResponse(\n\t\tuserPrompt: string,\n\t\tapiKey?: string,\n\t\tconversationHistory?: string,\n\t\tstreamCallback?: (chunk: string) => void,\n\t\tcollections?: any,\n\t\tcomponents?: Component[],\n\t\texternalTools?: any[],\n\t\tcategory?: 'data_analysis' | 'data_modification' | 'general',\n\t\tuserId?: string\n\t): Promise<T_RESPONSE> {\n\t\tconst methodStartTime = Date.now();\n\t\tconst methodName = 'generateTextResponse';\n\t\tconst promptPreview = userPrompt.substring(0, 50) + (userPrompt.length > 50 ? '...' : '');\n\t\tlogger.info(`[${this.getProviderName()}] [TIMING] START ${methodName} | model: ${this.getModelForTask('complex')} | category: ${category} | prompt: \"${promptPreview}\"`);\n\n\t\tconst errors: string[] = [];\n\n\t\ttry {\n\t\t\t// Step 1: Format available external tools for the system prompt\n\t\t\t// Separate tools by execution type\n\t\t\tlet availableToolsDoc = 'No external tools are available for this request.';\n\t\t\tif (externalTools && externalTools.length > 0) {\n\t\t\t\tlogger.info(`[${this.getProviderName()}] External tools available: ${externalTools.map(t => t.name).join(', ')}`);\n\n\t\t\t\t// Separate immediate and deferred tools\n\t\t\t\tconst immediateTools = externalTools.filter(t => t.executionType === 'immediate' || (t.executionType === 'deferred' && t.userProvidedData));\n\t\t\t\tconst deferredTools = externalTools.filter(t => t.executionType === 'deferred' && !t.userProvidedData);\n\n\t\t\t\tlet toolsDocParts: string[] = [];\n\n\t\t\t\t// Document IMMEDIATE tools (to be executed)\n\t\t\t\tif (immediateTools.length > 0) {\n\t\t\t\t\tconst immediateDoc = '## IMMEDIATE EXECUTION TOOLS\\n' +\n\t\t\t\t\t\t'Execute these tools right away:\\n\\n' +\n\t\t\t\t\t\timmediateTools.map((tool, idx) => {\n\t\t\t\t\t\t\tconst paramsText = Object.entries(tool.params || {})\n\t\t\t\t\t\t\t\t.map(([key, value]) => {\n\t\t\t\t\t\t\t\t\tconst valueType = typeof value;\n\t\t\t\t\t\t\t\t\tif (valueType === 'string' && ['string', 'number', 'integer', 'boolean', 'array', 'object'].includes(String(value).toLowerCase())) {\n\t\t\t\t\t\t\t\t\t\treturn `- ${key}: ${value}`;\n\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\treturn `- ${key}: ${JSON.stringify(value)} (default value - use this)`;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t\t.join('\\n ');\n\n\t\t\t\t\t\t\t// If deferred tool with user data, include the data\n\t\t\t\t\t\t\tlet userDataText = '';\n\t\t\t\t\t\t\tif (tool.userProvidedData) {\n\t\t\t\t\t\t\t\tuserDataText = '\\n **User Provided Data** (use these values):\\n ' +\n\t\t\t\t\t\t\t\t\tObject.entries(tool.userProvidedData)\n\t\t\t\t\t\t\t\t\t\t.map(([key, value]) => `- ${key}: ${JSON.stringify(value)}`)\n\t\t\t\t\t\t\t\t\t\t.join('\\n ');\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\treturn `${idx + 1}. **${tool.name}** (ID: ${tool.id})\\n Execution Type: IMMEDIATE\\n Description: ${tool.description}\\n Parameters:\\n ${paramsText}${userDataText}`;\n\t\t\t\t\t\t}).join('\\n\\n');\n\t\t\t\t\ttoolsDocParts.push(immediateDoc);\n\t\t\t\t}\n\n\t\t\t\t// Document DEFERRED tools (need user input - will generate Form)\n\t\t\t\tif (deferredTools.length > 0) {\n\t\t\t\t\tconst deferredDoc = '## DEFERRED TOOLS (DO NOT EXECUTE)\\n' +\n\t\t\t\t\t\t'These tools need user input. A Form component will be generated to collect the data.\\n' +\n\t\t\t\t\t\t'**DO NOT call these tools.** Instead, acknowledge the request and inform user that a form will be shown.\\n\\n' +\n\t\t\t\t\t\tdeferredTools.map((tool, idx) => {\n\t\t\t\t\t\t\tconst requiredFieldsText = (tool.requiredFields || [])\n\t\t\t\t\t\t\t\t.map((f: any) => `- ${f.label || f.name} (${f.type})${f.required ? ' *required*' : ''}`)\n\t\t\t\t\t\t\t\t.join('\\n ');\n\n\t\t\t\t\t\t\treturn `${idx + 1}. **${tool.name}** (ID: ${tool.id})\\n Execution Type: DEFERRED (needs form input)\\n Description: ${tool.description}\\n Reason: ${tool.executionReason || 'Write operation requires user confirmation'}\\n Required Fields:\\n ${requiredFieldsText || '(fields will be determined by form)'}`;\n\t\t\t\t\t\t}).join('\\n\\n');\n\t\t\t\t\ttoolsDocParts.push(deferredDoc);\n\t\t\t\t}\n\n\t\t\t\tavailableToolsDoc = toolsDocParts.join('\\n\\n---\\n\\n');\n\t\t\t}\n\n\t\t\t// Check if any external tool is a database source tool (has 'sql' parameter)\n\t\t\t// Database source tools include their schema in tool descriptions\n\t\t\tconst hasDatabaseSourceToolForSchema = externalTools?.some(tool => {\n\t\t\t\tconst params = tool.params || {};\n\t\t\t\treturn 'sql' in params || Object.keys(params).some(key => key.toLowerCase() === 'sql');\n\t\t\t}) ?? false;\n\n\t\t\t// Get schema documentation - skip if database source tools are available\n\t\t\t// (schema is included in source tool descriptions)\n\t\t\tlet schemaDoc: string;\n\t\t\tif (hasDatabaseSourceToolForSchema) {\n\t\t\t\tschemaDoc = 'Schema is available in the database source tool descriptions above.';\n\t\t\t\tlogger.info(`[${this.getProviderName()}] Skipping schema generation (database source tools include schema)`);\n\t\t\t} else {\n\t\t\t\tschemaDoc = schema.generateSchemaDocumentation();\n\t\t\t}\n\n\t\t\t// Get database-specific SQL rules\n\t\t\tconst databaseRules = await promptLoader.loadDatabaseRules();\n\n\t\t\t// Get all knowledge base contexts (global, user-specific, and query-matched)\n\t\t\tconst kbResult = await KB.getAllKnowledgeBase({\n\t\t\t\tprompt: userPrompt,\n\t\t\t\tcollections,\n\t\t\t\tuserId,\n\t\t\t\ttopK: KNOWLEDGE_BASE_TOP_K\n\t\t\t});\n\t\t\tconst knowledgeBaseContext = kbResult.combinedContext;\n\n\t\t\tconst prompts = await promptLoader.loadPrompts('text-response', {\n\t\t\t\tUSER_PROMPT: userPrompt,\n\t\t\t\tCONVERSATION_HISTORY: conversationHistory || 'No previous conversation',\n\t\t\t\tSCHEMA_DOC: schemaDoc,\n\t\t\t\tDATABASE_RULES: databaseRules,\n\t\t\t\tKNOWLEDGE_BASE_CONTEXT: knowledgeBaseContext || 'No additional knowledge base context available.',\n\t\t\t\tAVAILABLE_EXTERNAL_TOOLS: availableToolsDoc,\n\t\t\t\tCURRENT_DATETIME: getCurrentDateTimeForPrompt()\n\t\t\t});\n\n\t\t\t// Log prompts for debugging\n\t\t\tlogger.logLLMPrompt('generateTextResponse', 'system', extractPromptText(prompts.system));\n\t\t\tlogger.logLLMPrompt('generateTextResponse', 'user', extractPromptText(prompts.user));\n\n\t\t\t// Check if any external tool is a database source tool (has 'sql' parameter)\n\t\t\t// Database source tools include their schema and can execute SQL directly\n\t\t\tconst hasDatabaseSourceTool = externalTools?.some(tool => {\n\t\t\t\tconst params = tool.params || {};\n\t\t\t\treturn 'sql' in params || Object.keys(params).some(key => key.toLowerCase() === 'sql');\n\t\t\t}) ?? false;\n\n\t\t\t// Define tools array\n\t\t\tconst tools: any[] = [];\n\n\t\t\t// Only add execute_query if no database source tools are available\n\t\t\t// This provides backward compatibility while allowing source tools to take over\n\t\t\tif (!hasDatabaseSourceTool) {\n\t\t\t\ttools.push({\n\t\t\t\t\tname: 'execute_query',\n\t\t\t\t\tdescription: 'Executes a parameterized SQL query against the database. CRITICAL: NEVER hardcode literal values in WHERE/HAVING conditions - ALWAYS use $paramName placeholders and pass actual values in params object.',\n\t\t\t\t\tinput_schema: {\n\t\t\t\t\t\ttype: 'object',\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tsql: {\n\t\t\t\t\t\t\t\ttype: 'string',\n\t\t\t\t\t\t\t\tdescription: 'SQL query with $paramName placeholders for ALL literal values in WHERE/HAVING conditions. NEVER hardcode values like WHERE status = \\'Delivered\\' - instead use WHERE status = $status. Table names, column names, and SQL keywords stay as-is.'\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tparams: {\n\t\t\t\t\t\t\t\ttype: 'object',\n\t\t\t\t\t\t\t\tdescription: 'REQUIRED when SQL has WHERE/HAVING conditions. Maps each $paramName placeholder (without $) to its actual value. Pattern: WHERE col = $name → params: { \"name\": \"value\" }. Every placeholder in SQL MUST have a corresponding entry here.',\n\t\t\t\t\t\t\t\tadditionalProperties: true\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\treasoning: {\n\t\t\t\t\t\t\t\ttype: 'string',\n\t\t\t\t\t\t\t\tdescription: 'Brief explanation of what this query does and why it answers the user\\'s question.'\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t},\n\t\t\t\t\t\trequired: ['sql'],\n\t\t\t\t\t\tadditionalProperties: false\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\tlogger.info(`[${this.getProviderName()}] Added execute_query tool (no database source tools found)`);\n\t\t\t} else {\n\t\t\t\tlogger.info(`[${this.getProviderName()}] Skipping execute_query tool (database source tool available)`);\n\t\t\t}\n\n\t\t\t// Add external tools to the tools array (if any were identified by category classification)\n\t\t\t// IMPORTANT: Only add tools that should be executed (immediate OR deferred with user data)\n\t\t\tif (externalTools && externalTools.length > 0) {\n\t\t\t\t// Filter to only executable tools\n\t\t\t\tconst executableTools = externalTools.filter(t =>\n\t\t\t\t\tt.executionType === 'immediate' || (t.executionType === 'deferred' && t.userProvidedData)\n\t\t\t\t);\n\n\n\t\t\t\t// Track added tool IDs to avoid duplicates (Anthropic requires unique tool names)\n\t\t\t\tconst addedToolIds = new Set<string>();\n\n\t\t\t\texecutableTools.forEach(tool => {\n\t\t\t\t\t// Skip if this tool ID has already been added (same tool called multiple times with different params)\n\t\t\t\t\tif (addedToolIds.has(tool.id)) {\n\t\t\t\t\t\tlogger.info(`[${this.getProviderName()}] Skipping duplicate tool definition: ${tool.id} (already added)`);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\taddedToolIds.add(tool.id);\n\t\t\t\t\tconst properties: any = {};\n\t\t\t\t\tconst required: string[] = [];\n\n\t\t\t\t\tObject.entries(tool.params || {}).forEach(([key, typeOrValue]) => {\n\t\t\t\t\t\t// Normalize type to valid JSON Schema types\n\t\t\t\t\t\t// Handle both type strings and actual values from LLM classification\n\t\t\t\t\t\tlet schemaType: string;\n\t\t\t\t\t\tlet hasDefaultValue = false;\n\t\t\t\t\t\tlet defaultValue: any;\n\n\t\t\t\t\t\t// Check if this is an actual value (not a type string)\n\t\t\t\t\t\tconst valueType = typeof typeOrValue;\n\t\t\t\t\t\tif (valueType === 'number') {\n\t\t\t\t\t\t\t// If it's a number value, infer the type and mark as having default\n\t\t\t\t\t\t\tschemaType = Number.isInteger(typeOrValue) ? 'integer' : 'number';\n\t\t\t\t\t\t\thasDefaultValue = true;\n\t\t\t\t\t\t\tdefaultValue = typeOrValue;\n\t\t\t\t\t\t} else if (valueType === 'boolean') {\n\t\t\t\t\t\t\tschemaType = 'boolean';\n\t\t\t\t\t\t\thasDefaultValue = true;\n\t\t\t\t\t\t\tdefaultValue = typeOrValue;\n\t\t\t\t\t\t} else if (Array.isArray(typeOrValue)) {\n\t\t\t\t\t\t\tschemaType = 'array';\n\t\t\t\t\t\t\thasDefaultValue = true;\n\t\t\t\t\t\t\tdefaultValue = typeOrValue;\n\t\t\t\t\t\t} else if (valueType === 'object' && typeOrValue !== null) {\n\t\t\t\t\t\t\tschemaType = 'object';\n\t\t\t\t\t\t\thasDefaultValue = true;\n\t\t\t\t\t\t\tdefaultValue = typeOrValue;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// It's a string - could be a type name or a string value\n\t\t\t\t\t\t\tconst typeStr = String(typeOrValue).toLowerCase().trim();\n\n\t\t\t\t\t\t\t// Check if it's a known type name\n\t\t\t\t\t\t\tif (typeStr === 'string' || typeStr === 'str') {\n\t\t\t\t\t\t\t\tschemaType = 'string';\n\t\t\t\t\t\t\t\t// Type name, no default\n\t\t\t\t\t\t\t} else if (typeStr === 'number' || typeStr === 'num' || typeStr === 'float' || typeStr === 'double') {\n\t\t\t\t\t\t\t\tschemaType = 'number';\n\t\t\t\t\t\t\t\t// Type name, no default\n\t\t\t\t\t\t\t} else if (typeStr === 'integer' || typeStr === 'int') {\n\t\t\t\t\t\t\t\tschemaType = 'integer';\n\t\t\t\t\t\t\t\t// Type name, no default\n\t\t\t\t\t\t\t} else if (typeStr === 'boolean' || typeStr === 'bool') {\n\t\t\t\t\t\t\t\tschemaType = 'boolean';\n\t\t\t\t\t\t\t\t// Type name, no default\n\t\t\t\t\t\t\t} else if (typeStr === 'array' || typeStr === 'list') {\n\t\t\t\t\t\t\t\tschemaType = 'array';\n\t\t\t\t\t\t\t\t// Type name, no default\n\t\t\t\t\t\t\t} else if (typeStr === 'object' || typeStr === 'dict') {\n\t\t\t\t\t\t\t\tschemaType = 'object';\n\t\t\t\t\t\t\t\t// Type name, no default\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t// Not a recognized type name, assume it's a string value - infer type as string\n\t\t\t\t\t\t\t\tschemaType = 'string';\n\t\t\t\t\t\t\t\thasDefaultValue = true;\n\t\t\t\t\t\t\t\tdefaultValue = typeOrValue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst propertySchema: any = {\n\t\t\t\t\t\t\ttype: schemaType,\n\t\t\t\t\t\t\tdescription: `${key} parameter for ${tool.name}`\n\t\t\t\t\t\t};\n\n\t\t\t\t\t\t// Add default value to schema if present\n\t\t\t\t\t\tif (hasDefaultValue) {\n\t\t\t\t\t\t\tpropertySchema.default = defaultValue;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Only mark as required if no default value\n\t\t\t\t\t\t\trequired.push(key);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tproperties[key] = propertySchema;\n\t\t\t\t\t});\n\n\t\t\t\t\tconst inputSchema: any = {\n\t\t\t\t\t\ttype: 'object',\n\t\t\t\t\t\tproperties,\n\t\t\t\t\t\tadditionalProperties: false\n\t\t\t\t\t};\n\n\t\t\t\t\t// Only include required array if there are required fields\n\t\t\t\t\tif (required.length > 0) {\n\t\t\t\t\t\tinputSchema.required = required;\n\t\t\t\t\t}\n\n\t\t\t\t\ttools.push({\n\t\t\t\t\t\tname: tool.id,\n\t\t\t\t\t\tdescription: tool.description,\n\t\t\t\t\t\tinput_schema: inputSchema\n\t\t\t\t\t});\n\t\t\t\t});\n\n\t\t\t\tlogger.info(`[${this.getProviderName()}] Added ${addedToolIds.size} unique tool definitions from ${executableTools.length} tool calls (${externalTools.length - executableTools.length} deferred tools await form input)`);\n\t\t\t\t// Debug: Log the complete tools array to verify schema\n\t\t\t\tlogger.debug(`[${this.getProviderName()}] Complete tools array:`, JSON.stringify(tools, null, 2));\n\t\t\t}\n\n\t\t\t// Create stream buffer for smooth text delivery\n\t\t\tconst streamBuffer = new StreamBuffer(streamCallback);\n\n\t\t\t// Create tool executor service\n\t\t\tconst toolExecutor = new ToolExecutorService({\n\t\t\t\tproviderName: this.getProviderName(),\n\t\t\t\tcollections,\n\t\t\t\tstreamBuffer\n\t\t\t});\n\n\t\t\t// Convert externalTools to the expected format\n\t\t\tconst executableExternalTools: ExternalTool[] = externalTools?.map(t => ({\n\t\t\t\tid: t.id,\n\t\t\t\tname: t.name,\n\t\t\t\tdescription: t.description,\n\t\t\t\tfn: t.fn,\n\t\t\t\tlimit: t.limit,\n\t\t\t\toutputSchema: t.outputSchema,\n\t\t\t\texecutionType: t.executionType,\n\t\t\t\tuserProvidedData: t.userProvidedData\n\t\t\t})) || [];\n\n\t\t\t// Create tool handler\n\t\t\tconst toolHandler = toolExecutor.createToolHandler(executableExternalTools);\n\n\t\t\t// Use tool calling with streaming\n\t\t\t// Max iterations needs to account for:\n\t\t\t// - Up to MAX_QUERY_ATTEMPTS query retry attempts\n\t\t\t// - Up to MAX_TOOL_ATTEMPTS external tool retry attempts per tool\n\t\t\t// - 1 additional iteration for final text response\n\t\t\t// - Buffer for multiple tool calls in one iteration\n\t\t\tconst result = await LLM.streamWithTools(\n\t\t\t\t{\n\t\t\t\t\tsys: prompts.system,\n\t\t\t\t\tuser: prompts.user\n\t\t\t\t},\n\t\t\t\ttools,\n\t\t\t\ttoolHandler,\n\t\t\t\t{\n\t\t\t\t\tmodel: this.getModelForTask('complex'),\n\t\t\t\t\tmaxTokens: MAX_TOKENS_TEXT_RESPONSE,\n\t\t\t\t\ttemperature: 0,\n\t\t\t\t\tapiKey: this.getApiKey(apiKey),\n\t\t\t\t\tpartial: streamBuffer.hasCallback() ? (chunk: string) => streamBuffer.write(chunk) : undefined\n\t\t\t\t},\n\t\t\t\tMAX_TOOL_CALLING_ITERATIONS\n\t\t\t);\n\n\n\t\t\t// Use fullStreamedText if available (contains all streamed content), otherwise fallback to result\n\t\t\tconst textResponse = streamBuffer.getFullText() || result || 'I apologize, but I was unable to generate a response.';\n\n\t\t\t// Check if max query attempts were reached\n\t\t\tif (toolExecutor.isMaxAttemptsReached()) {\n\t\t\t\tconst methodDuration = Date.now() - methodStartTime;\n\t\t\t\tlogger.warn(`[${this.getProviderName()}] [TIMING] DONE ${methodName} in ${methodDuration}ms | result: max attempts reached`);\n\n\t\t\t\treturn {\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\terrors: [`Maximum query attempts (${MAX_QUERY_ATTEMPTS}) reached. Unable to generate a valid query for your question.`],\n\t\t\t\t\tdata: {\n\t\t\t\t\t\ttext: textResponse, // Include the streamed text showing all attempts\n\t\t\t\t\t\tactions: [],\n\t\t\t\t\t\tmethod: `${this.getProviderName()}-text-response-max-attempts`\n\t\t\t\t\t}\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Flush any remaining buffered text before component matching\n\t\t\tstreamBuffer.flush();\n\n\t\t\t// Send marker message to frontend indicating text generation is complete\n\t\t\t// and component matching is starting (skip for general category)\n\t\t\tif (streamBuffer.hasCallback() && components && components.length > 0 && category !== 'general') {\n\t\t\t\tstreamBuffer.write('\\n\\n📊 **Generating visualization components...**\\n\\n');\n\t\t\t\tstreamBuffer.write('__TEXT_COMPLETE__COMPONENT_GENERATION_START__');\n\t\t\t}\n\n\t\t\t// If components are provided, match them from the text response\n\t\t\t// Skip component generation for 'general' category (greetings, small talk, etc.)\n\t\t\tlet matchedComponents: Component[] = [];\n\t\t\tlet layoutTitle = 'Dashboard';\n\t\t\tlet layoutDescription = 'Multi-component dashboard';\n\t\t\tlet actions: Action[] = [];\n\n\t\t\tif (category === 'general') {\n\t\t\t\tlogger.info(`[${this.getProviderName()}] General category - wrapping text response in DynamicMarkdownBlock`);\n\n\t\t\t\t// Generate follow-up actions for general questions using text response as context\n\t\t\t\tconst nextQuestions = await this.generateNextQuestions(\n\t\t\t\t\tuserPrompt,\n\t\t\t\t\tnull, // no component\n\t\t\t\t\tundefined, // no component data\n\t\t\t\t\tapiKey,\n\t\t\t\t\tconversationHistory,\n\t\t\t\t\ttextResponse // pass text response as context\n\t\t\t\t);\n\t\t\t\tactions = convertQuestionsToActions(nextQuestions);\n\n\t\t\t\t// Wrap text response in a DynamicMarkdownBlock component\n\t\t\t\t// Strip any <DashboardComponents> tags from the content since they're not needed\n\t\t\t\tconst markdownContent = textResponse\n\t\t\t\t\t.replace(/<DashboardComponents>[\\s\\S]*?<\\/DashboardComponents>/g, '')\n\t\t\t\t\t.trim();\n\n\t\t\t\tmatchedComponents = [{\n\t\t\t\t\tid: 'dynamic-markdown-block',\n\t\t\t\t\tname: 'DynamicMarkdownBlock',\n\t\t\t\t\ttype: 'MarkdownBlock',\n\t\t\t\t\tdescription: 'Text response rendered as markdown',\n\t\t\t\t\tprops: {\n\t\t\t\t\t\tcontent: markdownContent\n\t\t\t\t\t}\n\t\t\t\t}];\n\t\t\t\tlayoutTitle = '';\n\t\t\t\tlayoutDescription = '';\n\t\t\t} else if (components && components.length > 0) {\n\n\t\t\t\t// Component streaming callback to send answer component immediately to frontend\n\t\t\t\t// For data_modification, skip answer component streaming - show full dashboard directly\n\t\t\t\tconst componentStreamCallback = (streamBuffer.hasCallback() && category === 'data_analysis') ? (component: Component) => {\n\t\t\t\t\t// Send special marker to indicate answer component is ready\n\t\t\t\t\tconst answerMarker = `__ANSWER_COMPONENT_START__${JSON.stringify(component)}__ANSWER_COMPONENT_END__`;\n\t\t\t\t\tstreamBuffer.write(answerMarker);\n\t\t\t\t} : undefined;\n\n\t\t\t\t// Extract deferred tools (tools that need user input via Form)\n\t\t\t\t// For data_modification category, tools with Create/Update/Delete in name are deferred\n\t\t\t\tconst deferredTools = externalTools?.filter(t => {\n\t\t\t\t\t// Explicit deferred type\n\t\t\t\t\tif (t.executionType === 'deferred' && !t.userProvidedData) return true;\n\t\t\t\t\t// Infer deferred for data_modification category based on tool name patterns\n\t\t\t\t\tif (category === 'data_modification' && !t.userProvidedData) {\n\t\t\t\t\t\tconst name = (t.name || t.id || '').toLowerCase();\n\t\t\t\t\t\tconst isWriteOperation = /create|add|insert|new|update|edit|modify|delete|remove|send/.test(name);\n\t\t\t\t\t\tif (isWriteOperation) {\n\t\t\t\t\t\t\tlogger.info(`[${this.getProviderName()}] Inferred deferred execution for tool: ${t.name}`);\n\t\t\t\t\t\t\treturn true;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\treturn false;\n\t\t\t\t}) || [];\n\n\t\t\t\tif (deferredTools.length > 0) {\n\t\t\t\t\tlogger.info(`[${this.getProviderName()}] Passing ${deferredTools.length} deferred tools for Form generation`);\n\t\t\t\t}\n\n\t\t\t\t// logger.info(`[${this.getProviderName()}] passing deferred tools to the matching function: ${JSON.stringify(deferredTools,null,2)}`);\n\t\t\t\t// logger.info(`[${this.getProviderName()}] passing executed tools to the matching function: ${JSON.stringify(executedToolsList,null,2)}`);\n\n\t\t\t\t// Sanitize textResponse before passing to component matching LLM\n\t\t\t\t// Replace <DataTable>JSON</DataTable> with a note to prevent LLM from embedding data directly\n\t\t\t\t// The actual data was already streamed to the UI, but the component matching LLM should write queries\n\t\t\t\t// The LLM still has access to: executed SQL query in text, outputSchema from executedTools, and recordCount\n\t\t\t\tconst sanitizedTextResponse = textResponse.replace(\n\t\t\t\t\t/<DataTable>[\\s\\S]*?<\\/DataTable>/g,\n\t\t\t\t\t'<DataTable>[Data preview removed - for table components, REUSE the exact SQL query shown above (the one that returned these results). Do NOT write a new query or embed data in props.]</DataTable>'\n\t\t\t\t);\n\n\t\t\t\tconst matchResult = await this.matchComponentsFromAnalysis(\n\t\t\t\t\tsanitizedTextResponse,\n\t\t\t\t\tcomponents,\n\t\t\t\t\tuserPrompt,\n\t\t\t\t\tapiKey,\n\t\t\t\t\tcomponentStreamCallback,\n\t\t\t\t\tdeferredTools,\n\t\t\t\t\ttoolExecutor.getExecutedTools(),\n\t\t\t\t\tcollections,\n\t\t\t\t\tuserId,\n\t\t\t\t);\n\t\t\t\tmatchedComponents = matchResult.components;\n\t\t\t\tlayoutTitle = matchResult.layoutTitle;\n\t\t\t\tlayoutDescription = matchResult.layoutDescription;\n\t\t\t\tactions = matchResult.actions;\n\t\t\t}\n\n\t\t\tlet container_componet:Component | null = null;\n\n\t\t\tif(matchedComponents.length > 0){\n\t\t\t\t// Create MultiComponentContainer with matched components\n\t\t\t\tcontainer_componet = {\n\t\t\t\t\tid: `container_${Date.now()}`,\n\t\t\t\t\tname: 'MultiComponentContainer',\n\t\t\t\t\ttype: 'Container',\n\t\t\t\t\tdescription: layoutDescription,\n\t\t\t\t\tprops: {\n\t\t\t\t\t\tconfig: {\n\t\t\t\t\t\t\ttitle: layoutTitle,\n\t\t\t\t\t\t\tdescription: layoutDescription,\n\t\t\t\t\t\t\tcomponents: matchedComponents\n\t\t\t\t\t\t},\n\t\t\t\t\t\tactions: actions\n\t\t\t\t\t}\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst methodDuration = Date.now() - methodStartTime;\n\t\t\tlogger.info(`[${this.getProviderName()}] [TIMING] DONE ${methodName} in ${methodDuration}ms | result: success | components: ${matchedComponents.length} | actions: ${actions.length}`);\n\n\t\t\treturn {\n\t\t\t\tsuccess: true,\n\t\t\t\tdata: {\n\t\t\t\t\ttext: textResponse,\n\t\t\t\t\tcomponent: container_componet,\n\t\t\t\t\tactions: actions,\n\t\t\t\t\tmethod: `${this.getProviderName()}-text-response`\n\t\t\t\t},\n\t\t\t\terrors: []\n\t\t\t};\n\t\t} catch (error) {\n\t\t\tconst methodDuration = Date.now() - methodStartTime;\n\t\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\t\t\tlogger.error(`[${this.getProviderName()}] [TIMING] FAILED ${methodName} in ${methodDuration}ms | error: ${errorMsg}`);\n\n\t\t\t// Log LLM error with full details\n\t\t\tuserPromptErrorLogger.logLlmError(\n\t\t\t\tthis.getProviderName(),\n\t\t\t\tthis.model,\n\t\t\t\t'generateTextResponse',\n\t\t\t\terror instanceof Error ? error : new Error(errorMsg),\n\t\t\t\t{ userPrompt }\n\t\t\t);\n\n\t\t\terrors.push(errorMsg);\n\n\t\t\treturn {\n\t\t\t\tsuccess: false,\n\t\t\t\terrors,\n\t\t\t\tdata: {\n\t\t\t\t\ttext: 'I apologize, but I encountered an error while processing your question. Please try rephrasing or ask something else.',\n\t\t\t\t\tactions: [],\n\t\t\t\t\tmethod: `${this.getProviderName()}-text-response-error`\n\t\t\t\t}\n\t\t\t};\n\t\t}\n\t}\n\n\n\t/**\n\t * Main orchestration function with semantic search and multi-step classification\n\t * NEW FLOW (Recommended):\n\t * 1. Semantic search: Check previous conversations (>60% match)\n\t * - If match found → Adapt UI block parameters and return\n\t * 2. Category classification: Determine if data_analysis, requires_external_tools, or text_response\n\t * 3. Route appropriately based on category and response mode\n\t */\n\tasync handleUserRequest(\n\t\tuserPrompt: string,\n\t\tcomponents: Component[],\n\t\tapiKey?: string,\n\t\tconversationHistory?: string,\n\t\tresponseMode: 'component' | 'text' = 'text',\n\t\tstreamCallback?: (chunk: string) => void,\n\t\tcollections?: any,\n\t\texternalTools?: any[],\n\t\tuserId?: string\n\t): Promise<T_RESPONSE> {\n\t\tconst startTime = Date.now();\n\n\t\t// Clear log file for new request and log the user prompt\n\t\tlogger.clearFile();\n\t\tlogger.logLLMPrompt('handleUserRequest', 'user', `User Prompt: ${userPrompt}`);\n\n\t\ttry {\n\t\t\t// STEP 1: Semantic search - Check if this question matches previous conversations (>99% similarity)\n\t\t\t\n\t\t\t// Only use exact matches (>99% similarity) - no semantic threshold matching\n\t\t\tconst conversationMatch = await ConversationSearch.searchConversationsWithReranking({\n\t\t\t\tuserPrompt,\n\t\t\t\tcollections,\n\t\t\t\tuserId,\n\t\t\t\tsimilarityThreshold: EXACT_MATCH_SIMILARITY_THRESHOLD\n\t\t\t});\n\n\t\t\tif (conversationMatch) {\n\t\t\t\t// Found a match! Check if it's a 100% match (exact same question)\n\t\t\t\tlogger.info(`[${this.getProviderName()}] ✓ Found matching conversation with ${(conversationMatch.similarity * 100).toFixed(2)}% similarity`);\n\n\t\t\t\t// Extract the component from the UI block\n\t\t\t\tconst rawComponent = conversationMatch.uiBlock?.component || conversationMatch.uiBlock?.generatedComponentMetadata;\n\n\t\t\t\t// Check if component is valid (not null, not empty object)\n\t\t\t\tconst isValidComponent = rawComponent && typeof rawComponent === 'object' && Object.keys(rawComponent).length > 0;\n\t\t\t\tconst component = isValidComponent ? rawComponent : null;\n\n\t\t\t\t// Extract text response - DB stores as 'analysis'\n\t\t\t\tconst cachedTextResponse = conversationMatch.uiBlock?.analysis || conversationMatch.uiBlock?.textResponse || conversationMatch.uiBlock?.text || '';\n\n\n\t\t\t\t// Check if cached component contains a Form (data_modification)\n\t\t\t\t// Forms have hardcoded defaultValues that become stale - always fetch fresh data\n\t\t\t\tif (this.containsFormComponent(component)) {\n\t\t\t\t\tlogger.info(`[${this.getProviderName()}] Skipping cached result - Form components contain stale defaultValues, fetching fresh data`);\n\t\t\t\t\t// Fall through to category classification to generate fresh form with current values\n\t\t\t\t}\n\t\t\t\t// Check if this was a general conversation (no component)\n\t\t\t\t// If so, return text response for exact match or skip adaptation for similar match\n\t\t\t\telse if (!component) {\n\t\t\t\t\tif (conversationMatch.similarity >= EXACT_MATCH_SIMILARITY_THRESHOLD) {\n\t\t\t\t\t\t// Exact match for general question - return cached text response\n\t\t\t\t\t\tconst elapsedTime = Date.now() - startTime;\n\t\t\t\t\t\tlogger.info(`[${this.getProviderName()}] ✓ Exact match for general question - returning cached text response (${elapsedTime}ms)`);\n\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tsuccess: true,\n\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\ttext: cachedTextResponse,\n\t\t\t\t\t\t\t\tcomponent: null,\n\t\t\t\t\t\t\t\tactions: conversationMatch.uiBlock?.actions || [],\n\t\t\t\t\t\t\t\treasoning: `Exact match from previous general conversation`,\n\t\t\t\t\t\t\t\tmethod: `${this.getProviderName()}-semantic-match-general`,\n\t\t\t\t\t\t\t\tsemanticSimilarity: conversationMatch.similarity\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\terrors: []\n\t\t\t\t\t\t};\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// Similar match but no component (general question) - skip adaptation and process fresh\n\t\t\t\t\t\tlogger.info(`[${this.getProviderName()}] Similar match but no component (general question) - processing fresh`);\n\t\t\t\t\t\t// Fall through to category classification\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\t// Has component - proceed with normal flow\n\t\t\t\t\t// If similarity >= 99%, it's essentially the same question - return UI block directly\n\t\t\t\t\tif (conversationMatch.similarity >= EXACT_MATCH_SIMILARITY_THRESHOLD) {\n\t\t\t\t\t\tconst elapsedTime = Date.now() - startTime;\n\t\t\t\t\t\tlogger.info(`[${this.getProviderName()}] ✓ 100% match - returning UI block directly without adaptation (${elapsedTime}ms)`);\n\n\t\t\t\t\t\t// Stream the cached text response to frontend if available\n\t\t\t\t\t\tif (streamCallback && cachedTextResponse) {\n\t\t\t\t\t\t\tlogger.info(`[${this.getProviderName()}] Streaming cached text response to frontend`);\n\t\t\t\t\t\t\tstreamCallback(cachedTextResponse);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Extract cached actions from UI block\n\t\t\t\t\t\tconst cachedActions = conversationMatch.uiBlock?.actions || [];\n\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tsuccess: true,\n\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\ttext: cachedTextResponse,\n\t\t\t\t\t\t\t\tcomponent: component,\n\t\t\t\t\t\t\t\tactions: cachedActions,\n\t\t\t\t\t\t\t\treasoning: `Exact match from previous conversation (${(conversationMatch.similarity * 100).toFixed(2)}% similarity)`,\n\t\t\t\t\t\t\t\tmethod: `${this.getProviderName()}-semantic-match-exact`,\n\t\t\t\t\t\t\t\tsemanticSimilarity: conversationMatch.similarity\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\terrors: []\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\t// Similarity between 60-99%, adapt the UI block parameters\n\t\t\t\t\tlogger.info(`[${this.getProviderName()}] Adapting parameters for similar question...`);\n\n\t\t\t\t\t// Get the original prompt from the matched UIBlock\n\t\t\t\t\tconst originalPrompt = conversationMatch.metadata?.userPrompt || 'Previous question';\n\n\t\t\t\t\t// Adapt the UI block parameters to the current question\n\t\t\t\t\tconst adaptResult = await this.adaptUIBlockParameters(\n\t\t\t\t\t\tuserPrompt,\n\t\t\t\t\t\toriginalPrompt,\n\t\t\t\t\t\tconversationMatch.uiBlock,\n\t\t\t\t\t\tapiKey,\n\t\t\t\t\t\tcachedTextResponse\n\t\t\t\t\t);\n\n\t\t\t\t\tif (adaptResult.success && adaptResult.adaptedComponent) {\n\t\t\t\t\t\tconst elapsedTime = Date.now() - startTime;\n\t\t\t\t\t\tlogger.info(`[${this.getProviderName()}] ✓ Successfully adapted UI block parameters (${elapsedTime}ms)`);\n\n\t\t\t\t\t\t// Use adapted text response if available, otherwise fall back to cached\n\t\t\t\t\t\tconst textResponseToUse = adaptResult.adaptedTextResponse || cachedTextResponse;\n\n\t\t\t\t\t\t// Stream the adapted text response to frontend if available\n\t\t\t\t\t\tif (streamCallback && textResponseToUse) {\n\t\t\t\t\t\t\tlogger.info(`[${this.getProviderName()}] Streaming ${adaptResult.adaptedTextResponse ? 'adapted' : 'cached'} text response to frontend`);\n\t\t\t\t\t\t\tstreamCallback(textResponseToUse);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Extract cached actions from UI block\n\t\t\t\t\t\tconst cachedActions = conversationMatch.uiBlock?.actions || [];\n\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tsuccess: true,\n\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\ttext: textResponseToUse,\n\t\t\t\t\t\t\t\tcomponent: adaptResult.adaptedComponent,\n\t\t\t\t\t\t\t\tactions: cachedActions,\n\t\t\t\t\t\t\t\treasoning: `Adapted from previous conversation: ${originalPrompt}`,\n\t\t\t\t\t\t\t\tmethod: `${this.getProviderName()}-semantic-match`,\n\t\t\t\t\t\t\t\tsemanticSimilarity: conversationMatch.similarity,\n\t\t\t\t\t\t\t\tparametersChanged: adaptResult.parametersChanged\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\terrors: []\n\t\t\t\t\t\t};\n\t\t\t\t\t} else {\n\t\t\t\t\t\tlogger.info(`[${this.getProviderName()}] Could not adapt matched conversation: ${adaptResult.explanation}, continuing to category classification`);\n\t\t\t\t\t\t// Fall through to category classification\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tlogger.info(`[${this.getProviderName()}] No matching previous conversations found, proceeding to category classification`);\n\t\t\t}\n\n\t\t\t// STEP 2: Category classification - Determine the type of request\n\t\t\tlogger.info(`[${this.getProviderName()}] Step 2: Classifying question category...`);\n\n\t\t\tconst categoryClassification = await this.classifyQuestionCategory(\n\t\t\t\tuserPrompt,\n\t\t\t\tapiKey,\n\t\t\t\tconversationHistory,\n\t\t\t\texternalTools\n\t\t\t);\n\n\t\t\tlogger.info(\n\t\t\t\t`[${this.getProviderName()}] Question classified as: ${categoryClassification.category} (confidence: ${categoryClassification.confidence}%)`\n\t\t\t);\n\n\t\t\t// STEP 3: Prepare tools based on category classification\n\t\t\t// Only use tools identified by the LLM classification\n\t\t\tlet toolsToUse: any[] = [];\n\t\t\tif (categoryClassification.externalTools && categoryClassification.externalTools.length > 0) {\n\t\t\t\tlogger.info(`[${this.getProviderName()}] Identified ${categoryClassification.externalTools.length} external tools needed: ${categoryClassification.externalTools.map((t: any) => t.name || t.type).join(', ')}`);\n\n\t\t\t\t// Log the raw external tools from category classification for debugging\n\t\t\t\tlogger.debug(`[${this.getProviderName()}] Raw external tools from classification: ${JSON.stringify(categoryClassification.externalTools, null, 2)}`);\n\n\t\t\t\t// Filter and map only tools that exist in the registered tools list\n\t\t\t\t// This prevents hallucinated/non-existent tools from being executed\n\t\t\t\ttoolsToUse = categoryClassification.externalTools.reduce<typeof toolsToUse>((acc, t: any) => {\n\t\t\t\t\t// Look up the real tool implementation\n\t\t\t\t\tconst realTool = externalTools?.find(tool => tool.id === t.type);\n\n\t\t\t\t\t// Skip tools that don't exist in registered tools\n\t\t\t\t\tif (!realTool) {\n\t\t\t\t\t\tlogger.warn(`[${this.getProviderName()}] Tool ${t.type} (${t.name}) not found in registered tools - skipping (likely hallucinated)`);\n\t\t\t\t\t\treturn acc;\n\t\t\t\t\t}\n\n\n\t\t\t\t\tacc.push({\n\t\t\t\t\t\tid: t.type,\n\t\t\t\t\t\tname: t.name,\n\t\t\t\t\t\tdescription: t.description,\n\t\t\t\t\t\tparams: t.parameters || {},\n\t\t\t\t\t\t// Include execution type info from category classification\n\t\t\t\t\t\texecutionType: t.executionType || 'immediate',\n\t\t\t\t\t\texecutionReason: t.executionReason || '',\n\t\t\t\t\t\trequiredFields: t.requiredFields || [],\n\t\t\t\t\t\tuserProvidedData: t.userProvidedData || null,\n\t\t\t\t\t\t// Include outputSchema from real tool for component config generation\n\t\t\t\t\t\toutputSchema: realTool.outputSchema,\n\t\t\t\t\t\tfn: realTool.fn\n\t\t\t\t\t});\n\t\t\t\t\treturn acc;\n\t\t\t\t}, []);\n\n\t\t\t\t// Log how many tools were valid vs hallucinated\n\t\t\t\tconst validCount = toolsToUse.length;\n\t\t\t\tconst hallucinatedCount = categoryClassification.externalTools.length - validCount;\n\t\t\t\tif (hallucinatedCount > 0) {\n\t\t\t\t\tlogger.warn(`[${this.getProviderName()}] Filtered out ${hallucinatedCount} hallucinated/non-existent tools, ${validCount} valid tools remaining`);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// STEP 4: Route based on category\n\n\t\t\t// Generate text response with identified tools\n\t\t\tconst textResponse = await this.generateTextResponse(\n\t\t\t\tuserPrompt,\n\t\t\t\tapiKey,\n\t\t\t\tconversationHistory,\n\t\t\t\tstreamCallback,\n\t\t\t\tcollections,\n\t\t\t\tcomponents,\n\t\t\t\ttoolsToUse,\n\t\t\t\tcategoryClassification.category,\n\t\t\t\tuserId\n\t\t\t);\n\n\t\t\tconst elapsedTime = Date.now() - startTime;\n\t\t\tlogger.info(`[${this.getProviderName()}] Total time taken: ${elapsedTime}ms (${(elapsedTime / 1000).toFixed(2)}s)`);\n\t\t\treturn textResponse;\n\t\t} catch (error) {\n\t\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\t\t\tlogger.error(`[${this.getProviderName()}] Error in handleUserRequest: ${errorMsg}`);\n\n\t\t\t// Log error with full details\n\t\t\tuserPromptErrorLogger.logError(\n\t\t\t\t'handleUserRequest',\n\t\t\t\terror instanceof Error ? error : new Error(errorMsg),\n\t\t\t\t{ userPrompt }\n\t\t\t);\n\n\t\t\tconst elapsedTime = Date.now() - startTime;\n\t\t\tlogger.info(`[${this.getProviderName()}] Total time taken: ${elapsedTime}ms (${(elapsedTime / 1000).toFixed(2)}s)`);\n\n\t\t\treturn {\n\t\t\t\tsuccess: false,\n\t\t\t\terrors: [errorMsg],\n\t\t\t\tdata: {\n\t\t\t\t\ttext: 'I apologize, but I encountered an error processing your request. Please try again.',\n\t\t\t\t\tmethod: `${this.getProviderName()}-orchestration-error`\n\t\t\t\t}\n\t\t\t};\n\t\t}\n\t}\n\n\t/**\n\t * Generate next questions that the user might ask based on the original prompt and generated component\n\t * This helps provide intelligent suggestions for follow-up queries\n\t * For general/conversational questions without components, pass textResponse instead\n\t */\n\tasync generateNextQuestions(\n\t\toriginalUserPrompt: string,\n\t\tcomponent?: Component | null,\n\t\tcomponentData?: Record<string, unknown>,\n\t\tapiKey?: string,\n\t\tconversationHistory?: string,\n\t\ttextResponse?: string\n\t): Promise<string[]> {\n\t\tconst methodStartTime = Date.now();\n\t\tconst methodName = 'generateNextQuestions';\n\t\tconst promptPreview = originalUserPrompt.substring(0, 50) + (originalUserPrompt.length > 50 ? '...' : '');\n\t\tlogger.info(`[${this.getProviderName()}] [TIMING] START ${methodName} | model: ${this.getModelForTask('simple')} | prompt: \"${promptPreview}\"`);\n\n\t\ttry {\n\t\t\t// Build component info - use component if available, otherwise use text response\n\t\t\tlet component_info: string;\n\t\t\tif (component) {\n\t\t\t\tcomponent_info = `\n\t\t\t\tComponent Name: ${component.name}\n\t\t\t\tComponent Type: ${component.type}\n\t\t\t\tComponent Description: ${component.description || 'No description'}\n\t\t\t\tComponent Props: ${component.props ? JSON.stringify(component.props, null, 2) : 'No props'}\n\t\t\t`;\n\t\t\t} else if (textResponse) {\n\t\t\t\tcomponent_info = `\n\t\t\t\tResponse Type: Text/Conversational Response\n\t\t\t\tResponse Content: ${textResponse.substring(0, 1000)}${textResponse.length > 1000 ? '...' : ''}\n\t\t\t`;\n\t\t\t} else {\n\t\t\t\tcomponent_info = 'No component or response context available';\n\t\t\t}\n\n\t\t\tconst component_data = componentData ? `Component Data: ${JSON.stringify(componentData, null, 2)}` : '';\n\n\t\t\tconst prompts = await promptLoader.loadPrompts('actions', {\n\t\t\t\tORIGINAL_USER_PROMPT: originalUserPrompt,\n\t\t\t\tCOMPONENT_INFO: component_info,\n\t\t\t\tCOMPONENT_DATA: component_data,\n\t\t\t\tCONVERSATION_HISTORY: conversationHistory || 'No previous conversation',\n\t\t\t\tCURRENT_DATETIME: getCurrentDateTimeForPrompt()\n\t\t\t});\n\n\t\t\t// Use appropriate model based on strategy (simple task)\n\t\t\tconst result = await LLM.stream<any>(\n\t\t\t\t{\n\t\t\t\t\tsys: prompts.system,\n\t\t\t\t\tuser: prompts.user\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tmodel: this.getModelForTask('simple'),\n\t\t\t\t\tmaxTokens: MAX_TOKENS_NEXT_QUESTIONS,\n\t\t\t\t\ttemperature: 0,\n\t\t\t\t\tapiKey: this.getApiKey(apiKey)\n\t\t\t\t},\n\t\t\t\ttrue // Parse as JSON\n\t\t\t) as any;\n\n\t\t\tconst nextQuestions = result.nextQuestions || [];\n\n\t\t\tconst methodDuration = Date.now() - methodStartTime;\n\t\t\tlogger.info(`[${this.getProviderName()}] [TIMING] DONE ${methodName} in ${methodDuration}ms | questions: ${nextQuestions.length}`);\n\n\t\t\treturn nextQuestions;\n\t\t} catch (error) {\n\t\t\tconst methodDuration = Date.now() - methodStartTime;\n\t\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\t\t\tlogger.error(`[${this.getProviderName()}] [TIMING] FAILED ${methodName} in ${methodDuration}ms | error: ${errorMsg}`);\n\t\t\t// Return empty array on error instead of throwing\n\t\t\treturn [];\n\t\t}\n\t}\n\n}\n","import dotenv from 'dotenv';\nimport { BaseLLM, BaseLLMConfig } from './base-llm';\n\ndotenv.config({ quiet: true });\n\nexport interface GroqLLMConfig extends BaseLLMConfig {}\n\n/**\n * GroqLLM class for handling AI-powered component generation and matching using Groq\n */\nexport class GroqLLM extends BaseLLM {\n\tconstructor(config?: GroqLLMConfig) {\n\t\tsuper(config);\n\t}\n\n\tprotected getDefaultModel(): string {\n\t\treturn 'groq/openai/gpt-oss-120b';\n\t}\n\n\tprotected getDefaultFastModel(): string {\n\t\t// Llama 3.1 8B is extremely fast and cheap on Groq\n\t\treturn 'groq/llama-3.1-8b-instant';\n\t}\n\n\tprotected getDefaultApiKey(): string | undefined {\n\t\treturn process.env.GROQ_API_KEY;\n\t}\n\n\tprotected getProviderName(): string {\n\t\treturn 'Groq';\n\t}\n}\n\n// Export a singleton instance\nexport const groqLLM = new GroqLLM();\n","import dotenv from 'dotenv';\nimport { BaseLLM, BaseLLMConfig } from './base-llm';\n\ndotenv.config({ quiet: true });\n\nexport interface GeminiLLMConfig extends BaseLLMConfig {}\n\n/**\n * GeminiLLM class for handling AI-powered component generation and matching using Google Gemini\n */\nexport class GeminiLLM extends BaseLLM {\n\tconstructor(config?: GeminiLLMConfig) {\n\t\tsuper(config);\n\t}\n\n\tprotected getDefaultModel(): string {\n\t\treturn 'gemini/gemini-2.5-flash';\n\t}\n\n\tprotected getDefaultFastModel(): string {\n\t\t// Gemini Flash is already fast/cheap, use the same\n\t\treturn 'gemini/gemini-2.5-flash';\n\t}\n\n\tprotected getDefaultApiKey(): string | undefined {\n\t\treturn process.env.GEMINI_API_KEY;\n\t}\n\n\tprotected getProviderName(): string {\n\t\treturn 'Gemini';\n\t}\n}\n\n// Export a singleton instance\nexport const geminiLLM = new GeminiLLM();\n","import dotenv from 'dotenv';\nimport { BaseLLM, BaseLLMConfig } from './base-llm';\n\ndotenv.config({ quiet: true });\n\nexport interface OpenAILLMConfig extends BaseLLMConfig {}\n\n/**\n * OpenAILLM class for handling AI-powered component generation and matching using OpenAI GPT models\n */\nexport class OpenAILLM extends BaseLLM {\n\tconstructor(config?: OpenAILLMConfig) {\n\t\tsuper(config);\n\t}\n\n\tprotected getDefaultModel(): string {\n\t\treturn 'openai/gpt-4.1';\n\t}\n\n\tprotected getDefaultFastModel(): string {\n\t\t// GPT-4o-mini is 60x cheaper than GPT-4o and faster - ideal for simple tasks\n\t\treturn 'openai/gpt-4o-mini';\n\t}\n\n\tprotected getDefaultApiKey(): string | undefined {\n\t\treturn process.env.OPENAI_API_KEY;\n\t}\n\n\tprotected getProviderName(): string {\n\t\treturn 'OpenAI';\n\t}\n}\n\n// Export a singleton instance\nexport const openaiLLM = new OpenAILLM();\n","/**\n * Agent-based User Response Flow\n *\n * Replaces the old flow (classifyCategory → generateTextResponse → matchComponents)\n * with the multi-agent architecture:\n *\n * 1. Semantic search (cached match) — reuses existing ConversationSearch\n * 2. MainAgent.handleQuestion() — routes, queries sources, analyzes (ONE LLM call)\n * 3. If data was queried → matchComponentsFromAnalysis() on the BaseLLM instance\n *\n * No separate classification LLM call — the MainAgent handles routing internally\n * via source summaries (~100 tokens each) instead of full schemas.\n */\n\nimport { Component, T_RESPONSE, LLMProvider } from '../types';\nimport { convertQuestionsToActions } from './utils';\nimport { logger } from '../utils/logger';\nimport { userPromptErrorLogger } from '../utils/user-prompt-error-logger';\nimport { queryCache } from '../utils/query-cache';\nimport type { Action } from '../threads/action';\nimport ConversationSearch from './conversation-search';\nimport { StreamBuffer } from './stream-buffer';\nimport type { ExternalTool } from './services/tool-executor-service';\nimport KB from './knowledge-base';\n\n// Multi-agent system\nimport { MainAgent, DEFAULT_AGENT_CONFIG, generateAgentComponents } from './agents';\nimport type { AgentConfig, WorkflowDescriptor } from './agents';\n\n// Script flow\nimport { ScriptStore, ScriptMatcher, runScript, generateScriptComponents, assembleComponentsFromSpecs } from './scripts';\nimport type { ScriptComponentSpec } from './scripts';\nimport type { ExecutedToolInfo } from './services/tool-executor-service';\n\n// LLM provider singletons (for next questions generation)\nimport { anthropicLLM } from './anthropic';\nimport { groqLLM } from './groq';\nimport { geminiLLM } from './gemini';\nimport { openaiLLM } from './openai';\nimport type { BaseLLM } from './base-llm';\n\nimport {\n\tKNOWLEDGE_BASE_TOP_K,\n\tTOOL_TRACKING_SAMPLE_ROWS,\n\tMAX_ROWS_RENDERED,\n} from './constants';\n\nimport { LLM } from '../llm';\nimport { promptLoader } from './prompt-loader';\nimport { getCurrentDateTimeForPrompt } from '../utils/datetime';\n\n/**\n * Walk a cached container/component tree and return true if any leaf component\n * targets the `script_dataset` sentinel whose queryId is no longer in the\n * query cache. Such components are unrecoverable from the conversation cache\n * alone — the post-JS computed data they reference lives only in queryCache,\n * not in `_queryMap`, so `rehydrateCachedComponent` can't reconstruct it.\n *\n * Used to fall through to the agent flow (which can re-run the saved script\n * via the script matcher) instead of returning a UIBlock whose components\n * will render the \"Script dataset expired\" sentinel error.\n */\nfunction hasExpiredScriptDataset(component: any): boolean {\n\tif (!component || typeof component !== 'object') return false;\n\n\tconst et = component?.props?.externalTool;\n\tif (et?.toolId === 'script_dataset') {\n\t\tconst qid = et?.parameters?.queryId;\n\t\tif (typeof qid === 'string' && qid.length > 0) {\n\t\t\t// queryId is an encrypted token containing the SQL itself — `getQuery`\n\t\t\t// only returns null on decryption failure. The data cache is a separate\n\t\t\t// in-memory map looked up via the token; that's what evicts. So we\n\t\t\t// need to check `.data`, not the whole entry. Empty data means the\n\t\t\t// frontend will hit the script_dataset sentinel's \"expired\" error.\n\t\t\tconst stored = queryCache.getQuery(qid);\n\t\t\tif (!stored || stored.data == null) return true;\n\t\t}\n\t}\n\n\tconst children = component?.props?.config?.components;\n\tif (Array.isArray(children)) {\n\t\tfor (const child of children) {\n\t\t\tif (hasExpiredScriptDataset(child)) return true;\n\t\t}\n\t}\n\n\treturn false;\n}\n\n/**\n * Re-hydrate a cached component by re-registering its SQL queries in the query cache.\n * The cached component contains a `_queryMap` (queryId → SQL) persisted alongside it.\n * We deep-clone to avoid mutating the original cached object (so future cache hits still work).\n */\nfunction rehydrateCachedComponent(component: any): any {\n\tconst qMap = component?.props?.config?._queryMap;\n\tif (!qMap || typeof qMap !== 'object' || Object.keys(qMap).length === 0) return component;\n\n\t// Deep clone — don't mutate the cached original\n\tconst cloned = JSON.parse(JSON.stringify(component));\n\n\t// Check if all existing queryIds are still valid — if so, reuse them as-is\n\tconst allValid = Object.keys(qMap).every(qId => queryCache.getQuery(qId) !== null);\n\tif (allValid) {\n\t\tlogger.info(`[AgentFlow] All ${Object.keys(qMap).length} cached queryIds still valid — reusing`);\n\t\tdelete cloned.props.config._queryMap;\n\t\treturn cloned;\n\t}\n\n\t// Some/all queryIds expired — re-register and replace with fresh queryIds\n\tconst idMap: Record<string, string> = {};\n\tfor (const [oldId, sql] of Object.entries(qMap)) {\n\t\tif (queryCache.getQuery(oldId) !== null) {\n\t\t\tidMap[oldId] = oldId; // Still valid, keep it\n\t\t} else {\n\t\t\tidMap[oldId] = queryCache.storeQuery(sql); // Expired, re-register\n\t\t}\n\t}\n\n\t// Replace queryIds in child components\n\tconst components = cloned.props?.config?.components;\n\tif (Array.isArray(components)) {\n\t\tfor (const comp of components) {\n\t\t\t// externalTool.parameters.queryId\n\t\t\tconst etqId = comp.props?.externalTool?.parameters?.queryId;\n\t\t\tif (etqId && idMap[etqId]) {\n\t\t\t\tcomp.props.externalTool.parameters.queryId = idMap[etqId];\n\t\t\t}\n\t\t\t// direct props.queryId\n\t\t\tif (comp.props?.queryId && idMap[comp.props.queryId]) {\n\t\t\t\tcomp.props.queryId = idMap[comp.props.queryId];\n\t\t\t}\n\t\t}\n\t}\n\n\t// Remove _queryMap from the CLONE only (original stays intact for future cache hits)\n\tdelete cloned.props.config._queryMap;\n\n\tconst rehydratedCount = Object.values(idMap).filter(newId => !Object.keys(qMap).includes(newId)).length;\n\tlogger.info(`[AgentFlow] Re-hydrated ${rehydratedCount}/${Object.keys(qMap).length} expired queries with fresh queryIds`);\n\treturn cloned;\n}\n\n/**\n * Get the BaseLLM instance for a given provider name.\n */\nfunction getLLMInstance(provider: LLMProvider): BaseLLM {\n\tswitch (provider) {\n\t\tcase 'anthropic': return anthropicLLM;\n\t\tcase 'groq': return groqLLM;\n\t\tcase 'gemini': return geminiLLM;\n\t\tcase 'openai': return openaiLLM;\n\t\tdefault: return anthropicLLM;\n\t}\n}\n\n/**\n * Build the queryId for a script-produced dataset.\n *\n * For VIRTUAL datasets (ctx.emit / the synthetic `computed:_final` entry — post-JS\n * data with no re-runnable SQL), the encrypted token holds a regeneration\n * descriptor `{ kind, recipeId, params, datasetRef }` instead of a placeholder\n * SQL string. On cache expiry the backend re-runs the saved recipe to produce\n * FRESH data (no stale persistence) — see backend/docs/SCRIPT-FLOW-SCALING-ISSUES.md #6\n * and project memory `project_script_dataset_expiry_fix`.\n *\n * Real SQL datasets keep storing their SQL — they already self-heal by re-executing.\n */\nfunction storeScriptDatasetQuery(\n\tq: { sourceId: string; sql: string; data: any[]; count: number; virtual?: boolean },\n\tctx: { recipeId?: string; params?: Record<string, any> },\n): string {\n\t// STAGE 3 (RENDER) cap: the cached payload is what the UI component draws and\n\t// what the data cache holds, so bound it here regardless of how many rows the\n\t// script's SQL returned. A script that needs to show more should pre-aggregate;\n\t// the cap is a safety net (and keeps the in-memory cache from holding huge rows).\n\tconst data = q.data.length > MAX_ROWS_RENDERED ? q.data.slice(0, MAX_ROWS_RENDERED) : q.data;\n\tif (q.data.length > MAX_ROWS_RENDERED) {\n\t\tlogger.warn(`[AgentFlow] Script dataset \"${q.sourceId}\" had ${q.data.length} rows — capped to ${MAX_ROWS_RENDERED} for render/cache. Consider pre-aggregating in the script.`);\n\t}\n\tconst count = data.length;\n\tif (q.virtual && ctx.recipeId) {\n\t\tconst descriptor = {\n\t\t\tkind: 'script_dataset' as const,\n\t\t\trecipeId: ctx.recipeId,\n\t\t\tparams: ctx.params || {},\n\t\t\tdatasetRef: q.sourceId,\n\t\t};\n\t\treturn queryCache.storeQuery(JSON.stringify(descriptor), { data, count });\n\t}\n\treturn queryCache.storeQuery(q.sql, { data, count });\n}\n\n/**\n * Main entry point for the agent-based user response flow.\n * Drop-in replacement for `get_user_response()` with the same signature and return type.\n */\nexport const get_agent_user_response = async (\n\tprompt: string,\n\tcomponents: Component[],\n\tanthropicApiKey?: string,\n\tgroqApiKey?: string,\n\tgeminiApiKey?: string,\n\topenaiApiKey?: string,\n\tllmProviders?: LLMProvider[],\n\tconversationHistory?: string,\n\tstreamCallback?: (chunk: string) => void,\n\tcollections?: any,\n\texternalTools?: any[],\n\tuserId?: string,\n\tmainAgentModel?: string,\n\tsourceAgentModel?: string,\n\tworkflows?: WorkflowDescriptor[],\n): Promise<T_RESPONSE> => {\n\tconst startTime = Date.now();\n\tconst providers = llmProviders || ['anthropic'];\n\tconst provider = providers[0]; // Use first provider\n\tconst llmInstance = getLLMInstance(provider);\n\n\t// Clear prompt log file for new request and log the user prompt\n\tlogger.clearFile();\n\tlogger.logLLMPrompt('agentUserResponse', 'user', `User Prompt: ${prompt}`);\n\n\tlogger.info(`[AgentFlow] Starting | provider: ${provider} | prompt: \"${prompt.substring(0, 50)}...\"`);\n\n\ttry {\n\t\t// ============================================\n\t\t// STEP 1: Exact prompt match (cached replay)\n\t\t// ============================================\n\t\t// Indexed DB lookup on the stored user_prompt — no embeddings, no rerank.\n\t\t// Only replays when the prompt is byte-for-byte identical to a previous one.\n\t\t// Similar-but-not-identical questions deliberately fall through to the agent.\n\n\t\tconst conversationMatch = await ConversationSearch.findExactMatch({\n\t\t\tuserPrompt: prompt,\n\t\t\tcollections,\n\t\t\tuserId,\n\t\t});\n\n\t\tif (conversationMatch) {\n\t\t\tconst rawComponent = conversationMatch.uiBlock?.component || conversationMatch.uiBlock?.generatedComponentMetadata;\n\t\t\tconst isValidComponent = rawComponent && typeof rawComponent === 'object' && Object.keys(rawComponent).length > 0;\n\t\t\tconst component = isValidComponent ? rawComponent : null;\n\t\t\tconst cachedTextResponse = conversationMatch.uiBlock?.analysis || conversationMatch.uiBlock?.textResponse || conversationMatch.uiBlock?.text || '';\n\n\t\t\t// Script-flow safety: post-JS computed data lives only in the in-memory\n\t\t\t// queryCache, and rehydrateCachedComponent only knows how to re-register\n\t\t\t// SQL queries (from `_queryMap`) — it can't reconstruct script_dataset\n\t\t\t// payloads. If any cached component references a script_dataset queryId\n\t\t\t// that's no longer cached, the UIBlock would render the \"Script dataset\n\t\t\t// expired\" sentinel error. Fall through to the agent flow instead — the\n\t\t\t// script matcher will replay the saved recipe and produce fresh data.\n\t\t\tif (component && hasExpiredScriptDataset(component)) {\n\t\t\t\tlogger.info(`[AgentFlow] Exact match found but cached UIBlock has expired script_dataset — falling through to script matcher`);\n\t\t\t} else {\n\t\t\t\tif (streamCallback && cachedTextResponse) {\n\t\t\t\t\tstreamCallback(cachedTextResponse);\n\t\t\t\t}\n\n\t\t\t\tconst elapsedTime = Date.now() - startTime;\n\t\t\t\tlogger.info(`[AgentFlow] Exact match — returning cached result (${elapsedTime}ms)`);\n\n\t\t\t\t// Re-hydrate cached component: re-register expired queryIds in the query cache\n\t\t\t\tconst rehydratedComponent = component ? rehydrateCachedComponent(component) : null;\n\n\t\t\t\treturn {\n\t\t\t\t\tsuccess: true,\n\t\t\t\t\tdata: {\n\t\t\t\t\t\ttext: cachedTextResponse,\n\t\t\t\t\t\tcomponent: rehydratedComponent,\n\t\t\t\t\t\tactions: conversationMatch.uiBlock?.actions || [],\n\t\t\t\t\t\treasoning: `Exact match from previous conversation`,\n\t\t\t\t\t\tmethod: `${provider}-agent-exact-match`,\n\t\t\t\t\t\tsemanticSimilarity: 1,\n\t\t\t\t\t},\n\t\t\t\t\terrors: []\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\n\t\tlogger.info(`[AgentFlow] No exact match — proceeding with agent`);\n\n\t\t// ============================================\n\t\t// STEP 2: MainAgent — route, query, analyze\n\t\t// ============================================\n\n\t\t// Resolve API key for the provider\n\t\tconst apiKey = (() => {\n\t\t\tswitch (provider) {\n\t\t\t\tcase 'anthropic': return anthropicApiKey;\n\t\t\t\tcase 'groq': return groqApiKey;\n\t\t\t\tcase 'gemini': return geminiApiKey;\n\t\t\t\tcase 'openai': return openaiApiKey;\n\t\t\t\tdefault: return anthropicApiKey;\n\t\t\t}\n\t\t})();\n\n\t\t// Convert externalTools to ExternalTool format\n\t\tconst agentTools: ExternalTool[] = (externalTools || []).map(t => ({\n\t\t\tid: t.id,\n\t\t\tname: t.name,\n\t\t\tdescription: t.description,\n\t\t\ttoolType: t.toolType,\n\t\t\tfullSchema: t.fullSchema,\n\t\t\tschemaTier: t.schemaTier,\n\t\t\tschemaSearchFn: t.schemaSearchFn,\n\t\t\tfn: t.fn,\n\t\t\tlimit: t.limit,\n\t\t\toutputSchema: t.outputSchema,\n\t\t\texecutionType: t.executionType,\n\t\t\tuserProvidedData: t.userProvidedData,\n\t\t\tparams: t.params,\n\t\t})) as any[];\n\n\t\t// Fetch knowledge base context (global + user + query-matched)\n\t\tlet globalKnowledgeBase = '';\n\t\tlet knowledgeBaseContext = '';\n\t\tif (collections) {\n\t\t\tconst kbResult = await KB.getAllKnowledgeBase({\n\t\t\t\tprompt,\n\t\t\t\tcollections,\n\t\t\t\tuserId,\n\t\t\t\ttopK: KNOWLEDGE_BASE_TOP_K,\n\t\t\t});\n\t\t\tglobalKnowledgeBase = kbResult.globalContext || '';\n\t\t\t// Combine user-specific + query-matched as dynamic context\n\t\t\tconst dynamicParts: string[] = [];\n\t\t\tif (kbResult.userContext) {\n\t\t\t\tdynamicParts.push('## User-Specific Knowledge Base\\n' + kbResult.userContext);\n\t\t\t}\n\t\t\tif (kbResult.queryContext) {\n\t\t\t\tdynamicParts.push('## Relevant Knowledge Base (Query-Matched)\\n' + kbResult.queryContext);\n\t\t\t}\n\t\t\tknowledgeBaseContext = dynamicParts.join('\\n\\n') || '';\n\t\t}\n\n\t\t// Derive projectId from tool IDs — every source tool carries a project-scoped ID.\n\t\t// Used to scope embedding searches to this project.\n\t\tconst projectId = (() => {\n\t\t\tconst firstSrcTool = agentTools.find(t => (t as any).toolType !== 'direct');\n\t\t\tconst m = firstSrcTool?.id?.match(/^(\\w+)-([a-f0-9]+)_/);\n\t\t\treturn m ? m[2] : undefined;\n\t\t})();\n\n\t\t// Build agent config\n\t\tconst agentConfig: AgentConfig = {\n\t\t\t...DEFAULT_AGENT_CONFIG,\n\t\t\tapiKey: apiKey || undefined,\n\t\t\tmainAgentModel: mainAgentModel || DEFAULT_AGENT_CONFIG.mainAgentModel,\n\t\t\tsourceAgentModel: sourceAgentModel || DEFAULT_AGENT_CONFIG.sourceAgentModel,\n\t\t\tglobalKnowledgeBase,\n\t\t\tknowledgeBaseContext,\n\t\t\tcollections,\n\t\t\tprojectId,\n\t\t};\n\n\t\t// Create stream buffer\n\t\tconst streamBuffer = new StreamBuffer(streamCallback);\n\n\t\t// ============================================\n\t\t// STEP 1.5: Script Matching (reuse existing script)\n\t\t// ============================================\n\n\t\tconst scriptStore = new ScriptStore({ collections, projectId });\n\n\t\t// Per-turn unique suffix used for draft filenames. Keeps concurrent turns\n\t\t// (multiple users on the same project) from clobbering each other's draft\n\t\t// files in scripts-store/. Drops off the filename when the draft is\n\t\t// promoted to verified — see ScriptStore.promoteToVerified().\n\t\tconst turnId = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;\n\n\t\t// Fork-and-adapt context — set when the matcher returns tier='near' AND\n\t\t// the parent's fork-depth is below the cap. Carried through MainAgent and\n\t\t// applied at save time so the resulting recipe records its parentage.\n\t\t// See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md.\n\t\tconst FORK_DEPTH_CAP = 3;\n\t\tlet forkContext: {\n\t\t\tparentRecipeId: string;\n\t\t\tparentName: string;\n\t\t\tparentDepth: number;\n\t\t\tparentBody: string;\n\t\t\tgaps: string[];\n\t\t\tmodificationHint?: string;\n\t\t} | undefined;\n\n\t\tif ((await scriptStore.count()) > 0) {\n\t\t\tconst scriptMatcher = new ScriptMatcher(scriptStore);\n\t\t\tconst scriptMatch = await scriptMatcher.match(prompt, apiKey, sourceAgentModel);\n\n\t\t\t// tier='near' diverts BEFORE the runScript replay path. Set up fork\n\t\t\t// context, then fall through to the normal MainAgent flow with a\n\t\t\t// modified prompt.\n\t\t\tif (scriptMatch && scriptMatch.tier === 'near') {\n\t\t\t\tconst parentDepth = scriptMatch.recipe.forkDepth ?? 0;\n\t\t\t\tif (parentDepth >= FORK_DEPTH_CAP) {\n\t\t\t\t\tlogger.info(\n\t\t\t\t\t\t`[AgentFlow] Fork-mode skipped: parent \"${scriptMatch.recipe.name}\" is at fork depth ${parentDepth} ` +\n\t\t\t\t\t\t`(cap=${FORK_DEPTH_CAP}). Falling through to fresh authoring.`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tforkContext = {\n\t\t\t\t\t\tparentRecipeId: scriptMatch.recipe.id,\n\t\t\t\t\t\tparentName: scriptMatch.recipe.name,\n\t\t\t\t\t\tparentDepth,\n\t\t\t\t\t\tparentBody: scriptMatch.recipe.scriptBody,\n\t\t\t\t\t\tgaps: scriptMatch.gaps || [],\n\t\t\t\t\t\tmodificationHint: scriptMatch.modificationHint,\n\t\t\t\t\t};\n\t\t\t\t\tlogger.info(\n\t\t\t\t\t\t`[AgentFlow] Fork mode: parent \"${scriptMatch.recipe.name}\" (depth=${parentDepth}) — ` +\n\t\t\t\t\t\t`MainAgent will adapt the body via write_script/execute_script.`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (scriptMatch && scriptMatch.tier === 'high') {\n\t\t\t\tlogger.info(`[AgentFlow] Script matched: \"${scriptMatch.recipe.name}\" (tier: high)`);\n\n\t\t\t\ttry {\n\t\t\t\t\t// Run the script in an isolated tsx subprocess with the LLM-extracted parameters.\n\t\t\t\t\t// The runner handles IPC, authorizes each ctx.query, and streams per-query UI progress.\n\t\t\t\t\tconst scriptPath = scriptStore.getScriptPath(scriptMatch.recipe);\n\t\t\t\t\tconst scriptResult = await runScript(\n\t\t\t\t\t\tscriptMatch.recipe,\n\t\t\t\t\t\tscriptPath,\n\t\t\t\t\t\tscriptMatch.extractedParams || {},\n\t\t\t\t\t\t{ externalTools: agentTools, streamBuffer },\n\t\t\t\t\t);\n\n\t\t\t\t\tif (scriptResult.success && scriptResult.data.length > 0) {\n\t\t\t\t\t\t// Script succeeded — run analysis LLM call\n\t\t\t\t\t\tawait scriptStore.recordSuccess(scriptMatch.recipe.id);\n\n\t\t\t\t\t\t// Build data context for analysis prompt\n\t\t\t\t\t\tconst dataContext = scriptResult.executedQueries.map(q => {\n\t\t\t\t\t\t\tconst preview = q.data.slice(0, 15);\n\t\t\t\t\t\t\treturn `## Data from \"${q.sourceName}\"\\nRows: ${q.count}${q.totalCount && q.totalCount > q.count ? ` (${q.totalCount} total)` : ''}\\nExecution time: ${q.executionTimeMs}ms\\n\\n\\`\\`\\`json\\n${JSON.stringify(preview, null, 2)}\\n\\`\\`\\``;\n\t\t\t\t\t\t}).join('\\n\\n');\n\n\t\t\t\t\t\t// Run analysis — 1 LLM call (prompts loaded from file → falls back to prompts.ts)\n\t\t\t\t\t\tconst analysisPrompts = await promptLoader.loadPrompts('script-analysis', {\n\t\t\t\t\t\t\tUSER_PROMPT: prompt,\n\t\t\t\t\t\t\tDATA_CONTEXT: dataContext,\n\t\t\t\t\t\t\tCURRENT_DATETIME: getCurrentDateTimeForPrompt(),\n\t\t\t\t\t\t\tGLOBAL_KNOWLEDGE_BASE: globalKnowledgeBase || 'No global knowledge base available.',\n\t\t\t\t\t\t\tKNOWLEDGE_BASE_CONTEXT: knowledgeBaseContext || 'No additional knowledge base context available.',\n\t\t\t\t\t\t\tCONVERSATION_HISTORY: conversationHistory || 'No previous conversation',\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tconst analysisText = await LLM.stream(\n\t\t\t\t\t\t\t{ sys: analysisPrompts.system, user: analysisPrompts.user },\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tmodel: mainAgentModel || undefined,\n\t\t\t\t\t\t\t\tmaxTokens: 3000,\n\t\t\t\t\t\t\t\ttemperature: 0,\n\t\t\t\t\t\t\t\tapiKey,\n\t\t\t\t\t\t\t\tpartial: streamBuffer.hasCallback()\n\t\t\t\t\t\t\t\t\t? (chunk: string) => streamBuffer.write(chunk)\n\t\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tconst scriptTextResponse = (streamBuffer.getFullText() || analysisText || '')\n\t\t\t\t\t\t\t.replace(/_?_?SB_END_?_?/g, '')\n\t\t\t\t\t\t\t.replace(/__SB_\\w+_(?:START|MSG)_?_?/g, '')\n\t\t\t\t\t\t\t.replace(/__QUERY_TIMER_START_[^_]*__/g, '')\n\t\t\t\t\t\t\t.replace(/__QUERY_TIMER_DONE_[\\d.]+__/g, '');\n\n\t\t\t\t\t\tstreamBuffer.flush();\n\n\t\t\t\t\t\t// Build executedTools for component generation (from script query results)\n\t\t\t\t\t\tconst scriptExecutedTools: ExecutedToolInfo[] = scriptResult.executedQueries.map(q => ({\n\t\t\t\t\t\t\tid: q.sourceId,\n\t\t\t\t\t\t\tname: q.sourceName,\n\t\t\t\t\t\t\tparams: { sql: q.sql },\n\t\t\t\t\t\t\tresult: {\n\t\t\t\t\t\t\t\t_totalRecords: q.totalCount || q.count,\n\t\t\t\t\t\t\t\t_recordsShown: q.data.length,\n\t\t\t\t\t\t\t\t_sampleData: q.data.slice(0, TOOL_TRACKING_SAMPLE_ROWS),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t}));\n\n\t\t\t\t\t\t// Component generation — lightweight script-components prompt\n\t\t\t\t\t\t// Reuses the script's SQL via queryId, LLM just picks component types + maps columns\n\t\t\t\t\t\tlet matchedComponents: Component[] = [];\n\t\t\t\t\t\tlet layoutTitle = 'Dashboard';\n\t\t\t\t\t\tlet layoutDescription = 'Multi-component dashboard';\n\t\t\t\t\t\tlet actions: Action[] = [];\n\n\t\t\t\t\t\tif (components && components.length > 0 && scriptExecutedTools.length > 0) {\n\t\t\t\t\t\t\tlogger.info(`[AgentFlow] Script flow — generating components (lightweight)`);\n\n\t\t\t\t\t\t\tif (streamBuffer.hasCallback()) {\n\t\t\t\t\t\t\t\tstreamBuffer.write('\\n\\n📊 **Generating visualization components...**\\n\\n');\n\t\t\t\t\t\t\t\tstreamBuffer.write('__TEXT_COMPLETE__COMPONENT_GENERATION_START__');\n\t\t\t\t\t\t\t\tstreamBuffer.flush();\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// Replay path: do NOT stream __ANSWER_COMPONENT__ markers.\n\t\t\t\t\t\t\t// Streaming them makes the frontend treat this as an\n\t\t\t\t\t\t\t// answer-component response (single component up front, the rest\n\t\t\t\t\t\t\t// hidden behind a \"View more details\" preload/expand that never\n\t\t\t\t\t\t\t// completes on replay). Leaving the callback undefined keeps\n\t\t\t\t\t\t\t// message.isAnswerComponent false on the client, so the final\n\t\t\t\t\t\t\t// full-dashboard component renders inline with ALL components —\n\t\t\t\t\t\t\t// identical to the within-TTL exact-match cache hit. Authoring\n\t\t\t\t\t\t\t// (first ask) still streams the early answer for perceived latency.\n\t\t\t\t\t\t\tconst componentStreamCallback: ((component: Component) => void) | undefined = undefined;\n\n\t\t\t\t\t\t\t// Register script SQL in query cache for component reuse.\n\t\t\t\t\t\t\t// Virtual (computed) datasets get a regeneration descriptor so they\n\t\t\t\t\t\t\t// can be re-derived on cache expiry instead of showing \"expired\" (#6).\n\t\t\t\t\t\t\tconst scriptQueryIds: Record<string, string> = {};\n\t\t\t\t\t\t\tfor (const q of scriptResult.executedQueries) {\n\t\t\t\t\t\t\t\tscriptQueryIds[q.sourceId] = storeScriptDatasetQuery(q, {\n\t\t\t\t\t\t\t\t\trecipeId: scriptMatch.recipe.id,\n\t\t\t\t\t\t\t\t\tparams: scriptMatch.extractedParams || {},\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// Component recipe fast path — rebind stored specs to fresh\n\t\t\t\t\t\t\t// queryIds deterministically (no component LLM call). Falls back\n\t\t\t\t\t\t\t// to LLM generation if the recipe has no specs or they drifted.\n\t\t\t\t\t\t\tlet usedRecipeComponents = false;\n\t\t\t\t\t\t\tif (scriptMatch.recipe.components && scriptMatch.recipe.components.length > 0) {\n\t\t\t\t\t\t\t\tconst assembled = assembleComponentsFromSpecs({\n\t\t\t\t\t\t\t\t\tspecs: scriptMatch.recipe.components,\n\t\t\t\t\t\t\t\t\tscriptResult,\n\t\t\t\t\t\t\t\t\tqueryIds: scriptQueryIds,\n\t\t\t\t\t\t\t\t\tavailableComponents: components,\n\t\t\t\t\t\t\t\t\tcomponentStreamCallback,\n\t\t\t\t\t\t\t\t\tanalysisText: scriptTextResponse,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tif (assembled && assembled.length > 0) {\n\t\t\t\t\t\t\t\t\tmatchedComponents = assembled;\n\t\t\t\t\t\t\t\t\tlayoutTitle = scriptMatch.recipe.name || layoutTitle;\n\t\t\t\t\t\t\t\t\tusedRecipeComponents = true;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif (!usedRecipeComponents) try {\n\t\t\t\t\t\t\t\t// Try lightweight script-components prompt first\n\t\t\t\t\t\t\t\tconst compResult = await generateScriptComponents({\n\t\t\t\t\t\t\t\t\tuserPrompt: prompt,\n\t\t\t\t\t\t\t\t\tscriptResult,\n\t\t\t\t\t\t\t\t\tqueryIds: scriptQueryIds,\n\t\t\t\t\t\t\t\t\tavailableComponents: components,\n\t\t\t\t\t\t\t\t\tapiKey,\n\t\t\t\t\t\t\t\t\tmodel: sourceAgentModel,\n\t\t\t\t\t\t\t\t\tcomponentStreamCallback,\n\t\t\t\t\t\t\t\t\texternalTools: agentTools,\n\t\t\t\t\t\t\t\t\tcollections,\n\t\t\t\t\t\t\t\t\tanalysisText: scriptTextResponse,\n\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\tif (compResult.components.length > 0) {\n\t\t\t\t\t\t\t\t\tmatchedComponents = compResult.components;\n\t\t\t\t\t\t\t\t\tlayoutTitle = compResult.layoutTitle;\n\t\t\t\t\t\t\t\t\tlayoutDescription = compResult.layoutDescription;\n\t\t\t\t\t\t\t\t\tactions = compResult.actions;\n\t\t\t\t\t\t\t\t\t// Backfill the component recipe so the next replay is deterministic.\n\t\t\t\t\t\t\t\t\tif (compResult.componentSpecs.length > 0) {\n\t\t\t\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\t\t\t\tscriptMatch.recipe.components = compResult.componentSpecs;\n\t\t\t\t\t\t\t\t\t\t\tawait scriptStore.save(scriptMatch.recipe);\n\t\t\t\t\t\t\t\t\t\t} catch (e) {\n\t\t\t\t\t\t\t\t\t\t\tlogger.warn(`[AgentFlow] Failed to backfill component recipe: ${e}`);\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t// Lightweight generator returned nothing — fall back to full generator\n\t\t\t\t\t\t\t\t\tlogger.info(`[AgentFlow] Lightweight component gen returned no components — falling back to full generator`);\n\t\t\t\t\t\t\t\t\tconst matchResult = await generateAgentComponents({\n\t\t\t\t\t\t\t\t\t\tanalysisContent: analysisText || scriptTextResponse,\n\t\t\t\t\t\t\t\t\t\tcomponents,\n\t\t\t\t\t\t\t\t\t\tuserPrompt: prompt,\n\t\t\t\t\t\t\t\t\t\texecutedTools: scriptExecutedTools,\n\t\t\t\t\t\t\t\t\t\tcollections,\n\t\t\t\t\t\t\t\t\t\tapiKey,\n\t\t\t\t\t\t\t\t\t\tcomponentStreamCallback,\n\t\t\t\t\t\t\t\t\t\tuserId,\n\t\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t\tmatchedComponents = matchResult.components;\n\t\t\t\t\t\t\t\t\tlayoutTitle = matchResult.layoutTitle;\n\t\t\t\t\t\t\t\t\tlayoutDescription = matchResult.layoutDescription;\n\t\t\t\t\t\t\t\t\tactions = matchResult.actions;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t} catch (compErr) {\n\t\t\t\t\t\t\t\tlogger.warn(`[AgentFlow] Script component generation failed, trying full generator: ${compErr}`);\n\t\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\t\tconst matchResult = await generateAgentComponents({\n\t\t\t\t\t\t\t\t\t\tanalysisContent: analysisText || scriptTextResponse,\n\t\t\t\t\t\t\t\t\t\tcomponents,\n\t\t\t\t\t\t\t\t\t\tuserPrompt: prompt,\n\t\t\t\t\t\t\t\t\t\texecutedTools: scriptExecutedTools,\n\t\t\t\t\t\t\t\t\t\tcollections,\n\t\t\t\t\t\t\t\t\t\tapiKey,\n\t\t\t\t\t\t\t\t\t\tcomponentStreamCallback,\n\t\t\t\t\t\t\t\t\t\tuserId,\n\t\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t\tmatchedComponents = matchResult.components;\n\t\t\t\t\t\t\t\t\tlayoutTitle = matchResult.layoutTitle;\n\t\t\t\t\t\t\t\t\tlayoutDescription = matchResult.layoutDescription;\n\t\t\t\t\t\t\t\t\tactions = matchResult.actions;\n\t\t\t\t\t\t\t\t} catch (fallbackErr) {\n\t\t\t\t\t\t\t\t\tlogger.warn(`[AgentFlow] Full component generation also failed (non-blocking): ${fallbackErr}`);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Strip SQL from components — same logic as agent flow\n\t\t\t\t\t\tconst queryMap: Record<string, any> = {};\n\t\t\t\t\t\tconst securedComponents = matchedComponents.map(comp => {\n\t\t\t\t\t\t\tconst props: any = { ...comp.props };\n\t\t\t\t\t\t\tconst sqlValue = props.externalTool?.parameters?.sql || props.externalTool?.parameters?.query;\n\t\t\t\t\t\t\tif (sqlValue) {\n\t\t\t\t\t\t\t\tconst { sql, query, ...restParams } = props.externalTool.parameters;\n\t\t\t\t\t\t\t\tconst toolId = props.externalTool?.toolId || '';\n\t\t\t\t\t\t\t\tconst cacheKey = toolId ? `${toolId}:${sqlValue}` : sqlValue;\n\t\t\t\t\t\t\t\tconst cachedData = queryCache.get(cacheKey);\n\t\t\t\t\t\t\t\tconst queryId = queryCache.storeQuery(sqlValue, cachedData);\n\t\t\t\t\t\t\t\tqueryMap[queryId] = sqlValue;\n\t\t\t\t\t\t\t\tprops.externalTool = { ...props.externalTool, parameters: { queryId, ...restParams } };\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (props.query) {\n\t\t\t\t\t\t\t\tconst { query, ...restProps } = props;\n\t\t\t\t\t\t\t\tconst queryId = queryCache.storeQuery(query);\n\t\t\t\t\t\t\t\tqueryMap[queryId] = query;\n\t\t\t\t\t\t\t\treturn { ...comp, props: { ...restProps, queryId } };\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn { ...comp, props };\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tlet containerComponent: Component | null = null;\n\t\t\t\t\t\tif (securedComponents.length > 0) {\n\t\t\t\t\t\t\tcontainerComponent = {\n\t\t\t\t\t\t\t\tid: `container_${Date.now()}`,\n\t\t\t\t\t\t\t\tname: 'MultiComponentContainer',\n\t\t\t\t\t\t\t\ttype: 'Container',\n\t\t\t\t\t\t\t\tdescription: layoutDescription,\n\t\t\t\t\t\t\t\tprops: {\n\t\t\t\t\t\t\t\t\tconfig: {\n\t\t\t\t\t\t\t\t\t\ttitle: layoutTitle,\n\t\t\t\t\t\t\t\t\t\tdescription: layoutDescription,\n\t\t\t\t\t\t\t\t\t\tcomponents: securedComponents,\n\t\t\t\t\t\t\t\t\t\t...(Object.keys(queryMap).length > 0 && { _queryMap: queryMap }),\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\tactions,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst elapsedTime = Date.now() - startTime;\n\t\t\t\t\t\tlogger.info(`[AgentFlow] Script flow complete | ${scriptResult.executedQueries.length} queries | ${matchedComponents.length} components | ${elapsedTime}ms`);\n\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tsuccess: true,\n\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\ttext: scriptTextResponse,\n\t\t\t\t\t\t\t\tcomponent: containerComponent,\n\t\t\t\t\t\t\t\tactions,\n\t\t\t\t\t\t\t\tmethod: `${provider}-script-response`,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\terrors: [],\n\t\t\t\t\t\t};\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// Script failed or returned no data — fall through to agent flow\n\t\t\t\t\t\tlogger.warn(`[AgentFlow] Script \"${scriptMatch.recipe.name}\" failed: ${scriptResult.error || 'no data returned'}`);\n\t\t\t\t\t\tawait scriptStore.recordFailure(scriptMatch.recipe.id);\n\t\t\t\t\t}\n\t\t\t\t} catch (scriptErr) {\n\t\t\t\t\tconst errMsg = scriptErr instanceof Error ? scriptErr.message : String(scriptErr);\n\t\t\t\t\tlogger.warn(`[AgentFlow] Script execution error (falling back to agent flow): ${errMsg}`);\n\t\t\t\t\tawait scriptStore.recordFailure(scriptMatch.recipe.id);\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tlogger.info(`[AgentFlow] No scripts in store — proceeding with agent flow`);\n\t\t}\n\n\t\t// If we're in fork mode, prepend a parent-script preamble to the user\n\t\t// prompt before MainAgent runs. The agent-main system prompt has the\n\t\t// \"Fork-and-Adapt Mode\" section that explains how to use this block.\n\t\tlet effectivePrompt = prompt;\n\t\tif (forkContext) {\n\t\t\tconst gapsBlock = forkContext.gaps.length\n\t\t\t\t? forkContext.gaps.map(g => `- ${g}`).join('\\n')\n\t\t\t\t: '- (matcher did not list specific gaps; infer from the user question and parent body)';\n\t\t\tconst hintBlock = forkContext.modificationHint || '(no specific hint — choose the smallest change that satisfies the user question)';\n\n\t\t\teffectivePrompt =\n\t\t\t\t`## Parent Script to Adapt (FORK MODE)\\n\\n` +\n\t\t\t\t`The script library has a near-matching parent for this question. ` +\n\t\t\t\t`Adapt its body via write_script + execute_script — do NOT dispatch source tools unless absolutely necessary. ` +\n\t\t\t\t`The parent's ctx.query SQL is already proven. Save your modified script under a new descriptive name; the runtime will record the parentage automatically.\\n\\n` +\n\t\t\t\t`### Parent name\\n${forkContext.parentName}\\n\\n` +\n\t\t\t\t`### Parent body\\n\\`\\`\\`ts\\n${forkContext.parentBody}\\n\\`\\`\\`\\n\\n` +\n\t\t\t\t`### What needs to change\\n${gapsBlock}\\n\\n` +\n\t\t\t\t`Hint: ${hintBlock}\\n\\n` +\n\t\t\t\t`---\\n\\n` +\n\t\t\t\t`User question: ${prompt}`;\n\t\t}\n\n\t\t// ============================================\n\t\t// STEP 2: MainAgent — route, query, analyze\n\t\t// ============================================\n\n\t\t// Source routing pre-filter via embedding search.\n\t\t// Only applies when there are 5+ SQL sources to filter — otherwise not worth the risk.\n\t\t// Direct tools (toolType='direct') are always kept. Fallback to all tools on error.\n\t\tlet filteredTools = agentTools;\n\t\tconst sqlToolCount = agentTools.filter(t => (t as any).toolType !== 'direct').length;\n\t\tconst routingSearch = collections?.['source-embeddings']?.['search'];\n\t\tif (sqlToolCount >= 5 && routingSearch) {\n\t\t\ttry {\n\t\t\t\tconst maxSources = Math.min(5, Math.ceil(sqlToolCount * 0.5));\n\t\t\t\tconst res = await routingSearch({\n\t\t\t\t\tquery: prompt,\n\t\t\t\t\tprojectId,\n\t\t\t\t\ttopK: 10,\n\t\t\t\t});\n\t\t\t\tconst matches = (res?.results || []) as Array<{ sourceId: string; similarity: number; toolType?: string }>;\n\t\t\t\t// Similarity = 1/(1+L2distance) — 0.35 is loose; top 3 always kept as safety\n\t\t\t\tconst passing = matches.filter(m => m.similarity >= 0.35);\n\t\t\t\t// Keep at least 3 — user might be asking cross-source\n\t\t\t\tconst keep = passing.length >= 3 ? passing.slice(0, maxSources) : matches.slice(0, 3);\n\t\t\t\tconst keepIds = new Set(keep.map(m => m.sourceId));\n\n\t\t\t\tif (keepIds.size > 0) {\n\t\t\t\t\tfilteredTools = agentTools.filter(t =>\n\t\t\t\t\t\t(t as any).toolType === 'direct' || keepIds.has(t.id),\n\t\t\t\t\t);\n\t\t\t\t\tlogger.info(`[AgentFlow] Source routing pre-filter: ${agentTools.length} → ${filteredTools.length} tools`);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconst msg = err instanceof Error ? err.message : String(err);\n\t\t\t\tlogger.warn(`[AgentFlow] Source routing pre-filter failed (using all tools): ${msg}`);\n\t\t\t}\n\t\t}\n\n\t\t// Create and run main agent. In fork mode, MainAgent receives the\n\t\t// effectivePrompt (parent body + gaps + hint + user question) instead\n\t\t// of the bare user prompt. The system prompt's \"Fork-and-Adapt Mode\"\n\t\t// section tells the LLM how to use the parent block.\n\t\tconst mainAgent = new MainAgent(filteredTools, agentConfig, scriptStore, turnId, streamBuffer, workflows || []);\n\t\tconst agentResponse = await mainAgent.handleQuestion(\n\t\t\teffectivePrompt,\n\t\t\tapiKey,\n\t\t\tconversationHistory,\n\t\t\tstreamBuffer.hasCallback() ? (chunk: string) => streamBuffer.write(chunk) : undefined\n\t\t);\n\n\t\t// ============================================\n\t\t// STEP 2.5: Workflow short-circuit\n\t\t// ============================================\n\t\t// If the main agent picked a workflow, return that workflow component\n\t\t// directly — skip analysis text, skip component generation, skip the\n\t\t// federation/SQL rewrite below. The workflow component renders its own\n\t\t// UI and fetches its own data via its embedded externalTool config.\n\t\tif (agentResponse.workflow) {\n\t\t\t// Flush any streamed pre-amble (e.g., the \"🧭 Launching ...\" banner)\n\t\t\tstreamBuffer.flush();\n\n\t\t\tconst workflowComponent: Component = {\n\t\t\t\tid: `workflow_${Date.now()}`,\n\t\t\t\tname: agentResponse.workflow.name,\n\t\t\t\ttype: `Workflow_${agentResponse.workflow.name}`,\n\t\t\t\tdescription: `Workflow: ${agentResponse.workflow.name}`,\n\t\t\t\tprops: agentResponse.workflow.props,\n\t\t\t};\n\n\t\t\tconst elapsedTime = Date.now() - startTime;\n\t\t\tlogger.info(`[AgentFlow] Workflow short-circuit | \"${agentResponse.workflow.name}\" | ${elapsedTime}ms`);\n\n\t\t\treturn {\n\t\t\t\tsuccess: true,\n\t\t\t\tdata: {\n\t\t\t\t\ttext: '',\n\t\t\t\t\tcomponent: workflowComponent,\n\t\t\t\t\tactions: [],\n\t\t\t\t\tmethod: `${provider}-agent-workflow`,\n\t\t\t\t},\n\t\t\t\terrors: [],\n\t\t\t};\n\t\t}\n\n\t\tconst rawText = streamBuffer.getFullText() || agentResponse.text || 'I apologize, but I was unable to generate a response.';\n\t\t// Strip streaming markers — these are for frontend rendering only,\n\t\t// not for component generation or markdown content.\n\t\t// Handle both exact (__SB_END__) and potentially truncated (SB_END__) marker formats.\n\t\tconst textResponse = rawText\n\t\t\t.replace(/_?_?SB_END_?_?/g, '')\n\t\t\t.replace(/__SB_\\w+_(?:START|MSG)_?_?/g, '')\n\t\t\t.replace(/__QUERY_TIMER_START_[^_]*__/g, '')\n\t\t\t.replace(/__QUERY_TIMER_DONE_[\\d.]+__/g, '')\n\t\t\t.replace(/__TEXT_COMPLETE__COMPONENT_GENERATION_START__/g, '')\n\t\t\t.replace(/\\[COMPLEXITY:\\s*(?:simple|medium|complex)\\]/gi, '');\n\n\t\t// Flush remaining buffered text\n\t\tstreamBuffer.flush();\n\n\t\t// ============================================\n\t\t// STEP 3: Component Generation (if data was queried)\n\t\t// ============================================\n\n\t\t// Fork-mode adapter — when MainAgent ran in fork mode, it did NOT dispatch\n\t\t// any source tool (the parent's SQL ran inside the script subprocess instead).\n\t\t// `executedTools` is therefore empty, but `savedScript.executedQueries` carries\n\t\t// the rows we want to render. Surface those queries as executedTools +\n\t\t// sourceResults so the existing component-generation path produces charts\n\t\t// instead of falling into the \"general question\" branch.\n\t\t// See backend/docs/SCRIPT-FLOW-FORK-ADAPT.md.\n\t\tif (\n\t\t\tagentResponse.executedTools.length === 0 &&\n\t\t\tagentResponse.savedScript?.executedQueries &&\n\t\t\tagentResponse.savedScript.executedQueries.length > 0\n\t\t) {\n\t\t\tfor (const q of agentResponse.savedScript.executedQueries) {\n\t\t\t\tagentResponse.executedTools.push({\n\t\t\t\t\tid: q.sourceId,\n\t\t\t\t\tname: q.sourceName,\n\t\t\t\t\tparams: { sql: q.sql },\n\t\t\t\t\tresult: {\n\t\t\t\t\t\t_totalRecords: q.totalCount || q.count,\n\t\t\t\t\t\t_recordsShown: q.data.length,\n\t\t\t\t\t\t_sampleData: q.data.slice(0, 3),\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t\tagentResponse.sourceResults.push({\n\t\t\t\t\tsourceId: q.sourceId,\n\t\t\t\t\tsourceName: q.sourceName,\n\t\t\t\t\tsuccess: true,\n\t\t\t\t\tdata: q.data,\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\ttotalRowsMatched: q.totalCount || q.count,\n\t\t\t\t\t\trowsReturned: q.count,\n\t\t\t\t\t\tisLimited: !!(q.totalCount && q.totalCount > q.count),\n\t\t\t\t\t\tqueryExecuted: q.sql,\n\t\t\t\t\t\texecutionTimeMs: q.executionTimeMs,\n\t\t\t\t\t},\n\t\t\t\t\texecutedTool: agentResponse.executedTools[agentResponse.executedTools.length - 1],\n\t\t\t\t});\n\t\t\t}\n\t\t\tlogger.info(\n\t\t\t\t`[AgentFlow] Fork-mode adapter: surfaced ${agentResponse.savedScript.executedQueries.length} ` +\n\t\t\t\t`script query result(s) as executedTools for component generation`,\n\t\t\t);\n\t\t}\n\n\t\tconst hasExecutedTools = agentResponse.executedTools.length > 0;\n\t\tlet matchedComponents: Component[] = [];\n\t\tlet layoutTitle = 'Dashboard';\n\t\tlet layoutDescription = 'Multi-component dashboard';\n\t\tlet actions: Action[] = [];\n\t\t// Validated component specs from the lightweight generator — persisted on\n\t\t// the recipe at promote time so future replays are deterministic (Phase 3).\n\t\tlet authoredComponentSpecs: ScriptComponentSpec[] = [];\n\n\t\tif (!hasExecutedTools) {\n\t\t\t// General question or all queries failed — wrap text in DynamicMarkdownBlock\n\t\t\t// Use agentResponse.text (main agent's final LLM output only), NOT the full\n\t\t\t// streamed text which includes source agent internals (schema searches, SQL, errors)\n\t\t\tlogger.info(`[AgentFlow] No tools executed — general question, wrapping in DynamicMarkdownBlock`);\n\n\t\t\tconst mainAgentText = agentResponse.text || textResponse;\n\n\t\t\tconst nextQuestions = await llmInstance.generateNextQuestions(\n\t\t\t\tprompt,\n\t\t\t\tnull,\n\t\t\t\tundefined,\n\t\t\t\tapiKey,\n\t\t\t\tconversationHistory,\n\t\t\t\tmainAgentText\n\t\t\t);\n\t\t\tactions = convertQuestionsToActions(nextQuestions);\n\n\t\t\tconst markdownContent = mainAgentText\n\t\t\t\t.replace(/<DashboardComponents>[\\s\\S]*?<\\/DashboardComponents>/g, '')\n\t\t\t\t.trim();\n\n\t\t\tmatchedComponents = [{\n\t\t\t\tid: 'dynamic-markdown-block',\n\t\t\t\tname: 'DynamicMarkdownBlock',\n\t\t\t\ttype: 'MarkdownBlock',\n\t\t\t\tdescription: 'Text response rendered as markdown',\n\t\t\t\tprops: {\n\t\t\t\t\tcontent: markdownContent\n\t\t\t\t}\n\t\t\t}];\n\t\t\tlayoutTitle = '';\n\t\t\tlayoutDescription = '';\n\t\t} else if (components && components.length > 0) {\n\t\t\t// Data was queried — generate components\n\t\t\tlogger.info(`[AgentFlow] ${agentResponse.executedTools.length} tools executed — generating components`);\n\n\t\t\t// Send marker for frontend — flush immediately so __TEXT_COMPLETE__ is\n\t\t\t// delivered as its own chunk before any answer component markers arrive\n\t\t\tif (streamBuffer.hasCallback()) {\n\t\t\t\tstreamBuffer.write('\\n\\n📊 **Generating visualization components...**\\n\\n');\n\t\t\t\tstreamBuffer.write('__TEXT_COMPLETE__COMPONENT_GENERATION_START__');\n\t\t\t\tstreamBuffer.flush();\n\t\t\t}\n\n\t\t\t// Component streaming callback — flush immediately so the marker\n\t\t\t// arrives at the frontend before the final USER_PROMPT_RES response\n\t\t\tconst componentStreamCallback = streamBuffer.hasCallback() ? (component: Component) => {\n\t\t\t\tconst answerMarker = `__ANSWER_COMPONENT_START__${JSON.stringify(component)}__ANSWER_COMPONENT_END__`;\n\t\t\t\tstreamBuffer.write(answerMarker);\n\t\t\t\tstreamBuffer.flush();\n\t\t\t} : undefined;\n\n\t\t\t// Try lightweight component generator first (for all cases — single & multi-source)\n\t\t\t// Falls back to full generator only for federation or failure\n\t\t\tconst hasSql = agentResponse.executedTools.some(t => t.params?.sql || t.params?.query);\n\t\t\tlet usedLightweight = false;\n\n\t\t\tif (hasSql) {\n\t\t\t\tlogger.info(`[AgentFlow] Trying lightweight component generator`);\n\n\t\t\t\ttry {\n\t\t\t\t\t// Prefer the freshly-authored script's executedQueries when MainAgent\n\t\t\t\t\t// wrote + verified a script during this turn. They include the synthetic\n\t\t\t\t\t// `computed:_final` entry carrying post-JS computed columns (e.g.,\n\t\t\t\t\t// MarketShareSquared, weighted averages), which the raw SourceAgent rows\n\t\t\t\t\t// don't have. Without this, the very first run of a script-authored\n\t\t\t\t\t// question would chart raw SQL columns and only the second (replay) run\n\t\t\t\t\t// would show computed columns. See SCRIPT-FLOW-IMPLEMENTATION.md.\n\t\t\t\t\tconst agentQueryIds: Record<string, string> = {};\n\t\t\t\t\tlet agentScriptResult: any;\n\n\t\t\t\t\tif (agentResponse.savedScript?.executedQueries && agentResponse.savedScript.executedQueries.length > 0) {\n\t\t\t\t\t\tlogger.info(`[AgentFlow] Using savedScript.executedQueries (${agentResponse.savedScript.executedQueries.length} entries, includes computed columns) for component gen`);\n\t\t\t\t\t\tconst scriptQueries = agentResponse.savedScript.executedQueries;\n\t\t\t\t\t\tfor (const q of scriptQueries) {\n\t\t\t\t\t\t\t// Authored scripts run with no params; virtual datasets get a\n\t\t\t\t\t\t\t// regeneration descriptor for cache-expiry re-derivation (#6).\n\t\t\t\t\t\t\tagentQueryIds[q.sourceId] = storeScriptDatasetQuery(q, {\n\t\t\t\t\t\t\t\trecipeId: agentResponse.savedScript!.recipeId,\n\t\t\t\t\t\t\t\tparams: {},\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t\tagentScriptResult = {\n\t\t\t\t\t\t\tsuccess: true,\n\t\t\t\t\t\t\tdata: scriptQueries[0]?.data || [],\n\t\t\t\t\t\t\texecutedQueries: scriptQueries,\n\t\t\t\t\t\t\texecutionTimeMs: 0,\n\t\t\t\t\t\t};\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// No script was authored — fall back to SourceAgent's raw rows.\n\t\t\t\t\t\t// IMPORTANT: pull data from `agentResponse.sourceResults` (full rows,\n\t\t\t\t\t\t// up to maxRowsPerSource) — not from `executedTools[].result._sampleData`,\n\t\t\t\t\t\t// which is capped at 3 rows for main-agent prompt efficiency. Using\n\t\t\t\t\t\t// sample data here caused the frontend chart to receive only 3 rows.\n\t\t\t\t\t\tconst sourceResultsById = new Map(\n\t\t\t\t\t\t\tagentResponse.sourceResults.map(sr => [sr.sourceId, sr])\n\t\t\t\t\t\t);\n\t\t\t\t\t\tagentScriptResult = {\n\t\t\t\t\t\t\tsuccess: true,\n\t\t\t\t\t\t\tdata: agentResponse.sourceResults[0]?.data || [],\n\t\t\t\t\t\t\texecutedQueries: agentResponse.executedTools.map(t => {\n\t\t\t\t\t\t\t\tconst sql = t.params?.sql || t.params?.query || '';\n\t\t\t\t\t\t\t\tconst sr = sourceResultsById.get(t.id);\n\t\t\t\t\t\t\t\tconst fullData = sr?.data || t.result._sampleData || [];\n\t\t\t\t\t\t\t\tconst qId = queryCache.storeQuery(sql, {\n\t\t\t\t\t\t\t\t\tdata: fullData,\n\t\t\t\t\t\t\t\t\tcount: sr?.metadata.rowsReturned || t.result._recordsShown || 0,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tagentQueryIds[t.id] = qId;\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\tsourceId: t.id,\n\t\t\t\t\t\t\t\t\tsourceName: t.name,\n\t\t\t\t\t\t\t\t\tsql,\n\t\t\t\t\t\t\t\t\tdata: fullData,\n\t\t\t\t\t\t\t\t\tcount: sr?.metadata.rowsReturned || t.result._recordsShown || 0,\n\t\t\t\t\t\t\t\t\ttotalCount: sr?.metadata.totalRowsMatched || t.result._totalRecords,\n\t\t\t\t\t\t\t\t\texecutionTimeMs: sr?.metadata.executionTimeMs || 0,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\texecutionTimeMs: 0,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\t// Retry the script-bound generator on TRANSIENT (network/provider)\n\t\t\t\t\t// errors before giving up. Without this, a one-off \"Connection error\"\n\t\t\t\t\t// drops us to the full generator, which writes its OWN SQL and can\n\t\t\t\t\t// diverge from the script's answer. Keeping the script-bound path is\n\t\t\t\t\t// what guarantees the analysis and the components render the SAME data.\n\t\t\t\t\tconst compGenArgs = {\n\t\t\t\t\t\tuserPrompt: prompt,\n\t\t\t\t\t\tscriptResult: agentScriptResult,\n\t\t\t\t\t\tqueryIds: agentQueryIds,\n\t\t\t\t\t\tavailableComponents: components,\n\t\t\t\t\t\tapiKey,\n\t\t\t\t\t\tmodel: sourceAgentModel,\n\t\t\t\t\t\tcomponentStreamCallback,\n\t\t\t\t\t\texternalTools: agentTools,\n\t\t\t\t\t\tcollections,\n\t\t\t\t\t\tanalysisText: agentResponse.text,\n\t\t\t\t\t};\n\t\t\t\t\tconst MAX_COMP_ATTEMPTS = 3;\n\t\t\t\t\tlet compResult: Awaited<ReturnType<typeof generateScriptComponents>> | undefined;\n\t\t\t\t\tfor (let attempt = 1; attempt <= MAX_COMP_ATTEMPTS; attempt++) {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tcompResult = await generateScriptComponents(compGenArgs);\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t\tconst msg = err instanceof Error ? err.message : String(err);\n\t\t\t\t\t\t\tconst transient = /connection error|econnreset|etimedout|fetch failed|socket hang up|overloaded|rate.?limit|\\b429\\b|\\b5\\d\\d\\b|network|timeout/i.test(msg);\n\t\t\t\t\t\t\tif (!transient || attempt === MAX_COMP_ATTEMPTS) throw err;\n\t\t\t\t\t\t\tlogger.warn(`[AgentFlow] Lightweight component gen transient error (attempt ${attempt}/${MAX_COMP_ATTEMPTS}) — retrying: ${msg}`);\n\t\t\t\t\t\t\tawait new Promise(res => setTimeout(res, 400 * attempt));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif (compResult && compResult.components.length > 0) {\n\t\t\t\t\t\tmatchedComponents = compResult.components;\n\t\t\t\t\t\tlayoutTitle = compResult.layoutTitle;\n\t\t\t\t\t\tlayoutDescription = compResult.layoutDescription;\n\t\t\t\t\t\tactions = compResult.actions;\n\t\t\t\t\t\tauthoredComponentSpecs = compResult.componentSpecs;\n\t\t\t\t\t\tusedLightweight = true;\n\t\t\t\t\t\tlogger.info(`[AgentFlow] Lightweight component gen succeeded — ${matchedComponents.length} components`);\n\t\t\t\t\t}\n\t\t\t\t} catch (lightErr) {\n\t\t\t\t\tlogger.warn(`[AgentFlow] Lightweight component gen failed — falling back to full generator: ${lightErr}`);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (!usedLightweight) {\n\t\t\t\t// Full component generator — writes its own SQL, validates, handles federation\n\t\t\t\tlogger.info(`[AgentFlow] Using full component generator`);\n\n\t\t\t\tconst cleanAnalysis = (agentResponse.text || textResponse)\n\t\t\t\t\t.replace(/__SB_\\w+_(?:START|MSG)_?_?/g, '')\n\t\t\t\t\t.replace(/_?_?SB_END_?_?/g, '')\n\t\t\t\t\t.replace(/__QUERY_TIMER_START_[^_]*__/g, '')\n\t\t\t\t\t.replace(/__QUERY_TIMER_DONE_[\\d.]+__/g, '')\n\t\t\t\t\t.replace(/\\[COMPLEXITY:\\s*(?:simple|medium|complex)\\]/gi, '')\n\t\t\t\t\t.replace(/<DataTable>[\\s\\S]*?<\\/DataTable>/g,\n\t\t\t\t\t\t'<DataTable>[Data preview removed - for table components, REUSE the exact SQL from EXECUTED_TOOLS parameters. Do NOT write a new query or embed data in props.]</DataTable>'\n\t\t\t\t\t);\n\n\t\t\t\t// If multiple source tools were executed and a federation tool exists,\n\t\t\t\t// inject it into executedTools so the component generator can write cross-source JOINs\n\t\t\t\tconst componentExecutedTools = [...agentResponse.executedTools];\n\t\t\t\tconst federationTool = agentTools.find(t => t.id === 'federation_query');\n\t\t\t\tif (federationTool && agentResponse.executedTools.length >= 2) {\n\t\t\t\t\tconst sourceToolIds = new Set(agentResponse.executedTools.map(t => t.id));\n\t\t\t\t\tconst hasMultipleSources = sourceToolIds.size >= 2;\n\t\t\t\t\tif (hasMultipleSources) {\n\t\t\t\t\t\tcomponentExecutedTools.push({\n\t\t\t\t\t\t\tid: 'federation_query',\n\t\t\t\t\t\t\tname: 'Cross-Source Query',\n\t\t\t\t\t\t\tparams: { sql: 'DuckDB SQL with schema-qualified table names' },\n\t\t\t\t\t\t\tresult: {\n\t\t\t\t\t\t\t\t_totalRecords: 0,\n\t\t\t\t\t\t\t\t_recordsShown: 0,\n\t\t\t\t\t\t\t\t_sampleData: [],\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\toutputSchema: federationTool.outputSchema,\n\t\t\t\t\t\t\tsourceSchema: federationTool.fullSchema || federationTool.description,\n\t\t\t\t\t\t\tsourceType: 'duckdb',\n\t\t\t\t\t\t});\n\t\t\t\t\t\tlogger.info(`[AgentFlow] Injected federation_query tool for cross-source component generation`);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst matchResult = await generateAgentComponents({\n\t\t\t\t\tanalysisContent: cleanAnalysis,\n\t\t\t\t\tcomponents,\n\t\t\t\t\tuserPrompt: prompt,\n\t\t\t\t\texecutedTools: componentExecutedTools,\n\t\t\t\t\tcollections,\n\t\t\t\t\tapiKey,\n\t\t\t\t\tcomponentStreamCallback,\n\t\t\t\t\tuserId,\n\t\t\t\t});\n\t\t\t\tmatchedComponents = matchResult.components;\n\t\t\t\tlayoutTitle = matchResult.layoutTitle;\n\t\t\t\tlayoutDescription = matchResult.layoutDescription;\n\t\t\t\tactions = matchResult.actions;\n\t\t\t}\n\t\t}\n\n\t\t// Strip SQL from components — only queryIds sent to frontend\n\t\t// Also build a queryMap (queryId → SQL) for persisting alongside the component\n\t\tconst queryMap: Record<string, any> = {};\n\t\tconst securedComponents = matchedComponents.map(comp => {\n\t\t\tconst props: any = { ...comp.props };\n\n\t\t\t// Handle externalTool.parameters.sql → replace with queryId\n\t\t\t// Also handle LLM using \"query\" instead of \"sql\" as parameter key\n\t\t\tconst sqlValue = props.externalTool?.parameters?.sql || props.externalTool?.parameters?.query;\n\t\t\tif (sqlValue) {\n\t\t\t\tconst { sql, query, ...restParams } = props.externalTool.parameters;\n\t\t\t\t// Pass cached validation data so frontend doesn't re-execute\n\t\t\t\tconst toolId = props.externalTool?.toolId || '';\n\t\t\t\tconst cacheKey = toolId ? `${toolId}:${sqlValue}` : sqlValue;\n\t\t\t\tconst cachedData = queryCache.get(cacheKey);\n\t\t\t\tconst queryId = queryCache.storeQuery(sqlValue, cachedData);\n\t\t\t\tqueryMap[queryId] = sqlValue;\n\t\t\t\tprops.externalTool = {\n\t\t\t\t\t...props.externalTool,\n\t\t\t\t\tparameters: { queryId, ...restParams },\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Handle legacy direct props.query → replace with queryId\n\t\t\tif (props.query) {\n\t\t\t\tconst { query, ...restProps } = props;\n\t\t\t\tconst queryId = queryCache.storeQuery(query);\n\t\t\t\tqueryMap[queryId] = query;\n\t\t\t\treturn { ...comp, props: { ...restProps, queryId } };\n\t\t\t}\n\n\t\t\treturn { ...comp, props };\n\t\t});\n\n\t\t// Build container component if we have matched components\n\t\tlet containerComponent: Component | null = null;\n\t\tif (securedComponents.length > 0) {\n\t\t\tcontainerComponent = {\n\t\t\t\tid: `container_${Date.now()}`,\n\t\t\t\tname: 'MultiComponentContainer',\n\t\t\t\ttype: 'Container',\n\t\t\t\tdescription: layoutDescription,\n\t\t\t\tprops: {\n\t\t\t\t\tconfig: {\n\t\t\t\t\t\ttitle: layoutTitle,\n\t\t\t\t\t\tdescription: layoutDescription,\n\t\t\t\t\t\tcomponents: securedComponents,\n\t\t\t\t\t\t// Persist queryId → SQL mapping so cached responses can re-register queries\n\t\t\t\t\t\t...(Object.keys(queryMap).length > 0 && { _queryMap: queryMap })\n\t\t\t\t\t},\n\t\t\t\t\tactions: actions\n\t\t\t\t}\n\t\t\t};\n\t\t}\n\n\t\t// ============================================\n\t\t// STEP 2.5: Persist the script MainAgent authored (if any)\n\t\t// ============================================\n\t\t// MainAgent writes + executes scripts inside its own turn via write_script /\n\t\t// execute_script. When a draft passed verification, it arrives here on\n\t\t// agentResponse.savedScript — we just persist it. No post-hoc LLM generation.\n\n\t\t// MainAgent already persisted the draft to disk via ScriptStore.saveDraft()\n\t\t// at write_script time. We just need to promote it: flip status='verified',\n\t\t// apply provenance + optional fork lineage, and (when possible) drop the\n\t\t// turn suffix from the on-disk filename. Failed drafts (no savedScript)\n\t\t// stay on disk with status='draft' for the user to inspect.\n\t\tif (agentResponse.savedScript) {\n\t\t\ttry {\n\t\t\t\tconst authored = agentResponse.savedScript;\n\n\t\t\t\t// Lineage from forkContext (when matcher returned tier='near').\n\t\t\t\tconst lineage = forkContext\n\t\t\t\t\t? {\n\t\t\t\t\t\tparentId: forkContext.parentRecipeId,\n\t\t\t\t\t\tforkDepth: Math.min(FORK_DEPTH_CAP, forkContext.parentDepth + 1),\n\t\t\t\t\t\tforkReason: forkContext.modificationHint,\n\t\t\t\t\t}\n\t\t\t\t\t: undefined;\n\n\t\t\t\tconst promoted = await scriptStore.promoteToVerified(authored.recipeId, {\n\t\t\t\t\tsourceIds: authored.sourceIds,\n\t\t\t\t\ttables: authored.tables,\n\t\t\t\t\t...lineage,\n\t\t\t\t\tcomponents: authoredComponentSpecs,\n\t\t\t\t});\n\n\t\t\t\tif (promoted && lineage?.parentId) {\n\t\t\t\t\tlogger.info(\n\t\t\t\t\t\t`[AgentFlow] Fork persisted: \"${authored.name}\" → parent \"${forkContext!.parentName}\" ` +\n\t\t\t\t\t\t`(depth ${promoted.forkDepth})`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconst msg = err instanceof Error ? err.message : String(err);\n\t\t\t\tlogger.warn(`[AgentFlow] Failed to promote authored script (non-blocking): ${msg}`);\n\t\t\t}\n\t\t}\n\n\t\tconst elapsedTime = Date.now() - startTime;\n\t\tlogger.info(`[AgentFlow] Complete | ${agentResponse.executedTools.length} tools | ${matchedComponents.length} components | ${elapsedTime}ms`);\n\n\t\treturn {\n\t\t\tsuccess: true,\n\t\t\tdata: {\n\t\t\t\ttext: textResponse,\n\t\t\t\tcomponent: containerComponent,\n\t\t\t\tactions: actions,\n\t\t\t\tmethod: `${provider}-agent-response`\n\t\t\t},\n\t\t\terrors: []\n\t\t};\n\n\t} catch (error) {\n\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\t\tlogger.error(`[AgentFlow] Error: ${errorMsg}`);\n\n\t\tuserPromptErrorLogger.logError(\n\t\t\t'agentUserResponse',\n\t\t\terror instanceof Error ? error : new Error(errorMsg),\n\t\t\t{ userPrompt: prompt }\n\t\t);\n\n\t\tconst elapsedTime = Date.now() - startTime;\n\t\tlogger.info(`[AgentFlow] Failed in ${elapsedTime}ms`);\n\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\terrors: [errorMsg],\n\t\t\tdata: {\n\t\t\t\ttext: 'I apologize, but I encountered an error processing your request. Please try again.',\n\t\t\t\tmethod: `${provider}-agent-error`\n\t\t\t}\n\t\t};\n\t}\n};\n","import { logger } from './logger';\nimport { llmUsageLogger } from './llm-usage-logger';\n\n/**\n * Chat Analytics Client\n * Posts analytics events to the global SA API after each user prompt request.\n *\n * Required env vars:\n * SA_API_URL — Base URL of the SA API (e.g. https://api.superatom.ai)\n *\n * Optional:\n * SA_ORG_ID — Organization ID (nullable, for multi-tenant filtering)\n * SA_ANALYTICS_ENABLED — Set to 'false' to disable (default: true)\n */\n\nexport interface ChatAnalyticsEvent {\n userId: string;\n orgId?: string;\n projectId: string;\n threadId: string;\n messageIndex: number;\n question: string;\n sourcesUsed?: { sourceId: string; sourceName: string; sourceType: string }[];\n sqlGenerated?: string;\n model: string;\n inputTokens: number;\n outputTokens: number;\n cost: string;\n latencyMs: number;\n status: 'success' | 'error';\n errorMessage?: string;\n}\n\nclass AnalyticsClient {\n private apiUrl: string | null = null;\n private orgId: string | null = null;\n private enabled: boolean = true;\n private initialized: boolean = false;\n\n private init(): void {\n if (this.initialized) return;\n this.initialized = true;\n\n this.apiUrl = process.env.SA_API_URL || null;\n this.orgId = process.env.SA_ORG_ID || null;\n this.enabled = process.env.SA_ANALYTICS_ENABLED !== 'false';\n\n if (!this.apiUrl) {\n if (this.enabled) {\n logger.warn('[Analytics] Disabled — SA_API_URL not configured');\n }\n this.enabled = false;\n }\n }\n\n /**\n * Track a chat analytics event. Fire-and-forget — never blocks or throws.\n */\n async track(event: ChatAnalyticsEvent): Promise<void> {\n this.init();\n if (!this.enabled) return;\n\n try {\n const response = await fetch(`${this.apiUrl}/analytics/chat`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(event),\n });\n\n if (!response.ok) {\n const errorText = await response.text().catch(() => 'unknown');\n logger.warn(`[Analytics] Failed to track event: ${response.status} ${errorText}`);\n }\n } catch (err) {\n logger.warn(`[Analytics] Failed to send event: ${err instanceof Error ? err.message : String(err)}`);\n }\n }\n\n /**\n * Build a ChatAnalyticsEvent from the available request context.\n * Reads session stats from llmUsageLogger automatically.\n */\n buildEvent(params: {\n userId: string;\n projectId: string;\n threadId: string;\n messageIndex: number;\n question: string;\n success: boolean;\n latencyMs: number;\n model: string;\n errorMessage?: string;\n sourcesUsed?: { sourceId: string; sourceName: string; sourceType: string }[];\n sqlGenerated?: string;\n }): ChatAnalyticsEvent {\n this.init();\n\n const stats = llmUsageLogger.getSessionStats();\n\n return {\n userId: params.userId,\n orgId: this.orgId || undefined,\n projectId: params.projectId,\n threadId: params.threadId,\n messageIndex: params.messageIndex,\n question: params.question,\n sourcesUsed: params.sourcesUsed,\n sqlGenerated: params.sqlGenerated,\n model: params.model,\n inputTokens: stats.totalInputTokens,\n outputTokens: stats.totalOutputTokens,\n cost: stats.totalCostUSD.toFixed(6),\n latencyMs: params.latencyMs,\n status: params.success ? 'success' : 'error',\n errorMessage: params.errorMessage,\n };\n }\n}\n\nexport const analyticsClient = new AnalyticsClient();\n","import { logger } from './logger';\n\n/**\n * Unified UIBlock structure for database storage\n * Used in both bookmarks and user-conversations tables\n */\nexport interface DBUIBlock {\n\tid: string;\n\tcomponent: Record<string, any> | null;\n\tanalysis: string | null;\n\tuser_prompt: string;\n}\n\n/**\n * Transform UIBlock.toJSON() output to the unified DB structure\n * Maps: userQuestion -> user_prompt, generatedComponentMetadata -> component, textResponse -> analysis\n * @param uiblock - UIBlock.toJSON() output\n * @param userPrompt - The user's prompt/question\n * @param uiBlockId - The UIBlock ID (passed separately, not in uiblock object)\n */\nexport function transformUIBlockForDB(uiblock: any, userPrompt: string, uiBlockId: string): DBUIBlock {\n\t// Extract component from generatedComponentMetadata, or null if empty\n\tconst component = uiblock?.generatedComponentMetadata && Object.keys(uiblock.generatedComponentMetadata).length > 0\n\t\t? uiblock.generatedComponentMetadata\n\t\t: null;\n\n\treturn {\n\t\tid: uiBlockId || uiblock?.id || '',\n\t\tcomponent: component,\n\t\tanalysis: uiblock?.textResponse || null,\n\t\tuser_prompt: userPrompt || uiblock?.userQuestion || '',\n\t};\n}\n\n/**\n * Parameters for saving a conversation\n */\nexport interface SaveConversationParams {\n\tuserId: string;\n\tuserPrompt: string;\n\tuiblock: any; // UIBlock toJSON() output\n\tuiBlockId: string; // ID of the UIBlock\n\tthreadId: string;\n\tcollections?: any;\n}\n\n/**\n * Result of attempting to save a conversation\n */\nexport interface SaveConversationResult {\n\tsuccess: boolean;\n\tconversationId?: string;\n\tmessage?: string;\n\terror?: string;\n}\n\n/**\n * Saves a user conversation to the backend user_conversations table\n * This enables semantic search and conversation history features\n *\n * @param params - Parameters containing userId, userPrompt, uiblock, threadId, and collections\n * @returns Result indicating success/failure and conversation ID if successful\n */\nexport async function saveConversation(params: SaveConversationParams): Promise<SaveConversationResult> {\n\tconst { userId, userPrompt, uiblock, uiBlockId, threadId, collections } = params;\n\n\t// Validate required parameters\n\tif (!userId) {\n\t\tlogger.warn('[CONVERSATION_SAVER] Skipping save: userId not provided');\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\terror: 'userId is required'\n\t\t};\n\t}\n\n\tif (!userPrompt) {\n\t\tlogger.warn('[CONVERSATION_SAVER] Skipping save: userPrompt not provided');\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\terror: 'userPrompt is required'\n\t\t};\n\t}\n\n\tif (!uiblock) {\n\t\tlogger.warn('[CONVERSATION_SAVER] Skipping save: uiblock not provided');\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\terror: 'uiblock is required'\n\t\t};\n\t}\n\n\tif (!threadId) {\n\t\tlogger.warn('[CONVERSATION_SAVER] Skipping save: threadId not provided');\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\terror: 'threadId is required'\n\t\t};\n\t}\n\n\tif (!uiBlockId) {\n\t\tlogger.warn('[CONVERSATION_SAVER] Skipping save: uiBlockId not provided');\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\terror: 'uiBlockId is required'\n\t\t};\n\t}\n\n\t// Check if collections are available\n\tif (!collections?.['user-conversations']?.['create']) {\n\t\tlogger.debug('[CONVERSATION_SAVER] Collection \"user-conversations.create\" not available, skipping save');\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\terror: 'user-conversations.create collection not available'\n\t\t};\n\t}\n\n\ttry {\n\t\tlogger.info(`[CONVERSATION_SAVER] Saving conversation for userId: ${userId}, uiBlockId: ${uiBlockId}, threadId: ${threadId}`);\n\n\t\t// Transform UIBlock to unified DB structure\n\t\tconst dbUIBlock = transformUIBlockForDB(uiblock, userPrompt, uiBlockId);\n\t\tlogger.debug(`[CONVERSATION_SAVER] Transformed UIBlock for DB: ${JSON.stringify(dbUIBlock)}`);\n\n\t\t// Step 1: Save to PostgreSQL user_conversations table\n\t\tconst saveResult = await collections['user-conversations']['create']({\n\t\t\tuserId,\n\t\t\tuserPrompt,\n\t\t\tuiblock: dbUIBlock,\n\t\t\tthreadId\n\t\t});\n\n\t\tif (!saveResult?.success) {\n\t\t\tlogger.warn(`[CONVERSATION_SAVER] Failed to save conversation to PostgreSQL: ${saveResult?.message || 'Unknown error'}`);\n\t\t\treturn {\n\t\t\t\tsuccess: false,\n\t\t\t\terror: saveResult?.message || 'Unknown error from backend'\n\t\t\t};\n\t\t}\n\n\t\tlogger.info(`[CONVERSATION_SAVER] Successfully saved conversation to PostgreSQL, id: ${saveResult.data?.id}`);\n\n\t\t// Step 2: Create embedding in ChromaDB for semantic search\n\t\tif (collections?.['conversation-history']?.['embed']) {\n\t\t\ttry {\n\t\t\t\tlogger.info('[CONVERSATION_SAVER] Creating embedding for semantic search...');\n\t\t\t\tconst embedResult = await collections['conversation-history']['embed']({\n\t\t\t\t\tuiBlockId,\n\t\t\t\t\tuserPrompt,\n\t\t\t\t\tuiBlock: dbUIBlock, // Use the transformed UIBlock\n\t\t\t\t\tuserId\n\t\t\t\t});\n\n\t\t\t\tif (embedResult?.success) {\n\t\t\t\t\tlogger.info('[CONVERSATION_SAVER] Successfully created embedding');\n\t\t\t\t} else {\n\t\t\t\t\tlogger.warn('[CONVERSATION_SAVER] Failed to create embedding:', embedResult?.error || 'Unknown error');\n\t\t\t\t\t// Don't fail the entire operation if embedding fails\n\t\t\t\t}\n\t\t\t} catch (embedError) {\n\t\t\t\tconst embedErrorMsg = embedError instanceof Error ? embedError.message : String(embedError);\n\t\t\t\tlogger.warn('[CONVERSATION_SAVER] Error creating embedding:', embedErrorMsg);\n\t\t\t\t// Don't fail the entire operation if embedding fails\n\t\t\t}\n\t\t} else {\n\t\t\tlogger.debug('[CONVERSATION_SAVER] Embedding collection not available, skipping ChromaDB storage');\n\t\t}\n\n\t\treturn {\n\t\t\tsuccess: true,\n\t\t\tconversationId: saveResult.data?.id,\n\t\t\tmessage: 'Conversation saved successfully'\n\t\t};\n\t} catch (error) {\n\t\tconst errorMessage = error instanceof Error ? error.message : String(error);\n\t\tlogger.error('[CONVERSATION_SAVER] Error saving conversation:', errorMessage);\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\terror: errorMessage\n\t\t};\n\t}\n}\n","/**\n * Configuration for conversation context and history management\n */\nexport const CONTEXT_CONFIG = {\n /**\n * Maximum number of previous UIBlocks to include as conversation context\n * Set to 0 to disable conversation history\n * Higher values provide more context but may increase token usage\n */\n MAX_CONVERSATION_CONTEXT_BLOCKS: 3,\n};\n","import { Component, LLMProvider, Message, UserPromptRequestMessageSchema, T_RESPONSE } from \"../types\";\n// Old flow kept for reference: import { get_user_response } from \"../userResponse\";\nimport { get_agent_user_response } from \"../userResponse/agent-user-response\";\nimport type { WorkflowDescriptor } from \"../userResponse/agents\";\nimport { logger } from \"../utils/logger\";\nimport { llmUsageLogger } from \"../utils/llm-usage-logger\";\nimport { userPromptErrorLogger } from \"../utils/user-prompt-error-logger\";\nimport { analyticsClient } from \"../utils/analytics-client\";\nimport { saveConversation } from \"../utils/conversation-saver\";\nimport { ThreadManager, UIBlock } from \"../threads\";\nimport { CONTEXT_CONFIG } from \"../config/context\";\nimport { ZodError } from \"zod\";\nimport { nanoid } from \"zod/v4\";\n\n\n// Define return type for get_user_request extending T_RESPONSE\ninterface UserRequestResponse extends T_RESPONSE {\n\tuiBlockId?: string;\n\tthreadId?: string;\n\tid?: string;\n\twsId?: string;\n}\n\n\nconst get_user_request = async (\n\tdata: any,\n\tcomponents: Component[],\n\tsendMessage: (message: Message) => void,\n\tanthropicApiKey?: string,\n\tgroqApiKey?: string,\n\tgeminiApiKey?: string,\n\topenaiApiKey?: string,\n\tllmProviders?: LLMProvider[],\n\tcollections?: any,\n\texternalTools?: any[],\n\tmainAgentModel?: string,\n\tsourceAgentModel?: string,\n\tworkflows?: WorkflowDescriptor[],\n): Promise<UserRequestResponse> => {\n\tconst errors: string[] = [];\n\n\t// Parse incoming message data\n\tconst parseResult = UserPromptRequestMessageSchema.safeParse(data);\n\n\tif (!parseResult.success) {\n\t\tconst zodError = parseResult.error as ZodError;\n\t\tzodError.errors.forEach(err => {\n\t\t\terrors.push(`${err.path.join('.')}: ${err.message}`);\n\t\t});\n\t\treturn { success: false, errors };\n\t}\n\n\tconst userPromptRequest = parseResult.data;\n\tconst { id, payload } = userPromptRequest;\n\n\tconst prompt = payload.prompt;\n\tconst userId = payload.userId;\n\tconst SA_RUNTIME = payload.SA_RUNTIME;\n\tconst wsId = userPromptRequest.from.id || 'unknown';\n\n\t// Reset log files for this new request (clears previous request logs)\n\tconst promptContext = `User Prompt: ${prompt?.substring(0, 50)}${(prompt?.length || 0) > 50 ? '...' : ''}`;\n\tllmUsageLogger.resetLogFile(promptContext);\n\tuserPromptErrorLogger.resetLogFile(promptContext);\n\n\t\n\t// Validate SA_RUNTIME and extract threadId and uiBlockId\n\tif (!SA_RUNTIME) {\n\t\terrors.push('SA_RUNTIME is required');\n\t}\n\t\n\tconst threadId = SA_RUNTIME?.threadId;\n\tconst existingUiBlockId = SA_RUNTIME?.uiBlockId;\n\t\n\tif (!threadId) {\n\t\terrors.push('threadId in SA_RUNTIME is required');\n\t}\n\t\n\tif (!existingUiBlockId) {\n\t\terrors.push('uiBlockId in SA_RUNTIME is required');\n\t}\n\t\n\tif (!prompt) {\n\t\terrors.push('Prompt not found');\n\t}\n\t\n\n\t// If there are validation errors, return early\n\tif (errors.length > 0) {\n\t\treturn { success: false, errors, id, wsId };\n\t}\n\n\t// Get or create thread BEFORE processing to enable conversation context\n\tconst threadManager = ThreadManager.getInstance();\n\tlet thread = threadManager.getThread(threadId!);\n\tif (!thread) {\n\t\tthread = threadManager.createThread(threadId!);\n\t\tlogger.info(`Created new thread: ${threadId}`);\n\t}\n\n\tconst requestStartTime = Date.now();\n\tlogger.info(`Starting user prompt request with ${components.length} components`);\n\n\t// Extract conversation context from thread (excluding current UIBlock)\n\tconst conversationHistory = thread.getConversationContext(CONTEXT_CONFIG.MAX_CONVERSATION_CONTEXT_BLOCKS, existingUiBlockId!);\n\n\t// Get responseMode from payload\n\tconst responseMode = payload.responseMode || 'component' ; \n\n\t// Create stream callback for text mode - sends chunks via WebSocket AND accumulates them\n\tlet streamCallback: ((chunk: string) => void) | undefined;\n\tlet accumulatedStreamResponse = ''; // Accumulate all streaming chunks for storage in UIBlock\n\tif (responseMode === 'text') {\n\t\tstreamCallback = (chunk: string) => {\n\t\t\t// Accumulate chunk for storage in UIBlock\n\t\t\taccumulatedStreamResponse += chunk;\n\n\t\t\t// Send streaming chunk to frontend via WebSocket\n\t\t\t// IMPORTANT: Use stream_${uiBlockId} as ID to avoid conflicting with the final response\n\t\t\t// The frontend will match by the stream_ prefix and uiBlockId\n\t\t\ttry {\n\t\t\t\tconst streamMessage: Message = {\n\t\t\t\t\tid: `stream_${existingUiBlockId}`, // Different ID pattern for streaming\n\t\t\t\t\ttype: 'USER_PROMPT_STREAM',\n\t\t\t\t\tfrom: { type: 'data-agent' },\n\t\t\t\t\tto: {\n\t\t\t\t\t\ttype: 'runtime',\n\t\t\t\t\t\tid: wsId\n\t\t\t\t\t},\n\t\t\t\t\tpayload: {\n\t\t\t\t\t\tuiBlockId: existingUiBlockId,\n\t\t\t\t\t\tchunk\n\t\t\t\t\t}\n\t\t\t\t};\n\t\t\t\tsendMessage(streamMessage);\n\t\t\t} catch (err) {\n\t\t\t\t// WebSocket may have closed mid-stream — log but don't crash the pipeline.\n\t\t\t\t// The response is still accumulated and will be stored in the UIBlock.\n\t\t\t\tlogger.warn(`[UserPromptRequest] Stream send failed (WebSocket may be closed): ${err instanceof Error ? err.message : String(err)}`);\n\t\t\t}\n\t\t};\n\t}\n\n\t// Get user response using multi-agent flow (MainAgent routes to SourceAgents)\n\t// Old flow (get_user_response) kept in userResponse/index.ts for reference\n\tconst userResponse = await get_agent_user_response(\n\t\tprompt!,\n\t\tcomponents,\n\t\tanthropicApiKey,\n\t\tgroqApiKey,\n\t\tgeminiApiKey,\n\t\topenaiApiKey,\n\t\tllmProviders,\n\t\tconversationHistory,\n\t\tstreamCallback,\n\t\tcollections,\n\t\texternalTools,\n\t\tuserId,\n\t\tmainAgentModel,\n\t\tsourceAgentModel,\n\t\tworkflows,\n\t);\n\n\tlogger.info('User prompt request completed');\n\n\t// Use the uiBlockId from SA_RUNTIME (already validated)\n\tconst uiBlockId = existingUiBlockId!;\n\n\t// If response failed, don't create UIBlock and return error\n\tif (!userResponse.success) {\n\t\tlogger.error(`User prompt request failed with errors: ${userResponse.errors.join(', ')}`);\n\n\t\t// Log detailed error information\n\t\tuserPromptErrorLogger.logError('User Response Failed', userResponse.errors.join('\\n'), {\n\t\t\tprompt: prompt,\n\t\t\tuiBlockId,\n\t\t\tthreadId: threadId!,\n\t\t\tresponseData: userResponse.data\n\t\t});\n\t\tuserPromptErrorLogger.writeSummary();\n\t\tllmUsageLogger.logSessionSummary(`FAILED: ${prompt?.substring(0, 30)}`);\n\n\t\t// Track failed request analytics (fire-and-forget)\n\t\tif (userId) {\n\t\t\tconst latencyMs = Date.now() - requestStartTime;\n\t\t\tconst model = mainAgentModel || 'unknown';\n\t\t\tconst event = analyticsClient.buildEvent({\n\t\t\t\tuserId,\n\t\t\t\tprojectId: process.env.SUPERATOM_PROJECT_ID || '',\n\t\t\t\tthreadId: threadId!,\n\t\t\t\tmessageIndex: 0,\n\t\t\t\tquestion: prompt!,\n\t\t\t\tsuccess: false,\n\t\t\t\tlatencyMs,\n\t\t\t\tmodel,\n\t\t\t\terrorMessage: userResponse.errors.join('; '),\n\t\t\t});\n\t\t\tanalyticsClient.track(event).catch(() => {});\n\t\t}\n\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\tdata: userResponse.data,\n\t\t\terrors: userResponse.errors,\n\t\t\tuiBlockId,\n\t\t\tthreadId: threadId!,\n\t\t\tid,\n\t\t\twsId\n\t\t};\n\t}\n\n\t// Extract component, textResponse, and actions from successful response\n\tlet component = null;\n\tlet textResponse = null;\n\tlet actions = [];\n\n\tif (userResponse.data) {\n\t\tif (typeof userResponse.data === 'object') {\n\t\t\tif ('component' in userResponse.data) {\n\t\t\t\tcomponent = (userResponse.data as any).component;\n\t\t\t}\n\t\t\t// Check for both 'text' and 'textResponse' keys\n\t\t\tif ('text' in userResponse.data) {\n\t\t\t\ttextResponse = (userResponse.data as any).text;\n\t\t\t} else if ('textResponse' in userResponse.data) {\n\t\t\t\ttextResponse = (userResponse.data as any).textResponse;\n\t\t\t}\n\t\t\tif ('actions' in userResponse.data) {\n\t\t\t\tactions = (userResponse.data as any).actions || [];\n\t\t\t}\n\t\t}\n\t}\n\n\t// For text mode, use accumulated stream response which includes ALL messages (analyzing, query, results, etc.)\n\t// Strip control markers before storing — they're only for real-time streaming, not for replay\n\t// For component mode, use the textResponse as-is\n\tlet finalTextResponse = responseMode === 'text' && accumulatedStreamResponse\n\t\t? accumulatedStreamResponse\n\t\t: textResponse;\n\tif (finalTextResponse) {\n\t\tfinalTextResponse = finalTextResponse\n\t\t\t.replace(/__TEXT_COMPLETE__[\\s\\S]*/g, '')\n\t\t\t.replace(/__ANSWER_COMPONENT_START__[\\s\\S]*?__ANSWER_COMPONENT_END__/g, '')\n\t\t\t.replace(/__COMPONENT_GENERATION_START__/g, '')\n\t\t\t.trim();\n\t}\n\n\t// Create UIBlock with component metadata, empty component data, and full text response\n\tconst uiBlock = new UIBlock(\n\t\tprompt!,\n\t\t{}, // componentData: initially empty, will be filled later\n\t\tcomponent, // generatedComponentMetadata: full component object (ComponentSchema)\n\t\t[], // actions: empty initially, will be set below\n\t\tuiBlockId,\n\t\tfinalTextResponse // textResponse: FULL streaming response including all intermediate messages\n\t);\n\n\t// Store actions in UIBlock if they were generated\n\tif (actions.length > 0) {\n\t\tuiBlock.setActions(actions);\n\t\tlogger.info(`Stored ${actions.length} actions in UIBlock: ${uiBlockId}`);\n\t}\n\n\t// Add UIBlock to Thread\n\tthread.addUIBlock(uiBlock);\n\n\tlogger.info(`Created UIBlock: ${uiBlockId} in Thread: ${threadId}`);\n\n\t// Save conversation to backend for semantic search and history\n\t// Skip saving if response came from exact semantic match (>=99%)\n\tif (userId) {\n\t\tconst responseMethod = userResponse.data?.method || '';\n\t\tconst semanticSimilarity = userResponse.data?.semanticSimilarity || 0;\n\n\t\t// Check if this is an exact semantic match (>=99%)\n\t\tconst isExactMatch =\n\t\t\tresponseMethod.includes('semantic-match') && semanticSimilarity >= 0.99;\n\n\t\tif (isExactMatch) {\n\t\t\tlogger.info(\n\t\t\t\t`Skipping conversation save - response from exact semantic match (${(semanticSimilarity * 100).toFixed(2)}% similarity)`\n\t\t\t);\n\t\t} else {\n\t\t\t// Save new conversation\n\t\t\tconst uiBlockData = uiBlock.toJSON();\n\t\t\tconst saveResult = await saveConversation({\n\t\t\t\tuserId,\n\t\t\t\tuserPrompt: prompt!,\n\t\t\t\tuiblock: uiBlockData,\n\t\t\t\tuiBlockId: uiBlockId,\n\t\t\t\tthreadId: threadId!,\n\t\t\t\tcollections\n\t\t\t});\n\n\t\t\tif (saveResult.success) {\n\t\t\t\tlogger.info(`Conversation saved with ID: ${saveResult.conversationId}`);\n\t\t\t} else {\n\t\t\t\tlogger.warn(`Failed to save conversation: ${saveResult.error}`);\n\t\t\t\t// Continue anyway - don't fail the request if conversation saving fails\n\t\t\t}\n\t\t}\n\t}\n\n\t// Log session summary for this request (writes total usage to log file)\n\tllmUsageLogger.logSessionSummary(prompt?.substring(0, 50));\n\n\t// Track analytics (fire-and-forget)\n\tif (userId) {\n\t\tconst latencyMs = Date.now() - requestStartTime;\n\t\tconst model = mainAgentModel || 'unknown';\n\n\t\t// Extract SQL and sources from the generated component's _queryMap and externalTool references\n\t\tconst componentConfig = userResponse.data?.component?.props?.config;\n\t\tconst queryMap = componentConfig?._queryMap || {};\n\t\tconst sqlStatements = Object.values(queryMap).filter((v): v is string => typeof v === 'string');\n\n\t\t// Extract source info from component children's externalTool references\n\t\tconst childComponents: any[] = componentConfig?.components || [];\n\t\tconst sourceSet = new Map<string, { sourceId: string; sourceName: string; sourceType: string }>();\n\t\tfor (const child of childComponents) {\n\t\t\tconst toolId = child?.props?.externalTool?.toolId;\n\t\t\tconst toolName = child?.props?.externalTool?.name;\n\t\t\tif (toolId && !sourceSet.has(toolId)) {\n\t\t\t\tsourceSet.set(toolId, {\n\t\t\t\t\tsourceId: toolId,\n\t\t\t\t\tsourceName: toolName || toolId,\n\t\t\t\t\tsourceType: toolId.split('-')[0] || '', // e.g. \"postgres\" from \"postgres-xxx_query\"\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\tconst sourcesUsed = Array.from(sourceSet.values());\n\n\t\tconst event = analyticsClient.buildEvent({\n\t\t\tuserId,\n\t\t\tprojectId: process.env.SUPERATOM_PROJECT_ID || '',\n\t\t\tthreadId: threadId!,\n\t\t\tmessageIndex: thread.getUIBlockCount(),\n\t\t\tquestion: prompt!,\n\t\t\tsuccess: true,\n\t\t\tlatencyMs,\n\t\t\tmodel,\n\t\t\tsourcesUsed: sourcesUsed.length > 0 ? sourcesUsed : undefined,\n\t\t\tsqlGenerated: sqlStatements.length > 0 ? sqlStatements.join(';\\n') : undefined,\n\t\t});\n\t\tanalyticsClient.track(event).catch(() => {}); // fire-and-forget\n\t}\n\n\t// Return response with uiBlockId and threadId\n\treturn {\n\t\tsuccess: userResponse.success,\n\t\tdata: userResponse.data,\n\t\terrors: userResponse.errors,\n\t\tuiBlockId,\n\t\tthreadId: threadId!,\n\t\tid,\n\t\twsId\n\t};\n}\n\nexport async function handleUserPromptRequest(\n\tdata: any,\n\tcomponents: Component[],\n\tsendMessage: (message: Message) => void,\n\tanthropicApiKey?: string,\n\tgroqApiKey?: string,\n\tgeminiApiKey?: string,\n\topenaiApiKey?: string,\n\tllmProviders?: LLMProvider[],\n\tcollections?: any,\n\texternalTools?: any[],\n\tmainAgentModel?: string,\n\tsourceAgentModel?: string,\n\tworkflows?: WorkflowDescriptor[],\n): Promise<void> {\n\tconst response = await get_user_request(data, components, sendMessage, anthropicApiKey, groqApiKey, geminiApiKey, openaiApiKey, llmProviders, collections, externalTools, mainAgentModel, sourceAgentModel, workflows);\n\n\tif (response.data?.component?.props?.config?.components) {\n response.data.component.props.config.components =\n response.data.component.props.config.components.map((comp: any) => ({\n ...comp,\n id: `comp_${Math.random().toString(36).substring(2, 8)}`\n }));\n }\n\t// Send response using sendDataResponse\n\tsendDataResponse(\n\t\tresponse.id || data.id,\n\t\t{\n\t\t\tsuccess: response.success,\n\t\t\terrors: response.errors,\n\t\t\tdata: response.data,\n\t\t\tuiBlockId: response.uiBlockId,\n\t\t\tthreadId: response.threadId\n\t\t},\n\t\tsendMessage,\n\t\tresponse.wsId || data.from?.id\n\t);\n}\n\n/**\n * Send a data_res response message\n */\nfunction sendDataResponse(\n\tid: string,\n\tres: T_RESPONSE & { uiBlockId?: string; threadId?: string },\n\tsendMessage: (message: Message) => void,\n\tclientId?: string,\n): void {\n\tconst response: Message = {\n\t\tid,\n\t\ttype: 'USER_PROMPT_RES',\n\t\tfrom: { type: 'data-agent' },\n\t\tto: {\n\t\t\ttype: 'runtime',\n\t\t\tid: clientId\n\t\t},\n\t\tpayload: {\n\t\t\t...res,\n\t\t}\n\t};\n\n\tsendMessage(response);\n} ","import { Component, Message, UserPromptSuggestionsMessageSchema, CollectionRegistry } from '../types';\nimport { logger } from '../utils/logger';\n\n/**\n * Handle user prompt suggestions request\n * Uses token-based search for components and PostgreSQL full-text search for bookmarks\n */\nexport async function handleUserPromptSuggestions(\n data: any,\n components: Component[],\n sendMessage: (message: Message) => void,\n collections: CollectionRegistry\n): Promise<void> {\n try {\n const request = UserPromptSuggestionsMessageSchema.parse(data);\n const { id, payload, from } = request;\n\n const { prompt, userId, limit = 10 } = payload;\n const wsId = from.id;\n\n logger.info(`[USER_PROMPT_SUGGESTIONS_REQ ${id}] Processing user prompt suggestions: ${prompt}`);\n\n // Validate input\n if (!prompt || prompt.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Prompt is required and cannot be empty'\n }, sendMessage, wsId);\n return;\n }\n\n // COMMENTED OUT: Semantic/embedding-based search (kept for potential future use)\n // const componentSearchHandler = collections?.['components']?.['search'];\n // const bookmarkSearchHandler = collections?.['bookmarks']?.['search'];\n //\n // let componentSuggestions: any[] = [];\n // let bookmarkSuggestions: any[] = [];\n // let useEmbeddingSearch = false;\n //\n // // Build parallel search promises\n // const searchPromises: Promise<void>[] = [];\n //\n // // Search components via embedding\n // if (componentSearchHandler) {\n // searchPromises.push(\n // (async () => {\n // try {\n // logger.info('Using embedding-based search for components');\n // const result = await componentSearchHandler({ prompt, limit });\n //\n // if (result.success && result.suggestions) {\n // componentSuggestions = result.suggestions.map((s: any) => ({\n // ...s,\n // suggestionType: 'component'\n // }));\n // useEmbeddingSearch = true;\n // logger.info(`[USER_PROMPT_SUGGESTIONS_REQ ${id}] Found ${componentSuggestions.length} component suggestions`);\n // }\n // } catch (embeddingError) {\n // logger.warn('Component embedding search failed:', embeddingError);\n // }\n // })()\n // );\n // }\n //\n // // Search bookmarks via embedding (if userId is available)\n // if (bookmarkSearchHandler && userId && userId !== 'anonymous') {\n // searchPromises.push(\n // (async () => {\n // try {\n // logger.info(`Using embedding-based search for bookmarks (user: ${userId})`);\n // const result = await bookmarkSearchHandler({ prompt, userId, limit });\n //\n // if (result.success && result.suggestions) {\n // bookmarkSuggestions = result.suggestions.map((s: any) => ({\n // ...s,\n // suggestionType: 'bookmark'\n // }));\n // useEmbeddingSearch = true;\n // logger.info(`[USER_PROMPT_SUGGESTIONS_REQ ${id}] Found ${bookmarkSuggestions.length} bookmark suggestions`);\n // }\n // } catch (embeddingError) {\n // logger.warn('Bookmark embedding search failed:', embeddingError);\n // }\n // })()\n // );\n // }\n //\n // // Wait for all searches to complete in parallel\n // if (searchPromises.length > 0) {\n // await Promise.all(searchPromises);\n // }\n //\n // // If we got embedding results, combine and return\n // if (useEmbeddingSearch && (componentSuggestions.length > 0 || bookmarkSuggestions.length > 0)) {\n // // Combine and sort by similarity (no threshold filtering - show all suggestions)\n // const allSuggestions = [...componentSuggestions, ...bookmarkSuggestions]\n // .sort((a, b) => (b.similarity || 0) - (a.similarity || 0))\n // .slice(0, limit);\n //\n // logger.info(`[USER_PROMPT_SUGGESTIONS_REQ ${id}] Returning all suggestions: ${componentSuggestions.length} components, ${bookmarkSuggestions.length} bookmarks`);\n //\n // sendResponse(id, {\n // success: true,\n // data: {\n // prompt,\n // suggestions: allSuggestions,\n // count: allSuggestions.length,\n // componentCount: componentSuggestions.length,\n // bookmarkCount: bookmarkSuggestions.length,\n // message: `Found ${allSuggestions.length} suggestions (${componentSuggestions.length} components, ${bookmarkSuggestions.length} bookmarks)`\n // }\n // }, sendMessage, wsId);\n // return;\n // }\n // END COMMENTED OUT SECTION\n\n // Use token-based search for components and PostgreSQL text search for bookmarks\n const displayComponents = components.filter(c => c.isDisplayComp === true);\n const bookmarkTextSearchHandler = collections?.['bookmarks']?.['textSearch'];\n\n logger.info(`[USER_PROMPT_SUGGESTIONS_REQ ${id}] Using token-based search for components (${displayComponents.length} display components) and PostgreSQL text search for bookmarks`);\n\n // Build parallel search promises\n const searchPromises: Promise<void>[] = [];\n let componentSuggestions: any[] = [];\n let bookmarkSuggestions: any[] = [];\n\n // Token-based search for components\n if (displayComponents.length > 0) {\n const componentResults = searchComponents(prompt, displayComponents, limit);\n componentSuggestions = componentResults.map((c: Component) => ({\n ...c,\n suggestionType: 'component',\n similarity: 1.0 // Token-based search doesn't have similarity scores\n }));\n logger.info(`[USER_PROMPT_SUGGESTIONS_REQ ${id}] Found ${componentSuggestions.length} component suggestions via token search`);\n }\n\n // PostgreSQL text search for bookmarks (if userId is available)\n if (bookmarkTextSearchHandler && userId && userId !== 'anonymous') {\n searchPromises.push(\n (async () => {\n try {\n logger.info(`Using PostgreSQL text search for bookmarks (user: ${userId})`);\n const result = await bookmarkTextSearchHandler({ prompt, userId, limit });\n\n if (result.success && result.suggestions) {\n bookmarkSuggestions = result.suggestions.map((s: any) => ({\n ...s,\n suggestionType: 'bookmark'\n }));\n logger.info(`[USER_PROMPT_SUGGESTIONS_REQ ${id}] Found ${bookmarkSuggestions.length} bookmark suggestions via PostgreSQL text search`);\n }\n } catch (searchError) {\n logger.warn('Bookmark PostgreSQL text search failed:', searchError);\n }\n })()\n );\n }\n\n // Wait for bookmark search to complete\n if (searchPromises.length > 0) {\n await Promise.all(searchPromises);\n }\n\n // Combine results\n const allSuggestions = [...componentSuggestions, ...bookmarkSuggestions].slice(0, limit);\n\n // Count how many of each type are actually in the final result\n const finalComponentCount = allSuggestions.filter(s => s.suggestionType === 'component').length;\n const finalBookmarkCount = allSuggestions.filter(s => s.suggestionType === 'bookmark').length;\n\n if (allSuggestions.length === 0) {\n sendResponse(id, {\n success: true,\n data: {\n prompt,\n suggestions: [],\n count: 0,\n componentCount: 0,\n bookmarkCount: 0,\n message: 'No matching suggestions found'\n }\n }, sendMessage, wsId);\n return;\n }\n\n logger.info(`[USER_PROMPT_SUGGESTIONS_REQ ${id}] Returning ${allSuggestions.length} suggestions: ${finalComponentCount} components, ${finalBookmarkCount} bookmarks`);\n\n sendResponse(id, {\n success: true,\n data: {\n prompt,\n suggestions: allSuggestions,\n count: allSuggestions.length,\n componentCount: finalComponentCount,\n bookmarkCount: finalBookmarkCount,\n message: `Found ${allSuggestions.length} suggestions (${finalComponentCount} components, ${finalBookmarkCount} bookmarks)`\n }\n }, sendMessage, wsId);\n\n } catch (error) {\n logger.error('Failed to handle user prompt suggestions request:', error);\n sendResponse(null, {\n success: false,\n error: error instanceof Error ? error.message : 'Unknown error occurred'\n }, sendMessage);\n }\n}\n\n/**\n * Search components based on prompt keywords\n * Searches in component name, displayName, description, keywords, and category\n * @param prompt - Search prompt\n * @param components - List of components to search in\n * @param limit - Maximum number of results to return\n * @returns Matching components sorted by relevance\n */\nfunction searchComponents(prompt: string, components: Component[], limit: number): Component[] {\n const promptLower = prompt.toLowerCase();\n const promptTokens = promptLower.split(/\\s+/).filter(token => token.length > 0);\n\n // Score each component based on keyword matches\n const scoredComponents = components.map(component => {\n let score = 0;\n\n const componentName = component.name.toLowerCase();\n const componentDisplayName = (component.displayName || '').toLowerCase();\n const componentDesc = component.description.toLowerCase();\n const componentKeywords = (component.keywords || []).map(k => k.toLowerCase());\n const componentCategory = (component.category || '').toLowerCase();\n\n // Search in each field with different weights\n for (const token of promptTokens) {\n // Exact name match (highest weight)\n if (componentName === token) {\n score += 10;\n }\n // Name contains token\n else if (componentName.includes(token)) {\n score += 5;\n }\n\n // DisplayName contains token (high weight - user-friendly name)\n if (componentDisplayName.includes(token)) {\n score += 6;\n }\n\n // Exact keyword match\n if (componentKeywords.includes(token)) {\n score += 8;\n }\n // Keywords contain token\n else if (componentKeywords.some(k => k.includes(token))) {\n score += 4;\n }\n\n // Description contains token\n if (componentDesc.includes(token)) {\n score += 2;\n }\n\n // Category contains token\n if (componentCategory.includes(token)) {\n score += 3;\n }\n }\n\n return { component, score };\n });\n\n // Filter out components with score 0, sort by score (descending), and take top results\n return scoredComponents\n .filter(({ score }) => score > 0)\n .sort((a, b) => b.score - a.score)\n .slice(0, limit)\n .map(({ component }) => component);\n}\n\n/**\n * Send user prompt suggestions response\n */\nfunction sendResponse(\n id: string | null,\n res: { success: boolean; error?: string; data?: any },\n sendMessage: (message: Message) => void,\n clientId?: string,\n): void {\n const response: Message = {\n id: id || 'unknown',\n type: 'USER_PROMPT_SUGGESTIONS_RES',\n from: { type: 'data-agent' },\n to: {\n type: 'runtime',\n id: clientId\n },\n payload: {\n ...res,\n }\n };\n\n sendMessage(response);\n}\n","import { anthropicLLM } from './anthropic';\nimport { groqLLM } from './groq';\nimport { geminiLLM } from './gemini';\nimport { openaiLLM } from './openai';\nimport { LLMProvider, Component } from '../types';\nimport { logger } from '../utils/logger';\n\n/**\n * Generate next questions based on the original user prompt and generated component\n * Routes to the appropriate LLM provider (Anthropic or Groq)\n * Falls back to next provider if current provider fails or returns empty results\n */\nexport async function generateNextQuestions(\n\toriginalUserPrompt: string,\n\tcomponent: Component,\n\tcomponentData?: Record<string, unknown>,\n\tanthropicApiKey?: string,\n\tgroqApiKey?: string,\n\tgeminiApiKey?: string,\n\topenaiApiKey?: string,\n\tllmProviders?: LLMProvider[],\n\tconversationHistory?: string\n): Promise<string[]> {\n\ttry {\n\t\tlogger.debug('[generateNextQuestions] Starting next questions generation');\n\t\tlogger.debug(`[generateNextQuestions] User prompt: \"${originalUserPrompt?.substring(0, 50)}...\"`);\n\t\tlogger.debug(`[generateNextQuestions] Component: ${component?.name || 'unknown'} (${component?.type || 'unknown'})`);\n\t\tlogger.debug(`[generateNextQuestions] Component data available: ${componentData ? 'yes' : 'no'}`);\n\n\t\t// Determine which providers to use\n\t\tconst providers = llmProviders || ['anthropic', 'gemini', 'openai', 'groq'];\n\t\tlogger.info(`[generateNextQuestions] Using LLM providers: [${providers.join(', ')}]`);\n\n\t\t// Log conversation context info\n\t\tif (conversationHistory && conversationHistory.length > 0) {\n\t\t\tconst exchangeCount = conversationHistory.split('\\n').filter((l: string) => l.startsWith('Q')).length;\n\t\t\tlogger.debug(`[generateNextQuestions] Using conversation history with ${exchangeCount} previous exchanges`);\n\t\t} else {\n\t\t\tlogger.debug('[generateNextQuestions] No conversation history available');\n\t\t}\n\n\t\t// Try each provider in order\n\t\tfor (let i = 0; i < providers.length; i++) {\n\t\t\tconst provider = providers[i];\n\t\t\tconst isLastProvider = i === providers.length - 1;\n\n\t\t\ttry {\n\t\t\t\tlogger.info(`[generateNextQuestions] Attempting provider: ${provider} (${i + 1}/${providers.length})`);\n\n\t\t\t\tlet result: string[] = [];\n\n\t\t\t\tif (provider === 'groq') {\n\t\t\t\t\tlogger.debug('[generateNextQuestions] Using Groq LLM for next questions');\n\t\t\t\t\tresult = await groqLLM.generateNextQuestions(\n\t\t\t\t\t\toriginalUserPrompt,\n\t\t\t\t\t\tcomponent,\n\t\t\t\t\t\tcomponentData,\n\t\t\t\t\t\tgroqApiKey,\n\t\t\t\t\t\tconversationHistory\n\t\t\t\t\t);\n\t\t\t\t} else if (provider === 'gemini') {\n\t\t\t\t\tlogger.debug('[generateNextQuestions] Using Gemini LLM for next questions');\n\t\t\t\t\tresult = await geminiLLM.generateNextQuestions(\n\t\t\t\t\t\toriginalUserPrompt,\n\t\t\t\t\t\tcomponent,\n\t\t\t\t\t\tcomponentData,\n\t\t\t\t\t\tgeminiApiKey,\n\t\t\t\t\t\tconversationHistory\n\t\t\t\t\t);\n\t\t\t\t} else if (provider === 'openai') {\n\t\t\t\t\tlogger.debug('[generateNextQuestions] Using OpenAI LLM for next questions');\n\t\t\t\t\tresult = await openaiLLM.generateNextQuestions(\n\t\t\t\t\t\toriginalUserPrompt,\n\t\t\t\t\t\tcomponent,\n\t\t\t\t\t\tcomponentData,\n\t\t\t\t\t\topenaiApiKey,\n\t\t\t\t\t\tconversationHistory\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\t// Default to Anthropic\n\t\t\t\t\tlogger.debug('[generateNextQuestions] Using Anthropic LLM for next questions');\n\t\t\t\t\tresult = await anthropicLLM.generateNextQuestions(\n\t\t\t\t\t\toriginalUserPrompt,\n\t\t\t\t\t\tcomponent,\n\t\t\t\t\t\tcomponentData,\n\t\t\t\t\t\tanthropicApiKey,\n\t\t\t\t\t\tconversationHistory\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\t// If we got results, return them\n\t\t\t\tif (result && result.length > 0) {\n\t\t\t\t\tlogger.info(`[generateNextQuestions] Successfully generated ${result.length} questions with ${provider}`);\n\t\t\t\t\tlogger.debug(`[generateNextQuestions] Questions: ${JSON.stringify(result)}`);\n\t\t\t\t\treturn result;\n\t\t\t\t}\n\n\t\t\t\tlogger.warn(`[generateNextQuestions] No questions generated from ${provider}${!isLastProvider ? ', trying next provider...' : ''}`);\n\t\t\t} catch (providerError) {\n\t\t\t\tconst errorMsg = providerError instanceof Error ? providerError.message : String(providerError);\n\t\t\t\tlogger.error(`[generateNextQuestions] Provider ${provider} failed: ${errorMsg}`);\n\t\t\t\tlogger.debug(`[generateNextQuestions] Provider error details:`, providerError);\n\n\t\t\t\tif (!isLastProvider) {\n\t\t\t\t\tlogger.info(`[generateNextQuestions] Provider ${provider} failed, trying next provider...`);\n\t\t\t\t}\n\t\t\t\t// Continue to next provider\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\n\t\t// All providers failed or returned empty results\n\t\tlogger.warn('[generateNextQuestions] All providers failed or returned no questions');\n\t\treturn [];\n\t} catch (error) {\n\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\t\tconst errorStack = error instanceof Error ? error.stack : undefined;\n\n\t\tlogger.error(`[generateNextQuestions] Error generating next questions: ${errorMsg}`);\n\t\tlogger.debug('[generateNextQuestions] Error stack trace:', errorStack);\n\t\t// Return empty array on error\n\t\treturn [];\n\t}\n}\n","import { ActionsRequestMessageSchema, type LLMProvider, type Message } from '../types';\nimport { generateNextQuestions } from '../userResponse/next-questions';\nimport { logger } from '../utils/logger';\nimport { ThreadManager } from '../threads';\nimport { CONTEXT_CONFIG } from '../config/context';\n\n/**\n * Handle incoming actions messages from runtime\n * Generates suggested next questions based on the original user prompt and generated component\n */\nexport async function handleActionsRequest(\n data: any,\n sendMessage: (message: Message) => void,\n anthropicApiKey?: string,\n groqApiKey?: string,\n geminiApiKey?: string,\n openaiApiKey?: string,\n llmProviders?: LLMProvider[]\n): Promise<void> {\n try {\n logger.debug('[ACTIONS_REQ] Parsing incoming actions request');\n const actionsRequest = ActionsRequestMessageSchema.parse(data);\n const { id, payload } = actionsRequest;\n const { SA_RUNTIME } = payload;\n\n const wsId = actionsRequest.from.id || 'unknown';\n\n logger.info(`[ACTIONS_REQ ${id}] Processing actions request from client: ${wsId}`);\n logger.debug(`[ACTIONS_REQ ${id}] Request payload:`, JSON.stringify(payload, null, 2).substring(0, 200));\n\n // SA_RUNTIME is required to fetch actions from UIBlock\n if (!SA_RUNTIME) {\n logger.error(`[ACTIONS_REQ ${id}] SA_RUNTIME missing from request`);\n sendResponse(id, {\n success: false,\n error: 'SA_RUNTIME with threadId and uiBlockId is required'\n }, sendMessage, wsId);\n return;\n }\n\n const uiBlockId = SA_RUNTIME.uiBlockId;\n const threadId = SA_RUNTIME.threadId;\n\n logger.debug(`[ACTIONS_REQ ${id}] SA_RUNTIME validated - threadId: ${threadId}, uiBlockId: ${uiBlockId}`);\n\n // Get UIBlock from ThreadManager\n const threadManager = ThreadManager.getInstance();\n const thread = threadManager.getThread(threadId);\n\n if (!thread) {\n logger.error(`[ACTIONS_REQ ${id}] Thread '${threadId}' not found`);\n sendResponse(id, {\n success: false,\n error: `Thread '${threadId}' not found`\n }, sendMessage, wsId);\n return;\n }\n\n logger.debug(`[ACTIONS_REQ ${id}] Thread found with ${thread.getUIBlocks().length} UIBlocks`);\n logger.debug(`[ACTIONS_REQ ${id}] Retrieving UIBlock: ${uiBlockId}`);\n\n const uiBlock = thread.getUIBlock(uiBlockId);\n if (!uiBlock) {\n logger.error(`[ACTIONS_REQ ${id}] UIBlock '${uiBlockId}' not found in thread '${threadId}'`);\n sendResponse(id, {\n success: false,\n error: `UIBlock '${uiBlockId}' not found in thread '${threadId}'`\n }, sendMessage, wsId);\n return;\n }\n\n logger.info(`[ACTIONS_REQ ${id}] UIBlock retrieved successfully`);\n\n // Extract data from UIBlock\n logger.debug(`[ACTIONS_REQ ${id}] Extracting data from UIBlock`);\n const userQuestion = uiBlock.getUserQuestion();\n const component = uiBlock.getComponentMetadata();\n const componentData = uiBlock.getComponentData();\n\n logger.debug(`[ACTIONS_REQ ${id}] User question: \"${userQuestion?.substring(0, 50)}...\"`);\n logger.debug(`[ACTIONS_REQ ${id}] Component: ${component?.name || 'unknown'} (${component?.type || 'unknown'})`);\n logger.debug(`[ACTIONS_REQ ${id}] Component data available: ${componentData ? 'yes' : 'no'}`);\n\n // Extract conversation context from thread (excluding current UIBlock)\n logger.debug(`[ACTIONS_REQ ${id}] Extracting conversation history (max ${CONTEXT_CONFIG.MAX_CONVERSATION_CONTEXT_BLOCKS} blocks)`);\n const conversationHistory = thread.getConversationContext(CONTEXT_CONFIG.MAX_CONVERSATION_CONTEXT_BLOCKS, uiBlockId);\n const historyLineCount = conversationHistory.split('\\n').filter(l => l.trim()).length;\n logger.info(`[ACTIONS_REQ ${id}] Conversation history extracted: ${historyLineCount} lines`);\n logger.debug(`[ACTIONS_REQ ${id}] Conversation history preview:\\n${conversationHistory.substring(0, 200)}...`);\n\n logger.info(`[ACTIONS_REQ ${id}] Generating actions for UIBlock: ${uiBlockId}, component: ${component?.name || 'unknown'}`);\n\n // Use getOrFetchActions to manage action state\n logger.debug(`[ACTIONS_REQ ${id}] Checking if actions are already cached`);\n const startTime = Date.now();\n\n const actions = await uiBlock.getOrFetchActions(async () => {\n logger.info(`[ACTIONS_REQ ${id}] Actions not cached, generating new actions...`);\n\n // Generate next questions using extracted data from UIBlock and conversation history\n logger.info(`[ACTIONS_REQ ${id}] Starting next questions generation with ${llmProviders?.join(', ') || 'default'} providers`);\n const nextQuestions = await generateNextQuestions(\n userQuestion,\n component as any,\n componentData,\n anthropicApiKey,\n groqApiKey,\n geminiApiKey,\n openaiApiKey,\n llmProviders,\n conversationHistory\n );\n\n logger.info(`[ACTIONS_REQ ${id}] Generated ${nextQuestions.length} questions`);\n logger.debug(`[ACTIONS_REQ ${id}] Questions: ${JSON.stringify(nextQuestions)}`);\n\n // Convert questions to actions format\n logger.debug(`[ACTIONS_REQ ${id}] Converting questions to actions format`);\n const convertedActions = nextQuestions.map((question: string, index: number) => ({\n id: `action_${index}_${Date.now()}`,\n name: question,\n type: 'next_question',\n question\n }));\n\n logger.debug(`[ACTIONS_REQ ${id}] Converted ${convertedActions.length} actions`);\n return convertedActions;\n });\n\n const processingTime = Date.now() - startTime;\n logger.info(`[ACTIONS_REQ ${id}] Actions retrieved in ${processingTime}ms - ${actions.length} actions total`);\n\n if (actions.length > 0) {\n logger.info(`[ACTIONS_REQ ${id}] Generated ${actions.length} follow-up questions successfully`);\n logger.debug(`[ACTIONS_REQ ${id}] Actions: ${actions.map(a => a.name).join(', ')}`);\n } else {\n logger.warn(`[ACTIONS_REQ ${id}] No actions generated`);\n }\n\n logger.debug(`[ACTIONS_REQ ${id}] Sending successful response to client`);\n sendResponse(id, {\n success: true,\n data: {\n actions,\n componentName: component?.name,\n componentId: component?.id,\n uiBlockId,\n threadId\n }\n }, sendMessage, wsId);\n\n logger.info(`[ACTIONS_REQ ${id}] ✓ Actions request completed successfully`);\n\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';\n const errorStack = error instanceof Error ? error.stack : undefined;\n\n logger.error(`[ACTIONS_REQ] Failed to handle actions request: ${errorMessage}`);\n logger.debug(`[ACTIONS_REQ] Error stack trace:`, errorStack);\n\n sendResponse(null, {\n success: false,\n error: errorMessage\n }, sendMessage);\n\n logger.info('[ACTIONS_REQ] ✗ Actions request completed with errors');\n }\n}\n\n/**\n * Send actions response\n */\nfunction sendResponse(\n id: string | null,\n res: { success: boolean; error?: string; data?: any },\n sendMessage: (message: Message) => void,\n clientId?: string,\n): void {\n const response: Message = {\n id: id || 'unknown',\n type: 'ACTIONS_RES',\n from: { type: 'data-agent' },\n to: {\n type: 'runtime',\n id: clientId\n },\n payload: {\n ...res,\n }\n };\n\n logger.debug(`[ACTIONS_RES ${id || 'unknown'}] Sending ${res.success ? 'successful' : 'failed'} response to client`);\n logger.debug(`[ACTIONS_RES ${id || 'unknown'}] Response payload size: ${JSON.stringify(response).length} bytes`);\n\n if (res.data?.actions) {\n logger.debug(`[ACTIONS_RES ${id || 'unknown'}] Sending ${res.data.actions.length} actions`);\n }\n\n sendMessage(response);\n}\n","import { Component, ComponentListResponseMessageSchema, ComponentSchema, ComponentsSchema, Message, CollectionRegistry } from \"../types\";\nimport { logger } from \"../utils/logger\";\n\n\nexport async function handleComponentListResponse(\n data: any,\n storeComponents:(components: Component[])=>void,\n collections?: CollectionRegistry\n ): Promise<void> {\n try {\n const componentListResponse = ComponentListResponseMessageSchema.parse(data);\n const { payload } = componentListResponse;\n\n const componentsList = payload.components;\n\n if(!componentsList){\n logger.error('Components list not found in the response');\n return;\n }\n\n const components = ComponentsSchema.parse(componentsList);\n storeComponents(components);\n\n // Embed display components for semantic search if collection is available\n const embedHandler = collections?.['components']?.['embed'];\n if (embedHandler) {\n try {\n logger.info('Embedding display components for semantic search...');\n const result = await embedHandler({ components });\n if (result.success) {\n logger.info(`Successfully embedded ${result.count} display components`);\n } else {\n logger.warn('Failed to embed components:', result.error);\n }\n } catch (embedError) {\n logger.warn('Failed to embed components:', embedError);\n }\n }\n\n return;\n } catch (error) {\n logger.error('Failed to handle user prompt request:', error);\n }\n }\n\n\n","import { WorkflowListResponseMessageSchema, WorkflowsSchema } from '../types';\nimport type { WorkflowDescriptor } from '../userResponse/agents';\nimport { logger } from '../utils/logger';\n\n/**\n * Handle a WORKFLOW_LIST_RES message from the web SDK.\n *\n * The frontend sends each registered workflow component's descriptor (id,\n * name, description, whenToUse, propsSchema, defaultProps) so the backend\n * MainAgent can route prompts to them.\n */\nexport async function handleWorkflowListResponse(\n\tdata: any,\n\tstoreWorkflows: (workflows: WorkflowDescriptor[]) => void,\n): Promise<void> {\n\ttry {\n\t\tconst parsed = WorkflowListResponseMessageSchema.parse(data);\n\t\tconst { payload } = parsed;\n\n\t\tconst workflowsList = payload.workflows;\n\t\tif (!workflowsList) {\n\t\t\tlogger.error('Workflows list not found in WORKFLOW_LIST_RES payload');\n\t\t\treturn;\n\t\t}\n\n\t\tconst workflows = WorkflowsSchema.parse(workflowsList) as WorkflowDescriptor[];\n\t\tstoreWorkflows(workflows);\n\t\tlogger.info(`Stored ${workflows.length} workflow descriptor(s) from frontend`);\n\t} catch (error) {\n\t\tlogger.error('Failed to handle workflow list response:', error);\n\t}\n}\n","import { UsersRequestMessageSchema, Message, CollectionRegistry } from '../types';\nimport { getUserManager } from '../auth/user-storage';\nimport { logger } from '../utils/logger';\nimport { userInfo } from 'os';\n\n/**\n * Handle unified users management request\n * Supports operations: create, update, delete, getAll, getOne\n * Only accepts requests from 'admin' type\n *\n * Strategy: Try database collections first, fall back to file-based storage if failed\n */\nexport async function handleUsersRequest(\n data: any,\n collections: CollectionRegistry,\n sendMessage: (message: Message) => void\n): Promise<void> {\n // Helper function to execute collection operations\n const executeCollection = async (collection: string, op: string, params: any): Promise<any> => {\n const handler = collections[collection]?.[op];\n if (!handler) {\n return null; // Collection not found, will fall back to file storage\n }\n return await handler(params);\n };\n\n try {\n const request = UsersRequestMessageSchema.parse(data);\n const { id, payload, from } = request;\n const { operation, data: requestData } = payload;\n const numericId = requestData?.id;\n const username = requestData?.username;\n const email = requestData?.email;\n const password = requestData?.password;\n const fullname = requestData?.fullname;\n const role = requestData?.role;\n const userInfo = requestData?.userInfo;\n // Query operation fields\n const filters = requestData?.filters;\n const limit = requestData?.limit;\n const sort = requestData?.sort;\n\n // Verify request is from admin\n if (from.type !== 'admin' && operation !== 'getOne' && operation !== 'getAll' && operation !== 'query') {\n sendResponse(id, {\n success: false,\n error: 'Unauthorized: Only admin can manage users'\n }, sendMessage, from.id);\n logger.warn(`Unauthorized user management attempt from: ${from.type}`);\n return;\n }\n\n const userManager = getUserManager();\n\n // Route to appropriate operation handler\n switch (operation) {\n case 'create':\n await handleCreate(id, { username, email, password, fullname, role, userInfo }, executeCollection, userManager, sendMessage, from.id);\n break;\n\n case 'update':\n await handleUpdate(id, numericId, { username, email, password, fullname, role, userInfo }, executeCollection, userManager, sendMessage, from.id);\n break;\n\n case 'delete':\n await handleDelete(id, numericId, username, executeCollection, userManager, sendMessage, from.id);\n break;\n\n case 'getAll':\n await handleGetAll(id, executeCollection, userManager, sendMessage, from.id);\n break;\n\n case 'getOne':\n await handleGetOne(id, numericId, username, executeCollection, userManager, sendMessage, from.id);\n break;\n\n case 'query':\n await handleQuery(id, filters, limit, sort, executeCollection, userManager, sendMessage, from.id);\n break;\n\n default:\n sendResponse(id, {\n success: false,\n error: `Unknown operation: ${operation}`\n }, sendMessage, from.id);\n }\n\n } catch (error) {\n logger.error('Failed to handle users request:', error);\n sendResponse(null, {\n success: false,\n error: error instanceof Error ? error.message : 'Unknown error occurred'\n }, sendMessage);\n }\n}\n\n/**\n * Handle create user operation\n * Tries database collection first, falls back to file storage if failed\n */\nasync function handleCreate(\n id: string,\n userData: {\n username?: string;\n email?: string;\n password?: string;\n fullname?: string;\n role?: string;\n userInfo?: Record<string, unknown>;\n },\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n userManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n const { username, email, password, fullname, role, userInfo } = userData;\n\n // Validate input - username is required\n if (!username || username.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Username is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n if (!password || password.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Password is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n // Validate email format if provided\n if (email && email.trim().length > 0) {\n const emailRegex = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/;\n if (!emailRegex.test(email)) {\n sendResponse(id, {\n success: false,\n error: 'Invalid email format'\n }, sendMessage, clientId);\n return;\n }\n }\n\n // Try database collection first\n try {\n const result = await executeCollection('users', 'create', {\n username,\n email: email || undefined,\n password,\n fullname: fullname || undefined,\n role: role || undefined,\n userInfo: userInfo || undefined\n });\n\n if (result && result.success) {\n logger.info(`[DB] User created successfully: ${username}`);\n sendResponse(id, {\n success: true,\n data: {\n id: result.data?.id,\n username: result.data?.username,\n email: result.data?.email,\n fullname: result.data?.fullname,\n role: result.data?.role,\n message: `User '${username}' created successfully (DB)`\n }\n }, sendMessage, clientId);\n return;\n }\n } catch (dbError) {\n logger.warn(`[DB] Failed to create user, falling back to file storage: ${dbError instanceof Error ? dbError.message : 'Unknown error'}`);\n }\n\n // Fallback to file-based storage\n try {\n // Check if user already exists\n if (userManager.userExists(username)) {\n sendResponse(id, {\n success: false,\n error: `User '${username}' already exists`\n }, sendMessage, clientId);\n return;\n }\n\n // Check if email already exists (if provided)\n if (email && userManager.getUserByEmail(email)) {\n sendResponse(id, {\n success: false,\n error: `User with email '${email}' already exists`\n }, sendMessage, clientId);\n return;\n }\n\n // Create user object\n const newUserData: any = {\n username,\n password,\n };\n\n if (email && email.trim().length > 0) {\n newUserData.email = email.trim();\n }\n\n if (fullname && fullname.trim().length > 0) {\n newUserData.fullname = fullname.trim();\n }\n\n if (role && role.trim().length > 0) {\n newUserData.role = role.trim();\n }\n\n // Create user\n const newUser = userManager.createUser(newUserData);\n\n logger.info(`[FILE] User '${username}' created successfully`);\n sendResponse(id, {\n success: true,\n data: {\n username: newUser.username,\n email: newUser.email,\n fullname: newUser.fullname,\n role: newUser.role,\n message: `User '${username}' created successfully (File)`\n }\n }, sendMessage, clientId);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to create user'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle update user operation\n * Tries database collection first, falls back to file storage if failed\n */\nasync function handleUpdate(\n id: string,\n numericId: number | undefined,\n userData: {\n username?: string;\n email?: string;\n password?: string;\n fullname?: string;\n role?: string;\n userInfo?: Record<string, unknown>;\n },\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n userManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n const { username, email, password, fullname, role, userInfo } = userData;\n\n // For DB update, we need numeric ID; for file storage, we need username\n if (!numericId && !username) {\n sendResponse(id, {\n success: false,\n error: 'User ID or username is required'\n }, sendMessage, clientId);\n return;\n }\n\n // Try database collection first (if numeric ID is provided)\n if (numericId) {\n try {\n const result = await executeCollection('users', 'update', {\n id: numericId,\n username,\n email,\n password,\n fullname,\n role,\n userInfo\n });\n\n if (result && result.success) {\n logger.info(`[DB] User updated successfully, ID: ${numericId}`);\n sendResponse(id, {\n success: true,\n data: {\n id: result.data?.id,\n username: result.data?.username,\n email: result.data?.email,\n fullname: result.data?.fullname,\n role: result.data?.role,\n message: `User updated successfully (DB)`\n }\n }, sendMessage, clientId);\n return;\n }\n } catch (dbError) {\n logger.warn(`[DB] Failed to update user, falling back to file storage: ${dbError instanceof Error ? dbError.message : 'Unknown error'}`);\n }\n }\n\n // Fallback to file-based storage (requires username)\n if (!username || username.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Username is required for file-based storage update'\n }, sendMessage, clientId);\n return;\n }\n\n // Check if user exists\n if (!userManager.userExists(username)) {\n sendResponse(id, {\n success: false,\n error: `User '${username}' not found`\n }, sendMessage, clientId);\n return;\n }\n\n // Build update object\n const updates: any = {};\n\n // Validate and add email if provided\n if (email !== undefined) {\n if (email.trim().length > 0) {\n const emailRegex = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/;\n if (!emailRegex.test(email)) {\n sendResponse(id, {\n success: false,\n error: 'Invalid email format'\n }, sendMessage, clientId);\n return;\n }\n\n // Check if email is already used by another user\n const existingUser = userManager.getUserByEmail(email);\n if (existingUser && existingUser.username !== username) {\n sendResponse(id, {\n success: false,\n error: `Email '${email}' is already used by another user`\n }, sendMessage, clientId);\n return;\n }\n\n updates.email = email.trim();\n } else {\n updates.email = undefined;\n }\n }\n\n if (password !== undefined && password.trim().length > 0) {\n updates.password = password.trim();\n }\n\n if (fullname !== undefined) {\n updates.fullname = fullname.trim().length > 0 ? fullname.trim() : undefined;\n }\n\n if (role !== undefined) {\n updates.role = role.trim().length > 0 ? role.trim() : undefined;\n }\n\n if (Object.keys(updates).length === 0) {\n sendResponse(id, {\n success: false,\n error: 'No fields to update. Please provide at least one field to update.'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const updatedUser = userManager.updateUser(username, updates);\n\n logger.info(`[FILE] User '${username}' updated successfully`);\n sendResponse(id, {\n success: true,\n data: {\n username: updatedUser.username,\n email: updatedUser.email,\n fullname: updatedUser.fullname,\n role: updatedUser.role,\n message: `User '${username}' updated successfully (File)`\n }\n }, sendMessage, clientId);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to update user'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle delete user operation\n * Tries database collection first, falls back to file storage if failed\n */\nasync function handleDelete(\n id: string,\n numericId: number | undefined,\n username: string | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n userManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // For DB delete, we need numeric ID; for file storage, we need username\n if (!numericId && !username) {\n sendResponse(id, {\n success: false,\n error: 'User ID or username is required'\n }, sendMessage, clientId);\n return;\n }\n\n // Try database collection first (if numeric ID is provided)\n if (numericId) {\n try {\n const result = await executeCollection('users', 'delete', { id: numericId });\n\n if (result && result.success) {\n logger.info(`[DB] User deleted successfully, ID: ${numericId}`);\n sendResponse(id, {\n success: true,\n data: {\n id: numericId,\n username: result.data?.username,\n message: `User deleted successfully (DB)`\n }\n }, sendMessage, clientId);\n return;\n }\n } catch (dbError) {\n logger.warn(`[DB] Failed to delete user, falling back to file storage: ${dbError instanceof Error ? dbError.message : 'Unknown error'}`);\n }\n }\n\n // Fallback to file-based storage (requires username)\n if (!username || username.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Username is required for file-based storage delete'\n }, sendMessage, clientId);\n return;\n }\n\n // Check if user exists\n if (!userManager.userExists(username)) {\n sendResponse(id, {\n success: false,\n error: `User '${username}' not found`\n }, sendMessage, clientId);\n return;\n }\n\n const deleted = userManager.deleteUser(username);\n\n if (!deleted) {\n sendResponse(id, {\n success: false,\n error: `Failed to delete user '${username}'`\n }, sendMessage, clientId);\n return;\n }\n\n logger.info(`[FILE] User '${username}' deleted successfully`);\n sendResponse(id, {\n success: true,\n data: {\n username: username,\n message: `User '${username}' deleted successfully (File)`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Handle get all users operation\n * Tries database collection first, falls back to file storage if failed\n */\nasync function handleGetAll(\n id: string,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n userManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Try database collection first\n try {\n const result = await executeCollection('users', 'getAll', {});\n\n if (result && result.success) {\n // Remove sensitive information like passwords\n const sanitizedUsers = result.data.map((user: any) => ({\n id: user.id,\n username: user.username,\n email: user.email,\n fullname: user.fullname,\n role: user.role,\n userInfo : user.userInfo,\n createdAt: user.createdAt,\n updatedAt: user.updatedAt\n }));\n\n logger.info(`[DB] Retrieved ${result.count} users`);\n sendResponse(id, {\n success: true,\n data: {\n users: sanitizedUsers,\n count: result.count,\n message: `Retrieved ${result.count} users (DB)`\n }\n }, sendMessage, clientId);\n return;\n }\n } catch (dbError) {\n logger.warn(`[DB] Failed to get all users, falling back to file storage: ${dbError instanceof Error ? dbError.message : 'Unknown error'}`);\n }\n\n // Fallback to file-based storage\n const users = userManager.getAllUsers();\n\n // Remove sensitive information like passwords\n const sanitizedUsers = users.map((user: any) => ({\n username: user.username,\n email: user.email,\n fullname: user.fullname,\n role: user.role,\n wsIds: user.wsIds || []\n }));\n\n logger.info(`[FILE] Retrieved ${sanitizedUsers.length} users`);\n sendResponse(id, {\n success: true,\n data: {\n users: sanitizedUsers,\n count: sanitizedUsers.length,\n message: `Retrieved ${sanitizedUsers.length} users (File)`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Handle get one user operation\n * Tries database collection first, falls back to file storage if failed\n */\nasync function handleGetOne(\n id: string,\n numericId: number | undefined,\n username: string | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n userManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // For DB getOne, we need numeric ID; for file storage, we need username\n if (!numericId && !username) {\n sendResponse(id, {\n success: false,\n error: 'User ID or username is required'\n }, sendMessage, clientId);\n return;\n }\n\n // Try database collection first (if numeric ID is provided)\n if (numericId) {\n try {\n const result = await executeCollection('users', 'getOne', { id: numericId });\n\n if (result && result.success) {\n // Remove sensitive information\n const sanitizedUser = {\n id: result.data?.id,\n username: result.data?.username,\n email: result.data?.email,\n fullname: result.data?.fullname,\n role: result.data?.role,\n userInfo: result.data?.userInfo,\n createdAt: result.data?.createdAt,\n updatedAt: result.data?.updatedAt\n };\n\n logger.info(`[DB] Retrieved user ID: ${numericId}`);\n sendResponse(id, {\n success: true,\n data: {\n user: sanitizedUser,\n message: `Retrieved user (DB)`\n }\n }, sendMessage, clientId);\n return;\n }\n } catch (dbError) {\n logger.warn(`[DB] Failed to get user, falling back to file storage: ${dbError instanceof Error ? dbError.message : 'Unknown error'}`);\n }\n }\n\n // Fallback to file-based storage (requires username)\n if (!username || username.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Username is required for file-based storage lookup'\n }, sendMessage, clientId);\n return;\n }\n\n // Check if user exists\n if (!userManager.userExists(username)) {\n sendResponse(id, {\n success: false,\n error: `User '${username}' not found`\n }, sendMessage, clientId);\n return;\n }\n\n const user = userManager.getUser(username);\n\n // Remove sensitive information\n const sanitizedUser = {\n username: user.username,\n email: user.email,\n fullname: user.fullname,\n role: user.role,\n wsIds: user.wsIds || []\n };\n\n logger.info(`[FILE] Retrieved user: ${username}`);\n sendResponse(id, {\n success: true,\n data: {\n user: sanitizedUser,\n message: `Retrieved user '${username}' (File)`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Handle query users operation with filters\n * Database only - no file fallback for query operations\n */\nasync function handleQuery(\n id: string,\n filters: any,\n limit: number | undefined,\n sort: string | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n userManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Try database collection\n try {\n const result = await executeCollection('users', 'query', {\n filters: filters || {},\n limit,\n sort\n });\n\n if (result && result.success) {\n // Remove sensitive information like passwords\n const sanitizedUsers = result.data.map((user: any) => ({\n id: user.id,\n username: user.username,\n email: user.email,\n fullname: user.fullname,\n role: user.role,\n userInfo: user.userInfo,\n createdAt: user.createdAt,\n updatedAt: user.updatedAt\n }));\n\n logger.info(`[DB] Query returned ${result.count} users`);\n sendResponse(id, {\n success: true,\n data: {\n users: sanitizedUsers,\n count: result.count,\n message: `Query returned ${result.count} users (DB)`\n }\n }, sendMessage, clientId);\n return;\n }\n } catch (dbError) {\n logger.warn(`[DB] Failed to query users: ${dbError instanceof Error ? dbError.message : 'Unknown error'}`);\n }\n\n // Fallback to file-based storage (get all and return - no filter support)\n const users = userManager.getAllUsers();\n\n // Remove sensitive information like passwords\n const sanitizedUsers = users.map((user: any) => ({\n username: user.username,\n email: user.email,\n fullname: user.fullname,\n role: user.role,\n userInfo : user.userInfo,\n wsIds: user.wsIds || []\n }));\n\n logger.info(`[FILE] Retrieved ${sanitizedUsers.length} users (all - no query filter)`);\n sendResponse(id, {\n success: true,\n data: {\n users: sanitizedUsers,\n count: sanitizedUsers.length,\n message: `Retrieved ${sanitizedUsers.length} users (File - no query filter)`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Send users response\n */\nfunction sendResponse(\n id: string | null,\n res: { success: boolean; error?: string; data?: any },\n sendMessage: (message: Message) => void,\n clientId?: string,\n): void {\n const response: Message = {\n id: id || 'unknown',\n type: 'USERS_RES',\n from: { type: 'data-agent' },\n to: {\n type: 'admin',\n id: clientId\n },\n payload: {\n ...res,\n }\n };\n\n sendMessage(response);\n}\n","import { DashboardManager } from './dashboard-manager';\nimport { logger } from '../utils/logger';\n\nlet dashboardManager: DashboardManager | null = null;\n\n/**\n * Set the dashboard manager instance\n * @param manager - DashboardManager instance to use\n */\nexport function setDashboardManager(manager: DashboardManager): void {\n dashboardManager = manager;\n}\n\n/**\n * Get the dashboard manager instance\n * @throws Error if dashboard manager is not initialized\n * @returns DashboardManager instance\n */\nexport function getDashboardManager(): DashboardManager {\n if (!dashboardManager) {\n throw new Error('DashboardManager not initialized. Call setDashboardManager first.');\n }\n return dashboardManager;\n}\n","import { DashboardsRequestMessageSchema, Message, CollectionRegistry } from '../types';\nimport { getDashboardManager } from '../dashboards/dashboard-storage';\nimport { logger } from '../utils/logger';\n\n/**\n * Handle unified dashboards management request\n * Supports operations: create, update, delete, getAll, getOne\n * Only accepts requests from 'admin' type\n *\n * Strategy: Try database collections first, fall back to file-based storage if failed\n */\nexport async function handleDashboardsRequest(\n data: any,\n collections: CollectionRegistry,\n sendMessage: (message: Message) => void\n): Promise<void> {\n // Helper function to execute collection operations\n const executeCollection = async (collection: string, op: string, params: any): Promise<any> => {\n const handler = collections[collection]?.[op];\n if (!handler) {\n return null; // Collection not found, will fall back to file storage\n }\n return await handler(params);\n };\n\n try {\n const request = DashboardsRequestMessageSchema.parse(data);\n const { id, payload, from } = request;\n const { operation, data: requestData } = payload;\n const dashboardId = requestData?.dashboardId;\n const dashboard = requestData?.dashboard;\n const projectId = requestData?.projectId;\n const name = requestData?.name;\n const description = requestData?.description;\n const published = requestData?.published;\n const createdBy = requestData?.createdBy;\n const updatedBy = requestData?.updatedBy;\n const allowedUsers = requestData?.allowedUsers as string[] | undefined;\n const numericId = requestData?.id;\n // Query operation fields\n const filters = requestData?.filters;\n const limit = requestData?.limit;\n const sort = requestData?.sort;\n\n // Read-only operations allowed for all client types (runtime, admin, etc.)\n const readOnlyOperations = ['query', 'getAll', 'getOne'];\n\n // Write operations require admin\n if (!readOnlyOperations.includes(operation) && from.type !== 'admin') {\n sendResponse(id, {\n success: false,\n error: 'Unauthorized: Only admin can manage dashboards'\n }, sendMessage, from.id);\n logger.warn(`Unauthorized dashboard write attempt from: ${from.type}`);\n return;\n }\n\n const dashboardManager = getDashboardManager();\n\n // Route to appropriate operation handler\n switch (operation) {\n case 'create':\n await handleCreate(id, dashboardId, dashboard, projectId, name, description, published, createdBy, allowedUsers, executeCollection, dashboardManager, sendMessage, from.id);\n break;\n\n case 'update':\n await handleUpdate(id, numericId, dashboardId, dashboard, name, description, published, updatedBy, allowedUsers, executeCollection, dashboardManager, sendMessage, from.id);\n break;\n\n case 'delete':\n await handleDelete(id, numericId, dashboardId, executeCollection, dashboardManager, sendMessage, from.id);\n break;\n\n case 'getAll':\n await handleGetAll(id, executeCollection, dashboardManager, sendMessage, from.id);\n break;\n\n case 'getOne':\n await handleGetOne(id, numericId, dashboardId, executeCollection, dashboardManager, sendMessage, from.id);\n break;\n\n case 'query':\n await handleQuery(id, filters, limit, sort, executeCollection, dashboardManager, sendMessage, from.id);\n break;\n\n default:\n sendResponse(id, {\n success: false,\n error: `Unknown operation: ${operation}`\n }, sendMessage, from.id);\n }\n\n } catch (error) {\n logger.error('Failed to handle dashboards request:', error);\n sendResponse(null, {\n success: false,\n error: error instanceof Error ? error.message : 'Unknown error occurred'\n }, sendMessage);\n }\n}\n\n/**\n * Handle create dashboard operation\n * Tries database collection first, falls back to file storage if failed\n */\nasync function handleCreate(\n id: string,\n dashboardId: string | undefined,\n dashboard: any,\n projectId: string | undefined,\n name: string | undefined,\n description: string | undefined,\n published: boolean | undefined,\n createdBy: string | undefined,\n allowedUsers: string[] | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n dashboardManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!dashboardId || dashboardId.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Dashboard ID is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n if (!projectId) {\n sendResponse(id, {\n success: false,\n error: 'Project ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n // Try database collection first\n try {\n const result = await executeCollection('dashboards', 'create', {\n dashboardId,\n projectId,\n name: name || '',\n description,\n dashboard,\n published,\n createdBy,\n allowedUsers\n });\n\n if (result && result.success) {\n logger.info(`[DB] Dashboard created successfully, ID: ${result.data?.id}`);\n sendResponse(id, {\n success: true,\n data: {\n id: result.data?.id,\n dashboardId: result.data?.dashboardId,\n dashboard: result.data?.dashboard || dashboard,\n message: `Dashboard created successfully (DB)`\n }\n }, sendMessage, clientId);\n return;\n }\n } catch (dbError) {\n logger.warn(`[DB] Failed to create dashboard, falling back to file storage: ${dbError instanceof Error ? dbError.message : 'Unknown error'}`);\n }\n\n // Fallback to file-based storage\n try {\n if (!dashboard) {\n dashboard = {};\n }\n\n const createdDashboard = dashboardManager.createDashboard(dashboardId, dashboard);\n\n logger.info(`[FILE] Dashboard '${dashboardId}' created successfully`);\n sendResponse(id, {\n success: true,\n data: {\n dashboardId,\n dashboard: createdDashboard,\n message: `Dashboard '${dashboardId}' created successfully (File)`\n }\n }, sendMessage, clientId);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to create dashboard'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle update dashboard operation\n * Tries database collection first, falls back to file storage if failed\n */\nasync function handleUpdate(\n id: string,\n numericId: number | undefined,\n dashboardId: string | undefined,\n dashboard: any,\n name: string | undefined,\n description: string | undefined,\n published: boolean | undefined,\n updatedBy: string | undefined,\n allowedUsers: string[] | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n dashboardManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!numericId) {\n sendResponse(id, {\n success: false,\n error: 'Dashboard ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n // Try database collection first\n try {\n const result = await executeCollection('dashboards', 'update', {\n id: numericId,\n name,\n description,\n dashboard,\n published,\n updatedBy,\n allowedUsers\n });\n\n if (result && result.success) {\n logger.info(`[DB] Dashboard updated successfully, ID: ${numericId}`);\n sendResponse(id, {\n success: true,\n data: {\n id: numericId,\n dashboardId: result.data?.dashboardId,\n dashboard: result.data?.dashboard || dashboard,\n message: `Dashboard updated successfully (DB)`\n }\n }, sendMessage, clientId);\n return;\n }\n } catch (dbError) {\n logger.warn(`[DB] Failed to update dashboard, falling back to file storage: ${dbError instanceof Error ? dbError.message : 'Unknown error'}`);\n }\n\n // Fallback to file-based storage - use dashboardId if exists, otherwise stringify numericId\n const fileStorageId = dashboardId || String(numericId);\n try {\n const updatedDashboard = dashboardManager.updateDashboard(fileStorageId, dashboard);\n\n if (!updatedDashboard) {\n sendResponse(id, {\n success: false,\n error: `Dashboard '${fileStorageId}' not found`\n }, sendMessage, clientId);\n return;\n }\n\n logger.info(`[FILE] Dashboard '${fileStorageId}' updated successfully`);\n sendResponse(id, {\n success: true,\n data: {\n id: numericId,\n dashboardId: fileStorageId,\n dashboard: updatedDashboard,\n message: `Dashboard '${fileStorageId}' updated successfully (File)`\n }\n }, sendMessage, clientId);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to update dashboard'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle delete dashboard operation\n * Tries database collection first, falls back to file storage if failed\n */\nasync function handleDelete(\n id: string,\n numericId: number | undefined,\n dashboardId: string | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n dashboardManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!numericId) {\n sendResponse(id, {\n success: false,\n error: 'Dashboard ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n // Try database collection first\n try {\n const result = await executeCollection('dashboards', 'delete', { id: numericId });\n\n if (result && result.success) {\n logger.info(`[DB] Dashboard deleted successfully, ID: ${numericId}`);\n sendResponse(id, {\n success: true,\n data: {\n id: numericId,\n message: `Dashboard deleted successfully (DB)`\n }\n }, sendMessage, clientId);\n return;\n }\n } catch (dbError) {\n logger.warn(`[DB] Failed to delete dashboard, falling back to file storage: ${dbError instanceof Error ? dbError.message : 'Unknown error'}`);\n }\n\n // Fallback to file-based storage - use dashboardId if exists, otherwise stringify numericId\n const fileStorageId = dashboardId || String(numericId);\n const deleted = dashboardManager.deleteDashboard(fileStorageId);\n\n if (!deleted) {\n sendResponse(id, {\n success: false,\n error: `Dashboard '${fileStorageId}' not found`\n }, sendMessage, clientId);\n return;\n }\n\n logger.info(`[FILE] Dashboard '${fileStorageId}' deleted successfully`);\n sendResponse(id, {\n success: true,\n data: {\n id: numericId,\n dashboardId: fileStorageId,\n message: `Dashboard '${fileStorageId}' deleted successfully (File)`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Handle get all dashboards operation\n * Tries database collection first, falls back to file storage if failed\n */\nasync function handleGetAll(\n id: string,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n dashboardManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Try database collection first\n try {\n const result = await executeCollection('dashboards', 'getAll', {});\n\n if (result && result.success) {\n logger.info(`[DB] Retrieved ${result.count} dashboards`);\n sendResponse(id, {\n success: true,\n data: {\n dashboards: result.data,\n count: result.count,\n message: `Retrieved ${result.count} dashboards (DB)`\n }\n }, sendMessage, clientId);\n return;\n }\n } catch (dbError) {\n logger.warn(`[DB] Failed to get all dashboards, falling back to file storage: ${dbError instanceof Error ? dbError.message : 'Unknown error'}`);\n }\n\n // Fallback to file-based storage\n const dashboards = dashboardManager.getAllDashboards();\n\n logger.info(`[FILE] Retrieved ${dashboards.length} dashboards`);\n sendResponse(id, {\n success: true,\n data: {\n dashboards,\n count: dashboards.length,\n message: `Retrieved ${dashboards.length} dashboards (File)`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Handle get one dashboard operation\n * Tries database collection first, falls back to file storage if failed\n */\nasync function handleGetOne(\n id: string,\n numericId: number | undefined,\n dashboardId: string | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n dashboardManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!numericId) {\n sendResponse(id, {\n success: false,\n error: 'Dashboard ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n // Try database collection first\n try {\n const result = await executeCollection('dashboards', 'getOne', { id: numericId });\n\n if (result && result.success) {\n logger.info(`[DB] Retrieved dashboard ID: ${numericId}`);\n sendResponse(id, {\n success: true,\n data: {\n id: numericId,\n dashboardId: result.data?.dashboardId,\n dashboard: result.data?.dashboard || result.data,\n message: `Retrieved dashboard (DB)`\n }\n }, sendMessage, clientId);\n return;\n }\n } catch (dbError) {\n logger.warn(`[DB] Failed to get dashboard, falling back to file storage: ${dbError instanceof Error ? dbError.message : 'Unknown error'}`);\n }\n\n // Fallback to file-based storage - use dashboardId if exists, otherwise stringify numericId\n const fileStorageId = dashboardId || String(numericId);\n const dashboard = dashboardManager.getDashboard(fileStorageId);\n\n if (!dashboard) {\n sendResponse(id, {\n success: false,\n error: `Dashboard '${fileStorageId}' not found`\n }, sendMessage, clientId);\n return;\n }\n\n logger.info(`[FILE] Retrieved dashboard: ${fileStorageId}`);\n sendResponse(id, {\n success: true,\n data: {\n id: numericId,\n dashboardId: fileStorageId,\n dashboard,\n message: `Retrieved dashboard '${fileStorageId}' (File)`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Handle query dashboards operation with filters\n * Database only - no file fallback for query operations\n */\nasync function handleQuery(\n id: string,\n filters: any,\n limit: number | undefined,\n sort: string | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n dashboardManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Try database collection\n try {\n const result = await executeCollection('dashboards', 'query', {\n filters: filters || {},\n limit,\n sort\n });\n\n if (result && result.success) {\n logger.info(`[DB] Query returned ${result.count} dashboards`);\n sendResponse(id, {\n success: true,\n data: {\n dashboards: result.data,\n count: result.count,\n message: `Query returned ${result.count} dashboards (DB)`\n }\n }, sendMessage, clientId);\n return;\n }\n } catch (dbError) {\n logger.warn(`[DB] Failed to query dashboards: ${dbError instanceof Error ? dbError.message : 'Unknown error'}`);\n }\n\n // Fallback to file-based storage (get all and return)\n const dashboards = dashboardManager.getAllDashboards();\n\n logger.info(`[FILE] Retrieved ${dashboards.length} dashboards (all - no query filter)`);\n sendResponse(id, {\n success: true,\n data: {\n dashboards,\n count: dashboards.length,\n message: `Retrieved ${dashboards.length} dashboards (File - no query filter)`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Send dashboards response\n */\nfunction sendResponse(\n id: string | null,\n res: { success: boolean; error?: string; data?: any },\n sendMessage: (message: Message) => void,\n clientId?: string,\n): void {\n const response: Message = {\n id: id || 'unknown',\n type: 'DASHBOARDS_RES',\n from: { type: 'data-agent' },\n to: {\n type: 'admin',\n id: clientId\n },\n payload: {\n ...res,\n }\n };\n\n sendMessage(response);\n}\n","import { ReportManager } from './report-manager';\n\n/**\n * Global report manager instance\n */\nlet reportManager: ReportManager | null = null;\n\n/**\n * Get the global report manager instance\n * @returns ReportManager instance\n * @throws Error if report manager is not initialized\n */\nexport function getReportManager(): ReportManager {\n if (!reportManager) {\n throw new Error('Report manager not initialized. Call setReportManager first.');\n }\n return reportManager;\n}\n\n/**\n * Set the global report manager instance\n * @param manager - ReportManager instance to set\n */\nexport function setReportManager(manager: ReportManager): void {\n reportManager = manager;\n}\n","import { ReportsRequestMessageSchema, Message, CollectionRegistry } from '../types';\nimport { getReportManager } from '../reports/report-storage';\nimport { logger } from '../utils/logger';\n\n/**\n * Handle unified reports management request\n * Supports operations: create, update, delete, getAll, getOne, query\n * Only accepts requests from 'admin' type\n *\n * Strategy: Try database collections first, fall back to file-based storage if failed\n */\nexport async function handleReportsRequest(\n data: any,\n collections: CollectionRegistry,\n sendMessage: (message: Message) => void\n): Promise<void> {\n // Helper function to execute collection operations\n const executeCollection = async (collection: string, op: string, params: any): Promise<any> => {\n const handler = collections[collection]?.[op];\n if (!handler) {\n return null; // Collection not found, will fall back to file storage\n }\n return await handler(params);\n };\n\n try {\n const request = ReportsRequestMessageSchema.parse(data);\n const { id, payload, from } = request;\n const { operation, data: requestData } = payload;\n const reportId = requestData?.reportId;\n const report = requestData?.report;\n const projectId = requestData?.projectId;\n const name = requestData?.name;\n const description = requestData?.description;\n const published = requestData?.published;\n const createdBy = requestData?.createdBy;\n const updatedBy = requestData?.updatedBy;\n const numericId = requestData?.id;\n // Query operation fields\n const filters = requestData?.filters;\n const limit = requestData?.limit;\n const sort = requestData?.sort;\n\n // Read-only operations allowed for all client types (runtime, admin, etc.)\n const readOnlyOperations = ['query', 'getAll', 'getOne'];\n\n // Write operations require admin\n if (!readOnlyOperations.includes(operation) && from.type !== 'admin') {\n sendResponse(id, {\n success: false,\n error: 'Unauthorized: Only admin can manage reports'\n }, sendMessage, from.id);\n logger.warn(`Unauthorized report write attempt from: ${from.type}`);\n return;\n }\n\n const reportManager = getReportManager();\n\n // Route to appropriate operation handler\n switch (operation) {\n case 'create':\n await handleCreate(id, reportId, report, projectId, name, description, published, createdBy, executeCollection, reportManager, sendMessage, from.id);\n break;\n\n case 'update':\n await handleUpdate(id, numericId, reportId, report, name, description, published, updatedBy, executeCollection, reportManager, sendMessage, from.id);\n break;\n\n case 'delete':\n await handleDelete(id, numericId, reportId, executeCollection, reportManager, sendMessage, from.id);\n break;\n\n case 'getAll':\n await handleGetAll(id, executeCollection, reportManager, sendMessage, from.id);\n break;\n\n case 'getOne':\n await handleGetOne(id, numericId, reportId, executeCollection, reportManager, sendMessage, from.id);\n break;\n\n case 'query':\n await handleQuery(id, filters, limit, sort, executeCollection, reportManager, sendMessage, from.id);\n break;\n\n default:\n sendResponse(id, {\n success: false,\n error: `Unknown operation: ${operation}`\n }, sendMessage, from.id);\n }\n\n } catch (error) {\n logger.error('Failed to handle reports request:', error);\n sendResponse(null, {\n success: false,\n error: error instanceof Error ? error.message : 'Unknown error occurred'\n }, sendMessage);\n }\n}\n\n/**\n * Handle create report operation\n * Tries database collection first, falls back to file storage if failed\n */\nasync function handleCreate(\n id: string,\n reportId: string | undefined,\n report: any,\n projectId: string | undefined,\n name: string | undefined,\n description: string | undefined,\n published: boolean | undefined,\n createdBy: string | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n reportManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!reportId || reportId.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Report ID is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n if (!projectId) {\n sendResponse(id, {\n success: false,\n error: 'Project ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n // Try database collection first\n try {\n const result = await executeCollection('reports', 'create', {\n reportId,\n projectId,\n name: name || '',\n description,\n report,\n published,\n createdBy\n });\n\n if (result && result.success) {\n logger.info(`[DB] Report created successfully, ID: ${result.data?.id}`);\n sendResponse(id, {\n success: true,\n data: {\n id: result.data?.id,\n reportId: result.data?.reportId,\n report: result.data?.report || report,\n message: `Report created successfully (DB)`\n }\n }, sendMessage, clientId);\n return;\n }\n } catch (dbError) {\n logger.warn(`[DB] Failed to create report, falling back to file storage: ${dbError instanceof Error ? dbError.message : 'Unknown error'}`);\n }\n\n // Fallback to file-based storage\n try {\n if (!report) {\n report = {};\n }\n\n const createdReport = reportManager.createReport(reportId, report);\n\n logger.info(`[FILE] Report '${reportId}' created successfully`);\n sendResponse(id, {\n success: true,\n data: {\n reportId,\n report: createdReport,\n message: `Report '${reportId}' created successfully (File)`\n }\n }, sendMessage, clientId);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to create report'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle update report operation\n * Tries database collection first, falls back to file storage if failed\n */\nasync function handleUpdate(\n id: string,\n numericId: number | undefined,\n reportId: string | undefined,\n report: any,\n name: string | undefined,\n description: string | undefined,\n published: boolean | undefined,\n updatedBy: string | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n reportManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!numericId) {\n sendResponse(id, {\n success: false,\n error: 'Report ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n // Try database collection first\n try {\n const result = await executeCollection('reports', 'update', {\n id: numericId,\n name,\n description,\n report,\n published,\n updatedBy\n });\n\n if (result && result.success) {\n logger.info(`[DB] Report updated successfully, ID: ${numericId}`);\n sendResponse(id, {\n success: true,\n data: {\n id: numericId,\n reportId: result.data?.reportId,\n report: result.data?.report || report,\n message: `Report updated successfully (DB)`\n }\n }, sendMessage, clientId);\n return;\n }\n } catch (dbError) {\n logger.warn(`[DB] Failed to update report, falling back to file storage: ${dbError instanceof Error ? dbError.message : 'Unknown error'}`);\n }\n\n // Fallback to file-based storage - use reportId if exists, otherwise stringify numericId\n const fileStorageId = reportId || String(numericId);\n try {\n const updatedReport = reportManager.updateReport(fileStorageId, report);\n\n if (!updatedReport) {\n sendResponse(id, {\n success: false,\n error: `Report '${fileStorageId}' not found`\n }, sendMessage, clientId);\n return;\n }\n\n logger.info(`[FILE] Report '${fileStorageId}' updated successfully`);\n sendResponse(id, {\n success: true,\n data: {\n id: numericId,\n reportId: fileStorageId,\n report: updatedReport,\n message: `Report '${fileStorageId}' updated successfully (File)`\n }\n }, sendMessage, clientId);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to update report'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle delete report operation\n * Tries database collection first, falls back to file storage if failed\n */\nasync function handleDelete(\n id: string,\n numericId: number | undefined,\n reportId: string | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n reportManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!numericId) {\n sendResponse(id, {\n success: false,\n error: 'Report ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n // Try database collection first\n try {\n const result = await executeCollection('reports', 'delete', { id: numericId });\n\n if (result && result.success) {\n logger.info(`[DB] Report deleted successfully, ID: ${numericId}`);\n sendResponse(id, {\n success: true,\n data: {\n id: numericId,\n message: `Report deleted successfully (DB)`\n }\n }, sendMessage, clientId);\n return;\n }\n } catch (dbError) {\n logger.warn(`[DB] Failed to delete report, falling back to file storage: ${dbError instanceof Error ? dbError.message : 'Unknown error'}`);\n }\n\n // Fallback to file-based storage - use reportId if exists, otherwise stringify numericId\n const fileStorageId = reportId || String(numericId);\n const deleted = reportManager.deleteReport(fileStorageId);\n\n if (!deleted) {\n sendResponse(id, {\n success: false,\n error: `Report '${fileStorageId}' not found`\n }, sendMessage, clientId);\n return;\n }\n\n logger.info(`[FILE] Report '${fileStorageId}' deleted successfully`);\n sendResponse(id, {\n success: true,\n data: {\n id: numericId,\n reportId: fileStorageId,\n message: `Report '${fileStorageId}' deleted successfully (File)`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Handle get all reports operation\n * Tries database collection first, falls back to file storage if failed\n */\nasync function handleGetAll(\n id: string,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n reportManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Try database collection first\n try {\n const result = await executeCollection('reports', 'getAll', {});\n\n if (result && result.success) {\n logger.info(`[DB] Retrieved ${result.count} reports`);\n sendResponse(id, {\n success: true,\n data: {\n reports: result.data,\n count: result.count,\n message: `Retrieved ${result.count} reports (DB)`\n }\n }, sendMessage, clientId);\n return;\n }\n } catch (dbError) {\n logger.warn(`[DB] Failed to get all reports, falling back to file storage: ${dbError instanceof Error ? dbError.message : 'Unknown error'}`);\n }\n\n // Fallback to file-based storage\n const reports = reportManager.getAllReports();\n\n logger.info(`[FILE] Retrieved ${reports.length} reports`);\n sendResponse(id, {\n success: true,\n data: {\n reports,\n count: reports.length,\n message: `Retrieved ${reports.length} reports (File)`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Handle get one report operation\n * Tries database collection first, falls back to file storage if failed\n */\nasync function handleGetOne(\n id: string,\n numericId: number | undefined,\n reportId: string | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n reportManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!numericId) {\n sendResponse(id, {\n success: false,\n error: 'Report ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n // Try database collection first\n try {\n const result = await executeCollection('reports', 'getOne', { id: numericId });\n\n if (result && result.success) {\n logger.info(`[DB] Retrieved report ID: ${numericId}`);\n sendResponse(id, {\n success: true,\n data: {\n id: numericId,\n reportId: result.data?.reportId,\n report: result.data?.report || result.data,\n message: `Retrieved report (DB)`\n }\n }, sendMessage, clientId);\n return;\n }\n } catch (dbError) {\n logger.warn(`[DB] Failed to get report, falling back to file storage: ${dbError instanceof Error ? dbError.message : 'Unknown error'}`);\n }\n\n // Fallback to file-based storage - use reportId if exists, otherwise stringify numericId\n const fileStorageId = reportId || String(numericId);\n const report = reportManager.getReport(fileStorageId);\n\n if (!report) {\n sendResponse(id, {\n success: false,\n error: `Report '${fileStorageId}' not found`\n }, sendMessage, clientId);\n return;\n }\n\n logger.info(`[FILE] Retrieved report: ${fileStorageId}`);\n sendResponse(id, {\n success: true,\n data: {\n id: numericId,\n reportId: fileStorageId,\n report,\n message: `Retrieved report '${fileStorageId}' (File)`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Handle query reports operation with filters\n * Database only - no file fallback for query operations\n */\nasync function handleQuery(\n id: string,\n filters: any,\n limit: number | undefined,\n sort: string | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n reportManager: any,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Try database collection\n try {\n const result = await executeCollection('reports', 'query', {\n filters: filters || {},\n limit,\n sort\n });\n\n if (result && result.success) {\n logger.info(`[DB] Query returned ${result.count} reports`);\n sendResponse(id, {\n success: true,\n data: {\n reports: result.data,\n count: result.count,\n message: `Query returned ${result.count} reports (DB)`\n }\n }, sendMessage, clientId);\n return;\n }\n } catch (dbError) {\n logger.warn(`[DB] Failed to query reports: ${dbError instanceof Error ? dbError.message : 'Unknown error'}`);\n }\n\n // Fallback to file-based storage (get all and return)\n const reports = reportManager.getAllReports();\n\n logger.info(`[FILE] Retrieved ${reports.length} reports (all - no query filter)`);\n sendResponse(id, {\n success: true,\n data: {\n reports,\n count: reports.length,\n message: `Retrieved ${reports.length} reports (File - no query filter)`\n }\n }, sendMessage, clientId);\n}\n\n/**\n * Send reports response\n */\nfunction sendResponse(\n id: string | null,\n res: { success: boolean; error?: string; data?: any },\n sendMessage: (message: Message) => void,\n clientId?: string,\n): void {\n const response: Message = {\n id: id || 'unknown',\n type: 'REPORTS_RES',\n from: { type: 'data-agent' },\n to: {\n type: 'admin',\n id: clientId\n },\n payload: {\n ...res,\n }\n };\n\n sendMessage(response);\n}\n","import { UIsRequestMessageSchema, Message, CollectionRegistry } from '../types';\nimport { logger } from '../utils/logger';\n\n/**\n * Handle unified UIs management request\n * Supports operations: create, update, delete, getAll, getOne, query\n * Only accepts requests from 'admin' type\n *\n * Uses database collections only (no file-based fallback)\n */\nexport async function handleUIsRequest(\n data: any,\n collections: CollectionRegistry,\n sendMessage: (message: Message) => void\n): Promise<void> {\n // Helper function to execute collection operations\n const executeCollection = async (collection: string, op: string, params: any): Promise<any> => {\n const handler = collections[collection]?.[op];\n if (!handler) {\n throw new Error(`Collection handler not found: ${collection}.${op}`);\n }\n return await handler(params);\n };\n\n try {\n const request = UIsRequestMessageSchema.parse(data);\n const { id, payload, from } = request;\n const { operation, data: requestData } = payload;\n const uiId = requestData?.uiId;\n const ui = requestData?.ui;\n const projectId = requestData?.projectId;\n const name = requestData?.name;\n const description = requestData?.description;\n const createdBy = requestData?.createdBy;\n const updatedBy = requestData?.updatedBy;\n const numericId = requestData?.id;\n // Query operation fields\n const filters = requestData?.filters;\n const limit = requestData?.limit;\n const sort = requestData?.sort;\n\n // Verify request is from admin\n if (from.type !== 'admin') {\n sendResponse(id, {\n success: false,\n error: 'Unauthorized: Only admin can manage UIs'\n }, sendMessage, from.id);\n logger.warn(`Unauthorized UI management attempt from: ${from.type}`);\n return;\n }\n\n // Route to appropriate operation handler\n switch (operation) {\n case 'create':\n await handleCreate(id, uiId, ui, projectId, name, description, createdBy, executeCollection, sendMessage, from.id);\n break;\n\n case 'update':\n await handleUpdate(id, numericId, ui, name, description, updatedBy, executeCollection, sendMessage, from.id);\n break;\n\n case 'delete':\n await handleDelete(id, numericId, executeCollection, sendMessage, from.id);\n break;\n\n case 'getAll':\n await handleGetAll(id, executeCollection, sendMessage, from.id);\n break;\n\n case 'getOne':\n await handleGetOne(id, numericId, executeCollection, sendMessage, from.id);\n break;\n\n case 'query':\n await handleQuery(id, filters, limit, sort, executeCollection, sendMessage, from.id);\n break;\n\n default:\n sendResponse(id, {\n success: false,\n error: `Unknown operation: ${operation}`\n }, sendMessage, from.id);\n }\n\n } catch (error) {\n logger.error('Failed to handle UIs request:', error);\n sendResponse(null, {\n success: false,\n error: error instanceof Error ? error.message : 'Unknown error occurred'\n }, sendMessage);\n }\n}\n\n/**\n * Handle create UI operation\n */\nasync function handleCreate(\n id: string,\n uiId: string | undefined,\n ui: any,\n projectId: string | undefined,\n name: string | undefined,\n description: string | undefined,\n createdBy: string | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!uiId || uiId.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'UI ID is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n if (!projectId || projectId.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Project ID is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('uis', 'create', {\n uiId,\n projectId,\n name: name || uiId,\n description,\n ui,\n createdBy\n });\n\n if (result && result.success) {\n logger.info(`UI created successfully, ID: ${result.data?.id}`);\n sendResponse(id, {\n success: true,\n data: {\n id: result.data?.id,\n uiId: result.data?.uiId || uiId,\n ui: result.data?.ui || ui,\n message: `UI created successfully`\n }\n }, sendMessage, clientId);\n } else {\n sendResponse(id, {\n success: false,\n error: 'Failed to create UI'\n }, sendMessage, clientId);\n }\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to create UI'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle update UI operation\n */\nasync function handleUpdate(\n id: string,\n numericId: number | undefined,\n ui: any,\n name: string | undefined,\n description: string | undefined,\n updatedBy: string | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!numericId) {\n sendResponse(id, {\n success: false,\n error: 'UI ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('uis', 'update', {\n id: numericId,\n name,\n description,\n ui,\n updatedBy\n });\n\n if (result && result.success) {\n logger.info(`UI updated successfully, ID: ${numericId}`);\n sendResponse(id, {\n success: true,\n data: {\n id: numericId,\n uiId: result.data?.uiId,\n ui: result.data?.ui || ui,\n message: `UI updated successfully`\n }\n }, sendMessage, clientId);\n } else {\n sendResponse(id, {\n success: false,\n error: 'Failed to update UI'\n }, sendMessage, clientId);\n }\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to update UI'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle delete UI operation\n */\nasync function handleDelete(\n id: string,\n numericId: number | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!numericId) {\n sendResponse(id, {\n success: false,\n error: 'UI ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('uis', 'delete', { id: numericId });\n\n if (result && result.success) {\n logger.info(`UI deleted successfully, ID: ${numericId}`);\n sendResponse(id, {\n success: true,\n data: {\n id: numericId,\n message: `UI deleted successfully`\n }\n }, sendMessage, clientId);\n } else {\n sendResponse(id, {\n success: false,\n error: 'Failed to delete UI'\n }, sendMessage, clientId);\n }\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to delete UI'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle get all UIs operation\n */\nasync function handleGetAll(\n id: string,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n try {\n const result = await executeCollection('uis', 'getAll', {});\n\n if (result && result.success) {\n logger.info(`Retrieved ${result.count} UIs`);\n sendResponse(id, {\n success: true,\n data: {\n uis: result.data,\n count: result.count,\n message: `Retrieved ${result.count} UIs`\n }\n }, sendMessage, clientId);\n } else {\n sendResponse(id, {\n success: false,\n error: 'Failed to get UIs'\n }, sendMessage, clientId);\n }\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to get UIs'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle get one UI operation\n */\nasync function handleGetOne(\n id: string,\n numericId: number | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!numericId) {\n sendResponse(id, {\n success: false,\n error: 'UI ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('uis', 'getOne', { id: numericId });\n\n if (result && result.success) {\n logger.info(`Retrieved UI ID: ${numericId}`);\n sendResponse(id, {\n success: true,\n data: {\n id: numericId,\n uiId: result.data?.uiId,\n ui: result.data?.ui || result.data,\n message: `Retrieved UI`\n }\n }, sendMessage, clientId);\n } else {\n sendResponse(id, {\n success: false,\n error: `UI not found`\n }, sendMessage, clientId);\n }\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to get UI'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle query UIs operation with filters\n */\nasync function handleQuery(\n id: string,\n filters: any,\n limit: number | undefined,\n sort: string | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n try {\n const result = await executeCollection('uis', 'query', {\n filters: filters || {},\n limit,\n sort\n });\n\n if (result && result.success) {\n logger.info(`Query returned ${result.count} UIs`);\n sendResponse(id, {\n success: true,\n data: {\n uis: result.data,\n count: result.count,\n message: `Query returned ${result.count} UIs`\n }\n }, sendMessage, clientId);\n } else {\n sendResponse(id, {\n success: false,\n error: 'Failed to query UIs'\n }, sendMessage, clientId);\n }\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to query UIs'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Send UIs response\n */\nfunction sendResponse(\n id: string | null,\n res: { success: boolean; error?: string; data?: any },\n sendMessage: (message: Message) => void,\n clientId?: string,\n): void {\n const response: Message = {\n id: id || 'unknown',\n type: 'UIS_RES',\n from: { type: 'data-agent' },\n to: {\n type: 'admin',\n id: clientId\n },\n payload: {\n ...res,\n }\n };\n\n sendMessage(response);\n}\n","import { BookmarksRequestMessageSchema, Message, CollectionRegistry, DBUIBlock } from '../types';\nimport { logger } from '../utils/logger';\n\n/**\n * Handle unified bookmarks management request\n * Supports operations: create, update, delete, getAll, getOne\n * Routes requests to backend collections\n */\nexport async function handleBookmarksRequest(\n data: any,\n collections: CollectionRegistry,\n sendMessage: (message: Message) => void\n): Promise<void> {\n // Helper function to execute collection operations\n const executeCollection = async (collection: string, op: string, params: any): Promise<any> => {\n const handler = collections[collection]?.[op];\n if (!handler) {\n throw new Error(`Collection operation ${collection}.${op} not found`);\n }\n return await handler(params);\n };\n try {\n const request = BookmarksRequestMessageSchema.parse(data);\n const { id, payload, from } = request;\n const { operation, data: requestData } = payload;\n const bookmarkId = requestData?.id;\n const userId = requestData?.userId;\n const threadId = requestData?.threadId;\n const name = requestData?.name;\n const description = requestData?.description;\n const uiblock = requestData?.uiblock;\n // Query operation fields\n const filters = requestData?.filters;\n const limit = requestData?.limit;\n const sort = requestData?.sort;\n\n // Route to appropriate operation handler\n switch (operation) {\n case 'create':\n await handleCreate(id, userId, threadId, name, description, uiblock, executeCollection, sendMessage, from.id);\n break;\n\n case 'update':\n await handleUpdate(id, bookmarkId, threadId, name, description, uiblock, executeCollection, sendMessage, from.id);\n break;\n\n case 'delete':\n await handleDelete(id, bookmarkId, executeCollection, sendMessage, from.id);\n break;\n\n case 'getAll':\n await handleGetAll(id, executeCollection, sendMessage, from.id);\n break;\n\n case 'getOne':\n await handleGetOne(id, bookmarkId, executeCollection, sendMessage, from.id);\n break;\n\n case 'query':\n await handleQuery(id, filters, limit, sort, executeCollection, sendMessage, from.id);\n break;\n\n default:\n sendResponse(id, {\n success: false,\n error: `Unknown operation: ${operation}`\n }, sendMessage, from.id);\n }\n\n } catch (error) {\n logger.error('Failed to handle bookmarks request:', error);\n sendResponse(null, {\n success: false,\n error: error instanceof Error ? error.message : 'Unknown error occurred'\n }, sendMessage);\n }\n}\n\n/**\n * Handle create bookmark operation\n */\nasync function handleCreate(\n id: string,\n userId: string | undefined,\n threadId: string | undefined,\n name: string | undefined,\n description: string | undefined,\n uiblock: DBUIBlock | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!userId) {\n sendResponse(id, {\n success: false,\n error: 'userId is required'\n }, sendMessage, clientId);\n return;\n }\n\n if (!uiblock) {\n sendResponse(id, {\n success: false,\n error: 'UIBlock data is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('bookmarks', 'create', { userId, threadId, name, description, uiblock });\n\n sendResponse(id, {\n success: true,\n data: result.data,\n message: 'Bookmark created successfully'\n }, sendMessage, clientId);\n\n logger.info(`Bookmark created: ID ${result.data.id}`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to create bookmark'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle update bookmark operation\n */\nasync function handleUpdate(\n id: string,\n bookmarkId: number | undefined,\n threadId: string | undefined,\n name: string | undefined,\n description: string | undefined,\n uiblock: DBUIBlock | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!bookmarkId) {\n sendResponse(id, {\n success: false,\n error: 'Bookmark ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('bookmarks', 'update', { id: bookmarkId, threadId, name, description, uiblock });\n\n sendResponse(id, {\n success: true,\n data: result.data,\n message: 'Bookmark updated successfully'\n }, sendMessage, clientId);\n\n logger.info(`Bookmark updated: ID ${bookmarkId}`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to update bookmark'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle delete bookmark operation\n */\nasync function handleDelete(\n id: string,\n bookmarkId: number | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!bookmarkId) {\n sendResponse(id, {\n success: false,\n error: 'Bookmark ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('bookmarks', 'delete', { id: bookmarkId });\n\n sendResponse(id, {\n success: true,\n data: result.data,\n message: 'Bookmark deleted successfully'\n }, sendMessage, clientId);\n\n logger.info(`Bookmark deleted: ID ${bookmarkId}`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to delete bookmark'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle get all bookmarks operation\n */\nasync function handleGetAll(\n id: string,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n try {\n const result = await executeCollection('bookmarks', 'getAll', {});\n\n sendResponse(id, {\n success: true,\n data: result.data,\n count: result.count,\n message: `Retrieved ${result.count} bookmarks`\n }, sendMessage, clientId);\n\n logger.info(`Retrieved all bookmarks (count: ${result.count})`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to get bookmarks'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle get one bookmark operation\n */\nasync function handleGetOne(\n id: string,\n bookmarkId: number | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!bookmarkId) {\n sendResponse(id, {\n success: false,\n error: 'Bookmark ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('bookmarks', 'getOne', { id: bookmarkId });\n\n sendResponse(id, {\n success: true,\n data: result.data,\n message: `Retrieved bookmark ID ${bookmarkId}`\n }, sendMessage, clientId);\n\n logger.info(`Retrieved bookmark: ID ${bookmarkId}`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to get bookmark'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle query bookmarks operation with filters\n */\nasync function handleQuery(\n id: string,\n filters: any,\n limit: number | undefined,\n sort: string | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n try {\n const result = await executeCollection('bookmarks', 'query', {\n filters: filters || {},\n limit,\n sort\n });\n\n sendResponse(id, {\n success: true,\n data: result.data,\n count: result.count,\n message: `Query returned ${result.count} bookmarks`\n }, sendMessage, clientId);\n\n logger.info(`Query returned ${result.count} bookmarks`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to query bookmarks'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Send bookmarks response\n */\nfunction sendResponse(\n id: string | null,\n res: { success: boolean; error?: string; data?: any; count?: number; message?: string },\n sendMessage: (message: Message) => void,\n clientId?: string,\n): void {\n const response: Message = {\n id: id || 'unknown',\n type: 'BOOKMARKS_RES',\n from: { type: 'data-agent' },\n to: {\n type: 'user',\n id: clientId\n },\n payload: {\n ...res,\n }\n };\n\n sendMessage(response);\n}\n","import { ArtifactsRequestMessageSchema, Message, CollectionRegistry } from '../types';\nimport { logger } from '../utils/logger';\n\n/**\n * Handle unified artifacts management request\n * Supports operations: create, update, delete, getAll, getOne\n * Routes requests to backend collections\n */\nexport async function handleArtifactsRequest(\n data: any,\n collections: CollectionRegistry,\n sendMessage: (message: Message) => void\n): Promise<void> {\n // Helper function to execute collection operations\n const executeCollection = async (collection: string, op: string, params: any): Promise<any> => {\n const handler = collections[collection]?.[op];\n if (!handler) {\n throw new Error(`Collection operation ${collection}.${op} not found`);\n }\n return await handler(params);\n };\n\n try {\n const request = ArtifactsRequestMessageSchema.parse(data);\n const { id, payload, from } = request;\n const { operation, data: requestData } = payload;\n\n logger.info('[SDK-NODEJS] Received artifacts request:', JSON.stringify({ operation, requestData }, null, 2));\n\n const artifactId = requestData?.id;\n const name = requestData?.name;\n const createdBy = requestData?.createdBy;\n const dsl = requestData?.dsl;\n const type = requestData?.type;\n const filters = requestData?.filters;\n const status = requestData?.status;\n const deleted = requestData?.deleted;\n const limit = requestData?.limit;\n const offset = requestData?.offset;\n const sort = requestData?.sort;\n\n logger.info('[SDK-NODEJS] Extracted params:', JSON.stringify({\n name,\n type,\n filters: filters ? 'present' : 'undefined'\n }, null, 2));\n\n logger.info('[SDK-NODEJS] Full requestData.filters:', JSON.stringify(filters, null, 2));\n\n // Route to appropriate operation handler\n switch (operation) {\n case 'create':\n await handleCreate(id, name, createdBy, dsl, type, filters, status, executeCollection, sendMessage, from.id);\n break;\n\n case 'update':\n await handleUpdate(id, artifactId, name, dsl, type, filters, status, deleted, executeCollection, sendMessage, from.id);\n break;\n\n case 'delete':\n await handleDelete(id, artifactId, executeCollection, sendMessage, from.id);\n break;\n\n case 'getAll':\n await handleGetAll(id, limit, executeCollection, sendMessage, from.id);\n break;\n\n case 'getOne':\n await handleGetOne(id, artifactId, executeCollection, sendMessage, from.id);\n break;\n\n case 'query':\n await handleQuery(id, { filters, limit, offset, sort }, executeCollection, sendMessage, from.id);\n break;\n\n default:\n sendResponse(id, {\n success: false,\n error: `Unknown operation: ${operation}`\n }, sendMessage, from.id);\n }\n\n } catch (error) {\n logger.error('Failed to handle artifacts request:', error);\n sendResponse(null, {\n success: false,\n error: error instanceof Error ? error.message : 'Unknown error occurred'\n }, sendMessage);\n }\n}\n\n/**\n * Handle create artifact operation\n */\nasync function handleCreate(\n id: string,\n name: string | undefined,\n createdBy: string | undefined,\n dsl: Record<string, any> | undefined,\n type: string | undefined,\n filters: Record<string, any> | undefined,\n status: string | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!name) {\n sendResponse(id, {\n success: false,\n error: 'name is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('artifacts', 'create', {\n name,\n createdBy,\n dsl,\n type,\n filters,\n status\n });\n\n sendResponse(id, {\n success: true,\n data: result.data,\n message: 'Artifact created successfully'\n }, sendMessage, clientId);\n\n logger.info(`Artifact created: ID ${result.data.id}`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to create artifact'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle update artifact operation\n */\nasync function handleUpdate(\n id: string,\n artifactId: number | undefined,\n name: string | undefined,\n dsl: Record<string, any> | undefined,\n type: string | undefined,\n filters: Record<string, any> | undefined,\n status: string | undefined,\n deleted: boolean | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!artifactId) {\n sendResponse(id, {\n success: false,\n error: 'Artifact ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('artifacts', 'update', { id: artifactId, name, dsl, type, filters, status, deleted });\n\n sendResponse(id, {\n success: true,\n data: result.data,\n message: 'Artifact updated successfully'\n }, sendMessage, clientId);\n\n logger.info(`Artifact updated: ID ${artifactId}`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to update artifact'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle delete artifact operation\n */\nasync function handleDelete(\n id: string,\n artifactId: number | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!artifactId) {\n sendResponse(id, {\n success: false,\n error: 'Artifact ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('artifacts', 'delete', { id: artifactId });\n\n sendResponse(id, {\n success: true,\n data: result.data,\n message: 'Artifact deleted successfully'\n }, sendMessage, clientId);\n\n logger.info(`Artifact deleted: ID ${artifactId}`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to delete artifact'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle get all artifacts operation\n */\nasync function handleGetAll(\n id: string,\n limit: number | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n try {\n const result = await executeCollection('artifacts', 'getAll', { limit });\n\n sendResponse(id, {\n success: true,\n data: result.data,\n count: result.count,\n message: `Retrieved ${result.count} artifacts`\n }, sendMessage, clientId);\n\n logger.info(`Retrieved all artifacts (count: ${result.count})`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to get artifacts'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle get one artifact operation\n */\nasync function handleGetOne(\n id: string,\n artifactId: number | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!artifactId) {\n sendResponse(id, {\n success: false,\n error: 'Artifact ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('artifacts', 'getOne', { id: artifactId });\n\n sendResponse(id, {\n success: true,\n data: result.data,\n message: `Retrieved artifact ID ${artifactId}`\n }, sendMessage, clientId);\n\n logger.info(`Retrieved artifact: ID ${artifactId}`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to get artifact'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle query artifacts operation\n */\nasync function handleQuery(\n id: string,\n queryParams: {\n filters?: {\n createdBy?: string;\n type?: string;\n status?: string;\n name?: string;\n deleted?: boolean;\n createdAt?: string;\n };\n limit?: number;\n offset?: number;\n sort?: 'ASC' | 'DESC';\n },\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n try {\n logger.info('[SDK-NODEJS] handleQuery - queryParams:', JSON.stringify(queryParams, null, 2));\n logger.info('[SDK-NODEJS] handleQuery - filters.type:', queryParams.filters?.type);\n\n const result = await executeCollection('artifacts', 'query', {\n filters: queryParams.filters,\n limit: queryParams.limit || 50,\n offset: queryParams.offset || 0,\n sort: queryParams.sort || 'DESC',\n });\n\n logger.info('[SDK-NODEJS] handleQuery - executeCollection result count:', result.count);\n\n sendResponse(id, {\n success: true,\n data: result.data,\n count: result.count,\n message: `Query returned ${result.count} artifacts`\n }, sendMessage, clientId);\n\n logger.info(`Query artifacts (count: ${result.count})`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to query artifacts'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Send artifacts response\n */\nfunction sendResponse(\n id: string | null,\n res: { success: boolean; error?: string; data?: any; count?: number; message?: string },\n sendMessage: (message: Message) => void,\n clientId?: string,\n): void {\n const response: Message = {\n id: id || 'unknown',\n type: 'ARTIFACTS_RES',\n from: { type: 'data-agent' },\n to: {\n type: 'user',\n id: clientId\n },\n payload: {\n ...res,\n }\n };\n\n sendMessage(response);\n}\n","import { KbNodesRequestMessageSchema, Message, CollectionRegistry } from '../types';\nimport { logger } from '../utils/logger';\n\n/**\n * Handle unified KB Nodes (Knowledge Base) management request\n * Supports operations: create, update, delete, getAll, getOne, search, getByCategory, getByUser, getCategories, getTags\n *\n * Strategy: Uses database collections for all operations\n */\nexport async function handleKbNodesRequest(\n data: any,\n collections: CollectionRegistry,\n sendMessage: (message: Message) => void\n): Promise<void> {\n // Helper function to execute collection operations\n const executeCollection = async (collection: string, op: string, params: any): Promise<any> => {\n const handler = collections[collection]?.[op];\n if (!handler) {\n throw new Error(`Collection '${collection}' or operation '${op}' not found`);\n }\n return await handler(params);\n };\n\n try {\n const request = KbNodesRequestMessageSchema.parse(data);\n const { id, payload, from } = request;\n const { operation, data: requestData } = payload;\n\n // Extract fields from request data\n const nodeId = requestData?.id;\n const title = requestData?.title;\n const content = requestData?.content;\n const category = requestData?.category;\n const tags = requestData?.tags;\n const type = requestData?.type;\n const createdBy = requestData?.createdBy;\n const updatedBy = requestData?.updatedBy;\n const userId = requestData?.userId;\n const query = requestData?.query;\n const filters = requestData?.filters;\n const limit = requestData?.limit;\n const offset = requestData?.offset;\n\n // Route to appropriate operation handler\n switch (operation) {\n case 'create':\n await handleCreate(id, { title, content, category, tags, type, createdBy }, executeCollection, sendMessage, from.id);\n break;\n\n case 'update':\n await handleUpdate(id, nodeId, { title, content, category, tags, type, updatedBy }, executeCollection, sendMessage, from.id);\n break;\n\n case 'delete':\n await handleDelete(id, nodeId, executeCollection, sendMessage, from.id);\n break;\n\n case 'getAll':\n await handleGetAll(id, limit, offset, executeCollection, sendMessage, from.id);\n break;\n\n case 'getOne':\n await handleGetOne(id, nodeId, executeCollection, sendMessage, from.id);\n break;\n\n case 'search':\n await handleSearch(id, { query, category, tags, type, createdBy, limit, offset }, executeCollection, sendMessage, from.id);\n break;\n\n case 'getByCategory':\n await handleGetByCategory(id, category, limit, offset, executeCollection, sendMessage, from.id);\n break;\n\n case 'getByUser':\n await handleGetByUser(id, userId, limit, offset, executeCollection, sendMessage, from.id);\n break;\n\n case 'getCategories':\n await handleGetCategories(id, executeCollection, sendMessage, from.id);\n break;\n\n case 'getTags':\n await handleGetTags(id, executeCollection, sendMessage, from.id);\n break;\n\n default:\n sendResponse(id, {\n success: false,\n error: `Unknown operation: ${operation}`\n }, sendMessage, from.id);\n }\n\n } catch (error) {\n logger.error('Failed to handle KB nodes request:', error);\n sendResponse(null, {\n success: false,\n error: error instanceof Error ? error.message : 'Unknown error occurred'\n }, sendMessage);\n }\n}\n\n/**\n * Handle create KB node operation\n */\nasync function handleCreate(\n id: string,\n nodeData: {\n title?: string;\n content?: string;\n category?: string;\n tags?: string[];\n type?: 'global' | 'user' | 'query';\n createdBy?: string;\n },\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n const { title, content, category, tags, type, createdBy } = nodeData;\n\n // Validate required fields\n if (!title || title.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Title is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n if (!content || content.trim().length === 0) {\n sendResponse(id, {\n success: false,\n error: 'Content is required and cannot be empty'\n }, sendMessage, clientId);\n return;\n }\n\n if (!createdBy) {\n sendResponse(id, {\n success: false,\n error: 'createdBy (user ID) is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('kbNodes', 'create', {\n title,\n content,\n category: category || undefined,\n tags: tags || undefined,\n type: type || 'query',\n createdBy\n });\n\n if (result && result.success) {\n logger.info(`[DB] KB node created successfully: ${title}`);\n sendResponse(id, {\n success: true,\n data: {\n ...result.data,\n message: `Knowledge node '${title}' created successfully`\n }\n }, sendMessage, clientId);\n return;\n }\n\n sendResponse(id, {\n success: false,\n error: 'Failed to create knowledge node'\n }, sendMessage, clientId);\n } catch (error) {\n logger.error('[DB] Failed to create KB node:', error);\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to create knowledge node'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle update KB node operation\n */\nasync function handleUpdate(\n id: string,\n nodeId: number | undefined,\n nodeData: {\n title?: string;\n content?: string;\n category?: string;\n tags?: string[];\n type?: 'global' | 'user' | 'query';\n updatedBy?: string;\n },\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n const { title, content, category, tags, type, updatedBy } = nodeData;\n\n if (!nodeId) {\n sendResponse(id, {\n success: false,\n error: 'Knowledge node ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n if (!updatedBy) {\n sendResponse(id, {\n success: false,\n error: 'updatedBy (user ID) is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('kbNodes', 'update', {\n id: nodeId,\n title,\n content,\n category,\n tags,\n type,\n updatedBy\n });\n\n if (result && result.success) {\n logger.info(`[DB] KB node updated successfully, ID: ${nodeId}`);\n sendResponse(id, {\n success: true,\n data: {\n ...result.data,\n message: `Knowledge node updated successfully`\n }\n }, sendMessage, clientId);\n return;\n }\n\n sendResponse(id, {\n success: false,\n error: 'Failed to update knowledge node'\n }, sendMessage, clientId);\n } catch (error) {\n logger.error('[DB] Failed to update KB node:', error);\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to update knowledge node'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle delete KB node operation\n */\nasync function handleDelete(\n id: string,\n nodeId: number | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n if (!nodeId) {\n sendResponse(id, {\n success: false,\n error: 'Knowledge node ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('kbNodes', 'delete', { id: nodeId });\n\n if (result && result.success) {\n logger.info(`[DB] KB node deleted successfully, ID: ${nodeId}`);\n sendResponse(id, {\n success: true,\n data: {\n id: nodeId,\n ...result.data,\n message: `Knowledge node deleted successfully`\n }\n }, sendMessage, clientId);\n return;\n }\n\n sendResponse(id, {\n success: false,\n error: 'Failed to delete knowledge node'\n }, sendMessage, clientId);\n } catch (error) {\n logger.error('[DB] Failed to delete KB node:', error);\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to delete knowledge node'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle get all KB nodes operation\n */\nasync function handleGetAll(\n id: string,\n limit: number | undefined,\n offset: number | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n try {\n const result = await executeCollection('kbNodes', 'getAll', {\n limit: limit || 100,\n offset: offset || 0\n });\n\n if (result && result.success) {\n logger.info(`[DB] Retrieved ${result.count} KB nodes`);\n sendResponse(id, {\n success: true,\n data: {\n nodes: result.data,\n count: result.count,\n message: `Retrieved ${result.count} knowledge nodes`\n }\n }, sendMessage, clientId);\n return;\n }\n\n sendResponse(id, {\n success: false,\n error: 'Failed to retrieve knowledge nodes'\n }, sendMessage, clientId);\n } catch (error) {\n logger.error('[DB] Failed to get all KB nodes:', error);\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to retrieve knowledge nodes'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle get one KB node operation\n */\nasync function handleGetOne(\n id: string,\n nodeId: number | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n if (!nodeId) {\n sendResponse(id, {\n success: false,\n error: 'Knowledge node ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('kbNodes', 'getOne', { id: nodeId });\n\n if (result && result.success) {\n logger.info(`[DB] Retrieved KB node ID: ${nodeId}`);\n sendResponse(id, {\n success: true,\n data: {\n node: result.data,\n message: `Retrieved knowledge node`\n }\n }, sendMessage, clientId);\n return;\n }\n\n sendResponse(id, {\n success: false,\n error: 'Failed to retrieve knowledge node'\n }, sendMessage, clientId);\n } catch (error) {\n logger.error('[DB] Failed to get KB node:', error);\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to retrieve knowledge node'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle search KB nodes operation\n */\nasync function handleSearch(\n id: string,\n searchParams: {\n query?: string;\n category?: string;\n tags?: string[];\n type?: 'global' | 'user' | 'query';\n createdBy?: string;\n limit?: number;\n offset?: number;\n },\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n const { query, category, tags, type, createdBy, limit, offset } = searchParams;\n\n try {\n const result = await executeCollection('kbNodes', 'search', {\n query,\n category,\n tags,\n type,\n createdBy,\n limit: limit || 50,\n offset: offset || 0\n });\n\n if (result && result.success) {\n logger.info(`[DB] Search returned ${result.count} KB nodes`);\n sendResponse(id, {\n success: true,\n data: {\n nodes: result.data,\n count: result.count,\n message: `Search returned ${result.count} knowledge nodes`\n }\n }, sendMessage, clientId);\n return;\n }\n\n sendResponse(id, {\n success: false,\n error: 'Failed to search knowledge nodes'\n }, sendMessage, clientId);\n } catch (error) {\n logger.error('[DB] Failed to search KB nodes:', error);\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to search knowledge nodes'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle get KB nodes by category operation\n */\nasync function handleGetByCategory(\n id: string,\n category: string | undefined,\n limit: number | undefined,\n offset: number | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n if (!category) {\n sendResponse(id, {\n success: false,\n error: 'Category is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('kbNodes', 'getByCategory', {\n category,\n limit: limit || 50,\n offset: offset || 0\n });\n\n if (result && result.success) {\n logger.info(`[DB] Retrieved ${result.count} KB nodes for category: ${category}`);\n sendResponse(id, {\n success: true,\n data: {\n nodes: result.data,\n count: result.count,\n category,\n message: `Retrieved ${result.count} knowledge nodes for category '${category}'`\n }\n }, sendMessage, clientId);\n return;\n }\n\n sendResponse(id, {\n success: false,\n error: 'Failed to retrieve knowledge nodes by category'\n }, sendMessage, clientId);\n } catch (error) {\n logger.error('[DB] Failed to get KB nodes by category:', error);\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to retrieve knowledge nodes by category'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle get KB nodes by user operation\n */\nasync function handleGetByUser(\n id: string,\n userId: string | undefined,\n limit: number | undefined,\n offset: number | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n if (!userId) {\n sendResponse(id, {\n success: false,\n error: 'User ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('kbNodes', 'getByUser', {\n userId,\n limit: limit || 50,\n offset: offset || 0\n });\n\n if (result && result.success) {\n logger.info(`[DB] Retrieved ${result.count} KB nodes for user: ${userId}`);\n sendResponse(id, {\n success: true,\n data: {\n nodes: result.data,\n count: result.count,\n userId,\n message: `Retrieved ${result.count} knowledge nodes for user ${userId}`\n }\n }, sendMessage, clientId);\n return;\n }\n\n sendResponse(id, {\n success: false,\n error: 'Failed to retrieve knowledge nodes by user'\n }, sendMessage, clientId);\n } catch (error) {\n logger.error('[DB] Failed to get KB nodes by user:', error);\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to retrieve knowledge nodes by user'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle get all categories operation\n */\nasync function handleGetCategories(\n id: string,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n try {\n const result = await executeCollection('kbNodes', 'getCategories', {});\n\n if (result && result.success) {\n logger.info(`[DB] Retrieved ${result.count} categories`);\n sendResponse(id, {\n success: true,\n data: {\n categories: result.data,\n count: result.count,\n message: `Retrieved ${result.count} categories`\n }\n }, sendMessage, clientId);\n return;\n }\n\n sendResponse(id, {\n success: false,\n error: 'Failed to retrieve categories'\n }, sendMessage, clientId);\n } catch (error) {\n logger.error('[DB] Failed to get categories:', error);\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to retrieve categories'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle get all tags operation\n */\nasync function handleGetTags(\n id: string,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n try {\n const result = await executeCollection('kbNodes', 'getTags', {});\n\n if (result && result.success) {\n logger.info(`[DB] Retrieved ${result.count} tags`);\n sendResponse(id, {\n success: true,\n data: {\n tags: result.data,\n count: result.count,\n message: `Retrieved ${result.count} tags`\n }\n }, sendMessage, clientId);\n return;\n }\n\n sendResponse(id, {\n success: false,\n error: 'Failed to retrieve tags'\n }, sendMessage, clientId);\n } catch (error) {\n logger.error('[DB] Failed to get tags:', error);\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to retrieve tags'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Send KB nodes response\n */\nfunction sendResponse(\n id: string | null,\n res: { success: boolean; error?: string; data?: any },\n sendMessage: (message: Message) => void,\n clientId?: string,\n): void {\n const response: Message = {\n id: id || 'unknown',\n type: 'KB_NODES_RES',\n from: { type: 'data-agent' },\n to: {\n type: 'admin',\n id: clientId\n },\n payload: {\n ...res,\n }\n };\n\n sendMessage(response);\n}\n","import { MenusRequestMessageSchema, Message, CollectionRegistry } from '../types';\nimport { logger } from '../utils/logger';\n\n/**\n * Handle unified menus management request\n * Supports operations: create, update, delete, getAll, getOne, getRootMenus, getChildMenus, getHierarchy, query, reorder\n * Routes requests to backend collections\n */\nexport async function handleMenusRequest(\n data: any,\n collections: CollectionRegistry,\n sendMessage: (message: Message) => void\n): Promise<void> {\n // Helper function to execute collection operations\n const executeCollection = async (collection: string, op: string, params: any): Promise<any> => {\n const handler = collections[collection]?.[op];\n if (!handler) {\n throw new Error(`Collection operation ${collection}.${op} not found`);\n }\n return await handler(params);\n };\n\n try {\n const request = MenusRequestMessageSchema.parse(data);\n const { id, payload, from } = request;\n const { operation, data: requestData } = payload;\n\n // Extract common fields\n const menuId = requestData?.id;\n const name = requestData?.name;\n const componentName = requestData?.componentName;\n const icon = requestData?.icon;\n const userMessage = requestData?.userMessage;\n const parentId = requestData?.parentId;\n const sortOrder = requestData?.sortOrder;\n const props = requestData?.props;\n const isActive = requestData?.isActive;\n // Query operation fields\n const filters = requestData?.filters;\n const limit = requestData?.limit;\n const sort = requestData?.sort;\n // Reorder operation fields\n const items = requestData?.items;\n\n // Route to appropriate operation handler\n switch (operation) {\n case 'create':\n await handleCreate(id, name, componentName, icon, userMessage, parentId, sortOrder, props, isActive, executeCollection, sendMessage, from.id);\n break;\n\n case 'update':\n await handleUpdate(id, menuId, name, componentName, icon, userMessage, parentId, sortOrder, props, isActive, executeCollection, sendMessage, from.id);\n break;\n\n case 'delete':\n await handleDelete(id, menuId, executeCollection, sendMessage, from.id);\n break;\n\n case 'getAll':\n await handleGetAll(id, executeCollection, sendMessage, from.id);\n break;\n\n case 'getOne':\n await handleGetOne(id, menuId, executeCollection, sendMessage, from.id);\n break;\n\n case 'getRootMenus':\n await handleGetRootMenus(id, executeCollection, sendMessage, from.id);\n break;\n\n case 'getChildMenus':\n await handleGetChildMenus(id, parentId, executeCollection, sendMessage, from.id);\n break;\n\n case 'getHierarchy':\n await handleGetHierarchy(id, executeCollection, sendMessage, from.id);\n break;\n\n case 'query':\n await handleQuery(id, filters, limit, sort, executeCollection, sendMessage, from.id);\n break;\n\n case 'reorder':\n await handleReorder(id, items, executeCollection, sendMessage, from.id);\n break;\n\n default:\n sendResponse(id, {\n success: false,\n error: `Unknown operation: ${operation}`\n }, sendMessage, from.id);\n }\n\n } catch (error) {\n logger.error('Failed to handle menus request:', error);\n sendResponse(null, {\n success: false,\n error: error instanceof Error ? error.message : 'Unknown error occurred'\n }, sendMessage);\n }\n}\n\n/**\n * Handle create menu operation\n */\nasync function handleCreate(\n id: string,\n name: string | undefined,\n componentName: string | undefined,\n icon: string | undefined,\n userMessage: string | undefined,\n parentId: number | null | undefined,\n sortOrder: number | undefined,\n props: Record<string, unknown> | undefined,\n isActive: boolean | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!name) {\n sendResponse(id, {\n success: false,\n error: 'name is required'\n }, sendMessage, clientId);\n return;\n }\n\n if (!componentName) {\n sendResponse(id, {\n success: false,\n error: 'componentName is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('menus', 'create', {\n name,\n componentName,\n icon,\n userMessage,\n parentId,\n sortOrder,\n props,\n isActive\n });\n\n sendResponse(id, {\n success: true,\n data: result.data,\n message: 'Menu created successfully'\n }, sendMessage, clientId);\n\n logger.info(`Menu created: ID ${result.data?.id}`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to create menu'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle update menu operation\n */\nasync function handleUpdate(\n id: string,\n menuId: number | undefined,\n name: string | undefined,\n componentName: string | undefined,\n icon: string | undefined,\n userMessage: string | undefined,\n parentId: number | null | undefined,\n sortOrder: number | undefined,\n props: Record<string, unknown> | undefined,\n isActive: boolean | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!menuId) {\n sendResponse(id, {\n success: false,\n error: 'Menu ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('menus', 'update', {\n id: menuId,\n name,\n componentName,\n icon,\n userMessage,\n parentId,\n sortOrder,\n props,\n isActive\n });\n\n sendResponse(id, {\n success: true,\n data: result.data,\n message: 'Menu updated successfully'\n }, sendMessage, clientId);\n\n logger.info(`Menu updated: ID ${menuId}`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to update menu'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle delete menu operation\n */\nasync function handleDelete(\n id: string,\n menuId: number | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!menuId) {\n sendResponse(id, {\n success: false,\n error: 'Menu ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('menus', 'delete', { id: menuId });\n\n sendResponse(id, {\n success: true,\n data: result.data,\n message: 'Menu deleted successfully'\n }, sendMessage, clientId);\n\n logger.info(`Menu deleted: ID ${menuId}`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to delete menu'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle get all menus operation\n */\nasync function handleGetAll(\n id: string,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n try {\n const result = await executeCollection('menus', 'getAll', {});\n\n sendResponse(id, {\n success: true,\n data: result.data,\n count: result.count,\n message: `Retrieved ${result.count} menus`\n }, sendMessage, clientId);\n\n logger.info(`Retrieved all menus (count: ${result.count})`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to get menus'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle get one menu operation\n */\nasync function handleGetOne(\n id: string,\n menuId: number | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!menuId) {\n sendResponse(id, {\n success: false,\n error: 'Menu ID is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('menus', 'getOne', { id: menuId });\n\n sendResponse(id, {\n success: true,\n data: result.data,\n message: `Retrieved menu ID ${menuId}`\n }, sendMessage, clientId);\n\n logger.info(`Retrieved menu: ID ${menuId}`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to get menu'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle get root menus operation (top-level sidebar items)\n */\nasync function handleGetRootMenus(\n id: string,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n try {\n const result = await executeCollection('menus', 'getRootMenus', {});\n\n sendResponse(id, {\n success: true,\n data: result.data,\n count: result.count,\n message: `Retrieved ${result.count} root menus`\n }, sendMessage, clientId);\n\n logger.info(`Retrieved root menus (count: ${result.count})`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to get root menus'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle get child menus operation\n */\nasync function handleGetChildMenus(\n id: string,\n parentId: number | null | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (parentId === undefined || parentId === null) {\n sendResponse(id, {\n success: false,\n error: 'parentId is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('menus', 'getChildMenus', { parentId });\n\n sendResponse(id, {\n success: true,\n data: result.data,\n count: result.count,\n message: `Retrieved ${result.count} child menus for parent ${parentId}`\n }, sendMessage, clientId);\n\n logger.info(`Retrieved child menus for parent ${parentId} (count: ${result.count})`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to get child menus'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle get menus hierarchy operation (nested structure)\n */\nasync function handleGetHierarchy(\n id: string,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n try {\n const result = await executeCollection('menus', 'getHierarchy', {});\n\n sendResponse(id, {\n success: true,\n data: result.data,\n count: result.count,\n message: `Retrieved menus hierarchy with ${result.count} root items`\n }, sendMessage, clientId);\n\n logger.info(`Retrieved menus hierarchy (root count: ${result.count})`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to get menus hierarchy'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle query menus operation with filters\n */\nasync function handleQuery(\n id: string,\n filters: any,\n limit: number | undefined,\n sort: string | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n try {\n const result = await executeCollection('menus', 'query', {\n filters: filters || {},\n limit,\n sort\n });\n\n sendResponse(id, {\n success: true,\n data: result.data,\n count: result.count,\n message: `Query returned ${result.count} menus`\n }, sendMessage, clientId);\n\n logger.info(`Query returned ${result.count} menus`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to query menus'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Handle reorder menus operation\n */\nasync function handleReorder(\n id: string,\n items: Array<{ id: number; sortOrder: number }> | undefined,\n executeCollection: (collection: string, op: string, params: any) => Promise<any>,\n sendMessage: (message: Message) => void,\n clientId?: string\n): Promise<void> {\n // Validate input\n if (!items || !Array.isArray(items) || items.length === 0) {\n sendResponse(id, {\n success: false,\n error: 'items array is required'\n }, sendMessage, clientId);\n return;\n }\n\n try {\n const result = await executeCollection('menus', 'reorder', { items });\n\n sendResponse(id, {\n success: true,\n data: result.data,\n message: `Reordered ${items.length} menus successfully`\n }, sendMessage, clientId);\n\n logger.info(`Reordered ${items.length} menus`);\n } catch (error) {\n sendResponse(id, {\n success: false,\n error: error instanceof Error ? error.message : 'Failed to reorder menus'\n }, sendMessage, clientId);\n }\n}\n\n/**\n * Send menus response\n */\nfunction sendResponse(\n id: string | null,\n res: { success: boolean; error?: string; data?: any; count?: number; message?: string },\n sendMessage: (message: Message) => void,\n clientId?: string,\n): void {\n const response: Message = {\n id: id || 'unknown',\n type: 'MENUS_RES',\n from: { type: 'data-agent' },\n to: {\n type: 'admin',\n id: clientId\n },\n payload: {\n ...res,\n }\n };\n\n sendMessage(response);\n}\n","import { T_RESPONSE, LLMProvider } from \"../types\";\n\n/**\n * Request type for DASH_COMP_REQ\n */\nexport type DashCompReqType = 'create' | 'update' | 'filter';\n\n/**\n * Response type for dash comp request\n */\nexport interface DashCompResponse extends T_RESPONSE {\n\tid?: string;\n\twsId?: string;\n}\n\n/**\n * Default models for DASH_COMP flow (can be overridden via config)\n * Using faster/cheaper models since DASH_COMP is a simpler task\n */\nexport const DEFAULT_DASH_COMP_MODELS: Record<LLMProvider, string> = {\n\tanthropic: 'anthropic/claude-haiku-4-5-20251001',\n\tgemini: 'gemini/gemini-3-flash-preview',\n\topenai: 'openai/gpt-4o-mini',\n\tgroq: 'groq/llama-3.3-70b-versatile'\n};\n","import { Component, LLMProvider, Tool, DashCompModelConfig, Message, T_RESPONSE } from \"../types\";\nimport { DEFAULT_DASH_COMP_MODELS } from \"./types\";\n\n/**\n * Get API key and model based on provider configuration\n */\nexport function getApiKeyAndModel(\n\tanthropicApiKey?: string,\n\tgroqApiKey?: string,\n\tgeminiApiKey?: string,\n\topenaiApiKey?: string,\n\tllmProviders?: LLMProvider[],\n\tdashCompModels?: DashCompModelConfig\n): { apiKey: string | undefined; model: string | undefined } {\n\tconst providers = llmProviders || ['anthropic', 'gemini', 'openai', 'groq'];\n\tlet apiKey: string | undefined;\n\tlet model: string | undefined;\n\n\t// Check if custom model is configured via dashCompModels\n\tif (dashCompModels?.model) {\n\t\tmodel = dashCompModels.model;\n\t\tconst modelProvider = model.split('/')[0] as LLMProvider;\n\n\t\tif (modelProvider === 'anthropic') apiKey = anthropicApiKey;\n\t\telse if (modelProvider === 'gemini') apiKey = geminiApiKey;\n\t\telse if (modelProvider === 'openai') apiKey = openaiApiKey;\n\t\telse if (modelProvider === 'groq') apiKey = groqApiKey;\n\t} else {\n\t\tfor (const provider of providers) {\n\t\t\tif (provider === 'anthropic' && anthropicApiKey) {\n\t\t\t\tapiKey = anthropicApiKey;\n\t\t\t\tmodel = DEFAULT_DASH_COMP_MODELS.anthropic;\n\t\t\t\tbreak;\n\t\t\t} else if (provider === 'gemini' && geminiApiKey) {\n\t\t\t\tapiKey = geminiApiKey;\n\t\t\t\tmodel = DEFAULT_DASH_COMP_MODELS.gemini;\n\t\t\t\tbreak;\n\t\t\t} else if (provider === 'openai' && openaiApiKey) {\n\t\t\t\tapiKey = openaiApiKey;\n\t\t\t\tmodel = DEFAULT_DASH_COMP_MODELS.openai;\n\t\t\t\tbreak;\n\t\t\t} else if (provider === 'groq' && groqApiKey) {\n\t\t\t\tapiKey = groqApiKey;\n\t\t\t\tmodel = DEFAULT_DASH_COMP_MODELS.groq;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn { apiKey, model };\n}\n\n/**\n * Format components for prompt\n */\nexport function formatComponentsForPrompt(components: Component[]): string {\n\tif (!components || components.length === 0) {\n\t\treturn 'No components available';\n\t}\n\tcomponents = components.filter(c => c.name !== 'MultiComponentContainer');\n\treturn components\n\t\t.map((comp, idx) => {\n\t\t\tconst keywords = comp.keywords ? comp.keywords.join(', ') : '';\n\t\t\tconst propsPreview = comp.props ? JSON.stringify(comp.props, null, 2) : 'No props';\n\t\t\treturn `${idx + 1}. ID: ${comp.id}\n Name: ${comp.name}\n Type: ${comp.type}\n Description: ${comp.description || 'No description'}\n Keywords: ${keywords}\n Props Structure: ${propsPreview}`;\n\t\t})\n\t\t.join('\\n\\n');\n}\n\n/**\n * Classify a tool so the prompt can render it under the right heading and the\n * LLM can apply the priority rule (direct tools beat SQL when both can answer).\n * `toolType: 'direct'` is the authoritative signal; the toolId suffix is a\n * fallback for source tools that don't set toolType.\n */\nfunction classifyTool(tool: Tool): 'direct' | 'sql' | 'rest' | 'graphql' {\n\tif (tool.toolType === 'direct') return 'direct';\n\tif (tool.id.endsWith('_query')) return 'sql';\n\tif (tool.id.endsWith('_call')) return 'rest';\n\tif (tool.id.endsWith('_graphql')) return 'graphql';\n\treturn 'direct';\n}\n\n/**\n * Format tools for prompt. Direct tools are listed first under their own\n * heading so the LLM applies them by default; SQL/REST/GraphQL source tools\n * follow as the fallback for questions no direct tool can answer.\n */\nexport function formatToolsForPrompt(tools?: Tool[]): string {\n\tif (!tools || tools.length === 0) {\n\t\treturn 'No external tools available.';\n\t}\n\tconst directTools: Tool[] = [];\n\tconst sourceTools: Tool[] = [];\n\tfor (const t of tools) {\n\t\t(classifyTool(t) === 'direct' ? directTools : sourceTools).push(t);\n\t}\n\n\tconst renderTool = (tool: Tool, idx: number, label: string) => {\n\t\tconst paramsStr = Object.entries(tool.params || {})\n\t\t\t.map(([key, type]) => `${key}: ${type}`)\n\t\t\t.join(', ');\n\t\treturn `${idx + 1}. [${label}] ID: ${tool.id}\n Name: ${tool.name}\n Description: ${tool.description}\n Parameters: { ${paramsStr} }`;\n\t};\n\n\tconst sections: string[] = [];\n\tif (directTools.length > 0) {\n\t\tconst body = directTools.map((t, i) => renderTool(t, i, 'DIRECT — PREFER')).join('\\n\\n');\n\t\tsections.push(`### Direct Tools (try these first — they answer most questions without SQL)\\n${body}`);\n\t}\n\tif (sourceTools.length > 0) {\n\t\tconst body = sourceTools\n\t\t\t.map((t, i) => {\n\t\t\t\tconst kind = classifyTool(t);\n\t\t\t\tconst label = kind === 'sql' ? 'SQL' : kind === 'rest' ? 'REST' : 'GRAPHQL';\n\t\t\t\treturn renderTool(t, i, label);\n\t\t\t})\n\t\t\t.join('\\n\\n');\n\t\tsections.push(`### Source Tools (fallback — use only when no direct tool fits)\\n${body}`);\n\t}\n\treturn sections.join('\\n\\n');\n}\n\n/**\n * Format existing components for filter prompt\n */\nexport function formatExistingComponentsForPrompt(existingComponents?: Component[]): string {\n\tif (!existingComponents || existingComponents.length === 0) {\n\t\treturn 'No existing components in dashboard';\n\t}\n\treturn JSON.stringify(existingComponents, null, 2);\n}\n\n/**\n * Send DASH_COMP_RES response message\n */\nexport function sendDashCompResponse(\n\tid: string,\n\tres: T_RESPONSE,\n\tsendMessage: (message: Message) => void,\n\tclientId?: string\n): void {\n\tconst response: Message = {\n\t\tid,\n\t\ttype: 'DASH_COMP_RES',\n\t\tfrom: { type: 'data-agent' },\n\t\tto: {\n\t\t\ttype: 'runtime',\n\t\t\tid: clientId\n\t\t},\n\t\tpayload: {\n\t\t\t...res\n\t\t}\n\t};\n\n\tsendMessage(response);\n}\n","import { Component, LLMProvider, T_RESPONSE, CollectionRegistry, Tool, DashCompModelConfig } from \"../types\";\nimport { logger } from \"../utils/logger\";\nimport { LLM } from \"../llm\";\nimport { promptLoader } from \"../userResponse/prompt-loader\";\nimport { schema } from \"../userResponse/schema\";\nimport { userPromptErrorLogger } from \"../utils/user-prompt-error-logger\";\nimport { getApiKeyAndModel, formatComponentsForPrompt, formatToolsForPrompt } from \"./utils\";\nimport { getCurrentDateTimeForPrompt } from \"../utils/datetime\";\nimport { QueryExecutionService } from \"../userResponse/services/query-execution-service\";\nimport { ensureQueryLimit, validateAndFixSqlQuery, getDbTypeFromToolId } from \"../userResponse/utils\";\nimport { MAX_COMPONENT_QUERY_LIMIT, DEFAULT_QUERY_LIMIT, MAX_QUERY_VALIDATION_RETRIES, KNOWLEDGE_BASE_TOP_K } from \"../userResponse/constants\";\nimport { queryCache } from \"../utils/query-cache\";\nimport { buildDirectToolCacheKey } from \"../utils/cache-key\";\nimport KB from \"../userResponse/knowledge-base\";\nimport { extractPromptText } from \"../userResponse/prompt-extractor\";\n\n/**\n * Pick a component and generate props using LLM\n * Supports both database queries and external tools for data viewing/modification\n */\nexport async function pickComponentWithLLM(\n\tprompt: string,\n\tcomponents: Component[],\n\tanthropicApiKey?: string,\n\tgroqApiKey?: string,\n\tgeminiApiKey?: string,\n\topenaiApiKey?: string,\n\tllmProviders?: LLMProvider[],\n\tcollections?: CollectionRegistry,\n\ttools?: Tool[],\n\tdashCompModels?: DashCompModelConfig,\n\tconversationHistory?: string,\n\tuserId?: string\n): Promise<T_RESPONSE> {\n\tconst errors: string[] = [];\n\n\t// Format available components for the prompt\n\tconst availableComponentsText = formatComponentsForPrompt(components);\n\n\t// Format available external tools for the prompt\n\tconst availableToolsText = formatToolsForPrompt(tools);\n\n\ttry {\n\t\t// Check if any tool is a database source tool (has 'sql' parameter)\n\t\tconst hasDatabaseSourceTool = tools?.some(tool => {\n\t\t\tconst params = tool.params || {};\n\t\t\treturn 'sql' in params || Object.keys(params).some(key => key.toLowerCase() === 'sql');\n\t\t}) ?? false;\n\n\t\t// Get database schema documentation - skip if database source tools are available\n\t\tlet schemaDoc: string;\n\t\tif (hasDatabaseSourceTool) {\n\t\t\tschemaDoc = 'Schema is available in the database source tool descriptions.';\n\t\t} else {\n\t\t\tschemaDoc = schema.generateSchemaDocumentation();\n\t\t}\n\n\t\t// Get database-specific SQL rules\n\t\tconst databaseRules = await promptLoader.loadDatabaseRules();\n\n\t\t// Knowledge base context\n\t\tlet globalKnowledgeBase = 'No global knowledge base available.';\n\t\tlet knowledgeBaseContext = 'No additional knowledge base context available.';\n\t\tif (collections) {\n\t\t\tconst kbResult = await KB.getAllKnowledgeBase({\n\t\t\t\tprompt,\n\t\t\t\tcollections,\n\t\t\t\tuserId,\n\t\t\t\ttopK: KNOWLEDGE_BASE_TOP_K,\n\t\t\t});\n\t\t\tglobalKnowledgeBase = kbResult.globalContext || globalKnowledgeBase;\n\t\t\tconst dynamicParts: string[] = [];\n\t\t\tif (kbResult.userContext) {\n\t\t\t\tdynamicParts.push('## User-Specific Knowledge Base\\n' + kbResult.userContext);\n\t\t\t}\n\t\t\tif (kbResult.queryContext) {\n\t\t\t\tdynamicParts.push('## Relevant Knowledge Base (Query-Matched)\\n' + kbResult.queryContext);\n\t\t\t}\n\t\t\tknowledgeBaseContext = dynamicParts.join('\\n\\n') || knowledgeBaseContext;\n\t\t}\n\n\t\t// Load prompts for dash-comp-picker\n\t\tconst prompts = await promptLoader.loadPrompts('dash-comp-picker', {\n\t\t\tUSER_PROMPT: prompt,\n\t\t\tAVAILABLE_COMPONENTS: availableComponentsText,\n\t\t\tSCHEMA_DOC: schemaDoc || 'No database schema available',\n\t\t\tDATABASE_RULES: databaseRules,\n\t\t\tAVAILABLE_TOOLS: availableToolsText,\n\t\t\tCURRENT_DATETIME: getCurrentDateTimeForPrompt(),\n\t\t\tCONVERSATION_HISTORY: conversationHistory || 'No previous conversation',\n\t\t\tGLOBAL_KNOWLEDGE_BASE: globalKnowledgeBase,\n\t\t\tKNOWLEDGE_BASE_CONTEXT: knowledgeBaseContext,\n\t\t});\n\n\t\tlogger.logLLMPrompt('dashCompPicker', 'system', extractPromptText(prompts.system));\n\t\tlogger.logLLMPrompt('dashCompPicker', 'user', prompts.user);\n\t\tlogger.debug('[DASH_COMP_REQ] Loaded dash-comp-picker prompts with schema and tools');\n\n\t\tconst { apiKey, model } = getApiKeyAndModel(\n\t\t\tanthropicApiKey, groqApiKey, geminiApiKey, openaiApiKey, llmProviders, dashCompModels\n\t\t);\n\n\t\tif (!apiKey || !model) {\n\t\t\terrors.push('No API key available for any LLM provider');\n\t\t\treturn { success: false, errors };\n\t\t}\n\n\t\tlogger.info(`[DASH_COMP_REQ] Using model: ${model}`);\n\n\t\t// Track executed tools for response\n\t\tconst executedTools: Array<{ id: string; name: string; params: any; result: any; outputSchema?: any }> = [];\n\n\t\t// Build LLM tool definitions from external tools\n\t\tconst llmTools = (tools || []).map(tool => {\n\t\t\tconst properties: Record<string, any> = {};\n\t\t\tconst required: string[] = [];\n\n\t\t\tObject.entries(tool.params || {}).forEach(([key, typeOrValue]) => {\n\t\t\t\tconst valueStr = String(typeOrValue).toLowerCase();\n\n\t\t\t\t// Extract type from description like \"string - SQL query\" or \"object (optional) - params\"\n\t\t\t\tlet schemaType = 'string';\n\t\t\t\tconst typeMatch = valueStr.match(/^(string|number|integer|boolean|array|object)\\b/);\n\t\t\t\tif (typeMatch) {\n\t\t\t\t\tschemaType = typeMatch[1];\n\t\t\t\t}\n\n\t\t\t\tconst isOptional = valueStr.includes('(optional)') || valueStr.includes('optional');\n\t\t\t\tconst description = typeof typeOrValue === 'string' ? typeOrValue : `Parameter: ${key}`;\n\n\t\t\t\tif (schemaType === 'array') {\n\t\t\t\t\tproperties[key] = { type: 'array', items: { type: 'string' }, description };\n\t\t\t\t} else if (schemaType === 'object') {\n\t\t\t\t\tproperties[key] = { type: 'object', description };\n\t\t\t\t} else {\n\t\t\t\t\tproperties[key] = { type: schemaType, description };\n\t\t\t\t}\n\n\t\t\t\tif (!isOptional) {\n\t\t\t\t\trequired.push(key);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\tname: tool.id,\n\t\t\t\tdescription: tool.description,\n\t\t\t\tinput_schema: {\n\t\t\t\t\ttype: 'object',\n\t\t\t\t\tproperties,\n\t\t\t\t\trequired: required.length > 0 ? required : undefined,\n\t\t\t\t},\n\t\t\t};\n\t\t});\n\n\t\t// Tool handler to execute external tools\n\t\tconst toolHandler = async (toolName: string, toolInput: any): Promise<string> => {\n\t\t\tconst tool = tools?.find(t => t.id === toolName);\n\t\t\tif (!tool) {\n\t\t\t\tthrow new Error(`Unknown tool: ${toolName}`);\n\t\t\t}\n\n\t\t\tlogger.info(`[DASH_COMP_REQ] Executing external tool: ${tool.name} (${tool.id})`);\n\t\t\tconst result = await tool.fn(toolInput);\n\n\t\t\texecutedTools.push({\n\t\t\t\tid: tool.id,\n\t\t\t\tname: tool.name,\n\t\t\t\tparams: toolInput,\n\t\t\t\tresult: result,\n\t\t\t\toutputSchema: tool.outputSchema\n\t\t\t});\n\n\t\t\tlogger.info(`[DASH_COMP_REQ] Tool ${tool.name} executed successfully`);\n\t\t\tconst resultJson = JSON.stringify(result, null, 2);\n\t\t\treturn resultJson + '\\n\\n[REMINDER: The above is tool output for verification. You MUST still respond with ONLY the JSON component selection object. Do NOT summarize or describe these results.]';\n\t\t};\n\n\t\t// Make LLM call with tool support\n\t\tconst result = await LLM.streamWithTools(\n\t\t\t{\n\t\t\t\tsys: prompts.system,\n\t\t\t\tuser: prompts.user\n\t\t\t},\n\t\t\tllmTools,\n\t\t\ttoolHandler,\n\t\t\t{\n\t\t\t\tmodel,\n\t\t\t\tmaxTokens: 8192,\n\t\t\t\ttemperature: 0.2,\n\t\t\t\tapiKey\n\t\t\t},\n\t\t\t5 // max iterations\n\t\t);\n\n\t\t// Parse the JSON response\n\t\tlet jsonMatch = result.match(/\\{[\\s\\S]*\\}/);\n\t\tlet parsedResult = jsonMatch ? (() => { try { return JSON.parse(jsonMatch![0]); } catch { return null; } })() : null;\n\n\t\t// Validate it has the required component structure\n\t\tconst isValidComponent = parsedResult && parsedResult.componentId && parsedResult.props;\n\n\t\t// If first attempt failed (prose response or invalid JSON), retry with tool data baked in\n\t\tif (!isValidComponent && executedTools.length > 0) {\n\t\t\tconst toolDataSummary = executedTools.map(t =>\n\t\t\t\t`Tool \"${t.name}\" was called with params ${JSON.stringify(t.params)} and returned:\\n${JSON.stringify(t.result, null, 2).substring(0, 5000)}`\n\t\t\t).join('\\n\\n');\n\n\t\t\tconst retryUserPrompt = `Original user request: ${prompt}\\n\\nThe following tool was already called and returned data:\\n${toolDataSummary}\\n\\nUsing this data, select the appropriate component and respond with ONLY the JSON component selection object. No explanation, no markdown, just JSON.`;\n\n\t\t\tconst retryResult = await LLM.text(\n\t\t\t\t{\n\t\t\t\t\tsys: prompts.system,\n\t\t\t\t\tuser: retryUserPrompt\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tmodel,\n\t\t\t\t\tmaxTokens: 4096,\n\t\t\t\t\ttemperature: 0.1,\n\t\t\t\t\tapiKey\n\t\t\t\t}\n\t\t\t);\n\n\t\t\tjsonMatch = retryResult.match(/\\{[\\s\\S]*\\}/);\n\t\t\tparsedResult = jsonMatch ? (() => { try { return JSON.parse(jsonMatch![0]); } catch { return null; } })() : null;\n\t\t}\n\n\t\tif (!parsedResult) {\n\t\t\terrors.push('Failed to parse LLM response as JSON');\n\t\t\terrors.push(`LLM Response: ${result}`);\n\t\t\tlogger.error(`[DASH_COMP_REQ] Failed to parse JSON from LLM response`);\n\t\t\treturn { success: false, errors };\n\t\t}\n\n\t\tlogger.info(`[DASH_COMP_REQ] Parsed component: ${parsedResult.componentName} (${parsedResult.componentId})`);\n\t\tlogger.file('[DASH_COMP_REQ] LLM response:', JSON.stringify(parsedResult, null, 2));\n\n\t\t// Validate response structure\n\t\tif (!parsedResult.componentId || !parsedResult.props) {\n\t\t\terrors.push('Invalid LLM response: missing componentId or props');\n\t\t\terrors.push(`LLM Response: ${result}`);\n\t\t\tlogger.error(`[DASH_COMP_REQ] Invalid structure - missing componentId: ${!parsedResult.componentId}, missing props: ${!parsedResult.props}`);\n\t\t\tuserPromptErrorLogger.logError('DASH_COMP_REQ', 'Invalid LLM response structure', {\n\t\t\t\tprompt,\n\t\t\t\tresult: parsedResult,\n\t\t\t\tmissingFields: { componentId: !parsedResult.componentId, props: !parsedResult.props }\n\t\t\t});\n\t\t\treturn { success: false, errors };\n\t\t}\n\n\t\t// Find the original component\n\t\tconst originalComponent = components.find(c => c.name === parsedResult.componentName);\n\t\tif (!originalComponent) {\n\t\t\terrors.push(`Component ${parsedResult.componentName} not found in available components`);\n\t\t\terrors.push(`LLM Response: ${result}`);\n\t\t\tuserPromptErrorLogger.logError('DASH_COMP_REQ', 'Component not found', {\n\t\t\t\tprompt,\n\t\t\t\tcomponentName: parsedResult.componentName,\n\t\t\t\tavailableComponentNames: components.map(c => c.name)\n\t\t\t});\n\t\t\treturn { success: false, errors };\n\t\t}\n\n\t\t// Build the final component with generated props\n\t\tlet finalComponent: Component = {\n\t\t\t...originalComponent,\n\t\t\tprops: {\n\t\t\t\t...originalComponent.props,\n\t\t\t\t...parsedResult.props\n\t\t\t}\n\t\t};\n\n\t\t// Ensure query limit before validation\n\t\tif (finalComponent.props?.query) {\n\t\t\tconst query = finalComponent.props.query as string | { sql: string; values?: any; params?: any };\n\t\t\tif (typeof query === 'string') {\n\t\t\t\tfinalComponent.props.query = ensureQueryLimit(query, DEFAULT_QUERY_LIMIT, MAX_COMPONENT_QUERY_LIMIT);\n\t\t\t} else if (query?.sql) {\n\t\t\t\tfinalComponent.props.query = {\n\t\t\t\t\t...query,\n\t\t\t\t\tsql: ensureQueryLimit(query.sql, DEFAULT_QUERY_LIMIT, MAX_COMPONENT_QUERY_LIMIT)\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\n\t\t// Ensure query limit for externalTool SQL parameters\n\t\tconst etSql = (finalComponent.props as any)?.externalTool?.parameters?.sql;\n\t\tif (etSql && typeof etSql === 'string') {\n\t\t\tconst etToolId = (finalComponent.props as any)?.externalTool?.toolId;\n\t\t\tconst etDbType = getDbTypeFromToolId(etToolId);\n\t\t\t(finalComponent.props as any).externalTool.parameters.sql = ensureQueryLimit(etSql, DEFAULT_QUERY_LIMIT, MAX_COMPONENT_QUERY_LIMIT, etDbType);\n\t\t}\n\n\t\tlogger.info(`[DASH_COMP_REQ] Successfully picked component: ${finalComponent.name} (${finalComponent.type})`);\n\n\t\t// Validate direct query prop if present (legacy path)\n\t\tif (finalComponent.props?.query && collections?.['database']?.['execute']) {\n\t\t\tlogger.info(`[DASH_COMP_REQ] Validating direct query for component: ${finalComponent.name}`);\n\n\t\t\tconst queryService = new QueryExecutionService({\n\t\t\t\tdefaultLimit: DEFAULT_QUERY_LIMIT,\n\t\t\t\tgetModelForTask: () => model,\n\t\t\t\tgetApiKey: () => apiKey,\n\t\t\t\tproviderName: 'DASH_COMP_REQ'\n\t\t\t});\n\n\t\t\ttry {\n\t\t\t\tconst validationResult = await queryService.validateSingleQuery(\n\t\t\t\t\tfinalComponent,\n\t\t\t\t\tcollections,\n\t\t\t\t\tapiKey\n\t\t\t\t);\n\n\t\t\t\tif (!validationResult.validated) {\n\t\t\t\t\tlogger.error(`[DASH_COMP_REQ] Query validation failed for component: ${finalComponent.name}`);\n\t\t\t\t\terrors.push(`Query validation failed for component ${finalComponent.name}. The generated SQL query could not be executed.`);\n\t\t\t\t\treturn {\n\t\t\t\t\t\tsuccess: false,\n\t\t\t\t\t\terrors,\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\treasoning: parsedResult.reasoning || 'Component selected but query validation failed',\n\t\t\t\t\t\t\trawResponse: parsedResult\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tif (validationResult.component) {\n\t\t\t\t\tfinalComponent = validationResult.component;\n\t\t\t\t}\n\n\t\t\t\tlogger.info(`[DASH_COMP_REQ] Query validated successfully for component: ${finalComponent.name}`);\n\t\t\t} catch (validationError) {\n\t\t\t\tconst validationErrorMsg = validationError instanceof Error ? validationError.message : String(validationError);\n\t\t\t\tlogger.error(`[DASH_COMP_REQ] Query validation error: ${validationErrorMsg}`);\n\t\t\t\terrors.push(`Query validation error: ${validationErrorMsg}`);\n\t\t\t\treturn {\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\terrors,\n\t\t\t\t\tdata: {\n\t\t\t\t\t\treasoning: parsedResult.reasoning || 'Component selected but query validation encountered an error',\n\t\t\t\t\t\trawResponse: parsedResult\n\t\t\t\t\t}\n\t\t\t\t};\n\t\t\t}\n\t\t} else if (finalComponent.props?.query && !collections?.['database']?.['execute']) {\n\t\t\tlogger.warn(`[DASH_COMP_REQ] Skipping query validation - database execute function not available`);\n\t\t}\n\n\t\t// Validate externalTool SQL via external-tools.execute with retry + LLM fix\n\t\tconst extToolId = (finalComponent.props as any)?.externalTool?.toolId;\n\t\tconst extToolName = (finalComponent.props as any)?.externalTool?.toolName;\n\t\tlet extToolSql = (finalComponent.props as any)?.externalTool?.parameters?.sql;\n\t\tif (extToolSql && extToolId && collections?.['external-tools']?.['execute']) {\n\t\t\tlogger.info(`[DASH_COMP_REQ] Validating externalTool SQL for component: ${finalComponent.name}`);\n\n\t\t\t// Find the tool to get schema for LLM fix\n\t\t\tconst matchedTool = tools?.find(t => t.id === extToolId);\n\t\t\tlet validated = false;\n\t\t\tlet attempts = 0;\n\n\t\t\twhile (attempts < MAX_QUERY_VALIDATION_RETRIES && !validated) {\n\t\t\t\tattempts++;\n\t\t\t\ttry {\n\t\t\t\t\tconst result = await collections['external-tools']['execute']({\n\t\t\t\t\t\ttoolId: extToolId, toolName: extToolName, sql: extToolSql, data: {},\n\t\t\t\t\t});\n\n\t\t\t\t\tif (result?.success === false || result?.error) {\n\t\t\t\t\t\tconst errorMsg = result?.error || 'Unknown error';\n\t\t\t\t\t\tthrow new Error(typeof errorMsg === 'string' ? errorMsg : JSON.stringify(errorMsg));\n\t\t\t\t\t}\n\n\t\t\t\t\t// Success — update component with (possibly fixed) SQL and cache the result\n\t\t\t\t\t(finalComponent.props as any).externalTool.parameters.sql = extToolSql;\n\t\t\t\t\tvalidated = true;\n\t\t\t\t\tconst cacheKey = extToolId ? `${extToolId}:${extToolSql}` : extToolSql;\n\t\t\t\t\tqueryCache.set(cacheKey, result?.data ?? result);\n\t\t\t\t\tlogger.info(`[DASH_COMP_REQ] externalTool SQL validated (attempt ${attempts}) - cached for frontend`);\n\t\t\t\t} catch (execError) {\n\t\t\t\t\tconst errorMsg = execError instanceof Error ? execError.message : String(execError);\n\t\t\t\t\tlogger.warn(`[DASH_COMP_REQ] externalTool SQL failed (attempt ${attempts}/${MAX_QUERY_VALIDATION_RETRIES}): ${errorMsg}`);\n\n\t\t\t\t\tif (attempts >= MAX_QUERY_VALIDATION_RETRIES) {\n\t\t\t\t\t\tlogger.error(`[DASH_COMP_REQ] Max retries reached for externalTool SQL validation`);\n\t\t\t\t\t\terrors.push(`SQL validation failed for component ${finalComponent.name}: ${errorMsg}`);\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tsuccess: false,\n\t\t\t\t\t\t\terrors,\n\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\treasoning: parsedResult.reasoning || 'Component selected but externalTool SQL validation failed after retries',\n\t\t\t\t\t\t\t\trawResponse: parsedResult\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\t// Ask LLM to fix the SQL\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst toolSchema = matchedTool?.description || 'Schema not available';\n\t\t\t\t\t\tconst databaseRules = await promptLoader.loadDatabaseRules();\n\n\t\t\t\t\t\tconst fixPrompt = `You are a SQL expert. Fix the following SQL query that failed execution.\n\n## Database Schema\n${toolSchema}\n\n## Database-Specific SQL Rules\n${databaseRules}\n\n## Component Context\n- Component Name: ${finalComponent.name}\n- Component Type: ${finalComponent.type}\n- Title: ${(finalComponent.props as any)?.title || 'N/A'}\n\n## Failed Query\n\\`\\`\\`sql\n${extToolSql}\n\\`\\`\\`\n\n## Error Message\n${errorMsg}\n\n## Instructions\n1. Analyze the error message and identify what caused the query to fail\n2. Fix the query to resolve the error while preserving the original intent\n3. Ensure the fixed query follows the database-specific SQL rules above\n4. Return ONLY the fixed SQL query, no explanations or markdown\n\nFixed SQL query:`;\n\n\t\t\t\t\t\tconst fixResponse = await LLM.text(\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tsys: 'You are a SQL expert. Return only the fixed SQL query with no additional text, explanations, or markdown formatting.',\n\t\t\t\t\t\t\t\tuser: fixPrompt,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tmodel: model || undefined,\n\t\t\t\t\t\t\t\tmaxTokens: 2048,\n\t\t\t\t\t\t\t\ttemperature: 0,\n\t\t\t\t\t\t\t\tapiKey,\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tlet fixedSql = fixResponse.trim();\n\t\t\t\t\t\tfixedSql = fixedSql.replace(/^```sql\\s*/i, '').replace(/\\s*```$/i, '');\n\t\t\t\t\t\tfixedSql = fixedSql.replace(/^```\\s*/i, '').replace(/\\s*```$/i, '');\n\n\t\t\t\t\t\tconst { query: validatedSql } = validateAndFixSqlQuery(fixedSql);\n\n\t\t\t\t\t\tif (validatedSql && validatedSql !== extToolSql) {\n\t\t\t\t\t\t\textToolSql = ensureQueryLimit(validatedSql, DEFAULT_QUERY_LIMIT, MAX_COMPONENT_QUERY_LIMIT, getDbTypeFromToolId(extToolId));\n\t\t\t\t\t\t\tlogger.info(`[DASH_COMP_REQ] LLM provided SQL fix, retrying...`);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tlogger.warn(`[DASH_COMP_REQ] LLM returned same or empty query, stopping retries`);\n\t\t\t\t\t\t\terrors.push(`SQL validation failed for component ${finalComponent.name}: ${errorMsg}`);\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tsuccess: false,\n\t\t\t\t\t\t\t\terrors,\n\t\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\t\treasoning: parsedResult.reasoning || 'Component selected but SQL fix failed',\n\t\t\t\t\t\t\t\t\trawResponse: parsedResult\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\t\t\t\t\t} catch (fixError) {\n\t\t\t\t\t\tconst fixMsg = fixError instanceof Error ? fixError.message : String(fixError);\n\t\t\t\t\t\tlogger.error(`[DASH_COMP_REQ] Failed to get LLM SQL fix: ${fixMsg}`);\n\t\t\t\t\t\terrors.push(`SQL validation failed for component ${finalComponent.name}: ${errorMsg}`);\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tsuccess: false,\n\t\t\t\t\t\t\terrors,\n\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\treasoning: parsedResult.reasoning || 'Component selected but SQL fix encountered an error',\n\t\t\t\t\t\t\t\trawResponse: parsedResult\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Replace SQL with queryId — only queryIds sent to frontend (same as agent flow)\n\t\tconst props: any = { ...finalComponent.props };\n\n\t\t// Handle externalTool.parameters.sql → replace with queryId\n\t\tconst sqlValue = props.externalTool?.parameters?.sql || props.externalTool?.parameters?.query;\n\t\tif (sqlValue) {\n\t\t\tconst { sql, query, ...restParams } = props.externalTool.parameters;\n\t\t\tconst toolIdForCache = props.externalTool?.toolId || '';\n\t\t\tconst cacheKeyForGet = toolIdForCache ? `${toolIdForCache}:${sqlValue}` : sqlValue;\n\t\t\tconst cachedData = queryCache.get(cacheKeyForGet);\n\t\t\tconst queryId = queryCache.storeQuery(sqlValue, cachedData);\n\t\t\tprops.externalTool = {\n\t\t\t\t...props.externalTool,\n\t\t\t\tparameters: { queryId, ...restParams },\n\t\t\t};\n\t\t\tlogger.info(`[DASH_COMP_REQ] Replaced externalTool SQL with queryId: ${queryId}`);\n\t\t}\n\n\t\t// Handle legacy direct props.query → replace with queryId\n\t\tif (props.query) {\n\t\t\tconst { query, ...restProps } = props;\n\t\t\tconst queryId = queryCache.storeQuery(query);\n\t\t\tObject.keys(props).forEach(k => delete props[k]);\n\t\t\tObject.assign(props, { ...restProps, queryId });\n\t\t\tlogger.info(`[DASH_COMP_REQ] Replaced direct query with queryId: ${queryId}`);\n\t\t}\n\n\t\tfinalComponent = { ...finalComponent, props };\n\n\t\t// Pre-populate the et-direct cache slot for direct tools so the frontend's\n\t\t// first render hits cache instead of re-executing the tool. SQL tools have\n\t\t// the queryId substitution above; direct tools key on the (toolId, params)\n\t\t// pair the frontend will dispatch, which is reproducible without a token.\n\t\tconst ext = (finalComponent.props as any)?.externalTool;\n\t\tif (ext?.toolId && !ext?.parameters?.sql) {\n\t\t\tconst matchedTool = tools?.find(t => t.id === ext.toolId);\n\t\t\tif (matchedTool && matchedTool.cache !== false) {\n\t\t\t\tconst match = executedTools.find(t => t.id === ext.toolId);\n\t\t\t\tif (match) {\n\t\t\t\t\tconst cacheKey = buildDirectToolCacheKey(ext.toolId, ext.parameters);\n\t\t\t\t\tqueryCache.set(cacheKey, { success: true, data: match.result });\n\t\t\t\t\tlogger.info(`[DASH_COMP_REQ] Pre-populated et-direct cache for ${ext.toolId}`);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Log data source info\n\t\tif (parsedResult.props.query) {\n\t\t\tlogger.info(`[DASH_COMP_REQ] Data source: Database query`);\n\t\t}\n\t\tif (parsedResult.props.externalTool) {\n\t\t\tlogger.info(`[DASH_COMP_REQ] Data source: External tool - ${parsedResult.props.externalTool.toolName}`);\n\t\t}\n\t\tif (executedTools.length > 0) {\n\t\t\tlogger.info(`[DASH_COMP_REQ] Executed ${executedTools.length} external tool(s): ${executedTools.map(t => t.name).join(', ')}`);\n\t\t}\n\n\t\treturn {\n\t\t\tsuccess: true,\n\t\t\tdata: {\n\t\t\t\tcomponent: finalComponent,\n\t\t\t\treasoning: parsedResult.reasoning || 'Component selected based on user prompt',\n\t\t\t\tdataSource: parsedResult.props.query ? 'database' : (parsedResult.props.externalTool ? 'external_tool' : 'none'),\n\t\t\t\tisUpdate: parsedResult.isUpdate || false,\n\t\t\t\texecutedTools: executedTools.length > 0 ? executedTools : undefined\n\t\t\t},\n\t\t\terrors: []\n\t\t};\n\t} catch (error) {\n\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\t\tlogger.error(`[DASH_COMP_REQ] Error picking component: ${errorMsg}`);\n\n\t\t// Log error with full details\n\t\tuserPromptErrorLogger.logError('DASH_COMP_REQ', error instanceof Error ? error : new Error(errorMsg), {\n\t\t\tprompt,\n\t\t\tcomponentsCount: components.length,\n\t\t\ttoolsCount: tools?.length || 0\n\t\t});\n\n\t\terrors.push(errorMsg);\n\t\treturn { success: false, errors };\n\t}\n}\n","import { Component, LLMProvider, T_RESPONSE, CollectionRegistry, Tool, DashCompModelConfig } from \"../types\";\nimport { logger } from \"../utils/logger\";\nimport { LLM } from \"../llm\";\nimport { promptLoader } from \"../userResponse/prompt-loader\";\nimport { schema } from \"../userResponse/schema\";\nimport { userPromptErrorLogger } from \"../utils/user-prompt-error-logger\";\nimport {\n\tgetApiKeyAndModel,\n\tformatComponentsForPrompt,\n\tformatToolsForPrompt,\n\tformatExistingComponentsForPrompt\n} from \"./utils\";\nimport { getCurrentDateTimeForPrompt } from \"../utils/datetime\";\nimport { queryCache } from \"../utils/query-cache\";\nimport { buildDirectToolCacheKey } from \"../utils/cache-key\";\nimport { ensureQueryLimit, getDbTypeFromToolId } from \"../userResponse/utils\";\nimport { DEFAULT_QUERY_LIMIT, MAX_COMPONENT_QUERY_LIMIT, KNOWLEDGE_BASE_TOP_K } from \"../userResponse/constants\";\nimport KB from \"../userResponse/knowledge-base\";\nimport { extractPromptText } from \"../userResponse/prompt-extractor\";\n\n/**\n * Create filter component and update existing components using LLM\n */\nexport async function createFilterWithLLM(\n\tprompt: string,\n\tcomponents: Component[],\n\texistingComponents: Component[],\n\tanthropicApiKey?: string,\n\tgroqApiKey?: string,\n\tgeminiApiKey?: string,\n\topenaiApiKey?: string,\n\tllmProviders?: LLMProvider[],\n\ttools?: Tool[],\n\tdashCompModels?: DashCompModelConfig,\n\tcollections?: CollectionRegistry,\n\tuserId?: string\n): Promise<T_RESPONSE> {\n\tconst errors: string[] = [];\n\n\ttry {\n\t\t// Filter to only include filter components (type starts with \"Filter\")\n\t\tconst filterComponents = components.filter(c => c.type.startsWith('Filter'));\n\n\t\tif (filterComponents.length === 0) {\n\t\t\terrors.push('No filter components available');\n\t\t\treturn { success: false, errors };\n\t\t}\n\n\t\tlogger.debug(`[DASH_COMP_REQ:FILTER] Found ${filterComponents.length} filter components`);\n\n\t\t// Check if any tool is a database source tool (has 'sql' parameter)\n\t\tconst hasDatabaseSourceTool = tools?.some(tool => {\n\t\t\tconst params = tool.params || {};\n\t\t\treturn 'sql' in params || Object.keys(params).some(key => key.toLowerCase() === 'sql');\n\t\t}) ?? false;\n\n\t\t// Get database schema documentation - skip if database source tools are available\n\t\tlet schemaDoc: string;\n\t\tif (hasDatabaseSourceTool) {\n\t\t\tschemaDoc = 'Schema is available in the database source tool descriptions.';\n\t\t} else {\n\t\t\tschemaDoc = schema.generateSchemaDocumentation();\n\t\t}\n\t\tconst databaseRules = await promptLoader.loadDatabaseRules();\n\n\t\t// Knowledge base context\n\t\tlet globalKnowledgeBase = 'No global knowledge base available.';\n\t\tlet knowledgeBaseContext = 'No additional knowledge base context available.';\n\t\tif (collections) {\n\t\t\tconst kbResult = await KB.getAllKnowledgeBase({\n\t\t\t\tprompt,\n\t\t\t\tcollections,\n\t\t\t\tuserId,\n\t\t\t\ttopK: KNOWLEDGE_BASE_TOP_K,\n\t\t\t});\n\t\t\tglobalKnowledgeBase = kbResult.globalContext || globalKnowledgeBase;\n\t\t\tconst dynamicParts: string[] = [];\n\t\t\tif (kbResult.userContext) {\n\t\t\t\tdynamicParts.push('## User-Specific Knowledge Base\\n' + kbResult.userContext);\n\t\t\t}\n\t\t\tif (kbResult.queryContext) {\n\t\t\t\tdynamicParts.push('## Relevant Knowledge Base (Query-Matched)\\n' + kbResult.queryContext);\n\t\t\t}\n\t\t\tknowledgeBaseContext = dynamicParts.join('\\n\\n') || knowledgeBaseContext;\n\t\t}\n\n\t\t// Load filter-specific prompt\n\t\tconst prompts = await promptLoader.loadPrompts('dash-filter-picker', {\n\t\t\tUSER_PROMPT: prompt,\n\t\t\tAVAILABLE_COMPONENTS: formatComponentsForPrompt(filterComponents),\n\t\t\tEXISTING_COMPONENTS: formatExistingComponentsForPrompt(existingComponents),\n\t\t\tSCHEMA_DOC: schemaDoc || 'No database schema available',\n\t\t\tDATABASE_RULES: databaseRules,\n\t\t\tAVAILABLE_TOOLS: formatToolsForPrompt(tools),\n\t\t\tCURRENT_DATETIME: getCurrentDateTimeForPrompt(),\n\t\t\tGLOBAL_KNOWLEDGE_BASE: globalKnowledgeBase,\n\t\t\tKNOWLEDGE_BASE_CONTEXT: knowledgeBaseContext,\n\t\t});\n\n\t\tlogger.logLLMPrompt('dashFilterPicker', 'system', extractPromptText(prompts.system));\n\t\tlogger.logLLMPrompt('dashFilterPicker', 'user', prompts.user);\n\n\t\tlogger.debug('[DASH_COMP_REQ:FILTER] Loaded dash-filter-picker prompts');\n\n\t\tconst { apiKey, model } = getApiKeyAndModel(\n\t\t\tanthropicApiKey, groqApiKey, geminiApiKey, openaiApiKey, llmProviders, dashCompModels\n\t\t);\n\n\t\tif (!apiKey || !model) {\n\t\t\terrors.push('No API key available for any LLM provider');\n\t\t\treturn { success: false, errors };\n\t\t}\n\n\t\tlogger.info(`[DASH_COMP_REQ:FILTER] Using model: ${model}`);\n\n\t\t// Track executed tools for response\n\t\tconst executedTools: Array<{ id: string; name: string; params: any; result: any; outputSchema?: any }> = [];\n\n\t\t// Build LLM tool definitions from external tools\n\t\tconst llmTools = (tools || []).map(tool => {\n\t\t\tconst properties: Record<string, any> = {};\n\t\t\tconst required: string[] = [];\n\n\t\t\tObject.entries(tool.params || {}).forEach(([key, typeOrValue]) => {\n\t\t\t\tconst valueStr = String(typeOrValue).toLowerCase();\n\n\t\t\t\tlet schemaType = 'string';\n\t\t\t\tconst typeMatch = valueStr.match(/^(string|number|integer|boolean|array|object)\\b/);\n\t\t\t\tif (typeMatch) {\n\t\t\t\t\tschemaType = typeMatch[1];\n\t\t\t\t}\n\n\t\t\t\tconst isOptional = valueStr.includes('(optional)') || valueStr.includes('optional');\n\t\t\t\tconst description = typeof typeOrValue === 'string' ? typeOrValue : `Parameter: ${key}`;\n\n\t\t\t\tif (schemaType === 'array') {\n\t\t\t\t\tproperties[key] = { type: 'array', items: { type: 'string' }, description };\n\t\t\t\t} else if (schemaType === 'object') {\n\t\t\t\t\tproperties[key] = { type: 'object', description };\n\t\t\t\t} else {\n\t\t\t\t\tproperties[key] = { type: schemaType, description };\n\t\t\t\t}\n\n\t\t\t\tif (!isOptional) {\n\t\t\t\t\trequired.push(key);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\tname: tool.id,\n\t\t\t\tdescription: tool.description,\n\t\t\t\tinput_schema: {\n\t\t\t\t\ttype: 'object',\n\t\t\t\t\tproperties,\n\t\t\t\t\trequired: required.length > 0 ? required : undefined,\n\t\t\t\t},\n\t\t\t};\n\t\t});\n\n\t\t// Tool handler to execute external tools\n\t\tconst toolHandler = async (toolName: string, toolInput: any): Promise<string> => {\n\t\t\tconst tool = tools?.find(t => t.id === toolName);\n\t\t\tif (!tool) {\n\t\t\t\tthrow new Error(`Unknown tool: ${toolName}`);\n\t\t\t}\n\n\t\t\tlogger.info(`[DASH_COMP_REQ:FILTER] Executing external tool: ${tool.name} (${tool.id})`);\n\t\t\tconst result = await tool.fn(toolInput);\n\n\t\t\texecutedTools.push({\n\t\t\t\tid: tool.id,\n\t\t\t\tname: tool.name,\n\t\t\t\tparams: toolInput,\n\t\t\t\tresult: result,\n\t\t\t\toutputSchema: tool.outputSchema\n\t\t\t});\n\n\t\t\tlogger.info(`[DASH_COMP_REQ:FILTER] Tool ${tool.name} executed successfully`);\n\t\t\treturn JSON.stringify(result, null, 2);\n\t\t};\n\n\t\t// Make LLM call with tool support\n\t\tconst rawResult = await LLM.streamWithTools(\n\t\t\t{ sys: prompts.system, user: prompts.user },\n\t\t\tllmTools,\n\t\t\ttoolHandler,\n\t\t\t{ model, maxTokens: 16384, temperature: 0.2, apiKey },\n\t\t\t5 // max iterations\n\t\t);\n\n\t\t// Parse the JSON response — strip markdown fences, try-catch like pick-component.ts\n\t\tconst cleaned = rawResult.replace(/```(?:json)?\\s*/g, '').replace(/```\\s*/g, '');\n\t\tconst jsonMatch = cleaned.match(/\\{[\\s\\S]*\\}/);\n\t\tconst result = jsonMatch ? (() => { try { return JSON.parse(jsonMatch[0]); } catch { return null; } })() : null;\n\n\t\tif (!result) {\n\t\t\terrors.push('Failed to parse LLM response as JSON');\n\t\t\terrors.push(`LLM Response: ${rawResult}`);\n\t\t\treturn { success: false, errors };\n\t\t}\n\n\t\tlogger.debug('[DASH_COMP_REQ:FILTER] LLM response received');\n\t\tlogger.file('[DASH_COMP_REQ:FILTER] LLM response:', JSON.stringify(result, null, 2));\n\n\t\t// Validate filter response structure\n\t\tif (!result.filterComponent) {\n\t\t\terrors.push('Invalid LLM response: missing filterComponent');\n\t\t\terrors.push(`LLM Response: ${rawResult}`);\n\t\t\tuserPromptErrorLogger.logError('DASH_COMP_REQ:FILTER', 'Invalid LLM response structure', {\n\t\t\t\tprompt, result, missingFields: { filterComponent: !result.filterComponent }\n\t\t\t});\n\t\t\treturn { success: false, errors };\n\t\t}\n\n\t\tlogger.info(`[DASH_COMP_REQ:FILTER] Successfully created filter: ${result.filterComponent.componentType}`);\n\t\tconsole.log(`[DASH_COMP_REQ:FILTER] Filter component props:`, JSON.stringify(result.filterComponent.props, null, 2));\n\t\tlogger.info(`[DASH_COMP_REQ:FILTER] Updated ${result.updatedComponents?.length || 0} components`);\n\t\tif (executedTools.length > 0) {\n\t\t\tlogger.info(`[DASH_COMP_REQ:FILTER] Executed ${executedTools.length} external tool(s): ${executedTools.map(t => t.name).join(', ')}`);\n\t\t}\n\n\t\t// Validate updated component SQL and replace with queryId (like pick-component does)\n\t\tconst updatedComponents = result.updatedComponents || [];\n\t\tfor (const comp of updatedComponents) {\n\t\t\tconst extTool = comp.props?.externalTool;\n\t\t\tif (!extTool?.toolId) continue;\n\n\t\t\t// Direct tool path: validate that the LLM only added params the tool\n\t\t\t// declares, then pre-execute with current parameters so the first\n\t\t\t// render after filter creation hits cache. SQL tools fall through to\n\t\t\t// the existing branch below.\n\t\t\tconst isDirectTool = !extTool?.parameters?.sql;\n\t\t\tif (isDirectTool) {\n\t\t\t\tconst directTool = tools?.find(t => t.id === extTool.toolId);\n\t\t\t\tif (!directTool) {\n\t\t\t\t\tlogger.warn(`[DASH_COMP_REQ:FILTER] direct tool ${extTool.toolId} not found in tool registry`);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tconst declared = new Set(Object.keys(directTool.params || {}));\n\t\t\t\tconst sentKeys = Object.keys(extTool.parameters || {});\n\t\t\t\tconst unknown = sentKeys.filter(k => !declared.has(k));\n\t\t\t\tif (unknown.length > 0) {\n\t\t\t\t\tlogger.warn(`[DASH_COMP_REQ:FILTER] dropping unknown params on ${extTool.toolId}: ${unknown.join(', ')}`);\n\t\t\t\t\tunknown.forEach(k => delete extTool.parameters[k]);\n\t\t\t\t}\n\t\t\t\tif (directTool.cache !== false) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst directResult = await directTool.fn(extTool.parameters || {});\n\t\t\t\t\t\tconst cacheKey = buildDirectToolCacheKey(extTool.toolId, extTool.parameters);\n\t\t\t\t\t\tqueryCache.set(cacheKey, { success: true, data: directResult });\n\t\t\t\t\t\tlogger.info(`[DASH_COMP_REQ:FILTER] direct tool ${extTool.toolId} validated and cached for component: ${comp.id}`);\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\tconst errMsg = err instanceof Error ? err.message : String(err);\n\t\t\t\t\t\tlogger.warn(`[DASH_COMP_REQ:FILTER] direct tool validation failed for ${extTool.toolId} on component ${comp.id}: ${errMsg}`);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (!extTool?.parameters?.sql) continue;\n\n\t\t\tlet sql = extTool.parameters.sql;\n\t\t\tconst defaultParams = extTool.parameters.params || {};\n\t\t\tconst toolId = extTool.toolId;\n\t\t\tconst toolName = extTool.toolName;\n\n\t\t\t// Enforce query limit\n\t\t\tsql = ensureQueryLimit(sql, DEFAULT_QUERY_LIMIT, MAX_COMPONENT_QUERY_LIMIT, getDbTypeFromToolId(toolId));\n\t\t\textTool.parameters.sql = sql;\n\n\t\t\t// Validate SQL with default params via external-tools.execute\n\t\t\tif (collections?.['external-tools']?.['execute']) {\n\t\t\t\ttry {\n\t\t\t\t\tconst valResult = await collections['external-tools']['execute']({\n\t\t\t\t\t\ttoolId, toolName, sql, ...defaultParams, data: {},\n\t\t\t\t\t});\n\n\t\t\t\t\tif (valResult?.success === false || valResult?.error) {\n\t\t\t\t\t\tconst errMsg = valResult?.error || 'Unknown error';\n\t\t\t\t\t\tlogger.warn(`[DASH_COMP_REQ:FILTER] SQL validation failed for component ${comp.id}: ${typeof errMsg === 'string' ? errMsg : JSON.stringify(errMsg)}`);\n\t\t\t\t\t\t// Don't fail the whole filter — just skip caching for this component\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\n\t\t\t\t\t// Cache validated result\n\t\t\t\t\tconst cacheKey = `${toolId}:${sql}`;\n\t\t\t\t\tqueryCache.set(cacheKey, valResult?.data ?? valResult);\n\t\t\t\t\tlogger.info(`[DASH_COMP_REQ:FILTER] SQL validated and cached for component: ${comp.id}`);\n\t\t\t\t} catch (err) {\n\t\t\t\t\tconst errMsg = err instanceof Error ? err.message : String(err);\n\t\t\t\t\tlogger.warn(`[DASH_COMP_REQ:FILTER] SQL validation error for component ${comp.id}: ${errMsg}`);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Replace SQL with queryId — SQL never reaches the frontend\n\t\t\tconst { sql: _sql, ...restParams } = extTool.parameters;\n\t\t\tconst cacheKey = `${toolId}:${sql}`;\n\t\t\tconst cachedData = queryCache.get(cacheKey);\n\t\t\tconst queryId = queryCache.storeQuery(sql, cachedData);\n\t\t\textTool.parameters = { queryId, ...restParams };\n\t\t\tlogger.info(`[DASH_COMP_REQ:FILTER] Replaced SQL with queryId: ${queryId} for component: ${comp.id}`);\n\t\t}\n\n\t\treturn {\n\t\t\tsuccess: true,\n\t\t\tdata: {\n\t\t\t\tfilterComponent: result.filterComponent,\n\t\t\t\tupdatedComponents,\n\t\t\t\tfilterBindings: result.filterBindings || {},\n\t\t\t\treasoning: result.reasoning || 'Filter created based on user prompt',\n\t\t\t\texecutedTools: executedTools.length > 0 ? executedTools : undefined\n\t\t\t},\n\t\t\terrors: []\n\t\t};\n\t} catch (error) {\n\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\t\tlogger.error(`[DASH_COMP_REQ:FILTER] Error creating filter: ${errorMsg}`);\n\t\tuserPromptErrorLogger.logError('DASH_COMP_REQ:FILTER', error instanceof Error ? error : new Error(errorMsg), {\n\t\t\tprompt, existingComponentsCount: existingComponents.length\n\t\t});\n\t\terrors.push(errorMsg);\n\t\treturn { success: false, errors };\n\t}\n}\n","import { logger } from '../utils/logger';\n\ninterface ConversationEntry {\n\tuserPrompt: string;\n\tcomponentSummary: string;\n\ttimestamp: number;\n}\n\ninterface DashboardHistory {\n\tentries: ConversationEntry[];\n\tlastAccessedAt: number;\n}\n\n/**\n * Build a scope key from userId and dashboardId.\n * Ensures per-user, per-dashboard isolation.\n */\nfunction buildScopeKey(userId: string | undefined, dashboardId: string): string {\n\treturn userId ? `${userId}::${dashboardId}` : dashboardId;\n}\n\n/**\n * Manages conversation history scoped per user + dashboard.\n * Each user-dashboard pair has its own isolated history that expires after a configurable TTL.\n */\nclass DashboardConversationHistory {\n\tprivate histories: Map<string, DashboardHistory> = new Map();\n\tprivate ttlMs: number = 30 * 60 * 1000; // Default: 30 minutes\n\tprivate maxEntries: number = 3;\n\tprivate cleanupInterval: NodeJS.Timeout | null = null;\n\n\tconstructor() {\n\t\tthis.startCleanup();\n\t}\n\n\t/**\n\t * Set the TTL for dashboard histories\n\t * @param minutes - TTL in minutes\n\t */\n\tsetTTL(minutes: number): void {\n\t\tthis.ttlMs = minutes * 60 * 1000;\n\t\tlogger.info(`[DashboardHistory] TTL set to ${minutes} minutes`);\n\t}\n\n\t/**\n\t * Set max entries per dashboard\n\t */\n\tsetMaxEntries(max: number): void {\n\t\tthis.maxEntries = max;\n\t}\n\n\t/**\n\t * Add a conversation entry for a user's dashboard\n\t */\n\taddEntry(dashboardId: string, userPrompt: string, componentSummary: string, userId?: string): void {\n\t\tconst key = buildScopeKey(userId, dashboardId);\n\t\tlet history = this.histories.get(key);\n\t\tif (!history) {\n\t\t\thistory = { entries: [], lastAccessedAt: Date.now() };\n\t\t\tthis.histories.set(key, history);\n\t\t}\n\n\t\thistory.entries.push({\n\t\t\tuserPrompt,\n\t\t\tcomponentSummary,\n\t\t\ttimestamp: Date.now(),\n\t\t});\n\n\t\t// Trim to max entries\n\t\tif (history.entries.length > this.maxEntries) {\n\t\t\thistory.entries = history.entries.slice(-this.maxEntries);\n\t\t}\n\n\t\thistory.lastAccessedAt = Date.now();\n\t\tlogger.debug(`[DashboardHistory] Added entry for ${key} (${history.entries.length} total)`);\n\t}\n\n\t/**\n\t * Get formatted conversation history for a user's dashboard\n\t */\n\tgetHistory(dashboardId: string, userId?: string): string {\n\t\tconst key = buildScopeKey(userId, dashboardId);\n\t\tconst history = this.histories.get(key);\n\t\tif (!history || history.entries.length === 0) {\n\t\t\treturn 'No previous conversation';\n\t\t}\n\n\t\t// Check if expired\n\t\tif (Date.now() - history.lastAccessedAt > this.ttlMs) {\n\t\t\tthis.histories.delete(key);\n\t\t\tlogger.debug(`[DashboardHistory] History expired for ${key}`);\n\t\t\treturn 'No previous conversation';\n\t\t}\n\n\t\t// Update access time\n\t\thistory.lastAccessedAt = Date.now();\n\n\t\tconst lines: string[] = [];\n\t\tfor (const entry of history.entries) {\n\t\t\tlines.push(`User:\\n ${entry.userPrompt}`);\n\t\t\tlines.push(`Assistant:\\n ${entry.componentSummary}`);\n\t\t\tlines.push('---');\n\t\t}\n\n\t\treturn lines.join('\\n').trim();\n\t}\n\n\t/**\n\t * Clear history for a specific user's dashboard\n\t */\n\tclearDashboard(dashboardId: string, userId?: string): void {\n\t\tconst key = buildScopeKey(userId, dashboardId);\n\t\tthis.histories.delete(key);\n\t\tlogger.debug(`[DashboardHistory] Cleared history for ${key}`);\n\t}\n\n\t/**\n\t * Clear all dashboard histories\n\t */\n\tclearAll(): void {\n\t\tthis.histories.clear();\n\t\tlogger.info(`[DashboardHistory] All histories cleared`);\n\t}\n\n\t/**\n\t * Start periodic cleanup of expired histories\n\t */\n\tprivate startCleanup(): void {\n\t\tthis.cleanupInterval = setInterval(() => {\n\t\t\tconst now = Date.now();\n\t\t\tlet expiredCount = 0;\n\n\t\t\tfor (const [key, history] of this.histories.entries()) {\n\t\t\t\tif (now - history.lastAccessedAt > this.ttlMs) {\n\t\t\t\t\tthis.histories.delete(key);\n\t\t\t\t\texpiredCount++;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (expiredCount > 0) {\n\t\t\t\tlogger.debug(`[DashboardHistory] Cleaned up ${expiredCount} expired histories`);\n\t\t\t}\n\t\t}, 5 * 60 * 1000); // Run every 5 minutes\n\t}\n\n\t/**\n\t * Stop cleanup interval (for graceful shutdown)\n\t */\n\tdestroy(): void {\n\t\tif (this.cleanupInterval) {\n\t\t\tclearInterval(this.cleanupInterval);\n\t\t\tthis.cleanupInterval = null;\n\t\t}\n\t\tthis.histories.clear();\n\t}\n}\n\n// Export singleton instance\nexport const dashboardConversationHistory = new DashboardConversationHistory();\n","import { Component, LLMProvider, Message, DashCompRequestMessageSchema, T_RESPONSE, CollectionRegistry, Tool, DashCompModelConfig } from \"../types\";\nimport { logger } from \"../utils/logger\";\nimport { llmUsageLogger } from \"../utils/llm-usage-logger\";\nimport { ZodError } from \"zod\";\n\nimport { DashCompReqType, DashCompResponse } from \"./types\";\nimport { sendDashCompResponse } from \"./utils\";\nimport { pickComponentWithLLM } from \"./pick-component\";\nimport { createFilterWithLLM } from \"./create-filter\";\nimport { dashboardConversationHistory } from \"./dashboard-conversation-history\";\n\n// Re-export types for external use\nexport * from \"./types\";\nexport { dashboardConversationHistory } from \"./dashboard-conversation-history\";\n\n/**\n * Process DASH_COMP_REQ request\n */\nexport const processDashCompRequest = async (\n\tdata: any,\n\tcomponents: Component[],\n\t_sendMessage: (message: Message) => void,\n\tanthropicApiKey?: string,\n\tgroqApiKey?: string,\n\tgeminiApiKey?: string,\n\topenaiApiKey?: string,\n\tllmProviders?: LLMProvider[],\n\tcollections?: CollectionRegistry,\n\ttools?: Tool[],\n\tdashCompModels?: DashCompModelConfig\n): Promise<DashCompResponse> => {\n\tconst errors: string[] = [];\n\n\t// Parse incoming message data\n\tlogger.debug('[DASH_COMP_REQ] Parsing incoming message data');\n\tconst parseResult = DashCompRequestMessageSchema.safeParse(data);\n\n\tif (!parseResult.success) {\n\t\tconst zodError = parseResult.error as ZodError;\n\t\tzodError.errors.forEach(err => {\n\t\t\terrors.push(`${err.path.join('.')}: ${err.message}`);\n\t\t});\n\t\treturn { success: false, errors };\n\t}\n\n\tconst dashCompRequest = parseResult.data;\n\tconst { id, payload } = dashCompRequest;\n\tconst prompt = payload.prompt;\n\tconst reqType: DashCompReqType = payload.req_type || 'create';\n\tconst existingComponents = payload.existingComponents || [];\n\tconst wsId = dashCompRequest.from.id || 'unknown';\n\tconst dashboardId = payload.dashboardId;\n\tconst userId = payload.userId;\n\n\t// Get conversation history scoped to this user + dashboard\n\tconst conversationHistory = dashboardId\n\t\t? dashboardConversationHistory.getHistory(dashboardId, userId)\n\t\t: undefined;\n\n\t// Reset log files for this request\n\tconst promptContext = `DASH_COMP[${reqType}]: ${prompt?.substring(0, 50)}${(prompt?.length || 0) > 50 ? '...' : ''}`;\n\tllmUsageLogger.resetLogFile(promptContext);\n\n\t// Validate required fields\n\tif (!prompt) {\n\t\terrors.push('Prompt is required');\n\t}\n\n\t// Validate filter request has existing components\n\tif (reqType === 'filter' && existingComponents.length === 0) {\n\t\terrors.push('Filter request requires existingComponents');\n\t}\n\n\tif (errors.length > 0) {\n\t\treturn { success: false, errors, id, wsId };\n\t}\n\n\tlogger.info(`[DASH_COMP_REQ] Processing ${reqType} request for prompt: \"${prompt.substring(0, 50)}...\"`);\n\tlogger.info(`[DASH_COMP_REQ] Available: ${components?.length || 0} components, ${tools?.length || 0} tools`);\n\tif (existingComponents.length > 0) {\n\t\tlogger.info(`[DASH_COMP_REQ] Existing components in dashboard: ${existingComponents.length}`);\n\t}\n\n\t// Check if components are available\n\tif (!components || components.length === 0) {\n\t\tlogger.warn('[DASH_COMP_REQ] No components available');\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\terrors: ['No components available. Please ensure components are loaded.'],\n\t\t\tid,\n\t\t\twsId\n\t\t};\n\t}\n\n\tlet llmResponse: T_RESPONSE;\n\n\t// Route based on request type\n\tif (reqType === 'filter') {\n\t\t// Filter request: create filter + update existing components\n\t\tllmResponse = await createFilterWithLLM(\n\t\t\tprompt,\n\t\t\tcomponents,\n\t\t\texistingComponents,\n\t\t\tanthropicApiKey,\n\t\t\tgroqApiKey,\n\t\t\tgeminiApiKey,\n\t\t\topenaiApiKey,\n\t\t\tllmProviders,\n\t\t\ttools,\n\t\t\tdashCompModels,\n\t\t\tcollections,\n\t\t\tuserId\n\t\t);\n\t} else {\n\t\t// Create or Update request: use dash-comp-picker\n\t\tllmResponse = await pickComponentWithLLM(\n\t\t\tprompt,\n\t\t\tcomponents,\n\t\t\tanthropicApiKey,\n\t\t\tgroqApiKey,\n\t\t\tgeminiApiKey,\n\t\t\topenaiApiKey,\n\t\t\tllmProviders,\n\t\t\tcollections,\n\t\t\ttools,\n\t\t\tdashCompModels,\n\t\t\tconversationHistory,\n\t\t\tuserId\n\t\t);\n\t}\n\n\t// Store conversation entry for this user's dashboard on success\n\tif (llmResponse.success && dashboardId && prompt) {\n\t\tconst comp = llmResponse.data?.component;\n\t\tlet componentSummary = 'No component generated';\n\t\tif (comp) {\n\t\t\tconst parts: string[] = [];\n\t\t\tif (comp.type) parts.push(`Component Type: ${comp.type}`);\n\t\t\tif (comp.name) parts.push(`Name: ${comp.name}`);\n\t\t\tif (comp.description) parts.push(`Description: ${comp.description}`);\n\t\t\tcomponentSummary = parts.join('\\n') || 'Component generated';\n\t\t}\n\t\tdashboardConversationHistory.addEntry(dashboardId, prompt, componentSummary, userId);\n\t}\n\n\t// Log session summary\n\tllmUsageLogger.logSessionSummary(`DASH_COMP[${reqType}]: ${prompt?.substring(0, 30)}`);\n\n\treturn {\n\t\tsuccess: llmResponse.success,\n\t\tdata: llmResponse.data,\n\t\terrors: llmResponse.errors,\n\t\tid,\n\t\twsId\n\t};\n};\n\n/**\n * Handle DASH_COMP_REQ WebSocket message\n */\nexport async function handleDashCompRequest(\n\tdata: any,\n\tcomponents: Component[],\n\tsendMessage: (message: Message) => void,\n\tanthropicApiKey?: string,\n\tgroqApiKey?: string,\n\tgeminiApiKey?: string,\n\topenaiApiKey?: string,\n\tllmProviders?: LLMProvider[],\n\tcollections?: CollectionRegistry,\n\ttools?: Tool[],\n\tdashCompModels?: DashCompModelConfig\n): Promise<void> {\n\tconst response = await processDashCompRequest(\n\t\tdata,\n\t\tcomponents,\n\t\tsendMessage,\n\t\tanthropicApiKey,\n\t\tgroqApiKey,\n\t\tgeminiApiKey,\n\t\topenaiApiKey,\n\t\tllmProviders,\n\t\tcollections,\n\t\ttools,\n\t\tdashCompModels\n\t);\n\n\t// Send response back via WebSocket\n\tsendDashCompResponse(\n\t\tresponse.id || data.id,\n\t\t{\n\t\t\tsuccess: response.success,\n\t\t\terrors: response.errors,\n\t\t\tdata: response.data\n\t\t},\n\t\tsendMessage,\n\t\tresponse.wsId || data.from?.id\n\t);\n\n\tlogger.info(`[DASH_COMP_REQ] Response sent to client ${response.wsId || data.from?.id}`);\n}\n","import { T_RESPONSE, LLMProvider } from \"../types\";\n\n/**\n * Response type for report comp request\n */\nexport interface ReportCompResponse extends T_RESPONSE {\n\tid?: string;\n\twsId?: string;\n}\n\n/**\n * Default models for REPORT_COMP flow\n * Using faster/cheaper models since report generation is a structured task\n */\nexport const DEFAULT_REPORT_COMP_MODELS: Record<LLMProvider, string> = {\n\tanthropic: 'anthropic/claude-haiku-4-5-20251001',\n\tgemini: 'gemini/gemini-3-flash-preview',\n\topenai: 'openai/gpt-4o-mini',\n\tgroq: 'groq/llama-3.3-70b-versatile'\n};\n","import { Component, LLMProvider, Tool, DashCompModelConfig, Message, T_RESPONSE } from \"../types\";\nimport { DEFAULT_REPORT_COMP_MODELS } from \"./types\";\n\n/**\n * Get API key and model based on provider configuration\n */\nexport function getApiKeyAndModel(\n\tanthropicApiKey?: string,\n\tgroqApiKey?: string,\n\tgeminiApiKey?: string,\n\topenaiApiKey?: string,\n\tllmProviders?: LLMProvider[],\n\tmodelConfig?: DashCompModelConfig\n): { apiKey: string | undefined; model: string | undefined } {\n\tconst providers = llmProviders || ['anthropic', 'gemini', 'openai', 'groq'];\n\tlet apiKey: string | undefined;\n\tlet model: string | undefined;\n\n\tif (modelConfig?.model) {\n\t\tmodel = modelConfig.model;\n\t\tconst modelProvider = model.split('/')[0] as LLMProvider;\n\n\t\tif (modelProvider === 'anthropic') apiKey = anthropicApiKey;\n\t\telse if (modelProvider === 'gemini') apiKey = geminiApiKey;\n\t\telse if (modelProvider === 'openai') apiKey = openaiApiKey;\n\t\telse if (modelProvider === 'groq') apiKey = groqApiKey;\n\t} else {\n\t\tfor (const provider of providers) {\n\t\t\tif (provider === 'anthropic' && anthropicApiKey) {\n\t\t\t\tapiKey = anthropicApiKey;\n\t\t\t\tmodel = DEFAULT_REPORT_COMP_MODELS.anthropic;\n\t\t\t\tbreak;\n\t\t\t} else if (provider === 'gemini' && geminiApiKey) {\n\t\t\t\tapiKey = geminiApiKey;\n\t\t\t\tmodel = DEFAULT_REPORT_COMP_MODELS.gemini;\n\t\t\t\tbreak;\n\t\t\t} else if (provider === 'openai' && openaiApiKey) {\n\t\t\t\tapiKey = openaiApiKey;\n\t\t\t\tmodel = DEFAULT_REPORT_COMP_MODELS.openai;\n\t\t\t\tbreak;\n\t\t\t} else if (provider === 'groq' && groqApiKey) {\n\t\t\t\tapiKey = groqApiKey;\n\t\t\t\tmodel = DEFAULT_REPORT_COMP_MODELS.groq;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn { apiKey, model };\n}\n\n/**\n * Format components for prompt\n */\nexport function formatComponentsForPrompt(components: Component[]): string {\n\tif (!components || components.length === 0) {\n\t\treturn 'No components available';\n\t}\n\tcomponents = components.filter(c => c.name !== 'MultiComponentContainer');\n\treturn components\n\t\t.map((comp, idx) => {\n\t\t\tconst keywords = comp.keywords ? comp.keywords.join(', ') : '';\n\t\t\tconst propsPreview = comp.props ? JSON.stringify(comp.props, null, 2) : 'No props';\n\t\t\treturn `${idx + 1}. ID: ${comp.id}\n Name: ${comp.name}\n Type: ${comp.type}\n Description: ${comp.description || 'No description'}\n Keywords: ${keywords}\n Props Structure: ${propsPreview}`;\n\t\t})\n\t\t.join('\\n\\n');\n}\n\n/**\n * Format tools for prompt\n */\nexport function formatToolsForPrompt(tools?: Tool[]): string {\n\tif (!tools || tools.length === 0) {\n\t\treturn 'No external tools available.';\n\t}\n\treturn tools\n\t\t.map((tool, idx) => {\n\t\t\tconst paramsStr = Object.entries(tool.params || {})\n\t\t\t\t.map(([key, type]) => `${key}: ${type}`)\n\t\t\t\t.join(', ');\n\t\t\treturn `${idx + 1}. ID: ${tool.id}\n Name: ${tool.name}\n Description: ${tool.description}\n Parameters: { ${paramsStr} }`;\n\t\t})\n\t\t.join('\\n\\n');\n}\n\n/**\n * Send REPORT_COMP_RES response message\n */\nexport function sendReportCompResponse(\n\tid: string,\n\tres: T_RESPONSE,\n\tsendMessage: (message: Message) => void,\n\tclientId?: string\n): void {\n\tconst response: Message = {\n\t\tid,\n\t\ttype: 'REPORT_COMP_RES',\n\t\tfrom: { type: 'data-agent' },\n\t\tto: {\n\t\t\ttype: 'runtime',\n\t\t\tid: clientId\n\t\t},\n\t\tpayload: {\n\t\t\t...res\n\t\t}\n\t};\n\n\tsendMessage(response);\n}\n","import { Component, LLMProvider, T_RESPONSE, CollectionRegistry, Tool, DashCompModelConfig } from \"../types\";\nimport { logger } from \"../utils/logger\";\nimport { LLM } from \"../llm\";\nimport { promptLoader } from \"../userResponse/prompt-loader\";\nimport { schema } from \"../userResponse/schema\";\nimport { userPromptErrorLogger } from \"../utils/user-prompt-error-logger\";\nimport { getApiKeyAndModel, formatComponentsForPrompt, formatToolsForPrompt } from \"./utils\";\nimport { getCurrentDateTimeForPrompt } from \"../utils/datetime\";\nimport { ensureQueryLimit, validateAndFixSqlQuery, getDbTypeFromToolId } from \"../userResponse/utils\";\nimport { MAX_COMPONENT_QUERY_LIMIT, DEFAULT_QUERY_LIMIT, MAX_QUERY_VALIDATION_RETRIES, KNOWLEDGE_BASE_TOP_K } from \"../userResponse/constants\";\nimport { queryCache } from \"../utils/query-cache\";\nimport KB from \"../userResponse/knowledge-base\";\nimport { extractPromptText } from \"../userResponse/prompt-extractor\";\n\n/**\n * Generate multiple report components using LLM with tool support.\n *\n * Similar to pickComponentWithLLM but:\n * - Uses report-comp-picker prompt (generates MULTIPLE components)\n * - Returns an array of components with layout metadata\n * - Validates all externalTool SQL queries\n */\nexport async function generateReportComponents(\n\tprompt: string,\n\tcomponents: Component[],\n\tanthropicApiKey?: string,\n\tgroqApiKey?: string,\n\tgeminiApiKey?: string,\n\topenaiApiKey?: string,\n\tllmProviders?: LLMProvider[],\n\tcollections?: CollectionRegistry,\n\ttools?: Tool[],\n\tmodelConfig?: DashCompModelConfig,\n\tconversationHistory?: string,\n\tuserId?: string\n): Promise<T_RESPONSE> {\n\tconst errors: string[] = [];\n\n\tconst availableComponentsText = formatComponentsForPrompt(components);\n\tconst availableToolsText = formatToolsForPrompt(tools);\n\n\ttry {\n\t\t// Check if any tool is a database source tool\n\t\tconst hasDatabaseSourceTool = tools?.some(tool => {\n\t\t\tconst params = tool.params || {};\n\t\t\treturn 'sql' in params || Object.keys(params).some(key => key.toLowerCase() === 'sql');\n\t\t}) ?? false;\n\n\t\tlet schemaDoc: string;\n\t\tif (hasDatabaseSourceTool) {\n\t\t\tschemaDoc = 'Schema is available in the database source tool descriptions.';\n\t\t} else {\n\t\t\tschemaDoc = schema.generateSchemaDocumentation();\n\t\t}\n\n\t\tconst databaseRules = await promptLoader.loadDatabaseRules();\n\n\t\t// Knowledge base context\n\t\tlet globalKnowledgeBase = 'No global knowledge base available.';\n\t\tlet knowledgeBaseContext = 'No additional knowledge base context available.';\n\t\tif (collections) {\n\t\t\tconst kbResult = await KB.getAllKnowledgeBase({\n\t\t\t\tprompt,\n\t\t\t\tcollections,\n\t\t\t\tuserId,\n\t\t\t\ttopK: KNOWLEDGE_BASE_TOP_K,\n\t\t\t});\n\t\t\tglobalKnowledgeBase = kbResult.globalContext || globalKnowledgeBase;\n\t\t\tconst dynamicParts: string[] = [];\n\t\t\tif (kbResult.userContext) {\n\t\t\t\tdynamicParts.push('## User-Specific Knowledge Base\\n' + kbResult.userContext);\n\t\t\t}\n\t\t\tif (kbResult.queryContext) {\n\t\t\t\tdynamicParts.push('## Relevant Knowledge Base (Query-Matched)\\n' + kbResult.queryContext);\n\t\t\t}\n\t\t\tknowledgeBaseContext = dynamicParts.join('\\n\\n') || knowledgeBaseContext;\n\t\t}\n\n\t\t// Load report-specific prompts\n\t\tconst prompts = await promptLoader.loadPrompts('report-comp-picker', {\n\t\t\tUSER_PROMPT: prompt,\n\t\t\tAVAILABLE_COMPONENTS: availableComponentsText,\n\t\t\tSCHEMA_DOC: schemaDoc || 'No database schema available',\n\t\t\tDATABASE_RULES: databaseRules,\n\t\t\tAVAILABLE_TOOLS: availableToolsText,\n\t\t\tCURRENT_DATETIME: getCurrentDateTimeForPrompt(),\n\t\t\tCONVERSATION_HISTORY: conversationHistory || 'No previous conversation',\n\t\t\tGLOBAL_KNOWLEDGE_BASE: globalKnowledgeBase,\n\t\t\tKNOWLEDGE_BASE_CONTEXT: knowledgeBaseContext,\n\t\t});\n\n\t\tlogger.logLLMPrompt('reportCompPicker', 'system', extractPromptText(prompts.system));\n\t\tlogger.logLLMPrompt('reportCompPicker', 'user', prompts.user);\n\t\tlogger.debug('[REPORT_COMP_REQ] Loaded report-comp-picker prompts with schema and tools');\n\n\t\tconst { apiKey, model } = getApiKeyAndModel(\n\t\t\tanthropicApiKey, groqApiKey, geminiApiKey, openaiApiKey, llmProviders, modelConfig\n\t\t);\n\n\t\tif (!apiKey || !model) {\n\t\t\terrors.push('No API key available for any LLM provider');\n\t\t\treturn { success: false, errors };\n\t\t}\n\n\t\tlogger.info(`[REPORT_COMP_REQ] Using model: ${model}`);\n\n\t\t// Track executed tools\n\t\tconst executedTools: Array<{ id: string; name: string; params: any; result: any; outputSchema?: any }> = [];\n\n\t\t// Build LLM tool definitions from external tools\n\t\tconst llmTools = (tools || []).map(tool => {\n\t\t\tconst properties: Record<string, any> = {};\n\t\t\tconst required: string[] = [];\n\n\t\t\tObject.entries(tool.params || {}).forEach(([key, typeOrValue]) => {\n\t\t\t\tconst valueStr = String(typeOrValue).toLowerCase();\n\t\t\t\tlet schemaType = 'string';\n\t\t\t\tconst typeMatch = valueStr.match(/^(string|number|integer|boolean|array|object)\\b/);\n\t\t\t\tif (typeMatch) schemaType = typeMatch[1];\n\n\t\t\t\tconst isOptional = valueStr.includes('(optional)') || valueStr.includes('optional');\n\t\t\t\tconst description = typeof typeOrValue === 'string' ? typeOrValue : `Parameter: ${key}`;\n\n\t\t\t\tif (schemaType === 'array') {\n\t\t\t\t\tproperties[key] = { type: 'array', items: { type: 'string' }, description };\n\t\t\t\t} else if (schemaType === 'object') {\n\t\t\t\t\tproperties[key] = { type: 'object', description };\n\t\t\t\t} else {\n\t\t\t\t\tproperties[key] = { type: schemaType, description };\n\t\t\t\t}\n\n\t\t\t\tif (!isOptional) required.push(key);\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\tname: tool.id,\n\t\t\t\tdescription: tool.description,\n\t\t\t\tinput_schema: {\n\t\t\t\t\ttype: 'object',\n\t\t\t\t\tproperties,\n\t\t\t\t\trequired: required.length > 0 ? required : undefined,\n\t\t\t\t},\n\t\t\t};\n\t\t});\n\n\t\t// Tool handler\n\t\tconst toolHandler = async (toolName: string, toolInput: any): Promise<string> => {\n\t\t\tconst tool = tools?.find(t => t.id === toolName);\n\t\t\tif (!tool) throw new Error(`Unknown tool: ${toolName}`);\n\n\t\t\tlogger.info(`[REPORT_COMP_REQ] Executing external tool: ${tool.name} (${tool.id})`);\n\t\t\tconst result = await tool.fn(toolInput);\n\n\t\t\texecutedTools.push({\n\t\t\t\tid: tool.id,\n\t\t\t\tname: tool.name,\n\t\t\t\tparams: toolInput,\n\t\t\t\tresult,\n\t\t\t\toutputSchema: tool.outputSchema\n\t\t\t});\n\n\t\t\tlogger.info(`[REPORT_COMP_REQ] Tool ${tool.name} executed successfully`);\n\t\t\tconst resultJson = JSON.stringify(result, null, 2);\n\t\t\treturn resultJson + '\\n\\n[REMINDER: The above is tool output for verification. You MUST still respond with ONLY the JSON report object containing multiple components. Do NOT summarize these results.]';\n\t\t};\n\n\t\t// LLM call with tool support\n\t\tconst result = await LLM.streamWithTools(\n\t\t\t{\n\t\t\t\tsys: prompts.system,\n\t\t\t\tuser: prompts.user\n\t\t\t},\n\t\t\tllmTools,\n\t\t\ttoolHandler,\n\t\t\t{\n\t\t\t\tmodel,\n\t\t\t\tmaxTokens: 16384,\n\t\t\t\ttemperature: 0.2,\n\t\t\t\tapiKey\n\t\t\t},\n\t\t\t5\n\t\t);\n\n\t\t// Parse JSON response\n\t\tlet jsonMatch = result.match(/\\{[\\s\\S]*\\}/);\n\t\tlet parsedResult = jsonMatch ? (() => { try { return JSON.parse(jsonMatch![0]); } catch { return null; } })() : null;\n\n\t\t// Retry if first attempt failed with tool data baked in\n\t\tif (!parsedResult?.components && executedTools.length > 0) {\n\t\t\tconst toolDataSummary = executedTools.map(t =>\n\t\t\t\t`Tool \"${t.name}\" was called with params ${JSON.stringify(t.params)} and returned:\\n${JSON.stringify(t.result, null, 2).substring(0, 5000)}`\n\t\t\t).join('\\n\\n');\n\n\t\t\tconst retryUserPrompt = `Original user request: ${prompt}\\n\\nThe following tools were already called and returned data:\\n${toolDataSummary}\\n\\nUsing this data, generate the complete report with multiple components. Respond with ONLY the JSON report object. No explanation, no markdown, just JSON.`;\n\n\t\t\tconst retryResult = await LLM.text(\n\t\t\t\t{\n\t\t\t\t\tsys: prompts.system,\n\t\t\t\t\tuser: retryUserPrompt\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tmodel,\n\t\t\t\t\tmaxTokens: 16384,\n\t\t\t\t\ttemperature: 0.1,\n\t\t\t\t\tapiKey\n\t\t\t\t}\n\t\t\t);\n\n\t\t\tjsonMatch = retryResult.match(/\\{[\\s\\S]*\\}/);\n\t\t\tparsedResult = jsonMatch ? (() => { try { return JSON.parse(jsonMatch![0]); } catch { return null; } })() : null;\n\t\t}\n\n\t\tif (!parsedResult || !parsedResult.components) {\n\t\t\terrors.push('Failed to parse LLM response as report JSON');\n\t\t\terrors.push(`LLM Response: ${result}`);\n\t\t\tlogger.error(`[REPORT_COMP_REQ] Failed to parse report JSON from LLM response`);\n\t\t\treturn { success: false, errors };\n\t\t}\n\n\t\tlogger.info(`[REPORT_COMP_REQ] LLM generated ${parsedResult.components.length} report components`);\n\n\t\t// Build final component objects\n\t\tconst finalComponents: Component[] = [];\n\t\tfor (const compData of parsedResult.components) {\n\t\t\tif (!compData.componentId || !compData.props) {\n\t\t\t\tlogger.warn(`[REPORT_COMP_REQ] Skipping component with missing componentId or props`);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst originalComponent = components.find(c => c.name === compData.componentName);\n\t\t\tif (!originalComponent) {\n\t\t\t\tlogger.warn(`[REPORT_COMP_REQ] Component ${compData.componentName} not found, skipping`);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tlet finalComp: Component = {\n\t\t\t\t...originalComponent,\n\t\t\t\tprops: {\n\t\t\t\t\t...originalComponent.props,\n\t\t\t\t\t...compData.props\n\t\t\t\t}\n\t\t\t};\n\n\t\t\t// Ensure query limits\n\t\t\tconst etSql = (finalComp.props as any)?.externalTool?.parameters?.sql;\n\t\t\tif (etSql && typeof etSql === 'string') {\n\t\t\t\tconst etToolId = (finalComp.props as any)?.externalTool?.toolId;\n\t\t\t\t(finalComp.props as any).externalTool.parameters.sql = ensureQueryLimit(etSql, DEFAULT_QUERY_LIMIT, MAX_COMPONENT_QUERY_LIMIT, getDbTypeFromToolId(etToolId));\n\t\t\t}\n\n\t\t\tfinalComponents.push(finalComp);\n\t\t}\n\n\t\tlogger.info(`[REPORT_COMP_REQ] Built ${finalComponents.length} final components`);\n\n\t\t// Validate externalTool SQL queries\n\t\tconst validatedComponents = await validateAllExternalToolQueries(\n\t\t\tfinalComponents, collections, tools, model, apiKey\n\t\t);\n\n\t\tlogger.info(`[REPORT_COMP_REQ] ${validatedComponents.length}/${finalComponents.length} components validated`);\n\n\t\t// Strip SQL from components — only queryIds sent to frontend\n\t\tconst securedComponents = validatedComponents.map(comp => {\n\t\t\tconst props: any = { ...comp.props };\n\n\t\t\t// Handle externalTool.parameters.sql → replace with queryId\n\t\t\tconst sqlValue = props.externalTool?.parameters?.sql || props.externalTool?.parameters?.query;\n\t\t\tif (sqlValue) {\n\t\t\t\tconst { sql, query, ...restParams } = props.externalTool.parameters;\n\t\t\t\tconst toolId = props.externalTool?.toolId || '';\n\t\t\t\tconst cacheKey = toolId ? `${toolId}:${sqlValue}` : sqlValue;\n\t\t\t\tconst cachedData = queryCache.get(cacheKey);\n\t\t\t\tconst queryId = queryCache.storeQuery(sqlValue, cachedData);\n\t\t\t\tprops.externalTool = {\n\t\t\t\t\t...props.externalTool,\n\t\t\t\t\tparameters: { queryId, ...restParams },\n\t\t\t\t};\n\t\t\t\tlogger.info(`[REPORT_COMP_REQ] Replaced SQL with queryId for ${comp.name}: ${queryId}`);\n\t\t\t}\n\n\t\t\t// Handle legacy direct props.query → replace with queryId\n\t\t\tif (props.query) {\n\t\t\t\tconst { query, ...restProps } = props;\n\t\t\t\tconst queryId = queryCache.storeQuery(query);\n\t\t\t\treturn { ...comp, props: { ...restProps, queryId } };\n\t\t\t}\n\n\t\t\treturn { ...comp, props };\n\t\t});\n\n\t\treturn {\n\t\t\tsuccess: true,\n\t\t\tdata: {\n\t\t\t\tcomponents: securedComponents,\n\t\t\t\treportTitle: parsedResult.reportTitle || 'Report',\n\t\t\t\treportDescription: parsedResult.reportDescription || '',\n\t\t\t\texecutedTools: executedTools.length > 0 ? executedTools : undefined\n\t\t\t},\n\t\t\terrors: []\n\t\t};\n\t} catch (error) {\n\t\tconst errorMsg = error instanceof Error ? error.message : String(error);\n\t\tlogger.error(`[REPORT_COMP_REQ] Error generating report: ${errorMsg}`);\n\n\t\tuserPromptErrorLogger.logError('REPORT_COMP_REQ', error instanceof Error ? error : new Error(errorMsg), {\n\t\t\tprompt,\n\t\t\tcomponentsCount: components.length,\n\t\t\ttoolsCount: tools?.length || 0\n\t\t});\n\n\t\terrors.push(errorMsg);\n\t\treturn { success: false, errors };\n\t}\n}\n\n/**\n * Validate all component externalTool SQL queries with retry + LLM fix.\n */\nasync function validateAllExternalToolQueries(\n\tcomponents: Component[],\n\tcollections?: CollectionRegistry,\n\ttools?: Tool[],\n\tmodel?: string,\n\tapiKey?: string\n): Promise<Component[]> {\n\tif (!collections?.['external-tools']?.['execute']) {\n\t\tlogger.warn(`[REPORT_COMP_REQ] external-tools.execute not available, skipping validation`);\n\t\treturn components;\n\t}\n\n\tconst validated: Component[] = [];\n\n\tfor (const comp of components) {\n\t\tconst extToolId = (comp.props as any)?.externalTool?.toolId;\n\t\tconst extToolName = (comp.props as any)?.externalTool?.toolName;\n\t\tconst extToolParams = (comp.props as any)?.externalTool?.parameters;\n\t\tlet extToolSql = extToolParams?.sql;\n\n\t\tif (!extToolId) {\n\t\t\t// No external tool (e.g., markdown components)\n\t\t\tvalidated.push(comp);\n\t\t\tcontinue;\n\t\t}\n\n\t\t// For non-SQL tools (REST API, GraphQL), prefetch data without SQL validation\n\t\tif (!extToolSql) {\n\t\t\ttry {\n\t\t\t\tconst result = await collections['external-tools']['execute']({\n\t\t\t\t\ttoolId: extToolId, toolName: extToolName, ...extToolParams, data: {},\n\t\t\t\t});\n\n\t\t\t\tif (result?.success !== false && !result?.error) {\n\t\t\t\t\tconst toolResult = result?.data ?? result;\n\t\t\t\t\tconst valueKey = (comp.props as any)?.config?.valueKey;\n\t\t\t\t\tconst isKpi = comp.type === 'KPICard' || comp.name === 'DynamicKPICard';\n\n\t\t\t\t\t// KPI cards read a single top-level aggregate (e.g. totalDeadstockValue,\n\t\t\t\t\t// totalBranches) that tools return alongside the rows array\n\t\t\t\t\t// ({ totalX, data: [...] }). Flattening to the rows array drops the\n\t\t\t\t\t// aggregate, so the card's data[0][valueKey] resolves to N/A. For KPI\n\t\t\t\t\t// cards whose valueKey is a top-level field, keep the aggregate object.\n\t\t\t\t\tlet dataArray: any[];\n\t\t\t\t\tif (\n\t\t\t\t\t\tisKpi && valueKey &&\n\t\t\t\t\t\ttoolResult && typeof toolResult === 'object' && !Array.isArray(toolResult) &&\n\t\t\t\t\t\ttoolResult[valueKey] !== undefined\n\t\t\t\t\t) {\n\t\t\t\t\t\tdataArray = [toolResult];\n\t\t\t\t\t} else {\n\t\t\t\t\t\tconst resultData = (toolResult as any)?.data ?? toolResult ?? [];\n\t\t\t\t\t\tdataArray = Array.isArray(resultData) ? resultData : [resultData];\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!(comp.props as any).config) {\n\t\t\t\t\t\t(comp.props as any).config = {};\n\t\t\t\t\t}\n\t\t\t\t\t(comp.props as any).config.data = dataArray;\n\n\t\t\t\t\tlogger.info(`[REPORT_COMP_REQ] ✓ ${comp.name} prefetched ${dataArray.length} ${dataArray.length === 1 && isKpi ? 'aggregate' : 'rows'} (non-SQL tool)`);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tlogger.warn(`[REPORT_COMP_REQ] ⚠ ${comp.name} non-SQL prefetch failed: ${err instanceof Error ? err.message : String(err)}`);\n\t\t\t}\n\t\t\tvalidated.push(comp);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst matchedTool = tools?.find(t => t.id === extToolId);\n\t\tlet isValid = false;\n\t\tlet attempts = 0;\n\n\t\twhile (attempts < MAX_QUERY_VALIDATION_RETRIES && !isValid) {\n\t\t\tattempts++;\n\t\t\ttry {\n\t\t\t\tconst result = await collections['external-tools']['execute']({\n\t\t\t\t\ttoolId: extToolId, toolName: extToolName, sql: extToolSql, data: {},\n\t\t\t\t});\n\n\t\t\t\tif (result?.success === false || result?.error) {\n\t\t\t\t\tconst errorMsg = result?.error || 'Unknown error';\n\t\t\t\t\tthrow new Error(typeof errorMsg === 'string' ? errorMsg : JSON.stringify(errorMsg));\n\t\t\t\t}\n\n\t\t\t\t// Success — update SQL, cache result, and embed prefetched data\n\t\t\t\t(comp.props as any).externalTool.parameters.sql = extToolSql;\n\t\t\t\tconst cacheKey = extToolId ? `${extToolId}:${extToolSql}` : extToolSql;\n\t\t\t\tqueryCache.set(cacheKey, result?.data ?? result);\n\n\t\t\t\t// Extract the data array from the result\n\t\t\t\tconst resultData = result?.data?.data ?? result?.data ?? [];\n\t\t\t\tconst dataArray = Array.isArray(resultData) ? resultData : [resultData];\n\n\t\t\t\tif (!(comp.props as any).config) {\n\t\t\t\t\t(comp.props as any).config = {};\n\t\t\t\t}\n\t\t\t\t(comp.props as any).config.data = dataArray;\n\n\t\t\t\tlogger.info(`[REPORT_COMP_REQ] ✓ ${comp.name} validated + cached + prefetched ${dataArray.length} rows (attempt ${attempts})`);\n\t\t\t\tvalidated.push(comp);\n\t\t\t\tisValid = true;\n\t\t\t} catch (execError) {\n\t\t\t\tconst errorMsg = execError instanceof Error ? execError.message : String(execError);\n\t\t\t\tlogger.warn(`[REPORT_COMP_REQ] ✗ ${comp.name} failed (attempt ${attempts}/${MAX_QUERY_VALIDATION_RETRIES}): ${errorMsg}`);\n\n\t\t\t\tif (attempts >= MAX_QUERY_VALIDATION_RETRIES) {\n\t\t\t\t\tlogger.error(`[REPORT_COMP_REQ] Max retries for ${comp.name}, excluding`);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\t// LLM fix\n\t\t\t\ttry {\n\t\t\t\t\tconst toolSchema = matchedTool?.description || 'Schema not available';\n\t\t\t\t\tconst databaseRules = await promptLoader.loadDatabaseRules();\n\n\t\t\t\t\tconst fixPrompt = `You are a SQL expert. Fix the following SQL query that failed execution.\n\n## Database Schema\n${toolSchema}\n\n## Database-Specific SQL Rules\n${databaseRules}\n\n## Component Context\n- Component Name: ${comp.name}\n- Component Type: ${comp.type}\n- Title: ${(comp.props as any)?.title || 'N/A'}\n\n## Failed Query\n\\`\\`\\`sql\n${extToolSql}\n\\`\\`\\`\n\n## Error Message\n${errorMsg}\n\n## Instructions\n1. Analyze the error and fix the query\n2. Ensure the fixed query follows database-specific SQL rules\n3. Return ONLY the fixed SQL query\n\nFixed SQL query:`;\n\n\t\t\t\t\tconst fixResponse = await LLM.text(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tsys: 'You are a SQL expert. Return only the fixed SQL query with no additional text.',\n\t\t\t\t\t\t\tuser: fixPrompt,\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{ model: model || undefined, maxTokens: 2048, temperature: 0, apiKey }\n\t\t\t\t\t);\n\n\t\t\t\t\tlet fixedSql = fixResponse.trim();\n\t\t\t\t\tfixedSql = fixedSql.replace(/^```sql\\s*/i, '').replace(/\\s*```$/i, '');\n\t\t\t\t\tfixedSql = fixedSql.replace(/^```\\s*/i, '').replace(/\\s*```$/i, '');\n\n\t\t\t\t\tconst { query: validatedSql } = validateAndFixSqlQuery(fixedSql);\n\n\t\t\t\t\tif (validatedSql && validatedSql !== extToolSql) {\n\t\t\t\t\t\textToolSql = ensureQueryLimit(validatedSql, DEFAULT_QUERY_LIMIT, MAX_COMPONENT_QUERY_LIMIT, getDbTypeFromToolId(extToolId));\n\t\t\t\t\t\tlogger.info(`[REPORT_COMP_REQ] LLM provided SQL fix for ${comp.name}, retrying...`);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tlogger.warn(`[REPORT_COMP_REQ] LLM returned same or empty query for ${comp.name}`);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t} catch (fixError) {\n\t\t\t\t\tconst fixMsg = fixError instanceof Error ? fixError.message : String(fixError);\n\t\t\t\t\tlogger.error(`[REPORT_COMP_REQ] Failed to get LLM SQL fix: ${fixMsg}`);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn validated;\n}\n","import { Component, LLMProvider, Message, T_RESPONSE, CollectionRegistry, Tool, DashCompModelConfig } from \"../types\";\nimport { logger } from \"../utils/logger\";\nimport { llmUsageLogger } from \"../utils/llm-usage-logger\";\n\nimport { ReportCompResponse } from \"./types\";\nimport { sendReportCompResponse } from \"./utils\";\nimport { generateReportComponents } from \"./generate-report\";\nimport { dashboardConversationHistory } from \"../dashComp/dashboard-conversation-history\";\n\n// Re-export types for external use\nexport * from \"./types\";\n\n/**\n * Process REPORT_COMP_REQ request\n */\nexport const processReportCompRequest = async (\n\tdata: any,\n\tcomponents: Component[],\n\t_sendMessage: (message: Message) => void,\n\tanthropicApiKey?: string,\n\tgroqApiKey?: string,\n\tgeminiApiKey?: string,\n\topenaiApiKey?: string,\n\tllmProviders?: LLMProvider[],\n\tcollections?: CollectionRegistry,\n\ttools?: Tool[],\n\tmodelConfig?: DashCompModelConfig\n): Promise<ReportCompResponse> => {\n\tconst errors: string[] = [];\n\n\t// Parse incoming message data — reuse the same payload structure\n\tconst id = data?.id;\n\tconst payload = data?.payload;\n\tconst prompt = payload?.prompt;\n\tconst wsId = data?.from?.id || 'unknown';\n\tconst reportId = payload?.reportId;\n\tconst userId = payload?.userId;\n\n\t// Get conversation history scoped to this user + report\n\tconst conversationHistory = reportId\n\t\t? dashboardConversationHistory.getHistory(reportId, userId)\n\t\t: undefined;\n\n\t// Reset log files for this request\n\tconst promptContext = `REPORT_COMP: ${prompt?.substring(0, 50)}${(prompt?.length || 0) > 50 ? '...' : ''}`;\n\tllmUsageLogger.resetLogFile(promptContext);\n\n\tif (!prompt) {\n\t\terrors.push('Prompt is required');\n\t\treturn { success: false, errors, id, wsId };\n\t}\n\n\tlogger.info(`[REPORT_COMP_REQ] Processing report request: \"${prompt.substring(0, 80)}...\"`);\n\tlogger.info(`[REPORT_COMP_REQ] Available: ${components?.length || 0} components, ${tools?.length || 0} tools`);\n\n\tif (!components || components.length === 0) {\n\t\tlogger.warn('[REPORT_COMP_REQ] No components available');\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\terrors: ['No components available. Please ensure components are loaded.'],\n\t\t\tid,\n\t\t\twsId\n\t\t};\n\t}\n\n\tconst llmResponse = await generateReportComponents(\n\t\tprompt,\n\t\tcomponents,\n\t\tanthropicApiKey,\n\t\tgroqApiKey,\n\t\tgeminiApiKey,\n\t\topenaiApiKey,\n\t\tllmProviders,\n\t\tcollections,\n\t\ttools,\n\t\tmodelConfig,\n\t\tconversationHistory,\n\t\tuserId\n\t);\n\n\t// Store conversation entry for this user's report on success\n\tif (llmResponse.success && reportId && prompt) {\n\t\tconst comps = llmResponse.data?.components;\n\t\tlet summary = 'No components generated';\n\t\tif (Array.isArray(comps) && comps.length > 0) {\n\t\t\tsummary = comps.map((c: any) => `${c.type || 'Component'}: ${c.name || 'unnamed'}`).join(', ');\n\t\t\tsummary = `Generated ${comps.length} components: ${summary}`;\n\t\t}\n\t\tdashboardConversationHistory.addEntry(reportId, prompt, summary, userId);\n\t}\n\n\tllmUsageLogger.logSessionSummary(`REPORT_COMP: ${prompt?.substring(0, 30)}`);\n\n\treturn {\n\t\tsuccess: llmResponse.success,\n\t\tdata: llmResponse.data,\n\t\terrors: llmResponse.errors,\n\t\tid,\n\t\twsId\n\t};\n};\n\n/**\n * Handle REPORT_COMP_REQ WebSocket message\n */\nexport async function handleReportCompRequest(\n\tdata: any,\n\tcomponents: Component[],\n\tsendMessage: (message: Message) => void,\n\tanthropicApiKey?: string,\n\tgroqApiKey?: string,\n\tgeminiApiKey?: string,\n\topenaiApiKey?: string,\n\tllmProviders?: LLMProvider[],\n\tcollections?: CollectionRegistry,\n\ttools?: Tool[],\n\tmodelConfig?: DashCompModelConfig\n): Promise<void> {\n\tconst response = await processReportCompRequest(\n\t\tdata,\n\t\tcomponents,\n\t\tsendMessage,\n\t\tanthropicApiKey,\n\t\tgroqApiKey,\n\t\tgeminiApiKey,\n\t\topenaiApiKey,\n\t\tllmProviders,\n\t\tcollections,\n\t\ttools,\n\t\tmodelConfig\n\t);\n\n\tsendReportCompResponse(\n\t\tresponse.id || data.id,\n\t\t{\n\t\t\tsuccess: response.success,\n\t\t\terrors: response.errors,\n\t\t\tdata: response.data\n\t\t},\n\t\tsendMessage,\n\t\tresponse.wsId || data.from?.id\n\t);\n\n\tlogger.info(`[REPORT_COMP_REQ] Response sent to client ${response.wsId || data.from?.id}`);\n}\n","/**\n * Schema Request Handler\n * Handles SCHEMA_REQ messages to return the database schema\n */\n\nimport type { Message } from '../types';\nimport { schema } from '../userResponse/schema';\nimport { logger } from '../utils/logger';\n\ninterface SchemaRequestPayload {\n /** If true, returns the formatted documentation string instead of raw JSON */\n formatted?: boolean;\n}\n\ninterface SchemaResponsePayload {\n success: boolean;\n error?: string;\n data?: {\n schema: any;\n formatted?: string;\n };\n}\n\n/**\n * Handle schema request\n * Returns the database schema from schema.json\n */\nexport async function handleSchemaRequest(\n message: any,\n sendMessage: (msg: Message) => void\n): Promise<void> {\n const startTime = Date.now();\n\n try {\n const payload = message.payload as SchemaRequestPayload;\n const formatted = payload?.formatted ?? false;\n\n logger.info(`[SchemaRequest] Processing schema request (formatted: ${formatted})`);\n\n // Get the schema\n const schemaData = schema.getSchema();\n\n if (!schemaData) {\n const response: Message = {\n id: message.id,\n type: 'SCHEMA_RES',\n from: { type: 'data-agent' },\n to: message.from,\n payload: {\n success: false,\n error: 'Schema not found or failed to load',\n } as SchemaResponsePayload,\n };\n sendMessage(response);\n return;\n }\n\n // Build response data\n const responseData: SchemaResponsePayload['data'] = {\n schema: schemaData,\n };\n\n // Include formatted documentation if requested\n if (formatted) {\n responseData.formatted = schema.generateSchemaDocumentation();\n }\n\n const executionMs = Date.now() - startTime;\n logger.info(`[SchemaRequest] Schema retrieved successfully in ${executionMs}ms`);\n\n const response: Message = {\n id: message.id,\n type: 'SCHEMA_RES',\n from: { type: 'data-agent' },\n to: message.from,\n payload: {\n success: true,\n data: responseData,\n } as SchemaResponsePayload,\n };\n\n sendMessage(response);\n } catch (error) {\n const errorMsg = error instanceof Error ? error.message : String(error);\n logger.error(`[SchemaRequest] Error: ${errorMsg}`);\n\n const response: Message = {\n id: message.id,\n type: 'SCHEMA_RES',\n from: { type: 'data-agent' },\n to: message.from,\n payload: {\n success: false,\n error: errorMsg,\n } as SchemaResponsePayload,\n };\n\n sendMessage(response);\n }\n}\n","import { groqLLM } from \"./groq\";\nimport { anthropicLLM } from \"./anthropic\";\nimport { geminiLLM } from \"./gemini\";\nimport { openaiLLM } from \"./openai\";\nimport { Component, LLMProvider, T_RESPONSE } from \"../types\";\nimport dotenv from 'dotenv';\nimport { logger } from \"../utils/logger\";\n\ndotenv.config({ quiet: true });\n\n// Export types and singletons for model strategy configuration\n// Note: ModelStrategy is exported from types.ts (defined there for SDK config)\nexport { TaskType, BaseLLMConfig } from './base-llm';\nexport { anthropicLLM } from './anthropic';\nexport { groqLLM } from './groq';\nexport { geminiLLM } from './gemini';\nexport { openaiLLM } from './openai';\n\n\n\n/**\n * Parse LLM_PROVIDERS from environment variable\n * Expects a stringified JSON array like: '[\"anthropic\",\"groq\"]'\n */\nexport function getLLMProviders(): LLMProvider[] {\n const envProviders = process.env.LLM_PROVIDERS;\n\n const DEFAULT_PROVIDERS: LLMProvider[] = ['anthropic', 'gemini', 'openai', 'groq'];\n if (!envProviders) {\n // Default to anthropic if not specified\n return DEFAULT_PROVIDERS;\n }\n\n try {\n const providers = JSON.parse(envProviders) as LLMProvider[];\n\n // Validate providers\n const validProviders = providers.filter(p => p === 'anthropic' || p === 'groq' || p === 'gemini' || p === 'openai');\n\n if (validProviders.length === 0) {\n return DEFAULT_PROVIDERS;\n }\n\n return validProviders;\n } catch (error) {\n logger.error('Failed to parse LLM_PROVIDERS, defaulting to [\"anthropic\"]:', error);\n return DEFAULT_PROVIDERS;\n }\n}\n\n/**\n * Method 1: Use Anthropic Claude LLM\n */\nexport const useAnthropicMethod = async (\n prompt: string,\n components: Component[],\n apiKey?: string,\n conversationHistory?: string,\n responseMode: 'component' | 'text' = 'component',\n streamCallback?: (chunk: string) => void,\n collections?: any,\n externalTools?: any[],\n userId?: string\n): Promise<T_RESPONSE> => {\n\n // Only check components for component mode\n if (responseMode === 'component' && components.length === 0) {\n const emptyMsg = 'Components not loaded in memory. Please ensure components are fetched first.';\n logger.error('[useAnthropicMethod] No components available');\n return { success: false, errors: [emptyMsg] };\n }\n\n const matchResult = await anthropicLLM.handleUserRequest(prompt, components, apiKey, conversationHistory, responseMode, streamCallback, collections, externalTools, userId);\n logger.info(`[useAnthropicMethod] Successfully generated ${responseMode} using Anthropic`);\n return matchResult;\n};\n\n/**\n * Method 2: Use Groq LLM\n */\nexport const useGroqMethod = async (\n prompt: string,\n components: Component[],\n apiKey?: string,\n conversationHistory?: string,\n responseMode: 'component' | 'text' = 'component',\n streamCallback?: (chunk: string) => void,\n collections?: any,\n externalTools?: any[],\n userId?: string\n): Promise<T_RESPONSE> => {\n logger.debug('[useGroqMethod] Initializing Groq LLM matching method');\n logger.debug(`[useGroqMethod] Response mode: ${responseMode}`);\n\n // Only check components for component mode\n if (responseMode === 'component' && components.length === 0) {\n const emptyMsg = 'Components not loaded in memory. Please ensure components are fetched first.';\n logger.error('[useGroqMethod] No components available');\n return { success: false, errors: [emptyMsg] };\n }\n\n logger.debug(`[useGroqMethod] Processing with ${components.length} components`);\n\n const matchResult = await groqLLM.handleUserRequest(prompt, components, apiKey, conversationHistory, responseMode, streamCallback, collections, externalTools, userId);\n logger.info(`[useGroqMethod] Successfully generated ${responseMode} using Groq`);\n return matchResult;\n}\n\n/**\n * Method 3: Use Google Gemini LLM\n */\nexport const useGeminiMethod = async (\n prompt: string,\n components: Component[],\n apiKey?: string,\n conversationHistory?: string,\n responseMode: 'component' | 'text' = 'component',\n streamCallback?: (chunk: string) => void,\n collections?: any,\n externalTools?: any[],\n userId?: string\n): Promise<T_RESPONSE> => {\n logger.debug('[useGeminiMethod] Initializing Gemini LLM matching method');\n logger.debug(`[useGeminiMethod] Response mode: ${responseMode}`);\n\n // Only check components for component mode\n if (responseMode === 'component' && components.length === 0) {\n const emptyMsg = 'Components not loaded in memory. Please ensure components are fetched first.';\n logger.error('[useGeminiMethod] No components available');\n return { success: false, errors: [emptyMsg] };\n }\n\n logger.debug(`[useGeminiMethod] Processing with ${components.length} components`);\n\n const matchResult = await geminiLLM.handleUserRequest(prompt, components, apiKey, conversationHistory, responseMode, streamCallback, collections, externalTools, userId);\n logger.info(`[useGeminiMethod] Successfully generated ${responseMode} using Gemini`);\n return matchResult;\n}\n\n/**\n * Method 4: Use OpenAI GPT LLM\n */\nexport const useOpenAIMethod = async (\n prompt: string,\n components: Component[],\n apiKey?: string,\n conversationHistory?: string,\n responseMode: 'component' | 'text' = 'component',\n streamCallback?: (chunk: string) => void,\n collections?: any,\n externalTools?: any[],\n userId?: string\n): Promise<T_RESPONSE> => {\n logger.debug('[useOpenAIMethod] Initializing OpenAI GPT matching method');\n logger.debug(`[useOpenAIMethod] Response mode: ${responseMode}`);\n\n // Only check components for component mode\n if (responseMode === 'component' && components.length === 0) {\n const emptyMsg = 'Components not loaded in memory. Please ensure components are fetched first.';\n logger.error('[useOpenAIMethod] No components available');\n return { success: false, errors: [emptyMsg] };\n }\n\n logger.debug(`[useOpenAIMethod] Processing with ${components.length} components`);\n\n const matchResult = await openaiLLM.handleUserRequest(prompt, components, apiKey, conversationHistory, responseMode, streamCallback, collections, externalTools, userId);\n logger.info(`[useOpenAIMethod] Successfully generated ${responseMode} using OpenAI`);\n return matchResult;\n}\n\n\n/**\n * Get user response with automatic fallback between LLM providers\n * Tries providers in order specified by LLM_PROVIDERS or passed parameter\n * @param responseMode - 'component' for component generation (default), 'text' for text responses\n * @param streamCallback - Optional callback function to receive text chunks as they stream (only for text mode)\n * @param collections - Collection registry for executing database queries (required for text mode with queries)\n * @param externalTools - Optional array of external tools (email, calendar, etc.) that can be called (only for text mode)\n */\nexport const get_user_response = async (\n prompt: string,\n components: Component[],\n anthropicApiKey?: string,\n groqApiKey?: string,\n geminiApiKey?: string,\n openaiApiKey?: string,\n llmProviders?: LLMProvider[],\n conversationHistory?: string,\n responseMode: 'component' | 'text' = 'component',\n streamCallback?: (chunk: string) => void,\n collections?: any,\n externalTools?: any[],\n userId?: string\n): Promise<T_RESPONSE> => {\n\n const providers = llmProviders || getLLMProviders();\n const errors: string[] = [];\n\n logger.info(`[get_user_response] LLM Provider order: [${providers.join(', ')}]`);\n\n for (let i = 0; i < providers.length; i++) {\n const provider = providers[i];\n const isLastProvider = i === providers.length - 1;\n\n logger.info(`[get_user_response] Attempting provider: ${provider} (${i + 1}/${providers.length})`);\n\n let result: T_RESPONSE;\n if (provider === 'anthropic') {\n result = await useAnthropicMethod(prompt, components, anthropicApiKey, conversationHistory, responseMode, streamCallback, collections, externalTools, userId);\n } else if (provider === 'groq') {\n result = await useGroqMethod(prompt, components, groqApiKey, conversationHistory, responseMode, streamCallback, collections, externalTools, userId);\n } else if (provider === 'gemini') {\n result = await useGeminiMethod(prompt, components, geminiApiKey, conversationHistory, responseMode, streamCallback, collections, externalTools, userId);\n } else if (provider === 'openai') {\n result = await useOpenAIMethod(prompt, components, openaiApiKey, conversationHistory, responseMode, streamCallback, collections, externalTools, userId);\n } else {\n logger.warn(`[get_user_response] Unknown provider: ${provider} - skipping`);\n errors.push(`Unknown provider: ${provider}`);\n continue; // Skip unknown providers\n }\n\n if (result.success) {\n logger.info(`[get_user_response] Success with provider: ${provider}`);\n return result;\n } else {\n // Collect errors from result\n const providerErrors = result.errors.map(err => `${provider}: ${err}`);\n errors.push(...providerErrors);\n logger.warn(`[get_user_response] Provider ${provider} returned unsuccessful result: ${result.errors.join(', ')}`);\n\n // If this is not the last provider, try the next one\n if (!isLastProvider) {\n logger.info('[get_user_response] Falling back to next provider...');\n }\n }\n }\n\n // All providers failed\n logger.error(`[get_user_response] All LLM providers failed. Errors: ${errors.join('; ')}`);\n\n return {\n success: false,\n errors\n };\n}\n\n","import fs from 'fs';\nimport path from 'path';\nimport os from 'os';\nimport { logger } from '../utils/logger';\nimport { UserSchema, UsersDataSchema, type User, type UsersData } from '../types';\n\n/**\n * UserManager class to handle CRUD operations on users with file persistence\n * and in-memory caching. Changes are synced to file periodically.\n */\nexport class UserManager {\n private users: User[] = [];\n private filePath: string;\n private hasChanged: boolean = false;\n private syncInterval: ReturnType<typeof setInterval> | null = null;\n private syncIntervalMs: number;\n private isInitialized: boolean = false;\n\n /**\n * Initialize UserManager with file path and sync interval\n * @param projectId - Project ID to use in file path (default: 'snowflake-dataset')\n * @param syncIntervalMs - Interval in milliseconds to sync changes to file (default: 5000ms)\n */\n constructor(projectId: string = 'snowflake-dataset', syncIntervalMs: number = 5000) {\n this.filePath = path.join(os.homedir(), '.superatom', 'projects', projectId, 'users.json');\n this.syncIntervalMs = syncIntervalMs;\n }\n\n /**\n * Initialize the UserManager by loading users from file and starting sync interval\n */\n async init(): Promise<void> {\n if (this.isInitialized) {\n return;\n }\n\n try {\n // Load users from file into memory\n await this.loadUsersFromFile();\n logger.info(`UserManager initialized with ${this.users.length} users`);\n\n // Start the sync interval\n this.startSyncInterval();\n this.isInitialized = true;\n } catch (error) {\n logger.error('Failed to initialize UserManager:', error);\n throw error;\n }\n }\n\n /**\n * Load users from the JSON file into memory\n */\n private async loadUsersFromFile(): Promise<void> {\n try {\n // Create directory structure if it doesn't exist\n const dir = path.dirname(this.filePath);\n if (!fs.existsSync(dir)) {\n logger.info(`Creating directory structure: ${dir}`);\n fs.mkdirSync(dir, { recursive: true });\n }\n\n // Create file with empty users array if it doesn't exist\n if (!fs.existsSync(this.filePath)) {\n logger.info(`Users file does not exist at ${this.filePath}, creating with empty users`);\n const initialData: UsersData = { users: [] };\n fs.writeFileSync(this.filePath, JSON.stringify(initialData, null, 4));\n this.users = [];\n this.hasChanged = false;\n return;\n }\n\n const fileContent = fs.readFileSync(this.filePath, 'utf-8');\n const rawData = JSON.parse(fileContent);\n\n // Validate using Zod schema\n const validatedData = UsersDataSchema.parse(rawData);\n this.users = validatedData.users;\n this.hasChanged = false;\n logger.debug(`Loaded ${this.users.length} users from file`);\n } catch (error) {\n logger.error('Failed to load users from file:', error);\n throw new Error(`Failed to load users from file: ${error instanceof Error ? error.message : 'Unknown error'}`);\n }\n }\n\n /**\n * Save users from memory to the JSON file\n */\n private async saveUsersToFile(): Promise<void> {\n if (!this.hasChanged) {\n return;\n }\n\n try {\n // Create directory if it doesn't exist\n const dir = path.dirname(this.filePath);\n if (!fs.existsSync(dir)) {\n fs.mkdirSync(dir, { recursive: true });\n }\n\n // Filter out wsIds before saving to file (wsIds are only stored in memory)\n const usersToSave = this.users.map(user => {\n const { wsIds, ...userWithoutWsIds } = user;\n return userWithoutWsIds;\n });\n\n const data: UsersData = { users: usersToSave };\n fs.writeFileSync(this.filePath, JSON.stringify(data, null, 4));\n\n this.hasChanged = false;\n logger.debug(`Synced ${this.users.length} users to file (wsIds excluded)`);\n } catch (error) {\n logger.error('Failed to save users to file:', error);\n throw new Error(`Failed to save users to file: ${error instanceof Error ? error.message : 'Unknown error'}`);\n }\n }\n\n /**\n * Start the periodic sync interval\n */\n private startSyncInterval(): void {\n if (this.syncInterval) {\n return;\n }\n\n this.syncInterval = setInterval(async () => {\n if (this.hasChanged) {\n try {\n await this.saveUsersToFile();\n logger.debug('Auto-sync: Users saved to file');\n } catch (error) {\n logger.error('Auto-sync failed:', error);\n }\n }\n }, this.syncIntervalMs);\n\n logger.debug(`Sync interval started (${this.syncIntervalMs}ms)`);\n }\n\n /**\n * Stop the periodic sync interval\n */\n public stopSyncInterval(): void {\n if (this.syncInterval) {\n clearInterval(this.syncInterval);\n this.syncInterval = null;\n logger.debug('Sync interval stopped');\n }\n }\n\n /**\n * Force sync users to file immediately\n */\n public async forceSync(): Promise<void> {\n await this.saveUsersToFile();\n }\n\n /**\n * Create a new user\n * @param user - User object to create\n * @returns The created user\n */\n public createUser(user: User): User {\n // Validate user data with Zod schema\n const validatedUser = UserSchema.parse(user);\n\n // Check for duplicate username\n if (this.users.some(u => u.username === validatedUser.username)) {\n throw new Error(`User with username ${validatedUser.username} already exists`);\n }\n\n // Check for duplicate email if email is provided\n if (validatedUser.email && this.users.some(u => u.email === validatedUser.email)) {\n throw new Error(`User with email ${validatedUser.email} already exists`);\n }\n\n this.users.push(validatedUser);\n this.hasChanged = true;\n logger.debug(`User created: ${validatedUser.username}`);\n\n return validatedUser;\n }\n\n /**\n * Read a user by username\n * @param username - Username to retrieve\n * @returns The user if found, undefined otherwise\n */\n public getUser(username: string): User | undefined {\n return this.users.find(u => u.username === username);\n }\n\n /**\n * Read a user by email\n * @param email - Email to retrieve\n * @returns The user if found, undefined otherwise\n */\n public getUserByEmail(email: string): User | undefined {\n return this.users.find(u => u.email === email);\n }\n\n /**\n * Find user by username or email\n * @param identifier - Username or email to search for\n * @returns The user if found, undefined otherwise\n */\n public getUserByUsernameOrEmail(identifier: string): User | undefined {\n return this.users.find(u => u.username === identifier || u.email === identifier);\n }\n\n /**\n * Read all users\n * @returns Array of all users\n */\n public getAllUsers(): User[] {\n return [...this.users];\n }\n\n /**\n * Find users by a predicate function\n * @param predicate - Function to filter users\n * @returns Array of matching users\n */\n public findUsers(predicate: (user: User) => boolean): User[] {\n return this.users.filter(predicate);\n }\n\n /**\n * Update an existing user by username\n * @param username - Username of user to update\n * @param updates - Partial user object with fields to update\n * @returns The updated user\n */\n public updateUser(username: string, updates: Partial<User>): User {\n const userIndex = this.users.findIndex(u => u.username === username);\n if (userIndex === -1) {\n throw new Error(`User with username ${username} not found`);\n }\n\n const updatedUser = { ...this.users[userIndex], ...updates };\n this.users[userIndex] = updatedUser;\n this.hasChanged = true;\n logger.debug(`User updated: ${username}`);\n\n return updatedUser;\n }\n\n /**\n * Delete a user by username\n * @param username - Username of user to delete\n * @returns true if user was deleted, false if not found\n */\n public deleteUser(username: string): boolean {\n const initialLength = this.users.length;\n this.users = this.users.filter(u => u.username !== username);\n\n if (this.users.length < initialLength) {\n this.hasChanged = true;\n logger.debug(`User deleted: ${username}`);\n return true;\n }\n return false;\n }\n\n /**\n * Delete all users\n */\n public deleteAllUsers(): void {\n if (this.users.length > 0) {\n this.users = [];\n this.hasChanged = true;\n logger.debug('All users deleted');\n }\n }\n\n /**\n * Get the count of users\n * @returns Number of users in memory\n */\n public getUserCount(): number {\n return this.users.length;\n }\n\n /**\n * Check if a user exists\n * @param username - Username to check\n * @returns true if user exists, false otherwise\n */\n public userExists(username: string): boolean {\n return this.users.some(u => u.username === username);\n }\n\n /**\n * Add a WebSocket ID to a user's wsIds array\n * @param username - Username to update\n * @param wsId - WebSocket ID to add\n * @returns true if successful, false if user not found\n */\n public addWsId(username: string, wsId: string): boolean {\n const user = this.getUser(username);\n if (!user) {\n return false;\n }\n\n if(!user.wsIds || !Array.isArray(user.wsIds)) {\n user.wsIds = [];\n }\n\n if (!user.wsIds.includes(wsId)) {\n user.wsIds.push(wsId);\n // this.hasChanged = true;\n logger.debug(`WebSocket ID added to user ${username}: ${wsId}`);\n }\n\n return true;\n }\n\n /**\n * Remove a WebSocket ID from a user's wsIds array\n * @param username - Username to update\n * @param wsId - WebSocket ID to remove\n * @returns true if successful, false if user not found\n */\n public removeWsId(username: string, wsId: string): boolean {\n const user = this.getUser(username);\n if (!user) {\n return false;\n }\n\n if(!user.wsIds || !Array.isArray(user.wsIds)) {\n return false;\n }\n\n const initialLength = user.wsIds.length;\n user.wsIds = user.wsIds.filter(id => id !== wsId);\n\n if (user.wsIds.length < initialLength) {\n // this.hasChanged = true;\n logger.debug(`WebSocket ID removed from user ${username}: ${wsId}`);\n }\n\n return true;\n }\n\n /**\n * Get the change status\n * @returns true if there are unsaved changes, false otherwise\n */\n public hasUnsavedChanges(): boolean {\n return this.hasChanged;\n }\n\n /**\n * Cleanup resources and stop sync interval\n */\n public async destroy(): Promise<void> {\n this.stopSyncInterval();\n // Final sync before cleanup\n if (this.hasChanged) {\n await this.saveUsersToFile();\n }\n logger.info('UserManager destroyed');\n }\n}\n","import fs from 'fs';\nimport path from 'path';\nimport os from 'os';\nimport { logger } from '../utils/logger';\nimport { DSLRendererProps, DSLRendererPropsSchema } from './types';\n\n/**\n * DashboardManager class to handle CRUD operations on dashboards\n * All operations read/write directly to files (no in-memory caching)\n */\nexport class DashboardManager {\n private dashboardsBasePath: string;\n private projectId: string;\n\n /**\n * Initialize DashboardManager with project ID\n * @param projectId - Project ID to use in file path\n */\n constructor(projectId: string = 'snowflake-dataset') {\n this.projectId = projectId;\n this.dashboardsBasePath = path.join(\n os.homedir(),\n '.superatom',\n 'projects',\n projectId,\n 'dashboards'\n );\n }\n\n /**\n * Get the file path for a specific dashboard\n * @param dashboardId - Dashboard ID\n * @returns Full path to dashboard data.json file\n */\n private getDashboardPath(dashboardId: string): string {\n return path.join(this.dashboardsBasePath, dashboardId, 'data.json');\n }\n\n /**\n * Create a new dashboard\n * @param dashboardId - Unique dashboard ID\n * @param dashboard - Dashboard data\n * @returns Created dashboard with metadata\n */\n createDashboard(dashboardId: string, dashboard: DSLRendererProps): DSLRendererProps {\n const dashboardPath = this.getDashboardPath(dashboardId);\n const dashboardDir = path.dirname(dashboardPath);\n\n // Check if dashboard already exists\n if (fs.existsSync(dashboardPath)) {\n throw new Error(`Dashboard '${dashboardId}' already exists`);\n }\n\n // Validate dashboard structure\n const validated = DSLRendererPropsSchema.parse(dashboard);\n\n // Create directory structure\n fs.mkdirSync(dashboardDir, { recursive: true });\n\n // Write dashboard to file\n fs.writeFileSync(dashboardPath, JSON.stringify(validated, null, 4));\n\n logger.info(`Dashboard created: ${dashboardId}`);\n return validated;\n }\n\n /**\n * Get a specific dashboard by ID\n * @param dashboardId - Dashboard ID\n * @returns Dashboard data or null if not found\n */\n getDashboard(dashboardId: string): DSLRendererProps | null {\n const dashboardPath = this.getDashboardPath(dashboardId);\n\n if (!fs.existsSync(dashboardPath)) {\n logger.warn(`Dashboard not found: ${dashboardId}`);\n return null;\n }\n\n try {\n const fileContent = fs.readFileSync(dashboardPath, 'utf-8');\n const dashboard = JSON.parse(fileContent) as DSLRendererProps;\n\n // Validate structure\n const validated = DSLRendererPropsSchema.parse(dashboard);\n return validated;\n } catch (error) {\n logger.error(`Failed to read dashboard ${dashboardId}:`, error);\n return null;\n }\n }\n\n /**\n * Get all dashboards\n * @returns Array of dashboard objects with their IDs\n */\n getAllDashboards(): Array<{ dashboardId: string; dashboard: DSLRendererProps }> {\n // Create base directory if it doesn't exist\n if (!fs.existsSync(this.dashboardsBasePath)) {\n fs.mkdirSync(this.dashboardsBasePath, { recursive: true });\n return [];\n }\n\n const dashboards: Array<{ dashboardId: string; dashboard: DSLRendererProps }> = [];\n\n try {\n const dashboardDirs = fs.readdirSync(this.dashboardsBasePath);\n\n for (const dashboardId of dashboardDirs) {\n const dashboardPath = this.getDashboardPath(dashboardId);\n\n if (fs.existsSync(dashboardPath)) {\n const dashboard = this.getDashboard(dashboardId);\n if (dashboard) {\n dashboards.push({ dashboardId, dashboard });\n }\n }\n }\n\n logger.debug(`Retrieved ${dashboards.length} dashboards`);\n return dashboards;\n } catch (error) {\n logger.error('Failed to get all dashboards:', error);\n return [];\n }\n }\n\n /**\n * Update an existing dashboard\n * @param dashboardId - Dashboard ID\n * @param dashboard - Updated dashboard data\n * @returns Updated dashboard or null if not found\n */\n updateDashboard(dashboardId: string, dashboard: DSLRendererProps): DSLRendererProps | null {\n const dashboardPath = this.getDashboardPath(dashboardId);\n\n if (!fs.existsSync(dashboardPath)) {\n logger.warn(`Dashboard not found for update: ${dashboardId}`);\n return null;\n }\n\n try {\n // Validate dashboard structure\n const validated = DSLRendererPropsSchema.parse(dashboard);\n\n // Write updated dashboard to file\n fs.writeFileSync(dashboardPath, JSON.stringify(validated, null, 4));\n\n logger.info(`Dashboard updated: ${dashboardId}`);\n return validated;\n } catch (error) {\n logger.error(`Failed to update dashboard ${dashboardId}:`, error);\n throw error;\n }\n }\n\n /**\n * Delete a dashboard\n * @param dashboardId - Dashboard ID\n * @returns True if deleted, false if not found\n */\n deleteDashboard(dashboardId: string): boolean {\n const dashboardPath = this.getDashboardPath(dashboardId);\n const dashboardDir = path.dirname(dashboardPath);\n\n if (!fs.existsSync(dashboardPath)) {\n logger.warn(`Dashboard not found for deletion: ${dashboardId}`);\n return false;\n }\n\n try {\n // Delete the entire dashboard directory\n fs.rmSync(dashboardDir, { recursive: true, force: true });\n\n logger.info(`Dashboard deleted: ${dashboardId}`);\n return true;\n } catch (error) {\n logger.error(`Failed to delete dashboard ${dashboardId}:`, error);\n return false;\n }\n }\n\n /**\n * Check if a dashboard exists\n * @param dashboardId - Dashboard ID\n * @returns True if dashboard exists, false otherwise\n */\n dashboardExists(dashboardId: string): boolean {\n const dashboardPath = this.getDashboardPath(dashboardId);\n return fs.existsSync(dashboardPath);\n }\n\n /**\n * Get dashboard count\n * @returns Number of dashboards\n */\n getDashboardCount(): number {\n if (!fs.existsSync(this.dashboardsBasePath)) {\n return 0;\n }\n\n try {\n const dashboardDirs = fs.readdirSync(this.dashboardsBasePath);\n return dashboardDirs.filter((dir) => {\n const dashboardPath = this.getDashboardPath(dir);\n return fs.existsSync(dashboardPath);\n }).length;\n } catch (error) {\n logger.error('Failed to get dashboard count:', error);\n return 0;\n }\n }\n}\n","import fs from 'fs';\nimport path from 'path';\nimport os from 'os';\nimport { logger } from '../utils/logger';\nimport { DSLRendererProps, DSLRendererPropsSchema } from './types';\n\n/**\n * ReportManager class to handle CRUD operations on reports\n * All operations read/write directly to files (no in-memory caching)\n */\nexport class ReportManager {\n private reportsBasePath: string;\n private projectId: string;\n\n /**\n * Initialize ReportManager with project ID\n * @param projectId - Project ID to use in file path\n */\n constructor(projectId: string = 'snowflake-dataset') {\n this.projectId = projectId;\n this.reportsBasePath = path.join(\n os.homedir(),\n '.superatom',\n 'projects',\n projectId,\n 'reports'\n );\n }\n\n /**\n * Get the file path for a specific report\n * @param reportId - Report ID\n * @returns Full path to report data.json file\n */\n private getReportPath(reportId: string): string {\n return path.join(this.reportsBasePath, reportId, 'data.json');\n }\n\n /**\n * Create a new report\n * @param reportId - Unique report ID\n * @param report - Report data\n * @returns Created report with metadata\n */\n createReport(reportId: string, report: DSLRendererProps): DSLRendererProps {\n const reportPath = this.getReportPath(reportId);\n const reportDir = path.dirname(reportPath);\n\n // Check if report already exists\n if (fs.existsSync(reportPath)) {\n throw new Error(`Report '${reportId}' already exists`);\n }\n\n // Validate report structure\n const validated = DSLRendererPropsSchema.parse(report);\n\n // Create directory structure\n fs.mkdirSync(reportDir, { recursive: true });\n\n // Write report to file\n fs.writeFileSync(reportPath, JSON.stringify(validated, null, 4));\n\n logger.info(`Report created: ${reportId}`);\n return validated;\n }\n\n /**\n * Get a specific report by ID\n * @param reportId - Report ID\n * @returns Report data or null if not found\n */\n getReport(reportId: string): DSLRendererProps | null {\n const reportPath = this.getReportPath(reportId);\n\n if (!fs.existsSync(reportPath)) {\n logger.warn(`Report not found: ${reportId}`);\n return null;\n }\n\n try {\n const fileContent = fs.readFileSync(reportPath, 'utf-8');\n const report = JSON.parse(fileContent) as DSLRendererProps;\n\n // Validate structure\n const validated = DSLRendererPropsSchema.parse(report);\n return validated;\n } catch (error) {\n logger.error(`Failed to read report ${reportId}:`, error);\n return null;\n }\n }\n\n /**\n * Get all reports\n * @returns Array of report objects with their IDs\n */\n getAllReports(): Array<{ reportId: string; report: DSLRendererProps }> {\n // Create base directory if it doesn't exist\n if (!fs.existsSync(this.reportsBasePath)) {\n fs.mkdirSync(this.reportsBasePath, { recursive: true });\n return [];\n }\n\n const reports: Array<{ reportId: string; report: DSLRendererProps }> = [];\n\n try {\n const reportDirs = fs.readdirSync(this.reportsBasePath);\n\n for (const reportId of reportDirs) {\n const reportPath = this.getReportPath(reportId);\n\n if (fs.existsSync(reportPath)) {\n const report = this.getReport(reportId);\n if (report) {\n reports.push({ reportId, report });\n }\n }\n }\n\n logger.debug(`Retrieved ${reports.length} reports`);\n return reports;\n } catch (error) {\n logger.error('Failed to get all reports:', error);\n return [];\n }\n }\n\n /**\n * Update an existing report\n * @param reportId - Report ID\n * @param report - Updated report data\n * @returns Updated report or null if not found\n */\n updateReport(reportId: string, report: DSLRendererProps): DSLRendererProps | null {\n const reportPath = this.getReportPath(reportId);\n\n if (!fs.existsSync(reportPath)) {\n logger.warn(`Report not found for update: ${reportId}`);\n return null;\n }\n\n try {\n // Validate report structure\n const validated = DSLRendererPropsSchema.parse(report);\n\n // Write updated report to file\n fs.writeFileSync(reportPath, JSON.stringify(validated, null, 4));\n\n logger.info(`Report updated: ${reportId}`);\n return validated;\n } catch (error) {\n logger.error(`Failed to update report ${reportId}:`, error);\n throw error;\n }\n }\n\n /**\n * Delete a report\n * @param reportId - Report ID\n * @returns True if deleted, false if not found\n */\n deleteReport(reportId: string): boolean {\n const reportPath = this.getReportPath(reportId);\n const reportDir = path.dirname(reportPath);\n\n if (!fs.existsSync(reportPath)) {\n logger.warn(`Report not found for deletion: ${reportId}`);\n return false;\n }\n\n try {\n // Delete the entire report directory\n fs.rmSync(reportDir, { recursive: true, force: true });\n\n logger.info(`Report deleted: ${reportId}`);\n return true;\n } catch (error) {\n logger.error(`Failed to delete report ${reportId}:`, error);\n return false;\n }\n }\n\n /**\n * Check if a report exists\n * @param reportId - Report ID\n * @returns True if report exists, false otherwise\n */\n reportExists(reportId: string): boolean {\n const reportPath = this.getReportPath(reportId);\n return fs.existsSync(reportPath);\n }\n\n /**\n * Get report count\n * @returns Number of reports\n */\n getReportCount(): number {\n if (!fs.existsSync(this.reportsBasePath)) {\n return 0;\n }\n\n try {\n const reportDirs = fs.readdirSync(this.reportsBasePath);\n return reportDirs.filter((dir) => {\n const reportPath = this.getReportPath(dir);\n return fs.existsSync(reportPath);\n }).length;\n } catch (error) {\n logger.error('Failed to get report count:', error);\n return 0;\n }\n }\n}\n","import { Message } from '../types';\nimport { logger, type LogLevel } from './logger';\n\nexport interface CapturedLog {\n timestamp: number;\n level: 'info' | 'error' | 'warn' | 'debug';\n message: string;\n type?: 'explanation' | 'query' | 'general';\n data?: Record<string, any>;\n}\n\n/**\n * Log level hierarchy for filtering\n */\nconst LOG_LEVEL_PRIORITY: Record<LogLevel, number> = {\n errors: 0,\n warnings: 1,\n info: 2,\n verbose: 3,\n};\n\nconst MESSAGE_LEVEL_PRIORITY: Record<'error' | 'warn' | 'info' | 'debug', number> = {\n error: 0,\n warn: 1,\n info: 2,\n debug: 3,\n};\n\n/**\n * UILogCollector captures logs during user prompt processing\n * and sends them to runtime via ui_logs message with uiBlockId as the message id\n * Logs are sent in real-time for streaming effect in the UI\n * Respects the global log level configuration\n */\nexport class UILogCollector {\n private logs: CapturedLog[] = [];\n private uiBlockId: string | null;\n private clientId: string;\n private sendMessage: (message: Message) => void;\n private currentLogLevel: LogLevel;\n\n constructor(\n clientId: string,\n sendMessage: (message: Message) => void,\n uiBlockId?: string\n ) {\n this.uiBlockId = uiBlockId || null;\n this.clientId = clientId;\n this.sendMessage = sendMessage;\n this.currentLogLevel = logger.getLogLevel();\n }\n\n /**\n * Check if logging is enabled (uiBlockId is provided)\n */\n isEnabled(): boolean {\n return this.uiBlockId !== null;\n }\n\n /**\n * Check if a message should be logged based on current log level\n */\n private shouldLog(messageLevel: 'error' | 'warn' | 'info' | 'debug'): boolean {\n const currentLevelPriority = LOG_LEVEL_PRIORITY[this.currentLogLevel];\n const messagePriority = MESSAGE_LEVEL_PRIORITY[messageLevel];\n return messagePriority <= currentLevelPriority;\n }\n\n /**\n * Add a log entry with timestamp and immediately send to runtime\n * Only logs that pass the log level filter are captured and sent\n */\n private addLog(\n level: 'info' | 'error' | 'warn' | 'debug',\n message: string,\n type?: 'explanation' | 'query' | 'general',\n data?: Record<string, any>\n ): void {\n // Check if this log level should be captured based on current log level\n if (!this.shouldLog(level)) {\n return;\n }\n\n const log: CapturedLog = {\n timestamp: Date.now(),\n level,\n message,\n ...(type && { type }),\n ...(data && { data }),\n };\n\n this.logs.push(log);\n\n // Send the log immediately to runtime for streaming effect\n this.sendLogImmediately(log);\n\n // Also log to terminal using the main logger based on level\n switch (level) {\n case 'error':\n logger.error('UILogCollector:', log);\n break;\n case 'warn':\n logger.warn('UILogCollector:', log);\n break;\n case 'info':\n logger.info('UILogCollector:', log);\n break;\n case 'debug':\n logger.debug('UILogCollector:', log);\n break;\n }\n }\n\n /**\n * Send a single log to runtime immediately\n */\n private sendLogImmediately(log: CapturedLog): void {\n if (!this.isEnabled()) {\n return;\n }\n\n const response: Message = {\n id: this.uiBlockId!,\n type: 'UI_LOGS',\n from: { type: 'data-agent' },\n to: {\n type: 'runtime',\n id: this.clientId,\n },\n payload: {\n logs: [log], // Send single log in array\n },\n };\n\n this.sendMessage(response);\n }\n\n /**\n * Log info message\n */\n info(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void {\n if (this.isEnabled()) {\n this.addLog('info', message, type, data);\n }\n }\n\n /**\n * Log error message\n */\n error(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void {\n if (this.isEnabled()) {\n this.addLog('error', message, type, data);\n }\n }\n\n /**\n * Log warning message\n */\n warn(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void {\n if (this.isEnabled()) {\n this.addLog('warn', message, type, data);\n }\n }\n\n /**\n * Log debug message\n */\n debug(message: string, type?: 'explanation' | 'query' | 'general', data?: Record<string, any>): void {\n if (this.isEnabled()) {\n this.addLog('debug', message, type, data);\n }\n }\n\n /**\n * Log LLM explanation with typed metadata\n */\n logExplanation(message: string, explanation: string, data?: Record<string, any>): void {\n if (this.isEnabled()) {\n this.addLog('info', message, 'explanation', {\n explanation,\n ...data,\n });\n }\n }\n\n /**\n * Log generated query with typed metadata\n */\n logQuery(message: string, query: string, data?: Record<string, any>): void {\n if (this.isEnabled()) {\n this.addLog('info', message, 'query', {\n query,\n ...data,\n });\n }\n }\n\n /**\n * Send all collected logs at once (optional, for final summary)\n */\n sendAllLogs(): void {\n if (!this.isEnabled() || this.logs.length === 0) {\n return;\n }\n\n const response: Message = {\n id: this.uiBlockId!,\n type: 'UI_LOGS',\n from: { type: 'data-agent' },\n to: {\n type: 'runtime',\n id: this.clientId,\n },\n payload: {\n logs: this.logs,\n },\n };\n\n this.sendMessage(response);\n }\n\n /**\n * Get all collected logs\n */\n getLogs(): CapturedLog[] {\n return [...this.logs];\n }\n\n /**\n * Clear all logs\n */\n clearLogs(): void {\n this.logs = [];\n }\n\n /**\n * Set uiBlockId (in case it's provided later)\n */\n setUIBlockId(uiBlockId: string): void {\n this.uiBlockId = uiBlockId;\n }\n}\n","import { ThreadManager } from '../threads';\nimport { logger } from '../utils/logger';\nimport { STORAGE_CONFIG } from '../config/storage';\n\n/**\n * CleanupService handles cleanup of old threads and UIBlocks\n * to prevent memory bloat and maintain optimal performance\n */\nexport class CleanupService {\n private static instance: CleanupService;\n private cleanupInterval: NodeJS.Timeout | null = null;\n\n private constructor() {}\n\n /**\n * Get singleton instance of CleanupService\n */\n static getInstance(): CleanupService {\n if (!CleanupService.instance) {\n CleanupService.instance = new CleanupService();\n }\n return CleanupService.instance;\n }\n\n /**\n * Clean up old threads based on retention period\n * @param retentionDays - Number of days to keep threads (defaults to config)\n * @returns Number of threads deleted\n */\n cleanupOldThreads(retentionDays: number = STORAGE_CONFIG.THREAD_RETENTION_DAYS): number {\n const threadManager = ThreadManager.getInstance();\n const cutoffDate = new Date();\n cutoffDate.setDate(cutoffDate.getDate() - retentionDays);\n\n const threads = threadManager.getAllThreads();\n let deletedCount = 0;\n\n for (const thread of threads) {\n if (thread.getCreatedAt() < cutoffDate) {\n const threadId = thread.getId();\n if (threadManager.deleteThread(threadId)) {\n deletedCount++;\n logger.info(`Deleted old thread: ${threadId} (created: ${thread.getCreatedAt().toISOString()})`);\n }\n }\n }\n\n if (deletedCount > 0) {\n logger.info(`Cleanup: Deleted ${deletedCount} old threads (older than ${retentionDays} days)`);\n }\n\n return deletedCount;\n }\n\n /**\n * Clean up old UIBlocks within threads based on retention period\n * @param retentionDays - Number of days to keep UIBlocks (defaults to config)\n * @returns Object with number of UIBlocks deleted per thread\n */\n cleanupOldUIBlocks(retentionDays: number = STORAGE_CONFIG.UIBLOCK_RETENTION_DAYS): { [threadId: string]: number } {\n const threadManager = ThreadManager.getInstance();\n const cutoffDate = new Date();\n cutoffDate.setDate(cutoffDate.getDate() - retentionDays);\n\n const threads = threadManager.getAllThreads();\n const deletionStats: { [threadId: string]: number } = {};\n\n for (const thread of threads) {\n const uiblocks = thread.getUIBlocks();\n let deletedInThread = 0;\n\n for (const uiblock of uiblocks) {\n if (uiblock.getCreatedAt() < cutoffDate) {\n if (thread.removeUIBlock(uiblock.getId())) {\n deletedInThread++;\n }\n }\n }\n\n if (deletedInThread > 0) {\n deletionStats[thread.getId()] = deletedInThread;\n logger.info(\n `Deleted ${deletedInThread} old UIBlocks from thread ${thread.getId()} (older than ${retentionDays} days)`\n );\n }\n }\n\n const totalDeleted = Object.values(deletionStats).reduce((sum, count) => sum + count, 0);\n if (totalDeleted > 0) {\n logger.info(`Cleanup: Deleted ${totalDeleted} old UIBlocks across ${Object.keys(deletionStats).length} threads`);\n }\n\n return deletionStats;\n }\n\n /**\n * Clear all component data from UIBlocks to free memory\n * Keeps metadata but removes the actual data\n * @param retentionDays - Number of days to keep full data (defaults to config)\n * @returns Number of UIBlocks whose data was cleared\n */\n clearOldUIBlockData(retentionDays: number = STORAGE_CONFIG.UIBLOCK_RETENTION_DAYS): number {\n const threadManager = ThreadManager.getInstance();\n const cutoffDate = new Date();\n cutoffDate.setDate(cutoffDate.getDate() - retentionDays);\n\n const threads = threadManager.getAllThreads();\n let clearedCount = 0;\n\n for (const thread of threads) {\n const uiblocks = thread.getUIBlocks();\n\n for (const uiblock of uiblocks) {\n if (uiblock.getCreatedAt() < cutoffDate) {\n const componentData = uiblock.getComponentData();\n\n // Only clear if data exists\n if (componentData && Object.keys(componentData).length > 0) {\n // Keep metadata but clear actual data\n const metadata = {\n dataCleared: true,\n clearedAt: new Date().toISOString(),\n originalDataInfo: {\n totalRows: componentData.totalRows,\n storedRows: componentData.storedRows,\n isTruncated: componentData.isTruncated,\n },\n };\n\n uiblock.setComponentData({ ...metadata, data: null });\n clearedCount++;\n }\n }\n }\n }\n\n if (clearedCount > 0) {\n logger.info(`Cleanup: Cleared data from ${clearedCount} old UIBlocks (older than ${retentionDays} days)`);\n }\n\n return clearedCount;\n }\n\n /**\n * Run full cleanup (threads, UIBlocks, and data)\n * @returns Cleanup statistics\n */\n runFullCleanup(): {\n threadsDeleted: number;\n uiblocksDeleted: { [threadId: string]: number };\n dataCleared: number;\n } {\n logger.info('Starting full cleanup...');\n\n const stats = {\n threadsDeleted: this.cleanupOldThreads(),\n uiblocksDeleted: this.cleanupOldUIBlocks(),\n dataCleared: this.clearOldUIBlockData(),\n };\n\n const totalUIBlocksDeleted = Object.values(stats.uiblocksDeleted).reduce((sum, count) => sum + count, 0);\n\n logger.info(\n `Full cleanup completed: ${stats.threadsDeleted} threads, ${totalUIBlocksDeleted} UIBlocks deleted, ${stats.dataCleared} UIBlock data cleared`\n );\n\n return stats;\n }\n\n /**\n * Start automatic cleanup at regular intervals\n * @param intervalHours - Hours between cleanup runs (default: 24)\n */\n startAutoCleanup(intervalHours: number = 24): void {\n if (this.cleanupInterval) {\n logger.warn('Auto cleanup is already running');\n\n //stop this and run with new interval\n\n \n return;\n }\n\n const intervalMs = intervalHours * 60 * 60 * 1000;\n\n // Run initial cleanup\n this.runFullCleanup();\n\n // Schedule recurring cleanup\n this.cleanupInterval = setInterval(() => {\n this.runFullCleanup();\n }, intervalMs);\n\n logger.info(`Auto cleanup started: running every ${intervalHours} hours`);\n }\n\n /**\n * Stop automatic cleanup\n */\n stopAutoCleanup(): void {\n if (this.cleanupInterval) {\n clearInterval(this.cleanupInterval);\n this.cleanupInterval = null;\n logger.info('Auto cleanup stopped');\n }\n }\n\n /**\n * Check if auto cleanup is running\n */\n isAutoCleanupRunning(): boolean {\n return this.cleanupInterval !== null;\n }\n\n /**\n * Get current memory usage statistics\n */\n getMemoryStats(): {\n threadCount: number;\n totalUIBlocks: number;\n avgUIBlocksPerThread: number;\n } {\n const threadManager = ThreadManager.getInstance();\n const threads = threadManager.getAllThreads();\n const threadCount = threads.length;\n\n let totalUIBlocks = 0;\n for (const thread of threads) {\n totalUIBlocks += thread.getUIBlockCount();\n }\n\n return {\n threadCount,\n totalUIBlocks,\n avgUIBlocksPerThread: threadCount > 0 ? totalUIBlocks / threadCount : 0,\n };\n }\n}\n","import { createWebSocket } from './websocket';\nimport {\n IncomingMessageSchema,\n type Message,\n type IncomingMessage,\n type SuperatomSDKConfig,\n type CollectionRegistry,\n type CollectionHandler,\n type CollectionOperation,\n type DatabaseType,\n type ModelStrategy,\n type DashCompModelConfig,\n Component,\n LLMProvider,\n Tool,\n} from './types';\nimport { logger } from './utils/logger';\nimport { handleDataRequest } from './handlers/data-request';\nimport { handleBundleRequest } from './handlers/bundle-request';\nimport { handleAuthLoginRequest } from './handlers/auth-login-requests';\nimport { handleAuthVerifyRequest } from './handlers/auth-verify-request';\nimport { handleUserPromptRequest } from './handlers/user-prompt-request';\nimport { handleUserPromptSuggestions } from './handlers/user-prompt-suggestions';\nimport { handleActionsRequest } from './handlers/actions-request';\nimport { handleComponentListResponse } from './handlers/components-list-response';\nimport { handleWorkflowListResponse } from './handlers/workflow-list-response';\nimport { handleUsersRequest } from './handlers/users';\nimport { handleDashboardsRequest } from './handlers/dashboards';\nimport { handleReportsRequest } from './handlers/reports';\nimport { handleUIsRequest } from './handlers/uis';\nimport { handleBookmarksRequest } from './handlers/bookmarks';\nimport { handleArtifactsRequest } from './handlers/artifacts';\nimport { handleKbNodesRequest } from './handlers/kb-nodes';\nimport { handleMenusRequest } from './handlers/menus';\nimport { handleDashCompRequest, dashboardConversationHistory } from './dashComp';\nimport { handleReportCompRequest } from './reportComp';\nimport { handleSchemaRequest } from './handlers/schema-request';\nimport { getLLMProviders, anthropicLLM, groqLLM, geminiLLM, openaiLLM } from './userResponse';\nimport { setUserManager, cleanupUserStorage } from './auth/user-storage';\nimport { UserManager } from './auth/user-manager';\nimport { setDashboardManager } from './dashboards/dashboard-storage';\nimport { DashboardManager } from './dashboards/dashboard-manager';\nimport { setReportManager } from './reports/report-storage';\nimport { ReportManager } from './reports/report-manager';\nimport { promptLoader } from './userResponse/prompt-loader';\nimport { queryCache } from './utils/query-cache';\nimport type { WorkflowDescriptor } from './userResponse/agents';\n\n\nconst DEFAULT_WS_URL = 'wss://ws.superatom.ai/websocket';\n\ntype MessageTypeHandler = (message: IncomingMessage) => void | Promise<void>;\n\nexport class SuperatomSDK {\n private ws: ReturnType<typeof createWebSocket> | null = null;\n private url: string;\n private apiKey?: string;\n private projectId: string;\n private type: string;\n private bundleDir: string | undefined;\n private messageHandlers: Map<string, (message: IncomingMessage) => void> = new Map();\n private messageTypeHandlers: Map<string, MessageTypeHandler> = new Map();\n private connected: boolean = false;\n private reconnectAttempts: number = 0;\n // Retry forever — the backend must self-heal across NAT timeouts, DO\n // redeploys, and long network outages. The previous cap (5) gave up after\n // ~30s and left the singleton SDK dead until process restart.\n private maxReconnectAttempts: number = Infinity;\n private collections: CollectionRegistry = {};\n\tprivate components: Component[] = [];\n private tools: Tool[] = [];\n private workflows: WorkflowDescriptor[] = [];\n private anthropicApiKey: string;\n private groqApiKey: string;\n private geminiApiKey: string;\n private openaiApiKey: string;\n private llmProviders: LLMProvider[];\n private databaseType: DatabaseType;\n private modelStrategy: ModelStrategy;\n private mainAgentModel: string;\n private sourceAgentModel: string;\n private dashCompModels?: DashCompModelConfig;\n private conversationSimilarityThreshold: number;\n private userManager: UserManager;\n private dashboardManager: DashboardManager;\n private reportManager: ReportManager;\n\n // Heartbeat properties for keeping WebSocket connection alive.\n // 25s ping + 10s grace stays under common NAT/LB idle thresholds (~60-100s)\n // so we detect dead sockets within seconds instead of minutes.\n private pingInterval: NodeJS.Timeout | null = null;\n private lastPong: number = Date.now();\n private readonly PING_INTERVAL_MS = 25000;\n private readonly PONG_TIMEOUT_MS = 35000;\n\n constructor(config: SuperatomSDKConfig) {\n // Set log level if provided in config (overrides environment variable)\n if (config.logLevel) {\n logger.setLogLevel(config.logLevel);\n }\n\n this.apiKey = config.apiKey;\n this.projectId = config.projectId;\n this.type = config.type || 'data-agent';\n this.bundleDir = config.bundleDir;\n this.url = config.url || process.env.SA_WEBSOCKET_URL || DEFAULT_WS_URL;\n this.anthropicApiKey = config.ANTHROPIC_API_KEY || process.env.ANTHROPIC_API_KEY || '';\n this.groqApiKey = config.GROQ_API_KEY || process.env.GROQ_API_KEY || '';\n this.geminiApiKey = config.GEMINI_API_KEY || process.env.GEMINI_API_KEY || '';\n this.openaiApiKey = config.OPENAI_API_KEY || process.env.OPENAI_API_KEY || '';\n this.llmProviders = config.LLM_PROVIDERS || getLLMProviders();\n this.databaseType = config.databaseType || 'postgresql';\n this.modelStrategy = config.modelStrategy || 'fast';\n this.mainAgentModel = config.mainAgentModel || 'anthropic/claude-sonnet-4-5-20250929';\n this.sourceAgentModel = config.sourceAgentModel || 'anthropic/claude-sonnet-4-5-20250929';\n this.dashCompModels = config.dashCompModels;\n this.conversationSimilarityThreshold = config.conversationSimilarityThreshold ?? 0.8;\n\n // Apply model strategy to all LLM providers\n this.applyModelStrategy(this.modelStrategy);\n\n // Apply conversation similarity threshold to all LLM providers\n this.applyConversationSimilarityThreshold(this.conversationSimilarityThreshold);\n\n // Configure query cache TTL (default: 5 minutes)\n if (config.queryCacheTTL !== undefined) {\n queryCache.setTTL(config.queryCacheTTL);\n }\n\n // Configure dashboard conversation history TTL (default: 30 minutes)\n if (config.dashboardHistoryTTL !== undefined) {\n dashboardConversationHistory.setTTL(config.dashboardHistoryTTL);\n }\n\n logger.info(`Initializing Superatom SDK for project ${this.projectId}, llm providers: ${this.llmProviders.join(', ')}, database type: ${this.databaseType}, model strategy: ${this.modelStrategy}, query cache TTL: ${queryCache.getTTL()} minutes`);\n\n // Initialize UserManager for this SDK instance\n this.userManager = new UserManager(this.projectId, 5000);\n\n // Initialize DashboardManager for this SDK instance\n this.dashboardManager = new DashboardManager(this.projectId);\n\n // Initialize ReportManager for this SDK instance\n this.reportManager = new ReportManager(this.projectId);\n\n // Initialize PromptLoader (load prompts into memory)\n this.initializePromptLoader(config.promptsDir).catch((error) => {\n logger.error('Failed to initialize PromptLoader:', error);\n });\n\n // Initialize UserManager with projectId (startup)\n this.initializeUserManager().catch((error) => {\n logger.error('Failed to initialize UserManager:', error);\n });\n\n // Initialize DashboardManager\n this.initializeDashboardManager();\n\n // Initialize ReportManager\n this.initializeReportManager();\n\n // Note: Connection is not automatic - call connect() explicitly when ready\n }\n\n /**\n * Initialize PromptLoader and load prompts into memory\n * Tries to load from file system first, then falls back to hardcoded prompts\n */\n private async initializePromptLoader(promptsDir?: string): Promise<void> {\n try {\n // Set custom prompts directory if provided\n if (promptsDir) {\n promptLoader.setPromptsDir(promptsDir);\n }\n\n // Set database type for SQL rules loading\n promptLoader.setDatabaseType(this.databaseType);\n\n await promptLoader.initialize();\n logger.info(`PromptLoader initialized with ${promptLoader.getCacheSize()} prompts from ${promptLoader.getPromptsDir()}, database type: ${this.databaseType}`);\n } catch (error) {\n logger.error('Failed to initialize PromptLoader:', error);\n throw error;\n }\n }\n\n /**\n * Initialize UserManager for the project\n */\n private async initializeUserManager(): Promise<void> {\n try {\n await this.userManager.init();\n // Set the global reference for backward compatibility with existing auth code\n setUserManager(this.userManager);\n logger.info(`UserManager initialized for project: ${this.projectId}`);\n } catch (error) {\n logger.error('Failed to initialize UserManager:', error);\n throw error;\n }\n }\n\n /**\n * Get the UserManager instance for this SDK\n */\n public getUserManager(): UserManager {\n return this.userManager;\n }\n\n /**\n * Initialize DashboardManager for the project\n */\n private initializeDashboardManager(): void {\n // Set the global reference for dashboard operations\n setDashboardManager(this.dashboardManager);\n }\n\n /**\n * Get the DashboardManager instance for this SDK\n */\n public getDashboardManager(): DashboardManager {\n return this.dashboardManager;\n }\n\n /**\n * Initialize ReportManager for the project\n */\n private initializeReportManager(): void {\n // Set the global reference for report operations\n setReportManager(this.reportManager);\n }\n\n /**\n * Get the ReportManager instance for this SDK\n */\n public getReportManager(): ReportManager {\n return this.reportManager;\n }\n\n /**\n * Connect to the Superatom WebSocket service\n */\n async connect(): Promise<void> {\n // If already connected, return immediately\n if (this.connected && this.ws && this.ws.readyState === this.ws.OPEN) {\n logger.info('Already connected to WebSocket');\n return Promise.resolve();\n }\n\n return new Promise((resolve, reject) => {\n try {\n // Add all required query parameters\n const url = new URL(this.url);\n if (this.apiKey) {\n url.searchParams.set('apiKey', this.apiKey);\n }\n url.searchParams.set('projectId', this.projectId);\n url.searchParams.set('type', this.type);\n\n logger.info(`Connecting to WebSocket: ${url.toString()}`);\n\n this.ws = createWebSocket(url.toString());\n\n this.ws.addEventListener('open', () => {\n this.connected = true;\n this.reconnectAttempts = 0;\n logger.info('WebSocket connected successfully');\n this.startHeartbeat(); // Start heartbeat to keep connection alive\n resolve();\n });\n\n this.ws.addEventListener('message', (event: any) => {\n this.handleMessage(event.data);\n });\n\n this.ws.addEventListener('error', (error: any) => {\n logger.error('WebSocket error:', error);\n reject(error);\n });\n\n this.ws.addEventListener('close', () => {\n this.connected = false;\n logger.warn('WebSocket closed');\n this.handleReconnect();\n });\n } catch (error) {\n reject(error);\n }\n });\n }\n\n /**\n * Handle incoming WebSocket messages\n */\n private handleMessage(data: string): void {\n try {\n const parsed = JSON.parse(data);\n const message = IncomingMessageSchema.parse(parsed);\n\n logger.debug('Received message:', message.type);\n\n // Route message by type\n switch (message.type) {\n case 'PONG':\n // Handle heartbeat PONG response\n this.handlePong();\n break;\n\n case 'DATA_REQ':\n handleDataRequest(parsed, this.collections, (msg) => this.send(msg), this.tools).catch((error) => {\n logger.error('Failed to handle data request:', error);\n });\n break;\n\n case 'BUNDLE_REQ':\n handleBundleRequest(parsed, this.bundleDir, (msg) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle bundle request:', error);\n });\n break;\n\n case 'AUTH_LOGIN_REQ':\n handleAuthLoginRequest(parsed, this.collections, (msg) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle auth login request:', error);\n });\n break;\n\n case 'AUTH_VERIFY_REQ':\n handleAuthVerifyRequest(parsed, this.collections, (msg) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle auth verify request:', error);\n });\n break;\n\n case 'USER_PROMPT_REQ':\n handleUserPromptRequest(parsed, this.components, (msg) => this.send(msg), this.anthropicApiKey, this.groqApiKey, this.geminiApiKey, this.openaiApiKey, this.llmProviders, this.collections, this.tools, this.mainAgentModel, this.sourceAgentModel, this.workflows).catch((error) => {\n logger.error('Failed to handle user prompt request:', error);\n });\n break;\n\n case 'ACTIONS':\n handleActionsRequest(parsed, (msg) => this.send(msg), this.anthropicApiKey, this.groqApiKey, this.geminiApiKey, this.openaiApiKey, this.llmProviders).catch((error) => {\n logger.error('Failed to handle actions request:', error);\n });\n break;\n\n case 'USER_PROMPT_SUGGESTIONS_REQ':\n handleUserPromptSuggestions(parsed, this.components, (msg) => this.send(msg), this.collections).catch((error) => {\n logger.error('Failed to handle user prompt suggestions request:', error);\n });\n break;\n\n case 'COMPONENT_LIST_RES':\n handleComponentListResponse(parsed, (com) => this.storeComponents(com), this.collections).catch((error) => {\n logger.error('Failed to handle component list request:', error);\n });\n break;\n\n case 'WORKFLOW_LIST_RES':\n handleWorkflowListResponse(parsed, (wf) => this.setWorkflows(wf)).catch((error) => {\n logger.error('Failed to handle workflow list request:', error);\n });\n break;\n\n case 'USERS':\n handleUsersRequest(parsed, this.collections, (msg) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle users request:', error);\n });\n break;\n\n case 'DASHBOARDS':\n handleDashboardsRequest(parsed, this.collections, (msg: Message) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle dashboards request:', error);\n });\n break;\n\n case 'REPORTS':\n handleReportsRequest(parsed, this.collections, (msg: Message) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle reports request:', error);\n });\n break;\n\n case 'UIS':\n handleUIsRequest(parsed, this.collections, (msg: Message) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle UIs request:', error);\n });\n break;\n\n case 'BOOKMARKS':\n handleBookmarksRequest(parsed, this.collections, (msg) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle bookmarks request:', error);\n });\n break;\n\n case 'ARTIFACTS':\n handleArtifactsRequest(parsed, this.collections, (msg) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle artifacts request:', error);\n });\n break;\n\n case 'KB_NODES':\n handleKbNodesRequest(parsed, this.collections, (msg) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle KB nodes request:', error);\n });\n break;\n\n case 'MENUS':\n handleMenusRequest(parsed, this.collections, (msg) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle menus request:', error);\n });\n break;\n\n case 'DASH_COMP_REQ':\n handleDashCompRequest(parsed, this.components, (msg) => this.send(msg), this.anthropicApiKey, this.groqApiKey, this.geminiApiKey, this.openaiApiKey, this.llmProviders, this.collections, this.tools, this.dashCompModels).catch((error) => {\n logger.error('Failed to handle dash comp request:', error);\n });\n break;\n\n case 'REPORT_COMP_REQ':\n handleReportCompRequest(parsed, this.components, (msg) => this.send(msg), this.anthropicApiKey, this.groqApiKey, this.geminiApiKey, this.openaiApiKey, this.llmProviders, this.collections, this.tools, this.dashCompModels).catch((error) => {\n logger.error('Failed to handle report comp request:', error);\n });\n break;\n\n case 'SCHEMA_REQ':\n handleSchemaRequest(parsed, (msg) => this.send(msg)).catch((error) => {\n logger.error('Failed to handle schema request:', error);\n });\n break;\n\n default:\n // Check for custom message type handlers\n const handler = this.messageTypeHandlers.get(message.type);\n if (handler) {\n Promise.resolve(handler(message)).catch((error) => {\n logger.error(`Failed to handle ${message.type}:`, error);\n });\n }\n break;\n }\n\n // Call registered message handlers\n this.messageHandlers.forEach((handler) => {\n handler(message);\n });\n } catch (error) {\n logger.error('Failed to parse incoming message:', error);\n }\n }\n\n /**\n * Send a message to the Superatom service.\n * Returns true if the message was sent, false if the WebSocket is not connected.\n * Does NOT throw on closed connections — callers can check the return value if needed.\n */\n send(message: Message): boolean {\n if (!this.ws || !this.connected) {\n logger.warn('WebSocket is not connected, message not sent:', message.type);\n return false;\n }\n\n if (this.ws.readyState !== this.ws.OPEN) {\n logger.warn('WebSocket is not in OPEN state, message not sent:', message.type);\n return false;\n }\n\n try {\n const payload = JSON.stringify(message);\n this.ws.send(payload);\n return true;\n } catch (error) {\n logger.warn(`Failed to send WebSocket message (${message.type}):`, error instanceof Error ? error.message : String(error));\n return false;\n }\n }\n\n /**\n * Register a message handler to receive all messages\n */\n onMessage(handler: (message: IncomingMessage) => void): () => void {\n const id = Math.random().toString(36).substring(7);\n this.messageHandlers.set(id, handler);\n\n // Return unsubscribe function\n return () => {\n this.messageHandlers.delete(id);\n };\n }\n\n /**\n * Register a handler for a specific message type\n */\n onMessageType(type: string, handler: MessageTypeHandler): () => void {\n this.messageTypeHandlers.set(type, handler);\n\n // Return unsubscribe function\n return () => {\n this.messageTypeHandlers.delete(type);\n };\n }\n\n /**\n * Disconnect from the WebSocket service\n */\n disconnect(): void {\n this.stopHeartbeat(); // Stop heartbeat before disconnecting\n if (this.ws) {\n this.ws.close();\n this.ws = null;\n this.connected = false;\n }\n }\n\n /**\n * Cleanup and disconnect - stops reconnection attempts and closes the connection\n */\n async destroy(): Promise<void> {\n // Stop heartbeat first\n this.stopHeartbeat();\n\n // Prevent further reconnection attempts\n this.maxReconnectAttempts = 0;\n this.reconnectAttempts = 0;\n\n // Clear all message handlers\n this.messageHandlers.clear();\n\n // Clear all collections\n this.collections = {};\n\n // Cleanup UserManager\n try {\n await cleanupUserStorage();\n logger.info('UserManager cleanup completed');\n } catch (error) {\n logger.error('Error during UserManager cleanup:', error);\n }\n\n // Disconnect\n this.disconnect();\n }\n\n /**\n * Check if the SDK is currently connected\n */\n isConnected(): boolean {\n return this.connected && this.ws !== null && this.ws.readyState === this.ws.OPEN;\n }\n\n /**\n * Register a collection handler for data operations\n */\n addCollection<TParams = any, TResult = any>(\n collectionName: string,\n operation: CollectionOperation | string,\n handler: CollectionHandler<TParams, TResult>\n ): void {\n if (!this.collections[collectionName]) {\n this.collections[collectionName] = {};\n }\n this.collections[collectionName][operation] = handler;\n }\n\n private handleReconnect(): void {\n if (this.reconnectAttempts < this.maxReconnectAttempts) {\n this.reconnectAttempts++;\n // Cap backoff at 30s so we don't sit idle for minutes after a long outage.\n const delay = Math.min(1000 * Math.pow(2, this.reconnectAttempts), 30000);\n\n setTimeout(() => {\n logger.info(`Attempting to reconnect (attempt ${this.reconnectAttempts})...`);\n this.connect().catch((error) => {\n logger.error('Reconnection failed:', error);\n });\n }, delay);\n } else {\n logger.error('Max reconnection attempts reached');\n }\n }\n\n /**\n * Start heartbeat to keep WebSocket connection alive\n * Sends PING every 3 minutes to prevent idle timeout from cloud infrastructure\n */\n private startHeartbeat(): void {\n this.stopHeartbeat(); // Clear any existing interval\n this.lastPong = Date.now();\n\n this.pingInterval = setInterval(() => {\n if (!this.ws || this.ws.readyState !== this.ws.OPEN) {\n logger.warn('WebSocket not open during heartbeat check, stopping heartbeat');\n this.stopHeartbeat();\n return;\n }\n\n // Check if we received a PONG within the timeout period\n const timeSinceLastPong = Date.now() - this.lastPong;\n if (timeSinceLastPong > this.PONG_TIMEOUT_MS) {\n logger.warn(`No PONG received for ${timeSinceLastPong}ms, connection may be dead. Reconnecting...`);\n this.stopHeartbeat();\n this.ws.close(); // This will trigger handleReconnect via the close event\n return;\n }\n\n // Send PING message\n try {\n const pingMessage: Message = {\n type: 'PING',\n from: { type: this.type },\n payload: { timestamp: Date.now() }\n };\n this.ws.send(JSON.stringify(pingMessage));\n logger.debug('Heartbeat PING sent');\n } catch (error) {\n logger.error('Failed to send PING:', error);\n this.stopHeartbeat();\n this.ws.close();\n }\n }, this.PING_INTERVAL_MS);\n\n logger.info(`Heartbeat started (PING every ${this.PING_INTERVAL_MS / 1000}s)`);\n }\n\n /**\n * Stop the heartbeat interval\n */\n private stopHeartbeat(): void {\n if (this.pingInterval) {\n clearInterval(this.pingInterval);\n this.pingInterval = null;\n logger.debug('Heartbeat stopped');\n }\n }\n\n /**\n * Handle PONG response from server\n */\n private handlePong(): void {\n this.lastPong = Date.now();\n logger.debug('Heartbeat PONG received');\n }\n\n private storeComponents(components: Component[]){\n this.components = components;\n }\n\n /**\n * Set tools for the SDK instance\n */\n public setTools(tools: Tool[]): void {\n this.tools = tools;\n logger.info(`Tools stored in SDK: ${tools.length} tools`);\n }\n\n /**\n * Get the stored tools\n */\n public getTools(): Tool[] {\n return this.tools;\n }\n\n /**\n * Register workflow components for the SDK instance.\n *\n * Workflows are pre-built multi-step UI flows the main agent can pick when\n * the user's prompt matches a workflow's `whenToUse` trigger. Picking a\n * workflow short-circuits analysis text + dashboard component generation —\n * the workflow component is returned directly, with the LLM-extracted props.\n */\n public setWorkflows(workflows: WorkflowDescriptor[]): void {\n this.workflows = workflows;\n logger.info(`Workflows stored in SDK: ${workflows.length} workflow(s)`);\n }\n\n /**\n * Get the registered workflow components.\n */\n public getWorkflows(): WorkflowDescriptor[] {\n return this.workflows;\n }\n\n /**\n * Apply model strategy to all LLM provider singletons\n * @param strategy - 'best', 'fast', or 'balanced'\n */\n private applyModelStrategy(strategy: ModelStrategy): void {\n anthropicLLM.setModelStrategy(strategy);\n groqLLM.setModelStrategy(strategy);\n geminiLLM.setModelStrategy(strategy);\n openaiLLM.setModelStrategy(strategy);\n logger.info(`Model strategy '${strategy}' applied to all LLM providers`);\n }\n\n /**\n * Set model strategy at runtime\n * @param strategy - 'best', 'fast', or 'balanced'\n */\n public setModelStrategy(strategy: ModelStrategy): void {\n this.modelStrategy = strategy;\n this.applyModelStrategy(strategy);\n }\n\n /**\n * Get current model strategy\n */\n public getModelStrategy(): ModelStrategy {\n return this.modelStrategy;\n }\n\n /**\n * Apply conversation similarity threshold to all LLM provider singletons\n * @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)\n */\n private applyConversationSimilarityThreshold(threshold: number): void {\n anthropicLLM.setConversationSimilarityThreshold(threshold);\n groqLLM.setConversationSimilarityThreshold(threshold);\n geminiLLM.setConversationSimilarityThreshold(threshold);\n openaiLLM.setConversationSimilarityThreshold(threshold);\n logger.info(`Conversation similarity threshold '${threshold}' applied to all LLM providers`);\n }\n\n /**\n * Set conversation similarity threshold at runtime\n * @param threshold - Value between 0 and 1 (e.g., 0.8 = 80% similarity required)\n */\n public setConversationSimilarityThreshold(threshold: number): void {\n this.conversationSimilarityThreshold = threshold;\n this.applyConversationSimilarityThreshold(threshold);\n }\n\n /**\n * Get current conversation similarity threshold\n */\n public getConversationSimilarityThreshold(): number {\n return this.conversationSimilarityThreshold;\n }\n\n}\n\n// Export types\nexport type { Message, IncomingMessage, SuperatomSDKConfig, CollectionHandler, CollectionOperation, User, UsersData, Tool, ToolOutputSchema, OutputField, DBUIBlock, KbNodesRequestPayload, KbNodesQueryFilters, DatabaseType, ModelStrategy } from './types';\nexport {LLM} from './llm';\nexport { UserManager } from './auth/user-manager';\nexport { UILogCollector, type CapturedLog } from './utils/log-collector';\nexport { Thread, UIBlock, ThreadManager, type Action } from './threads';\nexport { CleanupService } from './services/cleanup-service';\nexport { STORAGE_CONFIG } from './config/storage';\nexport { CONTEXT_CONFIG } from './config/context';\nexport { logger, type LogLevel } from './utils/logger';\n// LLM Usage Logger for tracking API costs and performance\nexport { llmUsageLogger, type LLMUsageEntry } from './utils/llm-usage-logger';\n// User Prompt Error Logger for capturing detailed error information\nexport { userPromptErrorLogger } from './utils/user-prompt-error-logger';\n// BM25L Reranker for hybrid semantic search\nexport {\n BM25L,\n hybridRerank,\n rerankChromaResults,\n rerankConversationResults,\n type BM25LOptions,\n type HybridSearchOptions,\n type RerankedResult\n} from './utils/bm25l-reranker';\n// LLM Model Strategy for configuring model usage (ModelStrategy exported from types above)\nexport {\n type TaskType,\n type BaseLLMConfig,\n anthropicLLM,\n groqLLM,\n geminiLLM,\n openaiLLM\n} from './userResponse';\n// Query Cache for database query result caching\nexport { queryCache } from './utils/query-cache';\n// Dashboard conversation history for per-dashboard scoped history\nexport { dashboardConversationHistory } from './dashComp';\n// Multi-Agent system for alert analysis and programmatic use\nexport { MainAgent } from './userResponse/agents';\nexport {\n DEFAULT_AGENT_CONFIG,\n type AgentConfig,\n type AgentResponse,\n type WorkflowDescriptor,\n type SelectedWorkflow,\n} from './userResponse/agents';\n// Script flow — store, runner, and types. Exported so the backend can\n// regenerate expired script datasets by re-running the saved recipe (#6).\nexport {\n ScriptStore,\n ScriptMatcher,\n runScript,\n normalizeScriptBody,\n resolveScriptRecipeStore,\n} from './userResponse/scripts';\nexport type {\n ScriptRecipe,\n ScriptParameter,\n ScriptResult,\n ScriptComponentSpec,\n ScriptStoreOptions,\n ScriptRecipeStore,\n ScriptRecipeMetaRow,\n} from './userResponse/scripts';"],"mappings":";;;;;;;;AAAA,OAAO,eAAe;AAMf,SAAS,gBAAgB,KAA4B;A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WAAO,KAAK,sCAAsC,SAAS,gCAAgC;AAAA,EAC7F;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,mCAAmC,WAAyB;AACjE,SAAK,kCAAkC;AACvC,SAAK,qCAAqC,SAAS;AAAA,EACrD;AAAA;AAAA;AAAA;AAAA,EAKO,qCAA6C;AAClD,WAAO,KAAK;AAAA,EACd;AAEF;","names":["z","z","ExpressionSchema","BindingSchema","ForDirectiveSchema","QuerySpecSchema","UIElementSchema","UIComponentSchema","DSLRendererPropsSchema","z","DSLRendererPropsSchema","size","randomUUID","result","schema","truncationNote","result","fs","crypto","sendDataResponse","sendDataResponse","fs","path","path","fs","fs","path","b","DEFAULT_MAX_CHARS_PER_FIELD","resolve","fs","path","model","result","fullText","inputTokens","outputTokens","durationMs","path","resolve","executedQueries","schema","summary","fs","path","path","fs","path","fs","schema","DEFAULT_MAX_CHARS_PER_FIELD","result","methodDuration","elapsedTime","dotenv","dotenv","dotenv","dotenv","dotenv","dotenv","elapsedTime","matchedComponents","layoutTitle","layoutDescription","actions","queryMap","securedComponents","containerComponent","sendDataResponse","sendResponse","sendResponse","sanitizedUsers","sanitizedUser","sendResponse","dashboardManager","handleCreate","handleUpdate","handleDelete","handleGetAll","handleGetOne","handleQuery","sendResponse","reportManager","handleCreate","handleUpdate","handleDelete","handleGetAll","handleGetOne","handleQuery","sendResponse","handleCreate","handleUpdate","handleDelete","handleGetAll","handleGetOne","handleQuery","handleCreate","handleUpdate","handleDelete","handleGetAll","handleGetOne","handleQuery","sendResponse","handleCreate","handleUpdate","handleDelete","handleGetAll","handleGetOne","handleQuery","sendResponse","handleCreate","handleUpdate","handleDelete","handleGetAll","handleGetOne","sendResponse","handleCreate","handleUpdate","handleDelete","handleGetAll","handleGetOne","handleQuery","sendResponse","result","databaseRules","result","cacheKey","getApiKeyAndModel","formatComponentsForPrompt","formatToolsForPrompt","formatComponentsForPrompt","formatToolsForPrompt","getApiKeyAndModel","result","response","dotenv","dotenv","fs","path","os","path","os","fs","fs","path","os","path","os","fs","fs","path","os","path","os","fs","DSLRendererPropsSchema","LOG_LEVEL_PRIORITY","MESSAGE_LEVEL_PRIORITY","resolve"]}