@smythos/sre 1.7.1 → 1.7.5

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 (48) hide show
  1. package/dist/index.js +33 -31
  2. package/dist/index.js.map +1 -1
  3. package/dist/types/helpers/BinaryInput.helper.d.ts +1 -1
  4. package/dist/types/helpers/LocalCache.helper.d.ts +18 -0
  5. package/dist/types/helpers/TemplateString.helper.d.ts +2 -1
  6. package/dist/types/subsystems/IO/VectorDB.service/VectorDBConnector.d.ts +4 -4
  7. package/dist/types/subsystems/IO/VectorDB.service/connectors/MilvusVectorDB.class.d.ts +2 -2
  8. package/dist/types/subsystems/IO/VectorDB.service/connectors/PineconeVectorDB.class.d.ts +2 -2
  9. package/dist/types/subsystems/IO/VectorDB.service/connectors/RAMVecrtorDB.class.d.ts +2 -2
  10. package/dist/types/subsystems/IO/VectorDB.service/embed/BaseEmbedding.d.ts +16 -9
  11. package/dist/types/subsystems/IO/VectorDB.service/embed/index.d.ts +4 -1
  12. package/dist/types/subsystems/LLMManager/LLM.inference.d.ts +36 -2
  13. package/dist/types/types/LLM.types.d.ts +54 -31
  14. package/dist/types/types/VectorDB.types.d.ts +6 -3
  15. package/dist/types/utils/string.utils.d.ts +0 -4
  16. package/package.json +1 -1
  17. package/src/Components/Classifier.class.ts +8 -2
  18. package/src/Components/GenAILLM.class.ts +11 -7
  19. package/src/Components/LLMAssistant.class.ts +12 -3
  20. package/src/Components/ScrapflyWebScrape.class.ts +8 -1
  21. package/src/Components/TavilyWebSearch.class.ts +4 -1
  22. package/src/Core/SmythRuntime.class.ts +13 -2
  23. package/src/helpers/BinaryInput.helper.ts +8 -8
  24. package/src/helpers/Conversation.helper.ts +11 -1
  25. package/src/helpers/LocalCache.helper.ts +18 -0
  26. package/src/helpers/TemplateString.helper.ts +20 -9
  27. package/src/index.ts +208 -208
  28. package/src/index.ts.bak +208 -208
  29. package/src/subsystems/AgentManager/Agent.class.ts +2 -0
  30. package/src/subsystems/AgentManager/AgentData.service/AgentDataConnector.ts +6 -5
  31. package/src/subsystems/AgentManager/AgentLogger.class.ts +1 -1
  32. package/src/subsystems/IO/VectorDB.service/VectorDBConnector.ts +15 -4
  33. package/src/subsystems/IO/VectorDB.service/connectors/MilvusVectorDB.class.ts +31 -10
  34. package/src/subsystems/IO/VectorDB.service/connectors/PineconeVectorDB.class.ts +27 -10
  35. package/src/subsystems/IO/VectorDB.service/connectors/RAMVecrtorDB.class.ts +25 -9
  36. package/src/subsystems/IO/VectorDB.service/embed/BaseEmbedding.ts +182 -12
  37. package/src/subsystems/IO/VectorDB.service/embed/GoogleEmbedding.ts +1 -1
  38. package/src/subsystems/IO/VectorDB.service/embed/OpenAIEmbedding.ts +1 -1
  39. package/src/subsystems/IO/VectorDB.service/embed/index.ts +12 -2
  40. package/src/subsystems/LLMManager/LLM.inference.ts +76 -17
  41. package/src/subsystems/LLMManager/LLM.service/connectors/openai/apiInterfaces/ChatCompletionsApiInterface.ts +3 -2
  42. package/src/subsystems/LLMManager/LLM.service/connectors/openai/apiInterfaces/ResponsesApiInterface.ts +2 -2
  43. package/src/subsystems/LLMManager/ModelsProvider.service/ModelsProviderConnector.ts +2 -2
  44. package/src/subsystems/LLMManager/ModelsProvider.service/connectors/JSONModelsProvider.class.ts +4 -1
  45. package/src/subsystems/MemoryManager/Cache.service/connectors/RedisCache.class.ts +12 -0
  46. package/src/types/LLM.types.ts +66 -38
  47. package/src/types/VectorDB.types.ts +7 -3
  48. package/src/utils/string.utils.ts +193 -191
@@ -9,6 +9,8 @@ import pkg from '../../package.json';
9
9
 
10
10
  const logger = Logger('SRE');
11
11
 
