Skip to content

A plugin marketplace for GitHub Copilot and other AI agents that provides Power Platform development plugins, including reusable skills, agents, and commands for building and deploying solutions.

7.5 KB
Raw Blame
"use strict";

const fs = require("node:fs");
const path = require("node:path");
const os = require("node:os");
const crypto = require("node:crypto");

const { detectSlashCommand } = require("./prompt-detector");
const { buildSkillStarted } = require("./events");
const { getSessionId } = require("./session");
const { fireAndForget } = require("./emit-spawn");
const { loadResolver } = require("./resolver-loader");
const { readPacAuth } = require("./pac-auth");
const { readPacCliVersion, readAiAgent } = require("./agent-info");

function readIkey(telemetryDir) {
  // Test/override seam: POWER_PLATFORM_SKILLS_IKEY_JSON points at an alternate
  // ikey.json so tests don't have to mutate the checked-in config file.
  const override = process.env.POWER_PLATFORM_SKILLS_IKEY_JSON;
  const overridePath = override && override.trim() ? override : "";
  // Guard a missing/invalid telemetryDir (a caller bug this fail-closed helper
  // must tolerate): with no override there is no config to read, so fail CLOSED
  // (disabled: true) instead of letting path.join throw out of the library.
  if (!overridePath && (typeof telemetryDir !== "string" || !telemetryDir)) {
    return { cfg: null, dir: "", ikeyPath: "", eventStreamName: "", disabled: true };
  }
  const ikeyPath = overridePath || path.join(telemetryDir, "ikey.json");
  const dir = path.dirname(ikeyPath);
  try {
    const cfg = JSON.parse(fs.readFileSync(ikeyPath, "utf8"));
    return { cfg, dir, ikeyPath, eventStreamName: cfg.event_stream_name || "", disabled: cfg.disabled === true };
  } catch {
    // ikey.json missing/unreadable → fail CLOSED (disabled: true), matching
    // emit-dispatcher.js's isDisabledByConfig(). If we can't read the config we
    // cannot confirm emission is authorized, so suppress.
    return { cfg: null, dir, ikeyPath, eventStreamName: "", disabled: true };
  }
}

function osFriendlyName(platform) {
  if (platform === "win32") return "Windows";
  if (platform === "darwin") return "Mac";
  if (platform === "linux") return "Linux";
  return platform;
}

