@pooder/core 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,118 +1,118 @@
1
- import { Contribution, ContributionPoint, ContributionPointIds } from "./index";
2
- import Disposable from "../disposable";
3
-
4
- export class ContributionRegistry {
5
- private points: Map<string, ContributionPoint> = new Map();
6
- private contributions = {
7
- byId: new Map<string, Contribution>(),
8
- byPointId: new Map<string, Contribution[]>(),
9
- };
10
-
11
- /**
12
- * Register a new contribution point
13
- */
14
- registerPoint<T>(point: ContributionPoint<T>): void {
15
- if (this.points.has(point.id)) {
16
- console.warn(
17
- `Contribution point ${point.id} already exists. Overwriting definitions may cause issues.`,
18
- );
19
- }
20
- this.points.set(point.id, point);
21
- if (!this.contributions.byPointId.has(point.id)) {
22
- this.contributions.byPointId.set(point.id, []);
23
- }
24
- }
25
-
26
- /**
27
- * Register a contribution to a specific point
28
- * @returns Disposable to unregister the contribution
29
- */
30
- register<T>(pointId: string, contribution: Contribution<T>): Disposable {
31
- if (this.contributions.byId.has(contribution.id)) {
32
- console.warn(
33
- `Contribution with ID "${contribution.id}" is already registered. Overwriting.`,
34
- );
35
- // We could choose to throw, or overwrite. Let's overwrite for now but warn.
36
- // If we overwrite, we should probably remove the old one from the list first to avoid duplicates.
37
- this.unregister(pointId, contribution.id);
38
- }
39
-
40
- if (!this.points.has(pointId)) {
41
- console.warn(
42
- `Contribution point ${pointId} does not exist. The contribution ${contribution.id} will be queued but may not be valid.`,
43
- );
44
- if (!this.contributions.byPointId.has(pointId)) {
45
- this.contributions.byPointId.set(pointId, []);
46
- }
47
- }
48
-
49
- const point = this.points.get(pointId);
50
- if (point?.validate) {
51
- try {
52
- if (!point.validate(contribution.data)) {
53
- console.error(
54
- `Contribution ${contribution.id} failed validation for point ${pointId}.`,
55
- );
56
- return { dispose: () => {} };
57
- }
58
- } catch (e) {
59
- console.error(
60
- `Validation error for contribution ${contribution.id}:`,
61
- e,
62
- );
63
- return { dispose: () => {} };
64
- }
65
- }
66
-
67
- const arr = this.contributions.byPointId.get(pointId)!;
68
-
69
- arr.push(contribution);
70
- this.contributions.byId.set(contribution.id, contribution);
71
-
72
- // Auto-register if this is a contribution point contribution
73
- if (pointId === ContributionPointIds.CONTRIBUTIONS) {
74
- this.registerPoint(contribution.data as ContributionPoint);
75
- }
76
-
77
- return {
78
- dispose: () => {
79
- this.unregister(pointId, contribution.id);
80
- },
81
- };
82
- }
83
-
84
- /**
85
- * Get all contributions for a given point
86
- */
87
- get<T>(pointId: string): Contribution<T>[] {
88
- return Array.from(
89
- this.contributions.byPointId.get(pointId)?.values() || [],
90
- ) as Contribution<T>[];
91
- }
92
-
93
- /**
94
- * Get a specific contribution by ID
95
- */
96
- getById<T>(id: string): Contribution<T> | undefined {
97
- return this.contributions.byId.get(id) as Contribution<T> | undefined;
98
- }
99
-
100
- /**
101
- * Get the contribution point definition
102
- */
103
- getPoint(pointId: string): ContributionPoint | undefined {
104
- return this.points.get(pointId);
105
- }
106
-
107
- /**
108
- * Unregister a contribution
109
- */
110
- private unregister(pointId: string, contributionId: string): void {
111
- const arr = this.contributions.byPointId.get(pointId);
112
- arr?.splice(
113
- arr.findIndex((c) => c.id === contributionId),
114
- 1,
115
- );
116
- this.contributions.byId.delete(contributionId);
117
- }
118
- }
1
+ import { Contribution, ContributionPoint, ContributionPointIds } from "./index";
2
+ import Disposable from "../disposable";
3
+
4
+ export class ContributionRegistry {
5
+ private points: Map<string, ContributionPoint> = new Map();
6
+ private contributions = {
7
+ byId: new Map<string, Contribution>(),
8
+ byPointId: new Map<string, Contribution[]>(),
9
+ };
10
+
11
+ /**
12
+ * Register a new contribution point
13
+ */
14
+ registerPoint<T>(point: ContributionPoint<T>): void {
15
+ if (this.points.has(point.id)) {
16
+ console.warn(
17
+ `Contribution point ${point.id} already exists. Overwriting definitions may cause issues.`,
18
+ );
19
+ }
20
+ this.points.set(point.id, point);
21
+ if (!this.contributions.byPointId.has(point.id)) {
22
+ this.contributions.byPointId.set(point.id, []);
23
+ }
24
+ }
25
+
26
+ /**
27
+ * Register a contribution to a specific point
28
+ * @returns Disposable to unregister the contribution
29
+ */
30
+ register<T>(pointId: string, contribution: Contribution<T>): Disposable {
31
+ if (this.contributions.byId.has(contribution.id)) {
32
+ console.warn(
33
+ `Contribution with ID "${contribution.id}" is already registered. Overwriting.`,
34
+ );
35
+ // We could choose to throw, or overwrite. Let's overwrite for now but warn.
36
+ // If we overwrite, we should probably remove the old one from the list first to avoid duplicates.
37
+ this.unregister(pointId, contribution.id);
38
+ }
39
+
40
+ if (!this.points.has(pointId)) {
41
+ console.warn(
42
+ `Contribution point ${pointId} does not exist. The contribution ${contribution.id} will be queued but may not be valid.`,
43
+ );
44
+ if (!this.contributions.byPointId.has(pointId)) {
45
+ this.contributions.byPointId.set(pointId, []);
46
+ }
47
+ }
48
+
49
+ const point = this.points.get(pointId);
50
+ if (point?.validate) {
51
+ try {
52
+ if (!point.validate(contribution.data)) {
53
+ console.error(
54
+ `Contribution ${contribution.id} failed validation for point ${pointId}.`,
55
+ );
56
+ return { dispose: () => {} };
57
+ }
58
+ } catch (e) {
59
+ console.error(
60
+ `Validation error for contribution ${contribution.id}:`,
61
+ e,
62
+ );
63
+ return { dispose: () => {} };
64
+ }
65
+ }
66
+
67
+ const arr = this.contributions.byPointId.get(pointId)!;
68
+
69
+ arr.push(contribution);
70
+ this.contributions.byId.set(contribution.id, contribution);
71
+
72
+ // Auto-register if this is a contribution point contribution
73
+ if (pointId === ContributionPointIds.CONTRIBUTIONS) {
74
+ this.registerPoint(contribution.data as ContributionPoint);
75
+ }
76
+
77
+ return {
78
+ dispose: () => {
79
+ this.unregister(pointId, contribution.id);
80
+ },
81
+ };
82
+ }
83
+
84
+ /**
85
+ * Get all contributions for a given point
86
+ */
87
+ get<T>(pointId: string): Contribution<T>[] {
88
+ return Array.from(
89
+ this.contributions.byPointId.get(pointId)?.values() || [],
90
+ ) as Contribution<T>[];
91
+ }
92
+
93
+ /**
94
+ * Get a specific contribution by ID
95
+ */
96
+ getById<T>(id: string): Contribution<T> | undefined {
97
+ return this.contributions.byId.get(id) as Contribution<T> | undefined;
98
+ }
99
+
100
+ /**
101
+ * Get the contribution point definition
102
+ */
103
+ getPoint(pointId: string): ContributionPoint | undefined {
104
+ return this.points.get(pointId);
105
+ }
106
+
107
+ /**
108
+ * Unregister a contribution
109
+ */
110
+ private unregister(pointId: string, contributionId: string): void {
111
+ const arr = this.contributions.byPointId.get(pointId);
112
+ arr?.splice(
113
+ arr.findIndex((c) => c.id === contributionId),
114
+ 1,
115
+ );
116
+ this.contributions.byId.delete(contributionId);
117
+ }
118
+ }
package/src/disposable.ts CHANGED
@@ -1,3 +1,3 @@
1
- export default interface Disposable {
2
- dispose(): void;
3
- }
1
+ export default interface Disposable {
2
+ dispose(): void;
3
+ }
package/src/extension.ts CHANGED
@@ -1,164 +1,171 @@
1
- import { ExtensionContext } from "./context";
2
- import { Contribution, ContributionPointIds } from "./contribution";
3
- import Disposable from "./disposable";
4
- import CommandService from "./services/CommandService";
5
- import { ConfigurationService } from "./services";
6
-
7
- interface ExtensionMetadata {
8
- name: string;
9
- }
10
-
11
- interface Extension {
12
- id: string;
13
- metadata?: Partial<ExtensionMetadata>;
14
-
15
- activate(context: ExtensionContext): void;
16
- deactivate(context: ExtensionContext): void;
17
- contribute?(): Record<string, any[]>;
18
- }
19
-
20
- class ExtensionRegistry extends Map<string, Extension> {}
21
-
22
- class ExtensionManager {
23
- private readonly context: ExtensionContext;
24
- private extensionRegistry: ExtensionRegistry = new ExtensionRegistry();
25
- private extensionDisposables: Map<string, Disposable[]> = new Map();
26
-
27
- constructor(context: ExtensionContext) {
28
- this.context = context;
29
- }
30
-
31
- register(extension: Extension) {
32
- if (this.extensionRegistry.has(extension.id)) {
33
- console.warn(
34
- `Plugin "${extension.id}" already registered. It will be overwritten.`,
35
- );
36
- }
37
-
38
- // Initialize disposables for this extension
39
- this.extensionDisposables.set(extension.id, []);
40
- const disposables = this.extensionDisposables.get(extension.id)!;
41
-
42
- // Process declarative contributions
43
- if (extension.contribute) {
44
- for (const [pointId, items] of Object.entries(extension.contribute())) {
45
- if (Array.isArray(items)) {
46
- items.forEach((item, index) => {
47
- const contributionId =
48
- item.id ||
49
- (item.command
50
- ? item.command
51
- : `${extension.id}.${pointId}.${index}`);
52
- const contribution: Contribution = {
53
- id: contributionId,
54
- metadata: {
55
- extensionId: extension.id,
56
- ...item?.metadata,
57
- },
58
- data: item,
59
- };
60
- const disposable = this.context.contributions.register(
61
- pointId,
62
- contribution,
63
- );
64
-
65
- // Track contribution registration to unregister later
66
- disposables.push(disposable);
67
-
68
- const dispose = this.collectContribution(pointId, contribution);
69
- if (dispose) {
70
- disposables.push(dispose);
71
- }
72
- });
73
- }
74
- }
75
- }
76
-
77
- try {
78
- this.extensionRegistry.set(extension.id, extension);
79
- this.context.eventBus.emit("extension:register", extension);
80
- } catch (error) {
81
- console.error(
82
- `Error in onCreate hook for plugin "${extension.id}":`,
83
- error,
84
- );
85
- }
86
-
87
- try {
88
- extension.activate(this.context);
89
- } catch (error) {
90
- console.error(
91
- `Error in onActivate hook for plugin "${extension.id}":`,
92
- error,
93
- );
94
- }
95
-
96
- console.log(`Plugin "${extension.id}" registered successfully`);
97
- }
98
-
99
- collectContribution(pointId: string, item: any): Disposable | undefined {
100
- // If registering configurations, update ConfigurationService defaults
101
- if (pointId === ContributionPointIds.CONFIGURATIONS) {
102
- const configService = this.context.services.get<ConfigurationService>(
103
- "ConfigurationService",
104
- );
105
- configService?.initializeDefaults([item.data]);
106
- }
107
- if (pointId === ContributionPointIds.COMMANDS && item.data.handler) {
108
- const commandService =
109
- this.context.services.get<CommandService>("CommandService")!;
110
-
111
- return commandService.registerCommand(item.id, item.data.handler);
112
- }
113
- }
114
-
115
- unregister(name: string) {
116
- const extension = this.extensionRegistry.get(name);
117
- if (!extension) {
118
- console.warn(`Plugin "${name}" not found.`);
119
- return;
120
- }
121
-
122
- try {
123
- extension.deactivate(this.context);
124
- } catch (error) {
125
- console.error(`Error in deactivate for plugin "${name}":`, error);
126
- }
127
-
128
- // Dispose all resources associated with this extension
129
- const disposables = this.extensionDisposables.get(name);
130
- if (disposables) {
131
- disposables.forEach((d) => d.dispose());
132
- this.extensionDisposables.delete(name);
133
- }
134
-
135
- this.extensionRegistry.delete(name);
136
- console.log(`Plugin "${name}" unregistered`);
137
- return true;
138
- }
139
-
140
- enable(name: string) {
141
- const extension = this.extensionRegistry.get(name);
142
- if (!extension) {
143
- console.warn(`Plugin "${name}" not found.`);
144
- return;
145
- }
146
- }
147
-
148
- disable(name: string) {
149
- const extension = this.extensionRegistry.get(name);
150
- if (!extension) {
151
- console.warn(`Plugin "${name}" not found.`);
152
- return;
153
- }
154
- }
155
-
156
- update() {}
157
-
158
- destroy() {
159
- const extensionNames = Array.from(this.extensionRegistry.keys());
160
- extensionNames.forEach((name) => this.unregister(name));
161
- }
162
- }
163
-
164
- export { Extension, ExtensionMetadata, ExtensionRegistry, ExtensionManager };
1
+ import { ExtensionContext } from "./context";
2
+ import { Contribution, ContributionPointIds } from "./contribution";
3
+ import Disposable from "./disposable";
4
+ import CommandService from "./services/CommandService";
5
+ import { ConfigurationService } from "./services";
6
+ import ToolRegistryService from "./services/ToolRegistryService";
7
+
8
+ interface ExtensionMetadata {
9
+ name: string;
10
+ }
11
+
12
+ interface Extension {
13
+ id: string;
14
+ metadata?: Partial<ExtensionMetadata>;
15
+
16
+ activate(context: ExtensionContext): void;
17
+ deactivate(context: ExtensionContext): void;
18
+ contribute?(): Record<string, any[]>;
19
+ }
20
+
21
+ class ExtensionRegistry extends Map<string, Extension> {}
22
+
23
+ class ExtensionManager {
24
+ private readonly context: ExtensionContext;
25
+ private extensionRegistry: ExtensionRegistry = new ExtensionRegistry();
26
+ private extensionDisposables: Map<string, Disposable[]> = new Map();
27
+
28
+ constructor(context: ExtensionContext) {
29
+ this.context = context;
30
+ }
31
+
32
+ register(extension: Extension) {
33
+ if (this.extensionRegistry.has(extension.id)) {
34
+ console.warn(
35
+ `Plugin "${extension.id}" already registered. It will be overwritten.`,
36
+ );
37
+ }
38
+
39
+ // Initialize disposables for this extension
40
+ this.extensionDisposables.set(extension.id, []);
41
+ const disposables = this.extensionDisposables.get(extension.id)!;
42
+
43
+ // Process declarative contributions
44
+ if (extension.contribute) {
45
+ for (const [pointId, items] of Object.entries(extension.contribute())) {
46
+ if (Array.isArray(items)) {
47
+ items.forEach((item, index) => {
48
+ const contributionId =
49
+ item.id ||
50
+ (item.command
51
+ ? item.command
52
+ : `${extension.id}.${pointId}.${index}`);
53
+ const contribution: Contribution = {
54
+ id: contributionId,
55
+ metadata: {
56
+ extensionId: extension.id,
57
+ ...item?.metadata,
58
+ },
59
+ data: item,
60
+ };
61
+ const disposable = this.context.contributions.register(
62
+ pointId,
63
+ contribution,
64
+ );
65
+
66
+ // Track contribution registration to unregister later
67
+ disposables.push(disposable);
68
+
69
+ const dispose = this.collectContribution(pointId, contribution);
70
+ if (dispose) {
71
+ disposables.push(dispose);
72
+ }
73
+ });
74
+ }
75
+ }
76
+ }
77
+
78
+ try {
79
+ this.extensionRegistry.set(extension.id, extension);
80
+ this.context.eventBus.emit("extension:register", extension);
81
+ } catch (error) {
82
+ console.error(
83
+ `Error in onCreate hook for plugin "${extension.id}":`,
84
+ error,
85
+ );
86
+ }
87
+
88
+ try {
89
+ extension.activate(this.context);
90
+ } catch (error) {
91
+ console.error(
92
+ `Error in onActivate hook for plugin "${extension.id}":`,
93
+ error,
94
+ );
95
+ }
96
+
97
+ console.log(`Plugin "${extension.id}" registered successfully`);
98
+ }
99
+
100
+ collectContribution(pointId: string, item: any): Disposable | undefined {
101
+ // If registering configurations, update ConfigurationService defaults
102
+ if (pointId === ContributionPointIds.CONFIGURATIONS) {
103
+ const configService = this.context.services.get<ConfigurationService>(
104
+ "ConfigurationService",
105
+ );
106
+ configService?.initializeDefaults([item.data]);
107
+ }
108
+ if (pointId === ContributionPointIds.COMMANDS && item.data.handler) {
109
+ const commandService =
110
+ this.context.services.get<CommandService>("CommandService")!;
111
+
112
+ return commandService.registerCommand(item.id, item.data.handler);
113
+ }
114
+ if (pointId === ContributionPointIds.TOOLS) {
115
+ const toolRegistry =
116
+ this.context.services.get<ToolRegistryService>("ToolRegistryService");
117
+ if (!toolRegistry) return;
118
+ return toolRegistry.registerTool(item.data);
119
+ }
120
+ }
121
+
122
+ unregister(name: string) {
123
+ const extension = this.extensionRegistry.get(name);
124
+ if (!extension) {
125
+ console.warn(`Plugin "${name}" not found.`);
126
+ return;
127
+ }
128
+
129
+ try {
130
+ extension.deactivate(this.context);
131
+ } catch (error) {
132
+ console.error(`Error in deactivate for plugin "${name}":`, error);
133
+ }
134
+
135
+ // Dispose all resources associated with this extension
136
+ const disposables = this.extensionDisposables.get(name);
137
+ if (disposables) {
138
+ disposables.forEach((d) => d.dispose());
139
+ this.extensionDisposables.delete(name);
140
+ }
141
+
142
+ this.extensionRegistry.delete(name);
143
+ console.log(`Plugin "${name}" unregistered`);
144
+ return true;
145
+ }
146
+
147
+ enable(name: string) {
148
+ const extension = this.extensionRegistry.get(name);
149
+ if (!extension) {
150
+ console.warn(`Plugin "${name}" not found.`);
151
+ return;
152
+ }
153
+ }
154
+
155
+ disable(name: string) {
156
+ const extension = this.extensionRegistry.get(name);
157
+ if (!extension) {
158
+ console.warn(`Plugin "${name}" not found.`);
159
+ return;
160
+ }
161
+ }
162
+
163
+ update() {}
164
+
165
+ destroy() {
166
+ const extensionNames = Array.from(this.extensionRegistry.keys());
167
+ extensionNames.forEach((name) => this.unregister(name));
168
+ }
169
+ }
170
+
171
+ export { Extension, ExtensionMetadata, ExtensionRegistry, ExtensionManager };