@nest-boot/mastra 0.0.0-stage → 8.0.0
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/LICENSE +21 -0
- package/README.md +60 -2
- package/dist/base.agent.d.ts +11 -0
- package/dist/base.agent.js +15 -0
- package/dist/base.agent.js.map +1 -0
- package/dist/base.step.d.ts +42 -0
- package/dist/base.step.js +62 -0
- package/dist/base.step.js.map +1 -0
- package/dist/base.tool.d.ts +61 -0
- package/dist/base.tool.js +52 -0
- package/dist/base.tool.js.map +1 -0
- package/dist/base.workflow.d.ts +47 -0
- package/dist/base.workflow.js +96 -0
- package/dist/base.workflow.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/mastra-studio.middleware.d.ts +40 -0
- package/dist/mastra-studio.middleware.js +230 -0
- package/dist/mastra-studio.middleware.js.map +1 -0
- package/dist/mastra.controller.d.ts +30 -0
- package/dist/mastra.controller.js +209 -0
- package/dist/mastra.controller.js.map +1 -0
- package/dist/mastra.manager.d.ts +51 -0
- package/dist/mastra.manager.js +159 -0
- package/dist/mastra.manager.js.map +1 -0
- package/dist/mastra.module-definition.d.ts +90 -0
- package/dist/mastra.module-definition.js +75 -0
- package/dist/mastra.module-definition.js.map +1 -0
- package/dist/mastra.module.d.ts +44 -0
- package/dist/mastra.module.js +351 -0
- package/dist/mastra.module.js.map +1 -0
- package/package.json +103 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 nest-boot
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,61 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @nest-boot/mastra
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
NestJS-native [Mastra](https://mastra.ai/) integration for Nest Boot with dependency-injected `BaseAgent`, `BaseTool`, `BaseStep`, and `BaseWorkflow` base classes, automatic provider discovery, embedded Mastra Studio, `@nest-boot/auth` route protection, and environment-driven PostgreSQL / ClickHouse storage.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @nest-boot/mastra @mastra/core @nest-boot/auth zod
|
|
9
|
+
# Optional: install `mastra` to enable embedded Mastra Studio at `/api/mastra/studio`
|
|
10
|
+
pnpm add mastra
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
Import `MastraModule` in your root or common module:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { MastraModule } from "@nest-boot/mastra";
|
|
19
|
+
import { Module } from "@nestjs/common";
|
|
20
|
+
|
|
21
|
+
@Module({
|
|
22
|
+
imports: [MastraModule],
|
|
23
|
+
})
|
|
24
|
+
export class AppModule {}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
NestJS owns the HTTP server. `server.*` configuration is forbidden in both TypeScript and at startup, including options returned by `forRootAsync`. Define custom endpoints with NestJS controllers and configure authentication, middleware, CORS, and exception handling through NestJS. `api.auth` and `api.customRouteAuthConfig` are also unsupported; use `@nest-boot/auth` for authorization.
|
|
28
|
+
|
|
29
|
+
API and Studio paths include the application's global prefix. For example, `app.setGlobalPrefix("v1")` serves the API at `/v1/api/mastra` and Studio at `/v1/api/mastra/studio`.
|
|
30
|
+
|
|
31
|
+
Register permission mapping in your `AuthModule` `buildAbility` callback to grant access to `/api/mastra/*`:
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { Mastra } from "@mastra/core/mastra";
|
|
35
|
+
|
|
36
|
+
buildAbility: ({ builder, permissions }) => {
|
|
37
|
+
if (permissions.includes("mastra:manage")) {
|
|
38
|
+
builder.can("manage", Mastra);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Base Classes
|
|
44
|
+
|
|
45
|
+
- `BaseAgent` — injectable Mastra `Agent` subclass with automatic kebab-case `id` derivation.
|
|
46
|
+
- `BaseTool(definition)` — injectable Mastra `Tool` class factory preserving native input/output validation and `super(options)` runtime configuration.
|
|
47
|
+
- `BaseStep(schemas)` — injectable workflow `Step` class factory with full static schema typing.
|
|
48
|
+
- `BaseWorkflow(schemas)` — injectable Mastra `Workflow` class factory with lifecycle hooks (`onStart`, `onFinish`, `onError`) and NestJS DI-safe `commit()`.
|
|
49
|
+
|
|
50
|
+
## Environment Variables
|
|
51
|
+
|
|
52
|
+
| Variable | Description |
|
|
53
|
+
| ----------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
|
|
54
|
+
| `MASTRA_DATABASE_URL` | Dedicated PostgreSQL URL for Mastra `PostgresStore` (`schemaName: "mastra"`). Takes precedence over `DATABASE_URL`. |
|
|
55
|
+
| `DATABASE_URL` | Fallback PostgreSQL URL when `MASTRA_DATABASE_URL` is unset. Non-PostgreSQL URLs (e.g. PGlite) do not enable `PostgresStore`. |
|
|
56
|
+
| `MASTRA_CLICKHOUSE_URL` | Optional ClickHouse URL (`http:`, `https:`, `clickhouse:`, `clickhouses:`) enabling `ObservabilityStorageClickhouseVNext`. |
|
|
57
|
+
|
|
58
|
+
## Routes
|
|
59
|
+
|
|
60
|
+
- **Mastra HTTP API**: `/api/mastra/*` (protected by `@Can("manage", Mastra)`).
|
|
61
|
+
- **Mastra Studio**: `/api/mastra/studio` (served via Express middleware; static shell is unauthenticated while API calls go through `@Can("manage", Mastra)`).
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Agent as MastraAgent, type AgentConfig } from "@mastra/core/agent";
|
|
2
|
+
export type BaseAgentConfig = Omit<AgentConfig, "id"> & {
|
|
3
|
+
id?: string;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Base NestJS-injectable Mastra agent that automatically derives its `id`
|
|
7
|
+
* from the concrete subclass name in kebab-case unless explicitly overridden.
|
|
8
|
+
*/
|
|
9
|
+
export declare abstract class BaseAgent extends MastraAgent {
|
|
10
|
+
constructor(config: BaseAgentConfig);
|
|
11
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Agent as MastraAgent } from "@mastra/core/agent";
|
|
2
|
+
import { kebabCase } from "lodash-es";
|
|
3
|
+
/**
|
|
4
|
+
* Base NestJS-injectable Mastra agent that automatically derives its `id`
|
|
5
|
+
* from the concrete subclass name in kebab-case unless explicitly overridden.
|
|
6
|
+
*/
|
|
7
|
+
export class BaseAgent extends MastraAgent {
|
|
8
|
+
constructor(config) {
|
|
9
|
+
super({
|
|
10
|
+
...config,
|
|
11
|
+
id: config.id ?? kebabCase(new.target.name),
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=base.agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.agent.js","sourceRoot":"","sources":["../src/base.agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,IAAI,WAAW,EAAoB,MAAM,oBAAoB,CAAC;AAC5E,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAMtC;;;GAGG;AACH,MAAM,OAAgB,SAAU,SAAQ,WAAW;IACjD,YAAY,MAAuB;QACjC,KAAK,CAAC;YACJ,GAAG,MAAM;YACT,EAAE,EAAE,MAAM,CAAC,EAAE,IAAI,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC;SAC5C,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type InferPublicSchema, type InferStandardSchemaOutput, type PublicSchema, type StandardSchemaWithJSON } from "@mastra/core/schema";
|
|
2
|
+
import type { DefaultEngineType, ExecuteFunctionParams, InnerOutput, Step, StepParams } from "@mastra/core/workflows";
|
|
3
|
+
type StepStaticKeys = "id" | "description" | "inputSchema" | "outputSchema" | "stateSchema" | "resumeSchema" | "suspendSchema" | "requestContextSchema";
|
|
4
|
+
export type AnyMastraStep = Step<string, any, any, any, any, any, DefaultEngineType, any>;
|
|
5
|
+
/** Extracts the validated input type from a `BaseStep` subclass or Mastra `Step`. */
|
|
6
|
+
export type StepInput<T extends AnyMastraStep> = InferStandardSchemaOutput<T["inputSchema"]>;
|
|
7
|
+
/** Extracts the validated output type from a `BaseStep` subclass or Mastra `Step`. */
|
|
8
|
+
export type StepOutput<T extends AnyMastraStep> = InferStandardSchemaOutput<T["outputSchema"]>;
|
|
9
|
+
/** Extracts the optional workflow state type from a `BaseStep` subclass or Mastra `Step`. */
|
|
10
|
+
export type StepState<T extends AnyMastraStep> = NonNullable<T["stateSchema"]> extends StandardSchemaWithJSON ? InferStandardSchemaOutput<NonNullable<T["stateSchema"]>> : unknown;
|
|
11
|
+
/** Extracts the optional resume payload type from a `BaseStep` subclass or Mastra `Step`. */
|
|
12
|
+
export type StepResume<T extends AnyMastraStep> = NonNullable<T["resumeSchema"]> extends StandardSchemaWithJSON ? InferStandardSchemaOutput<NonNullable<T["resumeSchema"]>> : unknown;
|
|
13
|
+
/** Extracts the optional suspend payload type from a `BaseStep` subclass or Mastra `Step`. */
|
|
14
|
+
export type StepSuspend<T extends AnyMastraStep> = NonNullable<T["suspendSchema"]> extends StandardSchemaWithJSON ? InferStandardSchemaOutput<NonNullable<T["suspendSchema"]>> : unknown;
|
|
15
|
+
/** Extracts the optional request context type from a `BaseStep` subclass or Mastra `Step`. */
|
|
16
|
+
export type StepRequestContext<T extends AnyMastraStep> = NonNullable<T["requestContextSchema"]> extends StandardSchemaWithJSON ? InferStandardSchemaOutput<NonNullable<T["requestContextSchema"]>> : unknown;
|
|
17
|
+
/** Typed `execute` parameter object derived from a `BaseStep` subclass or Mastra `Step`. */
|
|
18
|
+
export type StepExecuteParams<T extends AnyMastraStep> = ExecuteFunctionParams<StepState<T>, StepInput<T>, StepOutput<T>, StepResume<T>, StepSuspend<T>, DefaultEngineType, StepRequestContext<T>>;
|
|
19
|
+
/** Static definition (`id`, `description`, and schemas) passed exclusively to `BaseStep(definition)`. */
|
|
20
|
+
export type BaseStepSchemas<TInputSchema extends PublicSchema, TOutputSchema extends PublicSchema, TStateSchema extends PublicSchema | undefined = undefined, TResumeSchema extends PublicSchema | undefined = undefined, TSuspendSchema extends PublicSchema | undefined = undefined, TRequestContextSchema extends PublicSchema | undefined = undefined> = Omit<Pick<StepParams<string, TStateSchema, TInputSchema, TOutputSchema, TResumeSchema, TSuspendSchema, TRequestContextSchema>, StepStaticKeys>, "id"> & {
|
|
21
|
+
id?: string;
|
|
22
|
+
};
|
|
23
|
+
/** Runtime options passed exclusively to `super(options)` inside a `BaseStep` subclass. */
|
|
24
|
+
export type BaseStepOptions = Omit<StepParams<string, any, any, any, any, any, any>, StepStaticKeys | "execute">;
|
|
25
|
+
type InferOptionalSchema<TSchema extends PublicSchema | undefined> = TSchema extends PublicSchema ? InferPublicSchema<TSchema> : unknown;
|
|
26
|
+
/** Constructor type returned by the `BaseStep(schemas)` class factory. */
|
|
27
|
+
export type BaseStepConstructor<TInput, TOutput, TState = unknown, TResume = unknown, TSuspend = unknown, TRequestContext = unknown> = abstract new (options?: BaseStepOptions) => Omit<Step<string, TState, TInput, TOutput, TResume, TSuspend, DefaultEngineType, TRequestContext>, "execute"> & {
|
|
28
|
+
execute(params: ExecuteFunctionParams<TState, TInput, TOutput, TResume, TSuspend, DefaultEngineType, TRequestContext>): Promise<TOutput | InnerOutput>;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* NestJS-native Mastra workflow step base class factory.
|
|
32
|
+
*
|
|
33
|
+
* `BaseStep(schemas)` owns exclusively the static definition (`Pick`: `id`, `description`,
|
|
34
|
+
* and schemas) so `execute(...)` and `Workflow.then(...)` preserve full, un-erased Mastra types.
|
|
35
|
+
* `super(options)` owns exclusively runtime configuration (`Omit`, e.g. `retries`, `scorers`,
|
|
36
|
+
* `metadata`) with full access to DI dependencies.
|
|
37
|
+
* Automatically derives `id` from the concrete subclass name in kebab-case unless overridden.
|
|
38
|
+
* @param schemas - Static Zod/Standard schema contract
|
|
39
|
+
* @returns Abstract base class implementing Mastra `Step`
|
|
40
|
+
*/
|
|
41
|
+
export declare function BaseStep<TInputSchema extends PublicSchema, TOutputSchema extends PublicSchema, TStateSchema extends PublicSchema | undefined = undefined, TResumeSchema extends PublicSchema | undefined = undefined, TSuspendSchema extends PublicSchema | undefined = undefined, TRequestContextSchema extends PublicSchema | undefined = undefined>(schemas: BaseStepSchemas<TInputSchema, TOutputSchema, TStateSchema, TResumeSchema, TSuspendSchema, TRequestContextSchema>): BaseStepConstructor<InferPublicSchema<TInputSchema>, InferPublicSchema<TOutputSchema>, InferOptionalSchema<TStateSchema>, InferOptionalSchema<TResumeSchema>, InferOptionalSchema<TSuspendSchema>, InferOptionalSchema<TRequestContextSchema>>;
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { toStandardSchema, } from "@mastra/core/schema";
|
|
2
|
+
import { kebabCase } from "lodash-es";
|
|
3
|
+
const BASE_STEP_MARKER = Symbol("nest-mastra-base-step");
|
|
4
|
+
/**
|
|
5
|
+
* NestJS-native Mastra workflow step base class factory.
|
|
6
|
+
*
|
|
7
|
+
* `BaseStep(schemas)` owns exclusively the static definition (`Pick`: `id`, `description`,
|
|
8
|
+
* and schemas) so `execute(...)` and `Workflow.then(...)` preserve full, un-erased Mastra types.
|
|
9
|
+
* `super(options)` owns exclusively runtime configuration (`Omit`, e.g. `retries`, `scorers`,
|
|
10
|
+
* `metadata`) with full access to DI dependencies.
|
|
11
|
+
* Automatically derives `id` from the concrete subclass name in kebab-case unless overridden.
|
|
12
|
+
* @param schemas - Static Zod/Standard schema contract
|
|
13
|
+
* @returns Abstract base class implementing Mastra `Step`
|
|
14
|
+
*/
|
|
15
|
+
export function BaseStep(schemas) {
|
|
16
|
+
const inputSchema = toStandardSchema(schemas.inputSchema);
|
|
17
|
+
const outputSchema = toStandardSchema(schemas.outputSchema);
|
|
18
|
+
const stateSchema = schemas.stateSchema
|
|
19
|
+
? toStandardSchema(schemas.stateSchema)
|
|
20
|
+
: undefined;
|
|
21
|
+
const resumeSchema = schemas.resumeSchema
|
|
22
|
+
? toStandardSchema(schemas.resumeSchema)
|
|
23
|
+
: undefined;
|
|
24
|
+
const suspendSchema = schemas.suspendSchema
|
|
25
|
+
? toStandardSchema(schemas.suspendSchema)
|
|
26
|
+
: undefined;
|
|
27
|
+
const requestContextSchema = schemas.requestContextSchema
|
|
28
|
+
? toStandardSchema(schemas.requestContextSchema)
|
|
29
|
+
: undefined;
|
|
30
|
+
class GeneratedBaseStep {
|
|
31
|
+
[BASE_STEP_MARKER] = true;
|
|
32
|
+
id;
|
|
33
|
+
description;
|
|
34
|
+
inputSchema = inputSchema;
|
|
35
|
+
outputSchema = outputSchema;
|
|
36
|
+
stateSchema = stateSchema;
|
|
37
|
+
resumeSchema = resumeSchema;
|
|
38
|
+
suspendSchema = suspendSchema;
|
|
39
|
+
requestContextSchema = requestContextSchema;
|
|
40
|
+
retries;
|
|
41
|
+
scorers;
|
|
42
|
+
metadata;
|
|
43
|
+
constructor(options) {
|
|
44
|
+
this.id = schemas.id ?? kebabCase(new.target.name);
|
|
45
|
+
this.description = schemas.description;
|
|
46
|
+
this.retries = options?.retries;
|
|
47
|
+
this.scorers = options?.scorers;
|
|
48
|
+
this.metadata = options?.metadata;
|
|
49
|
+
const proto = new.target.prototype;
|
|
50
|
+
this.execute = (params) => proto.execute.call(this, params);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return GeneratedBaseStep;
|
|
54
|
+
}
|
|
55
|
+
Object.defineProperty(BaseStep, Symbol.hasInstance, {
|
|
56
|
+
value(instance) {
|
|
57
|
+
return (typeof instance === "object" &&
|
|
58
|
+
instance !== null &&
|
|
59
|
+
BASE_STEP_MARKER in instance);
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
//# sourceMappingURL=base.step.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.step.js","sourceRoot":"","sources":["../src/base.step.ts"],"names":[],"mappings":"AACA,OAAO,EAKL,gBAAgB,GACjB,MAAM,qBAAqB,CAAC;AAU7B,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,MAAM,gBAAgB,GAAG,MAAM,CAAC,uBAAuB,CAAC,CAAC;AAyIzD;;;;;;;;;;GAUG;AACH,MAAM,UAAU,QAAQ,CAQtB,OAOC;IAgBD,MAAM,WAAW,GAAG,gBAAgB,CAClC,OAAO,CAAC,WAAW,CACc,CAAC;IACpC,MAAM,YAAY,GAAG,gBAAgB,CACnC,OAAO,CAAC,YAAY,CACc,CAAC;IACrC,MAAM,WAAW,GAAG,OAAO,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,SAYxB,CAAC;YAEF,IAAI,CAAC,OAAO,GAAG,CAAC,MAAM,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAC9D,CAAC;KAaF;IAED,OAAO,iBAAiB,CAAC;AAC3B,CAAC;AAED,MAAM,CAAC,cAAc,CAAC,QAAQ,EAAE,MAAM,CAAC,WAAW,EAAE;IAClD,KAAK,CAAC,QAAiB;QACrB,OAAO,CACL,OAAO,QAAQ,KAAK,QAAQ;YAC5B,QAAQ,KAAK,IAAI;YACjB,gBAAgB,IAAI,QAAQ,CAC7B,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type InferPublicSchema, type InferStandardSchemaOutput, type PublicSchema, type StandardSchemaWithJSON } from "@mastra/core/schema";
|
|
2
|
+
import { Tool as MastraTool, type ToolAction, type ToolExecuteContext, type ToolExecutionContext, type ValidationError } from "@mastra/core/tools";
|
|
3
|
+
type ToolStaticKeys = "id" | "title" | "description" | "inputSchema" | "outputSchema" | "suspendSchema" | "resumeSchema" | "requestContextSchema";
|
|
4
|
+
type SchemaLike = PublicSchema | undefined;
|
|
5
|
+
type InferOptionalSchema<TSchema extends SchemaLike> = TSchema extends PublicSchema ? InferPublicSchema<TSchema> : unknown;
|
|
6
|
+
export type AnyMastraTool = MastraTool<any, any, any, any, any, any, any>;
|
|
7
|
+
type MastraToolSchemas = Pick<AnyMastraTool, "inputSchema" | "outputSchema" | "suspendSchema" | "resumeSchema" | "requestContextSchema">;
|
|
8
|
+
/** Extracts the validated input type from a `BaseTool` subclass or Mastra `Tool`. */
|
|
9
|
+
export type ToolInput<T extends MastraToolSchemas> = NonNullable<T["inputSchema"]> extends StandardSchemaWithJSON ? InferStandardSchemaOutput<NonNullable<T["inputSchema"]>> : unknown;
|
|
10
|
+
/** Extracts the validated output type from a `BaseTool` subclass or Mastra `Tool`. */
|
|
11
|
+
export type ToolOutput<T extends MastraToolSchemas> = NonNullable<T["outputSchema"]> extends StandardSchemaWithJSON ? InferStandardSchemaOutput<NonNullable<T["outputSchema"]>> : unknown;
|
|
12
|
+
/** Extracts the optional suspend payload type from a `BaseTool` subclass or Mastra `Tool`. */
|
|
13
|
+
export type ToolSuspend<T extends MastraToolSchemas> = NonNullable<T["suspendSchema"]> extends StandardSchemaWithJSON ? InferStandardSchemaOutput<NonNullable<T["suspendSchema"]>> : unknown;
|
|
14
|
+
/** Extracts the optional resume payload type from a `BaseTool` subclass or Mastra `Tool`. */
|
|
15
|
+
export type ToolResume<T extends MastraToolSchemas> = NonNullable<T["resumeSchema"]> extends StandardSchemaWithJSON ? InferStandardSchemaOutput<NonNullable<T["resumeSchema"]>> : unknown;
|
|
16
|
+
/**
|
|
17
|
+
* Extracts the optional request context type from a `BaseTool` subclass or Mastra `Tool`.
|
|
18
|
+
* Infers from Tool's generic because PublicSchema also includes untyped JSON schemas.
|
|
19
|
+
*/
|
|
20
|
+
export type ToolRequestContext<T extends MastraToolSchemas> = T extends Omit<MastraTool<any, any, any, any, any, any, infer TRequestContext>, "execute"> ? TRequestContext : unknown;
|
|
21
|
+
/** Typed `execute` context parameter derived from a `BaseTool` subclass or Mastra `Tool`. */
|
|
22
|
+
export type ToolContext<T extends MastraToolSchemas> = ToolExecuteContext<ToolExecutionContext<ToolSuspend<T>, ToolResume<T>, ToolRequestContext<T>>, ToolRequestContext<T>>;
|
|
23
|
+
/** Static definition (`id`, `title`, `description`, and schemas) passed exclusively to `BaseTool(definition)`. */
|
|
24
|
+
export interface BaseToolSchemas<TInputSchema extends SchemaLike = undefined, TOutputSchema extends SchemaLike = undefined, TSuspendSchema extends SchemaLike = undefined, TResumeSchema extends SchemaLike = undefined, TRequestContextSchema extends SchemaLike = undefined> {
|
|
25
|
+
/** Unique identifier for the tool. Defaults to kebab-case of the concrete subclass name. */
|
|
26
|
+
id?: string;
|
|
27
|
+
/** Display name for UIs and MCP clients. Never sent to the model. */
|
|
28
|
+
title?: string;
|
|
29
|
+
/** Description of what the tool does. */
|
|
30
|
+
description: string;
|
|
31
|
+
/** Optional schema for validating input parameters. */
|
|
32
|
+
inputSchema?: TInputSchema;
|
|
33
|
+
/** Optional schema for validating output structure. */
|
|
34
|
+
outputSchema?: TOutputSchema;
|
|
35
|
+
/** Optional schema for suspend operation data. */
|
|
36
|
+
suspendSchema?: TSuspendSchema;
|
|
37
|
+
/** Optional schema for resume operation data. */
|
|
38
|
+
resumeSchema?: TResumeSchema;
|
|
39
|
+
/** Optional schema for validating request context values. */
|
|
40
|
+
requestContextSchema?: TRequestContextSchema;
|
|
41
|
+
}
|
|
42
|
+
/** Runtime options passed exclusively to `super(options)` inside a `BaseTool` subclass. */
|
|
43
|
+
export type BaseToolOptions<TInput = unknown, TOutput = unknown, TSuspend = unknown, TResume = unknown, TRequestContext = unknown> = Omit<ToolAction<TInput, TOutput, TSuspend, TResume, ToolExecutionContext<TSuspend, TResume, TRequestContext>, string, TRequestContext>, ToolStaticKeys | "execute" | "renderOnly">;
|
|
44
|
+
/** Constructor type returned by the `BaseTool(definition)` class factory. */
|
|
45
|
+
export type BaseToolConstructor<TInput = unknown, TOutput = unknown, TSuspend = unknown, TResume = unknown, TRequestContext = unknown> = abstract new (options?: BaseToolOptions<TInput, TOutput, TSuspend, TResume, TRequestContext>) => Omit<MastraTool<TInput, TOutput, TSuspend, TResume, ToolExecutionContext<TSuspend, TResume, TRequestContext>, string, TRequestContext>, "execute"> & {
|
|
46
|
+
execute(inputData: TInput, context?: ToolExecuteContext<ToolExecutionContext<TSuspend, TResume, TRequestContext>, TRequestContext>): Promise<TOutput | ValidationError>;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* NestJS-native Mastra tool base class factory.
|
|
50
|
+
*
|
|
51
|
+
* `BaseTool(definition)` owns exclusively the static definition (`id`, `title`,
|
|
52
|
+
* `description`, and schemas) so `execute(...)` preserves full, un-erased Mastra types
|
|
53
|
+
* while retaining Mastra's built-in `Tool` input/output/requestContext validation wrapper.
|
|
54
|
+
* `super(options)` owns exclusively runtime configuration (e.g. `requireApproval`,
|
|
55
|
+
* `beforeApproval`, `toModelOutput`, `mcp`, `providerOptions`) with full access to DI dependencies.
|
|
56
|
+
* Automatically derives `id` from the concrete subclass name in kebab-case unless overridden.
|
|
57
|
+
* @param definition - Static tool metadata and Zod/Standard schema contract
|
|
58
|
+
* @returns Abstract base class extending Mastra `Tool`
|
|
59
|
+
*/
|
|
60
|
+
export declare function BaseTool<TInputSchema extends SchemaLike = undefined, TOutputSchema extends SchemaLike = undefined, TSuspendSchema extends SchemaLike = undefined, TResumeSchema extends SchemaLike = undefined, TRequestContextSchema extends SchemaLike = undefined>(definition: BaseToolSchemas<TInputSchema, TOutputSchema, TSuspendSchema, TResumeSchema, TRequestContextSchema>): BaseToolConstructor<InferOptionalSchema<TInputSchema>, InferOptionalSchema<TOutputSchema>, InferOptionalSchema<TSuspendSchema>, InferOptionalSchema<TResumeSchema>, InferOptionalSchema<TRequestContextSchema>>;
|
|
61
|
+
export {};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Tool as MastraTool, } from "@mastra/core/tools";
|
|
2
|
+
import { kebabCase } from "lodash-es";
|
|
3
|
+
const BASE_TOOL_MARKER = Symbol("nest-mastra-base-tool");
|
|
4
|
+
/**
|
|
5
|
+
* NestJS-native Mastra tool base class factory.
|
|
6
|
+
*
|
|
7
|
+
* `BaseTool(definition)` owns exclusively the static definition (`id`, `title`,
|
|
8
|
+
* `description`, and schemas) so `execute(...)` preserves full, un-erased Mastra types
|
|
9
|
+
* while retaining Mastra's built-in `Tool` input/output/requestContext validation wrapper.
|
|
10
|
+
* `super(options)` owns exclusively runtime configuration (e.g. `requireApproval`,
|
|
11
|
+
* `beforeApproval`, `toModelOutput`, `mcp`, `providerOptions`) with full access to DI dependencies.
|
|
12
|
+
* Automatically derives `id` from the concrete subclass name in kebab-case unless overridden.
|
|
13
|
+
* @param definition - Static tool metadata and Zod/Standard schema contract
|
|
14
|
+
* @returns Abstract base class extending Mastra `Tool`
|
|
15
|
+
*/
|
|
16
|
+
export function BaseTool(definition) {
|
|
17
|
+
const MastraToolBase = MastraTool;
|
|
18
|
+
class GeneratedBaseTool extends MastraToolBase {
|
|
19
|
+
[BASE_TOOL_MARKER] = true;
|
|
20
|
+
constructor(options) {
|
|
21
|
+
const proto = new.target.prototype;
|
|
22
|
+
const delegate = {};
|
|
23
|
+
super({
|
|
24
|
+
...options,
|
|
25
|
+
id: definition.id ?? kebabCase(new.target.name),
|
|
26
|
+
title: definition.title,
|
|
27
|
+
description: definition.description,
|
|
28
|
+
inputSchema: definition.inputSchema,
|
|
29
|
+
outputSchema: definition.outputSchema,
|
|
30
|
+
suspendSchema: definition.suspendSchema,
|
|
31
|
+
resumeSchema: definition.resumeSchema,
|
|
32
|
+
requestContextSchema: definition.requestContextSchema,
|
|
33
|
+
execute: (inputData, context) => {
|
|
34
|
+
if (!delegate.execute) {
|
|
35
|
+
throw new Error("BaseTool execute delegate is not initialized");
|
|
36
|
+
}
|
|
37
|
+
return delegate.execute(inputData, context);
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
delegate.execute = (inputData, context) => proto.execute.call(this, inputData, context);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return GeneratedBaseTool;
|
|
44
|
+
}
|
|
45
|
+
Object.defineProperty(BaseTool, Symbol.hasInstance, {
|
|
46
|
+
value(instance) {
|
|
47
|
+
return (typeof instance === "object" &&
|
|
48
|
+
instance !== null &&
|
|
49
|
+
BASE_TOOL_MARKER in instance);
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
//# sourceMappingURL=base.tool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.tool.js","sourceRoot":"","sources":["../src/base.tool.ts"],"names":[],"mappings":"AAMA,OAAO,EACL,IAAI,IAAI,UAAU,GAKnB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,E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OAAO,iBAAiB,CAAC;AAC3B,CAAC;AAED,MAAM,CAAC,cAAc,CAAC,QAAQ,EAAE,MAAM,CAAC,WAAW,EAAE;IAClD,KAAK,CAAC,QAAiB;QACrB,OAAO,CACL,OAAO,QAAQ,KAAK,QAAQ;YAC5B,QAAQ,KAAK,IAAI;YACjB,gBAAgB,IAAI,QAAQ,CAC7B,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { InferPublicSchema, InferStandardSchemaOutput, PublicSchema, StandardSchemaWithJSON } from "@mastra/core/schema";
|
|
2
|
+
import { type AnyWorkflow, type DefaultEngineType, type Step, Workflow as MastraWorkflow, type WorkflowConfig, type WorkflowErrorCallbackInfo, type WorkflowFinishCallbackResult, type WorkflowStartCallbackInfo } from "@mastra/core/workflows";
|
|
3
|
+
type WorkflowStaticKeys = "id" | "description" | "inputSchema" | "outputSchema" | "stateSchema" | "requestContextSchema";
|
|
4
|
+
/** Extracts the validated input type from a `BaseWorkflow` subclass or Mastra `Workflow`. */
|
|
5
|
+
export type WorkflowInput<T extends AnyWorkflow> = InferStandardSchemaOutput<T["inputSchema"]>;
|
|
6
|
+
/** Extracts the validated output type from a `BaseWorkflow` subclass or Mastra `Workflow`. */
|
|
7
|
+
export type WorkflowOutput<T extends AnyWorkflow> = InferStandardSchemaOutput<T["outputSchema"]>;
|
|
8
|
+
/** Extracts the optional state type from a `BaseWorkflow` subclass or Mastra `Workflow`. */
|
|
9
|
+
export type WorkflowState<T extends AnyWorkflow> = NonNullable<T["stateSchema"]> extends StandardSchemaWithJSON ? InferStandardSchemaOutput<NonNullable<T["stateSchema"]>> : unknown;
|
|
10
|
+
/** Extracts the optional request context type from a `BaseWorkflow` subclass or Mastra `Workflow`. */
|
|
11
|
+
export type WorkflowRequestContext<T extends AnyWorkflow> = NonNullable<T["requestContextSchema"]> extends StandardSchemaWithJSON ? InferStandardSchemaOutput<NonNullable<T["requestContextSchema"]>> : unknown;
|
|
12
|
+
type InferOptionalSchema<TSchema extends PublicSchema | undefined> = TSchema extends PublicSchema ? InferPublicSchema<TSchema> : unknown;
|
|
13
|
+
/** Static definition (`id`, `description`, and schemas) passed exclusively to `BaseWorkflow(definition)`. */
|
|
14
|
+
export type BaseWorkflowSchemas<TInputSchema extends PublicSchema, TOutputSchema extends PublicSchema, TStateSchema extends PublicSchema | undefined = undefined, TRequestContextSchema extends PublicSchema | undefined = undefined> = Omit<Pick<WorkflowConfig<string, InferOptionalSchema<TStateSchema>, InferPublicSchema<TInputSchema>, InferPublicSchema<TOutputSchema>, Step[], InferOptionalSchema<TRequestContextSchema>>, WorkflowStaticKeys>, "id"> & {
|
|
15
|
+
id?: string;
|
|
16
|
+
inputSchema: TInputSchema;
|
|
17
|
+
outputSchema: TOutputSchema;
|
|
18
|
+
stateSchema?: TStateSchema;
|
|
19
|
+
requestContextSchema?: TRequestContextSchema;
|
|
20
|
+
};
|
|
21
|
+
/** Runtime options passed exclusively to `super(options)` inside a `BaseWorkflow` subclass. */
|
|
22
|
+
export type BaseWorkflowOptions = Omit<WorkflowConfig<string, any, any, any, Step[], any>, WorkflowStaticKeys | "mastra" | "steps">;
|
|
23
|
+
/** Optional lifecycle hooks subclasses may implement as class methods. */
|
|
24
|
+
export interface BaseWorkflowLifecycleHooks {
|
|
25
|
+
onStart?(info: WorkflowStartCallbackInfo): Promise<void> | void;
|
|
26
|
+
onFinish?(result: WorkflowFinishCallbackResult): Promise<void> | void;
|
|
27
|
+
onError?(errorInfo: WorkflowErrorCallbackInfo): Promise<void> | void;
|
|
28
|
+
}
|
|
29
|
+
/** Constructor type returned by the `BaseWorkflow(schemas)` class factory. */
|
|
30
|
+
export type BaseWorkflowConstructor<TInput, TOutput, TState = unknown, TRequestContext = unknown> = abstract new (options?: BaseWorkflowOptions) => MastraWorkflow<DefaultEngineType, Step[], string, TState, TInput, TOutput, TInput, TRequestContext, 1> & BaseWorkflowLifecycleHooks;
|
|
31
|
+
/**
|
|
32
|
+
* NestJS-native Mastra workflow base class factory directly extending Mastra `Workflow`.
|
|
33
|
+
*
|
|
34
|
+
* `BaseWorkflow(schemas)` owns exclusively the static definition (`Pick`: `id`, `description`,
|
|
35
|
+
* and schemas) so native Mastra builder methods (`this.then(...)`, `this.branch(...)`,
|
|
36
|
+
* `this.dowhile(...)`, `this.map(...)`, `createRun()`) preserve full, un-erased type checking.
|
|
37
|
+
* `super(options)` owns exclusively runtime configuration (`Omit`, e.g. `metadata`,
|
|
38
|
+
* `retryConfig`, `options`, `schedule`) with full access to DI dependencies.
|
|
39
|
+
* Subclasses may also implement optional `onStart`, `onFinish`, and `onError` class methods.
|
|
40
|
+
* Calling `this.commit()` at the end of the constructor shadows `then` on the instance
|
|
41
|
+
* so NestJS's async DI container does not treat the workflow as a JS Thenable.
|
|
42
|
+
* Automatically derives `id` from the concrete subclass name in kebab-case unless overridden.
|
|
43
|
+
* @param schemas - Static Zod/Standard schema contract
|
|
44
|
+
* @returns Abstract base class extending Mastra `Workflow`
|
|
45
|
+
*/
|
|
46
|
+
export declare function BaseWorkflow<TInputSchema extends PublicSchema, TOutputSchema extends PublicSchema, TStateSchema extends PublicSchema | undefined = undefined, TRequestContextSchema extends PublicSchema | undefined = undefined>(schemas: BaseWorkflowSchemas<TInputSchema, TOutputSchema, TStateSchema, TRequestContextSchema>): BaseWorkflowConstructor<InferPublicSchema<TInputSchema>, InferPublicSchema<TOutputSchema>, InferOptionalSchema<TStateSchema>, InferOptionalSchema<TRequestContextSchema>>;
|
|
47
|
+
export {};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { Workflow as MastraWorkflow, } from "@mastra/core/workflows";
|
|
2
|
+
import { kebabCase } from "lodash-es";
|
|
3
|
+
const BASE_WORKFLOW_MARKER = Symbol("nest-mastra-base-workflow");
|
|
4
|
+
/**
|
|
5
|
+
* Non-callable object assigned to `this.then` after `commit()` so:
|
|
6
|
+
* 1. JavaScript/NestJS async DI does not treat the workflow as a Promise Thenable, and
|
|
7
|
+
* 2. Metadata scanners calling `Reflect.getMetadata(key, instance[methodName])`
|
|
8
|
+
* (such as `@nest-boot/schedule`) safely receive `undefined` instead of throwing `TypeError`.
|
|
9
|
+
*/
|
|
10
|
+
const NON_THENABLE_SENTINEL = Object.freeze({});
|
|
11
|
+
/**
|
|
12
|
+
* NestJS-native Mastra workflow base class factory directly extending Mastra `Workflow`.
|
|
13
|
+
*
|
|
14
|
+
* `BaseWorkflow(schemas)` owns exclusively the static definition (`Pick`: `id`, `description`,
|
|
15
|
+
* and schemas) so native Mastra builder methods (`this.then(...)`, `this.branch(...)`,
|
|
16
|
+
* `this.dowhile(...)`, `this.map(...)`, `createRun()`) preserve full, un-erased type checking.
|
|
17
|
+
* `super(options)` owns exclusively runtime configuration (`Omit`, e.g. `metadata`,
|
|
18
|
+
* `retryConfig`, `options`, `schedule`) with full access to DI dependencies.
|
|
19
|
+
* Subclasses may also implement optional `onStart`, `onFinish`, and `onError` class methods.
|
|
20
|
+
* Calling `this.commit()` at the end of the constructor shadows `then` on the instance
|
|
21
|
+
* so NestJS's async DI container does not treat the workflow as a JS Thenable.
|
|
22
|
+
* Automatically derives `id` from the concrete subclass name in kebab-case unless overridden.
|
|
23
|
+
* @param schemas - Static Zod/Standard schema contract
|
|
24
|
+
* @returns Abstract base class extending Mastra `Workflow`
|
|
25
|
+
*/
|
|
26
|
+
export function BaseWorkflow(schemas) {
|
|
27
|
+
class GeneratedBaseWorkflow extends MastraWorkflow {
|
|
28
|
+
[BASE_WORKFLOW_MARKER] = true;
|
|
29
|
+
constructor(runtimeOptions) {
|
|
30
|
+
const id = schemas.id ?? kebabCase(new.target.name);
|
|
31
|
+
const mergedOptions = {
|
|
32
|
+
...runtimeOptions?.options,
|
|
33
|
+
};
|
|
34
|
+
const proto = new.target.prototype;
|
|
35
|
+
const instanceRef = {};
|
|
36
|
+
if (typeof proto.onStart === "function") {
|
|
37
|
+
const externalOnStart = mergedOptions.onStart;
|
|
38
|
+
mergedOptions.onStart = async (info) => {
|
|
39
|
+
await externalOnStart?.(info);
|
|
40
|
+
await instanceRef.current?.onStart?.(info);
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (typeof proto.onFinish === "function") {
|
|
44
|
+
const externalOnFinish = mergedOptions.onFinish;
|
|
45
|
+
mergedOptions.onFinish = async (result) => {
|
|
46
|
+
await externalOnFinish?.(result);
|
|
47
|
+
await instanceRef.current?.onFinish?.(result);
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
if (typeof proto.onError === "function") {
|
|
51
|
+
const externalOnError = mergedOptions.onError;
|
|
52
|
+
mergedOptions.onError = async (errorInfo) => {
|
|
53
|
+
await externalOnError?.(errorInfo);
|
|
54
|
+
await instanceRef.current?.onError?.(errorInfo);
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
super({
|
|
58
|
+
...runtimeOptions,
|
|
59
|
+
id,
|
|
60
|
+
description: schemas.description,
|
|
61
|
+
inputSchema: schemas.inputSchema,
|
|
62
|
+
outputSchema: schemas.outputSchema,
|
|
63
|
+
...(schemas.stateSchema ? { stateSchema: schemas.stateSchema } : {}),
|
|
64
|
+
...(schemas.requestContextSchema
|
|
65
|
+
? { requestContextSchema: schemas.requestContextSchema }
|
|
66
|
+
: {}),
|
|
67
|
+
options: mergedOptions,
|
|
68
|
+
});
|
|
69
|
+
instanceRef.current = this;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Commits the workflow graph and disables `then` on this instance to prevent
|
|
73
|
+
* JavaScript/NestJS async DI Thenable resolution hangs.
|
|
74
|
+
* @returns This committed workflow instance
|
|
75
|
+
*/
|
|
76
|
+
commit() {
|
|
77
|
+
const result = super.commit();
|
|
78
|
+
Object.defineProperty(this, "then", {
|
|
79
|
+
value: NON_THENABLE_SENTINEL,
|
|
80
|
+
configurable: true,
|
|
81
|
+
enumerable: false,
|
|
82
|
+
writable: true,
|
|
83
|
+
});
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return GeneratedBaseWorkflow;
|
|
88
|
+
}
|
|
89
|
+
Object.defineProperty(BaseWorkflow, Symbol.hasInstance, {
|
|
90
|
+
value(instance) {
|
|
91
|
+
return (typeof instance === "object" &&
|
|
92
|
+
instance !== null &&
|
|
93
|
+
BASE_WORKFLOW_MARKER in instance);
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
//# sourceMappingURL=base.workflow.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.workflow.js","sourceRoot":"","sources":["../src/base.workflow.ts"],"names":[],"mappings":"AAMA,OAAO,EAIL,QAAQ,IAAI,cAAc,GAM3B,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,MAAM,oBAAoB,GAAG,MAAM,CAAC,2BAA2B,CAAC,CAAC;AACjE;;;;;GAKG;AACH,MAAM,qBAAqB,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;AAgGhD;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,YAAY,CAM1B,OAKC;IAYD,MAAe,qBACb,SAAQ,cAUP;QAGQ,CAAC,oBAAoB,CAAC,GAAG,IAAI,CAAC;QAMvC,YAAY,cAAoC;YAC9C,MAAM,EAAE,GAAG,OAAO,CAAC,EAAE,IAAI,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YACpD,MAAM,aAAa,GAAoB;gBACrC,GAAG,cAAc,EAAE,OAAO;aAC3B,CAAC;YACF,MAAM,KAAK,GAAG,GAAG,CAAC,MAAM,CAAC,SAAuC,CAAC;YACjE,MAAM,WAAW,GAAwC,EAAE,CAAC;YAE5D,IAAI,OAAO,KAAK,CAAC,OAAO,KAAK,UAAU,EAAE,CAAC;gBACxC,MAAM,eAAe,GAAG,aAAa,CAAC,OAAO,CAAC;gBAC9C,aAAa,CAAC,OAAO,GAAG,KAAK,EAAE,IAAI,E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C,CAAC,EAAE,CAAC;gBACP,OAAO,EAAE,aAAa;aACvB,CAAC,CAAC;YAEH,WAAW,CAAC,OAAO,GAAG,IAAI,CAAC;QAC7B,CAAC;QAED;;;;WAIG;QACM,MAAM;YACb,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;YAE9B,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE;gBAClC,KAAK,EAAE,qBAAqB;gBAC5B,YAAY,EAAE,IAAI;gBAClB,UAAU,EAAE,KAAK;gBACjB,QAAQ,EAAE,IAAI;aACf,CAAC,CAAC;YAEH,OAAO,MAAM,CAAC;QAChB,CAAC;KACF;IAED,OAAO,qBAAqB,CAAC;AAC/B,CAAC;AAED,MAAM,CAAC,cAAc,CAAC,YAAY,EAAE,MAAM,CAAC,WAAW,EAAE;IACtD,KAAK,CAAC,QAAiB;QACrB,OAAO,CACL,OAAO,QAAQ,KAAK,QAAQ;YAC5B,QAAQ,KAAK,IAAI;YACjB,oBAAoB,IAAI,QAAQ,CACjC,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { BaseAgent, type BaseAgentConfig } from "./base.agent.js";
|
|
2
|
+
export { type AnyMastraStep, BaseStep, type BaseStepConstructor, type BaseStepOptions, type BaseStepSchemas, type StepExecuteParams, type StepInput, type StepOutput, type StepRequestContext, type StepResume, type StepState, type StepSuspend, } from "./base.step.js";
|
|
3
|
+
export { type AnyMastraTool, BaseTool, type BaseToolConstructor, type BaseToolOptions, type BaseToolSchemas, type ToolContext, type ToolInput, type ToolOutput, type ToolRequestContext, type ToolResume, type ToolSuspend, } from "./base.tool.js";
|
|
4
|
+
export { BaseWorkflow, type BaseWorkflowConstructor, type BaseWorkflowLifecycleHooks, type BaseWorkflowOptions, type BaseWorkflowSchemas, type WorkflowInput, type WorkflowOutput, type WorkflowRequestContext, type WorkflowState, } from "./base.workflow.js";
|
|
5
|
+
export { MastraController } from "./mastra.controller.js";
|
|
6
|
+
export { MastraManager } from "./mastra.manager.js";
|
|
7
|
+
export { MastraModule, type MastraModuleAsyncOptions, } from "./mastra.module.js";
|
|
8
|
+
export { DEFAULT_MASTRA_API_PREFIX, MASTRA_MODULE_OPTIONS, type MastraApiOptions, type MastraModuleOptions, type MastraServerModuleOptions, type MastraStudioOptions, normalizeMastraApiOptions, normalizeMastraStudioOptions, resolveMastraApiPrefix, resolveMastraStudioBasePath, } from "./mastra.module-definition.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { BaseAgent } from "./base.agent.js";
|
|
2
|
+
export { BaseStep, } from "./base.step.js";
|
|
3
|
+
export { BaseTool, } from "./base.tool.js";
|
|
4
|
+
export { BaseWorkflow, } from "./base.workflow.js";
|
|
5
|
+
export { MastraController } from "./mastra.controller.js";
|
|
6
|
+
export { MastraManager } from "./mastra.manager.js";
|
|
7
|
+
export { MastraModule, } from "./mastra.module.js";
|
|
8
|
+
export { DEFAULT_MASTRA_API_PREFIX, MASTRA_MODULE_OPTIONS, normalizeMastraApiOptions, normalizeMastraStudioOptions, resolveMastraApiPrefix, resolveMastraStudioBasePath, } from "./mastra.module-definition.js";
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAwB,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAEL,QAAQ,GAWT,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAEL,QAAQ,GAUT,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,YAAY,GASb,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EACL,YAAY,GAEb,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,yBAAyB,EACzB,qBAAqB,EAKrB,yBAAyB,EACzB,4BAA4B,EAC5B,sBAAsB,EACtB,2BAA2B,GAC5B,MAAM,+BAA+B,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type NestMiddleware, type OnApplicationBootstrap } from "@nestjs/common";
|
|
2
|
+
import { ApplicationConfig } from "@nestjs/core";
|
|
3
|
+
import { type NextFunction, type Request, type Response } from "express";
|
|
4
|
+
import { type MastraModuleOptions } from "./mastra.module-definition.js";
|
|
5
|
+
/**
|
|
6
|
+
* Resolves the bundled `mastra/dist/studio` directory path when the optional
|
|
7
|
+
* `mastra` peer dependency is installed.
|
|
8
|
+
* @param resolvePackage - Package specifier resolver (overridable for tests)
|
|
9
|
+
* @returns Absolute path to the Mastra Studio distribution directory, or `null` when unavailable
|
|
10
|
+
*/
|
|
11
|
+
export declare function resolveMastraStudioDistDir(resolvePackage?: (specifier: string) => string): string | null;
|
|
12
|
+
/**
|
|
13
|
+
* Express-backed NestJS middleware serving bundled Mastra Studio assets and SPA HTML
|
|
14
|
+
* at `${prefix}/studio` without requiring route-level authentication (matching GraphQL Playground).
|
|
15
|
+
*/
|
|
16
|
+
export declare class MastraStudioMiddleware implements NestMiddleware, OnApplicationBootstrap {
|
|
17
|
+
private readonly options;
|
|
18
|
+
private readonly applicationConfig?;
|
|
19
|
+
private readonly logger;
|
|
20
|
+
private router;
|
|
21
|
+
constructor(options: MastraModuleOptions, applicationConfig?: ApplicationConfig | undefined);
|
|
22
|
+
/**
|
|
23
|
+
* Pre-builds the Studio router once NestJS has finished applying global prefix options,
|
|
24
|
+
* avoiding synchronous file I/O on the first incoming HTTP request.
|
|
25
|
+
*/
|
|
26
|
+
onApplicationBootstrap(): void;
|
|
27
|
+
private ensureRouter;
|
|
28
|
+
/**
|
|
29
|
+
* Builds the Studio router after the host has configured its global prefix.
|
|
30
|
+
* @returns Mounted Studio router, or null when Studio is unavailable
|
|
31
|
+
*/
|
|
32
|
+
private createRouter;
|
|
33
|
+
/**
|
|
34
|
+
* Delegates incoming Studio requests to the mounted Express router.
|
|
35
|
+
* @param req - Incoming Express request
|
|
36
|
+
* @param res - Outgoing Express response
|
|
37
|
+
* @param next - Next middleware callback
|
|
38
|
+
*/
|
|
39
|
+
use(req: Request, res: Response, next: NextFunction): void;
|
|
40
|
+
}
|