extension.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // E2E 扩展 fixture(docs/plugins/extension-host.md §18.1):Jarvis 要的宿主能力,纯 JS,不需要构建。
  2. // - 视图 home:scope "app"(H5),从 nav.primary 打开;页面调工具 describe 读实例 id 与作用域。
  3. // - 激活时报告 ctx.extension.dataPath 与目录是否存在(H6),以及 models.list 的结果(H9)。
  4. // - 导航条目的未读标记(H11):激活时设为 3;视图工具 setBadge 改成数字、"dot" 或 null(清除)。
  5. // - 视图工具 requestGrants 调 grants.request(H12),把返回的生效档位记进日志。
  6. // - 视图工具 notify 调 notifications.show(H8),把结果记进日志(不记标题与正文以外的东西)。
  7. // - 每次激活在 dataPath 里的计数文件加一,日志记上一次的值(App 重启后数据还在,H6);
  8. // 插件目录旁写当前扩展进程的 pid,用例据此杀进程。
  9. // 运行时证据写进日志(E2E_JARVISHOST_*)。
  10. import { readFile, stat, writeFile } from "node:fs/promises";
  11. import { join } from "node:path";
  12. export async function activate(ctx) {
  13. const dataPath = ctx.extension.dataPath;
  14. const isDirectory = await stat(dataPath).then(
  15. (info) => info.isDirectory(),
  16. () => false,
  17. );
  18. ctx.log.info(`E2E_JARVISHOST_DATAPATH ${JSON.stringify({ dataPath, isDirectory })}`);
  19. const counterPath = join(dataPath, "e2e-activations.json");
  20. const previous = await readFile(counterPath, "utf8").then(
  21. (text) => Number(JSON.parse(text).count) || 0,
  22. () => 0,
  23. );
  24. await writeFile(counterPath, JSON.stringify({ count: previous + 1 }));
  25. ctx.log.info(`E2E_JARVISHOST_DATA previous=${previous} count=${previous + 1}`);
  26. await writeFile(
  27. join(ctx.extension.path, "..", "e2e-ext-jarvis-host-state.json"),
  28. JSON.stringify({ pid: process.pid }),
  29. );
  30. ctx.views.register("home", {
  31. entry: "./view.html",
  32. onDidOpen(view) {
  33. ctx.log.info(
  34. `E2E_JARVISHOST_VIEW_OPEN instance=${view.id} scope=${view.scope.kind} session=${view.sessionId ?? "none"}`,
  35. );
  36. view.onDidClose(() => ctx.log.info(`E2E_JARVISHOST_VIEW_CLOSE instance=${view.id}`));
  37. },
  38. tools: {
  39. notify: {
  40. description: "Show a system notification",
  41. inputSchema: { type: "object", properties: { view: { type: "string" } } },
  42. run: async (input) => {
  43. try {
  44. const result = await ctx.notifications.show({
  45. title: "E2E title",
  46. body: "E2E body",
  47. ...(input?.view ? { target: { view: input.view } } : {}),
  48. });
  49. ctx.log.info(`E2E_JARVISHOST_NOTIFY ok ${JSON.stringify(result)}`);
  50. return result;
  51. } catch (error) {
  52. ctx.log.info(`E2E_JARVISHOST_NOTIFY error code=${error?.code ?? "unknown"}`);
  53. return { error: error?.code ?? "unknown" };
  54. }
  55. },
  56. },
  57. requestGrants: {
  58. description: "Ask the host to show the consent dialog",
  59. inputSchema: { type: "object", properties: {} },
  60. run: async () => {
  61. try {
  62. const result = await ctx.grants.request();
  63. ctx.log.info(`E2E_JARVISHOST_GRANTS ok granted=${result.granted.join(",")}`);
  64. return result;
  65. } catch (error) {
  66. ctx.log.info(`E2E_JARVISHOST_GRANTS error code=${error?.code ?? "unknown"}`);
  67. return { error: error?.code ?? "unknown" };
  68. }
  69. },
  70. },
  71. setBadge: {
  72. description: "Set the nav badge",
  73. inputSchema: { type: "object", properties: { badge: {} } },
  74. run: (input) => {
  75. ctx.slots.update("home", { badge: input?.badge ?? null });
  76. return { badge: input?.badge ?? null };
  77. },
  78. },
  79. describe: {
  80. description: "Describe this view instance",
  81. inputSchema: { type: "object", properties: {} },
  82. run: (_input, call) => ({
  83. instance: call.view.id,
  84. scope: call.view.scope.kind,
  85. sessionId: call.view.sessionId ?? null,
  86. }),
  87. },
  88. },
  89. });
  90. ctx.slots.update("home", { badge: 3 });
  91. // 不阻塞激活:模型选择服务就绪前 getView 会等待。
  92. ctx.models.list().then(
  93. (models) => ctx.log.info(`E2E_JARVISHOST_MODELS ok ${JSON.stringify(models)}`),
  94. (error) => ctx.log.info(`E2E_JARVISHOST_MODELS error code=${error?.code ?? "unknown"}`),
  95. );
  96. }
  97. export function deactivate() {}