|
| 1 | +import { TinybirdIcon } from '@/components/icons' |
| 2 | +import type { BlockConfig } from '@/blocks/types' |
| 3 | +import { AuthMode } from '@/blocks/types' |
| 4 | +import type { TinybirdResponse } from '@/tools/tinybird/types' |
| 5 | + |
| 6 | +export const TinybirdBlock: BlockConfig<TinybirdResponse> = { |
| 7 | + type: 'tinybird', |
| 8 | + name: 'Tinybird', |
| 9 | + description: 'Send events and query data with Tinybird', |
| 10 | + authMode: AuthMode.ApiKey, |
| 11 | + longDescription: |
| 12 | + 'Interact with Tinybird using the Events API to stream JSON or NDJSON events, or use the Query API to execute SQL queries against Pipes and Data Sources.', |
| 13 | + docsLink: 'https://www.tinybird.co/docs/api-reference', |
| 14 | + category: 'tools', |
| 15 | + bgColor: '#2EF598', |
| 16 | + icon: TinybirdIcon, |
| 17 | + subBlocks: [ |
| 18 | + { |
| 19 | + id: 'operation', |
| 20 | + title: 'Operation', |
| 21 | + type: 'dropdown', |
| 22 | + options: [ |
| 23 | + { label: 'Send Events', id: 'tinybird_events' }, |
| 24 | + { label: 'Query', id: 'tinybird_query' }, |
| 25 | + ], |
| 26 | + value: () => 'tinybird_events', |
| 27 | + }, |
| 28 | + { |
| 29 | + id: 'base_url', |
| 30 | + title: 'Base URL', |
| 31 | + type: 'short-input', |
| 32 | + placeholder: 'https://api.tinybird.co', |
| 33 | + required: true, |
| 34 | + }, |
| 35 | + { |
| 36 | + id: 'token', |
| 37 | + title: 'API Token', |
| 38 | + type: 'short-input', |
| 39 | + placeholder: 'Enter your Tinybird API token', |
| 40 | + password: true, |
| 41 | + required: true, |
| 42 | + }, |
| 43 | + // Send Events operation inputs |
| 44 | + { |
| 45 | + id: 'datasource', |
| 46 | + title: 'Data Source', |
| 47 | + type: 'short-input', |
| 48 | + placeholder: 'my_events_datasource', |
| 49 | + condition: { field: 'operation', value: 'tinybird_events' }, |
| 50 | + required: true, |
| 51 | + }, |
| 52 | + { |
| 53 | + id: 'data', |
| 54 | + title: 'Data', |
| 55 | + type: 'code', |
| 56 | + placeholder: |
| 57 | + '{"event": "click", "timestamp": "2024-01-01T12:00:00Z"}\n{"event": "view", "timestamp": "2024-01-01T12:00:01Z"}', |
| 58 | + condition: { field: 'operation', value: 'tinybird_events' }, |
| 59 | + required: true, |
| 60 | + }, |
| 61 | + { |
| 62 | + id: 'format', |
| 63 | + title: 'Format', |
| 64 | + type: 'dropdown', |
| 65 | + options: [ |
| 66 | + { label: 'NDJSON (Newline-delimited JSON)', id: 'ndjson' }, |
| 67 | + { label: 'JSON', id: 'json' }, |
| 68 | + ], |
| 69 | + value: () => 'ndjson', |
| 70 | + condition: { field: 'operation', value: 'tinybird_events' }, |
| 71 | + }, |
| 72 | + { |
| 73 | + id: 'compression', |
| 74 | + title: 'Compression', |
| 75 | + type: 'dropdown', |
| 76 | + options: [ |
| 77 | + { label: 'None', id: 'none' }, |
| 78 | + { label: 'Gzip', id: 'gzip' }, |
| 79 | + ], |
| 80 | + value: () => 'none', |
| 81 | + mode: 'advanced', |
| 82 | + condition: { field: 'operation', value: 'tinybird_events' }, |
| 83 | + }, |
| 84 | + { |
| 85 | + id: 'wait', |
| 86 | + title: 'Wait for Acknowledgment', |
| 87 | + type: 'switch', |
| 88 | + value: () => 'false', |
| 89 | + mode: 'advanced', |
| 90 | + condition: { field: 'operation', value: 'tinybird_events' }, |
| 91 | + }, |
| 92 | + // Query operation inputs |
| 93 | + { |
| 94 | + id: 'query', |
| 95 | + title: 'SQL Query', |
| 96 | + type: 'code', |
| 97 | + placeholder: 'SELECT * FROM my_pipe FORMAT JSON\nOR\nSELECT * FROM my_pipe FORMAT CSV', |
| 98 | + condition: { field: 'operation', value: 'tinybird_query' }, |
| 99 | + required: true, |
| 100 | + }, |
| 101 | + { |
| 102 | + id: 'pipeline', |
| 103 | + title: 'Pipeline Name', |
| 104 | + type: 'short-input', |
| 105 | + placeholder: 'my_pipe (optional)', |
| 106 | + condition: { field: 'operation', value: 'tinybird_query' }, |
| 107 | + }, |
| 108 | + ], |
| 109 | + tools: { |
| 110 | + access: ['tinybird_events', 'tinybird_query'], |
| 111 | + config: { |
| 112 | + tool: (params) => params.operation || 'tinybird_events', |
| 113 | + params: (params) => { |
| 114 | + const operation = params.operation || 'tinybird_events' |
| 115 | + const result: Record<string, any> = { |
| 116 | + base_url: params.base_url, |
| 117 | + token: params.token, |
| 118 | + } |
| 119 | + |
| 120 | + if (operation === 'tinybird_events') { |
| 121 | + // Send Events operation |
| 122 | + if (!params.datasource) { |
| 123 | + throw new Error('Data Source is required for Send Events operation') |
| 124 | + } |
| 125 | + if (!params.data) { |
| 126 | + throw new Error('Data is required for Send Events operation') |
| 127 | + } |
| 128 | + |
| 129 | + result.datasource = params.datasource |
| 130 | + result.data = params.data |
| 131 | + result.format = params.format || 'ndjson' |
| 132 | + result.compression = params.compression || 'none' |
| 133 | + |
| 134 | + // Convert wait from string to boolean |
| 135 | + // Convert wait from string to boolean |
| 136 | + if (params.wait !== undefined) { |
| 137 | + const waitValue = |
| 138 | + typeof params.wait === 'string' ? params.wait.toLowerCase() : params.wait |
| 139 | + result.wait = waitValue === 'true' || waitValue === true |
| 140 | + } |
| 141 | + } else if (operation === 'tinybird_query') { |
| 142 | + // Query operation |
| 143 | + if (!params.query) { |
| 144 | + throw new Error('SQL Query is required for Query operation') |
| 145 | + } |
| 146 | + |
| 147 | + result.query = params.query |
| 148 | + if (params.pipeline) { |
| 149 | + result.pipeline = params.pipeline |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return result |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + inputs: { |
| 158 | + operation: { type: 'string', description: 'Operation to perform' }, |
| 159 | + base_url: { type: 'string', description: 'Tinybird API base URL' }, |
| 160 | + // Send Events inputs |
| 161 | + datasource: { |
| 162 | + type: 'string', |
| 163 | + description: 'Name of the Tinybird Data Source', |
| 164 | + }, |
| 165 | + data: { |
| 166 | + type: 'string', |
| 167 | + description: 'Data to send as JSON or NDJSON string', |
| 168 | + }, |
| 169 | + wait: { type: 'boolean', description: 'Wait for database acknowledgment' }, |
| 170 | + format: { |
| 171 | + type: 'string', |
| 172 | + description: 'Format of the events (ndjson or json)', |
| 173 | + }, |
| 174 | + compression: { |
| 175 | + type: 'string', |
| 176 | + description: 'Compression format (none or gzip)', |
| 177 | + }, |
| 178 | + // Query inputs |
| 179 | + query: { type: 'string', description: 'SQL query to execute' }, |
| 180 | + pipeline: { type: 'string', description: 'Optional pipeline name' }, |
| 181 | + // Common |
| 182 | + token: { type: 'string', description: 'Tinybird API Token' }, |
| 183 | + }, |
| 184 | + outputs: { |
| 185 | + // Send Events outputs |
| 186 | + successful_rows: { |
| 187 | + type: 'number', |
| 188 | + description: 'Number of rows successfully ingested', |
| 189 | + }, |
| 190 | + quarantined_rows: { |
| 191 | + type: 'number', |
| 192 | + description: 'Number of rows quarantined (failed validation)', |
| 193 | + }, |
| 194 | + // Query outputs |
| 195 | + data: { |
| 196 | + type: 'json', |
| 197 | + description: |
| 198 | + 'Query result data. FORMAT JSON: array of objects. Other formats (CSV, TSV, etc.): raw text string.', |
| 199 | + }, |
| 200 | + rows: { type: 'number', description: 'Number of rows returned (only with FORMAT JSON)' }, |
| 201 | + statistics: { |
| 202 | + type: 'json', |
| 203 | + description: |
| 204 | + 'Query execution statistics - elapsed time, rows read, bytes read (only with FORMAT JSON)', |
| 205 | + }, |
| 206 | + }, |
| 207 | +} |
0 commit comments