@lastbrain/ai-ui-core 1.0.18 → 1.0.20

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.
@@ -1 +1 @@
1
- {"version":3,"file":"createClient.d.ts","sourceRoot":"","sources":["../../src/client/createClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,QAAQ,EACR,aAAa,EACb,cAAc,EACd,cAAc,EACd,eAAe,EACf,cAAc,EACd,eAAe,EACf,QAAQ,EACT,MAAM,UAAU,CAAC;AA4FlB,wBAAgB,YAAY,CAAC,MAAM,EAAE,YAAY;qBAsBnB,OAAO,CAAC,QAAQ,EAAE,CAAC;wBAqCd,aAAa,KAAG,OAAO,CAAC,cAAc,CAAC;yBAoCtC,cAAc,KAAG,OAAO,CAAC,eAAe,CAAC;iBAoCjD,cAAc,KAAG,OAAO,CAAC,eAAe,CAAC;qBAkBvC,OAAO,CAAC,QAAQ,CAAC;EAyB9C"}
1
+ {"version":3,"file":"createClient.d.ts","sourceRoot":"","sources":["../../src/client/createClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,QAAQ,EACR,aAAa,EACb,cAAc,EACd,cAAc,EACd,eAAe,EACf,cAAc,EACd,eAAe,EACf,QAAQ,EACT,MAAM,UAAU,CAAC;AA4FlB,wBAAgB,YAAY,CAAC,MAAM,EAAE,YAAY;qBAsBnB,OAAO,CAAC,QAAQ,EAAE,CAAC;wBAqCd,aAAa,KAAG,OAAO,CAAC,cAAc,CAAC;yBAoCtC,cAAc,KAAG,OAAO,CAAC,eAAe,CAAC;iBAqDjD,cAAc,KAAG,OAAO,CAAC,eAAe,CAAC;qBAkBvC,OAAO,CAAC,QAAQ,CAAC;EAyB9C"}
@@ -12,7 +12,7 @@ async function fetchWithRetry(url, options, retryConfig) {
12
12
  const response = await fetch(url, options);
13
13
  if (!response.ok) {
14
14
  const errorData = await response.json().catch(() => ({}));
15
- const error = new Error(errorData.message || `HTTP ${response.status}`);
15
+ const error = new Error(JSON.stringify(errorData) || `HTTP ${response.status}`);
16
16
  error.status = response.status;
17
17
  error.response = { status: response.status };
18
18
  throw error;
@@ -141,10 +141,21 @@ export function createClient(config) {
141
141
  async function generateImage(req) {
142
142
  try {
143
143
  const url = buildUrl(config.baseUrl, "image-ai");
144
+ // Convert camelCase to snake_case for API
145
+ const apiRequest = {
146
+ model: req.model,
147
+ prompt: req.prompt,
148
+ context: req.context,
149
+ size: req.size,
150
+ n: req.n,
151
+ prompt_id: req.promptId,
152
+ store_outputs: req.storeOutputs,
153
+ artifact_title: req.artifactTitle,
154
+ };
144
155
  const response = await fetchWithRetry(url, {
145
156
  method: "POST",
146
157
  headers: createHeaders(),
147
- body: JSON.stringify(req),
158
+ body: JSON.stringify(apiRequest),
148
159
  signal: createAbortSignal(),
149
160
  }, { retries, delay: INITIAL_RETRY_DELAY });
150
161
  // Track prompt usage if promptId is provided
@@ -162,7 +173,12 @@ export function createClient(config) {
162
173
  // Ignore tracking errors
163
174
  }
164
175
  }
165
- return response;
176
+ // Map API response to AiImageResponse format
177
+ return {
178
+ url: response.image_url || response.url,
179
+ debitTokens: response.cost?.usd ? response.cost.usd * 1000 : 0, // Convert USD to tokens (approx)
180
+ requestId: response.request_id || response.requestId,
181
+ };
166
182
  }
167
183
  catch (error) {
168
184
  throw normalizeError(error);
@@ -1 +1 @@
1
- {"version":3,"file":"normalizeError.d.ts","sourceRoot":"","sources":["../../src/errors/normalizeError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,SAAS,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,GAAG,GAAG,eAAe,CAkG1D"}
1
+ {"version":3,"file":"normalizeError.d.ts","sourceRoot":"","sources":["../../src/errors/normalizeError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,SAAS,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,GAAG,GAAG,eAAe,CA0G1D"}
@@ -31,6 +31,13 @@ export function normalizeError(error) {
31
31
  status,
32
32
  };
33
33
  }
34
+ if (status === 402) {
35
+ return {
36
+ code: ErrorCode.INSUFFICIENT_TOKENS,
37
+ message: error?.message || "Insufficient balance",
38
+ status,
39
+ };
40
+ }
34
41
  if (status === 503) {
35
42
  return {
36
43
  code: ErrorCode.PROVIDER_DOWN,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lastbrain/ai-ui-core",
3
- "version": "1.0.18",
3
+ "version": "1.0.20",
4
4
  "description": "Framework-agnostic core library for LastBrain AI UI Kit",
5
5
  "private": false,
6
6
  "type": "module",
@@ -38,7 +38,7 @@ async function fetchWithRetry<T>(
38
38
  if (!response.ok) {
39
39
  const errorData = await response.json().catch(() => ({}));
40
40
  const error: any = new Error(
41
- errorData.message || `HTTP ${response.status}`
41
+ JSON.stringify(errorData) || `HTTP ${response.status}`
42
42
  );
43
43
  error.status = response.status;
44
44
  error.response = { status: response.status };
@@ -199,12 +199,24 @@ export function createClient(config: ClientConfig) {
199
199
  try {
200
200
  const url = buildUrl(config.baseUrl, "image-ai");
201
201
 
202
- const response = await fetchWithRetry<AiImageResponse>(
202
+ // Convert camelCase to snake_case for API
203
+ const apiRequest = {
204
+ model: req.model,
205
+ prompt: req.prompt,
206
+ context: req.context,
207
+ size: req.size,
208
+ n: req.n,
209
+ prompt_id: req.promptId,
210
+ store_outputs: req.storeOutputs,
211
+ artifact_title: req.artifactTitle,
212
+ };
213
+
214
+ const response = await fetchWithRetry<any>(
203
215
  url,
204
216
  {
205
217
  method: "POST",
206
218
  headers: createHeaders(),
207
- body: JSON.stringify(req),
219
+ body: JSON.stringify(apiRequest),
208
220
  signal: createAbortSignal(),
209
221
  },
210
222
  { retries, delay: INITIAL_RETRY_DELAY }
@@ -225,7 +237,12 @@ export function createClient(config: ClientConfig) {
225
237
  }
226
238
  }
227
239
 
228
- return response;
240
+ // Map API response to AiImageResponse format
241
+ return {
242
+ url: response.image_url || response.url,
243
+ debitTokens: response.cost?.usd ? response.cost.usd * 1000 : 0, // Convert USD to tokens (approx)
244
+ requestId: response.request_id || response.requestId,
245
+ };
229
246
  } catch (error) {
230
247
  throw normalizeError(error);
231
248
  }
@@ -45,6 +45,14 @@ export function normalizeError(error: any): NormalizedError {
45
45
  };
46
46
  }
47
47
 
48
+ if (status === 402) {
49
+ return {
50
+ code: ErrorCode.INSUFFICIENT_TOKENS,
51
+ message: error?.message || "Insufficient balance",
52
+ status,
53
+ };
54
+ }
55
+
48
56
  if (status === 503) {
49
57
  return {
50
58
  code: ErrorCode.PROVIDER_DOWN,