@pooflabs/core 0.0.31 → 0.0.32

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/dist/index.mjs CHANGED
@@ -3220,7 +3220,7 @@ async function makeApiRequest(method, urlPath, data, _overrides) {
3220
3220
  requestConfig.data = data ? JSON.stringify(data) : {};
3221
3221
  }
3222
3222
  const response = await axios(requestConfig);
3223
- return { data: response.data, status: response.status };
3223
+ return { data: response.data, status: response.status, headers: response.headers };
3224
3224
  }
3225
3225
  try {
3226
3226
  return await executeRequest();
@@ -3362,6 +3362,13 @@ const pendingRequests = {};
3362
3362
  const GET_CACHE_TTL = 500; // Adjust this value as needed (in milliseconds)
3363
3363
  // Last time we cleaned up the cache
3364
3364
  let lastCacheCleanup = Date.now();
3365
+ function hashForKey$1(value) {
3366
+ let h = 5381;
3367
+ for (let i = 0; i < value.length; i++) {
3368
+ h = ((h << 5) + h + value.charCodeAt(i)) & 0x7fffffff;
3369
+ }
3370
+ return h.toString(36);
3371
+ }
3365
3372
  async function get(path, opts = {}) {
3366
3373
  try {
3367
3374
  let normalizedPath = path.startsWith("/") ? path.slice(1) : path;
@@ -3372,10 +3379,12 @@ async function get(path, opts = {}) {
3372
3379
  if (!normalizedPath || normalizedPath.length === 0) {
3373
3380
  return new Error("Invalid path provided.");
3374
3381
  }
3375
- // Create cache key combining path, prompt, includeSubPaths, and shape
3382
+ // Create cache key combining path, prompt, includeSubPaths, shape, limit, and cursor
3376
3383
  const shapeKey = opts.shape ? JSON.stringify(opts.shape) : '';
3377
3384
  const includeSubPathsKey = opts.includeSubPaths ? ':subpaths' : '';
3378
- const cacheKey = `${normalizedPath}:${opts.prompt || ''}${includeSubPathsKey}:${shapeKey}`;
3385
+ const limitKey = opts.limit !== undefined ? `:l${opts.limit}` : '';
3386
+ const cursorKey = opts.cursor ? `:c${hashForKey$1(opts.cursor)}` : '';
3387
+ const cacheKey = `${normalizedPath}:${opts.prompt || ''}${includeSubPathsKey}:${shapeKey}${limitKey}${cursorKey}`;
3379
3388
  const now = Date.now();
3380
3389
  // Check for valid cache entry if not bypassing cache
3381
3390
  if (!opts.bypassCache && getCache[cacheKey] && now < getCache[cacheKey].expiresAt) {
@@ -3399,6 +3408,8 @@ async function get(path, opts = {}) {
3399
3408
  // Build common query params
3400
3409
  const includeSubPathsParam = opts.includeSubPaths ? '&includeSubPaths=true' : '';
3401
3410
  const shapeParam = opts.shape ? `&shape=${encodeURIComponent(JSON.stringify(opts.shape))}` : '';
3411
+ const limitParam = opts.limit !== undefined ? `&limit=${opts.limit}` : '';
3412
+ const cursorParam = opts.cursor ? `&cursor=${encodeURIComponent(opts.cursor)}` : '';
3402
3413
  if (pathIsDocument) {
3403
3414
  const itemId = encodeURIComponent(normalizedPath);
3404
3415
  // For documents, query params go after the path
@@ -3409,13 +3420,14 @@ async function get(path, opts = {}) {
3409
3420
  else {
3410
3421
  const path = encodeURIComponent(normalizedPath);
3411
3422
  const promptQueryParam = (opts === null || opts === void 0 ? void 0 : opts.prompt) ? `&prompt=${btoa(opts.prompt)}` : "";
3412
- const apiPath = `items?path=${path}${promptQueryParam}${includeSubPathsParam}${shapeParam}`;
3423
+ const apiPath = `items?path=${path}${promptQueryParam}${includeSubPathsParam}${shapeParam}${limitParam}${cursorParam}`;
3413
3424
  response = await makeApiRequest('GET', apiPath, null, opts._overrides);
3414
3425
  }
3426
+ const responseData = response.data;
3415
3427
  // Cache the response (unless bypassing cache)
3416
3428
  if (!opts.bypassCache) {
3417
3429
  getCache[cacheKey] = {
3418
- data: response.data,
3430
+ data: responseData,
3419
3431
  expiresAt: now + GET_CACHE_TTL
3420
3432
  };
3421
3433
  // Periodically clean up expired cache entries (every 5 seconds)
@@ -3425,7 +3437,7 @@ async function get(path, opts = {}) {
3425
3437
  }
3426
3438
  }
3427
3439
  // Return the data from the response
3428
- return response.data;
3440
+ return responseData;
3429
3441
  }
3430
3442
  finally {
3431
3443
  // Remove this request from pendingRequests regardless of success/failure
@@ -3722,21 +3734,21 @@ function clearCacheByPrefix(prefix) {
3722
3734
  }
3723
3735
  });
3724
3736
  }
3725
- async function getFiles(path) {
3737
+ async function getFiles(path, options) {
3726
3738
  try {
3727
3739
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
3728
3740
  if (!normalizedPath || normalizedPath.length === 0) {
3729
3741
  return new Error("Invalid path provided.");
3730
3742
  }
3731
3743
  const apiPath = `storage?path=${normalizedPath}`;
3732
- const response = await makeApiRequest('GET', apiPath, null, undefined);
3744
+ const response = await makeApiRequest('GET', apiPath, null, options === null || options === void 0 ? void 0 : options._overrides);
3733
3745
  return response.data;
3734
3746
  }
3735
3747
  catch (error) {
3736
3748
  throw error;
3737
3749
  }
3738
3750
  }
3739
- async function setFile(path, file) {
3751
+ async function setFile(path, file, options) {
3740
3752
  var _a;
3741
3753
  // 1) Get the presigned URL from your backend
3742
3754
  const requestBody = {
@@ -3747,7 +3759,7 @@ async function setFile(path, file) {
3747
3759
  if (file) {
3748
3760
  requestBody.contentLength = file.size;
3749
3761
  }
3750
- const response = await makeApiRequest('POST', 'storage/url', requestBody, undefined);
3762
+ const response = await makeApiRequest('POST', 'storage/url', requestBody, options === null || options === void 0 ? void 0 : options._overrides);
3751
3763
  if (file == null) {
3752
3764
  return true;
3753
3765
  }
@@ -3843,10 +3855,19 @@ let lastBrowserTriggeredReconnectAt = 0;
3843
3855
  function generateSubscriptionId() {
3844
3856
  return `sub_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`;
3845
3857
  }
3846
- function getCacheKey(path, prompt, shape) {
3858
+ function hashForKey(value) {
3859
+ let h = 5381;
3860
+ for (let i = 0; i < value.length; i++) {
3861
+ h = ((h << 5) + h + value.charCodeAt(i)) & 0x7fffffff;
3862
+ }
3863
+ return h.toString(36);
3864
+ }
3865
+ function getCacheKey(path, prompt, shape, limit, cursor) {
3847
3866
  const normalizedPath = path.startsWith('/') ? path.slice(1) : path;
3848
3867
  const shapeKey = shape && Object.keys(shape).length > 0 ? JSON.stringify(shape) : '';
3849
- return `${normalizedPath}:${prompt || 'default'}:${shapeKey}`;
3868
+ const limitKey = limit !== undefined ? `:l${limit}` : '';
3869
+ const cursorKey = cursor ? `:c${hashForKey(cursor)}` : '';
3870
+ return `${normalizedPath}:${prompt || 'default'}:${shapeKey}${limitKey}${cursorKey}`;
3850
3871
  }
3851
3872
  function isTokenExpired(token) {
3852
3873
  try {
@@ -3987,8 +4008,12 @@ async function getOrCreateConnection(appId, isServer) {
3987
4008
  const wsUrl = new URL(config.wsApiUrl);
3988
4009
  // Always use v2 path
3989
4010
  wsUrl.pathname = WS_V2_PATH;
3990
- // Set appId
3991
- if (typeof window !== 'undefined' && window.CUSTOM_TAROBASE_APP_ID_HEADER) {
4011
+ // Set appId — prefer the explicit appId passed to getOrCreateConnection,
4012
+ // fall back to window global for legacy callers, then config default
4013
+ if (appId && appId !== config.appId) {
4014
+ wsUrl.searchParams.append('appId', appId);
4015
+ }
4016
+ else if (typeof window !== 'undefined' && window.CUSTOM_TAROBASE_APP_ID_HEADER) {
3992
4017
  wsUrl.searchParams.append('appId', window.CUSTOM_TAROBASE_APP_ID_HEADER);
3993
4018
  }
3994
4019
  else {
@@ -4061,7 +4086,7 @@ function handleServerMessage(connection, message) {
4061
4086
  // If we already received data for this subscription, treat subscribed
4062
4087
  // as an ack only and avoid regressing to an older snapshot.
4063
4088
  if (subscription.lastData === undefined) {
4064
- const cacheKey = getCacheKey(subscription.path, subscription.prompt, subscription.shape);
4089
+ const cacheKey = getCacheKey(subscription.path, subscription.prompt, subscription.shape, subscription.limit, subscription.cursor);
4065
4090
  responseCache.set(cacheKey, { data: message.data, timestamp: Date.now() });
4066
4091
  subscription.lastData = message.data;
4067
4092
  notifyCallbacks(subscription, message.data);
@@ -4088,7 +4113,7 @@ function handleServerMessage(connection, message) {
4088
4113
  const subscription = connection.subscriptions.get(message.subscriptionId);
4089
4114
  if (subscription) {
4090
4115
  // Update cache
4091
- const cacheKey = getCacheKey(subscription.path, subscription.prompt, subscription.shape);
4116
+ const cacheKey = getCacheKey(subscription.path, subscription.prompt, subscription.shape, subscription.limit, subscription.cursor);
4092
4117
  responseCache.set(cacheKey, { data: message.data, timestamp: Date.now() });
4093
4118
  // Store last data
4094
4119
  subscription.lastData = message.data;
@@ -4145,6 +4170,8 @@ function sendSubscribe(connection, subscription) {
4145
4170
  shape: subscription.shape && Object.keys(subscription.shape).length > 0
4146
4171
  ? subscription.shape
4147
4172
  : undefined,
4173
+ limit: subscription.limit,
4174
+ cursor: subscription.cursor,
4148
4175
  };
4149
4176
  try {
4150
4177
  connection.ws.send(JSON.stringify(message));
@@ -4176,7 +4203,7 @@ function sendUnsubscribe(connection, subscriptionId) {
4176
4203
  async function subscribeV2(path, subscriptionOptions) {
4177
4204
  const config = await getConfig();
4178
4205
  const normalizedPath = path.startsWith('/') ? path.slice(1) : path;
4179
- const cacheKey = getCacheKey(normalizedPath, subscriptionOptions.prompt, subscriptionOptions.shape);
4206
+ const cacheKey = getCacheKey(normalizedPath, subscriptionOptions.prompt, subscriptionOptions.shape, subscriptionOptions.limit, subscriptionOptions.cursor);
4180
4207
  // Deliver cached data immediately if available
4181
4208
  const cachedEntry = responseCache.get(cacheKey);
4182
4209
  if (cachedEntry && Date.now() - cachedEntry.timestamp < CACHE_TTL && subscriptionOptions.onData) {
@@ -4185,14 +4212,16 @@ async function subscribeV2(path, subscriptionOptions) {
4185
4212
  (_a = subscriptionOptions.onData) === null || _a === void 0 ? void 0 : _a.call(subscriptionOptions, cachedEntry.data);
4186
4213
  }, 0);
4187
4214
  }
4215
+ // Use explicit appId override if provided, otherwise fall back to config
4216
+ const effectiveAppId = subscriptionOptions.appId || config.appId;
4188
4217
  // Get or create connection for this appId
4189
- const connection = await getOrCreateConnection(config.appId, config.isServer);
4190
- // Check if we already have a subscription for this path+prompt+shape
4218
+ const connection = await getOrCreateConnection(effectiveAppId, config.isServer);
4219
+ // Check if we already have a subscription for this path+prompt+shape+limit+cursor
4191
4220
  const shapeKey = subscriptionOptions.shape ? JSON.stringify(subscriptionOptions.shape) : '';
4192
4221
  let existingSubscription;
4193
4222
  for (const sub of connection.subscriptions.values()) {
4194
4223
  const subShapeKey = sub.shape ? JSON.stringify(sub.shape) : '';
4195
- if (sub.path === normalizedPath && sub.prompt === subscriptionOptions.prompt && subShapeKey === shapeKey) {
4224
+ if (sub.path === normalizedPath && sub.prompt === subscriptionOptions.prompt && subShapeKey === shapeKey && sub.limit === subscriptionOptions.limit && sub.cursor === subscriptionOptions.cursor) {
4196
4225
  existingSubscription = sub;
4197
4226
  break;
4198
4227
  }
@@ -4218,6 +4247,8 @@ async function subscribeV2(path, subscriptionOptions) {
4218
4247
  path: normalizedPath,
4219
4248
  prompt: subscriptionOptions.prompt,
4220
4249
  shape: subscriptionOptions.shape,
4250
+ limit: subscriptionOptions.limit,
4251
+ cursor: subscriptionOptions.cursor,
4221
4252
  includeSubPaths: false,
4222
4253
  callbacks: [subscriptionOptions],
4223
4254
  lastData: undefined,