@noetic-tools/core 5.22.0-next.5 → 5.22.0-next.7

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.
@@ -15,6 +15,7 @@ import { z } from 'zod';
15
15
  import { emitFrameworkEvent, getBroadcaster } from './broadcaster-utils.js';
16
16
  import { ContextImpl, rootIdOf } from './context-impl.js';
17
17
  import { hashStepInput } from './durable/step-ledger.js';
18
+ import { questionAnswerProblem } from './held-question.js';
18
19
  import { createInMemoryStorage } from './in-memory-storage.js';
19
20
  //#region Types
20
21
  const ResolutionSchema = z.object({
@@ -42,6 +43,7 @@ const HeldRecordSchema = z.object({
42
43
  'approval',
43
44
  'result',
44
45
  'outcome',
46
+ 'question',
45
47
  ]),
46
48
  dueAt: z.number().optional(),
47
49
  onDue: z
@@ -52,6 +54,14 @@ const HeldRecordSchema = z.object({
52
54
  .optional(),
53
55
  /** Passed back by a tool's `hold()`, for whoever answers. */
54
56
  request: z.unknown().optional(),
57
+ /** The question, on a call held with `reason: 'question'`. */
58
+ question: z
59
+ .object({
60
+ message: z.string(),
61
+ schema: z.record(z.string(), z.unknown()).optional(),
62
+ url: z.string().optional(),
63
+ })
64
+ .optional(),
55
65
  resolution: ResolutionSchema.optional(),
56
66
  });
57
67
  /**
@@ -94,6 +104,29 @@ function refused(callId, reason) {
94
104
  reason,
95
105
  });
96
106
  }
107
+ /** Why `resolution` is the wrong kind of answer for `record`, or undefined. */
108
+ function wrongAnswerKind(record, resolution) {
109
+ // An unknown outcome takes either answer: "it landed, here is the result", or
110
+ // a decision on whether to run it again.
111
+ if (record.reason === 'outcome') {
112
+ return undefined;
113
+ }
114
+ if (record.reason === 'question') {
115
+ if (resolution.hasOutput === true) {
116
+ const problem = questionAnswerProblem(record.question, resolution.output);
117
+ return problem ? `the answer does not fit the question: ${problem}` : undefined;
118
+ }
119
+ return resolution.action === 'deny'
120
+ ? undefined
121
+ : 'a question takes an answer (output) or deny, not an approval';
122
+ }
123
+ if ((record.reason === 'result') === (resolution.hasOutput === true)) {
124
+ return undefined;
125
+ }
126
+ return record.reason === 'result'
127
+ ? 'the call waits for a result, not an approval'
128
+ : 'the call waits for an approval, not a result';
129
+ }
97
130
  function sameResolution(a, b) {
98
131
  return JSON.stringify(a) === JSON.stringify(b);
99
132
  }
@@ -168,6 +201,11 @@ export function toHeldCall(record) {
168
201
  : {
169
202
  request: record.request,
170
203
  }),
204
+ ...(record.question === undefined
205
+ ? {}
206
+ : {
207
+ question: record.question,
208
+ }),
171
209
  };
172
210
  }
173
211
  //#endregion
@@ -410,14 +448,9 @@ export class HeldCalls {
410
448
  throw refused(callId, 'the answer is for a call with different arguments');
411
449
  }
412
450
  const resolution = toResolutionRecord(answer.resolution);
413
- // An unknown outcome takes either answer: "it landed, here is the result", or
414
- // a decision on whether to run it again.
415
- const wrongKind = record.reason !== 'outcome' &&
416
- (record.reason === 'result') !== (resolution.hasOutput === true);
451
+ const wrongKind = wrongAnswerKind(record, resolution);
417
452
  if (wrongKind) {
418
- throw refused(callId, record.reason === 'result'
419
- ? 'the call waits for a result, not an approval'
420
- : 'the call waits for an approval, not a result');
453
+ throw refused(callId, wrongKind);
421
454
  }
