prompt-identifiers-baml 0.1.1 → 0.1.2

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 (3) hide show
  1. package/dist/index.js +12 -106
  2. package/dist/index.mjs +13 -107
  3. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -70,94 +70,6 @@ function matchesFieldPath(currentPath, targetSegments) {
70
70
  }
71
71
  return true;
72
72
  }
73
- function getInputPattern(inputFormat) {
74
- if (inputFormat instanceof RegExp) {
75
- const flags = inputFormat.flags.includes("g") ? inputFormat.flags : inputFormat.flags + "g";
76
- return new RegExp(inputFormat.source, flags);
77
- }
78
- if (inputFormat === "UUID") {
79
- return /\b[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\b/gi;
80
- }
81
- return /\b[0-9A-HJKMNP-TV-Z]{26}\b/gi;
82
- }
83
- function formatPlaceholder(outputFormat, index) {
84
- if (outputFormat === "SafeNumeric") {
85
- const s = index.toString();
86
- const width = Math.max(3, Math.ceil(s.length / 3) * 3);
87
- return `~${s.padStart(width, "0")}~`;
88
- }
89
- if (outputFormat === "Numeric") {
90
- const s = index.toString();
91
- const width = Math.max(3, Math.ceil(s.length / 3) * 3);
92
- return s.padStart(width, "0");
93
- }
94
- if (outputFormat === "IdToken") {
95
- const BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
96
- if (index < 62) return BASE62[index];
97
- let result = "";
98
- let n = index;
99
- while (n > 0) {
100
- result = BASE62[n % 62] + result;
101
- n = Math.floor(n / 62);
102
- }
103
- return result;
104
- }
105
- if (outputFormat === "Passthrough") {
106
- return "";
107
- }
108
- if (typeof outputFormat === "function") {
109
- return outputFormat(index);
110
- }
111
- const template = outputFormat.template;
112
- const match = template.match(/\{i(?::([^}]+))?\}/);
113
- if (!match) return `${index}`;
114
- const specifier = match[1];
115
- let formatted;
116
- if (!specifier) {
117
- formatted = index.toString();
118
- } else if (specifier === "base62") {
119
- const BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
120
- if (index < 62) formatted = BASE62[index];
121
- else {
122
- formatted = "";
123
- let n = index;
124
- while (n > 0) {
125
- formatted = BASE62[n % 62] + formatted;
126
- n = Math.floor(n / 62);
127
- }
128
- }
129
- } else if (specifier === "zeroFilled") {
130
- const s = index.toString();
131
- const width = Math.max(3, Math.ceil(s.length / 3) * 3);
132
- formatted = s.padStart(width, "0");
133
- } else {
134
- const padMatch = specifier.match(/^(\d+)$/);
135
- if (padMatch) {
136
- formatted = index.toString().padStart(parseInt(padMatch[1], 10), "0");
137
- } else {
138
- formatted = index.toString();
139
- }
140
- }
141
- return template.replace(match[0], formatted);
142
- }
143
- function encodeStringWithContext(text, ctx) {
144
- if (ctx.config.outputFormat === "Passthrough") {
145
- return text;
146
- }
147
- const pattern = getInputPattern(ctx.config.inputFormat);
148
- pattern.lastIndex = 0;
149
- return text.replace(pattern, (match) => {
150
- const id = match.toLowerCase();
151
- if (ctx.idToPlaceholder.has(id)) {
152
- return ctx.idToPlaceholder.get(id);
153
- }
154
- const placeholder = formatPlaceholder(ctx.config.outputFormat, ctx.nextIndex);
155
- ctx.nextIndex++;
156
- ctx.idToPlaceholder.set(id, placeholder);
157
- ctx.mapping[placeholder] = id;
158
- return placeholder;
159
- });
160
- }
161
73
  function deepEncode(value, ctx, path = []) {
162
74
  if (value === null || value === void 0) {
163
75
  return value;
@@ -167,7 +79,7 @@ function deepEncode(value, ctx, path = []) {
167
79
  if (!shouldEncode) {
168
80
  return value;
169
81
  }
170
- return encodeStringWithContext(value, ctx);
82
+ return (0, import_prompt_identifiers.encode)(value, ctx.config, ctx.state).encoded;
171
83
  }
172
84
  if (Array.isArray(value)) {
173
85
  return value.map((item, index) => deepEncode(item, ctx, [...path, String(index)]));
@@ -211,18 +123,16 @@ function wrapBamlFunction(fn, options) {
211
123
  const ctx = {
212
124
  config,
213
125
  fieldPaths,
214
- idToPlaceholder: /* @__PURE__ */ new Map(),
215
- mapping: {},
216
- nextIndex: 0
126
+ state: (0, import_prompt_identifiers.createEncodeState)()
217
127
  };
218
128
  const startEncode = debug ? performance.now() : 0;
219
129
  const encodedInput = deepEncode(input, ctx);
220
130
  const encodeDurationMs = debug ? performance.now() - startEncode : 0;
221
131
  onEncode?.({
222
- mapping: ctx.mapping,
132
+ mapping: ctx.state.mapping,
223
133
  ...debug && {
224
134
  debugData: {
225
- encodedCount: Object.keys(ctx.mapping).length,
135
+ encodedCount: Object.keys(ctx.state.mapping).length,
226
136
  input,
227
137
  output: encodedInput,
228
138
  durationMs: encodeDurationMs
@@ -232,7 +142,7 @@ function wrapBamlFunction(fn, options) {
232
142
  const output = await fn(encodedInput);
233
143
  const countRef = { count: 0 };
234
144
  const startDecode = debug ? performance.now() : 0;
235
- const decodedOutput = deepDecode(output, ctx.mapping, countRef);
145
+ const decodedOutput = deepDecode(output, ctx.state.mapping, countRef);
236
146
  const decodeDurationMs = debug ? performance.now() - startDecode : 0;
237
147
  onDecode?.({
238
148
  ...debug && {
@@ -254,18 +164,16 @@ function wrapBamlStreamingFunction(fn, options) {
254
164
  const ctx = {
255
165
  config,
256
166
  fieldPaths,
257
- idToPlaceholder: /* @__PURE__ */ new Map(),
258
- mapping: {},
259
- nextIndex: 0
167
+ state: (0, import_prompt_identifiers.createEncodeState)()
260
168
  };
261
169
  const startEncode = debug ? performance.now() : 0;
262
170
  const encodedInput = deepEncode(input, ctx);
263
171
  const encodeDurationMs = debug ? performance.now() - startEncode : 0;
264
172
  onEncode?.({
265
- mapping: ctx.mapping,
173
+ mapping: ctx.state.mapping,
266
174
  ...debug && {
267
175
  debugData: {
268
- encodedCount: Object.keys(ctx.mapping).length,
176
+ encodedCount: Object.keys(ctx.state.mapping).length,
269
177
  input,
270
178
  output: encodedInput,
271
179
  durationMs: encodeDurationMs
@@ -279,7 +187,7 @@ function wrapBamlStreamingFunction(fn, options) {
279
187
  const { value, done } = await generator.next();
280
188
  if (done) {
281
189
  const countRef2 = { count: 0 };
282
- const decodedValue2 = deepDecode(value, ctx.mapping, countRef2);
190
+ const decodedValue2 = deepDecode(value, ctx.state.mapping, countRef2);
283
191
  totalDecoded += countRef2.count;
284
192
  const decodeDurationMs = debug ? performance.now() - startDecode : 0;
285
193
  onDecode?.({
@@ -295,7 +203,7 @@ function wrapBamlStreamingFunction(fn, options) {
295
203
  return decodedValue2;
296
204
  }
297
205
  const countRef = { count: 0 };
298
- const decodedValue = deepDecode(value, ctx.mapping, countRef);
206
+ const decodedValue = deepDecode(value, ctx.state.mapping, countRef);
299
207
  totalDecoded += countRef.count;
300
208
  yield decodedValue;
301
209
  }
@@ -306,12 +214,10 @@ function encodeObject(obj, config, encodeFields) {
306
214
  const ctx = {
307
215
  config,
308
216
  fieldPaths,
309
- idToPlaceholder: /* @__PURE__ */ new Map(),
310
- mapping: {},
311
- nextIndex: 0
217
+ state: (0, import_prompt_identifiers.createEncodeState)()
312
218
  };
313
219
  const encoded = deepEncode(obj, ctx);
314
- return { encoded, mapping: ctx.mapping };
220
+ return { encoded, mapping: ctx.state.mapping };
315
221
  }
316
222
  function decodeObject(obj, mapping) {
317
223
  const countRef = { count: 0 };
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/index.ts
2
- import { decode } from "prompt-identifiers";
2
+ import { createEncodeState, decode, encode } from "prompt-identifiers";
3
3
  function parseFieldPath(path) {
4
4
  const segments = [];
5
5
  let current = "";
@@ -43,94 +43,6 @@ function matchesFieldPath(currentPath, targetSegments) {
43
43
  }
44
44
  return true;
45
45
  }
46
- function getInputPattern(inputFormat) {
47
- if (inputFormat instanceof RegExp) {
48
- const flags = inputFormat.flags.includes("g") ? inputFormat.flags : inputFormat.flags + "g";
49
- return new RegExp(inputFormat.source, flags);
50
- }
51
- if (inputFormat === "UUID") {
52
- return /\b[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\b/gi;
53
- }
54
- return /\b[0-9A-HJKMNP-TV-Z]{26}\b/gi;
55
- }
56
- function formatPlaceholder(outputFormat, index) {
57
- if (outputFormat === "SafeNumeric") {
58
- const s = index.toString();
59
- const width = Math.max(3, Math.ceil(s.length / 3) * 3);
60
- return `~${s.padStart(width, "0")}~`;
61
- }
62
- if (outputFormat === "Numeric") {
63
- const s = index.toString();
64
- const width = Math.max(3, Math.ceil(s.length / 3) * 3);
65
- return s.padStart(width, "0");
66
- }
67
- if (outputFormat === "IdToken") {
68
- const BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
69
- if (index < 62) return BASE62[index];
70
- let result = "";
71
- let n = index;
72
- while (n > 0) {
73
- result = BASE62[n % 62] + result;
74
- n = Math.floor(n / 62);
75
- }
76
- return result;
77
- }
78
- if (outputFormat === "Passthrough") {
79
- return "";
80
- }
81
- if (typeof outputFormat === "function") {
82
- return outputFormat(index);
83
- }
84
- const template = outputFormat.template;
85
- const match = template.match(/\{i(?::([^}]+))?\}/);
86
- if (!match) return `${index}`;
87
- const specifier = match[1];
88
- let formatted;
89
- if (!specifier) {
90
- formatted = index.toString();
91
- } else if (specifier === "base62") {
92
- const BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
93
- if (index < 62) formatted = BASE62[index];
94
- else {
95
- formatted = "";
96
- let n = index;
97
- while (n > 0) {
98
- formatted = BASE62[n % 62] + formatted;
99
- n = Math.floor(n / 62);
100
- }
101
- }
102
- } else if (specifier === "zeroFilled") {
103
- const s = index.toString();
104
- const width = Math.max(3, Math.ceil(s.length / 3) * 3);
105
- formatted = s.padStart(width, "0");
106
- } else {
107
- const padMatch = specifier.match(/^(\d+)$/);
108
- if (padMatch) {
109
- formatted = index.toString().padStart(parseInt(padMatch[1], 10), "0");
110
- } else {
111
- formatted = index.toString();
112
- }
113
- }
114
- return template.replace(match[0], formatted);
115
- }
116
- function encodeStringWithContext(text, ctx) {
117
- if (ctx.config.outputFormat === "Passthrough") {
118
- return text;
119
- }
120
- const pattern = getInputPattern(ctx.config.inputFormat);
121
- pattern.lastIndex = 0;
122
- return text.replace(pattern, (match) => {
123
- const id = match.toLowerCase();
124
- if (ctx.idToPlaceholder.has(id)) {
125
- return ctx.idToPlaceholder.get(id);
126
- }
127
- const placeholder = formatPlaceholder(ctx.config.outputFormat, ctx.nextIndex);
128
- ctx.nextIndex++;
129
- ctx.idToPlaceholder.set(id, placeholder);
130
- ctx.mapping[placeholder] = id;
131
- return placeholder;
132
- });
133
- }
134
46
  function deepEncode(value, ctx, path = []) {
135
47
  if (value === null || value === void 0) {
136
48
  return value;
@@ -140,7 +52,7 @@ function deepEncode(value, ctx, path = []) {
140
52
  if (!shouldEncode) {
141
53
  return value;
142
54
  }
143
- return encodeStringWithContext(value, ctx);
55
+ return encode(value, ctx.config, ctx.state).encoded;
144
56
  }
145
57
  if (Array.isArray(value)) {
146
58
  return value.map((item, index) => deepEncode(item, ctx, [...path, String(index)]));
@@ -184,18 +96,16 @@ function wrapBamlFunction(fn, options) {
184
96
  const ctx = {
185
97
  config,
186
98
  fieldPaths,
187
- idToPlaceholder: /* @__PURE__ */ new Map(),
188
- mapping: {},
189
- nextIndex: 0
99
+ state: createEncodeState()
190
100
  };
191
101
  const startEncode = debug ? performance.now() : 0;
192
102
  const encodedInput = deepEncode(input, ctx);
193
103
  const encodeDurationMs = debug ? performance.now() - startEncode : 0;
194
104
  onEncode?.({
195
- mapping: ctx.mapping,
105
+ mapping: ctx.state.mapping,
196
106
  ...debug && {
197
107
  debugData: {
198
- encodedCount: Object.keys(ctx.mapping).length,
108
+ encodedCount: Object.keys(ctx.state.mapping).length,
199
109
  input,
200
110
  output: encodedInput,
201
111
  durationMs: encodeDurationMs
@@ -205,7 +115,7 @@ function wrapBamlFunction(fn, options) {
205
115
  const output = await fn(encodedInput);
206
116
  const countRef = { count: 0 };
207
117
  const startDecode = debug ? performance.now() : 0;
208
- const decodedOutput = deepDecode(output, ctx.mapping, countRef);
118
+ const decodedOutput = deepDecode(output, ctx.state.mapping, countRef);
209
119
  const decodeDurationMs = debug ? performance.now() - startDecode : 0;
210
120
  onDecode?.({
211
121
  ...debug && {
@@ -227,18 +137,16 @@ function wrapBamlStreamingFunction(fn, options) {
227
137
  const ctx = {
228
138
  config,
229
139
  fieldPaths,
230
- idToPlaceholder: /* @__PURE__ */ new Map(),
231
- mapping: {},
232
- nextIndex: 0
140
+ state: createEncodeState()
233
141
  };
234
142
  const startEncode = debug ? performance.now() : 0;
235
143
  const encodedInput = deepEncode(input, ctx);
236
144
  const encodeDurationMs = debug ? performance.now() - startEncode : 0;
237
145
  onEncode?.({
238
- mapping: ctx.mapping,
146
+ mapping: ctx.state.mapping,
239
147
  ...debug && {
240
148
  debugData: {
241
- encodedCount: Object.keys(ctx.mapping).length,
149
+ encodedCount: Object.keys(ctx.state.mapping).length,
242
150
  input,
243
151
  output: encodedInput,
244
152
  durationMs: encodeDurationMs
@@ -252,7 +160,7 @@ function wrapBamlStreamingFunction(fn, options) {
252
160
  const { value, done } = await generator.next();
253
161
  if (done) {
254
162
  const countRef2 = { count: 0 };
255
- const decodedValue2 = deepDecode(value, ctx.mapping, countRef2);
163
+ const decodedValue2 = deepDecode(value, ctx.state.mapping, countRef2);
256
164
  totalDecoded += countRef2.count;
257
165
  const decodeDurationMs = debug ? performance.now() - startDecode : 0;
258
166
  onDecode?.({
@@ -268,7 +176,7 @@ function wrapBamlStreamingFunction(fn, options) {
268
176
  return decodedValue2;
269
177
  }
270
178
  const countRef = { count: 0 };
271
- const decodedValue = deepDecode(value, ctx.mapping, countRef);
179
+ const decodedValue = deepDecode(value, ctx.state.mapping, countRef);
272
180
  totalDecoded += countRef.count;
273
181
  yield decodedValue;
274
182
  }
@@ -279,12 +187,10 @@ function encodeObject(obj, config, encodeFields) {
279
187
  const ctx = {
280
188
  config,
281
189
  fieldPaths,
282
- idToPlaceholder: /* @__PURE__ */ new Map(),
283
- mapping: {},
284
- nextIndex: 0
190
+ state: createEncodeState()
285
191
  };
286
192
  const encoded = deepEncode(obj, ctx);
287
- return { encoded, mapping: ctx.mapping };
193
+ return { encoded, mapping: ctx.state.mapping };
288
194
  }
289
195
  function decodeObject(obj, mapping) {
290
196
  const countRef = { count: 0 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prompt-identifiers-baml",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "BAML integration for prompt-identifiers. Efficient ID compression for token optimization.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -39,7 +39,7 @@
39
39
  "url": "https://github.com/fogx/prompt-identifiers"
40
40
  },
41
41
  "peerDependencies": {
42
- "prompt-identifiers": ">=0.1.0",
42
+ "prompt-identifiers": ">=0.1.2",
43
43
  "@boundaryml/baml": ">=0.70.0"
44
44
  },
45
45
  "peerDependenciesMeta": {
@@ -54,7 +54,7 @@
54
54
  "ts-jest": "^29.0.0",
55
55
  "tsup": "^8.5.1",
56
56
  "typescript": "^5.0.0",
57
- "prompt-identifiers": "0.1.1"
57
+ "prompt-identifiers": "0.1.2"
58
58
  },
59
59
  "scripts": {
60
60
  "build": "tsup src/index.ts --format cjs,esm --dts --clean",