@aaac/runtime 0.2.8 → 0.2.9

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.
package/dist/index.js CHANGED
@@ -12,9 +12,10 @@ import {
12
12
  loadDslContext,
13
13
  resolveArtifacts,
14
14
  resolveGuardrailContext,
15
+ resolveHandoffSchemas,
15
16
  resolveNavigationDsl,
16
17
  resolveNavigationSources
17
- } from "./chunk-CPSXQCNA.js";
18
+ } from "./chunk-CJLKKDKN.js";
18
19
  import {
19
20
  jsonSchemaToZod
20
21
  } from "./chunk-ET6TGQRT.js";
@@ -9604,12 +9605,10 @@ var GATE_SCHEMA = {
9604
9605
  },
9605
9606
  required: ["approved"]
9606
9607
  };
9607
- function resolveHandoffSchema(task, handoffTypes, components) {
9608
+ function resultHandoffSchema(task, reg) {
9608
9609
  const id = task.result_handoff;
9609
- if (!id) return void 0;
9610
- const ht = handoffTypes[id];
9611
- if (!ht?.schema) return void 0;
9612
- return resolveSchemaRefs(ht.schema, components);
9610
+ if (!id || !Object.prototype.hasOwnProperty.call(reg.handoffSchemas, id)) return void 0;
9611
+ return reg.handoffSchemas[id];
9613
9612
  }
9614
9613
  function toTaskContract(id, raw) {
9615
9614
  return {
@@ -9626,13 +9625,10 @@ function buildRegistries(dsl, modelMapping) {
9626
9625
  const agents = buildAgentRegistry(dsl);
9627
9626
  const tasks = {};
9628
9627
  for (const [id, t] of Object.entries(rawTasks)) tasks[id] = toTaskContract(id, t);
9629
- const componentsRoot = dsl.components ?? {};
9630
- const components = componentsRoot.schemas ?? componentsRoot;
9631
9628
  return {
9632
9629
  agents,
9633
9630
  tasks,
9634
- handoffTypes: dsl.handoff_types ?? {},
9635
- components,
9631
+ handoffSchemas: resolveHandoffSchemas(dsl, resolveSchemaRefs),
9636
9632
  // The runtime's resolver, so a class resolves to the model it runs on when
9637
9633
  // the runtime executes the same step. No environment: `AGENT_RUNTIME_MODEL*`
9638
9634
  // override a run, and a committed artifact that depended on the generating
@@ -9703,28 +9699,125 @@ function priorContextExpr(i, steps, deps) {
9703
9699
  if (fragments.length === 0) return "";
9704
9700
  return fragments.join(" + ");
9705
9701
  }
9706
- function emitWorkflowInputHelper(pad) {
9702
+ function emitWorkflowInputOutputHelper(pad) {
9707
9703
  const id = WORKFLOW_INPUT_STEP_ID;
9708
9704
  return [
9709
- `${pad}const _wfInput = (args) => {`,
9710
- `${pad} if (args === undefined || args === null) return ''`,
9711
- `${pad} let output`,
9712
- `${pad} if (typeof args === 'string') {`,
9713
- `${pad} output = { user_request: args }`,
9714
- `${pad} } else if (typeof args === 'object' && ('user_request' in args || 'handoff' in args)) {`,
9705
+ `${pad}const _wfInputOutput = (args) => {`,
9706
+ `${pad} if (args === undefined || args === null) return undefined`,
9707
+ `${pad} if (typeof args === 'string') return { user_request: args }`,
9708
+ `${pad} if (typeof args === 'object' && ('user_request' in args || 'handoff' in args)) {`,
9715
9709
  `${pad} const h = args.handoff`,
9716
9710
  `${pad} const userRequest = typeof args.user_request === 'string'`,
9717
9711
  `${pad} ? args.user_request`,
9718
9712
  `${pad} : h`,
9719
9713
  `${pad} ? (typeof h.payload === 'string' ? h.payload : JSON.stringify(h.payload, null, 2))`,
9720
9714
  `${pad} : ''`,
9721
- `${pad} output = h`,
9715
+ `${pad} return h`,
9722
9716
  `${pad} ? { user_request: userRequest, handoff: (h.version !== undefined ? { type: h.type, version: h.version, payload: h.payload } : { type: h.type, payload: h.payload }) }`,
9723
9717
  `${pad} : { user_request: userRequest }`,
9724
- `${pad} } else {`,
9725
- `${pad} output = { user_request: JSON.stringify(args, null, 2), handoff: { type: ${jsString(id)}, payload: args } }`,
9726
9718
  `${pad} }`,
9727
- `${pad} return '\\n\\n--- Step: ${id} ---\\n' + JSON.stringify(output, null, 2)`,
9719
+ `${pad} return { user_request: JSON.stringify(args, null, 2), handoff: { type: ${jsString(id)}, payload: args } }`,
9720
+ `${pad}}`
9721
+ ];
9722
+ }
9723
+ function emitWorkflowInputHelper(pad) {
9724
+ const id = WORKFLOW_INPUT_STEP_ID;
9725
+ return [
9726
+ `${pad}const _wfInput = (args) => {`,
9727
+ `${pad} const output = _wfInputOutput(args)`,
9728
+ `${pad} return output === undefined ? '' : '\\n\\n--- Step: ${id} ---\\n' + JSON.stringify(output, null, 2)`,
9729
+ `${pad}}`
9730
+ ];
9731
+ }
9732
+ function emitInputValidation(pad, schemas) {
9733
+ const schemasLiteral = jsLiteral(schemas, 0).replace(/\n/g, `
9734
+ ${pad}`);
9735
+ return [
9736
+ `${pad}const _HANDOFF_SCHEMAS = ${schemasLiteral}`,
9737
+ `${pad}const _isObject = (v) => typeof v === 'object' && v !== null && !Array.isArray(v)`,
9738
+ `${pad}const _shapeOf = (s) => {`,
9739
+ `${pad} if (!_isObject(s) || s.enum || s.const !== undefined) return null`,
9740
+ `${pad} if (s.type === 'object') {`,
9741
+ `${pad} if (!s.properties) return null`,
9742
+ `${pad} const req = new Set(s.required ?? [])`,
9743
+ `${pad} const shape = {}`,
9744
+ `${pad} for (const [k, ps] of Object.entries(s.properties)) shape[k] = { s: ps, required: req.has(k) }`,
9745
+ `${pad} return shape`,
9746
+ `${pad} }`,
9747
+ `${pad} if (['string', 'integer', 'number', 'boolean', 'array'].includes(s.type) || !s.allOf || s.allOf.length === 0) return null`,
9748
+ `${pad} let shape = _shapeOf(s.allOf[0])`,
9749
+ `${pad} if (!shape) return null`,
9750
+ `${pad} for (const part of s.allOf.slice(1)) {`,
9751
+ `${pad} const next = _shapeOf(part)`,
9752
+ `${pad} if (next) shape = { ...shape, ...next }`,
9753
+ `${pad} }`,
9754
+ `${pad} return shape`,
9755
+ `${pad}}`,
9756
+ `${pad}const _conformShape = (shape, v, p) => {`,
9757
+ `${pad} if (!_isObject(v)) return p + ': expected object'`,
9758
+ `${pad} for (const [k, f] of Object.entries(shape)) {`,
9759
+ `${pad} const has = Object.prototype.hasOwnProperty.call(v, k)`,
9760
+ `${pad} if (f.required ? !has : (!has || v[k] === undefined)) {`,
9761
+ `${pad} if (f.required) return p + '.' + k + ': required'`,
9762
+ `${pad} continue`,
9763
+ `${pad} }`,
9764
+ `${pad} const e = _conform(f.s, v[k], p + '.' + k)`,
9765
+ `${pad} if (e) return e`,
9766
+ `${pad} }`,
9767
+ `${pad} return ''`,
9768
+ `${pad}}`,
9769
+ `${pad}const _conform = (s, v, p) => {`,
9770
+ `${pad} if (!_isObject(s)) return ''`,
9771
+ `${pad} if (s.enum) {`,
9772
+ `${pad} if (s.enum.length === 0) return ''`,
9773
+ `${pad} return typeof v === 'string' && s.enum.includes(v) ? '' : p + ': expected one of ' + JSON.stringify(s.enum)`,
9774
+ `${pad} }`,
9775
+ `${pad} if (s.const !== undefined) {`,
9776
+ `${pad} const c = s.const`,
9777
+ `${pad} if (typeof c !== 'string' && typeof c !== 'number' && typeof c !== 'boolean') return ''`,
9778
+ `${pad} return v === c ? '' : p + ': expected ' + JSON.stringify(c)`,
9779
+ `${pad} }`,
9780
+ `${pad} const t = s.type`,
9781
+ `${pad} if (t === 'string') {`,
9782
+ `${pad} if (typeof v !== 'string') return p + ': expected string'`,
9783
+ `${pad} if (typeof s.minLength === 'number' && v.length < s.minLength) return p + ': shorter than ' + s.minLength`,
9784
+ `${pad} if (typeof s.maxLength === 'number' && v.length > s.maxLength) return p + ': longer than ' + s.maxLength`,
9785
+ `${pad} if (typeof s.pattern === 'string' && !new RegExp(s.pattern).test(v)) return p + ': does not match ' + s.pattern`,
9786
+ `${pad} return ''`,
9787
+ `${pad} }`,
9788
+ `${pad} if (t === 'integer' || t === 'number') {`,
9789
+ `${pad} if (typeof v !== 'number' || !Number.isFinite(v) || (t === 'integer' && !Number.isSafeInteger(v))) return p + ': expected ' + t`,
9790
+ `${pad} if (typeof s.minimum === 'number' && v < s.minimum) return p + ': less than ' + s.minimum`,
9791
+ `${pad} if (typeof s.maximum === 'number' && v > s.maximum) return p + ': greater than ' + s.maximum`,
9792
+ `${pad} return ''`,
9793
+ `${pad} }`,
9794
+ `${pad} if (t === 'boolean') return typeof v === 'boolean' ? '' : p + ': expected boolean'`,
9795
+ `${pad} if (t === 'array') {`,
9796
+ `${pad} if (!Array.isArray(v)) return p + ': expected array'`,
9797
+ `${pad} if (s.items) {`,
9798
+ `${pad} for (let i = 0; i < v.length; i++) {`,
9799
+ `${pad} const e = _conform(s.items, v[i], p + '[' + i + ']')`,
9800
+ `${pad} if (e) return e`,
9801
+ `${pad} }`,
9802
+ `${pad} }`,
9803
+ `${pad} if (typeof s.minItems === 'number' && v.length < s.minItems) return p + ': fewer than ' + s.minItems + ' items'`,
9804
+ `${pad} if (typeof s.maxItems === 'number' && v.length > s.maxItems) return p + ': more than ' + s.maxItems + ' items'`,
9805
+ `${pad} return ''`,
9806
+ `${pad} }`,
9807
+ `${pad} if (t === 'object') {`,
9808
+ `${pad} if (!_isObject(v)) return p + ': expected object'`,
9809
+ `${pad} return s.properties ? _conformShape(_shapeOf(s), v, p) : ''`,
9810
+ `${pad} }`,
9811
+ `${pad} if (!s.allOf || s.allOf.length === 0) return ''`,
9812
+ `${pad} const shape = _shapeOf(s)`,
9813
+ `${pad} return shape ? _conformShape(shape, v, p) : _conform(s.allOf[0], v, p)`,
9814
+ `${pad}}`,
9815
+ `${pad}{`,
9816
+ `${pad} const h = _wfInputOutput(args)?.handoff`,
9817
+ `${pad} if (h && Object.prototype.hasOwnProperty.call(_HANDOFF_SCHEMAS, h.type)) {`,
9818
+ `${pad} const e = _conform(_HANDOFF_SCHEMAS[h.type], h.payload, 'payload')`,
9819
+ `${pad} if (e) throw new Error('Input handoff validation failed for "' + h.type + '": ' + e)`,
9820
+ `${pad} }`,
9728
9821
  `${pad}}`
9729
9822
  ];
9730
9823
  }
@@ -9762,7 +9855,7 @@ function resolveStepAgentTask(step, reg, kind) {
9762
9855
  function emitDelegateExpr(step, reg, i, steps, deps, indent, feedback) {
9763
9856
  const { agent, task } = resolveStepAgentTask(step, reg, "delegate");
9764
9857
  const prompt = renderDelegatePrompt(agent, task, step);
9765
- const schema = resolveHandoffSchema(task, reg.handoffTypes, reg.components);
9858
+ const schema = resultHandoffSchema(task, reg);
9766
9859
  const promptExpr = withPriorContext(jsLiteral(prompt), priorContextExpr(i, steps, deps), feedback);
9767
9860
  const opts = agentOpts(task, step, reg, schema, indent);
9768
9861
  return `agent(${promptExpr}, ${opts})`;
@@ -9807,7 +9900,7 @@ ${pad} `)},`);
9807
9900
  function emitEvaluateAgentCall(step, reg, i, steps, deps, pad, feedback) {
9808
9901
  const { agent, task } = resolveStepAgentTask(step, reg, "evaluate");
9809
9902
  const prompt = renderDelegatePrompt(agent, task, step);
9810
- const schema = resolveHandoffSchema(task, reg.handoffTypes, reg.components);
9903
+ const schema = resultHandoffSchema(task, reg);
9811
9904
  const promptExpr = withPriorContext(jsLiteral(prompt), priorContextExpr(i, steps, deps), feedback);
9812
9905
  const opts = {};
9813
9906
  if (schema) opts.schema = schema;
@@ -9923,15 +10016,22 @@ function renderWorkflowScript(id, wf, dsl, modelMapping) {
9923
10016
  const usesWorkflowInput = steps.some(
9924
10017
  (step) => stepDependsOnWorkflowInput(step.depends_on)
9925
10018
  );
10019
+ const validatesInput = Object.keys(reg.handoffSchemas).length > 0;
9926
10020
  const body = [];
9927
10021
  if (usesStepContext) {
9928
10022
  body.push(
9929
10023
  " const _ctx = (n, id, r) => '\\n\\n--- Step ' + n + ': ' + id + ' ---\\n' + (typeof r === 'string' ? r : JSON.stringify(r, null, 2))"
9930
10024
  );
9931
10025
  }
10026
+ if (usesWorkflowInput || validatesInput) {
10027
+ body.push(...emitWorkflowInputOutputHelper(" "));
10028
+ }
9932
10029
  if (usesWorkflowInput) {
9933
10030
  body.push(...emitWorkflowInputHelper(" "));
9934
10031
  }
10032
+ if (validatesInput) {
10033
+ body.push(...emitInputValidation(" ", reg.handoffSchemas));
10034
+ }
9935
10035
  const emittedWaves = /* @__PURE__ */ new Set();
9936
10036
  for (let i = 0; i < steps.length; i++) {
9937
10037
  if (spanOwner.has(i) && spanOwner.get(i) !== i) continue;