atomix-cli 1.0.0 → 1.1.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.
@@ -0,0 +1,360 @@
1
+ /**
2
+ * 会话库的公开类型面(docs/session-api-v1.md §4 / §6)。
3
+ *
4
+ * 自包含:不 import atomix-core 的任何类型。原因是 `atomix-cli/session` 的 d.ts 要发给消费方,
5
+ * 而 atomix-core 是未发布的 fork,其类型又牵着 @anthropic-ai/sdk;这里用结构等价的本地定义,
6
+ * session.ts 里有编译期断言保证与 core 的定义一致(PermissionMode 等)。
7
+ * `core` 字段对外只承诺 SessionCoreApi 这个子集;运行时就是完整的 AtomixCore,内部模块用泛型参数拿到全类型。
8
+ */
9
+ export type PermissionMode = "step-by-step" | "action-check" | "free-style";
10
+ export type LogLevel = "debug" | "info" | "warn" | "error" | "none";
11
+ export interface Usage {
12
+ useTokens: number;
13
+ maxTokens: number;
14
+ promptTokens: number;
15
+ }
16
+ /** core `session:ready` 事件数据(结构同 atomix-core 的 SessionReadyData) */
17
+ export interface SessionReadyInfo {
18
+ workingDir: string;
19
+ sessionId: string;
20
+ historyLoaded: boolean;
21
+ usage: Usage;
22
+ projectInputHistory: string[];
23
+ }
24
+ /** 对外承诺的 core 子集:事件、权限应答、中断、释放。完整 AtomixCore 仍可用,但不在类型面上承诺 */
25
+ export interface SessionCoreApi {
26
+ on<T>(event: string, listener: (data: T) => void): unknown;
27
+ once<T>(event: string, listener: (data: T) => void): unknown;
28
+ off<T>(event: string, listener: (data: T) => void): unknown;
29
+ updatePermissionMode(mode: PermissionMode): void;
30
+ getPermissionMode(): PermissionMode;
31
+ interruptSession(): unknown;
32
+ getCurrentSessionId(): string | null;
33
+ respondToToolPermission(response: {
34
+ toolName: string;
35
+ selected: string;
36
+ agentId?: string;
37
+ }): unknown;
38
+ respondToAskQuestion(response: {
39
+ agentId: string;
40
+ answers: Record<string, string>;
41
+ }): unknown;
42
+ respondToForm(response: {
43
+ agentId: string;
44
+ values: Record<string, unknown>;
45
+ submitted: boolean;
46
+ }): unknown;
47
+ dispose(): Promise<void>;
48
+ }
49
+ export interface SessionCoreOptions {
50
+ cwd: string;
51
+ /** 缺省:interactive → step-by-step;headless → free-style(与现行 -p 一致,决策 1) */
52
+ permissionMode?: PermissionMode;
53
+ /** true = 有人值守(权限/提问/表单由宿主 UI 应答);false(默认)= 无人值守,自动应答器 fail-closed 接管(决策 1) */
54
+ interactive?: boolean;
55
+ /** 一进程多会话宿主传 true(core multiSession,session-api-v1 §3.2);交互入口保持缺省 */
56
+ multiSession?: boolean;
57
+ /** 缺省跟随 interactive */
58
+ stream?: boolean;
59
+ /** 是否加载 hooks(交互入口 true;-p 现状 false);缺省跟随 interactive */
60
+ hooks?: boolean;
61
+ systemPrompt?: string;
62
+ logLevel?: LogLevel;
63
+ }
64
+ export interface StartSessionOptions {
65
+ /** 会话级 harness 覆盖(不读不写项目态);缺省读 <cwd>/.atomix/harness-state.json;名字不存在直接抛错 */
66
+ harness?: string;
67
+ /** create-or-resume(决策 2):不存在即新建,存在即续;缺省新建 */
68
+ sessionId?: string;
69
+ }
70
+ export type OpenSessionOptions = SessionCoreOptions & StartSessionOptions;
71
+ declare const SESSION_HANDLE_BRAND: unique symbol;
72
+ /** createSessionCore 的产物(不透明句柄):只读元数据 + core 子集;完整内容只有库内部知道 */
73
+ export interface SessionCoreHandle {
74
+ readonly [SESSION_HANDLE_BRAND]: true;
75
+ readonly core: SessionCoreApi;
76
+ readonly cwd: string;
77
+ readonly interactive: boolean;
78
+ readonly permissionMode: PermissionMode;
79
+ /** 构造阶段的备注(如 marketplace 项目域与首个 session 不一致) */
80
+ readonly notes: string[];
81
+ }
82
+ export interface BlockedItem {
83
+ kind: "permission" | "question" | "form";
84
+ /** 工具名 / 表单标题 */
85
+ name: string;
86
+ title?: string;
87
+ agentId?: string;
88
+ }
89
+ export interface SendResult {
90
+ /** 主代理最后一条完整回复(无则空串) */
91
+ text: string;
92
+ /** 本轮内所有主代理完整回复(多轮工具调用间可能有多条) */
93
+ texts: string[];
94
+ /** 无人值守下被拒绝的权限请求 / 被跳过的提问与表单 */
95
+ blocked: BlockedItem[];
96
+ usage?: Usage;
97
+ /** type:会话错误类型 | 'timeout' | 'aborted'(调用方 signal)| 'disposed'(执行中被 dispose) */
98
+ error?: {
99
+ type: string;
100
+ message: string;
101
+ };
102
+ }
103
+ export interface SendOptions {
104
+ timeoutMs?: number;
105
+ signal?: AbortSignal;
106
+ }
107
+ export interface AtomixSession<C extends SessionCoreApi = SessionCoreApi> {
108
+ readonly core: C;
109
+ readonly sessionId: string;
110
+ readonly cwd: string;
111
+ readonly harness: string;
112
+ readonly permissionMode: PermissionMode;
113
+ /** session:ready 原始数据(交互入口要 projectInputHistory) */
114
+ readonly ready: SessionReadyInfo;
115
+ /** harness 加载告警、agent 名单应用失败、MCP override 备注、构造期备注 */
116
+ readonly notes: string[];
117
+ /** 发送一条用户输入,等到本轮 idle 后返回。多次调用按序排队 */
118
+ send(input: string, opts?: SendOptions): Promise<SendResult>;
119
+ on<T>(event: string, listener: (data: T) => void): void;
120
+ off<T>(event: string, listener: (data: T) => void): void;
121
+ /** 立即结束:中断进行中的一轮(其 send 以 error.type='disposed' 返回),排队中的 send 拒绝,再释放 core */
122
+ dispose(): Promise<void>;
123
+ }
124
+ export type StreamEvent = {
125
+ type: "state";
126
+ state: string;
127
+ } | {
128
+ type: "text";
129
+ delta: string;
130
+ } | {
131
+ type: "thinking";
132
+ delta: string;
133
+ } | {
134
+ type: "message";
135
+ agentId: string;
136
+ content: string;
137
+ hasToolCalls: boolean;
138
+ } | {
139
+ type: "tool_complete";
140
+ agentId: string;
141
+ toolName: string;
142
+ title: string;
143
+ summary: string;
144
+ } | {
145
+ type: "tool_error";
146
+ agentId: string;
147
+ toolName: string;
148
+ title: string;
149
+ content: string;
150
+ } | {
151
+ type: "permission_request";
152
+ agentId: string;
153
+ toolName: string;
154
+ title: string;
155
+ } | {
156
+ type: "agent_start";
157
+ taskId: string;
158
+ subagentType: string;
159
+ description: string;
160
+ } | {
161
+ type: "agent_end";
162
+ taskId: string;
163
+ status: string;
164
+ content: string;
165
+ } | {
166
+ type: "usage";
167
+ agentId: string;
168
+ usage: Usage;
169
+ } | {
170
+ type: "compact";
171
+ phase: "start" | "done";
172
+ agentId: string;
173
+ messageCount?: number;
174
+ tokenBefore?: number;
175
+ tokenCompact?: number;
176
+ error?: string;
177
+ } | {
178
+ type: "interrupted";
179
+ agentId: string;
180
+ content: string;
181
+ } | {
182
+ type: "error";
183
+ errorType: string;
184
+ message: string;
185
+ };
186
+ /** 历史会话条目(resume.ts listSessions) */
187
+ export interface SessionEntry {
188
+ sessionId: string;
189
+ file: string;
190
+ date: string;
191
+ mtime: Date;
192
+ /** 首条用户消息摘要 */
193
+ preview: string;
194
+ }
195
+ export interface ServeOpenParams {
196
+ /** 缺省 = serve 启动时的 -C / 进程 cwd */
197
+ cwd?: string;
198
+ harness?: string;
199
+ permissionMode?: PermissionMode;
200
+ /** create-or-resume;同一 serve 进程内同一 id 只能打开一次 */
201
+ sessionId?: string;
202
+ systemPrompt?: string;
203
+ hooks?: boolean;
204
+ }
205
+ export type ServeRequest = {
206
+ id: string | number;
207
+ op: "session.open";
208
+ params: ServeOpenParams;
209
+ } | {
210
+ id: string | number;
211
+ op: "session.send";
212
+ params: {
213
+ session: string;
214
+ input: string;
215
+ timeoutMs?: number;
216
+ };
217
+ } | {
218
+ id: string | number;
219
+ op: "session.interrupt";
220
+ params: {
221
+ session: string;
222
+ };
223
+ }
224
+ /** close 排在该会话前面的 send 之后;force = 先中止进行中与排队中的 send(它们以 aborted 返回)再关 */
225
+ | {
226
+ id: string | number;
227
+ op: "session.close";
228
+ params: {
229
+ session: string;
230
+ force?: boolean;
231
+ };
232
+ } | {
233
+ id: string | number;
234
+ op: "session.list";
235
+ params?: Record<string, never>;
236
+ } | {
237
+ id: string | number;
238
+ op: "history.list";
239
+ params: {
240
+ cwd?: string;
241
+ };
242
+ } | {
243
+ id: string | number;
244
+ op: "harness.list";
245
+ params?: Record<string, never>;
246
+ } | {
247
+ id: string | number;
248
+ op: "server.ping";
249
+ params?: Record<string, never>;
250
+ } | {
251
+ id: string | number;
252
+ op: "server.shutdown";
253
+ params?: Record<string, never>;
254
+ };
255
+ export type ServeOp = ServeRequest["op"];
256
+ /** 本进程内一个活跃会话的摘要 */
257
+ export interface ServeSessionInfo {
258
+ sessionId: string;
259
+ cwd: string;
260
+ harness: string;
261
+ permissionMode: PermissionMode;
262
+ /** 进行中 + 排队中的 send 数 */
263
+ pending: number;
264
+ }
265
+ export interface ServeOpenResult extends ServeSessionInfo {
266
+ notes: string[];
267
+ historyLoaded: boolean;
268
+ usage: Usage;
269
+ }
270
+ export interface ServeSendResult extends SendResult {
271
+ sessionId: string;
272
+ }
273
+ export type ServeResult = ServeOpenResult | ServeSendResult | {
274
+ sessionId: string;
275
+ interrupted: number;
276
+ } | {
277
+ sessionId: string;
278
+ closed: true;
279
+ } | {
280
+ sessions: ServeSessionInfo[];
281
+ } | {
282
+ cwd: string;
283
+ sessions: Array<Omit<SessionEntry, "mtime"> & {
284
+ mtime: string;
285
+ }>;
286
+ } | {
287
+ harnesses: string[];
288
+ } | {
289
+ pong: true;
290
+ version: string;
291
+ sessions: number;
292
+ } | {
293
+ shutdown: true;
294
+ };
295
+ /**
296
+ * error.type:'protocol'(不是合法 JSON / 不是对象 / 缺 id(id 须为非空 string 或安全整数)/ id 在途重复 / 未知 op
297
+ * / 超单行上限 / 正在关闭)| 'args'(参数缺失或类型不对:boolean / number 严格按类型,cwd 必须是目录)
298
+ * | 'not_found'(session 不存在)| 'conflict'(session id 已打开)
299
+ * | 'limit'(活跃会话数 / 在途请求数超上限;shutdown / ping / interrupt / force close 不受在途上限约束)
300
+ * | 'init'(打开失败)| 'internal'
301
+ */
302
+ export type ServeResponse = {
303
+ type: "response";
304
+ id: string | number | null;
305
+ ok: true;
306
+ result: ServeResult;
307
+ } | {
308
+ type: "response";
309
+ id: string | number | null;
310
+ ok: false;
311
+ error: {
312
+ type: string;
313
+ message: string;
314
+ };
315
+ };
316
+ export interface ServeEventRecord {
317
+ type: "event";
318
+ session: string;
319
+ event: StreamEvent;
320
+ }
321
+ export interface ServeServerRecord {
322
+ type: "server";
323
+ status: "ready" | "shutdown";
324
+ version: string;
325
+ pid: number;
326
+ cwd: string;
327
+ }
328
+ export type ServeRecord = ServeResponse | ServeEventRecord | ServeServerRecord;
329
+ /** 一步到位:构造 + 装配(无人值守缺省)。模型未配置 / session id 非法 / harness 不存在直接抛错 */
330
+ export declare const openSession: (opts: OpenSessionOptions) => Promise<AtomixSession>;
331
+ /** 两段式第一步:只构造 core(交互入口在此之后跑模型向导)。返回不透明句柄 */
332
+ export declare const createSessionCore: (opts: SessionCoreOptions) => SessionCoreHandle;
333
+ /** 两段式第二步:harness → createSession → 启停名单 → MCP override。句柄必须来自 createSessionCore */
334
+ export declare const startSession: (handle: SessionCoreHandle, opts?: StartSessionOptions) => Promise<AtomixSession>;
335
+ /** 构造了 core 但不再启动时释放 */
336
+ export declare const discardSessionCore: (handle: SessionCoreHandle) => Promise<void>;
337
+ /** 模型是否已配置(未配置时 openSession 会抛错;两段式调用方可先查) */
338
+ export declare const isModelConfigured: (handle: SessionCoreHandle) => Promise<boolean>;
339
+ /** 进程级准备(openSession / createSessionCore 内部会调;显式调用可提前拿到项目域备注) */
340
+ export declare const prepareProcess: (cwd: string) => string | null;
341
+ /** session id 规则:1–128 个字母 / 数字 / 点 / 下划线 / 连字符;不合规抛错(CLI 与库共用) */
342
+ export declare const validateSessionId: (id: string) => void;
343
+ export declare const SESSION_ID_PATTERN: RegExp;
344
+ export declare const DEFAULT_SYSTEM_PROMPT: string;
345
+ export declare const DEFAULT_SEND_TIMEOUT_MS: number;
346
+ /** 订阅 core 事件并转成 stream-json 形状(与 `-p --output-format stream-json` / serve 同一套) */
347
+ export declare const attachStreamEvents: (core: SessionCoreApi, emit: (e: StreamEvent) => void) => () => void;
348
+ /** 列出某工作目录的历史会话(新 → 旧) */
349
+ export declare const listSessions: (cwd: string) => SessionEntry[];
350
+ /** 列出可用 harness(base 恒在首位) */
351
+ export declare const listHarnesses: () => string[];
352
+ export declare const BASE_HARNESS: string;
353
+ export declare const PERMISSION_MODE_ORDER: readonly PermissionMode[];
354
+ export declare const DEFAULT_PERMISSION_MODE: PermissionMode;
355
+ /** 解析权限模式参数:全名 / 唯一前缀 / 缩写;无法解析返回 null */
356
+ export declare const parsePermissionMode: (arg: string | undefined) => PermissionMode | null;
357
+ /** ~/.atomix(或 ATOMIX_ROOT) */
358
+ export declare const getAtomixRoot: () => string;
359
+
360
+ export {};