@granular-software/sdk 0.4.43 → 0.4.45

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.
@@ -11445,6 +11445,164 @@ async function invokeRegisteredEffect(effectMap, request) {
11445
11445
  return resolved.handler(request.input, context);
11446
11446
  }
11447
11447
 
11448
+ // src/client-normalizers.ts
11449
+ function createEmptyHeapSnapshot(now = Date.now()) {
11450
+ return {
11451
+ entriesByPath: {},
11452
+ listsByName: {},
11453
+ variablesByName: {},
11454
+ updatedAt: now
11455
+ };
11456
+ }
11457
+ function normalizeHeapSnapshot(raw) {
11458
+ if (!raw || typeof raw !== "object") {
11459
+ return createEmptyHeapSnapshot();
11460
+ }
11461
+ const heap = raw;
11462
+ return {
11463
+ entriesByPath: heap.entriesByPath && typeof heap.entriesByPath === "object" ? JSON.parse(JSON.stringify(heap.entriesByPath)) : {},
11464
+ listsByName: heap.listsByName && typeof heap.listsByName === "object" ? JSON.parse(JSON.stringify(heap.listsByName)) : {},
11465
+ variablesByName: heap.variablesByName && typeof heap.variablesByName === "object" ? JSON.parse(JSON.stringify(heap.variablesByName)) : {},
11466
+ updatedAt: typeof heap.updatedAt === "number" ? heap.updatedAt : Date.now()
11467
+ };
11468
+ }
11469
+ function normalizeGraphPathSegment(value) {
11470
+ return value.trim().toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_+|_+$/g, "");
11471
+ }
11472
+ function extractRecordIdFromGraphPath(path2, className) {
11473
+ const normalizedPrefix = `${normalizeGraphPathSegment(className)}_`;
11474
+ if (path2.startsWith(normalizedPrefix)) {
11475
+ return path2.slice(normalizedPrefix.length);
11476
+ }
11477
+ const legacyPrefix = `${className}_`;
11478
+ if (path2.startsWith(legacyPrefix)) {
11479
+ return path2.slice(legacyPrefix.length);
11480
+ }
11481
+ return path2;
11482
+ }
11483
+ function toRecordSearchResult(className, node) {
11484
+ const path2 = typeof node.path === "string" ? node.path : "";
11485
+ if (!path2) return null;
11486
+ const fields = Array.isArray(node.submodels) ? node.submodels.flatMap(
11487
+ (submodel) => {
11488
+ const name = typeof submodel?.label === "string" && submodel.label.trim() ? submodel.label : typeof submodel?.path === "string" ? submodel.path.split(":").pop() || submodel.path : "";
11489
+ if (!name) return [];
11490
+ if (typeof submodel.string_value === "string") {
11491
+ return [{ name, type: "string", value: submodel.string_value }];
11492
+ }
11493
+ if (typeof submodel.number_value === "number") {
11494
+ return [{ name, type: "number", value: submodel.number_value }];
11495
+ }
11496
+ if (typeof submodel.boolean_value === "boolean") {
11497
+ return [
11498
+ {
11499
+ name,
11500
+ type: "boolean",
11501
+ value: submodel.boolean_value
11502
+ }
11503
+ ];
11504
+ }
11505
+ return [];
11506
+ }
11507
+ ) : [];
11508
+ return {
11509
+ path: path2,
11510
+ className,
11511
+ id: extractRecordIdFromGraphPath(path2, className),
11512
+ label: typeof node.label === "string" && node.label.trim() ? node.label : extractRecordIdFromGraphPath(path2, className),
11513
+ description: typeof node.description === "string" && node.description.trim() ? node.description : null,
11514
+ fields
11515
+ };
11516
+ }
11517
+ function normalizeRecordSearchText(value) {
11518
+ return value.trim().toLowerCase().replace(/[^a-z0-9]+/g, " ").replace(/\s+/g, " ").trim();
11519
+ }
11520
+ function rankRecordSearchResult(result, query, index) {
11521
+ const normalizedQuery = normalizeRecordSearchText(query);
11522
+ if (!normalizedQuery) {
11523
+ return index;
11524
+ }
11525
+ const label = normalizeRecordSearchText(result.label || "");
11526
+ const id = normalizeRecordSearchText(result.id || "");
11527
+ const path2 = normalizeRecordSearchText(result.path || "");
11528
+ const className = normalizeRecordSearchText(result.className || "");
11529
+ const searchable = [label, id, path2, className].filter(Boolean);
11530
+ if (label === normalizedQuery) return index;
11531
+ if (id === normalizedQuery || path2 === normalizedQuery) return 100 + index;
11532
+ if (label.startsWith(normalizedQuery)) return 200 + index;
11533
+ if (searchable.some((value) => value.startsWith(normalizedQuery))) {
11534
+ return 300 + index;
11535
+ }
11536
+ if (label.includes(normalizedQuery)) return 400 + index;
11537
+ if (searchable.some((value) => value.includes(normalizedQuery))) {
11538
+ return 500 + index;
11539
+ }
11540
+ return 900 + index;
11541
+ }
11542
+ function deriveRuntimeBaseUrl(apiEndpoint) {
11543
+ try {
11544
+ const endpoint = new URL(apiEndpoint);
11545
+ const graphqlSuffix = "/orchestrator/graphql";
11546
+ if (endpoint.pathname.endsWith(graphqlSuffix)) {
11547
+ endpoint.pathname = endpoint.pathname.slice(0, -graphqlSuffix.length);
11548
+ } else if (endpoint.pathname.endsWith("/graphql")) {
11549
+ endpoint.pathname = endpoint.pathname.slice(0, -"/graphql".length);
11550
+ }
11551
+ endpoint.search = "";
11552
+ endpoint.hash = "";
11553
+ return endpoint.toString().replace(/\/$/, "");
11554
+ } catch {
11555
+ return apiEndpoint.replace(/\/orchestrator\/graphql$/, "").replace(/\/$/, "");
11556
+ }
11557
+ }
11558
+
11559
+ // src/client-transport.ts
11560
+ function sleep(ms) {
11561
+ return new Promise((resolve) => setTimeout(resolve, ms));
11562
+ }
11563
+ function withTimeout(promise, timeoutMs, label) {
11564
+ let timer = null;
11565
+ const timeout = new Promise((_, reject) => {
11566
+ timer = setTimeout(() => {
11567
+ reject(new Error(`${label} timed out after ${timeoutMs}ms`));
11568
+ }, timeoutMs);
11569
+ });
11570
+ return Promise.race([promise, timeout]).finally(() => {
11571
+ if (timer) {
11572
+ clearTimeout(timer);
11573
+ }
11574
+ });
11575
+ }
11576
+ function isLocalControlUrl(url) {
11577
+ try {
11578
+ const parsed = new URL(url);
11579
+ return parsed.hostname === "127.0.0.1" || parsed.hostname === "localhost" || parsed.hostname === "::1";
11580
+ } catch {
11581
+ return false;
11582
+ }
11583
+ }
11584
+ function isRetryableLocalWorkerRestart(status, body, url) {
11585
+ return isLocalControlUrl(url) && (status === 503 && body.includes("Your worker restarted mid-request") || status === 500 && body.includes("Network connection lost"));
11586
+ }
11587
+ function isRetryableRecordObjectsError(error) {
11588
+ const message = error instanceof Error ? error.message : String(error);
11589
+ return /worker restarted mid-request|econnreset|network|socket connection was closed unexpectedly|timed out|bad gateway|too many requests|gateway timeout|control plane api error \((?:429|500|502|503|504)\)|graphql api error \((?:429|500|502|503|504)\)|failed to record batch/i.test(
11590
+ message
11591
+ );
11592
+ }
11593
+ function isRetryableEffectRegistrationError(error) {
11594
+ const message = error instanceof Error ? error.message : String(error);
11595
+ return /timed out|websocket disconnected|websocket not connected|rpc timeout|worker restarted mid-request|network connection lost|bad gateway|gateway timeout|too many requests|(?:control plane|granular|graphql) api error \((?:429|500|502|503|504)\)/i.test(
11596
+ message
11597
+ );
11598
+ }
11599
+ function isRetryableSessionDataError(error) {
11600
+ const message = error instanceof Error ? error.message : String(error);
11601
+ return /network connection lost|worker restarted mid-request|econnreset|socket connection was closed unexpectedly|bad gateway|gateway timeout|service unavailable|session data api error \((?:429|500|502|503|504)\)/i.test(
11602
+ message
11603
+ );
11604
+ }
11605
+
11448
11606
  // src/spend.ts
