-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: middleware
for agentica
#64
base: main
Are you sure you want to change the base?
Conversation
is it draft? i think that first registered middleware should first execute |
Yes, you are right. I will change its ordering. |
}); | ||
|
||
agent.use(async function middleware_three(_ctx, next) { | ||
i = i - 1; // 3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please write additional test for to test after called next()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
too
packages/core/src/Agentica.ts
Outdated
usage: this.token_usage_, | ||
}), | ||
); | ||
const middlewares = this.middlewares_.get("conversate" as const); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend this "conversate"
string literal convert string literal constant variable
packages/core/src/Agentica.ts
Outdated
* - If not called, the current middleware will act as a termination point. | ||
*/ | ||
next: () => Promise<void>, | ||
) => any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this return value should be a Awaitable<void>
if return type is any
, it means that it is being used somewhere
packages/core/src/Agentica.ts
Outdated
})(context, async () => {}); | ||
} | ||
|
||
const newbie: IAgenticaPrompt[] = await this.executor_(context); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
middleware should cover reqeust and response
but, current architecture can't cover response
nextjs:
https://nextjs.org/docs/app/building-your-application/routing/middleware
if user can use next()
, it means cover request and response
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just added a comment about this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the reason why we chose next()
is because of to manage before process and after process.
however, the current middleware runs before the process.
For the previous process, we should not use the next()
keyword as it confuses clients.
packages/core/src/Agentica.ts
Outdated
usage: this.token_usage_, | ||
}), | ||
); | ||
const CONVERSATE = "conversate" as const; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this constant variable should exsits outer
packages/core/src/Agentica.ts
Outdated
*/ | ||
use(middleware: Middleware): void; | ||
public use(middleware: Middleware) { | ||
const type = "conversate" as const; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
packages/core/src/Agentica.ts
Outdated
})(context, async () => {}); | ||
} | ||
|
||
const newbie: IAgenticaPrompt[] = await this.executor_(context); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the reason why we chose next()
is because of to manage before process and after process.
however, the current middleware runs before the process.
For the previous process, we should not use the next()
keyword as it confuses clients.
}); | ||
|
||
agent.use(async function middleware_three(_ctx, next) { | ||
i = i - 1; // 3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
too
{
...
public async conversate(content: string): Promise<IAgenticaPrompt[]> {
const prompt: IAgenticaPrompt.IText<"user"> = {
type: "text",
role: "user",
text: content,
};
await this.dispatch(prompt);
const context = this.getContext({ prompt, usage: this.token_usage_ });
const newbie: IAgenticaPrompt[] = await this.executor_(context);
this.prompt_histories_.push(prompt, ...newbie);
const middlewares = this.middlewares_.get(CONVERSATE);
if (middlewares) {
await Array.from(middlewares).reduce((acc, cur) => {
return this.middlewareCompose(acc, cur);
})(context, async () => {});
}
return [prompt, ...newbie];
}
...
} Do you think should be after execute like this? @sunrabbit123 |
middleware should process both before and after so, example const layer = this.middlewares_.get(CONVERSTAE) ?? [];
layer.push(this._executor);
const newbie = await Array.from(layer)
.reduce((acc, cur) => this.middlewareCompose(acc, cur))(context, async () => {}); |
I don't understand if middleware can be processed before and after execution in your code. Isn't this the act of seeing the final execution as one of the middleware and just putting execute in the end? |
I've heard explanations from you. The functional form is profound. But I understand it perfectly. |
Temporary stops |
This PR improves the documentation for the use method to better describe its role in registering middlewares. The updated comment clarifies execution order, next() handling, and async considerations, making it more suitable for developers using this API.
by the way, I'm still thinking about the best way to handle middleware order. What do you all think? 🤔