@pixerate/editor 0.23.0 → 0.25.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.
Files changed (187) hide show
  1. package/README.md +167 -2
  2. package/THIRD_PARTY_NOTICES.md +80 -0
  3. package/dist/agent/index.cjs +10 -8
  4. package/dist/agent/index.cjs.map +1 -1
  5. package/dist/agent/index.d.cts +4 -3
  6. package/dist/agent/index.d.ts +4 -3
  7. package/dist/agent/index.js +4 -2
  8. package/dist/agent/index.js.map +1 -1
  9. package/dist/canvas/index.cjs +4437 -411
  10. package/dist/canvas/index.cjs.map +1 -1
  11. package/dist/canvas/index.d.cts +934 -4
  12. package/dist/canvas/index.d.ts +934 -4
  13. package/dist/canvas/index.js +4309 -283
  14. package/dist/canvas/index.js.map +1 -1
  15. package/dist/chunk-4BVP64JJ.cjs +1406 -0
  16. package/dist/chunk-4BVP64JJ.cjs.map +1 -0
  17. package/dist/chunk-664LQYFJ.js +115 -0
  18. package/dist/chunk-664LQYFJ.js.map +1 -0
  19. package/dist/chunk-7WISYZFO.cjs +740 -0
  20. package/dist/chunk-7WISYZFO.cjs.map +1 -0
  21. package/dist/chunk-AL6JMGWW.cjs +783 -0
  22. package/dist/chunk-AL6JMGWW.cjs.map +1 -0
  23. package/dist/chunk-BVP3TSLH.cjs +69 -0
  24. package/dist/chunk-BVP3TSLH.cjs.map +1 -0
  25. package/dist/{chunk-RIKBJLS2.cjs → chunk-C7P22VIP.cjs} +2001 -279
  26. package/dist/chunk-C7P22VIP.cjs.map +1 -0
  27. package/dist/chunk-D6FH2YAX.cjs +131 -0
  28. package/dist/chunk-D6FH2YAX.cjs.map +1 -0
  29. package/dist/chunk-ECCCYKGL.js +740 -0
  30. package/dist/chunk-ECCCYKGL.js.map +1 -0
  31. package/dist/{chunk-4SUNJZWB.cjs → chunk-FD4LPFAE.cjs} +48 -200
  32. package/dist/chunk-FD4LPFAE.cjs.map +1 -0
  33. package/dist/{chunk-QBJ22YZR.js → chunk-FH6F67DN.js} +1999 -277
  34. package/dist/chunk-FH6F67DN.js.map +1 -0
  35. package/dist/{chunk-OYXSKQFR.js → chunk-FPENXAEJ.js} +35 -187
  36. package/dist/chunk-FPENXAEJ.js.map +1 -0
  37. package/dist/chunk-HLIMTC6N.js +1 -0
  38. package/dist/chunk-HLIMTC6N.js.map +1 -0
  39. package/dist/chunk-HRMEDT2L.js +1406 -0
  40. package/dist/chunk-HRMEDT2L.js.map +1 -0
  41. package/dist/{chunk-QGHGJ56O.js → chunk-IXHSYOVY.js} +2518 -122
  42. package/dist/chunk-IXHSYOVY.js.map +1 -0
  43. package/dist/chunk-MKA3AHLV.cjs +256 -0
  44. package/dist/chunk-MKA3AHLV.cjs.map +1 -0
  45. package/dist/chunk-NAXVVS5I.js +783 -0
  46. package/dist/chunk-NAXVVS5I.js.map +1 -0
  47. package/dist/chunk-O5RYH2PG.js +69 -0
  48. package/dist/chunk-O5RYH2PG.js.map +1 -0
  49. package/dist/chunk-OXSV64JX.js +689 -0
  50. package/dist/chunk-OXSV64JX.js.map +1 -0
  51. package/dist/chunk-P3RQ5UJE.cjs +689 -0
  52. package/dist/chunk-P3RQ5UJE.cjs.map +1 -0
  53. package/dist/chunk-P6ODPZ4W.js +256 -0
  54. package/dist/chunk-P6ODPZ4W.js.map +1 -0
  55. package/dist/chunk-PL4JB3BF.cjs +1 -0
  56. package/dist/chunk-PL4JB3BF.cjs.map +1 -0
  57. package/dist/{chunk-GVSMRPHB.cjs → chunk-PO6A6QQH.cjs} +2607 -211
  58. package/dist/chunk-PO6A6QQH.cjs.map +1 -0
  59. package/dist/{chunk-JJDLPEWL.js → chunk-QGYDWPRM.js} +2 -2
  60. package/dist/chunk-RR7F5GUM.js +5726 -0
  61. package/dist/chunk-RR7F5GUM.js.map +1 -0
  62. package/dist/chunk-TNNJXF46.cjs +115 -0
  63. package/dist/chunk-TNNJXF46.cjs.map +1 -0
  64. package/dist/chunk-U5HZSFCS.js +131 -0
  65. package/dist/chunk-U5HZSFCS.js.map +1 -0
  66. package/dist/{chunk-MJZBNWG7.js → chunk-UAKDG62Z.js} +309 -581
  67. package/dist/chunk-UAKDG62Z.js.map +1 -0
  68. package/dist/{chunk-CL37JGFW.cjs → chunk-V3CHHWOE.cjs} +4 -4
  69. package/dist/{chunk-CL37JGFW.cjs.map → chunk-V3CHHWOE.cjs.map} +1 -1
  70. package/dist/chunk-WSQKHXW7.cjs +5726 -0
  71. package/dist/chunk-WSQKHXW7.cjs.map +1 -0
  72. package/dist/{chunk-AA4ZEVCC.cjs → chunk-X4N4WUKB.cjs} +336 -608
  73. package/dist/chunk-X4N4WUKB.cjs.map +1 -0
  74. package/dist/commands/index.cjs +74 -1
  75. package/dist/commands/index.cjs.map +1 -1
  76. package/dist/commands/index.d.cts +38 -353
  77. package/dist/commands/index.d.ts +38 -353
  78. package/dist/commands/index.js +73 -0
  79. package/dist/commands/index.js.map +1 -1
  80. package/dist/comment-store-BEMgVlAA.d.cts +135 -0
  81. package/dist/comment-store-BEMgVlAA.d.ts +135 -0
  82. package/dist/compare/index.cjs +15 -0
  83. package/dist/compare/index.cjs.map +1 -0
  84. package/dist/compare/index.d.cts +58 -0
  85. package/dist/compare/index.d.ts +58 -0
  86. package/dist/compare/index.js +15 -0
  87. package/dist/compare/index.js.map +1 -0
  88. package/dist/controller-BIJiNoVs.d.cts +1842 -0
  89. package/dist/controller-BIJiNoVs.d.ts +1842 -0
  90. package/dist/documents-CHT1U5Vs.d.ts +86 -0
  91. package/dist/documents-Gp7wb0Me.d.cts +86 -0
  92. package/dist/extensions/index.cjs +100 -5
  93. package/dist/extensions/index.cjs.map +1 -1
  94. package/dist/extensions/index.d.cts +16 -4
  95. package/dist/extensions/index.d.ts +16 -4
  96. package/dist/extensions/index.js +100 -5
  97. package/dist/generation/index.cjs +2317 -0
  98. package/dist/generation/index.cjs.map +1 -0
  99. package/dist/generation/index.d.cts +1015 -0
  100. package/dist/generation/index.d.ts +1015 -0
  101. package/dist/generation/index.js +2317 -0
  102. package/dist/generation/index.js.map +1 -0
  103. package/dist/grammar/index.cjs +48 -3
  104. package/dist/grammar/index.cjs.map +1 -1
  105. package/dist/grammar/index.d.cts +322 -100
  106. package/dist/grammar/index.d.ts +322 -100
  107. package/dist/grammar/index.js +54 -9
  108. package/dist/image-editor/index.cjs +4392 -481
  109. package/dist/image-editor/index.cjs.map +1 -1
  110. package/dist/image-editor/index.d.cts +1249 -97
  111. package/dist/image-editor/index.d.ts +1249 -97
  112. package/dist/image-editor/index.js +4281 -370
  113. package/dist/image-editor/index.js.map +1 -1
  114. package/dist/index-7a6bHTLy.d.ts +1754 -0
  115. package/dist/index-Csd5qI8u.d.cts +1754 -0
  116. package/dist/index.cjs +1682 -99
  117. package/dist/index.cjs.map +1 -1
  118. package/dist/index.d.cts +118 -21
  119. package/dist/index.d.ts +118 -21
  120. package/dist/index.js +1630 -47
  121. package/dist/index.js.map +1 -1
  122. package/dist/mention-text-CDnCFd3c.d.cts +53 -0
  123. package/dist/mention-text-CDnCFd3c.d.ts +53 -0
  124. package/dist/migrate-iTlm3a8h.d.cts +112 -0
  125. package/dist/migrate-iTlm3a8h.d.ts +112 -0
  126. package/dist/model-C1zApQM8.d.cts +219 -0
  127. package/dist/model-C1zApQM8.d.ts +219 -0
  128. package/dist/persistence/index.cjs +676 -0
  129. package/dist/persistence/index.cjs.map +1 -0
  130. package/dist/persistence/index.d.cts +230 -0
  131. package/dist/persistence/index.d.ts +230 -0
  132. package/dist/persistence/index.js +676 -0
  133. package/dist/persistence/index.js.map +1 -0
  134. package/dist/{registry-D5-R90p0.d.cts → registry-BDe-vlv3.d.ts} +2 -40
  135. package/dist/{registry-GJI4gToU.d.ts → registry-CEgwXED1.d.cts} +12 -1
  136. package/dist/{registry-GJI4gToU.d.cts → registry-CEgwXED1.d.ts} +12 -1
  137. package/dist/registry-D32LpIc4.d.cts +354 -0
  138. package/dist/{registry-D5-R90p0.d.ts → registry-DnzpgY27.d.cts} +2 -40
  139. package/dist/registry-evKTP4ls.d.ts +354 -0
  140. package/dist/schema-DYDGdYL9.d.cts +41 -0
  141. package/dist/schema-DYDGdYL9.d.ts +41 -0
  142. package/dist/serializers/index.cjs +12 -3
  143. package/dist/serializers/index.cjs.map +1 -1
  144. package/dist/serializers/index.d.cts +40 -12
  145. package/dist/serializers/index.d.ts +40 -12
  146. package/dist/serializers/index.js +12 -3
  147. package/dist/sourcemap-C8AH3MBQ.d.cts +91 -0
  148. package/dist/sourcemap-DA1Evhrv.d.ts +91 -0
  149. package/dist/spreadsheet/index.cjs +161 -3
  150. package/dist/spreadsheet/index.cjs.map +1 -1
  151. package/dist/spreadsheet/index.d.cts +611 -20
  152. package/dist/spreadsheet/index.d.ts +611 -20
  153. package/dist/spreadsheet/index.js +168 -10
  154. package/dist/storage-BpFfspHm.d.cts +120 -0
  155. package/dist/storage-BpFfspHm.d.ts +120 -0
  156. package/dist/tokens/index.cjs +115 -0
  157. package/dist/tokens/index.cjs.map +1 -0
  158. package/dist/tokens/index.d.cts +212 -0
  159. package/dist/tokens/index.d.ts +212 -0
  160. package/dist/tokens/index.js +115 -0
  161. package/dist/tokens/index.js.map +1 -0
  162. package/dist/{tokens-CyLZmIei.d.cts → tokens-D7yXvUU6.d.cts} +15 -2
  163. package/dist/{tokens-CyLZmIei.d.ts → tokens-D7yXvUU6.d.ts} +15 -2
  164. package/dist/{track-changes-BSF58RK0.d.cts → track-changes-D_VAPELi.d.cts} +110 -144
  165. package/dist/{track-changes-BSF58RK0.d.ts → track-changes-Db_fGEvx.d.ts} +110 -144
  166. package/package.json +21 -1
  167. package/dist/chunk-4SUNJZWB.cjs.map +0 -1
  168. package/dist/chunk-AA4ZEVCC.cjs.map +0 -1
  169. package/dist/chunk-AXV2ZW34.cjs +0 -118
  170. package/dist/chunk-AXV2ZW34.cjs.map +0 -1
  171. package/dist/chunk-B2ZRLN4S.js +0 -118
  172. package/dist/chunk-B2ZRLN4S.js.map +0 -1
  173. package/dist/chunk-GVSMRPHB.cjs.map +0 -1
  174. package/dist/chunk-K3R5NG2D.js +0 -3100
  175. package/dist/chunk-K3R5NG2D.js.map +0 -1
  176. package/dist/chunk-MJZBNWG7.js.map +0 -1
  177. package/dist/chunk-OYXSKQFR.js.map +0 -1
  178. package/dist/chunk-QBJ22YZR.js.map +0 -1
  179. package/dist/chunk-QGHGJ56O.js.map +0 -1
  180. package/dist/chunk-RIKBJLS2.cjs.map +0 -1
  181. package/dist/chunk-XDMXXC4Z.cjs +0 -3100
  182. package/dist/chunk-XDMXXC4Z.cjs.map +0 -1
  183. package/dist/controller-pePCJLT1.d.cts +0 -959
  184. package/dist/controller-pePCJLT1.d.ts +0 -959
  185. package/dist/index-CCr_z9kF.d.cts +0 -899
  186. package/dist/index-Ck6ajwXh.d.ts +0 -899
  187. /package/dist/{chunk-JJDLPEWL.js.map → chunk-QGYDWPRM.js.map} +0 -0
