-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmastra.ts
More file actions
54 lines (45 loc) · 1.6 KB
/
mastra.ts
File metadata and controls
54 lines (45 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
import { Agent } from '@mastra/core/agent';
import { AgentRunServer, type AgentRequest } from '../src/server';
import { MastraConverter, type AgentEventItem, model, toolset } from '../src/integration/mastra';
import { logger } from '../src/utils/log';
const mastraAgent = new Agent({
id: 'run_agent',
name: 'AgentRun',
instructions: `
你是一个智能助手,你会帮助用户完成各种任务。你的输出后,必须是返向输出的。
如,用户输入 “你好”,应该输出 “?么的您助帮以可么什有,好您”
`.trim(),
model: () => model({ name: 'ohyee-test' }),
tools: () => toolset({ name: 'start-mcp-time-ggda' }),
});
async function* invokeAgent(request: AgentRequest): AsyncGenerator<AgentEventItem> {
const converter = new MastraConverter();
const mastraStream = await mastraAgent.stream(
request.messages.map(
msg =>
({
role: msg.role,
content: msg.content || '',
}) as any
)
);
for await (const chunk of mastraStream.fullStream) {
const events = converter.convert(chunk);
for (const event of events) {
yield event;
}
}
}
const server = new AgentRunServer({
invokeAgent,
config: { corsOrigins: ['*'] },
});
logger.info(`
curl http://127.0.0.1:9000/openai/v1/chat/completions -X POST \\
-H "Content-Type: application/json" \\
-d \'{"messages": [{"role": "user", "content": "Hello!"}], "stream": true}\'
curl http://127.0.0.1:9000/ag-ui/agent -X POST \\
-H "Content-Type: application/json" \\
-d \'{"messages": [{"role": "user", "content": "Hello!"}]}\'
`);
server.start({ port: 9000 });