@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,15 @@
1
+ import type { Tool } from '../types/common';
2
+ import type { Context, LastLayerUsage } from '../types/context';
3
+ import type { RecallLayerOutput } from '../types/runtime';
4
+ interface ComputeLayerUsageParams {
5
+ ctx: Context;
6
+ modelId: string;
7
+ instructions?: string;
8
+ tools?: ReadonlyArray<Tool>;
9
+ /** Per-layer recall output assembled into the context view for this LLM call. */
10
+ recallResults: ReadonlyArray<RecallLayerOutput>;
11
+ }
12
+ export declare function computeLayerUsage({ ctx, modelId, instructions, tools, recallResults, }: ComputeLayerUsageParams): LastLayerUsage;
13
+ /** Commit computed usage to the Context, replacing any prior snapshot. */
14
+ export declare function commitLayerUsage(ctx: Context, usage: LastLayerUsage): void;
15
+ export {};
@@ -0,0 +1,48 @@
1
+ import { estimateTokens } from '../interpreter/message-helpers';
2
+ import { isContextImpl } from '../interpreter/typeguards';
3
+ //#endregion
4
+ //#region Helpers
5
+ function estimateJsonTokens(value) {
6
+ try {
7
+ return estimateTokens(JSON.stringify(value));
8
+ }
9
+ catch {
10
+ return 0;
11
+ }
12
+ }
13
+ //#endregion
14
+ //#region Public API
15
+ export function computeLayerUsage({ ctx, modelId, instructions, tools, recallResults, }) {
16
+ const layers = recallResults
17
+ .map((r) => ({
18
+ layerId: r.layerId,
19
+ tokenCount: r.tokenCount,
20
+ items: r.items,
21
+ }))
22
+ .sort((a, b) => a.layerId.localeCompare(b.layerId));
23
+ let historyTokens = 0;
24
+ for (const item of ctx.itemLog.items) {
25
+ historyTokens += estimateJsonTokens(item);
26
+ }
27
+ const systemPromptTokens = instructions ? estimateTokens(instructions) : 0;
28
+ const toolsTokens = tools && tools.length > 0 ? estimateJsonTokens(tools) : 0;
29
+ const layerTotal = layers.reduce((sum, l) => sum + l.tokenCount, 0);
30
+ const totalUsedTokens = systemPromptTokens + toolsTokens + historyTokens + layerTotal;
31
+ return {
32
+ executionId: ctx.id,
33
+ modelId,
34
+ layers,
35
+ systemPromptTokens,
36
+ toolsTokens,
37
+ historyTokens,
38
+ totalUsedTokens,
39
+ };
40
+ }
41
+ /** Commit computed usage to the Context, replacing any prior snapshot. */
42
+ export function commitLayerUsage(ctx, usage) {
43
+ if (!isContextImpl(ctx)) {
44
+ return;
45
+ }
46
+ ctx.lastLayerUsage = usage;
47
+ }
48
+ //#endregion
@@ -0,0 +1,79 @@
1
+ import type { ZodType } from 'zod';
2
+ export interface DurableTaskState {
3
+ checkpoints: Array<{
4
+ timestamp: number;
5
+ depth: number;
6
+ }>;
7
+ files: string[];
8
+ data: Record<string, unknown>;
9
+ }
10
+ export interface DurableTaskStateSerializer {
11
+ serialize(state: DurableTaskState): string;
12
+ deserialize(data: string): DurableTaskState;
13
+ }
14
+ export interface DurableTaskStateConfig {
15
+ baseDir?: string;
16
+ gitCommit?: boolean;
17
+ schema?: ZodType;
18
+ serializer?: DurableTaskStateSerializer;
19
+ }
20
+ /**
21
+ * Creates a memory layer that persists task checkpoints, files, and arbitrary data across iterations.
22
+ *
23
+ * @public
24
+ * @param _config - Optional configuration for base directory, git commit behavior, schema, and serializer.
25
+ * @returns A `MemoryLayer` scoped to the execution with durable task state.
26
+ */
27
+ export declare function durableTaskState(_config?: DurableTaskStateConfig): {
28
+ id: "durable-task-state";
29
+ name: string;
30
+ slot: number;
31
+ scope: "execution";
32
+ budget: {
33
+ min: number;
34
+ max: number;
35
+ };
36
+ timeouts: {
37
+ store: number;
38
+ };
39
+ hooks: {
40
+ init({ storage }: import("../..").InitParams): Promise<{
41
+ state: DurableTaskState;
42
+ }>;
43
+ recall({ state }: import("../..").RecallParams<DurableTaskState>): Promise<{
44
+ items: import("../..").InputMessageItem[];
45
+ tokenCount: number;
46
+ } | null>;
47
+ store({ state, ctx }: import("../..").StoreParams<DurableTaskState>): Promise<{
48
+ state: DurableTaskState;
49
+ }>;
50
+ onSpawn({ parentState }: import("../..").SpawnParams<DurableTaskState>): Promise<{
51
+ childState: DurableTaskState;
52
+ items: never[];
53
+ }>;
54
+ onReturn({ childState, parentState }: import("../..").ReturnParams<DurableTaskState>): Promise<{
55
+ parentState: {
56
+ checkpoints: {
57
+ timestamp: number;
58
+ depth: number;
59
+ }[];
60
+ files: string[];
61
+ data: {
62
+ [x: string]: unknown;
63
+ };
64
+ };
65
+ }>;
66
+ onComplete({ state, outcome }: import("../..").CompleteParams<DurableTaskState>): Promise<{
67
+ state: {
68
+ data: {
69
+ __outcome: import("../..").ExecutionOutcome;
70
+ };
71
+ checkpoints: {
72
+ timestamp: number;
73
+ depth: number;
74
+ }[];
75
+ files: string[];
76
+ };
77
+ } | undefined>;
78
+ };
79
+ };
@@ -0,0 +1,118 @@
1
+ import { createMessage, estimateTokens } from '../../interpreter/message-helpers';
2
+ import { Slot } from '../../types/memory';
3
+ /**
4
+ * Creates a memory layer that persists task checkpoints, files, and arbitrary data across iterations.
5
+ *
6
+ * @public
7
+ * @param _config - Optional configuration for base directory, git commit behavior, schema, and serializer.
8
+ * @returns A `MemoryLayer` scoped to the execution with durable task state.
9
+ */
10
+ export function durableTaskState(_config) {
11
+ return {
12
+ id: 'durable-task-state',
13
+ name: 'Durable Task State',
14
+ slot: Slot.WORKING_MEMORY + 10, // 110
15
+ scope: 'execution',
16
+ budget: {
17
+ min: 100,
18
+ max: 800,
19
+ },
20
+ timeouts: {
21
+ store: 30_000,
22
+ },
23
+ hooks: {
24
+ async init({ storage }) {
25
+ const saved = await storage.get('state');
26
+ return {
27
+ state: saved ?? {
28
+ checkpoints: [],
29
+ files: [],
30
+ data: {},
31
+ },
32
+ };
33
+ },
34
+ async recall({ state }) {
35
+ if (!state) {
36
+ return null;
37
+ }
38
+ const text = `<task_state>\n${JSON.stringify(state, null, 2)}\n</task_state>`;
39
+ return {
40
+ items: [
41
+ createMessage(text, 'developer'),
42
+ ],
43
+ tokenCount: estimateTokens(text),
44
+ };
45
+ },
46
+ async store({ state, ctx }) {
47
+ const currentState = state ?? {
48
+ checkpoints: [],
49
+ files: [],
50
+ data: {},
51
+ };
52
+ // Add a checkpoint for each store call
53
+ const newState = {
54
+ ...currentState,
55
+ checkpoints: [
56
+ ...currentState.checkpoints,
57
+ {
58
+ timestamp: Date.now(),
59
+ depth: ctx.depth,
60
+ },
61
+ ],
62
+ };
63
+ return {
64
+ state: newState,
65
+ };
66
+ },
67
+ async onSpawn({ parentState }) {
68
+ // ALWAYS provides child state (unlike other layers)
69
+ return {
70
+ childState: structuredClone(parentState),
71
+ items: [],
72
+ };
73
+ },
74
+ async onReturn({ childState, parentState }) {
75
+ // Merge child artifacts back to parent
76
+ return {
77
+ parentState: {
78
+ checkpoints: [
79
+ ...parentState.checkpoints,
80
+ ...childState.checkpoints,
81
+ ],
82
+ files: [
83
+ ...new Set([
84
+ ...parentState.files,
85
+ ...childState.files,
86
+ ]),
87
+ ],
88
+ data: {
89
+ ...parentState.data,
90
+ ...childState.data,
91
+ },
92
+ },
93
+ };
94
+ },
95
+ async onComplete({ state, outcome }) {
96
+ if (!state) {
97
+ return;
98
+ }
99
+ return {
100
+ state: {
101
+ ...state,
102
+ data: {
103
+ ...state.data,
104
+ __outcome: outcome,
105
+ },
106
+ checkpoints: [
107
+ ...state.checkpoints,
108
+ {
109
+ timestamp: Date.now(),
110
+ depth: 0,
111
+ },
112
+ ],
113
+ },
114
+ };
115
+ },
116
+ },
117
+ };
118
+ }
@@ -0,0 +1,55 @@
1
+ import type { MemoryLayer } from '../../types/memory';
2
+ interface TrackedFile {
3
+ /** Absolute path to the file */
4
+ absolutePath: string;
5
+ /** Original reference path (as written by user) */
6
+ referencePath: string;
7
+ /** File content (null if deleted or error) */
8
+ content: string | null;
9
+ /** LLM-assigned priority score (0-100, higher = more relevant) */
10
+ priority: number;
11
+ /** Content hash for change detection */
12
+ contentHash: string;
13
+ /** Whether the file has been deleted */
14
+ deleted: boolean;
15
+ /** Estimated token count */
16
+ tokenCount: number;
17
+ /** Error message if file couldn't be read (security, size, permission) */
18
+ error?: string;
19
+ }
20
+ interface FileReferenceState {
21
+ /** Map of reference path to tracked file info */
22
+ files: Map<string, TrackedFile>;
23
+ /** Base directory for resolving relative paths */
24
+ baseDir: string;
25
+ }
26
+ interface FileReferenceOptions {
27
+ /** Base directory for resolving relative paths (defaults to cwd) */
28
+ baseDir?: string;
29
+ /** Slot position (defaults to Slot.RAG) */
30
+ slot?: number;
31
+ /** Model to use for priority scoring */
32
+ scoringModel?: string;
33
+ /** Maximum file size in bytes (defaults to 1MB) */
34
+ maxFileSize?: number;
35
+ /** Whether to follow symlinks (defaults to false for security) */
36
+ followSymlinks?: boolean;
37
+ /** Allowed file extensions (defaults to common code/text extensions) */
38
+ allowedExtensions?: string[];
39
+ }
40
+ /**
41
+ * Creates a memory layer that tracks file references in user messages.
42
+ *
43
+ * Syntax: `#path/to/file` in user messages
44
+ *
45
+ * Behavior:
46
+ * - Transforms references to markdown anchor links `[#path/to/file](#path-to-file)`
47
+ * - Scores file relevance using LLM when first referenced
48
+ * - Injects file contents into context via recall(), ordered by priority
49
+ * - Detects file changes on each new message, triggers immediate re-render
50
+ * - Shows warning for deleted files
51
+ *
52
+ * @public
53
+ */
54
+ export declare function fileReference(opts?: FileReferenceOptions): MemoryLayer<FileReferenceState>;
55
+ export {};