feat(ai): implement opt-in OpenAI-compatible AiService, secret redactor, surface guides, and WS streaming

This commit is contained in:
tyuXX 2026-11-27 14:25:17 +00:00
parent fbc1eb94e0
commit 076df61f4d
6 changed files with 1073 additions and 1 deletions

View file

@ -3,6 +3,7 @@ import http from 'node:http';
import path from 'node:path';
import type Database from 'better-sqlite3';
import express, { type Express } from 'express';
import { AiService } from './ai/ai-service.js';
import { AlertEngine } from './alerts/alert-engine.js';
import { BroadcastService } from './automation/broadcast-service.js';
import { TransferEngine } from './automation/transfer-engine.js';
@ -28,6 +29,7 @@ import { SftpService } from './ssh/sftp-service.js';
import { SSHPool } from './ssh/ssh-pool.js';
import { TunnelManager } from './ssh/tunnel-manager.js';
import { CustomSystemsEngine } from './systems/systems-engine.js';
import { createAiRouter } from './api/routes/ai.routes.js';
import { createAlertsRouter } from './api/routes/alerts.routes.js';
import { createAuditRouter } from './api/routes/audit.routes.js';
import { createAuthRouter } from './api/routes/auth.routes.js';
@ -76,6 +78,7 @@ export interface GCoordinatorInstance {
workflowEngine: WorkflowEngine;
broadcastService: BroadcastService;
alertEngine: AlertEngine;
aiService: AiService;
wsHub: WsHub;
port: number;
close(): Promise<void>;
@ -186,6 +189,13 @@ export async function createGCoordinatorServer(
wsHub,
auditService,
);
const aiService = new AiService(
configManager,
vaultService,
sshPool,
wsHub,
auditService,
);
wsHub.registerUpgradeHandler('/proxy/', (req, socket, head) => {
tunnelManager.handleWsProxyUpgrade(req, socket, head);
@ -198,6 +208,11 @@ export async function createGCoordinatorServer(
ptyManager.resize(sessionId, cols, rows);
},
});
wsHub.attachAiHandlers({
onCancel: (chatId) => {
aiService.cancelChat(chatId);
},
});
wsHub.onChannelSubscribe((ctx, channel) => {
if (channel.startsWith('pty:')) {
const sessionId = channel.slice('pty:'.length);
@ -228,6 +243,11 @@ export async function createGCoordinatorServer(
for (const frame of replay) {
ctx.send(frame);
}
} else if (channel.startsWith('ai:')) {
const chatId = channel.slice('ai:'.length);
for (const frame of aiService.getReplayFrames(chatId)) {
ctx.send(frame);
}
} else if (channel === 'metrics') {
ctx.send({ type: 'servers:state', states: sshPool.getStates() });
}
@ -290,6 +310,7 @@ export async function createGCoordinatorServer(
alertEngine.start();
vaultService.onPanicLock(() => {
aiService.cancelAll();
alertEngine.handlePanicLock();
workflowEngine.handlePanicLock();
broadcastService.cancelAll();
@ -445,6 +466,7 @@ export async function createGCoordinatorServer(
'/api/alerts',
createAlertsRouter({ authService, configManager, auditService, alertEngine }),
);
app.use('/api/ai', createAiRouter({ authService, aiService }));
app.use(
'/api/servers/:id/sftp',
createSftpRouter({ authService, configManager, auditService, sftpService }),
@ -520,9 +542,11 @@ export async function createGCoordinatorServer(
workflowEngine,
broadcastService,
alertEngine,
aiService,
wsHub,
port: actualPort,
async close() {
aiService.close();
alertEngine.close();
workflowEngine.close();
broadcastService.cancelAll();