@ai.ntellect/core 0.0.24 → 0.0.26

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, ProcessPromptCallbacks, QueueItem, QueueResult } from "../../types";
2
+ export declare class ActionHandler {
3
+ executeActions(predefinedActions: QueueItem[], tools: ActionSchema[], callbacks?: ProcessPromptCallbacks): Promise<{
4
+ type: string;
5
+ data: QueueResult[];
6
+ }>;
7
+ hasNonPrepareActions(actions: QueueResult[]): boolean;
8
+ }
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ActionHandler = void 0;
4
+ const queue_1 = require("../../services/queue");
5
+ class ActionHandler {
6
+ async executeActions(predefinedActions, tools, callbacks) {
7
+ try {
8
+ const queueManager = new queue_1.ActionQueueManager(tools, {
9
+ onActionStart: callbacks?.onActionStart,
10
+ onActionComplete: callbacks?.onActionComplete,
11
+ onQueueComplete: callbacks?.onQueueComplete,
12
+ onConfirmationRequired: async (message) => {
13
+ if (callbacks?.onConfirmationRequired) {
14
+ return await callbacks.onConfirmationRequired(message);
15
+ }
16
+ return false;
17
+ },
18
+ });
19
+ queueManager.addToQueue(predefinedActions);
20
+ if (callbacks?.onQueueStart) {
21
+ callbacks.onQueueStart(predefinedActions);
22
+ }
23
+ const results = await queueManager.processQueue();
24
+ return { type: "success", data: results || [] };
25
+ }
26
+ catch (error) {
27
+ console.error("Error processing prompt:", error);
28
+ throw error;
29
+ }
30
+ }
31
+ hasNonPrepareActions(actions) {
32
+ return (Array.isArray(actions) &&
33
+ actions.some((action) => action.name?.split("-")[0] !== "prepare"));
34
+ }
35
+ }
36
+ exports.ActionHandler = ActionHandler;
@@ -0,0 +1,7 @@
1
+ import EventEmitter from "events";
2
+ export declare class ConfirmationHandler {
3
+ private readonly eventEmitter;
4
+ private readonly CONFIRMATION_TIMEOUT;
5
+ constructor(eventEmitter: EventEmitter);
6
+ handleConfirmationRequest(message: string): Promise<boolean>;
7
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConfirmationHandler = void 0;
4
+ class ConfirmationHandler {
5
+ constructor(eventEmitter) {
6
+ this.eventEmitter = eventEmitter;
7
+ this.CONFIRMATION_TIMEOUT = 5 * 60 * 1000; // 5 minutes
8
+ }
9
+ async handleConfirmationRequest(message) {
10
+ return new Promise((resolve) => {
11
+ const confirmationId = Date.now().toString();
12
+ const handleConfirmation = (data) => {
13
+ if (data.confirmationId === confirmationId) {
14
+ this.eventEmitter.removeListener("confirmation-response", handleConfirmation);
15
+ resolve(data.confirmed);
16
+ }
17
+ };
18
+ this.eventEmitter.once("confirmation-response", handleConfirmation);
19
+ this.eventEmitter.emit("orchestrator-update", {
20
+ type: "confirmation-required",
21
+ id: confirmationId,
22
+ message,
23
+ });
24
+ setTimeout(() => {
25
+ this.eventEmitter.removeListener("confirmation-response", handleConfirmation);
26
+ resolve(false);
27
+ }, this.CONFIRMATION_TIMEOUT);
28
+ });
29
+ }
30
+ }
31
+ exports.ConfirmationHandler = ConfirmationHandler;
@@ -0,0 +1,10 @@
1
+ import EventEmitter from "events";
2
+ import { IEventHandler, QueueItem, QueueResult } from "../../types";
3
+ export declare class EventHandler implements IEventHandler {
4
+ private readonly eventEmitter;
5
+ constructor(eventEmitter: EventEmitter);
6
+ emitQueueStart(actions: QueueItem[]): void;
7
+ emitActionStart(action: QueueItem): void;
8
+ emitActionComplete(action: QueueResult): void;
9
+ emitQueueComplete(): void;
10
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EventHandler = void 0;
4
+ class EventHandler {
5
+ constructor(eventEmitter) {
6
+ this.eventEmitter = eventEmitter;
7
+ }
8
+ emitQueueStart(actions) {
9
+ this.eventEmitter.emit("orchestrator-update", {
10
+ type: "queue-start",
11
+ actions,
12
+ });
13
+ }
14
+ emitActionStart(action) {
15
+ this.eventEmitter.emit("orchestrator-update", {
16
+ type: "action-start",
17
+ action: action.name,
18
+ args: action.parameters,
19
+ });
20
+ }
21
+ emitActionComplete(action) {
22
+ this.eventEmitter.emit("orchestrator-update", {
23
+ type: "action-complete",
24
+ action: action.name,
25
+ result: action.result,
26
+ });
27
+ }
28
+ emitQueueComplete() {
29
+ this.eventEmitter.emit("orchestrator-update", {
30
+ type: "queue-complete",
31
+ });
32
+ }
33
+ }
34
+ exports.EventHandler = EventHandler;
@@ -0,0 +1,22 @@
1
+ import EventEmitter from "events";
2
+ import { Orchestrator } from "../llm/orchestrator";
3
+ import { MemoryCache } from "../memory";
4
+ import { AgentEvent, User } from "../types";
5
+ export declare class Agent {
6
+ private readonly user;
7
+ private readonly dependencies;
8
+ private readonly stream;
9
+ private readonly SIMILARITY_THRESHOLD;
10
+ private readonly MAX_RESULTS;
11
+ private readonly actionHandler;
12
+ constructor(user: User, dependencies: {
13
+ orchestrator: Orchestrator;
14
+ memoryCache: MemoryCache;
15
+ eventEmitter: EventEmitter;
16
+ }, stream?: boolean);
17
+ start(prompt: string, contextualizedPrompt: string, events: AgentEvent): Promise<any>;
18
+ private handleActions;
19
+ private findSimilarActions;
20
+ private transformActions;
21
+ private handleActionResults;
22
+ }
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Agent = void 0;
4
+ const synthesizer_1 = require("../llm/synthesizer");
5
+ const types_1 = require("../types");
6
+ const queue_item_transformer_1 = require("../utils/queue-item-transformer");
7
+ const ActionHandler_1 = require("./handlers/ActionHandler");
8
+ class Agent {
9
+ constructor(user, dependencies, stream = true) {
10
+ this.user = user;
11
+ this.dependencies = dependencies;
12
+ this.stream = stream;
13
+ this.SIMILARITY_THRESHOLD = 95;
14
+ this.MAX_RESULTS = 1;
15
+ this.actionHandler = new ActionHandler_1.ActionHandler();
16
+ }
17
+ async start(prompt, contextualizedPrompt, events) {
18
+ const request = await this.dependencies.orchestrator.process(contextualizedPrompt);
19
+ events.onMessage?.(request);
20
+ if (request.actions.length > 0) {
21
+ return this.handleActions({
22
+ initialPrompt: prompt,
23
+ actions: request.actions,
24
+ }, events);
25
+ }
26
+ }
27
+ async handleActions({ initialPrompt, actions, }, events) {
28
+ const similarActions = await this.findSimilarActions(initialPrompt);
29
+ const predefinedActions = this.transformActions(actions, similarActions);
30
+ const callbacks = {
31
+ onQueueStart: events.onQueueStart,
32
+ onActionStart: events.onActionStart,
33
+ onActionComplete: events.onActionComplete,
34
+ onQueueComplete: events.onQueueComplete,
35
+ onConfirmationRequired: events.onConfirmationRequired,
36
+ };
37
+ const actionsResult = await this.actionHandler.executeActions(predefinedActions, this.dependencies.orchestrator.tools, callbacks);
38
+ if (!this.actionHandler.hasNonPrepareActions(actionsResult.data)) {
39
+ return {
40
+ data: actionsResult.data,
41
+ initialPrompt,
42
+ };
43
+ }
44
+ return this.handleActionResults({ ...actionsResult, initialPrompt });
45
+ }
46
+ async findSimilarActions(prompt) {
47
+ return this.dependencies.memoryCache.findBestMatches(prompt, {
48
+ similarityThreshold: this.SIMILARITY_THRESHOLD,
49
+ maxResults: this.MAX_RESULTS,
50
+ userId: this.user.id,
51
+ scope: types_1.MemoryScope.USER,
52
+ });
53
+ }
54
+ transformActions(actions, similarActions) {
55
+ let predefinedActions = queue_item_transformer_1.QueueItemTransformer.transformActionsToQueueItems(actions) || [];
56
+ if (similarActions?.length > 0) {
57
+ predefinedActions =
58
+ queue_item_transformer_1.QueueItemTransformer.transformFromSimilarActions(similarActions) || [];
59
+ }
60
+ return predefinedActions;
61
+ }
62
+ async handleActionResults(actionsResult) {
63
+ const summarizer = new synthesizer_1.Summarizer();
64
+ const summaryData = JSON.stringify({
65
+ result: actionsResult.data,
66
+ initialPrompt: actionsResult.initialPrompt,
67
+ });
68
+ return this.stream
69
+ ? (await summarizer.streamProcess(summaryData)).toDataStreamResponse()
70
+ : await summarizer.process(summaryData);
71
+ }
72
+ }
73
+ exports.Agent = Agent;
@@ -0,0 +1,6 @@
1
+ export * from "./agent";
2
+ export * from "./llm/orchestrator";
3
+ export * from "./llm/synthesizer";
4
+ export * from "./services/queue";
5
+ export * from "./types";
6
+ export * from "./memory";
package/dist/index.js CHANGED
@@ -14,9 +14,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./agents/orchestrator"), exports);
18
- __exportStar(require("./agents/synthesizer"), exports);
17
+ __exportStar(require("./agent"), exports);
18
+ __exportStar(require("./llm/orchestrator"), exports);
19
+ __exportStar(require("./llm/synthesizer"), exports);
19
20
  __exportStar(require("./services/queue"), exports);
20
21
  __exportStar(require("./types"), exports);
21
22
  __exportStar(require("./memory"), exports);
22
- __exportStar(require("./workflow"), exports);
@@ -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,10 @@
1
+ export declare const summarizerContext: {
2
+ role: string;
3
+ guidelines: {
4
+ important: string[];
5
+ forMarketAnalysis: string[];
6
+ forGeneralRequests: string[];
7
+ never: string[];
8
+ };
9
+ compose: (results: string) => string;
10
+ };
@@ -0,0 +1,14 @@
1
+ import { StreamTextResult } from "ai";
2
+ import { BaseLLM } from "../../types";
3
+ export declare class Summarizer implements BaseLLM {
4
+ private readonly model;
5
+ process(prompt: string, onFinish?: (event: any) => void): Promise<{
6
+ actions: {
7
+ name: string;
8
+ result: string;
9
+ why: string;
10
+ }[];
11
+ response: string;
12
+ } | StreamTextResult<Record<string, any>>>;
13
+ streamProcess(prompt: string, onFinish?: (event: any) => void): Promise<StreamTextResult<Record<string, any>>>;
14
+ }
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Summarizer = 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 Summarizer {
9
+ constructor() {
10
+ this.model = (0, openai_1.openai)("gpt-4-turbo");
11
+ }
12
+ async process(prompt, onFinish) {
13
+ console.log("Summarizing results...");
14
+ const result = await (0, ai_1.generateObject)({
15
+ model: this.model,
16
+ schema: zod_1.z.object({
17
+ actions: zod_1.z.array(zod_1.z.object({
18
+ name: zod_1.z.string(),
19
+ result: zod_1.z.string(),
20
+ why: zod_1.z.string(),
21
+ })),
22
+ response: zod_1.z.string(),
23
+ }),
24
+ prompt: context_1.summarizerContext.compose(prompt),
25
+ system: context_1.summarizerContext.role,
26
+ });
27
+ console.log("Summarized results:", result.object);
28
+ if (onFinish)
29
+ onFinish(result.object);
30
+ return result.object;
31
+ }
32
+ async streamProcess(prompt, onFinish) {
33
+ const result = await (0, ai_1.streamText)({
34
+ model: this.model,
35
+ prompt: context_1.summarizerContext.compose(prompt),
36
+ onFinish: onFinish,
37
+ system: context_1.summarizerContext.role,
38
+ });
39
+ return result;
40
+ }
41
+ }
42
+ exports.Summarizer = Summarizer;
@@ -0,0 +1,21 @@
1
+ import { CreateMemoryInput, MatchOptions, MemoryCacheOptions, MemoryScope } from "../types";
2
+ export declare class MemoryCache {
3
+ private redis;
4
+ private readonly CACHE_PREFIX;
5
+ private readonly CACHE_TTL;
6
+ constructor(options?: MemoryCacheOptions);
7
+ private initRedis;
8
+ private getMemoryKey;
9
+ private storeMemory;
10
+ findBestMatches(query: string, options?: MatchOptions & {
11
+ userId?: string;
12
+ scope?: MemoryScope;
13
+ }): Promise<{
14
+ data: any;
15
+ similarityPercentage: number;
16
+ purpose: string;
17
+ }[]>;
18
+ private getAllMemories;
19
+ private getMemoriesFromKeys;
20
+ createMemory(input: CreateMemoryInput): Promise<string | undefined>;
21
+ }