@mastra/mcp-docs-server 1.1.37-alpha.1 → 1.1.37-alpha.4
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/.docs/docs/agents/processors.md +10 -0
- package/.docs/docs/deployment/cloud-providers.md +1 -0
- package/.docs/docs/mastra-platform/observability.md +31 -1
- package/.docs/docs/memory/observational-memory.md +34 -0
- package/.docs/docs/voice/overview.md +76 -0
- package/.docs/guides/deployment/aws-bedrock-agentcore.md +432 -0
- package/.docs/models/index.md +1 -1
- package/.docs/models/providers/xpersona.md +71 -0
- package/.docs/models/providers.md +1 -0
- package/.docs/reference/cli/mastra.md +114 -3
- package/.docs/reference/index.md +1 -0
- package/.docs/reference/processors/processor-interface.md +148 -71
- package/.docs/reference/voice/xai-realtime.md +267 -0
- package/.docs/reference/workspace/docker-sandbox.md +60 -0
- package/CHANGELOG.md +14 -0
- package/dist/stdio.js +5 -5
- package/dist/stdio.js.map +1 -1
- package/package.json +5 -5
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Xpersona
|
|
2
|
+
|
|
3
|
+
Access 1 Xpersona model through Mastra's model router. Authentication is handled automatically using the `XPERSONA_API_KEY` environment variable.
|
|
4
|
+
|
|
5
|
+
Learn more in the [Xpersona documentation](https://xpersona.co/docs).
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
XPERSONA_API_KEY=your-api-key
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
import { Agent } from "@mastra/core/agent";
|
|
13
|
+
|
|
14
|
+
const agent = new Agent({
|
|
15
|
+
id: "my-agent",
|
|
16
|
+
name: "My Agent",
|
|
17
|
+
instructions: "You are a helpful assistant",
|
|
18
|
+
model: "xpersona/xpersona-frieren-coder"
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// Generate a response
|
|
22
|
+
const response = await agent.generate("Hello!");
|
|
23
|
+
|
|
24
|
+
// Stream a response
|
|
25
|
+
const stream = await agent.stream("Tell me a story");
|
|
26
|
+
for await (const chunk of stream) {
|
|
27
|
+
console.log(chunk);
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
> **Info:** Mastra uses the OpenAI-compatible `/chat/completions` endpoint. Some provider-specific features may not be available. Check the [Xpersona documentation](https://xpersona.co/docs) for details.
|
|
32
|
+
|
|
33
|
+
## Models
|
|
34
|
+
|
|
35
|
+
| Model | Context | Tools | Reasoning | Image | Audio | Video | Input $/1M | Output $/1M |
|
|
36
|
+
| --------------------------------- | ------- | ----- | --------- | ----- | ----- | ----- | ---------- | ----------- |
|
|
37
|
+
| `xpersona/xpersona-frieren-coder` | 128K | | | | | | $2 | $6 |
|
|
38
|
+
|
|
39
|
+
## Advanced configuration
|
|
40
|
+
|
|
41
|
+
### Custom headers
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
const agent = new Agent({
|
|
45
|
+
id: "custom-agent",
|
|
46
|
+
name: "custom-agent",
|
|
47
|
+
model: {
|
|
48
|
+
url: "https://xpersona.co/v1",
|
|
49
|
+
id: "xpersona/xpersona-frieren-coder",
|
|
50
|
+
apiKey: process.env.XPERSONA_API_KEY,
|
|
51
|
+
headers: {
|
|
52
|
+
"X-Custom-Header": "value"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Dynamic model selection
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
const agent = new Agent({
|
|
62
|
+
id: "dynamic-agent",
|
|
63
|
+
name: "Dynamic Agent",
|
|
64
|
+
model: ({ requestContext }) => {
|
|
65
|
+
const useAdvanced = requestContext.task === "complex";
|
|
66
|
+
return useAdvanced
|
|
67
|
+
? "xpersona/xpersona-frieren-coder"
|
|
68
|
+
: "xpersona/xpersona-frieren-coder";
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
```
|
|
@@ -105,6 +105,7 @@ Direct access to individual AI model providers. Each provider offers unique mode
|
|
|
105
105
|
- [Xiaomi Token Plan (China)](https://mastra.ai/models/providers/xiaomi-token-plan-cn)
|
|
106
106
|
- [Xiaomi Token Plan (Europe)](https://mastra.ai/models/providers/xiaomi-token-plan-ams)
|
|
107
107
|
- [Xiaomi Token Plan (Singapore)](https://mastra.ai/models/providers/xiaomi-token-plan-sgp)
|
|
108
|
+
- [Xpersona](https://mastra.ai/models/providers/xpersona)
|
|
108
109
|
- [Z.AI](https://mastra.ai/models/providers/zai)
|
|
109
110
|
- [Z.AI Coding Plan](https://mastra.ai/models/providers/zai-coding-plan)
|
|
110
111
|
- [ZenMux](https://mastra.ai/models/providers/zenmux)
|
|
@@ -600,12 +600,13 @@ It accepts [common flags](#common-flags).
|
|
|
600
600
|
|
|
601
601
|
## `mastra api`
|
|
602
602
|
|
|
603
|
-
Calls a Mastra runtime server with JSON input and JSON output. Use it for local development servers, deployed Mastra platform projects,
|
|
603
|
+
Calls a Mastra runtime server with JSON input and JSON output. Use it for local development servers, deployed Mastra platform projects, self-hosted Mastra servers, or hosted Mastra Platform Observability APIs.
|
|
604
604
|
|
|
605
605
|
```bash
|
|
606
606
|
mastra api agent list --pretty
|
|
607
607
|
mastra api agent run weather-agent '{"messages":"What is the weather in London?"}'
|
|
608
608
|
mastra api tool execute get-weather '{"location":"San Francisco"}'
|
|
609
|
+
mastra api trace list '{"page":0,"perPage":20}' --pretty
|
|
609
610
|
```
|
|
610
611
|
|
|
611
612
|
Use `mastra api <resource> <action> --help` to see examples for a command.
|
|
@@ -638,7 +639,7 @@ Errors are written to `stderr` as JSON and return a non-zero exit code:
|
|
|
638
639
|
|
|
639
640
|
### Target resolution
|
|
640
641
|
|
|
641
|
-
|
|
642
|
+
For runtime commands, the command resolves the target server in this order:
|
|
642
643
|
|
|
643
644
|
1. `--url <url>` for an explicit remote or self-hosted server.
|
|
644
645
|
2. `http://localhost:4111` for a local `mastra dev` server.
|
|
@@ -646,6 +647,15 @@ The command resolves the target server in this order:
|
|
|
646
647
|
|
|
647
648
|
Automatic platform auth is only used when the CLI resolves a Mastra platform target from `.mastra-project.json`. Localhost targets and explicit `--url` targets don't receive automatic credentials. Headers passed with `--header` are sent to any target, including localhost.
|
|
648
649
|
|
|
650
|
+
For observability commands (`trace`, `log`, `score`, and `metric`), the CLI targets `https://observability.mastra.ai` by default instead of a project deployment URL. It resolves credentials in this order:
|
|
651
|
+
|
|
652
|
+
1. Explicit `Authorization` and `X-Mastra-Project-Id` headers passed with `--header`.
|
|
653
|
+
2. `MASTRA_PLATFORM_ACCESS_TOKEN` and `MASTRA_PROJECT_ID` from your environment.
|
|
654
|
+
3. Project metadata from `.mastra-project.json` for the project ID.
|
|
655
|
+
4. Your Mastra CLI login token as an auth fallback.
|
|
656
|
+
|
|
657
|
+
Use `--url` and `--header` when you need to override the default hosted observability target or credentials.
|
|
658
|
+
|
|
649
659
|
### Flags
|
|
650
660
|
|
|
651
661
|
#### `--url <url>`
|
|
@@ -701,6 +711,13 @@ List commands accept `page` and `perPage` in the JSON input when the target rout
|
|
|
701
711
|
|
|
702
712
|
```bash
|
|
703
713
|
mastra api score list '{"page":0,"perPage":50}'
|
|
714
|
+
mastra api trace list '{"page":0,"perPage":20}'
|
|
715
|
+
```
|
|
716
|
+
|
|
717
|
+
Routes that support filters accept them in the same JSON input. For example, observability trace listing supports pagination and route-supported filters:
|
|
718
|
+
|
|
719
|
+
```bash
|
|
720
|
+
mastra api trace list '{"page":0,"perPage":20,"filters":{"spanType":"agent"}}' --pretty
|
|
704
721
|
```
|
|
705
722
|
|
|
706
723
|
### Get command-specific help
|
|
@@ -956,14 +973,26 @@ Lists observability traces. Pass optional JSON input for route-supported filters
|
|
|
956
973
|
|
|
957
974
|
```bash
|
|
958
975
|
mastra api trace list [input]
|
|
976
|
+
mastra api trace list '{"page":0,"perPage":20}' --pretty
|
|
959
977
|
```
|
|
960
978
|
|
|
979
|
+
`trace list` returns full root span records. For debugging large traces without overfetching, use `trace get` first and then fetch a specific span with `trace span` only when you need full span payloads.
|
|
980
|
+
|
|
961
981
|
#### `mastra api trace get`
|
|
962
982
|
|
|
963
|
-
Gets one observability trace
|
|
983
|
+
Gets a lightweight timeline for one observability trace without fetching full span input, output, attributes, or metadata payloads. Pass `--verbose` to fetch the full trace payload.
|
|
964
984
|
|
|
965
985
|
```bash
|
|
966
986
|
mastra api trace get <traceId>
|
|
987
|
+
mastra api trace get <traceId> --verbose
|
|
988
|
+
```
|
|
989
|
+
|
|
990
|
+
#### `mastra api trace span`
|
|
991
|
+
|
|
992
|
+
Gets one full span from an observability trace. Use this after `trace get` when you know which span you need to inspect.
|
|
993
|
+
|
|
994
|
+
```bash
|
|
995
|
+
mastra api trace span <traceId> <spanId>
|
|
967
996
|
```
|
|
968
997
|
|
|
969
998
|
#### `mastra api log list`
|
|
@@ -974,6 +1003,88 @@ Lists observability logs. Pass optional JSON input for route-supported filters o
|
|
|
974
1003
|
mastra api log list [input]
|
|
975
1004
|
```
|
|
976
1005
|
|
|
1006
|
+
#### `mastra api metric aggregate`
|
|
1007
|
+
|
|
1008
|
+
Gets a single aggregate metric value.
|
|
1009
|
+
|
|
1010
|
+
```bash
|
|
1011
|
+
mastra api metric aggregate '{"name":["latency_ms"],"aggregation":"avg"}'
|
|
1012
|
+
```
|
|
1013
|
+
|
|
1014
|
+
#### `mastra api metric breakdown`
|
|
1015
|
+
|
|
1016
|
+
Gets metric values grouped by a label or field.
|
|
1017
|
+
|
|
1018
|
+
```bash
|
|
1019
|
+
mastra api metric breakdown '{"name":["latency_ms"],"aggregation":"avg","groupBy":["model"],"limit":10}'
|
|
1020
|
+
```
|
|
1021
|
+
|
|
1022
|
+
#### `mastra api metric timeseries`
|
|
1023
|
+
|
|
1024
|
+
Gets metric values over time.
|
|
1025
|
+
|
|
1026
|
+
```bash
|
|
1027
|
+
mastra api metric timeseries '{"name":["latency_ms"],"aggregation":"avg","interval":"1h"}'
|
|
1028
|
+
```
|
|
1029
|
+
|
|
1030
|
+
#### `mastra api metric percentiles`
|
|
1031
|
+
|
|
1032
|
+
Gets metric percentile values over time. Percentile values use decimals from `0` to `1`.
|
|
1033
|
+
|
|
1034
|
+
```bash
|
|
1035
|
+
mastra api metric percentiles '{"name":"latency_ms","percentiles":[0.5,0.95,0.99],"interval":"1h"}'
|
|
1036
|
+
```
|
|
1037
|
+
|
|
1038
|
+
#### `mastra api metric names`
|
|
1039
|
+
|
|
1040
|
+
Lists discovered metric names. Pass optional JSON input for prefix search and limit.
|
|
1041
|
+
|
|
1042
|
+
```bash
|
|
1043
|
+
mastra api metric names '{"prefix":"lat","limit":10}'
|
|
1044
|
+
```
|
|
1045
|
+
|
|
1046
|
+
#### `mastra api metric label-keys`
|
|
1047
|
+
|
|
1048
|
+
Lists label keys for a metric.
|
|
1049
|
+
|
|
1050
|
+
```bash
|
|
1051
|
+
mastra api metric label-keys '{"metricName":"latency_ms"}'
|
|
1052
|
+
```
|
|
1053
|
+
|
|
1054
|
+
#### `mastra api metric label-values`
|
|
1055
|
+
|
|
1056
|
+
Lists label values for a metric label key. Pass optional prefix and limit values to narrow the result.
|
|
1057
|
+
|
|
1058
|
+
```bash
|
|
1059
|
+
mastra api metric label-values '{"metricName":"latency_ms","labelKey":"model","prefix":"g","limit":10}'
|
|
1060
|
+
```
|
|
1061
|
+
|
|
1062
|
+
#### Observability with `curl`
|
|
1063
|
+
|
|
1064
|
+
You can call the hosted observability API directly with your platform access token and project ID:
|
|
1065
|
+
|
|
1066
|
+
```bash
|
|
1067
|
+
curl -sS "https://observability.mastra.ai/api/observability/traces?page=0&perPage=20" \
|
|
1068
|
+
-H "Authorization: Bearer $MASTRA_PLATFORM_ACCESS_TOKEN" \
|
|
1069
|
+
-H "X-Mastra-Project-Id: $MASTRA_PROJECT_ID" | jq
|
|
1070
|
+
```
|
|
1071
|
+
|
|
1072
|
+
Get a lightweight trace timeline:
|
|
1073
|
+
|
|
1074
|
+
```bash
|
|
1075
|
+
curl -sS "https://observability.mastra.ai/api/observability/traces/<trace-id>/light" \
|
|
1076
|
+
-H "Authorization: Bearer $MASTRA_PLATFORM_ACCESS_TOKEN" \
|
|
1077
|
+
-H "X-Mastra-Project-Id: $MASTRA_PROJECT_ID" | jq
|
|
1078
|
+
```
|
|
1079
|
+
|
|
1080
|
+
Get a specific span:
|
|
1081
|
+
|
|
1082
|
+
```bash
|
|
1083
|
+
curl -sS "https://observability.mastra.ai/api/observability/traces/<trace-id>/spans/<span-id>" \
|
|
1084
|
+
-H "Authorization: Bearer $MASTRA_PLATFORM_ACCESS_TOKEN" \
|
|
1085
|
+
-H "X-Mastra-Project-Id: $MASTRA_PROJECT_ID" | jq
|
|
1086
|
+
```
|
|
1087
|
+
|
|
977
1088
|
#### `mastra api score create`
|
|
978
1089
|
|
|
979
1090
|
Creates an observability score. The input uses the server score body shape; inspect it with `--schema`.
|
package/.docs/reference/index.md
CHANGED
|
@@ -267,6 +267,7 @@ The Reference section provides documentation of Mastra's API, including paramete
|
|
|
267
267
|
- [PlayAI](https://mastra.ai/reference/voice/playai)
|
|
268
268
|
- [Sarvam](https://mastra.ai/reference/voice/sarvam)
|
|
269
269
|
- [Speechify](https://mastra.ai/reference/voice/speechify)
|
|
270
|
+
- [xAI Realtime](https://mastra.ai/reference/voice/xai-realtime)
|
|
270
271
|
- [.addInstructions()](https://mastra.ai/reference/voice/voice.addInstructions)
|
|
271
272
|
- [.addTools()](https://mastra.ai/reference/voice/voice.addTools)
|
|
272
273
|
- [.answer()](https://mastra.ai/reference/voice/voice.answer)
|
|
@@ -4,75 +4,83 @@ The `Processor` interface defines the contract for all processors in Mastra. Pro
|
|
|
4
4
|
|
|
5
5
|
## When processor methods run
|
|
6
6
|
|
|
7
|
-
The
|
|
7
|
+
The eight processor methods run at different points in the agent execution lifecycle:
|
|
8
8
|
|
|
9
9
|
```text
|
|
10
|
-
|
|
11
|
-
│
|
|
12
|
-
|
|
13
|
-
│
|
|
14
|
-
│ User Input
|
|
15
|
-
│ │
|
|
16
|
-
│ ▼
|
|
17
|
-
│
|
|
18
|
-
│ │ processInput
|
|
19
|
-
│
|
|
20
|
-
│
|
|
21
|
-
│
|
|
22
|
-
│
|
|
23
|
-
│ │
|
|
24
|
-
│ │
|
|
25
|
-
│ │ │
|
|
26
|
-
│ │
|
|
27
|
-
│ │
|
|
28
|
-
│ │
|
|
29
|
-
│ │
|
|
30
|
-
│ │ │
|
|
31
|
-
│ │
|
|
32
|
-
│ │
|
|
33
|
-
│ │
|
|
34
|
-
│ │
|
|
35
|
-
│ │
|
|
36
|
-
│ │
|
|
37
|
-
│ │
|
|
38
|
-
│ │
|
|
39
|
-
│ │
|
|
40
|
-
│ │
|
|
41
|
-
│ │
|
|
42
|
-
│ │ │
|
|
43
|
-
│ │
|
|
44
|
-
│ │
|
|
45
|
-
│ │
|
|
46
|
-
│ │
|
|
47
|
-
│ │ │
|
|
48
|
-
│ │
|
|
49
|
-
│ │
|
|
50
|
-
│ │
|
|
51
|
-
│ │
|
|
52
|
-
│ │
|
|
53
|
-
│ │
|
|
54
|
-
│
|
|
55
|
-
│
|
|
56
|
-
│
|
|
57
|
-
│
|
|
58
|
-
│ │
|
|
59
|
-
│
|
|
60
|
-
│
|
|
61
|
-
│
|
|
62
|
-
│
|
|
63
|
-
│
|
|
64
|
-
|
|
10
|
+
┌────────────────────────────────────────────────────────────────────┐
|
|
11
|
+
│ Agent Execution Flow │
|
|
12
|
+
├────────────────────────────────────────────────────────────────────┤
|
|
13
|
+
│ │
|
|
14
|
+
│ User Input │
|
|
15
|
+
│ │ │
|
|
16
|
+
│ ▼ │
|
|
17
|
+
│ ┌────────────────────────┐ │
|
|
18
|
+
│ │ processInput │ ← Runs ONCE at start │
|
|
19
|
+
│ └───────────┬────────────┘ │
|
|
20
|
+
│ │ │
|
|
21
|
+
│ ▼ │
|
|
22
|
+
│ ┌──────────────────────────────────────────────────────────────┐ │
|
|
23
|
+
│ │ Agentic Loop │ │
|
|
24
|
+
│ │ │ │
|
|
25
|
+
│ │ ┌────────────────────────┐ │ │
|
|
26
|
+
│ │ │ processInputStep │ ← Runs at EACH step │ │
|
|
27
|
+
│ │ └───────────┬────────────┘ │ │
|
|
28
|
+
│ │ │ │ │
|
|
29
|
+
│ │ ▼ │ │
|
|
30
|
+
│ │ ┌────────────────────────┐ │ │
|
|
31
|
+
│ │ │ processLLMRequest │ ← Before provider call │ │
|
|
32
|
+
│ │ └───────────┬────────────┘ │ │
|
|
33
|
+
│ │ │ │ │
|
|
34
|
+
│ │ ▼ │ │
|
|
35
|
+
│ │ LLM Execution ──── API Error? ───┐ │ │
|
|
36
|
+
│ │ │ │ │ │
|
|
37
|
+
│ │ │ ┌───────────┴──────────┐ │ │
|
|
38
|
+
│ │ │ │ processAPIError │ │ │
|
|
39
|
+
│ │ │ └──────────────────────┘ │ │
|
|
40
|
+
│ │ │ (retry loops back to LLM) │ │
|
|
41
|
+
│ │ ▼ │ │
|
|
42
|
+
│ │ ┌────────────────────────┐ │ │
|
|
43
|
+
│ │ │ processOutputStream │ ← Runs on EACH stream chunk │ │
|
|
44
|
+
│ │ └───────────┬────────────┘ │ │
|
|
45
|
+
│ │ │ │ │
|
|
46
|
+
│ │ ▼ │ │
|
|
47
|
+
│ │ ┌────────────────────────┐ │ │
|
|
48
|
+
│ │ │ processLLMResponse │ ← After stream completes │ │
|
|
49
|
+
│ │ └───────────┬────────────┘ │ │
|
|
50
|
+
│ │ │ │ │
|
|
51
|
+
│ │ ▼ │ │
|
|
52
|
+
│ │ ┌────────────────────────┐ │ │
|
|
53
|
+
│ │ │ processOutputStep │ ← Runs after EACH LLM step │ │
|
|
54
|
+
│ │ └───────────┬────────────┘ │ │
|
|
55
|
+
│ │ │ │ │
|
|
56
|
+
│ │ ▼ │ │
|
|
57
|
+
│ │ Tool Execution (if needed) │ │
|
|
58
|
+
│ │ │ │ │
|
|
59
|
+
│ │ └──────── Loop back if tools called ────────────│ │
|
|
60
|
+
│ │ │ │
|
|
61
|
+
│ └──────────────────────────────────────────────────────────────┘ │
|
|
62
|
+
│ │ │
|
|
63
|
+
│ ▼ │
|
|
64
|
+
│ ┌────────────────────────┐ │
|
|
65
|
+
│ │ processOutputResult │ ← Runs ONCE after completion │
|
|
66
|
+
│ └────────────────────────┘ │
|
|
67
|
+
│ │ │
|
|
68
|
+
│ ▼ │
|
|
69
|
+
│ Final Response │
|
|
70
|
+
│ │
|
|
71
|
+
└────────────────────────────────────────────────────────────────────┘
|
|
65
72
|
```
|
|
66
73
|
|
|
67
|
-
| Method | When it runs
|
|
68
|
-
| --------------------- |
|
|
69
|
-
| `processInput` | Once at the start, before the agentic loop
|
|
70
|
-
| `processInputStep` | At each step of the agentic loop, before each LLM call
|
|
71
|
-
| `processLLMRequest` | After LLM request conversion, before the provider call
|
|
72
|
-
| `processAPIError` | When an LLM API call fails
|
|
73
|
-
| `processOutputStream` | On each streaming chunk during LLM response
|
|
74
|
-
| `
|
|
75
|
-
| `
|
|
74
|
+
| Method | When it runs | Use case |
|
|
75
|
+
| --------------------- | ------------------------------------------------------------ | ---------------------------------------------------------------------------------------------- |
|
|
76
|
+
| `processInput` | Once at the start, before the agentic loop | Validate/transform initial user input, add context |
|
|
77
|
+
| `processInputStep` | At each step of the agentic loop, before each LLM call | Transform messages between steps, handle tool results |
|
|
78
|
+
| `processLLMRequest` | After LLM request conversion, before the provider call | Rewrite the outbound `LanguageModelV2Prompt` for the current call without persisting changes |
|
|
79
|
+
| `processAPIError` | When an LLM API call fails | Inspect API rejections, optionally mutate state/messages, and request a retry |
|
|
80
|
+
| `processOutputStream` | On each streaming chunk during LLM response | Filter/modify streaming content, detect patterns in real-time |
|
|
81
|
+
| `processLLMResponse` | After the LLM step completes and stream chunks are collected | Capture or cache the full response, run post-call side effects paired with `processLLMRequest` |
|
|
82
|
+
| `processOutputStep` | After each LLM response, before tool execution | Validate output quality, implement guardrails with retry |
|
|
83
|
+
| `processOutputResult` | Once after generation completes | Post-process final response, log results |
|
|
76
84
|
|
|
77
85
|
## Interface definition
|
|
78
86
|
|
|
@@ -107,6 +115,10 @@ interface Processor<TId extends string = string, TTripwireMetadata = unknown> {
|
|
|
107
115
|
args: ProcessLLMRequestArgs<TTripwireMetadata>,
|
|
108
116
|
): Promise<ProcessLLMRequestResult> | ProcessLLMRequestResult
|
|
109
117
|
|
|
118
|
+
processLLMResponse?(
|
|
119
|
+
args: ProcessLLMResponseArgs<TTripwireMetadata>,
|
|
120
|
+
): Promise<ProcessLLMResponseResult> | ProcessLLMResponseResult
|
|
121
|
+
|
|
110
122
|
processAPIError?(
|
|
111
123
|
args: ProcessAPIErrorArgs<TTripwireMetadata>,
|
|
112
124
|
): Promise<ProcessAPIErrorResult | void> | ProcessAPIErrorResult | void
|
|
@@ -255,8 +267,11 @@ processInputStep?<TTripwireMetadata = unknown>(
|
|
|
255
267
|
3. `prepareStep` callback (runs as part of the processInputStep pipeline, after inputProcessors)
|
|
256
268
|
4. `processLLMRequest` from inputProcessors (after prompt conversion, before the provider call)
|
|
257
269
|
5. LLM execution
|
|
258
|
-
6.
|
|
259
|
-
7.
|
|
270
|
+
6. `processOutputStream` from outputProcessors (on each streaming chunk)
|
|
271
|
+
7. `processLLMResponse` from inputProcessors (after stream completes, pairs with `processLLMRequest`)
|
|
272
|
+
8. `processOutputStep` from outputProcessors (after LLM response, before tool execution)
|
|
273
|
+
9. Tool execution (if needed)
|
|
274
|
+
10. Repeat from step 2 if tools were called
|
|
260
275
|
|
|
261
276
|
#### `ProcessInputStepArgs`
|
|
262
277
|
|
|
@@ -401,6 +416,62 @@ processLLMRequest?(
|
|
|
401
416
|
|
|
402
417
|
***
|
|
403
418
|
|
|
419
|
+
### `processLLMResponse`
|
|
420
|
+
|
|
421
|
+
Processes the LLM response after the step completes (or after a cached response is replayed) and after output processors have collected the response chunks. This hook pairs with `processLLMRequest`: use `processLLMRequest` to stash state (such as a cache key) before the provider call, and `processLLMResponse` to act on the completed response (such as writing it to a cache).
|
|
422
|
+
|
|
423
|
+
The `state` object is the same instance passed to `processLLMRequest` for the same step, so processors can correlate pre- and post-call work.
|
|
424
|
+
|
|
425
|
+
```typescript
|
|
426
|
+
processLLMResponse?(
|
|
427
|
+
args: ProcessLLMResponseArgs,
|
|
428
|
+
): Promise<ProcessLLMResponseResult> | ProcessLLMResponseResult;
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
#### `ProcessLLMResponseArgs`
|
|
432
|
+
|
|
433
|
+
**chunks** (`CachedLLMStepChunk[]`): Chunks produced by the LLM call (or replayed from cache) for this step, in stripped form (\`{ type, payload }\`).
|
|
434
|
+
|
|
435
|
+
**model** (`MastraLanguageModel`): The model that produced (or would have produced) the response.
|
|
436
|
+
|
|
437
|
+
**stepNumber** (`number`): Current step number (0-indexed).
|
|
438
|
+
|
|
439
|
+
**steps** (`StepResult[]`): All completed steps so far, including this step.
|
|
440
|
+
|
|
441
|
+
**state** (`Record<string, unknown>`): Per-processor state shared with \`processLLMRequest\` for the same step. Use this to pass data between the two hooks (e.g. a cache key).
|
|
442
|
+
|
|
443
|
+
**fromCache** (`boolean`): When \`true\`, the response was replayed from a cache via \`processLLMRequest\` returning \`{ response }\`. Processors that write to a cache should skip writes when this is \`true\`.
|
|
444
|
+
|
|
445
|
+
**warnings** (`LanguageModelV2CallWarning[]`): Warnings reported by the language model call (e.g. unsupported settings).
|
|
446
|
+
|
|
447
|
+
**request** (`unknown`): Provider request body, when available. Useful for tracing.
|
|
448
|
+
|
|
449
|
+
**rawResponse** (`unknown`): Raw provider response, when available. Useful for tracing.
|
|
450
|
+
|
|
451
|
+
**abort** (`(reason?: string, options?: { retry?: boolean; metadata?: unknown }) => never`): Function to abort processing. Throws a TripWire error that stops execution.
|
|
452
|
+
|
|
453
|
+
**retryCount** (`number`): Current retry attempt count. Starts at \`0\`; use to cap processor-triggered retries.
|
|
454
|
+
|
|
455
|
+
**requestContext** (`RequestContext`): Request-scoped context with execution metadata.
|
|
456
|
+
|
|
457
|
+
**tracingContext** (`TracingContext`): Tracing context for observability.
|
|
458
|
+
|
|
459
|
+
**writer** (`ProcessorStreamWriter`): Stream writer for emitting custom data chunks.
|
|
460
|
+
|
|
461
|
+
**abortSignal** (`AbortSignal`): Signal for cancelling the operation.
|
|
462
|
+
|
|
463
|
+
#### Return value
|
|
464
|
+
|
|
465
|
+
`processLLMResponse` returns `ProcessLLMResponseResult`, which is `undefined | void`. The return value is reserved for future extensibility.
|
|
466
|
+
|
|
467
|
+
#### Use cases
|
|
468
|
+
|
|
469
|
+
- Writing LLM responses to a cache after a live call (paired with cache-key derivation in `processLLMRequest`)
|
|
470
|
+
- Logging or recording the full response for analytics
|
|
471
|
+
- Triggering side effects based on the completed response
|
|
472
|
+
|
|
473
|
+
***
|
|
474
|
+
|
|
404
475
|
### `processAPIError`
|
|
405
476
|
|
|
406
477
|
Handles LLM API rejection errors before they surface as final errors. This runs when the API call fails with a non-retryable error (such as a 400 or 422 status code). Unlike `processOutputStep` which runs after successful responses, this runs when the API rejects the request.
|
|
@@ -628,8 +699,14 @@ export class QualityGuardrail implements Processor {
|
|
|
628
699
|
Mastra provides type aliases to ensure processors implement the required methods:
|
|
629
700
|
|
|
630
701
|
```typescript
|
|
631
|
-
// Must implement processInput
|
|
632
|
-
type InputProcessor = Processor &
|
|
702
|
+
// Must implement processInput, processInputStep, processLLMRequest, or processLLMResponse (or any combination)
|
|
703
|
+
type InputProcessor = Processor &
|
|
704
|
+
(
|
|
705
|
+
| { processInput: required }
|
|
706
|
+
| { processInputStep: required }
|
|
707
|
+
| { processLLMRequest: required }
|
|
708
|
+
| { processLLMResponse: required }
|
|
709
|
+
)
|
|
633
710
|
|
|
634
711
|
// Must implement processOutputStream, processOutputStep, OR processOutputResult (or any combination)
|
|
635
712
|
type OutputProcessor = Processor &
|
|
@@ -813,11 +890,11 @@ export class WordCounter implements Processor {
|
|
|
813
890
|
|
|
814
891
|
## State lifecycle
|
|
815
892
|
|
|
816
|
-
Every processor receives a `state` object in `processOutputStream`, `processOutputStep`, `processOutputResult`, and `processAPIError`. State has three important properties:
|
|
893
|
+
Every processor receives a `state` object in `processLLMRequest`, `processLLMResponse`, `processOutputStream`, `processOutputStep`, `processOutputResult`, and `processAPIError`. State has three important properties:
|
|
817
894
|
|
|
818
895
|
- **Per-processor**: Each processor gets its own `state` object, keyed by the processor's `id`. Two processors with different ids cannot read or overwrite each other's state.
|
|
819
896
|
- **Per-request**: A fresh state object is created at the start of every `agent.generate()` or `agent.stream()` call. State does not leak between requests or between users.
|
|
820
|
-
- **Shared across methods**: Within one request, the same `state` object is passed to `processOutputStream` (for every chunk), `processOutputStep` (after every LLM step), `processOutputResult` (once at the end), and `processAPIError` (when an LLM call fails).
|
|
897
|
+
- **Shared across methods**: Within one request, the same `state` object is passed to `processLLMRequest` (before the provider call), `processLLMResponse` (after the step completes), `processOutputStream` (for every chunk), `processOutputStep` (after every LLM step), `processOutputResult` (once at the end), and `processAPIError` (when an LLM call fails). For example, `processLLMRequest` can stash a cache key and `processLLMResponse` can read it back to write the response.
|
|
821
898
|
|
|
822
899
|
Initialize fields defensively on first access, because `state` starts as an empty object:
|
|
823
900
|
|