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.
JavaScript claude-code-pluginclaude-code-skillscode-appsgithub-copilot-pluginpower-appspower-pagespower-platform
#!/usr/bin/env node
'use strict';
// ai-preflight: read-only AI readiness report for a Power Platform environment.
// Calls sdk.getAiReadiness and prints a per-feature on/off summary plus admin actions
// for any disabled features. Never fails on a disabled feature (informational); exits
// non-zero only on a usage error (missing --env) or an unexpected failure.
//
// Usage: node ai-preflight.js --env <orgUrl> [--app <uniqueName>]
const { parseArgs, validateFlags, emitResult } = require('./lib/dataverse-auth.js');
const { createAzHttpClient } = require('./lib/sdk-http-client.js');
const { AI_APP_SETTING, AI_SETTING_CODEC, resolveAppModuleId, effectiveSettingValue, settingIsOn } = require('./lib/ai-app-settings.js');
const { makeTempWorkspace } = require('./lib/temp-workspace.js');
const fs = require('node:fs');
const { makeWritesBlocking } = require('./lib/output-redaction.js');
// Human-readable label + admin-action hint for each feature key returned by getAiReadiness.
const FEATURE_META = {
formFill: {
// `FormFillBarUXEnabled` is the assist TOOLBAR only. The SDK now models the whole AI form-fill
// family, so each capability is reported separately rather than one label implying all four.
label: 'Form fill assist toolbar',
action: (f) => `Enable "Form fill assist toolbar" (setting: ${f.setting}) in Power Platform Admin Center → Environments → Settings → Product → Features.`,
},
formFillSuggestions: {
label: 'Form fill predictions (edit forms)',
action: (f) => `Enable "AI form fill predictions" (setting: ${f.setting}) in Power Platform Admin Center → Environments → Settings → Product → Features.`,
},
formFillSmartPaste: {
label: 'Form fill smart paste',
action: (f) => `Enable "Smart paste" (setting: ${f.setting}) in Power Platform Admin Center → Environments → Settings → Product → Features.`,
},
formFillFiles: {
label: 'Form fill from files',
action: (f) => `Enable "Form fill from files" (setting: ${f.setting}) in Power Platform Admin Center → Environments → Settings → Product → Features.`,
},
nlSearch: {
label: 'Natural language search',
action: (f) => `Enable "Natural language search" (setting: ${f.setting}) in Power Platform Admin Center → Environments → Settings → Product → Features.`,
},
nlChart: {
label: 'Natural language charts',
action: (f) => `Enable "Natural language charts" (setting: ${f.setting}) in Power Platform Admin Center → Environments → Settings → Product → Features.`,
},
summaries: {
label: 'AI row summaries',
action: (f) => `Enable "AI row summaries" via the "AI insight cards" setting (${f.setting}) in Power Platform Admin Center → Environments → Settings → Product → Features.`,
},
m365: {
label: 'M365 Copilot integration',
action: (f) => `Enable "M365 Copilot integration" (setting: ${f.setting}) in Power Platform Admin Center → Environments → Settings → Product → Features.`,
},
};
/**
* Pure: given the getAiReadiness result, produce a structured preflight report.
*
* `effective` (optional) maps a feature key to `{ value, scope, on }` for that feature's PER-APP
* setting, as resolved by `effectiveSettingValue`. It exists because the readiness gate and the
* feature's actual setting are different rows: a gate can read false while the feature is switched
* on at environment scope and therefore running in every app. Without this, the report tells an
* operator to go and enable something that is already on.
*
* A feature that is IN EFFECT gets no admin action, because there is nothing for an admin to do.
*
* @param {{ formFill, nlSearch, nlChart, summaries, m365 }} readiness
* @param {Record<string, {value?: string, scope?: string, on?: boolean, error?: string}>} [effective]
* @returns {{ features: Array<{feature, enabled, setting, inEffect?, effectiveValue?, effectiveScope?}>, adminActions: string[] }}
*/
function runPreflight(readiness, effective = {}) {
const features = [];
const adminActions = [];
for (const [key, meta] of Object.entries(FEATURE_META)) {
const f = readiness[key];
if (!f) continue;
// Where a feature's readiness gate IS its own per-app setting (the form-fill toolbar,
// predictions, files, and M365), the SDK decides `enabled` with its own codec, which — as
// vendored — knows only the form-fill family's scale, so M365's Off (`1`) reads as enabled.
// Read that value through this plugin's codec instead. A gate that is a different row (NL search,
// NL charts, smart paste, summaries) is a boolean the SDK already reads correctly, so it keeps
// the SDK's answer.
const enabled = f.setting === AI_APP_SETTING[key] && f.value !== undefined && f.value !== null
? settingIsOn(f.value, key) === true
: f.enabled;
const eff = effective[key] || {};
// Only a POSITIVE reading counts. `eff.error` (could not look, or the setting is not
// provisioned here) must never be read as "in effect" — that would suppress a real admin action.
const inEffect = eff.on === true;
// "Platform default" is a SPECIFIC state, not a synonym for "we could not decide". Only a
// codec-governed feature sitting at its own platform-default value ('0' for most, '1' — Auto —
// for charts) defers to the platform; an unrecognised value like '3' or 'yes' is simply
// indeterminate, and calling that "the platform decides" would invent a fact. Both suppress the
// ✓/✗ claim, but only one earns the explanation.
const codec = AI_SETTING_CODEC[key];
const effectiveDefault = eff.on === undefined && !eff.error && codec
&& eff.value !== undefined && String(eff.value).trim() === codec.platformDefault;
const effectiveIndeterminate = eff.on === undefined && !eff.error && eff.value !== undefined && !effectiveDefault;
features.push({
feature: key,
enabled,
setting: f.setting,
...(eff.value !== undefined ? { effectiveValue: eff.value, effectiveScope: eff.scope } : {}),
...(inEffect ? { inEffect: true } : {}),
...(effectiveDefault ? { effectiveDefault: true } : {}),
...(effectiveIndeterminate ? { effectiveIndeterminate: true } : {}),
});
if (!enabled && !inEffect) {
adminActions.push(meta.action(f));
}
}
return { features, adminActions };
}
/**
* One feature's line of the readiness report. "Not enabled" is not one state, so there are four:
* ✓ in effect through a setting the readiness gate does not read; ? at the platform default (the
* platform decides); ? a value this plugin does not recognise; otherwise ✓ or ✗ from the gate.
*/
function featureLine(f) {
const label = FEATURE_META[f.feature]?.label || f.feature;
if (f.inEffect && !f.enabled) {
// The distinction that matters: running, but not because of anything this app declares.
return ` ✓ ${label} (${f.setting}) — in effect via the ${f.effectiveScope} setting (value "${f.effectiveValue}"), though the readiness gate reads off\n`;
}
if (f.effectiveDefault) {
// "Platform default" is not off. `0` (Default) for most of these settings and `1` (Auto) for
// charts defer to the platform, so the feature may well be running; printing ✗ would assert
// something we cannot see.
return ` ? ${label} (${f.setting}) — set to the platform default ("${f.effectiveValue}"), so whether it runs is decided by the platform, not by this environment\n`;
}
if (f.effectiveIndeterminate) {
return ` ? ${label} (${f.setting}) — holds an unrecognised value ("${f.effectiveValue}"), so its state cannot be determined from here\n`;
}
return ` ${f.enabled ? '✓' : '✗'} ${label} (${f.setting})\n`;
}
async function main() {
const argv = process.argv.slice(2);
const { flags } = parseArgs(argv);
const USAGE = 'Usage: node scripts/ai-preflight.js --env <orgUrl> [--app <uniqueName>]';
const flagError = validateFlags(argv, { known: ['env', 'app'], needValue: ['env', 'app'] });
if (flagError) {
makeWritesBlocking(process.stdout, process.stderr);
process.stderr.write(`✗ ${flagError}\n${USAGE}\n`);
process.exit(1);
}
const env = flags.env;
const app = flags.app || null;
if (!env) {
process.stderr.write(USAGE + '\n');
process.exit(1);
}
// getAiReadiness is a read-only org/app query, but the vendored SDK expects an initialized
// workspace (throws WorkspaceNotInitializedError otherwise) — mirror the other entrypoints:
// create a throwaway workspace, initWorkspace(), and remove it in finally.
const workspaceDir = makeTempWorkspace('ai-preflight-');
let report;
try {
const { createMakerSdk, createNodeWorkspaceStorage } = require('./lib/maker-sdk.js');
const httpClient = createAzHttpClient(env);
const sdk = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(workspaceDir), instanceUrl: env, httpClient });
// Keep SDK construction inside the protected region too: a constructor failure happens after the
// temp directory exists, so the finally must own both construction and init to avoid leaks.
await sdk.initWorkspace();
const readinessOpts = app ? { appUniqueName: app } : {};
const readiness = await sdk.getAiReadiness(readinessOpts);
// Resolve each feature's ACTUAL setting alongside the gate. The gate only decides whether a
// build would write an app-scope override; the feature can already be switched on at
// environment scope (or on by default), in which case reporting it as disabled — and telling an
// admin to go enable it — is simply wrong. Best-effort: any lookup failure leaves the feature
// reported from the gate alone, which is the previous behaviour.
const read = { queryRecords: (entity, opts) => sdk.queryRecords(entity, opts) };
// If `--app` was supplied but the app cannot be resolved, the app-scope layer is UNKNOWN, not
// absent. Falling through to the environment value would be actively harmful: an app that
// overrides a feature to "0" while the environment holds "2" would be reported as in effect,
// and the admin action for a genuinely disabled feature would be suppressed. So a failed
// resolution makes every effective value indeterminate instead.
const appLookup = app ? await resolveAppModuleId(read, app) : { appModuleId: null };
const appModuleId = appLookup.appModuleId || null;
const effective = {};
for (const key of Object.keys(FEATURE_META)) {
const setting = AI_APP_SETTING[key];
if (!setting) continue; // `summaries` is not a per-app on/off setting
if (appLookup.error) { effective[key] = { error: `app scope unknown: ${appLookup.error}` }; continue; }
const res = await effectiveSettingValue(read, appModuleId, setting);
effective[key] = res.error ? { error: res.error } : { value: res.value, scope: res.scope, on: settingIsOn(res.value, key) };
}
// The run's last writes: these lines, then the result and the exit, with no turn of the event loop between them — made blocking (lib/output-redaction.js).
makeWritesBlocking(process.stdout, process.stderr);
if (appLookup.error) {
process.stderr.write(`\nWarning: could not resolve app '${app}' (${appLookup.error}); reporting readiness gates only.\n`);
}
report = runPreflight(readiness, effective);
process.stderr.write('\nAI Feature Readiness\n');
process.stderr.write('====================\n');
for (const f of report.features) process.stderr.write(featureLine(f));
if (report.adminActions.length) {
process.stderr.write('\nAdmin actions required:\n');
for (const a of report.adminActions) {
process.stderr.write(` • ${a}\n`);
}
} else {
process.stderr.write('\nAll AI features are available (enabled, or already in effect).\n');
}
} finally {
// emitResult() calls process.exit(), so clean up the throwaway workspace BEFORE emitting.
try {
fs.rmSync(workspaceDir, { recursive: true, force: true });
} catch {
// This is an informational read-only probe; on Windows the SDK can briefly retain a handle,
// and cleanup must not turn an otherwise successful readiness report into a script failure.
}
}
emitResult(true, { ok: true, ...report });
}
module.exports = { runPreflight, featureLine };
if (require.main === module) {
main().catch((err) => {
makeWritesBlocking(process.stdout, process.stderr);
process.stderr.write(`Error: ${err.message}\n`);
process.exit(1);
});
}