@ai.ntellect/core 0.2.8 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.FR.md +58 -19
  2. package/README.md +40 -17
  3. package/agent/index.ts +68 -27
  4. package/dist/agent/index.d.ts +12 -8
  5. package/dist/agent/index.js +34 -12
  6. package/dist/index.d.ts +2 -1
  7. package/dist/index.js +2 -1
  8. package/dist/llm/evaluator/context.d.ts +0 -1
  9. package/dist/llm/evaluator/context.js +3 -12
  10. package/dist/llm/evaluator/index.d.ts +7 -1
  11. package/dist/llm/evaluator/index.js +37 -24
  12. package/dist/llm/interpreter/context.d.ts +32 -0
  13. package/dist/llm/interpreter/context.js +90 -0
  14. package/dist/llm/interpreter/index.d.ts +17 -0
  15. package/dist/llm/{synthesizer → interpreter}/index.js +18 -27
  16. package/dist/llm/orchestrator/context.js +1 -1
  17. package/dist/llm/orchestrator/index.d.ts +7 -3
  18. package/dist/llm/orchestrator/index.js +5 -48
  19. package/dist/memory/cache.d.ts +2 -2
  20. package/dist/memory/cache.js +4 -5
  21. package/dist/memory/persistent.d.ts +1 -0
  22. package/dist/memory/persistent.js +2 -1
  23. package/dist/test.d.ts +54 -0
  24. package/dist/test.js +125 -20
  25. package/dist/types.d.ts +12 -13
  26. package/index.ts +2 -2
  27. package/llm/evaluator/context.ts +9 -16
  28. package/llm/evaluator/index.ts +60 -30
  29. package/llm/interpreter/context.ts +89 -0
  30. package/llm/{synthesizer → interpreter}/index.ts +20 -26
  31. package/llm/orchestrator/context.ts +4 -6
  32. package/llm/orchestrator/index.ts +15 -68
  33. package/memory/cache.ts +5 -6
  34. package/memory/persistent.ts +5 -9
  35. package/package.json +1 -1
  36. package/types.ts +13 -13
  37. package/dist/llm/synthesizer/context.d.ts +0 -15
  38. package/dist/llm/synthesizer/context.js +0 -71
  39. package/dist/llm/synthesizer/index.d.ts +0 -14
  40. package/llm/synthesizer/context.ts +0 -68
@@ -3,7 +3,7 @@ import { generateObject } from "ai";
3
3
  import { z } from "zod";
4
4
  import { CacheMemory } from "../../memory/cache";
5
5
  import { PersistentMemory } from "../../memory/persistent";
6
- import { ActionSchema, MemoryScope, MemoryScopeType, State } from "../../types";
6
+ import { ActionSchema, State } from "../../types";
7
7
  import { injectActions } from "../../utils/inject-actions";
8
8
  import { orchestratorContext } from "./context";
9
9
 
@@ -16,14 +16,18 @@ export class Orchestrator {
16
16
  };
17
17
  private id: string;
18
18
 
