@noetic-tools/core 0.0.1

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.
Files changed (207) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +38 -0
  3. package/dist/adapters/in-memory-fs-adapter.d.ts +3 -0
  4. package/dist/adapters/in-memory-fs-adapter.js +245 -0
  5. package/dist/adapters/in-memory-shell-adapter.d.ts +9 -0
  6. package/dist/adapters/in-memory-shell-adapter.js +20 -0
  7. package/dist/adapters/in-memory-subprocess-adapter.d.ts +5 -0
  8. package/dist/adapters/in-memory-subprocess-adapter.js +126 -0
  9. package/dist/adapters/local-fs-adapter.d.ts +3 -0
  10. package/dist/adapters/local-fs-adapter.js +43 -0
  11. package/dist/adapters/local-shell-adapter.d.ts +51 -0
  12. package/dist/adapters/local-shell-adapter.js +167 -0
  13. package/dist/adapters/local-subprocess-adapter.d.ts +16 -0
  14. package/dist/adapters/local-subprocess-adapter.js +187 -0
  15. package/dist/adapters/openrouter.d.ts +57 -0
  16. package/dist/adapters/openrouter.js +248 -0
  17. package/dist/builders/channel-builder.d.ts +36 -0
  18. package/dist/builders/channel-builder.js +15 -0
  19. package/dist/builders/control-flow-builders.d.ts +79 -0
  20. package/dist/builders/control-flow-builders.js +53 -0
  21. package/dist/builders/every.d.ts +45 -0
  22. package/dist/builders/every.js +60 -0
  23. package/dist/builders/layer-provides-builders.d.ts +32 -0
  24. package/dist/builders/layer-provides-builders.js +33 -0
  25. package/dist/builders/loop-builder.d.ts +23 -0
  26. package/dist/builders/loop-builder.js +46 -0
  27. package/dist/builders/memory-builder.d.ts +11 -0
  28. package/dist/builders/memory-builder.js +15 -0
  29. package/dist/builders/provide-builder.d.ts +20 -0
  30. package/dist/builders/provide-builder.js +34 -0
  31. package/dist/builders/spawn-builder.d.ts +20 -0
  32. package/dist/builders/spawn-builder.js +33 -0
  33. package/dist/builders/step-builders.d.ts +62 -0
  34. package/dist/builders/step-builders.js +99 -0
  35. package/dist/builders/tool-builder.d.ts +52 -0
  36. package/dist/builders/tool-builder.js +59 -0
  37. package/dist/conditions/conditions.d.ts +114 -0
  38. package/dist/conditions/conditions.js +271 -0
  39. package/dist/conditions/cosine-similarity.d.ts +11 -0
  40. package/dist/conditions/cosine-similarity.js +28 -0
  41. package/dist/conditions/index.d.ts +3 -0
  42. package/dist/conditions/index.js +2 -0
  43. package/dist/errors/noetic-config-error.d.ts +41 -0
  44. package/dist/errors/noetic-config-error.js +44 -0
  45. package/dist/errors/noetic-error.d.ts +18 -0
  46. package/dist/errors/noetic-error.js +52 -0
  47. package/dist/index.d.ts +152 -0
  48. package/dist/index.js +112 -0
  49. package/dist/internal-test.d.ts +11 -0
  50. package/dist/internal-test.js +11 -0
  51. package/dist/interpreter/clone-guard.d.ts +4 -0
  52. package/dist/interpreter/clone-guard.js +18 -0
  53. package/dist/interpreter/collect-tools.d.ts +12 -0
  54. package/dist/interpreter/collect-tools.js +70 -0
  55. package/dist/interpreter/execute-branch.d.ts +3 -0
  56. package/dist/interpreter/execute-branch.js +10 -0
  57. package/dist/interpreter/execute-every.d.ts +15 -0
  58. package/dist/interpreter/execute-every.js +143 -0
  59. package/dist/interpreter/execute-fork.d.ts +3 -0
  60. package/dist/interpreter/execute-fork.js +215 -0
  61. package/dist/interpreter/execute-llm.d.ts +4 -0
  62. package/dist/interpreter/execute-llm.js +217 -0
  63. package/dist/interpreter/execute-loop.d.ts +3 -0
  64. package/dist/interpreter/execute-loop.js +171 -0
  65. package/dist/interpreter/execute-provide.d.ts +10 -0
  66. package/dist/interpreter/execute-provide.js +64 -0
  67. package/dist/interpreter/execute-run.d.ts +3 -0
  68. package/dist/interpreter/execute-run.js +39 -0
  69. package/dist/interpreter/execute-spawn.d.ts +11 -0
  70. package/dist/interpreter/execute-spawn.js +113 -0
  71. package/dist/interpreter/execute-tool.d.ts +5 -0
  72. package/dist/interpreter/execute-tool.js +81 -0
  73. package/dist/interpreter/execute.d.ts +14 -0
  74. package/dist/interpreter/execute.js +119 -0
  75. package/dist/interpreter/framework-cast.d.ts +10 -0
  76. package/dist/interpreter/framework-cast.js +16 -0
  77. package/dist/interpreter/message-helpers.d.ts +9 -0
  78. package/dist/interpreter/message-helpers.js +42 -0
  79. package/dist/interpreter/typeguards.d.ts +25 -0
  80. package/dist/interpreter/typeguards.js +50 -0
  81. package/dist/memory/budget.d.ts +23 -0
  82. package/dist/memory/budget.js +117 -0
  83. package/dist/memory/function-call-utils.d.ts +6 -0
  84. package/dist/memory/function-call-utils.js +25 -0
  85. package/dist/memory/layer-api.d.ts +14 -0
  86. package/dist/memory/layer-api.js +91 -0
  87. package/dist/memory/layer-lifecycle.d.ts +160 -0
  88. package/dist/memory/layer-lifecycle.js +552 -0
  89. package/dist/memory/layer-usage.d.ts +15 -0
  90. package/dist/memory/layer-usage.js +48 -0
  91. package/dist/memory/layers/durable-task-state.d.ts +79 -0
  92. package/dist/memory/layers/durable-task-state.js +118 -0
  93. package/dist/memory/layers/file-reference.d.ts +55 -0
  94. package/dist/memory/layers/file-reference.js +538 -0
  95. package/dist/memory/layers/history-window.d.ts +29 -0
  96. package/dist/memory/layers/history-window.js +114 -0
  97. package/dist/memory/layers/observational-memory.d.ts +53 -0
  98. package/dist/memory/layers/observational-memory.js +110 -0
  99. package/dist/memory/layers/plan.d.ts +55 -0
  100. package/dist/memory/layers/plan.js +369 -0
  101. package/dist/memory/layers/static-content.d.ts +27 -0
  102. package/dist/memory/layers/static-content.js +35 -0
  103. package/dist/memory/layers/steering.d.ts +51 -0
  104. package/dist/memory/layers/steering.js +258 -0
  105. package/dist/memory/layers/tool-memory-layer.d.ts +16 -0
  106. package/dist/memory/layers/tool-memory-layer.js +43 -0
  107. package/dist/memory/layers/working-memory.d.ts +44 -0
  108. package/dist/memory/layers/working-memory.js +92 -0
  109. package/dist/memory/projector.d.ts +10 -0
  110. package/dist/memory/projector.js +19 -0
  111. package/dist/memory/scope.d.ts +3 -0
  112. package/dist/memory/scope.js +31 -0
  113. package/dist/memory/strip-unresolved.d.ts +13 -0
  114. package/dist/memory/strip-unresolved.js +35 -0
  115. package/dist/observability/genai-attributes.d.ts +11 -0
  116. package/dist/observability/genai-attributes.js +12 -0
  117. package/dist/observability/span-impl.d.ts +20 -0
  118. package/dist/observability/span-impl.js +33 -0
  119. package/dist/observability/trace-exporter.d.ts +15 -0
  120. package/dist/observability/trace-exporter.js +26 -0
  121. package/dist/patterns/flow.d.ts +52 -0
  122. package/dist/patterns/flow.js +103 -0
  123. package/dist/patterns/interview.d.ts +53 -0
  124. package/dist/patterns/interview.js +113 -0
  125. package/dist/patterns/plans.d.ts +45 -0
  126. package/dist/patterns/plans.js +125 -0
  127. package/dist/patterns/ralph-wiggum.d.ts +20 -0
  128. package/dist/patterns/ralph-wiggum.js +37 -0
  129. package/dist/patterns/react.d.ts +18 -0
  130. package/dist/patterns/react.js +39 -0
  131. package/dist/portable.d.ts +26 -0
  132. package/dist/portable.js +34 -0
  133. package/dist/runtime/agent-harness.d.ts +206 -0
  134. package/dist/runtime/agent-harness.js +1170 -0
  135. package/dist/runtime/broadcaster-utils.d.ts +30 -0
  136. package/dist/runtime/broadcaster-utils.js +53 -0
  137. package/dist/runtime/channel-store.d.ts +18 -0
  138. package/dist/runtime/channel-store.js +187 -0
  139. package/dist/runtime/context-impl.d.ts +80 -0
  140. package/dist/runtime/context-impl.js +140 -0
  141. package/dist/runtime/cwd-helpers.d.ts +42 -0
  142. package/dist/runtime/cwd-helpers.js +68 -0
  143. package/dist/runtime/detached-handle.d.ts +14 -0
  144. package/dist/runtime/detached-handle.js +56 -0
  145. package/dist/runtime/event-broadcaster.d.ts +61 -0
  146. package/dist/runtime/event-broadcaster.js +187 -0
  147. package/dist/runtime/exec-context-factory.d.ts +25 -0
  148. package/dist/runtime/exec-context-factory.js +30 -0
  149. package/dist/runtime/in-memory-storage.d.ts +9 -0
  150. package/dist/runtime/in-memory-storage.js +33 -0
  151. package/dist/runtime/item-log-impl.d.ts +11 -0
  152. package/dist/runtime/item-log-impl.js +25 -0
  153. package/dist/runtime/message-queue.d.ts +30 -0
  154. package/dist/runtime/message-queue.js +46 -0
  155. package/dist/runtime/session-runner.d.ts +77 -0
  156. package/dist/runtime/session-runner.js +253 -0
  157. package/dist/runtime/session-streams.d.ts +11 -0
  158. package/dist/runtime/session-streams.js +204 -0
  159. package/dist/runtime/tool-memory.d.ts +6 -0
  160. package/dist/runtime/tool-memory.js +22 -0
  161. package/dist/schemas/item.d.ts +35 -0
  162. package/dist/schemas/item.js +155 -0
  163. package/dist/types/channel.d.ts +29 -0
  164. package/dist/types/channel.js +1 -0
  165. package/dist/types/common.d.ts +111 -0
  166. package/dist/types/common.js +1 -0
  167. package/dist/types/context.d.ts +93 -0
  168. package/dist/types/context.js +1 -0
  169. package/dist/types/detached.d.ts +17 -0
  170. package/dist/types/detached.js +7 -0
  171. package/dist/types/embed.d.ts +5 -0
  172. package/dist/types/embed.js +1 -0
  173. package/dist/types/error.d.ts +51 -0
  174. package/dist/types/error.js +1 -0
  175. package/dist/types/fs-adapter.d.ts +51 -0
  176. package/dist/types/fs-adapter.js +3 -0
  177. package/dist/types/harness-result.d.ts +43 -0
  178. package/dist/types/harness-result.js +2 -0
  179. package/dist/types/items.d.ts +115 -0
  180. package/dist/types/items.js +2 -0
  181. package/dist/types/memory.d.ts +336 -0
  182. package/dist/types/memory.js +13 -0
  183. package/dist/types/mutable-context.d.ts +13 -0
  184. package/dist/types/mutable-context.js +1 -0
  185. package/dist/types/observability.d.ts +30 -0
  186. package/dist/types/observability.js +1 -0
  187. package/dist/types/runtime.d.ts +255 -0
  188. package/dist/types/runtime.js +1 -0
  189. package/dist/types/shell-adapter.d.ts +34 -0
  190. package/dist/types/shell-adapter.js +10 -0
  191. package/dist/types/steering.d.ts +89 -0
  192. package/dist/types/steering.js +14 -0
  193. package/dist/types/step.d.ts +206 -0
  194. package/dist/types/step.js +1 -0
  195. package/dist/types/subprocess-adapter.d.ts +41 -0
  196. package/dist/types/subprocess-adapter.js +1 -0
  197. package/dist/types/tool-context.d.ts +34 -0
  198. package/dist/types/tool-context.js +1 -0
  199. package/dist/unstable.d.ts +28 -0
  200. package/dist/unstable.js +24 -0
  201. package/dist/until/combinators.d.ts +17 -0
  202. package/dist/until/combinators.js +49 -0
  203. package/dist/until/never.d.ts +9 -0
  204. package/dist/until/never.js +12 -0
  205. package/dist/until/predicates.d.ts +43 -0
  206. package/dist/until/predicates.js +126 -0
  207. package/package.json +81 -0
