pulse-coder-engine 0.0.1-alpha.1 → 0.0.1-alpha.2

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.
@@ -0,0 +1,598 @@
1
+ import { FlexibleSchema, ModelMessage, StepResult, ToolSet, Tool as Tool$1, streamText } from 'ai';
2
+ import { EventEmitter } from 'events';
3
+ import { z } from 'zod';
4
+
5
+ interface ClarificationRequest {
6
+ id: string;
7
+ question: string;
8
+ context?: string;
9
+ defaultAnswer?: string;
10
+ timeout: number;
11
+ }
12
+ interface ToolExecutionContext {
13
+ onClarificationRequest?: (request: ClarificationRequest) => Promise<string>;
14
+ abortSignal?: AbortSignal;
15
+ }
16
+ interface Tool<Input = any, Output = any> {
17
+ name: string;
18
+ description: string;
19
+ inputSchema: FlexibleSchema<Input>;
20
+ execute: (input: Input, context?: ToolExecutionContext) => Promise<Output>;
21
+ }
22
+ interface Context {
23
+ messages: ModelMessage[];
24
+ }
25
+ interface IPlugin {
26
+ name: string;
27
+ version: string;
28
+ extensions: Extension[];
29
+ activate(context: IExtensionContext): Promise<void>;
30
+ deactivate?(): Promise<void>;
31
+ }
32
+ interface Extension {
33
+ type: 'skill' | 'mcp' | 'tool' | 'context';
34
+ provider: any;
35
+ }
36
+ interface IExtensionContext {
37
+ registerTools(toolName: string, tool: Tool<any, any>): void;
38
+ logger: ILogger;
39
+ }
40
+ interface ILogger {
41
+ debug(message: string, meta?: Record<string, unknown>): void;
42
+ info(message: string, meta?: Record<string, unknown>): void;
43
+ warn(message: string, meta?: Record<string, unknown>): void;
44
+ error(message: string, error?: Error, meta?: Record<string, unknown>): void;
45
+ }
46
+
47
+ interface LoopOptions {
48
+ onText?: (delta: string) => void;
49
+ onToolCall?: (toolCall: any) => void;
50
+ onToolResult?: (toolResult: any) => void;
51
+ onStepFinish?: (step: StepResult<any>) => void;
52
+ onClarificationRequest?: (request: ClarificationRequest) => Promise<string>;
53
+ onCompacted?: (newMessages: ModelMessage[]) => void;
54
+ onResponse?: (messages: StepResult<ToolSet>['response']['messages']) => void;
55
+ abortSignal?: AbortSignal;
56
+ tools?: Record<string, Tool>;
57
+ }
58
+ declare function loop(context: Context, options?: LoopOptions): Promise<string>;
59
+
60
+ interface EnginePlugin {
61
+ name: string;
62
+ version: string;
63
+ dependencies?: string[];
64
+ beforeInitialize?(context: EnginePluginContext): Promise<void>;
65
+ initialize(context: EnginePluginContext): Promise<void>;
66
+ afterInitialize?(context: EnginePluginContext): Promise<void>;
67
+ destroy?(context: EnginePluginContext): Promise<void>;
68
+ }
69
+ interface EnginePluginContext {
70
+ registerTool(name: string, tool: Tool$1): void;
71
+ registerTools(tools: Record<string, Tool$1>): void;
72
+ getTool(name: string): Tool$1 | undefined;
73
+ registerProtocol(name: string, handler: ProtocolHandler): void;
74
+ getProtocol(name: string): ProtocolHandler | undefined;
75
+ registerService<T>(name: string, service: T): void;
76
+ getService<T>(name: string): T | undefined;
77
+ getConfig<T>(key: string): T | undefined;
78
+ setConfig<T>(key: string, value: T): void;
79
+ events: EventEmitter;
80
+ logger: {
81
+ debug(message: string, meta?: any): void;
82
+ info(message: string, meta?: any): void;
83
+ warn(message: string, meta?: any): void;
84
+ error(message: string, error?: Error, meta?: any): void;
85
+ };
86
+ }
87
+ interface ProtocolHandler {
88
+ name: string;
89
+ handle(message: any): Promise<any>;
90
+ }
91
+ interface EnginePluginLoadOptions {
92
+ plugins?: EnginePlugin[];
93
+ dirs?: string[];
94
+ scan?: boolean;
95
+ }
96
+
97
+ /**
98
+ * 用户配置插件接口 - 面向终端用户
99
+ * 使用声明式配置扩展引擎能力
100
+ */
101
+ interface UserConfigPlugin {
102
+ version: string;
103
+ name?: string;
104
+ description?: string;
105
+ tools?: Record<string, ToolConfig>;
106
+ mcp?: {
107
+ servers: MCPServerConfig[];
108
+ };
109
+ prompts?: {
110
+ system?: string;
111
+ user?: string;
112
+ assistant?: string;
113
+ };
114
+ subAgents?: SubAgentConfig[];
115
+ skills?: {
116
+ directories?: string[];
117
+ autoScan?: boolean;
118
+ cache?: boolean;
119
+ };
120
+ env?: Record<string, string>;
121
+ conditions?: {
122
+ environment?: string;
123
+ features?: string[];
124
+ };
125
+ }
126
+ /**
127
+ * 工具配置接口
128
+ */
129
+ declare const ToolConfigSchema: z.ZodObject<{
130
+ type: z.ZodEnum<{
131
+ skill: "skill";
132
+ bash: "bash";
133
+ http: "http";
134
+ javascript: "javascript";
135
+ custom: "custom";
136
+ }>;
137
+ command: z.ZodOptional<z.ZodString>;
138
+ args: z.ZodOptional<z.ZodArray<z.ZodString>>;
139
+ cwd: z.ZodOptional<z.ZodString>;
140
+ env: z.ZodOptional<z.ZodAny>;
141
+ timeout: z.ZodOptional<z.ZodNumber>;
142
+ method: z.ZodOptional<z.ZodEnum<{
143
+ GET: "GET";
144
+ POST: "POST";
145
+ PUT: "PUT";
146
+ DELETE: "DELETE";
147
+ PATCH: "PATCH";
148
+ }>>;
149
+ url: z.ZodOptional<z.ZodString>;
150
+ baseUrl: z.ZodOptional<z.ZodString>;
151
+ headers: z.ZodOptional<z.ZodAny>;
152
+ params: z.ZodOptional<z.ZodAny>;
153
+ data: z.ZodOptional<z.ZodAny>;
154
+ code: z.ZodOptional<z.ZodString>;
155
+ description: z.ZodOptional<z.ZodString>;
156
+ enabled: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
157
+ conditions: z.ZodOptional<z.ZodObject<{
158
+ environment: z.ZodOptional<z.ZodString>;
159
+ platform: z.ZodOptional<z.ZodString>;
160
+ }, z.core.$strip>>;
161
+ }, z.core.$strip>;
162
+ type ToolConfig = z.infer<typeof ToolConfigSchema>;
163
+ /**
164
+ * MCP 服务器配置接口
165
+ */
166
+ declare const MCPServerConfigSchema: z.ZodObject<{
167
+ name: z.ZodString;
168
+ command: z.ZodString;
169
+ args: z.ZodOptional<z.ZodArray<z.ZodString>>;
170
+ env: z.ZodOptional<z.ZodAny>;
171
+ cwd: z.ZodOptional<z.ZodString>;
172
+ timeout: z.ZodOptional<z.ZodNumber>;
173
+ description: z.ZodOptional<z.ZodString>;
174
+ enabled: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
175
+ }, z.core.$strip>;
176
+ type MCPServerConfig = z.infer<typeof MCPServerConfigSchema>;
177
+ /**
178
+ * 子代理配置接口
179
+ */
180
+ declare const SubAgentConfigSchema: z.ZodObject<{
181
+ name: z.ZodString;
182
+ trigger: z.ZodArray<z.ZodString>;
183
+ prompt: z.ZodString;
184
+ model: z.ZodOptional<z.ZodString>;
185
+ temperature: z.ZodOptional<z.ZodNumber>;
186
+ maxTokens: z.ZodOptional<z.ZodNumber>;
187
+ tools: z.ZodOptional<z.ZodArray<z.ZodString>>;
188
+ enabled: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
189
+ }, z.core.$strip>;
190
+ type SubAgentConfig = z.infer<typeof SubAgentConfigSchema>;
191
+ /**
192
+ * 用户配置插件加载选项
193
+ */
194
+ interface UserConfigPluginLoadOptions {
195
+ configs?: UserConfigPlugin[];
196
+ dirs?: string[];
197
+ scan?: boolean;
198
+ }
199
+
200
+ interface StreamOptions {
201
+ abortSignal?: AbortSignal;
202
+ onStepFinish?: (event: StepResult<any>) => void;
203
+ onChunk?: (event: {
204
+ chunk: any;
205
+ }) => void;
206
+ toolExecutionContext?: ToolExecutionContext;
207
+ }
208
+ declare const streamTextAI: (messages: ModelMessage[], tools: Record<string, Tool>, options?: StreamOptions) => ReturnType<typeof streamText> & {
209
+ steps: StepResult<any>[];
210
+ finishReason: string;
211
+ };
212
+
213
+ type CompactResult = {
214
+ didCompact: boolean;
215
+ reason?: string;
216
+ newMessages?: ModelMessage[];
217
+ };
218
+ declare const maybeCompactContext: (context: Context, options?: {
219
+ force?: boolean;
220
+ }) => Promise<CompactResult>;
221
+
222
+ interface ClarifyInput {
223
+ question: string;
224
+ context?: string;
225
+ defaultAnswer?: string;
226
+ timeout?: number;
227
+ }
228
+ interface ClarifyOutput {
229
+ answer: string;
230
+ timedOut: boolean;
231
+ }
232
+ declare const ClarifyTool: Tool<ClarifyInput, ClarifyOutput>;
233
+
234
+ declare const ReadTool: Tool<{
235
+ filePath: string;
236
+ offset?: number;
237
+ limit?: number;
238
+ }, {
239
+ content: string;
240
+ totalLines?: number;
241
+ }>;
242
+
243
+ declare const WriteTool: Tool<{
244
+ filePath: string;
245
+ content: string;
246
+ }, {
247
+ success: boolean;
248
+ created: boolean;
249
+ bytes: number;
250
+ }>;
251
+
252
+ declare const EditTool: Tool<{
253
+ filePath: string;
254
+ oldString: string;
255
+ newString: string;
256
+ replaceAll?: boolean;
257
+ }, {
258
+ success: boolean;
259
+ replacements: number;
260
+ preview?: string;
261
+ }>;
262
+
263
+ declare const GrepTool: Tool<{
264
+ pattern: string;
265
+ path?: string;
266
+ glob?: string;
267
+ type?: string;
268
+ outputMode?: 'content' | 'files_with_matches' | 'count';
269
+ context?: number;
270
+ caseInsensitive?: boolean;
271
+ headLimit?: number;
272
+ offset?: number;
273
+ multiline?: boolean;
274
+ }, {
275
+ output: string;
276
+ matches?: number;
277
+ }>;
278
+
279
+ declare const LsTool: Tool<{
280
+ path?: string;
281
+ }, {
282
+ files: string[];
283
+ }>;
284
+
285
+ declare const BashTool: Tool<{
286
+ command: string;
287
+ timeout?: number;
288
+ cwd?: string;
289
+ description?: string;
290
+ }, {
291
+ output: string;
292
+ error?: string;
293
+ exitCode?: number;
294
+ }>;
295
+
296
+ declare const TavilyTool: Tool<{
297
+ query: string;
298
+ maxResults?: number;
299
+ }, {
300
+ results: Array<{
301
+ title: string;
302
+ url: string;
303
+ content: string;
304
+ score?: number;
305
+ }>;
306
+ }>;
307
+
308
+ declare const BuiltinTools: readonly [Tool<{
309
+ filePath: string;
310
+ offset?: number;
311
+ limit?: number;
312
+ }, {
313
+ content: string;
314
+ totalLines?: number;
315
+ }>, Tool<{
316
+ filePath: string;
317
+ content: string;
318
+ }, {
319
+ success: boolean;
320
+ created: boolean;
321
+ bytes: number;
322
+ }>, Tool<{
323
+ filePath: string;
324
+ oldString: string;
325
+ newString: string;
326
+ replaceAll?: boolean;
327
+ }, {
328
+ success: boolean;
329
+ replacements: number;
330
+ preview?: string;
331
+ }>, Tool<{
332
+ pattern: string;
333
+ path?: string;
334
+ glob?: string;
335
+ type?: string;
336
+ outputMode?: "content" | "files_with_matches" | "count";
337
+ context?: number;
338
+ caseInsensitive?: boolean;
339
+ headLimit?: number;
340
+ offset?: number;
341
+ multiline?: boolean;
342
+ }, {
343
+ output: string;
344
+ matches?: number;
345
+ }>, Tool<{
346
+ path?: string;
347
+ }, {
348
+ files: string[];
349
+ }>, Tool<{
350
+ command: string;
351
+ timeout?: number;
352
+ cwd?: string;
353
+ description?: string;
354
+ }, {
355
+ output: string;
356
+ error?: string;
357
+ exitCode?: number;
358
+ }>, Tool<{
359
+ query: string;
360
+ maxResults?: number;
361
+ }, {
362
+ results: Array<{
363
+ title: string;
364
+ url: string;
365
+ content: string;
366
+ score?: number;
367
+ }>;
368
+ }>, Tool<ClarifyInput, ClarifyOutput>];
369
+ declare const BuiltinToolsMap: Record<string, any>;
370
+ declare const getFinalToolsMap: (customTools?: Record<string, Tool$1>) => Record<string, any>;
371
+
372
+ /**
373
+ * 引擎配置选项
374
+ */
375
+ interface EngineOptions {
376
+ enginePlugins?: EnginePluginLoadOptions;
377
+ userConfigPlugins?: UserConfigPluginLoadOptions;
378
+ disableBuiltInPlugins?: boolean;
379
+ config?: Record<string, any>;
380
+ }
381
+ /**
382
+ * 重构后的引擎类
383
+ * 自动包含内置插件,支持可选禁用
384
+ */
385
+ declare class Engine {
386
+ private pluginManager;
387
+ private tools;
388
+ private options;
389
+ private config;
390
+ constructor(options?: EngineOptions);
391
+ /**
392
+ * 初始化引擎和插件系统
393
+ * 自动包含内置插件
394
+ */
395
+ initialize(): Promise<void>;
396
+ /**
397
+ * 准备引擎插件列表(包含内置插件)
398
+ */
399
+ private prepareEnginePlugins;
400
+ /**
401
+ * 运行AI循环
402
+ */
403
+ run(context: Context, options?: LoopOptions): Promise<string>;
404
+ /**
405
+ * 获取插件状态
406
+ */
407
+ getPluginStatus(): {
408
+ enginePlugins: string[];
409
+ userConfigPlugins: string[];
410
+ tools: string[];
411
+ services: string[];
412
+ protocols: string[];
413
+ };
414
+ /**
415
+ * 获取工具
416
+ */
417
+ getTools(): Record<string, any>;
418
+ /**
419
+ * 获取服务
420
+ */
421
+ getService<T>(name: string): T | undefined;
422
+ /**
423
+ * 获取配置
424
+ */
425
+ getConfig<T>(key: string): T | undefined;
426
+ /**
427
+ * 设置配置
428
+ */
429
+ setConfig<T>(key: string, value: T): void;
430
+ }
431
+
432
+ /**
433
+ * 插件管理器 - 管理双轨插件系统
434
+ */
435
+ declare class PluginManager {
436
+ private enginePlugins;
437
+ private userConfigPlugins;
438
+ private tools;
439
+ private services;
440
+ private protocols;
441
+ private config;
442
+ private events;
443
+ private logger;
444
+ /**
445
+ * 初始化插件系统
446
+ */
447
+ initialize(options?: {
448
+ enginePlugins?: EnginePluginLoadOptions;
449
+ userConfigPlugins?: UserConfigPluginLoadOptions;
450
+ }): Promise<void>;
451
+ /**
452
+ * 加载引擎插件
453
+ */
454
+ private loadEnginePlugins;
455
+ /**
456
+ * 扫描引擎插件目录
457
+ */
458
+ private scanEnginePlugins;
459
+ /**
460
+ * 加载单个引擎插件文件
461
+ */
462
+ private loadEnginePluginFile;
463
+ /**
464
+ * 初始化单个引擎插件
465
+ */
466
+ private initializeEnginePlugin;
467
+ /**
468
+ * 加载用户配置插件
469
+ */
470
+ private loadUserConfigPlugins;
471
+ /**
472
+ * 扫描用户配置插件目录
473
+ */
474
+ private scanUserConfigPlugins;
475
+ /**
476
+ * 加载单个用户配置文件
477
+ */
478
+ private loadUserConfigFile;
479
+ /**
480
+ * 应用用户配置
481
+ */
482
+ private applyUserConfig;
483
+ /**
484
+ * 验证核心能力
485
+ */
486
+ private validateCoreCapabilities;
487
+ /**
488
+ * 插件依赖排序
489
+ */
490
+ private sortPluginsByDependencies;
491
+ /**
492
+ * 工具获取
493
+ */
494
+ getTools(): Record<string, any>;
495
+ /**
496
+ * 服务获取
497
+ */
498
+ getService<T>(name: string): T | undefined;
499
+ /**
500
+ * 协议获取
501
+ */
502
+ getProtocol(name: string): any;
503
+ /**
504
+ * 获取插件状态
505
+ */
506
+ getStatus(): {
507
+ enginePlugins: string[];
508
+ userConfigPlugins: string[];
509
+ tools: string[];
510
+ services: string[];
511
+ protocols: string[];
512
+ };
513
+ /**
514
+ * 解析路径(支持 ~ 和相对路径)
515
+ */
516
+ private resolvePath;
517
+ }
518
+
519
+ declare class SubAgentPlugin implements EnginePlugin {
520
+ name: string;
521
+ version: string;
522
+ private configLoader;
523
+ private agentRunner;
524
+ initialize(context: EnginePluginContext): Promise<void>;
525
+ private getAvailableTools;
526
+ private registerAgentTool;
527
+ destroy(context: EnginePluginContext): Promise<void>;
528
+ }
529
+
530
+ /**
531
+ * Built-in MCP Plugin for Pulse Coder Engine
532
+ * 将 MCP 功能作为引擎内置插件
533
+ */
534
+
535
+ declare const builtInMCPPlugin: EnginePlugin;
536
+
537
+ /**
538
+ * Built-in Skills Plugin for Pulse Coder Engine
539
+ * 将技能系统作为引擎内置插件
540
+ */
541
+
542
+ /**
543
+ * 技能信息接口
544
+ */
545
+ interface SkillInfo {
546
+ name: string;
547
+ description: string;
548
+ location: string;
549
+ content: string;
550
+ metadata?: Record<string, any>;
551
+ }
552
+ /**
553
+ * 技能注册表
554
+ */
555
+ declare class BuiltInSkillRegistry {
556
+ private skills;
557
+ private initialized;
558
+ /**
559
+ * 初始化注册表,扫描并加载所有技能
560
+ */
561
+ initialize(cwd: string): Promise<void>;
562
+ /**
563
+ * 扫描技能文件
564
+ */
565
+ private scanSkills;
566
+ /**
567
+ * 解析技能文件
568
+ */
569
+ private parseSkillFile;
570
+ /**
571
+ * 获取所有技能
572
+ */
573
+ getAll(): SkillInfo[];
574
+ /**
575
+ * 根据名称获取技能
576
+ */
577
+ get(name: string): SkillInfo | undefined;
578
+ /**
579
+ * 检查技能是否存在
580
+ */
581
+ has(name: string): boolean;
582
+ /**
583
+ * 搜索技能(模糊匹配)
584
+ */
585
+ search(keyword: string): SkillInfo[];
586
+ }
587
+ /**
588
+ * 内置技能插件
589
+ */
590
+ declare const builtInSkillsPlugin: EnginePlugin;
591
+
592
+ /**
593
+ * 默认内置插件列表
594
+ * 这些插件会在引擎启动时自动加载
595
+ */
596
+ declare const builtInPlugins: (EnginePlugin | SubAgentPlugin)[];
597
+
598
+ export { BashTool, BuiltInSkillRegistry, BuiltinTools, BuiltinToolsMap, type ClarificationRequest, ClarifyTool, type Context, EditTool, Engine, type EnginePlugin, type EnginePluginContext, type Extension, GrepTool, type IExtensionContext, type ILogger, type IPlugin, LsTool, PluginManager, Engine as PulseAgent, ReadTool, TavilyTool, type Tool, type ToolExecutionContext, type UserConfigPlugin, type UserConfigPluginLoadOptions, WriteTool, builtInMCPPlugin, builtInPlugins, builtInSkillsPlugin, getFinalToolsMap, loop, maybeCompactContext, streamTextAI };