Skip to content

Commit 232f729

Browse files
committed
refactor: use path mappings for internal libs
1 parent 577bee3 commit 232f729

8 files changed

Lines changed: 87 additions & 76 deletions

File tree

ai-server/src/mastra/agents/ticketing-agent.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Agent } from '@mastra/core/agent';
2-
import { Memory } from '@mastra/memory';
3-
41
import {
52
addCustomCatalogInstructions,
63
renderA2uiTool,
74
} from '@internal/ag-ui-server';
5+
import { Agent } from '@mastra/core/agent';
6+
import { Memory } from '@mastra/memory';
7+
88
import { model } from '../config.js';
99
import { bookFlightTool } from '../tools/book-flight.js';
1010
import { cancelFlightTool } from '../tools/cancel-flight.js';

ai-server/src/mastra/dashboard-dsl/compile-dashboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import {
77
getBookedFlights,
88
} from '../data/booked-flights-store.js';
99
import { buildAndCacheChartUrl } from '../tools/render-chart.js';
10+
import { fetchFlights, type FlightRecord } from '../tools/search-flights.js';
1011
import { searchHotels } from '../tools/search-hotels.js';
1112
import { searchRentalCars } from '../tools/search-rental-cars.js';
12-
import { fetchFlights, type FlightRecord } from '../tools/search-flights.js';
1313
import { weatherForecast, weatherIconFor } from '../tools/weather-forecast.js';
1414
import type { DashboardSpec, DashboardTile } from './dashboard-spec.js';
1515

ai-server/src/mastra/routes/ag-ui-route.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import { getExtendedLocalAgent } from '@internal/ag-ui-server';
12
import type { ContextWithMastra } from '@mastra/core/server';
23
import { streamSSE } from 'hono/streaming';
34

4-
import { getExtendedLocalAgent } from '@internal/ag-ui-server';
5-
65
import { parseRunAgentInput, streamAgentEvents } from './ag-ui-stream.js';
76

87
export async function agUiRouteHandler(
@@ -24,7 +23,12 @@ export async function agUiRouteHandler(
2423
requestContext,
2524
});
2625

27-
return streamSSE(c, async (sse) => {
28-
await streamAgentEvents(sse, agent, parsed.input);
29-
});
26+
// `c` is typed against @mastra/core's bundled hono, which is structurally
27+
// incompatible with the project's hono `Context` that `streamSSE` expects.
28+
return streamSSE(
29+
c as unknown as Parameters<typeof streamSSE>[0],
30+
async (sse) => {
31+
await streamAgentEvents(sse, agent, parsed.input);
32+
},
33+
);
3034
}

ai-server/src/mastra/routes/dashboard-ag-ui-route.ts

Lines changed: 67 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import {
44
randomUUID,
55
type RunAgentInput,
66
} from '@ag-ui/client';
7+
import { getExtendedLocalAgent } from '@internal/ag-ui-server';
78
import type { ContextWithMastra } from '@mastra/core/server';
89
import { streamSSE } from 'hono/streaming';
910