@@ -0,0 +1,689 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
+
3
+
4
+
5
+
6
+ var _chunkFD4LPFAEcjs = require('./chunk-FD4LPFAE.cjs');
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+ var _chunkMKA3AHLVcjs = require('./chunk-MKA3AHLV.cjs');
16
+
17
+
18
+ var _chunkTNNJXF46cjs = require('./chunk-TNNJXF46.cjs');
19
+
20
+ // src/grammar/resolver.ts
21
+ function resolveTemplates(text, templates = [], options) {
22
+ return _chunkFD4LPFAEcjs.resolvePrompt.call(void 0, text, templates, options).text;
23
+ }
24
+ function resolveVariables(text, values) {
25
+ if (!text) return "";
26
+ return _chunkFD4LPFAEcjs.resolvePrompt.call(void 0, text, [], { values }).text;
27
+ }
28
+ function extractTemplateNames(text) {
29
+ if (!text) return [];
30
+ const set = /* @__PURE__ */ new Set();
31
+ const regex = new RegExp(_chunkMKA3AHLVcjs.TEMPLATE_REGEX.source, _chunkMKA3AHLVcjs.TEMPLATE_REGEX.flags);
32
+ let match;
33
+ while ((match = regex.exec(text)) !== null) {
34
+ set.add(match[1].trim());
35
+ }
36
+ return Array.from(set);
37
+ }
38
+ function extractVariableNames(text) {
39
+ if (!text) return [];
40
+ const set = /* @__PURE__ */ new Set();
41
+ const regex = new RegExp(_chunkMKA3AHLVcjs.VARIABLE_REGEX.source, _chunkMKA3AHLVcjs.VARIABLE_REGEX.flags);
42
+ let comparisonStarts;
43
+ let match;
44
+ while ((match = regex.exec(text)) !== null) {
45
+ const parsed = _chunkMKA3AHLVcjs.parseVariableBody.call(void 0, match[1]);
46
+ let name = parsed.name;
47
+ if (parsed.kind === "conditional" && parsed.conditional !== "end") {
48
+ const subject = _chunkMKA3AHLVcjs.conditionSubject.call(void 0, name);
49
+ if (subject) {
50
+ comparisonStarts ??= new Set(
51
+ _chunkFD4LPFAEcjs.tokenizePrompt.call(void 0, text).filter((token) => token.comparison).map((token) => token.start)
52
+ );
53
+ if (comparisonStarts.has(match.index)) name = subject;
54
+ }
55
+ }
56
+ set.add(name);
57
+ }
58
+ return Array.from(set);
59
+ }
60
+ function extractInstructionNames(text) {
61
+ if (!text) return [];
62
+ const set = /* @__PURE__ */ new Set();
63
+ const regex = new RegExp(_chunkMKA3AHLVcjs.INSTRUCTION_REGEX.source, _chunkMKA3AHLVcjs.INSTRUCTION_REGEX.flags);
64
+ let match;
65
+ while ((match = regex.exec(text)) !== null) {
66
+ set.add(match[1].trim());
67
+ }
68
+ return Array.from(set);
69
+ }
70
+
71
+ // src/grammar/sourcemap.ts
72
+ function resolveTemplatesWithMapping(text, templates = [], templateBindings, options) {
73
+ if (_optionalChain([options, 'optionalAccess', _2 => _2.values]) === void 0 && (!templates || templates.length === 0 || !text.includes("{{"))) {
74
+ return {
75
+ diagnostics: _chunkFD4LPFAEcjs.getPromptDiagnostics.call(void 0, text),
76
+ text,
77
+ map: [
78
+ {
79
+ resolvedStart: 0,
80
+ resolvedEnd: text.length,
81
+ source: "raw",
82
+ rawStart: 0,
83
+ rawEnd: text.length
84
+ }
85
+ ]
86
+ };
87
+ }
88
+ return _chunkFD4LPFAEcjs.resolvePrompt.call(void 0, text, templates, {
89
+ bindings: templateBindings,
90
+ maxDepth: _optionalChain([options, 'optionalAccess', _3 => _3.maxDepth]),
91
+ values: _optionalChain([options, 'optionalAccess', _4 => _4.values])
92
+ });
93
+ }
94
+ function mapResolvedOffsetToRaw(resolvedOffset, map) {
95
+ for (let i = 0; i < map.length; i++) {
96
+ const seg = map[i];
97
+ const isLast = i === map.length - 1;
98
+ const inRange = isLast ? resolvedOffset >= seg.resolvedStart && resolvedOffset <= seg.resolvedEnd : resolvedOffset >= seg.resolvedStart && resolvedOffset < seg.resolvedEnd;
99
+ if (inRange) {
100
+ if (seg.source === "raw") {
101
+ return seg.rawStart + (resolvedOffset - seg.resolvedStart);
102
+ }
103
+ if (resolvedOffset === seg.resolvedEnd) {
104
+ return seg.rawEnd;
105
+ }
106
+ return seg.rawStart;
107
+ }
108
+ }
109
+ return resolvedOffset;
110
+ }
111
+ function mapRawOffsetToResolved(rawOffset, map) {
112
+ for (let i = 0; i < map.length; i++) {
113
+ const seg = map[i];
114
+ const isLast = i === map.length - 1;
115
+ const inRange = isLast ? rawOffset >= seg.rawStart && rawOffset <= seg.rawEnd : rawOffset >= seg.rawStart && rawOffset < seg.rawEnd;
116
+ if (inRange) {
117
+ if (seg.source === "raw") {
118
+ return seg.resolvedStart + (rawOffset - seg.rawStart);
119
+ }
120
+ if (rawOffset === seg.rawEnd) {
121
+ return seg.resolvedEnd;
122
+ }
123
+ return seg.resolvedStart;
124
+ }
125
+ }
126
+ return rawOffset;
127
+ }
128
+
129
+ // src/grammar/fields.ts
130
+ function extractVariables(text, options = {}) {
131
+ const descriptors = /* @__PURE__ */ new Map();
132
+ const templateMap = new Map(
133
+ (_nullishCoalesce(options.templates, () => ( []))).map((t) => [t.name, t])
134
+ );
135
+ const maxDepth = _nullishCoalesce(options.maxDepth, () => ( 10));
136
+ const describe = (name, templateName) => {
137
+ let descriptor = descriptors.get(name);
138
+ if (!descriptor) {
139
+ descriptor = {
140
+ name,
141
+ usedAsValue: false,
142
+ usedInCondition: false,
143
+ occurrences: 0,
144
+ templates: []
145
+ };
146
+ descriptors.set(name, descriptor);
147
+ }
148
+ descriptor.occurrences++;
149
+ if (templateName && !descriptor.templates.includes(templateName)) {
150
+ descriptor.templates.push(templateName);
151
+ }
152
+ return descriptor;
153
+ };
154
+ const visit = (source, visited, depth, templateName) => {
155
+ for (const token of _chunkFD4LPFAEcjs.tokenizePrompt.call(void 0, source)) {
156
+ if (token.type === "variable") {
157
+ const descriptor = describe(token.value, templateName);
158
+ descriptor.usedAsValue = true;
159
+ if (descriptor.defaultValue === void 0 && token.defaultValue !== void 0) {
160
+ descriptor.defaultValue = token.defaultValue;
161
+ }
162
+ if (descriptor.description === void 0 && token.description !== void 0) {
163
+ descriptor.description = token.description;
164
+ }
165
+ } else if (token.type === "conditional" && token.conditional !== "end") {
166
+ const descriptor = describe(token.value, templateName);
167
+ descriptor.usedInCondition = true;
168
+ if (token.comparison) descriptor.usedInComparison = true;
169
+ } else if (token.type === "template") {
170
+ const template = templateMap.get(token.value);
171
+ if (!template || visited.has(token.value) || depth + 1 > maxDepth) {
172
+ continue;
173
+ }
174
+ const body = _chunkFD4LPFAEcjs.getTemplateBody.call(void 0, template, options.bindings);
175
+ if (body === void 0) continue;
176
+ visit(body, new Set(visited).add(token.value), depth + 1, token.value);
177
+ }
178
+ }
179
+ };
180
+ if (text) visit(text, /* @__PURE__ */ new Set(), 0);
181
+ return Array.from(descriptors.values());
182
+ }
183
+ function getVariableFormFields(text, options = {}) {
184
+ return extractVariables(text, options).map((variable) => ({
185
+ name: variable.name,
186
+ label: variable.description || variable.name,
187
+ placeholder: _nullishCoalesce(variable.defaultValue, () => ( "")),
188
+ ...variable.defaultValue !== void 0 ? { defaultValue: variable.defaultValue } : {},
189
+ ...variable.description !== void 0 ? { description: variable.description } : {},
190
+ required: variable.usedAsValue && variable.defaultValue === void 0,
191
+ conditionOnly: !variable.usedAsValue && !variable.usedInComparison
192
+ }));
193
+ }
194
+ function compactVariableValues(input) {
195
+ const values = {};
196
+ for (const [name, value] of Object.entries(input)) {
197
+ if (value !== null && value !== void 0 && value.trim() !== "") {
198
+ values[name] = value;
199
+ }
200
+ }
201
+ return values;
202
+ }
203
+
204
+ // src/grammar/data-source.ts
205
+ var MAX_DATA_SOURCE_RECORDS = 1e5;
206
+ var MAX_DATA_SOURCE_FIELDS = 1e3;
207
+ var AUTO_DELIMITERS = [",", ";", " "];
208
+ function detectDelimiter(text) {
209
+ const counts = new Map(AUTO_DELIMITERS.map((d) => [d, 0]));
210
+ let quoted = false;
211
+ for (let i = 0; i < text.length; i++) {
212
+ const ch = text[i];
213
+ if (ch === '"') {
214
+ quoted = !quoted;
215
+ continue;
216
+ }
217
+ if (quoted) continue;
218
+ if (ch === "\n" || ch === "\r") break;
219
+ if (counts.has(ch)) counts.set(ch, counts.get(ch) + 1);
220
+ }
221
+ let best = ",";
222
+ for (const d of AUTO_DELIMITERS) {
223
+ if (counts.get(d) > counts.get(best)) best = d;
224
+ }
225
+ return best;
226
+ }
227
+ function normalizeFieldNames(row) {
228
+ const out = [];
229
+ const taken = /* @__PURE__ */ new Set();
230
+ row.forEach((cell, i) => {
231
+ const trimmed = cellText(cell).trim();
232
+ const base = trimmed === "" ? `Column ${i + 1}` : trimmed;
233
+ let name = base;
234
+ for (let k = 2; taken.has(name.toLowerCase()); k++) name = `${base} ${k}`;
235
+ taken.add(name.toLowerCase());
236
+ out.push(name);
237
+ });
238
+ return out;
239
+ }
240
+ function cellText(value) {
241
+ if (value === null || value === void 0) return "";
242
+ if (typeof value === "string") return value;
243
+ if (typeof value === "object") {
244
+ try {
245
+ return JSON.stringify(value);
246
+ } catch (e) {
247
+ return String(value);
248
+ }
249
+ }
250
+ return String(value);
251
+ }
252
+ function setField(record, name, value) {
253
+ Object.defineProperty(record, name, {
254
+ value,
255
+ enumerable: true,
256
+ writable: true,
257
+ configurable: true
258
+ });
259
+ }
260
+ function isBlankRow(row) {
261
+ return row.every((cell) => cellText(cell).trim() === "");
262
+ }
263
+ function parseDataSource(input, options = {}) {
264
+ const warnings = [];
265
+ if (typeof input === "string") {
266
+ const format = _nullishCoalesce(options.format, () => ( "auto"));
267
+ const trimmed = input.trim();
268
+ if (format === "json" || format === "auto" && /^\[/.test(trimmed)) {
269
+ let parsed;
270
+ let ok = true;
271
+ try {
272
+ parsed = JSON.parse(trimmed);
273
+ } catch (error) {
274
+ ok = false;
275
+ if (format === "json") {
276
+ warnings.push({
277
+ code: "invalid-json",
278
+ message: `Invalid JSON: ${error.message}`
279
+ });
280
+ return { fields: [], records: [], warnings, format: "json" };
281
+ }
282
+ }
283
+ if (ok) {
284
+ if (!Array.isArray(parsed)) {
285
+ warnings.push({
286
+ code: "unsupported-json",
287
+ message: "JSON data must be an array of objects or of arrays"
288
+ });
289
+ return { fields: [], records: [], warnings, format: "json" };
290
+ }
291
+ const result2 = fromArray(parsed, options, warnings);
292
+ return { ...result2, format: "json" };
293
+ }
294
+ }
295
+ const delimiter = !options.delimiter || options.delimiter === "auto" ? detectDelimiter(input) : options.delimiter;
296
+ let rows;
297
+ try {
298
+ rows = _chunkTNNJXF46cjs.parseCsv.call(void 0, input, { delimiter });
299
+ } catch (error) {
300
+ warnings.push({ code: "empty", message: error.message });
301
+ return { fields: [], records: [], warnings, format: "csv", delimiter };
302
+ }
303
+ return {
304
+ ...fromRows(rows, options, warnings),
305
+ format: "csv",
306
+ delimiter
307
+ };
308
+ }
309
+ const result = fromArray(input, options, warnings);
310
+ return {
311
+ ...result,
312
+ format: result.objects ? "objects" : "rows"
313
+ };
314
+ }
315
+ function fromArray(items, options, warnings) {
316
+ const objects = items.some(
317
+ (item) => item !== null && typeof item === "object" && !Array.isArray(item)
318
+ );
319
+ if (!objects) {
320
+ const rows2 = items.filter((item) => Array.isArray(item));
321
+ return { ...fromRows(rows2, options, warnings), objects: false };
322
+ }
323
+ const maxFields = _nullishCoalesce(options.maxFields, () => ( MAX_DATA_SOURCE_FIELDS));
324
+ const maxRecords = _nullishCoalesce(options.maxRecords, () => ( MAX_DATA_SOURCE_RECORDS));
325
+ const fields = [];
326
+ const seen = /* @__PURE__ */ new Set();
327
+ let fieldsTruncated = false;
328
+ const rows = items.filter(
329
+ (item) => item !== null && typeof item === "object" && !Array.isArray(item)
330
+ );
331
+ for (const row of rows) {
332
+ for (const key of Object.keys(row)) {
333
+ if (seen.has(key)) continue;
334
+ if (fields.length >= maxFields) {
335
+ fieldsTruncated = true;
336
+ continue;
337
+ }
338
+ seen.add(key);
339
+ fields.push(key);
340
+ }
341
+ }
342
+ if (fieldsTruncated) {
343
+ warnings.push({
344
+ code: "fields-truncated",
345
+ message: `Only the first ${maxFields} fields were kept`
346
+ });
347
+ }
348
+ const records = [];
349
+ for (const row of rows) {
350
+ if (fields.every((field) => cellText(row[field]).trim() === "")) continue;
351
+ if (records.length >= maxRecords) {
352
+ warnings.push({
353
+ code: "records-truncated",
354
+ message: `Only the first ${maxRecords} records were kept`
355
+ });
356
+ break;
357
+ }
358
+ const record = {};
359
+ for (const field of fields) {
360
+ const value = Object.prototype.hasOwnProperty.call(row, field) ? row[field] : void 0;
361
+ setField(record, field, cellText(value));
362
+ }
363
+ records.push(record);
364
+ }
365
+ if (records.length === 0) {
366
+ warnings.push({ code: "empty", message: "The data source has no records" });
367
+ }
368
+ return { fields, records, warnings, objects: true };
369
+ }
370
+ function fromRows(allRows, options, warnings) {
371
+ const maxFields = _nullishCoalesce(options.maxFields, () => ( MAX_DATA_SOURCE_FIELDS));
372
+ const maxRecords = _nullishCoalesce(options.maxRecords, () => ( MAX_DATA_SOURCE_RECORDS));
373
+ const rows = allRows.filter((row) => !isBlankRow(row));
374
+ if (rows.length === 0) {
375
+ warnings.push({ code: "empty", message: "The data source has no records" });
376
+ return { fields: [], records: [], warnings };
377
+ }
378
+ const hasHeader = _nullishCoalesce(options.hasHeader, () => ( true));
379
+ let fields;
380
+ let body;
381
+ let widest;
382
+ if (hasHeader) {
383
+ const header = rows[0];
384
+ widest = header.length;
385
+ if (header.some((cell) => cellText(cell).trim() === "")) {
386
+ warnings.push({
387
+ code: "blank-header",
388
+ message: 'Blank column headers were named "Column N"'
389
+ });
390
+ }
391
+ fields = normalizeFieldNames(header.slice(0, maxFields));
392
+ const lower = header.map((cell) => cellText(cell).trim().toLowerCase());
393
+ if (lower.some((name, i) => name !== "" && lower.indexOf(name) !== i)) {
394
+ warnings.push({
395
+ code: "duplicate-header",
396
+ message: "Repeated column headers were numbered"
397
+ });
398
+ }
399
+ body = rows.slice(1);
400
+ } else {
401
+ widest = Math.max(...rows.map((row) => row.length));
402
+ fields = Array.from(
403
+ { length: Math.min(widest, maxFields) },
404
+ (_, i) => `Column ${i + 1}`
405
+ );
406
+ body = rows;
407
+ }
408
+ if (widest > maxFields) {
409
+ warnings.push({
410
+ code: "fields-truncated",
411
+ message: `Only the first ${maxFields} fields were kept`
412
+ });
413
+ }
414
+ if (body.length > maxRecords) {
415
+ warnings.push({
416
+ code: "records-truncated",
417
+ message: `Only the first ${maxRecords} of ${body.length} records were kept`
418
+ });
419
+ body = body.slice(0, maxRecords);
420
+ }
421
+ let ragged = 0;
422
+ const records = body.map((row) => {
423
+ if (hasHeader && row.length !== widest) ragged++;
424
+ const record = {};
425
+ fields.forEach((field, i) => setField(record, field, cellText(row[i])));
426
+ return record;
427
+ });
428
+ if (ragged > 0) {
429
+ warnings.push({
430
+ code: "ragged-rows",
431
+ message: `${ragged} record${ragged === 1 ? " was" : "s were"} padded or cut to ${widest} fields`,
432
+ count: ragged
433
+ });
434
+ }
435
+ if (records.length === 0) {
436
+ warnings.push({ code: "empty", message: "The data source has no records" });
437
+ }
438
+ return { fields, records, warnings };
439
+ }
440
+ function normalizeFieldKey(name) {
441
+ return name.toLowerCase().replace(/[\s_.\-]+/g, "");
442
+ }
443
+ var FIELD_ALIASES = [
444
+ ["FirstName", "First", "GivenName", "Forename", "FName"],
445
+ ["LastName", "Last", "Surname", "FamilyName", "LName"],
446
+ ["Name", "FullName", "DisplayName"],
447
+ ["Company", "CompanyName", "Organization", "Organisation", "Employer", "Business"],
448
+ ["Address", "AddressLine1", "Address1", "Street", "StreetAddress"],
449
+ ["City", "Town"],
450
+ ["State", "Province", "Region", "County"],
451
+ ["PostalCode", "ZIP", "ZipCode", "Postcode"],
452
+ ["Country", "CountryName", "Nation"],
453
+ ["Title", "CourtesyTitle", "Salutation", "Honorific"],
454
+ ["Email", "EmailAddress", "Mail", "EMail"],
455
+ ["Phone", "PhoneNumber", "Telephone", "Tel", "Mobile"]
456
+ ];
457
+ function matchFields(variables, fields, options = {}) {
458
+ const names = Array.from(
459
+ new Set(variables.map((v) => typeof v === "string" ? v : v.name))
460
+ );
461
+ const byKey = /* @__PURE__ */ new Map();
462
+ for (const field of fields) {
463
+ const key = normalizeFieldKey(field);
464
+ if (!byKey.has(key)) byKey.set(key, field);
465
+ }
466
+ const groups = [...FIELD_ALIASES, ..._nullishCoalesce(options.aliases, () => ( []))].map(
467
+ (group) => group.map(normalizeFieldKey)
468
+ );
469
+ const matches = names.map((variable) => {
470
+ if (fields.includes(variable)) {
471
+ return { variable, field: variable, kind: "exact" };
472
+ }
473
+ const key = normalizeFieldKey(variable);
474
+ const normalized = byKey.get(key);
475
+ if (normalized !== void 0) {
476
+ return { variable, field: normalized, kind: "normalized" };
477
+ }
478
+ for (const group of groups) {
479
+ if (!group.includes(key)) continue;
480
+ for (const alias of group) {
481
+ const field = byKey.get(alias);
482
+ if (field !== void 0) return { variable, field, kind: "alias" };
483
+ }
484
+ }
485
+ return { variable, field: null, kind: null };
486
+ });
487
+ const mapping = {};
488
+ for (const match of matches) {
489
+ Object.defineProperty(mapping, match.variable, {
490
+ value: match.field,
491
+ enumerable: true,
492
+ writable: true,
493
+ configurable: true
494
+ });
495
+ }
496
+ const used = new Set(matches.map((m) => m.field));
497
+ return {
498
+ mapping,
499
+ matches,
500
+ unmatchedVariables: matches.filter((m) => !m.field).map((m) => m.variable),
501
+ unusedFields: fields.filter((field) => !used.has(field))
502
+ };
503
+ }
504
+ function recordToValues(record, mapping = {}, options = {}) {
505
+ const blankAsMissing = _nullishCoalesce(options.blankAsMissing, () => ( true));
506
+ const values = {};
507
+ const assign = (name, value) => {
508
+ if (value === void 0 || blankAsMissing && value.trim() === "") {
509
+ delete values[name];
510
+ return;
511
+ }
512
+ Object.defineProperty(values, name, {
513
+ value,
514
+ enumerable: true,
515
+ writable: true,
516
+ configurable: true
517
+ });
518
+ };
519
+ const own = (name) => Object.prototype.hasOwnProperty.call(record, name) ? record[name] : void 0;
520
+ for (const field of Object.keys(record)) assign(field, record[field]);
521
+ for (const variable of Object.keys(mapping)) {
522
+ const field = mapping[variable];
523
+ if (field === void 0) continue;
524
+ assign(variable, field === null ? void 0 : own(field));
525
+ }
526
+ return values;
527
+ }
528
+
529
+ // src/grammar/merge.ts
530
+ var MAX_MERGE_OUTPUTS = 1e4;
531
+ var MAX_MERGE_LENGTH = 2e7;
532
+ function isParsedSource(source) {
533
+ return !!source && typeof source === "object" && !Array.isArray(source) && Array.isArray(source.fields) && Array.isArray(source.records);
534
+ }
535
+ function mergeRecords(template, source, options = {}) {
536
+ const data = isParsedSource(source) ? source : parseDataSource(source, options.parse);
537
+ const diagnostics = [];
538
+ if (!isParsedSource(source)) {
539
+ for (const warning of data.warnings) {
540
+ if (warning.code === "empty") continue;
541
+ diagnostics.push({ code: "data-source", message: warning.message });
542
+ }
543
+ }
544
+ const variables = extractVariables(template, {
545
+ templates: options.templates,
546
+ bindings: options.bindings,
547
+ maxDepth: options.maxDepth
548
+ });
549
+ const auto = matchFields(variables, data.fields).mapping;
550
+ const mapping = { ...auto };
551
+ for (const [variable, field] of Object.entries(_nullishCoalesce(options.mapping, () => ( {})))) {
552
+ Object.defineProperty(mapping, variable, {
553
+ value: field,
554
+ enumerable: true,
555
+ writable: true,
556
+ configurable: true
557
+ });
558
+ }
559
+ for (const variable of variables) {
560
+ const field = mapping[variable.name];
561
+ if ((field === null || field === void 0) && variable.defaultValue === void 0 && variable.usedAsValue) {
562
+ diagnostics.push({
563
+ code: "unmapped-variable",
564
+ message: `No field is mapped to "${variable.name}", so it stays as a placeholder`,
565
+ name: variable.name
566
+ });
567
+ }
568
+ }
569
+ let skip;
570
+ if (typeof options.skipIf === "function") {
571
+ skip = options.skipIf;
572
+ } else if (typeof options.skipIf === "string" && options.skipIf.trim()) {
573
+ const parsed = _chunkMKA3AHLVcjs.parseConditionExpression.call(void 0, options.skipIf);
574
+ if ("error" in parsed) {
575
+ diagnostics.push({
576
+ code: "invalid-skip-condition",
577
+ message: `Invalid skipIf condition "${options.skipIf}": ${parsed.error}`
578
+ });
579
+ } else {
580
+ const field = _nullishCoalesce(mapping[parsed.name], () => ( matchFields([parsed.name], data.fields).mapping[parsed.name]));
581
+ skip = (record, values) => {
582
+ if (field && !Object.prototype.hasOwnProperty.call(values, parsed.name) && Object.prototype.hasOwnProperty.call(record, field)) {
583
+ return _chunkMKA3AHLVcjs.evaluateConditionExpression.call(void 0, parsed, {
584
+ ...values,
585
+ [parsed.name]: record[field]
586
+ });
587
+ }
588
+ return _chunkMKA3AHLVcjs.evaluateConditionExpression.call(void 0, parsed, values);
589
+ };
590
+ }
591
+ }
592
+ const separator = options.separator === void 0 ? "\n\n" : options.separator === "none" ? "" : options.separator;
593
+ const maxOutputs = Math.max(0, _nullishCoalesce(options.maxOutputs, () => ( MAX_MERGE_OUTPUTS)));
594
+ const maxLength = Math.max(0, _nullishCoalesce(options.maxLength, () => ( MAX_MERGE_LENGTH)));
595
+ const first = Math.max(1, Math.floor(_nullishCoalesce(options.from, () => ( 1)))) - 1;
596
+ const last = Math.min(
597
+ data.records.length,
598
+ Math.floor(_nullishCoalesce(options.to, () => ( data.records.length)))
599
+ );
600
+ const outputs = [];
601
+ const skipped = [];
602
+ let length = 0;
603
+ let truncated = false;
604
+ let reportedGrammar = false;
605
+ for (let index = first; index < last; index++) {
606
+ const record = data.records[index];
607
+ const values = recordToValues(record, mapping, {
608
+ blankAsMissing: options.blankAsMissing
609
+ });
610
+ if (_optionalChain([skip, 'optionalCall', _5 => _5(record, values, index)])) {
611
+ skipped.push({ index, record, reason: "skip-if" });
612
+ continue;
613
+ }
614
+ if (outputs.length >= maxOutputs) {
615
+ truncated = true;
616
+ diagnostics.push({
617
+ code: "max-outputs",
618
+ message: `Stopped after ${maxOutputs} outputs`
619
+ });
620
+ break;
621
+ }
622
+ const resolved = _chunkFD4LPFAEcjs.resolvePrompt.call(void 0, template, options.templates, {
623
+ bindings: options.bindings,
624
+ maxDepth: options.maxDepth,
625
+ values
626
+ });
627
+ if (!reportedGrammar) {
628
+ reportedGrammar = true;
629
+ diagnostics.push(...resolved.diagnostics);
630
+ }
631
+ const added = resolved.text.length + (outputs.length > 0 ? separator.length : 0);
632
+ if (length + added > maxLength) {
633
+ truncated = true;
634
+ diagnostics.push({
635
+ code: "max-length",
636
+ message: `Stopped at ${outputs.length} outputs: the merged text would exceed ${maxLength} characters`
637
+ });
638
+ break;
639
+ }
640
+ length += added;
641
+ outputs.push({ index, record, values, text: resolved.text, map: resolved.map });
642
+ }
643
+ return {
644
+ outputs,
645
+ skipped,
646
+ diagnostics,
647
+ text: outputs.map((output) => output.text).join(separator),
648
+ mapping,
649
+ truncated
650
+ };
651
+ }
652
+ function serializeMergeOutputs(outputs, format = "text", separator = "\n\n") {
653
+ if (format === "json") {
654
+ return JSON.stringify(
655
+ outputs.map((output) => ({ record: output.index + 1, text: output.text })),
656
+ null,
657
+ 2
658
+ );
659
+ }
660
+ return outputs.map((output) => output.text).join(separator === "none" ? "" : separator);
661
+ }
662
+
663
+
664
+
665
+
666
+
667
+
668
+
669
+
670
+
671
+
672
+
673
+
674
+
675
+
676
+
677
+
678
+
679
+
680
+
681
+
682
+
683
+
684
+
685
+
686
+
687
+
688
+ exports.resolveTemplates = resolveTemplates; exports.resolveVariables = resolveVariables; exports.extractTemplateNames = extractTemplateNames; exports.extractVariableNames = extractVariableNames; exports.extractInstructionNames = extractInstructionNames; exports.resolveTemplatesWithMapping = resolveTemplatesWithMapping; exports.mapResolvedOffsetToRaw = mapResolvedOffsetToRaw; exports.mapRawOffsetToResolved = mapRawOffsetToResolved; exports.extractVariables = extractVariables; exports.getVariableFormFields = getVariableFormFields; exports.compactVariableValues = compactVariableValues; exports.MAX_DATA_SOURCE_RECORDS = MAX_DATA_SOURCE_RECORDS; exports.MAX_DATA_SOURCE_FIELDS = MAX_DATA_SOURCE_FIELDS; exports.detectDelimiter = detectDelimiter; exports.normalizeFieldNames = normalizeFieldNames; exports.parseDataSource = parseDataSource; exports.normalizeFieldKey = normalizeFieldKey; exports.FIELD_ALIASES = FIELD_ALIASES; exports.matchFields = matchFields; exports.recordToValues = recordToValues; exports.MAX_MERGE_OUTPUTS = MAX_MERGE_OUTPUTS; exports.MAX_MERGE_LENGTH = MAX_MERGE_LENGTH; exports.mergeRecords = mergeRecords; exports.serializeMergeOutputs = serializeMergeOutputs;
689
+ //# sourceMappingURL=chunk-P3RQ5UJE.cjs.map