extension.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // E2E 扩展 fixture(docs/plugins/extension-host.md §17.3、§17.7):纯 JS,不需要构建。
  2. // 激活时与每次换语言(env.onDidChange)时探测一次会话读能力:
  3. // - ctx.sessions.listSessions({}):成功记 E2E_SESSIONS_LIST ... ok count=N ids=…,失败记 error=<code>。
  4. // - 列表里每个会话 ctx.sessions.readSession({ sessionId, view: "messages" }):记 E2E_SESSIONS_READ,
  5. // 只记消息数、角色与是否含测试标记 E2E_EXT_SESSIONS,不记正文。
  6. const PLUGIN = "e2e-ext-sessions@inline";
  7. const MARKER = "E2E_EXT_SESSIONS";
  8. async function probe(ctx, reason) {
  9. let list;
  10. try {
  11. list = await ctx.sessions.listSessions({});
  12. } catch (error) {
  13. ctx.log.info(
  14. `E2E_SESSIONS_LIST plugin=${PLUGIN} reason=${reason} error=${error?.code ?? "unknown"}`,
  15. );
  16. return;
  17. }
  18. const ids = list.sessions.map((item) => item.session.sessionId);
  19. ctx.log.info(
  20. `E2E_SESSIONS_LIST plugin=${PLUGIN} reason=${reason} ok count=${ids.length} ids=${ids.join(",")}`,
  21. );
  22. for (const sessionId of ids) {
  23. try {
  24. const read = await ctx.sessions.readSession({ sessionId, view: "messages" });
  25. const messages = read.view === "messages" ? read.messages : [];
  26. const roles = [...new Set(messages.map((message) => message.role))].sort().join(",");
  27. const marker = messages.some((message) => message.text.includes(MARKER));
  28. ctx.log.info(
  29. `E2E_SESSIONS_READ plugin=${PLUGIN} reason=${reason} id=${sessionId} view=${read.view} messages=${messages.length} roles=${roles} marker=${marker}`,
  30. );
  31. } catch (error) {
  32. ctx.log.info(
  33. `E2E_SESSIONS_READ plugin=${PLUGIN} reason=${reason} id=${sessionId} error=${error?.code ?? "unknown"}`,
  34. );
  35. }
  36. }
  37. }
  38. export function activate(ctx) {
  39. void probe(ctx, "activate");
  40. ctx.env.onDidChange(({ locale }) => {
  41. void probe(ctx, `locale-${locale}`);
  42. });
  43. }
  44. export function deactivate() {}