12
+ const SRE_GLOBAL_KEY = Symbol.for('SRE@singleton');
13
+
12
14
  export class SmythRuntime {
13
15
  public started = false;
14
16
 
@@ -84,10 +86,19 @@ export class SmythRuntime {
84
86
  }
85
87
 
86
88
  protected static instance?: SmythRuntime;
89
+ // public static get Instance(): SmythRuntime {
90
+ // if (!SmythRuntime.instance) {
91
+ // SmythRuntime.instance = new SmythRuntime();
92
+ // }
93
+ // return SmythRuntime.instance;
94
+ // }
95
+
87
96
  public static get Instance(): SmythRuntime {
88
- if (!SmythRuntime.instance) {
89
- SmythRuntime.instance = new SmythRuntime();
97
+ if (global[SRE_GLOBAL_KEY]) {
98
+ return global[SRE_GLOBAL_KEY];
90
99
  }
100
+ SmythRuntime.instance = new SmythRuntime();
101
+ global[SRE_GLOBAL_KEY] = SmythRuntime.instance;
91
102
  return SmythRuntime.instance;
92
103
  }
93
104
 
@@ -23,7 +23,7 @@ export class BinaryInput {
23
23
  data: BinaryInput | Buffer | ArrayBuffer | Blob | string | Record<string, any>,
24
24
  private _name?: string,
25
25
  public mimetype?: string,
26
- private candidate?: IAccessCandidate,
26
+ private candidate?: IAccessCandidate
27
27
  ) {
28
28
  if (!_name) _name = uid();
29
29
  this._name = _name;
@@ -97,6 +97,9 @@ export class BinaryInput {
97
97
  this._ready = true;
98
98
  }
99
99
  } else {
100
+ //try to guess the mimetype from the url extension
101
+ const ext = this.url.split('.').pop();
102
+ this.mimetype = mime.getType(ext) || '';
100
103
  this._ready = true;
101
104
  }
102
105
  return;
@@ -205,8 +208,6 @@ export class BinaryInput {
205
208
  this._ready = true;
206
209
  return;
207
210
  }
208
-
209
-
210
211
  }
211
212
 
212
213
  private async getUrlInfo(url) {
@@ -221,9 +222,9 @@ export class BinaryInput {
221
222
  }
222
223
  }
223
224
 
224
- public static from(data, name?: string, mimetype?: string, candidate?: IAccessCandidate) {
225
- if (data instanceof BinaryInput) return data;
226
- return new BinaryInput(data, name, mimetype, candidate);
225
+ public static from(source, name?: string, mimetype?: string, candidate?: IAccessCandidate) {
226
+ if (source instanceof BinaryInput) return source;
227
+ return new BinaryInput(source, name, mimetype, candidate);
227
228
  }
228
229
 
229
230
  public async upload(candidate: IAccessCandidate, ttl?: number) {
@@ -239,8 +240,7 @@ export class BinaryInput {
239
240
  this.url = `smythfs://${teamId}.team/${candidate.id}/_temp/${this._name}`;
240
241
  await SmythFS.Instance.write(this.url, this._source, candidate, undefined, ttl);
241
242
  this._uploading = false;
242
- }
243
- else {
243
+ } else {
244
244
  this._uploading = false;
245
245
  }
246
246
  } catch (error) {
@@ -224,6 +224,7 @@ export class Conversation extends EventEmitter {
224
224
  teamId: instance._teamId,
225
225
  agentId: instance._agentId,
226
226
  model: instance._model,
227
+ agentData: instance.agentData,
227
228
  };
228
229
  })
229
230
  public async prompt(message?: string | any, toolHeaders = {}, concurrentToolCalls = 4, abortSignal?: AbortSignal) {
@@ -250,6 +251,7 @@ export class Conversation extends EventEmitter {
250
251
  teamId: instance._teamId,
251
252
  agentId: instance._agentId,
252
253
  model: instance._model,
254
+ agentData: instance.agentData,
253
255
  };
254
256
  })
