@louloulinx/metagpt 0.1.3

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 (113) hide show
  1. package/.eslintrc.json +23 -0
  2. package/.prettierrc +7 -0
  3. package/LICENSE +21 -0
  4. package/README-CN.md +754 -0
  5. package/README.md +238 -0
  6. package/bun.lock +1023 -0
  7. package/doc/TutorialAssistant.md +114 -0
  8. package/doc/VercelLLMProvider.md +164 -0
  9. package/eslint.config.js +55 -0
  10. package/examples/data-interpreter-example.ts +173 -0
  11. package/examples/qwen-direct-example.ts +60 -0
  12. package/examples/qwen-example.ts +62 -0
  13. package/examples/tutorial-assistant-example.ts +97 -0
  14. package/jest.config.ts +22 -0
  15. package/output/tutorials/Go/350/257/255/350/250/200/347/274/226/347/250/213/346/225/231/347/250/213_2025-02-25T09-35-15-436Z.md +2208 -0
  16. package/output/tutorials/Rust/346/225/231/347/250/213_2025-02-25T08-27-27-632Z.md +1967 -0
  17. package/output/tutorials//345/246/202/344/275/225/344/275/277/347/224/250TypeScript/345/274/200/345/217/221Node.js/345/272/224/347/224/250_2025-02-25T08-14-39-605Z.md +1721 -0
  18. package/output/tutorials//346/225/260/345/255/227/347/273/217/346/265/216/345/255/246/346/225/231/347/250/213_2025-02-25T10-45-03-605Z.md +902 -0
  19. package/output/tutorials//346/232/250/345/215/227/345/244/247/345/255/246/346/225/260/345/255/227/347/273/217/346/265/216/345/255/246/345/244/215/350/257/225/350/265/204/346/226/231_2025-02-25T11-16-59-133Z.md +719 -0
  20. package/package.json +58 -0
  21. package/plan-cn.md +321 -0
  22. package/plan.md +154 -0
  23. package/src/actions/analyze-task.ts +65 -0
  24. package/src/actions/base-action.ts +103 -0
  25. package/src/actions/di/execute-nb-code.ts +247 -0
  26. package/src/actions/di/write-analysis-code.ts +234 -0
  27. package/src/actions/write-tutorial.ts +232 -0
  28. package/src/config/browser.ts +33 -0
  29. package/src/config/config.ts +345 -0
  30. package/src/config/embedding.ts +26 -0
  31. package/src/config/llm.ts +36 -0
  32. package/src/config/mermaid.ts +37 -0
  33. package/src/config/omniparse.ts +25 -0
  34. package/src/config/redis.ts +34 -0
  35. package/src/config/s3.ts +33 -0
  36. package/src/config/search.ts +30 -0
  37. package/src/config/workspace.ts +20 -0
  38. package/src/index.ts +40 -0
  39. package/src/management/team.ts +168 -0
  40. package/src/memory/longterm.ts +218 -0
  41. package/src/memory/manager.ts +160 -0
  42. package/src/memory/types.ts +100 -0
  43. package/src/memory/working.ts +154 -0
  44. package/src/monitoring/system.ts +413 -0
  45. package/src/monitoring/types.ts +230 -0
  46. package/src/plugin/manager.ts +79 -0
  47. package/src/plugin/types.ts +114 -0
  48. package/src/provider/vercel-llm.ts +314 -0
  49. package/src/rag/base-rag.ts +194 -0
  50. package/src/rag/document-qa.ts +102 -0
  51. package/src/roles/base-role.ts +155 -0
  52. package/src/roles/data-interpreter.ts +360 -0
  53. package/src/roles/engineer.ts +1 -0
  54. package/src/roles/tutorial-assistant.ts +217 -0
  55. package/src/skills/base-skill.ts +144 -0
  56. package/src/skills/code-review.ts +120 -0
  57. package/src/tools/base-tool.ts +155 -0
  58. package/src/tools/file-system.ts +204 -0
  59. package/src/tools/tool-recommend.d.ts +14 -0
  60. package/src/tools/tool-recommend.ts +31 -0
  61. package/src/types/action.ts +38 -0
  62. package/src/types/config.ts +129 -0
  63. package/src/types/document.ts +354 -0
  64. package/src/types/llm.ts +64 -0
  65. package/src/types/memory.ts +36 -0
  66. package/src/types/message.ts +193 -0
  67. package/src/types/rag.ts +86 -0
  68. package/src/types/role.ts +67 -0
  69. package/src/types/skill.ts +71 -0
  70. package/src/types/task.ts +32 -0
  71. package/src/types/team.ts +55 -0
  72. package/src/types/tool.ts +77 -0
  73. package/src/types/workflow.ts +133 -0
  74. package/src/utils/common.ts +73 -0
  75. package/src/utils/yaml.ts +67 -0
  76. package/src/websocket/browser-client.ts +187 -0
  77. package/src/websocket/client.ts +186 -0
  78. package/src/websocket/server.ts +169 -0
  79. package/src/websocket/types.ts +125 -0
  80. package/src/workflow/executor.ts +193 -0
  81. package/src/workflow/executors/action-executor.ts +72 -0
  82. package/src/workflow/executors/condition-executor.ts +118 -0
  83. package/src/workflow/executors/parallel-executor.ts +201 -0
  84. package/src/workflow/executors/role-executor.ts +76 -0
  85. package/src/workflow/executors/sequence-executor.ts +196 -0
  86. package/tests/actions.test.ts +105 -0
  87. package/tests/benchmark/performance.test.ts +147 -0
  88. package/tests/config/config.test.ts +115 -0
  89. package/tests/config.test.ts +106 -0
  90. package/tests/e2e/setup.ts +74 -0
  91. package/tests/e2e/workflow.test.ts +88 -0
  92. package/tests/llm.test.ts +84 -0
  93. package/tests/memory/memory.test.ts +164 -0
  94. package/tests/memory.test.ts +63 -0
  95. package/tests/monitoring/monitoring.test.ts +225 -0
  96. package/tests/plugin/plugin.test.ts +183 -0
  97. package/tests/provider/bailian-llm.test.ts +98 -0
  98. package/tests/rag.test.ts +162 -0
  99. package/tests/roles.test.ts +88 -0
  100. package/tests/skills.test.ts +166 -0
  101. package/tests/team.test.ts +143 -0
  102. package/tests/tools.test.ts +170 -0
  103. package/tests/types/document.test.ts +181 -0
  104. package/tests/types/message.test.ts +122 -0
  105. package/tests/utils/yaml.test.ts +110 -0
  106. package/tests/utils.test.ts +74 -0
  107. package/tests/websocket/browser-client.test.ts +1 -0
  108. package/tests/websocket/websocket.test.ts +42 -0
  109. package/tests/workflow/parallel-executor.test.ts +224 -0
  110. package/tests/workflow/sequence-executor.test.ts +207 -0
  111. package/tests/workflow.test.ts +290 -0
  112. package/tsconfig.json +27 -0
  113. package/typedoc.json +25 -0
