Skip to content

Commit 43fdc5e

Browse files
committed
enhanced Electron support
fix wrong display of vault search directory SDK : adding the possibility to enable and disable planner mode
1 parent dcabf4c commit 43fdc5e

File tree

7 files changed

+134
-27
lines changed

7 files changed

+134
-27
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@smythos/sre",
3-
"version": "1.6.13",
3+
"version": "1.6.14",
44
"description": "Smyth Runtime Environment",
55
"author": "Alaa-eddine KADDOURI",
66
"license": "MIT",

packages/core/src/helpers/Sysconfig.helper.ts

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,23 @@ export function findSmythPath(_path: string = '', callback?: (smythDir: string,
3131
return envDir;
3232
}
3333

34+
const isElectron = !!process.versions.electron;
35+
let execPath = '';
36+
if (isElectron) {
37+
// In packaged Electron apps, use resourcesPath if available
38+
if ((process as any).resourcesPath) {
39+
// process.resourcesPath points to the 'resources' folder in packaged app
40+
// Go up one level to get the app directory
41+
execPath = path.dirname((process as any).resourcesPath);
42+
} else {
43+
// Development mode or fallback
44+
execPath = path.dirname(process.execPath);
45+
}
46+
} else {
47+
execPath = process.cwd();
48+
}
3449
// 1. Try to find in local directory (the directory from which the program was run)
35-
const localDir = path.resolve(process.cwd(), '.smyth', _path);
50+
const localDir = path.resolve(execPath, '.smyth', _path);
3651
searchDirectories.push(localDir);
3752

3853
// 2. Try to find from main script's directory (entry point)
@@ -61,10 +76,11 @@ export function findSmythPath(_path: string = '', callback?: (smythDir: string,
6176
const homeDir = path.resolve(os.homedir(), '.smyth', _path);
6277
searchDirectories.push(homeDir);
6378

79+
const deduplicatedSearchDirectories = Array.from(new Set(searchDirectories));
6480
//check if any of the directories exist
65-
for (let i = 0; i < searchDirectories.length; i++) {
66-
const dir = searchDirectories[i];
67-
const nextDir = searchDirectories[i + 1];
81+
for (let i = 0; i < deduplicatedSearchDirectories.length; i++) {
82+
const dir = deduplicatedSearchDirectories[i];
83+
const nextDir = deduplicatedSearchDirectories[i + 1];
6884
if (!fs.existsSync(dir)) {
6985
callback?.(dir, false, nextDir);
7086
continue;
@@ -82,7 +98,8 @@ export function findSmythPath(_path: string = '', callback?: (smythDir: string,
8298

8399
export function findValidResourcePath(listOfLocations: string[], callback?: (dir: string, success?: boolean, nextDir?: string) => void) {
84100
let found = '';
85-
for (let location of listOfLocations) {
101+
const deduplicatedLocations = Array.from(new Set(listOfLocations));
102+
for (let location of deduplicatedLocations) {
86103
findSmythPath(location, (dir, success, nextDir) => {
87104
callback?.(dir, success, nextDir);
88105
if (success) {
@@ -93,16 +110,35 @@ export function findValidResourcePath(listOfLocations: string[], callback?: (dir
93110
}
94111
return found;
95112
}
96-
function findPackageRoot(startDir = process.cwd()) {
97-
let currentDir = startDir;
98-
99-
while (currentDir !== path.dirname(currentDir)) {
100-
const packageJsonPath = path.resolve(currentDir, 'package.json');
101-
if (fs.existsSync(packageJsonPath)) {
102-
return currentDir;
113+
function findPackageRoot(startDir?) {
114+
try {
115+
if (!startDir) {
116+
const isElectron = !!process.versions.electron;
117+
let execPath = '';
118+
if (isElectron) {
119+
// In packaged Electron apps, use resourcesPath if available
120+
if ((process as any).resourcesPath) {
121+
// process.resourcesPath points to the 'resources' folder in packaged app
122+
// Go up one level to get the app directory
123+
execPath = path.dirname((process as any).resourcesPath);
124+
} else {
125+
// Development mode or fallback
126+
execPath = path.dirname(process.execPath);
127+
}
128+
} else {
129+
execPath = process.cwd();
130+
}
131+
startDir = execPath;
103132
}
104-
currentDir = path.dirname(currentDir);
105-
}
133+
let currentDir = startDir;
106134

135+
while (currentDir !== path.dirname(currentDir)) {
136+
const packageJsonPath = path.resolve(currentDir, 'package.json');
137+
if (fs.existsSync(packageJsonPath)) {
138+
return currentDir;
139+
}
140+
currentDir = path.dirname(currentDir);
141+
}
142+
} catch (error) {}
107143
return null;
108144
}

packages/core/src/subsystems/Security/Vault.service/connectors/JSONFileVault.class.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ export class JSONFileVault extends VaultConnector {
5151
const relativeSearchLocations = ['vault.json', 'vault/vault.json', '.sre/vault.json'];
5252
found = findValidResourcePath(relativeSearchLocations, (dir, success, nextDir) => {
5353
if (!success) {
54-
logger.warn('Vault file not found in:', nextDir);
54+
logger.warn('Vault file not found in:', dir);
5555
}
5656
});
5757

5858
if (found) {
59-
logger.warn('Using alternative vault file found in : ', found);
59+
logger.warn('Found a Vault file in : ', found, ' I will use this one.');
6060
return found;
6161
}
6262

packages/sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@smythos/sdk",
3-
"version": "1.2.7",
3+
"version": "1.3.0",
44
"description": "SRE SDK",
55
"keywords": [
66
"smythos",

packages/sdk/src/Agent/Agent.class.ts

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ export class Agent extends SDKObject {
294294
connections: [],
295295
};
296296

297+
private _modes: TAgentMode[] = [];
298+
297299
public get behavior() {
298300
return this._data.behavior;
299301
}
@@ -302,6 +304,10 @@ export class Agent extends SDKObject {
302304
this._data.behavior = behavior;
303305
}
304306

307+
public get modes() {
308+
return this._modes;
309+
}
310+
305311
/**
306312
* The agent internal structure
307313
* used for by internal operations to generate the agent data
@@ -394,7 +400,7 @@ export class Agent extends SDKObject {
394400
DummyAccountHelper.addAgentToTeam(this._data.id, this._data.teamId);
395401

396402
if (mode) {
397-
this.addMode(mode);
403+
this.setMode(mode);
398404
}
399405
}
400406

@@ -676,6 +682,25 @@ export class Agent extends SDKObject {
676682
return component;
677683
}
678684

685+
/**
686+
* Remove a skill from the agent.
687+
*
688+
* @param skillName - The name of the skill to remove
689+
* @returns The removed skill
690+
*
691+
* @example
692+
* ```typescript
693+
* agent.removeSkill('fetch_weather');
694+
* ```
695+
*/
696+
removeSkill(skillName: string) {
697+
const component = this.structure.components.find((c) => c.data.endpoint === skillName);
698+
if (component) {
699+
this.structure.components = this.structure.components.filter((c) => c.data.endpoint !== skillName);
700+
this.data.components = this.data.components.filter((c) => c.data.endpoint !== skillName);
701+
}
702+
}
703+
679704
async call(skillName: string, ...args: (Record<string, any> | any)[]) {
680705
try {
681706
const _agentData = this.data;
@@ -804,7 +829,7 @@ export class Agent extends SDKObject {
804829
if (!chatOptions.model) {
805830
chatOptions.model = this._data.defaultModel;
806831
}
807-
return new Chat(chatOptions, this.data, {
832+
return new Chat(chatOptions, this, {
808833
agentId: this._data.id,
809834
baseUrl: chatOptions.baseUrl,
810835
});
@@ -845,15 +870,33 @@ export class Agent extends SDKObject {
845870
* @example
846871
* ```typescript
847872
* agent.setMode(TAgentMode.PLANNER);
848-
* //or
849-
* agent.addMode('planner');
850873
* ```
851874
*
852875
* @param mode - The mode to apply, currently only "planner" is supported
853876
*/
854-
public addMode(mode: TAgentMode) {
877+
public setMode(mode: TAgentMode) {
855878
if (typeof AgentMode?.[mode]?.apply === 'function') {
856879
AgentMode[mode].apply(this);
880+
this._modes.push(mode);
881+
} else {
882+
console.warn(`Mode ${mode} is not a valid mode, skipping...`);
883+
}
884+
}
885+
886+
/**
887+
* Remove the operational mode of the agent.
888+
*
889+
* @example
890+
* ```typescript
891+
* agent.unsetMode(TAgentMode.PLANNER);
892+
* ```
893+
*
894+
* @param mode - The mode to remove
895+
*/
896+
public unsetMode(mode: TAgentMode) {
897+
if (typeof AgentMode?.[mode]?.remove === 'function') {
898+
AgentMode[mode].remove(this);
899+
this._modes = this._modes.filter((m) => m !== mode);
857900
} else {
858901
console.warn(`Mode ${mode} is not a valid mode, skipping...`);
859902
}

packages/sdk/src/Agent/mode/Planner.mode.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,13 @@ export default class PlannerMode {
223223
// },
224224
// });
225225
}
226+
227+
static remove(agent: Agent) {
228+
agent.removeSkill('_sre_AddTasks');
229+
agent.removeSkill('_sre_AddSubTasks');
230+
agent.removeSkill('_sre_UpdateTasks');
231+
agent.removeSkill('_sre_TasksCompleted');
232+
agent.removeSkill('_sre_clearTasks');
233+
agent.behavior = agent.behavior.replace(_planner_prompt, '');
234+
}
226235
}

packages/sdk/src/LLM/Chat.class.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AgentData, ChatOptions, PromptOptions } from '../types/SDKTypes';
55
import { SDKObject } from '../Core/SDKObject.class';
66
import { StorageInstance } from '../Storage/StorageInstance.class';
77
import { SDKLog } from '../utils/console.utils';
8-
8+
import type { Agent, TAgentMode } from '../Agent/Agent.class';
99
const console = SDKLog;
1010

1111
class LocalChatStore extends SDKObject implements ILLMContextStore {
@@ -156,12 +156,13 @@ class ChatCommand {
156156
export class Chat extends SDKObject {
157157
private _id: string;
158158
public _conversation: Conversation;
159+
private _curAgentModes: string = '';
159160

160161
public get id() {
161162
return this._id;
162163
}
163164

164-
private _data: any = {
165+
private _emptyData: any = {
165166
version: '1.0.0',
166167
name: 'Agent',
167168
behavior: '',
@@ -170,14 +171,17 @@ export class Chat extends SDKObject {
170171
defaultModel: '',
171172
id: uid(),
172173
};
174+
private _data: any = {};
173175
public get agentData() {
174176
return this._data;
175177
}
176-
constructor(options: ChatOptions & { candidate: AccessCandidate }, _data?: any, private _convOptions: any = {}) {
178+
constructor(options: ChatOptions & { candidate: AccessCandidate }, private source?: Agent | Record<string, any>, private _convOptions: any = {}) {
177179
super();
178180

181+
const _data = source?.data || source || {};
182+
179183
const _model = options.model || _data?.defaultModel || '';
180-
this._data = { ...this._data, ..._data, defaultModel: _model };
184+
this._data = { ...this._emptyData, ..._data, defaultModel: _model };
181185

182186
this._id = options.id || uid();
183187
if (options.persist) {
@@ -203,6 +207,9 @@ export class Chat extends SDKObject {
203207
this._convOptions.maxOutputTokens = options.maxOutputTokens;
204208
}
205209

210+
if ((this.source as Agent)?.modes) {
211+
this._curAgentModes = (this.source as Agent).modes.join('|');
212+
}
206213
this._conversation = createConversation(this._data, this._convOptions);
207214
}
208215

@@ -241,6 +248,18 @@ export class Chat extends SDKObject {
241248
* @returns ChatCommand that can be executed or streamed
242249
*/
243250
prompt(prompt: string, options?: PromptOptions) {
251+
if ((this.source as Agent)?.modes) {
252+
const modes = (this.source as Agent).modes.join('|');
253+
if (modes !== this._curAgentModes) {
254+
//agent mode changed we need to recreate the conversation object
255+
const _data = this.source.data || this.source;
256+
const _model = this._data?.defaultModel || '';
257+
this._data = { ...this._emptyData, ..._data, defaultModel: _model };
258+
259+
this._conversation = createConversation(this._data, this._convOptions);
260+
this._curAgentModes = modes;
261+
}
262+
}
244263
return new ChatCommand(prompt, this, options);
245264
}
246265
}

0 commit comments

Comments
 (0)