-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathcontainer.ts
More file actions
284 lines (277 loc) · 11.2 KB
/
container.ts
File metadata and controls
284 lines (277 loc) · 11.2 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import { logger } from "../lib/logger.js";
import { ControlPlaneHealthService } from "../runtime/control-plane-health.js";
import { CreditGuardStateWriter } from "../runtime/credit-guard-state-writer.js";
import { GatewayClient } from "../runtime/gateway-client.js";
import { startHealthLoop } from "../runtime/loops.js";
import { startAnalyticsLoop } from "../runtime/loops.js";
import { OpenClawAuthProfilesStore } from "../runtime/openclaw-auth-profiles-store.js";
import { OpenClawAuthProfilesWriter } from "../runtime/openclaw-auth-profiles-writer.js";
import { OpenClawConfigWriter } from "../runtime/openclaw-config-writer.js";
import { OpenClawProcessManager } from "../runtime/openclaw-process.js";
import { OpenClawWatchTrigger } from "../runtime/openclaw-watch-trigger.js";
import { OpenClawWsClient } from "../runtime/openclaw-ws-client.js";
import { SessionsRuntime } from "../runtime/sessions-runtime.js";
import { OpenClawRuntimeModelWriter } from "../runtime/slimclaw-runtime-model-writer.js";
import { OpenClawRuntimePluginWriter } from "../runtime/slimclaw-runtime-plugin-writer.js";
import {
type ControllerRuntimeState,
createRuntimeState,
} from "../runtime/state.js";
import { WorkspaceTemplateWriter } from "../runtime/workspace-template-writer.js";
import { AgentService } from "../services/agent-service.js";
import { AnalyticsService } from "../services/analytics-service.js";
import { ArtifactService } from "../services/artifact-service.js";
import { AttachmentStore } from "../services/attachment-store.js";
import { ChannelFallbackService } from "../services/channel-fallback-service.js";
import { ChannelService } from "../services/channel-service.js";
import { DesktopLocalService } from "../services/desktop-local-service.js";
import { GithubStarVerificationService } from "../services/github-star-verification-service.js";
import { IntegrationService } from "../services/integration-service.js";
import { LocalUserService } from "../services/local-user-service.js";
import { ModelProviderService } from "../services/model-provider-service.js";
import { OpenClawAuthService } from "../services/openclaw-auth-service.js";
import { OpenClawGatewayService } from "../services/openclaw-gateway-service.js";
import { OpenClawSyncService } from "../services/openclaw-sync-service.js";
import { QuotaFallbackService } from "../services/quota-fallback-service.js";
import { RuntimeConfigService } from "../services/runtime-config-service.js";
import { RuntimeModelStateService } from "../services/runtime-model-state-service.js";
import { SessionService } from "../services/session-service.js";
import { SkillhubService } from "../services/skillhub-service.js";
import { TemplateService } from "../services/template-service.js";
import { ArtifactsStore } from "../store/artifacts-store.js";
import { CompiledOpenClawStore } from "../store/compiled-openclaw-store.js";
import { NexuConfigStore } from "../store/nexu-config-store.js";
import { type ControllerEnv, env } from "./env.js";
export interface ControllerContainer {
env: ControllerEnv;
configStore: NexuConfigStore;
gatewayClient: GatewayClient;
controlPlaneHealth: ControlPlaneHealthService;
openclawProcess: OpenClawProcessManager;
agentService: AgentService;
channelService: ChannelService;
channelFallbackService: ChannelFallbackService;
sessionService: SessionService;
runtimeConfigService: RuntimeConfigService;
runtimeModelStateService: RuntimeModelStateService;
modelProviderService: ModelProviderService;
integrationService: IntegrationService;
localUserService: LocalUserService;
desktopLocalService: DesktopLocalService;
analyticsService: AnalyticsService;
artifactService: ArtifactService;
attachmentStore: AttachmentStore;
templateService: TemplateService;
skillhubService: SkillhubService;
openclawSyncService: OpenClawSyncService;
openclawAuthService: OpenClawAuthService;
quotaFallbackService: QuotaFallbackService;
githubStarVerificationService: GithubStarVerificationService;
wsClient: OpenClawWsClient;
gatewayService: OpenClawGatewayService;
runtimeState: ControllerRuntimeState;
startBackgroundLoops: () => () => void;
}
const NEXU_OFFICIAL_MODEL_REFRESH_INTERVAL_MS = 60 * 1000;
export async function createContainer(): Promise<ControllerContainer> {
const configStore = new NexuConfigStore(env);
await configStore.reconcileConfiguredDesktopCloudState();
await configStore.syncManagedRuntimeGateway({
port: env.openclawGatewayPort,
authMode: env.openclawGatewayToken ? "token" : "none",
});
const artifactsStore = new ArtifactsStore(env);
const compiledStore = new CompiledOpenClawStore(env);
const configWriter = new OpenClawConfigWriter(env);
const authProfilesStore = new OpenClawAuthProfilesStore(env);
const authProfilesWriter = new OpenClawAuthProfilesWriter(authProfilesStore);
const runtimePluginWriter = new OpenClawRuntimePluginWriter(env);
const runtimeModelWriter = new OpenClawRuntimeModelWriter(env);
const creditGuardStateWriter = new CreditGuardStateWriter(env);
const templateWriter = new WorkspaceTemplateWriter(env);
const gatewayClient = new GatewayClient(env);
const sessionsRuntime = new SessionsRuntime(env);
const runtimeState = createRuntimeState();
// Construct openclawProcess before watchTrigger so the watch trigger can
// delegate gateway restarts to OpenClawProcessManager.restart() instead of
// re-implementing the dev-vs-launchd branching inline.
const openclawProcess = new OpenClawProcessManager(env);
const watchTrigger = new OpenClawWatchTrigger(env, openclawProcess);
const wsClient = new OpenClawWsClient(env);
const gatewayService = new OpenClawGatewayService(wsClient);
const controlPlaneHealth = new ControlPlaneHealthService(
gatewayService,
wsClient,
runtimeState,
openclawProcess,
);
const channelFallbackService = new ChannelFallbackService(
openclawProcess,
gatewayService,
{
getLocale: () => configStore.getDesktopLocale(),
},
);
let syncService: OpenClawSyncService | null = null;
const skillhubService = await SkillhubService.create(env, {
onSyncNeeded: () => {
void syncService?.syncAll().catch(() => {});
},
getBotIds: async () => {
const config = await configStore.getConfig();
return config.bots.map((b) => b.id);
},
});
const openclawSyncService = new OpenClawSyncService(
env,
configStore,
compiledStore,
configWriter,
authProfilesWriter,
authProfilesStore,
runtimePluginWriter,
runtimeModelWriter,
creditGuardStateWriter,
templateWriter,
watchTrigger,
gatewayService,
skillhubService.skillDb,
skillhubService.workspaceSkillScanner,
);
syncService = openclawSyncService;
const openclawAuthService = new OpenClawAuthService(env, authProfilesStore);
const analyticsService = new AnalyticsService(
env,
configStore,
sessionsRuntime,
);
const modelProviderService = new ModelProviderService(
configStore,
env,
openclawSyncService,
openclawProcess,
);
modelProviderService.setAuthService(openclawAuthService);
const runtimeModelStateService = new RuntimeModelStateService(env);
const quotaFallbackService = new QuotaFallbackService(
configStore,
openclawSyncService,
);
const githubStarVerificationService = new GithubStarVerificationService();
const attachmentStore = new AttachmentStore({
openclawStateDir: env.openclawStateDir,
});
// Sweep expired webchat attachments on boot. Fire-and-forget so controller
// startup isn't blocked by disk I/O, and errors are swallowed-with-log by
// the store itself.
void attachmentStore.cleanupExpired().catch((err) => {
logger.warn(
{ error: err instanceof Error ? err.message : String(err) },
"attachment-store: startup cleanup failed",
);
});
configStore.onCloudStateChanged = async (_change) => {
// Auto-select a valid default model: on login, pick a managed model;
// on logout, fall back to any remaining BYOK/OAuth model (or leave
// cleared, which surfaces the explicit no-model guidance).
await modelProviderService.ensureValidDefaultModel();
await openclawSyncService.syncAll();
// Restart the gateway on every login/logout. Provider-level changes
// are not hot-reload-safe: OpenClaw builds its provider/model registry
// once at boot, so without a restart the old providers map stays
// resident in memory and the runtime keeps resolving to stale entries
// (e.g. link/*) after disconnect, or reports "Unknown model" after a
// fresh login because the registry never saw the new providers map.
// `restart()` handles both dev-managed and launchd-managed modes.
await openclawProcess.restart("cloud_state_changed");
};
return {
env,
gatewayClient,
controlPlaneHealth,
openclawProcess,
agentService: new AgentService(configStore, openclawSyncService),
channelService: new ChannelService(
env,
configStore,
openclawSyncService,
gatewayService,
openclawProcess,
controlPlaneHealth,
wsClient,
quotaFallbackService,
),
channelFallbackService,
sessionService: new SessionService(sessionsRuntime),
runtimeConfigService: new RuntimeConfigService(
configStore,
openclawSyncService,
),
runtimeModelStateService,
modelProviderService,
integrationService: new IntegrationService(configStore),
localUserService: new LocalUserService(configStore),
desktopLocalService: new DesktopLocalService(
configStore,
modelProviderService,
openclawProcess,
),
analyticsService,
artifactService: new ArtifactService(artifactsStore),
attachmentStore,
templateService: new TemplateService(configStore, openclawSyncService),
skillhubService,
openclawSyncService,
openclawAuthService,
quotaFallbackService,
githubStarVerificationService,
wsClient,
gatewayService,
configStore,
runtimeState,
startBackgroundLoops: () => {
let isRefreshingNexuOfficialModels = false;
const stopHealthLoop = startHealthLoop({
env,
state: runtimeState,
controlPlaneHealth,
processManager: openclawProcess,
wsClient,
});
const stopAnalyticsLoop = startAnalyticsLoop({
env,
analyticsService,
});
const nexuOfficialModelRefreshInterval = setInterval(() => {
if (isRefreshingNexuOfficialModels) {
return;
}
isRefreshingNexuOfficialModels = true;
void modelProviderService
.refreshNexuOfficialModels()
.catch((error) => {
logger.warn(
{
error: error instanceof Error ? error.message : String(error),
},
"nexu_official_model_refresh_failed",
);
})
.finally(() => {
isRefreshingNexuOfficialModels = false;
});
}, NEXU_OFFICIAL_MODEL_REFRESH_INTERVAL_MS);
nexuOfficialModelRefreshInterval.unref?.();
skillhubService.start();
return () => {
stopHealthLoop();
stopAnalyticsLoop();
clearInterval(nexuOfficialModelRefreshInterval);
skillhubService.dispose();
openclawAuthService.dispose();
channelFallbackService.stop();
wsClient.stop();
};
},
};
}