Skip to content

Commit ac17487

Browse files
authored
Add setup callback to @hook (#64)
1 parent 2d572a5 commit ac17487

File tree

4 files changed

+7
-2
lines changed

4 files changed

+7
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pompeii-labs/magma",
3-
"version": "1.8.1",
3+
"version": "1.8.2",
44
"description": "The Typescript framework to build AI agents quickly and easily",
55
"keywords": [
66
"Agents",

src/decorators.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export function middleware<T extends MagmaMiddlewareTriggerType>(
135135
* @param hookName name of the hook
136136
* @param options configuration options for the hook
137137
* @param options.session session configuration for the hook
138+
* @param options.setup arguments to pass to the agent's setup function before the hook handler is called
138139
* Examples:
139140
* @hook('notification') -> POST /hooks/notification
140141
* @hook('notification', { session: 'default' })
@@ -143,7 +144,7 @@ export function middleware<T extends MagmaMiddlewareTriggerType>(
143144
*/
144145
export function hook(
145146
hookName: string,
146-
options: { session?: MagmaHook['session']; description?: string } = {}
147+
options: { session?: MagmaHook['session']; description?: string; setup?: MagmaHook['setup'] } = {}
147148
) {
148149
return function <R extends void>(
149150
target: object,
@@ -153,6 +154,7 @@ export function hook(
153154
_hookName?: string;
154155
_session?: MagmaHook['session'];
155156
_description?: string;
157+
_setup?: MagmaHook['setup'];
156158
}
157159
>
158160
) {
@@ -163,6 +165,7 @@ export function hook(
163165
descriptor.value._hookName = hookName;
164166
descriptor.value._session = options.session;
165167
descriptor.value._description = options.description;
168+
descriptor.value._setup = options.setup;
166169
};
167170
}
168171

src/helpers/utilities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export function loadHooks(target: any): MagmaHook[] {
9494
name: method['_hookName'],
9595
handler: method.bind(target),
9696
session: (method as any)['_session'],
97+
setup: (method as any)['_setup'],
9798
description: (method as any)['_description'],
9899
} as MagmaHook);
99100
}

src/types/utilities/hooks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ export type MagmaHook = {
55
name: string;
66
handler: (request: Request, response: Response, agent: MagmaAgent) => Promise<void>;
77
session?: 'default' | (string & {}) | ((req: Request) => string | Promise<string>);
8+
setup?: ((req: Request) => Record<string, any> | Promise<Record<string, any>>) | Record<string, any>;
89
description?: string;
910
};

0 commit comments

Comments
 (0)