extension.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // E2E 扩展 fixture(docs/plugins/extension-host.md §16.7、§18.1 H1):纯 JS,不需要构建。
  2. // - env.onDidChange(订阅后先收当前值,之后每次换语言)时调 ctx.settings.get() 并记一行 E2E_SETTINGS_PROBE。
  3. // - ctx.settings.onDidChange:本插件的设置保存或重置成功后记一行 E2E_SETTINGS_CHANGED。
  4. // - 视图 account(写法一):工具 login({ token }) 调 ctx.settings.update 写敏感键,logout() 清除它。
  5. // 与 GitLab 面板的登录、退出同一条路径(§18.2)。工具的返回不带 token。
  6. // 工具 setRegion({ region }) 调 ctx.settings.update 写普通键(非敏感值进 config.json)。
  7. // 只记 hasToken、token 的长度、region 与键名,不记 token 本身(日志不能泄露敏感值)。
  8. function describe(values, source, extra = "") {
  9. const token = typeof values.token === "string" ? values.token : "";
  10. const keys = Object.keys(values).sort().join(",");
  11. return `${source} plugin=e2e-ext-settings${extra} hasToken=${token.length > 0} tokenLength=${token.length} region=${String(values.region ?? "")} keys=${keys}`;
  12. }
  13. export function activate(ctx) {
  14. ctx.env.onDidChange(({ locale }) => {
  15. ctx.settings
  16. .get()
  17. .then(({ values }) =>
  18. ctx.log.info(describe(values, "E2E_SETTINGS_PROBE", ` locale=${locale}`)),
  19. )
  20. .catch((error) => ctx.log.error(`E2E_SETTINGS_PROBE failed: ${error?.code ?? error}`));
  21. });
  22. ctx.settings.onDidChange(({ values }) => {
  23. ctx.log.info(describe(values, "E2E_SETTINGS_CHANGED"));
  24. });
  25. const run = async (name, update) => {
  26. try {
  27. await ctx.settings.update(update);
  28. ctx.log.info(`E2E_SETTINGS_UPDATE ${name} ok`);
  29. return { ok: true };
  30. } catch (error) {
  31. ctx.log.info(`E2E_SETTINGS_UPDATE ${name} code=${error?.code ?? "unknown"}`);
  32. return { ok: false, code: error?.code ?? "unknown" };
  33. }
  34. };
  35. ctx.views.register("account", {
  36. entry: "./view.html",
  37. tools: {
  38. login: {
  39. description: "Store the token in this plugin's settings",
  40. inputSchema: { type: "object", properties: { token: { type: "string" } } },
  41. run: (input) => run("login", { values: { token: String(input?.token ?? "") } }),
  42. },
  43. logout: {
  44. description: "Clear the token from this plugin's settings",
  45. inputSchema: { type: "object", properties: {} },
  46. run: () => run("logout", { clear: ["token"] }),
  47. },
  48. setRegion: {
  49. description: "Store a normal (non-sensitive) setting",
  50. inputSchema: { type: "object", properties: { region: { type: "string" } } },
  51. run: (input) => run("region", { values: { region: String(input?.region ?? "") } }),
  52. },
  53. },
  54. });
  55. }
  56. export function deactivate() {}