gheim 0.1.3 → 0.1.4

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.
@@ -155,7 +155,9 @@ class Session {
155
155
  cursor = end;
156
156
  }
157
157
  out.push(text.slice(cursor));
158
- return { redacted: out.join(""), merged };
158
+ let redacted = out.join("");
159
+ redacted = recoverDuplicateOccurrences(redacted, this.mapping);
160
+ return { redacted, merged };
159
161
  }
160
162
  restore(text) {
161
163
  if (Object.keys(this.mapping).length === 0)
@@ -211,6 +213,18 @@ function mergeAdjacent(text, spans) {
211
213
  }
212
214
  return out;
213
215
  }
216
+ function escapeRegex(s) {
217
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
218
+ }
219
+ function recoverDuplicateOccurrences(redacted, mapping) {
220
+ const entries = Object.entries(mapping).filter(([, surface]) => surface && surface.length > 0).sort((a, b) => b[1].length - a[1].length);
221
+ let result = redacted;
222
+ for (const [sentinel, surface] of entries) {
223
+ const pattern = new RegExp(`(?<![\\p{L}\\p{N}_])${escapeRegex(surface)}(?![\\p{L}\\p{N}_])`, "gu");
224
+ result = result.replace(pattern, sentinel);
225
+ }
226
+ return result;
227
+ }
214
228
  // src/core/stream.ts
215
229
  var SENTINEL_AT = /<([A-Z][A-Z0-9_]*)_(\d+)>/y;
216
230
  var MAX_SENTINEL_LEN = 64;
@@ -882,4 +896,4 @@ async function* deanonymizeStream(chunks, session) {
882
896
  }
883
897
 
884
898
  // src/index.ts
885
- var VERSION = "0.1.3";
899
+ var VERSION = "0.1.4";
@@ -814,7 +814,9 @@ class Session {
814
814
  cursor = end;
815
815
  }
816
816
  out.push(text.slice(cursor));
817
- return { redacted: out.join(""), merged };
817
+ let redacted = out.join("");
818
+ redacted = recoverDuplicateOccurrences(redacted, this.mapping);
819
+ return { redacted, merged };
818
820
  }
819
821
  restore(text) {
820
822
  if (Object.keys(this.mapping).length === 0)
@@ -870,6 +872,18 @@ function mergeAdjacent(text, spans) {
870
872
  }
871
873
  return out;
872
874
  }
875
+ function escapeRegex(s) {
876
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
877
+ }
878
+ function recoverDuplicateOccurrences(redacted, mapping) {
879
+ const entries = Object.entries(mapping).filter(([, surface]) => surface && surface.length > 0).sort((a, b) => b[1].length - a[1].length);
880
+ let result = redacted;
881
+ for (const [sentinel, surface] of entries) {
882
+ const pattern = new RegExp(`(?<![\\p{L}\\p{N}_])${escapeRegex(surface)}(?![\\p{L}\\p{N}_])`, "gu");
883
+ result = result.replace(pattern, sentinel);
884
+ }
885
+ return result;
886
+ }
873
887
 
874
888
  // src/openai/_base.ts
875
889
  function resolveCallSession(body, defaultDet) {
package/dist/esm/index.js CHANGED
@@ -99,7 +99,9 @@ class Session {
99
99
  cursor = end;
100
100
  }
101
101
  out.push(text.slice(cursor));
102
- return { redacted: out.join(""), merged };
102
+ let redacted = out.join("");
103
+ redacted = recoverDuplicateOccurrences(redacted, this.mapping);
104
+ return { redacted, merged };
103
105
  }
104
106
  restore(text) {
105
107
  if (Object.keys(this.mapping).length === 0)
@@ -155,6 +157,18 @@ function mergeAdjacent(text, spans) {
155
157
  }
156
158
  return out;
157
159
  }
