extension.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // E2E 扩展 fixture(docs/plugins/extension-host.md §17.12,P3e):纯 JS,不需要构建。
  2. // composer.status 的命令每次点击读一次插件目录旁的 e2e-ext-approvals-step.json(用例写入),按顺序执行里面的操作:
  3. // - watch / unwatch:订阅或退订 ctx.sessions.onDidChangeInteractions,每个事件写一行 E2E_APPROVALS_EVENT
  4. // (会话、模式、交互的 id 与种类、审批选项的 kind:optionId),订阅以错误结束时写 E2E_APPROVALS_WATCH_ERROR。
  5. // - watchRows / unwatchRows:订阅或退订这个会话的行,每个事件写一行 E2E_APPROVALS_ROWS(工具行的
  6. // 工具名:状态,不含参数与输出)。
  7. // - 其余是 ctx.sessions.* 的单个操作;结果或错误码写一行 E2E_APPROVALS_RESULT。
  8. import { readFile } from "node:fs/promises";
  9. import { join } from "node:path";
  10. const watches = new Map();
  11. const rowWatches = new Map();
  12. /** 行镜像里工具行的状态(toolCall 行的 status):插件靠它画「等你确认」与执行结果。 */
  13. function watchRows(ctx, sessionId) {
  14. rowWatches.get(sessionId)?.dispose();
  15. const rows = new Map();
  16. const subscription = ctx.sessions.onDidChangeRows(
  17. { sessionId },
  18. (event) => {
  19. if (event.kind === "reset") {
  20. rows.clear();
  21. for (const row of event.rows) rows.set(row.rowId, row);
  22. } else if (event.kind === "rows") {
  23. for (const row of event.upserted) rows.set(row.rowId, row);
  24. }
  25. const tools = [...rows.values()]
  26. .filter((row) => row.kind === "toolCall")
  27. .map((row) => `${row.toolName}:${row.status}`);
  28. ctx.log.info(
  29. `E2E_APPROVALS_ROWS session=${sessionId} kind=${event.kind} tools=${tools.join(",")}`,
  30. );
  31. },
  32. (error) => {
  33. rowWatches.delete(sessionId);
  34. ctx.log.info(`E2E_APPROVALS_ROWS session=${sessionId} ended=${error?.code ?? "unknown"}`);
  35. },
  36. );
  37. rowWatches.set(sessionId, subscription);
  38. return { watchingRows: sessionId };
  39. }
  40. function describeInteraction(interaction) {
  41. const options =
  42. interaction.payload.kind === "permission"
  43. ? [
  44. ...interaction.payload.options.map((option) => `${option.kind}:${option.optionId}`),
  45. ...(interaction.payload.fullAccessOption
  46. ? [`fullAccess:${interaction.payload.fullAccessOption.optionId}`]
  47. : []),
  48. ]
  49. : // 提问带每个问题的选项(宿主投影里的 label),E2E 断言插件看到的选项与模型给的一致。
  50. (interaction.payload.questions ?? []).flatMap((question) => [
  51. `question:${question.question}`,
  52. ...question.options.map((option) => `choice:${option.label}`),
  53. ]);
  54. return { id: interaction.interactionId, kind: interaction.kind, options };
  55. }
  56. function watch(ctx, sessionId) {
  57. watches.get(sessionId)?.dispose();
  58. const subscription = ctx.sessions.onDidChangeInteractions(
  59. { sessionId },
  60. (event) => {
  61. ctx.log.info(
  62. `E2E_APPROVALS_EVENT session=${event.sessionId} mode=${event.mode} interactions=${JSON.stringify(
  63. event.interactions.map(describeInteraction),
  64. )}`,
  65. );
  66. },
  67. (error) => {
  68. watches.delete(sessionId);
  69. ctx.log.info(
  70. `E2E_APPROVALS_WATCH_ERROR session=${sessionId} error=${error?.code ?? "unknown"}`,
  71. );
  72. },
  73. );
  74. watches.set(sessionId, subscription);
  75. return { watching: sessionId };
  76. }
  77. async function runOp(ctx, op) {
  78. if (op.name === "watch") return watch(ctx, op.input.sessionId);
  79. if (op.name === "watchRows") return watchRows(ctx, op.input.sessionId);
  80. if (op.name === "unwatchRows") {
  81. rowWatches.get(op.input.sessionId)?.dispose();
  82. rowWatches.delete(op.input.sessionId);
  83. return { unwatchedRows: op.input.sessionId };
  84. }
  85. if (op.name === "unwatch") {
  86. watches.get(op.input.sessionId)?.dispose();
  87. watches.delete(op.input.sessionId);
  88. return { unwatched: op.input.sessionId };
  89. }
  90. return ctx.sessions[op.name](op.input);
  91. }
  92. export async function activate(ctx) {
  93. const root = join(ctx.extension.path, "..");
  94. ctx.commands.register("run-step", async () => {
  95. const step = JSON.parse(await readFile(join(root, "e2e-ext-approvals-step.json"), "utf8"));
  96. for (const [index, op] of step.ops.entries()) {
  97. const label = `seq=${step.seq} index=${index} op=${op.name}`;
  98. try {
  99. const result = await runOp(ctx, op);
  100. ctx.log.info(`E2E_APPROVALS_RESULT ${label} ok=${JSON.stringify(result ?? null)}`);
  101. } catch (error) {
  102. ctx.log.info(`E2E_APPROVALS_RESULT ${label} error=${error?.code ?? "unknown"}`);
  103. }
  104. }
  105. ctx.log.info(`E2E_APPROVALS_DONE seq=${step.seq}`);
  106. });
  107. ctx.slots.update("run", { text: "E2E approvals", tooltip: "E2E_APPROVALS" });
  108. ctx.log.info(
  109. `E2E_APPROVALS_ACTIVATED plugin=${ctx.extension.id} dataPath=${JSON.stringify(ctx.extension.dataPath)}`,
  110. );
  111. }
  112. export function deactivate() {
  113. for (const subscription of watches.values()) subscription.dispose();
  114. watches.clear();
  115. }