(options?: HttpServerOptions)
| 90 | * 启动独立 HTTP API 服务 |
| 91 | */ |
| 92 | export async function startHttpServer(options?: HttpServerOptions): Promise<{ |
| 93 | port: number |
| 94 | host: string |
| 95 | token: string |
| 96 | }> { |
| 97 | if (server) { |
| 98 | throw new Error('HTTP server is already running') |
| 99 | } |
| 100 | |
| 101 | let config = loadConfig() |
| 102 | const port = options?.port ?? config.api.port |
| 103 | const host = options?.host ?? config.api.host |
| 104 | const token = options?.token ?? ensureToken(config) |
| 105 | |
| 106 | const pendingMigration = applyPendingNodeDataDirMigrationIfNeeded() |
| 107 | if (!pendingMigration.skipped) { |
| 108 | if (pendingMigration.success) { |
| 109 | console.log('[Migration] Pending data directory migration completed') |
| 110 | config = loadConfig() |
| 111 | } else { |
| 112 | console.error('[Migration] Pending data directory migration failed:', pendingMigration.error) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | const userDataDir = config.data.user_data_dir || undefined |
| 117 | const pathProvider = new NodePathProvider(userDataDir) |
| 118 | pathProvider.ensureAllDirs() |
| 119 | const runtime = assertCliDataDirCompatible(pathProvider, 'cli') |
| 120 | |
| 121 | if (hasPendingElectronDataWarning() || !verifyCliDataPath(pathProvider.getDatabaseDir())) { |
| 122 | console.error( |
| 123 | '\n' + |
| 124 | '='.repeat(68) + |
| 125 | '\n' + |
| 126 | ' ChatLab: Electron desktop data not found\n' + |
| 127 | '='.repeat(68) + |
| 128 | '\n\n' + |
| 129 | ' Detected that ChatLab desktop app was installed on this machine,\n' + |
| 130 | ' but could not locate your chat databases.\n\n' + |
| 131 | ' This usually means you changed the data directory in desktop settings.\n\n' + |
| 132 | ' To fix this, choose one of:\n\n' + |
| 133 | ' 1. Open ChatLab desktop app — it will auto-migrate your data\n' + |
| 134 | ' 2. Set the data directory manually:\n' + |
| 135 | ' export CHATLAB_DATA_DIR="/path/to/your/data"\n' + |
| 136 | ' 3. Edit ~/.chatlab/config.toml:\n' + |
| 137 | ' [data]\n' + |
| 138 | ' user_data_dir = "/path/to/your/data"\n\n' + |
| 139 | '='.repeat(68) + |
| 140 | '\n' |
| 141 | ) |
| 142 | } |
| 143 | |
| 144 | const migrationRunner = new MigrationRunner(ALL_MIGRATIONS, { |
| 145 | dataDir: pathProvider.getSystemDir(), |
| 146 | aiDataDir: pathProvider.getAiDataDir(), |
| 147 | logger: { |
| 148 | info: (_cat: string, msg: string) => console.log(`[Migration] ${msg}`), |
| 149 | warn: (_cat: string, msg: string) => console.warn(`[Migration] ${msg}`), |
no test coverage detected