@metriport/fhir-sdk 0.30.0-alpha.1

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.
Files changed (155) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +564 -0
  3. package/dist/__tests__/careplan.test.d.ts +2 -0
  4. package/dist/__tests__/careplan.test.d.ts.map +1 -0
  5. package/dist/__tests__/careplan.test.js +544 -0
  6. package/dist/__tests__/careplan.test.js.map +1 -0
  7. package/dist/__tests__/clinical-dates.test.d.ts +2 -0
  8. package/dist/__tests__/clinical-dates.test.d.ts.map +1 -0
  9. package/dist/__tests__/clinical-dates.test.js +341 -0
  10. package/dist/__tests__/clinical-dates.test.js.map +1 -0
  11. package/dist/__tests__/coding-utilities.test.d.ts +2 -0
  12. package/dist/__tests__/coding-utilities.test.d.ts.map +1 -0
  13. package/dist/__tests__/coding-utilities.test.js +482 -0
  14. package/dist/__tests__/coding-utilities.test.js.map +1 -0
  15. package/dist/__tests__/date-range-performance.test.d.ts +2 -0
  16. package/dist/__tests__/date-range-performance.test.d.ts.map +1 -0
  17. package/dist/__tests__/date-range-performance.test.js +218 -0
  18. package/dist/__tests__/date-range-performance.test.js.map +1 -0
  19. package/dist/__tests__/date-range-search.test.d.ts +2 -0
  20. package/dist/__tests__/date-range-search.test.d.ts.map +1 -0
  21. package/dist/__tests__/date-range-search.test.js +215 -0
  22. package/dist/__tests__/date-range-search.test.js.map +1 -0
  23. package/dist/__tests__/env-setup.d.ts +2 -0
  24. package/dist/__tests__/env-setup.d.ts.map +1 -0
  25. package/dist/__tests__/env-setup.js +37 -0
  26. package/dist/__tests__/env-setup.js.map +1 -0
  27. package/dist/__tests__/fhir-bundle-sdk-basic.test.d.ts +2 -0
  28. package/dist/__tests__/fhir-bundle-sdk-basic.test.d.ts.map +1 -0
  29. package/dist/__tests__/fhir-bundle-sdk-basic.test.js +19 -0
  30. package/dist/__tests__/fhir-bundle-sdk-basic.test.js.map +1 -0
  31. package/dist/__tests__/fhir-bundle-sdk.test.d.ts +2 -0
  32. package/dist/__tests__/fhir-bundle-sdk.test.d.ts.map +1 -0
  33. package/dist/__tests__/fhir-bundle-sdk.test.js +953 -0
  34. package/dist/__tests__/fhir-bundle-sdk.test.js.map +1 -0
  35. package/dist/__tests__/fixtures/fhir-bundles.d.ts +31 -0
  36. package/dist/__tests__/fixtures/fhir-bundles.d.ts.map +1 -0
  37. package/dist/__tests__/fixtures/fhir-bundles.js +487 -0
  38. package/dist/__tests__/fixtures/fhir-bundles.js.map +1 -0
  39. package/dist/__tests__/phase1-verification.test.d.ts +2 -0
  40. package/dist/__tests__/phase1-verification.test.d.ts.map +1 -0
  41. package/dist/__tests__/phase1-verification.test.js +141 -0
  42. package/dist/__tests__/phase1-verification.test.js.map +1 -0
  43. package/dist/__tests__/phase2-verification.test.d.ts +2 -0
  44. package/dist/__tests__/phase2-verification.test.d.ts.map +1 -0
  45. package/dist/__tests__/phase2-verification.test.js +234 -0
  46. package/dist/__tests__/phase2-verification.test.js.map +1 -0
  47. package/dist/__tests__/phase3-verification.test.d.ts +2 -0
  48. package/dist/__tests__/phase3-verification.test.d.ts.map +1 -0
  49. package/dist/__tests__/phase3-verification.test.js +121 -0
  50. package/dist/__tests__/phase3-verification.test.js.map +1 -0
  51. package/dist/__tests__/phase4-verification.test.d.ts +2 -0
  52. package/dist/__tests__/phase4-verification.test.d.ts.map +1 -0
  53. package/dist/__tests__/phase4-verification.test.js +168 -0
  54. package/dist/__tests__/phase4-verification.test.js.map +1 -0
  55. package/dist/__tests__/phase5-verification.test.d.ts +2 -0
  56. package/dist/__tests__/phase5-verification.test.d.ts.map +1 -0
  57. package/dist/__tests__/phase5-verification.test.js +397 -0
  58. package/dist/__tests__/phase5-verification.test.js.map +1 -0
  59. package/dist/__tests__/reverse-references.test.d.ts +2 -0
  60. package/dist/__tests__/reverse-references.test.d.ts.map +1 -0
  61. package/dist/__tests__/reverse-references.test.js +294 -0
  62. package/dist/__tests__/reverse-references.test.js.map +1 -0
  63. package/dist/__tests__/type-guards.test.d.ts +2 -0
  64. package/dist/__tests__/type-guards.test.d.ts.map +1 -0
  65. package/dist/__tests__/type-guards.test.js +163 -0
  66. package/dist/__tests__/type-guards.test.js.map +1 -0
  67. package/dist/clinical-dates.d.ts +26 -0
  68. package/dist/clinical-dates.d.ts.map +1 -0
  69. package/dist/clinical-dates.js +571 -0
  70. package/dist/clinical-dates.js.map +1 -0
  71. package/dist/fhir-bundle-sdk.d.ts +233 -0
  72. package/dist/fhir-bundle-sdk.d.ts.map +1 -0
  73. package/dist/fhir-bundle-sdk.js +545 -0
  74. package/dist/fhir-bundle-sdk.js.map +1 -0
  75. package/dist/index.d.ts +8 -0
  76. package/dist/index.d.ts.map +1 -0
  77. package/dist/index.js +23 -0
  78. package/dist/index.js.map +1 -0
  79. package/dist/internal/bundle-operations.d.ts +14 -0
  80. package/dist/internal/bundle-operations.d.ts.map +1 -0
  81. package/dist/internal/bundle-operations.js +55 -0
  82. package/dist/internal/bundle-operations.js.map +1 -0
  83. package/dist/internal/coding-systems.d.ts +48 -0
  84. package/dist/internal/coding-systems.d.ts.map +1 -0
  85. package/dist/internal/coding-systems.js +55 -0
  86. package/dist/internal/coding-systems.js.map +1 -0
  87. package/dist/internal/coding-utilities.d.ts +27 -0
  88. package/dist/internal/coding-utilities.d.ts.map +1 -0
  89. package/dist/internal/coding-utilities.js +297 -0
  90. package/dist/internal/coding-utilities.js.map +1 -0
  91. package/dist/internal/date-extraction.d.ts +12 -0
  92. package/dist/internal/date-extraction.d.ts.map +1 -0
  93. package/dist/internal/date-extraction.js +629 -0
  94. package/dist/internal/date-extraction.js.map +1 -0
  95. package/dist/internal/graph-traversal.d.ts +13 -0
  96. package/dist/internal/graph-traversal.d.ts.map +1 -0
  97. package/dist/internal/graph-traversal.js +89 -0
  98. package/dist/internal/graph-traversal.js.map +1 -0
  99. package/dist/internal/indexing.d.ts +17 -0
  100. package/dist/internal/indexing.d.ts.map +1 -0
  101. package/dist/internal/indexing.js +129 -0
  102. package/dist/internal/indexing.js.map +1 -0
  103. package/dist/internal/llm-context.d.ts +40 -0
  104. package/dist/internal/llm-context.d.ts.map +1 -0
  105. package/dist/internal/llm-context.js +214 -0
  106. package/dist/internal/llm-context.js.map +1 -0
  107. package/dist/internal/reference-resolution.d.ts +29 -0
  108. package/dist/internal/reference-resolution.d.ts.map +1 -0
  109. package/dist/internal/reference-resolution.js +338 -0
  110. package/dist/internal/reference-resolution.js.map +1 -0
  111. package/dist/internal/reference-utils.d.ts +26 -0
  112. package/dist/internal/reference-utils.d.ts.map +1 -0
  113. package/dist/internal/reference-utils.js +89 -0
  114. package/dist/internal/reference-utils.js.map +1 -0
  115. package/dist/internal/transparent-proxy.d.ts +12 -0
  116. package/dist/internal/transparent-proxy.d.ts.map +1 -0
  117. package/dist/internal/transparent-proxy.js +81 -0
  118. package/dist/internal/transparent-proxy.js.map +1 -0
  119. package/dist/internal/validation.d.ts +16 -0
  120. package/dist/internal/validation.d.ts.map +1 -0
  121. package/dist/internal/validation.js +45 -0
  122. package/dist/internal/validation.js.map +1 -0
  123. package/dist/type-guards.d.ts +212 -0
  124. package/dist/type-guards.d.ts.map +1 -0
  125. package/dist/type-guards.js +375 -0
  126. package/dist/type-guards.js.map +1 -0
  127. package/dist/types/coding-fields.d.ts +311 -0
  128. package/dist/types/coding-fields.d.ts.map +1 -0
  129. package/dist/types/coding-fields.js +3 -0
  130. package/dist/types/coding-fields.js.map +1 -0
  131. package/dist/types/sdk-types.d.ts +107 -0
  132. package/dist/types/sdk-types.d.ts.map +1 -0
  133. package/dist/types/sdk-types.js +17 -0
  134. package/dist/types/sdk-types.js.map +1 -0
  135. package/dist/types/smart-resources.d.ts +470 -0
  136. package/dist/types/smart-resources.d.ts.map +1 -0
  137. package/dist/types/smart-resources.js +249 -0
  138. package/dist/types/smart-resources.js.map +1 -0
  139. package/dist/utils/interval-tree/index.d.ts +87 -0
  140. package/dist/utils/interval-tree/index.d.ts.map +1 -0
  141. package/dist/utils/interval-tree/index.js +774 -0
  142. package/dist/utils/interval-tree/index.js.map +1 -0
  143. package/dist/utils/interval-tree/shallowequal/arrays.d.ts +3 -0
  144. package/dist/utils/interval-tree/shallowequal/arrays.d.ts.map +1 -0
  145. package/dist/utils/interval-tree/shallowequal/arrays.js +25 -0
  146. package/dist/utils/interval-tree/shallowequal/arrays.js.map +1 -0
  147. package/dist/utils/interval-tree/shallowequal/index.d.ts +6 -0
  148. package/dist/utils/interval-tree/shallowequal/index.d.ts.map +1 -0
  149. package/dist/utils/interval-tree/shallowequal/index.js +28 -0
  150. package/dist/utils/interval-tree/shallowequal/index.js.map +1 -0
  151. package/dist/utils/interval-tree/shallowequal/objects.d.ts +3 -0
  152. package/dist/utils/interval-tree/shallowequal/objects.d.ts.map +1 -0
  153. package/dist/utils/interval-tree/shallowequal/objects.js +28 -0
  154. package/dist/utils/interval-tree/shallowequal/objects.js.map +1 -0
  155. package/package.json +70 -0
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.walkReferences = void 0;
4
+ const smart_resources_1 = require("../types/smart-resources");
5
+ /**
6
+ * Walk references from a starting resource using BFS traversal.
7
+ * Discovers all reachable resources up to maxDepth levels.
8
+ *
9
+ * @param startResource - The smart resource to start traversal from
10
+ * @param options - Walk options including maxDepth and includeStartResource
11
+ * @returns WalkResult containing all discovered resources organized by depth
12
+ */
13
+ function walkReferences(startResource, options) {
14
+ const maxDepth = options?.maxDepth ?? Infinity;
15
+ const includeStartResource = options?.includeStartResource ?? true;
16
+ const resourcesByDepth = new Map();
17
+ const visited = new Set();
18
+ const queue = [];
19
+ // Initialize with start resource at depth 0 - cast to avoid type complexity
20
+ queue.push({ resource: startResource, depth: 0 });
21
+ if (startResource.id) {
22
+ visited.add(startResource.id);
23
+ }
24
+ let maxDepthReached = 0;
25
+ while (queue.length > 0) {
26
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
27
+ const { resource, depth } = queue.shift();
28
+ // Track max depth reached
29
+ if (depth > maxDepthReached) {
30
+ maxDepthReached = depth;
31
+ }
32
+ // Add resource to current depth level
33
+ if (!resourcesByDepth.has(depth)) {
34
+ resourcesByDepth.set(depth, []);
35
+ }
36
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
37
+ resourcesByDepth.get(depth).push(resource);
38
+ // Stop if we've reached max depth
39
+ if (depth >= maxDepth) {
40
+ continue;
41
+ }
42
+ // Get all reference methods for this resource type
43
+ const referenceMethods = smart_resources_1.REFERENCE_METHOD_MAPPING[resource.resourceType];
44
+ if (!referenceMethods) {
45
+ continue;
46
+ }
47
+ // Iterate through all reference methods
48
+ for (const methodName of Object.keys(referenceMethods)) {
49
+ // Call the reference method
50
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
51
+ const referencedResources = resource[methodName]();
52
+ if (!referencedResources) {
53
+ continue;
54
+ }
55
+ // Handle both single and array references
56
+ const resourceArray = Array.isArray(referencedResources)
57
+ ? referencedResources
58
+ : [referencedResources];
59
+ for (const referencedResource of resourceArray) {
60
+ if (!referencedResource?.id) {
61
+ continue;
62
+ }
63
+ // Skip if already visited
64
+ if (visited.has(referencedResource.id)) {
65
+ continue;
66
+ }
67
+ visited.add(referencedResource.id);
68
+ queue.push({ resource: referencedResource, depth: depth + 1 });
69
+ }
70
+ }
71
+ }
72
+ // Build flat array of all resources
73
+ const allResources = [];
74
+ for (const [depth, resources] of resourcesByDepth.entries()) {
75
+ // Skip depth 0 (start resource) if includeStartResource is false
76
+ if (depth === 0 && !includeStartResource) {
77
+ continue;
78
+ }
79
+ allResources.push(...resources);
80
+ }
81
+ return {
82
+ resourcesByDepth,
83
+ allResources,
84
+ startResource,
85
+ depthReached: maxDepthReached,
86
+ };
87
+ }
88
+ exports.walkReferences = walkReferences;
89
+ //# sourceMappingURL=graph-traversal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph-traversal.js","sourceRoot":"","sources":["../../src/internal/graph-traversal.ts"],"names":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AC;AA7FD,wCA6FC"}
@@ -0,0 +1,17 @@
1
+ import { Bundle, Resource } from "@medplum/fhirtypes";
2
+ import { ReverseReference } from "../types/sdk-types";
3
+ import { IntervalTree } from "../utils/interval-tree";
4
+ import { DateIndexRecord } from "../types/sdk-types";
5
+ /**
6
+ * Build O(1) indexes for resource lookup
7
+ */
8
+ export declare function buildResourceIndexes(bundle: Bundle, resourcesById: Map<string, Resource>, resourcesByFullUrl: Map<string, Resource>, resourcesByType: Map<string, Resource[]>, reverseReferencesById: Map<string, ReverseReference[]>, dateRangeIndex: IntervalTree<DateIndexRecord, number>, resolutionStack: Set<string>): void;
9
+ /**
10
+ * Build reverse reference index for O(1) reverse lookup
11
+ */
12
+ export declare function buildReverseReferenceIndex(bundle: Bundle, reverseReferencesById: Map<string, ReverseReference[]>, resourcesByFullUrl: Map<string, Resource>): void;
13
+ /**
14
+ * Build date range index for O(log n + k) date range searches
15
+ */
16
+ export declare function buildDateRangeIndex(bundle: Bundle, dateRangeIndex: IntervalTree<DateIndexRecord, number>): void;
17
+ //# sourceMappingURL=indexing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"indexing.d.ts","sourceRoot":"","sources":["../../src/internal/indexing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAQrD;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,MAAM,EACd,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,EACpC,kBAAkB,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,EACzC,eAAe,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAC,EACxC,qBAAqB,EAAE,GAAG,CAAC,MAAM,EAAE,gBAAgB,EAAE,CAAC,EACtD,cAAc,EAAE,YAAY,CAAC,eAAe,EAAE,MAAM,CAAC,EACrD,eAAe,EAAE,GAAG,CAAC,MAAM,CAAC,GAC3B,IAAI,CAuCN;AAED;;GAEG;AACH,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,MAAM,EACd,qBAAqB,EAAE,GAAG,CAAC,MAAM,EAAE,gBAAgB,EAAE,CAAC,EACtD,kBAAkB,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,GACxC,IAAI,CA6DN;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,MAAM,EACd,cAAc,EAAE,YAAY,CAAC,eAAe,EAAE,MAAM,CAAC,GACpD,IAAI,CA6BN"}
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildDateRangeIndex = exports.buildReverseReferenceIndex = exports.buildResourceIndexes = void 0;
4
+ const reference_utils_1 = require("./reference-utils");
5
+ const date_extraction_1 = require("./date-extraction");
6
+ /**
7
+ * Build O(1) indexes for resource lookup
8
+ */
9
+ function buildResourceIndexes(bundle, resourcesById, resourcesByFullUrl, resourcesByType, reverseReferencesById, dateRangeIndex, resolutionStack) {
10
+ if (!bundle.entry) {
11
+ return;
12
+ }
13
+ for (const entry of bundle.entry) {
14
+ if (!entry.resource) {
15
+ continue;
16
+ }
17
+ const resource = entry.resource;
18
+ // Index by resource.id if it exists
19
+ if (resource.id) {
20
+ resourcesById.set(resource.id, resource);
21
+ }
22
+ // Index by fullUrl if it exists
23
+ if (entry.fullUrl) {
24
+ resourcesByFullUrl.set(entry.fullUrl, resource);
25
+ }
26
+ // Index by resource type for type-specific getters
27
+ const resourceType = resource.resourceType;
28
+ if (!resourcesByType.has(resourceType)) {
29
+ resourcesByType.set(resourceType, []);
30
+ }
31
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
32
+ resourcesByType.get(resourceType).push(resource);
33
+ }
34
+ // Build reverse reference index
35
+ buildReverseReferenceIndex(bundle, reverseReferencesById, resourcesByFullUrl);
36
+ // Build date range index
37
+ buildDateRangeIndex(bundle, dateRangeIndex);
38
+ // Clear resolution stack after index building
39
+ resolutionStack.clear();
40
+ }
41
+ exports.buildResourceIndexes = buildResourceIndexes;
42
+ /**
43
+ * Build reverse reference index for O(1) reverse lookup
44
+ */
45
+ function buildReverseReferenceIndex(bundle, reverseReferencesById, resourcesByFullUrl) {
46
+ if (!bundle.entry) {
47
+ return;
48
+ }
49
+ for (const entry of bundle.entry) {
50
+ if (!entry.resource) {
51
+ continue;
52
+ }
53
+ const resource = entry.resource;
54
+ const sourceResourceId = resource.id ?? entry.fullUrl ?? "unknown";
55
+ const references = (0, reference_utils_1.findAllReferences)(resource);
56
+ for (const { field, reference } of references) {
57
+ // Extract target ID from reference
58
+ const targetId = (0, reference_utils_1.extractTargetIdFromReference)(reference);
59
+ if (!targetId) {
60
+ continue;
61
+ }
62
+ // Add to reverse index for both the extracted ID and the full reference
63
+ const reverseRef = {
64
+ sourceResourceId,
65
+ sourceResourceType: resource.resourceType,
66
+ referenceField: (0, reference_utils_1.normalizeReferenceField)(field),
67
+ };
68
+ // Index by extracted resource ID (e.g., "patient-123" from "Patient/patient-123")
69
+ if (!reverseReferencesById.has(targetId)) {
70
+ reverseReferencesById.set(targetId, []);
71
+ }
72
+ reverseReferencesById.get(targetId)?.push(reverseRef);
73
+ // Also index by full reference if it's different (e.g., "urn:uuid:patient-123")
74
+ if (targetId !== reference) {
75
+ if (!reverseReferencesById.has(reference)) {
76
+ reverseReferencesById.set(reference, []);
77
+ }
78
+ reverseReferencesById.get(reference)?.push(reverseRef);
79
+ }
80
+ // Also try to index by the actual resource ID if we can find it efficiently
81
+ // Check if this reference can be resolved to a resource with a different ID
82
+ if (!reference.includes("/")) {
83
+ // This is likely a fullUrl reference (e.g., "urn:uuid:xxx")
84
+ // Check if we have a resource with this fullUrl
85
+ const targetResource = resourcesByFullUrl.get(reference);
86
+ if (targetResource?.id &&
87
+ targetResource.id !== targetId &&
88
+ targetResource.id !== reference) {
89
+ if (!reverseReferencesById.has(targetResource.id)) {
90
+ reverseReferencesById.set(targetResource.id, []);
91
+ }
92
+ reverseReferencesById.get(targetResource.id)?.push(reverseRef);
93
+ }
94
+ }
95
+ }
96
+ }
97
+ }
98
+ exports.buildReverseReferenceIndex = buildReverseReferenceIndex;
99
+ /**
100
+ * Build date range index for O(log n + k) date range searches
101
+ */
102
+ function buildDateRangeIndex(bundle, dateRangeIndex) {
103
+ if (!bundle.entry) {
104
+ return;
105
+ }
106
+ for (const entry of bundle.entry) {
107
+ if (!entry.resource || !entry.resource.id) {
108
+ continue;
109
+ }
110
+ const resource = entry.resource;
111
+ const dateIntervals = (0, date_extraction_1.extractDateIntervalsFromResource)(resource);
112
+ for (const interval of dateIntervals) {
113
+ try {
114
+ dateRangeIndex.insert(interval);
115
+ }
116
+ catch (error) {
117
+ // Handle bad data case where low > high
118
+ console.warn(`Error inserting date interval (${interval.low}, ${interval.high}) into index. Swapping values.`);
119
+ dateRangeIndex.insert({
120
+ ...interval,
121
+ low: interval.high,
122
+ high: interval.low,
123
+ });
124
+ }
125
+ }
126
+ }
127
+ }
128
+ exports.buildDateRangeIndex = buildDateRangeIndex;
129
+ //# sourceMappingURL=indexing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"indexing.js","sourceRoot":"","sources":["../../src/internal/indexing.ts"],"names":[],"mappings":";;;AAIA,uDAI2B;AAC3B,uDAAqE;AAErE;;GAEG;AACH,SAAgB,oBAAoB,CAClC,MAAc,EACd,aAAoC,EACpC,kBAAyC,EACzC,eAAwC,EACxC,qBAAsD,EACtD,cAAqD,EACrD,eAA4B;IAE5B,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE;QACjB,OAAO;KACR;IAED,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,KAAK,EAAE;QAChC,IAAI,CAAC,KAAK,CA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extOptions, WalkResult } from "../types/sdk-types";
4
+ /**
5
+ * Strip non-clinical metadata from a FHIR resource to reduce noise for LLM consumption.
6
+ * Removes: meta, extension, modifierExtension, text, id, identifier, and all reference fields
7
+ * defined in REFERENCE_METHOD_MAPPING
8
+ * Returns an immutable copy without mutating the original.
9
+ */
10
+ export declare function stripNonClinicalData<T extends Resource>(resource: T): T;
11
+ /**
12
+ * Group resources by their resource type
13
+ */
14
+ export declare function groupResourcesByType(resources: Resource[]): Record<string, Resource[]>;
15
+ /**
16
+ * Format a single resource as readable text
17
+ */
18
+ export declare function formatResourceAsText(resource: Resource): string;
19
+ /**
20
+ * Format resources as structured text for better LLM readability
21
+ */
22
+ export declare function formatAsStructuredText(resourcesByDepth: Map<number, Resource[]>): string;
23
+ /**
24
+ * Format resources as JSON with depth annotations
25
+ */
26
+ export declare function formatAsJSON(resourcesByDepth: Map<number, Resource[]>): string;
27
+ /**
28
+ * Generate LLM-friendly context from a starting resource and its related resources.
29
+ * Uses BFS to discover related resources, strips non-clinical data, and formats output.
30
+ *
31
+ * @param startResource - The smart resource to start traversal from
32
+ * @param options - Options for depth, inclusion, and format
33
+ * @param walkReferencesFn - Function to walk references (passed in to avoid circular dependency)
34
+ * @returns Formatted string suitable for LLM context
35
+ */
36
+ export declare function generateLLMContext<T extends Resource>(startResource: Smart<T>, options: LLMContextOptions | undefined, walkReferencesFn: (startResource: Smart<T>, options: {
37
+ maxDepth: number;
38
+ includeStartResource: boolean;
39
+ }) => WalkResult<T>): string;
40
+ //# sourceMappingURL=llm-context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"llm-context.d.ts","sourceRoot":"","sources":["../../src/internal/llm-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,KAAK,EAA4B,MAAM,0BAA0B,CAAC;AAC3E,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAuBnE;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,SAAS,QAAQ,EAAE,QAAQ,EAAE,CAAC,GAAG,CAAC,CAkCvE;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,QAAQ,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAC,CAetF;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,QAAQ,GAAG,MAAM,CA0C/D;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,gBAAgB,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,MAAM,CAqCxF;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,gBAAgB,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,MAAM,CAc9E;AAED;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,SAAS,QAAQ,EACnD,aAAa,EAAE,KAAK,CAAC,CAAC,CAAC,EACvB,OAAO,EAAE,iBAAiB,GAAG,SAAS,EACtC,gBAAgB,EAAE,CAChB,aAAa,EAAE,KAAK,CAAC,CAAC,CAAC,EACvB,OAAO,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,oBAAoB,EAAE,OAAO,CAAA;CAAE,KACzD,UAAU,CAAC,CAAC,CAAC,GACjB,MAAM,CAqCR"}
@@ -0,0 +1,214 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateLLMContext = exports.formatAsJSON = exports.formatAsStructuredText = exports.formatResourceAsText = exports.groupResourcesByType = exports.stripNonClinicalData = void 0;
4
+ const smart_resources_1 = require("../types/smart-resources");
5
+ /**
6
+ * Build a set of all reference field names from REFERENCE_METHOD_MAPPING.
7
+ * Extracts base field names, handling nested paths (e.g., "participant.individual" -> "participant")
8
+ */
9
+ function buildReferenceFieldSet() {
10
+ const fields = new Set();
11
+ for (const resourceMapping of Object.values(smart_resources_1.REFERENCE_METHOD_MAPPING)) {
12
+ for (const fieldPath of Object.values(resourceMapping)) {
13
+ // Handle nested paths - take the first part
14
+ const parts = fieldPath.split(".");
15
+ const baseField = parts[0];
16
+ if (baseField !== undefined) {
17
+ fields.add(baseField);
18
+ }
19
+ }
20
+ }
21
+ return fields;
22
+ }
23
+ const REFERENCE_FIELDS = buildReferenceFieldSet();
24
+ /**
25
+ * Strip non-clinical metadata from a FHIR resource to reduce noise for LLM consumption.
26
+ * Removes: meta, extension, modifierExtension, text, id, identifier, and all reference fields
27
+ * defined in REFERENCE_METHOD_MAPPING
28
+ * Returns an immutable copy without mutating the original.
29
+ */
30
+ function stripNonClinicalData(resource) {
31
+ function deepCloneAndStrip(obj, resourceType) {
32
+ if (obj === null || obj === undefined) {
33
+ return obj;
34
+ }
35
+ if (Array.isArray(obj)) {
36
+ return obj.map(item => deepCloneAndStrip(item, resourceType));
37
+ }
38
+ if (typeof obj === "object") {
39
+ const result = {};
40
+ for (const [key, value] of Object.entries(obj)) {
41
+ // Skip non-clinical fields, identifiers, and reference fields
42
+ if (key === "meta" ||
43
+ key === "extension" ||
44
+ key === "modifierExtension" ||
45
+ key === "text" ||
46
+ key === "id" ||
47
+ key === "identifier" ||
48
+ REFERENCE_FIELDS.has(key)) {
49
+ continue;
50
+ }
51
+ result[key] = deepCloneAndStrip(value, resourceType);
52
+ }
53
+ return result;
54
+ }
55
+ return obj;
56
+ }
57
+ return deepCloneAndStrip(resource, resource.resourceType);
58
+ }
59
+ exports.stripNonClinicalData = stripNonClinicalData;
60
+ /**
61
+ * Group resources by their resource type
62
+ */
63
+ function groupResourcesByType(resources) {
64
+ const grouped = {};
65
+ for (const resource of resources) {
66
+ const type = resource.resourceType;
67
+ if (!grouped[type]) {
68
+ grouped[type] = [];
69
+ }
70
+ const resourceArray = grouped[type];
71
+ if (resourceArray) {
72
+ resourceArray.push(resource);
73
+ }
74
+ }
75
+ return grouped;
76
+ }
77
+ exports.groupResourcesByType = groupResourcesByType;
78
+ /**
79
+ * Format a single resource as readable text
80
+ */
81
+ function formatResourceAsText(resource) {
82
+ const lines = [];
83
+ lines.push(`[Type: ${resource.resourceType}, ID: ${resource.id ?? "unknown"}]`);
84
+ function formatValue(value, indent = 2) {
85
+ const prefix = " ".repeat(indent);
86
+ const result = [];
87
+ if (value === null || value === undefined) {
88
+ return [`${prefix}(none)`];
89
+ }
90
+ if (Array.isArray(value)) {
91
+ if (value.length === 0) {
92
+ return [`${prefix}[]`];
93
+ }
94
+ for (const [index, item] of value.entries()) {
95
+ result.push(`${prefix}[${index}]:`);
96
+ result.push(...formatValue(item, indent + 2));
97
+ }
98
+ return result;
99
+ }
100
+ if (typeof value === "object") {
101
+ for (const [key, val] of Object.entries(value)) {
102
+ result.push(`${prefix}${key}:`);
103
+ result.push(...formatValue(val, indent + 2));
104
+ }
105
+ return result;
106
+ }
107
+ return [`${prefix}${String(value)}`];
108
+ }
109
+ // Format key fields
110
+ for (const [key, value] of Object.entries(resource)) {
111
+ if (key === "resourceType" || key === "id")
112
+ continue; // Already shown in header
113
+ lines.push(` ${key}:`);
114
+ lines.push(...formatValue(value, 4));
115
+ }
116
+ return lines.join("\n");
117
+ }
118
+ exports.formatResourceAsText = formatResourceAsText;
119
+ /**
120
+ * Format resources as structured text for better LLM readability
121
+ */
122
+ function formatAsStructuredText(resourcesByDepth) {
123
+ const lines = [];
124
+ // Primary resource section
125
+ const primaryResources = resourcesByDepth.get(0) ?? [];
126
+ if (primaryResources.length > 0) {
127
+ lines.push("PRIMARY RESOURCE:");
128
+ lines.push("=".repeat(80));
129
+ for (const resource of primaryResources) {
130
+ lines.push(formatResourceAsText(resource));
131
+ lines.push("");
132
+ }
133
+ }
134
+ // Related resources by depth
135
+ for (const [depth, resources] of resourcesByDepth.entries()) {
136
+ if (depth === 0)
137
+ continue; // Already handled above
138
+ const depthLabel = depth === 1 ? "DIRECTLY REFERENCED" : `INDIRECTLY REFERENCED (Depth ${depth})`;
139
+ lines.push(`${depthLabel} RESOURCES:`);
140
+ lines.push("=".repeat(80));
141
+ // Group by resource type for better organization
142
+ const groupedByType = groupResourcesByType(resources);
143
+ for (const [resourceType, typeResources] of Object.entries(groupedByType)) {
144
+ lines.push(`\n--- ${resourceType} (${typeResources.length}) ---\n`);
145
+ for (const resource of typeResources) {
146
+ lines.push(formatResourceAsText(resource));
147
+ lines.push("");
148
+ }
149
+ }
150
+ }
151
+ return lines.join("\n");
152
+ }
153
+ exports.formatAsStructuredText = formatAsStructuredText;
154
+ /**
155
+ * Format resources as JSON with depth annotations
156
+ */
157
+ function formatAsJSON(resourcesByDepth) {
158
+ const relatedResourcesByDepth = {};
159
+ for (const [depth, resources] of resourcesByDepth.entries()) {
160
+ if (depth === 0)
161
+ continue; // Skip primary resource (already at top)
162
+ relatedResourcesByDepth[`depth_${depth}`] = resources;
163
+ }
164
+ const output = {
165
+ primaryResource: resourcesByDepth.get(0)?.[0],
166
+ relatedResourcesByDepth,
167
+ };
168
+ return JSON.stringify(output, null, 2);
169
+ }
170
+ exports.formatAsJSON = formatAsJSON;
171
+ /**
172
+ * Generate LLM-friendly context from a starting resource and its related resources.
173
+ * Uses BFS to discover related resources, strips non-clinical data, and formats output.
174
+ *
175
+ * @param startResource - The smart resource to start traversal from
176
+ * @param options - Options for depth, inclusion, and format
177
+ * @param walkReferencesFn - Function to walk references (passed in to avoid circular dependency)
178
+ * @returns Formatted string suitable for LLM context
179
+ */
180
+ function generateLLMContext(startResource, options, walkReferencesFn) {
181
+ const maxDepth = options?.maxDepth ?? 2;
182
+ const includeStartResource = options?.includeStartResource ?? true;
183
+ const format = options?.format ?? "structured-text";
184
+ // Walk the reference graph
185
+ const walkResult = walkReferencesFn(startResource, {
186
+ maxDepth,
187
+ includeStartResource,
188
+ });
189
+ // Log resource counts for debugging
190
+ console.log(`[LLM Context] Total resources discovered: ${walkResult.allResources.length}`);
191
+ for (const [depth, resources] of walkResult.resourcesByDepth.entries()) {
192
+ console.log(`[LLM Context] Depth ${depth}: ${resources.length} resources`);
193
+ }
194
+ // Strip non-clinical data from all resources
195
+ const cleanedResourcesByDepth = new Map();
196
+ for (const [depth, resources] of walkResult.resourcesByDepth.entries()) {
197
+ // Skip depth 0 if includeStartResource is false
198
+ if (depth === 0 && !includeStartResource) {
199
+ continue;
200
+ }
201
+ cleanedResourcesByDepth.set(depth,
202
+ // Cast to Resource to satisfy type checker - Smart extends Resource
203
+ resources.map(r => stripNonClinicalData(r)));
204
+ }
205
+ // Format based on selected format
206
+ if (format === "json") {
207
+ return formatAsJSON(cleanedResourcesByDepth);
208
+ }
209
+ else {
210
+ return formatAsStructuredText(cleanedResourcesByDepth);
211
+ }
212
+ }
213
+ exports.generateLLMContext = generateLLMContext;
214
+ //# sourceMappingURL=llm-context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"llm-context.js","sourceRoot":"","sources":["../../src/internal/llm-context.ts"],"names":[],"mappings":";;;AACA,8DAA2E;AAG3E;;;GAGG;AACH,SAAS,sBAAsB;IAC7B,MAAM,MAAM,GAAG,IAAI,GAAG,EAAU,CAAC;IACjC,KAAK,MAAM,eAAe,IAAI,MAAM,CAAC,MAAM,CAAC,0CAAwB,CAAC,EAAE;QACrE,KAAK,MAAM,SAAS,IAAI,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,EAAE;YACtD,4CAA4C;YAC5C,MAAM,KAAK,GAAG,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YACnC,MAAM,SAAS,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;YAC3B,IAAI,SAAS,KAAK,SAAS,EAAE;gBAC3B,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;aACvB;SACF;KACF;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,MAAM,gBAAgB,GAAG,sBAAsB,EAAE,CAAC;AAElD;;;;;GAKG;AACH,SAAgB,oBAAoB,CAAqB,QAAW;IAClE,SAAS,iBAAiB,CAAC,GAAY,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EAAoE;QACpE,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,oBAAoB,CAAC,CAAwB,CAAC,CAAC,CACnE,CAAC;KACH;IAED,kCAAkC;IAClC,IAAI,MAAM,KAAK,MAAM,EAAE;QACrB,OAAO,YAAY,CAAC,uBAAuB,CAAC,CAAC;KAC9C;SAAM;QACL,OAAO,sBAAsB,CAAC,uBAAuB,CAAC,CAAC;KACxD;AACH,CAAC;AA5CD,gDA4CC"}
@@ -0,0 +1,29 @@
1
+ import { Resource } from "@medplum/fhirtypes";
2
+ import { Smart } from "../types/smart-resources";
3
+ import { ReverseReferenceOptions } from "../types/sdk-types";
4
+ /**
5
+ * Check if a reference field expects an array of references.
6
+ * Handles both simple paths ("performer") and nested paths ("diagnosis.condition").
7
+ *
8
+ * For nested paths, if the intermediate level is an array, the method returns an array.
9
+ * For example, "diagnosis.condition" returns an array because diagnosis is an array field.
10
+ */
11
+ export declare function isArrayReferenceField(fieldName: string): boolean;
12
+ /**
13
+ * Resolve a single reference object to a resource
14
+ * FR-5.5: Reference resolution methods handle both resource.id and fullUrl matching
15
+ */
16
+ export declare function resolveReferenceObject(referenceObj: unknown, resourcesById: Map<string, Resource>, resourcesByFullUrl: Map<string, Resource>, resolutionStack: Set<string>): Resource | undefined;
17
+ /**
18
+ * Resolve a reference method call to actual resources
19
+ * FR-5.2-5.6: Handle different reference types and patterns
20
+ */
21
+ export declare function resolveReference(methodName: string, resource: Resource, resourcesById: Map<string, Resource>, resourcesByFullUrl: Map<string, Resource>, resolutionStack: Set<string>, createSmartResourceFn: <T extends Resource>(resource: T) => Smart<T>): Smart<Resource> | Smart<Resource>[] | undefined;
22
+ /**
23
+ * Create a smart resource with reference resolution methods
24
+ * FR-5.1: Resources returned by SDK have additional getter methods for each Reference field
25
+ * FR-5.7: Reference resolution operates in O(1) time complexity per reference
26
+ * FR-5.8: Original reference fields remain unchanged
27
+ */
28
+ export declare function createSmartResource<T extends Resource>(resource: T, smartResourceCache: WeakMap<Resource, Smart<Resource>>, resourcesById: Map<string, Resource>, resourcesByFullUrl: Map<string, Resource>, resolutionStack: Set<string>, getResourcesReferencingIdFn: (targetId: string, options?: ReverseReferenceOptions) => Smart<Resource>[]): Smart<T>;
29
+ //# sourceMappingURL=reference-resolution.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reference-resolution.d.ts","sourceRoot":"","sources":["../../src/internal/reference-resolution.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EACL,KAAK,EAIN,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AA2D7D;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAyChE;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,CACpC,YAAY,EAAE,OAAO,EACrB,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,EACpC,kBAAkB,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,EACzC,eAAe,EAAE,GAAG,CAAC,MAAM,CAAC,GAC3B,QAAQ,GAAG,SAAS,CAgCtB;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAC9B,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,QAAQ,EAClB,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,EACpC,kBAAkB,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,EACzC,eAAe,EAAE,GAAG,CAAC,MAAM,CAAC,EAC5B,qBAAqB,EAAE,CAAC,CAAC,SAAS,QAAQ,EAAE,QAAQ,EAAE,CAAC,KAAK,KAAK,CAAC,CAAC,CAAC,GACnE,KAAK,CAAC,QAAQ,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,EAAE,GAAG,SAAS,CA0CjD;AAED;;;;;GAKG;AACH,wBAAg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