160
+ function escapeRegex(s) {
161
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
162
+ }
163
+ function recoverDuplicateOccurrences(redacted, mapping) {
164
+ const entries = Object.entries(mapping).filter(([, surface]) => surface && surface.length > 0).sort((a, b) => b[1].length - a[1].length);
165
+ let result = redacted;
166
+ for (const [sentinel, surface] of entries) {
167
+ const pattern = new RegExp(`(?<![\\p{L}\\p{N}_])${escapeRegex(surface)}(?![\\p{L}\\p{N}_])`, "gu");
168
+ result = result.replace(pattern, sentinel);
169
+ }
170
+ return result;
171
+ }
158
172
  // src/core/stream.ts
159
173
  var SENTINEL_AT = /<([A-Z][A-Z0-9_]*)_(\d+)>/y;
160
174
  var MAX_SENTINEL_LEN = 64;
@@ -826,7 +840,7 @@ async function* deanonymizeStream(chunks, session) {
826
840
  }
827
841
 
828
842
  // src/index.ts
829
- var VERSION = "0.1.3";
843
+ var VERSION = "0.1.4";
830
844
  export {
831
845
  renderSentinel,
832
846
  parseSentinel,
@@ -771,7 +771,9 @@ class Session {
771
771
  cursor = end;
772
772
  }
773
773
  out.push(text.slice(cursor));
774
- return { redacted: out.join(""), merged };
774
+ let redacted = out.join("");
775
+ redacted = recoverDuplicateOccurrences(redacted, this.mapping);
776
+ return { redacted, merged };
775
777
  }
776
778
  restore(text) {
777
779
  if (Object.keys(this.mapping).length === 0)
@@ -827,6 +829,18 @@ function mergeAdjacent(text, spans) {
827
829
  }
828
830
  return out;
829
831
  }
832
+ function escapeRegex(s) {
833
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
834
+ }
835
+ function recoverDuplicateOccurrences(redacted, mapping) {
836
+ const entries = Object.entries(mapping).filter(([, surface]) => surface && surface.length > 0).sort((a, b) => b[1].length - a[1].length);
837
+ let result = redacted;
838
+ for (const [sentinel, surface] of entries) {
839
+ const pattern = new RegExp(`(?<![\\p{L}\\p{N}_])${escapeRegex(surface)}(?![\\p{L}\\p{N}_])`, "gu");
840
+ result = result.replace(pattern, sentinel);
841
+ }
842
+ return result;
843
+ }
830
844
 
831
845
  // src/openai/_base.ts
832
846
  function resolveCallSession(body, defaultDet) {
@@ -1 +1 @@
1
- {"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../../src/core/session.ts"],"names":[],"mappings":"AAAA,kDAAkD;AAClD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAGjD;;;gEAGgE;AAChE,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,CAAC;AAkBtD,uCAAuC;AACvC,MAAM,WAAW,cAAc;IAC7B;;;;;;;;;;OAUG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;CAC1C;AAED,qBAAa,OAA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,UAAU;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAC3B,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,SAAS,CAAC,IAAI,GAAG,UAAU,CAAC,EAAE,GACpC,UAAU,EAAE,CAqBd"}
@@ -17,7 +17,7 @@ export { DEFAULT_BASE_URL, REMOTE_DEFAULT_MODEL, RemoteDetector, LocalDetector,
17
17
  export type { RemoteDetectorOptions, LocalDetectorOptions, LocalDetectorLoadEvent, CompositeDetectorOptions, } from "./detectors/index.ts";
18
18
  export { anonymizeMessages, anonymizeText, deanonymizeStream, deanonymizeText, } from "./plain.ts";
19
19
  export type { ChatMessage } from "./plain.ts";
20
- export declare const VERSION = "0.1.3";
20
+ export declare const VERSION = "0.1.4";
21
21
  export { mergeAdjacent } from "./core/session.ts";
22
22
  export type { MergedSpan } from "./core/session.ts";
23
23
  //# sourceMappingURL=index.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gheim",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "PII round-trip for LLM APIs: anonymize before the request, de-anonymize the stream on the way back.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Joel Barmettler <joel.barmettler@gmail.com>",