@orkestrel/toolbox 0.0.21 → 0.0.23

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.
@@ -8,7 +8,7 @@ let _orkestrel_agent = require("@orkestrel/agent");
8
8
  let _orkestrel_workspace = require("@orkestrel/workspace");
9
9
  let _orkestrel_tool = require("@orkestrel/tool");
10
10
  let _orkestrel_form = require("@orkestrel/form");
11
- //#region src/core/constants.ts
11
+ // src/core/constants.ts
12
12
  /**
13
13
  * Holds the name {@link import('./factories.js').createAgentTool} advertises by default, `'agent'`
14
14
  * — the key a model calls and the `ToolManagerInterface` (`@orkestrel/tool`) registers under.
@@ -581,8 +581,7 @@ var INFER_TOOL_DESCRIPTION = [
581
581
  ]
582
582
  })}`
583
583
  ].join("\n");
584
- //#endregion
585
- //#region src/core/errors.ts
584
+ // src/core/errors.ts
586
585
  /**
587
586
  * Represents a package-owned tool-call failure: malformed input or unresolved configuration (`TOOL`),
588
587
  * delegation depth/cycle rejection (`DEPTH`), prompt failure (`DEADLOCK` / `EXPIRE` / `ANSWER`),
@@ -610,6 +609,7 @@ var ToolboxError = class extends Error {
610
609
  code;
611
610
  constructor(code, message, context) {
612
611
  super(message);
612
+ _orkestrel_contract.INTRINSICS.define(this, Symbol.for("@orkestrel/toolbox.ToolboxError"), { value: this });
613
613
  this.name = "ToolboxError";
614
614
  this.code = code;
615
615
  if (context !== void 0) this.context = context;
@@ -618,6 +618,10 @@ var ToolboxError = class extends Error {
618
618
  /**
619
619
  * Narrows an unknown caught value to a {@link ToolboxError}.
620
620
  *
621
+ * @remarks
622
+ * Recognizes the self-brand across package copies and subclasses without requiring a name.
623
+ * Rejects transparent proxies and validates the code and optional context.
624
+ *
621
625
  * @param value - The value to test (typically a `catch` binding)
622
626
  * @returns True if `value` is an {@link ToolboxError}; false otherwise
623
627
  *
@@ -633,10 +637,14 @@ var ToolboxError = class extends Error {
633
637
  * ```
634
638
  */
635
639
  function isToolboxError(value) {
636
- return (0, _orkestrel_contract.isInstance)(value, ToolboxError);
640
+ return (0, _orkestrel_contract.holds)(() => {
641
+ if (!(value instanceof Error && _orkestrel_contract.INTRINSICS.prototype(value) !== Error.prototype && _orkestrel_contract.INTRINSICS.describe(value, Symbol.for("@orkestrel/toolbox.ToolboxError"))?.value === value)) return false;
642
+ const code = _orkestrel_contract.INTRINSICS.reflect.read(value, "code");
643
+ const context = _orkestrel_contract.INTRINSICS.reflect.read(value, "context");
644
+ return (code === "TOOL" || code === "DEPTH" || code === "DEADLOCK" || code === "EXPIRE" || code === "ANSWER" || code === "DATABASE" || code === "RELATION") && (context === void 0 || (0, _orkestrel_contract.isObject)(context) || (0, _orkestrel_contract.isFunction)(context));
645
+ });
637
646
  }
638
- //#endregion
639
- //#region src/core/validators.ts
647
+ // src/core/validators.ts
640
648
  /**
641
649
  * Narrows an unknown value to a valid alternating workflow lineage.
642
650
  *
@@ -726,8 +734,7 @@ function isDatabaseDefinition(value) {
726
734
  if (value.version !== void 0 && !(0, _orkestrel_contract.isFiniteNumber)(value.version)) return false;
727
735
  return true;
728
736
  }
729
- //#endregion
730
- //#region src/core/shapers.ts
737
+ // src/core/shapers.ts
731
738
  /**
732
739
  * Describes the shape of {@link import('./factories.js').createPromptTool}'s call arguments — `to` names the
733
740
  * terminal identity and `schema` carries the complete multi-field form document.
@@ -1309,8 +1316,7 @@ var inferToolShape = (0, _orkestrel_contract.objectShape)({
1309
1316
  enum: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.booleanShape)({ description: "Infer enum constraints from repeated literal values. Defaults to false." })),
1310
1317
  candidates: (0, _orkestrel_contract.optionalShape)((0, _orkestrel_contract.arrayShape)((0, _orkestrel_contract.jsonShape)(), { description: "Optional values to check against the freshly inferred schema. When present, the tool returns a per-candidate verdict (strict — no coercion) alongside the inferred parameters." }))
1311
1318
  });
1312
- //#endregion
1313
- //#region src/core/helpers.ts
1319
+ // src/core/helpers.ts
1314
1320
  /**
1315
1321
  * Returns the ancestry identifier of a workflow in a run chain — `workflow:<id>`.
1316
1322
  *
@@ -1556,51 +1562,46 @@ function inferRelationCode(error) {
1556
1562
  * ```
1557
1563
  */
