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

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,627 @@
1
+ import { FlexibleSchema, ModelMessage, LanguageModel, StepResult, ToolSet, streamText, Tool as Tool$1 } from 'ai';
2
+ import { E as EnginePluginLoadOptions } from './index-DDqISE31.cjs';
3
+ export { B as BuiltInSkillRegistry, a as EnginePlugin, b as EnginePluginContext, c as builtInMCPPlugin, d as builtInPlugins, e as builtInSkillsPlugin } from './index-DDqISE31.cjs';
4
+ import { z } from 'zod';
5
+ import 'events';
6
+
7
+ /**
8
+ * Custom LLM provider factory - receives a model name and returns a LanguageModel.
9
+ * Use this to plug in any Vercel AI SDK-compatible provider.
10
+ *
11
+ * @example
12
+ * import { createOpenAI } from '@ai-sdk/openai';
13
+ * const provider: LLMProviderFactory = createOpenAI({ apiKey: '...' }).chat;
14
+ *
15
+ * @example
16
+ * import { createAnthropic } from '@ai-sdk/anthropic';
17
+ * const provider: LLMProviderFactory = createAnthropic({ apiKey: '...' });
18
+ */
19
+ type LLMProviderFactory = (model: string) => LanguageModel;
20
+ /**
21
+ * System prompt customization.
22
+ * - `string` — replaces the built-in prompt entirely.
23
+ * - `() => string` — factory evaluated on every request (supports dynamic prompts).
24
+ * - `{ append: string }` — appends extra content after the built-in prompt.
25
+ */
26
+ type SystemPromptOption = string | (() => string) | {
27
+ append: string;
28
+ };
29
+ /**
30
+ * Hooks that fire around every tool execution.
31
+ *
32
+ * @example
33
+ * const hooks: ToolHooks = {
34
+ * onBeforeToolCall: (name, input) => {
35
+ * if (name === 'bash') throw new Error('bash tool is disabled');
36
+ * return input; // can return modified input
37
+ * },
38
+ * onAfterToolCall: (name, input, output) => {
39
+ * auditLog(name, input, output);
40
+ * return output; // can return modified output
41
+ * },
42
+ * };
43
+ */
44
+ interface ToolHooks {
45
+ /**
46
+ * Called before a tool executes.
47
+ * Return a (possibly modified) input object, or throw to abort the call.
48
+ */
49
+ onBeforeToolCall?: (name: string, input: any) => any | Promise<any>;
50
+ /**
51
+ * Called after a tool executes.
52
+ * Return a (possibly modified) output value.
53
+ */
54
+ onAfterToolCall?: (name: string, input: any, output: any) => any | Promise<any>;
55
+ }
56
+ interface ClarificationRequest {
57
+ id: string;
58
+ question: string;
59
+ context?: string;
60
+ defaultAnswer?: string;
61
+ timeout: number;
62
+ }
63
+ interface ToolExecutionContext {
64
+ onClarificationRequest?: (request: ClarificationRequest) => Promise<string>;
65
+ abortSignal?: AbortSignal;
66
+ }
67
+ interface Tool<Input = any, Output = any> {
68
+ name: string;
69
+ description: string;
70
+ inputSchema: FlexibleSchema<Input>;
71
+ execute: (input: Input, context?: ToolExecutionContext) => Promise<Output>;
72
+ }
73
+ interface Context {
74
+ messages: ModelMessage[];
75
+ }
76
+ interface IPlugin {
77
+ name: string;
78
+ version: string;
79
+ extensions: Extension[];
80
+ activate(context: IExtensionContext): Promise<void>;
81
+ deactivate?(): Promise<void>;
82
+ }
83
+ interface Extension {
84
+ type: 'skill' | 'mcp' | 'tool' | 'context';
85
+ provider: any;
86
+ }
87
+ interface IExtensionContext {
88
+ registerTools(toolName: string, tool: Tool<any, any>): void;
89
+ logger: ILogger;
90
+ }
91
+ interface ILogger {
92
+ debug(message: string, meta?: Record<string, unknown>): void;
93
+ info(message: string, meta?: Record<string, unknown>): void;
94
+ warn(message: string, meta?: Record<string, unknown>): void;
95
+ error(message: string, error?: Error, meta?: Record<string, unknown>): void;
96
+ }
97
+
98
+ interface LoopOptions {
99
+ onText?: (delta: string) => void;
100
+ onToolCall?: (toolCall: any) => void;
101
+ onToolResult?: (toolResult: any) => void;
102
+ onStepFinish?: (step: StepResult<any>) => void;
103
+ onClarificationRequest?: (request: ClarificationRequest) => Promise<string>;
104
+ onCompacted?: (newMessages: ModelMessage[]) => void;
105
+ onResponse?: (messages: StepResult<ToolSet>['response']['messages']) => void;
106
+ abortSignal?: AbortSignal;
107
+ tools?: Record<string, Tool>;
108
+ /** Custom LLM provider factory. Overrides the default provider when set. */
109
+ provider?: LLMProviderFactory;
110
+ /** Model name passed to the provider. Overrides DEFAULT_MODEL when set. */
111
+ model?: string;
112
+ /** Custom system prompt. See SystemPromptOption for the three supported forms. */
113
+ systemPrompt?: SystemPromptOption;
114
+ /** Hooks fired around every tool execution (before/after). */
115
+ hooks?: ToolHooks;
116
+ }
117
+ declare function loop(context: Context, options?: LoopOptions): Promise<string>;
118
+
119
+ /**
120
+ * 用户配置插件接口 - 面向终端用户
121
+ * 使用声明式配置扩展引擎能力
122
+ */
123
+ interface UserConfigPlugin {
124
+ version: string;
125
+ name?: string;
126
+ description?: string;
127
+ tools?: Record<string, ToolConfig>;
128
+ mcp?: {
129
+ servers: MCPServerConfig[];
130
+ };
131
+ prompts?: {
132
+ system?: string;
133
+ user?: string;
134
+ assistant?: string;
135
+ };
136
+ subAgents?: SubAgentConfig[];
137
+ skills?: {
138
+ directories?: string[];
139
+ autoScan?: boolean;
140
+ cache?: boolean;
141
+ };
142
+ env?: Record<string, string>;
143
+ conditions?: {
144
+ environment?: string;
145
+ features?: string[];
146
+ };
147
+ }
148
+ /**
149
+ * 工具配置接口
150
+ */
151
+ declare const ToolConfigSchema: z.ZodObject<{
152
+ type: z.ZodEnum<{
153
+ skill: "skill";
154
+ bash: "bash";
155
+ http: "http";
156
+ javascript: "javascript";
157
+ custom: "custom";
158
+ }>;
159
+ command: z.ZodOptional<z.ZodString>;
160
+ args: z.ZodOptional<z.ZodArray<z.ZodString>>;
161
+ cwd: z.ZodOptional<z.ZodString>;
162
+ env: z.ZodOptional<z.ZodAny>;
163
+ timeout: z.ZodOptional<z.ZodNumber>;
164
+ method: z.ZodOptional<z.ZodEnum<{
165
+ GET: "GET";
166
+ POST: "POST";
167
+ PUT: "PUT";
168
+ DELETE: "DELETE";
169
+ PATCH: "PATCH";
170
+ }>>;
171
+ url: z.ZodOptional<z.ZodString>;
172
+ baseUrl: z.ZodOptional<z.ZodString>;
173
+ headers: z.ZodOptional<z.ZodAny>;
174
+ params: z.ZodOptional<z.ZodAny>;
175
+ data: z.ZodOptional<z.ZodAny>;
176
+ code: z.ZodOptional<z.ZodString>;
177
+ description: z.ZodOptional<z.ZodString>;
178
+ enabled: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
179
+ conditions: z.ZodOptional<z.ZodObject<{
180
+ environment: z.ZodOptional<z.ZodString>;
181
+ platform: z.ZodOptional<z.ZodString>;
182
+ }, z.core.$strip>>;
183
+ }, z.core.$strip>;
184
+ type ToolConfig = z.infer<typeof ToolConfigSchema>;
185
+ /**
186
+ * MCP 服务器配置接口
187
+ */
188
+ declare const MCPServerConfigSchema: z.ZodObject<{
189
+ name: z.ZodString;
190
+ command: z.ZodString;
191
+ args: z.ZodOptional<z.ZodArray<z.ZodString>>;
192
+ env: z.ZodOptional<z.ZodAny>;
193
+ cwd: z.ZodOptional<z.ZodString>;
194
+ timeout: z.ZodOptional<z.ZodNumber>;
195
+ description: z.ZodOptional<z.ZodString>;
196
+ enabled: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
197
+ }, z.core.$strip>;
198
+ type MCPServerConfig = z.infer<typeof MCPServerConfigSchema>;
199
+ /**
200
+ * 子代理配置接口
201
+ */
202
+ declare const SubAgentConfigSchema: z.ZodObject<{
203
+ name: z.ZodString;
204
+ trigger: z.ZodArray<z.ZodString>;
205
+ prompt: z.ZodString;
206
+ model: z.ZodOptional<z.ZodString>;
207
+ temperature: z.ZodOptional<z.ZodNumber>;
208
+ maxTokens: z.ZodOptional<z.ZodNumber>;
209
+ tools: z.ZodOptional<z.ZodArray<z.ZodString>>;
210
+ enabled: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
211
+ }, z.core.$strip>;
212
+ type SubAgentConfig = z.infer<typeof SubAgentConfigSchema>;
213
+ /**
214
+ * 用户配置插件加载选项
215
+ */
216
+ interface UserConfigPluginLoadOptions {
217
+ configs?: UserConfigPlugin[];
218
+ dirs?: string[];
219
+ scan?: boolean;
220
+ }
221
+
222
+ interface StreamOptions {
223
+ abortSignal?: AbortSignal;
224
+ onStepFinish?: (event: StepResult<any>) => void;
225
+ onChunk?: (event: {
226
+ chunk: any;
227
+ }) => void;
228
+ toolExecutionContext?: ToolExecutionContext;
229
+ /** Custom LLM provider. Falls back to the default CoderAI provider when not set. */
230
+ provider?: LLMProviderFactory;
231
+ /** Model name to pass to the provider. Falls back to DEFAULT_MODEL when not set. */
232
+ model?: string;
233
+ /** Custom system prompt. See SystemPromptOption for the three supported forms. */
234
+ systemPrompt?: SystemPromptOption;
235
+ }
236
+ declare const streamTextAI: (messages: ModelMessage[], tools: Record<string, Tool>, options?: StreamOptions) => ReturnType<typeof streamText> & {
237
+ steps: StepResult<any>[];
238
+ finishReason: string;
239
+ };
240
+
241
+ type CompactResult = {
242
+ didCompact: boolean;
243
+ reason?: string;
244
+ newMessages?: ModelMessage[];
245
+ };
246
+ declare const maybeCompactContext: (context: Context, options?: {
247
+ force?: boolean;
248
+ provider?: LLMProviderFactory;
249
+ model?: string;
250
+ }) => Promise<CompactResult>;
251
+
252
+ interface ClarifyInput {
253
+ question: string;
254
+ context?: string;
255
+ defaultAnswer?: string;
256
+ timeout?: number;
257
+ }
258
+ interface ClarifyOutput {
259
+ answer: string;
260
+ timedOut: boolean;
261
+ }
262
+ declare const ClarifyTool: Tool<ClarifyInput, ClarifyOutput>;
263
+
264
+ declare const ReadTool: Tool<{
265
+ filePath: string;
266
+ offset?: number;
267
+ limit?: number;
268
+ }, {
269
+ content: string;
270
+ totalLines?: number;
271
+ }>;
272
+
273
+ declare const WriteTool: Tool<{
274
+ filePath: string;
275
+ content: string;
276
+ }, {
277
+ success: boolean;
278
+ created: boolean;
279
+ bytes: number;
280
+ }>;
281
+
282
+ declare const EditTool: Tool<{
283
+ filePath: string;
284
+ oldString: string;
285
+ newString: string;
286
+ replaceAll?: boolean;
287
+ }, {
288
+ success: boolean;
289
+ replacements: number;
290
+ preview?: string;
291
+ }>;
292
+
293
+ declare const GrepTool: Tool<{
294
+ pattern: string;
295
+ path?: string;
296
+ glob?: string;
297
+ type?: string;
298
+ outputMode?: 'content' | 'files_with_matches' | 'count';
299
+ context?: number;
300
+ caseInsensitive?: boolean;
301
+ headLimit?: number;
302
+ offset?: number;
303
+ multiline?: boolean;
304
+ }, {
305
+ output: string;
306
+ matches?: number;
307
+ }>;
308
+
309
+ declare const LsTool: Tool<{
310
+ path?: string;
311
+ }, {
312
+ files: string[];
313
+ }>;
314
+
315
+ declare const BashTool: Tool<{
316
+ command: string;
317
+ timeout?: number;
318
+ cwd?: string;
319
+ description?: string;
320
+ }, {
321
+ output: string;
322
+ error?: string;
323
+ exitCode?: number;
324
+ }>;
325
+
326
+ declare const TavilyTool: Tool<{
327
+ query: string;
328
+ maxResults?: number;
329
+ }, {
330
+ results: Array<{
331
+ title: string;
332
+ url: string;
333
+ content: string;
334
+ score?: number;
335
+ }>;
336
+ }>;
337
+
338
+ declare const BuiltinTools: readonly [Tool<{
339
+ filePath: string;
340
+ offset?: number;
341
+ limit?: number;
342
+ }, {
343
+ content: string;
344
+ totalLines?: number;
345
+ }>, Tool<{
346
+ filePath: string;
347
+ content: string;
348
+ }, {
349
+ success: boolean;
350
+ created: boolean;
351
+ bytes: number;
352
+ }>, Tool<{
353
+ filePath: string;
354
+ oldString: string;
355
+ newString: string;
356
+ replaceAll?: boolean;
357
+ }, {
358
+ success: boolean;
359
+ replacements: number;
360
+ preview?: string;
361
+ }>, Tool<{
362
+ pattern: string;
363
+ path?: string;
364
+ glob?: string;
365
+ type?: string;
366
+ outputMode?: "content" | "files_with_matches" | "count";
367
+ context?: number;
368
+ caseInsensitive?: boolean;
369
+ headLimit?: number;
370
+ offset?: number;
371
+ multiline?: boolean;
372
+ }, {
373
+ output: string;
374
+ matches?: number;
375
+ }>, Tool<{
376
+ path?: string;
377
+ }, {
378
+ files: string[];
379
+ }>, Tool<{
380
+ command: string;
381
+ timeout?: number;
382
+ cwd?: string;
383
+ description?: string;
384
+ }, {
385
+ output: string;
386
+ error?: string;
387
+ exitCode?: number;
388
+ }>, Tool<{
389
+ query: string;
390
+ maxResults?: number;
391
+ }, {
392
+ results: Array<{
393
+ title: string;
394
+ url: string;
395
+ content: string;
396
+ score?: number;
397
+ }>;
398
+ }>, Tool<ClarifyInput, ClarifyOutput>];
399
+ declare const BuiltinToolsMap: Record<string, any>;
400
+ declare const getFinalToolsMap: (customTools?: Record<string, Tool$1>) => Record<string, any>;
401
+
402
+ /**
403
+ * 引擎配置选项
404
+ */
405
+ interface EngineOptions {
406
+ enginePlugins?: EnginePluginLoadOptions;
407
+ userConfigPlugins?: UserConfigPluginLoadOptions;
408
+ disableBuiltInPlugins?: boolean;
409
+ config?: Record<string, any>;
410
+ /**
411
+ * 自定义 LLM Provider。
412
+ * 接收模型名称,返回 Vercel AI SDK LanguageModel 实例。
413
+ * 未设置时使用环境变量配置的默认 Provider(OpenAI / Anthropic)。
414
+ *
415
+ * @example
416
+ * import { createOpenAI } from '@ai-sdk/openai';
417
+ * const engine = new Engine({
418
+ * llmProvider: createOpenAI({ apiKey: 'sk-...', baseURL: 'https://my-proxy/v1' }).chat,
419
+ * model: 'gpt-4o',
420
+ * });
421
+ */
422
+ llmProvider?: LLMProviderFactory;
423
+ /**
424
+ * 模型名称,传递给 llmProvider。未设置时使用 DEFAULT_MODEL。
425
+ */
426
+ model?: string;
427
+ /**
428
+ * 直接注册自定义工具,无需创建 EnginePlugin。
429
+ * 这些工具会与内置工具以及插件注册的工具合并。
430
+ * 若与内置工具同名,自定义工具优先级更高。
431
+ *
432
+ * @example
433
+ * import { z } from 'zod';
434
+ * const engine = new Engine({
435
+ * tools: {
436
+ * myTool: {
437
+ * name: 'myTool',
438
+ * description: '查询内部数据库',
439
+ * inputSchema: z.object({ query: z.string() }),
440
+ * execute: async ({ query }) => fetchFromDB(query),
441
+ * },
442
+ * },
443
+ * });
444
+ */
445
+ tools?: Record<string, Tool>;
446
+ /**
447
+ * 自定义 System Prompt,三种形式:
448
+ * - `string` — 完全替换内置 prompt
449
+ * - `() => string` — 工厂函数,每次请求调用(支持动态 prompt)
450
+ * - `{ append: string }` — 在内置 prompt 末尾追加业务上下文
451
+ *
452
+ * @example
453
+ * const engine = new Engine({
454
+ * systemPrompt: { append: '公司规范:所有变量使用 camelCase。禁止使用 any 类型。' },
455
+ * });
456
+ */
457
+ systemPrompt?: SystemPromptOption;
458
+ /**
459
+ * Tool 执行钩子,在每次工具调用前/后触发。
460
+ * - `onBeforeToolCall` 可以修改入参,或抛错来拦截调用。
461
+ * - `onAfterToolCall` 可以修改返回值(如脱敏、截断)。
462
+ *
463
+ * @example
464
+ * const engine = new Engine({
465
+ * hooks: {
466
+ * onBeforeToolCall: (name, input) => {
467
+ * if (name === 'bash') throw new Error('bash 工具已被禁用');
468
+ * },
469
+ * onAfterToolCall: (name, input, output) => {
470
+ * auditLogger.log({ name, input, output });
471
+ * return output;
472
+ * },
473
+ * },
474
+ * });
475
+ */
476
+ hooks?: ToolHooks;
477
+ /**
478
+ * 自定义日志实现。未设置时使用 console.*。
479
+ * 兼容 winston / pino 等主流日志库。
480
+ *
481
+ * @example
482
+ * import pino from 'pino';
483
+ * const logger = pino();
484
+ * const engine = new Engine({ logger });
485
+ */
486
+ logger?: ILogger;
487
+ }
488
+ /**
489
+ * 重构后的引擎类
490
+ * 自动包含内置插件,支持可选禁用
491
+ */
492
+ declare class Engine {
493
+ private pluginManager;
494
+ private tools;
495
+ private options;
496
+ private config;
497
+ constructor(options?: EngineOptions);
498
+ /**
499
+ * 初始化引擎和插件系统
500
+ * 自动包含内置插件
501
+ */
502
+ initialize(): Promise<void>;
503
+ /**
504
+ * 准备引擎插件列表(包含内置插件)
505
+ */
506
+ private prepareEnginePlugins;
507
+ /**
508
+ * 运行AI循环
509
+ */
510
+ run(context: Context, options?: LoopOptions): Promise<string>;
511
+ /**
512
+ * 获取插件状态
513
+ */
514
+ getPluginStatus(): {
515
+ enginePlugins: string[];
516
+ userConfigPlugins: string[];
517
+ tools: string[];
518
+ services: string[];
519
+ protocols: string[];
520
+ };
521
+ /**
522
+ * 获取工具
523
+ */
524
+ getTools(): Record<string, any>;
525
+ /**
526
+ * 获取服务
527
+ */
528
+ getService<T>(name: string): T | undefined;
529
+ /**
530
+ * 获取配置
531
+ */
532
+ getConfig<T>(key: string): T | undefined;
533
+ /**
534
+ * 设置配置
535
+ */
536
+ setConfig<T>(key: string, value: T): void;
537
+ }
538
+
539
+ /**
540
+ * 插件管理器 - 管理双轨插件系统
541
+ */
542
+ declare class PluginManager {
543
+ private enginePlugins;
544
+ private userConfigPlugins;
545
+ private tools;
546
+ private services;
547
+ private protocols;
548
+ private config;
549
+ private events;
550
+ private logger;
551
+ constructor(logger?: ILogger);
552
+ /**
553
+ * 初始化插件系统
554
+ */
555
+ initialize(options?: {
556
+ enginePlugins?: EnginePluginLoadOptions;
557
+ userConfigPlugins?: UserConfigPluginLoadOptions;
558
+ }): Promise<void>;
559
+ /**
560
+ * 加载引擎插件
561
+ */
562
+ private loadEnginePlugins;
563
+ /**
564
+ * 扫描引擎插件目录
565
+ */
566
+ private scanEnginePlugins;
567
+ /**
568
+ * 加载单个引擎插件文件
569
+ */
570
+ private loadEnginePluginFile;
571
+ /**
572
+ * 初始化单个引擎插件
573
+ */
574
+ private initializeEnginePlugin;
575
+ /**
576
+ * 加载用户配置插件
577
+ */
578
+ private loadUserConfigPlugins;
579
+ /**
580
+ * 扫描用户配置插件目录
581
+ */
582
+ private scanUserConfigPlugins;
583
+ /**
584
+ * 加载单个用户配置文件
585
+ */
586
+ private loadUserConfigFile;
587
+ /**
588
+ * 应用用户配置
589
+ */
590
+ private applyUserConfig;
591
+ /**
592
+ * 验证核心能力
593
+ */
594
+ private validateCoreCapabilities;
595
+ /**
596
+ * 插件依赖排序
597
+ */
598
+ private sortPluginsByDependencies;
599
+ /**
600
+ * 工具获取
601
+ */
602
+ getTools(): Record<string, any>;
603
+ /**
604
+ * 服务获取
605
+ */
606
+ getService<T>(name: string): T | undefined;
607
+ /**
608
+ * 协议获取
609
+ */
610
+ getProtocol(name: string): any;
611
+ /**
612
+ * 获取插件状态
613
+ */
614
+ getStatus(): {
615
+ enginePlugins: string[];
616
+ userConfigPlugins: string[];
617
+ tools: string[];
618
+ services: string[];
619
+ protocols: string[];
620
+ };
621
+ /**
622
+ * 解析路径(支持 ~ 和相对路径)
623
+ */
624
+ private resolvePath;
625
+ }
626
+
627
+ export { BashTool, BuiltinTools, BuiltinToolsMap, type ClarificationRequest, ClarifyTool, type Context, EditTool, Engine, type Extension, GrepTool, type IExtensionContext, type ILogger, type IPlugin, type LLMProviderFactory, LsTool, PluginManager, Engine as PulseAgent, ReadTool, type SystemPromptOption, TavilyTool, type Tool, type ToolExecutionContext, type ToolHooks, type UserConfigPlugin, type UserConfigPluginLoadOptions, WriteTool, getFinalToolsMap, loop, maybeCompactContext, streamTextAI };