255
257
  public async streamPrompt(message?: string | any, toolHeaders = {}, concurrentToolCalls = 4, abortSignal?: AbortSignal) {
@@ -402,6 +404,11 @@ export class Conversation extends EventEmitter {
402
404
  llmMessage.thinkingBlocks = thinkingBlocks;
403
405
  }
404
406
 
407
+ //add tool status for every tool entry
408
+ toolsData.forEach((tool) => {
409
+ tool.status = tool.name ? this._toolStatusMap?.[tool.name] : undefined;
410
+ });
411
+
405
412
  llmMessage.tool_calls = toolsData.map((tool) => {
406
413
  return {
407
414
  id: tool.id,
@@ -678,7 +685,10 @@ export class Conversation extends EventEmitter {
678
685
  const requiresRemoteCall =
679
686
  reqConfig.headers['X-DEBUG'] !== undefined ||
680
687
  reqConfig.headers['X-MONITOR-ID'] !== undefined ||
681
- reqConfig.headers['X-AGENT-REMOTE-CALL'] !== undefined;
688
+ //This is used for cases that requre to inject a default attachment handler
689
+ //TODO : this need to be removed from the conversation helper in the future since default attachment handler is a specific server feature
690
+ //reqConfig.headers['X-AGENT-HAS-ATTACHMENTS'] !== undefined;
691
+ reqConfig.headers['x-conversation-id'] !== undefined;
682
692
 
683
693
  if (canRunLocally && !requiresRemoteCall) {
684
694
  console.log('RUNNING AGENT LOCALLY');
@@ -3,6 +3,13 @@ interface CacheItem<T> {
3
3
  expiry: number;
4
4
  }
5
5
 
6
+ /**
7
+ * A local cache helper used to cache data, functions or object instances
8
+ * This is useful when you need to cache an object and refresh the cache if the object is accessed within a certain time period
9
+ *
10
+ * @template K - The type of the key
11
+ * @template V - The type of the value
12
+ */
6
13
  export class LocalCache<K, V> {
7
14
  private cache: Map<K, V>;
8
15
  private expiryMap: Map<K, number>;
@@ -48,6 +55,12 @@ export class LocalCache<K, V> {
48
55
  timeout.unref(); //unblock the event loop
49
56
  }
50
57
 
58
+ /**
59
+ * Get the value from the cache, and update the TTL if provided
60
+ * @param key - The key to get the value from the cache
61
+ * @param ttlMs - The TTL in milliseconds
62
+ * @returns The value from the cache
63
+ */
51
64
  get(key: K, ttlMs?: number): V | undefined {
52
65
  if (!this.has(key)) {
53
66
  return undefined;
@@ -60,6 +73,11 @@ export class LocalCache<K, V> {
60
73
  return value;
61
74
  }
62
75
 
76
+ /**
77
+ * Check if the key exists in the cache
78
+ * @param key - The key to check if it exists in the cache
79
+ * @returns True if the key exists in the cache, false otherwise
80
+ */
63
81
  has(key: K): boolean {
64
82
  if (!this.cache.has(key)) {
65
83
  return false;
@@ -101,19 +101,30 @@ export class TemplateStringHelper {
101
101
  /**
102
102
  * Parses a template string by replacing the placeholders with the values from the provided data object
103
103
  * unmatched placeholders will be left as is
104
+ * Recursively resolves nested template variables until no more variables are found
104
105
  */
105
- public parse(data: Record<string, string>, regex: TemplateStringMatch = Match.default) {
106
+ public parse(data: Record<string, string>, regex: TemplateStringMatch = Match.default, maxDepth: number = 5) {
106
107
  if (typeof this._current !== 'string' || typeof data !== 'object') return this;
107
- this._current = this._current.replace(regex, (match, token) => {
108
- let val = data?.[token] ?? match; // Use nullish coalescing to preserve falsy values (0, '', false)
109
108
 
110
- //if no exact match, try to parse the token as a JSON expression
111
- if (!data?.[token]) {
112
- val = JSONExpression(data, token) || `{{${token}}}`; //if no match, use the token as is
113
- }
109
+ // Keep parsing until no more template variables are resolved or max depth is reached
110
+ // this is useful for chained template variables : e.g {{defaultVar}} => "text {{nestedVar}} more text" ==> "text value of nestedVar more text"
111
+ for (let i = 0; i < maxDepth; i++) {
112
+ const previous = this._current;
114
113
 
115
- return typeof val === 'object' ? JSON.stringify(val) : escapeJsonField(val);
116
- });
114
+ this._current = this._current.replace(regex, (match, token) => {
115
+ let val = data?.[token] ?? match; // Use nullish coalescing to preserve falsy values (0, '', false)
116
+
117
+ //if no exact match, try to parse the token as a JSON expression
118
+ if (!data?.[token]) {
119
+ val = JSONExpression(data, token) || `{{${token}}}`; //if no match, use the token as is
120
+ }
121
+
122
+ return typeof val === 'object' ? JSON.stringify(val) : escapeJsonField(val);
123
+ });
124
+
125
+ // Break early if no changes were made : we parsed all the template variables
126
+ if (previous === this._current) break;
127
+ }
117
128
 
118
129
  return this;
119
130
  }