1558
1564
  function expandInclude(paths, depth) {
1559
- let include = {};
1560
- for (const path of paths ?? []) {
1565
+ return Iterator.from(paths ?? []).reduce((include, path) => {
1561
1566
  const segments = path.split(".");
1562
1567
  if (segments.length > depth || segments.some((segment) => segment.length === 0)) throw new ToolboxError("TOOL", `malformed include path '${path}'`, {
1563
1568
  path,
1564
1569
  depth
1565
1570
  });
1566
- const ancestors = [];
1567
- let branch = include;
1571
+ const ancestors = [include];
1568
1572
  const last = segments.length - 1;
1569
- for (let index = 0; index < last; index++) {
1570
- const segment = segments[index];
1571
- if (segment === void 0) throw new ToolboxError("TOOL", `malformed include path '${path}'`, {
1573
+ for (const [index, segment] of segments.entries()) {
1574
+ if (index === last) break;
1575
+ const branch = ancestors.at(-1);
1576
+ if (branch === void 0) throw new ToolboxError("TOOL", `malformed include path '${path}'`, {
1572
1577
  path,
1573
1578
  depth
1574
1579
  });
1575
- ancestors.push(branch);
1576
1580
  const existing = branch[segment];
1577
- branch = (0, _orkestrel_contract.isObject)(existing) ? existing : {};
1581
+ ancestors.push((0, _orkestrel_contract.isObject)(existing) ? existing : {});
1578
1582
  }
1579
1583
  const leaf = segments[last];
1580
- if (leaf === void 0) throw new ToolboxError("TOOL", `malformed include path '${path}'`, {
1584
+ const branch = ancestors.at(-1);
1585
+ if (leaf === void 0 || branch === void 0) throw new ToolboxError("TOOL", `malformed include path '${path}'`, {
1581
1586
  path,
1582
1587
  depth
1583
1588
  });
1584
1589
  const existing = branch[leaf];
1585
- let merged = {
1586
- ...branch,
1587
- [leaf]: existing === void 0 ? true : existing
1588
- };
1589
- for (let index = last - 1; index >= 0; index--) {
1590
- const ancestor = ancestors[index];
1590
+ return ancestors.slice(0, -1).reduceRight((merged, ancestor, index) => {
1591
1591
  const segment = segments[index];
1592
- if (ancestor === void 0 || segment === void 0) throw new ToolboxError("TOOL", `malformed include path '${path}'`, {
1592
+ if (segment === void 0) throw new ToolboxError("TOOL", `malformed include path '${path}'`, {
1593
1593
  path,
1594
1594
  depth
1595
1595
  });
1596
- merged = {
1596
+ return {
1597
1597
  ...ancestor,
1598
1598
  [segment]: merged
1599
1599
  };
1600
- }
1601
- include = merged;
1602
- }
1603
- return include;
1600
+ }, {
1601
+ ...branch,
1602
+ [leaf]: existing === void 0 ? true : existing
1603
+ });
1604
+ }, {});
1604
1605
  }
1605
1606
  /**
1606
1607
  * Resolves which registered {@link RelationManagerInterface} a relation-tool call addresses — the
@@ -1736,8 +1737,28 @@ function clampQuery(query, cap) {
1736
1737
  limit
1737
1738
  };
1738
1739
  }
1739
- //#endregion
1740
- //#region src/core/compilers.ts
1740
+ /**
1741
+ * Resolves an owned workflow input through the supplied flat or draft contracts.
1742
+ * @example
1743
+ * ```ts
1744
+ * import { createContract } from '@orkestrel/contract'
1745
+ * import { completeDraft, createWorkflowDraftContract, resolveWorkflowTarget, workflowStepsShape } from '@src/core'
1746
+ *
1747
+ * const definition = completeDraft({ phases: [{ tasks: [{}] }] })
1748
+ * const steps: import('@orkestrel/contract').ContractInterface<import('./types.js').WorkflowSteps> = createContract(workflowStepsShape)
1749
+ * resolveWorkflowTarget({}, definition, steps, createWorkflowDraftContract()) // definition
1750
+ * ```
1751
+ */
1752
+ function resolveWorkflowTarget(owned, definition, steps, draft) {
1753
+ if (Object.keys(owned).length === 0) return definition;
1754
+ if ((0, _orkestrel_contract.isArray)(owned.steps)) {
1755
+ const flat = steps.parse(owned);
1756
+ return flat === void 0 ? void 0 : expandSteps(flat);
1757
+ }
1758
+ const parsed = draft.parse(owned);
1759
+ return parsed === void 0 ? void 0 : completeDraft(parsed);
1760
+ }
1761
+ // src/core/compilers.ts
1741
1762
  /**
1742
1763
  * Compiles a {@link TableSpec} into the `@orkestrel/database` {@link TableMap} it configures —
1743
1764
  * each {@link ColumnSpec} maps to the matching primitive shaper (`'string'` → `stringShape()`,
@@ -1782,8 +1803,7 @@ function compileColumnPrimitive(primitive) {
1782
1803
  if (primitive === "number") return (0, _orkestrel_contract.numberShape)();
1783
1804
  return (0, _orkestrel_contract.booleanShape)();
1784
1805
  }
1785
- //#endregion
1786
- //#region src/core/stores/MemoryDefinitionStore.ts
1806
+ // src/core/stores/MemoryDefinitionStore.ts
1787
1807
  /**
1788
1808
  * Represents the in-memory {@link DefinitionStoreInterface} — a process-lifetime `Map` of
1789
1809
  * {@link DatabaseDefinition}s keyed by database id, the default store
@@ -1853,8 +1873,7 @@ var MemoryDefinitionStore = class {
1853
1873
  return Promise.resolve();
1854
1874
  }
1855
1875
  };
1856
- //#endregion
1857
- //#region src/core/stores/DatabaseDefinitionStore.ts
1876
+ // src/core/stores/DatabaseDefinitionStore.ts
1858
1877
  /**
1859
1878
  * Represents a {@link DefinitionStoreInterface} backed by one table of the `@orkestrel/database` layer — a
1860
1879
  * database's durable config state is a row, so persistence reduces to keyed point-access
@@ -1942,8 +1961,7 @@ var DatabaseDefinitionStore = class {
1942
1961
  await this.#table.remove(id);
1943
1962
  }
1944
1963
  };
1945
- //#endregion
1946
- //#region src/core/databases/DatabaseResolver.ts
1964
+ // src/core/databases/DatabaseResolver.ts
1947
1965
  /**
1948
1966
  * Resolves a database definition into a cached live handle for a database tool, over the tool's
1949
1967
  * live handles, its stored definitions, its driver registry, and an optional key generator.
@@ -2046,8 +2064,7 @@ var DatabaseResolver = class {
2046
2064
  throw new ToolboxError("TOOL", `unknown database '${id}'`, { id });
2047
2065
  }
2048
2066
  };
2049
- //#endregion
2050
- //#region src/core/factories.ts
2067
+ // src/core/factories.ts
2051
2068
  /**
2052
2069
  * Wraps a registered tool as a {@link WorkflowFunction} (`@orkestrel/workflow`) — the opt-in
2053
2070
  * adapter that lets a `function`-form task run a `@orkestrel/tool` tool by name.
@@ -2336,15 +2353,7 @@ function createWorkflowTool(definition, runner, options) {
2336
2353
  const cloned = (0, _orkestrel_contract.attempt)(() => (0, _orkestrel_contract.cloneJSONRecord)(args));
2337
2354
  if (!cloned.success) throw new ToolboxError("TOOL", "malformed workflow definition", { workflow: definition.id });
2338
2355
  const owned = cloned.value;
2339
- let target;
2340
- if (Object.keys(owned).length === 0) target = definition;
2341
- else if ((0, _orkestrel_contract.isArray)(owned.steps)) {
2342
- const flat = steps.parse(owned);
2343
- target = flat === void 0 ? void 0 : expandSteps(flat);
2344
- } else {
2345
- const parsed = draft.parse(owned);
2346
- target = parsed === void 0 ? void 0 : completeDraft(parsed);
2347
- }
2356
+ const target = resolveWorkflowTarget(owned, definition, steps, draft);
2348
2357
  if (target === void 0 || !strict.is(target)) throw new ToolboxError("TOOL", "malformed workflow definition", { workflow: definition.id });
2349
2358
  const tag = tagWorkflow(target.id);
2350
2359
  if (lineage.includes(tag)) throw new ToolboxError("DEPTH", `workflow '${target.id}' is already an ancestor (cycle)`, {
@@ -2453,7 +2462,7 @@ function createWorkspaceTool(options) {
2453
2462
  state: file.state,
2454
2463
  size: file.size,
2455
2464
  lines: file.lines,
2456
- kind: (0, _orkestrel_workspace.isText)(file.content) ? "text" : "binary"
2465
+ kind: (0, _orkestrel_workspace.isTextContent)(file.content) ? "text" : "binary"
2457
2466
  }));
2458
2467
  case "has": return active?.has(op.path) ?? false;
2459
2468
  case "search": return active?.search(op.query, {
@@ -3386,7 +3395,6 @@ function createEndpointTool(definition, options) {
3386
3395
  }
3387
3396
  });
3388
3397
  }
3389
- //#endregion
3390
3398
  exports.AGENT_TOOL_DEPTH = AGENT_TOOL_DEPTH;
3391
3399
  exports.AGENT_TOOL_DESCRIPTION = AGENT_TOOL_DESCRIPTION;
3392
3400
  exports.AGENT_TOOL_NAME = AGENT_TOOL_NAME;
@@ -3484,6 +3492,7 @@ exports.relationToolShape = relationToolShape;
3484
3492
  exports.resolveLimit = resolveLimit;
3485
3493
  exports.resolveRelationManager = resolveRelationManager;
3486
3494
  exports.resolveRelationModel = resolveRelationModel;
3495
+ exports.resolveWorkflowTarget = resolveWorkflowTarget;
3487
3496
  exports.rowShape = rowShape;
3488
3497
  exports.rowsShape = rowsShape;
3489
3498
  exports.singleKeyShape = singleKeyShape;