Skip to content

Commit 3c86a20

Browse files
authored
feat: more examples with chat-api and js client (#7)
1 parent 3d97812 commit 3c86a20

70 files changed

Lines changed: 2411 additions & 532 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ ENV/
3737
# OS specific files
3838
.DS_Store
3939
Thumbs.db
40-
node_modules/
40+
node_modules/
41+
.claude

README.md

Lines changed: 184 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,198 @@
1-
# Cube D3 Agent Platform Integration Examples
1+
# Cube AI Agent Integration Examples
22

3-
This repository contains a collection of examples demonstrating how to integrate the Cube D3 agent platform into your applications using the Chat API.
3+
This repository contains a collection of examples demonstrating how to integrate Cube AI Agents into your applications using the Chat API and advanced workflow orchestration.
44

55
## Overview
66

7-
Cube D3 is a powerful agent platform that enables you to build AI-powered data applications. This repository showcases practical integration patterns and implementations.
7+
Cube AI Agents enable you to build intelligent, data-driven applications with natural language interfaces. This repository showcases practical integration patterns from simple chat interfaces to complex multi-step workflows.
8+
9+
## Repository Structure
10+
11+
```
12+
d3_examples/
13+
├── shared/ # Shared utilities
14+
│ └── cube-agent-client/ # Reusable Cube Agent client library
15+
├── chat-api/ # Simple chat API example
16+
├── langgraph-analytics/ # LangGraph workflow example
17+
└── react-app/ # React application example
18+
```
819

920
## Examples
1021

11-
### 1. Chat API
12-
[Node.js example](chat-api/) for using the Cube Chat API with streaming responses.
22+
### 1. Cube Agent Client Library
23+
24+
**Location**: [shared/cube-agent-client/](shared/cube-agent-client/)
25+
26+
A reusable Node.js client library for interacting with Cube AI Agents. Provides a clean API for session management, authentication, and streaming chat interactions.
27+
28+
**Features**:
29+
- Simple `chat()` and `streamChat()` methods
30+
- Automatic session/token management
31+
- Detailed responses with thinking and tool calls
32+
- Support for Cube Cloud
33+
34+
**Quick Start**:
35+
```javascript
36+
import { CubeAgentClient } from '../shared/cube-agent-client/index.js';
37+
38+
const client = new CubeAgentClient({
39+
tenantName: 'your-tenant',
40+
agentId: 1,
41+
apiKey: 'your-api-key'
42+
});
43+
44+
const response = await client.chat('What is the total revenue?');
45+
```
46+
47+
### 2. Chat API
48+
49+
**Location**: [chat-api/](chat-api/)
50+
51+
A simple Node.js example for using the Cube Chat API with streaming responses. Great starting point for understanding the basics of Cube AI Agent integration.
52+
53+
**Features**:
54+
- Session generation and token authentication
55+
- Streaming chat responses
56+
- Real-time display of assistant messages, thinking process, and tool calls
57+
58+
**Quick Start**:
59+
```bash
60+
cd chat-api
61+
CUBE_TENANT_NAME=xxx CUBE_AGENT_ID=yyy CUBE_API_KEY=zzz node chat.js "Your question"
62+
```
63+
64+
### 3. LangGraph Analytics Workflow
65+
66+
**Location**: [langgraph-analytics/](langgraph-analytics/)
67+
68+
An advanced example demonstrating stateful analytics workflows using **LangGraph.js** and Cube AI Agents. Shows how to build complex, multi-step data analysis workflows with state management, retry logic, and conditional routing.
69+
70+
**Features**:
71+
- Graph-based workflow orchestration
72+
- Stateful execution with persistent memory
73+
- Automatic retry logic for failed queries
74+
- Question classification and intelligent routing
75+
- Multi-step analysis pipeline: Classify → Query → Analyze → Insights
76+
77+
**Workflow**:
78+
```
79+
START → Classify Question → Query Cube → Analyze Results → Generate Insights → END
80+
↓ (retry on error)
81+
Query Cube
82+
```
83+
84+
**Quick Start**:
85+
```bash
86+
cd langgraph-analytics
87+
npm install
88+
npm start "What are the top 10 customers by revenue?"
89+
```
90+
91+
**Why LangGraph?**:
92+
LangGraph.js enables cyclic graphs with loops and state management, unlike linear chains in LangChain.js. Perfect for building sophisticated AI agents with complex control flow.
93+
94+
### 4. React App
95+
96+
**Location**: [react-app/](react-app/)
1397

14-
### 2. D3 React App
15-
[Next.js React application](d3-react-app/) that demonstrates integration with the D3 AI Agent API for streaming chat functionality.
98+
A Next.js React application demonstrating how to integrate Cube AI Agents into a modern web application with streaming chat functionality.
99+
100+
**Features**:
101+
- React-based chat interface
102+
- Streaming responses with real-time updates
103+
- UI components for displaying agent responses
16104

17105
## Getting Started
18106

19-
Each example directory contains its own README with detailed setup instructions and usage examples.
107+
### Prerequisites
108+
109+
- Node.js 20+
110+
- Access to Cube Cloud
111+
- Cube AI Agent configured
112+
113+
### Environment Variables
114+
115+
All examples support the following environment variables:
116+
117+
```bash
118+
# Required
119+
CUBE_TENANT_NAME=your-tenant-name
120+
CUBE_AGENT_ID=1
121+
CUBE_API_KEY=your-api-key
122+
123+
# Optional (for custom deployments)
124+
CUBE_API_URL=https://your-custom-domain.com
125+
AI_ENGINEER_URL=https://your-ai-engineer-url.com
126+
```
127+
128+
## Example Progression
129+
130+
We recommend exploring the examples in this order:
131+
132+
1. **Start with [chat-api/](chat-api/)** - Learn the basics of Cube AI Agent integration
133+
2. **Review [shared/cube-agent-client/](shared/cube-agent-client/)** - Understand the reusable client library
134+
3. **Explore [langgraph-analytics/](langgraph-analytics/)** - Build complex workflows with state management
135+
4. **Check out [react-app/](react-app/)** - Integrate into a React application
136+
137+
## Use Cases
138+
139+
### Simple Chat Interface
140+
Use the **chat-api** or **cube-agent-client** examples for:
141+
- Basic Q&A interfaces
142+
- Simple data queries
143+
- Prototyping and testing
144+
145+
### Complex Workflows
146+
Use the **langgraph-analytics** example for:
147+
- Multi-step data analysis pipelines
148+
- Stateful conversations with memory
149+
- Conditional logic and branching
150+
- Retry mechanisms and error handling
151+
- Business process automation
152+
153+
### Web Applications
154+
Use the **react-app** example for:
155+
- Interactive dashboards
156+
- BI copilots
157+
- Embedded analytics
158+
- Customer-facing data apps
20159

21160
## API Documentation
22161

23-
Full documentation: https://docs.cube.dev/embed/api/chat
162+
- [Cube Chat API](https://docs.cube.dev/embed/api/chat)
163+
- [LangGraph.js Documentation](https://langchain-ai.github.io/langgraphjs/)
164+
165+
## Architecture
166+
167+
### High-Level Flow
168+
169+
```
170+
User Question
171+
172+
[Chat API / LangGraph Workflow]
173+
174+
Cube Agent Client Library
175+
176+
1. Generate Session (Cube API)
177+
2. Get Auth Token (Cube API)
178+
3. Stream Chat (AI Engineer API)
179+
180+
Cube AI Agent
181+
182+
Response (with thinking, tool calls, results)
183+
```
184+
185+
### Components
186+
187+
- **Cube Agent Client**: Handles authentication and communication
188+
- **LangGraph Workflow**: Orchestrates multi-step analysis
189+
- **Cube AI Agent**: Processes queries and generates insights
190+
- **Tool Calls**: Agent can query data sources, perform calculations, etc.
191+
192+
## Contributing
193+
194+
Each example directory contains its own README with detailed setup instructions and usage examples. Feel free to extend these examples for your specific use cases.
195+
196+
## License
197+
198+
MIT

chat-api/README.md

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Node.js example for using the Cube Chat API with streaming responses.
44

5+
This example uses the [Cube Agent Client library](../shared/cube-agent-client/) for seamless integration with Cube AI Agents.
6+
57
## Usage
68

79
```bash
@@ -12,7 +14,7 @@ CUBE_TENANT_NAME=xxx CUBE_AGENT_ID=yyy CUBE_API_KEY=zzz node chat.js "Your quest
1214

1315
| Variable | Required | Default | Description |
1416
|----------|----------|---------|-------------|
15-
| `CUBE_TENANT_NAME` | Yes | - | Your Cube Cloud tenant name or 'localhost' |
17+
| `CUBE_TENANT_NAME` | Yes | - | Your Cube Cloud tenant name |
1618
| `CUBE_AGENT_ID` | Yes | - | The ID of your AI agent |
1719
| `CUBE_API_KEY` | Yes | - | Your Cube Cloud API key |
1820
| `CUBE_API_URL` | No | `https://{TENANT_NAME}.cubecloud.dev` | Base URL for Cube API endpoints |
@@ -21,15 +23,21 @@ CUBE_TENANT_NAME=xxx CUBE_AGENT_ID=yyy CUBE_API_KEY=zzz node chat.js "Your quest
2123

2224
## Features
2325

24-
- Session generation and token authentication
25-
- Streaming chat responses
26-
- Real-time display of:
27-
- Assistant messages (streamed)
28-
- Agent thinking process
29-
- Tool calls and results
26+
- **Shared Library**: Uses the reusable [Cube Agent Client](../shared/cube-agent-client/) library
27+
- **Session Management**: Automatic session generation and token authentication
28+
- **Streaming Responses**: Real-time display of chat responses as they arrive
29+
- **Debug Mode**: View thinking process, tool calls, and full event stream
30+
31+
### Real-time Display
32+
33+
- Assistant messages (streamed character by character)
34+
- Agent thinking process (in DEBUG mode)
35+
- Tool calls and results (in DEBUG mode)
3036

3137
## Example Usage
3238

39+
### Basic Usage
40+
3341
```bash
3442
# Default question
3543
node chat.js
@@ -38,6 +46,33 @@ node chat.js
3846
node chat.js "What is the total revenue?"
3947
```
4048

49+
### With Debug Mode
50+
51+
```bash
52+
# See detailed agent activity
53+
DEBUG=1 node chat.js "What are the top 10 customers?"
54+
```
55+
56+
This will display:
57+
- Thinking process: Shows how the agent analyzes your question
58+
- Tool calls: Shows which data sources are queried
59+
- Full event stream: Complete JSON events for debugging
60+
61+
## How It Works
62+
63+
This example demonstrates the simplest way to integrate Cube AI Agents:
64+
65+
1. **Import the Client**: Uses the shared `CubeAgentClient` library
66+
2. **Configure**: Set up with your Cube Cloud credentials
67+
3. **Stream Chat**: Call `streamChat()` with callbacks for real-time output
68+
4. **Display Results**: Show responses as they arrive
69+
70+
## Related Examples
71+
72+
- [Cube Agent Client Library](../shared/cube-agent-client/) - The underlying client library
73+
- [LangGraph Analytics](../langgraph-analytics/) - Advanced stateful workflows with LangGraph.js
74+
- [React App](../react-app/) - React application integration
75+
4176
## API Documentation
4277

4378
Full documentation: https://docs.cube.dev/embed/api/chat

0 commit comments

Comments
 (0)