@spaceflow/core 0.14.0 → 0.15.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/dist/index.js +35 -36
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/cli-runtime/di/container.ts +12 -1
- package/src/cli-runtime/di/services.ts +3 -2
- package/src/cli-runtime/extension-loader.ts +18 -8
- package/src/commands/mcp/mcp.service.ts +6 -9
- package/src/extension-system/define-extension.ts +2 -16
- package/src/extension-system/extension.interface.ts +0 -6
- package/src/extension-system/types.ts +4 -16
- package/src/index.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spaceflow/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "Spaceflow 核心能力库",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Lydanne",
|
|
@@ -99,7 +99,7 @@
|
|
|
99
99
|
"zod-to-json-schema": "^3.25.1",
|
|
100
100
|
"commander": "^12.1.0",
|
|
101
101
|
"i18next": "^25.8.4",
|
|
102
|
-
"@spaceflow/shared": "0.
|
|
102
|
+
"@spaceflow/shared": "0.6.0"
|
|
103
103
|
},
|
|
104
104
|
"devDependencies": {
|
|
105
105
|
"@swc/core": "1.15.3",
|
|
@@ -25,6 +25,7 @@ interface ServiceRegistration<T = unknown> {
|
|
|
25
25
|
*/
|
|
26
26
|
export class ServiceContainer implements SpaceflowContext {
|
|
27
27
|
private registrations = new Map<string, ServiceRegistration>();
|
|
28
|
+
private _cwd!: string;
|
|
28
29
|
private _config!: IConfigReader;
|
|
29
30
|
private _output!: IOutputService;
|
|
30
31
|
private _storage!: IStorageService;
|
|
@@ -32,7 +33,13 @@ export class ServiceContainer implements SpaceflowContext {
|
|
|
32
33
|
/**
|
|
33
34
|
* 设置核心服务(config, output, storage)
|
|
34
35
|
*/
|
|
35
|
-
setCoreServices(
|
|
36
|
+
setCoreServices(
|
|
37
|
+
config: IConfigReader,
|
|
38
|
+
output: IOutputService,
|
|
39
|
+
storage: IStorageService,
|
|
40
|
+
cwd: string,
|
|
41
|
+
): void {
|
|
42
|
+
this._cwd = cwd;
|
|
36
43
|
this._config = config;
|
|
37
44
|
this._output = output;
|
|
38
45
|
this._storage = storage;
|
|
@@ -95,6 +102,10 @@ export class ServiceContainer implements SpaceflowContext {
|
|
|
95
102
|
}
|
|
96
103
|
}
|
|
97
104
|
|
|
105
|
+
get cwd(): string {
|
|
106
|
+
return this._cwd;
|
|
107
|
+
}
|
|
108
|
+
|
|
98
109
|
get config(): IConfigReader {
|
|
99
110
|
return this._config;
|
|
100
111
|
}
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
LlmProxyService,
|
|
7
7
|
FileAdapter,
|
|
8
8
|
} from "@spaceflow/core";
|
|
9
|
+
import { findProjectRoot } from "@spaceflow/shared";
|
|
9
10
|
import { join } from "path";
|
|
10
11
|
import type { ServiceContainer } from "./container";
|
|
11
12
|
import { UnifiedConfigReader } from "./config";
|
|
@@ -14,13 +15,13 @@ import { UnifiedConfigReader } from "./config";
|
|
|
14
15
|
* 初始化服务容器
|
|
15
16
|
*/
|
|
16
17
|
export function initializeContainer(container: ServiceContainer, cwd?: string): void {
|
|
17
|
-
const workDir = cwd || process.
|
|
18
|
+
const workDir = cwd || process.env.SPACEFLOW_CWD || findProjectRoot();
|
|
18
19
|
// 初始化核心服务(.env 已在 CLI 壳子阶段加载)
|
|
19
20
|
const config = new UnifiedConfigReader(workDir);
|
|
20
21
|
const output = new OutputService();
|
|
21
22
|
const storageDir = join(workDir, ".spaceflow", "cache");
|
|
22
23
|
const storage = new StorageService(new FileAdapter(storageDir));
|
|
23
|
-
container.setCoreServices(config, output, storage);
|
|
24
|
+
container.setCoreServices(config, output, storage, workDir);
|
|
24
25
|
// 注册服务工厂
|
|
25
26
|
registerServiceFactories(container);
|
|
26
27
|
}
|
|
@@ -11,6 +11,16 @@ export class ExtensionLoader {
|
|
|
11
11
|
|
|
12
12
|
constructor(private readonly ctx: SpaceflowContext) {}
|
|
13
13
|
|
|
14
|
+
/** 当前工作目录(项目根) */
|
|
15
|
+
get cwd(): string {
|
|
16
|
+
return this.ctx.cwd;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** 获取上下文(供内部命令使用) */
|
|
20
|
+
getContext(): SpaceflowContext {
|
|
21
|
+
return this.ctx;
|
|
22
|
+
}
|
|
23
|
+
|
|
14
24
|
/**
|
|
15
25
|
* 注册扩展
|
|
16
26
|
*/
|
|
@@ -56,19 +66,19 @@ export class ExtensionLoader {
|
|
|
56
66
|
}
|
|
57
67
|
|
|
58
68
|
/**
|
|
59
|
-
* 获取所有 MCP
|
|
60
|
-
* 返回扩展中定义的
|
|
69
|
+
* 获取所有 MCP 工具
|
|
70
|
+
* 返回扩展中定义的 tools 字段
|
|
61
71
|
*/
|
|
62
|
-
|
|
63
|
-
const
|
|
72
|
+
getTools(): Array<{ extensionName: string; tools: NonNullable<ExtensionDefinition["tools"]> }> {
|
|
73
|
+
const result: Array<{
|
|
64
74
|
extensionName: string;
|
|
65
|
-
|
|
75
|
+
tools: NonNullable<ExtensionDefinition["tools"]>;
|
|
66
76
|
}> = [];
|
|
67
77
|
for (const ext of this.extensions.values()) {
|
|
68
|
-
if (ext.
|
|
69
|
-
|
|
78
|
+
if (ext.tools && ext.tools.length > 0) {
|
|
79
|
+
result.push({ extensionName: ext.name, tools: ext.tools });
|
|
70
80
|
}
|
|
71
81
|
}
|
|
72
|
-
return
|
|
82
|
+
return result;
|
|
73
83
|
}
|
|
74
84
|
}
|
|
@@ -21,27 +21,24 @@ export class McpService {
|
|
|
21
21
|
return;
|
|
22
22
|
}
|
|
23
23
|
if (shouldLog(verbose, 1)) {
|
|
24
|
-
const cwd =
|
|
24
|
+
const cwd = this.extensionLoader.cwd;
|
|
25
25
|
console.error(t("mcp:cwdInfo", { cwd }));
|
|
26
|
-
if (!process.env.SPACEFLOW_CWD) {
|
|
27
|
-
console.error(t("mcp:cwdEnvHint"));
|
|
28
|
-
}
|
|
29
26
|
console.error(t("mcp:scanning"));
|
|
30
27
|
}
|
|
31
28
|
|
|
32
29
|
const extensions = this.extensionLoader.getExtensions();
|
|
33
|
-
const
|
|
30
|
+
const extensionTools = this.extensionLoader.getTools();
|
|
34
31
|
|
|
35
32
|
if (shouldLog(verbose, 2)) {
|
|
36
33
|
console.error(t("mcp:foundExtensions", { count: extensions.length }));
|
|
37
34
|
}
|
|
38
35
|
|
|
39
36
|
const allTools: Array<{ tool: McpToolMetadata; handler: any; ctx: any }> = [];
|
|
40
|
-
for (const { extensionName,
|
|
37
|
+
for (const { extensionName, tools } of extensionTools) {
|
|
41
38
|
if (shouldLog(verbose, 2)) {
|
|
42
|
-
console.error(` 扩展 ${extensionName} 提供 ${
|
|
39
|
+
console.error(` 扩展 ${extensionName} 提供 ${tools.length} 个 MCP 工具`);
|
|
43
40
|
}
|
|
44
|
-
for (const tool of
|
|
41
|
+
for (const tool of tools) {
|
|
45
42
|
if (shouldLog(verbose, 3)) {
|
|
46
43
|
console.error(` - ${tool.name}: ${tool.description}`);
|
|
47
44
|
}
|
|
@@ -55,7 +52,7 @@ export class McpService {
|
|
|
55
52
|
methodName: "handler",
|
|
56
53
|
},
|
|
57
54
|
handler: tool.handler,
|
|
58
|
-
ctx: this.extensionLoader
|
|
55
|
+
ctx: this.extensionLoader.getContext(),
|
|
59
56
|
});
|
|
60
57
|
}
|
|
61
58
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ExtensionDefinition
|
|
1
|
+
import type { ExtensionDefinition } from "./types";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* 定义扩展
|
|
@@ -6,20 +6,6 @@ import type { ExtensionDefinition, McpServerDefinition } from "./types";
|
|
|
6
6
|
* @param definition 扩展定义
|
|
7
7
|
* @returns 扩展定义(原样返回,仅用于类型推断)
|
|
8
8
|
*/
|
|
9
|
-
export function defineExtension(
|
|
10
|
-
definition: ExtensionDefinition,
|
|
11
|
-
): ExtensionDefinition {
|
|
12
|
-
return definition;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* 定义 MCP 服务器
|
|
17
|
-
* 这是一个类型安全的工厂函数,用于创建 MCP 服务器定义
|
|
18
|
-
* @param definition MCP 服务器定义
|
|
19
|
-
* @returns MCP 服务器定义(原样返回,仅用于类型推断)
|
|
20
|
-
*/
|
|
21
|
-
export function defineMcpServer(
|
|
22
|
-
definition: McpServerDefinition,
|
|
23
|
-
): McpServerDefinition {
|
|
9
|
+
export function defineExtension(definition: ExtensionDefinition): ExtensionDefinition {
|
|
24
10
|
return definition;
|
|
25
11
|
}
|
|
@@ -19,6 +19,8 @@ export interface OptionDefinition {
|
|
|
19
19
|
* 命令和 MCP 工具共用此上下文
|
|
20
20
|
*/
|
|
21
21
|
export interface SpaceflowContext {
|
|
22
|
+
/** 当前工作目录(优先 SPACEFLOW_CWD 环境变量) */
|
|
23
|
+
readonly cwd: string;
|
|
22
24
|
/** 配置读取器 */
|
|
23
25
|
readonly config: IConfigReader;
|
|
24
26
|
/** 输出服务 */
|
|
@@ -152,20 +154,6 @@ export interface McpToolDefinition {
|
|
|
152
154
|
handler: (input: unknown, ctx: SpaceflowContext) => Promise<unknown>;
|
|
153
155
|
}
|
|
154
156
|
|
|
155
|
-
/**
|
|
156
|
-
* MCP 服务器定义
|
|
157
|
-
*/
|
|
158
|
-
export interface McpServerDefinition {
|
|
159
|
-
/** 服务器名称 */
|
|
160
|
-
name: string;
|
|
161
|
-
/** 服务器版本 */
|
|
162
|
-
version?: string;
|
|
163
|
-
/** 服务器描述 */
|
|
164
|
-
description?: string;
|
|
165
|
-
/** 工具列表 */
|
|
166
|
-
tools: McpToolDefinition[];
|
|
167
|
-
}
|
|
168
|
-
|
|
169
157
|
/**
|
|
170
158
|
* 扩展定义
|
|
171
159
|
*/
|
|
@@ -184,8 +172,8 @@ export interface ExtensionDefinition {
|
|
|
184
172
|
configDependencies?: string[];
|
|
185
173
|
/** 命令列表 */
|
|
186
174
|
commands: CommandDefinition[];
|
|
187
|
-
/** MCP
|
|
188
|
-
|
|
175
|
+
/** MCP 工具列表 */
|
|
176
|
+
tools?: McpToolDefinition[];
|
|
189
177
|
/** 服务定义列表 */
|
|
190
178
|
services?: ServiceDefinition[];
|
|
191
179
|
/**
|
package/src/index.ts
CHANGED
|
@@ -48,7 +48,7 @@ export * from "./shared/spaceflow-dir";
|
|
|
48
48
|
export * from "./shared/rspack-config";
|
|
49
49
|
|
|
50
50
|
// MCP - Model Context Protocol 支持
|
|
51
|
-
// 注意:
|
|
51
|
+
// 注意:McpToolDefinition 已在 extension-system/types.ts 中定义(用于 defineExtension 的 tools 字段)
|
|
52
52
|
// 这里只导出装饰器和工具函数,避免重复导出
|
|
53
53
|
export {
|
|
54
54
|
MCP_SERVER_METADATA,
|