@ai.ntellect/core 0.4.1 → 0.6.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.
Files changed (125) hide show
  1. package/.mocharc.json +1 -1
  2. package/README.md +311 -272
  3. package/dist/graph/controller.js +63 -0
  4. package/dist/graph/engine.js +563 -0
  5. package/dist/index.js +6 -6
  6. package/dist/memory/adapters/meilisearch/index.js +249 -0
  7. package/dist/memory/adapters/redis/index.js +96 -0
  8. package/dist/memory/index.js +9 -0
  9. package/dist/services/agenda.js +115 -0
  10. package/dist/services/embedding.js +40 -0
  11. package/dist/services/queue.js +99 -103
  12. package/dist/test/graph/controller.test.js +170 -0
  13. package/dist/test/graph/engine.test.js +465 -0
  14. package/dist/test/memory/adapters/meilisearch.test.js +250 -0
  15. package/dist/test/memory/adapters/redis.test.js +143 -0
  16. package/dist/test/memory/base.test.js +209 -0
  17. package/dist/test/services/agenda.test.js +230 -0
  18. package/dist/test/services/queue.test.js +258 -0
  19. package/dist/types/index.js +2 -0
  20. package/dist/utils/generate-object.js +32 -11
  21. package/dist/utils/inject-actions.js +2 -2
  22. package/dist/utils/queue-item-transformer.js +2 -2
  23. package/dist/utils/state-manager.js +20 -0
  24. package/graph/controller.ts +60 -0
  25. package/graph/engine.ts +709 -0
  26. package/index.ts +7 -7
  27. package/interfaces/index.ts +119 -0
  28. package/memory/adapters/meilisearch/index.ts +286 -0
  29. package/memory/adapters/redis/index.ts +103 -0
  30. package/memory/index.ts +22 -0
  31. package/package.json +9 -2
  32. package/services/agenda.ts +118 -0
  33. package/services/embedding.ts +26 -0
  34. package/services/queue.ts +5 -32
  35. package/test/.env.test +4 -0
  36. package/test/graph/controller.test.ts +186 -0
  37. package/test/graph/engine.test.ts +563 -0
  38. package/test/memory/adapters/meilisearch.test.ts +297 -0
  39. package/test/memory/adapters/redis.test.ts +160 -0
  40. package/test/memory/base.test.ts +229 -0
  41. package/test/services/agenda.test.ts +280 -0
  42. package/test/services/queue.test.ts +286 -44
  43. package/tsconfig.json +10 -9
  44. package/types/index.ts +270 -0
  45. package/utils/generate-object.js +111 -0
  46. package/utils/generate-object.ts +24 -12
  47. package/utils/header-builder.js +34 -0
  48. package/utils/inject-actions.js +16 -0
  49. package/utils/inject-actions.ts +3 -3
  50. package/utils/queue-item-transformer.js +24 -0
  51. package/utils/queue-item-transformer.ts +8 -11
  52. package/utils/sanitize-results.js +60 -0
  53. package/utils/schema-generator.js +46 -0
  54. package/utils/state-manager.js +20 -0
  55. package/utils/state-manager.ts +30 -0
  56. package/.nvmrc +0 -1
  57. package/README.FR.md +0 -365
  58. package/agent/index.ts +0 -244
  59. package/agent/tools/get-rss.ts +0 -64
  60. package/bull.ts +0 -5
  61. package/dist/agent/index.d.ts +0 -38
  62. package/dist/agent/index.js +0 -143
  63. package/dist/agent/tools/get-rss.d.ts +0 -16
  64. package/dist/agent/tools/get-rss.js +0 -62
  65. package/dist/bull.d.ts +0 -1
  66. package/dist/bull.js +0 -9
  67. package/dist/examples/index.d.ts +0 -2
  68. package/dist/examples/index.js +0 -89
  69. package/dist/index.d.ts +0 -7
  70. package/dist/llm/interpreter/context.d.ts +0 -15
  71. package/dist/llm/interpreter/context.js +0 -89
  72. package/dist/llm/interpreter/index.d.ts +0 -21
  73. package/dist/llm/interpreter/index.js +0 -87
  74. package/dist/llm/memory-manager/context.d.ts +0 -2
  75. package/dist/llm/memory-manager/context.js +0 -22
  76. package/dist/llm/memory-manager/index.d.ts +0 -17
  77. package/dist/llm/memory-manager/index.js +0 -107
  78. package/dist/llm/orchestrator/context.d.ts +0 -2
  79. package/dist/llm/orchestrator/context.js +0 -23
  80. package/dist/llm/orchestrator/index.d.ts +0 -44
  81. package/dist/llm/orchestrator/index.js +0 -139
  82. package/dist/llm/orchestrator/types.d.ts +0 -12
  83. package/dist/memory/cache.d.ts +0 -22
  84. package/dist/memory/cache.js +0 -165
  85. package/dist/memory/persistent.d.ts +0 -57
  86. package/dist/memory/persistent.js +0 -189
  87. package/dist/services/queue.d.ts +0 -13
  88. package/dist/services/redis-cache.d.ts +0 -37
  89. package/dist/services/redis-cache.js +0 -93
  90. package/dist/services/scheduler.d.ts +0 -40
  91. package/dist/services/scheduler.js +0 -99
  92. package/dist/services/telegram-monitor.d.ts +0 -0
  93. package/dist/services/telegram-monitor.js +0 -118
  94. package/dist/t.d.ts +0 -46
  95. package/dist/t.js +0 -102
  96. package/dist/test.d.ts +0 -0
  97. package/dist/test.js +0 -438
  98. package/dist/types.d.ts +0 -258
  99. package/dist/types.js +0 -22
  100. package/dist/utils/generate-object.d.ts +0 -12
  101. package/dist/utils/header-builder.d.ts +0 -11
  102. package/dist/utils/inject-actions.d.ts +0 -2
  103. package/dist/utils/queue-item-transformer.d.ts +0 -7
  104. package/dist/utils/sanitize-results.d.ts +0 -17
  105. package/dist/utils/schema-generator.d.ts +0 -16
  106. package/examples/index.ts +0 -103
  107. package/llm/interpreter/context.ts +0 -101
  108. package/llm/interpreter/index.ts +0 -136
  109. package/llm/memory-manager/context.ts +0 -21
  110. package/llm/memory-manager/index.ts +0 -163
  111. package/llm/orchestrator/context.ts +0 -22
  112. package/llm/orchestrator/index.ts +0 -232
  113. package/llm/orchestrator/types.ts +0 -14
  114. package/memory/cache.ts +0 -221
  115. package/memory/persistent.ts +0 -265
  116. package/services/redis-cache.ts +0 -128
  117. package/services/scheduler.ts +0 -128
  118. package/services/telegram-monitor.ts +0 -138
  119. package/t.py +0 -79
  120. package/t.spec +0 -38
  121. package/t.ts +0 -133
  122. package/test/llm/orchestrator.test.ts +0 -47
  123. package/test/llm/synthesizer.test.ts +0 -31
  124. package/types.ts +0 -288
  125. /package/dist/{llm/orchestrator/types.js → interfaces/index.js} +0 -0