function emitSkillStartedFromPrompt(promptText, opts = {}) {
  const {
    pluginName,
    pluginVersion,
    trackedSkills,
    telemetryDir,
    sessionId, // primed from Claude Code's hook payload (parsed.session_id)
    // Optional plugin-supplied dynamic payload merged into `eventInfo` below.
    // Accepts a plain object, or a thunk returning one. Prefer the thunk: it is
    // invoked only after the slash-command / `disabled` / `isProvisioned` gates
    // below, so a plugin whose payload costs real work (filesystem probing, etc.)
    // pays nothing on untracked prompts or a disabled plugin.
    eventInfo,
    _emit, // test seam; defaults to fireAndForget
    _readPacAuth, // test seam; defaults to lib/pac-auth
    _readAgentInfo, // test seam; defaults to lib/agent-info
  } = opts;

  const skillName = detectSlashCommand(promptText, { pluginName, trackedSkills });
  if (!skillName) return { emitted: false, skillName: null };

  // Repo-side hard-off: short-circuit BEFORE any PAC / agent-info shellouts
  // (~3-5s combined) so a disabled or half-provisioned plugin pays effectively
  // no cost. The user opt-out is NOT checked here: the event is still built and
  // dispatched so the detached dispatcher can write the local diagnostic mirror;
  // the dispatcher reads the per-plugin config and skips the POST when opted out.
  const { cfg, dir, ikeyPath, eventStreamName, disabled } = readIkey(telemetryDir);
  if (disabled) return { emitted: false, skillName };
  // Provisioning fast-gate (generic): a plugin resolver decides "is there a key
  // worth paying the pac shellout for?"; default is "static key present".
  const resolver = loadResolver(dir);
  let provisioned;
  try {
    provisioned =
      resolver && typeof resolver.isProvisioned === "function"
        ? resolver.isProvisioned(cfg)
        // Static-key fallback (no resolver): a real key must be present AND not be
        // the shipped placeholder sentinel — otherwise a plugin that flipped
        // `disabled:false` before replacing the key would be treated as provisioned
        // and pay the pac/agent-info shellouts + dispatch (local-log write) even
        // though the dispatcher can never POST a placeholder key. Matches the
        // dispatcher's PLACEHOLDER_IKEY guard and the pretool hook's gate.
        : !!(cfg && cfg.instrumentationKey && cfg.instrumentationKey !== "PLACEHOLDER_REPLACE_BEFORE_SHIPPING");
  } catch {
    // A plugin-provided resolver threw (or assumed cfg non-null) — treat as not
    // provisioned so a bad resolver can't break prompt handling (fail closed).
    provisioned = false;
  }
  if (!provisioned) return { emitted: false, skillName };

  const pacReader = typeof _readPacAuth === "function" ? _readPacAuth : readPacAuth;
  let pacAuth = null;
  try {
    pacAuth = pacReader();
  } catch {
    pacAuth = null;
  }

  const agentReader =
    typeof _readAgentInfo === "function"
      ? _readAgentInfo
      : () => ({
          ...readAiAgent(),
          pacCliVersion: readPacCliVersion(),
        });
  let agentInfo;
  try {
    agentInfo = agentReader() || {};
  } catch {
    agentInfo = {};
  }

  const fields = {
    pluginName,
    pluginVersion: pluginVersion || "unknown",
    sessionId: getSessionId(sessionId),
    correlationId: crypto.randomUUID(),
    osName: osFriendlyName(process.platform),
    osVersion: os.release(),
    nodeVersion: "v" + String(process.versions.node).split(".")[0],
    skillName,
  };
  if (pacAuth && pacAuth.orgId) fields.orgId = pacAuth.orgId;
  if (pacAuth && pacAuth.tenantId) fields.tenantId = pacAuth.tenantId;
  // `eventInfo` is assembled from two INDEPENDENT sources and assigned only when
  // non-empty. Building it inside the objectId guard instead would drop the
  // caller's payload on every unauthenticated run (`pac auth who` surfaces no
  // object id), and any later `fields.eventInfo.x = ...` would throw on the
  // undefined — a throw the fail-closed wrapper swallows, silently degrading the
  // run to no event at all. The caller owns not putting PII in its payload;
  // `aadObjectId` is written last so the library's own field always wins.
  const mergedEventInfo = {};
  let callerEventInfo = eventInfo;
  if (typeof callerEventInfo === "function") {
    try {
      callerEventInfo = callerEventInfo();
    } catch {
      // A caller thunk threw — drop its contribution rather than lose the whole
      // event. The library's own fields below are unaffected.
      callerEventInfo = null;
    }
  }
  if (callerEventInfo && typeof callerEventInfo === "object" && !Array.isArray(callerEventInfo)) {
    Object.assign(mergedEventInfo, callerEventInfo);
  }
  if (pacAuth && pacAuth.objectId) mergedEventInfo.aadObjectId = pacAuth.objectId;
  if (Object.keys(mergedEventInfo).length > 0) fields.eventInfo = mergedEventInfo;
  if (agentInfo.aiAgentName) fields.aiAgentName = agentInfo.aiAgentName;
  if (agentInfo.aiAgentVersion) fields.aiAgentVersion = agentInfo.aiAgentVersion;
  if (agentInfo.pacCliVersion) fields.pacCliVersion = agentInfo.pacCliVersion;

  const event = buildSkillStarted(eventStreamName, fields);

  const emit = typeof _emit === "function" ? _emit : fireAndForget;
  try {
    emit(event, {
      cloud: (pacAuth && pacAuth.cloud) || "",
      configDir: process.env.POWER_PLATFORM_SKILLS_CONFIG_DIR || "",
      fakeProbe: process.env.POWER_PLATFORM_SKILLS_FAKE_HTTPS || "",
      ikeyJsonPath: ikeyPath,
    });
  } catch {
    // fail closed — telemetry never propagates errors
  }

  return { emitted: true, skillName };
}

module.exports = { emitSkillStartedFromPrompt };