mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-07-07 11:00:32 +02:00
Compare commits
107 Commits
dev/mjolle
...
gleb/advan
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
780abe199c | ||
|
|
6e6e637c5a | ||
|
|
05e4076929 | ||
|
|
b123a4f14a | ||
|
|
4ce652945c | ||
|
|
2710f0c599 | ||
|
|
f6c8550039 | ||
|
|
54943c943e | ||
|
|
714da1ab92 | ||
|
|
3768a9584c | ||
|
|
1729e26ce3 | ||
|
|
2019d0b419 | ||
|
|
64071b7af0 | ||
|
|
4d1171f7d0 | ||
|
|
ca3854744c | ||
|
|
b63c319d83 | ||
|
|
a7829722d3 | ||
|
|
c648980233 | ||
|
|
a2ee84f285 | ||
|
|
faf0a7cc3f | ||
|
|
aa09683d96 | ||
|
|
2572b1217b | ||
|
|
96fb1d004b | ||
|
|
046796ae3b | ||
|
|
56af1e42eb | ||
|
|
5e0f92fbf2 | ||
|
|
ccbd288a68 | ||
|
|
e9221d2174 | ||
|
|
e7e3db830f | ||
|
|
cc55315092 | ||
|
|
6f981528de | ||
|
|
450568a0e8 | ||
|
|
9ea621b180 | ||
|
|
020dd67316 | ||
|
|
c6176fb436 | ||
|
|
0f7c3f70d3 | ||
|
|
ec475f0dcc | ||
|
|
7a3539f6d8 | ||
|
|
f4ddb79707 | ||
|
|
b2251cab7f | ||
|
|
5931b3d569 | ||
|
|
abcb77dea1 | ||
|
|
7da7a0b277 | ||
|
|
6d1c414b07 | ||
|
|
279aaf6035 | ||
|
|
125fffc607 | ||
|
|
0fcd72920d | ||
|
|
361959374b | ||
|
|
17a0533d08 | ||
|
|
d3c8b4833c | ||
|
|
832edcd580 | ||
|
|
c2caa46a3f | ||
|
|
6657da8310 | ||
|
|
79f850b0c5 | ||
|
|
8370908d75 | ||
|
|
ca7bc16b7a | ||
|
|
7416209cc7 | ||
|
|
3269de6172 | ||
|
|
96c5d16a45 | ||
|
|
9e02f485f0 | ||
|
|
822303a68c | ||
|
|
68eb695626 | ||
|
|
d776b64a64 | ||
|
|
dbae67dfaa | ||
|
|
011fc5a190 | ||
|
|
d628d73e0b | ||
|
|
3281c0e1a5 | ||
|
|
749378d3a2 | ||
|
|
00bc7ec822 | ||
|
|
faae1ae694 | ||
|
|
c43261f7cc | ||
|
|
ce8508ef97 | ||
|
|
ab9a8c979c | ||
|
|
9befd280a2 | ||
|
|
7436b46fd6 | ||
|
|
3dfeeceb7d | ||
|
|
26bbfdeb8f | ||
|
|
c46e6147b5 | ||
|
|
36366e13c9 | ||
|
|
4fac403966 | ||
|
|
d28ae33c22 | ||
|
|
401ddcd95e | ||
|
|
0bc45045e0 | ||
|
|
03438406c7 | ||
|
|
9f6d6b9cf2 | ||
|
|
f53e64a456 | ||
|
|
0a2bc81c25 | ||
|
|
fd96b463c4 | ||
|
|
9421efe015 | ||
|
|
32ffd3a3b2 | ||
|
|
a60eab4d03 | ||
|
|
c2789d6080 | ||
|
|
8224062c3b | ||
|
|
dc1484549a | ||
|
|
51c5d320f3 | ||
|
|
b5fc9fea78 | ||
|
|
a25ce4ed02 | ||
|
|
a3620f1d5e | ||
|
|
f8b158659e | ||
|
|
a025d4dfc4 | ||
|
|
f6149bb72f | ||
|
|
74eafb56b3 | ||
|
|
6072ec7d8f | ||
|
|
1c78950d83 | ||
|
|
ecc93eaa33 | ||
|
|
eed0e20f6b | ||
|
|
a48e741a40 |
5
.github/actions/spell-check/allow/code.txt
vendored
5
.github/actions/spell-check/allow/code.txt
vendored
@@ -309,6 +309,11 @@ pwa
|
||||
AOT
|
||||
Aot
|
||||
ify
|
||||
LAF
|
||||
Laf
|
||||
languagemodel
|
||||
philm
|
||||
phisilica
|
||||
TFM
|
||||
|
||||
# YML
|
||||
|
||||
1
.github/actions/spell-check/expect.txt
vendored
1
.github/actions/spell-check/expect.txt
vendored
@@ -1602,7 +1602,6 @@ sancov
|
||||
SAVEFAILED
|
||||
schedtasks
|
||||
SCID
|
||||
SCL
|
||||
Scode
|
||||
SCREENFONTS
|
||||
screenruler
|
||||
|
||||
3
.github/actions/spell-check/patterns.txt
vendored
3
.github/actions/spell-check/patterns.txt
vendored
@@ -313,6 +313,9 @@ ms-windows-store://\S+
|
||||
# ANSI color codes
|
||||
(?:\\(?:u00|x)1[Bb]|\\03[1-7]|\x1b|\\u\{1[Bb]\})\[\d+(?:;\d+)*m
|
||||
|
||||
# Phi Silica internal token/ID literals
|
||||
<PhiSilicaLafToken\b[^>]*>[^<]+</PhiSilicaLafToken>
|
||||
\bdjwsxzxb4ksa8\b
|
||||
# Special licenses text from RNNoise (BSD-style disclaimer: ``AS IS'')
|
||||
``AS IS''
|
||||
|
||||
|
||||
@@ -102,7 +102,7 @@ extends:
|
||||
useManagedIdentity: $(SigningUseManagedIdentity)
|
||||
clientId: $(SigningOriginalClientId)
|
||||
# Have msbuild use the release nuget config profile
|
||||
additionalBuildOptions: /p:RestoreConfigFile="$(Build.SourcesDirectory)\.pipelines\release-nuget.config" /p:EnableCmdPalAOT=true
|
||||
additionalBuildOptions: /p:RestoreConfigFile="$(Build.SourcesDirectory)\.pipelines\release-nuget.config" /p:EnableCmdPalAOT=true /p:PhiSilicaLafToken=$(PhiSilicaLafToken) /p:PhiSilicaLafAttestation="$(PhiSilicaLafAttestation)"
|
||||
beforeBuildSteps:
|
||||
# Install the Terrapin retrieval tool, which replaces vcpkg's download handler
|
||||
# to redirect it to a safe Microsoft-controlled location
|
||||
|
||||
@@ -266,6 +266,17 @@ jobs:
|
||||
VCWhereExtraVersionTarget: '-prerelease'
|
||||
|
||||
- ${{ if eq(parameters.official, true) }}:
|
||||
# M.W.T.V Setup.ps1 sets the pipeline-level XES_APPXMANIFESTVERSION env var
|
||||
# from the supplied -ProjectDirectory's custom.props. Whichever invocation
|
||||
# runs last wins. cmdpal MUST run last because $(CmdPalVersion) (used by the
|
||||
# VNext installer and CmdPal's AppxPackageTestDir) falls back to that env
|
||||
# var; if AP wins, CmdPal's folder name and MSIX filename disagree on
|
||||
# VersionMinor and the installer fails with WIX0103. Each project's own
|
||||
# custom.props is still applied at MSBuild time, so AP versioning is
|
||||
# unaffected by the order.
|
||||
- template: .\steps-setup-versioning.yml
|
||||
parameters:
|
||||
directory: $(build.sourcesdirectory)\src\modules\AdvancedPaste
|
||||
- template: .\steps-setup-versioning.yml
|
||||
parameters:
|
||||
directory: $(build.sourcesdirectory)\src\modules\cmdpal
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
<RepoRoot>$(MSBuildThisFileDirectory)</RepoRoot>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(RepoRoot)src\Version.props" />
|
||||
<Import Project="$(RepoRoot)src\PhiSilicaLaf.props" />
|
||||
<PropertyGroup>
|
||||
<Copyright>Copyright (C) Microsoft Corporation. All rights reserved.</Copyright>
|
||||
<AssemblyCopyright>Copyright (C) Microsoft Corporation. All rights reserved.</AssemblyCopyright>
|
||||
|
||||
100
doc/devdocs/modules/advancedpaste-phisilica-local-testing.md
Normal file
100
doc/devdocs/modules/advancedpaste-phisilica-local-testing.md
Normal file
@@ -0,0 +1,100 @@
|
||||
# Advanced Paste – Phi Silica local testing
|
||||
|
||||
How to build, register, and test **Phi Silica** in **Advanced Paste (AP)** on a dev machine,
|
||||
plus the few things that actually break it.
|
||||
|
||||
## How it fits together
|
||||
|
||||
AP ships as an **unpackaged, self-contained WinUI 3 exe** (`PowerToys.AdvancedPaste.exe`).
|
||||
The Windows AI `LanguageModel` (Phi Silica) API is a **Limited Access Feature (LAF)**. For it
|
||||
to work, all of these must line up:
|
||||
|
||||
1. **Package identity** — AP runs with identity granted by the sparse MSIX
|
||||
`Microsoft.PowerToys.SparseApp`.
|
||||
2. **Matching LAF creds** — the token/attestation baked into the exe match the registered
|
||||
sparse package's publisher.
|
||||
3. **AI metadata deployed** — the `Microsoft.Windows.AI*.winmd` files ship next to the exe;
|
||||
the AI runtime resolves them **at runtime**.
|
||||
4. **Model ready** — supported hardware and the on-device model downloaded
|
||||
(`GetReadyState() == Ready`).
|
||||
|
||||
Two identities — the baked token must match the registered package's publisher:
|
||||
|
||||
| Build | Publisher Id | LAF creds |
|
||||
|-------|--------------|-----------|
|
||||
| **Dev** | `djwsxzxb4ksa8` | dev default in [`src/PhiSilicaLaf.props`](../../../src/PhiSilicaLaf.props) |
|
||||
| **Prod** | `8wekyb3d8bbwe` | secret, injected only by `.pipelines/v2/release.yml` |
|
||||
|
||||
Non-secret pairing check: the exe's baked **Attestation** must equal the registered package's
|
||||
**PublisherId**.
|
||||
|
||||
## Build + register (dev loop)
|
||||
|
||||
```powershell
|
||||
$repo = "X:\GitHub\PowerToys"; $Plat = "ARM64"; $Cfg = "Debug" # or x64 / Release
|
||||
|
||||
# Build AP only (C#; reuses existing C++ outputs):
|
||||
dotnet restore "$repo\src\modules\AdvancedPaste\AdvancedPaste\AdvancedPaste.csproj" /p:Platform=$Plat
|
||||
& "$repo\tools\build\build.cmd" -Path "$repo\src\modules\AdvancedPaste\AdvancedPaste" `
|
||||
-Platform $Plat -Configuration $Cfg /p:BuildProjectReferences=false
|
||||
|
||||
# Register the dev sparse package (creates + trusts a dev cert, grants identity):
|
||||
pwsh -ExecutionPolicy Bypass -File "$repo\src\PackageIdentity\BuildSparsePackage.ps1" `
|
||||
-Platform $Plat -Configuration $Cfg -DevRegister
|
||||
# Expect: PublisherId djwsxzxb4ksa8, IsDevelopmentMode True
|
||||
```
|
||||
|
||||
## Check the API
|
||||
|
||||
`PowerToys.AdvancedPaste.exe` is a **GUI-subsystem** app — run directly in a console it prints
|
||||
nothing and returns no exit code. **Redirect** stdout/stderr and wait:
|
||||
|
||||
```powershell
|
||||
$exe = "$repo\$Plat\$Cfg\WinUI3Apps\PowerToys.AdvancedPaste.exe"
|
||||
$o = "$env:TEMP\ap.out"; $e = "$env:TEMP\ap.err"
|
||||
$p = Start-Process $exe '--check-phi-silica' -Wait -PassThru -WindowStyle Hidden `
|
||||
-RedirectStandardOutput $o -RedirectStandardError $e
|
||||
"exit=$($p.ExitCode) stdout=$((Get-Content $o -Raw).Trim())"
|
||||
Get-Content $e -Raw # stderr: [phi-silica] LAF unlock status: <…>; ReadyState: <…>
|
||||
```
|
||||
|
||||
| `--check-phi-silica` | `--prepare-phi-silica` (downloads the model) |
|
||||
|----------------------|----------------------------------------------|
|
||||
| `0` Available · `1` NotReady · `2` NotSupported / unlock failed | `0` Ready · `1` Failed · `2` NotSupported |
|
||||
|
||||
`--check` only reads state; use `--prepare` to trigger the model download (`EnsureReadyAsync`).
|
||||
On failure it prints the `HRESULT` to stderr.
|
||||
|
||||
Confirm the running AP has identity:
|
||||
|
||||
```powershell
|
||||
$apPid = (Get-Process PowerToys.AdvancedPaste -EA SilentlyContinue | Select-Object -First 1).Id
|
||||
if ($apPid) { & "$repo\src\PackageIdentity\Check-ProcessIdentity.ps1" -ProcessId $apPid }
|
||||
# Expect a PFN ending in the publisher id that matches the baked attestation
|
||||
```
|
||||
|
||||
## What actually breaks it
|
||||
|
||||
- **Missing `.winmd` (most important).** The Windows AI runtime resolves
|
||||
`Microsoft.Windows.AI*.winmd` from the app folder at runtime. If they aren't deployed,
|
||||
`GetReadyState()` returns `NotReady` and `EnsureReadyAsync()` fails with
|
||||
`RO_E_METADATA_NAME_NOT_FOUND` (`0x8000000F`) — even though identity, token, and the AI DLLs
|
||||
are all correct. The build emits these winmd into `WinUI3Apps\`; the **installer must harvest
|
||||
them** (`*.winmd` is in the inclusion list of
|
||||
[`generateAllFileComponents.ps1`](../../../installer/PowerToysSetupVNext/generateAllFileComponents.ps1)).
|
||||
Classic symptom: "works from the build output but not from the installer" → check that the
|
||||
installed `WinUI3Apps\` contains `Microsoft.Windows.AI*.winmd`.
|
||||
- **Dev/prod mismatch.** A dev-cred exe running against a prod sparse package (or vice versa)
|
||||
makes the LAF unlock silently return `Unavailable`. Keep the exe and the registered package
|
||||
the same flavor, and verify with the attestation == publisherId check above.
|
||||
- **Forgot to redirect.** `--check-phi-silica` in a console prints nothing — that's the
|
||||
GUI-subsystem quirk, not a result.
|
||||
|
||||
## Cleanup
|
||||
|
||||
```powershell
|
||||
pwsh -ExecutionPolicy Bypass -File "$repo\src\PackageIdentity\BuildSparsePackage.ps1" -Unregister
|
||||
```
|
||||
|
||||
⚠️ This removes any `Microsoft.PowerToys.SparseApp` registration, **including a prod one** from
|
||||
an installer — reinstall/repair PowerToys to restore it.
|
||||
@@ -33,7 +33,81 @@ See the `ExecutePasteFormatAsync(PasteFormat, PasteActionSource)` method in `Opt
|
||||
|
||||
## Debugging
|
||||
|
||||
TODO: Add debugging information
|
||||
Advanced Paste is an unpackaged, self-contained WinUI 3 app (`PowerToys.AdvancedPaste.exe`). To call Windows AI APIs (Phi Silica / `Microsoft.Windows.AI.Text.LanguageModel`) it acquires **package identity** at runtime via a shared sparse MSIX package (`Microsoft.PowerToys.SparseApp`).
|
||||
|
||||
### Running and attaching the debugger
|
||||
|
||||
1. Set the **Runner** project (`src/runner`) as the startup project in Visual Studio.
|
||||
2. Launch the Runner (F5). This starts the PowerToys tray icon and loads all module interfaces.
|
||||
3. Open Settings (right-click tray icon → Settings) and enable the **Advanced Paste** module if it isn't already. The module launches `PowerToys.AdvancedPaste.exe` in the background immediately.
|
||||
4. In Visual Studio, go to **Debug → Attach to Process** (`Ctrl+Alt+P`) and attach to `PowerToys.AdvancedPaste.exe` (select **Managed (.NET Core)** debugger).
|
||||
|
||||
Alternatively, use the VS Code launch configuration **"Run AdvancedPaste"** from [.vscode/launch.json](/.vscode/launch.json) to launch the exe directly — but note that without the Runner, IPC and hotkeys won't work.
|
||||
|
||||
### Sparse package identity (local development)
|
||||
|
||||
#### Why is this needed?
|
||||
|
||||
- The `LanguageModel` API requires a Limited Access Feature (LAF) unlock, which only succeeds when the calling process has a matching package identity.
|
||||
- Advanced Paste is an unpackaged, self-contained WinUI 3 app. The sparse package grants it identity without converting it to a full MSIX.
|
||||
- The csproj uses `<ProjectPriFileName>PowerToys.AdvancedPaste.pri</ProjectPriFileName>` (matching the convention of other WinUI3 apps like ImageResizer). This requires WindowsAppSDK Foundation >= 2.0.22 ([PR #6376](https://github.com/microsoft/WindowsAppSDK/pull/6376)) which fixes MRT PRI lookup under sparse identity so `Application.LoadComponent` resolves custom-named PRI files instead of hard-coding `resources.pri`.
|
||||
|
||||
#### One-step dev setup
|
||||
|
||||
```powershell
|
||||
pwsh src/PackageIdentity/BuildSparsePackage.ps1 -Platform ARM64 -Configuration Debug -DevRegister
|
||||
```
|
||||
|
||||
`-DevRegister`:
|
||||
1. Generates a dev certificate under `src/PackageIdentity/.user/` (first run only).
|
||||
2. Auto-imports that certificate into `CurrentUser\TrustedPeople` and `CurrentUser\Root` so the OS grants sparse identity to AP (without trust, `GetPackageFamilyName` returns `APPMODEL_ERROR_NO_PACKAGE` and LAF unlock silently fails).
|
||||
3. Removes any prior registration.
|
||||
4. Rewrites the publisher in a temp copy of `AppxManifest.xml` to match the dev cert subject.
|
||||
5. Registers via `Add-AppxPackage -Register … -ExternalLocation X:\…\<Platform>\<Config>\WinUI3Apps`.
|
||||
|
||||
After registration verify:
|
||||
|
||||
```powershell
|
||||
$pkg = Get-AppxPackage -Name '*SparseApp*'
|
||||
$pkg.PackageFamilyName # Microsoft.PowerToys.SparseApp_<PublisherId>
|
||||
$pkg.PublisherId # djwsxzxb4ksa8
|
||||
$pkg.IsDevelopmentMode # True
|
||||
```
|
||||
|
||||
Confirm AP picks up sparse identity at runtime:
|
||||
|
||||
```powershell
|
||||
& 'ARM64\Debug\WinUI3Apps\PowerToys.AdvancedPaste.exe' --check-phi-silica
|
||||
# Exit 0 = Available, 1 = NotReady, 2 = NotSupported
|
||||
```
|
||||
|
||||
Re-register after rebuilding AP, changing `src/PackageIdentity/AppxManifest.xml`, or switching platforms/configurations by re-running the same command. Unregister with `-Unregister`.
|
||||
|
||||
#### Troubleshooting
|
||||
|
||||
| Problem | Cause | Fix |
|
||||
|---------|-------|-----|
|
||||
| `GetPackageFamilyName` returns `APPMODEL_ERROR_NO_PACKAGE` (15700) at runtime; LAF unlock returns `Unavailable` | Dev certificate not trusted (or sparse package not registered) | Re-run `BuildSparsePackage.ps1 -DevRegister` — auto-imports the cert into `TrustedPeople` and `Root`. |
|
||||
| `Microsoft.UI.Xaml.dll` crash with `0xC000027B` (class-not-registered) on AP or Settings startup | `<Application>` `Executable` path in `src/PackageIdentity/AppxManifest.xml` does not resolve under the registered `ExternalLocation` (`<Config>\WinUI3Apps\`) | Confirm every `Executable` is relative to `WinUI3Apps\` (per #47177) and the file exists under the build output. |
|
||||
| AP launches but never shows a window when triggered via hotkey | Runner's pipe-server wait timed out before AP's cold-start finished bootstrapping WinAppSDK + DI host | Already mitigated by the 15 s pipe timeout in `AdvancedPasteProcessManager.cpp`; warm-start launches connect in well under 1 s. |
|
||||
| `XamlParseException` / `ms-appx:///Microsoft.UI.Xaml/Themes/…` not found | WindowsAppSDK Foundation < 2.0.22; MRT can't resolve custom PRI name under sparse identity | Ensure `Microsoft.WindowsAppSDK.Foundation` >= 2.0.22 in `Directory.Packages.props`. |
|
||||
|
||||
### How Settings UI checks Phi Silica availability
|
||||
|
||||
Settings UI does not have sparse package identity. To check whether Phi Silica is available, it launches Advanced Paste as a short-lived subprocess:
|
||||
|
||||
```
|
||||
PowerToys.AdvancedPaste.exe --check-phi-silica
|
||||
```
|
||||
|
||||
`Program.Main` recognizes this flag, calls `PhiSilicaLafHelper.TryUnlock()` + `LanguageModel.GetReadyState()`, prints one of `Available` / `NotReady` / `NotSupported` to stdout, and exits with the matching code (0/1/2). Settings reads stdout with a 10 s wait. Because each call is a fresh process, transient `Unavailable` results are not cached across checks.
|
||||
|
||||
### See also
|
||||
|
||||
- [Phi Silica local testing & troubleshooting guide](advancedpaste-phisilica-local-testing.md) — layer-by-layer diagnostics for Phi Silica availability
|
||||
- [`src/PackageIdentity/readme.md`](/src/PackageIdentity/readme.md) — full sparse package documentation
|
||||
- [microsoft/microsoft-ui-xaml#10856](https://github.com/microsoft/microsoft-ui-xaml/issues/10856) — original WinUI sparse-identity PRI bug
|
||||
- [microsoft/WindowsAppSDK#6376](https://github.com/microsoft/WindowsAppSDK/pull/6376) — MRT sparse PRI fix (Foundation >= 2.0.22)
|
||||
|
||||
## Settings
|
||||
|
||||
|
||||
@@ -28,12 +28,23 @@ Function Generate-FileList() {
|
||||
|
||||
$fileExclusionList = @("*.pdb", "*.lastcodeanalysissucceeded", "createdump.exe", "powertoys.exe")
|
||||
|
||||
$fileInclusionList = @("*.dll", "*.exe", "*.json", "*.msix", "*.png", "*.gif", "*.ico", "*.cur", "*.svg", "index.html", "reg.js", "gitignore.js", "srt.js", "monacoSpecialLanguages.js", "customTokenThemeRules.js", "*.pri", "*.yml")
|
||||
# *.winmd: WinRT metadata for the Windows App SDK AI APIs (Phi Silica, Imaging, etc.). The AI
|
||||
# runtime resolves these from the app directory at runtime, so they must ship with the product.
|
||||
# Without them GetReadyState() reports NotReady and EnsureReadyAsync() fails with
|
||||
# RO_E_METADATA_NAME_NOT_FOUND (0x8000000F). The build already emits them into the app output
|
||||
# (e.g. WinUI3Apps); they were previously dropped here because the harvest didn't include them.
|
||||
$fileInclusionList = @("*.dll", "*.exe", "*.json", "*.msix", "*.png", "*.gif", "*.ico", "*.cur", "*.svg", "index.html", "reg.js", "gitignore.js", "srt.js", "monacoSpecialLanguages.js", "customTokenThemeRules.js", "*.pri", "*.yml", "*.winmd")
|
||||
|
||||
# MFC DLLs leak into the output via WindowsAppSDKSelfContained but no PowerToys binary imports them.
|
||||
# Verified with dumpbin /dependents across all 2176 binaries — zero consumers.
|
||||
$fileExclusionList += @("mfc140.dll", "mfc140u.dll", "mfcm140.dll", "mfcm140u.dll")
|
||||
|
||||
# Microsoft.CommandPalette.Extensions.winmd already has a dedicated WiX component
|
||||
# (Microsoft_CommandPalette_Extensions_winmd in BaseApplications.wxs, placed in WinUI3Apps for
|
||||
# CmdPal's WinRT resolution). Exclude it from the generic *.winmd harvest so it isn't declared
|
||||
# by two components (WIX ICE30 "installed by two different components" breaks ref-counting).
|
||||
$fileExclusionList += @("Microsoft.CommandPalette.Extensions.winmd")
|
||||
|
||||
$dllsToIgnore = @("System.CodeDom.dll", "WindowsBase.dll")
|
||||
|
||||
if ($fileDepsJson -eq [string]::Empty) {
|
||||
|
||||
@@ -58,6 +58,16 @@
|
||||
AppListEntry="none">
|
||||
</uap:VisualElements>
|
||||
</Application>
|
||||
<Application Id="PowerToys.AdvancedPasteUI" Executable="PowerToys.AdvancedPaste.exe" EntryPoint="Windows.FullTrustApplication">
|
||||
<uap:VisualElements
|
||||
DisplayName="PowerToys.AdvancedPaste"
|
||||
Description="PowerToys Advanced Paste"
|
||||
BackgroundColor="transparent"
|
||||
Square150x150Logo="Images\Square150x150Logo.png"
|
||||
Square44x44Logo="Images\Square44x44Logo.png"
|
||||
AppListEntry="none">
|
||||
</uap:VisualElements>
|
||||
</Application>
|
||||
<Application Id="PowerToys.CmdPalExtension" Executable="Microsoft.CmdPal.Ext.PowerToys.exe" EntryPoint="Windows.FullTrustApplication">
|
||||
<uap:VisualElements
|
||||
DisplayName="PowerToys.CommandPaletteExtension"
|
||||
|
||||
@@ -13,7 +13,9 @@ Param(
|
||||
[switch]$Clean,
|
||||
[switch]$ForceCert,
|
||||
[switch]$NoSign,
|
||||
[switch]$CIBuild
|
||||
[switch]$CIBuild,
|
||||
[switch]$DevRegister,
|
||||
[switch]$Unregister
|
||||
)
|
||||
|
||||
# PowerToys sparse packaging helper.
|
||||
@@ -22,6 +24,19 @@ Param(
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
|
||||
# Handle -Unregister early exit
|
||||
if ($Unregister) {
|
||||
$existing = Get-AppxPackage -Name 'Microsoft.PowerToys.SparseApp' -ErrorAction SilentlyContinue
|
||||
if ($existing) {
|
||||
Write-Host "Removing Microsoft.PowerToys.SparseApp..."
|
||||
$existing | Remove-AppxPackage
|
||||
Write-Host "Done."
|
||||
} else {
|
||||
Write-Host "Microsoft.PowerToys.SparseApp is not registered."
|
||||
}
|
||||
exit 0
|
||||
}
|
||||
|
||||
$isCIBuild = $false
|
||||
if ($CIBuild.IsPresent) {
|
||||
$isCIBuild = $true
|
||||
@@ -421,3 +436,85 @@ $winUI3AppsDir = Join-Path $outDir "WinUI3Apps"
|
||||
Write-BuildLog "Register sparse package:" -Level Info
|
||||
Write-BuildLog " Add-AppxPackage -Path `"$msixPath`" -ExternalLocation `"$winUI3AppsDir`"" -Level Warning
|
||||
Write-BuildLog "(If already installed and you changed manifest only): Add-AppxPackage -Register `"$manifestPath`" -ExternalLocation `"$winUI3AppsDir`" -ForceApplicationShutdown" -Level Warning
|
||||
|
||||
# -DevRegister: automatically register the sparse package for local development
|
||||
if ($DevRegister) {
|
||||
Write-BuildLog "`nDev registration requested..." -Level Info
|
||||
|
||||
# Ensure the dev certificate is trusted (CurrentUser\TrustedPeople + CurrentUser\Root).
|
||||
# Without trust, sparse identity lookup silently fails at runtime (GetPackageFamilyName
|
||||
# returns APPMODEL_ERROR_NO_PACKAGE) so LAF unlocks like Phi Silica return Unavailable.
|
||||
if (Test-Path $CertCerFile) {
|
||||
try {
|
||||
$devCert = (Get-PfxCertificate -FilePath $CertCerFile -ErrorAction Stop)
|
||||
$devThumbprint = $devCert.Thumbprint
|
||||
foreach ($store in @('TrustedPeople', 'Root')) {
|
||||
$storePath = "Cert:\CurrentUser\$store"
|
||||
$present = Get-ChildItem $storePath -ErrorAction SilentlyContinue |
|
||||
Where-Object { $_.Thumbprint -eq $devThumbprint }
|
||||
if (-not $present) {
|
||||
Write-BuildLog "Importing dev certificate ($devThumbprint) into CurrentUser\$store" -Level Info
|
||||
Import-Certificate -FilePath $CertCerFile -CertStoreLocation $storePath | Out-Null
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
Write-BuildLog "Failed to import dev certificate into trust stores: $($_.Exception.Message)" -Level Warning
|
||||
Write-BuildLog "Sparse identity may not be granted at runtime; LAF unlocks (e.g., Phi Silica) will fail." -Level Warning
|
||||
}
|
||||
} else {
|
||||
Write-BuildLog "Certificate .cer file not found at $CertCerFile; skipping trust-store import." -Level Warning
|
||||
}
|
||||
|
||||
# Remove existing registration
|
||||
$existing = Get-AppxPackage -Name $script:Config.IdentityName -ErrorAction SilentlyContinue
|
||||
if ($existing) {
|
||||
Write-BuildLog "Removing existing registration..." -Level Info
|
||||
$existing | Remove-AppxPackage
|
||||
}
|
||||
|
||||
# Create a temp manifest with the dev publisher for -Register
|
||||
$devRegDir = Join-Path ([System.IO.Path]::GetTempPath()) "PowerToysSparseDevReg"
|
||||
if (Test-Path $devRegDir) { Remove-Item $devRegDir -Recurse -Force }
|
||||
New-Item -ItemType Directory -Path $devRegDir -Force | Out-Null
|
||||
|
||||
try {
|
||||
Copy-Item $manifestPath (Join-Path $devRegDir 'AppxManifest.xml')
|
||||
$imagesDir = Join-Path $sparseDir 'Images'
|
||||
if (Test-Path $imagesDir) {
|
||||
Copy-Item $imagesDir (Join-Path $devRegDir 'Images') -Recurse
|
||||
}
|
||||
|
||||
$devManifest = Join-Path $devRegDir 'AppxManifest.xml'
|
||||
[xml]$devXml = Get-Content $devManifest -Raw
|
||||
$devIdentity = $devXml.Package.Identity
|
||||
|
||||
if ($devIdentity.Publisher -ne $script:Config.CertSubject) {
|
||||
Write-BuildLog "Rewriting publisher for dev registration" -Level Info
|
||||
$devIdentity.SetAttribute('Publisher', $script:Config.CertSubject)
|
||||
$devXml.Save($devManifest)
|
||||
}
|
||||
|
||||
Write-BuildLog "Registering with ExternalLocation: $winUI3AppsDir" -Level Info
|
||||
Add-AppxPackage -Register $devManifest -ExternalLocation $winUI3AppsDir
|
||||
|
||||
$pkg = Get-AppxPackage -Name $script:Config.IdentityName -ErrorAction SilentlyContinue
|
||||
if ($pkg) {
|
||||
Write-BuildLog "Dev registration successful:" -Level Success
|
||||
Write-BuildLog " Publisher: $($pkg.Publisher)" -Level Info
|
||||
Write-BuildLog " PublisherId: $($pkg.PublisherId)" -Level Info
|
||||
Write-BuildLog " IsDevelopmentMode: $($pkg.IsDevelopmentMode)" -Level Info
|
||||
Write-BuildLog " InstallLocation: $($pkg.InstallLocation)" -Level Info
|
||||
|
||||
if ($pkg.PublisherId -ne 'djwsxzxb4ksa8') {
|
||||
Write-BuildLog "PublisherId mismatch! Expected 'djwsxzxb4ksa8', got '$($pkg.PublisherId)'. LAF unlock will fail." -Level Warning
|
||||
}
|
||||
} else {
|
||||
Write-BuildLog "Dev registration failed — package not found." -Level Error
|
||||
exit 1
|
||||
}
|
||||
} finally {
|
||||
if (Test-Path $devRegDir) {
|
||||
Remove-Item $devRegDir -Recurse -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
9
src/PhiSilicaLaf.props
Normal file
9
src/PhiSilicaLaf.props
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<!-- Phi Silica Limited Access Feature credentials.
|
||||
Local dev defaults below; overridden by /p: in release pipelines. -->
|
||||
<PhiSilicaLafToken Condition="'$(PhiSilicaLafToken)'==''">RmToMMYJHZkQSrKP5lWesA==</PhiSilicaLafToken>
|
||||
<PhiSilicaLafAttestation Condition="'$(PhiSilicaLafAttestation)'==''">djwsxzxb4ksa8</PhiSilicaLafAttestation>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -55,6 +55,22 @@ internal sealed class IntegrationTestUserSettings : IUserSettings
|
||||
|
||||
public IReadOnlyList<PasteFormats> AdditionalActions => _additionalActions;
|
||||
|
||||
public string FixSpellingAndGrammarPrompt => string.Empty;
|
||||
|
||||
public string FixSpellingAndGrammarSystemPrompt => string.Empty;
|
||||
|
||||
public string FixSpellingAndGrammarProviderId => string.Empty;
|
||||
|
||||
public bool FixSpellingAndGrammarCoachingEnabled => false;
|
||||
|
||||
public bool FixSpellingAndGrammarCoachingShortcutSet => false;
|
||||
|
||||
public string FixSpellingAndGrammarCoachingPrompt => string.Empty;
|
||||
|
||||
public string FixSpellingAndGrammarCoachingSystemPrompt => string.Empty;
|
||||
|
||||
public string FixSpellingAndGrammarCoachingProviderId => string.Empty;
|
||||
|
||||
public PasteAIConfiguration PasteAIConfiguration => _configuration;
|
||||
|
||||
public event EventHandler Changed;
|
||||
|
||||
@@ -30,6 +30,16 @@ public sealed class CustomActionKernelQueryCacheServiceTests
|
||||
private static readonly CacheValue TestValue = new([new(PasteFormats.PlainText, [])]);
|
||||
private static readonly CacheValue TestValue2 = new([new(PasteFormats.KernelQuery, new() { { "a", "b" }, { "c", "d" } })]);
|
||||
|
||||
private static string LocalizeResourceId(string resourceId) => resourceId switch
|
||||
{
|
||||
"PasteAsPlainText" => "Paste as plain text",
|
||||
"PasteAsMarkdown" => MarkdownTestKey.Prompt,
|
||||
"PasteAsJson" => JSONTestKey.Prompt,
|
||||
"PasteAsTxtFile" => PasteAsTxtFileKey.Prompt,
|
||||
"PasteAsPngFile" => PasteAsPngFileKey.Prompt,
|
||||
_ => resourceId,
|
||||
};
|
||||
|
||||
private CustomActionKernelQueryCacheService _cacheService;
|
||||
private Mock<IUserSettings> _userSettings;
|
||||
private MockFileSystem _fileSystem;
|
||||
@@ -41,7 +51,7 @@ public sealed class CustomActionKernelQueryCacheServiceTests
|
||||
UpdateUserActions([], []);
|
||||
|
||||
_fileSystem = new();
|
||||
_cacheService = new(_userSettings.Object, _fileSystem);
|
||||
_cacheService = new(_userSettings.Object, _fileSystem, LocalizeResourceId);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -122,7 +132,7 @@ public sealed class CustomActionKernelQueryCacheServiceTests
|
||||
await _cacheService.WriteAsync(JSONTestKey, TestValue);
|
||||
await _cacheService.WriteAsync(MarkdownTestKey, TestValue2);
|
||||
|
||||
_cacheService = new(_userSettings.Object, _fileSystem); // recreate using same mock file-system to simulate app restart
|
||||
_cacheService = new(_userSettings.Object, _fileSystem, LocalizeResourceId); // recreate using same mock file-system to simulate app restart
|
||||
|
||||
AssertAreEqual(TestValue, _cacheService.ReadOrNull(JSONTestKey));
|
||||
AssertAreEqual(TestValue2, _cacheService.ReadOrNull(MarkdownTestKey));
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<!-- Look at Directory.Build.props in root for common stuff as well -->
|
||||
<Import Project="$(RepoRoot)src\CmdPalVersion.props" />
|
||||
<Import Project="$(RepoRoot)src\Common.Dotnet.CsWinRT.props" />
|
||||
<Import Project="$(RepoRoot)src\Common.SelfContained.props" />
|
||||
|
||||
@@ -8,7 +9,7 @@
|
||||
<OutputPath>$(RepoRoot)$(Platform)\$(Configuration)\WinUI3Apps</OutputPath>
|
||||
<UseWinUI>true</UseWinUI>
|
||||
<ApplicationIcon>Assets\AdvancedPaste\AdvancedPaste.ico</ApplicationIcon>
|
||||
<ApplicationManifest>app.manifest</ApplicationManifest>
|
||||
<ApplicationManifest>AdvancedPaste.dev.manifest</ApplicationManifest>
|
||||
<GenerateSatelliteAssembliesForCore>true</GenerateSatelliteAssembliesForCore>
|
||||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
|
||||
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
|
||||
@@ -20,9 +21,13 @@
|
||||
<RootNamespace>AdvancedPaste</RootNamespace>
|
||||
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<!-- MRT from windows app sdk will search for a pri file with the same name of the module before defaulting to resources.pri -->
|
||||
<ProjectPriFileName>PowerToys.AdvancedPaste.pri</ProjectPriFileName>
|
||||
<DefineConstants>DISABLE_XAML_GENERATED_MAIN,TRACE</DefineConstants>
|
||||
<Version>$(AdvancedPasteVersion)</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(CIBuild)'=='true'">
|
||||
<ApplicationManifest>AdvancedPaste.prod.manifest</ApplicationManifest>
|
||||
</PropertyGroup>
|
||||
|
||||
<!-- See https://learn.microsoft.com/windows/apps/develop/platform/csharp-winrt/net-projection-from-cppwinrt-component for more info -->
|
||||
@@ -32,6 +37,25 @@
|
||||
<ErrorOnDuplicatePublishOutputFiles>false</ErrorOnDuplicatePublishOutputFiles>
|
||||
</PropertyGroup>
|
||||
|
||||
<!-- BODGY: XES Versioning and WinAppSDK get into a fight about the app manifest, which breaks WinAppSDK. -->
|
||||
<Target Name="RearrangeXefVersioningAndWinAppSDKResourceGeneration" DependsOnTargets="GetNewAppManifestValues;CreateWinRTRegistration" BeforeTargets="XesWriteVersionInfoResourceFile">
|
||||
<PropertyGroup>
|
||||
<OriginalApplicationManifest>$(ApplicationManifest.Replace("$(MSBuildProjectDirectory)\",""))</OriginalApplicationManifest>
|
||||
</PropertyGroup>
|
||||
<Message Importance="High" Text="Updated final manifest path to $(OriginalApplicationManifest)" />
|
||||
</Target>
|
||||
|
||||
<!-- Generate PhiSilicaLafCredentials from MSBuild properties (PhiSilicaLaf.props / CI overrides). -->
|
||||
<Target Name="GeneratePhiSilicaLafCredentials" BeforeTargets="BeforeCompile;CoreCompile;XamlPreCompile">
|
||||
<PropertyGroup>
|
||||
<PhiSilicaLafCredentialsFile>$(IntermediateOutputPath)PhiSilicaLafCredentials.g.cs</PhiSilicaLafCredentialsFile>
|
||||
</PropertyGroup>
|
||||
<WriteLinesToFile File="$(PhiSilicaLafCredentialsFile)" Overwrite="true" Lines="// <auto-generated/>;namespace AdvancedPaste%3B;;internal static class PhiSilicaLafCredentials;{; internal const string Token = "$(PhiSilicaLafToken)"%3B; internal const string Attestation = "$(PhiSilicaLafAttestation)"%3B;}" />
|
||||
<ItemGroup>
|
||||
<Compile Include="$(PhiSilicaLafCredentialsFile)" Condition="!@(Compile->AnyHaveMetadataValue('Identity', '$(PhiSilicaLafCredentialsFile)'))" />
|
||||
</ItemGroup>
|
||||
</Target>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="AdvancedPasteXAML\Controls\ClipboardHistoryItemPreviewControl.xaml" />
|
||||
<None Remove="AdvancedPasteXAML\Controls\PromptBox.xaml" />
|
||||
@@ -66,9 +90,9 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" />
|
||||
<PackageReference Include="Microsoft.SemanticKernel" />
|
||||
<PackageReference Include="Microsoft.WindowsAppSDK" />
|
||||
<PackageReference Include="Microsoft.WindowsAppSDK.AI" />
|
||||
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" />
|
||||
<PackageReference Include="System.ClientModel" />
|
||||
<PackageReference Include="Microsoft.Windows.Compatibility" />
|
||||
<PackageReference Include="Microsoft.Windows.CsWin32" />
|
||||
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" />
|
||||
<PackageReference Include="ReverseMarkdown" />
|
||||
@@ -85,7 +109,8 @@
|
||||
<!-- TODO: fix issues and reenable -->
|
||||
<!-- These are caused by streamjsonrpc dependency on Microsoft.VisualStudio.Threading.Analyzers -->
|
||||
<!-- We might want to add that to the project and fix the issues as well -->
|
||||
<NoWarn>VSTHRD002;VSTHRD110;VSTHRD100;VSTHRD200;VSTHRD101</NoWarn>
|
||||
<!-- CS8305: generated XamlTypeInfo references experimental ItemsView enums in WinAppSDK 2.2.x-experimental. -->
|
||||
<NoWarn>VSTHRD002;VSTHRD110;VSTHRD100;VSTHRD200;VSTHRD101;CS8305</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<!--
|
||||
@@ -97,6 +122,10 @@
|
||||
<ProjectCapability Include="Msix" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<SDKReference Include="Microsoft.VCLibs.Desktop, Version=14.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
|
||||
<_Parameter1>AdvancedPaste.UnitTests</_Parameter1>
|
||||
@@ -104,8 +133,6 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<!-- HACK: Common.UI is referenced, even if it is not used, to force dll versions to be the same as in other projects that use it. It's still unclear why this is the case, but this is need for flattening the install directory. -->
|
||||
<ProjectReference Include="..\..\..\common\Common.UI\Common.UI.csproj" />
|
||||
<ProjectReference Include="..\..\..\common\ManagedCommon\ManagedCommon.csproj" />
|
||||
<ProjectReference Include="..\..\..\common\LanguageModelProvider\LanguageModelProvider.csproj" />
|
||||
<ProjectReference Include="..\..\..\common\GPOWrapper\GPOWrapper.vcxproj" />
|
||||
@@ -154,4 +181,5 @@
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<assembly manifestVersion="1.0"
|
||||
xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<assemblyIdentity version="1.0.0.0" name="AdvancedPaste.app"/>
|
||||
|
||||
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
|
||||
<application>
|
||||
<!-- Windows 10 -->
|
||||
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
|
||||
</application>
|
||||
</compatibility>
|
||||
|
||||
<application xmlns="urn:schemas-microsoft-com:asm.v3">
|
||||
<windowsSettings>
|
||||
<!-- The combination of below two tags have the following effect:
|
||||
1) Per-Monitor for >= Windows 10 Anniversary Update
|
||||
2) System < Windows 10 Anniversary Update
|
||||
-->
|
||||
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
|
||||
</windowsSettings>
|
||||
</application>
|
||||
|
||||
<msix xmlns="urn:schemas-microsoft-com:msix.v1"
|
||||
publisher="CN=PowerToys Dev, O=PowerToys, L=Redmond, S=Washington, C=US"
|
||||
packageName="Microsoft.PowerToys.SparseApp"
|
||||
applicationId="PowerToys.AdvancedPasteUI" />
|
||||
</assembly>
|
||||
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<assembly manifestVersion="1.0"
|
||||
xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<assemblyIdentity version="1.0.0.0" name="AdvancedPaste.app"/>
|
||||
|
||||
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
|
||||
<application>
|
||||
<!-- Windows 10 -->
|
||||
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
|
||||
</application>
|
||||
</compatibility>
|
||||
|
||||
<application xmlns="urn:schemas-microsoft-com:asm.v3">
|
||||
<windowsSettings>
|
||||
<!-- The combination of below two tags have the following effect:
|
||||
1) Per-Monitor for >= Windows 10 Anniversary Update
|
||||
2) System < Windows 10 Anniversary Update
|
||||
-->
|
||||
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
|
||||
</windowsSettings>
|
||||
</application>
|
||||
|
||||
<msix xmlns="urn:schemas-microsoft-com:msix.v1"
|
||||
publisher="CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US"
|
||||
packageName="Microsoft.PowerToys.SparseApp"
|
||||
applicationId="PowerToys.AdvancedPasteUI" />
|
||||
</assembly>
|
||||
@@ -188,14 +188,23 @@ namespace AdvancedPaste
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!AdditionalActionIPCKeys.TryGetValue(messageParts[1], out PasteFormats pasteFormat))
|
||||
const string coachingSuffix = "-coaching";
|
||||
var actionKey = messageParts[1];
|
||||
bool forceCoaching = actionKey.EndsWith(coachingSuffix, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
if (forceCoaching)
|
||||
{
|
||||
actionKey = actionKey[..^coachingSuffix.Length];
|
||||
}
|
||||
|
||||
if (!AdditionalActionIPCKeys.TryGetValue(actionKey, out PasteFormats pasteFormat))
|
||||
{
|
||||
Logger.LogWarning($"Unexpected additional action type {messageParts[1]}");
|
||||
}
|
||||
else
|
||||
{
|
||||
await ShowWindow();
|
||||
await viewModel.ExecutePasteFormatAsync(pasteFormat, PasteActionSource.GlobalKeyboardShortcut);
|
||||
await viewModel.ExecutePasteFormatAsync(pasteFormat, PasteActionSource.GlobalKeyboardShortcut, forceCoaching);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -382,6 +382,7 @@
|
||||
<RowDefinition Height="*" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<Grid
|
||||
@@ -438,13 +439,32 @@
|
||||
TextWrapping="Wrap" />
|
||||
</ScrollViewer>
|
||||
</Grid>
|
||||
<Rectangle
|
||||
<StackPanel
|
||||
Grid.Row="1"
|
||||
Padding="12,8,12,8"
|
||||
Background="{ThemeResource CardBackgroundFillColorSecondaryBrush}"
|
||||
Spacing="4"
|
||||
Visibility="{x:Bind ViewModel.HasCoachingExplanation, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}">
|
||||
<TextBlock
|
||||
x:Uid="CoachingExplanationTitle"
|
||||
FontWeight="SemiBold"
|
||||
Style="{StaticResource CaptionTextBlockStyle}" />
|
||||
<ScrollViewer MaxHeight="200">
|
||||
<TextBlock
|
||||
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
|
||||
IsTextSelectionEnabled="True"
|
||||
Style="{StaticResource CaptionTextBlockStyle}"
|
||||
Text="{x:Bind ViewModel.CoachingExplanation, Mode=OneWay}"
|
||||
TextWrapping="Wrap" />
|
||||
</ScrollViewer>
|
||||
</StackPanel>
|
||||
<Rectangle
|
||||
Grid.Row="2"
|
||||
Height="1"
|
||||
Fill="{ThemeResource DividerStrokeColorDefaultBrush}" />
|
||||
|
||||
<Grid
|
||||
Grid.Row="2"
|
||||
Grid.Row="3"
|
||||
Margin="12"
|
||||
ColumnSpacing="8">
|
||||
<Grid.ColumnDefinitions>
|
||||
|
||||
@@ -25,6 +25,22 @@ namespace AdvancedPaste.Settings
|
||||
|
||||
public IReadOnlyList<PasteFormats> AdditionalActions { get; }
|
||||
|
||||
public string FixSpellingAndGrammarPrompt { get; }
|
||||
|
||||
public string FixSpellingAndGrammarSystemPrompt { get; }
|
||||
|
||||
public string FixSpellingAndGrammarProviderId { get; }
|
||||
|
||||
public bool FixSpellingAndGrammarCoachingEnabled { get; }
|
||||
|
||||
public bool FixSpellingAndGrammarCoachingShortcutSet { get; }
|
||||
|
||||
public string FixSpellingAndGrammarCoachingPrompt { get; }
|
||||
|
||||
public string FixSpellingAndGrammarCoachingSystemPrompt { get; }
|
||||
|
||||
public string FixSpellingAndGrammarCoachingProviderId { get; }
|
||||
|
||||
public PasteAIConfiguration PasteAIConfiguration { get; }
|
||||
|
||||
public event EventHandler Changed;
|
||||
|
||||
@@ -157,8 +157,6 @@ namespace AdvancedPaste.Helpers
|
||||
{
|
||||
public int X;
|
||||
public int Y;
|
||||
|
||||
public static explicit operator System.Windows.Point(PointInter point) => new System.Windows.Point(point.X, point.Y);
|
||||
}
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
|
||||
@@ -46,6 +46,22 @@ namespace AdvancedPaste.Settings
|
||||
|
||||
public IReadOnlyList<AdvancedPasteCustomAction> CustomActions => _customActions;
|
||||
|
||||
public string FixSpellingAndGrammarPrompt { get; private set; } = string.Empty;
|
||||
|
||||
public string FixSpellingAndGrammarSystemPrompt { get; private set; } = string.Empty;
|
||||
|
||||
public string FixSpellingAndGrammarProviderId { get; private set; } = string.Empty;
|
||||
|
||||
public bool FixSpellingAndGrammarCoachingEnabled { get; private set; }
|
||||
|
||||
public bool FixSpellingAndGrammarCoachingShortcutSet { get; private set; }
|
||||
|
||||
public string FixSpellingAndGrammarCoachingPrompt { get; private set; } = string.Empty;
|
||||
|
||||
public string FixSpellingAndGrammarCoachingSystemPrompt { get; private set; } = string.Empty;
|
||||
|
||||
public string FixSpellingAndGrammarCoachingProviderId { get; private set; } = string.Empty;
|
||||
|
||||
public PasteAIConfiguration PasteAIConfiguration { get; private set; }
|
||||
|
||||
public UserSettings(IFileSystem fileSystem)
|
||||
@@ -113,10 +129,21 @@ namespace AdvancedPaste.Settings
|
||||
EnableClipboardPreview = properties.EnableClipboardPreview;
|
||||
PasteAIConfiguration = properties.PasteAIConfiguration ?? new PasteAIConfiguration();
|
||||
|
||||
var fixSpellingAction = properties.AdditionalActions.FixSpellingAndGrammar;
|
||||
FixSpellingAndGrammarPrompt = fixSpellingAction.Prompt ?? string.Empty;
|
||||
FixSpellingAndGrammarSystemPrompt = fixSpellingAction.SystemPrompt ?? string.Empty;
|
||||
FixSpellingAndGrammarProviderId = fixSpellingAction.ProviderId ?? string.Empty;
|
||||
FixSpellingAndGrammarCoachingEnabled = fixSpellingAction.CoachingEnabled;
|
||||
FixSpellingAndGrammarCoachingShortcutSet = fixSpellingAction.CoachingShortcut?.Code > 0;
|
||||
FixSpellingAndGrammarCoachingPrompt = fixSpellingAction.CoachingPrompt ?? string.Empty;
|
||||
FixSpellingAndGrammarCoachingSystemPrompt = fixSpellingAction.CoachingSystemPrompt ?? string.Empty;
|
||||
FixSpellingAndGrammarCoachingProviderId = fixSpellingAction.CoachingProviderId ?? string.Empty;
|
||||
|
||||
var sourceAdditionalActions = properties.AdditionalActions;
|
||||
(PasteFormats Format, IAdvancedPasteAction[] Actions)[] additionalActionFormats =
|
||||
[
|
||||
(PasteFormats.ImageToText, [sourceAdditionalActions.ImageToText]),
|
||||
(PasteFormats.FixSpellingAndGrammar, [sourceAdditionalActions.FixSpellingAndGrammar]),
|
||||
(PasteFormats.PasteAsTxtFile, [sourceAdditionalActions.PasteAsFile, sourceAdditionalActions.PasteAsFile.PasteAsTxtFile]),
|
||||
(PasteFormats.PasteAsPngFile, [sourceAdditionalActions.PasteAsFile, sourceAdditionalActions.PasteAsFile.PasteAsPngFile]),
|
||||
(PasteFormats.PasteAsHtmlFile, [sourceAdditionalActions.PasteAsFile, sourceAdditionalActions.PasteAsFile.PasteAsHtmlFile]),
|
||||
|
||||
@@ -24,20 +24,22 @@ public sealed class PasteFormat
|
||||
IsEnabled = SupportsClipboardFormats(clipboardFormats) && (isAIServiceEnabled || !Metadata.RequiresAIService);
|
||||
}
|
||||
|
||||
public static PasteFormat CreateStandardFormat(PasteFormats format, ClipboardFormat clipboardFormats, bool isAIServiceEnabled, Func<string, string> resourceLoader) =>
|
||||
public static PasteFormat CreateStandardFormat(PasteFormats format, ClipboardFormat clipboardFormats, bool isAIServiceEnabled, Func<string, string> resourceLoader, string providerId = null) =>
|
||||
new(format, clipboardFormats, isAIServiceEnabled)
|
||||
{
|
||||
Name = MetadataDict[format].ResourceId == null ? string.Empty : resourceLoader(MetadataDict[format].ResourceId),
|
||||
Prompt = string.Empty,
|
||||
IsSavedQuery = false,
|
||||
ProviderId = providerId ?? string.Empty,
|
||||
};
|
||||
|
||||
public static PasteFormat CreateCustomAIFormat(PasteFormats format, string name, string prompt, bool isSavedQuery, ClipboardFormat clipboardFormats, bool isAIServiceEnabled) =>
|
||||
public static PasteFormat CreateCustomAIFormat(PasteFormats format, string name, string prompt, bool isSavedQuery, ClipboardFormat clipboardFormats, bool isAIServiceEnabled, string providerId = null) =>
|
||||
new(format, clipboardFormats, isAIServiceEnabled)
|
||||
{
|
||||
Name = name,
|
||||
Prompt = prompt,
|
||||
IsSavedQuery = isSavedQuery,
|
||||
ProviderId = providerId ?? string.Empty,
|
||||
};
|
||||
|
||||
public PasteFormatMetadataAttribute Metadata => MetadataDict[Format];
|
||||
@@ -50,6 +52,8 @@ public sealed class PasteFormat
|
||||
|
||||
public string Prompt { get; private init; }
|
||||
|
||||
public string ProviderId { get; private init; } = string.Empty;
|
||||
|
||||
public bool IsSavedQuery { get; private init; }
|
||||
|
||||
public bool IsEnabled { get; private init; }
|
||||
|
||||
@@ -38,6 +38,17 @@ public enum PasteFormats
|
||||
KernelFunctionDescription = "Takes clipboard text and formats it as JSON text.")]
|
||||
Json,
|
||||
|
||||
[PasteFormatMetadata(
|
||||
IsCoreAction = false,
|
||||
ResourceId = "FixSpellingAndGrammar",
|
||||
IconGlyph = "\uE8E2",
|
||||
RequiresAIService = true,
|
||||
CanPreview = true,
|
||||
SupportedClipboardFormats = ClipboardFormat.Text,
|
||||
IPCKey = AdvancedPasteAdditionalActions.PropertyNames.FixSpellingAndGrammar,
|
||||
KernelFunctionDescription = "Fixes all spelling and grammar errors in the clipboard text and returns the corrected version.")]
|
||||
FixSpellingAndGrammar,
|
||||
|
||||
[PasteFormatMetadata(
|
||||
IsCoreAction = false,
|
||||
ResourceId = "ImageToText",
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using Windows.ApplicationModel;
|
||||
|
||||
namespace AdvancedPaste;
|
||||
|
||||
internal static class PhiSilicaLafHelper
|
||||
{
|
||||
private const string FeatureId = "com.microsoft.windows.ai.languagemodel";
|
||||
|
||||
private static readonly object _lock = new();
|
||||
private static bool _unlocked;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the status of the most recent <see cref="TryUnlock"/> attempt
|
||||
/// (e.g. Available, AvailableWithoutToken, Unavailable, or "Exception: ...").
|
||||
/// Exposed so callers can surface the real LAF result for diagnostics; the
|
||||
/// generic "Access is denied" from downstream model calls does not reveal it.
|
||||
/// </summary>
|
||||
public static string LastUnlockStatus { get; private set; } = "NotAttempted";
|
||||
|
||||
public static bool TryUnlock()
|
||||
{
|
||||
// Only cache a successful unlock. Negative results (Unavailable, Unknown, exceptions)
|
||||
// are often transient — e.g., AI feature stack not yet initialized after sign-in or
|
||||
// sparse identity not fully applied to a freshly-started process — and retrying on
|
||||
// the next call lets AP recover without restart.
|
||||
if (_unlocked)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
if (_unlocked)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var access = LimitedAccessFeatures.TryUnlockFeature(
|
||||
FeatureId,
|
||||
PhiSilicaLafCredentials.Token,
|
||||
PhiSilicaLafCredentials.Attestation + " has registered their use of com.microsoft.windows.ai.languagemodel with Microsoft and agrees to the terms of use.");
|
||||
|
||||
_unlocked = access.Status == LimitedAccessFeatureStatus.Available
|
||||
|| access.Status == LimitedAccessFeatureStatus.AvailableWithoutToken;
|
||||
|
||||
LastUnlockStatus = access.Status.ToString();
|
||||
Debug.WriteLine($"Phi Silica LAF unlock status: {access.Status}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LastUnlockStatus = "Exception: " + ex.Message;
|
||||
Debug.WriteLine($"Phi Silica LAF unlock failed: {ex.Message}");
|
||||
_unlocked = false;
|
||||
}
|
||||
|
||||
return _unlocked;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
||||
using ManagedCommon;
|
||||
@@ -14,16 +15,26 @@ namespace AdvancedPaste
|
||||
public static class Program
|
||||
{
|
||||
[STAThread]
|
||||
public static void Main(string[] args)
|
||||
public static int Main(string[] args)
|
||||
{
|
||||
Logger.InitializeLogger("\\AdvancedPaste\\Logs");
|
||||
|
||||
WinRT.ComWrappersSupport.InitializeComWrappers();
|
||||
|
||||
if (args.Contains("--check-phi-silica", StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
return CheckPhiSilicaAvailability();
|
||||
}
|
||||
|
||||
if (args.Contains("--prepare-phi-silica", StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
return PreparePhiSilica();
|
||||
}
|
||||
|
||||
if (PowerToys.GPOWrapper.GPOWrapper.GetConfiguredAdvancedPasteEnabledValue() == PowerToys.GPOWrapper.GpoRuleConfigured.Disabled)
|
||||
{
|
||||
Logger.LogWarning("Tried to start with a GPO policy setting the utility to always be disabled. Please contact your systems administrator.");
|
||||
return;
|
||||
return 1;
|
||||
}
|
||||
|
||||
var instanceKey = AppInstance.FindOrRegisterForKey("PowerToys_AdvancedPaste_Instance");
|
||||
@@ -41,6 +52,100 @@ namespace AdvancedPaste
|
||||
{
|
||||
Logger.LogWarning("Another instance of AdvancedPasteUI is running. Exiting.");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks Phi Silica availability without starting the WinUI app.
|
||||
/// Used by Settings UI to probe API status via subprocess.
|
||||
/// Exit codes: 0 = available, 1 = not ready (model needs download), 2 = not supported or error.
|
||||
/// </summary>
|
||||
private static int CheckPhiSilicaAvailability()
|
||||
{
|
||||
try
|
||||
{
|
||||
PhiSilicaLafHelper.TryUnlock();
|
||||
var readyState = Microsoft.Windows.AI.Text.LanguageModel.GetReadyState();
|
||||
|
||||
Console.Error.WriteLine($"[phi-silica] LAF unlock status: {PhiSilicaLafHelper.LastUnlockStatus}; ReadyState: {readyState}");
|
||||
|
||||
switch (readyState)
|
||||
{
|
||||
case Microsoft.Windows.AI.AIFeatureReadyState.Ready:
|
||||
Console.Out.WriteLine("Available");
|
||||
return 0;
|
||||
case Microsoft.Windows.AI.AIFeatureReadyState.NotReady:
|
||||
Console.Out.WriteLine("NotReady");
|
||||
return 1;
|
||||
default:
|
||||
// NotSupportedOnCurrentSystem, DisabledByUser, CapabilityMissing,
|
||||
// NotCompatibleWithSystemHardware, OSUpdateNeeded, or any future state:
|
||||
// the model isn't usable and "Download model" (EnsureReadyAsync) won't fix it.
|
||||
// CapabilityMissing in particular means the systemAIModels capability isn't
|
||||
// authorized for the app, so EnsureReadyAsync throws E_ACCESSDENIED (0x80070005).
|
||||
Console.Out.WriteLine("NotSupported");
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine(ex.Message);
|
||||
Console.Out.WriteLine("NotSupported");
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Triggers Phi Silica model preparation (download) without starting the WinUI app.
|
||||
/// Moves the model from NotReady to Ready by calling EnsureReadyAsync.
|
||||
/// Exit codes: 0 = ready, 1 = preparation failed, 2 = not supported or error.
|
||||
/// </summary>
|
||||
private static int PreparePhiSilica()
|
||||
{
|
||||
try
|
||||
{
|
||||
PhiSilicaLafHelper.TryUnlock();
|
||||
var readyState = Microsoft.Windows.AI.Text.LanguageModel.GetReadyState();
|
||||
|
||||
Console.Error.WriteLine($"[phi-silica] LAF unlock status: {PhiSilicaLafHelper.LastUnlockStatus}; ReadyState: {readyState}");
|
||||
|
||||
if (readyState is Microsoft.Windows.AI.AIFeatureReadyState.NotSupportedOnCurrentSystem
|
||||
or Microsoft.Windows.AI.AIFeatureReadyState.DisabledByUser)
|
||||
{
|
||||
Console.Out.WriteLine("NotSupported");
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (readyState == Microsoft.Windows.AI.AIFeatureReadyState.Ready)
|
||||
{
|
||||
Console.Out.WriteLine("Ready");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Run on a thread-pool (MTA) thread: the WinRT async operation does not
|
||||
// marshal correctly when blocked on from the [STAThread] entry point.
|
||||
var result = System.Threading.Tasks.Task.Run(
|
||||
() => Microsoft.Windows.AI.Text.LanguageModel.EnsureReadyAsync().AsTask()).GetAwaiter().GetResult();
|
||||
|
||||
if (result.Status != Microsoft.Windows.AI.AIFeatureReadyResultState.Success)
|
||||
{
|
||||
int hresult = result.ExtendedError?.HResult ?? 0;
|
||||
Console.Error.WriteLine($"[phi-silica] EnsureReadyAsync Status: {result.Status}; HRESULT: 0x{hresult:X8}; Message: {result.ExtendedError?.Message}");
|
||||
Console.Error.WriteLine(result.ExtendedError?.Message ?? result.Status.ToString());
|
||||
Console.Out.WriteLine("Failed");
|
||||
return 1;
|
||||
}
|
||||
|
||||
Console.Out.WriteLine("Ready");
|
||||
return 0;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine(ex.Message);
|
||||
Console.Out.WriteLine("NotSupported");
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,14 +33,21 @@ public sealed class CustomActionKernelQueryCacheService : IKernelQueryCacheServi
|
||||
private readonly IUserSettings _userSettings;
|
||||
private readonly IFileSystem _fileSystem;
|
||||
private readonly SettingsUtils _settingsUtil;
|
||||
private readonly Func<string, string> _getLocalizedString;
|
||||
|
||||
private static string Version => Assembly.GetExecutingAssembly()?.GetName()?.Version?.ToString() ?? string.Empty;
|
||||
|
||||
public CustomActionKernelQueryCacheService(IUserSettings userSettings, IFileSystem fileSystem)
|
||||
: this(userSettings, fileSystem, ResourceLoaderInstance.ResourceLoader.GetString)
|
||||
{
|
||||
}
|
||||
|
||||
internal CustomActionKernelQueryCacheService(IUserSettings userSettings, IFileSystem fileSystem, Func<string, string> getLocalizedString)
|
||||
{
|
||||
_userSettings = userSettings;
|
||||
_fileSystem = fileSystem;
|
||||
_settingsUtil = new SettingsUtils(fileSystem);
|
||||
_getLocalizedString = getLocalizedString;
|
||||
|
||||
_userSettings.Changed += OnUserSettingsChanged;
|
||||
|
||||
@@ -112,7 +119,7 @@ public sealed class CustomActionKernelQueryCacheService : IKernelQueryCacheServi
|
||||
let metadata = pair.Value
|
||||
where !string.IsNullOrEmpty(metadata.ResourceId)
|
||||
where metadata.IsCoreAction || _userSettings.AdditionalActions.Contains(format)
|
||||
select ResourceLoaderInstance.ResourceLoader.GetString(metadata.ResourceId);
|
||||
select _getLocalizedString(metadata.ResourceId);
|
||||
|
||||
var customActionPrompts = from customAction in _userSettings.CustomActions
|
||||
select customAction.Prompt;
|
||||
|
||||
@@ -40,10 +40,15 @@ namespace AdvancedPaste.Services.CustomActions
|
||||
this.userSettings = userSettings;
|
||||
}
|
||||
|
||||
public async Task<CustomActionTransformResult> TransformAsync(string prompt, string inputText, byte[] imageBytes, CancellationToken cancellationToken, IProgress<double> progress)
|
||||
public async Task<CustomActionTransformResult> TransformAsync(string prompt, string inputText, byte[] imageBytes, CancellationToken cancellationToken, IProgress<double> progress, string systemPromptOverride = null, string providerIdOverride = null)
|
||||
{
|
||||
var pasteConfig = userSettings?.PasteAIConfiguration;
|
||||
var providerConfig = BuildProviderConfig(pasteConfig);
|
||||
var providerConfig = BuildProviderConfig(pasteConfig, providerIdOverride);
|
||||
|
||||
if (systemPromptOverride != null)
|
||||
{
|
||||
providerConfig.SystemPrompt = systemPromptOverride;
|
||||
}
|
||||
|
||||
return await TransformAsync(prompt, inputText, imageBytes, providerConfig, cancellationToken, progress);
|
||||
}
|
||||
@@ -148,13 +153,26 @@ namespace AdvancedPaste.Services.CustomActions
|
||||
return serviceType == AIServiceType.Unknown ? AIServiceType.OpenAI : serviceType;
|
||||
}
|
||||
|
||||
private PasteAIConfig BuildProviderConfig(PasteAIConfiguration config)
|
||||
private PasteAIConfig BuildProviderConfig(PasteAIConfiguration config, string providerIdOverride = null)
|
||||
{
|
||||
config ??= new PasteAIConfiguration();
|
||||
var provider = config.ActiveProvider ?? config.Providers?.FirstOrDefault() ?? new PasteAIProviderDefinition();
|
||||
PasteAIProviderDefinition provider;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(providerIdOverride))
|
||||
{
|
||||
provider = config.Providers?.FirstOrDefault(p => string.Equals(p.Id, providerIdOverride, StringComparison.OrdinalIgnoreCase))
|
||||
?? config.ActiveProvider
|
||||
?? config.Providers?.FirstOrDefault()
|
||||
?? new PasteAIProviderDefinition();
|
||||
}
|
||||
else
|
||||
{
|
||||
provider = config.ActiveProvider ?? config.Providers?.FirstOrDefault() ?? new PasteAIProviderDefinition();
|
||||
}
|
||||
|
||||
var serviceType = NormalizeServiceType(provider.ServiceTypeKind);
|
||||
var systemPrompt = string.IsNullOrWhiteSpace(provider.SystemPrompt) ? DefaultSystemPrompt : provider.SystemPrompt;
|
||||
var apiKey = AcquireApiKey(serviceType);
|
||||
var apiKey = AcquireApiKey(serviceType, provider.Id);
|
||||
var modelName = provider.ModelName;
|
||||
|
||||
var providerConfig = new PasteAIConfig
|
||||
@@ -173,15 +191,14 @@ namespace AdvancedPaste.Services.CustomActions
|
||||
return providerConfig;
|
||||
}
|
||||
|
||||
private string AcquireApiKey(AIServiceType serviceType)
|
||||
private string AcquireApiKey(AIServiceType serviceType, string providerId)
|
||||
{
|
||||
if (!RequiresApiKey(serviceType))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
credentialsProvider.Refresh();
|
||||
return credentialsProvider.GetKey() ?? string.Empty;
|
||||
return credentialsProvider.GetKey(serviceType, providerId ?? string.Empty);
|
||||
}
|
||||
|
||||
private static bool RequiresApiKey(AIServiceType serviceType)
|
||||
@@ -190,6 +207,8 @@ namespace AdvancedPaste.Services.CustomActions
|
||||
{
|
||||
AIServiceType.Onnx => false,
|
||||
AIServiceType.Ollama => false,
|
||||
AIServiceType.FoundryLocal => false,
|
||||
AIServiceType.PhiSilica => false,
|
||||
_ => true,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -12,6 +12,6 @@ namespace AdvancedPaste.Services.CustomActions
|
||||
{
|
||||
public interface ICustomActionTransformService
|
||||
{
|
||||
Task<CustomActionTransformResult> TransformAsync(string prompt, string inputText, byte[] imageBytes, CancellationToken cancellationToken, IProgress<double> progress);
|
||||
Task<CustomActionTransformResult> TransformAsync(string prompt, string inputText, byte[] imageBytes, CancellationToken cancellationToken, IProgress<double> progress, string systemPromptOverride = null, string providerIdOverride = null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ namespace AdvancedPaste.Services.CustomActions
|
||||
SemanticKernelPasteProvider.Registration,
|
||||
LocalModelPasteProvider.Registration,
|
||||
FoundryLocalPasteProvider.Registration,
|
||||
PhiSilicaPasteProvider.Registration,
|
||||
};
|
||||
|
||||
private static readonly IReadOnlyDictionary<AIServiceType, Func<PasteAIConfig, IPasteAIProvider>> ProviderFactories = CreateProviderFactories();
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AdvancedPaste.Models;
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Microsoft.Windows.AI;
|
||||
using Microsoft.Windows.AI.ContentSafety;
|
||||
using Microsoft.Windows.AI.Text;
|
||||
using PhiSilicaLanguageModel = Microsoft.Windows.AI.Text.LanguageModel;
|
||||
|
||||
namespace AdvancedPaste.Services.CustomActions;
|
||||
|
||||
public sealed class PhiSilicaPasteProvider : IPasteAIProvider
|
||||
{
|
||||
private static readonly IReadOnlyCollection<AIServiceType> SupportedTypes = new[]
|
||||
{
|
||||
AIServiceType.PhiSilica,
|
||||
};
|
||||
|
||||
public static PasteAIProviderRegistration Registration { get; } = new(SupportedTypes, config => new PhiSilicaPasteProvider(config));
|
||||
|
||||
private static readonly SemaphoreSlim _initLock = new(1, 1);
|
||||
private static PhiSilicaLanguageModel _cachedModel;
|
||||
|
||||
private readonly PasteAIConfig _config;
|
||||
|
||||
public PhiSilicaPasteProvider(PasteAIConfig config)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(config);
|
||||
_config = config;
|
||||
}
|
||||
|
||||
public Task<bool> IsAvailableAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
try
|
||||
{
|
||||
PhiSilicaLafHelper.TryUnlock();
|
||||
var readyState = PhiSilicaLanguageModel.GetReadyState();
|
||||
return Task.FromResult(readyState is not (AIFeatureReadyState.NotSupportedOnCurrentSystem or AIFeatureReadyState.DisabledByUser));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> ProcessPasteAsync(PasteAIRequest request, CancellationToken cancellationToken, IProgress<double> progress)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
|
||||
try
|
||||
{
|
||||
var systemPrompt = request.SystemPrompt;
|
||||
if (string.IsNullOrWhiteSpace(systemPrompt))
|
||||
{
|
||||
throw new PasteActionException(
|
||||
"System prompt is required for Phi Silica",
|
||||
new ArgumentException("System prompt must be provided", nameof(request)));
|
||||
}
|
||||
|
||||
var prompt = request.Prompt;
|
||||
var inputText = request.InputText;
|
||||
if (string.IsNullOrWhiteSpace(prompt) || string.IsNullOrWhiteSpace(inputText))
|
||||
{
|
||||
throw new PasteActionException(
|
||||
"Prompt and input text are required",
|
||||
new ArgumentException("Prompt and input text must be provided", nameof(request)));
|
||||
}
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var languageModel = await GetOrCreateModelAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
progress?.Report(0.1);
|
||||
|
||||
var contentFilterOptions = new ContentFilterOptions();
|
||||
var context = languageModel.CreateContext(systemPrompt, contentFilterOptions);
|
||||
|
||||
var userPrompt = $"""
|
||||
User instructions:
|
||||
{prompt}
|
||||
|
||||
Text:
|
||||
{inputText}
|
||||
|
||||
Output:
|
||||
""";
|
||||
|
||||
if ((ulong)userPrompt.Length > languageModel.GetUsablePromptLength(context, userPrompt))
|
||||
{
|
||||
throw new PasteActionException(
|
||||
"Prompt is too large for the Phi Silica model context",
|
||||
new InvalidOperationException("Prompt exceeds usable prompt length"),
|
||||
aiServiceMessage: "The input text is too large for on-device processing. Try with shorter text.");
|
||||
}
|
||||
|
||||
var options = new LanguageModelOptions
|
||||
{
|
||||
ContentFilterOptions = contentFilterOptions,
|
||||
};
|
||||
|
||||
var result = await languageModel.GenerateResponseAsync(context, userPrompt, options).AsTask(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
progress?.Report(0.8);
|
||||
|
||||
if (result.Status != LanguageModelResponseStatus.Complete)
|
||||
{
|
||||
var statusMessage = result.Status switch
|
||||
{
|
||||
LanguageModelResponseStatus.BlockedByPolicy => "Response was blocked by policy.",
|
||||
LanguageModelResponseStatus.PromptBlockedByContentModeration => "Prompt was blocked by content moderation.",
|
||||
LanguageModelResponseStatus.ResponseBlockedByContentModeration => "Response was blocked by content moderation.",
|
||||
LanguageModelResponseStatus.PromptLargerThanContext => "Prompt is too large for the model context.",
|
||||
_ => $"Unexpected status: {result.Status}",
|
||||
};
|
||||
|
||||
throw new PasteActionException(
|
||||
$"Phi Silica returned status: {result.Status}",
|
||||
new InvalidOperationException($"LanguageModel response status: {result.Status}"),
|
||||
aiServiceMessage: statusMessage);
|
||||
}
|
||||
|
||||
var responseText = result.Text ?? string.Empty;
|
||||
request.Usage = AIServiceUsage.None;
|
||||
|
||||
progress?.Report(1.0);
|
||||
|
||||
return responseText;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (PasteActionException)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new PasteActionException(
|
||||
"Failed to generate response using Phi Silica",
|
||||
ex,
|
||||
aiServiceMessage: $"Error details: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<PhiSilicaLanguageModel> GetOrCreateModelAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_cachedModel is not null)
|
||||
{
|
||||
return _cachedModel;
|
||||
}
|
||||
|
||||
await _initLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
if (_cachedModel is not null)
|
||||
{
|
||||
return _cachedModel;
|
||||
}
|
||||
|
||||
PhiSilicaLafHelper.TryUnlock();
|
||||
var readyState = PhiSilicaLanguageModel.GetReadyState();
|
||||
|
||||
if (readyState is AIFeatureReadyState.NotSupportedOnCurrentSystem or AIFeatureReadyState.DisabledByUser)
|
||||
{
|
||||
throw new PasteActionException(
|
||||
"Phi Silica is not supported on this device. A Copilot+ PC is required.",
|
||||
new InvalidOperationException("Phi Silica requires a Copilot+ PC with an NPU."),
|
||||
aiServiceMessage: "Phi Silica requires a Copilot+ PC with an NPU. For on-device AI on any Windows PC, consider using Foundry Local.");
|
||||
}
|
||||
|
||||
if (readyState is AIFeatureReadyState.NotReady)
|
||||
{
|
||||
var ensureResult = await PhiSilicaLanguageModel.EnsureReadyAsync().AsTask(cancellationToken).ConfigureAwait(false);
|
||||
if (ensureResult.Status != AIFeatureReadyResultState.Success)
|
||||
{
|
||||
throw new PasteActionException(
|
||||
"Failed to prepare Phi Silica model",
|
||||
ensureResult.ExtendedError,
|
||||
aiServiceMessage: $"Model preparation failed (status: {ensureResult.Status})");
|
||||
}
|
||||
}
|
||||
|
||||
if (PhiSilicaLanguageModel.GetReadyState() is not AIFeatureReadyState.Ready)
|
||||
{
|
||||
throw new PasteActionException(
|
||||
"Phi Silica model is not ready",
|
||||
new InvalidOperationException("Phi Silica model is not in Ready state after preparation."),
|
||||
aiServiceMessage: "Phi Silica model is not available. Please ensure the model is downloaded and ready.");
|
||||
}
|
||||
|
||||
_cachedModel = await PhiSilicaLanguageModel.CreateAsync().AsTask(cancellationToken).ConfigureAwait(false);
|
||||
return _cachedModel;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_initLock.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -175,6 +175,7 @@ namespace AdvancedPaste.Services.CustomActions
|
||||
AIServiceType.OpenAI or AIServiceType.AzureOpenAI => new OpenAIPromptExecutionSettings
|
||||
{
|
||||
FunctionChoiceBehavior = null,
|
||||
ReasoningEffort = "minimal",
|
||||
},
|
||||
_ => new PromptExecutionSettings(),
|
||||
};
|
||||
|
||||
@@ -55,6 +55,13 @@ public sealed class EnhancedVaultCredentialsProvider : IAICredentialsProvider
|
||||
return !string.IsNullOrEmpty(GetKey());
|
||||
}
|
||||
|
||||
public string GetKey(AIServiceType serviceType, string providerId)
|
||||
{
|
||||
var normalizedType = NormalizeServiceType(serviceType);
|
||||
var entry = BuildCredentialEntry(normalizedType, providerId ?? string.Empty);
|
||||
return LoadKey(entry);
|
||||
}
|
||||
|
||||
public bool Refresh()
|
||||
{
|
||||
using (_syncRoot.EnterScope())
|
||||
@@ -121,6 +128,7 @@ public sealed class EnhancedVaultCredentialsProvider : IAICredentialsProvider
|
||||
try
|
||||
{
|
||||
var credential = new PasswordVault().Retrieve(entry.Value.Resource, entry.Value.Username);
|
||||
credential?.RetrievePassword();
|
||||
return credential?.Password ?? string.Empty;
|
||||
}
|
||||
catch (Exception)
|
||||
@@ -160,6 +168,7 @@ public sealed class EnhancedVaultCredentialsProvider : IAICredentialsProvider
|
||||
case AIServiceType.ML:
|
||||
case AIServiceType.Onnx:
|
||||
case AIServiceType.Ollama:
|
||||
case AIServiceType.PhiSilica:
|
||||
return null;
|
||||
default:
|
||||
return null;
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
|
||||
namespace AdvancedPaste.Services;
|
||||
|
||||
/// <summary>
|
||||
@@ -21,6 +23,14 @@ public interface IAICredentialsProvider
|
||||
/// <returns>Credential string or <see cref="string.Empty"/> when missing.</returns>
|
||||
string GetKey();
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the credential for a specific AI provider.
|
||||
/// </summary>
|
||||
/// <param name="serviceType">The AI service type.</param>
|
||||
/// <param name="providerId">The provider identifier.</param>
|
||||
/// <returns>Credential string or <see cref="string.Empty"/> when missing.</returns>
|
||||
string GetKey(AIServiceType serviceType, string providerId);
|
||||
|
||||
/// <summary>
|
||||
/// Refreshes the cached credential for the active AI provider.
|
||||
/// </summary>
|
||||
|
||||
@@ -12,5 +12,5 @@ namespace AdvancedPaste.Services;
|
||||
|
||||
public interface IKernelService
|
||||
{
|
||||
Task<DataPackage> TransformClipboardAsync(string prompt, DataPackageView clipboardData, bool isSavedQuery, CancellationToken cancellationToken, IProgress<double> progress);
|
||||
Task<DataPackage> TransformClipboardAsync(string prompt, DataPackageView clipboardData, bool isSavedQuery, CancellationToken cancellationToken, IProgress<double> progress, string providerIdOverride = null);
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ public abstract class KernelServiceBase(
|
||||
|
||||
protected abstract IKernelRuntimeConfiguration GetRuntimeConfiguration();
|
||||
|
||||
public async Task<DataPackage> TransformClipboardAsync(string prompt, DataPackageView clipboardData, bool isSavedQuery, CancellationToken cancellationToken, IProgress<double> progress)
|
||||
public async Task<DataPackage> TransformClipboardAsync(string prompt, DataPackageView clipboardData, bool isSavedQuery, CancellationToken cancellationToken, IProgress<double> progress, string providerIdOverride = null)
|
||||
{
|
||||
Logger.LogTrace();
|
||||
|
||||
|
||||
@@ -9,15 +9,18 @@ using System.Threading.Tasks;
|
||||
using AdvancedPaste.Helpers;
|
||||
using AdvancedPaste.Models;
|
||||
using AdvancedPaste.Services.CustomActions;
|
||||
using AdvancedPaste.Settings;
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Microsoft.PowerToys.Telemetry;
|
||||
using Windows.ApplicationModel.DataTransfer;
|
||||
|
||||
namespace AdvancedPaste.Services;
|
||||
|
||||
public sealed class PasteFormatExecutor(IKernelService kernelService, ICustomActionTransformService customActionTransformService) : IPasteFormatExecutor
|
||||
public sealed class PasteFormatExecutor(IKernelService kernelService, ICustomActionTransformService customActionTransformService, IUserSettings userSettings) : IPasteFormatExecutor
|
||||
{
|
||||
private readonly IKernelService _kernelService = kernelService;
|
||||
private readonly ICustomActionTransformService _customActionTransformService = customActionTransformService;
|
||||
private readonly IUserSettings _userSettings = userSettings;
|
||||
|
||||
public async Task<DataPackage> ExecutePasteFormatAsync(PasteFormat pasteFormat, PasteActionSource source, CancellationToken cancellationToken, IProgress<double> progress)
|
||||
{
|
||||
@@ -36,8 +39,9 @@ public sealed class PasteFormatExecutor(IKernelService kernelService, ICustomAct
|
||||
return await Task.Run(async () =>
|
||||
pasteFormat.Format switch
|
||||
{
|
||||
PasteFormats.KernelQuery => await _kernelService.TransformClipboardAsync(pasteFormat.Prompt, clipboardData, pasteFormat.IsSavedQuery, cancellationToken, progress),
|
||||
PasteFormats.CustomTextTransformation => DataPackageHelpers.CreateFromText((await _customActionTransformService.TransformAsync(pasteFormat.Prompt, await clipboardData.GetTextOrHtmlTextAsync(), await clipboardData.GetImageAsPngBytesAsync(), cancellationToken, progress))?.Content ?? string.Empty),
|
||||
PasteFormats.KernelQuery => await _kernelService.TransformClipboardAsync(pasteFormat.Prompt, clipboardData, pasteFormat.IsSavedQuery, cancellationToken, progress, pasteFormat.ProviderId),
|
||||
PasteFormats.CustomTextTransformation => DataPackageHelpers.CreateFromText((await _customActionTransformService.TransformAsync(pasteFormat.Prompt, await clipboardData.GetTextOrHtmlTextAsync(), await clipboardData.GetImageAsPngBytesAsync(), cancellationToken, progress, providerIdOverride: pasteFormat.ProviderId))?.Content ?? string.Empty),
|
||||
PasteFormats.FixSpellingAndGrammar => DataPackageHelpers.CreateFromText((await _customActionTransformService.TransformAsync(GetFixSpellingPrompt(), await clipboardData.GetTextOrHtmlTextAsync(), null, cancellationToken, progress, GetFixSpellingSystemPrompt(), pasteFormat.ProviderId))?.Content ?? string.Empty),
|
||||
_ => await TransformHelpers.TransformAsync(format, clipboardData, cancellationToken, progress),
|
||||
});
|
||||
}
|
||||
@@ -62,4 +66,16 @@ public sealed class PasteFormatExecutor(IKernelService kernelService, ICustomAct
|
||||
throw new ArgumentOutOfRangeException(nameof(format));
|
||||
}
|
||||
}
|
||||
|
||||
private string GetFixSpellingPrompt()
|
||||
{
|
||||
var customPrompt = _userSettings.FixSpellingAndGrammarPrompt;
|
||||
return string.IsNullOrWhiteSpace(customPrompt) ? AdvancedPasteDefaultPrompts.FixSpellingAndGrammar : customPrompt;
|
||||
}
|
||||
|
||||
private string GetFixSpellingSystemPrompt()
|
||||
{
|
||||
var customSystemPrompt = _userSettings.FixSpellingAndGrammarSystemPrompt;
|
||||
return string.IsNullOrWhiteSpace(customSystemPrompt) ? AdvancedPasteDefaultPrompts.FixSpellingAndGrammarSystem : customSystemPrompt;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,6 +232,12 @@
|
||||
<data name="PasteAsPlainText" xml:space="preserve">
|
||||
<value>Paste as plain text</value>
|
||||
</data>
|
||||
<data name="FixSpellingAndGrammar" xml:space="preserve">
|
||||
<value>Fix spelling and grammar</value>
|
||||
</data>
|
||||
<data name="CoachingExplanationTitle.Text" xml:space="preserve">
|
||||
<value>What was changed and why</value>
|
||||
</data>
|
||||
<data name="ImageToText" xml:space="preserve">
|
||||
<value>Image to text</value>
|
||||
</data>
|
||||
|
||||
@@ -16,8 +16,8 @@ using System.Threading.Tasks;
|
||||
using AdvancedPaste.Helpers;
|
||||
using AdvancedPaste.Models;
|
||||
using AdvancedPaste.Services;
|
||||
using AdvancedPaste.Services.CustomActions;
|
||||
using AdvancedPaste.Settings;
|
||||
using Common.UI;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using ManagedCommon;
|
||||
@@ -41,6 +41,7 @@ namespace AdvancedPaste.ViewModels
|
||||
private readonly IUserSettings _userSettings;
|
||||
private readonly IPasteFormatExecutor _pasteFormatExecutor;
|
||||
private readonly IAICredentialsProvider _credentialsProvider;
|
||||
private readonly ICustomActionTransformService _customActionTransformService;
|
||||
|
||||
private CancellationTokenSource _pasteActionCancellationTokenSource;
|
||||
|
||||
@@ -258,11 +259,12 @@ namespace AdvancedPaste.ViewModels
|
||||
|
||||
public event EventHandler PreviewRequested;
|
||||
|
||||
public OptionsViewModel(IFileSystem fileSystem, IAICredentialsProvider credentialsProvider, IUserSettings userSettings, IPasteFormatExecutor pasteFormatExecutor)
|
||||
public OptionsViewModel(IFileSystem fileSystem, IAICredentialsProvider credentialsProvider, IUserSettings userSettings, IPasteFormatExecutor pasteFormatExecutor, ICustomActionTransformService customActionTransformService)
|
||||
{
|
||||
_credentialsProvider = credentialsProvider;
|
||||
_userSettings = userSettings;
|
||||
_pasteFormatExecutor = pasteFormatExecutor;
|
||||
_customActionTransformService = customActionTransformService;
|
||||
|
||||
GeneratedResponses = [];
|
||||
GeneratedResponses.CollectionChanged += (s, e) =>
|
||||
@@ -341,11 +343,21 @@ namespace AdvancedPaste.ViewModels
|
||||
});
|
||||
}
|
||||
|
||||
private PasteFormat CreateStandardPasteFormat(PasteFormats format) =>
|
||||
PasteFormat.CreateStandardFormat(format, AvailableClipboardFormats, IsCustomAIServiceEnabled, ResourceLoaderInstance.ResourceLoader.GetString);
|
||||
private PasteFormat CreateStandardPasteFormat(PasteFormats format)
|
||||
{
|
||||
var providerId = GetProviderIdForFormat(format);
|
||||
return PasteFormat.CreateStandardFormat(format, AvailableClipboardFormats, IsCustomAIServiceEnabled, ResourceLoaderInstance.ResourceLoader.GetString, providerId);
|
||||
}
|
||||
|
||||
private PasteFormat CreateCustomAIPasteFormat(string name, string prompt, bool isSavedQuery) =>
|
||||
PasteFormat.CreateCustomAIFormat(CustomAIFormat, name, prompt, isSavedQuery, AvailableClipboardFormats, IsCustomAIServiceEnabled);
|
||||
private PasteFormat CreateCustomAIPasteFormat(string name, string prompt, bool isSavedQuery, string providerId = null) =>
|
||||
PasteFormat.CreateCustomAIFormat(CustomAIFormat, name, prompt, isSavedQuery, AvailableClipboardFormats, IsCustomAIServiceEnabled, providerId);
|
||||
|
||||
private string GetProviderIdForFormat(PasteFormats format) =>
|
||||
format switch
|
||||
{
|
||||
PasteFormats.FixSpellingAndGrammar => _userSettings.FixSpellingAndGrammarProviderId,
|
||||
_ => string.Empty,
|
||||
};
|
||||
|
||||
private void UpdateAIProviderActiveFlags()
|
||||
{
|
||||
@@ -418,7 +430,7 @@ namespace AdvancedPaste.ViewModels
|
||||
|
||||
UpdateFormats(
|
||||
CustomActionPasteFormats,
|
||||
IsCustomAIServiceEnabled ? _userSettings.CustomActions.Select(customAction => CreateCustomAIPasteFormat(customAction.Name, customAction.Prompt, isSavedQuery: true)) : []);
|
||||
IsCustomAIServiceEnabled ? _userSettings.CustomActions.Select(customAction => CreateCustomAIPasteFormat(customAction.Name, customAction.Prompt, isSavedQuery: true, customAction.ProviderId)) : []);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
@@ -539,6 +551,7 @@ namespace AdvancedPaste.ViewModels
|
||||
{
|
||||
PasteActionError = PasteActionError.None;
|
||||
Query = string.Empty;
|
||||
CoachingExplanation = null;
|
||||
|
||||
await ReadClipboardAsync();
|
||||
|
||||
@@ -616,6 +629,12 @@ namespace AdvancedPaste.ViewModels
|
||||
[ObservableProperty]
|
||||
private string _customFormatResult;
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyPropertyChangedFor(nameof(HasCoachingExplanation))]
|
||||
private string _coachingExplanation;
|
||||
|
||||
public bool HasCoachingExplanation => !string.IsNullOrEmpty(CoachingExplanation);
|
||||
|
||||
[RelayCommand]
|
||||
public async Task PasteCustomAsync()
|
||||
{
|
||||
@@ -661,17 +680,37 @@ namespace AdvancedPaste.ViewModels
|
||||
[RelayCommand]
|
||||
public void OpenSettings()
|
||||
{
|
||||
SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.AdvancedPaste);
|
||||
try
|
||||
{
|
||||
var exePath = System.IO.Path.Combine(
|
||||
ManagedCommon.PowerToysPathResolver.GetPowerToysInstallPath(),
|
||||
"PowerToys.exe");
|
||||
|
||||
if (exePath != null && System.IO.File.Exists(exePath))
|
||||
{
|
||||
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
|
||||
{
|
||||
FileName = exePath,
|
||||
Arguments = "--open-settings=AdvancedPaste",
|
||||
UseShellExecute = false,
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError("Failed to open settings", ex);
|
||||
}
|
||||
|
||||
GetMainWindow()?.Close();
|
||||
}
|
||||
|
||||
internal async Task ExecutePasteFormatAsync(PasteFormats format, PasteActionSource source)
|
||||
internal async Task ExecutePasteFormatAsync(PasteFormats format, PasteActionSource source, bool forceCoaching = false)
|
||||
{
|
||||
await ReadClipboardAsync();
|
||||
await ExecutePasteFormatAsync(CreateStandardPasteFormat(format), source);
|
||||
await ExecutePasteFormatAsync(CreateStandardPasteFormat(format), source, forceCoaching);
|
||||
}
|
||||
|
||||
internal async Task ExecutePasteFormatAsync(PasteFormat pasteFormat, PasteActionSource source)
|
||||
internal async Task ExecutePasteFormatAsync(PasteFormat pasteFormat, PasteActionSource source, bool forceCoaching = false)
|
||||
{
|
||||
if (IsBusy)
|
||||
{
|
||||
@@ -704,12 +743,30 @@ namespace AdvancedPaste.ViewModels
|
||||
await delayTask;
|
||||
|
||||
var outputText = await dataPackage.GetView().GetTextOrEmptyAsync();
|
||||
bool isCoachingAction = pasteFormat.Format == PasteFormats.FixSpellingAndGrammar &&
|
||||
(forceCoaching || (_userSettings.FixSpellingAndGrammarCoachingEnabled && !_userSettings.FixSpellingAndGrammarCoachingShortcutSet));
|
||||
bool shouldPreview = pasteFormat.Metadata.CanPreview && _userSettings.ShowCustomPreview && !string.IsNullOrEmpty(outputText) && source != PasteActionSource.GlobalKeyboardShortcut;
|
||||
|
||||
// Coaching mode forces preview even for global keyboard shortcuts
|
||||
if (isCoachingAction && !string.IsNullOrEmpty(outputText))
|
||||
{
|
||||
shouldPreview = true;
|
||||
}
|
||||
|
||||
if (shouldPreview)
|
||||
{
|
||||
GeneratedResponses.Add(outputText);
|
||||
CurrentResponseIndex = GeneratedResponses.Count - 1;
|
||||
|
||||
if (isCoachingAction)
|
||||
{
|
||||
await GenerateCoachingExplanationAsync(outputText);
|
||||
}
|
||||
else
|
||||
{
|
||||
CoachingExplanation = null;
|
||||
}
|
||||
|
||||
PreviewRequested?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
else
|
||||
@@ -730,6 +787,65 @@ namespace AdvancedPaste.ViewModels
|
||||
Logger.LogDebug($"Finished executing {pasteFormat.Format} from source {source}; timeTakenMs={elapsedWatch.ElapsedMilliseconds}");
|
||||
}
|
||||
|
||||
private async Task GenerateCoachingExplanationAsync(string correctedText)
|
||||
{
|
||||
try
|
||||
{
|
||||
var originalText = ClipboardData != null ? await ClipboardData.GetTextOrEmptyAsync() : string.Empty;
|
||||
|
||||
if (string.IsNullOrEmpty(originalText))
|
||||
{
|
||||
CoachingExplanation = null;
|
||||
return;
|
||||
}
|
||||
|
||||
static string NormalizeForComparison(string s) =>
|
||||
s.Replace('\u2018', '\'') // left single quote
|
||||
.Replace('\u2019', '\'') // right single quote / apostrophe
|
||||
.Replace('\u201C', '"') // left double quote
|
||||
.Replace('\u201D', '"') // right double quote
|
||||
.Replace('\u2013', '-') // en dash
|
||||
.Replace('\u2014', '-'); // em dash
|
||||
|
||||
if (string.Equals(NormalizeForComparison(originalText), NormalizeForComparison(correctedText), StringComparison.Ordinal))
|
||||
{
|
||||
CoachingExplanation = null;
|
||||
return;
|
||||
}
|
||||
|
||||
var coachingInstruction = string.IsNullOrWhiteSpace(_userSettings.FixSpellingAndGrammarCoachingPrompt)
|
||||
? AdvancedPasteDefaultPrompts.FixSpellingAndGrammarCoaching
|
||||
: _userSettings.FixSpellingAndGrammarCoachingPrompt;
|
||||
var coachingInputText = $"Original:\n\"{originalText}\"\n\nCorrected:\n\"{correctedText}\"";
|
||||
|
||||
var coachingSystemPrompt = string.IsNullOrWhiteSpace(_userSettings.FixSpellingAndGrammarCoachingSystemPrompt)
|
||||
? AdvancedPasteDefaultPrompts.FixSpellingAndGrammarCoachingSystem
|
||||
: _userSettings.FixSpellingAndGrammarCoachingSystemPrompt;
|
||||
|
||||
var coachingProviderId = _userSettings.FixSpellingAndGrammarCoachingProviderId;
|
||||
if (string.IsNullOrWhiteSpace(coachingProviderId))
|
||||
{
|
||||
coachingProviderId = _userSettings.FixSpellingAndGrammarProviderId;
|
||||
}
|
||||
|
||||
var result = await _customActionTransformService.TransformAsync(
|
||||
coachingInstruction,
|
||||
coachingInputText,
|
||||
null,
|
||||
_pasteActionCancellationTokenSource?.Token ?? CancellationToken.None,
|
||||
null,
|
||||
coachingSystemPrompt,
|
||||
string.IsNullOrWhiteSpace(coachingProviderId) ? null : coachingProviderId);
|
||||
|
||||
CoachingExplanation = result?.Content;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError("Error generating coaching explanation", ex);
|
||||
CoachingExplanation = null;
|
||||
}
|
||||
}
|
||||
|
||||
internal async Task ExecutePasteFormatAsync(VirtualKey key)
|
||||
{
|
||||
var pasteFormat = StandardPasteFormats.Concat(CustomActionPasteFormats)
|
||||
|
||||
@@ -1,40 +1,6 @@
|
||||
#include <windows.h>
|
||||
#include "resource.h"
|
||||
#include "../../../../common/version/version.h"
|
||||
|
||||
#define APSTUDIO_READONLY_SYMBOLS
|
||||
#include "winres.h"
|
||||
#undef APSTUDIO_READONLY_SYMBOLS
|
||||
|
||||
1 VERSIONINFO
|
||||
FILEVERSION FILE_VERSION
|
||||
PRODUCTVERSION PRODUCT_VERSION
|
||||
FILEFLAGSMASK VS_FFI_FILEFLAGSMASK
|
||||
#ifdef _DEBUG
|
||||
FILEFLAGS VS_FF_DEBUG
|
||||
#else
|
||||
FILEFLAGS 0x0L
|
||||
#endif
|
||||
FILEOS VOS_NT_WINDOWS32
|
||||
FILETYPE VFT_DLL
|
||||
FILESUBTYPE VFT2_UNKNOWN
|
||||
BEGIN
|
||||
BLOCK "StringFileInfo"
|
||||
BEGIN
|
||||
BLOCK "040904b0" // US English (0x0409), Unicode (0x04B0) charset
|
||||
BEGIN
|
||||
VALUE "CompanyName", COMPANY_NAME
|
||||
VALUE "FileDescription", FILE_DESCRIPTION
|
||||
VALUE "FileVersion", FILE_VERSION_STRING
|
||||
VALUE "InternalName", INTERNAL_NAME
|
||||
VALUE "LegalCopyright", COPYRIGHT_NOTE
|
||||
VALUE "OriginalFilename", ORIGINAL_FILENAME
|
||||
VALUE "ProductName", PRODUCT_NAME
|
||||
VALUE "ProductVersion", PRODUCT_VERSION_STRING
|
||||
END
|
||||
END
|
||||
BLOCK "VarFileInfo"
|
||||
BEGIN
|
||||
VALUE "Translation", 0x409, 1200 // US English (0x0409), Unicode (1200) charset
|
||||
END
|
||||
END
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Label="Configuration">
|
||||
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
||||
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
|
||||
@@ -100,25 +100,30 @@ HRESULT AdvancedPasteProcessManager::start_process(const std::wstring& pipe_name
|
||||
{
|
||||
const unsigned long powertoys_pid = GetCurrentProcessId();
|
||||
|
||||
const auto executable_args = std::format(L"{} {}", std::to_wstring(powertoys_pid), pipe_name);
|
||||
const auto launch_direct_exe = [&]() -> HRESULT {
|
||||
// Fallback: launch exe directly (dev builds without GenerateAppxPackageOnBuild)
|
||||
const auto executable_args = std::format(L"{} {}", std::to_wstring(powertoys_pid), pipe_name);
|
||||
|
||||
SHELLEXECUTEINFOW sei{ sizeof(sei) };
|
||||
sei.fMask = { SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI };
|
||||
sei.lpFile = L"WinUI3Apps\\PowerToys.AdvancedPaste.exe";
|
||||
sei.nShow = SW_SHOWNORMAL;
|
||||
sei.lpParameters = executable_args.data();
|
||||
if (ShellExecuteExW(&sei))
|
||||
{
|
||||
Logger::trace("Successfully started Advanced Paste process");
|
||||
terminate_process();
|
||||
m_hProcess = sei.hProcess;
|
||||
return S_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger::error(L"Advanced Paste process failed to start. {}", get_last_error_or_default(GetLastError()));
|
||||
return E_FAIL;
|
||||
}
|
||||
SHELLEXECUTEINFOW sei{ sizeof(sei) };
|
||||
sei.fMask = { SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI };
|
||||
sei.lpFile = L"WinUI3Apps\\PowerToys.AdvancedPaste.exe";
|
||||
sei.nShow = SW_SHOWNORMAL;
|
||||
sei.lpParameters = executable_args.data();
|
||||
if (ShellExecuteExW(&sei))
|
||||
{
|
||||
Logger::trace("Successfully started Advanced Paste process (direct)");
|
||||
terminate_process();
|
||||
m_hProcess = sei.hProcess;
|
||||
return S_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger::error(L"Advanced Paste process failed to start. {}", get_last_error_or_default(GetLastError()));
|
||||
return E_FAIL;
|
||||
}
|
||||
};
|
||||
|
||||
return launch_direct_exe();
|
||||
}
|
||||
|
||||
HRESULT AdvancedPasteProcessManager::start_named_pipe_server(const std::wstring& pipe_name)
|
||||
@@ -175,8 +180,9 @@ HRESULT AdvancedPasteProcessManager::start_named_pipe_server(const std::wstring&
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for client.
|
||||
const constexpr DWORD client_timeout_millis = 5000;
|
||||
// Wait for client. AdvancedPaste under sparse identity can take >5s on cold start to
|
||||
// bootstrap WinAppSDK + DI host before connecting back to this pipe.
|
||||
const constexpr DWORD client_timeout_millis = 15000;
|
||||
switch (WaitForSingleObject(overlapped.hEvent, client_timeout_millis))
|
||||
{
|
||||
case WAIT_OBJECT_0:
|
||||
|
||||
@@ -66,6 +66,8 @@ namespace
|
||||
const wchar_t JSON_KEY_PROVIDERS[] = L"providers";
|
||||
const wchar_t JSON_KEY_SERVICE_TYPE[] = L"service-type";
|
||||
const wchar_t JSON_KEY_ENABLE_ADVANCED_AI[] = L"enable-advanced-ai";
|
||||
const wchar_t JSON_KEY_COACHING_SHORTCUT[] = L"coaching-shortcut";
|
||||
const wchar_t JSON_KEY_COACHING_ENABLED[] = L"coaching-enabled";
|
||||
const wchar_t JSON_KEY_VALUE[] = L"value";
|
||||
}
|
||||
|
||||
@@ -255,6 +257,21 @@ private:
|
||||
};
|
||||
|
||||
m_additional_actions.push_back(additionalAction);
|
||||
|
||||
// Register coaching shortcut as a separate hotkey with a "-coaching" suffix ID
|
||||
if (action.HasKey(JSON_KEY_COACHING_SHORTCUT) && action.GetNamedBoolean(JSON_KEY_COACHING_ENABLED, false))
|
||||
{
|
||||
auto coachingHotkey = parse_single_hotkey(action.GetNamedObject(JSON_KEY_COACHING_SHORTCUT), actionIsShown);
|
||||
if (coachingHotkey.key != 0)
|
||||
{
|
||||
const AdditionalAction coachingAction
|
||||
{
|
||||
std::wstring(actionName.c_str()) + L"-coaching",
|
||||
coachingHotkey
|
||||
};
|
||||
m_additional_actions.push_back(coachingAction);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -407,6 +424,7 @@ private:
|
||||
// Define the expected order to ensure consistent hotkey ID assignment
|
||||
const std::vector<winrt::hstring> expectedOrder = {
|
||||
L"image-to-text",
|
||||
L"fix-spelling-and-grammar",
|
||||
L"paste-as-file",
|
||||
L"transcode"
|
||||
};
|
||||
@@ -982,6 +1000,12 @@ public:
|
||||
m_triggerEventWaiter.start(CommonSharedConstants::ADVANCED_PASTE_SHOW_UI_EVENT, [this](DWORD) {
|
||||
// Same logic as hotkeyId == 1 (m_advanced_paste_ui_hotkey)
|
||||
Logger::trace(L"AdvancedPaste ShowUI event triggered");
|
||||
|
||||
if (m_auto_copy_selection_custom_action)
|
||||
{
|
||||
send_copy_selection(); // best-effort; ignore failure
|
||||
}
|
||||
|
||||
m_process_manager.start();
|
||||
m_process_manager.bring_to_front();
|
||||
m_process_manager.send_message(CommonSharedConstants::ADVANCED_PASTE_SHOW_UI_MESSAGE);
|
||||
@@ -1032,13 +1056,11 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
if (is_custom_action_hotkey && m_auto_copy_selection_custom_action)
|
||||
// Try to capture selected text for all hotkey actions when the setting is enabled.
|
||||
// If nothing is selected (clipboard unchanged), fall through to use existing clipboard content.
|
||||
if (m_auto_copy_selection_custom_action)
|
||||
{
|
||||
if (!send_copy_selection())
|
||||
{
|
||||
Logger::warn(L"Auto-copy: failed to copy selection for custom action index {} — aborting action", custom_action_index);
|
||||
return false;
|
||||
}
|
||||
send_copy_selection(); // best-effort; ignore failure
|
||||
}
|
||||
|
||||
m_process_manager.start();
|
||||
|
||||
11
src/modules/AdvancedPaste/custom.props
Normal file
11
src/modules/AdvancedPaste/custom.props
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<!-- This file is read by XES, which we use in our Release builds. -->
|
||||
<PropertyGroup Label="Version">
|
||||
<XesUseOneStoreVersioning>true</XesUseOneStoreVersioning>
|
||||
<XesBaseYearForStoreVersion>2025</XesBaseYearForStoreVersion>
|
||||
<VersionMajor>0</VersionMajor>
|
||||
<VersionMinor>9</VersionMinor>
|
||||
<VersionInfoProductName>PowerToys Advanced Paste</VersionInfoProductName>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -1,248 +0,0 @@
|
||||
# 01 - Architecture Overview
|
||||
|
||||
## Process Model
|
||||
|
||||
Each JavaScript/TypeScript extension runs as an **isolated Node.js process**. The CmdPal host manages these processes through `JsonRpcExtensionService`, which implements the same `IExtensionService` interface used by WinRT and built-in extensions.
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
subgraph Host["Host Process (CmdPal)"]
|
||||
JSSVC["JsonRpcExtensionService"]
|
||||
WRAP["JSExtensionWrapper"]
|
||||
RPC["JsonRpcConnection"]
|
||||
PROXY["JSCommandProviderProxy"]
|
||||
ONE["(one per extension)"]
|
||||
|
||||
JSSVC --> WRAP --> RPC --> PROXY
|
||||
JSSVC --> ONE
|
||||
end
|
||||
|
||||
subgraph Ext["Extension Process (Node.js)"]
|
||||
SDK["@microsoft/cmdpal-sdk"]
|
||||
STDIO["stdio-server.ts\n(JSON-RPC 2.0)"]
|
||||
CODE["Extension code\nindex.ts"]
|
||||
|
||||
SDK --> STDIO
|
||||
CODE --> STDIO
|
||||
end
|
||||
|
||||
RPC -->|"stdin"| STDIO
|
||||
STDIO -->|"stdout"| RPC
|
||||
```
|
||||
|
||||
### Why Per-Process?
|
||||
|
||||
1. **Crash isolation** — A runaway extension cannot crash CmdPal. If an extension process dies, only that extension stops working.
|
||||
2. **Resource isolation** — Each extension has independent memory, CPU, and event loop.
|
||||
3. **Independent debugging** — Attach a debugger to a specific extension's Node.js process via `--inspect`.
|
||||
4. **Clean lifecycle** — Stop/restart an extension by killing and re-spawning its process.
|
||||
5. **Security boundary** — Extensions cannot access host memory or other extensions' state.
|
||||
|
||||
### Crash Recovery
|
||||
|
||||
The host tracks consecutive crashes per extension:
|
||||
|
||||
| Crash Count | Behavior |
|
||||
|-------------|----------|
|
||||
| 1–3 | Extension marked as disconnected, available for restart |
|
||||
| > 3 | Extension marked as **unhealthy**, disabled until manual re-enable |
|
||||
|
||||
## Extension Discovery
|
||||
|
||||
Extensions are discovered from a well-known directory:
|
||||
|
||||
```
|
||||
%LOCALAPPDATA%\Microsoft\PowerToys\CommandPalette\JSExtensions\
|
||||
```
|
||||
|
||||
Each subdirectory containing a valid `package.json` with a `cmdpal` section is treated as an extension:
|
||||
|
||||
```mermaid
|
||||
flowchart TB
|
||||
ROOT["JSExtensions/"] --> MY["my-extension/"]
|
||||
ROOT --> ANOTHER["another-extension/"]
|
||||
|
||||
MY --> PKG["package.json\nmanifest with cmdpal section (required)"]
|
||||
MY --> DIST["dist/"]
|
||||
DIST --> JSENTRY["index.js\ncompiled entry point"]
|
||||
MY --> NM["node_modules/"]
|
||||
NM --> MS["@microsoft/cmdpal-sdk"]
|
||||
MY --> SRC["src/"]
|
||||
SRC --> TSENTRY["index.ts\nTypeScript source"]
|
||||
|
||||
ANOTHER --> APKG["package.json"]
|
||||
ANOTHER --> AETC["..."]
|
||||
```
|
||||
|
||||
### Directory Watching
|
||||
|
||||
The service watches the `JSExtensions` directory for:
|
||||
|
||||
| Event | Behavior |
|
||||
|-------|----------|
|
||||
| **New subdirectory created** | Scans for `package.json` with `cmdpal` section, loads extension if valid |
|
||||
| **Subdirectory deleted** | Stops the extension process, removes from provider list |
|
||||
| **`*.js` file changed** (within an extension) | Hot-reloads the extension (debounced 500ms) |
|
||||
|
||||
Source file watchers ignore `node_modules/` changes.
|
||||
|
||||
## Extension Lifecycle
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
DISC["Discover\n(find package.json with cmdpal)"] --> PARSE["Parse Manifest\n(validate manifest)"]
|
||||
PARSE --> START["Start Process\n(spawn node process)"]
|
||||
START --> INIT["Initialize\n(JSON-RPC initialize)"]
|
||||
INIT --> READY["Ready\n(host queries commands)"]
|
||||
READY -->|"on shutdown/hot-reload"| STOP["Stop\n(send dispose, kill process)"]
|
||||
READY -->|"on crash or error"| STOP
|
||||
```
|
||||
|
||||
### Startup Sequence
|
||||
|
||||
1. `JsonRpcExtensionService.LoadProvidersAsync()` scans `JSExtensions/`
|
||||
2. For each valid `package.json` containing a `cmdpal` section:
|
||||
- Creates `JSExtensionWrapper`
|
||||
- Spawns `node <entrypoint>` with stdio redirection
|
||||
- Creates `JsonRpcConnection` over the process's stdin/stdout
|
||||
- Sends `initialize` request and waits for response
|
||||
- Creates `JSCommandProviderProxy` as the `ICommandProvider` implementation
|
||||
- Wraps in `CommandProviderWrapper` and returns to `TopLevelCommandManager`
|
||||
|
||||
### Hot-Reload (Development)
|
||||
|
||||
When a `*.js` file changes in an extension directory:
|
||||
|
||||
1. Change detected by `FileSystemWatcher`
|
||||
2. Debounced 500ms to coalesce rapid saves
|
||||
3. Current process receives `dispose` notification
|
||||
4. Process is killed after 2s grace period
|
||||
5. New process is spawned and initialized
|
||||
6. Crash counter is reset on successful initialization
|
||||
|
||||
### Debug Mode
|
||||
|
||||
Set `"debug": true` in the `cmdpal` section of `package.json` to start the Node.js process with `--inspect`:
|
||||
|
||||
```json
|
||||
{
|
||||
"cmdpal": {
|
||||
"debug": true,
|
||||
"debugPort": 9230
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The host logs a Chrome DevTools URL for attaching:
|
||||
|
||||
```
|
||||
chrome-devtools://devtools/bundled/js_app.html?experiments=true&v8only=true&ws=127.0.0.1:9230
|
||||
```
|
||||
|
||||
If `debugPort` is not specified, ports are auto-assigned starting at 9229.
|
||||
|
||||
## Transport Layer
|
||||
|
||||
### Framing
|
||||
|
||||
All messages use **LSP-style framing** (Language Server Protocol):
|
||||
|
||||
```http
|
||||
Content-Length: <byte-count>\r\n
|
||||
\r\n
|
||||
<UTF-8 JSON body>
|
||||
```
|
||||
|
||||
This is the same framing used by VS Code's Language Server Protocol, making it compatible with existing JSON-RPC tooling.
|
||||
|
||||
### Connection Details
|
||||
|
||||
| Property | Value |
|
||||
|----------|-------|
|
||||
| Transport | Process stdio (stdin for writes, stdout for reads) |
|
||||
| Encoding | UTF-8 |
|
||||
| Framing | `Content-Length` header (LSP-style) |
|
||||
| Protocol | JSON-RPC 2.0 |
|
||||
| Request timeout | 10 seconds |
|
||||
| Concurrency | Serialized writes (lock-protected), async reads |
|
||||
| Error channel | stderr (logged by host, not part of protocol) |
|
||||
|
||||
### Message Flow
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant Host
|
||||
participant Extension
|
||||
|
||||
Host->>Extension: Content-Length: N {"jsonrpc":"2.0","id":1,...}
|
||||
Extension-->>Host: Content-Length: M {"jsonrpc":"2.0","id":1,...}
|
||||
Extension-->>Host: Content-Length: K {"jsonrpc":"2.0","method":...} (no id)
|
||||
```
|
||||
|
||||
## C# Host-Side Architecture
|
||||
|
||||
### Key Classes
|
||||
|
||||
| Class | Role |
|
||||
|-------|------|
|
||||
| `JsonRpcExtensionService` | Discovers, loads, and manages JS extension processes |
|
||||
| `JSExtensionManifest` | Parses and validates `package.json` with `cmdpal` section |
|
||||
| `JSExtensionWrapper` | Manages a single Node.js process lifecycle (implements `IExtensionWrapper`) |
|
||||
| `JsonRpcConnection` | Low-level JSON-RPC 2.0 transport over stdio |
|
||||
| `JSCommandProviderProxy` | Translates `ICommandProvider` interface calls to JSON-RPC requests |
|
||||
| `JSCommandAdapter` | Adapts JSON command data to `ICommand` interface |
|
||||
| `JSListPageProxy` | Adapts JSON list page data to `IListPage` interface |
|
||||
| `JSContentPageProxy` | Adapts JSON content page data to `IContentPage` interface |
|
||||
| `JSIconInfoAdapter` | Adapts JSON icon data (glyphs, base64, data URIs) to `IIconInfo` |
|
||||
| `JSSeparatorAdapter` | Adapts separator markers to `ISeparatorFilterItem` |
|
||||
|
||||
### Adapter Pattern
|
||||
|
||||
The host-side uses an **adapter/proxy pattern** to present JSON-RPC responses as native `ICommand`, `IListPage`, `IContentPage`, etc. interfaces. This allows the existing CmdPal UI (built for WinRT extensions) to consume JS extensions without any changes to the UI layer.
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
UI["UI Layer (unchanged)"] --> CPP["JSCommandProviderProxy: ICommandProvider"] --> CONN["JsonRpcConnection"] --> NODE["Node.js"]
|
||||
|
||||
LPP["JSListPageProxy: IListPage"]
|
||||
CPPG["JSContentPageProxy: IContentPage"]
|
||||
CAD["JSCommandAdapter: ICommand"]
|
||||
IAD["JSIconInfoAdapter: IIconInfo"]
|
||||
|
||||
CPP --> LPP
|
||||
CPP --> CPPG
|
||||
CPP --> CAD
|
||||
CPP --> IAD
|
||||
```
|
||||
|
||||
### Icon Data Pipeline
|
||||
|
||||
JS extensions can provide icons in three formats:
|
||||
|
||||
| Format | `IconData` field | C# handling |
|
||||
|--------|------------------|-------------|
|
||||
| **Font glyph** | `icon: "\uE91B"` | `IconPathConverter.IconSourceMUX` → `FontIconSource` |
|
||||
| **File/URI path** | `icon: "C:\\path\\icon.png"` | `IconPathConverter.IconSourceMUX` → `BitmapImage` |
|
||||
| **Base64 data** | `data: "iVBOR..."` | `JSIconDataAdapter.Data` → `InMemoryRandomAccessStream` → `BitmapImage` |
|
||||
| **Data URI** | `data: "data:image/png;base64,..."` | `JSIconDataAdapter.Data` → parse data URI → decode → stream |
|
||||
|
||||
For base64 images, the TS extension fetches/encodes the image data at runtime. The SDK provides helper functions (`iconFromUrl`, `iconFromFile`, `iconFromBase64`) to simplify this.
|
||||
|
||||
## Security Considerations
|
||||
|
||||
### Current State (v1)
|
||||
|
||||
JS extensions run as **unsandboxed Node.js processes** with the same permissions as the user running CmdPal. This is equivalent to running any Node.js application.
|
||||
|
||||
### Mitigations
|
||||
|
||||
- **Process isolation** — Extensions cannot access host process memory
|
||||
- **No elevated privileges** — Extensions run at the user's permission level, never elevated
|
||||
- **Crash containment** — Runaway extensions are auto-disabled after 3 consecutive crashes
|
||||
- **No network exposure** — Communication is via stdio, not network sockets
|
||||
|
||||
### Future Considerations
|
||||
|
||||
- Permission model for filesystem, network, and clipboard access
|
||||
- Extension signing and trust verification
|
||||
- Sandboxed execution environments (e.g., V8 isolates)
|
||||
@@ -1,603 +0,0 @@
|
||||
# 02 - TypeScript SDK Reference
|
||||
|
||||
> **Package:** `@microsoft/cmdpal-sdk`
|
||||
> **Node.js:** ≥ 22.0.0
|
||||
> **TypeScript:** ≥ 5.8
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install @microsoft/cmdpal-sdk
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Core Types
|
||||
|
||||
### Icon Types
|
||||
|
||||
```typescript
|
||||
interface IconData {
|
||||
icon?: string; // Font glyph character or file/URI path
|
||||
data?: string | null; // Base64-encoded image data or data URI
|
||||
}
|
||||
|
||||
interface IconInfo {
|
||||
light?: IconData; // Icon for light theme
|
||||
dark?: IconData; // Icon for dark theme
|
||||
}
|
||||
```
|
||||
|
||||
Icons can be provided as:
|
||||
- **Font glyphs:** `{ icon: '\uE91B' }` — Segoe Fluent Icons / MDL2 Assets
|
||||
- **File paths:** `{ icon: 'C:\\path\\to\\icon.png' }`
|
||||
- **Base64 data:** `{ data: 'iVBORw0KGgo...' }` — raw base64-encoded image bytes
|
||||
- **Data URIs:** `{ data: 'data:image/png;base64,iVBOR...' }`
|
||||
|
||||
### Color Types
|
||||
|
||||
```typescript
|
||||
interface Color {
|
||||
r: number; // 0-255
|
||||
g: number;
|
||||
b: number;
|
||||
a: number; // 0-255 (default: 255)
|
||||
}
|
||||
|
||||
interface OptionalColor {
|
||||
hasValue: boolean;
|
||||
color?: Color;
|
||||
}
|
||||
```
|
||||
|
||||
### Tags
|
||||
|
||||
```typescript
|
||||
interface Tag {
|
||||
icon?: IconInfo | null;
|
||||
text: string;
|
||||
foreground?: OptionalColor | null;
|
||||
background?: OptionalColor | null;
|
||||
toolTip?: string;
|
||||
}
|
||||
```
|
||||
|
||||
### Key Chords
|
||||
|
||||
```typescript
|
||||
interface KeyChord {
|
||||
modifiers: number; // Bitmask: Ctrl=1, Alt=2, Shift=4, Win=8
|
||||
vkey: number; // Virtual key code
|
||||
scanCode: number;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Command Types
|
||||
|
||||
### ICommand
|
||||
|
||||
The base contract for all commands and pages.
|
||||
|
||||
```typescript
|
||||
interface ICommand {
|
||||
id: string; // Unique identifier
|
||||
name: string; // Display name
|
||||
icon?: IconInfo | null; // Optional icon
|
||||
}
|
||||
```
|
||||
|
||||
### IInvokableCommand
|
||||
|
||||
A command that can be executed.
|
||||
|
||||
```typescript
|
||||
interface IInvokableCommand extends ICommand {
|
||||
invoke(): Promise<CommandResult> | CommandResult;
|
||||
}
|
||||
```
|
||||
|
||||
### CommandResult
|
||||
|
||||
Returned from `invoke()` to tell the host what to do next.
|
||||
|
||||
```typescript
|
||||
type CommandResultKind =
|
||||
| 'dismiss' // Close CmdPal
|
||||
| 'goHome' // Navigate to home
|
||||
| 'goBack' // Navigate back
|
||||
| 'hide' // Hide CmdPal (keep state)
|
||||
| 'keepOpen' // Stay on current page
|
||||
| 'goToPage' // Navigate to a page
|
||||
| 'showToast' // Show toast notification
|
||||
| 'confirm'; // Show confirmation dialog
|
||||
|
||||
interface CommandResult {
|
||||
kind: CommandResultKind;
|
||||
args?: CommandResultArgs;
|
||||
}
|
||||
```
|
||||
|
||||
### Result Args
|
||||
|
||||
```typescript
|
||||
interface GoToPageArgs {
|
||||
pageId: string;
|
||||
navigationMode?: 'push' | 'goBack' | 'goHome';
|
||||
}
|
||||
|
||||
interface ToastArgs {
|
||||
message: string;
|
||||
result?: CommandResult; // What to do after toast is dismissed
|
||||
}
|
||||
|
||||
interface ConfirmationArgs {
|
||||
title: string;
|
||||
description: string;
|
||||
primaryCommand?: ICommand;
|
||||
isPrimaryCommandCritical?: boolean;
|
||||
}
|
||||
```
|
||||
|
||||
### Helper: Creating Results
|
||||
|
||||
```typescript
|
||||
// Navigate to a page
|
||||
{ kind: 'goToPage', args: { pageId: 'my-page', navigationMode: 'push' } }
|
||||
|
||||
// Show a toast
|
||||
{ kind: 'showToast', args: { message: 'Done!' } }
|
||||
|
||||
// Confirmation dialog
|
||||
{ kind: 'confirm', args: { title: 'Delete?', description: 'This cannot be undone.' } }
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Item Types
|
||||
|
||||
### ICommandItem
|
||||
|
||||
A selectable item shown in lists.
|
||||
|
||||
```typescript
|
||||
interface ICommandItem {
|
||||
command: ICommand;
|
||||
moreCommands?: ContextItem[]; // Right-click / overflow menu
|
||||
icon?: IconInfo | null;
|
||||
title: string;
|
||||
subtitle?: string;
|
||||
}
|
||||
```
|
||||
|
||||
### IListItem
|
||||
|
||||
Extended list item with metadata.
|
||||
|
||||
```typescript
|
||||
interface IListItem extends ICommandItem {
|
||||
tags?: Tag[];
|
||||
details?: Details;
|
||||
section?: string; // Section header text (creates visual grouping)
|
||||
textToSuggest?: string; // Text to fill into search box on selection
|
||||
}
|
||||
```
|
||||
|
||||
### IFallbackCommandItem
|
||||
|
||||
A command that receives the user's search query in real-time.
|
||||
|
||||
```typescript
|
||||
interface IFallbackCommandItem extends ICommandItem {
|
||||
fallbackHandler?: IFallbackHandler;
|
||||
displayTitle?: string; // Dynamic title that updates as user types
|
||||
}
|
||||
|
||||
interface IFallbackHandler {
|
||||
updateQuery(query: string): void;
|
||||
}
|
||||
```
|
||||
|
||||
### ContextItem
|
||||
|
||||
An action in a right-click or overflow menu.
|
||||
|
||||
```typescript
|
||||
interface ContextItem {
|
||||
command: ICommand;
|
||||
icon?: IconInfo | null;
|
||||
title: string;
|
||||
subtitle?: string;
|
||||
isCritical?: boolean; // Show in red/destructive style
|
||||
requestedShortcut?: KeyChord; // Keyboard shortcut hint
|
||||
}
|
||||
```
|
||||
|
||||
### Separator
|
||||
|
||||
A visual divider in lists.
|
||||
|
||||
```typescript
|
||||
import { Separator } from '@microsoft/cmdpal-sdk';
|
||||
|
||||
// Untitled separator (horizontal line)
|
||||
new Separator()
|
||||
|
||||
// Section header separator
|
||||
new Separator('Section Title')
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Details Panel
|
||||
|
||||
Rich metadata shown alongside a selected list item.
|
||||
|
||||
```typescript
|
||||
interface Details {
|
||||
heroImage?: IconInfo | null;
|
||||
title?: string;
|
||||
body?: string; // Markdown-formatted body text
|
||||
metadata?: DetailsElement[];
|
||||
}
|
||||
|
||||
interface DetailsElement {
|
||||
key: string; // Label shown to the left
|
||||
data: DetailsData; // Value shown to the right
|
||||
}
|
||||
|
||||
// Discriminated union of detail data types
|
||||
type DetailsData =
|
||||
| DetailsTags // { type: 'tags', tags: Tag[] }
|
||||
| DetailsLink // { type: 'link', link: string, text: string }
|
||||
| DetailsCommands // { type: 'commands', commands: ICommand[] }
|
||||
| DetailsSeparator; // { type: 'separator' }
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Page Types
|
||||
|
||||
### IListPage
|
||||
|
||||
A page that shows a scrollable list of items.
|
||||
|
||||
```typescript
|
||||
interface IListPage extends IPage {
|
||||
searchText?: string;
|
||||
placeholderText?: string;
|
||||
showDetails?: boolean; // Show details panel
|
||||
filters?: Filters | null; // Filter bar
|
||||
gridProperties?: GridProperties | null; // Grid/gallery layout
|
||||
hasMoreItems?: boolean; // Infinite scroll
|
||||
emptyContent?: ICommandItem | null;
|
||||
getItems(): IListItem[] | Promise<IListItem[]>;
|
||||
loadMore?(): void | Promise<void>;
|
||||
}
|
||||
```
|
||||
|
||||
### IDynamicListPage
|
||||
|
||||
A list page that receives search input in real-time.
|
||||
|
||||
```typescript
|
||||
interface IDynamicListPage extends IListPage {
|
||||
setSearchText(text: string): void;
|
||||
}
|
||||
```
|
||||
|
||||
### IContentPage
|
||||
|
||||
A page that displays rich content (markdown, forms, images, trees).
|
||||
|
||||
```typescript
|
||||
interface IContentPage extends IPage {
|
||||
getContent(): Content[] | Promise<Content[]>;
|
||||
details?: Details | null;
|
||||
commands?: ContextItem[];
|
||||
}
|
||||
```
|
||||
|
||||
### Filters
|
||||
|
||||
```typescript
|
||||
interface Filter {
|
||||
id: string;
|
||||
name: string;
|
||||
icon?: IconInfo | null;
|
||||
}
|
||||
|
||||
interface Filters {
|
||||
currentFilterId: string;
|
||||
filters: Array<Filter | { separator: true }>;
|
||||
}
|
||||
```
|
||||
|
||||
### Grid Properties
|
||||
|
||||
```typescript
|
||||
type GridLayoutType = 'small' | 'medium' | 'gallery';
|
||||
|
||||
interface GridProperties {
|
||||
type: GridLayoutType;
|
||||
showTitle?: boolean;
|
||||
showSubtitle?: boolean;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Content Types
|
||||
|
||||
Content pages display an array of `Content` items:
|
||||
|
||||
```typescript
|
||||
type ContentType = 'markdown' | 'form' | 'tree' | 'plainText' | 'image';
|
||||
|
||||
interface MarkdownContent {
|
||||
type: 'markdown';
|
||||
body: string;
|
||||
}
|
||||
|
||||
interface FormContent {
|
||||
type: 'form';
|
||||
templateJson: string; // Adaptive Card JSON template
|
||||
dataJson: string; // Form data values JSON
|
||||
stateJson?: string;
|
||||
submitForm(inputs: string, data: string): CommandResult | Promise<CommandResult>;
|
||||
}
|
||||
|
||||
interface ImageContent {
|
||||
type: 'image';
|
||||
image: IconInfo; // Base64-encoded image data (use iconFromUrl/iconFromFile)
|
||||
maxWidth?: number;
|
||||
maxHeight?: number;
|
||||
}
|
||||
|
||||
interface PlainTextContent {
|
||||
type: 'plainText';
|
||||
text: string;
|
||||
fontFamily?: 'userInterface' | 'monospace';
|
||||
wrapWords?: boolean;
|
||||
}
|
||||
|
||||
interface TreeContent {
|
||||
type: 'tree';
|
||||
rootContent: Content;
|
||||
getChildren(): Content[] | Promise<Content[]>;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Settings
|
||||
|
||||
```typescript
|
||||
import { Settings, ToggleSetting, TextSetting, ChoiceSetSetting } from '@microsoft/cmdpal-sdk';
|
||||
|
||||
const settings = new Settings();
|
||||
|
||||
// Toggle (boolean)
|
||||
settings.add(new ToggleSetting('darkMode', 'Dark Mode', true, 'Enable dark theme'));
|
||||
|
||||
// Text input
|
||||
settings.add(new TextSetting('apiKey', 'API Key', '', 'Your API key'));
|
||||
|
||||
// Choice set (dropdown)
|
||||
settings.add(new ChoiceSetSetting('language', 'Language', [
|
||||
{ title: 'English', value: 'en' },
|
||||
{ title: 'Spanish', value: 'es' },
|
||||
], 'en'));
|
||||
|
||||
// Read values
|
||||
const darkMode = settings.getSetting<ToggleSetting>('darkMode')?.value;
|
||||
|
||||
// Expose in provider
|
||||
class MyProvider extends CommandProviderBase {
|
||||
settings = settings;
|
||||
// ... settings page auto-generated from settings definitions
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Base Classes
|
||||
|
||||
### CommandProviderBase
|
||||
|
||||
The entry point for every extension.
|
||||
|
||||
```typescript
|
||||
class MyProvider extends CommandProviderBase implements ICommandProvider {
|
||||
readonly id = 'my-extension';
|
||||
readonly displayName = 'My Extension';
|
||||
readonly icon = iconFromGlyph('\uE8A5');
|
||||
|
||||
topLevelCommands(): ICommandItem[] {
|
||||
return [ /* ... */ ];
|
||||
}
|
||||
|
||||
// Optional
|
||||
fallbackCommands?(): IFallbackCommandItem[];
|
||||
getCommand?(id: string): ICommand | null;
|
||||
settings?: ICommandSettings;
|
||||
initializeWithHost?(host: IExtensionHost): void;
|
||||
dispose?(): void;
|
||||
}
|
||||
```
|
||||
|
||||
### ListPageBase
|
||||
|
||||
```typescript
|
||||
class MyListPage extends ListPageBase implements IListPage {
|
||||
readonly id = 'my-list';
|
||||
readonly name = 'My List';
|
||||
readonly title = 'My List Page';
|
||||
|
||||
getItems(): IListItem[] {
|
||||
return [ /* ... */ ];
|
||||
}
|
||||
|
||||
// Optional
|
||||
showDetails?: boolean;
|
||||
filters?: Filters;
|
||||
gridProperties?: GridProperties;
|
||||
placeholderText?: string;
|
||||
}
|
||||
```
|
||||
|
||||
### DynamicListPageBase
|
||||
|
||||
```typescript
|
||||
class MySearchPage extends DynamicListPageBase implements IDynamicListPage {
|
||||
readonly id = 'my-search';
|
||||
readonly name = 'Search';
|
||||
readonly title = 'Search Page';
|
||||
private query = '';
|
||||
|
||||
setSearchText(text: string): void {
|
||||
this.query = text;
|
||||
this.notifyItemsChanged(); // Tell host to re-fetch items
|
||||
}
|
||||
|
||||
getItems(): IListItem[] {
|
||||
return allItems.filter(item => item.title.includes(this.query));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### ContentPageBase
|
||||
|
||||
```typescript
|
||||
class MyContentPage extends ContentPageBase implements IContentPage {
|
||||
readonly id = 'my-content';
|
||||
readonly name = 'Content';
|
||||
readonly title = 'My Content Page';
|
||||
|
||||
getContent(): Content[] {
|
||||
return [
|
||||
{ type: 'markdown', body: '# Hello\n\nThis is **markdown** content.' },
|
||||
];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### InvokableCommandBase
|
||||
|
||||
```typescript
|
||||
class MyCommand extends InvokableCommandBase implements IInvokableCommand {
|
||||
readonly id = 'my-command';
|
||||
readonly name = 'Do Something';
|
||||
|
||||
invoke(): CommandResult {
|
||||
// Do work...
|
||||
return { kind: 'showToast', args: { message: 'Done!' } };
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Built-In Commands
|
||||
|
||||
| Class | Purpose | `invoke()` returns |
|
||||
|-------|---------|-------------------|
|
||||
| `NoOpCommand` | Does nothing | `{ kind: 'keepOpen' }` |
|
||||
| `OpenUrlCommand` | Opens a URL in the default browser | `{ kind: 'dismiss' }` |
|
||||
| `CopyTextCommand` | Copies text to clipboard | Toast with copy confirmation |
|
||||
| `ConfirmableCommand` | Shows a confirmation dialog | `{ kind: 'confirm', args: {...} }` |
|
||||
|
||||
---
|
||||
|
||||
## Icon Helpers
|
||||
|
||||
```typescript
|
||||
import { iconFromGlyph, iconFromBase64, iconFromUrl, iconFromFile } from '@microsoft/cmdpal-sdk';
|
||||
|
||||
// Font glyph (Segoe Fluent Icons)
|
||||
const icon = iconFromGlyph('\uE91B');
|
||||
|
||||
// Base64-encoded image data
|
||||
const icon = iconFromBase64('iVBORw0KGgoAAAANSUhEUg...');
|
||||
|
||||
// Fetch image from URL (async — downloads and encodes as base64)
|
||||
const icon = await iconFromUrl('https://example.com/icon.png');
|
||||
|
||||
// Read local file (async — reads and encodes as base64)
|
||||
const icon = await iconFromFile('./assets/icon.png');
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Runtime API
|
||||
|
||||
### ExtensionHost
|
||||
|
||||
Static bridge for communicating with the CmdPal host.
|
||||
|
||||
```typescript
|
||||
import { ExtensionHost } from '@microsoft/cmdpal-sdk';
|
||||
|
||||
// Logging
|
||||
ExtensionHost.log('Something happened');
|
||||
ExtensionHost.log('Error occurred', 'error');
|
||||
|
||||
// Status bar
|
||||
ExtensionHost.showStatus('Loading...', 'info', { isIndeterminate: true });
|
||||
ExtensionHost.hideStatus('loading-id');
|
||||
|
||||
// Clipboard
|
||||
ExtensionHost.copyToClipboard('Hello, clipboard!');
|
||||
```
|
||||
|
||||
### Activation
|
||||
|
||||
```typescript
|
||||
import { activate, startJsonRpcServer } from '@microsoft/cmdpal-sdk';
|
||||
|
||||
// Standard activation pattern
|
||||
startJsonRpcServer(() => {
|
||||
return new MyProvider();
|
||||
});
|
||||
```
|
||||
|
||||
### Notifications
|
||||
|
||||
```typescript
|
||||
import { sendNotification } from '@microsoft/cmdpal-sdk';
|
||||
|
||||
// Tell the host that a list page's items have changed
|
||||
sendNotification('listPage/itemsChanged', { pageId: 'my-list' });
|
||||
|
||||
// Tell the host that a command's properties changed
|
||||
sendNotification('command/propChanged', {
|
||||
commandId: 'my-fallback',
|
||||
properties: { displayTitle: 'Search: query text' },
|
||||
});
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## C# Toolkit Equivalence
|
||||
|
||||
| C# Toolkit | TypeScript SDK |
|
||||
|------------|----------------|
|
||||
| `ListPage` | `ListPageBase` |
|
||||
| `DynamicListPage` | `DynamicListPageBase` |
|
||||
| `ContentPage` | `ContentPageBase` |
|
||||
| `InvokableCommand` | `InvokableCommandBase` |
|
||||
| `CommandItem` | `CommandItemBase` |
|
||||
| `ListItem` | `ListItemBase` |
|
||||
| `FallbackCommandItem` | `FallbackCommandItemBase` |
|
||||
| `Separator` | `Separator` |
|
||||
| `NoOpCommand` | `NoOpCommand` |
|
||||
| `OpenUrlCommand` | `OpenUrlCommand` |
|
||||
| `CopyTextCommand` | `CopyTextCommand` |
|
||||
| `ConfirmableCommand` | `ConfirmableCommand` |
|
||||
| `Settings`/`SettingsPage` | `Settings` (auto-generates `IContentPage`) |
|
||||
| `ToggleSetting` | `ToggleSetting` |
|
||||
| `TextSetting` | `TextSetting` |
|
||||
| `ChoiceSetSetting` | `ChoiceSetSetting` |
|
||||
| `IconHelpers.FromRelativePath` | `iconFromFile` |
|
||||
| `IconInfo.FromStream` | `iconFromBase64` / `iconFromUrl` |
|
||||
@@ -1,509 +0,0 @@
|
||||
# 03 - JSON-RPC Protocol Specification
|
||||
|
||||
## Overview
|
||||
|
||||
Communication between the CmdPal host and JavaScript extensions uses **JSON-RPC 2.0** over **stdio** with **LSP-style `Content-Length` framing**.
|
||||
|
||||
### Framing Format
|
||||
|
||||
Every message (request, response, and notification) is preceded by a header:
|
||||
|
||||
```http
|
||||
Content-Length: <byte-count>\r\n
|
||||
\r\n
|
||||
<UTF-8 JSON body>
|
||||
```
|
||||
|
||||
Where `<byte-count>` is the byte length (not character length) of the JSON body.
|
||||
|
||||
### Connection Properties
|
||||
|
||||
| Property | Value |
|
||||
|----------|-------|
|
||||
| Transport | stdin (host→extension), stdout (extension→host) |
|
||||
| Encoding | UTF-8 |
|
||||
| Protocol | JSON-RPC 2.0 |
|
||||
| Request timeout | 10 seconds |
|
||||
| Concurrency | Requests are serialized (one at a time); notifications can interleave |
|
||||
|
||||
### Message Types
|
||||
|
||||
| Type | Has `id` | Has `method` | Direction |
|
||||
|------|----------|-------------|-----------|
|
||||
| Request | ✅ | ✅ | Host → Extension |
|
||||
| Response | ✅ | ❌ | Extension → Host |
|
||||
| Notification | ❌ | ✅ | Either direction |
|
||||
|
||||
---
|
||||
|
||||
## Host → Extension Requests
|
||||
|
||||
### `initialize`
|
||||
|
||||
Called once after the Node.js process starts. The extension should initialize its provider and return capabilities.
|
||||
|
||||
**Parameters:**
|
||||
```json
|
||||
{
|
||||
"extensionId": "my-extension"
|
||||
}
|
||||
```
|
||||
|
||||
**Response:**
|
||||
```json
|
||||
{
|
||||
"capabilities": ["commands"]
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `provider/getTopLevelCommands`
|
||||
|
||||
Fetches the extension's top-level command items (shown in the main CmdPal list).
|
||||
|
||||
**Parameters:** `null`
|
||||
|
||||
**Response:** Array of command items:
|
||||
```json
|
||||
[
|
||||
{
|
||||
"id": "cmd-1",
|
||||
"title": "My Command",
|
||||
"displayName": "My Command",
|
||||
"subtitle": "Does something useful",
|
||||
"command": {
|
||||
"id": "cmd-1",
|
||||
"name": "My Command",
|
||||
"icon": { "light": { "icon": "\uE8A5" } },
|
||||
"pageType": "dynamicListPage"
|
||||
},
|
||||
"icon": { "light": { "icon": "\uE8A5" } },
|
||||
"moreCommands": []
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
The `command` object includes a `pageType` field when the command represents a page:
|
||||
- `"listPage"` — static list page
|
||||
- `"dynamicListPage"` — search-enabled list page
|
||||
- `"contentPage"` — rich content page
|
||||
- Absent — invokable command (no page)
|
||||
|
||||
---
|
||||
|
||||
### `provider/getFallbackCommands`
|
||||
|
||||
Fetches commands that receive the user's search query when no other results match.
|
||||
|
||||
**Parameters:** `null`
|
||||
|
||||
**Response:** Array of fallback command items, or `null`:
|
||||
```json
|
||||
[
|
||||
{
|
||||
"id": "search-web",
|
||||
"title": "Search the web",
|
||||
"displayName": "Search the web",
|
||||
"command": { "id": "search-web", "name": "Search the web" }
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `provider/getCommand`
|
||||
|
||||
Fetches a specific command/page by ID. Used when navigating to a page.
|
||||
|
||||
**Parameters:**
|
||||
```json
|
||||
{
|
||||
"commandId": "my-page-id"
|
||||
}
|
||||
```
|
||||
|
||||
**Response:** Command object or `null`:
|
||||
```json
|
||||
{
|
||||
"id": "my-page-id",
|
||||
"name": "My Page",
|
||||
"pageType": "listPage",
|
||||
"icon": { "light": { "icon": "\uE8A5" } }
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `provider/getSettings`
|
||||
|
||||
Fetches the extension's settings page ID.
|
||||
|
||||
**Parameters:** `null`
|
||||
|
||||
**Response:**
|
||||
```json
|
||||
{
|
||||
"id": "settings-page-id"
|
||||
}
|
||||
```
|
||||
|
||||
Or `null` if the extension has no settings.
|
||||
|
||||
---
|
||||
|
||||
### `command/invoke`
|
||||
|
||||
Invokes a command by ID.
|
||||
|
||||
**Parameters:**
|
||||
```json
|
||||
{
|
||||
"commandId": "my-command-id"
|
||||
}
|
||||
```
|
||||
|
||||
**Response:** Command result:
|
||||
```json
|
||||
{
|
||||
"Kind": 6,
|
||||
"Args": {
|
||||
"Message": "Operation complete!"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`Kind` values:
|
||||
| Value | Name | Description |
|
||||
|-------|------|-------------|
|
||||
| 0 | Dismiss | Close CmdPal |
|
||||
| 1 | GoHome | Navigate to home |
|
||||
| 2 | GoBack | Navigate back |
|
||||
| 3 | Hide | Hide CmdPal (keep state) |
|
||||
| 4 | KeepOpen | Stay on current page |
|
||||
| 5 | GoToPage | Navigate to page (requires `PageId` in args) |
|
||||
| 6 | ShowToast | Show toast notification (requires `Message` in args) |
|
||||
| 7 | Confirm | Show confirmation dialog |
|
||||
|
||||
---
|
||||
|
||||
### `listPage/getItems`
|
||||
|
||||
Fetches items for a list page.
|
||||
|
||||
**Parameters:**
|
||||
```json
|
||||
{
|
||||
"pageId": "my-list-page"
|
||||
}
|
||||
```
|
||||
|
||||
**Response:**
|
||||
```json
|
||||
{
|
||||
"items": [
|
||||
{
|
||||
"id": "item-1",
|
||||
"title": "Item One",
|
||||
"subtitle": "Description",
|
||||
"section": "Group A",
|
||||
"command": { "id": "item-1-cmd", "name": "Item One" },
|
||||
"icon": { "light": { "icon": "\uE8A5" } },
|
||||
"tags": [{ "text": "New", "foreground": { "hasValue": true, "color": { "r": 255, "g": 255, "b": 255, "a": 255 } } }],
|
||||
"details": {
|
||||
"title": "Item One Details",
|
||||
"body": "**Rich** markdown description",
|
||||
"metadata": [
|
||||
{ "key": "Author", "data": { "type": "tags", "tags": [{ "text": "mjolley" }] } },
|
||||
{ "key": "Link", "data": { "type": "link", "link": "https://github.com", "text": "GitHub" } }
|
||||
]
|
||||
},
|
||||
"moreCommands": [
|
||||
{
|
||||
"command": { "id": "copy-cmd", "name": "Copy" },
|
||||
"title": "Copy to clipboard",
|
||||
"icon": { "light": { "icon": "\uE8C8" } }
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Items with `_isSeparator: true` are rendered as visual separators:
|
||||
```json
|
||||
{
|
||||
"title": "Section Header",
|
||||
"section": "Section Header",
|
||||
"_isSeparator": true,
|
||||
"command": null
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `listPage/setSearchText`
|
||||
|
||||
Updates the search text for a dynamic list page.
|
||||
|
||||
**Parameters:**
|
||||
```json
|
||||
{
|
||||
"pageId": "my-dynamic-page",
|
||||
"searchText": "user query"
|
||||
}
|
||||
```
|
||||
|
||||
**Response:** `null`
|
||||
|
||||
The extension should update its internal state and send a `listPage/itemsChanged` notification when items are ready.
|
||||
|
||||
---
|
||||
|
||||
### `listPage/setFilter`
|
||||
|
||||
Updates the active filter for a list page.
|
||||
|
||||
**Parameters:**
|
||||
```json
|
||||
{
|
||||
"pageId": "my-filtered-page",
|
||||
"filterId": "recent"
|
||||
}
|
||||
```
|
||||
|
||||
**Response:** `null`
|
||||
|
||||
---
|
||||
|
||||
### `listPage/loadMore`
|
||||
|
||||
Requests additional items for infinite scroll.
|
||||
|
||||
**Parameters:**
|
||||
```json
|
||||
{
|
||||
"pageId": "my-page"
|
||||
}
|
||||
```
|
||||
|
||||
**Response:** `null`
|
||||
|
||||
The extension should append items and send a `listPage/itemsChanged` notification.
|
||||
|
||||
---
|
||||
|
||||
### `fallback/updateQuery`
|
||||
|
||||
Updates the search query for a fallback command.
|
||||
|
||||
**Parameters:**
|
||||
```json
|
||||
{
|
||||
"commandId": "search-fallback",
|
||||
"query": "user typed text"
|
||||
}
|
||||
```
|
||||
|
||||
**Response:** `null`
|
||||
|
||||
The extension should update its internal state and send a `command/propChanged` notification to update the display title.
|
||||
|
||||
---
|
||||
|
||||
### `contentPage/getContent`
|
||||
|
||||
Fetches content for a content page.
|
||||
|
||||
**Parameters:**
|
||||
```json
|
||||
{
|
||||
"pageId": "my-content-page"
|
||||
}
|
||||
```
|
||||
|
||||
**Response:** Array of content items:
|
||||
```json
|
||||
[
|
||||
{
|
||||
"type": "markdown",
|
||||
"body": "# Hello\n\nMarkdown content"
|
||||
},
|
||||
{
|
||||
"type": "image",
|
||||
"image": { "light": { "data": "iVBORw0KGgo..." } },
|
||||
"maxWidth": 600,
|
||||
"maxHeight": 400
|
||||
},
|
||||
{
|
||||
"type": "form",
|
||||
"templateJson": "{...adaptive card JSON...}",
|
||||
"dataJson": "{...data values...}"
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `form/submit`
|
||||
|
||||
Submits form data from a content page or form content.
|
||||
|
||||
**Parameters:**
|
||||
```json
|
||||
{
|
||||
"pageId": "my-form-page",
|
||||
"inputs": "{\"name\":\"John\"}",
|
||||
"data": "{}"
|
||||
}
|
||||
```
|
||||
|
||||
**Response:** Command result (same format as `command/invoke`).
|
||||
|
||||
---
|
||||
|
||||
### `dispose`
|
||||
|
||||
Notification sent before the host kills the extension process.
|
||||
|
||||
**Parameters:** `null`
|
||||
|
||||
**Note:** This is a notification (no `id`), not a request. The extension should clean up resources.
|
||||
|
||||
---
|
||||
|
||||
## Extension → Host Notifications
|
||||
|
||||
### `listPage/itemsChanged`
|
||||
|
||||
Tells the host to re-fetch items for a list page.
|
||||
|
||||
```json
|
||||
{
|
||||
"jsonrpc": "2.0",
|
||||
"method": "listPage/itemsChanged",
|
||||
"params": {
|
||||
"pageId": "my-list-page"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `command/propChanged`
|
||||
|
||||
Tells the host that a command's properties have changed (e.g., fallback display title).
|
||||
|
||||
```json
|
||||
{
|
||||
"jsonrpc": "2.0",
|
||||
"method": "command/propChanged",
|
||||
"params": {
|
||||
"commandId": "my-fallback",
|
||||
"properties": {
|
||||
"displayTitle": "Search: new query"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `host/logMessage`
|
||||
|
||||
Sends a log message to the host's logging system.
|
||||
|
||||
```json
|
||||
{
|
||||
"jsonrpc": "2.0",
|
||||
"method": "host/logMessage",
|
||||
"params": {
|
||||
"message": "Extension initialized successfully",
|
||||
"state": 0
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
State values: 0 = Info, 1 = Success, 2 = Warning, 3 = Error
|
||||
|
||||
---
|
||||
|
||||
### `host/showStatus`
|
||||
|
||||
Shows a status message in the CmdPal status bar.
|
||||
|
||||
```json
|
||||
{
|
||||
"jsonrpc": "2.0",
|
||||
"method": "host/showStatus",
|
||||
"params": {
|
||||
"message": {
|
||||
"Message": "Loading data...",
|
||||
"State": 0
|
||||
},
|
||||
"context": "extension"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `host/hideStatus`
|
||||
|
||||
Hides a previously shown status message.
|
||||
|
||||
```json
|
||||
{
|
||||
"jsonrpc": "2.0",
|
||||
"method": "host/hideStatus",
|
||||
"params": {
|
||||
"message": {
|
||||
"Message": "Loading data...",
|
||||
"State": 0
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### `host/copyText`
|
||||
|
||||
Copies text to the system clipboard (since Node.js doesn't have clipboard access).
|
||||
|
||||
```json
|
||||
{
|
||||
"jsonrpc": "2.0",
|
||||
"method": "host/copyText",
|
||||
"params": {
|
||||
"text": "Text to copy to clipboard"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Protocol Summary Table
|
||||
|
||||
| Method | Direction | Type | Purpose |
|
||||
|--------|-----------|------|---------|
|
||||
| `initialize` | Host → Ext | Request | Initialize extension |
|
||||
| `provider/getTopLevelCommands` | Host → Ext | Request | Get top-level commands |
|
||||
| `provider/getFallbackCommands` | Host → Ext | Request | Get fallback commands |
|
||||
| `provider/getCommand` | Host → Ext | Request | Get command by ID |
|
||||
| `provider/getSettings` | Host → Ext | Request | Get settings page |
|
||||
| `command/invoke` | Host → Ext | Request | Invoke a command |
|
||||
| `listPage/getItems` | Host → Ext | Request | Get list page items |
|
||||
| `listPage/setSearchText` | Host → Ext | Request | Update search query |
|
||||
| `listPage/setFilter` | Host → Ext | Request | Update active filter |
|
||||
| `listPage/loadMore` | Host → Ext | Request | Load more items |
|
||||
| `fallback/updateQuery` | Host → Ext | Request | Update fallback query |
|
||||
| `contentPage/getContent` | Host → Ext | Request | Get content page content |
|
||||
| `form/submit` | Host → Ext | Request | Submit form data |
|
||||
| `dispose` | Host → Ext | Notification | Clean up before exit |
|
||||
| `listPage/itemsChanged` | Ext → Host | Notification | Items have changed |
|
||||
| `command/propChanged` | Ext → Host | Notification | Command props changed |
|
||||
| `host/logMessage` | Ext → Host | Notification | Log message |
|
||||
| `host/showStatus` | Ext → Host | Notification | Show status bar message |
|
||||
| `host/hideStatus` | Ext → Host | Notification | Hide status bar message |
|
||||
| `host/copyText` | Ext → Host | Notification | Copy text to clipboard |
|
||||
@@ -1,326 +0,0 @@
|
||||
# 04 - Manifest, Packaging, and Installation
|
||||
|
||||
## Extension Project Structure
|
||||
|
||||
A CmdPal JavaScript extension is a standard Node.js project. Extension metadata is declared in the `cmdpal` field of `package.json` (similar to how VS Code uses `contributes`):
|
||||
|
||||
```
|
||||
my-extension/
|
||||
├── package.json # Node.js manifest + "cmdpal" section (required)
|
||||
├── dist/
|
||||
│ └── index.js # Compiled entry point
|
||||
├── src/
|
||||
│ └── index.ts # TypeScript source
|
||||
├── tsconfig.json # TypeScript config
|
||||
├── node_modules/ # Dependencies (ideally bundled)
|
||||
└── icon.png # Extension icon (optional)
|
||||
```
|
||||
|
||||
The key files:
|
||||
- **`package.json`** — Standard Node.js package manifest with an added `cmdpal` section for CmdPal-specific metadata
|
||||
- **`dist/index.js`** — The compiled JavaScript entry point that CmdPal will execute
|
||||
|
||||
---
|
||||
|
||||
## `package.json` Schema
|
||||
|
||||
CmdPal discovers extensions by finding directories with a `package.json` that contains a `cmdpal` object. Top-level npm fields provide identity; the `cmdpal` section provides CmdPal-specific metadata.
|
||||
|
||||
### Full Example
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "@microsoft/cmdpal-my-extension",
|
||||
"version": "1.0.0",
|
||||
"description": "A brief description of the extension",
|
||||
"main": "dist/index.js",
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"cmdpal": {
|
||||
"displayName": "My Extension",
|
||||
"icon": "icon.png",
|
||||
"publisher": "your-name",
|
||||
"capabilities": ["commands"],
|
||||
"debug": false,
|
||||
"debugPort": 9230
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"dev": "tsc --watch"
|
||||
},
|
||||
"dependencies": {
|
||||
"@microsoft/cmdpal-sdk": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^5.0.0"
|
||||
},
|
||||
"keywords": ["cmdpal", "powertoys", "command-palette"]
|
||||
}
|
||||
```
|
||||
|
||||
### Field Reference
|
||||
|
||||
#### Top-level fields (standard npm)
|
||||
|
||||
| Field | Type | Required | Description |
|
||||
|-------|------|----------|-------------|
|
||||
| `name` | `string` | ✅ | Package identifier. Must be unique across installed extensions. Used as the extension ID. |
|
||||
| `version` | `string` | ❌ | Semantic version string (e.g., `"1.0.0"`). |
|
||||
| `description` | `string` | ❌ | Brief description shown in the extension gallery and settings. |
|
||||
| `main` | `string` | ✅ | Relative path to the entry point JavaScript file. This is what `node` executes. |
|
||||
| `engines.node` | `string` | ❌ | Node.js version requirement (e.g., `">=18"`). |
|
||||
|
||||
#### `cmdpal` section fields
|
||||
|
||||
| Field | Type | Required | Description |
|
||||
|-------|------|----------|-------------|
|
||||
| `displayName` | `string` | ❌ | Human-readable name shown in CmdPal UI. Falls back to `name` if not provided. |
|
||||
| `icon` | `string` | ❌ | Icon glyph character (e.g., `"\uE943"`) or relative path to an icon file (PNG recommended). |
|
||||
| `publisher` | `string` | ❌ | Author or publisher name. |
|
||||
| `capabilities` | `string[]` | ❌ | List of capabilities the extension provides (e.g., `["commands"]`). |
|
||||
| `debug` | `boolean` | ❌ | When `true`, starts Node.js with `--inspect` for debugger attachment. Default: `false`. |
|
||||
| `debugPort` | `integer` | ❌ | Inspector port when `debug` is `true`. If not specified, auto-assigned starting at 9229. |
|
||||
| `main` | `string` | ❌ | Optional override of the top-level `main` field (for packages where the CmdPal entry point differs from the npm main). |
|
||||
|
||||
### Validation Rules
|
||||
|
||||
A `package.json` is recognized as a CmdPal extension if:
|
||||
1. It contains a `cmdpal` object (even if empty: `"cmdpal": {}`)
|
||||
2. `name` is present and non-empty
|
||||
3. Either `cmdpal.main` or top-level `main` resolves to an existing file
|
||||
|
||||
---
|
||||
|
||||
## Installation Directory
|
||||
|
||||
Extensions are installed to:
|
||||
|
||||
```
|
||||
%LOCALAPPDATA%\Microsoft\PowerToys\CommandPalette\JSExtensions\
|
||||
```
|
||||
|
||||
Each extension occupies its own subdirectory:
|
||||
|
||||
```
|
||||
JSExtensions/
|
||||
├── my-extension/
|
||||
│ ├── package.json ← contains "cmdpal" section
|
||||
│ ├── dist/
|
||||
│ │ └── index.js
|
||||
│ └── node_modules/
|
||||
├── another-extension/
|
||||
│ ├── package.json
|
||||
│ └── ...
|
||||
```
|
||||
|
||||
### Discovery
|
||||
|
||||
The `JsonRpcExtensionService` watches this directory with a `FileSystemWatcher`:
|
||||
- **New directory with valid `package.json`** → extension is loaded automatically
|
||||
- **Directory removed** → extension is unloaded, Node.js process is terminated
|
||||
- **`*.js` file changed** within an extension → hot-reload (500ms debounce)
|
||||
|
||||
This means:
|
||||
- Installing an extension = copying its directory to `JSExtensions/`
|
||||
- Uninstalling = deleting the directory
|
||||
- Updating = replacing files (hot-reload handles `*.js` changes)
|
||||
|
||||
---
|
||||
|
||||
## Development Setup
|
||||
|
||||
### Creating a New Extension
|
||||
|
||||
1. **Create the project directory:**
|
||||
```bash
|
||||
mkdir my-extension && cd my-extension
|
||||
npm init -y
|
||||
```
|
||||
|
||||
2. **Install the SDK:**
|
||||
```bash
|
||||
npm install @microsoft/cmdpal-sdk
|
||||
```
|
||||
|
||||
3. **Add the `cmdpal` section to `package.json`:**
|
||||
```json
|
||||
{
|
||||
"name": "my-extension",
|
||||
"version": "1.0.0",
|
||||
"description": "My awesome CmdPal extension",
|
||||
"main": "dist/index.js",
|
||||
"cmdpal": {
|
||||
"displayName": "My Extension",
|
||||
"debug": true
|
||||
},
|
||||
"dependencies": {
|
||||
"@microsoft/cmdpal-sdk": "^1.0.0"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
4. **Create `tsconfig.json`:**
|
||||
```json
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "commonjs",
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"moduleResolution": "node"
|
||||
},
|
||||
"include": ["src/**/*"]
|
||||
}
|
||||
```
|
||||
|
||||
5. **Write your extension** in `src/index.ts` (see [05-getting-started.md](./05-getting-started.md))
|
||||
|
||||
6. **Build:**
|
||||
```bash
|
||||
npx tsc
|
||||
```
|
||||
|
||||
### Development Installation
|
||||
|
||||
For development, symlink or copy your extension to the JSExtensions directory:
|
||||
|
||||
```powershell
|
||||
# Option 1: Copy
|
||||
Copy-Item -Recurse ./my-extension "$env:LOCALAPPDATA\Microsoft\PowerToys\CommandPalette\JSExtensions\my-extension"
|
||||
|
||||
# Option 2: Junction link (recommended for development)
|
||||
New-Item -ItemType Junction -Path "$env:LOCALAPPDATA\Microsoft\PowerToys\CommandPalette\JSExtensions\my-extension" -Target (Resolve-Path ./my-extension)
|
||||
```
|
||||
|
||||
With a junction link, changes to your source files are reflected immediately (after build). The `*.js` file watcher triggers hot-reload automatically.
|
||||
|
||||
### Debugging
|
||||
|
||||
1. Set `"debug": true` in the `cmdpal` section of `package.json`
|
||||
2. Optionally set `"debugPort": 9230` (or any available port)
|
||||
3. Open Chrome DevTools: `chrome://inspect` or attach VS Code's debugger
|
||||
4. The Node.js process starts with `--inspect=0.0.0.0:<port>`, ready for debugger attachment
|
||||
|
||||
---
|
||||
|
||||
## Production Packaging
|
||||
|
||||
### npm Package Structure
|
||||
|
||||
Extensions are distributed as standard npm packages. The recommended `package.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "@publisher/cmdpal-my-extension",
|
||||
"version": "1.0.0",
|
||||
"description": "My CmdPal extension",
|
||||
"main": "dist/index.js",
|
||||
"cmdpal": {
|
||||
"displayName": "My Extension",
|
||||
"icon": "icon.png",
|
||||
"publisher": "your-name",
|
||||
"capabilities": ["commands"]
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"prepublishOnly": "npm run build"
|
||||
},
|
||||
"files": [
|
||||
"dist/",
|
||||
"icon.png"
|
||||
],
|
||||
"dependencies": {
|
||||
"@microsoft/cmdpal-sdk": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^5.0.0"
|
||||
},
|
||||
"keywords": ["cmdpal", "powertoys", "command-palette"],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Naming Convention
|
||||
|
||||
Recommended npm package naming: `@publisher/cmdpal-<name>` or `cmdpal-<name>`.
|
||||
|
||||
The `cmdpal-` prefix helps with discoverability and could be used for future npm-based discovery.
|
||||
|
||||
---
|
||||
|
||||
## Extension Gallery Integration
|
||||
|
||||
|
||||
### Gallery Manifest Entry
|
||||
|
||||
The existing CmdPal extension gallery pulls from a manifest that lists available extensions. For JavaScript extensions, the manifest entry includes npm package details:
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "my-extension",
|
||||
"displayName": "My Extension",
|
||||
"description": "Does amazing things",
|
||||
"publisher": "your-name",
|
||||
"version": "1.0.0",
|
||||
"icon": "https://example.com/icon.png",
|
||||
"type": "jsonrpc",
|
||||
"npm": {
|
||||
"package": "@publisher/cmdpal-my-extension",
|
||||
"registry": "https://registry.npmjs.org"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The `type: "jsonrpc"` field distinguishes JavaScript extensions from COM-based extensions.
|
||||
|
||||
### Installation Flow
|
||||
|
||||
When a user clicks "Install" for a JavaScript extension in the gallery:
|
||||
|
||||
1. CmdPal runs `npm install <package>` in the JSExtensions directory
|
||||
2. The package is installed to `JSExtensions/<name>/`
|
||||
3. `FileSystemWatcher` detects the new directory → extension loads automatically
|
||||
4. The extension appears in the main CmdPal list
|
||||
|
||||
### Uninstallation Flow
|
||||
|
||||
When a user clicks "Uninstall":
|
||||
|
||||
1. CmdPal terminates the extension's Node.js process
|
||||
2. The extension directory is deleted from `JSExtensions/`
|
||||
3. `FileSystemWatcher` detects the removal → extension is unloaded
|
||||
|
||||
---
|
||||
|
||||
## Security Considerations
|
||||
|
||||
### Process Isolation
|
||||
|
||||
Each JavaScript extension runs in its own Node.js process:
|
||||
- Separate memory space
|
||||
- Separate event loop
|
||||
- No direct access to other extensions or CmdPal internals
|
||||
- Communication only through the JSON-RPC protocol
|
||||
|
||||
### Permissions
|
||||
|
||||
Currently, JavaScript extensions have the same permissions as the Node.js process:
|
||||
- File system access
|
||||
- Network access
|
||||
- Process spawning
|
||||
|
||||
Future considerations:
|
||||
- Extension permission declarations in `package.json` `cmdpal` section
|
||||
- User consent prompts for sensitive permissions
|
||||
- Sandboxing via Node.js `--experimental-policy` or similar mechanisms
|
||||
|
||||
### Trust Model
|
||||
|
||||
- Extensions installed from the gallery are implicitly trusted by the user
|
||||
- Sideloaded extensions (copied to JSExtensions/) have no verification
|
||||
@@ -1,484 +0,0 @@
|
||||
# 05 — Getting Started: Build Your First CmdPal Extension
|
||||
|
||||
This guide walks you through building a CmdPal JavaScript extension from scratch. By the end, you'll have a working extension with a searchable list page, a content page, and settings.
|
||||
|
||||
---
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- **Node.js 18+** installed and on your PATH
|
||||
- **PowerToys** with Command Palette enabled
|
||||
- A text editor (VS Code recommended)
|
||||
|
||||
---
|
||||
|
||||
## Step 1: Scaffold the Project
|
||||
|
||||
```bash
|
||||
mkdir my-first-extension && cd my-first-extension
|
||||
npm init -y
|
||||
npm install @microsoft/cmdpal-sdk
|
||||
npm install --save-dev typescript
|
||||
```
|
||||
|
||||
Create `tsconfig.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "commonjs",
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"moduleResolution": "node"
|
||||
},
|
||||
"include": ["src/**/*"]
|
||||
}
|
||||
```
|
||||
|
||||
Add the `cmdpal` section to your `package.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "my-first-extension",
|
||||
"version": "1.0.0",
|
||||
"description": "A tutorial CmdPal extension",
|
||||
"main": "dist/index.js",
|
||||
"cmdpal": {
|
||||
"displayName": "My First Extension",
|
||||
"debug": true
|
||||
},
|
||||
"dependencies": {
|
||||
"@microsoft/cmdpal-sdk": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^5.0.0"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Step 2: Create a Simple Command
|
||||
|
||||
Create `src/index.ts`:
|
||||
|
||||
```typescript
|
||||
import {
|
||||
CommandProvider,
|
||||
Command,
|
||||
ListPage,
|
||||
ListItem,
|
||||
CommandResultKind,
|
||||
run,
|
||||
iconFromGlyph,
|
||||
} from "@microsoft/cmdpal-sdk";
|
||||
|
||||
// Define a simple command that shows a toast
|
||||
class GreetCommand extends Command {
|
||||
constructor() {
|
||||
super({
|
||||
id: "greet",
|
||||
name: "Say Hello",
|
||||
icon: iconFromGlyph("\uE76E"), // Checkmark icon
|
||||
});
|
||||
}
|
||||
|
||||
async invoke(): Promise<{ kind: CommandResultKind }> {
|
||||
return {
|
||||
kind: CommandResultKind.ShowToast,
|
||||
args: { Message: "Hello from my extension!" },
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Define the main list page
|
||||
class MainPage extends ListPage {
|
||||
constructor() {
|
||||
super({
|
||||
id: "main-page",
|
||||
name: "My First Extension",
|
||||
icon: iconFromGlyph("\uE8A5"),
|
||||
});
|
||||
}
|
||||
|
||||
async getItems(): Promise<ListItem[]> {
|
||||
return [
|
||||
new ListItem({
|
||||
title: "Say Hello",
|
||||
subtitle: "Shows a greeting toast",
|
||||
command: new GreetCommand(),
|
||||
icon: iconFromGlyph("\uE76E"),
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// Define the provider
|
||||
class MyProvider extends CommandProvider {
|
||||
constructor() {
|
||||
super("my-first-extension", "My First Extension");
|
||||
}
|
||||
|
||||
getTopLevelCommands() {
|
||||
return [
|
||||
new ListItem({
|
||||
title: "My First Extension",
|
||||
subtitle: "A tutorial extension",
|
||||
command: new MainPage(),
|
||||
icon: iconFromGlyph("\uE8A5"),
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// Start the extension
|
||||
run(new MyProvider());
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Step 3: Build and Install
|
||||
|
||||
```bash
|
||||
# Build
|
||||
npx tsc
|
||||
|
||||
# Install (junction link for development)
|
||||
$extensionsDir = "$env:LOCALAPPDATA\Microsoft\PowerToys\CommandPalette\JSExtensions"
|
||||
New-Item -ItemType Junction -Path "$extensionsDir\my-first-extension" -Target (Resolve-Path .)
|
||||
```
|
||||
|
||||
Open CmdPal — you should see "My First Extension" in the list. Click it to see your command, then click "Say Hello" to see the toast!
|
||||
|
||||
---
|
||||
|
||||
## Step 4: Add a Dynamic List Page (Search)
|
||||
|
||||
Let's add a searchable list that filters items as you type:
|
||||
|
||||
```typescript
|
||||
import {
|
||||
DynamicListPage,
|
||||
// ... other imports
|
||||
} from "@microsoft/cmdpal-sdk";
|
||||
|
||||
class SearchablePage extends DynamicListPage {
|
||||
private allItems = [
|
||||
{ title: "Apple", emoji: "🍎" },
|
||||
{ title: "Banana", emoji: "🍌" },
|
||||
{ title: "Cherry", emoji: "🍒" },
|
||||
{ title: "Dragon Fruit", emoji: "🐉" },
|
||||
{ title: "Elderberry", emoji: "🫐" },
|
||||
];
|
||||
|
||||
constructor() {
|
||||
super({
|
||||
id: "searchable-page",
|
||||
name: "Fruit Search",
|
||||
icon: iconFromGlyph("\uE721"), // Search icon
|
||||
placeholderText: "Search fruits...",
|
||||
});
|
||||
}
|
||||
|
||||
async getItems(): Promise<ListItem[]> {
|
||||
const query = this.searchText?.toLowerCase() ?? "";
|
||||
return this.allItems
|
||||
.filter((item) => item.title.toLowerCase().includes(query))
|
||||
.map(
|
||||
(item) =>
|
||||
new ListItem({
|
||||
title: `${item.emoji} ${item.title}`,
|
||||
subtitle: "A delicious fruit",
|
||||
command: new ToastCommand(item.title),
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ToastCommand extends Command {
|
||||
private fruit: string;
|
||||
|
||||
constructor(fruit: string) {
|
||||
super({ id: `toast-${fruit}`, name: `Select ${fruit}` });
|
||||
this.fruit = fruit;
|
||||
}
|
||||
|
||||
async invoke() {
|
||||
return {
|
||||
kind: CommandResultKind.ShowToast,
|
||||
args: { Message: `You selected ${this.fruit}!` },
|
||||
};
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Add the searchable page to your `MainPage.getItems()`:
|
||||
|
||||
```typescript
|
||||
new ListItem({
|
||||
title: "Fruit Search",
|
||||
subtitle: "Search through a list of fruits",
|
||||
command: new SearchablePage(),
|
||||
icon: iconFromGlyph("\uE721"),
|
||||
}),
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Step 5: Add a Content Page
|
||||
|
||||
Content pages display rich content — markdown, images, and forms:
|
||||
|
||||
```typescript
|
||||
import {
|
||||
ContentPage,
|
||||
MarkdownContent,
|
||||
ImageContent,
|
||||
FormContent,
|
||||
iconFromUrl,
|
||||
// ... other imports
|
||||
} from "@microsoft/cmdpal-sdk";
|
||||
|
||||
class AboutPage extends ContentPage {
|
||||
constructor() {
|
||||
super({
|
||||
id: "about-page",
|
||||
name: "About",
|
||||
icon: iconFromGlyph("\uE946"), // Info icon
|
||||
});
|
||||
}
|
||||
|
||||
async getContent() {
|
||||
return [
|
||||
new MarkdownContent({
|
||||
body: `# My First Extension
|
||||
|
||||
This extension was built with the CmdPal TypeScript SDK.
|
||||
|
||||
## Features
|
||||
- **Simple commands** with toast notifications
|
||||
- **Searchable lists** with dynamic filtering
|
||||
- **Rich content** with markdown and images
|
||||
`,
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Step 6: Add Settings
|
||||
|
||||
Extensions can have a settings page using Adaptive Cards:
|
||||
|
||||
```typescript
|
||||
import {
|
||||
FormPage,
|
||||
// ... other imports
|
||||
} from "@microsoft/cmdpal-sdk";
|
||||
|
||||
class SettingsPage extends FormPage {
|
||||
constructor() {
|
||||
super({
|
||||
id: "settings",
|
||||
name: "Settings",
|
||||
icon: iconFromGlyph("\uE713"),
|
||||
});
|
||||
}
|
||||
|
||||
async getContent() {
|
||||
return [
|
||||
new FormContent({
|
||||
templateJson: JSON.stringify({
|
||||
type: "AdaptiveCard",
|
||||
body: [
|
||||
{
|
||||
type: "Input.Text",
|
||||
id: "greeting",
|
||||
label: "Custom Greeting",
|
||||
placeholder: "Enter a greeting...",
|
||||
value: "Hello",
|
||||
},
|
||||
{
|
||||
type: "Input.Toggle",
|
||||
id: "showEmoji",
|
||||
title: "Show emoji in results",
|
||||
value: "true",
|
||||
},
|
||||
],
|
||||
actions: [
|
||||
{
|
||||
type: "Action.Submit",
|
||||
title: "Save",
|
||||
},
|
||||
],
|
||||
$schema: "http://adaptivecards.io/schemas/adaptive-card.json",
|
||||
version: "1.5",
|
||||
}),
|
||||
dataJson: JSON.stringify({
|
||||
greeting: "Hello",
|
||||
showEmoji: "true",
|
||||
}),
|
||||
}),
|
||||
];
|
||||
}
|
||||
|
||||
async onFormSubmit(inputs: Record<string, string>) {
|
||||
// Save settings (e.g., to a file or state)
|
||||
console.log("Settings saved:", inputs);
|
||||
return {
|
||||
kind: CommandResultKind.ShowToast,
|
||||
args: { Message: "Settings saved!" },
|
||||
};
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Register the settings page in your provider:
|
||||
|
||||
```typescript
|
||||
class MyProvider extends CommandProvider {
|
||||
constructor() {
|
||||
super("my-first-extension", "My First Extension");
|
||||
}
|
||||
|
||||
getSettings() {
|
||||
return new SettingsPage();
|
||||
}
|
||||
|
||||
// ... getTopLevelCommands
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Step 7: Add Context Commands and Details
|
||||
|
||||
List items can have context menu commands and a details panel:
|
||||
|
||||
```typescript
|
||||
import {
|
||||
CopyTextCommand,
|
||||
OpenUrlCommand,
|
||||
Details,
|
||||
DetailsMetadata,
|
||||
TagsData,
|
||||
Tag,
|
||||
LinkData,
|
||||
// ... other imports
|
||||
} from "@microsoft/cmdpal-sdk";
|
||||
|
||||
// In your getItems():
|
||||
new ListItem({
|
||||
title: "GitHub",
|
||||
subtitle: "Open GitHub in your browser",
|
||||
command: new OpenUrlCommand({
|
||||
id: "open-github",
|
||||
name: "Open GitHub",
|
||||
url: "https://github.com",
|
||||
}),
|
||||
icon: iconFromGlyph("\uE774"),
|
||||
tags: [
|
||||
new Tag({ text: "Web", foreground: { r: 100, g: 200, b: 255, a: 255 } }),
|
||||
],
|
||||
details: new Details({
|
||||
title: "GitHub",
|
||||
body: "The world's leading software development platform.",
|
||||
heroImage: iconFromGlyph("\uE774"),
|
||||
metadata: [
|
||||
new DetailsMetadata({
|
||||
key: "URL",
|
||||
data: new LinkData({
|
||||
link: "https://github.com",
|
||||
text: "github.com",
|
||||
}),
|
||||
}),
|
||||
new DetailsMetadata({
|
||||
key: "Tags",
|
||||
data: new TagsData({
|
||||
tags: [new Tag({ text: "Development" }), new Tag({ text: "Git" })],
|
||||
}),
|
||||
}),
|
||||
],
|
||||
}),
|
||||
moreCommands: [
|
||||
{
|
||||
title: "Copy URL",
|
||||
command: new CopyTextCommand({
|
||||
id: "copy-github-url",
|
||||
name: "Copy URL",
|
||||
text: "https://github.com",
|
||||
}),
|
||||
icon: iconFromGlyph("\uE8C8"),
|
||||
},
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Step 8: Rebuild and Test
|
||||
|
||||
```bash
|
||||
# Rebuild after changes
|
||||
npx tsc
|
||||
```
|
||||
|
||||
CmdPal watches for `*.js` file changes and hot-reloads automatically. After running `tsc`, your extension will reload within ~500ms.
|
||||
|
||||
---
|
||||
|
||||
## Debugging Tips
|
||||
|
||||
### Enable Debug Mode
|
||||
|
||||
Set `"debug": true` in the `cmdpal` section of your `package.json`. The Node.js process starts with `--inspect`, allowing you to attach a debugger.
|
||||
|
||||
### Attach VS Code Debugger
|
||||
|
||||
Add to `.vscode/launch.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "node",
|
||||
"request": "attach",
|
||||
"name": "Attach to CmdPal Extension",
|
||||
"port": 9229,
|
||||
"restart": true,
|
||||
"skipFiles": ["<node_internals>/**"]
|
||||
}
|
||||
```
|
||||
|
||||
### View Logs
|
||||
|
||||
Use `host.log()` in your extension to send messages to the CmdPal log:
|
||||
|
||||
```typescript
|
||||
// Inside any command or page method:
|
||||
host.log("Fetching items...", MessageState.Info);
|
||||
host.log("Error occurred!", MessageState.Error);
|
||||
```
|
||||
|
||||
### Common Issues
|
||||
|
||||
| Issue | Solution |
|
||||
|-------|----------|
|
||||
| Extension not showing | Check `package.json` has a `cmdpal` section and valid `name` + `main` |
|
||||
| Blank page | Check `getItems()` or `getContent()` is returning data |
|
||||
| Command does nothing | Ensure `invoke()` returns a valid `CommandResultKind` |
|
||||
| Images not loading | Use `iconFromUrl()` or `iconFromBase64()` helpers |
|
||||
| Settings crash | Ensure form template JSON is valid Adaptive Card schema |
|
||||
|
||||
---
|
||||
|
||||
## Next Steps
|
||||
|
||||
- Read the [TypeScript SDK Reference](./02-typescript-sdk.md) for the full API
|
||||
- Read the [JSON-RPC Protocol Specification](./03-jsonrpc-protocol.md) to understand the wire format
|
||||
- Check out the [Sample Extension](../src/modules/cmdpal/ext/SampleJSExtension/) for a comprehensive example
|
||||
- Read the [Architecture Overview](./01-architecture.md) for how it all fits together
|
||||
@@ -1,83 +0,0 @@
|
||||
# Command Palette JavaScript Extension System Design Specification
|
||||
|
||||
> **Status:** Draft — seeking community and internal feedback<br/>
|
||||
> **Last updated:** 2006-06-17
|
||||
|
||||
## Table of Contents
|
||||
|
||||
| Document | Description |
|
||||
|----------|-------------|
|
||||
| [01 — Architecture Overview](01-architecture.md) | Process model, extension lifecycle, transport, and security |
|
||||
| [02 — TypeScript SDK Reference](02-typescript-sdk.md) | Full API surface: types, base classes, helpers, and runtime |
|
||||
| [03 — JSON-RPC Protocol](03-jsonrpc-protocol.md) | Complete protocol specification: methods, notifications, framing |
|
||||
| [04 — Extension Manifest & Packaging](04-manifest-packaging.md) | `package.json` schema, project structure, distribution |
|
||||
|
||||
---
|
||||
|
||||
## Executive Summary
|
||||
|
||||
Command Palette (CmdPal) is extending its extension model beyond in-process WinRT/COM extensions to support **JavaScript and TypeScript extensions** that run as isolated Node.js processes, communicating with the host over JSON-RPC 2.0 via stdio.
|
||||
|
||||
### Goals
|
||||
|
||||
1. **Developer accessibility** — Let web developers build CmdPal extensions using familiar tools (TypeScript, npm, Node.js)
|
||||
2. **Process isolation** — Extension crashes don't take down CmdPal; extensions can't corrupt host state
|
||||
3. **Type-safe SDK** — Full TypeScript type definitions mirroring the C# toolkit surface
|
||||
4. **Developer experience** — Hot-reload on file changes, debugger attachment, familiar project structure
|
||||
5. **Feature parity** — JS extensions can create list pages, content pages, forms, grids, settings, and more
|
||||
|
||||
### Non-Goals (v1)
|
||||
|
||||
- Browser/WebView-based extension UI rendering
|
||||
- Sandboxed filesystem access or permission model
|
||||
- Extension marketplace / auto-update infrastructure
|
||||
- Multi-language JSONRPC support beyond JavaScript/TypeScript (Python, Go, etc.)
|
||||
|
||||
### Architecture at a Glance
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
subgraph Host["Command Palette (WinUI Host)"]
|
||||
subgraph JS["JsonRpcExtensionService"]
|
||||
A["JSExtensionWrapper (ext A)"] --> ARPC["JsonRpcConnection\n(stdio, LSP framing)"]
|
||||
B["JSExtensionWrapper (ext B)"] --> BRPC["JsonRpcConnection\n(stdio, LSP framing)"]
|
||||
N["... one process per extension"]
|
||||
end
|
||||
|
||||
W["WinRTExtensionService\n(existing COM/WinRT extensions)"]
|
||||
BI["BuiltInExtensionService\n(built-in extensions)"]
|
||||
end
|
||||
|
||||
ARPC <-->|"JSON-RPC 2.0 over stdio"| NODEA["node ext-a.js\n(TS SDK runtime)"]
|
||||
BRPC <-->|"JSON-RPC 2.0 over stdio"| NODEB["node ext-b.js\n(TS SDK runtime)"]
|
||||
```
|
||||
|
||||
Each JS extension runs in its own Node.js process. The host spawns the process, establishes a JSON-RPC 2.0 connection over stdin/stdout with LSP-style `Content-Length` framing, sends an `initialize` request, and then queries the extension for commands, pages, and content as the user navigates.
|
||||
|
||||
### Key Design Decisions
|
||||
|
||||
| Decision | Choice | Rationale |
|
||||
|----------|--------|-----------|
|
||||
| Process model | One Node.js process per extension | Isolation, independent crash recovery, independent debugging |
|
||||
| Transport | stdio with LSP framing | No port conflicts, no network exposure, proven by LSP ecosystem |
|
||||
| Protocol | JSON-RPC 2.0 | Standard, well-tooled, bidirectional |
|
||||
| SDK language | TypeScript | Type safety, npm ecosystem, familiar to web developers |
|
||||
| Entry point | `cmdpal` field in `package.json` | Simple, declarative, same pattern as VS Code's contributions |
|
||||
| Icon data | Base64-encoded in JSON | No filesystem sharing needed, works with generated/fetched images |
|
||||
| Hot-reload | FileSystemWatcher on `*.js` | Immediate feedback during development |
|
||||
|
||||
---
|
||||
|
||||
## Feedback Requested
|
||||
|
||||
We are seeking feedback on the following areas:
|
||||
|
||||
1. **API surface** — Are the base classes and types intuitive? What's missing?
|
||||
2. **Extension lifecycle** — Is the initialize → query → dispose model sufficient?
|
||||
3. **Manifest schema** — What additional fields would be useful?
|
||||
4. **Distribution** — Should we support npm-based installation? Local-only? Both?
|
||||
5. **Security** — What permission boundaries should exist for JS extensions?
|
||||
6. **Developer experience** — What tooling (CLI scaffolding, debugging, testing) is most important?
|
||||
7. **Performance** — Are there concerns about per-extension Node.js processes?
|
||||
|
||||
Please file issues with the tag `[CmdPal-JS-SDK]`.
|
||||
@@ -19,5 +19,6 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
Google,
|
||||
AzureAIInference,
|
||||
Ollama,
|
||||
PhiSilica,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
"google" or "googleai" or "googlegemini" => AIServiceType.Google,
|
||||
"azureaiinference" or "azureinference" => AIServiceType.AzureAIInference,
|
||||
"ollama" => AIServiceType.Ollama,
|
||||
"phisilica" or "phi" or "philm" => AIServiceType.PhiSilica,
|
||||
_ => AIServiceType.Unknown,
|
||||
};
|
||||
}
|
||||
@@ -51,6 +52,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
AIServiceType.Google => "Google",
|
||||
AIServiceType.AzureAIInference => "AzureAIInference",
|
||||
AIServiceType.Ollama => "Ollama",
|
||||
AIServiceType.PhiSilica => "PhiSilica",
|
||||
AIServiceType.Unknown => string.Empty,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(serviceType), serviceType, "Unsupported AI service type."),
|
||||
};
|
||||
@@ -72,6 +74,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
AIServiceType.Google => "google",
|
||||
AIServiceType.AzureAIInference => "azureaiinference",
|
||||
AIServiceType.Ollama => "ollama",
|
||||
AIServiceType.PhiSilica => "phisilica",
|
||||
_ => string.Empty,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -118,6 +118,15 @@ public static class AIServiceTypeRegistry
|
||||
PrivacyLabel = "AdvancedPaste_OpenAI_PrivacyLabel",
|
||||
PrivacyUri = new Uri("https://openai.com/privacy"),
|
||||
},
|
||||
[AIServiceType.PhiSilica] = new AIServiceTypeMetadata
|
||||
{
|
||||
ServiceType = AIServiceType.PhiSilica,
|
||||
DisplayName = "Phi Silica",
|
||||
IconPath = "ms-appx:///Assets/Settings/Icons/Models/WindowsML.svg",
|
||||
IsOnlineService = false,
|
||||
IsLocalModel = true,
|
||||
LegalDescription = "AdvancedPaste_LocalModel_LegalDescription",
|
||||
},
|
||||
[AIServiceType.Unknown] = new AIServiceTypeMetadata
|
||||
{
|
||||
ServiceType = AIServiceType.Unknown,
|
||||
|
||||
@@ -12,7 +12,15 @@ namespace Microsoft.PowerToys.Settings.UI.Library;
|
||||
public sealed partial class AdvancedPasteAdditionalAction : Observable, IAdvancedPasteAction
|
||||
{
|
||||
private HotkeySettings _shortcut = new();
|
||||
private HotkeySettings _coachingShortcut = new();
|
||||
private bool _isShown;
|
||||
private string _prompt = string.Empty;
|
||||
private string _systemPrompt = string.Empty;
|
||||
private string _coachingPrompt = string.Empty;
|
||||
private string _coachingSystemPrompt = string.Empty;
|
||||
private string _providerId = string.Empty;
|
||||
private string _coachingProviderId = string.Empty;
|
||||
private bool _coachingEnabled;
|
||||
private bool _hasConflict;
|
||||
private string _tooltip;
|
||||
|
||||
@@ -33,6 +41,20 @@ public sealed partial class AdvancedPasteAdditionalAction : Observable, IAdvance
|
||||
}
|
||||
}
|
||||
|
||||
[JsonPropertyName("coaching-shortcut")]
|
||||
public HotkeySettings CoachingShortcut
|
||||
{
|
||||
get => _coachingShortcut;
|
||||
set
|
||||
{
|
||||
if (_coachingShortcut != value)
|
||||
{
|
||||
_coachingShortcut = value ?? new();
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[JsonPropertyName("isShown")]
|
||||
public bool IsShown
|
||||
{
|
||||
@@ -40,6 +62,55 @@ public sealed partial class AdvancedPasteAdditionalAction : Observable, IAdvance
|
||||
set => Set(ref _isShown, value);
|
||||
}
|
||||
|
||||
[JsonPropertyName("prompt")]
|
||||
public string Prompt
|
||||
{
|
||||
get => _prompt;
|
||||
set => Set(ref _prompt, value ?? string.Empty);
|
||||
}
|
||||
|
||||
[JsonPropertyName("system-prompt")]
|
||||
public string SystemPrompt
|
||||
{
|
||||
get => _systemPrompt;
|
||||
set => Set(ref _systemPrompt, value ?? string.Empty);
|
||||
}
|
||||
|
||||
[JsonPropertyName("coaching-prompt")]
|
||||
public string CoachingPrompt
|
||||
{
|
||||
get => _coachingPrompt;
|
||||
set => Set(ref _coachingPrompt, value ?? string.Empty);
|
||||
}
|
||||
|
||||
[JsonPropertyName("coaching-system-prompt")]
|
||||
public string CoachingSystemPrompt
|
||||
{
|
||||
get => _coachingSystemPrompt;
|
||||
set => Set(ref _coachingSystemPrompt, value ?? string.Empty);
|
||||
}
|
||||
|
||||
[JsonPropertyName("provider-id")]
|
||||
public string ProviderId
|
||||
{
|
||||
get => _providerId;
|
||||
set => Set(ref _providerId, value ?? string.Empty);
|
||||
}
|
||||
|
||||
[JsonPropertyName("coaching-provider-id")]
|
||||
public string CoachingProviderId
|
||||
{
|
||||
get => _coachingProviderId;
|
||||
set => Set(ref _coachingProviderId, value ?? string.Empty);
|
||||
}
|
||||
|
||||
[JsonPropertyName("coaching-enabled")]
|
||||
public bool CoachingEnabled
|
||||
{
|
||||
get => _coachingEnabled;
|
||||
set => Set(ref _coachingEnabled, value);
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public bool HasConflict
|
||||
{
|
||||
|
||||
@@ -11,12 +11,14 @@ namespace Microsoft.PowerToys.Settings.UI.Library;
|
||||
public sealed class AdvancedPasteAdditionalActions
|
||||
{
|
||||
private AdvancedPasteAdditionalAction _imageToText = new();
|
||||
private AdvancedPasteAdditionalAction _fixSpellingAndGrammar = new();
|
||||
private AdvancedPastePasteAsFileAction _pasteAsFile = new();
|
||||
private AdvancedPasteTranscodeAction _transcode = new();
|
||||
|
||||
public static class PropertyNames
|
||||
{
|
||||
public const string ImageToText = "image-to-text";
|
||||
public const string FixSpellingAndGrammar = "fix-spelling-and-grammar";
|
||||
public const string PasteAsFile = "paste-as-file";
|
||||
public const string Transcode = "transcode";
|
||||
}
|
||||
@@ -28,6 +30,13 @@ public sealed class AdvancedPasteAdditionalActions
|
||||
init => _imageToText = value ?? new();
|
||||
}
|
||||
|
||||
[JsonPropertyName(PropertyNames.FixSpellingAndGrammar)]
|
||||
public AdvancedPasteAdditionalAction FixSpellingAndGrammar
|
||||
{
|
||||
get => _fixSpellingAndGrammar;
|
||||
init => _fixSpellingAndGrammar = value ?? new();
|
||||
}
|
||||
|
||||
[JsonPropertyName(PropertyNames.PasteAsFile)]
|
||||
public AdvancedPastePasteAsFileAction PasteAsFile
|
||||
{
|
||||
@@ -44,7 +53,7 @@ public sealed class AdvancedPasteAdditionalActions
|
||||
|
||||
public IEnumerable<IAdvancedPasteAction> GetAllActions()
|
||||
{
|
||||
return GetAllActionsRecursive([ImageToText, PasteAsFile, Transcode]);
|
||||
return GetAllActionsRecursive([ImageToText, FixSpellingAndGrammar, PasteAsFile, Transcode]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -16,6 +16,7 @@ public sealed class AdvancedPasteCustomAction : Observable, IAdvancedPasteAction
|
||||
private string _name = string.Empty;
|
||||
private string _description = string.Empty;
|
||||
private string _prompt = string.Empty;
|
||||
private string _providerId = string.Empty;
|
||||
private HotkeySettings _shortcut = new();
|
||||
private bool _isShown;
|
||||
private bool _canMoveUp;
|
||||
@@ -64,6 +65,13 @@ public sealed class AdvancedPasteCustomAction : Observable, IAdvancedPasteAction
|
||||
}
|
||||
}
|
||||
|
||||
[JsonPropertyName("provider-id")]
|
||||
public string ProviderId
|
||||
{
|
||||
get => _providerId;
|
||||
set => Set(ref _providerId, value ?? string.Empty);
|
||||
}
|
||||
|
||||
[JsonPropertyName("shortcut")]
|
||||
public HotkeySettings Shortcut
|
||||
{
|
||||
@@ -138,6 +146,7 @@ public sealed class AdvancedPasteCustomAction : Observable, IAdvancedPasteAction
|
||||
Name = other.Name;
|
||||
Description = other.Description;
|
||||
Prompt = other.Prompt;
|
||||
ProviderId = other.ProviderId;
|
||||
Shortcut = other.GetShortcutClone();
|
||||
IsShown = other.IsShown;
|
||||
CanMoveUp = other.CanMoveUp;
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.Library;
|
||||
|
||||
/// <summary>
|
||||
/// Shared default prompts for built-in AI actions. Referenced by both the AdvancedPaste module
|
||||
/// and the Settings UI to ensure consistent defaults and enable "reset to default" functionality.
|
||||
/// </summary>
|
||||
public static class AdvancedPasteDefaultPrompts
|
||||
{
|
||||
public const string FixSpellingAndGrammar = "Fix all spelling and grammar errors in the following text. Return only the corrected text without any additional explanation or commentary.";
|
||||
|
||||
public const string FixSpellingAndGrammarSystem = "You are a professional proofreader. You fix spelling and grammar errors in text. You return only the corrected text with no commentary.";
|
||||
|
||||
public const string FixSpellingAndGrammarCoaching = "Briefly explain what was changed and why in terms of language rules. Be concise as reviewer.";
|
||||
|
||||
public const string FixSpellingAndGrammarCoachingSystem = "You are a writing coach and language teacher. You will be given an original sentence and a corrected version.";
|
||||
}
|
||||
@@ -68,6 +68,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
string[] additionalActionHeaderKeys =
|
||||
[
|
||||
"ImageToText",
|
||||
"FixSpellingAndGrammar",
|
||||
"PasteAsTxtFile",
|
||||
"PasteAsPngFile",
|
||||
"PasteAsHtmlFile",
|
||||
@@ -79,11 +80,25 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
{
|
||||
if (action is AdvancedPasteAdditionalAction additionalAction)
|
||||
{
|
||||
var headerKey = additionalActionHeaderKeys[Math.Min(index, additionalActionHeaderKeys.Length - 1)];
|
||||
hotkeyAccessors.Add(new HotkeyAccessor(
|
||||
() => additionalAction.Shortcut,
|
||||
value => additionalAction.Shortcut = value ?? new HotkeySettings(),
|
||||
additionalActionHeaderKeys[index]));
|
||||
headerKey));
|
||||
index++;
|
||||
|
||||
// The coaching shortcut is registered by the runner as a separate hotkey
|
||||
// immediately after Fix Spelling and Grammar (and only when it's active), so it
|
||||
// must appear in the same position here to keep hotkey IDs aligned with conflicts.
|
||||
if (ReferenceEquals(additionalAction, Properties.AdditionalActions.FixSpellingAndGrammar)
|
||||
&& additionalAction.CoachingEnabled
|
||||
&& additionalAction.CoachingShortcut is { Code: not 0 })
|
||||
{
|
||||
hotkeyAccessors.Add(new HotkeyAccessor(
|
||||
() => additionalAction.CoachingShortcut,
|
||||
value => additionalAction.CoachingShortcut = value ?? new HotkeySettings(),
|
||||
"FixSpellingAndGrammarCoaching"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -116,6 +116,17 @@
|
||||
Header="{x:Bind ModelName, Mode=OneWay}"
|
||||
HeaderIcon="{x:Bind ServiceType, Mode=OneWay, Converter={StaticResource ServiceTypeToIconConverter}}">
|
||||
<StackPanel Orientation="Horizontal" Spacing="8">
|
||||
<Border
|
||||
Padding="6,2"
|
||||
VerticalAlignment="Center"
|
||||
Background="{ThemeResource AccentFillColorDefaultBrush}"
|
||||
CornerRadius="4"
|
||||
Visibility="{x:Bind IsActive, Mode=OneWay}">
|
||||
<TextBlock
|
||||
x:Uid="AdvancedPaste_DefaultBadge"
|
||||
FontSize="12"
|
||||
Foreground="{ThemeResource TextOnAccentFillColorPrimaryBrush}" />
|
||||
</Border>
|
||||
<Button
|
||||
Padding="8"
|
||||
Background="Transparent"
|
||||
@@ -124,6 +135,11 @@
|
||||
<FontIcon FontSize="16" Glyph="" />
|
||||
<Button.Flyout>
|
||||
<MenuFlyout>
|
||||
<MenuFlyoutItem
|
||||
x:Uid="AdvancedPaste_SetAsDefault"
|
||||
Click="SetAsDefaultProviderButton_Click"
|
||||
Icon="{ui:FontIcon Glyph=}"
|
||||
Tag="{x:Bind}" />
|
||||
<MenuFlyoutItem
|
||||
x:Uid="AdvancedPaste_Edit"
|
||||
Click="EditPasteAIProviderButton_Click"
|
||||
@@ -164,7 +180,7 @@
|
||||
</InfoBar.IconSource>
|
||||
</InfoBar>
|
||||
</tkcontrols:SettingsExpander.ItemsHeader>
|
||||
<controls:ShortcutControl HotkeySettings="{x:Bind Path=ViewModel.AdvancedPasteUIShortcut, Mode=TwoWay}" />
|
||||
<controls:ShortcutControl MinWidth="{StaticResource SettingActionControlMinWidth}" HotkeySettings="{x:Bind Path=ViewModel.AdvancedPasteUIShortcut, Mode=TwoWay}" />
|
||||
<tkcontrols:SettingsExpander.Items>
|
||||
<tkcontrols:SettingsCard Name="AdvancedPasteEnableClipboardPreview" ContentAlignment="Left">
|
||||
<ptcontrols:CheckBoxWithDescriptionControl x:Uid="AdvancedPaste_EnableClipboardPreview" IsChecked="{x:Bind ViewModel.EnableClipboardPreview, Mode=TwoWay}" />
|
||||
@@ -195,7 +211,7 @@
|
||||
Name="PasteAsPlainTextShortcut"
|
||||
x:Uid="PasteAsPlainText_Shortcut"
|
||||
HeaderIcon="{ui:FontIcon Glyph=}">
|
||||
<controls:ShortcutControl HotkeySettings="{x:Bind Path=ViewModel.PasteAsPlainTextShortcut, Mode=TwoWay}" />
|
||||
<controls:ShortcutControl MinWidth="{StaticResource SettingActionControlMinWidth}" HotkeySettings="{x:Bind Path=ViewModel.PasteAsPlainTextShortcut, Mode=TwoWay}" />
|
||||
</tkcontrols:SettingsCard>
|
||||
<tkcontrols:SettingsCard
|
||||
Name="PasteAsMarkdownShortcut"
|
||||
@@ -222,6 +238,109 @@
|
||||
HeaderIcon="{ui:FontIcon Glyph=}">
|
||||
<ContentControl ContentTemplate="{StaticResource AdditionalActionTemplate}" />
|
||||
</tkcontrols:SettingsCard>
|
||||
<tkcontrols:SettingsExpander
|
||||
x:Name="FixSpellingAndGrammar"
|
||||
x:Uid="FixSpellingAndGrammar"
|
||||
DataContext="{x:Bind ViewModel.AdditionalActions.FixSpellingAndGrammar, Mode=OneWay}"
|
||||
HeaderIcon="{ui:FontIcon Glyph=}"
|
||||
IsEnabled="{x:Bind ViewModel.IsAIEnabled, Mode=OneWay}"
|
||||
IsExpanded="{Binding IsShown, Mode=OneWay}">
|
||||
<tkcontrols:SettingsExpander.Content>
|
||||
<ContentControl ContentTemplate="{StaticResource AdditionalActionTemplate}" />
|
||||
</tkcontrols:SettingsExpander.Content>
|
||||
<tkcontrols:SettingsExpander.Items>
|
||||
<tkcontrols:SettingsCard Visibility="Collapsed" />
|
||||
<tkcontrols:SettingsCard x:Uid="AdvancedPaste_ActionProvider" IsEnabled="{x:Bind ViewModel.AdditionalActions.FixSpellingAndGrammar.IsShown, Mode=OneWay}">
|
||||
<StackPanel Orientation="Horizontal" Spacing="4">
|
||||
<ComboBox
|
||||
x:Name="FixSpellingProviderComboBox"
|
||||
MinWidth="200"
|
||||
DisplayMemberPath="DisplayName"
|
||||
ItemsSource="{x:Bind ViewModel.PasteAIConfiguration.Providers, Mode=OneWay}"
|
||||
PlaceholderText="{x:Bind GetDefaultProviderLabel()}"
|
||||
SelectedValue="{Binding ProviderId, Mode=TwoWay}"
|
||||
SelectedValuePath="Id" />
|
||||
<Button
|
||||
x:Uid="AdvancedPaste_ClearProviderSelection"
|
||||
VerticalAlignment="Center"
|
||||
Click="ClearProviderSelection_Click"
|
||||
Content="{ui:FontIcon Glyph=,
|
||||
FontSize=12}"
|
||||
Style="{StaticResource SubtleButtonStyle}"
|
||||
Tag="{x:Bind FixSpellingProviderComboBox}" />
|
||||
</StackPanel>
|
||||
</tkcontrols:SettingsCard>
|
||||
<tkcontrols:SettingsCard
|
||||
x:Uid="FixSpellingAndGrammar_CustomPrompt"
|
||||
HorizontalContentAlignment="Stretch"
|
||||
ContentAlignment="Vertical"
|
||||
IsEnabled="{x:Bind ViewModel.AdditionalActions.FixSpellingAndGrammar.IsShown, Mode=OneWay}">
|
||||
<TextBox PlaceholderText="{x:Bind models:AdvancedPasteDefaultPrompts.FixSpellingAndGrammar}" Text="{Binding Prompt, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
|
||||
</tkcontrols:SettingsCard>
|
||||
<tkcontrols:SettingsCard
|
||||
x:Uid="FixSpellingAndGrammar_SystemPrompt"
|
||||
HorizontalContentAlignment="Stretch"
|
||||
ContentAlignment="Vertical"
|
||||
IsEnabled="{x:Bind ViewModel.AdditionalActions.FixSpellingAndGrammar.IsShown, Mode=OneWay}">
|
||||
<TextBox PlaceholderText="{x:Bind models:AdvancedPasteDefaultPrompts.FixSpellingAndGrammarSystem}" Text="{Binding SystemPrompt, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
|
||||
</tkcontrols:SettingsCard>
|
||||
|
||||
<!-- Coaching mode -->
|
||||
<tkcontrols:SettingsCard x:Uid="FixSpellingAndGrammar_CoachingSection" IsEnabled="{x:Bind ViewModel.AdditionalActions.FixSpellingAndGrammar.IsShown, Mode=OneWay}">
|
||||
<StackPanel Orientation="Horizontal" Spacing="4">
|
||||
<controls:ShortcutControl
|
||||
MinWidth="{StaticResource SettingActionControlMinWidth}"
|
||||
AllowDisable="True"
|
||||
HotkeySettings="{x:Bind ViewModel.AdditionalActions.FixSpellingAndGrammar.CoachingShortcut, Mode=TwoWay}" />
|
||||
<ToggleSwitch
|
||||
IsOn="{x:Bind ViewModel.AdditionalActions.FixSpellingAndGrammar.CoachingEnabled, Mode=TwoWay}"
|
||||
OffContent=""
|
||||
OnContent="" />
|
||||
</StackPanel>
|
||||
</tkcontrols:SettingsCard>
|
||||
<tkcontrols:SettingsCard
|
||||
x:Uid="FixSpellingAndGrammar_CoachingProvider"
|
||||
IsEnabled="{x:Bind ViewModel.AdditionalActions.FixSpellingAndGrammar.CoachingEnabled, Mode=OneWay}"
|
||||
Visibility="{x:Bind ViewModel.AdditionalActions.FixSpellingAndGrammar.CoachingEnabled, Mode=OneWay}">
|
||||
<StackPanel Orientation="Horizontal" Spacing="4">
|
||||
<ComboBox
|
||||
x:Name="CoachingProviderComboBox"
|
||||
MinWidth="200"
|
||||
DisplayMemberPath="DisplayName"
|
||||
ItemsSource="{x:Bind ViewModel.PasteAIConfiguration.Providers, Mode=OneWay}"
|
||||
PlaceholderText="{x:Bind GetDefaultProviderLabel()}"
|
||||
SelectedValue="{Binding CoachingProviderId, Mode=TwoWay}"
|
||||
SelectedValuePath="Id" />
|
||||
<Button
|
||||
x:Uid="AdvancedPaste_ClearProviderSelection"
|
||||
VerticalAlignment="Center"
|
||||
Click="ClearProviderSelection_Click"
|
||||
Content="{ui:FontIcon Glyph=,
|
||||
FontSize=12}"
|
||||
Style="{StaticResource SubtleButtonStyle}"
|
||||
Tag="{x:Bind CoachingProviderComboBox}" />
|
||||
</StackPanel>
|
||||
</tkcontrols:SettingsCard>
|
||||
<tkcontrols:SettingsCard
|
||||
x:Uid="FixSpellingAndGrammar_CoachingPrompt"
|
||||
HorizontalContentAlignment="Stretch"
|
||||
ContentAlignment="Vertical"
|
||||
IsEnabled="{x:Bind ViewModel.AdditionalActions.FixSpellingAndGrammar.CoachingEnabled, Mode=OneWay}"
|
||||
Visibility="{x:Bind ViewModel.AdditionalActions.FixSpellingAndGrammar.CoachingEnabled, Mode=OneWay}">
|
||||
<TextBox PlaceholderText="{x:Bind models:AdvancedPasteDefaultPrompts.FixSpellingAndGrammarCoaching}" Text="{Binding CoachingPrompt, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
|
||||
</tkcontrols:SettingsCard>
|
||||
<tkcontrols:SettingsCard
|
||||
x:Uid="FixSpellingAndGrammar_CoachingSystemPrompt"
|
||||
HorizontalContentAlignment="Stretch"
|
||||
ContentAlignment="Vertical"
|
||||
IsEnabled="{x:Bind ViewModel.AdditionalActions.FixSpellingAndGrammar.CoachingEnabled, Mode=OneWay}"
|
||||
Visibility="{x:Bind ViewModel.AdditionalActions.FixSpellingAndGrammar.CoachingEnabled, Mode=OneWay}">
|
||||
<TextBox PlaceholderText="{x:Bind models:AdvancedPasteDefaultPrompts.FixSpellingAndGrammarCoachingSystem}" Text="{Binding CoachingSystemPrompt, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
|
||||
</tkcontrols:SettingsCard>
|
||||
<tkcontrols:SettingsCard Visibility="Collapsed" />
|
||||
</tkcontrols:SettingsExpander.Items>
|
||||
</tkcontrols:SettingsExpander>
|
||||
|
||||
<tkcontrols:SettingsExpander
|
||||
Name="PasteAsFile"
|
||||
x:Uid="PasteAsFile"
|
||||
@@ -306,7 +425,7 @@
|
||||
<!-- Custom actions -->
|
||||
<controls:SettingsGroup x:Uid="AdvancedPaste_Additional_Actions_GroupSettings" IsEnabled="{x:Bind ViewModel.IsEnabled, Mode=OneWay}">
|
||||
<tkcontrols:SettingsExpander
|
||||
Name="AdvancedPasteUIActions"
|
||||
x:Name="AdvancedPasteUIActions"
|
||||
x:Uid="AdvancedPasteUI_Actions"
|
||||
HeaderIcon="{ui:FontIcon Glyph=}"
|
||||
IsEnabled="{x:Bind ViewModel.IsAIEnabled, Mode=OneWay}"
|
||||
@@ -425,6 +544,28 @@
|
||||
AcceptsReturn="true"
|
||||
Text="{Binding Prompt, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
|
||||
TextWrapping="Wrap" />
|
||||
<StackPanel
|
||||
HorizontalAlignment="Left"
|
||||
Orientation="Horizontal"
|
||||
Spacing="4">
|
||||
<ComboBox
|
||||
x:Name="CustomActionProviderComboBox"
|
||||
x:Uid="AdvancedPaste_CustomAction_Provider"
|
||||
Width="306"
|
||||
DisplayMemberPath="DisplayName"
|
||||
ItemsSource="{x:Bind ViewModel.PasteAIConfiguration.Providers, Mode=OneWay}"
|
||||
PlaceholderText="{x:Bind GetDefaultProviderLabel()}"
|
||||
SelectedValue="{Binding ProviderId, Mode=TwoWay}"
|
||||
SelectedValuePath="Id" />
|
||||
<Button
|
||||
x:Uid="AdvancedPaste_ClearProviderSelection"
|
||||
VerticalAlignment="Bottom"
|
||||
Click="ClearProviderSelection_Click"
|
||||
Content="{ui:FontIcon Glyph=,
|
||||
FontSize=12}"
|
||||
Style="{StaticResource SubtleButtonStyle}"
|
||||
Tag="{x:Bind CustomActionProviderComboBox}" />
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
</ContentDialog>
|
||||
|
||||
@@ -542,6 +683,92 @@
|
||||
<controls:FoundryLocalModelPicker x:Name="FoundryLocalPicker" />
|
||||
</Grid>
|
||||
|
||||
<Grid
|
||||
x:Name="PhiSilicaPanel"
|
||||
Margin="0,8,0,0"
|
||||
Visibility="Collapsed">
|
||||
<StackPanel
|
||||
x:Name="PhiSilicaLoadingPanel"
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
Spacing="12">
|
||||
<ProgressRing
|
||||
Width="36"
|
||||
Height="36"
|
||||
HorizontalAlignment="Center" />
|
||||
<TextBlock
|
||||
x:Name="PhiSilicaLoadingText"
|
||||
x:Uid="AdvancedPaste_PhiSilicaLoadingStatus"
|
||||
HorizontalAlignment="Center"
|
||||
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
|
||||
TextAlignment="Center"
|
||||
TextWrapping="Wrap" />
|
||||
</StackPanel>
|
||||
<StackPanel
|
||||
x:Name="PhiSilicaAvailablePanel"
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
Spacing="8"
|
||||
Visibility="Collapsed">
|
||||
<FontIcon
|
||||
HorizontalAlignment="Center"
|
||||
FontFamily="{StaticResource SymbolThemeFontFamily}"
|
||||
FontSize="24"
|
||||
Foreground="{ThemeResource SystemFillColorSuccessBrush}"
|
||||
Glyph="" />
|
||||
<TextBlock
|
||||
x:Name="PhiSilicaAvailableText"
|
||||
HorizontalAlignment="Center"
|
||||
FontWeight="SemiBold"
|
||||
TextAlignment="Center"
|
||||
TextWrapping="Wrap" />
|
||||
</StackPanel>
|
||||
<StackPanel
|
||||
x:Name="PhiSilicaNotAvailablePanel"
|
||||
Margin="48,0,48,48"
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
Spacing="8"
|
||||
Visibility="Collapsed">
|
||||
<Image Width="36" Source="ms-appx:///Assets/Settings/Icons/Models/WindowsML.svg" />
|
||||
<TextBlock
|
||||
x:Name="PhiSilicaNotAvailableTitle"
|
||||
HorizontalAlignment="Center"
|
||||
FontWeight="SemiBold"
|
||||
TextAlignment="Center"
|
||||
TextWrapping="Wrap" />
|
||||
<TextBlock
|
||||
x:Name="PhiSilicaNotAvailableDescription"
|
||||
HorizontalAlignment="Center"
|
||||
FontSize="12"
|
||||
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
|
||||
TextAlignment="Center"
|
||||
TextWrapping="Wrap" />
|
||||
<TextBlock
|
||||
x:Name="PhiSilicaNotAvailableDetails"
|
||||
HorizontalAlignment="Center"
|
||||
FontFamily="Consolas"
|
||||
FontSize="11"
|
||||
Foreground="{ThemeResource TextFillColorTertiaryBrush}"
|
||||
IsTextSelectionEnabled="True"
|
||||
TextAlignment="Center"
|
||||
TextWrapping="Wrap"
|
||||
Visibility="Collapsed" />
|
||||
<Button
|
||||
x:Name="PhiSilicaPrepareButton"
|
||||
x:Uid="AdvancedPaste_PhiSilicaPrepareButton"
|
||||
Margin="0,4,0,0"
|
||||
HorizontalAlignment="Center"
|
||||
Click="PhiSilicaPrepareButton_Click"
|
||||
Style="{StaticResource AccentButtonStyle}"
|
||||
Visibility="Collapsed" />
|
||||
<HyperlinkButton
|
||||
x:Uid="AdvancedPaste_PhiSilicaCopilotLearnMore"
|
||||
HorizontalAlignment="Center"
|
||||
NavigateUri="https://learn.microsoft.com/windows/ai/npu-devices/" />
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
|
||||
<StackPanel
|
||||
x:Name="PasteAIModelPanel"
|
||||
Orientation="Horizontal"
|
||||
|
||||
@@ -6,6 +6,8 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
@@ -30,6 +32,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
private CancellationTokenSource _foundryModelLoadCts;
|
||||
private bool _suppressFoundrySelectionChanged;
|
||||
private bool _isFoundryLocalAvailable;
|
||||
private bool _isPhiSilicaAvailable;
|
||||
private bool _disposed;
|
||||
private const string PasteAiDialogDefaultTitle = "Paste with AI provider configuration";
|
||||
|
||||
@@ -37,6 +40,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
private const string SimpleAISystemPrompt = "You are tasked with reformatting user's clipboard data. Use the user's instructions, and the content of their clipboard below to edit their clipboard content as they have requested it. Do not output anything else besides the reformatted clipboard content.";
|
||||
private static readonly string AdvancedAISystemPromptNormalized = AdvancedAISystemPrompt.Trim();
|
||||
private static readonly string SimpleAISystemPromptNormalized = SimpleAISystemPrompt.Trim();
|
||||
private static readonly char[] NewLineSeparators = ['\r', '\n'];
|
||||
|
||||
private AdvancedPasteViewModel ViewModel { get; set; }
|
||||
|
||||
@@ -65,6 +69,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
ViewModel.OnPageLoaded();
|
||||
UpdatePasteAIUIVisibility();
|
||||
await UpdateFoundryLocalUIAsync();
|
||||
await UpdatePhiSilicaUIAsync();
|
||||
};
|
||||
|
||||
Unloaded += (_, _) =>
|
||||
@@ -83,6 +88,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
ViewModel.RefreshEnabledState();
|
||||
UpdatePasteAIUIVisibility();
|
||||
_ = UpdateFoundryLocalUIAsync();
|
||||
_ = UpdatePhiSilicaUIAsync();
|
||||
}
|
||||
|
||||
private void EnableAdvancedPasteAI() => ViewModel.EnableAI();
|
||||
@@ -103,6 +109,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
else
|
||||
{
|
||||
ViewModel.DisableAI();
|
||||
FixSpellingAndGrammar.IsExpanded = false;
|
||||
AdvancedPasteUIActions.IsExpanded = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,7 +327,9 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
bool requiresApiVersion = serviceKind == AIServiceType.AzureOpenAI;
|
||||
bool requiresModelPath = serviceKind == AIServiceType.Onnx;
|
||||
bool isFoundryLocal = serviceKind == AIServiceType.FoundryLocal;
|
||||
bool isPhiSilica = serviceKind == AIServiceType.PhiSilica;
|
||||
bool requiresApiKey = RequiresApiKeyForService(selectedType);
|
||||
bool requiresModelName = !isFoundryLocal && !isPhiSilica;
|
||||
bool showModerationToggle = serviceKind == AIServiceType.OpenAI;
|
||||
bool showAdvancedAI = serviceKind == AIServiceType.OpenAI || serviceKind == AIServiceType.AzureOpenAI;
|
||||
|
||||
@@ -344,7 +354,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
PasteAIModerationToggle.Visibility = showModerationToggle ? Visibility.Visible : Visibility.Collapsed;
|
||||
PasteAIEnableAdvancedAICheckBox.Visibility = showAdvancedAI ? Visibility.Visible : Visibility.Collapsed;
|
||||
PasteAIApiKeyPasswordBox.Visibility = requiresApiKey ? Visibility.Visible : Visibility.Collapsed;
|
||||
PasteAIModelNameTextBox.Visibility = isFoundryLocal ? Visibility.Collapsed : Visibility.Visible;
|
||||
PasteAIModelNameTextBox.Visibility = requiresModelName ? Visibility.Visible : Visibility.Collapsed;
|
||||
|
||||
if (requiresApiKey)
|
||||
{
|
||||
@@ -373,6 +383,11 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
// For Foundry Local, UpdateFoundrySaveButtonState will handle button state
|
||||
// based on model selection status
|
||||
}
|
||||
else if (isPhiSilica)
|
||||
{
|
||||
// For Phi Silica, UpdatePhiSilicaUIAsync will handle button state
|
||||
// based on device availability
|
||||
}
|
||||
else
|
||||
{
|
||||
// GPO allows this provider, enable save button
|
||||
@@ -421,6 +436,360 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task UpdatePhiSilicaUIAsync()
|
||||
{
|
||||
string selectedType = ViewModel?.PasteAIProviderDraft?.ServiceType ?? string.Empty;
|
||||
bool isPhiSilica = string.Equals(selectedType, "PhiSilica", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
if (PhiSilicaPanel is not null)
|
||||
{
|
||||
PhiSilicaPanel.Visibility = isPhiSilica ? Visibility.Visible : Visibility.Collapsed;
|
||||
}
|
||||
|
||||
if (!isPhiSilica)
|
||||
{
|
||||
_isPhiSilicaAvailable = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (PasteAIProviderConfigurationDialog is not null)
|
||||
{
|
||||
PasteAIProviderConfigurationDialog.IsPrimaryButtonEnabled = false;
|
||||
}
|
||||
|
||||
ShowPhiSilicaLoadingState();
|
||||
var resourceLoader = ResourceLoaderInstance.ResourceLoader;
|
||||
|
||||
try
|
||||
{
|
||||
// Settings doesn't have package identity, so it can't call
|
||||
// LanguageModel.GetReadyState() directly. Instead, probe via AdvancedPaste
|
||||
// which runs with its own package identity. See microsoft-ui-xaml#10856.
|
||||
var (status, diagnostics) = await Task.Run(() => CheckPhiSilicaViaAdvancedPaste());
|
||||
|
||||
if (status == "NotSupported")
|
||||
{
|
||||
_isPhiSilicaAvailable = false;
|
||||
ShowPhiSilicaNotAvailableState(
|
||||
resourceLoader.GetString("AdvancedPaste_PhiSilicaNotAvailable_Title"),
|
||||
resourceLoader.GetString("AdvancedPaste_PhiSilicaNotAvailable_Description"),
|
||||
details: diagnostics);
|
||||
}
|
||||
else if (status == "NotReady")
|
||||
{
|
||||
_isPhiSilicaAvailable = false;
|
||||
ShowPhiSilicaNotAvailableState(
|
||||
resourceLoader.GetString("AdvancedPaste_PhiSilicaNotReady_Title"),
|
||||
resourceLoader.GetString("AdvancedPaste_PhiSilicaNotReady_Description"),
|
||||
showPrepareButton: true,
|
||||
details: diagnostics);
|
||||
}
|
||||
else
|
||||
{
|
||||
_isPhiSilicaAvailable = true;
|
||||
ShowPhiSilicaAvailableState(resourceLoader.GetString("AdvancedPaste_PhiSilicaAvailable_Message"));
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
_isPhiSilicaAvailable = false;
|
||||
ShowPhiSilicaNotAvailableState(
|
||||
resourceLoader.GetString("AdvancedPaste_PhiSilicaNotAvailable_Title"),
|
||||
resourceLoader.GetString("AdvancedPaste_PhiSilicaCheckFailed_Description"));
|
||||
}
|
||||
|
||||
if (PasteAIProviderConfigurationDialog is not null)
|
||||
{
|
||||
PasteAIProviderConfigurationDialog.IsPrimaryButtonEnabled = _isPhiSilicaAvailable;
|
||||
}
|
||||
}
|
||||
|
||||
private void ShowPhiSilicaLoadingState(string message = null)
|
||||
{
|
||||
if (PhiSilicaLoadingText is not null && !string.IsNullOrEmpty(message))
|
||||
{
|
||||
PhiSilicaLoadingText.Text = message;
|
||||
}
|
||||
|
||||
if (PhiSilicaLoadingPanel is not null)
|
||||
{
|
||||
PhiSilicaLoadingPanel.Visibility = Visibility.Visible;
|
||||
}
|
||||
|
||||
if (PhiSilicaAvailablePanel is not null)
|
||||
{
|
||||
PhiSilicaAvailablePanel.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
|
||||
if (PhiSilicaNotAvailablePanel is not null)
|
||||
{
|
||||
PhiSilicaNotAvailablePanel.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
}
|
||||
|
||||
private void ShowPhiSilicaAvailableState(string message)
|
||||
{
|
||||
if (PhiSilicaLoadingPanel is not null)
|
||||
{
|
||||
PhiSilicaLoadingPanel.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
|
||||
if (PhiSilicaAvailablePanel is not null)
|
||||
{
|
||||
PhiSilicaAvailablePanel.Visibility = Visibility.Visible;
|
||||
}
|
||||
|
||||
if (PhiSilicaAvailableText is not null)
|
||||
{
|
||||
PhiSilicaAvailableText.Text = message;
|
||||
}
|
||||
|
||||
if (PhiSilicaNotAvailablePanel is not null)
|
||||
{
|
||||
PhiSilicaNotAvailablePanel.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
}
|
||||
|
||||
private void ShowPhiSilicaNotAvailableState(string title, string description, bool showPrepareButton = false, string details = null)
|
||||
{
|
||||
if (PhiSilicaLoadingPanel is not null)
|
||||
{
|
||||
PhiSilicaLoadingPanel.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
|
||||
if (PhiSilicaAvailablePanel is not null)
|
||||
{
|
||||
PhiSilicaAvailablePanel.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
|
||||
if (PhiSilicaNotAvailablePanel is not null)
|
||||
{
|
||||
PhiSilicaNotAvailablePanel.Visibility = Visibility.Visible;
|
||||
}
|
||||
|
||||
if (PhiSilicaNotAvailableTitle is not null)
|
||||
{
|
||||
PhiSilicaNotAvailableTitle.Text = title;
|
||||
}
|
||||
|
||||
if (PhiSilicaNotAvailableDescription is not null)
|
||||
{
|
||||
PhiSilicaNotAvailableDescription.Text = description;
|
||||
}
|
||||
|
||||
if (PhiSilicaPrepareButton is not null)
|
||||
{
|
||||
PhiSilicaPrepareButton.Visibility = showPrepareButton ? Visibility.Visible : Visibility.Collapsed;
|
||||
}
|
||||
|
||||
// Surface the AdvancedPaste diagnostics (LAF status, ready state, HRESULT) so the user
|
||||
// can see why the model isn't ready / why a download attempt failed, instead of nothing.
|
||||
if (PhiSilicaNotAvailableDetails is not null)
|
||||
{
|
||||
PhiSilicaNotAvailableDetails.Text = details ?? string.Empty;
|
||||
PhiSilicaNotAvailableDetails.Visibility = string.IsNullOrWhiteSpace(details) ? Visibility.Collapsed : Visibility.Visible;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks Phi Silica availability by launching AdvancedPaste.exe with --check-phi-silica.
|
||||
/// AdvancedPaste has sparse package identity and can call the Windows AI APIs directly.
|
||||
/// Returns the status ("Available", "NotReady", or "NotSupported") plus any diagnostic lines
|
||||
/// AdvancedPaste wrote to stderr (LAF unlock status, ready state).
|
||||
/// </summary>
|
||||
private static (string Status, string Diagnostics) CheckPhiSilicaViaAdvancedPaste()
|
||||
{
|
||||
var settingsDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
|
||||
|
||||
// PowerToys.AdvancedPaste.exe ships in the same WinUI3Apps folder as PowerToys.Settings.exe
|
||||
// (see installer harvest and .vscode/launch.json), not in an "AdvancedPaste" subfolder.
|
||||
var advancedPastePath = Path.Combine(settingsDir, "PowerToys.AdvancedPaste.exe");
|
||||
|
||||
if (!File.Exists(advancedPastePath))
|
||||
{
|
||||
return ("NotSupported", string.Empty);
|
||||
}
|
||||
|
||||
var startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = advancedPastePath,
|
||||
Arguments = "--check-phi-silica",
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
};
|
||||
|
||||
using var process = Process.Start(startInfo);
|
||||
if (process == null)
|
||||
{
|
||||
return ("NotSupported", string.Empty);
|
||||
}
|
||||
|
||||
// Read stdout/stderr asynchronously so a stalled child can't block us before the timeout elapses.
|
||||
var outputTask = process.StandardOutput.ReadToEndAsync();
|
||||
var errorTask = process.StandardError.ReadToEndAsync();
|
||||
|
||||
if (!process.WaitForExit(10_000))
|
||||
{
|
||||
TryKillProcess(process);
|
||||
return ("NotSupported", string.Empty);
|
||||
}
|
||||
|
||||
var output = outputTask.GetAwaiter().GetResult().Trim();
|
||||
var diagnostics = ExtractPhiSilicaDiagnostics(errorTask.GetAwaiter().GetResult());
|
||||
|
||||
var status = output switch
|
||||
{
|
||||
"Available" => "Available",
|
||||
"NotReady" => "NotReady",
|
||||
_ => "NotSupported",
|
||||
};
|
||||
|
||||
return (status, diagnostics);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Triggers Phi Silica model preparation (download) by launching AdvancedPaste.exe with
|
||||
/// --prepare-phi-silica. AdvancedPaste has sparse package identity and can call EnsureReadyAsync.
|
||||
/// Returns the status ("Ready", "Failed", or "NotSupported") plus any diagnostic lines
|
||||
/// AdvancedPaste wrote to stderr (ready state and, on failure, the EnsureReadyAsync HRESULT).
|
||||
/// </summary>
|
||||
private static (string Status, string Diagnostics) PreparePhiSilicaViaAdvancedPaste()
|
||||
{
|
||||
var settingsDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
|
||||
var advancedPastePath = Path.Combine(settingsDir, "PowerToys.AdvancedPaste.exe");
|
||||
|
||||
if (!File.Exists(advancedPastePath))
|
||||
{
|
||||
return ("NotSupported", string.Empty);
|
||||
}
|
||||
|
||||
var startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = advancedPastePath,
|
||||
Arguments = "--prepare-phi-silica",
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
};
|
||||
|
||||
using var process = Process.Start(startInfo);
|
||||
if (process == null)
|
||||
{
|
||||
return ("NotSupported", string.Empty);
|
||||
}
|
||||
|
||||
// Read stdout/stderr asynchronously; model download can take a while, but a stalled child
|
||||
// must not block us indefinitely, so cap the wait and kill the process on timeout.
|
||||
var outputTask = process.StandardOutput.ReadToEndAsync();
|
||||
var errorTask = process.StandardError.ReadToEndAsync();
|
||||
|
||||
if (!process.WaitForExit(600_000))
|
||||
{
|
||||
TryKillProcess(process);
|
||||
return ("Failed", string.Empty);
|
||||
}
|
||||
|
||||
var output = outputTask.GetAwaiter().GetResult().Trim();
|
||||
var diagnostics = ExtractPhiSilicaDiagnostics(errorTask.GetAwaiter().GetResult());
|
||||
|
||||
var status = output switch
|
||||
{
|
||||
"Ready" => "Ready",
|
||||
"Failed" => "Failed",
|
||||
_ => "NotSupported",
|
||||
};
|
||||
|
||||
return (status, diagnostics);
|
||||
}
|
||||
|
||||
private static void TryKillProcess(Process process)
|
||||
{
|
||||
try
|
||||
{
|
||||
process.Kill(entireProcessTree: true);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
// AdvancedPaste writes structured Phi Silica diagnostics to stderr, one per line prefixed with
|
||||
// "[phi-silica] " (LAF unlock status, ready state, and on failure the EnsureReadyAsync HRESULT
|
||||
// and message). Pull those lines out so the configurator can show the real error/HRESULT.
|
||||
private static string ExtractPhiSilicaDiagnostics(string standardError)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(standardError))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
const string prefix = "[phi-silica] ";
|
||||
var lines = standardError
|
||||
.Split(NewLineSeparators, StringSplitOptions.RemoveEmptyEntries)
|
||||
.Select(line => line.Trim())
|
||||
.Where(line => line.StartsWith(prefix, StringComparison.Ordinal))
|
||||
.Select(line => line.Substring(prefix.Length))
|
||||
.Distinct()
|
||||
.ToArray();
|
||||
|
||||
return string.Join(Environment.NewLine, lines);
|
||||
}
|
||||
|
||||
private async void PhiSilicaPrepareButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var resourceLoader = ResourceLoaderInstance.ResourceLoader;
|
||||
|
||||
ShowPhiSilicaLoadingState(resourceLoader.GetString("AdvancedPaste_PhiSilicaPreparing_Status"));
|
||||
|
||||
if (PasteAIProviderConfigurationDialog is not null)
|
||||
{
|
||||
PasteAIProviderConfigurationDialog.IsPrimaryButtonEnabled = false;
|
||||
}
|
||||
|
||||
(string Status, string Diagnostics) prepareResult;
|
||||
try
|
||||
{
|
||||
prepareResult = await Task.Run(() => PreparePhiSilicaViaAdvancedPaste());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
prepareResult = ("Failed", ex.Message);
|
||||
}
|
||||
|
||||
if (prepareResult.Status == "Ready")
|
||||
{
|
||||
// Model is now ready; re-probe so the UI flips to the available state and Save enables.
|
||||
await UpdatePhiSilicaUIAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
_isPhiSilicaAvailable = false;
|
||||
|
||||
if (prepareResult.Status == "NotSupported")
|
||||
{
|
||||
ShowPhiSilicaNotAvailableState(
|
||||
resourceLoader.GetString("AdvancedPaste_PhiSilicaNotAvailable_Title"),
|
||||
resourceLoader.GetString("AdvancedPaste_PhiSilicaNotAvailable_Description"),
|
||||
details: prepareResult.Diagnostics);
|
||||
}
|
||||
else
|
||||
{
|
||||
ShowPhiSilicaNotAvailableState(
|
||||
resourceLoader.GetString("AdvancedPaste_PhiSilicaNotReady_Title"),
|
||||
resourceLoader.GetString("AdvancedPaste_PhiSilicaPrepareFailed_Description"),
|
||||
showPrepareButton: true,
|
||||
details: prepareResult.Diagnostics);
|
||||
}
|
||||
|
||||
if (PasteAIProviderConfigurationDialog is not null)
|
||||
{
|
||||
PasteAIProviderConfigurationDialog.IsPrimaryButtonEnabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task LoadFoundryLocalModelsAsync()
|
||||
{
|
||||
if (FoundryLocalPanel is null)
|
||||
@@ -835,6 +1204,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
AIServiceType.Onnx => false,
|
||||
AIServiceType.Ollama => false,
|
||||
AIServiceType.FoundryLocal => false,
|
||||
AIServiceType.PhiSilica => false,
|
||||
AIServiceType.ML => false,
|
||||
_ => true,
|
||||
};
|
||||
@@ -1112,6 +1482,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
}
|
||||
|
||||
await UpdateFoundryLocalUIAsync();
|
||||
await UpdatePhiSilicaUIAsync();
|
||||
UpdatePasteAIUIVisibility();
|
||||
RefreshDialogBindings();
|
||||
|
||||
@@ -1141,6 +1512,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
|
||||
UpdatePasteAIUIVisibility();
|
||||
await UpdateFoundryLocalUIAsync();
|
||||
await UpdatePhiSilicaUIAsync();
|
||||
RefreshDialogBindings();
|
||||
PasteAIApiKeyPasswordBox.Password = ViewModel.GetPasteAIApiKey(provider.Id, provider.ServiceType);
|
||||
await PasteAIProviderConfigurationDialog.ShowAsync();
|
||||
@@ -1157,11 +1529,41 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
ViewModel?.RemovePasteAIProvider(provider);
|
||||
}
|
||||
|
||||
private void SetAsDefaultProviderButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (sender is not MenuFlyoutItem menuItem || menuItem.Tag is not PasteAIProviderDefinition provider)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ViewModel?.SetAsDefaultProvider(provider);
|
||||
}
|
||||
|
||||
private void PasteAIProviderConfigurationDialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args)
|
||||
{
|
||||
ViewModel?.CancelPasteAIProviderDraft();
|
||||
PasteAIProviderConfigurationDialog.Title = PasteAiDialogDefaultTitle;
|
||||
PasteAIApiKeyPasswordBox.Password = string.Empty;
|
||||
}
|
||||
|
||||
private void ClearProviderSelection_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (sender is Button button && button.Tag is ComboBox comboBox)
|
||||
{
|
||||
comboBox.SelectedIndex = -1;
|
||||
}
|
||||
}
|
||||
|
||||
private string GetDefaultProviderLabel()
|
||||
{
|
||||
try
|
||||
{
|
||||
return Microsoft.PowerToys.Settings.UI.Helpers.ResourceLoaderInstance.ResourceLoader.GetString("AdvancedPaste_ActionProvider_Default");
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "Default (use active provider)";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1992,6 +1992,86 @@ Made with 💗 by Microsoft and the PowerToys community.</value>
|
||||
<data name="ImageToText.Header" xml:space="preserve">
|
||||
<value>Image to text</value>
|
||||
</data>
|
||||
<data name="FixSpellingAndGrammar.Header" xml:space="preserve">
|
||||
<value>Fix spelling and grammar</value>
|
||||
</data>
|
||||
<data name="FixSpellingAndGrammar_CustomPrompt.Header" xml:space="preserve">
|
||||
<value>Custom prompt</value>
|
||||
</data>
|
||||
<data name="FixSpellingAndGrammar_CustomPrompt.Description" xml:space="preserve">
|
||||
<value>Override the default AI prompt used for fixing spelling and grammar. Leave empty to use the default prompt.</value>
|
||||
</data>
|
||||
<data name="FixSpellingAndGrammar_SystemPrompt.Header" xml:space="preserve">
|
||||
<value>System prompt</value>
|
||||
<comment>Header for the system prompt customization for fix spelling action</comment>
|
||||
</data>
|
||||
<data name="FixSpellingAndGrammar_SystemPrompt.Description" xml:space="preserve">
|
||||
<value>Override the system prompt that sets the AI's role for fixing spelling and grammar. Leave empty to use the default.</value>
|
||||
<comment>Description for the system prompt customization</comment>
|
||||
</data>
|
||||
<data name="AdvancedPaste_ActionProvider.Header" xml:space="preserve">
|
||||
<value>AI model</value>
|
||||
<comment>Label for selecting which AI model provider to use for this action</comment>
|
||||
</data>
|
||||
<data name="AdvancedPaste_ActionProvider.Description" xml:space="preserve">
|
||||
<value>Select the AI model to use for this action. Leave as default to use the globally active provider.</value>
|
||||
</data>
|
||||
<data name="AdvancedPaste_ActionProvider_Default" xml:space="preserve">
|
||||
<value>Default (use active provider)</value>
|
||||
<comment>Option in provider picker that means use the globally configured provider</comment>
|
||||
</data>
|
||||
<data name="AdvancedPaste_CustomAction_Provider.Header" xml:space="preserve">
|
||||
<value>AI model</value>
|
||||
<comment>Label for selecting the AI model provider in the custom action dialog</comment>
|
||||
</data>
|
||||
<data name="AdvancedPaste_ClearProviderSelection.[using:Microsoft.UI.Xaml.Controls]ToolTipService.ToolTip" xml:space="preserve">
|
||||
<value>Reset to default provider</value>
|
||||
<comment>Tooltip for button that clears the provider selection back to default</comment>
|
||||
</data>
|
||||
<data name="AdvancedPaste_ClearProviderSelection.AutomationProperties.Name" xml:space="preserve">
|
||||
<value>Reset to default provider</value>
|
||||
<comment>Accessible name for button that clears the provider selection back to default</comment>
|
||||
</data>
|
||||
<data name="FixSpellingAndGrammar_CoachingSection.Header" xml:space="preserve">
|
||||
<value>Coaching mode</value>
|
||||
<comment>Header for the coaching mode collapsible settings section</comment>
|
||||
</data>
|
||||
<data name="FixSpellingAndGrammar_CoachingSection.Description" xml:space="preserve">
|
||||
<value>When enabled, shows a preview with an explanation of what was fixed and why.</value>
|
||||
<comment>Description for the coaching mode settings section</comment>
|
||||
</data>
|
||||
<data name="FixSpellingAndGrammar_CoachingShortcut.Header" xml:space="preserve">
|
||||
<value>Coaching shortcut</value>
|
||||
<comment>Header for the coaching mode keyboard shortcut</comment>
|
||||
</data>
|
||||
<data name="FixSpellingAndGrammar_CoachingShortcut.Description" xml:space="preserve">
|
||||
<value>Optional shortcut that triggers fix spelling with coaching enabled. If not set, uses the main action shortcut.</value>
|
||||
<comment>Description for the coaching shortcut setting</comment>
|
||||
</data>
|
||||
<data name="FixSpellingAndGrammar_CoachingPrompt.Header" xml:space="preserve">
|
||||
<value>Coaching prompt</value>
|
||||
<comment>Header for the coaching prompt customization text box</comment>
|
||||
</data>
|
||||
<data name="FixSpellingAndGrammar_CoachingProvider.Header" xml:space="preserve">
|
||||
<value>Coaching AI model</value>
|
||||
<comment>Label for selecting which AI model provider to use for coaching explanations</comment>
|
||||
</data>
|
||||
<data name="FixSpellingAndGrammar_CoachingProvider.Description" xml:space="preserve">
|
||||
<value>Select the AI model to use for coaching explanations. Leave as default to use the same model as Fix Spelling.</value>
|
||||
<comment>Description for the coaching provider selector</comment>
|
||||
</data>
|
||||
<data name="FixSpellingAndGrammar_CoachingPrompt.Description" xml:space="preserve">
|
||||
<value>Override the AI prompt used when generating the coaching explanation. Leave empty to use the default prompt.</value>
|
||||
<comment>Description for the coaching prompt customization</comment>
|
||||
</data>
|
||||
<data name="FixSpellingAndGrammar_CoachingSystemPrompt.Header" xml:space="preserve">
|
||||
<value>Coaching system prompt</value>
|
||||
<comment>Header for the coaching system prompt customization text box</comment>
|
||||
</data>
|
||||
<data name="FixSpellingAndGrammar_CoachingSystemPrompt.Description" xml:space="preserve">
|
||||
<value>Override the system prompt that sets the AI's role for coaching explanations. Leave empty to use the default.</value>
|
||||
<comment>Description for the coaching system prompt customization</comment>
|
||||
</data>
|
||||
<data name="PasteAsFile.Header" xml:space="preserve">
|
||||
<value>Paste as file</value>
|
||||
</data>
|
||||
@@ -4391,11 +4471,11 @@ Activate by holding the key for the character you want to add an accent to, then
|
||||
<comment>Enables display of clipboard contents preview in the Advanced Paste window</comment>
|
||||
</data>
|
||||
<data name="AdvancedPaste_AutoCopySelectionForCustomActionHotkey.Header" xml:space="preserve">
|
||||
<value>Auto-copy selection for custom action hotkeys</value>
|
||||
<value>Use selected text for all hotkeys</value>
|
||||
<comment>Advanced Paste is a product name</comment>
|
||||
</data>
|
||||
<data name="AdvancedPaste_AutoCopySelectionForCustomActionHotkey.Description" xml:space="preserve">
|
||||
<value>Attempts to copy the current selection before running a custom action shortcut</value>
|
||||
<value>Attempts to copy the current text selection before running any Advanced Paste shortcut. Falls back to clipboard contents if nothing is selected.</value>
|
||||
<comment>Advanced Paste is a product name</comment>
|
||||
</data>
|
||||
<data name="GPO_CommandNotFound_ForceDisabled.Title" xml:space="preserve">
|
||||
@@ -5963,6 +6043,14 @@ The break timer font matches the text font.</value>
|
||||
<data name="AdvancedPaste_Edit.Text" xml:space="preserve">
|
||||
<value>Edit</value>
|
||||
</data>
|
||||
<data name="AdvancedPaste_SetAsDefault.Text" xml:space="preserve">
|
||||
<value>Set as default</value>
|
||||
<comment>Menu item to mark a provider as the default/active one</comment>
|
||||
</data>
|
||||
<data name="AdvancedPaste_DefaultBadge.Text" xml:space="preserve">
|
||||
<value>Default</value>
|
||||
<comment>Badge label shown on the provider that is currently the default/active one</comment>
|
||||
</data>
|
||||
<data name="AdvancedPaste_Remove.Text" xml:space="preserve">
|
||||
<value>Remove</value>
|
||||
</data>
|
||||
@@ -6003,6 +6091,46 @@ The break timer font matches the text font.</value>
|
||||
<value>Foundry Local is still in public preview</value>
|
||||
<comment>Do not loc "Foundry Local"</comment>
|
||||
</data>
|
||||
<data name="AdvancedPaste_PhiSilicaLoadingStatus.Text" xml:space="preserve">
|
||||
<value>Checking Phi Silica availability...</value>
|
||||
<comment>Do not localize "Phi Silica", it's a model name</comment>
|
||||
</data>
|
||||
<data name="AdvancedPaste_PhiSilicaCopilotLearnMore.Content" xml:space="preserve">
|
||||
<value>Learn more about Copilot+ PCs</value>
|
||||
<comment>Do not localize "Copilot+ PCs", it's a product name</comment>
|
||||
</data>
|
||||
<data name="AdvancedPaste_PhiSilicaNotAvailable_Title" xml:space="preserve">
|
||||
<value>Phi Silica is not available on this device.</value>
|
||||
<comment>Do not localize "Phi Silica", it's a model name</comment>
|
||||
</data>
|
||||
<data name="AdvancedPaste_PhiSilicaNotAvailable_Description" xml:space="preserve">
|
||||
<value>A Copilot+ PC with an NPU is required to use Phi Silica. For on-device AI on any Windows PC, consider using Foundry Local.</value>
|
||||
<comment>Do not localize "Copilot+ PC", "NPU", "Phi Silica", and "Foundry Local", they are product/technical names</comment>
|
||||
</data>
|
||||
<data name="AdvancedPaste_PhiSilicaNotReady_Title" xml:space="preserve">
|
||||
<value>Phi Silica model is not ready.</value>
|
||||
<comment>Do not localize "Phi Silica", it's a model name</comment>
|
||||
</data>
|
||||
<data name="AdvancedPaste_PhiSilicaNotReady_Description" xml:space="preserve">
|
||||
<value>The model needs to be downloaded before it can be used. Select "Download model" to start, or check Windows Update for progress.</value>
|
||||
</data>
|
||||
<data name="AdvancedPaste_PhiSilicaPrepareButton.Content" xml:space="preserve">
|
||||
<value>Download model</value>
|
||||
</data>
|
||||
<data name="AdvancedPaste_PhiSilicaPreparing_Status" xml:space="preserve">
|
||||
<value>Downloading and preparing the model. This may take several minutes...</value>
|
||||
</data>
|
||||
<data name="AdvancedPaste_PhiSilicaPrepareFailed_Description" xml:space="preserve">
|
||||
<value>Couldn't prepare the model. Check Windows Update for the AI model download, then try again.</value>
|
||||
</data>
|
||||
<data name="AdvancedPaste_PhiSilicaAvailable_Message" xml:space="preserve">
|
||||
<value>Phi Silica is available and ready on this device.</value>
|
||||
<comment>Do not localize "Phi Silica", it's a model name</comment>
|
||||
</data>
|
||||
<data name="AdvancedPaste_PhiSilicaCheckFailed_Description" xml:space="preserve">
|
||||
<value>Unable to check Phi Silica availability. A Copilot+ PC with an NPU is required.</value>
|
||||
<comment>Do not localize "Phi Silica", "Copilot+ PC", and "NPU", they are product/technical names</comment>
|
||||
</data>
|
||||
<data name="CmdPal_Settings.Description" xml:space="preserve">
|
||||
<value>Configure the activation shortcut, extensions, behavior and much more</value>
|
||||
</data>
|
||||
|
||||
@@ -139,6 +139,15 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
||||
if (action is AdvancedPasteAdditionalAction additionalAction)
|
||||
{
|
||||
hotkeySettings.Add(additionalAction.Shortcut);
|
||||
|
||||
// Mirror the runner's hotkey order: the coaching shortcut is registered as a
|
||||
// separate hotkey immediately after Fix Spelling and Grammar when it's active.
|
||||
if (ReferenceEquals(additionalAction, _additionalActions.FixSpellingAndGrammar)
|
||||
&& additionalAction.CoachingEnabled
|
||||
&& additionalAction.CoachingShortcut is { Code: not 0 })
|
||||
{
|
||||
hotkeySettings.Add(additionalAction.CoachingShortcut);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -492,6 +501,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
||||
|
||||
var newValue = value ?? new PasteAIConfiguration();
|
||||
_advancedPasteSettings.Properties.PasteAIConfiguration = newValue;
|
||||
SyncProviderActiveFlags(newValue);
|
||||
SubscribeToPasteAIConfiguration(newValue);
|
||||
|
||||
OnPropertyChanged(nameof(PasteAIConfiguration));
|
||||
@@ -587,11 +597,25 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
||||
.Concat([PasteAsPlainTextShortcut, AdvancedPasteUIShortcut, PasteAsMarkdownShortcut, PasteAsJsonShortcut])
|
||||
.Any(hotkey => WarnHotkeys.Contains(hotkey.ToString()));
|
||||
|
||||
public bool IsAdditionalActionConflictingCopyShortcut =>
|
||||
_additionalActions.GetAllActions()
|
||||
.OfType<AdvancedPasteAdditionalAction>()
|
||||
.Select(additionalAction => additionalAction.Shortcut)
|
||||
.Any(hotkey => WarnHotkeys.Contains(hotkey.ToString()));
|
||||
public bool IsAdditionalActionConflictingCopyShortcut
|
||||
{
|
||||
get
|
||||
{
|
||||
var shortcuts = _additionalActions.GetAllActions()
|
||||
.OfType<AdvancedPasteAdditionalAction>()
|
||||
.Select(additionalAction => additionalAction.Shortcut)
|
||||
.ToList();
|
||||
|
||||
// The coaching shortcut is a separately-registered hotkey; include it when active.
|
||||
var fixSpelling = _additionalActions.FixSpellingAndGrammar;
|
||||
if (fixSpelling.CoachingEnabled && fixSpelling.CoachingShortcut is { Code: not 0 })
|
||||
{
|
||||
shortcuts.Add(fixSpelling.CoachingShortcut);
|
||||
}
|
||||
|
||||
return shortcuts.Any(hotkey => WarnHotkeys.Contains(hotkey.ToString()));
|
||||
}
|
||||
}
|
||||
|
||||
private void NotifySettingsChanged()
|
||||
{
|
||||
@@ -781,6 +805,25 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
public void SetAsDefaultProvider(PasteAIProviderDefinition provider)
|
||||
{
|
||||
if (provider is null || string.IsNullOrEmpty(provider.Id))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var config = PasteAIConfiguration;
|
||||
if (config is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
config.ActiveProviderId = provider.Id;
|
||||
SyncProviderActiveFlags(config);
|
||||
SaveAndNotifySettings();
|
||||
OnPropertyChanged(nameof(PasteAIConfiguration));
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (!_disposed)
|
||||
@@ -1083,7 +1126,9 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
||||
{
|
||||
SaveAndNotifySettings();
|
||||
|
||||
if (e.PropertyName == nameof(AdvancedPasteAdditionalAction.Shortcut))
|
||||
if (e.PropertyName is nameof(AdvancedPasteAdditionalAction.Shortcut)
|
||||
or nameof(AdvancedPasteAdditionalAction.CoachingShortcut)
|
||||
or nameof(AdvancedPasteAdditionalAction.CoachingEnabled))
|
||||
{
|
||||
OnPropertyChanged(nameof(IsAdditionalActionConflictingCopyShortcut));
|
||||
}
|
||||
@@ -1324,7 +1369,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
||||
return true;
|
||||
}
|
||||
|
||||
if (existing?.ModerationEnabled != updated?.ModerationEnabled || existing?.EnableAdvancedAI != updated?.EnableAdvancedAI || existing?.IsActive != updated?.IsActive)
|
||||
if (existing?.ModerationEnabled != updated?.ModerationEnabled || existing?.EnableAdvancedAI != updated?.EnableAdvancedAI)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -1411,6 +1456,12 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
||||
{
|
||||
if (sender is PasteAIProviderDefinition provider)
|
||||
{
|
||||
// IsActive is a UI-only (JsonIgnore) flag; don't save when it changes.
|
||||
if (string.Equals(e.PropertyName, nameof(PasteAIProviderDefinition.IsActive), StringComparison.Ordinal))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// When service type changes we may need to update credentials entry names.
|
||||
if (string.Equals(e.PropertyName, nameof(PasteAIProviderDefinition.ServiceType), StringComparison.Ordinal))
|
||||
{
|
||||
@@ -1427,12 +1478,14 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
||||
if (string.Equals(e.PropertyName, nameof(PasteAIConfiguration.Providers), StringComparison.Ordinal))
|
||||
{
|
||||
SubscribeToPasteAIProviders(PasteAIConfiguration);
|
||||
SyncProviderActiveFlags(PasteAIConfiguration);
|
||||
SaveAndNotifySettings();
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.Equals(e.PropertyName, nameof(PasteAIConfiguration.ActiveProviderId), StringComparison.Ordinal))
|
||||
{
|
||||
SyncProviderActiveFlags(PasteAIConfiguration);
|
||||
SaveAndNotifySettings();
|
||||
}
|
||||
}
|
||||
@@ -1448,9 +1501,32 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
||||
|
||||
pasteConfig.Providers ??= new ObservableCollection<PasteAIProviderDefinition>();
|
||||
|
||||
SyncProviderActiveFlags(pasteConfig);
|
||||
SubscribeToPasteAIProviders(pasteConfig);
|
||||
}
|
||||
|
||||
private static void SyncProviderActiveFlags(PasteAIConfiguration config)
|
||||
{
|
||||
if (config?.Providers is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var activeId = config.ActiveProviderId;
|
||||
|
||||
// If no explicit active ID, default to the first provider
|
||||
if (string.IsNullOrEmpty(activeId) && config.Providers.Count > 0)
|
||||
{
|
||||
activeId = config.Providers[0].Id;
|
||||
config.ActiveProviderId = activeId;
|
||||
}
|
||||
|
||||
foreach (var provider in config.Providers)
|
||||
{
|
||||
provider.IsActive = !string.IsNullOrEmpty(activeId) && string.Equals(provider.Id, activeId, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
|
||||
private static string RetrieveCredentialValue(string credentialResource, string credentialUserName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(credentialResource) || string.IsNullOrWhiteSpace(credentialUserName))
|
||||
|
||||
Reference in New Issue
Block a user