@@ -0,0 +1,230 @@
1
+ "use strict";
2
+ // import { expect } from "chai";
3
+ // import sinon from "sinon";
4
+ // import { Agenda } from "../../services/agenda";
5
+ // before(function () {
6
+ // this.timeout(10000);
7
+ // });
8
+ // describe("Agenda Service", () => {
9
+ // let agenda: Agenda;
10
+ // const scheduledIds: string[] = []; // Track all scheduled request IDs
11
+ // beforeEach(() => {
12
+ // agenda = new Agenda();
13
+ // });
14
+ // afterEach(async () => {
15
+ // // Cancel all scheduled requests by their IDs
16
+ // scheduledIds.forEach((id) => agenda.cancelScheduledRequest(id));
17
+ // scheduledIds.length = 0; // Clear the array
18
+ // // Ensure all tasks are stopped
19
+ // await agenda.stop();
20
+ // // Vider la file d'attente
21
+ // await agenda.cancel({});
22
+ // // Attendre un peu pour s'assurer que tout est arrêté
23
+ // await new Promise((resolve) => setTimeout(resolve, 100));
24
+ // });
25
+ // describe("Request Scheduling", () => {
26
+ // it("should schedule a new request and return an id", async () => {
27
+ // const request = {
28
+ // originalRequest: "test request",
29
+ // cronExpression: "0 0 * * *",
30
+ // };
31
+ // const id = await agenda.scheduleRequest(request);
32
+ // scheduledIds.push(id); // Track the ID
33
+ // expect(id).to.be.a("string");
34
+ // expect(agenda.getScheduledRequests()).to.have.lengthOf(1);
35
+ // const scheduledRequest = agenda.getScheduledRequests()[0];
36
+ // expect(scheduledRequest.originalRequest).to.equal(
37
+ // request.originalRequest
38
+ // );
39
+ // expect(scheduledRequest.cronExpression).to.equal(request.cronExpression);
40
+ // expect(scheduledRequest.isRecurring).to.be.false;
41
+ // agenda.cancelScheduledRequest(id);
42
+ // });
43
+ // it("should execute callbacks when scheduling and executing", async function () {
44
+ // this.timeout(5000);
45
+ // const onScheduledSpy = sinon.spy();
46
+ // const onExecutedSpy = sinon.spy();
47
+ // const request = {
48
+ // originalRequest: "test request",
49
+ // cronExpression: `${(new Date().getSeconds() + 1) % 60} * * * * *`,
50
+ // };
51
+ // const id = await agenda.scheduleRequest(request, {
52
+ // onScheduled: onScheduledSpy,
53
+ // onExecuted: onExecutedSpy,
54
+ // });
55
+ // scheduledIds.push(id); // Track the ID
56
+ // expect(onScheduledSpy.calledOnce).to.be.true;
57
+ // await new Promise<void>((resolve, reject) => {
58
+ // const timeout = setTimeout(() => {
59
+ // reject(new Error("Callback execution timeout"));
60
+ // }, 4000);
61
+ // const checkExecution = () => {
62
+ // if (onExecutedSpy.calledOnce) {
63
+ // clearTimeout(timeout);
64
+ // agenda.cancelScheduledRequest(id);
65
+ // resolve();
66
+ // return;
67
+ // }
68
+ // setTimeout(checkExecution, 100);
69
+ // };
70
+ // checkExecution();
71
+ // });
72
+ // expect(onExecutedSpy.calledOnce).to.be.true;
73
+ // });
74
+ // });
75
+ // describe("Request Management", () => {
76
+ // it("should cancel a scheduled request", async () => {
77
+ // const request = {
78
+ // originalRequest: "test request",
79
+ // cronExpression: "*/1 * * * *",
80
+ // };
81
+ // const id = await agenda.scheduleRequest(request);
82
+ // scheduledIds.push(id); // Track the ID
83
+ // expect(agenda.getScheduledRequests()).to.have.lengthOf(1);
84
+ // const cancelled = agenda.cancelScheduledRequest(id);
85
+ // expect(cancelled).to.be.true;
86
+ // expect(agenda.getScheduledRequests()).to.have.lengthOf(0);
87
+ // });
88
+ // it("should return false when cancelling non-existent request", () => {
89
+ // const cancelled = agenda.cancelScheduledRequest("non-existent-id");
90
+ // expect(cancelled).to.be.false;
91
+ // });
92
+ // it("should get all scheduled requests", async () => {
93
+ // const requests = [
94
+ // {
95
+ // originalRequest: "request 1",
96
+ // cronExpression: "*/1 * * * *",
97
+ // },
98
+ // {
99
+ // originalRequest: "request 2",
100
+ // cronExpression: "*/5 * * * *",
101
+ // },
102
+ // ];
103
+ // for (const request of requests) {
104
+ // const id = await agenda.scheduleRequest(request);
105
+ // scheduledIds.push(id); // Track the ID
106
+ // }
107
+ // const scheduledRequests = agenda.getScheduledRequests();
108
+ // expect(scheduledRequests).to.have.lengthOf(2);
109
+ // expect(scheduledRequests[0].originalRequest).to.equal("request 1");
110
+ // expect(scheduledRequests[1].originalRequest).to.equal("request 2");
111
+ // });
112
+ // });
113
+ // describe("Global Management", () => {
114
+ // it("should stop all scheduled requests", async () => {
115
+ // const requests = [
116
+ // {
117
+ // originalRequest: "request 1",
118
+ // cronExpression: "*/1 * * * *",
119
+ // },
120
+ // {
121
+ // originalRequest: "request 2",
122
+ // cronExpression: "*/5 * * * *",
123
+ // },
124
+ // ];
125
+ // for (const request of requests) {
126
+ // await agenda.scheduleRequest(request);
127
+ // }
128
+ // expect(agenda.getScheduledRequests()).to.have.lengthOf(2);
129
+ // agenda.stopAll();
130
+ // expect(agenda.getScheduledRequests()).to.have.lengthOf(0);
131
+ // });
132
+ // });
133
+ // describe("Error Handling", () => {
134
+ // it("should handle execution errors gracefully", async () => {
135
+ // const consoleSpy = sinon.spy(console, "error");
136
+ // const request = {
137
+ // originalRequest: "error request",
138
+ // cronExpression: "0 0 * * *",
139
+ // };
140
+ // const id = await agenda.scheduleRequest(request);
141
+ // // Wait for execution
142
+ // await new Promise((resolve) => setTimeout(resolve, 1100));
143
+ // expect(consoleSpy.called).to.be.false;
144
+ // agenda.cancelScheduledRequest(id);
145
+ // consoleSpy.restore();
146
+ // });
147
+ // });
148
+ // describe("Request Execution", () => {
149
+ // it("should execute non-recurring requests only once", async function () {
150
+ // this.timeout(5000);
151
+ // const onExecutedSpy = sinon.spy();
152
+ // const request = {
153
+ // originalRequest: "single execution",
154
+ // cronExpression: `${new Date().getSeconds() + 1} * * * * *`,
155
+ // };
156
+ // const id = await agenda.scheduleRequest(request, {
157
+ // onExecuted: onExecutedSpy,
158
+ // });
159
+ // try {
160
+ // await new Promise<void>((resolve, reject) => {
161
+ // const timeout = setTimeout(
162
+ // () => reject(new Error("Test timeout")),
163
+ // 4000
164
+ // );
165
+ // const checkExecution = () => {
166
+ // if (onExecutedSpy.calledOnce) {
167
+ // clearTimeout(timeout);
168
+ // resolve();
169
+ // return;
170
+ // }
171
+ // setTimeout(checkExecution, 100);
172
+ // };
173
+ // checkExecution();
174
+ // });
175
+ // } finally {
176
+ // agenda.cancelScheduledRequest(id);
177
+ // }
178
+ // expect(onExecutedSpy.calledOnce).to.be.true;
179
+ // expect(agenda.getScheduledRequests()).to.have.lengthOf(0);
180
+ // });
181
+ // it("should log execution status", async function () {
182
+ // this.timeout(10000);
183
+ // const consoleLogSpy = sinon.spy(console, "log");
184
+ // const request = {
185
+ // originalRequest: "test request",
186
+ // cronExpression: `${new Date().getSeconds() + 1} * * * * *`,
187
+ // };
188
+ // const id = await agenda.scheduleRequest(request);
189
+ // await new Promise<void>((resolve) => {
190
+ // const checkExecution = () => {
191
+ // if (
192
+ // consoleLogSpy.calledWith(`🔄 Executing scheduled request: ${id}`) &&
193
+ // consoleLogSpy.calledWith(
194
+ // `✅ Scheduled request executed successfully: ${id}`
195
+ // )
196
+ // ) {
197
+ // agenda.cancelScheduledRequest(id);
198
+ // resolve();
199
+ // return;
200
+ // }
201
+ // setTimeout(checkExecution, 100);
202
+ // };
203
+ // checkExecution();
204
+ // });
205
+ // expect(consoleLogSpy.calledWith(`🔄 Executing scheduled request: ${id}`))
206
+ // .to.be.true;
207
+ // expect(
208
+ // consoleLogSpy.calledWith(
209
+ // `✅ Scheduled request executed successfully: ${id}`
210
+ // )
211
+ // ).to.be.true;
212
+ // consoleLogSpy.restore();
213
+ // });
214
+ // });
215
+ // });
216
+ // // Déplacer la variable agenda en dehors du describe pour la rendre accessible
217
+ // let globalAgenda: Agenda;
218
+ // before(() => {
219
+ // globalAgenda = new Agenda();
220
+ // });
221
+ // after(async () => {
222
+ // if (globalAgenda) {
223
+ // globalAgenda.stopAll();
224
+ // await new Promise((resolve) => setTimeout(resolve, 100));
225
+ // }
226
+ // // Nettoyage final
227
+ // await globalAgenda.stop();
228
+ // await globalAgenda.cancel({});
229
+ // await new Promise((resolve) => setTimeout(resolve, 100));
230
+ // });
@@ -0,0 +1,258 @@
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
+ const queue_1 = require("@/services/queue");
13
+ const chai_1 = require("chai");
14
+ const zod_1 = require("zod");
15
+ describe("Queue", () => {
16
+ // Test actions setup
17
+ const testActions = [
18
+ {
19
+ name: "action1",
20
+ description: "Test action 1",
21
+ parameters: zod_1.z.object({}),
22
+ execute: (params) => __awaiter(void 0, void 0, void 0, function* () { return ({ success: true, params }); }),
23
+ },
24
+ {
25
+ name: "action2",
26
+ description: "Test action 2",
27
+ parameters: zod_1.z.object({}),
28
+ execute: (params) => __awaiter(void 0, void 0, void 0, function* () { return ({ success: true, params }); }),
29
+ confirmation: {
30
+ requireConfirmation: true,
31
+ message: "Confirm action2?",
32
+ },
33
+ },
34
+ {
35
+ name: "actionWithError",
36
+ description: "Test error action",
37
+ parameters: zod_1.z.object({}),
38
+ execute: () => __awaiter(void 0, void 0, void 0, function* () {
39
+ throw new Error("Test error");
40
+ }),
41
+ },
42
+ ];
43
+ let queue;
44
+ let callbacks;
45
+ beforeEach(() => {
46
+ // Reset callbacks for each test
47
+ callbacks = {
48
+ onActionStart: () => { },
49
+ onActionComplete: () => { },
50
+ onQueueComplete: () => { },
51
+ onConfirmationRequired: () => __awaiter(void 0, void 0, void 0, function* () { return true; }),
52
+ };
53
+ queue = new queue_1.Queue(testActions, callbacks);
54
+ });
55
+ describe("Queue Management", () => {
56
+ it("should add a single action to the queue", () => {
57
+ const action = {
58
+ name: "action1",
59
+ parameters: [{ name: "param1", value: "value1" }],
60
+ };
61
+ queue.add(action);
62
+ (0, chai_1.expect)(queue["queue"]).to.have.lengthOf(1);
63
+ (0, chai_1.expect)(queue["queue"][0]).to.deep.equal(action);
64
+ });
65
+ it("should add multiple actions to the queue", () => {
66
+ const actions = [
67
+ {
68
+ name: "action1",
69
+ parameters: [{ name: "param1", value: "value1" }],
70
+ },
71
+ {
72
+ name: "action2",
73
+ parameters: [{ name: "param2", value: "value2" }],
74
+ },
75
+ ];
76
+ queue.add(actions);
77
+ (0, chai_1.expect)(queue["queue"]).to.have.lengthOf(2);
78
+ (0, chai_1.expect)(queue["queue"]).to.deep.equal(actions);
79
+ });
80
+ });
81
+ describe("Action Execution", () => {
82
+ it("should execute a single action successfully", () => __awaiter(void 0, void 0, void 0, function* () {
83
+ const action = {
84
+ name: "action1",
85
+ parameters: [{ name: "param1", value: "value1" }],
86
+ };
87
+ queue.add(action);
88
+ const results = yield queue.execute();
89
+ if (!results) {
90
+ throw new Error("Results are undefined");
91
+ }
92
+ (0, chai_1.expect)(results).to.not.be.undefined;
93
+ (0, chai_1.expect)(results).to.have.lengthOf(1);
94
+ (0, chai_1.expect)(results[0].name).to.equal("action1");
95
+ (0, chai_1.expect)(results[0].error).to.be.null;
96
+ (0, chai_1.expect)(results[0].result).to.deep.include({ success: true });
97
+ }));
98
+ it("should handle action execution errors", () => __awaiter(void 0, void 0, void 0, function* () {
99
+ const action = {
100
+ name: "actionWithError",
101
+ parameters: [],
102
+ };
103
+ queue.add(action);
104
+ const results = yield queue.execute();
105
+ if (!results) {
106
+ throw new Error("Results are undefined");
107
+ }
108
+ (0, chai_1.expect)(results).to.not.be.undefined;
109
+ (0, chai_1.expect)(results).to.have.lengthOf(1);
110
+ (0, chai_1.expect)(results[0].name).to.equal("actionWithError");
111
+ (0, chai_1.expect)(results[0].error).to.equal("Test error");
112
+ (0, chai_1.expect)(results[0].result).to.be.null;
113
+ }));
114
+ it("should respect confirmation requirements", () => __awaiter(void 0, void 0, void 0, function* () {
115
+ let confirmationCalled = false;
116
+ callbacks.onConfirmationRequired = () => __awaiter(void 0, void 0, void 0, function* () {
117
+ confirmationCalled = true;
118
+ return false; // Reject the confirmation
119
+ });
120
+ queue = new queue_1.Queue(testActions, callbacks);
121
+ const action = {
122
+ name: "action2", // Action requiring confirmation
123
+ parameters: [],
124
+ };
125
+ queue.add(action);
126
+ const results = yield queue.execute();
127
+ if (!results) {
128
+ throw new Error("Results are undefined");
129
+ }
130
+ (0, chai_1.expect)(results).to.not.be.undefined;
131
+ (0, chai_1.expect)(confirmationCalled).to.be.true;
132
+ (0, chai_1.expect)(results[0].cancelled).to.be.true;
133
+ (0, chai_1.expect)(results[0].error).to.equal("Action cancelled by user");
134
+ }));
135
+ });
136
+ describe("Parameter Handling", () => {
137
+ it("should correctly format simple parameters", () => __awaiter(void 0, void 0, void 0, function* () {
138
+ const action = {
139
+ name: "action1",
140
+ parameters: [
141
+ { name: "param1", value: "value1" },
142
+ { name: "param2", value: "value2" },
143
+ ],
144
+ };
145
+ queue.add(action);
146
+ const results = yield queue.execute();
147
+ if (!results) {
148
+ throw new Error("Results are undefined");
149
+ }
150
+ (0, chai_1.expect)(results).to.not.be.undefined;
151
+ (0, chai_1.expect)(results[0].parameters).to.deep.equal({
152
+ param1: "value1",
153
+ param2: "value2",
154
+ });
155
+ }));
156
+ it("should handle JSON stringified parameters", () => __awaiter(void 0, void 0, void 0, function* () {
157
+ const action = {
158
+ name: "action1",
159
+ parameters: [
160
+ {
161
+ name: "jsonParam",
162
+ value: JSON.stringify({ name: "test", value: "value" }),
163
+ },
164
+ ],
165
+ };
166
+ queue.add(action);
167
+ const results = yield queue.execute();
168
+ if (!results) {
169
+ throw new Error("Results are undefined");
170
+ }
171
+ (0, chai_1.expect)(results).to.not.be.undefined;
172
+ (0, chai_1.expect)(results[0].parameters).to.deep.equal({
173
+ test: "value",
174
+ });
175
+ }));
176
+ });
177
+ describe("Queue Processing State", () => {
178
+ it("should prevent concurrent queue processing", () => __awaiter(void 0, void 0, void 0, function* () {
179
+ const action = {
180
+ name: "action1",
181
+ parameters: [],
182
+ };
183
+ queue.add(action);
184
+ // Start first execution
185
+ const firstExecution = queue.execute();
186
+ // Try to execute again while first execution is running
187
+ const secondExecution = queue.execute();
188
+ const [firstResults, secondResults] = yield Promise.all([
189
+ firstExecution,
190
+ secondExecution,
191
+ ]);
192
+ (0, chai_1.expect)(firstResults).to.not.be.undefined;
193
+ (0, chai_1.expect)(firstResults).to.have.lengthOf(1);
194
+ (0, chai_1.expect)(secondResults).to.be.undefined;
195
+ }));
196
+ it("should reset processing state after completion", () => __awaiter(void 0, void 0, void 0, function* () {
197
+ const action = {
198
+ name: "action1",
199
+ parameters: [],
200
+ };
201
+ queue.add(action);
202
+ const results = yield queue.execute();
203
+ if (!results) {
204
+ throw new Error("Results are undefined");
205
+ }
206
+ (0, chai_1.expect)(results).to.have.lengthOf(1);
207
+ // Verify that isProcessing is reset
208
+ (0, chai_1.expect)(queue["isProcessing"]).to.be.false;
209
+ // Clear both queue and results before adding new action
210
+ queue["queue"] = [];
211
+ queue["results"] = [];
212
+ // Should be able to execute again
213
+ queue.add(action);
214
+ const secondResults = yield queue.execute();
215
+ if (!secondResults) {
216
+ throw new Error("Second results are undefined");
217
+ }
218
+ (0, chai_1.expect)(secondResults).to.have.lengthOf(1);
219
+ }));
220
+ });
221
+ describe("Callback Handling", () => {
222
+ it("should trigger all callbacks in correct order", () => __awaiter(void 0, void 0, void 0, function* () {
223
+ const callbackOrder = [];
224
+ callbacks = {
225
+ onActionStart: () => callbackOrder.push("start"),
226
+ onActionComplete: () => callbackOrder.push("complete"),
227
+ onQueueComplete: () => callbackOrder.push("queueComplete"),
228
+ };
229
+ queue = new queue_1.Queue(testActions, callbacks);
230
+ const action = {
231
+ name: "action1",
232
+ parameters: [],
233
+ };
234
+ queue.add(action);
235
+ yield queue.execute();
236
+ (0, chai_1.expect)(callbackOrder).to.deep.equal([
237
+ "start",
238
+ "complete",
239
+ "queueComplete",
240
+ ]);
241
+ }));
242
+ it("should handle missing callbacks gracefully", () => __awaiter(void 0, void 0, void 0, function* () {
243
+ queue = new queue_1.Queue(testActions, {}); // No callbacks provided
244
+ const action = {
245
+ name: "action1",
246
+ parameters: [],
247
+ };
248
+ queue.add(action);
249
+ const results = yield queue.execute();
250
+ if (!results) {
251
+ throw new Error("Results are undefined");
252
+ }
253
+ (0, chai_1.expect)(results).to.not.be.undefined;
254
+ (0, chai_1.expect)(results).to.have.lengthOf(1);
255
+ (0, chai_1.expect)(results[0].error).to.be.null;
256
+ }));
257
+ });
258
+ });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,4 +1,13 @@
1
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
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.generateObject = exports.describeZodSchema = void 0;
4
13
  const ai_1 = require("ai");
