-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCocoonMain.ts
More file actions
117 lines (103 loc) · 3.11 KB
/
CocoonMain.ts
File metadata and controls
117 lines (103 loc) · 3.11 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
/**
* @module CocoonMain
* @description
* Main entry point for Cocoon extension host.
* Bootstrap script that initializes all services and starts the extension host.
*
* Supports both old-style service-based architecture and new Effect-TS based architecture.
*/
import { NodeRuntime } from "@effect/platform-node";
import { Effect } from "effect";
// Effect services
import { BootstrapTag, TelemetryTag } from "../../Effect/index.js";
import { EffectServices } from "../../ServiceMapping.js";
// ============================================================================
// EFFECT-BASED BOOTSTRAP (NEW APPROACH)
// ============================================================================
/**
* Bootstrap the Cocoon extension host using Effect-TS services
* This is the modern, recommended approach
*/
const bootstrapCocoonEffect = Effect.gen(function* () {
const telemetry = yield* TelemetryTag;
const bootstrap = yield* BootstrapTag;
telemetry.log(
"info",
"[CocoonMain] Starting Cocoon bootstrap with Effect-TS...",
);
// Run the Effect-TS bootstrap orchestration
const result = yield* bootstrap.run({ debugMode: false });
if (!result.success) {
telemetry.log("error", "[CocoonMain] Bootstrap failed");
for (const stage of result.stages) {
if (!stage.success) {
telemetry.log(
"error",
`[CocoonMain] - ${stage.stageName}: ${stage.error?.message}`,
);
}
}
return yield* Effect.die(new Error("Bootstrap failed"));
}
telemetry.log("info", "[CocoonMain] 🟢 Bootstrap completed successfully");
telemetry.log(
"info",
`[CocoonMain] Total bootstrap time: ${result.totalDuration}ms`,
);
// TODO: Enter main event loop for extension handling
telemetry.log("info", "[CocoonMain] Extension host ready");
});
/**
* Map unknown errors to Error type for consistent handling
*/
const mapUnknownToError = (error: unknown): Error => {
if (error instanceof Error) {
return error;
}
return new Error(String(error));
};
/**
* Main effect with error handling and cleanup
*/
const mainEffectWithServices = bootstrapCocoonEffect.pipe(
Effect.tapError((error) =>
Effect.gen(function* () {
const telemetry = yield* TelemetryTag;
const mappedError = mapUnknownToError(error);
telemetry.log(
"error",
`[CocoonMain] Fatal error: ${mappedError.message}`,
);
if (mappedError.stack) {
telemetry.log(
"error",
`[CocoonMain] Error stack: ${mappedError.stack}`,
);
}
}),
),
Effect.ensuring(
Effect.gen(function* () {
const telemetry = yield* TelemetryTag;
telemetry.log(
"info",
"[CocoonMain] Cocoon extension host shutting down",
);
}),
),
);
/**
* Provide all service layers to create a runnable effect
*/
const mainEffect = mainEffectWithServices.pipe(
Effect.provide(EffectServices.composeAppLayer()),
Effect.scoped,
);
// ============================================================================
// MAIN ENTRY POINT
// ============================================================================
/**
* Main entry point for Cocoon extension host
* Uses Effect-TS NodeRuntime to run the application
*/
NodeRuntime.runMain(mainEffect);