@cogitator-ai/core 0.1.0 → 0.3.0
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/README.md +920 -15
- package/dist/__tests__/agent.test.js +2 -2
- package/dist/__tests__/agent.test.js.map +1 -1
- package/dist/__tests__/base64.test.js +1 -1
- package/dist/__tests__/base64.test.js.map +1 -1
- package/dist/__tests__/calculator.test.js +1 -1
- package/dist/__tests__/calculator.test.js.map +1 -1
- package/dist/__tests__/cogitator-memory.test.js +2 -2
- package/dist/__tests__/cogitator-memory.test.js.map +1 -1
- package/dist/__tests__/datetime.test.js +1 -1
- package/dist/__tests__/datetime.test.js.map +1 -1
- package/dist/__tests__/exec.test.js +1 -1
- package/dist/__tests__/exec.test.js.map +1 -1
- package/dist/__tests__/filesystem.test.js +1 -1
- package/dist/__tests__/filesystem.test.js.map +1 -1
- package/dist/__tests__/google-backend.test.js +1 -1
- package/dist/__tests__/google-backend.test.js.map +1 -1
- package/dist/__tests__/hash.test.js +1 -1
- package/dist/__tests__/hash.test.js.map +1 -1
- package/dist/__tests__/http.test.js +1 -1
- package/dist/__tests__/http.test.js.map +1 -1
- package/dist/__tests__/json.test.js +1 -1
- package/dist/__tests__/json.test.js.map +1 -1
- package/dist/__tests__/logger.test.js +1 -1
- package/dist/__tests__/logger.test.js.map +1 -1
- package/dist/__tests__/random.test.js +1 -1
- package/dist/__tests__/random.test.js.map +1 -1
- package/dist/__tests__/regex.test.js +1 -1
- package/dist/__tests__/regex.test.js.map +1 -1
- package/dist/__tests__/registry.test.js +2 -2
- package/dist/__tests__/registry.test.js.map +1 -1
- package/dist/__tests__/sleep.test.js +1 -1
- package/dist/__tests__/sleep.test.js.map +1 -1
- package/dist/__tests__/tool.test.js +1 -1
- package/dist/__tests__/tool.test.js.map +1 -1
- package/dist/__tests__/uuid.test.js +1 -1
- package/dist/__tests__/uuid.test.js.map +1 -1
- package/dist/cogitator.d.ts +46 -1
- package/dist/cogitator.d.ts.map +1 -1
- package/dist/cogitator.js +274 -17
- package/dist/cogitator.js.map +1 -1
- package/dist/constitutional/constitution.d.ts +9 -0
- package/dist/constitutional/constitution.d.ts.map +1 -0
- package/dist/constitutional/constitution.js +215 -0
- package/dist/constitutional/constitution.js.map +1 -0
- package/dist/constitutional/constitutional-ai.d.ts +36 -0
- package/dist/constitutional/constitutional-ai.d.ts.map +1 -0
- package/dist/constitutional/constitutional-ai.js +163 -0
- package/dist/constitutional/constitutional-ai.js.map +1 -0
- package/dist/constitutional/critique-reviser.d.ts +20 -0
- package/dist/constitutional/critique-reviser.d.ts.map +1 -0
- package/dist/constitutional/critique-reviser.js +98 -0
- package/dist/constitutional/critique-reviser.js.map +1 -0
- package/dist/constitutional/index.d.ts +13 -0
- package/dist/constitutional/index.d.ts.map +1 -0
- package/dist/constitutional/index.js +8 -0
- package/dist/constitutional/index.js.map +1 -0
- package/dist/constitutional/input-filter.d.ts +19 -0
- package/dist/constitutional/input-filter.d.ts.map +1 -0
- package/dist/constitutional/input-filter.js +88 -0
- package/dist/constitutional/input-filter.js.map +1 -0
- package/dist/constitutional/output-filter.d.ts +19 -0
- package/dist/constitutional/output-filter.d.ts.map +1 -0
- package/dist/constitutional/output-filter.js +86 -0
- package/dist/constitutional/output-filter.js.map +1 -0
- package/dist/constitutional/prompts.d.ts +11 -0
- package/dist/constitutional/prompts.d.ts.map +1 -0
- package/dist/constitutional/prompts.js +202 -0
- package/dist/constitutional/prompts.js.map +1 -0
- package/dist/constitutional/tool-guard.d.ts +18 -0
- package/dist/constitutional/tool-guard.d.ts.map +1 -0
- package/dist/constitutional/tool-guard.js +125 -0
- package/dist/constitutional/tool-guard.js.map +1 -0
- package/dist/cost-routing/budget-enforcer.d.ts +26 -0
- package/dist/cost-routing/budget-enforcer.d.ts.map +1 -0
- package/dist/cost-routing/budget-enforcer.js +86 -0
- package/dist/cost-routing/budget-enforcer.js.map +1 -0
- package/dist/cost-routing/cost-router.d.ts +34 -0
- package/dist/cost-routing/cost-router.d.ts.map +1 -0
- package/dist/cost-routing/cost-router.js +80 -0
- package/dist/cost-routing/cost-router.js.map +1 -0
- package/dist/cost-routing/cost-tracker.d.ts +20 -0
- package/dist/cost-routing/cost-tracker.d.ts.map +1 -0
- package/dist/cost-routing/cost-tracker.js +85 -0
- package/dist/cost-routing/cost-tracker.js.map +1 -0
- package/dist/cost-routing/index.d.ts +6 -0
- package/dist/cost-routing/index.d.ts.map +1 -0
- package/dist/cost-routing/index.js +6 -0
- package/dist/cost-routing/index.js.map +1 -0
- package/dist/cost-routing/model-selector.d.ts +15 -0
- package/dist/cost-routing/model-selector.d.ts.map +1 -0
- package/dist/cost-routing/model-selector.js +216 -0
- package/dist/cost-routing/model-selector.js.map +1 -0
- package/dist/cost-routing/task-analyzer.d.ts +13 -0
- package/dist/cost-routing/task-analyzer.d.ts.map +1 -0
- package/dist/cost-routing/task-analyzer.js +185 -0
- package/dist/cost-routing/task-analyzer.js.map +1 -0
- package/dist/index.d.ts +19 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -1
- package/dist/learning/ab-testing.d.ts +45 -0
- package/dist/learning/ab-testing.d.ts.map +1 -0
- package/dist/learning/ab-testing.js +267 -0
- package/dist/learning/ab-testing.js.map +1 -0
- package/dist/learning/agent-optimizer.d.ts +42 -0
- package/dist/learning/agent-optimizer.d.ts.map +1 -0
- package/dist/learning/agent-optimizer.js +273 -0
- package/dist/learning/agent-optimizer.js.map +1 -0
- package/dist/learning/auto-optimizer.d.ts +38 -0
- package/dist/learning/auto-optimizer.d.ts.map +1 -0
- package/dist/learning/auto-optimizer.js +229 -0
- package/dist/learning/auto-optimizer.js.map +1 -0
- package/dist/learning/demo-selector.d.ts +29 -0
- package/dist/learning/demo-selector.d.ts.map +1 -0
- package/dist/learning/demo-selector.js +235 -0
- package/dist/learning/demo-selector.js.map +1 -0
- package/dist/learning/index.d.ts +24 -0
- package/dist/learning/index.d.ts.map +1 -0
- package/dist/learning/index.js +13 -0
- package/dist/learning/index.js.map +1 -0
- package/dist/learning/instruction-optimizer.d.ts +29 -0
- package/dist/learning/instruction-optimizer.d.ts.map +1 -0
- package/dist/learning/instruction-optimizer.js +175 -0
- package/dist/learning/instruction-optimizer.js.map +1 -0
- package/dist/learning/metrics.d.ts +37 -0
- package/dist/learning/metrics.d.ts.map +1 -0
- package/dist/learning/metrics.js +310 -0
- package/dist/learning/metrics.js.map +1 -0
- package/dist/learning/postgres-trace-store.d.ts +53 -0
- package/dist/learning/postgres-trace-store.d.ts.map +1 -0
- package/dist/learning/postgres-trace-store.js +692 -0
- package/dist/learning/postgres-trace-store.js.map +1 -0
- package/dist/learning/prompt-logger.d.ts +29 -0
- package/dist/learning/prompt-logger.d.ts.map +1 -0
- package/dist/learning/prompt-logger.js +157 -0
- package/dist/learning/prompt-logger.js.map +1 -0
- package/dist/learning/prompt-monitor.d.ts +29 -0
- package/dist/learning/prompt-monitor.d.ts.map +1 -0
- package/dist/learning/prompt-monitor.js +243 -0
- package/dist/learning/prompt-monitor.js.map +1 -0
- package/dist/learning/prompts.d.ts +28 -0
- package/dist/learning/prompts.d.ts.map +1 -0
- package/dist/learning/prompts.js +195 -0
- package/dist/learning/prompts.js.map +1 -0
- package/dist/learning/rollback-manager.d.ts +36 -0
- package/dist/learning/rollback-manager.d.ts.map +1 -0
- package/dist/learning/rollback-manager.js +177 -0
- package/dist/learning/rollback-manager.js.map +1 -0
- package/dist/learning/trace-store.d.ts +26 -0
- package/dist/learning/trace-store.d.ts.map +1 -0
- package/dist/learning/trace-store.js +218 -0
- package/dist/learning/trace-store.js.map +1 -0
- package/dist/llm/google.d.ts.map +1 -1
- package/dist/llm/google.js +1 -2
- package/dist/llm/google.js.map +1 -1
- package/dist/reasoning/branch-evaluator.d.ts +28 -0
- package/dist/reasoning/branch-evaluator.d.ts.map +1 -0
- package/dist/reasoning/branch-evaluator.js +143 -0
- package/dist/reasoning/branch-evaluator.js.map +1 -0
- package/dist/reasoning/branch-generator.d.ts +9 -0
- package/dist/reasoning/branch-generator.d.ts.map +1 -0
- package/dist/reasoning/branch-generator.js +60 -0
- package/dist/reasoning/branch-generator.js.map +1 -0
- package/dist/reasoning/index.d.ts +5 -0
- package/dist/reasoning/index.d.ts.map +1 -0
- package/dist/reasoning/index.js +5 -0
- package/dist/reasoning/index.js.map +1 -0
- package/dist/reasoning/prompts.d.ts +19 -0
- package/dist/reasoning/prompts.d.ts.map +1 -0
- package/dist/reasoning/prompts.js +161 -0
- package/dist/reasoning/prompts.js.map +1 -0
- package/dist/reasoning/thought-tree.d.ts +32 -0
- package/dist/reasoning/thought-tree.d.ts.map +1 -0
- package/dist/reasoning/thought-tree.js +352 -0
- package/dist/reasoning/thought-tree.js.map +1 -0
- package/dist/reflection/index.d.ts +4 -0
- package/dist/reflection/index.d.ts.map +1 -0
- package/dist/reflection/index.js +4 -0
- package/dist/reflection/index.js.map +1 -0
- package/dist/reflection/insight-store.d.ts +19 -0
- package/dist/reflection/insight-store.d.ts.map +1 -0
- package/dist/reflection/insight-store.js +129 -0
- package/dist/reflection/insight-store.js.map +1 -0
- package/dist/reflection/prompts.d.ts +18 -0
- package/dist/reflection/prompts.d.ts.map +1 -0
- package/dist/reflection/prompts.js +157 -0
- package/dist/reflection/prompts.js.map +1 -0
- package/dist/reflection/reflection-engine.d.ts +25 -0
- package/dist/reflection/reflection-engine.d.ts.map +1 -0
- package/dist/reflection/reflection-engine.js +202 -0
- package/dist/reflection/reflection-engine.js.map +1 -0
- package/dist/registry.d.ts +1 -0
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +3 -0
- package/dist/registry.js.map +1 -1
- package/dist/time-travel/checkpoint-store.d.ts +34 -0
- package/dist/time-travel/checkpoint-store.d.ts.map +1 -0
- package/dist/time-travel/checkpoint-store.js +240 -0
- package/dist/time-travel/checkpoint-store.js.map +1 -0
- package/dist/time-travel/comparator.d.ts +26 -0
- package/dist/time-travel/comparator.d.ts.map +1 -0
- package/dist/time-travel/comparator.js +253 -0
- package/dist/time-travel/comparator.js.map +1 -0
- package/dist/time-travel/forker.d.ts +22 -0
- package/dist/time-travel/forker.d.ts.map +1 -0
- package/dist/time-travel/forker.js +118 -0
- package/dist/time-travel/forker.js.map +1 -0
- package/dist/time-travel/index.d.ts +6 -0
- package/dist/time-travel/index.d.ts.map +1 -0
- package/dist/time-travel/index.js +6 -0
- package/dist/time-travel/index.js.map +1 -0
- package/dist/time-travel/replayer.d.ts +20 -0
- package/dist/time-travel/replayer.d.ts.map +1 -0
- package/dist/time-travel/replayer.js +147 -0
- package/dist/time-travel/replayer.js.map +1 -0
- package/dist/time-travel/time-travel.d.ts +41 -0
- package/dist/time-travel/time-travel.d.ts.map +1 -0
- package/dist/time-travel/time-travel.js +127 -0
- package/dist/time-travel/time-travel.js.map +1 -0
- package/dist/tool.d.ts.map +1 -1
- package/dist/tool.js +2 -0
- package/dist/tool.js.map +1 -1
- package/dist/tools/base64.d.ts.map +1 -1
- package/dist/tools/base64.js +2 -8
- package/dist/tools/base64.js.map +1 -1
- package/dist/tools/datetime.d.ts.map +1 -1
- package/dist/tools/datetime.js.map +1 -1
- package/dist/tools/exec.d.ts.map +1 -1
- package/dist/tools/exec.js +1 -4
- package/dist/tools/exec.js.map +1 -1
- package/dist/tools/filesystem.d.ts.map +1 -1
- package/dist/tools/filesystem.js +4 -1
- package/dist/tools/filesystem.js.map +1 -1
- package/dist/tools/hash.d.ts.map +1 -1
- package/dist/tools/hash.js +1 -4
- package/dist/tools/hash.js.map +1 -1
- package/dist/tools/http.d.ts.map +1 -1
- package/dist/tools/http.js +1 -4
- package/dist/tools/http.js.map +1 -1
- package/dist/tools/regex.d.ts.map +1 -1
- package/dist/tools/regex.js +4 -1
- package/dist/tools/regex.js.map +1 -1
- package/dist/utils/circuit-breaker.d.ts.map +1 -1
- package/dist/utils/circuit-breaker.js.map +1 -1
- package/dist/utils/fallback.d.ts.map +1 -1
- package/dist/utils/fallback.js +1 -4
- package/dist/utils/fallback.js.map +1 -1
- package/dist/utils/retry.d.ts.map +1 -1
- package/dist/utils/retry.js +8 -13
- package/dist/utils/retry.js.map +1 -1
- package/package.json +17 -8
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sleep.test.js","sourceRoot":"","sources":["../../src/__tests__/sleep.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"sleep.test.js","sourceRoot":"","sources":["../../src/__tests__/sleep.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAE1C,MAAM,WAAW,GAAG;IAClB,OAAO,EAAE,YAAY;IACrB,KAAK,EAAE,UAAU;IACjB,MAAM,EAAE,IAAI,eAAe,EAAE,CAAC,MAAM;CACrC,CAAC;AAEF,QAAQ,CAAC,YAAY,EAAE,GAAG,EAAE;IAC1B,EAAE,CAAC,+BAA+B,EAAE,KAAK,IAAI,EAAE;QAC7C,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACzB,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,OAAO,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,WAAW,CAAC,CAAC;QAC7D,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC;QAEnC,MAAM,CAAC,MAAM,CAAC,CAAC,cAAc,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC;QAChD,MAAM,CAAC,MAAM,CAAC,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;QACvC,MAAM,CAAC,OAAO,CAAC,CAAC,sBAAsB,CAAC,EAAE,CAAC,CAAC;QAC3C,MAAM,CAAC,OAAO,CAAC,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC;IACpC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,gBAAgB,EAAE,KAAK,IAAI,EAAE;QAC9B,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,OAAO,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,WAAW,CAAC,CAAC;QAC3D,MAAM,CAAC,MAAM,CAAC,CAAC,cAAc,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC;IAChD,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,sBAAsB,EAAE,GAAG,EAAE;QAC9B,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACjC,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,WAAW,EAAE,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;QAC3D,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;QAC9B,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;IAC5D,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, it, expect } from 'vitest';
|
|
2
2
|
import { z } from 'zod';
|
|
3
|
-
import { tool, toolToSchema } from '../tool';
|
|
3
|
+
import { tool, toolToSchema } from '../tool.js';
|
|
4
4
|
describe('tool()', () => {
|
|
5
5
|
it('creates a tool with required properties', () => {
|
|
6
6
|
const calculator = tool({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool.test.js","sourceRoot":"","sources":["../../src/__tests__/tool.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,IAAI,EA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C5D,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;IAC9D,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uuid.test.js","sourceRoot":"","sources":["../../src/__tests__/uuid.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,IAAI,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"uuid.test.js","sourceRoot":"","sources":["../../src/__tests__/uuid.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAExC,MAAM,WAAW,GAAG;IAClB,OAAO,EAAE,YAAY;IACrB,KAAK,EAAE,UAAU;IACjB,MAAM,EAAE,IAAI,eAAe,EAAE,CAAC,MAAM;CACrC,CAAC;AAEF,QAAQ,CAAC,WAAW,EAAE,GAAG,EAAE;IACzB,EAAE,CAAC,oCAAoC,EAAE,KAAK,IAAI,EAAE;QAClD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,EAAE,EAAE,WAAW,CAAC,CAAC;QACnD,MAAM,CAAC,MAAM,CAAC,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;QACtC,MAAM,CAAE,MAA2B,CAAC,IAAI,CAAC,CAAC,OAAO,CAC/C,wEAAwE,CACzE,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,kDAAkD,EAAE,KAAK,IAAI,EAAE;QAChE,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,WAAW,CAAC,CAAC;QAC7D,MAAM,CAAC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11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Cogitator - Main runtime class
|
|
3
3
|
*/
|
|
4
|
-
import type { CogitatorConfig, RunOptions, RunResult } from '@cogitator-ai/types';
|
|
4
|
+
import type { CogitatorConfig, RunOptions, RunResult, Constitution, CostSummary } from '@cogitator-ai/types';
|
|
5
5
|
import { type Agent } from './agent';
|
|
6
6
|
import { ToolRegistry } from './registry';
|
|
7
|
+
import { ConstitutionalAI } from './constitutional/index';
|
|
8
|
+
import { CostAwareRouter } from './cost-routing/index';
|
|
7
9
|
export declare class Cogitator {
|
|
8
10
|
private config;
|
|
9
11
|
private backends;
|
|
@@ -13,6 +15,13 @@ export declare class Cogitator {
|
|
|
13
15
|
private memoryInitialized;
|
|
14
16
|
private sandboxManager?;
|
|
15
17
|
private sandboxInitialized;
|
|
18
|
+
private reflectionEngine?;
|
|
19
|
+
private insightStore?;
|
|
20
|
+
private reflectionInitialized;
|
|
21
|
+
private constitutionalAI?;
|
|
22
|
+
private guardrailsInitialized;
|
|
23
|
+
private costRouter?;
|
|
24
|
+
private costRoutingInitialized;
|
|
16
25
|
constructor(config?: CogitatorConfig);
|
|
17
26
|
/**
|
|
18
27
|
* Initialize memory adapter and context builder (lazy, on first run)
|
|
@@ -22,6 +31,18 @@ export declare class Cogitator {
|
|
|
22
31
|
* Initialize sandbox manager (lazy, on first sandboxed tool execution)
|
|
23
32
|
*/
|
|
24
33
|
private initializeSandbox;
|
|
34
|
+
/**
|
|
35
|
+
* Initialize reflection engine (lazy, on first run with reflection enabled)
|
|
36
|
+
*/
|
|
37
|
+
private initializeReflection;
|
|
38
|
+
/**
|
|
39
|
+
* Initialize constitutional AI guardrails (lazy, on first run with guardrails enabled)
|
|
40
|
+
*/
|
|
41
|
+
private initializeGuardrails;
|
|
42
|
+
/**
|
|
43
|
+
* Initialize cost-aware routing (lazy, on first run with cost routing enabled)
|
|
44
|
+
*/
|
|
45
|
+
private initializeCostRouting;
|
|
25
46
|
/**
|
|
26
47
|
* Build initial messages for a run, loading history if memory is enabled
|
|
27
48
|
*/
|
|
@@ -58,6 +79,30 @@ export declare class Cogitator {
|
|
|
58
79
|
* Calculate cost based on model and tokens using dynamic model registry
|
|
59
80
|
*/
|
|
60
81
|
private calculateCost;
|
|
82
|
+
/**
|
|
83
|
+
* Get accumulated insights for an agent
|
|
84
|
+
*/
|
|
85
|
+
getInsights(agentId: string): Promise<import("@cogitator-ai/types").Insight[]>;
|
|
86
|
+
/**
|
|
87
|
+
* Get reflection summary for an agent
|
|
88
|
+
*/
|
|
89
|
+
getReflectionSummary(agentId: string): Promise<import("@cogitator-ai/types").ReflectionSummary | null>;
|
|
90
|
+
/**
|
|
91
|
+
* Get the constitutional AI guardrails instance
|
|
92
|
+
*/
|
|
93
|
+
getGuardrails(): ConstitutionalAI | undefined;
|
|
94
|
+
/**
|
|
95
|
+
* Set the constitution for guardrails
|
|
96
|
+
*/
|
|
97
|
+
setConstitution(constitution: Constitution): void;
|
|
98
|
+
/**
|
|
99
|
+
* Get cost-aware routing summary
|
|
100
|
+
*/
|
|
101
|
+
getCostSummary(): CostSummary | undefined;
|
|
102
|
+
/**
|
|
103
|
+
* Get the cost-aware router instance
|
|
104
|
+
*/
|
|
105
|
+
getCostRouter(): CostAwareRouter | undefined;
|
|
61
106
|
/**
|
|
62
107
|
* Close all connections
|
|
63
108
|
*/
|
package/dist/cogitator.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cogitator.d.ts","sourceRoot":"","sources":["../src/cogitator.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,KAAK,EACV,eAAe,EACf,UAAU,EACV,SAAS,
|
|
1
|
+
{"version":3,"file":"cogitator.d.ts","sourceRoot":"","sources":["../src/cogitator.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,KAAK,EACV,eAAe,EACf,UAAU,EACV,SAAS,EAcT,YAAY,EACZ,WAAW,EACZ,MAAM,qBAAqB,CAAC;AAW7B,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F9B;;OAEG;IACH,OAAO,CAAC,UAAU;IAalB;;OAEG;IACH,OAAO,CAAC,aAAa;IAWrB;;OAEG;IACG,WAAW,CAAC,OAAO,EAAE,MAAM;IAKjC;;OAEG;IACG,oBAAoB,CAAC,OAAO,EAAE,MAAM;IAK1C;;OAEG;IACH,aAAa,IAAI,gBAAgB,GAAG,SAAS;IAI7C;;OAEG;IACH,eAAe,CAAC,YAAY,EAAE,YAAY,GAAG,IAAI;IAIjD;;OAEG;IACH,cAAc,IAAI,WAAW,GAAG,SAAS;IAIzC;;OAEG;IACH,aAAa,IAAI,eAAe,GAAG,SAAS;IAI5C;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAqB7B"}
|
package/dist/cogitator.js
CHANGED
|
@@ -7,6 +7,9 @@ import { getPrice } from '@cogitator-ai/models';
|
|
|
7
7
|
import { ToolRegistry } from './registry';
|
|
8
8
|
import { createLLMBackend, parseModel } from './llm/index';
|
|
9
9
|
import { getLogger } from './logger';
|
|
10
|
+
import { ReflectionEngine, InMemoryInsightStore } from './reflection/index';
|
|
11
|
+
import { ConstitutionalAI } from './constitutional/index';
|
|
12
|
+
import { CostAwareRouter } from './cost-routing/index';
|
|
10
13
|
export class Cogitator {
|
|
11
14
|
config;
|
|
12
15
|
backends = new Map();
|
|
@@ -16,6 +19,13 @@ export class Cogitator {
|
|
|
16
19
|
memoryInitialized = false;
|
|
17
20
|
sandboxManager;
|
|
18
21
|
sandboxInitialized = false;
|
|
22
|
+
reflectionEngine;
|
|
23
|
+
insightStore;
|
|
24
|
+
reflectionInitialized = false;
|
|
25
|
+
constitutionalAI;
|
|
26
|
+
guardrailsInitialized = false;
|
|
27
|
+
costRouter;
|
|
28
|
+
costRoutingInitialized = false;
|
|
19
29
|
constructor(config = {}) {
|
|
20
30
|
this.config = config;
|
|
21
31
|
}
|
|
@@ -96,6 +106,44 @@ export class Cogitator {
|
|
|
96
106
|
this.sandboxInitialized = true;
|
|
97
107
|
}
|
|
98
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* Initialize reflection engine (lazy, on first run with reflection enabled)
|
|
111
|
+
*/
|
|
112
|
+
async initializeReflection(agent) {
|
|
113
|
+
if (this.reflectionInitialized || !this.config.reflection?.enabled)
|
|
114
|
+
return;
|
|
115
|
+
const backend = this.getBackend(this.config.reflection.reflectionModel ?? agent.model);
|
|
116
|
+
this.insightStore = new InMemoryInsightStore();
|
|
117
|
+
this.reflectionEngine = new ReflectionEngine({
|
|
118
|
+
llm: backend,
|
|
119
|
+
insightStore: this.insightStore,
|
|
120
|
+
config: this.config.reflection,
|
|
121
|
+
});
|
|
122
|
+
this.reflectionInitialized = true;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Initialize constitutional AI guardrails (lazy, on first run with guardrails enabled)
|
|
126
|
+
*/
|
|
127
|
+
initializeGuardrails(agent) {
|
|
128
|
+
if (this.guardrailsInitialized || !this.config.guardrails?.enabled)
|
|
129
|
+
return;
|
|
130
|
+
const backend = this.getBackend(this.config.guardrails.model ?? agent.model);
|
|
131
|
+
this.constitutionalAI = new ConstitutionalAI({
|
|
132
|
+
llm: backend,
|
|
133
|
+
constitution: this.config.guardrails.constitution,
|
|
134
|
+
config: this.config.guardrails,
|
|
135
|
+
});
|
|
136
|
+
this.guardrailsInitialized = true;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Initialize cost-aware routing (lazy, on first run with cost routing enabled)
|
|
140
|
+
*/
|
|
141
|
+
initializeCostRouting() {
|
|
142
|
+
if (this.costRoutingInitialized || !this.config.costRouting?.enabled)
|
|
143
|
+
return;
|
|
144
|
+
this.costRouter = new CostAwareRouter({ config: this.config.costRouting });
|
|
145
|
+
this.costRoutingInitialized = true;
|
|
146
|
+
}
|
|
99
147
|
/**
|
|
100
148
|
* Build initial messages for a run, loading history if memory is enabled
|
|
101
149
|
*/
|
|
@@ -135,7 +183,7 @@ export class Cogitator {
|
|
|
135
183
|
/**
|
|
136
184
|
* Save a message entry to memory (non-blocking, won't crash on failure)
|
|
137
185
|
*/
|
|
138
|
-
async saveEntry(threadId, message, toolCalls, toolResults) {
|
|
186
|
+
async saveEntry(threadId, message, toolCalls, toolResults, onError) {
|
|
139
187
|
if (!this.memoryAdapter)
|
|
140
188
|
return;
|
|
141
189
|
try {
|
|
@@ -147,8 +195,10 @@ export class Cogitator {
|
|
|
147
195
|
tokenCount: countMessageTokens(message),
|
|
148
196
|
});
|
|
149
197
|
}
|
|
150
|
-
catch (
|
|
151
|
-
|
|
198
|
+
catch (err) {
|
|
199
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
200
|
+
getLogger().warn('Failed to save memory entry', { error: error.message });
|
|
201
|
+
onError?.(error, 'save');
|
|
152
202
|
}
|
|
153
203
|
}
|
|
154
204
|
/**
|
|
@@ -179,19 +229,51 @@ export class Cogitator {
|
|
|
179
229
|
const traceId = `trace_${nanoid(16)}`;
|
|
180
230
|
const startTime = Date.now();
|
|
181
231
|
const spans = [];
|
|
232
|
+
const timeout = options.timeout ?? agent.config?.timeout;
|
|
233
|
+
const abortController = new AbortController();
|
|
234
|
+
let timeoutId;
|
|
235
|
+
if (timeout && timeout > 0) {
|
|
236
|
+
timeoutId = setTimeout(() => {
|
|
237
|
+
abortController.abort(new Error(`Run timed out after ${timeout}ms`));
|
|
238
|
+
}, timeout);
|
|
239
|
+
}
|
|
182
240
|
options.onRunStart?.({ runId, agentId: agent.id, input: options.input, threadId });
|
|
183
241
|
const rootSpanId = `span_${nanoid(12)}`;
|
|
184
242
|
try {
|
|
185
243
|
if (this.config.memory?.adapter && !this.memoryInitialized) {
|
|
186
244
|
await this.initializeMemory();
|
|
187
245
|
}
|
|
246
|
+
if (this.config.reflection?.enabled && !this.reflectionInitialized) {
|
|
247
|
+
await this.initializeReflection(agent);
|
|
248
|
+
}
|
|
249
|
+
if (this.config.guardrails?.enabled && !this.guardrailsInitialized) {
|
|
250
|
+
this.initializeGuardrails(agent);
|
|
251
|
+
}
|
|
252
|
+
if (this.config.costRouting?.enabled && !this.costRoutingInitialized) {
|
|
253
|
+
this.initializeCostRouting();
|
|
254
|
+
}
|
|
188
255
|
const registry = new ToolRegistry();
|
|
189
256
|
if (agent.tools && agent.tools.length > 0) {
|
|
190
257
|
registry.registerMany(agent.tools);
|
|
191
258
|
}
|
|
192
|
-
|
|
193
|
-
|
|
259
|
+
let effectiveModel = agent.model;
|
|
260
|
+
if (this.costRouter && this.config.costRouting?.autoSelectModel) {
|
|
261
|
+
const recommendation = await this.costRouter.recommendModel(options.input);
|
|
262
|
+
effectiveModel = `${recommendation.provider}/${recommendation.modelId}`;
|
|
263
|
+
const budgetCheck = this.costRouter.checkBudget(recommendation.estimatedCost);
|
|
264
|
+
if (!budgetCheck.allowed) {
|
|
265
|
+
throw new Error(`Budget exceeded: ${budgetCheck.reason}`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
const backend = this.getBackend(effectiveModel);
|
|
269
|
+
const { model } = parseModel(effectiveModel);
|
|
194
270
|
const messages = await this.buildInitialMessages(agent, options, threadId);
|
|
271
|
+
if (this.constitutionalAI && this.config.guardrails?.filterInput) {
|
|
272
|
+
const inputResult = await this.constitutionalAI.filterInput(options.input);
|
|
273
|
+
if (!inputResult.allowed) {
|
|
274
|
+
throw new Error(`Input blocked: ${inputResult.blockedReason ?? 'Policy violation'}`);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
195
277
|
if (options.context && messages.length > 0 && messages[0].role === 'system') {
|
|
196
278
|
const contextStr = Object.entries(options.context)
|
|
197
279
|
.map(([k, v]) => `${k}: ${JSON.stringify(v)}`)
|
|
@@ -199,15 +281,38 @@ export class Cogitator {
|
|
|
199
281
|
messages[0].content += `\n\nContext:\n${contextStr}`;
|
|
200
282
|
}
|
|
201
283
|
if (this.memoryAdapter && options.saveHistory !== false && options.useMemory !== false) {
|
|
202
|
-
await this.saveEntry(threadId, { role: 'user', content: options.input });
|
|
284
|
+
await this.saveEntry(threadId, { role: 'user', content: options.input }, undefined, undefined, options.onMemoryError);
|
|
203
285
|
}
|
|
204
286
|
const allToolCalls = [];
|
|
205
287
|
let totalInputTokens = 0;
|
|
206
288
|
let totalOutputTokens = 0;
|
|
207
289
|
let iterations = 0;
|
|
208
290
|
const maxIterations = agent.config?.maxIterations ?? 10;
|
|
291
|
+
const allReflections = [];
|
|
292
|
+
const allActions = [];
|
|
293
|
+
const agentContext = {
|
|
294
|
+
agentId: agent.id,
|
|
295
|
+
agentName: agent.name,
|
|
296
|
+
runId,
|
|
297
|
+
threadId,
|
|
298
|
+
goal: options.input,
|
|
299
|
+
iterationIndex: 0,
|
|
300
|
+
previousActions: [],
|
|
301
|
+
availableTools: registry.getNames(),
|
|
302
|
+
};
|
|
303
|
+
if (this.reflectionEngine && this.config.reflection?.enabled) {
|
|
304
|
+
const insights = await this.reflectionEngine.getRelevantInsights(agentContext);
|
|
305
|
+
if (insights.length > 0 && messages.length > 0 && messages[0].role === 'system') {
|
|
306
|
+
messages[0].content += `\n\nPast learnings that may help:\n${insights.map((i) => `- ${i.content}`).join('\n')}`;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
209
309
|
while (iterations < maxIterations) {
|
|
310
|
+
if (abortController.signal.aborted) {
|
|
311
|
+
throw abortController.signal.reason ?? new Error('Run aborted');
|
|
312
|
+
}
|
|
210
313
|
iterations++;
|
|
314
|
+
agentContext.iterationIndex = iterations - 1;
|
|
315
|
+
agentContext.previousActions = [...allActions];
|
|
211
316
|
const llmSpanStart = Date.now();
|
|
212
317
|
let response;
|
|
213
318
|
if (options.stream && options.onToken) {
|
|
@@ -234,20 +339,32 @@ export class Cogitator {
|
|
|
234
339
|
spans.push(llmSpan);
|
|
235
340
|
totalInputTokens += response.usage.inputTokens;
|
|
236
341
|
totalOutputTokens += response.usage.outputTokens;
|
|
342
|
+
let outputContent = response.content;
|
|
343
|
+
if (this.constitutionalAI && this.config.guardrails?.filterOutput) {
|
|
344
|
+
const outputResult = await this.constitutionalAI.filterOutput(outputContent, messages);
|
|
345
|
+
if (!outputResult.allowed) {
|
|
346
|
+
if (outputResult.suggestedRevision) {
|
|
347
|
+
outputContent = outputResult.suggestedRevision;
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
throw new Error(`Output blocked: ${outputResult.blockedReason ?? 'Policy violation'}`);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
237
354
|
const assistantMessage = {
|
|
238
355
|
role: 'assistant',
|
|
239
|
-
content:
|
|
356
|
+
content: outputContent,
|
|
240
357
|
};
|
|
241
358
|
messages.push(assistantMessage);
|
|
242
359
|
if (this.memoryAdapter && options.saveHistory !== false && options.useMemory !== false) {
|
|
243
|
-
await this.saveEntry(threadId, assistantMessage, response.toolCalls);
|
|
360
|
+
await this.saveEntry(threadId, assistantMessage, response.toolCalls, undefined, options.onMemoryError);
|
|
244
361
|
}
|
|
245
362
|
if (response.finishReason === 'tool_calls' && response.toolCalls) {
|
|
246
363
|
for (const toolCall of response.toolCalls) {
|
|
247
364
|
allToolCalls.push(toolCall);
|
|
248
365
|
options.onToolCall?.(toolCall);
|
|
249
366
|
const toolSpanStart = Date.now();
|
|
250
|
-
const result = await this.executeTool(registry, toolCall, runId, agent.id);
|
|
367
|
+
const result = await this.executeTool(registry, toolCall, runId, agent.id, abortController.signal);
|
|
251
368
|
const toolSpanEnd = Date.now();
|
|
252
369
|
const toolSpan = this.createSpan(`tool.${toolCall.name}`, traceId, rootSpanId, toolSpanStart, toolSpanEnd, {
|
|
253
370
|
'tool.name': toolCall.name,
|
|
@@ -265,8 +382,40 @@ export class Cogitator {
|
|
|
265
382
|
name: toolCall.name,
|
|
266
383
|
};
|
|
267
384
|
messages.push(toolMessage);
|
|
268
|
-
if (this.memoryAdapter &&
|
|
269
|
-
|
|
385
|
+
if (this.memoryAdapter &&
|
|
386
|
+
options.saveHistory !== false &&
|
|
387
|
+
options.useMemory !== false) {
|
|
388
|
+
await this.saveEntry(threadId, toolMessage, undefined, [result], options.onMemoryError);
|
|
389
|
+
}
|
|
390
|
+
const action = {
|
|
391
|
+
type: 'tool_call',
|
|
392
|
+
toolName: toolCall.name,
|
|
393
|
+
input: toolCall.arguments,
|
|
394
|
+
output: result.result,
|
|
395
|
+
error: result.error,
|
|
396
|
+
duration: toolSpanEnd - toolSpanStart,
|
|
397
|
+
};
|
|
398
|
+
allActions.push(action);
|
|
399
|
+
if (this.reflectionEngine &&
|
|
400
|
+
this.config.reflection?.enabled &&
|
|
401
|
+
this.config.reflection.reflectAfterToolCall) {
|
|
402
|
+
try {
|
|
403
|
+
const reflectionResult = await this.reflectionEngine.reflectOnToolCall(action, agentContext);
|
|
404
|
+
allReflections.push(reflectionResult.reflection);
|
|
405
|
+
if (reflectionResult.shouldAdjustStrategy && reflectionResult.suggestedAction) {
|
|
406
|
+
messages.push({
|
|
407
|
+
role: 'system',
|
|
408
|
+
content: `Reflection: ${reflectionResult.reflection.analysis.reasoning}. Consider: ${reflectionResult.suggestedAction}`,
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
catch (reflectionError) {
|
|
413
|
+
getLogger().warn('Reflection failed', {
|
|
414
|
+
error: reflectionError instanceof Error
|
|
415
|
+
? reflectionError.message
|
|
416
|
+
: String(reflectionError),
|
|
417
|
+
});
|
|
418
|
+
}
|
|
270
419
|
}
|
|
271
420
|
}
|
|
272
421
|
}
|
|
@@ -276,6 +425,20 @@ export class Cogitator {
|
|
|
276
425
|
}
|
|
277
426
|
const endTime = Date.now();
|
|
278
427
|
const lastAssistantMessage = messages.filter((m) => m.role === 'assistant').pop();
|
|
428
|
+
const finalOutput = lastAssistantMessage?.content ?? '';
|
|
429
|
+
if (this.reflectionEngine &&
|
|
430
|
+
this.config.reflection?.enabled &&
|
|
431
|
+
this.config.reflection.reflectAtEnd) {
|
|
432
|
+
try {
|
|
433
|
+
const runReflection = await this.reflectionEngine.reflectOnRun(agentContext, allActions, finalOutput, true);
|
|
434
|
+
allReflections.push(runReflection.reflection);
|
|
435
|
+
}
|
|
436
|
+
catch (reflectionError) {
|
|
437
|
+
getLogger().warn('End-of-run reflection failed', {
|
|
438
|
+
error: reflectionError instanceof Error ? reflectionError.message : String(reflectionError),
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
}
|
|
279
442
|
const rootSpan = this.createSpan('agent.run', traceId, undefined, startTime, endTime, {
|
|
280
443
|
'agent.id': agent.id,
|
|
281
444
|
'agent.name': agent.name,
|
|
@@ -288,16 +451,29 @@ export class Cogitator {
|
|
|
288
451
|
'run.output_tokens': totalOutputTokens,
|
|
289
452
|
}, 'ok', 'server', options.onSpan);
|
|
290
453
|
spans.unshift(rootSpan);
|
|
454
|
+
const runCost = this.calculateCost(effectiveModel, totalInputTokens, totalOutputTokens);
|
|
455
|
+
if (this.costRouter) {
|
|
456
|
+
this.costRouter.recordCost({
|
|
457
|
+
runId,
|
|
458
|
+
agentId: agent.id,
|
|
459
|
+
threadId,
|
|
460
|
+
model: effectiveModel,
|
|
461
|
+
inputTokens: totalInputTokens,
|
|
462
|
+
outputTokens: totalOutputTokens,
|
|
463
|
+
cost: runCost,
|
|
464
|
+
});
|
|
465
|
+
}
|
|
291
466
|
const result = {
|
|
292
|
-
output:
|
|
467
|
+
output: finalOutput,
|
|
293
468
|
runId,
|
|
294
469
|
agentId: agent.id,
|
|
295
470
|
threadId,
|
|
471
|
+
modelUsed: this.config.costRouting?.enabled ? effectiveModel : undefined,
|
|
296
472
|
usage: {
|
|
297
473
|
inputTokens: totalInputTokens,
|
|
298
474
|
outputTokens: totalOutputTokens,
|
|
299
475
|
totalTokens: totalInputTokens + totalOutputTokens,
|
|
300
|
-
cost:
|
|
476
|
+
cost: runCost,
|
|
301
477
|
duration: endTime - startTime,
|
|
302
478
|
},
|
|
303
479
|
toolCalls: allToolCalls,
|
|
@@ -306,6 +482,10 @@ export class Cogitator {
|
|
|
306
482
|
traceId,
|
|
307
483
|
spans,
|
|
308
484
|
},
|
|
485
|
+
reflections: allReflections.length > 0 ? allReflections : undefined,
|
|
486
|
+
reflectionSummary: this.reflectionEngine
|
|
487
|
+
? await this.reflectionEngine.getSummary(agent.id)
|
|
488
|
+
: undefined,
|
|
309
489
|
};
|
|
310
490
|
options.onRunComplete?.(result);
|
|
311
491
|
return result;
|
|
@@ -317,12 +497,17 @@ export class Cogitator {
|
|
|
317
497
|
'agent.name': agent.name,
|
|
318
498
|
'agent.model': agent.model,
|
|
319
499
|
'run.id': runId,
|
|
320
|
-
|
|
500
|
+
error: error instanceof Error ? error.message : String(error),
|
|
321
501
|
}, 'error', 'server', options.onSpan);
|
|
322
502
|
spans.unshift(errorSpan);
|
|
323
503
|
options.onRunError?.(error instanceof Error ? error : new Error(String(error)), runId);
|
|
324
504
|
throw error;
|
|
325
505
|
}
|
|
506
|
+
finally {
|
|
507
|
+
if (timeoutId) {
|
|
508
|
+
clearTimeout(timeoutId);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
326
511
|
}
|
|
327
512
|
/**
|
|
328
513
|
* Stream chat with token callback
|
|
@@ -379,7 +564,7 @@ export class Cogitator {
|
|
|
379
564
|
/**
|
|
380
565
|
* Execute a tool
|
|
381
566
|
*/
|
|
382
|
-
async executeTool(registry, toolCall, runId, agentId) {
|
|
567
|
+
async executeTool(registry, toolCall, runId, agentId, signal) {
|
|
383
568
|
const tool = registry.get(toolCall.name);
|
|
384
569
|
if (!tool) {
|
|
385
570
|
return {
|
|
@@ -389,16 +574,41 @@ export class Cogitator {
|
|
|
389
574
|
error: `Tool not found: ${toolCall.name}`,
|
|
390
575
|
};
|
|
391
576
|
}
|
|
577
|
+
const parseResult = tool.parameters.safeParse(toolCall.arguments);
|
|
578
|
+
if (!parseResult.success) {
|
|
579
|
+
return {
|
|
580
|
+
callId: toolCall.id,
|
|
581
|
+
name: toolCall.name,
|
|
582
|
+
result: null,
|
|
583
|
+
error: `Invalid arguments: ${parseResult.error.message}`,
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
if (this.constitutionalAI && this.config.guardrails?.filterToolCalls) {
|
|
587
|
+
const context = {
|
|
588
|
+
agentId,
|
|
589
|
+
runId,
|
|
590
|
+
signal: signal ?? new AbortController().signal,
|
|
591
|
+
};
|
|
592
|
+
const guardResult = await this.constitutionalAI.guardTool(tool, toolCall.arguments, context);
|
|
593
|
+
if (!guardResult.approved) {
|
|
594
|
+
return {
|
|
595
|
+
callId: toolCall.id,
|
|
596
|
+
name: toolCall.name,
|
|
597
|
+
result: null,
|
|
598
|
+
error: `Tool blocked: ${guardResult.reason ?? 'Policy violation'}`,
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
}
|
|
392
602
|
if (tool.sandbox?.type === 'docker' || tool.sandbox?.type === 'wasm') {
|
|
393
603
|
return this.executeInSandbox(tool, toolCall, runId, agentId);
|
|
394
604
|
}
|
|
395
605
|
const context = {
|
|
396
606
|
agentId,
|
|
397
607
|
runId,
|
|
398
|
-
signal: new AbortController().signal,
|
|
608
|
+
signal: signal ?? new AbortController().signal,
|
|
399
609
|
};
|
|
400
610
|
try {
|
|
401
|
-
const result = await tool.execute(
|
|
611
|
+
const result = await tool.execute(parseResult.data, context);
|
|
402
612
|
return {
|
|
403
613
|
callId: toolCall.id,
|
|
404
614
|
name: toolCall.name,
|
|
@@ -517,6 +727,46 @@ export class Cogitator {
|
|
|
517
727
|
}
|
|
518
728
|
return (inputTokens * price.input + outputTokens * price.output) / 1_000_000;
|
|
519
729
|
}
|
|
730
|
+
/**
|
|
731
|
+
* Get accumulated insights for an agent
|
|
732
|
+
*/
|
|
733
|
+
async getInsights(agentId) {
|
|
734
|
+
if (!this.insightStore)
|
|
735
|
+
return [];
|
|
736
|
+
return this.insightStore.getAll(agentId);
|
|
737
|
+
}
|
|
738
|
+
/**
|
|
739
|
+
* Get reflection summary for an agent
|
|
740
|
+
*/
|
|
741
|
+
async getReflectionSummary(agentId) {
|
|
742
|
+
if (!this.reflectionEngine)
|
|
743
|
+
return null;
|
|
744
|
+
return this.reflectionEngine.getSummary(agentId);
|
|
745
|
+
}
|
|
746
|
+
/**
|
|
747
|
+
* Get the constitutional AI guardrails instance
|
|
748
|
+
*/
|
|
749
|
+
getGuardrails() {
|
|
750
|
+
return this.constitutionalAI;
|
|
751
|
+
}
|
|
752
|
+
/**
|
|
753
|
+
* Set the constitution for guardrails
|
|
754
|
+
*/
|
|
755
|
+
setConstitution(constitution) {
|
|
756
|
+
this.constitutionalAI?.setConstitution(constitution);
|
|
757
|
+
}
|
|
758
|
+
/**
|
|
759
|
+
* Get cost-aware routing summary
|
|
760
|
+
*/
|
|
761
|
+
getCostSummary() {
|
|
762
|
+
return this.costRouter?.getCostSummary();
|
|
763
|
+
}
|
|
764
|
+
/**
|
|
765
|
+
* Get the cost-aware router instance
|
|
766
|
+
*/
|
|
767
|
+
getCostRouter() {
|
|
768
|
+
return this.costRouter;
|
|
769
|
+
}
|
|
520
770
|
/**
|
|
521
771
|
* Close all connections
|
|
522
772
|
*/
|
|
@@ -532,6 +782,13 @@ export class Cogitator {
|
|
|
532
782
|
this.sandboxManager = undefined;
|
|
533
783
|
this.sandboxInitialized = false;
|
|
534
784
|
}
|
|
785
|
+
this.reflectionEngine = undefined;
|
|
786
|
+
this.insightStore = undefined;
|
|
787
|
+
this.reflectionInitialized = false;
|
|
788
|
+
this.constitutionalAI = undefined;
|
|
789
|
+
this.guardrailsInitialized = false;
|
|
790
|
+
this.costRouter = undefined;
|
|
791
|
+
this.costRoutingInitialized = false;
|
|
535
792
|
this.backends.clear();
|
|
536
793
|
}
|
|
537
794
|
}
|