2026-09-23 19:29:57 +08:00
|
|
|
|
/**
|
2026-09-24 15:09:37 +08:00
|
|
|
|
* 平台服务入口(架构设计第 2 节)。
|
2026-09-23 19:29:57 +08:00
|
|
|
|
*
|
2026-09-24 15:09:37 +08:00
|
|
|
|
* 组装:应用服务 + HTTP / SSE 入口。控制面(core)与引擎适配(agent)在后续阶段接入。
|
2026-09-23 19:29:57 +08:00
|
|
|
|
*/
|
2026-09-24 15:50:07 +08:00
|
|
|
|
import { randomUUID } from 'node:crypto';
|
2026-09-24 15:09:37 +08:00
|
|
|
|
import type { Server } from 'node:http';
|
|
|
|
|
|
import type { AddressInfo } from 'node:net';
|
|
|
|
|
|
|
2026-09-28 15:38:42 +08:00
|
|
|
|
import type { AgentRuntime } from './agent/runtime.js';
|
2026-09-28 16:01:04 +08:00
|
|
|
|
import { loadConfig } from './config.js';
|
2026-09-24 15:09:37 +08:00
|
|
|
|
import { ApplicationService } from './app/service.js';
|
|
|
|
|
|
import { createHttpServer } from './entry/http.js';
|
|
|
|
|
|
|
|
|
|
|
|
export { ApplicationService } from './app/service.js';
|
|
|
|
|
|
export type { Subject } from './app/subject.js';
|
|
|
|
|
|
export { createHttpServer } from './entry/http.js';
|
|
|
|
|
|
|
|
|
|
|
|
export interface StartOptions {
|
|
|
|
|
|
host?: string;
|
|
|
|
|
|
port?: number;
|
|
|
|
|
|
version?: string;
|
2026-09-28 15:38:42 +08:00
|
|
|
|
/** 引擎实现:默认假引擎;A3.3 起可换成 Pi 子进程,测试也可注入脚本化假引擎 */
|
|
|
|
|
|
runtime?: AgentRuntime;
|
2026-09-24 15:09:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface RunningServer {
|
|
|
|
|
|
url: string;
|
|
|
|
|
|
port: number;
|
|
|
|
|
|
service: ApplicationService;
|
2026-09-24 15:50:07 +08:00
|
|
|
|
/** 引擎工具回调的内部令牌:由平台生成,启动引擎子进程时注入(A3.11) */
|
|
|
|
|
|
internalToken: string;
|
2026-09-24 15:09:37 +08:00
|
|
|
|
close(): Promise<void>;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function startServer(options: StartOptions = {}): Promise<RunningServer> {
|
2026-09-28 16:01:04 +08:00
|
|
|
|
const config = loadConfig();
|
|
|
|
|
|
const host = options.host ?? config.server.host;
|
|
|
|
|
|
const port = options.port ?? config.server.port;
|
2026-09-28 15:38:42 +08:00
|
|
|
|
const service = new ApplicationService({
|
|
|
|
|
|
...(options.version === undefined ? {} : { version: options.version }),
|
|
|
|
|
|
...(options.runtime === undefined ? {} : { runtime: options.runtime }),
|
|
|
|
|
|
});
|
2026-09-24 15:48:45 +08:00
|
|
|
|
// 开发期可选:APS_DEV_PROVIDER=1 时注册假 Provider,用于本地联调与演示(默认不注册,能力清单为空)
|
|
|
|
|
|
if (process.env.APS_DEV_PROVIDER === '1') {
|
|
|
|
|
|
const { createFakeProvider } = await import('@aps/provider-sdk/testing');
|
|
|
|
|
|
await service.registerProvider(createFakeProvider());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-24 15:50:07 +08:00
|
|
|
|
const internalToken = randomUUID();
|
|
|
|
|
|
const server: Server = createHttpServer(service, internalToken);
|
2026-09-24 15:09:37 +08:00
|
|
|
|
|
|
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
|
|
|
|
server.once('error', reject);
|
|
|
|
|
|
server.listen(port, host, () => resolve());
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const address = server.address() as AddressInfo | null;
|
|
|
|
|
|
const actualPort = address ? address.port : port;
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
url: `http://${host}:${actualPort}`,
|
|
|
|
|
|
port: actualPort,
|
|
|
|
|
|
service,
|
2026-09-24 15:50:07 +08:00
|
|
|
|
internalToken,
|
2026-09-24 15:09:37 +08:00
|
|
|
|
close: () =>
|
|
|
|
|
|
new Promise<void>((resolve, reject) => {
|
|
|
|
|
|
server.close((error) => (error ? reject(error) : resolve()));
|
2026-09-28 15:38:42 +08:00
|
|
|
|
// 事件流是长连接,关停时必须主动断开,否则 close 会一直等它们
|
|
|
|
|
|
// 客户端断线后按 Last-Event-ID 重连,不会丢事件(协议文档第 5 节)
|
|
|
|
|
|
server.closeAllConnections();
|
2026-09-24 15:09:37 +08:00
|
|
|
|
}),
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|