|
1 | 1 | import type { Transport } from '@connectrpc/connect'; |
2 | 2 | import { createConnectTransport } from '@connectrpc/connect-web'; |
3 | | -import { addBearerTokenInterceptor } from 'config'; |
| 3 | +import { addBearerTokenInterceptor, config, isEmbedded } from 'config'; |
4 | 4 | import { protobufRegistry } from 'protobuf-registry'; |
5 | 5 | import { useMemo } from 'react'; |
6 | 6 |
|
7 | 7 | /** |
8 | 8 | * Custom hook to create and memoize a Connect transport for AI Gateway API calls |
9 | 9 | * |
10 | | - * Uses base path: /.redpanda/api/ |
11 | | - * Connect Query will append the service path: redpanda.api.aigateway.v1.GatewayService/ListGateways |
12 | | - * Full path becomes: /.redpanda/api/redpanda.api.aigateway.v1.GatewayService/ListGateways |
| 10 | + * In Development: |
| 11 | + * - Uses base path: /.redpanda/api/ |
| 12 | + * - Dev server proxies to: https://ai-gateway.{clusterId}.clusters.ign.rdpa.co |
13 | 13 | * |
14 | | - * Dev server proxies /.redpanda/api/redpanda.api.aigateway.v1 to: |
15 | | - * https://ai-gateway.${CLUSTER_ID}.clusters.ign.rdpa.co |
| 14 | + * In Production (Embedded): |
| 15 | + * - Uses config.aiGatewayUrl set by cloud-ui parent app (from REACT_APP_AI_GATEWAY_URL env var) |
| 16 | + * - Appends /.redpanda/api to the base URL |
| 17 | + * |
| 18 | + * In Production (Standalone): |
| 19 | + * - Uses relative path /.redpanda/api (backend handles routing) |
16 | 20 | * |
17 | 21 | * @returns Transport instance configured for AI Gateway communication |
18 | 22 | */ |
19 | 23 | export const useAIGatewayTransport = (): Transport => { |
20 | 24 | const aiGatewayTransport = useMemo(() => { |
21 | | - // Use /.redpanda/api/ base path (AI Gateway's Connect RPC endpoint prefix) |
22 | | - // Dev server will proxy to the correct AI Gateway based on cluster ID |
23 | | - const baseUrl = '/.redpanda/api'; |
| 25 | + // In development, use relative path and rely on dev server proxy |
| 26 | + if (process.env.NODE_ENV === 'development') { |
| 27 | + return createConnectTransport({ |
| 28 | + baseUrl: '/.redpanda/api', |
| 29 | + interceptors: [addBearerTokenInterceptor], |
| 30 | + jsonOptions: { |
| 31 | + registry: protobufRegistry, |
| 32 | + }, |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + // In production embedded mode (cloud-ui), use AI Gateway URL from config |
| 37 | + if (isEmbedded() && config.aiGatewayUrl) { |
| 38 | + // Ensure URL ends with /.redpanda/api |
| 39 | + const baseUrl = config.aiGatewayUrl.endsWith('/.redpanda/api') |
| 40 | + ? config.aiGatewayUrl |
| 41 | + : `${config.aiGatewayUrl}/.redpanda/api`; |
| 42 | + |
| 43 | + return createConnectTransport({ |
| 44 | + baseUrl, |
| 45 | + interceptors: [addBearerTokenInterceptor], |
| 46 | + jsonOptions: { |
| 47 | + registry: protobufRegistry, |
| 48 | + }, |
| 49 | + }); |
| 50 | + } |
24 | 51 |
|
| 52 | + // Fallback to relative path for standalone mode |
25 | 53 | return createConnectTransport({ |
26 | | - baseUrl, |
| 54 | + baseUrl: '/.redpanda/api', |
27 | 55 | interceptors: [addBearerTokenInterceptor], |
28 | 56 | jsonOptions: { |
29 | 57 | registry: protobufRegistry, |
|
0 commit comments