@spaceflow/core 0.13.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spaceflow/core",
3
- "version": "0.13.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.4.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(config: IConfigReader, output: IOutputService, storage: IStorageService): void {
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.cwd();
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
- * 返回扩展中定义的 mcp 字段
69
+ * 获取所有 MCP 工具
70
+ * 返回扩展中定义的 tools 字段
61
71
  */
62
- getMcpServers(): Array<{ extensionName: string; mcp: NonNullable<ExtensionDefinition["mcp"]> }> {
63
- const mcpServers: Array<{
72
+ getTools(): Array<{ extensionName: string; tools: NonNullable<ExtensionDefinition["tools"]> }> {
73
+ const result: Array<{
64
74
  extensionName: string;
65
- mcp: NonNullable<ExtensionDefinition["mcp"]>;
75
+ tools: NonNullable<ExtensionDefinition["tools"]>;
66
76
  }> = [];
67
77
  for (const ext of this.extensions.values()) {
68
- if (ext.mcp) {
69
- mcpServers.push({ extensionName: ext.name, mcp: ext.mcp });
78
+ if (ext.tools && ext.tools.length > 0) {
79
+ result.push({ extensionName: ext.name, tools: ext.tools });
70
80
  }
71
81
  }
72
- return mcpServers;
82
+ return result;
73
83
  }
74
84
  }
@@ -25,9 +25,9 @@ export const mcpExtension = defineExtension({
25
25
  ],
26
26
  run: async (_args, options, ctx) => {
27
27
  const verbose = (options?.verbose ? 2 : 1) as VerboseLevel;
28
- const inspector = options?.inspector as boolean | undefined;
29
28
  const extensionLoader = ctx.getService<ExtensionLoader>("extensionLoader");
30
29
  const mcpService = new McpService(extensionLoader);
30
+ const inspector = options?.inspector as boolean | undefined;
31
31
  await mcpService.startServer(verbose, inspector);
32
32
  },
33
33
  },
@@ -21,29 +21,24 @@ export class McpService {
21
21
  return;
22
22
  }
23
23
  if (shouldLog(verbose, 1)) {
24
- const cwd = process.env.SPACEFLOW_CWD || process.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
- // 获取所有扩展(已在 exec() 阶段通过 registerExtension 注册完毕)
33
29
  const extensions = this.extensionLoader.getExtensions();
34
- const mcpServers = this.extensionLoader.getMcpServers();
30
+ const extensionTools = this.extensionLoader.getTools();
35
31
 
36
32
  if (shouldLog(verbose, 2)) {
37
33
  console.error(t("mcp:foundExtensions", { count: extensions.length }));
38
34
  }
39
35
 
40
- // 收集所有扩展的 MCP 工具
41
36
  const allTools: Array<{ tool: McpToolMetadata; handler: any; ctx: any }> = [];
42
- for (const { extensionName, mcp } of mcpServers) {
37
+ for (const { extensionName, tools } of extensionTools) {
43
38
  if (shouldLog(verbose, 2)) {
44
- console.error(` 扩展 ${extensionName} 提供 ${mcp.tools.length} 个 MCP 工具`);
39
+ console.error(` 扩展 ${extensionName} 提供 ${tools.length} 个 MCP 工具`);
45
40
  }
46
- for (const tool of mcp.tools) {
41
+ for (const tool of tools) {
47
42
  if (shouldLog(verbose, 3)) {
48
43
  console.error(` - ${tool.name}: ${tool.description}`);
49
44
  }
@@ -57,7 +52,7 @@ export class McpService {
57
52
  methodName: "handler",
58
53
  },
59
54
  handler: tool.handler,
60
- ctx: this.extensionLoader["ctx"], // 获取 SpaceflowContext
55
+ ctx: this.extensionLoader.getContext(),
61
56
  });
62
57
  }
63
58
  }
@@ -1,4 +1,4 @@
1
- import type { ExtensionDefinition, McpServerDefinition } from "./types";
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
  }
@@ -1,9 +1,3 @@
1
- /** .spaceflow 目录名 */
2
- export const SPACEFLOW_DIR = ".spaceflow";
3
-
4
- /** package.json 文件名 */
5
- export const PACKAGE_JSON = "package.json";
6
-
7
1
  /**
8
2
  * Extension 元数据
9
3
  */
@@ -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
- mcp?: McpServerDefinition;
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
- // 注意:McpServerDefinition 和 McpToolDefinition 已在 extension-system/types.ts 中定义
51
+ // 注意:McpToolDefinition 已在 extension-system/types.ts 中定义(用于 defineExtension 的 tools 字段)
52
52
  // 这里只导出装饰器和工具函数,避免重复导出
53
53
  export {
54
54
  MCP_SERVER_METADATA,