19
- constructor(
20
- id: string,
21
- tools: ActionSchema[],
19
+ constructor({
20
+ id,
21
+ tools,
22
+ memory,
23
+ }: {
24
+ id: string;
25
+ tools: ActionSchema[];
22
26
  memory: {
23
27
  persistent: PersistentMemory;
24
28
  cache: CacheMemory;
25
- }
26
- ) {
29
+ };
30
+ }) {
27
31
  this.id = id;
28
32
  this.memory = memory;
29
33
  this.tools = [
@@ -37,73 +41,18 @@ export class Orchestrator {
37
41
  }),
38
42
  execute: async ({ query }: { query: string }) => {
39
43
  const persistentMemories =
40
- await this.memory.persistent.searchSimilarQueries(query, {
44
+ await this.memory.persistent.findRelevantDocuments(query, {
41
45
  similarityThreshold: 70,
42
46
  });
43
- return persistentMemories;
44
- },
45
- },
46
- {
47
- name: "search_cache_memory",
48
- description: "Search for relevant information in the cache",
49
- parameters: z.object({
50
- query: z.string(),
51
- }),
52
- execute: async ({ query }: { query: string }) => {
53
- const cacheMemories = await this.memory.cache.findSimilarQueries(
54
- query,
55
- {
56
- similarityThreshold: 70,
57
- maxResults: 1,
58
- userId: this.id,
59
- scope: MemoryScope.GLOBAL,
60
- }
61
- );
62
- return cacheMemories;
63
- },
64
- },
65
- {
66
- name: "save_memory",
67
- description: "Save relevant information in the internal knowledge base",
68
- parameters: z.object({
69
- query: z.string(),
70
- memoryType: z.string(),
71
- data: z.any(),
72
- scope: z.string().default("GLOBAL").describe("GLOBAL or USER"),
73
- userId: z.string(),
74
- whyStored: z.string(),
75
- }),
76
- execute: async ({
77
- query,
78
- purpose,
79
- data,
80
- scope,
81
- userId,
82
- }: {
83
- query: string;
84
- purpose: string;
85
- data: any;
86
- scope: MemoryScopeType;
87
- userId?: string;
88
- }) => {
89
- const memories = await this.memory.persistent.createMemory({
90
- query,
91
- purpose,
92
- data,
93
- scope,
94
- userId,
95
- createdAt: new Date(),
96
- id: crypto.randomUUID(),
97
- });
98
- return memories;
47
+ return `# LONG_TERM_MEMORY: ${JSON.stringify(persistentMemories)}`;
99
48
  },
100
49
  },
101
50
  ];
102
51
  }
103
52
 
