-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathapp.ts
More file actions
202 lines (181 loc) · 8.35 KB
/
Copy pathapp.ts
File metadata and controls
202 lines (181 loc) · 8.35 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import assert from 'node:assert';
import { ControllerMetaBuilderFactory, ControllerType } from '@eggjs/controller-decorator';
import { GlobalGraph, type LoadUnitLifecycleContext } from '@eggjs/metadata';
import { LoaderUtil } from '@eggjs/tegg-loader';
import { type LoadUnitInstanceLifecycleContext, ModuleLoadUnitInstance } from '@eggjs/tegg-runtime';
import { AGENT_CONTROLLER_PROTO_IMPL_TYPE } from '@eggjs/tegg-types';
import type { Application, ILifecycleBoot } from 'egg';
import { AgentControllerObject } from './lib/AgentControllerObject.ts';
import { AgentControllerProto } from './lib/AgentControllerProto.ts';
import { AppLoadUnitControllerHook } from './lib/AppLoadUnitControllerHook.ts';
import { CONTROLLER_LOAD_UNIT, ControllerLoadUnit } from './lib/ControllerLoadUnit.ts';
import { ControllerLoadUnitHandler } from './lib/ControllerLoadUnitHandler.ts';
import { ControllerMetadataManager } from './lib/ControllerMetadataManager.ts';
import { ControllerRegisterFactory } from './lib/ControllerRegisterFactory.ts';
import { EggControllerLoader } from './lib/EggControllerLoader.ts';
import { EggControllerPrototypeHook } from './lib/EggControllerPrototypeHook.ts';
import { HTTPControllerRegister } from './lib/impl/http/HTTPControllerRegister.ts';
import { MCPControllerRegister } from './lib/impl/mcp/MCPControllerRegister.ts';
import { middlewareGraphHook } from './lib/MiddlewareGraphHook.ts';
import { RootProtoManager } from './lib/RootProtoManager.ts';
function isMissingDirectoryError(error: unknown): boolean {
return (error as NodeJS.ErrnoException).code === 'ENOENT';
}
// Load Controller process
// 1. await add load unit is ready, controller may depend other load unit
// 2. load ${app_base_dir}app/controller file
// 3. ControllerRegister register controller implement
export default class ControllerAppBootHook implements ILifecycleBoot {
private readonly app: Application;
private readonly loadUnitHook: AppLoadUnitControllerHook;
private readonly controllerRegisterFactory: ControllerRegisterFactory;
private controllerLoadUnitHandler: ControllerLoadUnitHandler;
private readonly controllerPrototypeHook: EggControllerPrototypeHook;
constructor(app: Application) {
this.app = app;
this.controllerRegisterFactory = new ControllerRegisterFactory(this.app);
this.app.rootProtoManager = new RootProtoManager();
this.app.controllerRegisterFactory = this.controllerRegisterFactory;
this.app.controllerMetaBuilderFactory = ControllerMetaBuilderFactory;
this.loadUnitHook = new AppLoadUnitControllerHook(this.controllerRegisterFactory, this.app.rootProtoManager);
this.controllerPrototypeHook = new EggControllerPrototypeHook();
this.app.eggPrototypeCreatorFactory.registerPrototypeCreator(
AGENT_CONTROLLER_PROTO_IMPL_TYPE,
AgentControllerProto.createProto,
);
AgentControllerObject.setLogger(this.app.logger);
}
configWillLoad(): void {
this.app.loadUnitLifecycleUtil.registerLifecycle(this.loadUnitHook);
this.app.eggPrototypeLifecycleUtil.registerLifecycle(this.controllerPrototypeHook);
this.app.eggObjectFactory.registerEggObjectCreateMethod(AgentControllerProto, AgentControllerObject.createObject);
this.app.loaderFactory.registerLoader(CONTROLLER_LOAD_UNIT, (unitPath) => {
const filePattern = LoaderUtil.filePattern();
const discoverFiles = () => {
try {
return LoaderUtil.globFiles(filePattern, { cwd: unitPath });
} catch (error) {
if (isMissingDirectoryError(error)) {
return [];
}
throw error;
}
};
const files = this.app.loader.manifest?.globFiles(unitPath, discoverFiles) ?? discoverFiles();
return new EggControllerLoader(unitPath, files);
});
this.controllerRegisterFactory.registerControllerRegister(ControllerType.HTTP, HTTPControllerRegister.create);
this.app.loadUnitFactory.registerLoadUnitCreator(
CONTROLLER_LOAD_UNIT,
(ctx: LoadUnitLifecycleContext): ControllerLoadUnit => {
return new ControllerLoadUnit(
`tegg-app-controller:${ctx.unitPath}`,
ctx.unitPath,
ctx.loader,
this.app.eggPrototypeFactory,
this.app.eggPrototypeCreatorFactory,
);
},
);
this.app.loadUnitInstanceFactory.registerLoadUnitInstanceClass(
CONTROLLER_LOAD_UNIT,
(ctx: LoadUnitInstanceLifecycleContext): ModuleLoadUnitInstance => {
return new ModuleLoadUnitInstance(ctx.loadUnit);
},
);
if (this.app.config.security?.csrf !== void 0) {
assert(
typeof this.app.config.security.csrf === 'boolean' || typeof this.app.config.security.csrf === 'object',
'csrf must be boolean or object',
);
if (typeof this.app.config.security.csrf === 'boolean') {
(this.app.config.security as any).csrf = {
enable: this.app.config.security.csrf,
};
}
}
// init http root proto middleware
this.prepareMiddleware(this.app.config.coreMiddleware);
if (this.mcpEnable()) {
this.controllerRegisterFactory.registerControllerRegister(ControllerType.MCP, MCPControllerRegister.create);
// Don't let the mcp's body be consumed
this.app.config.coreMiddleware.unshift('mcpBodyMiddleware');
if (this.app.config.security.csrf.ignore) {
if (Array.isArray(this.app.config.security.csrf.ignore)) {
this.app.config.security.csrf.ignore = [
/^\/mcp\//,
this.app.config.mcp.sseInitPath,
this.app.config.mcp.sseMessagePath,
this.app.config.mcp.streamPath,
this.app.config.mcp.statelessStreamPath,
...(Array.isArray(this.app.config.security.csrf.ignore)
? this.app.config.security.csrf.ignore
: [this.app.config.security.csrf.ignore]),
];
}
} else {
this.app.config.security.csrf.ignore = [
/^\/mcp\//,
this.app.config.mcp.sseInitPath,
this.app.config.mcp.sseMessagePath,
this.app.config.mcp.streamPath,
this.app.config.mcp.statelessStreamPath,
];
}
if (this.app.config.mcp.multipleServer) {
for (const name of Object.keys(this.app.config.mcp.multipleServer)) {
['sseInitPath', 'sseMessagePath', 'streamPath', 'statelessStreamPath'].forEach((key) => {
if (this.app.config.mcp.multipleServer[name][key])
(this.app.config.security.csrf.ignore as any[]).push(this.app.config.mcp.multipleServer[name][key]);
});
}
}
}
}
prepareMiddleware(middlewareNames: string[]): string[] {
if (!middlewareNames.includes('teggCtxLifecycleMiddleware')) {
middlewareNames.unshift('teggCtxLifecycleMiddleware');
}
const index = middlewareNames.indexOf('teggCtxLifecycleMiddleware');
middlewareNames.splice(index, 0, 'teggRootProto');
return middlewareNames;
}
async didLoad(): Promise<void> {
await this.app.moduleHandler.ready();
this.controllerLoadUnitHandler = new ControllerLoadUnitHandler(this.app);
await this.controllerLoadUnitHandler.ready();
// The real register HTTP controller/method.
// HTTP method should sort by priority
// The HTTPControllerRegister will collect all the methods
// and register methods after collect is done.
HTTPControllerRegister.instance?.doRegister(this.app.rootProtoManager);
this.app.config.mcp.hooks = MCPControllerRegister.hooks;
}
configDidLoad(): void {
GlobalGraph.instance?.registerBuildHook(middlewareGraphHook);
}
async willReady(): Promise<void> {
if (this.mcpEnable()) {
await MCPControllerRegister.connectStatelessStreamTransport();
const names = MCPControllerRegister.instance?.mcpConfig.getMultipleServerNames();
if (names && names.length > 0) {
for (const name of names) {
await MCPControllerRegister.connectStatelessStreamTransport(name);
}
}
}
}
mcpEnable(): boolean {
return !!this.app.plugins.mcpProxy?.enable;
}
async beforeClose(): Promise<void> {
if (this.controllerLoadUnitHandler) {
await this.controllerLoadUnitHandler.destroy();
}
this.app.loadUnitLifecycleUtil.deleteLifecycle(this.loadUnitHook);
this.app.eggPrototypeLifecycleUtil.deleteLifecycle(this.controllerPrototypeHook);
ControllerMetadataManager.instance.clear();
HTTPControllerRegister.clean();
MCPControllerRegister.clean();
}
}