sloth-d2c-mcp 1.0.4-beta100

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.
Files changed (34) hide show
  1. package/README.md +83 -0
  2. package/cli/run.js +328 -0
  3. package/cli/sloth-server.log +1622 -0
  4. package/dist/build/config-manager/index.js +240 -0
  5. package/dist/build/core/prompt-builder.js +366 -0
  6. package/dist/build/core/sampling.js +375 -0
  7. package/dist/build/core/types.js +1 -0
  8. package/dist/build/index.js +852 -0
  9. package/dist/build/interceptor/client.js +142 -0
  10. package/dist/build/interceptor/vscode.js +143 -0
  11. package/dist/build/interceptor/web.js +28 -0
  12. package/dist/build/plugin/index.js +4 -0
  13. package/dist/build/plugin/loader.js +349 -0
  14. package/dist/build/plugin/manager.js +129 -0
  15. package/dist/build/plugin/types.js +6 -0
  16. package/dist/build/server.js +2116 -0
  17. package/dist/build/socket-client.js +166 -0
  18. package/dist/build/socket-server.js +260 -0
  19. package/dist/build/utils/client-capabilities.js +143 -0
  20. package/dist/build/utils/extract.js +168 -0
  21. package/dist/build/utils/file-manager.js +868 -0
  22. package/dist/build/utils/image-matcher.js +154 -0
  23. package/dist/build/utils/logger.js +90 -0
  24. package/dist/build/utils/opencv-loader.js +70 -0
  25. package/dist/build/utils/prompt-parser.js +46 -0
  26. package/dist/build/utils/tj.js +139 -0
  27. package/dist/build/utils/update.js +100 -0
  28. package/dist/build/utils/utils.js +184 -0
  29. package/dist/build/utils/vscode-logger.js +133 -0
  30. package/dist/build/utils/webpack-substitutions.js +196 -0
  31. package/dist/interceptor-web/dist/build-report.json +18 -0
  32. package/dist/interceptor-web/dist/detail.html +1 -0
  33. package/dist/interceptor-web/dist/index.html +1 -0
  34. package/package.json +96 -0
@@ -0,0 +1,129 @@
1
+ import { loadAllPlugins, loadWorkspacePlugins } from './loader.js';
2
+ /**
3
+ * 插件管理器 - 负责管理生命周期 hooks 并按顺序调用插件
4
+ */
5
+ class PluginManager {
6
+ plugins = [];
7
+ initialized = false;
8
+ workspaceRoot = '';
9
+ /**
10
+ * 设置工作区根目录
11
+ * @param root 工作区根目录
12
+ */
13
+ setWorkspaceRoot(root) {
14
+ this.workspaceRoot = root;
15
+ // 重置初始化状态,以便重新加载工作区插件
16
+ if (this.initialized) {
17
+ this.reset();
18
+ }
19
+ }
20
+ /**
21
+ * 获取工作区根目录
22
+ */
23
+ getWorkspaceRoot() {
24
+ return this.workspaceRoot;
25
+ }
26
+ /**
27
+ * 初始化插件管理器
28
+ * 如果设置了工作区根目录,则加载工作区插件
29
+ * 否则加载全局插件(兼容旧逻辑)
30
+ */
31
+ async initialize() {
32
+ if (this.initialized) {
33
+ return;
34
+ }
35
+ try {
36
+ if (this.workspaceRoot) {
37
+ // 加载工作区声明的插件
38
+ console.log(`[sloth] 从工作区加载插件: ${this.workspaceRoot}`);
39
+ this.plugins = await loadWorkspacePlugins(this.workspaceRoot);
40
+ }
41
+ else {
42
+ // 兼容旧逻辑:加载全局插件
43
+ console.log(`[sloth] 未设置工作区,加载全局插件`);
44
+ this.plugins = await loadAllPlugins();
45
+ }
46
+ this.initialized = true;
47
+ console.log(`[sloth] 插件管理器初始化完成,已加载 ${this.plugins.length} 个插件`);
48
+ }
49
+ catch (error) {
50
+ console.error('[sloth] 插件管理器初始化失败:', error);
51
+ // 如果是工作区模式,抛出错误让调用方知道
52
+ if (this.workspaceRoot) {
53
+ throw error;
54
+ }
55
+ this.plugins = [];
56
+ this.initialized = true;
57
+ }
58
+ }
59
+ /**
60
+ * 获取已加载的插件列表
61
+ */
62
+ getPlugins() {
63
+ return [...this.plugins];
64
+ }
65
+ /**
66
+ * 注册一个插件
67
+ */
68
+ registerPlugin(plugin) {
69
+ this.plugins.push(plugin);
70
+ console.log(`[sloth] 插件 ${plugin.name} 已注册`);
71
+ }
72
+ /**
73
+ * 执行指定的 hook
74
+ * @param hookName hook 名称
75
+ * @param params hook 参数
76
+ * 按顺序调用所有插件,每个插件都会执行,返回最后一个成功的结果
77
+ * 每个插件执行时会收到上一个插件的信息 { lastPlugin, returnVal }
78
+ */
79
+ async executeHook(hookName, params) {
80
+ if (!this.initialized) {
81
+ await this.initialize();
82
+ }
83
+ const pluginsWithHook = this.plugins.filter((p) => typeof p[hookName] === 'function');
84
+ if (pluginsWithHook.length === 0) {
85
+ console.log(`[sloth] 没有插件注册 ${hookName} hook`);
86
+ return params;
87
+ }
88
+ let lastPlugin = '';
89
+ let lastReturnVal = null;
90
+ for (const plugin of pluginsWithHook) {
91
+ try {
92
+ console.log(`[sloth] 执行插件 ${plugin.name} 的 ${hookName} hook`);
93
+ const hookFn = plugin[hookName];
94
+ const context = {
95
+ lastPlugin,
96
+ returnVal: lastReturnVal,
97
+ config: plugin._config,
98
+ };
99
+ const result = await hookFn({
100
+ ...params,
101
+ ...(lastReturnVal || {}),
102
+ _config: context.config,
103
+ workspaceRoot: this.workspaceRoot,
104
+ }, context);
105
+ // 更新上一个插件信息
106
+ lastPlugin = plugin.name;
107
+ lastReturnVal = {
108
+ ...params,
109
+ ...(lastReturnVal || {}),
110
+ ...(result || {}),
111
+ workspaceRoot: this.workspaceRoot,
112
+ };
113
+ }
114
+ catch (error) {
115
+ console.error(`[sloth] 插件 ${plugin.name} 执行 ${hookName} 出错:`, error);
116
+ }
117
+ }
118
+ return lastReturnVal || params;
119
+ }
120
+ /**
121
+ * 重置插件管理器
122
+ */
123
+ reset() {
124
+ this.plugins = [];
125
+ this.initialized = false;
126
+ }
127
+ }
128
+ // 导出单例实例
129
+ export const pluginManager = new PluginManager();
@@ -0,0 +1,6 @@
1
+ export const PLUGIN_HOOKS = [
2
+ 'afterSubmitConfigData',
3
+ 'onFetchImage',
4
+ 'onSamplingComplete',
5
+ 'beforeFinalPrompt',
6
+ ];