|
| 1 | +import beginContext from "../../utils/beginContext"; |
| 2 | +import { GLOBAL_CONFIG } from "../../config/params"; |
| 3 | +import swarm from "../../lib"; |
| 4 | + |
| 5 | +/** @private Constant defining the method name for logging and validation context */ |
| 6 | +const METHOD_NAME = "function.commit.commitDeveloperMessage"; |
| 7 | + |
| 8 | +/** |
| 9 | + * Function implementation |
| 10 | + */ |
| 11 | +const commitDeveloperMessageInternal = beginContext( |
| 12 | + async (content: string, clientId: string, agentName: string): Promise<void> => { |
| 13 | + GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG && |
| 14 | + swarm.loggerService.log(METHOD_NAME, { |
| 15 | + content, |
| 16 | + clientId, |
| 17 | + agentName, |
| 18 | + }); |
| 19 | + |
| 20 | + swarm.agentValidationService.validate(agentName, METHOD_NAME); |
| 21 | + |
| 22 | + swarm.sessionValidationService.validate(clientId, METHOD_NAME); |
| 23 | + const swarmName = swarm.sessionValidationService.getSwarm(clientId); |
| 24 | + |
| 25 | + swarm.swarmValidationService.validate(swarmName, METHOD_NAME); |
| 26 | + |
| 27 | + const currentAgentName = await swarm.swarmPublicService.getAgentName( |
| 28 | + METHOD_NAME, |
| 29 | + clientId, |
| 30 | + swarmName |
| 31 | + ); |
| 32 | + if (currentAgentName !== agentName) { |
| 33 | + GLOBAL_CONFIG.CC_LOGGER_ENABLE_LOG && |
| 34 | + swarm.loggerService.log( |
| 35 | + 'function "commitDeveloperMessage" skipped due to the agent change', |
| 36 | + { |
| 37 | + currentAgentName, |
| 38 | + agentName, |
| 39 | + clientId, |
| 40 | + } |
| 41 | + ); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + await swarm.sessionPublicService.commitDeveloperMessage( |
| 46 | + content, |
| 47 | + METHOD_NAME, |
| 48 | + clientId, |
| 49 | + swarmName |
| 50 | + ); |
| 51 | + } |
| 52 | +); |
| 53 | + |
| 54 | +/** |
| 55 | + * Commits a developer-generated message to the active agent in the swarm system. |
| 56 | + * Validates the agent, session, and swarm, ensuring the current agent matches the provided agent before committing the message. |
| 57 | + * Runs within a beginContext wrapper for execution context management, logging operations via LoggerService. |
| 58 | + * Integrates with AgentValidationService (agent validation), SessionValidationService (session and swarm retrieval), |
| 59 | + * SwarmValidationService (swarm validation), SwarmPublicService (agent retrieval), SessionPublicService (message committing), |
| 60 | + * and LoggerService (logging). Complements functions like commitSystemMessage by handling developer messages |
| 61 | + * (e.g., user or developer instructions) rather than system-generated or assistant-generated responses. |
| 62 | + * |
| 63 | + * @param {string} content - The content of the developer message to commit, typically related to user or developer instructions. |
| 64 | + * @param {string} clientId - The ID of the client associated with the session, validated against active sessions. |
| 65 | + * @param {string} agentName - The name of the agent to commit the message for, validated against registered agents. |
| 66 | + * @returns {Promise<void>} A promise that resolves when the message is committed or skipped (e.g., agent mismatch). |
| 67 | + * @throws {Error} If agent, session, or swarm validation fails, propagated from respective validation services. |
| 68 | + */ |
| 69 | +export async function commitDeveloperMessage(content: string, clientId: string, agentName: string) { |
| 70 | + return await commitDeveloperMessageInternal(content, clientId, agentName); |
| 71 | +} |
0 commit comments