@keyou007/agent-bot 0.1.20-alpha.1 → 0.1.20

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.
Files changed (63) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +13 -0
  3. package/README.zh.md +9 -0
  4. package/dist/cli/AgentPrerequisites.js +11 -47
  5. package/dist/cli/AgentPrerequisites.js.map +1 -1
  6. package/dist/cli/DefaultAgentSelection.js +1 -1
  7. package/dist/cli/DefaultAgentSelection.js.map +1 -1
  8. package/dist/cli.js +15 -0
  9. package/dist/cli.js.map +1 -1
  10. package/dist/codex/CodexLocalActivityDetector.js +32 -10
  11. package/dist/codex/CodexLocalActivityDetector.js.map +1 -1
  12. package/dist/codex/CodexProcessManager.js +53 -7
  13. package/dist/codex/CodexProcessManager.js.map +1 -1
  14. package/dist/codex/CodexRuntime.js +225 -164
  15. package/dist/codex/CodexRuntime.js.map +1 -1
  16. package/dist/codex/CodexVersion.js +18 -0
  17. package/dist/codex/CodexVersion.js.map +1 -0
  18. package/dist/codex/ProviderSwitch.js +32 -0
  19. package/dist/codex/ProviderSwitch.js.map +1 -0
  20. package/dist/feishu/CardRenderer.js +36 -11
  21. package/dist/feishu/CardRenderer.js.map +1 -1
  22. package/dist/feishu/FeishuConnector.js +17 -5
  23. package/dist/feishu/FeishuConnector.js.map +1 -1
  24. package/dist/feishu/InboundText.js +11 -0
  25. package/dist/feishu/InboundText.js.map +1 -1
  26. package/dist/feishu/MergedForwardMessage.js +17 -5
  27. package/dist/feishu/MergedForwardMessage.js.map +1 -1
  28. package/dist/index.js +1 -1
  29. package/dist/index.js.map +1 -1
  30. package/dist/local-files/LocalFileViewerServer.js +14 -4
  31. package/dist/local-files/LocalFileViewerServer.js.map +1 -1
  32. package/dist/local-files/MarkdownFileLinks.js +65 -0
  33. package/dist/local-files/MarkdownFileLinks.js.map +1 -0
  34. package/dist/proxy/ProxySessionController.js +399 -257
  35. package/dist/proxy/ProxySessionController.js.map +1 -1
  36. package/dist/state/StateStore.js +58 -65
  37. package/dist/state/StateStore.js.map +1 -1
  38. package/dist/utils/semanticVersion.js +39 -0
  39. package/dist/utils/semanticVersion.js.map +1 -0
  40. package/docs/technical-reference.md +5 -3
  41. package/docs/technical-reference.zh.md +5 -3
  42. package/npm-shrinkwrap.json +2 -2
  43. package/package.json +1 -1
  44. package/skills/agent-bot/SKILL.md +7 -1
  45. package/src/cli/AgentPrerequisites.ts +14 -40
  46. package/src/cli/DefaultAgentSelection.ts +1 -1
  47. package/src/cli.ts +13 -0
  48. package/src/codex/CodexLocalActivityDetector.ts +41 -9
  49. package/src/codex/CodexProcessManager.ts +44 -7
  50. package/src/codex/CodexRuntime.ts +244 -195
  51. package/src/codex/CodexVersion.ts +18 -0
  52. package/src/codex/ProviderSwitch.ts +36 -0
  53. package/src/feishu/CardRenderer.ts +37 -27
  54. package/src/feishu/FeishuConnector.ts +15 -5
  55. package/src/feishu/InboundText.ts +11 -0
  56. package/src/feishu/MergedForwardMessage.ts +15 -5
  57. package/src/index.ts +1 -1
  58. package/src/local-files/LocalFileViewerServer.ts +14 -4
  59. package/src/local-files/MarkdownFileLinks.ts +64 -0
  60. package/src/proxy/ProxySessionController.ts +369 -238
  61. package/src/runtime/types.ts +15 -2
  62. package/src/state/StateStore.ts +77 -90
  63. package/src/utils/semanticVersion.ts +28 -0
