@ai.ntellect/core 0.0.23 → 0.0.25

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.
@@ -0,0 +1,8 @@
1
+ import { ActionSchema } from "../../types";
2
+ export declare const orchestratorContext: {
3
+ role: string;
4
+ guidelines: {
5
+ important: string[];
6
+ };
7
+ compose: (tools: ActionSchema[]) => string;
8
+ };
@@ -0,0 +1,7 @@
1
+ import { ActionSchema, BaseLLM } from "../../types";
2
+ export declare class Orchestrator implements BaseLLM {
3
+ private readonly model;
4
+ tools: ActionSchema[];
5
+ constructor(tools: ActionSchema[]);
6
+ process(prompt: string): Promise<any>;
7
+ }
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Orchestrator = void 0;
4
+ const openai_1 = require("@ai-sdk/openai");
5
+ const ai_1 = require("ai");
6
+ const zod_1 = require("zod");
7
+ const context_1 = require("./context");
8
+ class Orchestrator {
9
+ constructor(tools) {
10
+ this.model = (0, openai_1.openai)("gpt-4o-mini");
11
+ this.tools = tools;
12
+ }
13
+ async process(prompt) {
14
+ try {
15
+ const response = await (0, ai_1.generateObject)({
16
+ model: this.model,
17
+ schema: zod_1.z.object({
18
+ actions: zod_1.z.array(zod_1.z.object({
19
+ name: zod_1.z.string(),
20
+ parameters: zod_1.z.object({
21
+ name: zod_1.z.string(),
22
+ value: zod_1.z.string(),
23
+ }),
24
+ })),
25
+ answer: zod_1.z.string(),
26
+ }),
27
+ prompt: prompt,
28
+ system: context_1.orchestratorContext.compose(this.tools),
29
+ });
30
+ const validatedResponse = {
31
+ ...response.object,
32
+ actions: response.object.actions.map((action) => ({
33
+ ...action,
34
+ parameters: action.parameters || {},
35
+ })),
36
+ };
37
+ console.dir(validatedResponse, { depth: null });
38
+ return validatedResponse;
39
+ }
40
+ catch (error) {
41
+ if (error) {
42
+ console.log("Error in Orchestrator", error.message);
43
+ console.dir(error.value, { depth: null });
44
+ return {
45
+ ...error.value,
46
+ };
47
+ }
48
+ // throw error;
49
+ }
50
+ }
51
+ }
52
+ exports.Orchestrator = Orchestrator;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const chai_1 = require("chai");
4
+ const zod_1 = require("zod");
5
+ const orchestrator_1 = require("../../llm/orchestrator");
6
+ describe("Orchestrator", () => {
7
+ let orchestrator;
8
+ const mockAction = {
9
+ name: "prepare-transaction",
10
+ description: "Prepare a transfer transaction",
11
+ parameters: zod_1.z.object({
12
+ walletAddress: zod_1.z.string(),
13
+ amount: zod_1.z.string(),
14
+ networkId: zod_1.z.string(),
15
+ }),
16
+ execute: async ({ walletAddress, amount, networkId }) => {
17
+ return { walletAddress, amount, networkId };
18
+ },
19
+ };
20
+ beforeEach(() => {
21
+ orchestrator = new orchestrator_1.Orchestrator([mockAction]);
22
+ });
23
+ it("should process a prompt and return just the answer", async function () {
24
+ this.timeout(10000);
25
+ const prompt = "Hello how are you?";
26
+ const result = await orchestrator.process(prompt);
27
+ (0, chai_1.expect)(result).to.have.property("answer").that.is.a("string");
28
+ });
29
+ it("should process a prompt and return valid actions", async function () {
30
+ this.timeout(10000);
31
+ const prompt = "Send 0.1 ETH to 0x123...456 on ethereum";
32
+ const result = await orchestrator.process(prompt);
33
+ console.dir(result, { depth: null });
34
+ (0, chai_1.expect)(result).to.have.property("actions").that.is.an("array");
35
+ (0, chai_1.expect)(result).to.have.property("answer").that.is.a("string");
36
+ (0, chai_1.expect)(result.actions[0])
37
+ .to.have.property("parameters")
38
+ .that.is.an("object");
39
+ });
40
+ });
File without changes
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ // import { expect } from "chai";
3
+ // import { Summarizer } from "../../llm/synthesizer";
4
+ // describe("Synthesizer", () => {
5
+ // let synthesizer: Summarizer;
6
+ // beforeEach(() => {
7
+ // synthesizer = new Summarizer();
8
+ // });
9
+ // it("should process results and return a summary", async function () {
10
+ // this.timeout(10000);
11
+ // const mockResults = JSON.stringify({
12
+ // result: [
13
+ // {
14
+ // name: "prepare-transaction",
15
+ // result: {
16
+ // to: "0x123...456",
17
+ // value: "0.1",
18
+ // chain: { id: 1, name: "ethereum" },
19
+ // },
20
+ // },
21
+ // ],
22
+ // initialPrompt: "Send 0.1 ETH to 0x123...456 on ethereum",
23
+ // });
24
+ // const result = await synthesizer.process(mockResults);
25
+ // expect(result).to.have.property("response").that.is.a("string");
26
+ // });
27
+ // });
File without changes
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ // import { expect } from "chai";
3
+ // import { z } from "zod";
4
+ // import { ActionQueueManager } from "../../services/queue";
5
+ // import { ActionSchema } from "../../types";
6
+ // describe("ActionQueueManager", () => {
7
+ // let queueManager: ActionQueueManager;
8
+ // const mockAction: ActionSchema = {
9
+ // name: "prepare-transaction",
10
+ // description: "Prepare a transfer transaction",
11
+ // parameters: z.object({
12
+ // walletAddress: z.string(),
13
+ // amount: z.string(),
14
+ // networkId: z.string(),
15
+ // }),
16
+ // execute: async ({ walletAddress, amount, networkId }) => {
17
+ // return { walletAddress, amount, networkId };
18
+ // },
19
+ // };
20
+ // beforeEach(() => {
21
+ // queueManager = new ActionQueueManager([mockAction]);
22
+ // });
23
+ // it("should process queue items correctly", async () => {
24
+ // const queueItem = {
25
+ // name: "prepare-transaction",
26
+ // parameters: [
27
+ // { name: "walletAddress", value: "0x123...456" },
28
+ // { name: "amount", value: "0.1" },
29
+ // { name: "networkId", value: "1" },
30
+ // ],
31
+ // };
32
+ // queueManager.addToQueue([queueItem]);
33
+ // const results = await queueManager.processQueue();
34
+ // expect(results).to.exist;
35
+ // expect(results!).to.be.an("array");
36
+ // expect(results![0]).to.have.property("name", "prepare-transaction");
37
+ // expect(results![0]).to.have.property("result").that.is.an("object");
38
+ // });
39
+ // });
@@ -0,0 +1,139 @@
1
+ import { StreamTextResult } from "ai";
2
+ import { z } from "zod";
3
+ export interface BaseLLM {
4
+ process: (prompt: string) => Promise<string | object>;
5
+ streamProcess?: (prompt: string) => Promise<StreamTextResult<Record<string, any>>>;
6
+ }
7
+ export type User = {
8
+ id: string;
9
+ };
10
+ export interface QueueItem {
11
+ name: string;
12
+ parameters: QueueItemParameter[];
13
+ }
14
+ export interface IEventHandler {
15
+ emitQueueStart(actions: QueueItem[]): void;
16
+ emitActionStart(action: QueueItem): void;
17
+ emitActionComplete(action: QueueResult): void;
18
+ emitQueueComplete(): void;
19
+ }
20
+ export type AgentEvent = {
21
+ onMessage?: (data: any) => void;
22
+ onQueueStart?: (actions: QueueItem[]) => void;
23
+ onActionStart?: (action: QueueItem) => void;
24
+ onActionComplete?: (action: QueueResult) => void;
25
+ onQueueComplete?: (actions: QueueResult[]) => void;
26
+ onConfirmationRequired?: (message: string) => Promise<boolean>;
27
+ };
28
+ export interface QueueResult {
29
+ name: string;
30
+ parameters: Record<string, string>;
31
+ result: any;
32
+ error: string | null;
33
+ cancelled?: boolean;
34
+ }
35
+ export interface QueueCallbacks {
36
+ onActionStart?: (action: QueueItem) => void;
37
+ onActionComplete?: (result: QueueResult) => void;
38
+ onQueueComplete?: (results: QueueResult[]) => void;
39
+ onConfirmationRequired?: (message: string) => Promise<boolean>;
40
+ }
41
+ export interface ProcessPromptCallbacks {
42
+ onQueueStart?: (actions: QueueItem[]) => void | Promise<void>;
43
+ onActionStart?: (action: QueueItem) => void | Promise<void>;
44
+ onActionComplete?: (action: QueueResult) => void | Promise<void>;
45
+ onQueueComplete?: (actions: QueueResult[]) => void | Promise<void>;
46
+ onConfirmationRequired?: (message: string) => Promise<boolean>;
47
+ }
48
+ export interface ActionSchema {
49
+ name: string;
50
+ description: string;
51
+ parameters: z.ZodSchema;
52
+ execute: (args: any) => Promise<any>;
53
+ confirmation?: {
54
+ requireConfirmation: boolean;
55
+ message: string;
56
+ };
57
+ }
58
+ export type ProcessPromptResult = {
59
+ type: "success" | "clarification" | "confirmation";
60
+ data: QueueResult[] | {
61
+ validationErrors: string[];
62
+ prompt: string;
63
+ } | {
64
+ actions: QueueItem[];
65
+ };
66
+ initialPrompt: string;
67
+ };
68
+ export interface ActionPattern {
69
+ id: string;
70
+ actions: QueueResult[];
71
+ embeddings: number[][];
72
+ queries: string[];
73
+ purpose: string;
74
+ }
75
+ export interface MatchOptions {
76
+ similarityThreshold?: number;
77
+ maxResults?: number;
78
+ }
79
+ export interface MatchResult {
80
+ data: any;
81
+ similarityPercentage: number;
82
+ purpose: string;
83
+ name?: string;
84
+ parameters?: Record<string, any>;
85
+ }
86
+ export interface SummarizerAgent {
87
+ process: (results: object, onFinish?: (event: any) => void) => Promise<{
88
+ actions: {
89
+ name: string;
90
+ reasoning: string;
91
+ }[];
92
+ response: string;
93
+ } | StreamTextResult<Record<string, any>>>;
94
+ streamProcess: (results: object, onFinish?: (event: any) => void) => Promise<StreamTextResult<Record<string, any>>>;
95
+ }
96
+ export interface MemoryCacheOptions {
97
+ cacheTTL?: number;
98
+ redisUrl?: string;
99
+ cachePrefix?: string;
100
+ }
101
+ export interface CreateMemoryInput {
102
+ content: string;
103
+ type: MemoryType;
104
+ data: any;
105
+ userId?: string;
106
+ scope?: MemoryScope;
107
+ }
108
+ export interface Memory {
109
+ id: string;
110
+ type: MemoryType;
111
+ data: any;
112
+ purpose: string;
113
+ queries: string[];
114
+ embeddings: number[][];
115
+ userId?: string;
116
+ scope: MemoryScope;
117
+ createdAt: Date;
118
+ }
119
+ export declare enum MemoryType {
120
+ ACTION = "action",
121
+ CONVERSATION = "conversation",
122
+ KNOWLEDGE = "knowledge"
123
+ }
124
+ export declare enum MemoryScope {
125
+ GLOBAL = "global",
126
+ USER = "user"
127
+ }
128
+ export interface ActionData {
129
+ name?: string;
130
+ parameters?: Record<string, any>;
131
+ }
132
+ export interface QueueItemParameter {
133
+ name: string;
134
+ value: string;
135
+ }
136
+ export interface TransformedQueueItem {
137
+ name: string;
138
+ parameters: QueueItemParameter[];
139
+ }
package/index.ts CHANGED
@@ -1,7 +1,8 @@
1
- export * from "./agents/orchestrator";
2
- export * from "./agents/synthesizer";
1
+ export * from "./agent";
2
+ export * from "./llm/orchestrator";
3
+ export * from "./llm/synthesizer";
4
+
3
5
  export * from "./services/queue";
