@dbx-tools/appkit-mastra 0.6.223 → 0.6.224

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.
@@ -1,100 +0,0 @@
1
- /**
2
- * Shared agent binding and request-context resolution for Mastra custom routes.
3
- *
4
- * @module
5
- */
6
-
7
- import { ValidationError } from "@databricks/appkit";
8
- import type { Agent } from "@mastra/core/agent";
9
- import {
10
- MASTRA_RESOURCE_ID_KEY,
11
- MASTRA_THREAD_ID_KEY,
12
- type RequestContext,
13
- } from "@mastra/core/request-context";
14
- import type { ContextWithMastra } from "@mastra/core/server";
15
-
16
- /** Fixed-agent or dynamic `:agentId` binding accepted by custom agent routes. */
17
- export type AgentRouteOptions =
18
- { path: `${string}:agentId${string}`; agent?: never } | { path: string; agent: string };
19
-
20
- /** Validated route path and optional fixed agent binding. */
21
- export interface ResolvedAgentRouteOptions {
22
- path: string;
23
- fixedAgent: string | undefined;
24
- }
25
-
26
- /** Resolve and validate a fixed or dynamic agent route declaration. */
27
- export function resolveAgentRouteOptions(
28
- options: AgentRouteOptions,
29
- field: string,
30
- ): ResolvedAgentRouteOptions {
31
- const fixedAgent = "agent" in options ? options.agent : undefined;
32
- if (!fixedAgent && !options.path.includes(":agentId")) {
33
- throw ValidationError.invalidValue(
34
- field,
35
- options.path,
36
- "a path containing `:agentId`, or an explicit `agent`",
37
- );
38
- }
39
- return { path: options.path, fixedAgent };
40
- }
41
-
42
- interface ResolvedAgentRequestContext {
43
- agentId: string;
44
- agent: Agent;
45
- requestContext: RequestContext;
46
- resourceId: string;
47
- }
48
-
49
- interface AgentRequestContextError {
50
- error: Response;
51
- }
52
-
53
- interface ResolveAgentRequestContextOptions {
54
- fixedAgent?: string;
55
- threadId?: "optional" | "required";
56
- }
57
-
58
- /** Resolve an agent plus the caller-scoped Mastra request context. */
59
- export function resolveAgentRequestContext(
60
- context: ContextWithMastra,
61
- options: ResolveAgentRequestContextOptions & { threadId: "required" },
62
- ): (ResolvedAgentRequestContext & { threadId: string }) | AgentRequestContextError;
63
- export function resolveAgentRequestContext(
64
- context: ContextWithMastra,
65
- options?: ResolveAgentRequestContextOptions,
66
- ): (ResolvedAgentRequestContext & { threadId?: string }) | AgentRequestContextError;
67
- export function resolveAgentRequestContext(
68
- context: ContextWithMastra,
69
- options: ResolveAgentRequestContextOptions = {},
70
- ): (ResolvedAgentRequestContext & { threadId?: string }) | AgentRequestContextError {
71
- const mastra = context.get("mastra");
72
- const requestContext = context.get("requestContext");
73
- const agentId = options.fixedAgent ?? context.req.param("agentId");
74
- if (!agentId) {
75
- return { error: context.json({ error: "agentId is required" }, 400) };
76
- }
77
- const agent = mastra.getAgentById(agentId);
78
- if (!agent) {
79
- return { error: context.json({ error: `Unknown agent "${agentId}"` }, 404) };
80
- }
81
- const resourceId = requestContext.get(MASTRA_RESOURCE_ID_KEY) as string | undefined;
82
- if (!resourceId) {
83
- return {
84
- error: context.json({ error: "resource id missing from request context" }, 400),
85
- };
86
- }
87
- const threadId = requestContext.get(MASTRA_THREAD_ID_KEY) as string | undefined;
88
- if (options.threadId === "required" && !threadId) {
89
- return {
90
- error: context.json({ error: "thread id missing from request context" }, 400),
91
- };
92
- }
93
- return {
94
- agentId,
95
- agent,
96
- requestContext,
97
- resourceId,
98
- ...(threadId ? { threadId } : {}),
99
- };
100
- }