@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.
- package/LICENSE +201 -0
- package/README.md +38 -0
- package/dist/adapters/in-memory-fs-adapter.d.ts +3 -0
- package/dist/adapters/in-memory-fs-adapter.js +245 -0
- package/dist/adapters/in-memory-shell-adapter.d.ts +9 -0
- package/dist/adapters/in-memory-shell-adapter.js +20 -0
- package/dist/adapters/in-memory-subprocess-adapter.d.ts +5 -0
- package/dist/adapters/in-memory-subprocess-adapter.js +126 -0
- package/dist/adapters/local-fs-adapter.d.ts +3 -0
- package/dist/adapters/local-fs-adapter.js +43 -0
- package/dist/adapters/local-shell-adapter.d.ts +51 -0
- package/dist/adapters/local-shell-adapter.js +167 -0
- package/dist/adapters/local-subprocess-adapter.d.ts +16 -0
- package/dist/adapters/local-subprocess-adapter.js +187 -0
- package/dist/adapters/openrouter.d.ts +57 -0
- package/dist/adapters/openrouter.js +248 -0
- package/dist/builders/channel-builder.d.ts +36 -0
- package/dist/builders/channel-builder.js +15 -0
- package/dist/builders/control-flow-builders.d.ts +79 -0
- package/dist/builders/control-flow-builders.js +53 -0
- package/dist/builders/every.d.ts +45 -0
- package/dist/builders/every.js +60 -0
- package/dist/builders/layer-provides-builders.d.ts +32 -0
- package/dist/builders/layer-provides-builders.js +33 -0
- package/dist/builders/loop-builder.d.ts +23 -0
- package/dist/builders/loop-builder.js +46 -0
- package/dist/builders/memory-builder.d.ts +11 -0
- package/dist/builders/memory-builder.js +15 -0
- package/dist/builders/provide-builder.d.ts +20 -0
- package/dist/builders/provide-builder.js +34 -0
- package/dist/builders/spawn-builder.d.ts +20 -0
- package/dist/builders/spawn-builder.js +33 -0
- package/dist/builders/step-builders.d.ts +62 -0
- package/dist/builders/step-builders.js +99 -0
- package/dist/builders/tool-builder.d.ts +52 -0
- package/dist/builders/tool-builder.js +59 -0
- package/dist/conditions/conditions.d.ts +114 -0
- package/dist/conditions/conditions.js +271 -0
- package/dist/conditions/cosine-similarity.d.ts +11 -0
- package/dist/conditions/cosine-similarity.js +28 -0
- package/dist/conditions/index.d.ts +3 -0
- package/dist/conditions/index.js +2 -0
- package/dist/errors/noetic-config-error.d.ts +41 -0
- package/dist/errors/noetic-config-error.js +44 -0
- package/dist/errors/noetic-error.d.ts +18 -0
- package/dist/errors/noetic-error.js +52 -0
- package/dist/index.d.ts +152 -0
- package/dist/index.js +112 -0
- package/dist/internal-test.d.ts +11 -0
- package/dist/internal-test.js +11 -0
- package/dist/interpreter/clone-guard.d.ts +4 -0
- package/dist/interpreter/clone-guard.js +18 -0
- package/dist/interpreter/collect-tools.d.ts +12 -0
- package/dist/interpreter/collect-tools.js +70 -0
- package/dist/interpreter/execute-branch.d.ts +3 -0
- package/dist/interpreter/execute-branch.js +10 -0
- package/dist/interpreter/execute-every.d.ts +15 -0
- package/dist/interpreter/execute-every.js +143 -0
- package/dist/interpreter/execute-fork.d.ts +3 -0
- package/dist/interpreter/execute-fork.js +215 -0
- package/dist/interpreter/execute-llm.d.ts +4 -0
- package/dist/interpreter/execute-llm.js +217 -0
- package/dist/interpreter/execute-loop.d.ts +3 -0
- package/dist/interpreter/execute-loop.js +171 -0
- package/dist/interpreter/execute-provide.d.ts +10 -0
- package/dist/interpreter/execute-provide.js +64 -0
- package/dist/interpreter/execute-run.d.ts +3 -0
- package/dist/interpreter/execute-run.js +39 -0
- package/dist/interpreter/execute-spawn.d.ts +11 -0
- package/dist/interpreter/execute-spawn.js +113 -0
- package/dist/interpreter/execute-tool.d.ts +5 -0
- package/dist/interpreter/execute-tool.js +81 -0
- package/dist/interpreter/execute.d.ts +14 -0
- package/dist/interpreter/execute.js +119 -0
- package/dist/interpreter/framework-cast.d.ts +10 -0
- package/dist/interpreter/framework-cast.js +16 -0
- package/dist/interpreter/message-helpers.d.ts +9 -0
- package/dist/interpreter/message-helpers.js +42 -0
- package/dist/interpreter/typeguards.d.ts +25 -0
- package/dist/interpreter/typeguards.js +50 -0
- package/dist/memory/budget.d.ts +23 -0
- package/dist/memory/budget.js +117 -0
- package/dist/memory/function-call-utils.d.ts +6 -0
- package/dist/memory/function-call-utils.js +25 -0
- package/dist/memory/layer-api.d.ts +14 -0
- package/dist/memory/layer-api.js +91 -0
- package/dist/memory/layer-lifecycle.d.ts +160 -0
- package/dist/memory/layer-lifecycle.js +552 -0
- package/dist/memory/layer-usage.d.ts +15 -0
- package/dist/memory/layer-usage.js +48 -0
- package/dist/memory/layers/durable-task-state.d.ts +79 -0
- package/dist/memory/layers/durable-task-state.js +118 -0
- package/dist/memory/layers/file-reference.d.ts +55 -0
- package/dist/memory/layers/file-reference.js +538 -0
- package/dist/memory/layers/history-window.d.ts +29 -0
- package/dist/memory/layers/history-window.js +114 -0
- package/dist/memory/layers/observational-memory.d.ts +53 -0
- package/dist/memory/layers/observational-memory.js +110 -0
- package/dist/memory/layers/plan.d.ts +55 -0
- package/dist/memory/layers/plan.js +369 -0
- package/dist/memory/layers/static-content.d.ts +27 -0
- package/dist/memory/layers/static-content.js +35 -0
- package/dist/memory/layers/steering.d.ts +51 -0
- package/dist/memory/layers/steering.js +258 -0
- package/dist/memory/layers/tool-memory-layer.d.ts +16 -0
- package/dist/memory/layers/tool-memory-layer.js +43 -0
- package/dist/memory/layers/working-memory.d.ts +44 -0
- package/dist/memory/layers/working-memory.js +92 -0
- package/dist/memory/projector.d.ts +10 -0
- package/dist/memory/projector.js +19 -0
- package/dist/memory/scope.d.ts +3 -0
- package/dist/memory/scope.js +31 -0
- package/dist/memory/strip-unresolved.d.ts +13 -0
- package/dist/memory/strip-unresolved.js +35 -0
- package/dist/observability/genai-attributes.d.ts +11 -0
- package/dist/observability/genai-attributes.js +12 -0
- package/dist/observability/span-impl.d.ts +20 -0
- package/dist/observability/span-impl.js +33 -0
- package/dist/observability/trace-exporter.d.ts +15 -0
- package/dist/observability/trace-exporter.js +26 -0
- package/dist/patterns/flow.d.ts +52 -0
- package/dist/patterns/flow.js +103 -0
- package/dist/patterns/interview.d.ts +53 -0
- package/dist/patterns/interview.js +113 -0
- package/dist/patterns/plans.d.ts +45 -0
- package/dist/patterns/plans.js +125 -0
- package/dist/patterns/ralph-wiggum.d.ts +20 -0
- package/dist/patterns/ralph-wiggum.js +37 -0
- package/dist/patterns/react.d.ts +18 -0
- package/dist/patterns/react.js +39 -0
- package/dist/portable.d.ts +26 -0
- package/dist/portable.js +34 -0
- package/dist/runtime/agent-harness.d.ts +206 -0
- package/dist/runtime/agent-harness.js +1170 -0
- package/dist/runtime/broadcaster-utils.d.ts +30 -0
- package/dist/runtime/broadcaster-utils.js +53 -0
- package/dist/runtime/channel-store.d.ts +18 -0
- package/dist/runtime/channel-store.js +187 -0
- package/dist/runtime/context-impl.d.ts +80 -0
- package/dist/runtime/context-impl.js +140 -0
- package/dist/runtime/cwd-helpers.d.ts +42 -0
- package/dist/runtime/cwd-helpers.js +68 -0
- package/dist/runtime/detached-handle.d.ts +14 -0
- package/dist/runtime/detached-handle.js +56 -0
- package/dist/runtime/event-broadcaster.d.ts +61 -0
- package/dist/runtime/event-broadcaster.js +187 -0
- package/dist/runtime/exec-context-factory.d.ts +25 -0
- package/dist/runtime/exec-context-factory.js +30 -0
- package/dist/runtime/in-memory-storage.d.ts +9 -0
- package/dist/runtime/in-memory-storage.js +33 -0
- package/dist/runtime/item-log-impl.d.ts +11 -0
- package/dist/runtime/item-log-impl.js +25 -0
- package/dist/runtime/message-queue.d.ts +30 -0
- package/dist/runtime/message-queue.js +46 -0
- package/dist/runtime/session-runner.d.ts +77 -0
- package/dist/runtime/session-runner.js +253 -0
- package/dist/runtime/session-streams.d.ts +11 -0
- package/dist/runtime/session-streams.js +204 -0
- package/dist/runtime/tool-memory.d.ts +6 -0
- package/dist/runtime/tool-memory.js +22 -0
- package/dist/schemas/item.d.ts +35 -0
- package/dist/schemas/item.js +155 -0
- package/dist/types/channel.d.ts +29 -0
- package/dist/types/channel.js +1 -0
- package/dist/types/common.d.ts +111 -0
- package/dist/types/common.js +1 -0
- package/dist/types/context.d.ts +93 -0
- package/dist/types/context.js +1 -0
- package/dist/types/detached.d.ts +17 -0
- package/dist/types/detached.js +7 -0
- package/dist/types/embed.d.ts +5 -0
- package/dist/types/embed.js +1 -0
- package/dist/types/error.d.ts +51 -0
- package/dist/types/error.js +1 -0
- package/dist/types/fs-adapter.d.ts +51 -0
- package/dist/types/fs-adapter.js +3 -0
- package/dist/types/harness-result.d.ts +43 -0
- package/dist/types/harness-result.js +2 -0
- package/dist/types/items.d.ts +115 -0
- package/dist/types/items.js +2 -0
- package/dist/types/memory.d.ts +336 -0
- package/dist/types/memory.js +13 -0
- package/dist/types/mutable-context.d.ts +13 -0
- package/dist/types/mutable-context.js +1 -0
- package/dist/types/observability.d.ts +30 -0
- package/dist/types/observability.js +1 -0
- package/dist/types/runtime.d.ts +255 -0
- package/dist/types/runtime.js +1 -0
- package/dist/types/shell-adapter.d.ts +34 -0
- package/dist/types/shell-adapter.js +10 -0
- package/dist/types/steering.d.ts +89 -0
- package/dist/types/steering.js +14 -0
- package/dist/types/step.d.ts +206 -0
- package/dist/types/step.js +1 -0
- package/dist/types/subprocess-adapter.d.ts +41 -0
- package/dist/types/subprocess-adapter.js +1 -0
- package/dist/types/tool-context.d.ts +34 -0
- package/dist/types/tool-context.js +1 -0
- package/dist/unstable.d.ts +28 -0
- package/dist/unstable.js +24 -0
- package/dist/until/combinators.d.ts +17 -0
- package/dist/until/combinators.js +49 -0
- package/dist/until/never.d.ts +9 -0
- package/dist/until/never.js +12 -0
- package/dist/until/predicates.d.ts +43 -0
- package/dist/until/predicates.js +126 -0
- package/package.json +81 -0
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { createMessage, extractAssistantText, trackUsage } from '../interpreter/message-helpers';
|
|
3
|
+
import { cosineSimilarity } from './cosine-similarity';
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region Helpers
|
|
6
|
+
function serializeInput(input) {
|
|
7
|
+
return typeof input === 'string' ? input : JSON.stringify(input);
|
|
8
|
+
}
|
|
9
|
+
function hashLabel(label) {
|
|
10
|
+
return `embed:${encodeURIComponent(label)}`;
|
|
11
|
+
}
|
|
12
|
+
async function getLabelVectors(embed, labels, ref) {
|
|
13
|
+
if (ref.storage) {
|
|
14
|
+
const storage = ref.storage;
|
|
15
|
+
const keys = labels.map(hashLabel);
|
|
16
|
+
const fetched = await Promise.all(keys.map((k) => storage.get(k)));
|
|
17
|
+
const missingIndices = [];
|
|
18
|
+
for (let i = 0; i < fetched.length; i++) {
|
|
19
|
+
if (!fetched[i]) {
|
|
20
|
+
missingIndices.push(i);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
let freshEmbeddings = [];
|
|
24
|
+
if (missingIndices.length > 0) {
|
|
25
|
+
const missingLabels = missingIndices.map((i) => labels[i]);
|
|
26
|
+
// embed returns readonly number[][], spread each row into a mutable copy
|
|
27
|
+
freshEmbeddings = (await embed(missingLabels)).map((v) => Array.from(v));
|
|
28
|
+
await Promise.all(missingIndices.map((idx, j) => storage.set(keys[idx], freshEmbeddings[j])));
|
|
29
|
+
}
|
|
30
|
+
const results = new Array(labels.length);
|
|
31
|
+
let freshIndex = 0;
|
|
32
|
+
for (let i = 0; i < fetched.length; i++) {
|
|
33
|
+
results[i] = fetched[i] ?? freshEmbeddings[freshIndex++];
|
|
34
|
+
}
|
|
35
|
+
return results;
|
|
36
|
+
}
|
|
37
|
+
if (ref.memory) {
|
|
38
|
+
return ref.memory;
|
|
39
|
+
}
|
|
40
|
+
const vectors = await embed(labels);
|
|
41
|
+
ref.memory = vectors;
|
|
42
|
+
return vectors;
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region Clause Builders
|
|
46
|
+
/**
|
|
47
|
+
* Creates a conditional clause that routes to the given step when the condition is true.
|
|
48
|
+
*
|
|
49
|
+
* @public
|
|
50
|
+
* @param condition - Async predicate evaluated against the input and context.
|
|
51
|
+
* @param step - Step to execute when the condition matches.
|
|
52
|
+
* @returns A `WhenClause` for use in `semanticRoute`.
|
|
53
|
+
*/
|
|
54
|
+
export function when(condition, step) {
|
|
55
|
+
return {
|
|
56
|
+
kind: 'when',
|
|
57
|
+
condition,
|
|
58
|
+
step,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Creates a fallback clause that always matches, used as the last clause in `semanticRoute`.
|
|
63
|
+
*
|
|
64
|
+
* @public
|
|
65
|
+
* @param step - Step to execute when no prior `when` clause matches.
|
|
66
|
+
* @returns An `OtherwiseClause` for use in `semanticRoute`.
|
|
67
|
+
*/
|
|
68
|
+
export function otherwise(step) {
|
|
69
|
+
return {
|
|
70
|
+
kind: 'otherwise',
|
|
71
|
+
step,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region Route Builders
|
|
76
|
+
function isOtherwise(clause) {
|
|
77
|
+
return clause.kind === 'otherwise';
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Builds a route function from an ordered list of `when`/`otherwise` clauses.
|
|
81
|
+
* Evaluates clauses sequentially, returning the first matching step or `null`.
|
|
82
|
+
*
|
|
83
|
+
* @public
|
|
84
|
+
* @param clauses - Ordered `WhenClause` and optional trailing `OtherwiseClause`.
|
|
85
|
+
* @returns A route function suitable for `branch({ route })`.
|
|
86
|
+
*/
|
|
87
|
+
export function semanticRoute(...clauses) {
|
|
88
|
+
return async (input, ctx) => {
|
|
89
|
+
for (const clause of clauses) {
|
|
90
|
+
if (isOtherwise(clause)) {
|
|
91
|
+
return clause.step;
|
|
92
|
+
}
|
|
93
|
+
const matched = await clause.condition(input, ctx);
|
|
94
|
+
if (matched) {
|
|
95
|
+
return clause.step;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
export function semanticSwitch(opts) {
|
|
102
|
+
const threshold = opts.threshold ?? 0.7;
|
|
103
|
+
// Normalize to advanced form
|
|
104
|
+
const cases = Array.isArray(opts.cases)
|
|
105
|
+
? opts.cases.map((c) => ({
|
|
106
|
+
labels: Array.isArray(c.labels)
|
|
107
|
+
? c.labels
|
|
108
|
+
: [
|
|
109
|
+
c.labels,
|
|
110
|
+
],
|
|
111
|
+
step: c.step,
|
|
112
|
+
}))
|
|
113
|
+
: Object.entries(opts.cases).map(([label, step]) => ({
|
|
114
|
+
labels: [
|
|
115
|
+
label,
|
|
116
|
+
],
|
|
117
|
+
step,
|
|
118
|
+
}));
|
|
119
|
+
const allLabels = cases.flatMap((c) => c.labels);
|
|
120
|
+
const labelToCaseIndex = cases.flatMap((c, i) => c.labels.map(() => i));
|
|
121
|
+
const vectorCache = {
|
|
122
|
+
memory: null,
|
|
123
|
+
storage: opts.cache,
|
|
124
|
+
};
|
|
125
|
+
return async (input, _ctx) => {
|
|
126
|
+
const text = serializeInput(input);
|
|
127
|
+
const [inputVector] = await opts.embed([
|
|
128
|
+
text,
|
|
129
|
+
]);
|
|
130
|
+
const labelVectors = await getLabelVectors(opts.embed, allLabels, vectorCache);
|
|
131
|
+
let bestScore = Number.NEGATIVE_INFINITY;
|
|
132
|
+
let bestCaseIndex = -1;
|
|
133
|
+
for (let i = 0; i < allLabels.length; i++) {
|
|
134
|
+
const score = cosineSimilarity(inputVector, labelVectors[i]);
|
|
135
|
+
if (score > bestScore) {
|
|
136
|
+
bestScore = score;
|
|
137
|
+
bestCaseIndex = labelToCaseIndex[i];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (bestScore < threshold) {
|
|
141
|
+
return opts.default ?? null;
|
|
142
|
+
}
|
|
143
|
+
return cases[bestCaseIndex].step;
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
export function embeddingMatch(embedOrOpts, label, threshold) {
|
|
147
|
+
// Simple form
|
|
148
|
+
if (typeof embedOrOpts === 'function') {
|
|
149
|
+
const embed = embedOrOpts;
|
|
150
|
+
if (label === undefined || threshold === undefined) {
|
|
151
|
+
throw new Error('embeddingMatch: label and threshold are required in simple form');
|
|
152
|
+
}
|
|
153
|
+
const singleLabel = label;
|
|
154
|
+
const thresh = threshold;
|
|
155
|
+
const vectorCache = {
|
|
156
|
+
memory: null,
|
|
157
|
+
};
|
|
158
|
+
return async (input, _ctx) => {
|
|
159
|
+
const text = serializeInput(input);
|
|
160
|
+
// On first call, batch input + label into one embed request
|
|
161
|
+
if (!vectorCache.memory) {
|
|
162
|
+
const [inputVector, labelVector] = await embed([
|
|
163
|
+
text,
|
|
164
|
+
singleLabel,
|
|
165
|
+
]);
|
|
166
|
+
vectorCache.memory = [
|
|
167
|
+
labelVector,
|
|
168
|
+
];
|
|
169
|
+
return cosineSimilarity(inputVector, labelVector) >= thresh;
|
|
170
|
+
}
|
|
171
|
+
const [inputVector] = await embed([
|
|
172
|
+
text,
|
|
173
|
+
]);
|
|
174
|
+
return cosineSimilarity(inputVector, vectorCache.memory[0]) >= thresh;
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
// Advanced form
|
|
178
|
+
const opts = embedOrOpts;
|
|
179
|
+
const matchMode = opts.match ?? 'any';
|
|
180
|
+
const vectorCache = {
|
|
181
|
+
memory: null,
|
|
182
|
+
storage: opts.cache,
|
|
183
|
+
};
|
|
184
|
+
return async (input, _ctx) => {
|
|
185
|
+
const text = serializeInput(input);
|
|
186
|
+
const [inputVector] = await opts.embed([
|
|
187
|
+
text,
|
|
188
|
+
]);
|
|
189
|
+
const labelVectors = await getLabelVectors(opts.embed, opts.labels, vectorCache);
|
|
190
|
+
if (matchMode === 'any') {
|
|
191
|
+
return labelVectors.some((vec) => cosineSimilarity(inputVector, vec) >= opts.threshold);
|
|
192
|
+
}
|
|
193
|
+
return labelVectors.every((vec) => cosineSimilarity(inputVector, vec) >= opts.threshold);
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
//#endregion
|
|
197
|
+
//#region Combinators
|
|
198
|
+
/**
|
|
199
|
+
* Combines conditions with OR semantics; returns true on the first truthy condition.
|
|
200
|
+
*
|
|
201
|
+
* @public
|
|
202
|
+
* @param conditions - Conditions to evaluate.
|
|
203
|
+
* @returns A `Condition` that short-circuits on the first match.
|
|
204
|
+
*/
|
|
205
|
+
export function anyCondition(...conditions) {
|
|
206
|
+
return async (input, ctx) => {
|
|
207
|
+
for (const condition of conditions) {
|
|
208
|
+
if (await condition(input, ctx)) {
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return false;
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Combines conditions with AND semantics; returns false on the first falsy condition.
|
|
217
|
+
*
|
|
218
|
+
* @public
|
|
219
|
+
* @param conditions - Conditions to evaluate.
|
|
220
|
+
* @returns A `Condition` that short-circuits on the first non-match.
|
|
221
|
+
*/
|
|
222
|
+
export function allCondition(...conditions) {
|
|
223
|
+
return async (input, ctx) => {
|
|
224
|
+
for (const condition of conditions) {
|
|
225
|
+
if (!(await condition(input, ctx))) {
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return true;
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
//#endregion
|
|
233
|
+
//#region aiCondition
|
|
234
|
+
const AiConditionResponseSchema = z.object({
|
|
235
|
+
answer: z.boolean(),
|
|
236
|
+
});
|
|
237
|
+
/**
|
|
238
|
+
* Creates a condition that uses an LLM to classify the input as true or false.
|
|
239
|
+
*
|
|
240
|
+
* @public
|
|
241
|
+
* @param opts - Configuration with model identifier and classification prompt.
|
|
242
|
+
* @returns A `Condition` that delegates boolean classification to the model.
|
|
243
|
+
*/
|
|
244
|
+
export function aiCondition(opts) {
|
|
245
|
+
return async (input, ctx) => {
|
|
246
|
+
const text = serializeInput(input);
|
|
247
|
+
const systemPrompt = `You are a boolean classifier. Given the user's input, answer the following question with JSON: {"answer": true} or {"answer": false}.\n\nQuestion: ${opts.prompt}`;
|
|
248
|
+
const items = [
|
|
249
|
+
createMessage(systemPrompt, 'developer'),
|
|
250
|
+
createMessage(text, 'user'),
|
|
251
|
+
];
|
|
252
|
+
const response = await ctx.harness.callModel({
|
|
253
|
+
model: opts.model,
|
|
254
|
+
items,
|
|
255
|
+
params: {
|
|
256
|
+
temperature: 0,
|
|
257
|
+
},
|
|
258
|
+
});
|
|
259
|
+
trackUsage(ctx, response);
|
|
260
|
+
const responseText = extractAssistantText(response.items);
|
|
261
|
+
try {
|
|
262
|
+
const parsed = JSON.parse(responseText);
|
|
263
|
+
const validated = AiConditionResponseSchema.parse(parsed);
|
|
264
|
+
return validated.answer;
|
|
265
|
+
}
|
|
266
|
+
catch {
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
//#endregion
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cosine similarity between two vectors.
|
|
3
|
+
* Returns a value in [-1, 1]. Throws on dimension mismatch.
|
|
4
|
+
* Returns 0 for zero-magnitude vectors.
|
|
5
|
+
*
|
|
6
|
+
* @public
|
|
7
|
+
* @param a - First vector.
|
|
8
|
+
* @param b - Second vector (must have the same length as `a`).
|
|
9
|
+
* @returns Cosine similarity score.
|
|
10
|
+
*/
|
|
11
|
+
export declare function cosineSimilarity(a: readonly number[], b: readonly number[]): number;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cosine similarity between two vectors.
|
|
3
|
+
* Returns a value in [-1, 1]. Throws on dimension mismatch.
|
|
4
|
+
* Returns 0 for zero-magnitude vectors.
|
|
5
|
+
*
|
|
6
|
+
* @public
|
|
7
|
+
* @param a - First vector.
|
|
8
|
+
* @param b - Second vector (must have the same length as `a`).
|
|
9
|
+
* @returns Cosine similarity score.
|
|
10
|
+
*/
|
|
11
|
+
export function cosineSimilarity(a, b) {
|
|
12
|
+
if (a.length !== b.length) {
|
|
13
|
+
throw new Error(`Dimension mismatch: ${a.length} vs ${b.length}`);
|
|
14
|
+
}
|
|
15
|
+
let dot = 0;
|
|
16
|
+
let normA = 0;
|
|
17
|
+
let normB = 0;
|
|
18
|
+
for (let i = 0; i < a.length; i++) {
|
|
19
|
+
dot += a[i] * b[i];
|
|
20
|
+
normA += a[i] * a[i];
|
|
21
|
+
normB += b[i] * b[i];
|
|
22
|
+
}
|
|
23
|
+
const magnitude = Math.sqrt(normA * normB);
|
|
24
|
+
if (magnitude === 0) {
|
|
25
|
+
return 0;
|
|
26
|
+
}
|
|
27
|
+
return dot / magnitude;
|
|
28
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @public
|
|
3
|
+
* Configuration-time error thrown during construction and setup.
|
|
4
|
+
*
|
|
5
|
+
* `NoeticConfigError` is separate from `NoeticError`. The two are never interchangeable:
|
|
6
|
+
* - `NoeticError` — thrown during execution (LLM failures, fork failures, budget exceeded).
|
|
7
|
+
* - `NoeticConfigError` — thrown during construction (invalid step config, missing env vars, runtime misconfiguration).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* try {
|
|
12
|
+
* loop({ id: '', steps: [], until: until.maxSteps(10) });
|
|
13
|
+
* } catch (e) {
|
|
14
|
+
* if (isNoeticConfigError(e)) {
|
|
15
|
+
* console.error(e.code, e.hint);
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare class NoeticConfigError extends Error {
|
|
21
|
+
/** SCREAMING_SNAKE_CASE code uniquely identifying this error condition. */
|
|
22
|
+
readonly code: string;
|
|
23
|
+
/** Complete sentence describing what the user should do next. */
|
|
24
|
+
readonly hint: string;
|
|
25
|
+
/** Link to the relevant docs page, if available. */
|
|
26
|
+
readonly docsUrl?: string;
|
|
27
|
+
constructor(opts: {
|
|
28
|
+
code: string;
|
|
29
|
+
message: string;
|
|
30
|
+
hint: string;
|
|
31
|
+
docsUrl?: string;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* @public
|
|
36
|
+
* Type guard for `NoeticConfigError`.
|
|
37
|
+
*
|
|
38
|
+
* @param e - Value to check.
|
|
39
|
+
* @returns `true` if `e` is a `NoeticConfigError`.
|
|
40
|
+
*/
|
|
41
|
+
export declare function isNoeticConfigError(e: unknown): e is NoeticConfigError;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @public
|
|
3
|
+
* Configuration-time error thrown during construction and setup.
|
|
4
|
+
*
|
|
5
|
+
* `NoeticConfigError` is separate from `NoeticError`. The two are never interchangeable:
|
|
6
|
+
* - `NoeticError` — thrown during execution (LLM failures, fork failures, budget exceeded).
|
|
7
|
+
* - `NoeticConfigError` — thrown during construction (invalid step config, missing env vars, runtime misconfiguration).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* try {
|
|
12
|
+
* loop({ id: '', steps: [], until: until.maxSteps(10) });
|
|
13
|
+
* } catch (e) {
|
|
14
|
+
* if (isNoeticConfigError(e)) {
|
|
15
|
+
* console.error(e.code, e.hint);
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export class NoeticConfigError extends Error {
|
|
21
|
+
/** SCREAMING_SNAKE_CASE code uniquely identifying this error condition. */
|
|
22
|
+
code;
|
|
23
|
+
/** Complete sentence describing what the user should do next. */
|
|
24
|
+
hint;
|
|
25
|
+
/** Link to the relevant docs page, if available. */
|
|
26
|
+
docsUrl;
|
|
27
|
+
constructor(opts) {
|
|
28
|
+
super(opts.message);
|
|
29
|
+
this.name = 'NoeticConfigError';
|
|
30
|
+
this.code = opts.code;
|
|
31
|
+
this.hint = opts.hint;
|
|
32
|
+
this.docsUrl = opts.docsUrl;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @public
|
|
37
|
+
* Type guard for `NoeticConfigError`.
|
|
38
|
+
*
|
|
39
|
+
* @param e - Value to check.
|
|
40
|
+
* @returns `true` if `e` is a `NoeticConfigError`.
|
|
41
|
+
*/
|
|
42
|
+
export function isNoeticConfigError(e) {
|
|
43
|
+
return e instanceof NoeticConfigError;
|
|
44
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { NoeticError } from '../types/error';
|
|
2
|
+
/**
|
|
3
|
+
* Runtime error thrown during step execution (LLM failures, fork failures, budget exceeded, etc.).
|
|
4
|
+
*
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export declare class NoeticErrorImpl extends Error {
|
|
8
|
+
readonly noeticError: NoeticError;
|
|
9
|
+
constructor(error: NoeticError);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Type guard for `NoeticErrorImpl`.
|
|
13
|
+
*
|
|
14
|
+
* @public
|
|
15
|
+
* @param e - Value to check.
|
|
16
|
+
* @returns `true` if `e` is a `NoeticErrorImpl`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function isNoeticError(e: unknown): e is NoeticErrorImpl;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime error thrown during step execution (LLM failures, fork failures, budget exceeded, etc.).
|
|
3
|
+
*
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export class NoeticErrorImpl extends Error {
|
|
7
|
+
noeticError;
|
|
8
|
+
constructor(error) {
|
|
9
|
+
super(formatMessage(error));
|
|
10
|
+
this.name = 'NoeticError';
|
|
11
|
+
this.noeticError = error;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Type guard for `NoeticErrorImpl`.
|
|
16
|
+
*
|
|
17
|
+
* @public
|
|
18
|
+
* @param e - Value to check.
|
|
19
|
+
* @returns `true` if `e` is a `NoeticErrorImpl`.
|
|
20
|
+
*/
|
|
21
|
+
export function isNoeticError(e) {
|
|
22
|
+
return e instanceof NoeticErrorImpl;
|
|
23
|
+
}
|
|
24
|
+
function formatMessage(error) {
|
|
25
|
+
switch (error.kind) {
|
|
26
|
+
case 'step_failed':
|
|
27
|
+
return `Step '${error.stepId}' failed: ${error.cause.message}`;
|
|
28
|
+
case 'llm_refused':
|
|
29
|
+
return `LLM refused at step '${error.stepId}': ${error.refusal}`;
|
|
30
|
+
case 'llm_parse_error':
|
|
31
|
+
return `LLM parse error at step '${error.stepId}': failed to parse output`;
|
|
32
|
+
case 'llm_rate_limit':
|
|
33
|
+
return `LLM rate limited at step '${error.stepId}'${error.retryAfter ? ` (retry after ${error.retryAfter}ms)` : ''}`;
|
|
34
|
+
case 'fork_partial':
|
|
35
|
+
return `Fork '${error.stepId}' partial failure: ${error.succeeded.length} succeeded, ${error.failed.length} failed`;
|
|
36
|
+
case 'channel_timeout':
|
|
37
|
+
return `Channel '${error.channelName}' timed out after ${error.timeout}ms`;
|
|
38
|
+
case 'channel_closed':
|
|
39
|
+
return `Channel '${error.channelName}' is closed`;
|
|
40
|
+
case 'cancelled':
|
|
41
|
+
return `Cancelled${error.reason ? `: ${error.reason}` : ''}`;
|
|
42
|
+
case 'budget_exceeded':
|
|
43
|
+
return `Budget exceeded: ${error.field} limit ${error.limit}, actual ${error.actual}`;
|
|
44
|
+
case 'steering_denied':
|
|
45
|
+
return `Steering denied${error.guidance ? `: ${error.guidance}` : ''}`;
|
|
46
|
+
default: {
|
|
47
|
+
const _exhaustive = error;
|
|
48
|
+
void _exhaustive;
|
|
49
|
+
return 'NoeticError: unknown kind';
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/** @public */
|
|
2
|
+
export { createInMemoryFsAdapter } from './adapters/in-memory-fs-adapter';
|
|
3
|
+
/** @public */
|
|
4
|
+
export { createInMemoryShellAdapter } from './adapters/in-memory-shell-adapter';
|
|
5
|
+
/** @public */
|
|
6
|
+
export { createInMemorySubprocessAdapter } from './adapters/in-memory-subprocess-adapter';
|
|
7
|
+
/** @public */
|
|
8
|
+
export { createLocalFsAdapter } from './adapters/local-fs-adapter';
|
|
9
|
+
/** @public */
|
|
10
|
+
export { type CreateLocalShellAdapterOptions, createLocalShellAdapter, type LocalShellAdapter, } from './adapters/local-shell-adapter';
|
|
11
|
+
/** @public */
|
|
12
|
+
export { type CreateLocalSubprocessAdapterOptions, createLocalSubprocessAdapter, defaultProcessSignaller, type ProcessSignaller, type SubprocessSignal, } from './adapters/local-subprocess-adapter';
|
|
13
|
+
/** @public */
|
|
14
|
+
export { createOpenRouterEmbed } from './adapters/openrouter';
|
|
15
|
+
/** @public */
|
|
16
|
+
export { channel } from './builders/channel-builder';
|
|
17
|
+
/** @public */
|
|
18
|
+
export { branch, fork } from './builders/control-flow-builders';
|
|
19
|
+
/** @public */
|
|
20
|
+
export type { EveryOptions } from './builders/every';
|
|
21
|
+
/** @public */
|
|
22
|
+
export { every } from './builders/every';
|
|
23
|
+
/** @public */
|
|
24
|
+
export { layerData, layerFn } from './builders/layer-provides-builders';
|
|
25
|
+
/** @public */
|
|
26
|
+
export type { LoopConfig } from './builders/loop-builder';
|
|
27
|
+
/** @public */
|
|
28
|
+
export { loop } from './builders/loop-builder';
|
|
29
|
+
/** @public */
|
|
30
|
+
export { memory } from './builders/memory-builder';
|
|
31
|
+
/** @public */
|
|
32
|
+
export { provide } from './builders/provide-builder';
|
|
33
|
+
/** @public */
|
|
34
|
+
export { spawn } from './builders/spawn-builder';
|
|
35
|
+
/** @public */
|
|
36
|
+
export { step } from './builders/step-builders';
|
|
37
|
+
/** @public */
|
|
38
|
+
export { tool, toolWithGenerator } from './builders/tool-builder';
|
|
39
|
+
/** @public */
|
|
40
|
+
export type { Condition, OtherwiseClause, WhenClause } from './conditions';
|
|
41
|
+
/** @public */
|
|
42
|
+
export { aiCondition, allCondition, anyCondition, cosineSimilarity, embeddingMatch, otherwise, semanticRoute, semanticSwitch, when, } from './conditions';
|
|
43
|
+
/** @public */
|
|
44
|
+
export { isNoeticConfigError, NoeticConfigError } from './errors/noetic-config-error';
|
|
45
|
+
/** @public */
|
|
46
|
+
export { isNoeticError, NoeticErrorImpl } from './errors/noetic-error';
|
|
47
|
+
/** @public */
|
|
48
|
+
export { execute } from './interpreter/execute';
|
|
49
|
+
/** @public */
|
|
50
|
+
export type { DurableTaskState, DurableTaskStateConfig } from './memory/layers/durable-task-state';
|
|
51
|
+
/** @public */
|
|
52
|
+
export { durableTaskState } from './memory/layers/durable-task-state';
|
|
53
|
+
/** @public */
|
|
54
|
+
export { fileReference } from './memory/layers/file-reference';
|
|
55
|
+
/** @public */
|
|
56
|
+
export type { HistoryWindowConfig } from './memory/layers/history-window';
|
|
57
|
+
/** @public */
|
|
58
|
+
export { historyWindow } from './memory/layers/history-window';
|
|
59
|
+
/** @public */
|
|
60
|
+
export type { ObservationalMemoryConfig, ObservationalState, } from './memory/layers/observational-memory';
|
|
61
|
+
/** @public */
|
|
62
|
+
export { observationalMemory } from './memory/layers/observational-memory';
|
|
63
|
+
/** @public */
|
|
64
|
+
export type { PlanEnterSessionCallback, PlanExecutionEntry, PlanExitCallback, PlanMemoryConfig, PlanState, } from './memory/layers/plan';
|
|
65
|
+
/** @public */
|
|
66
|
+
export { PlanPhase, planMemory } from './memory/layers/plan';
|
|
67
|
+
/** @public */
|
|
68
|
+
export { staticContent } from './memory/layers/static-content';
|
|
69
|
+
/** @public */
|
|
70
|
+
export { steering } from './memory/layers/steering';
|
|
71
|
+
/** @public */
|
|
72
|
+
export { toolMemoryLayer } from './memory/layers/tool-memory-layer';
|
|
73
|
+
/** @public */
|
|
74
|
+
export type { WorkingMemoryConfig, WorkingMemoryState } from './memory/layers/working-memory';
|
|
75
|
+
/** @public */
|
|
76
|
+
export { workingMemory } from './memory/layers/working-memory';
|
|
77
|
+
/** @public */
|
|
78
|
+
export { stripUnresolvedToolCalls } from './memory/strip-unresolved';
|
|
79
|
+
/** @public */
|
|
80
|
+
export { InMemoryExporter, NoopExporter } from './observability/trace-exporter';
|
|
81
|
+
/** @public */
|
|
82
|
+
export { createInMemoryStorage } from './runtime/in-memory-storage';
|
|
83
|
+
/** @public */
|
|
84
|
+
export type { FlowNode, ForkFlowNode, LlmFlowNode, SequenceFlowNode, SpawnFlowNode, SubagentFlowNode, } from './patterns/flow';
|
|
85
|
+
/** @public */
|
|
86
|
+
export { FlowSchema, flowDepth, validateFlow, walkFlow } from './patterns/flow';
|
|
87
|
+
/** @public */
|
|
88
|
+
export type { InterviewOpts, InterviewQuestionAnswer, InterviewResult } from './patterns/interview';
|
|
89
|
+
/** @public */
|
|
90
|
+
export { interview } from './patterns/interview';
|
|
91
|
+
/** @public */
|
|
92
|
+
export type { PlanConstraints, PlanNode } from './patterns/plans';
|
|
93
|
+
/** @public */
|
|
94
|
+
export { adaptivePlan, compilePlan, PlanNodeSchema } from './patterns/plans';
|
|
95
|
+
/** @public */
|
|
96
|
+
export { ralphWiggum } from './patterns/ralph-wiggum';
|
|
97
|
+
/** @public */
|
|
98
|
+
export { react } from './patterns/react';
|
|
99
|
+
/** @public */
|
|
100
|
+
export { AgentHarness } from './runtime/agent-harness';
|
|
101
|
+
/** @public */
|
|
102
|
+
export { defaultItemSchemaRegistry, ItemSchema, ItemSchemaRegistry } from './schemas/item';
|
|
103
|
+
/** @public */
|
|
104
|
+
export type { Channel, ChannelHandle, ExternalChannel } from './types/channel';
|
|
105
|
+
/** @public */
|
|
106
|
+
export type { LLMResponse, LlmProviderConfig, ModelParams, RetryPolicy, StepMeta, TokenUsage, Tool, ToolMemoryDeclaration, } from './types/common';
|
|
107
|
+
/** @public */
|
|
108
|
+
export { getToolCwd, setToolCwd, snapshotCwdState } from './runtime/cwd-helpers';
|
|
109
|
+
/** @public */
|
|
110
|
+
export type { Context, CwdState, ItemLog, LastLayerUsage, LayerUsageEntry, } from './types/context';
|
|
111
|
+
/** @public */
|
|
112
|
+
export type { DetachedHandle } from './types/detached';
|
|
113
|
+
/** @public */
|
|
114
|
+
export { DetachedStatus } from './types/detached';
|
|
115
|
+
/** @public */
|
|
116
|
+
export type { EmbedFn } from './types/embed';
|
|
117
|
+
/** @public */
|
|
118
|
+
export type { NoeticError } from './types/error';
|
|
119
|
+
/** @public */
|
|
120
|
+
export type { ContentPart, DeveloperMessageExtensionItem, ExecuteInput, ExtendedItem, ExtensionItem, FileSearchItem, FunctionCallItem, FunctionCallOutputItem, ImageGenerationItem, InferExtendedItem, InputMessageItem, InputTextPart, Item, ItemBase, ItemSchemaExtensions, MessageItem, OutputItem, OutputTextPart, ReasoningItem, ReasoningTextPart, RefusalPart, ServerToolItem, SummaryTextPart, WebSearchItem, } from './types/items';
|
|
121
|
+
/** @public */
|
|
122
|
+
export type { BudgetConfig, CompleteParams, ContextMemory, DisposeParams, ExecutionContext, ExecutionOutcome, InferMemory, InferMemoryShape, InitParams, InitResult, LayerDataDecl, LayerFunctionDecl, LayerProvides, LayerTimeouts, MemoryConfig, MemoryHooks, MemoryLayer, MemoryScope, ProjectionPolicy, RecallParams, RecallResult, ReturnParams, ReturnResult, ScopedStorage, SpawnParams, SpawnResult, StorageAdapter, StoreParams, StoreResult, } from './types/memory';
|
|
123
|
+
/** @public */
|
|
124
|
+
export { Slot } from './types/memory';
|
|
125
|
+
/** @public */
|
|
126
|
+
export type { MemoryTraceSpan, Span, TraceExporter } from './types/observability';
|
|
127
|
+
/** @public */
|
|
128
|
+
export type { FrameworkStreamEvent, HarnessResponse, SdkStreamEvent, StreamEvent, StreamingItem, } from './types/harness-result';
|
|
129
|
+
/** @public */
|
|
130
|
+
export type { AgentConfig, AgentHarnessContract, AgentHooks, CallModelRequest, DeliveryMode, ExecuteOptions, HarnessStatus, RecallLayerOutput, SessionScope, } from './types/runtime';
|
|
131
|
+
/** @public */
|
|
132
|
+
export type { AfterModelCallParams, AfterModelCallResult, BeforeToolCallParams, BeforeToolCallResult, LedgerEntry, SteeringConfig, SteeringDecision, SteeringRule, SteeringState, } from './types/steering';
|
|
133
|
+
/** @public */
|
|
134
|
+
export { LedgerEntryKind, SteeringAction } from './types/steering';
|
|
135
|
+
/** @public */
|
|
136
|
+
export type { ExecuteStepFn, SettleResult, Snapshot, Step, StepBranch, StepFork, StepForkAll, StepForkRace, StepForkSettle, StepLLM, StepLoop, StepProvide, StepRun, StepSpawn, StepTool, Until, Verdict, } from './types/step';
|
|
137
|
+
/** @public */
|
|
138
|
+
export type { FsAdapter, FsStats } from './types/fs-adapter';
|
|
139
|
+
/** @public */
|
|
140
|
+
export type { ShellAdapter, ShellExecOptions, ShellExecResult } from './types/shell-adapter';
|
|
141
|
+
/** @public */
|
|
142
|
+
export { TIMEOUT_ERROR_PREFIX } from './types/shell-adapter';
|
|
143
|
+
/** @public */
|
|
144
|
+
export type { SubprocessAdapter, SubprocessControlResult, SubprocessHandle, SubprocessRequest, SubprocessStatus, SubprocessStopResult, } from './types/subprocess-adapter';
|
|
145
|
+
/** @public */
|
|
146
|
+
export type { ToolExecutionContext, ToolMemory } from './types/tool-context';
|
|
147
|
+
/** @public */
|
|
148
|
+
export { all, any } from './until/combinators';
|
|
149
|
+
/** @public */
|
|
150
|
+
export type { ConvergeConfig, VerifyFn } from './until/predicates';
|
|
151
|
+
/** @public */
|
|
152
|
+
export { until } from './until/predicates';
|