@@ -113,6 +113,11 @@ export interface RemoteCompletedTurnSummary {
113
113
  completedAt?: number;
114
114
  }
115
115
 
116
+ export interface RemoteTurnPage {
117
+ turns: RemoteCompletedTurnSummary[];
118
+ nextCursor?: string;
119
+ }
120
+
116
121
  export interface RemoteSessionSummary {
117
122
  id: string;
118
123
  title?: string;
@@ -163,6 +168,8 @@ export interface RuntimeSession {
163
168
  reasoningEffort?: string;
164
169
  permissionMode: PermissionMode;
165
170
  activeTurnId?: string;
171
+ // Returned by reconciliation so status callers can reuse the same observation.
172
+ remoteSummary?: RemoteSessionSummary;
166
173
  }
167
174
 
168
175
  export interface CreateRuntimeSessionInput {
@@ -229,8 +236,9 @@ export interface AgentRuntime {
229
236
  getSession(localSessionId: string): RuntimeSession | undefined;
230
237
  readSessionMetadata(remoteSessionId: string): Promise<RuntimeSessionMetadata>;
231
238
  listRemoteSessions?(input?: { searchTerm?: string; cursor?: string; limit?: number }): Promise<RemoteSessionPage>;
232
- readRemoteSession?(remoteSessionId: string): Promise<RemoteSessionSummary>;
239
+ readRemoteSession?(remoteSessionId: string, view?: "metadata" | "latest" | "latest-full"): Promise<RemoteSessionSummary>;
233
240
  readRemoteForkSource?(remoteSessionId: string): Promise<RemoteSessionSummary>;
241
+ listRemoteTurnSummaries?(remoteSessionId: string, input: { cursor?: string; limit: number }): Promise<RemoteTurnPage>;
234
242
  inspectRemoteSessionActivity?(remoteSessionId: string): Promise<RemoteSessionActivity>;
235
243
  synchronizeSession(sessionId: string): Promise<RuntimeSession>;
236
244
  startTurn(sessionId: string, prompt: RuntimePrompt): Promise<string>;
@@ -246,7 +254,12 @@ export interface AgentRuntime {
246
254
  setModel(sessionId: string, model: string): Promise<void>;
247
255
  setReasoningEffort(sessionId: string, effort: string): Promise<void>;
248
256
  setPermissionMode(sessionId: string, mode: PermissionMode): Promise<void>;
249
- setExecutionSettings?(sessionId: string, settings: RuntimeExecutionSettings): Promise<RuntimeSession>;
257
+ setExecutionSettings?(
258
+ sessionId: string,
259
+ settings: RuntimeExecutionSettings,
260
+ // Called after remote verification, before releasing the task's operation lock.
261
+ persist?: (session: RuntimeSession) => Promise<void>,
262
+ ): Promise<RuntimeSession>;
250
263
  respondToApproval(sessionId: string, requestId: string, decision: ApprovalDecision): Promise<void>;
251
264
  listModels(): Promise<ModelOption[]>;
252
265
  listModelProviders?(): Promise<ModelProviderOption[]>;
@@ -95,6 +95,21 @@ export interface CompletedTurnSnapshotRecord {
95
95
  updatedAt: string;
96
96
  }
97
97
 
98
+ export interface CompletedTurnIndexRecord {
99
+ turnId: string;
100
+ parentTurnId?: string;
101
+ timestamp: number;
102
+ updatedAt: string;
103
+ }
104
+
105
+ export interface TurnPromptSummary {
106
+ status?: string;
107
+ prompt?: string;
108
+ taskTitle?: string;
109
+ startedAt?: number;
110
+ completedAt?: number;
111
+ }
112
+
98
113
  export interface ImportedCompletedTurnRecord {
99
114
  turnId: string;
100
115
  snapshot: unknown;
@@ -1032,6 +1047,21 @@ export class StateStore {
1032
1047
  return row ? JSON.parse(row.snapshot_json) : undefined;
1033
1048
  }
1034
1049
 
1050
+ getTurnPromptSummary(turnId: string): TurnPromptSummary | undefined {
1051
+ const row = this.db.prepare(`SELECT
1052
+ json_extract(snapshot_json, '$.status') AS status,
1053
+ json_extract(snapshot_json, '$.prompt') AS prompt,
1054
+ json_extract(snapshot_json, '$.taskTitle') AS taskTitle,
1055
+ json_extract(snapshot_json, '$.startedAt') AS startedAt,
1056
+ json_extract(snapshot_json, '$.completedAt') AS completedAt
1057
+ FROM turn_snapshots WHERE turn_id = ?`).get(turnId) as TurnPromptSummary | undefined;
1058
+ return row ? {
1059
+ status: row.status ?? undefined, prompt: row.prompt ?? undefined,
1060
+ taskTitle: row.taskTitle ?? undefined, startedAt: row.startedAt ?? undefined,
1061
+ completedAt: row.completedAt ?? undefined,
1062
+ } : undefined;
1063
+ }
1064
+
1035
1065
  findLatestTurnSnapshotForSession(localSessionId: string): PersistedTurnSnapshotRecord | undefined {
1036
1066
  const row = this.db.prepare(`
1037
1067
  SELECT turn_id, local_session_id, context_key, snapshot_json, updated_at
@@ -1096,16 +1126,16 @@ export class StateStore {
1096
1126
  : undefined;
1097
1127
  }
1098
1128
 
1099
- saveTurnParent(turnId: string, localSessionId: string, parentTurnId?: string): void {
1129
+ saveTurnParent(turnId: string, localSessionId: string, parentTurnId?: string, replaceExisting = false): void {
1100
1130
  this.db.prepare(`
1101
1131
  INSERT INTO turn_parent_links (
1102
1132
  turn_id, local_session_id, parent_turn_id, created_at
1103
1133
  ) VALUES (?, ?, ?, ?)
1104
1134
  ON CONFLICT(turn_id) DO UPDATE SET
1105
1135
  parent_turn_id = excluded.parent_turn_id
1106
- WHERE turn_parent_links.parent_turn_id IS NULL
1136
+ WHERE (turn_parent_links.parent_turn_id IS NULL OR ?)
1107
1137
  AND excluded.parent_turn_id IS NOT NULL
1108
- `).run(turnId, localSessionId, parentTurnId ?? null, new Date().toISOString());
1138
+ `).run(turnId, localSessionId, parentTurnId ?? null, new Date().toISOString(), Number(replaceExisting));
1109
1139
  }
1110
1140
 
1111
1141
  importCompletedTurnHistory(input: {
@@ -1114,6 +1144,7 @@ export class StateStore {
1114
1144
  agentName: string;
1115
1145
  remoteSessionId: string;
1116
1146
  turns: ImportedCompletedTurnRecord[];
1147
+ replaceParents?: boolean;
1117
1148
  }): void {
1118
1149
  const importHistory = this.db.transaction(() => {
1119
1150
  let parentTurnId: string | undefined;
@@ -1140,7 +1171,7 @@ export class StateStore {
1140
1171
  input.remoteSessionId,
1141
1172
  turn.updatedAt,
1142
1173
  );
1143
- this.saveTurnParent(turn.turnId, input.localSessionId, parentTurnId);
1174
+ this.saveTurnParent(turn.turnId, input.localSessionId, parentTurnId, input.replaceParents);
1144
1175
  parentTurnId = turn.turnId;
1145
1176
  }
1146
1177
  });
@@ -1156,13 +1187,13 @@ export class StateStore {
1156
1187
  return row?.parent_turn_id ?? undefined;
1157
1188
  }
1158
1189
 
1159
- findLatestCompletedTurnId(localSessionId: string, contextKey: string): string | undefined {
1190
+ findLatestCompletedTurnId(localSessionId: string, contextKey?: string): string | undefined {
1160
1191
  const row = this.db
1161
1192
  .prepare(`
1162
1193
  SELECT turn_id
1163
1194
  FROM turn_snapshots
1164
1195
  WHERE local_session_id = ?
1165
- AND context_key = ?
1196
+ AND (? IS NULL OR context_key = ?)
1166
1197
  AND json_extract(snapshot_json, '$.status') = 'completed'
1167
1198
  ORDER BY
1168
1199
  coalesce(
@@ -1173,7 +1204,7 @@ export class StateStore {
1173
1204
  updated_at DESC
1174
1205
  LIMIT 1
1175
1206
  `)
1176
- .get(localSessionId, contextKey) as { turn_id: string } | undefined;
1207
+ .get(localSessionId, contextKey ?? null, contextKey ?? null) as { turn_id: string } | undefined;
1177
1208
  return row?.turn_id;
1178
1209
  }
1179
1210
 
@@ -1296,23 +1327,51 @@ export class StateStore {
1296
1327
  }
1297
1328
 
1298
1329
  listTaskTurnGraph(localSessionId: string): CompletedTurnSnapshotRecord[] {
1299
- this.backfillTurnParents(localSessionId);
1300
- const records = new Map(
1301
- this.listCompletedTurnGraphRows(localSessionId).map((record) => [record.turnId, record]),
1302
- );
1330
+ return this.listTaskTurnGraphIndex(localSessionId).map((record) => ({
1331
+ ...record, snapshot: this.getTurnSnapshot(record.turnId),
1332
+ }));
1333
+ }
1334
+
1335
+ listTaskTurnGraphIndex(localSessionId: string): CompletedTurnIndexRecord[] {
1336
+ const backfilled = new Set<string>();
1337
+ const backfill = (id: string): void => {
1338
+ if (backfilled.has(id)) return;
1339
+ this.backfillTurnParents(id);
1340
+ backfilled.add(id);
1341
+ };
1342
+ backfill(localSessionId);
1343
+ const projection = `SELECT ts.turn_id AS turnId, tpl.parent_turn_id AS parentTurnId,
1344
+ ts.local_session_id AS localSessionId, ts.updated_at AS updatedAt,
1345
+ coalesce(json_extract(ts.snapshot_json, '$.completedAt'),
1346
+ json_extract(ts.snapshot_json, '$.startedAt'),
1347
+ cast(strftime('%s', ts.updated_at) AS INTEGER) * 1000, 0) AS timestamp
1348
+ FROM turn_snapshots ts LEFT JOIN turn_parent_links tpl ON tpl.turn_id = ts.turn_id
1349
+ WHERE json_extract(ts.snapshot_json, '$.status') = 'completed'`;
1350
+ type Row = CompletedTurnIndexRecord & { localSessionId: string; parentTurnId: string | undefined };
1351
+ const normalize = (row: Row): CompletedTurnIndexRecord => ({
1352
+ turnId: row.turnId, parentTurnId: row.parentTurnId ?? undefined,
1353
+ timestamp: row.timestamp, updatedAt: row.updatedAt,
1354
+ });
1355
+ const own = this.db.prepare(`${projection} AND ts.local_session_id = ?`).all(localSessionId) as Row[];
1356
+ const read = this.db.prepare(`${projection} AND ts.turn_id = ?`);
1357
+ const records = new Map(own.map((row) => [row.turnId, normalize(row)]));
1303
1358
  let ancestorTurnId = this.findForkSourceTurnId(localSessionId)
1304
1359
  ?? this.findCrossSessionParentTurnId(localSessionId);
1305
1360
  const visited = new Set<string>();
1306
1361
 
1307
1362
  while (ancestorTurnId && !visited.has(ancestorTurnId)) {
1308
1363
  visited.add(ancestorTurnId);
1309
- const ancestor = this.getCompletedTurnGraphRecord(ancestorTurnId);
1364
+ let ancestor = read.get(ancestorTurnId) as Row | undefined;
1310
1365
  if (!ancestor) break;
1311
- records.set(ancestor.turnId, ancestor);
1366
+ if (!backfilled.has(ancestor.localSessionId)) {
1367
+ backfill(ancestor.localSessionId);
1368
+ ancestor = read.get(ancestorTurnId) as Row;
1369
+ }
1370
+ records.set(ancestor.turnId, normalize(ancestor));
1312
1371
  ancestorTurnId = ancestor.parentTurnId;
1313
1372
  }
1314
1373
 
1315
- return [...records.values()].sort(compareCompletedTurnSnapshots);
1374
+ return [...records.values()].sort((a, b) => b.timestamp - a.timestamp || b.updatedAt.localeCompare(a.updatedAt));
1316
1375
  }
1317
1376
 
1318
1377
  getForkHistorySource(localSessionId: string): ForkHistorySourceRecord | undefined {
@@ -1340,54 +1399,16 @@ export class StateStore {
1340
1399
  };
1341
1400
  }
1342
1401
 
1343
- hasImportedForkTurnHistory(localSessionId: string): boolean {
1402
+ hasImportedForkTurnHistory(localSessionId: string, sourceRemoteSessionId?: string, sourceTurnId?: string): boolean {
1344
1403
  return Boolean(this.db.prepare(`
1345
1404
  SELECT 1
1346
1405
  FROM audit_events
1347
1406
  WHERE event_type = 'fork_turn_history_imported'
1348
1407
  AND json_extract(payload_json, '$.forkedLocalSessionId') = ?
1408
+ AND (? IS NULL OR json_extract(payload_json, '$.sourceRemoteSessionId') = ?)
1409
+ AND (? IS NULL OR json_extract(payload_json, '$.sourceTurnId') = ?)
1349
1410
  LIMIT 1
1350
- `).get(localSessionId));
1351
- }
1352
-
1353
- private listCompletedTurnGraphRows(localSessionId: string): CompletedTurnSnapshotRecord[] {
1354
- const rows = this.db.prepare(`
1355
- SELECT ts.turn_id, tpl.parent_turn_id, ts.snapshot_json, ts.updated_at
1356
- FROM turn_snapshots ts
1357
- LEFT JOIN turn_parent_links tpl ON tpl.turn_id = ts.turn_id
1358
- WHERE ts.local_session_id = ?
1359
- AND json_extract(ts.snapshot_json, '$.status') = 'completed'
1360
- `).all(localSessionId) as Array<{
1361
- turn_id: string;
1362
- parent_turn_id: string | null;
1363
- snapshot_json: string;
1364
- updated_at: string;
1365
- }>;
1366
- return rows.map(mapCompletedTurnSnapshot);
1367
- }
1368
-
1369
- private getCompletedTurnGraphRecord(turnId: string): CompletedTurnSnapshotRecord | undefined {
1370
- const owner = this.db.prepare(`
1371
- SELECT local_session_id
1372
- FROM turn_snapshots
1373
- WHERE turn_id = ?
1374
- AND json_extract(snapshot_json, '$.status') = 'completed'
1375
- `).get(turnId) as { local_session_id: string } | undefined;
1376
- if (!owner) return undefined;
1377
- this.backfillTurnParents(owner.local_session_id);
1378
- const row = this.db.prepare(`
1379
- SELECT ts.turn_id, tpl.parent_turn_id, ts.snapshot_json, ts.updated_at
1380
- FROM turn_snapshots ts
1381
- LEFT JOIN turn_parent_links tpl ON tpl.turn_id = ts.turn_id
1382
- WHERE ts.turn_id = ?
1383
- AND json_extract(ts.snapshot_json, '$.status') = 'completed'
1384
- `).get(turnId) as {
1385
- turn_id: string;
1386
- parent_turn_id: string | null;
1387
- snapshot_json: string;
1388
- updated_at: string;
1389
- } | undefined;
1390
- return row ? mapCompletedTurnSnapshot(row) : undefined;
1411
+ `).get(localSessionId, sourceRemoteSessionId ?? null, sourceRemoteSessionId ?? null, sourceTurnId ?? null, sourceTurnId ?? null));
1391
1412
  }
1392
1413
 
1393
1414
  private findForkSourceTurnId(localSessionId: string): string | undefined {
@@ -2289,40 +2310,6 @@ function mapTurnAttempt(row: TurnAttemptRow): TurnAttemptRecord {
2289
2310
  };
2290
2311
  }
2291
2312
 
2292
- function mapCompletedTurnSnapshot(row: {
2293
- turn_id: string;
2294
- parent_turn_id: string | null;
2295
- snapshot_json: string;
2296
- updated_at: string;
2297
- }): CompletedTurnSnapshotRecord {
2298
- return {
2299
- turnId: row.turn_id,
2300
- parentTurnId: row.parent_turn_id ?? undefined,
2301
- snapshot: JSON.parse(row.snapshot_json),
2302
- updatedAt: row.updated_at,
2303
- };
2304
- }
2305
-
2306
- function compareCompletedTurnSnapshots(
2307
- left: CompletedTurnSnapshotRecord,
2308
- right: CompletedTurnSnapshotRecord,
2309
- ): number {
2310
- const timeDifference = completedTurnTimestamp(right) - completedTurnTimestamp(left);
2311
- return timeDifference || right.updatedAt.localeCompare(left.updatedAt);
2312
- }
2313
-
2314
- function completedTurnTimestamp(record: CompletedTurnSnapshotRecord): number {
2315
- const snapshot = record.snapshot && typeof record.snapshot === "object"
2316
- ? record.snapshot as { completedAt?: unknown; startedAt?: unknown }
2317
- : undefined;
2318
- const timestamp = typeof snapshot?.completedAt === "number"
2319
- ? snapshot.completedAt
2320
- : typeof snapshot?.startedAt === "number"
2321
- ? snapshot.startedAt
2322
- : Date.parse(record.updatedAt);
2323
- return Number.isFinite(timestamp) ? timestamp : 0;
2324
- }
2325
-
2326
2313
  function stringValue(value: unknown): string | undefined {
2327
2314
  return typeof value === "string" && value.trim() ? value.trim() : undefined;
2328
2315
  }
@@ -0,0 +1,28 @@
1
+ export function compareSemanticVersions(left: string, right: string): number {
2
+ const parse = (value: string) => /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/u.exec(value);
3
+ const a = parse(left);
4
+ const b = parse(right);
5
+ if (!a || !b) return 0;
6
+ for (let i = 1; i <= 3; i += 1) {
7
+ const difference = Number(a[i]) - Number(b[i]);
8
+ if (difference) return difference;
9
+ }
10
+ if (!a[4] && !b[4]) return 0;
11
+ if (!a[4]) return 1;
12
+ if (!b[4]) return -1;
13
+ const aParts = a[4].split(".");
14
+ const bParts = b[4].split(".");
15
+ for (let i = 0; i < Math.max(aParts.length, bParts.length); i += 1) {
16
+ const x = aParts[i];
17
+ const y = bParts[i];
18
+ if (x === undefined) return -1;
19
+ if (y === undefined) return 1;
20
+ if (x === y) continue;
21
+ const nx = /^\d+$/u.test(x);
22
+ const ny = /^\d+$/u.test(y);
23
+ if (nx && ny) return Number(x) - Number(y);
24
+ if (nx !== ny) return nx ? -1 : 1;
25
+ return x.localeCompare(y);
26
+ }
27
+ return 0;
28
+ }