@theoremjs/agents 0.3.5-next.896.g0fa6df9 → 0.3.5-next.897.g1c14afe

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.
@@ -32,6 +32,8 @@ function lineProblem(parts, schema) {
32
32
  for (const check of line.when) {
33
33
  if (check.path.includes('.'))
34
34
  return `"${check.path}" isn't one option.`; // labels-exempt: developer contract error
35
+ if ('has' in check)
36
+ return 'An option is one value, so it has no fields.'; // labels-exempt: developer contract error
35
37
  if (check.expected === null)
36
38
  return 'An option has no null value, so "is null" never fits.'; // labels-exempt: developer contract error
37
39
  const problem = checkProblem(check, schema, 'this profile’s options');
@@ -1,10 +1,16 @@
1
1
  /** One line of wording, or a list of lines read top to bottom: the first that fits is shown. */
2
2
  export type LabelTemplate = string | readonly string[];
3
- /** `expected: null` is the check "is null". */
3
+ /**
4
+ * `when status is archived`, or `when action has erase`: whether the group at `path` holds the
5
+ * field `has`. `expected: null` is the check "is null".
6
+ */
4
7
  export type Check = {
5
8
  path: string;
6
9
  negated: boolean;
7
10
  expected: string | null;
11
+ } | {
12
+ path: string;
13
+ has: string;
8
14
  };
9
15
  export type Line = {
10
16
  joined: boolean;
@@ -18,6 +18,7 @@ const JOINED = /^\+\s+/;
18
18
  /** The colon that ends a line's condition: the first one followed by a space or the line's end. */
19
19
  const WHEN_ENDS = /:(?:\s|$)/;
20
20
  const CHECK = /^(\S+) is (?:(not) )?(\S.*)$/;
21
+ const HAS = /^(\S+) has (\S+)$/;
21
22
  function parsePlaceholder(raw) {
22
23
  const text = raw.trim();
23
24
  const end = text.search(/[\s|]/);
@@ -82,6 +83,11 @@ function parseChecks(raw) {
82
83
  const checks = [];
83
84
  // why: one space between words first, so no pattern below can try a run of spaces two ways.
84
85
  for (const part of raw.replace(/\s+/g, ' ').split(' and ')) {
86
+ const has = HAS.exec(part.trim());
87
+ if (has?.[1] && has[2]) {
88
+ checks.push({ path: has[1], has: has[2] });
89
+ continue;
90
+ }
85
91
  const match = CHECK.exec(part.trim());
86
92
  const [, path, not, expected] = match ?? [];
87
93
  if (!path || !expected)
@@ -131,6 +137,8 @@ export function checkHolds(check, input) {
131
137
  if (!keys)
132
138
  return false;
133
139
  const value = valueAt(input, keys);
140
+ if ('has' in check)
141
+ return isRecord(value) && Object.hasOwn(value, check.has);
134
142
  const comparable = typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean';
135
143
  const is = check.expected === null ? value === null : comparable && `${value}` === check.expected;
136
144
  return is !== check.negated;
@@ -375,6 +383,40 @@ function schemaLeavesAt(node, keys) {
375
383
  }
376
384
  return types.length === 0 && rawTypes(node).length === 0 ? [OPEN_LEAF] : [];
377
385
  }
386
+ /** The field names a group at `keys` can hold, or `'any'` when the schema doesn't say. */
387
+ function schemaFieldsAt(node, keys) {
388
+ if (!isRecord(node) || '$ref' in node)
389
+ return 'any';
390
+ const branches = schemaBranches(node);
391
+ if (branches.length) {
392
+ const found = branches.map((branch) => schemaFieldsAt(branch, keys));
393
+ return found.includes('any')
394
+ ? 'any'
395
+ : found.flatMap((fields) => (fields === 'any' ? [] : fields));
396
+ }
397
+ const [key, ...rest] = keys;
398
+ if (key === undefined) {
399
+ if (isRecord(node.properties))
400
+ return Object.keys(node.properties);
401
+ return schemaTypes(node).length === 0 ? 'any' : [];
402
+ }
403
+ if (schemaTypes(node).includes('array') && INDEX.test(key)) {
404
+ return schemaFieldsAt(node.items, rest);
405
+ }
406
+ if (isRecord(node.properties)) {
407
+ return Object.hasOwn(node.properties, key) ? schemaFieldsAt(node.properties[key], rest) : [];
408
+ }
409
+ return schemaTypes(node).length === 0 ? 'any' : [];
410
+ }
411
+ function hasProblem(path, has, holds, fields) {
412
+ if (holds !== 'group') {
413
+ return `"${path}" in the "when" is not a group, so it has no "${has}". "has" asks which field a group holds.`; // labels-exempt: builder diagnostic
414
+ }
415
+ if (fields === 'any' || fields.includes(has))
416
+ return undefined;
417
+ const itHas = fields.length ? ` It has ${oneOf(fields)}.` : ''; // labels-exempt: builder diagnostic
418
+ return `"${path}" never has "${has}".${itHas}`; // labels-exempt: builder diagnostic
419
+ }
378
420
  /** The input's own field names, flags included: what a `when` can compare. */
379
421
  function inputFields(node) {
380
422
  if (!isRecord(node))
@@ -400,8 +442,11 @@ export function checkProblem(check, input, reads = "this tool's input") {
400
442
  const tryThese = fields.length ? ` Try ${oneOf(fields)}.` : ''; // labels-exempt: builder diagnostic
401
443
  return `"${check.path}" in the "when" is not a field of ${reads}.${tryThese}`; // labels-exempt: builder diagnostic
402
444
  }
445
+ if ('has' in check) {
446
+ return hasProblem(check.path, check.has, found.holds, schemaFieldsAt(input, keys));
447
+ }
403
448
  if (found.holds === 'group') {
404
- return `"${check.path}" in the "when" is a list or group; a "when" compares text, a number, or true or false.`; // labels-exempt: builder diagnostic
449
+ return `"${check.path}" in the "when" is a list or group; a "when" compares text, a number, or true or false, or says which field a group has, like "when ${check.path} has <field>".`; // labels-exempt: builder diagnostic
405
450
  }
406
451
  const leaves = schemaLeavesAt(input, keys);
407
452
  if (check.expected === null) {
@@ -420,7 +465,7 @@ export function checkProblem(check, input, reads = "this tool's input") {
420
465
  }
421
466
  function lineProblem(line, schemas, sides, mustShow) {
422
467
  if (line.when === 'unreadable') {
423
- return 'A line that opens with "when" needs a condition and a colon, like "when status is archived: archiving {name}". A condition uses "is", "is not", "is null", and "and".'; // labels-exempt: builder diagnostic
468
+ return 'A line that opens with "when" needs a condition and a colon, like "when status is archived: archiving {name}". A condition uses "is", "is not", "is null", "has", and "and".'; // labels-exempt: builder diagnostic
424
469
  }
425
470
  for (const check of line.when) {
426
471
  const problem = checkProblem(check, schemas.input);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theoremjs/agents",
3
- "version": "0.3.5-next.896.g0fa6df9",
3
+ "version": "0.3.5-next.897.g1c14afe",
4
4
  "description": "A flat TypeScript agent kernel for typed profiles, deterministic turn execution, registered tools, provider adapters, guardrails, and host-injected traces.",
5
5
  "keywords": [
6
6
  "agent",