Skip to content

Commit 7f8ce6e

Browse files
committed
before-navigate-lifecycle
1 parent 93d41ef commit 7f8ce6e

5 files changed

Lines changed: 44 additions & 22 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "agent-swarm-kit",
3-
"version": "1.1.87",
3+
"version": "1.1.88",
44
"description": "A TypeScript library for building orchestrated framework-agnostic multi-agent AI systems",
55
"author": {
66
"name": "Petr Tripolsky",

src/template/createNavigateToAgent.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ const DEFAULT_EXECUTE_MESSAGE = "";
4646
* };
4747
*/
4848
export interface INavigateToAgentParams {
49+
beforeNavigate?: (
50+
clientId: string,
51+
lastMessage: string | null,
52+
lastAgent: AgentName,
53+
agentName: AgentName
54+
) => void | Promise<void>;
4955
flushMessage?:
5056
| string
5157
| ((clientId: string, defaultAgent: AgentName) => string | Promise<string>);
@@ -145,6 +151,7 @@ const DEFAULT_LAST_MESSAGE_FN = (
145151
* // Navigates to SupportAgent, commits dynamic tool output, and executes the message with the last user message.
146152
*/
147153
export const createNavigateToAgent = ({
154+
beforeNavigate,
148155
lastMessage: lastMessageFn = DEFAULT_LAST_MESSAGE_FN,
149156
executeMessage = DEFAULT_EXECUTE_MESSAGE,
150157
emitMessage,
@@ -168,7 +175,7 @@ export const createNavigateToAgent = ({
168175
toolId,
169176
});
170177

171-
const lastMessage = await getLastUserMessage(clientId);
178+
const lastMessageRaw = await getLastUserMessage(clientId);
172179
const lastAgent = await getAgentName(clientId);
173180

174181
await commitStopToolsForce(clientId);
@@ -179,6 +186,13 @@ export const createNavigateToAgent = ({
179186
Promise.resolve(!emitMessage)
180187
)
181188
) {
189+
const lastMessage = await lastMessageFn(
190+
clientId,
191+
lastMessageRaw,
192+
lastAgent,
193+
agentName
194+
);
195+
beforeNavigate && await beforeNavigate(clientId, lastMessage, lastAgent, agentName);
182196
await commitToolOutputForce(
183197
toolId,
184198
typeof toolOutput === "string"
@@ -192,12 +206,7 @@ export const createNavigateToAgent = ({
192206
? executeMessage
193207
: await executeMessage(
194208
clientId,
195-
await lastMessageFn(
196-
clientId,
197-
lastMessage,
198-
lastAgent,
199-
agentName
200-
),
209+
lastMessage,
201210
lastAgent,
202211
agentName
203212
),
@@ -212,6 +221,13 @@ export const createNavigateToAgent = ({
212221
Promise.resolve(!!emitMessage)
213222
)
214223
) {
224+
const lastMessage = await lastMessageFn(
225+
clientId,
226+
lastMessageRaw,
227+
lastAgent,
228+
agentName
229+
);
230+
beforeNavigate && await beforeNavigate(clientId, lastMessage, lastAgent, agentName);
215231
await commitToolOutputForce(
216232
toolId,
217233
typeof toolOutput === "string"
@@ -225,12 +241,7 @@ export const createNavigateToAgent = ({
225241
? emitMessage
226242
: await emitMessage(
227243
clientId,
228-
await lastMessageFn(
229-
clientId,
230-
lastMessage,
231-
lastAgent,
232-
agentName
233-
),
244+
lastMessage,
234245
lastAgent,
235246
agentName
236247
),

src/template/createNavigateToTriageAgent.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ const DEFAULT_EXECUTE_MESSAGE = "";
4646
* };
4747
*/
4848
export interface INavigateToTriageParams {
49+
beforeNavigate?: (
50+
clientId: string,
51+
lastMessage: string | null,
52+
lastAgent: AgentName,
53+
defaultAgent: AgentName,
54+
) => Promise<void> | void;
4955
lastMessage?: (
5056
clientId: string,
5157
lastMessage: string | null,
@@ -111,6 +117,7 @@ const DEFAULT_LAST_MESSAGE_FN = (
111117
*/
112118
export const createNavigateToTriageAgent = ({
113119
flushMessage,
120+
beforeNavigate,
114121
lastMessage: lastMessageFn = DEFAULT_LAST_MESSAGE_FN,
115122
executeMessage = DEFAULT_EXECUTE_MESSAGE,
116123
toolOutputAccept = DEFAULT_ACCEPT_FN,
@@ -139,17 +146,19 @@ export const createNavigateToTriageAgent = ({
139146
await commitStopToolsForce(clientId);
140147

141148
if (await not(hasNavigation(clientId, defaultAgent))) {
142-
const lastMessage = await getLastUserMessage(clientId);
143-
await changeToDefaultAgent(clientId);
149+
const lastMessageRaw = await getLastUserMessage(clientId);
150+
const lastMessage = await lastMessageFn(clientId, lastMessageRaw, defaultAgent, lastAgent);
151+
beforeNavigate && await beforeNavigate(clientId, lastMessage, lastAgent, defaultAgent);
144152
await commitToolOutputForce(
145153
toolId,
146154
typeof toolOutputAccept === "string"
147155
? toolOutputAccept
148-
: await toolOutputAccept(clientId, defaultAgent),
156+
: await toolOutputAccept(clientId, lastAgent),
149157
clientId
150158
);
159+
await changeToDefaultAgent(clientId);
151160
await executeForce(
152-
await lastMessageFn(clientId, lastMessage, defaultAgent, lastAgent),
161+
lastMessage,
153162
clientId
154163
);
155164
return;

types.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11123,6 +11123,7 @@ declare const dumpClientPerformance: {
1112311123
* };
1112411124
*/
1112511125
interface INavigateToTriageParams {
11126+
beforeNavigate?: (clientId: string, lastMessage: string | null, lastAgent: AgentName, defaultAgent: AgentName) => Promise<void> | void;
1112611127
lastMessage?: (clientId: string, lastMessage: string | null, defaultAgent: AgentName, lastAgent: AgentName) => string | Promise<string>;
1112711128
flushMessage?: string | ((clientId: string, defaultAgent: AgentName) => string | Promise<string>);
1112811129
executeMessage?: string | ((clientId: string, defaultAgent: AgentName) => string | Promise<string>);
@@ -11161,7 +11162,7 @@ interface INavigateToTriageParams {
1116111162
* await navigate("tool-789", "client-012");
1116211163
* // Commits dynamic reject message and executes the message if already on the default agent.
1116311164
*/
11164-
declare const createNavigateToTriageAgent: ({ flushMessage, lastMessage: lastMessageFn, executeMessage, toolOutputAccept, toolOutputReject, }: INavigateToTriageParams) => (toolId: string, clientId: string) => Promise<void>;
11165+
declare const createNavigateToTriageAgent: ({ flushMessage, beforeNavigate, lastMessage: lastMessageFn, executeMessage, toolOutputAccept, toolOutputReject, }: INavigateToTriageParams) => (toolId: string, clientId: string) => Promise<void>;
1116511166

1116611167
/**
1116711168
* Configuration parameters for creating a navigation handler to a specific agent.
@@ -11188,6 +11189,7 @@ declare const createNavigateToTriageAgent: ({ flushMessage, lastMessage: lastMes
1118811189
* };
1118911190
*/
1119011191
interface INavigateToAgentParams {
11192+
beforeNavigate?: (clientId: string, lastMessage: string | null, lastAgent: AgentName, agentName: AgentName) => void | Promise<void>;
1119111193
flushMessage?: string | ((clientId: string, defaultAgent: AgentName) => string | Promise<string>);
1119211194
toolOutput?: string | ((clientId: string, lastAgent: AgentName, agentName: AgentName) => string | Promise<string>);
1119311195
lastMessage?: (clientId: string, lastMessage: string | null, lastAgent: AgentName, agentName: AgentName) => string | Promise<string>;
@@ -11227,7 +11229,7 @@ interface INavigateToAgentParams {
1122711229
* await navigate("tool-789", "client-012", "SupportAgent");
1122811230
* // Navigates to SupportAgent, commits dynamic tool output, and executes the message with the last user message.
1122911231
*/
11230-
declare const createNavigateToAgent: ({ lastMessage: lastMessageFn, executeMessage, emitMessage, flushMessage, toolOutput, }: INavigateToAgentParams) => (toolId: string, clientId: string, agentName: string) => Promise<void>;
11232+
declare const createNavigateToAgent: ({ beforeNavigate, lastMessage: lastMessageFn, executeMessage, emitMessage, flushMessage, toolOutput, }: INavigateToAgentParams) => (toolId: string, clientId: string, agentName: string) => Promise<void>;
1123111233

1123211234
/**
1123311235
* Adds navigation functionality to an agent by creating a tool that allows navigation to a specified agent.

0 commit comments

Comments
 (0)