@huyooo/ai-chat-bridge-electron 0.1.6 → 0.2.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.
@@ -18,13 +18,15 @@ interface SendMessageParams {
18
18
  };
19
19
  }
20
20
  /** 模型配置 */
21
- interface ModelConfig {
22
- provider: string;
23
- model: string;
21
+ interface ModelOption {
22
+ modelId: string;
24
23
  displayName: string;
25
- supportsTools: boolean;
26
- supportsWebSearch: boolean;
27
- supportedThinkingModes: string[];
24
+ /** 分组名称(由后端决定,前端只负责渲染,必填) */
25
+ group: string;
26
+ /** 是否来自 OpenRouter(保留用于兼容,后续可能移除) */
27
+ isOpenRouter?: boolean;
28
+ /** 提供商名称(保留用于兼容,后续可能移除) */
29
+ provider?: string;
28
30
  }
29
31
  /** 会话记录 */
30
32
  interface SessionRecord {
@@ -34,6 +36,10 @@ interface SessionRecord {
34
36
  title: string;
35
37
  model: string;
36
38
  mode: 'agent' | 'plan' | 'ask';
39
+ webSearchEnabled: boolean;
40
+ thinkingEnabled: boolean;
41
+ /** 是否在 tab 栏隐藏(关闭但不删除) */
42
+ hidden: boolean;
37
43
  createdAt: Date;
38
44
  updatedAt: Date;
39
45
  }
@@ -43,9 +49,16 @@ interface MessageRecord {
43
49
  sessionId: string;
44
50
  role: 'user' | 'assistant';
45
51
  content: string;
46
- thinking?: string | null;
47
- toolCalls?: string | null;
48
- searchResults?: string | null;
52
+ /** 生成此消息时使用的模型 */
53
+ model?: string | null;
54
+ /** 生成此消息时使用的模式 (ask/agent) */
55
+ mode?: string | null;
56
+ /** 生成此消息时是否启用 web 搜索 */
57
+ webSearchEnabled?: boolean | null;
58
+ /** 生成此消息时是否启用深度思考 */
59
+ thinkingEnabled?: boolean | null;
60
+ /** 执行步骤列表 JSON */
61
+ steps?: string | null;
49
62
  operationIds?: string | null;
50
63
  timestamp: Date;
51
64
  }
@@ -69,19 +82,24 @@ interface TrashRecord {
69
82
  deletedAt: Date;
70
83
  autoDeleteAt: Date;
71
84
  }
85
+ /** 文件信息 */
86
+ interface FileInfo {
87
+ name: string;
88
+ path: string;
89
+ isDirectory: boolean;
90
+ size: number;
91
+ modifiedAt: Date;
92
+ extension: string;
93
+ }
72
94
  interface AiChatBridge {
73
95
  /** 获取可用模型 */
74
- getModels(): Promise<ModelConfig[]>;
96
+ getModels(): Promise<ModelOption[]>;
75
97
  /** 发送消息 */
76
98
  send(params: SendMessageParams): Promise<void>;
77
99
  /** 取消当前请求 */
78
100
  cancel(): Promise<void>;
79
- /** 清空 Agent 内存历史 */
80
- clearAgentHistory(): Promise<void>;
81
- /** 获取 Agent 内存历史 */
82
- getAgentHistory(): Promise<unknown[]>;
83
- /** 设置工作目录 */
84
- setWorkingDir(dir: string): Promise<void>;
101
+ /** 设置当前工作目录 */
102
+ setCwd(dir: string): Promise<void>;
85
103
  /** 获取配置 */
86
104
  getConfig(): Promise<unknown>;
87
105
  /** 监听进度事件 */
@@ -95,12 +113,18 @@ interface AiChatBridge {
95
113
  title?: string;
96
114
  model?: string;
97
115
  mode?: string;
116
+ webSearchEnabled?: boolean;
117
+ thinkingEnabled?: boolean;
118
+ hidden?: boolean;
98
119
  }): Promise<SessionRecord>;
99
120
  /** 更新会话 */
100
121
  updateSession(id: string, data: {
101
122
  title?: string;
102
123
  model?: string;
103
124
  mode?: string;
125
+ webSearchEnabled?: boolean;
126
+ thinkingEnabled?: boolean;
127
+ hidden?: boolean;
104
128
  }): Promise<SessionRecord | null>;
105
129
  /** 删除会话 */
106
130
  deleteSession(id: string): Promise<{
@@ -110,14 +134,31 @@ interface AiChatBridge {
110
134
  getMessages(sessionId: string): Promise<MessageRecord[]>;
111
135
  /** 保存消息 */
112
136
  saveMessage(params: {
137
+ /** 消息 ID(可选,如果不传则自动生成) */
138
+ id?: string;
113
139
  sessionId: string;
114
140
  role: 'user' | 'assistant';
115
141
  content: string;
116
- thinking?: string;
117
- toolCalls?: string;
118
- searchResults?: string;
142
+ /** 生成此消息时使用的模型 */
143
+ model?: string;
144
+ /** 生成此消息时使用的模式 (ask/agent) */
145
+ mode?: string;
146
+ /** 生成此消息时是否启用 web 搜索 */
147
+ webSearchEnabled?: boolean;
148
+ /** 生成此消息时是否启用深度思考 */
149
+ thinkingEnabled?: boolean;
150
+ /** 执行步骤列表 JSON */
151
+ steps?: string;
119
152
  operationIds?: string;
120
153
  }): Promise<MessageRecord>;
154
+ /** 更新消息(增量保存) */
155
+ updateMessage(params: {
156
+ id: string;
157
+ content?: string;
158
+ steps?: string;
159
+ }): Promise<{
160
+ success: boolean;
161
+ }>;
121
162
  /** 删除指定时间之后的消息(用于分叉) */
122
163
  deleteMessagesAfter(sessionId: string, timestamp: number): Promise<{
123
164
  success: boolean;
@@ -128,6 +169,98 @@ interface AiChatBridge {
128
169
  getTrashItems(): Promise<TrashRecord[]>;
129
170
  /** 恢复回收站项目 */
130
171
  restoreFromTrash(id: string): Promise<TrashRecord | undefined>;
172
+ /** 在系统默认浏览器中打开链接 */
173
+ openExternal(url: string): Promise<void>;
174
+ /** 列出目录内容 */
175
+ listDir(dirPath: string): Promise<FileInfo[]>;
176
+ /** 检查路径是否存在 */
177
+ exists(filePath: string): Promise<boolean>;
178
+ /** 获取文件信息 */
179
+ stat(filePath: string): Promise<FileInfo | null>;
180
+ /** 读取文件内容(文本) */
181
+ readFile(filePath: string): Promise<string | null>;
182
+ /** 读取文件为 base64 */
183
+ readFileBase64(filePath: string): Promise<string | null>;
184
+ /** 获取用户主目录 */
185
+ homeDir(): Promise<string>;
186
+ /** 解析路径(处理 ~) */
187
+ resolvePath(inputPath: string): Promise<string>;
188
+ /** 获取父目录 */
189
+ parentDir(dirPath: string): Promise<string>;
190
+ /** 监听目录变化 */
191
+ watchDir(dirPath: string): Promise<boolean>;
192
+ /** 停止监听目录 */
193
+ unwatchDir(dirPath: string): Promise<void>;
194
+ /** 监听目录变化事件 */
195
+ onDirChange(callback: (data: {
196
+ dirPath: string;
197
+ eventType: string;
198
+ filename: string | null;
199
+ }) => void): () => void;
200
+ /** 获取用户设置 */
201
+ getSetting(key: string): Promise<string | null>;
202
+ /** 设置用户设置 */
203
+ setSetting(key: string, value: string): Promise<{
204
+ success: boolean;
205
+ }>;
206
+ /** 获取所有用户设置 */
207
+ getAllSettings(): Promise<Record<string, string>>;
208
+ /** 删除用户设置 */
209
+ deleteSetting(key: string): Promise<{
210
+ success: boolean;
211
+ }>;
212
+ /** 获取索引统计信息 */
213
+ getIndexStats(): Promise<{
214
+ totalDocuments: number;
215
+ indexSize: number;
216
+ lastUpdated: string | null;
217
+ }>;
218
+ /** 检查索引状态 */
219
+ getIndexStatus(): Promise<{
220
+ isIndexing: boolean;
221
+ lastProgress: {
222
+ indexed: number;
223
+ total: number;
224
+ currentFile?: string;
225
+ stage: string;
226
+ } | null;
227
+ }>;
228
+ /** 同步索引(重新索引工作空间) */
229
+ syncIndex(): Promise<{
230
+ success: boolean;
231
+ }>;
232
+ /** 取消索引 */
233
+ cancelIndex(): Promise<{
234
+ success: boolean;
235
+ }>;
236
+ /** 删除索引 */
237
+ deleteIndex(): Promise<{
238
+ success: boolean;
239
+ }>;
240
+ /** 注册索引进度监听器 */
241
+ registerIndexListener(): Promise<{
242
+ success: boolean;
243
+ }>;
244
+ /** 注销索引进度监听器 */
245
+ unregisterIndexListener(): Promise<{
246
+ success: boolean;
247
+ }>;
248
+ /** 监听索引进度 */
249
+ onIndexProgress(callback: (progress: {
250
+ indexed: number;
251
+ total: number;
252
+ currentFile?: string;
253
+ stage: string;
254
+ error?: string;
255
+ }) => void): () => void;
256
+ /** 监听工具批准请求(manual 模式) */
257
+ onToolApprovalRequest(callback: (request: {
258
+ id: string;
259
+ name: string;
260
+ args: Record<string, unknown>;
261
+ }) => void): () => void;
262
+ /** 响应工具批准请求 */
263
+ respondToolApproval(id: string, approved: boolean): Promise<void>;
131
264
  }
132
265
  interface ExposeOptions {
133
266
  /** IPC channel 前缀 */
@@ -7,9 +7,8 @@ function exposeElectronBridge(options = {}) {
7
7
  getModels: () => ipcRenderer.invoke(`${channelPrefix}:models`),
8
8
  send: (params) => ipcRenderer.invoke(`${channelPrefix}:send`, params),
9
9
  cancel: () => ipcRenderer.invoke(`${channelPrefix}:cancel`),
10
- clearAgentHistory: () => ipcRenderer.invoke(`${channelPrefix}:clearAgentHistory`),
11
- getAgentHistory: () => ipcRenderer.invoke(`${channelPrefix}:agentHistory`),
12
- setWorkingDir: (dir) => ipcRenderer.invoke(`${channelPrefix}:setWorkingDir`, dir),
10
+ // 无状态架构:历史通过 ChatOptions.history 传入
11
+ setCwd: (dir) => ipcRenderer.invoke(`${channelPrefix}:setCwd`, dir),
13
12
  getConfig: () => ipcRenderer.invoke(`${channelPrefix}:config`),
14
13
  onProgress: (callback) => {
15
14
  const handler = (_event, progress) => {
@@ -20,6 +19,16 @@ function exposeElectronBridge(options = {}) {
20
19
  ipcRenderer.removeListener(`${channelPrefix}:progress`, handler);
21
20
  };
22
21
  },
22
+ onToolApprovalRequest: (callback) => {
23
+ const handler = (_event, request) => {
24
+ callback(request);
25
+ };
26
+ ipcRenderer.on(`${channelPrefix}:toolApprovalRequest`, handler);
27
+ return () => {
28
+ ipcRenderer.removeListener(`${channelPrefix}:toolApprovalRequest`, handler);
29
+ };
30
+ },
31
+ respondToolApproval: (id, approved) => ipcRenderer.invoke(`${channelPrefix}:toolApprovalResponse`, { id, approved }),
23
32
  // ============ Sessions API ============
24
33
  getSessions: () => ipcRenderer.invoke(`${channelPrefix}:sessions:list`),
25
34
  getSession: (id) => ipcRenderer.invoke(`${channelPrefix}:sessions:get`, id),
@@ -29,12 +38,57 @@ function exposeElectronBridge(options = {}) {
29
38
  // ============ Messages API ============
30
39
  getMessages: (sessionId) => ipcRenderer.invoke(`${channelPrefix}:messages:list`, sessionId),
31
40
  saveMessage: (params) => ipcRenderer.invoke(`${channelPrefix}:messages:save`, params),
41
+ updateMessage: (params) => ipcRenderer.invoke(`${channelPrefix}:messages:update`, params),
32
42
  deleteMessagesAfter: (sessionId, timestamp) => ipcRenderer.invoke(`${channelPrefix}:messages:deleteAfter`, sessionId, timestamp),
33
43
  // ============ Operations API ============
34
44
  getOperations: (sessionId) => ipcRenderer.invoke(`${channelPrefix}:operations:list`, sessionId),
35
45
  // ============ Trash API ============
36
46
  getTrashItems: () => ipcRenderer.invoke(`${channelPrefix}:trash:list`),
37
- restoreFromTrash: (id) => ipcRenderer.invoke(`${channelPrefix}:trash:restore`, id)
47
+ restoreFromTrash: (id) => ipcRenderer.invoke(`${channelPrefix}:trash:restore`, id),
48
+ // ============ System API ============
49
+ openExternal: (url) => ipcRenderer.invoke(`${channelPrefix}:openExternal`, url),
50
+ // ============ File System API ============
51
+ listDir: (dirPath) => ipcRenderer.invoke(`${channelPrefix}:fs:listDir`, dirPath),
52
+ exists: (filePath) => ipcRenderer.invoke(`${channelPrefix}:fs:exists`, filePath),
53
+ stat: (filePath) => ipcRenderer.invoke(`${channelPrefix}:fs:stat`, filePath),
54
+ readFile: (filePath) => ipcRenderer.invoke(`${channelPrefix}:fs:readFile`, filePath),
55
+ readFileBase64: (filePath) => ipcRenderer.invoke(`${channelPrefix}:fs:readFileBase64`, filePath),
56
+ homeDir: () => ipcRenderer.invoke(`${channelPrefix}:fs:homeDir`),
57
+ resolvePath: (inputPath) => ipcRenderer.invoke(`${channelPrefix}:fs:resolvePath`, inputPath),
58
+ parentDir: (dirPath) => ipcRenderer.invoke(`${channelPrefix}:fs:parentDir`, dirPath),
59
+ watchDir: (dirPath) => ipcRenderer.invoke(`${channelPrefix}:fs:watchDir`, dirPath),
60
+ unwatchDir: (dirPath) => ipcRenderer.invoke(`${channelPrefix}:fs:unwatchDir`, dirPath),
61
+ onDirChange: (callback) => {
62
+ const handler = (_event, data) => {
63
+ callback(data);
64
+ };
65
+ ipcRenderer.on(`${channelPrefix}:fs:dirChange`, handler);
66
+ return () => {
67
+ ipcRenderer.removeListener(`${channelPrefix}:fs:dirChange`, handler);
68
+ };
69
+ },
70
+ // ============ Settings API ============
71
+ getSetting: (key) => ipcRenderer.invoke(`${channelPrefix}:settings:get`, key),
72
+ setSetting: (key, value) => ipcRenderer.invoke(`${channelPrefix}:settings:set`, key, value),
73
+ getAllSettings: () => ipcRenderer.invoke(`${channelPrefix}:settings:getAll`),
74
+ deleteSetting: (key) => ipcRenderer.invoke(`${channelPrefix}:settings:delete`, key),
75
+ // ============ Index API ============
76
+ getIndexStats: () => ipcRenderer.invoke(`${channelPrefix}:index:getStats`),
77
+ getIndexStatus: () => ipcRenderer.invoke(`${channelPrefix}:index:status`),
78
+ syncIndex: () => ipcRenderer.invoke(`${channelPrefix}:index:sync`),
79
+ cancelIndex: () => ipcRenderer.invoke(`${channelPrefix}:index:cancel`),
80
+ deleteIndex: () => ipcRenderer.invoke(`${channelPrefix}:index:delete`),
81
+ registerIndexListener: () => ipcRenderer.invoke(`${channelPrefix}:index:registerListener`),
82
+ unregisterIndexListener: () => ipcRenderer.invoke(`${channelPrefix}:index:unregisterListener`),
83
+ onIndexProgress: (callback) => {
84
+ const handler = (_event, progress) => {
85
+ callback(progress);
86
+ };
87
+ ipcRenderer.on(`${channelPrefix}:index:progress`, handler);
88
+ return () => {
89
+ ipcRenderer.removeListener(`${channelPrefix}:index:progress`, handler);
90
+ };
91
+ }
38
92
  };
39
93
  contextBridge.exposeInMainWorld(exposeName, bridge);
40
94
  }
@@ -36,26 +36,25 @@ function createElectronAdapter(options = {}) {
36
36
  // ============ Chat API ============
37
37
  async getModels() {
38
38
  const bridge = getBridge();
39
- const models = await bridge.getModels();
40
- return models.map((m) => ({
41
- ...m,
42
- provider: m.provider,
43
- supportedThinkingModes: m.supportedThinkingModes
44
- }));
45
- },
46
- async *sendMessage(message, options2, images) {
39
+ return bridge.getModels();
40
+ },
41
+ async *sendMessage(message, options2, images, sessionId) {
47
42
  const bridge = getBridge();
48
43
  const queue = [];
49
44
  let resolveNext = null;
50
45
  let done = false;
51
46
  const cleanup = bridge.onProgress((progress) => {
47
+ const eventWithSession = progress;
48
+ if (sessionId && eventWithSession.sessionId && eventWithSession.sessionId !== sessionId) {
49
+ return;
50
+ }
52
51
  queue.push(progress);
53
52
  resolveNext?.();
54
53
  if (progress.type === "done" || progress.type === "error") {
55
54
  done = true;
56
55
  }
57
56
  });
58
- bridge.send({ message, images, options: options2 });
57
+ bridge.send({ message, images, options: options2, sessionId });
59
58
  try {
60
59
  while (!done || queue.length > 0) {
61
60
  while (queue.length === 0 && !done) {
@@ -76,14 +75,9 @@ function createElectronAdapter(options = {}) {
76
75
  cancel() {
77
76
  getBridge().cancel();
78
77
  },
79
- clearAgentHistory() {
80
- getBridge().clearAgentHistory();
81
- },
82
- async getAgentHistory() {
83
- return getBridge().getAgentHistory();
84
- },
85
- setWorkingDir(dir) {
86
- getBridge().setWorkingDir(dir);
78
+ // 无状态架构:历史通过 ChatOptions.history 传入
79
+ setCwd(dir) {
80
+ getBridge().setCwd(dir);
87
81
  },
88
82
  // ============ Sessions API ============
89
83
  async getSessions() {
@@ -108,6 +102,9 @@ function createElectronAdapter(options = {}) {
108
102
  async saveMessage(params) {
109
103
  return getBridge().saveMessage(params);
110
104
  },
105
+ async updateMessage(params) {
106
+ await getBridge().updateMessage(params);
107
+ },
111
108
  async deleteMessagesAfter(sessionId, timestamp) {
112
109
  await getBridge().deleteMessagesAfter(sessionId, timestamp);
113
110
  },
@@ -121,6 +118,82 @@ function createElectronAdapter(options = {}) {
121
118
  },
122
119
  async restoreFromTrash(id) {
123
120
  return getBridge().restoreFromTrash(id);
121
+ },
122
+ // ============ File System API ============
123
+ async listDir(dirPath) {
124
+ return getBridge().listDir(dirPath);
125
+ },
126
+ async exists(filePath) {
127
+ return getBridge().exists(filePath);
128
+ },
129
+ async stat(filePath) {
130
+ return getBridge().stat(filePath);
131
+ },
132
+ async readFile(filePath) {
133
+ return getBridge().readFile(filePath);
134
+ },
135
+ async readFileBase64(filePath) {
136
+ return getBridge().readFileBase64(filePath);
137
+ },
138
+ async homeDir() {
139
+ return getBridge().homeDir();
140
+ },
141
+ async resolvePath(inputPath) {
142
+ return getBridge().resolvePath(inputPath);
143
+ },
144
+ async parentDir(dirPath) {
145
+ return getBridge().parentDir(dirPath);
146
+ },
147
+ async watchDir(dirPath) {
148
+ return getBridge().watchDir(dirPath);
149
+ },
150
+ async unwatchDir(dirPath) {
151
+ return getBridge().unwatchDir(dirPath);
152
+ },
153
+ onDirChange(callback) {
154
+ return getBridge().onDirChange(callback);
155
+ },
156
+ // ============ Settings API ============
157
+ async getSetting(key) {
158
+ const bridge = getBridge();
159
+ if (bridge.getSetting) {
160
+ return bridge.getSetting(key);
161
+ }
162
+ return null;
163
+ },
164
+ async setSetting(key, value) {
165
+ const bridge = getBridge();
166
+ if (bridge.setSetting) {
167
+ await bridge.setSetting(key, value);
168
+ }
169
+ },
170
+ async getAllSettings() {
171
+ const bridge = getBridge();
172
+ if (bridge.getAllSettings) {
173
+ return bridge.getAllSettings();
174
+ }
175
+ return {};
176
+ },
177
+ async deleteSetting(key) {
178
+ const bridge = getBridge();
179
+ if (bridge.deleteSetting) {
180
+ await bridge.deleteSetting(key);
181
+ }
182
+ },
183
+ // ============ Tool Approval API ============
184
+ onToolApprovalRequest(callback) {
185
+ const bridge = getBridge();
186
+ if (bridge.onToolApprovalRequest) {
187
+ return bridge.onToolApprovalRequest(callback);
188
+ }
189
+ return () => {
190
+ };
191
+ },
192
+ async respondToolApproval(id, approved) {
193
+ const bridge = getBridge();
194
+ if (bridge.respondToolApproval) {
195
+ return bridge.respondToolApproval(id, approved);
196
+ }
124
197
  }
125
198
  };
126
199
  }