@snack-kit/porygon 0.1.0 → 0.3.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/dist/index.d.ts CHANGED
@@ -15,7 +15,15 @@ interface BackendConfig {
15
15
  appendSystemPrompt?: string;
16
16
  proxy?: ProxyConfig;
17
17
  cwd?: string;
18
- /** 透传给 CLI 的后端特定选项 */
18
+ /** 远程服务地址(OpenCode serve 模式) */
19
+ serverUrl?: string;
20
+ /** API Key 认证 */
21
+ apiKey?: string;
22
+ /** 是否为交互模式,false 时 Claude 会添加 --dangerously-skip-permissions */
23
+ interactive?: boolean;
24
+ /** CLI 可执行文件路径(如 Claude CLI 的自定义安装路径) */
25
+ cliPath?: string;
26
+ /** 透传给 CLI 的后端特定选项(向后兼容,推荐使用上方的显式字段) */
19
27
  options?: Record<string, unknown>;
20
28
  }
21
29
  /**
@@ -84,6 +92,13 @@ interface AgentSystemMessage extends BaseAgentMessage {
84
92
  interface AgentAssistantMessage extends BaseAgentMessage {
85
93
  type: "assistant";
86
94
  text: string;
95
+ /**
96
+ * 标记此消息为一个 turn 的完整文本汇总。
97
+ * 当为 true 时,text 内容与之前发出的 stream_chunk 消息完全重复,
98
+ * 调用方如果已通过 stream_chunk 累加文本,应忽略此消息的 text 以避免重复。
99
+ * run() 和拦截器仍会使用此消息。
100
+ */
101
+ turnComplete?: boolean;
87
102
  }
