poe-code 3.0.273 → 3.0.275

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.
@@ -6,9 +6,18 @@ const defaultOutput = {
6
6
  stderr: process.stderr
7
7
  };
8
8
  function parseTokenCount(value) {
9
+ const isDecimalDigit = (character) => {
10
+ const code = character.charCodeAt(0);
11
+ return code >= 48 && code <= 57;
12
+ };
13
+ const isCanonicalDecimal = value === "0" ||
14
+ (value.length > 0 && value[0] !== "0" && Array.from(value).every(isDecimalDigit));
15
+ if (!isCanonicalDecimal) {
16
+ throw new InvalidArgumentError("count must be a non-negative decimal integer");
17
+ }
9
18
  const tokenCount = Number(value);
10
- if (!Number.isInteger(tokenCount) || tokenCount < 0) {
11
- throw new InvalidArgumentError("count must be a non-negative integer");
19
+ if (!Number.isSafeInteger(tokenCount) || tokenCount < 0) {
20
+ throw new InvalidArgumentError("count must be a non-negative decimal integer");
12
21
  }
13
22
  return tokenCount;
14
23
  }
@@ -38,10 +47,10 @@ function createTokenfillProgram(output) {
38
47
  output.stderr.write(`Generated ${result.actualTokens} tokens using ${encoding}\n`);
39
48
  });
40
49
  program.configureOutput({
41
- writeOut: value => {
50
+ writeOut: (value) => {
42
51
  output.stdout.write(value);
43
52
  },
44
- writeErr: value => {
53
+ writeErr: (value) => {
45
54
  output.stderr.write(value);
46
55
  }
47
56
  });
@@ -3,12 +3,29 @@ export const DEFAULT_ENCODING = "cl100k_base";
3
3
  export function createTokenizer(options = {}) {
4
4
  const encoding = options.encoding ?? DEFAULT_ENCODING;
5
5
  const tokenizer = get_encoding(encoding);
6
- const utf8Decoder = new TextDecoder();
7
6
  const strictUtf8Decoder = new TextDecoder("utf-8", { fatal: true });
8
7
  const encode = (text) => tokenizer.encode(text);
8
+ const normalizeDecodeTokens = (tokens) => {
9
+ if (tokens instanceof Uint32Array) {
10
+ return tokens;
11
+ }
12
+ const tokenArray = new Uint32Array(tokens.length);
13
+ tokens.forEach((token, index) => {
14
+ if (!Number.isFinite(token) || !Number.isInteger(token) || token < 0 || token > 0xffffffff) {
15
+ throw new TypeError(`token id at index ${index} must be a finite non-negative integer.`);
16
+ }
17
+ tokenArray[index] = token;
18
+ });
19
+ return tokenArray;
20
+ };
9
21
  const decode = (tokens) => {
10
- const tokenArray = tokens instanceof Uint32Array ? tokens : Uint32Array.from(tokens);
11
- return utf8Decoder.decode(tokenizer.decode(tokenArray));
22
+ const tokenArray = normalizeDecodeTokens(tokens);
23
+ try {
24
+ return strictUtf8Decoder.decode(tokenizer.decode(tokenArray));
25
+ }
26
+ catch {
27
+ throw new Error("Cannot decode tokens without corrupting UTF-8 text.");
28
+ }
12
29
  };
13
30
  const count = (text) => encode(text).length;
14
31
  const truncate = (text, tokenCount) => {