@hasura/promptql 1.0.0 → 2.0.0-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +252 -139
- package/biome.json +15 -0
- package/dist/index.d.mts +5155 -0
- package/dist/index.d.ts +5155 -5
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -925
- package/package.json +42 -53
- package/tsconfig.json +29 -0
- package/LICENSE +0 -201
- package/dist/index.iife.js +0 -1
- package/dist/index.umd.js +0 -1
- package/dist/promptql.d.ts +0 -598
- package/dist/types.d.ts +0 -26
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/query-chunks.d.ts +0 -35
- package/dist/utils/query-stream.d.ts +0 -3
- package/dist/utils/utils.d.ts +0 -36
- package/dist/v1/client.d.ts +0 -8
- package/dist/v1/index.d.ts +0 -11
- package/dist/v1/types.d.ts +0 -100
- package/dist/v2/client.d.ts +0 -8
- package/dist/v2/index.d.ts +0 -2
- package/dist/v2/types.d.ts +0 -75
package/README.md
CHANGED
|
@@ -1,196 +1,309 @@
|
|
|
1
|
-
# PromptQL
|
|
1
|
+
# PromptQL TypeScript SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The PromptQL TypeScript SDK is a general-purpose SDK that interacts with the [PromptQL API](https://promptql.hasura.io) by Hasura. PromptQL enables natural language querying of your data through an AI-powered interface.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
> [!NOTE]
|
|
6
|
+
> This SDK supports PromptQL v2. Please install [@hasura/promptql@1.0.0](https://www.npmjs.com/package/@hasura/promptql/v/1.0.0) if you wants to integrate PromptQL v1.
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
## Features
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
- **Type-safe API Client**: Fully typed TypeScript SDK with auto-generated types from GraphQL schema
|
|
11
|
+
- **Thread Management**: Create, list, and manage conversation threads
|
|
12
|
+
- **Real-time Streaming**: Subscribe to thread events with GraphQL subscriptions using RxJS observables
|
|
13
|
+
- **Authentication**: Built-in authentication handling with service account tokens
|
|
14
|
+
- **Query Planning**: Access to AI agent query plans and execution steps
|
|
15
|
+
- **Artifact Support**: Handle modified artifacts from AI responses
|
|
16
|
+
- **CLI Tool**: Command-line interface for quick interactions
|
|
17
|
+
- **Flexible Configuration**: Support for custom base URLs, headers, and fetch implementations
|
|
18
|
+
|
|
19
|
+
## Requirements
|
|
20
|
+
|
|
21
|
+
- Node.js >= 24
|
|
22
|
+
- TypeScript >= 5.9.2
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm install @hasura/promptql@latest
|
|
11
28
|
```
|
|
12
29
|
|
|
13
|
-
|
|
30
|
+
Or with your preferred package manager:
|
|
14
31
|
|
|
15
|
-
|
|
32
|
+
```bash
|
|
33
|
+
# Yarn
|
|
34
|
+
yarn add @hasura/promptql@latest
|
|
16
35
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
- Security headers and your Project API endpoint (version 1) or build version (version 2).
|
|
36
|
+
# pnpm
|
|
37
|
+
pnpm add @hasura/promptql@latest
|
|
20
38
|
|
|
21
|
-
|
|
39
|
+
# Bun
|
|
40
|
+
bun add @hasura/promptql@latest
|
|
41
|
+
```
|
|
22
42
|
|
|
23
|
-
|
|
43
|
+
## Quick Start
|
|
24
44
|
|
|
25
|
-
|
|
45
|
+
### Getting Service Account Token
|
|
26
46
|
|
|
27
|
-
|
|
28
|
-
|
|
47
|
+
1. Visit your Hasura project console
|
|
48
|
+
2. Navigate to Project Settings
|
|
49
|
+
3. Go to Service Accounts section
|
|
50
|
+
4. Create a new service account token with `Read Only` permission. Avoid using the `Admin` role. Hackers can modify your project metadata if the service account token is leaked.
|
|
51
|
+
5. Copy the token and use it in your SDK configuration
|
|
29
52
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
// }}
|
|
53
|
+
For more details, check the [Hasura documentation](https://hasura.io/docs/3.0/project-configuration/project-management/service-accounts/#how-to-create-service-account).
|
|
54
|
+
|
|
55
|
+
### Basic Usage
|
|
56
|
+
|
|
57
|
+
#### Start a PromptQL thread
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { PromptQLSdk } from '@hasura/promptql';
|
|
61
|
+
|
|
62
|
+
// Initialize the SDK
|
|
63
|
+
const sdk = new PromptQLSdk({
|
|
64
|
+
projectId: 'your-project-id',
|
|
65
|
+
serviceAccountToken: 'your-service-account-token',
|
|
44
66
|
});
|
|
45
|
-
```
|
|
46
67
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
],
|
|
60
|
-
ddn: {
|
|
61
|
-
// you can override the default ddn config,
|
|
62
|
-
// for example, dynamic auth credentials
|
|
63
|
-
headers: {}
|
|
64
|
-
}
|
|
68
|
+
// Start a new thread
|
|
69
|
+
const thread = await sdk.thread.start({
|
|
70
|
+
message: 'What are the top 10 customers by revenue?',
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
console.log('Thread ID:', thread.thread_id);
|
|
74
|
+
|
|
75
|
+
// Stream events from the thread
|
|
76
|
+
sdk.thread.streamEvents(thread.thread_id).subscribe({
|
|
77
|
+
next: (events) => {
|
|
78
|
+
events.forEach(event => {
|
|
79
|
+
console.log('Event:', event);
|
|
65
80
|
});
|
|
81
|
+
},
|
|
82
|
+
error: (err) => console.error('Error:', err),
|
|
83
|
+
complete: () => console.log('Interaction complete'),
|
|
84
|
+
});
|
|
85
|
+
```
|
|
66
86
|
|
|
67
|
-
|
|
68
|
-
}
|
|
87
|
+
#### Continue a Conversation
|
|
69
88
|
|
|
70
|
-
|
|
71
|
-
|
|
89
|
+
```typescript
|
|
90
|
+
// Send a follow-up message to an existing thread
|
|
91
|
+
const result = await sdk.thread.sendMessage({
|
|
92
|
+
threadId: 'existing-thread-id',
|
|
93
|
+
message: 'Now show me their average order value',
|
|
72
94
|
});
|
|
95
|
+
|
|
96
|
+
// Stream the response
|
|
97
|
+
sdk.streamEvents(result.thread_id, result.thread_event_id)
|
|
98
|
+
.subscribe({
|
|
99
|
+
next: (events) => console.log('New events:', events),
|
|
100
|
+
});
|
|
73
101
|
```
|
|
74
102
|
|
|
75
|
-
|
|
103
|
+
#### Wait until the final response only
|
|
104
|
+
|
|
105
|
+
Use the `lastValueFrom` function to wrap the subscription.
|
|
76
106
|
|
|
77
|
-
|
|
107
|
+
```typescript
|
|
108
|
+
import { PromptQLSdk } from '@hasura/promptql';
|
|
109
|
+
import { lastValueFrom } from "rxjs";
|
|
78
110
|
|
|
79
|
-
|
|
111
|
+
// Initialize the SDK
|
|
112
|
+
const sdk = new PromptQLSdk({
|
|
113
|
+
projectId: 'your-project-id',
|
|
114
|
+
serviceAccountToken: 'your-service-account-token',
|
|
115
|
+
});
|
|
80
116
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
117
|
+
// Start a new thread
|
|
118
|
+
const thread = await sdk.thread.start({
|
|
119
|
+
message: 'What are the top 10 customers by revenue?',
|
|
120
|
+
});
|
|
84
121
|
|
|
85
|
-
|
|
122
|
+
console.log('Thread ID:', thread.thread_id);
|
|
86
123
|
|
|
87
|
-
|
|
88
|
-
|
|
124
|
+
const events = await lastValueFrom(
|
|
125
|
+
sdk.thread.streamEvents(
|
|
126
|
+
result.thread_id,
|
|
127
|
+
),
|
|
128
|
+
);
|
|
89
129
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
ddn: {
|
|
93
|
-
// build_version: '<your-build-version>',
|
|
94
|
-
headers: {
|
|
95
|
-
'Authorization': '<credential>'
|
|
96
|
-
}
|
|
97
|
-
},
|
|
130
|
+
events.forEach(event => {
|
|
131
|
+
console.log('Event:', event);
|
|
98
132
|
});
|
|
99
133
|
```
|
|
100
134
|
|
|
101
|
-
|
|
135
|
+
#### Print new events only
|
|
102
136
|
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
137
|
+
```typescript
|
|
138
|
+
import { PromptQLSdk, diffThreadEvents } from '@hasura/promptql';
|
|
139
|
+
import { pairwise, map } from 'rxjs';
|
|
140
|
+
|
|
141
|
+
// Initialize the SDK
|
|
142
|
+
const sdk = new PromptQLSdk({
|
|
143
|
+
projectId: 'your-project-id',
|
|
144
|
+
serviceAccountToken: 'your-service-account-token',
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// Start a new thread
|
|
148
|
+
const thread = await sdk.thread.start({
|
|
149
|
+
message: 'What are the top 10 customers by revenue?',
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
console.log('Thread ID:', thread.thread_id);
|
|
153
|
+
|
|
154
|
+
// Stream events from the thread
|
|
155
|
+
sdk.streamEvents(thread.thread_id)
|
|
156
|
+
.pipe(
|
|
157
|
+
pairwise(),
|
|
158
|
+
map(([prev, curr]) => diffThreadEvents(prev, curr)),
|
|
159
|
+
)
|
|
160
|
+
.subscribe({
|
|
161
|
+
next: (events) => {
|
|
162
|
+
events.forEach(event => {
|
|
163
|
+
console.log('Event:', event);
|
|
164
|
+
});
|
|
165
|
+
},
|
|
166
|
+
error: (err) => console.error('Error:', err),
|
|
167
|
+
complete: () => console.log('Interaction complete'),
|
|
168
|
+
});
|
|
108
169
|
```
|
|
109
170
|
|
|
110
|
-
|
|
171
|
+
### Listing Threads
|
|
111
172
|
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
173
|
+
```typescript
|
|
174
|
+
const threads = await sdk.thread.list({
|
|
175
|
+
limit: 20,
|
|
176
|
+
offset: 0,
|
|
177
|
+
order_by: [{ created_at: 'desc' }],
|
|
178
|
+
where: {
|
|
179
|
+
// Optional filters
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
threads.forEach(thread => {
|
|
184
|
+
console.log(`Thread ${thread.id}:`, thread.title);
|
|
185
|
+
});
|
|
118
186
|
```
|
|
119
187
|
|
|
120
|
-
|
|
188
|
+
### Get a Specific Thread
|
|
121
189
|
|
|
122
|
-
|
|
190
|
+
```typescript
|
|
191
|
+
const thread = await sdk.thread.get('thread-id');
|
|
192
|
+
console.log('Thread:', thread);
|
|
193
|
+
```
|
|
123
194
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
195
|
+
## Event Handling
|
|
196
|
+
|
|
197
|
+
### Basics
|
|
198
|
+
|
|
199
|
+
The data structure of event data is the union of many event type. Each event type contains a different payload. You can go to the definition of `ThreadEventData` type to view the full data structure details.
|
|
200
|
+
|
|
201
|
+
The SDK also provides utility functions to parse different event types, for example, the following example catches and prints the query plan event and the final generated response:
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
import {
|
|
205
|
+
getAgentPlanGenerationStartedEvent,
|
|
206
|
+
getAgentPlanStepGeneratedEvent,
|
|
207
|
+
getAgentGeneratedResponse,
|
|
208
|
+
getUserMessageEvent,
|
|
209
|
+
} from '@hasura/promptql';
|
|
210
|
+
|
|
211
|
+
sdk.thread.streamEvents(threadId).subscribe({
|
|
212
|
+
next: (events) => {
|
|
213
|
+
events.forEach(event => {
|
|
214
|
+
// Check for plan generation start
|
|
215
|
+
const planStart = getAgentPlanGenerationStartedEvent(event.event_data);
|
|
216
|
+
if (planStart) {
|
|
217
|
+
console.log('Query Plan:\n');
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Check for plan steps
|
|
222
|
+
const planStep = getAgentPlanStepGeneratedEvent(event.event_data);
|
|
223
|
+
if (planStep) {
|
|
224
|
+
console.log(` - Step ${planStep.step_index}: ${planStep.step_title}\n`);
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Check for final response
|
|
229
|
+
const response = getAgentGeneratedResponse(event.event_data);
|
|
230
|
+
if (response) {
|
|
231
|
+
const xmlRegex = /<\w+.*\/>/gm;
|
|
232
|
+
console.log('Response:', source.replaceAll(xmlRegex, response.response.message));
|
|
233
|
+
if (response.response.modified_artifacts) {
|
|
234
|
+
console.log('Artifacts:', response.response.modified_artifacts);
|
|
235
|
+
}
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
},
|
|
240
|
+
});
|
|
129
241
|
```
|
|
130
242
|
|
|
131
|
-
###
|
|
243
|
+
### Handle XML tags in generated response message
|
|
244
|
+
|
|
245
|
+
The generated message may contains XML tags. They represents custom UI elements that the markdown text doesn't support. You should parse or remove them if they aren't required.
|
|
132
246
|
|
|
133
|
-
|
|
247
|
+
Common XML tags are:
|
|
134
248
|
|
|
135
|
-
|
|
249
|
+
- `<artifact type="table" identifier="last_5_products" />`: the identifier of the artifact block.
|
|
250
|
+
- `<user_mention id="user_id" />`: mentioned user in the chat message.
|
|
251
|
+
- `<user_group_mention id="group_id" />`: mentioned group in the chat message.
|
|
252
|
+
- `<promptql_mention />`: represents a `@promptql` tag in the chat message.
|
|
136
253
|
|
|
137
|
-
|
|
254
|
+
You can remove them using regular expressions or parse the message to XML elements for advanced format.
|
|
138
255
|
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
queryOptions?: FetchOptions
|
|
143
|
-
) => Promise<QueryResponse>
|
|
256
|
+
```typescript
|
|
257
|
+
const xmlRegex = /<\w+.*\/>/gm;
|
|
258
|
+
console.log(source.replaceAll(xmlRegex, ""));
|
|
144
259
|
```
|
|
145
260
|
|
|
146
|
-
|
|
261
|
+
## Configuration Options
|
|
147
262
|
|
|
148
|
-
|
|
149
|
-
|
|
263
|
+
```typescript
|
|
264
|
+
interface PromptQLSdkOptions {
|
|
265
|
+
// Required: Project ID from PromptQL console
|
|
266
|
+
projectId: string;
|
|
150
267
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
body: PromptQLQueryRequestV1,
|
|
154
|
-
callback?: (data: QueryResponseChunk) => void | Promise<void>,
|
|
155
|
-
queryOptions?: FetchOptions
|
|
156
|
-
) Promise<Response>;
|
|
157
|
-
```
|
|
268
|
+
// Required: Service account token for authentication
|
|
269
|
+
serviceAccountToken: string;
|
|
158
270
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
}
|
|
169
|
-
],
|
|
170
|
-
},
|
|
171
|
-
async (chunk) => {
|
|
172
|
-
console.log(chunk);
|
|
173
|
-
},
|
|
174
|
-
);
|
|
175
|
-
```
|
|
271
|
+
// Optional: Base URL of PromptQL data plane
|
|
272
|
+
// Default: 'https://promptql.ddn.hasura.app'
|
|
273
|
+
promptqlBaseUrl?: string;
|
|
274
|
+
|
|
275
|
+
// Optional: Custom authentication host (for self-hosted control plane)
|
|
276
|
+
authHost?: string;
|
|
277
|
+
|
|
278
|
+
// Optional: Custom fetch implementation
|
|
279
|
+
fetch?: typeof fetch;
|
|
176
280
|
|
|
177
|
-
|
|
281
|
+
// Optional: Additional headers for all requests
|
|
282
|
+
headers?: Record<string, string>;
|
|
178
283
|
|
|
179
|
-
|
|
284
|
+
// Optional: IANA timezone for time-based queries
|
|
285
|
+
// Default: Client's timezone
|
|
286
|
+
timezone?: string;
|
|
180
287
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
) => Promise<PromptQlExecutionResult>
|
|
288
|
+
// Optional: DDN build ID
|
|
289
|
+
// Default: Applied build
|
|
290
|
+
buildId?: string;
|
|
291
|
+
}
|
|
186
292
|
```
|
|
187
293
|
|
|
188
|
-
##
|
|
294
|
+
## License
|
|
189
295
|
|
|
190
|
-
|
|
296
|
+
Apache License 2.0 - see [LICENSE](LICENSE) file for details.
|
|
191
297
|
|
|
192
|
-
|
|
298
|
+
## Links
|
|
193
299
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
300
|
+
- [PromptQL Website](https://promptql.hasura.io)
|
|
301
|
+
- [PromptQL Console](https://promptql.console.hasura.io)
|
|
302
|
+
- [Hasura Documentation](https://hasura.io/docs)
|
|
303
|
+
|
|
304
|
+
## Support
|
|
305
|
+
|
|
306
|
+
For support, please:
|
|
307
|
+
|
|
308
|
+
1. Check the [documentation](https://promptql.hasura.io)
|
|
309
|
+
2. Join the [Hasura Discord community](https://discord.gg/hasura)
|
package/biome.json
ADDED