|
| 1 | +import { |
| 2 | + type DatabricksGenieAgentAskParams, |
| 3 | + type DatabricksGenieAgentAskResponse, |
| 4 | + GENIE_AGENT_ITEM_OUTPUT_PROPERTIES, |
| 5 | +} from '@/tools/databricks/types' |
| 6 | +import { |
| 7 | + databricksUrl, |
| 8 | + GENIE_SPACE_PARAMS, |
| 9 | + genieAgentErrorMessage, |
| 10 | + genieAgentPath, |
| 11 | + mapGenieAgentItem, |
| 12 | + parseGenieAgentQuery, |
| 13 | + parseGenieAgentStream, |
| 14 | +} from '@/tools/databricks/utils' |
| 15 | +import type { ToolConfig } from '@/tools/types' |
| 16 | + |
| 17 | +export const genieAgentAskTool: ToolConfig< |
| 18 | + DatabricksGenieAgentAskParams, |
| 19 | + DatabricksGenieAgentAskResponse |
| 20 | +> = { |
| 21 | + id: 'databricks_genie_agent_ask', |
| 22 | + name: 'Databricks Genie Agent Ask', |
| 23 | + description: |
| 24 | + 'Ask a Genie agent a question in agent mode, where Genie plans multi-step research, runs several SQL queries, and writes a report with citations. Waits for the final report. Starts a new agent-mode conversation, or continues one when a conversation ID is given. Agent mode is in preview and must be enabled on the workspace.', |
| 25 | + version: '1.0.0', |
| 26 | + |
| 27 | + params: { |
| 28 | + ...GENIE_SPACE_PARAMS, |
| 29 | + spaceId: { |
| 30 | + ...GENIE_SPACE_PARAMS.spaceId, |
| 31 | + description: 'The ID of the Genie space (the Genie agent ID)', |
| 32 | + }, |
| 33 | + content: { |
| 34 | + type: 'string', |
| 35 | + required: true, |
| 36 | + visibility: 'user-or-llm', |
| 37 | + description: 'The question to ask the Genie agent', |
| 38 | + }, |
| 39 | + conversationId: { |
| 40 | + type: 'string', |
| 41 | + required: false, |
| 42 | + visibility: 'user-or-llm', |
| 43 | + description: 'Continue this agent-mode conversation. Omit to start a new conversation.', |
| 44 | + }, |
| 45 | + enableVisualization: { |
| 46 | + type: 'boolean', |
| 47 | + required: false, |
| 48 | + visibility: 'user-or-llm', |
| 49 | + description: 'Ask the agent to also generate charts where they fit', |
| 50 | + }, |
| 51 | + }, |
| 52 | + |
| 53 | + request: { |
| 54 | + modelInput: { |
| 55 | + mode: 'project', |
| 56 | + select: (params) => ({ content: params.content }), |
| 57 | + }, |
| 58 | + url: (params) => databricksUrl(params.host, `${genieAgentPath(params.spaceId)}/responses`), |
| 59 | + method: 'POST', |
| 60 | + headers: (params) => ({ |
| 61 | + 'Content-Type': 'application/json', |
| 62 | + Accept: 'text/event-stream', |
| 63 | + Authorization: `Bearer ${params.apiKey}`, |
| 64 | + }), |
| 65 | + body: (params) => { |
| 66 | + const conversationId = params.conversationId?.trim() |
| 67 | + return { |
| 68 | + input: [ |
| 69 | + { |
| 70 | + type: 'message', |
| 71 | + role: 'user', |
| 72 | + content: [{ type: 'input_text', text: params.content }], |
| 73 | + }, |
| 74 | + ], |
| 75 | + ...(conversationId ? { conversation_id: conversationId } : {}), |
| 76 | + ...(params.enableVisualization ? { enable_viz: true } : {}), |
| 77 | + } |
| 78 | + }, |
| 79 | + }, |
| 80 | + |
| 81 | + transformResponse: async (response: Response) => { |
| 82 | + const { created, final } = parseGenieAgentStream(await response.text()) |
| 83 | + |
| 84 | + if (!final) { |
| 85 | + throw new Error( |
| 86 | + created?.id |
| 87 | + ? `Genie agent stream ended before response ${created.id} completed` |
| 88 | + : 'Genie agent stream ended without a response' |
| 89 | + ) |
| 90 | + } |
| 91 | + const responseId = final.id ?? created?.id ?? '' |
| 92 | + const conversationId = final.conversation_id ?? created?.conversation_id ?? '' |
| 93 | + const context = `Genie agent response ${responseId} in conversation ${conversationId}` |
| 94 | + |
| 95 | + if (final.status === 'failed') { |
| 96 | + throw new Error(`${context} failed: ${genieAgentErrorMessage(final.error)}`) |
| 97 | + } |
| 98 | + |
| 99 | + const items = (final.output ?? []).map(mapGenieAgentItem) |
| 100 | + const joinMessages = (role: string) => |
| 101 | + items |
| 102 | + .filter((item) => item.type === 'message' && item.role === role && item.text) |
| 103 | + .map((item) => item.text) |
| 104 | + .join('\n\n') || null |
| 105 | + const report = joinMessages('assistant') |
| 106 | + /** A message item can be a system error rather than an assistant report. */ |
| 107 | + const error = joinMessages('system') |
| 108 | + |
| 109 | + if (!report && error) { |
| 110 | + throw new Error(`${context} ended with an error: ${error}`) |
| 111 | + } |
| 112 | + |
| 113 | + return { |
| 114 | + success: true, |
| 115 | + output: { |
| 116 | + responseId, |
| 117 | + conversationId, |
| 118 | + status: final.status ?? 'completed', |
| 119 | + report, |
| 120 | + queries: items |
| 121 | + .filter((item) => item.type === 'function_call' && item.name === 'execute_sql') |
| 122 | + .map(parseGenieAgentQuery), |
| 123 | + items, |
| 124 | + createdAt: final.created_at ?? null, |
| 125 | + error, |
| 126 | + }, |
| 127 | + } |
| 128 | + }, |
| 129 | + |
| 130 | + outputs: { |
| 131 | + responseId: { type: 'string', description: 'Agent response ID' }, |
| 132 | + conversationId: { |
| 133 | + type: 'string', |
| 134 | + description: 'Agent-mode conversation ID; pass it back to ask a follow-up', |
| 135 | + }, |
| 136 | + status: { type: 'string', description: 'Response status (completed)' }, |
| 137 | + report: { |
| 138 | + type: 'string', |
| 139 | + description: "The agent's final report, with citation links", |
| 140 | + nullable: true, |
| 141 | + }, |
| 142 | + queries: { |
| 143 | + type: 'array', |
| 144 | + description: 'SQL queries the agent ran', |
| 145 | + items: { |
| 146 | + type: 'object', |
| 147 | + properties: { |
| 148 | + callId: { type: 'string', description: 'Function call ID' }, |
| 149 | + title: { type: 'string', description: 'Query title', nullable: true }, |
| 150 | + sql: { type: 'string', description: 'SQL the agent ran', nullable: true }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + items: { |
| 155 | + type: 'array', |
| 156 | + description: 'Every output item: reasoning, SQL calls, query results, and messages', |
| 157 | + items: { |
| 158 | + type: 'object', |
| 159 | + properties: GENIE_AGENT_ITEM_OUTPUT_PROPERTIES, |
| 160 | + }, |
| 161 | + }, |
| 162 | + createdAt: { |
| 163 | + type: 'number', |
| 164 | + description: 'When the response was created (Unix epoch seconds)', |
| 165 | + nullable: true, |
| 166 | + }, |
| 167 | + error: { |
| 168 | + type: 'string', |
| 169 | + description: 'System error message the agent reported alongside its report', |
| 170 | + nullable: true, |
| 171 | + }, |
| 172 | + }, |
| 173 | +} |
0 commit comments