88
103
  interface AgentToolUseMessage extends BaseAgentMessage {
89
104
  type: "tool_use";
@@ -113,7 +128,13 @@ interface AgentErrorMessage extends BaseAgentMessage {
113
128
  * 适配器能力声明
114
129
  */
115
130
  interface AdapterCapabilities {
116
- features: Set<"streaming" | "session-resume" | "system-prompt" | "tool-restriction" | "mcp" | "subagents" | "worktree" | "budget-limit" | "serve-mode">;
131
+ features: Set<"streaming" | "session-resume" | "system-prompt" | "tool-restriction" | "mcp" | "subagents" | "worktree" | "budget-limit" | "serve-mode" | "interactive-session">;
132
+ /**
133
+ * 流式输出模式:
134
+ * - 'delta': 后端原生产生增量 stream_chunk 事件(如 OpenCode)
135
+ * - 'chunked': 适配器将完整 assistant 消息拆分为 stream_chunk + assistant(如 Claude)
136
+ */
137
+ streamingMode: "delta" | "chunked";
117
138
  outputFormats: string[];
118
139
  testedVersionRange: string;
119
140
  }
@@ -159,6 +180,8 @@ interface IAgentAdapter {
159
180
  /** 获取可用模型列表 */
160
181
  listModels(): Promise<ModelInfo[]>;
161
182
  abort(sessionId: string): void;
183
+ /** 删除会话及其资源(可选实现) */
184
+ deleteSession?(sessionId: string): Promise<void>;
162
185
  dispose(): Promise<void>;
163
186
  }
164
187
  /**
@@ -186,6 +209,48 @@ interface InterceptorContext {
186
209
  */
187
210
  type InterceptorFn = (text: string, context: InterceptorContext) => string | boolean | undefined | Promise<string | boolean | undefined>;
188
211
 
212
+ /**
213
+ * 便捷的多轮对话封装。
214
+ *
215
+ * 底层仍是 per-turn spawn + --resume,但对调用方透明:
216
+ * ```ts
217
+ * const session = porygon.session({ systemPrompt: '...' });
218
+ * for await (const msg of session.send('Hello')) { ... }
219
+ * for await (const msg of session.send('继续')) { ... } // 自动 resume
220
+ * session.close();
221
+ * ```
222
+ */
223
+ declare class InteractiveSession {
224
+ readonly initialSessionId: string;
225
+ private resolvedSessionId?;
226
+ private adapter;
227
+ private baseRequest;
228
+ private firstSent;
229
+ private closed;
230
+ constructor(initialSessionId: string, adapter: IAgentAdapter, baseRequest: Omit<PromptRequest, "prompt">);
231
+ /** 当前生效的 sessionId(首次 send 后反映 CLI 返回的真实 ID) */
232
+ get sessionId(): string;
233
+ /** 会话是否仍然活跃 */
234
+ get isActive(): boolean;
235
+ /**
236
+ * 发送一条消息,返回流式响应。
237
+ * 首次调用使用 initialSessionId,后续自动附加 resume。
238
+ */
239
+ send(prompt: string): AsyncGenerator<AgentMessage>;
240
+ /** 关闭会话(仅清理内部状态,无进程需要释放) */
241
+ close(): void;
242
+ }
243
+
244
+ /**
245
+ * 健康检查结果(扁平化结构)
246
+ */
247
+ interface HealthCheckResult {
248
+ available: boolean;
249
+ version?: string;
250
+ supported?: boolean;
251
+ warnings?: string[];
252
+ error?: string;
253
+ }
189
254
  /**
190
255
  * Porygon 事件类型定义
191
256
  */
@@ -237,6 +302,11 @@ declare class Porygon extends EventEmitter {
237
302
  * @returns 最终结果文本
238
303
  */
239
304
  run(request: PromptRequest): Promise<string>;
305
+ /**
306
+ * 创建交互式多轮对话会话。
307
+ * 自动管理 sessionId 和 resume,对调用方透明。
308
+ */
309
+ session(options?: Omit<PromptRequest, "prompt">): InteractiveSession;
240
310
  /**
241
311
  * 注册拦截器
242
312
  * @param direction 拦截方向
@@ -255,13 +325,15 @@ declare class Porygon extends EventEmitter {
255
325
  */
256
326
  listModels(backend?: string): Promise<ModelInfo[]>;
257
327
  /**
258
- * 对所有已注册后端进行健康检查
328
+ * 检查单个后端的健康状态
329
+ * @param backend 后端名称
259
330
  */
260
- healthCheck(): Promise<Record<string, {
261
- available: boolean;
262
- compatibility: CompatibilityResult | null;
263
- error?: string;
264
- }>>;
331
+ checkBackend(backend: string): Promise<HealthCheckResult>;
332
+ /**
333
+ * 对所有已注册后端进行健康检查。
334
+ * 返回扁平化结构,包含 version/supported/warnings 等字段。
335
+ */
336
+ healthCheck(): Promise<Record<string, HealthCheckResult>>;
265
337
  /**
266
338
  * 读取或更新后端设置
267
339
  * @param backend 后端名称
@@ -286,7 +358,16 @@ declare class Porygon extends EventEmitter {
286
358
  */
287
359
  dispose(): Promise<void>;
288
360
  /**
289
- * 合并请求参数与配置默认值
361
+ * 合并请求参数与配置默认值。
362
+ *
363
+ * 合并策略:
364
+ * - model: request > backendConfig > 不设置(后端使用自身默认值)
365
+ * - timeoutMs: request > defaults
366
+ * - maxTurns: request > defaults
367
+ * - cwd: request > backendConfig
368
+ * - appendSystemPrompt: **追加模式** — defaults + backendConfig + request 三层拼接(换行分隔)
369
+ * 但如果 request.systemPrompt 已设置(替换模式),则忽略所有 appendSystemPrompt
370
+ *
290
371
  * @param request 原始请求
291
372
  * @param backend 后端名称
292
373
  * @returns 合并后的请求
@@ -414,6 +495,8 @@ interface SpawnOptions {
414
495
  cwd?: string;
415
496
  env?: Record<string, string>;
416
497
  timeoutMs?: number;
498
+ /** 写入 stdin 后自动关闭的数据 */
499
+ stdinData?: string;
417
500
  }
418
501
  /**
419
502
  * 进程执行结果
@@ -588,6 +671,8 @@ declare abstract class AbstractAgentAdapter implements IAgentAdapter {
588
671
  */
589
672
  declare class ClaudeAdapter extends AbstractAgentAdapter {
590
673
  readonly backend = "claude";
674
+ /** CLI 命令名或路径 */
675
+ private get cliCommand();
591
676
  /**
592
677
  * 检查 Claude CLI 是否可用
593
678
  */
@@ -635,6 +720,11 @@ declare class ClaudeAdapter extends AbstractAgentAdapter {
635
720
  * @param noProxy 不走代理的地址列表
636
721
  */
637
722
  private applyProxyEnv;
723
+ /**
724
+ * 构建通过 stdin 传递给 Claude CLI 的数据。
725
+ * 使用 stdin 而非 CLI 参数传递 prompt,避免超长参数导致的 403 错误。
726
+ */
727
+ private buildStdinData;
638
728
  private buildArgs;
639
729
  }
640
730
 
@@ -646,11 +736,18 @@ declare class OpenCodeAdapter extends AbstractAgentAdapter {
646
736
  readonly backend = "opencode";
647
737
  private apiClient;
648
738
  private servePort;
739
+ /** 远程服务器 URL(提供时跳过自启 serve) */
740
+ private remoteServerUrl?;
741
+ /** API Key(用于 Basic Auth) */
742
+ private apiKey?;
649
743
  /**
650
744
  * @param processManager 进程管理器实例
651
- * @param servePort 可选的 serve 服务端口
745
+ * @param config 可选的后端配置
746
+ * - serverUrl: 远程 OpenCode 服务地址,提供时跳过自启 serve
747
+ * - apiKey: API Key,用于 Basic Auth 认证
748
+ * - options.servePort: 本地 serve 端口(仅自启模式)
652
749
  */
653
- constructor(processManager: ProcessManager, servePort?: number);
750
+ constructor(processManager: ProcessManager, config?: BackendConfig);
654
751
  /**
655
752
  * 检查 opencode 命令是否可用
656
753
  */
@@ -712,4 +809,4 @@ declare class OpenCodeAdapter extends AbstractAgentAdapter {
712
809
  private getConfigPath;
713
810
  }
714
811
 
715
- export { AbstractAgentAdapter, type AdapterCapabilities, AdapterIncompatibleError, AdapterNotAvailableError, AdapterNotFoundError, type AgentAssistantMessage, type AgentErrorMessage, AgentExecutionError, type AgentMessage, type AgentResultMessage, type AgentStreamChunkMessage, type AgentSystemMessage, AgentTimeoutError, type AgentToolUseMessage, type BackendConfig, ClaudeAdapter, type CompatibilityResult, ConfigValidationError, type GuardAction, type GuardOptions, type IAgentAdapter, type InterceptorContext, type InterceptorDirection, type InterceptorFn, InterceptorRejectedError, type McpServerConfig, type ModelInfo, OpenCodeAdapter, Porygon, type PorygonConfig, PorygonError, type PorygonEvents, type PromptRequest, type ProxyConfig, type SessionInfo, type SessionListOptions, SessionNotFoundError, createInputGuard, createOutputGuard, createPorygon };
812
+ export { AbstractAgentAdapter, type AdapterCapabilities, AdapterIncompatibleError, AdapterNotAvailableError, AdapterNotFoundError, type AgentAssistantMessage, type AgentErrorMessage, AgentExecutionError, type AgentMessage, type AgentResultMessage, type AgentStreamChunkMessage, type AgentSystemMessage, AgentTimeoutError, type AgentToolUseMessage, type BackendConfig, ClaudeAdapter, type CompatibilityResult, ConfigValidationError, type GuardAction, type GuardOptions, type HealthCheckResult, type IAgentAdapter, InteractiveSession, type InterceptorContext, type InterceptorDirection, type InterceptorFn, InterceptorRejectedError, type McpServerConfig, type ModelInfo, OpenCodeAdapter, Porygon, type PorygonConfig, PorygonError, type PorygonEvents, type PromptRequest, type ProxyConfig, type SessionInfo, type SessionListOptions, SessionNotFoundError, createInputGuard, createOutputGuard, createPorygon };