@atscript/typescript 0.2.8 → 0.2.10

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.
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
- const require_plugin = require('./plugin-lDfE9qm8.cjs');
3
- const require_user_text = require('./user-text-BvyErcKK.cjs');
2
+ const require_plugin = require('./plugin-BNbjc9-d.cjs');
3
+ const require_user_text = require('./user-text-BrP_qD3T.cjs');
4
4
  let path = require("path");
5
5
  path = require_plugin.__toESM(path);
6
6
  let _atscript_core = require("@atscript/core");
@@ -39,6 +39,13 @@ let fs = require("fs");
39
39
  * (interpreted or compiled) nor the literal-union shortcut, so it always runs the full
40
40
  * error-collecting walk.
41
41
  */ var WalkValidator = class extends require_user_text.Validator {};
42
+ /** Freezes `value` and everything it holds (a spec of a function that must not mutate). */ function deepFreeze(value) {
43
+ if (typeof value === "object" && value !== null) {
44
+ for (const item of Object.values(value)) deepFreeze(item);
45
+ Object.freeze(value);
46
+ }
47
+ return value;
48
+ }
42
49
  /** A seeded random number generator (mulberry32) returning values in `[0, 1)`. */ function rng(seed) {
43
50
  return () => {
44
51
  seed = seed + 1831565813 | 0;
@@ -108,6 +115,7 @@ let fs = require("fs");
108
115
 
109
116
  //#endregion
110
117
  exports.WalkValidator = WalkValidator;
118
+ exports.deepFreeze = deepFreeze;
111
119
  exports.denseSchema = denseSchema;
112
120
  exports.prepareFixtures = prepareFixtures;
113
121
  exports.rng = rng;
@@ -251,7 +251,10 @@ declare class Validator<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedTyp
251
251
  private builtin;
252
252
  /** {@link fail} of a type mismatch of `def`: its `@expect.message` replaces the message. */
253
253
  private mismatch;
254
- /** The `@meta.required` error of `def` (custom message or the type's default). */
254
+ /**
255
+ * The `@meta.required` error of `def`: its message, else its `@expect.message`, else the code's
256
+ * default.
257
+ */
255
258
  private required;
256
259
  /**
257
260
  * Adds `error`, already made, to the errors of the current level (as {@link error} does);
@@ -280,6 +283,12 @@ declare class Validator<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedTyp
280
283
  private get shortcuts();
281
284
  /** Validates `value` against `def` (after `replace`, `optional` and the plugins). */
282
285
  protected validateSafe(def: TAtscriptAnnotatedType, value: any): boolean;
286
+ /**
287
+ * A missing value (`undefined`, or `null`) of a `@meta.required` `def`: when `def` rejects it,
288
+ * the `@meta.required` error replaces the type error the walk reports (the same verdict). A
289
+ * bare `@meta.required` keeps the `@expect.message` text the type error had.
290
+ */
291
+ private missing;
283
292
  /**
284
293
  * What {@link validateSafe} decides of `def` (replaced) before its own validation: an
285
294
  * optional `def` passes `undefined` / `null`, a plugin may decide — `undefined` to go on.
@@ -468,6 +477,41 @@ interface TMetadataMap<O extends object> extends Map<keyof O, O[keyof O]> {
468
477
  has(key: string): boolean;
469
478
  }
470
479
 
480
+ /**
481
+ * What a strict tool / structured-output schema of an LLM provider accepts (the `strict` option
482
+ * of {@link buildJsonSchema}): the schema is built closed (`additionalProperties: false` on every
483
+ * object), then rewritten to the profile. Constraints the profile drops are not enforced by the
484
+ * provider: validate the model's output with the atscript validator.
485
+ */
486
+ interface TStrictProfile {
487
+ /** The provider's name, in the errors of a shape it can't take. */
488
+ name: string;
489
+ /**
490
+ * The keywords the provider accepts (an allow-list): `true`, or a check of the keyword's value
491
+ * (`false`: the keyword is dropped there). Every other keyword is dropped.
492
+ */
493
+ keywords: Readonly<Record<string, true | ((value: unknown) => boolean)>>;
494
+ /**
495
+ * Every property in `required`, the optional ones nullable: the model sends `null` for an
496
+ * optional field it leaves out. The validator rejects `null` on an optional `@meta.required`
497
+ * field: drop those values first (see `dropOptionalNulls`).
498
+ */
499
+ requireAll: boolean;
500
+ /**
501
+ * Rewrites the `allOf` the schema builder writes: a `$ref` with notes beside it (a field's
502
+ * title, description, key counts) → the referenced schema inlined with them; several patterns
503
+ * → the first one (the rest dropped) when `allOf` itself is not allowed. Any other `allOf`
504
+ * the profile does not allow throws.
505
+ */
506
+ collapseAllOf: boolean;
507
+ /** Appends each dropped constraint to the schema's `description`, in plain words. */
508
+ describeDropped: boolean;
509
+ /** Recursive types (a `$defs` entry that refers to itself); `false`: throws. */
510
+ allowRecursion: boolean;
511
+ /** The root must be an object schema; else throws. */
512
+ objectRoot: boolean;
513
+ }
514
+
471
515
  /** Options of {@link buildJsonSchema} (and of the `jsonSchemaOptions` plugin option). */
472
516
  interface TJsonSchemaOptions {
473
517
  /**
@@ -486,6 +530,14 @@ interface TJsonSchemaOptions {
486
530
  * `anyOf` of `const` schemas. *(default: `true`)*
487
531
  */
488
532
  enums?: boolean;
533
+ /**
534
+ * Targets a strict LLM tool / structured-output schema: `OPENAI_STRICT`, `ANTHROPIC_STRICT` or
535
+ * a profile of your own (see {@link TStrictProfile}). The schema is built `closed`, then keeps
536
+ * only the keywords the profile allows (the dropped constraints described in `description`);
537
+ * a shape the profile can't take throws. The provider does not enforce what was dropped:
538
+ * validate the model's output with the type's validator. Runtime only (not a plugin option).
539
+ */
540
+ strict?: TStrictProfile;
489
541
  }
490
542
 
491
543
  interface TTsPluginOptions {
@@ -506,7 +558,7 @@ interface TTsPluginOptions {
506
558
  * `title` from `@meta.description` / `@meta.label`, default `true`), `enums` (a union of
507
559
  * literals as `enum`, default `true`).
508
560
  */
509
- jsonSchemaOptions?: TJsonSchemaOptions;
561
+ jsonSchemaOptions?: Omit<TJsonSchemaOptions, 'strict'>;
510
562
  /**
511
563
  * Example data support:
512
564
  * - `false` — No support. `toExampleData` is not rendered. *(default)*
@@ -580,6 +632,8 @@ declare function prepareFixtures(options: PrepareFixturesOptions): Promise<void>
580
632
  */
581
633
  declare class WalkValidator extends Validator {
582
634
  }
635
+ /** Freezes `value` and everything it holds (a spec of a function that must not mutate). */
636
+ declare function deepFreeze<T>(value: T): T;
583
637
  /** A seeded random number generator (mulberry32) returning values in `[0, 1)`. */
584
638
  declare function rng(seed: number): () => number;
585
639
  /**
@@ -590,5 +644,5 @@ declare function rng(seed: number): () => number;
590
644
  */
591
645
  declare function denseSchema(seed: number): Record<string, string>;
592
646
 
593
- export { WalkValidator, denseSchema, prepareFixtures, rng };
647
+ export { WalkValidator, deepFreeze, denseSchema, prepareFixtures, rng };
594
648
  export type { PrepareFixturesOptions };
@@ -1,5 +1,5 @@
1
- import { r as tsPlugin } from "./plugin-DUInOy5v.mjs";
2
- import { j as Validator } from "./user-text-DZR5hfLa.mjs";
1
+ import { r as tsPlugin } from "./plugin-CnraKYN3.mjs";
2
+ import { j as Validator } from "./user-text-DS0lorlV.mjs";
3
3
  import path from "path";
4
4
  import { build } from "@atscript/core";
5
5
  import { readFileSync, writeFileSync } from "fs";
@@ -37,6 +37,13 @@ import { readFileSync, writeFileSync } from "fs";
37
37
  * (interpreted or compiled) nor the literal-union shortcut, so it always runs the full
38
38
  * error-collecting walk.
39
39
  */ var WalkValidator = class extends Validator {};
40
+ /** Freezes `value` and everything it holds (a spec of a function that must not mutate). */ function deepFreeze(value) {
41
+ if (typeof value === "object" && value !== null) {
42
+ for (const item of Object.values(value)) deepFreeze(item);
43
+ Object.freeze(value);
44
+ }
45
+ return value;
46
+ }
40
47
  /** A seeded random number generator (mulberry32) returning values in `[0, 1)`. */ function rng(seed) {
41
48
  return () => {
42
49
  seed = seed + 1831565813 | 0;
@@ -105,4 +112,4 @@ import { readFileSync, writeFileSync } from "fs";
105
112
  }
106
113
 
107
114
  //#endregion
108
- export { WalkValidator, denseSchema, prepareFixtures, rng };
115
+ export { WalkValidator, deepFreeze, denseSchema, prepareFixtures, rng };
@@ -1528,12 +1528,14 @@ const regexCache = /* @__PURE__ */ new Map();
1528
1528
  }
1529
1529
  return regex;
1530
1530
  }
1531
+ /** Whether `def` carries `@meta.required`. */ function isRequired(def) {
1532
+ return !!filledMeta(def.metadata)?.get("meta.required");
1533
+ }
1531
1534
  /**
1532
1535
  * `true` when `def` (an optional field holding `null`) carries `@meta.required` and its
1533
1536
  * type does not itself accept `null`. Shared by the walk and the fast pre-check.
1534
1537
  */ function rejectsNullAsRequired(def) {
1535
- const metadata = filledMeta(def.metadata);
1536
- return metadata !== void 0 && !!metadata.get("meta.required") && !acceptsNull(def);
1538
+ return isRequired(def) && !acceptsNull(def);
1537
1539
  }
1538
1540
  function acceptsNull(def) {
1539
1541
  const type = def.type;
@@ -2455,9 +2457,12 @@ function _define_property(obj, key, value) {
2455
2457
  /** {@link fail} of a type mismatch of `def`: its `@expect.message` replaces the message. */ mismatch(def, code, params) {
2456
2458
  return this.fail(code, params, expectMessage(def));
2457
2459
  }
2458
- /** The `@meta.required` error of `def` (custom message or the type's default). */ required(def) {
2460
+ /**
2461
+ * The `@meta.required` error of `def`: its message, else its `@expect.message`, else the code's
2462
+ * default.
2463
+ */ required(def) {
2459
2464
  const isBoolean = def.type.kind === "" && def.type.designType === "boolean";
2460
- return this.fail(isBoolean ? "REQUIRED_CHECKED" : "REQUIRED", {}, messageOf(def.metadata.get("meta.required")));
2465
+ return this.fail(isBoolean ? "REQUIRED_CHECKED" : "REQUIRED", {}, messageOf(def.metadata.get("meta.required")) ?? expectMessage(def));
2461
2466
  }
2462
2467
  /**
2463
2468
  * Adds `error`, already made, to the errors of the current level (as {@link error} does);
@@ -2528,9 +2533,22 @@ function _define_property(obj, key, value) {
2528
2533
  const decided = this.preCheck(def, value);
2529
2534
  if (decided !== void 0) return decided;
2530
2535
  }
2536
+ if ((value === void 0 || value === null) && isRequired(def)) return this.missing(def, value);
2531
2537
  return this.validateAnnotatedType(def, value);
2532
2538
  }
2533
2539
  /**
2540
+ * A missing value (`undefined`, or `null`) of a `@meta.required` `def`: when `def` rejects it,
2541
+ * the `@meta.required` error replaces the type error the walk reports (the same verdict). A
2542
+ * bare `@meta.required` keeps the `@expect.message` text the type error had.
2543
+ */ missing(def, value) {
2544
+ this.stackErrors.push(null);
2545
+ const passed = this.validateAnnotatedType(def, value);
2546
+ this.stackErrors.pop();
2547
+ if (passed) return true;
2548
+ this.limitExceeded = false;
2549
+ return this.required(def);
2550
+ }
2551
+ /**
2534
2552
  * What {@link validateSafe} decides of `def` (replaced) before its own validation: an
2535
2553
  * optional `def` passes `undefined` / `null`, a plugin may decide — `undefined` to go on.
2536
2554
  */ preCheck(def, value) {
@@ -4142,6 +4160,12 @@ Object.defineProperty(exports, 'ValidatorError', {
4142
4160
  return ValidatorError;
4143
4161
  }
4144
4162
  });
4163
+ Object.defineProperty(exports, 'acceptsNull', {
4164
+ enumerable: true,
4165
+ get: function () {
4166
+ return acceptsNull;
4167
+ }
4168
+ });
4145
4169
  Object.defineProperty(exports, 'annotate', {
4146
4170
  enumerable: true,
4147
4171
  get: function () {
@@ -1527,12 +1527,14 @@ const regexCache = /* @__PURE__ */ new Map();
1527
1527
  }
1528
1528
  return regex;
1529
1529
  }
1530
+ /** Whether `def` carries `@meta.required`. */ function isRequired(def) {
1531
+ return !!filledMeta(def.metadata)?.get("meta.required");
1532
+ }
1530
1533
  /**
1531
1534
  * `true` when `def` (an optional field holding `null`) carries `@meta.required` and its
1532
1535
  * type does not itself accept `null`. Shared by the walk and the fast pre-check.
1533
1536
  */ function rejectsNullAsRequired(def) {
1534
- const metadata = filledMeta(def.metadata);
1535
- return metadata !== void 0 && !!metadata.get("meta.required") && !acceptsNull(def);
1537
+ return isRequired(def) && !acceptsNull(def);
1536
1538
  }
1537
1539
  function acceptsNull(def) {
1538
1540
  const type = def.type;
@@ -2454,9 +2456,12 @@ function _define_property(obj, key, value) {
2454
2456
  /** {@link fail} of a type mismatch of `def`: its `@expect.message` replaces the message. */ mismatch(def, code, params) {
2455
2457
  return this.fail(code, params, expectMessage(def));
2456
2458
  }
2457
- /** The `@meta.required` error of `def` (custom message or the type's default). */ required(def) {
2459
+ /**
2460
+ * The `@meta.required` error of `def`: its message, else its `@expect.message`, else the code's
2461
+ * default.
2462
+ */ required(def) {
2458
2463
  const isBoolean = def.type.kind === "" && def.type.designType === "boolean";
2459
- return this.fail(isBoolean ? "REQUIRED_CHECKED" : "REQUIRED", {}, messageOf(def.metadata.get("meta.required")));
2464
+ return this.fail(isBoolean ? "REQUIRED_CHECKED" : "REQUIRED", {}, messageOf(def.metadata.get("meta.required")) ?? expectMessage(def));
2460
2465
  }
2461
2466
  /**
2462
2467
  * Adds `error`, already made, to the errors of the current level (as {@link error} does);
@@ -2527,9 +2532,22 @@ function _define_property(obj, key, value) {
2527
2532
  const decided = this.preCheck(def, value);
2528
2533
  if (decided !== void 0) return decided;
2529
2534
  }
2535
+ if ((value === void 0 || value === null) && isRequired(def)) return this.missing(def, value);
2530
2536
  return this.validateAnnotatedType(def, value);
2531
2537
  }
2532
2538
  /**
2539
+ * A missing value (`undefined`, or `null`) of a `@meta.required` `def`: when `def` rejects it,
2540
+ * the `@meta.required` error replaces the type error the walk reports (the same verdict). A
2541
+ * bare `@meta.required` keeps the `@expect.message` text the type error had.
2542
+ */ missing(def, value) {
2543
+ this.stackErrors.push(null);
2544
+ const passed = this.validateAnnotatedType(def, value);
2545
+ this.stackErrors.pop();
2546
+ if (passed) return true;
2547
+ this.limitExceeded = false;
2548
+ return this.required(def);
2549
+ }
2550
+ /**
2533
2551
  * What {@link validateSafe} decides of `def` (replaced) before its own validation: an
2534
2552
  * optional `def` passes `undefined` / `null`, a plugin may decide — `undefined` to go on.
2535
2553
  */ preCheck(def, value) {
@@ -4021,4 +4039,4 @@ const USER_TEXT = new Map(CORE_USER_TEXT);
4021
4039
  }
4022
4040
 
4023
4041
  //#endregion
4024
- export { VALIDATOR_KEYS as $, propAtChain as A, registerCompiledCheck as B, createAnnotatedTypeNode as C, isAnnotatedType as D, defineAnnotatedType as E, VALIDATOR_MESSAGES as F, $nr as G, jsonSchemaPattern as H, formatCodeOf as I, watchLayer as J, LayerMap as K, formatValidatorMessage as L, ValidatorError as M, unionDiscriminator as N, isAnnotatedTypeOfPrimitive as O, VALIDATOR_ERROR_CODES as P, DECIMAL_RE as Q, localizeErrors as R, annotate as S, createObjectTypeDef as T, statelessRegExp as U, detectDiscriminator as V, $ak as W, touched as X, invalidateCompiledValidators as Y, typeWriteCount as Z, pendingStatements as _, userTextOf as a, cutKey as b, $blk as c, $fc as d, arrayKeyProps as et, $ft as f, appliedBlockModules as g, $sh as h, userTextKeys as i, Validator as j, isPhantomType as k, $end as l, $q as m, CORE_USER_TEXT as n, uniqueName as o, $ps as p, metaEmpty as q, registerUserText as r, $alias as s, $ut as t, boundOf as tt, $f as u, stageEnds as v, createMetadataMap as w, enterCut as x, cutAs as y, messagesTranslator as z };
4042
+ export { DECIMAL_RE as $, propAtChain as A, acceptsNull as B, createAnnotatedTypeNode as C, isAnnotatedType as D, defineAnnotatedType as E, VALIDATOR_MESSAGES as F, $ak as G, detectDiscriminator as H, formatCodeOf as I, metaEmpty as J, $nr as K, formatValidatorMessage as L, ValidatorError as M, unionDiscriminator as N, isAnnotatedTypeOfPrimitive as O, VALIDATOR_ERROR_CODES as P, typeWriteCount as Q, localizeErrors as R, annotate as S, createObjectTypeDef as T, jsonSchemaPattern as U, registerCompiledCheck as V, statelessRegExp as W, invalidateCompiledValidators as X, watchLayer as Y, touched as Z, pendingStatements as _, userTextOf as a, cutKey as b, $blk as c, $fc as d, VALIDATOR_KEYS as et, $ft as f, appliedBlockModules as g, $sh as h, userTextKeys as i, Validator as j, isPhantomType as k, $end as l, $q as m, CORE_USER_TEXT as n, boundOf as nt, uniqueName as o, $ps as p, LayerMap as q, registerUserText as r, $alias as s, $ut as t, arrayKeyProps as tt, $f as u, stageEnds as v, createMetadataMap as w, enterCut as x, cutAs as y, messagesTranslator as z };