10-
import { getExtendedLocalAgent } from '../../../../libs/ag-ui-server/index.js';
1111
import {
1212
computeDashboardRequestHash,
1313
type DashboardCacheEntry,
@@ -35,10 +35,10 @@ const DASHBOARD_AGENT_ID = 'dashboardAgent';
3535
const CACHED_FRAME_DELAY_MS = resolveCachedFrameDelayMs();
3636

3737
function resolveCachedFrameDelayMs(): number | null {
38-
if (process.env.NODE_ENV === 'production') {
38+
if (process.env['NODE_ENV'] === 'production') {
3939
return null;
4040
}
41-
const raw = process.env.AG_UI_STREAM_FRAME_DELAY_MS;
41+
const raw = process.env['AG_UI_STREAM_FRAME_DELAY_MS'];
4242
if (raw === undefined) {
4343
return 0;
4444
}
@@ -78,62 +78,67 @@ export async function dashboardAgUiRouteHandler(
7878
requestContext,
7979
});
8080

81-
return streamSSE(c, async (sse) => {
82-
let renderToolCallId: string | undefined;
83-
let argsBuffer = '';
84-
let capturedSpec: DashboardSpec | undefined;
85-
86-
await streamAgentEvents(sse, agent, input, {
87-
onEvent: async (event): Promise<readonly BaseEvent[] | void> => {
88-
const e = event as BaseEvent & {
89-
toolCallId?: string;
90-
toolCallName?: string;
91-
delta?: string;
92-
};
93-
94-
if (
95-
e.type === EventType.TOOL_CALL_START &&
96-
e.toolCallName === RENDER_DASHBOARD_TOOL_NAME &&
97-
typeof e.toolCallId === 'string'
98-
) {
99-
renderToolCallId = e.toolCallId;
100-
argsBuffer = '';
101-
return;
102-
}
81+
// `c` is typed against @mastra/core's bundled hono, which is structurally
82+
// incompatible with the project's hono `Context` that `streamSSE` expects.
83+
return streamSSE(
84+
c as unknown as Parameters<typeof streamSSE>[0],
85+
async (sse) => {
86+
let renderToolCallId: string | undefined;
87+
let argsBuffer = '';
88+
let capturedSpec: DashboardSpec | undefined;
89+
90+
await streamAgentEvents(sse, agent, input, {
91+
onEvent: async (event): Promise<readonly BaseEvent[] | void> => {
92+
const e = event as BaseEvent & {
93+
toolCallId?: string;
94+
toolCallName?: string;
95+
delta?: string;
96+
};
97+
98+
if (
99+
e.type === EventType.TOOL_CALL_START &&
100+
e.toolCallName === RENDER_DASHBOARD_TOOL_NAME &&
101+
typeof e.toolCallId === 'string'
102+
) {
103+
renderToolCallId = e.toolCallId;
104+
argsBuffer = '';
105+
return;
106+
}
103107

104-
if (
105-
e.type === EventType.TOOL_CALL_ARGS &&
106-
e.toolCallId === renderToolCallId &&
107-
typeof e.delta === 'string'
108-
) {
109-
argsBuffer += e.delta;
110-
return;
111-
}
108+
if (
109+
e.type === EventType.TOOL_CALL_ARGS &&
110+
e.toolCallId === renderToolCallId &&
111+
typeof e.delta === 'string'
112+
) {
113+
argsBuffer += e.delta;
114+
return;
115+
}
112116

113-
if (
114-
e.type === EventType.TOOL_CALL_END &&
115-
e.toolCallId === renderToolCallId
116-
) {
117-
const { events, spec } = await handleRenderToolCallEnd(argsBuffer);
118-
if (spec) {
119-
capturedSpec = spec;
117+
if (
118+
e.type === EventType.TOOL_CALL_END &&
119+
e.toolCallId === renderToolCallId
120+
) {
121+
const { events, spec } = await handleRenderToolCallEnd(argsBuffer);
122+
if (spec) {
123+
capturedSpec = spec;
124+
}
125+
return events;
120126
}
121-
return events;
127+
},
128+
});
129+
130+
if (capturedSpec && !preventCaching) {
131+
try {
132+
await writeDashboardCache(cacheKey, capturedSpec);
133+
} catch (err) {
134+
console.error(
135+
`Failed to write dashboard cache (hash=${cacheKey}):`,
136+
err,
137+
);
122138
}
123-
},
124-
});
125-
126-
if (capturedSpec && !preventCaching) {
127-
try {
128-
await writeDashboardCache(cacheKey, capturedSpec);
129-
} catch (err) {
130-
console.error(
131-
`Failed to write dashboard cache (hash=${cacheKey}):`,
132-
err,
133-
);
134139
}
135-
}
136-
});
140+
},
141+
);
137142
}
138143

139144
async function streamCachedDashboard(
@@ -324,9 +329,14 @@ async function tryServeFromCache(
324329
if (!entry) {
325330
return null;
326331
}
327-
return streamSSE(c, async (sse) => {
328-
await streamCachedDashboard(sse, input, entry.spec);
329-
});
332+
// `c` is typed against @mastra/core's bundled hono, which is structurally
333+
// incompatible with the project's hono `Context` that `streamSSE` expects.
334+
return streamSSE(
335+
c as unknown as Parameters<typeof streamSSE>[0],
336+
async (sse) => {
337+
await streamCachedDashboard(sse, input, entry.spec);
338+
},
339+
);
330340
}
331341

332342
interface RenderToolCallEndResult {

ai-server/src/mastra/widgets/flight-widget.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { randomUUID } from 'node:crypto';
22

3-
import { z } from 'zod';
4-
53
import {
64
type BuiltComponent,
75
defineServerWidget,
8-
} from '../../../../libs/ag-ui-server/index.js';
6+
} from '@internal/ag-ui-server';
7+
import { z } from 'zod';
98

109
const flightSchema = z.object({
1110
id: z.number().describe('Unique id of the flight'),

ai-server/src/mastra/widgets/message-widget.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { randomUUID } from 'node:crypto';
22

3-
import { z } from 'zod';
4-
53
import {
64
type BuiltComponent,
75
defineServerWidget,
8-
} from '../../../../libs/ag-ui-server/index.js';
6+
} from '@internal/ag-ui-server';
7+
import { z } from 'zod';
98

109
export const messageWidget = defineServerWidget({
1110
name: 'messageWidget',

ai-server/src/mastra/widgets/question-widget.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { randomUUID } from 'node:crypto';
22

3-
import { z } from 'zod';
4-
53
import {
64
type BuiltComponent,
75
defineServerWidget,
8-
} from '../../../../libs/ag-ui-server/index.js';
6+
} from '@internal/ag-ui-server';
7+
import { z } from 'zod';
98

109
const questionSchema = z.object({
1110
id: z

ai-server/src/mastra/workflows/package-tour-workflow.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readBridge } from '@internal/ag-ui-server';
1+
import { readBridge } from '@internal/ag-ui-server/step-bridge.js';
22
import { createStep, createWorkflow } from '@mastra/core/workflows';
33
import { z } from 'zod';
44

0 commit comments

Comments
 (0)