extension.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // E2E 扩展 fixture(docs/plugins/extension-host.md §17.3、§17.8,P3c):纯 JS,不需要构建。
  2. // composer.status 的命令每次点击读一次插件目录旁的 e2e-ext-writes-step.json(用例写入),按顺序执行里面的
  3. // ctx.sessions.* 操作,每个操作的结果或错误码写一行 E2E_WRITES_RESULT(结果只有身份与状态,不含提示词)。
  4. // 操作带 subscribeRows 时,结果里一有会话就立即订阅它的行,复现「新建后立即订阅」的时序,记第一个事件或错误码。
  5. // 操作带 watchRows 时(或操作名是 watchRows),持续订阅这个会话的行,每个事件写一行 E2E_WRITES_WATCH:
  6. // 本轮状态(turnHeader 的 state)、助手文字的总字数、事件数;订阅以错误结束时写 ended=<错误码>。unwatchRows 退订。
  7. import { readFile } from "node:fs/promises";
  8. import { join } from "node:path";
  9. /** 订阅行,等第一个事件或错误,然后退订。 */
  10. function probeRows(ctx, sessionId, label) {
  11. return new Promise((resolve) => {
  12. let subscription = null;
  13. let done = false;
  14. const finish = (line) => {
  15. if (done) return;
  16. done = true;
  17. ctx.log.info(`E2E_WRITES_ROWS ${label} session=${sessionId} ${line}`);
  18. subscription?.dispose();
  19. resolve();
  20. };
  21. subscription = ctx.sessions.onDidChangeRows(
  22. { sessionId },
  23. (event) =>
  24. finish(`kind=${event.kind} rows=${event.kind === "reset" ? event.rows.length : 0}`),
  25. (error) => finish(`error=${error.code}`),
  26. );
  27. if (done) subscription.dispose();
  28. });
  29. }
  30. const watches = new Map();
  31. /** 持续订阅行:宿主的行镜像每推一次,记一行摘要(不含正文)。 */
  32. function watchRows(ctx, sessionId) {
  33. watches.get(sessionId)?.dispose();
  34. const rows = new Map();
  35. let events = 0;
  36. const subscription = ctx.sessions.onDidChangeRows(
  37. { sessionId },
  38. (event) => {
  39. if (event.kind === "reset") {
  40. rows.clear();
  41. for (const row of event.rows) rows.set(row.rowId, row);
  42. } else if (event.kind === "rows") {
  43. for (const row of event.upserted) rows.set(row.rowId, row);
  44. }
  45. events += 1;
  46. const all = [...rows.values()];
  47. const turns = all.filter((row) => row.kind === "turnHeader").map((row) => row.state);
  48. const replyChars = all
  49. .filter((row) => row.kind === "assistantText")
  50. .reduce((sum, row) => sum + (typeof row.text === "string" ? row.text.length : 0), 0);
  51. ctx.log.info(
  52. `E2E_WRITES_WATCH session=${sessionId} kind=${event.kind} turns=${turns.join(",")} replyChars=${replyChars} events=${events}`,
  53. );
  54. },
  55. (error) => {
  56. watches.delete(sessionId);
  57. ctx.log.info(`E2E_WRITES_WATCH session=${sessionId} ended=${error?.code ?? "unknown"}`);
  58. },
  59. );
  60. watches.set(sessionId, subscription);
  61. return { watching: sessionId };
  62. }
  63. async function runOp(ctx, op) {
  64. if (op.name === "watchRows") return watchRows(ctx, op.input.sessionId);
  65. if (op.name === "unwatchRows") {
  66. watches.get(op.input.sessionId)?.dispose();
  67. watches.delete(op.input.sessionId);
  68. return { unwatched: op.input.sessionId };
  69. }
  70. return ctx.sessions[op.name](op.input);
  71. }
  72. export async function activate(ctx) {
  73. const root = join(ctx.extension.path, "..");
  74. ctx.commands.register("run-step", async () => {
  75. const step = JSON.parse(await readFile(join(root, "e2e-ext-writes-step.json"), "utf8"));
  76. for (const [index, op] of step.ops.entries()) {
  77. const label = `seq=${step.seq} index=${index} op=${op.name}`;
  78. try {
  79. const result = await runOp(ctx, op);
  80. ctx.log.info(`E2E_WRITES_RESULT ${label} ok=${JSON.stringify(result ?? null)}`);
  81. const sessionId = result?.session?.sessionId;
  82. if (op.subscribeRows && sessionId) await probeRows(ctx, sessionId, label);
  83. if (op.watchRows && sessionId) watchRows(ctx, sessionId);
  84. } catch (error) {
  85. // 扩展错误的 message 是固定文案(session-control 的错误文本不含路径与正文)。
  86. ctx.log.info(
  87. `E2E_WRITES_RESULT ${label} error=${error?.code ?? "unknown"} message=${JSON.stringify(error?.message ?? "")}`,
  88. );
  89. }
  90. }
  91. ctx.log.info(`E2E_WRITES_DONE seq=${step.seq}`);
  92. });
  93. ctx.slots.update("run", { text: "E2E writes", tooltip: "E2E_WRITES" });
  94. ctx.log.info(`E2E_WRITES_ACTIVATED plugin=${ctx.extension.id}`);
  95. }
  96. export function deactivate() {}