@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.
- package/CHANGELOG.md +6 -0
- package/dist/index.d.mts +100 -4
- package/dist/index.d.ts +100 -4
- package/dist/index.js +280 -4
- package/dist/index.mjs +278 -4
- package/package.json +1 -1
- package/src/command.ts +10 -10
- package/src/context.ts +17 -17
- package/src/contribution/index.ts +12 -12
- package/src/contribution/points.ts +27 -3
- package/src/contribution/registry.ts +118 -118
- package/src/disposable.ts +3 -3
- package/src/extension.ts +171 -164
- package/src/index.ts +161 -145
- package/src/run-test-full.ts +98 -98
- package/src/services/CommandService.ts +79 -79
- package/src/services/ConfigurationService.ts +107 -107
- package/src/services/ToolRegistryService.ts +41 -0
- package/src/services/ToolSessionService.ts +176 -0
- package/src/services/WorkbenchService.ts +138 -9
- package/src/services/index.ts +9 -1
- package/src/test-extension-full.ts +79 -79
|
@@ -1,107 +1,107 @@
|
|
|
1
|
-
import { Service } from "../service";
|
|
2
|
-
import EventBus from "../event";
|
|
3
|
-
import { ConfigurationContribution } from "../contribution";
|
|
4
|
-
|
|
5
|
-
export default class ConfigurationService implements Service {
|
|
6
|
-
private configValues: Map<string, any> = new Map();
|
|
7
|
-
private eventBus: EventBus = new EventBus();
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Get a configuration value.
|
|
11
|
-
*/
|
|
12
|
-
get<T = any>(key: string, defaultValue?: T): T {
|
|
13
|
-
if (this.configValues.has(key)) {
|
|
14
|
-
return this.configValues.get(key);
|
|
15
|
-
}
|
|
16
|
-
return defaultValue as T;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Update a configuration value.
|
|
21
|
-
* Emits 'change' event.
|
|
22
|
-
*/
|
|
23
|
-
update(key: string, value: any) {
|
|
24
|
-
const oldValue = this.configValues.get(key);
|
|
25
|
-
if (oldValue !== value) {
|
|
26
|
-
this.configValues.set(key, value);
|
|
27
|
-
this.eventBus.emit(`change:${key}`, { key, value, oldValue });
|
|
28
|
-
this.eventBus.emit("change", { key, value, oldValue });
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Listen for changes to a specific configuration key.
|
|
34
|
-
*/
|
|
35
|
-
onDidChange(
|
|
36
|
-
key: string,
|
|
37
|
-
callback: (event: { key: string; value: any; oldValue: any }) => void,
|
|
38
|
-
) {
|
|
39
|
-
this.eventBus.on(`change:${key}`, callback);
|
|
40
|
-
return {
|
|
41
|
-
dispose: () => this.eventBus.off(`change:${key}`, callback),
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Listen for any configuration change.
|
|
47
|
-
*/
|
|
48
|
-
onAnyChange(
|
|
49
|
-
callback: (event: { key: string; value: any; oldValue: any }) => void,
|
|
50
|
-
) {
|
|
51
|
-
this.eventBus.on("change", callback);
|
|
52
|
-
return {
|
|
53
|
-
dispose: () => this.eventBus.off("change", callback),
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Export current configuration state as a JSON-serializable object.
|
|
59
|
-
* Useful for saving configuration templates.
|
|
60
|
-
*/
|
|
61
|
-
export(): Record<string, any> {
|
|
62
|
-
const exportData: Record<string, any> = {};
|
|
63
|
-
for (const [key, value] of this.configValues) {
|
|
64
|
-
exportData[key] = value;
|
|
65
|
-
}
|
|
66
|
-
return exportData;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Import configuration from a JSON object.
|
|
71
|
-
* This will merge the provided configuration with the current state,
|
|
72
|
-
* overwriting existing keys and triggering change events.
|
|
73
|
-
*/
|
|
74
|
-
import(data: Record<string, any>): void {
|
|
75
|
-
if (!data || typeof data !== "object") {
|
|
76
|
-
console.warn("ConfigurationService: Import data must be an object.");
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
Object.entries(data).forEach(([key, value]) => {
|
|
80
|
-
this.update(key, value);
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Initialize configuration with defaults from contributions.
|
|
86
|
-
* This should be called when a contribution is registered.
|
|
87
|
-
*/
|
|
88
|
-
initializeDefaults(contributions: ConfigurationContribution[]) {
|
|
89
|
-
contributions.forEach((contrib) => {
|
|
90
|
-
if (!contrib.id) {
|
|
91
|
-
console.warn(
|
|
92
|
-
"Configuration contribution missing 'id'. Skipping default initialization.",
|
|
93
|
-
contrib,
|
|
94
|
-
);
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
if (!this.configValues.has(contrib.id) && contrib.default !== undefined) {
|
|
98
|
-
this.configValues.set(contrib.id, contrib.default);
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
dispose() {
|
|
104
|
-
this.configValues.clear();
|
|
105
|
-
// EventBus doesn't have a clear/dispose in the snippet, but it's fine for now.
|
|
106
|
-
}
|
|
107
|
-
}
|
|
1
|
+
import { Service } from "../service";
|
|
2
|
+
import EventBus from "../event";
|
|
3
|
+
import { ConfigurationContribution } from "../contribution";
|
|
4
|
+
|
|
5
|
+
export default class ConfigurationService implements Service {
|
|
6
|
+
private configValues: Map<string, any> = new Map();
|
|
7
|
+
private eventBus: EventBus = new EventBus();
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Get a configuration value.
|
|
11
|
+
*/
|
|
12
|
+
get<T = any>(key: string, defaultValue?: T): T {
|
|
13
|
+
if (this.configValues.has(key)) {
|
|
14
|
+
return this.configValues.get(key);
|
|
15
|
+
}
|
|
16
|
+
return defaultValue as T;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Update a configuration value.
|
|
21
|
+
* Emits 'change' event.
|
|
22
|
+
*/
|
|
23
|
+
update(key: string, value: any) {
|
|
24
|
+
const oldValue = this.configValues.get(key);
|
|
25
|
+
if (oldValue !== value) {
|
|
26
|
+
this.configValues.set(key, value);
|
|
27
|
+
this.eventBus.emit(`change:${key}`, { key, value, oldValue });
|
|
28
|
+
this.eventBus.emit("change", { key, value, oldValue });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Listen for changes to a specific configuration key.
|
|
34
|
+
*/
|
|
35
|
+
onDidChange(
|
|
36
|
+
key: string,
|
|
37
|
+
callback: (event: { key: string; value: any; oldValue: any }) => void,
|
|
38
|
+
) {
|
|
39
|
+
this.eventBus.on(`change:${key}`, callback);
|
|
40
|
+
return {
|
|
41
|
+
dispose: () => this.eventBus.off(`change:${key}`, callback),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Listen for any configuration change.
|
|
47
|
+
*/
|
|
48
|
+
onAnyChange(
|
|
49
|
+
callback: (event: { key: string; value: any; oldValue: any }) => void,
|
|
50
|
+
) {
|
|
51
|
+
this.eventBus.on("change", callback);
|
|
52
|
+
return {
|
|
53
|
+
dispose: () => this.eventBus.off("change", callback),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Export current configuration state as a JSON-serializable object.
|
|
59
|
+
* Useful for saving configuration templates.
|
|
60
|
+
*/
|
|
61
|
+
export(): Record<string, any> {
|
|
62
|
+
const exportData: Record<string, any> = {};
|
|
63
|
+
for (const [key, value] of this.configValues) {
|
|
64
|
+
exportData[key] = value;
|
|
65
|
+
}
|
|
66
|
+
return exportData;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Import configuration from a JSON object.
|
|
71
|
+
* This will merge the provided configuration with the current state,
|
|
72
|
+
* overwriting existing keys and triggering change events.
|
|
73
|
+
*/
|
|
74
|
+
import(data: Record<string, any>): void {
|
|
75
|
+
if (!data || typeof data !== "object") {
|
|
76
|
+
console.warn("ConfigurationService: Import data must be an object.");
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
Object.entries(data).forEach(([key, value]) => {
|
|
80
|
+
this.update(key, value);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Initialize configuration with defaults from contributions.
|
|
86
|
+
* This should be called when a contribution is registered.
|
|
87
|
+
*/
|
|
88
|
+
initializeDefaults(contributions: ConfigurationContribution[]) {
|
|
89
|
+
contributions.forEach((contrib) => {
|
|
90
|
+
if (!contrib.id) {
|
|
91
|
+
console.warn(
|
|
92
|
+
"Configuration contribution missing 'id'. Skipping default initialization.",
|
|
93
|
+
contrib,
|
|
94
|
+
);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (!this.configValues.has(contrib.id) && contrib.default !== undefined) {
|
|
98
|
+
this.configValues.set(contrib.id, contrib.default);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
dispose() {
|
|
104
|
+
this.configValues.clear();
|
|
105
|
+
// EventBus doesn't have a clear/dispose in the snippet, but it's fine for now.
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ToolContribution } from "../contribution";
|
|
2
|
+
import Disposable from "../disposable";
|
|
3
|
+
import { Service } from "../service";
|
|
4
|
+
|
|
5
|
+
export default class ToolRegistryService implements Service {
|
|
6
|
+
private tools = new Map<string, ToolContribution>();
|
|
7
|
+
|
|
8
|
+
registerTool(tool: ToolContribution): Disposable {
|
|
9
|
+
if (!tool?.id) {
|
|
10
|
+
throw new Error("ToolContribution.id is required.");
|
|
11
|
+
}
|
|
12
|
+
this.tools.set(tool.id, tool);
|
|
13
|
+
return {
|
|
14
|
+
dispose: () => {
|
|
15
|
+
if (this.tools.get(tool.id) === tool) {
|
|
16
|
+
this.tools.delete(tool.id);
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
unregisterTool(toolId: string) {
|
|
23
|
+
this.tools.delete(toolId);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
getTool(toolId: string): ToolContribution | undefined {
|
|
27
|
+
return this.tools.get(toolId);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
listTools(): ToolContribution[] {
|
|
31
|
+
return Array.from(this.tools.values());
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
hasTool(toolId: string): boolean {
|
|
35
|
+
return this.tools.has(toolId);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
dispose() {
|
|
39
|
+
this.tools.clear();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { ToolContribution } from "../contribution";
|
|
2
|
+
import Disposable from "../disposable";
|
|
3
|
+
import { Service } from "../service";
|
|
4
|
+
import CommandService from "./CommandService";
|
|
5
|
+
import ToolRegistryService from "./ToolRegistryService";
|
|
6
|
+
|
|
7
|
+
export type ToolSessionStatus = "idle" | "active";
|
|
8
|
+
|
|
9
|
+
export interface ToolSessionState {
|
|
10
|
+
toolId: string;
|
|
11
|
+
status: ToolSessionStatus;
|
|
12
|
+
dirty: boolean;
|
|
13
|
+
startedAt?: number;
|
|
14
|
+
lastUpdatedAt?: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type LeaveDecision = "allow" | "blocked";
|
|
18
|
+
|
|
19
|
+
export interface LeaveResult {
|
|
20
|
+
decision: LeaveDecision;
|
|
21
|
+
reason?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default class ToolSessionService implements Service {
|
|
25
|
+
private readonly sessions = new Map<string, ToolSessionState>();
|
|
26
|
+
private commandService?: CommandService;
|
|
27
|
+
private toolRegistry?: ToolRegistryService;
|
|
28
|
+
|
|
29
|
+
setCommandService(commandService: CommandService) {
|
|
30
|
+
this.commandService = commandService;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
setToolRegistry(toolRegistry: ToolRegistryService) {
|
|
34
|
+
this.toolRegistry = toolRegistry;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
registerDirtyTracker(toolId: string, callback: () => boolean): Disposable {
|
|
38
|
+
const wrapped = () => {
|
|
39
|
+
try {
|
|
40
|
+
return callback();
|
|
41
|
+
} catch {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
this.dirtyTrackers.set(toolId, wrapped);
|
|
46
|
+
return {
|
|
47
|
+
dispose: () => {
|
|
48
|
+
if (this.dirtyTrackers.get(toolId) === wrapped) {
|
|
49
|
+
this.dirtyTrackers.delete(toolId);
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private readonly dirtyTrackers = new Map<string, () => boolean>();
|
|
56
|
+
|
|
57
|
+
private ensureSession(toolId: string): ToolSessionState {
|
|
58
|
+
const existing = this.sessions.get(toolId);
|
|
59
|
+
if (existing) return existing;
|
|
60
|
+
|
|
61
|
+
const created: ToolSessionState = {
|
|
62
|
+
toolId,
|
|
63
|
+
status: "idle",
|
|
64
|
+
dirty: false,
|
|
65
|
+
};
|
|
66
|
+
this.sessions.set(toolId, created);
|
|
67
|
+
return created;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
getState(toolId: string): ToolSessionState {
|
|
71
|
+
return { ...this.ensureSession(toolId) };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
isDirty(toolId: string): boolean {
|
|
75
|
+
const tracker = this.dirtyTrackers.get(toolId);
|
|
76
|
+
if (tracker) return tracker();
|
|
77
|
+
return this.ensureSession(toolId).dirty;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
markDirty(toolId: string, dirty = true) {
|
|
81
|
+
const session = this.ensureSession(toolId);
|
|
82
|
+
session.dirty = dirty;
|
|
83
|
+
session.lastUpdatedAt = Date.now();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private resolveTool(toolId: string): ToolContribution | undefined {
|
|
87
|
+
return this.toolRegistry?.getTool(toolId);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private async runCommand(commandId: string | undefined, ...args: any[]) {
|
|
91
|
+
if (!commandId || !this.commandService) return undefined;
|
|
92
|
+
return await this.commandService.executeCommand(commandId, ...args);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async begin(toolId: string): Promise<void> {
|
|
96
|
+
const tool = this.resolveTool(toolId);
|
|
97
|
+
const session = this.ensureSession(toolId);
|
|
98
|
+
if (session.status === "active") return;
|
|
99
|
+
|
|
100
|
+
await this.runCommand(tool?.commands?.begin);
|
|
101
|
+
session.status = "active";
|
|
102
|
+
session.startedAt = Date.now();
|
|
103
|
+
session.lastUpdatedAt = session.startedAt;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async validate(toolId: string): Promise<{ ok: boolean; result?: any }> {
|
|
107
|
+
const tool = this.resolveTool(toolId);
|
|
108
|
+
if (!tool?.commands?.validate) {
|
|
109
|
+
return { ok: true };
|
|
110
|
+
}
|
|
111
|
+
const result = await this.runCommand(tool.commands.validate);
|
|
112
|
+
if (result === false) return { ok: false, result };
|
|
113
|
+
if (result && typeof result === "object" && "ok" in result) {
|
|
114
|
+
return { ok: Boolean((result as any).ok), result };
|
|
115
|
+
}
|
|
116
|
+
return { ok: true, result };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async commit(toolId: string): Promise<{ ok: boolean; result?: any }> {
|
|
120
|
+
const tool = this.resolveTool(toolId);
|
|
121
|
+
const validateResult = await this.validate(toolId);
|
|
122
|
+
if (!validateResult.ok) return validateResult;
|
|
123
|
+
|
|
124
|
+
const result = await this.runCommand(tool?.commands?.commit);
|
|
125
|
+
const session = this.ensureSession(toolId);
|
|
126
|
+
session.dirty = false;
|
|
127
|
+
session.status = "idle";
|
|
128
|
+
session.lastUpdatedAt = Date.now();
|
|
129
|
+
return { ok: true, result };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async rollback(toolId: string): Promise<void> {
|
|
133
|
+
const tool = this.resolveTool(toolId);
|
|
134
|
+
await this.runCommand(tool?.commands?.rollback || tool?.commands?.reset);
|
|
135
|
+
const session = this.ensureSession(toolId);
|
|
136
|
+
session.dirty = false;
|
|
137
|
+
session.status = "idle";
|
|
138
|
+
session.lastUpdatedAt = Date.now();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
deactivateSession(toolId: string) {
|
|
142
|
+
const session = this.ensureSession(toolId);
|
|
143
|
+
session.status = "idle";
|
|
144
|
+
session.lastUpdatedAt = Date.now();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async handleBeforeLeave(toolId: string): Promise<LeaveResult> {
|
|
148
|
+
const tool = this.resolveTool(toolId);
|
|
149
|
+
if (!tool) return { decision: "allow" };
|
|
150
|
+
if (tool.interaction !== "session") return { decision: "allow" };
|
|
151
|
+
|
|
152
|
+
const dirty = this.isDirty(toolId);
|
|
153
|
+
if (!dirty) return { decision: "allow" };
|
|
154
|
+
|
|
155
|
+
const leavePolicy = tool.session?.leavePolicy ?? "block";
|
|
156
|
+
if (leavePolicy === "commit") {
|
|
157
|
+
const committed = await this.commit(toolId);
|
|
158
|
+
if (!committed.ok) {
|
|
159
|
+
return { decision: "blocked", reason: "session-validation-failed" };
|
|
160
|
+
}
|
|
161
|
+
return { decision: "allow" };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (leavePolicy === "rollback") {
|
|
165
|
+
await this.rollback(toolId);
|
|
166
|
+
return { decision: "allow" };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return { decision: "blocked", reason: "session-dirty" };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
dispose() {
|
|
173
|
+
this.sessions.clear();
|
|
174
|
+
this.dirtyTrackers.clear();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
@@ -1,30 +1,159 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Disposable from "../disposable";
|
|
2
2
|
import EventBus from "../event";
|
|
3
|
+
import { Service } from "../service";
|
|
4
|
+
import ToolRegistryService from "./ToolRegistryService";
|
|
5
|
+
import ToolSessionService from "./ToolSessionService";
|
|
6
|
+
|
|
7
|
+
export interface ToolSwitchContext {
|
|
8
|
+
from: string | null;
|
|
9
|
+
to: string | null;
|
|
10
|
+
reason?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface ToolSwitchResult {
|
|
14
|
+
ok: boolean;
|
|
15
|
+
from: string | null;
|
|
16
|
+
to: string | null;
|
|
17
|
+
reason?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type ToolSwitchGuard = (
|
|
21
|
+
context: ToolSwitchContext,
|
|
22
|
+
) => boolean | Promise<boolean>;
|
|
23
|
+
|
|
24
|
+
interface GuardItem {
|
|
25
|
+
guard: ToolSwitchGuard;
|
|
26
|
+
priority: number;
|
|
27
|
+
}
|
|
3
28
|
|
|
4
29
|
export default class WorkbenchService implements Service {
|
|
5
30
|
private _activeToolId: string | null = null;
|
|
6
31
|
private eventBus?: EventBus;
|
|
32
|
+
private toolRegistry?: ToolRegistryService;
|
|
33
|
+
private sessionService?: ToolSessionService;
|
|
34
|
+
private guards: GuardItem[] = [];
|
|
7
35
|
|
|
8
|
-
init() {
|
|
9
|
-
// Initialization logic if needed
|
|
10
|
-
}
|
|
36
|
+
init() {}
|
|
11
37
|
|
|
12
38
|
dispose() {
|
|
13
|
-
|
|
39
|
+
this.guards = [];
|
|
14
40
|
}
|
|
15
41
|
|
|
16
42
|
setEventBus(bus: EventBus) {
|
|
17
43
|
this.eventBus = bus;
|
|
18
44
|
}
|
|
19
45
|
|
|
20
|
-
|
|
46
|
+
setToolRegistry(toolRegistry: ToolRegistryService) {
|
|
47
|
+
this.toolRegistry = toolRegistry;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
setToolSessionService(sessionService: ToolSessionService) {
|
|
51
|
+
this.sessionService = sessionService;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
get activeToolId(): string | null {
|
|
21
55
|
return this._activeToolId;
|
|
22
56
|
}
|
|
23
57
|
|
|
24
|
-
|
|
25
|
-
|
|
58
|
+
registerSwitchGuard(
|
|
59
|
+
guard: ToolSwitchGuard,
|
|
60
|
+
priority: number = 0,
|
|
61
|
+
): Disposable {
|
|
62
|
+
const item: GuardItem = { guard, priority };
|
|
63
|
+
this.guards.push(item);
|
|
64
|
+
this.guards.sort((a, b) => b.priority - a.priority);
|
|
65
|
+
return {
|
|
66
|
+
dispose: () => {
|
|
67
|
+
const index = this.guards.indexOf(item);
|
|
68
|
+
if (index >= 0) this.guards.splice(index, 1);
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
private async runGuards(context: ToolSwitchContext): Promise<boolean> {
|
|
74
|
+
for (const { guard } of this.guards) {
|
|
75
|
+
const allowed = await Promise.resolve(guard(context));
|
|
76
|
+
if (!allowed) return false;
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async switchTool(
|
|
82
|
+
id: string | null,
|
|
83
|
+
options?: { reason?: string },
|
|
84
|
+
): Promise<ToolSwitchResult> {
|
|
85
|
+
if (this._activeToolId === id) {
|
|
86
|
+
return { ok: true, from: this._activeToolId, to: id };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (id && this.toolRegistry && !this.toolRegistry.hasTool(id)) {
|
|
90
|
+
return {
|
|
91
|
+
ok: false,
|
|
92
|
+
from: this._activeToolId,
|
|
93
|
+
to: id,
|
|
94
|
+
reason: `tool-not-registered:${id}`,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const context: ToolSwitchContext = {
|
|
99
|
+
from: this._activeToolId,
|
|
100
|
+
to: id,
|
|
101
|
+
reason: options?.reason,
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const guardAllowed = await this.runGuards(context);
|
|
105
|
+
if (!guardAllowed) {
|
|
106
|
+
this.eventBus?.emit("tool:switch:blocked", {
|
|
107
|
+
...context,
|
|
108
|
+
reason: "blocked-by-guard",
|
|
109
|
+
});
|
|
110
|
+
return {
|
|
111
|
+
ok: false,
|
|
112
|
+
from: this._activeToolId,
|
|
113
|
+
to: id,
|
|
114
|
+
reason: "blocked-by-guard",
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (context.from && this.sessionService) {
|
|
119
|
+
const leaveResult = await this.sessionService.handleBeforeLeave(
|
|
120
|
+
context.from,
|
|
121
|
+
);
|
|
122
|
+
if (leaveResult.decision === "blocked") {
|
|
123
|
+
this.eventBus?.emit("tool:switch:blocked", {
|
|
124
|
+
...context,
|
|
125
|
+
reason: leaveResult.reason || "session-blocked",
|
|
126
|
+
});
|
|
127
|
+
return {
|
|
128
|
+
ok: false,
|
|
129
|
+
from: this._activeToolId,
|
|
130
|
+
to: id,
|
|
131
|
+
reason: leaveResult.reason || "session-blocked",
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
this.sessionService.deactivateSession(context.from);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (id && this.sessionService && this.toolRegistry) {
|
|
138
|
+
const tool = this.toolRegistry.getTool(id);
|
|
139
|
+
if (tool?.interaction === "session" && tool.session?.autoBegin !== false) {
|
|
140
|
+
await this.sessionService.begin(id);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
26
144
|
const previous = this._activeToolId;
|
|
27
145
|
this._activeToolId = id;
|
|
28
|
-
|
|
146
|
+
const reason = options?.reason;
|
|
147
|
+
this.eventBus?.emit("tool:activated", { id, previous, reason });
|
|
148
|
+
this.eventBus?.emit("tool:switch", { from: previous, to: id, reason });
|
|
149
|
+
return { ok: true, from: previous, to: id };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async activate(id: string | null): Promise<ToolSwitchResult> {
|
|
153
|
+
return await this.switchTool(id, { reason: "activate" });
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async deactivate(): Promise<ToolSwitchResult> {
|
|
157
|
+
return await this.switchTool(null, { reason: "deactivate" });
|
|
29
158
|
}
|
|
30
159
|
}
|
package/src/services/index.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import CommandService from "./CommandService";
|
|
2
2
|
import ConfigurationService from "./ConfigurationService";
|
|
3
|
+
import ToolRegistryService from "./ToolRegistryService";
|
|
4
|
+
import ToolSessionService from "./ToolSessionService";
|
|
3
5
|
import WorkbenchService from "./WorkbenchService";
|
|
4
6
|
|
|
5
|
-
export {
|
|
7
|
+
export {
|
|
8
|
+
CommandService,
|
|
9
|
+
ConfigurationService,
|
|
10
|
+
ToolRegistryService,
|
|
11
|
+
ToolSessionService,
|
|
12
|
+
WorkbenchService,
|
|
13
|
+
};
|