@ai.ntellect/core 0.2.7 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. package/README.FR.md +58 -19
  2. package/README.md +40 -17
  3. package/agent/index.ts +64 -36
  4. package/dist/agent/index.d.ts +12 -8
  5. package/dist/agent/index.js +42 -20
  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 +9 -3
  11. package/dist/llm/evaluator/index.js +38 -26
  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 +19 -29
  16. package/dist/llm/orchestrator/context.js +1 -1
  17. package/dist/llm/orchestrator/index.d.ts +9 -5
  18. package/dist/llm/orchestrator/index.js +6 -49
  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 +3 -12
  28. package/llm/evaluator/index.ts +59 -30
  29. package/llm/interpreter/context.ts +89 -0
  30. package/llm/{synthesizer → interpreter}/index.ts +21 -28
  31. package/llm/orchestrator/context.ts +1 -1
  32. package/llm/orchestrator/index.ts +16 -75
  33. package/memory/cache.ts +5 -6
  34. package/memory/persistent.ts +3 -1
  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,13 +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 {
7
- ActionSchema,
8
- MemoryScope,
9
- MemoryScopeType,
10
- QueueResult,
11
- State,
12
- } from "../../types";
6
+ import { ActionSchema, State } from "../../types";
13
7
  import { injectActions } from "../../utils/inject-actions";
14
8
  import { orchestratorContext } from "./context";
15
9
 
@@ -22,14 +16,18 @@ export class Orchestrator {
22
16
  };
23
17
  private id: string;
24
18
 
25
- constructor(
26
- id: string,
27
- tools: ActionSchema[],
19
+ constructor({
20
+ id,
21
+ tools,
22
+ memory,
23
+ }: {
24
+ id: string;
25
+ tools: ActionSchema[];
28
26
  memory: {
29
27
  persistent: PersistentMemory;
30
28
  cache: CacheMemory;
31
- }
32
- ) {
29
+ };
30
+ }) {
33
31
  this.id = id;
34
32
  this.memory = memory;
35
33
  this.tools = [
@@ -46,70 +44,15 @@ export class Orchestrator {
46
44
  await this.memory.persistent.searchSimilarQueries(query, {
47
45
  similarityThreshold: 70,
48
46
  });
49
- return persistentMemories;
50
- },
51
- },
52
- {
53
- name: "search_cache_memory",
54
- description: "Search for relevant information in the cache",
55
- parameters: z.object({
56
- query: z.string(),
57
- }),
58
- execute: async ({ query }: { query: string }) => {
59
- const cacheMemories = await this.memory.cache.findSimilarQueries(
60
- query,
61
- {
62
- similarityThreshold: 70,
63
- maxResults: 1,
64
- userId: this.id,
65
- scope: MemoryScope.GLOBAL,
66
- }
67
- );
68
- return cacheMemories;
69
- },
70
- },
71
- {
72
- name: "save_memory",
73
- description: "Save relevant information in the internal knowledge base",
74
- parameters: z.object({
75
- query: z.string(),
76
- memoryType: z.string(),
77
- data: z.any(),
78
- scope: z.string().default("GLOBAL").describe("GLOBAL or USER"),
79
- userId: z.string(),
80
- whyStored: z.string(),
81
- }),
82
- execute: async ({
83
- query,
84
- purpose,
85
- data,
86
- scope,
87
- userId,
88
- }: {
89
- query: string;
90
- purpose: string;
91
- data: any;
92
- scope: MemoryScopeType;
93
- userId?: string;
94
- }) => {
95
- const memories = await this.memory.persistent.createMemory({
96
- query,
97
- purpose,
98
- data,
99
- scope,
100
- userId,
101
- createdAt: new Date(),
102
- id: crypto.randomUUID(),
103
- });
104
- return memories;
47
+ return `# LONG_TERM_MEMORY: ${JSON.stringify(persistentMemories)}`;
105
48
  },
106
49
  },
107
50
  ];
108
51
  }
109
52
 
110
53
  composeContext(state: State) {
111
- const { behavior, userRequest, actions, results } = state;
112
- const { role, language, guidelines } = behavior;
54
+ const { userRequest, results } = state;
55
+ const { role, language, guidelines } = orchestratorContext.behavior;
113
56
  const { important, warnings } = guidelines;
114
57
 
115
58
  const context = `
@@ -117,8 +60,8 @@ export class Orchestrator {
117
60
  # LANGUAGE: ${language}
118
61
  # IMPORTANT: ${important.join("\n")}
119
62
  # USER_REQUEST: ${userRequest}
120
- # ACTIONS_AVAILABLES: ${injectActions(actions)} (NEVER REPEAT ACTIONS)
121
- # CURRENT_RESULTS: ${results.map((r) => r.result).join(", ")}
63
+ # ACTIONS_AVAILABLES: ${injectActions(this.tools)} (NEVER REPEAT ACTIONS)
64
+ # CURRENT_RESULTS: ${results}
122
65
  `.trim();
123
66
 
124
67
  return context;
@@ -126,7 +69,7 @@ export class Orchestrator {
126
69
 
127
70
  async process(
128
71
  prompt: string,
129
- results: QueueResult[]
72
+ results: string
130
73
  ): Promise<{
131
74
  actions: {
132
75
  name: string;
@@ -139,9 +82,7 @@ export class Orchestrator {
139
82
  answer: string;
140
83
  }> {
141
84
  const state = this.composeContext({
142
- behavior: orchestratorContext.behavior,
143
85
  userRequest: prompt,
144
- actions: this.tools,
145
86
  results: results,
146
87
  });
147
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,
@@ -243,6 +244,7 @@ export class PersistentMemory {
243
244
  const results = searchResults
244
245
  .flatMap((hit) => {
245
246
  const chunkSimilarities = hit.chunks.map((chunk) => ({
247
+ createdAt: hit.createdAt,
246
248
  data: hit.data,
247
249
  purpose: hit.purpose,
248
250
  query: hit.query,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai.ntellect/core",
3
- "version": "0.2.7",
3
+ "version": "0.3.0",
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: QueueResult[];
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 { QueueResult, State } from "../../types";
3
- export declare class Synthesizer {
4
- private readonly model;
5
- composeContext(state: Partial<State>): string;
6
- process(prompt: string, results: QueueResult[], 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: QueueResult[], 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
- };