@economic/agents 0.0.1-alpha.20 → 0.0.1-alpha.22
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/dist/index.d.mts +7 -5
- package/dist/index.mjs +6 -4
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -184,20 +184,22 @@ declare abstract class AIChatAgent<Env extends Cloudflare.Env = Cloudflare.Env>
|
|
|
184
184
|
* Typical uses include auth, rate limits, or feature flags — all logic lives here;
|
|
185
185
|
* the `guard` decorator only forwards `body` and handles the return shape.
|
|
186
186
|
*/
|
|
187
|
-
type GuardFn<TBody = Record<string, unknown>> = (body: TBody
|
|
187
|
+
type GuardFn<TBody = Record<string, unknown>> = (body: TBody) => Response | void | Promise<Response | void>;
|
|
188
188
|
/**
|
|
189
189
|
* Method decorator (TypeScript 5+ stage-3) that runs `guardFn` with the second
|
|
190
190
|
* argument's `body` (the chat request body). If `guardFn` returns a
|
|
191
191
|
* {@link Response}, that value is returned and the original method is not called.
|
|
192
192
|
*
|
|
193
193
|
* Intended for `onChatMessage(onFinish, options?)` on subclasses of
|
|
194
|
-
* `AIChatAgent`; `options` is read as `{ body?:
|
|
194
|
+
* `AIChatAgent`; `options` is read as `{ body?: TBody }`.
|
|
195
195
|
*
|
|
196
|
-
* @param guardFn - Called with `options?.body` before the method body.
|
|
196
|
+
* @param guardFn - Called with `options?.body` (cast to `TBody`) before the method body.
|
|
197
197
|
*
|
|
198
198
|
* @example
|
|
199
199
|
* ```ts
|
|
200
|
-
*
|
|
200
|
+
* interface RequestBody { token: string }
|
|
201
|
+
*
|
|
202
|
+
* const requireToken: GuardFn<RequestBody> = async (body) => {
|
|
201
203
|
* if (!await isValidToken(body?.token)) {
|
|
202
204
|
* return new Response("Unauthorized", { status: 401 });
|
|
203
205
|
* }
|
|
@@ -211,7 +213,7 @@ type GuardFn<TBody = Record<string, unknown>> = (body: TBody | undefined) => Res
|
|
|
211
213
|
* }
|
|
212
214
|
* ```
|
|
213
215
|
*/
|
|
214
|
-
declare function guard(guardFn: GuardFn): (target: (...args:
|
|
216
|
+
declare function guard<TBody = Record<string, unknown>>(guardFn: GuardFn<TBody>): (target: (...args: any[]) => Promise<Response>, _context: ClassMethodDecoratorContext) => (this: unknown, ...args: unknown[]) => Promise<Response>;
|
|
215
217
|
//#endregion
|
|
216
218
|
//#region src/types.d.ts
|
|
217
219
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -3956,13 +3956,15 @@ var AIChatAgent = class extends AIChatAgent$1 {
|
|
|
3956
3956
|
* {@link Response}, that value is returned and the original method is not called.
|
|
3957
3957
|
*
|
|
3958
3958
|
* Intended for `onChatMessage(onFinish, options?)` on subclasses of
|
|
3959
|
-
* `AIChatAgent`; `options` is read as `{ body?:
|
|
3959
|
+
* `AIChatAgent`; `options` is read as `{ body?: TBody }`.
|
|
3960
3960
|
*
|
|
3961
|
-
* @param guardFn - Called with `options?.body` before the method body.
|
|
3961
|
+
* @param guardFn - Called with `options?.body` (cast to `TBody`) before the method body.
|
|
3962
3962
|
*
|
|
3963
3963
|
* @example
|
|
3964
3964
|
* ```ts
|
|
3965
|
-
*
|
|
3965
|
+
* interface RequestBody { token: string }
|
|
3966
|
+
*
|
|
3967
|
+
* const requireToken: GuardFn<RequestBody> = async (body) => {
|
|
3966
3968
|
* if (!await isValidToken(body?.token)) {
|
|
3967
3969
|
* return new Response("Unauthorized", { status: 401 });
|
|
3968
3970
|
* }
|
|
@@ -3980,7 +3982,7 @@ function guard(guardFn) {
|
|
|
3980
3982
|
return function(target, _context) {
|
|
3981
3983
|
return async function(...args) {
|
|
3982
3984
|
const options = args[1];
|
|
3983
|
-
const result = await guardFn(options?.body);
|
|
3985
|
+
const result = await guardFn(options?.body ?? {});
|
|
3984
3986
|
if (result instanceof Response) return result;
|
|
3985
3987
|
return target.apply(this, args);
|
|
3986
3988
|
};
|