@@ -0,0 +1,204 @@
1
+ import { BaseTool } from './base-tool';
2
+ import type { ToolConfig, ToolResult } from '../types/tool';
3
+ import { promises as fs } from 'fs';
4
+ import { join, dirname } from 'path';
5
+
6
+ /**
7
+ * 文件系统工具
8
+ * 提供基本的文件操作功能
9
+ */
10
+ export class FileSystemTool extends BaseTool {
11
+ constructor(config?: Partial<ToolConfig>) {
12
+ super({
13
+ name: 'file_system',
14
+ description: 'Basic file system operations',
15
+ version: '1.0.0',
16
+ category: 'system',
17
+ ...config,
18
+ });
19
+ }
20
+
21
+ /**
22
+ * 执行文件系统操作
23
+ * @param args 执行参数
24
+ * @returns 执行结果
25
+ */
26
+ async execute(args?: Record<string, any>): Promise<ToolResult> {
27
+ try {
28
+ // 验证操作类型
29
+ const operation = args?.operation;
30
+ if (!operation) {
31
+ return this.createResult(false, 'No operation specified');
32
+ }
33
+
34
+ // 执行相应的操作
35
+ switch (operation) {
36
+ case 'read':
37
+ return await this.readFile(args);
38
+ case 'write':
39
+ return await this.writeFile(args);
40
+ case 'delete':
41
+ return await this.deleteFile(args);
42
+ case 'list':
43
+ return await this.listDirectory(args);
44
+ default:
45
+ return this.createResult(false, `Unknown operation: ${operation}`);
46
+ }
47
+ } catch (error) {
48
+ await this.handleError(error as Error);
49
+ return this.createResult(
50
+ false,
51
+ `File system operation failed: ${(error as Error).message}`,
52
+ undefined,
53
+ error
54
+ );
55
+ }
56
+ }
57
+
58
+ /**
59
+ * 读取文件
60
+ * @param args 参数
61
+ * @returns 执行结果
62
+ */
63
+ private async readFile(args: Record<string, any>): Promise<ToolResult> {
64
+ const path = args.path;
65
+ if (!path) {
66
+ return this.createResult(false, 'No file path provided');
67
+ }
68
+
69
+ try {
70
+ const content = await fs.readFile(path, 'utf-8');
71
+ return this.createResult(true, 'File read successfully', { content });
72
+ } catch (error) {
73
+ return this.createResult(
74
+ false,
75
+ `Failed to read file: ${(error as Error).message}`,
76
+ undefined,
77
+ error
78
+ );
79
+ }
80
+ }
81
+
82
+ /**
83
+ * 写入文件
84
+ * @param args 参数
85
+ * @returns 执行结果
86
+ */
87
+ private async writeFile(args: Record<string, any>): Promise<ToolResult> {
88
+ const { path, content } = args;
89
+ if (!path || content === undefined) {
90
+ return this.createResult(false, 'Path and content are required');
91
+ }
92
+
93
+ try {
94
+ // 确保目录存在
95
+ await fs.mkdir(dirname(path), { recursive: true });
96
+ await fs.writeFile(path, content);
97
+ return this.createResult(true, 'File written successfully');
98
+ } catch (error) {
99
+ return this.createResult(
100
+ false,
101
+ `Failed to write file: ${(error as Error).message}`,
102
+ undefined,
103
+ error
104
+ );
105
+ }
106
+ }
107
+
108
+ /**
109
+ * 删除文件
110
+ * @param args 参数
111
+ * @returns 执行结果
112
+ */
113
+ private async deleteFile(args: Record<string, any>): Promise<ToolResult> {
114
+ const path = args.path;
115
+ if (!path) {
116
+ return this.createResult(false, 'No file path provided');
117
+ }
118
+
119
+ try {
120
+ await fs.unlink(path);
121
+ return this.createResult(true, 'File deleted successfully');
122
+ } catch (error) {
123
+ return this.createResult(
124
+ false,
125
+ `Failed to delete file: ${(error as Error).message}`,
126
+ undefined,
127
+ error
128
+ );
129
+ }
130
+ }
131
+
132
+ /**
133
+ * 列出目录内容
134
+ * @param args 参数
135
+ * @returns 执行结果
136
+ */
137
+ private async listDirectory(args: Record<string, any>): Promise<ToolResult> {
138
+ const path = args.path || '.';
139
+
140
+ try {
141
+ const entries = await fs.readdir(path, { withFileTypes: true });
142
+ const files = entries.map(entry => ({
143
+ name: entry.name,
144
+ isDirectory: entry.isDirectory(),
145
+ path: join(path, entry.name),
146
+ }));
147
+
148
+ return this.createResult(true, 'Directory listed successfully', { files });
149
+ } catch (error) {
150
+ return this.createResult(
151
+ false,
152
+ `Failed to list directory: ${(error as Error).message}`,
153
+ undefined,
154
+ error
155
+ );
156
+ }
157
+ }
158
+
159
+ /**
160
+ * 自定义错误处理
161
+ */
162
+ async handleError(error: Error): Promise<void> {
163
+ await super.handleError(error);
164
+ this.setState('lastError', error.message);
165
+ this.setState('errorTimestamp', new Date().toISOString());
166
+ }
167
+
168
+ /**
169
+ * 获取帮助信息
170
+ */
171
+ getHelp(): string {
172
+ return `
173
+ Tool: ${this.name} (v${this.version})
174
+ Category: ${this.category}
175
+ Description: ${this.description}
176
+
177
+ Operations:
178
+ - read: Read file content
179
+ Args: path (string)
180
+
181
+ - write: Write content to file
182
+ Args: path (string), content (string)
183
+
184
+ - delete: Delete a file
185
+ Args: path (string)
186
+
187
+ - list: List directory contents
188
+ Args: path (string, optional)
189
+
190
+ Examples:
191
+ 1. Read file:
192
+ { operation: 'read', path: '/path/to/file.txt' }
193
+
194
+ 2. Write file:
195
+ { operation: 'write', path: '/path/to/file.txt', content: 'Hello World' }
196
+
197
+ 3. Delete file:
198
+ { operation: 'delete', path: '/path/to/file.txt' }
199
+
200
+ 4. List directory:
201
+ { operation: 'list', path: '/path/to/dir' }
202
+ `.trim();
203
+ }
204
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * 工具推荐器接口
3
+ */
4
+ export interface ToolRecommender {
5
+ getRecommendedToolInfo(context: string, plan?: any): Promise<string>;
6
+ }
7
+
8
+ /**
9
+ * BM25 工具推荐器
10
+ */
11
+ export declare class BM25ToolRecommender implements ToolRecommender {
12
+ constructor(tools: string[]);
13
+ getRecommendedToolInfo(context: string, plan?: any): Promise<string>;
14
+ }
@@ -0,0 +1,31 @@
1
+ import type { LLMProvider } from '../types/llm';
2
+
3
+ /**
4
+ * 工具推荐器接口
5
+ */
6
+ export interface ToolRecommender {
7
+ getRecommendedToolInfo(context: string, plan?: any): Promise<string>;
8
+ }
9
+
10
+ /**
11
+ * BM25 工具推荐器
12
+ */
13
+ export class BM25ToolRecommender implements ToolRecommender {
14
+ private tools: string[];
15
+
16
+ constructor(tools: string[]) {
17
+ this.tools = tools;
18
+ }
19
+
20
+ /**
21
+ * 基于上下文和计划获取推荐工具信息
22
+ */
23
+ async getRecommendedToolInfo(context: string, plan?: any): Promise<string> {
24
+ if (!context || this.tools.length === 0) {
25
+ return '';
26
+ }
27
+
28
+ // Simple implementation returning all tools
29
+ return `Available tools: ${this.tools.join(', ')}`;
30
+ }
31
+ }
@@ -0,0 +1,38 @@
1
+ import { z } from 'zod';
2
+ import type { Message } from './message';
3
+
4
+ export const ActionStatusSchema = z.enum([
5
+ 'created',
6
+ 'running',
7
+ 'completed',
8
+ 'failed',
9
+ 'blocked'
10
+ ]);
11
+
12
+ export type ActionStatus = z.infer<typeof ActionStatusSchema>;
13
+
14
+ export const ActionOutputSchema = z.object({
15
+ content: z.string(),
16
+ status: ActionStatusSchema,
17
+ instructContent: z.any().optional(),
18
+ });
19
+
20
+ export type ActionOutput = z.infer<typeof ActionOutputSchema>;
21
+
22
+ export const ActionContextSchema = z.object({
23
+ // TODO: Define action context properties
24
+ memory: z.any(),
25
+ workingMemory: z.any(),
26
+ llm: z.any(),
27
+ });
28
+
29
+ export type ActionContext = z.infer<typeof ActionContextSchema>;
30
+
31
+ export interface Action {
32
+ name: string;
33
+ context: ActionContext;
34
+ llm: any; // TODO: Define LLMProvider type
35
+
36
+ run(): Promise<ActionOutput>;
37
+ handleException(error: Error): Promise<void>;
38
+ }
@@ -0,0 +1,129 @@
1
+ /**
2
+ * @module Config
3
+ * @category Core
4
+ */
5
+
6
+ import { z } from 'zod';
7
+ import type { LLMConfig } from './llm';
8
+
9
+ /**
10
+ * CLI parameters configuration schema
11
+ * Defines command line interface parameters for the application
12
+ */
13
+ export const CLIParamsSchema = z.object({
14
+ /** Project directory path */
15
+ projectPath: z.string().default(''),
16
+ /** Project name */
17
+ projectName: z.string().default(''),
18
+ /** Whether to use incremental mode */
19
+ inc: z.boolean().default(false),
20
+ /** Path to requirements file */
21
+ reqaFile: z.string().default(''),
22
+ /** Maximum number of auto code summarizations */
23
+ maxAutoSummarizeCode: z.number().default(0),
24
+ /** Whether to reinitialize Git */
25
+ gitReinit: z.boolean().default(false),
26
+ });
27
+
28
+ export type CLIParams = z.infer<typeof CLIParamsSchema>;
29
+
30
+ /**
31
+ * Embedding configuration schema
32
+ * Defines settings for text embedding operations
33
+ */
34
+ export const EmbeddingConfigSchema = z.object({
35
+ /** API type (e.g., 'openai') */
36
+ apiType: z.string().default('openai'),
37
+ /** API key for authentication */
38
+ apiKey: z.string().optional(),
39
+ /** Model name for embeddings */
40
+ model: z.string().default('text-embedding-ada-002'),
41
+ /** Proxy URL if needed */
42
+ proxy: z.string().optional(),
43
+ /** Batch size for embedding operations */
44
+ batchSize: z.number().default(512),
45
+ });
46
+
47
+ export type EmbeddingConfig = z.infer<typeof EmbeddingConfigSchema>;
48
+
49
+ /**
50
+ * Workspace configuration schema
51
+ * Defines settings for the application workspace
52
+ */
53
+ export const WorkspaceConfigSchema = z.object({
54
+ /** Root directory for workspace */
55
+ root: z.string().default('./workspace'),
56
+ /** Whether to automatically clean workspace */
57
+ autoClean: z.boolean().default(false),
58
+ /** Path for storage */
59
+ storagePath: z.string().default('./storage'),
60
+ });
61
+
62
+ export type WorkspaceConfig = z.infer<typeof WorkspaceConfigSchema>;
63
+
64
+ /**
65
+ * Search configuration schema
66
+ * Defines settings for search operations
67
+ */
68
+ export const SearchConfigSchema = z.object({
69
+ /** Search engine to use */
70
+ engine: z.string().default('google'),
71
+ /** API key for search engine */
72
+ apiKey: z.string().optional(),
73
+ /** Proxy URL if needed */
74
+ proxy: z.string().optional(),
75
+ /** Maximum results per search */
76
+ maxResults: z.number().default(10),
77
+ });
78
+
79
+ export type SearchConfig = z.infer<typeof SearchConfigSchema>;
80
+
81
+ /**
82
+ * Main configuration schema
83
+ * Combines all configuration schemas into one comprehensive configuration
84
+ */
85
+ export const ConfigSchema = CLIParamsSchema.extend({
86
+ /** LLM provider configuration */
87
+ llm: z.any(), // LLMConfig type
88
+ /** Embedding configuration */
89
+ embedding: EmbeddingConfigSchema.default({}),
90
+ /** Global proxy URL */
91
+ proxy: z.string().default(''),
92
+ /** Search configuration */
93
+ search: SearchConfigSchema.default({}),
94
+ /** Workspace configuration */
95
+ workspace: WorkspaceConfigSchema.default({}),
96
+ /** Whether to enable long-term memory */
97
+ enableLongtermMemory: z.boolean().default(false),
98
+ /** Number of code review iterations */
99
+ codeReviewKTimes: z.number().default(2),
100
+ /** Interface language */
101
+ language: z.string().default('English'),
102
+ /** Additional configuration options */
103
+ extra: z.record(z.any()).default({}),
104
+ });
105
+
106
+ export type Config = z.infer<typeof ConfigSchema>;
107
+
108
+ /**
109
+ * Configuration source type
110
+ * Defines possible sources for configuration values
111
+ */
112
+ export type ConfigSource = 'env' | 'file' | 'default' | 'cli';
113
+
114
+ /**
115
+ * Configuration loading options schema
116
+ * Defines options for loading configuration
117
+ */
118
+ export const ConfigOptionsSchema = z.object({
119
+ /** Path to configuration file */
120
+ configPath: z.string().optional(),
121
+ /** Whether to load from environment variables */
122
+ loadEnv: z.boolean().default(true),
123
+ /** Whether to use default values */
124
+ useDefaults: z.boolean().default(true),
125
+ /** Priority order for configuration sources */
126
+ sourcePriority: z.array(z.enum(['env', 'file', 'default', 'cli'])).default(['cli', 'env', 'file', 'default']),
127
+ });
128
+
129
+ export type ConfigOptions = z.infer<typeof ConfigOptionsSchema>;