@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,230 @@
1
+ import { z } from 'zod';
2
+
3
+ /**
4
+ * Metric types for different measurements
5
+ */
6
+ export enum MetricType {
7
+ /** Counter that only increases */
8
+ COUNTER = 'counter',
9
+ /** Gauge that can increase and decrease */
10
+ GAUGE = 'gauge',
11
+ /** Histogram for distribution of values */
12
+ HISTOGRAM = 'histogram',
13
+ /** Summary of observations */
14
+ SUMMARY = 'summary',
15
+ }
16
+
17
+ /**
18
+ * Metric labels schema
19
+ */
20
+ export const MetricLabelsSchema = z.record(z.string());
21
+
22
+ /**
23
+ * Metric value schema
24
+ */
25
+ export const MetricValueSchema = z.object({
26
+ /** Value of the metric */
27
+ value: z.number(),
28
+ /** Timestamp of the measurement */
29
+ timestamp: z.number().default(() => Date.now()),
30
+ /** Labels associated with the measurement */
31
+ labels: MetricLabelsSchema.default({}),
32
+ });
33
+
34
+ /**
35
+ * Metric schema
36
+ */
37
+ export const MetricSchema = z.object({
38
+ /** Metric name */
39
+ name: z.string(),
40
+ /** Metric type */
41
+ type: z.nativeEnum(MetricType),
42
+ /** Metric description */
43
+ description: z.string(),
44
+ /** Metric unit */
45
+ unit: z.string().optional(),
46
+ /** Labels that this metric can have */
47
+ labelNames: z.array(z.string()).default([]),
48
+ });
49
+
50
+ /**
51
+ * Trace span schema
52
+ */
53
+ export const TraceSpanSchema = z.object({
54
+ /** Span ID */
55
+ id: z.string(),
56
+ /** Parent span ID */
57
+ parentId: z.string().optional(),
58
+ /** Trace ID */
59
+ traceId: z.string(),
60
+ /** Operation name */
61
+ name: z.string(),
62
+ /** Start time */
63
+ startTime: z.number(),
64
+ /** End time */
65
+ endTime: z.number().optional(),
66
+ /** Span kind (client, server, producer, consumer) */
67
+ kind: z.enum(['client', 'server', 'producer', 'consumer']).default('client'),
68
+ /** Span status */
69
+ status: z.enum(['ok', 'error', 'unset']).default('unset'),
70
+ /** Error details if status is error */
71
+ error: z.any().optional(),
72
+ /** Span attributes */
73
+ attributes: z.record(z.any()).default({}),
74
+ /** Events that occurred during the span */
75
+ events: z.array(z.object({
76
+ name: z.string(),
77
+ timestamp: z.number(),
78
+ attributes: z.record(z.any()).default({}),
79
+ })).default([]),
80
+ });
81
+
82
+ /**
83
+ * Log level enum
84
+ */
85
+ export enum LogLevel {
86
+ DEBUG = 'debug',
87
+ INFO = 'info',
88
+ WARN = 'warn',
89
+ ERROR = 'error',
90
+ }
91
+
92
+ /**
93
+ * Log entry schema
94
+ */
95
+ export const LogEntrySchema = z.object({
96
+ /** Log timestamp */
97
+ timestamp: z.number().default(() => Date.now()),
98
+ /** Log level */
99
+ level: z.nativeEnum(LogLevel),
100
+ /** Log message */
101
+ message: z.string(),
102
+ /** Log context */
103
+ context: z.record(z.any()).default({}),
104
+ /** Associated trace ID */
105
+ traceId: z.string().optional(),
106
+ /** Associated span ID */
107
+ spanId: z.string().optional(),
108
+ });
109
+
110
+ /**
111
+ * Metric collector interface
112
+ */
113
+ export interface MetricCollector {
114
+ /** Create or get a counter metric */
115
+ counter(options: z.infer<typeof MetricSchema>): Counter;
116
+ /** Create or get a gauge metric */
117
+ gauge(options: z.infer<typeof MetricSchema>): Gauge;
118
+ /** Create or get a histogram metric */
119
+ histogram(options: z.infer<typeof MetricSchema>): Histogram;
120
+ /** Create or get a summary metric */
121
+ summary(options: z.infer<typeof MetricSchema>): Summary;
122
+ /** Export metrics in Prometheus format */
123
+ exportPrometheus(): string;
124
+ }
125
+
126
+ /**
127
+ * Counter metric interface
128
+ */
129
+ export interface Counter {
130
+ /** Increment counter by given value */
131
+ inc(value?: number, labels?: Record<string, string>): void;
132
+ /** Get current value */
133
+ getValue(labels?: Record<string, string>): number;
134
+ }
135
+
136
+ /**
137
+ * Gauge metric interface
138
+ */
139
+ export interface Gauge {
140
+ /** Set gauge to given value */
141
+ set(value: number, labels?: Record<string, string>): void;
142
+ /** Increment gauge by given value */
143
+ inc(value?: number, labels?: Record<string, string>): void;
144
+ /** Decrement gauge by given value */
145
+ dec(value?: number, labels?: Record<string, string>): void;
146
+ /** Get current value */
147
+ getValue(labels?: Record<string, string>): number;
148
+ }
149
+
150
+ /**
151
+ * Histogram metric interface
152
+ */
153
+ export interface Histogram {
154
+ /** Observe a value */
155
+ observe(value: number, labels?: Record<string, string>): void;
156
+ /** Get current buckets */
157
+ getBuckets(labels?: Record<string, string>): Record<number, number>;
158
+ }
159
+
160
+ /**
161
+ * Summary metric interface
162
+ */
163
+ export interface Summary {
164
+ /** Observe a value */
165
+ observe(value: number, labels?: Record<string, string>): void;
166
+ /** Get current quantiles */
167
+ getQuantiles(labels?: Record<string, string>): Record<number, number>;
168
+ }
169
+
170
+ /**
171
+ * Tracer interface
172
+ */
173
+ export interface Tracer {
174
+ /** Start a new span */
175
+ startSpan(name: string, options?: {
176
+ parent?: z.infer<typeof TraceSpanSchema>;
177
+ kind?: z.infer<typeof TraceSpanSchema>['kind'];
178
+ attributes?: Record<string, any>;
179
+ }): z.infer<typeof TraceSpanSchema>;
180
+ /** End a span */
181
+ endSpan(span: z.infer<typeof TraceSpanSchema>): void;
182
+ /** Add an event to a span */
183
+ addEvent(span: z.infer<typeof TraceSpanSchema>, name: string, attributes?: Record<string, any>): void;
184
+ /** Set span status */
185
+ setStatus(span: z.infer<typeof TraceSpanSchema>, status: z.infer<typeof TraceSpanSchema>['status'], error?: any): void;
186
+ /** Get active span for the current context */
187
+ getActiveSpan(): z.infer<typeof TraceSpanSchema> | undefined;
188
+ /** Set active span for the current context */
189
+ setActiveSpan(span: z.infer<typeof TraceSpanSchema>): void;
190
+ /** Clear active span for the current context */
191
+ clearActiveSpan(span: z.infer<typeof TraceSpanSchema>): void;
192
+ }
193
+
194
+ /**
195
+ * Logger interface
196
+ */
197
+ export interface Logger {
198
+ /** Log a debug message */
199
+ debug(message: string, context?: Record<string, any>): void;
200
+ /** Log an info message */
201
+ info(message: string, context?: Record<string, any>): void;
202
+ /** Log a warning message */
203
+ warn(message: string, context?: Record<string, any>): void;
204
+ /** Log an error message */
205
+ error(message: string, error?: Error, context?: Record<string, any>): void;
206
+ /** Get logs with optional filtering */
207
+ getLogs(filter?: {
208
+ level?: LogLevel;
209
+ traceId?: string;
210
+ spanId?: string;
211
+ startTime?: number;
212
+ endTime?: number;
213
+ }): any[];
214
+ }
215
+
216
+ /**
217
+ * Monitoring system interface
218
+ */
219
+ export interface MonitoringSystem {
220
+ /** Get metric collector */
221
+ metrics: MetricCollector;
222
+ /** Get tracer */
223
+ tracer: Tracer;
224
+ /** Get logger */
225
+ logger: Logger;
226
+ /** Initialize monitoring system */
227
+ init(): Promise<void>;
228
+ /** Shutdown monitoring system */
229
+ shutdown(): Promise<void>;
230
+ }
@@ -0,0 +1,79 @@
1
+ import type { Plugin } from './types';
2
+
3
+ /**
4
+ * Plugin manager implementation for loading and managing plugins
5
+ */
6
+ export class PluginManagerImpl {
7
+ private plugins: Map<string, Plugin> = new Map();
8
+
9
+ /**
10
+ * Register a plugin
11
+ */
12
+ public async register(plugin: Plugin): Promise<void> {
13
+ if (!plugin.name) {
14
+ throw new Error('Plugin name is required');
15
+ }
16
+ if (this.plugins.has(plugin.name)) {
17
+ throw new Error(`Plugin ${plugin.name} is already registered`);
18
+ }
19
+ this.plugins.set(plugin.name, plugin);
20
+ }
21
+
22
+ /**
23
+ * Unregister a plugin
24
+ */
25
+ public async unregister(name: string): Promise<void> {
26
+ if (!this.plugins.has(name)) {
27
+ throw new Error(`Plugin ${name} is not registered`);
28
+ }
29
+ this.plugins.delete(name);
30
+ }
31
+
32
+ /**
33
+ * Get a plugin by name
34
+ */
35
+ public getPlugin(name: string): Plugin | undefined {
36
+ return this.plugins.get(name);
37
+ }
38
+
39
+ /**
40
+ * Get all registered plugins
41
+ */
42
+ public getPlugins(): Plugin[] {
43
+ return Array.from(this.plugins.values());
44
+ }
45
+
46
+ /**
47
+ * Initialize all plugins
48
+ */
49
+ public async init(): Promise<void> {
50
+ for (const plugin of this.plugins.values()) {
51
+ if (plugin.init) {
52
+ await plugin.init();
53
+ }
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Cleanup all plugins
59
+ */
60
+ public async destroy(): Promise<void> {
61
+ for (const plugin of this.plugins.values()) {
62
+ if (plugin.cleanup) {
63
+ await plugin.cleanup();
64
+ }
65
+ }
66
+ this.plugins.clear();
67
+ }
68
+
69
+ /**
70
+ * Execute a hook across all plugins
71
+ */
72
+ public async executeHook(hook: string, ...args: any[]): Promise<void> {
73
+ for (const plugin of this.plugins.values()) {
74
+ if (plugin.hooks?.[hook]) {
75
+ await plugin.hooks[hook](...args);
76
+ }
77
+ }
78
+ }
79
+ }
@@ -0,0 +1,114 @@
1
+ import { z } from 'zod';
2
+
3
+ /**
4
+ * Plugin lifecycle hooks
5
+ */
6
+ export enum PluginHook {
7
+ BEFORE_INIT = 'beforeInit',
8
+ AFTER_INIT = 'afterInit',
9
+ BEFORE_ACTION = 'beforeAction',
10
+ AFTER_ACTION = 'afterAction',
11
+ BEFORE_MESSAGE = 'beforeMessage',
12
+ AFTER_MESSAGE = 'afterMessage',
13
+ BEFORE_THINK = 'beforeThink',
14
+ AFTER_THINK = 'afterThink',
15
+ ON_ERROR = 'onError',
16
+ }
17
+
18
+ /**
19
+ * Plugin metadata schema
20
+ */
21
+ export const PluginMetadataSchema = z.object({
22
+ /** Unique plugin identifier */
23
+ id: z.string(),
24
+ /** Plugin name */
25
+ name: z.string(),
26
+ /** Plugin version */
27
+ version: z.string(),
28
+ /** Plugin description */
29
+ description: z.string().optional(),
30
+ /** Plugin author */
31
+ author: z.string().optional(),
32
+ /** Plugin homepage */
33
+ homepage: z.string().optional(),
34
+ /** Plugin repository */
35
+ repository: z.string().optional(),
36
+ /** Plugin license */
37
+ license: z.string().optional(),
38
+ /** Plugin dependencies */
39
+ dependencies: z.record(z.string()).optional(),
40
+ /** Plugin configuration schema */
41
+ configSchema: z.any().optional(),
42
+ });
43
+
44
+ /**
45
+ * Plugin configuration schema
46
+ */
47
+ export const PluginConfigSchema = z.object({
48
+ /** Whether the plugin is enabled */
49
+ enabled: z.boolean().default(true),
50
+ /** Plugin-specific configuration */
51
+ options: z.record(z.any()).default({}),
52
+ });
53
+
54
+ /**
55
+ * Plugin context passed to hooks
56
+ */
57
+ export interface PluginContext {
58
+ /** Plugin metadata */
59
+ metadata: z.infer<typeof PluginMetadataSchema>;
60
+ /** Plugin configuration */
61
+ config: z.infer<typeof PluginConfigSchema>;
62
+ /** Plugin storage */
63
+ storage: Map<string, any>;
64
+ /** Plugin logger */
65
+ logger: {
66
+ debug: (message: string, ...args: any[]) => void;
67
+ info: (message: string, ...args: any[]) => void;
68
+ warn: (message: string, ...args: any[]) => void;
69
+ error: (message: string, ...args: any[]) => void;
70
+ };
71
+ }
72
+
73
+ /**
74
+ * Plugin hook handler type
75
+ */
76
+ export type PluginHookHandler = (...args: any[]) => Promise<void>;
77
+
78
+ /**
79
+ * Plugin interface for extending functionality
80
+ */
81
+ export interface Plugin {
82
+ /** Plugin name */
83
+ name: string;
84
+ /** Plugin version */
85
+ version: string;
86
+ /** Plugin description */
87
+ description?: string;
88
+ /** Plugin hooks */
89
+ hooks?: Record<string, PluginHookHandler>;
90
+ /** Plugin initialization */
91
+ init?(): Promise<void>;
92
+ /** Plugin cleanup */
93
+ cleanup?(): Promise<void>;
94
+ }
95
+
96
+ /**
97
+ * Plugin manager interface
98
+ */
99
+ export interface PluginManager {
100
+ /** Register a plugin */
101
+ register(plugin: Plugin): Promise<void>;
102
+ /** Unregister a plugin */
103
+ unregister(pluginId: string): Promise<void>;
104
+ /** Get a plugin by ID */
105
+ getPlugin(pluginId: string): Plugin | undefined;
106
+ /** Get all registered plugins */
107
+ getPlugins(): Plugin[];
108
+ /** Execute a hook */
109
+ executeHook(hook: PluginHook, ...args: any[]): Promise<void>;
110
+ /** Initialize all plugins */
111
+ init(): Promise<void>;
112
+ /** Destroy all plugins */
113
+ destroy(): Promise<void>;
114
+ }