import type { ElasticsearchDocumentResponse, ElasticsearchUpdateDocumentParams, } from '@/tools/elasticsearch/types' import type { ToolConfig } from '@/tools/types' function buildBaseUrl(params: ElasticsearchUpdateDocumentParams): string { if (params.deploymentType === 'cloud' && params.cloudId) { const parts = params.cloudId.split(':') if (parts.length >= 2) { try { const decoded = Buffer.from(parts[1], 'base64').toString('utf-8') const [esHost] = decoded.split('$') if (esHost) { return `https://${parts[0]}.${esHost}` } } catch { // Fallback } } throw new Error('Invalid Cloud ID format') } if (!params.host) { throw new Error('Host is required for self-hosted deployments') } return params.host.replace(/\/$/, '') } function buildAuthHeaders(params: ElasticsearchUpdateDocumentParams): Record { const headers: Record = { 'Content-Type': 'application/json', } if (params.authMethod === 'api_key' && params.apiKey) { headers.Authorization = `ApiKey ${params.apiKey}` } else if (params.authMethod === 'basic_auth' && params.username && params.password) { const credentials = Buffer.from(`${params.username}:${params.password}`).toString('base64') headers.Authorization = `Basic ${credentials}` } else { throw new Error('Invalid authentication configuration') } return headers } export const updateDocumentTool: ToolConfig< ElasticsearchUpdateDocumentParams, ElasticsearchDocumentResponse > = { id: 'elasticsearch_update_document', name: 'Elasticsearch Update Document', description: 'Partially update a document in Elasticsearch using doc merge.', version: '1.0.0', params: { deploymentType: { type: 'string', required: true, description: 'Deployment type: self_hosted or cloud', }, host: { type: 'string', required: false, visibility: 'user-only', description: 'Elasticsearch host URL (for self-hosted)', }, cloudId: { type: 'string', required: false, visibility: 'user-only', description: 'Elastic Cloud ID (for cloud deployments)', }, authMethod: { type: 'string', required: true, description: 'Authentication method: api_key or basic_auth', }, apiKey: { type: 'string', required: false, visibility: 'user-only', description: 'Elasticsearch API key', }, username: { type: 'string', required: false, visibility: 'user-only', description: 'Username for basic auth', }, password: { type: 'string', required: false, visibility: 'user-only', description: 'Password for basic auth', }, index: { type: 'string', required: true, visibility: 'user-or-llm', description: 'Index name (e.g., "products", "logs-2024")', }, documentId: { type: 'string', required: true, visibility: 'user-or-llm', description: 'Document ID to update (e.g., "abc123", "user_456")', }, document: { type: 'string', required: true, description: 'Partial document to merge as JSON string', }, retryOnConflict: { type: 'number', required: false, description: 'Number of retries on version conflict', }, }, request: { url: (params) => { const baseUrl = buildBaseUrl(params) let url = `${baseUrl}/${encodeURIComponent(params.index)}/_update/${encodeURIComponent(params.documentId)}` if (params.retryOnConflict !== undefined) { url += `?retry_on_conflict=${params.retryOnConflict}` } return url }, method: 'POST', headers: (params) => buildAuthHeaders(params), body: (params) => { try { return { doc: JSON.parse(params.document) } } catch { throw new Error('Invalid JSON document') } }, }, transformResponse: async (response: Response) => { if (!response.ok) { const errorText = await response.text() let errorMessage = `Elasticsearch error: ${response.status}` try { const errorJson = JSON.parse(errorText) errorMessage = errorJson.error?.reason || errorJson.error?.type || errorMessage } catch { errorMessage = errorText || errorMessage } return { success: false, output: { _index: '', _id: '' }, error: errorMessage, } } const data = await response.json() return { success: true, output: { _index: data._index, _id: data._id, _version: data._version, result: data.result, }, } }, outputs: { _index: { type: 'string', description: 'Index name', }, _id: { type: 'string', description: 'Document ID', }, _version: { type: 'number', description: 'New document version', }, result: { type: 'string', description: 'Operation result (updated or noop)', }, }, }