@@ -0,0 +1,11 @@
1
+ import type { MemoryConfig, MemoryLayer } from '../types/memory';
2
+ /**
3
+ * Creates a type-safe memory configuration from a tuple of memory layers.
4
+ * The returned config preserves literal layer types for compile-time inference
5
+ * via `InferMemory<typeof config>`.
6
+ *
7
+ * @public
8
+ * @param layers - Tuple of memory layers to include in the configuration.
9
+ * @returns A `MemoryConfig` carrying the inferred memory shape as a phantom type.
10
+ */
11
+ export declare function memory<const T extends readonly MemoryLayer[]>(layers: T): MemoryConfig<T>;
@@ -0,0 +1,15 @@
1
+ import { frameworkCast } from '../interpreter/framework-cast';
2
+ /**
3
+ * Creates a type-safe memory configuration from a tuple of memory layers.
4
+ * The returned config preserves literal layer types for compile-time inference
5
+ * via `InferMemory<typeof config>`.
6
+ *
7
+ * @public
8
+ * @param layers - Tuple of memory layers to include in the configuration.
9
+ * @returns A `MemoryConfig` carrying the inferred memory shape as a phantom type.
10
+ */
11
+ export function memory(layers) {
12
+ return frameworkCast({
13
+ layers,
14
+ });
15
+ }
@@ -0,0 +1,20 @@
1
+ import type { ContextMemory, MemoryConfig, MemoryLayer } from '../types/memory';
2
+ import type { Step, StepProvide } from '../types/step';
3
+ /**
4
+ * Creates a provide step that attaches memory layers to its child without creating an isolated context.
5
+ * Like React's Context.Provider — layers are available to all descendant steps.
6
+ * Spawn and detachedSpawn break the inheritance chain.
7
+ *
8
+ * @public
9
+ * @param opts.id - Unique step identifier used in traces and error messages.
10
+ * @param opts.child - Step to execute with the provided layers.
11
+ * @param opts.memory - Memory layers to provide to descendant steps.
12
+ * @returns A `StepProvide` step.
13
+ * @throws `NoeticConfigError` with code `EMPTY_STEP_ID` if `id` is empty.
14
+ * @throws `NoeticConfigError` with code `MISSING_CHILD_STEP` if `child` is not provided.
15
+ */
16
+ export declare function provide<TMemory = ContextMemory, I = unknown, O = unknown>(opts: {
17
+ id: string;
18
+ child: Step<TMemory, I, O>;
19
+ memory: MemoryConfig | MemoryLayer[];
20
+ }): StepProvide<TMemory, I, O>;
@@ -0,0 +1,34 @@
1
+ import { NoeticConfigError } from '../errors/noetic-config-error';
2
+ /**
3
+ * Creates a provide step that attaches memory layers to its child without creating an isolated context.
4
+ * Like React's Context.Provider — layers are available to all descendant steps.
5
+ * Spawn and detachedSpawn break the inheritance chain.
6
+ *
7
+ * @public
8
+ * @param opts.id - Unique step identifier used in traces and error messages.
9
+ * @param opts.child - Step to execute with the provided layers.
10
+ * @param opts.memory - Memory layers to provide to descendant steps.
11
+ * @returns A `StepProvide` step.
12
+ * @throws `NoeticConfigError` with code `EMPTY_STEP_ID` if `id` is empty.
13
+ * @throws `NoeticConfigError` with code `MISSING_CHILD_STEP` if `child` is not provided.
14
+ */
15
+ export function provide(opts) {
16
+ if (!opts.id?.trim()) {
17
+ throw new NoeticConfigError({
18
+ code: 'EMPTY_STEP_ID',
19
+ message: 'provide() requires a non-empty id.',
20
+ hint: 'Pass a unique string as the id field, e.g. provide({ id: "my-provider", ... }).',
21
+ });
22
+ }
23
+ if (!opts.child) {
24
+ throw new NoeticConfigError({
25
+ code: 'MISSING_CHILD_STEP',
26
+ message: 'provide() requires a child step.',
27
+ hint: 'Provide a child step to execute with the provided memory layers.',
28
+ });
29
+ }
30
+ return {
31
+ kind: 'provide',
32
+ ...opts,
33
+ };
34
+ }
@@ -0,0 +1,20 @@
1
+ import type { ContextMemory, MemoryConfig, MemoryLayer } from '../types/memory';
2
+ import type { Step, StepSpawn } from '../types/step';
3
+ /**
4
+ * Creates a spawn step that executes a child step in an isolated context boundary.
5
+ *
6
+ * @public
7
+ * @param opts.id - Unique step identifier used in traces and error messages.
8
+ * @param opts.child - Step to execute in the isolated child context.
9
+ * @param opts.memory - Optional memory config or layers for the child context (replaces parent layers entirely).
10
+ * @param opts.timeout - Optional execution timeout in ms; the child is aborted if it exceeds this.
11
+ * @returns A `StepSpawn` step.
12
+ * @throws `NoeticConfigError` with code `EMPTY_STEP_ID` if `id` is empty.
13
+ * @throws `NoeticConfigError` with code `MISSING_CHILD_STEP` if `child` is not provided.
14
+ */
15
+ export declare function spawn<TMemory = ContextMemory, I = unknown, O = unknown>(opts: {
16
+ id: string;
17
+ child: Step<TMemory, I, O>;
18
+ memory?: MemoryConfig | MemoryLayer[];
19
+ timeout?: number;
20
+ }): StepSpawn<TMemory, I, O>;
@@ -0,0 +1,33 @@
1
+ import { NoeticConfigError } from '../errors/noetic-config-error';
2
+ /**
3
+ * Creates a spawn step that executes a child step in an isolated context boundary.
4
+ *
5
+ * @public
6
+ * @param opts.id - Unique step identifier used in traces and error messages.
7
+ * @param opts.child - Step to execute in the isolated child context.
8
+ * @param opts.memory - Optional memory config or layers for the child context (replaces parent layers entirely).
9
+ * @param opts.timeout - Optional execution timeout in ms; the child is aborted if it exceeds this.
10
+ * @returns A `StepSpawn` step.
11
+ * @throws `NoeticConfigError` with code `EMPTY_STEP_ID` if `id` is empty.
12
+ * @throws `NoeticConfigError` with code `MISSING_CHILD_STEP` if `child` is not provided.
13
+ */
14
+ export function spawn(opts) {
15
+ if (!opts.id?.trim()) {
16
+ throw new NoeticConfigError({
17
+ code: 'EMPTY_STEP_ID',
18
+ message: 'spawn() requires a non-empty id.',
19
+ hint: 'Pass a unique string as the id field, e.g. spawn({ id: "my-spawn", ... }).',
20
+ });
21
+ }
22
+ if (!opts.child) {
23
+ throw new NoeticConfigError({
24
+ code: 'MISSING_CHILD_STEP',
25
+ message: 'spawn() requires a child step.',
26
+ hint: 'Provide a child step to execute in the isolated child context.',
27
+ });
28
+ }
29
+ return {
30
+ kind: 'spawn',
31
+ ...opts,
32
+ };
33
+ }
@@ -0,0 +1,62 @@
1
+ import type { ZodType } from 'zod';
2
+ import type { ModelParams, RetryPolicy, Tool } from '../types/common';
3
+ import type { Context } from '../types/context';
4
+ import type { ContextMemory } from '../types/memory';
5
+ import type { StepLLM, StepRun, StepTool } from '../types/step';
6
+ export declare const step: {
7
+ /**
8
+ * Creates a pure async computation step.
9
+ *
10
+ * @public
11
+ * @param opts.id - Unique step identifier used in traces and error messages.
12
+ * @param opts.execute - Async function `(input, ctx) => output` that performs the work.
13
+ * @param opts.retry - Optional retry policy controlling attempts, backoff, and delay.
14
+ * @returns A `StepRun` that can be composed into larger pipelines.
15
+ * @throws `NoeticConfigError` with code `EMPTY_STEP_ID` if `id` is empty.
16
+ * @throws `NoeticConfigError` with code `MISSING_EXECUTE_FUNCTION` if `execute` is not provided.
17
+ */
18
+ run<TMemory = Readonly<Record<string, Record<string, unknown>>>, I = unknown, O = unknown>(opts: {
19
+ id: string;
20
+ execute: (input: I, ctx: Context<TMemory>) => Promise<O>;
21
+ retry?: RetryPolicy;
22
+ }): StepRun<TMemory, I, O>;
23
+ /**
24
+ * Creates an LLM model call step with optional tools and structured output.
25
+ *
26
+ * @public
27
+ * @param opts.id - Unique step identifier used in traces and error messages.
28
+ * @param opts.model - Model identifier string (e.g. `'anthropic/claude-sonnet-4-20250514'`).
29
+ * @param opts.instructions - Optional system prompt / instructions for the model.
30
+ * @param opts.tools - Optional tools available to the model during this call.
31
+ * @param opts.output - Optional Zod schema enabling structured output parsing.
32
+ * @param opts.params - Optional model parameters (temperature, topP, maxTokens, stopSequences).
33
+ * @returns A `StepLLM` that can be composed into larger pipelines.
34
+ * @throws `NoeticConfigError` with code `EMPTY_STEP_ID` if `id` is empty.
35
+ * @throws `NoeticConfigError` with code `MISSING_MODEL` if `model` is empty.
36
+ */
37
+ llm<TMemory = Readonly<Record<string, Record<string, unknown>>>, I = unknown, O = unknown>(opts: {
38
+ id: string;
39
+ model: string;
40
+ instructions?: string;
41
+ tools?: Tool[];
42
+ output?: ZodType<O>;
43
+ params?: ModelParams;
44
+ emit?: boolean | ((eventType: string, data: Record<string, unknown>) => boolean);
45
+ }): StepLLM<TMemory, I, O>;
46
+ /**
47
+ * Creates a tool execution step that invokes a typed tool definition.
48
+ *
49
+ * @public
50
+ * @param opts.id - Unique step identifier used in traces and error messages.
51
+ * @param opts.tool - The tool definition with typed input/output schemas.
52
+ * @param opts.args - Optional partial args that override or supplement LLM-provided arguments.
53
+ * @returns A `StepTool` that can be composed into larger pipelines.
54
+ * @throws `NoeticConfigError` with code `EMPTY_STEP_ID` if `id` is empty.
55
+ * @throws `NoeticConfigError` with code `MISSING_TOOL` if `tool` is not provided.
56
+ */
57
+ tool<TMemory = Readonly<Record<string, Record<string, unknown>>>, I = unknown, O = unknown>(opts: {
58
+ id: string;
59
+ tool: Tool<ZodType<I>, ZodType<O>>;
60
+ args?: Partial<I>;
61
+ }): StepTool<TMemory, I, O>;
62
+ };
@@ -0,0 +1,99 @@
1
+ import { NoeticConfigError } from '../errors/noetic-config-error';
2
+ export const step = {
3
+ /**
4
+ * Creates a pure async computation step.
5
+ *
6
+ * @public
7
+ * @param opts.id - Unique step identifier used in traces and error messages.
8
+ * @param opts.execute - Async function `(input, ctx) => output` that performs the work.
9
+ * @param opts.retry - Optional retry policy controlling attempts, backoff, and delay.
10
+ * @returns A `StepRun` that can be composed into larger pipelines.
11
+ * @throws `NoeticConfigError` with code `EMPTY_STEP_ID` if `id` is empty.
12
+ * @throws `NoeticConfigError` with code `MISSING_EXECUTE_FUNCTION` if `execute` is not provided.
13
+ */
14
+ run(opts) {
15
+ if (!opts.id || opts.id.trim() === '') {
16
+ throw new NoeticConfigError({
17
+ code: 'EMPTY_STEP_ID',
18
+ message: 'step.run() requires a non-empty id.',
19
+ hint: 'Pass a unique string as the id field, e.g. step.run({ id: "my-step", ... }).',
20
+ });
21
+ }
22
+ if (!opts.execute) {
23
+ throw new NoeticConfigError({
24
+ code: 'MISSING_EXECUTE_FUNCTION',
25
+ message: 'step.run() requires an execute function.',
26
+ hint: 'Provide an async execute function, e.g. execute: async (input, ctx) => result.',
27
+ });
28
+ }
29
+ return {
30
+ kind: 'run',
31
+ ...opts,
32
+ };
33
+ },
34
+ /**
35
+ * Creates an LLM model call step with optional tools and structured output.
36
+ *
37
+ * @public
38
+ * @param opts.id - Unique step identifier used in traces and error messages.
39
+ * @param opts.model - Model identifier string (e.g. `'anthropic/claude-sonnet-4-20250514'`).
40
+ * @param opts.instructions - Optional system prompt / instructions for the model.
41
+ * @param opts.tools - Optional tools available to the model during this call.
42
+ * @param opts.output - Optional Zod schema enabling structured output parsing.
43
+ * @param opts.params - Optional model parameters (temperature, topP, maxTokens, stopSequences).
44
+ * @returns A `StepLLM` that can be composed into larger pipelines.
45
+ * @throws `NoeticConfigError` with code `EMPTY_STEP_ID` if `id` is empty.
46
+ * @throws `NoeticConfigError` with code `MISSING_MODEL` if `model` is empty.
47
+ */
48
+ llm(opts) {
49
+ if (!opts.id || opts.id.trim() === '') {
50
+ throw new NoeticConfigError({
51
+ code: 'EMPTY_STEP_ID',
52
+ message: 'step.llm() requires a non-empty id.',
53
+ hint: 'Pass a unique string as the id field, e.g. step.llm({ id: "my-llm", ... }).',
54
+ });
55
+ }
56
+ if (!opts.model || opts.model.trim() === '') {
57
+ throw new NoeticConfigError({
58
+ code: 'MISSING_MODEL',
59
+ message: 'step.llm() requires a non-empty model.',
60
+ hint: "Pass a model identifier, e.g. model: 'anthropic/claude-sonnet-4-20250514'.",
61
+ });
62
+ }
63
+ return {
64
+ kind: 'llm',
65
+ ...opts,
66
+ };
67
+ },
68
+ /**
69
+ * Creates a tool execution step that invokes a typed tool definition.
70
+ *
71
+ * @public
72
+ * @param opts.id - Unique step identifier used in traces and error messages.
73
+ * @param opts.tool - The tool definition with typed input/output schemas.
74
+ * @param opts.args - Optional partial args that override or supplement LLM-provided arguments.
75
+ * @returns A `StepTool` that can be composed into larger pipelines.
76
+ * @throws `NoeticConfigError` with code `EMPTY_STEP_ID` if `id` is empty.
77
+ * @throws `NoeticConfigError` with code `MISSING_TOOL` if `tool` is not provided.
78
+ */
79
+ tool(opts) {
80
+ if (!opts.id || opts.id.trim() === '') {
81
+ throw new NoeticConfigError({
82
+ code: 'EMPTY_STEP_ID',
83
+ message: 'step.tool() requires a non-empty id.',
84
+ hint: 'Pass a unique string as the id field, e.g. step.tool({ id: "my-tool", ... }).',
85
+ });
86
+ }
87
+ if (!opts.tool) {
88
+ throw new NoeticConfigError({
89
+ code: 'MISSING_TOOL',
90
+ message: 'step.tool() requires a tool.',
91
+ hint: 'Provide a tool definition created with the tool() builder.',
92
+ });
93
+ }
94
+ return {
95
+ kind: 'tool',
96
+ ...opts,
97
+ };
98
+ },
99
+ };
@@ -0,0 +1,52 @@
1
+ import type { ZodTypeAny, z } from 'zod';
2
+ import type { Tool } from '../types/common';
3
+ import type { FunctionCallItem, FunctionCallOutputItem, Item, ItemSchemaExtensions, ToolResultExtensionItem } from '../types/items';
4
+ import type { ToolExecutionContext } from '../types/tool-context';
5
+ interface ToolConfig<I extends ZodTypeAny, O extends ZodTypeAny> {
6
+ name: string;
7
+ description: string;
8
+ input: I;
9
+ output: O;
10
+ itemSchemas?: Pick<ItemSchemaExtensions, 'toolCalls' | 'toolResults' | 'items'>;
11
+ decorateResultItem?: (params: {
12
+ baseItem: FunctionCallOutputItem;
13
+ callItem: FunctionCallItem;
14
+ args: z.infer<I>;
15
+ result: z.infer<O> | undefined;
16
+ output: string;
17
+ error?: boolean;
18
+ }) => Item | ToolResultExtensionItem;
19
+ execute: (args: z.infer<I>, toolCtx: ToolExecutionContext) => Promise<z.infer<O>>;
20
+ needsApproval?: boolean;
21
+ }
22
+ interface GeneratorToolConfig<I extends ZodTypeAny, E extends ZodTypeAny, O extends ZodTypeAny> {
23
+ name: string;
24
+ description: string;
25
+ input: I;
26
+ event: E;
27
+ output: O;
28
+ itemSchemas?: Pick<ItemSchemaExtensions, 'toolCalls' | 'toolResults' | 'items'>;
29
+ decorateResultItem?: (params: {
30
+ baseItem: FunctionCallOutputItem;
31
+ callItem: FunctionCallItem;
32
+ args: z.infer<I>;
33
+ result: z.infer<O> | undefined;
34
+ output: string;
35
+ error?: boolean;
36
+ }) => Item | ToolResultExtensionItem;
37
+ execute: (args: z.infer<I>, toolCtx: ToolExecutionContext) => AsyncGenerator<z.infer<E>, z.infer<O>>;
38
+ needsApproval?: boolean;
39
+ }
40
+ /**
41
+ * Creates a typed Tool with Zod schema inference for input and output.
42
+ *
43
+ * @public
44
+ */
45
+ export declare function tool<I extends ZodTypeAny, O extends ZodTypeAny>(config: ToolConfig<I, O>): Tool<I, O>;
46
+ /**
47
+ * Creates a typed Tool that can stream progress events before returning a final output.
48
+ *
49
+ * @public
50
+ */
51
+ export declare function toolWithGenerator<I extends ZodTypeAny, E extends ZodTypeAny, O extends ZodTypeAny>(config: GeneratorToolConfig<I, E, O>): Tool<I, O>;
52
+ export {};
@@ -0,0 +1,59 @@
1
+ import { NoeticConfigError } from '../errors/noetic-config-error';
2
+ //#endregion
3
+ //#region Helpers
4
+ function validateToolConfig(name, execute) {
5
+ if (!name || name.trim() === '') {
6
+ throw new NoeticConfigError({
7
+ code: 'EMPTY_TOOL_NAME',
8
+ message: 'tool() requires a non-empty name.',
9
+ hint: "Pass a unique name for the tool, e.g. tool({ name: 'greet', ... }).",
10
+ });
11
+ }
12
+ if (!execute) {
13
+ throw new NoeticConfigError({
14
+ code: 'MISSING_EXECUTE_FUNCTION',
15
+ message: 'tool() requires an execute function.',
16
+ hint: 'Provide an async execute function, e.g. execute: async (args, toolCtx) => result.',
17
+ });
18
+ }
19
+ }
20
+ //#endregion
21
+ //#region Public API
22
+ /**
23
+ * Creates a typed Tool with Zod schema inference for input and output.
24
+ *
25
+ * @public
26
+ */
27
+ export function tool(config) {
28
+ validateToolConfig(config.name, config.execute);
29
+ return {
30
+ name: config.name,
31
+ description: config.description,
32
+ input: config.input,
33
+ output: config.output,
34
+ itemSchemas: config.itemSchemas,
35
+ decorateResultItem: config.decorateResultItem,
36
+ execute: config.execute,
37
+ needsApproval: config.needsApproval,
38
+ };
39
+ }
40
+ /**
41
+ * Creates a typed Tool that can stream progress events before returning a final output.
42
+ *
43
+ * @public
44
+ */
45
+ export function toolWithGenerator(config) {
46
+ validateToolConfig(config.name, config.execute);
47
+ return {
48
+ name: config.name,
49
+ description: config.description,
50
+ input: config.input,
51
+ event: config.event,
52
+ output: config.output,
53
+ itemSchemas: config.itemSchemas,
54
+ decorateResultItem: config.decorateResultItem,
55
+ execute: config.execute,
56
+ needsApproval: config.needsApproval,
57
+ };
58
+ }
59
+ //#endregion
@@ -0,0 +1,114 @@
1
+ import type { Context } from '../types/context';
2
+ import type { EmbedFn } from '../types/embed';
3
+ import type { ContextMemory, StorageAdapter } from '../types/memory';
4
+ import type { Step } from '../types/step';
5
+ export type Condition<I> = (input: I, ctx: Context) => Promise<boolean>;
6
+ export interface WhenClause<TMemory = ContextMemory, I = unknown, O = unknown> {
7
+ readonly kind: 'when';
8
+ readonly condition: Condition<I>;
9
+ readonly step: Step<TMemory, I, O>;
10
+ }
11
+ export interface OtherwiseClause<TMemory = ContextMemory, I = unknown, O = unknown> {
12
+ readonly kind: 'otherwise';
13
+ readonly step: Step<TMemory, I, O>;
14
+ }
15
+ type Clause<TMemory, I, O> = WhenClause<TMemory, I, O> | OtherwiseClause<TMemory, I, O>;
16
+ /**
17
+ * Creates a conditional clause that routes to the given step when the condition is true.
18
+ *
19
+ * @public
20
+ * @param condition - Async predicate evaluated against the input and context.
21
+ * @param step - Step to execute when the condition matches.
22
+ * @returns A `WhenClause` for use in `semanticRoute`.
23
+ */
24
+ export declare function when<TMemory = ContextMemory, I = unknown, O = unknown>(condition: Condition<I>, step: Step<TMemory, I, O>): WhenClause<TMemory, I, O>;
25
+ /**
26
+ * Creates a fallback clause that always matches, used as the last clause in `semanticRoute`.
27
+ *
28
+ * @public
29
+ * @param step - Step to execute when no prior `when` clause matches.
30
+ * @returns An `OtherwiseClause` for use in `semanticRoute`.
31
+ */
32
+ export declare function otherwise<TMemory = ContextMemory, I = unknown, O = unknown>(step: Step<TMemory, I, O>): OtherwiseClause<TMemory, I, O>;
33
+ /**
34
+ * Builds a route function from an ordered list of `when`/`otherwise` clauses.
35
+ * Evaluates clauses sequentially, returning the first matching step or `null`.
36
+ *
37
+ * @public
38
+ * @param clauses - Ordered `WhenClause` and optional trailing `OtherwiseClause`.
39
+ * @returns A route function suitable for `branch({ route })`.
40
+ */
41
+ export declare function semanticRoute<TMemory = ContextMemory, I = unknown, O = unknown>(...clauses: Clause<TMemory, I, O>[]): (input: I, ctx: Context) => Promise<Step<TMemory, I, O> | null>;
42
+ interface SemanticSwitchSimple<TMemory, I, O> {
43
+ embed: EmbedFn;
44
+ cases: Record<string, Step<TMemory, I, O>>;
45
+ default?: Step<TMemory, I, O>;
46
+ threshold?: number;
47
+ cache?: StorageAdapter;
48
+ }
49
+ interface SemanticSwitchAdvanced<TMemory, I, O> {
50
+ embed: EmbedFn;
51
+ cases: {
52
+ labels: string | string[];
53
+ step: Step<TMemory, I, O>;
54
+ }[];
55
+ default?: Step<TMemory, I, O>;
56
+ threshold?: number;
57
+ cache?: StorageAdapter;
58
+ }
59
+ /**
60
+ * Builds a route function that selects the best-matching case via embedding cosine similarity.
61
+ *
62
+ * @public
63
+ * @param opts - Simple form with `Record<string, Step>` cases, or advanced form with multi-label cases.
64
+ * @returns A route function suitable for `branch({ route })`.
65
+ */
66
+ export declare function semanticSwitch<TMemory = ContextMemory, I = unknown, O = unknown>(opts: SemanticSwitchSimple<TMemory, I, O>): (input: I, ctx: Context) => Promise<Step<TMemory, I, O> | null>;
67
+ /** @public */
68
+ export declare function semanticSwitch<TMemory = ContextMemory, I = unknown, O = unknown>(opts: SemanticSwitchAdvanced<TMemory, I, O>): (input: I, ctx: Context) => Promise<Step<TMemory, I, O> | null>;
69
+ /**
70
+ * Creates a condition that matches when the input embedding is within a cosine similarity threshold of a label.
71
+ *
72
+ * @public
73
+ * @param embed - Embedding function or advanced options object.
74
+ * @param label - Label string (simple form).
75
+ * @param threshold - Minimum cosine similarity score to match.
76
+ * @returns A `Condition` usable in `when()` clauses.
77
+ */
78
+ export declare function embeddingMatch<I>(embed: EmbedFn, label: string, threshold: number): Condition<I>;
79
+ /** @public */
80
+ export declare function embeddingMatch<I>(opts: {
81
+ embed: EmbedFn;
82
+ labels: string[];
83
+ threshold: number;
84
+ match?: 'any' | 'all';
85
+ cache?: StorageAdapter;
86
+ }): Condition<I>;
87
+ /**
88
+ * Combines conditions with OR semantics; returns true on the first truthy condition.
89
+ *
90
+ * @public
91
+ * @param conditions - Conditions to evaluate.
92
+ * @returns A `Condition` that short-circuits on the first match.
93
+ */
94
+ export declare function anyCondition<I>(...conditions: Condition<I>[]): Condition<I>;
95
+ /**
96
+ * Combines conditions with AND semantics; returns false on the first falsy condition.
97
+ *
98
+ * @public
99
+ * @param conditions - Conditions to evaluate.
100
+ * @returns A `Condition` that short-circuits on the first non-match.
101
+ */
102
+ export declare function allCondition<I>(...conditions: Condition<I>[]): Condition<I>;
103
+ /**
104
+ * Creates a condition that uses an LLM to classify the input as true or false.
105
+ *
106
+ * @public
107
+ * @param opts - Configuration with model identifier and classification prompt.
108
+ * @returns A `Condition` that delegates boolean classification to the model.
109
+ */
110
+ export declare function aiCondition<I>(opts: {
111
+ model: string;
112
+ prompt: string;
113
+ }): Condition<I>;
114
+ export {};