@@ -44,14 +53,12 @@ const describeZodSchema = (schema) => {
44
53
  return "z.unknown()"; // Fallback for unknown types
45
54
  };
46
55
  exports.describeZodSchema = describeZodSchema;
47
- const generateObject = async (config) => {
56
+ const generateObject = (config) => __awaiter(void 0, void 0, void 0, function* () {
57
+ var _a;
48
58
  // Generate a detailed description of the schema
49
59
  const schemaDescription = (0, exports.describeZodSchema)(config.schema);
50
- console.log("🔍 Schema Description:\n", schemaDescription);
51
- const response = await (0, ai_1.generateText)({
52
- model: config.model,
53
- prompt: `${config.prompt}
54
-
60
+ const baseContext = `
61
+ ${config.system}
55
62
  EXPECTED SCHEMA:
56
63
  ${schemaDescription}
57
64
 
@@ -61,16 +68,30 @@ const generateObject = async (config) => {
61
68
  "key": "value"
62
69
  }
63
70
  \`\`\`
64
-
71
+
65
72
  GOOD EXAMPLE:
66
73
  {
67
74
  "key": "value"
68
75
  }
69
-
70
- Output only the JSON schema, no 'triple quotes'json or any other text. Only the JSON schema.
71
- `,
76
+
77
+ OUTPUT ONLY THE JSON SCHEMA, NO 'TRIPLE QUOTES'JSON OR ANY OTHER TEXT. ONLY THE JSON SCHEMA.
78
+ `;
79
+ console.log("🔍 Generating object with context:");
80
+ console.log(`${config.prompt}\n${baseContext}\n`);
81
+ const response = yield (0, ai_1.generateText)({
82
+ model: config.model,
83
+ messages: !config.prompt
84
+ ? [
85
+ {
86
+ role: "system",
87
+ content: baseContext,
88
+ },
89
+ ...((_a = config.messages) !== null && _a !== void 0 ? _a : []),
90
+ ]
91
+ : undefined,
72
92
  system: config.system,
73
93
  temperature: config.temperature,
94
+ prompt: !config.prompt ? undefined : `${config.prompt}\n\n${baseContext}`,
74
95
  });
75
96
  try {
76
97
  // Clean the response text from any markdown or code block markers
@@ -86,5 +107,5 @@ const generateObject = async (config) => {
86
107
  console.error("Error parsing or validating JSON response:", error);
87
108
  throw new Error("Failed to generate valid JSON response");
88
109
  }
89
- };
110
+ });
90
111
  exports.generateObject = generateObject;
@@ -5,8 +5,8 @@ const injectActions = (actions) => {
5
5
  return actions.map((action) => {
6
6
  const parameters = action.parameters;
7
7
  const schemaShape = Object.keys(parameters._def.shape()).join(", ");
8
- const actionString = `Name: ${action.name}, Description: ${action.description}, Arguments: { ${schemaShape} } ${action.examples
9
- ? `Format examples: ${action.examples.map((example) => {
8
+ const actionString = `* ${action.name}( { ${schemaShape} }) (${action.description}) ${action.examples
9
+ ? `Eg: ${action.examples.map((example) => {
10
10
  return JSON.stringify(example);
11
11
  })}`
12
12
  : ""}`;
@@ -9,7 +9,7 @@ class QueueItemTransformer {
9
9
  };
10
10
  }
11
11
  static transformFromSimilarActions(similarActions) {
12
- return similarActions?.map((action) => QueueItemTransformer.transformActionToQueueItem(action));
12
+ return similarActions === null || similarActions === void 0 ? void 0 : similarActions.map((action) => QueueItemTransformer.transformActionToQueueItem(action));
13
13
  }
14
14
  static transformParameters(parameters) {
15
15
  return Object.entries(parameters).map(([name, value]) => ({
@@ -18,7 +18,7 @@ class QueueItemTransformer {
18
18
  }));
19
19
  }
20
20
  static transformActionsToQueueItems(actions) {
21
- return actions?.map((action) => this.transformActionToQueueItem(action));
21
+ return actions === null || actions === void 0 ? void 0 : actions.map((action) => this.transformActionToQueueItem(action));
22
22
  }
23
23
  }
24
24
  exports.QueueItemTransformer = QueueItemTransformer;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StateManager = void 0;
4
+ class StateManager {
5
+ /**
6
+ * Updates the shared state while preserving immutability
7
+ * @param currentState Current shared state
8
+ * @param updates Partial updates to apply
9
+ * @returns Updated shared state
10
+ */
11
+ static updateState(state, updates) {
12
+ return Object.assign(Object.assign({}, state), { context: Object.assign(Object.assign({}, (state.context || {})), updates) });
13
+ }
14
+ static createUpdate(updates) {
15
+ return {
16
+ context: Object.assign({}, updates),
17
+ };
18
+ }
19
+ }
20
+ exports.StateManager = StateManager;
@@ -0,0 +1,60 @@
1
+ import { GraphDefinition } from "@/types";
2
+ import { GraphEngine } from "./engine";
3
+
4
+ /**
5
+ * Controller responsible for executing workflows based on graph definitions.
6
+ * @template T The type representing the graph's state.
7
+ */
8
+ export class GraphController<T> {
9
+ /**
10
+ * Executes a sequence of actions using the corresponding graph definitions.
11
+ * @param {any[]} actions - The list of actions to execute.
12
+ * @param {GraphDefinition<T>[]} graphs - The available graph definitions.
13
+ * @returns {Promise<any>} The final state of the executed workflow.
14
+ * @throws {Error} If no actions are provided or if the graph is not found.
15
+ */
16
+ async run(actions: any[], graphs: GraphDefinition<T>[]): Promise<any> {
17
+ if (actions.length === 0) {
18
+ throw new Error("No actions provided");
19
+ }
20
+
21
+ // Create a mapping of graph names to their definitions for quick lookup.
22
+ const graphMap = new Map(graphs.map((g) => [g.name, g]));
23
+
24
+ for (const action of actions) {
25
+ // Retrieve the graph definition based on the action name.
26
+ const graphDefinition = graphMap.get(action.name);
27
+ if (!graphDefinition) {
28
+ throw new Error(`Graph not found: ${action.name}`);
29
+ }
30
+
31
+ // Initialize the graph engine with the selected graph definition.
32
+ const graph = new GraphEngine(graphDefinition, {
33
+ schema: graphDefinition.schema,
34
+ autoDetectCycles: true,
35
+ });
36
+
37
+ // Construct the initial state from action parameters.
38
+ const initialState = {
39
+ context: action.parameters.reduce(
40
+ (acc: Record<string, any>, param: { name: string; value: any }) => {
41
+ acc[param.name] = param.value;
42
+ return acc;
43
+ },
44
+ {}
45
+ ),
46
+ };
47
+
48
+ // Execute the graph starting from the defined entry node.
49
+ await graph.execute(initialState, graphDefinition.entryNode);
50
+
51
+ // Retrieve the final state after execution.
52
+ const result = graph.getState();
53
+ if (!result) {
54
+ throw new Error("Workflow execution failed to return a state");
55
+ }
56
+
57
+ return result;
58
+ }
59
+ }
60
+ }