agent-swarm-kit 1.0.176 → 1.0.178
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.
- package/build/index.cjs +97 -33
- package/build/index.mjs +97 -33
- package/package.json +1 -1
- package/types.d.ts +46 -11
package/build/index.cjs
CHANGED
|
@@ -4535,6 +4535,10 @@ const WAIT_FOR_OUTPUT_FN = async (self) => {
|
|
|
4535
4535
|
agentName,
|
|
4536
4536
|
output: await agent.waitForOutput(),
|
|
4537
4537
|
}))
|
|
4538
|
+
.concat(self._emitSubject.toPromise().then(async (output) => ({
|
|
4539
|
+
agentName: await self.getAgentName(),
|
|
4540
|
+
output,
|
|
4541
|
+
})))
|
|
4538
4542
|
.concat(self._cancelOutputSubject.toPromise())));
|
|
4539
4543
|
const handleOutput = () => {
|
|
4540
4544
|
getOutput.cancel();
|
|
@@ -4611,6 +4615,13 @@ class ClientSwarm {
|
|
|
4611
4615
|
* @type {AgentName[] | typeof STACK_NEED_FETCH}
|
|
4612
4616
|
*/
|
|
4613
4617
|
this._navigationStack = STACK_NEED_FETCH;
|
|
4618
|
+
/**
|
|
4619
|
+
* Subject for emitting output messages to subscribers, used by emit and connect methods.
|
|
4620
|
+
* Provides an asynchronous stream of validated messages, supporting real-time updates to external connectors.
|
|
4621
|
+
* @type {Subject<string>}
|
|
4622
|
+
* @readonly
|
|
4623
|
+
*/
|
|
4624
|
+
this._emitSubject = new functoolsKit.Subject();
|
|
4614
4625
|
/**
|
|
4615
4626
|
* Subject that emits to cancel output waiting, providing an empty output string and agent name.
|
|
4616
4627
|
* Triggered by cancelOutput to interrupt waitForOutput, ensuring responsive cancellation.
|
|
@@ -4628,6 +4639,30 @@ class ClientSwarm {
|
|
|
4628
4639
|
params,
|
|
4629
4640
|
});
|
|
4630
4641
|
}
|
|
4642
|
+
/**
|
|
4643
|
+
* Emits a message to subscribers via _emitSubject after validating it against the policy (ClientPolicy).
|
|
4644
|
+
* Emits the ban message if validation fails, notifying subscribers and logging via BusService.
|
|
4645
|
+
* Supports SwarmConnectionService by broadcasting session outputs within the swarm.
|
|
4646
|
+
* @param {string} message - The message to emit, typically an agent response or tool output.
|
|
4647
|
+
* @returns {Promise<void>} Resolves when the message (or ban message) is emitted and the event is logged.
|
|
4648
|
+
*/
|
|
4649
|
+
async emit(message) {
|
|
4650
|
+
GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
|
|
4651
|
+
this.params.logger.debug(`ClientSwarm swarmName=${this.params.swarmName} clientId=${this.params.clientId} emit`);
|
|
4652
|
+
await this._emitSubject.next(message);
|
|
4653
|
+
await this.params.bus.emit(this.params.clientId, {
|
|
4654
|
+
type: "emit",
|
|
4655
|
+
source: "session-bus",
|
|
4656
|
+
input: {
|
|
4657
|
+
message,
|
|
4658
|
+
},
|
|
4659
|
+
output: {},
|
|
4660
|
+
context: {
|
|
4661
|
+
swarmName: this.params.swarmName,
|
|
4662
|
+
},
|
|
4663
|
+
clientId: this.params.clientId,
|
|
4664
|
+
});
|
|
4665
|
+
}
|
|
4631
4666
|
/**
|
|
4632
4667
|
* Pops the most recent agent from the navigation stack, falling back to the default agent if empty.
|
|
4633
4668
|
* Updates and persists the stack via params.setNavigationStack, supporting ClientSession’s agent navigation.
|
|
@@ -4861,6 +4896,18 @@ class SwarmConnectionService {
|
|
|
4861
4896
|
callbacks,
|
|
4862
4897
|
});
|
|
4863
4898
|
});
|
|
4899
|
+
/**
|
|
4900
|
+
* Emits a message to the session, typically for asynchronous communication.
|
|
4901
|
+
* Delegates to ClientSession.emit, using context from MethodContextService to identify the session, logging via LoggerService if GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO is true.
|
|
4902
|
+
* Mirrors SessionPublicService’s emit, supporting ClientAgent’s output handling and SwarmPublicService’s messaging.
|
|
4903
|
+
* @param {string} content - The content to emit to the session.
|
|
4904
|
+
* @returns {Promise<void>} A promise resolving when the message is emitted.
|
|
4905
|
+
*/
|
|
4906
|
+
this.emit = async (message) => {
|
|
4907
|
+
GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO &&
|
|
4908
|
+
this.loggerService.info(`swarmConnectionService emit`, { message });
|
|
4909
|
+
return await this.getSwarm(this.methodContextService.context.clientId, this.methodContextService.context.swarmName).emit(message);
|
|
4910
|
+
};
|
|
4864
4911
|
/**
|
|
4865
4912
|
* Pops the navigation stack or returns the default agent if the stack is empty.
|
|
4866
4913
|
* Delegates to ClientSwarm.navigationPop, using context from MethodContextService to identify the swarm, logging via LoggerService if GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO is true.
|
|
@@ -5150,13 +5197,6 @@ class ClientSession {
|
|
|
5150
5197
|
*/
|
|
5151
5198
|
constructor(params) {
|
|
5152
5199
|
this.params = params;
|
|
5153
|
-
/**
|
|
5154
|
-
* Subject for emitting output messages to subscribers, used by emit and connect methods.
|
|
5155
|
-
* Provides an asynchronous stream of validated messages, supporting real-time updates to external connectors.
|
|
5156
|
-
* @type {Subject<string>}
|
|
5157
|
-
* @readonly
|
|
5158
|
-
*/
|
|
5159
|
-
this._emitSubject = new functoolsKit.Subject();
|
|
5160
5200
|
GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
|
|
5161
5201
|
this.params.logger.debug(`ClientSession clientId=${this.params.clientId} CTOR`, {
|
|
5162
5202
|
params,
|
|
@@ -5164,7 +5204,7 @@ class ClientSession {
|
|
|
5164
5204
|
this.params.onInit && this.params.onInit(params.clientId, params.swarmName);
|
|
5165
5205
|
}
|
|
5166
5206
|
/**
|
|
5167
|
-
* Emits a message to subscribers via _emitSubject after validating it against the policy (ClientPolicy).
|
|
5207
|
+
* Emits a message to subscribers via swarm _emitSubject after validating it against the policy (ClientPolicy).
|
|
5168
5208
|
* Emits the ban message if validation fails, notifying subscribers and logging via BusService.
|
|
5169
5209
|
* Supports SwarmConnectionService by broadcasting session outputs within the swarm.
|
|
5170
5210
|
* @param {string} message - The message to emit, typically an agent response or tool output.
|
|
@@ -5180,12 +5220,12 @@ class ClientSession {
|
|
|
5180
5220
|
this.params.logger.debug(`ClientSession clientId=${this.params.clientId} emit method canceled due to the banhammer of a client`, {
|
|
5181
5221
|
message,
|
|
5182
5222
|
});
|
|
5183
|
-
await this.
|
|
5223
|
+
await this.params.swarm.emit(await this.params.policy.getBanMessage(this.params.clientId, this.params.swarmName));
|
|
5184
5224
|
return;
|
|
5185
5225
|
}
|
|
5186
5226
|
this.params.onEmit &&
|
|
5187
5227
|
this.params.onEmit(this.params.clientId, this.params.swarmName, message);
|
|
5188
|
-
await this.
|
|
5228
|
+
await this.params.swarm.emit(message);
|
|
5189
5229
|
await this.params.bus.emit(this.params.clientId, {
|
|
5190
5230
|
type: "emit",
|
|
5191
5231
|
source: "session-bus",
|
|
@@ -5452,11 +5492,6 @@ class ClientSession {
|
|
|
5452
5492
|
this.params.logger.debug(`ClientSession clientId=${this.params.clientId} connect`);
|
|
5453
5493
|
this.params.onConnect &&
|
|
5454
5494
|
this.params.onConnect(this.params.clientId, this.params.swarmName);
|
|
5455
|
-
this._emitSubject.subscribe(async (data) => await connector({
|
|
5456
|
-
data,
|
|
5457
|
-
agentName: await this.params.swarm.getAgentName(),
|
|
5458
|
-
clientId: this.params.clientId,
|
|
5459
|
-
}));
|
|
5460
5495
|
this.params.bus.emit(this.params.clientId, {
|
|
5461
5496
|
type: "connect",
|
|
5462
5497
|
source: "session-bus",
|
|
@@ -6980,6 +7015,36 @@ class SwarmPublicService {
|
|
|
6980
7015
|
* @private
|
|
6981
7016
|
*/
|
|
6982
7017
|
this.swarmConnectionService = inject(TYPES.swarmConnectionService);
|
|
7018
|
+
/**
|
|
7019
|
+
* Emits a message to the session for a specific client and swarm.
|
|
7020
|
+
* Wraps SessionConnectionService.emit with MethodContextService for scoping, logging via LoggerService if GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO is true.
|
|
7021
|
+
* Used in ClientAgent (e.g., session-level messaging) and AgentPublicService (e.g., swarm context emission).
|
|
7022
|
+
* @param {string} content - The message content to emit to the session.
|
|
7023
|
+
* @param {string} methodName - The name of the method invoking the operation, logged and scoped in context.
|
|
7024
|
+
* @param {string} clientId - The client ID, tying to ClientAgent sessions and PerfService tracking.
|
|
7025
|
+
* @param {SwarmName} swarmName - The swarm name, sourced from Swarm.interface, used in SwarmMetaService context.
|
|
7026
|
+
* @returns {Promise<void>} A promise resolving when the message is emitted.
|
|
7027
|
+
*/
|
|
7028
|
+
this.emit = async (content, methodName, clientId, swarmName) => {
|
|
7029
|
+
GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO &&
|
|
7030
|
+
this.loggerService.info("swarmPublicService emit", {
|
|
7031
|
+
content,
|
|
7032
|
+
clientId,
|
|
7033
|
+
methodName,
|
|
7034
|
+
swarmName,
|
|
7035
|
+
});
|
|
7036
|
+
return await MethodContextService.runInContext(async () => {
|
|
7037
|
+
return await this.swarmConnectionService.emit(content);
|
|
7038
|
+
}, {
|
|
7039
|
+
methodName,
|
|
7040
|
+
clientId,
|
|
7041
|
+
swarmName,
|
|
7042
|
+
policyName: "",
|
|
7043
|
+
agentName: "",
|
|
7044
|
+
storageName: "",
|
|
7045
|
+
stateName: "",
|
|
7046
|
+
});
|
|
7047
|
+
};
|
|
6983
7048
|
/**
|
|
6984
7049
|
* Pops the navigation stack or returns the default agent for the swarm, scoped to a client.
|
|
6985
7050
|
* Wraps SwarmConnectionService.navigationPop with MethodContextService for scoping, logging via LoggerService if GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO is true.
|
|
@@ -13979,11 +14044,6 @@ const emitForce = beginContext(async (content, clientId) => {
|
|
|
13979
14044
|
content,
|
|
13980
14045
|
clientId,
|
|
13981
14046
|
});
|
|
13982
|
-
// Check if the session mode is "makeConnection"
|
|
13983
|
-
if (swarm$1.sessionValidationService.getSessionMode(clientId) !==
|
|
13984
|
-
"makeConnection") {
|
|
13985
|
-
throw new Error(`agent-swarm-kit emitForce session is not makeConnection clientId=${clientId}`);
|
|
13986
|
-
}
|
|
13987
14047
|
// Validate the session and swarm
|
|
13988
14048
|
swarm$1.sessionValidationService.validate(clientId, METHOD_NAME$q);
|
|
13989
14049
|
const swarmName = swarm$1.sessionValidationService.getSwarm(clientId);
|
|
@@ -14296,11 +14356,6 @@ const emit = beginContext(async (content, clientId, agentName) => {
|
|
|
14296
14356
|
clientId,
|
|
14297
14357
|
agentName,
|
|
14298
14358
|
});
|
|
14299
|
-
// Check if the session mode is "makeConnection"
|
|
14300
|
-
if (swarm$1.sessionValidationService.getSessionMode(clientId) !==
|
|
14301
|
-
"makeConnection") {
|
|
14302
|
-
throw new Error(`agent-swarm-kit emit session is not makeConnection clientId=${clientId}`);
|
|
14303
|
-
}
|
|
14304
14359
|
// Validate the agent, session, and swarm
|
|
14305
14360
|
swarm$1.agentValidationService.validate(agentName, METHOD_NAME$l);
|
|
14306
14361
|
swarm$1.sessionValidationService.validate(clientId, METHOD_NAME$l);
|
|
@@ -14760,7 +14815,7 @@ const METHOD_NAME$g = "function.target.session";
|
|
|
14760
14815
|
* @param {SwarmName} swarmName - The name of the swarm to connect to.
|
|
14761
14816
|
* @returns {Object} An object containing `complete` and `dispose` methods for session management.
|
|
14762
14817
|
*/
|
|
14763
|
-
const sessionInternal = (clientId, swarmName) => {
|
|
14818
|
+
const sessionInternal = (clientId, swarmName, { onDispose = () => { } } = {}) => {
|
|
14764
14819
|
const executionId = functoolsKit.randomString();
|
|
14765
14820
|
// Log the operation details if logging is enabled in GLOBAL_CONFIG
|
|
14766
14821
|
GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG &&
|
|
@@ -14799,7 +14854,10 @@ const sessionInternal = (clientId, swarmName) => {
|
|
|
14799
14854
|
complete: (async (content) => {
|
|
14800
14855
|
return await complete(content);
|
|
14801
14856
|
}),
|
|
14802
|
-
dispose: async () =>
|
|
14857
|
+
dispose: async () => {
|
|
14858
|
+
await disposeConnection(clientId, swarmName, METHOD_NAME$g);
|
|
14859
|
+
await onDispose();
|
|
14860
|
+
},
|
|
14803
14861
|
};
|
|
14804
14862
|
};
|
|
14805
14863
|
/**
|
|
@@ -14818,8 +14876,10 @@ const sessionInternal = (clientId, swarmName) => {
|
|
|
14818
14876
|
* console.log(result); // Outputs the swarm's response
|
|
14819
14877
|
* await dispose();
|
|
14820
14878
|
*/
|
|
14821
|
-
const session = (clientId, swarmName) => {
|
|
14822
|
-
const { complete, dispose } = sessionInternal(clientId, swarmName
|
|
14879
|
+
const session = (clientId, swarmName, { onDispose } = {}) => {
|
|
14880
|
+
const { complete, dispose } = sessionInternal(clientId, swarmName, {
|
|
14881
|
+
onDispose,
|
|
14882
|
+
});
|
|
14823
14883
|
let isMounted = true;
|
|
14824
14884
|
const online = functoolsKit.singleshot(async () => {
|
|
14825
14885
|
await markOnline(clientId, swarmName);
|
|
@@ -14863,8 +14923,10 @@ const session = (clientId, swarmName) => {
|
|
|
14863
14923
|
* console.log(result);
|
|
14864
14924
|
* await dispose();
|
|
14865
14925
|
*/
|
|
14866
|
-
session.scheduled = (clientId, swarmName, { delay = SCHEDULED_DELAY } = {}) => {
|
|
14867
|
-
const { complete, dispose } = sessionInternal(clientId, swarmName
|
|
14926
|
+
session.scheduled = (clientId, swarmName, { delay = SCHEDULED_DELAY, onDispose } = {}) => {
|
|
14927
|
+
const { complete, dispose } = sessionInternal(clientId, swarmName, {
|
|
14928
|
+
onDispose,
|
|
14929
|
+
});
|
|
14868
14930
|
let isMounted = true;
|
|
14869
14931
|
const online = functoolsKit.singleshot(async () => {
|
|
14870
14932
|
await markOnline(clientId, swarmName);
|
|
@@ -14928,8 +14990,10 @@ session.scheduled = (clientId, swarmName, { delay = SCHEDULED_DELAY } = {}) => {
|
|
|
14928
14990
|
* console.log(result);
|
|
14929
14991
|
* await dispose();
|
|
14930
14992
|
*/
|
|
14931
|
-
session.rate = (clientId, swarmName, { delay = SCHEDULED_DELAY } = {}) => {
|
|
14932
|
-
const { complete, dispose } = sessionInternal(clientId, swarmName
|
|
14993
|
+
session.rate = (clientId, swarmName, { delay = SCHEDULED_DELAY, onDispose } = {}) => {
|
|
14994
|
+
const { complete, dispose } = sessionInternal(clientId, swarmName, {
|
|
14995
|
+
onDispose,
|
|
14996
|
+
});
|
|
14933
14997
|
let isMounted = true;
|
|
14934
14998
|
const online = functoolsKit.singleshot(async () => {
|
|
14935
14999
|
await markOnline(clientId, swarmName);
|
package/build/index.mjs
CHANGED
|
@@ -4533,6 +4533,10 @@ const WAIT_FOR_OUTPUT_FN = async (self) => {
|
|
|
4533
4533
|
agentName,
|
|
4534
4534
|
output: await agent.waitForOutput(),
|
|
4535
4535
|
}))
|
|
4536
|
+
.concat(self._emitSubject.toPromise().then(async (output) => ({
|
|
4537
|
+
agentName: await self.getAgentName(),
|
|
4538
|
+
output,
|
|
4539
|
+
})))
|
|
4536
4540
|
.concat(self._cancelOutputSubject.toPromise())));
|
|
4537
4541
|
const handleOutput = () => {
|
|
4538
4542
|
getOutput.cancel();
|
|
@@ -4609,6 +4613,13 @@ class ClientSwarm {
|
|
|
4609
4613
|
* @type {AgentName[] | typeof STACK_NEED_FETCH}
|
|
4610
4614
|
*/
|
|
4611
4615
|
this._navigationStack = STACK_NEED_FETCH;
|
|
4616
|
+
/**
|
|
4617
|
+
* Subject for emitting output messages to subscribers, used by emit and connect methods.
|
|
4618
|
+
* Provides an asynchronous stream of validated messages, supporting real-time updates to external connectors.
|
|
4619
|
+
* @type {Subject<string>}
|
|
4620
|
+
* @readonly
|
|
4621
|
+
*/
|
|
4622
|
+
this._emitSubject = new Subject();
|
|
4612
4623
|
/**
|
|
4613
4624
|
* Subject that emits to cancel output waiting, providing an empty output string and agent name.
|
|
4614
4625
|
* Triggered by cancelOutput to interrupt waitForOutput, ensuring responsive cancellation.
|
|
@@ -4626,6 +4637,30 @@ class ClientSwarm {
|
|
|
4626
4637
|
params,
|
|
4627
4638
|
});
|
|
4628
4639
|
}
|
|
4640
|
+
/**
|
|
4641
|
+
* Emits a message to subscribers via _emitSubject after validating it against the policy (ClientPolicy).
|
|
4642
|
+
* Emits the ban message if validation fails, notifying subscribers and logging via BusService.
|
|
4643
|
+
* Supports SwarmConnectionService by broadcasting session outputs within the swarm.
|
|
4644
|
+
* @param {string} message - The message to emit, typically an agent response or tool output.
|
|
4645
|
+
* @returns {Promise<void>} Resolves when the message (or ban message) is emitted and the event is logged.
|
|
4646
|
+
*/
|
|
4647
|
+
async emit(message) {
|
|
4648
|
+
GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
|
|
4649
|
+
this.params.logger.debug(`ClientSwarm swarmName=${this.params.swarmName} clientId=${this.params.clientId} emit`);
|
|
4650
|
+
await this._emitSubject.next(message);
|
|
4651
|
+
await this.params.bus.emit(this.params.clientId, {
|
|
4652
|
+
type: "emit",
|
|
4653
|
+
source: "session-bus",
|
|
4654
|
+
input: {
|
|
4655
|
+
message,
|
|
4656
|
+
},
|
|
4657
|
+
output: {},
|
|
4658
|
+
context: {
|
|
4659
|
+
swarmName: this.params.swarmName,
|
|
4660
|
+
},
|
|
4661
|
+
clientId: this.params.clientId,
|
|
4662
|
+
});
|
|
4663
|
+
}
|
|
4629
4664
|
/**
|
|
4630
4665
|
* Pops the most recent agent from the navigation stack, falling back to the default agent if empty.
|
|
4631
4666
|
* Updates and persists the stack via params.setNavigationStack, supporting ClientSession’s agent navigation.
|
|
@@ -4859,6 +4894,18 @@ class SwarmConnectionService {
|
|
|
4859
4894
|
callbacks,
|
|
4860
4895
|
});
|
|
4861
4896
|
});
|
|
4897
|
+
/**
|
|
4898
|
+
* Emits a message to the session, typically for asynchronous communication.
|
|
4899
|
+
* Delegates to ClientSession.emit, using context from MethodContextService to identify the session, logging via LoggerService if GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO is true.
|
|
4900
|
+
* Mirrors SessionPublicService’s emit, supporting ClientAgent’s output handling and SwarmPublicService’s messaging.
|
|
4901
|
+
* @param {string} content - The content to emit to the session.
|
|
4902
|
+
* @returns {Promise<void>} A promise resolving when the message is emitted.
|
|
4903
|
+
*/
|
|
4904
|
+
this.emit = async (message) => {
|
|
4905
|
+
GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO &&
|
|
4906
|
+
this.loggerService.info(`swarmConnectionService emit`, { message });
|
|
4907
|
+
return await this.getSwarm(this.methodContextService.context.clientId, this.methodContextService.context.swarmName).emit(message);
|
|
4908
|
+
};
|
|
4862
4909
|
/**
|
|
4863
4910
|
* Pops the navigation stack or returns the default agent if the stack is empty.
|
|
4864
4911
|
* Delegates to ClientSwarm.navigationPop, using context from MethodContextService to identify the swarm, logging via LoggerService if GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO is true.
|
|
@@ -5148,13 +5195,6 @@ class ClientSession {
|
|
|
5148
5195
|
*/
|
|
5149
5196
|
constructor(params) {
|
|
5150
5197
|
this.params = params;
|
|
5151
|
-
/**
|
|
5152
|
-
* Subject for emitting output messages to subscribers, used by emit and connect methods.
|
|
5153
|
-
* Provides an asynchronous stream of validated messages, supporting real-time updates to external connectors.
|
|
5154
|
-
* @type {Subject<string>}
|
|
5155
|
-
* @readonly
|
|
5156
|
-
*/
|
|
5157
|
-
this._emitSubject = new Subject();
|
|
5158
5198
|
GLOBAL_CONFIG.CC_LOGGER_ENABLE_DEBUG &&
|
|
5159
5199
|
this.params.logger.debug(`ClientSession clientId=${this.params.clientId} CTOR`, {
|
|
5160
5200
|
params,
|
|
@@ -5162,7 +5202,7 @@ class ClientSession {
|
|
|
5162
5202
|
this.params.onInit && this.params.onInit(params.clientId, params.swarmName);
|
|
5163
5203
|
}
|
|
5164
5204
|
/**
|
|
5165
|
-
* Emits a message to subscribers via _emitSubject after validating it against the policy (ClientPolicy).
|
|
5205
|
+
* Emits a message to subscribers via swarm _emitSubject after validating it against the policy (ClientPolicy).
|
|
5166
5206
|
* Emits the ban message if validation fails, notifying subscribers and logging via BusService.
|
|
5167
5207
|
* Supports SwarmConnectionService by broadcasting session outputs within the swarm.
|
|
5168
5208
|
* @param {string} message - The message to emit, typically an agent response or tool output.
|
|
@@ -5178,12 +5218,12 @@ class ClientSession {
|
|
|
5178
5218
|
this.params.logger.debug(`ClientSession clientId=${this.params.clientId} emit method canceled due to the banhammer of a client`, {
|
|
5179
5219
|
message,
|
|
5180
5220
|
});
|
|
5181
|
-
await this.
|
|
5221
|
+
await this.params.swarm.emit(await this.params.policy.getBanMessage(this.params.clientId, this.params.swarmName));
|
|
5182
5222
|
return;
|
|
5183
5223
|
}
|
|
5184
5224
|
this.params.onEmit &&
|
|
5185
5225
|
this.params.onEmit(this.params.clientId, this.params.swarmName, message);
|
|
5186
|
-
await this.
|
|
5226
|
+
await this.params.swarm.emit(message);
|
|
5187
5227
|
await this.params.bus.emit(this.params.clientId, {
|
|
5188
5228
|
type: "emit",
|
|
5189
5229
|
source: "session-bus",
|
|
@@ -5450,11 +5490,6 @@ class ClientSession {
|
|
|
5450
5490
|
this.params.logger.debug(`ClientSession clientId=${this.params.clientId} connect`);
|
|
5451
5491
|
this.params.onConnect &&
|
|
5452
5492
|
this.params.onConnect(this.params.clientId, this.params.swarmName);
|
|
5453
|
-
this._emitSubject.subscribe(async (data) => await connector({
|
|
5454
|
-
data,
|
|
5455
|
-
agentName: await this.params.swarm.getAgentName(),
|
|
5456
|
-
clientId: this.params.clientId,
|
|
5457
|
-
}));
|
|
5458
5493
|
this.params.bus.emit(this.params.clientId, {
|
|
5459
5494
|
type: "connect",
|
|
5460
5495
|
source: "session-bus",
|
|
@@ -6978,6 +7013,36 @@ class SwarmPublicService {
|
|
|
6978
7013
|
* @private
|
|
6979
7014
|
*/
|
|
6980
7015
|
this.swarmConnectionService = inject(TYPES.swarmConnectionService);
|
|
7016
|
+
/**
|
|
7017
|
+
* Emits a message to the session for a specific client and swarm.
|
|
7018
|
+
* Wraps SessionConnectionService.emit with MethodContextService for scoping, logging via LoggerService if GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO is true.
|
|
7019
|
+
* Used in ClientAgent (e.g., session-level messaging) and AgentPublicService (e.g., swarm context emission).
|
|
7020
|
+
* @param {string} content - The message content to emit to the session.
|
|
7021
|
+
* @param {string} methodName - The name of the method invoking the operation, logged and scoped in context.
|
|
7022
|
+
* @param {string} clientId - The client ID, tying to ClientAgent sessions and PerfService tracking.
|
|
7023
|
+
* @param {SwarmName} swarmName - The swarm name, sourced from Swarm.interface, used in SwarmMetaService context.
|
|
7024
|
+
* @returns {Promise<void>} A promise resolving when the message is emitted.
|
|
7025
|
+
*/
|
|
7026
|
+
this.emit = async (content, methodName, clientId, swarmName) => {
|
|
7027
|
+
GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO &&
|
|
7028
|
+
this.loggerService.info("swarmPublicService emit", {
|
|
7029
|
+
content,
|
|
7030
|
+
clientId,
|
|
7031
|
+
methodName,
|
|
7032
|
+
swarmName,
|
|
7033
|
+
});
|
|
7034
|
+
return await MethodContextService.runInContext(async () => {
|
|
7035
|
+
return await this.swarmConnectionService.emit(content);
|
|
7036
|
+
}, {
|
|
7037
|
+
methodName,
|
|
7038
|
+
clientId,
|
|
7039
|
+
swarmName,
|
|
7040
|
+
policyName: "",
|
|
7041
|
+
agentName: "",
|
|
7042
|
+
storageName: "",
|
|
7043
|
+
stateName: "",
|
|
7044
|
+
});
|
|
7045
|
+
};
|
|
6981
7046
|
/**
|
|
6982
7047
|
* Pops the navigation stack or returns the default agent for the swarm, scoped to a client.
|
|
6983
7048
|
* Wraps SwarmConnectionService.navigationPop with MethodContextService for scoping, logging via LoggerService if GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO is true.
|
|
@@ -13977,11 +14042,6 @@ const emitForce = beginContext(async (content, clientId) => {
|
|
|
13977
14042
|
content,
|
|
13978
14043
|
clientId,
|
|
13979
14044
|
});
|
|
13980
|
-
// Check if the session mode is "makeConnection"
|
|
13981
|
-
if (swarm$1.sessionValidationService.getSessionMode(clientId) !==
|
|
13982
|
-
"makeConnection") {
|
|
13983
|
-
throw new Error(`agent-swarm-kit emitForce session is not makeConnection clientId=${clientId}`);
|
|
13984
|
-
}
|
|
13985
14045
|
// Validate the session and swarm
|
|
13986
14046
|
swarm$1.sessionValidationService.validate(clientId, METHOD_NAME$q);
|
|
13987
14047
|
const swarmName = swarm$1.sessionValidationService.getSwarm(clientId);
|
|
@@ -14294,11 +14354,6 @@ const emit = beginContext(async (content, clientId, agentName) => {
|
|
|
14294
14354
|
clientId,
|
|
14295
14355
|
agentName,
|
|
14296
14356
|
});
|
|
14297
|
-
// Check if the session mode is "makeConnection"
|
|
14298
|
-
if (swarm$1.sessionValidationService.getSessionMode(clientId) !==
|
|
14299
|
-
"makeConnection") {
|
|
14300
|
-
throw new Error(`agent-swarm-kit emit session is not makeConnection clientId=${clientId}`);
|
|
14301
|
-
}
|
|
14302
14357
|
// Validate the agent, session, and swarm
|
|
14303
14358
|
swarm$1.agentValidationService.validate(agentName, METHOD_NAME$l);
|
|
14304
14359
|
swarm$1.sessionValidationService.validate(clientId, METHOD_NAME$l);
|
|
@@ -14758,7 +14813,7 @@ const METHOD_NAME$g = "function.target.session";
|
|
|
14758
14813
|
* @param {SwarmName} swarmName - The name of the swarm to connect to.
|
|
14759
14814
|
* @returns {Object} An object containing `complete` and `dispose` methods for session management.
|
|
14760
14815
|
*/
|
|
14761
|
-
const sessionInternal = (clientId, swarmName) => {
|
|
14816
|
+
const sessionInternal = (clientId, swarmName, { onDispose = () => { } } = {}) => {
|
|
14762
14817
|
const executionId = randomString();
|
|
14763
14818
|
// Log the operation details if logging is enabled in GLOBAL_CONFIG
|
|
14764
14819
|
GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG &&
|
|
@@ -14797,7 +14852,10 @@ const sessionInternal = (clientId, swarmName) => {
|
|
|
14797
14852
|
complete: (async (content) => {
|
|
14798
14853
|
return await complete(content);
|
|
14799
14854
|
}),
|
|
14800
|
-
dispose: async () =>
|
|
14855
|
+
dispose: async () => {
|
|
14856
|
+
await disposeConnection(clientId, swarmName, METHOD_NAME$g);
|
|
14857
|
+
await onDispose();
|
|
14858
|
+
},
|
|
14801
14859
|
};
|
|
14802
14860
|
};
|
|
14803
14861
|
/**
|
|
@@ -14816,8 +14874,10 @@ const sessionInternal = (clientId, swarmName) => {
|
|
|
14816
14874
|
* console.log(result); // Outputs the swarm's response
|
|
14817
14875
|
* await dispose();
|
|
14818
14876
|
*/
|
|
14819
|
-
const session = (clientId, swarmName) => {
|
|
14820
|
-
const { complete, dispose } = sessionInternal(clientId, swarmName
|
|
14877
|
+
const session = (clientId, swarmName, { onDispose } = {}) => {
|
|
14878
|
+
const { complete, dispose } = sessionInternal(clientId, swarmName, {
|
|
14879
|
+
onDispose,
|
|
14880
|
+
});
|
|
14821
14881
|
let isMounted = true;
|
|
14822
14882
|
const online = singleshot(async () => {
|
|
14823
14883
|
await markOnline(clientId, swarmName);
|
|
@@ -14861,8 +14921,10 @@ const session = (clientId, swarmName) => {
|
|
|
14861
14921
|
* console.log(result);
|
|
14862
14922
|
* await dispose();
|
|
14863
14923
|
*/
|
|
14864
|
-
session.scheduled = (clientId, swarmName, { delay = SCHEDULED_DELAY } = {}) => {
|
|
14865
|
-
const { complete, dispose } = sessionInternal(clientId, swarmName
|
|
14924
|
+
session.scheduled = (clientId, swarmName, { delay = SCHEDULED_DELAY, onDispose } = {}) => {
|
|
14925
|
+
const { complete, dispose } = sessionInternal(clientId, swarmName, {
|
|
14926
|
+
onDispose,
|
|
14927
|
+
});
|
|
14866
14928
|
let isMounted = true;
|
|
14867
14929
|
const online = singleshot(async () => {
|
|
14868
14930
|
await markOnline(clientId, swarmName);
|
|
@@ -14926,8 +14988,10 @@ session.scheduled = (clientId, swarmName, { delay = SCHEDULED_DELAY } = {}) => {
|
|
|
14926
14988
|
* console.log(result);
|
|
14927
14989
|
* await dispose();
|
|
14928
14990
|
*/
|
|
14929
|
-
session.rate = (clientId, swarmName, { delay = SCHEDULED_DELAY } = {}) => {
|
|
14930
|
-
const { complete, dispose } = sessionInternal(clientId, swarmName
|
|
14991
|
+
session.rate = (clientId, swarmName, { delay = SCHEDULED_DELAY, onDispose } = {}) => {
|
|
14992
|
+
const { complete, dispose } = sessionInternal(clientId, swarmName, {
|
|
14993
|
+
onDispose,
|
|
14994
|
+
});
|
|
14931
14995
|
let isMounted = true;
|
|
14932
14996
|
const online = singleshot(async () => {
|
|
14933
14997
|
await markOnline(clientId, swarmName);
|
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -1131,6 +1131,13 @@ interface ISwarm {
|
|
|
1131
1131
|
* @throws {Error} If setting the agent fails (e.g., due to persistence issues or invalid name).
|
|
1132
1132
|
*/
|
|
1133
1133
|
setAgentName(agentName: AgentName): Promise<void>;
|
|
1134
|
+
/**
|
|
1135
|
+
* Emits a message to the session's communication channel.
|
|
1136
|
+
* @param {string} message - The message content to emit.
|
|
1137
|
+
* @returns {Promise<void>} A promise that resolves when the message is successfully emitted.
|
|
1138
|
+
* @throws {Error} If the emission fails due to connection issues or invalid message format.
|
|
1139
|
+
*/
|
|
1140
|
+
emit(message: string): Promise<void>;
|
|
1134
1141
|
}
|
|
1135
1142
|
/**
|
|
1136
1143
|
* Type representing the unique name of a swarm within the system.
|
|
@@ -3894,6 +3901,13 @@ declare class ClientSwarm implements ISwarm {
|
|
|
3894
3901
|
* @type {AgentName[] | typeof STACK_NEED_FETCH}
|
|
3895
3902
|
*/
|
|
3896
3903
|
_navigationStack: AgentName[] | typeof STACK_NEED_FETCH;
|
|
3904
|
+
/**
|
|
3905
|
+
* Subject for emitting output messages to subscribers, used by emit and connect methods.
|
|
3906
|
+
* Provides an asynchronous stream of validated messages, supporting real-time updates to external connectors.
|
|
3907
|
+
* @type {Subject<string>}
|
|
3908
|
+
* @readonly
|
|
3909
|
+
*/
|
|
3910
|
+
readonly _emitSubject: Subject<string>;
|
|
3897
3911
|
/**
|
|
3898
3912
|
* Subject that emits to cancel output waiting, providing an empty output string and agent name.
|
|
3899
3913
|
* Triggered by cancelOutput to interrupt waitForOutput, ensuring responsive cancellation.
|
|
@@ -3915,6 +3929,14 @@ declare class ClientSwarm implements ISwarm {
|
|
|
3915
3929
|
* @param {ISwarmParams} params - The parameters for initializing the swarm, including clientId, swarmName, agentMap, getActiveAgent, etc.
|
|
3916
3930
|
*/
|
|
3917
3931
|
constructor(params: ISwarmParams);
|
|
3932
|
+
/**
|
|
3933
|
+
* Emits a message to subscribers via _emitSubject after validating it against the policy (ClientPolicy).
|
|
3934
|
+
* Emits the ban message if validation fails, notifying subscribers and logging via BusService.
|
|
3935
|
+
* Supports SwarmConnectionService by broadcasting session outputs within the swarm.
|
|
3936
|
+
* @param {string} message - The message to emit, typically an agent response or tool output.
|
|
3937
|
+
* @returns {Promise<void>} Resolves when the message (or ban message) is emitted and the event is logged.
|
|
3938
|
+
*/
|
|
3939
|
+
emit(message: string): Promise<void>;
|
|
3918
3940
|
/**
|
|
3919
3941
|
* Pops the most recent agent from the navigation stack, falling back to the default agent if empty.
|
|
3920
3942
|
* Updates and persists the stack via params.setNavigationStack, supporting ClientSession’s agent navigation.
|
|
@@ -4017,6 +4039,14 @@ declare class SwarmConnectionService implements ISwarm {
|
|
|
4017
4039
|
* @returns {ClientSwarm} The memoized ClientSwarm instance configured for the client and swarm.
|
|
4018
4040
|
*/
|
|
4019
4041
|
getSwarm: ((clientId: string, swarmName: string) => ClientSwarm) & functools_kit.IClearableMemoize<string> & functools_kit.IControlMemoize<string, ClientSwarm>;
|
|
4042
|
+
/**
|
|
4043
|
+
* Emits a message to the session, typically for asynchronous communication.
|
|
4044
|
+
* Delegates to ClientSession.emit, using context from MethodContextService to identify the session, logging via LoggerService if GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO is true.
|
|
4045
|
+
* Mirrors SessionPublicService’s emit, supporting ClientAgent’s output handling and SwarmPublicService’s messaging.
|
|
4046
|
+
* @param {string} content - The content to emit to the session.
|
|
4047
|
+
* @returns {Promise<void>} A promise resolving when the message is emitted.
|
|
4048
|
+
*/
|
|
4049
|
+
emit: (message: string) => Promise<void>;
|
|
4020
4050
|
/**
|
|
4021
4051
|
* Pops the navigation stack or returns the default agent if the stack is empty.
|
|
4022
4052
|
* Delegates to ClientSwarm.navigationPop, using context from MethodContextService to identify the swarm, logging via LoggerService if GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO is true.
|
|
@@ -4196,13 +4226,6 @@ declare class CompletionSchemaService {
|
|
|
4196
4226
|
*/
|
|
4197
4227
|
declare class ClientSession implements ISession {
|
|
4198
4228
|
readonly params: ISessionParams;
|
|
4199
|
-
/**
|
|
4200
|
-
* Subject for emitting output messages to subscribers, used by emit and connect methods.
|
|
4201
|
-
* Provides an asynchronous stream of validated messages, supporting real-time updates to external connectors.
|
|
4202
|
-
* @type {Subject<string>}
|
|
4203
|
-
* @readonly
|
|
4204
|
-
*/
|
|
4205
|
-
readonly _emitSubject: Subject<string>;
|
|
4206
4229
|
/**
|
|
4207
4230
|
* Constructs a new ClientSession instance with the provided parameters.
|
|
4208
4231
|
* Invokes the onInit callback if defined and logs construction if debugging is enabled.
|
|
@@ -4210,7 +4233,7 @@ declare class ClientSession implements ISession {
|
|
|
4210
4233
|
*/
|
|
4211
4234
|
constructor(params: ISessionParams);
|
|
4212
4235
|
/**
|
|
4213
|
-
* Emits a message to subscribers via _emitSubject after validating it against the policy (ClientPolicy).
|
|
4236
|
+
* Emits a message to subscribers via swarm _emitSubject after validating it against the policy (ClientPolicy).
|
|
4214
4237
|
* Emits the ban message if validation fails, notifying subscribers and logging via BusService.
|
|
4215
4238
|
* Supports SwarmConnectionService by broadcasting session outputs within the swarm.
|
|
4216
4239
|
* @param {string} message - The message to emit, typically an agent response or tool output.
|
|
@@ -4940,6 +4963,17 @@ declare class SwarmPublicService implements TSwarmConnectionService {
|
|
|
4940
4963
|
* @private
|
|
4941
4964
|
*/
|
|
4942
4965
|
private readonly swarmConnectionService;
|
|
4966
|
+
/**
|
|
4967
|
+
* Emits a message to the session for a specific client and swarm.
|
|
4968
|
+
* Wraps SessionConnectionService.emit with MethodContextService for scoping, logging via LoggerService if GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO is true.
|
|
4969
|
+
* Used in ClientAgent (e.g., session-level messaging) and AgentPublicService (e.g., swarm context emission).
|
|
4970
|
+
* @param {string} content - The message content to emit to the session.
|
|
4971
|
+
* @param {string} methodName - The name of the method invoking the operation, logged and scoped in context.
|
|
4972
|
+
* @param {string} clientId - The client ID, tying to ClientAgent sessions and PerfService tracking.
|
|
4973
|
+
* @param {SwarmName} swarmName - The swarm name, sourced from Swarm.interface, used in SwarmMetaService context.
|
|
4974
|
+
* @returns {Promise<void>} A promise resolving when the message is emitted.
|
|
4975
|
+
*/
|
|
4976
|
+
emit: (content: string, methodName: string, clientId: string, swarmName: SwarmName) => Promise<void>;
|
|
4943
4977
|
/**
|
|
4944
4978
|
* Pops the navigation stack or returns the default agent for the swarm, scoped to a client.
|
|
4945
4979
|
* Wraps SwarmConnectionService.navigationPop with MethodContextService for scoping, logging via LoggerService if GLOBAL_CONFIG.CC_LOGGER_ENABLE_INFO is true.
|
|
@@ -8734,7 +8768,7 @@ declare const complete: <Payload extends object = object>(content: string, clien
|
|
|
8734
8768
|
* await dispose();
|
|
8735
8769
|
*/
|
|
8736
8770
|
declare const session: {
|
|
8737
|
-
<Payload extends object = object>(clientId: string, swarmName: SwarmName): {
|
|
8771
|
+
<Payload extends object = object>(clientId: string, swarmName: SwarmName, { onDispose }?: Partial<Omit<ISessionConfig, "delay">>): {
|
|
8738
8772
|
complete: (content: string, payload?: Payload) => Promise<string>;
|
|
8739
8773
|
dispose: () => Promise<void>;
|
|
8740
8774
|
};
|
|
@@ -8755,7 +8789,7 @@ declare const session: {
|
|
|
8755
8789
|
* console.log(result);
|
|
8756
8790
|
* await dispose();
|
|
8757
8791
|
*/
|
|
8758
|
-
scheduled<Payload extends object = object>(clientId: string, swarmName: SwarmName, { delay }?: Partial<ISessionConfig>): {
|
|
8792
|
+
scheduled<Payload extends object = object>(clientId: string, swarmName: SwarmName, { delay, onDispose }?: Partial<ISessionConfig>): {
|
|
8759
8793
|
complete: (content: string, payload?: Payload) => Promise<string>;
|
|
8760
8794
|
dispose: () => Promise<void>;
|
|
8761
8795
|
};
|
|
@@ -8776,7 +8810,7 @@ declare const session: {
|
|
|
8776
8810
|
* console.log(result);
|
|
8777
8811
|
* await dispose();
|
|
8778
8812
|
*/
|
|
8779
|
-
rate<Payload extends object = object>(clientId: string, swarmName: SwarmName, { delay }?: Partial<ISessionConfig>): {
|
|
8813
|
+
rate<Payload extends object = object>(clientId: string, swarmName: SwarmName, { delay, onDispose }?: Partial<ISessionConfig>): {
|
|
8780
8814
|
complete(content: string, payload?: Payload): Promise<string>;
|
|
8781
8815
|
dispose: () => Promise<void>;
|
|
8782
8816
|
};
|
|
@@ -8789,6 +8823,7 @@ declare const session: {
|
|
|
8789
8823
|
*/
|
|
8790
8824
|
interface ISessionConfig {
|
|
8791
8825
|
delay?: number;
|
|
8826
|
+
onDispose?: () => void;
|
|
8792
8827
|
}
|
|
8793
8828
|
|
|
8794
8829
|
/**
|