MCPcopy Index your code
hub / github.com/ChatLab/ChatLab / startInternalServer

Function startInternalServer

apps/desktop/main/internal-api.ts:84–291  ·  view source on GitHub ↗
(pathProvider: PathProvider)

Source from the content-addressed store, hash-verified

82 * Must be called before createWindow() so the Renderer can retrieve the endpoint.
83 */
84export async function startInternalServer(pathProvider: PathProvider): Promise<InternalEndpoint> {
85 if (server) return endpoint!
86
87 let newServer: FastifyInstance | null = null
88 let newDbManager: DatabaseManager | null = null
89 let newSemanticIndexService: SemanticIndexRuntime | null = null
90
91 try {
92 const token = `int_${randomBytes(32).toString('hex')}`
93 const { app } = await import('electron')
94 const runtime = assertDesktopDataDirCompatible(pathProvider, getDesktopAppVersion(app.getVersion()))
95
96 newDbManager = new DatabaseManager(pathProvider, { runtime })
97 const sessionAdapter = createDatabaseManagerAdapter(newDbManager)
98
99 const aiDataDir = pathProvider.getAiDataDir()
100 const llmConfigStore = new LLMConfigStore(createFileConfigStorage(aiDataDir), {
101 resolveApiKey: (provider, authProfile) => resolveApiKey(provider, authProfile) || undefined,
102 onApiKeyCreated: (config, apiKey) => {
103 const profileName = config.name?.toLowerCase().replace(/\s+/g, '-') || config.provider
104 writeAuthProfile(profileName, { type: 'api_key', provider: config.provider, key: apiKey })
105 return profileName
106 },
107 onApiKeyDeleted: (config) => {
108 const profileName = (config as unknown as Record<string, unknown>).authProfile as string | undefined
109 if (profileName) deleteAuthProfile(profileName)
110 },
111 })
112
113 const configStorage = createFileConfigStorage(aiDataDir)
114
115 const newMergeCache = new MergeSessionCache(pathProvider)
116 newMergeCache.cleanupOrphans()
117
118 // 语义索引 worker client:启动 internal server 时不拉起 worker;状态/构建/检索按需 lazy start。
119 try {
120 newSemanticIndexService = createSemanticIndexWorkerRuntimeClient({
121 pathProvider,
122 runtime,
123 sqliteVecLoadablePath: getSqliteVecLoadablePath().replace('app.asar', 'app.asar.unpacked'),
124 getModelDownloadProxyUrl: resolveModelDownloadProxyUrl,
125 workerEntryUrl: import.meta.url.endsWith('.ts')
126 ? undefined
127 : new URL('./semantic-index-worker.js', import.meta.url),
128 })
129 } catch (err) {
130 console.warn('[semantic-index] worker client unavailable:', err instanceof Error ? err.message : String(err))
131 newSemanticIndexService = null
132 }
133
134 const electronStreamImport = async (dm: DatabaseManager, filePath: string) => {
135 const deps: StreamImportDeps = {
136 openDatabase(sessionId: string) {
137 return dm.openRawSessionDatabase(sessionId, { create: true, initializeChatTables: true })
138 },
139 deleteDatabase(sessionId: string) {
140 dm.deleteSessionDatabaseFiles(sessionId)
141 },

Callers 1

mainAppEventsMethod · 0.90

Calls 15

cleanupOrphansMethod · 0.95
getDesktopAppVersionFunction · 0.90
resolveApiKeyFunction · 0.90
writeAuthProfileFunction · 0.90
deleteAuthProfileFunction · 0.90
apiErrorFromUnknownFunction · 0.90
errorResponseFunction · 0.90

Tested by

no test coverage detected