Skip to content

Commit

Permalink
feat(fjage.js): adding gw.agents and gw.containsAgent API
Browse files Browse the repository at this point in the history
  • Loading branch information
notthetup committed Jan 16, 2024
1 parent b4d3dec commit 937c93f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
24 changes: 24 additions & 0 deletions gateways/js/dist/esm/fjage.js
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,30 @@ class Gateway {
this._update_watch();
}

/**
* Gets a list of all agents in the container.
* @returns {Promise<AgentID[]>} - a promise which returns an array of all agent ids when resolved
*/
async agents() {
let rq = { action: 'agents' };
let rsp = await this._msgTxRx(rq);
if (!rsp || !Array.isArray(rsp.agentIDs)) throw new Error('Unable to get agents');
return rsp.agentIDs.map(aid => new AgentID(aid, false, this));
}

/**
* Check if an agent with a given name exists in the container.
*
* @param {AgentID|String} agentID - the agent id to check
* @returns {Promise<boolean>} - a promise which returns true if the agent exists when resolved
*/
async containsAgent(agentID) {
let rq = { action: 'containsAgent', agentID: agentID instanceof AgentID ? agentID.getName() : agentID };
let rsp = await this._msgTxRx(rq);
if (!rsp) throw new Error('Unable to check if agent exists');
return !!rsp.answer;
}

/**
* Finds an agent that provides a named service. If multiple agents are registered
* to provide a given service, any of the agents' id may be returned.
Expand Down
24 changes: 24 additions & 0 deletions gateways/js/dist/fjage.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions gateways/js/src/fjage.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,30 @@ export class Gateway {
this._update_watch();
}

/**
* Gets a list of all agents in the container.
* @returns {Promise<AgentID[]>} - a promise which returns an array of all agent ids when resolved
*/
async agents() {
let rq = { action: 'agents' };
let rsp = await this._msgTxRx(rq);
if (!rsp || !Array.isArray(rsp.agentIDs)) throw new Error('Unable to get agents');
return rsp.agentIDs.map(aid => new AgentID(aid, false, this));
}

/**
* Check if an agent with a given name exists in the container.
*
* @param {AgentID|String} agentID - the agent id to check
* @returns {Promise<boolean>} - a promise which returns true if the agent exists when resolved
*/
async containsAgent(agentID) {
let rq = { action: 'containsAgent', agentID: agentID instanceof AgentID ? agentID.getName() : agentID };
let rsp = await this._msgTxRx(rq);
if (!rsp) throw new Error('Unable to check if agent exists');
return !!rsp.answer;
}

/**
* Finds an agent that provides a named service. If multiple agents are registered
* to provide a given service, any of the agents' id may be returned.
Expand Down
18 changes: 18 additions & 0 deletions gateways/js/test/spec/fjage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,24 @@ describe('An AgentID', function () {
});
});

it('should be able to get all the agents on the platform', async function () {
let val = await gw.agents();
let aids = val.map(a => a.name);
expect(aids).toContain('shell');
expect(aids).toContain('S');
expect(aids).toContain('echo');
expect(aids).toContain('test');
});

it('should be able to check if an agent exists on the container', async function () {
let aid = new AgentID('S', false, gw);
let val = await gw.containsAgent(aid);
expect(val).toEqual(true);
aid = new AgentID('T', false, gw);
val = await gw.containsAgent(aid);
expect(val).toEqual(false);
});

});

describe('An AgentID setup to reject promises', function () {
Expand Down

0 comments on commit 937c93f

Please sign in to comment.