nuabase 1.1.0 → 2.0.0
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 +105 -56
- package/dist/cjs/index.cjs +302 -361
- package/dist/esm/index.mjs +299 -361
- package/dist/types/__tests__/helpers/client-factory.d.ts +17 -0
- package/dist/types/__tests__/helpers/index.d.ts +1 -0
- package/dist/types/backend/direct.d.ts +17 -0
- package/dist/types/backend/gateway.d.ts +23 -0
- package/dist/types/backend/types.d.ts +61 -0
- package/dist/types/index.d.cts +5 -2
- package/dist/types/index.d.mts +5 -2
- package/dist/types/lib/auth.d.ts +12 -0
- package/dist/types/nua.d.ts +34 -3
- package/package.json +19 -17
- package/dist/types/__tests__/utils/sse.d.ts +0 -12
- package/dist/types/lib/api-client.d.ts +0 -11
- package/dist/types/nua/cast_array/factory.d.ts +0 -4
- package/dist/types/nua/cast_array/request-validation.d.ts +0 -1
- package/dist/types/nua/cast_array/response-schema.d.ts +0 -17
- package/dist/types/nua/cast_array/types.d.ts +0 -33
- package/dist/types/nua/cast_value/factory.d.ts +0 -4
- package/dist/types/nua/cast_value/response-schema.d.ts +0 -13
- package/dist/types/nua/cast_value/types.d.ts +0 -18
- package/dist/types/nua/common/queued-response.d.ts +0 -6
- package/dist/types/nua/core.d.ts +0 -12
- package/dist/types/nua/get-request/index.d.ts +0 -6
- package/dist/types/nua/get-request/types.d.ts +0 -34
- package/dist/types/nua/index.d.ts +0 -5
package/README.md
CHANGED
|
@@ -31,24 +31,23 @@ const LeadInsights = z.object({
|
|
|
31
31
|
});
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
**3.
|
|
34
|
+
**3. Call the API.** Use `list()` for array operations:
|
|
35
35
|
|
|
36
36
|
```ts
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
},
|
|
37
|
+
const nua = Nua.gateway({ apiKey: 'YOUR_API_KEY' });
|
|
38
|
+
|
|
39
|
+
const result = await nua.list('Classify each lead with industry and company_size bucket.', {
|
|
40
|
+
input: leads,
|
|
41
|
+
primaryKey: 'id',
|
|
42
|
+
output: { name: 'leadInsights', schema: LeadInsights },
|
|
43
43
|
});
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
-
**4.
|
|
46
|
+
**4. Use the result.** Check for success and access typed data:
|
|
47
47
|
|
|
48
48
|
```ts
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
console.log(result.data[0].insights);
|
|
49
|
+
if (!result.success) throw new Error(result.error);
|
|
50
|
+
console.log(result.data[0].leadInsights);
|
|
52
51
|
// -> { industry: 'Software', company_size: 'Mid-market' }
|
|
53
52
|
```
|
|
54
53
|
|
|
@@ -62,7 +61,7 @@ npm install nuabase zod
|
|
|
62
61
|
|
|
63
62
|
2. Get your API key from the [Nuabase Console](https://console.nuabase.com/dashboard/api-keys/new).
|
|
64
63
|
|
|
65
|
-
3. Set `NUABASE_API_KEY` environment variable, or pass it directly in `
|
|
64
|
+
3. Set `NUABASE_API_KEY` environment variable, or pass it directly in `Nua.gateway({ apiKey: "KEY" })`:
|
|
66
65
|
|
|
67
66
|
## Lead Enrichment Example
|
|
68
67
|
|
|
@@ -70,7 +69,7 @@ npm install nuabase zod
|
|
|
70
69
|
import { Nua } from 'nuabase';
|
|
71
70
|
import { z } from 'zod';
|
|
72
71
|
|
|
73
|
-
const nua =
|
|
72
|
+
const nua = Nua.gateway({ apiKey: 'API-KEY' });
|
|
74
73
|
|
|
75
74
|
const LeadInsights = z.object({
|
|
76
75
|
company_name: z.string(),
|
|
@@ -79,15 +78,6 @@ const LeadInsights = z.object({
|
|
|
79
78
|
recommended_follow_up: z.enum(['Call', 'Email', 'Event']),
|
|
80
79
|
});
|
|
81
80
|
|
|
82
|
-
const enrichLeads = nua.createArrayFn({
|
|
83
|
-
prompt:
|
|
84
|
-
'Summarize each inbound lead by extracting company_name, industry, company_size bucket (SMB, Mid-market, Enterprise), and the recommended_follow_up channel (Call, Email, or Event) based on their notes.',
|
|
85
|
-
output: {
|
|
86
|
-
name: 'leadInsights',
|
|
87
|
-
schema: LeadInsights,
|
|
88
|
-
},
|
|
89
|
-
});
|
|
90
|
-
|
|
91
81
|
const rows = [
|
|
92
82
|
{
|
|
93
83
|
leadId: 'lead-101',
|
|
@@ -107,22 +97,29 @@ const rows = [
|
|
|
107
97
|
},
|
|
108
98
|
];
|
|
109
99
|
|
|
110
|
-
const response = await
|
|
100
|
+
const response = await nua.list(
|
|
101
|
+
'Summarize each inbound lead by extracting company_name, industry, company_size bucket (SMB, Mid-market, Enterprise), and the recommended_follow_up channel (Call, Email, or Event) based on their notes.',
|
|
102
|
+
{
|
|
103
|
+
input: rows,
|
|
104
|
+
primaryKey: 'leadId',
|
|
105
|
+
output: { name: 'leadInsights', schema: LeadInsights },
|
|
106
|
+
}
|
|
107
|
+
);
|
|
111
108
|
|
|
112
|
-
if (response.
|
|
109
|
+
if (!response.success) {
|
|
113
110
|
console.error(response.error);
|
|
114
111
|
return;
|
|
115
112
|
}
|
|
116
113
|
|
|
117
|
-
response.data.forEach(({
|
|
114
|
+
response.data.forEach(({ leadId, leadInsights }) => {
|
|
118
115
|
// leadInsights satisfies the LeadInsights schema
|
|
119
|
-
console.log(
|
|
116
|
+
console.log(leadId, leadInsights.company_name, leadInsights.industry, leadInsights.company_size);
|
|
120
117
|
});
|
|
121
118
|
|
|
122
119
|
console.log(response.data);
|
|
123
120
|
// -> [
|
|
124
121
|
// {
|
|
125
|
-
//
|
|
122
|
+
// leadId: 'lead-101',
|
|
126
123
|
// leadInsights: {
|
|
127
124
|
// company_name: 'Acme Analytics',
|
|
128
125
|
// industry: 'Software',
|
|
@@ -134,7 +131,7 @@ console.log(response.data);
|
|
|
134
131
|
// ];
|
|
135
132
|
```
|
|
136
133
|
|
|
137
|
-
|
|
134
|
+
The default invocation waits for completion and returns the full result. Use `nua.queueList()` to submit an asynchronous job and receive streaming updates via SSE.
|
|
138
135
|
|
|
139
136
|
## Why Nuabase
|
|
140
137
|
|
|
@@ -145,55 +142,103 @@ console.log(response.data);
|
|
|
145
142
|
|
|
146
143
|
## API Response
|
|
147
144
|
|
|
148
|
-
Every call returns a discriminated union. When `
|
|
145
|
+
Every call returns a discriminated union. When `success` is `false`, you get an error object. Otherwise `success` is `true`, and `data` contains your typed output along with metadata.
|
|
149
146
|
|
|
150
147
|
**Success**
|
|
151
148
|
|
|
152
|
-
| Field
|
|
153
|
-
|
|
|
154
|
-
| `
|
|
155
|
-
| `data`
|
|
156
|
-
| `
|
|
157
|
-
| `
|
|
158
|
-
| `
|
|
159
|
-
| `
|
|
149
|
+
| Field | Type | Description |
|
|
150
|
+
| ----------- | --------------------------------------------------------- | ------------------------------------------------------- |
|
|
151
|
+
| `success` | `true` | Indicates that the request completed successfully. |
|
|
152
|
+
| `data` | `T` (for `get`) or `Array<{ [pk]: value; [output]: T }>` (for `list`) | The typed result matching your schema. |
|
|
153
|
+
| `usage` | `{ promptTokens, completionTokens, totalTokens }` | Token usage statistics for the LLM request. |
|
|
154
|
+
| `model` | `string` | The LLM model used for this request. |
|
|
155
|
+
| `latencyMs` | `number` | Request latency in milliseconds. |
|
|
156
|
+
| `source` | `'gateway'` \| `'direct'` | Which backend processed the request. |
|
|
157
|
+
| `meta` | `object` | Additional metadata (varies by source). |
|
|
158
|
+
|
|
159
|
+
**Gateway-specific metadata** (`meta` when `source` is `'gateway'`):
|
|
160
|
+
|
|
161
|
+
| Field | Type | Description |
|
|
162
|
+
| ------------ | --------- | ---------------------------------------------- |
|
|
163
|
+
| `requestId` | `string` | Unique identifier for tracing. |
|
|
164
|
+
| `cached` | `boolean` | Whether the result was served from cache. |
|
|
165
|
+
| `llmUsage` | `object` | Token usage from LLM calls. |
|
|
166
|
+
| `cacheUsage` | `object` | Token usage served from cache. |
|
|
160
167
|
|
|
161
168
|
**Error**
|
|
162
169
|
|
|
163
|
-
| Field
|
|
164
|
-
|
|
|
165
|
-
| `
|
|
166
|
-
| `error`
|
|
170
|
+
| Field | Type | Description |
|
|
171
|
+
| ----------- | ----------------------------- | -------------------------------------------------------- |
|
|
172
|
+
| `success` | `false` | Indicates the request failed. |
|
|
173
|
+
| `error` | `string` | Message describing the error. |
|
|
174
|
+
| `source` | `'gateway'` \| `'direct'` | Which backend returned the error. |
|
|
175
|
+
| `latencyMs` | `number` | Time until failure in milliseconds. |
|
|
167
176
|
|
|
168
177
|
## SDK API
|
|
169
178
|
|
|
170
|
-
|
|
171
|
-
|
|
179
|
+
### Constructors
|
|
180
|
+
|
|
181
|
+
**`Nua.gateway(config?)`**
|
|
182
|
+
Creates a client that connects to the Nuabase API gateway. Use this for production deployments with caching, request tracking, and async job support.
|
|
172
183
|
|
|
173
184
|
- `apiKey?: string` – API key for authentication. Defaults to `process.env.NUABASE_API_KEY`.
|
|
174
185
|
- `baseUrl?: string` – Override the API host. Defaults to `https://api.nuabase.com`.
|
|
175
186
|
|
|
176
|
-
**`
|
|
177
|
-
|
|
187
|
+
**`Nua.direct(config)`**
|
|
188
|
+
Creates a client that calls LLM providers directly (in-process). Use this for serverless, edge functions, or CLI tools where you don't need the gateway infrastructure.
|
|
189
|
+
|
|
190
|
+
- `model: string` – The model to use (e.g., `'gpt-4o-mini'`).
|
|
191
|
+
- `providers: { openai?: { apiKey: string }, ... }` – Provider credentials.
|
|
192
|
+
|
|
193
|
+
### Methods
|
|
194
|
+
|
|
195
|
+
**`nua.get(prompt)`**
|
|
196
|
+
Returns a simple string response from the LLM.
|
|
197
|
+
|
|
198
|
+
```ts
|
|
199
|
+
const result = await nua.get("Tell me a joke");
|
|
200
|
+
if (result.success) console.log(result.data); // string
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
**`nua.get(prompt, options)`**
|
|
204
|
+
Returns a typed single value. Parameters:
|
|
178
205
|
|
|
179
|
-
- `name: string` – Label applied to the enriched payload in each response row (e.g., `result.data[i][name]`).
|
|
180
206
|
- `prompt: string` – Natural-language instructions sent to the LLM.
|
|
181
|
-
- `
|
|
207
|
+
- `options.input?: unknown` – Optional input data to include in the prompt.
|
|
208
|
+
- `options.output: { name: string; schema: ZodSchema }` – Output name and Zod schema.
|
|
182
209
|
|
|
183
|
-
|
|
210
|
+
```ts
|
|
211
|
+
const result = await nua.get("Extract the address", {
|
|
212
|
+
input: "123 Main St, Springfield, IL 62701",
|
|
213
|
+
output: { name: 'address', schema: AddressSchema },
|
|
214
|
+
});
|
|
215
|
+
if (result.success) console.log(result.data); // typed as z.infer<typeof AddressSchema>
|
|
216
|
+
```
|
|
184
217
|
|
|
185
|
-
|
|
186
|
-
|
|
218
|
+
**`nua.list(prompt, options)`**
|
|
219
|
+
Processes an array of items. Parameters:
|
|
187
220
|
|
|
188
|
-
|
|
221
|
+
- `prompt: string` – Natural-language instructions for each item.
|
|
222
|
+
- `options.input: Array<Record<string, unknown>>` – Rows to process.
|
|
223
|
+
- `options.primaryKey: string` – Field that uniquely identifies each row.
|
|
224
|
+
- `options.output: { name: string; schema: ZodSchema }` – Output configuration.
|
|
189
225
|
|
|
190
|
-
|
|
191
|
-
|
|
226
|
+
```ts
|
|
227
|
+
const result = await nua.list("Classify each lead", {
|
|
228
|
+
input: leads,
|
|
229
|
+
primaryKey: 'id',
|
|
230
|
+
output: { name: 'classification', schema: ClassificationSchema },
|
|
231
|
+
});
|
|
232
|
+
if (result.success) {
|
|
233
|
+
result.data.forEach(row => console.log(row.id, row.classification));
|
|
234
|
+
}
|
|
235
|
+
```
|
|
192
236
|
|
|
193
|
-
|
|
237
|
+
**`nua.queueGet(prompt, options?)`** *(Gateway mode only)*
|
|
238
|
+
Submits an async job for single-value operations. Returns `{ success: true, jobId }`.
|
|
194
239
|
|
|
195
|
-
|
|
196
|
-
|
|
240
|
+
**`nua.queueList(prompt, options)`** *(Gateway mode only)*
|
|
241
|
+
Submits an async job for array operations. Returns `{ success: true, jobId }`.
|
|
197
242
|
|
|
198
243
|
## Next Steps
|
|
199
244
|
|
|
@@ -204,6 +249,10 @@ Both entry points resolve to either:
|
|
|
204
249
|
|
|
205
250
|
Email us at [hello@nuabase.com](mailto:hello@nuabase.com). On X at [@NuabaseHQ](x.com/NuabaseHQ).
|
|
206
251
|
|
|
252
|
+
## Tests
|
|
253
|
+
|
|
254
|
+
See [TESTING.md](TESTING.md)
|
|
255
|
+
|
|
207
256
|
## Credits
|
|
208
257
|
|
|
209
258
|
The template for this package comes from https://github.com/rtivital/ts-package-template
|