422
455
  if (record.resolution) {
423
456
  if (sameResolution(record.resolution, resolution)) {
@@ -610,6 +643,11 @@ export async function holdAndWait(opts) {
610
643
  for (;;) {
611
644
  const current = await holds.load(executionId, callId);
612
645
  const settled = current?.resolution !== undefined;
646
+ if (settled && opts.keepAnswer) {
647
+ return {
648
+ resolution: current?.resolution,
649
+ };
650
+ }
613
651
  if (!current || settled || isOverdue(current, holds.now())) {
614
652
  if (mayWriteHolds(opts.ctx)) {
615
653
  await holds.close(executionId, callId);
@@ -624,6 +662,7 @@ export async function holdAndWait(opts) {
624
662
  callIds: [
625
663
  callId,
626
664
  ],
665
+ park: opts.park,
627
666
  });
628
667
  }
629
668
  }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * A question held for a person (spec 08 § Held tool calls, `reason: 'question'`):
3
+ * checking that an answer fits the question's form before the question settles.
4
+ *
5
+ * A question's `schema` is the flat form both askers use — MCP Tasks
6
+ * `input_required` and ACP `elicitation/create`: an object whose own properties
7
+ * are strings, numbers, integers, booleans or arrays of strings, with the usual
8
+ * constraints (`enum`, `minLength`/`maxLength`, `pattern`, `minimum`/`maximum`,
9
+ * `minItems`/`maxItems`, `items.enum`), some of them `required`, and
10
+ * `additionalProperties: false` to refuse any other field.
11
+ */
12
+ import type { HeldQuestion } from '@noetic-tools/types';
13
+ /**
14
+ * Why `answer` does not fit `question`'s form, or undefined when it does (or the
15
+ * question has no form). Only the answer's own fields count.
16
+ * @internal
17
+ */
18
+ export declare function questionAnswerProblem(question: HeldQuestion | undefined, answer: unknown): string | undefined;
@@ -0,0 +1,179 @@
1
+ /**
2
+ * A question held for a person (spec 08 § Held tool calls, `reason: 'question'`):
3
+ * checking that an answer fits the question's form before the question settles.
4
+ *
5
+ * A question's `schema` is the flat form both askers use — MCP Tasks
6
+ * `input_required` and ACP `elicitation/create`: an object whose own properties
7
+ * are strings, numbers, integers, booleans or arrays of strings, with the usual
8
+ * constraints (`enum`, `minLength`/`maxLength`, `pattern`, `minimum`/`maximum`,
9
+ * `minItems`/`maxItems`, `items.enum`), some of them `required`, and
10
+ * `additionalProperties: false` to refuse any other field.
11
+ */
12
+ //#endregion
13
+ //#region Helpers
14
+ function isRecord(value) {
15
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
16
+ }
17
+ function numberAt(schema, key) {
18
+ const value = schema[key];
19
+ return typeof value === 'number' ? value : undefined;
20
+ }
21
+ /** The values a titled single-select (`oneOf: [{ const, title }]`) allows. */
22
+ function titledChoices(schema) {
23
+ const choices = schema.oneOf ?? schema.anyOf;
24
+ if (!Array.isArray(choices)) {
25
+ return undefined;
26
+ }
27
+ return choices.filter(isRecord).map((choice) => choice.const);
28
+ }
29
+ function checkString(name, schema, value) {
30
+ if (typeof value !== 'string') {
31
+ return `'${name}' must be a string`;
32
+ }
33
+ // JSON Schema counts characters as code points, not UTF-16 units.
34
+ const length = [
35
+ ...value,
36
+ ].length;
37
+ const min = numberAt(schema, 'minLength');
38
+ if (min !== undefined && length < min) {
39
+ return `'${name}' must be at least ${min} characters`;
40
+ }
41
+ const max = numberAt(schema, 'maxLength');
42
+ if (max !== undefined && length > max) {
43
+ return `'${name}' must be at most ${max} characters`;
44
+ }
45
+ const pattern = schema.pattern;
46
+ if (typeof pattern === 'string' && !new RegExp(pattern, 'u').test(value)) {
47
+ return `'${name}' must match ${pattern}`;
48
+ }
49
+ const choices = titledChoices(schema);
50
+ if (choices && !choices.includes(value)) {
51
+ return `'${name}' must be one of ${JSON.stringify(choices)}`;
52
+ }
53
+ return undefined;
54
+ }
55
+ function checkRange(name, schema, value) {
56
+ const min = numberAt(schema, 'minimum');
57
+ if (min !== undefined && value < min) {
58
+ return `'${name}' must be at least ${min}`;
59
+ }
60
+ const max = numberAt(schema, 'maximum');
61
+ if (max !== undefined && value > max) {
62
+ return `'${name}' must be at most ${max}`;
63
+ }
64
+ return undefined;
65
+ }
66
+ function checkNumber(name, schema, value) {
67
+ if (typeof value !== 'number' || !Number.isFinite(value)) {
68
+ return `'${name}' must be a number`;
69
+ }
70
+ return checkRange(name, schema, value);
71
+ }
72
+ function checkInteger(name, schema, value) {
73
+ if (typeof value !== 'number' || !Number.isInteger(value)) {
74
+ return `'${name}' must be an integer`;
75
+ }
76
+ return checkRange(name, schema, value);
77
+ }
78
+ function checkBoolean(name, _schema, value) {
79
+ return typeof value === 'boolean' ? undefined : `'${name}' must be a boolean`;
80
+ }
81
+ /** The choices an array's items may take: `items.enum`, or `items.anyOf[].const`. */
82
+ function itemChoices(schema) {
83
+ const items = schema.items;
84
+ if (!isRecord(items)) {
85
+ return undefined;
86
+ }
87
+ if (Array.isArray(items.enum)) {
88
+ return items.enum;
89
+ }
90
+ if (!Array.isArray(items.anyOf)) {
91
+ return undefined;
92
+ }
93
+ return items.anyOf.filter(isRecord).map((choice) => choice.const);
94
+ }
95
+ function checkArray(name, schema, value) {
96
+ // `Array.from` fills holes with `undefined`, which `every` would skip.
97
+ if (!Array.isArray(value) || !Array.from(value).every((item) => typeof item === 'string')) {
98
+ return `'${name}' must be a list of strings`;
99
+ }
100
+ const min = numberAt(schema, 'minItems');
101
+ if (min !== undefined && value.length < min) {
102
+ return `'${name}' must have at least ${min} items`;
103
+ }
104
+ const max = numberAt(schema, 'maxItems');
105
+ if (max !== undefined && value.length > max) {
106
+ return `'${name}' must have at most ${max} items`;
107
+ }
108
+ const choices = itemChoices(schema);
109
+ if (choices && !value.every((item) => choices.includes(item))) {
110
+ return `'${name}' items must be among ${JSON.stringify(choices)}`;
111
+ }
112
+ return undefined;
113
+ }
114
+ const TYPE_CHECKS = {
115
+ string: checkString,
116
+ number: checkNumber,
117
+ integer: checkInteger,
118
+ boolean: checkBoolean,
119
+ array: checkArray,
120
+ };
121
+ /** Why `value` does not fit the property `name`'s schema, or undefined when it does. */
122
+ function propertyProblem(name, schema, value) {
123
+ if (!isRecord(schema)) {
124
+ return undefined;
125
+ }
126
+ const type = schema.type;
127
+ const check = typeof type === 'string' ? TYPE_CHECKS[type] : undefined;
128
+ const problem = check?.(name, schema, value);
129
+ if (problem) {
130
+ return problem;
131
+ }
132
+ const choices = schema.enum;
133
+ if (Array.isArray(choices) && !choices.includes(value)) {
134
+ return `'${name}' must be one of ${JSON.stringify(choices)}`;
135
+ }
136
+ return undefined;
137
+ }
138
+ function requiredOf(schema) {
139
+ const required = schema.required;
140
+ if (!Array.isArray(required)) {
141
+ return [];
142
+ }
143
+ return required.filter((name) => typeof name === 'string');
144
+ }
145
+ //#endregion
146
+ //#region Public API
147
+ /**
148
+ * Why `answer` does not fit `question`'s form, or undefined when it does (or the
149
+ * question has no form). Only the answer's own fields count.
150
+ * @internal
151
+ */
152
+ export function questionAnswerProblem(question, answer) {
153
+ const schema = question?.schema;
154
+ if (!schema) {
155
+ return undefined;
156
+ }
157
+ if (!isRecord(answer)) {
158
+ return 'the answer must be an object of the form’s fields';
159
+ }
160
+ for (const name of requiredOf(schema)) {
161
+ if (!Object.hasOwn(answer, name) || answer[name] === undefined) {
162
+ return `'${name}' is required`;
163
+ }
164
+ }
165
+ const properties = isRecord(schema.properties) ? schema.properties : {};
166
+ const closed = schema.additionalProperties === false;
167
+ for (const [name, value] of Object.entries(answer)) {
168
+ const described = Object.hasOwn(properties, name);
169
+ if (!described && closed) {
170
+ return `'${name}' is not a field of the form`;
171
+ }
172
+ const problem = described ? propertyProblem(name, properties[name], value) : undefined;
173
+ if (problem) {
174
+ return problem;
175
+ }
176
+ }
177
+ return undefined;
178
+ }
179
+ //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noetic-tools/core",
3
- "version": "5.22.0-next.5",
3
+ "version": "5.22.0-next.7",
4
4
  "type": "module",
5
5
  "description": "Step primitives, interpreter, runtime, and context layer contract for Noetic agents.",
6
6
  "license": "Apache-2.0",
@@ -69,7 +69,7 @@
69
69
  },
70
70
  "dependencies": {
71
71
  "@noetic-tools/context": "^2.0.0-next.1",
72
- "@noetic-tools/types": "^3.19.0-next.4",
72
+ "@noetic-tools/types": "^3.19.0-next.6",
73
73
  "@openrouter/agent": "^0.11.0",
74
74
  "@openrouter/sdk": "1.4.25",
75
75
  "jsonata": "2.2.2",
@@ -2879,6 +2879,10 @@
2879
2879
  "type": "string",
2880
2880
  "minLength": 1
2881
2881
  },
2882
+ "instructions": {
2883
+ "type": "string",
2884
+ "minLength": 1
2885
+ },
2882
2886
  "cwd": {
2883
2887
  "type": "string",
2884
2888
  "minLength": 1
@@ -3153,6 +3157,9 @@
3153
3157
  },
3154
3158
  "terminal": {
3155
3159
  "type": "boolean"
3160
+ },
3161
+ "elicitation": {
3162
+ "type": "boolean"
3156
3163
  }
3157
3164
  },
3158
3165
  "additionalProperties": false