104
53
  composeContext(state: State) {
105
- const { behavior, userRequest, actions, results } = state;
106
- const { role, language, guidelines } = behavior;
54
+ const { userRequest, results } = state;
55
+ const { role, language, guidelines } = orchestratorContext.behavior;
107
56
  const { important, warnings } = guidelines;
108
57
 
109
58
  const context = `
@@ -111,7 +60,7 @@ export class Orchestrator {
111
60
  # LANGUAGE: ${language}
112
61
  # IMPORTANT: ${important.join("\n")}
113
62
  # USER_REQUEST: ${userRequest}
114
- # ACTIONS_AVAILABLES: ${injectActions(actions)} (NEVER REPEAT ACTIONS)
63
+ # ACTIONS_AVAILABLES: ${injectActions(this.tools)} (NEVER REPEAT ACTIONS)
115
64
  # CURRENT_RESULTS: ${results}
116
65
  `.trim();
117
66
 
@@ -133,9 +82,7 @@ export class Orchestrator {
133
82
  answer: string;
134
83
  }> {
135
84
  const state = this.composeContext({
136
- behavior: orchestratorContext.behavior,
137
85
  userRequest: prompt,
138
- actions: this.tools,
139
86
  results: results,
140
87
  });
141
88
  try {
package/memory/cache.ts CHANGED
@@ -60,12 +60,12 @@ export class CacheMemory {
60
60
  console.log("💾 Cache memory created:", result);
61
61
  }
62
62
 
63
- async findSimilarQueries(
63
+ async findSimilarActions(
64
64
  query: string,
65
65
  options: MatchOptions & { userId?: string; scope?: MemoryScope } = {}
66
66
  ): Promise<
67
67
  {
68
- data: QueueResult[];
68
+ actions: QueueResult[];
69
69
  similarityPercentage: number;
70
70
  query: string;
71
71
  }[]
@@ -87,10 +87,10 @@ export class CacheMemory {
87
87
  const similarity = cosineSimilarity(embedding, memory.embedding);
88
88
  const similarityPercentage = (similarity + 1) * 50;
89
89
  return {
90
- data: memory.data,
90
+ actions: memory.data,
91
91
  query: memory.query,
92
92
  similarityPercentage,
93
- memoryId: memory.id,
93
+ createdAt: memory.createdAt,
94
94
  };
95
95
  })
96
96
  .filter(
@@ -111,7 +111,6 @@ export class CacheMemory {
111
111
  console.log(`\n${index + 1}. Match Details:`);
112
112
  console.log(` Query: ${match.query}`);
113
113
  console.log(` Similarity: ${match.similarityPercentage.toFixed(2)}%`);
114
- console.log(` Memory ID: ${match.memoryId}`);
115
114
  console.log("─".repeat(50));
116
115
  });
117
116
  } else {
@@ -165,7 +164,7 @@ export class CacheMemory {
165
164
  console.log("Type:", input.type);
166
165
  console.log("Scope:", input.scope);
167
166
 
168
- const existingPattern = await this.findSimilarQueries(input.content, {
167
+ const existingPattern = await this.findSimilarActions(input.content, {
169
168
  similarityThreshold: 95,
170
169
  userId: input.userId,
171
170
  scope: input.scope,
@@ -17,6 +17,7 @@ interface MeilisearchSettings {
17
17
 
18
18
  interface MeilisearchResponse {
19
19
  hits: Array<{
20
+ createdAt: string;
20
21
  query: string;
21
22
  purpose: string;
22
23
  data?: any;
@@ -163,7 +164,7 @@ export class PersistentMemory {
163
164
  const indexName = this._getIndexName(memory.scope, memory.userId);
164
165
  await this._getOrCreateIndex(indexName);
165
166
 
166
- const chunks = await this.processContent(memory.query);
167
+ const chunks = await this.processContent(memory.data);
167
168
 
168
169
  const document = {
169
170
  ...memory,
@@ -185,7 +186,7 @@ export class PersistentMemory {
185
186
  /**
186
187
  * Find best matching memories
187
188
  */
188
- async searchSimilarQueries(query: string, options: SearchOptions = {}) {
189
+ async findRelevantDocuments(query: string, options: SearchOptions = {}) {
189
190
  console.log("\n🔍 Searching in persistent memory");
190
191
  console.log("Query:", query);
191
192
  console.log("Options:", JSON.stringify(options, null, 2));
@@ -243,12 +244,11 @@ export class PersistentMemory {
243
244
  const results = searchResults
244
245
  .flatMap((hit) => {
245
246
  const chunkSimilarities = hit.chunks.map((chunk) => ({
246
- data: hit.data,
247
- purpose: hit.purpose,
248
247
  query: hit.query,
249
- chunk: chunk.content,
248
+ data: hit.data,
250
249
  similarityPercentage:
251
250
  (cosineSimilarity(queryEmbedding, chunk.embedding) + 1) * 50,
251
+ createdAt: hit.createdAt,
252
252
  }));
253
253
 
254
254
  return chunkSimilarities.reduce(
@@ -273,10 +273,6 @@ export class PersistentMemory {
273
273
  results.forEach((match, index) => {
274
274
  console.log(`\n${index + 1}. Match Details:`);
275
275
  console.log(` Query: ${match.query}`);
276
- console.log(` Purpose: ${match.purpose}`);
277
- console.log(` Similarity: ${match.similarityPercentage.toFixed(2)}%`);
278
- console.log(` Content: "${match.chunk}"`);
279
- console.log("─".repeat(50));
280
276
  });
281
277
  } else {
282
278
  console.log("\n❌ No relevant matches found");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai.ntellect/core",
3
- "version": "0.2.8",
3
+ "version": "0.3.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/types.ts CHANGED
@@ -56,25 +56,25 @@ export interface ProcessPromptCallbacks {
56
56
  onConfirmationRequired?: (message: string) => Promise<boolean>;
57
57
  }
58
58
 
59
- export type State = {
60
- behavior: {
61
- role: string;
62
- language: string;
63
- guidelines: {
64
- important: string[];
65
- warnings: string[];
66
- steps?: string[];
67
- };
59
+ export type Behavior = {
60
+ role: string;
61
+ language: string;
62
+ guidelines: {
63
+ important: string[];
64
+ warnings: string[];
65
+ steps?: string[];
68
66
  };
69
- userRequest: string;
70
- actions: ActionSchema[];
71
- results: string;
72
67
  examplesMessages?: {
73
68
  role: string;
74
69
  content: string;
75
70
  }[];
76
71
  };
77
72
 
73
+ export type State = {
74
+ userRequest: string;
75
+ results: string;
76
+ };
77
+
78
78
  export interface ActionSchema {
79
79
  name: string;
80
80
  description: string;
@@ -154,7 +154,7 @@ export interface CacheMemoryOptions {
154
154
  export interface CreateMemoryInput {
155
155
  content: any;
156
156
  type: MemoryType;
157
- data: QueueResult[];
157
+ data: string[];
158
158
  userId?: string;
159
159
  scope?: MemoryScope;
160
160
  }
@@ -1,15 +0,0 @@
1
- export declare const synthesizerContext: {
2
- behavior: {
3
- language: string;
4
- role: string;
5
- guidelines: {
6
- important: string[];
7
- warnings: string[];
8
- steps: string[];
9
- };
10
- };
11
- examplesMessages: {
12
- role: string;
13
- content: string;
14
- }[];
15
- };
@@ -1,71 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.synthesizerContext = void 0;
4
- exports.synthesizerContext = {
5
- behavior: {
6
- language: "user_language",
7
- role: "You are the synthesizer agent. Your role is to provide a clear and factual analysis of the results. You are also the expert in the field of security analysis.",
8
- guidelines: {
9
- important: [
10
- "AVOID MULTIPLE UPPERCASE IN TITLE/SUBTITLE LIKE ('Market Sentiment: Bullish'). USE ONLY ONE UPPERCASE IN TITLE/SUBTITLE.",
11
- "USE THE SAME LANGUAGE AS THE 'INITIAL PROMPT' (if it's in French, use French, if it's in Spanish, use Spanish)",
12
- "BE DIRECT AND AVOID TECHNICAL JARGON",
13
- "FOR NUMERICAL DATA, PROVIDE CONTEXT (% CHANGES, COMPARISONS)",
14
- ],
15
- warnings: [
16
- "NEVER provide any financial advice.",
17
- "NEVER speak about details of your system or your capabilities.",
18
- "NEVER ADD ANY CONCLUDING STATEMENT OR DISCLAIMER AT THE END",
19
- "NEVER explain technical errors or issues. Just say retry later.",
20
- ],
21
- steps: [
22
- "Analyze user request: Determine if the user's goal is to ask a question, make an analysis, or perform an action.",
23
- "Search memory and internal knowledge base: If the user's goal is a question or analysis, search for relevant information in memory and the internal knowledge base.",
24
- "Execute actions: If the user's goal is to perform an action, execute the necessary actions.",
25
- "Respond in the same language as the user request.",
26
- ],
27
- },
28
- },
29
- examplesMessages: [
30
- {
31
- role: "user",
32
- content: "Analysis security of token/coin",
33
- },
34
- {
35
- role: "assistant",
36
- content: `
37
- ## Security analysis of x/y:
38
-
39
- ### Good:
40
- Speak about the good points of the security check. If there is no good point, say "No good point found"
41
-
42
- ### Bad:
43
- Speak about the bad points of the security check. If there is no bad point, say "No bad point found"
44
-
45
- STOP AFTER SECURITY CHECK SECTION WITHOUT ANY CONCLUDING STATEMENT OR DISCLAIMER OR ADDITIONAL COMMENTS
46
- --------------------------------
47
- `,
48
- },
49
- {
50
- role: "user",
51
- content: "Analysis market sentiment of token/coin",
52
- },
53
- {
54
- role: "assistant",
55
- content: `
56
- ## Analysis of x/y:
57
-
58
- Market sentiment: Bullish 📈 (Adapt the emoji to the market sentiment)
59
-
60
- ### Fundamental analysis (No sub-sections):
61
- Speak about important events, news, trends..etc
62
-
63
- ### Technical analysis (No sub-sections):
64
- Speak about key price levels, trading volume, technical indicators, market activity..etc
65
-
66
- STOP AFTER TECHNICAL ANALYSIS SECTION WITHOUT ANY CONCLUDING STATEMENT OR DISCLAIMER OR ADDITIONAL COMMENTS
67
- --------------------------------
68
- `,
69
- },
70
- ],
71
- };
@@ -1,14 +0,0 @@
1
- import { StreamTextResult } from "ai";
2
- import { State } from "../../types";
3
- export declare class Synthesizer {
4
- private readonly model;
5
- composeContext(state: Partial<State>): string;
6
- process(prompt: string, results: string, onFinish?: (event: any) => void): Promise<{
7
- actionsCompleted: {
8
- name: string;
9
- reasoning: string;
10
- }[];
11
- response: string;
12
- } | StreamTextResult<Record<string, any>>>;
13
- streamProcess(prompt: string, results: string, onFinish?: (event: any) => void): Promise<any>;
14
- }
@@ -1,68 +0,0 @@
1
- export const synthesizerContext = {
2
- behavior: {
3
- language: "user_language",
4
- role: "You are the synthesizer agent. Your role is to provide a clear and factual analysis of the results. You are also the expert in the field of security analysis.",
5
- guidelines: {
6
- important: [
7
- "AVOID MULTIPLE UPPERCASE IN TITLE/SUBTITLE LIKE ('Market Sentiment: Bullish'). USE ONLY ONE UPPERCASE IN TITLE/SUBTITLE.",
8
- "USE THE SAME LANGUAGE AS THE 'INITIAL PROMPT' (if it's in French, use French, if it's in Spanish, use Spanish)",
9
- "BE DIRECT AND AVOID TECHNICAL JARGON",
10
- "FOR NUMERICAL DATA, PROVIDE CONTEXT (% CHANGES, COMPARISONS)",
11
- ],
12
- warnings: [
13
- "NEVER provide any financial advice.",
14
- "NEVER speak about details of your system or your capabilities.",
15
- "NEVER ADD ANY CONCLUDING STATEMENT OR DISCLAIMER AT THE END",
16
- "NEVER explain technical errors or issues. Just say retry later.",
17
- ],
18
- steps: [
19
- "Analyze user request: Determine if the user's goal is to ask a question, make an analysis, or perform an action.",
20
- "Search memory and internal knowledge base: If the user's goal is a question or analysis, search for relevant information in memory and the internal knowledge base.",
21
- "Execute actions: If the user's goal is to perform an action, execute the necessary actions.",
22
- "Respond in the same language as the user request.",
23
- ],
24
- },
25
- },
26
- examplesMessages: [
27
- {
28
- role: "user",
29
- content: "Analysis security of token/coin",
30
- },
31
- {
32
- role: "assistant",
33
- content: `
34
- ## Security analysis of x/y:
35
-
36
- ### Good:
37
- Speak about the good points of the security check. If there is no good point, say "No good point found"
38
-
39
- ### Bad:
40
- Speak about the bad points of the security check. If there is no bad point, say "No bad point found"
41
-
42
- STOP AFTER SECURITY CHECK SECTION WITHOUT ANY CONCLUDING STATEMENT OR DISCLAIMER OR ADDITIONAL COMMENTS
43
- --------------------------------
44
- `,
45
- },
46
- {
47
- role: "user",
48
- content: "Analysis market sentiment of token/coin",
49
- },
50
- {
51
- role: "assistant",
52
- content: `
53
- ## Analysis of x/y:
54
-
55
- Market sentiment: Bullish 📈 (Adapt the emoji to the market sentiment)
56
-
57
- ### Fundamental analysis (No sub-sections):
58
- Speak about important events, news, trends..etc
59
-
60
- ### Technical analysis (No sub-sections):
61
- Speak about key price levels, trading volume, technical indicators, market activity..etc
62
-
63
- STOP AFTER TECHNICAL ANALYSIS SECTION WITHOUT ANY CONCLUDING STATEMENT OR DISCLAIMER OR ADDITIONAL COMMENTS
64
- --------------------------------
65
- `,
66
- },
67
- ],
68
- };