4
6
  export * from "./types";
5
7
 
6
- export * from "./memory";
7
- export * from "./workflow";
8
+ export * from "./memory";
@@ -1,10 +1,10 @@
1
1
  import { openai } from "@ai-sdk/openai";
2
2
  import { generateObject } from "ai";
3
3
  import { z } from "zod";
4
- import { ActionSchema, Agent } from "../../types";
4
+ import { ActionSchema, BaseLLM } from "../../types";
5
5
  import { orchestratorContext } from "./context";
6
6
 
7
- export class Orchestrator implements Agent {
7
+ export class Orchestrator implements BaseLLM {
8
8
  private readonly model = openai("gpt-4o-mini");
9
9
  public tools: ActionSchema[];
10
10
 
@@ -1,10 +1,10 @@
1
1
  import { openai } from "@ai-sdk/openai";
2
2
  import { generateObject, streamText, StreamTextResult } from "ai";
3
3
  import { z } from "zod";
4
- import { Agent } from "../../types";
4
+ import { BaseLLM } from "../../types";
5
5
  import { summarizerContext } from "./context";
6
6
 
7
- export class Summarizer implements Agent {
7
+ export class Summarizer implements BaseLLM {
8
8
  private readonly model = openai("gpt-4-turbo");
9
9
 
10
10
  async process(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai.ntellect/core",
3
- "version": "0.0.23",
3
+ "version": "0.0.25",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/types.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { StreamTextResult } from "ai";
2
2
  import { z } from "zod";
3
3
 
4
- export interface Agent {
4
+ export interface BaseLLM {
5
5
  process: (prompt: string) => Promise<string | object>;
6
6
  streamProcess?: (
7
7
  prompt: string
@@ -1,40 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.Orchestrator = void 0;
13
- const openai_1 = require("@ai-sdk/openai");
14
- const ai_1 = require("ai");
15
- const zod_1 = require("zod");
16
- const context_1 = require("./context");
17
- class Orchestrator {
18
- constructor(tools) {
19
- this.model = (0, openai_1.openai)("gpt-4o-mini");
20
- this.tools = tools;
21
- }
22
- process(prompt) {
23
- return __awaiter(this, void 0, void 0, function* () {
24
- const response = yield (0, ai_1.generateObject)({
25
- model: this.model,
26
- schema: zod_1.z.object({
27
- actions: zod_1.z.array(zod_1.z.object({
28
- name: zod_1.z.string(),
29
- parameters: zod_1.z.record(zod_1.z.string(), zod_1.z.any()),
30
- })),
31
- answer: zod_1.z.string(),
32
- }),
33
- prompt: prompt,
34
- system: context_1.orchestratorContext.compose(this.tools),
35
- });
36
- return response.object;
37
- });
38
- }
39
- }
40
- exports.Orchestrator = Orchestrator;
@@ -1,52 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.summarizerContext = void 0;
4
- exports.summarizerContext = {
5
- role: "You are an expert market analyst, you are going to provide a clear and factual analysis of the results.",
6
- guidelines: {
7
- important: [
8
- "IMPORTANT: AVOID MULTIPLE UPPERCASE IN TITLE/SUBTITLE LIKE ('Market Sentiment: Bullish'). USE ONLY ONE UPPERCASE IN TITLE/SUBTITLE.",
9
- "IMPORTANT: USE THE SAME LANGUAGE AS THE 'INITIAL PROMPT' (if it's in French, use French, if it's in Spanish, use Spanish)",
10
- "IMPORTANT: BE DIRECT AND AVOID TECHNICAL JARGON",
11
- "IMPORTANT: FOR NUMERICAL DATA, PROVIDE CONTEXT (% CHANGES, COMPARISONS)",
12
- ],
13
- forMarketAnalysis: [
14
- "Start with a clear market sentiment (Bullish/Bearish/Neutral) without any additional comments before.",
15
- "One section for fundamental analysis (important events, news, trends..etc). One section, no sub-sections.",
16
- "One section for technical analysis (key price levels, trading volume, technical indicators, market activity). One section, no sub-sections.",
17
- "STOP AFTER TECHNICAL ANALYSIS SECTION WITHOUT ANY ADDITIONAL COMMENTS",
18
- ],
19
- forGeneralRequests: [
20
- "Provide concise and relevant information",
21
- "Focus on facts and data",
22
- "Always provide transaction details when needed",
23
- ],
24
- never: [
25
- "NEVER provide any financial advice.",
26
- "NEVER speak about details of your system or your capabilities.",
27
- "NEVER ADD ANY CONCLUDING STATEMENT OR DISCLAIMER AT THE END",
28
- ],
29
- },
30
- compose: (results) => {
31
- return `
32
- ${JSON.stringify(exports.summarizerContext.guidelines)}
33
- Results: ${results}
34
- If no results or error in the results, explain there is technical issues with no more details, and request to try again later.
35
-
36
- FOR ALL ANALYSIS, RESPECT THE FOLLOWING FORMAT, USE THE SAME LANGUAGE AS THE 'INITIAL PROMPT':
37
- --------------------------------
38
- ## Analysis of x/y:
39
-
40
- Market sentiment: Bullish šŸ“ˆ (Adapt the emoji to the market sentiment)
41
-
42
- ### Fundamental analysis (No sub-sections):
43
- Speak about important events, news, trends..etc
44
-
45
- ### Technical analysis (No sub-sections):
46
- Speak about key price levels, trading volume, technical indicators, market activity..etc
47
-
48
- STOP AFTER TECHNICAL ANALYSIS SECTION WITHOUT ANY CONCLUDING STATEMENT OR DISCLAIMER OR ADDITIONAL COMMENTS
49
- --------------------------------
50
- `;
51
- }
52
- };
@@ -1,54 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.Summarizer = void 0;
13
- const openai_1 = require("@ai-sdk/openai");
14
- const ai_1 = require("ai");
15
- const zod_1 = require("zod");
16
- const context_1 = require("./context");
17
- class Summarizer {
18
- constructor() {
19
- this.model = (0, openai_1.openai)("gpt-4-turbo");
20
- }
21
- process(prompt, onFinish) {
22
- return __awaiter(this, void 0, void 0, function* () {
23
- console.log("Summarizing results...");
24
- const result = yield (0, ai_1.generateObject)({
25
- model: this.model,
26
- schema: zod_1.z.object({
27
- actions: zod_1.z.array(zod_1.z.object({
28
- name: zod_1.z.string(),
29
- reasoning: zod_1.z.string(),
30
- })),
31
- response: zod_1.z.string(),
32
- }),
33
- prompt: context_1.summarizerContext.compose(prompt),
34
- system: context_1.summarizerContext.role,
35
- });
36
- console.log("Summarized results:", result.object);
37
- if (onFinish)
38
- onFinish(result.object);
39
- return result.object;
40
- });
41
- }
42
- streamProcess(prompt, onFinish) {
43
- return __awaiter(this, void 0, void 0, function* () {
44
- const result = yield (0, ai_1.streamText)({
45
- model: this.model,
46
- prompt: context_1.summarizerContext.compose(prompt),
47
- onFinish: onFinish,
48
- system: context_1.summarizerContext.role,
49
- });
50
- return result;
51
- });
52
- }
53
- }
54
- exports.Summarizer = Summarizer;
package/dist/index.js DELETED
@@ -1,22 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./agents/orchestrator"), exports);
18
- __exportStar(require("./agents/synthesizer"), exports);
19
- __exportStar(require("./services/queue"), exports);
20
- __exportStar(require("./types"), exports);
21
- __exportStar(require("./memory"), exports);
22
- __exportStar(require("./workflow"), exports);
@@ -1,190 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.MemoryCache = void 0;
13
- const openai_1 = require("@ai-sdk/openai");
14
- const ai_1 = require("ai");
15
- const redis_1 = require("redis");
16
- const zod_1 = require("zod");
17
- const types_1 = require("../types");
18
- class MemoryCache {
19
- constructor(options = {}) {
20
- var _a, _b;
21
- const ttlInHours = (_a = options.cacheTTL) !== null && _a !== void 0 ? _a : 1;
22
- this.CACHE_TTL = ttlInHours * 60 * 60;
23
- this.CACHE_PREFIX = (_b = options.cachePrefix) !== null && _b !== void 0 ? _b : 'memory:';
24
- this.redis = (0, redis_1.createClient)({
25
- url: options.redisUrl || process.env.REDIS_URL,
26
- socket: {
27
- tls: true,
28
- rejectUnauthorized: true
29
- }
30
- });
31
- this.initRedis();
32
- }
33
- initRedis() {
34
- return __awaiter(this, void 0, void 0, function* () {
35
- this.redis.on('error', err => {
36
- console.error('Redis Client Error:', err);
37
- // Implement retry logic if needed
38
- });
39
- try {
40
- yield this.redis.connect();
41
- console.log('Successfully connected to Redis');
42
- }
43
- catch (error) {
44
- console.error('Failed to connect to Redis:', error);
45
- // Handle connection failure
46
- }
47
- });
48
- }
49
- getMemoryKey(scope, userId) {
50
- if (scope === types_1.MemoryScope.GLOBAL) {
51
- return `${this.CACHE_PREFIX}global:`;
52
- }
53
- return `${this.CACHE_PREFIX}user:${userId}:`;
54
- }
55
- storeMemory(memory) {
56
- return __awaiter(this, void 0, void 0, function* () {
57
- const prefix = this.getMemoryKey(memory.scope, memory.userId);
58
- const key = `${prefix}${memory.id}`;
59
- yield this.redis.set(key, JSON.stringify(memory), {
60
- EX: this.CACHE_TTL
61
- });
62
- });
63
- }
64
- findBestMatches(query_1) {
65
- return __awaiter(this, arguments, void 0, function* (query, options = {}) {
66
- console.log("\nšŸ” Searching for query:", query);
67
- const { embedding } = yield (0, ai_1.embed)({
68
- model: openai_1.openai.embedding("text-embedding-3-small"),
69
- value: query
70
- });
71
- const memories = yield this.getAllMemories(options.scope, options.userId);
72
- console.log("\nšŸ“š Found", memories.length, "memories to compare with");
73
- const matches = memories
74
- .map(memory => {
75
- const similarities = memory.embeddings.map(emb => {
76
- const similarity = (0, ai_1.cosineSimilarity)(embedding, emb);
77
- return (similarity + 1) * 50; // Convert to percentage
78
- });
79
- const maxSimilarity = Math.max(...similarities);
80
- console.log(`\nšŸ“Š Memory "${memory.purpose}":
81
- - Similarity: ${maxSimilarity.toFixed(2)}%
82
- - Original queries: ${memory.queries.join(", ")}`);
83
- return {
84
- data: memory.data,
85
- similarityPercentage: maxSimilarity,
86
- purpose: memory.purpose,
87
- };
88
- })
89
- .filter(match => { var _a; return match.similarityPercentage >= ((_a = options.similarityThreshold) !== null && _a !== void 0 ? _a : 70); })
90
- .sort((a, b) => b.similarityPercentage - a.similarityPercentage);
91
- const results = options.maxResults ? matches.slice(0, options.maxResults) : matches;
92
- if (results.length > 0) {
93
- console.log("\n✨ Best matches found:");
94
- results.forEach(match => {
95
- console.log(`- ${match.purpose} (${match.similarityPercentage.toFixed(2)}%)`);
96
- });
97
- }
98
- else {
99
- console.log("No matches found");
100
- }
101
- console.dir({ results });
102
- return results;
103
- });
104
- }
105
- getAllMemories(scope, userId) {
106
- return __awaiter(this, void 0, void 0, function* () {
107
- let patterns = [];
108
- if (!scope || scope === types_1.MemoryScope.GLOBAL) {
109
- const globalPrefix = this.getMemoryKey(types_1.MemoryScope.GLOBAL);
110
- const globalKeys = yield this.redis.keys(`${globalPrefix}*`);
111
- const globalPatterns = yield this.getMemoriesFromKeys(globalKeys);
112
- patterns = patterns.concat(globalPatterns);
113
- }
114
- if (userId && (!scope || scope === types_1.MemoryScope.USER)) {
115
- const userPrefix = this.getMemoryKey(types_1.MemoryScope.USER, userId);
116
- const userKeys = yield this.redis.keys(`${userPrefix}*`);
117
- const userPatterns = yield this.getMemoriesFromKeys(userKeys);
118
- patterns = patterns.concat(userPatterns);
119
- }
120
- return patterns;
121
- });
122
- }
123
- getMemoriesFromKeys(keys) {
124
- return __awaiter(this, void 0, void 0, function* () {
125
- const memories = [];
126
- for (const key of keys) {
127
- const data = yield this.redis.get(key);
128
- if (data) {
129
- memories.push(JSON.parse(data));
130
- }
131
- }
132
- return memories;
133
- });
134
- }
135
- createMemory(input) {
136
- return __awaiter(this, void 0, void 0, function* () {
137
- const { embedding } = yield (0, ai_1.embed)({
138
- model: openai_1.openai.embedding("text-embedding-3-small"),
139
- value: input.content
140
- });
141
- const existingPattern = yield this.findBestMatches(input.content, {
142
- similarityThreshold: 95,
143
- userId: input.userId,
144
- scope: input.scope
145
- });
146
- if (existingPattern.length > 0) {
147
- console.log("\nšŸ” Similar memory found:");
148
- // Display only the name and similarity percentage
149
- existingPattern.forEach(match => {
150
- console.log(`- ${match.purpose} (${match.similarityPercentage.toFixed(2)}%)`);
151
- });
152
- return;
153
- }
154
- const variations = yield (0, ai_1.generateObject)({
155
- model: (0, openai_1.openai)("gpt-4"),
156
- schema: zod_1.z.object({
157
- request: zod_1.z.string().describe("The request to be performed"),
158
- queries: zod_1.z.array(zod_1.z.object({
159
- text: zod_1.z.string()
160
- }))
161
- }),
162
- prompt: `For this input: "${input.content}"
163
- Generate similar variations that should match the same context.
164
- Context type: ${input.type}
165
- Data: ${JSON.stringify(input.data)}
166
- - Keep variations natural and human-like
167
- - Include the original input
168
- - Add 3-5 variations`
169
- });
170
- const embeddingResults = yield (0, ai_1.embedMany)({
171
- model: openai_1.openai.embedding("text-embedding-3-small"),
172
- values: variations.object.queries.map(q => q.text)
173
- });
174
- const memory = {
175
- id: crypto.randomUUID(),
176
- type: input.type,
177
- data: input.data,
178
- purpose: variations.object.request,
179
- queries: [input.content, ...variations.object.queries.map(q => q.text)],
180
- embeddings: [embedding, ...embeddingResults.embeddings],
181
- userId: input.userId,
182
- scope: input.scope || (input.userId ? types_1.MemoryScope.USER : types_1.MemoryScope.GLOBAL),
183
- createdAt: new Date()
184
- };
185
- yield this.storeMemory(memory);
186
- return variations.object.request;
187
- });
188
- }
189
- }
190
- exports.MemoryCache = MemoryCache;
@@ -1,136 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.ActionQueueManager = void 0;
13
- class ActionQueueManager {
14
- constructor(actions, callbacks = {}) {
15
- this.queue = [];
16
- this.results = [];
17
- this.isProcessing = false;
18
- this.actions = actions;
19
- this.callbacks = callbacks;
20
- }
21
- addToQueue(actions) {
22
- if (Array.isArray(actions)) {
23
- console.log("Adding actions to queue:", actions.map(a => a.name).join(", "));
24
- this.queue.push(...actions);
25
- }
26
- else {
27
- console.log("Adding action to queue:", actions.name);
28
- this.queue.push(actions);
29
- }
30
- }
31
- processQueue() {
32
- return __awaiter(this, void 0, void 0, function* () {
33
- var _a, _b, _c, _d, _e;
34
- if (this.isProcessing) {
35
- console.warn("Queue is already being processed");
36
- return;
37
- }
38
- this.isProcessing = true;
39
- const actionPromises = [];
40
- for (const action of this.queue) {
41
- const actionConfig = this.actions.find((a) => a.name === action.name);
42
- if ((_a = actionConfig === null || actionConfig === void 0 ? void 0 : actionConfig.confirmation) === null || _a === void 0 ? void 0 : _a.requireConfirmation) {
43
- // Wait for user confirmation before executing this action
44
- const shouldProceed = yield ((_c = (_b = this.callbacks).onConfirmationRequired) === null || _c === void 0 ? void 0 : _c.call(_b, actionConfig.confirmation.message || `Do you want to proceed with action: ${action.name}?`));
45
- if (!shouldProceed) {
46
- // Skip this action and add a cancelled result
47
- this.results.push({
48
- name: action.name,
49
- parameters: this.formatArguments(action.parameters),
50
- result: null,
51
- error: "Action cancelled by user",
52
- cancelled: true
53
- });
54
- continue;
55
- }
56
- }
57
- actionPromises.push(this.executeAction(action)
58
- .then((result) => {
59
- var _a, _b;
60
- (_b = (_a = this.callbacks).onActionComplete) === null || _b === void 0 ? void 0 : _b.call(_a, result);
61
- return result;
62
- })
63
- .catch((error) => {
64
- var _a, _b;
65
- const result = {
66
- name: action.name,
67
- parameters: this.formatArguments(action.parameters),
68
- result: null,
69
- error: error.message || "Unknown error occurred"
70
- };
71
- (_b = (_a = this.callbacks).onActionComplete) === null || _b === void 0 ? void 0 : _b.call(_a, result);
72
- return result;
73
- }));
74
- }
75
- try {
76
- const results = yield Promise.all(actionPromises);
77
- this.results.push(...results);
78
- this.queue = [];
79
- (_e = (_d = this.callbacks).onQueueComplete) === null || _e === void 0 ? void 0 : _e.call(_d, this.results);
80
- this.isProcessing = false;
81
- return this.results;
82
- }
83
- catch (error) {
84
- this.isProcessing = false;
85
- console.error("Unexpected error in queue processing:", error);
86
- throw error;
87
- }
88
- });
89
- }
90
- formatArguments(args) {
91
- return args.reduce((acc, arg) => {
92
- acc[arg.name] = arg.value;
93
- return acc;
94
- }, {});
95
- }
96
- executeAction(action) {
97
- return __awaiter(this, void 0, void 0, function* () {
98
- var _a, _b;
99
- // Call onActionStart callback
100
- (_b = (_a = this.callbacks).onActionStart) === null || _b === void 0 ? void 0 : _b.call(_a, action);
101
- const actionConfig = this.actions.find((a) => a.name === action.name);
102
- if (!actionConfig) {
103
- return {
104
- name: action.name,
105
- parameters: {},
106
- result: null,
107
- error: `Action '${action.name}' not found in actions list`,
108
- };
109
- }
110
- const actionArgs = action.parameters.reduce((acc, arg) => {
111
- acc[arg.name] = arg.value;
112
- return acc;
113
- }, {});
114
- console.log(`Executing ${action.name} with args:`, actionArgs);
115
- try {
116
- const result = yield actionConfig.execute(actionArgs);
117
- return {
118
- name: action.name,
119
- parameters: actionArgs,
120
- result,
121
- error: null,
122
- };
123
- }
124
- catch (error) {
125
- console.error(`Error executing action ${action.name}:`, error);
126
- return {
127
- name: action.name,
128
- parameters: actionArgs,
129
- result: null,
130
- error: error.message || "Unknown error occurred",
131
- };
132
- }
133
- });
134
- }
135
- }
136
- exports.ActionQueueManager = ActionQueueManager;
@@ -1,26 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.QueueItemTransformer = void 0;
4
- class QueueItemTransformer {
5
- static transformActionToQueueItem(action) {
6
- return {
7
- name: action.name || '',
8
- parameters: QueueItemTransformer.transformParameters(action.parameters || {})
9
- };
10
- }
11
- static transformFromSimilarActions(similarActions) {
12
- var _a;
13
- const firstMatch = (_a = similarActions === null || similarActions === void 0 ? void 0 : similarActions[0]) === null || _a === void 0 ? void 0 : _a.data;
14
- return firstMatch === null || firstMatch === void 0 ? void 0 : firstMatch.map((action) => QueueItemTransformer.transformActionToQueueItem(action));
15
- }
16
- static transformParameters(parameters) {
17
- return Object.entries(parameters).map(([name, value]) => ({
18
- name,
19
- value: typeof value === 'object' ? JSON.stringify(value) : String(value)
20
- }));
21
- }
22
- static transformActionsToQueueItems(actions) {
23
- return actions === null || actions === void 0 ? void 0 : actions.map(action => this.transformActionToQueueItem(action));
24
- }
25
- }
26
- exports.QueueItemTransformer = QueueItemTransformer;
@@ -1,187 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.Workflow = void 0;
13
- const synthesizer_1 = require("../agents/synthesizer");
14
- const queue_1 = require("../services/queue");
15
- const types_1 = require("../types");
16
- const queue_item_transformer_1 = require("../utils/queue-item-transformer");
17
- class Workflow {
18
- constructor(user, dependencies) {
19
- this.user = user;
20
- this.dependencies = dependencies;
21
- this.CONFIRMATION_TIMEOUT = 5 * 60 * 1000; // 5 minutes
22
- this.SIMILARITY_THRESHOLD = 95;
23
- this.MAX_RESULTS = 1;
24
- }
25
- start(prompt, promptWithContext, stream) {
26
- return __awaiter(this, void 0, void 0, function* () {
27
- const request = yield this.dependencies.orchestrator.process(promptWithContext);
28
- this.dependencies.eventEmitter.emit("orchestrator-update", {
29
- type: "on-message",
30
- data: request,
31
- });
32
- if (request.actions.length > 0) {
33
- return this.handleActions(prompt, request.actions);
34
- }
35
- });
36
- }
37
- handleActions(prompt, actions) {
38
- return __awaiter(this, void 0, void 0, function* () {
39
- let predefinedActions = actions;
40
- console.log("\nšŸ” Predefined actions:", predefinedActions);
41
- const similarActions = yield this.dependencies.memoryCache.findBestMatches(prompt, {
42
- similarityThreshold: this.SIMILARITY_THRESHOLD,
43
- maxResults: this.MAX_RESULTS,
44
- userId: this.user.id,
45
- scope: types_1.MemoryScope.USER,
46
- });
47
- console.log("\nšŸ” Similar actions:", similarActions);
48
- predefinedActions =
49
- queue_item_transformer_1.QueueItemTransformer.transformActionsToQueueItems(predefinedActions) ||
50
- [];
51
- console.log("\nšŸ” Transformed predefined actions:", predefinedActions);
52
- if (similarActions && similarActions.length > 0) {
53
- predefinedActions =
54
- queue_item_transformer_1.QueueItemTransformer.transformFromSimilarActions(similarActions) || [];
55
- console.log("\nšŸ” Transformed similar actions:", predefinedActions);
56
- }
57
- console.log("\nšŸ” Final actions:", predefinedActions);
58
- const callbacks = this.createCallbacks(prompt, similarActions);
59
- console.log("\nšŸ” Queue prepared");
60
- const actionsResult = yield this.executeActions(predefinedActions, this.dependencies.orchestrator.tools, callbacks);
61
- console.log("\nšŸ” Actions result:", actionsResult);
62
- return this.handleActionResults(Object.assign(Object.assign({}, actionsResult), { initialPrompt: prompt }), prompt);
63
- });
64
- }
65
- executeActions(predefinedActions, tools, callbacks) {
66
- return __awaiter(this, void 0, void 0, function* () {
67
- try {
68
- const queueManager = new queue_1.ActionQueueManager(tools, {
69
- onActionStart: callbacks === null || callbacks === void 0 ? void 0 : callbacks.onActionStart,
70
- onActionComplete: callbacks === null || callbacks === void 0 ? void 0 : callbacks.onActionComplete,
71
- onQueueComplete: callbacks === null || callbacks === void 0 ? void 0 : callbacks.onQueueComplete,
72
- onConfirmationRequired: (message) => __awaiter(this, void 0, void 0, function* () {
73
- if (callbacks === null || callbacks === void 0 ? void 0 : callbacks.onConfirmationRequired) {
74
- return yield callbacks.onConfirmationRequired(message);
75
- }
76
- return false;
77
- }),
78
- });
79
- queueManager.addToQueue(predefinedActions);
80
- if (callbacks === null || callbacks === void 0 ? void 0 : callbacks.onQueueStart) {
81
- callbacks.onQueueStart(predefinedActions);
82
- }
83
- console.log("Processing queue...");
84
- const results = yield queueManager.processQueue();
85
- console.log("Queue completed:");
86
- console.dir(results, { depth: null });
87
- return {
88
- type: "success",
89
- data: results || [],
90
- };
91
- }
92
- catch (error) {
93
- console.error("Error processing prompt:", error);
94
- throw error;
95
- }
96
- });
97
- }
98
- createCallbacks(prompt, similarActions) {
99
- return {
100
- onQueueStart: (actions) => __awaiter(this, void 0, void 0, function* () {
101
- console.dir(actions, { depth: null });
102
- this.dependencies.eventEmitter.emit("orchestrator-update", {
103
- type: "queue-start",
104
- actions,
105
- });
106
- }),
107
- onActionStart: (action) => {
108
- this.dependencies.eventEmitter.emit("orchestrator-update", {
109
- type: "action-start",
110
- action: action.name,
111
- args: action.parameters,
112
- });
113
- },
114
- onActionComplete: (action) => {
115
- this.dependencies.eventEmitter.emit("orchestrator-update", {
116
- type: "action-complete",
117
- action: action.name,
118
- result: action.result,
119
- });
120
- },
121
- onQueueComplete: (actions) => __awaiter(this, void 0, void 0, function* () {
122
- if (!similarActions.length) {
123
- yield this.saveToMemory(prompt, actions);
124
- }
125
- this.dependencies.eventEmitter.emit("orchestrator-update", {
126
- type: "queue-complete",
127
- });
128
- }),
129
- onConfirmationRequired: this.handleConfirmationRequest.bind(this),
130
- };
131
- }
132
- handleConfirmationRequest(message) {
133
- return __awaiter(this, void 0, void 0, function* () {
134
- return new Promise((resolve) => {
135
- const confirmationId = Date.now().toString();
136
- const handleConfirmation = (data) => {
137
- if (data.confirmationId === confirmationId) {
138
- this.dependencies.eventEmitter.removeListener("confirmation-response", handleConfirmation);
139
- resolve(data.confirmed);
140
- }
141
- };
142
- this.dependencies.eventEmitter.once("confirmation-response", handleConfirmation);
143
- this.dependencies.eventEmitter.emit("orchestrator-update", {
144
- type: "confirmation-required",
145
- id: confirmationId,
146
- message,
147
- });
148
- setTimeout(() => {
149
- this.dependencies.eventEmitter.removeListener("confirmation-response", handleConfirmation);
150
- resolve(false);
151
- }, this.CONFIRMATION_TIMEOUT);
152
- });
153
- });
154
- }
155
- saveToMemory(prompt, actions) {
156
- return __awaiter(this, void 0, void 0, function* () {
157
- console.log("\nšŸ” Creating memory...");
158
- yield this.dependencies.memoryCache.createMemory({
159
- content: prompt,
160
- userId: this.user.id,
161
- scope: types_1.MemoryScope.USER,
162
- type: types_1.MemoryType.ACTION,
163
- data: actions,
164
- });
165
- });
166
- }
167
- handleActionResults(actionsResult_1, prompt_1) {
168
- return __awaiter(this, arguments, void 0, function* (actionsResult, prompt, stream = true) {
169
- if (!this.hasNonPrepareActions(actionsResult.data)) {
170
- return;
171
- }
172
- const summarizer = new synthesizer_1.Summarizer();
173
- const summaryData = JSON.stringify({
174
- result: actionsResult.data,
175
- initialPrompt: prompt,
176
- });
177
- return stream
178
- ? (yield summarizer.streamProcess(summaryData)).toDataStreamResponse()
179
- : yield summarizer.process(summaryData);
180
- });
181
- }
182
- hasNonPrepareActions(actions) {
183
- return (Array.isArray(actions) &&
184
- actions.some((action) => { var _a; return ((_a = action.name) === null || _a === void 0 ? void 0 : _a.split("-")[0]) !== "prepare"; }));
185
- }
186
- }
187
- exports.Workflow = Workflow;
File without changes