@polymorphism-tech/morph-spec 7.7.3 → 7.8.1
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/README.md +2 -2
- package/bin/validate.js +379 -379
- package/claude-plugin.json +1 -1
- package/docs/CHEATSHEET.md +1 -1
- package/docs/QUICKSTART.md +1 -1
- package/framework/CLAUDE.md +1 -1
- package/framework/MORPH.md +3 -2
- package/framework/agents.json +1 -1
- package/framework/commands/commit.md +166 -166
- package/framework/commands/morph-infra.md +215 -215
- package/framework/commands/morph-preflight.md +205 -205
- package/framework/commands/morph-troubleshoot.md +124 -124
- package/framework/evals/scenarios/dotnet-crud.yaml +1 -1
- package/framework/hooks/claude-code/statusline.py +679 -679
- package/framework/hooks/claude-code/statusline.sh +11 -11
- package/framework/hooks/git/commit-msg/conventional-commits.sh +33 -33
- package/framework/hooks/git/pre-commit/agents.sh +25 -25
- package/framework/hooks/git/pre-commit/orchestrator.sh +64 -64
- package/framework/hooks/git/pre-commit/specs.sh +50 -50
- package/framework/hooks/git/pre-push/run-tests.sh +44 -44
- package/framework/hooks/shared/activity-logger.js +160 -160
- package/framework/hooks/shared/hook-response.js +45 -45
- package/framework/hooks/shared/payload-utils.js +78 -78
- package/framework/hooks/shared/stdin-reader.js +26 -26
- package/framework/rubrics/backend.json +41 -41
- package/framework/rules/ai-agents-standards.md +45 -45
- package/framework/rules/dotnet-templates.md +86 -87
- package/framework/rules/frontend-standards.md +13 -13
- package/framework/rules/infrastructure-standards.md +27 -27
- package/framework/rules/neon-standards.md +25 -25
- package/framework/rules/nextjs-standards.md +20 -20
- package/framework/rules/nextjs-templates.md +40 -40
- package/framework/skills/core/create-umbraco-lp/SKILL.md +1 -1
- package/framework/skills/core/migrate-lp-to-umbraco/SKILL.md +1 -1
- package/framework/skills/core/morph-apply/SKILL.md +1 -1
- package/framework/skills/core/morph-brainstorming/SKILL.md +9 -5
- package/framework/skills/core/morph-eval/SKILL.md +1 -1
- package/framework/skills/core/morph-implement/SKILL.md +1 -1
- package/framework/skills/core/morph-plan/SKILL.md +11 -6
- package/framework/skills/core/morph-proposal/SKILL.md +1 -1
- package/framework/skills/core/morph-review/SKILL.md +13 -7
- package/framework/skills/core/morph-stack-scan/SKILL.md +5 -5
- package/framework/skills/core/morph-standards/SKILL.md +1 -1
- package/framework/standards/STANDARDS.json +1 -1
- package/framework/standards/ai-agents/agent-session.md +160 -160
- package/framework/standards/ai-agents/batching.md +193 -193
- package/framework/standards/ai-agents/context-providers.md +188 -188
- package/framework/standards/ai-agents/mcp-server.md +250 -250
- package/framework/standards/ai-agents/mcp-tools.md +197 -197
- package/framework/standards/ai-agents/middleware-patterns.md +207 -207
- package/framework/standards/ai-agents/modalities-image-gen.md +173 -173
- package/framework/standards/ai-agents/modalities-speech-to-text.md +171 -171
- package/framework/standards/ai-agents/modalities-text-to-speech.md +181 -181
- package/framework/standards/ai-agents/modalities-vision.md +201 -201
- package/framework/standards/ai-agents/multi-agent-patterns.md +155 -155
- package/framework/standards/ai-agents/production.md +275 -275
- package/framework/standards/ai-agents/prompts-in-db.md +214 -214
- package/framework/standards/ai-agents/providers/model-registry.md +158 -158
- package/framework/standards/ai-agents/service-tier-flex.md +135 -135
- package/framework/standards/ai-agents/structured-output.md +249 -249
- package/framework/standards/ai-agents/sweet-spot.md +109 -109
- package/framework/standards/architecture/vertical-slice/vertical-slice.md +34 -35
- package/framework/standards/backend/api/validation.md +2 -2
- package/framework/standards/backend/dotnet/scrutor.md +1 -3
- package/framework/standards/backend/dotnet/vsa-handler-patterns.md +42 -37
- package/framework/standards/data/nosql/cache/redis.md +2 -2
- package/framework/templates/CLAUDE.md.core.template +1 -1
- package/framework/templates/IDE-SNIPPETS.md +262 -262
- package/framework/templates/README.md +0 -1
- package/framework/templates/REGISTRY.json +0 -21
- package/framework/templates/code/dotnet/abstractions/api-endpoint.cs +7 -7
- package/framework/templates/code/dotnet/abstractions/handler.cs +0 -3
- package/framework/templates/code/dotnet/ai-agents/AgentBootstrap.cs.template +39 -39
- package/framework/templates/code/dotnet/ai-agents/EmbeddingService.cs.template +68 -68
- package/framework/templates/code/dotnet/ai-agents/ModelRegistry.cs.template +117 -117
- package/framework/templates/code/dotnet/ai-agents/PromptRepository.cs.template +95 -95
- package/framework/templates/code/dotnet/ai-agents/ResilientToolMiddleware.cs.template +61 -61
- package/framework/templates/code/dotnet/ai-agents/VectorSearchTool.cs.template +107 -107
- package/framework/templates/code/dotnet/ai-agents/model-registry.json.template +58 -58
- package/framework/templates/code/dotnet/constants/api-tags.cs +1 -1
- package/framework/templates/code/dotnet/contracts/contracts-vsa.cs +281 -282
- package/framework/templates/code/dotnet/database/migration.cs +83 -83
- package/framework/templates/code/dotnet/feature/endpoint.cs.hbs +21 -21
- package/framework/templates/code/dotnet/feature/handler.cs.hbs +23 -24
- package/framework/templates/code/dotnet/feature/request.cs.hbs +4 -4
- package/framework/templates/code/dotnet/feature/response.cs.hbs +4 -4
- package/framework/templates/code/dotnet/feature/validator.cs.hbs +15 -15
- package/framework/templates/code/dotnet/jobs/job.cs +15 -15
- package/framework/templates/code/dotnet/test.cs +72 -101
- package/framework/templates/code/sql/ai-agents/agent-prompts-migration.sql.template +58 -58
- package/framework/templates/code/sql/ai-agents/pgvector-embeddings-migration.sql.template +91 -91
- package/framework/templates/code/typescript/contracts.ts +167 -167
- package/framework/templates/docs/clarifications.md +253 -253
- package/framework/templates/docs/schema-analysis.md +119 -119
- package/framework/templates/docs/ui-components.md +52 -52
- package/framework/templates/docs/ui-design-system.md +76 -76
- package/framework/templates/docs/ui-flows.md +167 -167
- package/framework/templates/docs/ui-mockups.md +98 -98
- package/framework/templates/feature/decisions.md +122 -122
- package/framework/templates/frontend/nextjs/Dockerfile.nextjs.hbs +43 -43
- package/framework/templates/frontend/nextjs/client-component.tsx.hbs +26 -26
- package/framework/templates/frontend/nextjs/env.mjs.hbs +32 -32
- package/framework/templates/frontend/nextjs/feature-form.tsx.hbs +56 -56
- package/framework/templates/frontend/nextjs/page.tsx.hbs +22 -22
- package/framework/templates/frontend/nextjs/tsconfig.json.hbs +26 -26
- package/framework/templates/frontend/nextjs/use-feature.ts.hbs +54 -54
- package/framework/templates/infrastructure/docker/Dockerfile.template +58 -58
- package/framework/templates/infrastructure/docker/docker-compose.template.yml +50 -50
- package/framework/templates/infrastructure/docker/dockerfile-api.dockerfile +38 -38
- package/framework/templates/infrastructure/docker/dockerfile-web.dockerfile +48 -48
- package/framework/templates/infrastructure/github/README.md +56 -56
- package/framework/templates/infrastructure/github/actions/docker-build-push/action.yml.hbs +45 -45
- package/framework/templates/infrastructure/github/actions/health-check/action.yml.hbs +27 -27
- package/framework/templates/infrastructure/github/workflows/docker-build-push.yml.hbs +59 -59
- package/framework/templates/infrastructure/github/workflows/dotnet-build.yml.hbs +39 -39
- package/framework/templates/integrations/asaas-client.cs +387 -387
- package/framework/templates/integrations/asaas-webhook.cs +351 -351
- package/framework/templates/meta-prompts/fusion/fusion-agent.md +76 -76
- package/framework/templates/meta-prompts/fusion/fusion-aggregator.md +100 -100
- package/framework/templates/meta-prompts/parallel-workers/parallel-coordinator.md +113 -113
- package/framework/templates/meta-prompts/validators/pre-commit-validator.md +95 -95
- package/framework/templates/project/validate.js +120 -120
- package/framework/templates/state.template.json +17 -17
- package/framework/templates/ui/design-system.css +226 -226
- package/package.json +1 -1
- package/src/commands/dashboard/dashboard.js +53 -53
- package/src/commands/index.js +13 -13
- package/src/commands/mcp/mcp-setup.js +519 -519
- package/src/commands/project/checkpoint.js +209 -209
- package/src/commands/project/update.js +7 -4
- package/src/core/state/phase-state-machine.js +197 -197
- package/src/core/templates/template-renderer.js +477 -477
- package/src/dashboard/app/components/DetailPanel.jsx +11 -11
- package/src/dashboard/app/components/FlowGraph.jsx +17 -17
- package/src/dashboard/app/components/SearchFilter.jsx +22 -22
- package/src/dashboard/app/hooks/useDetailPanel.js +15 -15
- package/src/dashboard/app/main.jsx +13 -13
- package/src/dashboard/app/pages/AgentMap.jsx +370 -370
- package/src/dashboard/app/pages/HookInspector.jsx +418 -418
- package/src/dashboard/app/pages/LiveState.jsx +239 -239
- package/src/dashboard/app/pages/PhasePipeline.jsx +408 -408
- package/src/dashboard/app/pages/RulesMap.jsx +273 -273
- package/src/dashboard/app/pages/SkillsLibrary.jsx +366 -366
- package/src/dashboard/app/pages/Standards.jsx +332 -332
- package/src/dashboard/app/styles/index.css +47 -47
- package/src/dashboard/index.html +12 -12
- package/src/dashboard/package.json +24 -24
- package/src/dashboard/scanner/hooks-scanner.js +41 -41
- package/src/dashboard/scanner/phases-scanner.js +15 -15
- package/src/dashboard/scanner/rubrics-scanner.js +12 -12
- package/src/dashboard/scanner/rules-scanner.js +37 -37
- package/src/dashboard/scanner/skills-scanner.js +43 -43
- package/src/dashboard/scanner/squads-scanner.js +13 -13
- package/src/dashboard/scanner/standards-scanner.js +26 -26
- package/src/dashboard/vite-plugin-morph-data.js +17 -17
- package/src/dashboard/vite.config.js +24 -24
- package/src/lib/agents/metrics-aggregator.js +56 -56
- package/src/lib/agents/metrics-calculator.js +125 -125
- package/src/lib/agents/output-collector.js +73 -73
- package/src/lib/detectors/claude-config-detector.js +152 -152
- package/src/lib/detectors/design-system-detector.js +231 -231
- package/src/lib/detectors/index.js +217 -217
- package/src/lib/generators/architecture-scanner.js +639 -639
- package/src/lib/generators/design-system-generator.js +508 -508
- package/src/lib/generators/index.js +13 -13
- package/src/lib/generators/settings-generator.js +209 -209
- package/src/lib/hooks/hook-executor.js +167 -167
- package/src/lib/installers/mcp-installer.js +719 -719
- package/src/lib/tasks/task-classifier.js +30 -30
- package/src/lib/tasks/task-parser.js +209 -209
- package/src/lib/tasks/test-runner.js +81 -81
- package/src/lib/validators/architecture/architecture-validator.js +259 -259
- package/src/lib/validators/checkpoint-persistence.js +39 -39
- package/src/lib/validators/contracts/contract-compliance-validator.js +274 -274
- package/src/lib/validators/css/css-validator.js +340 -340
- package/src/lib/validators/packages/package-validator.js +347 -347
- package/src/lib/validators/shared/emit-validator-dispatch.js +14 -14
- package/src/lib/validators/shared/issue-counter.js +14 -14
- package/src/lib/validators/validation-runner.js +349 -349
- package/src/scripts/setup-infra.js +383 -383
- package/src/utils/agent-id-mapper.js +11 -11
- package/src/utils/banner.js +52 -43
- package/src/utils/claude-settings-manager.js +300 -300
- package/src/utils/skills-installer.js +172 -172
- package/framework/templates/.idea/morph-templates.xml +0 -92
- package/framework/templates/.vscode/morph-templates.code-snippets +0 -186
- package/framework/templates/code/dotnet/backend/repository.cs +0 -141
|
@@ -1,207 +1,207 @@
|
|
|
1
|
-
# Middleware Patterns — Intercepting agent behavior
|
|
2
|
-
|
|
3
|
-
> **Scope:** stacks=["dotnet"]
|
|
4
|
-
> **Layer:** 1 (on-keyword)
|
|
5
|
-
> **Keywords:** middleware, agent middleware, function calling middleware, retry, anti-hallucination, resilience, telemetry middleware, agent run middleware
|
|
6
|
-
> **Read by Claude in:** implement (when wiring cross-cutting behavior into an agent)
|
|
7
|
-
|
|
8
|
-
**Verified against:** MAF 1.0 GA. Last-verified: 2026-05-19.
|
|
9
|
-
|
|
10
|
-
---
|
|
11
|
-
|
|
12
|
-
## Os 3 níveis de middleware
|
|
13
|
-
|
|
14
|
-
| Tipo | Intercepts | Use Case |
|
|
15
|
-
|------|-----------|----------|
|
|
16
|
-
| **Agent Run** | Invocação completa do agent | Logging, auth, rate limiting |
|
|
17
|
-
| **Function Calling** | Chamadas individuais de tools | Error handling, validação, auditoria |
|
|
18
|
-
| **IChatClient** | Chamadas brutas ao LLM | Telemetry, caching, content filtering |
|
|
19
|
-
|
|
20
|
-
Cada nível tem uma assinatura de delegate distinta. Os três podem coexistir no mesmo agent — cada um atua na sua camada sem interferir nos outros.
|
|
21
|
-
|
|
22
|
-
---
|
|
23
|
-
|
|
24
|
-
## Registrando middleware
|
|
25
|
-
|
|
26
|
-
O registration point é sempre `.AsBuilder().Use(...).Build()`. Encadeie múltiplos middlewares antes de chamar `Build()`.
|
|
27
|
-
|
|
28
|
-
```csharp
|
|
29
|
-
using Microsoft.Agents.AI;
|
|
30
|
-
using Microsoft.Extensions.AI;
|
|
31
|
-
|
|
32
|
-
// Obter o IChatClient via Model Registry — provider-agnostic
|
|
33
|
-
IChatClient chatClient = _modelRegistry.GetChatClient("text-default");
|
|
34
|
-
|
|
35
|
-
// IChatClient-level middleware (antes de criar o agent)
|
|
36
|
-
IChatClient instrumentedClient = chatClient
|
|
37
|
-
.AsBuilder()
|
|
38
|
-
.Use(getResponseFunc: TelemetryMiddleware, getStreamingResponseFunc: null)
|
|
39
|
-
.Build();
|
|
40
|
-
|
|
41
|
-
// Agent Run + Function Calling middleware
|
|
42
|
-
AIAgent agent = instrumentedClient
|
|
43
|
-
.AsAIAgent(instructions: "You are a helpful assistant.")
|
|
44
|
-
.AsBuilder()
|
|
45
|
-
.Use(runFunc: LoggingMiddleware, runStreamingFunc: null)
|
|
46
|
-
.Use(ResilientToolMiddleware) // function calling middleware
|
|
47
|
-
.Build();
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
> Se apenas middleware não-streaming for fornecido, chamadas streaming rodam em modo não-streaming.
|
|
51
|
-
|
|
52
|
-
---
|
|
53
|
-
|
|
54
|
-
## Pattern: telemetria
|
|
55
|
-
|
|
56
|
-
IChatClient-level middleware que mede latência e token usage de cada chamada bruta ao LLM.
|
|
57
|
-
|
|
58
|
-
```csharp
|
|
59
|
-
using System.Diagnostics;
|
|
60
|
-
using Microsoft.Agents.AI;
|
|
61
|
-
using Microsoft.Extensions.AI;
|
|
62
|
-
|
|
63
|
-
async Task<ChatResponse> TelemetryMiddleware(
|
|
64
|
-
IEnumerable<ChatMessage> messages,
|
|
65
|
-
ChatOptions? options,
|
|
66
|
-
IChatClient innerChatClient,
|
|
67
|
-
CancellationToken cancellationToken)
|
|
68
|
-
{
|
|
69
|
-
var sw = Stopwatch.StartNew();
|
|
70
|
-
var response = await innerChatClient.GetResponseAsync(messages, options, cancellationToken);
|
|
71
|
-
sw.Stop();
|
|
72
|
-
Console.WriteLine($"LLM call: {sw.ElapsedMilliseconds}ms, Tokens: {response.Usage?.TotalTokenCount}");
|
|
73
|
-
return response;
|
|
74
|
-
}
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
Para o setup completo de OpenTelemetry (traces, métricas, Aspire Dashboard, `AgentOpenTelemetryConsts.DefaultSourceName`), consulte `ai-agents-production` §Observability. Não duplique aqui — este middleware é um ponto de entrada, não a solução completa.
|
|
78
|
-
|
|
79
|
-
---
|
|
80
|
-
|
|
81
|
-
## Pattern: resilência (retry + rate-limit)
|
|
82
|
-
|
|
83
|
-
Function Calling middleware que intercepta erros de tools antes que o agent receba exceções brutas. Devolver uma string descritiva ao agent permite que ele se autocorrija ou informe o usuário adequadamente.
|
|
84
|
-
|
|
85
|
-
```csharp
|
|
86
|
-
using Microsoft.Agents.AI;
|
|
87
|
-
using Microsoft.Extensions.AI;
|
|
88
|
-
|
|
89
|
-
async ValueTask<object?> ResilientToolMiddleware(
|
|
90
|
-
AIAgent agent,
|
|
91
|
-
FunctionInvocationContext context,
|
|
92
|
-
Func<FunctionInvocationContext, CancellationToken, ValueTask<object?>> next,
|
|
93
|
-
CancellationToken cancellationToken)
|
|
94
|
-
{
|
|
95
|
-
try
|
|
96
|
-
{
|
|
97
|
-
return await next(context, cancellationToken);
|
|
98
|
-
}
|
|
99
|
-
catch (HttpRequestException ex) when (ex.StatusCode == System.Net.HttpStatusCode.TooManyRequests)
|
|
100
|
-
{
|
|
101
|
-
return "Service is temporarily busy. Please try again in a moment.";
|
|
102
|
-
}
|
|
103
|
-
catch (TimeoutException)
|
|
104
|
-
{
|
|
105
|
-
return $"Tool '{context.Function.Name}' timed out. Try a simpler query.";
|
|
106
|
-
}
|
|
107
|
-
catch (Exception ex)
|
|
108
|
-
{
|
|
109
|
-
return $"Error in {context.Function.Name}: {ex.Message}";
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
Template completo com registro e stub de anti-hallucination: `templates/code/dotnet/ai-agents/ResilientToolMiddleware.cs.template`.
|
|
115
|
-
|
|
116
|
-
---
|
|
117
|
-
|
|
118
|
-
## Pattern: anti-hallucination
|
|
119
|
-
|
|
120
|
-
O LLM gera os argumentos de tool call baseado em linguagem natural — pode gerar formatos inválidos (datas malformadas, IDs fora de range, strings vazias). Validar **antes** de chamar `next()` permite que o agent se autocorrija na próxima iteração, sem precisar de retry externo.
|
|
121
|
-
|
|
122
|
-
```csharp
|
|
123
|
-
using Microsoft.Agents.AI;
|
|
124
|
-
using Microsoft.Extensions.AI;
|
|
125
|
-
|
|
126
|
-
async ValueTask<object?> AntiHallucinationMiddleware(
|
|
127
|
-
AIAgent agent,
|
|
128
|
-
FunctionInvocationContext context,
|
|
129
|
-
Func<FunctionInvocationContext, CancellationToken, ValueTask<object?>> next,
|
|
130
|
-
CancellationToken cancellationToken)
|
|
131
|
-
{
|
|
132
|
-
// Validate 'date' argument before calling the tool
|
|
133
|
-
if (context.Arguments.TryGetValue("date", out var dateArg)
|
|
134
|
-
&& !DateTime.TryParse(dateArg?.ToString(), out _))
|
|
135
|
-
{
|
|
136
|
-
return $"Invalid date format for '{context.Function.Name}'. Expected ISO 8601 (e.g. 2026-01-31).";
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// Validate 'orderId' is a positive integer
|
|
140
|
-
if (context.Arguments.TryGetValue("orderId", out var idArg)
|
|
141
|
-
&& (!int.TryParse(idArg?.ToString(), out var id) || id <= 0))
|
|
142
|
-
{
|
|
143
|
-
return $"Invalid orderId '{idArg}' for '{context.Function.Name}'. Must be a positive integer.";
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return await next(context, cancellationToken);
|
|
147
|
-
}
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
**Princípio:** devolva uma mensagem de erro descritiva — o LLM vai corrigir o argumento na próxima iteração. Não lance exceção; não chame `next()` com argumento inválido.
|
|
151
|
-
|
|
152
|
-
---
|
|
153
|
-
|
|
154
|
-
## Decision tree
|
|
155
|
-
|
|
156
|
-
```
|
|
157
|
-
Precisa de middleware no agent?
|
|
158
|
-
│
|
|
159
|
-
├─ Logar / autorizar a invocação completa do agent?
|
|
160
|
-
│ └─ Agent Run middleware
|
|
161
|
-
│ .Use(runFunc: MyMiddleware, runStreamingFunc: null)
|
|
162
|
-
│
|
|
163
|
-
├─ Tratar falhas de tool / validar argumentos do LLM?
|
|
164
|
-
│ └─ Function Calling middleware
|
|
165
|
-
│ .Use(ResilientToolMiddleware)
|
|
166
|
-
│
|
|
167
|
-
└─ Medir / cachear / filtrar chamadas brutas ao LLM?
|
|
168
|
-
└─ IChatClient middleware
|
|
169
|
-
.Use(getResponseFunc: TelemetryMiddleware, getStreamingResponseFunc: null)
|
|
170
|
-
— aplicado no IChatClient.AsBuilder() ANTES de .AsAIAgent()
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
---
|
|
174
|
-
|
|
175
|
-
## Anti-patterns
|
|
176
|
-
|
|
177
|
-
| Anti-pattern | Problema |
|
|
178
|
-
|--------------|---------|
|
|
179
|
-
| Swallow exception sem retornar contexto ao agent | O agent não consegue se autocorrigir — para silenciosamente |
|
|
180
|
-
| Middleware que muta estado global | Viola isolation; causa race conditions em requests concorrentes |
|
|
181
|
-
| Fornecer apenas middleware não-streaming | Chamadas streaming rodam em modo não-streaming silenciosamente — downgrade imperceptível |
|
|
182
|
-
| Lógica de negócio em middleware | Middleware é cross-cutting only; lógica de domínio pertence ao tool ou ao handler |
|
|
183
|
-
| Registrar middleware depois de `Build()` | `Build()` finaliza o pipeline — chamadas após não têm efeito |
|
|
184
|
-
|
|
185
|
-
---
|
|
186
|
-
|
|
187
|
-
## Checklist (verifiable by morph-eval)
|
|
188
|
-
|
|
189
|
-
- [ ] Middleware registrado via `.AsBuilder().Use(...).Build()` — nunca depois de `Build()`
|
|
190
|
-
- [ ] IChatClient middleware aplicado no `chatClient.AsBuilder()` antes de `.AsAIAgent()`
|
|
191
|
-
- [ ] Function Calling middleware retorna string descritiva em vez de lançar exceção
|
|
192
|
-
- [ ] Anti-hallucination: argumentos validados antes de chamar `next()`
|
|
193
|
-
- [ ] Nenhum middleware com `using Microsoft.SemanticKernel` — MAF 1.0 GA não usa SK
|
|
194
|
-
- [ ] Streaming: se nenhum middleware streaming for fornecido, downgrade documentado como decisão consciente
|
|
195
|
-
- [ ] Middleware que chama serviços externos tem timeout configurado
|
|
196
|
-
|
|
197
|
-
---
|
|
198
|
-
|
|
199
|
-
## References
|
|
200
|
-
|
|
201
|
-
- `ai-agents-setup` — pacotes, DI, quick start
|
|
202
|
-
- `ai-agents-production` — panorama completo: observability, A2A, MCP, caching, middleware-as-one-of-many-concerns
|
|
203
|
-
- `ai-agents-providers-model-registry` — `_modelRegistry.GetChatClient(alias)`, provider-agnostic pattern
|
|
204
|
-
|
|
205
|
-
---
|
|
206
|
-
|
|
207
|
-
*MORPH-SPEC by Polymorphism Tech — ai-agents/middleware-patterns.md v1.0 (2026-05-19)*
|
|
1
|
+
# Middleware Patterns — Intercepting agent behavior
|
|
2
|
+
|
|
3
|
+
> **Scope:** stacks=["dotnet"]
|
|
4
|
+
> **Layer:** 1 (on-keyword)
|
|
5
|
+
> **Keywords:** middleware, agent middleware, function calling middleware, retry, anti-hallucination, resilience, telemetry middleware, agent run middleware
|
|
6
|
+
> **Read by Claude in:** implement (when wiring cross-cutting behavior into an agent)
|
|
7
|
+
|
|
8
|
+
**Verified against:** MAF 1.0 GA. Last-verified: 2026-05-19.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Os 3 níveis de middleware
|
|
13
|
+
|
|
14
|
+
| Tipo | Intercepts | Use Case |
|
|
15
|
+
|------|-----------|----------|
|
|
16
|
+
| **Agent Run** | Invocação completa do agent | Logging, auth, rate limiting |
|
|
17
|
+
| **Function Calling** | Chamadas individuais de tools | Error handling, validação, auditoria |
|
|
18
|
+
| **IChatClient** | Chamadas brutas ao LLM | Telemetry, caching, content filtering |
|
|
19
|
+
|
|
20
|
+
Cada nível tem uma assinatura de delegate distinta. Os três podem coexistir no mesmo agent — cada um atua na sua camada sem interferir nos outros.
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Registrando middleware
|
|
25
|
+
|
|
26
|
+
O registration point é sempre `.AsBuilder().Use(...).Build()`. Encadeie múltiplos middlewares antes de chamar `Build()`.
|
|
27
|
+
|
|
28
|
+
```csharp
|
|
29
|
+
using Microsoft.Agents.AI;
|
|
30
|
+
using Microsoft.Extensions.AI;
|
|
31
|
+
|
|
32
|
+
// Obter o IChatClient via Model Registry — provider-agnostic
|
|
33
|
+
IChatClient chatClient = _modelRegistry.GetChatClient("text-default");
|
|
34
|
+
|
|
35
|
+
// IChatClient-level middleware (antes de criar o agent)
|
|
36
|
+
IChatClient instrumentedClient = chatClient
|
|
37
|
+
.AsBuilder()
|
|
38
|
+
.Use(getResponseFunc: TelemetryMiddleware, getStreamingResponseFunc: null)
|
|
39
|
+
.Build();
|
|
40
|
+
|
|
41
|
+
// Agent Run + Function Calling middleware
|
|
42
|
+
AIAgent agent = instrumentedClient
|
|
43
|
+
.AsAIAgent(instructions: "You are a helpful assistant.")
|
|
44
|
+
.AsBuilder()
|
|
45
|
+
.Use(runFunc: LoggingMiddleware, runStreamingFunc: null)
|
|
46
|
+
.Use(ResilientToolMiddleware) // function calling middleware
|
|
47
|
+
.Build();
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
> Se apenas middleware não-streaming for fornecido, chamadas streaming rodam em modo não-streaming.
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## Pattern: telemetria
|
|
55
|
+
|
|
56
|
+
IChatClient-level middleware que mede latência e token usage de cada chamada bruta ao LLM.
|
|
57
|
+
|
|
58
|
+
```csharp
|
|
59
|
+
using System.Diagnostics;
|
|
60
|
+
using Microsoft.Agents.AI;
|
|
61
|
+
using Microsoft.Extensions.AI;
|
|
62
|
+
|
|
63
|
+
async Task<ChatResponse> TelemetryMiddleware(
|
|
64
|
+
IEnumerable<ChatMessage> messages,
|
|
65
|
+
ChatOptions? options,
|
|
66
|
+
IChatClient innerChatClient,
|
|
67
|
+
CancellationToken cancellationToken)
|
|
68
|
+
{
|
|
69
|
+
var sw = Stopwatch.StartNew();
|
|
70
|
+
var response = await innerChatClient.GetResponseAsync(messages, options, cancellationToken);
|
|
71
|
+
sw.Stop();
|
|
72
|
+
Console.WriteLine($"LLM call: {sw.ElapsedMilliseconds}ms, Tokens: {response.Usage?.TotalTokenCount}");
|
|
73
|
+
return response;
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Para o setup completo de OpenTelemetry (traces, métricas, Aspire Dashboard, `AgentOpenTelemetryConsts.DefaultSourceName`), consulte `ai-agents-production` §Observability. Não duplique aqui — este middleware é um ponto de entrada, não a solução completa.
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## Pattern: resilência (retry + rate-limit)
|
|
82
|
+
|
|
83
|
+
Function Calling middleware que intercepta erros de tools antes que o agent receba exceções brutas. Devolver uma string descritiva ao agent permite que ele se autocorrija ou informe o usuário adequadamente.
|
|
84
|
+
|
|
85
|
+
```csharp
|
|
86
|
+
using Microsoft.Agents.AI;
|
|
87
|
+
using Microsoft.Extensions.AI;
|
|
88
|
+
|
|
89
|
+
async ValueTask<object?> ResilientToolMiddleware(
|
|
90
|
+
AIAgent agent,
|
|
91
|
+
FunctionInvocationContext context,
|
|
92
|
+
Func<FunctionInvocationContext, CancellationToken, ValueTask<object?>> next,
|
|
93
|
+
CancellationToken cancellationToken)
|
|
94
|
+
{
|
|
95
|
+
try
|
|
96
|
+
{
|
|
97
|
+
return await next(context, cancellationToken);
|
|
98
|
+
}
|
|
99
|
+
catch (HttpRequestException ex) when (ex.StatusCode == System.Net.HttpStatusCode.TooManyRequests)
|
|
100
|
+
{
|
|
101
|
+
return "Service is temporarily busy. Please try again in a moment.";
|
|
102
|
+
}
|
|
103
|
+
catch (TimeoutException)
|
|
104
|
+
{
|
|
105
|
+
return $"Tool '{context.Function.Name}' timed out. Try a simpler query.";
|
|
106
|
+
}
|
|
107
|
+
catch (Exception ex)
|
|
108
|
+
{
|
|
109
|
+
return $"Error in {context.Function.Name}: {ex.Message}";
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Template completo com registro e stub de anti-hallucination: `templates/code/dotnet/ai-agents/ResilientToolMiddleware.cs.template`.
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
## Pattern: anti-hallucination
|
|
119
|
+
|
|
120
|
+
O LLM gera os argumentos de tool call baseado em linguagem natural — pode gerar formatos inválidos (datas malformadas, IDs fora de range, strings vazias). Validar **antes** de chamar `next()` permite que o agent se autocorrija na próxima iteração, sem precisar de retry externo.
|
|
121
|
+
|
|
122
|
+
```csharp
|
|
123
|
+
using Microsoft.Agents.AI;
|
|
124
|
+
using Microsoft.Extensions.AI;
|
|
125
|
+
|
|
126
|
+
async ValueTask<object?> AntiHallucinationMiddleware(
|
|
127
|
+
AIAgent agent,
|
|
128
|
+
FunctionInvocationContext context,
|
|
129
|
+
Func<FunctionInvocationContext, CancellationToken, ValueTask<object?>> next,
|
|
130
|
+
CancellationToken cancellationToken)
|
|
131
|
+
{
|
|
132
|
+
// Validate 'date' argument before calling the tool
|
|
133
|
+
if (context.Arguments.TryGetValue("date", out var dateArg)
|
|
134
|
+
&& !DateTime.TryParse(dateArg?.ToString(), out _))
|
|
135
|
+
{
|
|
136
|
+
return $"Invalid date format for '{context.Function.Name}'. Expected ISO 8601 (e.g. 2026-01-31).";
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Validate 'orderId' is a positive integer
|
|
140
|
+
if (context.Arguments.TryGetValue("orderId", out var idArg)
|
|
141
|
+
&& (!int.TryParse(idArg?.ToString(), out var id) || id <= 0))
|
|
142
|
+
{
|
|
143
|
+
return $"Invalid orderId '{idArg}' for '{context.Function.Name}'. Must be a positive integer.";
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return await next(context, cancellationToken);
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
**Princípio:** devolva uma mensagem de erro descritiva — o LLM vai corrigir o argumento na próxima iteração. Não lance exceção; não chame `next()` com argumento inválido.
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
## Decision tree
|
|
155
|
+
|
|
156
|
+
```
|
|
157
|
+
Precisa de middleware no agent?
|
|
158
|
+
│
|
|
159
|
+
├─ Logar / autorizar a invocação completa do agent?
|
|
160
|
+
│ └─ Agent Run middleware
|
|
161
|
+
│ .Use(runFunc: MyMiddleware, runStreamingFunc: null)
|
|
162
|
+
│
|
|
163
|
+
├─ Tratar falhas de tool / validar argumentos do LLM?
|
|
164
|
+
│ └─ Function Calling middleware
|
|
165
|
+
│ .Use(ResilientToolMiddleware)
|
|
166
|
+
│
|
|
167
|
+
└─ Medir / cachear / filtrar chamadas brutas ao LLM?
|
|
168
|
+
└─ IChatClient middleware
|
|
169
|
+
.Use(getResponseFunc: TelemetryMiddleware, getStreamingResponseFunc: null)
|
|
170
|
+
— aplicado no IChatClient.AsBuilder() ANTES de .AsAIAgent()
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
## Anti-patterns
|
|
176
|
+
|
|
177
|
+
| Anti-pattern | Problema |
|
|
178
|
+
|--------------|---------|
|
|
179
|
+
| Swallow exception sem retornar contexto ao agent | O agent não consegue se autocorrigir — para silenciosamente |
|
|
180
|
+
| Middleware que muta estado global | Viola isolation; causa race conditions em requests concorrentes |
|
|
181
|
+
| Fornecer apenas middleware não-streaming | Chamadas streaming rodam em modo não-streaming silenciosamente — downgrade imperceptível |
|
|
182
|
+
| Lógica de negócio em middleware | Middleware é cross-cutting only; lógica de domínio pertence ao tool ou ao handler |
|
|
183
|
+
| Registrar middleware depois de `Build()` | `Build()` finaliza o pipeline — chamadas após não têm efeito |
|
|
184
|
+
|
|
185
|
+
---
|
|
186
|
+
|
|
187
|
+
## Checklist (verifiable by morph-eval)
|
|
188
|
+
|
|
189
|
+
- [ ] Middleware registrado via `.AsBuilder().Use(...).Build()` — nunca depois de `Build()`
|
|
190
|
+
- [ ] IChatClient middleware aplicado no `chatClient.AsBuilder()` antes de `.AsAIAgent()`
|
|
191
|
+
- [ ] Function Calling middleware retorna string descritiva em vez de lançar exceção
|
|
192
|
+
- [ ] Anti-hallucination: argumentos validados antes de chamar `next()`
|
|
193
|
+
- [ ] Nenhum middleware com `using Microsoft.SemanticKernel` — MAF 1.0 GA não usa SK
|
|
194
|
+
- [ ] Streaming: se nenhum middleware streaming for fornecido, downgrade documentado como decisão consciente
|
|
195
|
+
- [ ] Middleware que chama serviços externos tem timeout configurado
|
|
196
|
+
|
|
197
|
+
---
|
|
198
|
+
|
|
199
|
+
## References
|
|
200
|
+
|
|
201
|
+
- `ai-agents-setup` — pacotes, DI, quick start
|
|
202
|
+
- `ai-agents-production` — panorama completo: observability, A2A, MCP, caching, middleware-as-one-of-many-concerns
|
|
203
|
+
- `ai-agents-providers-model-registry` — `_modelRegistry.GetChatClient(alias)`, provider-agnostic pattern
|
|
204
|
+
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
*MORPH-SPEC by Polymorphism Tech — ai-agents/middleware-patterns.md v1.0 (2026-05-19)*
|