11449
11607
  function toGranularHttpBase(apiUrl) {
11450
11608
  const url = new URL(apiUrl);
@@ -12930,51 +13088,6 @@ function planRecordObjectsChunks(records, batchSize) {
12930
13088
  }
12931
13089
  return plans;
12932
13090
  }
12933
- function sleep(ms) {
12934
- return new Promise((resolve) => setTimeout(resolve, ms));
12935
- }
12936
- function withTimeout(promise, timeoutMs, label) {
12937
- let timer = null;
12938
- const timeout = new Promise((_, reject) => {
12939
- timer = setTimeout(() => {
12940
- reject(new Error(`${label} timed out after ${timeoutMs}ms`));
12941
- }, timeoutMs);
12942
- });
12943
- return Promise.race([promise, timeout]).finally(() => {
12944
- if (timer) {
12945
- clearTimeout(timer);
12946
- }
12947
- });
12948
- }
12949
- function isLocalControlUrl(url) {
12950
- try {
12951
- const parsed = new URL(url);
12952
- return parsed.hostname === "127.0.0.1" || parsed.hostname === "localhost" || parsed.hostname === "::1";
12953
- } catch {
12954
- return false;
12955
- }
12956
- }
12957
- function isRetryableLocalWorkerRestart(status, body, url) {
12958
- return isLocalControlUrl(url) && (status === 503 && body.includes("Your worker restarted mid-request") || status === 500 && body.includes("Network connection lost"));
12959
- }
12960
- function isRetryableRecordObjectsError(error) {
12961
- const message = error instanceof Error ? error.message : String(error);
12962
- return /worker restarted mid-request|econnreset|network|socket connection was closed unexpectedly|timed out|bad gateway|too many requests|gateway timeout|control plane api error \((?:429|500|502|503|504)\)|graphql api error \((?:429|500|502|503|504)\)|failed to record batch/i.test(
12963
- message
12964
- );
12965
- }
12966
- function isRetryableEffectRegistrationError(error) {
12967
- const message = error instanceof Error ? error.message : String(error);
12968
- return /timed out|websocket disconnected|websocket not connected|rpc timeout|worker restarted mid-request|network connection lost|bad gateway|gateway timeout|too many requests|(?:control plane|granular|graphql) api error \((?:429|500|502|503|504)\)/i.test(
12969
- message
12970
- );
12971
- }
12972
- function isRetryableSessionDataError(error) {
12973
- const message = error instanceof Error ? error.message : String(error);
12974
- return /network connection lost|worker restarted mid-request|econnreset|socket connection was closed unexpectedly|bad gateway|gateway timeout|service unavailable|session data api error \((?:429|500|502|503|504)\)/i.test(
12975
- message
12976
- );
12977
- }
12978
13091
  function computeEffectKey2(effect) {
12979
13092
  const attachedClass = effect.className?.trim();
12980
13093
  if (!attachedClass) {
@@ -13029,115 +13142,6 @@ function buildEffectHostUrl(apiUrl, sandboxId, effectClientId, clientId, effectH
13029
13142
  url.searchParams.set("clientId", clientId);
13030
13143
  return url.toString();
13031
13144
  }
13032
- function createEmptyHeapSnapshot(now = Date.now()) {
13033
- return {
13034
- entriesByPath: {},
13035
- listsByName: {},
13036
- variablesByName: {},
13037
- updatedAt: now
13038
- };
13039
- }
13040
- function normalizeHeapSnapshot(raw) {
13041
- if (!raw || typeof raw !== "object") {
13042
- return createEmptyHeapSnapshot();
13043
- }
13044
- const heap = raw;
13045
- return {
13046
- entriesByPath: heap.entriesByPath && typeof heap.entriesByPath === "object" ? JSON.parse(JSON.stringify(heap.entriesByPath)) : {},
13047
- listsByName: heap.listsByName && typeof heap.listsByName === "object" ? JSON.parse(JSON.stringify(heap.listsByName)) : {},
13048
- variablesByName: heap.variablesByName && typeof heap.variablesByName === "object" ? JSON.parse(JSON.stringify(heap.variablesByName)) : {},
13049
- updatedAt: typeof heap.updatedAt === "number" ? heap.updatedAt : Date.now()
13050
- };
13051
- }
13052
- function normalizeGraphPathSegment(value) {
13053
- return value.trim().toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_+|_+$/g, "");
13054
- }
13055
- function extractRecordIdFromGraphPath(path2, className) {
13056
- const normalizedPrefix = `${normalizeGraphPathSegment(className)}_`;
13057
- if (path2.startsWith(normalizedPrefix)) {
13058
- return path2.slice(normalizedPrefix.length);
13059
- }
13060
- const legacyPrefix = `${className}_`;
13061
- if (path2.startsWith(legacyPrefix)) {
13062
- return path2.slice(legacyPrefix.length);
13063
- }
13064
- return path2;
13065
- }
13066
- function toRecordSearchResult(className, node) {
13067
- const path2 = typeof node.path === "string" ? node.path : "";
13068
- if (!path2) return null;
13069
- const fields = Array.isArray(node.submodels) ? node.submodels.flatMap(
13070
- (submodel) => {
13071
- const name = typeof submodel?.label === "string" && submodel.label.trim() ? submodel.label : typeof submodel?.path === "string" ? submodel.path.split(":").pop() || submodel.path : "";
13072
- if (!name) return [];
13073
- if (typeof submodel.string_value === "string") {
13074
- return [{ name, type: "string", value: submodel.string_value }];
13075
- }
13076
- if (typeof submodel.number_value === "number") {
13077
- return [{ name, type: "number", value: submodel.number_value }];
13078
- }
13079
- if (typeof submodel.boolean_value === "boolean") {
13080
- return [
13081
- {
13082
- name,
13083
- type: "boolean",
13084
- value: submodel.boolean_value
13085
- }
13086
- ];
13087
- }
13088
- return [];
13089
- }
13090
- ) : [];
13091
- return {
13092
- path: path2,
13093
- className,
13094
- id: extractRecordIdFromGraphPath(path2, className),
13095
- label: typeof node.label === "string" && node.label.trim() ? node.label : extractRecordIdFromGraphPath(path2, className),
13096
- description: typeof node.description === "string" && node.description.trim() ? node.description : null,
13097
- fields
13098
- };
13099
- }
13100
- function normalizeRecordSearchText(value) {
13101
- return value.trim().toLowerCase().replace(/[^a-z0-9]+/g, " ").replace(/\s+/g, " ").trim();
13102
- }
13103
- function rankRecordSearchResult(result, query, index) {
13104
- const normalizedQuery = normalizeRecordSearchText(query);
13105
- if (!normalizedQuery) {
13106
- return index;
13107
- }
13108
- const label = normalizeRecordSearchText(result.label || "");
13109
- const id = normalizeRecordSearchText(result.id || "");
13110
- const path2 = normalizeRecordSearchText(result.path || "");
13111
- const className = normalizeRecordSearchText(result.className || "");
13112
- const searchable = [label, id, path2, className].filter(Boolean);
13113
- if (label === normalizedQuery) return index;
13114
- if (id === normalizedQuery || path2 === normalizedQuery) return 100 + index;
13115
- if (label.startsWith(normalizedQuery)) return 200 + index;
13116
- if (searchable.some((value) => value.startsWith(normalizedQuery))) {
13117
- return 300 + index;
13118
- }
13119
- if (label.includes(normalizedQuery)) return 400 + index;
13120
- if (searchable.some((value) => value.includes(normalizedQuery))) {
13121
- return 500 + index;
13122
- }
13123
- return 900 + index;
13124
- }
13125
- function deriveRuntimeBaseUrl(apiEndpoint) {
13126
- try {
13127
- const endpoint = new URL(apiEndpoint);
13128
- const graphqlSuffix = "/orchestrator/graphql";
13129
- if (endpoint.pathname.endsWith(graphqlSuffix)) {
13130
- endpoint.pathname = endpoint.pathname.slice(0, -graphqlSuffix.length);
13131
- } else if (endpoint.pathname.endsWith("/graphql")) {
13132
- endpoint.pathname = endpoint.pathname.slice(0, -"/graphql".length);
13133
- }
13134
- endpoint.search = "";
13135
- endpoint.hash = "";
13136
- return endpoint.toString().replace(/\/$/, "");
13137
- } catch {
13138
- return apiEndpoint.replace(/\/orchestrator\/graphql$/, "").replace(/\/$/, "");
13139
- }
13140
- }
13141
13145
  function normalizeSubject(subject) {
13142
13146
  const granularId = subject.granularId || subject.subjectId;
13143
13147
  const userId = subject.userId || subject.identityId || granularId;